@dzhechkov/harness-core 0.7.0 → 0.7.3

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 (62) hide show
  1. package/.dz-manifest.json +116 -56
  2. package/README.md +3 -0
  3. package/dist/event-chain.d.ts +50 -0
  4. package/dist/event-chain.d.ts.map +1 -1
  5. package/dist/event-chain.js +31 -0
  6. package/dist/event-chain.js.map +1 -1
  7. package/dist/index.d.ts +8 -5
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +6 -3
  10. package/dist/index.js.map +1 -1
  11. package/dist/name-check.d.ts +98 -0
  12. package/dist/name-check.d.ts.map +1 -0
  13. package/dist/name-check.js +333 -0
  14. package/dist/name-check.js.map +1 -0
  15. package/dist/operations.d.ts.map +1 -1
  16. package/dist/operations.js +25 -4
  17. package/dist/operations.js.map +1 -1
  18. package/dist/provenance.d.ts +100 -92
  19. package/dist/provenance.d.ts.map +1 -1
  20. package/dist/provenance.js +122 -122
  21. package/dist/provenance.js.map +1 -1
  22. package/dist/recall-domain-boost.d.ts +10 -3
  23. package/dist/recall-domain-boost.d.ts.map +1 -1
  24. package/dist/recall-domain-boost.js +7 -0
  25. package/dist/recall-domain-boost.js.map +1 -1
  26. package/dist/recall-hook-policy.d.ts +15 -0
  27. package/dist/recall-hook-policy.d.ts.map +1 -1
  28. package/dist/recall-hook-policy.js +59 -0
  29. package/dist/recall-hook-policy.js.map +1 -1
  30. package/dist/recap.d.ts +146 -0
  31. package/dist/recap.d.ts.map +1 -0
  32. package/dist/recap.js +346 -0
  33. package/dist/recap.js.map +1 -0
  34. package/dist/retro.d.ts +131 -0
  35. package/dist/retro.d.ts.map +1 -0
  36. package/dist/retro.js +207 -0
  37. package/dist/retro.js.map +1 -0
  38. package/dist/score.d.ts +21 -0
  39. package/dist/score.d.ts.map +1 -1
  40. package/dist/score.js +44 -3
  41. package/dist/score.js.map +1 -1
  42. package/dist/sign.d.ts +14 -2
  43. package/dist/sign.d.ts.map +1 -1
  44. package/dist/sign.js +140 -7
  45. package/dist/sign.js.map +1 -1
  46. package/dist/vector-tier.d.ts +54 -0
  47. package/dist/vector-tier.d.ts.map +1 -1
  48. package/dist/vector-tier.js +69 -8
  49. package/dist/vector-tier.js.map +1 -1
  50. package/package.json +6 -6
  51. package/sbom.json +205 -55
  52. package/src/event-chain.ts +64 -0
  53. package/src/index.ts +12 -0
  54. package/src/name-check.ts +331 -0
  55. package/src/operations.ts +26 -5
  56. package/src/provenance.ts +217 -0
  57. package/src/recall-domain-boost.ts +10 -3
  58. package/src/recall-hook-policy.ts +60 -0
  59. package/src/recap.ts +462 -0
  60. package/src/score.ts +53 -3
  61. package/src/sign.ts +129 -7
  62. package/src/vector-tier.ts +109 -10
@@ -868,3 +868,67 @@ export function renderEventChainVerification(v: EventChainVerification, label: s
868
868
  if (kinds.length > 0) parts.push(kinds.join('/'));
869
869
  return `${parts.join(' · ')} — ${v.scope}`;
870
870
  }
