@apralabs/apra-fleet 0.4.2 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (370) hide show
  1. package/README.md +38 -5
  2. package/dist/agents/backlog-groomer.md +25 -0
  3. package/dist/agents/ci-watcher.md +11 -0
  4. package/dist/agents/deployer.md +37 -2
  5. package/dist/agents/doer.md +36 -12
  6. package/dist/agents/harvester.md +11 -0
  7. package/dist/agents/integ-test-runner.md +11 -0
  8. package/dist/agents/kb-reconciler.md +9 -0
  9. package/dist/agents/plan-reviewer.md +110 -22
  10. package/dist/agents/planner.md +125 -37
  11. package/dist/agents/regression-test-runner.md +11 -0
  12. package/dist/agents/reviewer.md +13 -2
  13. package/dist/agents/schemas/plan-reviewer-output.json +37 -0
  14. package/dist/cli/agent-transform.d.ts +83 -2
  15. package/dist/cli/agent-transform.d.ts.map +1 -1
  16. package/dist/cli/agent-transform.js +266 -21
  17. package/dist/cli/agent-transform.js.map +1 -1
  18. package/dist/cli/auth.d.ts.map +1 -1
  19. package/dist/cli/auth.js +28 -9
  20. package/dist/cli/auth.js.map +1 -1
  21. package/dist/cli/config.d.ts +7 -5
  22. package/dist/cli/config.d.ts.map +1 -1
  23. package/dist/cli/config.js +44 -34
  24. package/dist/cli/config.js.map +1 -1
  25. package/dist/cli/install.d.ts +33 -0
  26. package/dist/cli/install.d.ts.map +1 -1
  27. package/dist/cli/install.js +290 -23
  28. package/dist/cli/install.js.map +1 -1
  29. package/dist/cli/kb-directives.d.ts.map +1 -1
  30. package/dist/cli/kb-directives.js +2 -1
  31. package/dist/cli/kb-directives.js.map +1 -1
  32. package/dist/cli/register-member.d.ts.map +1 -1
  33. package/dist/cli/register-member.js +6 -2
  34. package/dist/cli/register-member.js.map +1 -1
  35. package/dist/commands/kb-server.d.ts +3 -0
  36. package/dist/commands/kb-server.d.ts.map +1 -1
  37. package/dist/commands/kb-server.js +43 -3
  38. package/dist/commands/kb-server.js.map +1 -1
  39. package/dist/index.js +3 -3
  40. package/dist/os/linux.d.ts +6 -0
  41. package/dist/os/linux.d.ts.map +1 -1
  42. package/dist/os/linux.js +35 -2
  43. package/dist/os/linux.js.map +1 -1
  44. package/dist/os/os-commands.d.ts +61 -2
  45. package/dist/os/os-commands.d.ts.map +1 -1
  46. package/dist/os/windows-gitbash.d.ts +5 -0
  47. package/dist/os/windows-gitbash.d.ts.map +1 -1
  48. package/dist/os/windows-gitbash.js +15 -0
  49. package/dist/os/windows-gitbash.js.map +1 -1
  50. package/dist/os/windows.d.ts +6 -0
  51. package/dist/os/windows.d.ts.map +1 -1
  52. package/dist/os/windows.js +46 -3
  53. package/dist/os/windows.js.map +1 -1
  54. package/dist/providers/agy.d.ts +115 -6
  55. package/dist/providers/agy.d.ts.map +1 -1
  56. package/dist/providers/agy.js +569 -53
  57. package/dist/providers/agy.js.map +1 -1
  58. package/dist/providers/claude.d.ts +81 -3
  59. package/dist/providers/claude.d.ts.map +1 -1
  60. package/dist/providers/claude.js +344 -10
  61. package/dist/providers/claude.js.map +1 -1
  62. package/dist/providers/codex.d.ts +4 -2
  63. package/dist/providers/codex.d.ts.map +1 -1
  64. package/dist/providers/codex.js +19 -2
  65. package/dist/providers/codex.js.map +1 -1
  66. package/dist/providers/copilot.d.ts +4 -2
  67. package/dist/providers/copilot.d.ts.map +1 -1
  68. package/dist/providers/copilot.js +20 -2
  69. package/dist/providers/copilot.js.map +1 -1
  70. package/dist/providers/none.d.ts +4 -2
  71. package/dist/providers/none.d.ts.map +1 -1
  72. package/dist/providers/none.js +17 -2
  73. package/dist/providers/none.js.map +1 -1
  74. package/dist/providers/opencode.d.ts +3 -1
  75. package/dist/providers/opencode.d.ts.map +1 -1
  76. package/dist/providers/opencode.js +22 -2
  77. package/dist/providers/opencode.js.map +1 -1
  78. package/dist/providers/provider.d.ts +201 -6
  79. package/dist/providers/provider.d.ts.map +1 -1
  80. package/dist/providers/provider.js +56 -0
  81. package/dist/providers/provider.js.map +1 -1
  82. package/dist/services/agy-project.d.ts +102 -0
  83. package/dist/services/agy-project.d.ts.map +1 -0
  84. package/dist/services/agy-project.js +356 -0
  85. package/dist/services/agy-project.js.map +1 -0
  86. package/dist/services/auth-socket.js +4 -4
  87. package/dist/services/auth-web.js +1 -1
  88. package/dist/services/cloud/lifecycle.d.ts.map +1 -1
  89. package/dist/services/cloud/lifecycle.js +33 -4
  90. package/dist/services/cloud/lifecycle.js.map +1 -1
  91. package/dist/services/credential-cleanup.d.ts.map +1 -1
  92. package/dist/services/credential-cleanup.js +35 -17
  93. package/dist/services/credential-cleanup.js.map +1 -1
  94. package/dist/services/exit-drain.d.ts +44 -0
  95. package/dist/services/exit-drain.d.ts.map +1 -0
  96. package/dist/services/exit-drain.js +52 -0
  97. package/dist/services/exit-drain.js.map +1 -0
  98. package/dist/services/github-app.d.ts.map +1 -1
  99. package/dist/services/github-app.js +32 -4
  100. package/dist/services/github-app.js.map +1 -1
  101. package/dist/services/http-transport.d.ts +1 -0
  102. package/dist/services/http-transport.d.ts.map +1 -1
  103. package/dist/services/http-transport.js +48 -1
  104. package/dist/services/http-transport.js.map +1 -1
  105. package/dist/services/knowledge/kb-config.d.ts +42 -0
  106. package/dist/services/knowledge/kb-config.d.ts.map +1 -0
  107. package/dist/services/knowledge/kb-config.js +54 -0
  108. package/dist/services/knowledge/kb-config.js.map +1 -0
  109. package/dist/services/knowledge/kb-providers.d.ts +7 -1
  110. package/dist/services/knowledge/kb-providers.d.ts.map +1 -1
  111. package/dist/services/knowledge/kb-providers.js +191 -17
  112. package/dist/services/knowledge/kb-providers.js.map +1 -1
  113. package/dist/services/knowledge/require-sqlite-project.d.ts +32 -0
  114. package/dist/services/knowledge/require-sqlite-project.d.ts.map +1 -0
  115. package/dist/services/knowledge/require-sqlite-project.js +38 -0
  116. package/dist/services/knowledge/require-sqlite-project.js.map +1 -0
  117. package/dist/services/model-pricing.d.ts.map +1 -1
  118. package/dist/services/model-pricing.js +7 -3
  119. package/dist/services/model-pricing.js.map +1 -1
  120. package/dist/services/secret-token.d.ts +38 -0
  121. package/dist/services/secret-token.d.ts.map +1 -0
  122. package/dist/services/secret-token.js +51 -0
  123. package/dist/services/secret-token.js.map +1 -0
  124. package/dist/services/service-manager/types.d.ts +3 -1
  125. package/dist/services/service-manager/types.d.ts.map +1 -1
  126. package/dist/services/service-manager/windows.d.ts +14 -2
  127. package/dist/services/service-manager/windows.d.ts.map +1 -1
  128. package/dist/services/service-manager/windows.js +65 -7
  129. package/dist/services/service-manager/windows.js.map +1 -1
  130. package/dist/services/ssh.d.ts.map +1 -1
  131. package/dist/services/ssh.js +58 -2
  132. package/dist/services/ssh.js.map +1 -1
  133. package/dist/services/stall/read-log-tail.d.ts.map +1 -1
  134. package/dist/services/stall/read-log-tail.js +20 -10
  135. package/dist/services/stall/read-log-tail.js.map +1 -1
  136. package/dist/services/stall/stall-detector.d.ts +53 -4
  137. package/dist/services/stall/stall-detector.d.ts.map +1 -1
  138. package/dist/services/stall/stall-detector.js +212 -34
  139. package/dist/services/stall/stall-detector.js.map +1 -1
  140. package/dist/services/strategy.d.ts.map +1 -1
  141. package/dist/services/strategy.js +79 -2
  142. package/dist/services/strategy.js.map +1 -1
  143. package/dist/services/substitution-engine.js +2 -2
  144. package/dist/services/substitution-engine.js.map +1 -1
  145. package/dist/services/tool-registry.d.ts.map +1 -1
  146. package/dist/services/tool-registry.js +5 -3
  147. package/dist/services/tool-registry.js.map +1 -1
  148. package/dist/services/vcs/azure-devops.d.ts +66 -1
  149. package/dist/services/vcs/azure-devops.d.ts.map +1 -1
  150. package/dist/services/vcs/azure-devops.js +192 -21
  151. package/dist/services/vcs/azure-devops.js.map +1 -1
  152. package/dist/services/vcs/types.d.ts +1 -1
  153. package/dist/skills/fleet-sprint-cli/SKILL.md +18 -0
  154. package/dist/skills/pm/SKILL.md +1 -1
  155. package/dist/skills/pm/cost.md +0 -3
  156. package/dist/skills/pm/fleet-addendum.md +2 -2
  157. package/dist/tools/check-status.d.ts +7 -4
  158. package/dist/tools/check-status.d.ts.map +1 -1
  159. package/dist/tools/check-status.js +2 -0
  160. package/dist/tools/check-status.js.map +1 -1
  161. package/dist/tools/compose-permissions.d.ts +6 -0
  162. package/dist/tools/compose-permissions.d.ts.map +1 -1
  163. package/dist/tools/compose-permissions.js +182 -34
  164. package/dist/tools/compose-permissions.js.map +1 -1
  165. package/dist/tools/credential-store-set.js +1 -1
  166. package/dist/tools/execute-command.d.ts.map +1 -1
  167. package/dist/tools/execute-command.js +31 -20
  168. package/dist/tools/execute-command.js.map +1 -1
  169. package/dist/tools/execute-prompt.d.ts +15 -2
  170. package/dist/tools/execute-prompt.d.ts.map +1 -1
  171. package/dist/tools/execute-prompt.js +1196 -892
  172. package/dist/tools/execute-prompt.js.map +1 -1
  173. package/dist/tools/kb-capture.d.ts +2 -2
  174. package/dist/tools/kb-capture.d.ts.map +1 -1
  175. package/dist/tools/kb-capture.js +29 -5
  176. package/dist/tools/kb-capture.js.map +1 -1
  177. package/dist/tools/kb-export.d.ts +2 -2
  178. package/dist/tools/kb-export.d.ts.map +1 -1
  179. package/dist/tools/kb-export.js +3 -28
  180. package/dist/tools/kb-export.js.map +1 -1
  181. package/dist/tools/kb-feedback.d.ts.map +1 -1
  182. package/dist/tools/kb-feedback.js +3 -1
  183. package/dist/tools/kb-feedback.js.map +1 -1
  184. package/dist/tools/kb-freshness-sweep.d.ts.map +1 -1
  185. package/dist/tools/kb-freshness-sweep.js +3 -1
  186. package/dist/tools/kb-freshness-sweep.js.map +1 -1
  187. package/dist/tools/kb-import.d.ts.map +1 -1
  188. package/dist/tools/kb-import.js +2 -1
  189. package/dist/tools/kb-import.js.map +1 -1
  190. package/dist/tools/kb-list.d.ts.map +1 -1
  191. package/dist/tools/kb-list.js +3 -1
  192. package/dist/tools/kb-list.js.map +1 -1
  193. package/dist/tools/kb-query.d.ts +2 -2
  194. package/dist/tools/kb-reconcile-prefilter.d.ts.map +1 -1
  195. package/dist/tools/kb-reconcile-prefilter.js +3 -1
  196. package/dist/tools/kb-reconcile-prefilter.js.map +1 -1
  197. package/dist/tools/kb-resolve-contradiction.d.ts.map +1 -1
  198. package/dist/tools/kb-resolve-contradiction.js +3 -1
  199. package/dist/tools/kb-resolve-contradiction.js.map +1 -1
  200. package/dist/tools/kb-setup.d.ts.map +1 -1
  201. package/dist/tools/kb-setup.js +125 -20
  202. package/dist/tools/kb-setup.js.map +1 -1
  203. package/dist/tools/kb-stats.d.ts.map +1 -1
  204. package/dist/tools/kb-stats.js +64 -43
  205. package/dist/tools/kb-stats.js.map +1 -1
  206. package/dist/tools/list-members.d.ts +2 -2
  207. package/dist/tools/list-members.d.ts.map +1 -1
  208. package/dist/tools/list-members.js +1 -0
  209. package/dist/tools/list-members.js.map +1 -1
  210. package/dist/tools/member-detail.d.ts +2 -2
  211. package/dist/tools/member-detail.d.ts.map +1 -1
  212. package/dist/tools/member-detail.js +17 -0
  213. package/dist/tools/member-detail.js.map +1 -1
  214. package/dist/tools/member-reservation.d.ts +58 -1
  215. package/dist/tools/member-reservation.d.ts.map +1 -1
  216. package/dist/tools/member-reservation.js +61 -20
  217. package/dist/tools/member-reservation.js.map +1 -1
  218. package/dist/tools/provision-auth.d.ts +80 -1
  219. package/dist/tools/provision-auth.d.ts.map +1 -1
  220. package/dist/tools/provision-auth.js +142 -54
  221. package/dist/tools/provision-auth.js.map +1 -1
  222. package/dist/tools/provision-vcs-auth.d.ts +91 -1
  223. package/dist/tools/provision-vcs-auth.d.ts.map +1 -1
  224. package/dist/tools/provision-vcs-auth.js +209 -49
  225. package/dist/tools/provision-vcs-auth.js.map +1 -1
  226. package/dist/tools/register-member.d.ts +12 -9
  227. package/dist/tools/register-member.d.ts.map +1 -1
  228. package/dist/tools/register-member.js +99 -12
  229. package/dist/tools/register-member.js.map +1 -1
  230. package/dist/tools/remove-member.d.ts.map +1 -1
  231. package/dist/tools/remove-member.js +27 -2
  232. package/dist/tools/remove-member.js.map +1 -1
  233. package/dist/tools/send-email.d.ts +2 -2
  234. package/dist/tools/setup-git-app.d.ts.map +1 -1
  235. package/dist/tools/setup-git-app.js +11 -7
  236. package/dist/tools/setup-git-app.js.map +1 -1
  237. package/dist/tools/shutdown-server.d.ts +2 -0
  238. package/dist/tools/shutdown-server.d.ts.map +1 -1
  239. package/dist/tools/shutdown-server.js +23 -1
  240. package/dist/tools/shutdown-server.js.map +1 -1
  241. package/dist/tools/update-member.d.ts +12 -9
  242. package/dist/tools/update-member.d.ts.map +1 -1
  243. package/dist/tools/update-member.js +43 -11
  244. package/dist/tools/update-member.js.map +1 -1
  245. package/dist/tools/vcs-credential-exec.d.ts +127 -0
  246. package/dist/tools/vcs-credential-exec.d.ts.map +1 -0
  247. package/dist/tools/vcs-credential-exec.js +197 -0
  248. package/dist/tools/vcs-credential-exec.js.map +1 -0
  249. package/dist/types.d.ts +13 -0
  250. package/dist/types.d.ts.map +1 -1
  251. package/dist/utils/log-helpers.d.ts.map +1 -1
  252. package/dist/utils/log-helpers.js +2 -2
  253. package/dist/utils/log-helpers.js.map +1 -1
  254. package/dist/utils/prompt-errors.d.ts.map +1 -1
  255. package/dist/utils/prompt-errors.js +7 -1
  256. package/dist/utils/prompt-errors.js.map +1 -1
  257. package/dist/utils/shell-escape.d.ts +16 -0
  258. package/dist/utils/shell-escape.d.ts.map +1 -1
  259. package/dist/utils/shell-escape.js +22 -2
  260. package/dist/utils/shell-escape.js.map +1 -1
  261. package/dist/utils/vcs-provider-detect.d.ts +65 -0
  262. package/dist/utils/vcs-provider-detect.d.ts.map +1 -0
  263. package/dist/utils/vcs-provider-detect.js +109 -0
  264. package/dist/utils/vcs-provider-detect.js.map +1 -0
  265. package/dist/utils/workspace-trust.d.ts +20 -17
  266. package/dist/utils/workspace-trust.d.ts.map +1 -1
  267. package/dist/utils/workspace-trust.js +87 -1
  268. package/dist/utils/workspace-trust.js.map +1 -1
  269. package/dist/workflows/auto-sprint.js +176 -15
  270. package/package.json +6 -4
  271. package/packages/apra-fleet-client/src/client/api.mjs +319 -17
  272. package/packages/apra-fleet-client/src/client/errors.mjs +6 -2
  273. package/packages/apra-fleet-se/apra-pm/agents/_shared/GRAPH-SEMANTICS.md +10 -2
  274. package/packages/apra-fleet-se/apra-pm/agents/backlog-groomer.md +25 -0
  275. package/packages/apra-fleet-se/apra-pm/agents/ci-watcher.md +11 -0
  276. package/packages/apra-fleet-se/apra-pm/agents/deployer.md +37 -2
  277. package/packages/apra-fleet-se/apra-pm/agents/doer.md +36 -12
  278. package/packages/apra-fleet-se/apra-pm/agents/harvester.md +11 -0
  279. package/packages/apra-fleet-se/apra-pm/agents/integ-test-runner.md +11 -0
  280. package/packages/apra-fleet-se/apra-pm/agents/kb-reconciler.md +9 -0
  281. package/packages/apra-fleet-se/apra-pm/agents/plan-reviewer.md +110 -22
  282. package/packages/apra-fleet-se/apra-pm/agents/planner.md +125 -37
  283. package/packages/apra-fleet-se/apra-pm/agents/regression-test-runner.md +11 -0
  284. package/packages/apra-fleet-se/apra-pm/agents/reviewer.md +13 -2
  285. package/packages/apra-fleet-se/apra-pm/agents/schemas/plan-reviewer-output.json +37 -0
  286. package/packages/apra-fleet-se/apra-pm/skills/pm/SKILL.md +1 -1
  287. package/packages/apra-fleet-se/apra-pm/skills/pm/cost.md +0 -3
  288. package/packages/apra-fleet-se/apra-pm/skills/pm/fleet-addendum.md +2 -2
  289. package/packages/apra-fleet-se/bin/cli.mjs +149 -1
  290. package/packages/apra-fleet-se/bin/serve.mjs +78 -4
  291. package/packages/apra-fleet-se/fleet-sprint/abort.mjs +481 -0
  292. package/packages/apra-fleet-se/fleet-sprint/beads-children.mjs +491 -0
  293. package/packages/apra-fleet-se/fleet-sprint/beads-identity-check.mjs +282 -0
  294. package/packages/apra-fleet-se/fleet-sprint/beads-identity.mjs +208 -0
  295. package/packages/apra-fleet-se/fleet-sprint/beads-scope.mjs +638 -0
  296. package/packages/apra-fleet-se/fleet-sprint/beads-transitions.mjs +301 -0
  297. package/packages/apra-fleet-se/fleet-sprint/branch-ensure.mjs +96 -0
  298. package/packages/apra-fleet-se/fleet-sprint/conflict-ladder.mjs +8 -0
  299. package/packages/apra-fleet-se/fleet-sprint/contracts.mjs +61 -14
  300. package/packages/apra-fleet-se/fleet-sprint/coordination.mjs +516 -0
  301. package/packages/apra-fleet-se/fleet-sprint/dispatch-failure.mjs +184 -0
  302. package/packages/apra-fleet-se/fleet-sprint/dispatch-role.mjs +1121 -0
  303. package/packages/apra-fleet-se/fleet-sprint/dispatch-safety-guard.mjs +216 -4
  304. package/packages/apra-fleet-se/fleet-sprint/docs/README.md +1 -1
  305. package/packages/apra-fleet-se/fleet-sprint/docs/dolt-sync-redesign.md +276 -0
  306. package/packages/apra-fleet-se/fleet-sprint/docs/escalate-to-llm-design.md +1 -1
  307. package/packages/apra-fleet-se/fleet-sprint/docs/run-sprint-cycle-prelude-decision.md +215 -0
  308. package/packages/apra-fleet-se/fleet-sprint/dolt-literal-guard.mjs +56 -1
  309. package/packages/apra-fleet-se/fleet-sprint/dolt-sync.mjs +996 -32
  310. package/packages/apra-fleet-se/fleet-sprint/errors.mjs +267 -0
  311. package/packages/apra-fleet-se/fleet-sprint/explicit-id-create-guard.mjs +501 -0
  312. package/packages/apra-fleet-se/fleet-sprint/fatal-diagnostics.mjs +134 -0
  313. package/packages/apra-fleet-se/fleet-sprint/full-db-fetch-guard.mjs +102 -0
  314. package/packages/apra-fleet-se/fleet-sprint/git-sync.mjs +556 -0
  315. package/packages/apra-fleet-se/fleet-sprint/git-topology.mjs +495 -0
  316. package/packages/apra-fleet-se/fleet-sprint/guarded-modules.mjs +564 -0
  317. package/packages/apra-fleet-se/fleet-sprint/inline-ladder-guard.mjs +216 -0
  318. package/packages/apra-fleet-se/fleet-sprint/kb.mjs +728 -0
  319. package/packages/apra-fleet-se/fleet-sprint/mcp-result.mjs +75 -0
  320. package/packages/apra-fleet-se/fleet-sprint/member-provisioning.mjs +252 -0
  321. package/packages/apra-fleet-se/fleet-sprint/member-sync.mjs +581 -0
  322. package/packages/apra-fleet-se/fleet-sprint/member-target.mjs +93 -0
  323. package/packages/apra-fleet-se/fleet-sprint/newtask-text.mjs +266 -0
  324. package/packages/apra-fleet-se/fleet-sprint/parent-notes-staleness.mjs +201 -0
  325. package/packages/apra-fleet-se/fleet-sprint/phases/deploy.mjs +136 -0
  326. package/packages/apra-fleet-se/fleet-sprint/phases/develop.mjs +822 -0
  327. package/packages/apra-fleet-se/fleet-sprint/phases/ensure-sprint-branch.mjs +294 -0
  328. package/packages/apra-fleet-se/fleet-sprint/phases/final-review.mjs +403 -0
  329. package/packages/apra-fleet-se/fleet-sprint/phases/harvest.mjs +180 -0
  330. package/packages/apra-fleet-se/fleet-sprint/phases/integ-test.mjs +358 -0
  331. package/packages/apra-fleet-se/fleet-sprint/phases/plan.mjs +390 -0
  332. package/packages/apra-fleet-se/fleet-sprint/phases/publish-pr.mjs +323 -0
  333. package/packages/apra-fleet-se/fleet-sprint/phases/re-review.mjs +232 -0
  334. package/packages/apra-fleet-se/fleet-sprint/phases/regression-test.mjs +174 -0
  335. package/packages/apra-fleet-se/fleet-sprint/phases/replan.mjs +197 -0
  336. package/packages/apra-fleet-se/fleet-sprint/phases/review.mjs +305 -0
  337. package/packages/apra-fleet-se/fleet-sprint/prompts.mjs +559 -0
  338. package/packages/apra-fleet-se/fleet-sprint/role-policies.mjs +1416 -0
  339. package/packages/apra-fleet-se/fleet-sprint/round-session.mjs +100 -0
  340. package/packages/apra-fleet-se/fleet-sprint/runner.js +1467 -9798
  341. package/packages/apra-fleet-se/fleet-sprint/se-posix.mjs +17 -12
  342. package/packages/apra-fleet-se/fleet-sprint/shell-command-guard.mjs +304 -0
  343. package/packages/apra-fleet-se/fleet-sprint/skills/fleet-sprint-cli/SKILL.md +18 -0
  344. package/packages/apra-fleet-se/fleet-sprint/skills/fleet-supervisor/SKILL.md +21 -2
  345. package/packages/apra-fleet-se/fleet-sprint/sprint-args.mjs +454 -0
  346. package/packages/apra-fleet-se/fleet-sprint/sprint-report.mjs +239 -0
  347. package/packages/apra-fleet-se/fleet-sprint/sprint-state.mjs +163 -0
  348. package/packages/apra-fleet-se/fleet-sprint/unbracketed-push-guard.mjs +234 -0
  349. package/packages/apra-fleet-se/fleet-sprint/usage-limit-controller.mjs +211 -0
  350. package/packages/apra-fleet-se/fleet-sprint/vcs-auth.mjs +1279 -0
  351. package/packages/apra-fleet-se/fleet-sprint/vcs-module.mjs +153 -19
  352. package/packages/apra-fleet-se/fleet-sprint/vcs-providers/azure-devops.mjs +71 -21
  353. package/packages/apra-fleet-se/fleet-sprint/vcs-providers/bitbucket.mjs +28 -0
  354. package/packages/apra-fleet-se/fleet-sprint/vcs-providers/github.mjs +131 -0
  355. package/packages/apra-fleet-se/fleet-sprint/vcs-providers/index.mjs +140 -16
  356. package/packages/apra-fleet-se/fleet-sprint/vcs-providers/shell-helpers.mjs +1 -0
  357. package/packages/apra-fleet-se/fleet-sprint/viewer-extensions.mjs +77 -2
  358. package/packages/apra-fleet-se/fleet-sprint/worklists.mjs +559 -0
  359. package/packages/apra-fleet-se/package.json +1 -1
  360. package/packages/apra-fleet-workflow/src/viewer/index.mjs +17 -6
  361. package/packages/apra-fleet-workflow/src/workflow/index.mjs +49 -7
  362. package/skills/fleet/SKILL.md +8 -8
  363. package/skills/fleet/auth-azdevops.md +14 -3
  364. package/skills/fleet/auth-bitbucket.md +2 -2
  365. package/skills/fleet/auth-github.md +5 -4
  366. package/skills/fleet/onboarding.md +3 -3
  367. package/skills/fleet/permissions.md +5 -1
  368. package/skills/fleet/profiles/node.json +1 -1
  369. package/skills/fleet/troubleshooting.md +3 -3
  370. package/version.json +1 -1
