@criogaid/pi-hashline-edit 0.6.0 → 0.7.0
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 +161 -304
- package/THIRD_PARTY_NOTICES.md +80 -0
- package/package.json +10 -6
- package/src/core/AGENTS.md +20 -0
- package/src/core/apply.test.ts +189 -60
- package/src/core/apply.ts +110 -74
- package/src/core/case-fold.test.ts +40 -0
- package/src/core/case-fold.ts +81 -0
- package/src/core/character-set.test.ts +103 -0
- package/src/core/character-set.ts +170 -0
- package/src/core/errors.test.ts +23 -0
- package/src/core/errors.ts +70 -4
- package/src/core/lines.ts +7 -0
- package/src/core/regex.ts +21 -0
- package/src/core/replace-schema.ts +34 -0
- package/src/core/replace.ts +37 -54
- package/src/core/search-analysis.test.ts +199 -0
- package/src/core/search-analysis.ts +375 -0
- package/src/core/search-matrix.test.ts +64 -0
- package/src/core/search-matrix.testing.ts +183 -0
- package/src/core/search-schema.ts +18 -0
- package/src/core/search.test.ts +145 -0
- package/src/core/search.ts +119 -0
- package/src/core/text.test.ts +16 -8
- package/src/core/text.ts +26 -6
- package/src/core/types.ts +16 -11
- package/src/index.ts +25 -15
- package/src/integration/config-registration.test.ts +30 -24
- package/src/integration/forget-grep.test.ts +52 -2
- package/src/integration/fusion-dispatch.test.ts +21 -105
- package/src/integration/grep-errors.test.ts +50 -0
- package/src/integration/grep-flags.test.ts +280 -0
- package/src/integration/grep-js-completeness.test.ts +208 -0
- package/src/integration/grep-line-candidates.test.ts +233 -0
- package/src/integration/grep-matrix.test.ts +121 -0
- package/src/integration/grep-path-matrix.test.ts +132 -0
- package/src/integration/grep-prefilter-coverage.test.ts +189 -0
- package/src/integration/grep-regex.test.ts +410 -0
- package/src/integration/grep-rg.test.ts +220 -402
- package/src/integration/grep-scan-budget.test.ts +89 -0
- package/src/integration/grep-translation.test.ts +206 -0
- package/src/integration/large-text.test.ts +57 -1
- package/src/integration/model-budget-lifecycle.test.ts +128 -0
- package/src/integration/mutation-file-types.test.ts +12 -25
- package/src/integration/mutation-file-types.testing.ts +33 -0
- package/src/integration/npm-package.test.ts +17 -0
- package/src/integration/npm-package.testing.ts +123 -0
- package/src/integration/publication-read-changes.test.ts +344 -0
- package/src/integration/read-changes.test.ts +98 -0
- package/src/integration/replace-package.test.ts +17 -36
- package/src/integration/replace-package.testing.ts +33 -0
- package/src/integration/replace-worker-boundary.test.ts +107 -0
- package/src/integration/report-replay.test.ts +90 -0
- package/src/integration/utf8-errors.test.ts +46 -0
- package/src/pi/AGENTS.md +55 -0
- package/src/pi/action-fusion.test.ts +253 -103
- package/src/pi/action-fusion.ts +116 -290
- package/src/pi/anchor-format.ts +9 -0
- package/src/pi/argument-diagnostics.ts +478 -0
- package/src/pi/argument-error.testing.ts +43 -0
- package/src/pi/argument-error.ts +78 -0
- package/src/pi/argument-format.test.ts +208 -0
- package/src/pi/argument-validation.test.ts +742 -45
- package/src/pi/argument-validation.ts +56 -59
- package/src/pi/budgets.ts +45 -16
- package/src/pi/codemode.test.ts +96 -0
- package/src/pi/config.test.ts +14 -10
- package/src/pi/config.ts +21 -7
- package/src/pi/diagnostic-buffer.test.ts +52 -16
- package/src/pi/diagnostic-buffer.ts +11 -14
- package/src/pi/directory-sync.test.ts +4 -1
- package/src/pi/edit-tool.ts +100 -93
- package/src/pi/error-text.ts +39 -16
- package/src/pi/execute.test.ts +362 -315
- package/src/pi/extension-loading.test.ts +14 -0
- package/src/pi/extension-loading.testing.ts +3 -0
- package/src/pi/failure-context.test.ts +220 -88
- package/src/pi/failure-context.ts +161 -180
- package/src/pi/failure-path.test.ts +85 -88
- package/src/pi/file-commit.test.ts +78 -45
- package/src/pi/file-commit.ts +218 -151
- package/src/pi/file-read.test.ts +29 -0
- package/src/pi/file-read.ts +60 -0
- package/src/pi/file-snapshot.test.ts +1 -1
- package/src/pi/forget-tool.ts +189 -90
- package/src/pi/forget.test.ts +309 -22
- package/src/pi/forget.testing.ts +41 -113
- package/src/pi/fusion-card.test.ts +40 -14
- package/src/pi/fusion-card.ts +84 -50
- package/src/pi/grep-candidates.test.ts +127 -0
- package/src/pi/grep-candidates.ts +193 -0
- package/src/pi/grep-matrix.test.ts +83 -0
- package/src/pi/grep-output.ts +181 -138
- package/src/pi/grep-render.ts +28 -25
- package/src/pi/grep-scope.ts +61 -53
- package/src/pi/grep-search.ts +299 -242
- package/src/pi/grep-tool.ts +150 -140
- package/src/pi/grep-worker.mjs +4 -0
- package/src/pi/grep-worker.ts +14 -0
- package/src/pi/grep.test.ts +631 -632
- package/src/pi/model-budgets.test.ts +43 -0
- package/src/pi/model-budgets.ts +44 -0
- package/src/pi/mutation-outcome.testing.ts +5 -5
- package/src/pi/mutation-result.test.ts +21 -74
- package/src/pi/mutation-result.ts +74 -87
- package/src/pi/mutation-runner.ts +83 -75
- package/src/pi/output-budgets.test.ts +166 -0
- package/src/pi/path.ts +7 -0
- package/src/pi/read-tool.ts +151 -137
- package/src/pi/read.test.ts +39 -33
- package/src/pi/regex-worker-contract.ts +73 -0
- package/src/pi/regex-worker.ts +101 -0
- package/src/pi/registration.test.ts +111 -92
- package/src/pi/render.test.ts +193 -19
- package/src/pi/render.ts +137 -29
- package/src/pi/replace-regex.ts +11 -44
- package/src/pi/replace-tool.ts +38 -145
- package/src/pi/replace-worker-contract.ts +19 -0
- package/src/pi/replace-worker.ts +12 -8
- package/src/pi/replace.test.ts +93 -160
- package/src/pi/report-cause.ts +99 -0
- package/src/pi/report-failure.testing.ts +20 -0
- package/src/pi/report-schema.ts +512 -0
- package/src/pi/report.test.ts +276 -0
- package/src/pi/report.ts +288 -0
- package/src/pi/result-safety.test.ts +122 -427
- package/src/pi/rg-process.test.ts +78 -79
- package/src/pi/rg-process.ts +124 -177
- package/src/pi/rg-regex.test.ts +50 -0
- package/src/pi/rg-regex.ts +709 -0
- package/src/pi/text-stream.test.ts +67 -38
- package/src/pi/text-stream.ts +96 -75
- package/src/pi/tool-call.testing.ts +3 -39
- package/src/pi/write-tool.ts +45 -34
- package/src/pi/write.test.ts +45 -37
- package/src/testing/extension-loader.testing.ts +24 -0
- package/src/testing/installation.testing.ts +77 -0
- package/src/testing/process.testing.ts +33 -0
- package/src/testing/regex-host.testing.ts +20 -0
- package/src/testing/regex-translation-corpus.ts +308 -0
- package/src/testing/rg-line-listing.fixture.mjs +7 -0
- package/src/testing/run-tests.ts +47 -0
- package/src/testing/session.testing.ts +127 -0
- package/src/pi/rg-line-ranges.test.ts +0 -66
- package/src/pi/rg-line-ranges.ts +0 -75
- package/src/pi/rg-match-bytes.ts +0 -95
- package/src/pi/rg-text-view.ts +0 -192
- package/src/pi/tool-prompts.ts +0 -15
|
@@ -1,43 +1,49 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Bounded
|
|
3
|
-
* candidates,
|
|
2
|
+
* Bounded edit failure facts for the error record: anchor failures with their
|
|
3
|
+
* recovery candidates or observed rows, the batch's matched anchors, and
|
|
4
|
+
* ambiguous-candidate neighborhoods.
|
|
5
|
+
*
|
|
6
|
+
* Each fact has one owner. A failure entry states what was cited and what the
|
|
7
|
+
* search found; a shifted candidate's anchor is stated once, beside its
|
|
8
|
+
* `content`; `matched` lists only the anchors that passed, so a mismatch
|
|
9
|
+
* appears once, in `failures`; a row shown in a neighborhood is not repeated
|
|
10
|
+
* in a failure entry. The `observed` and neighborhood rows are observations,
|
|
11
|
+
* not verified targets. Every list has its own byte budget and an explicit
|
|
12
|
+
* omission count. The recovery instruction is the report's `next`, derived
|
|
13
|
+
* from the code in report-schema.ts.
|
|
4
14
|
*
|
|
5
15
|
* @module pi-hashline-edit/pi
|
|
6
16
|
*/
|
|
7
17
|
|
|
8
|
-
import {
|
|
9
|
-
import type { AnchorFormatter } from "./anchor-format.ts";
|
|
18
|
+
import { type AnchorFormatter, displayCarriageReturns } from "./anchor-format.ts";
|
|
10
19
|
import { splitLines } from "../core/lines.ts";
|
|
11
20
|
import type { Anchor, AnchorFailure, ApplyFailure } from "../core/types.ts";
|
|
12
21
|
import { mergeRanges } from "../core/ranges.ts";
|
|
13
|
-
import {
|
|
22
|
+
import { HashlineError } from "../core/errors.ts";
|
|
23
|
+
import { DEFAULT_OUTPUT_BUDGETS, formatKiB, type OutputBudgets } from "./budgets.ts";
|
|
24
|
+
import { boundedFacts } from "./report.ts";
|
|
25
|
+
import type { AnchorFailureFact, ErrorFacts } from "./report-schema.ts";
|
|
26
|
+
|
|
27
|
+
type MismatchFacts = ErrorFacts<"ANCHOR_MISMATCH">;
|
|
14
28
|
|
|
15
29
|
const CONTEXT_RADIUS = 3;
|
|
16
30
|
const MAX_AMBIGUOUS_CANDIDATES = 8;
|
|
17
31
|
|
|
32
|
+
type Snapshot = Readonly<{ currentText: string; anchors: AnchorFormatter }>;
|
|
33
|
+
|
|
18
34
|
/** Select the shared candidate prefix for detail lists and observation neighborhoods. */
|
|
19
35
|
export function selectAmbiguousCandidates(candidates: readonly Anchor[]): readonly Anchor[] {
|
|
20
36
|
return candidates.slice(0, MAX_AMBIGUOUS_CANDIDATES);
|
|
21
37
|
}
|
|
22
38
|
|
|
23
|
-
type Interval = { lo: number; hi: number };
|
|
24
39
|
type ContextRow = { line: number; text: string };
|
|
25
40
|
|
|
26
|
-
function shownIntervals(rows: readonly ContextRow[]): Interval[] {
|
|
27
|
-
const intervals: Interval[] = [];
|
|
28
|
-
for (const row of rows) {
|
|
29
|
-
const previous = intervals.at(-1);
|
|
30
|
-
if (previous && row.line === previous.hi + 1) previous.hi = row.line;
|
|
31
|
-
else intervals.push({ lo: row.line, hi: row.line });
|
|
32
|
-
}
|
|
33
|
-
return intervals;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
41
|
function collectContextRows(
|
|
37
42
|
lines: readonly string[],
|
|
38
43
|
centers: readonly number[],
|
|
39
44
|
anchors: AnchorFormatter,
|
|
40
45
|
candidateLines: ReadonlySet<number>,
|
|
46
|
+
budget: OutputBudgets,
|
|
41
47
|
) {
|
|
42
48
|
const windows = mergeRanges(
|
|
43
49
|
centers.map((center) => {
|
|
@@ -51,210 +57,185 @@ function collectContextRows(
|
|
|
51
57
|
const total = windows.reduce((sum, [start, end]) => sum + end - start, 0);
|
|
52
58
|
const rows: ContextRow[] = [];
|
|
53
59
|
let bytes = 0;
|
|
54
|
-
const omissions = new Set<"byte limit" | "candidate row limit">();
|
|
55
60
|
|
|
56
61
|
for (const [start, end] of windows) {
|
|
57
62
|
for (let line = start; line < end; line++) {
|
|
58
|
-
const
|
|
59
|
-
const text = anchors.row(line, content);
|
|
63
|
+
const text = anchors.row(line, lines[line - 1]);
|
|
60
64
|
const rowBytes = Buffer.byteLength(text, "utf8");
|
|
61
65
|
// Neighborhoods must not bypass the standalone candidate's complete-row limit.
|
|
62
|
-
if (candidateLines.has(line) && rowBytes >
|
|
63
|
-
|
|
64
|
-
continue;
|
|
65
|
-
}
|
|
66
|
-
if (bytes + rowBytes + 1 > MAX_BLOCK_BYTES) {
|
|
67
|
-
omissions.add("byte limit");
|
|
68
|
-
continue;
|
|
69
|
-
}
|
|
66
|
+
if (candidateLines.has(line) && rowBytes > budget.fieldBytes) continue;
|
|
67
|
+
if (bytes + rowBytes + 1 > budget.blockBytes) continue;
|
|
70
68
|
rows.push({ line, text });
|
|
71
69
|
bytes += rowBytes + 1;
|
|
72
70
|
}
|
|
73
71
|
}
|
|
74
|
-
return { rows, total
|
|
72
|
+
return { rows, omitted: total - rows.length };
|
|
75
73
|
}
|
|
76
74
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
75
|
+
/** Group shown rows into contiguous neighborhoods; gaps mark omitted rows. */
|
|
76
|
+
function groupContextRows(rows: readonly ContextRow[]) {
|
|
77
|
+
const groups: { lines: string; rows: string[] }[] = [];
|
|
78
|
+
let previous: ContextRow | undefined;
|
|
79
|
+
let first = 0;
|
|
80
|
+
for (const row of rows) {
|
|
81
|
+
if (!previous || row.line !== previous.line + 1) {
|
|
82
|
+
groups.push({ lines: "", rows: [] });
|
|
83
|
+
first = row.line;
|
|
84
|
+
}
|
|
85
|
+
const group = groups[groups.length - 1];
|
|
86
|
+
group.rows.push(row.text);
|
|
87
|
+
group.lines = `${first}-${row.line}`;
|
|
88
|
+
previous = row;
|
|
83
89
|
}
|
|
84
|
-
return
|
|
90
|
+
return groups;
|
|
85
91
|
}
|
|
86
92
|
|
|
87
93
|
/**
|
|
88
|
-
*
|
|
89
|
-
*
|
|
94
|
+
* Observation rows (±3) around listed ambiguous candidates, lowest lines first,
|
|
95
|
+
* and the lines actually shown so failure entries do not repeat them.
|
|
96
|
+
* @internal Performs no I/O.
|
|
90
97
|
*/
|
|
91
|
-
export function
|
|
98
|
+
export function ambiguousCandidateNeighborhoods(
|
|
92
99
|
currentText: string,
|
|
93
100
|
failures: readonly AnchorFailure[],
|
|
94
101
|
anchors: AnchorFormatter,
|
|
95
|
-
|
|
102
|
+
budget: OutputBudgets = DEFAULT_OUTPUT_BUDGETS,
|
|
103
|
+
): {
|
|
104
|
+
facts: Pick<MismatchFacts, "candidateNeighborhoods" | "omittedNeighborhoodRows">;
|
|
105
|
+
shownLines: ReadonlySet<number>;
|
|
106
|
+
} {
|
|
96
107
|
const centers = failures.flatMap((failure) =>
|
|
97
108
|
failure.recovery.kind === "ambiguous"
|
|
98
109
|
? selectAmbiguousCandidates(failure.recovery.candidates).map((candidate) => candidate.line)
|
|
99
110
|
: [],
|
|
100
111
|
);
|
|
101
|
-
if (centers.length === 0) return {
|
|
112
|
+
if (centers.length === 0) return { facts: {}, shownLines: new Set() };
|
|
102
113
|
|
|
103
|
-
const lines = splitLines(currentText);
|
|
104
114
|
const candidateLines = new Set(centers);
|
|
105
115
|
for (const failure of failures) {
|
|
106
116
|
if (failure.recovery.kind === "found") candidateLines.add(failure.recovery.newLine);
|
|
107
117
|
}
|
|
108
|
-
const { rows,
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
return { text: `\n${body.join("\n")}`, shownLines: new Set(rows.map((row) => row.line)) };
|
|
118
|
+
const { rows, omitted } = collectContextRows(
|
|
119
|
+
splitLines(currentText),
|
|
120
|
+
centers,
|
|
121
|
+
anchors,
|
|
122
|
+
candidateLines,
|
|
123
|
+
budget,
|
|
124
|
+
);
|
|
125
|
+
return {
|
|
126
|
+
facts: {
|
|
127
|
+
candidateNeighborhoods: groupContextRows(rows),
|
|
128
|
+
...(omitted ? { omittedNeighborhoodRows: omitted } : {}),
|
|
129
|
+
},
|
|
130
|
+
shownLines: new Set(rows.map((row) => row.line)),
|
|
131
|
+
};
|
|
124
132
|
}
|
|
125
133
|
|
|
126
|
-
|
|
127
|
-
function boundDiagnostic(message: string, notice: string): string {
|
|
128
|
-
const bounded = truncateHead(message, { maxBytes: MAX_BLOCK_BYTES - Buffer.byteLength(notice) });
|
|
129
|
-
return bounded.content + (bounded.truncated ? notice : "");
|
|
130
|
-
}
|
|
131
|
-
/** Format failure mappings and complete unique-candidate rows within the detail budget. */
|
|
132
|
-
function formatFailureDetails(
|
|
133
|
-
failure: ApplyFailure,
|
|
134
|
-
snapshot: Readonly<{ currentText: string; anchors: AnchorFormatter }>,
|
|
135
|
-
candidateLines: ReadonlySet<number>,
|
|
136
|
-
): string {
|
|
137
|
-
if (failure.kind !== "anchor") return failure.message;
|
|
134
|
+
const RESULT = { found: "shifted", ambiguous: "ambiguous", none: "unresolved" } as const;
|
|
138
135
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
detail += ` Candidate content exceeds ${formatKiB(MAX_RECOVERY_CANDIDATE_BYTES)}.`;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
lines.push(detail);
|
|
175
|
-
break;
|
|
176
|
-
}
|
|
177
|
-
case "ambiguous": {
|
|
178
|
-
const candidates = selectAmbiguousCandidates(f.recovery.candidates);
|
|
179
|
-
const list = candidates
|
|
180
|
-
.map((candidate) => `"${snapshot.anchors.reference(candidate.line, candidate.hash)}"`)
|
|
181
|
-
.join(" / ");
|
|
182
|
-
const omitted = f.recovery.candidates.length - candidates.length;
|
|
183
|
-
const more = omitted ? ` (${omitted} more candidates omitted)` : "";
|
|
184
|
-
lines.push(`• ${where}: ambiguous checksum matches: ${list}${more}. ${search}`);
|
|
185
|
-
break;
|
|
186
|
-
}
|
|
187
|
-
case "none": {
|
|
188
|
-
const row =
|
|
189
|
-
f.current === null ? null : snapshot.anchors.row(f.cited.line, f.current.content);
|
|
190
|
-
if (row !== null && Buffer.byteLength(row, "utf8") <= MAX_RECOVERY_CANDIDATE_BYTES) {
|
|
191
|
-
lines.push(
|
|
192
|
-
`• ${where}: no checksum-matching candidate found. Current cited line (validation snapshot; observation only):\n${row}\nConfirm this is the intended target before reusing its anchor directly; retries revalidate.`,
|
|
193
|
-
);
|
|
194
|
-
} else {
|
|
195
|
-
lines.push(
|
|
196
|
-
`• ${where}: no checksum-matching candidate found. Use read or grep to inspect the current file before retrying.` +
|
|
197
|
-
(f.current === null
|
|
198
|
-
? " Cited line is out of range."
|
|
199
|
-
: ` Current row exceeds ${formatKiB(MAX_RECOVERY_CANDIDATE_BYTES)}.`),
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
break;
|
|
136
|
+
/** One failure entry: what was cited, then what the search found or what the cited line holds. */
|
|
137
|
+
function failureEntry(
|
|
138
|
+
f: AnchorFailure,
|
|
139
|
+
snapshot: Snapshot,
|
|
140
|
+
currentLines: readonly string[],
|
|
141
|
+
shownLines: Set<number>,
|
|
142
|
+
budget: OutputBudgets,
|
|
143
|
+
): AnchorFailureFact {
|
|
144
|
+
const rowTooLarge = `row exceeds ${formatKiB(budget.fieldBytes)}`;
|
|
145
|
+
const entry: { -readonly [K in keyof AnchorFailureFact]: AnchorFailureFact[K] } = {
|
|
146
|
+
field: `edits[${f.opIndex}].${f.which}`,
|
|
147
|
+
op: f.op,
|
|
148
|
+
cited: snapshot.anchors.reference(f.cited.line, f.cited.hash),
|
|
149
|
+
result: RESULT[f.recovery.kind],
|
|
150
|
+
};
|
|
151
|
+
if (f.recovery.kind !== "none")
|
|
152
|
+
entry.search = f.recovery.scope === "local" ? "local window" : "full file";
|
|
153
|
+
switch (f.recovery.kind) {
|
|
154
|
+
case "found": {
|
|
155
|
+
const { newLine, newHash } = f.recovery;
|
|
156
|
+
entry.candidate = snapshot.anchors.reference(newLine, newHash);
|
|
157
|
+
if (shownLines.has(newLine)) break;
|
|
158
|
+
const content = currentLines[newLine - 1];
|
|
159
|
+
// The row budget matches the neighborhoods'; the anchor itself is already `candidate`.
|
|
160
|
+
if (
|
|
161
|
+
content !== undefined &&
|
|
162
|
+
Buffer.byteLength(snapshot.anchors.row(newLine, content), "utf8") <= budget.fieldBytes
|
|
163
|
+
) {
|
|
164
|
+
entry.content = displayCarriageReturns(content);
|
|
165
|
+
shownLines.add(newLine);
|
|
166
|
+
} else {
|
|
167
|
+
entry.contentOmitted = rowTooLarge;
|
|
203
168
|
}
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
case "ambiguous": {
|
|
172
|
+
const candidates = selectAmbiguousCandidates(f.recovery.candidates);
|
|
173
|
+
entry.candidates = candidates.map((candidate) =>
|
|
174
|
+
snapshot.anchors.reference(candidate.line, candidate.hash),
|
|
175
|
+
);
|
|
176
|
+
const omitted = f.recovery.candidates.length - candidates.length;
|
|
177
|
+
if (omitted) entry.omittedCandidates = omitted;
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
case "none": {
|
|
181
|
+
const row = f.current === null ? null : snapshot.anchors.row(f.cited.line, f.current.content);
|
|
182
|
+
if (row === null) entry.observedOmitted = "cited line is out of range";
|
|
183
|
+
else if (Buffer.byteLength(row, "utf8") <= budget.fieldBytes) entry.observed = row;
|
|
184
|
+
else entry.observedOmitted = rowTooLarge;
|
|
185
|
+
break;
|
|
204
186
|
}
|
|
205
187
|
}
|
|
206
|
-
|
|
207
|
-
if (found) parts.push(`${found} shifted`);
|
|
208
|
-
if (ambiguous) parts.push(`${ambiguous} ambiguous`);
|
|
209
|
-
if (none) parts.push(`${none} unresolved`);
|
|
210
|
-
const message = [
|
|
211
|
-
`Anchor mismatch: ${parts.join(", ")}.`,
|
|
212
|
-
"No changes written by this edit batch.",
|
|
213
|
-
...lines,
|
|
214
|
-
].join("\n");
|
|
215
|
-
return boundDiagnostic(
|
|
216
|
-
message,
|
|
217
|
-
`\nDiagnostic output truncated at ${formatKiB(MAX_BLOCK_BYTES)}.`,
|
|
218
|
-
);
|
|
188
|
+
return entry;
|
|
219
189
|
}
|
|
220
190
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
191
|
+
/**
|
|
192
|
+
* The batch's anchors whose checksum matched, by field. An anchor absent from
|
|
193
|
+
* both `matched` and `failures` is counted as omitted, never implied matched.
|
|
194
|
+
*/
|
|
195
|
+
function matchedAnchorFacts(
|
|
196
|
+
failure: ApplyFailure,
|
|
197
|
+
budget: OutputBudgets,
|
|
198
|
+
): Pick<MismatchFacts, "matched" | "omittedMatched"> {
|
|
199
|
+
const { kept, omitted } = boundedFacts(
|
|
200
|
+
failure.checks
|
|
201
|
+
.filter((check) => check.status === "matched")
|
|
202
|
+
.map((check) => `edits[${check.opIndex}].${check.which}`),
|
|
203
|
+
budget.blockBytes,
|
|
234
204
|
);
|
|
205
|
+
return kept.length || omitted
|
|
206
|
+
? { matched: kept, ...(omitted ? { omittedMatched: omitted } : {}) }
|
|
207
|
+
: {};
|
|
235
208
|
}
|
|
236
209
|
|
|
237
|
-
/**
|
|
238
|
-
export function
|
|
210
|
+
/** Classify a rejected edit batch with its bounded facts. */
|
|
211
|
+
export function describeEditFailure(
|
|
239
212
|
failure: ApplyFailure,
|
|
240
|
-
snapshot:
|
|
213
|
+
snapshot: Snapshot,
|
|
241
214
|
isBatch: boolean,
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
const
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
215
|
+
budget: OutputBudgets = DEFAULT_OUTPUT_BUDGETS,
|
|
216
|
+
): HashlineError<"ANCHOR_MISMATCH" | "INVALID_RANGE" | "OVERLAPPING_EDITS"> {
|
|
217
|
+
const matched = isBatch ? matchedAnchorFacts(failure, budget) : {};
|
|
218
|
+
if (failure.kind === "range")
|
|
219
|
+
return new HashlineError(failure.code, failure.message, { facts: matched });
|
|
220
|
+
|
|
221
|
+
const neighborhoods = ambiguousCandidateNeighborhoods(
|
|
222
|
+
snapshot.currentText,
|
|
223
|
+
failure.failures,
|
|
224
|
+
snapshot.anchors,
|
|
225
|
+
budget,
|
|
226
|
+
);
|
|
227
|
+
const currentLines = splitLines(snapshot.currentText);
|
|
228
|
+
const shownLines = new Set(neighborhoods.shownLines);
|
|
229
|
+
const { kept, omitted } = boundedFacts(
|
|
230
|
+
failure.failures.map((f) => failureEntry(f, snapshot, currentLines, shownLines, budget)),
|
|
231
|
+
budget.blockBytes,
|
|
232
|
+
);
|
|
233
|
+
return new HashlineError("ANCHOR_MISMATCH", "Anchors did not match the current file.", {
|
|
234
|
+
facts: {
|
|
235
|
+
failures: kept,
|
|
236
|
+
...(omitted ? { omittedFailures: omitted } : {}),
|
|
237
|
+
...matched,
|
|
238
|
+
...neighborhoods.facts,
|
|
239
|
+
},
|
|
240
|
+
});
|
|
260
241
|
}
|