@openparachute/agent 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (605) hide show
  1. package/.parachute/module.json +124 -8
  2. package/LICENSE +2 -16
  3. package/README.md +118 -166
  4. package/package.json +32 -43
  5. package/scripts/spawn-agent.ts +371 -0
  6. package/src/_parked/interactive-spawn.test.ts +324 -0
  7. package/src/_parked/interactive-spawn.ts +701 -0
  8. package/src/agent-defs.test.ts +1504 -0
  9. package/src/agent-defs.ts +1702 -0
  10. package/src/agent-mcp-config.test.ts +115 -0
  11. package/src/agent-mcp-config.ts +115 -0
  12. package/src/agents.test.ts +360 -0
  13. package/src/agents.ts +379 -0
  14. package/src/auth.test.ts +46 -0
  15. package/src/auth.ts +140 -0
  16. package/src/backends/attached-queue.test.ts +376 -0
  17. package/src/backends/attached-queue.ts +372 -0
  18. package/src/backends/programmatic.test.ts +1715 -0
  19. package/src/backends/programmatic.ts +927 -0
  20. package/src/backends/registry.test.ts +1494 -0
  21. package/src/backends/registry.ts +1202 -0
  22. package/src/backends/stream-json.test.ts +570 -0
  23. package/src/backends/stream-json.ts +392 -0
  24. package/src/backends/types.ts +223 -0
  25. package/src/bridge.ts +417 -0
  26. package/src/channel-backend-wiring.test.ts +237 -0
  27. package/src/credentials.test.ts +274 -0
  28. package/src/credentials.ts +380 -0
  29. package/src/cron.test.ts +342 -0
  30. package/src/cron.ts +380 -0
  31. package/src/daemon-agent-def-api.test.ts +166 -0
  32. package/src/daemon-agent-defs-api.test.ts +953 -0
  33. package/src/daemon-agent-env-api.test.ts +338 -0
  34. package/src/daemon-attached-queue-store.test.ts +65 -0
  35. package/src/daemon-config-api.test.ts +962 -0
  36. package/src/daemon-jobs-api.test.ts +271 -0
  37. package/src/daemon-vault-chat.test.ts +250 -0
  38. package/src/daemon.test.ts +746 -0
  39. package/src/daemon.ts +3314 -0
  40. package/src/def-vaults.test.ts +136 -0
  41. package/src/def-vaults.ts +165 -0
  42. package/src/delivery-state.test.ts +110 -0
  43. package/src/delivery-state.ts +154 -0
  44. package/src/effective-env.test.ts +114 -0
  45. package/src/effective-env.ts +184 -0
  46. package/src/env-compat.ts +39 -0
  47. package/src/grants.test.ts +638 -0
  48. package/src/grants.ts +675 -0
  49. package/src/hub-jwt.test.ts +161 -0
  50. package/src/hub-jwt.ts +182 -0
  51. package/src/jobs.test.ts +245 -0
  52. package/src/jobs.ts +266 -0
  53. package/src/mcp-http.test.ts +265 -0
  54. package/src/mcp-http.ts +771 -0
  55. package/src/mint-token.test.ts +152 -0
  56. package/src/mint-token.ts +139 -0
  57. package/src/module-manifest.test.ts +158 -0
  58. package/src/oauth-discovery.ts +134 -0
  59. package/src/programmatic-wiring.test.ts +838 -0
  60. package/src/registry.test.ts +227 -0
  61. package/src/registry.ts +228 -0
  62. package/src/resolve-port.test.ts +64 -0
  63. package/src/routing.test.ts +184 -0
  64. package/src/routing.ts +76 -0
  65. package/src/runner.test.ts +506 -0
  66. package/src/runner.ts +255 -0
  67. package/src/sandbox/config.test.ts +150 -0
  68. package/src/sandbox/config.ts +102 -0
  69. package/src/sandbox/egress.test.ts +113 -0
  70. package/src/sandbox/egress.ts +123 -0
  71. package/src/sandbox/index.ts +180 -0
  72. package/src/sandbox/live-seatbelt.test.ts +277 -0
  73. package/src/sandbox/mounts.test.ts +154 -0
  74. package/src/sandbox/mounts.ts +133 -0
  75. package/src/sandbox/sandbox.test.ts +168 -0
  76. package/src/sandbox/types.ts +382 -0
  77. package/src/services-manifest.test.ts +106 -0
  78. package/src/services-manifest.ts +95 -0
  79. package/src/spa-serve.test.ts +116 -0
  80. package/src/spa-serve.ts +116 -0
  81. package/src/spawn-agent-cli.test.ts +172 -0
  82. package/src/spawn-agent.test.ts +1218 -0
  83. package/src/spawn-agent.ts +569 -0
  84. package/src/spawn-deps.test.ts +54 -0
  85. package/src/spawn-deps.ts +166 -0
  86. package/src/telegram/api.ts +153 -0
  87. package/src/terminal-assets.test.ts +50 -0
  88. package/src/terminal-assets.ts +79 -0
  89. package/src/terminal-ui.ts +305 -0
  90. package/src/terminal.test.ts +530 -0
  91. package/src/terminal.ts +458 -0
  92. package/src/transport.ts +270 -0
  93. package/src/transports/http-ui.test.ts +455 -0
  94. package/src/transports/http-ui.ts +201 -0
  95. package/src/transports/telegram.test.ts +174 -0
  96. package/src/transports/telegram.ts +426 -0
  97. package/src/transports/vault.test.ts +2011 -0
  98. package/src/transports/vault.ts +1790 -0
  99. package/src/ui-kit.test.ts +178 -0
  100. package/src/ui-kit.ts +402 -0
  101. package/tsconfig.json +8 -14
  102. package/web/ui/tsconfig.json +2 -1
  103. package/.claude/scheduled_tasks.lock +0 -1
  104. package/.claude/settings.json +0 -5
  105. package/.claude/skills/add-atomic-chat-tool/SKILL.md +0 -243
  106. package/.claude/skills/add-atomic-chat-tool/atomic-chat-mcp-stdio.ts +0 -229
  107. package/.claude/skills/add-codex/SKILL.md +0 -161
  108. package/.claude/skills/add-dashboard/SKILL.md +0 -138
  109. package/.claude/skills/add-dashboard/resources/dashboard-pusher.ts +0 -495
  110. package/.claude/skills/add-emacs/SKILL.md +0 -296
  111. package/.claude/skills/add-gcal-tool/SKILL.md +0 -210
  112. package/.claude/skills/add-gchat/REMOVE.md +0 -6
  113. package/.claude/skills/add-gchat/SKILL.md +0 -92
  114. package/.claude/skills/add-gchat/VERIFY.md +0 -3
  115. package/.claude/skills/add-github/REMOVE.md +0 -6
  116. package/.claude/skills/add-github/SKILL.md +0 -148
  117. package/.claude/skills/add-github/VERIFY.md +0 -3
  118. package/.claude/skills/add-gmail-tool/SKILL.md +0 -229
  119. package/.claude/skills/add-imessage/REMOVE.md +0 -6
  120. package/.claude/skills/add-imessage/SKILL.md +0 -113
  121. package/.claude/skills/add-imessage/VERIFY.md +0 -3
  122. package/.claude/skills/add-karpathy-llm-wiki/SKILL.md +0 -110
  123. package/.claude/skills/add-karpathy-llm-wiki/llm-wiki.md +0 -75
  124. package/.claude/skills/add-linear/REMOVE.md +0 -6
  125. package/.claude/skills/add-linear/SKILL.md +0 -168
  126. package/.claude/skills/add-linear/VERIFY.md +0 -3
  127. package/.claude/skills/add-macos-statusbar/SKILL.md +0 -133
  128. package/.claude/skills/add-macos-statusbar/add/src/statusbar.swift +0 -147
  129. package/.claude/skills/add-matrix/REMOVE.md +0 -6
  130. package/.claude/skills/add-matrix/SKILL.md +0 -148
  131. package/.claude/skills/add-matrix/VERIFY.md +0 -3
  132. package/.claude/skills/add-ollama-provider/SKILL.md +0 -179
  133. package/.claude/skills/add-ollama-tool/SKILL.md +0 -193
  134. package/.claude/skills/add-opencode/SKILL.md +0 -229
  135. package/.claude/skills/add-parallel/SKILL.md +0 -290
  136. package/.claude/skills/add-resend/REMOVE.md +0 -6
  137. package/.claude/skills/add-resend/SKILL.md +0 -93
  138. package/.claude/skills/add-resend/VERIFY.md +0 -3
  139. package/.claude/skills/add-signal/REMOVE.md +0 -13
  140. package/.claude/skills/add-signal/SKILL.md +0 -318
  141. package/.claude/skills/add-signal/VERIFY.md +0 -5
  142. package/.claude/skills/add-slack/REMOVE.md +0 -6
  143. package/.claude/skills/add-slack/SKILL.md +0 -112
  144. package/.claude/skills/add-slack/VERIFY.md +0 -3
  145. package/.claude/skills/add-teams/REMOVE.md +0 -6
  146. package/.claude/skills/add-teams/SKILL.md +0 -207
  147. package/.claude/skills/add-teams/VERIFY.md +0 -3
  148. package/.claude/skills/add-vercel/SKILL.md +0 -147
  149. package/.claude/skills/add-vercel/container-skills/vercel-cli/SKILL.md +0 -103
  150. package/.claude/skills/add-webex/REMOVE.md +0 -6
  151. package/.claude/skills/add-webex/SKILL.md +0 -88
  152. package/.claude/skills/add-webex/VERIFY.md +0 -3
  153. package/.claude/skills/add-wechat/REMOVE.md +0 -49
  154. package/.claude/skills/add-wechat/SKILL.md +0 -170
  155. package/.claude/skills/add-wechat/scripts/wire-dm.ts +0 -172
  156. package/.claude/skills/add-whatsapp/SKILL.md +0 -264
  157. package/.claude/skills/add-whatsapp-cloud/REMOVE.md +0 -6
  158. package/.claude/skills/add-whatsapp-cloud/SKILL.md +0 -95
  159. package/.claude/skills/add-whatsapp-cloud/VERIFY.md +0 -3
  160. package/.claude/skills/claw/SKILL.md +0 -131
  161. package/.claude/skills/claw/scripts/claw +0 -374
  162. package/.claude/skills/convert-to-apple-container/SKILL.md +0 -212
  163. package/.claude/skills/customize/SKILL.md +0 -110
  164. package/.claude/skills/debug/SKILL.md +0 -349
  165. package/.claude/skills/get-qodo-rules/SKILL.md +0 -122
  166. package/.claude/skills/get-qodo-rules/references/output-format.md +0 -41
  167. package/.claude/skills/get-qodo-rules/references/pagination.md +0 -33
  168. package/.claude/skills/get-qodo-rules/references/repository-scope.md +0 -26
  169. package/.claude/skills/init-first-agent/SKILL.md +0 -120
  170. package/.claude/skills/init-onecli/SKILL.md +0 -270
  171. package/.claude/skills/manage-channels/SKILL.md +0 -87
  172. package/.claude/skills/manage-mounts/SKILL.md +0 -47
  173. package/.claude/skills/migrate-from-openclaw/MIGRATE_CRONS.md +0 -100
  174. package/.claude/skills/migrate-from-openclaw/SKILL.md +0 -447
  175. package/.claude/skills/migrate-from-openclaw/scripts/discover-openclaw.ts +0 -734
  176. package/.claude/skills/migrate-from-openclaw/scripts/extract-channel-credentials.ts +0 -476
  177. package/.claude/skills/migrate-nanoclaw/SKILL.md +0 -484
  178. package/.claude/skills/migrate-nanoclaw/diagnostics.md +0 -51
  179. package/.claude/skills/qodo-pr-resolver/SKILL.md +0 -326
  180. package/.claude/skills/qodo-pr-resolver/resources/providers.md +0 -329
  181. package/.claude/skills/update-nanoclaw/SKILL.md +0 -243
  182. package/.claude/skills/update-nanoclaw/diagnostics.md +0 -48
  183. package/.claude/skills/update-skills/SKILL.md +0 -130
  184. package/.claude/skills/use-native-credential-proxy/SKILL.md +0 -167
  185. package/.claude/skills/x-integration/SKILL.md +0 -417
  186. package/.claude/skills/x-integration/agent.ts +0 -243
  187. package/.claude/skills/x-integration/host.ts +0 -155
  188. package/.claude/skills/x-integration/lib/browser.ts +0 -148
  189. package/.claude/skills/x-integration/lib/config.ts +0 -62
  190. package/.claude/skills/x-integration/scripts/like.ts +0 -56
  191. package/.claude/skills/x-integration/scripts/post.ts +0 -66
  192. package/.claude/skills/x-integration/scripts/quote.ts +0 -80
  193. package/.claude/skills/x-integration/scripts/reply.ts +0 -74
  194. package/.claude/skills/x-integration/scripts/retweet.ts +0 -62
  195. package/.claude/skills/x-integration/scripts/setup.ts +0 -87
  196. package/.github/CODEOWNERS +0 -10
  197. package/.github/PULL_REQUEST_TEMPLATE.md +0 -18
  198. package/.github/workflows/bump-version.yml +0 -35
  199. package/.github/workflows/ci.yml +0 -39
  200. package/.github/workflows/label-pr.yml +0 -40
  201. package/.github/workflows/update-tokens.yml +0 -43
  202. package/.husky/pre-commit +0 -1
  203. package/.mcp.json +0 -3
  204. package/.nvmrc +0 -1
  205. package/.prettierrc +0 -4
  206. package/CHANGELOG.md +0 -263
  207. package/CLAUDE.md +0 -307
  208. package/CODE_OF_CONDUCT.md +0 -128
  209. package/CONTRIBUTING.md +0 -159
  210. package/CONTRIBUTORS.md +0 -26
  211. package/LICENSE-NANOCLAW-MIT +0 -21
  212. package/README_ja.md +0 -194
  213. package/README_zh.md +0 -194
  214. package/assets/nanoclaw-favicon.png +0 -0
  215. package/assets/nanoclaw-icon.png +0 -0
  216. package/assets/nanoclaw-logo-dark.png +0 -0
  217. package/assets/nanoclaw-logo.png +0 -0
  218. package/assets/nanoclaw-profile.jpeg +0 -0
  219. package/assets/nanoclaw-sales.png +0 -0
  220. package/assets/social-preview.jpg +0 -0
  221. package/config-examples/mount-allowlist.json +0 -25
  222. package/container/.dockerignore +0 -2
  223. package/container/CLAUDE.md +0 -21
  224. package/container/Dockerfile +0 -121
  225. package/container/agent-runner/bun.lock +0 -243
  226. package/container/agent-runner/package.json +0 -22
  227. package/container/agent-runner/scripts/sdk-signal-probe.ts +0 -169
  228. package/container/agent-runner/src/config.ts +0 -55
  229. package/container/agent-runner/src/db/connection.ts +0 -267
  230. package/container/agent-runner/src/db/index.ts +0 -20
  231. package/container/agent-runner/src/db/messages-in.ts +0 -138
  232. package/container/agent-runner/src/db/messages-out.ts +0 -143
  233. package/container/agent-runner/src/db/session-routing.ts +0 -30
  234. package/container/agent-runner/src/db/session-state.test.ts +0 -100
  235. package/container/agent-runner/src/db/session-state.ts +0 -79
  236. package/container/agent-runner/src/destinations.ts +0 -135
  237. package/container/agent-runner/src/formatter.test.ts +0 -167
  238. package/container/agent-runner/src/formatter.ts +0 -260
  239. package/container/agent-runner/src/index.ts +0 -110
  240. package/container/agent-runner/src/integration.test.ts +0 -121
  241. package/container/agent-runner/src/mcp-tools/agents.instructions.md +0 -26
  242. package/container/agent-runner/src/mcp-tools/agents.ts +0 -66
  243. package/container/agent-runner/src/mcp-tools/core.instructions.md +0 -27
  244. package/container/agent-runner/src/mcp-tools/core.ts +0 -262
  245. package/container/agent-runner/src/mcp-tools/index.ts +0 -22
  246. package/container/agent-runner/src/mcp-tools/interactive.instructions.md +0 -22
  247. package/container/agent-runner/src/mcp-tools/interactive.ts +0 -169
  248. package/container/agent-runner/src/mcp-tools/scheduling.instructions.md +0 -40
  249. package/container/agent-runner/src/mcp-tools/scheduling.ts +0 -299
  250. package/container/agent-runner/src/mcp-tools/self-mod.instructions.md +0 -25
  251. package/container/agent-runner/src/mcp-tools/self-mod.ts +0 -120
  252. package/container/agent-runner/src/mcp-tools/server.ts +0 -54
  253. package/container/agent-runner/src/mcp-tools/types.ts +0 -6
  254. package/container/agent-runner/src/poll-loop.test.ts +0 -248
  255. package/container/agent-runner/src/poll-loop.ts +0 -437
  256. package/container/agent-runner/src/providers/claude.ts +0 -379
  257. package/container/agent-runner/src/providers/factory.test.ts +0 -19
  258. package/container/agent-runner/src/providers/factory.ts +0 -13
  259. package/container/agent-runner/src/providers/index.ts +0 -6
  260. package/container/agent-runner/src/providers/mock.ts +0 -77
  261. package/container/agent-runner/src/providers/provider-registry.ts +0 -33
  262. package/container/agent-runner/src/providers/types.ts +0 -82
  263. package/container/agent-runner/src/scheduling/task-script.ts +0 -121
  264. package/container/agent-runner/src/timezone.test.ts +0 -93
  265. package/container/agent-runner/src/timezone.ts +0 -107
  266. package/container/agent-runner/tsconfig.json +0 -14
  267. package/container/build.sh +0 -48
  268. package/container/entrypoint.sh +0 -16
  269. package/container/skills/agent-browser/SKILL.md +0 -159
  270. package/container/skills/frontend-engineer/SKILL.md +0 -157
  271. package/container/skills/self-customize/SKILL.md +0 -87
  272. package/container/skills/slack-formatting/SKILL.md +0 -94
  273. package/container/skills/vercel-cli/SKILL.md +0 -111
  274. package/container/skills/welcome/SKILL.md +0 -85
  275. package/docs/APPLE-CONTAINER-NETWORKING.md +0 -90
  276. package/docs/BRANCH-FORK-MAINTENANCE.md +0 -81
  277. package/docs/README.md +0 -25
  278. package/docs/SDK_DEEP_DIVE.md +0 -643
  279. package/docs/SECURITY.md +0 -162
  280. package/docs/agent-runner-details.md +0 -749
  281. package/docs/api-details.md +0 -365
  282. package/docs/architecture-diagram.html +0 -422
  283. package/docs/architecture-diagram.md +0 -215
  284. package/docs/architecture.md +0 -751
  285. package/docs/audit/2026-04-30-channel-endpoint-audit.md +0 -36
  286. package/docs/build-and-runtime.md +0 -80
  287. package/docs/cross-mount-stress/README.md +0 -112
  288. package/docs/cross-mount-stress/container-writer-retry.mjs +0 -55
  289. package/docs/cross-mount-stress/container-writer-slow.mjs +0 -42
  290. package/docs/cross-mount-stress/container-writer.mjs +0 -47
  291. package/docs/cross-mount-stress/host-writer-retry.mjs +0 -55
  292. package/docs/cross-mount-stress/host-writer-slow.mjs +0 -43
  293. package/docs/cross-mount-stress/host-writer.mjs +0 -47
  294. package/docs/db-central.md +0 -316
  295. package/docs/db-session.md +0 -183
  296. package/docs/db.md +0 -119
  297. package/docs/design/2026-04-29-vault-management-ui.md +0 -231
  298. package/docs/design/2026-04-30-channel-wiring-rework.md +0 -234
  299. package/docs/design/2026-05-01-channel-wiring-approvals-deep-dive.md +0 -272
  300. package/docs/design/2026-05-02-channel-policy-and-approval-routing.md +0 -250
  301. package/docs/docker-sandboxes.md +0 -359
  302. package/docs/isolation-model.md +0 -88
  303. package/docs/ollama.md +0 -79
  304. package/docs/parachute-integration.md +0 -109
  305. package/docs/post-night-rebirth-reflections.md +0 -151
  306. package/eslint.config.js +0 -32
  307. package/pnpm-workspace.yaml +0 -8
  308. package/repo-tokens/README.md +0 -113
  309. package/repo-tokens/action.yml +0 -186
  310. package/repo-tokens/badge.svg +0 -23
  311. package/repo-tokens/examples/green.svg +0 -14
  312. package/repo-tokens/examples/red.svg +0 -14
  313. package/repo-tokens/examples/yellow-green.svg +0 -14
  314. package/repo-tokens/examples/yellow.svg +0 -14
  315. package/scripts/chat.ts +0 -101
  316. package/scripts/cleanup-sessions.sh +0 -150
  317. package/scripts/init-cli-agent.ts +0 -172
  318. package/scripts/init-first-agent.ts +0 -378
  319. package/scripts/parachute.ts +0 -158
  320. package/scripts/run-migrations.ts +0 -105
  321. package/scripts/sanity-live-poll.ts +0 -95
  322. package/scripts/seed-discord.ts +0 -80
  323. package/scripts/test-v2-agent.ts +0 -106
  324. package/scripts/test-v2-channel-e2e.ts +0 -265
  325. package/scripts/test-v2-host.ts +0 -184
  326. package/src/channels/adapter.ts +0 -214
  327. package/src/channels/api-translator.test.ts +0 -306
  328. package/src/channels/api-translator.ts +0 -214
  329. package/src/channels/ask-question.ts +0 -46
  330. package/src/channels/channel-registry.test.ts +0 -421
  331. package/src/channels/channel-registry.ts +0 -313
  332. package/src/channels/chat-sdk-bridge.test.ts +0 -84
  333. package/src/channels/chat-sdk-bridge.ts +0 -652
  334. package/src/channels/cli.ts +0 -276
  335. package/src/channels/discord.ts +0 -90
  336. package/src/channels/index.ts +0 -17
  337. package/src/channels/telegram-markdown-sanitize.test.ts +0 -78
  338. package/src/channels/telegram-markdown-sanitize.ts +0 -55
  339. package/src/channels/telegram-pairing.test.ts +0 -254
  340. package/src/channels/telegram-pairing.ts +0 -339
  341. package/src/channels/telegram.ts +0 -279
  342. package/src/channels/trust-hint.test.ts +0 -48
  343. package/src/channels/trust-hint.ts +0 -75
  344. package/src/claude-md-compose.migrate.test.ts +0 -64
  345. package/src/claude-md-compose.ts +0 -205
  346. package/src/command-gate.ts +0 -63
  347. package/src/config.test.ts +0 -93
  348. package/src/config.ts +0 -128
  349. package/src/container-config.ts +0 -167
  350. package/src/container-runner.test.ts +0 -32
  351. package/src/container-runner.ts +0 -576
  352. package/src/container-runtime.test.ts +0 -269
  353. package/src/container-runtime.ts +0 -167
  354. package/src/db/_bun-sqlite-shim.ts +0 -88
  355. package/src/db/agent-activity.test.ts +0 -155
  356. package/src/db/agent-activity.ts +0 -121
  357. package/src/db/agent-groups.ts +0 -77
  358. package/src/db/connection.migrate.test.ts +0 -176
  359. package/src/db/connection.ts +0 -259
  360. package/src/db/db-v2.test.ts +0 -440
  361. package/src/db/dropped-messages.ts +0 -44
  362. package/src/db/index.ts +0 -40
  363. package/src/db/messaging-groups.ts +0 -252
  364. package/src/db/migrations/001-initial.ts +0 -112
  365. package/src/db/migrations/002-chat-sdk-state.ts +0 -36
  366. package/src/db/migrations/008-dropped-messages.ts +0 -27
  367. package/src/db/migrations/009-drop-pending-credentials.ts +0 -13
  368. package/src/db/migrations/010-engage-modes.ts +0 -103
  369. package/src/db/migrations/011-pending-sender-approvals.ts +0 -40
  370. package/src/db/migrations/012-channel-registration.ts +0 -48
  371. package/src/db/migrations/013-approval-render-metadata.ts +0 -27
  372. package/src/db/migrations/014-secrets.ts +0 -44
  373. package/src/db/migrations/015-secrets-drop-host-pattern.ts +0 -18
  374. package/src/db/migrations/016-secret-assignments.ts +0 -30
  375. package/src/db/migrations/017-agent-activity.ts +0 -40
  376. package/src/db/migrations/018-oauth-app-configs.ts +0 -34
  377. package/src/db/migrations/019-oauth-app-connections.ts +0 -48
  378. package/src/db/migrations/020-agent-app-connections.ts +0 -28
  379. package/src/db/migrations/021-pending-oauth-states.ts +0 -35
  380. package/src/db/migrations/022-app-connections-provider.ts +0 -25
  381. package/src/db/migrations/023-agent-group-secret-mode.test.ts +0 -124
  382. package/src/db/migrations/023-agent-group-secret-mode.ts +0 -65
  383. package/src/db/migrations/024-collapse-approvals.test.ts +0 -249
  384. package/src/db/migrations/024-collapse-approvals.ts +0 -182
  385. package/src/db/migrations/025-secret-mode-check.test.ts +0 -155
  386. package/src/db/migrations/025-secret-mode-check.ts +0 -49
  387. package/src/db/migrations/026-user-dms-bot-id.test.ts +0 -116
  388. package/src/db/migrations/026-user-dms-bot-id.ts +0 -54
  389. package/src/db/migrations/027-provider-credentials.ts +0 -41
  390. package/src/db/migrations/_test-helpers.ts +0 -41
  391. package/src/db/migrations/index.ts +0 -127
  392. package/src/db/migrations/module-agent-to-agent-destinations.ts +0 -84
  393. package/src/db/migrations/module-approvals-pending-approvals.ts +0 -42
  394. package/src/db/migrations/module-approvals-title-options.ts +0 -40
  395. package/src/db/schema.ts +0 -258
  396. package/src/db/session-db.test.ts +0 -93
  397. package/src/db/session-db.ts +0 -325
  398. package/src/db/sessions.ts +0 -241
  399. package/src/delivery.test.ts +0 -148
  400. package/src/delivery.ts +0 -445
  401. package/src/env.ts +0 -74
  402. package/src/group-folder.test.ts +0 -35
  403. package/src/group-folder.ts +0 -44
  404. package/src/group-init.ts +0 -92
  405. package/src/host-core.test.ts +0 -456
  406. package/src/host-sweep.test.ts +0 -146
  407. package/src/host-sweep.ts +0 -287
  408. package/src/index.ts +0 -232
  409. package/src/install-slug.ts +0 -33
  410. package/src/log.test.ts +0 -81
  411. package/src/log.ts +0 -117
  412. package/src/mcp/http.ts +0 -72
  413. package/src/mcp/server.ts +0 -92
  414. package/src/mcp/stdio.ts +0 -51
  415. package/src/mcp/tools/activity.ts +0 -88
  416. package/src/mcp/tools/agent-groups.ts +0 -183
  417. package/src/mcp/tools/approvals.ts +0 -122
  418. package/src/mcp/tools/channels.test.ts +0 -126
  419. package/src/mcp/tools/channels.ts +0 -134
  420. package/src/mcp/tools/index.ts +0 -27
  421. package/src/mcp/tools/oauth.ts +0 -48
  422. package/src/mcp/tools/secrets.ts +0 -169
  423. package/src/mcp/tools/sessions.ts +0 -135
  424. package/src/mcp/types.ts +0 -51
  425. package/src/modules/agent-to-agent/agent-route.test.ts +0 -46
  426. package/src/modules/agent-to-agent/agent-route.ts +0 -223
  427. package/src/modules/agent-to-agent/create-agent.ts +0 -127
  428. package/src/modules/agent-to-agent/db/agent-destinations.ts +0 -135
  429. package/src/modules/agent-to-agent/index.ts +0 -22
  430. package/src/modules/agent-to-agent/write-destinations.ts +0 -59
  431. package/src/modules/approvals/agent.md +0 -45
  432. package/src/modules/approvals/index.ts +0 -21
  433. package/src/modules/approvals/picks.test.ts +0 -291
  434. package/src/modules/approvals/primitive.ts +0 -279
  435. package/src/modules/approvals/project.md +0 -27
  436. package/src/modules/approvals/response-handler.ts +0 -87
  437. package/src/modules/index.ts +0 -24
  438. package/src/modules/interactive/agent.md +0 -21
  439. package/src/modules/interactive/index.ts +0 -69
  440. package/src/modules/interactive/project.md +0 -12
  441. package/src/modules/mount-security/expand-path.test.ts +0 -82
  442. package/src/modules/mount-security/index.ts +0 -459
  443. package/src/modules/mount-security/migrate.test.ts +0 -91
  444. package/src/modules/permissions/access.ts +0 -28
  445. package/src/modules/permissions/channel-approval.test.ts +0 -389
  446. package/src/modules/permissions/channel-approval.ts +0 -188
  447. package/src/modules/permissions/db/agent-group-members.ts +0 -44
  448. package/src/modules/permissions/db/pending-channel-approvals.test.ts +0 -86
  449. package/src/modules/permissions/db/pending-channel-approvals.ts +0 -66
  450. package/src/modules/permissions/db/pending-sender-approvals.ts +0 -60
  451. package/src/modules/permissions/db/user-dms.ts +0 -58
  452. package/src/modules/permissions/db/user-roles.ts +0 -85
  453. package/src/modules/permissions/db/users.ts +0 -38
  454. package/src/modules/permissions/index.ts +0 -421
  455. package/src/modules/permissions/permissions.test.ts +0 -358
  456. package/src/modules/permissions/sender-approval.test.ts +0 -641
  457. package/src/modules/permissions/sender-approval.ts +0 -165
  458. package/src/modules/permissions/user-dm.ts +0 -200
  459. package/src/modules/provider-credentials/db.ts +0 -121
  460. package/src/modules/provider-credentials/index.ts +0 -12
  461. package/src/modules/provider-credentials/spawn.test.ts +0 -206
  462. package/src/modules/provider-credentials/spawn.ts +0 -114
  463. package/src/modules/scheduling/actions.ts +0 -113
  464. package/src/modules/scheduling/db.test.ts +0 -282
  465. package/src/modules/scheduling/db.ts +0 -148
  466. package/src/modules/scheduling/index.ts +0 -34
  467. package/src/modules/scheduling/recurrence.test.ts +0 -98
  468. package/src/modules/scheduling/recurrence.ts +0 -54
  469. package/src/modules/self-mod/agent.md +0 -30
  470. package/src/modules/self-mod/apply.ts +0 -85
  471. package/src/modules/self-mod/index.ts +0 -30
  472. package/src/modules/self-mod/project.md +0 -39
  473. package/src/modules/self-mod/request.ts +0 -91
  474. package/src/modules/typing/index.ts +0 -165
  475. package/src/oauth/agent-app-connections.ts +0 -103
  476. package/src/oauth/app-configs.test.ts +0 -64
  477. package/src/oauth/app-configs.ts +0 -114
  478. package/src/oauth/app-connections.test.ts +0 -109
  479. package/src/oauth/app-connections.ts +0 -178
  480. package/src/oauth/crypto.ts +0 -56
  481. package/src/oauth/flow.ts +0 -104
  482. package/src/oauth/providers/google.test.ts +0 -38
  483. package/src/oauth/providers/google.ts +0 -46
  484. package/src/oauth/providers/index.ts +0 -48
  485. package/src/oauth/state-store.test.ts +0 -54
  486. package/src/oauth/state-store.ts +0 -93
  487. package/src/parachute/README.md +0 -27
  488. package/src/parachute/create-agent.test.ts +0 -83
  489. package/src/parachute/create-agent.ts +0 -122
  490. package/src/parachute/group-status.test.ts +0 -165
  491. package/src/parachute/group-status.ts +0 -136
  492. package/src/parachute/types.ts +0 -41
  493. package/src/parachute/vault-mcp.test.ts +0 -251
  494. package/src/parachute/vault-mcp.ts +0 -232
  495. package/src/platform-id.test.ts +0 -104
  496. package/src/platform-id.ts +0 -109
  497. package/src/providers/index.ts +0 -6
  498. package/src/providers/provider-container-registry.ts +0 -58
  499. package/src/response-registry.ts +0 -45
  500. package/src/router.ts +0 -530
  501. package/src/secrets/crypto.test.ts +0 -45
  502. package/src/secrets/crypto.ts +0 -55
  503. package/src/secrets/index.ts +0 -461
  504. package/src/secrets/master-key.ts +0 -70
  505. package/src/secrets/secrets.test.ts +0 -651
  506. package/src/session-manager.attachments.test.ts +0 -171
  507. package/src/session-manager.dup-skip.test.ts +0 -173
  508. package/src/session-manager.migrate.test.ts +0 -59
  509. package/src/session-manager.ts +0 -451
  510. package/src/startup-bootstrap.test.ts +0 -226
  511. package/src/startup-bootstrap.ts +0 -207
  512. package/src/state-sqlite.ts +0 -182
  513. package/src/timezone.test.ts +0 -64
  514. package/src/timezone.ts +0 -37
  515. package/src/types.ts +0 -233
  516. package/src/web/auth.test.ts +0 -335
  517. package/src/web/auth.ts +0 -214
  518. package/src/web/discord-validate.test.ts +0 -77
  519. package/src/web/discord-validate.ts +0 -88
  520. package/src/web/hub-discovery.test.ts +0 -98
  521. package/src/web/hub-discovery.ts +0 -69
  522. package/src/web/routes/activity.ts +0 -106
  523. package/src/web/routes/agent-provider.test.ts +0 -282
  524. package/src/web/routes/agent-provider.ts +0 -309
  525. package/src/web/routes/approvals.ts +0 -185
  526. package/src/web/routes/apps.ts +0 -434
  527. package/src/web/routes/channels-mg-detail.test.ts +0 -324
  528. package/src/web/routes/channels-mga-detail.test.ts +0 -472
  529. package/src/web/routes/channels.ts +0 -311
  530. package/src/web/routes/oauth-providers.ts +0 -42
  531. package/src/web/routes/secrets.test.ts +0 -220
  532. package/src/web/routes/secrets.ts +0 -317
  533. package/src/web/routes/sessions.ts +0 -123
  534. package/src/web/routes/settings.test.ts +0 -106
  535. package/src/web/routes/settings.ts +0 -247
  536. package/src/web/routes/setup-status.ts +0 -205
  537. package/src/web/routes/vaults.test.ts +0 -389
  538. package/src/web/routes/vaults.ts +0 -225
  539. package/src/web/server-version.test.ts +0 -16
  540. package/src/web/server.ts +0 -1024
  541. package/src/web/services-manifest.test.ts +0 -148
  542. package/src/web/services-manifest.ts +0 -66
  543. package/src/web/static-serve.test.ts +0 -255
  544. package/src/web/static-serve.ts +0 -104
  545. package/src/web/telegram-validate.test.ts +0 -116
  546. package/src/web/telegram-validate.ts +0 -107
  547. package/src/web/vault-proxy.test.ts +0 -214
  548. package/src/web/vault-proxy.ts +0 -120
  549. package/src/web/wire-channel.ts +0 -181
  550. package/src/webhook-server.ts +0 -134
  551. package/vitest.config.ts +0 -18
  552. package/web/README.md +0 -63
  553. package/web/ui/index.html +0 -13
  554. package/web/ui/package.json +0 -35
  555. package/web/ui/pnpm-lock.yaml +0 -2164
  556. package/web/ui/scripts/verify-base.mjs +0 -31
  557. package/web/ui/src/App.tsx +0 -88
  558. package/web/ui/src/components/ActivityFeed.tsx +0 -444
  559. package/web/ui/src/components/AgentGroupPicker.tsx +0 -263
  560. package/web/ui/src/components/AgentProviderCards.tsx +0 -220
  561. package/web/ui/src/components/CredentialForm.tsx +0 -214
  562. package/web/ui/src/components/ScopeGrants.tsx +0 -74
  563. package/web/ui/src/components/StatusDot.tsx +0 -43
  564. package/web/ui/src/components/VaultPicker.tsx +0 -127
  565. package/web/ui/src/components/setup/AdapterInstallStep.tsx +0 -178
  566. package/web/ui/src/components/setup/AgentGroupStep.tsx +0 -43
  567. package/web/ui/src/components/setup/ChannelPickStep.tsx +0 -74
  568. package/web/ui/src/components/setup/DoneStep.tsx +0 -49
  569. package/web/ui/src/components/setup/PrereqStep.tsx +0 -129
  570. package/web/ui/src/components/setup/TestConnectionStep.tsx +0 -108
  571. package/web/ui/src/components/setup/TestMessageStep.tsx +0 -104
  572. package/web/ui/src/components/setup/WireChannelStep.tsx +0 -166
  573. package/web/ui/src/components/setup/types.ts +0 -105
  574. package/web/ui/src/lib/api.test.ts +0 -410
  575. package/web/ui/src/lib/api.ts +0 -1248
  576. package/web/ui/src/lib/auth.test.ts +0 -352
  577. package/web/ui/src/lib/auth.ts +0 -405
  578. package/web/ui/src/lib/channel-adapters.ts +0 -136
  579. package/web/ui/src/main.tsx +0 -19
  580. package/web/ui/src/routes/ApprovalsList.tsx +0 -294
  581. package/web/ui/src/routes/Apps.tsx +0 -613
  582. package/web/ui/src/routes/ChannelWireDetail.test.tsx +0 -233
  583. package/web/ui/src/routes/ChannelWireDetail.tsx +0 -403
  584. package/web/ui/src/routes/ChannelsList.tsx +0 -158
  585. package/web/ui/src/routes/GroupDetail.test.tsx +0 -206
  586. package/web/ui/src/routes/GroupDetail.tsx +0 -880
  587. package/web/ui/src/routes/GroupList.tsx +0 -187
  588. package/web/ui/src/routes/MessagingGroupDetail.test.tsx +0 -233
  589. package/web/ui/src/routes/MessagingGroupDetail.tsx +0 -306
  590. package/web/ui/src/routes/NewGroupWizard.tsx +0 -390
  591. package/web/ui/src/routes/OAuthCallback.tsx +0 -56
  592. package/web/ui/src/routes/SecretsList.tsx +0 -942
  593. package/web/ui/src/routes/SessionsList.tsx +0 -220
  594. package/web/ui/src/routes/SettingsAgentProvider.tsx +0 -109
  595. package/web/ui/src/routes/SettingsApprovals.tsx +0 -234
  596. package/web/ui/src/routes/SetupWizard.tsx +0 -219
  597. package/web/ui/src/routes/VaultDetail.test.tsx +0 -363
  598. package/web/ui/src/routes/VaultDetail.tsx +0 -960
  599. package/web/ui/src/routes/VaultsList.tsx +0 -295
  600. package/web/ui/src/routes/WireChannelPage.tsx +0 -413
  601. package/web/ui/src/styles.css +0 -608
  602. package/web/ui/src/test/setup.ts +0 -23
  603. package/web/ui/src/vite-env.d.ts +0 -10
  604. package/web/ui/vite.config.ts +0 -34
  605. package/web/ui/vitest.config.ts +0 -25
