@apralabs/apra-fleet 0.4.2 → 0.4.4

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 (997) hide show
  1. package/README.md +103 -34
  2. package/dist/agents/backlog-groomer.md +87 -14
  3. package/dist/agents/ci-watcher.md +35 -9
  4. package/dist/agents/deployer.md +63 -11
  5. package/dist/agents/doer.md +99 -41
  6. package/dist/agents/harvester.md +47 -27
  7. package/dist/agents/integ-test-runner.md +38 -10
  8. package/dist/agents/kb-reconciler.md +37 -7
  9. package/dist/agents/plan-reviewer.md +145 -32
  10. package/dist/agents/planner.md +180 -69
  11. package/dist/agents/regression-test-runner.md +98 -64
  12. package/dist/agents/reviewer.md +117 -45
  13. package/dist/agents/schemas/backlog-groomer-output.json +1 -1
  14. package/dist/agents/schemas/doer-output.json +32 -0
  15. package/dist/agents/schemas/harvester-input.json +1 -5
  16. package/dist/agents/schemas/plan-reviewer-output.json +37 -0
  17. package/dist/agents/schemas/planner-output.json +1 -1
  18. package/dist/agents/schemas/regression-test-runner-output.json +41 -7
  19. package/dist/agents/schemas/reviewer-output.json +62 -3
  20. package/dist/cli/agent-transform.d.ts +90 -2
  21. package/dist/cli/agent-transform.js +310 -20
  22. package/dist/cli/auth.d.ts +1 -0
  23. package/dist/cli/auth.js +66 -17
  24. package/dist/cli/beads-install.d.ts +35 -0
  25. package/dist/cli/beads-install.js +125 -0
  26. package/dist/cli/beads-pin.d.ts +5 -0
  27. package/dist/cli/beads-pin.js +27 -0
  28. package/dist/cli/call.d.ts +27 -0
  29. package/dist/cli/call.js +177 -0
  30. package/dist/cli/config.d.ts +31 -6
  31. package/dist/cli/config.js +113 -36
  32. package/dist/cli/dolt-install.d.ts +18 -2
  33. package/dist/cli/dolt-install.js +46 -15
  34. package/dist/cli/fleet-se-guard.d.ts +51 -0
  35. package/dist/cli/fleet-se-guard.js +215 -0
  36. package/dist/cli/install-guard.d.ts +54 -0
  37. package/dist/cli/install-guard.js +132 -4
  38. package/dist/cli/install.d.ts +100 -11
  39. package/dist/cli/install.js +864 -139
  40. package/dist/cli/kb-commit.d.ts +2 -1
  41. package/dist/cli/kb-commit.js +1 -1
  42. package/dist/cli/kb-directives.js +2 -1
  43. package/dist/cli/kb-import.d.ts +2 -1
  44. package/dist/cli/kb-import.js +1 -1
  45. package/dist/cli/register-member.js +26 -5
  46. package/dist/cli/remove-member.d.ts +4 -0
  47. package/dist/cli/remove-member.js +63 -0
  48. package/dist/cli/secret.js +9 -0
  49. package/dist/cli/start.js +59 -4
  50. package/dist/cli/status.d.ts +3 -0
  51. package/dist/cli/status.js +45 -12
  52. package/dist/cli/stop.js +64 -9
  53. package/dist/cli/uninstall.js +101 -15
  54. package/dist/cli/update.js +33 -2
  55. package/dist/cli/watch.js +1 -1
  56. package/dist/cli/workflow.js +3 -2
  57. package/dist/commands/kb-server.d.ts +3 -0
  58. package/dist/commands/kb-server.js +52 -5
  59. package/dist/hub-service/http-server.d.ts +5 -1
  60. package/dist/hub-service/http-server.js +55 -5
  61. package/dist/index.js +158 -11
  62. package/dist/os/child-env-bound.d.ts +77 -0
  63. package/dist/os/child-env-bound.js +171 -0
  64. package/dist/os/linux.d.ts +13 -2
  65. package/dist/os/linux.js +75 -16
  66. package/dist/os/macos.d.ts +1 -1
  67. package/dist/os/macos.js +2 -11
  68. package/dist/os/os-commands.d.ts +78 -4
  69. package/dist/os/windows-gitbash.d.ts +6 -0
  70. package/dist/os/windows-gitbash.js +32 -3
  71. package/dist/os/windows-wrapper.d.ts +0 -4
  72. package/dist/os/windows-wrapper.js +2 -1
  73. package/dist/os/windows.d.ts +11 -2
  74. package/dist/os/windows.js +121 -32
  75. package/dist/paths.d.ts +21 -0
  76. package/dist/paths.js +35 -0
  77. package/dist/providers/agy.d.ts +120 -7
  78. package/dist/providers/agy.js +741 -86
  79. package/dist/providers/claude-permission-denial.d.ts +7 -0
  80. package/dist/providers/claude-permission-denial.js +137 -0
  81. package/dist/providers/claude.d.ts +132 -6
  82. package/dist/providers/claude.js +644 -48
  83. package/dist/providers/codex.d.ts +4 -2
  84. package/dist/providers/codex.js +23 -2
  85. package/dist/providers/copilot.d.ts +4 -2
  86. package/dist/providers/copilot.js +24 -2
  87. package/dist/providers/none.d.ts +4 -2
  88. package/dist/providers/none.js +17 -2
  89. package/dist/providers/opencode.d.ts +12 -2
  90. package/dist/providers/opencode.js +135 -54
  91. package/dist/providers/provider.d.ts +368 -35
  92. package/dist/providers/provider.js +219 -2
  93. package/dist/services/agent-provisioner.d.ts +29 -0
  94. package/dist/services/agent-provisioner.js +107 -0
  95. package/dist/services/agent-shadow.d.ts +96 -0
  96. package/dist/services/agent-shadow.js +479 -0
  97. package/dist/services/agy-project.d.ts +103 -0
  98. package/dist/services/agy-project.js +354 -0
  99. package/dist/services/auth-socket.d.ts +25 -0
  100. package/dist/services/auth-socket.js +72 -27
  101. package/dist/services/auth-web.js +2 -2
  102. package/dist/services/budget-awareness.d.ts +11 -11
  103. package/dist/services/budget-awareness.js +15 -3
  104. package/dist/services/claude-local-scope-mcp.d.ts +19 -0
  105. package/dist/services/claude-local-scope-mcp.js +91 -0
  106. package/dist/services/cloud/aws.js +2 -1
  107. package/dist/services/cloud/lifecycle.js +52 -6
  108. package/dist/services/cloud/task-wrapper.d.ts +0 -9
  109. package/dist/services/cloud/task-wrapper.js +15 -6
  110. package/dist/services/credential-cleanup.js +35 -17
  111. package/dist/services/event-loop-watchdog.d.ts +32 -0
  112. package/dist/services/event-loop-watchdog.js +140 -0
  113. package/dist/services/exit-drain.d.ts +44 -0
  114. package/dist/services/exit-drain.js +52 -0
  115. package/dist/services/github-app.js +32 -4
  116. package/dist/services/http-transport.d.ts +50 -1
  117. package/dist/services/http-transport.js +238 -25
  118. package/dist/services/in-flight-agents.d.ts +2 -0
  119. package/dist/services/in-flight-agents.js +4 -0
  120. package/dist/services/knowledge/bible-basis-filter.d.ts +53 -0
  121. package/dist/services/knowledge/bible-basis-filter.js +134 -0
  122. package/dist/services/knowledge/bible-blob-id.d.ts +44 -0
  123. package/dist/services/knowledge/bible-blob-id.js +113 -0
  124. package/dist/services/knowledge/bible-import.d.ts +65 -0
  125. package/dist/services/knowledge/bible-import.js +211 -0
  126. package/dist/services/knowledge/file-hash.d.ts +31 -0
  127. package/dist/services/knowledge/file-hash.js +153 -23
  128. package/dist/services/knowledge/http-provider.d.ts +4 -3
  129. package/dist/services/knowledge/http-provider.js +17 -5
  130. package/dist/services/knowledge/kb-config.d.ts +42 -0
  131. package/dist/services/knowledge/kb-config.js +54 -0
  132. package/dist/services/knowledge/kb-maintainer-grant.d.ts +15 -0
  133. package/dist/services/knowledge/kb-maintainer-grant.js +34 -0
  134. package/dist/services/knowledge/kb-providers.d.ts +11 -1
  135. package/dist/services/knowledge/kb-providers.js +199 -17
  136. package/dist/services/knowledge/kb-removed-scope-keys.d.ts +28 -0
  137. package/dist/services/knowledge/kb-removed-scope-keys.js +59 -0
  138. package/dist/services/knowledge/kb-scopes.d.ts +14 -0
  139. package/dist/services/knowledge/kb-scopes.js +47 -0
  140. package/dist/services/knowledge/kb-self.d.ts +91 -0
  141. package/dist/services/knowledge/kb-self.js +198 -0
  142. package/dist/services/knowledge/member-bible-view.d.ts +21 -0
  143. package/dist/services/knowledge/member-bible-view.js +194 -0
  144. package/dist/services/knowledge/pre-init.js +1 -0
  145. package/dist/services/knowledge/project-slug.js +2 -2
  146. package/dist/services/knowledge/require-sqlite-project.d.ts +32 -0
  147. package/dist/services/knowledge/require-sqlite-project.js +38 -0
  148. package/dist/services/knowledge/sqlite-provider.d.ts +64 -11
  149. package/dist/services/knowledge/sqlite-provider.js +303 -41
  150. package/dist/services/knowledge/types.d.ts +45 -2
  151. package/dist/services/member-access-secret.d.ts +70 -0
  152. package/dist/services/member-access-secret.js +195 -0
  153. package/dist/services/member-call-counts.d.ts +24 -0
  154. package/dist/services/member-call-counts.js +58 -0
  155. package/dist/services/member-config-io.d.ts +192 -0
  156. package/dist/services/member-config-io.js +443 -0
  157. package/dist/services/member-fleet-install.d.ts +710 -0
  158. package/dist/services/member-fleet-install.js +1895 -0
  159. package/dist/services/member-secret-env.d.ts +42 -0
  160. package/dist/services/member-secret-env.js +68 -0
  161. package/dist/services/member-tool-allowlist.d.ts +49 -0
  162. package/dist/services/member-tool-allowlist.js +111 -0
  163. package/dist/services/model-pricing.d.ts +19 -0
  164. package/dist/services/model-pricing.js +60 -10
  165. package/dist/services/port-holder.d.ts +86 -0
  166. package/dist/services/port-holder.js +351 -0
  167. package/dist/services/preflight-check.js +2 -0
  168. package/dist/services/registry.d.ts +13 -2
  169. package/dist/services/registry.js +27 -1
  170. package/dist/services/relay-strategy.d.ts +2 -0
  171. package/dist/services/relay-strategy.js +8 -0
  172. package/dist/services/secret-token.d.ts +38 -0
  173. package/dist/services/secret-token.js +51 -0
  174. package/dist/services/server-lifecycle.d.ts +26 -0
  175. package/dist/services/server-lifecycle.js +85 -0
  176. package/dist/services/service-manager/index.d.ts +7 -1
  177. package/dist/services/service-manager/index.js +17 -4
  178. package/dist/services/service-manager/linux.d.ts +1 -1
  179. package/dist/services/service-manager/linux.js +15 -7
  180. package/dist/services/service-manager/macos.d.ts +11 -1
  181. package/dist/services/service-manager/macos.js +45 -7
  182. package/dist/services/service-manager/service-path.d.ts +24 -0
  183. package/dist/services/service-manager/service-path.js +55 -0
  184. package/dist/services/service-manager/types.d.ts +39 -2
  185. package/dist/services/service-manager/types.js +29 -0
  186. package/dist/services/service-manager/windows.d.ts +194 -3
  187. package/dist/services/service-manager/windows.js +705 -30
  188. package/dist/services/service-notice.d.ts +14 -0
  189. package/dist/services/service-notice.js +62 -0
  190. package/dist/services/service-start-guard.d.ts +20 -0
  191. package/dist/services/service-start-guard.js +86 -0
  192. package/dist/services/session-cost.d.ts +24 -0
  193. package/dist/services/session-cost.js +105 -0
  194. package/dist/services/session-mcp-config.d.ts +83 -0
  195. package/dist/services/session-mcp-config.js +211 -0
  196. package/dist/services/sftp.d.ts +34 -0
  197. package/dist/services/sftp.js +304 -76
  198. package/dist/services/shell-probe.d.ts +0 -24
  199. package/dist/services/shell-probe.js +28 -3
  200. package/dist/services/singleton.d.ts +68 -2
  201. package/dist/services/singleton.js +168 -11
  202. package/dist/services/sprint-coordination.js +9 -3
  203. package/dist/services/ssh.d.ts +27 -0
  204. package/dist/services/ssh.js +287 -40
  205. package/dist/services/stall/find-log-file.js +12 -5
  206. package/dist/services/stall/index.d.ts +1 -1
  207. package/dist/services/stall/read-log-tail.d.ts +23 -0
  208. package/dist/services/stall/read-log-tail.js +53 -12
  209. package/dist/services/stall/stall-detector.d.ts +78 -5
  210. package/dist/services/stall/stall-detector.js +296 -39
  211. package/dist/services/stall/stall-poller.d.ts +16 -0
  212. package/dist/services/stall/stall-poller.js +50 -6
  213. package/dist/services/stopped-marker.d.ts +16 -0
  214. package/dist/services/stopped-marker.js +55 -0
  215. package/dist/services/strategy.d.ts +33 -1
  216. package/dist/services/strategy.js +165 -19
  217. package/dist/services/substitution-engine.js +2 -2
  218. package/dist/services/supervisor-service-cleanup.d.ts +52 -0
  219. package/dist/services/supervisor-service-cleanup.js +332 -0
  220. package/dist/services/tool-registry.d.ts +8 -1
  221. package/dist/services/tool-registry.js +164 -61
  222. package/dist/services/tool-scope.d.ts +58 -0
  223. package/dist/services/tool-scope.js +99 -0
  224. package/dist/services/vcs/azure-devops.d.ts +66 -1
  225. package/dist/services/vcs/azure-devops.js +192 -21
  226. package/dist/services/vcs/types.d.ts +1 -1
  227. package/dist/services/watch/project-resolver.js +1 -0
  228. package/dist/skills/auto-sprint-args/SKILL.md +2 -2
  229. package/dist/skills/fleet-sprint-cli/SKILL.md +48 -6
  230. package/dist/skills/pm/SKILL.md +4 -4
  231. package/dist/skills/pm/cost.md +0 -3
  232. package/dist/skills/pm/fleet-addendum.md +2 -2
  233. package/dist/skills/pm/index.md +4 -2
  234. package/dist/skills/pm/kb-reconcile.md +29 -14
  235. package/dist/skills/pm/kb-review.md +1 -0
  236. package/dist/skills/pm/sprint.md +5 -5
  237. package/dist/tools/check-status.d.ts +60 -9
  238. package/dist/tools/check-status.js +211 -52
  239. package/dist/tools/child-id-allocator.d.ts +2 -2
  240. package/dist/tools/child-id-allocator.js +1 -1
  241. package/dist/tools/code-index-heal.d.ts +8 -0
  242. package/dist/tools/code-index-heal.js +21 -0
  243. package/dist/tools/code-index-state.d.ts +45 -0
  244. package/dist/tools/code-index-state.js +115 -0
  245. package/dist/tools/code-intelligence-codebase-memory.js +6 -33
  246. package/dist/tools/code-intelligence-freshness.js +1 -1
  247. package/dist/tools/code-intelligence-gitnexus.d.ts +30 -0
  248. package/dist/tools/code-intelligence-gitnexus.js +333 -57
  249. package/dist/tools/code-intelligence-kb-enrich.js +7 -6
  250. package/dist/tools/code-intelligence-readiness.d.ts +99 -0
  251. package/dist/tools/code-intelligence-readiness.js +206 -0
  252. package/dist/tools/code-intelligence-reindex.d.ts +195 -0
  253. package/dist/tools/code-intelligence-reindex.js +530 -50
  254. package/dist/tools/code-intelligence-telemetry.d.ts +6 -1
  255. package/dist/tools/code-intelligence-telemetry.js +12 -3
  256. package/dist/tools/code-intelligence.d.ts +81 -31
  257. package/dist/tools/code-intelligence.js +156 -52
  258. package/dist/tools/compose-permissions.d.ts +63 -2
  259. package/dist/tools/compose-permissions.js +673 -72
  260. package/dist/tools/credential-store-set.js +1 -1
  261. package/dist/tools/execute-command.d.ts +11 -0
  262. package/dist/tools/execute-command.js +111 -32
  263. package/dist/tools/execute-prompt.d.ts +69 -12
  264. package/dist/tools/execute-prompt.js +1610 -895
  265. package/dist/tools/kb-bible-commit.d.ts +65 -0
  266. package/dist/tools/kb-bible-commit.js +239 -0
  267. package/dist/tools/kb-capture.d.ts +15 -11
  268. package/dist/tools/kb-capture.js +71 -16
  269. package/dist/tools/kb-context.d.ts +27 -7
  270. package/dist/tools/kb-context.js +60 -9
  271. package/dist/tools/kb-export.d.ts +98 -9
  272. package/dist/tools/kb-export.js +230 -92
  273. package/dist/tools/kb-feedback.d.ts +13 -9
  274. package/dist/tools/kb-feedback.js +16 -8
  275. package/dist/tools/kb-freshness-sweep.d.ts +11 -7
  276. package/dist/tools/kb-freshness-sweep.js +10 -8
  277. package/dist/tools/kb-harvest.d.ts +11 -7
  278. package/dist/tools/kb-harvest.js +7 -7
  279. package/dist/tools/kb-import.d.ts +26 -10
  280. package/dist/tools/kb-import.js +170 -159
  281. package/dist/tools/kb-invalidate.d.ts +17 -10
  282. package/dist/tools/kb-invalidate.js +41 -8
  283. package/dist/tools/kb-list.d.ts +14 -10
  284. package/dist/tools/kb-list.js +33 -11
  285. package/dist/tools/kb-promote.d.ts +11 -7
  286. package/dist/tools/kb-promote.js +14 -8
  287. package/dist/tools/kb-query.d.ts +21 -11
  288. package/dist/tools/kb-query.js +76 -13
  289. package/dist/tools/kb-reconcile-prefilter.d.ts +11 -7
  290. package/dist/tools/kb-reconcile-prefilter.js +13 -9
  291. package/dist/tools/kb-resolve-contradiction.d.ts +11 -7
  292. package/dist/tools/kb-resolve-contradiction.js +10 -8
  293. package/dist/tools/kb-session-prime.d.ts +14 -7
  294. package/dist/tools/kb-session-prime.js +44 -44
  295. package/dist/tools/kb-setup.d.ts +11 -4
  296. package/dist/tools/kb-setup.js +146 -27
  297. package/dist/tools/kb-stats.d.ts +11 -10
  298. package/dist/tools/kb-stats.js +84 -82
  299. package/dist/tools/list-members.d.ts +2 -2
  300. package/dist/tools/list-members.js +6 -2
  301. package/dist/tools/member-detail.d.ts +5 -2
  302. package/dist/tools/member-detail.js +105 -17
  303. package/dist/tools/member-reservation.d.ts +60 -3
  304. package/dist/tools/member-reservation.js +61 -20
  305. package/dist/tools/provision-auth.d.ts +118 -1
  306. package/dist/tools/provision-auth.js +402 -92
  307. package/dist/tools/provision-vcs-auth.d.ts +93 -3
  308. package/dist/tools/provision-vcs-auth.js +209 -49
  309. package/dist/tools/receive-files.js +3 -1
  310. package/dist/tools/register-member.d.ts +44 -15
  311. package/dist/tools/register-member.js +175 -70
  312. package/dist/tools/remove-member.js +118 -15
  313. package/dist/tools/send-email.d.ts +2 -2
  314. package/dist/tools/send-files.js +6 -4
  315. package/dist/tools/session-stats.d.ts +17 -0
  316. package/dist/tools/session-stats.js +27 -0
  317. package/dist/tools/setup-git-app.js +11 -7
  318. package/dist/tools/shutdown-server.d.ts +2 -0
  319. package/dist/tools/shutdown-server.js +28 -1
  320. package/dist/tools/update-agent-cli.d.ts +19 -0
  321. package/dist/tools/update-agent-cli.js +59 -2
  322. package/dist/tools/update-member.d.ts +23 -17
  323. package/dist/tools/update-member.js +181 -18
  324. package/dist/tools/vcs-credential-exec.d.ts +127 -0
  325. package/dist/tools/vcs-credential-exec.js +203 -0
  326. package/dist/types.d.ts +111 -1
  327. package/dist/utils/agent-helpers.js +2 -0
  328. package/dist/utils/auth-env.d.ts +15 -4
  329. package/dist/utils/auth-env.js +52 -23
  330. package/dist/utils/find-on-path.d.ts +37 -0
  331. package/dist/utils/find-on-path.js +85 -0
  332. package/dist/utils/log-helpers.d.ts +37 -0
  333. package/dist/utils/log-helpers.js +108 -22
  334. package/dist/utils/owner-only-fs.d.ts +56 -0
  335. package/dist/utils/owner-only-fs.js +98 -0
  336. package/dist/utils/process-utils.d.ts +28 -0
  337. package/dist/utils/process-utils.js +68 -0
  338. package/dist/utils/prompt-errors.js +11 -2
  339. package/dist/utils/session-caller.d.ts +4 -2
  340. package/dist/utils/session-caller.js +3 -2
  341. package/dist/utils/shell-escape.d.ts +58 -3
  342. package/dist/utils/shell-escape.js +91 -5
  343. package/dist/utils/ssh-error-messages.js +3 -0
  344. package/dist/utils/vcs-provider-detect.d.ts +65 -0
  345. package/dist/utils/vcs-provider-detect.js +109 -0
  346. package/dist/utils/workspace-trust.d.ts +26 -0
  347. package/dist/utils/workspace-trust.js +91 -1
  348. package/dist/workflows/auto-sprint.js +397 -56
  349. package/package.json +15 -10
  350. package/packages/apra-fleet-client/package.json +2 -2
  351. package/packages/apra-fleet-client/src/client/api.mjs +864 -31
  352. package/packages/apra-fleet-client/src/client/auto-start.mjs +434 -0
  353. package/packages/apra-fleet-client/src/client/client.mjs +9 -0
  354. package/packages/apra-fleet-client/src/client/errors.mjs +6 -2
  355. package/packages/apra-fleet-client/src/client/factory.mjs +23 -4
  356. package/packages/apra-fleet-client/src/client/reconnecting-transport.mjs +137 -0
  357. package/packages/apra-fleet-client/src/client/server-resolution.mjs +267 -30
  358. package/packages/apra-fleet-client/src/client/transport.mjs +37 -3
  359. package/packages/apra-fleet-se/apra-pm/agents/_shared/GRAPH-SEMANTICS.md +10 -2
  360. package/packages/apra-fleet-se/apra-pm/agents/backlog-groomer.md +87 -14
  361. package/packages/apra-fleet-se/apra-pm/agents/ci-watcher.md +35 -9
  362. package/packages/apra-fleet-se/apra-pm/agents/deployer.md +63 -11
  363. package/packages/apra-fleet-se/apra-pm/agents/doer.md +99 -41
  364. package/packages/apra-fleet-se/apra-pm/agents/harvester.md +47 -27
  365. package/packages/apra-fleet-se/apra-pm/agents/integ-test-runner.md +38 -10
  366. package/packages/apra-fleet-se/apra-pm/agents/kb-reconciler.md +37 -7
  367. package/packages/apra-fleet-se/apra-pm/agents/plan-reviewer.md +145 -32
  368. package/packages/apra-fleet-se/apra-pm/agents/planner.md +180 -69
  369. package/packages/apra-fleet-se/apra-pm/agents/regression-test-runner.md +98 -64
  370. package/packages/apra-fleet-se/apra-pm/agents/reviewer.md +117 -45
  371. package/packages/apra-fleet-se/apra-pm/agents/schemas/backlog-groomer-output.json +1 -1
  372. package/packages/apra-fleet-se/apra-pm/agents/schemas/doer-output.json +32 -0
  373. package/packages/apra-fleet-se/apra-pm/agents/schemas/harvester-input.json +1 -5
  374. package/packages/apra-fleet-se/apra-pm/agents/schemas/plan-reviewer-output.json +37 -0
  375. package/packages/apra-fleet-se/apra-pm/agents/schemas/planner-output.json +1 -1
  376. package/packages/apra-fleet-se/apra-pm/agents/schemas/regression-test-runner-output.json +41 -7
  377. package/packages/apra-fleet-se/apra-pm/agents/schemas/reviewer-output.json +62 -3
  378. package/packages/apra-fleet-se/apra-pm/skills/pm/SKILL.md +4 -4
  379. package/packages/apra-fleet-se/apra-pm/skills/pm/cost.md +0 -3
  380. package/packages/apra-fleet-se/apra-pm/skills/pm/fleet-addendum.md +2 -2
  381. package/packages/apra-fleet-se/apra-pm/skills/pm/index.md +4 -2
  382. package/packages/apra-fleet-se/apra-pm/skills/pm/kb-reconcile.md +29 -14
  383. package/packages/apra-fleet-se/apra-pm/skills/pm/kb-review.md +1 -0
  384. package/packages/apra-fleet-se/apra-pm/skills/pm/sprint.md +5 -5
  385. package/packages/apra-fleet-se/bin/cli.mjs +326 -48
  386. package/packages/apra-fleet-se/bin/serve.mjs +218 -11
  387. package/packages/apra-fleet-se/fleet-sprint/abort.mjs +517 -0
  388. package/packages/apra-fleet-se/fleet-sprint/backlog-role.mjs +93 -0
  389. package/packages/apra-fleet-se/fleet-sprint/beads-children.mjs +646 -0
  390. package/packages/apra-fleet-se/fleet-sprint/beads-identity-check.mjs +825 -0
  391. package/packages/apra-fleet-se/fleet-sprint/beads-identity.mjs +217 -0
  392. package/packages/apra-fleet-se/fleet-sprint/beads-memory-hygiene.mjs +201 -0
  393. package/packages/apra-fleet-se/fleet-sprint/beads-scope.mjs +745 -0
  394. package/packages/apra-fleet-se/fleet-sprint/beads-transitions.mjs +447 -0
  395. package/packages/apra-fleet-se/fleet-sprint/branch-ensure.mjs +96 -0
  396. package/packages/apra-fleet-se/fleet-sprint/ci-gate.mjs +353 -0
  397. package/packages/apra-fleet-se/fleet-sprint/conflict-ladder.mjs +20 -0
  398. package/packages/apra-fleet-se/fleet-sprint/contracts.mjs +121 -15
  399. package/packages/apra-fleet-se/fleet-sprint/coordination.mjs +539 -0
  400. package/packages/apra-fleet-se/fleet-sprint/dispatch-accounting.mjs +270 -0
  401. package/packages/apra-fleet-se/fleet-sprint/dispatch-failure.mjs +204 -0
  402. package/packages/apra-fleet-se/fleet-sprint/dispatch-role.mjs +1310 -0
  403. package/packages/apra-fleet-se/fleet-sprint/dispatch-safety-guard.mjs +216 -4
  404. package/packages/apra-fleet-se/fleet-sprint/docs/README.md +53 -15
  405. package/packages/apra-fleet-se/fleet-sprint/docs/dolt-sync-redesign.md +281 -0
  406. package/packages/apra-fleet-se/fleet-sprint/docs/escalate-to-llm-design.md +1 -1
  407. package/packages/apra-fleet-se/fleet-sprint/docs/fleet-sprint-diagram.md +3 -3
  408. package/packages/apra-fleet-se/fleet-sprint/docs/run-sprint-cycle-prelude-decision.md +215 -0
  409. package/packages/apra-fleet-se/fleet-sprint/dolt-literal-guard.mjs +56 -1
  410. package/packages/apra-fleet-se/fleet-sprint/dolt-sync.mjs +1327 -44
  411. package/packages/apra-fleet-se/fleet-sprint/errors.mjs +470 -4
  412. package/packages/apra-fleet-se/fleet-sprint/explicit-id-create-guard.mjs +501 -0
  413. package/packages/apra-fleet-se/fleet-sprint/fatal-diagnostics.mjs +134 -0
  414. package/packages/apra-fleet-se/fleet-sprint/full-db-fetch-guard.mjs +102 -0
  415. package/packages/apra-fleet-se/fleet-sprint/git-sync.mjs +747 -0
  416. package/packages/apra-fleet-se/fleet-sprint/git-topology.mjs +543 -0
  417. package/packages/apra-fleet-se/fleet-sprint/guarded-modules.mjs +613 -0
  418. package/packages/apra-fleet-se/fleet-sprint/inline-ladder-guard.mjs +216 -0
  419. package/packages/apra-fleet-se/fleet-sprint/kb-hints.mjs +181 -0
  420. package/packages/apra-fleet-se/fleet-sprint/kb-injection.mjs +64 -0
  421. package/packages/apra-fleet-se/fleet-sprint/kb-maintainer.mjs +298 -0
  422. package/packages/apra-fleet-se/fleet-sprint/kb.mjs +1684 -0
  423. package/packages/apra-fleet-se/fleet-sprint/lower-quality.mjs +39 -0
  424. package/packages/apra-fleet-se/fleet-sprint/mcp-result.mjs +75 -0
  425. package/packages/apra-fleet-se/fleet-sprint/member-align.mjs +391 -0
  426. package/packages/apra-fleet-se/fleet-sprint/member-call.mjs +374 -0
  427. package/packages/apra-fleet-se/fleet-sprint/member-init-probe.mjs +644 -0
  428. package/packages/apra-fleet-se/fleet-sprint/member-provisioning.mjs +699 -0
  429. package/packages/apra-fleet-se/fleet-sprint/member-sync.mjs +802 -0
  430. package/packages/apra-fleet-se/fleet-sprint/member-target.mjs +93 -0
  431. package/packages/apra-fleet-se/fleet-sprint/newtask-text.mjs +266 -0
  432. package/packages/apra-fleet-se/fleet-sprint/orphan-reclaim.mjs +153 -0
  433. package/packages/apra-fleet-se/fleet-sprint/parent-notes-staleness.mjs +201 -0
  434. package/packages/apra-fleet-se/fleet-sprint/phases/deploy.mjs +218 -0
  435. package/packages/apra-fleet-se/fleet-sprint/phases/develop.mjs +869 -0
  436. package/packages/apra-fleet-se/fleet-sprint/phases/ensure-sprint-branch.mjs +295 -0
  437. package/packages/apra-fleet-se/fleet-sprint/phases/final-review.mjs +466 -0
  438. package/packages/apra-fleet-se/fleet-sprint/phases/harvest.mjs +212 -0
  439. package/packages/apra-fleet-se/fleet-sprint/phases/integ-test.mjs +358 -0
  440. package/packages/apra-fleet-se/fleet-sprint/phases/plan.mjs +395 -0
  441. package/packages/apra-fleet-se/fleet-sprint/phases/publish-pr.mjs +345 -0
  442. package/packages/apra-fleet-se/fleet-sprint/phases/re-review.mjs +264 -0
  443. package/packages/apra-fleet-se/fleet-sprint/phases/regression-test.mjs +179 -0
  444. package/packages/apra-fleet-se/fleet-sprint/phases/replan.mjs +230 -0
  445. package/packages/apra-fleet-se/fleet-sprint/phases/review.mjs +365 -0
  446. package/packages/apra-fleet-se/fleet-sprint/pr-body.mjs +259 -0
  447. package/packages/apra-fleet-se/fleet-sprint/prompts.mjs +617 -0
  448. package/packages/apra-fleet-se/fleet-sprint/role-policies.mjs +1434 -0
  449. package/packages/apra-fleet-se/fleet-sprint/round-session.mjs +100 -0
  450. package/packages/apra-fleet-se/fleet-sprint/runner.js +2289 -9928
  451. package/packages/apra-fleet-se/fleet-sprint/se-os-commands.mjs +24 -0
  452. package/packages/apra-fleet-se/fleet-sprint/se-posix.mjs +147 -12
  453. package/packages/apra-fleet-se/fleet-sprint/se-windows.mjs +111 -0
  454. package/packages/apra-fleet-se/fleet-sprint/shell-command-guard.mjs +347 -0
  455. package/packages/apra-fleet-se/fleet-sprint/skills/fleet-sprint-cli/SKILL.md +48 -6
  456. package/packages/apra-fleet-se/fleet-sprint/skills/fleet-supervisor/SKILL.md +98 -25
  457. package/packages/apra-fleet-se/fleet-sprint/sprint-args.mjs +590 -0
  458. package/packages/apra-fleet-se/fleet-sprint/sprint-report.mjs +339 -0
  459. package/packages/apra-fleet-se/fleet-sprint/sprint-state.mjs +185 -0
  460. package/packages/apra-fleet-se/fleet-sprint/unbracketed-push-guard.mjs +234 -0
  461. package/packages/apra-fleet-se/fleet-sprint/usage-limit-controller.mjs +218 -0
  462. package/packages/apra-fleet-se/fleet-sprint/vcs-auth.mjs +1445 -0
  463. package/packages/apra-fleet-se/fleet-sprint/vcs-module.mjs +284 -23
  464. package/packages/apra-fleet-se/fleet-sprint/vcs-providers/azure-devops.mjs +170 -21
  465. package/packages/apra-fleet-se/fleet-sprint/vcs-providers/bitbucket.mjs +28 -0
  466. package/packages/apra-fleet-se/fleet-sprint/vcs-providers/github.mjs +354 -0
  467. package/packages/apra-fleet-se/fleet-sprint/vcs-providers/index.mjs +189 -16
  468. package/packages/apra-fleet-se/fleet-sprint/vcs-providers/shell-helpers.mjs +9 -4
  469. package/packages/apra-fleet-se/fleet-sprint/verify-block.mjs +152 -0
  470. package/packages/apra-fleet-se/fleet-sprint/viewer-extensions.mjs +311 -22
  471. package/packages/apra-fleet-se/fleet-sprint/worklists.mjs +559 -0
  472. package/packages/apra-fleet-se/package.json +2 -2
  473. package/packages/apra-fleet-se/src/supervisor/api.mjs +919 -0
  474. package/packages/apra-fleet-se/src/supervisor/backlog-member.mjs +332 -0
  475. package/packages/apra-fleet-se/src/supervisor/backlog.mjs +982 -0
  476. package/packages/apra-fleet-se/src/supervisor/beads-identity.mjs +246 -0
  477. package/packages/apra-fleet-se/src/supervisor/beads-view.mjs +438 -0
  478. package/packages/apra-fleet-se/src/supervisor/child-port.mjs +36 -0
  479. package/packages/apra-fleet-se/src/supervisor/dashboard.mjs +1659 -0
  480. package/packages/apra-fleet-se/src/supervisor/dolt-mutex.mjs +350 -0
  481. package/packages/apra-fleet-se/src/supervisor/dolt-orphan-sweep.mjs +338 -0
  482. package/packages/apra-fleet-se/src/supervisor/fleet-members.mjs +279 -0
  483. package/packages/apra-fleet-se/src/supervisor/history-view.mjs +276 -0
  484. package/packages/apra-fleet-se/src/supervisor/history.mjs +328 -0
  485. package/packages/apra-fleet-se/src/supervisor/id-allocator.mjs +521 -0
  486. package/packages/apra-fleet-se/src/supervisor/launch-form.mjs +444 -0
  487. package/packages/apra-fleet-se/src/supervisor/ledger.mjs +629 -0
  488. package/packages/apra-fleet-se/src/supervisor/lib/exec-bd.mjs +303 -0
  489. package/packages/apra-fleet-se/src/supervisor/log-timestamp.mjs +51 -0
  490. package/packages/apra-fleet-se/src/supervisor/log-view.mjs +159 -0
  491. package/packages/apra-fleet-se/src/supervisor/proxy.mjs +474 -0
  492. package/packages/apra-fleet-se/src/supervisor/readopt.mjs +136 -0
  493. package/packages/apra-fleet-se/src/supervisor/reconcile.mjs +263 -0
  494. package/packages/apra-fleet-se/src/supervisor/rename-with-retry.mjs +87 -0
  495. package/packages/apra-fleet-se/src/supervisor/scope-overlap.mjs +327 -0
  496. package/packages/apra-fleet-se/src/supervisor/self-log.mjs +133 -0
  497. package/packages/apra-fleet-se/src/supervisor/server.mjs +399 -0
  498. package/packages/apra-fleet-se/src/supervisor/spawner.mjs +542 -0
  499. package/packages/apra-fleet-se/src/supervisor/sprint-identity.mjs +96 -0
  500. package/packages/apra-fleet-se/src/supervisor/watchdog.mjs +987 -0
  501. package/packages/apra-fleet-workflow/package.json +1 -1
  502. package/packages/apra-fleet-workflow/src/viewer/index.mjs +81 -15
  503. package/packages/apra-fleet-workflow/src/viewer/run-summary.mjs +127 -0
  504. package/packages/apra-fleet-workflow/src/workflow/index.mjs +118 -10
  505. package/packages/apra-fleet-workflow/src/workflow/pricing.mjs +75 -25
  506. package/skills/fleet/SKILL.md +8 -8
  507. package/skills/fleet/auth-azdevops.md +14 -3
  508. package/skills/fleet/auth-bitbucket.md +2 -2
  509. package/skills/fleet/auth-github.md +5 -4
  510. package/skills/fleet/onboarding.md +3 -3
  511. package/skills/fleet/permissions.md +14 -1
  512. package/skills/fleet/profiles/node.json +1 -1
  513. package/skills/fleet/profiles/tag-kb-reconciler.json +2 -6
  514. package/skills/fleet/troubleshooting.md +3 -3
  515. package/version.json +1 -1
  516. package/dist/cli/agent-transform.d.ts.map +0 -1
  517. package/dist/cli/agent-transform.js.map +0 -1
  518. package/dist/cli/auth.d.ts.map +0 -1
  519. package/dist/cli/auth.js.map +0 -1
  520. package/dist/cli/config.d.ts.map +0 -1
  521. package/dist/cli/config.js.map +0 -1
  522. package/dist/cli/dolt-install.d.ts.map +0 -1
  523. package/dist/cli/dolt-install.js.map +0 -1
  524. package/dist/cli/install-guard.d.ts.map +0 -1
  525. package/dist/cli/install-guard.js.map +0 -1
  526. package/dist/cli/install.d.ts.map +0 -1
  527. package/dist/cli/install.js.map +0 -1
  528. package/dist/cli/join.d.ts.map +0 -1
  529. package/dist/cli/join.js.map +0 -1
  530. package/dist/cli/kb-commit.d.ts.map +0 -1
  531. package/dist/cli/kb-commit.js.map +0 -1
  532. package/dist/cli/kb-directives.d.ts.map +0 -1
  533. package/dist/cli/kb-directives.js.map +0 -1
  534. package/dist/cli/kb-import.d.ts.map +0 -1
  535. package/dist/cli/kb-import.js.map +0 -1
  536. package/dist/cli/register-member.d.ts.map +0 -1
  537. package/dist/cli/register-member.js.map +0 -1
  538. package/dist/cli/restart.d.ts.map +0 -1
  539. package/dist/cli/restart.js.map +0 -1
  540. package/dist/cli/secret.d.ts.map +0 -1
  541. package/dist/cli/secret.js.map +0 -1
  542. package/dist/cli/spoke.d.ts.map +0 -1
  543. package/dist/cli/spoke.js.map +0 -1
  544. package/dist/cli/start.d.ts.map +0 -1
  545. package/dist/cli/start.js.map +0 -1
  546. package/dist/cli/status.d.ts.map +0 -1
  547. package/dist/cli/status.js.map +0 -1
  548. package/dist/cli/stop.d.ts.map +0 -1
  549. package/dist/cli/stop.js.map +0 -1
  550. package/dist/cli/uninstall.d.ts.map +0 -1
  551. package/dist/cli/uninstall.js.map +0 -1
  552. package/dist/cli/update.d.ts.map +0 -1
  553. package/dist/cli/update.js.map +0 -1
  554. package/dist/cli/watch.d.ts.map +0 -1
  555. package/dist/cli/watch.js.map +0 -1
  556. package/dist/cli/workflow-assets.d.ts.map +0 -1
  557. package/dist/cli/workflow-assets.js.map +0 -1
  558. package/dist/cli/workflow.d.ts.map +0 -1
  559. package/dist/cli/workflow.js.map +0 -1
  560. package/dist/commands/kb-server.d.ts.map +0 -1
  561. package/dist/commands/kb-server.js.map +0 -1
  562. package/dist/delivery-mode.d.ts.map +0 -1
  563. package/dist/delivery-mode.js.map +0 -1
  564. package/dist/hub-service/activity.d.ts.map +0 -1
  565. package/dist/hub-service/activity.js.map +0 -1
  566. package/dist/hub-service/audit-log.d.ts.map +0 -1
  567. package/dist/hub-service/audit-log.js.map +0 -1
  568. package/dist/hub-service/db/pool.d.ts.map +0 -1
  569. package/dist/hub-service/db/pool.js.map +0 -1
  570. package/dist/hub-service/enrollment.d.ts.map +0 -1
  571. package/dist/hub-service/enrollment.js.map +0 -1
  572. package/dist/hub-service/envelope-routes.d.ts.map +0 -1
  573. package/dist/hub-service/envelope-routes.js.map +0 -1
  574. package/dist/hub-service/handlers/installers.d.ts.map +0 -1
  575. package/dist/hub-service/handlers/installers.js.map +0 -1
  576. package/dist/hub-service/http-server.d.ts.map +0 -1
  577. package/dist/hub-service/http-server.js.map +0 -1
  578. package/dist/hub-service/hub-jwt.d.ts.map +0 -1
  579. package/dist/hub-service/hub-jwt.js.map +0 -1
  580. package/dist/hub-service/jwt-revocation.d.ts.map +0 -1
  581. package/dist/hub-service/jwt-revocation.js.map +0 -1
  582. package/dist/hub-service/machines.d.ts.map +0 -1
  583. package/dist/hub-service/machines.js.map +0 -1
  584. package/dist/hub-service/main.d.ts.map +0 -1
  585. package/dist/hub-service/main.js.map +0 -1
  586. package/dist/hub-service/member-tokens.d.ts.map +0 -1
  587. package/dist/hub-service/member-tokens.js.map +0 -1
  588. package/dist/hub-service/member-view.d.ts.map +0 -1
  589. package/dist/hub-service/member-view.js.map +0 -1
  590. package/dist/hub-service/members.d.ts.map +0 -1
  591. package/dist/hub-service/members.js.map +0 -1
  592. package/dist/hub-service/presence.d.ts.map +0 -1
  593. package/dist/hub-service/presence.js.map +0 -1
  594. package/dist/hub-service/project-view.d.ts.map +0 -1
  595. package/dist/hub-service/project-view.js.map +0 -1
  596. package/dist/hub-service/projects.d.ts.map +0 -1
  597. package/dist/hub-service/projects.js.map +0 -1
  598. package/dist/hub-service/relay-queue.d.ts.map +0 -1
  599. package/dist/hub-service/relay-queue.js.map +0 -1
  600. package/dist/hub-service/session-jwt.d.ts.map +0 -1
  601. package/dist/hub-service/session-jwt.js.map +0 -1
  602. package/dist/hub-service/usage.d.ts.map +0 -1
  603. package/dist/hub-service/usage.js.map +0 -1
  604. package/dist/hub-service/users.d.ts.map +0 -1
  605. package/dist/hub-service/users.js.map +0 -1
  606. package/dist/hub-service/workspaces.d.ts.map +0 -1
  607. package/dist/hub-service/workspaces.js.map +0 -1
  608. package/dist/index.d.ts.map +0 -1
  609. package/dist/index.js.map +0 -1
  610. package/dist/onboarding/text.d.ts.map +0 -1
  611. package/dist/onboarding/text.js.map +0 -1
  612. package/dist/os/git-bash-candidates.d.ts.map +0 -1
  613. package/dist/os/git-bash-candidates.js.map +0 -1
  614. package/dist/os/index.d.ts.map +0 -1
  615. package/dist/os/index.js.map +0 -1
  616. package/dist/os/linux.d.ts.map +0 -1
  617. package/dist/os/linux.js.map +0 -1
  618. package/dist/os/macos.d.ts.map +0 -1
  619. package/dist/os/macos.js.map +0 -1
  620. package/dist/os/os-commands.d.ts.map +0 -1
  621. package/dist/os/os-commands.js.map +0 -1
  622. package/dist/os/windows-gitbash.d.ts.map +0 -1
  623. package/dist/os/windows-gitbash.js.map +0 -1
  624. package/dist/os/windows-wrapper.d.ts.map +0 -1
  625. package/dist/os/windows-wrapper.js.map +0 -1
  626. package/dist/os/windows.d.ts.map +0 -1
  627. package/dist/os/windows.js.map +0 -1
  628. package/dist/paths.d.ts.map +0 -1
  629. package/dist/paths.js.map +0 -1
  630. package/dist/providers/agy.d.ts.map +0 -1
  631. package/dist/providers/agy.js.map +0 -1
  632. package/dist/providers/claude.d.ts.map +0 -1
  633. package/dist/providers/claude.js.map +0 -1
  634. package/dist/providers/codex.d.ts.map +0 -1
  635. package/dist/providers/codex.js.map +0 -1
  636. package/dist/providers/copilot.d.ts.map +0 -1
  637. package/dist/providers/copilot.js.map +0 -1
  638. package/dist/providers/email/headers.d.ts.map +0 -1
  639. package/dist/providers/email/headers.js.map +0 -1
  640. package/dist/providers/email/index.d.ts.map +0 -1
  641. package/dist/providers/email/index.js.map +0 -1
  642. package/dist/providers/email/provider.d.ts.map +0 -1
  643. package/dist/providers/email/provider.js.map +0 -1
  644. package/dist/providers/email/sendgrid.d.ts.map +0 -1
  645. package/dist/providers/email/sendgrid.js.map +0 -1
  646. package/dist/providers/email/smtp.d.ts.map +0 -1
  647. package/dist/providers/email/smtp.js.map +0 -1
  648. package/dist/providers/index.d.ts.map +0 -1
  649. package/dist/providers/index.js.map +0 -1
  650. package/dist/providers/none.d.ts.map +0 -1
  651. package/dist/providers/none.js.map +0 -1
  652. package/dist/providers/opencode.d.ts.map +0 -1
  653. package/dist/providers/opencode.js.map +0 -1
  654. package/dist/providers/provider.d.ts.map +0 -1
  655. package/dist/providers/provider.js.map +0 -1
  656. package/dist/services/agent-provisioner.d.ts.map +0 -1
  657. package/dist/services/agent-provisioner.js.map +0 -1
  658. package/dist/services/auth-socket.d.ts.map +0 -1
  659. package/dist/services/auth-socket.js.map +0 -1
  660. package/dist/services/auth-web.d.ts.map +0 -1
  661. package/dist/services/auth-web.js.map +0 -1
  662. package/dist/services/budget-awareness.d.ts.map +0 -1
  663. package/dist/services/budget-awareness.js.map +0 -1
  664. package/dist/services/cloud/activity.d.ts.map +0 -1
  665. package/dist/services/cloud/activity.js.map +0 -1
  666. package/dist/services/cloud/aws.d.ts.map +0 -1
  667. package/dist/services/cloud/aws.js.map +0 -1
  668. package/dist/services/cloud/cost.d.ts.map +0 -1
  669. package/dist/services/cloud/cost.js.map +0 -1
  670. package/dist/services/cloud/idle-manager.d.ts.map +0 -1
  671. package/dist/services/cloud/idle-manager.js.map +0 -1
  672. package/dist/services/cloud/lifecycle.d.ts.map +0 -1
  673. package/dist/services/cloud/lifecycle.js.map +0 -1
  674. package/dist/services/cloud/task-wrapper.d.ts.map +0 -1
  675. package/dist/services/cloud/task-wrapper.js.map +0 -1
  676. package/dist/services/cloud/types.d.ts.map +0 -1
  677. package/dist/services/cloud/types.js.map +0 -1
  678. package/dist/services/cloud-sync.d.ts.map +0 -1
  679. package/dist/services/cloud-sync.js.map +0 -1
  680. package/dist/services/context-admission.d.ts.map +0 -1
  681. package/dist/services/context-admission.js.map +0 -1
  682. package/dist/services/credential-cleanup.d.ts.map +0 -1
  683. package/dist/services/credential-cleanup.js.map +0 -1
  684. package/dist/services/credential-store.d.ts.map +0 -1
  685. package/dist/services/credential-store.js.map +0 -1
  686. package/dist/services/event-bus.d.ts.map +0 -1
  687. package/dist/services/event-bus.js.map +0 -1
  688. package/dist/services/file-chunker.d.ts.map +0 -1
  689. package/dist/services/file-chunker.js.map +0 -1
  690. package/dist/services/file-transfer-relay.d.ts.map +0 -1
  691. package/dist/services/file-transfer-relay.js.map +0 -1
  692. package/dist/services/file-transfer.d.ts.map +0 -1
  693. package/dist/services/file-transfer.js.map +0 -1
  694. package/dist/services/git-config.d.ts.map +0 -1
  695. package/dist/services/git-config.js.map +0 -1
  696. package/dist/services/github-app.d.ts.map +0 -1
  697. package/dist/services/github-app.js.map +0 -1
  698. package/dist/services/http-transport.d.ts.map +0 -1
  699. package/dist/services/http-transport.js.map +0 -1
  700. package/dist/services/hub-client.d.ts.map +0 -1
  701. package/dist/services/hub-client.js.map +0 -1
  702. package/dist/services/icons.d.ts.map +0 -1
  703. package/dist/services/icons.js.map +0 -1
  704. package/dist/services/jwt.d.ts.map +0 -1
  705. package/dist/services/jwt.js.map +0 -1
  706. package/dist/services/knowledge/audn.d.ts.map +0 -1
  707. package/dist/services/knowledge/audn.js.map +0 -1
  708. package/dist/services/knowledge/file-hash.d.ts.map +0 -1
  709. package/dist/services/knowledge/file-hash.js.map +0 -1
  710. package/dist/services/knowledge/http-provider.d.ts.map +0 -1
  711. package/dist/services/knowledge/http-provider.js.map +0 -1
  712. package/dist/services/knowledge/kb-providers.d.ts.map +0 -1
  713. package/dist/services/knowledge/kb-providers.js.map +0 -1
  714. package/dist/services/knowledge/kb-scope-input.d.ts +0 -5
  715. package/dist/services/knowledge/kb-scope-input.d.ts.map +0 -1
  716. package/dist/services/knowledge/kb-scope-input.js +0 -11
  717. package/dist/services/knowledge/kb-scope-input.js.map +0 -1
  718. package/dist/services/knowledge/kb-service.d.ts.map +0 -1
  719. package/dist/services/knowledge/kb-service.js.map +0 -1
  720. package/dist/services/knowledge/path-validation.d.ts.map +0 -1
  721. package/dist/services/knowledge/path-validation.js.map +0 -1
  722. package/dist/services/knowledge/pre-init.d.ts.map +0 -1
  723. package/dist/services/knowledge/pre-init.js.map +0 -1
  724. package/dist/services/knowledge/project-slug.d.ts.map +0 -1
  725. package/dist/services/knowledge/project-slug.js.map +0 -1
  726. package/dist/services/knowledge/sqlite-provider.d.ts.map +0 -1
  727. package/dist/services/knowledge/sqlite-provider.js.map +0 -1
  728. package/dist/services/knowledge/types.d.ts.map +0 -1
  729. package/dist/services/knowledge/types.js.map +0 -1
  730. package/dist/services/known-hosts.d.ts.map +0 -1
  731. package/dist/services/known-hosts.js.map +0 -1
  732. package/dist/services/known-sessions.d.ts.map +0 -1
  733. package/dist/services/known-sessions.js.map +0 -1
  734. package/dist/services/member-home.d.ts.map +0 -1
  735. package/dist/services/member-home.js.map +0 -1
  736. package/dist/services/member-remote-url.d.ts.map +0 -1
  737. package/dist/services/member-remote-url.js.map +0 -1
  738. package/dist/services/model-pricing.d.ts.map +0 -1
  739. package/dist/services/model-pricing.js.map +0 -1
  740. package/dist/services/onboarding.d.ts.map +0 -1
  741. package/dist/services/onboarding.js.map +0 -1
  742. package/dist/services/orphan-recovery.d.ts.map +0 -1
  743. package/dist/services/orphan-recovery.js.map +0 -1
  744. package/dist/services/pending-responses.d.ts.map +0 -1
  745. package/dist/services/pending-responses.js.map +0 -1
  746. package/dist/services/preflight-check.d.ts.map +0 -1
  747. package/dist/services/preflight-check.js.map +0 -1
  748. package/dist/services/registry.d.ts.map +0 -1
  749. package/dist/services/registry.js.map +0 -1
  750. package/dist/services/relay-context.d.ts.map +0 -1
  751. package/dist/services/relay-context.js.map +0 -1
  752. package/dist/services/relay-executor.d.ts.map +0 -1
  753. package/dist/services/relay-executor.js.map +0 -1
  754. package/dist/services/relay-request.d.ts.map +0 -1
  755. package/dist/services/relay-request.js.map +0 -1
  756. package/dist/services/relay-strategy.d.ts.map +0 -1
  757. package/dist/services/relay-strategy.js.map +0 -1
  758. package/dist/services/service-manager/index.d.ts.map +0 -1
  759. package/dist/services/service-manager/index.js.map +0 -1
  760. package/dist/services/service-manager/linux.d.ts.map +0 -1
  761. package/dist/services/service-manager/linux.js.map +0 -1
  762. package/dist/services/service-manager/macos.d.ts.map +0 -1
  763. package/dist/services/service-manager/macos.js.map +0 -1
  764. package/dist/services/service-manager/types.d.ts.map +0 -1
  765. package/dist/services/service-manager/types.js.map +0 -1
  766. package/dist/services/service-manager/windows.d.ts.map +0 -1
  767. package/dist/services/service-manager/windows.js.map +0 -1
  768. package/dist/services/session-registry.d.ts.map +0 -1
  769. package/dist/services/session-registry.js.map +0 -1
  770. package/dist/services/sftp.d.ts.map +0 -1
  771. package/dist/services/sftp.js.map +0 -1
  772. package/dist/services/shell-probe.d.ts.map +0 -1
  773. package/dist/services/shell-probe.js.map +0 -1
  774. package/dist/services/singleton.d.ts.map +0 -1
  775. package/dist/services/singleton.js.map +0 -1
  776. package/dist/services/sprint-coordination.d.ts.map +0 -1
  777. package/dist/services/sprint-coordination.js.map +0 -1
  778. package/dist/services/ssh.d.ts.map +0 -1
  779. package/dist/services/ssh.js.map +0 -1
  780. package/dist/services/stall/find-log-file.d.ts.map +0 -1
  781. package/dist/services/stall/find-log-file.js.map +0 -1
  782. package/dist/services/stall/index.d.ts.map +0 -1
  783. package/dist/services/stall/index.js.map +0 -1
  784. package/dist/services/stall/log-path-resolver.d.ts.map +0 -1
  785. package/dist/services/stall/log-path-resolver.js.map +0 -1
  786. package/dist/services/stall/read-log-tail.d.ts.map +0 -1
  787. package/dist/services/stall/read-log-tail.js.map +0 -1
  788. package/dist/services/stall/stall-detector.d.ts.map +0 -1
  789. package/dist/services/stall/stall-detector.js.map +0 -1
  790. package/dist/services/stall/stall-poller.d.ts.map +0 -1
  791. package/dist/services/stall/stall-poller.js.map +0 -1
  792. package/dist/services/stall/time-utils.d.ts.map +0 -1
  793. package/dist/services/stall/time-utils.js.map +0 -1
  794. package/dist/services/statusline.d.ts.map +0 -1
  795. package/dist/services/statusline.js.map +0 -1
  796. package/dist/services/strategy.d.ts.map +0 -1
  797. package/dist/services/strategy.js.map +0 -1
  798. package/dist/services/substitution-engine.d.ts.map +0 -1
  799. package/dist/services/substitution-engine.js.map +0 -1
  800. package/dist/services/task-cleanup.d.ts.map +0 -1
  801. package/dist/services/task-cleanup.js.map +0 -1
  802. package/dist/services/token-issuer.d.ts.map +0 -1
  803. package/dist/services/token-issuer.js.map +0 -1
  804. package/dist/services/tool-registry.d.ts.map +0 -1
  805. package/dist/services/tool-registry.js.map +0 -1
  806. package/dist/services/update-check.d.ts.map +0 -1
  807. package/dist/services/update-check.js.map +0 -1
  808. package/dist/services/user-config.d.ts.map +0 -1
  809. package/dist/services/user-config.js.map +0 -1
  810. package/dist/services/vcs/azure-devops.d.ts.map +0 -1
  811. package/dist/services/vcs/azure-devops.js.map +0 -1
  812. package/dist/services/vcs/bitbucket.d.ts.map +0 -1
  813. package/dist/services/vcs/bitbucket.js.map +0 -1
  814. package/dist/services/vcs/constants.d.ts.map +0 -1
  815. package/dist/services/vcs/constants.js.map +0 -1
  816. package/dist/services/vcs/github.d.ts.map +0 -1
  817. package/dist/services/vcs/github.js.map +0 -1
  818. package/dist/services/vcs/types.d.ts.map +0 -1
  819. package/dist/services/vcs/types.js.map +0 -1
  820. package/dist/services/version-check.d.ts.map +0 -1
  821. package/dist/services/version-check.js.map +0 -1
  822. package/dist/services/watch/fleet-log.d.ts.map +0 -1
  823. package/dist/services/watch/fleet-log.js.map +0 -1
  824. package/dist/services/watch/project-resolver.d.ts.map +0 -1
  825. package/dist/services/watch/project-resolver.js.map +0 -1
  826. package/dist/services/watch/transcript-formatter.d.ts.map +0 -1
  827. package/dist/services/watch/transcript-formatter.js.map +0 -1
  828. package/dist/smoke-test.d.ts.map +0 -1
  829. package/dist/smoke-test.js.map +0 -1
  830. package/dist/tools/check-status.d.ts.map +0 -1
  831. package/dist/tools/check-status.js.map +0 -1
  832. package/dist/tools/child-id-allocator.d.ts.map +0 -1
  833. package/dist/tools/child-id-allocator.js.map +0 -1
  834. package/dist/tools/cloud-control.d.ts.map +0 -1
  835. package/dist/tools/cloud-control.js.map +0 -1
  836. package/dist/tools/code-intelligence-codebase-memory.d.ts.map +0 -1
  837. package/dist/tools/code-intelligence-codebase-memory.js.map +0 -1
  838. package/dist/tools/code-intelligence-freshness.d.ts.map +0 -1
  839. package/dist/tools/code-intelligence-freshness.js.map +0 -1
  840. package/dist/tools/code-intelligence-gitnexus.d.ts.map +0 -1
  841. package/dist/tools/code-intelligence-gitnexus.js.map +0 -1
  842. package/dist/tools/code-intelligence-kb-enrich.d.ts.map +0 -1
  843. package/dist/tools/code-intelligence-kb-enrich.js.map +0 -1
  844. package/dist/tools/code-intelligence-reindex.d.ts.map +0 -1
  845. package/dist/tools/code-intelligence-reindex.js.map +0 -1
  846. package/dist/tools/code-intelligence-telemetry.d.ts.map +0 -1
  847. package/dist/tools/code-intelligence-telemetry.js.map +0 -1
  848. package/dist/tools/code-intelligence-tests.d.ts.map +0 -1
  849. package/dist/tools/code-intelligence-tests.js.map +0 -1
  850. package/dist/tools/code-intelligence.d.ts.map +0 -1
  851. package/dist/tools/code-intelligence.js.map +0 -1
  852. package/dist/tools/compose-permissions.d.ts.map +0 -1
  853. package/dist/tools/compose-permissions.js.map +0 -1
  854. package/dist/tools/credential-store-delete.d.ts.map +0 -1
  855. package/dist/tools/credential-store-delete.js.map +0 -1
  856. package/dist/tools/credential-store-list.d.ts.map +0 -1
  857. package/dist/tools/credential-store-list.js.map +0 -1
  858. package/dist/tools/credential-store-set.d.ts.map +0 -1
  859. package/dist/tools/credential-store-set.js.map +0 -1
  860. package/dist/tools/credential-store-update.d.ts.map +0 -1
  861. package/dist/tools/credential-store-update.js.map +0 -1
  862. package/dist/tools/dolt-push-mutex.d.ts.map +0 -1
  863. package/dist/tools/dolt-push-mutex.js.map +0 -1
  864. package/dist/tools/execute-command.d.ts.map +0 -1
  865. package/dist/tools/execute-command.js.map +0 -1
  866. package/dist/tools/execute-prompt.d.ts.map +0 -1
  867. package/dist/tools/execute-prompt.js.map +0 -1
  868. package/dist/tools/get-member-model-pricing.d.ts.map +0 -1
  869. package/dist/tools/get-member-model-pricing.js.map +0 -1
  870. package/dist/tools/kb-capture.d.ts.map +0 -1
  871. package/dist/tools/kb-capture.js.map +0 -1
  872. package/dist/tools/kb-context.d.ts.map +0 -1
  873. package/dist/tools/kb-context.js.map +0 -1
  874. package/dist/tools/kb-export.d.ts.map +0 -1
  875. package/dist/tools/kb-export.js.map +0 -1
  876. package/dist/tools/kb-feedback.d.ts.map +0 -1
  877. package/dist/tools/kb-feedback.js.map +0 -1
  878. package/dist/tools/kb-freshness-sweep.d.ts.map +0 -1
  879. package/dist/tools/kb-freshness-sweep.js.map +0 -1
  880. package/dist/tools/kb-harvest.d.ts.map +0 -1
  881. package/dist/tools/kb-harvest.js.map +0 -1
  882. package/dist/tools/kb-import.d.ts.map +0 -1
  883. package/dist/tools/kb-import.js.map +0 -1
  884. package/dist/tools/kb-invalidate.d.ts.map +0 -1
  885. package/dist/tools/kb-invalidate.js.map +0 -1
  886. package/dist/tools/kb-list.d.ts.map +0 -1
  887. package/dist/tools/kb-list.js.map +0 -1
  888. package/dist/tools/kb-promote.d.ts.map +0 -1
  889. package/dist/tools/kb-promote.js.map +0 -1
  890. package/dist/tools/kb-query.d.ts.map +0 -1
  891. package/dist/tools/kb-query.js.map +0 -1
  892. package/dist/tools/kb-reconcile-prefilter.d.ts.map +0 -1
  893. package/dist/tools/kb-reconcile-prefilter.js.map +0 -1
  894. package/dist/tools/kb-resolve-contradiction.d.ts.map +0 -1
  895. package/dist/tools/kb-resolve-contradiction.js.map +0 -1
  896. package/dist/tools/kb-session-prime.d.ts.map +0 -1
  897. package/dist/tools/kb-session-prime.js.map +0 -1
  898. package/dist/tools/kb-setup.d.ts.map +0 -1
  899. package/dist/tools/kb-setup.js.map +0 -1
  900. package/dist/tools/kb-stats.d.ts.map +0 -1
  901. package/dist/tools/kb-stats.js.map +0 -1
  902. package/dist/tools/list-members.d.ts.map +0 -1
  903. package/dist/tools/list-members.js.map +0 -1
  904. package/dist/tools/member-detail.d.ts.map +0 -1
  905. package/dist/tools/member-detail.js.map +0 -1
  906. package/dist/tools/member-reservation.d.ts.map +0 -1
  907. package/dist/tools/member-reservation.js.map +0 -1
  908. package/dist/tools/monitor-task.d.ts.map +0 -1
  909. package/dist/tools/monitor-task.js.map +0 -1
  910. package/dist/tools/provision-auth.d.ts.map +0 -1
  911. package/dist/tools/provision-auth.js.map +0 -1
  912. package/dist/tools/provision-vcs-auth.d.ts.map +0 -1
  913. package/dist/tools/provision-vcs-auth.js.map +0 -1
  914. package/dist/tools/receive-files.d.ts.map +0 -1
  915. package/dist/tools/receive-files.js.map +0 -1
  916. package/dist/tools/register-member.d.ts.map +0 -1
  917. package/dist/tools/register-member.js.map +0 -1
  918. package/dist/tools/remove-member.d.ts.map +0 -1
  919. package/dist/tools/remove-member.js.map +0 -1
  920. package/dist/tools/report-status.d.ts.map +0 -1
  921. package/dist/tools/report-status.js.map +0 -1
  922. package/dist/tools/respond-to-message.d.ts.map +0 -1
  923. package/dist/tools/respond-to-message.js.map +0 -1
  924. package/dist/tools/revoke-vcs-auth.d.ts.map +0 -1
  925. package/dist/tools/revoke-vcs-auth.js.map +0 -1
  926. package/dist/tools/send-email.d.ts.map +0 -1
  927. package/dist/tools/send-email.js.map +0 -1
  928. package/dist/tools/send-files.d.ts.map +0 -1
  929. package/dist/tools/send-files.js.map +0 -1
  930. package/dist/tools/send-message.d.ts.map +0 -1
  931. package/dist/tools/send-message.js.map +0 -1
  932. package/dist/tools/setup-git-app.d.ts.map +0 -1
  933. package/dist/tools/setup-git-app.js.map +0 -1
  934. package/dist/tools/setup-ssh-key.d.ts.map +0 -1
  935. package/dist/tools/setup-ssh-key.js.map +0 -1
  936. package/dist/tools/shutdown-server.d.ts.map +0 -1
  937. package/dist/tools/shutdown-server.js.map +0 -1
  938. package/dist/tools/stop-prompt.d.ts.map +0 -1
  939. package/dist/tools/stop-prompt.js.map +0 -1
  940. package/dist/tools/update-agent-cli.d.ts.map +0 -1
  941. package/dist/tools/update-agent-cli.js.map +0 -1
  942. package/dist/tools/update-member.d.ts.map +0 -1
  943. package/dist/tools/update-member.js.map +0 -1
  944. package/dist/tools/version.d.ts.map +0 -1
  945. package/dist/tools/version.js.map +0 -1
  946. package/dist/types.d.ts.map +0 -1
  947. package/dist/types.js.map +0 -1
  948. package/dist/utils/agent-helpers.d.ts.map +0 -1
  949. package/dist/utils/agent-helpers.js.map +0 -1
  950. package/dist/utils/ansi.d.ts.map +0 -1
  951. package/dist/utils/ansi.js.map +0 -1
  952. package/dist/utils/auth-env.d.ts.map +0 -1
  953. package/dist/utils/auth-env.js.map +0 -1
  954. package/dist/utils/collect-secret.d.ts.map +0 -1
  955. package/dist/utils/collect-secret.js.map +0 -1
  956. package/dist/utils/credential-validation.d.ts.map +0 -1
  957. package/dist/utils/credential-validation.js.map +0 -1
  958. package/dist/utils/crypto.d.ts.map +0 -1
  959. package/dist/utils/crypto.js.map +0 -1
  960. package/dist/utils/deep-merge.d.ts.map +0 -1
  961. package/dist/utils/deep-merge.js.map +0 -1
  962. package/dist/utils/file-permissions.d.ts.map +0 -1
  963. package/dist/utils/file-permissions.js.map +0 -1
  964. package/dist/utils/gpu-parser.d.ts.map +0 -1
  965. package/dist/utils/gpu-parser.js.map +0 -1
  966. package/dist/utils/log-helpers.d.ts.map +0 -1
  967. package/dist/utils/log-helpers.js.map +0 -1
  968. package/dist/utils/oob-timeout.d.ts.map +0 -1
  969. package/dist/utils/oob-timeout.js.map +0 -1
  970. package/dist/utils/opencode-model-validation.d.ts.map +0 -1
  971. package/dist/utils/opencode-model-validation.js.map +0 -1
  972. package/dist/utils/pid-helpers.d.ts.map +0 -1
  973. package/dist/utils/pid-helpers.js.map +0 -1
  974. package/dist/utils/platform.d.ts.map +0 -1
  975. package/dist/utils/platform.js.map +0 -1
  976. package/dist/utils/process-utils.d.ts.map +0 -1
  977. package/dist/utils/process-utils.js.map +0 -1
  978. package/dist/utils/prompt-errors.d.ts.map +0 -1
  979. package/dist/utils/prompt-errors.js.map +0 -1
  980. package/dist/utils/resolve-member.d.ts.map +0 -1
  981. package/dist/utils/resolve-member.js.map +0 -1
  982. package/dist/utils/secure-input.d.ts.map +0 -1
  983. package/dist/utils/secure-input.js.map +0 -1
  984. package/dist/utils/session-caller.d.ts.map +0 -1
  985. package/dist/utils/session-caller.js.map +0 -1
  986. package/dist/utils/shell-escape.d.ts.map +0 -1
  987. package/dist/utils/shell-escape.js.map +0 -1
  988. package/dist/utils/skill-matrix.d.ts.map +0 -1
  989. package/dist/utils/skill-matrix.js.map +0 -1
  990. package/dist/utils/ssh-error-messages.d.ts.map +0 -1
  991. package/dist/utils/ssh-error-messages.js.map +0 -1
  992. package/dist/utils/work-folder-validation.d.ts.map +0 -1
  993. package/dist/utils/work-folder-validation.js.map +0 -1
  994. package/dist/utils/workspace-trust.d.ts.map +0 -1
  995. package/dist/utils/workspace-trust.js.map +0 -1
  996. package/dist/version.d.ts.map +0 -1
  997. package/dist/version.js.map +0 -1
