@holmes-lab/holmes-kit 0.39.0 → 0.40.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 (298) hide show
  1. package/CHANGELOG.md +344 -6
  2. package/README.md +54 -41
  3. package/dist/.build-id +1 -1
  4. package/dist/holmes/admission/core/admission-policy.d.ts +37 -0
  5. package/dist/holmes/admission/core/admission-policy.js +147 -0
  6. package/dist/holmes/admission/core/bounded-reader.d.ts +56 -0
  7. package/dist/holmes/admission/core/bounded-reader.js +135 -0
  8. package/dist/holmes/admission/core/change-classifier.d.ts +34 -0
  9. package/dist/holmes/admission/core/change-classifier.js +278 -0
  10. package/dist/holmes/admission/core/config-loader.d.ts +30 -0
  11. package/dist/holmes/admission/core/config-loader.js +145 -0
  12. package/dist/holmes/admission/core/event-judgement.d.ts +37 -0
  13. package/dist/holmes/admission/core/event-judgement.js +251 -0
  14. package/dist/holmes/admission/core/path-rules.d.ts +35 -0
  15. package/dist/holmes/admission/core/path-rules.js +88 -0
  16. package/dist/holmes/admission/core/pin.d.ts +7 -0
  17. package/dist/holmes/admission/core/pin.js +29 -0
  18. package/dist/holmes/admission/core/ports.d.ts +48 -0
  19. package/dist/holmes/admission/core/ports.js +11 -0
  20. package/dist/holmes/admission/core/range-resolver.d.ts +32 -0
  21. package/dist/holmes/admission/core/range-resolver.js +51 -0
  22. package/dist/holmes/admission/core/self-containment.d.ts +1 -0
  23. package/dist/holmes/admission/core/self-containment.js +125 -0
  24. package/dist/holmes/admission/core/signature-source.d.ts +9 -0
  25. package/dist/holmes/admission/core/signature-source.js +4 -0
  26. package/dist/holmes/admission/core/transition-planner.d.ts +15 -0
  27. package/dist/holmes/admission/core/transition-planner.js +14 -0
  28. package/dist/holmes/admission/core/verdict.d.ts +38 -0
  29. package/dist/holmes/admission/core/verdict.js +17 -0
  30. package/dist/holmes/admission/core/wiring.d.ts +44 -0
  31. package/dist/holmes/admission/core/wiring.js +618 -0
  32. package/dist/holmes/admission/forge/git-object-port.d.ts +4 -0
  33. package/dist/holmes/admission/forge/git-object-port.js +132 -0
  34. package/dist/holmes/assoc/assoc-arm.d.ts +1 -1
  35. package/dist/holmes/assoc/assoc-arm.js +1 -1
  36. package/dist/holmes/cli/agents.d.ts +8 -0
  37. package/dist/holmes/cli/agents.js +9 -5
  38. package/dist/holmes/cli/approve-cli.d.ts +2 -0
  39. package/dist/holmes/cli/approve-cli.js +392 -0
  40. package/dist/holmes/cli/approve-ref.d.ts +13 -7
  41. package/dist/holmes/cli/approve-ref.js +30 -10
  42. package/dist/holmes/cli/approve-status.d.ts +26 -2
  43. package/dist/holmes/cli/approve-status.js +98 -18
  44. package/dist/holmes/cli/approve-watch.d.ts +5 -0
  45. package/dist/holmes/cli/approve-watch.js +10 -5
  46. package/dist/holmes/cli/approve.d.ts +49 -13
  47. package/dist/holmes/cli/approve.js +249 -67
  48. package/dist/holmes/cli/attest.js +2 -1
  49. package/dist/holmes/cli/autonomy.d.ts +4 -1
  50. package/dist/holmes/cli/autonomy.js +17 -10
  51. package/dist/holmes/cli/codex-oracle.d.ts +11 -0
  52. package/dist/holmes/cli/codex-oracle.js +5 -1
  53. package/dist/holmes/cli/codex-toml.d.ts +10 -0
  54. package/dist/holmes/cli/codex-toml.js +36 -2
  55. package/dist/holmes/cli/doctor-boundary.d.ts +76 -0
  56. package/dist/holmes/cli/doctor-boundary.js +308 -0
  57. package/dist/holmes/cli/doctor-locks.d.ts +15 -0
  58. package/dist/holmes/cli/doctor-locks.js +119 -0
  59. package/dist/holmes/cli/doctor.d.ts +2 -0
  60. package/dist/holmes/cli/doctor.js +34 -5
  61. package/dist/holmes/cli/flags.d.ts +23 -0
  62. package/dist/holmes/cli/flags.js +89 -0
  63. package/dist/holmes/cli/index.d.ts +5 -1
  64. package/dist/holmes/cli/index.js +202 -424
  65. package/dist/holmes/cli/init.js +33 -0
  66. package/dist/holmes/cli/lock-recover.d.ts +5 -0
  67. package/dist/holmes/cli/lock-recover.js +126 -0
  68. package/dist/holmes/cli/mcp-processes.js +1 -1
  69. package/dist/holmes/cli/release-docs.js +1 -1
  70. package/dist/holmes/cli/run-exec.d.ts +141 -0
  71. package/dist/holmes/cli/run-exec.js +1435 -0
  72. package/dist/holmes/cli/run-provision.d.ts +3 -1
  73. package/dist/holmes/cli/run-provision.js +255 -44
  74. package/dist/holmes/cli/run-wiring.d.ts +69 -0
  75. package/dist/holmes/cli/run-wiring.js +270 -0
  76. package/dist/holmes/cli/run.d.ts +21 -0
  77. package/dist/holmes/cli/run.js +341 -92
  78. package/dist/holmes/cli/runtime-binding-approval.js +32 -19
  79. package/dist/holmes/cli/screen-safe.d.ts +3 -92
  80. package/dist/holmes/cli/screen-safe.js +18 -756
  81. package/dist/holmes/config/config.d.ts +2 -0
  82. package/dist/holmes/config/config.js +5 -2
  83. package/dist/holmes/cpg/foundation/language-matrix.js +2 -2
  84. package/dist/holmes/cpg/import-resolver.js +1 -1
  85. package/dist/holmes/governance/approval-decisions.d.ts +47 -0
  86. package/dist/holmes/governance/approval-decisions.js +29 -0
  87. package/dist/holmes/governance/approval-grants.d.ts +220 -1
  88. package/dist/holmes/governance/approval-grants.js +612 -33
  89. package/dist/holmes/governance/approval-queue.d.ts +26 -0
  90. package/dist/holmes/governance/approval-queue.js +109 -11
  91. package/dist/holmes/governance/approval-spend.d.ts +95 -0
  92. package/dist/holmes/governance/approval-spend.js +191 -0
  93. package/dist/holmes/governance/autonomous-shell.d.ts +61 -0
  94. package/dist/holmes/governance/autonomous-shell.js +332 -0
  95. package/dist/holmes/governance/autonomy.d.ts +5 -0
  96. package/dist/holmes/governance/autonomy.js +16 -11
  97. package/dist/holmes/governance/constitution.d.ts +5 -0
  98. package/dist/holmes/governance/constitution.js +32 -12
  99. package/dist/holmes/governance/full-autonomy.d.ts +10 -0
  100. package/dist/holmes/governance/full-autonomy.js +103 -7
  101. package/dist/holmes/governance/harness-catalog.d.ts +103 -0
  102. package/dist/holmes/governance/harness-catalog.js +348 -0
  103. package/dist/holmes/governance/human-channel.d.ts +38 -0
  104. package/dist/holmes/governance/human-channel.js +49 -0
  105. package/dist/holmes/governance/install-manifest.d.ts +22 -0
  106. package/dist/holmes/governance/install-manifest.js +160 -0
  107. package/dist/holmes/governance/ledger-rechain.js +2 -0
  108. package/dist/holmes/governance/ledger-store.conformance.d.ts +7 -1
  109. package/dist/holmes/governance/ledger-store.conformance.js +66 -0
  110. package/dist/holmes/governance/ledger-store.d.ts +88 -11
  111. package/dist/holmes/governance/ledger-store.js +186 -8
  112. package/dist/holmes/governance/provenance-chain.d.ts +16 -4
  113. package/dist/holmes/governance/provenance-chain.js +19 -4
  114. package/dist/holmes/governance/provenance-ledger.d.ts +6 -3
  115. package/dist/holmes/governance/provenance-ledger.js +53 -18
  116. package/dist/holmes/governance/push-gate-runner.js +11 -7
  117. package/dist/holmes/governance/recorded-pairs.d.ts +96 -0
  118. package/dist/holmes/governance/recorded-pairs.js +522 -0
  119. package/dist/holmes/governance/replica-id.d.ts +1 -1
  120. package/dist/holmes/governance/replica-id.js +1 -1
  121. package/dist/holmes/governance/spec-seam.d.ts +59 -0
  122. package/dist/holmes/governance/spec-seam.js +587 -0
  123. package/dist/holmes/governance/spend-kinds.d.ts +16 -0
  124. package/dist/holmes/governance/spend-kinds.js +18 -0
  125. package/dist/holmes/guardrail/decision-ledger.d.ts +2 -1
  126. package/dist/holmes/guardrail/decision-ledger.js +2 -1
  127. package/dist/holmes/guardrail/impact-gate.d.ts +1 -0
  128. package/dist/holmes/guardrail/impact-gate.js +1 -1
  129. package/dist/holmes/guardrail/line-continuation.d.ts +8 -0
  130. package/dist/holmes/guardrail/line-continuation.js +39 -0
  131. package/dist/holmes/guardrail/operator-person.d.ts +28 -4
  132. package/dist/holmes/guardrail/operator-person.js +57 -11
  133. package/dist/holmes/guardrail/refusal-codes.d.ts +8 -3
  134. package/dist/holmes/guardrail/refusal-codes.js +64 -10
  135. package/dist/holmes/guardrail/risk-classifier.d.ts +15 -9
  136. package/dist/holmes/guardrail/risk-classifier.js +228 -16
  137. package/dist/holmes/guardrail/risk-gate.d.ts +2 -8
  138. package/dist/holmes/guardrail/risk-gate.js +7 -34
  139. package/dist/holmes/guardrail/risk-types.d.ts +10 -0
  140. package/dist/holmes/guardrail/run-plan-scope.js +3 -1
  141. package/dist/holmes/guardrail/run-refusals.js +24 -8
  142. package/dist/holmes/guardrail/run-stages.d.ts +5 -0
  143. package/dist/holmes/guardrail/run-stages.js +25 -5
  144. package/dist/holmes/guardrail/scope-judgment.js +1 -1
  145. package/dist/holmes/guardrail/shell-words.js +24 -3
  146. package/dist/holmes/guardrail/unresolved-write.d.ts +40 -0
  147. package/dist/holmes/guardrail/unresolved-write.js +556 -0
  148. package/dist/holmes/guardrail/write-target.d.ts +3 -0
  149. package/dist/holmes/guardrail/write-target.js +26 -5
  150. package/dist/holmes/hooks/pre-tool-use.d.ts +105 -1
  151. package/dist/holmes/hooks/pre-tool-use.js +1134 -167
  152. package/dist/holmes/hooks/rtm-refresh-child.d.ts +1 -1
  153. package/dist/holmes/hooks/rtm-refresh-child.js +1 -1
  154. package/dist/holmes/hooks/stop.d.ts +23 -0
  155. package/dist/holmes/hooks/stop.js +86 -33
  156. package/dist/holmes/mcp/approval-adapters.js +3 -1
  157. package/dist/holmes/mcp/approval-commit.js +3 -2
  158. package/dist/holmes/mcp/approval-policy.d.ts +2 -1
  159. package/dist/holmes/mcp/approval-policy.js +3 -1
  160. package/dist/holmes/mcp/handlers/adoption.d.ts +1 -1
  161. package/dist/holmes/mcp/handlers/approval-ask.d.ts +3 -0
  162. package/dist/holmes/mcp/handlers/approval-ask.js +18 -7
  163. package/dist/holmes/mcp/handlers/entity-integration.d.ts +3 -3
  164. package/dist/holmes/mcp/handlers/entity-integration.js +29 -11
  165. package/dist/holmes/mcp/handlers/entity-renumber.d.ts +4 -4
  166. package/dist/holmes/mcp/handlers/entity-renumber.js +29 -11
  167. package/dist/holmes/mcp/handlers/entity-store.d.ts +2 -2
  168. package/dist/holmes/mcp/handlers/entity-store.js +133 -59
  169. package/dist/holmes/mcp/handlers/operator-inspection.d.ts +9 -5
  170. package/dist/holmes/mcp/handlers/operator-inspection.js +79 -16
  171. package/dist/holmes/mcp/handlers/phase-queries.d.ts +1 -1
  172. package/dist/holmes/mcp/handlers/requirement-triage.d.ts +4 -4
  173. package/dist/holmes/mcp/handlers/review-evidence.d.ts +2 -2
  174. package/dist/holmes/mcp/handlers/review-evidence.js +35 -11
  175. package/dist/holmes/mcp/handlers/review-queries.d.ts +5 -0
  176. package/dist/holmes/mcp/handlers/review-queries.js +8 -1
  177. package/dist/holmes/mcp/handlers/review-verdict.d.ts +3 -0
  178. package/dist/holmes/mcp/handlers/review-verdict.js +10 -3
  179. package/dist/holmes/mcp/handlers/risk-assessment.d.ts +7 -2
  180. package/dist/holmes/mcp/handlers/risk-assessment.js +34 -12
  181. package/dist/holmes/mcp/handlers/runtime-binding-tool.d.ts +2 -2
  182. package/dist/holmes/mcp/handlers/slice-orchestration.d.ts +6 -1
  183. package/dist/holmes/mcp/handlers/slice-orchestration.js +14 -1
  184. package/dist/holmes/mcp/handlers/spec-approval.d.ts +15 -3
  185. package/dist/holmes/mcp/handlers/spec-approval.js +32 -5
  186. package/dist/holmes/mcp/handlers/spec-authoring.d.ts +2 -2
  187. package/dist/holmes/mcp/handlers/spec-authoring.js +19 -2
  188. package/dist/holmes/mcp/handlers/spec-lifecycle.d.ts +11 -38
  189. package/dist/holmes/mcp/handlers/spec-lifecycle.js +441 -94
  190. package/dist/holmes/mcp/handlers/spec-queries.d.ts +2 -2
  191. package/dist/holmes/mcp/handlers/taint-screening.d.ts +1 -1
  192. package/dist/holmes/mcp/handlers/taint-screening.js +2 -2
  193. package/dist/holmes/mcp/handlers/test-execution.d.ts +1 -1
  194. package/dist/holmes/mcp/handlers/workspace-queries.d.ts +2 -2
  195. package/dist/holmes/mcp/handlers.d.ts +89 -79
  196. package/dist/holmes/mcp/handlers.js +69 -22
  197. package/dist/holmes/mcp/harness-attestation.d.ts +78 -5
  198. package/dist/holmes/mcp/harness-attestation.js +325 -53
  199. package/dist/holmes/mcp/maintenance-analyze.d.ts +1 -1
  200. package/dist/holmes/mcp/maintenance-analyze.js +9 -9
  201. package/dist/holmes/mcp/maintenance-evidence.js +31 -2
  202. package/dist/holmes/mcp/requirement-triage-evidence.js +22 -13
  203. package/dist/holmes/mcp/server-staleness.js +1 -1
  204. package/dist/holmes/mcp/server.js +9 -8
  205. package/dist/holmes/mcp/spec-id-guard.js +1 -1
  206. package/dist/holmes/mcp/tool-schemas.d.ts +13 -0
  207. package/dist/holmes/mcp/tool-schemas.js +29 -2
  208. package/dist/holmes/project/act-root.d.ts +19 -0
  209. package/dist/holmes/project/act-root.js +100 -0
  210. package/dist/holmes/project/baseline.js +4 -3
  211. package/dist/holmes/project/bounded-group.d.ts +47 -0
  212. package/dist/holmes/project/bounded-group.js +140 -0
  213. package/dist/holmes/project/bounded-io.d.ts +50 -2
  214. package/dist/holmes/project/bounded-io.js +138 -6
  215. package/dist/holmes/project/change-source.js +2 -2
  216. package/dist/holmes/project/instance-id.d.ts +10 -0
  217. package/dist/holmes/project/instance-id.js +233 -0
  218. package/dist/holmes/project/machine-identity.d.ts +25 -0
  219. package/dist/holmes/project/machine-identity.js +133 -0
  220. package/dist/holmes/project/owned-lock.d.ts +84 -0
  221. package/dist/holmes/project/owned-lock.js +263 -0
  222. package/dist/holmes/project/plain-lock-fs.d.ts +2 -0
  223. package/dist/holmes/project/plain-lock-fs.js +126 -0
  224. package/dist/holmes/project/request-lock.d.ts +85 -0
  225. package/dist/holmes/project/request-lock.js +459 -0
  226. package/dist/holmes/project/root.d.ts +12 -4
  227. package/dist/holmes/project/root.js +26 -7
  228. package/dist/holmes/project/run-records.d.ts +19 -0
  229. package/dist/holmes/project/run-records.js +94 -12
  230. package/dist/holmes/project/screen-text.d.ts +94 -0
  231. package/dist/holmes/project/screen-text.js +763 -0
  232. package/dist/holmes/project/spend-lock.d.ts +82 -0
  233. package/dist/holmes/project/spend-lock.js +413 -0
  234. package/dist/holmes/refresh/freshness-coordinator.d.ts +1 -1
  235. package/dist/holmes/refresh/freshness-coordinator.js +1 -1
  236. package/dist/holmes/review/condition-hunk-replay.js +4 -2
  237. package/dist/holmes/review/confidence-calibration.d.ts +1 -1
  238. package/dist/holmes/review/confidence-calibration.js +1 -1
  239. package/dist/holmes/review/content-verify.js +1 -1
  240. package/dist/holmes/review/hop-ablation.d.ts +4 -4
  241. package/dist/holmes/review/hop-ablation.js +5 -5
  242. package/dist/holmes/review/point-in-time-replay.js +12 -3
  243. package/dist/holmes/review/rank-diagnosis.d.ts +2 -2
  244. package/dist/holmes/review/rank-diagnosis.js +1 -1
  245. package/dist/holmes/review/replay-corpus.d.ts +10 -4
  246. package/dist/holmes/review/replay-corpus.js +25 -9
  247. package/dist/holmes/review/requirement-triage.js +2 -2
  248. package/dist/holmes/review/semantic-arm.d.ts +3 -3
  249. package/dist/holmes/review/semantic-arm.js +3 -3
  250. package/dist/holmes/review/test-outcomes.js +3 -1
  251. package/dist/holmes/review/test-runner.d.ts +11 -0
  252. package/dist/holmes/review/test-runner.js +31 -10
  253. package/dist/holmes/review/test-selection-breadth.js +2 -2
  254. package/dist/holmes/review/verdicts.d.ts +105 -8
  255. package/dist/holmes/review/verdicts.js +640 -36
  256. package/dist/holmes/rtm/anchor-census.d.ts +9 -0
  257. package/dist/holmes/rtm/anchor-census.js +151 -0
  258. package/dist/holmes/rtm/anchor-ids.d.ts +25 -1
  259. package/dist/holmes/rtm/anchor-ids.js +45 -2
  260. package/dist/holmes/rtm/comment-ranges.d.ts +2 -0
  261. package/dist/holmes/rtm/comment-ranges.js +104 -0
  262. package/dist/holmes/rtm/counted-tests.d.ts +1 -0
  263. package/dist/holmes/rtm/counted-tests.js +63 -0
  264. package/dist/holmes/rtm/impact-advisory.d.ts +1 -1
  265. package/dist/holmes/rtm/known-defects.js +1 -1
  266. package/dist/holmes/rtm/localize.js +1 -1
  267. package/dist/holmes/rtm/taint-benchmark.d.ts +1 -1
  268. package/dist/holmes/rtm/taint-vocabulary.d.ts +1 -1
  269. package/dist/holmes/rtm/test-scope.d.ts +31 -0
  270. package/dist/holmes/rtm/test-scope.js +166 -5
  271. package/dist/holmes/rtm/trace-gaps.js +1 -1
  272. package/dist/holmes/runtime/dispatch.d.ts +10 -0
  273. package/dist/holmes/runtime/dispatch.js +12 -1
  274. package/dist/holmes/semantic/cloud-usage.d.ts +1 -1
  275. package/dist/holmes/semantic/cloud-usage.js +1 -1
  276. package/dist/holmes/semantic/unreadable-paths.d.ts +1 -1
  277. package/dist/holmes/semantic/unreadable-paths.js +1 -1
  278. package/dist/holmes/spec/approval-blockers.js +1 -1
  279. package/dist/holmes/spec/entity-store.d.ts +6 -1
  280. package/dist/holmes/spec/entity-store.js +16 -2
  281. package/dist/holmes/spec/entity-transaction.d.ts +7 -2
  282. package/dist/holmes/spec/entity-transaction.js +87 -170
  283. package/dist/holmes/spec/spec-store.d.ts +12 -6
  284. package/dist/holmes/spec/spec-store.js +4 -2
  285. package/dist/holmes/spec/spec-types.js +1 -1
  286. package/dist/holmes/spec/succession-guard.d.ts +56 -0
  287. package/dist/holmes/spec/succession-guard.js +187 -0
  288. package/dist/holmes/spec/succession.d.ts +70 -0
  289. package/dist/holmes/spec/succession.js +171 -0
  290. package/dist/holmes/spec/transition-policy.d.ts +2 -2
  291. package/docs/install-guide.md +9 -3
  292. package/docs/language-support.md +78 -0
  293. package/package.json +4 -3
  294. package/playbooks/author-slice/PLAYBOOK.md +1 -1
  295. package/playbooks/promote-slice/PLAYBOOK.md +5 -1
  296. package/playbooks/publish/PLAYBOOK.md +45 -33
  297. package/dist/holmes/review/manual-baseline.d.ts +0 -209
  298. package/dist/holmes/review/manual-baseline.js +0 -2847
