diff --git a/CHANGELOG.md b/CHANGELOG.md index 7023c6a..41733d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -149,6 +149,15 @@ ### Fixed +- **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. - **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 513109a..c4112f6 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -118,7 +118,7 @@ import { takeTurn } from "./localgate.js"; import { parseLenient } from "./lenient-json.js"; import { LONG_RATE_LIMIT_MS, longQuotaText, rateLimitResetAt, untilText, providerErrorText, normalizeMessage, readStream, transientProviderError, type ProviderError, type StreamAccumulator, type Usage } from "./stream.js"; import { Fragments, SafeStream } from "./live.js"; -import { Transcript, excerpt, toolDetail } from "./transcript.js"; +import { Transcript, canonPath, excerpt, toolDetail } from "./transcript.js"; import { acpAgentFor, acpHealth, acpModels, AcpSession, backendStallMs, isAcp } from "./acp.js"; import { type BackendSession, type ToolRunner } from "./backend.js"; import { endpointProblem, removedSubscriptionProblem } from "./endpoint.js"; @@ -5759,7 +5759,10 @@ export class Engine { const path = String(args.path ?? ""); const from = num(args.offset, 0); const to = actualReadEnd(result, from, path); - const covered = ctx.shown.get(path) ?? []; + // 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) ?? []; // 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 @@ -5783,7 +5786,7 @@ export class Engine { repeatedHere = true; } else { covered.push({ from, to }); - ctx.shown.set(path, covered); + ctx.shown.set(file, covered); } } @@ -5828,7 +5831,7 @@ export class Engine { sha256: createHash("sha256").update(result, "utf8").digest("hex").slice(0, 16), }); if ((name === "write_file" || name === "edit_file") && allowed) { - ctx.shown.delete(String(args.path ?? "")); + ctx.shown.delete(canonPath(String(args.path ?? ""), this.cwd)); this.pinTask(ctx.userText); } this.did.push( diff --git a/src/transcript.ts b/src/transcript.ts index b52f25c..b80af80 100644 --- a/src/transcript.ts +++ b/src/transcript.ts @@ -19,6 +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 { parseLenient } from "./lenient-json.js"; import { estTokens, type Bom, type Msg } from "./types.js"; @@ -29,6 +30,27 @@ const SAME_CALL_PREFIX = "[molt: this is the same "; /** How the engine sends a read of lines the model has already been shown. */ 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. + * + * 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 || "."; + } + return posix.normalize(p).replace(/^(?:\.\/)+/, "") || "."; +} + /** * A bash command that only prints one file: `cat f`, `head -n 40 f`, * `sed -n '1,80p' f`, `nl f`, `tail f`. Anything with a pipe, a redirect or a @@ -611,11 +633,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(read[1]!.replace(/^\.\//, ""), `bash:${cmd}`, at); + if (read && !pointer) noteRead(canonPath(read[1]!), `bash:${cmd}`, at); return; } - const path = String(args.path ?? "").replace(/^\.\//, ""); + const path = canonPath(String(args.path ?? "")); if (!path) return; if (call.function.name === "read_file") { diff --git a/test/lean-fixes.test.ts b/test/lean-fixes.test.ts index 622ad00..0f6f174 100644 --- a/test/lean-fixes.test.ts +++ b/test/lean-fixes.test.ts @@ -17,7 +17,7 @@ import { readFileSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { describe, it } from "node:test"; import { Engine } from "../src/engine.js"; -import { ELIDED_PREFIX, SHED_MIN_FREE, Transcript } from "../src/transcript.js"; +import { ELIDED_PREFIX, SHED_MIN_FREE, Transcript, canonPath } from "../src/transcript.js"; import { allowAll, drain, scriptedProvider, toolCall, workspace } from "./helpers.js"; describe("shed min-free (default)", () => { @@ -292,3 +292,61 @@ describe("per-call elision (default)", () => { assert.equal(t.elideSupersededReads().elided, 0); }); }); + +/** + * The engine's read-coverage map (`shown`) keyed a path as spelled. A read of + * `dur.py`, then an edit of `./dur.py`, cleared the `./dur.py` key and left + * `dur.py` marked as shown: the re-read came back as "you have already been + * shown … nothing has changed since", while the transcript (which strips the + * `./`) had already elided the old copy. The model held no copy of the file + * it had just edited and was told not to read it again. + */ +describe("shown map: one spelling per file", () => { + async function editThenReread(readAs: string, editAs: (dir: string) => string, rereadAs: string) { + const ws = workspace(); + try { + writeFileSync(join(ws.dir, "dur.py"), `def parse(s):\n return 0 # OLD_MARK\n${"# pad\n".repeat(60)}`); + const p = scriptedProvider([ + { calls: [{ name: "read_file", args: { path: readAs } }] }, + { calls: [{ name: "edit_file", args: { path: editAs(ws.dir), old_text: "return 0 # OLD_MARK", new_text: "return 5400 # NEW_MARK" } }] }, + { calls: [{ name: "read_file", args: { path: rereadAs } }] }, + { 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("fix dur.py", allowAll)); + assert.match(readFileSync(join(ws.dir, "dur.py"), "utf8"), /NEW_MARK/, "the edit did not land"); + const last = p.bodies.at(-1)!; + assert.doesNotMatch(last, /you have already been shown/, "the re-read after the edit was answered with a pointer"); + assert.match(last, /NEW_MARK/, "the model has no copy of the edited file"); + } finally { + ws.cleanup(); + } + } + + it("read dur.py, edit ./dur.py, read dur.py: the re-read returns the new contents", () => + editThenReread("dur.py", () => "./dur.py", "dur.py")); + it("read ./dur.py, edit dur.py, read ./dur.py", () => editThenReread("./dur.py", () => "dur.py", "./dur.py")); + it("an absolute path inside the workspace is the same file", () => + editThenReread("dur.py", (dir) => join(dir, "dur.py"), "dur.py")); + it("a/../dur.py is the same file", () => editThenReread("dur.py", () => "sub/../dur.py", "dur.py")); + + it("canonPath: one spelling, and a path outside the workspace stays as it is", () => { + assert.equal(canonPath("./dur.py"), "dur.py"); + assert.equal(canonPath("././a//b/../dur.py"), "a/dur.py"); + assert.equal(canonPath("/ws/src/dur.py", "/ws"), "src/dur.py"); + assert.equal(canonPath("/ws", "/ws"), "."); + assert.equal(canonPath("/other/dur.py", "/ws"), "/other/dur.py"); + assert.equal(canonPath("/ws2/dur.py", "/ws"), "/ws2/dur.py"); + assert.equal(canonPath("../dur.py"), "../dur.py"); + assert.equal(canonPath(""), ""); + }); + + 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")] }); + 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: "sub/../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); + }); +});