dsh-better-edit 0.6.1 → 0.7.1

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/lib/constants.js CHANGED
@@ -10,5 +10,9 @@ export const BATCH_EDIT_MAX_ITEMS = EDITS_MAX_ITEMS;
10
10
  export const SERVED_TTL_MS = 7 * 24 * 60 * 60 * 1000;
11
11
  export const SERVED_ECHO_CAP = 150;
12
12
  export const NOOP_LOOP_THRESHOLD = 3;
13
- export const NEW_CONTENT_NOT_STRING_MSG = `[E_BAD_SHAPE] "replacement_text" must be a string with \\n line separators, not an array.` +
13
+ export const NEW_CONTENT_NOT_STRING_MSG = `[MODEL] [E_BAD_PAYLOAD] "replacement_text" must be a string with \\n line separators, not an array.` +
14
14
  ` Do not pass an array of lines — pass the replacement text as one string: "line1\\nline2". Use "" to delete a range.`;
15
+ export function eLargeFileMsg(displayPath, lineCount, maxLines) {
16
+ return `[MODEL] [E_LARGE_FILE] ${displayPath} has ${lineCount} lines, exceeding the ${maxLines}-line edit limit. Hashline editing targets source-sized files; for very large files use write or a non-line-based approach.`;
17
+ }
18
+ export const E_ANCHOR_SPACE_EXHAUSTED_WARNING = "[E_ANCHOR_SPACE_EXHAUSTED] Anchor space exhausted";
package/lib/contract.js CHANGED
@@ -2,14 +2,14 @@
2
2
  * One module owns the request shapes for the hashline tools — edit,
3
3
  * read, undo_last_edit — plus their validation. Field sets are
4
4
  * declared once here; every tool validates through these asserts, and the
5
- * [E_BAD_SHAPE] vocabulary is shared instead of re-implemented per tool.
5
+ * [E_BAD_PAYLOAD] vocabulary is shared instead of re-implemented per tool.
6
6
  *
7
7
  * Contract now mirrors upstream ADR-0007: {path: string|null, edits: [[remove_from,remove_to,replacement_text],...]} tuple payload,
8
8
  * single-file, atomic. batch_edit is removed.
9
9
  * @module dsh-better-edit/contract
10
10
  */
11
11
  import { EDITS_MAX_ITEMS } from "./constants.js";
12
- import { isRec, normalizeFilePath, rejectUnknownFields } from "./utils.js";
12
+ import { isRec, normalizeFilePath, rejectUnknownFields, CodedError } from "./utils.js";
13
13
  // ---- normalized marker -----------------------------------------------------
14
14
  export const normalizedEdit = Symbol("normalizedEdit");
15
15
  export function isNormalizedEdit(input) {
@@ -33,7 +33,16 @@ export function editRequestFrom(input) {
33
33
  // but handle here for direct calls
34
34
  }
35
35
  const { path, edits } = rec;
36
- if (path !== null && (typeof path !== "string" || path.length === 0))
36
+ // Gemma-4 tool-call bleed (#55): the model may wrap the path in <|>, │, |,
37
+ // quotes, or backticks after seeing │ separators. Strip wrappers iteratively.
38
+ let effectivePath = path;
39
+ if (typeof effectivePath === "string") {
40
+ const sanitized = sanitizePath(effectivePath);
41
+ if (sanitized === null)
42
+ return undefined;
43
+ effectivePath = sanitized;
44
+ }
45
+ if (effectivePath !== null && (typeof effectivePath !== "string" || effectivePath.length === 0))
37
46
  return undefined;
38
47
  if (!Array.isArray(edits) || edits.length === 0)
39
48
  return undefined;
@@ -44,7 +53,47 @@ export function editRequestFrom(input) {
44
53
  return undefined;
45
54
  items.push(normalized);
46
55
  }
47
- return { path: path, edits: items };
56
+ return { path: effectivePath, edits: items };
57
+ }
58
+ /**
59
+ * Strip Gemma-4 tool-call bleed wrappers from a path string: `<|>`, `\u2502`, `|`
60
+ * pairs plus surrounding quotes/backticks, applied iteratively. Returns the
61
+ * cleaned path, or null when nothing usable remains (caller rejects).
62
+ */
63
+ /** Paired/single bleed wrappers, longest-token first. Each entry strips one layer. */
64
+ const PATH_WRAPPERS = [
65
+ { open: "<|>", close: "<|>", minLen: 6, pairOnly: false },
66
+ { open: "\u2502", close: "\u2502", minLen: 2, pairOnly: true },
67
+ { open: "|", close: "|", minLen: 2, pairOnly: true },
68
+ { open: '"', close: '"', minLen: 0, pairOnly: true },
69
+ { open: "'", close: "'", minLen: 0, pairOnly: true },
70
+ { open: "`", close: "`", minLen: 0, pairOnly: true },
71
+ ];
72
+ export function sanitizePath(value) {
73
+ if (typeof value !== "string")
74
+ return null;
75
+ let cleaned = value.trim();
76
+ let changed = true;
77
+ while (changed) {
78
+ changed = false;
79
+ for (const w of PATH_WRAPPERS) {
80
+ if (cleaned.length > w.minLen && cleaned.startsWith(w.open) && cleaned.endsWith(w.close)) {
81
+ cleaned = cleaned.slice(w.open.length, -w.close.length).trim();
82
+ changed = true;
83
+ }
84
+ else if (!w.pairOnly) {
85
+ if (cleaned.startsWith(w.open)) {
86
+ cleaned = cleaned.slice(w.open.length).trim();
87
+ changed = true;
88
+ }
89
+ else if (cleaned.endsWith(w.close)) {
90
+ cleaned = cleaned.slice(0, -w.close.length).trim();
91
+ changed = true;
92
+ }
93
+ }
94
+ }
95
+ }
96
+ return cleaned.length > 0 ? cleaned : null;
48
97
  }
