dsh-better-edit 0.3.0 → 0.3.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.
Files changed (73) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +9 -5
  3. package/README.zh.md +1 -2
  4. package/lib/constants.js +3 -1
  5. package/lib/contract.js +133 -76
  6. package/lib/drift.js +5 -98
  7. package/lib/dsh-context.js +2 -24
  8. package/lib/edit-engine.js +6 -5
  9. package/lib/edit-pipeline.js +3 -121
  10. package/lib/edit-response.js +16 -8
  11. package/lib/file-kind.js +6 -121
  12. package/lib/file-reader.js +4 -77
  13. package/lib/file-view.js +446 -23
  14. package/lib/guidance/materialize.js +25 -4
  15. package/lib/guidance/resolve.js +3 -9
  16. package/lib/hash-store.js +6 -3
  17. package/lib/hashline/alphabet.js +2 -6
  18. package/lib/hashline/anchor-pipeline.js +770 -7
  19. package/lib/hashline/apply.js +2 -223
  20. package/lib/hashline/hash-assign.js +248 -13
  21. package/lib/hashline/hash.js +67 -29
  22. package/lib/hashline/hasher.js +2 -23
  23. package/lib/hashline/index.js +7 -1
  24. package/lib/hashline/parse.js +2 -35
  25. package/lib/hashline/pure.js +2 -213
  26. package/lib/hashline/resolve.js +3 -366
  27. package/lib/hashline/served.js +2 -120
  28. package/lib/index.js +0 -2
  29. package/lib/mutation.js +218 -10
  30. package/lib/noop-guard.js +20 -0
  31. package/lib/preview-controller.js +65 -0
  32. package/lib/prompts.js +14 -20
  33. package/lib/read-render.js +3 -139
  34. package/lib/served-store.js +2 -216
  35. package/lib/session-view.js +309 -11
  36. package/lib/tool-edit.js +71 -125
  37. package/lib/tool-undo.js +11 -5
  38. package/lib/truncate.js +3 -95
  39. package/lib/types/constants.d.ts +2 -0
  40. package/lib/types/contract.d.ts +102 -28
  41. package/lib/types/drift.d.ts +5 -29
  42. package/lib/types/dsh-context.d.ts +2 -20
  43. package/lib/types/edit-pipeline.d.ts +3 -44
  44. package/lib/types/file-kind.d.ts +6 -18
  45. package/lib/types/file-reader.d.ts +4 -45
  46. package/lib/types/file-view.d.ts +99 -34
  47. package/lib/types/guidance/materialize.d.ts +3 -3
  48. package/lib/types/guidance/resolve.d.ts +2 -2
  49. package/lib/types/hashline/alphabet.d.ts +1 -5
  50. package/lib/types/hashline/anchor-pipeline.d.ts +111 -10
  51. package/lib/types/hashline/apply.d.ts +1 -22
  52. package/lib/types/hashline/hash-assign.d.ts +25 -19
  53. package/lib/types/hashline/hash.d.ts +8 -9
  54. package/lib/types/hashline/hasher.d.ts +1 -8
  55. package/lib/types/hashline/index.d.ts +7 -1
  56. package/lib/types/hashline/parse.d.ts +1 -7
  57. package/lib/types/hashline/pure.d.ts +1 -26
  58. package/lib/types/hashline/resolve.d.ts +1 -63
  59. package/lib/types/hashline/served.d.ts +1 -43
  60. package/lib/types/mutation.d.ts +63 -10
  61. package/lib/types/noop-guard.d.ts +29 -0
  62. package/lib/types/preview-controller.d.ts +33 -0
  63. package/lib/types/prompts.d.ts +9 -9
  64. package/lib/types/read-render.d.ts +3 -18
  65. package/lib/types/served-store.d.ts +2 -81
  66. package/lib/types/session-view.d.ts +68 -9
  67. package/lib/types/tool-edit.d.ts +3 -14
  68. package/lib/types/truncate.d.ts +3 -29
  69. package/lib/types/validation.d.ts +6 -7
  70. package/lib/types/workspace.d.ts +2 -21
  71. package/lib/validation.js +6 -33
  72. package/lib/workspace.js +2 -27
  73. package/package.json +2 -2
