open-multi-agent-kit 1.0.0 → 1.2.2

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 (622) hide show
  1. package/CHANGELOG.md +52 -1
  2. package/README.md +2 -2
  3. package/dist/approval-api.d.ts +5 -0
  4. package/dist/approval-api.d.ts.map +1 -0
  5. package/dist/approval-api.js +5 -0
  6. package/dist/approval-api.js.map +1 -0
  7. package/dist/cli/help.d.ts.map +1 -1
  8. package/dist/cli/help.js +1 -0
  9. package/dist/cli/help.js.map +1 -1
  10. package/dist/cli/session-picker.d.ts +2 -2
  11. package/dist/cli/session-picker.d.ts.map +1 -1
  12. package/dist/cli/session-picker.js.map +1 -1
  13. package/dist/commands/verified-run-cli.d.ts.map +1 -1
  14. package/dist/commands/verified-run-cli.js +69 -22
  15. package/dist/commands/verified-run-cli.js.map +1 -1
  16. package/dist/commands/verified-run-signal.d.ts +3 -0
  17. package/dist/commands/verified-run-signal.d.ts.map +1 -0
  18. package/dist/commands/verified-run-signal.js +15 -0
  19. package/dist/commands/verified-run-signal.js.map +1 -0
  20. package/dist/coordination/awareness.d.ts +32 -0
  21. package/dist/coordination/awareness.d.ts.map +1 -0
  22. package/dist/coordination/awareness.js +55 -0
  23. package/dist/coordination/awareness.js.map +1 -0
  24. package/dist/coordination/broker.d.ts +79 -0
  25. package/dist/coordination/broker.d.ts.map +1 -0
  26. package/dist/coordination/broker.js +202 -0
  27. package/dist/coordination/broker.js.map +1 -0
  28. package/dist/coordination/index.d.ts +16 -0
  29. package/dist/coordination/index.d.ts.map +1 -0
  30. package/dist/coordination/index.js +16 -0
  31. package/dist/coordination/index.js.map +1 -0
  32. package/dist/coordination/integration.d.ts +59 -0
  33. package/dist/coordination/integration.d.ts.map +1 -0
  34. package/dist/coordination/integration.js +126 -0
  35. package/dist/coordination/integration.js.map +1 -0
  36. package/dist/coordination/operation.d.ts +107 -0
  37. package/dist/coordination/operation.d.ts.map +1 -0
  38. package/dist/coordination/operation.js +231 -0
  39. package/dist/coordination/operation.js.map +1 -0
  40. package/dist/coordination/resource.d.ts +33 -0
  41. package/dist/coordination/resource.d.ts.map +1 -0
  42. package/dist/coordination/resource.js +95 -0
  43. package/dist/coordination/resource.js.map +1 -0
  44. package/dist/coordination/session.d.ts +74 -0
  45. package/dist/coordination/session.d.ts.map +1 -0
  46. package/dist/coordination/session.js +143 -0
  47. package/dist/coordination/session.js.map +1 -0
  48. package/dist/coordination/types.d.ts +68 -0
  49. package/dist/coordination/types.d.ts.map +1 -0
  50. package/dist/coordination/types.js +26 -0
  51. package/dist/coordination/types.js.map +1 -0
  52. package/dist/core/adaptorch-bridge.d.ts +1 -1
  53. package/dist/core/adaptorch-bridge.d.ts.map +1 -1
  54. package/dist/core/adaptorch-bridge.js +1 -1
  55. package/dist/core/adaptorch-bridge.js.map +1 -1
  56. package/dist/core/agent-session-runtime.d.ts.map +1 -1
  57. package/dist/core/agent-session-runtime.js +16 -16
  58. package/dist/core/agent-session-runtime.js.map +1 -1
  59. package/dist/core/agent-session.d.ts.map +1 -1
  60. package/dist/core/agent-session.js +35 -15
  61. package/dist/core/agent-session.js.map +1 -1
  62. package/dist/core/canonical-json.d.ts +3 -0
  63. package/dist/core/canonical-json.d.ts.map +1 -0
  64. package/dist/core/canonical-json.js +20 -0
  65. package/dist/core/canonical-json.js.map +1 -0
  66. package/dist/core/compaction/compaction.d.ts +7 -3
  67. package/dist/core/compaction/compaction.d.ts.map +1 -1
  68. package/dist/core/compaction/compaction.js +14 -29
  69. package/dist/core/compaction/compaction.js.map +1 -1
  70. package/dist/core/compaction/control-state.d.ts +41 -0
  71. package/dist/core/compaction/control-state.d.ts.map +1 -0
  72. package/dist/core/compaction/control-state.js +85 -0
  73. package/dist/core/compaction/control-state.js.map +1 -0
  74. package/dist/core/compaction/fallback.d.ts +71 -0
  75. package/dist/core/compaction/fallback.d.ts.map +1 -0
  76. package/dist/core/compaction/fallback.js +118 -0
  77. package/dist/core/compaction/fallback.js.map +1 -0
  78. package/dist/core/compaction/index.d.ts +2 -0
  79. package/dist/core/compaction/index.d.ts.map +1 -1
  80. package/dist/core/compaction/index.js +2 -0
  81. package/dist/core/compaction/index.js.map +1 -1
  82. package/dist/core/compaction/model-failover.d.ts +21 -0
  83. package/dist/core/compaction/model-failover.d.ts.map +1 -0
  84. package/dist/core/compaction/model-failover.js +46 -0
  85. package/dist/core/compaction/model-failover.js.map +1 -0
  86. package/dist/core/compaction/session-summary.d.ts +21 -0
  87. package/dist/core/compaction/session-summary.d.ts.map +1 -0
  88. package/dist/core/compaction/session-summary.js +27 -0
  89. package/dist/core/compaction/session-summary.js.map +1 -0
  90. package/dist/core/context-budget-headroom-candidates.d.ts +16 -1
  91. package/dist/core/context-budget-headroom-candidates.d.ts.map +1 -1
  92. package/dist/core/context-budget-headroom-candidates.js +31 -13
  93. package/dist/core/context-budget-headroom-candidates.js.map +1 -1
  94. package/dist/core/context-budget-headroom-types.d.ts +7 -1
  95. package/dist/core/context-budget-headroom-types.d.ts.map +1 -1
  96. package/dist/core/context-budget-headroom-types.js +0 -1
  97. package/dist/core/context-budget-headroom-types.js.map +1 -1
  98. package/dist/core/context-budget-headroom.d.ts +10 -0
  99. package/dist/core/context-budget-headroom.d.ts.map +1 -1
  100. package/dist/core/context-budget-headroom.js +11 -2
  101. package/dist/core/context-budget-headroom.js.map +1 -1
  102. package/dist/core/context-budget-v2-global-pass.d.ts +21 -0
  103. package/dist/core/context-budget-v2-global-pass.d.ts.map +1 -0
  104. package/dist/core/context-budget-v2-global-pass.js +203 -0
  105. package/dist/core/context-budget-v2-global-pass.js.map +1 -0
  106. package/dist/core/context-budget-v2-planned-items.d.ts +12 -0
  107. package/dist/core/context-budget-v2-planned-items.d.ts.map +1 -1
  108. package/dist/core/context-budget-v2-planned-items.js +21 -2
  109. package/dist/core/context-budget-v2-planned-items.js.map +1 -1
  110. package/dist/core/context-budget-v2-planner.d.ts.map +1 -1
  111. package/dist/core/context-budget-v2-planner.js +18 -19
  112. package/dist/core/context-budget-v2-planner.js.map +1 -1
  113. package/dist/core/context-budget-v2-scoring.d.ts +7 -1
  114. package/dist/core/context-budget-v2-scoring.d.ts.map +1 -1
  115. package/dist/core/context-budget-v2-scoring.js.map +1 -1
  116. package/dist/core/context-budget-v2-selection.d.ts +3 -0
  117. package/dist/core/context-budget-v2-selection.d.ts.map +1 -1
  118. package/dist/core/context-budget-v2-selection.js +6 -2
  119. package/dist/core/context-budget-v2-selection.js.map +1 -1
  120. package/dist/core/context-budget-v2-types.d.ts +6 -1
  121. package/dist/core/context-budget-v2-types.d.ts.map +1 -1
  122. package/dist/core/context-budget-v2-types.js +6 -1
  123. package/dist/core/context-budget-v2-types.js.map +1 -1
  124. package/dist/core/export-html/template.js +14 -0
  125. package/dist/core/extensions/index.d.ts +1 -1
  126. package/dist/core/extensions/index.d.ts.map +1 -1
  127. package/dist/core/extensions/index.js.map +1 -1
  128. package/dist/core/extensions/session-lifecycle-types.d.ts +14 -0
  129. package/dist/core/extensions/session-lifecycle-types.d.ts.map +1 -0
  130. package/dist/core/extensions/session-lifecycle-types.js +2 -0
  131. package/dist/core/extensions/session-lifecycle-types.js.map +1 -0
  132. package/dist/core/extensions/types.d.ts +4 -9
  133. package/dist/core/extensions/types.d.ts.map +1 -1
  134. package/dist/core/extensions/types.js.map +1 -1
  135. package/dist/core/http-dispatcher.d.ts.map +1 -1
  136. package/dist/core/http-dispatcher.js +10 -1
  137. package/dist/core/http-dispatcher.js.map +1 -1
  138. package/dist/core/keybindings.d.ts.map +1 -1
  139. package/dist/core/keybindings.js +3 -6
  140. package/dist/core/keybindings.js.map +1 -1
  141. package/dist/core/legacy-resource-policy.d.ts +7 -0
  142. package/dist/core/legacy-resource-policy.d.ts.map +1 -0
  143. package/dist/core/legacy-resource-policy.js +9 -0
  144. package/dist/core/legacy-resource-policy.js.map +1 -0
  145. package/dist/core/mcp/config.d.ts.map +1 -1
  146. package/dist/core/mcp/config.js +17 -3
  147. package/dist/core/mcp/config.js.map +1 -1
  148. package/dist/core/mcp/connection-queue.d.ts +12 -0
  149. package/dist/core/mcp/connection-queue.d.ts.map +1 -0
  150. package/dist/core/mcp/connection-queue.js +46 -0
  151. package/dist/core/mcp/connection-queue.js.map +1 -0
  152. package/dist/core/mcp/manager-runtime.d.ts +55 -0
  153. package/dist/core/mcp/manager-runtime.d.ts.map +1 -0
  154. package/dist/core/mcp/manager-runtime.js +66 -0
  155. package/dist/core/mcp/manager-runtime.js.map +1 -0
  156. package/dist/core/mcp/manager.d.ts +12 -68
  157. package/dist/core/mcp/manager.d.ts.map +1 -1
  158. package/dist/core/mcp/manager.js +33 -113
  159. package/dist/core/mcp/manager.js.map +1 -1
  160. package/dist/core/mcp/protocol.d.ts.map +1 -1
  161. package/dist/core/mcp/protocol.js +70 -40
  162. package/dist/core/mcp/protocol.js.map +1 -1
  163. package/dist/core/package-manager.d.ts +4 -0
  164. package/dist/core/package-manager.d.ts.map +1 -1
  165. package/dist/core/package-manager.js +59 -129
  166. package/dist/core/package-manager.js.map +1 -1
  167. package/dist/core/package-resource-patterns.d.ts +7 -0
  168. package/dist/core/package-resource-patterns.d.ts.map +1 -0
  169. package/dist/core/package-resource-patterns.js +99 -0
  170. package/dist/core/package-resource-patterns.js.map +1 -0
  171. package/dist/core/provider-default-models.d.ts +1 -0
  172. package/dist/core/provider-default-models.d.ts.map +1 -1
  173. package/dist/core/provider-default-models.js +1 -0
  174. package/dist/core/provider-default-models.js.map +1 -1
  175. package/dist/core/provider-display-names.d.ts.map +1 -1
  176. package/dist/core/provider-display-names.js +1 -0
  177. package/dist/core/provider-display-names.js.map +1 -1
  178. package/dist/core/provider-resilience.d.ts +9 -0
  179. package/dist/core/provider-resilience.d.ts.map +1 -1
  180. package/dist/core/provider-resilience.js +15 -0
  181. package/dist/core/provider-resilience.js.map +1 -1
  182. package/dist/core/reasoning-router-resolver.d.ts +7 -0
  183. package/dist/core/reasoning-router-resolver.d.ts.map +1 -1
  184. package/dist/core/reasoning-router-resolver.js +21 -3
  185. package/dist/core/reasoning-router-resolver.js.map +1 -1
  186. package/dist/core/reasoning-router-v4.d.ts +13 -7
  187. package/dist/core/reasoning-router-v4.d.ts.map +1 -1
  188. package/dist/core/reasoning-router-v4.js +8 -5
  189. package/dist/core/reasoning-router-v4.js.map +1 -1
  190. package/dist/core/resource-loader.d.ts.map +1 -1
  191. package/dist/core/resource-loader.js +4 -8
  192. package/dist/core/resource-loader.js.map +1 -1
  193. package/dist/core/run-execution-api.d.ts +8 -1
  194. package/dist/core/run-execution-api.d.ts.map +1 -1
  195. package/dist/core/run-execution-api.js +4 -0
  196. package/dist/core/run-execution-api.js.map +1 -1
  197. package/dist/core/run-journal.d.ts +1 -2
  198. package/dist/core/run-journal.d.ts.map +1 -1
  199. package/dist/core/run-journal.js +2 -19
  200. package/dist/core/run-journal.js.map +1 -1
  201. package/dist/core/session-compaction-service.d.ts +15 -0
  202. package/dist/core/session-compaction-service.d.ts.map +1 -1
  203. package/dist/core/session-compaction-service.js +17 -4
  204. package/dist/core/session-compaction-service.js.map +1 -1
  205. package/dist/core/session-listing.d.ts +51 -0
  206. package/dist/core/session-listing.d.ts.map +1 -0
  207. package/dist/core/session-listing.js +111 -0
  208. package/dist/core/session-listing.js.map +1 -0
  209. package/dist/core/session-manager.d.ts +12 -37
  210. package/dist/core/session-manager.d.ts.map +1 -1
  211. package/dist/core/session-manager.js +12 -118
  212. package/dist/core/session-manager.js.map +1 -1
  213. package/dist/core/session-message-text.d.ts +5 -0
  214. package/dist/core/session-message-text.d.ts.map +1 -0
  215. package/dist/core/session-message-text.js +9 -0
  216. package/dist/core/session-message-text.js.map +1 -0
  217. package/dist/core/skills-catalog-cache.d.ts +2 -0
  218. package/dist/core/skills-catalog-cache.d.ts.map +1 -1
  219. package/dist/core/skills-catalog-cache.js +71 -17
  220. package/dist/core/skills-catalog-cache.js.map +1 -1
  221. package/dist/core/todo-runtime-state.d.ts +12 -0
  222. package/dist/core/todo-runtime-state.d.ts.map +1 -1
  223. package/dist/core/todo-runtime-state.js +23 -0
  224. package/dist/core/todo-runtime-state.js.map +1 -1
  225. package/dist/core/tools/read.d.ts.map +1 -1
  226. package/dist/core/tools/read.js +1 -1
  227. package/dist/core/tools/read.js.map +1 -1
  228. package/dist/core/verified-run/authority-clock.d.ts +3 -0
  229. package/dist/core/verified-run/authority-clock.d.ts.map +1 -0
  230. package/dist/core/verified-run/authority-clock.js +25 -0
  231. package/dist/core/verified-run/authority-clock.js.map +1 -0
  232. package/dist/core/verified-run/authority-errors.d.ts +10 -0
  233. package/dist/core/verified-run/authority-errors.d.ts.map +1 -0
  234. package/dist/core/verified-run/authority-errors.js +17 -0
  235. package/dist/core/verified-run/authority-errors.js.map +1 -0
  236. package/dist/core/verified-run/authority-events.d.ts +126 -0
  237. package/dist/core/verified-run/authority-events.d.ts.map +1 -0
  238. package/dist/core/verified-run/authority-events.js +482 -0
  239. package/dist/core/verified-run/authority-events.js.map +1 -0
  240. package/dist/core/verified-run/authority-journal.d.ts +27 -0
  241. package/dist/core/verified-run/authority-journal.d.ts.map +1 -0
  242. package/dist/core/verified-run/authority-journal.js +83 -0
  243. package/dist/core/verified-run/authority-journal.js.map +1 -0
  244. package/dist/core/verified-run/authority-meaning.d.ts +13 -0
  245. package/dist/core/verified-run/authority-meaning.d.ts.map +1 -0
  246. package/dist/core/verified-run/authority-meaning.js +40 -0
  247. package/dist/core/verified-run/authority-meaning.js.map +1 -0
  248. package/dist/core/verified-run/authority-runtime.d.ts +82 -0
  249. package/dist/core/verified-run/authority-runtime.d.ts.map +1 -0
  250. package/dist/core/verified-run/authority-runtime.js +169 -0
  251. package/dist/core/verified-run/authority-runtime.js.map +1 -0
  252. package/dist/core/verified-run/authority-store.d.ts +204 -0
  253. package/dist/core/verified-run/authority-store.d.ts.map +1 -0
  254. package/dist/core/verified-run/authority-store.js +852 -0
  255. package/dist/core/verified-run/authority-store.js.map +1 -0
  256. package/dist/core/verified-run/broker.d.ts +8 -1
  257. package/dist/core/verified-run/broker.d.ts.map +1 -1
  258. package/dist/core/verified-run/broker.js +94 -90
  259. package/dist/core/verified-run/broker.js.map +1 -1
  260. package/dist/core/verified-run/candidate-policy.d.ts +10 -0
  261. package/dist/core/verified-run/candidate-policy.d.ts.map +1 -0
  262. package/dist/core/verified-run/candidate-policy.js +33 -0
  263. package/dist/core/verified-run/candidate-policy.js.map +1 -0
  264. package/dist/core/verified-run/candidate.d.ts.map +1 -1
  265. package/dist/core/verified-run/candidate.js +5 -10
  266. package/dist/core/verified-run/candidate.js.map +1 -1
  267. package/dist/core/verified-run/coordinator.d.ts +35 -17
  268. package/dist/core/verified-run/coordinator.d.ts.map +1 -1
  269. package/dist/core/verified-run/coordinator.js +111 -57
  270. package/dist/core/verified-run/coordinator.js.map +1 -1
  271. package/dist/core/verified-run/dag-phase.d.ts.map +1 -1
  272. package/dist/core/verified-run/dag-phase.js +1 -1
  273. package/dist/core/verified-run/dag-phase.js.map +1 -1
  274. package/dist/core/verified-run/dag-recovery.d.ts +5 -1
  275. package/dist/core/verified-run/dag-recovery.d.ts.map +1 -1
  276. package/dist/core/verified-run/dag-recovery.js +10 -4
  277. package/dist/core/verified-run/dag-recovery.js.map +1 -1
  278. package/dist/core/verified-run/event-parser.d.ts.map +1 -1
  279. package/dist/core/verified-run/event-parser.js +33 -0
  280. package/dist/core/verified-run/event-parser.js.map +1 -1
  281. package/dist/core/verified-run/evidence-read-projection.d.ts +4 -0
  282. package/dist/core/verified-run/evidence-read-projection.d.ts.map +1 -0
  283. package/dist/core/verified-run/evidence-read-projection.js +27 -0
  284. package/dist/core/verified-run/evidence-read-projection.js.map +1 -0
  285. package/dist/core/verified-run/evidence.d.ts +12 -1
  286. package/dist/core/verified-run/evidence.d.ts.map +1 -1
  287. package/dist/core/verified-run/evidence.js +51 -2
  288. package/dist/core/verified-run/evidence.js.map +1 -1
  289. package/dist/core/verified-run/git-candidate-preflight.d.ts +4 -0
  290. package/dist/core/verified-run/git-candidate-preflight.d.ts.map +1 -0
  291. package/dist/core/verified-run/git-candidate-preflight.js +34 -0
  292. package/dist/core/verified-run/git-candidate-preflight.js.map +1 -0
  293. package/dist/core/verified-run/git-effect-supervisor.d.ts +24 -0
  294. package/dist/core/verified-run/git-effect-supervisor.d.ts.map +1 -0
  295. package/dist/core/verified-run/git-effect-supervisor.js +174 -0
  296. package/dist/core/verified-run/git-effect-supervisor.js.map +1 -0
  297. package/dist/core/verified-run/git-execution.d.ts +27 -0
  298. package/dist/core/verified-run/git-execution.d.ts.map +1 -0
  299. package/dist/core/verified-run/git-execution.js +118 -0
  300. package/dist/core/verified-run/git-execution.js.map +1 -0
  301. package/dist/core/verified-run/git-plumbing.d.ts +37 -0
  302. package/dist/core/verified-run/git-plumbing.d.ts.map +1 -0
  303. package/dist/core/verified-run/git-plumbing.js +171 -0
  304. package/dist/core/verified-run/git-plumbing.js.map +1 -0
  305. package/dist/core/verified-run/git-publication-worker.d.ts +2 -0
  306. package/dist/core/verified-run/git-publication-worker.d.ts.map +1 -0
  307. package/dist/core/verified-run/git-publication-worker.js +54 -0
  308. package/dist/core/verified-run/git-publication-worker.js.map +1 -0
  309. package/dist/core/verified-run/git-sandbox-layout.d.ts +4 -0
  310. package/dist/core/verified-run/git-sandbox-layout.d.ts.map +1 -0
  311. package/dist/core/verified-run/git-sandbox-layout.js +29 -0
  312. package/dist/core/verified-run/git-sandbox-layout.js.map +1 -0
  313. package/dist/core/verified-run/git-worker-protocol.d.ts +19 -0
  314. package/dist/core/verified-run/git-worker-protocol.d.ts.map +1 -0
  315. package/dist/core/verified-run/git-worker-protocol.js +109 -0
  316. package/dist/core/verified-run/git-worker-protocol.js.map +1 -0
  317. package/dist/core/verified-run/git-worker-runtime.d.ts +19 -0
  318. package/dist/core/verified-run/git-worker-runtime.d.ts.map +1 -0
  319. package/dist/core/verified-run/git-worker-runtime.js +74 -0
  320. package/dist/core/verified-run/git-worker-runtime.js.map +1 -0
  321. package/dist/core/verified-run/journal.d.ts +2 -3
  322. package/dist/core/verified-run/journal.d.ts.map +1 -1
  323. package/dist/core/verified-run/journal.js.map +1 -1
  324. package/dist/core/verified-run/namespace-identity.d.ts.map +1 -1
  325. package/dist/core/verified-run/namespace-identity.js +1 -1
  326. package/dist/core/verified-run/namespace-identity.js.map +1 -1
  327. package/dist/core/verified-run/owned-execution.d.ts +10 -1
  328. package/dist/core/verified-run/owned-execution.d.ts.map +1 -1
  329. package/dist/core/verified-run/owned-execution.js +124 -31
  330. package/dist/core/verified-run/owned-execution.js.map +1 -1
  331. package/dist/core/verified-run/phase-context.d.ts +2 -0
  332. package/dist/core/verified-run/phase-context.d.ts.map +1 -1
  333. package/dist/core/verified-run/phase-context.js.map +1 -1
  334. package/dist/core/verified-run/projection.d.ts.map +1 -1
  335. package/dist/core/verified-run/projection.js +51 -1
  336. package/dist/core/verified-run/projection.js.map +1 -1
  337. package/dist/core/verified-run/publish-preflight.d.ts +12 -0
  338. package/dist/core/verified-run/publish-preflight.d.ts.map +1 -0
  339. package/dist/core/verified-run/publish-preflight.js +38 -0
  340. package/dist/core/verified-run/publish-preflight.js.map +1 -0
  341. package/dist/core/verified-run/publish-start.d.ts +5 -0
  342. package/dist/core/verified-run/publish-start.d.ts.map +1 -0
  343. package/dist/core/verified-run/publish-start.js +7 -0
  344. package/dist/core/verified-run/publish-start.js.map +1 -0
  345. package/dist/core/verified-run/recovery-projection.d.ts.map +1 -1
  346. package/dist/core/verified-run/recovery-projection.js +5 -0
  347. package/dist/core/verified-run/recovery-projection.js.map +1 -1
  348. package/dist/core/verified-run/recovery.d.ts +5 -1
  349. package/dist/core/verified-run/recovery.d.ts.map +1 -1
  350. package/dist/core/verified-run/recovery.js +10 -4
  351. package/dist/core/verified-run/recovery.js.map +1 -1
  352. package/dist/core/verified-run/run-plan.d.ts +17 -0
  353. package/dist/core/verified-run/run-plan.d.ts.map +1 -0
  354. package/dist/core/verified-run/run-plan.js +18 -0
  355. package/dist/core/verified-run/run-plan.js.map +1 -0
  356. package/dist/core/verified-run/run-publish-owned.d.ts +6 -0
  357. package/dist/core/verified-run/run-publish-owned.d.ts.map +1 -0
  358. package/dist/core/verified-run/run-publish-owned.js +199 -0
  359. package/dist/core/verified-run/run-publish-owned.js.map +1 -0
  360. package/dist/core/verified-run/run-publish.d.ts +19 -0
  361. package/dist/core/verified-run/run-publish.d.ts.map +1 -0
  362. package/dist/core/verified-run/run-publish.js +210 -0
  363. package/dist/core/verified-run/run-publish.js.map +1 -0
  364. package/dist/core/verified-run/run-status-owned.d.ts +4 -0
  365. package/dist/core/verified-run/run-status-owned.d.ts.map +1 -0
  366. package/dist/core/verified-run/run-status-owned.js +14 -0
  367. package/dist/core/verified-run/run-status-owned.js.map +1 -0
  368. package/dist/core/verified-run/run-status.d.ts +133 -0
  369. package/dist/core/verified-run/run-status.d.ts.map +1 -0
  370. package/dist/core/verified-run/run-status.js +193 -0
  371. package/dist/core/verified-run/run-status.js.map +1 -0
  372. package/dist/core/verified-run/run-types.d.ts +31 -0
  373. package/dist/core/verified-run/run-types.d.ts.map +1 -1
  374. package/dist/core/verified-run/run-types.js.map +1 -1
  375. package/dist/core/verified-run/storage.d.ts.map +1 -1
  376. package/dist/core/verified-run/storage.js +1 -1
  377. package/dist/core/verified-run/storage.js.map +1 -1
  378. package/dist/core/verified-run/supervisor-adapter.d.ts +77 -0
  379. package/dist/core/verified-run/supervisor-adapter.d.ts.map +1 -0
  380. package/dist/core/verified-run/supervisor-adapter.js +164 -0
  381. package/dist/core/verified-run/supervisor-adapter.js.map +1 -0
  382. package/dist/core/verified-run/verification-phase.d.ts.map +1 -1
  383. package/dist/core/verified-run/verification-phase.js +1 -1
  384. package/dist/core/verified-run/verification-phase.js.map +1 -1
  385. package/dist/core/verified-run/writer-phase.d.ts.map +1 -1
  386. package/dist/core/verified-run/writer-phase.js +1 -0
  387. package/dist/core/verified-run/writer-phase.js.map +1 -1
  388. package/dist/core/verified-run/writer-recovery.d.ts +2 -0
  389. package/dist/core/verified-run/writer-recovery.d.ts.map +1 -1
  390. package/dist/core/verified-run/writer-recovery.js +1 -0
  391. package/dist/core/verified-run/writer-recovery.js.map +1 -1
  392. package/dist/core/workload-shard-execution-types.d.ts +4 -1
  393. package/dist/core/workload-shard-execution-types.d.ts.map +1 -1
  394. package/dist/core/workload-shard-execution-types.js.map +1 -1
  395. package/dist/core/workload-shard-executor.d.ts +2 -2
  396. package/dist/core/workload-shard-executor.d.ts.map +1 -1
  397. package/dist/core/workload-shard-executor.js +16 -37
  398. package/dist/core/workload-shard-executor.js.map +1 -1
  399. package/dist/core/workload-shard-frontier.d.ts +5 -0
  400. package/dist/core/workload-shard-frontier.d.ts.map +1 -0
  401. package/dist/core/workload-shard-frontier.js +47 -0
  402. package/dist/core/workload-shard-frontier.js.map +1 -0
  403. package/dist/core/workload-shard-runner.d.ts.map +1 -1
  404. package/dist/core/workload-shard-runner.js +28 -17
  405. package/dist/core/workload-shard-runner.js.map +1 -1
  406. package/dist/index.d.ts +4 -5
  407. package/dist/index.d.ts.map +1 -1
  408. package/dist/index.js +3 -4
  409. package/dist/index.js.map +1 -1
  410. package/dist/main.d.ts.map +1 -1
  411. package/dist/main.js +2 -1
  412. package/dist/main.js.map +1 -1
  413. package/dist/metacognition/bridge-result.d.ts +148 -0
  414. package/dist/metacognition/bridge-result.d.ts.map +1 -0
  415. package/dist/metacognition/bridge-result.js +46 -0
  416. package/dist/metacognition/bridge-result.js.map +1 -0
  417. package/dist/metacognition/calibration-selective.d.ts +78 -0
  418. package/dist/metacognition/calibration-selective.d.ts.map +1 -0
  419. package/dist/metacognition/calibration-selective.js +167 -0
  420. package/dist/metacognition/calibration-selective.js.map +1 -0
  421. package/dist/metacognition/calibration.d.ts +62 -0
  422. package/dist/metacognition/calibration.d.ts.map +1 -0
  423. package/dist/metacognition/calibration.js +122 -0
  424. package/dist/metacognition/calibration.js.map +1 -0
  425. package/dist/metacognition/checkpoint.d.ts +60 -0
  426. package/dist/metacognition/checkpoint.d.ts.map +1 -0
  427. package/dist/metacognition/checkpoint.js +57 -0
  428. package/dist/metacognition/checkpoint.js.map +1 -0
  429. package/dist/metacognition/context7.d.ts +48 -0
  430. package/dist/metacognition/context7.d.ts.map +1 -0
  431. package/dist/metacognition/context7.js +154 -0
  432. package/dist/metacognition/context7.js.map +1 -0
  433. package/dist/metacognition/decision.d.ts +44 -0
  434. package/dist/metacognition/decision.d.ts.map +1 -0
  435. package/dist/metacognition/decision.js +111 -0
  436. package/dist/metacognition/decision.js.map +1 -0
  437. package/dist/metacognition/evaluation.d.ts +73 -0
  438. package/dist/metacognition/evaluation.d.ts.map +1 -0
  439. package/dist/metacognition/evaluation.js +97 -0
  440. package/dist/metacognition/evaluation.js.map +1 -0
  441. package/dist/metacognition/index.d.ts +32 -0
  442. package/dist/metacognition/index.d.ts.map +1 -0
  443. package/dist/metacognition/index.js +32 -0
  444. package/dist/metacognition/index.js.map +1 -0
  445. package/dist/metacognition/knowledge-action.d.ts +43 -0
  446. package/dist/metacognition/knowledge-action.d.ts.map +1 -0
  447. package/dist/metacognition/knowledge-action.js +60 -0
  448. package/dist/metacognition/knowledge-action.js.map +1 -0
  449. package/dist/metacognition/knowledge.d.ts +84 -0
  450. package/dist/metacognition/knowledge.d.ts.map +1 -0
  451. package/dist/metacognition/knowledge.js +165 -0
  452. package/dist/metacognition/knowledge.js.map +1 -0
  453. package/dist/metacognition/obligations.d.ts +82 -0
  454. package/dist/metacognition/obligations.d.ts.map +1 -0
  455. package/dist/metacognition/obligations.js +139 -0
  456. package/dist/metacognition/obligations.js.map +1 -0
  457. package/dist/metacognition/observation-validity.d.ts +83 -0
  458. package/dist/metacognition/observation-validity.d.ts.map +1 -0
  459. package/dist/metacognition/observation-validity.js +118 -0
  460. package/dist/metacognition/observation-validity.js.map +1 -0
  461. package/dist/metacognition/observe.d.ts +45 -0
  462. package/dist/metacognition/observe.d.ts.map +1 -0
  463. package/dist/metacognition/observe.js +59 -0
  464. package/dist/metacognition/observe.js.map +1 -0
  465. package/dist/metacognition/policy.d.ts +53 -0
  466. package/dist/metacognition/policy.d.ts.map +1 -0
  467. package/dist/metacognition/policy.js +155 -0
  468. package/dist/metacognition/policy.js.map +1 -0
  469. package/dist/metacognition/predictions.d.ts +78 -0
  470. package/dist/metacognition/predictions.d.ts.map +1 -0
  471. package/dist/metacognition/predictions.js +181 -0
  472. package/dist/metacognition/predictions.js.map +1 -0
  473. package/dist/metacognition/retrieval.d.ts +53 -0
  474. package/dist/metacognition/retrieval.d.ts.map +1 -0
  475. package/dist/metacognition/retrieval.js +170 -0
  476. package/dist/metacognition/retrieval.js.map +1 -0
  477. package/dist/metacognition/risk.d.ts +48 -0
  478. package/dist/metacognition/risk.d.ts.map +1 -0
  479. package/dist/metacognition/risk.js +179 -0
  480. package/dist/metacognition/risk.js.map +1 -0
  481. package/dist/metacognition/route-economics.d.ts +95 -0
  482. package/dist/metacognition/route-economics.d.ts.map +1 -0
  483. package/dist/metacognition/route-economics.js +110 -0
  484. package/dist/metacognition/route-economics.js.map +1 -0
  485. package/dist/metacognition/runtime-bridge.d.ts +81 -0
  486. package/dist/metacognition/runtime-bridge.d.ts.map +1 -0
  487. package/dist/metacognition/runtime-bridge.js +458 -0
  488. package/dist/metacognition/runtime-bridge.js.map +1 -0
  489. package/dist/metacognition/skills.d.ts +42 -0
  490. package/dist/metacognition/skills.d.ts.map +1 -0
  491. package/dist/metacognition/skills.js +220 -0
  492. package/dist/metacognition/skills.js.map +1 -0
  493. package/dist/metacognition/state.d.ts +110 -0
  494. package/dist/metacognition/state.d.ts.map +1 -0
  495. package/dist/metacognition/state.js +52 -0
  496. package/dist/metacognition/state.js.map +1 -0
  497. package/dist/metacognition/validation.d.ts +19 -0
  498. package/dist/metacognition/validation.d.ts.map +1 -0
  499. package/dist/metacognition/validation.js +58 -0
  500. package/dist/metacognition/validation.js.map +1 -0
  501. package/dist/metacognition/verification.d.ts +93 -0
  502. package/dist/metacognition/verification.d.ts.map +1 -0
  503. package/dist/metacognition/verification.js +132 -0
  504. package/dist/metacognition/verification.js.map +1 -0
  505. package/dist/metacognition/verifier.d.ts +49 -0
  506. package/dist/metacognition/verifier.d.ts.map +1 -0
  507. package/dist/metacognition/verifier.js +88 -0
  508. package/dist/metacognition/verifier.js.map +1 -0
  509. package/dist/modes/interactive/components/assistant-message.d.ts +2 -0
  510. package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -1
  511. package/dist/modes/interactive/components/assistant-message.js +42 -26
  512. package/dist/modes/interactive/components/assistant-message.js.map +1 -1
  513. package/dist/modes/interactive/components/chat-container.d.ts +7 -0
  514. package/dist/modes/interactive/components/chat-container.d.ts.map +1 -0
  515. package/dist/modes/interactive/components/chat-container.js +14 -0
  516. package/dist/modes/interactive/components/chat-container.js.map +1 -0
  517. package/dist/modes/interactive/components/dynamic-border.d.ts.map +1 -1
  518. package/dist/modes/interactive/components/dynamic-border.js +4 -2
  519. package/dist/modes/interactive/components/dynamic-border.js.map +1 -1
  520. package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -1
  521. package/dist/modes/interactive/components/login-dialog.js +0 -1
  522. package/dist/modes/interactive/components/login-dialog.js.map +1 -1
  523. package/dist/modes/interactive/components/session-selector-async-search.d.ts +33 -0
  524. package/dist/modes/interactive/components/session-selector-async-search.d.ts.map +1 -0
  525. package/dist/modes/interactive/components/session-selector-async-search.js +109 -0
  526. package/dist/modes/interactive/components/session-selector-async-search.js.map +1 -0
  527. package/dist/modes/interactive/components/session-selector-loaders.d.ts +6 -0
  528. package/dist/modes/interactive/components/session-selector-loaders.d.ts.map +1 -0
  529. package/dist/modes/interactive/components/session-selector-loaders.js +9 -0
  530. package/dist/modes/interactive/components/session-selector-loaders.js.map +1 -0
  531. package/dist/modes/interactive/components/session-selector-search.d.ts +1 -1
  532. package/dist/modes/interactive/components/session-selector-search.d.ts.map +1 -1
  533. package/dist/modes/interactive/components/session-selector-search.js.map +1 -1
  534. package/dist/modes/interactive/components/session-selector-tree.d.ts +16 -0
  535. package/dist/modes/interactive/components/session-selector-tree.d.ts.map +1 -0
  536. package/dist/modes/interactive/components/session-selector-tree.js +46 -0
  537. package/dist/modes/interactive/components/session-selector-tree.js.map +1 -0
  538. package/dist/modes/interactive/components/session-selector.d.ts +6 -1
  539. package/dist/modes/interactive/components/session-selector.d.ts.map +1 -1
  540. package/dist/modes/interactive/components/session-selector.js +36 -76
  541. package/dist/modes/interactive/components/session-selector.js.map +1 -1
  542. package/dist/modes/interactive/components/tool-execution-images.d.ts +32 -0
  543. package/dist/modes/interactive/components/tool-execution-images.d.ts.map +1 -0
  544. package/dist/modes/interactive/components/tool-execution-images.js +144 -0
  545. package/dist/modes/interactive/components/tool-execution-images.js.map +1 -0
  546. package/dist/modes/interactive/components/tool-execution.d.ts +3 -4
  547. package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
  548. package/dist/modes/interactive/components/tool-execution.js +22 -71
  549. package/dist/modes/interactive/components/tool-execution.js.map +1 -1
  550. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  551. package/dist/modes/interactive/interactive-mode.js +7 -9
  552. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  553. package/dist/modes/interactive/tui-diagnostics.d.ts +8 -4
  554. package/dist/modes/interactive/tui-diagnostics.d.ts.map +1 -1
  555. package/dist/modes/interactive/tui-diagnostics.js +35 -1
  556. package/dist/modes/interactive/tui-diagnostics.js.map +1 -1
  557. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
  558. package/dist/modes/rpc/rpc-mode.js +14 -17
  559. package/dist/modes/rpc/rpc-mode.js.map +1 -1
  560. package/dist/modes/rpc/rpc-response.d.ts +5 -0
  561. package/dist/modes/rpc/rpc-response.d.ts.map +1 -0
  562. package/dist/modes/rpc/rpc-response.js +17 -0
  563. package/dist/modes/rpc/rpc-response.js.map +1 -0
  564. package/dist/observation/identity.d.ts +14 -0
  565. package/dist/observation/identity.d.ts.map +1 -0
  566. package/dist/observation/identity.js +29 -0
  567. package/dist/observation/identity.js.map +1 -0
  568. package/dist/observation/index.d.ts +13 -0
  569. package/dist/observation/index.d.ts.map +1 -0
  570. package/dist/observation/index.js +13 -0
  571. package/dist/observation/index.js.map +1 -0
  572. package/dist/observation/observe-mode.d.ts +46 -0
  573. package/dist/observation/observe-mode.d.ts.map +1 -0
  574. package/dist/observation/observe-mode.js +83 -0
  575. package/dist/observation/observe-mode.js.map +1 -0
  576. package/dist/observation/store.d.ts +53 -0
  577. package/dist/observation/store.d.ts.map +1 -0
  578. package/dist/observation/store.js +126 -0
  579. package/dist/observation/store.js.map +1 -0
  580. package/dist/observation/types.d.ts +72 -0
  581. package/dist/observation/types.d.ts.map +1 -0
  582. package/dist/observation/types.js +9 -0
  583. package/dist/observation/types.js.map +1 -0
  584. package/dist/observation/view.d.ts +39 -0
  585. package/dist/observation/view.d.ts.map +1 -0
  586. package/dist/observation/view.js +173 -0
  587. package/dist/observation/view.js.map +1 -0
  588. package/dist/utils/exif-orientation.d.ts.map +1 -1
  589. package/dist/utils/exif-orientation.js +2 -3
  590. package/dist/utils/exif-orientation.js.map +1 -1
  591. package/dist/utils/tools-manager.d.ts.map +1 -1
  592. package/dist/utils/tools-manager.js +3 -5
  593. package/dist/utils/tools-manager.js.map +1 -1
  594. package/docs/adaptorch-preview-spec.md +7 -7
  595. package/docs/atomic-commit-planning.md +69 -0
  596. package/docs/compaction.md +26 -1
  597. package/docs/extensions.md +14 -0
  598. package/docs/mcp.md +22 -0
  599. package/docs/metacognition.md +130 -0
  600. package/docs/model-catalog-refresh.md +218 -1
  601. package/docs/models.md +1 -1
  602. package/docs/providers.md +53 -0
  603. package/docs/runtime-algorithms.md +156 -7
  604. package/docs/session-format.md +8 -0
  605. package/docs/sessions.md +19 -0
  606. package/docs/skills.md +19 -0
  607. package/docs/terminal-setup.md +15 -0
  608. package/docs/tui.md +41 -0
  609. package/docs/verified-run-safety.md +75 -0
  610. package/examples/extensions/custom-provider-anthropic/index.ts +1 -1
  611. package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
  612. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  613. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  614. package/examples/extensions/doom-overlay/wad-finder.ts +7 -3
  615. package/examples/extensions/gondolin/package-lock.json +2 -2
  616. package/examples/extensions/gondolin/package.json +1 -1
  617. package/examples/extensions/sandbox/package-lock.json +2 -2
  618. package/examples/extensions/sandbox/package.json +1 -1
  619. package/examples/extensions/with-deps/package-lock.json +2 -2
  620. package/examples/extensions/with-deps/package.json +1 -1
  621. package/npm-shrinkwrap.json +18 -18
  622. package/package.json +6 -8