871
+
872
+ /**
873
+ * How old a defect is relative to the log's CURRENT unbroken run.
874
+ *
875
+ * `historical` — the break happened, and an unbroken segment has run since. It cannot be un-happened
876
+ * and it does not make today's records suspect. `live` — the break is inside the segment we are
877
+ * still appending to, so records after it are the ones a verdict would rest on.
878
+ */
879
+ /**
880
+ * Where a defect sits relative to the log's current unbroken run.
881
+ *
882
+ * Deliberately NOT called "historical". A break followed by ONE record is not a healed log, and a
883
+ * name that implies healing would let a caller skip the count — the reviewer's exact objection
884
+ * (codex `gpt-5.6-sol`, 2026-08-24: a defect at line 2 of 3 was filed as historical because line 3
885
+ * existed). These names state a position; the COUNT states how much evidence stands behind it, and
886
+ * the count is what a caller must show.
887
+ */
888
+ export type ChainDefectAge = 'before-run' | 'in-run';
889
+
890
+ /**
891
+ * The line where the log's current unbroken run begins: one past the last defect, or 1 when there
892
+ * are none.
893
+ *
894
+ * Why this exists: `dz doctor` reported "learning verdicts computed from this log are unsafe" for
895
+ * both `.dz` event logs, flatly, for four weeks. MEASURED 2026-08-24 — every defect in both files
896
+ * precedes an unbroken run of 998 of 1138 rows in one and 88 of 426 in the other. The verdict was
897
+ * true of the file and false of those runs, and a permanent red about something nobody can change is
898
+ * a red that stops being read.
899
+ */
900
+ export function liveSegmentStart(verification: EventChainVerification): number {
901
+ let last = 0;
902
+ for (const d of verification.defects) if (d.line > last) last = d.line;
903
+ return last + 1;
904
+ }
905
+
906
+ export interface ChainDefectAges {
907
+ /** Defects that an unbroken run has followed. How MUCH of a run is `runRecords`, never implied. */
908
+ readonly beforeRun: readonly EventChainDefect[];
909
+ /** Defects with nothing sound after them — the strong verdict is earned here. */
910
+ readonly inRun: readonly EventChainDefect[];
911
+ /** First line of the current unbroken run. */
912
+ readonly runFrom: number;
913
+ /**
914
+ * Records in that run. THE NUMBER IS THE EVIDENCE: 998 sound records after a break say something a
915
+ * caller may relax on; 1 says almost nothing, and a caller that prints "sound" without printing
916
+ * this number is overclaiming on its behalf.
917
+ */
918
+ readonly runRecords: number;
919
+ }
920
+
921
+ /**
922
+ * Split defects by whether an unbroken run followed them, and say how long that run is.
923
+ *
924
+ * `runFrom` is computed from the defects themselves, so a defect can never classify itself: the run
925
+ * only begins after the LAST of them. A log whose newest defect is its final record therefore has an
926
+ * empty `beforeRun` and a `runRecords` of zero.
927
+ */
928
+ export function classifyChainDefects(verification: EventChainVerification, totalRecords: number): ChainDefectAges {
929
+ const runFrom = liveSegmentStart(verification);
930
+ const runRecords = Math.max(0, totalRecords - runFrom + 1);
931
+ const beforeRun = runRecords > 0 ? verification.defects : [];
932
+ const inRun = runRecords > 0 ? [] : verification.defects;
933
+ return { beforeRun, inRun, runFrom, runRecords };
934
+ }
package/src/index.ts CHANGED
@@ -127,6 +127,8 @@ export {
127
127
  readVectorEngineMode,
128
128
  readHarmonizeThreshold,
129
129
  vectorMirrorEnabled,
130
+ mirrorWriterReason,
131
+ mirrorWriterExplanation,
130
132
  resolveVectorEngine,
131
133
  mirrorEntriesToVector,
132
134
  mirrorPatternsToVector,
@@ -155,6 +157,7 @@ export type {
155
157
  RankedPattern,
156
158
  VectorServiceOptions,
157
159
  VectorTierStatus,
160
+ MirrorWriterState,
158
161
  HarmonizeItem,
159
162
  HarmonizeCluster,
160
163
  HarmonizeReport,
@@ -311,6 +314,8 @@ export {
311
314
  DEFAULT_RECALL_HOOK_BUDGET_CHARS,
312
315
  MIN_PROMPT_CHARS,
313
316
  MIN_CONTENT_TOKENS,
317
+ closenessLine,
318
+ anyAboveFloor,
314
319
  } from './recall-hook-policy.js';
315
320
  export type { QueryLang, RecallFloors, HookCandidate, HookSelection } from './recall-hook-policy.js';
316
321
  export {
@@ -437,6 +442,8 @@ export {
437
442
  rewriteSnapshotUnchanged,
438
443
  guardedRewrite,
439
444
  DEFAULT_REWRITE_ATTEMPTS,
445
+ liveSegmentStart,
446
+ classifyChainDefects,
440
447
  } from './event-chain.js';
441
448
  export type {
442
449
  ChainFields,
@@ -451,6 +458,8 @@ export type {
451
458
  EventChainDefectKind,
452
459
  EventChainVerification,
453
460
  VerifyEventChainOptions,
461
+ ChainDefectAge,
462
+ ChainDefectAges,
454
463
  } from './event-chain.js';
455
464
  export { decideProvenance, environmentCanMintProvenance, publishArgv, discoverPackages, publishPackages, bumpPatch, compareVersions, findUnpackagedSkills, orderByDependencies, syncReadmeVersion } from './publish.js';
456
465
  export { fetchAllDownloads } from './downloads.js';
@@ -805,6 +814,9 @@ export {
805
814
  // Run-process scorecard (feature dz-score, Reading C) — scores the DISCIPLINE of a feature-adr run
806
815
  // from its artifacts. Descriptive-only, permanently: it never gates.
807
816
  export * from './score.js';
817
+ export * from './recap.js';
818
+ export * from './provenance.js';
819
+ export * from './name-check.js';
808
820
 
809
821
  // Mutation gate (feature ha-mutation-gate) — deliberately break each NAMED protection in a scratch
810
822
  // copy, run the suite, REQUIRE red. Proves a test DISCRIMINATES, not merely that it is green.
@@ -0,0 +1,331 @@
1
+ /**
2
+ * `dz name-check` — is this name free, before a line of code is written?
3
+ *
4
+ * WHY THIS EXISTS, stated plainly: twice in one day a name collision broke the build outright.
5
+ * `dz retro` was already a command (the per-session process retro), and its star re-export clash
6
+ * stopped the CLI from importing at all; `decideProvenance` was already an export (npm provenance),
7
+ * and the build went red mid-feature. MEASURED 2026-08-24: `case 'retro':` is in the dispatcher, and
8
+ * both `buildRetro` and `decideProvenance` are in the core's 1020-name public surface — so BOTH were
9
+ * answerable before any code, and nobody asked.
10
+ *
11
+ * The owner's question was "what guarantees you will check?". An agent's intention is layer 4 on this
12
+ * project's cost-of-detection ladder: it works while remembered and is silent when it lapses. A
13
+ * command is the guarantee; a promise is not.
14
+ *
15
+ * PURE: no filesystem here. The scan runs in the CLI and arrives as facts — see ADR-001 for why
16
+ * those facts come from SOURCE and never from `dist`.
17
+ */
18
+
19
+ /** What kind of name was asked about. */
20
+ export type NameKind = 'command' | 'module' | 'export';
21
+
22
+ export interface NameQuery {
23
+ readonly kind: NameKind;
24
+ readonly name: string;
25
+ }
26
+
27
+ export interface NameFacts {
28
+ /** Command names found in the dispatcher and in the help block. */
29
+ readonly commands: ReadonlySet<string>;
30
+ /** Module basenames found as `src/<basename>.ts` in any workspace package. */
31
+ readonly modules: ReadonlyMap<string, string>;
32
+ /** Exported identifiers found by scanning SOURCE, mapped to the file that declares them. */
33
+ readonly exports: ReadonlyMap<string, string>;
34
+ /**
35
+ * True when the scan itself could not be performed (no workspace found, unreadable tree).
36
+ * A scan that did not run must never report "free" — that is the whole failure this command
37
+ * exists to prevent, one level up.
38
+ */
39
+ readonly scanFailed?: boolean;
40
+ /**
41
+ * What the sweep actually saw. Reported to the operator, because "one empty .ts file in a
42
+ * lookalike directory" and "the real workspace" both used to satisfy a bare did-it-open-a-file
43
+ * test, and the second question a reviewer asked was exactly that (2026-08-24).
44
+ */
45
+ readonly scanned?: { readonly packages: number; readonly files: number; readonly exports: number; readonly commands: number };
46
+ }
47
+
48
+ export type NameVerdict = 'free' | 'taken';
49
+
50
+ export interface NameResolution {
51
+ readonly kind: NameKind;
52
+ readonly name: string;
53
+ readonly verdict: NameVerdict;
54
+ /** Where the collision lives, when there is one. Empty for a free name. */
55
+ readonly where: string;
56
+ }
57
+
58
+ export type NameOutcome = 'free' | 'taken' | 'not-established';
59
+
60
+ export interface NameDecision {
61
+ readonly outcome: NameOutcome;
62
+ /** 0 every name free · 1 at least one taken · 2 nothing asked or the scan did not run. */
63
+ readonly exit: 0 | 1 | 2;
64
+ readonly results: readonly NameResolution[];
65
+ readonly reason: string;
66
+ }
67
+
68
+ /** Is a proposed name already spoken for? */
69
+ export function classifyName(query: NameQuery, facts: NameFacts): NameResolution {
70
+ const name = query.name.trim();
71
+ const at = (verdict: NameVerdict, where: string): NameResolution => ({ kind: query.kind, name, verdict, where });
72
+ if (query.kind === 'command') {
73
+ return facts.commands.has(name) ? at('taken', 'already dispatched as a dz command') : at('free', '');
74
+ }
75
+ if (query.kind === 'module') {
76
+ const file = facts.modules.get(name);
77
+ return file === undefined ? at('free', '') : at('taken', file);
78
+ }
79
+ const file = facts.exports.get(name);
80
+ return file === undefined ? at('free', '') : at('taken', file);
81
+ }
82
+
83
+ /**
84
+ * The whole verdict.
85
+ *
86
+ * Two ways to be not-established, and neither returns zero: nothing was asked, or the scan did not
87
+ * run. "I checked nothing" and "nothing is taken" are different answers, and a gate that conflates
88
+ * them is green exactly when it is blind — the defect measured on `dz sync` (0/0, exit 0) and on the
89
+ * source scanner that printed `github: 0` for a 401.
90
+ */
91
+ export function decideNameCheck(queries: readonly NameQuery[], facts: NameFacts): NameDecision {
92
+ if (facts.scanFailed === true) {
93
+ return { outcome: 'not-established', exit: 2, results: [], reason: 'the workspace could not be scanned, so no name was checked — this is not a clean bill' };
94
+ }
95
+ const asked = queries.filter((q) => q.name.trim() !== '');
96
+ if (asked.length === 0) {
97
+ return { outcome: 'not-established', exit: 2, results: [], reason: 'no name was asked about — pass --command, --module or --export' };
98
+ }
99
+ // ESTABLISHMENT IS PER KIND. A question about an export cannot be answered by a sweep that found
100
+ // no exports at all; a question about a command cannot be answered without having seen a CLI. A
101
+ // sweep of a lookalike tree satisfies neither, and the honest verdict there is "not established",
102
+ // not "free".
103
+ const unanswerable = asked.filter((q) => {
104
+ if (q.kind === 'command') return facts.commands.size === 0;
105
+ if (q.kind === 'module') return facts.modules.size === 0;
106
+ return facts.exports.size === 0;
107
+ });
108
+ if (unanswerable.length > 0) {
109
+ const kinds = [...new Set(unanswerable.map((q) => q.kind))].join(', ');
110
+ return {
111
+ outcome: 'not-established',
112
+ exit: 2,
113
+ results: [],
114
+ reason: `the sweep found nothing of kind: ${kinds} — a tree with no ${kinds} cannot answer a question about one, and reporting "free" from it would be a clean bill from an empty room`,
115
+ };
116
+ }
117
+ const results = asked.map((q) => classifyName(q, facts));
118
+ const taken = results.filter((r) => r.verdict === 'taken');
119
+ if (taken.length > 0) {
120
+ return {
121
+ outcome: 'taken',
122
+ exit: 1,
123
+ results,
124
+ reason: `${taken.length} of ${results.length} name(s) already spoken for — rename before writing, not after the build goes red`,
125
+ };
126
+ }
127
+ return { outcome: 'free', exit: 0, results, reason: `all ${results.length} name(s) are free` };
128
+ }
129
+
130
+ export function renderNameCheck(decision: NameDecision, scanned?: NameFacts['scanned']): string[] {
131
+ const out: string[] = [];
132
+ if (scanned !== undefined) {
133
+ // Printed always, pass or fail: the operator must be able to see that the sweep looked at a real
134
+ // workspace and not at a directory that merely has the right shape.
135
+ out.push(` swept ${scanned.packages} package(s), ${scanned.files} source file(s) — ${scanned.exports} export(s), ${scanned.commands} command(s)`);
136
+ }
137
+ for (const r of decision.results) {
138
+ out.push(r.verdict === 'taken'
139
+ ? ` [taken] ${r.kind} ${r.name} — ${r.where}`
140
+ : ` [free] ${r.kind} ${r.name}`);
141
+ }
142
+ const label = decision.outcome === 'free' ? 'FREE' : decision.outcome === 'taken' ? 'TAKEN' : 'NOT ESTABLISHED';
143
+ out.push(`dz name-check: ${label} — ${decision.reason}`);
144
+ if (decision.outcome === 'free') {
145
+ // Said on the passing path, because that is where the limit gets forgotten: the scan reads
146
+ // declarations, so a re-export under a different name (`export { a as b }`) is invisible to it.
147
+ out.push(' note: this reads declarations in source. A re-export under a different name is not visible here — the build still owns that case.');
148
+ }
149
+ return out;
150
+ }
151
+
152
+ /**
153
+ * Exported identifiers declared in one TypeScript source file.
154
+ *
155
+ * Deliberately a scanner over DECLARATIONS, not a loader of the built package (ADR-001): a stale
156
+ * `dist` answers "free" about a name the source already took, and answers it confidently. MEASURED
157
+ * 2026-08-22 in this repo — half an hour of live runs against a previous build while `tsc` was red.
158
+ */
159
+ /**
160
+ * Source with comments blanked out, quotes respected.
161
+ *
162
+ * Trivia may sit between ANY two tokens: an `export` followed by a block comment and then `class`
163
+ * was reported FREE, because the declaration pattern expects the keyword to be adjacent
164
+ * (cross-family review round 4, codex gpt-5.6-sol, 2026-08-24). Rather than widen the pattern for
165
+ * one shape of trivia, the trivia is removed first — which fixes the whole class at once.
166
+ *
167
+ * Newlines are PRESERVED so line-anchored patterns keep their anchors.
168
+ */
169
+ export function stripComments(source: string): string {
170
+ let out = '';
171
+ let i = 0;
172
+ let quote: string | null = null;
173
+ // The previous significant character decides whether a `/` opens a REGEX or divides. Without that
174
+ // distinction the regex literal `/[/*]/` reads as a block-comment opener and everything after it
175
+ // is blanked — so a later `export const Taken = 2;` vanished and was reported FREE (cross-family
176
+ // review round 5, codex gpt-5.6-sol, 2026-08-24).
177
+ let prev = '';
178
+ const REGEX_MAY_FOLLOW = new Set(['', '(', ',', '=', ':', '[', '!', '&', '|', '?', '{', '}', ';', '+', '-', '*', '%', '~', '^', '<', '>', '\n']);
179
+ const KEYWORD_BEFORE_REGEX = /\b(return|typeof|case|in|of|new|delete|void|instanceof|do|else|yield|await)\s*$/;
180
+ while (i < source.length) {
181
+ const c = source[i] as string;
182
+ const next = source[i + 1];
183
+ if (quote !== null) {
184
+ out += c;
185
+ if (c === '\\') { out += next ?? ''; i += 2; continue; }
186
+ if (c === quote) quote = null;
187
+ i++;
188
+ continue;
189
+ }
190
+ if (c === '"' || c === "'" || c === '`') { quote = c; out += c; i++; prev = c; continue; }
191
+ // Comment forms are checked FIRST because they are unambiguous: a regex literal can begin
192
+ // with neither `/` nor `*`. Putting the regex check first made a block comment at the start
193
+ // of a line look like a literal and survive the strip — a regression caught by its own test.
194
+ if (c === '/' && next === '/') {
195
+ while (i < source.length && source[i] !== '\n') { out += ' '; i++; }
196
+ prev = '\n';
197
+ continue;
198
+ }
199
+ if (c === '/' && next === '*') {
200
+ const close = source.indexOf('*/', i + 2);
201
+ if (close === -1) {
202
+ // An UNTERMINATED block comment is not a comment — it is a misread. Blanking to EOF turned
203
+ // every heuristic slip into a whole-file loss: `if (true) /[/*]/.test('*')` was read as an
204
+ // opener after `)`, and every export below it vanished and was reported FREE (cross-family
205
+ // review round 6, codex gpt-5.6-sol, 2026-08-24). Real source with an unclosed comment does
206
+ // not compile, so treating the text as text is strictly the safer reading: the worst case
207
+ // becomes a false TAKEN, which is conservative, instead of a false FREE, which is a lie.
208
+ out += c;
209
+ prev = c;
210
+ i++;
211
+ continue;
212
+ }
213
+ const stop = close + 2;
214
+ for (let k = i; k < stop; k++) out += source[k] === '\n' ? '\n' : ' ';
215
+ i = stop;
216
+ prev = ' ';
217
+ continue;
218
+ }
219
+ if (c === '/' && (REGEX_MAY_FOLLOW.has(prev) || KEYWORD_BEFORE_REGEX.test(out))) {
220
+ // A regex literal: copy it verbatim to its unescaped closing slash. A `/` inside a character
221
+ // class does not close it.
222
+ let j = i + 1;
223
+ let inClass = false;
224
+ let closed = false;
225
+ while (j < source.length) {
226
+ const d = source[j] as string;
227
+ if (d === '\\') { j += 2; continue; }
228
+ if (d === '\n') break; // an unterminated literal is not one
229
+ if (d === '[') inClass = true;
230
+ else if (d === ']') inClass = false;
231
+ else if (d === '/' && !inClass) { closed = true; j++; break; }
232
+ j++;
233
+ }
234
+ if (closed) { out += source.slice(i, j); prev = '/'; i = j; continue; }
235
+ // Not a regex after all — fall through to the comment checks below.
236
+ }
237
+ out += c;
238
+ if (!/\s/.test(c) || c === '\n') prev = c;
239
+ i++;
240
+ }
241
+ return out;
242
+ }
243
+
244
+ export function exportedNamesIn(rawSource: string): string[] {
245
+ const source = stripComments(rawSource);
246
+ const names = new Set<string>();
247
+ // Non-binding declarations export exactly one name. The KEYWORD SET is the correctness of this
248
+ // line just as much as the modifier set was: `export namespace Taken {}` was reported FREE because
249
+ // `namespace` was missing (cross-family review round 3, codex gpt-5.6-sol, 2026-08-24). `module` is
250
+ // the legacy spelling of the same thing and is admitted with it.
251
+ const decl = /^\s*export\s+(?:(?:declare|abstract|async)\s+)*(?:function|class|interface|type|enum|namespace|module)\s+([A-Za-z_$][\w$]*)/gm;
252
+ for (let m = decl.exec(source); m !== null; m = decl.exec(source)) if (m[1] !== undefined) names.add(m[1]);
253
+
254
+ // `const`/`let`/`var` can declare MANY names in one statement, and only the first was captured:
255
+ // `export const Seen = 1, Taken = 2;` reported `Taken` FREE (cross-family review round 2, codex
256
+ // gpt-5.6-sol, 2026-08-24). Destructuring exports names too. So the declarator list is parsed.
257
+ const binding = /^\s*export\s+(?:declare\s+)*(?:const|let|var)\s+/gm;
258
+ for (let m = binding.exec(source); m !== null; m = binding.exec(source)) {
259
+ for (const n of declaredBindingNames(source.slice(m.index + m[0].length))) names.add(n);
260
+ }
261
+
262
+ // `export { a, b as c }` — the EXPORTED name is what a consumer collides with, so for an alias it
263
+ // is the right-hand side. A bare list contributes its own names.
264
+ const list = /^\s*export\s*\{([^}]*)\}/gm;
265
+ for (let m = list.exec(source); m !== null; m = list.exec(source)) {
266
+ for (const raw of (m[1] ?? '').split(',')) {
267
+ const part = raw.trim();
268
+ if (part === '' || part.startsWith('*')) continue;
269
+ const alias = /\bas\s+([A-Za-z_$][\w$]*)\s*$/.exec(part);
270
+ const bare = /^(?:type\s+)?([A-Za-z_$][\w$]*)$/.exec(part);
271
+ const picked = alias?.[1] ?? bare?.[1];
272
+ if (picked !== undefined && picked !== 'default') names.add(picked);
273
+ }
274
+ }
275
+ return [...names];
276
+ }
277
+
278
+ const BINDING_NOISE: ReadonlySet<string> = new Set(['readonly', 'as', 'const', 'await', 'typeof']);
279
+
280
+ /**
281
+ * Every name bound by one `const`/`let`/`var` statement, given the text just after the keyword.
282
+ *
283
+ * Walks to the statement end at depth zero, splits the declarator list on top-level commas, and for
284
+ * each declarator takes the identifiers before its first top-level `=` or `:` — so an initialiser
285
+ * and a type annotation contribute nothing, while `a, b`, `{ a, b }` and `[a, b]` all do.
286
+ */
287
+ function declaredBindingNames(after: string): string[] {
288
+ let depth = 0;
289
+ let end = after.length;
290
+ for (let i = 0; i < after.length; i++) {
291
+ const c = after[i] as string;
292
+ if (c === '(' || c === '[' || c === '{') depth++;
293
+ else if (c === ')' || c === ']' || c === '}') { if (depth === 0) { end = i; break; } depth--; }
294
+ else if (c === ';' && depth === 0) { end = i; break; }
295
+ }
296
+ const stmt = after.slice(0, end);
297
+ const parts: string[] = [];
298
+ let level = 0;
299
+ let start = 0;
300
+ for (let i = 0; i < stmt.length; i++) {
301
+ const c = stmt[i] as string;
302
+ if (c === '(' || c === '[' || c === '{') level++;
303
+ else if (c === ')' || c === ']' || c === '}') level--;
304
+ else if (c === ',' && level === 0) { parts.push(stmt.slice(start, i)); start = i + 1; }
305
+ }
306
+ parts.push(stmt.slice(start));
307
+
308
+ const out: string[] = [];
309
+ for (const part of parts) {
310
+ let head = part;
311
+ let lvl = 0;
312
+ for (let i = 0; i < part.length; i++) {
313
+ const c = part[i] as string;
314
+ if (c === '(' || c === '[' || c === '{') lvl++;
315
+ else if (c === ')' || c === ']' || c === '}') lvl--;
316
+ else if ((c === '=' || c === ':') && lvl === 0) { head = part.slice(0, i); break; }
317
+ }
318
+ for (const m of head.matchAll(/[A-Za-z_$][\w$]*/g)) {
319
+ if (!BINDING_NOISE.has(m[0])) out.push(m[0]);
320
+ }
321
+ }
322
+ return out;
323
+ }
324
+
325
+ /** Command names a CLI source dispatches. The help block is scanned separately by the caller. */
326
+ export function dispatchedCommandsIn(source: string): string[] {
327
+ const names = new Set<string>();
328
+ const re = /^\s*case\s+'([a-z][a-z0-9-]*)':/gm;
329
+ for (let m = re.exec(source); m !== null; m = re.exec(source)) if (m[1] !== undefined) names.add(m[1]);
330
+ return [...names];
331
+ }
package/src/operations.ts CHANGED
@@ -1070,18 +1070,39 @@ export async function runDoctor(options: { projectRoot: string }): Promise<Docto
1070
1070
  // Silent when a log is absent or has never been chained: an unchained file is legal (FR-5), not a
1071
1071
  // fault, and reporting it would train the reader to ignore this line.
1072
1072
  try {
1073
- const { verifyEventChainText, EVENT_CHAIN_SCOPE } = await import('./event-chain.js');
1073
+ const { verifyEventChainText, classifyChainDefects, EVENT_CHAIN_SCOPE } = await import('./event-chain.js');
1074
1074
  for (const rel of ['recall-usage.jsonl', 'guard-audit.jsonl']) {
1075
1075
  const p = join(root, '.dz', rel);
1076
1076
  if (!existsSync(p)) continue;
1077
- const v = verifyEventChainText(readFileSync(p, 'utf-8'));
1077
+ const text = readFileSync(p, 'utf-8');
1078
+ const v = verifyEventChainText(text);
1078
1079
  if (v.chained === 0 || v.ok) continue;
1080
+ const total = text.split('\n').filter((l) => l.trim() !== '').length;
1081
+ const age = classifyChainDefects(v, total);
1082
+ const named = `${v.defects.length} defect(s): ${v.defects.slice(0, 3).map((d) => `${d.kind}@L${d.line}`).join(', ')}`;
1083
+ // A break that an unbroken run has already outlived is not a reason to distrust today's
1084
+ // records. Reporting both alike made this line PERMANENTLY red for four weeks — MEASURED
1085
+ // 2026-08-24: every defect in both logs is historical, with 998 of 1138 rows in one and 88 of
1086
+ // 426 in the other forming an unbroken run after the last of them. The verdict was true of the
1087
+ // file and false of the present, and a red nobody can act on is a red nobody reads.
1088
+ if (age.inRun.length === 0 && age.runRecords > 0) {
1089
+ checks.push({
1090
+ name: `evidence chain (.dz/${rel})`,
1091
+ ok: true,
1092
+ // The COUNT carries the meaning, and is printed first for that reason: "1 record forms an
1093
+ // unbroken run" is true and says almost nothing, while 998 says a great deal. Naming the
1094
+ // position without the count would overclaim on the reader's behalf (cross-family review,
1095
+ // codex gpt-5.6-sol, 2026-08-24).
1096
+ detail:
1097
+ `${named} — all BEFORE the current run: the last ${age.runRecords} record(s), from L${age.runFrom}, are unbroken, ` +
1098
+ `so verdicts over those ${age.runRecords} are sound. The break itself cannot be un-happened. Scope: ${EVENT_CHAIN_SCOPE}`,
1099
+ });
1100
+ continue;
1101
+ }
1079
1102
  checks.push({
1080
1103
  name: `evidence chain (.dz/${rel})`,
1081
1104
  ok: false,
1082
- detail:
1083
- `${v.defects.length} defect(s): ${v.defects.slice(0, 3).map((d) => `${d.kind}@L${d.line}`).join(', ')}` +
1084
- ` — learning verdicts computed from this log are unsafe. Scope: ${EVENT_CHAIN_SCOPE}`,
1105
+ detail: `${named} — with NO sound records after them: learning verdicts computed from this log are unsafe. Scope: ${EVENT_CHAIN_SCOPE}`,
1085
1106
  });
1086
1107
  }
1087
1108
  } catch {