@@ -33,16 +33,56 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.MemoryLedgerStore = exports.FileLedgerStore = exports.LedgerConflictRefusal = void 0;
36
+ exports.MemoryLedgerStore = exports.FileLedgerStore = exports.LedgerConflictRefusal = exports.LATE_CHAIN_READ_MS = exports.SpendKindMismatch = exports.NONCE_REVOKED = void 0;
37
37
  exports.writerChainFile = writerChainFile;
38
38
  exports.ledgerConflictReason = ledgerConflictReason;
39
+ exports.nonceRevokedIn = nonceRevokedIn;
39
40
  exports.nonceConsumedIn = nonceConsumedIn;
40
- // @implements A-SPEC-150, A-SPEC-624
41
+ // @implements A-SPEC-150, A-SPEC-624, A-SPEC-245.10, A-SPEC-245.11, A-SPEC-245.12, A-SPEC-794.4, A-SPEC-794.5
41
42
  // @implements A-SPEC-643
42
- // @implements A-SPEC-642
43
+ // @implements A-SPEC-642, A-SPEC-245.9, A-SPEC-245.16
44
+ // @implements A-SPEC-792.26
43
45
  const provenance_chain_1 = require("./provenance-chain");
44
46
  const provenance_ledger_1 = require("./provenance-ledger");