49
98
  export const EDIT_TUPLE_HINT = "Edit must be called with exactly one payload. Use the canonical payload " +
50
99
  '{"path": path, "edits": [[remove_from, remove_to, replacement_text], ...]}: ' +
@@ -99,49 +148,49 @@ export function prepareEditArguments(args) {
99
148
  if (valid) {
100
149
  return { path: valid.path, edits: args.edits };
101
150
  }
102
- throw new Error(`[E_BAD_SHAPE] ${EDIT_TUPLE_HINT} ${describeReceived(args)}`);
151
+ throw new CodedError("E_BAD_PAYLOAD", `[MODEL] [E_BAD_PAYLOAD] ${EDIT_TUPLE_HINT} ${describeReceived(args)}`);
103
152
  }
104
153
  // ---- assertions ---------------------------------------------------------------
105
154
  export function assertEditRequest(request) {
106
155
  if (!isNormalizedEdit(request)) {
107
- throw new Error("[E_BAD_SHAPE] Edit request must be exactly { path, edits: [[remove_from, remove_to, replacement_text], ...] }.");
156
+ throw new CodedError("E_BAD_PAYLOAD", "[MODEL] [E_BAD_PAYLOAD] Edit request must be exactly { path, edits: [[remove_from, remove_to, replacement_text], ...] }.");
108
157
  }
109
158
  rejectUnknownFields(request, EDIT_KS, "Edit request");
110
159
  const req = request;
111
160
  if (req.path !== null && (typeof req.path !== "string" || req.path.length === 0)) {
112
- throw new Error('[E_BAD_SHAPE] Edit request path must be a non-empty string or null.');
161
+ throw new CodedError("E_BAD_PAYLOAD", '[MODEL] [E_BAD_PAYLOAD] Edit request path must be a non-empty string or null.');
113
162
  }
114
163
  if (!Array.isArray(req.edits) || req.edits.length === 0) {
115
- throw new Error('[E_BAD_SHAPE] Edit request requires a non-empty "edits" array.');
164
+ throw new CodedError("E_BAD_PAYLOAD", '[MODEL] [E_BAD_PAYLOAD] Edit request requires a non-empty "edits" array.');
116
165
  }
117
166
  if (req.edits.length > EDITS_MAX_ITEMS) {
118
- throw new Error(`[E_BAD_SHAPE] edit accepts at most ${EDITS_MAX_ITEMS} edits; got ${req.edits.length}. Split the batch.`);
167
+ throw new CodedError("E_BAD_PAYLOAD", `[MODEL] [E_BAD_PAYLOAD] edit accepts at most ${EDITS_MAX_ITEMS} edits; got ${req.edits.length}. Split the batch.`);
119
168
  }
120
169
  for (let index = 0; index < req.edits.length; index++) {
121
170
  const item = req.edits[index];
122
171
  if (typeof item.remove_from !== "string" || typeof item.remove_to !== "string" || typeof item.replacement_text !== "string") {
123
- throw new Error(`[E_BAD_SHAPE] Edit request edits[${index}] must be a three-position array [remove_from, remove_to, replacement_text].`);
172
+ throw new CodedError("E_BAD_PAYLOAD", `[MODEL] [E_BAD_PAYLOAD] Edit request edits[${index}] must be a three-position array [remove_from, remove_to, replacement_text].`);
124
173
  }
125
174
  }
126
175
  }
127
176
  // legacy — now always fails with new shape message (batch_edit removed)
128
177
  export function assertBatchEditRequest(_request) {
129
- throw new Error("[E_BAD_SHAPE] batch_edit has been removed. Use edit with { path, edits: [[remove_from, remove_to, replacement_text], ...] }.");
178
+ throw new CodedError("E_BAD_PAYLOAD", "[MODEL] [E_BAD_PAYLOAD] batch_edit has been removed. Use edit with { path, edits: [[remove_from, remove_to, replacement_text], ...] }.");
130
179
  }
131
180
  export function assertReadRequest(request) {
132
181
  if (!isRec(request))
133
- throw new Error("[E_BAD_SHAPE] Read request must be an object.");
182
+ throw new CodedError("E_BAD_PAYLOAD", "[MODEL] [E_BAD_PAYLOAD] Read request must be an object.");
134
183
  rejectUnknownFields(request, READ_KS, "Read request");
135
184
  if (typeof request.path !== "string" || request.path.length === 0) {
136
- throw new Error('[E_BAD_SHAPE] Read request requires a non-empty "path" string.');
185
+ throw new CodedError("E_BAD_PAYLOAD", '[MODEL] [E_BAD_PAYLOAD] Read request requires a non-empty "path" string.');
137
186
  }
138
187
  }
