An editor that reads but does not write: a saved file is edited on disk, an unsaved one refused
ci / check (push) Canceled after 0s
ci / check (push) Canceled after 0s
A client offering fs.readTextFile without fs.writeTextFile left the agent unable to change any file it had read through the editor: with no write to ask, every edit was answered "refused the write". Spec (3.11.0, acp.md Unsaved text; conformance editor.json kind buffer, 2 cases more: 51): with such a client the device writes the disk. For a file the session read through the client it asks for the text once more: equal to the disk's, nothing is unsaved and the disk is written; different, the call is refused with a sentence asking for the file to be saved in the editor first. Tests: both branches against a client that only reads, and a client that writes (buffer and disk before it answers) taking two edits and a new file. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
7 files changed
+135
-11
No files matched your search
+3
-1
@@ -26,7 +26,9 @@ not text shown in a chat.
|
||||
`fs/write_text_file`, the agent reads the editor's buffers and writes through them. A file
|
||||
you changed in the editor after the agent read it is not overwritten: the agent is told to
|
||||
read it again. Text it read from the editor is never replaced with the text on disk — if the
|
||||
editor stops answering, the edit is refused rather than worked out from the saved file.
|
||||
editor stops answering, the edit is refused rather than worked out from the saved file. With an
|
||||
editor that only reads, a saved file is edited on disk; one with unsaved changes is refused
|
||||
until you save it.
|
||||
- **Edits as diffs**: when an edit is asked about (always in manual mode) the approval carries
|
||||
each file's text before and after, so the editor shows the diff itself; every finished edit
|
||||
carries the same, for a list of changed files. Edit and auto mode write as before.
|
||||
|
||||
+1
-1
@@ -87,7 +87,7 @@ file manager, and files that are not text shown in a chat. Three capabilities, o
|
||||
one tool's description; no new tool, and nothing about permissions or the hardline changes.
|
||||
|
||||
- **An editor as the client** (`acp.md`, section of that name; capability `editor`;
|
||||
`conformance/editor.json`, 49 cases; `loop.json` `editor`). A client says
|
||||
`conformance/editor.json`, 51 cases; `loop.json` `editor`). A client says
|
||||
`clientCapabilities._meta.lembas.editor: true`.
|
||||
- **`_lembas/complete`** — autocomplete: `prefix` and `suffix` in, the insertion out, through
|
||||
the server's new `POST /v1/lembas/complete`. No session and nothing read per request; a newer
|
||||
|
||||
+8
-1
@@ -930,9 +930,16 @@ machine's files, so what it says of `fs` is not read at all. In a session an edi
|
||||
still apply it: the device does not write the disk as well — the call fails, saying the write
|
||||
is unconfirmed, and the model reads the file again. A file a patch deletes or moves is deleted
|
||||
or moved on disk by the device.
|
||||
- **A client that reads but does not write** (`readTextFile` without `writeTextFile`): the
|
||||
device writes the disk. For a file the session read through the client it first asks for the
|
||||
file's text once more: when that is what the disk holds, nothing is unsaved and the disk is
|
||||
written; when the editor holds text that is not saved, the call is refused with a plain
|
||||
sentence (*… has changes in the editor that are not saved … save it in the editor first*) —
|
||||
the disk's new content and the buffer would part ways.
|
||||
- **Text the session read from the editor is never replaced from the disk.** Once the `read`
|
||||
tool was given a file's text by the client, a tool that changes the file needs the client's
|
||||
text again: if the client does not give it, or refuses the write, the call is refused in words
|
||||
text again: if the client does not give it, or refuses the write, or cannot write and holds unsaved text
|
||||
for it, the call is refused in words
|
||||
(*… is open in the editor … Read it again before changing it*) and nothing is written. Without
|
||||
this, a slow client at edit time would have the edit worked out from the disk's text and
|
||||
written over what the person had not saved.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"about": "Spec 3.11.0 (acp.md, *An editor as the client*). `kind: trim` — `_lembas/complete`: `prefix` and `suffix` cut to `max` UTF-8 bytes each, the prefix keeping its end and the suffix its start, at a character boundary. `kind: context` — the block a prompt's `_meta.lembas.editor` becomes in the user message (`block: null`: none); `root` is the session's project, `limits` override `loop.json` `editor.context` for the case. `kind: message` — the user message of a rewrite (`_lembas/edit`) from its params; `limits` override `editor.rewrite`'s context limits. `kind: output` — what a rewrite answers from what the model streamed in `chunks`, for a selection `selection`: a fence around the whole answer taken off, and the end made the selection's; the pieces sent as `_lembas/edit/delta` joined are that text. `kind: model` — the model a rewrite runs on: `asked` (its `model`), `session` (the model of the session it names), `main` (the person's main model on the server), `served` (the login's models), `start` (what a new session in the directory would start on). `kind: diff` — what the content of an approval card (`when: ask`) or of a finished edit's update (`when: done`) is made of, for a client that said `editor: true` or not, the texts being `bytes` together against `max`. `kind: buffer` — unsaved text: where a tool that changes a file takes its text from (`read`: the client gave `text`, `fails`, or offers `none`; `tracked`: the session read the file through the client before), and where a write goes (`write`: `taken`, `refused`, `unknown` — no answer — or `none`). CLI: src/acp/editor.ts, src/tool/buffers.ts.",
|
||||
"about": "Spec 3.11.0 (acp.md, *An editor as the client*). `kind: trim` — `_lembas/complete`: `prefix` and `suffix` cut to `max` UTF-8 bytes each, the prefix keeping its end and the suffix its start, at a character boundary. `kind: context` — the block a prompt's `_meta.lembas.editor` becomes in the user message (`block: null`: none); `root` is the session's project, `limits` override `loop.json` `editor.context` for the case. `kind: message` — the user message of a rewrite (`_lembas/edit`) from its params; `limits` override `editor.rewrite`'s context limits. `kind: output` — what a rewrite answers from what the model streamed in `chunks`, for a selection `selection`: a fence around the whole answer taken off, and the end made the selection's; the pieces sent as `_lembas/edit/delta` joined are that text. `kind: model` — the model a rewrite runs on: `asked` (its `model`), `session` (the model of the session it names), `main` (the person's main model on the server), `served` (the login's models), `start` (what a new session in the directory would start on). `kind: diff` — what the content of an approval card (`when: ask`) or of a finished edit's update (`when: done`) is made of, for a client that said `editor: true` or not, the texts being `bytes` together against `max`. `kind: buffer` — unsaved text: where a tool that changes a file takes its text from (`read`: the client gave `text`, `fails`, or offers `none`; `tracked`: the session read the file through the client before), and where a write goes (`write`: `taken`, `refused`, `unknown` — no answer — or `none`: the client does not write; `unsaved`: the editor's text for the file, asked once more, is not the disk's). CLI: src/acp/editor.ts, src/tool/buffers.ts.",
|
||||
"cases": [
|
||||
{
|
||||
"about": "Both short: sent as they are.",
|
||||
@@ -644,6 +644,26 @@
|
||||
"expect": {
|
||||
"write": "disk"
|
||||
}
|
||||
},
|
||||
{
|
||||
"about": "A client that reads but does not write, a file the session read there, and the editor's text is what is on disk: nothing is unsaved — the device writes the disk.",
|
||||
"kind": "buffer",
|
||||
"tracked": true,
|
||||
"write": "none",
|
||||
"unsaved": false,
|
||||
"expect": {
|
||||
"write": "disk"
|
||||
}
|
||||
},
|
||||
{
|
||||
"about": "…and the editor holds text that is not saved: refused, with a sentence asking for the file to be saved first.",
|
||||
"kind": "buffer",
|
||||
"tracked": true,
|
||||
"write": "none",
|
||||
"unsaved": true,
|
||||
"expect": {
|
||||
"write": "refuse"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
+28
-6
@@ -30,11 +30,14 @@ export function textFrom(o: { tracked: boolean; read: "text" | "fails" | "none"
|
||||
}
|
||||
|
||||
/** What becomes of a write (`kind: buffer`): `taken` by the client; `refused` by it — the disk,
|
||||
* unless the session read the file in the editor; `unknown` — no answer: never the disk as well. */
|
||||
export function writeTo(o: { tracked: boolean; write: "taken" | "refused" | "unknown" | "none" }): "editor" | "disk" | "refuse" {
|
||||
* unless the session read the file in the editor; `unknown` — no answer: never the disk as well;
|
||||
* `none` — the client reads but does not write: the disk, unless the session read the file in
|
||||
* the editor and the editor holds text for it that is not saved (`unsaved`). */
|
||||
export function writeTo(o: { tracked: boolean; write: "taken" | "refused" | "unknown" | "none"; unsaved?: boolean }): "editor" | "disk" | "refuse" {
|
||||
if (o.write === "taken") return "editor"
|
||||
if (o.write === "unknown") return "refuse"
|
||||
return o.tracked && o.write === "refused" ? "refuse" : "disk"
|
||||
if (o.write === "none") return o.tracked && o.unsaved === true ? "refuse" : "disk"
|
||||
return o.tracked ? "refuse" : "disk"
|
||||
}
|
||||
|
||||
/** Note what a call that ran changed, where a client wants to draw it (ToolContext.changed). */
|
||||
@@ -82,17 +85,36 @@ export async function changedInEditor(abs: string, ctx: Ctx): Promise<boolean> {
|
||||
* A write the client neither took nor refused (no answer in time) is not repeated on the disk:
|
||||
* the client may still apply it, and two writers would cross. */
|
||||
export async function putText(abs: string, content: string, ctx: Ctx): Promise<boolean> {
|
||||
const outcome = ctx.editor?.write ? await ctx.editor.write(abs, content).catch(() => false as const) : false
|
||||
const isTracked = tracked(abs, ctx)
|
||||
if (!ctx.editor?.write) {
|
||||
// A client that reads but does not write: the device writes the disk — unless the editor
|
||||
// holds text for the file that is not saved, which the disk's new content would part from.
|
||||
let unsaved = false
|
||||
if (isTracked) {
|
||||
const now = await ctx.editor?.read?.(abs).catch(() => undefined)
|
||||
if (now === undefined) throw readAgain(abs)
|
||||
const disk = await readText(abs).catch(() => undefined)
|
||||
unsaved = disk !== now
|
||||
}
|
||||
if (writeTo({ tracked: isTracked, write: "none", unsaved }) === "refuse") throw new ToolError(`${abs} has changes in the editor that are not saved. Nothing was written: ask the user to save ${abs} in the editor first, then do this again.`)
|
||||
await mkdir(dirname(abs), { recursive: true })
|
||||
await Bun.write(abs, content)
|
||||
// What the editor shows once it has taken the file from the disk again.
|
||||
if (isTracked) ctx.editorSeen?.set(abs, sha256(content))
|
||||
return false
|
||||
}
|
||||
const outcome = await ctx.editor.write(abs, content).catch(() => false as const)
|
||||
const to = writeTo({ tracked: isTracked, write: outcome === true ? "taken" : outcome === "unknown" ? "unknown" : "refused" })
|
||||
if (outcome === "unknown") {
|
||||
ctx.editorSeen?.set(abs, UNKNOWN)
|
||||
throw new ToolError(`The editor did not confirm writing ${abs}; it may or may not have been written. Read it again to see what is there before going on.`)
|
||||
}
|
||||
if (outcome === true) {
|
||||
if (to === "editor") {
|
||||
ctx.editorSeen?.set(abs, sha256(content))
|
||||
return true
|
||||
}
|
||||
// The client would not take it. Text it holds unsaved is not written over behind its back.
|
||||
if (tracked(abs, ctx)) throw new ToolError(`${abs} is open in the editor, which refused the write. Nothing was written; tell the user, or read it again.`)
|
||||
if (to === "refuse") throw new ToolError(`${abs} is open in the editor, which refused the write. Nothing was written; tell the user, or read it again.`)
|
||||
await mkdir(dirname(abs), { recursive: true })
|
||||
await Bun.write(abs, content)
|
||||
return false
|
||||
|
||||
@@ -788,3 +788,76 @@ test("a typed message that begins like the editor's block is shown as typed", as
|
||||
// What was typed is there; the block the client added is not.
|
||||
expect(h.turns[0].text).toBe(typed)
|
||||
})
|
||||
|
||||
test("a client that reads but does not write: a saved file is edited on the disk; one with unsaved text is refused until it is saved", async () => {
|
||||
fake = fakeProvider([
|
||||
{ chunks: [toolCall(0, "r1", "read", JSON.stringify({ path: "saved.txt" }))] },
|
||||
{ chunks: [toolCall(0, "e1", "edit", JSON.stringify({ path: "saved.txt", old: "one", new: "two" }))] },
|
||||
{ chunks: [toolCall(0, "e2", "edit", JSON.stringify({ path: "saved.txt", old: "two", new: "three" }))] },
|
||||
{ chunks: [toolCall(0, "r2", "read", JSON.stringify({ path: "dirty.txt" }))] },
|
||||
{ chunks: [toolCall(0, "e3", "edit", JSON.stringify({ path: "dirty.txt", old: "draft", new: "final" }))] },
|
||||
{ chunks: [delta({ content: "done" })] },
|
||||
])
|
||||
config(fake.url)
|
||||
const root = project({ "saved.txt": "one\n", "dirty.txt": "on disk\n" })
|
||||
const saved = join(root, "saved.txt")
|
||||
const dirty = join(root, "dirty.txt")
|
||||
const { c, init } = await editor({
|
||||
// Both plugins' first versions: buffers are read, nothing is written through them.
|
||||
fs: { readTextFile: true },
|
||||
handlers: {
|
||||
// saved.txt is open with nothing unsaved (the editor shows what the disk holds, reloading
|
||||
// it when it changes); dirty.txt has a line the person has not saved.
|
||||
"fs/read_text_file": (p) => ({ content: p.path === dirty ? "on disk\na draft line\n" : readFileSync(p.path, "utf8") }),
|
||||
},
|
||||
})
|
||||
expect(init.protocolVersion).toBe(1)
|
||||
const s: any = await c.request("session/new", { cwd: root, _meta: { mode: "auto" } })
|
||||
await c.request("session/prompt", { sessionId: s.sessionId, prompt: [{ type: "text", text: "go" }] })
|
||||
const tools = fake.requests.at(-1).messages.filter((m: any) => m.role === "tool").map((m: any) => m.content)
|
||||
// Open in the editor, nothing unsaved: edited on the disk, twice in a row.
|
||||
expect(tools[1]).toContain("-one\n+two")
|
||||
expect(tools[2]).toContain("-two\n+three")
|
||||
expect(readFileSync(saved, "utf8")).toBe("three\n")
|
||||
// Unsaved text there: refused in plain words, and the disk is not touched.
|
||||
expect(tools[3]).toContain("2: a draft line")
|
||||
expect(tools[4]).toContain("has changes in the editor that are not saved")
|
||||
expect(tools[4]).toContain("save")
|
||||
expect(readFileSync(dirty, "utf8")).toBe("on disk\n")
|
||||
})
|
||||
|
||||
test("a client that writes puts the text into its buffer and saves before answering: edits go through it, one after another", async () => {
|
||||
fake = fakeProvider([
|
||||
{ chunks: [toolCall(0, "r1", "read", JSON.stringify({ path: "a.txt" }))] },
|
||||
{ chunks: [toolCall(0, "e1", "edit", JSON.stringify({ path: "a.txt", old: "draft", new: "second" }))] },
|
||||
{ chunks: [toolCall(0, "e2", "edit", JSON.stringify({ path: "a.txt", old: "second", new: "third" }))] },
|
||||
{ chunks: [toolCall(0, "n1", "write", JSON.stringify({ path: "b.txt", content: "new\n" }))] },
|
||||
{ chunks: [delta({ content: "done" })] },
|
||||
])
|
||||
config(fake.url)
|
||||
const root = project({ "a.txt": "on disk\n" })
|
||||
const file = join(root, "a.txt")
|
||||
const buffers: Record<string, string> = { [file]: "a draft\n" }
|
||||
const writes: string[] = []
|
||||
const { c } = await editor({
|
||||
fs: { readTextFile: true, writeTextFile: true },
|
||||
handlers: {
|
||||
"fs/read_text_file": (p) => {
|
||||
if (!(p.path in buffers)) throw new Error("not open")
|
||||
return { content: buffers[p.path] }
|
||||
},
|
||||
// As the spec asks of a client: the buffer takes the text and is saved, then the answer.
|
||||
"fs/write_text_file": (p) => (writes.push(p.path), (buffers[p.path] = p.content), writeFileSync(p.path, p.content), null),
|
||||
},
|
||||
})
|
||||
const s: any = await c.request("session/new", { cwd: root, _meta: { mode: "auto" } })
|
||||
await c.request("session/prompt", { sessionId: s.sessionId, prompt: [{ type: "text", text: "go" }] })
|
||||
const tools = fake.requests.at(-1).messages.filter((m: any) => m.role === "tool").map((m: any) => m.content)
|
||||
// The unsaved buffer was the starting point, and both edits and the new file went through the client.
|
||||
expect(tools[0]).toContain("1: a draft")
|
||||
expect(tools.slice(1).some((t: string) => /Read it again|not saved|refused/.test(t))).toBe(false)
|
||||
expect(writes).toEqual([file, file, join(root, "b.txt")])
|
||||
expect(buffers[file]).toBe("a third\n")
|
||||
expect(readFileSync(file, "utf8")).toBe("a third\n")
|
||||
expect(readFileSync(join(root, "b.txt"), "utf8")).toBe("new\n")
|
||||
})
|
||||
@@ -88,7 +88,7 @@ export const AREAS: Record<string, (c: Case) => unknown> = {
|
||||
const r = rewriteModel({ asked: c.asked as string | undefined, session: c.session as string | undefined, main: c.main as string | undefined, served: c.served as string[], start: c.start as string | undefined })
|
||||
return { model: r.model, from: r.from }
|
||||
}
|
||||
if (c.kind === "buffer") return "read" in c ? { text: textFrom({ tracked: c.tracked === true, read: c.read as "text" | "fails" | "none" }) } : { write: writeTo({ tracked: c.tracked === true, write: c.write as "taken" | "refused" | "unknown" | "none" }) }
|
||||
if (c.kind === "buffer") return "read" in c ? { text: textFrom({ tracked: c.tracked === true, read: c.read as "text" | "fails" | "none" }) } : { write: writeTo({ tracked: c.tracked === true, write: c.write as "taken" | "refused" | "unknown" | "none", unsaved: c.unsaved === true }) }
|
||||
if (c.kind === "diff") return { content: diffContent({ editor: c.editor === true, when: c.when as "ask" | "done", bytes: c.bytes as number, max: c.max as number }) }
|
||||
throw new Error(`editor: unknown kind ${String(c.kind)}`)
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user