Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,27 @@

### Fixed

- **"Already shown, nothing has changed since" is checked, not assumed.** A
file changed outside the file tools (a bash `sed -i` or `printf >`, a
generator, the person's editor) was still answered with the pointer on a
re-read, so the model was told its stale copy was current. Each read now
takes the file's size, mtime and, under 1 MB, a content hash, before and
after reading it (as the worker, under privilege separation); the pointer
is sent only when before, after and the last time it was shown all match,
so a write that lands during the read is caught too. A fingerprint that
cannot be taken counts as a change. A deleted file gives its read error,
never a pointer. At 1 MB and over the check is size and mtime only, so on a
filesystem with a coarse mtime (HFS+, FAT, some network mounts) a same-size
edit within one tick can still be missed.
- **A re-read after an edit returns the file, whatever the path's spelling.**
The engine recorded which lines of a file the model had been shown under the
path as spelled, so `read_file dur.py`, then `edit_file ./dur.py`, then
`read_file dur.py` was answered "you have already been shown … nothing has
changed since", while the transcript had already elided the old copy: the
model held no copy of the file it had just edited. The engine and the
transcript now key files through one normaliser (`canonPath`), which also
treats `sub/../dur.py` and an absolute path inside the workspace as the same
file.
treats `sub/../dur.py`, an absolute path inside the workspace and, on
Windows, `src\dur.py` as the same file.
- **Anthropic prices are per model version, checked 2026-10-08.** The table
priced by family: Opus 5.5 was billed at $5 / $25 per MTok instead of
$4 / $20, Sonnet 5 and 5.5 at $3 / $15 instead of $2 / $10, Opus 4 / 4.1 at
Expand Down
49 changes: 42 additions & 7 deletions src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import {
applyEdit,
diffSyntaxIn,
diffSyntaxRefusal,
fileFingerprint,
isPatchPath,
formatListing,
formatMatches,
Expand Down Expand Up @@ -2036,8 +2037,12 @@ type ToolContext = {
userText: string;
confirm: Confirm;
log?: Journal;
/** Line ranges of each file already shown, so a re-read can be named as one. */
shown: Map<string, { from: number; to: number }[]>;
/**
* Line ranges of each file already shown, so a re-read can be named as one,
* and the file's fingerprint when they were shown: the pointer says nothing
* has changed since, and that is checked, not assumed.
*/
shown: Map<string, { ranges: { from: number; to: number }[]; fp: string | null }>;
/** The last result of each distinct call, so an identical one can be pointed at. */
answered: Map<string, { step: number; sha: string }>;
};
Expand Down Expand Up @@ -2477,7 +2482,7 @@ export class Engine {
this.repoMapText = cfg.repoMap ?? "";
this.briefText = cfg.brief ?? "";
for (const p of cfg.readOnly ?? []) this.readOnlyPaths.add(p);
this.transcript = new Transcript(this.systemPrompt());
this.transcript = new Transcript(this.systemPrompt(), this.cwd);
this.barHash = barFingerprint(this.cwd);
// The key molt was handed is the one secret it can mask exactly.
cfg.journal?.protect(cfg.apiKey, env("API_KEY"));
Expand Down Expand Up @@ -2639,7 +2644,7 @@ export class Engine {
}

reset(): void {
this.transcript = new Transcript(this.systemPrompt());
this.transcript = new Transcript(this.systemPrompt(), this.cwd);
this.ledger = [];
this.archivedWrites = 0;
this.sessionArchives = new Set();
Expand Down Expand Up @@ -3526,6 +3531,22 @@ export class Engine {
return wfs ? wfs.exists(abs) : existsSync(abs);
}

/**
* fileFingerprint (src/files.ts) of a file, as the file tools see it: under
* privilege separation the worker takes it. Never throws: a read that
* succeeded must not turn into a failed step because its fingerprint could
* not be taken (a dying helper, EACCES, a swap to a directory). Null is
* "unknown" and never matches.
*/
private async fingerprint(abs: string): Promise<string | null> {
try {
const wfs = this.workerFs;
return wfs ? await wfs.fingerprint(abs) : fileFingerprint(abs);
} catch {
return null;
}
}

private async fileSha(abs: string): Promise<string | null> {
const wfs = this.workerFs;
return wfs ? wfs.sha256(abs) : sha256Of(abs);
Expand Down Expand Up @@ -5779,6 +5800,8 @@ export class Engine {
// tool is not the tool being slow, and folding the two together
// would make every gated call look like one.
let durationMs: number | undefined;
/** read_file only: the file's fingerprint just before it was read. */
let fpBefore: string | null = null;
if (!allowed) {
result = outOfTime
? `[molt: the time budget for this turn is up — ${name} was not run, and no further ` +
Expand All @@ -5796,6 +5819,10 @@ export class Engine {
id: callId,
args: redact(capture(call.rawArgs), this.secrets()),
};
// Taken before the read as well as after it: a write that lands
// between the read and a fingerprint taken only afterwards would be
// recorded as the state of what the model was shown.
if (name === "read_file") fpBefore = await this.fingerprint(resolve(this.cwd, String(args.path ?? "")));
const toolStartedAt = Date.now();
// Scoped to this one call, so ctrl+C reaches the command that is
// actually running and nothing that ran before it.
Expand Down Expand Up @@ -5843,7 +5870,15 @@ export class Engine {
// Keyed by the file, not the spelling: an edit of `./dur.py` has to
// clear what a read of `dur.py` recorded.
const file = canonPath(path, this.cwd);
const covered = ctx.shown.get(file) ?? [];
// "Nothing has changed since" is a claim about the file, and bash,
// a generator or the person's editor can change it behind the file
// tools' back. The file is the same only if it was the same before
// this read, after it, and when it was last shown; null is unknown
// and never the same. Anything else and what was shown is history.
const fpAfter = await this.fingerprint(resolve(this.cwd, path));
const fp = fpBefore !== null && fpBefore === fpAfter ? fpBefore : null;
const entry = ctx.shown.get(file);
const covered = entry && fp !== null && entry.fp === fp ? entry.ranges : [];
// How much of this window is genuinely new. Containment alone is
// too strict: a read that overlaps an earlier one by 99% and
// runs three lines past it is not contained, and a model
Expand All @@ -5867,7 +5902,7 @@ export class Engine {
repeatedHere = true;
} else {
covered.push({ from, to });
ctx.shown.set(file, covered);
ctx.shown.set(file, { ranges: covered, fp });
}
}

Expand Down Expand Up @@ -6194,7 +6229,7 @@ export class Engine {
* actually matters — "has this already been shown?" — rather than "is this
* byte-identical to something?".
*/
const shown = new Map<string, { from: number; to: number }[]>();
const shown: ToolContext["shown"] = new Map();
/** Consecutive steps in which nothing new came back. */
let dryStreak = 0;
/** Consecutive assistant turns that arrived with nothing in them. */
Expand Down
27 changes: 27 additions & 0 deletions src/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,33 @@ export function formatListing(
* identical — would have to be a same-length edit written within the
* filesystem's timestamp granularity, and molt's own writes always move mtime.
*/
/** Files under this size are content-hashed by fileFingerprint; larger ones are stat-only. */
export const FILE_FP_HASH_MAX_BYTES = 1024 * 1024;

/**
* One file's fingerprint, for "has it changed since it was shown": size and
* mtime, plus a sha256 of the content under FILE_FP_HASH_MAX_BYTES, which
* catches a same-size edit inside one mtime tick. At that size and above it
* is size and mtime only, so on a filesystem with a coarse mtime (HFS+ 1 s,
* FAT 2 s, some network mounts) a same-size edit inside one tick is missed.
* Null when it cannot be taken (no such file, not a regular file, no access):
* callers treat null as unknown, never as a match.
*
* Run by Maat on the host and by the worker's fs-helper under privilege
* separation, so both take the same fingerprint at the same cost.
*/
export function fileFingerprint(abs: string): string | null {
try {
const st = statSync(abs);
if (!st.isFile()) return null;
const stamp = `${st.size}:${st.mtimeMs}`;
if (st.size >= FILE_FP_HASH_MAX_BYTES) return stamp;
return `${stamp}:${createHash("sha256").update(readFileSync(abs)).digest("hex")}`;
} catch {
return null;
}
}

/** Makes every unreadable-scope signature unique, even within a millisecond. */
let unreadable = 0;

Expand Down
3 changes: 2 additions & 1 deletion src/fs-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
writeFileSync,
} from "node:fs";
import { dirname, join } from "node:path";
import { grepFiles, walkAsync, type WalkOptions } from "./files.js";
import { fileFingerprint, grepFiles, walkAsync, type WalkOptions } from "./files.js";
import { inspectDir, inspectFile } from "./inspect.js";

type Op = (...args: never[]) => unknown;
Expand All @@ -38,6 +38,7 @@ const ops: Record<string, Op> = {
exists: (p: string) => existsSync(p),
isDir: (p: string) => statSync(p).isDirectory(),
sha256: (p: string) => (existsSync(p) ? createHash("sha256").update(readFileSync(p)).digest("hex") : null),
fingerprint: (p: string) => fileFingerprint(p),
walk: (p: string, o: WalkOptions) => walkAsync(p, o),
grep: (p: string, pattern: string, o: { glob?: string; ignoreCase?: boolean }) => grepFiles(p, pattern, o),
inspectDir: (p: string, rel: string) => inspectDir(p, rel),
Expand Down
5 changes: 5 additions & 0 deletions src/privsep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ export type WorkerFs = {
exists(abs: string): Promise<boolean>;
isDir(abs: string): Promise<boolean>;
sha256(abs: string): Promise<string | null>;
/** fileFingerprint (src/files.ts), taken as the worker. */
fingerprint(abs: string): Promise<string | null>;
walk(abs: string, opts: WalkOptions): Promise<WalkResult>;
grep(abs: string, pattern: string, opts: { glob?: string; ignoreCase?: boolean }): Promise<GrepResult>;
inspectDir(abs: string, rel: string): Promise<string>;
Expand Down Expand Up @@ -1233,6 +1235,9 @@ class FsHelper implements WorkerFs {
sha256(abs: string) {
return this.call<string | null>("sha256", abs);
}
fingerprint(abs: string) {
return this.call<string | null>("fingerprint", abs);
}
walk(abs: string, opts: WalkOptions) {
const { skip: _skip, ...plain } = opts;
return this.call<WalkResult>("walk", abs, plain);
Expand Down
38 changes: 26 additions & 12 deletions src/transcript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* No filesystem access here — archiving lives in archive.ts so this whole
* module stays pure and testable.
*/
import { isAbsolute, posix, relative } from "node:path";
import * as nodePath from "node:path";
import { parseLenient } from "./lenient-json.js";
import { estTokens, type Bom, type Msg } from "./types.js";

Expand All @@ -34,21 +34,28 @@ const SHOWN_PREFIX = "[molt: you have already been shown";
* One spelling per file, for every map that is keyed by path: `dur.py`,
* `./dur.py` and `sub/../dur.py` are one file, and with `root` (the
* workspace) so is its absolute path. A path outside `root` stays absolute.
* An empty path stays empty: it names no file.
* An empty path stays empty: it names no file. Where the separator is `\`
* (Windows), `src\dur.py` and `src/dur.py` are one file too; `path` is the
* platform's path module, injectable so that case can be tested anywhere.
*
* The engine's read-coverage map and this module's elision have to agree on
* it. When they did not, an edit of `./dur.py` cleared the wrong key, the
* transcript elided the old copy, and the re-read was answered "you have
* already been shown it": the model held no copy of the file it had edited.
*/
export function canonPath(path: string, root?: string): string {
if (!path) return path;
let p = path;
if (root && isAbsolute(p)) {
const rel = relative(root, p);
if (rel !== ".." && !rel.startsWith("../") && !isAbsolute(rel)) p = rel || ".";
export function canonPath(
file: string,
root?: string,
path: Pick<typeof nodePath, "isAbsolute" | "relative" | "sep"> = nodePath,
): string {
if (!file) return file;
const slashes = (s: string) => (path.sep === "\\" ? s.replace(/\\/g, "/") : s);
let p = file;
if (root && path.isAbsolute(p)) {
const rel = slashes(path.relative(root, p));
if (rel !== ".." && !rel.startsWith("../") && !path.isAbsolute(rel)) p = rel || ".";
}
return posix.normalize(p).replace(/^(?:\.\/)+/, "") || ".";
return nodePath.posix.normalize(slashes(p)).replace(/^(?:\.\/)+/, "") || ".";
}

/**
Expand Down Expand Up @@ -120,7 +127,14 @@ export class Transcript {
/** See SHED_MIN_FREE. 0 restores the plain cut on user turns. */
shedMinFree = SHED_MIN_FREE;

constructor(systemPrompt: string) {
/**
* @param root The workspace, so an absolute path inside it keys the same
* file as its relative spelling, exactly as the engine keys it.
*/
constructor(
systemPrompt: string,
private readonly root?: string,
) {
this.system = { role: "system", content: systemPrompt };
}

Expand Down Expand Up @@ -633,11 +647,11 @@ export class Transcript {
// pointer to an earlier copy is not a read: the copy it points at
// is the one a write must invalidate.
const read = SIMPLE_READ.exec(cmd);
if (read && !pointer) noteRead(canonPath(read[1]!), `bash:${cmd}`, at);
if (read && !pointer) noteRead(canonPath(read[1]!, this.root), `bash:${cmd}`, at);
return;
}

const path = canonPath(String(args.path ?? ""));
const path = canonPath(String(args.path ?? ""), this.root);
if (!path) return;

if (call.function.name === "read_file") {
Expand Down
Loading
Loading