139
188
  export function assertUndoRequest(request) {
140
189
  if (!isRec(request))
141
- throw new Error("[E_BAD_SHAPE] undo_last_edit request must be an object.");
190
+ throw new CodedError("E_BAD_PAYLOAD", "[MODEL] [E_BAD_PAYLOAD] undo_last_edit request must be an object.");
142
191
  normalizeFilePath(request);
143
192
  if (typeof request.path !== "string" || request.path.length === 0) {
144
- throw new Error('[E_BAD_SHAPE] undo_last_edit request requires a non-empty "path" string.');
193
+ throw new CodedError("E_BAD_PAYLOAD", '[MODEL] [E_BAD_PAYLOAD] undo_last_edit request requires a non-empty "path" string.');
145
194
  }
146
195
  }
147
196
  // ---- shared JSON Schema literals (co-located with field sets) ---------------
@@ -23,8 +23,7 @@ export function buildMetrics(args) {
23
23
  return metrics;
24
24
  }
25
25
  export function finalizeResult(input) {
26
- const modelWarnings = input.warnings?.filter((w) => !w.startsWith("Batch drift note:"));
27
- const base = input.diff + warnBlock(modelWarnings);
26
+ const base = input.diff + warnBlock(input.warnings);
28
27
  return base;
29
28
  }
