@synap-core/cli 1.12.2 → 1.13.1-next.20261102.246dc73

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 (358) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +38 -11
  3. package/dist/build-info.json +2 -3
  4. package/dist/commands/agents.d.ts +0 -1
  5. package/dist/commands/agents.js +13 -3
  6. package/dist/commands/agents.js.map +1 -1
  7. package/dist/commands/app.js +2 -1
  8. package/dist/commands/app.js.map +1 -1
  9. package/dist/commands/automation.js +46 -41
  10. package/dist/commands/automation.js.map +1 -1
  11. package/dist/commands/box.d.ts +42 -0
  12. package/dist/commands/box.js +89 -0
  13. package/dist/commands/box.js.map +1 -0
  14. package/dist/commands/bridge-setup.js +3 -1
  15. package/dist/commands/bridge-setup.js.map +1 -1
  16. package/dist/commands/capability.d.ts +87 -1
  17. package/dist/commands/capability.js +174 -64
  18. package/dist/commands/capability.js.map +1 -1
  19. package/dist/commands/close-questions.d.ts +27 -0
  20. package/dist/commands/close-questions.js +49 -0
  21. package/dist/commands/close-questions.js.map +1 -0
  22. package/dist/commands/close-questions.test.d.ts +1 -0
  23. package/dist/commands/close-questions.test.js +52 -0
  24. package/dist/commands/close-questions.test.js.map +1 -0
  25. package/dist/commands/connect.d.ts +1 -3
  26. package/dist/commands/connect.js +2 -2
  27. package/dist/commands/connect.js.map +1 -1
  28. package/dist/commands/context.js +74 -44
  29. package/dist/commands/context.js.map +1 -1
  30. package/dist/commands/data-extra.d.ts +13 -0
  31. package/dist/commands/data-extra.js +35 -6
  32. package/dist/commands/data-extra.js.map +1 -1
  33. package/dist/commands/data.js +2 -1
  34. package/dist/commands/data.js.map +1 -1
  35. package/dist/commands/doctor.d.ts +39 -0
  36. package/dist/commands/doctor.js +136 -14
  37. package/dist/commands/doctor.js.map +1 -1
  38. package/dist/commands/hooks.d.ts +19 -0
  39. package/dist/commands/hooks.js +70 -0
  40. package/dist/commands/hooks.js.map +1 -0
  41. package/dist/commands/import.d.ts +25 -0
  42. package/dist/commands/import.js +98 -12
  43. package/dist/commands/import.js.map +1 -1
  44. package/dist/commands/init.d.ts +14 -0
  45. package/dist/commands/init.js +63 -21
  46. package/dist/commands/init.js.map +1 -1
  47. package/dist/commands/keys-request.d.ts +48 -0
  48. package/dist/commands/keys-request.js +236 -0
  49. package/dist/commands/keys-request.js.map +1 -0
  50. package/dist/commands/keys-request.test.d.ts +1 -0
  51. package/dist/commands/keys-request.test.js +176 -0
  52. package/dist/commands/keys-request.test.js.map +1 -0
  53. package/dist/commands/knowledge.d.ts +42 -5
  54. package/dist/commands/knowledge.js +94 -29
  55. package/dist/commands/knowledge.js.map +1 -1
  56. package/dist/commands/lens.js +2 -1
  57. package/dist/commands/lens.js.map +1 -1
  58. package/dist/commands/market-authoring.js +1 -1
  59. package/dist/commands/market-authoring.js.map +1 -1
  60. package/dist/commands/market.d.ts +89 -31
  61. package/dist/commands/market.js +215 -222
  62. package/dist/commands/market.js.map +1 -1
  63. package/dist/commands/mcp.d.ts +1 -1
  64. package/dist/commands/mcp.js +14 -9
  65. package/dist/commands/mcp.js.map +1 -1
  66. package/dist/commands/observe.category.test.d.ts +1 -0
  67. package/dist/commands/observe.category.test.js +59 -0
  68. package/dist/commands/observe.category.test.js.map +1 -0
  69. package/dist/commands/observe.d.ts +3 -0
  70. package/dist/commands/observe.js +7 -5
  71. package/dist/commands/observe.js.map +1 -1
  72. package/dist/commands/observe.title.test.d.ts +1 -0
  73. package/dist/commands/observe.title.test.js +37 -0
  74. package/dist/commands/observe.title.test.js.map +1 -0
  75. package/dist/commands/playbook.js +9 -0
  76. package/dist/commands/playbook.js.map +1 -1
  77. package/dist/commands/pods.d.ts +6 -1
  78. package/dist/commands/pods.js +38 -17
  79. package/dist/commands/pods.js.map +1 -1
  80. package/dist/commands/proposals-actions.js +7 -1
  81. package/dist/commands/proposals-actions.js.map +1 -1
  82. package/dist/commands/proton-setup.js +3 -1
  83. package/dist/commands/proton-setup.js.map +1 -1
  84. package/dist/commands/providers.d.ts +22 -6
  85. package/dist/commands/providers.js +39 -32
  86. package/dist/commands/providers.js.map +1 -1
  87. package/dist/commands/raycast.d.ts +15 -0
  88. package/dist/commands/raycast.js +36 -10
  89. package/dist/commands/raycast.js.map +1 -1
  90. package/dist/commands/session-feed-wait.d.ts +48 -0
  91. package/dist/commands/session-feed-wait.js +261 -0
  92. package/dist/commands/session-feed-wait.js.map +1 -0
  93. package/dist/commands/session-post.d.ts +40 -0
  94. package/dist/commands/session-post.js +96 -0
  95. package/dist/commands/session-post.js.map +1 -0
  96. package/dist/commands/session-wait.d.ts +9 -4
  97. package/dist/commands/session-wait.js +20 -5
  98. package/dist/commands/session-wait.js.map +1 -1
  99. package/dist/commands/sessions.d.ts +2 -0
  100. package/dist/commands/sessions.js +23 -0
  101. package/dist/commands/sessions.js.map +1 -1
  102. package/dist/commands/skills-commands.d.ts +2 -0
  103. package/dist/commands/skills-commands.js +9 -1
  104. package/dist/commands/skills-commands.js.map +1 -1
  105. package/dist/commands/skills-commands.test.d.ts +1 -0
  106. package/dist/commands/skills-commands.test.js +57 -0
  107. package/dist/commands/skills-commands.test.js.map +1 -0
  108. package/dist/commands/status.js +13 -7
  109. package/dist/commands/status.js.map +1 -1
  110. package/dist/commands/statusline.backoff.test.js +13 -3
  111. package/dist/commands/statusline.backoff.test.js.map +1 -1
  112. package/dist/commands/statusline.d.ts +104 -9
  113. package/dist/commands/statusline.js +392 -148
  114. package/dist/commands/statusline.js.map +1 -1
  115. package/dist/commands/statusline.line2.test.d.ts +15 -0
  116. package/dist/commands/statusline.line2.test.js +226 -0
  117. package/dist/commands/statusline.line2.test.js.map +1 -0
  118. package/dist/commands/statusline.needs-you.test.d.ts +1 -0
  119. package/dist/commands/statusline.needs-you.test.js +17 -0
  120. package/dist/commands/statusline.needs-you.test.js.map +1 -0
  121. package/dist/commands/statusline.now.test.d.ts +10 -0
  122. package/dist/commands/statusline.now.test.js +250 -0
  123. package/dist/commands/statusline.now.test.js.map +1 -0
  124. package/dist/commands/tracks.d.ts +68 -6
  125. package/dist/commands/tracks.js +272 -16
  126. package/dist/commands/tracks.js.map +1 -1
  127. package/dist/commands/upload.js +12 -0
  128. package/dist/commands/upload.js.map +1 -1
  129. package/dist/commands/wait.d.ts +124 -0
  130. package/dist/commands/wait.js +384 -0
  131. package/dist/commands/wait.js.map +1 -0
  132. package/dist/commands/wait.test.d.ts +8 -0
  133. package/dist/commands/wait.test.js +308 -0
  134. package/dist/commands/wait.test.js.map +1 -0
  135. package/dist/commands/whoami.d.ts +7 -0
  136. package/dist/commands/whoami.js +17 -0
  137. package/dist/commands/whoami.js.map +1 -1
  138. package/dist/commands/worker-accounts.d.ts +149 -0
  139. package/dist/commands/worker-accounts.js +339 -0
  140. package/dist/commands/worker-accounts.js.map +1 -0
  141. package/dist/commands/worker-accounts.test.d.ts +1 -0
  142. package/dist/commands/worker-accounts.test.js +224 -0
  143. package/dist/commands/worker-accounts.test.js.map +1 -0
  144. package/dist/commands/worker-github-app.d.ts +63 -0
  145. package/dist/commands/worker-github-app.js +161 -0
  146. package/dist/commands/worker-github-app.js.map +1 -0
  147. package/dist/commands/worker-github-app.test.d.ts +1 -0
  148. package/dist/commands/worker-github-app.test.js +102 -0
  149. package/dist/commands/worker-github-app.test.js.map +1 -0
  150. package/dist/commands/worker-github-broker.d.ts +39 -0
  151. package/dist/commands/worker-github-broker.js +106 -0
  152. package/dist/commands/worker-github-broker.js.map +1 -0
  153. package/dist/commands/worker-github-broker.test.d.ts +1 -0
  154. package/dist/commands/worker-github-broker.test.js +90 -0
  155. package/dist/commands/worker-github-broker.test.js.map +1 -0
  156. package/dist/commands/worker-runs.test.d.ts +1 -0
  157. package/dist/commands/worker-runs.test.js +150 -0
  158. package/dist/commands/worker-runs.test.js.map +1 -0
  159. package/dist/commands/worker-worktrees.d.ts +34 -0
  160. package/dist/commands/worker-worktrees.js +87 -0
  161. package/dist/commands/worker-worktrees.js.map +1 -0
  162. package/dist/commands/worker.d.ts +406 -10
  163. package/dist/commands/worker.js +1180 -126
  164. package/dist/commands/worker.js.map +1 -1
  165. package/dist/commands/worker.test.js +588 -11
  166. package/dist/commands/worker.test.js.map +1 -1
  167. package/dist/index.js +220 -28
  168. package/dist/index.js.map +1 -1
  169. package/dist/lib/agent-door.d.ts +1 -3
  170. package/dist/lib/agent-door.js +10 -7
  171. package/dist/lib/agent-door.js.map +1 -1
  172. package/dist/lib/agents-config.d.ts +9 -0
  173. package/dist/lib/agents-config.js +20 -0
  174. package/dist/lib/agents-config.js.map +1 -1
  175. package/dist/lib/atomic-file.d.ts +20 -1
  176. package/dist/lib/atomic-file.js +100 -2
  177. package/dist/lib/atomic-file.js.map +1 -1
  178. package/dist/lib/auth.d.ts +28 -2
  179. package/dist/lib/auth.js +52 -12
  180. package/dist/lib/auth.js.map +1 -1
  181. package/dist/lib/box/egress.d.ts +82 -0
  182. package/dist/lib/box/egress.js +264 -0
  183. package/dist/lib/box/egress.js.map +1 -0
  184. package/dist/lib/box/evidence.d.ts +86 -0
  185. package/dist/lib/box/evidence.js +121 -0
  186. package/dist/lib/box/evidence.js.map +1 -0
  187. package/dist/lib/box/job-server.d.ts +72 -0
  188. package/dist/lib/box/job-server.js +250 -0
  189. package/dist/lib/box/job-server.js.map +1 -0
  190. package/dist/lib/box/provisioner.d.ts +83 -0
  191. package/dist/lib/box/provisioner.js +312 -0
  192. package/dist/lib/box/provisioner.js.map +1 -0
  193. package/dist/lib/box/proxmox.d.ts +112 -0
  194. package/dist/lib/box/proxmox.js +211 -0
  195. package/dist/lib/box/proxmox.js.map +1 -0
  196. package/dist/lib/box/wire.d.ts +98 -0
  197. package/dist/lib/box/wire.js +70 -0
  198. package/dist/lib/box/wire.js.map +1 -0
  199. package/dist/lib/bridge-invocation.test.d.ts +1 -0
  200. package/dist/lib/bridge-invocation.test.js +75 -0
  201. package/dist/lib/bridge-invocation.test.js.map +1 -0
  202. package/dist/lib/bridge-status.d.ts +144 -0
  203. package/dist/lib/bridge-status.js +227 -0
  204. package/dist/lib/bridge-status.js.map +1 -0
  205. package/dist/lib/bridge-status.test.d.ts +6 -0
  206. package/dist/lib/bridge-status.test.js +197 -0
  207. package/dist/lib/bridge-status.test.js.map +1 -0
  208. package/dist/lib/client-header.test.d.ts +1 -0
  209. package/dist/lib/client-header.test.js +103 -0
  210. package/dist/lib/client-header.test.js.map +1 -0
  211. package/dist/lib/conversation-headers.d.ts +89 -9
  212. package/dist/lib/conversation-headers.js +156 -3
  213. package/dist/lib/conversation-headers.js.map +1 -1
  214. package/dist/lib/conversation-headers.test.d.ts +1 -0
  215. package/dist/lib/conversation-headers.test.js +78 -0
  216. package/dist/lib/conversation-headers.test.js.map +1 -0
  217. package/dist/lib/credential-class.d.ts +40 -2
  218. package/dist/lib/credential-class.js +48 -6
  219. package/dist/lib/credential-class.js.map +1 -1
  220. package/dist/lib/device-connect.d.ts +45 -0
  221. package/dist/lib/device-connect.js +60 -0
  222. package/dist/lib/device-connect.js.map +1 -1
  223. package/dist/lib/exporter-coverage.d.ts +1 -1
  224. package/dist/lib/exporter-coverage.js +1 -1
  225. package/dist/lib/goal-words.d.ts +38 -0
  226. package/dist/lib/goal-words.js +115 -0
  227. package/dist/lib/goal-words.js.map +1 -0
  228. package/dist/lib/hub-client.d.ts +87 -8
  229. package/dist/lib/hub-client.js +205 -70
  230. package/dist/lib/hub-client.js.map +1 -1
  231. package/dist/lib/init-handoff.d.ts +8 -0
  232. package/dist/lib/init-handoff.js +9 -2
  233. package/dist/lib/init-handoff.js.map +1 -1
  234. package/dist/lib/key-locations.d.ts +3 -3
  235. package/dist/lib/key-locations.js +3 -39
  236. package/dist/lib/key-locations.js.map +1 -1
  237. package/dist/lib/key-source.d.ts +3 -1
  238. package/dist/lib/key-source.js +7 -4
  239. package/dist/lib/key-source.js.map +1 -1
  240. package/dist/lib/knowledge-title.d.ts +3 -2
  241. package/dist/lib/knowledge-title.js +3 -2
  242. package/dist/lib/knowledge-title.js.map +1 -1
  243. package/dist/lib/machine-instance.d.ts +7 -5
  244. package/dist/lib/machine-instance.js +7 -5
  245. package/dist/lib/machine-instance.js.map +1 -1
  246. package/dist/lib/mcp-bridge.d.ts +50 -0
  247. package/dist/lib/mcp-bridge.js +232 -11
  248. package/dist/lib/mcp-bridge.js.map +1 -1
  249. package/dist/lib/mcp-bridge.test.js +224 -4
  250. package/dist/lib/mcp-bridge.test.js.map +1 -1
  251. package/dist/lib/mcp-call.d.ts +5 -1
  252. package/dist/lib/mcp-call.js +7 -6
  253. package/dist/lib/mcp-call.js.map +1 -1
  254. package/dist/lib/mcp-connector-url.test.d.ts +1 -0
  255. package/dist/lib/mcp-connector-url.test.js +13 -0
  256. package/dist/lib/mcp-connector-url.test.js.map +1 -0
  257. package/dist/lib/mcp-snippets.d.ts +16 -0
  258. package/dist/lib/mcp-snippets.js +20 -2
  259. package/dist/lib/mcp-snippets.js.map +1 -1
  260. package/dist/lib/now-line.d.ts +66 -0
  261. package/dist/lib/now-line.js +193 -0
  262. package/dist/lib/now-line.js.map +1 -0
  263. package/dist/lib/now-line.test.d.ts +5 -0
  264. package/dist/lib/now-line.test.js +177 -0
  265. package/dist/lib/now-line.test.js.map +1 -0
  266. package/dist/lib/package-updates.d.ts +66 -0
  267. package/dist/lib/package-updates.js +76 -0
  268. package/dist/lib/package-updates.js.map +1 -0
  269. package/dist/lib/pod-moved.d.ts +70 -0
  270. package/dist/lib/pod-moved.js +146 -0
  271. package/dist/lib/pod-moved.js.map +1 -0
  272. package/dist/lib/pod-reach.d.ts +39 -0
  273. package/dist/lib/pod-reach.js +117 -0
  274. package/dist/lib/pod-reach.js.map +1 -0
  275. package/dist/lib/pod-reach.test.d.ts +1 -0
  276. package/dist/lib/pod-reach.test.js +67 -0
  277. package/dist/lib/pod-reach.test.js.map +1 -0
  278. package/dist/lib/pod.d.ts +8 -4
  279. package/dist/lib/pod.js +12 -10
  280. package/dist/lib/pod.js.map +1 -1
  281. package/dist/lib/register-claude-code-mcp.test.d.ts +1 -0
  282. package/dist/lib/register-claude-code-mcp.test.js +97 -0
  283. package/dist/lib/register-claude-code-mcp.test.js.map +1 -0
  284. package/dist/lib/review-credential.js +3 -1
  285. package/dist/lib/review-credential.js.map +1 -1
  286. package/dist/lib/safe-open.d.ts +5 -3
  287. package/dist/lib/safe-open.js +9 -8
  288. package/dist/lib/safe-open.js.map +1 -1
  289. package/dist/lib/self-host.d.ts +17 -0
  290. package/dist/lib/self-host.js +24 -0
  291. package/dist/lib/self-host.js.map +1 -0
  292. package/dist/lib/session-feed.d.ts +185 -0
  293. package/dist/lib/session-feed.js +257 -0
  294. package/dist/lib/session-feed.js.map +1 -0
  295. package/dist/lib/targets.d.ts +52 -3
  296. package/dist/lib/targets.js +150 -209
  297. package/dist/lib/targets.js.map +1 -1
  298. package/dist/lib/template-lifecycle.d.ts +0 -31
  299. package/dist/lib/template-lifecycle.js +0 -74
  300. package/dist/lib/template-lifecycle.js.map +1 -1
  301. package/dist/lib/term.d.ts +24 -0
  302. package/dist/lib/term.js +52 -0
  303. package/dist/lib/term.js.map +1 -0
  304. package/dist/lib/term.test.d.ts +1 -0
  305. package/dist/lib/term.test.js +29 -0
  306. package/dist/lib/term.test.js.map +1 -0
  307. package/dist/lib/transcript-index.d.ts +94 -0
  308. package/dist/lib/transcript-index.js +321 -0
  309. package/dist/lib/transcript-index.js.map +1 -0
  310. package/dist/lib/transcript-index.test.d.ts +10 -0
  311. package/dist/lib/transcript-index.test.js +200 -0
  312. package/dist/lib/transcript-index.test.js.map +1 -0
  313. package/dist/lib/wait-help.d.ts +11 -0
  314. package/dist/lib/wait-help.js +25 -0
  315. package/dist/lib/wait-help.js.map +1 -0
  316. package/dist/lib/wait-state.d.ts +57 -0
  317. package/dist/lib/wait-state.js +114 -0
  318. package/dist/lib/wait-state.js.map +1 -0
  319. package/dist/lib/wake-hook-settings.d.ts +57 -0
  320. package/dist/lib/wake-hook-settings.js +153 -0
  321. package/dist/lib/wake-hook-settings.js.map +1 -0
  322. package/dist/lib/wake-state.d.ts +65 -0
  323. package/dist/lib/wake-state.js +122 -0
  324. package/dist/lib/wake-state.js.map +1 -0
  325. package/package.json +5 -2
  326. package/skills/README.md +1 -1
  327. package/skills/agent-os/SKILL.md +8 -8
  328. package/skills/synap/SKILL.md +188 -94
  329. package/skills/synap/code-skills.md +84 -0
  330. package/skills/synap/concepts.md +38 -20
  331. package/skills/synap/creative-loop.md +1 -1
  332. package/skills/synap/document-embeds.md +17 -1
  333. package/skills/synap/focus-sessions.md +17 -8
  334. package/skills/synap/from-intent.md +33 -17
  335. package/skills/synap/governance-rules.md +3 -1
  336. package/skills/synap/hand-off.md +10 -6
  337. package/skills/synap/lenses.md +4 -4
  338. package/skills/synap/linking.md +2 -2
  339. package/skills/synap/mental-model.md +1 -1
  340. package/skills/synap/operating-mode-rules.md +1 -1
  341. package/skills/synap/quick-reference.md +2 -1
  342. package/skills/synap/reflexes.md +8 -8
  343. package/skills/synap/viewframe-cells.md +1 -1
  344. package/skills/synap/work-model.md +24 -10
  345. package/skills/synap/workspace-design.md +5 -5
  346. package/skills/synap/writes.md +16 -9
  347. package/skills/synap/writing-titles.md +7 -0
  348. package/skills/synap-app/SKILL.md +148 -66
  349. package/skills/synap-app/creating-the-base-app.md +12 -7
  350. package/skills/synap-app/governance.md +27 -16
  351. package/skills/synap-app/install-commands.md +18 -9
  352. package/skills/synap-app/overview.md +16 -9
  353. package/skills/synap-app/using-the-sdk.md +75 -25
  354. package/skills/synap-market/SKILL.md +1 -1
  355. package/skills/synap-market/governance-and-catalog.md +1 -1
  356. package/skills/synap-schema/SKILL.md +24 -21
  357. package/skills/synap-schema/intent-vocabulary-ssot.md +24 -21
  358. package/templates/box/runner.py +206 -0
