dsh-hashline-edit 0.4.3 → 0.5.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 (63) hide show
  1. package/CHANGELOG.md +345 -0
  2. package/README.md +58 -5
  3. package/cordis.patch.yml +11 -1
  4. package/grammars/go-0.25.0-9504573f352b20be.wasm +0 -0
  5. package/grammars/java-0.23.5-4fdeac4ca6ca089f.wasm +0 -0
  6. package/grammars/python-0.25.0-16108b50df4ee9a3.wasm +0 -0
  7. package/grammars/rust-0.24.2-24c89bd9252255e4.wasm +0 -0
  8. package/grammars/tsx-0.23.2-79e5da75ea62855a.wasm +0 -0
  9. package/grammars/typescript-0.23.2-778025db5a8be0e7.wasm +0 -0
  10. package/grammars/vue-0.1.0-193c01119229352f.wasm +0 -0
  11. package/lib/client.js +13 -3
  12. package/lib/index.js +2345 -75
  13. package/lib/types/dsh/codegraph/cli.d.ts +61 -0
  14. package/lib/types/dsh/codegraph/project.d.ts +27 -0
  15. package/lib/types/dsh/codegraph/tools.d.ts +36 -0
  16. package/lib/types/dsh/codegraph/view.d.ts +25 -0
  17. package/lib/types/dsh/diagnostics/cli.d.ts +76 -0
  18. package/lib/types/dsh/diagnostics/messages.d.ts +37 -0
  19. package/lib/types/dsh/diagnostics/parse.d.ts +39 -0
  20. package/lib/types/dsh/diagnostics/tools.d.ts +20 -0
  21. package/lib/types/dsh/diagnostics/view.d.ts +31 -0
  22. package/lib/types/dsh/guidance.d.ts +7 -3
  23. package/lib/types/dsh/index.d.ts +32 -3
  24. package/lib/types/dsh/syntax-health.d.ts +60 -0
  25. package/lib/types/dsh/tools/edit.d.ts +15 -1
  26. package/lib/types/dsh/tools/output.d.ts +10 -0
  27. package/lib/types/dsh/tools/read-folded.d.ts +85 -0
  28. package/lib/types/src/bundled-assets.d.ts +13 -0
  29. package/lib/types/src/domain-errors.d.ts +12 -1
  30. package/lib/types/src/edit-tool.d.ts +9 -1
  31. package/lib/types/src/mutation-engine/edit-expand.d.ts +36 -0
  32. package/lib/types/src/mutation-engine/edit-indent.d.ts +49 -0
  33. package/lib/types/src/mutation-engine/edit-request.d.ts +8 -0
  34. package/lib/types/src/mutation-engine/types.d.ts +57 -0
  35. package/lib/types/src/payload-contract.d.ts +8 -0
  36. package/lib/types/src/read-expansion/fold-skeleton.d.ts +32 -0
  37. package/lib/types/src/read-expansion/grammar-manifest.d.ts +3 -1
  38. package/lib/types/src/read-expansion/grammar-runtime.d.ts +1 -1
  39. package/lib/types/src/read-expansion/span-expansion.d.ts +26 -0
  40. package/package.json +21 -23
  41. package/prompts/codegraph-guidelines.md +8 -0
  42. package/prompts/diagnostics-guidelines.md +8 -0
  43. package/prompts/edit-guidelines.md +6 -2
  44. package/prompts/edit.md +3 -1
  45. package/prompts/read-description.md +1 -1
  46. package/prompts/read-guidelines.md +3 -1
  47. package/src/bundled-assets.ts +23 -0
  48. package/src/domain-errors.ts +52 -1
  49. package/src/edit-tool.ts +12 -1
  50. package/src/mutation-engine/edit-expand.ts +328 -0
  51. package/src/mutation-engine/edit-indent.ts +121 -0
  52. package/src/mutation-engine/edit-request.ts +8 -1
  53. package/src/mutation-engine/pipeline.ts +150 -48
  54. package/src/mutation-engine/types.ts +54 -0
  55. package/src/payload-contract.ts +165 -19
  56. package/src/read-expansion/ast-provider.ts +32 -0
  57. package/src/read-expansion/fold-skeleton.ts +56 -0
  58. package/src/read-expansion/grammar-cache.ts +33 -1
  59. package/src/read-expansion/grammar-manifest.ts +3 -1
  60. package/src/read-expansion/grammar-runtime.ts +1 -1
  61. package/src/read-expansion/qualified-names.ts +7 -0
  62. package/src/read-expansion/read-provider.ts +9 -0
  63. package/src/read-expansion/span-expansion.ts +26 -0