@@ -0,0 +1,1494 @@
1
+ /**
2
+ * Tests for the daemon-level PROGRAMMATIC-AGENT registry + per-channel serial queue
3
+ * (`src/backends/registry.ts`) — the wiring that drives the {@link ProgrammaticBackend}.
4
+ *
5
+ * A FAKE backend (implements {@link AgentBackend}) lets us control each turn's
6
+ * outcome + timing without a real `claude -p`: a deferred-promise gate makes a turn
7
+ * "run" until the test releases it, so the FIFO / never-concurrent invariant is
8
+ * directly observable. The outbound writes go to a recorder array. No tmux, no
9
+ * vault, no hub.
10
+ */
11
+
12
+ import { describe, test, expect } from "bun:test";
13
+ import {
14
+ ProgrammaticAgentRegistry,
15
+ OUTBOUND_MAX_RETRIES,
16
+ PENDING_INBOUND_CAP,
17
+ MAX_DELEGATION_DEPTH,
18
+ isTransientOutboundError,
19
+ type WriteOutbound,
20
+ type WriteThread,
21
+ type WriteCallback,
22
+ type CallbackMeta,
23
+ type ThreadNote,
24
+ type TurnEventSink,
25
+ type TurnLifecycleEvent,
26
+ } from "./registry.ts";
27
+ import type {
28
+ AgentBackend,
29
+ AgentHandle,
30
+ AgentStatus,
31
+ DeliverResult,
32
+ InterimSink,
33
+ TurnSession,
34
+ } from "./types.ts";
35
+ import type { AgentSpec } from "../sandbox/types.ts";
36
+
37
+ /** A deferred promise — resolve it externally to release a gated turn. */
38
+ function deferred<T>(): { promise: Promise<T>; resolve: (v: T) => void } {
39
+ let resolve!: (v: T) => void;
40
+ const promise = new Promise<T>((r) => (resolve = r));
41
+ return { promise, resolve };
42
+ }
43
+
44
+ /**
45
+ * A controllable fake backend. `deliver` records each (channel, message), tracks how
46
+ * many turns are CONCURRENTLY in flight (the serial-queue invariant asserts this
47
+ * never exceeds 1), and resolves with whatever `nextResult` says. When `gate` is set,
48
+ * a turn blocks on it until the test releases it — so we can hold a turn "running"
49
+ * while we enqueue more.
50
+ */
51
+ class FakeBackend implements AgentBackend {
52
+ readonly kind = "programmatic";
53
+ /** Per-call records, in arrival order — including the caller-resolved {@link TurnSession}. */
54
+ readonly calls: { channel: string; message: string; session: TurnSession }[] = [];
55
+ /** Max concurrent in-flight turns observed (must stay ≤ 1 for serial). */
56
+ maxConcurrent = 0;
57
+ private inFlight = 0;
58
+ /** Whether `stop` was called, per channel. */
59
+ readonly stopped = new Set<string>();
60
+ /** A gate the next turn waits on (release to let it finish). Reset per use. */
61
+ gate: { promise: Promise<void>; resolve: () => void } | null = null;
62
+ /**
63
+ * The result function — given the message + the turn's session id, returns the
64
+ * DeliverResult to resolve. The DEFAULT ECHOES `sessionId` (mirroring real claude, which
65
+ * always echoes the `--session-id`/`--resume` id it was handed on a successful turn) so a
66
+ * successful turn's thread note carries the established session — the FIX-2 invariant.
67
+ * An override that omits `sessionId` models a turn that failed BEFORE establishing one.
68
+ */
69
+ resultFor: (message: string, sessionId: string) => DeliverResult = (m, sid) => ({
70
+ ok: true,
71
+ reply: "reply:" + m,
72
+ sessionId: sid,
73
+ });
74
+ /** If set, `deliver` THROWS this (to test the defensive catch). */
75
+ throwOnce: Error | null = null;
76
+ /** Interim events to emit (via `onInterim`) during the next turn — set per test. */
77
+ interimToEmit: Parameters<InterimSink>[0][] = [];
78
+
79
+ async start(spec: AgentSpec): Promise<AgentHandle> {
80
+ return { backend: this.kind, channel: spec.channels[0] as string, name: spec.name, spec };
81
+ }
82
+
83
+ async deliver(
84
+ handle: AgentHandle,
85
+ message: string,
86
+ session: TurnSession,
87
+ onInterim?: InterimSink,
88
+ ): Promise<DeliverResult> {
89
+ this.calls.push({ channel: handle.channel, message, session });
90
+ this.inFlight++;
91
+ this.maxConcurrent = Math.max(this.maxConcurrent, this.inFlight);
92
+ try {
93
+ // Emit any configured interim events (mirrors the real backend streaming text +
94
+ // tool_use as the turn runs) so the registry's forwarding can be asserted.
95
+ if (onInterim) for (const e of this.interimToEmit) onInterim(e);
96
+ if (this.gate) await this.gate.promise;
97
+ if (this.throwOnce) {
98
+ const e = this.throwOnce;
99
+ this.throwOnce = null;
100
+ throw e;
101
+ }
102
+ return this.resultFor(message, session.id);
103
+ } finally {
104
+ this.inFlight--;
105
+ }
106
+ }
107
+
108
+ async stop(handle: AgentHandle): Promise<void> {
109
+ this.stopped.add(handle.channel);
110
+ }
111
+
112
+ async status(_handle: AgentHandle): Promise<AgentStatus> {
113
+ return { live: true };
114
+ }
115
+ }
116
+
117
+ /** A recorder WriteOutbound — captures every posted reply. */
118
+ function recorder(): { calls: { channel: string; reply: string; inReplyTo?: string }[]; fn: WriteOutbound } {
119
+ const calls: { channel: string; reply: string; inReplyTo?: string }[] = [];
120
+ const fn: WriteOutbound = async (channel, reply, inReplyTo) => {
121
+ calls.push({ channel, reply, ...(inReplyTo ? { inReplyTo } : {}) });
122
+ };
123
+ return { calls, fn };
124
+ }
125
+
126
+ /**
127
+ * A recorder WriteThread — captures every `#agent/thread` note the registry writes.
128
+ *
129
+ * The thread-as-container lifecycle writes TWO notes per turn: a `phase:"start"`
130
+ * working-ensure BEFORE the turn, then a `phase:"end"` final record after. `threads`
131
+ * holds ALL writes in order; `ends()` / `starts()` filter by phase so a test can assert
132
+ * the FINAL records (the pre-thread-as-container assertions) without counting the
133
+ * working-ensure, or assert the working-ensure specifically.
134
+ */
135
+ function threadRecorder(): {
136
+ threads: ThreadNote[];
137
+ ends: () => ThreadNote[];
138
+ starts: () => ThreadNote[];
139
+ fn: WriteThread;
140
+ } {
141
+ const threads: ThreadNote[] = [];
142
+ const fn: WriteThread = async (thread) => {
143
+ threads.push(thread);
144
+ };
145
+ return {
146
+ threads,
147
+ // `phase:"end"` is explicit on every registry-emitted final record; a write with no
148
+ // phase would also be a final record (back-compat), so treat absent as end too.
149
+ ends: () => threads.filter((t) => t.phase !== "start"),
150
+ starts: () => threads.filter((t) => t.phase === "start"),
151
+ fn,
152
+ };
153
+ }
154
+
155
+ /** A multi-threaded spec (materializes one `#agent/thread` note per fire). */
156
+ const specMultiThreaded = (name: string, channel = name, definition?: string): AgentSpec => ({
157
+ name,
158
+ channels: [channel],
159
+ mode: "multi-threaded",
160
+ ...(definition ? { definition } : {}),
161
+ });
162
+
163
+ /** A recorder TurnEventSink — captures every (channel, event) the registry emits. */
164
+ function turnRecorder(): { events: { channel: string; event: TurnLifecycleEvent }[]; fn: TurnEventSink } {
165
+ const events: { channel: string; event: TurnLifecycleEvent }[] = [];
166
+ const fn: TurnEventSink = (channel, event) => {
167
+ events.push({ channel, event });
168
+ };
169
+ return { events, fn };
170
+ }
171
+
172
+ const specFor = (name: string, channel = name): AgentSpec => ({ name, channels: [channel] });
173
+
174
+ /** Spin the microtask/timer queue until `pred()` is true or we give up. */
175
+ async function until(pred: () => boolean, tries = 200): Promise<void> {
176
+ for (let i = 0; i < tries && !pred(); i++) {
177
+ await new Promise<void>((r) => setTimeout(r, 1));
178
+ }
179
+ }
180
+
181
+ describe("ProgrammaticAgentRegistry — registration + indexes", () => {
182
+ test("register indexes by channel + name; has/get reflect it", async () => {
183
+ const backend = new FakeBackend();
184
+ const { fn } = recorder();
185
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: fn });
186
+
187
+ expect(reg.hasChannel("eng")).toBe(false);
188
+ const h = await reg.register(specFor("eng"));
189
+ expect(h.name).toBe("eng");
190
+ expect(h.channel).toBe("eng");
191
+ expect(reg.hasChannel("eng")).toBe(true);
192
+ expect(reg.hasName("eng")).toBe(true);
193
+ expect(reg.getByChannel("eng")?.name).toBe("eng");
194
+ expect(reg.getByName("eng")?.channel).toBe("eng");
195
+ expect(reg.list().map((x) => x.name)).toEqual(["eng"]);
196
+ });
197
+
198
+ test("deregister drops the indexes + tears down the backend handle (stop)", async () => {
199
+ const backend = new FakeBackend();
200
+ const { fn } = recorder();
201
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: fn });
202
+ await reg.register(specFor("eng"));
203
+ expect(await reg.deregister("eng")).toBe(true);
204
+ expect(reg.hasChannel("eng")).toBe(false);
205
+ expect(reg.hasName("eng")).toBe(false);
206
+ // deregister calls backend.stop (a no-op for programmatic) — it does NOT clear the
207
+ // thread-note session; re-registering should resume. Wiping continuity is resetSession.
208
+ expect(backend.stopped.has("eng")).toBe(true);
209
+ // A second deregister is a no-op false.
210
+ expect(await reg.deregister("eng")).toBe(false);
211
+ });
212
+
213
+ test("resetSession CLEARS the thread-note session (via clearSession) WITHOUT deregistering", async () => {
214
+ const backend = new FakeBackend();
215
+ const { fn } = recorder();
216
+ // Track the clearSession invocations + simulate the note's session being wiped: after a
217
+ // clear, readSession returns undefined for that (channel, name).
218
+ const cleared: { channel: string; name: string }[] = [];
219
+ let priorSession: string | undefined = "sess-OLD";
220
+ const clearSession = async (channel: string, name: string) => {
221
+ cleared.push({ channel, name });
222
+ priorSession = undefined; // the note's session is now empty.
223
+ };
224
+ const reader: { calls: { channel: string; name: string }[] } = { calls: [] };
225
+ const readSession = async (channel: string, name: string) => {
226
+ reader.calls.push({ channel, name });
227
+ return priorSession;
228
+ };
229
+ const threads = threadRecorder();
230
+ const reg = new ProgrammaticAgentRegistry({
231
+ backend,
232
+ writeOutbound: fn,
233
+ writeThread: threads.fn,
234
+ readSession,
235
+ clearSession,
236
+ });
237
+ await reg.register(specFor("eng"));
238
+
239
+ // RESET → invokes clearSession with the right (channel, name); does NOT deregister.
240
+ expect(await reg.resetSession("eng")).toBe(true);
241
+ expect(cleared).toEqual([{ channel: "eng", name: "eng" }]);
242
+ expect(reg.hasName("eng")).toBe(true); // still registered.
243
+ // reset() does NOT route through backend.stop anymore (the session lives on the note).
244
+ expect(backend.stopped.has("eng")).toBe(false);
245
+
246
+ // BONUS — after the reset, the next drain finds NO prior session → a fresh {resume:false}
247
+ // create (self-heal), proving reset actually wiped continuity (not a dead no-op).
248
+ reg.enqueue("eng", { content: "after reset" });
249
+ await until(() => backend.calls.length === 1);
250
+ expect(backend.calls[0]!.session.resume).toBe(false);
251
+ expect(backend.calls[0]!.session.id).toMatch(/^[0-9a-f-]{36}$/);
252
+
253
+ expect(await reg.resetSession("nope")).toBe(false);
254
+ });
255
+ });
256
+
257
+ describe("ProgrammaticAgentRegistry — inbound enqueue + outbound", () => {
258
+ test("a delivered turn writes a non-empty reply as an outbound note", async () => {
259
+ const backend = new FakeBackend();
260
+ const rec = recorder();
261
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
262
+ await reg.register(specFor("eng"));
263
+
264
+ expect(reg.enqueue("eng", { content: "hello", inReplyTo: "note-1" })).toBe(true);
265
+ await until(() => rec.calls.length === 1);
266
+
267
+ expect(backend.calls).toHaveLength(1);
268
+ expect(backend.calls[0]!.channel).toBe("eng");
269
+ expect(backend.calls[0]!.message).toBe("hello");
270
+ // No readSession wired → a single-threaded turn CREATES a fresh session (resume:false).
271
+ expect(backend.calls[0]!.session.resume).toBe(false);
272
+ expect(backend.calls[0]!.session.id).toMatch(/^[0-9a-f-]{36}$/);
273
+ expect(rec.calls).toEqual([{ channel: "eng", reply: "reply:hello", inReplyTo: "note-1" }]);
274
+ });
275
+
276
+ test("enqueue for an UNREGISTERED channel is a no-op false (caller falls back)", () => {
277
+ const backend = new FakeBackend();
278
+ const rec = recorder();
279
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
280
+ expect(reg.enqueue("ghost", { content: "x" })).toBe(false);
281
+ expect(backend.calls).toHaveLength(0);
282
+ });
283
+
284
+ test("an EMPTY reply writes NO outbound note (reviewer contract)", async () => {
285
+ const backend = new FakeBackend();
286
+ backend.resultFor = () => ({ ok: true, reply: "" });
287
+ const rec = recorder();
288
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
289
+ await reg.register(specFor("eng"));
290
+
291
+ reg.enqueue("eng", { content: "tool-only work" });
292
+ await until(() => backend.calls.length === 1);
293
+ // Give any erroneous outbound write a chance to land, then assert none did.
294
+ await new Promise<void>((r) => setTimeout(r, 5));
295
+ expect(backend.calls).toHaveLength(1);
296
+ expect(rec.calls).toHaveLength(0);
297
+ });
298
+
299
+ test("an ok:false turn writes a user-facing FAILURE note + does not crash/loop", async () => {
300
+ const backend = new FakeBackend();
301
+ backend.resultFor = () => ({ ok: false, error: "mint refused" });
302
+ const rec = recorder();
303
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
304
+ await reg.register(specFor("eng"));
305
+
306
+ reg.enqueue("eng", { content: "do it" });
307
+ await until(() => rec.calls.length === 1);
308
+ await new Promise<void>((r) => setTimeout(r, 5));
309
+ // Exactly ONE turn ran (the backend owns turn-retry, not the drain), and the drain
310
+ // posted a SINGLE user-facing failure note carrying the reason (no silent no-reply).
311
+ expect(backend.calls).toHaveLength(1);
312
+ expect(rec.calls).toHaveLength(1);
313
+ expect(rec.calls[0]!.reply).toContain("mint refused");
314
+ });
315
+
316
+ test("a deliver() that THROWS is caught — the worker survives + drains the rest", async () => {
317
+ const backend = new FakeBackend();
318
+ backend.throwOnce = new Error("surprise throw");
319
+ const rec = recorder();
320
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
321
+ await reg.register(specFor("eng"));
322
+
323
+ reg.enqueue("eng", { content: "first (throws)" });
324
+ reg.enqueue("eng", { content: "second (ok)" });
325
+ await until(() => rec.calls.length === 2);
326
+ // Both turns ran; the throw on the first didn't strand the second. The caught throw
327
+ // posts a user-facing failure note (carrying the reason); the second succeeds normally.
328
+ expect(backend.calls.map((c) => c.message)).toEqual(["first (throws)", "second (ok)"]);
329
+ expect(rec.calls).toHaveLength(2);
330
+ expect(rec.calls[0]!.reply).toContain("surprise throw");
331
+ expect(rec.calls[1]!).toEqual({ channel: "eng", reply: "reply:second (ok)" });
332
+ });
333
+ });
334
+
335
+ describe("ProgrammaticAgentRegistry — #agent/thread notes (unified lifecycle, BOTH modes)", () => {
336
+ test("a completed MULTI-THREADED turn materializes an #agent/thread note (status ok) carrying input/output/definition/mode/name", async () => {
337
+ const backend = new FakeBackend();
338
+ const rec = recorder();
339
+ const threads = threadRecorder();
340
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
341
+ await reg.register(specMultiThreaded("digest", "digest", "Agents/digest"));
342
+
343
+ reg.enqueue("digest", { content: "run the digest" });
344
+ await until(() => threads.ends().length === 1);
345
+
346
+ // Thread-as-container: ONE working-ensure (phase:start, status:working) BEFORE the turn,
347
+ // then ONE final record (phase:end, status:ok) after — for the same per-fire note.
348
+ expect(threads.starts()).toHaveLength(1);
349
+ expect(threads.starts()[0]!.status).toBe("working");
350
+ expect(threads.ends()).toHaveLength(1);
351
+ const thread = threads.ends()[0]!;
352
+ expect(thread.channel).toBe("digest");
353
+ expect(thread.name).toBe("digest");
354
+ expect(thread.status).toBe("ok");
355
+ expect(thread.mode).toBe("multi-threaded");
356
+ expect(thread.definition).toBe("Agents/digest");
357
+ expect(thread.input).toBe("run the digest");
358
+ expect(thread.output).toBe("reply:run the digest");
359
+ expect(typeof thread.started_at).toBe("string");
360
+ expect(typeof thread.ended_at).toBe("string");
361
+ // The start + end target the SAME per-fire note (same threadId) — no duplicate minted.
362
+ expect(threads.starts()[0]!.threadId).toBe(thread.threadId!);
363
+ // The dual-write is ADDITIVE: a non-empty reply writes EXACTLY one outbound
364
+ // (the chat delivery) AND exactly one FINAL thread note (the primary record).
365
+ await until(() => rec.calls.length === 1);
366
+ expect(rec.calls.length).toBe(1);
367
+ expect(threads.ends().length).toBe(1);
368
+ });
369
+
370
+ test("a SINGLE-THREADED turn ALSO materializes ONE #agent/thread note (the unified model — named after the def)", async () => {
371
+ const backend = new FakeBackend();
372
+ const rec = recorder();
373
+ const threads = threadRecorder();
374
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
375
+ // specFor → no mode → single-threaded (the default).
376
+ await reg.register(specFor("eng"));
377
+
378
+ reg.enqueue("eng", { content: "hello" });
379
+ await until(() => threads.ends().length === 1);
380
+
381
+ // BOTH modes materialize a thread note now (the structural unification): a
382
+ // single-threaded turn writes ONE FINAL record, mode single-threaded, NAMED AFTER THE
383
+ // DEF (the deterministic upsert key the transport derives the stable path from).
384
+ // Thread-as-container: a working-ensure (phase:start) preceded it (same upsert key).
385
+ expect(threads.starts()).toHaveLength(1);
386
+ expect(threads.starts()[0]!.name).toBe("eng");
387
+ expect(threads.ends()).toHaveLength(1);
388
+ expect(threads.ends()[0]!.mode).toBe("single-threaded");
389
+ expect(threads.ends()[0]!.name).toBe("eng");
390
+ expect(threads.ends()[0]!.channel).toBe("eng");
391
+ expect(threads.ends()[0]!.input).toBe("hello");
392
+ expect(threads.ends()[0]!.output).toBe("reply:hello");
393
+ // The single-threaded outbound reply was still written (no regression).
394
+ expect(rec.calls).toHaveLength(1);
395
+ });
396
+
397
+ test("a single-threaded agent over TWO turns records ONE thread (same name/channel — the upsert key) both turns, status carries forward", async () => {
398
+ const backend = new FakeBackend();
399
+ const rec = recorder();
400
+ const threads = threadRecorder();
401
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
402
+ await reg.register(specFor("eng")); // single-threaded (default).
403
+
404
+ // Two turns on the same channel — drained serially, FIFO.
405
+ reg.enqueue("eng", { content: "turn one" });
406
+ reg.enqueue("eng", { content: "turn two" });
407
+ await until(() => threads.ends().length === 2);
408
+
409
+ // recordThread (the FINAL record) is called for BOTH turns (the registry seam can't
410
+ // simulate the transport's read-existing upsert, so we assert the UPSERT KEY is stable
411
+ // across turns — same channel + same name + same mode — which the transport maps to the
412
+ // SAME deterministic path `Threads/<channel>/<name>`, overwriting in place. The per-turn
413
+ // turn_count/usage aggregation — incl. the start-ensure NOT double-counting — is covered
414
+ // at the vault-transport layer). Each turn ALSO emits its own working-ensure (phase:start).
415
+ expect(threads.starts()).toHaveLength(2);
416
+ expect(threads.ends()).toHaveLength(2);
417
+ const [t1, t2] = threads.ends();
418
+ expect(t1!.mode).toBe("single-threaded");
419
+ expect(t2!.mode).toBe("single-threaded");
420
+ expect(t1!.name).toBe("eng");
421
+ expect(t2!.name).toBe("eng"); // SAME upsert key → same note, upserted.
422
+ expect(t1!.channel).toBe("eng");
423
+ expect(t2!.channel).toBe("eng");
424
+ expect(t1!.input).toBe("turn one");
425
+ expect(t2!.input).toBe("turn two");
426
+ });
427
+
428
+ test("a multi-threaded fire writes a thread note per fire (each carries this fire's turn — distinct records)", async () => {
429
+ const backend = new FakeBackend();
430
+ const rec = recorder();
431
+ const threads = threadRecorder();
432
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
433
+ await reg.register(specMultiThreaded("digest"));
434
+
435
+ reg.enqueue("digest", { content: "fire A" });
436
+ reg.enqueue("digest", { content: "fire B" });
437
+ await until(() => threads.ends().length === 2);
438
+
439
+ // One FINAL thread note PER FIRE (today one fire = one thread = one note; the transport
440
+ // assigns each a fresh uuid path, so they're distinct records). Each fire's start-ensure
441
+ // shares that fire's threadId (so start + end target the SAME per-fire note).
442
+ expect(threads.ends()).toHaveLength(2);
443
+ expect(threads.ends().map((t) => t.input)).toEqual(["fire A", "fire B"]);
444
+ expect(threads.ends().every((t) => t.mode === "multi-threaded")).toBe(true);
445
+ expect(threads.starts()).toHaveLength(2);
446
+ // Per fire the working-ensure + final record share a threadId (distinct across fires).
447
+ expect(threads.starts()[0]!.threadId).toBe(threads.ends()[0]!.threadId!);
448
+ expect(threads.starts()[1]!.threadId).toBe(threads.ends()[1]!.threadId!);
449
+ expect(threads.ends()[0]!.threadId).not.toBe(threads.ends()[1]!.threadId);
450
+ });
451
+
452
+ test("a FAILED MULTI-THREADED turn still materializes an #agent/thread note with status:error + the reason", async () => {
453
+ const backend = new FakeBackend();
454
+ backend.resultFor = () => ({ ok: false, error: "mint refused" });
455
+ const rec = recorder();
456
+ const threads = threadRecorder();
457
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
458
+ await reg.register(specMultiThreaded("digest"));
459
+
460
+ reg.enqueue("digest", { content: "do it" });
461
+ await until(() => threads.ends().length === 1);
462
+
463
+ // The working-ensure (phase:start) still ran BEFORE the turn; the FINAL record is error.
464
+ expect(threads.starts()).toHaveLength(1);
465
+ expect(threads.ends()).toHaveLength(1);
466
+ expect(threads.ends()[0]!.mode).toBe("multi-threaded");
467
+ expect(threads.ends()[0]!.status).toBe("error");
468
+ expect(threads.ends()[0]!.output).toBe("mint refused");
469
+ // A user-facing failure note IS now written for a failed turn (carries the reason).
470
+ expect(rec.calls).toHaveLength(1);
471
+ expect(rec.calls[0]!.reply).toContain("mint refused");
472
+ });
473
+
474
+ test("a FAILED SINGLE-THREADED turn ALSO materializes an #agent/thread note with status:error (substantiates BOTH modes)", async () => {
475
+ const backend = new FakeBackend();
476
+ backend.resultFor = () => ({ ok: false, error: "mint refused" });
477
+ const rec = recorder();
478
+ const threads = threadRecorder();
479
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
480
+ // specFor → no mode → single-threaded (the default).
481
+ await reg.register(specFor("eng"));
482
+
483
+ reg.enqueue("eng", { content: "do it" });
484
+ await until(() => threads.ends().length === 1);
485
+
486
+ // The working-ensure (phase:start) still ran BEFORE the turn; the FINAL record is error.
487
+ expect(threads.starts()).toHaveLength(1);
488
+ expect(threads.ends()).toHaveLength(1);
489
+ expect(threads.ends()[0]!.mode).toBe("single-threaded");
490
+ expect(threads.ends()[0]!.name).toBe("eng");
491
+ expect(threads.ends()[0]!.status).toBe("error");
492
+ expect(threads.ends()[0]!.output).toBe("mint refused");
493
+ // A user-facing failure note IS now written for a failed turn (carries the reason).
494
+ expect(rec.calls).toHaveLength(1);
495
+ expect(rec.calls[0]!.reply).toContain("mint refused");
496
+ });
497
+
498
+ test("a turn with an empty reply STILL materializes a thread note (status ok, empty output)", async () => {
499
+ const backend = new FakeBackend();
500
+ backend.resultFor = () => ({ ok: true, reply: "" });
501
+ const rec = recorder();
502
+ const threads = threadRecorder();
503
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
504
+ await reg.register(specMultiThreaded("digest"));
505
+
506
+ reg.enqueue("digest", { content: "tool-only run" });
507
+ await until(() => threads.ends().length === 1);
508
+ // The working-ensure (phase:start, no fake reply) preceded the empty-reply final record.
509
+ expect(threads.starts()).toHaveLength(1);
510
+ expect(threads.starts()[0]!.output).toBe("");
511
+ expect(threads.ends()[0]!.status).toBe("ok");
512
+ expect(threads.ends()[0]!.output).toBe("");
513
+ // Empty reply → no outbound message note (the thread note IS the record).
514
+ expect(rec.calls).toHaveLength(0);
515
+ });
516
+
517
+ test("REGRESSION (c34db03, now BOTH modes): a turn whose outbound write THROWS still leaves a primary #agent/thread note (now re-recorded as error — FIX 1)", async () => {
518
+ const backend = new FakeBackend();
519
+ const threads = threadRecorder();
520
+ // A THROWING WriteOutbound — the thread note is written BEFORE the additive outbound
521
+ // (c34db03, now applied uniformly to BOTH modes), so the failed transcript write must
522
+ // NOT cost us the primary record. Use a SINGLE-THREADED spec to prove the c34db03
523
+ // ordering now protects single-threaded too. (`recorder()` can't throw; inline variant.)
524
+ // The error message carries NO HTTP status → classified TRANSIENT → it RETRIES the
525
+ // bounded budget (FIX 1, PR #3) before giving up, then re-records the thread as error.
526
+ let outboundAttempts = 0;
527
+ const throwingWriteOutbound: WriteOutbound = async () => {
528
+ outboundAttempts++;
529
+ throw new Error("vault write boom"); // no (NNN) status → transient → retried.
530
+ };
531
+ const reg = new ProgrammaticAgentRegistry({
532
+ backend,
533
+ writeOutbound: throwingWriteOutbound,
534
+ writeThread: threads.fn,
535
+ outboundRetryBaseMs: 0,
536
+ });
537
+ await reg.register(specFor("eng")); // single-threaded (default) — the ordering applies here now.
538
+
539
+ reg.enqueue("eng", { content: "fire it" });
540
+ // Thread-as-container + FIX 1: a working-ensure (phase:start) is written first, then the
541
+ // primary `ok` FINAL record, then after retries exhaust a `error` FINAL record re-records
542
+ // the UN-DELIVERED reply — so `ends()` (the final records) is exactly [ok, error].
543
+ await until(() => threads.ends().length === 2);
544
+
545
+ // The working-ensure preceded everything (status:working, no fake reply).
546
+ expect(threads.starts()).toHaveLength(1);
547
+ expect(threads.starts()[0]!.status).toBe("working");
548
+ // First FINAL (optimistic) record was `ok`; the second re-records the failure so the
549
+ // durable thread record does NOT falsely claim the reply landed.
550
+ expect(threads.ends()[0]!.status).toBe("ok");
551
+ expect(threads.ends()[0]!.output).toBe("reply:fire it");
552
+ expect(threads.ends()[1]!.status).toBe("error");
553
+ expect(threads.ends()[1]!.mode).toBe("single-threaded");
554
+ // The undelivered reply text is preserved in the error record for recovery.
555
+ expect(threads.ends()[1]!.output).toContain("reply:fire it");
556
+ // The re-record reuses the SAME per-turn threadId + sameTurn (no double-count, no dup).
557
+ expect(threads.ends()[1]!.threadId).toBe(threads.ends()[0]!.threadId!);
558
+ expect(threads.ends()[1]!.sameTurn).toBe(true);
559
+ // Transient → the outbound was retried the full budget (1 initial + OUTBOUND_MAX_RETRIES).
560
+ expect(outboundAttempts).toBe(1 + OUTBOUND_MAX_RETRIES);
561
+ });
562
+ });
563
+
564
+ describe("ProgrammaticAgentRegistry — thread≡session (the daemon owns the uuid)", () => {
565
+ /** A recorder readSession — captures every (channel, name) consulted; returns `prior`. */
566
+ function sessionReader(prior?: string): {
567
+ calls: { channel: string; name: string }[];
568
+ fn: (channel: string, name: string) => Promise<string | undefined>;
569
+ } {
570
+ const calls: { channel: string; name: string }[] = [];
571
+ const fn = async (channel: string, name: string) => {
572
+ calls.push({ channel, name });
573
+ return prior;
574
+ };
575
+ return { calls, fn };
576
+ }
577
+
578
+ test("single-threaded with a PRIOR session: consults readSession + passes {resume:true} to deliver", async () => {
579
+ const backend = new FakeBackend();
580
+ const rec = recorder();
581
+ const threads = threadRecorder();
582
+ const reader = sessionReader("11111111-1111-4111-8111-111111111111");
583
+ const reg = new ProgrammaticAgentRegistry({
584
+ backend,
585
+ writeOutbound: rec.fn,
586
+ writeThread: threads.fn,
587
+ readSession: reader.fn,
588
+ });
589
+ await reg.register(specFor("eng")); // single-threaded (default).
590
+
591
+ reg.enqueue("eng", { content: "hello" });
592
+ await until(() => backend.calls.length === 1);
593
+
594
+ // readSession was consulted with the channel + the def name (the deterministic key).
595
+ expect(reader.calls).toEqual([{ channel: "eng", name: "eng" }]);
596
+ // A prior session → RESUME it (continue the conversation), with that exact id.
597
+ expect(backend.calls[0]!.session).toEqual({
598
+ id: "11111111-1111-4111-8111-111111111111",
599
+ resume: true,
600
+ });
601
+ // The END record carries the session claude echoed (the persisted thread≡session record).
602
+ await until(() => threads.ends().length === 1);
603
+ expect(threads.ends()[0]!.session).toBe("11111111-1111-4111-8111-111111111111");
604
+ // The START-ensure carries NO session (FIX 2) — it runs before claude, so no session is
605
+ // established yet; persisting one there would brick the next turn if claude never inited.
606
+ // (Continuity for a single-threaded resume is preserved by the transport's prior-read.)
607
+ expect(threads.starts()[0]!.session).toBeUndefined();
608
+ });
609
+
610
+ test("single-threaded with NO prior session: consults readSession + passes {resume:false} + a fresh uuid", async () => {
611
+ const backend = new FakeBackend();
612
+ const rec = recorder();
613
+ const threads = threadRecorder();
614
+ const reader = sessionReader(undefined); // no prior — first turn.
615
+ const reg = new ProgrammaticAgentRegistry({
616
+ backend,
617
+ writeOutbound: rec.fn,
618
+ writeThread: threads.fn,
619
+ readSession: reader.fn,
620
+ });
621
+ await reg.register(specFor("eng"));
622
+
623
+ reg.enqueue("eng", { content: "hello" });
624
+ await until(() => backend.calls.length === 1);
625
+
626
+ expect(reader.calls).toEqual([{ channel: "eng", name: "eng" }]);
627
+ // No prior → CREATE a fresh session with a generated uuid (--session-id, not --resume).
628
+ expect(backend.calls[0]!.session.resume).toBe(false);
629
+ expect(backend.calls[0]!.session.id).toMatch(/^[0-9a-f-]{36}$/);
630
+ // The fresh uuid is the one persisted onto the thread note (so turn 2 can resume it).
631
+ await until(() => threads.ends().length === 1);
632
+ expect(threads.ends()[0]!.session).toBe(backend.calls[0]!.session.id);
633
+ });
634
+
635
+ test("multi-threaded NEVER consults readSession + ALWAYS passes {resume:false} with a fresh uuid", async () => {
636
+ const backend = new FakeBackend();
637
+ const rec = recorder();
638
+ const threads = threadRecorder();
639
+ const reader = sessionReader("should-never-be-used");
640
+ const reg = new ProgrammaticAgentRegistry({
641
+ backend,
642
+ writeOutbound: rec.fn,
643
+ writeThread: threads.fn,
644
+ readSession: reader.fn,
645
+ });
646
+ await reg.register(specMultiThreaded("digest", "digest"));
647
+
648
+ // Two fires — each must mint its OWN fresh session, never resume.
649
+ reg.enqueue("digest", { content: "fire one" });
650
+ await until(() => backend.calls.length === 1);
651
+ reg.enqueue("digest", { content: "fire two" });
652
+ await until(() => backend.calls.length === 2);
653
+
654
+ // readSession is NEVER consulted for a multi-threaded agent (each fire is a fresh thread).
655
+ expect(reader.calls).toHaveLength(0);
656
+ // Both fires CREATE fresh sessions (resume:false), with DISTINCT uuids.
657
+ expect(backend.calls[0]!.session.resume).toBe(false);
658
+ expect(backend.calls[1]!.session.resume).toBe(false);
659
+ expect(backend.calls[0]!.session.id).toMatch(/^[0-9a-f-]{36}$/);
660
+ expect(backend.calls[1]!.session.id).toMatch(/^[0-9a-f-]{36}$/);
661
+ expect(backend.calls[0]!.session.id).not.toBe(backend.calls[1]!.session.id);
662
+ // Each per-fire thread note carries its own fire's session.
663
+ await until(() => threads.ends().length === 2);
664
+ expect(threads.ends()[0]!.session).toBe(backend.calls[0]!.session.id);
665
+ expect(threads.ends()[1]!.session).toBe(backend.calls[1]!.session.id);
666
+ });
667
+
668
+ test("no readSession wired: a single-threaded turn still CREATES a fresh session", async () => {
669
+ const backend = new FakeBackend();
670
+ const rec = recorder();
671
+ const threads = threadRecorder();
672
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
673
+ await reg.register(specFor("eng"));
674
+
675
+ reg.enqueue("eng", { content: "hello" });
676
+ await until(() => backend.calls.length === 1);
677
+
678
+ expect(backend.calls[0]!.session.resume).toBe(false);
679
+ expect(backend.calls[0]!.session.id).toMatch(/^[0-9a-f-]{36}$/);
680
+ await until(() => threads.ends().length === 1);
681
+ expect(threads.ends()[0]!.session).toBe(backend.calls[0]!.session.id);
682
+ });
683
+
684
+ test("the captured backend sessionId (Claude's echoed id) is what lands on the thread note", async () => {
685
+ const backend = new FakeBackend();
686
+ // The backend echoes a DIFFERENT id than the one we passed (Claude's authoritative id).
687
+ backend.resultFor = (m) => ({ ok: true, reply: "reply:" + m, sessionId: "echoed-by-claude-id" });
688
+ const rec = recorder();
689
+ const threads = threadRecorder();
690
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
691
+ await reg.register(specFor("eng"));
692
+
693
+ reg.enqueue("eng", { content: "hello" });
694
+ await until(() => threads.ends().length === 1);
695
+
696
+ // The END record prefers Claude's echoed id (result.sessionId) over the uuid we passed.
697
+ expect(threads.ends()[0]!.session).toBe("echoed-by-claude-id");
698
+ });
699
+
700
+ test("FIX 2 — a failed turn that established NO session persists NONE → next turn self-heals (no brick)", async () => {
701
+ const backend = new FakeBackend();
702
+ // A turn that FAILS before claude ever creates a session: { ok:false } with NO sessionId
703
+ // (claude exited before emitting an init/result session_id). The OLD code persisted the
704
+ // passed uuid here → next turn `--resume`d a phantom id → "No conversation found" →
705
+ // permanent brick. FIX 2: persist NOTHING when claude echoed no session.
706
+ backend.resultFor = () => ({ ok: false, error: "claude exited 1 before init" });
707
+ const rec = recorder();
708
+ const threads = threadRecorder();
709
+ // Simulate the note: readSession returns whatever the last persisted end-record carried.
710
+ let stored: string | undefined; // no prior session.
711
+ const reg = new ProgrammaticAgentRegistry({
712
+ backend,
713
+ writeOutbound: rec.fn,
714
+ writeThread: async (t) => {
715
+ threads.threads.push(t);
716
+ // Mirror the transport's persistence: an end record with a session sets it; a start
717
+ // or a sessionless end leaves the prior value (the transport preserves single-threaded).
718
+ if (t.phase !== "start" && t.session) stored = t.session;
719
+ },
720
+ readSession: async () => stored,
721
+ });
722
+ await reg.register(specFor("eng")); // single-threaded (default).
723
+
724
+ // Turn 1 — fails before establishing a session.
725
+ reg.enqueue("eng", { content: "boom" });
726
+ await until(() => threads.ends().length === 1);
727
+ // The error end-record carries NO session (claude echoed none) — so the note stays clean.
728
+ expect(threads.ends()[0]!.status).toBe("error");
729
+ expect(threads.ends()[0]!.session).toBeUndefined();
730
+ expect(stored).toBeUndefined(); // nothing persisted → no phantom to --resume.
731
+
732
+ // Turn 2 — readSession finds no session → a FRESH {resume:false} create (self-heal,
733
+ // NOT a brick). The next turn is a clean new conversation, not a doomed --resume.
734
+ reg.enqueue("eng", { content: "again" });
735
+ await until(() => backend.calls.length === 2);
736
+ expect(backend.calls[1]!.session.resume).toBe(false);
737
+ expect(backend.calls[1]!.session.id).toMatch(/^[0-9a-f-]{36}$/);
738
+ });
739
+ });
740
+
741
+ describe("ProgrammaticAgentRegistry — outbound retry on transient failure (FIX 1, PR #3)", () => {
742
+ test("isTransientOutboundError: 5xx + network = transient; 4xx = permanent", () => {
743
+ expect(isTransientOutboundError(new Error("write reply failed (502) boom"))).toBe(true);
744
+ expect(isTransientOutboundError(new Error("write reply failed (503)"))).toBe(true);
745
+ expect(isTransientOutboundError(new Error("ECONNREFUSED"))).toBe(true); // no status → network.
746
+ expect(isTransientOutboundError(new Error("fetch failed"))).toBe(true);
747
+ expect(isTransientOutboundError(new Error("write reply failed (400) bad"))).toBe(false);
748
+ expect(isTransientOutboundError(new Error("write reply failed (401)"))).toBe(false);
749
+ expect(isTransientOutboundError(new Error("write reply failed (409)"))).toBe(false);
750
+ });
751
+
752
+ test("a transient-then-success outbound RETRIES and the reply LANDS (no loss, turn not re-run)", async () => {
753
+ const backend = new FakeBackend();
754
+ const threads = threadRecorder();
755
+ // Fail twice with a transient (5xx) error, then succeed — the retry must land the reply.
756
+ let attempts = 0;
757
+ const recorded: { reply: string }[] = [];
758
+ const flakyWriteOutbound: WriteOutbound = async (_channel, reply) => {
759
+ attempts++;
760
+ if (attempts <= 2) throw new Error("vault transport: write reply failed (502) blip");
761
+ recorded.push({ reply });
762
+ };
763
+ const reg = new ProgrammaticAgentRegistry({
764
+ backend,
765
+ writeOutbound: flakyWriteOutbound,
766
+ writeThread: threads.fn,
767
+ outboundRetryBaseMs: 0,
768
+ });
769
+ await reg.register(specFor("eng"));
770
+
771
+ reg.enqueue("eng", { content: "important" });
772
+ await until(() => recorded.length === 1);
773
+ await new Promise<void>((r) => setTimeout(r, 5));
774
+
775
+ // The reply landed on the 3rd attempt (1 initial + 2 retries == OUTBOUND_MAX_RETRIES).
776
+ expect(attempts).toBe(1 + OUTBOUND_MAX_RETRIES);
777
+ expect(recorded).toEqual([{ reply: "reply:important" }]);
778
+ // The backend ran the turn EXACTLY ONCE (no re-run / fork on the retry).
779
+ expect(backend.calls).toHaveLength(1);
780
+ // The FINAL thread note is the single `ok` record (the reply was ultimately delivered) —
781
+ // no error re-record because delivery succeeded. (A working-ensure preceded it.)
782
+ expect(threads.starts()).toHaveLength(1);
783
+ expect(threads.ends()).toHaveLength(1);
784
+ expect(threads.ends()[0]!.status).toBe("ok");
785
+ });
786
+
787
+ test("a PERSISTENT failure surfaces an error event + re-records the thread as error + does NOT claim success", async () => {
788
+ const backend = new FakeBackend();
789
+ const threads = threadRecorder();
790
+ const turn = turnRecorder();
791
+ let attempts = 0;
792
+ const alwaysFail: WriteOutbound = async () => {
793
+ attempts++;
794
+ throw new Error("vault transport: write reply failed (503) down");
795
+ };
796
+ const reg = new ProgrammaticAgentRegistry({
797
+ backend,
798
+ writeOutbound: alwaysFail,
799
+ writeThread: threads.fn,
800
+ onTurnEvent: turn.fn,
801
+ outboundRetryBaseMs: 0,
802
+ });
803
+ await reg.register(specFor("eng"));
804
+
805
+ reg.enqueue("eng", { content: "doomed" });
806
+ await until(() => threads.ends().length === 2);
807
+ await new Promise<void>((r) => setTimeout(r, 5));
808
+
809
+ // Retried the full budget then gave up (1 + OUTBOUND_MAX_RETRIES).
810
+ expect(attempts).toBe(1 + OUTBOUND_MAX_RETRIES);
811
+ // The live view resolved to ERROR (not `done`) — no silently-vanished reply.
812
+ const errorEvents = turn.events.filter((e) => e.event.kind === "error");
813
+ expect(errorEvents.length).toBeGreaterThanOrEqual(1);
814
+ expect(turn.events.some((e) => e.event.kind === "done")).toBe(false);
815
+ // The FINAL thread record does NOT falsely claim a clean ok: the second final record is
816
+ // error, carrying the un-delivered reply text for recovery. (A working-ensure preceded.)
817
+ expect(threads.ends()).toHaveLength(2);
818
+ expect(threads.ends()[1]!.status).toBe("error");
819
+ expect(threads.ends()[1]!.output).toContain("reply:doomed");
820
+ });
821
+
822
+ test("a PERMANENT (4xx) outbound failure does NOT retry — gives up immediately", async () => {
823
+ const backend = new FakeBackend();
824
+ const threads = threadRecorder();
825
+ let attempts = 0;
826
+ const reject4xx: WriteOutbound = async () => {
827
+ attempts++;
828
+ throw new Error("vault transport: write reply failed (400) bad request");
829
+ };
830
+ const reg = new ProgrammaticAgentRegistry({
831
+ backend,
832
+ writeOutbound: reject4xx,
833
+ writeThread: threads.fn,
834
+ outboundRetryBaseMs: 0,
835
+ });
836
+ await reg.register(specFor("eng"));
837
+
838
+ reg.enqueue("eng", { content: "rejected" });
839
+ await until(() => threads.ends().length === 2);
840
+ await new Promise<void>((r) => setTimeout(r, 5));
841
+
842
+ // A 4xx is a real rejection → exactly ONE attempt, no retry.
843
+ expect(attempts).toBe(1);
844
+ // The second FINAL record re-records the turn as error (the un-delivered reply).
845
+ expect(threads.ends()).toHaveLength(2);
846
+ expect(threads.ends()[1]!.status).toBe("error");
847
+ });
848
+ });
849
+
850
+ describe("ProgrammaticAgentRegistry — serial queue (the hard invariant)", () => {
851
+ test("two inbounds during a running turn are processed ONE AT A TIME, FIFO, never concurrent", async () => {
852
+ const backend = new FakeBackend();
853
+ const gate = deferred<void>();
854
+ backend.gate = { promise: gate.promise, resolve: gate.resolve };
855
+ const rec = recorder();
856
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
857
+ await reg.register(specFor("eng"));
858
+
859
+ // First enqueue starts a turn that blocks on the gate.
860
+ reg.enqueue("eng", { content: "m1" });
861
+ await until(() => backend.calls.length === 1);
862
+ expect(reg.statusOf("eng").state).toBe("working");
863
+
864
+ // Two more arrive WHILE the first turn is in flight — they queue.
865
+ reg.enqueue("eng", { content: "m2" });
866
+ reg.enqueue("eng", { content: "m3" });
867
+ // Still exactly one call has STARTED (the gate holds the first; the others wait).
868
+ expect(backend.calls).toHaveLength(1);
869
+ expect(reg.statusOf("eng")).toEqual({ state: "queued", queued: 2 });
870
+
871
+ // Release the gate: the worker drains m1, then m2, then m3 in order. Because the
872
+ // gate's promise is already resolved, subsequent turns don't block.
873
+ gate.resolve();
874
+ await until(() => rec.calls.length === 3);
875
+
876
+ expect(backend.calls.map((c) => c.message)).toEqual(["m1", "m2", "m3"]);
877
+ expect(rec.calls.map((c) => c.reply)).toEqual(["reply:m1", "reply:m2", "reply:m3"]);
878
+ // The invariant: never two concurrent turns for the same channel.
879
+ expect(backend.maxConcurrent).toBe(1);
880
+ // Queue fully drained → idle.
881
+ expect(reg.statusOf("eng").state).toBe("idle");
882
+ });
883
+
884
+ test("statusOf — idle with no work, working with one in flight, queued:N with a backlog", async () => {
885
+ const backend = new FakeBackend();
886
+ const gate = deferred<void>();
887
+ backend.gate = { promise: gate.promise, resolve: gate.resolve };
888
+ const rec = recorder();
889
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
890
+ await reg.register(specFor("eng"));
891
+ expect(reg.statusOf("eng")).toEqual({ state: "idle", queued: 0 });
892
+
893
+ reg.enqueue("eng", { content: "a" });
894
+ await until(() => backend.calls.length === 1);
895
+ expect(reg.statusOf("eng")).toEqual({ state: "working", queued: 0 });
896
+
897
+ reg.enqueue("eng", { content: "b" });
898
+ expect(reg.statusOf("eng")).toEqual({ state: "queued", queued: 1 });
899
+
900
+ gate.resolve();
901
+ await until(() => rec.calls.length === 2);
902
+ expect(reg.statusOf("eng")).toEqual({ state: "idle", queued: 0 });
903
+ });
904
+ });
905
+
906
+ describe("ProgrammaticAgentRegistry — streaming turn view (onTurnEvent)", () => {
907
+ test("forwards the backend's interim events + a final 'done' (keyed by channel)", async () => {
908
+ const backend = new FakeBackend();
909
+ backend.interimToEmit = [
910
+ { kind: "init", sessionId: "s-1" },
911
+ { kind: "text", text: "thinking…" },
912
+ { kind: "tool", tool: "Read" },
913
+ ];
914
+ backend.resultFor = () => ({ ok: true, reply: "final answer" });
915
+ const rec = recorder();
916
+ const turns = turnRecorder();
917
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, onTurnEvent: turns.fn });
918
+ await reg.register(specFor("eng"));
919
+
920
+ reg.enqueue("eng", { content: "hi" });
921
+ await until(() => rec.calls.length === 1);
922
+ // Let the trailing 'done' (emitted after the outbound write) land.
923
+ await until(() => turns.events.some((e) => e.event.kind === "done"));
924
+
925
+ expect(turns.events.map((e) => e.channel)).toEqual(["eng", "eng", "eng", "eng"]);
926
+ expect(turns.events.map((e) => e.event)).toEqual([
927
+ { kind: "init", sessionId: "s-1" },
928
+ { kind: "text", text: "thinking…" },
929
+ { kind: "tool", tool: "Read" },
930
+ { kind: "done", reply: "final answer" },
931
+ ]);
932
+ // The durable outbound write is unchanged by the live view.
933
+ expect(rec.calls).toEqual([{ channel: "eng", reply: "final answer" }]);
934
+ });
935
+
936
+ test("an ok:false turn emits a 'error' lifecycle event (no stuck working state)", async () => {
937
+ const backend = new FakeBackend();
938
+ backend.resultFor = () => ({ ok: false, error: "mint refused" });
939
+ const rec = recorder();
940
+ const turns = turnRecorder();
941
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, onTurnEvent: turns.fn });
942
+ await reg.register(specFor("eng"));
943
+
944
+ reg.enqueue("eng", { content: "x" });
945
+ await until(() => rec.calls.length === 1);
946
+
947
+ expect(turns.events).toEqual([{ channel: "eng", event: { kind: "error", error: "mint refused" } }]);
948
+ // The failed turn ALSO posts a user-facing failure note (carrying the reason).
949
+ expect(rec.calls).toHaveLength(1);
950
+ expect(rec.calls[0]!.reply).toContain("mint refused");
951
+ });
952
+
953
+ test("a backend THROW also emits 'error' (the defensive catch resolves the live view)", async () => {
954
+ const backend = new FakeBackend();
955
+ backend.throwOnce = new Error("boom");
956
+ const rec = recorder();
957
+ const turns = turnRecorder();
958
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, onTurnEvent: turns.fn });
959
+ await reg.register(specFor("eng"));
960
+
961
+ reg.enqueue("eng", { content: "x" });
962
+ await until(() => turns.events.some((e) => e.event.kind === "error"));
963
+
964
+ expect(turns.events).toEqual([{ channel: "eng", event: { kind: "error", error: "boom" } }]);
965
+ });
966
+
967
+ test("an empty reply still emits 'done' (with reply '') so the live view finalizes", async () => {
968
+ const backend = new FakeBackend();
969
+ backend.resultFor = () => ({ ok: true, reply: "" });
970
+ const rec = recorder();
971
+ const turns = turnRecorder();
972
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, onTurnEvent: turns.fn });
973
+ await reg.register(specFor("eng"));
974
+
975
+ reg.enqueue("eng", { content: "tool-only" });
976
+ await until(() => turns.events.some((e) => e.event.kind === "done"));
977
+
978
+ expect(turns.events).toEqual([{ channel: "eng", event: { kind: "done", reply: "" } }]);
979
+ // No durable note for an empty reply (the existing contract), but the view finalizes.
980
+ expect(rec.calls).toHaveLength(0);
981
+ });
982
+
983
+ test("a throwing sink can't break the worker (the durable write still lands)", async () => {
984
+ const backend = new FakeBackend();
985
+ backend.interimToEmit = [{ kind: "text", text: "hi" }];
986
+ const rec = recorder();
987
+ const reg = new ProgrammaticAgentRegistry({
988
+ backend,
989
+ writeOutbound: rec.fn,
990
+ onTurnEvent: () => {
991
+ throw new Error("dead stream");
992
+ },
993
+ });
994
+ await reg.register(specFor("eng"));
995
+
996
+ reg.enqueue("eng", { content: "hi" });
997
+ await until(() => rec.calls.length === 1);
998
+ expect(rec.calls).toEqual([{ channel: "eng", reply: "reply:hi" }]);
999
+ });
1000
+
1001
+ test("with NO sink wired, turns run exactly as before (no throw, durable write lands)", async () => {
1002
+ const backend = new FakeBackend();
1003
+ backend.interimToEmit = [{ kind: "text", text: "ignored" }];
1004
+ const rec = recorder();
1005
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
1006
+ await reg.register(specFor("eng"));
1007
+
1008
+ reg.enqueue("eng", { content: "hi" });
1009
+ await until(() => rec.calls.length === 1);
1010
+ expect(rec.calls).toEqual([{ channel: "eng", reply: "reply:hi" }]);
1011
+ });
1012
+ });
1013
+
1014
+ describe("ProgrammaticAgentRegistry — pending-inbound queue + replay-on-register (agent#121)", () => {
1015
+ test("an inbound for an EXPECTED-but-not-yet-registered channel is QUEUED pending (not dropped)", () => {
1016
+ const backend = new FakeBackend();
1017
+ const rec = recorder();
1018
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
1019
+
1020
+ // No live agent yet — but the channel is EXPECTED (the def-instantiation path marked it
1021
+ // before bringing the agent up). enqueue() would no-op false here; queuePending OWNS it.
1022
+ reg.expectChannel("eng");
1023
+ expect(reg.hasChannel("eng")).toBe(false);
1024
+ expect(reg.enqueue("eng", { content: "early" })).toBe(false); // not live → enqueue declines.
1025
+ expect(reg.queuePending("eng", { content: "early" })).toBe("queued");
1026
+ expect(reg.pendingCount("eng")).toBe(1);
1027
+ // Nothing ran yet (no live agent), but nothing was lost either.
1028
+ expect(backend.calls).toHaveLength(0);
1029
+ });
1030
+
1031
+ test("queuePending for a genuinely UNKNOWN channel (not expected) returns 'unknown' (caller logs+drops)", () => {
1032
+ const backend = new FakeBackend();
1033
+ const rec = recorder();
1034
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
1035
+ // Never expected, never registered → nothing maps to it.
1036
+ expect(reg.queuePending("ghost", { content: "x" })).toBe("unknown");
1037
+ expect(reg.pendingCount("ghost")).toBe(0);
1038
+ });
1039
+
1040
+ test("on register() the channel's pending queue DRAINS into the serial worker, in arrival order (FIFO)", async () => {
1041
+ const backend = new FakeBackend();
1042
+ const rec = recorder();
1043
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
1044
+
1045
+ // Three inbound arrive BEFORE the agent is live — all buffered pending.
1046
+ reg.expectChannel("eng");
1047
+ expect(reg.queuePending("eng", { content: "first" })).toBe("queued");
1048
+ expect(reg.queuePending("eng", { content: "second" })).toBe("queued");
1049
+ expect(reg.queuePending("eng", { content: "third" })).toBe("queued");
1050
+ expect(reg.pendingCount("eng")).toBe(3);
1051
+
1052
+ // The agent registers → the buffer replays through the normal serial path, FIFO.
1053
+ await reg.register(specFor("eng"));
1054
+ await until(() => rec.calls.length === 3);
1055
+
1056
+ // The buffer is drained + the EXPECTED mark cleared (the live index is the truth now).
1057
+ expect(reg.pendingCount("eng")).toBe(0);
1058
+ expect(reg.isExpected("eng")).toBe(false);
1059
+ // Turns ran in arrival order (the serial worker drains FIFO).
1060
+ expect(backend.calls.map((c) => c.message)).toEqual(["first", "second", "third"]);
1061
+ expect(rec.calls.map((c) => c.reply)).toEqual(["reply:first", "reply:second", "reply:third"]);
1062
+ });
1063
+
1064
+ test("the pending buffer is CAPPED — past the cap the OLDEST is evicted (FIFO), newest kept", () => {
1065
+ const backend = new FakeBackend();
1066
+ const rec = recorder();
1067
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
1068
+
1069
+ reg.expectChannel("eng");
1070
+ // Fill to the cap, then push one MORE — the oldest ("m0") is evicted.
1071
+ for (let i = 0; i < PENDING_INBOUND_CAP; i++) {
1072
+ expect(reg.queuePending("eng", { content: `m${i}` })).toBe("queued");
1073
+ }
1074
+ expect(reg.pendingCount("eng")).toBe(PENDING_INBOUND_CAP);
1075
+ expect(reg.queuePending("eng", { content: "overflow" })).toBe("queued");
1076
+ // Still capped (didn't grow past the cap).
1077
+ expect(reg.pendingCount("eng")).toBe(PENDING_INBOUND_CAP);
1078
+ });
1079
+
1080
+ test("an UNKNOWN channel queuePending does NOT crash + leaves the registry usable", () => {
1081
+ const backend = new FakeBackend();
1082
+ const rec = recorder();
1083
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
1084
+ expect(() => reg.queuePending("ghost", { content: "x" })).not.toThrow();
1085
+ // The registry still works for a real registration afterward.
1086
+ expect(reg.queuePending("ghost", { content: "y" })).toBe("unknown");
1087
+ });
1088
+
1089
+ test("register() clears the EXPECTED mark even with an EMPTY pending buffer", async () => {
1090
+ const backend = new FakeBackend();
1091
+ const rec = recorder();
1092
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
1093
+ reg.expectChannel("eng");
1094
+ expect(reg.isExpected("eng")).toBe(true);
1095
+ await reg.register(specFor("eng"));
1096
+ expect(reg.isExpected("eng")).toBe(false);
1097
+ expect(reg.pendingCount("eng")).toBe(0);
1098
+ });
1099
+
1100
+ test("unexpectChannel drops a stale EXPECTED mark + its buffered pending (teardown)", () => {
1101
+ const backend = new FakeBackend();
1102
+ const rec = recorder();
1103
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
1104
+ reg.expectChannel("eng");
1105
+ reg.queuePending("eng", { content: "stranded" });
1106
+ expect(reg.pendingCount("eng")).toBe(1);
1107
+ reg.unexpectChannel("eng");
1108
+ expect(reg.isExpected("eng")).toBe(false);
1109
+ expect(reg.pendingCount("eng")).toBe(0);
1110
+ // A subsequent inbound for the now-unexpected channel is 'unknown' (correctly dropped).
1111
+ expect(reg.queuePending("eng", { content: "after" })).toBe("unknown");
1112
+ });
1113
+
1114
+ test("a channel-move re-register clears the OLD channel's expected mark + pending buffer (no leak)", async () => {
1115
+ const backend = new FakeBackend();
1116
+ const rec = recorder();
1117
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
1118
+
1119
+ // Register the agent on channel "old", then buffer a pending inbound for "old".
1120
+ await reg.register({ name: "mover", channels: ["old"] });
1121
+ reg.expectChannel("old");
1122
+ reg.queuePending("old", { content: "stranded" });
1123
+ expect(reg.pendingCount("old")).toBe(1);
1124
+
1125
+ // Re-register the SAME name onto a DIFFERENT wake channel — the old channel's indexes,
1126
+ // expected mark, and pending buffer must all be dropped (nothing routes to "old" now).
1127
+ await reg.register({ name: "mover", channels: ["new"] });
1128
+ expect(reg.hasChannel("old")).toBe(false);
1129
+ expect(reg.isExpected("old")).toBe(false);
1130
+ expect(reg.pendingCount("old")).toBe(0);
1131
+ expect(reg.hasChannel("new")).toBe(true);
1132
+ });
1133
+
1134
+ test("a pending inbound that arrives DURING a drain (after register) still replays in order", async () => {
1135
+ const backend = new FakeBackend();
1136
+ const rec = recorder();
1137
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
1138
+
1139
+ // Two pending before register.
1140
+ reg.expectChannel("eng");
1141
+ reg.queuePending("eng", { content: "p1" });
1142
+ reg.queuePending("eng", { content: "p2" });
1143
+
1144
+ await reg.register(specFor("eng"));
1145
+ // After register the channel is LIVE — a further inbound goes through enqueue directly.
1146
+ await until(() => rec.calls.length === 2);
1147
+ expect(reg.enqueue("eng", { content: "p3" })).toBe(true);
1148
+ await until(() => rec.calls.length === 3);
1149
+
1150
+ expect(backend.calls.map((c) => c.message)).toEqual(["p1", "p2", "p3"]);
1151
+ });
1152
+ });
1153
+
1154
+ describe("ProgrammaticAgentRegistry — thread-as-container working-ensure (Part B)", () => {
1155
+ test("the working-ensure (phase:start, status:working) is written BEFORE deliver() runs", async () => {
1156
+ const backend = new FakeBackend();
1157
+ // Gate the turn so we can observe the working-ensure write WHILE the turn is in flight.
1158
+ const gate = deferred<void>();
1159
+ backend.gate = { promise: gate.promise, resolve: gate.resolve };
1160
+ const rec = recorder();
1161
+ const threads = threadRecorder();
1162
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
1163
+ await reg.register(specFor("eng"));
1164
+
1165
+ reg.enqueue("eng", { content: "do work" });
1166
+ // Wait until deliver() is in flight (the turn was handed the message + is blocked on the
1167
+ // gate). The start-ensure is `await`ed BEFORE deliver() in the drain, so by the time
1168
+ // deliver() has been called the working-ensure MUST already be written — and the FINAL
1169
+ // (end) record must NOT be (the turn hasn't completed). This proves the ordering:
1170
+ // working-ensure strictly precedes the turn.
1171
+ await until(() => backend.calls.length === 1);
1172
+ expect(threads.starts()).toHaveLength(1);
1173
+ expect(threads.starts()[0]!.status).toBe("working");
1174
+ expect(threads.starts()[0]!.output).toBe(""); // NO fake reply while working.
1175
+ expect(threads.starts()[0]!.input).toBe("do work");
1176
+ // The turn is in flight but hasn't produced its end record yet (gated).
1177
+ expect(backend.calls).toHaveLength(1);
1178
+ expect(threads.ends()).toHaveLength(0);
1179
+
1180
+ // Release the turn → it completes → the FINAL (ok) record lands, same threadId.
1181
+ gate.resolve();
1182
+ await until(() => threads.ends().length === 1);
1183
+ expect(threads.ends()[0]!.status).toBe("ok");
1184
+ expect(threads.ends()[0]!.threadId).toBe(threads.starts()[0]!.threadId!);
1185
+ });
1186
+
1187
+ test("the start-ensure does NOT write a fake reply even though the turn ultimately replies", async () => {
1188
+ const backend = new FakeBackend();
1189
+ const rec = recorder();
1190
+ const threads = threadRecorder();
1191
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
1192
+ await reg.register(specFor("eng"));
1193
+
1194
+ reg.enqueue("eng", { content: "hi" });
1195
+ await until(() => threads.ends().length === 1);
1196
+ // The working-ensure carries status:working + empty output; the end carries the reply.
1197
+ expect(threads.starts()[0]!.status).toBe("working");
1198
+ expect(threads.starts()[0]!.output).toBe("");
1199
+ expect(threads.ends()[0]!.output).toBe("reply:hi");
1200
+ });
1201
+
1202
+ test("the OUTBOUND reply is stamped with the turn's thread id (definition→thread→message link); multi-threaded → the per-fire note leaf", async () => {
1203
+ const backend = new FakeBackend();
1204
+ const threads = threadRecorder();
1205
+ // A recorder that ALSO captures the threadId the worker passes to writeOutbound.
1206
+ const outbound: { reply: string; threadId?: string }[] = [];
1207
+ const writeOutbound: WriteOutbound = async (_channel, reply, _inReplyTo, threadId) => {
1208
+ outbound.push({ reply, ...(threadId ? { threadId } : {}) });
1209
+ };
1210
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound, writeThread: threads.fn });
1211
+ await reg.register(specMultiThreaded("digest"));
1212
+
1213
+ reg.enqueue("digest", { content: "go" });
1214
+ await until(() => outbound.length === 1);
1215
+ // The outbound carries the per-turn thread id, which for multi-threaded equals the
1216
+ // per-fire thread note's leaf — the explicit message↔thread link.
1217
+ expect(outbound[0]!.threadId).toBeDefined();
1218
+ expect(outbound[0]!.threadId).toBe(threads.ends()[0]!.threadId!);
1219
+ });
1220
+ });
1221
+
1222
+ // ───────────────────────────────────────────────────────────────────────────────
1223
+ // AGENT-TO-AGENT CALLBACK ROUTING ("reply_to") — design 2026-06-20-agent-callbacks.md.
1224
+ // A FAKE WriteCallback recorder captures every callback the drain delivers (its target
1225
+ // channel + content + the metadata contract), so we can assert: a reply_to message gets
1226
+ // exactly one callback w/ the right metadata; a no-reply_to message gets none; ok AND error
1227
+ // both fire; the depth guard suppresses; and N callbacks to one channel drain FIFO + none
1228
+ // is lost (the orchestrator-resume concurrency story).
1229
+ // ───────────────────────────────────────────────────────────────────────────────
1230
+
1231
+ /** A recorder WriteCallback — captures every callback the registry delivers, in order. */
1232
+ function callbackRecorder(): {
1233
+ calls: { channel: string; content: string; meta: CallbackMeta }[];
1234
+ fn: WriteCallback;
1235
+ } {
1236
+ const calls: { channel: string; content: string; meta: CallbackMeta }[] = [];
1237
+ const fn: WriteCallback = async (channel, content, meta) => {
1238
+ calls.push({ channel, content, meta });
1239
+ };
1240
+ return { calls, fn };
1241
+ }
1242
+
1243
+ /** A WriteOutbound that returns a deterministic note id, so source_message is assertable. */
1244
+ function recorderWithId(noteId = "outbound-note-1"): {
1245
+ calls: { channel: string; reply: string }[];
1246
+ fn: WriteOutbound;
1247
+ } {
1248
+ const calls: { channel: string; reply: string }[] = [];
1249
+ const fn: WriteOutbound = async (channel, reply) => {
1250
+ calls.push({ channel, reply });
1251
+ return { id: noteId };
1252
+ };
1253
+ return { calls, fn };
1254
+ }
1255
+
1256
+ describe("ProgrammaticAgentRegistry — agent-to-agent callbacks (reply_to)", () => {
1257
+ test("an inbound WITH reply_to → exactly ONE callback to the reply_to channel with the full metadata contract (ok)", async () => {
1258
+ const backend = new FakeBackend();
1259
+ backend.resultFor = (m) => ({ ok: true, reply: "done:" + m });
1260
+ const out = recorderWithId("reply-note-42");
1261
+ const cb = callbackRecorder();
1262
+ const reg = new ProgrammaticAgentRegistry({
1263
+ backend,
1264
+ writeOutbound: out.fn,
1265
+ writeCallback: cb.fn,
1266
+ });
1267
+ await reg.register(specFor("worker"));
1268
+
1269
+ reg.enqueue("worker", {
1270
+ content: "sub-task",
1271
+ inReplyTo: "inbound-note-7",
1272
+ replyTo: "orchestrator",
1273
+ correlationId: "corr-abc",
1274
+ delegationDepth: 2,
1275
+ });
1276
+ await until(() => cb.calls.length === 1);
1277
+ // Let any erroneous SECOND callback land, then assert there was exactly one.
1278
+ await new Promise<void>((r) => setTimeout(r, 5));
1279
+ expect(cb.calls).toHaveLength(1);
1280
+
1281
+ const { channel, content, meta } = cb.calls[0]!;
1282
+ expect(channel).toBe("orchestrator"); // delivered to the SENDER's channel.
1283
+ expect(content).toContain("[callback]");
1284
+ expect(content).not.toContain("done:sub-task"); // summary + link, NOT the full reply.
1285
+ expect(meta.callback).toBe("true");
1286
+ expect(meta.status).toBe("ok");
1287
+ expect(meta.source_channel).toBe("worker");
1288
+ expect(meta.source_message).toBe("reply-note-42"); // the delivered outbound note id.
1289
+ expect(meta.source_thread).toBeDefined(); // the per-turn thread id (pull link).
1290
+ expect(meta.correlation_id).toBe("corr-abc"); // echoed verbatim.
1291
+ expect(meta.delegation_depth).toBe("3"); // incoming 2 + 1 hop.
1292
+ // The callback note must NOT itself carry a reply_to (terminal — the loop guard).
1293
+ expect((meta as unknown as Record<string, unknown>).reply_to).toBeUndefined();
1294
+ });
1295
+
1296
+ test("an inbound WITHOUT reply_to → NO callback (a normal turn never emits one)", async () => {
1297
+ const backend = new FakeBackend();
1298
+ const out = recorderWithId();
1299
+ const cb = callbackRecorder();
1300
+ const reg = new ProgrammaticAgentRegistry({
1301
+ backend,
1302
+ writeOutbound: out.fn,
1303
+ writeCallback: cb.fn,
1304
+ });
1305
+ await reg.register(specFor("worker"));
1306
+
1307
+ reg.enqueue("worker", { content: "plain message" });
1308
+ await until(() => out.calls.length === 1);
1309
+ await new Promise<void>((r) => setTimeout(r, 5));
1310
+ expect(out.calls).toHaveLength(1); // the turn ran + replied normally,
1311
+ expect(cb.calls).toHaveLength(0); // but no callback fired.
1312
+ });
1313
+
1314
+ test("the callback fires on an ERROR turn too (status:error, no source_message)", async () => {
1315
+ const backend = new FakeBackend();
1316
+ backend.resultFor = () => ({ ok: false, error: "mint refused" });
1317
+ const out = recorderWithId();
1318
+ const cb = callbackRecorder();
1319
+ const reg = new ProgrammaticAgentRegistry({
1320
+ backend,
1321
+ writeOutbound: out.fn,
1322
+ writeCallback: cb.fn,
1323
+ });
1324
+ await reg.register(specFor("worker"));
1325
+
1326
+ reg.enqueue("worker", { content: "do it", replyTo: "orchestrator", delegationDepth: 0 });
1327
+ await until(() => cb.calls.length === 1);
1328
+ await new Promise<void>((r) => setTimeout(r, 5));
1329
+ expect(cb.calls).toHaveLength(1);
1330
+ // An error turn now posts a user-facing failure note to the worker's own channel
1331
+ // (in addition to the orchestrator callback) — carrying the reason.
1332
+ expect(out.calls).toHaveLength(1);
1333
+ expect(out.calls[0]!.reply).toContain("mint refused");
1334
+ const { meta, content } = cb.calls[0]!;
1335
+ expect(meta.status).toBe("error"); // but the orchestrator still learns it failed.
1336
+ expect(content).toContain("error");
1337
+ expect(meta.source_message).toBeUndefined(); // no delivered reply note.
1338
+ expect(meta.delegation_depth).toBe("1"); // 0 + 1.
1339
+ });
1340
+
1341
+ test("the callback fires when deliver() THROWS (defensive catch → status:error)", async () => {
1342
+ const backend = new FakeBackend();
1343
+ backend.throwOnce = new Error("surprise throw");
1344
+ const out = recorderWithId();
1345
+ const cb = callbackRecorder();
1346
+ const reg = new ProgrammaticAgentRegistry({
1347
+ backend,
1348
+ writeOutbound: out.fn,
1349
+ writeCallback: cb.fn,
1350
+ });
1351
+ await reg.register(specFor("worker"));
1352
+
1353
+ reg.enqueue("worker", { content: "boom", replyTo: "orchestrator" });
1354
+ await until(() => cb.calls.length === 1);
1355
+ expect(cb.calls[0]!.meta.status).toBe("error");
1356
+ });
1357
+
1358
+ test("the callback fires status:error when the outbound write FAILS after retries (reply produced but not delivered)", async () => {
1359
+ const backend = new FakeBackend();
1360
+ backend.resultFor = (m) => ({ ok: true, reply: "done:" + m });
1361
+ // Always-fail outbound (a permanent 4xx → no retry); the reply was produced but lost.
1362
+ const alwaysFail: WriteOutbound = async () => {
1363
+ throw new Error("vault transport: write reply failed (400) bad request");
1364
+ };
1365
+ const cb = callbackRecorder();
1366
+ const reg = new ProgrammaticAgentRegistry({
1367
+ backend,
1368
+ writeOutbound: alwaysFail,
1369
+ writeCallback: cb.fn,
1370
+ outboundRetryBaseMs: 1,
1371
+ });
1372
+ await reg.register(specFor("worker"));
1373
+
1374
+ reg.enqueue("worker", { content: "x", replyTo: "orchestrator" });
1375
+ await until(() => cb.calls.length === 1);
1376
+ // The orchestrator learns the turn did NOT truly succeed (the reply never landed).
1377
+ expect(cb.calls[0]!.meta.status).toBe("error");
1378
+ expect(cb.calls[0]!.meta.source_message).toBeUndefined(); // the note never landed.
1379
+ });
1380
+
1381
+ test("delegation_depth >= MAX → NO callback (the depth loop guard), turn still runs", async () => {
1382
+ const backend = new FakeBackend();
1383
+ backend.resultFor = (m) => ({ ok: true, reply: "done:" + m });
1384
+ const out = recorderWithId();
1385
+ const cb = callbackRecorder();
1386
+ const reg = new ProgrammaticAgentRegistry({
1387
+ backend,
1388
+ writeOutbound: out.fn,
1389
+ writeCallback: cb.fn,
1390
+ });
1391
+ await reg.register(specFor("worker"));
1392
+
1393
+ // An incoming message already AT the ceiling: a callback would push it over, so suppress.
1394
+ reg.enqueue("worker", {
1395
+ content: "deep",
1396
+ replyTo: "orchestrator",
1397
+ delegationDepth: MAX_DELEGATION_DEPTH,
1398
+ });
1399
+ await until(() => out.calls.length === 1); // the turn STILL ran + replied,
1400
+ await new Promise<void>((r) => setTimeout(r, 5));
1401
+ expect(out.calls).toHaveLength(1);
1402
+ expect(cb.calls).toHaveLength(0); // but the callback was suppressed by the depth guard.
1403
+ });
1404
+
1405
+ test("a message just UNDER the ceiling still gets a callback (boundary)", async () => {
1406
+ const backend = new FakeBackend();
1407
+ backend.resultFor = (m) => ({ ok: true, reply: "done:" + m });
1408
+ const out = recorderWithId();
1409
+ const cb = callbackRecorder();
1410
+ const reg = new ProgrammaticAgentRegistry({
1411
+ backend,
1412
+ writeOutbound: out.fn,
1413
+ writeCallback: cb.fn,
1414
+ });
1415
+ await reg.register(specFor("worker"));
1416
+
1417
+ reg.enqueue("worker", {
1418
+ content: "near-edge",
1419
+ replyTo: "orchestrator",
1420
+ delegationDepth: MAX_DELEGATION_DEPTH - 1,
1421
+ });
1422
+ await until(() => cb.calls.length === 1);
1423
+ expect(cb.calls).toHaveLength(1);
1424
+ expect(cb.calls[0]!.meta.delegation_depth).toBe(String(MAX_DELEGATION_DEPTH)); // the last hop.
1425
+ });
1426
+
1427
+ test("no WriteCallback wired → reply_to is inert (the turn runs normally, no crash)", async () => {
1428
+ const backend = new FakeBackend();
1429
+ backend.resultFor = (m) => ({ ok: true, reply: "done:" + m });
1430
+ const out = recorderWithId();
1431
+ // NOTE: writeCallback intentionally NOT passed.
1432
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: out.fn });
1433
+ await reg.register(specFor("worker"));
1434
+
1435
+ reg.enqueue("worker", { content: "x", replyTo: "orchestrator" });
1436
+ await until(() => out.calls.length === 1);
1437
+ expect(out.calls).toHaveLength(1); // the turn ran fine despite reply_to + no sink.
1438
+ });
1439
+
1440
+ test("a WriteCallback that THROWS does not strand the drain (best-effort, logged)", async () => {
1441
+ const backend = new FakeBackend();
1442
+ backend.resultFor = (m) => ({ ok: true, reply: "done:" + m });
1443
+ const out = recorderWithId();
1444
+ const throwingCb: WriteCallback = async () => {
1445
+ throw new Error("callback delivery boom");
1446
+ };
1447
+ const reg = new ProgrammaticAgentRegistry({
1448
+ backend,
1449
+ writeOutbound: out.fn,
1450
+ writeCallback: throwingCb,
1451
+ });
1452
+ await reg.register(specFor("worker"));
1453
+
1454
+ // Two reply_to messages; the first callback throws — the second turn must still drain.
1455
+ reg.enqueue("worker", { content: "first", replyTo: "orchestrator" });
1456
+ reg.enqueue("worker", { content: "second", replyTo: "orchestrator" });
1457
+ await until(() => out.calls.length === 2);
1458
+ expect(out.calls.map((c) => c.reply)).toEqual(["done:first", "done:second"]);
1459
+ });
1460
+
1461
+ test("CONCURRENCY: N callbacks returning to ONE orchestrator channel drain FIFO, none lost or clobbered", async () => {
1462
+ // The orchestrator-resume story: an orchestrator fires N sub-tasks; each worker's turn
1463
+ // completes and delivers a callback BACK to the orchestrator's channel. Those callbacks
1464
+ // arrive as inbound on the orchestrator's channel and are handled by ITS per-channel
1465
+ // serial drain — one at a time, FIFO, never concurrent (its --resume session carries
1466
+ // state across them). We exercise the DRAIN-SIDE FIFO property directly here (enqueue N
1467
+ // callback-shaped inbound messages on one channel + assert they drain in order, none
1468
+ // lost, the backend never ran two concurrently) — NOT the real vault-IPC delivery path
1469
+ // (callback note → trigger → /api/vault/inbound → emit), which the wiring + vault suites
1470
+ // cover. The serial drain is the same machinery either way, so this pins the invariant.
1471
+ const backend = new FakeBackend();
1472
+ backend.resultFor = (m) => ({ ok: true, reply: "ack:" + m });
1473
+ const out = recorderWithId();
1474
+ const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: out.fn });
1475
+ await reg.register(specFor("orchestrator"));
1476
+
1477
+ const N = 6;
1478
+ for (let i = 0; i < N; i++) {
1479
+ // A callback inbound carries NO reply_to (terminal) — exactly the shape the daemon
1480
+ // writes. The orchestrator processes each as "a sub-task finished" message.
1481
+ reg.enqueue("orchestrator", { content: `callback-${i}` });
1482
+ }
1483
+ await until(() => backend.calls.length === N);
1484
+ // FIFO: arrival order preserved, NONE lost or duplicated.
1485
+ expect(backend.calls.map((c) => c.message)).toEqual(
1486
+ Array.from({ length: N }, (_, i) => `callback-${i}`),
1487
+ );
1488
+ // The per-channel serial worker never ran two turns at once (the --resume invariant).
1489
+ expect(backend.maxConcurrent).toBe(1);
1490
+ expect(out.calls.map((c) => c.reply)).toEqual(
1491
+ Array.from({ length: N }, (_, i) => `ack:callback-${i}`),
1492
+ );
1493
+ });
1494
+ });