dsh-hashline-edit 0.5.2 → 0.5.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.
@@ -1,4 +1,5 @@
1
1
  import { ANCHOR_LEN, ALPHA_RE, HASH_CLASS } from "./hash-identity.js";
2
+ import { stripLineGutter } from "./gutter.js";
2
3
  import { DomainError } from "../domain-errors.js";
3
4
 
4
5
  export type Anchor = { hash: string };
@@ -22,8 +23,8 @@ function diagPayload(ref: string): { rawAnchor: string; reason: string } {
22
23
 
23
24
  if (trimmed.includes("│") && trimmed.includes("\n")) {
24
25
  const lines = trimmed.split("\n");
25
- const first = lines[0] ?? "";
26
- const last = lines.at(-1) ?? "";
26
+ const first = stripLineGutter(lines[0] ?? "");
27
+ const last = stripLineGutter(lines.at(-1) ?? "");
27
28
  // SAFETY: HASH_CLASS is derived from the trusted alphabet at the configured width, bounded linear search — no user-controlled pattern, no ReDoS.
28
29
  const hashRe = new RegExp(HASH_CLASS);
29
30
  const firstMatch = first.match(hashRe);
@@ -1,6 +1,7 @@
1
1
  import { abortIf, rejectUnknownFields } from "../utils.js";
2
2
  import { DomainError, formatWarning } from "../domain-errors.js";
3
3
  import { HASH_CLASS, HASH_LEN } from "./hash-identity.js";
4
+ import { ROW_GUTTER_PREFIX, stripLineGutter } from "./gutter.js";
4
5
  import { parseHashRef, parseText, type Anchor } from "./parse.js";
5
6
  import type { ServedRow } from "../served-session/serve-render.js";
6
7
  import type { FileSnapshotContext } from "../served-session/serve-render.js";
@@ -410,14 +411,15 @@ function assertItem(edit: Record<string, unknown>): void {
410
411
  }
411
412
 
412
413
  // SAFETY: HASH_CLASS is derived from the trusted alphabet at the configured width, linear row prefix — bounded, no user input, no ReDoS.
413
- const ANCHOR_ROW_RE = new RegExp(`^([+-]?)(${HASH_CLASS})│`);
414
+ const ANCHOR_ROW_RE = new RegExp(`^([+-]?)(?:${ROW_GUTTER_PREFIX})?(${HASH_CLASS})│`);
414
415
  function firstHashFromBlock(block: string): string | undefined {
415
416
  for (const line of block.split("\n")) {
416
417
  const m = line.match(ANCHOR_ROW_RE);
417
418
  if (m) return m[2]!;
418
- // SAFETY: HASH_CLASS is derived from the trusted alphabet at the configured width, bounded linear search — no user-controlled pattern, no ReDoS.
419
- const bare = line.match(new RegExp(HASH_CLASS));
420
- if (bare) return bare[0]!;
419
+ // WHY: strip the display-only gutter and reject an all-digit run (a line number, never a served
420
+ // WHY: anchor) so a pasted `12: abcd│…` resolves to `abcd`, not the leading digits.
421
+ const bare = stripLineGutter(line).match(new RegExp(HASH_CLASS));
422
+ if (bare && !/^\d+$/.test(bare[0]!)) return bare[0]!;
421
423
  }
422
424
  return undefined;
423
425
  }
@@ -449,7 +451,7 @@ export function resEdit(edit: HTEdit): HEdit {
449
451
  } else if (match[1] === "-") {
450
452
  reason = `anchor carries a leading "-" marker ("${trimmed}"). Nothing was written; pass the bare ${HASH_LEN}-char anchor and retry.`;
451
453
  } else {
452
- reason = `anchor carries a "HASH│" prefix ("${trimmed}"). Nothing was written; copy only the ${HASH_LEN} chars before │ and retry.`;
454
+ reason = `anchor carries a served-row prefix ("${trimmed}"). Nothing was written; copy only the ${HASH_LEN}-char anchor immediately left of │ and retry.`;
453
455
  }
454
456
  throw new DomainError("E_MALFORMED_ANCHOR", { rawAnchor: trimmed, reason });
455
457
  }
@@ -0,0 +1,43 @@
1
+ /**
2
+ * The active line-number gutter mode, shared by every renderer and by the anchor codebook.
3
+ *
4
+ * WHY module state + a setter rather than a load-time constant: the mode is a live plugin setting
5
+ * (the `nativeGutter` Volatile switch). The host re-mounts agents on a volatile update, and the plugin
6
+ * calls `applyConfigRowGutter()` then `configureAnchorSpace()` so the codebook, capacity and anchor
7
+ * generation all follow the switch. Changing it invalidates old anchors (new generation) — the caller
8
+ * opens a fresh session. `PI_BETTER_EDIT_ROW_GUTTER` pins the mode for tests/CI and wins over config.
9
+ */
10
+ export type RowGutter = "arrow" | "native";
11
+
12
+ export const ROW_GUTTER_ENV_VAR = "PI_BETTER_EDIT_ROW_GUTTER";
13
+
14
+ /** Arrow is the default: it does not merge with the following anchor token. */
15
+ export const ROW_GUTTER_DEFAULT: RowGutter = "arrow";
16
+
17
+ export function resolveRowGutter(env: Record<string, string | undefined>): RowGutter {
18
+ const raw = env[ROW_GUTTER_ENV_VAR];
19
+ if (raw === undefined || raw.trim() === "") return ROW_GUTTER_DEFAULT;
20
+ const value = raw.trim().toLowerCase();
21
+ if (value === "arrow") return "arrow";
22
+ if (value === "native") return "native";
23
+ throw new Error(
24
+ `${ROW_GUTTER_ENV_VAR} must be "arrow" or "native" (got ${JSON.stringify(raw)}).`,
25
+ );
26
+ }
27
+
28
+ const envPinned = (() => {
29
+ const raw = process.env[ROW_GUTTER_ENV_VAR];
30
+ return raw !== undefined && raw.trim() !== "";
31
+ })();
32
+
33
+ let current: RowGutter = resolveRowGutter(process.env);
34
+
35
+ export function getRowGutter(): RowGutter {
36
+ return current;
37
+ }
38
+
39
+ /** Apply a config-driven mode unless the env var is set (tests/CI pin it). */
40
+ export function applyConfigRowGutter(mode: RowGutter): RowGutter {
41
+ if (!envPinned) current = mode;
42
+ return current;
43
+ }
package/src/prompts.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { readFileSync } from "node:fs";
2
2
 
3
3
  import { HASH_LEN } from "./hashline/alphabet.js";
4
+ import { getRowGutter } from "./hashline/row-mode.js";
4
5
 
5
6
  /**
6
7
  * Resolve template variables in shipped prompt prose. Beyond caller-supplied replacements, the
@@ -14,7 +15,8 @@ function applyReplacements(content: string, replacements?: Record<string, string
14
15
  out = out.split(`{{${key}}}`).join(value);
15
16
  }
16
17
  }
17
- return out.split("{{HASH_LEN}}").join(String(HASH_LEN));
18
+ const withWidth = out.split("{{HASH_LEN}}").join(String(HASH_LEN));
19
+ return withWidth.split("{{GUTTER}}").join(getRowGutter() === "native" ? ": " : "→");
18
20
  }
19
21
 
20
22
  export function loadP(relativePath: string, replacements?: Record<string, string>): string {
@@ -30,6 +30,7 @@
30
30
  */
31
31
  import { HASH_LEN, HASH_RE } from "../hashline/alphabet.js";
32
32
  import { HASH_SEP } from "../hashline/hash-identity.js";
33
+ import { lineGutter as gutterLine, stripLineGutter } from "../hashline/gutter.js";
33
34
  import { canonDigest } from "../hashline/hash-identity.js";
34
35
  import { SERVED_ROWS_CAP } from "../constants.js";
35
36
  import {
@@ -84,8 +85,22 @@ export function denseServeRows(hashes: readonly string[]): ServedRow[] {
84
85
  return hashes.map((hash, position) => ({ position, hash }));
85
86
  }
86
87
 
88
+ export function lineGutter(lineNumber: number): string {
89
+ return gutterLine(lineNumber);
90
+ }
91
+
92
+ /**
93
+ * One served row: `12: abc│content`. WHY: line numbers are display-only, never part of the anchor —
94
+ * the anchor stays the HASH immediately left of the separator.
95
+ */
96
+ export function formatServedRow(lineNumber: number, hash: string, content: string): string {
97
+ return `${lineGutter(lineNumber)}${hash}${HASH_SEP}${content}`;
98
+ }
99
+
87
100
  export function fmtServedRows(rows: ServedRow[], fileLines: string[]): string {
88
- return rows.map((row) => `${row.hash}${HASH_SEP}${fileLines[row.position] ?? ""}`).join("\n");
101
+ return rows
102
+ .map((row) => formatServedRow(row.position + 1, row.hash, fileLines[row.position] ?? ""))
103
+ .join("\n");
89
104
  }
90
105
 
91
106
  // WHY: the one continuation a rejected edit offers (read-chrome): the `windows` offset, not a bare
@@ -448,6 +463,8 @@ function anchorShapeFromLine(line: string): { anchor: string; tail: string } | u
448
463
  if (text.length > 0 && (text[0] === "+" || text[0] === "-" || text[0] === " ")) {
449
464
  text = text.slice(1);
450
465
  }
466
+ // WHY: a served row may carry the display-only line-number gutter (`12: `); it is not part of the anchor.
467
+ text = stripLineGutter(text);
451
468
  if (text.length < HASH_LEN + 1) return undefined;
452
469
  if (text[HASH_LEN] !== HASH_SEP) return undefined;
453
470
  const anchor = text.slice(0, HASH_LEN);