47
+ const spend_kinds_1 = require("./spend-kinds");
48
+ const fs = __importStar(require("node:fs"));
45
49
  const path = __importStar(require("node:path"));
50
+ /** The ledger kind of a revocation (A-SPEC-245.9, H-2), in the grant namespace (A-SPEC-794.4). */
51
+ exports.NONCE_REVOKED = (0, spend_kinds_1.revokeKind)('grant');
52
+ /**
53
+ * @implements A-SPEC-794.5 — the port binds a row's kind to the namespace it judges: a spend judged in one namespace
54
+ * and written under another kind would be invisible to that namespace's readers, so it is refused before anything is
55
+ * written.
56
+ */
57
+ class SpendKindMismatch extends Error {
58
+ expected;
59
+ actual;
60
+ constructor(expected, actual) {
61
+ super(`a spend port row has kind ${JSON.stringify(actual)} where its namespace writes ${JSON.stringify(expected)}; nothing was written`);
62
+ this.expected = expected;
63
+ this.actual = actual;
64
+ this.name = 'SpendKindMismatch';
65
+ }
66
+ }
67
+ exports.SpendKindMismatch = SpendKindMismatch;
68
+ function assertKinds(rows, expected) {
69
+ for (const r of rows)
70
+ if (r.kind !== expected)
71
+ throw new SpendKindMismatch(expected, String(r.kind));
72
+ }
73
+ /** The least time a late chain gets to be read, even when the spend's budget is spent (reviewer A, L1). */
74
+ exports.LATE_CHAIN_READ_MS = 500;
75
+ /** The fingerprints a set of spend rows carries, and the count of their spends a ledger holds. */
76
+ function spendFingerprints(rows) { return [...new Set(rows.flatMap((r) => r.inputs ?? []))]; }
77
+ function spendsOf(events, fps, ns = 'grant') {
78
+ return events.filter((e) => e.kind === (0, spend_kinds_1.spendKind)(ns) && (e.inputs ?? []).some((i) => fps.includes(i))).length;
79
+ }
80
+ function conflictRow(fps, answer, why) {
81
+ const summary = answer === 'conflict'
82
+ ? 'a spend of the same approval appeared on a chain begun after the last look'
83
+ : 'a chain begun after the last look could not be read, so the spend is not confirmed single';
84
+ return { ts: new Date().toISOString(), actor: 'holmes-kit', kind: 'spend-conflict-detected', summary, inputs: fps, rationale: why };
85
+ }
46
86
  /**
47
87
  * @implements A-SPEC-643
48
88
  * The chain file THIS process writes: `provenance.<replica>.jsonl`, resolved exactly as every other
@@ -76,7 +116,10 @@ function ledgerConflictReason(dir) {
76
116
  return null;
77
117
  const d = pending[0];
78
118
  const replicas = [...new Set(d.events.map((e) => e.replicaId ?? 'legacy'))].join(', ');
79
- return `the merged ledger holds an unreconciled cross-replica double-spend: single-use approval ${d.nonce} was consumed on replicas ${replicas}`
119
+ return (d.kind === 'revoked-and-consumed'
120
+ // @implements A-SPEC-245.9 (N10)
121
+ ? `the merged ledger holds an unreconciled revocation that was also spent: single-use approval ${d.nonce} was revoked and consumed on replicas ${replicas}`
122
+ : `the merged ledger holds an unreconciled cross-replica double-spend: single-use approval ${d.nonce} was consumed on replicas ${replicas}`)
80
123
  + ` — approvals, applies and recoveries are refused until an operator reconciles it (ledger_reconcile { nonce: "${d.nonce}" })`
81
124
  + (pending.length > 1 ? `; ${pending.length - 1} more pending` : '');
82
125
  }
@@ -126,10 +169,124 @@ class FileLedgerStore {
126
169
  append(body) {
127
170
  return this.inner.append(body);
128
171
  }
129
- isNonceConsumed(nonce) { return this.inner.isNonceConsumed(nonce); }
172
+ isNonceConsumed(nonce, ns = 'grant') { return this.inner.isNonceConsumed(nonce, ns); }
130
173
  doubleSpends() { return this.inner.doubleSpends(); }
174
+ /**
175
+ * @implements A-SPEC-245.10, A-SPEC-245.11, A-SPEC-245.12 — the spend port on files: append the spend rows to this
176
+ * writer's chain while holding every replica chain's append lock, taken in sorted path order within the deadline.
177
+ * The chains are enumerated with their lock directories (M-a): a chain whose first append is in progress has a lock
178
+ * and no file yet, and is waited for. This writer's own chain is included even when absent. If a chain appears
179
+ * while the locks are taken, they are released and taken again. The spent check runs inside all the locks. After
180
+ * the append, still holding them, chains begun since are waited for (at least LATE_CHAIN_READ_MS) and read (W2).
181
+ * Throws the lock layer's error when the deadline passes before the append.
182
+ */
183
+ appendIfUnspent(spends, deadline, ns = 'grant') {
184
+ const rows = spends.map((s) => s.row);
185
+ assertKinds(rows, (0, spend_kinds_1.spendKind)(ns));
186
+ const fps = spendFingerprints(rows);
187
+ // the merged ledger as it is inside the locks; the raw nonce is asked, so a row written before fingerprints counts
188
+ // A-SPEC-245.16 (item 1): and no nonce revoked on any chain — a revocation between an act's claim and its spend
189
+ // refuses the spend here, under the same locks, whatever the caller judged before
190
+ const check = () => {
191
+ const merged = new FileLedgerStore(this.dir, this.givenId);
192
+ if (spends.some((s) => merged.isNonceConsumed(s.nonce, ns)))
193
+ return 'spent';
194
+ const events = merged.loadAll();
195
+ return spends.some((s) => nonceRevokedIn(events, s.nonce, ns)) ? 'revoked' : 'ok';
196
+ };
197
+ return this.underEveryChainLock(deadline, ({ chain, files, enumerate, holding }) => {
198
+ const judged = check();
199
+ if (judged !== 'ok')
200
+ return judged;
201
+ for (const row of rows)
202
+ chain.appendInsideLock(row);
203
+ // W2: a chain an older writer began after the last enumeration was not excluded. Wait for each such chain's
204
+ // lock, then count the spends of these fingerprints: more than this append's own is a double spend.
205
+ const late = enumerate().filter((f) => !files.includes(f));
206
+ if (late.length === 0)
207
+ return 'appended';
208
+ let verdict;
209
+ let why;
210
+ try {
211
+ const found = holding(late, () => spendsOf(new FileLedgerStore(this.dir, this.givenId).loadAll(), fps, ns), Math.max(deadline, Date.now() + exports.LATE_CHAIN_READ_MS));
212
+ verdict = found > rows.length ? 'conflict' : 'appended';
213
+ why = `${found} spend row(s) of these fingerprints across the merged ledger, ${rows.length} of them this append's; chains begun after the enumeration: ${late.map((f) => path.basename(f)).join(', ')}`;
214
+ }
215
+ catch (e) {
216
+ verdict = 'unconfirmed';
217
+ why = `chains begun after the enumeration could not be read within the budget (${e.message})`;
218
+ }
219
+ if (verdict !== 'appended')
220
+ chain.appendInsideLock(conflictRow(fps, verdict, why));
221
+ return verdict;
222
+ });
223
+ }
224
+ /**
225
+ * @implements A-SPEC-245.9 (1b-3, M-10) — revocation on files: under every replica chain's append lock, a nonce
226
+ * spent on any chain answers `spent`, one already revoked answers `revoked-already`, and otherwise the revocation row
227
+ * is appended (`revoked`). A spend that appears later on a chain begun after the enumeration is N10's report.
228
+ */
229
+ appendRevocationIfUnspent(nonce, row, deadline, ns = 'grant') {
230
+ assertKinds([row], (0, spend_kinds_1.revokeKind)(ns));
231
+ return this.underEveryChainLock(deadline, ({ chain }) => {
232
+ const merged = new FileLedgerStore(this.dir, this.givenId);
233
+ if (merged.isNonceConsumed(nonce, ns))
234
+ return 'spent';
235
+ if (nonceRevokedIn(merged.loadAll(), nonce, ns))
236
+ return 'revoked-already';
237
+ chain.appendInsideLock(row);
238
+ return 'revoked';
239
+ });
240
+ }
241
+ /**
242
+ * Run `body` holding every replica chain's append lock, taken in sorted path order within `deadline` (A-SPEC-245.10,
243
+ * 245.11): the chains are enumerated with their lock directories, this writer's own chain is included even when
244
+ * absent, and a chain that appears while the locks are taken starts the round again. Throws the lock layer's error
245
+ * when the deadline passes first.
246
+ */
247
+ underEveryChainLock(deadline, body) {
248
+ const lockLayer = require('./ledger-lock');
249
+ const { ProvenanceChain } = require('./provenance-chain');
250
+ const own = this.inner.currentFile();
251
+ const enumerate = () => {
252
+ let names = [];
253
+ try {
254
+ names = fs.readdirSync(this.dir);
255
+ }
256
+ catch { /* no ledger yet */ }
257
+ const files = names.filter((n) => (0, provenance_ledger_1.isLedgerFilename)(n)).map((n) => path.join(this.dir, n));
258
+ const locked = names.filter((n) => /^provenance(\.[^.]+)?\.jsonl\.lock$/.test(n)).map((n) => path.join(this.dir, n.slice(0, -'.lock'.length)));
259
+ return [...new Set([...files, ...locked, own])].sort();
260
+ };
261
+ /** Run `then` holding each of `files`' locks in order, until `until`. */
262
+ const holding = (files, then, until = deadline) => {
263
+ const hold = (i) => (i === files.length ? then()
264
+ : lockLayer.withLedgerLock(files[i], () => hold(i + 1), { acquireMs: Math.max(1, until - Date.now()) }));
265
+ return hold(0);
266
+ };
267
+ for (;;) {
268
+ const files = enumerate();
269
+ const RETRY = Symbol('retry');
270
+ const r = holding(files, () => {
271
+ // a chain that appeared while the locks were taken is not held: start over with it
272
+ if (enumerate().some((f) => !files.includes(f)))
273
+ return RETRY;
274
+ return body({ chain: new ProvenanceChain(own), files, enumerate, holding });
275
+ });
276
+ if (r !== RETRY)
277
+ return r;
278
+ if (Date.now() >= deadline)
279
+ throw new lockLayer.LedgerLockError('could not hold every replica chain lock within the budget: chains kept appearing');
280
+ }
281
+ }
131
282
  }