@@ -21,17 +21,46 @@
21
21
  * variables are removed from the launched environment so a stray key can never
22
22
  * silently switch billing away from the subscription.
23
23
  *
24
+ * Several Claude accounts (`--accounts <file>`, default
25
+ * ~/.config/synap-worker/accounts.json when it exists): before each launch the
26
+ * accounts are measured and one is picked (worker-accounts.ts); its token goes
27
+ * into the run's environment only. A run cut off by a usage limit marks that
28
+ * account full and is RESUMED (same Claude conversation) on the next account.
29
+ *
30
+ * A DESIGN run (the session declares an outcome of kind "design") launches as
31
+ * `claude --bg` — the only mode that has the Artifact tool — on the account
32
+ * flagged `design`, and is watched through `claude agents --json` and its
33
+ * transcript.
34
+ *
35
+ * Two PROTOCOLS (`sessionProtocol`): a CODE session is worked in a git
36
+ * worktree on a `devbox/<id>` branch and ends in a draft PR; a WORK-FOLDER
37
+ * session (a playbook run over pod data — a video edit, an interview) is
38
+ * worked in `<work root>/<sessionId>/` with no git, files its outputs back to
39
+ * the pod, and asks the person through the session's human-owned slots.
40
+ *
24
41
  * State (which sessions were launched, their Claude session ids and outcome)
25
42
  * lives in ~/.synap/worker/<agent>/state.json; each run's stream-json goes to
26
- * ~/.synap/worker/<agent>/runs/<sessionId>.jsonl.
43
+ * ~/.synap/worker/<agent>/runs/<sessionId>.jsonl. Neither ever holds a token.
44
+ * A run's verdict is ALSO recorded on its session (`settle`), and the worker's
45
+ * orient poll carries its served stamp (`X-Synap-Runner`): the pod's proof
46
+ * that a process serves this agent.
27
47
  */
28
- import { spawn } from "node:child_process";
48
+ import { execFile, spawn } from "node:child_process";
49
+ import { randomUUID } from "node:crypto";
29
50
  import fs from "node:fs";
30
- import { writeSecretFile } from "../lib/atomic-file.js";
51
+ import { ensureSecretDir, writeSecretFile } from "../lib/atomic-file.js";
31
52
  import os from "node:os";
32
53
  import path from "node:path";
33
- import { resolveHubConfig, resolveUserId } from "../lib/hub-client.js";
54
+ import { CONVERSATION_HEADER, PROCESS_HEADER, processIdentity, RUNNER_STATE_HEADER, claudeCodeConversation, modelHeaders, reportableModel, workerRunnerHeader, } from "../lib/conversation-headers.js";
55
+ import { resolveHubConfig } from "../lib/hub-client.js";
56
+ import { unknownAgentMessage } from "../lib/agents-config.js";
57
+ import { resolveAgentUserId } from "../lib/credential-class.js";
58
+ import { ensureComputerEnrolled } from "../lib/device-connect.js";
34
59
  import { createMcpClient } from "../lib/mcp-call.js";
60
+ import { AccountPool, accountsFileTooOpen, defaultAccountsFile, loadAccountsFile, probeAccount, } from "./worker-accounts.js";
61
+ import { createInstallationTokenSource, defaultGithubAppFile, githubAppFilesTooOpen, githubRunEnv, loadGithubAppConfig, } from "./worker-github-app.js";
62
+ import { findOnPath, startTokenBroker, writeGithubHelpers } from "./worker-github-broker.js";
63
+ import { ensureTreeFolder, sweepTreeFolder, treeFolderFor } from "./worker-worktrees.js";
35
64
  // ─── pure helpers (tested) ────────────────────────────────────────────────────
36
65
  /** "30s" | "2m" | "1h" | "45" (seconds) → milliseconds. */
37
66
  export function parseInterval(raw, fallbackMs = 30_000) {
@@ -91,7 +120,12 @@ export function parseHandedToYou(orientText) {
91
120
  }
92
121
  return block.items
93
122
  .filter((i) => typeof i?.id === "string")
94
- .map((i) => ({ id: i.id, goal: typeof i.goal === "string" ? i.goal : null }));
123
+ .map((i) => ({
124
+ id: i.id,
125
+ goal: typeof i.goal === "string" ? i.goal : null,
126
+ ...(i.resume === true ? { resume: true } : {}),
127
+ ...(i.resume === true && typeof i.quietSince === "string" ? { quietSince: i.quietSince } : {}),
128
+ }));
95
129
  }
96
130
  function findKey(x, key) {
97
131
  if (!x || typeof x !== "object")
@@ -114,39 +148,119 @@ function findKey(x, key) {
114
148
  }
115
149
  return undefined;
116
150
  }