@@ -1,124 +1,6 @@
1
1
  /**
2
- * The single-edit pipeline shared by `edit`, previews, and (through the
3
- * engine) `batch_edit`: resolve → normalize → hash → verify anchors against
4
- * served state → apply → re-hash with stable anchors → drift notice. The
5
- * apply/reject/re-hash computation lives in the edit engine
6
- * ({@link edit-engine}); this module keeps the single-edit orchestration
7
- * shape, preview-mode options, and the pipeline result type. All IO goes
8
- * through the {@link FileIO} bridge so the same pipeline runs on `ctx.fs` and
9
- * on the host filesystem.
2
+ * @deprecated — private to Mutation seam. Use `from "./mutation.js"` instead.
3
+ * Thin shim re-exporting the pipeline now owned by Mutation.
10
4
  * @module dsh-better-edit/edit-pipeline
11
5
  */
12
- import { normFromText, fileSnap } from './file-reader.js';
13
- import { toCwd } from './paths.js';
14
- import { resEdit } from './hashline/anchor-pipeline.js';
15
- import { MAX_HASH_LINES } from './hashline/hash-assign.js';
16
- import { AnchorMismatchError, ServedRejectionError, recordEchoServes, } from './hashline/anchor-pipeline.js';
17
- import { loadServed, sessionKeyFor, scanDrift } from './session-view.js';
18
- import { abortIf, splitLines } from './utils.js';
19
- import { applyOne } from './edit-engine.js';
20
- export async function execPipeline(io, params, cwd, options) {
21
- const path = params.path;
22
- const editWarnings = [];
23
- // Resolve the edit up front (before IO) so malformed anchors fail before
24
- // any filesystem work, exactly as the tool always did.
25
- const edit = resEdit({
26
- remove_from: params.remove_from,
27
- remove_to: params.remove_to,
28
- replacement_text: params.replacement_text,
29
- }, editWarnings);
30
- const hashStore = options?.store;
31
- const signal = options?.signal;
32
- abortIf(signal);
33
- const absolutePath = await io.resolve(path, cwd, signal);
34
- const rawText = await io.readText(absolutePath, signal);
35
- const { normalized: originalNormalized, bom, originalEnding, fileHashes: originalHashes, hadUtf8DecodeErrors, } = await normFromText({
36
- absolutePath,
37
- rawText,
38
- displayPath: path,
39
- signal,
40
- maxLines: MAX_HASH_LINES,
41
- store: hashStore,
42
- noPersist: options?.noPersist,
43
- });
44
- const sessionKey = options?.sessionKey ?? sessionKeyFor(undefined);
45
- const served = await loadServed(sessionKey, absolutePath);
46
- const policy = options?.noPersist === true ? 'preview' : 'live';
47
- const applied = await applyOne({
48
- content: originalNormalized,
49
- hashes: originalHashes,
50
- served,
51
- removeFrom: params.remove_from,
52
- removeTo: params.remove_to,
53
- replacementText: params.replacement_text,
54
- absolutePath,
55
- displayPath: path,
56
- signal,
57
- warnings: editWarnings,
58
- store: hashStore,
59
- persist: options?.noPersist !== true,
60
- edit,
61
- }, async (error) => {
62
- if (error instanceof AnchorMismatchError ||
63
- error instanceof ServedRejectionError) {
64
- await recordEchoServes(sessionKey, absolutePath, error.servedRows, policy, originalHashes.length);
65
- }
66
- throw error;
67
- });
68
- const result = applied.result;
69
- const isNoop = applied.noop;
70
- const warnings = [...editWarnings, ...(applied.anchorWarnings ?? [])];
71
- let driftNotice;
72
- if (options?.noPersist !== true) {
73
- try {
74
- driftNotice = await scanDrift({
75
- sessionKey,
76
- served,
77
- resultHashes: applied.hashes,
78
- resultLines: splitLines(result),
79
- range: applied.range,
80
- path: absolutePath,
81
- });
82
- }
83
- catch (error) {
84
- console.error('Failed to compute drift notice:', error);
85
- }
86
- }
87
- return {
88
- path,
89
- absolutePath,
90
- originalNormalized,
91
- result,
92
- bom,
93
- originalEnding,
94
- hadUtf8DecodeErrors,
95
- warnings,
96
- noopEdit: applied.noopEdit,
97
- firstChangedLine: applied.firstChangedLine,
98
- lastChangedLine: applied.lastChangedLine,
99
- originalHashes,
100
- resultHashes: applied.hashes,
101
- totalAddedLines: applied.totalAddedLines,
102
- totalRemovedLines: applied.totalRemovedLines,
103
- driftNotice,
104
- range: applied.range,
105
- };
106
- }
107
- /** Resolve the display path a caller names against the session cwd. */
108
- export function resolveDisplayPath(path, cwd) {
109
- return toCwd(path, cwd);
110
- }
111
- /** Snapshot bookkeeping for noop/success results (best-effort). */
112
- export async function snapshotIdFor(io, absolutePath, signal) {
113
- try {
114
- return await io.statVersion(absolutePath, signal);
115
- }
116
- catch {
117
- try {
118
- return (await fileSnap(absolutePath)).snapshotId;
119
- }
120
- catch {
121
- return undefined;
122
- }
123
- }
124
- }
6
+ export * from "./mutation.js";
@@ -34,7 +34,7 @@ export function finalizeToolResult(details) {
34
34
  return { content: [{ type: "text", text }], servedRows: details.servedRows };
35
35
  }
