pi-hashline-edit-pro 6.4.6 → 6.4.8
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.
- package/README.md +8 -4
- package/index.ts +13 -2
- package/package.json +9 -9
- package/src/anchor-registry.ts +33 -9
- package/src/batch.ts +2 -2
- package/src/config-ui.ts +3 -2
- package/src/config.ts +26 -0
- package/src/copy-move.ts +9 -4
- package/src/edit-common.ts +31 -33
- package/src/hash-store.ts +15 -0
- package/src/insert.ts +11 -4
- package/src/read.ts +80 -42
- package/src/replace-diff.ts +38 -15
- package/src/replace.ts +6 -3
- package/src/structured.ts +10 -7
- package/src/utils.ts +22 -2
package/README.md
CHANGED
|
@@ -302,6 +302,7 @@ Output is capped at `limit` matched lines, 2000 rows, and 50KB of text, whicheve
|
|
|
302
302
|
|
|
303
303
|
- History is per-file and single-level: only the most recent `replace`, `replace_match`, `insert`, `copy`, or `move` can be reverted. A same-message batch of `replace`/`insert` calls on one file counts as one entry: one undo reverts the whole batch.
|
|
304
304
|
- History is persisted and survives session restarts. A failed `write` does not clear it.
|
|
305
|
+
- Undo records older than `undoRetentionDays` (default 30; `0` keeps them forever) are deleted when the store opens.
|
|
305
306
|
- Every applied `replace`, `replace_match`, `insert`, `copy`, or `move` is undoable; the undo record is saved before the edit is written.
|
|
306
307
|
- A cross-file `move` stores one undo entry per file; `undo_last_change` reverts the file you name, so revert both sides to undo the whole move; a batched cross-file `move` reverts its destination side with the batch's undo and its source side with its own entry.
|
|
307
308
|
- A successful `write` clears the history for that file.
|
|
@@ -353,7 +354,7 @@ The setting lives in `/hashline-config` as Auto-read all and in `config.json` as
|
|
|
353
354
|
|
|
354
355
|
| Command | Description |
|
|
355
356
|
| --- | --- |
|
|
356
|
-
| `/hashline-config` | Open the settings window: auto-read anchors, auto-read all mode, git repos only, ignore folders/files, disable on models, read on disabled models, diff context lines, `grep` tool, copy/move tools, replace_match tool, required `path`, and
|
|
357
|
+
| `/hashline-config` | Open the settings window: auto-read anchors, auto-read all mode, git repos only, ignore folders/files, disable on models, read on disabled models, diff context lines, `grep` tool, copy/move tools, replace_match tool, required `path`, strict input, and undo retention. Persists across sessions. |
|
|
357
358
|
| `/clear-anchors` | Clear the session's anchor claims. Anchors are re-claimed on the next `read`. |
|
|
358
359
|
|
|
359
360
|
Settings live in `~/.config/pi-hashline-edit-pro/config.json`, created when a setting is first changed in `/hashline-config`:
|
|
@@ -370,6 +371,7 @@ Settings live in `~/.config/pi-hashline-edit-pro/config.json`, created when a se
|
|
|
370
371
|
"requirePath": false,
|
|
371
372
|
"strictInput": false,
|
|
372
373
|
"diffContextLines": 1,
|
|
374
|
+
"undoRetentionDays": 30,
|
|
373
375
|
"disableOnModels": [],
|
|
374
376
|
"readOnDisabledModels": "vanilla"
|
|
375
377
|
}
|
|
@@ -389,6 +391,7 @@ Settings live in `~/.config/pi-hashline-edit-pro/config.json`, created when a se
|
|
|
389
391
|
| `diffContextLines` | Diff context | `1` | Surrounding lines in post-edit diffs, 0-10 (needs Auto-read). |
|
|
390
392
|
| `disableOnModels` | Disable on models | `[]` | Model globs matched case-insensitively against `provider/id`, the bare `id`, and `api`, with `*` and `?` wildcards. A matching model gets no anchored edit tools, no auto-read, and no write-echo refusal; `read` stays available as pi's ordinary read unless Read on disabled models is `remove`. |
|
|
391
393
|
| `readOnDisabledModels` | Read on disabled models | `"vanilla"` | `read` for a model matched by `disableOnModels`: `"vanilla"` keeps pi's ordinary `read` active while the anchored edit tools stay off; `"remove"` disables `read` with the rest of the anchored tools. |
|
|
394
|
+
| `undoRetentionDays` | Undo retention | `30` | Days to keep undo history; records older than this are deleted when the store opens. `0` keeps them forever (0-3650). |
|
|
392
395
|
|
|
393
396
|
`disableOnModels` turns off the whole anchored surface for the models you name, so another edit tool (`apply_patch`, a shell read) can own the session without competing instructions. A match removes `replace`, `replace_match`, `insert`, `copy`, `move`, `grep`, and `undo_last_change` from the active tools, and also `read` when Read on disabled models is `remove`, registering pi's plain `grep` for those models, and it also skips the auto-read-all injection, the auto-read block after `write`, the post-edit diff substitution, and the `write` hook that refuses a `write` echoing a served anchor. Because the extension's `read` replaces the built-in one, a matched model has no `read` at all under `remove` unless another extension provides one; the default `vanilla` gives those models pi's ordinary `read` while the anchored edit tools stay off. The list is checked on `session_start`, on `model_select`, and again on `before_agent_start`, so a model change inside a session switches the surface immediately and another extension cannot re-add the tools mid-session. The default `[]` leaves every model unchanged.
|
|
394
397
|
|
|
@@ -411,6 +414,7 @@ When `PI_HASHLINE_DIR` is unset or empty, non-Windows platforms honor `XDG_CONFI
|
|
|
411
414
|
| Auto-read all budget | 200KB floor; 2MB ceiling in `full` | total injection size derived from the model context window; `outline` is not capped at 2MB |
|
|
412
415
|
| Stale-range feedback | first 100 lines | rows returned with `[E_RANGE_STALE]` |
|
|
413
416
|
| Session anchors | 1,353,139 anchors in use | all tools; the least recently read or edited files are freed when the quota is exhausted (`[W_ANCHOR_RECLAIMED]`) |
|
|
417
|
+
| Undo retention | 30 days by default (`undoRetentionDays` 0-3650) | `undo_last_change` history |
|
|
414
418
|
|
|
415
419
|
`grep` uses the same 100MB file-size cutoff as `read`. Files over the line cap are skipped silently in directory searches.
|
|
416
420
|
|
|
@@ -418,9 +422,9 @@ When `PI_HASHLINE_DIR` is unset or empty, non-Windows platforms honor `XDG_CONFI
|
|
|
418
422
|
|
|
419
423
|
All eight tools return machine-readable metadata in `details` alongside the model-visible text.
|
|
420
424
|
|
|
421
|
-
All eight tools also declare an `outputSchema` and return `structuredContent`, so codemode scripts receive anchors, line numbers, diffs, and errors as data instead of parsing the rendered text. The model still receives `content`. Every structured result is either `{ ok: true, kind, ... }` or `{ ok: false, kind: "error", error: { code, message } }`; a failed call is still a tool error (`isError: true`) and also carries `structuredContent`, so a script can branch on the code without matching the message.
|
|
425
|
+
All eight tools also declare an `outputSchema` and return `structuredContent`, so codemode scripts receive anchors, line numbers, diffs, and errors as data instead of parsing the rendered text. In a codemode session `tools.read()` resolves to the file's text, like the built-in `read`; pass `anchors: true` to receive `{ text, lines, notices }`, where `text` is the raw file content of the served window and `lines` carries the anchored rows. The model still receives `content`. Every structured result is either `{ ok: true, kind, ... }` or `{ ok: false, kind: "error", error: { code, message } }`; a failed call is still a tool error (`isError: true`) and also carries `structuredContent`, so a script can branch on the code without matching the message.
|
|
422
426
|
|
|
423
|
-
`read` returns `{ kind: "read", path, text, lines, totalLines, startLine, nextOffset, truncated, blockedByLongLine, hadUtf8DecodeErrors }`, where each entry of `lines` is `{ line, text, anchor, rendered }` for a row that was served (`text` is what was shown, so an oversized line appears as its marker while keeping its anchor). An image read returns `{
|
|
427
|
+
`read` returns `{ kind: "read", path, text, lines, totalLines, startLine, nextOffset, truncated, blockedByLongLine, hadUtf8DecodeErrors, notices }`, where `text` is the raw file content of the served window (no anchors, no footers) and each entry of `lines` is `{ line, text, anchor, rendered }` for a row that was served (`text` there is what was shown, so an oversized line appears as its marker while keeping its anchor). `notices` carries non-file notes such as the UTF-8 rewrite note and `[W_ANCHOR_RECLAIMED]`. An image read returns `{ type: "image", data, mimeType, note }`.
|
|
424
428
|
|
|
425
429
|
`replace`, `replace_match`, `insert`, `copy`, `move`, and `undo_last_change` return `{ kind: "edit", verb, classification, path, text, diff, warnings, hints, firstChangedLine, anchors, anchorsOmitted }`, where `verb` is `"replaced"`, `"inserted"`, `"copied"`, `"moved"`, or `"undone"`, `anchors` are the live rows of the post-edit diff, and `anchorsOmitted` is true when the diff was truncated or carried no rows, meaning a fresh read is needed. A cross-file move reports the destination file and its anchors.
|
|
426
430
|
|
|
@@ -513,7 +517,7 @@ All state lives under the config directory (see [Configuration](#configuration))
|
|
|
513
517
|
| `hash-store.sqlite` (+ `-wal`, `-shm`) | Per-file snapshots of allocated anchors keyed by content checksum, plus the undo table. |
|
|
514
518
|
| `sessions/<key>.registry.jsonl` | The session's anchor ownership log (`allocate`/`free`/`clear` events). |
|
|
515
519
|
|
|
516
|
-
The undo table contains the complete pre-edit and post-edit text for the latest edit to each file, so treat the store as sensitive data. On POSIX systems the state directory is restricted to mode `0700` and the SQLite database plus its WAL/SHM sidecars to `0600`. Sidecar logs whose session file is gone are garbage-collected at startup (never the sidecar of a session that is currently loaded in this process); the in-memory ownership of a session is released when that session shuts down and rebuilt from its sidecar on next use. Served records live in memory only and are recomputed on the next read.
|
|
520
|
+
The undo table contains the complete pre-edit and post-edit text for the latest edit to each file, so treat the store as sensitive data. Undo records older than `undoRetentionDays` are deleted when the store opens, and SQLite's `secure_delete` overwrites deleted content. On POSIX systems the state directory is restricted to mode `0700` and the SQLite database plus its WAL/SHM sidecars to `0600`. Sidecar logs whose session file is gone are garbage-collected at startup (never the sidecar of a session that is currently loaded in this process); the in-memory ownership of a session is released when that session shuts down and rebuilt from its sidecar on next use. Served records live in memory only and are recomputed on the next read.
|
|
517
521
|
|
|
518
522
|
### Isolated state
|
|
519
523
|
|
package/index.ts
CHANGED
|
@@ -29,10 +29,12 @@ import {
|
|
|
29
29
|
toggleRequirePath,
|
|
30
30
|
toggleStrictInput,
|
|
31
31
|
adjustDiffContextLines,
|
|
32
|
+
adjustUndoRetentionDays,
|
|
33
|
+
getUndoRetentionDays,
|
|
32
34
|
setAutoReadAllIgnoreFromText,
|
|
33
35
|
setDisableOnModelsFromText,
|
|
34
36
|
} from "./src/config";
|
|
35
|
-
import { loadHashStore, pruneMissing } from "./src/hash-store";
|
|
37
|
+
import { loadHashStore, pruneExpiredUndo, pruneMissing } from "./src/hash-store";
|
|
36
38
|
import { type AnchorSessionContext, initRegistry, gcRegistrySidecars, clearRegistry, freeAnchors, freeAnchorsForPaths, sessionKeyFor, sessionContextReadable, withAnchorSession, releaseRegistrySession, formatAnchorReclaimNotice, takeReclaimedPaths } from "./src/anchor-registry";
|
|
37
39
|
import { serveRows } from "./src/served";
|
|
38
40
|
import { finalizeTurn, planAssistantMessage } from "./src/batch";
|
|
@@ -69,6 +71,7 @@ export default function (pi: ExtensionAPI): void {
|
|
|
69
71
|
let autoReadAllRequireGit = true;
|
|
70
72
|
let disableOnModels: string[] = [];
|
|
71
73
|
let readOnDisabledModels: ReadOnDisabledModels = "vanilla";
|
|
74
|
+
let codemodeActive = false;
|
|
72
75
|
let readFlavor: "anchored" | "vanilla" = "anchored";
|
|
73
76
|
let readFlavorCwd: string | undefined;
|
|
74
77
|
let autoReadAllInjected = false;
|
|
@@ -81,7 +84,8 @@ export default function (pi: ExtensionAPI): void {
|
|
|
81
84
|
|
|
82
85
|
async function refreshEditTools(): Promise<void> {
|
|
83
86
|
try {
|
|
84
|
-
|
|
87
|
+
codemodeActive = pi.getActiveTools().includes("codemode");
|
|
88
|
+
const flags = await currentEditFlags(codemodeActive);
|
|
85
89
|
if (readFlavor !== "vanilla") regRead(pi, flags);
|
|
86
90
|
regReplace(pi, flags);
|
|
87
91
|
regReplaceMatch(pi, flags);
|
|
@@ -184,6 +188,11 @@ export default function (pi: ExtensionAPI): void {
|
|
|
184
188
|
loadHashStore()
|
|
185
189
|
.then(async store => {
|
|
186
190
|
freeAnchorsForPaths(await pruneMissing(store));
|
|
191
|
+
try {
|
|
192
|
+
pruneExpiredUndo(store, await getUndoRetentionDays());
|
|
193
|
+
} catch (error) {
|
|
194
|
+
console.error("Failed to prune expired undo records:", error);
|
|
195
|
+
}
|
|
187
196
|
})
|
|
188
197
|
.catch(err => {
|
|
189
198
|
console.error("Failed to load hash store:", err);
|
|
@@ -252,6 +261,7 @@ export default function (pi: ExtensionAPI): void {
|
|
|
252
261
|
}));
|
|
253
262
|
|
|
254
263
|
pi.on("before_agent_start", async (_event, ctx) => withRegistrySession(ctx, async () => {
|
|
264
|
+
if (pi.getActiveTools().includes("codemode") !== codemodeActive) await refreshEditTools();
|
|
255
265
|
await syncModelGate(ctx.model, ctx.cwd);
|
|
256
266
|
if (autoReadAll === "off" || autoReadAllInjected) return;
|
|
257
267
|
if (modelDisabled(ctx.model, disableOnModels)) return;
|
|
@@ -288,6 +298,7 @@ export default function (pi: ExtensionAPI): void {
|
|
|
288
298
|
else if (key === "disableOnModels") disableOnModels = await setDisableOnModelsFromText(value ?? "");
|
|
289
299
|
else if (key === "readOnDisabledModels") readOnDisabledModels = await cycleReadOnDisabledModels(delta);
|
|
290
300
|
else if (key === "diffContextLines") await adjustDiffContextLines(delta ?? 1);
|
|
301
|
+
else if (key === "undoRetentionDays") await adjustUndoRetentionDays((delta ?? 1) * 5);
|
|
291
302
|
else if (key === "anchorGrepEnabled") {
|
|
292
303
|
const enabled = await toggleAnchorGrep();
|
|
293
304
|
grepEnabled = enabled;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-hashline-edit-pro",
|
|
3
|
-
"version": "6.4.
|
|
3
|
+
"version": "6.4.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Hash-anchored read/replace/insert/grep tools for pi-coding-agent. Every line gets a unique 4-char tokenizer-friendly anchor that stays stable across edits; stale or ambiguous anchors are rejected, never fuzzy-matched. Undo persists across restarts.",
|
|
6
6
|
"main": "index.ts",
|
|
@@ -61,15 +61,15 @@
|
|
|
61
61
|
"typecheck": "tsc --noEmit"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
|
-
"@earendil-works/pi-coding-agent": "^1.
|
|
64
|
+
"@earendil-works/pi-coding-agent": "^1.3.0",
|
|
65
65
|
"@eslint/js": "^10.0.1",
|
|
66
|
-
"@types/node": "^24",
|
|
67
|
-
"@vitest/coverage-v8": "^
|
|
68
|
-
"eslint": "^10.
|
|
69
|
-
"typebox": "^1.
|
|
70
|
-
"typescript": "^
|
|
71
|
-
"typescript-eslint": "^8.
|
|
72
|
-
"vitest": "^
|
|
66
|
+
"@types/node": "^24.19.2",
|
|
67
|
+
"@vitest/coverage-v8": "^5.0.5",
|
|
68
|
+
"eslint": "^10.13.0",
|
|
69
|
+
"typebox": "^1.4.1",
|
|
70
|
+
"typescript": "^6.0.3",
|
|
71
|
+
"typescript-eslint": "^8.71.2",
|
|
72
|
+
"vitest": "^5.0.5"
|
|
73
73
|
},
|
|
74
74
|
"allowScripts": {
|
|
75
75
|
"@google/genai@1.52.0": true,
|
package/src/anchor-registry.ts
CHANGED
|
@@ -6,8 +6,8 @@ import { AsyncLocalStorage } from "node:async_hooks";
|
|
|
6
6
|
import { sessionClaimsDir } from "./paths";
|
|
7
7
|
import { contentChecksum } from "./hashline/hasher";
|
|
8
8
|
import { ANCHOR_COUNT, anchorAt } from "./hashline/alphabet";
|
|
9
|
-
import { HASH_PROBE_STRIDE } from "./hashline/hash";
|
|
10
|
-
import { errCode, isRec, splitLines } from "./utils";
|
|
9
|
+
import { HASH_PROBE_STRIDE, MAX_HASH_LINES } from "./hashline/hash";
|
|
10
|
+
import { assertLineCount, errCode, isRec, splitLines, spliceAll } from "./utils";
|
|
11
11
|
import * as Diff from "diff";
|
|
12
12
|
import { lineChecksum } from "./hashline";
|
|
13
13
|
import { getAllocatedState, persistSnapshot, type HashStore } from "./hash-store";
|
|
@@ -46,6 +46,7 @@ interface OwnedAnchorMap {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
interface AnchorMintedSet {
|
|
49
|
+
readonly size: number;
|
|
49
50
|
has(anchor: string): boolean;
|
|
50
51
|
add(anchor: string): unknown;
|
|
51
52
|
[Symbol.iterator](): IterableIterator<string>;
|
|
@@ -125,9 +126,12 @@ class ShadowOwnedMap implements OwnedAnchorMap {
|
|
|
125
126
|
class ShadowMintedSet implements AnchorMintedSet {
|
|
126
127
|
private readonly parent: AnchorMintedSet;
|
|
127
128
|
private readonly added = new Set<string>();
|
|
129
|
+
private extra = 0;
|
|
130
|
+
private parentSize: number;
|
|
128
131
|
|
|
129
132
|
constructor(parent: AnchorMintedSet) {
|
|
130
133
|
this.parent = parent;
|
|
134
|
+
this.parentSize = parent.size;
|
|
131
135
|
}
|
|
132
136
|
|
|
133
137
|
has(anchor: string): boolean {
|
|
@@ -135,10 +139,22 @@ class ShadowMintedSet implements AnchorMintedSet {
|
|
|
135
139
|
}
|
|
136
140
|
|
|
137
141
|
add(anchor: string): this {
|
|
142
|
+
if (!this.added.has(anchor) && !this.parent.has(anchor)) this.extra += 1;
|
|
138
143
|
this.added.add(anchor);
|
|
139
144
|
return this;
|
|
140
145
|
}
|
|
141
146
|
|
|
147
|
+
get size(): number {
|
|
148
|
+
if (this.parent.size !== this.parentSize) {
|
|
149
|
+
this.parentSize = this.parent.size;
|
|
150
|
+
this.extra = 0;
|
|
151
|
+
for (const anchor of this.added) {
|
|
152
|
+
if (!this.parent.has(anchor)) this.extra += 1;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return this.parentSize + this.extra;
|
|
156
|
+
}
|
|
157
|
+
|
|
142
158
|
*[Symbol.iterator](): IterableIterator<string> {
|
|
143
159
|
yield* this.parent;
|
|
144
160
|
for (const anchor of this.added) {
|
|
@@ -555,16 +571,19 @@ export function reclaimAnchorSpace(state: SessionState, protectPath?: string): s
|
|
|
555
571
|
|
|
556
572
|
export function mintAnchor(state: SessionState, protectPath?: string): string {
|
|
557
573
|
for (;;) {
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
574
|
+
if (state.everMinted.size < ANCHOR_COUNT) {
|
|
575
|
+
for (let probe = 0; probe < MINT_PROBE_LIMIT; probe++) {
|
|
576
|
+
state.probe = (state.probe + HASH_PROBE_STRIDE) % ANCHOR_COUNT;
|
|
577
|
+
const candidate = anchorAt(state.probe);
|
|
578
|
+
if (!state.owned.has(candidate) && !state.everMinted.has(candidate)) {
|
|
579
|
+
return candidate;
|
|
580
|
+
}
|
|
563
581
|
}
|
|
564
582
|
}
|
|
565
583
|
for (let step = 1; step <= ANCHOR_COUNT; step++) {
|
|
566
584
|
const candidate = anchorAt((state.probe + step) % ANCHOR_COUNT);
|
|
567
585
|
if (!state.owned.has(candidate)) {
|
|
586
|
+
state.probe = (state.probe + step) % ANCHOR_COUNT;
|
|
568
587
|
for (const served of state.served.values()) served.delete(candidate);
|
|
569
588
|
return candidate;
|
|
570
589
|
}
|
|
@@ -861,7 +880,7 @@ export function alignOwnershipWithSpans(
|
|
|
861
880
|
state.everMinted.add(settled[k]!);
|
|
862
881
|
state.owned.set(settled[k]!, { path, checksum: newChecksums[start + k]! });
|
|
863
882
|
}
|
|
864
|
-
anchors
|
|
883
|
+
spliceAll(anchors, start, spanLength, settled);
|
|
865
884
|
offset += span.replacementCount - spanLength;
|
|
866
885
|
}
|
|
867
886
|
const rows: [string, string][] = minted.map((m) => [m.anchor, m.checksum]);
|
|
@@ -963,8 +982,9 @@ export async function allocateFileAnchors(
|
|
|
963
982
|
ensureRegistry();
|
|
964
983
|
const registry = current();
|
|
965
984
|
const shadow = options?.shadow === true;
|
|
966
|
-
if (!shadow && registry) touchPath(registry, path);
|
|
967
985
|
const lines = splitLines(content);
|
|
986
|
+
assertLineCount(lines.length, path, MAX_HASH_LINES);
|
|
987
|
+
if (!shadow && registry) touchPath(registry, path);
|
|
968
988
|
const checksums = lines.map(lineChecksum);
|
|
969
989
|
if (options?.previous?.spans) {
|
|
970
990
|
const prevChecksums = splitLines(options.previous.content).map(lineChecksum);
|
|
@@ -1058,6 +1078,10 @@ export function resetRegistryForTests(): void {
|
|
|
1058
1078
|
registries.clear();
|
|
1059
1079
|
}
|
|
1060
1080
|
|
|
1081
|
+
export function lastUsedAtForTests(path: string): number | undefined {
|
|
1082
|
+
return current()?.lastUsedAt.get(path);
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1061
1085
|
async function readSidecarWindow(sidecar: string): Promise<{ text: string; filled: boolean }> {
|
|
1062
1086
|
const handle = await open(sidecar, "r");
|
|
1063
1087
|
const buffer = Buffer.alloc(SIDECAR_HEADER_BYTES);
|
package/src/batch.ts
CHANGED
|
@@ -28,7 +28,7 @@ import { saveUndo } from "./replace-undo";
|
|
|
28
28
|
import { buildChanged, buildNoop, type RMetrics, type TResult } from "./replace-response";
|
|
29
29
|
import { toEditVerb, withStructuredText, type EditStructured } from "./structured";
|
|
30
30
|
import { serveRows, servedHashesFromDiff } from "./served";
|
|
31
|
-
import { abortIf, assertByteLimit, assertLineLimit, errCode, isRec, mapIoError, splitLines } from "./utils";
|
|
31
|
+
import { abortIf, assertByteLimit, assertLineLimit, errCode, isRec, mapIoError, splitLines, spliceAll } from "./utils";
|
|
32
32
|
|
|
33
33
|
export interface PlannedMember {
|
|
34
34
|
batchKey: number;
|
|
@@ -793,7 +793,7 @@ function composeBatchLines(baseContent: string, pieces: BatchPiece[]): string {
|
|
|
793
793
|
const lines = splitLines(baseContent);
|
|
794
794
|
const baseLineCount = lines.length;
|
|
795
795
|
const descending = [...pieces].sort((a, b) => b.start - a.start);
|
|
796
|
-
for (const piece of descending) lines
|
|
796
|
+
for (const piece of descending) spliceAll(lines, piece.start - 1, piece.end - piece.start + 1, piece.newLines);
|
|
797
797
|
let composed = lines.join("\n");
|
|
798
798
|
if (lines.length > 0 && (baseContent.endsWith("\n") || lines[lines.length - 1] === "")) {
|
|
799
799
|
composed += "\n";
|
package/src/config-ui.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Key, matchesKey, truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
2
2
|
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
3
|
-
import { readConfig, type Config } from "./config";
|
|
3
|
+
import { DEFAULT_UNDO_RETENTION_DAYS, readConfig, type Config } from "./config";
|
|
4
4
|
|
|
5
|
-
export type ConfigToggleKey = "autoRead" | "autoReadAll" | "autoReadAllRequireGit" | "autoReadAllIgnore" | "anchorGrepEnabled" | "copyMoveEnabled" | "replaceMatchEnabled" | "requirePath" | "strictInput" | "diffContextLines" | "disableOnModels" | "readOnDisabledModels";
|
|
5
|
+
export type ConfigToggleKey = "autoRead" | "autoReadAll" | "autoReadAllRequireGit" | "autoReadAllIgnore" | "anchorGrepEnabled" | "copyMoveEnabled" | "replaceMatchEnabled" | "requirePath" | "strictInput" | "diffContextLines" | "disableOnModels" | "readOnDisabledModels" | "undoRetentionDays";
|
|
6
6
|
|
|
7
7
|
export interface ConfigRow {
|
|
8
8
|
key: ConfigToggleKey;
|
|
@@ -40,6 +40,7 @@ export function configRows(config: Config): ConfigRow[] {
|
|
|
40
40
|
{ key: "strictInput", group: "Edit behavior", depth: 0, label: "Strict input", hint: "Reject auto-fixable slips instead of warnings", enabled: config.strictInput === true },
|
|
41
41
|
{ key: "disableOnModels", group: "Model gating", depth: 0, label: "Disable on models", hint: "Model globs that turn off anchored tools (comma-separated)", enabled: (config.disableOnModels ?? []).length > 0, entries: config.disableOnModels ?? [] },
|
|
42
42
|
{ key: "readOnDisabledModels", group: "Model gating", depth: 1, dependsOn: "disableOnModels", gated: true, label: "Read on disabled models", hint: "Keep read available for models disabled above", enabled: (config.readOnDisabledModels ?? "vanilla") !== "remove", mode: config.readOnDisabledModels ?? "vanilla", cycle: ["remove", "vanilla"] },
|
|
43
|
+
{ key: "undoRetentionDays", group: "State", depth: 0, label: "Undo retention", hint: "Days to keep undo history; 0 keeps it forever", enabled: (config.undoRetentionDays ?? DEFAULT_UNDO_RETENTION_DAYS) > 0, value: config.undoRetentionDays ?? DEFAULT_UNDO_RETENTION_DAYS },
|
|
43
44
|
];
|
|
44
45
|
for (const row of rows) {
|
|
45
46
|
if (row.gated !== true || row.dependsOn === undefined) continue;
|
package/src/config.ts
CHANGED
|
@@ -12,6 +12,9 @@ const READ_ON_DISABLED_MODELS: ReadOnDisabledModels[] = ["remove", "vanilla"];
|
|
|
12
12
|
export const DEFAULT_DIFF_CONTEXT_LINES = 1;
|
|
13
13
|
export const MIN_DIFF_CONTEXT_LINES = 0;
|
|
14
14
|
export const MAX_DIFF_CONTEXT_LINES = 10;
|
|
15
|
+
export const DEFAULT_UNDO_RETENTION_DAYS = 30;
|
|
16
|
+
export const MIN_UNDO_RETENTION_DAYS = 0;
|
|
17
|
+
export const MAX_UNDO_RETENTION_DAYS = 3650;
|
|
15
18
|
|
|
16
19
|
export interface Config {
|
|
17
20
|
autoRead: boolean;
|
|
@@ -26,6 +29,7 @@ export interface Config {
|
|
|
26
29
|
diffContextLines?: number;
|
|
27
30
|
disableOnModels?: string[];
|
|
28
31
|
readOnDisabledModels?: ReadOnDisabledModels;
|
|
32
|
+
undoRetentionDays?: number;
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
const DEFAULT_CONFIG: Config = {
|
|
@@ -41,6 +45,7 @@ const DEFAULT_CONFIG: Config = {
|
|
|
41
45
|
diffContextLines: DEFAULT_DIFF_CONTEXT_LINES,
|
|
42
46
|
disableOnModels: [],
|
|
43
47
|
readOnDisabledModels: "vanilla",
|
|
48
|
+
undoRetentionDays: DEFAULT_UNDO_RETENTION_DAYS,
|
|
44
49
|
};
|
|
45
50
|
|
|
46
51
|
function parseAutoReadAllMode(value: unknown): AutoReadAllMode {
|
|
@@ -101,6 +106,14 @@ export function normalizeDiffContextLines(value: unknown): number {
|
|
|
101
106
|
return floored;
|
|
102
107
|
}
|
|
103
108
|
|
|
109
|
+
export function normalizeUndoRetentionDays(value: unknown): number {
|
|
110
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return DEFAULT_UNDO_RETENTION_DAYS;
|
|
111
|
+
const floored = Math.floor(value);
|
|
112
|
+
if (floored < MIN_UNDO_RETENTION_DAYS) return MIN_UNDO_RETENTION_DAYS;
|
|
113
|
+
if (floored > MAX_UNDO_RETENTION_DAYS) return MAX_UNDO_RETENTION_DAYS;
|
|
114
|
+
return floored;
|
|
115
|
+
}
|
|
116
|
+
|
|
104
117
|
function parseConfig(content: string): Config {
|
|
105
118
|
const parsed = JSON.parse(content) as unknown;
|
|
106
119
|
if (!isRec(parsed) || (parsed.autoRead !== undefined && typeof parsed.autoRead !== "boolean")) {
|
|
@@ -118,6 +131,7 @@ function parseConfig(content: string): Config {
|
|
|
118
131
|
const autoReadAllIgnore = parsed.autoReadAllIgnore;
|
|
119
132
|
const disableOnModels = parsed.disableOnModels;
|
|
120
133
|
const readOnDisabledModels = parsed.readOnDisabledModels;
|
|
134
|
+
const undoRetentionDays = parsed.undoRetentionDays;
|
|
121
135
|
const legacyDisableReadOnModels = parsed.disableReadOnModels;
|
|
122
136
|
return {
|
|
123
137
|
autoRead: typeof autoRead === "boolean" ? autoRead : DEFAULT_CONFIG.autoRead,
|
|
@@ -132,6 +146,7 @@ function parseConfig(content: string): Config {
|
|
|
132
146
|
autoReadAllIgnore: parseAutoReadAllIgnore(autoReadAllIgnore),
|
|
133
147
|
disableOnModels: parseDisableOnModels(disableOnModels),
|
|
134
148
|
readOnDisabledModels: parseReadOnDisabledModels(readOnDisabledModels, legacyDisableReadOnModels),
|
|
149
|
+
undoRetentionDays: normalizeUndoRetentionDays(undoRetentionDays),
|
|
135
150
|
};
|
|
136
151
|
}
|
|
137
152
|
|
|
@@ -277,6 +292,9 @@ export const toggleStrictInput = (): Promise<boolean> => toggleFlag("strictInput
|
|
|
277
292
|
export async function getDiffContextLines(): Promise<number> {
|
|
278
293
|
return normalizeDiffContextLines((await readConfig()).diffContextLines);
|
|
279
294
|
}
|
|
295
|
+
export async function getUndoRetentionDays(): Promise<number> {
|
|
296
|
+
return normalizeUndoRetentionDays((await readConfig()).undoRetentionDays);
|
|
297
|
+
}
|
|
280
298
|
export async function adjustDiffContextLines(delta: number): Promise<number> {
|
|
281
299
|
let next = DEFAULT_DIFF_CONTEXT_LINES;
|
|
282
300
|
await updateConfig((c) => {
|
|
@@ -285,6 +303,14 @@ export async function adjustDiffContextLines(delta: number): Promise<number> {
|
|
|
285
303
|
});
|
|
286
304
|
return next;
|
|
287
305
|
}
|
|
306
|
+
export async function adjustUndoRetentionDays(delta: number): Promise<number> {
|
|
307
|
+
let next = DEFAULT_UNDO_RETENTION_DAYS;
|
|
308
|
+
await updateConfig((c) => {
|
|
309
|
+
next = normalizeUndoRetentionDays(normalizeUndoRetentionDays(c.undoRetentionDays) + delta);
|
|
310
|
+
c.undoRetentionDays = next;
|
|
311
|
+
});
|
|
312
|
+
return next;
|
|
313
|
+
}
|
|
288
314
|
export async function setAutoReadAllIgnore(dirs: string[]): Promise<string[]> {
|
|
289
315
|
let next: string[] = [];
|
|
290
316
|
await updateConfig((c) => {
|
package/src/copy-move.ts
CHANGED
|
@@ -23,7 +23,7 @@ import { formatAnchorReclaimNotice, servedForPath, takeReclaimedPaths, withAncho
|
|
|
23
23
|
import { batchMemberFor, batchServedFor, ensureBatchBase, executeBatchMember, noteBatchFailure, pendingBatchMemberFor, registerBatchPlanner, type BatchBase, type BatchMemberPlanner, type PlannedMember } from "./batch";
|
|
24
24
|
import { loadP, loadGuidelines } from "./prompts";
|
|
25
25
|
import { assertTransferReq, normReq, type TransferReq } from "./payload-contract";
|
|
26
|
-
import { abortIf, assertByteLimit, isRec, splitLines } from "./utils";
|
|
26
|
+
import { abortIf, anchorCarryIndex, assertByteLimit, isRec, splitLines } from "./utils";
|
|
27
27
|
import { toDisplayPath } from "./paths";
|
|
28
28
|
import { getDiffContextLines } from "./config";
|
|
29
29
|
import { resolveInCwd, writeAtomic } from "./fs-write";
|
|
@@ -145,7 +145,7 @@ export function buildTransferEdit(input: {
|
|
|
145
145
|
: [...sourceLines, fileLines[anchorLine - 1]!],
|
|
146
146
|
},
|
|
147
147
|
foldedAnchorLines: 1,
|
|
148
|
-
anchorCarry: refs.direction
|
|
148
|
+
anchorCarry: anchorCarryIndex(refs.direction, sourceLines.length),
|
|
149
149
|
endingOverrides: refs.direction === "after"
|
|
150
150
|
? [undefined, ...sourceEndings]
|
|
151
151
|
: [...sourceEndings, undefined],
|
|
@@ -319,14 +319,14 @@ async function prepareCrossTransfer(input: {
|
|
|
319
319
|
? offsetEndings(1, sourceEndings)
|
|
320
320
|
: sourceEndings;
|
|
321
321
|
if (kind === "copy") {
|
|
322
|
-
return { sourcePreload, destinationPreload, sourceDisplay, destinationDisplay, destinationEdit, destinationFolded, ...(destinationFolded === 1 ? { anchorCarry: refs.direction
|
|
322
|
+
return { sourcePreload, destinationPreload, sourceDisplay, destinationDisplay, destinationEdit, destinationFolded, ...(destinationFolded === 1 ? { anchorCarry: anchorCarryIndex(refs.direction, moved.length) } : {}), endingOverrides };
|
|
323
323
|
}
|
|
324
324
|
const sourceEdit: HTEdit = {
|
|
325
325
|
remove_from: sourcePreload.fileHashes[sourceStart - 1]!,
|
|
326
326
|
remove_to: sourcePreload.fileHashes[sourceEnd - 1]!,
|
|
327
327
|
text: [],
|
|
328
328
|
};
|
|
329
|
-
return { sourcePreload, destinationPreload, sourceDisplay, destinationDisplay, destinationEdit, destinationFolded, ...(destinationFolded === 1 ? { anchorCarry: refs.direction
|
|
329
|
+
return { sourcePreload, destinationPreload, sourceDisplay, destinationDisplay, destinationEdit, destinationFolded, ...(destinationFolded === 1 ? { anchorCarry: anchorCarryIndex(refs.direction, moved.length) } : {}), sourceEdit, endingOverrides };
|
|
330
330
|
}
|
|
331
331
|
|
|
332
332
|
function pairEntry(pipe: PipelineResult, separators: LineEnding[] | undefined): UndoEntry {
|
|
@@ -584,6 +584,7 @@ async function executeCrossFile(
|
|
|
584
584
|
signal,
|
|
585
585
|
preloadedNorm: prepared.destinationPreload,
|
|
586
586
|
endingOverrides: prepared.endingOverrides,
|
|
587
|
+
...(prepared.anchorCarry !== undefined ? { anchorCarry: prepared.anchorCarry } : {}),
|
|
587
588
|
});
|
|
588
589
|
if (kind === "copy") {
|
|
589
590
|
return commitEdit(destinationPipe, {
|
|
@@ -827,6 +828,7 @@ export async function transferPreview(kind: TransferKind, request: unknown, cwd:
|
|
|
827
828
|
served: plan.servedOverride,
|
|
828
829
|
endingOverrides: plan.endingOverrides,
|
|
829
830
|
signal,
|
|
831
|
+
...(plan.anchorCarry !== undefined ? { anchorCarry: plan.anchorCarry } : {}),
|
|
830
832
|
});
|
|
831
833
|
return previewFromPipe(pipe);
|
|
832
834
|
}
|
|
@@ -837,6 +839,7 @@ export async function transferPreview(kind: TransferKind, request: unknown, cwd:
|
|
|
837
839
|
preloadedNorm: prepared.destinationPreload,
|
|
838
840
|
endingOverrides: prepared.endingOverrides,
|
|
839
841
|
signal,
|
|
842
|
+
...(prepared.anchorCarry !== undefined ? { anchorCarry: prepared.anchorCarry } : {}),
|
|
840
843
|
});
|
|
841
844
|
if (kind === "copy") return previewFromPipe(destinationPipe);
|
|
842
845
|
const sourcePipe = await execPipeline(sourcePath, prepared.sourceEdit!, cwd, {
|
|
@@ -943,6 +946,7 @@ export function buildTransferToolDef(kind: TransferKind, flags: EditToolFlags =
|
|
|
943
946
|
],
|
|
944
947
|
},
|
|
945
948
|
flags,
|
|
949
|
+
{ scriptGuidelines: kind === "copy" },
|
|
946
950
|
);
|
|
947
951
|
return {
|
|
948
952
|
name: kind,
|
|
@@ -1016,6 +1020,7 @@ export function buildTransferToolDef(kind: TransferKind, flags: EditToolFlags =
|
|
|
1016
1020
|
preloadedNorm: preload,
|
|
1017
1021
|
served: plan.servedOverride,
|
|
1018
1022
|
endingOverrides: plan.endingOverrides,
|
|
1023
|
+
...(plan.anchorCarry !== undefined ? { anchorCarry: plan.anchorCarry } : {}),
|
|
1019
1024
|
});
|
|
1020
1025
|
return commitEdit(pipe, {
|
|
1021
1026
|
path: pipe.path,
|
package/src/edit-common.ts
CHANGED
|
@@ -53,8 +53,19 @@ function preferenceGuideline(flags: EditToolFlags): string {
|
|
|
53
53
|
const SHARED_EDIT_OPS = ["replace", "replace_match", "insert", "copy", "move"];
|
|
54
54
|
const SHARED_PAYLOAD_OPS = ["replace", "replace_match", "insert"];
|
|
55
55
|
const SHARED_DIFF_OPS = ["replace", "replace_match", "insert", "copy", "move", "undo_last_change"];
|
|
56
|
-
const
|
|
57
|
-
|
|
56
|
+
const SHARED_SCRIPT_OPS = ["read", "replace", "replace_match", "insert", "copy", "move", "grep", "undo_last_change"];
|
|
57
|
+
|
|
58
|
+
function resultContractGuideline(flags: EditToolFlags): string {
|
|
59
|
+
const tools = operationNames(SHARED_SCRIPT_OPS, flags);
|
|
60
|
+
return `${tools}: when called from a codemode script, failures resolve to \`{ ok: false, kind: "error", error: { code, message } }\`; branch on \`ok\` instead of \`try\`/\`catch\`.`;
|
|
61
|
+
}
|
|
62
|
+
const READ_SCRIPT_GUIDELINE =
|
|
63
|
+
'`read` in a codemode script resolves to the file\'s text, like the built-in `read`; pass `anchors: true` to receive `{ text, lines, notices }` with the raw file content and `lines[].anchor` for chained edits. An image read resolves to an image block.';
|
|
64
|
+
const GREP_SCRIPT_GUIDELINE =
|
|
65
|
+
'`grep`: a codemode script receives `{ text, results }`; each `results[].lines[]` entry carries the matching line\'s `anchor` and `text`, so a result is editable without a fresh read.';
|
|
66
|
+
function editScriptGuideline(flags: EditToolFlags): string {
|
|
67
|
+
return `${operationNames(SHARED_EDIT_OPS, flags)}: a codemode script receives \`{ ok, kind: "edit", classification, diff, anchors, anchorsOmitted }\`; reuse \`anchors[].anchor\` to chain the next edit, and read again when \`anchorsOmitted\` is true.`;
|
|
68
|
+
}
|
|
58
69
|
const SCRIPT_BATCH_GUIDELINE =
|
|
59
70
|
"Script calls apply immediately in order, and only the most recent edit per file is undoable.";
|
|
60
71
|
const SCRIPT_TRANSFER_GUIDELINE =
|
|
@@ -106,31 +117,20 @@ function strictInputGuideline(flags: EditToolFlags): string {
|
|
|
106
117
|
return `${tools}: strict-input mode is on; auto-fixable slips are rejected instead of fixed with warnings.`;
|
|
107
118
|
}
|
|
108
119
|
|
|
109
|
-
function
|
|
110
|
-
description
|
|
111
|
-
snippet: string,
|
|
112
|
-
guidelines: string[],
|
|
113
|
-
flags: EditToolFlags,
|
|
114
|
-
options?: { stringPayload?: boolean },
|
|
115
|
-
): { description: string; snippet: string; guidelines: string[] } {
|
|
120
|
+
export function withReplacePrompts(base: { description: string; snippet: string; guidelines: Guideline[] }, flags: EditToolFlags): { description: string; snippet: string; guidelines: string[] } {
|
|
121
|
+
let description = base.description;
|
|
116
122
|
const shared = [batchGuideline(flags), pathGuideline(flags)];
|
|
117
|
-
if (flags.codemode) shared.push(
|
|
123
|
+
if (flags.codemode) shared.push(editScriptGuideline(flags));
|
|
118
124
|
if (flags.autoRead) shared.push(diffGuideline(flags));
|
|
119
|
-
|
|
125
|
+
shared.push(payloadGuideline(flags));
|
|
120
126
|
if (flags.strictInput) shared.push(strictInputGuideline(flags));
|
|
121
|
-
return { description, snippet, guidelines: [...guidelines, ...shared] };
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export function withReplacePrompts(base: { description: string; snippet: string; guidelines: Guideline[] }, flags: EditToolFlags): { description: string; snippet: string; guidelines: string[] } {
|
|
125
|
-
let description = base.description;
|
|
126
|
-
const guidelines = [preferenceGuideline(flags), ...base.guidelines.map((guideline) => guideline.text)];
|
|
127
127
|
if (!flags.replaceMatchEnabled) {
|
|
128
128
|
description = description
|
|
129
129
|
.split(/(?<=\.)\s+/)
|
|
130
130
|
.filter((sentence) => !sentence.includes("`replace_match`"))
|
|
131
131
|
.join(" ");
|
|
132
132
|
}
|
|
133
|
-
return
|
|
133
|
+
return { description, snippet: base.snippet, guidelines: [...base.guidelines.map((guideline) => guideline.text), ...shared] };
|
|
134
134
|
}
|
|
135
135
|
|
|
136
136
|
function readGuidelineApplies(guideline: Guideline, flags: EditToolFlags): boolean {
|
|
@@ -141,7 +141,7 @@ function readGuidelineApplies(guideline: Guideline, flags: EditToolFlags): boole
|
|
|
141
141
|
|
|
142
142
|
export function withReadPrompts(base: { description: string; snippet: string; guidelines: Guideline[] }, flags: EditToolFlags): { description: string; snippet: string; guidelines: string[] } {
|
|
143
143
|
const preference = preferenceGuideline(flags);
|
|
144
|
-
const script = flags.codemode ? [
|
|
144
|
+
const script = flags.codemode ? [READ_SCRIPT_GUIDELINE, resultContractGuideline(flags)] : [];
|
|
145
145
|
const applicable = base.guidelines.filter((guideline) => readGuidelineApplies(guideline, flags));
|
|
146
146
|
if (flags.autoReadAllActive) {
|
|
147
147
|
const rewritten = applicable.filter((guideline) => guideline.id !== READ_REREAD_ID).map((guideline) => guideline.text);
|
|
@@ -153,16 +153,14 @@ export function withReadPrompts(base: { description: string; snippet: string; gu
|
|
|
153
153
|
return { description: base.description, snippet: base.snippet, guidelines: [preference, ...guidelines, ...script] };
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
-
export function withInsertPrompts(base: { description: string; snippet: string; guidelines: Guideline[] },
|
|
157
|
-
|
|
158
|
-
return finalizePrompts(base.description, base.snippet, guidelines, flags);
|
|
156
|
+
export function withInsertPrompts(base: { description: string; snippet: string; guidelines: Guideline[] }, _flags: EditToolFlags): { description: string; snippet: string; guidelines: string[] } {
|
|
157
|
+
return { description: base.description, snippet: base.snippet, guidelines: base.guidelines.map((guideline) => guideline.text) };
|
|
159
158
|
}
|
|
160
159
|
|
|
161
160
|
export function withReplaceMatchPrompts(base: { description: string; snippet: string; guidelines: Guideline[] }, flags: EditToolFlags): { description: string; snippet: string; guidelines: string[] } {
|
|
162
|
-
const guidelines =
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
return prompted;
|
|
161
|
+
const guidelines = base.guidelines.map((guideline) => guideline.text);
|
|
162
|
+
if (flags.strictInput) guidelines.push("`replace_match`: strict-input mode rejects an `old_string` that covers the whole range or retypes unchanged text.");
|
|
163
|
+
return { description: base.description, snippet: base.snippet, guidelines };
|
|
166
164
|
}
|
|
167
165
|
|
|
168
166
|
function gatedEditOps(ops: string[], flags: EditToolFlags): string[] {
|
|
@@ -183,8 +181,8 @@ function joinOps(ops: string[], options?: { backtick?: boolean; separator?: "/"
|
|
|
183
181
|
}
|
|
184
182
|
|
|
185
183
|
export function withGrepPrompts(base: { description: string; snippet: string; guidelines: Guideline[] }, flags: EditToolFlags): { description: string; snippet: string; guidelines: string[] } {
|
|
186
|
-
const guidelines =
|
|
187
|
-
if (flags.codemode) guidelines.push(
|
|
184
|
+
const guidelines = base.guidelines.map((guideline) => guideline.text);
|
|
185
|
+
if (flags.codemode) guidelines.push(GREP_SCRIPT_GUIDELINE);
|
|
188
186
|
if (flags.copyMoveEnabled) return { description: base.description, snippet: base.snippet, guidelines };
|
|
189
187
|
const description = base.description.replaceAll(GREP_OPS_PHRASE, joinOps(gatedEditOps(["replace", "insert", "copy", "move"], flags)));
|
|
190
188
|
return { description, snippet: base.snippet, guidelines };
|
|
@@ -192,10 +190,9 @@ export function withGrepPrompts(base: { description: string; snippet: string; gu
|
|
|
192
190
|
|
|
193
191
|
export function withUndoPrompts(base: { description: string; snippet: string; guidelines: Guideline[] }, flags: EditToolFlags): { description: string; snippet: string; guidelines: string[] } {
|
|
194
192
|
const ops = gatedEditOps(["replace", "replace_match", "insert", "copy", "move"], flags);
|
|
195
|
-
const script = flags.codemode ? [
|
|
193
|
+
const script = flags.codemode ? [SCRIPT_UNDO_GUIDELINE] : [];
|
|
196
194
|
let description = base.description;
|
|
197
195
|
let guidelines = [
|
|
198
|
-
preferenceGuideline(flags),
|
|
199
196
|
...base.guidelines
|
|
200
197
|
.filter((guideline) => flags.copyMoveEnabled || guideline.id !== UNDO_CROSS_FILE_MOVE_ID)
|
|
201
198
|
.map((guideline) => (guideline.id === UNDO_WRITE_CLEARS_ID && !flags.autoRead ? NEUTRAL_UNDO_WRITE_CLEARS : guideline.text)),
|
|
@@ -208,9 +205,10 @@ export function withUndoPrompts(base: { description: string; snippet: string; gu
|
|
|
208
205
|
return { description, snippet: base.snippet, guidelines };
|
|
209
206
|
}
|
|
210
207
|
|
|
211
|
-
export function withTransferPrompts(base: { description: string; snippet: string; guidelines: Guideline[] }, flags: EditToolFlags): { description: string; snippet: string; guidelines: string[] } {
|
|
212
|
-
const guidelines =
|
|
213
|
-
|
|
208
|
+
export function withTransferPrompts(base: { description: string; snippet: string; guidelines: Guideline[] }, flags: EditToolFlags, options?: { scriptGuidelines?: boolean }): { description: string; snippet: string; guidelines: string[] } {
|
|
209
|
+
const guidelines = base.guidelines.map((guideline) => guideline.text);
|
|
210
|
+
if (flags.codemode && options?.scriptGuidelines !== false) guidelines.push(SCRIPT_TRANSFER_GUIDELINE);
|
|
211
|
+
return { description: base.description, snippet: base.snippet, guidelines };
|
|
214
212
|
}
|
|
215
213
|
|
|
216
214
|
function staleAnchorMessage(ref: string, hash: string, owners: Array<OwnedAnchor | undefined>): string {
|
package/src/hash-store.ts
CHANGED
|
@@ -136,6 +136,7 @@ interface Prepared {
|
|
|
136
136
|
undoDelete: (...params: SqlParams) => void;
|
|
137
137
|
snapshotTime: (...params: SqlParams) => Record<string, unknown> | undefined;
|
|
138
138
|
undoTime: (...params: SqlParams) => Record<string, unknown> | undefined;
|
|
139
|
+
undoExpired: (...params: SqlParams) => Record<string, unknown>[];
|
|
139
140
|
}
|
|
140
141
|
|
|
141
142
|
export interface HashStore {
|
|
@@ -183,6 +184,7 @@ function addColumnIfMissing(db: RawDb, statement: string): void {
|
|
|
183
184
|
function buildStore(db: RawDb): { db: RawDb; stmts: Prepared } {
|
|
184
185
|
db.exec("PRAGMA journal_mode = WAL");
|
|
185
186
|
db.exec("PRAGMA synchronous = NORMAL");
|
|
187
|
+
db.exec("PRAGMA secure_delete = ON");
|
|
186
188
|
db.exec(
|
|
187
189
|
"CREATE TABLE IF NOT EXISTS snapshots (" +
|
|
188
190
|
"path TEXT PRIMARY KEY, " +
|
|
@@ -242,6 +244,7 @@ function buildStore(db: RawDb): { db: RawDb; stmts: Prepared } {
|
|
|
242
244
|
const undoDelStmt = db.prepare("DELETE FROM undo WHERE path = ?");
|
|
243
245
|
const snapshotTimeStmt = db.prepare("SELECT updated_at FROM snapshots WHERE path = ?");
|
|
244
246
|
const undoTimeStmt = db.prepare("SELECT updated_at FROM undo WHERE path = ?");
|
|
247
|
+
const undoExpiredStmt = db.prepare("SELECT path FROM undo WHERE updated_at < ?");
|
|
245
248
|
const stmts: Prepared = {
|
|
246
249
|
get: (...params) => getStmt.get(...params) as Record<string, unknown> | undefined,
|
|
247
250
|
getState: (...params) => getStateStmt.get(...params) as Record<string, unknown> | undefined,
|
|
@@ -253,6 +256,7 @@ function buildStore(db: RawDb): { db: RawDb; stmts: Prepared } {
|
|
|
253
256
|
undoDelete: retriedWrite(undoDelStmt),
|
|
254
257
|
snapshotTime: (...params) => snapshotTimeStmt.get(...params) as Record<string, unknown> | undefined,
|
|
255
258
|
undoTime: (...params) => undoTimeStmt.get(...params) as Record<string, unknown> | undefined,
|
|
259
|
+
undoExpired: (...params) => withBusyRetry(() => undoExpiredStmt.all(...params) as Record<string, unknown>[]),
|
|
256
260
|
};
|
|
257
261
|
return { db, stmts };
|
|
258
262
|
}
|
|
@@ -651,6 +655,17 @@ export async function pruneMissing(store: HashStore): Promise<string[]> {
|
|
|
651
655
|
for (const path of missing) snapshotCache.delete(path);
|
|
652
656
|
return missing;
|
|
653
657
|
}
|
|
658
|
+
export function pruneExpiredUndo(store: HashStore, retentionDays: number, now = Date.now()): string[] {
|
|
659
|
+
if (!Number.isFinite(retentionDays) || retentionDays <= 0) return [];
|
|
660
|
+
const cutoff = now - retentionDays * 24 * 60 * 60 * 1000;
|
|
661
|
+
const rows = store.stmts.undoExpired(cutoff) as { path?: unknown }[];
|
|
662
|
+
const paths = rows.map((row) => row.path).filter((path): path is string => typeof path === "string");
|
|
663
|
+
if (paths.length === 0) return [];
|
|
664
|
+
withStore(() => {
|
|
665
|
+
for (const path of paths) store.stmts.undoDelete(path);
|
|
666
|
+
});
|
|
667
|
+
return paths;
|
|
668
|
+
}
|
|
654
669
|
|
|
655
670
|
|
|
656
671
|
|
package/src/insert.ts
CHANGED
|
@@ -12,7 +12,7 @@ import { stripAnchorRow } from "./hashline/resolve";
|
|
|
12
12
|
import { withAnchorSession } from "./anchor-registry";
|
|
13
13
|
import { loadP, loadGuidelines } from "./prompts";
|
|
14
14
|
import { assertInsertReq, normReq, type InsertReq } from "./payload-contract";
|
|
15
|
-
import { isRec, literalEscapeHints, splitLines } from "./utils";
|
|
15
|
+
import { anchorCarryIndex, isRec, literalEscapeHints, splitLines } from "./utils";
|
|
16
16
|
import { queuedEdit, editToolBase, editRenderCallWrapper, editRenderResultWrapper, resolveEditTargetWithRequirement, throwIfStrictInput, withInsertPrompts, DEFAULT_EDIT_FLAGS, type EditToolFlags } from "./edit-common";
|
|
17
17
|
import type { RPreview, RRState } from "./replace-render";
|
|
18
18
|
import { editResultSchema, withStructuredErrors } from "./structured";
|
|
@@ -98,6 +98,11 @@ function insertStripWarning(anchorLine: string | undefined, direction: "before"
|
|
|
98
98
|
return { label: "text", indexOffset: anchorLine !== undefined && direction === "after" ? -1 : 0 };
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
+
function insertAnchorCarry(direction: "before" | "after", editParams: HTEdit, anchorLine: string | undefined): number | undefined {
|
|
102
|
+
if (anchorLine === undefined) return undefined;
|
|
103
|
+
return anchorCarryIndex(direction, editParams.text.length - 1);
|
|
104
|
+
}
|
|
105
|
+
|
|
101
106
|
function planInsertBatchMember(input: BatchPlannerInput): BatchMemberPlan {
|
|
102
107
|
const req = normReq(input.member.args);
|
|
103
108
|
assertInsertReq(req);
|
|
@@ -149,6 +154,7 @@ export async function insertPreview(request: unknown, cwd: string, signal?: Abor
|
|
|
149
154
|
signal,
|
|
150
155
|
});
|
|
151
156
|
const { editParams, anchorLine, contentSeparators } = buildInsertEdit(normalized, preload, ref, targetPath);
|
|
157
|
+
const anchorCarry = insertAnchorCarry(normalized.direction, editParams, anchorLine);
|
|
152
158
|
const pipe = await execPipeline(targetPath, editParams, cwd, {
|
|
153
159
|
accessMode: constants.R_OK,
|
|
154
160
|
noPersist: true,
|
|
@@ -156,6 +162,7 @@ export async function insertPreview(request: unknown, cwd: string, signal?: Abor
|
|
|
156
162
|
signal,
|
|
157
163
|
stripWarning: insertStripWarning(anchorLine, normalized.direction),
|
|
158
164
|
endingOverrides: contentSeparators,
|
|
165
|
+
...(anchorCarry !== undefined ? { anchorCarry } : {}),
|
|
159
166
|
});
|
|
160
167
|
return previewFromPipe(pipe);
|
|
161
168
|
} catch (error: unknown) {
|
|
@@ -241,21 +248,21 @@ export function buildInsertToolDef(flags: EditToolFlags = DEFAULT_EDIT_FLAGS): I
|
|
|
241
248
|
maxLines: MAX_HASH_LINES,
|
|
242
249
|
});
|
|
243
250
|
const { editParams, anchorLine, contentSeparators } = buildInsertEdit(req, preload, ref, targetPath);
|
|
251
|
+
const anchorCarry = insertAnchorCarry(req.direction, editParams, anchorLine);
|
|
244
252
|
const pipe = await execPipeline(targetPath, editParams, ctx.cwd, {
|
|
245
253
|
accessMode: constants.R_OK | constants.W_OK,
|
|
246
254
|
signal,
|
|
247
255
|
preloadedNorm: preload,
|
|
248
256
|
stripWarning: insertStripWarning(anchorLine, req.direction),
|
|
249
257
|
endingOverrides: contentSeparators,
|
|
258
|
+
...(anchorCarry !== undefined ? { anchorCarry } : {}),
|
|
250
259
|
});
|
|
251
260
|
return commitEdit(pipe, {
|
|
252
261
|
path: pipe.path,
|
|
253
262
|
absolutePath,
|
|
254
263
|
mutationTargetPath,
|
|
255
264
|
editAnchors: [editParams.remove_from, editParams.remove_to],
|
|
256
|
-
...(
|
|
257
|
-
? {}
|
|
258
|
-
: { anchorCarry: req.direction === "after" ? 0 : editParams.text.length - 1 }),
|
|
265
|
+
...(anchorCarry !== undefined ? { anchorCarry } : {}),
|
|
259
266
|
signal,
|
|
260
267
|
verb: "inserted",
|
|
261
268
|
noopNoun: "Insertion",
|
package/src/read.ts
CHANGED
|
@@ -23,7 +23,7 @@ import { withAnchorSession, adoptAnchors, ownerOf, ownersDifferingOnlyByCase, fo
|
|
|
23
23
|
import { serveRows } from "./served";
|
|
24
24
|
import { snapshotCache } from "./hash-store/cache";
|
|
25
25
|
import { Text } from "@earendil-works/pi-tui";
|
|
26
|
-
import { anchoredLine, readResultSchema, withStructuredErrors, type AnchoredLine, type ReadResult } from "./structured";
|
|
26
|
+
import { anchoredLine, readResultSchema, withStructuredErrors, type AnchoredLine, type ReadResult, type ReadTextResult } from "./structured";
|
|
27
27
|
const R_DESC = loadP("../tool-prompts/read.md");
|
|
28
28
|
const R_SNIPPET = loadP("../tool-prompts/read-snippet.md");
|
|
29
29
|
function readGuide(): Guideline[] {
|
|
@@ -241,7 +241,6 @@ function anchorRefOf(args: unknown): { anchor: string; limit: number | undefined
|
|
|
241
241
|
const { offset, limit, path } = args;
|
|
242
242
|
if (typeof offset !== "string" || offset.length === 0 || /^\d+$/.test(offset)) return undefined;
|
|
243
243
|
const validLimit = typeof limit === "number" && Number.isInteger(limit) && limit >= 1 ? limit : undefined;
|
|
244
|
-
if (limit !== undefined && validLimit === undefined) return undefined;
|
|
245
244
|
return { anchor: offset, limit: validLimit, path: typeof path === "string" ? path : undefined };
|
|
246
245
|
}
|
|
247
246
|
|
|
@@ -263,28 +262,79 @@ function anchorLineOf(anchor: string, path: string | undefined, cwd: unknown): n
|
|
|
263
262
|
return index < 0 ? undefined : index + 1;
|
|
264
263
|
}
|
|
265
264
|
|
|
265
|
+
const ANSI_ESC = String.fromCharCode(27);
|
|
266
|
+
const CALL_ANSI_RE = new RegExp(`${ANSI_ESC}\\[[0-9;]*m`, "g");
|
|
267
|
+
const CALL_ANSI_AT_RE = new RegExp(`^${ANSI_ESC}\\[[0-9;]*m`);
|
|
268
|
+
|
|
269
|
+
function rawIndexForPlainIndex(text: string, plainIndex: number): number {
|
|
270
|
+
let raw = 0;
|
|
271
|
+
let plain = 0;
|
|
272
|
+
while (raw < text.length && plain < plainIndex) {
|
|
273
|
+
const escape = CALL_ANSI_AT_RE.exec(text.slice(raw));
|
|
274
|
+
if (escape !== null) {
|
|
275
|
+
raw += escape[0].length;
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
raw += 1;
|
|
279
|
+
plain += 1;
|
|
280
|
+
}
|
|
281
|
+
return raw;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export function insertBeforeExpandHint(text: string, suffix: string): string {
|
|
285
|
+
const plain = text.replace(CALL_ANSI_RE, "");
|
|
286
|
+
if (!plain.endsWith(" to expand)")) return `${text}${suffix}`;
|
|
287
|
+
const plainIndex = plain.lastIndexOf(" (");
|
|
288
|
+
const insertAt = plainIndex < 0 ? text.length : rawIndexForPlainIndex(text, plainIndex);
|
|
289
|
+
return `${text.slice(0, insertAt)}${suffix}${text.slice(insertAt)}`;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function anchorRangeLabel(ref: { anchor: string; limit: number | undefined }, line: number | undefined): string {
|
|
293
|
+
if (line === undefined) return ref.limit === undefined ? `:${ref.anchor}` : `:${ref.anchor} +${ref.limit}`;
|
|
294
|
+
return ref.limit === undefined ? `:${line} (${ref.anchor})` : `:${line}-${line + ref.limit - 1} (${ref.anchor})`;
|
|
295
|
+
}
|
|
296
|
+
|
|
266
297
|
function renderReadCall(args: unknown, theme: any, context: any): Text {
|
|
267
298
|
const normalized = normalizeReadCallArgs(args);
|
|
268
|
-
const rendered = builtinReadRenderCall(normalized, theme, context);
|
|
269
299
|
const ref = anchorRefOf(normalized);
|
|
270
|
-
if (ref === undefined) return
|
|
271
|
-
const
|
|
272
|
-
const text = (rendered as unknown as { text: string }).text;
|
|
273
|
-
const index = text.lastIndexOf(marker);
|
|
274
|
-
if (index < 0) return rendered;
|
|
300
|
+
if (ref === undefined) return builtinReadRenderCall(normalized, theme, context);
|
|
301
|
+
const rendered = builtinReadRenderCall(isRec(normalized) ? { ...normalized, offset: undefined, limit: undefined } : normalized, theme, context);
|
|
275
302
|
const line = anchorLineOf(ref.anchor, ref.path, context?.cwd);
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
rendered.setText(`${text.slice(0, index + marker.length)}${suffix}${text.slice(index + marker.length)}`);
|
|
280
|
-
return rendered;
|
|
281
|
-
}
|
|
282
|
-
const range = ref.limit === undefined ? `${line}` : `${line}-${line + ref.limit - 1}`;
|
|
283
|
-
const replacement = theme.fg("warning", `:${range} (${ref.anchor})`);
|
|
284
|
-
rendered.setText(`${text.slice(0, index)}${replacement}${text.slice(index + marker.length)}`);
|
|
303
|
+
const suffix = theme.fg("warning", anchorRangeLabel(ref, line));
|
|
304
|
+
const text = (rendered as unknown as { text: string }).text;
|
|
305
|
+
rendered.setText(insertBeforeExpandHint(text, suffix));
|
|
285
306
|
return rendered;
|
|
286
307
|
}
|
|
287
308
|
|
|
309
|
+
function buildReadToolSchema(codemode: boolean) {
|
|
310
|
+
return Type.Object({
|
|
311
|
+
path: Type.String({
|
|
312
|
+
description: "Path to the file to read (relative or absolute)",
|
|
313
|
+
}),
|
|
314
|
+
offset: Type.Optional(
|
|
315
|
+
Type.Union([
|
|
316
|
+
Type.Integer({ minimum: 1 }),
|
|
317
|
+
Type.String(),
|
|
318
|
+
], { description: "Line number (1-indexed) or a served anchor to start reading from" }),
|
|
319
|
+
),
|
|
320
|
+
limit: Type.Optional(
|
|
321
|
+
Type.Integer({
|
|
322
|
+
minimum: 1,
|
|
323
|
+
description: "Maximum number of lines to read from offset",
|
|
324
|
+
}),
|
|
325
|
+
),
|
|
326
|
+
...(codemode
|
|
327
|
+
? {
|
|
328
|
+
anchors: Type.Optional(
|
|
329
|
+
Type.Boolean({
|
|
330
|
+
description: "Return { text, lines, notices } with editable anchors instead of the plain file text to a codemode script",
|
|
331
|
+
}),
|
|
332
|
+
),
|
|
333
|
+
}
|
|
334
|
+
: {}),
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
|
|
288
338
|
export function regRead(pi: ExtensionAPI, flags: EditToolFlags = DEFAULT_EDIT_FLAGS): void {
|
|
289
339
|
const prompted = withReadPrompts({ description: R_DESC, snippet: R_SNIPPET, guidelines: readGuide() }, flags);
|
|
290
340
|
pi.registerTool({
|
|
@@ -294,23 +344,7 @@ export function regRead(pi: ExtensionAPI, flags: EditToolFlags = DEFAULT_EDIT_FL
|
|
|
294
344
|
promptSnippet: prompted.snippet,
|
|
295
345
|
promptGuidelines: prompted.guidelines,
|
|
296
346
|
prepareArguments: makePrepareArguments(),
|
|
297
|
-
parameters:
|
|
298
|
-
path: Type.String({
|
|
299
|
-
description: "Path to the file to read (relative or absolute)",
|
|
300
|
-
}),
|
|
301
|
-
offset: Type.Optional(
|
|
302
|
-
Type.Union([
|
|
303
|
-
Type.Integer({ minimum: 1 }),
|
|
304
|
-
Type.String(),
|
|
305
|
-
], { description: "Line number (1-indexed) or a served anchor to start reading from" }),
|
|
306
|
-
),
|
|
307
|
-
limit: Type.Optional(
|
|
308
|
-
Type.Integer({
|
|
309
|
-
minimum: 1,
|
|
310
|
-
description: "Maximum number of lines to read from offset",
|
|
311
|
-
}),
|
|
312
|
-
),
|
|
313
|
-
}),
|
|
347
|
+
parameters: buildReadToolSchema(flags.codemode),
|
|
314
348
|
outputSchema: readResultSchema,
|
|
315
349
|
executionMode: "sequential",
|
|
316
350
|
renderCall: renderReadCall,
|
|
@@ -346,8 +380,7 @@ export function regRead(pi: ExtensionAPI, flags: EditToolFlags = DEFAULT_EDIT_FL
|
|
|
346
380
|
context: typeof ctx,
|
|
347
381
|
) => ReturnType<typeof builtinRead.execute>;
|
|
348
382
|
const imageResult = await executeBuiltinRead(_toolCallId, params, signal, _onUpdate, ctx);
|
|
349
|
-
|
|
350
|
-
return { ...imageResult, structuredContent: imageStructured };
|
|
383
|
+
return { ...imageResult, structuredContent: imageResult.structuredContent as ReadResult };
|
|
351
384
|
}
|
|
352
385
|
const { normalized, fileHashes, hadUtf8DecodeErrors, absolutePath: resolvedPath } = await readNormFile(
|
|
353
386
|
rawPath, ctx.cwd, { signal, preloadedFile: file, maxLines: MAX_HASH_LINES },
|
|
@@ -363,25 +396,30 @@ export function regRead(pi: ExtensionAPI, flags: EditToolFlags = DEFAULT_EDIT_FL
|
|
|
363
396
|
serveRows(resolvedPath, fileHashes, fileLines, preview.servedHashes);
|
|
364
397
|
const snapshotId = await safeSnapId(absolutePath, "read");
|
|
365
398
|
const reclaimNotice = formatAnchorReclaimNotice(takeReclaimedPaths());
|
|
366
|
-
const
|
|
367
|
-
preview.text,
|
|
399
|
+
const notices = [
|
|
368
400
|
hadUtf8DecodeErrors ? "[Non-UTF-8 bytes shown as U+FFFD; editing rewrites the file as UTF-8.]" : undefined,
|
|
369
401
|
reclaimNotice,
|
|
370
|
-
].filter((part): part is string => part !== undefined)
|
|
402
|
+
].filter((part): part is string => part !== undefined);
|
|
403
|
+
const previewText = [preview.text, ...notices].join("\n\n");
|
|
404
|
+
const servedRawText = preview.anchoredLines.map((row) => fileLines[row.line - 1] ?? "").join("\n");
|
|
405
|
+
const rawTruncation = servedRawText.length > 0 ? truncateHead(servedRawText, { maxBytes: DEFAULT_MAX_BYTES, maxLines: DEFAULT_MAX_LINES }) : undefined;
|
|
371
406
|
|
|
372
|
-
const
|
|
407
|
+
const readResult: ReadTextResult = {
|
|
373
408
|
ok: true,
|
|
374
409
|
kind: "read",
|
|
375
410
|
path: rawPath,
|
|
376
|
-
text:
|
|
411
|
+
text: rawTruncation?.content ?? "",
|
|
377
412
|
lines: preview.anchoredLines,
|
|
378
413
|
totalLines: preview.totalLines,
|
|
379
414
|
startLine: preview.startLine,
|
|
380
415
|
nextOffset: preview.nextOffset ?? null,
|
|
381
|
-
truncated: preview.truncation !== undefined,
|
|
416
|
+
truncated: preview.truncation !== undefined || rawTruncation?.truncated === true,
|
|
382
417
|
blockedByLongLine: preview.blockedByLongLine,
|
|
383
418
|
hadUtf8DecodeErrors,
|
|
419
|
+
notices,
|
|
384
420
|
};
|
|
421
|
+
const anchorsRequested = isRec(params) && params.anchors === true;
|
|
422
|
+
const structuredContent: ReadResult = flags.codemode && !anchorsRequested ? readResult.text : readResult;
|
|
385
423
|
return {
|
|
386
424
|
content: [{ type: "text", text: previewText }],
|
|
387
425
|
details: {
|
package/src/replace-diff.ts
CHANGED
|
@@ -159,6 +159,40 @@ function placeSpans(
|
|
|
159
159
|
return placed;
|
|
160
160
|
}
|
|
161
161
|
|
|
162
|
+
function splitPlacedSpan(
|
|
163
|
+
span: PlacedSpan,
|
|
164
|
+
oldLines: string[],
|
|
165
|
+
newLines: string[],
|
|
166
|
+
oldHashes: string[],
|
|
167
|
+
newIndexByHash: ReadonlyMap<string, number>,
|
|
168
|
+
): PlacedSpan[] {
|
|
169
|
+
if (span.oldStart > span.oldEnd || span.newStart > span.newEnd) return [span];
|
|
170
|
+
const pairs: Array<[number, number]> = [];
|
|
171
|
+
let lastNew = span.newStart - 1;
|
|
172
|
+
for (let oldIdx = span.oldStart; oldIdx <= span.oldEnd; oldIdx++) {
|
|
173
|
+
const newIdx = newIndexByHash.get(oldHashes[oldIdx]!);
|
|
174
|
+
if (newIdx === undefined || newIdx < span.newStart || newIdx > span.newEnd || newIdx <= lastNew) continue;
|
|
175
|
+
if (oldLines[oldIdx] !== newLines[newIdx]) continue;
|
|
176
|
+
pairs.push([oldIdx, newIdx]);
|
|
177
|
+
lastNew = newIdx;
|
|
178
|
+
}
|
|
179
|
+
if (pairs.length === 0) return [span];
|
|
180
|
+
const out: PlacedSpan[] = [];
|
|
181
|
+
let oldCursor = span.oldStart;
|
|
182
|
+
let newCursor = span.newStart;
|
|
183
|
+
for (const [oldIdx, newIdx] of pairs) {
|
|
184
|
+
if (oldCursor <= oldIdx - 1 || newCursor <= newIdx - 1) {
|
|
185
|
+
out.push({ oldStart: oldCursor, oldEnd: oldIdx - 1, newStart: newCursor, newEnd: newIdx - 1 });
|
|
186
|
+
}
|
|
187
|
+
oldCursor = oldIdx + 1;
|
|
188
|
+
newCursor = newIdx + 1;
|
|
189
|
+
}
|
|
190
|
+
if (oldCursor <= span.oldEnd || newCursor <= span.newEnd) {
|
|
191
|
+
out.push({ oldStart: oldCursor, oldEnd: span.oldEnd, newStart: newCursor, newEnd: span.newEnd });
|
|
192
|
+
}
|
|
193
|
+
return out;
|
|
194
|
+
}
|
|
195
|
+
|
|
162
196
|
function trimPlacedSpans(
|
|
163
197
|
placed: PlacedSpan[],
|
|
164
198
|
oldLines: string[],
|
|
@@ -166,22 +200,11 @@ function trimPlacedSpans(
|
|
|
166
200
|
oldHashes: string[],
|
|
167
201
|
newHashes: string[],
|
|
168
202
|
): PlacedSpan[] {
|
|
203
|
+
const newIndexByHash = new Map<string, number>();
|
|
204
|
+
for (let index = 0; index < newHashes.length; index++) newIndexByHash.set(newHashes[index]!, index);
|
|
169
205
|
const out: PlacedSpan[] = [];
|
|
170
206
|
for (const span of placed) {
|
|
171
|
-
|
|
172
|
-
let oldEnd = span.oldEnd;
|
|
173
|
-
let newStart = span.newStart;
|
|
174
|
-
let newEnd = span.newEnd;
|
|
175
|
-
while (oldStart <= oldEnd && newStart <= newEnd && oldLines[oldStart] === newLines[newStart] && oldHashes[oldStart] === newHashes[newStart]) {
|
|
176
|
-
oldStart += 1;
|
|
177
|
-
newStart += 1;
|
|
178
|
-
}
|
|
179
|
-
while (oldStart <= oldEnd && newStart <= newEnd && oldLines[oldEnd] === newLines[newEnd] && oldHashes[oldEnd] === newHashes[newEnd]) {
|
|
180
|
-
oldEnd -= 1;
|
|
181
|
-
newEnd -= 1;
|
|
182
|
-
}
|
|
183
|
-
if (oldStart > oldEnd && newStart > newEnd) continue;
|
|
184
|
-
out.push({ oldStart, oldEnd, newStart, newEnd });
|
|
207
|
+
for (const piece of splitPlacedSpan(span, oldLines, newLines, oldHashes, newIndexByHash)) out.push(piece);
|
|
185
208
|
}
|
|
186
209
|
return out;
|
|
187
210
|
}
|
|
@@ -364,7 +387,7 @@ export function genDiff(
|
|
|
364
387
|
): { diff: string; firstChangedLine: number | undefined; lineNumbers: (number|undefined)[] } {
|
|
365
388
|
const maxLineBytes = limits?.unlimited ? Number.POSITIVE_INFINITY : (limits?.maxLineBytes ?? DEFAULT_MAX_BYTES);
|
|
366
389
|
const maxBytes = limits?.unlimited ? Number.POSITIVE_INFINITY : (limits?.maxBytes ?? DEFAULT_MAX_BYTES);
|
|
367
|
-
if (spans && newContentHashes && oldContentHashes) {
|
|
390
|
+
if (spans && spans.length > 0 && newContentHashes && oldContentHashes) {
|
|
368
391
|
const anchored = genSpanDiff(oldContent, newContent, contextLines, newContentHashes, oldContentHashes, maxLineBytes, maxBytes, spans);
|
|
369
392
|
if (anchored) {
|
|
370
393
|
const diff = disambiguateDuplicateAnchors(anchored.diff);
|
package/src/replace.ts
CHANGED
|
@@ -87,6 +87,7 @@ export interface ExecPipelineOptions {
|
|
|
87
87
|
stripWarning?: StripWarningLocation;
|
|
88
88
|
endingOverrides?: (LineEnding | undefined)[];
|
|
89
89
|
preserveDeletionSeparators?: boolean;
|
|
90
|
+
anchorCarry?: number;
|
|
90
91
|
}
|
|
91
92
|
|
|
92
93
|
export function hashSpan(hashes: string[], from: string, to: string): [number, number] | undefined {
|
|
@@ -205,20 +206,22 @@ export async function execPipeline(
|
|
|
205
206
|
const result = anchorResult.content;
|
|
206
207
|
const isNoop = result === originalNormalized;
|
|
207
208
|
|
|
209
|
+
const pipeSpan = isNoop ? undefined : spanForEdit(originalHashes, effectiveEdit.hash_bounds[0].hash, effectiveEdit.hash_bounds[1].hash, result);
|
|
210
|
+
if (pipeSpan && options?.anchorCarry !== undefined) pipeSpan.carry = options.anchorCarry;
|
|
211
|
+
const pipeSpans = pipeSpan ? [pipeSpan] : undefined;
|
|
212
|
+
|
|
208
213
|
const resultHashes = isNoop
|
|
209
214
|
? originalHashes
|
|
210
215
|
: await lineHashes(result, absolutePath, {
|
|
211
216
|
content: originalNormalized,
|
|
212
217
|
hashes: originalHashes,
|
|
218
|
+
...(pipeSpans ? { spans: pipeSpans } : {}),
|
|
213
219
|
}, hashStore, false, true);
|
|
214
220
|
const warnings = [...editWarnings, ...(anchorResult.warnings ?? [])];
|
|
215
221
|
await throwIfStrictInput(warnings);
|
|
216
222
|
const { totalAddedLines, totalRemovedLines } = countLineChanges(
|
|
217
223
|
effectiveEdit, originalHashes, isNoop,
|
|
218
224
|
);
|
|
219
|
-
|
|
220
|
-
const pipeSpan = isNoop ? undefined : spanForEdit(originalHashes, effectiveEdit.hash_bounds[0].hash, effectiveEdit.hash_bounds[1].hash, result);
|
|
221
|
-
const pipeSpans = pipeSpan ? [pipeSpan] : undefined;
|
|
222
225
|
return {
|
|
223
226
|
path: displayPath,
|
|
224
227
|
originalNormalized,
|
package/src/structured.ts
CHANGED
|
@@ -45,16 +45,17 @@ export type ReadTextResult = {
|
|
|
45
45
|
truncated: boolean;
|
|
46
46
|
blockedByLongLine: boolean;
|
|
47
47
|
hadUtf8DecodeErrors: boolean;
|
|
48
|
+
notices: string[];
|
|
48
49
|
};
|
|
49
50
|
|
|
50
51
|
export type ReadImageResult = {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
path: string;
|
|
52
|
+
type: "image";
|
|
53
|
+
data: string;
|
|
54
54
|
mimeType: string;
|
|
55
|
+
note: string;
|
|
55
56
|
};
|
|
56
57
|
|
|
57
|
-
export type ReadResult = ReadTextResult | ReadImageResult | StructuredError;
|
|
58
|
+
export type ReadResult = ReadTextResult | ReadImageResult | string | StructuredError;
|
|
58
59
|
|
|
59
60
|
export const readResultSchema = Type.Union([
|
|
60
61
|
Type.Object({
|
|
@@ -69,13 +70,15 @@ export const readResultSchema = Type.Union([
|
|
|
69
70
|
truncated: Type.Boolean(),
|
|
70
71
|
blockedByLongLine: Type.Boolean(),
|
|
71
72
|
hadUtf8DecodeErrors: Type.Boolean(),
|
|
73
|
+
notices: Type.Array(Type.String()),
|
|
72
74
|
}),
|
|
73
75
|
Type.Object({
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
path: Type.String(),
|
|
76
|
+
type: Type.Literal("image"),
|
|
77
|
+
data: Type.String(),
|
|
77
78
|
mimeType: Type.String(),
|
|
79
|
+
note: Type.String(),
|
|
78
80
|
}),
|
|
81
|
+
Type.String(),
|
|
79
82
|
structuredErrorSchema,
|
|
80
83
|
]);
|
|
81
84
|
|
package/src/utils.ts
CHANGED
|
@@ -41,6 +41,24 @@ export function splitLines(text: string): string[] {
|
|
|
41
41
|
return text.endsWith("\n") ? lines.slice(0, -1) : lines;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
export function spliceAll<T>(target: T[], start: number, deleteCount: number, items: readonly T[]): T[] {
|
|
45
|
+
const rawStart = Number.isNaN(start) ? 0 : Math.trunc(start);
|
|
46
|
+
const at = rawStart < 0 ? Math.max(target.length + rawStart, 0) : Math.min(rawStart, target.length);
|
|
47
|
+
const rawCount = Number.isNaN(deleteCount) ? 0 : Math.trunc(deleteCount);
|
|
48
|
+
const remove = Math.max(0, Math.min(rawCount, target.length - at));
|
|
49
|
+
const removed = target.slice(at, at + remove);
|
|
50
|
+
const tail = target.slice(at + remove);
|
|
51
|
+
const additions: readonly T[] = items === target ? [...items] : items;
|
|
52
|
+
target.length = at;
|
|
53
|
+
for (const item of additions) target.push(item);
|
|
54
|
+
for (const item of tail) target.push(item);
|
|
55
|
+
return removed;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function anchorCarryIndex(direction: "after" | "before", lineCount: number): number {
|
|
59
|
+
return direction === "after" ? 0 : lineCount;
|
|
60
|
+
}
|
|
61
|
+
|
|
44
62
|
export function visLines(text: string): string[] {
|
|
45
63
|
return text.length === 0 ? [] : splitLines(text);
|
|
46
64
|
}
|
|
@@ -194,10 +212,12 @@ export function clipLine(line: string, maxLen = 200): string {
|
|
|
194
212
|
const flat = line.replace(/\n/g, "\\n");
|
|
195
213
|
return flat.length > maxLen ? `${flat.slice(0, maxLen)}...` : flat;
|
|
196
214
|
}
|
|
197
|
-
export function
|
|
198
|
-
const count = splitLines(content).length;
|
|
215
|
+
export function assertLineCount(count: number, displayPath: string, limit: number): void {
|
|
199
216
|
if (count > limit) throw new Error(formatLineLimit(displayPath, limit, count));
|
|
200
217
|
}
|
|
218
|
+
export function assertLineLimit(content: string, displayPath: string, limit: number): void {
|
|
219
|
+
assertLineCount(splitLines(content).length, displayPath, limit);
|
|
220
|
+
}
|
|
201
221
|
export function assertByteLimit(content: string, displayPath: string, limit = MAX_BYTES): void {
|
|
202
222
|
if (Buffer.byteLength(content, "utf-8") > limit) {
|
|
203
223
|
throw new Error(`[E_FILE_TOO_LARGE] File is too large: ${displayPath} (exceeds the ${limit / (1024 * 1024)}MB size limit). For very large files, use write.`);
|