@dzhechkov/harness-core 0.8.41 → 0.8.43

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 (48) hide show
  1. package/.dz-manifest.json +92 -32
  2. package/README.md +86 -2
  3. package/dist/architecture.d.ts.map +1 -1
  4. package/dist/architecture.js +0 -0
  5. package/dist/architecture.js.map +1 -1
  6. package/dist/guard.d.ts +4 -0
  7. package/dist/guard.d.ts.map +1 -1
  8. package/dist/guard.js +60 -3
  9. package/dist/guard.js.map +1 -1
  10. package/dist/index.d.ts +6 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +3 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/memory-index-check.d.ts +26 -0
  15. package/dist/memory-index-check.d.ts.map +1 -0
  16. package/dist/memory-index-check.js +75 -0
  17. package/dist/memory-index-check.js.map +1 -0
  18. package/dist/publish.d.ts +14 -0
  19. package/dist/publish.d.ts.map +1 -1
  20. package/dist/publish.js +61 -44
  21. package/dist/publish.js.map +1 -1
  22. package/dist/release-line.d.ts +15 -1
  23. package/dist/release-line.d.ts.map +1 -1
  24. package/dist/release-line.js +40 -13
  25. package/dist/release-line.js.map +1 -1
  26. package/dist/session-retro.d.ts +8 -5
  27. package/dist/session-retro.d.ts.map +1 -1
  28. package/dist/session-retro.js +36 -11
  29. package/dist/session-retro.js.map +1 -1
  30. package/dist/text-mangling.d.ts +10 -0
  31. package/dist/text-mangling.d.ts.map +1 -0
  32. package/dist/text-mangling.js +23 -0
  33. package/dist/text-mangling.js.map +1 -0
  34. package/dist/tree-sync.d.ts +5 -0
  35. package/dist/tree-sync.d.ts.map +1 -0
  36. package/dist/tree-sync.js +13 -0
  37. package/dist/tree-sync.js.map +1 -0
  38. package/package.json +8 -8
  39. package/sbom.json +181 -31
  40. package/src/architecture.ts +0 -0
  41. package/src/guard.ts +62 -3
  42. package/src/index.ts +11 -1
  43. package/src/memory-index-check.ts +80 -0
  44. package/src/publish.ts +67 -43
  45. package/src/release-line.ts +60 -16
  46. package/src/session-retro.ts +51 -15
  47. package/src/text-mangling.ts +29 -0
  48. package/src/tree-sync.ts +17 -0
package/src/publish.ts CHANGED
@@ -22,7 +22,7 @@ type ExecSyncOptionsWithStringEncoding = NonNullable<Parameters<typeof execSync>
22
22
  import { createHash } from 'node:crypto';
23
23
 
24
24
  import { claimCheck } from './claim-check.js';
25
- import { rewriteReleaseLine, isReleaseLineToken } from './release-line.js';
25
+ import { findReleaseLine, rewriteReleaseLine, isReleaseLineToken, shortPackageName } from './release-line.js';
26
26
  import { packedTarballName } from './packed-install-smoke.js';
27
27
 