@@ -1,3 +1,4 @@
1
+ import { escapePowerShellArgInner } from '../utils/shell-escape.js';
1
2
  import fs from 'node:fs';
2
3
  import os from 'node:os';
3
4
  import path from 'node:path';
@@ -11,18 +12,20 @@ import { getAgentOS, getAgentShell, touchAgent, getStoredPid, isPosixShellMember
11
12
  import { updateAgent } from '../services/registry.js';
12
13
  import { memberIdentifier, resolveMember } from '../utils/resolve-member.js';
13
14
  import { isRetryable, authErrorAdvice, workspaceNotTrustedAdvice } from '../utils/prompt-errors.js';
14
- import { buildAuthEnvPrefix } from '../utils/auth-env.js';
15
+ import { stageAuthEnv, removeMemberSecretFile, SecretDeliveryError } from '../services/member-secret-env.js';
15
16
  import { writeStatusline } from '../services/statusline.js';
16
17
  import { getModelOverride } from '../services/user-config.js';
17
18
  import { ensureCloudReady } from '../services/cloud/lifecycle.js';
18
19
  import { getStallDetector, resolveSessionLogPath } from '../services/stall/index.js';
19
20
  import { getCachedMemberPathContext } from '../services/member-home.js';
20
21
  import { provisionAgents, remoteAgentsDir, loadCanonicalAgentSet } from '../services/agent-provisioner.js';
22
+ import { ensureNoProjectAgentShadows } from '../services/agent-shadow.js';
21
23
  import { escapeWindowsArg, escapeDoubleQuoted } from '../os/os-commands.js';
22
24
  import { resolveTilde } from './execute-command.js';
23
25
  import { clearStoredPid } from '../utils/agent-helpers.js';
24
26
  import { tryKillPid, isPidAlive } from '../utils/pid-helpers.js';
25
27
  import { recoverOrphanedDispatch, isRemoteProcessAlive } from '../services/orphan-recovery.js';
28
+ import { hasSecretToken } from '../services/secret-token.js';
26
29
  import { seedWorkspaceTrust } from '../utils/workspace-trust.js';
27
30
  import { durableOutputPath } from '../os/linux.js';
28
31
  import { LogScope, logLine, logWarn, maskSecrets, truncateForLog } from '../utils/log-helpers.js';
@@ -33,22 +36,50 @@ import { getTokenIssuer } from '../services/token-issuer.js';
33
36
  import { resolveExpectedDemand, checkContextAdmission, recordSessionUsage } from '../services/context-admission.js';
34
37
  import { recordKnownSession, isKnownSession } from '../services/known-sessions.js';
35
38
  import { resolveBudgetScope, evaluateBudget, recordAndEvaluate } from '../services/budget-awareness.js';
39
+ import { dispatchCostFromCumulative } from '../services/session-cost.js';
36
40
  import { sendMessage } from './send-message.js';
41
+ import { serverVersion } from '../version.js';
37
42
  import { registerPending } from '../services/pending-responses.js';
43
+ import { inFlightAgents } from '../services/in-flight-agents.js';
38
44
  import { isMaxTurnsResponse } from '../providers/provider.js';
39
45
  import { preflightCheck } from '../services/preflight-check.js';
46
+ import { ensureAgyProject } from '../services/agy-project.js';
47
+ import { sessionMcpInjectionAvailable, sessionMcpConfigPath, sessionMcpConfigIsPerDispatch, writeSessionMcpConfig, resolveSessionMcpAlwaysLoad } from '../services/session-mcp-config.js';
48
+ export function toStructuredUsage(u) {
49
+ return {
50
+ input_tokens: u.input_tokens,
51
+ output_tokens: u.output_tokens,
52
+ cache_read_input_tokens: u.cache_read_input_tokens ?? 0,
53
+ cache_creation_input_tokens: u.cache_creation_input_tokens ?? 0,
54
+ total_tokens: u.input_tokens + u.output_tokens,
55
+ ...(typeof u.cost_usd === 'number' ? { cost_usd: u.cost_usd } : {}),
56
+ };
57
+ }
58
+ /** Failure classes that win over a permission denial carried in the same
59
+ * result: the caller acts on these (resume, re-provision, retry later). */
60
+ const TYPED_FAILURE_CATEGORIES = new Set(['auth', 'server', 'overloaded', 'workspace_not_trusted', 'max_turns']);
40
61
  export const executePromptSchema = z.object({
41
62
  ...memberIdentifier,
42
63
  prompt: z.string().describe('The prompt to send to the LLM on the remote member'),
43
64
  session_id: z.string().optional().describe('Shorthand for explicit session resume. Equivalent to resume: "<session_id>". If provided, takes precedence over resume.'),
44
65
  resume: z.union([z.boolean(), z.string()]).default(true).describe('Session-resume control (default: true). ' +
45
- 'true = best-effort resume of the member\'s stored last session (a stale/unknown stored session transparently falls back to a fresh session). ' +
66
+ 'true = best-effort resume of the member\'s stored last session (a stale/unknown stored session, or one recorded by a different server build, transparently falls back to a fresh session). ' +
46
67
  'false = always start a fresh session. ' +
47
68
  '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
69
  '(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)'),
50
- 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.'),
70
+ fork: z.union([z.boolean(), z.string()]).optional().describe('Branch a NEW session from an existing one instead of continuing it in place. ' +
71
+ 'Mirrors resume\'s shape, but BRANCHES rather than continues: it mints a distinct new ' +
72
+ 'session id seeded from the source session\'s context, leaving the source session itself ' +
73
+ 'untouched. true = fork from the member\'s stored last session. A session-id STRING = fork ' +
74
+ 'from exactly that session. Mutually exclusive with resume (any non-default value, i.e. ' +
75
+ 'false or a session-id string) and with session_id (a resume shorthand) -- specifying fork ' +
76
+ 'together with either is rejected as a validation error before any member resolution or LLM ' +
77
+ 'call. NOTE: this field only covers the schema + the mutual-exclusivity guard; actual fork ' +
78
+ 'MODE RESOLUTION (minting/wiring the forked session) is implemented separately.'),
79
+ 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()).'),
80
+ max_total_s: z.number().optional().describe('Hard ceiling in seconds, measured from the moment the call is received (setup such as member readiness and preflight counts) -- the command is killed after this total elapsed time regardless of activity and the call returns reason max_total_time. If omitted, there is no total time limit.'),
51
81
  max_turns: z.number().min(1).max(500).optional().describe('Max turns for claude -p (default: 50)'),
82
+ fail_on_permission_denial: z.boolean().optional().describe('Strict permission handling (default: false). true = a tool call the member CLI refused fails the dispatch with reason "permission_denied" and a permissionDenied block (suggestedGrants to heal it with compose_permissions), even when the reply looks complete -- except a Claude refusal with healable:false (auto/bypass mode: safety classifier or deny rule) on a complete reply, which is only a permissionWarning. Omitted/false = each provider keeps its earlier behaviour: a complete Claude reply is a success carrying the refusals in permissionWarning; AGY refusals still fail permission_denied. Either way a max_turns / auth / server / overloaded result keeps its own reason, with the refusals attached as permissionDenied.'),
52
83
  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.'),
53
84
  substitutions: z.record(z.string(), z.string()).optional().describe('Optional map of token name to replacement value. ' +
54
85
  'When provided, every occurrence of {{name}} in the prompt is replaced before the prompt is staged on the member. ' +
@@ -56,8 +87,7 @@ export const executePromptSchema = z.object({
56
87
  'Extra keys are silently ignored. Values are never logged.'),
57
88
  agent: z.string().optional().describe('Optional agent name to activate. ' +
58
89
  'For Claude: invokes claude --agent <name>. ' +
59
- 'For AGY: prepends @<name> to the prompt on every dispatch. ' +
60
- 'Substitution runs before the @<name> prepend. ' +
90
+ 'For AGY: invokes agy --agent <name>. ' +
61
91
  'Agent file must exist at the provider-specific path on the member: ' +
62
92
  'Claude: <workFolder>/.claude/agents/<name>.md or ~/.claude/agents/<name>.md; ' +
63
93
  'AGY: <workFolder>/.gemini/antigravity-cli/agents/<name>.md or ~/.gemini/antigravity-cli/agents/<name>.md; ' +
@@ -100,6 +130,51 @@ ${output}`;
100
130
  : `[FAIL] Prompt failed on "${agentName}":
101
131
  ${output}`;
102
132
  }
133
+ // apra-fleet-25yl.2.1: the exec-level rolling deadline used for a provider
134
+ // whose ExecTimeoutSource is 'total_ceiling' when the caller supplied NO
135
+ // max_total_s -- i.e. there is no ceiling to mirror. 24h: large enough that it
136
+ // can never bind before any realistic dispatch or client-side deadline, and
137
+ // small enough to stay inside the int32 range setTimeout accepts (Infinity or
138
+ // MAX_SAFE_INTEGER overflow to "fire immediately", which would invert the
139
+ // decoupling into an instant kill). The alternative -- falling back to
140
+ // timeout_s -- is deliberately NOT taken: that is the exact coupling this
141
+ // removes, and for these providers timeout_s belongs to the StallDetector.
142
+ const EXEC_TIMER_NEVER_BINDS_MS = 86_400_000;
143
+ const FAILURE_STDERR_TAIL_CHARS = 2000;
144
+ const FAILURE_STDERR_TAIL_LINES = 20;
145
+ const FAILURE_EVENT_CHARS = 1000;
146
+ /**
147
+ * GitHub #585: the diagnostic payload logged when execute_prompt's dispatch
148
+ * exits non-zero -- the TAIL of stderr (the end is where CLIs print the
149
+ * fatal error), capped by lines and characters, plus the last JSON
150
+ * result/error event on stdout, capped. Redaction happens in logLine.
151
+ */
152
+ export function formatPromptFailureTail(result) {
153
+ let stderrTail = (result.stderr ?? '').trimEnd();
154
+ const lines = stderrTail.split('\n');
155
+ if (lines.length > FAILURE_STDERR_TAIL_LINES)
156
+ stderrTail = lines.slice(-FAILURE_STDERR_TAIL_LINES).join('\n');
157
+ if (stderrTail.length > FAILURE_STDERR_TAIL_CHARS)
158
+ stderrTail = stderrTail.slice(-FAILURE_STDERR_TAIL_CHARS);
159
+ let lastEvent;
160
+ const outLines = (result.stdout ?? '').split('\n');
161
+ for (let i = outLines.length - 1; i >= 0 && lastEvent === undefined; i--) {
162
+ const line = outLines[i].trim();
163
+ if (!line.startsWith('{'))
164
+ continue;
165
+ try {
166
+ const ev = JSON.parse(line);
167
+ if (ev.type === 'result' || ev.type === 'error' || ev.is_error === true || ev.error !== undefined) {
168
+ lastEvent = line.length > FAILURE_EVENT_CHARS ? line.slice(0, FAILURE_EVENT_CHARS) + '...[truncated]' : line;
169
+ }
170
+ }
171
+ catch { /* not a JSON event line */ }
172
+ }
173
+ return JSON.stringify({ exit: result.code, stderrTail, ...(lastEvent !== undefined ? { lastEvent } : {}) });
174
+ }
175
+ /** GitHub #563: max_total_s time held back from every attempt for prompt-file
176
+ * cleanup and the response (see cleanupReserveMs in executePrompt). */
177
+ export const EXECUTE_PROMPT_CLEANUP_RESERVE_MS = 5000;
103
178
  const SERVER_RETRY_DELAY_MS = 5000;
104
179
  // A prompt written whole into a single remote exec command line can exceed
105
180
  // the SSH exec channel's / Windows CreateProcess's command-line ceiling once
@@ -145,7 +220,7 @@ async function writePromptFile(agent, strategy, promptFilePath, content) {
145
220
  for (let i = 0; i < chunks.length; i++) {
146
221
  const setup = i === 0 ? `New-Item -Path '${escapedFolder}' -ItemType Directory -Force | Out-Null; ` : '';
147
222
  const cmdlet = i === 0 ? 'Set-Content' : 'Add-Content';
148
- const psScript = `${setup}Set-Location "${escapedFolder}"; ${cmdlet} -Path "${promptFileName}" -Value '${chunks[i].replace(/'/g, "''")}' -NoNewline -Encoding UTF8`;
223
+ const psScript = `${setup}Set-Location "${escapedFolder}"; ${cmdlet} -Path "${promptFileName}" -Value '${escapePowerShellArgInner(chunks[i])}' -NoNewline -Encoding UTF8`;
149
224
  const encoded = Buffer.from(psScript, 'utf16le').toString('base64');
150
225
  // eslint-disable-next-line no-await-in-loop -- each chunk must land before the next appends
151
226
  await strategy.execCommand(`powershell -EncodedCommand ${encoded}`);
@@ -179,12 +254,16 @@ async function deletePromptFile(agent, strategy, promptFilePath, extraPaths = []
179
254
  const promptFileName = path.basename(promptFilePath);
180
255
  const remoteDir = path.dirname(promptFilePath);
181
256
  // apra-fleet-7dir.5.4: gitbash members take the POSIX branch, matching
182
- // writePromptFile above -- and only that branch deletes `extraPaths` (the
183
- // durable stdout mirror), which lives at a POSIX /tmp path PowerShell would
184
- // resolve to a different directory entirely.
257
+ // writePromptFile above -- the durable stdout mirror in `extraPaths` lives
258
+ // at a POSIX /tmp path PowerShell would resolve to a different directory
259
+ // entirely.
185
260
  if (!isPosixShellMember(agent)) {
186
261
  const escapedFolder = escapeWindowsArg(remoteDir);
187
- const psScript = `Set-Location "${escapedFolder}"; Remove-Item "${promptFileName}" -Force -ErrorAction SilentlyContinue`;
262
+ // extraPaths here are only staged credential files (absolute member
263
+ // paths); PowerShell members have no durable stdout mirror.
264
+ // Removed first, by absolute path, independent of the work folder.
265
+ const extras = extraPaths.map(p => `Remove-Item -LiteralPath '${escapePowerShellArgInner(p)}' -Force -ErrorAction SilentlyContinue; `).join('');
266
+ const psScript = `${extras}Set-Location "${escapedFolder}"; Remove-Item "${promptFileName}" -Force -ErrorAction SilentlyContinue`;
188
267
  const encoded = Buffer.from(psScript, 'utf16le').toString('base64');
189
268
  await strategy.execCommand(`powershell -EncodedCommand ${encoded}`).catch(() => { });
190
269
  }
@@ -193,7 +272,10 @@ async function deletePromptFile(agent, strategy, promptFilePath, extraPaths = []
193
272
  // apra-fleet-6z8.1: the durable stdout mirror is cleaned up in the SAME
194
273
  // round trip as the prompt file -- no extra exec per dispatch.
195
274
  const extras = extraPaths.map(p => ` "${escapeDoubleQuoted(p)}"`).join('');
196
- await strategy.execCommand(`cd "${escapedFolder}" && rm -f ${promptFileName}${extras}`).catch(() => { });
275
+ // extraPaths are absolute (durable mirror, staged credential files): removed
276
+ // unconditionally, even when the work folder is gone and the cd fails.
277
+ const extrasRm = extras ? `rm -f${extras}; ` : '';
278
+ await strategy.execCommand(`${extrasRm}cd "${escapedFolder}" && rm -f ${promptFileName}`).catch(() => { });
197
279
  }
198
280
  }
199
281
  export function resolveModelForTier(agent, tier, provider) {
@@ -212,11 +294,10 @@ export function resolveModelForTier(agent, tier, provider) {
212
294
  * shared with member_detail (the fleet-sprint engine's only source for it).
213
295
  */
214
296
  export { knownRepoRemoteUrl };
215
- const SECURE_TOKEN_RE = /\{\{secure\.[a-zA-Z0-9_-]{1,64}\}\}/;
216
297
  /**
217
298
  * The sprint id this server process dispatches on behalf of, or undefined when
218
299
  * run outside a sprint (e.g. a manual cli.mjs invocation). Sourced from
219
- * APRA_FLEET_SPRINT_ID, which the auto-sprint spawner stamps into the per-sprint
300
+ * APRA_FLEET_SPRINT_ID, which the fleet-sprint spawner stamps into the per-sprint
220
301
  * server's environment (apra-fleet-eft.10.3). Read on every dispatch so a
221
302
  * reservation set/cleared mid-run is observed without a restart.
222
303
  */
@@ -227,7 +308,7 @@ export function currentSprintId() {
227
308
  const trimmed = raw.trim();
228
309
  return trimmed.length > 0 ? trimmed : undefined;
229
310
  }
230
- export const inFlightAgents = new Set();
311
+ export { inFlightAgents };
231
312
  // Member ids whose remote agent files (planner.md, doer.md, _shared/, schemas/, ...)
232
313
  // have already been probed/refreshed this server process uptime -- the #336
233
314
  // provisioner is a real SSH round trip, so we pay that cost once per member per
@@ -433,8 +514,48 @@ async function executePromptInteractive(agent, renderedPrompt, input, workspaceI
433
514
  }
434
515
  }
435
516
  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.';
517
+ // GitHub #563: max_total_s is measured from HERE, handler entry -- cloud
518
+ // readiness, preflight and agent provisioning are all charged to it, since
519
+ // the client's own deadline (max_total_s + a fixed grace) starts at the call.
520
+ const handlerStartedAt = Date.now();
521
+ if (hasSecretToken(input.prompt)) {
522
+ return 'error: execute_prompt prompt contains {{secret.NAME}} token. Secrets must never be passed to LLM prompts. Use execute_command with {{secret.NAME}} instead.';
523
+ }
524
+ // apra-fleet-3swo.42: normalise `fork` ONCE, here, before any predicate is
525
+ // derived from it. Previously `forkRequested` (below) checked
526
+ // input.fork.length > 0 (untrimmed) while `explicitForkId` (further down)
527
+ // checked input.fork.trim().length > 0 -- for a whitespace-only fork id the
528
+ // two disagreed (forkRequested true, explicitForkId undefined), which
529
+ // silently routed a caller's explicit-but-invalid fork id into the
530
+ // fork===true best-effort path and could hand back a plain FRESH session
531
+ // with no error. Both forkRequested and explicitForkId are now derived from
532
+ // this single normalised value, so an all-whitespace or empty string can
533
+ // never disagree with itself. Trimming a boolean/undefined value is a
534
+ // no-op -- the ternary passes it through unchanged.
535
+ const forkArg = typeof input.fork === 'string' ? input.fork.trim() : input.fork;
536
+ // fork/resume mutual-exclusivity guard (apra-fleet-lmtg.4): fork branches a
537
+ // NEW session from an existing one, resume continues IN PLACE -- the two
538
+ // are semantically incompatible, so a call requesting both is rejected here,
539
+ // before any member resolution or LLM invocation, rather than silently
540
+ // preferring one. session_id is documented as pure resume shorthand, so its
541
+ // mere presence also conflicts with fork even when the `resume` field
542
+ // itself still holds its default. "Non-default resume" means any value
543
+ // other than the schema default `true` (i.e. `false`, or an explicit
544
+ // session-id string) -- default-true resume is left alone since fork mode
545
+ // resolution (next task) supersedes it.
546
+ // Deliberately NOT `forkArg.length > 0`: any STRING value of fork (including
547
+ // '' and whitespace-only, once trimmed to '') is a fork REQUEST -- just one
548
+ // whose explicit id turns out to be invalid (see explicitForkId below),
549
+ // rejected with a terminal session_not_found rather than silently treated
550
+ // as "no fork requested at all".
551
+ const forkRequested = forkArg === true || typeof forkArg === 'string';
552
+ if (forkRequested) {
553
+ if (input.session_id !== undefined) {
554
+ 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.';
555
+ }
556
+ if (input.resume !== true) {
557
+ return 'error: execute_prompt cannot set both "fork" and a non-default "resume" -- fork branches a new session instead of resuming. Specify only one.';
558
+ }
438
559
  }
439
560
  // Validate substitution keys before any I/O or member resolution.
440
561
  if (input.substitutions !== undefined) {
@@ -463,7 +584,31 @@ export async function executePrompt(input, extra) {
463
584
  return agentOrError;
464
585
  let agent;
465
586
  try {
466
- agent = await ensureCloudReady(agentOrError); // auto-start if stopped
587
+ // GitHub #563 review: a cold cloud start can take minutes. Bound it by the
588
+ // max_total_s budget (minus the cleanup reserve) so a slow start returns
589
+ // the typed max_total_time error before the client's own deadline. The
590
+ // start itself keeps going in the background; the next call finds the
591
+ // instance running.
592
+ const cloudBudgetMs = input.max_total_s !== undefined
593
+ ? Math.max(0, input.max_total_s * 1000 - Math.min(EXECUTE_PROMPT_CLEANUP_RESERVE_MS, Math.floor(input.max_total_s * 100)) - (Date.now() - handlerStartedAt))
594
+ : undefined;
595
+ const ready = ensureCloudReady(agentOrError); // auto-start if stopped
596
+ if (cloudBudgetMs === undefined) {
597
+ agent = await ready;
598
+ }
599
+ else {
600
+ let budgetTimer;
601
+ const outOfBudget = new Promise((resolve) => { budgetTimer = setTimeout(() => resolve(null), cloudBudgetMs); });
602
+ const raced = await Promise.race([ready, outOfBudget]).finally(() => clearTimeout(budgetTimer));
603
+ if (raced === null) {
604
+ ready.catch(() => { });
605
+ return {
606
+ text: `[FAIL] execute_prompt on "${agentOrError.friendlyName}" exceeded max_total_s (${input.max_total_s}s) while waiting for the cloud member to start -- the start continues in the background; retry shortly.`,
607
+ structuredContent: { isError: true, reason: 'max_total_time', dispatched: false },
608
+ };
609
+ }
610
+ agent = raced;
611
+ }
467
612
  }
468
613
  catch (err) {
469
614
  return `[FAIL] Failed to execute prompt on "${agentOrError.friendlyName}": ${err.message}`;
@@ -537,965 +682,1535 @@ export async function executePrompt(input, extra) {
537
682
  // return immediately following) must release this lock explicitly, since
538
683
  // it now returns AFTER the lock is claimed instead of before.
539
684
  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
- };
685
+ // apra-fleet-c98q: if anything between this claim and a try/finally that
686
+ // owns the release throws (e.g. an SSH drop in writePromptFile), release
687
+ // the lock and stall entry here and rethrow. Body deliberately not
688
+ // reindented to keep this release fix minimal.
689
+ let claimGuardActive = true;
690
+ try {
691
+ // Peek at session state early so the preflight check can skip interactive
692
+ // members whose dispatch routes through a live MCP push channel, not SSH.
693
+ const earlyWorkspaceId = getTokenIssuer().workspaceId();
694
+ const earlySession = sessionRegistry.get(earlyWorkspaceId, agent.id);
695
+ const isChannelCapable = !!earlySession?.channelCapable;
696
+ // Pre-dispatch readiness check (apra-fleet preflight-check): verify
697
+ // connectivity and LLM auth BEFORE the expensive prompt dispatch
698
+ // (writePromptFile + CLI invocation). Catches expired OAuth, missing
699
+ // credentials, and offline members in <1s instead of burning a full
700
+ // round trip. Local members and interactive sessions are excluded
701
+ // (local shares this machine's credentials; interactive sessions have
702
+ // their own liveness probes). The check is cached for 60s so
703
+ // back-to-back dispatches do not add latency.
704
+ if (agent.agentType !== 'local' && !isChannelCapable) {
705
+ let preflight;
706
+ try {
707
+ preflight = await preflightCheck(agent);
708
+ }
709
+ catch (err) {
710
+ // preflightCheck's own synchronous prologue (getStrategy/getProvider)
711
+ // and its internal exec calls are not fully guarded -- if it throws
712
+ // instead of resolving {ok: false}, the lock claimed above would
713
+ // otherwise leak forever (no pid captured yet for findDeadLockPid's
714
+ // stale-lock self-heal to recognize). Release and propagate unchanged
715
+ // -- this preserves the exact same exception the caller would have
716
+ // seen before the lock was claimed this early.
717
+ inFlightAgents.delete(agent.id);
718
+ throw err;
719
+ }
720
+ if (!preflight.ok) {
721
+ // R2-F5: use preflight-specific reason codes so fleet-sprint can
722
+ // distinguish pre-dispatch failures from in-dispatch ones and avoid
723
+ // inappropriate self-heal loops (e.g. re-provisioning auth when the
724
+ // member is simply offline).
725
+ const preflightReason = preflight.code === 'offline' ? 'preflight_offline'
726
+ : preflight.code === 'auth_expired' ? 'preflight_auth_expired'
727
+ : preflight.code === 'auth_missing' ? 'preflight_auth_missing'
728
+ : 'dispatch_failed';
729
+ // Release the lock claimed above -- this return happens before the
730
+ // interactive/subprocess split's own add+finally cleanup ever runs.
731
+ inFlightAgents.delete(agent.id);
732
+ return {
733
+ text: `[FAIL] Pre-dispatch check failed for "${agent.friendlyName}": ${preflight.reason}`,
734
+ structuredContent: {
735
+ isError: true,
736
+ reason: preflightReason,
737
+ },
738
+ };
739
+ }
588
740
  }
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.
741
+ // Interactive routing (apra-fleet-2xs.8/us9.8, docs/cloud-fleet-architecture.md
742
+ // section 6): if this member has a live MCP session connected right now,
743
+ // route via send_message + wait-for-response instead of spawning a
744
+ // subprocess. Decided tier-2-locally against THIS machine's session
745
+ // registry only (never caller/hub-side state, per apra-fleet-2xs.8's own
746
+ // scope note) -- so behavior is unaffected by whether execute_prompt is
747
+ // invoked directly (Phase 1) or relayed through a future hub. Falls
748
+ // through to the unchanged subprocess/SSH path below for every member
749
+ // without a live session (the common case today, and always for members
750
+ // that never opt into an interactive session).
652
751
  //
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);
752
+ // Gated by capability, not provider name (apra-fleet-cqa, eft.74 follow-up):
753
+ // mode (b) -- server-push mid-session prompt injection -- was POC-proven on
754
+ // Claude via the provider-branded `notifications/claude/channel` capability
755
+ // (apra-fleet-us9.9's survey, docs/interactive-injection-provider-survey.md),
756
+ // but the routing decision itself must be provider-agnostic: whatever the
757
+ // provider, a session is only an interactive-routing candidate if it
758
+ // actually declared that capability at MCP initialize time (recorded as
759
+ // SessionState.channelCapable below). Codex is confirmed [FAIL]
760
+ // today (no equivalent push mechanism) and so never ends up channelCapable in
761
+ // practice, but that is a fact about what each provider adapter currently
762
+ // advertises, not a name-based pre-filter here -- any provider that
763
+ // implements the same MCP channel capability is picked up automatically. A
764
+ // member CAN still have a live sessionRegistry entry (its per-folder
765
+ // apra-fleet MCP entry gives it basic MCP tool access) without that meaning
766
+ // it can receive or act on this push -- routing to it anyway would silently
767
+ // spend the full timeout_s waiting for a response that can never arrive.
768
+ // R2-F3: re-query session registry after the preflight await (10-20s) so
769
+ // interactive routing sees sessions that became channelCapable during that
770
+ // window, rather than using the stale pre-preflight snapshot.
771
+ const workspaceId = getTokenIssuer().workspaceId();
772
+ const rawSession = sessionRegistry.get(workspaceId, agent.id);
773
+ // apra-fleet-eft.74.1: interactive routing requires the EXPLICIT channel
774
+ // opt-in handshake, not mere JWT registration. A plain subprocess
775
+ // connect-back (a Doer that opened an MCP tool-access session with a member
776
+ // JWT but never declared the `claude/channel` capability) registers a live
777
+ // `server` here, yet can never receive the `notifications/claude/channel`
778
+ // push -- routing to it would enqueue a message nothing reads and burn the
779
+ // full timeout_s on every later dispatch (the eft.74 wedge). Only a
780
+ // channel-capable session is an interactive-routing candidate; anything else
781
+ // (including that Doer's live tool session, which must be left untouched)
782
+ // falls through to the unchanged subprocess path below.
783
+ let interactiveSession = rawSession?.channelCapable ? rawSession : undefined;
784
+ // apra-fleet-eft.50.1: resolve the pid to test FRESH on every dispatch
785
+ // (never cached from a prior attempt) and fall back to the durable
786
+ // launch-pid anchor when this reused session lost its own pid on a
787
+ // reconnect. This is what re-arms the dead-session guard on a retry attempt
788
+ // 2+ exactly as on attempt 1: the specific eft.50 ordering (attempt 1 fails
789
+ // clean, attempt 2 targets a now-dead reconnected session) used to slip
790
+ // through here because the reconnected SessionState had pid=undefined, so the
791
+ // check below was skipped and the caller hung on the dead channel.
792
+ const interactivePid = interactiveSession?.pid
793
+ ?? sessionRegistry.lastKnownPid(workspaceId, agent.id);
794
+ if (interactiveSession?.server && interactivePid !== undefined && !isPidAlive(interactivePid)) {
795
+ // apra-fleet-eft.28.1/eft.28.5: never reuse a persistent interactive
796
+ // session whose underlying member claude process has already died. Before
797
+ // eft.28.1, a dead launch-time process (e.g. it crashed before ever
798
+ // producing a plan) left a `server` entry in sessionRegistry that looked
799
+ // reusable -- send_message would happily enqueue to it, but nothing would
800
+ // ever call respond_to_message, so the caller silently burned the full
801
+ // timeout_s (observed up to 3600s in apra-fleet-eft.28) with zero
802
+ // fleet-server log output and no watchdog coverage.
803
+ //
804
+ // eft.28.5 changes what happens once the death is detected: instead of
805
+ // surfacing a terminal dispatch_failed error that forces a manual
806
+ // register_member, EVICT the dead session and FALL THROUGH to a fresh
807
+ // non-interactive (subprocess) dispatch below -- i.e. re-dispatch fresh
808
+ // instead of blocking on waitForInteractiveResponse. The bug in
809
+ // apra-fleet-eft.28 was precisely that a dead session was reused "rather
810
+ // than detecting its death and spawning a fresh dispatch"; this does the
811
+ // spawning. If the fresh subprocess dispatch itself cannot start it
812
+ // returns its own terminal error, so nothing ever hangs.
813
+ //
814
+ // The liveness check now fires for connect-back interactive sessions too:
815
+ // http-transport carries the launch-time pid forward across re-registration
816
+ // (eft.28.5), so `pid` is no longer undefined for a member that registered
817
+ // via register_member and then connected back -- the exact real-fleet
818
+ // repro that evaded eft.28.1.
819
+ //
820
+ // apra-fleet-eft.50.1: eft.28.5's carry-forward still lost the pid when a
821
+ // reconnect happened AFTER the prior SessionState was already unregistered
822
+ // (priorPid lookup found nothing), so a retry attempt 2+ reused a
823
+ // pid=undefined session and hung. `interactivePid` above now back-stops
824
+ // that with sessionRegistry.lastKnownPid, the durable per-member launch-pid
825
+ // anchor, so this guard re-arms on EVERY dispatch attempt that reuses an
826
+ // interactive session, not just the first. It stays undefined only for
827
+ // sessions that never had a captured PID at all (e.g. tests, or a provider
828
+ // that never went through register_member's local spawn path); those are
829
+ // left to the pre-existing interactive behavior, unchanged.
830
+ const deadScope = new LogScope('execute_prompt', `[interactive] session liveness check pid=${interactivePid}`, agent);
831
+ sessionRegistry.unregister(workspaceId, agent.id);
832
+ deadScope.info(`member claude process (pid ${interactivePid}) for "${agent.friendlyName}" is dead -- evicting the stale interactive session and re-dispatching fresh (non-interactive)`);
833
+ interactiveSession = undefined;
691
834
  }
692
- finally {
693
- inFlightAgents.delete(agent.id);
694
- writeStatusline(new Map([[agent.id, 'idle']]));
835
+ // Project-level <workFolder>/.claude/agents files take precedence over the
836
+ // user-level role set; quarantine untracked shadows, report tracked ones on
837
+ // every dispatch. Runs before the interactive/subprocess split so both paths
838
+ // are covered, local members included. Independent of the user-level
839
+ // provisioning below (compares against the canonical set). Never throws.
840
+ const shadowWarning = await ensureNoProjectAgentShadows(agent);
841
+ if (shadowWarning)
842
+ heuristicWarningSuffix += `\n\n[WARN] ${shadowWarning}`;
843
+ if (interactiveSession?.server) {
844
+ // Lock already claimed above, before the preflight await -- do not
845
+ // re-add here (Set.add would be a harmless no-op, but keeping a second
846
+ // add site invites the lock and its release to drift out of sync).
847
+ writeStatusline(new Map([[agent.id, 'busy']]));
848
+ claimGuardActive = false;
849
+ try {
850
+ return await executePromptInteractive(agent, renderedPrompt, input, workspaceId, heuristicWarningSuffix);
851
+ }
852
+ finally {
853
+ inFlightAgents.delete(agent.id);
854
+ writeStatusline(new Map([[agent.id, 'idle']]));
855
+ }
695
856
  }
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.
857
+ // Lock already claimed above, before the preflight await.
858
+ // AGY: every dispatch is bound to the member's own agy project with
859
+ // --project; without it agy silently runs under the machine-wide
860
+ // default-cli-project. A member without a verified project (registered
861
+ // before project binding, or whose project file is gone/corrupt) gets one
862
+ // here. Failure is terminal -- never a dispatch without --project.
863
+ let agyProjectId;
864
+ if (agent.llmProvider === 'agy') {
738
865
  try {
739
- stallAbortController.abort();
866
+ agyProjectId = (await ensureAgyProject(agent)).projectId;
867
+ }
868
+ catch (e) {
869
+ inFlightAgents.delete(agent.id);
870
+ writeStatusline(new Map([[agent.id, 'idle']]));
871
+ return {
872
+ 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.`,
873
+ structuredContent: { isError: true, reason: 'dispatch_failed' },
874
+ };
740
875
  }
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
876
  }
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);
877
+ await ensureAgentFilesProvisioned(agent);
878
+ const stallDetector = getStallDetector();
879
+ let clearedByStall = false;
880
+ // GitHub #562: which stall fired -- a frozen transcript, or a session log
881
+ // that never appeared at its authoritative path (the agent never started).
882
+ let stallReason = 'stalled';
883
+ // apra-fleet-3c9.1: a CONFIRMED stall must not only kill the remote pid but
884
+ // also cancel the in-flight strategy.execCommand() promise. Before this, the
885
+ // client kept waiting out its full deriveTimeoutMs deadline after the
886
+ // server-side work had already died (the 60.5-min hung dispatch in
887
+ // apra-fleet-3c9). onStall aborts this controller; its signal is merged into
888
+ // the signal handed to every execCommand below (see dispatchSignal), so a
889
+ // confirmed stall settles the pending dispatch immediately and surfaces a
890
+ // typed 'stalled' error instead of hanging.
891
+ const stallAbortController = new AbortController();
892
+ // apra-fleet-25yl.1.2: input.timeout_s is the real per-dispatch stall
893
+ // baseline now, not merely an inactivity kill on stdout/stderr -- see the
894
+ // schema description on `timeout_s` above. A caller that omits timeout_s
895
+ // gets a 300s baseline (the schema default), a deliberate change from the
896
+ // previously silent 150s DEFAULT_STALL_THRESHOLD_MS fallback.
897
+ const stallThresholdMs = (input.timeout_s ?? 300) * 1000;
898
+ stallDetector.add(agent.id, {
899
+ sessionId: null,
900
+ logFilePath: null,
901
+ lastActivityAt: Date.now(),
902
+ consecutiveIdleCycles: 0,
903
+ consecutiveReadFailures: 0,
904
+ memberId: agent.id,
905
+ memberName: agent.friendlyName,
906
+ provisional: true,
907
+ stallReported: false,
908
+ thresholdMs: stallThresholdMs,
909
+ onStall: (reason) => {
910
+ stallReason = reason ?? 'stalled';
911
+ // Stall detector already wrote 'unknown' to the statusline before calling here.
912
+ // Our job: clear in-process state so the member can accept new calls.
913
+ // clearedByStall prevents the eventually-resolving finally block from clobbering
914
+ // a new execute_prompt that may have already claimed the member.
915
+ inFlightAgents.delete(agent.id);
916
+ clearedByStall = true;
917
+ claimGuardActive = false;
918
+ // apra-fleet-6z8.2: a CONFIRMED stall means the remote turn made no
919
+ // progress of any kind for the whole threshold. Clearing bookkeeping
920
+ // alone left that wedged process running indefinitely on the member --
921
+ // burning its LLM session, holding its work folder, and colliding with
922
+ // whatever dispatch takes the member next. Kill the tracked pid too.
923
+ // Best-effort and never awaited: onStall is a fire-and-forget callback
924
+ // from the poll loop, and tryKillPid already swallows its own errors.
925
+ void tryKillPid(agent, strategy, cmds).catch(() => { });
926
+ // apra-fleet-3c9.1: killing the remote pid alone left the pending
927
+ // execCommand promise still awaiting its full deadline. Abort it now so
928
+ // the dispatch settles promptly and returns a typed 'stalled' error.
929
+ try {
930
+ stallAbortController.abort();
931
+ }
932
+ catch { /* best-effort */ }
933
+ },
934
+ });
935
+ const tmpDir = agent.agentType === 'local' ? os.tmpdir() : '/tmp';
936
+ const resolvedWorkFolder = agent.agentType === 'local' ? resolveTilde(agent.workFolder) : agent.workFolder;
937
+ const promptFilePath = agent.agentType === 'local'
938
+ ? path.join(resolvedWorkFolder, promptFileName)
939
+ : `${resolvedWorkFolder}/${promptFileName}`;
940
+ const strategy = getStrategy(agent);
941
+ const cmds = getOsCommands(getAgentOS(agent), getAgentShell(agent));
942
+ const provider = getProvider(agent.llmProvider);
943
+ const tiers = provider.modelTiers();
944
+ let resolvedModel = input.model || 'standard';
945
+ let resolvedTier;
946
+ if (resolvedModel === 'cheap') {
947
+ resolvedTier = 'cheap';
948
+ resolvedModel = agent.modelTiers
949
+ ? resolveModelForTier(agent, 'cheap', provider)
950
+ : agent.modelCheap || getModelOverride(provider.name, 'cheap') || tiers.cheap;
852
951
  }
853
- catch {
854
- resolvedLogPath = null;
952
+ else if (resolvedModel === 'standard') {
953
+ resolvedTier = 'standard';
954
+ resolvedModel = agent.modelTiers
955
+ ? resolveModelForTier(agent, 'standard', provider)
956
+ : agent.modelStandard || getModelOverride(provider.name, 'standard') || tiers.standard;
855
957
  }
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) {
958
+ else if (resolvedModel === 'premium') {
959
+ resolvedTier = 'premium';
960
+ resolvedModel = agent.modelTiers
961
+ ? resolveModelForTier(agent, 'premium', provider)
962
+ : agent.modelPremium || getModelOverride(provider.name, 'premium') || tiers.premium;
963
+ }
964
+ else {
965
+ resolvedModel = tiers[resolvedModel] ?? resolvedModel;
966
+ }
967
+ 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);
968
+ // Resume semantics (apra-fleet-eft.78.1). `resume` is boolean | string:
969
+ // - true -> best-effort resume of the member's stored last session; a
970
+ // stale/unknown stored session transparently retries fresh.
971
+ // - false -> always a fresh session.
972
+ // - string -> EXPLICIT session-id resume: resume exactly this id, preferring
973
+ // it over agent.sessionId. The caller asserts the prompt depends
974
+ // on that session's context, so an unknown/expired id is a
975
+ // TERMINAL session_not_found (handled just below) and NO
976
+ // fresh-session fallback is ever applied (see the retry paths).
977
+ const explicitResumeId = (typeof input.session_id === 'string' && input.session_id.trim().length > 0)
978
+ ? input.session_id.trim()
979
+ : (typeof input.resume === 'string' && input.resume.length > 0 ? input.resume : undefined);
980
+ // Fork semantics (apra-fleet-lmtg.5) mirror resume's SHAPE but BRANCH instead
981
+ // of continuing in place. `fork` is boolean | string:
982
+ // - true -> best-effort fork of the member's stored last session; a
983
+ // stale/unknown stored session logs a warning and falls back to a
984
+ // plain FRESH session (analogous to resume=true), never a hard error.
985
+ // - string -> EXPLICIT fork of exactly that source id -- an unknown/expired
986
+ // source is a TERMINAL session_not_found (like an explicit resume).
987
+ // The mutual-exclusivity guard above only lets fork through with resume at its
988
+ // DEFAULT (true), so fork must SUPERSEDE that default resume here: a fork
989
+ // request forces resuming off so the dispatch mints a fresh distinct output
990
+ // session id instead of continuing the stored one in place.
991
+ // apra-fleet-3swo.42: derived from the SAME normalised forkArg the top-of-
992
+ // function mutual-exclusivity guard computed forkRequested from (no second,
993
+ // independently-trimmed copy of the rule). Deliberately NOT gated on
994
+ // `.length > 0`: an explicit fork string that trims to '' (fork: '' or
995
+ // fork: ' ') must still be an EXPLICIT id -- just an invalid one -- so it
996
+ // takes the explicit-id branch below (terminal session_not_found, no LLM
997
+ // call) instead of being mistaken for the fork===true best-effort path.
998
+ const explicitForkId = typeof forkArg === 'string' ? forkArg : undefined;
999
+ // forkRequested is already computed at the top of executePrompt for the
1000
+ // fork/resume mutual-exclusivity guard (apra-fleet-lmtg.4) -- reuse it here.
1001
+ const resumeRequested = (input.resume === true || explicitResumeId !== undefined) && !forkRequested;
1002
+ const resumeTargetId = explicitResumeId ?? agent.sessionId;
1003
+ // An explicit-id resume OR an explicit-id fork must never silently degrade to a
1004
+ // fresh session: that is exactly the wrong-context dispatch this feature
1005
+ // forbids. resume=true/false and fork=true keep their transparent recovery.
1006
+ const allowFreshSessionFallback = explicitResumeId === undefined && explicitForkId === undefined;
1007
+ // A DEFAULT resume (resume=true) of a stored session recorded under another
1008
+ // server build -- or before builds were stamped -- starts fresh: that
1009
+ // session's transcript lists tools/MCP config this build no longer serves,
1010
+ // and resuming it can end with no tool run yet a "successful" reply.
1011
+ // Explicit-id resumes are the caller's assertion and are left alone.
1012
+ const staleBuildSession = input.resume === true && explicitResumeId === undefined && !forkRequested
1013
+ && !!agent.sessionId && agent.sessionServerVersion !== serverVersion;
1014
+ if (staleBuildSession) {
1015
+ logWarn('execute_prompt', `"${agent.friendlyName}": stored session "${agent.sessionId}" was recorded by server build ${agent.sessionServerVersion ?? '(unrecorded, older build)'}, this server is ${serverVersion} -- starting a fresh session instead of resuming it`, agent);
1016
+ }
1017
+ const resuming = !!(resumeRequested && resumeTargetId && provider.supportsResume()) && !staleBuildSession;
1018
+ const isCallerMinted = provider.sessionIdStrategy().type === 'caller-minted';
1019
+ const mintedId = isCallerMinted
1020
+ ? (resuming ? resumeTargetId : uuid())
1021
+ : (resuming ? resumeTargetId : undefined);
1022
+ // Terminal session-not-found gate for explicit-id resumes (apra-fleet-eft.78.1).
1023
+ // Checked BEFORE any spawn (writePromptFile / the LLM invocation): if the
1024
+ // caller named a session the server has never issued for this member -- and it
1025
+ // is not the member's currently-stored session -- there is no context to
1026
+ // resume. Reject with a structured session_not_found and make NO LLM call,
1027
+ // so an orchestrator can rebuild a self-contained prompt and re-dispatch fresh
1028
+ // deliberately, rather than getting a silent blank-session response.
1029
+ if (explicitResumeId !== undefined) {
1030
+ const resumable = provider.supportsResume()
1031
+ && (isKnownSession(agent.id, explicitResumeId) || explicitResumeId === agent.sessionId);
1032
+ if (!resumable) {
1033
+ scope.abort(`explicit resume rejected -- session "${explicitResumeId}" is unknown/expired (no LLM call)`);
923
1034
  inFlightAgents.delete(agent.id);
924
1035
  stallDetector.remove(agent.id);
925
1036
  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, '/')}`;
1037
+ return {
1038
+ 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.`,
1039
+ structuredContent: { isError: true, reason: 'session_not_found', sessionId: explicitResumeId },
1040
+ };
927
1041
  }
928
1042
  }
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) {
1043
+ // Fork mode resolution (apra-fleet-lmtg.5). Resolved AFTER the resume gate
1044
+ // (the two are mutually exclusive, guarded before member resolution). Fork
1045
+ // requires a fork-capable provider; when active it produces a ForkDescriptor
1046
+ // that buildAgentPromptCommand (fork-prov lane, apra-fleet-lmtg.2) turns into
1047
+ // the provider's own source-seeded, new-session-id fork invocation.
1048
+ let forkActive = false;
1049
+ let forkSourceId;
1050
+ if (forkRequested) {
1051
+ // fork requires provider.supportsFork(): a fork request against a provider
1052
+ // whose CLI cannot fork is surfaced as a clear TERMINAL error, never
1053
+ // silently downgraded to a plain fresh/resume dispatch (the wrong behavior).
1054
+ if (!provider.supportsFork?.()) {
1055
+ scope.abort(`fork rejected -- provider "${provider.name}" does not support fork-mode dispatch (no LLM call)`);
961
1056
  inFlightAgents.delete(agent.id);
962
1057
  stallDetector.remove(agent.id);
963
1058
  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}`;
1059
+ return {
1060
+ 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.`,
1061
+ structuredContent: { isError: true, reason: 'fork_unsupported' },
1062
+ };
1063
+ }
1064
+ if (explicitForkId !== undefined) {
1065
+ // Terminal source-not-found gate, mirroring the explicit-resume gate: an
1066
+ // unknown/expired source id has no context to branch from, so reject
1067
+ // BEFORE any spawn with a structured session_not_found and NO LLM call.
1068
+ const forkable = isKnownSession(agent.id, explicitForkId) || explicitForkId === agent.sessionId;
1069
+ if (!forkable) {
1070
+ scope.abort(`explicit fork rejected -- source session "${explicitForkId}" is unknown/expired (no LLM call)`);
1071
+ inFlightAgents.delete(agent.id);
1072
+ stallDetector.remove(agent.id);
1073
+ writeStatusline(new Map([[agent.id, 'idle']]));
1074
+ return {
1075
+ 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.`,
1076
+ structuredContent: { isError: true, reason: 'session_not_found', sessionId: explicitForkId },
1077
+ };
1078
+ }
1079
+ forkSourceId = explicitForkId;
1080
+ forkActive = true;
1081
+ }
1082
+ else {
1083
+ // fork === true: best-effort branch from the member's stored last session.
1084
+ // A stale/unknown stored session (or none at all) is NOT a hard error --
1085
+ // log a warning and fall through to a plain FRESH session, mirroring
1086
+ // resume=true's transparent recovery.
1087
+ const stored = agent.sessionId;
1088
+ if (stored && isKnownSession(agent.id, stored)) {
1089
+ forkSourceId = stored;
1090
+ forkActive = true;
1091
+ }
1092
+ else {
1093
+ scope.info(`fork=true: stored session ${stored ? `"${stored}" is stale/unknown` : 'is absent'} -- falling back to a fresh session`);
1094
+ forkActive = false;
1095
+ }
965
1096
  }
966
1097
  }
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,
1098
+ // Mint a fresh distinct output session id for the forked conversation (never
1099
+ // reuse the source id): mintedId is already a freshly minted uuid here because
1100
+ // fork forced resuming off above (see resumeRequested). It IS emitted as an
1101
+ // explicit `--session-id` CLI flag in fork mode (see ForkDescriptor.newSessionId
1102
+ // and provider.forkFlag) -- the CLI honors a caller-supplied session id even
1103
+ // when forking, so we pre-mint and pass it rather than scraping the CLI's own
1104
+ // minted id back out of the response afterward. The same id also drives
1105
+ // recordKnownSession, the stall-detector log path, and the post-dispatch
1106
+ // session bookkeeping below.
1107
+ const forkDescriptor = forkActive && forkSourceId
1108
+ ? { sourceSessionId: forkSourceId, newSessionId: mintedId ?? uuid() }
1109
+ : undefined;
1110
+ // Per-session member MCP config (Claude --mcp-config): the dispatched
1111
+ // session gets the ?member=<uuid> apra-fleet server whatever the folder or
1112
+ // user config says. Path resolved here so every retry (freshOpts spreads
1113
+ // promptOpts) carries it; the file is written with the prompt file below,
1114
+ // and a failed write clears it (fallback: the session's own MCP config).
1115
+ const sessionMcpPath = sessionMcpInjectionAvailable(agent)
1116
+ ? sessionMcpConfigPath(agent, resolvedWorkFolder)
1117
+ : undefined;
1118
+ const promptOpts = {
1119
+ folder: resolvedWorkFolder,
1120
+ promptFile: promptFileName,
978
1121
  sessionId: mintedId,
979
- demand: contextDemand,
1122
+ resuming,
1123
+ unattended: agent.unattended,
1124
+ model: resolvedModel,
1125
+ tier: resolvedTier,
1126
+ maxTurns: input.max_turns,
1127
+ inv: scope.getInv(),
1128
+ agentName: input.agent,
1129
+ fork: forkDescriptor,
1130
+ projectId: agyProjectId,
1131
+ ...(sessionMcpPath ? { mcpConfigPath: sessionMcpPath } : {}),
1132
+ };
1133
+ // apra-fleet issue #390: session log paths live on the MEMBER's machine, under
1134
+ // the MEMBER's home directory, joined with the MEMBER's OS convention. Before
1135
+ // this, every remote member got a HUB-home path (os.homedir()) joined with the
1136
+ // HUB's path convention -- a path that can never exist on the member, which
1137
+ // silently disabled stall detection for Claude and manufactured
1138
+ // false-positive stall kills for AGY/OpenCode.
1139
+ //
1140
+ // This resolution is deliberately SYNCHRONOUS (cached probe result, else the
1141
+ // member's known login username's default home): the dispatch path must not
1142
+ // add a remote round trip, and a wrong guess here can only cost detection
1143
+ // fidelity, never cause a kill. The kill-capable directory poll in
1144
+ // stall-poller.ts uses the probe-backed async resolver instead.
1145
+ const memberPathCtx = getCachedMemberPathContext(agent);
1146
+ const activePreSpawnSid = resuming ? resumeTargetId : (isCallerMinted ? mintedId : undefined);
1147
+ let resolvedLogPath = null;
1148
+ if (activePreSpawnSid) {
1149
+ try {
1150
+ resolvedLogPath = resolveSessionLogPath(agent.llmProvider ?? 'claude', activePreSpawnSid, resolvedWorkFolder, memberPathCtx.homeDir, memberPathCtx.targetOs);
1151
+ }
1152
+ catch {
1153
+ resolvedLogPath = null;
1154
+ }
1155
+ }
1156
+ // GitHub #562: a path built from a local or probed home is authoritative --
1157
+ // if it never appears, the agent never started. A username-convention guess
1158
+ // is not, and a missing file there stays a warning.
1159
+ // Known causes of a transcript landing somewhere else make the path a
1160
+ // guess too: a relocated Claude config dir (CLAUDE_CONFIG_DIR, in this
1161
+ // process for a local member or in the member's configured env vars), or a
1162
+ // local POSIX work folder reached through a symlink (the CLI names the
1163
+ // project dir after the physical cwd).
1164
+ const claudeConfigRelocated = (agent.llmProvider ?? 'claude') === 'claude' && (Object.keys(agent.encryptedEnvVars ?? {}).includes('CLAUDE_CONFIG_DIR')
1165
+ || (agent.agentType === 'local' && !!process.env.CLAUDE_CONFIG_DIR));
1166
+ let localCwdIsSymlinked = false;
1167
+ if (agent.agentType === 'local' && process.platform !== 'win32') {
1168
+ try {
1169
+ const real = fs.realpathSync(resolvedWorkFolder);
1170
+ localCwdIsSymlinked = real.replace(/\/+$/, '') !== path.resolve(resolvedWorkFolder).replace(/\/+$/, '');
1171
+ }
1172
+ catch {
1173
+ localCwdIsSymlinked = true; // cannot resolve -> not authoritative
1174
+ }
1175
+ }
1176
+ const logPathAuthoritative = (memberPathCtx.source === 'local' || memberPathCtx.source === 'probe')
1177
+ && !claudeConfigRelocated && !localCwdIsSymlinked;
1178
+ stallDetector.update(agent.id, {
1179
+ sessionId: activePreSpawnSid,
1180
+ logFilePath: resolvedLogPath,
1181
+ provisional: !resolvedLogPath,
1182
+ thresholdMs: stallThresholdMs,
1183
+ logPathAuthoritative: !!resolvedLogPath && logPathAuthoritative,
980
1184
  });
981
- if (!admission.allowed) {
982
- scope.abort(`insufficient context headroom: demand=${admission.detail.demand} headroom=${admission.detail.headroom} window=${admission.detail.window}`);
1185
+ let claudeCmd = cmds.buildAgentPromptCommand(provider, promptOpts);
1186
+ // apra-fleet-6z8.1: the per-invocation durable stdout mirror the unix prompt
1187
+ // wrapper tees to (see durableOutputPath / buildAgentPromptCommand). A
1188
+ // PowerShell Windows member has no such companion tee, so recovery is
1189
+ // skipped for it.
1190
+ //
1191
+ // apra-fleet-7dir.5.4: a Windows member registered as gitbash runs
1192
+ // WindowsGitBashCommands, which INHERITS LinuxCommands.buildAgentPromptCommand
1193
+ // -- so it has already been teeing its stdout to /tmp/.fleet-out-<inv>.json
1194
+ // on every dispatch, with nothing reading it and (because extraPaths was
1195
+ // empty) nothing deleting it. Enabling the mirror for gitbash both turns
1196
+ // recovery on for that member class and makes the file get cleaned up.
1197
+ // This gate and the `unsupported` flag passed to recoverOrphanedDispatch
1198
+ // below are ONE decision: recoverOrphanedDispatch short-circuits on
1199
+ // `unsupported || !durablePath`, so they must always agree -- hence the
1200
+ // single shared predicate.
1201
+ //
1202
+ // LOCAL agents are deliberately excluded from the flip: orphan recovery
1203
+ // exists for a torn-down SSH channel, which a local spawn does not have, and
1204
+ // deletePromptFile's local branch would fs.unlinkSync('/tmp/...') through
1205
+ // Node on Windows (resolving to C:\tmp), not the MSYS /tmp bash teed to.
1206
+ // Non-Windows members (local or remote) keep today's behaviour verbatim.
1207
+ const durableMirrorSupported = getAgentOS(agent) !== 'windows'
1208
+ || (getAgentShell(agent) === 'gitbash' && agent.agentType !== 'local');
1209
+ const durablePath = durableMirrorSupported ? durableOutputPath(scope.getInv()) : undefined;
1210
+ // GitHub #563: the shared deadline is anchored at handler entry, not here --
1211
+ // setup awaits above (cloud readiness, preflight, provisioning) count.
1212
+ const dispatchStartedAt = handlerStartedAt;
1213
+ const maxTotalMs = input.max_total_s !== undefined ? input.max_total_s * 1000 : undefined;
1214
+ // Time kept back from every attempt's budget for the finally-block prompt
1215
+ // file cleanup and the response, so the whole call settles inside
1216
+ // max_total_s and well within the client's max_total_s + grace window.
1217
+ // Fixed 5s, capped at 10% of a very small max_total_s.
1218
+ const cleanupReserveMs = maxTotalMs !== undefined
1219
+ ? Math.min(EXECUTE_PROMPT_CLEANUP_RESERVE_MS, Math.floor(maxTotalMs / 10))
1220
+ : 0;
1221
+ // apra-fleet-25yl.2.1: the exec-level ROLLING (inactivity) deadline handed to
1222
+ // strategy.execCommand() is no longer provider-blind. It used to be
1223
+ // `timeout_s` for everyone, which is a false kill for the batch-only
1224
+ // providers (they emit nothing on this channel until the turn ends) and the
1225
+ // only working stall signal for the providers with no pollable transcript.
1226
+ // The per-provider answer lives in ONE named place -- ProviderAdapter
1227
+ // .execTimeoutSource() -- so a newly added provider must state its own
1228
+ // (a compile error if it does not) instead of inheriting a default branch.
1229
+ //
1230
+ // NOTE: input.timeout_s still reaches the StallDetector as thresholdMs for
1231
+ // EVERY provider (see stallThresholdMs above). This decision governs the
1232
+ // exec-channel timer only; it must not be used to skip that threading.
1233
+ const execTimeoutSource = provider.execTimeoutSource();
1234
+ const timeoutMs = execTimeoutSource === 'inactivity_timeout'
1235
+ ? (input.timeout_s ?? 300) * 1000
1236
+ // 'total_ceiling': mirror max_total_s, which can never bind before the
1237
+ // caller's own hard ceiling does (a rolling inactivity window of
1238
+ // max_total_s starts at dispatch start and only ever resets later).
1239
+ // max_total_s ABSENT: there is no ceiling to mirror, so use a documented
1240
+ // never-binds-first constant rather than silently falling back to
1241
+ // timeout_s (which is exactly the coupling this change removes). It is
1242
+ // deliberately a finite value well inside the int32 range setTimeout
1243
+ // accepts -- Infinity or Number.MAX_SAFE_INTEGER would overflow and fire
1244
+ // on the next tick, inverting this fix into an instant kill.
1245
+ : (maxTotalMs ?? EXEC_TIMER_NEVER_BINDS_MS);
1246
+ // apra-fleet-y8q.1: every retry below (dispatch-exception, stale-session,
1247
+ // server-overloaded) re-dispatches with a FRESH session but used to reuse the
1248
+ // SAME full timeoutMs/maxTotalMs as the original attempt -- so a single
1249
+ // dispatch could burn up to ~2x max_total_s server-side (original attempt +
1250
+ // one full-budget retry), well past what the client's deriveTimeoutMs()
1251
+ // (packages/apra-fleet-client/src/client/api.mjs) budgets for the whole
1252
+ // tools/call (max_total_s*1000 + a fixed grace margin). That let the
1253
+ // client's hard timeout fire before the server's own retry-and-report path
1254
+ // ever got a chance, hiding a clean typed server error behind a raw client
1255
+ // transport timeout. Share ONE deadline budget across the original attempt
1256
+ // and any single retry: cap a retry's maxTotalMs (and its inactivity
1257
+ // timeoutMs, so it can't independently outlast the shared ceiling) to
1258
+ // whatever remains of max_total_s since dispatchStartedAt, and skip the
1259
+ // retry entirely once that budget is exhausted -- so total wall-clock time
1260
+ // for this call never exceeds max_total_s, which is exactly what the client
1261
+ // is prepared to wait for. When max_total_s is absent there is no hard
1262
+ // ceiling to share, so retries keep their full timeout_s (unchanged,
1263
+ // pre-existing behavior).
1264
+ function retryBudget() {
1265
+ if (maxTotalMs === undefined)
1266
+ return { timeoutMs, maxTotalMs: undefined, exhausted: false };
1267
+ const remaining = Math.max(0, maxTotalMs - cleanupReserveMs - (Date.now() - dispatchStartedAt));
1268
+ return { timeoutMs: Math.min(timeoutMs, remaining), maxTotalMs: remaining, exhausted: remaining <= 0 };
1269
+ }
1270
+ // Agent file validation -- verify named agent exists before any CLI invocation
1271
+ if (input.agent) {
1272
+ const dirs = provider.agentDirectories(input.agent);
1273
+ let agentFound = false;
1274
+ if (agent.agentType === 'local') {
1275
+ const projPath = path.join(resolvedWorkFolder, dirs.project);
1276
+ const userPath = path.join(os.homedir(), dirs.home);
1277
+ agentFound = fs.existsSync(projPath) || fs.existsSync(userPath);
1278
+ if (!agentFound) {
1279
+ inFlightAgents.delete(agent.id);
1280
+ stallDetector.remove(agent.id);
1281
+ writeStatusline(new Map([[agent.id, 'idle']]));
1282
+ return `execute_prompt: agent "${input.agent}" not found.\n\nExpected at:\n ${projPath.replace(/\\/g, '/')}\n ${userPath.replace(/\\/g, '/')}`;
1283
+ }
1284
+ }
1285
+ else {
1286
+ // Canonical PM role agents (planner/doer/reviewer/plan-reviewer/...) are
1287
+ // already guaranteed present in ~/dirs.home by
1288
+ // ensureAgentFilesProvisioned() above (ln ~576, which ran provisionAgents()
1289
+ // for this exact member earlier in this same call) -- trust that instead
1290
+ // of re-probing the remote here. This also SIDESTEPS the bug this check
1291
+ // used to have: the old code hand-rolled a POSIX-only
1292
+ // `test -f ... || test -f ...` command run via strategy.execCommand(),
1293
+ // which throws a PowerShell parser error (not a POSIX shell) on every
1294
+ // Windows remote -- a nonzero exit that this check misread as "agent not
1295
+ // found" even when the file genuinely existed (apra-fleet P0 bug, fleet-
1296
+ // sprint via a Windows remote member always failed plan-review with
1297
+ // "agent 'plan-reviewer' not found").
1298
+ const canonicalRelPath = `${input.agent}.md`;
1299
+ agentFound = remoteAgentsDir(provider.name) !== null
1300
+ && loadCanonicalAgentSet(provider.name).some((f) => f.relPath === canonicalRelPath);
1301
+ if (!agentFound) {
1302
+ // Not part of the canonical PM set (e.g. a project-local custom
1303
+ // agent) -- fall back to a REAL existence probe, built platform-aware
1304
+ // via this repo's own getOsCommands() abstraction (src/os/*.ts,
1305
+ // already used the same way by list-members.ts/member-detail.ts for
1306
+ // credential-file checks) instead of a single hardcoded shell dialect.
1307
+ const cmds = getOsCommands(getAgentOS(agent), getAgentShell(agent));
1308
+ const projPath = `${resolvedWorkFolder}/${dirs.project}`;
1309
+ const userPath = `~/${dirs.home}`;
1310
+ const [projResult, userResult] = await Promise.all([
1311
+ strategy.execCommand(cmds.credentialFileCheck(projPath), 10000),
1312
+ strategy.execCommand(cmds.credentialFileCheck(userPath), 10000),
1313
+ ]);
1314
+ agentFound = projResult.stdout.includes('found') || userResult.stdout.includes('found');
1315
+ }
1316
+ if (!agentFound) {
1317
+ inFlightAgents.delete(agent.id);
1318
+ stallDetector.remove(agent.id);
1319
+ writeStatusline(new Map([[agent.id, 'idle']]));
1320
+ return `execute_prompt: agent "${input.agent}" not found on "${agent.friendlyName}".\n\nExpected at:\n ${resolvedWorkFolder}/${dirs.project}\n ~/${dirs.home}`;
1321
+ }
1322
+ }
1323
+ }
1324
+ // Context-headroom admission control (apra-fleet-eft.81.1): a declared
1325
+ // demand is checked against this session's remaining headroom BEFORE any
1326
+ // CLI is spawned. No declared demand (both fields omitted) means this
1327
+ // block is skipped entirely -- pre-existing behavior is unchanged.
1328
+ const contextDemand = resolveExpectedDemand(input.expected_context_tokens, input.context_size);
1329
+ let contextWarning;
1330
+ if (contextDemand !== undefined) {
1331
+ const admission = checkContextAdmission({
1332
+ provider: provider.name,
1333
+ resolvedModel,
1334
+ sessionId: mintedId,
1335
+ demand: contextDemand,
1336
+ });
1337
+ if (!admission.allowed) {
1338
+ scope.abort(`insufficient context headroom: demand=${admission.detail.demand} headroom=${admission.detail.headroom} window=${admission.detail.window}`);
1339
+ inFlightAgents.delete(agent.id);
1340
+ stallDetector.remove(agent.id);
1341
+ writeStatusline(new Map([[agent.id, 'idle']]));
1342
+ return {
1343
+ 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.`,
1344
+ structuredContent: { isError: true, reason: 'insufficient_context_headroom', detail: admission.detail },
1345
+ };
1346
+ }
1347
+ if (admission.warning) {
1348
+ contextWarning = { message: admission.warning, detail: admission.detail };
1349
+ }
1350
+ }
1351
+ // Usage/budget admission (apra-fleet-eft.80.2): when a budget is configured
1352
+ // for this member (or its workspace), a hard-threshold crossing rejects the
1353
+ // NEW dispatch BEFORE any LLM is spawned -- never killing an in-flight call.
1354
+ // No configured budget (the common case) means resolveBudgetScope returns
1355
+ // undefined and this block is skipped entirely -- behavior is unchanged.
1356
+ const budgetScope = resolveBudgetScope([agent.id, workspaceId]);
1357
+ if (budgetScope) {
1358
+ const preBudget = await evaluateBudget({ scope: budgetScope, agent, provider });
1359
+ if (preBudget?.exhausted) {
1360
+ scope.abort(`budget exhausted: scope=${budgetScope} spent=${preBudget.block.spent} budget=${preBudget.block.budget} source=${preBudget.block.source}`);
1361
+ inFlightAgents.delete(agent.id);
1362
+ stallDetector.remove(agent.id);
1363
+ writeStatusline(new Map([[agent.id, 'idle']]));
1364
+ return {
1365
+ 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.`,
1366
+ structuredContent: { isError: true, reason: 'budget_exhausted', budgetUsage: preBudget.block },
1367
+ };
1368
+ }
1369
+ }
1370
+ // Stage the first attempt's credential file BEFORE touching the member
1371
+ // (kill, prompt write, dispatch). A delivery failure (relay member, SFTP
1372
+ // subsystem disabled) is deterministic: a typed failure, never retried.
1373
+ const stagedEnvPaths = [];
1374
+ /** Per-dispatch session MCP config files to remove with the prompt file. */
1375
+ const sessionMcpCleanup = [];
1376
+ let preStaged;
1377
+ try {
1378
+ preStaged = await stageAuthEnv(agent);
1379
+ }
1380
+ catch (err) {
1381
+ if (!(err instanceof SecretDeliveryError))
1382
+ throw err;
1383
+ scope.abort(`secret delivery unavailable (no LLM call)`);
983
1384
  inFlightAgents.delete(agent.id);
984
1385
  stallDetector.remove(agent.id);
985
1386
  writeStatusline(new Map([[agent.id, 'idle']]));
986
1387
  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 },
1388
+ text: `[FAIL] execute_prompt on "${agent.friendlyName}" rejected -- ${err.message} No LLM call was made.`,
1389
+ structuredContent: { isError: true, reason: 'secret_delivery_unavailable' },
989
1390
  };
990
1391
  }
991
- if (admission.warning) {
992
- contextWarning = { message: admission.warning, detail: admission.detail };
1392
+ if (preStaged.path)
1393
+ stagedEnvPaths.push(preStaged.path);
1394
+ try {
1395
+ // Kill any leftover session from a previous (possibly zombie) execute_prompt call
1396
+ await tryKillPid(agent, strategy, cmds);
1397
+ // Write the rendered prompt (with substitutions applied) to the prompt file before execution
1398
+ await writePromptFile(agent, strategy, promptFilePath, renderedPrompt);
993
1399
  }
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}`);
1400
+ catch (err) {
1401
+ if (preStaged.path)
1402
+ await removeMemberSecretFile(agent, preStaged.path);
1403
+ throw err;
1404
+ }
1405
+ if (sessionMcpPath) {
1406
+ const memberExec = (cmd, t) => strategy.execCommand(cmd, t);
1407
+ // Non-deferral of the member's kb_*/code_* tools (Claude: alwaysLoad in
1408
+ // the session config). When it cannot be applied the config is still
1409
+ // attached, the tools stay deferred, and the cause is logged.
1410
+ const loadPolicy = await resolveSessionMcpAlwaysLoad(agent, provider.mcpAlwaysLoadMinVersion?.(), cmds.agentVersion(provider), memberExec);
1411
+ if (loadPolicy.warning)
1412
+ scope.warn(`session MCP config: ${loadPolicy.warning}`);
1413
+ const wrote = await writeSessionMcpConfig(agent, sessionMcpPath, memberExec, { alwaysLoad: loadPolicy.alwaysLoad });
1414
+ if (wrote.ok) {
1415
+ if (sessionMcpConfigIsPerDispatch(agent))
1416
+ sessionMcpCleanup.push(sessionMcpPath);
1417
+ }
1418
+ else {
1419
+ // Fallback, never zero: dispatch without the flag; the session then
1420
+ // uses the per-folder / user-scope apra-fleet entry it would have had.
1421
+ scope.warn(`session MCP config not written (${wrote.detail}); dispatching without --mcp-config`);
1422
+ delete promptOpts.mcpConfigPath;
1423
+ claudeCmd = cmds.buildAgentPromptCommand(provider, promptOpts);
1424
+ if (sessionMcpConfigIsPerDispatch(agent))
1425
+ sessionMcpCleanup.push(sessionMcpPath);
1426
+ }
1427
+ }
1428
+ // apra-fleet-6z8.1: remembered for the lease-of-life gate below -- the pid
1429
+ // outlives the SSH channel that reported it, and is the only way to tell a
1430
+ // fabricated "exit 0 / empty" close apart from a real one.
1431
+ let capturedPid;
1432
+ const onPidCaptured = (pid) => {
1433
+ capturedPid = pid;
1434
+ scope.info(`pid=${pid}`);
1435
+ if (mintedId) {
1436
+ let logPath = null;
1437
+ try {
1438
+ logPath = resolveSessionLogPath(agent.llmProvider ?? 'claude', mintedId, resolvedWorkFolder, memberPathCtx.homeDir, memberPathCtx.targetOs);
1439
+ }
1440
+ catch {
1441
+ logPath = null;
1442
+ }
1443
+ stallDetector.update(agent.id, {
1444
+ sessionId: mintedId,
1445
+ logFilePath: logPath,
1446
+ provisional: !logPath,
1447
+ thresholdMs: stallThresholdMs,
1448
+ logPathAuthoritative: !!logPath && logPathAuthoritative,
1449
+ });
1450
+ }
1451
+ };
1452
+ const abortHandler = () => {
1453
+ scope.abort('cancelled by MCP client');
1454
+ tryKillPid(agent, strategy, cmds).catch(() => { });
1455
+ };
1456
+ extra?.signal?.addEventListener('abort', abortHandler);
1457
+ // apra-fleet-3c9.1: the signal handed to execCommand fires on EITHER the MCP
1458
+ // client's cancellation OR a confirmed stall (stallAbortController). Merging
1459
+ // them means a stall aborts the pending dispatch exactly as a client cancel
1460
+ // would, while a live (non-stalled) dispatch -- whose controller is never
1461
+ // aborted -- is left completely untouched.
1462
+ const dispatchSignal = extra?.signal
1463
+ ? AbortSignal.any([extra.signal, stallAbortController.signal])
1464
+ : stallAbortController.signal;
1465
+ // GitHub #563 review: when the inactivity window is as long as the attempt's
1466
+ // remaining max_total_s budget (timeout_s >= max_total_s -- every
1467
+ // fleet-sprint dispatch, and Claude's total-ceiling mode), both timers come
1468
+ // due together and the inactivity one, armed first, wins the tie. That
1469
+ // kill IS the max_total_s ceiling, so it is classified as max_total_time.
1470
+ let lastAttempt;
1471
+ // Stored auth env vars reach the CLI through a per-attempt owner-only file
1472
+ // the command loads and deletes (stageAuthEnv) -- never as inline values in
1473
+ // the command line, which would sit in the member shell's argv (readable
1474
+ // via ps) for the whole dispatch. Each attempt stages its own file because
1475
+ // the prefix deletes it once loaded; any file a failed attempt left behind
1476
+ // is removed with the prompt file at the end.
1477
+ // The first attempt uses the file staged before dispatch (preStaged).
1478
+ // The session whose saved cost total the last dispatched invocation started
1479
+ // from: the resumed session (or a fork's source) for the original command,
1480
+ // nothing for every fresh-session retry. Read by parseDispatch below.
1481
+ // Compared against the UNPREFIXED cmd: the auth-env prefix is added here,
1482
+ // per attempt, so it never affects the resume-cost-delta decision.
1483
+ let lastAttemptContinues;
1484
+ const dispatchAttempt = async (cmd, attemptTimeoutMs, attemptMaxTotalMs) => {
1485
+ lastAttempt = { timeoutMs: attemptTimeoutMs, maxTotalMs: attemptMaxTotalMs };
1486
+ lastAttemptContinues = cmd === claudeCmd
1487
+ ? (forkDescriptor ? forkDescriptor.sourceSessionId : (resuming ? resumeTargetId : undefined))
1488
+ : undefined;
1489
+ let staged = preStaged;
1490
+ preStaged = undefined;
1491
+ if (!staged) {
1492
+ staged = await stageAuthEnv(agent);
1493
+ if (staged.path)
1494
+ stagedEnvPaths.push(staged.path);
1495
+ }
1496
+ return strategy.execCommand(staged.prefix + cmd, attemptTimeoutMs, attemptMaxTotalMs, onPidCaptured, dispatchSignal);
1497
+ };
1498
+ const isMaxTotalKill = (err) => {
1499
+ const msg = err?.message;
1500
+ if (maxTotalMs === undefined || typeof msg !== 'string')
1501
+ return false;
1502
+ if (/exceeded max total time/i.test(msg))
1503
+ return true;
1504
+ return /of inactivity/i.test(msg) && lastAttempt?.maxTotalMs !== undefined
1505
+ && lastAttempt.timeoutMs >= lastAttempt.maxTotalMs;
1506
+ };
1507
+ // Mark agent as busy in statusline
1508
+ writeStatusline(new Map([[agent.id, 'busy']]));
1509
+ let _epExitCode = 'error';
1510
+ let _epError;
1511
+ let _epUsage;
1512
+ let _epOffline = false;
1513
+ // The last session id a provider parse produced. Typed failures that follow a
1514
+ // parse (max_turns_exhausted, empty_response, dispatch_failed) carry it so an
1515
+ // explicit-id resume of that exact session passes isKnownSession.
1516
+ let _epParsedSid;
1517
+ const sessionIdField = (sid) => {
1518
+ if (!sid)
1519
+ return {};
1520
+ recordKnownSession(agent.id, sid);
1521
+ return { sessionId: sid };
1522
+ };
1523
+ // GitHub #563: the typed result for a dispatch that ran out of max_total_s
1524
+ // (measured from handler entry), so callers never see a raw transport timeout.
1525
+ // dispatched=false: the budget ran out before anything was sent to the
1526
+ // member, so callers can skip publishing work that cannot exist.
1527
+ const maxTotalTimeResult = (dispatched = true) => ({
1528
+ text: dispatched
1529
+ ? `[FAIL] execute_prompt on "${agent.friendlyName}" exceeded max_total_s (${input.max_total_s}s, measured from the call, including setup) -- the dispatch was stopped.`
1530
+ : `[FAIL] execute_prompt on "${agent.friendlyName}" exceeded max_total_s (${input.max_total_s}s, measured from the call) during setup -- nothing was dispatched.`,
1531
+ structuredContent: { isError: true, reason: 'max_total_time', ...(dispatched ? {} : { dispatched: false }) },
1532
+ });
1533
+ // apra-fleet-6a7.1: gates the exit-0/empty-stdout workspace_not_trusted
1534
+ // self-heal-and-retry below to exactly one attempt per call, mirroring
1535
+ // runGitStep's authHealAttempted shape (fleet-sprint/runner.js:616) -- a
1536
+ // repeat trust failure after the heal is terminal, never looped.
1537
+ let trustHealAttempted = false;
1538
+ // apra-fleet-hzeb.2: a provider usage/quota limit (e.g. Claude's 429) can
1539
+ // never be cured by a fresh session, so it must short-circuit the
1540
+ // stale-session and server-overloaded retries below rather than burning the
1541
+ // shared retryBudget() waiting out a window that is already known. Checked
1542
+ // immediately after EVERY provider.parseResponse(result) call in the
1543
+ // dispatch path (initial dispatch, stale-session retry, overloaded retry,
1544
+ // orphan recovery, trust-heal retry) -- and regardless of result.code,
1545
+ // since a 0-exit result event whose text carries the limit message must not
1546
+ // be returned as a success response either.
1547
+ // The member's OS reaches the parser (agy's permission-denial hint differs on Windows).
1548
+ // unattended + model let the provider tell which permission mode the
1549
+ // session ran in (Claude: a denial in auto mode is never healable).
1550
+ const parseCtx = { agentOs: agent.os, unattended: agent.unattended, model: resolvedModel };
1551
+ // Session-id assertion: the returned id must match the one we minted/resumed.
1552
+ // apra-fleet-lmtg.5: a fork now gets the SAME assertion as any other
1553
+ // caller-minted session. mintedId is explicitly passed to the CLI as
1554
+ // --session-id even in fork mode (provider.forkFlag/buildForkFlag), and the
1555
+ // CLI honors it -- so it is no longer a placeholder the CLI silently
1556
+ // discards, and a genuine mismatch is a real signal worth catching rather
1557
+ // than something to exempt. Shared by the success path and the
1558
+ // permission_denied early return so the two cannot drift apart.
1559
+ const sessionExpectation = (p) => {
1560
+ const expectedSid = resuming ? resumeTargetId : (isCallerMinted ? mintedId : undefined);
1561
+ return { expectedSid, isMismatch: !!(expectedSid && p.sessionId && p.sessionId !== expectedSid) };
1562
+ };
1563
+ // A mismatch on an EXPLICIT resume/fork with no fresh-session fallback is
1564
+ // terminal: the caller asked for that exact session and did not get it.
1565
+ const mismatchIsTerminal = () => !allowFreshSessionFallback && (explicitResumeId !== undefined || explicitForkId !== undefined);
1566
+ // The terminal session_not_found failure for such a mismatch, with the
1567
+ // returned session's usage still recorded. `extra` rides on structuredContent
1568
+ // (the permission_denied path attaches its denial).
1569
+ const terminalMismatchFailure = async (p, expectedSid, extra = {}) => {
1005
1570
  inFlightAgents.delete(agent.id);
1006
1571
  stallDetector.remove(agent.id);
1007
1572
  writeStatusline(new Map([[agent.id, 'idle']]));
1573
+ clearStoredPid(agent.id);
1574
+ if (p.sessionId) {
1575
+ recordKnownSession(agent.id, p.sessionId);
1576
+ }
1577
+ if (p.usage) {
1578
+ const prev = agent.tokenUsage ?? { input: 0, output: 0 };
1579
+ updateAgent(agent.id, {
1580
+ tokenUsage: {
1581
+ input: prev.input + p.usage.input_tokens,
1582
+ output: prev.output + p.usage.output_tokens,
1583
+ },
1584
+ });
1585
+ recordSessionUsage(p.sessionId ?? expectedSid, p.usage);
1586
+ if (budgetScope) {
1587
+ await recordAndEvaluate({ scope: budgetScope, agent, provider, tier: resolvedTier, usage: p.usage });
1588
+ }
1589
+ }
1008
1590
  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 },
1591
+ text: `[FAIL] execute_prompt on "${agent.friendlyName}" failed -- resumed session mismatch. Expected session "${expectedSid}", but provider returned "${p.sessionId}".`,
1592
+ structuredContent: {
1593
+ isError: true,
1594
+ reason: 'session_not_found',
1595
+ sessionId: expectedSid,
1596
+ returnedSessionId: p.sessionId,
1597
+ ...(p.usage ? { usage: toStructuredUsage(p.usage) } : {}),
1598
+ ...extra,
1599
+ },
1011
1600
  };
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;
1601
+ };
1602
+ // apra-fleet-eft.78.1: mark the session this dispatch actually landed on as
1603
+ // known/resumable for this member (so a later explicit-id resume of it passes
1604
+ // the terminal session_not_found gate), and hand the stall detector the
1605
+ // session's real log path.
1606
+ const recordLandedSession = (finalSid) => {
1607
+ if (!finalSid)
1608
+ return;
1609
+ recordKnownSession(agent.id, finalSid);
1610
+ let postLogPath = null;
1027
1611
  try {
1028
- logPath = resolveSessionLogPath(agent.llmProvider ?? 'claude', mintedId, resolvedWorkFolder, memberPathCtx.homeDir, memberPathCtx.targetOs);
1612
+ postLogPath = resolveSessionLogPath(agent.llmProvider ?? 'claude', finalSid, resolvedWorkFolder, memberPathCtx.homeDir, memberPathCtx.targetOs);
1029
1613
  }
1030
1614
  catch {
1031
- logPath = null;
1615
+ postLogPath = null;
1032
1616
  }
1033
1617
  stallDetector.update(agent.id, {
1034
- sessionId: mintedId,
1035
- logFilePath: logPath,
1036
- provisional: !logPath,
1618
+ sessionId: finalSid,
1619
+ logFilePath: postLogPath,
1620
+ provisional: !postLogPath,
1621
+ thresholdMs: stallThresholdMs,
1622
+ logPathAuthoritative: !!postLogPath && logPathAuthoritative,
1037
1623
  });
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
- }
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);
1105
- }
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
- };
1122
- }
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;
1624
+ };
1625
+ // The session/usage/budget/PID bookkeeping a dispatch that RAN gets, for an
1626
+ // early return that is not the success path (permission_denied): the
1627
+ // member's stored session advances (a mismatched id is not persisted, as on
1628
+ // the success path) so a later resume continues this turn, the stall
1629
+ // detector learns the landed session, the spend reaches the member total,
1630
+ // the session total and the budget, and the stored PID is cleared. Same
1631
+ // order and side effects as the success path.
1632
+ const recordDispatchBookkeeping = async (p, expectation = sessionExpectation(p)) => {
1633
+ const { expectedSid, isMismatch } = expectation;
1634
+ if (isMismatch)
1635
+ touchAgent(agent.id, undefined);
1636
+ else
1637
+ touchAgent(agent.id, p.sessionId ?? expectedSid);
1638
+ recordLandedSession(p.sessionId ?? expectedSid);
1639
+ clearStoredPid(agent.id);
1640
+ if (p.usage) {
1641
+ const prev = agent.tokenUsage ?? { input: 0, output: 0 };
1642
+ updateAgent(agent.id, {
1643
+ tokenUsage: { input: prev.input + p.usage.input_tokens, output: prev.output + p.usage.output_tokens },
1644
+ });
1645
+ recordSessionUsage(p.sessionId ?? mintedId, p.usage);
1646
+ if (budgetScope)
1647
+ await recordAndEvaluate({ scope: budgetScope, agent, provider, tier: resolvedTier, usage: p.usage });
1140
1648
  }
1141
- }
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;
1649
+ };
1650
+ // Every provider.parseResponse in the dispatch path goes through this, so
1651
+ // a provider-reported cumulative session cost (Claude total_cost_usd) is
1652
+ // turned into THIS dispatch's cost exactly once per invocation, on every
1653
+ // path (success and failure alike), and the session's baseline advances.
1654
+ // The per-dispatch figure rides on usage.cost_usd; when the baseline of a
1655
+ // continued session is unknown it is left off and cost consumers price the
1656
+ // token counts instead.
1657
+ const parseDispatch = (r) => {
1658
+ const p = provider.parseResponse(r, parseCtx);
1659
+ _epParsedSid = p.sessionId;
1660
+ if (typeof p.sessionCostUsd === 'number') {
1661
+ const landedOn = p.sessionId ?? (lastAttemptContinues && !forkDescriptor ? lastAttemptContinues : undefined);
1662
+ const cost = dispatchCostFromCumulative(p.sessionCostUsd, landedOn, lastAttemptContinues);
1663
+ if (cost !== undefined && p.usage)
1664
+ p.usage = { ...p.usage, cost_usd: cost };
1665
+ }
1666
+ return p;
1667
+ };
1668
+ const checkUsageLimit = (r, p) => {
1669
+ const signal = provider.detectUsageLimit(r, p);
1670
+ if (!signal)
1671
+ return null;
1672
+ // Accurate finally-block exit logging (scope.fail, not scope.abort) even
1673
+ // when this fires on the very first parse, before the shared
1674
+ // `_epExitCode = result.code` assignment below would otherwise run.
1675
+ _epExitCode = r.code;
1676
+ scope.info(`usage limit detected -- resumes ~${signal.resumeAt} (${signal.resumeAtSource})`);
1677
+ // Session bookkeeping mirrors the max_turns path: record/touch the
1678
+ // session id when present so fleet-sprint can resume the SAME session
1679
+ // after the pause instead of starting a fresh one that has lost context.
1680
+ if (p.sessionId) {
1681
+ recordKnownSession(agent.id, p.sessionId);
1682
+ touchAgent(agent.id, p.sessionId);
1159
1683
  }
1160
- }
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);
1171
1684
  return {
1172
- text: buildFailureMessage(agent.friendlyName, result, provider, parsed),
1685
+ text: `[FAIL] execute_prompt on "${agent.friendlyName}" hit a provider usage limit (resumes ~${signal.resumeAt}, ${signal.resumeAtSource}): ${signal.message}`,
1173
1686
  structuredContent: {
1174
1687
  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 } } : {}),
1688
+ reason: 'usage_limit',
1689
+ usageLimit: signal,
1690
+ ...(p.sessionId ? { sessionId: p.sessionId } : {}),
1691
+ ...(p.usage ? { usage: toStructuredUsage(p.usage) } : {}),
1198
1692
  },
1199
1693
  };
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`);
1694
+ };
1695
+ claimGuardActive = false;
1696
+ try {
1697
+ let result;
1698
+ // GitHub #563: the FIRST attempt is capped by the same shared budget as
1699
+ // any retry (max_total_s since handler entry, minus the cleanup reserve),
1700
+ // not handed a fresh full max_total_s after setup already spent some.
1701
+ const firstBudget = retryBudget();
1702
+ if (firstBudget.exhausted) {
1703
+ _epExitCode = 'error';
1704
+ _epError = 'max_total_s exhausted before dispatch';
1705
+ return maxTotalTimeResult(false);
1706
+ }
1707
+ try {
1708
+ result = await dispatchAttempt(claudeCmd, firstBudget.timeoutMs, firstBudget.maxTotalMs);
1709
+ }
1710
+ catch (dispatchErr) {
1711
+ // apra-fleet-02s.1: a genuine command-execution exception (e.g. an
1712
+ // inactivity timeout, or any other error strategy.execCommand throws)
1713
+ // used to be unconditionally unretried here -- it bypasses both retry
1714
+ // mechanisms below, since those only fire on a non-throwing nonzero
1715
+ // exit, never on a thrown exception. Retry once with a fresh session
1716
+ // before giving up, mirroring the stale-session/server-overloaded
1717
+ // retries' bounded, single-attempt shape. Skip the retry if the client
1718
+ // itself cancelled the request -- there is nothing to recover from a
1719
+ // deliberate cancellation.
1720
+ if (extra?.signal?.aborted)
1721
+ throw dispatchErr;
1722
+ // apra-fleet-3c9.1: a stall-triggered abort is terminal. onStall already
1723
+ // killed the remote process and its session is gone, so retrying in a
1724
+ // fresh session would just re-dispatch onto a member we just tore down.
1725
+ // Let the exception surface to the outer catch, which classifies it as a
1726
+ // typed 'stalled' error instead of retrying or hanging.
1727
+ if (stallAbortController.signal.aborted)
1728
+ throw dispatchErr;
1729
+ // apra-fleet-eft.78.1: an explicit-id resume must NOT retry in a fresh
1730
+ // session (that would run a context-dependent delta prompt with no
1731
+ // context). Let the exception surface as dispatch_failed instead.
1732
+ if (!allowFreshSessionFallback)
1733
+ throw dispatchErr;
1734
+ // apra-fleet-y8q.1: no budget left to share with a retry (the original
1735
+ // attempt already consumed the whole max_total_s) -- retrying here would
1736
+ // just re-burn a fresh full budget past what the client is waiting for.
1737
+ // Let the original exception surface instead of retrying blind.
1738
+ const budget = retryBudget();
1739
+ if (budget.exhausted)
1740
+ throw dispatchErr;
1741
+ scope.info(`[${resolvedModel}] retrying -- dispatch exception: ${dispatchErr.message}`);
1742
+ await tryKillPid(agent, strategy, cmds);
1743
+ const freshOpts = { ...promptOpts, sessionId: isCallerMinted ? uuid() : undefined, resuming: false, fork: undefined };
1744
+ const retryCmd = cmds.buildAgentPromptCommand(provider, freshOpts);
1745
+ result = await dispatchAttempt(retryCmd, budget.timeoutMs, budget.maxTotalMs);
1746
+ }
1747
+ let parsed = parseDispatch(result);
1748
+ if (parsed.usage)
1749
+ _epUsage = parsed.usage;
1750
+ {
1751
+ const usageLimitResult = checkUsageLimit(result, parsed);
1752
+ if (usageLimitResult)
1753
+ return usageLimitResult;
1754
+ }
1755
+ // apra-fleet-eft.40.3: workspace-not-trusted degrades composed permissions
1756
+ // (project-scoped allow entries silently dropped) without killing the CLI process --
1757
+ // from there, unattended -p cannot auto-approve or prompt, so tools get denied and
1758
+ // the turn eventually fails. Blindly falling through to the stale-session /
1759
+ // server-overloaded retries below just repeats the same degraded dispatch against a
1760
+ // workspace that is still untrusted, and can walk into eft.28's dead-session hang.
1761
+ // Classify and fail fast here, before either retry path, naming
1762
+ // ensureWorkspaceTrusted (apra-fleet-eft.40.1/40.2) as the remediation.
1763
+ if (result.code !== 0 && provider.classifyError(result.stderr || result.stdout) === 'workspace_not_trusted') {
1241
1764
  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' },
1765
+ text: `[FAIL] ${workspaceNotTrustedAdvice(agent.friendlyName)}\n${result.stderr || result.stdout}`,
1766
+ // A typed failure keeps its own reason; an incidental refusal rides along.
1767
+ structuredContent: { isError: true, reason: 'workspace_not_trusted', ...(parsed.permissionDenial ? { permissionDenied: parsed.permissionDenial } : {}) },
1244
1768
  };
1245
1769
  }
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;
1770
+ // Stale session retry -- fresh session ID, no resume. apra-fleet-eft.78.1:
1771
+ // ONLY the best-effort resume=true mode gets this transparent retry-fresh
1772
+ // recovery. An explicit session-id resume (string) deliberately does NOT --
1773
+ // its caller asserted context dependence, so a not-found id is terminal.
1774
+ // apra-fleet-lmtg.5: gate on allowFreshSessionFallback too, so an explicit
1775
+ // fork (fork="<id>") never degrades here. The mutual-exclusivity guard keeps
1776
+ // input.resume at its default true for ANY fork request, so `input.resume
1777
+ // === true` alone would also fire for an explicit fork -- a silent
1778
+ // wrong-context fresh dispatch the explicit-fork gate exists to prevent.
1779
+ // allowFreshSessionFallback is false only for explicit resume/fork; it stays
1780
+ // true for resume=true and fork=true, which the AC permits to degrade.
1781
+ if (result.code !== 0 && allowFreshSessionFallback && input.resume === true && agent.sessionId) {
1782
+ // apra-fleet-y8q.1: share the remaining max_total_s budget with this
1783
+ // retry too -- skip it outright once exhausted (see retryBudget above).
1784
+ const staleBudget = retryBudget();
1785
+ if (!staleBudget.exhausted) {
1786
+ scope.info(`[${resolvedModel}] retrying -- stale session`);
1787
+ await tryKillPid(agent, strategy, cmds);
1788
+ const freshOpts = { ...promptOpts, sessionId: isCallerMinted ? uuid() : undefined, resuming: false, fork: undefined };
1789
+ const retryCmd = cmds.buildAgentPromptCommand(provider, freshOpts);
1790
+ result = await dispatchAttempt(retryCmd, staleBudget.timeoutMs, staleBudget.maxTotalMs);
1791
+ parsed = parseDispatch(result);
1253
1792
  if (parsed.usage)
1254
1793
  _epUsage = parsed.usage;
1794
+ const usageLimitResult = checkUsageLimit(result, parsed);
1795
+ if (usageLimitResult)
1796
+ return usageLimitResult;
1255
1797
  }
1256
1798
  }
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) {
1799
+ // Server/overloaded error retry -- single attempt after delay. Skipped for
1800
+ // an explicit-id resume (apra-fleet-eft.78.1): the retry starts a fresh
1801
+ // session, which would discard the exact context the caller asked to resume.
1802
+ if (result.code !== 0 && allowFreshSessionFallback && isRetryable(provider.classifyError(result.stderr || result.stdout))) {
1803
+ // apra-fleet-y8q.1: share the remaining max_total_s budget with this
1804
+ // retry too -- skip it outright once exhausted (see retryBudget above).
1805
+ const overloadBudget = retryBudget();
1806
+ if (!overloadBudget.exhausted) {
1807
+ scope.info(`[${resolvedModel}] retrying -- server overloaded`);
1278
1808
  await tryKillPid(agent, strategy, cmds);
1279
- const freshOpts = { ...promptOpts, sessionId: isCallerMinted ? uuid() : undefined, resuming: false };
1280
- const retryCmd = authPrefix + cmds.buildAgentPromptCommand(provider, freshOpts);
1281
- result = await strategy.execCommand(retryCmd, healBudget.timeoutMs, healBudget.maxTotalMs, onPidCaptured, dispatchSignal);
1282
- parsed = provider.parseResponse(result);
1809
+ await new Promise(r => setTimeout(r, SERVER_RETRY_DELAY_MS));
1810
+ const freshOpts = { ...promptOpts, sessionId: isCallerMinted ? uuid() : undefined, resuming: false, fork: undefined };
1811
+ const retryCmd = cmds.buildAgentPromptCommand(provider, freshOpts);
1812
+ result = await dispatchAttempt(retryCmd, overloadBudget.timeoutMs, overloadBudget.maxTotalMs);
1813
+ parsed = parseDispatch(result);
1283
1814
  if (parsed.usage)
1284
1815
  _epUsage = parsed.usage;
1816
+ const usageLimitResult = checkUsageLimit(result, parsed);
1817
+ if (usageLimitResult)
1818
+ return usageLimitResult;
1285
1819
  }
1286
1820
  }
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';
1290
- 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' },
1295
- };
1296
- }
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);
1821
+ _epExitCode = result.code;
1822
+ // Permission refusals (parsed.permissionDenial). Order matters:
1823
+ //
1824
+ // 1. A TYPED failure is classified first and wins: a max_turns-terminated
1825
+ // turn, or a nonzero exit the provider classifies as auth / server /
1826
+ // overloaded / workspace_not_trusted. Its reason is what a caller acts
1827
+ // on (fleet-sprint resumes a max_turns turn, re-provisions on auth);
1828
+ // an incidental refusal inside such a turn rides along as an extra
1829
+ // `permissionDenied` field and never turns it into permission_denied.
1830
+ //
1831
+ // 2. Otherwise the caller's choice decides. `fail_on_permission_denial`
1832
+ // (fleet-sprint passes it) makes any refusal a `permission_denied`
1833
+ // failure the caller can heal with compose_permissions -- except a
1834
+ // denial the provider marks healable:false (Claude auto/bypass mode:
1835
+ // safety classifier or deny rule) on a complete reply, which is only a
1836
+ // warning. Without the flag each provider keeps its v0.4.3 semantics:
1837
+ // Claude's reply stands (a complete reply is a success carrying the
1838
+ // refusals as `permissionWarning`; an incomplete one gets its ordinary
1839
+ // classification with the denial attached), while AGY -- whose headless
1840
+ // denials exit 0 with an empty reply -- still fails permission_denied.
1841
+ const denial = parsed.permissionDenial;
1842
+ const strictDenials = input.fail_on_permission_denial === true || provider.name !== 'claude';
1843
+ const typedFailure = isMaxTurnsResponse(parsed)
1844
+ || (result.code !== 0 && TYPED_FAILURE_CATEGORIES.has(provider.classifyError(result.stderr || result.stdout)));
1845
+ const replyComplete = result.code === 0 && !parsed.isError && !!parsed.result?.trim();
1846
+ let permissionWarning;
1847
+ // A denial carried along on a non-permission outcome (typed failure, or
1848
+ // non-strict Claude whose reply was not complete).
1849
+ let carriedDenial;
1850
+ if (denial) {
1851
+ if (typedFailure) {
1852
+ carriedDenial = denial;
1310
1853
  }
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 });
1854
+ else if (replyComplete && (denial.healable === false || !strictDenials)) {
1855
+ permissionWarning = denial;
1856
+ logWarn('permission_denied_incidental', `"${agent.friendlyName}" (${denial.permissionMode ?? 'unknown'} mode): ${denial.hint} -- reply complete, reported as a warning${denial.healable === false ? '; never auto-granted' : ''}.`);
1857
+ }
1858
+ else if (strictDenials) {
1859
+ const partial = parsed.result?.trim() ? parsed.result.trim() : undefined;
1860
+ // The turn ran: keep the session, usage, budget and PID bookkeeping
1861
+ // a completed dispatch gets, so a later resume continues THIS session
1862
+ // and the spend is recorded.
1863
+ const expectation = sessionExpectation(parsed);
1864
+ if (expectation.isMismatch) {
1865
+ scope.info(`session-id mismatch: expected=${expectation.expectedSid} got=${parsed.sessionId} -- not persisting`);
1866
+ if (mismatchIsTerminal())
1867
+ return await terminalMismatchFailure(parsed, expectation.expectedSid, { permissionDenied: denial });
1322
1868
  }
1869
+ await recordDispatchBookkeeping(parsed, expectation);
1870
+ return {
1871
+ text: `[FAIL] execute_prompt on "${agent.friendlyName}": permission denied -- ${denial.hint}${partial ? `\n[partial response]\n${partial}` : ''}`,
1872
+ structuredContent: {
1873
+ isError: true,
1874
+ reason: 'permission_denied',
1875
+ permissionDenied: denial,
1876
+ ...(partial ? { response: partial } : {}),
1877
+ ...(parsed.sessionId ? { sessionId: parsed.sessionId } : {}),
1878
+ ...(_epUsage ? { usage: toStructuredUsage(_epUsage) } : {}),
1879
+ },
1880
+ };
1881
+ }
1882
+ else {
1883
+ carriedDenial = denial;
1323
1884
  }
1885
+ }
1886
+ const carriedDenialField = carriedDenial ? { permissionDenied: carriedDenial } : {};
1887
+ if (result.code !== 0) {
1888
+ // GitHub #585: a failed dispatch used to log only exit=N. Log a capped
1889
+ // stderr tail plus the last result/error event (logLine applies the
1890
+ // usual secret redaction). Success paths log nothing extra.
1891
+ logLine('prompt_failure_output', formatPromptFailureTail(result), { id: agent.id, friendlyName: agent.friendlyName }, scope.getInv());
1892
+ // apra-fleet-391: surface an auth failure as a STRUCTURED reason (not
1893
+ // just prose in `text`) so callers -- notably fleet-sprint's
1894
+ // isAuthDispatchError -- can key off it directly instead of regexing
1895
+ // the message string. Overloading 'nonzero_exit' for this case was
1896
+ // what made auth self-heal impossible to wire reliably upstream.
1897
+ const failureCategory = isMaxTurnsResponse(parsed)
1898
+ ? 'max_turns'
1899
+ : provider.classifyError(result.stderr || result.stdout);
1324
1900
  return {
1325
- text: `[FAIL] execute_prompt on "${agent.friendlyName}" failed -- resumed session mismatch. Expected session "${expectedSid}", but provider returned "${parsed.sessionId}".`,
1901
+ text: buildFailureMessage(agent.friendlyName, result, provider, parsed),
1326
1902
  structuredContent: {
1327
1903
  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 } } : {}),
1904
+ reason: failureCategory === 'max_turns'
1905
+ ? 'max_turns_exhausted'
1906
+ : failureCategory === 'auth'
1907
+ ? 'auth'
1908
+ : failureCategory === 'server'
1909
+ ? 'server'
1910
+ : failureCategory === 'overloaded'
1911
+ ? 'overloaded'
1912
+ : failureCategory === 'workspace_not_trusted'
1913
+ ? 'workspace_not_trusted'
1914
+ : 'nonzero_exit',
1915
+ // apra-fleet-63x.1: a nonzero exit -- most commonly max_turns_exhausted,
1916
+ // where the CLI ran real turns and burned real tokens before hitting its
1917
+ // ceiling -- still has a REAL parsed usage figure sitting in _epUsage
1918
+ // (captured just above from `parsed.usage` regardless of exit code).
1919
+ // This branch used to return no `usage` field at all on any failure,
1920
+ // so FleetWorkflow.agent() (packages/apra-fleet-workflow/src/workflow/
1921
+ // index.mjs, apra-fleet-202.3) saw hasRealUsage=false and never priced
1922
+ // it, silently under-counting a sprint's tracked spend (observed: a
1923
+ // 10-hour run with dozens of max_turns exhaustions reporting
1924
+ // stats.totalCost of $0). Attach it here whenever it's available so
1925
+ // the caller can record the real partial cost instead of nothing.
1926
+ ...(_epUsage ? { usage: toStructuredUsage(_epUsage) } : {}),
1927
+ ...sessionIdField(parsed.sessionId),
1928
+ ...carriedDenialField,
1332
1929
  },
1333
1930
  };
