@phren/agent 0.1.15 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/README.md +86 -0
  2. package/dist/agent-loop/index.js +239 -65
  3. package/dist/agent-loop/stream.js +212 -29
  4. package/dist/agent-loop/types.js +15 -2
  5. package/dist/checkpoint.js +103 -22
  6. package/dist/commands/info.js +30 -15
  7. package/dist/commands/model.js +6 -2
  8. package/dist/commands/review.js +183 -0
  9. package/dist/commands/session.js +70 -7
  10. package/dist/commands.js +168 -12
  11. package/dist/config.js +87 -10
  12. package/dist/context/compactor.js +239 -0
  13. package/dist/context/pruner.js +39 -13
  14. package/dist/context/token-counter.js +33 -1
  15. package/dist/custom-commands.js +69 -0
  16. package/dist/guards/repeat-tool-reminder.js +75 -0
  17. package/dist/headless.js +115 -0
  18. package/dist/herdr-hooks.js +71 -0
  19. package/dist/index.js +351 -48
  20. package/dist/mcp-client.js +434 -73
  21. package/dist/mcp-oauth.js +121 -0
  22. package/dist/memory/anti-patterns.js +7 -3
  23. package/dist/memory/auto-capture.js +7 -3
  24. package/dist/memory/context.js +92 -73
  25. package/dist/memory/project-context.js +24 -5
  26. package/dist/memory/provenance.js +15 -0
  27. package/dist/memory/review-triage.js +166 -0
  28. package/dist/memory/session.js +36 -1
  29. package/dist/models.js +152 -19
  30. package/dist/multi/child-entry.js +65 -10
  31. package/dist/multi/diff-renderer.js +3 -3
  32. package/dist/multi/markdown.js +125 -27
  33. package/dist/multi/provider-manager.js +8 -0
  34. package/dist/multi/publish.js +72 -0
  35. package/dist/multi/spawner.js +45 -4
  36. package/dist/multi/syntax-highlight.js +25 -4
  37. package/dist/multi/types.js +1 -1
  38. package/dist/permissions/allowlist.js +86 -13
  39. package/dist/permissions/checker.js +42 -7
  40. package/dist/permissions/kernel-sandbox.js +197 -0
  41. package/dist/permissions/prompt.js +10 -1
  42. package/dist/permissions/sandbox.js +40 -17
  43. package/dist/providers/anthropic.js +154 -7
  44. package/dist/providers/codex-auth.js +28 -0
  45. package/dist/providers/codex.js +134 -23
  46. package/dist/providers/history.js +35 -0
  47. package/dist/providers/ollama.js +37 -6
  48. package/dist/providers/openai-compat.js +171 -74
  49. package/dist/providers/openrouter.js +47 -17
  50. package/dist/providers/replay.js +62 -0
  51. package/dist/providers/resolve.js +41 -7
  52. package/dist/providers/retry.js +38 -5
  53. package/dist/providers/types.js +9 -1
  54. package/dist/repl.js +47 -5
  55. package/dist/session/log.js +217 -0
  56. package/dist/session/persist.js +190 -0
  57. package/dist/settings.js +42 -26
  58. package/dist/system-prompt.js +18 -2
  59. package/dist/tools/apply-patch.js +355 -0
  60. package/dist/tools/edit-engine.js +287 -0
  61. package/dist/tools/edit-file.js +86 -27
  62. package/dist/tools/grep.js +25 -10
  63. package/dist/tools/lint-test.js +0 -22
  64. package/dist/tools/mcp-resources.js +51 -6
  65. package/dist/tools/phren-finding.js +12 -4
  66. package/dist/tools/phren-search.js +7 -2
  67. package/dist/tools/read-file.js +37 -9
  68. package/dist/tools/read-image.js +76 -0
  69. package/dist/tools/registry.js +27 -5
  70. package/dist/tools/shell.js +134 -71
  71. package/dist/tools/skill.js +20 -20
  72. package/dist/tools/spawn-agent.js +77 -13
  73. package/dist/tools/update-plan.js +48 -0
  74. package/dist/tools/web-fetch.js +110 -12
  75. package/dist/tools/web-search.js +4 -4
  76. package/dist/tui/components/App.js +98 -26
  77. package/dist/tui/components/ApprovalPanel.js +8 -0
  78. package/dist/tui/components/InputArea.js +5 -5
  79. package/dist/tui/components/ModelPicker.js +43 -0
  80. package/dist/tui/components/PhrenInput.js +10 -5
  81. package/dist/tui/components/PlanReview.js +7 -0
  82. package/dist/tui/components/ShortcutHelp.js +5 -0
  83. package/dist/tui/components/StatusBar.js +46 -20
  84. package/dist/tui/components/SteerQueue.js +1 -1
  85. package/dist/tui/components/ThinkingIndicator.js +6 -4
  86. package/dist/tui/components/ToolCall.js +10 -4
  87. package/dist/tui/components/ToolDetail.js +21 -0
  88. package/dist/tui/help.js +6 -0
  89. package/dist/tui/hooks/useKeyboardShortcuts.js +15 -1
  90. package/dist/tui/index.js +0 -1
  91. package/dist/tui/ink-entry.js +317 -33
  92. package/dist/tui/tool-render.js +1 -1
  93. package/dist/user-hooks.js +171 -0
  94. package/package.json +14 -8
  95. package/dist/multi/multi-commands.js +0 -170
  96. package/dist/multi/multi-events.js +0 -98
  97. package/dist/multi/multi-render.js +0 -147
  98. package/dist/multi/pane.js +0 -28
  99. package/dist/multi/tui-multi.js +0 -200
  100. package/dist/permissions/privacy.js +0 -248
  101. package/dist/tui/components/AgentMessage.js +0 -5
  102. package/dist/tui/components/ChatMessage.js +0 -23
  103. package/dist/tui/components/CodeBlock.js +0 -8
  104. package/dist/tui/components/Separator.js +0 -7
  105. package/dist/tui/components/StreamingText.js +0 -5
  106. package/dist/tui/components/UserMessage.js +0 -5
  107. package/dist/tui/menu-mode.js +0 -86
  108. package/dist/tui/mouse.js +0 -71
  109. package/dist/tui/terminal-control.js +0 -62
  110. package/dist/tui.js +0 -781