@@ -68,7 +68,12 @@ import { loadHashStore } from "../hash-store.js";
68
68
  import { snapshotIOFor, upsertSnapshotFor, snapshotHashFor } from "../snapshot-store";
69
69
  import { applyEdit } from "../hashline/apply.js";
70
70
  import { buildNeverServedEditHint } from "../served-session/serve-render.js";
71
- import { resEdit, type HEdit, type NEdit } from "../hashline/resolve.js";
71
+ import {
72
+ resEdit,
73
+ type EditPlacement,
74
+ type HEdit,
75
+ type LeaseSpanSource,
76
+ } from "../hashline/resolve.js";
72
77
  import { defaultHashIdentity, lineHashes } from "../hashline/hash-identity.js";
73
78
  import { HASH_LEN } from "../hashline/alphabet.js";
74
79
  import { denseServeRows, type ResolvedRange } from "../served-session/serve-render.js";
@@ -110,6 +115,14 @@ import type {
110
115
  MemberLifecycle,
111
116
  TransactionRecoveryReport,
112
117
  } from "./types.js";
118
+ import type { ItemApplyOutcome } from "./types.js";
119
+ import { applyExpandedItem } from "./edit-expand.js";
120
+ import {
121
+ applyAutoIndent,
122
+ buildIndentNote,
123
+ leadingWhitespaceOf,
124
+ type IndentSummary,
125
+ } from "./edit-indent.js";
113
126
  import {
114
127
  loadEditFile,
115
128
  loadForeignServedView,
@@ -192,34 +205,48 @@ interface ApplyOneEditInput {
192
205
  */
193
206
  currentIds?: (number | null)[];
194
207
  onRejected: (error: DomainError) => Promise<never>;
208
+ /** The item's admitted `indent` mode (ADR-0030); absent writes the payload as submitted. */
209
+ indent?: "auto";
195
210
  }
196
211
 
197
- type ApplyOneEditOutcome =
198
- | {
199
- kind: "applied";
200
- content: string;
201
- hashes: string[];
202
- removedHashes: Set<string>;
203
- range: ResolvedRange;
204
- firstChangedLine: number | undefined;
205
- lastChangedLine: number | undefined;
206
- anchorWarnings: string[] | undefined;
207
- literalBypass: boolean;
208
- neverServedCount: number;
209
- /** Span-ref move: the retired source span in pre-item coordinates. */
210
- sourceRange: ResolvedRange | undefined;
211
- /** Span-ref item: the mutated line regions the identity splice must apply. */
212
- mutationSpans: { startLine: number; endLine: number; inserted: number }[] | undefined;
213
- mutationStats: { addedLines: number; removedLines: number } | undefined;
214
- }
215
- | {
216
- kind: "noop";
217
- range: ResolvedRange;
218
- noopEdit: NEdit | undefined;
219
- anchorWarnings: string[] | undefined;
220
- literalBypass: boolean;
221
- neverServedCount: number;
222
- };
212
+ /** One item's outcome: the range path and the `expand: "item"` path return the same shape. */
213
+ type ApplyOneEditOutcome = ItemApplyOutcome;
214
+
215
+ /**
216
+ * Re-base one item's payload on the anchor line's indentation (`indent: "auto"`, ADR-0030).
217
+ *
218
+ * The anchor line is resolved through the same lease seam the apply path uses, on the same working
219
+ * buffer, so an anchor that cannot resolve rejects with the identical diagnostic before anything is
220
+ * written and a chained batch aligns against the buffer positions its anchors now name.
221
+ */
222
+ function autoIndentedEdit(
223
+ input: ApplyOneEditInput,
224
+ identity: LeaseSpanSource,
225
+ ): { edit: HEdit; indent?: IndentSummary } {
226
+ if (input.edit.content_lines.length === 0) return { edit: input.edit };
227
+ const fileLines = splitLines(input.content);
228
+ const leased = resolveLeasedEdit({
229
+ edit: {
230
+ content_lines: [],
231
+ hash_bounds: [input.edit.hash_bounds[0], input.edit.hash_bounds[0]],
232
+ },
233
+ snapshot: { fileHashes: input.hashes, fileLines, filePath: input.absolutePath },
234
+ served: input.served,
235
+ source: identity,
236
+ });
237
+ const anchorLine = leased.resolved.hash_bounds[0].line;
238
+ const baseIndent = leadingWhitespaceOf(fileLines[anchorLine - 1] ?? "");
239
+ const rebased = applyAutoIndent(input.edit.content_lines.join("\n"), baseIndent);
240
+ if (!rebased.changed) return { edit: input.edit };
241
+ return {
242
+ edit: { ...input.edit, content_lines: rebased.text.split("\n") },
243
+ indent: {
244
+ baseIndent,
245
+ strippedPrefix: rebased.strippedPrefix,
246
+ changedLines: rebased.changedLines,
247
+ },
248
+ };
249
+ }
223
250
 
224
251
  async function applyOneEdit(input: ApplyOneEditInput): Promise<ApplyOneEditOutcome> {
225
252
  abortIf(input.signal);
@@ -232,9 +259,13 @@ async function applyOneEdit(input: ApplyOneEditInput): Promise<ApplyOneEditOutco
232
259
  ...(input.currentIds !== undefined ? { currentIds: input.currentIds } : {}),
233
260
  });