1334
1931
  }
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);
1932
+ // Exit 0 but the provider parser extracted NOTHING (no result text)
1933
+ // -- observed live (apra-fleet-eft.14, 2026-07-19 stabilization loop):
1934
+ // the claude CLI can die silently mid-turn (member-side session
1935
+ // transcript stops at a tool_result with no final assistant message)
1936
+ // and still exit 0 with EMPTY stdout, so parseResponse falls through
1937
+ // to its plain-text fallback with result: ''. Returning that as a
1938
+ // success used to hand callers a display wrapper with nothing inside,
1939
+ // which schema-extraction layers then misreported as "LLM returned
1940
+ // invalid JSON". Classify it at the source as a typed dispatch error
1941
+ // instead, with stderr's tail attached for diagnosis.
1942
+ //
1943
+ // apra-fleet-6z8.1 (lease-of-life gate): before declaring that, cross-check
1944
+ // whether the captured pid is STILL ALIVE via a fresh short exec. ssh.ts
1945
+ // substitutes code 0 when the channel closes without ever receiving an
1946
+ // 'exit' event, so "exit 0 + empty" is ALSO exactly what a torn-down
1947
+ // channel over a perfectly healthy, still-running turn looks like (live
1948
+ // evidence: pid 89858 running 2+ minutes past the close). Declaring
1949
+ // empty_response there orphans the CLI and invites a duplicate concurrent
1950
+ // dispatch of the same scope. A confirmed-dead pid keeps today's behavior
1951
+ // verbatim.
1952
+ if (!parsed.result || parsed.result.trim() === '') {
1953
+ const recovery = await recoverOrphanedDispatch({
1954
+ strategy,
1955
+ cmds,
1956
+ pid: capturedPid,
1957
+ durablePath,
1958
+ // apra-fleet-7dir.5.4: the SAME predicate that decides `durablePath`
1959
+ // above -- the mirror and recovery are one decision, and enabling
1960
+ // either alone is inert (no file to read / a file nothing reads).
1961
+ unsupported: !durableMirrorSupported,
1962
+ os: getAgentOS(agent),
1963
+ // Live for gitbash members now that `unsupported` no longer
1964
+ // short-circuits every Windows member: it selects the POSIX
1965
+ // `kill -0` / `cat` probes over the PowerShell ones (apra-fleet-7dir.2.4).
1966
+ shell: getAgentShell(agent),
1967
+ maxWaitMs: maxTotalMs !== undefined ? retryBudget().maxTotalMs : undefined,
1968
+ scope,
1969
+ });
1970
+ if (recovery.status === 'timeout') {
1971
+ scope.info(`orphan recovery timed out after ${Math.round((recovery.waitedMs ?? 0) / 1000)}s -- pid killed`);
1972
+ return {
1973
+ 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.`,
1974
+ structuredContent: { isError: true, reason: 'orphan_recovery_timeout' },
1975
+ };
1976
+ }
1977
+ if (recovery.status === 'recovered' && recovery.stdout) {
1978
+ // Feed the durable output through the normal provider parse path,
1979
+ // exactly as if it had arrived on the original channel.
1980
+ const recoveredResult = { stdout: recovery.stdout, stderr: result.stderr ?? '', code: 0 };
1981
+ const recoveredParsed = parseDispatch(recoveredResult);
1982
+ if (recoveredParsed.result && recoveredParsed.result.trim() !== '') {
1983
+ 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)`);
1984
+ parsed = recoveredParsed;
1985
+ if (parsed.usage)
1986
+ _epUsage = parsed.usage;
1987
+ const usageLimitResult = checkUsageLimit(recoveredResult, recoveredParsed);
1988
+ if (usageLimitResult)
1989
+ return usageLimitResult;
1990
+ }
1991
+ }
1992
+ // apra-fleet-6a7.1: the code!==0 workspace_not_trusted classification
1993
+ // above (~line 1048) never sees this branch -- exit 0 + empty stdout is
1994
+ // a SEPARATE path, but live evidence (apra-fleet-2g2, fleet-win-dev1,
1995
+ // 2026-08-02) showed it can be caused by the exact same trust-gate
1996
+ // failure: exited 0, empty stdout, and the stderr tail contained the
1997
+ // exact matched phrase, so the code!==0 classifier never ran. Classify
1998
+ // it here too and -- unlike that path, which only advises a manual fix
1999
+ // -- self-heal via the same seedWorkspaceTrust/ensureWorkspaceTrusted
2000
+ // path compose_permissions already uses, then retry the dispatch
2001
+ // exactly once (gated by trustHealAttempted, mirroring runGitStep's
2002
+ // onAuthFailure shape: a repeat failure after the heal is terminal, not
2003
+ // looped).
2004
+ if ((!parsed.result || parsed.result.trim() === '')
2005
+ && allowFreshSessionFallback
2006
+ && provider.classifyError(result.stderr || result.stdout) === 'workspace_not_trusted'
2007
+ && !trustHealAttempted) {
2008
+ trustHealAttempted = true;
2009
+ scope.info(`[${resolvedModel}] exit-0/empty-stdout classified as workspace_not_trusted -- self-healing (seedWorkspaceTrust) once, then retrying the dispatch`);
2010
+ await seedWorkspaceTrust(agent, strategy, 'execute_prompt');
2011
+ const healBudget = retryBudget();
2012
+ if (!healBudget.exhausted) {
2013
+ await tryKillPid(agent, strategy, cmds);
2014
+ const freshOpts = { ...promptOpts, sessionId: isCallerMinted ? uuid() : undefined, resuming: false, fork: undefined };
2015
+ const retryCmd = cmds.buildAgentPromptCommand(provider, freshOpts);
2016
+ result = await dispatchAttempt(retryCmd, healBudget.timeoutMs, healBudget.maxTotalMs);
2017
+ parsed = parseDispatch(result);
2018
+ if (parsed.usage)
2019
+ _epUsage = parsed.usage;
2020
+ const usageLimitResult = checkUsageLimit(result, parsed);
2021
+ if (usageLimitResult)
2022
+ return usageLimitResult;
2023
+ }
2024
+ }
2025
+ if (!parsed.result || parsed.result.trim() === '') {
2026
+ const stderrTail = (result.stderr || '').trim().slice(-500);
2027
+ const trustClassified = provider.classifyError(result.stderr || result.stdout) === 'workspace_not_trusted';
2028
+ return {
2029
+ text: trustClassified
2030
+ ? `[FAIL] ${workspaceNotTrustedAdvice(agent.friendlyName)}\n${stderrTail}`
2031
+ : `[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}` : ''}`,
2032
+ structuredContent: { isError: true, reason: trustClassified ? 'workspace_not_trusted' : 'empty_response', ...(trustClassified ? {} : sessionIdField(parsed.sessionId)), ...carriedDenialField },
2033
+ };
2034
+ }
1350
2035
  }
1351
- catch {
1352
- postLogPath = null;
2036
+ // Session-id assertion (see sessionExpectation).
2037
+ const { expectedSid, isMismatch } = sessionExpectation(parsed);
2038
+ if (isMismatch) {
2039
+ scope.info(`session-id mismatch: expected=${expectedSid} got=${parsed.sessionId} -- not persisting`);
2040
+ if (mismatchIsTerminal())
2041
+ return await terminalMismatchFailure(parsed, expectedSid);
2042
+ touchAgent(agent.id, undefined);
1353
2043
  }
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}:
2044
+ else {
2045
+ touchAgent(agent.id, parsed.sessionId ?? expectedSid);
2046
+ }
2047
+ recordLandedSession(parsed.sessionId ?? expectedSid);
2048
+ clearStoredPid(agent.id);
2049
+ if (parsed.usage) {
2050
+ const prev = agent.tokenUsage ?? { input: 0, output: 0 };
2051
+ updateAgent(agent.id, {
2052
+ tokenUsage: {
2053
+ input: prev.input + parsed.usage.input_tokens,
2054
+ output: prev.output + parsed.usage.output_tokens,
2055
+ },
2056
+ });
2057
+ // Per-session cumulative tracking for context-headroom admission
2058
+ // (apra-fleet-eft.81.1) -- separate from the per-member lifetime total
2059
+ // just above. Keyed by whichever session id this dispatch actually
2060
+ // landed on (the returned id, falling back to the minted/resumed one),
2061
+ // so the NEXT admission check on this same session sees this dispatch's
2062
+ // usage already accounted for.
2063
+ recordSessionUsage(parsed.sessionId ?? mintedId, parsed.usage);
2064
+ }
2065
+ // Usage/budget accounting (apra-fleet-eft.80.2): account this dispatch's
2066
+ // token usage against the scope's budget (self-metered only when the
2067
+ // provider has no native getUsage()), then attach the usage block to the
2068
+ // result when the warning band is crossed. Nothing is attached below the
2069
+ // band, and nothing happens at all when no budget is configured.
2070
+ let budgetUsage;
2071
+ if (budgetScope && parsed.usage) {
2072
+ const postBudget = await recordAndEvaluate({ scope: budgetScope, agent, provider, tier: resolvedTier, usage: parsed.usage });
2073
+ if (postBudget?.warned)
2074
+ budgetUsage = postBudget.block;
2075
+ }
2076
+ let output = `[RESULT] Response from ${agent.friendlyName}:
1389
2077
 
1390
2078
  ${parsed.result}`;
1391
- if (parsed.sessionId)
1392
- output += `
2079
+ if (parsed.sessionId)
2080
+ output += `
1393
2081
 
1394
2082
  ---
1395
2083
  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}`);
