@codyswann/lisa 8.2.4 → 8.2.6

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 (94) hide show
  1. package/all/copy-overwrite/scripts/check-npm-publish-landed.mjs +10 -0
  2. package/all/copy-overwrite/scripts/check-threshold-ratchet.mjs +280 -46
  3. package/all/copy-overwrite/scripts/npm-updater-helper-graph.json +3 -3
  4. package/all/copy-overwrite/scripts/threshold-ratchet-compare.mjs +433 -21
  5. package/all/copy-overwrite/scripts/threshold-ratchet-families.mjs +77 -4
  6. package/dist/core/lisa-owned-hash-ledger.json +58 -2
  7. package/dist/core/nightly-e2e-guard-behavior-certificate.js +2 -2
  8. package/dist/core/upstream-evidence-manifest.json +36 -15
  9. package/package.json +4 -4
  10. package/plugins/lisa/.claude-plugin/plugin.json +1 -1
  11. package/plugins/lisa/.codex-plugin/plugin.json +1 -1
  12. package/plugins/lisa/hooks/threshold-ratchet-compare.mjs +433 -21
  13. package/plugins/lisa/hooks/threshold-ratchet-families.mjs +77 -4
  14. package/plugins/lisa/hooks/threshold-ratchet.mjs +280 -46
  15. package/plugins/lisa-agy/plugin.json +1 -1
  16. package/plugins/lisa-cdk/.claude-plugin/plugin.json +1 -1
  17. package/plugins/lisa-cdk/.codex-plugin/plugin.json +1 -1
  18. package/plugins/lisa-cdk-agy/plugin.json +1 -1
  19. package/plugins/lisa-cdk-copilot/.claude-plugin/plugin.json +1 -1
  20. package/plugins/lisa-cdk-cursor/.claude-plugin/plugin.json +1 -1
  21. package/plugins/lisa-cdk-devin/.claude-plugin/plugin.json +1 -1
  22. package/plugins/lisa-cdk-devin/.devin-plugin/plugin.json +1 -1
  23. package/plugins/lisa-copilot/.claude-plugin/plugin.json +1 -1
  24. package/plugins/lisa-copilot/hooks/threshold-ratchet-compare.mjs +433 -21
  25. package/plugins/lisa-copilot/hooks/threshold-ratchet-families.mjs +77 -4
  26. package/plugins/lisa-copilot/hooks/threshold-ratchet.mjs +280 -46
  27. package/plugins/lisa-cursor/.claude-plugin/plugin.json +1 -1
  28. package/plugins/lisa-cursor/hooks/threshold-ratchet-compare.mjs +433 -21
  29. package/plugins/lisa-cursor/hooks/threshold-ratchet-families.mjs +77 -4
  30. package/plugins/lisa-cursor/hooks/threshold-ratchet.mjs +280 -46
  31. package/plugins/lisa-devin/.claude-plugin/plugin.json +1 -1
  32. package/plugins/lisa-devin/.devin-plugin/plugin.json +1 -1
  33. package/plugins/lisa-devin/hooks/threshold-ratchet-compare.mjs +433 -21
  34. package/plugins/lisa-devin/hooks/threshold-ratchet-families.mjs +77 -4
  35. package/plugins/lisa-devin/hooks/threshold-ratchet.mjs +280 -46
  36. package/plugins/lisa-expo/.claude-plugin/plugin.json +1 -1
  37. package/plugins/lisa-expo/.codex-plugin/plugin.json +1 -1
  38. package/plugins/lisa-expo-agy/plugin.json +1 -1
  39. package/plugins/lisa-expo-copilot/.claude-plugin/plugin.json +1 -1
  40. package/plugins/lisa-expo-cursor/.claude-plugin/plugin.json +1 -1
  41. package/plugins/lisa-expo-devin/.claude-plugin/plugin.json +1 -1
  42. package/plugins/lisa-expo-devin/.devin-plugin/plugin.json +1 -1
  43. package/plugins/lisa-harper-fabric/.claude-plugin/plugin.json +1 -1
  44. package/plugins/lisa-harper-fabric/.codex-plugin/plugin.json +1 -1
  45. package/plugins/lisa-harper-fabric-agy/plugin.json +1 -1
  46. package/plugins/lisa-harper-fabric-copilot/.claude-plugin/plugin.json +1 -1
  47. package/plugins/lisa-harper-fabric-cursor/.claude-plugin/plugin.json +1 -1
  48. package/plugins/lisa-harper-fabric-devin/.claude-plugin/plugin.json +1 -1
  49. package/plugins/lisa-harper-fabric-devin/.devin-plugin/plugin.json +1 -1
  50. package/plugins/lisa-nestjs/.claude-plugin/plugin.json +1 -1
  51. package/plugins/lisa-nestjs/.codex-plugin/plugin.json +1 -1
  52. package/plugins/lisa-nestjs-agy/plugin.json +1 -1
  53. package/plugins/lisa-nestjs-copilot/.claude-plugin/plugin.json +1 -1
  54. package/plugins/lisa-nestjs-cursor/.claude-plugin/plugin.json +1 -1
  55. package/plugins/lisa-nestjs-devin/.claude-plugin/plugin.json +1 -1
  56. package/plugins/lisa-nestjs-devin/.devin-plugin/plugin.json +1 -1
  57. package/plugins/lisa-phaser/.claude-plugin/plugin.json +1 -1
  58. package/plugins/lisa-phaser/.codex-plugin/plugin.json +1 -1
  59. package/plugins/lisa-phaser-agy/plugin.json +1 -1
  60. package/plugins/lisa-phaser-copilot/.claude-plugin/plugin.json +1 -1
  61. package/plugins/lisa-phaser-cursor/.claude-plugin/plugin.json +1 -1
  62. package/plugins/lisa-phaser-devin/.claude-plugin/plugin.json +1 -1
  63. package/plugins/lisa-phaser-devin/.devin-plugin/plugin.json +1 -1
  64. package/plugins/lisa-rails/.claude-plugin/plugin.json +1 -1
  65. package/plugins/lisa-rails/.codex-plugin/plugin.json +1 -1
  66. package/plugins/lisa-rails-agy/plugin.json +1 -1
  67. package/plugins/lisa-rails-copilot/.claude-plugin/plugin.json +1 -1
  68. package/plugins/lisa-rails-cursor/.claude-plugin/plugin.json +1 -1
  69. package/plugins/lisa-rails-devin/.claude-plugin/plugin.json +1 -1
  70. package/plugins/lisa-rails-devin/.devin-plugin/plugin.json +1 -1
  71. package/plugins/lisa-typescript/.claude-plugin/plugin.json +1 -1
  72. package/plugins/lisa-typescript/.codex-plugin/plugin.json +1 -1
  73. package/plugins/lisa-typescript-agy/plugin.json +1 -1
  74. package/plugins/lisa-typescript-copilot/.claude-plugin/plugin.json +1 -1
  75. package/plugins/lisa-typescript-cursor/.claude-plugin/plugin.json +1 -1
  76. package/plugins/lisa-typescript-devin/.claude-plugin/plugin.json +1 -1
  77. package/plugins/lisa-typescript-devin/.devin-plugin/plugin.json +1 -1
  78. package/plugins/lisa-wiki/.claude-plugin/plugin.json +1 -1
  79. package/plugins/lisa-wiki/.codex-plugin/plugin.json +1 -1
  80. package/plugins/lisa-wiki-agy/plugin.json +1 -1
  81. package/plugins/lisa-wiki-copilot/.claude-plugin/plugin.json +1 -1
  82. package/plugins/lisa-wiki-cursor/.claude-plugin/plugin.json +1 -1
  83. package/plugins/lisa-wiki-devin/.claude-plugin/plugin.json +1 -1
  84. package/plugins/lisa-wiki-devin/.devin-plugin/plugin.json +1 -1
  85. package/plugins/src/base/hooks/threshold-ratchet-compare.mjs +433 -21
  86. package/plugins/src/base/hooks/threshold-ratchet-families.mjs +77 -4
  87. package/plugins/src/base/hooks/threshold-ratchet.mjs +280 -46
  88. package/rails/copy-overwrite/scripts/check-threshold-ratchet.mjs +280 -46
  89. package/rails/copy-overwrite/scripts/threshold-ratchet-compare.mjs +433 -21
  90. package/rails/copy-overwrite/scripts/threshold-ratchet-families.mjs +77 -4
  91. package/typescript/copy-contents/.husky/pre-commit +3 -2
  92. package/typescript/copy-overwrite/scripts/check-threshold-ratchet.mjs +280 -46
  93. package/typescript/copy-overwrite/scripts/threshold-ratchet-compare.mjs +433 -21
  94. package/typescript/copy-overwrite/scripts/threshold-ratchet-families.mjs +77 -4