@@ -0,0 +1,355 @@
1
+ /**
2
+ * apply_patch: the Codex CLI patch format, which GPT/Codex models are trained
3
+ * to emit.
4
+ *
5
+ * *** Begin Patch
6
+ * *** Add File: path (+ lines follow)
7
+ * *** Delete File: path
8
+ * *** Update File: path
9
+ * *** Move to: new/path (optional, after Update File)
10
+ * @@ optional anchor line
11
+ * context
12
+ * -removed
13
+ * +added
14
+ * *** End of File (optional: hunk is anchored at end of file)
15
+ * *** End Patch
16
+ *
17
+ * The whole patch is validated and computed in memory before anything is
18
+ * written, so a hunk that fails to apply leaves every file untouched.
19
+ * Context is located exactly, then ignoring trailing whitespace, then
20
+ * ignoring surrounding whitespace, then with typographic punctuation folded
21
+ * to ASCII (the same fallbacks Codex uses).
22
+ */
23
+ import * as fs from "fs";
24
+ import * as path from "path";
25
+ import { checkSensitivePath, validatePath } from "../permissions/sandbox.js";
26
+ import { describeNotFound } from "./edit-engine.js";
27
+ const BEGIN = "*** Begin Patch";
28
+ const END = "*** End Patch";
29
+ const ADD = "*** Add File: ";
30
+ const DELETE = "*** Delete File: ";
31
+ const UPDATE = "*** Update File: ";
32
+ const MOVE = "*** Move to: ";
33
+ const EOF_MARK = "*** End of File";
34
+ export class PatchError extends Error {
35
+ }
36
+ /** Parse a patch. Tolerates a heredoc wrapper and a missing End marker. */
37
+ export function parsePatch(text) {
38
+ let lines = text.replace(/\r\n/g, "\n").split("\n");
39
+ // Strip an `apply_patch <<'EOF'` … `EOF` wrapper models sometimes include.
40
+ while (lines.length > 0 && lines[0].trim() === "")
41
+ lines.shift();
42
+ while (lines.length > 0 && lines[lines.length - 1].trim() === "")
43
+ lines.pop();
44
+ if (lines.length > 0 && /^(apply_patch\s*)?<<\s*['"]?EOF['"]?\s*$/.test(lines[0].trim())) {
45
+ lines = lines.slice(1);
46
+ if (lines.length > 0 && lines[lines.length - 1].trim() === "EOF")
47
+ lines = lines.slice(0, -1);
48
+ }
49
+ if (lines.length === 0 || lines[0].trim() !== BEGIN) {
50
+ throw new PatchError(`The first line of the patch must be '${BEGIN}'.`);
51
+ }
52
+ let end = lines.findIndex((l, i) => i > 0 && l.trim() === END);
53
+ if (end === -1)
54
+ end = lines.length;
55
+ const body = lines.slice(1, end);
56
+ const ops = [];
57
+ let i = 0;
58
+ while (i < body.length) {
59
+ const line = body[i];
60
+ if (line.trim() === "") {
61
+ i++;
62
+ continue;
63
+ }
64
+ if (line.startsWith(ADD)) {
65
+ const p = line.slice(ADD.length).trim();
66
+ const added = [];
67
+ i++;
68
+ while (i < body.length && !body[i].startsWith("*** ")) {
69
+ if (!body[i].startsWith("+")) {
70
+ throw new PatchError(`Add File ${p}: every content line must start with '+' (got ${JSON.stringify(body[i])}).`);
71
+ }
72
+ added.push(body[i].slice(1));
73
+ i++;
74
+ }
75
+ ops.push({ kind: "add", path: p, lines: added });
76
+ continue;
77
+ }
78
+ if (line.startsWith(DELETE)) {
79
+ ops.push({ kind: "delete", path: line.slice(DELETE.length).trim() });
80
+ i++;
81
+ continue;
82
+ }
83
+ if (line.startsWith(UPDATE)) {
84
+ const p = line.slice(UPDATE.length).trim();
85
+ i++;
86
+ let moveTo;
87
+ if (i < body.length && body[i].startsWith(MOVE)) {
88
+ moveTo = body[i].slice(MOVE.length).trim();
89
+ i++;
90
+ }
91
+ const chunks = [];
92
+ let current = null;
93
+ while (i < body.length) {
94
+ const l = body[i];
95
+ if (l.startsWith("*** ") && l.trim() !== EOF_MARK)
96
+ break;
97
+ if (l.trim() === EOF_MARK) {
98
+ if (current)
99
+ current.endOfFile = true;
100
+ i++;
101
+ continue;
102
+ }
103
+ if (l.startsWith("@@")) {
104
+ const anchor = l.slice(2).trim();
105
+ current = { ...(anchor ? { anchor } : {}), oldLines: [], newLines: [], diff: [], endOfFile: false };
106
+ chunks.push(current);
107
+ i++;
108
+ continue;
109
+ }
110
+ if (!current) {
111
+ // Codex allows the first hunk to omit its @@ line.
112
+ current = { oldLines: [], newLines: [], diff: [], endOfFile: false };
113
+ chunks.push(current);
114
+ }
115
+ const tag = l[0];
116
+ const rest = l.slice(1);
117
+ if (l === "" || tag === " ") {
118
+ const text = l === "" ? "" : rest;
119
+ current.oldLines.push(text);
120
+ current.newLines.push(text);
121
+ current.diff.push({ tag: " ", text });
122
+ }
123
+ else if (tag === "-") {
124
+ current.oldLines.push(rest);
125
+ current.diff.push({ tag: "-", text: rest });
126
+ }
127
+ else if (tag === "+") {
128
+ current.newLines.push(rest);
129
+ current.diff.push({ tag: "+", text: rest });
130
+ }
131
+ else {
132
+ throw new PatchError(`Update File ${p}: unexpected line ${JSON.stringify(l)}. Hunk lines must start with ' ', '-' or '+'.`);
133
+ }
134
+ i++;
135
+ }
136
+ const real = chunks.filter((c) => c.oldLines.length > 0 || c.newLines.length > 0);
137
+ if (real.length === 0 && !moveTo)
138
+ throw new PatchError(`Update File ${p}: no hunks.`);
139
+ ops.push({ kind: "update", path: p, ...(moveTo ? { moveTo } : {}), chunks: real });
140
+ continue;
141
+ }
142
+ throw new PatchError(`Unexpected line ${JSON.stringify(line)}. Expected '${ADD}', '${DELETE}' or '${UPDATE}'.`);
143
+ }
144
+ if (ops.length === 0)
145
+ throw new PatchError("The patch contains no file operations.");
146
+ return ops;
147
+ }
148
+ /** Every path a patch touches (sources and move targets). */
149
+ export function patchPaths(ops) {
150
+ const out = [];
151
+ for (const op of ops) {
152
+ out.push(op.path);
153
+ if (op.kind === "update" && op.moveTo)
154
+ out.push(op.moveTo);
155
+ }
156
+ return out;
157
+ }
158
+ const PUNCT = {
159
+ "‘": "'", "’": "'", "‚": "'", "‛": "'",
160
+ "“": '"', "”": '"', "„": '"', "‟": '"',
161
+ "‐": "-", "‑": "-", "‒": "-", "–": "-", "—": "-", "―": "-", "−": "-",
162
+ " ": " ", " ": " ", " ": " ", " ": " ", " ": " ",
163
+ };
164
+ const foldPunct = (s) => s.trim().replace(/[‘-‟‐-―−     ]/g, (c) => PUNCT[c] ?? c);
165
+ const NORMALIZERS = [
166
+ (s) => s,
167
+ (s) => s.trimEnd(),
168
+ (s) => s.trim(),
169
+ foldPunct,
170
+ ];
171
+ /** Find `pattern` in `lines` at or after `from`; with `eof`, prefer the end of file. */
172
+ export function seekSequence(lines, pattern, from, eof) {
173
+ if (pattern.length === 0)
174
+ return from;
175
+ if (pattern.length > lines.length)
176
+ return -1;
177
+ const last = lines.length - pattern.length;
178
+ const start = eof ? last : Math.max(0, from);
179
+ for (const norm of NORMALIZERS) {
180
+ for (let i = start; i <= last; i++) {
181
+ let ok = true;
182
+ for (let j = 0; j < pattern.length; j++) {
183
+ if (norm(lines[i + j]) !== norm(pattern[j])) {
184
+ ok = false;
185
+ break;
186
+ }
187
+ }
188
+ if (ok)
189
+ return i;
190
+ }
191
+ }
192
+ // An EOF-anchored hunk whose context is not at the very end: search normally.
193
+ return eof ? seekSequence(lines, pattern, from, false) : -1;
194
+ }
195
+ function splitFile(content) {
196
+ const lines = content.replace(/\r\n/g, "\n").split("\n");
197
+ if (lines.length > 0 && lines[lines.length - 1] === "")
198
+ lines.pop();
199
+ return lines;
200
+ }
201
+ /** Apply the chunks of one Update File to its current content. */
202
+ export function applyChunks(content, chunks, filePath) {
203
+ const crlf = content.includes("\r\n");
204
+ const lines = splitFile(content);
205
+ const replacements = [];
206
+ let cursor = 0;
207
+ chunks.forEach((chunk, n) => {
208
+ if (chunk.anchor) {
209
+ const at = seekSequence(lines, [chunk.anchor], cursor, false);
210
+ if (at === -1) {
211
+ throw new PatchError(`${filePath}: hunk ${n + 1}: could not find the @@ anchor line ${JSON.stringify(chunk.anchor)}.`);
212
+ }
213
+ cursor = at + 1;
214
+ }
215
+ if (chunk.oldLines.length === 0) {
216
+ // Pure insertion: at the anchor when one was given, otherwise at end of file.
217
+ replacements.push({ at: chunk.anchor ? cursor : lines.length, remove: 0, insert: chunk.newLines });
218
+ return;
219
+ }
220
+ let oldLines = chunk.oldLines;
221
+ let diff = chunk.diff;
222
+ let at = seekSequence(lines, oldLines, cursor, chunk.endOfFile);
223
+ if (at === -1 && oldLines[oldLines.length - 1] === "") {
224
+ // A trailing blank context line often stands for the file's final newline.
225
+ oldLines = oldLines.slice(0, -1);
226
+ const lastOld = diff.map((d) => d.tag !== "+").lastIndexOf(true);
227
+ const dropped = diff[lastOld];
228
+ diff = diff.filter((_, k) => k !== lastOld);
229
+ const tail = diff[diff.length - 1];
230
+ if (dropped.tag === "-" && tail?.tag === "+" && tail.text === "")
231
+ diff = diff.slice(0, -1);
232
+ at = seekSequence(lines, oldLines, cursor, chunk.endOfFile);
233
+ }
234
+ if (at === -1) {
235
+ const detail = describeNotFound(lines.join("\n"), oldLines.join("\n"), "The hunk's context and '-' lines were");
236
+ throw new PatchError(`${filePath}: hunk ${n + 1} did not apply.\n${detail}`);
237
+ }
238
+ // Context lines keep the file's own text (whitespace, typographic
239
+ // quotes) even when the patch matched them loosely.
240
+ const insert = [];
241
+ let k = 0;
242
+ for (const d of diff) {
243
+ if (d.tag === " ")
244
+ insert.push(lines[at + k++]);
245
+ else if (d.tag === "-")
246
+ k++;
247
+ else
248
+ insert.push(d.text);
249
+ }
250
+ replacements.push({ at, remove: oldLines.length, insert });
251
+ cursor = at + oldLines.length;
252
+ });
253
+ const out = [...lines];
254
+ for (const r of [...replacements].sort((a, b) => b.at - a.at))
255
+ out.splice(r.at, r.remove, ...r.insert);
256
+ const joined = `${out.join("\n")}\n`;
257
+ return crlf ? joined.replace(/\n/g, "\r\n") : joined;
258
+ }
259
+ /** Compute every file change for a patch without writing anything. */
260
+ export function planPatch(ops, cwd) {
261
+ const state = new Map();
262
+ const read = (p) => {
263
+ const abs = path.resolve(cwd, p);
264
+ if (state.has(abs))
265
+ return state.get(abs);
266
+ if (!fs.existsSync(abs))
267
+ return null;
268
+ if (fs.statSync(abs).isDirectory())
269
+ throw new PatchError(`${p} is a directory.`);
270
+ return fs.readFileSync(abs, "utf-8");
271
+ };
272
+ const writes = [];
273
+ const record = (p, content, before, status) => {
274
+ state.set(path.resolve(cwd, p), content);
275
+ writes.push({ path: p, content, before, status });
276
+ };
277
+ for (const op of ops) {
278
+ if (op.kind === "add") {
279
+ const before = read(op.path);
280
+ record(op.path, op.lines.length > 0 ? `${op.lines.join("\n")}\n` : "", before, before === null ? "A" : "M");
281
+ }
282
+ else if (op.kind === "delete") {
283
+ const before = read(op.path);
284
+ if (before === null)
285
+ throw new PatchError(`Delete File ${op.path}: file does not exist.`);
286
+ record(op.path, null, before, "D");
287
+ }
288
+ else {
289
+ const before = read(op.path);
290
+ if (before === null)
291
+ throw new PatchError(`Update File ${op.path}: file does not exist. Use '*** Add File:' to create it.`);
292
+ const after = op.chunks.length > 0 ? applyChunks(before, op.chunks, op.path) : before;
293
+ if (op.moveTo) {
294
+ record(op.path, null, before, "D");
295
+ record(op.moveTo, after, read(op.moveTo), "A");
296
+ }
297
+ else {
298
+ record(op.path, after, before, "M");
299
+ }
300
+ }
301
+ }
302
+ return writes;
303
+ }
304
+ export const applyPatchTool = {
305
+ name: "apply_patch",
306
+ description: "Apply a patch in the Codex apply_patch format to add, delete, update or move files in one atomic call. " +
307
+ "Format: '*** Begin Patch', then per file '*** Add File: <path>' (lines prefixed '+'), '*** Delete File: <path>', " +
308
+ "or '*** Update File: <path>' (optional '*** Move to: <path>') followed by hunks that start with '@@' (optionally " +
309
+ "'@@ <a line to anchor on>') and contain ' ' context, '-' removed and '+' added lines; end with '*** End Patch'. " +
310
+ "Give about 3 lines of context around each change. If any hunk fails, no file is changed.",
311
+ input_schema: {
312
+ type: "object",
313
+ properties: {
314
+ patch: { type: "string", description: "The full patch text, from '*** Begin Patch' to '*** End Patch'." },
315
+ },
316
+ required: ["patch"],
317
+ },
318
+ async execute(input) {
319
+ const text = (input.patch ?? input.input);
320
+ if (typeof text !== "string" || text.trim() === "") {
321
+ return { output: "patch is required.", is_error: true };
322
+ }
323
+ const cwd = process.cwd();
324
+ let writes;
325
+ try {
326
+ const ops = parsePatch(text);
327
+ for (const p of patchPaths(ops)) {
328
+ const sensitive = checkSensitivePath(path.resolve(cwd, p));
329
+ if (sensitive.sensitive)
330
+ return { output: `Access denied: ${p}: ${sensitive.reason}`, is_error: true };
331
+ const sandbox = validatePath(p, cwd, []);
332
+ if (!sandbox.ok)
333
+ return { output: `Path outside sandbox: ${sandbox.error}`, is_error: true };
334
+ }
335
+ writes = planPatch(ops, cwd);
336
+ }
337
+ catch (err) {
338
+ const msg = err instanceof Error ? err.message : String(err);
339
+ return { output: `apply_patch failed; no files were changed.\n${msg}`, is_error: true };
340
+ }
341
+ for (const w of writes) {
342
+ const abs = path.resolve(cwd, w.path);
343
+ if (w.content === null) {
344
+ if (fs.existsSync(abs))
345
+ fs.rmSync(abs);
346
+ }
347
+ else {
348
+ fs.mkdirSync(path.dirname(abs), { recursive: true });
349
+ fs.writeFileSync(abs, w.content);
350
+ }
351
+ }
352
+ const summary = writes.map((w) => `${w.status} ${w.path}`).join("\n");
353
+ return { output: `Success. Updated the following files:\n${summary}` };
354
+ },
355
+ };
@@ -0,0 +1,287 @@
1
+ /**
2
+ * Pure string-edit engine shared by edit_file, multi_edit and apply_patch.
3
+ *
4
+ * Matching order for one edit:
5
+ * 1. exact substring (the only mode that honours replace_all);
6
+ * 2. the same after stripping read_file's `N\t` line-number prefixes, a
7
+ * common copy mistake;
8
+ * 3. whole-line match ignoring trailing whitespace, then ignoring
9
+ * indentation (re-indenting new_string to the file's indentation).
10
+ *
11
+ * A CRLF file is edited in LF form and converted back, so a model that sends
12
+ * `\n` still matches. Every failure message says what to do next: the
13
+ * locations of an ambiguous match, or the closest region of the file and the
14
+ * first line that differs.
15
+ */
16
+ const LINE_PREFIX_RE = /^\s*\d+\t/;
17
+ /** 1-based line number of a character offset. */
18
+ export function lineOfOffset(content, offset) {
19
+ let line = 1;
20
+ for (let i = 0; i < offset && i < content.length; i++)
21
+ if (content.charCodeAt(i) === 10)
22
+ line++;
23
+ return line;
24
+ }
25
+ function occurrences(content, needle) {
26
+ const out = [];
27
+ let at = content.indexOf(needle);
28
+ while (at !== -1) {
29
+ out.push(at);
30
+ at = content.indexOf(needle, at + needle.length);
31
+ }
32
+ return out;
33
+ }
34
+ function stripLinePrefixes(text) {
35
+ const lines = text.split("\n");
36
+ const nonEmpty = lines.filter((l) => l.length > 0);
37
+ if (nonEmpty.length === 0 || !nonEmpty.every((l) => LINE_PREFIX_RE.test(l)))
38
+ return null;
39
+ return lines.map((l) => l.replace(LINE_PREFIX_RE, "")).join("\n");
40
+ }
41
+ function leadingWs(line) {
42
+ return /^[ \t]*/.exec(line)?.[0] ?? "";
43
+ }
44
+ /** Bigram Dice similarity in [0,1]. */
45
+ export function similarity(a, b) {
46
+ if (a === b)
47
+ return 1;
48
+ if (a.length < 2 || b.length < 2)
49
+ return 0;
50
+ const grams = new Map();
51
+ for (let i = 0; i < a.length - 1; i++) {
52
+ const g = a.slice(i, i + 2);
53
+ grams.set(g, (grams.get(g) ?? 0) + 1);
54
+ }
55
+ let hits = 0;
56
+ for (let i = 0; i < b.length - 1; i++) {
57
+ const g = b.slice(i, i + 2);
58
+ const n = grams.get(g) ?? 0;
59
+ if (n > 0) {
60
+ hits++;
61
+ grams.set(g, n - 1);
62
+ }
63
+ }
64
+ return (2 * hits) / (a.length - 1 + (b.length - 1));
65
+ }
66
+ /**
67
+ * The file region most similar to `needleLines`, for failure messages.
68
+ * Bounded so a large file cannot make a failed edit slow.
69
+ */
70
+ export function closestRegion(fileLines, needleLines) {
71
+ const n = Math.min(needleLines.length, 60);
72
+ if (n === 0 || fileLines.length === 0)
73
+ return null;
74
+ const needle = needleLines.slice(0, n).map((l) => l.trim());
75
+ const firstReal = needle.findIndex((l) => l.length > 0);
76
+ if (firstReal === -1)
77
+ return null;
78
+ const last = Math.max(0, fileLines.length - n);
79
+ const exhaustive = (last + 1) * n <= 400_000;
80
+ let best = null;
81
+ for (let start = 0; start <= last; start++) {
82
+ // On big files only score windows whose anchor line is already close.
83
+ if (!exhaustive && similarity(fileLines[start + firstReal]?.trim() ?? "", needle[firstReal]) < 0.5)
84
+ continue;
85
+ let total = 0;
86
+ let counted = 0;
87
+ for (let j = 0; j < n; j++) {
88
+ const want = needle[j];
89
+ const got = fileLines[start + j]?.trim() ?? "";
90
+ if (!want && !got)
91
+ continue;
92
+ total += similarity(got, want);
93
+ counted++;
94
+ }
95
+ const score = counted > 0 ? total / counted : 0;
96
+ if (!best || score > best.score)
97
+ best = { start, score };
98
+ }
99
+ return best;
100
+ }
101
+ /** A read_file-style excerpt, for failure messages. */
102
+ function excerpt(fileLines, start, count) {
103
+ return fileLines
104
+ .slice(start, start + count)
105
+ .map((l, i) => `${start + i + 1}\t${l.length > 200 ? `${l.slice(0, 200)}…` : l}`)
106
+ .join("\n");
107
+ }
108
+ export function describeNotFound(content, oldString, what = "old_string") {
109
+ const fileLines = content.split("\n");
110
+ const needleLines = oldString.replace(/\n$/, "").split("\n");
111
+ const parts = [`${what} was not found in the file.`];
112
+ const region = closestRegion(fileLines, needleLines);
113
+ if (region && region.score >= 0.5) {
114
+ const count = Math.min(needleLines.length, 60);
115
+ parts.push(`Closest match: lines ${region.start + 1}-${region.start + count} (${Math.round(region.score * 100)}% similar):`, excerpt(fileLines, region.start, count));
116
+ for (let j = 0; j < count; j++) {
117
+ const got = fileLines[region.start + j] ?? "";
118
+ if (got !== needleLines[j]) {
119
+ parts.push(`First difference at line ${region.start + j + 1}:\n expected: ${JSON.stringify(needleLines[j])}\n file has: ${JSON.stringify(got)}`);
120
+ break;
121
+ }
122
+ }
123
+ }
124
+ parts.push("Re-read the file with read_file and copy the text exactly (without the line-number prefix).");
125
+ return parts.join("\n");
126
+ }
127
+ function describeAmbiguous(content, offsets, what = "old_string") {
128
+ const lines = offsets.slice(0, 10).map((o) => lineOfOffset(content, o));
129
+ const more = offsets.length > 10 ? `, … ${offsets.length - 10} more` : "";
130
+ return `${what} matches ${offsets.length} locations (lines ${lines.join(", ")}${more}). ` +
131
+ "Include more surrounding lines to make it unique, or set replace_all: true to change every occurrence.";
132
+ }
133
+ const LINE_LEVELS = [
134
+ { norm: (l) => l.trimEnd(), label: "ignoring trailing whitespace", reindent: false },
135
+ { norm: (l) => l.trim(), label: "ignoring indentation", reindent: true },
136
+ ];
137
+ /** Line-window match under normalization; returns every start index. */
138
+ function lineMatches(fileLines, needle, norm) {
139
+ const want = needle.map(norm);
140
+ const out = [];
141
+ for (let i = 0; i + want.length <= fileLines.length; i++) {
142
+ let ok = true;
143
+ for (let j = 0; j < want.length; j++) {
144
+ if (norm(fileLines[i + j]) !== want[j]) {
145
+ ok = false;
146
+ break;
147
+ }
148
+ }
149
+ if (ok)
150
+ out.push(i);
151
+ }
152
+ return out;
153
+ }
154
+ function reindent(newLines, fromIndent, toIndent) {
155
+ if (fromIndent === toIndent)
156
+ return newLines;
157
+ return newLines.map((l) => {
158
+ if (l.trim() === "")
159
+ return l;
160
+ if (l.startsWith(fromIndent))
161
+ return toIndent + l.slice(fromIndent.length);
162
+ return l;
163
+ });
164
+ }
165
+ function applyLF(content, spec) {
166
+ const { old_string: oldStr, new_string: newStr, replace_all: all } = spec;
167
+ // 1. Exact.
168
+ const hits = occurrences(content, oldStr);
169
+ if (hits.length === 1 || (hits.length > 1 && all)) {
170
+ const out = all ? content.split(oldStr).join(newStr) : content.slice(0, hits[0]) + newStr + content.slice(hits[0] + oldStr.length);
171
+ const firstLine = lineOfOffset(content, hits[0]);
172
+ const lastHit = hits[hits.length - 1];
173
+ return {
174
+ ok: true,
175
+ content: out,
176
+ replacements: all ? hits.length : 1,
177
+ firstLine,
178
+ lastLine: lineOfOffset(content, lastHit) + newStr.split("\n").length - 1,
179
+ };
180
+ }
181
+ if (hits.length > 1)
182
+ return { ok: false, error: describeAmbiguous(content, hits) };
183
+ // 2. read_file line-number prefixes pasted into old_string.
184
+ const strippedOld = stripLinePrefixes(oldStr);
185
+ if (strippedOld !== null && strippedOld !== oldStr) {
186
+ const strippedNew = stripLinePrefixes(newStr) ?? newStr;
187
+ const retry = applyLF(content, { old_string: strippedOld, new_string: strippedNew, replace_all: all });
188
+ if (retry.ok)
189
+ return { ...retry, note: "removed read_file line-number prefixes from old_string" };
190
+ }
191
+ // 3. Whole-line fuzzy matches (single replacement only).
192
+ if (!all) {
193
+ const trailingNl = oldStr.endsWith("\n");
194
+ const needle = (trailingNl ? oldStr.slice(0, -1) : oldStr).split("\n");
195
+ if (needle.some((l) => l.trim().length > 0)) {
196
+ const fileLines = content.split("\n");
197
+ for (const level of LINE_LEVELS) {
198
+ const starts = lineMatches(fileLines, needle, level.norm);
199
+ if (starts.length > 1) {
200
+ const offsets = starts.map((s) => fileLines.slice(0, s).join("\n").length + (s > 0 ? 1 : 0));
201
+ return { ok: false, error: `${describeAmbiguous(content, offsets)} (matched ${level.label})` };
202
+ }
203
+ if (starts.length === 1) {
204
+ const start = starts[0];
205
+ let newLines = (trailingNl && newStr.endsWith("\n") ? newStr.slice(0, -1) : newStr).split("\n");
206
+ if (level.reindent) {
207
+ const firstIdx = needle.findIndex((l) => l.trim().length > 0);
208
+ newLines = reindent(newLines, leadingWs(needle[firstIdx]), leadingWs(fileLines[start + firstIdx]));
209
+ }
210
+ const replaced = newStr === "" && !trailingNl ? [] : newLines;
211
+ const out = [...fileLines.slice(0, start), ...replaced, ...fileLines.slice(start + needle.length)];
212
+ return {
213
+ ok: true,
214
+ content: out.join("\n"),
215
+ replacements: 1,
216
+ firstLine: start + 1,
217
+ lastLine: start + Math.max(1, replaced.length),
218
+ note: `matched ${level.label}`,
219
+ };
220
+ }
221
+ }
222
+ }
223
+ }
224
+ return { ok: false, error: describeNotFound(content, oldStr) };
225
+ }
226
+ /** Apply one edit to file content. Never throws. */
227
+ export function applyEdit(content, spec) {
228
+ if (typeof spec.old_string !== "string" || typeof spec.new_string !== "string") {
229
+ return { ok: false, error: "old_string and new_string must both be strings." };
230
+ }
231
+ if (spec.old_string === "") {
232
+ return { ok: false, error: "old_string must not be empty. Use write_file to create a file, or include existing text to anchor the edit." };
233
+ }
234
+ if (spec.old_string === spec.new_string) {
235
+ return { ok: false, error: "old_string and new_string are identical; nothing would change." };
236
+ }
237
+ // Edit CRLF files in LF form (models almost always send \n), then restore.
238
+ const crlf = content.includes("\r\n") && !/(^|[^\r])\n/.test(content);
239
+ if (!crlf)
240
+ return applyLF(content, spec);
241
+ const toLF = (s) => s.replace(/\r\n/g, "\n");
242
+ const result = applyLF(toLF(content), {
243
+ old_string: toLF(spec.old_string),
244
+ new_string: toLF(spec.new_string),
245
+ replace_all: spec.replace_all,
246
+ });
247
+ if (!result.ok)
248
+ return result;
249
+ return { ...result, content: result.content.replace(/\n/g, "\r\n") };
250
+ }
251
+ /** Apply edits in order, all or nothing. */
252
+ export function applyEdits(content, edits) {
253
+ let current = content;
254
+ let replacements = 0;
255
+ let firstLine = Number.POSITIVE_INFINITY;
256
+ let lastLine = 0;
257
+ const notes = [];
258
+ for (let i = 0; i < edits.length; i++) {
259
+ const r = applyEdit(current, edits[i]);
260
+ if (!r.ok) {
261
+ return { ok: false, error: `Edit ${i + 1} of ${edits.length} failed: ${r.error}\nNo changes were written; earlier edits in this call were not applied either.` };
262
+ }
263
+ current = r.content;
264
+ replacements += r.replacements;
265
+ firstLine = Math.min(firstLine, r.firstLine);
266
+ lastLine = Math.max(lastLine, r.lastLine);
267
+ if (r.note)
268
+ notes.push(`edit ${i + 1}: ${r.note}`);
269
+ }
270
+ return {
271
+ ok: true,
272
+ content: current,
273
+ replacements,
274
+ firstLine: Number.isFinite(firstLine) ? firstLine : 1,
275
+ lastLine,
276
+ ...(notes.length > 0 ? { note: notes.join("; ") } : {}),
277
+ };
278
+ }
279
+ /** A few numbered lines around an edited range so the model sees the result without re-reading. */
280
+ export function snippetAround(content, firstLine, lastLine, context = 3, maxLines = 40) {
281
+ const lines = content.split("\n");
282
+ const from = Math.max(1, firstLine - context);
283
+ const to = Math.min(lines.length, Math.max(lastLine, firstLine) + context);
284
+ const shown = lines.slice(from - 1, Math.min(to, from - 1 + maxLines));
285
+ const body = shown.map((l, i) => `${from + i}\t${l.length > 300 ? `${l.slice(0, 300)}…` : l}`).join("\n");
286
+ return to - from + 1 > maxLines ? `${body}\n… (${to - from + 1 - maxLines} more lines)` : body;
287
+ }