@holmes-lab/holmes-kit 0.7.1 → 0.9.0

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 (44) hide show
  1. package/CHANGELOG.md +102 -0
  2. package/README.md +10 -6
  3. package/dist/.build-id +1 -1
  4. package/dist/holmes/cli/agents.js +21 -11
  5. package/dist/holmes/cli/doctor.js +25 -2
  6. package/dist/holmes/cli/init.js +3 -0
  7. package/dist/holmes/cli/mcp-schema-cost.d.ts +18 -0
  8. package/dist/holmes/cli/mcp-schema-cost.js +28 -0
  9. package/dist/holmes/cli/settings-merge.d.ts +1 -0
  10. package/dist/holmes/cli/settings-merge.js +6 -1
  11. package/dist/holmes/config/config.d.ts +8 -0
  12. package/dist/holmes/config/config.js +1 -1
  13. package/dist/holmes/governance/autonomy.d.ts +14 -0
  14. package/dist/holmes/governance/autonomy.js +75 -0
  15. package/dist/holmes/governance/constitution.d.ts +26 -0
  16. package/dist/holmes/governance/constitution.js +33 -0
  17. package/dist/holmes/guardrail/write-target.d.ts +25 -0
  18. package/dist/holmes/guardrail/write-target.js +143 -0
  19. package/dist/holmes/hooks/pre-tool-use.js +131 -48
  20. package/dist/holmes/hooks/session-start.d.ts +23 -0
  21. package/dist/holmes/hooks/session-start.js +111 -0
  22. package/dist/holmes/hooks/stop.d.ts +24 -0
  23. package/dist/holmes/hooks/stop.js +83 -3
  24. package/dist/holmes/mcp/handlers.d.ts +19 -0
  25. package/dist/holmes/mcp/handlers.js +126 -21
  26. package/dist/holmes/mcp/server-instructions.d.ts +8 -0
  27. package/dist/holmes/mcp/server-instructions.js +13 -0
  28. package/dist/holmes/mcp/server.js +21 -1
  29. package/dist/holmes/mcp/tool-schemas.js +1 -0
  30. package/dist/holmes/review/mutate.d.ts +17 -0
  31. package/dist/holmes/review/mutate.js +66 -0
  32. package/dist/holmes/review/test-outcomes.d.ts +35 -0
  33. package/dist/holmes/review/test-outcomes.js +108 -0
  34. package/dist/holmes/review/test-runner.d.ts +30 -0
  35. package/dist/holmes/review/test-runner.js +71 -5
  36. package/dist/holmes/spec/kills.d.ts +14 -0
  37. package/dist/holmes/spec/kills.js +28 -0
  38. package/dist/holmes/spec/spec-store.d.ts +9 -0
  39. package/dist/holmes/spec/spec-store.js +17 -0
  40. package/dist/holmes/spec/validator.js +18 -0
  41. package/dist/holmes/update/update-notice.d.ts +28 -0
  42. package/dist/holmes/update/update-notice.js +131 -0
  43. package/package.json +1 -1
  44. package/playbooks/tdd-slice/PLAYBOOK.md +82 -0
@@ -43,6 +43,7 @@ exports.isProtectedTarget = isProtectedTarget;
43
43
  exports.specTargetOf = specTargetOf;
44
44
  exports.protectedFileKindOf = protectedFileKindOf;
45
45
  exports.protectedKindOf = protectedKindOf;
46
+ exports.shellSegments = shellSegments;
46
47
  exports.resolvesInside = resolvesInside;
47
48
  // @implements A-SPEC-163
48
49
  const fs = __importStar(require("node:fs"));
@@ -300,6 +301,148 @@ function protectedKindOf(root, raw) {
300
301
  }
301
302
  return null;
302
303
  }