28
28
  export type ProbeOutcome = {
@@ -147,6 +147,13 @@ export interface MirrorEpilogue {
147
147
  readonly warning?: string | undefined;
148
148
  }
149
149
 
150
+ export interface ReleaseLineReport {
151
+ readonly path: string; // 'README.md' | 'packages/@dzhechkov/harness-cli/README.md'
152
+ readonly rewritten: readonly { readonly name: string; readonly from: string; readonly to: string }[];
153
+ readonly kept: readonly { readonly name: string; readonly version: string }[];
154
+ readonly wrapped: boolean;
155
+ }
156
+
150
157
  /** Full publish report. */
151
158
  export interface PublishReport {
152
159
  readonly packages: readonly PublishResult[];
@@ -156,6 +163,7 @@ export interface PublishReport {
156
163
  readonly dryRun: boolean;
157
164
  /** Repo-relative README paths whose first joint core/CLI release line was rewritten. */
158
165
  readonly releaseLineSynced: readonly string[];
166
+ readonly releaseLineReport: readonly ReleaseLineReport[];
159
167
  /** Post-publication sync failures are warnings: registry-confirmed packages cannot be unpublished. */
160
168
  readonly warnings?: readonly string[] | undefined;
161
169
  /** CLI-owned command-hook outcome after a live publish; no transport logic lives in core. */
@@ -1510,52 +1518,67 @@ export function publishPackages(
1510
1518
  }
1511
1519
 
1512
1520
  const releaseLineSynced: string[] = [];
1521
+ const releaseLineReport: ReleaseLineReport[] = [];
1513
1522
  const warnings: string[] = [];
1514
- const releasePackageNames = new Set(['@dzhechkov/harness-core', '@dzhechkov/harness-cli']);
1515
- const releasePackagePublished = results.some(
1516
- (result) => result.status === 'published' && releasePackageNames.has(result.name),
1517
- );
1518
- if (releasePackagePublished && opts.dryRun !== true && opts.bumpOnly !== true) {
1519
- const currentVersion = (name: string): string | null => {
1520
- const landed = results.find((result) => result.name === name && result.status === 'published');
1521
- if (landed !== undefined) return landed.newVersion;
1522
- const pkg = packages.find((candidate) => candidate.name === name);
1523
- if (pkg === undefined) return null;
1524
- try {
1525
- const parsed = JSON.parse(readFileSync(pathJoin(pkg.dir, 'package.json'), 'utf8')) as { version?: unknown };
1526
- return typeof parsed.version === 'string' ? parsed.version : null;
1527
- } catch {
1528
- return null;
1523
+ const versions: Record<string, string> = {};
1524
+ for (const result of results.filter((result) => result.status === 'published')) {
1525
+ versions[shortPackageName(result.name)] = result.newVersion;
1526
+ }
1527
+ // Dry-run marks would-publish packages skipped, but records successful planning in landedInBatch.
1528
+ const planned: Record<string, string> = opts.dryRun === true ? {} : versions;
1529
+ if (opts.dryRun === true) {
1530
+ for (const result of results) {
1531
+ if (landedInBatch.has(result.name) && result.error === undefined) {
1532
+ planned[shortPackageName(result.name)] = result.newVersion;
1529
1533
  }
1530
- };
1531
- const coreVersion = currentVersion('@dzhechkov/harness-core');
1532
- const cliVersion = currentVersion('@dzhechkov/harness-cli');
1533
- if (coreVersion === null || cliVersion === null) {
1534
- warnings.push('release-line sync skipped: could not read both harness-core and harness-cli package versions');
1535
- } else {
1536
- const readmes = [
1537
- { path: 'README.md', absolute: pathJoin(monorepoRoot, 'README.md') },
1538
- {
1539
- path: 'packages/@dzhechkov/harness-cli/README.md',
1540
- absolute: pathJoin(monorepoRoot, 'packages', '@dzhechkov', 'harness-cli', 'README.md'),
1541
- },
1542
- ] as const;
1543
- for (const readme of readmes) {
1544
- try {
1545
- const original = readFileSync(readme.absolute, 'utf8');
1546
- const updated = rewriteReleaseLine(original, coreVersion, cliVersion);
1547
- if (updated === null) {
1548
- warnings.push(`release-line sync skipped ${readme.path}: release line not found`);
1549
- continue;
1534
+ }
1535
+ }
1536
+ const syncReleaseLine = Object.keys(versions).length > 0 && opts.dryRun !== true && opts.bumpOnly !== true;
1537
+ if (Object.keys(planned).length > 0 && opts.bumpOnly !== true) {
1538
+ const readmes = [
1539
+ { path: 'README.md', absolute: pathJoin(monorepoRoot, 'README.md') },
1540
+ {
1541
+ path: 'packages/@dzhechkov/harness-cli/README.md',
1542
+ absolute: pathJoin(monorepoRoot, 'packages', '@dzhechkov', 'harness-cli', 'README.md'),
1543
+ },
1544
+ ] as const;
1545
+ for (const readme of readmes) {
1546
+ try {
1547
+ const original = readFileSync(readme.absolute, 'utf8');
1548
+ const found = findReleaseLine(original);
1549
+ if (found === null) {
1550
+ // A repo without a joint release line is the NORMAL case for every consumer monorepo —
1551
+ // only a run that was actually about to WRITE has something to report here. The report-only
1552
+ // pass (dry-run) records an empty plan and says nothing, which is what keeps `dz publish`'s
1553
+ // output free of the word the FR-11 proxy watches for (cli.test.ts, "independent of the
1554
+ // release feature"). MEASURED 2026-09-22: widening the gate without this made every dry-run
1555
+ // in a line-less repo print a release-line warning.
1556
+ if (syncReleaseLine) warnings.push(`release-line sync skipped ${readme.path}: release line not found`);
1557
+ continue;
1558
+ }
1559
+ const rewritten: { name: string; from: string; to: string }[] = [];
1560
+ const kept: { name: string; version: string }[] = [];
1561
+ for (const token of found.tokens) {
1562
+ if (Object.hasOwn(planned, token.name) && planned[token.name] !== token.version) {
1563
+ rewritten.push({ name: token.name, from: token.version, to: planned[token.name]! });
1564
+ } else {
1565
+ kept.push({ name: token.name, version: token.version });
1550
1566
  }
1551
- if (updated === original) continue;
1552
- const tmp = readme.absolute + '.sync-tmp';
1553
- writeFileSync(tmp, updated);
1554
- renameSync(tmp, readme.absolute);
1555
- releaseLineSynced.push(readme.path);
1556
- } catch (error) {
1557
- warnings.push(`release-line sync failed ${readme.path}: ${error instanceof Error ? error.message : String(error)}`);
1558
1567
  }
1568
+ releaseLineReport.push({ path: readme.path, rewritten, kept, wrapped: found.wrapped });
1569
+ const updated = rewriteReleaseLine(original, planned);
1570
+ if (!syncReleaseLine || updated === null || updated === original) continue;
1571
+ const tmp = readme.absolute + '.sync-tmp';
1572
+ writeFileSync(tmp, updated);
1573
+ renameSync(tmp, readme.absolute);
1574
+ releaseLineSynced.push(readme.path);
1575
+ } catch (error) {
1576
+ // Same rule as the not-found branch above: only the pass that was actually going to WRITE
1577
+ // reports. A README that does not exist at all is the normal state of a consumer monorepo,
1578
+ // and a report-only pass must not turn that into a warning naming the release line
1579
+ // (FR-11 proxy, harness-cli/test/cli.test.ts — MEASURED 2026-09-22: ENOENT on both READMEs
1580
+ // of a tmp fixture made every dry-run print two release-line warnings).
1581
+ if (syncReleaseLine) warnings.push(`release-line sync failed ${readme.path}: ${error instanceof Error ? error.message : String(error)}`);
1559
1582
  }
1560
1583
  }
1561
1584
  }
@@ -1577,6 +1600,7 @@ export function publishPackages(
1577
1600
  errors: results.filter((r) => r.status === 'error').length,
1578
1601
  dryRun: opts.dryRun === true,
1579
1602
  releaseLineSynced,
1603
+ releaseLineReport,
1580
1604
  ...(warnings.length > 0 ? { warnings } : {}),
1581
1605
  };
1582
1606
  }
@@ -1,6 +1,19 @@
1
1
  export const RELEASE_LINE_RE = /`harness-core v(\d+\.\d+\.\d+)` · `harness-cli v(\d+\.\d+\.\d+)`/;
2
2
 
3
- export interface ReleaseLineMatch {
3
+ export interface ReleaseLineToken {
4
+ readonly name: string;
5
+ readonly version: string;
6
+ readonly start: number;
7
+ readonly end: number;
8
+ }
9
+
10
+ export interface ReleaseLineParse {
11
+ readonly tokens: readonly ReleaseLineToken[];
12
+ readonly chainEnd: number;
13
+ readonly wrapped: boolean;
14
+ }
15
+
16
+ export interface ReleaseLineMatch extends ReleaseLineParse {
4
17
  readonly index: number;
5
18
  readonly line: string;
6
19
  readonly core: string;
@@ -11,25 +24,43 @@ export function findReleaseLine(text: string): ReleaseLineMatch | null {
11
24
  const lines = text.split('\n');
12
25
  for (let index = 0; index < lines.length; index++) {
13
26
  const line = lines[index]!;
14
- const match = RELEASE_LINE_RE.exec(line);
15
- if (match?.[1] !== undefined && match[2] !== undefined) {
16
- return { index, line, core: match[1], cli: match[2] };
27
+ const parsed = parseReleaseLine(line);
28
+ if (parsed !== null) {
29
+ return { index, line, core: parsed.tokens[0]!.version, cli: parsed.tokens[1]!.version, ...parsed };
17
30
  }
18
31
  }
19
32
  return null;
20
33
  }
21
34
 
22
- export function rewriteReleaseLine(text: string, core: string, cli: string): string | null {
35
+ export function rewriteReleaseLine(text: string, versions: Readonly<Record<string, string>>): string | null;
36
+ export function rewriteReleaseLine(text: string, core: string, cli: string): string | null;
37
+ export function rewriteReleaseLine(
38
+ text: string,
39
+ versionsOrCore: Readonly<Record<string, string>> | string,
40
+ cli?: string,
41
+ ): string | null {
42
+ const versions: Readonly<Record<string, string>> = typeof arguments[1] === 'string'
43
+ ? { 'harness-core': versionsOrCore as string, 'harness-cli': cli! }
44
+ : versionsOrCore as Readonly<Record<string, string>>;
23
45
  const found = findReleaseLine(text);
24
46
  if (found === null) return null;
47
+ let line = found.line;
48
+ // Tokens are ordered by start; work from the right so length changes preserve earlier offsets.
49
+ for (let index = found.tokens.length - 1; index >= 0; index--) {
50
+ const token = found.tokens[index]!;
51
+ if (!Object.hasOwn(versions, token.name)) continue;
52
+ const versionStart = token.end - 1 - token.version.length;
53
+ line = line.slice(0, versionStart) + versions[token.name]! + line.slice(token.end - 1);
54
+ }
25
55
  const lines = text.split('\n');
26
- lines[found.index] = found.line.replace(
27
- RELEASE_LINE_RE,
28
- `\`harness-core v${core}\` · \`harness-cli v${cli}\``,
29
- );
56
+ lines[found.index] = line;
30
57
  return lines.join('\n');
31
58
  }
32
59
 
60
+ export function shortPackageName(name: string): string {
61
+ return name.replace(/^@[^/]+\//, '');
62
+ }
63
+
33
64
  /**
34
65
  * A generic RELEASE-LINE token: a backtick-quoted `<pkg-short-name> vX` pair, anywhere on a line —
35
66
  * the shape `RELEASE_LINE_RE` names for the joint `harness-core`/`harness-cli` pair, generalised to
@@ -40,6 +71,24 @@ export function rewriteReleaseLine(text: string, core: string, cli: string): str
40
71
  */
41
72
  export const GENERIC_RELEASE_TOKEN_RE = /`[a-z][a-z0-9-]*\s+v\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.]+)?`/;
42
73
 
74
+ export function parseReleaseLine(line: string): ReleaseLineParse | null {
75
+ const joint = RELEASE_LINE_RE.exec(line);
76
+ if (joint === null) return null;
77
+ // Keep the original sticky chain boundary: prose ends the release line's permission.
78
+ const chainTail = new RegExp(`(?:\\s*·\\s*${GENERIC_RELEASE_TOKEN_RE.source})*`, 'y');
79
+ chainTail.lastIndex = joint.index + joint[0].length;
80
+ const tail = chainTail.exec(line);
81
+ const chainEnd = joint.index + joint[0].length + (tail?.[0].length ?? 0);
82
+ const tokens: ReleaseLineToken[] = [];
83
+ const chain = line.slice(joint.index, chainEnd);
84
+ for (const match of chain.matchAll(new RegExp(GENERIC_RELEASE_TOKEN_RE.source, 'g'))) {
85
+ const [name, version] = match[0].slice(1, -1).split(/\s+v/);
86
+ const start = joint.index + match.index;
87
+ tokens.push({ name: name!, version: version!, start, end: start + match[0].length });
88
+ }
89
+ return { tokens, chainEnd, wrapped: /\s*·\s*$/.test(line.slice(chainEnd)) };
90
+ }
91
+
43
92
  /**
44
93
  * Is the OLD-VERSION occurrence at `[start, end)` in `line` sitting inside a `` `<name> vX` ``
45
94
  * backtick token? A POSITIVE override for `planReadmeVersionSync`'s citation heuristic: a token
@@ -53,11 +102,6 @@ export function isReleaseLineToken(line: string, start: number, end: number): bo
53
102
  // Codex r3 HIGH (lead): the permission is the joint pair PLUS the CONTIGUOUS ` · `<name> vX``
54
103
  // chain that follows it — not the whole line. `` `harness-core vX` · `harness-cli vY` — historically
55
104
  // `memory vZ` `` moves the first two and keeps the third (prose broke the chain).
56
- const joint = RELEASE_LINE_RE.exec(line);
57
- if (joint === null) return false;
58
- const chainTail = new RegExp(`(?:\\s*·\\s*${GENERIC_RELEASE_TOKEN_RE.source})*`, 'y');
59
- chainTail.lastIndex = joint.index + joint[0].length;
60
- const tail = chainTail.exec(line);
61
- const chainEnd = joint.index + joint[0].length + (tail?.[0].length ?? 0);
62
- return joint.index <= start && end <= chainEnd;
105
+ const p = parseReleaseLine(line);
106
+ return p !== null && p.tokens[0]!.start <= start && end <= p.chainEnd;
63
107
  }
@@ -547,6 +547,14 @@ export const RETRO_PENDING_FILE = 'retro-pending.json';
547
547
  const MAX_TAIL_SCAN_BYTES = 8 * 1024 * 1024;
548
548
  /** Without a session id to compare, a sentinel older than this is stale (fallback freshness only). */
549
549
  const SENTINEL_FRESH_MS = 30 * 60 * 1000;
550
+ const MAX_AWAITING_TEACHES = 16;
551
+
552
+ export interface AdmissionDebt {
553
+ readonly snippet: string;
554
+ /** Paired call ids issued against THIS debt, in registration order (oldest first).
555
+ * Absent, rather than an empty array, when no calls await confirmation. */
556
+ readonly awaiting?: readonly string[];
557
+ }
550
558
 
551
559
  export interface RetroPendingSentinel {
552
560
  readonly schema: 1;
@@ -554,6 +562,7 @@ export interface RetroPendingSentinel {
554
562
  readonly transcript: string; // absolute transcript path — the debt belongs to THIS session
555
563
  readonly snippet: string; // ≤200 chars around the admission marker
556
564
  readonly ts: string; // ISO time of the scan that recorded the debt
565
+ readonly awaiting?: readonly string[];
557
566
  }
558
567
 
559
568
  /** Where the Stop-hook scan got its transcript path — or why it has none. */
@@ -643,20 +652,27 @@ const RETRO_SCAN_LOCK_TIMEOUT_MS = 2_000;
643
652
  */
644
653
  export function foldAdmissionDebt(
645
654
  events: readonly SessionEvent[],
646
- prior: { snippet: string } | null,
647
- ): { snippet: string } | null {
655
+ prior: AdmissionDebt | null,
656
+ ): AdmissionDebt | null {
648
657
  let pending = prior;
649
658
  // EVERY teach issued against the live debt and still awaiting its own result, by `tool_use_id`.
650
659
  // Round 3, P1-2: a single `lastPaid` slot lost the FIRST of two parallel teaches — the second call
651
660
  // saw an already-cleared `pending` and overwrote the slot with null, so when both results came back
652
661
  // receipt-less neither could re-arm and the debt was silently forgiven. A set, and settlement moved
653
662
  // to the RECEIPT, removes the whole class: the CALL now registers a candidate and changes nothing.
654
- const awaiting = new Set<string>();
663
+ // Old or malformed sentinels mean no candidates, without skipping the rest of the scan.
664
+ const saved = prior?.awaiting;
665
+ const awaiting = new Set<string>(
666
+ Array.isArray(saved) && saved.every((id): id is string => typeof id === 'string') ? saved : [],
667
+ );
655
668
  for (const e of events) {
656
669
  if (isTeachCommand(e)) {
657
670
  if (typeof e.toolUseId === 'string') {
658
671
  // Registered, not settled. The debt stands until this call's own result carries a receipt.
659
- if (pending !== null) awaiting.add(e.toolUseId);
672
+ if (pending !== null) {
673
+ awaiting.add(e.toolUseId);
674
+ if (awaiting.size > MAX_AWAITING_TEACHES) awaiting.delete(awaiting.values().next().value!);
675
+ }
660
676
  } else {
661
677
  // No pairing key ⇒ no result can ever confirm OR refute this call, so it pays on the command
662
678
  // alone — the pre-ADR-004 behaviour, kept deliberately so every id-less fixture and every
@@ -679,10 +695,16 @@ export function foldAdmissionDebt(
679
695
  const snippet = admissionSnippet(e.text);
680
696
  // A NEW admission supersedes every teach still in flight: those calls were issued against the
681
697
  // OLDER debt, so their receipts must not settle this one (ADR-001 D4 asymmetry).
682
- if (snippet !== null) { pending = { snippet }; awaiting.clear(); }
698
+ if (snippet !== null) {
699
+ if (pending === null || pending.snippet !== snippet) awaiting.clear();
700
+ pending = { snippet };
701
+ }
683
702
  }
684
703
  }
685
- return pending;
704
+ return pending === null ? null : {
705
+ snippet: pending.snippet,
706
+ ...(awaiting.size > 0 ? { awaiting: [...awaiting] } : {}),
707
+ };
686
708
  }
687
709
 
688
710
  const writeJsonAtomic = (path: string, value: unknown): void => {
@@ -735,11 +757,14 @@ function scanTailUnderLock(dzDir: string, transcriptPath: string, nowIso?: strin
735
757
  // Prior debt carries over ONLY for the same session; a stale sentinel (another session's debt)
736
758
  // is dropped — the PreCompact/SessionEnd retro of THAT session was its collector, and injecting
737
759
  // an old session's debt into a new one is noise (acid A9).
738
- let prior: { snippet: string } | null = null;
760
+ let prior: AdmissionDebt | null = null;
739
761
  let hadSentinel = false;
740
762
  try {
741
763
  const s = JSON.parse(readFileSync(pendingPath, 'utf8')) as Partial<RetroPendingSentinel>;
742
- if (s.transcript === transcriptPath && typeof s.snippet === 'string') { prior = { snippet: s.snippet }; hadSentinel = true; }
764
+ if (s.transcript === transcriptPath && typeof s.snippet === 'string') {
765
+ prior = { snippet: s.snippet, ...(s.awaiting !== undefined ? { awaiting: s.awaiting } : {}) };
766
+ hadSentinel = true;
767
+ }
743
768
  else { try { unlinkSync(pendingPath); } catch { /* already gone */ } }
744
769
  } catch { /* no sentinel */ }
745
770
 
@@ -778,7 +803,7 @@ function scanTailUnderLock(dzDir: string, transcriptPath: string, nowIso?: strin
778
803
  const next = foldAdmissionDebt(events, prior);
779
804
  const newOffset = offset + consumed;
780
805
  mkdirSync(dzDir, { recursive: true });
781
- writeJsonAtomic(statePath, { schema: 1, transcript: transcriptPath, offset: newOffset });
806
+ let outcome: TailScanOutcome;
782
807
  if (next !== null) {
783
808
  const sentinel: RetroPendingSentinel = {
784
809
  schema: 1,
@@ -786,15 +811,26 @@ function scanTailUnderLock(dzDir: string, transcriptPath: string, nowIso?: strin
786
811
  transcript: transcriptPath,
787
812
  snippet: next.snippet.replace(/\s+/g, ' ').trim().slice(0, 200),
788
813
  ts: nowIso ?? new Date().toISOString(),
814
+ ...(next.awaiting !== undefined ? { awaiting: next.awaiting } : {}),
789
815
  };
790
816
  writeJsonAtomic(pendingPath, sentinel);
791
- return { status: 'pending', snippet: sentinel.snippet, scannedBytes: consumed, offset: newOffset };
817
+ outcome = { status: 'pending', snippet: sentinel.snippet, scannedBytes: consumed, offset: newOffset };
818
+ } else if (hadSentinel) {
819
+ // Step-8 finding (23.09.2026, HIGH): swallowing EVERY unlink failure here and then advancing
820
+ // the offset anyway recreates the very loss this feature removes — the sentinel survives, the
821
+ // receipt that paid it is consumed, and every later scan re-arms the SAME stale snippet
822
+ // forever. Only "already gone" is benign; any other failure must leave the bytes replayable,
823
+ // so it propagates and the offset write below never runs. This makes the CLEAR branch
824
+ // symmetric with the ARM branch, which already blocks the offset on a failed write.
825
+ try { unlinkSync(pendingPath); }
826
+ catch (e) { if ((e as NodeJS.ErrnoException).code !== 'ENOENT') throw e; }
827
+ outcome = { status: 'cleared', scannedBytes: consumed, offset: newOffset };
828
+ } else {
829
+ outcome = { status: 'none', scannedBytes: consumed, offset: newOffset };
792
830
  }
793
- if (hadSentinel) {
794
- try { unlinkSync(pendingPath); } catch { /* already gone */ }
795
- return { status: 'cleared', scannedBytes: consumed, offset: newOffset };
796
- }
797
- return { status: 'none', scannedBytes: consumed, offset: newOffset };
831
+ // Commit the debt BEFORE its offset: an interruption must leave these bytes replayable.
832
+ writeJsonAtomic(statePath, { schema: 1, transcript: transcriptPath, offset: newOffset });
833
+ return outcome;
798
834
  } catch {
799
835
  return { status: 'none', scannedBytes: 0, offset: 0 };
800
836
  }
@@ -0,0 +1,29 @@
1
+ /** Symptoms of text already damaged by shell expansion; these are warnings, never refusals. */
2
+ export type MangleKind = 'teach' | 'backlog';
3
+ export type MangleSymptom = {
4
+ kind: 'empty-substitution-hole' | 'dangling-arrow' | 'empty-brackets' | 'short-for-kind';
5
+ /** UTF-16 code-unit index, as used by JavaScript strings (not a UTF-8 byte offset). */
6
+ at: number;
7
+ excerpt: string;
8
+ };
9
+
10
+ const TEACH_MIN_CHARS = 40;
11
+ const BACKLOG_MIN_CHARS = 20;
12
+
13
+ export function detectMangledText(text: string, kind: MangleKind): readonly MangleSymptom[] {
14
+ // Literal backticks and $( are NOT symptoms: the shell left those characters intact.
15
+ const symptoms: MangleSymptom[] = [];
16
+ const add = (kind: MangleSymptom['kind'], at: number): void => {
17
+ symptoms.push({ kind, at, excerpt: text.slice(at, at + 40) });
18
+ };
19
+ const holes = /(?<=\S) {2,}(?=\S)|(?<=\S) +(?=[.,)»;:])/g;
20
+ const arrows = /(?:→|->)[\s\p{P}]*?(?=[.!?。!?]|$)/gu;
21
+ const brackets = /\(\s*\)|\[\s*\]|\{\s*\}|«\s*»|"\s*"|'\s*'/g;
22
+ for (const match of text.matchAll(holes)) add('empty-substitution-hole', match.index);
23
+ for (const match of text.matchAll(arrows)) add('dangling-arrow', match.index);
24
+ for (const match of text.matchAll(brackets)) add('empty-brackets', match.index);
25
+ if (text.trim().length < (kind === 'teach' ? TEACH_MIN_CHARS : BACKLOG_MIN_CHARS)) {
26
+ add('short-for-kind', 0);
27
+ }
28
+ return symptoms.sort((a, b) => a.at - b.at);
29
+ }
@@ -0,0 +1,17 @@
1
+ /** File states shared by skill-tree producers. Extra target files are preserved. */
2
+ export type FileState = 'match' | 'missing' | 'diverged' | 'extra';
3
+
4
+ /** Compare relative-path → SHA-256 maps without reading or changing either tree. */
5
+ export function compareTrees(
6
+ source: ReadonlyMap<string, string>,
7
+ target: ReadonlyMap<string, string>,
8
+ ): Map<string, FileState> {
9
+ const states = new Map<string, FileState>();
10
+ for (const [path, hash] of source) {
11
+ states.set(path, !target.has(path) ? 'missing' : target.get(path) === hash ? 'match' : 'diverged');
12
+ }
13
+ for (const path of target.keys()) {
14
+ if (!source.has(path)) states.set(path, 'extra');
15
+ }
16
+ return states;
17
+ }