@@ -23,6 +23,7 @@ import { resolveTilde } from './execute-command.js';
23
23
  import { clearStoredPid } from '../utils/agent-helpers.js';
24
24
  import { tryKillPid, isPidAlive } from '../utils/pid-helpers.js';
25
25
  import { recoverOrphanedDispatch, isRemoteProcessAlive } from '../services/orphan-recovery.js';
26
+ import { hasSecretToken } from '../services/secret-token.js';
26
27
  import { seedWorkspaceTrust } from '../utils/workspace-trust.js';
27
28
  import { durableOutputPath } from '../os/linux.js';
28
29
  import { LogScope, logLine, logWarn, maskSecrets, truncateForLog } from '../utils/log-helpers.js';
@@ -37,6 +38,7 @@ import { sendMessage } from './send-message.js';
37
38
  import { registerPending } from '../services/pending-responses.js';
38
39
  import { isMaxTurnsResponse } from '../providers/provider.js';
39
40
  import { preflightCheck } from '../services/preflight-check.js';
41
+ import { ensureAgyProject } from '../services/agy-project.js';
40
42
  export const executePromptSchema = z.object({
41
43
  ...memberIdentifier,
42
44
  prompt: z.string().describe('The prompt to send to the LLM on the remote member'),
@@ -46,7 +48,16 @@ export const executePromptSchema = z.object({
46
48
  'false = always start a fresh session. ' +
47
49
  'A session-id STRING = EXPLICIT resume of exactly that session, preferred over the member\'s stored session -- the caller asserts this prompt depends on that session\'s prior context, so an unknown/expired id is a TERMINAL error ' +
48
50
  '(structured {isError, reason: "session_not_found"}, NO LLM call, and NO fresh-session fallback) rather than a silent wrong-context dispatch.'),
49
- timeout_s: z.number().default(300).describe('Inactivity timeout in seconds -- the command is killed after this many seconds without any stdout/stderr output (default: 300s / 5 minutes)'),
51
+ fork: z.union([z.boolean(), z.string()]).optional().describe('Branch a NEW session from an existing one instead of continuing it in place. ' +
52
+ 'Mirrors resume\'s shape, but BRANCHES rather than continues: it mints a distinct new ' +
53
+ 'session id seeded from the source session\'s context, leaving the source session itself ' +
54
+ 'untouched. true = fork from the member\'s stored last session. A session-id STRING = fork ' +
55
+ 'from exactly that session. Mutually exclusive with resume (any non-default value, i.e. ' +
56
+ 'false or a session-id string) and with session_id (a resume shorthand) -- specifying fork ' +
57
+ 'together with either is rejected as a validation error before any member resolution or LLM ' +
58
+ 'call. NOTE: this field only covers the schema + the mutual-exclusivity guard; actual fork ' +
59
+ 'MODE RESOLUTION (minting/wiring the forked session) is implemented separately.'),
60
+ timeout_s: z.number().default(300).describe('Inactivity timeout in seconds -- always drives the stall detector\'s per-dispatch baseline threshold, measured against the member\'s own session transcript activity (default: 300s / 5 minutes). Omitting it yields a 300s baseline, a deliberate change from the previously silent 150s stall-detector default. Per-provider, it ALSO arms the exec-level rolling timer against this dispatch\'s stdout/stderr channel for Codex and Copilot, which have no pollable transcript; Claude and AGY take that exec-channel ceiling from max_total_s instead; and OpenCode keeps BOTH signals armed at once (this exec-channel timer plus coarse log-directory-mtime polling, combined with OR semantics -- either advancing counts as not-stalled), since its transcript signal is directory-level only, not a per-turn file (see ProviderAdapter.execTimeoutSource()).'),
50
61
  max_total_s: z.number().optional().describe('Hard ceiling in seconds -- the command is killed after this total elapsed time regardless of activity. If omitted, there is no total time limit.'),
51
62
  max_turns: z.number().min(1).max(500).optional().describe('Max turns for claude -p (default: 50)'),
52
63
  model: z.string().optional().describe('Model tier ("cheap", "standard", "premium") or a specific model ID for power users. Prefer tier names -- the server resolves them to the correct model per provider. If omitted, defaults to the standard tier. Applies to both new and resumed sessions.'),
@@ -56,8 +67,7 @@ export const executePromptSchema = z.object({
56
67
  'Extra keys are silently ignored. Values are never logged.'),
57
68
  agent: z.string().optional().describe('Optional agent name to activate. ' +
58
69
  'For Claude: invokes claude --agent <name>. ' +
59
- 'For AGY: prepends @<name> to the prompt on every dispatch. ' +
60
- 'Substitution runs before the @<name> prepend. ' +
70
+ 'For AGY: invokes agy --agent <name>. ' +
61
71
  'Agent file must exist at the provider-specific path on the member: ' +
62
72
  'Claude: <workFolder>/.claude/agents/<name>.md or ~/.claude/agents/<name>.md; ' +
63
73
  'AGY: <workFolder>/.gemini/antigravity-cli/agents/<name>.md or ~/.gemini/antigravity-cli/agents/<name>.md; ' +
@@ -100,6 +110,16 @@ ${output}`;
100
110
  : `[FAIL] Prompt failed on "${agentName}":
101
111
  ${output}`;
102
112
  }
113
+ // apra-fleet-25yl.2.1: the exec-level rolling deadline used for a provider
114
+ // whose ExecTimeoutSource is 'total_ceiling' when the caller supplied NO
115
+ // max_total_s -- i.e. there is no ceiling to mirror. 24h: large enough that it
116
+ // can never bind before any realistic dispatch or client-side deadline, and
117
+ // small enough to stay inside the int32 range setTimeout accepts (Infinity or
118
+ // MAX_SAFE_INTEGER overflow to "fire immediately", which would invert the
119
+ // decoupling into an instant kill). The alternative -- falling back to
120
+ // timeout_s -- is deliberately NOT taken: that is the exact coupling this
121
+ // removes, and for these providers timeout_s belongs to the StallDetector.
122
+ const EXEC_TIMER_NEVER_BINDS_MS = 86_400_000;
103
123
  const SERVER_RETRY_DELAY_MS = 5000;
104
124
  // A prompt written whole into a single remote exec command line can exceed
105
125
  // the SSH exec channel's / Windows CreateProcess's command-line ceiling once
@@ -212,7 +232,6 @@ export function resolveModelForTier(agent, tier, provider) {
212
232
  * shared with member_detail (the fleet-sprint engine's only source for it).
213
233
  */
214
234
  export { knownRepoRemoteUrl };
215
- const SECURE_TOKEN_RE = /\{\{secure\.[a-zA-Z0-9_-]{1,64}\}\}/;
216
235
  /**
217
236
  * The sprint id this server process dispatches on behalf of, or undefined when
218
237
  * run outside a sprint (e.g. a manual cli.mjs invocation). Sourced from
@@ -433,8 +452,44 @@ async function executePromptInteractive(agent, renderedPrompt, input, workspaceI
433
452
  }
434
453
  }
435
454
  export async function executePrompt(input, extra) {
436
- if (SECURE_TOKEN_RE.test(input.prompt)) {
437
- return 'error: execute_prompt prompt contains {{secure.NAME}} token. Secrets must never be passed to LLM prompts. Use execute_command with {{secure.NAME}} instead.';
455
+ if (hasSecretToken(input.prompt)) {
456
+ return 'error: execute_prompt prompt contains {{secret.NAME}} token. Secrets must never be passed to LLM prompts. Use execute_command with {{secret.NAME}} instead.';
457
+ }
458
+ // apra-fleet-3swo.42: normalise `fork` ONCE, here, before any predicate is
459
+ // derived from it. Previously `forkRequested` (below) checked
460
+ // input.fork.length > 0 (untrimmed) while `explicitForkId` (further down)
461
+ // checked input.fork.trim().length > 0 -- for a whitespace-only fork id the
462
+ // two disagreed (forkRequested true, explicitForkId undefined), which
463
+ // silently routed a caller's explicit-but-invalid fork id into the
464
+ // fork===true best-effort path and could hand back a plain FRESH session
465
+ // with no error. Both forkRequested and explicitForkId are now derived from
466
+ // this single normalised value, so an all-whitespace or empty string can
467
+ // never disagree with itself. Trimming a boolean/undefined value is a
468
+ // no-op -- the ternary passes it through unchanged.
469
+ const forkArg = typeof input.fork === 'string' ? input.fork.trim() : input.fork;
470
+ // fork/resume mutual-exclusivity guard (apra-fleet-lmtg.4): fork branches a
471
+ // NEW session from an existing one, resume continues IN PLACE -- the two
472
+ // are semantically incompatible, so a call requesting both is rejected here,
473
+ // before any member resolution or LLM invocation, rather than silently
474
+ // preferring one. session_id is documented as pure resume shorthand, so its
475
+ // mere presence also conflicts with fork even when the `resume` field
476
+ // itself still holds its default. "Non-default resume" means any value
477
+ // other than the schema default `true` (i.e. `false`, or an explicit
478
+ // session-id string) -- default-true resume is left alone since fork mode
479
+ // resolution (next task) supersedes it.
480
+ // Deliberately NOT `forkArg.length > 0`: any STRING value of fork (including
481
+ // '' and whitespace-only, once trimmed to '') is a fork REQUEST -- just one
482
+ // whose explicit id turns out to be invalid (see explicitForkId below),
483
+ // rejected with a terminal session_not_found rather than silently treated
484
+ // as "no fork requested at all".
485
+ const forkRequested = forkArg === true || typeof forkArg === 'string';
486
+ if (forkRequested) {
487
+ if (input.session_id !== undefined) {
488
+ return 'error: execute_prompt cannot set both "fork" and "session_id" -- session_id is resume shorthand, and fork branches a new session instead of resuming. Specify only one.';
489
+ }
490
+ if (input.resume !== true) {
491
+ return 'error: execute_prompt cannot set both "fork" and a non-default "resume" -- fork branches a new session instead of resuming. Specify only one.';
492
+ }
438
493
  }
439
494
  // Validate substitution keys before any I/O or member resolution.
440
495
  if (input.substitutions !== undefined) {
@@ -537,965 +592,1214 @@ export async function executePrompt(input, extra) {
537
592
  // return immediately following) must release this lock explicitly, since
538
593
  // it now returns AFTER the lock is claimed instead of before.
539
594
  inFlightAgents.add(agent.id);
540
- // Peek at session state early so the preflight check can skip interactive
541
- // members whose dispatch routes through a live MCP push channel, not SSH.
542
- const earlyWorkspaceId = getTokenIssuer().workspaceId();
543
- const earlySession = sessionRegistry.get(earlyWorkspaceId, agent.id);
544
- const isChannelCapable = !!earlySession?.channelCapable;
545
- // Pre-dispatch readiness check (apra-fleet preflight-check): verify
546
- // connectivity and LLM auth BEFORE the expensive prompt dispatch
547
- // (writePromptFile + CLI invocation). Catches expired OAuth, missing
548
- // credentials, and offline members in <1s instead of burning a full
549
- // round trip. Local members and interactive sessions are excluded
550
- // (local shares this machine's credentials; interactive sessions have
551
- // their own liveness probes). The check is cached for 60s so
552
- // back-to-back dispatches do not add latency.
553
- if (agent.agentType !== 'local' && !isChannelCapable) {
554
- let preflight;
555
- try {
556
- preflight = await preflightCheck(agent);
557
- }
558
- catch (err) {
559
- // preflightCheck's own synchronous prologue (getStrategy/getProvider)
560
- // and its internal exec calls are not fully guarded -- if it throws
561
- // instead of resolving {ok: false}, the lock claimed above would
562
- // otherwise leak forever (no pid captured yet for findDeadLockPid's
563
- // stale-lock self-heal to recognize). Release and propagate unchanged
564
- // -- this preserves the exact same exception the caller would have
565
- // seen before the lock was claimed this early.
566
- inFlightAgents.delete(agent.id);
567
- throw err;
568
- }
569
- if (!preflight.ok) {
570
- // R2-F5: use preflight-specific reason codes so fleet-sprint can
571
- // distinguish pre-dispatch failures from in-dispatch ones and avoid
572
- // inappropriate self-heal loops (e.g. re-provisioning auth when the
573
- // member is simply offline).
574
- const preflightReason = preflight.code === 'offline' ? 'preflight_offline'
575
- : preflight.code === 'auth_expired' ? 'preflight_auth_expired'
576
- : preflight.code === 'auth_missing' ? 'preflight_auth_missing'
577
- : 'dispatch_failed';
578
- // Release the lock claimed above -- this return happens before the
579
- // interactive/subprocess split's own add+finally cleanup ever runs.
580
- inFlightAgents.delete(agent.id);
581
- return {
582
- text: `[FAIL] Pre-dispatch check failed for "${agent.friendlyName}": ${preflight.reason}`,
583
- structuredContent: {
584
- isError: true,
585
- reason: preflightReason,
586
- },
587
- };
595
+ // apra-fleet-c98q: if anything between this claim and a try/finally that
596
+ // owns the release throws (e.g. an SSH drop in writePromptFile), release
597
+ // the lock and stall entry here and rethrow. Body deliberately not
598
+ // reindented to keep this release fix minimal.
599
+ let claimGuardActive = true;
600
+ try {
601
+ // Peek at session state early so the preflight check can skip interactive
602
+ // members whose dispatch routes through a live MCP push channel, not SSH.
603
+ const earlyWorkspaceId = getTokenIssuer().workspaceId();
604
+ const earlySession = sessionRegistry.get(earlyWorkspaceId, agent.id);
605
+ const isChannelCapable = !!earlySession?.channelCapable;
606
+ // Pre-dispatch readiness check (apra-fleet preflight-check): verify
607
+ // connectivity and LLM auth BEFORE the expensive prompt dispatch
608
+ // (writePromptFile + CLI invocation). Catches expired OAuth, missing
609
+ // credentials, and offline members in <1s instead of burning a full
610
+ // round trip. Local members and interactive sessions are excluded
611
+ // (local shares this machine's credentials; interactive sessions have
612
+ // their own liveness probes). The check is cached for 60s so
613
+ // back-to-back dispatches do not add latency.
614
+ if (agent.agentType !== 'local' && !isChannelCapable) {
615
+ let preflight;
616
+ try {
617
+ preflight = await preflightCheck(agent);
618
+ }
619
+ catch (err) {
620
+ // preflightCheck's own synchronous prologue (getStrategy/getProvider)
621
+ // and its internal exec calls are not fully guarded -- if it throws
622
+ // instead of resolving {ok: false}, the lock claimed above would
623
+ // otherwise leak forever (no pid captured yet for findDeadLockPid's
624
+ // stale-lock self-heal to recognize). Release and propagate unchanged
625
+ // -- this preserves the exact same exception the caller would have
626
+ // seen before the lock was claimed this early.
627
+ inFlightAgents.delete(agent.id);
628
+ throw err;
629
+ }
630
+ if (!preflight.ok) {
631
+ // R2-F5: use preflight-specific reason codes so fleet-sprint can
632
+ // distinguish pre-dispatch failures from in-dispatch ones and avoid
633
+ // inappropriate self-heal loops (e.g. re-provisioning auth when the
634
+ // member is simply offline).
635
+ const preflightReason = preflight.code === 'offline' ? 'preflight_offline'
636
+ : preflight.code === 'auth_expired' ? 'preflight_auth_expired'
637
+ : preflight.code === 'auth_missing' ? 'preflight_auth_missing'
638
+ : 'dispatch_failed';
639
+ // Release the lock claimed above -- this return happens before the
640
+ // interactive/subprocess split's own add+finally cleanup ever runs.
641
+ inFlightAgents.delete(agent.id);
642
+ return {
643
+ text: `[FAIL] Pre-dispatch check failed for "${agent.friendlyName}": ${preflight.reason}`,
644
+ structuredContent: {
645
+ isError: true,
646
+ reason: preflightReason,
647
+ },
648
+ };
649
+ }
588
650
  }
589
- }
590
- // Interactive routing (apra-fleet-2xs.8/us9.8, docs/cloud-fleet-architecture.md
591
- // section 6): if this member has a live MCP session connected right now,
592
- // route via send_message + wait-for-response instead of spawning a
593
- // subprocess. Decided tier-2-locally against THIS machine's session
594
- // registry only (never caller/hub-side state, per apra-fleet-2xs.8's own
595
- // scope note) -- so behavior is unaffected by whether execute_prompt is
596
- // invoked directly (Phase 1) or relayed through a future hub. Falls
597
- // through to the unchanged subprocess/SSH path below for every member
598
- // without a live session (the common case today, and always for members
599
- // that never opt into an interactive session).
600
- //
601
- // Gated by capability, not provider name (apra-fleet-cqa, eft.74 follow-up):
602
- // mode (b) -- server-push mid-session prompt injection -- was POC-proven on
603
- // Claude via the provider-branded `notifications/claude/channel` capability
604
- // (apra-fleet-us9.9's survey, docs/interactive-injection-provider-survey.md),
605
- // but the routing decision itself must be provider-agnostic: whatever the
606
- // provider, a session is only an interactive-routing candidate if it
607
- // actually declared that capability at MCP initialize time (recorded as
608
- // SessionState.channelCapable below). Codex is confirmed [FAIL]
609
- // today (no equivalent push mechanism) and so never ends up channelCapable in
610
- // practice, but that is a fact about what each provider adapter currently
611
- // advertises, not a name-based pre-filter here -- any provider that
612
- // implements the same MCP channel capability is picked up automatically. A
613
- // member CAN still have a live sessionRegistry entry (registerMcpEndpoint
614
- // gives it basic MCP tool access, apra-fleet-fnz.1-3) without that meaning
615
- // it can receive or act on this push -- routing to it anyway would silently
616
- // spend the full timeout_s waiting for a response that can never arrive.
617
- // R2-F3: re-query session registry after the preflight await (10-20s) so
618
- // interactive routing sees sessions that became channelCapable during that
619
- // window, rather than using the stale pre-preflight snapshot.
620
- const workspaceId = getTokenIssuer().workspaceId();
621
- const rawSession = sessionRegistry.get(workspaceId, agent.id);
622
- // apra-fleet-eft.74.1: interactive routing requires the EXPLICIT channel
623
- // opt-in handshake, not mere JWT registration. A plain subprocess
624
- // connect-back (a Doer that opened an MCP tool-access session with a member
625
- // JWT but never declared the `claude/channel` capability) registers a live
626
- // `server` here, yet can never receive the `notifications/claude/channel`
627
- // push -- routing to it would enqueue a message nothing reads and burn the
628
- // full timeout_s on every later dispatch (the eft.74 wedge). Only a
629
- // channel-capable session is an interactive-routing candidate; anything else
630
- // (including that Doer's live tool session, which must be left untouched)
631
- // falls through to the unchanged subprocess path below.
632
- let interactiveSession = rawSession?.channelCapable ? rawSession : undefined;
633
- // apra-fleet-eft.50.1: resolve the pid to test FRESH on every dispatch
634
- // (never cached from a prior attempt) and fall back to the durable
635
- // launch-pid anchor when this reused session lost its own pid on a
636
- // reconnect. This is what re-arms the dead-session guard on a retry attempt
637
- // 2+ exactly as on attempt 1: the specific eft.50 ordering (attempt 1 fails
638
- // clean, attempt 2 targets a now-dead reconnected session) used to slip
639
- // through here because the reconnected SessionState had pid=undefined, so the
640
- // check below was skipped and the caller hung on the dead channel.
641
- const interactivePid = interactiveSession?.pid
642
- ?? sessionRegistry.lastKnownPid(workspaceId, agent.id);
643
- if (interactiveSession?.server && interactivePid !== undefined && !isPidAlive(interactivePid)) {
644
- // apra-fleet-eft.28.1/eft.28.5: never reuse a persistent interactive
645
- // session whose underlying member claude process has already died. Before
646
- // eft.28.1, a dead launch-time process (e.g. it crashed before ever
647
- // producing a plan) left a `server` entry in sessionRegistry that looked
648
- // reusable -- send_message would happily enqueue to it, but nothing would
649
- // ever call respond_to_message, so the caller silently burned the full
650
- // timeout_s (observed up to 3600s in apra-fleet-eft.28) with zero
651
- // fleet-server log output and no watchdog coverage.
651
+ // Interactive routing (apra-fleet-2xs.8/us9.8, docs/cloud-fleet-architecture.md
652
+ // section 6): if this member has a live MCP session connected right now,
653
+ // route via send_message + wait-for-response instead of spawning a
654
+ // subprocess. Decided tier-2-locally against THIS machine's session
655
+ // registry only (never caller/hub-side state, per apra-fleet-2xs.8's own
656
+ // scope note) -- so behavior is unaffected by whether execute_prompt is
657
+ // invoked directly (Phase 1) or relayed through a future hub. Falls
658
+ // through to the unchanged subprocess/SSH path below for every member
659
+ // without a live session (the common case today, and always for members
660
+ // that never opt into an interactive session).
652
661
  //
653
- // eft.28.5 changes what happens once the death is detected: instead of
654
- // surfacing a terminal dispatch_failed error that forces a manual
655
- // register_member, EVICT the dead session and FALL THROUGH to a fresh
656
- // non-interactive (subprocess) dispatch below -- i.e. re-dispatch fresh
657
- // instead of blocking on waitForInteractiveResponse. The bug in
658
- // apra-fleet-eft.28 was precisely that a dead session was reused "rather
659
- // than detecting its death and spawning a fresh dispatch"; this does the
660
- // spawning. If the fresh subprocess dispatch itself cannot start it
661
- // returns its own terminal error, so nothing ever hangs.
662
- //
663
- // The liveness check now fires for connect-back interactive sessions too:
664
- // http-transport carries the launch-time pid forward across re-registration
665
- // (eft.28.5), so `pid` is no longer undefined for a member that registered
666
- // via register_member and then connected back -- the exact real-fleet
667
- // repro that evaded eft.28.1.
668
- //
669
- // apra-fleet-eft.50.1: eft.28.5's carry-forward still lost the pid when a
670
- // reconnect happened AFTER the prior SessionState was already unregistered
671
- // (priorPid lookup found nothing), so a retry attempt 2+ reused a
672
- // pid=undefined session and hung. `interactivePid` above now back-stops
673
- // that with sessionRegistry.lastKnownPid, the durable per-member launch-pid
674
- // anchor, so this guard re-arms on EVERY dispatch attempt that reuses an
675
- // interactive session, not just the first. It stays undefined only for
676
- // sessions that never had a captured PID at all (e.g. tests, or a provider
677
- // that never went through register_member's local spawn path); those are
678
- // left to the pre-existing interactive behavior, unchanged.
679
- const deadScope = new LogScope('execute_prompt', `[interactive] session liveness check pid=${interactivePid}`, agent);
680
- sessionRegistry.unregister(workspaceId, agent.id);
681
- deadScope.info(`member claude process (pid ${interactivePid}) for "${agent.friendlyName}" is dead -- evicting the stale interactive session and re-dispatching fresh (non-interactive)`);
682
- interactiveSession = undefined;
683
- }
684
- if (interactiveSession?.server) {
685
- // Lock already claimed above, before the preflight await -- do not
686
- // re-add here (Set.add would be a harmless no-op, but keeping a second
687
- // add site invites the lock and its release to drift out of sync).
688
- writeStatusline(new Map([[agent.id, 'busy']]));
689
- try {
690
- return await executePromptInteractive(agent, renderedPrompt, input, workspaceId, heuristicWarningSuffix);
662
+ // Gated by capability, not provider name (apra-fleet-cqa, eft.74 follow-up):
663
+ // mode (b) -- server-push mid-session prompt injection -- was POC-proven on
664
+ // Claude via the provider-branded `notifications/claude/channel` capability
665
+ // (apra-fleet-us9.9's survey, docs/interactive-injection-provider-survey.md),
666
+ // but the routing decision itself must be provider-agnostic: whatever the
667
+ // provider, a session is only an interactive-routing candidate if it
668
+ // actually declared that capability at MCP initialize time (recorded as
669
+ // SessionState.channelCapable below). Codex is confirmed [FAIL]
670
+ // today (no equivalent push mechanism) and so never ends up channelCapable in
671
+ // practice, but that is a fact about what each provider adapter currently
672
+ // advertises, not a name-based pre-filter here -- any provider that
673
+ // implements the same MCP channel capability is picked up automatically. A
674
+ // member CAN still have a live sessionRegistry entry (registerMcpEndpoint
675
+ // gives it basic MCP tool access, apra-fleet-fnz.1-3) without that meaning
676
+ // it can receive or act on this push -- routing to it anyway would silently
677
+ // spend the full timeout_s waiting for a response that can never arrive.
678
+ // R2-F3: re-query session registry after the preflight await (10-20s) so
679
+ // interactive routing sees sessions that became channelCapable during that
680
+ // window, rather than using the stale pre-preflight snapshot.
681
+ const workspaceId = getTokenIssuer().workspaceId();
682
+ const rawSession = sessionRegistry.get(workspaceId, agent.id);
683
+ // apra-fleet-eft.74.1: interactive routing requires the EXPLICIT channel
684
+ // opt-in handshake, not mere JWT registration. A plain subprocess
685
+ // connect-back (a Doer that opened an MCP tool-access session with a member
686
+ // JWT but never declared the `claude/channel` capability) registers a live
687
+ // `server` here, yet can never receive the `notifications/claude/channel`
688
+ // push -- routing to it would enqueue a message nothing reads and burn the
689
+ // full timeout_s on every later dispatch (the eft.74 wedge). Only a
690
+ // channel-capable session is an interactive-routing candidate; anything else
691
+ // (including that Doer's live tool session, which must be left untouched)
692
+ // falls through to the unchanged subprocess path below.
693
+ let interactiveSession = rawSession?.channelCapable ? rawSession : undefined;
694
+ // apra-fleet-eft.50.1: resolve the pid to test FRESH on every dispatch
695
+ // (never cached from a prior attempt) and fall back to the durable
696
+ // launch-pid anchor when this reused session lost its own pid on a
697
+ // reconnect. This is what re-arms the dead-session guard on a retry attempt
698
+ // 2+ exactly as on attempt 1: the specific eft.50 ordering (attempt 1 fails
699
+ // clean, attempt 2 targets a now-dead reconnected session) used to slip
700
+ // through here because the reconnected SessionState had pid=undefined, so the
701
+ // check below was skipped and the caller hung on the dead channel.
702
+ const interactivePid = interactiveSession?.pid
703
+ ?? sessionRegistry.lastKnownPid(workspaceId, agent.id);
704
+ if (interactiveSession?.server && interactivePid !== undefined && !isPidAlive(interactivePid)) {
705
+ // apra-fleet-eft.28.1/eft.28.5: never reuse a persistent interactive
706
+ // session whose underlying member claude process has already died. Before
707
+ // eft.28.1, a dead launch-time process (e.g. it crashed before ever
708
+ // producing a plan) left a `server` entry in sessionRegistry that looked
709
+ // reusable -- send_message would happily enqueue to it, but nothing would
710
+ // ever call respond_to_message, so the caller silently burned the full
711
+ // timeout_s (observed up to 3600s in apra-fleet-eft.28) with zero
712
+ // fleet-server log output and no watchdog coverage.
713
+ //
714
+ // eft.28.5 changes what happens once the death is detected: instead of
715
+ // surfacing a terminal dispatch_failed error that forces a manual
716
+ // register_member, EVICT the dead session and FALL THROUGH to a fresh
717
+ // non-interactive (subprocess) dispatch below -- i.e. re-dispatch fresh
718
+ // instead of blocking on waitForInteractiveResponse. The bug in
719
+ // apra-fleet-eft.28 was precisely that a dead session was reused "rather
720
+ // than detecting its death and spawning a fresh dispatch"; this does the
721
+ // spawning. If the fresh subprocess dispatch itself cannot start it
722
+ // returns its own terminal error, so nothing ever hangs.
723
+ //
724
+ // The liveness check now fires for connect-back interactive sessions too:
725
+ // http-transport carries the launch-time pid forward across re-registration
726
+ // (eft.28.5), so `pid` is no longer undefined for a member that registered
727
+ // via register_member and then connected back -- the exact real-fleet
728
+ // repro that evaded eft.28.1.
729
+ //
730
+ // apra-fleet-eft.50.1: eft.28.5's carry-forward still lost the pid when a
731
+ // reconnect happened AFTER the prior SessionState was already unregistered
732
+ // (priorPid lookup found nothing), so a retry attempt 2+ reused a
733
+ // pid=undefined session and hung. `interactivePid` above now back-stops
734
+ // that with sessionRegistry.lastKnownPid, the durable per-member launch-pid
735
+ // anchor, so this guard re-arms on EVERY dispatch attempt that reuses an
736
+ // interactive session, not just the first. It stays undefined only for
737
+ // sessions that never had a captured PID at all (e.g. tests, or a provider
738
+ // that never went through register_member's local spawn path); those are
739
+ // left to the pre-existing interactive behavior, unchanged.
740
+ const deadScope = new LogScope('execute_prompt', `[interactive] session liveness check pid=${interactivePid}`, agent);
741
+ sessionRegistry.unregister(workspaceId, agent.id);
742
+ deadScope.info(`member claude process (pid ${interactivePid}) for "${agent.friendlyName}" is dead -- evicting the stale interactive session and re-dispatching fresh (non-interactive)`);
743
+ interactiveSession = undefined;
691
744
  }
692
- finally {
693
- inFlightAgents.delete(agent.id);
694
- writeStatusline(new Map([[agent.id, 'idle']]));
745
+ if (interactiveSession?.server) {
746
+ // Lock already claimed above, before the preflight await -- do not
747
+ // re-add here (Set.add would be a harmless no-op, but keeping a second
748
+ // add site invites the lock and its release to drift out of sync).
749
+ writeStatusline(new Map([[agent.id, 'busy']]));
750
+ claimGuardActive = false;
751
+ try {
752
+ return await executePromptInteractive(agent, renderedPrompt, input, workspaceId, heuristicWarningSuffix);
753
+ }
754
+ finally {
755
+ inFlightAgents.delete(agent.id);
756
+ writeStatusline(new Map([[agent.id, 'idle']]));
757
+ }
695
758
  }
696
- }
697
- // Lock already claimed above, before the preflight await.
698
- await ensureAgentFilesProvisioned(agent);
699
- const stallDetector = getStallDetector();
700
- let clearedByStall = false;
701
- // apra-fleet-3c9.1: a CONFIRMED stall must not only kill the remote pid but
702
- // also cancel the in-flight strategy.execCommand() promise. Before this, the
703
- // client kept waiting out its full deriveTimeoutMs deadline after the
704
- // server-side work had already died (the 60.5-min hung dispatch in
705
- // apra-fleet-3c9). onStall aborts this controller; its signal is merged into
706
- // the signal handed to every execCommand below (see dispatchSignal), so a
707
- // confirmed stall settles the pending dispatch immediately and surfaces a
708
- // typed 'stalled' error instead of hanging.
709
- const stallAbortController = new AbortController();
710
- stallDetector.add(agent.id, {
711
- sessionId: null,
712
- logFilePath: null,
713
- lastActivityAt: Date.now(),
714
- consecutiveIdleCycles: 0,
715
- consecutiveReadFailures: 0,
716
- memberId: agent.id,
717
- memberName: agent.friendlyName,
718
- provisional: true,
719
- stallReported: false,
720
- onStall: () => {
721
- // Stall detector already wrote 'unknown' to the statusline before calling here.
722
- // Our job: clear in-process state so the member can accept new calls.
723
- // clearedByStall prevents the eventually-resolving finally block from clobbering
724
- // a new execute_prompt that may have already claimed the member.
725
- inFlightAgents.delete(agent.id);
726
- clearedByStall = true;
727
- // apra-fleet-6z8.2: a CONFIRMED stall means the remote turn made no
728
- // progress of any kind for the whole threshold. Clearing bookkeeping
729
- // alone left that wedged process running indefinitely on the member --
730
- // burning its LLM session, holding its work folder, and colliding with
731
- // whatever dispatch takes the member next. Kill the tracked pid too.
732
- // Best-effort and never awaited: onStall is a fire-and-forget callback
733
- // from the poll loop, and tryKillPid already swallows its own errors.
734
- void tryKillPid(agent, strategy, cmds).catch(() => { });
735
- // apra-fleet-3c9.1: killing the remote pid alone left the pending
736
- // execCommand promise still awaiting its full deadline. Abort it now so
737
- // the dispatch settles promptly and returns a typed 'stalled' error.
759
+ // Lock already claimed above, before the preflight await.
760
+ // AGY: every dispatch is bound to the member's own agy project with
761
+ // --project; without it agy silently runs under the machine-wide
762
+ // default-cli-project. A member without a verified project (registered
763
+ // before project binding, or whose project file is gone/corrupt) gets one
764
+ // here. Failure is terminal -- never a dispatch without --project.
765
+ let agyProjectId;
766
+ if (agent.llmProvider === 'agy') {
738
767
  try {
739
- stallAbortController.abort();
768
+ agyProjectId = (await ensureAgyProject(agent)).projectId;
769
+ }
770
+ catch (e) {
771
+ inFlightAgents.delete(agent.id);
772
+ writeStatusline(new Map([[agent.id, 'idle']]));
773
+ return {
774
+ text: `[FAIL] execute_prompt on "${agent.friendlyName}" rejected -- the member's agy project could not be provisioned: ${e?.message ?? String(e)}. No LLM call was made.`,
775
+ structuredContent: { isError: true, reason: 'dispatch_failed' },
776
+ };
740
777
  }
741
- catch { /* best-effort */ }
742
- },
743
- });
744
- const tmpDir = agent.agentType === 'local' ? os.tmpdir() : '/tmp';
745
- const resolvedWorkFolder = agent.agentType === 'local' ? resolveTilde(agent.workFolder) : agent.workFolder;
746
- const promptFilePath = agent.agentType === 'local'
747
- ? path.join(resolvedWorkFolder, promptFileName)
748
- : `${resolvedWorkFolder}/${promptFileName}`;
749
- const strategy = getStrategy(agent);
750
- const cmds = getOsCommands(getAgentOS(agent), getAgentShell(agent));
751
- const provider = getProvider(agent.llmProvider);
752
- const authPrefix = buildAuthEnvPrefix(agent, getAgentOS(agent));
753
- const tiers = provider.modelTiers();
754
- let resolvedModel = input.model || 'standard';
755
- let resolvedTier;
756
- if (resolvedModel === 'cheap') {
757
- resolvedTier = 'cheap';
758
- resolvedModel = agent.modelTiers
759
- ? resolveModelForTier(agent, 'cheap', provider)
760
- : agent.modelCheap || getModelOverride(provider.name, 'cheap') || tiers.cheap;
761
- }
762
- else if (resolvedModel === 'standard') {
763
- resolvedTier = 'standard';
764
- resolvedModel = agent.modelTiers
765
- ? resolveModelForTier(agent, 'standard', provider)
766
- : agent.modelStandard || getModelOverride(provider.name, 'standard') || tiers.standard;
767
- }
768
- else if (resolvedModel === 'premium') {
769
- resolvedTier = 'premium';
770
- resolvedModel = agent.modelTiers
771
- ? resolveModelForTier(agent, 'premium', provider)
772
- : agent.modelPremium || getModelOverride(provider.name, 'premium') || tiers.premium;
773
- }
774
- else {
775
- resolvedModel = tiers[resolvedModel] ?? resolvedModel;
776
- }
777
- const scope = new LogScope('execute_prompt', `[${resolvedModel}] resume=${input.resume} timeout=${input.timeout_s ?? 300}s ${truncateForLog(maskSecrets(input.prompt), getLogPreviewChars())}`, agent);
778
- // Resume semantics (apra-fleet-eft.78.1). `resume` is boolean | string:
779
- // - true -> best-effort resume of the member's stored last session; a
780
- // stale/unknown stored session transparently retries fresh.
781
- // - false -> always a fresh session.
782
- // - string -> EXPLICIT session-id resume: resume exactly this id, preferring
783
- // it over agent.sessionId. The caller asserts the prompt depends
784
- // on that session's context, so an unknown/expired id is a
785
- // TERMINAL session_not_found (handled just below) and NO
786
- // fresh-session fallback is ever applied (see the retry paths).
787
- const explicitResumeId = (typeof input.session_id === 'string' && input.session_id.trim().length > 0)
788
- ? input.session_id.trim()
789
- : (typeof input.resume === 'string' && input.resume.length > 0 ? input.resume : undefined);
790
- const resumeRequested = input.resume === true || explicitResumeId !== undefined;
791
- const resumeTargetId = explicitResumeId ?? agent.sessionId;
792
- // An explicit-id resume must never silently degrade to a fresh session: that
793
- // is exactly the wrong-context dispatch this feature forbids. resume=true and
794
- // resume=false keep their pre-existing transparent recovery.
795
- const allowFreshSessionFallback = explicitResumeId === undefined;
796
- const resuming = !!(resumeRequested && resumeTargetId && provider.supportsResume());
797
- const isCallerMinted = provider.sessionIdStrategy().type === 'caller-minted';
798
- const mintedId = isCallerMinted
799
- ? (resuming ? resumeTargetId : uuid())
800
- : (resuming ? resumeTargetId : undefined);
801
- // Terminal session-not-found gate for explicit-id resumes (apra-fleet-eft.78.1).
802
- // Checked BEFORE any spawn (writePromptFile / the LLM invocation): if the
803
- // caller named a session the server has never issued for this member -- and it
804
- // is not the member's currently-stored session -- there is no context to
805
- // resume. Reject with a structured session_not_found and make NO LLM call,
806
- // so an orchestrator can rebuild a self-contained prompt and re-dispatch fresh
807
- // deliberately, rather than getting a silent blank-session response.
808
- if (explicitResumeId !== undefined) {
809
- const resumable = provider.supportsResume()
810
- && (isKnownSession(agent.id, explicitResumeId) || explicitResumeId === agent.sessionId);
811
- if (!resumable) {
812
- scope.abort(`explicit resume rejected -- session "${explicitResumeId}" is unknown/expired (no LLM call)`);
813
- inFlightAgents.delete(agent.id);
814
- stallDetector.remove(agent.id);
815
- writeStatusline(new Map([[agent.id, 'idle']]));
816
- return {
817
- text: `[FAIL] execute_prompt on "${agent.friendlyName}" rejected -- session "${explicitResumeId}" cannot be resumed (unknown or expired). No LLM call was made. Rebuild the context and re-dispatch with a full, self-contained prompt (resume=false), or resume=true for best-effort recovery.`,
818
- structuredContent: { isError: true, reason: 'session_not_found', sessionId: explicitResumeId },
819
- };
820
778
  }
821
- }
822
- const promptOpts = {
823
- folder: resolvedWorkFolder,
824
- promptFile: promptFileName,
825
- sessionId: mintedId,
826
- resuming,
827
- unattended: agent.unattended,
828
- model: resolvedModel,
829
- tier: resolvedTier,
830
- maxTurns: input.max_turns,
831
- inv: scope.getInv(),
832
- agentName: input.agent,
833
- };
834
- // apra-fleet issue #390: session log paths live on the MEMBER's machine, under
835
- // the MEMBER's home directory, joined with the MEMBER's OS convention. Before
836
- // this, every remote member got a HUB-home path (os.homedir()) joined with the
837
- // HUB's path convention -- a path that can never exist on the member, which
838
- // silently disabled stall detection for Claude and manufactured
839
- // false-positive stall kills for AGY/OpenCode.
840
- //
841
- // This resolution is deliberately SYNCHRONOUS (cached probe result, else the
842
- // member's known login username's default home): the dispatch path must not
843
- // add a remote round trip, and a wrong guess here can only cost detection
844
- // fidelity, never cause a kill. The kill-capable directory poll in
845
- // stall-poller.ts uses the probe-backed async resolver instead.
846
- const memberPathCtx = getCachedMemberPathContext(agent);
847
- const activePreSpawnSid = resuming ? resumeTargetId : (isCallerMinted ? mintedId : undefined);
848
- let resolvedLogPath = null;
849
- if (activePreSpawnSid) {
850
- try {
851
- resolvedLogPath = resolveSessionLogPath(agent.llmProvider ?? 'claude', activePreSpawnSid, resolvedWorkFolder, memberPathCtx.homeDir, memberPathCtx.targetOs);
779
+ await ensureAgentFilesProvisioned(agent);
780
+ const stallDetector = getStallDetector();
781
+ let clearedByStall = false;
782
+ // apra-fleet-3c9.1: a CONFIRMED stall must not only kill the remote pid but
783
+ // also cancel the in-flight strategy.execCommand() promise. Before this, the
784
+ // client kept waiting out its full deriveTimeoutMs deadline after the
785
+ // server-side work had already died (the 60.5-min hung dispatch in
786
+ // apra-fleet-3c9). onStall aborts this controller; its signal is merged into
787
+ // the signal handed to every execCommand below (see dispatchSignal), so a
788
+ // confirmed stall settles the pending dispatch immediately and surfaces a
789
+ // typed 'stalled' error instead of hanging.
790
+ const stallAbortController = new AbortController();
791
+ // apra-fleet-25yl.1.2: input.timeout_s is the real per-dispatch stall
792
+ // baseline now, not merely an inactivity kill on stdout/stderr -- see the
793
+ // schema description on `timeout_s` above. A caller that omits timeout_s
794
+ // gets a 300s baseline (the schema default), a deliberate change from the
795
+ // previously silent 150s DEFAULT_STALL_THRESHOLD_MS fallback.
796
+ const stallThresholdMs = (input.timeout_s ?? 300) * 1000;
797
+ stallDetector.add(agent.id, {
798
+ sessionId: null,
799
+ logFilePath: null,
800
+ lastActivityAt: Date.now(),
801
+ consecutiveIdleCycles: 0,
802
+ consecutiveReadFailures: 0,
803
+ memberId: agent.id,
804
+ memberName: agent.friendlyName,
805
+ provisional: true,
806
+ stallReported: false,
807
+ thresholdMs: stallThresholdMs,
808
+ onStall: () => {
809
+ // Stall detector already wrote 'unknown' to the statusline before calling here.
810
+ // Our job: clear in-process state so the member can accept new calls.
811
+ // clearedByStall prevents the eventually-resolving finally block from clobbering
812
+ // a new execute_prompt that may have already claimed the member.
813
+ inFlightAgents.delete(agent.id);
814
+ clearedByStall = true;
815
+ claimGuardActive = false;
816
+ // apra-fleet-6z8.2: a CONFIRMED stall means the remote turn made no
817
+ // progress of any kind for the whole threshold. Clearing bookkeeping
818
+ // alone left that wedged process running indefinitely on the member --
819
+ // burning its LLM session, holding its work folder, and colliding with
820
+ // whatever dispatch takes the member next. Kill the tracked pid too.
821
+ // Best-effort and never awaited: onStall is a fire-and-forget callback
822
+ // from the poll loop, and tryKillPid already swallows its own errors.
823
+ void tryKillPid(agent, strategy, cmds).catch(() => { });
824
+ // apra-fleet-3c9.1: killing the remote pid alone left the pending
825
+ // execCommand promise still awaiting its full deadline. Abort it now so
826
+ // the dispatch settles promptly and returns a typed 'stalled' error.
827
+ try {
828
+ stallAbortController.abort();
829
+ }
830
+ catch { /* best-effort */ }
831
+ },
832
+ });
833
+ const tmpDir = agent.agentType === 'local' ? os.tmpdir() : '/tmp';
834
+ const resolvedWorkFolder = agent.agentType === 'local' ? resolveTilde(agent.workFolder) : agent.workFolder;
835
+ const promptFilePath = agent.agentType === 'local'
836
+ ? path.join(resolvedWorkFolder, promptFileName)
837
+ : `${resolvedWorkFolder}/${promptFileName}`;
838
+ const strategy = getStrategy(agent);
839
+ const cmds = getOsCommands(getAgentOS(agent), getAgentShell(agent));
840
+ const provider = getProvider(agent.llmProvider);
841
+ const authPrefix = buildAuthEnvPrefix(agent, getAgentOS(agent));
842
+ const tiers = provider.modelTiers();
843
+ let resolvedModel = input.model || 'standard';
844
+ let resolvedTier;
845
+ if (resolvedModel === 'cheap') {
846
+ resolvedTier = 'cheap';
847
+ resolvedModel = agent.modelTiers
848
+ ? resolveModelForTier(agent, 'cheap', provider)
849
+ : agent.modelCheap || getModelOverride(provider.name, 'cheap') || tiers.cheap;
852
850
  }
853
- catch {
854
- resolvedLogPath = null;
851
+ else if (resolvedModel === 'standard') {
852
+ resolvedTier = 'standard';
853
+ resolvedModel = agent.modelTiers
854
+ ? resolveModelForTier(agent, 'standard', provider)
855
+ : agent.modelStandard || getModelOverride(provider.name, 'standard') || tiers.standard;
855
856
  }
856
- }
857
- stallDetector.update(agent.id, {
858
- sessionId: activePreSpawnSid,
859
- logFilePath: resolvedLogPath,
860
- provisional: !resolvedLogPath,
861
- });
862
- const claudeCmd = authPrefix + cmds.buildAgentPromptCommand(provider, promptOpts);
863
- // apra-fleet-6z8.1: the per-invocation durable stdout mirror the unix prompt
864
- // wrapper tees to (see durableOutputPath / buildAgentPromptCommand). A
865
- // PowerShell Windows member has no such companion tee, so recovery is
866
- // skipped for it.
867
- //
868
- // apra-fleet-7dir.5.4: a Windows member registered as gitbash runs
869
- // WindowsGitBashCommands, which INHERITS LinuxCommands.buildAgentPromptCommand
870
- // -- so it has already been teeing its stdout to /tmp/.fleet-out-<inv>.json
871
- // on every dispatch, with nothing reading it and (because extraPaths was
872
- // empty) nothing deleting it. Enabling the mirror for gitbash both turns
873
- // recovery on for that member class and makes the file get cleaned up.
874
- // This gate and the `unsupported` flag passed to recoverOrphanedDispatch
875
- // below are ONE decision: recoverOrphanedDispatch short-circuits on
876
- // `unsupported || !durablePath`, so they must always agree -- hence the
877
- // single shared predicate.
878
- //
879
- // LOCAL agents are deliberately excluded from the flip: orphan recovery
880
- // exists for a torn-down SSH channel, which a local spawn does not have, and
881
- // deletePromptFile's local branch would fs.unlinkSync('/tmp/...') through
882
- // Node on Windows (resolving to C:\tmp), not the MSYS /tmp bash teed to.
883
- // Non-Windows members (local or remote) keep today's behaviour verbatim.
884
- const durableMirrorSupported = getAgentOS(agent) !== 'windows'
885
- || (getAgentShell(agent) === 'gitbash' && agent.agentType !== 'local');
886
- const durablePath = durableMirrorSupported ? durableOutputPath(scope.getInv()) : undefined;
887
- const dispatchStartedAt = Date.now();
888
- const timeoutMs = (input.timeout_s ?? 300) * 1000;
889
- const maxTotalMs = input.max_total_s !== undefined ? input.max_total_s * 1000 : undefined;
890
- // apra-fleet-y8q.1: every retry below (dispatch-exception, stale-session,
891
- // server-overloaded) re-dispatches with a FRESH session but used to reuse the
892
- // SAME full timeoutMs/maxTotalMs as the original attempt -- so a single
893
- // dispatch could burn up to ~2x max_total_s server-side (original attempt +
894
- // one full-budget retry), well past what the client's deriveTimeoutMs()
895
- // (packages/apra-fleet-client/src/client/api.mjs) budgets for the whole
896
- // tools/call (max_total_s*1000 + a fixed grace margin). That let the
897
- // client's hard timeout fire before the server's own retry-and-report path
898
- // ever got a chance, hiding a clean typed server error behind a raw client
899
- // transport timeout. Share ONE deadline budget across the original attempt
900
- // and any single retry: cap a retry's maxTotalMs (and its inactivity
901
- // timeoutMs, so it can't independently outlast the shared ceiling) to
902
- // whatever remains of max_total_s since dispatchStartedAt, and skip the
903
- // retry entirely once that budget is exhausted -- so total wall-clock time
904
- // for this call never exceeds max_total_s, which is exactly what the client
905
- // is prepared to wait for. When max_total_s is absent there is no hard
906
- // ceiling to share, so retries keep their full timeout_s (unchanged,
907
- // pre-existing behavior).
908
- function retryBudget() {
909
- if (maxTotalMs === undefined)
910
- return { timeoutMs, maxTotalMs: undefined, exhausted: false };
911
- const remaining = Math.max(0, maxTotalMs - (Date.now() - dispatchStartedAt));
912
- return { timeoutMs: Math.min(timeoutMs, remaining), maxTotalMs: remaining, exhausted: remaining <= 0 };
913
- }
914
- // Agent file validation -- verify named agent exists before any CLI invocation
915
- if (input.agent) {
916
- const dirs = provider.agentDirectories(input.agent);
917
- let agentFound = false;
918
- if (agent.agentType === 'local') {
919
- const projPath = path.join(resolvedWorkFolder, dirs.project);
920
- const userPath = path.join(os.homedir(), dirs.home);
921
- agentFound = fs.existsSync(projPath) || fs.existsSync(userPath);
922
- if (!agentFound) {
857
+ else if (resolvedModel === 'premium') {
858
+ resolvedTier = 'premium';
859
+ resolvedModel = agent.modelTiers
860
+ ? resolveModelForTier(agent, 'premium', provider)
861
+ : agent.modelPremium || getModelOverride(provider.name, 'premium') || tiers.premium;
862
+ }
863
+ else {
864
+ resolvedModel = tiers[resolvedModel] ?? resolvedModel;
865
+ }
866
+ const scope = new LogScope('execute_prompt', `[${resolvedModel}] resume=${input.resume} fork=${input.fork} timeout=${input.timeout_s ?? 300}s ${truncateForLog(maskSecrets(input.prompt), getLogPreviewChars())}`, agent);
867
+ // Resume semantics (apra-fleet-eft.78.1). `resume` is boolean | string:
868
+ // - true -> best-effort resume of the member's stored last session; a
869
+ // stale/unknown stored session transparently retries fresh.
870
+ // - false -> always a fresh session.
871
+ // - string -> EXPLICIT session-id resume: resume exactly this id, preferring
872
+ // it over agent.sessionId. The caller asserts the prompt depends
873
+ // on that session's context, so an unknown/expired id is a
874
+ // TERMINAL session_not_found (handled just below) and NO
875
+ // fresh-session fallback is ever applied (see the retry paths).
876
+ const explicitResumeId = (typeof input.session_id === 'string' && input.session_id.trim().length > 0)
877
+ ? input.session_id.trim()
878
+ : (typeof input.resume === 'string' && input.resume.length > 0 ? input.resume : undefined);
879
+ // Fork semantics (apra-fleet-lmtg.5) mirror resume's SHAPE but BRANCH instead
880
+ // of continuing in place. `fork` is boolean | string:
881
+ // - true -> best-effort fork of the member's stored last session; a
882
+ // stale/unknown stored session logs a warning and falls back to a
883
+ // plain FRESH session (analogous to resume=true), never a hard error.
884
+ // - string -> EXPLICIT fork of exactly that source id -- an unknown/expired
885
+ // source is a TERMINAL session_not_found (like an explicit resume).
886
+ // The mutual-exclusivity guard above only lets fork through with resume at its
887
+ // DEFAULT (true), so fork must SUPERSEDE that default resume here: a fork
888
+ // request forces resuming off so the dispatch mints a fresh distinct output
889
+ // session id instead of continuing the stored one in place.
890
+ // apra-fleet-3swo.42: derived from the SAME normalised forkArg the top-of-
891
+ // function mutual-exclusivity guard computed forkRequested from (no second,
892
+ // independently-trimmed copy of the rule). Deliberately NOT gated on
893
+ // `.length > 0`: an explicit fork string that trims to '' (fork: '' or
894
+ // fork: ' ') must still be an EXPLICIT id -- just an invalid one -- so it
895
+ // takes the explicit-id branch below (terminal session_not_found, no LLM
896
+ // call) instead of being mistaken for the fork===true best-effort path.
897
+ const explicitForkId = typeof forkArg === 'string' ? forkArg : undefined;
898
+ // forkRequested is already computed at the top of executePrompt for the
899
+ // fork/resume mutual-exclusivity guard (apra-fleet-lmtg.4) -- reuse it here.
900
+ const resumeRequested = (input.resume === true || explicitResumeId !== undefined) && !forkRequested;
901
+ const resumeTargetId = explicitResumeId ?? agent.sessionId;
902
+ // An explicit-id resume OR an explicit-id fork must never silently degrade to a
903
+ // fresh session: that is exactly the wrong-context dispatch this feature
904
+ // forbids. resume=true/false and fork=true keep their transparent recovery.
905
+ const allowFreshSessionFallback = explicitResumeId === undefined && explicitForkId === undefined;
906
+ const resuming = !!(resumeRequested && resumeTargetId && provider.supportsResume());
907
+ const isCallerMinted = provider.sessionIdStrategy().type === 'caller-minted';
908
+ const mintedId = isCallerMinted
909
+ ? (resuming ? resumeTargetId : uuid())
910
+ : (resuming ? resumeTargetId : undefined);
911
+ // Terminal session-not-found gate for explicit-id resumes (apra-fleet-eft.78.1).
912
+ // Checked BEFORE any spawn (writePromptFile / the LLM invocation): if the
913
+ // caller named a session the server has never issued for this member -- and it
914
+ // is not the member's currently-stored session -- there is no context to
915
+ // resume. Reject with a structured session_not_found and make NO LLM call,
916
+ // so an orchestrator can rebuild a self-contained prompt and re-dispatch fresh
917
+ // deliberately, rather than getting a silent blank-session response.
918
+ if (explicitResumeId !== undefined) {
919
+ const resumable = provider.supportsResume()
920
+ && (isKnownSession(agent.id, explicitResumeId) || explicitResumeId === agent.sessionId);
921
+ if (!resumable) {
922
+ scope.abort(`explicit resume rejected -- session "${explicitResumeId}" is unknown/expired (no LLM call)`);
923
923
  inFlightAgents.delete(agent.id);
924
924
  stallDetector.remove(agent.id);
925
925
  writeStatusline(new Map([[agent.id, 'idle']]));
926
- return `execute_prompt: agent "${input.agent}" not found.\n\nExpected at:\n ${projPath.replace(/\\/g, '/')}\n ${userPath.replace(/\\/g, '/')}`;
926
+ return {
927
+ text: `[FAIL] execute_prompt on "${agent.friendlyName}" rejected -- session "${explicitResumeId}" cannot be resumed (unknown or expired). No LLM call was made. Rebuild the context and re-dispatch with a full, self-contained prompt (resume=false), or resume=true for best-effort recovery.`,
928
+ structuredContent: { isError: true, reason: 'session_not_found', sessionId: explicitResumeId },
929
+ };
927
930
  }
928
931
  }
929
- else {
930
- // Canonical PM role agents (planner/doer/reviewer/plan-reviewer/...) are
931
- // already guaranteed present in ~/dirs.home by
932
- // ensureAgentFilesProvisioned() above (ln ~576, which ran provisionAgents()
933
- // for this exact member earlier in this same call) -- trust that instead
934
- // of re-probing the remote here. This also SIDESTEPS the bug this check
935
- // used to have: the old code hand-rolled a POSIX-only
936
- // `test -f ... || test -f ...` command run via strategy.execCommand(),
937
- // which throws a PowerShell parser error (not a POSIX shell) on every
938
- // Windows remote -- a nonzero exit that this check misread as "agent not
939
- // found" even when the file genuinely existed (apra-fleet P0 bug, fleet-
940
- // sprint via a Windows remote member always failed plan-review with
941
- // "agent 'plan-reviewer' not found").
942
- const canonicalRelPath = `${input.agent}.md`;
943
- agentFound = remoteAgentsDir(provider.name) !== null
944
- && loadCanonicalAgentSet(provider.name).some((f) => f.relPath === canonicalRelPath);
945
- if (!agentFound) {
946
- // Not part of the canonical PM set (e.g. a project-local custom
947
- // agent) -- fall back to a REAL existence probe, built platform-aware
948
- // via this repo's own getOsCommands() abstraction (src/os/*.ts,
949
- // already used the same way by list-members.ts/member-detail.ts for
950
- // credential-file checks) instead of a single hardcoded shell dialect.
951
- const cmds = getOsCommands(getAgentOS(agent), getAgentShell(agent));
952
- const projPath = `${resolvedWorkFolder}/${dirs.project}`;
953
- const userPath = `~/${dirs.home}`;
954
- const [projResult, userResult] = await Promise.all([
955
- strategy.execCommand(cmds.credentialFileCheck(projPath), 10000),
956
- strategy.execCommand(cmds.credentialFileCheck(userPath), 10000),
957
- ]);
958
- agentFound = projResult.stdout.includes('found') || userResult.stdout.includes('found');
959
- }
960
- if (!agentFound) {
932
+ // Fork mode resolution (apra-fleet-lmtg.5). Resolved AFTER the resume gate
933
+ // (the two are mutually exclusive, guarded before member resolution). Fork
934
+ // requires a fork-capable provider; when active it produces a ForkDescriptor
935
+ // that buildAgentPromptCommand (fork-prov lane, apra-fleet-lmtg.2) turns into
936
+ // the provider's own source-seeded, new-session-id fork invocation.
937
+ let forkActive = false;
938
+ let forkSourceId;
939
+ if (forkRequested) {
940
+ // fork requires provider.supportsFork(): a fork request against a provider
941
+ // whose CLI cannot fork is surfaced as a clear TERMINAL error, never
942
+ // silently downgraded to a plain fresh/resume dispatch (the wrong behavior).
943
+ if (!provider.supportsFork?.()) {
944
+ scope.abort(`fork rejected -- provider "${provider.name}" does not support fork-mode dispatch (no LLM call)`);
961
945
  inFlightAgents.delete(agent.id);
962
946
  stallDetector.remove(agent.id);
963
947
  writeStatusline(new Map([[agent.id, 'idle']]));
964
- return `execute_prompt: agent "${input.agent}" not found on "${agent.friendlyName}".\n\nExpected at:\n ${resolvedWorkFolder}/${dirs.project}\n ~/${dirs.home}`;
948
+ return {
949
+ text: `[FAIL] execute_prompt on "${agent.friendlyName}" rejected -- provider "${provider.name}" does not support fork-mode dispatch. No LLM call was made. Re-dispatch without fork (resume=true/false), or use a fork-capable provider.`,
950
+ structuredContent: { isError: true, reason: 'fork_unsupported' },
951
+ };
952
+ }
953
+ if (explicitForkId !== undefined) {
954
+ // Terminal source-not-found gate, mirroring the explicit-resume gate: an
955
+ // unknown/expired source id has no context to branch from, so reject
956
+ // BEFORE any spawn with a structured session_not_found and NO LLM call.
957
+ const forkable = isKnownSession(agent.id, explicitForkId) || explicitForkId === agent.sessionId;
958
+ if (!forkable) {
959
+ scope.abort(`explicit fork rejected -- source session "${explicitForkId}" is unknown/expired (no LLM call)`);
960
+ inFlightAgents.delete(agent.id);
961
+ stallDetector.remove(agent.id);
962
+ writeStatusline(new Map([[agent.id, 'idle']]));
963
+ return {
964
+ text: `[FAIL] execute_prompt on "${agent.friendlyName}" rejected -- source session "${explicitForkId}" cannot be forked (unknown or expired). No LLM call was made. Rebuild the context and re-dispatch with a full, self-contained prompt (fork=false/resume=false), or fork=true for best-effort branching from the member's stored session.`,
965
+ structuredContent: { isError: true, reason: 'session_not_found', sessionId: explicitForkId },
966
+ };
967
+ }
968
+ forkSourceId = explicitForkId;
969
+ forkActive = true;
970
+ }
971
+ else {
972
+ // fork === true: best-effort branch from the member's stored last session.
973
+ // A stale/unknown stored session (or none at all) is NOT a hard error --
974
+ // log a warning and fall through to a plain FRESH session, mirroring
975
+ // resume=true's transparent recovery.
976
+ const stored = agent.sessionId;
977
+ if (stored && isKnownSession(agent.id, stored)) {
978
+ forkSourceId = stored;
979
+ forkActive = true;
980
+ }
981
+ else {
982
+ scope.info(`fork=true: stored session ${stored ? `"${stored}" is stale/unknown` : 'is absent'} -- falling back to a fresh session`);
983
+ forkActive = false;
984
+ }
965
985
  }
966
986
  }
967
- }
968
- // Context-headroom admission control (apra-fleet-eft.81.1): a declared
969
- // demand is checked against this session's remaining headroom BEFORE any
970
- // CLI is spawned. No declared demand (both fields omitted) means this
971
- // block is skipped entirely -- pre-existing behavior is unchanged.
972
- const contextDemand = resolveExpectedDemand(input.expected_context_tokens, input.context_size);
973
- let contextWarning;
974
- if (contextDemand !== undefined) {
975
- const admission = checkContextAdmission({
976
- provider: provider.name,
977
- resolvedModel,
987
+ // Mint a fresh distinct output session id for the forked conversation (never
988
+ // reuse the source id): mintedId is already a freshly minted uuid here because
989
+ // fork forced resuming off above (see resumeRequested). It IS emitted as an
990
+ // explicit `--session-id` CLI flag in fork mode (see ForkDescriptor.newSessionId
991
+ // and provider.forkFlag) -- the CLI honors a caller-supplied session id even
992
+ // when forking, so we pre-mint and pass it rather than scraping the CLI's own
993
+ // minted id back out of the response afterward. The same id also drives
994
+ // recordKnownSession, the stall-detector log path, and the post-dispatch
995
+ // session bookkeeping below.
996
+ const forkDescriptor = forkActive && forkSourceId
997
+ ? { sourceSessionId: forkSourceId, newSessionId: mintedId ?? uuid() }
998
+ : undefined;
999
+ const promptOpts = {
1000
+ folder: resolvedWorkFolder,
1001
+ promptFile: promptFileName,
978
1002
  sessionId: mintedId,
979
- demand: contextDemand,
980
- });
981
- if (!admission.allowed) {
982
- scope.abort(`insufficient context headroom: demand=${admission.detail.demand} headroom=${admission.detail.headroom} window=${admission.detail.window}`);
983
- inFlightAgents.delete(agent.id);
984
- stallDetector.remove(agent.id);
985
- writeStatusline(new Map([[agent.id, 'idle']]));
986
- return {
987
- text: `[FAIL] execute_prompt on "${agent.friendlyName}" rejected -- insufficient context headroom (demand=${admission.detail.demand}, headroom=${admission.detail.headroom}, window=${admission.detail.window}). Start a fresh session, shrink the task, or split it.`,
988
- structuredContent: { isError: true, reason: 'insufficient_context_headroom', detail: admission.detail },
989
- };
990
- }
991
- if (admission.warning) {
992
- contextWarning = { message: admission.warning, detail: admission.detail };
993
- }
994
- }
995
- // Usage/budget admission (apra-fleet-eft.80.2): when a budget is configured
996
- // for this member (or its workspace), a hard-threshold crossing rejects the
997
- // NEW dispatch BEFORE any LLM is spawned -- never killing an in-flight call.
998
- // No configured budget (the common case) means resolveBudgetScope returns
999
- // undefined and this block is skipped entirely -- behavior is unchanged.
1000
- const budgetScope = resolveBudgetScope([agent.id, workspaceId]);
1001
- if (budgetScope) {
1002
- const preBudget = await evaluateBudget({ scope: budgetScope, agent, provider });
1003
- if (preBudget?.exhausted) {
1004
- scope.abort(`budget exhausted: scope=${budgetScope} spent=${preBudget.block.spent} budget=${preBudget.block.budget} source=${preBudget.block.source}`);
1005
- inFlightAgents.delete(agent.id);
1006
- stallDetector.remove(agent.id);
1007
- writeStatusline(new Map([[agent.id, 'idle']]));
1008
- return {
1009
- text: `[FAIL] execute_prompt on "${agent.friendlyName}" rejected -- budget exhausted (scope=${budgetScope}, spent=${preBudget.block.spent}, budget=${preBudget.block.budget} ${preBudget.block.unit}, source=${preBudget.block.source}). No LLM call was made; raise or reset the budget to resume.`,
1010
- structuredContent: { isError: true, reason: 'budget_exhausted', budgetUsage: preBudget.block },
1011
- };
1012
- }
1013
- }
1014
- // Kill any leftover session from a previous (possibly zombie) execute_prompt call
1015
- await tryKillPid(agent, strategy, cmds);
1016
- // Write the rendered prompt (with substitutions applied) to the prompt file before execution
1017
- await writePromptFile(agent, strategy, promptFilePath, renderedPrompt);
1018
- // apra-fleet-6z8.1: remembered for the lease-of-life gate below -- the pid
1019
- // outlives the SSH channel that reported it, and is the only way to tell a
1020
- // fabricated "exit 0 / empty" close apart from a real one.
1021
- let capturedPid;
1022
- const onPidCaptured = (pid) => {
1023
- capturedPid = pid;
1024
- scope.info(`pid=${pid}`);
1025
- if (mintedId) {
1026
- let logPath = null;
1003
+ resuming,
1004
+ unattended: agent.unattended,
1005
+ model: resolvedModel,
1006
+ tier: resolvedTier,
1007
+ maxTurns: input.max_turns,
1008
+ inv: scope.getInv(),
1009
+ agentName: input.agent,
1010
+ fork: forkDescriptor,
1011
+ projectId: agyProjectId,
1012
+ };
1013
+ // apra-fleet issue #390: session log paths live on the MEMBER's machine, under
1014
+ // the MEMBER's home directory, joined with the MEMBER's OS convention. Before
1015
+ // this, every remote member got a HUB-home path (os.homedir()) joined with the
1016
+ // HUB's path convention -- a path that can never exist on the member, which
1017
+ // silently disabled stall detection for Claude and manufactured
1018
+ // false-positive stall kills for AGY/OpenCode.
1019
+ //
1020
+ // This resolution is deliberately SYNCHRONOUS (cached probe result, else the
1021
+ // member's known login username's default home): the dispatch path must not
1022
+ // add a remote round trip, and a wrong guess here can only cost detection
1023
+ // fidelity, never cause a kill. The kill-capable directory poll in
1024
+ // stall-poller.ts uses the probe-backed async resolver instead.
1025
+ const memberPathCtx = getCachedMemberPathContext(agent);
1026
+ const activePreSpawnSid = resuming ? resumeTargetId : (isCallerMinted ? mintedId : undefined);
1027
+ let resolvedLogPath = null;
1028
+ if (activePreSpawnSid) {
1027
1029
  try {
1028
- logPath = resolveSessionLogPath(agent.llmProvider ?? 'claude', mintedId, resolvedWorkFolder, memberPathCtx.homeDir, memberPathCtx.targetOs);
1030
+ resolvedLogPath = resolveSessionLogPath(agent.llmProvider ?? 'claude', activePreSpawnSid, resolvedWorkFolder, memberPathCtx.homeDir, memberPathCtx.targetOs);
1029
1031
  }
1030
1032
  catch {
1031
- logPath = null;
1033
+ resolvedLogPath = null;
1032
1034
  }
1033
- stallDetector.update(agent.id, {
1034
- sessionId: mintedId,
1035
- logFilePath: logPath,
1036
- provisional: !logPath,
1037
- });
1038
- }
1039
- };
1040
- const abortHandler = () => {
1041
- scope.abort('cancelled by MCP client');
1042
- tryKillPid(agent, strategy, cmds).catch(() => { });
1043
- };
1044
- extra?.signal?.addEventListener('abort', abortHandler);
1045
- // apra-fleet-3c9.1: the signal handed to execCommand fires on EITHER the MCP
1046
- // client's cancellation OR a confirmed stall (stallAbortController). Merging
1047
- // them means a stall aborts the pending dispatch exactly as a client cancel
1048
- // would, while a live (non-stalled) dispatch -- whose controller is never
1049
- // aborted -- is left completely untouched.
1050
- const dispatchSignal = extra?.signal
1051
- ? AbortSignal.any([extra.signal, stallAbortController.signal])
1052
- : stallAbortController.signal;
1053
- // Mark agent as busy in statusline
1054
- writeStatusline(new Map([[agent.id, 'busy']]));
1055
- let _epExitCode = 'error';
1056
- let _epError;
1057
- let _epUsage;
1058
- let _epOffline = false;
1059
- // apra-fleet-6a7.1: gates the exit-0/empty-stdout workspace_not_trusted
1060
- // self-heal-and-retry below to exactly one attempt per call, mirroring
1061
- // runGitStep's authHealAttempted shape (fleet-sprint/runner.js:616) -- a
1062
- // repeat trust failure after the heal is terminal, never looped.
1063
- let trustHealAttempted = false;
1064
- try {
1065
- let result;
1066
- try {
1067
- result = await strategy.execCommand(claudeCmd, timeoutMs, maxTotalMs, onPidCaptured, dispatchSignal);
1068
1035
  }
1069
- catch (dispatchErr) {
1070
- // apra-fleet-02s.1: a genuine command-execution exception (e.g. an
1071
- // inactivity timeout, or any other error strategy.execCommand throws)
1072
- // used to be unconditionally unretried here -- it bypasses both retry
1073
- // mechanisms below, since those only fire on a non-throwing nonzero
1074
- // exit, never on a thrown exception. Retry once with a fresh session
1075
- // before giving up, mirroring the stale-session/server-overloaded
1076
- // retries' bounded, single-attempt shape. Skip the retry if the client
1077
- // itself cancelled the request -- there is nothing to recover from a
1078
- // deliberate cancellation.
1079
- if (extra?.signal?.aborted)
1080
- throw dispatchErr;
1081
- // apra-fleet-3c9.1: a stall-triggered abort is terminal. onStall already
1082
- // killed the remote process and its session is gone, so retrying in a
1083
- // fresh session would just re-dispatch onto a member we just tore down.
1084
- // Let the exception surface to the outer catch, which classifies it as a
1085
- // typed 'stalled' error instead of retrying or hanging.
1086
- if (stallAbortController.signal.aborted)
1087
- throw dispatchErr;
1088
- // apra-fleet-eft.78.1: an explicit-id resume must NOT retry in a fresh
1089
- // session (that would run a context-dependent delta prompt with no
1090
- // context). Let the exception surface as dispatch_failed instead.
1091
- if (!allowFreshSessionFallback)
1092
- throw dispatchErr;
1093
- // apra-fleet-y8q.1: no budget left to share with a retry (the original
1094
- // attempt already consumed the whole max_total_s) -- retrying here would
1095
- // just re-burn a fresh full budget past what the client is waiting for.
1096
- // Let the original exception surface instead of retrying blind.
1097
- const budget = retryBudget();
1098
- if (budget.exhausted)
1099
- throw dispatchErr;
1100
- scope.info(`[${resolvedModel}] retrying -- dispatch exception: ${dispatchErr.message}`);
1101
- await tryKillPid(agent, strategy, cmds);
1102
- const freshOpts = { ...promptOpts, sessionId: isCallerMinted ? uuid() : undefined, resuming: false };
1103
- const retryCmd = authPrefix + cmds.buildAgentPromptCommand(provider, freshOpts);
1104
- result = await strategy.execCommand(retryCmd, budget.timeoutMs, budget.maxTotalMs, onPidCaptured, dispatchSignal);
1036
+ stallDetector.update(agent.id, {
1037
+ sessionId: activePreSpawnSid,
1038
+ logFilePath: resolvedLogPath,
1039
+ provisional: !resolvedLogPath,
1040
+ thresholdMs: stallThresholdMs,
1041
+ });
1042
+ const claudeCmd = authPrefix + cmds.buildAgentPromptCommand(provider, promptOpts);
1043
+ // apra-fleet-6z8.1: the per-invocation durable stdout mirror the unix prompt
1044
+ // wrapper tees to (see durableOutputPath / buildAgentPromptCommand). A
1045
+ // PowerShell Windows member has no such companion tee, so recovery is
1046
+ // skipped for it.
1047
+ //
1048
+ // apra-fleet-7dir.5.4: a Windows member registered as gitbash runs
1049
+ // WindowsGitBashCommands, which INHERITS LinuxCommands.buildAgentPromptCommand
1050
+ // -- so it has already been teeing its stdout to /tmp/.fleet-out-<inv>.json
1051
+ // on every dispatch, with nothing reading it and (because extraPaths was
1052
+ // empty) nothing deleting it. Enabling the mirror for gitbash both turns
1053
+ // recovery on for that member class and makes the file get cleaned up.
1054
+ // This gate and the `unsupported` flag passed to recoverOrphanedDispatch
1055
+ // below are ONE decision: recoverOrphanedDispatch short-circuits on
1056
+ // `unsupported || !durablePath`, so they must always agree -- hence the
1057
+ // single shared predicate.
1058
+ //
1059
+ // LOCAL agents are deliberately excluded from the flip: orphan recovery
1060
+ // exists for a torn-down SSH channel, which a local spawn does not have, and
1061
+ // deletePromptFile's local branch would fs.unlinkSync('/tmp/...') through
1062
+ // Node on Windows (resolving to C:\tmp), not the MSYS /tmp bash teed to.
1063
+ // Non-Windows members (local or remote) keep today's behaviour verbatim.
1064
+ const durableMirrorSupported = getAgentOS(agent) !== 'windows'
1065
+ || (getAgentShell(agent) === 'gitbash' && agent.agentType !== 'local');
1066
+ const durablePath = durableMirrorSupported ? durableOutputPath(scope.getInv()) : undefined;
1067
+ const dispatchStartedAt = Date.now();
1068
+ const maxTotalMs = input.max_total_s !== undefined ? input.max_total_s * 1000 : undefined;
1069
+ // apra-fleet-25yl.2.1: the exec-level ROLLING (inactivity) deadline handed to
1070
+ // strategy.execCommand() is no longer provider-blind. It used to be
1071
+ // `timeout_s` for everyone, which is a false kill for the batch-only
1072
+ // providers (they emit nothing on this channel until the turn ends) and the
1073
+ // only working stall signal for the providers with no pollable transcript.
1074
+ // The per-provider answer lives in ONE named place -- ProviderAdapter
1075
+ // .execTimeoutSource() -- so a newly added provider must state its own
1076
+ // (a compile error if it does not) instead of inheriting a default branch.
1077
+ //
1078
+ // NOTE: input.timeout_s still reaches the StallDetector as thresholdMs for
1079
+ // EVERY provider (see stallThresholdMs above). This decision governs the
1080
+ // exec-channel timer only; it must not be used to skip that threading.
1081
+ const execTimeoutSource = provider.execTimeoutSource();
1082
+ const timeoutMs = execTimeoutSource === 'inactivity_timeout'
1083
+ ? (input.timeout_s ?? 300) * 1000
1084
+ // 'total_ceiling': mirror max_total_s, which can never bind before the
1085
+ // caller's own hard ceiling does (a rolling inactivity window of
1086
+ // max_total_s starts at dispatch start and only ever resets later).
1087
+ // max_total_s ABSENT: there is no ceiling to mirror, so use a documented
1088
+ // never-binds-first constant rather than silently falling back to
1089
+ // timeout_s (which is exactly the coupling this change removes). It is
1090
+ // deliberately a finite value well inside the int32 range setTimeout
1091
+ // accepts -- Infinity or Number.MAX_SAFE_INTEGER would overflow and fire
1092
+ // on the next tick, inverting this fix into an instant kill.
1093
+ : (maxTotalMs ?? EXEC_TIMER_NEVER_BINDS_MS);
1094
+ // apra-fleet-y8q.1: every retry below (dispatch-exception, stale-session,
1095
+ // server-overloaded) re-dispatches with a FRESH session but used to reuse the
1096
+ // SAME full timeoutMs/maxTotalMs as the original attempt -- so a single
1097
+ // dispatch could burn up to ~2x max_total_s server-side (original attempt +
1098
+ // one full-budget retry), well past what the client's deriveTimeoutMs()
1099
+ // (packages/apra-fleet-client/src/client/api.mjs) budgets for the whole
1100
+ // tools/call (max_total_s*1000 + a fixed grace margin). That let the
1101
+ // client's hard timeout fire before the server's own retry-and-report path
1102
+ // ever got a chance, hiding a clean typed server error behind a raw client
1103
+ // transport timeout. Share ONE deadline budget across the original attempt
1104
+ // and any single retry: cap a retry's maxTotalMs (and its inactivity
1105
+ // timeoutMs, so it can't independently outlast the shared ceiling) to
1106
+ // whatever remains of max_total_s since dispatchStartedAt, and skip the
1107
+ // retry entirely once that budget is exhausted -- so total wall-clock time
1108
+ // for this call never exceeds max_total_s, which is exactly what the client
1109
+ // is prepared to wait for. When max_total_s is absent there is no hard
1110
+ // ceiling to share, so retries keep their full timeout_s (unchanged,
1111
+ // pre-existing behavior).
1112
+ function retryBudget() {
1113
+ if (maxTotalMs === undefined)
1114
+ return { timeoutMs, maxTotalMs: undefined, exhausted: false };
1115
+ const remaining = Math.max(0, maxTotalMs - (Date.now() - dispatchStartedAt));
1116
+ return { timeoutMs: Math.min(timeoutMs, remaining), maxTotalMs: remaining, exhausted: remaining <= 0 };
1105
1117
  }
1106
- let parsed = provider.parseResponse(result);
1107
- if (parsed.usage)
1108
- _epUsage = parsed.usage;
1109
- // apra-fleet-eft.40.3: workspace-not-trusted degrades composed permissions
1110
- // (project-scoped allow entries silently dropped) without killing the CLI process --
1111
- // from there, unattended -p cannot auto-approve or prompt, so tools get denied and
1112
- // the turn eventually fails. Blindly falling through to the stale-session /
1113
- // server-overloaded retries below just repeats the same degraded dispatch against a
1114
- // workspace that is still untrusted, and can walk into eft.28's dead-session hang.
1115
- // Classify and fail fast here, before either retry path, naming
1116
- // ensureWorkspaceTrusted (apra-fleet-eft.40.1/40.2) as the remediation.
1117
- if (result.code !== 0 && provider.classifyError(result.stderr || result.stdout) === 'workspace_not_trusted') {
1118
- return {
1119
- text: `[FAIL] ${workspaceNotTrustedAdvice(agent.friendlyName)}\n${result.stderr || result.stdout}`,
1120
- structuredContent: { isError: true, reason: 'workspace_not_trusted' },
1121
- };
1118
+ // Agent file validation -- verify named agent exists before any CLI invocation
1119
+ if (input.agent) {
1120
+ const dirs = provider.agentDirectories(input.agent);
1121
+ let agentFound = false;
1122
+ if (agent.agentType === 'local') {
1123
+ const projPath = path.join(resolvedWorkFolder, dirs.project);
1124
+ const userPath = path.join(os.homedir(), dirs.home);
1125
+ agentFound = fs.existsSync(projPath) || fs.existsSync(userPath);
1126
+ if (!agentFound) {
1127
+ inFlightAgents.delete(agent.id);
1128
+ stallDetector.remove(agent.id);
1129
+ writeStatusline(new Map([[agent.id, 'idle']]));
1130
+ return `execute_prompt: agent "${input.agent}" not found.\n\nExpected at:\n ${projPath.replace(/\\/g, '/')}\n ${userPath.replace(/\\/g, '/')}`;
1131
+ }
1132
+ }
1133
+ else {
1134
+ // Canonical PM role agents (planner/doer/reviewer/plan-reviewer/...) are
1135
+ // already guaranteed present in ~/dirs.home by
1136
+ // ensureAgentFilesProvisioned() above (ln ~576, which ran provisionAgents()
1137
+ // for this exact member earlier in this same call) -- trust that instead
1138
+ // of re-probing the remote here. This also SIDESTEPS the bug this check
1139
+ // used to have: the old code hand-rolled a POSIX-only
1140
+ // `test -f ... || test -f ...` command run via strategy.execCommand(),
1141
+ // which throws a PowerShell parser error (not a POSIX shell) on every
1142
+ // Windows remote -- a nonzero exit that this check misread as "agent not
1143
+ // found" even when the file genuinely existed (apra-fleet P0 bug, fleet-
1144
+ // sprint via a Windows remote member always failed plan-review with
1145
+ // "agent 'plan-reviewer' not found").
1146
+ const canonicalRelPath = `${input.agent}.md`;
1147
+ agentFound = remoteAgentsDir(provider.name) !== null
1148
+ && loadCanonicalAgentSet(provider.name).some((f) => f.relPath === canonicalRelPath);
1149
+ if (!agentFound) {
1150
+ // Not part of the canonical PM set (e.g. a project-local custom
1151
+ // agent) -- fall back to a REAL existence probe, built platform-aware
1152
+ // via this repo's own getOsCommands() abstraction (src/os/*.ts,
1153
+ // already used the same way by list-members.ts/member-detail.ts for
1154
+ // credential-file checks) instead of a single hardcoded shell dialect.
1155
+ const cmds = getOsCommands(getAgentOS(agent), getAgentShell(agent));
1156
+ const projPath = `${resolvedWorkFolder}/${dirs.project}`;
1157
+ const userPath = `~/${dirs.home}`;
1158
+ const [projResult, userResult] = await Promise.all([
1159
+ strategy.execCommand(cmds.credentialFileCheck(projPath), 10000),
1160
+ strategy.execCommand(cmds.credentialFileCheck(userPath), 10000),
1161
+ ]);
1162
+ agentFound = projResult.stdout.includes('found') || userResult.stdout.includes('found');
1163
+ }
1164
+ if (!agentFound) {
1165
+ inFlightAgents.delete(agent.id);
1166
+ stallDetector.remove(agent.id);
1167
+ writeStatusline(new Map([[agent.id, 'idle']]));
1168
+ return `execute_prompt: agent "${input.agent}" not found on "${agent.friendlyName}".\n\nExpected at:\n ${resolvedWorkFolder}/${dirs.project}\n ~/${dirs.home}`;
1169
+ }
1170
+ }
1122
1171
  }
1123
- // Stale session retry -- fresh session ID, no resume. apra-fleet-eft.78.1:
1124
- // ONLY the best-effort resume=true mode gets this transparent retry-fresh
1125
- // recovery. An explicit session-id resume (string) deliberately does NOT --
1126
- // its caller asserted context dependence, so a not-found id is terminal.
1127
- if (result.code !== 0 && input.resume === true && agent.sessionId) {
1128
- // apra-fleet-y8q.1: share the remaining max_total_s budget with this
1129
- // retry too -- skip it outright once exhausted (see retryBudget above).
1130
- const staleBudget = retryBudget();
1131
- if (!staleBudget.exhausted) {
1132
- scope.info(`[${resolvedModel}] retrying -- stale session`);
1133
- await tryKillPid(agent, strategy, cmds);
1134
- const freshOpts = { ...promptOpts, sessionId: isCallerMinted ? uuid() : undefined, resuming: false };
1135
- const retryCmd = authPrefix + cmds.buildAgentPromptCommand(provider, freshOpts);
1136
- result = await strategy.execCommand(retryCmd, staleBudget.timeoutMs, staleBudget.maxTotalMs, onPidCaptured, dispatchSignal);
1137
- parsed = provider.parseResponse(result);
1138
- if (parsed.usage)
1139
- _epUsage = parsed.usage;
1172
+ // Context-headroom admission control (apra-fleet-eft.81.1): a declared
1173
+ // demand is checked against this session's remaining headroom BEFORE any
1174
+ // CLI is spawned. No declared demand (both fields omitted) means this
1175
+ // block is skipped entirely -- pre-existing behavior is unchanged.
1176
+ const contextDemand = resolveExpectedDemand(input.expected_context_tokens, input.context_size);
1177
+ let contextWarning;
1178
+ if (contextDemand !== undefined) {
1179
+ const admission = checkContextAdmission({
1180
+ provider: provider.name,
1181
+ resolvedModel,
1182
+ sessionId: mintedId,
1183
+ demand: contextDemand,
1184
+ });
1185
+ if (!admission.allowed) {
1186
+ scope.abort(`insufficient context headroom: demand=${admission.detail.demand} headroom=${admission.detail.headroom} window=${admission.detail.window}`);
1187
+ inFlightAgents.delete(agent.id);
1188
+ stallDetector.remove(agent.id);
1189
+ writeStatusline(new Map([[agent.id, 'idle']]));
1190
+ return {
1191
+ text: `[FAIL] execute_prompt on "${agent.friendlyName}" rejected -- insufficient context headroom (demand=${admission.detail.demand}, headroom=${admission.detail.headroom}, window=${admission.detail.window}). Start a fresh session, shrink the task, or split it.`,
1192
+ structuredContent: { isError: true, reason: 'insufficient_context_headroom', detail: admission.detail },
1193
+ };
1194
+ }
1195
+ if (admission.warning) {
1196
+ contextWarning = { message: admission.warning, detail: admission.detail };
1140
1197
  }
1141
1198
  }
1142
- // Server/overloaded error retry -- single attempt after delay. Skipped for
1143
- // an explicit-id resume (apra-fleet-eft.78.1): the retry starts a fresh
1144
- // session, which would discard the exact context the caller asked to resume.
1145
- if (result.code !== 0 && allowFreshSessionFallback && isRetryable(provider.classifyError(result.stderr || result.stdout))) {
1146
- // apra-fleet-y8q.1: share the remaining max_total_s budget with this
1147
- // retry too -- skip it outright once exhausted (see retryBudget above).
1148
- const overloadBudget = retryBudget();
1149
- if (!overloadBudget.exhausted) {
1150
- scope.info(`[${resolvedModel}] retrying -- server overloaded`);
1151
- await tryKillPid(agent, strategy, cmds);
1152
- await new Promise(r => setTimeout(r, SERVER_RETRY_DELAY_MS));
1153
- const freshOpts = { ...promptOpts, sessionId: isCallerMinted ? uuid() : undefined, resuming: false };
1154
- const retryCmd = authPrefix + cmds.buildAgentPromptCommand(provider, freshOpts);
1155
- result = await strategy.execCommand(retryCmd, overloadBudget.timeoutMs, overloadBudget.maxTotalMs, onPidCaptured, dispatchSignal);
1156
- parsed = provider.parseResponse(result);
1157
- if (parsed.usage)
1158
- _epUsage = parsed.usage;
1199
+ // Usage/budget admission (apra-fleet-eft.80.2): when a budget is configured
1200
+ // for this member (or its workspace), a hard-threshold crossing rejects the
1201
+ // NEW dispatch BEFORE any LLM is spawned -- never killing an in-flight call.
1202
+ // No configured budget (the common case) means resolveBudgetScope returns
1203
+ // undefined and this block is skipped entirely -- behavior is unchanged.
1204
+ const budgetScope = resolveBudgetScope([agent.id, workspaceId]);
1205
+ if (budgetScope) {
1206
+ const preBudget = await evaluateBudget({ scope: budgetScope, agent, provider });
1207
+ if (preBudget?.exhausted) {
1208
+ scope.abort(`budget exhausted: scope=${budgetScope} spent=${preBudget.block.spent} budget=${preBudget.block.budget} source=${preBudget.block.source}`);
1209
+ inFlightAgents.delete(agent.id);
1210
+ stallDetector.remove(agent.id);
1211
+ writeStatusline(new Map([[agent.id, 'idle']]));
1212
+ return {
1213
+ text: `[FAIL] execute_prompt on "${agent.friendlyName}" rejected -- budget exhausted (scope=${budgetScope}, spent=${preBudget.block.spent}, budget=${preBudget.block.budget} ${preBudget.block.unit}, source=${preBudget.block.source}). No LLM call was made; raise or reset the budget to resume.`,
1214
+ structuredContent: { isError: true, reason: 'budget_exhausted', budgetUsage: preBudget.block },
1215
+ };
1159
1216
  }
1160
1217
  }
1161
- _epExitCode = result.code;
1162
- if (result.code !== 0) {
1163
- // apra-fleet-391: surface an auth failure as a STRUCTURED reason (not
1164
- // just prose in `text`) so callers -- notably fleet-sprint's
1165
- // isAuthDispatchError -- can key off it directly instead of regexing
1166
- // the message string. Overloading 'nonzero_exit' for this case was
1167
- // what made auth self-heal impossible to wire reliably upstream.
1168
- const failureCategory = isMaxTurnsResponse(parsed)
1169
- ? 'max_turns'
1170
- : provider.classifyError(result.stderr || result.stdout);
1218
+ // Kill any leftover session from a previous (possibly zombie) execute_prompt call
1219
+ await tryKillPid(agent, strategy, cmds);
1220
+ // Write the rendered prompt (with substitutions applied) to the prompt file before execution
1221
+ await writePromptFile(agent, strategy, promptFilePath, renderedPrompt);
1222
+ // apra-fleet-6z8.1: remembered for the lease-of-life gate below -- the pid
1223
+ // outlives the SSH channel that reported it, and is the only way to tell a
1224
+ // fabricated "exit 0 / empty" close apart from a real one.
1225
+ let capturedPid;
1226
+ const onPidCaptured = (pid) => {
1227
+ capturedPid = pid;
1228
+ scope.info(`pid=${pid}`);
1229
+ if (mintedId) {
1230
+ let logPath = null;
1231
+ try {
1232
+ logPath = resolveSessionLogPath(agent.llmProvider ?? 'claude', mintedId, resolvedWorkFolder, memberPathCtx.homeDir, memberPathCtx.targetOs);
1233
+ }
1234
+ catch {
1235
+ logPath = null;
1236
+ }
1237
+ stallDetector.update(agent.id, {
1238
+ sessionId: mintedId,
1239
+ logFilePath: logPath,
1240
+ provisional: !logPath,
1241
+ thresholdMs: stallThresholdMs,
1242
+ });
1243
+ }
1244
+ };
1245
+ const abortHandler = () => {
1246
+ scope.abort('cancelled by MCP client');
1247
+ tryKillPid(agent, strategy, cmds).catch(() => { });
1248
+ };
1249
+ extra?.signal?.addEventListener('abort', abortHandler);
1250
+ // apra-fleet-3c9.1: the signal handed to execCommand fires on EITHER the MCP
1251
+ // client's cancellation OR a confirmed stall (stallAbortController). Merging
1252
+ // them means a stall aborts the pending dispatch exactly as a client cancel
1253
+ // would, while a live (non-stalled) dispatch -- whose controller is never
1254
+ // aborted -- is left completely untouched.
1255
+ const dispatchSignal = extra?.signal
1256
+ ? AbortSignal.any([extra.signal, stallAbortController.signal])
1257
+ : stallAbortController.signal;
1258
+ // Mark agent as busy in statusline
1259
+ writeStatusline(new Map([[agent.id, 'busy']]));
1260
+ let _epExitCode = 'error';
1261
+ let _epError;
1262
+ let _epUsage;
1263
+ let _epOffline = false;
1264
+ // apra-fleet-6a7.1: gates the exit-0/empty-stdout workspace_not_trusted
1265
+ // self-heal-and-retry below to exactly one attempt per call, mirroring
1266
+ // runGitStep's authHealAttempted shape (fleet-sprint/runner.js:616) -- a
1267
+ // repeat trust failure after the heal is terminal, never looped.
1268
+ let trustHealAttempted = false;
1269
+ // apra-fleet-hzeb.2: a provider usage/quota limit (e.g. Claude's 429) can
1270
+ // never be cured by a fresh session, so it must short-circuit the
1271
+ // stale-session and server-overloaded retries below rather than burning the
1272
+ // shared retryBudget() waiting out a window that is already known. Checked
1273
+ // immediately after EVERY provider.parseResponse(result) call in the
1274
+ // dispatch path (initial dispatch, stale-session retry, overloaded retry,
1275
+ // orphan recovery, trust-heal retry) -- and regardless of result.code,
1276
+ // since a 0-exit result event whose text carries the limit message must not
1277
+ // be returned as a success response either.
1278
+ // The member's OS reaches the parser (agy's permission-denial hint differs on Windows).
1279
+ const parseCtx = { agentOs: agent.os };
1280
+ const checkUsageLimit = (r, p) => {
1281
+ const signal = provider.detectUsageLimit(r, p);
1282
+ if (!signal)
1283
+ return null;
1284
+ // Accurate finally-block exit logging (scope.fail, not scope.abort) even
1285
+ // when this fires on the very first parse, before the shared
1286
+ // `_epExitCode = result.code` assignment below would otherwise run.
1287
+ _epExitCode = r.code;
1288
+ scope.info(`usage limit detected -- resumes ~${signal.resumeAt} (${signal.resumeAtSource})`);
1289
+ // Session bookkeeping mirrors the max_turns path: record/touch the
1290
+ // session id when present so fleet-sprint can resume the SAME session
1291
+ // after the pause instead of starting a fresh one that has lost context.
1292
+ if (p.sessionId) {
1293
+ recordKnownSession(agent.id, p.sessionId);
1294
+ touchAgent(agent.id, p.sessionId);
1295
+ }
1171
1296
  return {
1172
- text: buildFailureMessage(agent.friendlyName, result, provider, parsed),
1297
+ text: `[FAIL] execute_prompt on "${agent.friendlyName}" hit a provider usage limit (resumes ~${signal.resumeAt}, ${signal.resumeAtSource}): ${signal.message}`,
1173
1298
  structuredContent: {
1174
1299
  isError: true,
1175
- reason: failureCategory === 'max_turns'
1176
- ? 'max_turns_exhausted'
1177
- : failureCategory === 'auth'
1178
- ? 'auth'
1179
- : failureCategory === 'server'
1180
- ? 'server'
1181
- : failureCategory === 'overloaded'
1182
- ? 'overloaded'
1183
- : failureCategory === 'workspace_not_trusted'
1184
- ? 'workspace_not_trusted'
1185
- : 'nonzero_exit',
1186
- // apra-fleet-63x.1: a nonzero exit -- most commonly max_turns_exhausted,
1187
- // where the CLI ran real turns and burned real tokens before hitting its
1188
- // ceiling -- still has a REAL parsed usage figure sitting in _epUsage
1189
- // (captured just above from `parsed.usage` regardless of exit code).
1190
- // This branch used to return no `usage` field at all on any failure,
1191
- // so FleetWorkflow.agent() (packages/apra-fleet-workflow/src/workflow/
1192
- // index.mjs, apra-fleet-202.3) saw hasRealUsage=false and never priced
1193
- // it, silently under-counting a sprint's tracked spend (observed: a
1194
- // 10-hour run with dozens of max_turns exhaustions reporting
1195
- // stats.totalCost of $0). Attach it here whenever it's available so
1196
- // the caller can record the real partial cost instead of nothing.
1197
- ...(_epUsage ? { usage: { input_tokens: _epUsage.input_tokens, output_tokens: _epUsage.output_tokens, total_tokens: _epUsage.input_tokens + _epUsage.output_tokens } } : {}),
1300
+ reason: 'usage_limit',
1301
+ usageLimit: signal,
1302
+ ...(p.sessionId ? { sessionId: p.sessionId } : {}),
1303
+ ...(p.usage ? { usage: { input_tokens: p.usage.input_tokens, output_tokens: p.usage.output_tokens, total_tokens: p.usage.input_tokens + p.usage.output_tokens } } : {}),
1198
1304
  },
1199
1305
  };
1200
- }
1201
- // Exit 0 but the provider parser extracted NOTHING (no result text)
1202
- // -- observed live (apra-fleet-eft.14, 2026-07-19 stabilization loop):
1203
- // the claude CLI can die silently mid-turn (member-side session
1204
- // transcript stops at a tool_result with no final assistant message)
1205
- // and still exit 0 with EMPTY stdout, so parseResponse falls through
1206
- // to its plain-text fallback with result: ''. Returning that as a
1207
- // success used to hand callers a display wrapper with nothing inside,
1208
- // which schema-extraction layers then misreported as "LLM returned
1209
- // invalid JSON". Classify it at the source as a typed dispatch error
1210
- // instead, with stderr's tail attached for diagnosis.
1211
- //
1212
- // apra-fleet-6z8.1 (lease-of-life gate): before declaring that, cross-check
1213
- // whether the captured pid is STILL ALIVE via a fresh short exec. ssh.ts
1214
- // substitutes code 0 when the channel closes without ever receiving an
1215
- // 'exit' event, so "exit 0 + empty" is ALSO exactly what a torn-down
1216
- // channel over a perfectly healthy, still-running turn looks like (live
1217
- // evidence: pid 89858 running 2+ minutes past the close). Declaring
1218
- // empty_response there orphans the CLI and invites a duplicate concurrent
1219
- // dispatch of the same scope. A confirmed-dead pid keeps today's behavior
1220
- // verbatim.
1221
- if (!parsed.result || parsed.result.trim() === '') {
1222
- const recovery = await recoverOrphanedDispatch({
1223
- strategy,
1224
- cmds,
1225
- pid: capturedPid,
1226
- durablePath,
1227
- // apra-fleet-7dir.5.4: the SAME predicate that decides `durablePath`
1228
- // above -- the mirror and recovery are one decision, and enabling
1229
- // either alone is inert (no file to read / a file nothing reads).
1230
- unsupported: !durableMirrorSupported,
1231
- os: getAgentOS(agent),
1232
- // Live for gitbash members now that `unsupported` no longer
1233
- // short-circuits every Windows member: it selects the POSIX
1234
- // `kill -0` / `cat` probes over the PowerShell ones (apra-fleet-7dir.2.4).
1235
- shell: getAgentShell(agent),
1236
- maxWaitMs: maxTotalMs !== undefined ? Math.max(maxTotalMs - (Date.now() - dispatchStartedAt), 0) : undefined,
1237
- scope,
1238
- });
1239
- if (recovery.status === 'timeout') {
1240
- scope.info(`orphan recovery timed out after ${Math.round((recovery.waitedMs ?? 0) / 1000)}s -- pid killed`);
1306
+ };
1307
+ claimGuardActive = false;
1308
+ try {
1309
+ let result;
1310
+ try {
1311
+ result = await strategy.execCommand(claudeCmd, timeoutMs, maxTotalMs, onPidCaptured, dispatchSignal);
1312
+ }
1313
+ catch (dispatchErr) {
1314
+ // apra-fleet-02s.1: a genuine command-execution exception (e.g. an
1315
+ // inactivity timeout, or any other error strategy.execCommand throws)
1316
+ // used to be unconditionally unretried here -- it bypasses both retry
1317
+ // mechanisms below, since those only fire on a non-throwing nonzero
1318
+ // exit, never on a thrown exception. Retry once with a fresh session
1319
+ // before giving up, mirroring the stale-session/server-overloaded
1320
+ // retries' bounded, single-attempt shape. Skip the retry if the client
1321
+ // itself cancelled the request -- there is nothing to recover from a
1322
+ // deliberate cancellation.
1323
+ if (extra?.signal?.aborted)
1324
+ throw dispatchErr;
1325
+ // apra-fleet-3c9.1: a stall-triggered abort is terminal. onStall already
1326
+ // killed the remote process and its session is gone, so retrying in a
1327
+ // fresh session would just re-dispatch onto a member we just tore down.
1328
+ // Let the exception surface to the outer catch, which classifies it as a
1329
+ // typed 'stalled' error instead of retrying or hanging.
1330
+ if (stallAbortController.signal.aborted)
1331
+ throw dispatchErr;
1332
+ // apra-fleet-eft.78.1: an explicit-id resume must NOT retry in a fresh
1333
+ // session (that would run a context-dependent delta prompt with no
1334
+ // context). Let the exception surface as dispatch_failed instead.
1335
+ if (!allowFreshSessionFallback)
1336
+ throw dispatchErr;
1337
+ // apra-fleet-y8q.1: no budget left to share with a retry (the original
1338
+ // attempt already consumed the whole max_total_s) -- retrying here would
1339
+ // just re-burn a fresh full budget past what the client is waiting for.
1340
+ // Let the original exception surface instead of retrying blind.
1341
+ const budget = retryBudget();
1342
+ if (budget.exhausted)
1343
+ throw dispatchErr;
1344
+ scope.info(`[${resolvedModel}] retrying -- dispatch exception: ${dispatchErr.message}`);
1345
+ await tryKillPid(agent, strategy, cmds);
1346
+ const freshOpts = { ...promptOpts, sessionId: isCallerMinted ? uuid() : undefined, resuming: false, fork: undefined };
1347
+ const retryCmd = authPrefix + cmds.buildAgentPromptCommand(provider, freshOpts);
1348
+ result = await strategy.execCommand(retryCmd, budget.timeoutMs, budget.maxTotalMs, onPidCaptured, dispatchSignal);
1349
+ }
1350
+ let parsed = provider.parseResponse(result, parseCtx);
1351
+ if (parsed.usage)
1352
+ _epUsage = parsed.usage;
1353
+ {
1354
+ const usageLimitResult = checkUsageLimit(result, parsed);
1355
+ if (usageLimitResult)
1356
+ return usageLimitResult;
1357
+ }
1358
+ // apra-fleet-eft.40.3: workspace-not-trusted degrades composed permissions
1359
+ // (project-scoped allow entries silently dropped) without killing the CLI process --
1360
+ // from there, unattended -p cannot auto-approve or prompt, so tools get denied and
1361
+ // the turn eventually fails. Blindly falling through to the stale-session /
1362
+ // server-overloaded retries below just repeats the same degraded dispatch against a
1363
+ // workspace that is still untrusted, and can walk into eft.28's dead-session hang.
1364
+ // Classify and fail fast here, before either retry path, naming
1365
+ // ensureWorkspaceTrusted (apra-fleet-eft.40.1/40.2) as the remediation.
1366
+ if (result.code !== 0 && provider.classifyError(result.stderr || result.stdout) === 'workspace_not_trusted') {
1241
1367
  return {
1242
- text: `[FAIL] execute_prompt on "${agent.friendlyName}" lost its SSH channel while the member CLI (pid ${capturedPid}) was still running, and that process was still alive after the recovery window (${Math.round((recovery.waitedMs ?? 0) / 1000)}s). The process has been killed; no result was recovered. This is NOT an empty response -- do not treat it as a failed turn without checking the member's session transcript first.`,
1243
- structuredContent: { isError: true, reason: 'orphan_recovery_timeout' },
1368
+ text: `[FAIL] ${workspaceNotTrustedAdvice(agent.friendlyName)}\n${result.stderr || result.stdout}`,
1369
+ structuredContent: { isError: true, reason: 'workspace_not_trusted' },
1244
1370
  };
1245
1371
  }
1246
- if (recovery.status === 'recovered' && recovery.stdout) {
1247
- // Feed the durable output through the normal provider parse path,
1248
- // exactly as if it had arrived on the original channel.
1249
- const recoveredParsed = provider.parseResponse({ stdout: recovery.stdout, stderr: result.stderr ?? '', code: 0 });
1250
- if (recoveredParsed.result && recoveredParsed.result.trim() !== '') {
1251
- scope.info(`recovered the real result from the durable output file after a false-alarm empty_response (waited ${Math.round((recovery.waitedMs ?? 0) / 1000)}s)`);
1252
- parsed = recoveredParsed;
1372
+ // Stale session retry -- fresh session ID, no resume. apra-fleet-eft.78.1:
1373
+ // ONLY the best-effort resume=true mode gets this transparent retry-fresh
1374
+ // recovery. An explicit session-id resume (string) deliberately does NOT --
1375
+ // its caller asserted context dependence, so a not-found id is terminal.
1376
+ // apra-fleet-lmtg.5: gate on allowFreshSessionFallback too, so an explicit
1377
+ // fork (fork="<id>") never degrades here. The mutual-exclusivity guard keeps
1378
+ // input.resume at its default true for ANY fork request, so `input.resume
1379
+ // === true` alone would also fire for an explicit fork -- a silent
1380
+ // wrong-context fresh dispatch the explicit-fork gate exists to prevent.
1381
+ // allowFreshSessionFallback is false only for explicit resume/fork; it stays
1382
+ // true for resume=true and fork=true, which the AC permits to degrade.
1383
+ if (result.code !== 0 && allowFreshSessionFallback && input.resume === true && agent.sessionId) {
1384
+ // apra-fleet-y8q.1: share the remaining max_total_s budget with this
1385
+ // retry too -- skip it outright once exhausted (see retryBudget above).
1386
+ const staleBudget = retryBudget();
1387
+ if (!staleBudget.exhausted) {
1388
+ scope.info(`[${resolvedModel}] retrying -- stale session`);
1389
+ await tryKillPid(agent, strategy, cmds);
1390
+ const freshOpts = { ...promptOpts, sessionId: isCallerMinted ? uuid() : undefined, resuming: false, fork: undefined };
1391
+ const retryCmd = authPrefix + cmds.buildAgentPromptCommand(provider, freshOpts);
1392
+ result = await strategy.execCommand(retryCmd, staleBudget.timeoutMs, staleBudget.maxTotalMs, onPidCaptured, dispatchSignal);
1393
+ parsed = provider.parseResponse(result, parseCtx);
1253
1394
  if (parsed.usage)
1254
1395
  _epUsage = parsed.usage;
1396
+ const usageLimitResult = checkUsageLimit(result, parsed);
1397
+ if (usageLimitResult)
1398
+ return usageLimitResult;
1255
1399
  }
1256
1400
  }
1257
- // apra-fleet-6a7.1: the code!==0 workspace_not_trusted classification
1258
- // above (~line 1048) never sees this branch -- exit 0 + empty stdout is
1259
- // a SEPARATE path, but live evidence (apra-fleet-2g2, fleet-win-dev1,
1260
- // 2026-08-02) showed it can be caused by the exact same trust-gate
1261
- // failure: exited 0, empty stdout, and the stderr tail contained the
1262
- // exact matched phrase, so the code!==0 classifier never ran. Classify
1263
- // it here too and -- unlike that path, which only advises a manual fix
1264
- // -- self-heal via the same seedWorkspaceTrust/ensureWorkspaceTrusted
1265
- // path compose_permissions already uses, then retry the dispatch
1266
- // exactly once (gated by trustHealAttempted, mirroring runGitStep's
1267
- // onAuthFailure shape: a repeat failure after the heal is terminal, not
1268
- // looped).
1269
- if ((!parsed.result || parsed.result.trim() === '')
1270
- && allowFreshSessionFallback
1271
- && provider.classifyError(result.stderr || result.stdout) === 'workspace_not_trusted'
1272
- && !trustHealAttempted) {
1273
- trustHealAttempted = true;
1274
- scope.info(`[${resolvedModel}] exit-0/empty-stdout classified as workspace_not_trusted -- self-healing (seedWorkspaceTrust) once, then retrying the dispatch`);
1275
- await seedWorkspaceTrust(agent, strategy, 'execute_prompt');
1276
- const healBudget = retryBudget();
1277
- if (!healBudget.exhausted) {
1401
+ // Server/overloaded error retry -- single attempt after delay. Skipped for
1402
+ // an explicit-id resume (apra-fleet-eft.78.1): the retry starts a fresh
1403
+ // session, which would discard the exact context the caller asked to resume.
1404
+ if (result.code !== 0 && allowFreshSessionFallback && isRetryable(provider.classifyError(result.stderr || result.stdout))) {
1405
+ // apra-fleet-y8q.1: share the remaining max_total_s budget with this
1406
+ // retry too -- skip it outright once exhausted (see retryBudget above).
1407
+ const overloadBudget = retryBudget();
1408
+ if (!overloadBudget.exhausted) {
1409
+ scope.info(`[${resolvedModel}] retrying -- server overloaded`);
1278
1410
  await tryKillPid(agent, strategy, cmds);
1279
- const freshOpts = { ...promptOpts, sessionId: isCallerMinted ? uuid() : undefined, resuming: false };
1411
+ await new Promise(r => setTimeout(r, SERVER_RETRY_DELAY_MS));
1412
+ const freshOpts = { ...promptOpts, sessionId: isCallerMinted ? uuid() : undefined, resuming: false, fork: undefined };
1280
1413
  const retryCmd = authPrefix + cmds.buildAgentPromptCommand(provider, freshOpts);
1281
- result = await strategy.execCommand(retryCmd, healBudget.timeoutMs, healBudget.maxTotalMs, onPidCaptured, dispatchSignal);
1282
- parsed = provider.parseResponse(result);
1414
+ result = await strategy.execCommand(retryCmd, overloadBudget.timeoutMs, overloadBudget.maxTotalMs, onPidCaptured, dispatchSignal);
1415
+ parsed = provider.parseResponse(result, parseCtx);
1283
1416
  if (parsed.usage)
1284
1417
  _epUsage = parsed.usage;
1418
+ const usageLimitResult = checkUsageLimit(result, parsed);
1419
+ if (usageLimitResult)
1420
+ return usageLimitResult;
1285
1421
  }
1286
1422
  }
1287
- if (!parsed.result || parsed.result.trim() === '') {
1288
- const stderrTail = (result.stderr || '').trim().slice(-500);
1289
- const trustClassified = provider.classifyError(result.stderr || result.stdout) === 'workspace_not_trusted';
1423
+ _epExitCode = result.code;
1424
+ // The member CLI refused tool calls for lack of a grant (AGY headless mode
1425
+ // auto-denies them and still exits 0 with status SUCCESS). Report that as
1426
+ // a permission failure the caller can heal via compose_permissions, not
1427
+ // as an empty response. Only providers whose parser sets
1428
+ // permissionDenial reach this; any partial reply is kept.
1429
+ if (parsed.permissionDenial) {
1430
+ const denial = parsed.permissionDenial;
1431
+ const partial = parsed.result?.trim() ? parsed.result.trim() : undefined;
1290
1432
  return {
1291
- text: trustClassified
1292
- ? `[FAIL] ${workspaceNotTrustedAdvice(agent.friendlyName)}\n${stderrTail}`
1293
- : `[FAIL] execute_prompt on "${agent.friendlyName}" exited 0 but produced no parseable output (empty result -- the member CLI likely died mid-turn without printing its result envelope).${stderrTail ? `\n[stderr tail]\n${stderrTail}` : ''}`,
1294
- structuredContent: { isError: true, reason: trustClassified ? 'workspace_not_trusted' : 'empty_response' },
1433
+ text: `[FAIL] execute_prompt on "${agent.friendlyName}": permission denied -- ${denial.hint}${partial ? `\n[partial response]\n${partial}` : ''}`,
1434
+ structuredContent: {
1435
+ isError: true,
1436
+ reason: 'permission_denied',
1437
+ permissionDenied: denial,
1438
+ ...(partial ? { response: partial } : {}),
1439
+ ...(parsed.sessionId ? { sessionId: parsed.sessionId } : {}),
1440
+ ...(_epUsage ? { usage: { input_tokens: _epUsage.input_tokens, output_tokens: _epUsage.output_tokens, total_tokens: _epUsage.input_tokens + _epUsage.output_tokens } } : {}),
1441
+ },
1295
1442
  };
1296
1443
  }
1297
- }
1298
- // Session-id assertion: returned id must match the one we minted/resumed
1299
- const expectedSid = resuming ? resumeTargetId : (isCallerMinted ? mintedId : undefined);
1300
- const isMismatch = expectedSid && parsed.sessionId && parsed.sessionId !== expectedSid;
1301
- if (isMismatch) {
1302
- scope.info(`session-id mismatch: expected=${expectedSid} got=${parsed.sessionId} -- not persisting`);
1303
- if (!allowFreshSessionFallback && explicitResumeId !== undefined) {
1304
- inFlightAgents.delete(agent.id);
1305
- stallDetector.remove(agent.id);
1306
- writeStatusline(new Map([[agent.id, 'idle']]));
1307
- clearStoredPid(agent.id);
1308
- if (parsed.sessionId) {
1309
- recordKnownSession(agent.id, parsed.sessionId);
1310
- }
1311
- if (parsed.usage) {
1312
- const prev = agent.tokenUsage ?? { input: 0, output: 0 };
1313
- updateAgent(agent.id, {
1314
- tokenUsage: {
1315
- input: prev.input + parsed.usage.input_tokens,
1316
- output: prev.output + parsed.usage.output_tokens,
1317
- },
1318
- });
1319
- recordSessionUsage(parsed.sessionId ?? expectedSid, parsed.usage);
1320
- if (budgetScope) {
1321
- await recordAndEvaluate({ scope: budgetScope, agent, provider, tier: resolvedTier, usage: parsed.usage });
1322
- }
1323
- }
1444
+ if (result.code !== 0) {
1445
+ // apra-fleet-391: surface an auth failure as a STRUCTURED reason (not
1446
+ // just prose in `text`) so callers -- notably fleet-sprint's
1447
+ // isAuthDispatchError -- can key off it directly instead of regexing
1448
+ // the message string. Overloading 'nonzero_exit' for this case was
1449
+ // what made auth self-heal impossible to wire reliably upstream.
1450
+ const failureCategory = isMaxTurnsResponse(parsed)
1451
+ ? 'max_turns'
1452
+ : provider.classifyError(result.stderr || result.stdout);
1324
1453
  return {
1325
- text: `[FAIL] execute_prompt on "${agent.friendlyName}" failed -- resumed session mismatch. Expected session "${expectedSid}", but provider returned "${parsed.sessionId}".`,
1454
+ text: buildFailureMessage(agent.friendlyName, result, provider, parsed),
1326
1455
  structuredContent: {
1327
1456
  isError: true,
1328
- reason: 'session_not_found',
1329
- sessionId: expectedSid,
1330
- returnedSessionId: parsed.sessionId,
1331
- ...(parsed.usage ? { usage: { ...parsed.usage, total_tokens: parsed.usage.input_tokens + parsed.usage.output_tokens } } : {}),
1457
+ reason: failureCategory === 'max_turns'
1458
+ ? 'max_turns_exhausted'
1459
+ : failureCategory === 'auth'
1460
+ ? 'auth'
1461
+ : failureCategory === 'server'
1462
+ ? 'server'
1463
+ : failureCategory === 'overloaded'
1464
+ ? 'overloaded'
1465
+ : failureCategory === 'workspace_not_trusted'
1466
+ ? 'workspace_not_trusted'
1467
+ : 'nonzero_exit',
1468
+ // apra-fleet-63x.1: a nonzero exit -- most commonly max_turns_exhausted,
1469
+ // where the CLI ran real turns and burned real tokens before hitting its
1470
+ // ceiling -- still has a REAL parsed usage figure sitting in _epUsage
1471
+ // (captured just above from `parsed.usage` regardless of exit code).
1472
+ // This branch used to return no `usage` field at all on any failure,
1473
+ // so FleetWorkflow.agent() (packages/apra-fleet-workflow/src/workflow/
1474
+ // index.mjs, apra-fleet-202.3) saw hasRealUsage=false and never priced
1475
+ // it, silently under-counting a sprint's tracked spend (observed: a
1476
+ // 10-hour run with dozens of max_turns exhaustions reporting
1477
+ // stats.totalCost of $0). Attach it here whenever it's available so
1478
+ // the caller can record the real partial cost instead of nothing.
1479
+ ...(_epUsage ? { usage: { input_tokens: _epUsage.input_tokens, output_tokens: _epUsage.output_tokens, total_tokens: _epUsage.input_tokens + _epUsage.output_tokens } } : {}),
1332
1480
  },
1333
1481
  };
1334
1482
  }
1335
- touchAgent(agent.id, undefined);
1336
- }
1337
- else {
1338
- touchAgent(agent.id, parsed.sessionId ?? expectedSid);
1339
- }
1340
- // apra-fleet-eft.78.1: mark the session this dispatch actually landed on as
1341
- // known/resumable for this member, so a later explicit-id resume of it
1342
- // passes the terminal session_not_found gate above instead of being
1343
- // rejected as unknown.
1344
- const finalSid = parsed.sessionId ?? expectedSid;
1345
- if (finalSid) {
1346
- recordKnownSession(agent.id, finalSid);
1347
- let postLogPath = null;
1348
- try {
1349
- postLogPath = resolveSessionLogPath(agent.llmProvider ?? 'claude', finalSid, resolvedWorkFolder, memberPathCtx.homeDir, memberPathCtx.targetOs);
1483
+ // Exit 0 but the provider parser extracted NOTHING (no result text)
1484
+ // -- observed live (apra-fleet-eft.14, 2026-07-19 stabilization loop):
1485
+ // the claude CLI can die silently mid-turn (member-side session
1486
+ // transcript stops at a tool_result with no final assistant message)
1487
+ // and still exit 0 with EMPTY stdout, so parseResponse falls through
1488
+ // to its plain-text fallback with result: ''. Returning that as a
1489
+ // success used to hand callers a display wrapper with nothing inside,
1490
+ // which schema-extraction layers then misreported as "LLM returned
1491
+ // invalid JSON". Classify it at the source as a typed dispatch error
1492
+ // instead, with stderr's tail attached for diagnosis.
1493
+ //
1494
+ // apra-fleet-6z8.1 (lease-of-life gate): before declaring that, cross-check
1495
+ // whether the captured pid is STILL ALIVE via a fresh short exec. ssh.ts
1496
+ // substitutes code 0 when the channel closes without ever receiving an
1497
+ // 'exit' event, so "exit 0 + empty" is ALSO exactly what a torn-down
1498
+ // channel over a perfectly healthy, still-running turn looks like (live
1499
+ // evidence: pid 89858 running 2+ minutes past the close). Declaring
1500
+ // empty_response there orphans the CLI and invites a duplicate concurrent
1501
+ // dispatch of the same scope. A confirmed-dead pid keeps today's behavior
1502
+ // verbatim.
1503
+ if (!parsed.result || parsed.result.trim() === '') {
1504
+ const recovery = await recoverOrphanedDispatch({
1505
+ strategy,
1506
+ cmds,
1507
+ pid: capturedPid,
1508
+ durablePath,
1509
+ // apra-fleet-7dir.5.4: the SAME predicate that decides `durablePath`
1510
+ // above -- the mirror and recovery are one decision, and enabling
1511
+ // either alone is inert (no file to read / a file nothing reads).
1512
+ unsupported: !durableMirrorSupported,
1513
+ os: getAgentOS(agent),
1514
+ // Live for gitbash members now that `unsupported` no longer
1515
+ // short-circuits every Windows member: it selects the POSIX
1516
+ // `kill -0` / `cat` probes over the PowerShell ones (apra-fleet-7dir.2.4).
1517
+ shell: getAgentShell(agent),
1518
+ maxWaitMs: maxTotalMs !== undefined ? Math.max(maxTotalMs - (Date.now() - dispatchStartedAt), 0) : undefined,
1519
+ scope,
1520
+ });
1521
+ if (recovery.status === 'timeout') {
1522
+ scope.info(`orphan recovery timed out after ${Math.round((recovery.waitedMs ?? 0) / 1000)}s -- pid killed`);
1523
+ return {
1524
+ text: `[FAIL] execute_prompt on "${agent.friendlyName}" lost its SSH channel while the member CLI (pid ${capturedPid}) was still running, and that process was still alive after the recovery window (${Math.round((recovery.waitedMs ?? 0) / 1000)}s). The process has been killed; no result was recovered. This is NOT an empty response -- do not treat it as a failed turn without checking the member's session transcript first.`,
1525
+ structuredContent: { isError: true, reason: 'orphan_recovery_timeout' },
1526
+ };
1527
+ }
1528
+ if (recovery.status === 'recovered' && recovery.stdout) {
1529
+ // Feed the durable output through the normal provider parse path,
1530
+ // exactly as if it had arrived on the original channel.
1531
+ const recoveredResult = { stdout: recovery.stdout, stderr: result.stderr ?? '', code: 0 };
1532
+ const recoveredParsed = provider.parseResponse(recoveredResult, parseCtx);
1533
+ if (recoveredParsed.result && recoveredParsed.result.trim() !== '') {
1534
+ scope.info(`recovered the real result from the durable output file after a false-alarm empty_response (waited ${Math.round((recovery.waitedMs ?? 0) / 1000)}s)`);
1535
+ parsed = recoveredParsed;
1536
+ if (parsed.usage)
1537
+ _epUsage = parsed.usage;
1538
+ const usageLimitResult = checkUsageLimit(recoveredResult, recoveredParsed);
1539
+ if (usageLimitResult)
1540
+ return usageLimitResult;
1541
+ }
1542
+ }
1543
+ // apra-fleet-6a7.1: the code!==0 workspace_not_trusted classification
1544
+ // above (~line 1048) never sees this branch -- exit 0 + empty stdout is
1545
+ // a SEPARATE path, but live evidence (apra-fleet-2g2, fleet-win-dev1,
1546
+ // 2026-08-02) showed it can be caused by the exact same trust-gate
1547
+ // failure: exited 0, empty stdout, and the stderr tail contained the
1548
+ // exact matched phrase, so the code!==0 classifier never ran. Classify
1549
+ // it here too and -- unlike that path, which only advises a manual fix
1550
+ // -- self-heal via the same seedWorkspaceTrust/ensureWorkspaceTrusted
1551
+ // path compose_permissions already uses, then retry the dispatch
1552
+ // exactly once (gated by trustHealAttempted, mirroring runGitStep's
1553
+ // onAuthFailure shape: a repeat failure after the heal is terminal, not
1554
+ // looped).
1555
+ if ((!parsed.result || parsed.result.trim() === '')
1556
+ && allowFreshSessionFallback
1557
+ && provider.classifyError(result.stderr || result.stdout) === 'workspace_not_trusted'
1558
+ && !trustHealAttempted) {
1559
+ trustHealAttempted = true;
1560
+ scope.info(`[${resolvedModel}] exit-0/empty-stdout classified as workspace_not_trusted -- self-healing (seedWorkspaceTrust) once, then retrying the dispatch`);
1561
+ await seedWorkspaceTrust(agent, strategy, 'execute_prompt');
1562
+ const healBudget = retryBudget();
1563
+ if (!healBudget.exhausted) {
1564
+ await tryKillPid(agent, strategy, cmds);
1565
+ const freshOpts = { ...promptOpts, sessionId: isCallerMinted ? uuid() : undefined, resuming: false, fork: undefined };
1566
+ const retryCmd = authPrefix + cmds.buildAgentPromptCommand(provider, freshOpts);
1567
+ result = await strategy.execCommand(retryCmd, healBudget.timeoutMs, healBudget.maxTotalMs, onPidCaptured, dispatchSignal);
1568
+ parsed = provider.parseResponse(result, parseCtx);
1569
+ if (parsed.usage)
1570
+ _epUsage = parsed.usage;
1571
+ const usageLimitResult = checkUsageLimit(result, parsed);
1572
+ if (usageLimitResult)
1573
+ return usageLimitResult;
1574
+ }
1575
+ }
1576
+ if (!parsed.result || parsed.result.trim() === '') {
1577
+ const stderrTail = (result.stderr || '').trim().slice(-500);
1578
+ const trustClassified = provider.classifyError(result.stderr || result.stdout) === 'workspace_not_trusted';
1579
+ return {
1580
+ text: trustClassified
1581
+ ? `[FAIL] ${workspaceNotTrustedAdvice(agent.friendlyName)}\n${stderrTail}`
1582
+ : `[FAIL] execute_prompt on "${agent.friendlyName}" exited 0 but produced no parseable output (empty result -- the member CLI likely died mid-turn without printing its result envelope).${stderrTail ? `\n[stderr tail]\n${stderrTail}` : ''}`,
1583
+ structuredContent: { isError: true, reason: trustClassified ? 'workspace_not_trusted' : 'empty_response' },
1584
+ };
1585
+ }
1350
1586
  }
1351
- catch {
1352
- postLogPath = null;
1587
+ // Session-id assertion: returned id must match the one we minted/resumed.
1588
+ // apra-fleet-lmtg.5: a fork now gets the SAME assertion as any other
1589
+ // caller-minted session. mintedId is explicitly passed to the CLI as
1590
+ // --session-id even in fork mode (provider.forkFlag/buildForkFlag), and the
1591
+ // CLI honors it -- so it is no longer a placeholder the CLI silently
1592
+ // discards, and a genuine mismatch here is a real signal worth catching
1593
+ // rather than something to exempt.
1594
+ const expectedSid = resuming ? resumeTargetId : (isCallerMinted ? mintedId : undefined);
1595
+ const isMismatch = expectedSid && parsed.sessionId && parsed.sessionId !== expectedSid;
1596
+ if (isMismatch) {
1597
+ scope.info(`session-id mismatch: expected=${expectedSid} got=${parsed.sessionId} -- not persisting`);
1598
+ if (!allowFreshSessionFallback && (explicitResumeId !== undefined || explicitForkId !== undefined)) {
1599
+ inFlightAgents.delete(agent.id);
1600
+ stallDetector.remove(agent.id);
1601
+ writeStatusline(new Map([[agent.id, 'idle']]));
1602
+ clearStoredPid(agent.id);
1603
+ if (parsed.sessionId) {
1604
+ recordKnownSession(agent.id, parsed.sessionId);
1605
+ }
1606
+ if (parsed.usage) {
1607
+ const prev = agent.tokenUsage ?? { input: 0, output: 0 };
1608
+ updateAgent(agent.id, {
1609
+ tokenUsage: {
1610
+ input: prev.input + parsed.usage.input_tokens,
1611
+ output: prev.output + parsed.usage.output_tokens,
1612
+ },
1613
+ });
1614
+ recordSessionUsage(parsed.sessionId ?? expectedSid, parsed.usage);
1615
+ if (budgetScope) {
1616
+ await recordAndEvaluate({ scope: budgetScope, agent, provider, tier: resolvedTier, usage: parsed.usage });
1617
+ }
1618
+ }
1619
+ return {
1620
+ text: `[FAIL] execute_prompt on "${agent.friendlyName}" failed -- resumed session mismatch. Expected session "${expectedSid}", but provider returned "${parsed.sessionId}".`,
1621
+ structuredContent: {
1622
+ isError: true,
1623
+ reason: 'session_not_found',
1624
+ sessionId: expectedSid,
1625
+ returnedSessionId: parsed.sessionId,
1626
+ ...(parsed.usage ? { usage: { ...parsed.usage, total_tokens: parsed.usage.input_tokens + parsed.usage.output_tokens } } : {}),
1627
+ },
1628
+ };
1629
+ }
1630
+ touchAgent(agent.id, undefined);
1353
1631
  }
1354
- stallDetector.update(agent.id, {
1355
- sessionId: finalSid,
1356
- logFilePath: postLogPath,
1357
- provisional: !postLogPath,
1358
- });
1359
- }
1360
- clearStoredPid(agent.id);
1361
- if (parsed.usage) {
1362
- const prev = agent.tokenUsage ?? { input: 0, output: 0 };
1363
- updateAgent(agent.id, {
1364
- tokenUsage: {
1365
- input: prev.input + parsed.usage.input_tokens,
1366
- output: prev.output + parsed.usage.output_tokens,
1367
- },
1368
- });
1369
- // Per-session cumulative tracking for context-headroom admission
1370
- // (apra-fleet-eft.81.1) -- separate from the per-member lifetime total
1371
- // just above. Keyed by whichever session id this dispatch actually
1372
- // landed on (the returned id, falling back to the minted/resumed one),
1373
- // so the NEXT admission check on this same session sees this dispatch's
1374
- // usage already accounted for.
1375
- recordSessionUsage(parsed.sessionId ?? mintedId, parsed.usage);
1376
- }
1377
- // Usage/budget accounting (apra-fleet-eft.80.2): account this dispatch's
1378
- // token usage against the scope's budget (self-metered only when the
1379
- // provider has no native getUsage()), then attach the usage block to the
1380
- // result when the warning band is crossed. Nothing is attached below the
1381
- // band, and nothing happens at all when no budget is configured.
1382
- let budgetUsage;
1383
- if (budgetScope && parsed.usage) {
1384
- const postBudget = await recordAndEvaluate({ scope: budgetScope, agent, provider, tier: resolvedTier, usage: parsed.usage });
1385
- if (postBudget?.warned)
1386
- budgetUsage = postBudget.block;
1387
- }
1388
- let output = `[RESULT] Response from ${agent.friendlyName}:
1632
+ else {
1633
+ touchAgent(agent.id, parsed.sessionId ?? expectedSid);
1634
+ }
1635
+ // apra-fleet-eft.78.1: mark the session this dispatch actually landed on as
1636
+ // known/resumable for this member, so a later explicit-id resume of it
1637
+ // passes the terminal session_not_found gate above instead of being
1638
+ // rejected as unknown.
1639
+ const finalSid = parsed.sessionId ?? expectedSid;
1640
+ if (finalSid) {
1641
+ recordKnownSession(agent.id, finalSid);
1642
+ let postLogPath = null;
1643
+ try {
1644
+ postLogPath = resolveSessionLogPath(agent.llmProvider ?? 'claude', finalSid, resolvedWorkFolder, memberPathCtx.homeDir, memberPathCtx.targetOs);
1645
+ }
1646
+ catch {
1647
+ postLogPath = null;
1648
+ }
1649
+ stallDetector.update(agent.id, {
1650
+ sessionId: finalSid,
1651
+ logFilePath: postLogPath,
1652
+ provisional: !postLogPath,
1653
+ thresholdMs: stallThresholdMs,
1654
+ });
1655
+ }
1656
+ clearStoredPid(agent.id);
1657
+ if (parsed.usage) {
1658
+ const prev = agent.tokenUsage ?? { input: 0, output: 0 };
1659
+ updateAgent(agent.id, {
1660
+ tokenUsage: {
1661
+ input: prev.input + parsed.usage.input_tokens,
1662
+ output: prev.output + parsed.usage.output_tokens,
1663
+ },
1664
+ });
1665
+ // Per-session cumulative tracking for context-headroom admission
1666
+ // (apra-fleet-eft.81.1) -- separate from the per-member lifetime total
1667
+ // just above. Keyed by whichever session id this dispatch actually
1668
+ // landed on (the returned id, falling back to the minted/resumed one),
1669
+ // so the NEXT admission check on this same session sees this dispatch's
1670
+ // usage already accounted for.
1671
+ recordSessionUsage(parsed.sessionId ?? mintedId, parsed.usage);
1672
+ }
1673
+ // Usage/budget accounting (apra-fleet-eft.80.2): account this dispatch's
1674
+ // token usage against the scope's budget (self-metered only when the
1675
+ // provider has no native getUsage()), then attach the usage block to the
1676
+ // result when the warning band is crossed. Nothing is attached below the
1677
+ // band, and nothing happens at all when no budget is configured.
1678
+ let budgetUsage;
1679
+ if (budgetScope && parsed.usage) {
1680
+ const postBudget = await recordAndEvaluate({ scope: budgetScope, agent, provider, tier: resolvedTier, usage: parsed.usage });
1681
+ if (postBudget?.warned)
1682
+ budgetUsage = postBudget.block;
1683
+ }
1684
+ let output = `[RESULT] Response from ${agent.friendlyName}:
1389
1685
 
1390
1686
  ${parsed.result}`;
1391
- if (parsed.sessionId)
1392
- output += `
1687
+ if (parsed.sessionId)
1688
+ output += `
1393
1689
 
1394
1690
  ---
1395
1691
  session: ${parsed.sessionId}`;
1396
- // Auto-harvest learnings from session output (fire-and-forget)
1397
- if (parsed.result) {
1398
- // apra-fleet-tm7.2: always pass resolvedWorkFolder, local or remote.
1399
- // getKbProviders(undefined) falls back to slugFor(process.cwd()) --
1400
- // the FLEET SERVER's own cwd, not any generic 'default' -- so omitting
1401
- // repo_path for remote members would silently route their harvested
1402
- // learnings into the server's own repo KB (the exact defect apra-fleet-tm7
1403
- // describes).
1404
- //
1405
- // apra-fleet-b4g.6: repo_remote_url (apra-fleet-b4g.1) is how a REMOTE
1406
- // member's call reaches the SAME shared project KB a local clone would --
1407
- // without it, resolvedWorkFolder is a path on the remote host that will
1408
- // almost never exist on this (fleet server) machine, resolveProjectSlug's
1409
- // git calls on it fail with ENOENT, and it falls through to the literal
1410
- // 'default' slug, pooling every such member's harvest into one shared,
1411
- // useless bucket (no cross-repo contamination, but no real routing
1412
- // either). knownRepoRemoteUrl() forwards it whenever the member's own
1413
- // registration record already carries a genuine URL. It deliberately does
1414
- // NOT construct one from agent.gitRepos' bare "owner/repo" access-list
1415
- // entries (see src/services/vcs/github.ts's own
1416
- // https://github.com/${repo}.git construction for a DIFFERENT, narrower
1417
- // purpose -- connectivity testing) or shell out to the member host to
1418
- // discover it: guessing a URL that turns out wrong would route the
1419
- // harvest into a KB slug that does not match the repo's real local-clone
1420
- // slug, which is worse than today's honest 'default' degradation. When no
1421
- // genuine URL is known, behaviour is unchanged from before this fix.
1422
- // KB-TRUST PHASE 1 (apra-fleet-4wz.8): report the harvest counters. This is
1423
- // the highest-volume KB writer and its result was previously discarded
1424
- // entirely, so entries_rejected -- the fail-closed signal -- was invisible
1425
- // on the one path that produces most of it. Rejections are EXPECTED to
1426
- // dominate here: harvest's regex extraction frequently yields no file
1427
- // paths at all, and an entry with no basis is refused by design. A high
1428
- // rejected count is the invariant working, not a regression to tune away.
1429
- void import('./kb-harvest.js')
1430
- .then(({ kbHarvest }) => kbHarvest({
1431
- repo_path: resolvedWorkFolder,
1432
- repo_remote_url: knownRepoRemoteUrl(agent),
1433
- session_transcript: parsed.result,
1434
- session_id: parsed.sessionId,
1435
- }))
1436
- .then((raw) => {
1437
- try {
1438
- const r = JSON.parse(raw);
1439
- if (r.entries_captured || r.entries_updated || r.entries_skipped || r.entries_rejected) {
1440
- logLine('kb_harvest', `auto-harvest: captured=${r.entries_captured} updated=${r.entries_updated} skipped=${r.entries_skipped} rejected=${r.entries_rejected}`);
1692
+ // Auto-harvest learnings from session output (fire-and-forget)
1693
+ if (parsed.result) {
1694
+ // apra-fleet-tm7.2: always pass resolvedWorkFolder, local or remote.
1695
+ // getKbProviders(undefined) falls back to slugFor(process.cwd()) --
1696
+ // the FLEET SERVER's own cwd, not any generic 'default' -- so omitting
1697
+ // repo_path for remote members would silently route their harvested
1698
+ // learnings into the server's own repo KB (the exact defect apra-fleet-tm7
1699
+ // describes).
1700
+ //
1701
+ // apra-fleet-b4g.6: repo_remote_url (apra-fleet-b4g.1) is how a REMOTE
1702
+ // member's call reaches the SAME shared project KB a local clone would --
1703
+ // without it, resolvedWorkFolder is a path on the remote host that will
1704
+ // almost never exist on this (fleet server) machine, resolveProjectSlug's
1705
+ // git calls on it fail with ENOENT, and it falls through to the literal
1706
+ // 'default' slug, pooling every such member's harvest into one shared,
1707
+ // useless bucket (no cross-repo contamination, but no real routing
1708
+ // either). knownRepoRemoteUrl() forwards it whenever the member's own
1709
+ // registration record already carries a genuine URL. It deliberately does
1710
+ // NOT construct one from agent.gitRepos' bare "owner/repo" access-list
1711
+ // entries (see src/services/vcs/github.ts's own
1712
+ // https://github.com/${repo}.git construction for a DIFFERENT, narrower
1713
+ // purpose -- connectivity testing) or shell out to the member host to
1714
+ // discover it: guessing a URL that turns out wrong would route the
1715
+ // harvest into a KB slug that does not match the repo's real local-clone
1716
+ // slug, which is worse than today's honest 'default' degradation. When no
1717
+ // genuine URL is known, behaviour is unchanged from before this fix.
1718
+ // KB-TRUST PHASE 1 (apra-fleet-4wz.8): report the harvest counters. This is
1719
+ // the highest-volume KB writer and its result was previously discarded
1720
+ // entirely, so entries_rejected -- the fail-closed signal -- was invisible
1721
+ // on the one path that produces most of it. Rejections are EXPECTED to
1722
+ // dominate here: harvest's regex extraction frequently yields no file
1723
+ // paths at all, and an entry with no basis is refused by design. A high
1724
+ // rejected count is the invariant working, not a regression to tune away.
1725
+ void import('./kb-harvest.js')
1726
+ .then(({ kbHarvest }) => kbHarvest({
1727
+ repo_path: resolvedWorkFolder,
1728
+ repo_remote_url: knownRepoRemoteUrl(agent),
1729
+ session_transcript: parsed.result,
1730
+ session_id: parsed.sessionId,
1731
+ }))
1732
+ .then((raw) => {
1733
+ try {
1734
+ const r = JSON.parse(raw);
1735
+ if (r.entries_captured || r.entries_updated || r.entries_skipped || r.entries_rejected) {
1736
+ logLine('kb_harvest', `auto-harvest: captured=${r.entries_captured} updated=${r.entries_updated} skipped=${r.entries_skipped} rejected=${r.entries_rejected}`);
1737
+ }
1441
1738
  }
1442
- }
1443
- catch {
1444
- // A non-JSON payload is not worth failing a fire-and-forget harvest over.
1445
- }
1446
- })
1447
- .catch((err) => logWarn('kb_harvest', `auto-harvest failed: ${err.message}`));
1739
+ catch {
1740
+ // A non-JSON payload is not worth failing a fire-and-forget harvest over.
1741
+ }
1742
+ })
1743
+ .catch((err) => logWarn('kb_harvest', `auto-harvest failed: ${err.message}`));
1744
+ }
1745
+ if (heuristicWarningSuffix)
1746
+ output += heuristicWarningSuffix;
1747
+ return {
1748
+ text: output,
1749
+ structuredContent: {
1750
+ response: parsed.result,
1751
+ ...(_epUsage ? { usage: { input_tokens: _epUsage.input_tokens, output_tokens: _epUsage.output_tokens, total_tokens: _epUsage.input_tokens + _epUsage.output_tokens } } : {}),
1752
+ ...(parsed.sessionId ? { sessionId: parsed.sessionId } : {}),
1753
+ ...(contextWarning ? { contextWarning } : {}),
1754
+ ...(budgetUsage ? { budgetUsage } : {}),
1755
+ },
1756
+ };
1448
1757
  }
1449
- if (heuristicWarningSuffix)
1450
- output += heuristicWarningSuffix;
1451
- return {
1452
- text: output,
1453
- structuredContent: {
1454
- response: parsed.result,
1455
- ...(_epUsage ? { usage: { input_tokens: _epUsage.input_tokens, output_tokens: _epUsage.output_tokens, total_tokens: _epUsage.input_tokens + _epUsage.output_tokens } } : {}),
1456
- ...(parsed.sessionId ? { sessionId: parsed.sessionId } : {}),
1457
- ...(contextWarning ? { contextWarning } : {}),
1458
- ...(budgetUsage ? { budgetUsage } : {}),
1459
- },
1460
- };
1461
- }
1462
- catch (err) {
1463
- // apra-fleet-3c9.1: a confirmed stall aborted the in-flight execCommand (and
1464
- // NOT the MCP client). Surface it as a typed 'stalled' error so the dispatch
1465
- // settles here -- well under the client hard timeout -- instead of being
1466
- // mislabeled dispatch_failed or waiting out the full deadline.
1467
- if (stallAbortController.signal.aborted && !extra?.signal?.aborted) {
1468
- _epError = 'dispatch aborted by confirmed stall';
1758
+ catch (err) {
1759
+ // apra-fleet-3c9.1: a confirmed stall aborted the in-flight execCommand (and
1760
+ // NOT the MCP client). Surface it as a typed 'stalled' error so the dispatch
1761
+ // settles here -- well under the client hard timeout -- instead of being
1762
+ // mislabeled dispatch_failed or waiting out the full deadline.
1763
+ if (stallAbortController.signal.aborted && !extra?.signal?.aborted) {
1764
+ _epError = 'dispatch aborted by confirmed stall';
1765
+ return {
1766
+ text: `[FAIL] execute_prompt on "${agent.friendlyName}" was aborted after a confirmed stall -- the remote turn made no progress for the stall threshold, its process was killed, and the in-flight dispatch was cancelled immediately rather than waiting out the client timeout.`,
1767
+ structuredContent: { isError: true, reason: 'stalled' },
1768
+ };
1769
+ }
1770
+ // Only mark offline for genuine SSH/network connection failures, not for cancellations
1771
+ _epOffline = !!(err.message && /ssh|network|econnrefused|ehostunreach|connection timed out/i.test(err.message));
1772
+ _epError = err.message;
1469
1773
  return {
1470
- text: `[FAIL] execute_prompt on "${agent.friendlyName}" was aborted after a confirmed stall -- the remote turn made no progress for the stall threshold, its process was killed, and the in-flight dispatch was cancelled immediately rather than waiting out the client timeout.`,
1471
- structuredContent: { isError: true, reason: 'stalled' },
1774
+ text: `[FAIL] Failed to execute prompt on "${agent.friendlyName}": ${err.message}`,
1775
+ structuredContent: { isError: true, reason: 'dispatch_failed' },
1472
1776
  };
1473
1777
  }
1474
- // Only mark offline for genuine SSH/network connection failures, not for cancellations
1475
- _epOffline = !!(err.message && /ssh|network|econnrefused|ehostunreach|connection timed out/i.test(err.message));
1476
- _epError = err.message;
1477
- return {
1478
- text: `[FAIL] Failed to execute prompt on "${agent.friendlyName}": ${err.message}`,
1479
- structuredContent: { isError: true, reason: 'dispatch_failed' },
1480
- };
1778
+ finally {
1779
+ extra?.signal?.removeEventListener('abort', abortHandler);
1780
+ const _epTok = _epUsage ? ` in=${_epUsage.input_tokens} out=${_epUsage.output_tokens}` : '';
1781
+ if (_epExitCode === 'error')
1782
+ scope.abort(`${_epError ?? 'exception'}${_epTok}`);
1783
+ else if (_epExitCode !== 0)
1784
+ scope.fail(`exit=${_epExitCode}${_epTok}`);
1785
+ else
1786
+ scope.ok(`exit=0${_epTok}`);
1787
+ // Skip if stall detector already cleared state -- a new execute_prompt may have
1788
+ // claimed inFlightAgents and set busy again; clobbering it here would be wrong.
1789
+ if (!clearedByStall) {
1790
+ writeStatusline(new Map([[agent.id, _epOffline ? 'offline' : 'idle']]));
1791
+ inFlightAgents.delete(agent.id);
1792
+ }
1793
+ stallDetector.remove(agent.id);
1794
+ await deletePromptFile(agent, strategy, promptFilePath, durablePath ? [durablePath] : []);
1795
+ }
1481
1796
  }
1482
- finally {
1483
- extra?.signal?.removeEventListener('abort', abortHandler);
1484
- const _epTok = _epUsage ? ` in=${_epUsage.input_tokens} out=${_epUsage.output_tokens}` : '';
1485
- if (_epExitCode === 'error')
1486
- scope.abort(`${_epError ?? 'exception'}${_epTok}`);
1487
- else if (_epExitCode !== 0)
1488
- scope.fail(`exit=${_epExitCode}${_epTok}`);
1489
- else
1490
- scope.ok(`exit=0${_epTok}`);
1491
- // Skip if stall detector already cleared state -- a new execute_prompt may have
1492
- // claimed inFlightAgents and set busy again; clobbering it here would be wrong.
1493
- if (!clearedByStall) {
1494
- writeStatusline(new Map([[agent.id, _epOffline ? 'offline' : 'idle']]));
1797
+ catch (err) {
1798
+ if (claimGuardActive) {
1495
1799
  inFlightAgents.delete(agent.id);
1800
+ getStallDetector().remove(agent.id);
1496
1801
  }
1497
- stallDetector.remove(agent.id);
1498
- await deletePromptFile(agent, strategy, promptFilePath, durablePath ? [durablePath] : []);
1802
+ throw err;
1499
1803
  }
1500
1804
  }
1501
1805
  //# sourceMappingURL=execute-prompt.js.map