30
29
  export function finalizeToolResult(details) {
@@ -63,8 +62,7 @@ export function buildNoop(input) {
63
62
  const noopDetailsText = noopEdit
64
63
  ? `Edit for ${noopEdit.loc} is identical to current content:\n ${noopEdit.loc}: ${clipLine(noopEdit.currentContent)}`
65
64
  : "The edit produced identical content.";
66
- const modelWarnings = warnings?.filter((w) => !w.startsWith("Batch drift note:"));
67
- const text = `No changes made to ${path}\nClassification: noop\n${noopDetailsText}${warnBlock(modelWarnings)}`;
65
+ const text = `No changes made to ${path}\nClassification: noop\n${noopDetailsText}${warnBlock(warnings)}`;
68
66
  const metrics = buildMetrics({
69
67
  classification: "noop",
70
68
  editsAttempted: editMeta.editsAttempted,
@@ -90,8 +88,7 @@ export function buildChanged(input) {
90
88
  const diffResult = genDiff(originalNormalized, result, 1, resultHashes, originalHashes);
91
89
  const addedLines = editMeta.addedLines;
92
90
  const removedLines = editMeta.removedLines;
93
- const modelWarnings = warnings?.filter((w) => !w.startsWith("Batch drift note:"));
94
- const warningsBlock = warnBlock(modelWarnings);
91
+ const warningsBlock = warnBlock(warnings);
95
92
  const successPrefix = `Successfully edited in ${path}.`;
96
93
  const lineSummary = addedLines > 0 || removedLines > 0
97
94
  ? ` Added ${addedLines} line(s), removed ${removedLines} line(s).`
@@ -144,8 +141,7 @@ export function buildBatchResult(sections) {
144
141
  .filter((d) => d !== undefined)
145
142
  .join("\n\n");
146
143
  if (allNoop) {
147
- const modelWarnings = warnings.filter((w) => !w.startsWith("Batch drift note:"));
148
- const text = `No changes made. All ${totalEdits} edit(s) in the batch produced identical content.\nClassification: noop${warnBlock(modelWarnings)}`;
144
+ const text = `No changes made. All ${totalEdits} edit(s) in the batch produced identical content.\nClassification: noop${warnBlock(warnings)}`;
149
145
  return {
150
146
  content: [{ type: "text", text }],
151
147
  details: {
@@ -180,8 +176,7 @@ export function buildBatchResult(sections) {
180
176
  ? ` Added ${addedLines} line(s), removed ${removedLines} line(s).`
181
177
  : "";
182
178
  const summary = `Successfully edited ${appliedFiles.length} file(s) — ${appliedTotal} of ${totalEdits} edit(s) applied${noopTotal > 0 ? ` (${noopTotal} noop)` : ""}.${lineSummary}`;
183
- const modelWarnings = warnings.filter((w) => !w.startsWith("Batch drift note:"));
184
- const baseText = `${summary}${warnBlock(modelWarnings)}`;
179
+ const baseText = `${summary}${warnBlock(warnings)}`;
185
180
  const diffSection = formatDiffCard(diff);
186
181
  const text = `${baseText}${diffSection}`;
187
182
  return {
package/lib/encoding.js CHANGED
@@ -248,7 +248,7 @@ export async function detectWithChardet(bytes, allowlist) {
248
248
  }
249
249
  }
250
250
  /**
251
- * Chardet-based Top-3 for E_NOT_TEXT (primary) and autoGuess.
251
+ * Chardet-based Top-3 for E_UNSUPPORTED_FILE (primary) and autoGuess.
252
252
  * Returns chardet candidates filtered by allowlist and decodable, else empty.
253
253
  */
254
254
  export async function chardetTop3Candidates(bytes, allowlist) {
@@ -53,7 +53,7 @@ export function invalidateIfStale(targetKey, currentVersion) {
53
53
  // ---------------------------------------------------------------------------
54
54
  export function buildTop3ErrorMessage(displayPath, candidates) {
55
55
  const candStr = candidates.map((c) => `${c.encoding}("${c.sample.slice(0, 20).replace(/"/g, "'")}")`).join(", ");
56
- return `[E_NOT_TEXT] Path is not a readable UTF-8 text file: ${displayPath}. Hashline editing only supports text files. Top-3 guesses: ${candStr}. Try read({encoding: "<encoding>"}) or set DSH_BETTER_EDIT_AUTO_GUESS_ENCODING=true to auto-decode.`;
56
+ return `[MODEL] [E_UNSUPPORTED_FILE] Path is not a readable UTF-8 text file: ${displayPath}. Hashline editing only supports text files. Top-3 guesses: ${candStr}. Try read({encoding: "<encoding>"}) or set DSH_BETTER_EDIT_AUTO_GUESS_ENCODING=true to auto-decode.`;
57
57
  }
58
58
  function buildAutoGuessFooterFromCandidates(candidates) {
59
59
  // chardet path vs heuristic path share same footer shape, just score vs confidence
@@ -76,7 +76,7 @@ function isMidConfidenceHeuristic(best, second) {
76
76
  /**
77
77
  * Deterministic admission for a raw byte buffer. No IO, no side effects
78
78
  * except Top-3 scoring. Caller owns recording the returned state via
79
- * `recordOpenState` and handling the footer vs E_NOT_TEXT split.
79
+ * `recordOpenState` and handling the footer vs E_UNSUPPORTED_FILE split.
80
80
  *
81
81
  * Order is fixed: BOM sniff always precedes strict UTF-8, which always
82
82
  * precedes guessing. Probabilistic encodings are last resort, gated by
@@ -196,9 +196,9 @@ export async function decodeForOpen(bytes, config, opts = {}) {
196
196
  };
197
197
  }
198
198
  }
199
- // No candidate succeeded — fall through to E_NOT_TEXT with Top-3
199
+ // No candidate succeeded — fall through to E_UNSUPPORTED_FILE with Top-3
200
200
  }
201
- // 5) E_NOT_TEXT + Top-3 always pushed (even when autoGuess off)
201
+ // 5) E_UNSUPPORTED_FILE + Top-3 always pushed (even when autoGuess off)
202
202
  // Use chardet Top-3 when available, else heuristic Top-3 — same as fs-bridge fallback
203
203
  let candidates = [];
204
204
  try {
package/lib/file-view.js CHANGED
@@ -21,7 +21,7 @@ import { constants } from "node:fs";
21
21
  import { open as fsOpen, stat as fsStat } from "fs/promises";
22
22
  import { access as fsAccess } from "fs/promises";
23
23
  import { fileTypeFromBuffer } from "file-type";
24
- import { SNIFF_BYTES, MAX_BYTES, MAX_READ_LINE_BYTES } from "./constants.js";
24
+ import { SNIFF_BYTES, MAX_BYTES, MAX_READ_LINE_BYTES, eLargeFileMsg } from "./constants.js";
25
25
  import { lineHashes, fmtRegion, HASH_SEP } from "./hashline/index.js";
26
26
  import { HASH_SPACE } from "./hashline/hash-assign.js";
27
27
  import { visLines, abortIf, errCode } from "./utils.js";
@@ -206,7 +206,7 @@ export async function loadFileKindAndText(filePath, options) {
206
206
  newlineCount++;
207
207
  }
208
208
  if (newlineCount > options.maxLines) {
209
- throw new Error(`[E_FILE_TOO_LARGE] ${options.displayPath ?? filePath} has more than ${options.maxLines} lines, exceeding the ${options.maxLines}-line edit limit. Hashline editing targets source-sized files; for very large files use write or a non-line-based approach.`);
209
+ throw new Error(eLargeFileMsg(options.displayPath ?? filePath, newlineCount, options.maxLines));
210
210
  }
211
211
  }
212
212
  return decoded;
@@ -241,27 +241,27 @@ export async function valAccess(absolutePath, path, accessMode = constants.R_OK)
241
241
  catch (error) {
242
242
  const code = errCode(error);
243
243
  if (code === "ENOENT") {
244
- throw new Error(`[E_NOT_FOUND] File not found: ${path}`);
244
+ throw new Error(`[MODEL] [E_NOT_FOUND] File not found: ${path}`);
245
245
  }
246
246
  if (code === "EACCES" || code === "EPERM") {
247
247
  const accessLabel = accessMode & constants.W_OK ? "not writable" : "not readable";
248
- throw new Error(`[E_ACCESS] File is ${accessLabel}: ${path}`);
248
+ throw new Error(`[MODEL] [E_ACCESS] File is ${accessLabel}: ${path}`);
249
249
  }
250
250
  if (code === "ELOOP") {
251
- throw new Error(`[E_ACCESS] Too many symbolic links while resolving: ${path}`);
251
+ throw new Error(`[MODEL] [E_ACCESS] Too many symbolic links while resolving: ${path}`);
252
252
  }
253
- throw new Error(`[E_ACCESS] Cannot access file: ${path}`);
253
+ throw new Error(`[MODEL] [E_ACCESS] Cannot access file: ${path}`);
254
254
  }
255
255
  }
256
256
  export function valKind(file, path) {
257
257
  if (file.kind === "directory") {
258
- throw new Error(`[E_NOT_TEXT] Path is a directory: ${path}. Use ls to inspect directories.`);
258
+ throw new Error(`[MODEL] [E_UNSUPPORTED_FILE] Path is a directory: ${path}.`);
259
259
  }
260
260
  if (file.kind === "binary") {
261
- throw new Error(`[E_NOT_TEXT] Path is a binary file: ${path} (${file.description}). Hashline edit only supports text files.`);
261
+ throw new Error(`[MODEL] [E_UNSUPPORTED_FILE] Path is a binary file: ${path} (${file.description}). Hashline edit only supports text files.`);
262
262
  }
263
263
  if (file.kind === "image") {
264
- throw new Error(`[E_NOT_TEXT] Path is an image file: ${path}. Hashline edit only supports text files.`);
264
+ throw new Error(`[MODEL] [E_UNSUPPORTED_FILE] Path is an image file: ${path}. Hashline edit only supports text files.`);
265
265
  }
266
266
  }
267
267
  function fmtSnapId(canonicalPath, info) {
@@ -287,10 +287,10 @@ export async function normFromText(input) {
287
287
  if (input.maxLines !== undefined) {
288
288
  const lineCount = visLines(normalized).length;
289
289
  if (lineCount > input.maxLines) {
290
- throw new Error(`[E_FILE_TOO_LARGE] ${displayPath} has ${lineCount} lines, exceeding the ${input.maxLines}-line edit limit. Hashline editing targets source-sized files; for very large files use write or a non-line-based approach.`);
290
+ throw new Error(eLargeFileMsg(displayPath, lineCount, input.maxLines));
291
291
  }
292
292
  }
293
- const fileHashes = await lineHashes(normalized, absolutePath, undefined, input.store, input.noPersist !== true, input.reservedHashes, input.retiredHashes);
293
+ const fileHashes = await lineHashes(normalized, absolutePath, input.previous, input.store, input.noPersist !== true, input.reservedHashes, input.retiredHashes);
294
294
  return {
295
295
  absolutePath,
296
296
  normalized,
@@ -325,6 +325,7 @@ export async function readNormFile(path, cwd, options) {
325
325
  reservedHashes: options?.reservedHashes,
326
326
  retiredHashes: options?.retiredHashes,
327
327
  hadUtf8DecodeErrors: file.hadUtf8DecodeErrors,
328
+ previous: options?.previous,
328
329
  });
329
330
  }
330
331
  // --- Read render (from read-render.ts, private) ---
@@ -332,7 +333,7 @@ function normPosInt(value, name) {
332
333
  if (value === undefined)
333
334
  return undefined;
334
335
  if (!Number.isInteger(value) || value < 1) {
335
- throw new Error(`[E_BAD_SHAPE] Read request field "${name}" must be a positive integer.`);
336
+ throw new Error(`[MODEL] [E_BAD_PAYLOAD] Read request field "${name}" must be a positive integer.`);
336
337
  }
337
338
  return value;
338
339
  }
@@ -473,6 +474,7 @@ export async function readView(io, path, cwd, opts = {}) {
473
474
  maxLines: MAX_HASH_LINES,
474
475
  reservedHashes: opts.reservedHashes,
475
476
  retiredHashes: opts.retiredHashes,
477
+ previous: opts.previous,
476
478
  });
477
479
  const r = await fmtReadPreview(normalized, { offset: opts.offset, limit: opts.limit }, fileHashes, absolutePath);
478
480
  return {
package/lib/fs-bridge.js CHANGED
@@ -20,6 +20,7 @@ import { loadConfig } from "./store-config.js";
20
20
  import { fileSnap } from "./file-reader.js";
21
21
  import { resolveTarget, toCwd } from "./paths.js";
22
22
  import { decodeForOpen, recordOpenState, recordFooter, getEncodingState as _getEncodingState, setEncodingState as _setEncodingState, clearEncodingState as _clearEncodingState, getAutoGuessFooter as _getAutoGuessFooter, setAutoGuessFooter as _setAutoGuessFooter, clearAutoGuessFooter as _clearAutoGuessFooter, invalidateIfStale, prepareForSave, } from "./file-encoding-state.js";
23
+ import { codeOf } from "./utils.js";
23
24
  export { _getEncodingState as getEncodingState, _setEncodingState as setEncodingState, _clearEncodingState as clearEncodingState, _getAutoGuessFooter as getAutoGuessFooter, _setAutoGuessFooter as setAutoGuessFooter, _clearAutoGuessFooter as clearAutoGuessFooter, };
24
25
  // Keep alias for tests that import getAutoGuessFooter via fs-bridge
25
26
  export function getAutoGuessFooterCompat(k) {
@@ -37,13 +38,13 @@ export function mapFsError(error, displayPath) {
37
38
  typeof error.code === "string") {
38
39
  const code = error.code;
39
40
  if (code === "FS_NOT_FOUND") {
40
- throw new Error(`[E_NOT_FOUND] File not found: ${displayPath}`);
41
+ throw new Error(`[MODEL] [E_NOT_FOUND] File not found: ${displayPath}`);
41
42
  }
42
43
  if (code === "FS_PERMISSION_DENIED") {
43
- throw new Error(`[E_ACCESS] Cannot access file: ${displayPath}`);
44
+ throw new Error(`[MODEL] [E_ACCESS] Cannot access file: ${displayPath}`);
44
45
  }
45
46
  if (code === "FS_NOT_TEXT" || code === "FS_NOT_REGULAR_FILE") {
46
- throw new Error(`[E_NOT_TEXT] Path is not a readable UTF-8 text file: ${displayPath}. Hashline editing only supports text files. Try read({encoding: "gbk"}) or enable autoGuessEncoding.`);
47
+ throw new Error(`[MODEL] [E_UNSUPPORTED_FILE] Path is not a readable UTF-8 text file: ${displayPath}. Hashline editing only supports text files. Try read({encoding: "gbk"}) or enable autoGuessEncoding.`);
47
48
  }
48
49
  if (code === "FS_BAD_ENCODING") {
49
50
  throw new Error(`[E_BAD_ENCODING] Unknown encoding for ${displayPath}. Supported: utf8, gbk, big5, shift_jis, euc-kr, windows-1251, iso-8859-1`);
@@ -52,7 +53,7 @@ export function mapFsError(error, displayPath) {
52
53
  throw new Error(`[E_DECODE_FAILED] Bytes in ${displayPath} cannot be decoded with requested encoding.`);
53
54
  }
54
55
  if (code === "FS_STALE_VERSION") {
55
- throw new Error(`[E_RANGE_STALE] The file changed on disk since it was read (version guard rejected the write). Call read() to get fresh anchors, then retry.`);
56
+ throw new Error(`[MODEL] [E_STALE_RANGE] The file changed on disk since it was read (version guard rejected the write). Call read() to get fresh anchors, then retry.`);
56
57
  }
57
58
  if (code === "FS_NOT_OBSERVED") {
58
59
  throw new Error(`[E_NOT_OBSERVED] The file has not been observed in this session (read-before-write policy). Call read() first, then retry the edit.`);
@@ -254,7 +255,7 @@ export function localIO() {
254
255
  recordFooter(absolutePath, decoded.footer);
255
256
  return decoded.text;
256
257
  }
257
- // Deterministic path via seam, with local fallback for E_NOT_TEXT when autoGuess off
258
+ // Deterministic path via seam, with local fallback for E_UNSUPPORTED_FILE when autoGuess off
258
259
  try {
259
260
  const bytes = await readFile(absolutePath);
260
261
  const cfgL = loadConfig();
@@ -266,8 +267,8 @@ export function localIO() {
266
267
  return decoded.text;
267
268
  }
268
269
  catch (error) {
269
- // Local fallback: when autoGuess off, E_NOT_TEXT should fall back to raw UTF-8 with � (no throw)
270
- if (error instanceof Error && error.message.includes("[E_NOT_TEXT]")) {
270
+ // Local fallback: when autoGuess off, E_UNSUPPORTED_FILE should fall back to raw UTF-8 with � (no throw)
271
+ if (codeOf(error) === "E_UNSUPPORTED_FILE") {
271
272
  return readFile(absolutePath, "utf-8");
272
273
  }
273
274
  throw error;
package/lib/hash-store.js CHANGED
@@ -66,6 +66,58 @@ export function isValidServedList(value) {
66
66
  function cacheKey(checksum) {
67
67
  return `${CANON_VERSION}:${checksum}`;
68
68
  }
69
+ export function isValidRetiredEntry(value) {
70
+ if (typeof value !== "object" || value === null)
71
+ return false;
72
+ const v = value;
73
+ return typeof v.hash === "string" && (v.deathPos === null || typeof v.deathPos === "number") && HASH_RE.test(v.hash);
74
+ }
75
+ export function isValidRetiredEntries(value) {
76
+ if (!Array.isArray(value))
77
+ return false;
78
+ for (const e of value)
79
+ if (!isValidRetiredEntry(e))
80
+ return false;
81
+ return true;
82
+ }
83
+ function coerceRetiredEntry(e) {
84
+ if (typeof e === "string" && HASH_RE.test(e))
85
+ return { hash: e, deathPos: null };
86
+ if (typeof e !== "object" || e === null || !("hash" in e))
87
+ return null;
88
+ const ee = e;
89
+ if (typeof ee.hash !== "string" || !HASH_RE.test(ee.hash))
90
+ return null;
91
+ const rawPos = ee.deathPos;
92
+ const deathPos = typeof rawPos === "number" && rawPos !== -1 ? rawPos : null;
93
+ return { hash: ee.hash, deathPos };
94
+ }
95
+ function parseRetiredJson(raw) {
96
+ if (raw === null || raw === undefined || raw === "")
97
+ return [];
98
+ try {
99
+ const parsed = JSON.parse(raw);
100
+ if (!Array.isArray(parsed) || parsed.length === 0)
101
+ return [];
102
+ if (isValidRetiredEntries(parsed)) {
103
+ return parsed.map((e) => (e.deathPos === -1 ? { hash: e.hash, deathPos: null } : e));
104
+ }
105
+ const out = [];
106
+ for (const e of parsed) {
107
+ const coerced = coerceRetiredEntry(e);
108
+ if (coerced)
109
+ out.push(coerced);
110
+ }
111
+ if (out.length > 0)
112
+ return out;
113
+ if (isValidHashList(parsed))
114
+ return parsed.map((h) => ({ hash: h, deathPos: null }));
115
+ return [];
116
+ }
117
+ catch {
118
+ return [];
119
+ }
120
+ }
69
121
  /** Load the store as served persistence — internal, for SessionView only. */
70
122
  // SAFETY: loadHashStore returns InternalHashStore (HashStore & ServedPersistence) — cast narrows to served view, validated via ServedPersistence interface; safe because InternalHashStore extends both.
71
123
  export function loadServedStore(cwd) {
@@ -182,6 +234,7 @@ function buildStore(db) {
182
234
  "retired TEXT, " +
183
235
  "canons TEXT, " +
184
236
  "snapshotId TEXT, " +
237
+ "cards TEXT, " +
185
238
  "updated_at INTEGER NOT NULL, " +
186
239
  "PRIMARY KEY (session_id, path)" +
187
240
  ")");
@@ -225,6 +278,10 @@ function buildStore(db) {
225
278
  if (!snapshotColumns.some((column) => column.name === "snapshotId")) {
226
279
  db.exec("ALTER TABLE served ADD COLUMN snapshotId TEXT");
227
280
  }
281
+ const cardsColumns = db.prepare("PRAGMA table_info(served)").all();
282
+ if (!cardsColumns.some((column) => column.name === "cards")) {
283
+ db.exec("ALTER TABLE served ADD COLUMN cards TEXT");
284
+ }
228
285
  db
229
286
  .prepare("INSERT INTO meta (key, value) VALUES ('version', ?) " +
230
287
  "ON CONFLICT(key) DO UPDATE SET value = excluded.value")
@@ -240,7 +297,7 @@ function buildStore(db) {
240
297
  const undoGetStmt = db.prepare("SELECT content, bom, ending, hashes, result_content FROM undo WHERE path = ?");
241
298
  const undoDelStmt = db.prepare("DELETE FROM undo WHERE path = ?");
242
299
  const undoPruneOlderThanStmt = db.prepare("DELETE FROM undo WHERE updated_at < ?");
243
- const servedGetStmt = db.prepare("SELECT hashes, reported, retired, canons, snapshotId FROM served WHERE session_id = ? AND path = ?");
300
+ const servedGetStmt = db.prepare("SELECT hashes, reported, retired, canons, snapshotId, cards FROM served WHERE session_id = ? AND path = ?");
244
301
  const servedAllForPathStmt = db.prepare("SELECT session_id, hashes, retired FROM served WHERE path = ?");
245
302
  const servedUpsertStmt = db.prepare("INSERT INTO served (session_id, path, hashes, updated_at) VALUES (?, ?, ?, ?) " +
246
303
  "ON CONFLICT(session_id, path) DO UPDATE SET hashes = excluded.hashes, updated_at = excluded.updated_at");
@@ -256,6 +313,9 @@ function buildStore(db) {
256
313
  const servedSnapshotUpsertStmt = db.prepare("INSERT INTO served (session_id, path, hashes, snapshotId, updated_at) VALUES (?, ?, '[]', ?, ?) " +
257
314
  "ON CONFLICT(session_id, path) DO UPDATE SET snapshotId = excluded.snapshotId, updated_at = excluded.updated_at");
258
315
  const servedSnapshotClearStmt = db.prepare("UPDATE served SET snapshotId = NULL, updated_at = ? WHERE session_id = ? AND path = ?");
316
+ const servedCardsUpsertStmt = db.prepare("INSERT INTO served (session_id, path, hashes, cards, updated_at) VALUES (?, ?, '[]', ?, ?) " +
317
+ "ON CONFLICT(session_id, path) DO UPDATE SET cards = excluded.cards, updated_at = excluded.updated_at");
318
+ const servedCardsClearStmt = db.prepare("UPDATE served SET cards = NULL, updated_at = ? WHERE session_id = ? AND path = ?");
259
319
  const servedDeleteStmt = db.prepare("DELETE FROM served WHERE session_id = ? AND path = ?");
260
320
  const servedDeletePathStmt = db.prepare("DELETE FROM served WHERE path = ?");
261
321
  const servedWipeStmt = db.prepare("DELETE FROM served WHERE session_id = ?");
@@ -337,6 +397,16 @@ function buildStore(db) {
337
397
  servedSnapshotClearStmt.run(params[1], params[0], params[2]);
338
398
  });
339
399
  },
400
+ servedCardsUpsert: (...params) => {
401
+ withBusyRetry(() => {
402
+ servedCardsUpsertStmt.run(...params);
403
+ });
404
+ },
405
+ servedCardsClear: (...params) => {
406
+ withBusyRetry(() => {
407
+ servedCardsClearStmt.run(params[1], params[0], params[2]);
408
+ });
409
+ },
340
410
  servedDelete: (...params) => {
341
411
  withBusyRetry(() => {
342
412
  servedDeleteStmt.run(...params);
@@ -475,31 +545,30 @@ function makeDomainStore(stmts) {
475
545
  return new Set();
476
546
  }
477
547
  },
478
- getAnchorReservations(path) {
548
+ getAnchorReservations(sessionKey, path) {
479
549
  const reservedHashes = new Set();
480
550
  const retiredHashes = new Set();
481
- for (const row of stmts.servedAllForPath(path)) {
482
- try {
483
- const served = JSON.parse(row.hashes);
484
- const retired = row.retired === null || row.retired === undefined
485
- ? []
486
- : JSON.parse(row.retired);
487
- if (!isValidServedList(served) || !isValidHashList(retired)) {
488
- throw new TypeError("invalid stored anchor reservations");
489
- }
490
- for (const hash of served) {
491
- if (hash !== null)
492
- reservedHashes.add(hash);
493
- }
494
- for (const hash of retired) {
551
+ const row = stmts.servedGet(sessionKey, path);
552
+ if (!row)
553
+ return { reservedHashes, retiredHashes };
554
+ try {
555
+ const served = JSON.parse(row.hashes);
556
+ if (!isValidServedList(served)) {
557
+ throw new TypeError("invalid stored anchor reservations");
558
+ }
559
+ for (const hash of served) {
560
+ if (hash !== null)
495
561
  reservedHashes.add(hash);
496
- retiredHashes.add(hash);
497
- }
498
562
  }
499
- catch (error) {
500
- stmts.servedDelete(row.session_id, path);
563
+ const retiredEntries = parseRetiredJson(row.retired);
564
+ for (const e of retiredEntries) {
565
+ reservedHashes.add(e.hash);
566
+ retiredHashes.add(e.hash);
501
567
  }
502
568
  }
569
+ catch (error) {
570
+ stmts.servedDelete(sessionKey, path);
571
+ }
503
572
  return { reservedHashes, retiredHashes };
504
573
  },
505
574
  getRetiredAnchors(sessionKey, path) {
@@ -508,17 +577,26 @@ function makeDomainStore(stmts) {
508
577
  return new Set();
509
578
  }
510
579
  try {
511
- const parsed = JSON.parse(row.retired);
512
- if (!isValidHashList(parsed)) {
513
- throw new TypeError("invalid retired anchors");
514
- }
515
- return new Set(parsed);
580
+ const entries = parseRetiredJson(row.retired);
581
+ return new Set(entries.map((e) => e.hash));
516
582
  }
517
583
  catch (error) {
518
584
  stmts.servedDelete(sessionKey, path);
519
585
  return new Set();
520
586
  }
521
587
  },
588
+ getRetiredEntries(sessionKey, path) {
589
+ const row = stmts.servedGet(sessionKey, path);
590
+ if (!row || row.retired === null || row.retired === undefined)
591
+ return [];
592
+ try {
593
+ return parseRetiredJson(row.retired);
594
+ }
595
+ catch {
596
+ stmts.servedDelete(sessionKey, path);
597
+ return [];
598
+ }
599
+ },
522
600
  upsertServed(sessionKey, path, hashesJson) {
523
601
  stmts.servedUpsert(sessionKey, path, hashesJson, Date.now());
524
602
  },
@@ -567,6 +645,26 @@ function makeDomainStore(stmts) {
567
645
  clearEpochSnapshotId(sessionKey, path) {
568
646
  stmts.servedSnapshotClear(sessionKey, Date.now(), path);
569
647
  },
648
+ getCards(sessionKey, path) {
649
+ const row = stmts.servedGet(sessionKey, path);
650
+ if (!row || row.cards === null || row.cards === undefined)
651
+ return new Set();
652
+ try {
653
+ const parsed = JSON.parse(row.cards);
654
+ if (!Array.isArray(parsed))
655
+ return new Set();
656
+ return new Set(parsed.filter((v) => typeof v === "number" && Number.isInteger(v) && v >= 0));
657
+ }
658
+ catch {
659
+ return new Set();
660
+ }
661
+ },
662
+ upsertCards(sessionKey, path, cardsJson) {
663
+ stmts.servedCardsUpsert(sessionKey, path, cardsJson, Date.now());
664
+ },
665
+ clearCards(sessionKey, path) {
666
+ stmts.servedCardsClear(sessionKey, Date.now(), path);
667
+ },
570
668
  deleteServed(sessionKey, path) {
571
669
  stmts.servedDelete(sessionKey, path);
572
670
  },