Files
LLeMbas-CLI/src/tool/apply_patch.ts
T
HomerandClaude Fable 5.2 97eadde13b An editor as the client, device files, and files that are not text shown (spec 3.11.0)
The CLI's side of harness spec 3.11.0, on serve --stdio and on the link.

- acp/editor.ts: _lembas/complete (one HTTP request with the login the
  agent holds, the window trimmed, a newer request closes the older one's
  connection and that one is answered as cancelled), _lembas/edit (one
  model request with no tools; the stream cleaned of a fence around the
  whole answer and given the selection's ending; deltas; cancel),
  _lembas/models, _lembas/editor/settings, the editor block of a prompt,
  and ACP diff items.
- acp/files.ts: _lembas/fs/list, stat, read, write, mkdir, move, copy,
  delete, search. A path is judged where it really is, symlinks followed
  first; outside remote.roots, the CLI's own directories and protected
  paths are refused, and so is a folder that holds them; read-only where
  the device allows plan mode only. A write is checked against the hash
  the client last saw, twice for an upload in parts, and put in place
  whole. A delete goes to the system's trash or is refused until the
  client says permanent.
- tool/buffers.ts: read, write, edit, multiedit and apply_patch take a
  file's text from the client's editor and write through it where the
  client offers ACP's fs methods; any failure there is the disk. The call
  is judged before any of it, as before. An overwrite of text that changed
  in the editor since it was read is refused.
- A tool's preview gives each file's text before and after beside the
  unified diff; a client that said editor: true gets them as diff items on
  the approval card and on every finished edit. The texts are kept only
  for such a client.
- tool/show.ts: to a client that said media: true a file that is not text
  is handed over by a handle (_lembas/show/read), good while the request
  is open and for that file as it was; a table, an SVG and a notebook are
  named. On stdio the request also says where the file is.
- The hardline, the modes and the rules are untouched; tool/replace.ts is
  untouched.

Tests: tests/acp-editor.test.ts (19), tests/acp-files.test.ts (13), and
the conformance areas editor, device_files and show.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-24 11:42:03 +00:00

190 lines
8.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// apply_patch: the "*** Begin Patch" format (OpenCode tool/apply_patch.txt, parser in patch.ts),
// which GPT-family models are trained on, or a unified diff (unidiff.ts, from LLeMbas's file_edit),
// which every model has seen. Context lines locate each change, so unlike edit it does not demand
// the file was read first — a wrong context is refused instead.
import { rm, stat } from "node:fs/promises"
import { createTwoFilesPatch } from "diff"
import { z } from "zod"
import { derive, joinBom, parse, type Hunk } from "./patch.ts"
import { applyUnified, isUnified, parseUnified, UnidiffError, type UniFile } from "./unidiff.ts"
import { straySkillNote } from "../skill/index.ts"
import { absPath, defineTool, showPath, ToolError, type FileChange, type ToolContext } from "./tool.ts"
import { diffStat, stampOf } from "./write.ts"
import { fileText, noteChanges, putText } from "./buffers.ts"
import { described } from "../harness.ts"
import { hunks, lineCount, sha256, type EditedFile } from "../session/results.ts"
function hunksOf(text: string): Hunk[] {
try {
return [...parse(text)]
} catch {
return []
}
}
function uniFiles(text: string): UniFile[] {
try {
return parseUnified(text)
} catch {
return []
}
}
/** The files a patch names, as given: for the permission request, before anything is read. */
function namedFiles(a: { patch: string; path?: string }): string[] {
if (isUnified(a.patch)) return [...new Set(uniFiles(a.patch).map((f) => f.path ?? a.path).filter((p): p is string => p !== undefined))]
return hunksOf(a.patch).flatMap((h) => (h.type === "update" && h.movePath ? [h.path, h.movePath] : [h.path]))
}
type Op = { kind: "write"; path: string; content: string; before: string; created?: boolean } | { kind: "delete"; path: string; before: string }
function opsOf(original: Map<string, string | null>, current: Map<string, string | null>): Op[] {
const ops: Op[] = []
for (const [path, now] of current) {
const before = original.get(path) ?? null
if (now === before) continue
if (now === null) ops.push({ kind: "delete", path, before: before! })
else ops.push({ kind: "write", path, content: now, before: before ?? "", ...(before === null ? { created: true } : {}) })
}
return ops
}
/** A unified diff: each file's hunks applied to it in memory, all or nothing. */
async function planUnified(a: { patch: string; path?: string }, ctx: ToolContext): Promise<Op[]> {
let files: UniFile[]
try {
files = parseUnified(a.patch)
} catch (e) {
throw new ToolError(`${(e as Error).message} Nothing was applied.`)
}
const original = new Map<string, string | null>()
const current = new Map<string, string | null>()
for (const f of files) {
const path = f.path ?? a.path
if (!path) throw new ToolError("The diff names no file: give `path`, or start it with --- a/<file> and +++ b/<file> lines. Nothing was applied.")
const abs = absPath(path, ctx)
if (!current.has(abs)) {
const text = await stat(abs).then(() => fileText(abs, ctx), () => null)
original.set(abs, text)
current.set(abs, text)
}
const text = current.get(abs)!
if (f.remove) {
if (text === null) throw new ToolError(`${path} does not exist. Nothing was applied.`)
current.set(abs, null)
continue
}
if (text === null && !f.create) throw new ToolError(`${path} does not exist; a diff that creates it starts with --- /dev/null. Nothing was applied.`)
if (text !== null && f.create) throw new ToolError(`${path} already exists; diff it against its current content instead. Nothing was applied.`)
// A byte-order mark is not part of the first line the hunk's context names.
const bom = text?.startsWith("\uFEFF") ?? false
try {
current.set(abs, joinBom(applyUnified(bom ? text!.slice(1) : (text ?? ""), f.hunks), bom))
} catch (e) {
if (e instanceof UnidiffError) throw new ToolError(`${path}: ${e.message}`)
throw e
}
}
return opsOf(original, current)
}
async function planPatch(a: { patch: string; path?: string }, ctx: ToolContext): Promise<Op[]> {
if (isUnified(a.patch)) return planUnified(a, ctx)
let hunks: Hunk[]
try {
hunks = [...parse(a.patch)]
} catch (e) {
throw new ToolError(`The patch could not be parsed: ${(e as Error).message}`)
}
// Work everything out first; write nothing until every hunk is known to apply. Each file's
// content is followed through the patch, so a file named twice gets both changes, in order.
const original = new Map<string, string | null>() // on disk before; null: did not exist
const current = new Map<string, string | null>()
const load = async (abs: string) => {
if (!current.has(abs)) {
const text = await stat(abs).then(() => fileText(abs, ctx), () => null)
original.set(abs, text)
current.set(abs, text)
}
return current.get(abs)!
}
for (const h of hunks) {
const abs = absPath(h.path, ctx)
const text = await load(abs)
if (h.type === "add") {
if (text !== null) throw new ToolError(`${h.path} already exists; use Update File to change it. Nothing was applied.`)
current.set(abs, h.contents.endsWith("\n") ? h.contents : h.contents + "\n")
} else if (h.type === "delete") {
if (text === null) throw new ToolError(`${h.path} does not exist. Nothing was applied.`)
current.set(abs, null)
} else {
if (text === null) throw new ToolError(`${h.path} does not exist. Nothing was applied.`)
let next
try {
next = derive(h.path, h.chunks, text)
} catch (e) {
throw new ToolError(`${(e as Error).message}\nRead the file again and write the hunk from what is there. Nothing was applied.`)
}
const to = h.movePath ? absPath(h.movePath, ctx) : abs
if (to !== abs) {
if ((await load(to)) !== null) throw new ToolError(`${h.movePath} already exists; a move does not overwrite a file. Nothing was applied.`)
current.set(abs, null)
}
current.set(to, joinBom(next.content, next.bom))
}
}
return opsOf(original, current)
}
/** Each file a patch changes, with its text before and after (acp.md *Edits as diffs*). */
function changesOf(ops: Op[]): FileChange[] {
return ops.map((op) => (op.kind === "delete" ? { path: op.path, oldText: op.before, newText: null } : { path: op.path, oldText: op.created ? null : op.before, newText: op.content }))
}
export const applyPatchTool = defineTool({
...described("apply_patch"),
access: "edit",
schema: z.object({ patch: z.string().min(1), path: z.string().optional() }),
permission: (a, ctx) => {
const files = namedFiles(a).map((p) => absPath(p, ctx))
return { permission: "edit", class: "write", patterns: files.length ? files.map((f) => showPath(f, ctx)) : ["*"], paths: files }
},
async preview(a, ctx) {
const ops = await planPatch(a, ctx)
const diff = ops
.map((op) => {
const shown = showPath(op.path, ctx)
return createTwoFilesPatch(shown, op.kind === "delete" ? "/dev/null" : shown, op.before, op.kind === "delete" ? "" : op.content, "", "", { context: 3 })
})
.join("\n")
return { diff, files: changesOf(ops) }
},
async run(a, ctx) {
const ops = await planPatch(a, ctx)
const patches: string[] = []
for (const op of ops) {
const shown = showPath(op.path, ctx)
if (op.kind === "delete") {
await rm(op.path)
patches.push(createTwoFilesPatch(shown, "/dev/null", op.before, "", "", "", { context: 3 }))
continue
}
await putText(op.path, op.content, ctx)
ctx.readFiles.add(op.path)
ctx.fileStamps.set(op.path, await stampOf(op.path))
patches.push(createTwoFilesPatch(shown, shown, op.before, op.content, "", "", { context: 3 }))
}
const all = patches.join("\n")
noteChanges(ctx, changesOf(ops))
const { added, removed } = diffStat(all)
const names = ops.map((o) => showPath(o.path, ctx)).join(", ")
const note = straySkillNote(ops.filter((o) => o.kind === "write").map((o) => o.path), ctx)
const files: EditedFile[] = ops.map((op) => {
const path = showPath(op.path, ctx)
if (op.kind === "delete") return { path, deleted: true }
return op.created ? { path, sha: sha256(op.content), created: lineCount(op.content) } : { path, sha: sha256(op.content), hunks: hunks(op.before, op.content) }
})
return { output: `Applied to ${names} (+${added} −${removed}).${note}`, title: `${names} +${added} −${removed}`, meta: { diff: all }, context: { kind: "edit", files, ...(note ? { note } : {}) } }
},
})