234
261
 
262
+ let indent: IndentSummary | undefined;
235
263
  let anchorResult: ReturnType<typeof applyEdit>;
236
264
  try {
237
- anchorResult = applyEdit(input.content, input.edit, input.signal, input.hashes, {
265
+ const prepared =
266
+ input.indent === "auto" ? autoIndentedEdit(input, identity) : { edit: input.edit };
267
+ indent = prepared.indent;
268
+ anchorResult = applyEdit(input.content, prepared.edit, input.signal, input.hashes, {
238
269
  // WHY: read/write agreement — the verify path is literally the same
239
270
  // WHY: canonical absolute path the `hashesFor` call below uses (one field).
240
271
  filePath: input.absolutePath,
@@ -272,6 +303,7 @@ async function applyOneEdit(input: ApplyOneEditInput): Promise<ApplyOneEditOutco
272
303
  anchorWarnings,
273
304
  literalBypass,
274
305
  neverServedCount,
306
+ ...(indent !== undefined ? { indent } : {}),
275
307
  };
276
308
  }
277
309
 
@@ -308,6 +340,7 @@ async function applyOneEdit(input: ApplyOneEditInput): Promise<ApplyOneEditOutco
308
340
  sourceRange: anchorResult.sourceRange,
309
341
  mutationSpans: anchorResult.mutationSpans,
310
342
  mutationStats: anchorResult.mutationStats,
343
+ ...(indent !== undefined ? { indent } : {}),
311
344
  };
312
345
  }
313
346
 
@@ -362,6 +395,10 @@ function replacementTextForPayload(payload: DesiredContent): string {
362
395
  return `span:${payload.span.anchor_from}..${payload.span.anchor_to}:${payload.mode === "cut" ? "move" : "copy"}`;
363
396
  case "empty":
364
397
  return "";
398
+ // WHY: (ADR-0025) an insertion is identified by its side and its own lines, never by the anchor
399
+ // WHY: line it sits next to — that line is not part of this item's content.
400
+ case "insert":
401
+ return `${payload.side}:${payload.text}`;
365
402
  default:
366
403
  return assertNever(payload);
367
404
  }
@@ -394,6 +431,9 @@ function parseEdits(
394
431
  const payload = item.payload;
395
432
  let text: string;
396
433
  let source: { anchor_from: string; anchor_to: string; retire: boolean } | undefined;
434
+ // WHY: (ADR-0025) an insertion sets this instead of `item.at`; both feed the same
435
+ // WHY: zero-width splice, and `item.at` stays the placement of a resolved RANGE.
436
+ let placement: EditPlacement | undefined;
397
437
  switch (payload.kind) {
398
438
  case "literal":
399
439
  // WHY: (remediation-2 B4) the min-line guard must sit where the destructive behaviour is
@@ -441,6 +481,14 @@ function parseEdits(
441
481
  }
442
482
  text = "";
443
483
  break;
484
+ // WHY: (ADR-0025) a pure insertion reuses the engine's zero-width placement splice: the
485
+ // WHY: payload lines ARE the content and the anchor line is only the zero-width boundary.
486
+ // WHY: Because `content_lines` never contains the anchor line, no splice path can rewrite
487
+ // WHY: it — the "insert turned into a re-typed boundary row" failure is not representable.
488
+ case "insert":
489
+ text = payload.text;
490
+ placement = payload.side;
491
+ break;
444
492
  default:
445
493
  assertNever(payload);
446
494
  }
@@ -450,6 +498,7 @@ function parseEdits(
450
498
  anchor_to: item.target.anchor_to,
451
499
  text,
452
500
  ...(item.at !== "in-place" ? { placement: item.at } : {}),
501
+ ...(placement !== undefined ? { placement } : {}),
453
502
  ...(source ? { source } : {}),
454
503
  }),
455
504
  });