2084
+ // Auto-harvest learnings from session output (fire-and-forget)
2085
+ if (parsed.result) {
2086
+ // apra-fleet-tm7.2: always pass resolvedWorkFolder, local or remote.
2087
+ // getKbProviders(undefined) falls back to slugFor(process.cwd()) --
2088
+ // the FLEET SERVER's own cwd, not any generic 'default' -- so omitting
2089
+ // repo_path for remote members would silently route their harvested
2090
+ // learnings into the server's own repo KB (the exact defect apra-fleet-tm7
2091
+ // describes).
2092
+ //
2093
+ // apra-fleet-b4g.6: repo_remote_url (apra-fleet-b4g.1) is how a REMOTE
2094
+ // member's call reaches the SAME shared project KB a local clone would --
2095
+ // without it, resolvedWorkFolder is a path on the remote host that will
2096
+ // almost never exist on this (fleet server) machine, resolveProjectSlug's
2097
+ // git calls on it fail with ENOENT, and it falls through to the literal
2098
+ // 'default' slug, pooling every such member's harvest into one shared,
2099
+ // useless bucket (no cross-repo contamination, but no real routing
2100
+ // either). knownRepoRemoteUrl() forwards it whenever the member's own
2101
+ // registration record already carries a genuine URL. It deliberately does
2102
+ // NOT construct one from agent.gitRepos' bare "owner/repo" access-list
2103
+ // entries (see src/services/vcs/github.ts's own
2104
+ // https://github.com/${repo}.git construction for a DIFFERENT, narrower
2105
+ // purpose -- connectivity testing) or shell out to the member host to
2106
+ // discover it: guessing a URL that turns out wrong would route the
2107
+ // harvest into a KB slug that does not match the repo's real local-clone
2108
+ // slug, which is worse than today's honest 'default' degradation. When no
2109
+ // genuine URL is known, behaviour is unchanged from before this fix.
2110
+ // KB-TRUST PHASE 1 (apra-fleet-4wz.8): report the harvest counters. This is
2111
+ // the highest-volume KB writer and its result was previously discarded
2112
+ // entirely, so entries_rejected -- the fail-closed signal -- was invisible
2113
+ // on the one path that produces most of it. Rejections are EXPECTED to
2114
+ // dominate here: harvest's regex extraction frequently yields no file
2115
+ // paths at all, and an entry with no basis is refused by design. A high
2116
+ // rejected count is the invariant working, not a regression to tune away.
2117
+ void import('./kb-harvest.js')
2118
+ .then(({ kbHarvest }) =>
2119
+ // Explicit in-process anchor (not a tool input): this harvest is for
2120
+ // the dispatched member's repo, not the server's own session.
2121
+ kbHarvest({
2122
+ session_transcript: parsed.result,
2123
+ session_id: parsed.sessionId,
2124
+ }, { folder: resolvedWorkFolder, remoteUrl: knownRepoRemoteUrl(agent) }))
2125
+ .then((raw) => {
2126
+ try {
2127
+ const r = JSON.parse(raw);
2128
+ if (r.entries_captured || r.entries_updated || r.entries_skipped || r.entries_rejected) {
2129
+ logLine('kb_harvest', `auto-harvest: captured=${r.entries_captured} updated=${r.entries_updated} skipped=${r.entries_skipped} rejected=${r.entries_rejected}`);
2130
+ }
1441
2131
  }
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}`));
2132
+ catch {
2133
+ // A non-JSON payload is not worth failing a fire-and-forget harvest over.
2134
+ }
2135
+ })
2136
+ .catch((err) => logWarn('kb_harvest', `auto-harvest failed: ${err.message}`));
2137
+ }
2138
+ if (heuristicWarningSuffix)
2139
+ output += heuristicWarningSuffix;
2140
+ // A refusal that did not fail the dispatch (incidental, or carried on a
2141
+ // turn that still exited 0) rides on the success as a warning.
2142
+ const successWarning = permissionWarning ?? carriedDenial;
2143
+ if (successWarning)
2144
+ output += `\n\n[WARN] ${successWarning.hint}`;
2145
+ return {
2146
+ text: output,
2147
+ structuredContent: {
2148
+ response: parsed.result,
2149
+ ...(_epUsage ? { usage: toStructuredUsage(_epUsage) } : {}),
2150
+ ...(parsed.sessionId ? { sessionId: parsed.sessionId } : {}),
2151
+ ...(contextWarning ? { contextWarning } : {}),
2152
+ ...(budgetUsage ? { budgetUsage } : {}),
2153
+ ...(successWarning ? { permissionWarning: successWarning } : {}),
2154
+ },
2155
+ };
1448
2156
  }
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';
2157
+ catch (err) {
2158
+ // apra-fleet-3c9.1: a confirmed stall aborted the in-flight execCommand (and
2159
+ // NOT the MCP client). Surface it as a typed 'stalled' error so the dispatch
2160
+ // settles here -- well under the client hard timeout -- instead of being
2161
+ // mislabeled dispatch_failed or waiting out the full deadline.
2162
+ if (stallAbortController.signal.aborted && !extra?.signal?.aborted && stallReason === 'agent_never_started') {
2163
+ _epError = 'dispatch aborted: agent never started (session log never appeared)';
2164
+ return {
2165
+ text: `[FAIL] execute_prompt on "${agent.friendlyName}" was aborted: the agent never started -- its session log never appeared at the expected path within the inactivity threshold (timeout_s). Its process was killed and the dispatch cancelled.`,
2166
+ structuredContent: { isError: true, reason: 'agent_never_started' },
2167
+ };
2168
+ }
2169
+ if (stallAbortController.signal.aborted && !extra?.signal?.aborted) {
2170
+ _epError = 'dispatch aborted by confirmed stall';
2171
+ return {
2172
+ 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.`,
2173
+ structuredContent: { isError: true, reason: 'stalled' },
2174
+ };
2175
+ }
2176
+ // GitHub #563: the shared max_total_s ceiling fired -- a typed error.
2177
+ if (isMaxTotalKill(err)) {
2178
+ _epError = err.message;
2179
+ return maxTotalTimeResult();
2180
+ }
2181
+ // Only mark offline for genuine SSH/network connection failures, not for cancellations
2182
+ _epOffline = !!(err.message && /ssh|network|econnrefused|ehostunreach|connection timed out/i.test(err.message));
2183
+ _epError = err.message;
1469
2184
  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' },