36
36
  function warnBlock(warnings) {
37
- return warnings?.length ? `\n\nWarnings:\n${warnings.join("\n")}` : "";
37
+ return warnings?.length ? `\n\n${warnings.join("\n")}` : "";
38
38
  }
39
39
  function driftBlock(driftNotice) {
40
40
  return driftNotice ? `\n\n${driftNotice}` : "";
@@ -61,7 +61,7 @@ export function buildNoop(input) {
61
61
  classification: "noop",
62
62
  metrics,
63
63
  ...(warnings !== undefined && warnings.length > 0 ? { warnings } : {}),
64
- ...(driftNotice !== undefined ? { driftNotice } : {}),
64
+ ...(driftNotice === undefined ? {} : { driftNotice }),
65
65
  },
66
66
  };
67
67
  }
@@ -92,6 +92,10 @@ export function buildChanged(input) {
92
92
  addedLines,
93
93
  removedLines,
94
94
  });
95
+ const denseServedRows = [];
96
+ for (let i = 0; i < resultHashes.length; i++) {
97
+ denseServedRows.push({ position: i, hash: resultHashes[i] });
98
+ }
95
99
  return {
96
100
  content: [{ type: "text", text }],
97
101
  details: {
@@ -100,8 +104,8 @@ export function buildChanged(input) {
100
104
  snapshotId,
101
105
  metrics,
102
106
  ...(warnings !== undefined && warnings.length > 0 ? { warnings } : {}),
103
- servedRows: diffResult.servedRows,
104
- ...(driftNotice !== undefined ? { driftNotice } : {}),
107
+ servedRows: denseServedRows,
108
+ ...(driftNotice === undefined ? {} : { driftNotice }),
105
109
  },
106
110
  };
107
111
  }
@@ -132,7 +136,7 @@ export function buildBatchResult(sections) {
132
136
  warningsCount: warnings.length,
133
137
  }),
134
138
  ...(warnings.length > 0 ? { warnings } : {}),
135
- ...(driftNotice !== undefined ? { driftNotice } : {}),
139
+ ...(driftNotice === undefined ? {} : { driftNotice }),
136
140
  },
137
141
  };
138
142
  }
@@ -141,8 +145,12 @@ export function buildBatchResult(sections) {
141
145
  for (const s of appliedFiles) {
142
146
  const diffResult = genDiff(s.originalNormalized, s.result, 1, s.resultHashes, s.originalHashes);
143
147
  diffParts.push(`--- ${s.path} ---\n${diffResult.diff}`);
144
- if (diffResult.servedRows.length > 0) {
145
- servedByPath.push({ path: s.path, servedRows: diffResult.servedRows });
148
+ const denseRows = [];
149
+ for (let i = 0; i < s.resultHashes.length; i++) {
150
+ denseRows.push({ position: i, hash: s.resultHashes[i] });
151
+ }
152
+ if (denseRows.length > 0) {
153
+ servedByPath.push({ path: s.path, servedRows: denseRows });
146
154
  }
147
155
  }
148
156
  const diff = diffParts.join("\n\n");
@@ -166,7 +174,7 @@ export function buildBatchResult(sections) {
166
174
  ...(warnings.length > 0 ? { warnings } : {}),
167
175
  servedRows: servedByPath.flatMap((e) => e.servedRows),
168
176
  servedByPath,
169
- ...(driftNotice !== undefined ? { driftNotice } : {}),
177
+ ...(driftNotice === undefined ? {} : { driftNotice }),
170
178
  },
171
179
  };
172
180
  }