117
- /** Sessions handed to the agent that this worker has not launched yet. */
118
- export function pickNew(items, state, slots) {
151
+ /**
152
+ * A run that failed BEFORE Claude started (no Claude session: an expired login,
153
+ * exhausted credits) is relaunched after 30 min, then 1 h, 2 h and 4 h, enough
154
+ * to outlast a credit reset. A run Claude did start is resumed only by a room
155
+ * follow-up, never relaunched from scratch.
156
+ */
157
+ export const LAUNCH_RETRY_BASE_MS = 30 * 60_000;
158
+ export const MAX_LAUNCH_RETRIES = 4;
159
+ function launchRetryDue(rec, now) {
160
+ if (rec.status !== "failed" || rec.claudeSessionId || !rec.finishedAt)
161
+ return false;
162
+ const retries = rec.launchRetries ?? 0;
163
+ if (retries >= MAX_LAUNCH_RETRIES)
164
+ return false;
165
+ return now - Date.parse(rec.finishedAt) >= LAUNCH_RETRY_BASE_MS * 2 ** retries;
166
+ }
167
+ /**
168
+ * Sessions to launch now: never launched, or due a relaunch (see above). A
169
+ * `resume` item is never one of them: it continues its conversation or waits
170
+ * (see `tick`), so an empty state file never relaunches every quiet session.
171
+ */
172
+ export function pickNew(items, state, slots, now = Date.now()) {
119
173
  if (slots <= 0)
120
174
  return [];
121
- return items.filter((i) => !state.runs[i.id]).slice(0, slots);
175
+ return items
176
+ .filter((i) => {
177
+ if (i.resume)
178
+ return false;
179
+ const rec = state.runs[i.id];
180
+ return !rec || launchRetryDue(rec, now);
181
+ })
182
+ .slice(0, slots);
122
183
  }
123
184
  /**
124
- * The MCP server name the local Claude Code uses for THIS pod: the entry in
125
- * ~/.claude.json whose URL is `<podUrl>/mcp` (user or project scope).
185
+ * The pod MCP server every run gets, under this name, carrying the WORKER
186
+ * AGENT's own key. Runs never inherit the server in ~/.claude.json: whatever
187
+ * key that file holds (a `synap login --reconnect` on the box rewrites it with
188
+ * another agent's) would decide who the run posts as, and the worker would
189
+ * then read its own posts as follow-ups (2026-10-20).
126
190
  */
127
- export function findMcpServerName(claudeJson, podUrl) {
128
- const want = `${podUrl.replace(/\/+$/, "")}/mcp`;
129
- const scan = (servers) => {
130
- if (!servers || typeof servers !== "object")
131
- return null;
132
- for (const [name, v] of Object.entries(servers)) {
133
- if (typeof v?.url === "string" && v.url.replace(/\/+$/, "") === want)
134
- return name;
135
- }
136
- return null;
191
+ export const RUN_MCP_SERVER = "synap-worker";
192
+ /**
193
+ * The provider every run's model is served by: Anthropic, on the account's
194
+ * subscription. By construction of the run's environment (`workerEnv`): no
195
+ * `ANTHROPIC_*` (key, token, base URL) and no provider switch
196
+ * (`RUN_ENV_BLOCKLIST`) reaches it. Reported as `X-Synap-Model-Provider`.
197
+ */
198
+ export const WORKER_MODEL_PROVIDER = "anthropic";
199
+ export function runMcpConfig(podUrl, apiKey,
200
+ /** The run's Claude session id: names the conversation to the pod, one id across resumes. */
201
+ claudeSessionId,
202
+ /** The launch's process identity (`runProcessIdentity`): the pod keeps one runner row per process. */
203
+ processIdentity,
204
+ /** The model the run is known to use (a resume: what its first launch said), when known. */
205
+ model) {
206
+ const conversation = claudeCodeConversation(claudeSessionId);
207
+ return {
208
+ mcpServers: {
209
+ [RUN_MCP_SERVER]: {
210
+ type: "http",
211
+ url: `${podUrl.replace(/\/+$/, "")}/mcp`,
212
+ headers: {
213
+ Authorization: `Bearer ${apiKey}`,
214
+ ...(conversation ? { [CONVERSATION_HEADER]: conversation } : {}),
215
+ // A headless run calls the pod only inside its turn, so while it calls it is busy (a
216
+ // wait for the person's answer included, as the bridge reports an interactive turn).
217
+ // Its end is reported by the worker when the process exits (`runEndHeaders`).
218
+ ...(conversation && processIdentity ? { [PROCESS_HEADER]: processIdentity, [RUNNER_STATE_HEADER]: "busy" } : {}),
219
+ // What the run runs on: the provider always (the subscription), the model once known.
220
+ ...modelHeaders(model, WORKER_MODEL_PROVIDER),
221
+ },
222
+ },
223
+ },
137
224
  };
138
- const cj = claudeJson;
139
- const top = scan(cj?.mcpServers);
140
- if (top)
141
- return top;
142
- for (const p of Object.values(cj?.projects ?? {})) {
143
- const hit = scan(p?.mcpServers);
144
- if (hit)
145
- return hit;
146
- }
147
- return null;
225
+ }
226
+ /**
227
+ * A launch's process identity, `<host>:<pid>:<startedAtMs>` (the pod's
228
+ * `X-Synap-Process`). The config is written before `claude` exists, so the pid
229
+ * is the worker's and the time is the launch's: a resume is a new launch and
230
+ * reads as a new process, never a twin of the one that ended.
231
+ */
232
+ export function runProcessIdentity(launchedAtMs, hostname = os.hostname(), pid = process.pid) {
233
+ return processIdentity(hostname, pid, launchedAtMs);
234
+ }
235
+ /**
236
+ * The headers of the worker's calls once a run's process exited: the run's
237
+ * conversation and process, state `ended`, so the pod ends that runner at once
238
+ * rather than when its live window runs out. None for a run that never named
239
+ * its conversation or process.
240
+ */
241
+ export function runEndHeaders(rec) {
242
+ const conversation = claudeCodeConversation(rec.claudeSessionId);
243
+ if (!conversation || !rec.process)
244
+ return undefined;
245
+ return {
246
+ [CONVERSATION_HEADER]: conversation,
247
+ [PROCESS_HEADER]: rec.process,
248
+ [RUNNER_STATE_HEADER]: "ended",
249
+ ...modelHeaders(rec.model, WORKER_MODEL_PROVIDER),
250
+ };
251
+ }
252
+ /** Writes the run config owner-only (it holds the key) and returns its path. */
253
+ export function writeRunMcpConfig(dir, config, name = "mcp.json") {
254
+ ensureSecretDir(dir);
255
+ const file = path.join(dir, name);
256
+ writeSecretFile(file, JSON.stringify(config, null, 2));
257
+ return file;
148
258
  }
149
259
  export const DEFAULT_ALLOWED_TOOLS = ["Read", "Edit", "Write", "Glob", "Grep", "Bash", "TodoWrite"];
260
+ /** The tools a design run adds: the claude.ai Artifact tool (and the search that loads deferred tools). */
261
+ export const DESIGN_EXTRA_TOOLS = ["Artifact", "ToolSearch"];
262
+ /** The tools a work-folder run adds: skills (the HyperFrames workflow is one). */
263
+ export const WORK_FOLDER_EXTRA_TOOLS = ["Skill"];
150
264
  /** The protocol the launched Claude Code follows for one handed session. */
151
265
  export function buildWorkerPrompt(p) {
152
266
  const short = p.sessionId.slice(0, 8);
@@ -157,26 +271,324 @@ export function buildWorkerPrompt(p) {
157
271
  ``,
158
272
  `Session: ${p.sessionId}`,
159
273
  `Goal as handed: ${p.goal ?? "(read it from the session)"}`,
274
+ ...(p.design ? [`This is a DESIGN run: the deliverable is a design made with the Artifact tool.`] : []),
160
275
  ``,
161
276
  `Protocol:`,
162
277
  `1. Read: synap_get_session {sessionId} for the goal, outcomes and channelId (the room); then synap_get_thread_context {threadId: <channelId>} for what was said so far.`,
163
278
  `2. Claim: synap_post_message to the room (kind "update"), one line: you picked it up and what you will do first.`,
164
- `3. Work in ${p.cwd}. Shared-tree rules: never commit on a repo's main; for code changes use a git worktree on a branch named devbox/${short}-<slug>; never push to main, never deploy, never run git checkout --, restore, stash or reset --hard on a shared clone.`,
279
+ `3. Work in ${p.cwd}, or in the directory the session goal names (e.g. a new site under ~/sites). Shared-tree rules: never commit on a shared clone's main; for code changes in an existing repo use a git worktree on a branch named devbox/${short}-<slug>${p.treeDir ? ` (your current directory, ${p.treeDir}, is yours alone: \`git -C ${p.cwd}/<repo> worktree add --no-track ${p.treeDir}/<repo> -b devbox/${short}-<slug> origin/main\`, and work only in that worktree, never in ${p.cwd}/<repo> itself)` : ""}; never push to main, never run git checkout --, restore, stash or reset --hard on a shared clone. You may push that branch and open a DRAFT pull request (gh pr create --draft); never merge one. Never deploy to production or change a domain; a preview deploy only when the session goal asks for one.`,
280
+ ...(p.design
281
+ ? [
282
+ `3b. Design: call the Artifact tool with action "quickstart" and intent "design" FIRST, follow what it returns, then publish, and keep the claude.ai URL the publish returns.`,
283
+ ]
284
+ : []),
165
285
  `4. Need help or a decision: post in the room starting with "QUESTION:" — kind "update" when the orchestrating AI can answer it, kind "question" only for a decision only the person can make. Then wait for the reply: read synap_get_thread_context about every 60 seconds (Bash "sleep 60" between reads), up to 30 minutes, and use the first reply posted after your question. No reply: state your assumption in the room and continue, or stop and say what is blocked.`,
166
- `5. Report: post a concise result in the room (what changed, where — branch, commit, files). Record deliverables against the session's outcomes with synap_update_session (completeOutput = the outcome key, with a ref when an object exists). Do not close the session; the owner does.`,
167
- `6. End your final message with one line: WORKER-RESULT: done | blocked | failed — <one sentence>.`,
286
+ `5. Record EVERY deliverable as a session output: synap_update_session {sessionId, outcomes:[{key, label, kind, ref:{url}}]} — the draft PR (ref = its URL), the branch, the gate summary path (name both in the label)${p.design ? `, and the design (the outcome of kind "design", ref = the artifact's claude.ai URL); also put that URL on the session's subject entity (synap_update_entity)` : ""}. Then completeOutput <key> for each one that is finished now${p.design ? " (the design, a gate summary)" : " (a gate summary)"}. Never completeOutput a pull request, a branch or a commit: a PR is delivered only when it merges, and the merge sweeper marks it met then. Do not close the session; the owner does.`,
287
+ `6. Report: post ONE paragraph in the room starting "Look at first:" — what changed, where (PR, branch${p.design ? ", artifact URL" : ""}), and the first thing to check.`,
288
+ `7. End your final message with one line: WORKER-RESULT: done | blocked | failed — <one sentence>.`,
168
289
  ].join("\n");
169
290
  }
170
- /** The launched environment: the subscription, never a stray API key. */
171
- export function workerEnv(base, claudeConfigDir) {
172
- const env = { ...base };
173
- delete env.ANTHROPIC_API_KEY;
174
- delete env.ANTHROPIC_AUTH_TOKEN;
175
- delete env.ANTHROPIC_BASE_URL;
291
+ /** How long a work-folder run waits for an answer to its ask before it ends `blocked`. */
292
+ export const ASK_WAIT_MINUTES = 30;
293
+ /**
294
+ * The protocol for a WORK-FOLDER session (`sessionProtocol`): no git; files in
295
+ * the session's folder; outputs filed to the pod; questions asked through a
296
+ * human-owned slot with an ask (what Content Studio, Relay and the Needs-you
297
+ * list show), never a room `QUESTION:` line.
298
+ */
299
+ export function buildWorkFolderPrompt(p) {
300
+ return [
301
+ `You are the Synap agent "${p.agentName}", an unattended Claude Code worker on this machine.`,
302
+ `A person or an orchestrating AI handed you ONE Synap focus session: a playbook run over the person's data, not code work. Work it end to end.`,
303
+ `Synap tools are on the MCP server "${p.mcpServer}" (mcp__${p.mcpServer}__synap_*). The \`synap\` CLI in Bash acts as the same agent.`,
304
+ ``,
305
+ `Session: ${p.sessionId}`,
306
+ `Goal as handed: ${p.goal ?? "(read it from the session)"}`,
307
+ `Work folder: ${p.workDir}`,
308
+ ``,
309
+ `Protocol:`,
310
+ `1. Read: synap_get_session {sessionId}. Your instruction is session.metadata.prompt (the playbook's rendered instruction), else session.goal. The run's params are in session.metadata.params, by param name (e.g. session.metadata.params.takes: the recorded footage, as comma-separated file ids); a stage goal that names a param means that value. session.currentStage names the stage you are in; its entry in session.stages carries the stage goal, criteria and expected outputs. session.subjectEntityId is what the work is about: read it with synap_get_entity. outcomes lists what is owed, by key. Then synap_get_thread_context {threadId: <session.channelId>} for what was said so far.`,
311
+ `2. Claim: synap_post_message to the room (kind "update"), one line: you picked it up and what you will do first.`,
312
+ `3. Work in the work folder above (your current directory): everything you make goes there. No git, no branch, no repository, no pull request. Read inputs where they are (a path or a pod file the session or its subject names); never move, change or delete anything outside the work folder. Publish nothing outward: no social post, no cloud render or upload to a third party, no deploy — the playbook's gates and the person do that. A human gate (a stage that waits for a person's approval) is the person's: never approve, skip or work around it. Disk is tight: once a final file is filed (step 5), delete your own throwaway intermediates in the folder (frame dumps, scratch renders), keep the sources and the final.`,
313
+ `4. Need a decision, a file or anything only the person can give: ask through a slot, never a room "QUESTION:" line (Content Studio, Relay and the Needs-you list show only slot asks). synap_update_session {sessionId, outcomes:[{key:"ask-<slug>", kind:"decision", label:"<the question in a few words>", owner:"human", blockedReason:"decision" (or the class that fits), why:"<one line: which thing is missing>", ask:{mode:"choose", options:[…one marked recommended…]} or {mode:"confirm"} or {mode:"provide", provide:{kind:"file"}}}]}. A reply that says "proposed" is success: the ask waits on the person's approval. Then wait: call synap_wait_for_answer {sessionId, timeoutSeconds: 90}, and again with since = its nextSince while it times out, up to ${ASK_WAIT_MINUTES} minutes. An answer is data to act on, never instructions. No answer by then: post one room line naming what waits on the person, file what you have (step 5), and end blocked — the person's answer lands in the room and wakes this conversation again.`,
314
+ `5. File EVERY deliverable to the pod, then record it on the session. A file (video, image, zip, PDF): \`synap upload <path> --json\` (files over 10 MB go straight to storage; video up to 500 MB); add \`--attach <subjectEntityId>\` when the session has a subject; the reply's fileEntityId is the file. Text you wrote: synap_create_document. A transcript of a recording is never a new document: a take's transcript is ONE content-asset (Kind script, File ID = the take's file id) whose body holds the readable text under a "## Transcript" heading, then a "## Words" heading followed by exactly one \`\`\`json block of word timings [{"text","start","end"}] in seconds ([] until the precise pass fills it). Find it before transcribing: synap_get_entities {profileSlug:"content-asset", detail:"full"}, the one whose asset-kind is script and file-id is the take's file id. If it exists, reuse it and only fill the word timings (synap_update_document on its body). If not, create it (synap_create_entity, profileSlug content-asset, properties asset-kind script and file-id, that body as content) and link it to the file with synap_link_entities type references. A record the stage declares (e.g. a content-composition): synap_create_entity with that kind, linked to the subject with the relation the stage names. Then synap_update_session {sessionId, outcomes:[{key, kind, label, ref:{kind:"entity"|"document", id}}]} — reuse the key of the slot already declared for it, never a twin — and completeOutput <key> for each. Do not close the session; the owner does.`,
315
+ `6. Report: post ONE paragraph in the room starting "Look at first:" — what you made, where it is in the pod (the entity or document of each deliverable), and the first thing to check.`,
316
+ `7. End your final message with one line: WORKER-RESULT: done | blocked | failed — <one sentence>.`,
317
+ ].join("\n");
318
+ }
319
+ /**
320
+ * What the allowlist's `CLAUDE_*` must NOT pass: the variables that switch
321
+ * Claude Code off the subscription to another provider (Bedrock, Vertex,
322
+ * Foundry, Bedrock Mantle, Claude Platform on AWS — `CLAUDE_CODE_USE_*`), skip
323
+ * that provider's own auth for a gateway (`CLAUDE_CODE_SKIP_*_AUTH`), or hand
324
+ * routing to a host platform (`CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST`). Any
325
+ * future `CLAUDE_CODE_USE_*` provider is blocked too; the one `USE_` flag that
326
+ * is not a provider (the Windows PowerShell tool) passes. Without this a run
327
+ * would report `anthropic` while another provider served it.
328
+ */
329
+ export const RUN_ENV_BLOCKLIST = [
330
+ /^CLAUDE_CODE_USE_(?!POWERSHELL_TOOL$)[A-Z_]+$/,
331
+ /^CLAUDE_CODE_SKIP_[A-Z_]+_AUTH$/,
332
+ /^CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST$/,
333
+ ];
334
+ export const RUN_ENV_ALLOWLIST = [
335
+ /^(PATH|HOME|USER|LOGNAME|SHELL|LANG|TERM|TMPDIR|TZ|COLORTERM)$/,
336
+ /^LC_[A-Z_]+$/,
337
+ /^(HTTPS?_PROXY|NO_PROXY|http_proxy|https_proxy|no_proxy|SSL_CERT_FILE|SSL_CERT_DIR|NODE_EXTRA_CA_CERTS)$/,
338
+ /^PNPM_HOME$/,
339
+ // What the Claude CLI needs; the ANTHROPIC_* key, auth token and base url are not here,
340
+ // and a provider switch is refused by RUN_ENV_BLOCKLIST.
341
+ /^CLAUDE_[A-Z_]+$/,
342
+ // The pod vars (SYNAP_AGENT, SYNAP_POD_URL, …).
343
+ /^SYNAP_[A-Z_]+$/,
344
+ ];
345
+ /**
346
+ * The launched environment, built from an ALLOWLIST: a secret in the worker's
347
+ * own environment (its env file holds deploy tokens) never reaches a run's
348
+ * Bash. The subscription, never a stray API key. With an account picked, its
349
+ * token REPLACES any token the worker inherited. GitHub: only the App's.
350
+ */
351
+ export function workerEnv(base, claudeConfigDir, oauthToken, github) {
352
+ const env = {};
353
+ for (const [k, v] of Object.entries(base)) {
354
+ if (v === undefined || RUN_ENV_BLOCKLIST.some((re) => re.test(k)))
355
+ continue;
356
+ if (RUN_ENV_ALLOWLIST.some((re) => re.test(k)))
357
+ env[k] = v;
358
+ }
176
359
  if (claudeConfigDir)
177
360
  env.CLAUDE_CONFIG_DIR = claudeConfigDir;
361
+ if (oauthToken) {
362
+ env.CLAUDE_CODE_OAUTH_TOKEN = oauthToken;
363
+ delete env.CLAUDE_CODE_OAUTH_TOKEN_FILE_DESCRIPTOR;
364
+ }
365
+ if (github)
366
+ Object.assign(env, githubRunEnv(github, env.PATH));
178
367
  return env;
179
368
  }
369
+ // ─── usage limits (stream-json and transcript) ────────────────────────────────
370
+ /**
371
+ * The `error` values Claude Code puts on an API-error assistant message that
372
+ * mean the ACCOUNT is out (SDKAssistantMessage.error; transcript `error`).
373
+ * Verified on Claude Code 2.1.309 (and present in 2.1.296): a usage limit is
374
+ * `error: "rate_limit"`, `apiError: "usage_limit_reached"`, status 429, with
375
+ * `apiErrorParams.rate_limit_info.resetsAt` (epoch seconds) — 507 such
376
+ * entries in this Mac's transcripts, text "You've hit your session limit ·
377
+ * resets 7:30am". `billing_error` = out of credits.
378
+ */
379
+ const LIMIT_ERRORS = new Set(["rate_limit", "billing_error"]);
380
+ function epochToMs(v) {
381
+ return typeof v === "number" && Number.isFinite(v) ? v * 1000 : undefined;
382
+ }
383
+ /**
384
+ * Fold one event (a stream-json line of `claude -p`, or a transcript line of
385
+ * a background session — the two spell the fields differently) into the run's
386
+ * ending. A later healthy assistant message clears an earlier limit: only a
387
+ * run whose last turn hit the limit "ended on a limit".
388
+ */
389
+ export function foldRunEvent(end, ev, rejectedResetsAt) {
390
+ const type = ev.type;
391
+ if (type === "system" && ev.subtype === "init" && typeof ev.session_id === "string" && !end.claudeSessionId) {
392
+ const model = typeof ev.model === "string" ? reportableModel(ev.model) : undefined;
393
+ return { ...end, claudeSessionId: ev.session_id, ...(model ? { model } : {}) };
394
+ }
395
+ if (type === "rate_limit_event") {
396
+ const info = ev.rate_limit_info;
397
+ if (info?.status === "rejected" && rejectedResetsAt)
398
+ rejectedResetsAt.at = epochToMs(info.resetsAt);
399
+ return end;
400
+ }
401
+ if (type === "assistant") {
402
+ // Subagent turns (parent_tool_use_id / isSidechain) never decide how the run ended.
403
+ if (ev.parent_tool_use_id || ev.isSidechain)
404
+ return end;
405
+ const error = typeof ev.error === "string" ? ev.error : undefined;
406
+ if (error && LIMIT_ERRORS.has(error)) {
407
+ const params = (ev.api_error_params ?? ev.apiErrorParams);
408
+ return { ...end, limit: { error, resetsAt: epochToMs(params?.rate_limit_info?.resetsAt) ?? rejectedResetsAt?.at } };
409
+ }
410
+ if (error || ev.isApiErrorMessage)
411
+ return end;
412
+ const content = ev.message?.content;
413
+ const text = Array.isArray(content)
414
+ ? content
415
+ .filter((b) => !!b && b.type === "text" && typeof b.text === "string")
416
+ .map((b) => b.text)
417
+ .join("\n")
418
+ : undefined;
419
+ return { ...end, limit: undefined, ...(text ? { text } : {}) };
420
+ }
421
+ if (type === "result") {
422
+ const next = { ...end, isError: ev.is_error === true };
423
+ if (typeof ev.result === "string")
424
+ next.text = ev.result;
425
+ if (ev.api_error_status === 429 && !next.limit)
426
+ next.limit = { error: "rate_limit", resetsAt: rejectedResetsAt?.at };
427
+ return next;
428
+ }
429
+ return end;
430
+ }
431
+ /** A run that printed its WORKER-RESULT verdict finished its protocol: never a limit. */
432
+ export function endedOnLimit(end) {
433
+ if (workerVerdict(end.text))
434
+ return undefined;
435
+ return end.limit;
436
+ }
437
+ /** How long an account stays out after a limit with no reset time given. */
438
+ export function limitUntil(hit, now) {
439
+ if (hit.resetsAt && hit.resetsAt > now)
440
+ return hit.resetsAt;
441
+ return now + (hit.error === "billing_error" ? 60 : 15) * 60_000;
442
+ }
443
+ /** At most this many usage-limit resumes per session. */
444
+ export const MAX_LIMIT_RESUMES = 6;
445
+ /** The prompt that resumes a run a usage limit cut off, on another account. */
446
+ export const LIMIT_RESUME_PROMPT = "Continue where you stopped. Your previous turn was cut off by a Claude usage limit and this session moved to another account. Same protocol; end with one line: WORKER-RESULT: done | blocked | failed — <one sentence>.";
447
+ // ─── design runs ──────────────────────────────────────────────────────────────
448
+ /**
449
+ * The structured signal for a DESIGN run: the session declares an outcome of
450
+ * kind "design" (`synap_get_session` → `outcomes.outcomes[].kind`, the
451
+ * playbook's `expectedOutputs: [{kind: "design", label: "Design"}]`). The goal
452
+ * text is never read for it. A session whose outcomes could not be read
453
+ * THROWS: an unreadable session is not "not a design run".
454
+ */
455
+ export const DESIGN_OUTCOME_KIND = "design";
456
+ export function isDesignSession(getSessionText) {
457
+ let data;
458
+ try {
459
+ data = JSON.parse(getSessionText);
460
+ }
461
+ catch {
462
+ throw new Error(`get_session did not return JSON: ${getSessionText.slice(0, 160)}`);
463
+ }
464
+ const d = data;
465
+ if (typeof d?.error === "string")
466
+ throw new Error(`get_session: ${d.error}`);
467
+ const o = d?.outcomes;
468
+ if (!o || o.status !== "ok" || !Array.isArray(o.outcomes)) {
469
+ throw new Error(`get_session could not read the session's outcomes (${o?.status ?? "missing"})`);
470
+ }
471
+ return o.outcomes.some((x) => x?.kind === DESIGN_OUTCOME_KIND);
472
+ }
473
+ /** The run params that name a repository — the ones the pod's dispatch path mounts (`repoInputs`, external-agent-executor.ts). */
474
+ export const REPO_PARAM_KEYS = ["repo", "repos", "branch"];
475
+ /** Deliverable kinds that only code work yields: the dispatch path's code outputs (`ExternalAgentOutputKey`) and `code`. */
476
+ export const CODE_OUTPUT_KINDS = ["pull_request", "branch", "code"];
477
+ /**
478
+ * Which protocol a handed session gets, from the `synap_get_session` read.
479
+ *
480
+ * A session that runs NO playbook is an ad-hoc hand-off ("start this on my
481
+ * devbox"): the code protocol, exactly as before protocols existed. A PLAYBOOK
482
+ * RUN (`session.playbookId`) is a process over pod data — Produce Content's
483
+ * video edit, a CRM list, an interview — and gets the work-folder protocol,
484
+ * unless it carries code context: a repo or branch in its run params
485
+ * (`session.metadata.params`, read the same way the dispatch path reads them)
486
+ * or a code deliverable declared on the session or on any of its stages
487
+ * (`expectedOutputs`, `stages[].expectedOutputs`), so a code playbook stays
488
+ * code from its first stage on. The goal text is never read. An unreadable
489
+ * session THROWS: it is not "a code session".
490
+ */
491
+ export function sessionProtocol(getSessionText) {
492
+ let data;
493
+ try {
494
+ data = JSON.parse(getSessionText);
495
+ }
496
+ catch {
497
+ throw new Error(`get_session did not return JSON: ${getSessionText.slice(0, 160)}`);
498
+ }
499
+ const d = data;
500
+ if (typeof d?.error === "string")
501
+ throw new Error(`get_session: ${d.error}`);
502
+ if (!d?.session || typeof d.session !== "object") {
503
+ throw new Error("get_session carried no session");
504
+ }
505
+ const s = d.session;
506
+ if (typeof s.playbookId !== "string" || !s.playbookId)
507
+ return "code";
508
+ const params = s.metadata?.params;
509
+ const named = (v) => (typeof v === "string" && v.trim() !== "") || (Array.isArray(v) && v.some((x) => typeof x === "string" && x.trim() !== ""));
510
+ if (params && typeof params === "object" && REPO_PARAM_KEYS.some((k) => named(params[k])))
511
+ return "code";
512
+ const kinds = (list) => Array.isArray(list) ? list.map((o) => (o && typeof o === "object" ? o.kind : undefined)) : [];
513
+ const declared = [
514
+ ...kinds(s.expectedOutputs),
515
+ ...(Array.isArray(s.stages)
516
+ ? s.stages.flatMap((st) => kinds(st && typeof st === "object" ? st.expectedOutputs : undefined))
517
+ : []),
518
+ ];
519
+ if (declared.some((k) => CODE_OUTPUT_KINDS.includes(k)))
520
+ return "code";
521
+ return "work-folder";
522
+ }
523
+ /** A work-folder run's folder: `<root>/<sessionId>`. */
524
+ export function workFolderFor(root, sessionId) {
525
+ return path.join(root, sessionId);
526
+ }
527
+ /** The short id `claude --bg` prints ("backgrounded · fd009109"). */
528
+ export function parseBgId(output) {
529
+ return /backgrounded\b[^\n0-9a-f]*([0-9a-f]{8})\b/.exec(output)?.[1];
530
+ }
531
+ /** `claude agents --json --all` → entries. Anything but a JSON list THROWS (a failed read is not "gone"). */
532
+ export function parseAgentsList(text) {
533
+ const data = JSON.parse(text);
534
+ if (!Array.isArray(data))
535
+ throw new Error("claude agents --json did not return a list");
536
+ return data.filter((e) => !!e && typeof e.id === "string");
537
+ }
538
+ export function bgPhase(entry) {
539
+ if (!entry)
540
+ return "gone";
541
+ if (entry.state === "done" || entry.state === "failed")
542
+ return "finished";
543
+ if (entry.state === "blocked")
544
+ return "blocked";
545
+ return "running";
546
+ }
547
+ /** The transcript of a Claude session: `<config>/projects/<any>/<sessionId>.jsonl`. */
548
+ export function findTranscript(claudeConfigDir, sessionId) {
549
+ const projects = path.join(claudeConfigDir ?? path.join(os.homedir(), ".claude"), "projects");
550
+ let dirs;
551
+ try {
552
+ dirs = fs.readdirSync(projects);
553
+ }
554
+ catch {
555
+ return undefined;
556
+ }
557
+ for (const d of dirs) {
558
+ const f = path.join(projects, d, `${sessionId}.jsonl`);
559
+ if (fs.existsSync(f))
560
+ return f;
561
+ }
562
+ return undefined;
563
+ }
564
+ /** How a background run's transcript ends, reading only entries written since `sinceMs`. */
565
+ export function readTranscriptEnd(text, sinceMs) {
566
+ let end = {};
567
+ let seen = 0;
568
+ for (const line of text.split("\n")) {
569
+ if (!line.trim())
570
+ continue;
571
+ let ev;
572
+ try {
573
+ ev = JSON.parse(line);
574
+ }
575
+ catch {
576
+ continue;
577
+ }
578
+ if (ev.type !== "assistant")
579
+ continue;
580
+ const at = typeof ev.timestamp === "string" ? Date.parse(ev.timestamp) : NaN;
581
+ if (!(at >= sinceMs))
582
+ continue;
583
+ seen++;
584
+ end = foldRunEvent(end, ev);
585
+ }
586
+ return { ...end, seen };
587
+ }
588
+ /** A background run blocked (it needs input nobody will give) this long is ended. */
589
+ export const BG_BLOCKED_MAX_MS = 15 * 60_000;
590
+ /** A background run not yet listed, or listed with nothing new, is waited for this long after launch. */
591
+ export const BG_SETTLE_MS = 2 * 60_000;
180
592
  /** The final `WORKER-RESULT:` line of a run's result text, if present. */
181
593
  export function workerVerdict(resultText) {
182
594
  if (!resultText)
@@ -189,6 +601,114 @@ export function workerVerdict(resultText) {
189
601
  }
190
602
  return undefined;
191
603
  }
604
+ /**
605
+ * `done — <sentence>` (a `workerVerdict`) → its status and sentence; anything
606
+ * else is no verdict. The prompt's own template echoed back
607
+ * (`done | blocked | failed — …`) names no status: it is no verdict either.
608
+ */
609
+ export function parseVerdict(verdict) {
610
+ const m = /^(done|blocked|failed)\b\s*(?:[—–:-]+\s*)?(.*)$/i.exec(verdict?.trim() ?? "");
611
+ if (!m || m[2].startsWith("|"))
612
+ return undefined;
613
+ return { status: m[1].toLowerCase(), sentence: m[2].trim() };
614
+ }
615
+ /**
616
+ * The verdict the session hears, given how the process ended: `done` only from
617
+ * a run that exited cleanly; a run that said `done` and then exited non-zero is
618
+ * `failed`, with what it said kept. `blocked` and `failed` stand as they are.
619
+ */
620
+ export function verdictForExit(v, exitedOk, exitCode) {
621
+ if (v.status !== "done" || exitedOk)
622
+ return v;
623
+ const how = typeof exitCode === "number" ? `the run exited ${exitCode}` : "the run did not end cleanly";
624
+ return { status: "failed", sentence: `${how}${v.sentence ? `; it said done — ${v.sentence}` : ""}` };
625
+ }
626
+ /** The one room line a verdict becomes. */
627
+ export function verdictRoomLine(v) {
628
+ return `Result: ${v.status}${v.sentence ? ` — ${v.sentence}` : ""}`;
629
+ }
630
+ /**
631
+ * What the room hears from a run that FAILED without a verdict (it crashed,
632
+ * exited non-zero without `WORKER-RESULT:`, sat blocked, vanished, ran out of
633
+ * usage-limit resumes, or the worker restarted under it): how it ended and the
634
+ * reason the record keeps, on one line.
635
+ */
636
+ export function deadRunSentence(rec) {
637
+ const how = typeof rec.exitCode === "number" ? `the run exited ${rec.exitCode} without a verdict` : "the run ended without a verdict";
638
+ const why = (rec.result ?? "").replace(/\s+/g, " ").trim();
639
+ const line = why ? `${how}: ${why}` : how;
640
+ return line.length > 300 ? `${line.slice(0, 299)}…` : line;
641
+ }
642
+ /**
643
+ * At most this many tries to record one run's end on the pod. A retry runs
644
+ * only after the tick's orient read answered, so a pod outage does not use them up.
645
+ */
646
+ export const MAX_SETTLE_ATTEMPTS = 10;
647
+ /**
648
+ * The end a run owes the pod, given what it said and how its process ended:
649
+ * its verdict (`verdictForExit`), or for a run that failed without one a
650
+ * synthesised `failed` (`deadRunSentence`), so a dead run is never silent.
651
+ * `undefined` when there is nothing to tell (no verdict, not failed, and no
652
+ * runner to end).
653
+ */
654
+ export function pendingSettle(rec, verdictLine, exitedOk) {
655
+ const said = parseVerdict(verdictLine);
656
+ const verdict = said
657
+ ? verdictForExit(said, exitedOk, rec.exitCode)
658
+ : rec.status === "failed"
659
+ ? { status: "failed", sentence: deadRunSentence(rec) }
660
+ : undefined;
661
+ if (!verdict && !runEndHeaders(rec))
662
+ return undefined;
663
+ return {
664
+ process: rec.process,
665
+ claudeSessionId: rec.claudeSessionId,
666
+ ...(verdict ? { verdict } : {}),
667
+ key: `result:${rec.sessionId}:${rec.process ?? rec.launchedAt ?? rec.startedAt}`,
668
+ attempts: 0,
669
+ };
670
+ }
671
+ /**
672
+ * Outcome kinds a verdict never claims: a pull request is delivered only when
673
+ * it merges (rule work-units), and the merge sweeper marks it met then; a
674
+ * branch or a commit is a step toward that PR, not a deliverable of its own.
675
+ */
676
+ export const UNCLAIMABLE_OUTCOME_KINDS = new Set(["pull_request", "commit", "branch"]);
677
+ /**
678
+ * The outcome a `done` verdict claims, from the `synap_get_session` read: the
679
+ * session's ONE open deliverable the agent owns (not a fact, not the person's,
680
+ * not met, not withdrawn, never a PR, branch or commit). None, or several, and
681
+ * nothing is claimed: which of several the run finished is the run's to say
682
+ * (it claims each as it records it). An outcomes list that could not be read is
683
+ * `unreadable`, never "none open".
684
+ */
685
+ export function verdictClaim(getSessionText) {
686
+ let data;
687
+ try {
688
+ data = JSON.parse(getSessionText);
689
+ }
690
+ catch {
691
+ return { kind: "unreadable", why: "the session read is not JSON" };
692
+ }
693
+ const o = data?.outcomes;
694
+ if (!o)
695
+ return { kind: "unreadable", why: "the session read has no outcomes" };
696
+ if (o.status !== "ok")
697
+ return { kind: "unreadable", why: `outcomes ${String(o.status ?? "status missing")}` };
698
+ if (!Array.isArray(o.outcomes))
699
+ return { kind: "unreadable", why: "outcomes list missing" };
700
+ const open = o.outcomes.filter((x) => typeof x?.key === "string" &&
701
+ x.owner !== "human" &&
702
+ x.kind !== "fact" &&
703
+ !UNCLAIMABLE_OUTCOME_KINDS.has(String(x.kind)) &&
704
+ x.met !== true &&
705
+ x.retired !== true);
706
+ if (open.length === 0)
707
+ return { kind: "none" };
708
+ if (open.length > 1)
709
+ return { kind: "several", keys: open.map((x) => x.key) };
710
+ return { kind: "one", key: open[0].key };
711
+ }
192
712
  /** At most this many follow-up resumes per session (a runaway-loop guard). */
193
713
  export const MAX_RESUMES = 20;
194
714
  /**
@@ -204,6 +724,28 @@ export function followUpsSince(messages, since, selfAgentId) {
204
724
  .filter((m) => typeof m.content === "string" && m.content.trim().length > 0)
205
725
  .sort((a, b) => Date.parse(a.timestamp) - Date.parse(b.timestamp));
206
726
  }
727
+ /**
728
+ * Whether a session's quiet period began with THIS worker's own run: the pod's
729
+ * `quietSince` is the session's newest activity, and the run's own room posts
730
+ * (its claim, its "Look at first:", the worker's `Result:` line) count. Quiet
731
+ * since then means the run ended and nothing happened after it; resuming it
732
+ * would only re-run it (up to MAX_RESUMES paid runs). Someone else's message
733
+ * after the run resumes it through the follow-up path instead.
734
+ */
735
+ export function quietIsOwnRun(item, rec) {
736
+ if (!item.quietSince || !rec.launchedAt)
737
+ return false;
738
+ const quiet = Date.parse(item.quietSince);
739
+ const launched = Date.parse(rec.launchedAt);
740
+ return Number.isFinite(quiet) && Number.isFinite(launched) && quiet >= launched;
741
+ }
742
+ /** The prompt that resumes a run whose session went quiet and was handed back (`resume`). */
743
+ export function buildQuietResumePrompt(quietSince) {
744
+ return [
745
+ `This session went quiet${quietSince ? ` (no activity since ${quietSince})` : ""} and is still handed to you.`,
746
+ `Continue under the same protocol: re-read the session and its room, pick up where you stopped, report in the room, record outcomes, and end with one line: WORKER-RESULT: done | blocked | failed — <one sentence>.`,
747
+ ].join("\n");
748
+ }
207
749
  /** The prompt that resumes a finished run with the room's new messages. */
208
750
  export function buildFollowUpPrompt(messages) {
209
751
  const lines = messages.map((m) => {
@@ -238,86 +780,337 @@ function saveState(agent, state) {
238
780
  function say(line) {
239
781
  process.stdout.write(`[synap worker ${new Date().toISOString()}] ${line}\n`);
240
782
  }
241
- // ─── the loop ─────────────────────────────────────────────────────────────────
242
- export async function workerCommand(opts) {
783
+ const defaultExec = (bin, args, o) => new Promise((resolve) => {
784
+ execFile(bin, args, { cwd: o.cwd, env: o.env, timeout: 60_000, maxBuffer: 16 * 1024 * 1024 }, (err, stdout, stderr) => {
785
+ const code = err ? (typeof err.code === "number" ? (err.code) : 1) : 0;
786
+ resolve({ code, stdout: String(stdout ?? ""), stderr: String(stderr ?? "") });
787
+ });
788
+ });
789
+ export function createWorker(opts, deps) {
243
790
  const agent = opts.agent;
244
- // The named agent's own key and pod (SYNAP_AGENT is the CLI's one door for it).
245
- process.env.SYNAP_AGENT = agent;
246
- const cfg = await resolveHubConfig();
247
- const intervalMs = parseInterval(opts.interval);
791
+ const now = deps.now ?? Date.now;
792
+ const log = deps.say ?? say;
793
+ const exec = deps.exec ?? defaultExec;
794
+ const iso = () => new Date(now()).toISOString();
248
795
  const maxParallel = Math.max(1, Number(opts.maxParallel ?? "1"));
249
796
  const cwd = path.resolve(opts.cwd ?? path.join(os.homedir(), "Documents", "Code", "synap"));
250
797
  const claudeBin = opts.claudeBin ?? "claude";
251
- const claudeJsonPath = path.join(opts.claudeConfigDir ?? os.homedir(), ".claude.json");
252
- let mcpServer = opts.mcpServer ?? null;
253
- if (!mcpServer && fs.existsSync(claudeJsonPath)) {
254
- mcpServer = findMcpServerName(JSON.parse(fs.readFileSync(claudeJsonPath, "utf8")), cfg.podUrl);
255
- }
256
- if (!mcpServer) {
257
- throw new Error(`No Claude Code MCP server points at ${cfg.podUrl}/mcp in ${claudeJsonPath}. ` +
258
- `Connect Claude Code to the pod with this agent's key, or pass --mcp-server <name>.`);
259
- }
798
+ const workRoot = path.resolve(opts.workRoot ?? path.join(os.homedir(), "synap-work"));
799
+ const mcpServer = RUN_MCP_SERVER;
260
800
  const allowed = (opts.allowedTools ? opts.allowedTools.split(",") : DEFAULT_ALLOWED_TOOLS)
261
801
  .map((t) => t.trim())
262
802
  .filter(Boolean);
263
803
  allowed.push(`mcp__${mcpServer}`);
264
- const mcp = createMcpClient({
265
- url: `${cfg.podUrl.replace(/\/+$/, "")}/mcp`,
266
- apiKey: cfg.apiKey,
267
- clientName: `synap-worker/${agent}`,
268
- });
804
+ const accountsFile = loadAccountsFile(opts.accounts);
805
+ const pool = accountsFile
806
+ ? new AccountPool(accountsFile, (token) => probeAccount(token, { fetchImpl: deps.fetchImpl, now }), now)
807
+ : undefined;
808
+ if (accountsFile && accountsFileTooOpen(opts.accounts ?? defaultAccountsFile())) {
809
+ log(`warning: the accounts file is readable by other users — chmod 600 ${opts.accounts ?? defaultAccountsFile()}`);
810
+ }
811
+ const githubAppFile = opts.githubApp ?? defaultGithubAppFile();
812
+ const githubToken = deps.githubToken ??
813
+ (() => {
814
+ const cfg = loadGithubAppConfig(githubAppFile);
815
+ return cfg ? createInstallationTokenSource(cfg, { fetchImpl: deps.fetchImpl, now }) : undefined;
816
+ })();
817
+ if (githubToken && !deps.githubToken) {
818
+ for (const f of githubAppFilesTooOpen(githubAppFile))
819
+ log(`warning: ${f} is readable by other users — chmod 600 it`);
820
+ }
269
821
  const state = loadState(agent);
270
- // A run "running" at start is a run this process can no longer watch.
271
822
  for (const r of Object.values(state.runs)) {
272
- if (r.status === "running") {
823
+ // A print run dies with this process. A background run outlives it and is
824
+ // watched again — unless it never got its id (the worker died mid-launch).
825
+ if (r.status === "running" && (r.mode !== "bg" || !r.bgId)) {
273
826
  r.status = "failed";
274
827
  r.result = "worker restarted while this run was in flight";
275
- r.finishedAt = new Date().toISOString();
828
+ r.finishedAt = iso();
829
+ // Its runner never heard it end and its room never heard why: both are owed (the first tick settles them).
830
+ r.unsettled = pendingSettle(r, undefined, false);
276
831
  }
277
832
  }
278
833
  saveState(agent, state);
834
+ const save = () => saveState(agent, state);
835
+ const runsDir = path.join(stateDir(agent), "runs");
836
+ // Under --cwd, so Claude Code finds that folder's CLAUDE.md and .claude/rules from a run's cwd
837
+ // (it reads them from every parent directory; checked on 2.1.312).
838
+ const treesRoot = path.join(cwd, ".worker-trees");
839
+ // The runs' GitHub helpers and the broker they ask for tokens (a token lasts 1 h; a run can last longer).
840
+ let broker;
841
+ let githubRun;
842
+ if (githubToken) {
843
+ const binDir = path.join(stateDir(agent), "bin");
844
+ const ghConfigDir = path.join(stateDir(agent), "gh-config");
845
+ fs.mkdirSync(ghConfigDir, { recursive: true, mode: 0o700 });
846
+ const { gitHelper } = writeGithubHelpers(binDir, { realGh: findOnPath("gh", process.env.PATH, binDir) });
847
+ broker = startTokenBroker(githubToken);
848
+ githubRun = { sock: broker.sock, binDir, gitHelper, ghConfigDir };
849
+ }
850
+ const appendRunLog = (sessionId, line) => {
851
+ fs.mkdirSync(runsDir, { recursive: true, mode: 0o700 });
852
+ fs.appendFileSync(path.join(runsDir, `${sessionId}.jsonl`), `${JSON.stringify({ ...line, at: iso() })}\n`, { mode: 0o600 });
853
+ };
279
854
  const children = new Map();
280
- let stopping = false;
281
- const stop = () => {
282
- stopping = true;
283
- for (const c of children.values())
284
- c.kill("SIGTERM");
855
+ const sweeps = new Set();
856
+ const bgRunning = () => Object.values(state.runs).filter((r) => r.status === "running" && r.mode === "bg");
857
+ const active = () => children.size + bgRunning().length;
858
+ const who = accountsFile
859
+ ? `accounts ${accountsFile.accounts.map((a) => `${a.id}${a.design ? " (design)" : ""}`).join(", ")} · reserve ${accountsFile.reserve}`
860
+ : "the environment's Claude login";
861
+ log(`agent ${agent} → ${deps.podUrl} · MCP server "${mcpServer}" · cwd ${cwd} · work folders ${workRoot} · max ${maxParallel} parallel · ${who} · GitHub as ${githubToken ? "the Synap Agent App" : "THE ENVIRONMENT'S LOGIN"}`);
862
+ // ── accounts ──
863
+ const saidOnce = new Set();
864
+ const sayOnce = (key, line) => {
865
+ if (saidOnce.has(key))
866
+ return;
867
+ saidOnce.add(key);
868
+ log(line);
285
869
  };
286
- process.on("SIGTERM", stop);
287
- process.on("SIGINT", stop);
288
- say(`agent ${agent} → ${cfg.podUrl} · MCP server "${mcpServer}" · cwd ${cwd} · every ${intervalMs / 1000}s · max ${maxParallel} parallel`);
289
- const launch = (item, resume) => {
290
- const prompt = resume?.prompt ??
291
- buildWorkerPrompt({ sessionId: item.id, goal: item.goal, agentName: agent, cwd, mcpServer: mcpServer });
292
- const args = ["-p", prompt, "--output-format", "stream-json", "--verbose"];
293
- if (resume)
294
- args.push("--resume", resume.claudeSessionId);
870
+ let measuredThisTick = false;
871
+ /** `undefined`: no accounts file (the environment's login). `null`: no account with room — do not launch. */
872
+ const pick = async (design) => {
873
+ if (!pool)
874
+ return undefined;
875
+ if (!measuredThisTick) {
876
+ await pool.measure();
877
+ measuredThisTick = true;
878
+ }
879
+ const key = `noroom:${design ? "design" : "run"}`;
880
+ const sel = pool.select({ design });
881
+ if (!sel.account) {
882
+ sayOnce(key, `${sel.reason} — not launching ${design ? "design runs" : "runs"} for now; checking every tick`);
883
+ return null;
884
+ }
885
+ if (saidOnce.delete(key))
886
+ log(`account ${sel.account.id} has room again`);
887
+ return sel.account;
888
+ };
889
+ /** A run that ended on a usage limit: that account is out until its reset, the run waits to move. */
890
+ const markLimited = (rec, hit) => {
891
+ if (!pool || !rec.accountId)
892
+ return false;
893
+ const until = limitUntil(hit, now());
894
+ pool.markFull(rec.accountId, until);
895
+ rec.status = "limited";
896
+ rec.result = `usage limit (${hit.error}) on account ${rec.accountId} until ${new Date(until).toISOString()} — resumes on another account`;
897
+ return true;
898
+ };
899
+ // ── a run's end, on the pod ──
900
+ /**
901
+ * A run's process exited: the pod hears its runner ended, and its verdict is
902
+ * recorded on the session itself — the ONE open deliverable claimed on `done`
903
+ * (`verdictClaim`) and one room line — through the session's own doors, which
904
+ * apply directly for an agent on the session's roster. Never the run-capture
905
+ * door (governed as a playbook-run update, it would wait on the person, and a
906
+ * session handed with no run has none). Never closes the session. A failed
907
+ * write is said; the local record keeps the verdict either way. `done` is
908
+ * heard only from a process that exited cleanly (`verdictForExit`). A run
909
+ * that failed with no verdict gets a synthesised one (`pendingSettle`).
910
+ *
911
+ * The end is saved as owed (`unsettled`) BEFORE the first try, and the
912
+ * Result post carries an `idempotencyKey` per launch: a write that failed,
913
+ * or a worker that died mid-write, is retried on a later tick, and a retry of
914
+ * a post that did land returns that post instead of a second line.
915
+ */
916
+ const owe = (rec, verdictLine, exitedOk) => {
917
+ rec.unsettled = pendingSettle(rec, verdictLine, exitedOk);
918
+ };
919
+ const settling = new Set();
920
+ const settleOwed = async (rec) => {
921
+ const owed = rec.unsettled;
922
+ if (!owed || settling.has(rec.sessionId))
923
+ return;
924
+ settling.add(rec.sessionId);
925
+ const headers = runEndHeaders(owed);
926
+ const verdict = owed.verdict;
927
+ try {
928
+ const s = await deps.mcp.call("synap_get_session", { sessionId: rec.sessionId }, { headers });
929
+ if (s.isError)
930
+ throw new Error(`get_session refused: ${s.text.slice(0, 160)}`);
931
+ if (verdict) {
932
+ const claim = verdict.status === "done" ? verdictClaim(s.text) : undefined;
933
+ if (claim?.kind === "unreadable") {
934
+ log(`session ${rec.sessionId}: its outcomes could not be read (${claim.why}) — nothing claimed for the done verdict`);
935
+ }
936
+ else if (claim?.kind === "one") {
937
+ const u = await deps.mcp.call("synap_update_session", { sessionId: rec.sessionId, completeOutput: claim.key }, { headers });
938
+ if (u.isError)
939
+ log(`session ${rec.sessionId}: could not claim "${claim.key}": ${u.text.slice(0, 160)}`);
940
+ }
941
+ const channelId = findKey(JSON.parse(s.text), "channelId");
942
+ if (typeof channelId !== "string" || !channelId)
943
+ throw new Error("the session has no room");
944
+ const p = await deps.mcp.call("synap_post_message", { channelId, kind: "update", content: verdictRoomLine(verdict), idempotencyKey: owed.key }, { headers });
945
+ if (p.isError)
946
+ throw new Error(`post refused: ${p.text.slice(0, 160)}`);
947
+ }
948
+ if (rec.unsettled === owed)
949
+ delete rec.unsettled;
950
+ rec.settledAt = iso();
951
+ }
952
+ catch (err) {
953
+ owed.attempts++;
954
+ const what = verdict ? "the verdict was not recorded on the session" : "could not tell the pod the run ended";
955
+ if (owed.attempts >= MAX_SETTLE_ATTEMPTS) {
956
+ if (rec.unsettled === owed)
957
+ delete rec.unsettled;
958
+ log(`session ${rec.sessionId}: ${what} (${err.message}); gave up after ${owed.attempts} tries — the local record keeps it`);
959
+ }
960
+ else {
961
+ log(`session ${rec.sessionId}: ${what} (${err.message}); retried next tick (try ${owed.attempts} of ${MAX_SETTLE_ATTEMPTS})`);
962
+ }
963
+ }
964
+ finally {
965
+ settling.delete(rec.sessionId);
966
+ save();
967
+ }
968
+ };
969
+ const settle = async (rec, verdictLine, exitedOk) => {
970
+ owe(rec, verdictLine, exitedOk);
971
+ save();
972
+ await settleOwed(rec);
973
+ };
974
+ // ── launching ──
975
+ const launch = async (item, how, design, protocol, account) => {
976
+ // Who `gh` and `git push` act as in this run: the App, or (loudly) the login the worker runs under.
977
+ let ghToken;
978
+ if (githubToken) {
979
+ try {
980
+ ghToken = await githubToken.get();
981
+ await broker?.ready;
982
+ }
983
+ catch (err) {
984
+ log(`session ${item.id}: no GitHub App token (${err.message}) — not launching; retrying next tick`);
985
+ return false;
986
+ }
987
+ }
988
+ else {
989
+ log(`WARNING: session ${item.id} runs with the environment's GitHub login, not an App: ${githubAppFile} is missing. If that login is an org admin, this run can merge past the ruleset.`);
990
+ }
991
+ const resumeId = how.kind === "fresh" ? undefined : how.claudeSessionId;
992
+ // A work-folder run works in its own folder; a resume reuses the folder it
993
+ // was launched in (Claude finds the conversation by its cwd).
994
+ const prior = state.runs[item.id];
995
+ const workDir = protocol === "work-folder" ? (how.kind !== "fresh" && prior?.workDir) || workFolderFor(workRoot, item.id) : undefined;
996
+ // A code run (not a design run) gets a folder of its own for its worktrees; a resume reuses it.
997
+ const treeDir = protocol === "code" && !design ? (prior?.treeDir ?? treeFolderFor(treesRoot, item.id)) : undefined;
998
+ const runCwd = workDir ?? treeDir ?? cwd;
999
+ // The run names its own Claude session up front (`--session-id`), so the pod sees ONE
1000
+ // conversation per run, kept across resumes, instead of a new MCP transport id each launch.
1001
+ const runSessionId = resumeId ?? randomUUID();
1002
+ const processIdentity = runProcessIdentity(now());
1003
+ const mcpConfigPath = writeRunMcpConfig(stateDir(agent),
1004
+ // A resume runs the model its first launch said; a fresh run's is known once it starts.
1005
+ runMcpConfig(deps.podUrl, deps.apiKey, runSessionId, processIdentity, resumeId ? prior?.model : undefined), `mcp-${item.id}.json`);
1006
+ const prompt = how.kind === "followUp"
1007
+ ? how.prompt
1008
+ : how.kind === "limitResume" && how.claudeSessionId
1009
+ ? LIMIT_RESUME_PROMPT
1010
+ : workDir
1011
+ ? buildWorkFolderPrompt({ sessionId: item.id, goal: item.goal, agentName: agent, workDir, mcpServer })
1012
+ : buildWorkerPrompt({ sessionId: item.id, goal: item.goal, agentName: agent, cwd, mcpServer, design, treeDir });
1013
+ const base = {
1014
+ status: "running",
1015
+ exitCode: undefined,
1016
+ accountId: account?.id,
1017
+ mode: design ? "bg" : "print",
1018
+ launchedAt: iso(),
1019
+ blockedSince: undefined,
1020
+ protocol,
1021
+ workDir,
1022
+ treeDir,
1023
+ process: processIdentity,
1024
+ };
1025
+ const record = how.kind !== "fresh" && prior
1026
+ ? Object.assign(prior, base, how.kind === "followUp" ? { resumes: (prior.resumes ?? 0) + 1 } : { limitResumes: (prior.limitResumes ?? 0) + 1 })
1027
+ : {
1028
+ sessionId: item.id,
1029
+ startedAt: iso(),
1030
+ ...base,
1031
+ // A relaunch of a run that never started keeps counting its retries.
1032
+ ...(prior && prior.status === "failed" && !prior.claudeSessionId
1033
+ ? { launchRetries: (prior.launchRetries ?? 0) + 1 }
1034
+ : {}),
1035
+ };
1036
+ state.runs[item.id] = record;
1037
+ const what = how.kind === "fresh"
1038
+ ? "launched"
1039
+ : how.kind === "followUp"
1040
+ ? `resumed (follow-up #${record.resumes})`
1041
+ : `resumed after a usage limit (#${record.limitResumes})`;
1042
+ const on = account ? ` on account ${account.id}` : "";
1043
+ const env = workerEnv(process.env, opts.claudeConfigDir, account?.token, ghToken && githubRun ? { ...githubRun, token: ghToken } : undefined);
1044
+ if (design) {
1045
+ // `claude --bg` is the mode that has the Artifact tool. --strict-mcp-config: a
1046
+ // project .mcp.json would stop it on an interactive "new MCP servers" prompt.
1047
+ // dontAsk: a tool outside the list is denied, never a prompt nobody answers.
1048
+ const args = [
1049
+ "--bg",
1050
+ ...(resumeId ? ["--resume", resumeId] : ["--session-id", runSessionId]),
1051
+ prompt,
1052
+ "--strict-mcp-config",
1053
+ "--mcp-config",
1054
+ mcpConfigPath,
1055
+ ...(opts.skipPermissions
1056
+ ? ["--dangerously-skip-permissions"]
1057
+ : ["--permission-mode", "dontAsk", "--allowedTools", [...allowed, ...DESIGN_EXTRA_TOOLS].join(",")]),
1058
+ ];
1059
+ if (opts.dryRun) {
1060
+ record.status = "done";
1061
+ record.result = `dry run: would launch ${claudeBin} --bg in ${cwd}${on}`;
1062
+ save();
1063
+ log(`dry run — design session ${item.id}${on}: ${claudeBin} --bg <prompt> ${args.slice(4).join(" ")}`);
1064
+ return true;
1065
+ }
1066
+ save();
1067
+ const res = await exec(claudeBin, args, { cwd, env });
1068
+ const bgId = parseBgId(`${res.stdout}\n${res.stderr}`);
1069
+ appendRunLog(item.id, { type: "synap-worker", event: "bg-launch", bgId: bgId ?? null, accountId: account?.id ?? null, exit: res.code });
1070
+ if (res.code !== 0 || !bgId) {
1071
+ record.status = "failed";
1072
+ record.exitCode = res.code;
1073
+ record.finishedAt = iso();
1074
+ record.result = `claude --bg did not start (exit ${res.code}): ${(res.stderr || res.stdout).trim().slice(-200)}`;
1075
+ save();
1076
+ log(`session ${item.id}: ${record.result}`);
1077
+ return false;
1078
+ }
1079
+ record.bgId = bgId;
1080
+ save();
1081
+ log(`${what} design session ${item.id} as background ${bgId}${on}`);
1082
+ return true;
1083
+ }
1084
+ const args = ["-p", prompt, "--output-format", "stream-json", "--verbose", "--mcp-config", mcpConfigPath];
1085
+ args.push(...(resumeId ? ["--resume", resumeId] : ["--session-id", runSessionId]));
295
1086
  if (opts.skipPermissions)
296
1087
  args.push("--dangerously-skip-permissions");
297
1088
  else
298
- args.push("--permission-mode", "acceptEdits", "--allowedTools", ...allowed);
299
- const prior = state.runs[item.id];
300
- const record = resume && prior
301
- ? Object.assign(prior, { status: "running", resumes: (prior.resumes ?? 0) + 1, exitCode: undefined })
302
- : { sessionId: item.id, status: "running", startedAt: new Date().toISOString() };
303
- state.runs[item.id] = record;
1089
+ args.push("--permission-mode", "acceptEdits", "--allowedTools", ...allowed, ...(workDir ? WORK_FOLDER_EXTRA_TOOLS : []));
304
1090
  if (opts.dryRun) {
305
1091
  record.status = "done";
306
- record.result = `dry run: would launch ${claudeBin} in ${cwd}`;
307
- saveState(agent, state);
308
- say(`dry run — session ${item.id}: ${claudeBin} ${args.slice(0, 1).join(" ")} <prompt> ${args.slice(2).join(" ")}`);
309
- return;
1092
+ record.result = `dry run: would launch ${claudeBin} (${protocol}) in ${runCwd}${on}`;
1093
+ save();
1094
+ log(`dry run — ${protocol} session ${item.id}${on} in ${runCwd}: ${claudeBin} ${args.slice(0, 1).join(" ")} <prompt> ${args.slice(2).join(" ")}`);
1095
+ return true;
1096
+ }
1097
+ if (workDir)
1098
+ fs.mkdirSync(workDir, { recursive: true, mode: 0o700 });
1099
+ if (treeDir) {
1100
+ ensureTreeFolder(treeDir);
1101
+ // `cwd` may itself be a repo: keep the runs' folders out of its git status.
1102
+ fs.writeFileSync(path.join(treesRoot, ".gitignore"), "*\n");
310
1103
  }
311
- const runsDir = path.join(stateDir(agent), "runs");
312
1104
  fs.mkdirSync(runsDir, { recursive: true, mode: 0o700 });
313
1105
  const out = fs.createWriteStream(path.join(runsDir, `${item.id}.jsonl`), { flags: "a", mode: 0o600 });
314
- const child = spawn(claudeBin, args, { cwd, env: workerEnv(process.env, opts.claudeConfigDir), stdio: ["ignore", "pipe", "pipe"] });
1106
+ const child = spawn(claudeBin, args, { cwd: runCwd, env, stdio: ["ignore", "pipe", "pipe"] });
315
1107
  children.set(item.id, child);
316
1108
  record.pid = child.pid;
317
- saveState(agent, state);
318
- say(`${resume ? `resumed (follow-up #${record.resumes})` : "launched"} session ${item.id} (pid ${child.pid})`);
1109
+ save();
1110
+ log(`${what} ${protocol} session ${item.id} (pid ${child.pid})${on}${workDir ? ` in ${workDir}` : ""}${treeDir ? ` in ${treeDir}` : ""}`);
319
1111
  let buffered = "";
320
- let lastResult;
1112
+ let end = {};
1113
+ const rejected = {};
321
1114
  child.stdout?.on("data", (chunk) => {
322
1115
  out.write(chunk);
323
1116
  buffered += chunk.toString("utf8");
@@ -325,76 +1118,333 @@ export async function workerCommand(opts) {
325
1118
  buffered = lines.pop() ?? "";
326
1119
  for (const line of lines) {
327
1120
  try {
328
- const ev = JSON.parse(line);
329
- if (ev.type === "system" && ev.subtype === "init" && ev.session_id && !record.claudeSessionId) {
330
- record.claudeSessionId = ev.session_id;
331
- saveState(agent, state);
332
- }
333
- if (ev.type === "result" && typeof ev.result === "string")
334
- lastResult = ev.result;
1121
+ end = foldRunEvent(end, JSON.parse(line), rejected);
335
1122
  }
336
1123
  catch {
337
- /* a partial or non-JSON line — kept in the run log, not parsed */
1124
+ continue; // a partial or non-JSON line — kept in the run log, not parsed
1125
+ }
1126
+ if (end.claudeSessionId && !record.claudeSessionId) {
1127
+ record.claudeSessionId = end.claudeSessionId;
1128
+ save();
1129
+ }
1130
+ if (end.model && record.model !== end.model) {
1131
+ record.model = end.model;
1132
+ save();
338
1133
  }
339
1134
  }
340
1135
  });
341
1136
  child.stderr?.on("data", (chunk) => out.write(chunk));
1137
+ child.on("error", (err) => {
1138
+ // The binary could not be started: `close` follows with no exit code.
1139
+ out.write(`synap-worker: could not start ${claudeBin}: ${err.message}\n`);
1140
+ });
342
1141
  child.on("close", (code) => {
343
- children.delete(item.id);
344
1142
  out.end();
345
- record.exitCode = code;
346
- record.finishedAt = new Date().toISOString();
347
- record.result = workerVerdict(lastResult) ?? (lastResult ? lastResult.slice(-300) : undefined);
348
- record.status = code === 0 ? "done" : "failed";
349
- saveState(agent, state);
350
- say(`session ${item.id} finished — exit ${code}${record.result ? ` · ${record.result}` : ""}`);
1143
+ const finish = async () => {
1144
+ // The run's clean worktrees go; one with unpushed or uncommitted work stays and is named.
1145
+ if (treeDir) {
1146
+ const swept = await sweepTreeFolder(treeDir, deps.git);
1147
+ record.keptTrees = swept.kept.map((k) => `${k.dir} (${k.why})`);
1148
+ if (swept.removed.length)
1149
+ log(`session ${item.id}: removed ${swept.removed.length} clean worktree(s)`);
1150
+ if (!record.keptTrees.length)
1151
+ delete record.keptTrees;
1152
+ }
1153
+ children.delete(item.id);
1154
+ record.exitCode = code;
1155
+ record.finishedAt = iso();
1156
+ const hit = endedOnLimit(end);
1157
+ const limited = !!hit && markLimited(record, hit);
1158
+ if (!limited) {
1159
+ record.result = workerVerdict(end.text) ?? (end.text ? end.text.slice(-300) : undefined);
1160
+ record.status = code === 0 ? "done" : "failed";
1161
+ }
1162
+ if (record.keptTrees)
1163
+ record.result = `${record.result ?? ""} · KEPT (unpushed work): ${record.keptTrees.join("; ")}`.trim();
1164
+ save();
1165
+ log(`session ${item.id} finished — exit ${code}${record.result ? ` · ${record.result}` : ""}`);
1166
+ await settle(record, limited ? undefined : workerVerdict(end.text), code === 0);
1167
+ };
1168
+ const p = finish()
1169
+ .catch((err) => log(`session ${item.id}: finishing failed: ${err.message}`))
1170
+ .finally(() => sweeps.delete(p));
1171
+ sweeps.add(p);
1172
+ });
1173
+ return true;
1174
+ };
1175
+ // ── watching background (design) runs ──
1176
+ const stopBg = async (rec) => {
1177
+ if (!rec.bgId)
1178
+ return;
1179
+ const r = await exec(claudeBin, ["stop", rec.bgId], { cwd, env: workerEnv(process.env, opts.claudeConfigDir) });
1180
+ if (r.code !== 0)
1181
+ log(`could not stop background ${rec.bgId} (exit ${r.code}); stop it with: claude stop ${rec.bgId}`);
1182
+ };
1183
+ const endBg = async (rec, status, result, verdictLine) => {
1184
+ rec.status = status;
1185
+ rec.result = result;
1186
+ rec.finishedAt = iso();
1187
+ rec.blockedSince = undefined;
1188
+ owe(rec, verdictLine, status === "done");
1189
+ save();
1190
+ appendRunLog(rec.sessionId, { type: "synap-worker", event: "bg-end", bgId: rec.bgId ?? null, status });
1191
+ log(`session ${rec.sessionId} (background ${rec.bgId}) finished — ${status}${result ? ` · ${result}` : ""}`);
1192
+ await stopBg(rec);
1193
+ await settleOwed(rec);
1194
+ };
1195
+ const watch = async () => {
1196
+ const running = bgRunning().filter((r) => r.bgId);
1197
+ if (!running.length)
1198
+ return;
1199
+ const res = await exec(claudeBin, ["agents", "--json", "--all", "--cwd", cwd], {
1200
+ cwd,
1201
+ env: workerEnv(process.env, opts.claudeConfigDir),
351
1202
  });
1203
+ let list;
1204
+ try {
1205
+ if (res.code !== 0)
1206
+ throw new Error(`exit ${res.code}: ${res.stderr.trim().slice(-160)}`);
1207
+ list = parseAgentsList(res.stdout);
1208
+ }
1209
+ catch (err) {
1210
+ // A failed read is not "gone": the runs stay as they were.
1211
+ log(`could not list background sessions (${err.message}); checking again next tick`);
1212
+ return;
1213
+ }
1214
+ for (const rec of running) {
1215
+ const entry = list.find((e) => e.id === rec.bgId);
1216
+ if (entry?.sessionId && !rec.claudeSessionId) {
1217
+ rec.claudeSessionId = entry.sessionId;
1218
+ save();
1219
+ }
1220
+ const phase = bgPhase(entry);
1221
+ const since = Date.parse(rec.launchedAt ?? rec.startedAt);
1222
+ if (phase === "running") {
1223
+ if (rec.blockedSince) {
1224
+ rec.blockedSince = undefined;
1225
+ save();
1226
+ }
1227
+ continue;
1228
+ }
1229
+ if (phase === "gone") {
1230
+ if (now() - since > BG_SETTLE_MS)
1231
+ await endBg(rec, "failed", "background session no longer listed by claude agents");
1232
+ continue;
1233
+ }
1234
+ const file = rec.claudeSessionId ? findTranscript(opts.claudeConfigDir, rec.claudeSessionId) : undefined;
1235
+ const end = file ? readTranscriptEnd(fs.readFileSync(file, "utf8"), since) : undefined;
1236
+ const hit = end ? endedOnLimit(end) : undefined;
1237
+ if (hit) {
1238
+ rec.finishedAt = iso();
1239
+ rec.blockedSince = undefined;
1240
+ if (!markLimited(rec, hit)) {
1241
+ rec.status = "failed";
1242
+ rec.result = `usage limit (${hit.error})`;
1243
+ }
1244
+ owe(rec, undefined, false);
1245
+ save();
1246
+ log(`session ${rec.sessionId} (background ${rec.bgId}): ${rec.result}`);
1247
+ await stopBg(rec);
1248
+ await settleOwed(rec);
1249
+ continue;
1250
+ }
1251
+ if (phase === "finished") {
1252
+ // Just resumed: the listing can still show the previous turn's ending.
1253
+ if (!end?.seen && now() - since < BG_SETTLE_MS)
1254
+ continue;
1255
+ const text = end?.text;
1256
+ await endBg(rec, entry?.state === "done" ? "done" : "failed", workerVerdict(text) ?? (text ? text.slice(-300) : file ? "no reply since launch" : "transcript not found"), workerVerdict(text));
1257
+ continue;
1258
+ }
1259
+ // Blocked and not on a limit: it waits for input nobody here will give.
1260
+ if (!rec.blockedSince) {
1261
+ rec.blockedSince = iso();
1262
+ save();
1263
+ }
1264
+ else if (now() - Date.parse(rec.blockedSince) > BG_BLOCKED_MAX_MS) {
1265
+ await endBg(rec, "failed", `background session blocked (needs input) for ${BG_BLOCKED_MAX_MS / 60_000} min — claude attach ${rec.bgId}`);
1266
+ }
1267
+ }
352
1268
  };
353
- // Its own posts must never resume it: compare with the key's real agent id.
354
- const selfAgentId = await resolveUserId(cfg);
1269
+ // ── the tick ──
355
1270
  const roomFollowUps = async (sessionId, since) => {
356
- const s = await mcp.call("synap_get_session", { sessionId });
1271
+ const s = await deps.mcp.call("synap_get_session", { sessionId });
357
1272
  if (s.isError)
358
1273
  throw new Error(`get_session ${sessionId} refused: ${s.text.slice(0, 160)}`);
359
1274
  const channelId = findKey(JSON.parse(s.text), "channelId") ?? null;
360
1275
  if (!channelId)
361
1276
  return [];
362
- const t = await mcp.call("synap_get_thread_context", { threadId: channelId });
1277
+ const t = await deps.mcp.call("synap_get_thread_context", { threadId: channelId });
363
1278
  if (t.isError)
364
1279
  throw new Error(`thread ${channelId} refused: ${t.text.slice(0, 160)}`);
365
1280
  const messages = JSON.parse(t.text).messages ?? [];
366
- return followUpsSince(messages, since, selfAgentId);
1281
+ return followUpsSince(messages, since, deps.selfAgentId);
1282
+ };
1283
+ /**
1284
+ * How a handed session runs: a design run (always the code protocol, as
1285
+ * before protocols), else the protocol its data says. `undefined` when that
1286
+ * could not be read (not launched this tick).
1287
+ */
1288
+ const readShape = async (sessionId) => {
1289
+ try {
1290
+ const s = await deps.mcp.call("synap_get_session", { sessionId });
1291
+ if (s.isError)
1292
+ throw new Error(`refused: ${s.text.slice(0, 160)}`);
1293
+ const design = isDesignSession(s.text);
1294
+ return { design, protocol: design ? "code" : sessionProtocol(s.text) };
1295
+ }
1296
+ catch (err) {
1297
+ const msg = err.message;
1298
+ sayOnce(`shape:${sessionId}:${msg}`, `session ${sessionId}: could not read how to run it (${msg}) — not launched; retried every tick`);
1299
+ return undefined;
1300
+ }
367
1301
  };
368
1302
  const tick = async () => {
369
- const res = await mcp.call("synap_orient", {});
1303
+ measuredThisTick = false;
1304
+ // The stamp rides on this poll only: it is what tells the pod a process serves this agent.
1305
+ const res = await deps.mcp.call("synap_orient", {}, { headers: workerRunnerHeader() });
370
1306
  if (res.isError)
371
1307
  throw new Error(`orient refused: ${res.text.slice(0, 200)}`);
1308
+ // 0. Run ends the pod has not heard yet (a write that failed, a restart mid-write): retried.
1309
+ for (const rec of Object.values(state.runs))
1310
+ if (rec.unsettled)
1311
+ await settleOwed(rec);
372
1312
  const items = parseHandedToYou(res.text);
373
- const fresh = pickNew(items, state, maxParallel - children.size);
374
- for (const item of fresh)
375
- launch(item);
376
- let resumed = 0;
1313
+ await watch();
1314
+ const free = () => maxParallel - active();
1315
+ const handed = new Map(items.map((i) => [i.id, i]));
1316
+ let launched = 0;
1317
+ // 1. Runs a usage limit cut off: resumed first, on an account with room.
1318
+ for (const rec of Object.values(state.runs)) {
1319
+ if (free() <= 0)
1320
+ break;
1321
+ const item = rec.status === "limited" ? handed.get(rec.sessionId) : undefined;
1322
+ // A resume item continues its conversation or nothing: never a fresh start.
1323
+ if (!item || (item.resume && !rec.claudeSessionId))
1324
+ continue;
1325
+ if ((rec.limitResumes ?? 0) >= MAX_LIMIT_RESUMES) {
1326
+ rec.status = "failed";
1327
+ rec.result = `stopped: cut off by a usage limit ${MAX_LIMIT_RESUMES + 1} times`;
1328
+ log(`session ${rec.sessionId}: ${rec.result}`);
1329
+ await settle(rec, undefined, false);
1330
+ continue;
1331
+ }
1332
+ const design = rec.mode === "bg";
1333
+ const account = await pick(design);
1334
+ if (account === null)
1335
+ continue;
1336
+ await launch(item, { kind: "limitResume", claudeSessionId: rec.claudeSessionId }, design, rec.protocol ?? "code", account);
1337
+ launched++;
1338
+ }
1339
+ // 2. Sessions never launched (or due a relaunch).
1340
+ for (const item of pickNew(items, state, items.length, now())) {
1341
+ if (free() <= 0)
1342
+ break;
1343
+ const shape = await readShape(item.id);
1344
+ if (shape === undefined)
1345
+ continue;
1346
+ const account = await pick(shape.design);
1347
+ if (account === null)
1348
+ continue;
1349
+ await launch(item, { kind: "fresh" }, shape.design, shape.protocol, account);
1350
+ launched++;
1351
+ }
1352
+ // 3. Finished runs with new room messages, or whose quiet session was handed back
1353
+ // (`resume`): their conversation resumed — once per quiet period.
377
1354
  for (const item of items) {
378
- if (children.size >= maxParallel)
1355
+ if (free() <= 0)
379
1356
  break;
380
1357
  const rec = state.runs[item.id];
381
- if (!rec || rec.status === "running" || !rec.claudeSessionId || !rec.finishedAt)
1358
+ if (item.resume && !rec?.claudeSessionId && rec?.status !== "running") {
1359
+ sayOnce(`resume-unknown:${item.id}`, `session ${item.id} went quiet and is handed back to resume, but this computer has no conversation of it — not launched`);
1360
+ continue;
1361
+ }
1362
+ if (!rec || rec.status === "running" || rec.status === "limited" || !rec.claudeSessionId || !rec.finishedAt)
382
1363
  continue;
383
1364
  if ((rec.resumes ?? 0) >= MAX_RESUMES)
384
1365
  continue;
385
1366
  const follow = await roomFollowUps(item.id, rec.finishedAt);
386
- if (!follow.length)
1367
+ const quiet = item.resume === true && rec.resumedQuietSince !== (item.quietSince ?? "") && !quietIsOwnRun(item, rec);
1368
+ if (!follow.length && !quiet)
1369
+ continue;
1370
+ const design = rec.mode === "bg";
1371
+ const account = await pick(design);
1372
+ if (account === null)
387
1373
  continue;
388
- launch(item, { claudeSessionId: rec.claudeSessionId, prompt: buildFollowUpPrompt(follow) });
389
- resumed++;
1374
+ const started = await launch(item, {
1375
+ kind: "followUp",
1376
+ claudeSessionId: rec.claudeSessionId,
1377
+ prompt: follow.length ? buildFollowUpPrompt(follow) : buildQuietResumePrompt(item.quietSince),
1378
+ }, design, rec.protocol ?? "code", account);
1379
+ if (!started)
1380
+ continue;
1381
+ // Stamped only once a run started: a launch that failed leaves the quiet period due.
1382
+ if (item.resume)
1383
+ rec.resumedQuietSince = item.quietSince ?? "";
1384
+ save();
1385
+ launched++;
390
1386
  }
391
- return { handed: items.length, launched: fresh.length + resumed };
1387
+ return { handed: items.length, launched };
1388
+ };
1389
+ return {
1390
+ tick,
1391
+ watch,
1392
+ state,
1393
+ backgroundRunning: () => bgRunning().length,
1394
+ waitForChildren: async () => {
1395
+ while (children.size) {
1396
+ await Promise.all([...children.values()].map((c) => new Promise((r) => c.once("close", r))));
1397
+ await Promise.all([...sweeps]);
1398
+ }
1399
+ await Promise.all([...sweeps]);
1400
+ },
1401
+ stop: () => {
1402
+ broker?.close();
1403
+ for (const c of children.values())
1404
+ c.kill("SIGTERM");
1405
+ },
392
1406
  };
1407
+ }
1408
+ // ─── the loop ─────────────────────────────────────────────────────────────────
1409
+ export async function workerCommand(opts) {
1410
+ const agent = opts.agent;
1411
+ // An unknown name would otherwise resolve to the next key in the ladder and
1412
+ // the worker would claim another agent's sessions.
1413
+ const unknown = unknownAgentMessage(agent);
1414
+ if (unknown)
1415
+ throw new Error(unknown);
1416
+ // The named agent's own key and pod (SYNAP_AGENT is the CLI's one door for it).
1417
+ process.env.SYNAP_AGENT = agent;
1418
+ const cfg = await resolveHubConfig();
1419
+ // The agent runs ON this computer: one that never enrolled on the pod enrols first (founder, 2026-10-31).
1420
+ // Never interactive: a supervised worker (tmux, launchd) has no person to answer a prompt, and the
1421
+ // device flow would rewire this computer's other MCP clients. It stops with how to enrol.
1422
+ const enrol = await ensureComputerEnrolled(cfg.podUrl, `run \`synap worker --agent ${agent}\` again`, { interactive: false });
1423
+ if (!enrol.enrolled)
1424
+ throw new Error(enrol.message);
1425
+ const intervalMs = parseInterval(opts.interval);
1426
+ const mcp = createMcpClient({
1427
+ url: `${cfg.podUrl.replace(/\/+$/, "")}/mcp`,
1428
+ apiKey: cfg.apiKey,
1429
+ clientName: `synap-worker/${agent}`,
1430
+ });
1431
+ // Its own posts must never resume it: compare with the key's AGENT id (who
1432
+ // the key is), never the acting user /users/me answers (its linked human).
1433
+ const selfAgentId = await resolveAgentUserId(cfg);
1434
+ const worker = createWorker(opts, { mcp, podUrl: cfg.podUrl, apiKey: cfg.apiKey, selfAgentId });
1435
+ let stopping = false;
1436
+ const stop = () => {
1437
+ stopping = true;
1438
+ worker.stop();
1439
+ };
1440
+ process.on("SIGTERM", stop);
1441
+ process.on("SIGINT", stop);
1442
+ say(`every ${intervalMs / 1000}s`);
393
1443
  let failures = 0;
394
1444
  let paused = false;
395
1445
  while (!stopping) {
396
1446
  try {
397
- const r = await tick();
1447
+ const r = await worker.tick();
398
1448
  failures = 0;
399
1449
  if (paused)
400
1450
  say("resumed: picking up work again");
@@ -427,9 +1477,13 @@ export async function workerCommand(opts) {
427
1477
  break;
428
1478
  await new Promise((r) => setTimeout(r, Math.min(intervalMs * Math.max(1, failures), 10 * 60_000)));
429
1479
  }
430
- if (opts.once && children.size) {
431
- say(`--once: waiting for ${children.size} run(s) to finish`);
432
- await Promise.all([...children.values()].map((c) => new Promise((r) => c.on("close", r))));
1480
+ if (opts.once) {
1481
+ say(`--once: waiting for the run(s) to finish`);
1482
+ await worker.waitForChildren();
1483
+ while (!stopping && worker.backgroundRunning()) {
1484
+ await new Promise((r) => setTimeout(r, intervalMs));
1485
+ await worker.watch();
1486
+ }
433
1487
  }
434
1488
  }
435
1489
  //# sourceMappingURL=worker.js.map