2185
+ text: `[FAIL] Failed to execute prompt on "${agent.friendlyName}": ${err.message}`,
2186
+ structuredContent: { isError: true, reason: 'dispatch_failed', ...sessionIdField(_epParsedSid) },
1472
2187
  };
1473
2188
  }
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
- };
2189
+ finally {
2190
+ extra?.signal?.removeEventListener('abort', abortHandler);
2191
+ const _epTok = _epUsage ? ` in=${_epUsage.input_tokens} out=${_epUsage.output_tokens} cache_read=${_epUsage.cache_read_input_tokens ?? 0} cache_write=${_epUsage.cache_creation_input_tokens ?? 0}` : '';
2192
+ if (_epExitCode === 'error')
2193
+ scope.abort(`${_epError ?? 'exception'}${_epTok}`);
2194
+ else if (_epExitCode !== 0)
2195
+ scope.fail(`exit=${_epExitCode}${_epTok}`);
2196
+ else
2197
+ scope.ok(`exit=0${_epTok}`);
2198
+ // Skip if stall detector already cleared state -- a new execute_prompt may have
2199
+ // claimed inFlightAgents and set busy again; clobbering it here would be wrong.
2200
+ if (!clearedByStall) {
2201
+ writeStatusline(new Map([[agent.id, _epOffline ? 'offline' : 'idle']]));
2202
+ inFlightAgents.delete(agent.id);
2203
+ }
2204
+ stallDetector.remove(agent.id);
2205
+ await deletePromptFile(agent, strategy, promptFilePath, [...(durablePath ? [durablePath] : []), ...stagedEnvPaths, ...sessionMcpCleanup]);
2206
+ }
1481
2207
  }
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']]));
2208
+ catch (err) {
2209
+ if (claimGuardActive) {
1495
2210
  inFlightAgents.delete(agent.id);
2211
+ getStallDetector().remove(agent.id);
1496
2212
  }
1497
- stallDetector.remove(agent.id);
1498
- await deletePromptFile(agent, strategy, promptFilePath, durablePath ? [durablePath] : []);
2213
+ throw err;
1499
2214
  }
1500
2215
  }
1501
2216
  //# sourceMappingURL=execute-prompt.js.map