132
283
  exports.FileLedgerStore = FileLedgerStore;
284
+ /** @implements A-SPEC-245.9 (1b-3) — whether a ledger holds a revocation of `nonce` (its fingerprint, or the raw value). */
285
+ function nonceRevokedIn(events, nonce, ns = 'grant') {
286
+ const { nonceFingerprint } = require('./provenance-chain');
287
+ const fp = nonceFingerprint(nonce);
288
+ return events.some((e) => e.kind === (0, spend_kinds_1.revokeKind)(ns) && ((e.inputs ?? []).includes(fp) || (e.inputs ?? []).includes(nonce)));
289
+ }
133
290
  /**
134
291
  * In-memory adapter — the instrument that makes the contract testable.
135
292
  *
@@ -167,22 +324,43 @@ class MemoryLedgerStore {
167
324
  }
168
325
  append(body) {
169
326
  const chain = this.byReplica.get(this.id) ?? [];
170
- const evt = (0, provenance_chain_1.chainNext)(chain, body, process.env.HOLMES_LEDGER_KEY || undefined);
327
+ const evt = (0, provenance_chain_1.chainNext)(chain, (0, provenance_chain_1.writtenBody)(body), process.env.HOLMES_LEDGER_KEY || undefined); // A-SPEC-792.26 — the same seam
171
328
  // Push onto a copy-then-set so an earlier event object is never mutated in place — append-only
172
329
  // has to hold for the objects a caller already read, not just for the array.
173
330
  this.byReplica.set(this.id, [...chain, evt]);
174
331
  return evt;
175
332
  }
176
- isNonceConsumed(nonce) {
333
+ isNonceConsumed(nonce, ns = 'grant') {
177
334
  // @implements A-SPEC-191 §13 — the fingerprint form is what writers record now (§11c). An
178
335
  // adapter that only knew the raw value would report 'not consumed' for every real consumption,
179
336
  // and the conformance suite would still pass because it wrote raw values itself.
180
337
  const { nonceFingerprint } = require('./provenance-chain');
181
338
  const fp = nonceFingerprint(nonce);
182
- return this.loadAll().some((e) => e.kind === 'nonce-consumed'
339
+ return this.loadAll().some((e) => e.kind === (0, spend_kinds_1.spendKind)(ns)
183
340
  && ((e.inputs ?? []).includes(nonce) || (e.inputs ?? []).includes(fp)));
184
341
  }
185
342
  doubleSpends() { return (0, provenance_ledger_1.unreconciledDoubleSpends)(this.loadAll()); }
343
+ /** One process, no other writer to exclude: check, then append (A-SPEC-245.12, the spend port's in-memory form). */
344
+ appendIfUnspent(spends, _deadline, ns = 'grant') {
345
+ assertKinds(spends.map((s) => s.row), (0, spend_kinds_1.spendKind)(ns));
346
+ if (spends.some((s) => this.isNonceConsumed(s.nonce, ns)))
347
+ return 'spent';
348
+ if (spends.some((s) => nonceRevokedIn(this.loadAll(), s.nonce, ns)))
349
+ return 'revoked'; // A-SPEC-245.16
350
+ for (const s of spends)
351
+ this.append(s.row);
352
+ return 'appended';
353
+ }
354
+ /** @implements A-SPEC-245.9 (1b-3, M-10) — the revocation port's in-memory form: check, then append. */
355
+ appendRevocationIfUnspent(nonce, row, _deadline, ns = 'grant') {
356
+ assertKinds([row], (0, spend_kinds_1.revokeKind)(ns));
357
+ if (this.isNonceConsumed(nonce, ns))
358
+ return 'spent';
359
+ if (nonceRevokedIn(this.loadAll(), nonce, ns))
360
+ return 'revoked-already';
361
+ this.append(row);
362
+ return 'revoked';
363
+ }
186
364
  }
