@humwick/server 0.130.2 → 0.130.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (570) hide show
  1. package/bundled/prompts/manifest.json +1 -0
  2. package/bundled/prompts/task.hydration.md +1 -1
  3. package/bundled/roles/builder.md +1 -1
  4. package/docs/guides/sub-coordinators.md +12 -5
  5. package/docs/guides/tools-coordination.md +1 -0
  6. package/docs/guides/workflows.md +16 -0
  7. package/docs/openapi.json +73 -4
  8. package/package.json +8 -7
  9. package/public/assets/{MessageBubble-BWLqMGKG.js → MessageBubble-Bq9kr21N.js} +2 -2
  10. package/public/assets/{PlanGraphPanel-3TlIQpjk.js → PlanGraphPanel-Dqiroy06.js} +2 -2
  11. package/public/assets/{TraceView-Wimm0Czv.js → TraceView-DYU962-H.js} +2 -2
  12. package/public/assets/{VaultDecisionCard-Bau_vNax.js → VaultDecisionCard-DhPfzioE.js} +2 -2
  13. package/public/assets/{activity-CrSurn-r.js → activity-DzCcQTee.js} +2 -2
  14. package/public/assets/{agent-lineage-CknAJKcb.js → agent-lineage-SgDt9i3T.js} +2 -2
  15. package/public/assets/agent-liveness-Bq-s73jW.js +2 -0
  16. package/public/assets/{agent-name-DKbiIhBo.js → agent-name-CcY3Aijy.js} +2 -2
  17. package/public/assets/{agent-profiles-DwF6EQ4R.js → agent-profiles-cxtRBn6O.js} +2 -2
  18. package/public/assets/{agent-project-badge-Diz4Sjjb.js → agent-project-badge-DGYbIhaI.js} +2 -2
  19. package/public/assets/{agent-tools-CRhpRQza.js → agent-tools-DHbftV0N.js} +2 -2
  20. package/public/assets/{agent-type-icon-LrUEU3lB.js → agent-type-icon-CY113sp-.js} +2 -2
  21. package/public/assets/{agents-B-v66C-4.js → agents-BPzY7IlQ.js} +2 -2
  22. package/public/assets/{analytics-BqCb-LKi.js → analytics-DENFPv_a.js} +2 -2
  23. package/public/assets/{artifact-viewer-kf4pGuAR.js → artifact-viewer-KRvSc7sl.js} +2 -2
  24. package/public/assets/{artifacts-DJevXcvg.js → artifacts-C9gSmL3s.js} +2 -2
  25. package/public/assets/{artifacts-D32zyw4Y.js → artifacts-DT-HkI5O.js} +2 -2
  26. package/public/assets/{automation-BpRajx_5.js → automation-BsmjQhKD.js} +2 -2
  27. package/public/assets/{backlog-D_UjTdzX.js → backlog-8NYH_62t.js} +2 -2
  28. package/public/assets/{busy-health-N3VEh4ao.js → busy-health-D3hwvYb4.js} +2 -2
  29. package/public/assets/{channels-9jopungq.js → channels-DCJ5WL7O.js} +2 -2
  30. package/public/assets/{chat-CSXrTB3C.js → chat-DAIl357U.js} +3 -3
  31. package/public/assets/{code-preview-p1NnmqK0.js → code-preview-CzrZfr9U.js} +2 -2
  32. package/public/assets/{connectors-CZdqlDNL.js → connectors-xTxtlyou.js} +2 -2
  33. package/public/assets/{cosmos-BgdsE4dG.js → cosmos-BpebFLK2.js} +2 -2
  34. package/public/assets/{crate-agent-30aXTKva.js → crate-agent-BHvI5wXC.js} +2 -2
  35. package/public/assets/{dependency-graph-B_jvw2GE.js → dependency-graph-DGT0o3wJ.js} +2 -2
  36. package/public/assets/{deploys-DoN6bAb5.js → deploys-hqWOHSHE.js} +2 -2
  37. package/public/assets/{display-BrKWFGJz.js → display-CDeH4I8T.js} +2 -2
  38. package/public/assets/{files-D6XEu8Ng.js → files-BTeGVmTg.js} +2 -2
  39. package/public/assets/{fleet-C7jQWbw2.js → fleet-DgXtNgCa.js} +2 -2
  40. package/public/assets/{fleet-metrics-BiDs3MLx.js → fleet-metrics-BHEt5yc_.js} +2 -2
  41. package/public/assets/{formatted-body-CQBwc1fb.js → formatted-body-MLtodyPp.js} +3 -3
  42. package/public/assets/{formatted-body-impl-DyG0WsQS.js → formatted-body-impl-CAe664Z8.js} +2 -2
  43. package/public/assets/{host-providers-dialog-DKaW31dN.js → host-providers-dialog-mapF4Fj9.js} +2 -2
  44. package/public/assets/{index-CRHep3M9.js → index-CqqgOI5R.js} +5 -5
  45. package/public/assets/{insights-CzrtkpOE.js → insights-DNyf7IzF.js} +2 -2
  46. package/public/assets/{integrations-aVsUmigt.js → integrations-DlYOHq7T.js} +2 -2
  47. package/public/assets/{issue-ref-chip-BnSlM_95.js → issue-ref-chip-CeWBNWn7.js} +2 -2
  48. package/public/assets/{log-viewer-qe7Ee47Z.js → log-viewer-Cib8nqdE.js} +2 -2
  49. package/public/assets/{maintenance-NfjqFZAk.js → maintenance-DSn1cPm7.js} +2 -2
  50. package/public/assets/{managed-agents-BiH0HgaE.js → managed-agents-C3CkM81m.js} +2 -2
  51. package/public/assets/{markdown-preview-BixaT3Xr.js → markdown-preview-W4VQQPAv.js} +3 -3
  52. package/public/assets/{markdown-preview-impl-DNGlWIS3.js → markdown-preview-impl-CMsTdhjq.js} +2 -2
  53. package/public/assets/{memory-DNsCl6UX.js → memory-BV17qtO4.js} +2 -2
  54. package/public/assets/{messages-DopU1L1X.js → messages-Bm_1AqSA.js} +2 -2
  55. package/public/assets/{orchestrators-Cq7ckLQT.js → orchestrators-Dg-vfP09.js} +2 -2
  56. package/public/assets/{overview-C1DCTzzV.js → overview-DcYCrvlr.js} +2 -2
  57. package/public/assets/{pairs-DE_m6rrE.js → pairs-8o9nuG3V.js} +2 -2
  58. package/public/assets/{profile-icon-B3rfh77F.js → profile-icon-BsRyBMea.js} +2 -2
  59. package/public/assets/{project-profile-instructions-DQWZE0kA.js → project-profile-instructions-DETKFn_E.js} +2 -2
  60. package/public/assets/{projects-Br2Bd2h1.js → projects-M_m6AHws.js} +2 -2
  61. package/public/assets/{prompt-settings-BKvFsvGD.js → prompt-settings-C_mNCgeL.js} +2 -2
  62. package/public/assets/provider-display-BEeLXC_D.js +2 -0
  63. package/public/assets/{registry-Cw38_6gW.js → registry-DDHHHE5N.js} +2 -2
  64. package/public/assets/run-card-Dt4I4jaP.js +2 -0
  65. package/public/assets/{secrets-DxI-vzSq.js → secrets-BkVwxI5O.js} +2 -2
  66. package/public/assets/{service-access-settings-D9s_VJbd.js → service-access-settings-Crq-6a-B.js} +2 -2
  67. package/public/assets/{settings-CKZTVGIx.js → settings-CYcavMFu.js} +2 -2
  68. package/public/assets/shipping-H6gzEeZB.js +2 -0
  69. package/public/assets/{stats-DyUBKyWI.js → stats-BJVeWkpV.js} +2 -2
  70. package/public/assets/{status-dot-DYqlEUhJ.js → status-dot-BcHyw1Vw.js} +2 -2
  71. package/public/assets/store-DKPDIZBe.js +9 -0
  72. package/public/assets/{tasks-Bh7Chr4-.js → tasks-qJjlQPpx.js} +2 -2
  73. package/public/assets/{teams-bowI7qVY.js → teams-Cd5Sk1uX.js} +2 -2
  74. package/public/assets/{terminal-viewer-BgVLHdsU.js → terminal-viewer-CpY5_m7W.js} +3 -3
  75. package/public/assets/{terminal-viewer-impl-Dz22UPy6.js → terminal-viewer-impl-DI_2llFR.js} +2 -2
  76. package/public/assets/{use-agent-terminal-DSQQ-iY7.js → use-agent-terminal-0yJA_buM.js} +2 -2
  77. package/public/assets/{use-decisions-samMQhQf.js → use-decisions-uJXHf34W.js} +2 -2
  78. package/public/assets/{use-project-list-59LheVP7.js → use-project-list-CpWl6eHQ.js} +2 -2
  79. package/public/assets/{use-selectors-BZa25xEB.js → use-selectors-BdxBW8mL.js} +2 -2
  80. package/public/assets/{user-avatar-DHxlvUvV.js → user-avatar-3IndfpOc.js} +2 -2
  81. package/public/assets/{versioned-prompt-editor-BmED5DnJ.js → versioned-prompt-editor-CIVg7XB1.js} +2 -2
  82. package/public/assets/{work-queue-CSvpuNLz.js → work-queue-B9rnLwHZ.js} +2 -2
  83. package/public/assets/{workflow-marker-DAZcj5qt.js → workflow-marker-X8wxyhzC.js} +2 -2
  84. package/public/assets/{workflows-DdmxRSyg.js → workflows-o5YyMFW7.js} +2 -2
  85. package/public/assets/{workspaces-BxeKfVuF.js → workspaces-CzEGoAng.js} +2 -2
  86. package/public/cosmos.html +4 -4
  87. package/public/index.html +28 -28
  88. package/runner/plugins/claude/.claude-plugin/plugin.json +1 -1
  89. package/runner/src/adapters/claude-headless.ts +1 -1
  90. package/runner/src/adapters/claude-launch-exit.ts +26 -0
  91. package/runner/src/adapters/claude-session-probe.ts +1 -1
  92. package/runner/src/adapters/claude-tmux.ts +38 -10
  93. package/runner/src/adapters/claude.ts +28 -75
  94. package/runner/src/adapters/provider-quota-harvest.ts +20 -71
  95. package/runner/src/agent-runner/lifecycle-milestones.ts +2 -2
  96. package/runner/src/agent-runner/liveness-exchange.ts +104 -0
  97. package/runner/src/claude-system-prompt.ts +49 -0
  98. package/runner/src/launch-assembly.ts +14 -10
  99. package/runner/src/profile-home.ts +1 -1
  100. package/runner/src/project-instructions.ts +2 -2
  101. package/runner/src/provider-command.ts +18 -0
  102. package/runner/src/provider-setup.ts +2 -1
  103. package/runner/src/runner-core.ts +14 -15
  104. package/runner/src/runner-helpers.ts +0 -8
  105. package/runner/src/session-scratch.ts +6 -2
  106. package/runner/test-support/provisioning-context.ts +4 -2
  107. package/runner/test-support/runner-harness.ts +2 -1
  108. package/scripts/steward-policy-cleanup.ts +2 -2
  109. package/src/agent-authz.ts +2 -2
  110. package/src/agent-drive-arbiter.ts +3 -8
  111. package/src/agent-exit-cleanup-async.ts +2 -2
  112. package/src/agent-exit-cleanup.ts +23 -19
  113. package/src/agent-lifecycle-events.ts +112 -85
  114. package/src/agent-ref.ts +3 -2
  115. package/src/agent-status-events.ts +43 -14
  116. package/src/auth/password.ts +4 -3
  117. package/src/auth/session-refresh.ts +1 -1
  118. package/src/auth/session.test-fixture.ts +2 -2
  119. package/src/auth/session.ts +7 -7
  120. package/src/automations/crud.ts +8 -81
  121. package/src/automations/dispatch.ts +33 -58
  122. package/src/automations/index.ts +1 -1
  123. package/src/automations/persistence.ts +65 -0
  124. package/src/automations/precheck.ts +18 -16
  125. package/src/automations/read-async.ts +11 -0
  126. package/src/automations/reconcile.ts +30 -39
  127. package/src/automations/storage.ts +33 -10
  128. package/src/automations/write-async.ts +33 -0
  129. package/src/branch-land-notice.ts +3 -3
  130. package/src/branch-landed.ts +25 -23
  131. package/src/bus.ts +21 -19
  132. package/src/cd-deploy-notify.ts +2 -2
  133. package/src/ci/notifications.ts +4 -4
  134. package/src/ci/root-missing.ts +3 -3
  135. package/src/ci/transition-notify.ts +5 -5
  136. package/src/claude-app/oauth-http.ts +3 -3
  137. package/src/cli/workflow.ts +12 -3
  138. package/src/commands-db.ts +4 -4
  139. package/src/commands-write.ts +8 -0
  140. package/src/compaction-watch.ts +10 -8
  141. package/src/config-store-write.ts +2 -2
  142. package/src/config.ts +4 -11
  143. package/src/connectors.ts +10 -8
  144. package/src/context-advisory.ts +22 -21
  145. package/src/db/agent-action-write.ts +36 -0
  146. package/src/db/agent-control-async.ts +9 -0
  147. package/src/db/agent-control-write.ts +49 -0
  148. package/src/db/agent-effort-observe.ts +22 -0
  149. package/src/db/agent-meta.ts +14 -3
  150. package/src/db/agents-async.ts +8 -5
  151. package/src/db/agents.ts +15 -12
  152. package/src/db/auth-identities.ts +2 -2
  153. package/src/db/automation-read-ops.ts +11 -0
  154. package/src/db/automation-write-ops.ts +46 -0
  155. package/src/db/blackboard.ts +2 -2
  156. package/src/db/browser-session-write.ts +32 -0
  157. package/src/db/bus-outbox-write-ops.ts +3 -1
  158. package/src/db/checkout-refresh-schema.ts +47 -0
  159. package/src/db/checkout-refresh.ts +5 -5
  160. package/src/db/command-agent-profile-backfill.ts +39 -0
  161. package/src/db/command-agent-profiles.ts +5 -1
  162. package/src/db/command-retention.ts +126 -0
  163. package/src/db/commands-planner-stats.test-fixture.ts +8 -0
  164. package/src/db/commands-read-ops.ts +16 -0
  165. package/src/db/connection.ts +1 -182
  166. package/src/db/context-carry-write.ts +74 -0
  167. package/src/db/continuations.ts +4 -4
  168. package/src/db/db-error-registry.ts +12 -0
  169. package/src/db/gateway.ts +26 -133
  170. package/src/db/history-retention-ops.ts +13 -0
  171. package/src/db/inbox-async.ts +2 -2
  172. package/src/db/inbox-read-ops.ts +1 -1
  173. package/src/db/inbox.ts +9 -3
  174. package/src/db/issue-associations.ts +2 -2
  175. package/src/db/issue-refs-async.ts +4 -1
  176. package/src/db/issue-refs-read-ops.ts +6 -0
  177. package/src/db/issue-refs.ts +8 -8
  178. package/src/db/kernel-event-log-schema.ts +2 -0
  179. package/src/db/land-boundaries.ts +51 -5
  180. package/src/db/land-gate-attestations.ts +2 -2
  181. package/src/db/landing-receipts.ts +2 -2
  182. package/src/db/lifecycle-context-write.ts +5 -0
  183. package/src/db/lifecycle-tail-async.ts +76 -0
  184. package/src/db/lifecycle-tail-write-ops.ts +62 -0
  185. package/src/db/lifecycle-write-window.ts +2 -1
  186. package/src/db/lineage-pair-dedupe-sql.ts +3 -3
  187. package/src/db/main-connection-mode.ts +11 -0
  188. package/src/db/main-connection-writes.ts +20 -2
  189. package/src/db/maintenance-job-runs-async.ts +2 -2
  190. package/src/db/maintenance-job-write-ops.ts +1 -1
  191. package/src/db/maintenance-pauses.ts +2 -2
  192. package/src/db/message-send.ts +4 -0
  193. package/src/db/messages.ts +31 -25
  194. package/src/db/migrations.ts +18 -1
  195. package/src/db/notification-write-ops.ts +19 -0
  196. package/src/db/operator-answer-write.ts +19 -0
  197. package/src/db/orchestrators.ts +10 -4
  198. package/src/db/pairs-async.ts +22 -1
  199. package/src/db/pairs-write-ops.ts +8 -3
  200. package/src/db/pairs.ts +41 -48
  201. package/src/db/plan-dispatch-schema.ts +5 -1
  202. package/src/db/plan-ready-notices.ts +43 -0
  203. package/src/db/plan-tools-include-migration.ts +36 -0
  204. package/src/db/plans-write-ops.ts +2 -0
  205. package/src/db/presence-buffer.ts +5 -0
  206. package/src/db/presence-loop.ts +18 -23
  207. package/src/db/presence-write.ts +0 -6
  208. package/src/db/project-entries-async.ts +14 -0
  209. package/src/db/project-entries-read-ops.ts +5 -1
  210. package/src/db/project-entries-write-ops.ts +8 -0
  211. package/src/db/project-entries.ts +15 -12
  212. package/src/db/project-journal.ts +2 -2
  213. package/src/db/project-trust-approvals.ts +2 -2
  214. package/src/db/prompt-templates.ts +4 -4
  215. package/src/db/read-op-marshaling-fixtures.ts +18 -0
  216. package/src/db/read-ops.ts +4 -0
  217. package/src/db/retention.ts +13 -2
  218. package/src/db/review-publish-write.ts +8 -0
  219. package/src/db/runtime-admin-async.ts +19 -0
  220. package/src/db/runtime-admin-write-ops.ts +36 -0
  221. package/src/db/runtime-tail-async.ts +288 -0
  222. package/src/db/runtime-tail-write-ops.ts +97 -0
  223. package/src/db/schema.ts +4 -1
  224. package/src/db/spawn-budget.ts +5 -0
  225. package/src/db/spawn-delegation.ts +1 -11
  226. package/src/db/sql-audit-indexes.ts +3 -0
  227. package/src/db/stats-async.ts +5 -3
  228. package/src/db/task-entity.ts +5 -4
  229. package/src/db/team-claim-policy.ts +60 -0
  230. package/src/db/teams-async.ts +4 -0
  231. package/src/db/teams-write-ops.ts +4 -0
  232. package/src/db/terminal-events.ts +15 -0
  233. package/src/db/timer-cutover-bodies.ts +292 -0
  234. package/src/db/timer-cutover-core.ts +37 -278
  235. package/src/db/timer-cutover-state.ts +40 -0
  236. package/src/db/timer-cutover-write-ops.ts +1 -40
  237. package/src/db/token-db-write-ops.ts +2 -0
  238. package/src/db/token-standing-async.ts +12 -0
  239. package/src/db/token-standing.ts +6 -9
  240. package/src/db/token-usage-retention.ts +33 -0
  241. package/src/db/token-usage-write-ops.ts +1 -1
  242. package/src/db/token-usage.ts +4 -1
  243. package/src/db/user-invites-async.ts +13 -0
  244. package/src/db/user-invites.ts +3 -3
  245. package/src/db/users.ts +14 -6
  246. package/src/db/workspaces.ts +8 -5
  247. package/src/db/write-ops.ts +31 -12
  248. package/src/db/writer-fault-seam.ts +2 -9
  249. package/src/db/writer-fault-test-ops.ts +18 -0
  250. package/src/db/writer-gateway.ts +42 -46
  251. package/src/dev.ts +8 -3
  252. package/src/event-loop-lag.ts +4 -2
  253. package/src/event-loop-yield.ts +2 -2
  254. package/src/events.ts +10 -8
  255. package/src/host-provider-store.ts +5 -4
  256. package/src/hub-metrics-report.ts +5 -1
  257. package/src/hub-metrics.ts +38 -18
  258. package/src/injection-sweep-pacing.ts +6 -8
  259. package/src/land-gate-notifications.ts +11 -10
  260. package/src/land-gate-queue-progress.ts +3 -3
  261. package/src/land-settle-dispatch.ts +52 -92
  262. package/src/lifecycle-context-pressure.ts +11 -10
  263. package/src/lifecycle-manager.ts +104 -98
  264. package/src/maintenance/breaker.ts +18 -16
  265. package/src/maintenance/checkout-refresh.ts +18 -1
  266. package/src/maintenance/critical-job-watchdog.ts +3 -3
  267. package/src/maintenance/events.ts +3 -3
  268. package/src/maintenance/history-retention-job.ts +30 -0
  269. package/src/maintenance/jobs.ts +29 -67
  270. package/src/maintenance/long-running-work.ts +3 -1
  271. package/src/maintenance/offline-agent-prune-job.ts +28 -0
  272. package/src/maintenance/orphaned-sessions.ts +5 -4
  273. package/src/maintenance/pauses.ts +4 -10
  274. package/src/maintenance/plan-ready-notice-job.ts +71 -0
  275. package/src/maintenance/scheduler.ts +18 -13
  276. package/src/maintenance/stalled-delivery-pager.ts +4 -4
  277. package/src/maintenance/teams.ts +19 -30
  278. package/src/maintenance/workspaces.ts +17 -27
  279. package/src/managed-policy-escalation.ts +7 -7
  280. package/src/managed-policy.ts +3 -3
  281. package/src/managed-provider-gate.ts +5 -4
  282. package/src/managed-spawn-reservation.ts +5 -4
  283. package/src/mcp/index.ts +8 -8
  284. package/src/mcp/output-descriptions.ts +1 -1
  285. package/src/mcp/presentation.ts +2 -1
  286. package/src/mcp/runtime-output.ts +6 -1
  287. package/src/mcp/self-care-visibility.ts +2 -2
  288. package/src/mcp/spawn-targets-matrix.ts +10 -1
  289. package/src/mcp/tool-groups.ts +3 -2
  290. package/src/mcp/tool-roles.ts +81 -0
  291. package/src/mcp/tools-workspace.ts +1 -1
  292. package/src/mcp-blackboard-tools.ts +6 -5
  293. package/src/mcp-decision-tools.ts +7 -7
  294. package/src/mcp-egress-tools.ts +10 -9
  295. package/src/mcp-issue-activity-tools.ts +2 -2
  296. package/src/mcp-issue-association-tools.ts +4 -4
  297. package/src/mcp-issue-ref-tools.ts +3 -3
  298. package/src/mcp-merge-tools.ts +5 -5
  299. package/src/mcp-notification-subscription-tools.ts +14 -15
  300. package/src/mcp-output-levels.test-fixture.ts +1 -1
  301. package/src/mcp-profile-trust-tools.ts +1 -1
  302. package/src/mcp-project-journal-tools.ts +4 -12
  303. package/src/mcp-project-tools.ts +17 -18
  304. package/src/mcp-routing-policy-tools.ts +3 -2
  305. package/src/mcp-team-tools.ts +6 -6
  306. package/src/mcp-workflow-tools.ts +5 -3
  307. package/src/memory-area-injection.ts +13 -12
  308. package/src/memory-boundary-capture.ts +6 -6
  309. package/src/memory-injection.ts +45 -43
  310. package/src/merge-pause-maintenance.ts +5 -4
  311. package/src/notification-router.ts +44 -17
  312. package/src/notify.ts +5 -5
  313. package/src/observed-reset-refresh.ts +4 -4
  314. package/src/operator-alarm.ts +2 -5
  315. package/src/project-ingest.ts +6 -6
  316. package/src/project-instructions.ts +8 -1
  317. package/src/project-trust.ts +12 -11
  318. package/src/prompt-resolver.ts +3 -3
  319. package/src/provider-quota-async.ts +5 -7
  320. package/src/provider-timeline-event.ts +4 -7
  321. package/src/quota-band-delivery.ts +2 -2
  322. package/src/recipe-runner.ts +4 -3
  323. package/src/retention-config-store.ts +4 -3
  324. package/src/reviewer-pipeline.ts +11 -10
  325. package/src/routes/_route.ts +4 -4
  326. package/src/routes/_shared.ts +13 -12
  327. package/src/routes/agent-effort.ts +5 -5
  328. package/src/routes/agent-heartbeat-quota.ts +23 -0
  329. package/src/routes/agent-keep.ts +1 -1
  330. package/src/routes/agent-reporting.ts +1 -1
  331. package/src/routes/agent-self-effort.ts +5 -5
  332. package/src/routes/agent-sessions.ts +24 -24
  333. package/src/routes/agents-spawn.ts +1 -1
  334. package/src/routes/agents.ts +17 -119
  335. package/src/routes/auth.ts +14 -15
  336. package/src/routes/authority-audit.ts +2 -2
  337. package/src/routes/automations.ts +6 -5
  338. package/src/routes/channel-binding-delete.ts +1 -1
  339. package/src/routes/channel-output.ts +3 -3
  340. package/src/routes/claude-api-budget.ts +5 -5
  341. package/src/routes/commands-listing.ts +1 -1
  342. package/src/routes/commands.ts +29 -27
  343. package/src/routes/context-resets.ts +2 -2
  344. package/src/routes/coordinator-teams.ts +1 -1
  345. package/src/routes/decisions.ts +2 -2
  346. package/src/routes/host-secret-files.ts +1 -1
  347. package/src/routes/hosts.ts +11 -11
  348. package/src/routes/inbox.ts +3 -2
  349. package/src/routes/index.ts +2 -1
  350. package/src/routes/integrations.ts +9 -7
  351. package/src/routes/issue-activity.ts +2 -2
  352. package/src/routes/issue-associations.ts +4 -4
  353. package/src/routes/issue-refs.ts +3 -3
  354. package/src/routes/kernel-timers-operator.ts +1 -1
  355. package/src/routes/memory.ts +1 -1
  356. package/src/routes/merges.ts +11 -11
  357. package/src/routes/messages.ts +23 -17
  358. package/src/routes/orchestrator-bootstrap.ts +1 -1
  359. package/src/routes/orchestrator-host-registration.ts +2 -0
  360. package/src/routes/orchestrator-host-utilization.ts +2 -2
  361. package/src/routes/orchestrator-proxy.ts +2 -2
  362. package/src/routes/orchestrator.ts +27 -18
  363. package/src/routes/pairs.ts +8 -8
  364. package/src/routes/project-profile-instructions.ts +1 -1
  365. package/src/routes/project-trust.ts +1 -1
  366. package/src/routes/projects.ts +16 -16
  367. package/src/routes/prompt-templates.ts +12 -12
  368. package/src/routes/provider-quotas.ts +10 -10
  369. package/src/routes/run-command.ts +1 -1
  370. package/src/routes/secrets-backup.ts +2 -2
  371. package/src/routes/secrets-import.ts +1 -1
  372. package/src/routes/secrets-vault.ts +7 -7
  373. package/src/routes/settings.ts +1 -1
  374. package/src/routes/shipping.ts +9 -1
  375. package/src/routes/spawn-policy.ts +8 -7
  376. package/src/routes/sse.ts +2 -2
  377. package/src/routes/team-templates.ts +7 -7
  378. package/src/routes/teams.ts +10 -10
  379. package/src/routes/tokens.ts +18 -17
  380. package/src/routes/users.ts +24 -24
  381. package/src/routes/viewer-read.ts +3 -3
  382. package/src/routes/web-tool-calls.ts +4 -4
  383. package/src/routes/workflows.ts +2 -2
  384. package/src/routes/workspaces.ts +17 -16
  385. package/src/routing-policy-push.ts +6 -6
  386. package/src/runtime-grant.ts +17 -11
  387. package/src/runtime-tokens.ts +20 -28
  388. package/src/secrets-broker/config.ts +1 -1
  389. package/src/secrets-broker/service.ts +2 -2
  390. package/src/security.ts +3 -1
  391. package/src/self-resume-carry.ts +21 -110
  392. package/src/self-resume-supersede.ts +4 -3
  393. package/src/self-resume.ts +54 -66
  394. package/src/server-background.ts +1 -1
  395. package/src/server.ts +21 -22
  396. package/src/services/agent-action.ts +12 -31
  397. package/src/services/agent-effort.ts +7 -32
  398. package/src/services/agent-hang-watchdog-job.ts +2 -2
  399. package/src/services/agent-heartbeat.ts +166 -0
  400. package/src/services/agent-reporting.ts +1 -1
  401. package/src/services/browser-inventory.ts +3 -3
  402. package/src/services/browser-session-state.ts +3 -2
  403. package/src/services/browser-session.ts +21 -27
  404. package/src/services/bus-command-update.ts +9 -9
  405. package/src/services/checkout-refresh-state.ts +1 -0
  406. package/src/services/checkout-refresh.test-fixture.ts +3 -2
  407. package/src/services/command-host.ts +7 -2
  408. package/src/services/coordinator-digest.ts +3 -2
  409. package/src/services/dispatch-command.ts +12 -13
  410. package/src/services/effective-launch-driver.ts +14 -9
  411. package/src/services/host-changed-paths.test-fixture.ts +5 -6
  412. package/src/services/host-project-readiness.ts +2 -2
  413. package/src/services/host-providers.ts +1 -1
  414. package/src/services/host-utilization.ts +9 -9
  415. package/src/services/idle-agent-digest.ts +17 -12
  416. package/src/services/idle-epics.ts +4 -4
  417. package/src/services/interrupt-agent.ts +12 -9
  418. package/src/services/issue-activity.ts +3 -3
  419. package/src/services/issue-associations.ts +15 -18
  420. package/src/services/issue-lifecycle-comments.ts +3 -2
  421. package/src/services/issue-lifecycle.ts +14 -13
  422. package/src/services/issue-refs.ts +4 -5
  423. package/src/services/kernel-timers-cutover-chaos-worker.ts +1 -1
  424. package/src/services/kernel-timers-ingress-chaos-worker.ts +6 -1
  425. package/src/services/kernel-timers-ingress.ts +3 -3
  426. package/src/services/kernel-timers-message-authority-chaos-worker.ts +3 -10
  427. package/src/services/kernel-timers-operator-chaos-worker.ts +3 -2
  428. package/src/services/kernel-timers-outbox.ts +1 -1
  429. package/src/services/kernel-timers-stage10-operator.ts +1 -1
  430. package/src/services/kernel-timers.ts +4 -3
  431. package/src/services/land-gate-placement.ts +11 -2
  432. package/src/services/land-issue-relations.ts +5 -4
  433. package/src/services/land-restart-retry.ts +7 -6
  434. package/src/services/landed-ancestry.ts +6 -5
  435. package/src/services/legacy-landing-host.ts +4 -1
  436. package/src/services/legacy-landing-recovery.ts +8 -6
  437. package/src/services/managed-running.ts +17 -17
  438. package/src/services/memory-areas.ts +3 -3
  439. package/src/services/merge-pause.ts +9 -15
  440. package/src/services/operator-decisions.ts +37 -41
  441. package/src/services/orchestrator-registration-recovery.ts +6 -2
  442. package/src/services/orchestrator-upgrade.ts +14 -12
  443. package/src/services/permission-decision.ts +4 -3
  444. package/src/services/plan-ci-notice.ts +1 -1
  445. package/src/services/plan-conformance.ts +4 -4
  446. package/src/services/plan-dispatch-policy.ts +10 -3
  447. package/src/services/plan-dispatch.ts +35 -14
  448. package/src/services/plan-graph.ts +39 -261
  449. package/src/services/plan-import.ts +1 -1
  450. package/src/services/plan-live-bindings.ts +8 -37
  451. package/src/services/plan-task-create.ts +1 -1
  452. package/src/services/plan-terminal-refs.ts +2 -12
  453. package/src/services/plan-wake-digest.ts +8 -3
  454. package/src/services/profile-trust.ts +2 -2
  455. package/src/services/project.ts +11 -10
  456. package/src/services/prompt-templates.ts +6 -5
  457. package/src/services/provider-hold-nudge.ts +9 -9
  458. package/src/services/provider-hold-record.ts +13 -12
  459. package/src/services/provider-state-audit.ts +19 -18
  460. package/src/services/provisioning-harness/driver.ts +1 -1
  461. package/src/services/provisioning-harness/fixture-matrix.ts +3 -3
  462. package/src/services/provisioning-harness/lifecycle.ts +24 -22
  463. package/src/services/provisioning-harness/prepare.ts +16 -17
  464. package/src/services/rate-limit-resume.ts +10 -9
  465. package/src/services/red-main-auto-revert.ts +7 -5
  466. package/src/services/red-main-controller-deps.ts +20 -3
  467. package/src/services/red-main-incident.ts +3 -1
  468. package/src/services/red-main-quarantine.ts +2 -3
  469. package/src/services/register-agent.ts +22 -22
  470. package/src/services/restart-project-role.ts +1 -1
  471. package/src/services/restart-spawn.ts +17 -16
  472. package/src/services/review-publish.ts +7 -24
  473. package/src/services/review-ref-reconcile.ts +29 -38
  474. package/src/services/runtime-dependency-alert.ts +27 -0
  475. package/src/services/scheduler-command.ts +11 -9
  476. package/src/services/scheduler.ts +5 -5
  477. package/src/services/self-effort-grant.ts +5 -25
  478. package/src/services/send-message.ts +16 -33
  479. package/src/services/shutdown-agent.ts +17 -14
  480. package/src/services/spawn-agent.ts +12 -41
  481. package/src/services/spawn-chain-inspector.ts +1 -1
  482. package/src/services/spawn-cwd-placement.ts +21 -5
  483. package/src/services/spawn-issue-activity.ts +8 -6
  484. package/src/services/spawn-launch.ts +17 -1
  485. package/src/services/spawn-loss-escalation.ts +1 -1
  486. package/src/services/spawn-registration-watchdog.ts +2 -2
  487. package/src/services/spawn-repo-probe.ts +7 -5
  488. package/src/services/spawn-tree-cleanup.ts +7 -8
  489. package/src/services/steward-reaper.ts +4 -4
  490. package/src/services/task-context-hydration.ts +25 -1
  491. package/src/services/task-delivery.ts +20 -34
  492. package/src/services/task-entity.ts +40 -203
  493. package/src/services/task-issue-link.ts +13 -20
  494. package/src/services/task-issue-weave.ts +12 -11
  495. package/src/services/task-lifecycle.ts +27 -31
  496. package/src/services/task-repo-cwd.ts +37 -0
  497. package/src/services/task-review-hold.ts +4 -17
  498. package/src/services/task-semantic-events.ts +13 -11
  499. package/src/services/task-state-reconcile.ts +3 -4
  500. package/src/services/task-validation.ts +2 -2
  501. package/src/services/team-templates.ts +5 -5
  502. package/src/services/team.ts +31 -98
  503. package/src/services/terminal-provider-exit.ts +6 -5
  504. package/src/services/timer-message-fire-chaos-worker.ts +7 -3
  505. package/src/services/timer-message-fire-outbox.ts +1 -1
  506. package/src/services/transient-agent-reaper.ts +22 -19
  507. package/src/services/turn-reconcile-audit.ts +9 -9
  508. package/src/services/user-onboarding.ts +8 -6
  509. package/src/services/user-profile.ts +6 -9
  510. package/src/services/work-finality.ts +16 -4
  511. package/src/services/workflow-auto-start.ts +2 -1
  512. package/src/services/workflow-caller.ts +61 -4
  513. package/src/services/workflow-child-proof-worker.ts +1 -1
  514. package/src/services/workflow-control-proof-worker.ts +1 -1
  515. package/src/services/workflow-coordinator.ts +8 -2
  516. package/src/services/workflow-decisions.ts +5 -5
  517. package/src/services/workflow-deferred-cleanup.ts +1 -1
  518. package/src/services/workflow-effect-driver.ts +42 -19
  519. package/src/services/workflow-effects.ts +41 -27
  520. package/src/services/workflow-pins.ts +7 -4
  521. package/src/services/workflow-reconcile.ts +1 -0
  522. package/src/services/workflow-resume-pins.ts +3 -3
  523. package/src/services/workflow-routing-defaults.ts +18 -0
  524. package/src/services/workflow-run-view.ts +4 -3
  525. package/src/services/workflow-run.test-fixture.ts +1 -1
  526. package/src/services/workflow-runs.ts +34 -22
  527. package/src/services/workflow-supervisor-state.ts +21 -1
  528. package/src/services/workflow-supervisor.ts +4 -3
  529. package/src/services/workspace-recovery.ts +4 -3
  530. package/src/spawn-admission.ts +10 -2
  531. package/src/spawn-placement.ts +27 -9
  532. package/src/spawn-targets.ts +8 -8
  533. package/src/sse-agent-status.ts +15 -6
  534. package/src/sse.ts +49 -24
  535. package/src/startup-context-injection.ts +61 -57
  536. package/src/steward-handoff.ts +4 -3
  537. package/src/steward-policy-cleanup.ts +9 -8
  538. package/src/steward.ts +2 -2
  539. package/src/task-dispatch-live.ts +5 -2
  540. package/src/task-dispatcher.ts +2 -2
  541. package/src/token-db.ts +2 -2
  542. package/src/token-usage-async.ts +2 -4
  543. package/src/user-awaiting-reply.ts +5 -5
  544. package/src/workspace-actions.ts +24 -29
  545. package/src/workspace-auto-merge.ts +14 -12
  546. package/src/workspace-changed-paths-client.ts +16 -5
  547. package/src/workspace-cleanup-stale.ts +30 -7
  548. package/src/workspace-command-settle.ts +24 -16
  549. package/src/workspace-config.ts +1 -1
  550. package/src/workspace-conflict-resolver.ts +9 -7
  551. package/src/workspace-dead-owner-recovery.ts +5 -4
  552. package/src/workspace-escalation.ts +12 -11
  553. package/src/workspace-human-escalation.ts +3 -3
  554. package/src/workspace-land-gate-watchdog.ts +3 -3
  555. package/src/workspace-land-refusal.ts +20 -0
  556. package/src/workspace-land-stopped.ts +1 -0
  557. package/src/workspace-merge.ts +10 -26
  558. package/src/workspace-nothing-to-land-notice.ts +2 -2
  559. package/src/workspace-orphans.ts +18 -37
  560. package/src/workspace-pr-completion.ts +10 -8
  561. package/src/workspace-rekey.ts +3 -3
  562. package/src/workspace-steward-failure.ts +2 -2
  563. package/src/workspace-steward-wake.ts +2 -2
  564. package/public/assets/agent-liveness-CaNwK0Zf.js +0 -2
  565. package/public/assets/provider-display-DsuWMvVL.js +0 -2
  566. package/public/assets/run-card-CpKqRbhg.js +0 -2
  567. package/public/assets/shipping-B9YtnwtG.js +0 -2
  568. package/public/assets/store-DcClXLH0.js +0 -9
  569. package/runner/src/agent-runner/http-liveness.ts +0 -85
  570. package/src/db/on-loop-write-transaction.ts +0 -34
