dsh-better-edit 1.2.4 → 1.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/lib/domain-errors.js +11 -9
- package/lib/edit-mode.js +11 -0
- package/lib/encoding.js +2 -0
- package/lib/file-encoding-state.js +42 -25
- package/lib/file-view.js +26 -205
- package/lib/fs-bridge.js +14 -49
- package/lib/hashline/anchor-pipeline.js +8 -499
- package/lib/hashline/hash-assign.js +55 -4
- package/lib/hashline/index.js +5 -2
- package/lib/hashline/served-evidence.js +531 -0
- package/lib/index.js +176 -47
- package/lib/mutation/engine.js +10 -13
- package/lib/mutation.js +36 -100
- package/lib/noop-guard.js +24 -39
- package/lib/paths.js +9 -5
- package/lib/read-and-serve.js +51 -53
- package/lib/reclaim.js +65 -0
- package/lib/self-heal.js +6 -10
- package/lib/session-view.js +61 -21
- package/lib/store/index.js +14 -14
- package/lib/store-config.js +4 -5
- package/lib/store-lifecycle.js +1 -1
- package/lib/tool-edit.js +0 -3
- package/lib/tool-read.js +2 -8
- package/lib/tool-str-replace-editor.js +3 -10
- package/lib/tool-undo.js +5 -13
- package/lib/types/contract.d.ts +2 -1
- package/lib/types/domain-errors.d.ts +8 -9
- package/lib/types/edit-mode.d.ts +11 -0
- package/lib/types/encoding.d.ts +1 -0
- package/lib/types/file-encoding-state.d.ts +10 -10
- package/lib/types/file-view.d.ts +18 -56
- package/lib/types/hashline/anchor-pipeline.d.ts +6 -128
- package/lib/types/hashline/hash-assign.d.ts +40 -0
- package/lib/types/hashline/index.d.ts +6 -4
- package/lib/types/hashline/served-evidence.d.ts +152 -0
- package/lib/types/mutation/engine.d.ts +4 -2
- package/lib/types/mutation.d.ts +9 -57
- package/lib/types/noop-guard.d.ts +2 -33
- package/lib/types/paths.d.ts +0 -1
- package/lib/types/reclaim.d.ts +36 -0
- package/lib/types/session-view.d.ts +28 -8
- package/lib/types/store/index.d.ts +12 -11
- package/lib/types/store-config.d.ts +1 -1
- package/lib/types/store-lifecycle.d.ts +12 -2
- package/lib/types/tool-edit.d.ts +0 -2
- package/lib/types/tool-read.d.ts +0 -5
- package/lib/types/tool-str-replace-editor.d.ts +0 -6
- package/lib/types/tool-undo.d.ts +0 -5
- package/package.json +3 -2
- package/lib/drift.js +0 -5
- package/lib/dsh-context.js +0 -5
- package/lib/edit-engine.js +0 -7
- package/lib/edit-normalize.js +0 -9
- package/lib/edit-pipeline.js +0 -6
- package/lib/file-kind.js +0 -6
- package/lib/file-reader.js +0 -6
- package/lib/guidance/index.js +0 -8
- package/lib/hashline/alphabet.js +0 -2
- package/lib/hashline/apply.js +0 -2
- package/lib/hashline/hasher.js +0 -2
- package/lib/hashline/parse.js +0 -2
- package/lib/hashline/pure.js +0 -2
- package/lib/hashline/resolve.js +0 -3
- package/lib/hashline/served.js +0 -2
- package/lib/preview-controller.js +0 -65
- package/lib/read-render.js +0 -6
- package/lib/schema.js +0 -24
- package/lib/served-store.js +0 -5
- package/lib/tool-batch-edit.js +0 -190
- package/lib/truncate.js +0 -6
- package/lib/types/drift.d.ts +0 -5
- package/lib/types/dsh-context.d.ts +0 -5
- package/lib/types/edit-engine.d.ts +0 -7
- package/lib/types/edit-normalize.d.ts +0 -1
- package/lib/types/edit-pipeline.d.ts +0 -6
- package/lib/types/file-kind.d.ts +0 -6
- package/lib/types/file-reader.d.ts +0 -6
- package/lib/types/guidance/index.d.ts +0 -8
- package/lib/types/hashline/alphabet.d.ts +0 -1
- package/lib/types/hashline/apply.d.ts +0 -1
- package/lib/types/hashline/hasher.d.ts +0 -1
- package/lib/types/hashline/parse.d.ts +0 -1
- package/lib/types/hashline/pure.d.ts +0 -1
- package/lib/types/hashline/resolve.d.ts +0 -1
- package/lib/types/hashline/served.d.ts +0 -1
- package/lib/types/preview-controller.d.ts +0 -33
- package/lib/types/read-render.d.ts +0 -6
- package/lib/types/schema.d.ts +0 -25
- package/lib/types/served-store.d.ts +0 -5
- package/lib/types/tool-batch-edit.d.ts +0 -25
- package/lib/types/truncate.d.ts +0 -6
- package/lib/types/validation.d.ts +0 -6
- package/lib/types/workspace.d.ts +0 -5
- package/lib/validation.js +0 -6
- package/lib/workspace.js +0 -5
package/CHANGELOG.md
CHANGED
|
@@ -11,6 +11,25 @@ All notable changes to this project will be documented in this file.
|
|
|
11
11
|
|
|
12
12
|
## [Unreleased]
|
|
13
13
|
|
|
14
|
+
## [1.2.5] - 2026-11-18
|
|
15
|
+
|
|
16
|
+
### Bug Fixes
|
|
17
|
+
|
|
18
|
+
* **noop-guard:** scope the noop-loop counter per `(session, file)` so one session's repeated no-op can no longer reject another session's first submission of the same edit, and delete the dead `runNoopPolicy`/`runNoopPolicySync` copy of the live decision (ADR-0039). (#133)
|
|
19
|
+
* **encoding:** give each file one memo identity — the encoding and auto-guess footer memos are keyed by the slash-normalized absolute path, so the dual-key writes and their three-spelling probe ladders are gone (ADR-0038). (#133)
|
|
20
|
+
* **install:** claim tools synchronously in `agent/created` turn, isolate per-step failures, and reclaim contested entries on collision (#123) (#131)
|
|
21
|
+
* **mutation:** restore the undo row of every file a failed write never wrote, so a multi-item edit's failure no longer leaves `undo_last_edit` reporting `E_UNDO_STALE "modified"` for the tool's own failed write (ADR-0034). (#133)
|
|
22
|
+
* **read:** stop a paged read that happens to cover every line from clearing retired anchors, hazard cards and the served canon map — the full-read verdict is FileView's alone, and the lease grant stays unconditional because a shown anchor must remain editable (ADR-0035). (#133)
|
|
23
|
+
* **retired-anchors:** append a retirement as an object entry instead of rewriting the column into bare strings, so an existing retired anchor keeps its death position and the incremental hazard sweep can free it again rather than accumulating until a full read (ADR-0036). (#133)
|
|
24
|
+
|
|
25
|
+
### Code Refactoring
|
|
26
|
+
|
|
27
|
+
* **hashline:** retire the six `hashline` shims (`alphabet`, `apply`, `hasher`, `parse`, `pure`, `resolve`) with every test importer re-pointed at the owning module and the `parse` barrel edge moved to `anchor-pipeline`; the now-dead `noRestrictedImports` guard and its two disablers go, and the test-only `resolveDisplayPath`/`snapshotIdFor` mutation exports are deleted (ADR-0033 amendment). (#133)
|
|
28
|
+
* **dead-code:** retire the duplicate-name shims and the dead FileView sub-graph — `edit-engine`, `preview-controller`, the five `file-view` re-export shims, and the `mutation.ts` deprecated re-export block go, and `SUPPORTED_ENCODINGS` becomes the one encoding roster (ADR-0033). (#133)
|
|
29
|
+
* **dead-code:** retire the four `register*Tool` bypasses, the stray `tests/` tree (unique cases ported into `test/`), the duplicate guidance barrel and the `paths.ts → store/index.ts` barrel edge; `pnpm run build` now cleans `lib/` first, so a stale module cannot ride the output. (#133)
|
|
30
|
+
* **hashline:** extract the served-evidence verdict into `served-evidence.ts` — one entry point (`verifyServedRange`), one row renderer behind `fmtServedRows`/`fmtRegion`, one `[... N more — …]` frame — and cut both import cycles the mixed seam held: `store-lifecycle` types its store face structurally and `EditMode` moves to a leaf, leaving only `file-view ↔ fs-bridge` (ADR-0037). (#133)
|
|
31
|
+
* **hashline:** split the anchor-space-exhaustion test into two named predicates in the error's own module — `isAnchorSpaceExhaustion` types the failure, `isPromotableAnchorSpaceExhaustion` decides whether clearing retired anchors could cure it — so the uncurable line-cap refusal rethrows before the read path touches hazard state; the windowed `served` record stops de-masking duplicate positions, the private `hashline/served` shim retires, and the stale `anchor-pipeline` prose cites re-point at `served-evidence`. (#133)
|
|
32
|
+
|
|
14
33
|
## [1.2.4] - 2026-11-17
|
|
15
34
|
|
|
16
35
|
### Tests
|
package/lib/domain-errors.js
CHANGED
|
@@ -218,16 +218,14 @@ function batchAbortFormat(payload) {
|
|
|
218
218
|
function noopLoopFormat(payload) {
|
|
219
219
|
// WHY batch first: the batch ref already names the item (edits[i] (path)),
|
|
220
220
|
// so the batch arm renders from ref and appends the on-disk range when the
|
|
221
|
-
// engine supplies one
|
|
221
|
+
// engine supplies one. `enforceNoopLoop` is the only producer and always
|
|
222
|
+
// passes `batch: true`; the generic arm below keeps the message defined for
|
|
223
|
+
// a non-batch payload the type still admits.
|
|
222
224
|
if (payload.batch) {
|
|
223
225
|
return (`${payload.ref}: identical edit (${payload.anchorFrom} → ${payload.anchorTo}) submitted ${payload.count}×, no changes each time. ` +
|
|
224
226
|
`Range already contains this text; resend will reject the batch.` +
|
|
225
227
|
(payload.servedBlock ? `\nCurrent on-disk range:\n${payload.servedBlock}` : ""));
|
|
226
228
|
}
|
|
227
|
-
if (payload.path !== undefined) {
|
|
228
|
-
return (`identical edit (${payload.anchorFrom} → ${payload.anchorTo} in ${payload.path}) submitted ${payload.count}×, no changes each time. ` +
|
|
229
|
-
`Range already contains this text; resend will reject. Current range:\n${payload.servedBlock}`);
|
|
230
|
-
}
|
|
231
229
|
return (`identical edit (${payload.anchorFrom} → ${payload.anchorTo} ${payload.ref}) submitted ${payload.count}×, no changes each time. ` +
|
|
232
230
|
`Range already contains this text; resend will reject.`);
|
|
233
231
|
}
|
|
@@ -543,10 +541,14 @@ export const DECLARATION_ONLY_FIELDS = {
|
|
|
543
541
|
*
|
|
544
542
|
* | ported upstream diagnostic | upstream producer (pi-better-edit@b92e0ec) | local producer |
|
|
545
543
|
* |---|---|---|
|
|
546
|
-
* | `E_FOREIGN_ANCHOR` | `lease-resolve.ts:116` | `
|
|
547
|
-
* | `E_UNKNOWN_ANCHOR` | `lease-resolve.ts:118`, `served-verification.ts:745` | `
|
|
548
|
-
* | `E_UNVERIFIED_RANGE` | `lease-resolve.ts:186`, `served-verification.ts:187` | `
|
|
549
|
-
* | `E_TARGET_LOST` | `served-verification.ts:170`, `lease-resolve.ts:196` | `
|
|
544
|
+
* | `E_FOREIGN_ANCHOR` | `lease-resolve.ts:116` | `served-evidence.ts` (`throwUnknownOrForeign`, boundary placement) |
|
|
545
|
+
* | `E_UNKNOWN_ANCHOR` | `lease-resolve.ts:118`, `served-verification.ts:745` | `served-evidence.ts` (boundary placement, pre-gate) |
|
|
546
|
+
* | `E_UNVERIFIED_RANGE` | `lease-resolve.ts:186`, `served-verification.ts:187` | `served-evidence.ts` (pre-gate stale-boundary arm) |
|
|
547
|
+
* | `E_TARGET_LOST` | `served-verification.ts:170`, `lease-resolve.ts:196` | `served-evidence.ts` (pre-gate stale-boundary arm) |
|
|
548
|
+
*
|
|
549
|
+
* The upstream column is scoped to the upstream tree (`pi-better-edit@b92e0ec`): its
|
|
550
|
+
* `served-verification.ts` never existed downstream, so those cites never named a local
|
|
551
|
+
* file. The local column did move — the four arms live in `served-evidence.ts` (ADR-0037).
|
|
550
552
|
*
|
|
551
553
|
* Their owner is `"upstream absorb"` and their trigger is the absorb that
|
|
552
554
|
* ports the corresponding producer. When that happens, reintroduce the code
|
package/lib/edit-mode.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EditMode — the one member of the request contract the write pipeline names.
|
|
3
|
+
*
|
|
4
|
+
* WHY a module of its own (ADR-0037): `EditMode` is the only edge from
|
|
5
|
+
* anchor-pipeline back into contract.ts, and contract.ts reaches the read side
|
|
6
|
+
* through `ReadWindow`. Left there, the type re-closed the import ring; as a leaf
|
|
7
|
+
* it closes nothing — both ends import this file and never each other.
|
|
8
|
+
*
|
|
9
|
+
* @module dsh-better-edit/edit-mode
|
|
10
|
+
*/
|
|
11
|
+
export {};
|
package/lib/encoding.js
CHANGED
|
@@ -12,6 +12,8 @@ export const SUPPORTED_ENCODINGS = [
|
|
|
12
12
|
"windows-1251",
|
|
13
13
|
"iso-8859-1",
|
|
14
14
|
];
|
|
15
|
+
// Rejection-message roster: utf8 is always accepted (see ALIASES), the roster is the six model codecs.
|
|
16
|
+
export const SUPPORTED_ENCODINGS_LABEL = `utf8, ${SUPPORTED_ENCODINGS.join(", ")}`;
|
|
15
17
|
const ALIASES = {
|
|
16
18
|
utf8: "utf8",
|
|
17
19
|
utf8bom: "utf8bom",
|
|
@@ -33,44 +33,60 @@ import { DomainError, formatError } from "./domain-errors.js";
|
|
|
33
33
|
* A drifted memo is dropped first, so a changed file is re-detected instead of
|
|
34
34
|
* having the old encoding laundered onto its new bytes.
|
|
35
35
|
*/
|
|
36
|
-
export function encodingHintAtVersion(
|
|
37
|
-
invalidateIfStale(
|
|
38
|
-
const encoding =
|
|
36
|
+
export function encodingHintAtVersion(filePath, currentVersion) {
|
|
37
|
+
invalidateIfStale(filePath, currentVersion);
|
|
38
|
+
const encoding = getEncodingState(filePath)?.encoding;
|
|
39
39
|
if (encoding === undefined || encoding === "utf8" || encoding === "utf8bom")
|
|
40
40
|
return undefined;
|
|
41
41
|
return encoding;
|
|
42
42
|
}
|
|
43
43
|
const encodingMemo = new Map();
|
|
44
44
|
const autoGuessFooterMemo = new Map();
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
/**
|
|
46
|
+
* The memo identity of a file: its absolute path, slash-normalized (ADR-0038).
|
|
47
|
+
*
|
|
48
|
+
* This module is the only key construction site. A caller passes whatever path it
|
|
49
|
+
* holds — `FileIO.resolve` already returned `fs.processPath(target)` — and gets
|
|
50
|
+
* the same identity back, so no reader has to probe a second spelling: a memo
|
|
51
|
+
* keyed two ways makes every lookup a ladder and every write a dual write, and
|
|
52
|
+
* the two spellings then have to be kept in sync by hand. Backend canonicaliza-
|
|
53
|
+
* tion (symlink/case, the backend's opaque `targetKey`) is the backend's view of
|
|
54
|
+
* the target, not the memo's identity; it does not belong in this key — which is
|
|
55
|
+
* why every public function here takes `filePath`, never a backend key.
|
|
56
|
+
*/
|
|
57
|
+
function keyOf(path) {
|
|
58
|
+
return String(path).replace(/\\/g, "/");
|
|
59
|
+
}
|
|
60
|
+
export function getEncodingState(filePath) {
|
|
61
|
+
return encodingMemo.get(keyOf(filePath));
|
|
47
62
|
}
|
|
48
|
-
export function setEncodingState(
|
|
49
|
-
encodingMemo.set(
|
|
63
|
+
export function setEncodingState(filePath, state) {
|
|
64
|
+
encodingMemo.set(keyOf(filePath), state);
|
|
50
65
|
}
|
|
51
|
-
export function clearEncodingState(
|
|
52
|
-
if (
|
|
53
|
-
encodingMemo.delete(
|
|
66
|
+
export function clearEncodingState(filePath) {
|
|
67
|
+
if (filePath)
|
|
68
|
+
encodingMemo.delete(keyOf(filePath));
|
|
54
69
|
else
|
|
55
70
|
encodingMemo.clear();
|
|
56
71
|
}
|
|
57
|
-
export function getAutoGuessFooter(
|
|
58
|
-
return autoGuessFooterMemo.get(
|
|
72
|
+
export function getAutoGuessFooter(filePath) {
|
|
73
|
+
return autoGuessFooterMemo.get(keyOf(filePath));
|
|
59
74
|
}
|
|
60
|
-
export function setAutoGuessFooter(
|
|
61
|
-
autoGuessFooterMemo.set(
|
|
75
|
+
export function setAutoGuessFooter(filePath, footer) {
|
|
76
|
+
autoGuessFooterMemo.set(keyOf(filePath), footer);
|
|
62
77
|
}
|
|
63
|
-
export function clearAutoGuessFooter(
|
|
64
|
-
if (
|
|
65
|
-
autoGuessFooterMemo.delete(
|
|
78
|
+
export function clearAutoGuessFooter(filePath) {
|
|
79
|
+
if (filePath)
|
|
80
|
+
autoGuessFooterMemo.delete(keyOf(filePath));
|
|
66
81
|
else
|
|
67
82
|
autoGuessFooterMemo.clear();
|
|
68
83
|
}
|
|
69
84
|
/** Invalidate memo when FsVersion drifted (ADR-0012). */
|
|
70
|
-
export function invalidateIfStale(
|
|
71
|
-
const
|
|
85
|
+
export function invalidateIfStale(filePath, currentVersion) {
|
|
86
|
+
const key = keyOf(filePath);
|
|
87
|
+
const memo = encodingMemo.get(key);
|
|
72
88
|
if (memo && memo.version !== currentVersion)
|
|
73
|
-
encodingMemo.delete(
|
|
89
|
+
encodingMemo.delete(key);
|
|
74
90
|
}
|
|
75
91
|
// ---------------------------------------------------------------------------
|
|
76
92
|
// Pure helpers — no IO
|
|
@@ -283,21 +299,22 @@ export async function decodeForOpen(bytes, config, opts = {}) {
|
|
|
283
299
|
});
|
|
284
300
|
}
|
|
285
301
|
// ---------------------------------------------------------------------------
|
|
286
|
-
// Recording helpers — caller supplies
|
|
302
|
+
// Recording helpers — caller supplies the file path + version after decode
|
|
303
|
+
// (identity is `keyOf`'d inside the seam, so one path is one memo entry)
|
|
287
304
|
// ---------------------------------------------------------------------------
|
|
288
|
-
export function recordOpenState(
|
|
305
|
+
export function recordOpenState(filePath, text, encoding, hasBOM, version) {
|
|
289
306
|
const state = {
|
|
290
307
|
encoding,
|
|
291
308
|
hasBOM,
|
|
292
309
|
lineEnding: detectEnding(text),
|
|
293
310
|
version,
|
|
294
311
|
};
|
|
295
|
-
setEncodingState(
|
|
312
|
+
setEncodingState(filePath, state);
|
|
296
313
|
return state;
|
|
297
314
|
}
|
|
298
|
-
export function recordFooter(
|
|
315
|
+
export function recordFooter(filePath, footer) {
|
|
299
316
|
if (footer)
|
|
300
|
-
setAutoGuessFooter(
|
|
317
|
+
setAutoGuessFooter(filePath, footer);
|
|
301
318
|
}
|
|
302
319
|
/** Shared UTF-8 BOM marker — single source for the BOM round-trip seam.
|
|
303
320
|
* Prefer `prepareForSave` / `stripBOM` over re-implementing this check. */
|
package/lib/file-view.js
CHANGED
|
@@ -2,32 +2,27 @@
|
|
|
2
2
|
* FileView — deep module owning "what the model sees".
|
|
3
3
|
*
|
|
4
4
|
* Single seam for normalize → hash → render → truncate → served-row
|
|
5
|
-
* selection. Previously split across
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* selection. Previously split across five shallow modules
|
|
6
|
+
* (interface≈implementation). Now one file owns the invariant; deleting it
|
|
7
|
+
* would scatter complexity (deep).
|
|
8
8
|
*
|
|
9
|
-
* Private helpers inlined from
|
|
10
|
-
*
|
|
11
|
-
* (loadFileKindAndText), validation (valKind/valAccess).
|
|
12
|
-
* Old files are shims re-exporting from this seam for compat.
|
|
9
|
+
* Private helpers inlined from the retired reader, renderer and truncate
|
|
10
|
+
* modules: normFromText/fileSnap, fmtReadPreview, truncateHead.
|
|
13
11
|
*
|
|
14
12
|
* Two surfaces:
|
|
15
|
-
* - `
|
|
13
|
+
* - `fmtReadPreview` (pure, no IO) — tested without filesystem
|
|
16
14
|
* - `readView` (IO) — read + normalize + render + truncate + hashes
|
|
17
15
|
*
|
|
18
16
|
* @module dsh-better-edit/file-view
|
|
19
17
|
*/
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
22
|
-
import { access as fsAccess } from "fs/promises";
|
|
23
|
-
import { fileTypeFromBuffer } from "file-type";
|
|
24
|
-
import { SNIFF_BYTES, MAX_BYTES, MAX_READ_LINE_BYTES, eLargeFileMsg } from "./constants.js";
|
|
18
|
+
import { stat as fsStat } from "fs/promises";
|
|
19
|
+
import { MAX_READ_LINE_BYTES, eLargeFileMsg } from "./constants.js";
|
|
25
20
|
import { lineHashes, fmtRegion, HASH_SEP } from "./hashline/index.js";
|
|
26
21
|
import { HASH_SPACE } from "./hashline/hash-assign.js";
|
|
27
|
-
import { visLines, abortIf
|
|
22
|
+
import { visLines, abortIf } from "./utils.js";
|
|
28
23
|
import { DomainError } from "./domain-errors.js";
|
|
29
24
|
import { detectEnding, toLF, stripBOM } from "./edit-diff.js";
|
|
30
|
-
import { resolveTarget
|
|
25
|
+
import { resolveTarget } from "./paths.js";
|
|
31
26
|
export const MAX_HASH_LINES = HASH_SPACE;
|
|
32
27
|
export const DEFAULT_MAX_LINES = 2000;
|
|
33
28
|
export const DEFAULT_MAX_BYTES = 50 * 1024;
|
|
@@ -119,158 +114,6 @@ export function truncateHead(content, options = {}) {
|
|
|
119
114
|
maxBytes,
|
|
120
115
|
};
|
|
121
116
|
}
|
|
122
|
-
// --- File kind (from file-kind.ts, private) ---
|
|
123
|
-
const IMG_TYPES = new Set(["image/jpeg", "image/png", "image/gif", "image/webp"]);
|
|
124
|
-
const TEXT_TYPES = new Set([
|
|
125
|
-
"application/rtf",
|
|
126
|
-
"application/xml",
|
|
127
|
-
"application/x-ms-regedit",
|
|
128
|
-
]);
|
|
129
|
-
function detectTextBom(sample) {
|
|
130
|
-
if (sample.length >= 4 &&
|
|
131
|
-
sample[0] === 0xff &&
|
|
132
|
-
sample[1] === 0xfe &&
|
|
133
|
-
sample[2] === 0x00 &&
|
|
134
|
-
sample[3] === 0x00)
|
|
135
|
-
return "UTF-32LE";
|
|
136
|
-
if (sample.length >= 4 &&
|
|
137
|
-
sample[0] === 0x00 &&
|
|
138
|
-
sample[1] === 0x00 &&
|
|
139
|
-
sample[2] === 0xfe &&
|
|
140
|
-
sample[3] === 0xff)
|
|
141
|
-
return "UTF-32BE";
|
|
142
|
-
if (sample.length >= 2 && sample[0] === 0xff && sample[1] === 0xfe)
|
|
143
|
-
return "UTF-16LE";
|
|
144
|
-
if (sample.length >= 2 && sample[0] === 0xfe && sample[1] === 0xff)
|
|
145
|
-
return "UTF-16BE";
|
|
146
|
-
return undefined;
|
|
147
|
-
}
|
|
148
|
-
function isTextType(mimeType) {
|
|
149
|
-
return mimeType.startsWith("text/") || TEXT_TYPES.has(mimeType);
|
|
150
|
-
}
|
|
151
|
-
export async function loadFileKindAndText(filePath, options) {
|
|
152
|
-
const pathStat = await fsStat(filePath);
|
|
153
|
-
if (pathStat.isDirectory()) {
|
|
154
|
-
return { kind: "directory" };
|
|
155
|
-
}
|
|
156
|
-
if (!pathStat.isFile()) {
|
|
157
|
-
return {
|
|
158
|
-
kind: "binary",
|
|
159
|
-
description: "unsupported file type",
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
if (pathStat.size > MAX_BYTES) {
|
|
163
|
-
return {
|
|
164
|
-
kind: "binary",
|
|
165
|
-
description: `file exceeds ${MAX_BYTES} byte limit`,
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
const fileHandle = await fsOpen(filePath, "r");
|
|
169
|
-
try {
|
|
170
|
-
const buffer = Buffer.alloc(SNIFF_BYTES);
|
|
171
|
-
const { bytesRead } = await fileHandle.read(buffer, 0, SNIFF_BYTES, 0);
|
|
172
|
-
if (bytesRead === 0) {
|
|
173
|
-
return { kind: "text", text: "", stats: pathStat };
|
|
174
|
-
}
|
|
175
|
-
const sample = buffer.subarray(0, bytesRead);
|
|
176
|
-
const textBom = detectTextBom(sample);
|
|
177
|
-
if (textBom) {
|
|
178
|
-
return {
|
|
179
|
-
kind: "binary",
|
|
180
|
-
description: `${textBom} encoded text`,
|
|
181
|
-
};
|
|
182
|
-
}
|
|
183
|
-
const detectedMimeType = (await fileTypeFromBuffer(sample))?.mime;
|
|
184
|
-
if (detectedMimeType !== undefined && !isTextType(detectedMimeType)) {
|
|
185
|
-
if (IMG_TYPES.has(detectedMimeType)) {
|
|
186
|
-
return { kind: "image", mimeType: detectedMimeType };
|
|
187
|
-
}
|
|
188
|
-
return {
|
|
189
|
-
kind: "binary",
|
|
190
|
-
description: detectedMimeType,
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
const decoder = new TextDecoder("utf-8", { fatal: false, ignoreBOM: true });
|
|
194
|
-
let hadUtf8DecodeErrors = false;
|
|
195
|
-
let newlineCount = 0;
|
|
196
|
-
const parts = [];
|
|
197
|
-
function decodeChunk(chunk, stream) {
|
|
198
|
-
const decoded = decoder.decode(chunk, { stream });
|
|
199
|
-
if (!hadUtf8DecodeErrors && decoded.includes("\uFFFD")) {
|
|
200
|
-
hadUtf8DecodeErrors = true;
|
|
201
|
-
}
|
|
202
|
-
if (options?.maxLines !== undefined) {
|
|
203
|
-
for (let i = 0; i < decoded.length; i++) {
|
|
204
|
-
if (decoded.charCodeAt(i) === 10)
|
|
205
|
-
newlineCount++;
|
|
206
|
-
}
|
|
207
|
-
if (newlineCount > options.maxLines) {
|
|
208
|
-
throw new Error(eLargeFileMsg(options.displayPath ?? filePath, newlineCount, options.maxLines));
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
return decoded;
|
|
212
|
-
}
|
|
213
|
-
parts.push(decodeChunk(sample, true));
|
|
214
|
-
let position = bytesRead;
|
|
215
|
-
while (true) {
|
|
216
|
-
const { bytesRead: chunkBytesRead } = await fileHandle.read(buffer, 0, SNIFF_BYTES, position);
|
|
217
|
-
if (chunkBytesRead === 0) {
|
|
218
|
-
break;
|
|
219
|
-
}
|
|
220
|
-
const chunk = buffer.subarray(0, chunkBytesRead);
|
|
221
|
-
parts.push(decodeChunk(chunk, true));
|
|
222
|
-
position += chunkBytesRead;
|
|
223
|
-
}
|
|
224
|
-
parts.push(decodeChunk(new Uint8Array(0), false));
|
|
225
|
-
return {
|
|
226
|
-
kind: "text",
|
|
227
|
-
text: parts.join(""),
|
|
228
|
-
stats: pathStat,
|
|
229
|
-
...(hadUtf8DecodeErrors ? { hadUtf8DecodeErrors: true } : {}),
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
finally {
|
|
233
|
-
await fileHandle.close();
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
// --- Validation (from validation.ts, private) ---
|
|
237
|
-
export async function valAccess(absolutePath, path, accessMode = constants.R_OK) {
|
|
238
|
-
try {
|
|
239
|
-
await fsAccess(absolutePath, accessMode);
|
|
240
|
-
}
|
|
241
|
-
catch (error) {
|
|
242
|
-
const code = errCode(error);
|
|
243
|
-
if (code === "ENOENT") {
|
|
244
|
-
throw new DomainError("E_NOT_FOUND", { path });
|
|
245
|
-
}
|
|
246
|
-
if (code === "EACCES" || code === "EPERM") {
|
|
247
|
-
throw new DomainError("E_ACCESS", {
|
|
248
|
-
path,
|
|
249
|
-
kind: "denied",
|
|
250
|
-
access: accessMode & constants.W_OK ? "write" : "read",
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
if (code === "ELOOP") {
|
|
254
|
-
throw new DomainError("E_ACCESS", { path, kind: "symlink-loop" });
|
|
255
|
-
}
|
|
256
|
-
throw new DomainError("E_ACCESS", { path, kind: "unreachable" });
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
export function valKind(file, path) {
|
|
260
|
-
if (file.kind === "directory") {
|
|
261
|
-
throw new DomainError("E_UNSUPPORTED_FILE", { path, kind: "directory" });
|
|
262
|
-
}
|
|
263
|
-
if (file.kind === "binary") {
|
|
264
|
-
throw new DomainError("E_UNSUPPORTED_FILE", {
|
|
265
|
-
path,
|
|
266
|
-
kind: "binary",
|
|
267
|
-
description: file.description,
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
if (file.kind === "image") {
|
|
271
|
-
throw new DomainError("E_UNSUPPORTED_FILE", { path, kind: "image" });
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
117
|
function fmtSnapId(canonicalPath, info) {
|
|
275
118
|
return `v2|${canonicalPath}|${info.ino}|${info.mtimeMs}|${info.ctimeMs}|${info.size}`;
|
|
276
119
|
}
|
|
@@ -313,35 +156,7 @@ export async function normFromText(input) {
|
|
|
313
156
|
hadUtf8DecodeErrors: input.hadUtf8DecodeErrors === true,
|
|
314
157
|
};
|
|
315
158
|
}
|
|
316
|
-
|
|
317
|
-
const absolutePath = toCwd(path, cwd);
|
|
318
|
-
const resolvedPath = await resolveTarget(absolutePath);
|
|
319
|
-
const signal = options?.signal;
|
|
320
|
-
const accessMode = options?.accessMode ?? constants.R_OK;
|
|
321
|
-
abortIf(signal);
|
|
322
|
-
await valAccess(resolvedPath, path, accessMode);
|
|
323
|
-
abortIf(signal);
|
|
324
|
-
const file = options?.preloadedFile ??
|
|
325
|
-
(await loadFileKindAndText(resolvedPath, {
|
|
326
|
-
maxLines: options?.maxLines,
|
|
327
|
-
displayPath: path,
|
|
328
|
-
}));
|
|
329
|
-
valKind(file, path);
|
|
330
|
-
return normFromText({
|
|
331
|
-
absolutePath: resolvedPath,
|
|
332
|
-
rawText: file.text,
|
|
333
|
-
displayPath: path,
|
|
334
|
-
signal,
|
|
335
|
-
maxLines: options?.maxLines,
|
|
336
|
-
store: options?.store,
|
|
337
|
-
noPersist: options?.noPersist,
|
|
338
|
-
reservedHashes: options?.reservedHashes,
|
|
339
|
-
retiredHashes: options?.retiredHashes,
|
|
340
|
-
hadUtf8DecodeErrors: file.hadUtf8DecodeErrors,
|
|
341
|
-
previous: options?.previous,
|
|
342
|
-
});
|
|
343
|
-
}
|
|
344
|
-
// --- Read render (from read-render.ts, private) ---
|
|
159
|
+
// --- Read render (private to this seam) ---
|
|
345
160
|
function normPosInt(value, name) {
|
|
346
161
|
if (value === undefined)
|
|
347
162
|
return undefined;
|
|
@@ -569,12 +384,16 @@ function buildWindowSection(params) {
|
|
|
569
384
|
* runs first appear in the request — that order is part of the request, so it is never sorted by
|
|
570
385
|
* position — while every section draws on ONE shared byte/line budget so N windows cannot multiply
|
|
571
386
|
* the auto-read budget by N. Rows are served only for the lines actually shown; because each
|
|
572
|
-
* position belongs to exactly one run, the position record is injective by construction.
|
|
387
|
+
* position belongs to exactly one run, the union's position record is injective by construction.
|
|
388
|
+
* The record is SORTED by position but never de-duplicated: the sort is load-bearing (runs render
|
|
389
|
+
* in request order, so run order is not position order), yet a collector that dropped duplicates
|
|
390
|
+
* would mask a `mergeWindowRuns` regression — overlapping runs would collapse into one served row
|
|
391
|
+
* instead of surfacing as two for the same position.
|
|
573
392
|
*/
|
|
574
393
|
function buildWindowedPreview(params) {
|
|
575
394
|
const { windows, allLines, allHashes, totalLines, maxBytes, maxTruncLines } = params;
|
|
576
395
|
const sections = [];
|
|
577
|
-
const
|
|
396
|
+
const servedRows = [];
|
|
578
397
|
let truncation;
|
|
579
398
|
let remainingBytes = maxBytes;
|
|
580
399
|
let remainingLines = maxTruncLines;
|
|
@@ -614,7 +433,7 @@ function buildWindowedPreview(params) {
|
|
|
614
433
|
});
|
|
615
434
|
sections.push(`${header}\n${built.text}`);
|
|
616
435
|
for (const row of built.served)
|
|
617
|
-
|
|
436
|
+
servedRows.push(row);
|
|
618
437
|
remainingLines -= built.text === "" ? 0 : built.text.split("\n").length;
|
|
619
438
|
remainingBytes -= Buffer.byteLength(`${built.text}${header}`, "utf-8");
|
|
620
439
|
if (truncation === undefined && built.truncation)
|
|
@@ -626,9 +445,7 @@ function buildWindowedPreview(params) {
|
|
|
626
445
|
// WHY: a multi-window request is N discrete slices, not one stream, so the result carries no root
|
|
627
446
|
// WHY: `nextOffset`: a scalar would invite `offset = nextOffset` and silently re-read a window the
|
|
628
447
|
// WHY: caller never asked to continue. A truncated window says so in its own section text.
|
|
629
|
-
served: [...
|
|
630
|
-
.sort((left, right) => left[0] - right[0])
|
|
631
|
-
.map(([position, hash]) => ({ position, hash })),
|
|
448
|
+
served: [...servedRows].sort((left, right) => left.position - right.position),
|
|
632
449
|
};
|
|
633
450
|
}
|
|
634
451
|
export async function fmtReadPreview(text, options, precomputedHashes, path, maxLineBytes = MAX_READ_LINE_BYTES, maxTruncLines = DEFAULT_MAX_LINES) {
|
|
@@ -709,9 +526,6 @@ export async function fmtReadPreview(text, options, precomputedHashes, path, max
|
|
|
709
526
|
served: normal.served,
|
|
710
527
|
};
|
|
711
528
|
}
|
|
712
|
-
export async function preview(content, hashes, opts = {}, absolutePath) {
|
|
713
|
-
return fmtReadPreview(content, opts, hashes, absolutePath);
|
|
714
|
-
}
|
|
715
529
|
export async function readView(io, path, cwd, opts = {}) {
|
|
716
530
|
const { signal } = opts;
|
|
717
531
|
const absolutePath = await io.resolve(path, cwd, signal);
|
|
@@ -727,6 +541,12 @@ export async function readView(io, path, cwd, opts = {}) {
|
|
|
727
541
|
previous: opts.previous,
|
|
728
542
|
});
|
|
729
543
|
const r = await fmtReadPreview(normalized, { offset: opts.offset, limit: opts.limit, windows: opts.windows }, fileHashes, absolutePath);
|
|
544
|
+
// The ONE computation of the full-read verdict (ADR-0035): the whole file, no page request, and
|
|
545
|
+
// nothing truncated away. Consumers read `view.fullRead`; none of them re-derives it.
|
|
546
|
+
const fullRead = opts.offset === undefined &&
|
|
547
|
+
opts.limit === undefined &&
|
|
548
|
+
!hasRequestedWindows(opts.windows) &&
|
|
549
|
+
!r.truncation?.truncated;
|
|
730
550
|
return {
|
|
731
551
|
text: r.text,
|
|
732
552
|
hashes: fileHashes,
|
|
@@ -734,6 +554,7 @@ export async function readView(io, path, cwd, opts = {}) {
|
|
|
734
554
|
absolutePath,
|
|
735
555
|
truncation: r.truncation,
|
|
736
556
|
nextOffset: r.nextOffset,
|
|
557
|
+
fullRead,
|
|
737
558
|
hadUtf8DecodeErrors,
|
|
738
559
|
bom,
|
|
739
560
|
originalEnding,
|
package/lib/fs-bridge.js
CHANGED
|
@@ -17,10 +17,11 @@
|
|
|
17
17
|
import { readFile } from "node:fs/promises";
|
|
18
18
|
import { writeAtomic } from "./fs-write.js";
|
|
19
19
|
import { loadConfig } from "./store-config.js";
|
|
20
|
-
import { fileSnap } from "./file-
|
|
20
|
+
import { fileSnap } from "./file-view.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
23
|
import { codeOf, errCode } from "./utils.js";
|
|
24
|
+
import { SUPPORTED_ENCODINGS_LABEL } from "./encoding.js";
|
|
24
25
|
import { DomainError } from "./domain-errors.js";
|
|
25
26
|
export { _getEncodingState as getEncodingState, _setEncodingState as setEncodingState, _clearEncodingState as clearEncodingState, _getAutoGuessFooter as getAutoGuessFooter, _setAutoGuessFooter as setAutoGuessFooter, _clearAutoGuessFooter as clearAutoGuessFooter, };
|
|
26
27
|
// Keep alias for tests that import getAutoGuessFooter via fs-bridge
|
|
@@ -54,7 +55,7 @@ export function mapFsError(error, displayPath) {
|
|
|
54
55
|
}
|
|
55
56
|
if (code === "FS_BAD_ENCODING") {
|
|
56
57
|
throw new DomainError("E_BAD_PAYLOAD", {
|
|
57
|
-
message: `Unknown encoding for ${displayPath}. Supported:
|
|
58
|
+
message: `Unknown encoding for ${displayPath}. Supported: ${SUPPORTED_ENCODINGS_LABEL}`,
|
|
58
59
|
});
|
|
59
60
|
}
|
|
60
61
|
if (code === "FS_DECODE_FAILED") {
|
|
@@ -122,15 +123,6 @@ async function restoreStrippedUtf8Bom(fs, target, text, signal) {
|
|
|
122
123
|
}
|
|
123
124
|
return { text: `${UTF8_BOM}${text}`, version };
|
|
124
125
|
}
|
|
125
|
-
/**
|
|
126
|
-
* The memo/policy key for a resolved target: the backend's optional `targetKey`
|
|
127
|
-
* when it carries one (symlink/case canonicalisation), else the absolute path.
|
|
128
|
-
* One seam for the five read/write sites so the memo cannot be keyed two ways.
|
|
129
|
-
*/
|
|
130
|
-
function targetKeyOf(target, absolutePath) {
|
|
131
|
-
// SAFETY: FsTarget is opaque; `targetKey` is an optional string field on the local backend.
|
|
132
|
-
return String(target.targetKey ?? absolutePath);
|
|
133
|
-
}
|
|
134
126
|
export function ctxFsIO(fs, ctx) {
|
|
135
127
|
return {
|
|
136
128
|
async resolve(path, cwd, signal) {
|
|
@@ -154,18 +146,9 @@ export function ctxFsIO(fs, ctx) {
|
|
|
154
146
|
encodingHint,
|
|
155
147
|
displayPath: absolutePath,
|
|
156
148
|
});
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
// (symlink/case) — mirror under both keys (issue #69 note).
|
|
161
|
-
if (targetKey !== absolutePath) {
|
|
162
|
-
recordOpenState(absolutePath, decoded.text, decoded.encoding, decoded.hasBOM, info?.version);
|
|
163
|
-
}
|
|
164
|
-
if (decoded.footer) {
|
|
165
|
-
recordFooter(targetKey, decoded.footer);
|
|
166
|
-
if (targetKey !== absolutePath)
|
|
167
|
-
recordFooter(absolutePath, decoded.footer);
|
|
168
|
-
}
|
|
149
|
+
recordOpenState(absolutePath, decoded.text, decoded.encoding, decoded.hasBOM, info?.version);
|
|
150
|
+
if (decoded.footer)
|
|
151
|
+
recordFooter(absolutePath, decoded.footer);
|
|
169
152
|
return decoded.text;
|
|
170
153
|
}
|
|
171
154
|
try {
|
|
@@ -179,18 +162,12 @@ export function ctxFsIO(fs, ctx) {
|
|
|
179
162
|
const { text: restored, version } = await restoreStrippedUtf8Bom(fs, target, text, signal);
|
|
180
163
|
// record file encoding state for round-trip (BOM + lineEnding)
|
|
181
164
|
try {
|
|
182
|
-
const targetKey = targetKeyOf(target, absolutePath);
|
|
183
165
|
const hasBOM = restored.startsWith(UTF8_BOM);
|
|
184
166
|
const clean = hasBOM ? restored.slice(1) : restored;
|
|
185
167
|
// use seam to record with correct lineEnding detection
|
|
186
|
-
recordOpenState(
|
|
187
|
-
if (targetKey !== absolutePath) {
|
|
188
|
-
recordOpenState(absolutePath, clean, hasBOM ? "utf8bom" : "utf8", hasBOM, version);
|
|
189
|
-
}
|
|
168
|
+
recordOpenState(absolutePath, clean, hasBOM ? "utf8bom" : "utf8", hasBOM, version);
|
|
190
169
|
// clear any stale autoGuess footer on clean UTF-8 read
|
|
191
|
-
_clearAutoGuessFooter(
|
|
192
|
-
if (targetKey !== absolutePath)
|
|
193
|
-
_clearAutoGuessFooter(absolutePath);
|
|
170
|
+
_clearAutoGuessFooter(absolutePath);
|
|
194
171
|
}
|
|
195
172
|
catch {
|
|
196
173
|
// best-effort
|
|
@@ -209,18 +186,11 @@ export function ctxFsIO(fs, ctx) {
|
|
|
209
186
|
if (info && info.size !== undefined && info.size > maxBytes)
|
|
210
187
|
throw error;
|
|
211
188
|
const bytes = await fs.readBytes(target, signal, maxBytes);
|
|
212
|
-
const targetKey = targetKeyOf(target, absolutePath);
|
|
213
189
|
// Delegate deterministic admission to the deep seam
|
|
214
190
|
const decoded = await decodeForOpen(bytes, cfg, { displayPath: absolutePath });
|
|
215
|
-
recordOpenState(
|
|
216
|
-
if (
|
|
217
|
-
|
|
218
|
-
}
|
|
219
|
-
if (decoded.footer) {
|
|
220
|
-
recordFooter(targetKey, decoded.footer);
|
|
221
|
-
if (targetKey !== absolutePath)
|
|
222
|
-
recordFooter(absolutePath, decoded.footer);
|
|
223
|
-
}
|
|
191
|
+
recordOpenState(absolutePath, decoded.text, decoded.encoding, decoded.hasBOM, info?.version);
|
|
192
|
+
if (decoded.footer)
|
|
193
|
+
recordFooter(absolutePath, decoded.footer);
|
|
224
194
|
return decoded.text;
|
|
225
195
|
}
|
|
226
196
|
return mapFsError(error, absolutePath);
|
|
@@ -231,19 +201,16 @@ export function ctxFsIO(fs, ctx) {
|
|
|
231
201
|
const target = await fs.resolve(absolutePath, {
|
|
232
202
|
...(signal === undefined ? {} : { signal }),
|
|
233
203
|
});
|
|
234
|
-
const key = targetKeyOf(target, absolutePath);
|
|
235
204
|
// Drop a memo that no longer matches the on-disk version before deciding:
|
|
236
205
|
// the save verdict must describe the bytes actually being replaced.
|
|
237
206
|
const info = await fs.stat(target, signal).catch(() => undefined);
|
|
238
207
|
const version = info?.version;
|
|
239
|
-
invalidateIfStale(
|
|
240
|
-
if (key !== absolutePath)
|
|
241
|
-
invalidateIfStale(absolutePath, version);
|
|
208
|
+
invalidateIfStale(absolutePath, version);
|
|
242
209
|
// The seam owns the round-trip verdict. It throws E_ENCODING_UNWRITABLE
|
|
243
210
|
// BEFORE the provider sees a write when the recorded encoding cannot be
|
|
244
211
|
// reproduced through the text-only write (issue #112).
|
|
245
212
|
const cfgW = loadConfig();
|
|
246
|
-
const existing = _getEncodingState(
|
|
213
|
+
const existing = _getEncodingState(absolutePath);
|
|
247
214
|
const prep = prepareForSave(content, existing, {
|
|
248
215
|
normalizeToUtf8: cfgW.normalizeToUtf8,
|
|
249
216
|
displayPath: absolutePath,
|
|
@@ -259,9 +226,7 @@ export function ctxFsIO(fs, ctx) {
|
|
|
259
226
|
...prep.newState,
|
|
260
227
|
version: outcome.version,
|
|
261
228
|
};
|
|
262
|
-
_setEncodingState(
|
|
263
|
-
if (key !== absolutePath)
|
|
264
|
-
_setEncodingState(absolutePath, written);
|
|
229
|
+
_setEncodingState(absolutePath, written);
|
|
265
230
|
}
|
|
266
231
|
}
|
|
267
232
|
catch (error) {
|