187
365
  exports.MemoryLedgerStore = MemoryLedgerStore;
188
366
  // @implements A-SPEC-574.3
@@ -1,5 +1,6 @@
1
1
  import { ExecutionContext } from '../project/execution-context';
2
2
  import { LockOptions } from './ledger-lock';
3
+ import { LedgerActor } from './human-channel';
3
4
  /**
4
5
  * N3 — Decision PROVENANCE chain (target-architecture §7-N N3): "누가·언제·왜"의 완전 인과 재구성.
5
6
  *
@@ -32,6 +33,17 @@ export interface ProvenanceEvent {
32
33
  hash: string;
33
34
  }
34
35
  type ProvenanceBody = Omit<ProvenanceEvent, 'prevHash' | 'hash'>;
36
+ /** @implements A-SPEC-792.26 — what a writer hands the chain: its actor may be a minted person-channel actor. */
37
+ export type AppendBody = Omit<ProvenanceBody, 'seq' | 'actor'> & {
38
+ actor: LedgerActor;
39
+ };
40
+ /**
41
+ * @implements A-SPEC-792.26 — the write seam: the actor and its authorization reference are judged here, so no row
42
+ * claims a person channel unless the mint made its actor (an environment approval saying elicitation:claude is
43
+ * written as unattested:elicitation:claude). Re-chaining (ledger-rechain) re-writes recorded events verbatim and does
44
+ * not come through here.
45
+ */
46
+ export declare function writtenBody(body: AppendBody): Omit<ProvenanceBody, 'seq'>;
35
47
  export declare const GENESIS = "GENESIS";