304
+ const HEREDOC_RE = /<<-?\s*(['"]?)([A-Za-z_]\w*)\1/;
305
+ const SHELL_STDIN_RE = /(?:^|[\s;|&(])(?:sh|bash|zsh|dash|ksh)\b[^<\n]*<</;
306
+ const PROG_STRING_RE = /\b(?:(?:sh|bash|zsh|dash|ksh)\b[^\n;|&]*?-c|eval)\s+(?:'([^']*)'|"([^"]*)")/g;
307
+ function shellSegments(command, startBase, depth = 0) {
308
+ if (depth > 3)
309
+ return [{ text: command, base: null }];
310
+ const out = [];
311
+ let base = startBase;
312
+ const pieces = [];
313
+ {
314
+ const lines = command.split('\n');
315
+ let cur = [];
316
+ const flush = () => { if (cur.length > 0) {
317
+ pieces.push({ text: cur.join('\n'), kind: 'cmd' });
318
+ cur = [];
319
+ } };
320
+ for (let i = 0; i < lines.length; i++) {
321
+ const line = lines[i];
322
+ const hd = HEREDOC_RE.exec(line);
323
+ if (!hd) {
324
+ cur.push(line);
325
+ continue;
326
+ }
327
+ cur.push(line);
328
+ flush();
329
+ const body = [];
330
+ for (i++; i < lines.length && lines[i].replace(/^\t+/, '') !== hd[2]; i++)
331
+ body.push(lines[i]);
332
+ pieces.push({ text: body.join('\n'), kind: SHELL_STDIN_RE.test(line) ? 'prog' : 'data' });
333
+ }
334
+ flush();
335
+ }
336
+ // Quote-aware split at ; & | and newlines — separators inside quotes are data.
337
+ const splitTop = (text) => {
338
+ const parts = [];
339
+ let acc = '';
340
+ let quote = null;
341
+ for (let k = 0; k < text.length; k++) {
342
+ const ch = text[k];
343
+ if (quote !== null) {
344
+ acc += ch;
345
+ if (ch === quote)
346
+ quote = null;
347
+ continue;
348
+ }
349
+ if (ch === "'" || ch === '"') {
350
+ quote = ch;
351
+ acc += ch;
352
+ continue;
353
+ }
354
+ if (ch === '\\' && k + 1 < text.length) {
355
+ acc += ch + text[++k];
356
+ continue;
357
+ }
358
+ if (ch === ';' || ch === '&' || ch === '|' || ch === '\n') {
359
+ if (acc.trim() !== '')
360
+ parts.push(acc);
361
+ acc = '';
362
+ continue;
363
+ }
364
+ acc += ch;
365
+ }
366
+ if (acc.trim() !== '')
367
+ parts.push(acc);
368
+ return parts;
369
+ };
370
+ const applyCd = (part) => {
371
+ // `(`/`{` open groups whose leading cd still runs (a brace group shares the CURRENT shell; a
372
+ // subshell's cd is over-approximated by design — REQ-528 Out). `builtin cd` and `command cd`
373
+ // ARE the real cd — round-1 adversarial harvest: the unprefixed matcher left the base behind
374
+ // while the shell moved.
375
+ const lead = part.replace(/^[\s({]+/, '').replace(/^(?:builtin|command(?:\s+-p)?)\s+/, '');
376
+ if (/^popd\b/.test(lead)) {
377
+ base = null;
378
+ return;
379
+ }
380
+ const m = /^(?:cd|chdir|pushd)(?:\s+([\s\S]*))?$/.exec(lead);
381
+ if (!m)
382
+ return;
383
+ const rawArg = (m[1] ?? '').trim();
384
+ if (rawArg === '') {
385
+ base = os.homedir();
386
+ return;
387
+ }
388
+ if (/[$`]/.test(rawArg)) {
389
+ base = null;
390
+ return;
391
+ }
392
+ const words = rawArg.match(/(?:[^\s'"]+|'[^']*'|"[^"]*")+/g) ?? [];
393
+ if (words.length !== 1) {
394
+ base = null;
395
+ return;
396
+ }
397
+ const arg = words[0].replace(/^"([^"]*)"$/, '$1').replace(/^'([^']*)'$/, '$1');
398
+ if (arg === '' || arg === '-') {
399
+ base = null;
400
+ return;
401
+ }
402
+ if (arg === '~') {
403
+ base = os.homedir();
404
+ return;
405
+ }
406
+ if (arg.startsWith('~/')) {
407
+ base = path.join(os.homedir(), arg.slice(2));
408
+ return;
409
+ }
410
+ if (base === null) {
411
+ const kind = absoluteKindOf(arg);
412
+ base = kind === null ? null : pathFlavorFor(arg, arg).resolve(arg);
413
+ return;
414
+ }
415
+ base = pathFlavorFor(base, arg).resolve(base, arg);
416
+ };
417
+ for (const piece of pieces) {
418
+ if (piece.kind === 'data') {
419
+ out.push({ text: piece.text, base });
420
+ continue;
421
+ }
422
+ if (piece.kind === 'prog') {
423
+ // A shell reading its program from stdin is a CHILD — its cds do not move the parent base.
424
+ out.push(...shellSegments(piece.text, base ?? startBase, depth + 1)
425
+ .map((s) => (base === null ? { ...s, base: null } : s)));
426
+ continue;
427
+ }
428
+ for (const part of splitTop(piece.text)) {
429
+ let rem = part;
430
+ for (const m of part.matchAll(PROG_STRING_RE)) {
431
+ const prog = m[1] ?? m[2];
432
+ if (prog === undefined || prog === '')
433
+ continue;
434
+ // Same child-shell rule as stdin programs; blank the program out of the parent text so its
435
+ // candidates are judged once, at the child's own bases.
436
+ out.push(...shellSegments(prog, base ?? startBase, depth + 1)
437
+ .map((s) => (base === null ? { ...s, base: null } : s)));
438
+ rem = rem.replace(`'${prog}'`, "''").replace(`"${prog}"`, '""');
439
+ }
440
+ applyCd(rem);
441
+ out.push({ text: rem, base });
442
+ }
443
+ }
444
+ return out;
445
+ }
303
446
  function resolvesInside(raw, roots, impl = path) {
304
447
  if (typeof raw !== 'string' || raw.length === 0 || roots.length === 0)
305
448
  return false;
@@ -535,11 +535,14 @@ function evaluateHook(input, specsDir, opts) {
535
535
  // class as HOLMES_ROLE. The GEMINI_API_KEY spelling is included because that is the
536
536
  // ecosystem-compatible name the resolver honors; the narrowed usage forms below keep
537
537
  // `grep GEMINI_API_KEY src/…` free exactly as they do for the HOLMES names.
538
- const SECRET = String.raw `(?:HOLMES_(?:LEDGER_KEY|APPROVAL|ROLE|GATE_BYPASS|SEMANTIC_API_KEY)|GEMINI_API_KEY|GOOGLE_API_KEY)`;
538
+ // @implements A-SPEC-532.2 — HOLMES_AUTONOMOUS_APPROVAL is the out-of-band autonomy switch; a
539
+ // session that could set it would self-grant autonomous approval, the same self-disarm the
540
+ // ROLE/GATE_BYPASS names are blocked for.
541
+ const SECRET = String.raw `(?:HOLMES_(?:LEDGER_KEY|APPROVAL|ROLE|GATE_BYPASS|SEMANTIC_API_KEY|AUTONOMOUS_APPROVAL)|GEMINI_API_KEY|GOOGLE_API_KEY)`;
539
542
  // Which of the two harms this is. Setting a role or a bypass is not reading a secret, it is
540
543
  // self-granting authority — reporting both as "reads the environment" sends an operator to
541
544
  // hunt a leak that never happened.
542
- const GRANTS_SELF = new RegExp(String.raw `\b(?:HOLMES_(?:ROLE|GATE_BYPASS)|HOLMES_SEMANTIC_API_KEY|GEMINI_API_KEY|GOOGLE_API_KEY)\b`).test(command);
545
+ const GRANTS_SELF = new RegExp(String.raw `\b(?:HOLMES_(?:ROLE|GATE_BYPASS|AUTONOMOUS_APPROVAL)|HOLMES_SEMANTIC_API_KEY|GEMINI_API_KEY|GOOGLE_API_KEY)\b`).test(command);
543
546
  const usesSecret =
544
547
  // @implements A-SPEC-477 — the credential file is the same secret at rest; reading,
545
548
  // copying or redirecting it is the harvest in file form. Path-usage only, so mentioning
@@ -655,22 +658,53 @@ function evaluateHook(input, specsDir, opts) {
655
658
  return m[i];
656
659
  return undefined;
657
660
  };
658
- const cfgCandidates = [];
659
- const cfgCollect = (re) => {
660
- for (const m of command.matchAll(re)) {
661
+ // @implements A-SPEC-528.1 — every shell-write candidate carries the EFFECTIVE base of the
662
+ // segment it appears in. The rules below used to hand relative candidates to `resolvesInside`
663
+ // with the project root as the base; a `cd` earlier in the command moves that base, and the
664
+ // mismatch was measured both ways (2026-09-03 probe C1~C11): five in-project writes spelled
665
+ // from a subdirectory walked through, three legitimate out-of-tree writes were denied.
666
+ const segments = (0, write_target_1.shellSegments)(command, opts.projectRoot);
667
+ const isTilde = (p) => p === '~' || p.startsWith('~/');
668
+ // Relative candidate in a segment whose base is unknowable (dynamic cd argument): 'unknown',
669
+ // and the caller fails CLOSED — but only when such a candidate exists, so `cd "$X" && npm
670
+ // test` stays free.
671
+ const judgeCand = (c, roots) => {
672
+ if ((0, write_target_1.absoluteKindOf)(c.raw) !== null || isTilde(c.raw))
673
+ return (0, write_target_1.resolvesInside)(c.raw, roots) ? 'in' : 'out';
674
+ if (c.base === null)
675
+ return 'unknown';
676
+ const pp = (0, write_target_1.pathFlavorFor)(c.base, c.raw);
677
+ const abs = pp.resolve(c.base, c.raw);
678
+ const kind = (0, write_target_1.absoluteKindOf)(abs);
679
+ return (0, write_target_1.resolvesInside)(abs, roots, kind === 'drive' || kind === 'unc' ? path.win32 : path.posix) ? 'in' : 'out';
680
+ };
681
+ const collectSeg = (seg, re, list) => {
682
+ for (const m of seg.text.matchAll(re)) {
661
683
  const g = firstGroup(m);
662
684
  if (g !== undefined)
663
- cfgCandidates.push(g);
685
+ list.push({ raw: g, base: seg.base });
664
686
  }
665
687
  };
666
- cfgCollect(new RegExp(`(?:>{1,2}|\\btee\\b(?:\\s+-a)?\\s)\\s*${pathAlt(CONFIG_FILE)}`, 'g'));
667
- cfgCollect(new RegExp(`\\b(?:cp|mv|install|rsync|ln|dd|truncate|chmod|chown)\\b[^;|&]*?${pathAlt(CONFIG_FILE)}`, 'g'));
668
- cfgCollect(new RegExp(`\\bsed\\b[^;|&]*\\s-i\\b[^;|&]*?${pathAlt(CONFIG_FILE)}`, 'g'));
688
+ const cfgCandidates = [];
689
+ for (const seg of segments) {
690
+ collectSeg(seg, new RegExp(`(?:>{1,2}|\\btee\\b(?:\\s+-a)?\\s)\\s*${pathAlt(CONFIG_FILE)}`, 'g'), cfgCandidates);
691
+ collectSeg(seg, new RegExp(`\\b(?:cp|mv|install|rsync|ln|dd|truncate|chmod|chown)\\b[^;|&]*?${pathAlt(CONFIG_FILE)}`, 'g'), cfgCandidates);
692
+ collectSeg(seg, new RegExp(`\\bsed\\b[^;|&]*\\s-i\\b[^;|&]*?${pathAlt(CONFIG_FILE)}`, 'g'), cfgCandidates);
693
+ }
694
+ // Interpreter one-liner paths keep their whole-text matching (A-SPEC-448/450); a RELATIVE one
695
+ // is judged as a may-analysis over every determinate base plus the start base — deny-only
696
+ // over-approximation, sealed in A-SPEC-528.1 §3.
697
+ const detBases = [...new Set(segments.map((s) => s.base).filter((b) => b !== null)), opts.projectRoot];
698
+ const hasUnknownBase = segments.some((s) => s.base === null);
699
+ const expandInterp = (raw) => (0, write_target_1.absoluteKindOf)(raw) !== null || isTilde(raw)
700
+ ? [{ raw, base: opts.projectRoot }]
701
+ : [...detBases.map((b) => ({ raw, base: b })), ...(hasUnknownBase ? [{ raw, base: null }] : [])];
669
702
  // @implements A-SPEC-447 — one containment predicate, shared with the code rule below.
670
703
  const SESSION_ROOTS = [opts.projectRoot, path.join(os.homedir(), '.claude')];
671
704
  const CFG_RE = new RegExp(`${CONFIG_FILE}$`);
672
- const configWrite = [...cfgCandidates, ...interpreterWrites.filter((p) => CFG_RE.test(p))]
673
- .some((raw) => (0, write_target_1.resolvesInside)(raw, SESSION_ROOTS));
705
+ const cfgVerdicts = [...cfgCandidates, ...interpreterWrites.filter((p) => CFG_RE.test(p)).flatMap(expandInterp)]
706
+ .map((c) => judgeCand(c, SESSION_ROOTS));
707
+ const configWrite = cfgVerdicts.includes('in');
674
708
  // Residual, stated rather than papered over: an INTERPRETER can write these files too. A first
675
709
  // draft matched `node|python|perl|ruby` anywhere near the pattern and denied
676
710
  // `node -e "console.log(cfg.env.name)"` — reading a property named `env`. A rule that cannot
@@ -694,46 +728,55 @@ function evaluateHook(input, specsDir, opts) {
694
728
  // extension part and `pathAlt` supplies the prefix and the quoting alternatives.
695
729
  const EXT_TAIL = `\\.${CODE_EXT}`;
696
730
  const candidates = [];
697
- const collect = (re) => {
698
- for (const m of command.matchAll(re)) {
699
- const g = firstGroup(m);
700
- if (g !== undefined)
701
- candidates.push(g);
702
- }
703
- };
704
- collect(new RegExp(`(?:>{1,2}|\\btee\\b(?:\\s+-a)?\\s)\\s*${pathAlt(EXT_TAIL)}`, 'g'));
705
- collect(new RegExp(`\\btouch\\s+[^;|&]*?${pathAlt(EXT_TAIL)}`, 'g'));
706
- collect(new RegExp(`\\bsed\\b[^;|&]*\\s-i\\b[^;|&]*?${pathAlt(EXT_TAIL)}`, 'g'));
731
+ for (const seg of segments) {
732
+ collectSeg(seg, new RegExp(`(?:>{1,2}|\\btee\\b(?:\\s+-a)?\\s)\\s*${pathAlt(EXT_TAIL)}`, 'g'), candidates);
733
+ collectSeg(seg, new RegExp(`\\btouch\\s+[^;|&]*?${pathAlt(EXT_TAIL)}`, 'g'), candidates);
734
+ collectSeg(seg, new RegExp(`\\bsed\\b[^;|&]*\\s-i\\b[^;|&]*?${pathAlt(EXT_TAIL)}`, 'g'), candidates);
735
+ }
707
736
  // @implements A-SPEC-449 — the copy/move family. The CONFIG rule has carried it since
708
737
  // A-SPEC-191 §28 ("a gate that names one syntax for an act is a gate over that syntax, not
709
738
  // over the act"); the code rule never got it, and `cp /tmp/e.ts src/a.ts` walked through.
710
739
  // Only the TARGET counts: `cp src/a.ts /tmp/backup.ts` reads the project and writes outside,
711
740
  // which is an ordinary backup and not this gate's business.
712
- for (const seg of command.split(/[;|&]+/)) {
713
- if (!/\b(?:cp|mv|install|rsync|ln|dd)\b/.test(seg))
714
- continue;
715
- const of = seg.match(new RegExp(`\\bof=${pathAlt(EXT_TAIL)}`));
716
- if (of) {
717
- const g = firstGroup(of);
718
- if (g !== undefined) {
719
- candidates.push(g);
741
+ // (A data heredoc body may still carry separators inside one segment, so the per-segment
742
+ // text is split the way the whole command used to be — the base is the segment's either way.)
743
+ for (const seg of segments) {
744
+ for (const sub of seg.text.split(/[;|&]+/)) {
745
+ if (!/\b(?:cp|mv|install|rsync|ln|dd)\b/.test(sub))
720
746
  continue;
747
+ const of = sub.match(new RegExp(`\\bof=${pathAlt(EXT_TAIL)}`));
748
+ if (of) {
749
+ const g = firstGroup(of);
750
+ if (g !== undefined) {
751
+ candidates.push({ raw: g, base: seg.base });
752
+ continue;
753
+ }
721
754
  }
755
+ const all = [...sub.matchAll(new RegExp(pathAlt(EXT_TAIL), 'g'))]
756
+ .map(firstGroup).filter((x) => x !== undefined);
757
+ if (all.length >= 2)
758
+ candidates.push({ raw: all[all.length - 1], base: seg.base }); // last path is the destination
722
759
  }
723
- const all = [...seg.matchAll(new RegExp(pathAlt(EXT_TAIL), 'g'))]
724
- .map(firstGroup).filter((x) => x !== undefined);
725
- if (all.length >= 2)
726
- candidates.push(all[all.length - 1]); // last path is the destination
727
760
  }
728
761
  // @implements A-SPEC-447 — same predicate as the config rule; it lived in two copies, which
729
762
  // is how one of two gets fixed.
730
763
  const CODE_RE = new RegExp(`\\.${CODE_EXT}$`);
731
- const shellCodeWrite = [...candidates, ...interpreterWrites.filter((p) => CODE_RE.test(p))]
732
- .some((raw) => (0, write_target_1.resolvesInside)(raw, [opts.projectRoot]));
733
- if (shellCodeWrite) {
764
+ const codeVerdicts = [...candidates, ...interpreterWrites.filter((p) => CODE_RE.test(p)).flatMap(expandInterp)]
765
+ .map((c) => judgeCand(c, [opts.projectRoot]));
766
+ // @implements A-SPEC-528.1 — an unresolvable base under a governed relative write candidate
767
+ // fails CLOSED: the gate cannot know where the file lands, and guessing is the bypass this
768
+ // slice closes. Config candidates get the same fail-closed unconditionally (the config rule
769
+ // has never depended on governance); code candidates stay dormant on a spec-less repo.
770
+ const UNRESOLVED_BASE_DENY = '[Holmes-Kit] shell write to a relative path cannot be located: an earlier cd has a target the gate cannot resolve statically (dynamic argument) — use an absolute path, the gated Write/Edit tools, or out-of-band approval';
771
+ if (cfgVerdicts.includes('unknown')) {
772
+ return { permissionDecision: 'deny', permissionDecisionReason: UNRESOLVED_BASE_DENY };
773
+ }
774
+ if (codeVerdicts.includes('in') || codeVerdicts.includes('unknown')) {
734
775
  const governed = readSpecsSync(specsDir).some((s) => s.status === 'approved');
735
776
  if (governed) {
736
- return { permissionDecision: 'deny', permissionDecisionReason: '[Holmes-Kit] shell write to a project code file bypasses the No-Spec-No-Code gate — use the Write/Edit tools (gated) or supply out-of-band approval' };
777
+ return codeVerdicts.includes('in')
778
+ ? { permissionDecision: 'deny', permissionDecisionReason: '[Holmes-Kit] shell write to a project code file bypasses the No-Spec-No-Code gate — use the Write/Edit tools (gated) or supply out-of-band approval' }
779
+ : { permissionDecision: 'deny', permissionDecisionReason: UNRESOLVED_BASE_DENY };
737
780
  }
738
781
  }
739
782
  }
@@ -1231,10 +1274,32 @@ function evaluateHook(input, specsDir, opts) {
1231
1274
  // edit to that id. Only when the file carries no on-disk anchor (new file, or first anchoring) does
1232
1275
  // the incoming payload supply it. Found live: editing a test file whose fixture named an unapproved
1233
1276
  // spec was denied even though the file's own anchor is approved.
1234
- const ANCHOR_RE = /@implements\s+(A-SPEC-\d{3,}(?:\.\d+)?)/;
1235
- const payloadAnchor = ANCHOR_RE.exec([input.tool_input.content ?? '', input.tool_input.new_string ?? ''].join('\n'));
1236
- const diskAnchor = ANCHOR_RE.exec(onDiskContent);
1237
- const m = diskAnchor ?? payloadAnchor;
1277
+ // @implements A-SPEC-529.1
1278
+ // The GOVERNING anchor is a SET, not the first regex match. Comma lists are legal (S-503.1), so
1279
+ // `@implements A-SPEC-100, A-SPEC-200` claims both — and every id after the first used to be
1280
+ // absent from the stale gate and from phaseCheck (measured 2026-09-03: a broken seal on the
1281
+ // second id passed). The shared parser is consumed through `stripStringLiterals`, the same
1282
+ // preprocessing the claim gate got in S-504.1, so a fixture string anchor no longer governs.
1283
+ // Disk-anchor precedence is preserved as a set: a non-empty disk set governs, else the payload.
1284
+ const { anchorSpecIds: govAnchorIds, stripStringLiterals: govStrip } = require('../rtm/anchor-ids');
1285
+ const uniq = (xs) => [...new Set(xs)];
1286
+ // @implements A-SPEC-529.2 — the preprocessing branches on FILE FORMAT. A-SPEC-529.1 stripped
1287
+ // string literals so a code fixture like `const s = "@implements A-SPEC-999"` could not become
1288
+ // the governing anchor; but a JSON file's ONLY way to carry an anchor is a string value
1289
+ // (`"//": "@implements A-SPEC-209"` in package.json), and stripping over-dropped it — measured
1290
+ // 2026-09-03, a release package.json edit was refused with "A-SPEC(unspecified)". Code formats
1291
+ // (line-comment anchors) still strip; JSON/YAML/TOML/config and unknown extensions read raw
1292
+ // (over-inclusion is safe — an anchored-but-unapproved id is still refused downstream).
1293
+ const GOV_CODE_EXT = /\.(?:ts|tsx|mts|cts|js|jsx|mjs|cjs|py|go|rs|java|kt|cs|cc|cxx|cpp|hh|hpp|rb|php|swift)$/i;
1294
+ const govPrep = (s) => (GOV_CODE_EXT.test(relPath) ? govStrip(s) : s);
1295
+ const diskGovIds = uniq(govAnchorIds(govPrep(onDiskContent)));
1296
+ const payloadGovIds = uniq(govAnchorIds(govPrep([input.tool_input.content ?? '', input.tool_input.new_string ?? ''].join('\n'))));
1297
+ const governingIds = diskGovIds.length > 0 ? diskGovIds : payloadGovIds;
1298
+ // The single "primary" id kept for message templates and phaseCheck's targetAspecId default: the
1299
+ // first governing id, which is exactly what `m?.[1]` used to be for a single-anchor file.
1300
+ const m = governingIds.length > 0
1301
+ ? Object.assign([`@implements ${governingIds[0]}`, governingIds[0]], { index: 0, input: '', groups: undefined })
1302
+ : null;
1238
1303
  // RE-ANCHORING scope (review C6) moved into judgeScope below (S-508.1): every payload anchor the
1239
1304
  // disk does not carry is an ADMISSION, judged against the incoming spec's gate — the C6 property
1240
1305
  // (a rewrite of the anchor line is judged under the NEW spec) is preserved there, now for the
@@ -1268,11 +1333,15 @@ function evaluateHook(input, specsDir, opts) {
1268
1333
  }
1269
1334
  return null;
1270
1335
  };
1271
- // The qualifying T-SPEC matters for WRITE_CODE too a stale test spec must not bless code.
1272
- const staleTarget = sealProblem(m?.[1]) ?? (action === 'WRITE_CODE'
1273
- ? (specs.filter((s) => s.type === 'T-SPEC' && s.status === 'approved' && m?.[1] && s.dependsOn.includes(m[1]))
1274
- .map((s) => sealProblem(s.id)).find(Boolean) ?? null)
1275
- : null);
1336
+ // @implements A-SPEC-529.1 the stale gate runs over the WHOLE governing set: any anchored spec
1337
+ // whose seal is broken, and (for WRITE_CODE) any of that spec's qualifying T-SPECs, blocks — and
1338
+ // the message names the one that failed, not the first anchor. The single-anchor path is a set
1339
+ // of one, byte-identical to the previous `m?.[1]` behaviour.
1340
+ const staleTarget = (governingIds.map((id) => sealProblem(id)).find(Boolean) ?? null)
1341
+ ?? (action === 'WRITE_CODE'
1342
+ ? (specs.filter((s) => s.type === 'T-SPEC' && s.status === 'approved' && governingIds.some((id) => s.dependsOn.includes(id)))
1343
+ .map((s) => sealProblem(s.id)).find(Boolean) ?? null)
1344
+ : null);
1276
1345
  // @implements A-SPEC-133 — the override is authorized by a token covering the code write.
1277
1346
  const codeWriteCovered = (0, risk_gate_1.approvalCovers)(weApproval, { kind: 'code-write', target: relPath }, nowTs);
1278
1347
  if (staleTarget && !codeWriteCovered) {
@@ -1306,7 +1375,21 @@ function evaluateHook(input, specsDir, opts) {
1306
1375
  };
1307
1376
  }
1308
1377
  }
1309
- const res = (0, phase_1.phaseCheck)(action, { specs, targetAspecId: m?.[1] });
1378
+ // @implements A-SPEC-529.1 phaseCheck runs for EVERY governing id; the first failure is the
1379
+ // verdict, and its id flows into the refusal template (so the author sees the anchor that is
1380
+ // actually unsatisfied, not merely the first one on the file). An empty set keeps the prior
1381
+ // single call with an undefined target. Deterministic: governing-set order is document order.
1382
+ const phaseTargets = governingIds.length > 0 ? governingIds : [undefined];
1383
+ let failedTarget;
1384
+ let res = (0, phase_1.phaseCheck)(action, { specs, targetAspecId: phaseTargets[0] });
1385
+ for (const tid of phaseTargets) {
1386
+ const r = (0, phase_1.phaseCheck)(action, { specs, targetAspecId: tid });
1387
+ if (r.decision === 'deny') {
1388
+ res = r;
1389
+ failedTarget = tid;
1390
+ break;
1391
+ }
1392
+ }
1310
1393
  if (res.decision === 'deny') {
1311
1394
  // @implements A-SPEC-175
1312
1395
  // When the spec tree is gone from a project the ledger remembers approving, the generic
@@ -1315,7 +1398,7 @@ function evaluateHook(input, specsDir, opts) {
1315
1398
  // the phase gate's own verdict is still what denied this.
1316
1399
  const lost = specs.length === 0 && !fs.existsSync(specsDir)
1317
1400
  && (0, governance_history_1.hasGovernanceHistory)(path.resolve(specsDir, '..', '..'));
1318
- const targetSpecId = m?.[1] ?? 'A-SPEC-XXX';
1401
+ const targetSpecId = failedTarget ?? m?.[1] ?? 'A-SPEC-XXX';
1319
1402
  const prescriptiveGuide = ` — Next Action (DO NOT write workaround scripts in /tmp): Step 1: Call 'spec_next({})' to verify slice state. Step 2: Call 'spec_approve({ id: "${targetSpecId}" })' to seal spec. Step 3: Ensure '// @implements ${targetSpecId}' is on line 1 of target file.`;
1320
1403
  return {
1321
1404
  permissionDecision: 'deny',
@@ -0,0 +1,23 @@
1
+ import { InstallMode } from '../update/update-notice';
2
+ export interface SessionStartInput {
3
+ version: string;
4
+ home: string;
5
+ now: number;
6
+ env: NodeJS.ProcessEnv;
7
+ readFile: (p: string) => string;
8
+ mode: InstallMode;
9
+ }
10
+ export interface SessionStartOutput {
11
+ hookSpecificOutput: {
12
+ hookEventName: 'SessionStart';
13
+ additionalContext: string;
14
+ };
15
+ shouldRefresh: boolean;
16
+ }
17
+ /** node_modules → local dependency; anything else is treated as a source checkout (conservative:
18
+ * a source install shows no update command, which is safer than a wrong one). A global npx pin is
19
+ * detected by the caller from argv/exec path; here the package root alone distinguishes local-dep. */
20
+ export declare function detectInstallMode(packageRoot: string): InstallMode;
21
+ /** Pure: build the banner output and decide whether a refresh should fire. No I/O beyond the
22
+ * injected readFile; the caller performs the detached spawn when shouldRefresh is true. */
23
+ export declare function buildSessionStartOutput(input: SessionStartInput): SessionStartOutput;
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.detectInstallMode = detectInstallMode;
37
+ exports.buildSessionStartOutput = buildSessionStartOutput;
38
+ // @implements A-SPEC-531.2
39
+ // The SessionStart banner hook. It emits the intro (+ optional update notice) as additionalContext,
40
+ // which Claude Code delivers to BOTH the human transcript and the agent context — the "human and
41
+ // agent" delivery REQ-531 asks for. The update refresh is a DETACHED, fire-and-forget child so the
42
+ // session start is never delayed; its result shows up on the NEXT session.
43
+ //
44
+ // FAIL-OPEN: this is a banner, not a gate. Any error yields an empty, harmless output and exit 0 —
45
+ // the session must always start.
46
+ const fs = __importStar(require("node:fs"));
47
+ const os = __importStar(require("node:os"));
48
+ const path = __importStar(require("node:path"));
49
+ const node_child_process_1 = require("node:child_process");
50
+ const update_notice_1 = require("../update/update-notice");
51
+ /** node_modules → local dependency; anything else is treated as a source checkout (conservative:
52
+ * a source install shows no update command, which is safer than a wrong one). A global npx pin is
53
+ * detected by the caller from argv/exec path; here the package root alone distinguishes local-dep. */
54
+ function detectInstallMode(packageRoot) {
55
+ const folded = packageRoot.replace(/\\/g, '/');
56
+ return /(?:^|\/)node_modules\//.test(folded) ? 'local-dep' : 'source';
57
+ }
58
+ /** Pure: build the banner output and decide whether a refresh should fire. No I/O beyond the
59
+ * injected readFile; the caller performs the detached spawn when shouldRefresh is true. */
60
+ function buildSessionStartOutput(input) {
61
+ const cached = (0, update_notice_1.readCache)(input.home, input.readFile);
62
+ const additionalContext = (0, update_notice_1.composeBanner)({ current: input.version, cached, mode: input.mode, npmUrl: update_notice_1.NPM_URL });
63
+ const shouldRefresh = (0, update_notice_1.shouldQuery)(input.env) && (0, update_notice_1.cacheIsStale)(cached, input.now);
64
+ return { hookSpecificOutput: { hookEventName: 'SessionStart', additionalContext }, shouldRefresh };
65
+ }
66
+ // The package root, derived from this file's location (dist/holmes/hooks/session-start.js → 3 up).
67
+ function pkgRootFromEntry() {
68
+ return path.resolve(__dirname, '..', '..', '..');
69
+ }
70
+ function pkgVersion() {
71
+ try {
72
+ const raw = fs.readFileSync(path.join(pkgRootFromEntry(), 'package.json'), 'utf8');
73
+ return JSON.parse(raw).version ?? '0.0.0';
74
+ }
75
+ catch {
76
+ return '0.0.0';
77
+ }
78
+ }
79
+ // CLI entry: read the hook JSON on stdin (unused beyond triggering), emit the banner, and — when
80
+ // allowed — fire the detached refresh. Wrapped so any failure is an empty output + exit 0.
81
+ if (require.main === module) {
82
+ let buf = '';
83
+ process.stdin.on('data', (c) => (buf += c));
84
+ process.stdin.on('end', () => {
85
+ try {
86
+ const out = buildSessionStartOutput({
87
+ version: pkgVersion(),
88
+ home: os.homedir(),
89
+ now: Date.now(),
90
+ env: process.env,
91
+ readFile: (p) => fs.readFileSync(p, 'utf8'),
92
+ mode: detectInstallMode(pkgRootFromEntry()),
93
+ });
94
+ process.stdout.write(JSON.stringify({ hookSpecificOutput: out.hookSpecificOutput }));
95
+ if (out.shouldRefresh) {
96
+ // Detached, unref'd child so the session start does not wait on the network. The refresh
97
+ // subcommand queries the registry (dist-tags.latest), times out fast, and writes the cache;
98
+ // any failure there is silent. Reads only public metadata — no spec text, no egress.
99
+ try {
100
+ const child = (0, node_child_process_1.spawn)(process.execPath, [__filename, '--refresh'], { detached: true, stdio: 'ignore' });
101
+ child.unref();
102
+ }
103
+ catch { /* refresh is best-effort */ }
104
+ }
105
+ }
106
+ catch {
107
+ // Fail-open: emit nothing and let the session start.
108
+ process.stdout.write('{}');
109
+ }
110
+ });
111
+ }
@@ -1,5 +1,6 @@
1
1
  import { PendingRequest } from '../governance/approval-queue';
2
2
  import { Spec } from '../spec/spec-parser';
3
+ import type { TestOutcome } from '../review/test-runner';
3
4
  /**
4
5
  * @implements A-SPEC-100.2
5
6
  * Stop-hook governance gate (Phase-2 #1: push, not pull).
@@ -81,7 +82,26 @@ export interface StopEvidence {
81
82
  * catches it already existed inside `rechainLedger` and ran from the CLI only.
82
83
  */
83
84
  rolledBackLedgers?: string[];
85
+ /**
86
+ * @implements A-SPEC-534.4
87
+ * ART-8 RED-first evidence. `changedAspecs` are the A-SPECs whose source is dirty this turn;
88
+ * `outcomesByAspec` are their recorded outcomes at the current baseline HEAD; `redFirstMode` is the
89
+ * config posture. `strict` blocks via the constitution; `track` records to `tracked` without
90
+ * blocking; `off`/absent does nothing.
91
+ */
92
+ changedAspecs?: string[];
93
+ outcomesByAspec?: Record<string, Array<{
94
+ outcome: TestOutcome;
95
+ ts: string;
96
+ }>>;
97
+ redFirstMode?: 'strict' | 'track' | 'off';
84
98
  }
99
+ /**
100
+ * @implements A-SPEC-534.4
101
+ * ART-8 evidence (I/O half): the A-SPECs whose DIRTY source files carry an @implements anchor. git is
102
+ * a refinement — no repository means `undefined` (no signal), never a false clean.
103
+ */
104
+ export declare function changedAnchoredAspecs(root: string): string[] | undefined;
85
105
  /**
86
106
  * @implements A-SPEC-452
87
107
  * ART-1 evidence: which changed source files claim nothing.
@@ -127,6 +147,10 @@ export declare function evaluateStop(specs: Spec[], evidence?: StopEvidence): {
127
147
  article: string;
128
148
  detail: string;
129
149
  }[];
150
+ tracked?: {
151
+ article: string;
152
+ detail: string;
153
+ }[];
130
154
  };
131
155
  /**
132
156
  * @implements A-SPEC-134