From c0ec4ee30a5d645b7b492b3744e6d02c0e9b552b Mon Sep 17 00:00:00 2001 From: Tyler Skelton Date: Thu, 8 Oct 2026 04:40:25 -0700 Subject: [PATCH 1/2] Check the file before saying it has not changed since it was shown A re-read of a file that bash (sed -i, printf >), a generator or an editor had changed was still answered "you have already been shown ... nothing has changed since", so the model was told its stale copy was current. Each shown entry now carries a fingerprint of the file: size and mtimeMs, plus a sha256 of the content under 1 MB (catches a same-size edit within one mtime tick). It is re-taken on every read_file before a pointer is sent; if it moved, the old coverage is dropped and the read returns the file. Under privilege separation the worker's sha256 is the fingerprint. A deleted file gives its read error, not a pointer. --- CHANGELOG.md | 7 ++++ src/engine.ts | 43 +++++++++++++++++++++--- test/lean-fixes.test.ts | 73 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 117 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41733d6..5100617 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -149,6 +149,13 @@ ### 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 + records the file's size, mtime and, under 1 MB, a content hash; the + fingerprint is re-taken before a pointer is sent, and a changed file is + returned in full. A deleted file gives its read error, never a pointer. - **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 diff --git a/src/engine.ts b/src/engine.ts index c4112f6..d90fd62 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -592,6 +592,8 @@ export function historyBudget(window: number, fixedEst: number, scale: number): } export const TOOL_RESULT_MAX_BYTES = 8192; +/** Files under this size are hashed for the "already shown" check; larger ones are stat-only. */ +const SHOWN_HASH_MAX_BYTES = 1024 * 1024; /** * How much of a file one `read_file` may return. * @@ -2035,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; + /** + * 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; /** The last result of each distinct call, so an identical one can be pointed at. */ answered: Map; }; @@ -3482,6 +3488,27 @@ export class Engine { return wfs ? wfs.exists(abs) : existsSync(abs); } + /** + * A cheap fingerprint of a file, for "has it changed since it was shown": + * size and mtime, and a content hash when the file is under 1 MB, which + * catches a same-size edit inside one mtime tick. Under privilege + * separation the worker's hash, as the worker sees the file. Null when + * there is no such file. + */ + private async fingerprint(abs: string): Promise { + const wfs = this.workerFs; + if (wfs) return wfs.sha256(abs); + try { + const st = statSync(abs); + if (!st.isFile()) return null; + const stamp = `${st.size}:${st.mtimeMs}`; + if (st.size >= SHOWN_HASH_MAX_BYTES) return stamp; + return `${stamp}:${createHash("sha256").update(readFileSync(abs)).digest("hex")}`; + } catch { + return null; + } + } + private async fileSha(abs: string): Promise { const wfs = this.workerFs; return wfs ? wfs.sha256(abs) : sha256Of(abs); @@ -5762,7 +5789,13 @@ 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. Re-take the fingerprint; if it moved, what was shown + // is history and this read is new. + const fp = await this.fingerprint(resolve(this.cwd, path)); + const entry = ctx.shown.get(file); + const covered = entry && 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 @@ -5786,7 +5819,7 @@ export class Engine { repeatedHere = true; } else { covered.push({ from, to }); - ctx.shown.set(file, covered); + ctx.shown.set(file, { ranges: covered, fp }); } } @@ -6113,7 +6146,7 @@ export class Engine { * actually matters — "has this already been shown?" — rather than "is this * byte-identical to something?". */ - const shown = new Map(); + 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. */ diff --git a/test/lean-fixes.test.ts b/test/lean-fixes.test.ts index 0f6f174..8d4486f 100644 --- a/test/lean-fixes.test.ts +++ b/test/lean-fixes.test.ts @@ -13,7 +13,7 @@ * marker saying the current copy was further down. It was not. */ import assert from "node:assert/strict"; -import { readFileSync, writeFileSync } from "node:fs"; +import { readFileSync, utimesSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { describe, it } from "node:test"; import { Engine } from "../src/engine.js"; @@ -350,3 +350,74 @@ describe("shown map: one spelling per file", () => { assert.equal(t.elideSupersededReads().elided, 1); }); }); + +/** + * "You have already been shown … nothing has changed since" was a claim the + * engine never checked. A bash `sed -i`, a generator or the person's editor + * changes the file behind the file tools' back, and the re-read was still + * answered with the pointer. Each entry now carries a fingerprint of the file + * (size, mtime, and a hash under 1 MB), re-taken before a pointer is sent. + */ +describe("shown map: the pointer is checked against the file", () => { + async function reread(change: (dir: string) => object, setup?: (dir: string) => void) { + const ws = workspace(); + try { + writeFileSync(join(ws.dir, "x.py"), "value = 1 # OLD_MARK\n"); + setup?.(ws.dir); + const p = scriptedProvider([ + { calls: [{ name: "read_file", args: { path: "x.py" } }] }, + { calls: [change(ws.dir)] }, + { calls: [{ name: "read_file", args: { path: "x.py" } }] }, + { text: "Done." }, + ] as never); + const e = new Engine({ baseUrl: "http://p.test/v1", model: "m", cwd: ws.dir, fetchFn: p.fetchFn, bar: null, stream: false, autonomy: "high", maxSteps: 0 }); + await drain(e.run("look at x.py", allowAll)); + const tools = (JSON.parse(p.bodies.at(-1)!) as { messages: { role: string; content: string }[] }).messages.filter((m) => m.role === "tool"); + return tools.at(-1)!.content; + } finally { + ws.cleanup(); + } + } + + it("bash sed -i changes the file: the re-read returns the new contents", async () => { + const got = await reread(() => ({ name: "bash", args: { command: "sed -i.bak 's/OLD_MARK/NEW_MARK/' x.py" } })); + assert.doesNotMatch(got, /already been shown/); + assert.match(got, /NEW_MARK/); + }); + + it("bash printf > x.py changes the file: the re-read returns the new contents", async () => { + const got = await reread(() => ({ name: "bash", args: { command: "printf 'value = 2 # NEW_MARK\\n' > x.py" } })); + assert.doesNotMatch(got, /already been shown/); + assert.match(got, /NEW_MARK/); + }); + + it("same size, same mtime: the hash catches the change", async () => { + // Both versions are the same length and stamped with the same whole-second + // mtime, so size and mtime match exactly and only the content differs. + const T = 1_700_000_000; + const got = await reread( + () => ({ + name: "bash", + args: { + command: + `node -e "const fs=require('fs');fs.writeFileSync('x.py','value = 1 # NEW_MARK\\n');` + + `fs.utimesSync('x.py',${T},${T})"`, + }, + }), + (dir) => utimesSync(join(dir, "x.py"), T, T), + ); + assert.doesNotMatch(got, /already been shown/); + assert.match(got, /NEW_MARK/); + }); + + it("an unchanged file still gets the pointer", async () => { + const got = await reread(() => ({ name: "bash", args: { command: "true" } })); + assert.match(got, /already been shown/); + }); + + it("a deleted file gives an error, not a pointer", async () => { + const got = await reread(() => ({ name: "bash", args: { command: "rm x.py" } })); + assert.doesNotMatch(got, /already been shown/); + assert.match(got, /no such file|ENOENT|not found/i); + }); +}); From 8e7a16e625b3c79c90816503c05ffbc98ad71099 Mon Sep 17 00:00:00 2001 From: Tyler Skelton Date: Thu, 8 Oct 2026 05:31:29 -0700 Subject: [PATCH 2/2] Fingerprint the file around the read; key the transcript by the workspace Review of #55 and #54: - The fingerprint is taken before and after read_file, and the "already shown" pointer is sent only when before, after and the stored value all match. A write landing during the read is no longer recorded as what the model was shown. - A null fingerprint is unknown and never matches. - Fingerprinting never throws: a worker helper that fails turns into an unknown fingerprint, not a failed step. - One fileFingerprint (src/files.ts), taken by the worker's fs-helper as a new op, so the 1 MB hash cap applies under privilege separation too. - The 1 MB-and-over stat-only limit is stated in the docs and CHANGELOG. - Transcript takes the workspace root, so its elision folds absolute paths the way the engine does. - canonPath treats backslashes as separators where sep is a backslash (and ..\ as outside); path module injectable, tested with path.win32. --- CHANGELOG.md | 15 +++-- src/engine.ts | 42 +++++++------- src/files.ts | 27 +++++++++ src/fs-helper.ts | 3 +- src/privsep.ts | 5 ++ src/transcript.ts | 38 ++++++++---- test/lean-fixes.test.ts | 126 +++++++++++++++++++++++++++++++++++++++- 7 files changed, 217 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5100617..1ce1d5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -153,9 +153,14 @@ 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 - records the file's size, mtime and, under 1 MB, a content hash; the - fingerprint is re-taken before a pointer is sent, and a changed file is - returned in full. A deleted file gives its read error, never a pointer. + 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 @@ -163,8 +168,8 @@ 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 diff --git a/src/engine.ts b/src/engine.ts index d90fd62..845f832 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -67,6 +67,7 @@ import { applyEdit, diffSyntaxIn, diffSyntaxRefusal, + fileFingerprint, isPatchPath, formatListing, formatMatches, @@ -592,8 +593,6 @@ export function historyBudget(window: number, fixedEst: number, scale: number): } export const TOOL_RESULT_MAX_BYTES = 8192; -/** Files under this size are hashed for the "already shown" check; larger ones are stat-only. */ -const SHOWN_HASH_MAX_BYTES = 1024 * 1024; /** * How much of a file one `read_file` may return. * @@ -2457,7 +2456,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")); @@ -2602,7 +2601,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(); @@ -3489,21 +3488,16 @@ export class Engine { } /** - * A cheap fingerprint of a file, for "has it changed since it was shown": - * size and mtime, and a content hash when the file is under 1 MB, which - * catches a same-size edit inside one mtime tick. Under privilege - * separation the worker's hash, as the worker sees the file. Null when - * there is no such file. + * 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 { - const wfs = this.workerFs; - if (wfs) return wfs.sha256(abs); try { - const st = statSync(abs); - if (!st.isFile()) return null; - const stamp = `${st.size}:${st.mtimeMs}`; - if (st.size >= SHOWN_HASH_MAX_BYTES) return stamp; - return `${stamp}:${createHash("sha256").update(readFileSync(abs)).digest("hex")}`; + const wfs = this.workerFs; + return wfs ? await wfs.fingerprint(abs) : fileFingerprint(abs); } catch { return null; } @@ -5725,6 +5719,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 ` + @@ -5742,6 +5738,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. @@ -5791,11 +5791,13 @@ export class Engine { const file = canonPath(path, this.cwd); // "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. Re-take the fingerprint; if it moved, what was shown - // is history and this read is new. - const fp = await this.fingerprint(resolve(this.cwd, path)); + // 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 && entry.fp === fp ? entry.ranges : []; + 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 diff --git a/src/files.ts b/src/files.ts index adf1f2f..1690375 100644 --- a/src/files.ts +++ b/src/files.ts @@ -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; diff --git a/src/fs-helper.ts b/src/fs-helper.ts index f16a132..07dbef1 100644 --- a/src/fs-helper.ts +++ b/src/fs-helper.ts @@ -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; @@ -38,6 +38,7 @@ const ops: Record = { 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), diff --git a/src/privsep.ts b/src/privsep.ts index 4d249d4..579b1f7 100644 --- a/src/privsep.ts +++ b/src/privsep.ts @@ -90,6 +90,8 @@ export type WorkerFs = { exists(abs: string): Promise; isDir(abs: string): Promise; sha256(abs: string): Promise; + /** fileFingerprint (src/files.ts), taken as the worker. */ + fingerprint(abs: string): Promise; walk(abs: string, opts: WalkOptions): Promise; grep(abs: string, pattern: string, opts: { glob?: string; ignoreCase?: boolean }): Promise; inspectDir(abs: string, rel: string): Promise; @@ -1233,6 +1235,9 @@ class FsHelper implements WorkerFs { sha256(abs: string) { return this.call("sha256", abs); } + fingerprint(abs: string) { + return this.call("fingerprint", abs); + } walk(abs: string, opts: WalkOptions) { const { skip: _skip, ...plain } = opts; return this.call("walk", abs, plain); diff --git a/src/transcript.ts b/src/transcript.ts index b80af80..6b37d17 100644 --- a/src/transcript.ts +++ b/src/transcript.ts @@ -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"; @@ -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 = 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(/^(?:\.\/)+/, "") || "."; } /** @@ -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 }; } @@ -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") { diff --git a/test/lean-fixes.test.ts b/test/lean-fixes.test.ts index 8d4486f..fa49bfa 100644 --- a/test/lean-fixes.test.ts +++ b/test/lean-fixes.test.ts @@ -13,10 +13,13 @@ * marker saying the current copy was further down. It was not. */ import assert from "node:assert/strict"; +import { spawnSync } from "node:child_process"; import { readFileSync, utimesSync, writeFileSync } from "node:fs"; -import { join } from "node:path"; +import { join, win32 } from "node:path"; +import { fileURLToPath } from "node:url"; import { describe, it } from "node:test"; import { Engine } from "../src/engine.js"; +import { FILE_FP_HASH_MAX_BYTES, fileFingerprint } from "../src/files.js"; import { ELIDED_PREFIX, SHED_MIN_FREE, Transcript, canonPath } from "../src/transcript.js"; import { allowAll, drain, scriptedProvider, toolCall, workspace } from "./helpers.js"; @@ -341,6 +344,29 @@ describe("shown map: one spelling per file", () => { assert.equal(canonPath(""), ""); }); + it("canonPath on Windows: backslashes are separators, and ..\\ is outside", () => { + assert.equal(canonPath("C:\\ws\\src\\dur.py", "C:\\ws", win32), "src/dur.py"); + assert.equal(canonPath("src\\dur.py", "C:\\ws", win32), "src/dur.py"); + assert.equal(canonPath(".\\src\\..\\dur.py", "C:\\ws", win32), "dur.py"); + assert.equal(canonPath("C:\\ws", "C:\\ws", win32), "."); + assert.equal(canonPath("C:\\ws2\\dur.py", "C:\\ws", win32), "C:/ws2/dur.py"); + assert.equal(canonPath("D:\\ws\\dur.py", "C:\\ws", win32), "D:/ws/dur.py"); + // relative("C:\\ws\\a", "C:\\ws\\b") is "..\\b": outside, so it stays absolute. + assert.equal(canonPath("C:\\ws\\b\\x.py", "C:\\ws\\a", win32), "C:/ws/b/x.py"); + // On POSIX a backslash is a file-name character, not a separator. + assert.equal(canonPath("src\\dur.py", "/ws"), "src\\dur.py"); + }); + + it("the transcript folds an absolute path inside its workspace: read /ws/dur.py, edit dur.py", () => { + const t = new Transcript("S", "/ws"); + t.push({ role: "assistant", content: null, tool_calls: [toolCall("read_file", { path: "/ws/dur.py" }, "a")] }); + t.push({ role: "tool", tool_call_id: "a", content: `DUR_OLD\n${"x".repeat(2000)}` }); + t.push({ role: "assistant", content: null, tool_calls: [toolCall("edit_file", { path: "dur.py", old_text: "x", new_text: "y" }, "e")] }); + t.push({ role: "tool", tool_call_id: "e", content: "edited" }); + assert.equal(t.elideSupersededReads().elided, 1); + assert.ok(!JSON.stringify(t.wire()).includes("DUR_OLD")); + }); + it("the transcript keys elision the same way: a write to sub/../dur.py elides the read of dur.py", () => { const t = new Transcript("S"); t.push({ role: "assistant", content: null, tool_calls: [toolCall("read_file", { path: "dur.py" }, "a")] }); @@ -421,3 +447,101 @@ describe("shown map: the pointer is checked against the file", () => { assert.match(got, /no such file|ENOENT|not found/i); }); }); + +describe("shown map: the fingerprint is taken around the read", () => { + async function twoReads(rig: (e: Engine, dir: string) => void) { + const ws = workspace(); + try { + writeFileSync(join(ws.dir, "x.py"), "value = 1 # OLD_MARK\n"); + const p = scriptedProvider([ + { calls: [{ name: "read_file", args: { path: "x.py" } }] }, + // A different call over the same lines: only the coverage pointer + // ("already been shown") can answer it, not the byte-compared + // same-call pointer. + { calls: [{ name: "read_file", args: { path: "x.py", limit: 50 } }] }, + { text: "Done." }, + ] as never); + const e = new Engine({ baseUrl: "http://p.test/v1", model: "m", cwd: ws.dir, fetchFn: p.fetchFn, bar: null, stream: false, autonomy: "high", maxSteps: 0 }); + rig(e, ws.dir); + const events = await drain(e.run("look at x.py", allowAll)); + const tools = (JSON.parse(p.bodies.at(-1)!) as { messages: { role: string; content: string }[] }).messages.filter((m) => m.role === "tool"); + return { first: tools.at(-2)!.content, second: tools.at(-1)!.content, events: JSON.stringify(events) }; + } finally { + ws.cleanup(); + } + } + + it("a write that lands right after the read is not recorded as what was shown", async () => { + const { first, second } = await twoReads((e, dir) => { + const inner = e as unknown as { readText(abs: string): Promise }; + const orig = inner.readText.bind(e); + let once = true; + inner.readText = async (abs: string) => { + const text = await orig(abs); + if (once && abs.endsWith("x.py")) { + once = false; + writeFileSync(join(dir, "x.py"), "value = 2 # NEW_MARK\n"); + } + return text; + }; + }); + assert.match(first, /OLD_MARK/); + assert.doesNotMatch(second, /already been shown/, "the pointer claimed nothing had changed"); + assert.match(second, /NEW_MARK/); + }); + + it("control: the same shape, with nothing changing, gets the pointer", async () => { + const { second } = await twoReads(() => {}); + assert.match(second, /already been shown/); + }); + + it("an unknown (null) fingerprint never matches: no pointer", async () => { + const { second } = await twoReads((e) => { + (e as unknown as { fingerprint(abs: string): Promise }).fingerprint = async () => null; + }); + assert.doesNotMatch(second, /already been shown/); + assert.match(second, /OLD_MARK/); + }); + + it("a worker whose fingerprint call rejects: the read still succeeds, with no pointer", async () => { + const { first, second } = await twoReads((e) => { + Object.defineProperty(e, "workerFs", { + get: () => ({ + read: async (abs: string) => readFileSync(abs, "utf8"), + fingerprint: async () => { + throw new Error("helper died"); + }, + }), + }); + }); + assert.match(first, /OLD_MARK/); + assert.match(second, /OLD_MARK/); + assert.doesNotMatch(second, /already been shown|helper died|tool error/); + }); + + it("the worker's fs-helper takes the same fingerprint, and hashes only under the cap", () => { + const ws = workspace(); + try { + const small = join(ws.dir, "small.txt"); + const big = join(ws.dir, "big.txt"); + writeFileSync(small, "hello\n"); + writeFileSync(big, Buffer.alloc(FILE_FP_HASH_MAX_BYTES + 1, 97)); + const helper = fileURLToPath(new URL("../src/fs-helper.js", import.meta.url)); + const input = [small, big, join(ws.dir, "missing")] + .map((f, i) => JSON.stringify({ id: i, op: "fingerprint", args: [f] })) + .join("\n"); + const r = spawnSync(process.execPath, [helper], { input: input + "\n", encoding: "utf8" }); + const got = new Map(r.stdout.trim().split("\n").map((l) => { + const m = JSON.parse(l) as { id: number; value: string | null }; + return [m.id, m.value] as const; + })); + assert.equal(got.get(0), fileFingerprint(small)); + assert.equal(got.get(0)!.split(":").length, 3, "a small file is hashed"); + assert.equal(got.get(1), fileFingerprint(big)); + assert.equal(got.get(1)!.split(":").length, 2, "a file over the cap is stat-only"); + assert.equal(got.get(2), null); + } finally { + ws.cleanup(); + } + }); +});