36
48
  /** Ordered-key serialization so identical bodies always produce identical bytes (hash stability). */
37
49
  export declare function canonicalize(b: ProvenanceBody): string;
@@ -63,7 +75,7 @@ export declare function verifyChain(chain: ProvenanceEvent[], key?: string): {
63
75
  * #1 — the raw token in an audit file meant any local reader could harvest live gate-unlock
64
76
  * credentials).
65
77
  */
66
- export declare function authorizationRef(actor: string, token: string): string;
78
+ export declare function authorizationRef(actor: LedgerActor, token: string): string;
67
79
  /**
68
80
  * Redact a tool's target before it enters the audit trail.
69
81
  *
@@ -137,13 +149,13 @@ export declare class ProvenanceChain {
137
149
  * The read-and-write span runs under exclusion. Measured without it: two processes read the same
138
150
  * tail, both wrote events claiming the same `seq`, and `verify()` then reported `ok:false` —
139
151
  * tampering that never happened, 17 times out of 20. */
140
- append(body: Omit<ProvenanceBody, 'seq'>): ProvenanceEvent;
152
+ append(body: AppendBody): ProvenanceEvent;
141
153
  /**
142
154
  * Append assuming exclusion is ALREADY held. Only for callers running inside `withLedgerLock` on
143
155
  * this same file — `consumeNonceExclusively` needs the read and the write in one hold, and calling
144
156
  * `append()` from there would deadlock against its own budget.
145
157
  */
146
- appendInsideLock(body: Omit<ProvenanceBody, 'seq'>): ProvenanceEvent;
158
+ appendInsideLock(body: AppendBody): ProvenanceEvent;
147
159
  /** The append itself, assuming exclusion is already held. Only callers INSIDE a hold may use it. */
148
160
  private appendUnlocked;
149
161
  /**
@@ -179,7 +191,7 @@ export declare const PROVENANCE_FILE: string;
179
191
  * decision, not this function's: an authorization gate must deny, while a record-keeping append must
180
192
  * not flip a decision that was already made.
181
193
  */