@@ -0,0 +1,204 @@
1
+ import { type ResourceClaimInput } from "../../coordination/resource.ts";
2
+ import { type GrantToken, type Sequence } from "../../coordination/types.ts";
3
+ import { type AuthorityGrantRecord, type AuthorityProjection } from "./authority-events.ts";
4
+ import { type AuthorityRecord, type ParsedAuthority } from "./authority-journal.ts";
5
+ export type { AuthorityRecord } from "./authority-journal.ts";
6
+ import type { NamespaceIdentity } from "./namespace-identity.ts";
7
+ /**
8
+ * Durable authority store (WP03): the single-writer boundary for supervisor
9
+ * authorization state.
10
+ *
11
+ * Storage is a head-sidecar pair — `authority.events.jsonl` plus
12
+ * `authority.events.jsonl.head` — mirroring `ReplayLedgerStore`: the committed
13
+ * head is the transaction boundary, bytes after `head.size` are uncommitted
14
+ * and get quarantined then truncated on the next open, and every append is an
15
+ * expected-head CAS inside the `"mutation"` lock critical section. Cross-process
16
+ * exclusion is the `"authority"` scope durable-file lease held for the store's
17
+ * lifetime; its owner record (pid+host+token) is the writer identity, and dead
18
+ * owners are reclaimed by the existing reclaim protocol.
19
+ *
20
+ * Authority epoch durability: `open` appends `authority-epoch-advanced` inside
21
+ * the lease critical section — never the in-memory default "1" — and leaves the
22
+ * epoch unreconciled. Only `reconcile()` (termination-witness probes per
23
+ * quarantined effect, then `authority-reconciled`) reopens admission. A crash
24
+ * between the epoch event and reconciliation leaves `pendingReconcile` set;
25
+ * the next open adopts that epoch instead of advancing again.
26
+ */
27
+ export { AuthorityLeaseHeldError, AuthorityStoreError } from "./authority-errors.ts";
28
+ export declare const authorityStorePath: (root: string) => string;
29
+ export interface AuthorityHead {
30
+ readonly fileIdentity: {
31
+ readonly dev: string;
32
+ readonly ino: string;
33
+ } | null;
34
+ readonly size: number;
35
+ readonly lastSeq: number;
36
+ readonly lastHash: string;
37
+ }
38
+ export interface AuthorityStoreHooks {
39
+ /** Fires after the events file is fsynced but before the head publishes — the commit crash window. */
40
+ readonly afterLedgerFsync?: () => void;
41
+ /** Fires before a quarantine artifact is fsynced. */
42
+ readonly beforeQuarantineFsync?: () => void;
43
+ /** Fires after GC's atomic rewrite lands but before the new head publishes — the GC crash window. */
44
+ readonly afterAtomicRewrite?: () => void;
45
+ /** Test seam replacing the durable record append. */
46
+ readonly persistRecord?: (path: string, bytes: Uint8Array) => void;
47
+ }
48
+ export type AuthorityProbe = (grant: AuthorityGrantRecord) => "terminated" | "alive" | "unknown";
49
+ export interface OpenAuthorityStoreOptions {
50
+ /** Total admissible weight of concurrently active grants (broker parity). */
51
+ readonly capacity: number;
52
+ readonly hooks?: AuthorityStoreHooks;
53
+ readonly probe?: AuthorityProbe;
54
+ /** Trusted host clock; never supplied by a serialized command. */
55
+ readonly clock?: () => number;
56
+ /** Disable only the parsing cache; disk integrity and durability checks always run. */
57
+ readonly incrementalReplay?: boolean;
58
+ }
59
+ export interface AuthorityAcquireInput {
60
+ readonly sessionId: string;
61
+ readonly incarnation: Sequence | string;
62
+ /** Idempotency key; unique across grants and tombstones (I08/I31). */
63
+ readonly commandId: string;
64
+ readonly intentDigest: string;
65
+ readonly claims: readonly ResourceClaimInput[];
66
+ readonly now: number;
67
+ readonly ttl: number;
68
+ readonly weight?: number;
69
+ }
70
+ export type AuthorityAcquireResult = {
71
+ readonly status: "granted";
72
+ readonly token: GrantToken;
73
+ readonly duplicate: boolean;
74
+ } | {
75
+ readonly status: "blocked";
76
+ } | {
77
+ readonly status: "result";
78
+ readonly resultDigest: string;
79
+ readonly token: GrantToken;
80
+ } | {
81
+ readonly status: "result-expired";
82
+ };
83
+ export type AuthorityLookup = {
84
+ readonly status: "pending";
85
+ readonly token: GrantToken;
86
+ } | {
87
+ readonly status: "result";
88
+ readonly resultDigest: string;
89
+ readonly token: GrantToken;
90
+ } | {
91
+ readonly status: "result-expired";
92
+ readonly token: GrantToken;
93
+ } | {
94
+ readonly status: "unknown";
95
+ };
96
+ /** Committed projection plus the hashed records it was replayed from. */
97
+ export interface AuthorityJournalInspection {
98
+ readonly state: AuthorityProjection;
99
+ readonly records: readonly AuthorityRecord[];
100
+ readonly head: AuthorityHead;
101
+ }
102
+ /** The durable single-writer authority boundary. */
103
+ export declare class AuthorityStore {
104
+ private readonly context;
105
+ private readonly capacity;
106
+ private readonly probe;
107
+ private readonly lease;
108
+ private accepted;
109
+ private released;
110
+ private readonly now;
111
+ private readonly incrementalReplay;
112
+ private constructor();
113
+ /**
114
+ * Take the `"authority"` scope lease and advance the persisted epoch.
115
+ * When the previous epoch never reconciled, this restart adopts it — the
116
+ * pending reconciliation must finish before new admission, and the epoch
117
+ * is not bumped again (review §5 fault row).
118
+ */
119
+ static open(path: string, options: OpenAuthorityStoreOptions): AuthorityStore;
120
+ /** Read-only committed projection; performs no repair and writes nothing. */
121
+ static inspect(path: string): AuthorityProjection;
122
+ /**
123
+ * Read-only committed journal: projection plus the hashed records it was
124
+ * replayed from, so callers can see transition causes (cancel-requested vs
125
+ * authorization-expired vs epoch restart) and termination witnesses — the
126
+ * projection alone intentionally does not restate them.
127
+ */
128
+ static inspectJournal(path: string): AuthorityJournalInspection;
129
+ get path(): string;
130
+ get head(): AuthorityHead;
131
+ get state(): AuthorityProjection;
132
+ get lastReplay(): ParsedAuthority["replay"];
133
+ get pendingReconcile(): boolean;
134
+ private writerId;
135
+ /**
136
+ * Commit events as one transaction: mutation lock → committed-head CAS →
137
+ * reducer pre-validation → append+fsync → head publish → re-read
138
+ * verification. The head CAS is the fencing boundary — a store whose lease
139
+ * was reclaimed and taken by another writer sees a changed head and fails
140
+ * closed instead of authorizing. Persist failures never mint in-memory
141
+ * state (docs/04: no memory-only authorization after storage failure).
142
+ */
143
+ private commit;
144
+ private commitEpochAdvance;
145
+ /** Announce a session incarnation (S2); the counter survives restarts durably. */
146
+ register(sessionId: string): Sequence;
147
+ /**
148
+ * Admit an authorization: claim-overlap check, capacity check, idempotency
149
+ * replay, and the `grant-reserved` record in one lease/mutation-locked
150
+ * critical section — the check and the record are one transaction (docs/04).
151
+ * The lease is held for the store's lifetime, so committed state cannot
152
+ * move between the check and the append; the head CAS in commit() is the
153
+ * backstop if it ever could.
154
+ */
155
+ acquire(input: AuthorityAcquireInput): AuthorityAcquireResult;
156
+ private resolveToken;
157
+ /**
158
+ * Persisted dispatch intent. A committed intent whose spawn was never
159
+ * witnessed cannot be assumed un-run; on restart it quarantines like any
160
+ * other possibly-live effect (I01/I02).
161
+ */
162
+ dispatchIntent(token: GrantToken, dispatchId: string, now?: number): boolean;
163
+ /** Bind an effect to its reservation; actual claims must equal the reserved set exactly. */
164
+ effectStarted(token: GrantToken, actualClaims: readonly ResourceClaimInput[], identity?: NamespaceIdentity, now?: number): boolean;
165
+ /** Revalidate a running effect before its next irreversible boundary, without starting it again. */
166
+ effectAuthorized(token: GrantToken, actualClaims: readonly ResourceClaimInput[]): boolean;
167
+ /** Requested cancellation is not observed termination; live effects quarantine. */
168
+ cancel(token: GrantToken): boolean;
169
+ /** Settle lapsed authorizations; never evicts a live effect. */
170
+ expire(now: number): void;
171
+ /**
172
+ * Trusted supervisor witness: this exact effect is gone. Accepted across
173
+ * superseded epochs so a restarted authority can settle what the previous
174
+ * one started, never to authorize new work.
175
+ */
176
+ confirmTerminated(token: GrantToken): boolean;
177
+ /** Retain the final result as an idempotency tombstone (S3, docs/04). */
178
+ retainResult(commandId: string, resultDigest: string, now: number, retentionMs: number): void;
179
+ /** Command disposition without re-execution (I08/I31). */
180
+ lookup(commandId: string, now: number): AuthorityLookup;
181
+ /**
182
+ * Finish the restart reconciliation: every quarantined effect is compared
183
+ * against the real execution boundary — only an observed termination
184
+ * settles it ("gone"); "alive"/"unknown" stay quarantined and keep their
185
+ * claims (docs/04, review §6). Ends with `authority-reconciled` for the
186
+ * current epoch; the reconciled scope only then admits new work.
187
+ */
188
+ reconcile(probe?: AuthorityProbe): void;
189
+ /**
190
+ * GC (review §7): fold droppable state into an `authority-snapshot` and
191
+ * atomically rewrite the log to that snapshot. Retained: the epoch pair,
192
+ * incarnation counters, the grant counter (I31 — sequences never reissue),
193
+ * every unsettled grant, every settled grant still referenced by an
194
+ * unexpired tombstone, and the unexpired tombstones themselves. A crash
195
+ * between the atomic rewrite and head publish self-heals on next open via
196
+ * the snapshot continuation check.
197
+ */
198
+ compact(now: number): {
199
+ readonly dropped: number;
200
+ };
201
+ /** Release the authority lease; further commits fail closed. */
202
+ release(): void;
203
+ }
204
+ //# sourceMappingURL=authority-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authority-store.d.ts","sourceRoot":"","sources":["../../../src/core/verified-run/authority-store.ts"],"names":[],"mappings":"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","sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport {\n\tcloseSync,\n\texistsSync,\n\tconstants as fsConstants,\n\tfstatSync,\n\tfsyncSync,\n\tftruncateSync,\n\tmkdirSync,\n\topenSync,\n\treadFileSync,\n\trenameSync,\n\tunlinkSync,\n\twriteSync,\n} from \"node:fs\";\nimport { dirname, join } from \"node:path\";\nimport {\n\tcanonicalClaim,\n\tclaimSetsConflict,\n\ttype ResourceClaimInput,\n\tsameClaimSet,\n} from \"../../coordination/resource.ts\";\nimport { type GrantToken, SETTLED_EFFECT_STATES, type Sequence, sequence } from \"../../coordination/types.ts\";\nimport { atomicRewriteFileSync } from \"../atomic-session-file.ts\";\nimport {\n\tacquireDurableFileLockSync,\n\tacquireDurableFileMutationLockSync,\n\ttype DurableFileLock,\n\tDurableFileLockBusyError,\n\tinspectDurableFileLockSync,\n} from \"../durable-file-identity.ts\";\nimport { fsyncDirectorySync, writeExclusiveFileDurablySync } from \"../durable-file-io.ts\";\nimport { canonicalJson } from \"../run-journal.ts\";\nimport { createAuthorityClock } from \"./authority-clock.ts\";\nimport { AuthorityLeaseHeldError, AuthorityStoreError } from \"./authority-errors.ts\";\nimport {\n\ttype AuthorityEvent,\n\ttype AuthorityGrantRecord,\n\ttype AuthorityProjection,\n\tapplyAuthorityEvents,\n\tauthorityPendingReconcile,\n\tparseAuthorityEvent,\n\tsnapshotFromProjection,\n} from \"./authority-events.ts\";\nimport {\n\ttype AuthorityRecord,\n\tGENESIS_PREVIOUS,\n\tmaterializeRecord,\n\ttype ParsedAuthority,\n\tparseCommitted,\n\trecordLine,\n} from \"./authority-journal.ts\";\nimport { assertSameCommandMeaning, authorizationStillOpen, expiryEvents, sameGrantToken } from \"./authority-meaning.ts\";\n\nexport type { AuthorityRecord } from \"./authority-journal.ts\";\n\nimport type { NamespaceIdentity } from \"./namespace-identity.ts\";\nimport { readRunClock } from \"./recovery-clock.ts\";\nimport { VerifiedRunError } from \"./storage.ts\";\nimport { probeOwnedNamespace } from \"./supervisor-adapter.ts\";\n\n/**\n * Durable authority store (WP03): the single-writer boundary for supervisor\n * authorization state.\n *\n * Storage is a head-sidecar pair — `authority.events.jsonl` plus\n * `authority.events.jsonl.head` — mirroring `ReplayLedgerStore`: the committed\n * head is the transaction boundary, bytes after `head.size` are uncommitted\n * and get quarantined then truncated on the next open, and every append is an\n * expected-head CAS inside the `\"mutation\"` lock critical section. Cross-process\n * exclusion is the `\"authority\"` scope durable-file lease held for the store's\n * lifetime; its owner record (pid+host+token) is the writer identity, and dead\n * owners are reclaimed by the existing reclaim protocol.\n *\n * Authority epoch durability: `open` appends `authority-epoch-advanced` inside\n * the lease critical section — never the in-memory default \"1\" — and leaves the\n * epoch unreconciled. Only `reconcile()` (termination-witness probes per\n * quarantined effect, then `authority-reconciled`) reopens admission. A crash\n * between the epoch event and reconciliation leaves `pendingReconcile` set;\n * the next open adopts that epoch instead of advancing again.\n */\n\nexport { AuthorityLeaseHeldError, AuthorityStoreError } from \"./authority-errors.ts\";\n\nconst AUTHORITY_SCOPE = \"authority\";\n\nexport const authorityStorePath = (root: string): string => join(root, \"authority.events.jsonl\");\n\nexport interface AuthorityHead {\n\treadonly fileIdentity: { readonly dev: string; readonly ino: string } | null;\n\treadonly size: number;\n\treadonly lastSeq: number;\n\treadonly lastHash: string;\n}\n\nconst EMPTY_AUTHORITY_HEAD: AuthorityHead = Object.freeze({\n\tfileIdentity: null,\n\tsize: 0,\n\tlastSeq: 0,\n\tlastHash: GENESIS_PREVIOUS,\n});\n\ninterface CommittedView {\n\treadonly head: AuthorityHead;\n\treadonly parsed: ParsedAuthority;\n}\n\nfunction authorityHeadsEqual(left: AuthorityHead, right: AuthorityHead): boolean {\n\treturn (\n\t\tleft.size === right.size &&\n\t\tleft.lastSeq === right.lastSeq &&\n\t\tleft.lastHash === right.lastHash &&\n\t\t(left.fileIdentity === null\n\t\t\t? right.fileIdentity === null\n\t\t\t: right.fileIdentity !== null &&\n\t\t\t\tleft.fileIdentity.dev === right.fileIdentity.dev &&\n\t\t\t\tleft.fileIdentity.ino === right.fileIdentity.ino)\n\t);\n}\n\nfunction parseHead(raw: unknown): AuthorityHead {\n\tif (typeof raw !== \"object\" || raw === null || Array.isArray(raw)) throw new AuthorityStoreError(\"corrupt\");\n\tconst value = raw as Record<string, unknown>;\n\tif (Object.keys(value).sort().join(\",\") !== \"fileIdentity,lastHash,lastSeq,size\")\n\t\tthrow new AuthorityStoreError(\"corrupt\");\n\tlet fileIdentity: AuthorityHead[\"fileIdentity\"] = null;\n\tif (value.fileIdentity !== null) {\n\t\tif (typeof value.fileIdentity !== \"object\" || Array.isArray(value.fileIdentity))\n\t\t\tthrow new AuthorityStoreError(\"corrupt\");\n\t\tconst identity = value.fileIdentity as Record<string, unknown>;\n\t\tif (\n\t\t\tObject.keys(identity).sort().join(\",\") !== \"dev,ino\" ||\n\t\t\ttypeof identity.dev !== \"string\" ||\n\t\t\t!/^\\d+$/.test(identity.dev) ||\n\t\t\ttypeof identity.ino !== \"string\" ||\n\t\t\t!/^\\d+$/.test(identity.ino)\n\t\t)\n\t\t\tthrow new AuthorityStoreError(\"corrupt\");\n\t\tfileIdentity = Object.freeze({ dev: identity.dev, ino: identity.ino });\n\t}\n\tif (\n\t\t!Number.isSafeInteger(value.size) ||\n\t\t(value.size as number) < 0 ||\n\t\t!Number.isSafeInteger(value.lastSeq) ||\n\t\t(value.lastSeq as number) < 0 ||\n\t\ttypeof value.lastHash !== \"string\" ||\n\t\t(value.lastSeq === 0 ? value.lastHash !== GENESIS_PREVIOUS : !/^[0-9a-f]{64}$/.test(value.lastHash))\n\t)\n\t\tthrow new AuthorityStoreError(\"corrupt\");\n\treturn Object.freeze({\n\t\tfileIdentity,\n\t\tsize: value.size as number,\n\t\tlastSeq: value.lastSeq as number,\n\t\tlastHash: value.lastHash,\n\t});\n}\n\nfunction fileIdentityOf(stat: {\n\tdev: number | bigint;\n\tino: number | bigint;\n}): NonNullable<AuthorityHead[\"fileIdentity\"]> {\n\treturn Object.freeze({ dev: String(stat.dev), ino: String(stat.ino) });\n}\n\nfunction sameFileIdentity(left: AuthorityHead[\"fileIdentity\"], right: AuthorityHead[\"fileIdentity\"]): boolean {\n\treturn left === null ? right === null : right !== null && left.dev === right.dev && left.ino === right.ino;\n}\n\nfunction writeAll(fd: number, bytes: Uint8Array): void {\n\tlet offset = 0;\n\twhile (offset < bytes.byteLength) offset += writeSync(fd, bytes, offset, bytes.byteLength - offset);\n}\n\nfunction appendDurably(path: string, bytes: Uint8Array): void {\n\tconst fd = openSync(path, fsConstants.O_CREAT | fsConstants.O_APPEND | fsConstants.O_WRONLY, 0o600);\n\ttry {\n\t\twriteAll(fd, bytes);\n\t\tfsyncSync(fd);\n\t} finally {\n\t\tcloseSync(fd);\n\t}\n}\n\nexport interface AuthorityStoreHooks {\n\t/** Fires after the events file is fsynced but before the head publishes — the commit crash window. */\n\treadonly afterLedgerFsync?: () => void;\n\t/** Fires before a quarantine artifact is fsynced. */\n\treadonly beforeQuarantineFsync?: () => void;\n\t/** Fires after GC's atomic rewrite lands but before the new head publishes — the GC crash window. */\n\treadonly afterAtomicRewrite?: () => void;\n\t/** Test seam replacing the durable record append. */\n\treadonly persistRecord?: (path: string, bytes: Uint8Array) => void;\n}\n\nexport type AuthorityProbe = (grant: AuthorityGrantRecord) => \"terminated\" | \"alive\" | \"unknown\";\n\nexport interface OpenAuthorityStoreOptions {\n\t/** Total admissible weight of concurrently active grants (broker parity). */\n\treadonly capacity: number;\n\treadonly hooks?: AuthorityStoreHooks;\n\treadonly probe?: AuthorityProbe;\n\t/** Trusted host clock; never supplied by a serialized command. */\n\treadonly clock?: () => number;\n\t/** Disable only the parsing cache; disk integrity and durability checks always run. */\n\treadonly incrementalReplay?: boolean;\n}\n\nexport interface AuthorityAcquireInput {\n\treadonly sessionId: string;\n\treadonly incarnation: Sequence | string;\n\t/** Idempotency key; unique across grants and tombstones (I08/I31). */\n\treadonly commandId: string;\n\treadonly intentDigest: string;\n\treadonly claims: readonly ResourceClaimInput[];\n\treadonly now: number;\n\treadonly ttl: number;\n\treadonly weight?: number;\n}\n\nexport type AuthorityAcquireResult =\n\t| { readonly status: \"granted\"; readonly token: GrantToken; readonly duplicate: boolean }\n\t| { readonly status: \"blocked\" }\n\t| { readonly status: \"result\"; readonly resultDigest: string; readonly token: GrantToken }\n\t| { readonly status: \"result-expired\" };\n\nexport type AuthorityLookup =\n\t| { readonly status: \"pending\"; readonly token: GrantToken }\n\t| { readonly status: \"result\"; readonly resultDigest: string; readonly token: GrantToken }\n\t| { readonly status: \"result-expired\"; readonly token: GrantToken }\n\t| { readonly status: \"unknown\" };\n\n/** Committed projection plus the hashed records it was replayed from. */\nexport interface AuthorityJournalInspection {\n\treadonly state: AuthorityProjection;\n\treadonly records: readonly AuthorityRecord[];\n\treadonly head: AuthorityHead;\n}\n\ninterface StoreContext {\n\treadonly path: string;\n\treadonly headPath: string;\n\treadonly hooks: AuthorityStoreHooks;\n}\n\nfunction publishHead(context: StoreContext, head: AuthorityHead): void {\n\tconst temp = `${context.headPath}.${randomUUID()}.tmp`;\n\tconst fd = openSync(temp, fsConstants.O_CREAT | fsConstants.O_EXCL | fsConstants.O_WRONLY, 0o600);\n\ttry {\n\t\twriteAll(fd, Buffer.from(`${JSON.stringify(head)}\\n`, \"utf8\"));\n\t\tfsyncSync(fd);\n\t} finally {\n\t\tcloseSync(fd);\n\t}\n\ttry {\n\t\trenameSync(temp, context.headPath);\n\t\tfsyncDirectorySync(dirname(context.headPath));\n\t} catch (error) {\n\t\tif (existsSync(temp)) unlinkSync(temp);\n\t\tthrow error;\n\t}\n}\n\nfunction quarantineSuffix(context: StoreContext, bytes: Buffer): void {\n\tcontext.hooks.beforeQuarantineFsync?.();\n\twriteExclusiveFileDurablySync(`${context.path}.quarantine-${randomUUID()}`, bytes);\n}\n\n/**\n * Load committed state. With `repair`, an uncommitted suffix is quarantined\n * and truncated; a file whose identity no longer matches its head is accepted\n * only when it begins with exactly the GC snapshot continuation of that head\n * (the GC rename-landed/head-publish-crashed window), otherwise the store is\n * tampered and opens refuse. Without `repair` the suffix is left untouched\n * and simply not part of committed state.\n */\nfunction loadCommitted(context: StoreContext, repair: boolean, cached?: CommittedView): CommittedView {\n\tconst fileExists = existsSync(context.path);\n\tconst headExists = existsSync(context.headPath);\n\tconst empty: CommittedView = {\n\t\thead: EMPTY_AUTHORITY_HEAD,\n\t\tparsed: parseCommitted(Buffer.alloc(0)),\n\t};\n\tif (!fileExists && !headExists) return empty;\n\tif (fileExists && !headExists) {\n\t\tconst fd = openSync(context.path, fsConstants.O_RDONLY | (fsConstants.O_NOFOLLOW ?? 0));\n\t\ttry {\n\t\t\tconst stat = fstatSync(fd);\n\t\t\tif (!stat.isFile() || stat.size !== 0) throw new AuthorityStoreError(\"corrupt\");\n\t\t} finally {\n\t\t\tcloseSync(fd);\n\t\t}\n\t\tif (!repair) return empty;\n\t\tunlinkSync(context.path);\n\t\tfsyncDirectorySync(dirname(context.path));\n\t\treturn empty;\n\t}\n\tif (!fileExists) throw new AuthorityStoreError(\"corrupt\");\n\tlet head: AuthorityHead;\n\ttry {\n\t\thead = parseHead(JSON.parse(readFileSync(context.headPath, \"utf8\")));\n\t} catch (error) {\n\t\tif (error instanceof AuthorityStoreError || error instanceof VerifiedRunError) throw error;\n\t\tthrow new AuthorityStoreError(\"corrupt\");\n\t}\n\tconst fd = openSync(context.path, fsConstants.O_RDWR | (fsConstants.O_NOFOLLOW ?? 0));\n\ttry {\n\t\tconst before = fstatSync(fd);\n\t\tconst bytes = readFileSync(fd);\n\t\tconst after = fstatSync(fd);\n\t\tif (\n\t\t\t!before.isFile() ||\n\t\t\t!after.isFile() ||\n\t\t\tbefore.size !== after.size ||\n\t\t\tbefore.ctimeMs !== after.ctimeMs ||\n\t\t\tafter.size !== bytes.byteLength\n\t\t)\n\t\t\tthrow new AuthorityStoreError(\"tampered\");\n\t\tconst actual = fileIdentityOf(after);\n\t\tif (!sameFileIdentity(head.fileIdentity, actual)) {\n\t\t\thead = healGarbageCollect(context, head, bytes, actual, repair);\n\t\t}\n\t\tif (head.size > after.size) throw new AuthorityStoreError(\"tampered\");\n\t\tconst committed = Buffer.from(bytes.subarray(0, head.size));\n\t\tconst prefix =\n\t\t\tcached &&\n\t\t\tsameFileIdentity(cached.head.fileIdentity, actual) &&\n\t\t\thead.size >= cached.head.size &&\n\t\t\thead.lastSeq >= cached.head.lastSeq\n\t\t\t\t? cached.parsed\n\t\t\t\t: undefined;\n\t\tconst parsed = parseCommitted(committed, prefix);\n\t\tif (parsed.lastSeq !== head.lastSeq || parsed.lastHash !== head.lastHash)\n\t\t\tthrow new AuthorityStoreError(\"tampered\");\n\t\tif (head.size < after.size && repair) {\n\t\t\tquarantineSuffix(context, bytes.subarray(head.size));\n\t\t\tftruncateSync(fd, head.size);\n\t\t\tfsyncSync(fd);\n\t\t\tfsyncDirectorySync(dirname(context.path));\n\t\t}\n\t\treturn { head, parsed };\n\t} finally {\n\t\tcloseSync(fd);\n\t}\n}\n\n/**\n * The only legal file whose identity differs from its head: an atomic GC\n * rewrite that landed but whose head publish never committed. Its first\n * record must be exactly the snapshot continuing `head`; the healed head\n * covers just that record, and any bytes after it are uncommitted suffix.\n */\nfunction healGarbageCollect(\n\tcontext: StoreContext,\n\thead: AuthorityHead,\n\tbytes: Buffer,\n\tactual: NonNullable<AuthorityHead[\"fileIdentity\"]>,\n\trepair: boolean,\n): AuthorityHead {\n\tif (!repair) throw new AuthorityStoreError(\"tampered\");\n\tlet healed: AuthorityHead | null = null;\n\ttry {\n\t\tconst newline = bytes.indexOf(10);\n\t\tif (newline > 0) {\n\t\t\tconst first = bytes.subarray(0, newline + 1);\n\t\t\tconst raw: unknown = JSON.parse(new TextDecoder(\"utf-8\", { fatal: true }).decode(first).slice(0, -1));\n\t\t\tconst event =\n\t\t\t\ttypeof raw === \"object\" && raw !== null && \"event\" in raw\n\t\t\t\t\t? parseAuthorityEvent((raw as { event: unknown }).event)\n\t\t\t\t\t: null;\n\t\t\tif (event?.kind === \"authority-snapshot\") {\n\t\t\t\tconst record = materializeRecord(event.archivedThroughSequence + 1, event.continuesHash, event);\n\t\t\t\t// Only the snapshot line itself must be proven: any bytes after it\n\t\t\t\t// (including a torn tail) are uncommitted suffix and go to quarantine.\n\t\t\t\tif (\n\t\t\t\t\tcanonicalJson(raw) === canonicalJson(record) &&\n\t\t\t\t\tevent.archivedThroughSequence === head.lastSeq &&\n\t\t\t\t\tevent.continuesHash === head.lastHash\n\t\t\t\t) {\n\t\t\t\t\thealed = Object.freeze({\n\t\t\t\t\t\tfileIdentity: actual,\n\t\t\t\t\t\tsize: first.byteLength,\n\t\t\t\t\t\tlastSeq: record.seq,\n\t\t\t\t\t\tlastHash: record.hash,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t} catch (error) {\n\t\tif (!(error instanceof AuthorityStoreError || error instanceof VerifiedRunError)) throw error;\n\t}\n\tif (!healed) throw new AuthorityStoreError(\"tampered\");\n\tpublishHead(context, healed);\n\treturn healed;\n}\n\nfunction defaultProbe(grant: AuthorityGrantRecord): \"terminated\" | \"alive\" | \"unknown\" {\n\tif (!grant.identity) return \"unknown\";\n\treturn probeOwnedNamespace(grant.identity);\n}\n\nfunction nextSequenceValue(value: Sequence): Sequence {\n\treturn sequence(String(BigInt(value) + 1n));\n}\n\nfunction assertNonNegativeInteger(value: number, label: string): void {\n\tif (typeof value !== \"number\" || !Number.isSafeInteger(value) || value < 0)\n\t\tthrow new TypeError(`${label} must be a nonnegative safe integer`);\n}\n\n/** The durable single-writer authority boundary. */\nexport class AuthorityStore {\n\tprivate readonly context: StoreContext;\n\tprivate readonly capacity: number;\n\tprivate readonly probe: AuthorityProbe;\n\tprivate readonly lease: DurableFileLock;\n\tprivate accepted: CommittedView;\n\tprivate released = false;\n\tprivate readonly now: () => number;\n\tprivate readonly incrementalReplay: boolean;\n\n\tprivate constructor(\n\t\tcontext: StoreContext,\n\t\tlease: DurableFileLock,\n\t\taccepted: CommittedView,\n\t\toptions: OpenAuthorityStoreOptions,\n\t) {\n\t\tthis.context = context;\n\t\tthis.lease = lease;\n\t\tthis.accepted = accepted;\n\t\tthis.capacity = options.capacity;\n\t\tthis.probe = options.probe ?? defaultProbe;\n\t\tthis.now = createAuthorityClock(options.clock);\n\t\tthis.incrementalReplay = options.incrementalReplay !== false;\n\t}\n\n\t/**\n\t * Take the `\"authority\"` scope lease and advance the persisted epoch.\n\t * When the previous epoch never reconciled, this restart adopts it — the\n\t * pending reconciliation must finish before new admission, and the epoch\n\t * is not bumped again (review §5 fault row).\n\t */\n\tstatic open(path: string, options: OpenAuthorityStoreOptions): AuthorityStore {\n\t\tassertNonNegativeInteger(options.capacity, \"capacity\");\n\t\tmkdirSync(dirname(path), { recursive: true });\n\t\tconst context: StoreContext = { path, headPath: `${path}.head`, hooks: options.hooks ?? {} };\n\t\tconst mutation = acquireDurableFileMutationLockSync(path);\n\t\tlet lease: DurableFileLock;\n\t\ttry {\n\t\t\tlease = acquireDurableFileLockSync(path, AUTHORITY_SCOPE, { timeoutMs: 0 });\n\t\t} catch (error) {\n\t\t\tconst failure =\n\t\t\t\terror instanceof DurableFileLockBusyError\n\t\t\t\t\t? new AuthorityLeaseHeldError(inspectDurableFileLockSync(path, AUTHORITY_SCOPE))\n\t\t\t\t\t: error;\n\t\t\ttry {\n\t\t\t\tmutation.release();\n\t\t\t} catch (cleanupError) {\n\t\t\t\tthrow new AggregateError(\n\t\t\t\t\t[failure, cleanupError instanceof Error ? cleanupError : new Error(String(cleanupError))],\n\t\t\t\t\t\"Authority lease acquisition cleanup failed\",\n\t\t\t\t);\n\t\t\t}\n\t\t\tthrow failure;\n\t\t}\n\t\tlet store: AuthorityStore;\n\t\ttry {\n\t\t\tstore = new AuthorityStore(context, lease, loadCommitted(context, true), options);\n\t\t} catch (error) {\n\t\t\ttry {\n\t\t\t\tlease.release();\n\t\t\t} catch (releaseError) {\n\t\t\t\tthrow new AggregateError(\n\t\t\t\t\t[error, releaseError instanceof Error ? releaseError : new Error(String(releaseError))],\n\t\t\t\t\t\"Authority store open cleanup failed\",\n\t\t\t\t);\n\t\t\t}\n\t\t\tthrow error;\n\t\t} finally {\n\t\t\tmutation.release();\n\t\t}\n\t\ttry {\n\t\t\tif (store.state.epoch === null || !authorityPendingReconcile(store.state)) {\n\t\t\t\t// Fresh store or fully reconciled previous epoch: this restart\n\t\t\t\t// durably advances the epoch. A pending epoch is adopted, not bumped.\n\t\t\t\tstore.commitEpochAdvance();\n\t\t\t}\n\t\t} catch (error) {\n\t\t\ttry {\n\t\t\t\tstore.release();\n\t\t\t} catch (releaseError) {\n\t\t\t\tthrow new AggregateError(\n\t\t\t\t\t[error, releaseError instanceof Error ? releaseError : new Error(String(releaseError))],\n\t\t\t\t\t\"Authority store open cleanup failed\",\n\t\t\t\t);\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\t\treturn store;\n\t}\n\n\t/** Read-only committed projection; performs no repair and writes nothing. */\n\tstatic inspect(path: string): AuthorityProjection {\n\t\tconst context: StoreContext = { path, headPath: `${path}.head`, hooks: {} };\n\t\treturn loadCommitted(context, false).parsed.state;\n\t}\n\n\t/**\n\t * Read-only committed journal: projection plus the hashed records it was\n\t * replayed from, so callers can see transition causes (cancel-requested vs\n\t * authorization-expired vs epoch restart) and termination witnesses — the\n\t * projection alone intentionally does not restate them.\n\t */\n\tstatic inspectJournal(path: string): AuthorityJournalInspection {\n\t\tconst context: StoreContext = { path, headPath: `${path}.head`, hooks: {} };\n\t\tconst committed = loadCommitted(context, false);\n\t\treturn Object.freeze({\n\t\t\tstate: committed.parsed.state,\n\t\t\trecords: committed.parsed.records,\n\t\t\thead: committed.head,\n\t\t});\n\t}\n\n\tget path(): string {\n\t\treturn this.context.path;\n\t}\n\n\tget head(): AuthorityHead {\n\t\treturn this.accepted.head;\n\t}\n\n\tget state(): AuthorityProjection {\n\t\treturn structuredClone(this.accepted.parsed.state);\n\t}\n\n\tget lastReplay(): ParsedAuthority[\"replay\"] {\n\t\treturn this.accepted.parsed.replay;\n\t}\n\n\tget pendingReconcile(): boolean {\n\t\treturn authorityPendingReconcile(this.state);\n\t}\n\n\tprivate writerId(): string {\n\t\ttry {\n\t\t\treturn `${readRunClock().bootId}:${process.pid}`;\n\t\t} catch {\n\t\t\treturn `unknown:${process.pid}`;\n\t\t}\n\t}\n\n\t/**\n\t * Commit events as one transaction: mutation lock → committed-head CAS →\n\t * reducer pre-validation → append+fsync → head publish → re-read\n\t * verification. The head CAS is the fencing boundary — a store whose lease\n\t * was reclaimed and taken by another writer sees a changed head and fails\n\t * closed instead of authorizing. Persist failures never mint in-memory\n\t * state (docs/04: no memory-only authorization after storage failure).\n\t */\n\tprivate commit(events: readonly AuthorityEvent[]): void {\n\t\tif (this.released) throw new AuthorityStoreError(\"stale_owner\");\n\t\tconst mutation = acquireDurableFileMutationLockSync(this.context.path);\n\t\ttry {\n\t\t\tconst current = loadCommitted(this.context, true, this.incrementalReplay ? this.accepted : undefined);\n\t\t\tif (!authorityHeadsEqual(current.head, this.accepted.head)) throw new AuthorityStoreError(\"stale_head\");\n\t\t\tif (events.length === 0) {\n\t\t\t\tthis.accepted = current;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\t// Every event must be accepted by the deterministic reducer before a\n\t\t\t// single byte reaches disk.\n\t\t\tapplyAuthorityEvents(current.parsed.state, events);\n\t\t\tconst lines: string[] = [];\n\t\t\tlet seq = current.parsed.lastSeq;\n\t\t\tlet previous = current.parsed.lastHash;\n\t\t\tfor (const event of events) {\n\t\t\t\tseq += 1;\n\t\t\t\tconst record = materializeRecord(seq, previous, event);\n\t\t\t\tlines.push(recordLine(record));\n\t\t\t\tprevious = record.hash;\n\t\t\t}\n\t\t\tconst payload = Buffer.from(lines.join(\"\"), \"utf8\");\n\t\t\t(this.context.hooks.persistRecord ?? appendDurably)(this.context.path, payload);\n\t\t\tconst fd = openSync(this.context.path, fsConstants.O_RDWR | (fsConstants.O_NOFOLLOW ?? 0));\n\t\t\tlet head: AuthorityHead;\n\t\t\ttry {\n\t\t\t\tconst stat = fstatSync(fd);\n\t\t\t\t// CAS on the file itself: for an established store the inode must be\n\t\t\t\t// the one the committed head names; for genesis the append must have\n\t\t\t\t// produced exactly the expected size. Anything else is a replaced or\n\t\t\t\t// foreign file and commits must not bless it.\n\t\t\t\tif (\n\t\t\t\t\tstat.size !== current.head.size + payload.byteLength ||\n\t\t\t\t\t(current.head.fileIdentity !== null &&\n\t\t\t\t\t\t!sameFileIdentity(current.head.fileIdentity, fileIdentityOf(stat)))\n\t\t\t\t)\n\t\t\t\t\tthrow new AuthorityStoreError(\"stale_head\");\n\t\t\t\thead = Object.freeze({\n\t\t\t\t\tfileIdentity: fileIdentityOf(stat),\n\t\t\t\t\tsize: stat.size,\n\t\t\t\t\tlastSeq: seq,\n\t\t\t\t\tlastHash: previous,\n\t\t\t\t});\n\t\t\t} finally {\n\t\t\t\tcloseSync(fd);\n\t\t\t}\n\t\t\tthis.context.hooks.afterLedgerFsync?.();\n\t\t\tpublishHead(this.context, head);\n\t\t\tconst verified = loadCommitted(this.context, false, this.incrementalReplay ? current : undefined);\n\t\t\tif (verified.parsed.lastSeq !== head.lastSeq || verified.parsed.lastHash !== head.lastHash)\n\t\t\t\tthrow new AuthorityStoreError(\"corrupt\");\n\t\t\tthis.accepted = verified;\n\t\t} finally {\n\t\t\tmutation.release();\n\t\t}\n\t}\n\n\tprivate commitEpochAdvance(): void {\n\t\tconst state = this.state;\n\t\tconst epoch = nextSequenceValue(state.epoch ?? sequence(\"0\"));\n\t\tconst transitions = [...state.grants.values()]\n\t\t\t.filter((grant) => !SETTLED_EFFECT_STATES.has(grant.state))\n\t\t\t.map((grant) =>\n\t\t\t\tObject.freeze({\n\t\t\t\t\tgrantSequence: grant.token.grantSequence,\n\t\t\t\t\toutcome: grant.effectLive ? (\"quarantined\" as const) : (\"cancelled\" as const),\n\t\t\t\t}),\n\t\t\t);\n\t\tthis.commit([\n\t\t\t{ kind: \"authority-epoch-advanced\", authorityEpoch: epoch, writerId: this.writerId(), transitions },\n\t\t]);\n\t}\n\n\t/** Announce a session incarnation (S2); the counter survives restarts durably. */\n\tregister(sessionId: string): Sequence {\n\t\tif (typeof sessionId !== \"string\" || sessionId.length === 0)\n\t\t\tthrow new TypeError(\"sessionId must be a non-empty string\");\n\t\tconst current = this.state.incarnations.get(sessionId) ?? sequence(\"0\");\n\t\tconst incarnation = nextSequenceValue(current);\n\t\tthis.commit([{ kind: \"session-registered\", sessionId, incarnation }]);\n\t\treturn incarnation;\n\t}\n\n\t/**\n\t * Admit an authorization: claim-overlap check, capacity check, idempotency\n\t * replay, and the `grant-reserved` record in one lease/mutation-locked\n\t * critical section — the check and the record are one transaction (docs/04).\n\t * The lease is held for the store's lifetime, so committed state cannot\n\t * move between the check and the append; the head CAS in commit() is the\n\t * backstop if it ever could.\n\t */\n\tacquire(input: AuthorityAcquireInput): AuthorityAcquireResult {\n\t\tassertNonNegativeInteger(input.now, \"now\");\n\t\tconst weight = input.weight ?? 1;\n\t\tassertNonNegativeInteger(weight, \"weight\");\n\t\tif (!Number.isSafeInteger(input.ttl) || input.ttl <= 0)\n\t\t\tthrow new TypeError(\"ttl must be a positive safe integer\");\n\t\tconst authorizationDeadline = input.now + input.ttl;\n\t\tif (!Number.isSafeInteger(authorizationDeadline)) throw new RangeError(\"authorization deadline overflow\");\n\t\tconst incarnation = sequence(input.incarnation);\n\t\tif (this.state.incarnations.get(input.sessionId) !== incarnation)\n\t\t\tthrow new AuthorityStoreError(\"stale_incarnation\");\n\t\tif (!/^[a-f0-9]{64}$/.test(input.intentDigest)) throw new TypeError(\"intentDigest must be a sha256 hex digest\");\n\t\tif (!Array.isArray(input.claims) || input.claims.length === 0)\n\t\t\tthrow new TypeError(\"an unknown scope must not be admitted as an empty scope\");\n\t\tconst claims = Object.freeze(Array.from(input.claims, (claim) => canonicalClaim(claim)));\n\n\t\t// Idempotency (I08): replayed commands return their recorded disposition\n\t\t// without re-executing. A live tombstone returns the result; an expired\n\t\t// one reports expiry; an in-flight grant is a duplicate only when the\n\t\t// full intent matches — otherwise it is an ID collision (I09/I20).\n\t\tconst tombstone = this.state.tombstones.get(input.commandId);\n\t\tif (tombstone) {\n\t\t\tconst grant = this.state.grants.get(tombstone.grantSequence);\n\t\t\tif (!grant) throw new AuthorityStoreError(\"corrupt\");\n\t\t\tassertSameCommandMeaning(grant, {\n\t\t\t\tintentDigest: input.intentDigest,\n\t\t\t\tsessionId: input.sessionId,\n\t\t\t\tincarnation,\n\t\t\t\tclaims,\n\t\t\t\tweight,\n\t\t\t});\n\t\t\tif (tombstone.expiresAtMs <= input.now) return { status: \"result-expired\" };\n\t\t\treturn { status: \"result\", resultDigest: tombstone.resultDigest, token: grant.token };\n\t\t}\n\t\tfor (const grant of this.state.grants.values()) {\n\t\t\tif (grant.commandId !== input.commandId) continue;\n\t\t\tassertSameCommandMeaning(grant, {\n\t\t\t\tintentDigest: input.intentDigest,\n\t\t\t\tsessionId: input.sessionId,\n\t\t\t\tincarnation,\n\t\t\t\tclaims,\n\t\t\t\tweight,\n\t\t\t});\n\t\t\treturn { status: \"granted\", token: grant.token, duplicate: true };\n\t\t}\n\n\t\tif (this.pendingReconcile) throw new AuthorityStoreError(\"reconcile_pending\");\n\t\tconst epoch = this.state.epoch;\n\t\tif (epoch === null) throw new AuthorityStoreError(\"corrupt\");\n\n\t\tconst expired = expiryEvents(this.state, input.now, epoch);\n\t\tconst settledState = expired.length ? applyAuthorityEvents(this.state, expired) : this.state;\n\t\tconst active = [...settledState.grants.values()].filter((grant) => !SETTLED_EFFECT_STATES.has(grant.state));\n\t\tconst used = active.reduce((sum, grant) => sum + grant.weight, 0);\n\t\tif (weight > this.capacity - used) return { status: \"blocked\" };\n\t\tif (active.some((grant) => claimSetsConflict([...claims], [...grant.claims]))) return { status: \"blocked\" };\n\n\t\tconst grantSequence = nextSequenceValue(this.state.grantCounter);\n\t\tconst grant: AuthorityGrantRecord = Object.freeze({\n\t\t\ttoken: Object.freeze({\n\t\t\t\tauthorityEpoch: epoch,\n\t\t\t\tgrantSequence,\n\t\t\t\tsessionId: input.sessionId,\n\t\t\t\tsessionIncarnation: incarnation,\n\t\t\t\tauthorizationDeadline,\n\t\t\t}),\n\t\t\tcommandId: input.commandId,\n\t\t\tintentDigest: input.intentDigest,\n\t\t\tclaims,\n\t\t\tweight,\n\t\t\tstate: \"reserved\",\n\t\t\teffectLive: false,\n\t\t\tdispatchId: null,\n\t\t\tactualClaims: null,\n\t\t\tidentity: null,\n\t\t});\n\t\tthis.commit([...expired, { kind: \"grant-reserved\", grant }]);\n\t\treturn { status: \"granted\", token: grant.token, duplicate: false };\n\t}\n\n\tprivate resolveToken(token: GrantToken, authoritative: boolean): AuthorityGrantRecord | undefined {\n\t\tconst grant = this.state.grants.get(token.grantSequence);\n\t\tif (!grant || !sameGrantToken(grant.token, token)) return undefined;\n\t\tif (authoritative) {\n\t\t\tif (this.released) return undefined;\n\t\t\tif (\n\t\t\t\ttoken.authorityEpoch !== this.state.epoch ||\n\t\t\t\tthis.state.incarnations.get(token.sessionId) !== token.sessionIncarnation ||\n\t\t\t\tSETTLED_EFFECT_STATES.has(grant.state)\n\t\t\t)\n\t\t\t\treturn undefined;\n\t\t} else if (SETTLED_EFFECT_STATES.has(grant.state)) return undefined;\n\t\treturn grant;\n\t}\n\n\t/**\n\t * Persisted dispatch intent. A committed intent whose spawn was never\n\t * witnessed cannot be assumed un-run; on restart it quarantines like any\n\t * other possibly-live effect (I01/I02).\n\t */\n\tdispatchIntent(token: GrantToken, dispatchId: string, now = this.now()): boolean {\n\t\tif (!/^[A-Za-z0-9_-]{1,128}$/.test(dispatchId)) throw new TypeError(\"dispatchId is invalid\");\n\t\tconst grant = this.resolveToken(token, true);\n\t\tif (!grant || grant.state !== \"reserved\") return false;\n\t\tconst epoch = this.state.epoch;\n\t\tif (epoch === null) return false;\n\t\tif (!authorizationStillOpen(now, grant.token.authorizationDeadline)) {\n\t\t\tthis.expire(now);\n\t\t\treturn false;\n\t\t}\n\t\tthis.commit([\n\t\t\t{ kind: \"dispatch-intent\", grantSequence: grant.token.grantSequence, authorityEpoch: epoch, dispatchId },\n\t\t]);\n\t\treturn true;\n\t}\n\n\t/** Bind an effect to its reservation; actual claims must equal the reserved set exactly. */\n\teffectStarted(\n\t\ttoken: GrantToken,\n\t\tactualClaims: readonly ResourceClaimInput[],\n\t\tidentity?: NamespaceIdentity,\n\t\tnow = this.now(),\n\t): boolean {\n\t\tconst grant = this.resolveToken(token, true);\n\t\tif (!grant || (grant.state !== \"reserved\" && grant.state !== \"starting\")) return false;\n\t\tif (!authorizationStillOpen(now, grant.token.authorizationDeadline)) {\n\t\t\tthis.expire(now);\n\t\t\treturn false;\n\t\t}\n\t\tif (!Array.isArray(actualClaims)) throw new TypeError(\"actualClaims must be an array\");\n\t\tconst claims = Object.freeze(Array.from(actualClaims, (entry) => canonicalClaim(entry)));\n\t\tif (!sameClaimSet([...claims], [...grant.claims])) return false;\n\t\tconst epoch = this.state.epoch;\n\t\tif (epoch === null) return false;\n\t\tthis.commit([\n\t\t\t{\n\t\t\t\tkind: \"effect-started\",\n\t\t\t\tgrantSequence: grant.token.grantSequence,\n\t\t\t\tauthorityEpoch: epoch,\n\t\t\t\tactualClaims: claims,\n\t\t\t\t...(identity ? { identity } : {}),\n\t\t\t},\n\t\t]);\n\t\treturn true;\n\t}\n\n\t/** Revalidate a running effect before its next irreversible boundary, without starting it again. */\n\teffectAuthorized(token: GrantToken, actualClaims: readonly ResourceClaimInput[]): boolean {\n\t\tif (this.released) return false;\n\t\tthis.commit([]);\n\t\tconst now = this.now();\n\t\tconst grant = this.resolveToken(token, true);\n\t\tif (!grant || grant.state !== \"running\") return false;\n\t\tif (!authorizationStillOpen(now, token.authorizationDeadline)) {\n\t\t\tthis.expire(now);\n\t\t\treturn false;\n\t\t}\n\t\treturn sameClaimSet(\n\t\t\tArray.from(actualClaims, (claim) => canonicalClaim(claim)),\n\t\t\tgrant.claims,\n\t\t);\n\t}\n\n\t/** Requested cancellation is not observed termination; live effects quarantine. */\n\tcancel(token: GrantToken): boolean {\n\t\tconst grant = this.resolveToken(token, false);\n\t\tif (!grant) return false;\n\t\tconst epoch = this.state.epoch;\n\t\tif (epoch === null) return false;\n\t\tthis.commit([\n\t\t\t{\n\t\t\t\tkind: \"cancel-requested\",\n\t\t\t\tgrantSequence: grant.token.grantSequence,\n\t\t\t\tauthorityEpoch: epoch,\n\t\t\t\toutcome: grant.effectLive ? \"quarantined\" : \"cancelled\",\n\t\t\t},\n\t\t]);\n\t\treturn true;\n\t}\n\n\t/** Settle lapsed authorizations; never evicts a live effect. */\n\texpire(now: number): void {\n\t\tassertNonNegativeInteger(now, \"now\");\n\t\tconst epoch = this.state.epoch;\n\t\tif (epoch === null) return;\n\t\tconst events = expiryEvents(this.state, now, epoch);\n\t\tif (events.length) this.commit(events);\n\t}\n\n\t/**\n\t * Trusted supervisor witness: this exact effect is gone. Accepted across\n\t * superseded epochs so a restarted authority can settle what the previous\n\t * one started, never to authorize new work.\n\t */\n\tconfirmTerminated(token: GrantToken): boolean {\n\t\tconst grant = this.resolveToken(token, false);\n\t\tif (!grant) return false;\n\t\tconst epoch = this.state.epoch;\n\t\tif (epoch === null) return false;\n\t\tthis.commit([{ kind: \"termination-observed\", grantSequence: grant.token.grantSequence, authorityEpoch: epoch }]);\n\t\treturn true;\n\t}\n\n\t/** Retain the final result as an idempotency tombstone (S3, docs/04). */\n\tretainResult(commandId: string, resultDigest: string, now: number, retentionMs: number): void {\n\t\tassertNonNegativeInteger(now, \"now\");\n\t\tif (!Number.isSafeInteger(retentionMs) || retentionMs <= 0)\n\t\t\tthrow new TypeError(\"retentionMs must be a positive safe integer\");\n\t\tif (!/^[a-f0-9]{64}$/.test(resultDigest)) throw new TypeError(\"resultDigest must be a sha256 hex digest\");\n\t\tconst grant = [...this.state.grants.values()].find((entry) => entry.commandId === commandId);\n\t\tif (!grant) throw new AuthorityStoreError(\"unknown_command\");\n\t\tif (!SETTLED_EFFECT_STATES.has(grant.state)) throw new AuthorityStoreError(\"unsettled\");\n\t\tif (this.state.tombstones.has(commandId)) throw new AuthorityStoreError(\"duplicate\");\n\t\tthis.commit([\n\t\t\t{\n\t\t\t\tkind: \"result-retained\",\n\t\t\t\tcommandId,\n\t\t\t\tgrantSequence: grant.token.grantSequence,\n\t\t\t\tresultDigest,\n\t\t\t\tretainedAtMs: now,\n\t\t\t\texpiresAtMs: now + retentionMs,\n\t\t\t},\n\t\t]);\n\t}\n\n\t/** Command disposition without re-execution (I08/I31). */\n\tlookup(commandId: string, now: number): AuthorityLookup {\n\t\tassertNonNegativeInteger(now, \"now\");\n\t\tconst tombstone = this.state.tombstones.get(commandId);\n\t\tif (tombstone) {\n\t\t\tconst grant = this.state.grants.get(tombstone.grantSequence);\n\t\t\tif (!grant) throw new AuthorityStoreError(\"corrupt\");\n\t\t\treturn tombstone.expiresAtMs <= now\n\t\t\t\t? { status: \"result-expired\", token: grant.token }\n\t\t\t\t: { status: \"result\", resultDigest: tombstone.resultDigest, token: grant.token };\n\t\t}\n\t\tfor (const grant of this.state.grants.values())\n\t\t\tif (grant.commandId === commandId) return { status: \"pending\", token: grant.token };\n\t\treturn { status: \"unknown\" };\n\t}\n\n\t/**\n\t * Finish the restart reconciliation: every quarantined effect is compared\n\t * against the real execution boundary — only an observed termination\n\t * settles it (\"gone\"); \"alive\"/\"unknown\" stay quarantined and keep their\n\t * claims (docs/04, review §6). Ends with `authority-reconciled` for the\n\t * current epoch; the reconciled scope only then admits new work.\n\t */\n\treconcile(probe?: AuthorityProbe): void {\n\t\tconst state = this.state;\n\t\tconst epoch = state.epoch;\n\t\tif (epoch === null) throw new AuthorityStoreError(\"corrupt\");\n\t\tif (!authorityPendingReconcile(state)) return;\n\t\tconst check = probe ?? this.probe;\n\t\tconst events: AuthorityEvent[] = [];\n\t\tfor (const grant of state.grants.values()) {\n\t\t\tif (grant.state !== \"quarantined\") continue;\n\t\t\tif (check(grant) === \"terminated\") {\n\t\t\t\tevents.push({\n\t\t\t\t\tkind: \"termination-observed\",\n\t\t\t\t\tgrantSequence: grant.token.grantSequence,\n\t\t\t\t\tauthorityEpoch: epoch,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\tevents.push({ kind: \"authority-reconciled\", authorityEpoch: epoch });\n\t\tthis.commit(events);\n\t}\n\n\t/**\n\t * GC (review §7): fold droppable state into an `authority-snapshot` and\n\t * atomically rewrite the log to that snapshot. Retained: the epoch pair,\n\t * incarnation counters, the grant counter (I31 — sequences never reissue),\n\t * every unsettled grant, every settled grant still referenced by an\n\t * unexpired tombstone, and the unexpired tombstones themselves. A crash\n\t * between the atomic rewrite and head publish self-heals on next open via\n\t * the snapshot continuation check.\n\t */\n\tcompact(now: number): { readonly dropped: number } {\n\t\tassertNonNegativeInteger(now, \"now\");\n\t\tif (this.released) throw new AuthorityStoreError(\"stale_owner\");\n\t\tconst mutation = acquireDurableFileMutationLockSync(this.context.path);\n\t\ttry {\n\t\t\tconst current = loadCommitted(this.context, true);\n\t\t\tif (!authorityHeadsEqual(current.head, this.accepted.head)) throw new AuthorityStoreError(\"stale_head\");\n\t\t\tconst state = current.parsed.state;\n\t\t\tif (state.epoch === null) return { dropped: 0 };\n\t\t\tconst keepGrant = new Set<Sequence>();\n\t\t\tconst keepTombstone = new Set<string>();\n\t\t\tfor (const tombstone of state.tombstones.values()) {\n\t\t\t\tif (tombstone.expiresAtMs > now) {\n\t\t\t\t\tkeepTombstone.add(tombstone.commandId);\n\t\t\t\t\tkeepGrant.add(tombstone.grantSequence);\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst grants = [...state.grants.values()].filter(\n\t\t\t\t(grant) => !SETTLED_EFFECT_STATES.has(grant.state) || keepGrant.has(grant.token.grantSequence),\n\t\t\t);\n\t\t\tconst tombstones = [...state.tombstones.values()].filter((stone) => keepTombstone.has(stone.commandId));\n\t\t\tconst dropped = state.grants.size - grants.length + (state.tombstones.size - tombstones.length);\n\t\t\tif (dropped === 0) return { dropped: 0 };\n\t\t\tconst snapshot = snapshotFromProjection({\n\t\t\t\t...state,\n\t\t\t\tgrants: new Map(grants.map((grant) => [grant.token.grantSequence, grant])),\n\t\t\t\ttombstones: new Map(tombstones.map((stone) => [stone.commandId, stone])),\n\t\t\t});\n\t\t\tconst record = materializeRecord(current.parsed.lastSeq + 1, current.parsed.lastHash, {\n\t\t\t\tkind: \"authority-snapshot\",\n\t\t\t\tarchivedThroughSequence: current.parsed.lastSeq,\n\t\t\t\tcontinuesHash: current.parsed.lastHash,\n\t\t\t\tstate: snapshot,\n\t\t\t});\n\t\t\tatomicRewriteFileSync(this.context.path, recordLine(record));\n\t\t\tthis.context.hooks.afterAtomicRewrite?.();\n\t\t\tconst fd = openSync(this.context.path, fsConstants.O_RDONLY | (fsConstants.O_NOFOLLOW ?? 0));\n\t\t\tlet head: AuthorityHead;\n\t\t\ttry {\n\t\t\t\tconst stat = fstatSync(fd);\n\t\t\t\thead = Object.freeze({\n\t\t\t\t\tfileIdentity: fileIdentityOf(stat),\n\t\t\t\t\tsize: stat.size,\n\t\t\t\t\tlastSeq: record.seq,\n\t\t\t\t\tlastHash: record.hash,\n\t\t\t\t});\n\t\t\t} finally {\n\t\t\t\tcloseSync(fd);\n\t\t\t}\n\t\t\tpublishHead(this.context, head);\n\t\t\tthis.accepted = loadCommitted(this.context, false);\n\t\t\treturn { dropped };\n\t\t} finally {\n\t\t\tmutation.release();\n\t\t}\n\t}\n\n\t/** Release the authority lease; further commits fail closed. */\n\trelease(): void {\n\t\tif (this.released) return;\n\t\tthis.released = true;\n\t\tthis.lease.release();\n\t}\n}\n"]}