@@ -27,6 +27,21 @@ function hostLegacyLandingRecoveryCommands(args: { target: string; afterId?: str
27
27
  return rows.map((row) => getCommand(row.id)).filter((command): command is Command => command !== null);
28
28
  }
29
29
 
30
+ /** The newest attempt of a retried command: `baseId` is attempt 1, `baseId:<n>` attempt n (#3827). One PK range
31
+ * scan instead of a point read per past attempt. */
32
+ function latestCommandAttempt(args: { baseId: string }): { attempt: number; command: Command } | null {
33
+ const rows = getDb().query("SELECT id FROM commands WHERE id = ?1 OR (id > ?2 AND id < ?3)")
34
+ .all(args.baseId, `${args.baseId}:`, `${args.baseId};`) as Array<{ id: string }>;
35
+ let latest: { attempt: number; id: string } | undefined;
36
+ for (const { id } of rows) {
37
+ const suffix = id.slice(args.baseId.length + 1);
38
+ const attempt = id === args.baseId ? 1 : /^[1-9]\d*$/.test(suffix) ? Number(suffix) : 0;
39
+ if (attempt > (latest?.attempt ?? 0)) latest = { attempt, id };
40
+ }
41
+ const command = latest ? getCommand(latest.id) : null;
42
+ return latest && command ? { attempt: latest.attempt, command } : null;
43
+ }
44
+
30
45
  export const COMMANDS_READ_OPS = Object.freeze({
31
46
  "commands.latestByCorrelation": (args: { type: string; correlationId: string }) => findLatestCommandByCorrelation(args.type, args.correlationId),
32
47
  "commands.list": (args: Parameters<typeof listCommands>[0]) => listCommands(args),
@@ -39,6 +54,7 @@ export const COMMANDS_READ_OPS = Object.freeze({
39
54
  return command ? { status: command.status, error: command.error, registered: command.params.registrationConfirmed === true, admission: command.admission, orchestratorId: typeof command.params.orchestratorId === "string" ? command.params.orchestratorId : undefined } : null;
40
55
  },
41
56
  "commands.get": (args: { id: string }): Command | null => getCommand(args.id),
57
+ "commands.latestAttempt": latestCommandAttempt,
42
58
  "commands.effortChanges": (args: { agentId: string; from: number; to: number; after?: EffortChangeCursor }) => listEffortChanges(args.agentId, args.from, args.to, args.after),
43
59
  "commands.listLegacyLandingBackfillCandidates": listLegacyLandingBackfillCandidatesBody,
44
60
  // #2102 — the merge⟺deploy interlock drain signal, read off the loop so polling it can never add
@@ -66,7 +66,6 @@ export function closeDb(): void {
66
66
  }
67
67
  db = undefined as unknown as Database;
68
68
  dbPath = undefined;
69
- closeFastFailConnection();
70
69
  }
71
70
 
72
71
  /**
@@ -175,156 +174,6 @@ export const openExistingDb = (path: string): Database => {
175
174
  /** A read-only connection to `path` for a reader that must not share the main RW handle (#3012). */
176
175
  export const openReadOnlyDb = (path: string): Database => new Database(path, { readonly: true });
177
176
 
178
- // #1835/#1840 — a SEPARATE connection to the same database file, opened with a much shorter
179
- // busy_timeout than the shared app connection, lazily created and cached by (path, timeout).
180
- let fastFailConn: Database | undefined;
181
- let fastFailConnPath: string | undefined;
182
- let fastFailConnBusyTimeoutMs: number | undefined;
183
-
184
- function closeFastFailConnection(): void {
185
- if (fastFailConn) {
186
- try { fastFailConn.close(); } catch { /* a half-open handle must not wedge the caller */ }
187
- }
188
- fastFailConn = undefined;
189
- fastFailConnPath = undefined;
190
- fastFailConnBusyTimeoutMs = undefined;
191
- }
192
-
193
- /** Forbids `fn` from returning a `PromiseLike` — see `withFastFailConnection`'s own doc for why an
194
- * async callback would silently defeat the whole point of this helper. Resolves to `never` for any
195
- * `T` that is (or could be) a promise, which TypeScript then rejects at the CALL SITE (the argument
196
- * no longer matches `() => never`) rather than merely in a comment. */
197
- type NotPromiseLike<T> = T extends PromiseLike<unknown> ? never : T;
198
-
199
- function isThenable(value: unknown): value is PromiseLike<unknown> {
200
- return typeof value === "object" && value !== null && typeof (value as { then?: unknown }).then === "function";
201
- }
202
-
203
- /**
204
- * Run `fn` with `getDb()` temporarily pointing at a SEPARATE connection to the same database file,
205
- * using `busyTimeoutMs` instead of the shared app connection's own (much longer) busy_timeout — so a
206
- * write-lock conflict during `fn` fails FAST (a `SQLITE_BUSY`/`SQLITE_LOCKED` throw after
207
- * `busyTimeoutMs`) instead of blocking the event loop for up to the shared connection's own budget.
208
- * Built for `memory-store-health-probe.ts`'s per-scope DB cycle (#1840): a health probe under real
209
- * write-lock contention should report `cannot-tell` fast, not become a multi-second freeze.
210
- *
211
- * LOAD-BEARING INVARIANT — `fn` MUST be fully synchronous: no `await`, no callback deferred to a
212
- * later microtask/macrotask. The swap works by reassigning the SAME module-level `db` binding
213
- * `getDb()` reads, for the exact duration of `fn`'s synchronous call; nothing else can observe the
214
- * swapped connection only because nothing else CAN run while `fn` occupies the (single-threaded)
215
- * call stack, and the shared connection is restored in `finally` before control ever returns to the
216
- * event loop. This is exactly why the shared app connection's OWN busy_timeout is never touched by
217
- * this helper. A caller that needs fast-fail behavior spanning a real await needs a genuinely
218
- * different design (e.g. #1809/#1822's offload primitive), not this one.
219
- *
220
- * ENFORCED AT COMPILE TIME, backstopped (not fixed) at runtime (#1840 re-review MED-1/MED-2) — an
221
- * `async fn` would still return its Promise SYNCHRONOUSLY (the function body only runs to its first
222
- * `await` before returning), so a naive version of this helper would restore the shared connection in
223
- * `finally` and only THEN let the callback's post-`await` continuation run — silently against the
224
- * ordinary shared connection, with no error, defeating the fast-fail guarantee for exactly the call it
225
- * was supposed to protect. `NotPromiseLike<T>` is the REAL protection: it rejects an `async fn` at the
226
- * CALL SITE, so no caller reachable by the type checker can ever produce this shape.
227
- *
228
- * The runtime `isThenable` check below is NOT a second way to prevent the same outcome — it CANNOT be:
229
- * by the time `fn()` has returned a thenable, that thenable's continuation is already scheduled
230
- * independent of anything this function does next, so throwing here does not (and cannot) stop it
231
- * from eventually running against whatever connection is active when it resumes. What the runtime
232
- * check buys is FAIL-LOUD, not fail-safe: a caller that routes an async callback around the type
233
- * system (a cast through `any`, for instance) gets an immediate, unmissable `TypeError` at the call
234
- * site instead of a silent, delayed connection mix-up nobody notices until a later bug report. Treat a
235
- * caller ever seeing this error as a real bug in that call site, not a recoverable runtime condition.
236
- *
237
- * Falls back to running `fn` against the ordinary shared connection for `:memory:` databases (this
238
- * repo's whole test suite) or before any database is open: `bun:sqlite` gives every `:memory:` handle
239
- * a DISTINCT, empty database (no shared-cache URI in use here), so a second `:memory:` connection
240
- * would silently see no data at all rather than fail fast on contention — and there is no meaningful
241
- * lock contention to guard against in a single-process test run in the first place. The thenable
242
- * guard still applies on this path, so misuse is caught the same way regardless of environment.
243
- */
244
- export function withFastFailConnection<T>(busyTimeoutMs: number, fn: () => NotPromiseLike<T>): T {
245
- if (!Number.isSafeInteger(busyTimeoutMs) || busyTimeoutMs < 0) {
246
- throw new RangeError("busyTimeoutMs must be a non-negative integer");
247
- }
248
- const path = getDbPath();
249
- const useFastFail = Boolean(path) && path !== ":memory:";
250
- let original: Database | undefined;
251
- if (useFastFail) {
252
- if (!fastFailConn || fastFailConnPath !== path || fastFailConnBusyTimeoutMs !== busyTimeoutMs) {
253
- closeFastFailConnection();
254
- const conn = new Database(path!, { readwrite: true, create: false });
255
- // The live connection already established WAL/foreign_keys/etc.; only busy_timeout needs to
256
- // differ, applied AFTER the shared pragmas so it overrides the ordinary default.
257
- applyConnectionPragmas(conn, { ensureWal: false });
258
- conn.run(`PRAGMA busy_timeout = ${busyTimeoutMs}`);
259
- fastFailConn = conn;
260
- fastFailConnPath = path;
261
- fastFailConnBusyTimeoutMs = busyTimeoutMs;
262
- }
263
- original = db;
264
- db = fastFailConn;
265
- }
266
- try {
267
- const result = fn();
268
- if (isThenable(result)) {
269
- // Fail LOUD, not fail-safe — see this function's doc for why this can only make the misuse
270
- // visible, not undo it: the thenable's continuation is already scheduled and will still run
271
- // against whatever connection is active when it resumes, regardless of this throw.
272
- throw new TypeError(
273
- "withFastFailConnection: fn returned a thenable — this is a caller bug, not a recoverable " +
274
- "condition. fn must be fully synchronous; an async callback's post-await continuation runs " +
275
- "AFTER this call restores the shared connection, so it silently escapes the fast-fail " +
276
- "guarantee even though this check caught it here. Fix the caller — do not catch this and " +
277
- "continue.",
278
- );
279
- }
280
- return result as T;
281
- } finally {
282
- if (useFastFail) db = original!;
283
- }
284
- }
285
-
286
- /** #2712 — is the write lock free right now? One empty `BEGIN IMMEDIATE` on the fast-fail connection,
287
- * so a paced sweep can step aside for another writer instead of waiting out `busy_timeout` per write.
288
- * Always true for `:memory:` (no second writer can exist), like {@link withFastFailConnection}. */
289
- export function writerLockAvailable(busyTimeoutMs: number): boolean {
290
- try {
291
- return withFastFailConnection(busyTimeoutMs, () => {
292
- const conn = getDb();
293
- if (conn.inTransaction) return true;
294
- conn.run("BEGIN IMMEDIATE");
295
- conn.run("ROLLBACK");
296
- return true;
297
- });
298
- } catch (err) {
299
- if (isSqliteBusyError(err)) return false;
300
- throw err;
301
- }
302
- }
303
-
304
- // SQLITE_BUSY resilience for WRITE transactions (#1428).
305
- //
306
- // bun:sqlite's `db.transaction(fn)()` begins DEFERRED: the write lock is only taken when the first
307
- // write statement runs. A read-then-write transaction (the hot message-send path reads getAgent /
308
- // idempotency / duplicate checks, THEN inserts) therefore starts as a reader and UPGRADES to a
309
- // writer. If another connection holds the write lock at that instant — e.g. the maintenance-worker
310
- // subprocess mid-checkpoint/prune — SQLite returns SQLITE_BUSY *immediately* and IGNORES
311
- // busy_timeout, because waiting for the upgrade could deadlock two readers. That is exactly the
312
- // production `database is locked` on `POST /api/messages`.
313
- //
314
- // `BEGIN IMMEDIATE` takes the write lock up front, so busy_timeout governs contention: a concurrent
315
- // writer WAITS for the lock instead of erroring on the upgrade. Route every read-then-write
316
- // transaction through `writeTransaction`; keep pure read-only transactions on the deferred default
317
- // (they never upgrade, and IMMEDIATE would needlessly serialize reads).
318
- //
319
- // SHORT sync-block bias (#1428): `writeTransaction` makes a SINGLE immediate attempt — it never
320
- // synchronously sleeps-and-retries, because that would only lengthen the main-thread stall. With
321
- // busy_timeout kept low, that one attempt blocks the loop only briefly. Resilience for the rare BUSY
322
- // that survives busy_timeout lives in `runWriteWithBusyRetry`, which awaits between attempts so the
323
- // event loop breathes (other requests/timers run) instead of the thread spinning.
324
- const SQLITE_BUSY_RETRY_ATTEMPTS = 4;
325
- const SQLITE_BUSY_RETRY_BASE_MS = 15;
326
- const SQLITE_BUSY_RETRY_MAX_MS = 120;
327
-
328
177
  /** True for the transient lock-contention codes SQLite raises under concurrency. */
329
178
  export function isSqliteBusyError(err: unknown): boolean {
330
179
  if (!err || typeof err !== "object") return false;
@@ -337,13 +186,7 @@ export function isSqliteBusyError(err: unknown): boolean {
337
186
  return typeof message === "string" && /database (is|table is) locked|SQLITE_BUSY|SQLITE_LOCKED/i.test(message);
338
187
  }
339
188
 
340
- /**
341
- * Run `fn` inside a single `BEGIN IMMEDIATE` transaction. The write lock is taken at BEGIN, so
342
- * busy_timeout (short, by design) governs contention and the loop stalls only briefly. On a BUSY
343
- * that survives busy_timeout this THROWS — the caller on the hot path wraps it in
344
- * `runWriteWithBusyRetry` for an async, loop-yielding retry; other callers surface the error as
345
- * before. Keep the transaction body small: it runs synchronously with the write lock held.
346
- */
189
+ // Writer-thread transaction context; boot and in-memory fixtures use the same bodies.
347
190
  declare const WriteTransactionContextBrand: unique symbol;
348
191
 
349
192
  /** Unforgeable caller capability proving code is executing synchronously inside `writeTransaction`.
@@ -413,30 +256,6 @@ export function inWriteTransaction<T>(fn: (tx: WriteTransactionContext, db: Data
413
256
  return active ? fn(active, getDb()) : writeTransaction((tx) => fn(tx, getDb()));
414
257
  }
415
258
 
416
- /**
417
- * Hot-path write with a bounded, ASYNC retry around SQLITE_BUSY (#1428). Each attempt is one short
418
- * synchronous `writeTransaction`; between attempts we `await` a jittered backoff via setTimeout,
419
- * which YIELDS the event loop so other work runs and the contending writer (another process, a
420
- * checkpoint) can release the lock — the opposite of a long synchronous busy_timeout spin. A rare
421
- * contention spike thus degrades to a slightly slower request instead of a 500 or a multi-second
422
- * main-thread stall.
423
- */
424
- export async function runWriteWithBusyRetry<T>(fn: () => T, attempts: number = SQLITE_BUSY_RETRY_ATTEMPTS): Promise<T> {
425
- for (let attempt = 1; ; attempt++) {
426
- try {
427
- return writeTransaction(fn);
428
- } catch (err) {
429
- if (attempt < attempts && isSqliteBusyError(err)) {
430
- const cap = Math.min(SQLITE_BUSY_RETRY_MAX_MS, SQLITE_BUSY_RETRY_BASE_MS * 2 ** (attempt - 1));
431
- const delay = cap / 2 + Math.random() * (cap / 2);
432
- await new Promise((resolve) => setTimeout(resolve, delay));
433
- continue;
434
- }
435
- throw err;
436
- }
437
- }
438
- }
439
-
440
259
  // Slow-query log. Off unless AGENT_RELAY_DB_SLOW_MS is set (>0). Wraps the
441
260
  // execution of the heavy dynamic queries so the next slow query surfaces before
442
261
  // it wedges something (see issue #197 — the 8.6s reply-obligation query).
@@ -0,0 +1,74 @@
1
+ import { isRecord, stringValue } from "@humwick/sdk";
2
+ import { attachRecoveryCommand, carryingRecoveryObligations, closeRecoveryObligations, insertContextReset, openRecoveryObligation, readsToCarry, type ContextResetFact, type RecoveryObligation } from "./context-reset-carry";
3
+ import { sendMessageWithResultInternal, type SendMessageResult } from "./messages";
4
+ import { isContinuationSelfResumeCommand } from "../self-resume-command";
5
+ import type { Command } from "../types";
6
+ const LISTED_IDS = 200;
7
+ const CARRY_MARGIN_MS = 10 * 60_000;
8
+ const FALLBACK_STATUSES = new Set(["failed", "canceled", "timed_out", "rejected"]);
9
+
10
+ export function recordContextResetBody(fact: ContextResetFact, now: number): SendMessageResult | null {
11
+ if (!insertContextReset(fact, now)) return null;
12
+ const carrying = carryingRecoveryObligations(fact.agentId);
13
+ if (!carrying.length) return null;
14
+ const result = sendCarry(fact.agentId, carrying, `${fact.resetId}:0`, { resetId: fact.resetId, kind: fact.kind });
15
+ closeRecoveryObligations(fact.agentId, settledBy(fact, carrying), `reset:${fact.resetId}`, now);
16
+ return result;
17
+ }
18
+ export function settleContextResetCarryBody(command: Command, now: number): SendMessageResult | null {
19
+ const boundary = FALLBACK_STATUSES.has(command.status) ? commandBoundary(command) : null;
20
+ if (!boundary) return null;
21
+ const carrying = carryingRecoveryObligations(boundary.agentId);
22
+ if (!carrying.some(obligation => obligation.open && obligation.generation === boundary.generation)) return null;
23
+ const sent = sendCarry(boundary.agentId, carrying, `${command.id}:fallback`, { boundaryCommandId: command.id, status: command.status });
24
+ if (isRecord(command.result) && command.result.destructiveInputSubmitted === false) closeRecoveryObligations(boundary.agentId, [boundary.generation], `submit-aborted:${command.id}`, now);
25
+ return sent;
26
+ }
27
+ /** A reset settles only the boundary its provider evidence names; a replaced process settles all. */
28
+ function settledBy(fact: ContextResetFact, carrying: RecoveryObligation[]): number[] {
29
+ const open = carrying.filter((obligation) => obligation.open);
30
+ if (fact.kind === "restart") return open.map((obligation) => obligation.generation);
31
+ return open.filter((obligation) => fact.commandId !== undefined && obligation.commandId === fact.commandId).map((obligation) => obligation.generation);
32
+ }
33
+
34
+ export function commandBoundary(command: Command): { agentId: string; generation: number } | null {
35
+ const selfResume = isRecord(command.params.selfResume) ? command.params.selfResume : undefined;
36
+ const generation = selfResume?.generation;
37
+ if (typeof generation !== "number") return null;
38
+ if (command.type === "agent.restart") {
39
+ const agentId = stringValue(command.params.agentId);
40
+ return agentId && isRecord(command.params.selfRestart) ? { agentId, generation } : null;
41
+ }
42
+ return isContinuationSelfResumeCommand(command) ? { agentId: command.target, generation } : null;
43
+ }
44
+
45
+ type SendResult = SendMessageResult;
46
+
47
+ // Floor: the earliest capture start minus the margin. No upper bound: a read after the reset is a duplicate, never a loss, and needs no clock comparison.
48
+ function sendCarry(agentId: string, carrying: RecoveryObligation[], key: string, detail: Record<string, unknown>): SendResult | null {
49
+ const ids = readsToCarry(agentId, Math.min(...carrying.map((obligation) => obligation.openedAt)) - CARRY_MARGIN_MS);
50
+ if (!ids.length) return null;
51
+ return sendMessageWithResultInternal({
52
+ from: "system",
53
+ to: agentId,
54
+ kind: "system",
55
+ subject: "Possible unread messages",
56
+ body: renderCarryBody(ids),
57
+ payload: { selfResumeCarry: { ...detail, generations: carrying.map((obligation) => obligation.generation), messageIds: ids } },
58
+ idempotencyKey: `self-resume-carry:${agentId}:${key}`,
59
+ replyExpected: false,
60
+ maxAgeSeconds: null,
61
+ }, false);
62
+ }
63
+
64
+ function renderCarryBody(ids: number[]): string {
65
+ const listed = ids.slice(0, LISTED_IDS).map((id) => `#${id}`).join(" ");
66
+ const rest = ids.length > LISTED_IDS ? `\n…and ${ids.length - LISTED_IDS} more, up to #${ids.at(-1)}; every id is in this message's payload.` : "";
67
+ return [
68
+ `Possible unread messages: ${ids.length} message${ids.length === 1 ? "" : "s"} reached you around your context reset, after or shortly before you saved your working state, so your current context may not hold them.`,
69
+ "Check each one your working state does not already cover with relay_get_message <id>, and handle it. Some may be duplicates of work you have done.",
70
+ "Messages you already handled may appear here, including the reset instruction itself: do not repeat a reset instruction found here.",
71
+ "",
72
+ `${listed}${rest}`,
73
+ ].join("\n");
74
+ }
@@ -26,12 +26,12 @@ export function ensureContinuationEnvelope(input: {
26
26
  agentId: string;
27
27
  objective?: string;
28
28
  now?: number;
29
- }): ContinuationEnvelope {
29
+ }, submittedAt: number = Date.now()): ContinuationEnvelope {
30
30
  const existing = getContinuationEnvelope(input.agentId);
31
31
  if (existing) return existing;
32
32
  const objective = input.objective;
33
33
  if (!objective?.trim()) throw new ValidationError("objective required for first self-resume when no spawn prompt was recorded");
34
- const now = input.now ?? Date.now();
34
+ const now = input.now ?? submittedAt;
35
35
  getDb().query(`
36
36
  INSERT INTO agent_continuations (agent_id, objective, objective_epoch, generation, ledger, working_state, archive_refs, budget, created_at, updated_at)
37
37
  VALUES (?, ?, ?, 0, '[]', '', '[]', ?, ?, ?)
@@ -40,11 +40,11 @@ export function ensureContinuationEnvelope(input: {
40
40
  }
41
41
 
42
42
  /** Replace Relay-owned objective text and start a new render-time ledger epoch. */
43
- export function updateContinuationObjective(input: { agentId: string; objective: string; now?: number }): ContinuationEnvelope {
43
+ export function updateContinuationObjective(input: { agentId: string; objective: string; now?: number }, submittedAt: number = Date.now()): ContinuationEnvelope {
44
44
  if (!input.objective.trim()) throw new ValidationError("objective required");
45
45
  const existing = getContinuationEnvelope(input.agentId);
46
46
  if (!existing) throw new ValidationError("continuation envelope not found");
47
- const now = input.now ?? Date.now();
47
+ const now = input.now ?? submittedAt;
48
48
  getDb().query(`
49
49
  UPDATE agent_continuations
50
50
  SET objective = ?, objective_epoch = ?, updated_at = ?
@@ -1,3 +1,7 @@
1
+ import { EffortReportRejected } from "./agent-effort-state";
2
+ import { TeamAuthError, TeamNotFoundError } from "./team-claim-policy";
3
+ import { EffortReportFenced } from "./agent-effort-observe";
4
+ import { KernelReplayExpiredError } from "@humwick/kernel/sqlite-kernel-persistence";
1
5
  import { VaultError } from "../secrets-vault/keys";
2
6
  // H2 (ADR 0001 §3.7) — the error-reconstruction registry for the async DB seam.
3
7
  //
@@ -92,6 +96,10 @@ export const DB_ERROR_REGISTRY: Readonly<Record<string, DbErrorSpec>> = Object.f
92
96
  fields: ["code"],
93
97
  reconstruct: (message, fields) => new VaultError(String(fields.code ?? message), message),
94
98
  },
99
+ EffortReportRejected: { fields: [], reconstruct: message => new EffortReportRejected(message) },
100
+ EffortReportFenced: { fields: [], reconstruct: (message) => new EffortReportFenced(message) },
101
+ TeamAuthError: { fields: [], reconstruct: message => new TeamAuthError(message) },
102
+ TeamNotFoundError: { fields: [], reconstruct: message => new TeamNotFoundError(message) },
95
103
  ValidationError: {
96
104
  // Empty subclass — the prototype IS the payload; message carries everything.
97
105
  fields: [],
@@ -213,6 +221,10 @@ export const DB_ERROR_REGISTRY: Readonly<Record<string, DbErrorSpec>> = Object.f
213
221
  return err;
214
222
  },
215
223
  },
224
+ KernelReplayExpiredError: {
225
+ fields: ["operation", "requestKey"],
226
+ reconstruct: (_message, f) => new KernelReplayExpiredError(String(f.operation), String(f.requestKey)),
227
+ },
216
228
  WorkflowHistoryExpiredError: {
217
229
  // Thrown by reopenInstance inside the controlWorkflow write op, so it crosses the writer seam (#3018).
218
230
  fields: ["instanceId"],
package/src/db/gateway.ts CHANGED
@@ -37,34 +37,12 @@
37
37
  // inherits the phantom-failure guarantee ("a write may be committed even when reported as failed";
38
38
  // reconcile via the key, never blind-retry) for free by routing through this surface.
39
39
  //
40
- // TWO BACKENDS behind one interface (§3.8):
41
- // - in-process (the Phase-0 DEFAULT, §5/§7): every op runs synchronously against the ambient main
42
- // connection (`getDb()`), wrapped in a resolved Promise. Behaviorally identical to today's sync
43
- // path, so this whole module SHIPS GREEN with ZERO callsite moves. Under `bun test` the in-process
44
- // backend still round-trips args/results/errors through the SAME marshaling path as the worker
45
- // (H1 §3.8.1), so the default suite fails exactly where production would.
46
- // - worker-backed (a file DB, gated on explicitly, §7 Phase 1/2): writes to the size-1 writer pool,
47
- // reads to the reader pool. Off the loop; proven by the file-backed integration suite. Production
48
- // rollout flips per-domain in S3 (§11.5) — this module does NOT turn it on at bootstrap.
49
- //
50
- // THE TWO BACKENDS ARE SELECTED PER HALF (#1960 T1). `initGateway` takes `readerBackend` / `writerBackend`
51
- // independently (with `backend` kept as the both-halves shorthand); BOTH default to in-process, so this
52
- // change is behavior-preserving by construction. The split exists because the halves are different risk
53
- // classes, and coupling them is what blocked the rollout:
54
- // - READS are safe to offload. Under WAL a reader takes no write lock, so a worker reader cannot block
55
- // or be blocked by the writer, and no main-thread state changes. This is ADR §7 Phase 1 verbatim
56
- // ("Route reader ops through the reader pool in production; keep writes in-process on the loop
57
- // initially") — and it accounts for ~55% of measured on-loop block-ms on prod.
58
- // - WRITES are not. A worker WRITER owns the sole RW connection, so its bootstrap calls `closeDb()`
59
- // and every surviving main-thread `getDb()` callsite — ~1380 of them, `/health` included — starts
60
- // throwing. That is the S3 burn-down (#1870), not a config flip.
61
- // So a reader-only flip must leave the writer half COMPLETELY untouched: same in-process writer, same
62
- // live ambient connection, `closeDb()` never called by this module. Everything keyed off "is the main RW
63
- // connection still ours?" (`gatewayBackend()`, the writer fault seam, the writer self-heal path) is
64
- // therefore keyed off the WRITER half specifically, never off "either half is a worker".
40
+ // File databases always use the writer thread (#1870 S8). The main connection remains available
41
+ // for reads and boot migrations; the in-process writer is only for :memory:. Reader placement is
42
+ // independent and still controlled by the reader backend option.
65
43
 
66
44
  import { errMessage } from "@humwick/sdk";
67
- import { getDbPath } from "./connection.ts";
45
+ import { getDbGeneration, getDbPath } from "./connection.ts";
68
46
  // Op-timing capture source (#1882 / ADR §5). One-way dependency — loop-block-attribution imports
69
47
  // event-loop-lag + config only, never the gateway/writer stack, so this closes no cycle.
70
48
  import { withLoopFrameCapture } from "../loop-block-attribution.ts";
@@ -237,74 +215,38 @@ export interface DbGatewayStats {
237
215
  }
238
216
 
239
217
  export interface InitGatewayOptions {
240
- /** DB path; defaults to the live `getDbPath()`. Only consulted when a half runs on a worker. */
218
+ /** Defaults to the live getDbPath(). File databases default both pools to workers. */
241
219
  dbPath?: string;
242
- /**
243
- * Shorthand that sets BOTH halves at once. Defaults to `"in-process"` — the Phase-0 no-op that runs
244
- * every op synchronously against the ambient main connection (so bootstrap spawns NO worker threads
245
- * and behavior is unchanged). `"worker"` requires a file DB and stands up the writer + reader pools;
246
- * it is used by the integration suite and by the per-domain S3 rollout, never at Phase-0 bootstrap.
247
- *
248
- * `readerBackend` / `writerBackend` override this per half — see them for the split rationale.
249
- */
220
+ /** Shorthand for reader placement; file writes always run on the worker. */
250
221
  backend?: GatewayBackend;
251
- /**
252
- * The READ path's backend, overriding `backend`. Defaults to `backend ?? "in-process"`.
253
- *
254
- * THE POINT OF THE SPLIT (#1960 T1 / ADR §7 Phase 1). Reads are the safe half to offload: under WAL a
255
- * reader takes no write lock and cannot block or be blocked by the writer, so a worker-backed reader
256
- * changes NOTHING about write behavior. The writer half is a different risk class entirely — the
257
- * worker writer owns the sole RW connection, so its bootstrap calls `closeDb()` and every surviving
258
- * main-thread `getDb()` callsite (incl. `/health`) starts throwing. Keeping the writer in-process
259
- * while the reader moves off-loop is therefore the only landable first step, and before this split it
260
- * was not even expressible: one `backend` flag flipped both halves together.
261
- */
262
222
  readerBackend?: GatewayBackend;
263
- /**
264
- * The WRITE path's backend, overriding `backend`. Defaults to `backend ?? "in-process"`.
265
- *
266
- * CAUTION: a worker writer takes ownership of the sole RW connection — its callers are expected to
267
- * `closeDb()` first (see the module header and `initWriter`). Leave this `"in-process"` unless you
268
- * are deliberately doing that; a reader-only flip must NOT touch it.
269
- */
223
+ /** Legacy preference; a file database always resolves to worker. */
270
224
  writerBackend?: GatewayBackend;
271
- /** Writer-pool tuning (queue high-water, hard cap, writer-job deadline) — see `InitWriterOptions`. */
272
225
  writer?: Omit<InitWriterOptions, "path">;
273
- /** Reader-pool tuning (size, queue high-water, per-job timeout). */
274
226
  reader?: { size?: number; queueHighWater?: number; timeoutMs?: number };
275
227
  }
276
228
 
277
- /** The resolved per-half backend selection — what `initGateway` actually stands up. */
278
229
  export interface ResolvedGatewayBackends {
279
230
  reader: GatewayBackend;
280
231
  writer: GatewayBackend;
281
232
  }
282
233
 
283
- /**
284
- * Resolve the per-half backends from the options: the specific `readerBackend`/`writerBackend` wins,
285
- * then the `backend` shorthand, then the in-process default. Pure, so the selection matrix is testable
286
- * without standing up pools or spawning threads (mirrors `resolveReaderBackend` in reader-pool.ts).
287
- */
288
234
  export function resolveGatewayBackends(options: InitGatewayOptions = {}): ResolvedGatewayBackends {
289
- const both = options.backend ?? "in-process";
290
- return { reader: options.readerBackend ?? both, writer: options.writerBackend ?? both };
235
+ const path = options.dbPath ?? getDbPath();
236
+ return {
237
+ reader: options.readerBackend ?? options.backend ?? (path && path !== ":memory:" ? "worker" : "in-process"),
238
+ writer: path && path !== ":memory:" ? "worker" : options.writerBackend ?? options.backend ?? "in-process",
239
+ };
291
240
  }
292
241
 
293
242
  // ── Backend state ───────────────────────────────────────────────────────────────────────────────
294
243
 
295
244
  let reader: ReaderGateway | undefined;
296
- // The two halves are tracked SEPARATELY (#1960 T1). Both default to in-process, so a cold/plain
297
- // bootstrap is byte-for-byte the pre-split behavior.
298
245
  let currentReaderBackend: GatewayBackend = "in-process";
299
246
  let currentWriterBackend: GatewayBackend = "in-process";
300
- // The file path a WORKER WRITER was stood up against, RETAINED here so a writer-only re-init
301
- // (ensureInit self-heal after a lone shutdownWriter) can reconstruct the worker writer WITHOUT
302
- // depending on `getDbPath()` — worker-WRITER bootstrap intentionally calls `closeDb()` (the worker owns
303
- // the sole RW connection), which clears `getDbPath()`, so relying on it would silently re-init the
304
- // writer in-process against a torn-down main handle (HIGH-1, codex-terra L1 review). Deliberately
305
- // tracks the WRITER only: a worker READER never closes the main handle, so an in-process writer under a
306
- // worker reader must still self-heal to `":memory:"` (= the ambient `getDb()`), never to a file path.
247
+ // Retained for callers that release their bootstrap handle after initializing the gateway.
307
248
  let writerWorkerDbPath: string | undefined;
249
+ let gatewayDbGeneration = -1;
308
250
 
309
251
  function replaceReader(next: ReaderGateway): void {
310
252
  const previous = reader;
@@ -314,19 +256,7 @@ function replaceReader(next: ReaderGateway): void {
314
256
  if (previous) void previous.destroy().catch((error) => console.error(`[gateway] reader teardown failed: ${errMessage(error)}`));
315
257
  }
316
258
 
317
- /**
318
- * Stand up the unified gateway. Idempotent-friendly: re-calling it replaces the backend (tearing the
319
- * old one down). Chooses in-process for BOTH halves by default — the Phase-0 no-op — so a plain
320
- * `getGateway()` never spawns worker threads. Pass `{ backend: "worker" }` (file DB) to route both
321
- * families through the real pools, or `{ readerBackend: "worker" }` alone for the Phase-R shape:
322
- * reads off the loop, writes still on the ambient main connection.
323
- *
324
- * THE TWO HALVES ARE STOOD UP INDEPENDENTLY and neither reaches into the other's state. In particular
325
- * this function NEVER touches the main connection: `closeDb()` is the CALLER's step, required only for
326
- * a worker WRITER (which then owns the sole RW connection). So `{ readerBackend: "worker" }` leaves the
327
- * writer exactly as a default init leaves it — in-process, running op bodies against the live ambient
328
- * `getDb()` — and the ~1380 raw-handle callsites (incl. `/health`) keep working untouched.
329
- */
259
+ /** Select reader placement independently; file writes always use one FIFO and one worker. */
330
260
  export function initGateway(options: InitGatewayOptions = {}): void {
331
261
  const { reader: readerB, writer: writerB } = resolveGatewayBackends(options);
332
262
  const dbPath = options.dbPath ?? getDbPath();
@@ -338,17 +268,8 @@ export function initGateway(options: InitGatewayOptions = {}): void {
338
268
  `requested worker half(s): ${halves}`,
339
269
  );
340
270
  }
341
- // ── WRITER half ─────────────────────────────────────────────────────────────────────────────────
342
- // `initWriter` picks its backend from the path: a file → the worker pool, `":memory:"` → in-process.
343
- // The in-process writer holds no connection of its own and runs the op against the ambient `getDb()`
344
- // (the real main handle, file or memory) — which is why an in-process writer is passed `":memory:"`
345
- // even when the process is on a file DB, and why it is unaffected by a worker-backed reader.
346
- initWriter({ ...options.writer, path: writerB === "worker" ? (dbPath as string) : ":memory:" });
347
- // RETAIN for writer-only self-heal (HIGH-1) — getDbPath() is cleared by a worker writer's closeDb().
348
- writerWorkerDbPath = writerB === "worker" ? (dbPath as string) : undefined;
349
- // ── READER half ─────────────────────────────────────────────────────────────────────────────────
350
- // Force the backend rather than letting it fall out of the path, so a file-DB server in Phase 0 still
351
- // runs every read synchronously on the main connection (no worker spawned, no callsite moved).
271
+ initWriter({ ...options.writer, path: writerB === "worker" ? dbPath : ":memory:" });
272
+ writerWorkerDbPath = writerB === "worker" ? dbPath : undefined;
352
273
  replaceReader(
353
274
  readerB === "worker"
354
275
  ? createReaderGateway({
@@ -362,6 +283,7 @@ export function initGateway(options: InitGatewayOptions = {}): void {
362
283
  );
363
284
  currentReaderBackend = readerB;
364
285
  currentWriterBackend = writerB;
286
+ gatewayDbGeneration = getDbGeneration();
365
287
  // An in-transaction crash seam is a main-thread closure; once op bodies run on the WRITER thread it
366
288
  // can never be delivered, so arming one must FAIL rather than silently inject nothing (L-C #1899).
367
289
  // Keyed on the WRITER half only — the seam is armed inside write-op bodies, so a worker reader leaves
@@ -369,7 +291,7 @@ export function initGateway(options: InitGatewayOptions = {}): void {
369
291
  setWriterFaultsDeliverable(writerB === "in-process");
370
292
  }
371
293
 
372
- /** Tear the gateway down (both pools). After this, the next `getGateway()` lazily re-inits in-process. */
294
+ /** Tear the gateway down (both pools). The next getGateway() selects placement from the current database. */
373
295
  export async function shutdownGateway(): Promise<void> {
374
296
  const previousReader = reader;
375
297
  reader = undefined;
@@ -381,17 +303,7 @@ export async function shutdownGateway(): Promise<void> {
381
303
  await previousReader?.destroy();
382
304
  }
383
305
 
384
- /**
385
- * Tear down JUST the reader half — terminates any worker-pool threads and replaces them with a fresh
386
- * in-process reader. The writer is left COMPLETELY untouched (#1960 T2 server teardown).
387
- *
388
- * Deliberately NOT `shutdownGateway()`: that also calls `shutdownWriter()`, which unconditionally
389
- * REJECTS every write still queued at the writer (writer-gateway.ts) — a needless side effect on a
390
- * clean stop, since Phase-R never puts the writer on a worker (it stays in-process always). This is
391
- * the shutdown counterpart the design pass calls for (§1.5): "worker threads must be torn down on
392
- * shutdown or a restart can leave orphaned threads holding WAL read marks." Safe to call even when the
393
- * reader was never worker-backed — the in-process reader's `destroy()` resolves immediately.
394
- */
306
+ /** Release reader workers without rejecting the writer FIFO during graceful shutdown. */
395
307
  export async function shutdownGatewayReader(): Promise<void> {
396
308
  const previous = reader;
397
309
  reader = createReaderGateway({ backend: "in-process" });
@@ -404,16 +316,7 @@ export function isGatewayInitialized(): boolean {
404
316
  return reader !== undefined;
405
317
  }
406
318
 
407
- /**
408
- * The live WRITER backend. Domains whose post-commit main-thread work is not yet worker-safe (e.g.
409
- * getDb-backed effect delivery) read this to refuse rather than commit-then-silently-drop.
410
- *
411
- * DELIBERATELY THE WRITER HALF, not "either half" (#1960 T1). Every caller is really asking "is the
412
- * main RW connection still ours?" — `events.ts`'s keyless-append guard and `managed-policy.ts`'s
413
- * worker-safe builder both branch on that, and it is the WRITER flip that surrenders the connection.
414
- * A worker-backed READER changes nothing for them, so they must not start refusing when reads move off
415
- * the loop. Use `readerBackend()` for the read half.
416
- */
319
+ /** The live writer placement, retained as the historical backend alias. */
417
320
  export function gatewayBackend(): GatewayBackend {
418
321
  return currentWriterBackend;
419
322
  }
@@ -429,23 +332,13 @@ export function writerBackend(): GatewayBackend {
429
332
  }
430
333
 
431
334
  function ensureInit(): void {
432
- if (reader === undefined) {
433
- // Cold gateway: stand BOTH halves up (in-process by default).
335
+ if (reader === undefined || (getDbPath() !== undefined && gatewayDbGeneration !== getDbGeneration())) {
336
+ // A replaced database owns new pools; never send its writes to the previous file (#1870 S8).
434
337
  initGateway();
435
338
  return;
436
339
  }
437
- // The reader and writer are normally stood up AND torn down together by
438
- // initGateway()/shutdownGateway(), but the writer CAN be torn down on its own (`shutdownWriter()` —
439
- // used by the writer suite's afterEach and reachable directly), leaving `reader` defined while the
440
- // writer backend is undefined. A reader-only sentinel then skips re-init and the next write hits
441
- // `requireBackend()` → "writer gateway not initialized" — the failure L1 (#1875) surfaced once feature
442
- // writes began routing through `getGateway().write/coalesce(...)`. Restore JUST the writer, matching
443
- // the live backend, so a lone shutdownWriter() self-heals WITHOUT disturbing the reader (never
444
- // downgrading a worker-backed reader to in-process).
340
+ // A lone writer teardown preserves reader placement. Rebuild only that writer.
445
341
  if (!isWriterInitialized()) {
446
- // Reconstruct the writer against the RETAINED WRITER worker path (never getDbPath(), which closeDb()
447
- // clears at worker-writer bootstrap — HIGH-1). For in-process, ":memory:" selects the in-process
448
- // writer. Keyed on the WRITER half so a worker READER never drags the writer onto a worker.
449
342
  initWriter({ path: currentWriterBackend === "worker" ? writerWorkerDbPath : ":memory:" });
450
343
  }
451
344
  }
@@ -503,8 +396,8 @@ const GATEWAY: DbGateway = {
503
396
  };
504
397
 
505
398
  /**
506
- * The process-wide DB gateway — the sole sanctioned DB entry point (ADR §3.3). Lazily stands up the
507
- * in-process backend on first use if `initGateway()` was not called explicitly, so a caller never sees
399
+ * The process-wide DB gateway — the sole sanctioned DB entry point (ADR §3.3). Lazily selects pools for the
400
+ * current database on first use if `initGateway()` was not called explicitly, so a caller never sees
508
401
  * an uninitialized gateway. The returned facade is stable across `initGateway()` re-inits.
509
402
  */
510
403
  export function getGateway(): DbGateway {