@@ -35,7 +35,21 @@
35
35
  * reason, only when the regression is justified; a person can reverse it by
36
36
  * removing the entry. Honored ONLY from the baseline side (HEAD /
37
37
  * merge-base), never from the change under review — an agent cannot grant
38
- * itself an exception in the same change that weakens a gate. `key: "*"`
38
+ * itself an exception in the same change that weakens a gate. So an exception
39
+ * takes two changes, and the first has to be able to land (#5099): a change
40
+ * whose only findings are added entries, each with a reason and an `until`
41
+ * day that has not passed, is ACCEPTED in every mode with a notice, and
42
+ * exempts nothing until it is merged. An entry that arrives with any other
43
+ * finding is refused. Under `humanGate.mode: "strict"` — read from the
44
+ * baseline, like the allow list — an added entry is refused as before: there
45
+ * an exception is a person's decision, and no layer of this check can see
46
+ * that a person approved. See `settleAddedExceptions`.
47
+ *
48
+ * "Merged" has to mean the same thing at commit as it does in a pull request.
49
+ * `--staged` and `--hook` baseline against HEAD, which is whatever the branch
50
+ * last committed, so there an entry counts only when a protected branch
51
+ * (the remote's default branch, or a `deploy.branches` branch) also holds it
52
+ * as the remote last reported it. See `protectedBranchRefs`. `key: "*"`
39
53
  * allows every key in the file, and reports as file-wide so it is not mistaken
40
54
  * for a key-scoped one. A declared threshold leaves by this same list and by
41
55
  * nothing else: `file` is `.lisa.config.json` and `key` is
@@ -67,13 +81,18 @@ import { fileURLToPath } from "node:url";
67
81
  import {
68
82
  extractAllowEntries,
69
83
  familyFor,
84
+ humanGateModeOfText,
70
85
  parseJson,
71
86
  } from "./threshold-ratchet-families.mjs";
72
87
  import {
73
88
  applyAllowList,
74
89
  compareFile,
75
90
  describeAllowList,
91
+ describeNearMisses,
92
+ describeUnmergedCover,
76
93
  formatReport,
94
+ settleAddedExceptions,
95
+ splitMergedEntries,
77
96
  } from "./threshold-ratchet-compare.mjs";
78
97
 
79
98
  /**
@@ -105,6 +124,9 @@ const GIT_LOCATIONS = [
105
124
  ];
106
125
  const GIT = GIT_LOCATIONS.find(candidate => fs.existsSync(candidate)) ?? "git";
107
126
 
127
+ /** Where the remote-tracking refs of `origin` live. */
128
+ const REMOTE_REFS = "refs/remotes/origin/";
129
+
108
130
  /** Git flag shared by every changed-file listing. */
109
131
  const NAME_ONLY = "--name-only";
110
132
  /**
@@ -195,7 +217,16 @@ function resolvePlan(mode, root, baseRef, onlyFiles) {
195
217
  baselineRef: "HEAD",
196
218
  readCurrent: f => {
197
219
  try {
198
- return fs.readFileSync(path.join(root, f), "utf-8");
220
+ // Inside the repository only, by REAL location: the name can come
221
+ // from an allow entry, and a `..` segment or a symlink must not make
222
+ // this read a file somewhere else and print what it holds.
223
+ const home = fs.realpathSync(root);
224
+ const target = fs.realpathSync(path.resolve(root, f));
225
+ if (!target.startsWith(home + path.sep)) return null;
226
+ // Regular files only. Reading a named pipe would block for ever, and
227
+ // a hook that hangs wedges the agent session it is advising.
228
+ if (!fs.statSync(target).isFile()) return null;
229
+ return fs.readFileSync(target, "utf-8");
199
230
  } catch {
200
231
  // probe-direction: fail-closed — an unreadable current file compares as
201
232
  // absent against the baseline, which is the strict side of the ratchet.
@@ -291,26 +322,42 @@ function isPromotion(root, baselineConfig, baseRef, headRef) {
291
322
  * @param {"hook"|"staged"|"base"} mode Comparison mode
292
323
  * @param {string | undefined} baseRef Base ref (base mode only)
293
324
  * @param {string | undefined} headRef Head ref (base mode only)
294
- * @returns {{ entries: object[], promotion: boolean, note: string | null }}
295
- * Entries, whether this is a promotion, and an audit line to print when the
296
- * entries came from anywhere but the baseline
325
+ * @returns {{ entries: object[], unmerged: object[], protectedRefs: string[], promotion: boolean, note: string | null, humanGateMode: string }}
326
+ * Entries to honour, entries only this branch holds (local modes), the
327
+ * protected refs consulted for that, whether this is a promotion, an audit
328
+ * line to print when the entries came from anywhere but the baseline, and
329
+ * the baseline's `humanGate.mode`
297
330
  */
298
331
  function resolveAllowList(root, baselineRef, mode, baseRef, headRef) {
299
- const baselineConfig = parseJson(
300
- git(["show", `${baselineRef}:.lisa.config.json`], root)
301
- );
302
- if (mode !== "base" || !isPromotion(root, baselineConfig, baseRef, headRef)) {
332
+ const baselineText = git(["show", `${baselineRef}:.lisa.config.json`], root);
333
+ const baselineConfig = parseJson(baselineText);
334
+ const humanGateMode = humanGateModeOfText(baselineText);
335
+ if (mode !== "base") {
336
+ return {
337
+ ...localAllowList(root, baselineConfig),
338
+ promotion: false,
339
+ note: null,
340
+ humanGateMode,
341
+ };
342
+ }
343
+ if (!isPromotion(root, baselineConfig, baseRef, headRef)) {
303
344
  return {
304
345
  entries: extractAllowEntries(baselineConfig),
346
+ unmerged: [],
347
+ protectedRefs: [],
305
348
  promotion: false,
306
349
  note: null,
350
+ humanGateMode,
307
351
  };
308
352
  }
309
353
  return {
310
354
  entries: extractAllowEntries(
311
355
  parseJson(git(["show", "HEAD:.lisa.config.json"], root))
312
356
  ),
357
+ unmerged: [],
358
+ protectedRefs: [],
313
359
  promotion: true,
360
+ humanGateMode,
314
361
  note:
315
362
  `threshold-ratchet: promotion ${bareBranch(headRef)} → ` +
316
363
  `${bareBranch(baseRef)}; both are deploy-chain branches declared at ` +
@@ -320,19 +367,132 @@ function resolveAllowList(root, baselineRef, mode, baseRef, headRef) {
320
367
  };
321
368
  }
322
369
 
370
+ /**
371
+ * The remote-tracking refs of this repository's protected branches: the
372
+ * remote's default branch and every `deploy.branches` branch it declares.
373
+ *
374
+ * Which branches are protected is read from the remote's default branch and
375
+ * from nowhere else. HEAD's own `deploy.branches` is never consulted: a
376
+ * branch could name itself a deploy branch there, push, and count its own
377
+ * unmerged entry as merged.
378
+ *
379
+ * `origin` is the only remote consulted, the same assumption `bareBranch`
380
+ * makes.
381
+ *
382
+ * This is a local check and can be argued with: whoever owns `.git` owns
383
+ * these refs, and can repoint `origin/HEAD` or write a tracking ref by hand.
384
+ * The pull-request check, which compares against the base on the server's
385
+ * checkout, is the layer that cannot.
386
+ * @param {string} root Repo root
387
+ * @returns {string[]} Full ref names that resolve, e.g.
388
+ * `refs/remotes/origin/main`, `refs/remotes/origin/dev`; empty when the
389
+ * remote's default branch cannot be named
390
+ */
391
+ function protectedBranchRefs(root) {
392
+ const resolves = ref =>
393
+ git(["rev-parse", "--verify", "--quiet", `${ref}^{commit}`], root) !== null;
394
+ // Full ref names throughout. A short `origin/main` resolves to a LOCAL
395
+ // branch of that name before the remote-tracking one, so one
396
+ // `git branch origin/main` would have made an unmerged entry count as merged.
397
+ const declared = git(
398
+ ["symbolic-ref", "--quiet", "refs/remotes/origin/HEAD"],
399
+ root
400
+ )?.trim();
401
+ // Only the symbolic default counts. `origin/HEAD` is missing in a
402
+ // repository that was fetched rather than cloned, or freshly pushed, and it
403
+ // is tempting to try `origin/main` there. But a branch called `main` is not
404
+ // protected because of its name: where the default is `trunk`, an entry on
405
+ // an unprotected `origin/main` would count as merged. With no symbolic
406
+ // default nothing is returned, and the caller refuses and names the remedy.
407
+ const defaultRef =
408
+ declared?.startsWith(REMOTE_REFS) && resolves(declared) ? declared : null;
409
+ if (!defaultRef) return [];
410
+ const chain = deployChain(
411
+ parseJson(git(["show", `${defaultRef}:.lisa.config.json`], root))
412
+ );
413
+ return [
414
+ ...new Set([defaultRef, ...chain.map(branch => `${REMOTE_REFS}${branch}`)]),
415
+ ].filter(resolves);
416
+ }
417
+
418
+ /**
419
+ * Whether `origin` has any remote-tracking ref at all.
420
+ *
421
+ * A probe that fails is not an answer. `git()` returns null for every
422
+ * failure, a timeout included, and reading that as "never fetched" would let
423
+ * a transient error switch the local fence off.
424
+ * @param {string} root Repo root
425
+ * @returns {boolean | null} True when something was ever fetched from
426
+ * `origin`, false when the probe ran and found nothing, null when the probe
427
+ * itself failed
428
+ */
429
+ function originWasFetched(root) {
430
+ const first = git(
431
+ ["for-each-ref", "--count=1", "--format=%(refname)", REMOTE_REFS],
432
+ root
433
+ );
434
+ if (first === null) return null;
435
+ return first.trim() !== "";
436
+ }
437
+
438
+ /**
439
+ * The allow list for the local modes (`--staged`, `--hook`).
440
+ *
441
+ * HEAD's entries, minus those no protected branch holds yet. Two cases have
442
+ * no protected branch to ask, and they are opposite:
443
+ *
444
+ * nothing was ever fetched from `origin` — there is no merge for an entry
445
+ * to wait for, so every HEAD entry counts, as it always did. Refusing
446
+ * would wedge a repository that has no pull requests at all.
447
+ * `origin` was fetched, and its default branch cannot be named — there IS
448
+ * a merge to wait for and nothing here can tell whether it happened, so
449
+ * no HEAD entry counts. The refusal names the command that fixes it. A
450
+ * probe that fails is read the same way.
451
+ * @param {string} root Repo root
452
+ * @param {unknown} headConfig `.lisa.config.json` at HEAD
453
+ * @returns {{ entries: object[], unmerged: object[], protectedRefs: string[] }}
454
+ * Entries to honour, entries only this branch holds, and the refs consulted
455
+ */
456
+ function localAllowList(root, headConfig) {
457
+ const entries = extractAllowEntries(headConfig);
458
+ const protectedRefs = protectedBranchRefs(root);
459
+ if (protectedRefs.length === 0) {
460
+ // Honour HEAD only on a positive "nothing was ever fetched". A probe that
461
+ // failed is treated like a fetched remote whose default is unknown.
462
+ return originWasFetched(root) === false
463
+ ? { entries, unmerged: [], protectedRefs }
464
+ : { entries: [], unmerged: entries, protectedRefs };
465
+ }
466
+ const { merged, unmerged } = splitMergedEntries(
467
+ entries,
468
+ protectedRefs.map(ref =>
469
+ extractAllowEntries(
470
+ parseJson(git(["show", `${ref}:.lisa.config.json`], root))
471
+ )
472
+ )
473
+ );
474
+ return { entries: merged, unmerged, protectedRefs };
475
+ }
476
+
323
477
  /**
324
478
  * Split off the allow-entry additions a promotion is carrying forward.
325
479
  *
326
- * `applyAllowList` never drops an `allow-added` finding, from either side —
327
- * an exception must not approve its own creation. That is right for an
328
- * ordinary PR and wrong for a promotion, where the entry is not being created:
329
- * it already exists on the upstream branch, where its creation faced this same
330
- * unconditional block and needed a human to clear it. Without this the fix
331
- * would be cosmetic — a promotion carrying an approved exemption also carries
332
- * the `.lisa.config.json` diff that records it, so it would still be blocked
333
- * by the finding for the record of its own approval.
480
+ * An `allow-added` finding is never covered by an allow entry, from either
481
+ * side — an exception must not approve its own creation — and an entry that
482
+ * arrives with any other finding is refused (`settleAddedExceptions`). That is
483
+ * right for an ordinary PR and wrong for a promotion, where the entry is not
484
+ * being created: it already exists on the upstream branch, where it was
485
+ * recorded in a change of its own. Without this the fix would be cosmetic — a
486
+ * promotion carrying an approved exemption also carries the
487
+ * `.lisa.config.json` diff that records it, so it would still be blocked by
488
+ * the finding for the record of its own approval.
334
489
  *
335
- * Only `allow-added` is carried. An actual threshold weakening in the same
490
+ * A root config that stopped being `strict` is carried for the same reason:
491
+ * the change already landed on the upstream branch, and the downstream branch
492
+ * can only catch up by promotion, so refusing it here would refuse every
493
+ * promotion from then on, including ones carrying unrelated work.
494
+ *
495
+ * Nothing else is carried. An actual threshold weakening in the same
336
496
  * promotion still has to be covered by an allow entry.
337
497
  * @param {Array<{ type: string }>} findings All findings from the change
338
498
  * @param {boolean} promotion Whether the change is a recognised promotion
@@ -341,9 +501,12 @@ function resolveAllowList(root, baselineRef, mode, baseRef, headRef) {
341
501
  */
342
502
  function partitionCarriedEntries(findings, promotion) {
343
503
  if (!promotion) return { carried: [], rest: findings };
504
+ const isCarried = f =>
505
+ f.type === "allow-added" ||
506
+ (f.file === ".lisa.config.json" && f.key === "humanGate.mode");
344
507
  return {
345
- carried: findings.filter(f => f.type === "allow-added"),
346
- rest: findings.filter(f => f.type !== "allow-added"),
508
+ carried: findings.filter(isCarried),
509
+ rest: findings.filter(f => !isCarried(f)),
347
510
  };
348
511
  }
349
512
 
@@ -433,57 +596,128 @@ function run(mode, baseRef, onlyFiles, headRef) {
433
596
  // moment the condition passes, and reporting it only when something else has
434
597
  // already failed is how a list nobody reads stays a list nobody reads.
435
598
  if (findings.length === 0) {
436
- reportAllowList(allow.entries, []);
599
+ reportAllowList(allow.entries);
437
600
  return 0;
438
601
  }
439
- return reportFindings(findings, allow, mode);
602
+ return reportFindings(findings, allow, mode, plan.readCurrent);
440
603
  }
441
604
 
442
605
  /**
443
- * Print the allow entries a human has to act on, minus the ones a refusal is
444
- * about to name in full.
606
+ * The allow entries a human has to act on, minus the ones a refusal is about
607
+ * to name in full.
445
608
  * @param {object[]} entries The resolved allow list
446
609
  * @param {Array<{ message: string }>} expired Refusals already being printed
610
+ * @returns {string[]} One line per entry still to report
611
+ */
612
+ function allowListLines(entries, expired) {
613
+ return describeAllowList(entries)
614
+ .filter(line => !expired.some(item => item.message.startsWith(line)))
615
+ .map(line => `threshold-ratchet: ${line}`);
616
+ }
617
+
618
+ /**
619
+ * Print the allow entries a human has to act on.
620
+ * @param {object[]} entries The resolved allow list
621
+ * @returns {void}
622
+ */
623
+ function reportAllowList(entries) {
624
+ for (const line of allowListLines(entries, [])) {
625
+ process.stdout.write(`${line}\n`);
626
+ }
627
+ }
628
+
629
+ /**
630
+ * Write a passing run's lines where their reader will see them.
631
+ *
632
+ * At commit and in CI that is stdout. An agent-time hook that exits 0 is a
633
+ * different matter: the runtime shows the agent nothing from stdout unless it
634
+ * is the hook reply object, so a notice printed as plain text there reaches
635
+ * nobody. When the run recorded a new exception, hook mode therefore answers
636
+ * with the reply object and puts every line in `additionalContext`. Without a
637
+ * notice it prints plain lines exactly as it always has.
638
+ *
639
+ * Parity: the reply object is the Claude Code and Codex hook contract. A
640
+ * runtime that does not read it shows the JSON text or nothing; the commit and
641
+ * pull-request layers print the same notice in plain text there.
642
+ * @param {string[]} lines Lines to deliver
643
+ * @param {boolean} asHookReply Whether to wrap them in the hook reply object
447
644
  * @returns {void}
448
645
  */
449
- function reportAllowList(entries, expired) {
450
- for (const line of describeAllowList(entries)) {
451
- if (expired.some(item => item.message.startsWith(line))) continue;
452
- process.stdout.write(`threshold-ratchet: ${line}\n`);
646
+ function deliver(lines, asHookReply) {
647
+ if (lines.length === 0) return;
648
+ if (!asHookReply) {
649
+ process.stdout.write(`${lines.join("\n")}\n`);
650
+ return;
453
651
  }
652
+ process.stdout.write(
653
+ `${JSON.stringify({
654
+ hookSpecificOutput: {
655
+ hookEventName: "PostToolUse",
656
+ additionalContext: lines.join("\n"),
657
+ },
658
+ })}\n`
659
+ );
454
660
  }
455
661
 
456
662
  /**
457
663
  * Print the verdict for a change that produced findings.
458
664
  * @param {object[]} findings Findings from every watched file
459
- * @param {{ entries: object[], promotion: boolean, note: string | null }} allow
460
- * The resolved allow list and its provenance
665
+ * @param {{ entries: object[], unmerged: object[], protectedRefs: string[], promotion: boolean, note: string | null, humanGateMode: string }} allow
666
+ * The resolved allow list, its provenance and the baseline's mode
461
667
  * @param {"hook"|"staged"|"base"} mode Comparison mode
668
+ * @param {(file: string) => string | null} readCurrent Reads a repository
669
+ * file as the change has it
462
670
  * @returns {number} Process exit code (2 for hook mode, 1 otherwise; 0 clean)
463
671
  */
464
- function reportFindings(findings, allow, mode) {
672
+ function reportFindings(findings, allow, mode, readCurrent) {
465
673
  const split = partitionCarriedEntries(findings, allow.promotion);
466
- const { blocked, allowed, expired } = applyAllowList(
674
+ const settled = settleAddedExceptions(
467
675
  split.rest,
676
+ allow.humanGateMode,
677
+ Date.now(),
678
+ readCurrent
679
+ );
680
+ const { blocked, allowed, expired } = applyAllowList(
681
+ settled.rest,
468
682
  allow.entries
469
683
  );
470
- if (allow.note) process.stdout.write(`${allow.note}\n`);
471
- reportAllowList(allow.entries, expired);
472
- for (const finding of split.carried) {
473
- process.stdout.write(
474
- `threshold-ratchet: carried forward by this promotion, approved upstream — ${finding.message}\n`
475
- );
476
- }
477
- for (const item of allowed) {
478
- process.stdout.write(
479
- `threshold-ratchet: ${item.message} (.lisa.config.json exception)\n`
480
- );
481
- }
684
+ const refused = [...settled.refused, ...blocked];
685
+ const passed = refused.length === 0;
686
+ // A notice that an exception was recorded is only true of a change that
687
+ // lands. Printed beside a refusal it tells the reader to go on to the
688
+ // threshold change while the entry it speaks of was never committed.
689
+ const recorded = passed ? settled.accepted : [];
690
+ const lines = [
691
+ ...(allow.note ? [allow.note] : []),
692
+ ...allowListLines(allow.entries, expired),
693
+ ...split.carried.map(
694
+ finding =>
695
+ `threshold-ratchet: carried forward by this promotion, approved upstream — ${finding.message}`
696
+ ),
697
+ ...allowed.map(
698
+ item => `threshold-ratchet: ${item.message} (.lisa.config.json exception)`
699
+ ),
700
+ ...recorded.map(item => `threshold-ratchet: ${item.message}`),
701
+ ];
702
+ deliver(lines, mode === "hook" && recorded.length > 0);
482
703
  for (const item of expired) {
483
704
  process.stderr.write(`threshold-ratchet: refused — ${item.message}\n`);
484
705
  }
485
- if (blocked.length === 0) return 0;
486
- process.stderr.write(`${formatReport(blocked)}\n`);
706
+ for (const line of describeUnmergedCover(
707
+ blocked,
708
+ allow.unmerged,
709
+ allow.protectedRefs.map(ref => ref.replace(/^refs\/remotes\//u, ""))
710
+ )) {
711
+ process.stderr.write(`threshold-ratchet: refused — ${line}\n`);
712
+ }
713
+ for (const line of describeNearMisses(blocked, [
714
+ ...allow.entries,
715
+ ...allow.unmerged,
716
+ ])) {
717
+ process.stderr.write(`threshold-ratchet: refused — ${line}\n`);
718
+ }
719
+ if (passed) return 0;
720
+ process.stderr.write(`${formatReport(refused, allow.humanGateMode)}\n`);
487
721
  return mode === "hook" ? 2 : 1;
488
722
  }
489
723