182
- export declare function consumeNonceExclusively(nonce: string, ledgerFile: string, body: Omit<ProvenanceBody, 'seq'>, opts: LockOptions & {
194
+ export declare function consumeNonceExclusively(nonce: string, ledgerFile: string, body: AppendBody, opts: LockOptions & {
183
195
  /**
184
196
  * @implements A-SPEC-574.3
185
197
  * Whether this nonce was already consumed, ACROSS every replica chain (A-SPEC-148's rule).
@@ -34,6 +34,7 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.PROVENANCE_FILE = exports.ProvenanceChain = exports.GENESIS = void 0;
37
+ exports.writtenBody = writtenBody;
37
38
  exports.canonicalize = canonicalize;
38
39
  exports.computeHash = computeHash;
39
40
  exports.chainNext = chainNext;
@@ -50,13 +51,24 @@ exports.keyedSelfConsistent = keyedSelfConsistent;
50
51
  exports.bodyCanon = bodyCanon;
51
52
  exports.parseLedgerLines = parseLedgerLines;
52
53
  exports.consumeNonceExclusively = consumeNonceExclusively;
53
- // @implements A-SPEC-125.2, A-SPEC-624, A-SPEC-734
54
+ // @implements A-SPEC-125.2, A-SPEC-624, A-SPEC-734, A-SPEC-794.46
54
55
  const execution_context_1 = require("../project/execution-context");
55
56
  const crypto = __importStar(require("node:crypto"));
56
57
  const fs = __importStar(require("node:fs"));
57
58
  const path = __importStar(require("node:path"));
58
59
  const ledger_lock_1 = require("./ledger-lock");
59
60
  const bounded_io_1 = require("../project/bounded-io");
61
+ const human_channel_1 = require("./human-channel");
62
+ /**
63
+ * @implements A-SPEC-792.26 — the write seam: the actor and its authorization reference are judged here, so no row
64
+ * claims a person channel unless the mint made its actor (an environment approval saying elicitation:claude is
65
+ * written as unattested:elicitation:claude). Re-chaining (ledger-rechain) re-writes recorded events verbatim and does
66
+ * not come through here.
67
+ */
68
+ function writtenBody(body) {
69
+ const authorization = (0, human_channel_1.ledgerAuthorization)(body.authorization, body.actor);
70
+ return { ...body, actor: (0, human_channel_1.ledgerActor)(body.actor), ...(authorization !== undefined ? { authorization } : {}) };
71
+ }
60
72
  exports.GENESIS = 'GENESIS';
61
73
  /** Ordered-key serialization so identical bodies always produce identical bytes (hash stability). */
62
74
  function canonicalize(b) {
@@ -149,7 +161,7 @@ function verifyChain(chain, key) {
149
161
  * credentials).
150
162
  */
151
163
  function authorizationRef(actor, token) {
152
- return `${actor}:${crypto.createHash('sha256').update(token).digest('hex').slice(0, 12)}`;
164
+ return `${(0, human_channel_1.ledgerActor)(actor)}:${crypto.createHash('sha256').update(token).digest('hex').slice(0, 12)}`;
153
165
  }
154
166
  /**
155
167
  * Redact a tool's target before it enters the audit trail.
@@ -383,7 +395,7 @@ class ProvenanceChain {
383
395
  /** The append itself, assuming exclusion is already held. Only callers INSIDE a hold may use it. */
384
396
  appendUnlocked(body) {
385
397
  const chain = this.load();
386
- const evt = chainNext(chain, body, process.env.HOLMES_LEDGER_KEY || undefined);
398
+ const evt = chainNext(chain, writtenBody(body), process.env.HOLMES_LEDGER_KEY || undefined);
387
399
  fs.mkdirSync(path.dirname(this.file), { recursive: true });
388
400
  fs.appendFileSync(this.file, JSON.stringify(evt) + '\n', 'utf8');
389
401
  return evt;
@@ -435,7 +447,10 @@ function consumeNonceExclusively(nonce, ledgerFile, body, opts) {
435
447
  // @implements A-SPEC-148
436
448
  // The spent-check spans EVERY replica chain, not just the file being written. Judging from one
437
449
  // chain alone would let a single-use approval be spent once per replica, resurrecting REQ-141's
438
- // double-spend along a new axis. NAMED LIMIT: within one machine the lock makes this exact;
450
+ // double-spend along a new axis. NAMED LIMIT: this lock is one chain's, and since the replica split
451
+ // two writers with different replica ids hold different locks, so even on one machine both can pass
452
+ // (A-SPEC-245.8 corrects the earlier "within one machine the lock makes this exact"; the workspace
453
+ // spend lock of approval-spend.ts covers the callers moved there, A-SPEC-245.9). And
439
454
  // across machines there is no shared filesystem to lock, so two machines racing the same nonce
440
455
  // can both pass and the duplicate is DETECTED afterwards in the merged ledger rather than
441
456
  // prevented. Preventing it needs a shared authority, which is out of this slice's scope.
@@ -1,4 +1,5 @@
1
1
  import { ProvenanceChain, ProvenanceEvent } from './provenance-chain';
2
+ import { type SpendNamespace } from './spend-kinds';
2
3
  /**
3
4
  * The ledger as a SET of per-replica chains, read together and written one at a time.
4
5
  *
@@ -34,6 +35,8 @@ export interface DoubleSpend {
34
35
  hash: string;
35
36
  ts: string;
36
37
  }[];
38
+ /** @implements A-SPEC-245.9 (N10) — a nonce one clone revoked and another consumed; absent for a double spend. */
39
+ kind?: 'revoked-and-consumed';
37
40
  }
38
41
  /** A-SPEC-642 — the event kind an operator's reconciliation appends. */
39
42
  export declare const DOUBLE_SPEND_RECONCILED = "nonce-double-spend-reconciled";
@@ -46,7 +49,7 @@ export declare const DOUBLE_SPEND_RECONCILED = "nonce-double-spend-reconciled";
46
49
  * nonce input AND `event:<hash>` for every member — a later consumption of the same nonce is a new
47
50
  * event the old reconciliation never saw, so the group is open again.
48
51
  */
49
- export declare function unreconciledDoubleSpends(events: LedgerEvent[]): DoubleSpend[];
52
+ export declare function unreconciledDoubleSpends(events: LedgerEvent[], ns?: SpendNamespace): DoubleSpend[];
50
53
  export interface LedgerVerifyResult {
51
54
  ok: boolean;
52
55
  /** Per-chain failures, naming the replica so a report is actionable rather than merely alarming. */
@@ -55,7 +58,7 @@ export interface LedgerVerifyResult {
55
58
  file: string;
56
59
  detail?: string;
57
60
  brokenAt?: number;
58
- kind?: 'chain' | 'double-spend';
61
+ kind?: 'chain' | 'double-spend' | 'revoked-and-consumed';
59
62
  nonce?: string;
60
63
  events?: DoubleSpend['events'];
61
64
  }[];
@@ -133,5 +136,5 @@ export declare class ProvenanceLedger {
133
136
  * per replica, which would resurrect REQ-141's double-spend along a new axis. The question is only
134
137
  * meaningful across the whole set.
135
138
  */
136
- isNonceConsumed(nonce: string): boolean;
139
+ isNonceConsumed(nonce: string, ns?: SpendNamespace): boolean;
137
140
  }
@@ -37,13 +37,15 @@ exports.ProvenanceLedger = exports.DOUBLE_SPEND_RECONCILED = void 0;
37
37
  exports.unreconciledDoubleSpends = unreconciledDoubleSpends;
38
38
  exports.isLedgerFilename = isLedgerFilename;
39
39
  // @implements A-SPEC-206, A-SPEC-624
40
- // @implements A-SPEC-642
40
+ // @implements A-SPEC-642, A-SPEC-245.9, A-SPEC-245.16
41
41
  const execution_context_1 = require("../project/execution-context");
42
42
  // @implements A-SPEC-148
43
43
  const fs = __importStar(require("node:fs"));
44
44
  const path = __importStar(require("node:path"));
45
45
  const provenance_chain_1 = require("./provenance-chain");
46
46
  const replica_id_1 = require("./replica-id");
47
+ // @implements A-SPEC-794.4 — the spend and revoke kinds come from the one namespace table
48
+ const spend_kinds_1 = require("./spend-kinds");
47
49
  /** A-SPEC-642 — the event kind an operator's reconciliation appends. */
48
50
  exports.DOUBLE_SPEND_RECONCILED = 'nonce-double-spend-reconciled';
49
51
  /**
@@ -55,28 +57,57 @@ exports.DOUBLE_SPEND_RECONCILED = 'nonce-double-spend-reconciled';
55
57
  * nonce input AND `event:<hash>` for every member — a later consumption of the same nonce is a new
56
58
  * event the old reconciliation never saw, so the group is open again.
57
59
  */
58
- function unreconciledDoubleSpends(events) {
60
+ function unreconciledDoubleSpends(events, ns = 'grant') {
61
+ // @implements A-SPEC-245.9 (N4) — grouped by the canonical fingerprint: every input but an event: reference is
62
+ // normalised, so a row written before fingerprints (the raw nonce) and a fingerprint row of the same nonce are one
63
+ // group; a reconciliation's inputs are normalised the same way, so one that named the raw nonce still closes it
59
64
  const groups = new Map();
60
- for (const e of events) {
61
- if (e.kind !== 'nonce-consumed')
62
- continue;
63
- for (const input of e.inputs ?? []) {
64
- const list = groups.get(input) ?? [];
65
+ const revocations = new Map();
66
+ // the name a group is reported by: the input its rows wrote when they all wrote one, else the fingerprint — so a
67
+ // group that was reported (and reconciled) by its raw input before N4 keeps that name
68
+ const written = new Map();
69
+ const add = (into, e) => {
70
+ for (const raw of new Set(e.inputs ?? [])) {
71
+ const input = canonicalInput(raw);
72
+ const list = into.get(input) ?? [];
73
+ if (list.some((x) => x.hash === e.hash && x.replicaId === e.replicaId))
74
+ continue; // one row, two spellings
65
75
  list.push({ replicaId: e.replicaId, seq: e.seq, hash: e.hash, ts: e.ts });
66
- groups.set(input, list);
76
+ into.set(input, list);
77
+ written.set(input, (written.get(input) ?? new Set()).add(raw));
67
78
  }
79
+ };
80
+ const nameOf = (key) => { const w = written.get(key); return w && w.size === 1 ? [...w][0] : key; };
81
+ for (const e of events) {
82
+ if (e.kind === (0, spend_kinds_1.spendKind)(ns))
83
+ add(groups, e);
84
+ else if (e.kind === (0, spend_kinds_1.revokeKind)(ns))
85
+ add(revocations, e);
68
86
  }
69
- const reconciliations = events.filter((e) => e.kind === exports.DOUBLE_SPEND_RECONCILED).map((e) => new Set(e.inputs ?? []));
87
+ const reconciliations = events.filter((e) => e.kind === exports.DOUBLE_SPEND_RECONCILED).map((e) => new Set((e.inputs ?? []).map(canonicalInput)));
88
+ const covered = (nonce, list) => reconciliations.some((inputs) => inputs.has(nonce) && list.every((ev) => inputs.has('event:' + ev.hash)));
70
89
  const out = [];
71
90
  for (const [nonce, list] of groups) {
72
- if (list.length < 2)
73
- continue;
74
- const covered = reconciliations.some((inputs) => inputs.has(nonce) && list.every((ev) => inputs.has('event:' + ev.hash)));
75
- if (!covered)
76
- out.push({ nonce, events: list });
91
+ if (list.length >= 2 && !covered(nonce, list))
92
+ out.push({ nonce: nameOf(nonce), events: list });
93
+ // @implements A-SPEC-245.9 (N10) — revoked on one clone and consumed on another: refused like a double spend,
94
+ // cleared by the reconcile row naming the nonce and every revoking and consuming event
95
+ // @implements A-SPEC-245.16 (item 2) — whatever the spend count: a double spend's reconcile, which names only the
96
+ // spends, does not close the revocation that was overrun
97
+ const revoked = revocations.get(nonce);
98
+ if (revoked) {
99
+ const all = [...revoked, ...list];
100
+ if (!covered(nonce, all))
101
+ out.push({ nonce: nameOf(nonce), events: all, kind: 'revoked-and-consumed' });
102
+ }
77
103
  }
78
104
  return out.sort((a, b) => (a.nonce < b.nonce ? -1 : a.nonce > b.nonce ? 1 : 0));
79
105
  }
106
+ const FINGERPRINT_INPUT = /^nonce:[0-9a-f]{12}$/;
107
+ /** @implements A-SPEC-245.9 (N4) — an input as grouping reads it: a fingerprint, or an event: reference as it is. */
108
+ function canonicalInput(input) {
109
+ return input.startsWith('event:') || FINGERPRINT_INPUT.test(input) ? input : (0, provenance_chain_1.nonceFingerprint)(input);
110
+ }
80
111
  const LEGACY = 'provenance.jsonl';
81
112
  const REPLICA_FILE = /^provenance\.([^.]+)\.jsonl$/;
82
113
  /**
@@ -248,8 +279,12 @@ class ProvenanceLedger {
248
279
  // end "at seq undefined" with no act that could ever clear it.
249
280
  for (const d of unreconciledDoubleSpends(this.loadAll())) {
250
281
  const replicas = [...new Set(d.events.map((e) => e.replicaId ?? 'legacy'))].join(', ');
251
- broken.push({ replicaId: null, file: '(cross-replica)', kind: 'double-spend', nonce: d.nonce, events: d.events,
252
- detail: `single-use nonce ${d.nonce} spent ${d.events.length} times across replicas (${replicas}) — REQ-141 double-spend; reconcile with ledger_reconcile` });
282
+ broken.push(d.kind === 'revoked-and-consumed'
283
+ // @implements A-SPEC-245.9 (N10)
284
+ ? { replicaId: null, file: '(cross-replica)', kind: 'revoked-and-consumed', nonce: d.nonce, events: d.events,
285
+ detail: `single-use nonce ${d.nonce} was revoked and also consumed across replicas (${replicas}) — the act it allowed ran after its revocation; reconcile with ledger_reconcile` }
286
+ : { replicaId: null, file: '(cross-replica)', kind: 'double-spend', nonce: d.nonce, events: d.events,
287
+ detail: `single-use nonce ${d.nonce} spent ${d.events.length} times across replicas (${replicas}) — REQ-141 double-spend; reconcile with ledger_reconcile` });
253
288
  }
254
289
  return { ok: broken.length === 0, broken };
255
290
  }
@@ -276,11 +311,11 @@ class ProvenanceLedger {
276
311
  * per replica, which would resurrect REQ-141's double-spend along a new axis. The question is only
277
312
  * meaningful across the whole set.
278
313
  */
279
- isNonceConsumed(nonce) {
314
+ isNonceConsumed(nonce, ns = 'grant') {
280
315
  // @implements A-SPEC-191 §11 — fingerprint OR raw value: records written before nonces stopped
281
316
  // travelling in the clear must keep denying their replays.
282
317
  const fp = (0, provenance_chain_1.nonceFingerprint)(nonce);
283
- return this.loadAll().some((e) => e.kind === 'nonce-consumed'
318
+ return this.loadAll().some((e) => e.kind === (0, spend_kinds_1.spendKind)(ns)
284
319
  && ((e.inputs ?? []).includes(nonce) || (e.inputs ?? []).includes(fp)));
285
320
  }
286
321
  }
@@ -36,7 +36,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.effectiveHooksDir = effectiveHooksDir;
37
37
  exports.runPushReviewRefusal = runPushReviewRefusal;
38
38
  exports.runPrePush = runPrePush;
39
- // @implements A-SPEC-509.1, A-SPEC-734, A-SPEC-102.4, A-SPEC-774.21
39
+ // @implements A-SPEC-509.1, A-SPEC-734, A-SPEC-102.4, A-SPEC-774.21, A-SPEC-102.12
40
40
  /**
41
41
  * The push gate's IMPURE half: collect (stdin lines, git facts, the evidence ledger) and hand
42
42
  * everything to the pure judgePush. Any collection failure ALLOWS — this gate safens the default
@@ -86,21 +86,25 @@ function runPushReviewRefusal(cwd, env, sha) {
86
86
  if (!mode.id)
87
87
  return cannot('the run has no id');
88
88
  const { listRuns, mainCheckoutOf } = require('../project/run-records');
89
+ // A-SPEC-102.32 — non-strict: a forged pointer finds no run record and an unreadable one throws; both refuse, named
89
90
  const rec = listRuns(mainCheckoutOf(cwd)).find((r) => r.run_id === mode.id);
90
91
  if (!rec)
91
92
  return cannot(`no run record for ${mode.id}`);
92
- if (typeof rec.base !== 'string' || rec.base === '')
93
+ // @implements A-SPEC-102.12 — from the task's review base when the run declared one.
94
+ const { reviewBaseOf } = require('../guardrail/run-stages');
95
+ const base = reviewBaseOf(rec);
96
+ if (base === null)
93
97
  return cannot('run record carries no base');
94
- if (rec.base === sha)
98
+ if (base === sha)
95
99
  return null;
96
100
  const { gateRange } = require('../review/verdicts');
97
- const g = gateRange(rec.worktree, rec.base, sha, {});
101
+ const g = gateRange(rec.worktree, base, sha, {});
98
102
  if (g.ok)
99
103
  return null;
100
- return `[Holmes-Kit push gate] the run's commits ${rec.base}..${sha} are not reviewed (${g.reasons.join('; ')}) — an independent reviewer records a verdict with `
101
- + `holmes-kit review record --base ${rec.base} --head ${sha} --verdict pass`
104
+ return `[Holmes-Kit push gate] the run's commits ${base}..${sha} are not reviewed (${g.reasons.join('; ')}) — an independent reviewer records a verdict with `
105
+ + `holmes-kit review record --base ${base} --head ${sha} --verdict pass`
102
106
  // @implements A-SPEC-102.9 — a person in the session answers the same verdict in the form.
103
- + ` (a person in the session: review_verdict with base ${rec.base}, head ${sha})`;
107
+ + ` (a person in the session: review_verdict with base ${base}, head ${sha})`;
104
108
  }
105
109
  catch (e) {
106
110
  return cannot(e instanceof Error ? e.message : String(e));