package/lib/file-kind.js CHANGED
@@ -1,121 +1,6 @@
1
- import { open as fsOpen, stat as fsStat } from "fs/promises";
2
- import { fileTypeFromBuffer } from "file-type";
3
- import { SNIFF_BYTES, MAX_BYTES } from "./constants.js";
4
- const IMG_TYPES = new Set([
5
- "image/jpeg",
6
- "image/png",
7
- "image/gif",
8
- "image/webp",
9
- ]);
10
- const TEXT_TYPES = new Set([
11
- "application/rtf",
12
- "application/xml",
13
- "application/x-ms-regedit",
14
- ]);
15
- function detectTextBom(sample) {
16
- if (sample.length >= 4 &&
17
- sample[0] === 0xff &&
18
- sample[1] === 0xfe &&
19
- sample[2] === 0x00 &&
20
- sample[3] === 0x00)
21
- return "UTF-32LE";
22
- if (sample.length >= 4 &&
23
- sample[0] === 0x00 &&
24
- sample[1] === 0x00 &&
25
- sample[2] === 0xfe &&
26
- sample[3] === 0xff)
27
- return "UTF-32BE";
28
- if (sample.length >= 2 && sample[0] === 0xff && sample[1] === 0xfe)
29
- return "UTF-16LE";
30
- if (sample.length >= 2 && sample[0] === 0xfe && sample[1] === 0xff)
31
- return "UTF-16BE";
32
- return undefined;
33
- }
34
- function isTextType(mimeType) {
35
- return mimeType.startsWith("text/") || TEXT_TYPES.has(mimeType);
36
- }
37
- export async function loadFileKindAndText(filePath, options) {
38
- const pathStat = await fsStat(filePath);
39
- if (pathStat.isDirectory()) {
40
- return { kind: "directory" };
41
- }
42
- if (!pathStat.isFile()) {
43
- return {
44
- kind: "binary",
45
- description: "unsupported file type",
46
- };
47
- }
48
- if (pathStat.size > MAX_BYTES) {
49
- return {
50
- kind: "binary",
51
- description: `file exceeds ${MAX_BYTES} byte limit`
52
- };
53
- }
54
- const fileHandle = await fsOpen(filePath, "r");
55
- try {
56
- const buffer = Buffer.alloc(SNIFF_BYTES);
57
- const { bytesRead } = await fileHandle.read(buffer, 0, SNIFF_BYTES, 0);
58
- if (bytesRead === 0) {
59
- return { kind: "text", text: "" };
60
- }
61
- const sample = buffer.subarray(0, bytesRead);
62
- const textBom = detectTextBom(sample);
63
- if (textBom) {
64
- return {
65
- kind: "binary",
66
- description: `${textBom} encoded text`
67
- };
68
- }
69
- const detectedMimeType = (await fileTypeFromBuffer(sample))?.mime;
70
- if (detectedMimeType !== undefined &&
71
- !isTextType(detectedMimeType)) {
72
- if (IMG_TYPES.has(detectedMimeType)) {
73
- return { kind: "image", mimeType: detectedMimeType };
74
- }
75
- return {
76
- kind: "binary",
77
- description: detectedMimeType,
78
- };
79
- }
80
- const decoder = new TextDecoder("utf-8", { fatal: false, ignoreBOM: true });
81
- let hadUtf8DecodeErrors = false;
82
- let newlineCount = 0;
83
- const parts = [];
84
- function decodeChunk(chunk, stream) {
85
- const decoded = decoder.decode(chunk, { stream });
86
- if (!hadUtf8DecodeErrors && decoded.includes("\uFFFD")) {
87
- hadUtf8DecodeErrors = true;
88
- }
89
- if (options?.maxLines !== undefined) {
90
- for (let i = 0; i < decoded.length; i++) {
91
- if (decoded.charCodeAt(i) === 10)
92
- newlineCount++;
93
- }
94
- if (newlineCount > options.maxLines) {
95
- 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.`);
96
- }
97
- }
98
- return decoded;
99
- }
100
- parts.push(decodeChunk(sample, true));
101
- let position = bytesRead;
102
- while (true) {
103
- const { bytesRead: chunkBytesRead } = await fileHandle.read(buffer, 0, SNIFF_BYTES, position);
104
- if (chunkBytesRead === 0) {
105
- break;
106
- }
107
- const chunk = buffer.subarray(0, chunkBytesRead);
108
- parts.push(decodeChunk(chunk, true));
109
- position += chunkBytesRead;
110
- }
111
- parts.push(decodeChunk(new Uint8Array(0), false));
112
- return {
113
- kind: "text",
114
- text: parts.join(""),
115
- ...(hadUtf8DecodeErrors ? { hadUtf8DecodeErrors: true } : {}),
116
- };
117
- }
118
- finally {
119
- await fileHandle.close();
120
- }
121
- }
1
+ /**
2
+ * @deprecated — private to FileView seam. Use `from "./file-view.js"` instead.
3
+ * Thin shim re-exporting the read path now owned by FileView.
4
+ * @module dsh-better-edit/file-kind
5
+ */
6
+ export * from "./file-view.js";
@@ -1,79 +1,6 @@
1
- import { constants } from "node:fs";
2
- import { stat } from "fs/promises";
3
- import { lineHashes } from "./hashline/index.js";
4
- import { loadFileKindAndText } from "./file-kind.js";
5
- import { resolveTarget, toCwd } from "./paths.js";
6
- import { detectEnding, toLF, stripBOM } from "./edit-diff.js";
7
- import { abortIf } from "./utils.js";
8
- import { valKind, valAccess } from "./validation.js";
9
- import { visLines } from "./utils.js";
10
- function fmtSnapId(canonicalPath, info) {
11
- return `v2|${canonicalPath}|${info.ino}|${info.mtimeMs}|${info.ctimeMs}|${info.size}`;
12
- }
13
- export async function fileSnap(absolutePath) {
14
- const canonicalPath = await resolveTarget(absolutePath);
15
- const stats = await stat(canonicalPath);
16
- return {
17
- snapshotId: fmtSnapId(canonicalPath, stats),
18
- ino: stats.ino,
19
- mtimeMs: stats.mtimeMs,
20
- ctimeMs: stats.ctimeMs,
21
- size: stats.size,
22
- };
23
- }
24
1
  /**
25
- * Hashline normalization shared by every reader: strip the BOM, detect the
26
- * original line ending, normalize to LF, enforce the line ceiling, and assign
27
- * (or recall from the hash store) the per-line hash anchors.
28
- *
29
- * This is the string-level core of the read/edit pipeline; the caller owns
30
- * path resolution and raw IO (local filesystem, `ctx.fs`, or a sandbox
31
- * backend) so the same anchors are produced no matter where the file lives.
2
+ * @deprecated — private to FileView seam. Use `from "./file-view.js"` instead.
3
+ * Thin shim re-exporting the read path now owned by FileView.
4
+ * @module dsh-better-edit/file-reader
32
5
  */
33
- export async function normFromText(input) {
34
- const { absolutePath, displayPath, signal } = input;
35
- abortIf(signal);
36
- const { bom, text: rawContent } = stripBOM(input.rawText);
37
- const originalEnding = detectEnding(rawContent);
38
- const normalized = toLF(rawContent);
39
- if (input.maxLines !== undefined) {
40
- const lineCount = visLines(normalized).length;
41
- if (lineCount > input.maxLines) {
42
- 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.`);
43
- }
44
- }
45
- const fileHashes = await lineHashes(normalized, absolutePath, undefined, input.store, input.noPersist !== true);
46
- return {
47
- absolutePath,
48
- normalized,
49
- bom,
50
- originalEnding,
51
- fileHashes,
52
- hadUtf8DecodeErrors: input.hadUtf8DecodeErrors === true,
53
- };
54
- }
55
- export async function readNormFile(path, cwd, options) {
56
- const absolutePath = toCwd(path, cwd);
57
- const resolvedPath = await resolveTarget(absolutePath);
58
- const signal = options?.signal;
59
- const accessMode = options?.accessMode ?? constants.R_OK;
60
- abortIf(signal);
61
- await valAccess(resolvedPath, path, accessMode);
62
- abortIf(signal);
63
- const file = options?.preloadedFile ??
64
- (await loadFileKindAndText(resolvedPath, {
65
- maxLines: options?.maxLines,
66
- displayPath: path,
67
- }));
68
- valKind(file, path);
69
- return normFromText({
70
- absolutePath: resolvedPath,
71
- rawText: file.text,
72
- displayPath: path,
73
- signal,
74
- maxLines: options?.maxLines,
75
- store: options?.store,
76
- noPersist: options?.noPersist,
77
- hadUtf8DecodeErrors: file.hadUtf8DecodeErrors,
78
- });
79
- }
6
+ export * from "./file-view.js";