@@ -792,6 +841,7 @@ async function runMutations(
792
841
  hadUtf8DecodeErrors,
793
842
  absolutePath,
794
843
  served,
844
+ canonDigests,
795
845
  } = await loadEditFile({
796
846
  path,
797
847
  cwd,
@@ -912,28 +962,80 @@ async function runMutations(
912
962
  }
913
963
  const edit = parsedItem.edit;
914
964
 
915
- const outcome = await applyOneEdit({
916
- content: currentContent,
917
- hashes: currentHashes,
918
- edit,
919
- ...(options?.signal !== undefined ? { signal: options.signal } : {}),
920
- served,
921
- blockedHashes: batchBlockedHashes,
922
- canonDigests: baseCanonDigests,
923
- sessionKey,
924
- absolutePath,
925
- store: hashStore,
926
- isPreview,
927
- mode,
928
- // WHY: the intermediate buffer is in-memory only, so its identities come from the buffer map
929
- // WHY: (`null` lines are the batch's own creations) — a re-diff of it against S_latest cannot
930
- // WHY: tell which of two byte-identical lines carries a leased line_id.
931
- ...(isPreview ? {} : { currentIds }),
932
- onRejected: async (error) => {
965
+ let outcome: ApplyOneEditOutcome;
966
+ if (item.expand === "item") {
967
+ // WHY: (ADR-0031) an expanded item is applied by its own seam: the AST decides the span, the
968
+ // WHY: licensing anchor still resolves through the leases, and the outcome re-enters this loop so
969
+ // WHY: identity bookkeeping, counting, drift, undo, commit and serve stay shared.
970
+ try {
971
+ outcome = await applyExpandedItem({
972
+ edit,
973
+ ...(item.indent !== undefined ? { indent: item.indent } : {}),
974
+ content: currentContent,
975
+ hashes: currentHashes,
976
+ served,
977
+ canonDigests,
978
+ identity: leaseSpanSource({
979
+ store: hashStore,
980
+ sessionKey,
981
+ absolutePath,
982
+ content: currentContent,
983
+ ...(isPreview ? {} : { currentIds }),
984
+ }),
985
+ absolutePath,
986
+ filePath: path,
987
+ mode,
988
+ sessionKey,
989
+ store: hashStore,
990
+ blockedHashes: batchBlockedHashes,
991
+ provider: options?.spanProvider,
992
+ ...(options?.signal !== undefined ? { signal: options.signal } : {}),
993
+ });
994
+ } catch (error) {
995
+ if (!(error instanceof DomainError)) throw error;
996
+ await recordRejectionServe({
997
+ error,
998
+ sessionKey,
999
+ absolutePath,
1000
+ isPreview,
1001
+ lineCount: currentHashes.length,
1002
+ contentHash: isPreview ? undefined : snapshotHashFor(currentContent),
1003
+ });
933
1004
  if (items.length === 1) throw error;
934
1005
  throw batchAbortFor({ error, index: number, path });
935
- },
936
- });
1006
+ }
1007
+ } else {
1008
+ outcome = await applyOneEdit({
1009
+ content: currentContent,
1010
+ hashes: currentHashes,
1011
+ edit,
1012
+ ...(options?.signal !== undefined ? { signal: options.signal } : {}),
1013
+ served,
1014
+ blockedHashes: batchBlockedHashes,
1015
+ canonDigests: baseCanonDigests,
1016
+ sessionKey,
1017
+ absolutePath,
1018
+ store: hashStore,
1019
+ isPreview,
1020
+ mode,
1021
+ ...(item.indent !== undefined ? { indent: item.indent } : {}),
1022
+ // WHY: the intermediate buffer is in-memory only, so its identities come from the buffer map
1023
+ // WHY: (`null` lines are the batch's own creations) — a re-diff of it against S_latest cannot
1024
+ // WHY: tell which of two byte-identical lines carries a leased line_id.
1025
+ ...(isPreview ? {} : { currentIds }),
1026
+ onRejected: async (error) => {
1027
+ if (items.length === 1) throw error;
1028
+ throw batchAbortFor({ error, index: number, path });
1029
+ },
1030
+ });
1031
+ }
1032
+
1033
+ // WHY: (ADR-0030) the applied re-indentation is reported through the warnings channel: the diff
1034
+ // WHY: shows the bytes but not that `indent: "auto"` is what produced them, and the model must not
1035
+ // WHY: have to re-read to learn the depth it landed at.
1036
+ if (outcome.kind === "applied" && outcome.indent !== undefined) {
1037
+ warnings.push(buildIndentNote({ ref: `edit[${number}] (${path})`, summary: outcome.indent }));
1038
+ }
937
1039
 
938
1040
  const range = outcome.range;
939
1041
  // WHY: (ticket-02) a move's retired source is an edited interval too: the drift scan must
@@ -8,6 +8,8 @@
8
8
  import type { LineEnding } from "../edit-diff.js";
9
9
  import type { ResolvedRange } from "../served-session/serve-render.js";
10
10
  import type { HashStore } from "../hash-store.js";
11
+ import type { SpanProvider } from "../read-expansion/span-expansion.js";
12
+ import type { IndentSummary } from "./edit-indent.js";
11
13
 
12
14
  // WHY: Re-export pipeline-facing options — validated once at admission (edit.ts),
13
15
  // WHY: trusted inside the engine. No `any`.
@@ -40,6 +42,11 @@ export interface PipelineOptions {
40
42
  * destination content is rewritten, so the rollback of that in-flight member is exercised.
41
43
  */
42
44
  onAfterMoveRename?: (toAbsolutePath: string) => void | Promise<void>;
45
+ /**
46
+ * Override seam for `expand: "item"` (ADR-0031): the AST span provider the expansion reads. Absent
47
+ * uses the lazily created AST provider, so a caller that never expands a node wires nothing.
48
+ */
49
+ spanProvider?: SpanProvider;
43
50
  }
44
51
 
45
52
  /**
@@ -135,3 +142,50 @@ export interface ProcessedEditFile {
135
142
  */
136
143
  lifecycle?: MemberLifecycle;
137
144
  }
145
+
146
+ /** Where one item's replacement landed, in pre-item coordinates. */
147
+ interface ItemMutationSpan {
148
+ startLine: number;
149
+ endLine: number;
150
+ inserted: number;
151
+ }
152
+
153
+ /**
154
+ * The outcome of applying ONE edit item. Both the range path (`applyOneEdit`) and the
155
+ * `expand: "item"` path (`applyExpandedItem`) return it, so the mutate loop's identity bookkeeping,
156
+ * counting, drift scan and commit are shared rather than forked per payload shape.
157
+ */
158
+ interface AppliedItemOutcome {
159
+ kind: "applied";
160
+ content: string;
161
+ hashes: string[];
162
+ removedHashes: Set<string>;
163
+ range: ResolvedRange;
164
+ firstChangedLine: number | undefined;
165
+ lastChangedLine: number | undefined;
166
+ anchorWarnings: string[] | undefined;
167
+ literalBypass: boolean;
168
+ neverServedCount: number;
169
+ /** A span-ref move's retired source span, in pre-item coordinates. */
170
+ sourceRange: ResolvedRange | undefined;
171
+ /** Every line region the item mutated; the identity map splices exactly these. */
172
+ mutationSpans: ItemMutationSpan[] | undefined;
173
+ /** Honest per-item line counts for a span-ref item. */
174
+ mutationStats: { addedLines: number; removedLines: number } | undefined;
175
+ /** The applied auto-indentation, when `indent: "auto"` changed the payload. */
176
+ indent?: IndentSummary;
177
+ }
178
+
179
+ /** The outcome of an item whose replacement left the bytes unchanged. */
180
+ interface NoopItemOutcome {
181
+ kind: "noop";
182
+ range: ResolvedRange;
183
+ noopEdit: { loc: string; currentContent: string } | undefined;
184
+ anchorWarnings: string[] | undefined;
185
+ literalBypass: boolean;
186
+ neverServedCount: number;
187
+ indent?: IndentSummary;
188
+ }
189
+
190
+ /** One item's outcome: applied, or a noop that wrote nothing. */
191
+ export type ItemApplyOutcome = AppliedItemOutcome | NoopItemOutcome;