agent-control-plane 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (317) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +589 -0
  3. package/SKILL.md +149 -0
  4. package/assets/workflow-catalog.json +57 -0
  5. package/bin/audit-issue-routing.sh +74 -0
  6. package/bin/issue-resource-class.sh +58 -0
  7. package/bin/label-follow-up-issues.sh +114 -0
  8. package/bin/pr-risk.sh +532 -0
  9. package/bin/sync-pr-labels.sh +112 -0
  10. package/hooks/heartbeat-hooks.sh +573 -0
  11. package/hooks/issue-reconcile-hooks.sh +217 -0
  12. package/hooks/pr-reconcile-hooks.sh +225 -0
  13. package/npm/bin/agent-control-plane.js +1984 -0
  14. package/npm/public-bin/agent-control-plane +3 -0
  15. package/package.json +61 -0
  16. package/tools/bin/agent-cleanup-worktree +247 -0
  17. package/tools/bin/agent-github-update-labels +66 -0
  18. package/tools/bin/agent-init-worktree +216 -0
  19. package/tools/bin/agent-project-archive-run +52 -0
  20. package/tools/bin/agent-project-capture-worker +46 -0
  21. package/tools/bin/agent-project-catch-up-merged-prs +137 -0
  22. package/tools/bin/agent-project-cleanup-session +244 -0
  23. package/tools/bin/agent-project-detached-launch +107 -0
  24. package/tools/bin/agent-project-heartbeat-loop +2347 -0
  25. package/tools/bin/agent-project-open-issue-worktree +89 -0
  26. package/tools/bin/agent-project-open-pr-worktree +80 -0
  27. package/tools/bin/agent-project-publish-issue-pr +349 -0
  28. package/tools/bin/agent-project-reconcile-issue-session +1128 -0
  29. package/tools/bin/agent-project-reconcile-pr-session +1005 -0
  30. package/tools/bin/agent-project-retry-state +147 -0
  31. package/tools/bin/agent-project-run-claude-session +657 -0
  32. package/tools/bin/agent-project-run-codex-resilient +718 -0
  33. package/tools/bin/agent-project-run-codex-session +316 -0
  34. package/tools/bin/agent-project-run-kilo-session +27 -0
  35. package/tools/bin/agent-project-run-openclaw-session +984 -0
  36. package/tools/bin/agent-project-run-opencode-session +27 -0
  37. package/tools/bin/agent-project-sync-anchor-repo +128 -0
  38. package/tools/bin/agent-project-worker-status +143 -0
  39. package/tools/bin/audit-agent-worktrees.sh +310 -0
  40. package/tools/bin/audit-issue-routing.sh +11 -0
  41. package/tools/bin/audit-retained-layout.sh +58 -0
  42. package/tools/bin/audit-retained-overlap.sh +135 -0
  43. package/tools/bin/audit-retained-worktrees.sh +228 -0
  44. package/tools/bin/branch-verification-guard.sh +351 -0
  45. package/tools/bin/capture-worker.sh +18 -0
  46. package/tools/bin/check-skill-contracts.sh +324 -0
  47. package/tools/bin/cleanup-worktree.sh +44 -0
  48. package/tools/bin/codex-quota +31 -0
  49. package/tools/bin/create-follow-up-issue.sh +114 -0
  50. package/tools/bin/dashboard-launchd-bootstrap.sh +38 -0
  51. package/tools/bin/flow-config-lib.sh +2127 -0
  52. package/tools/bin/flow-resident-worker-lib.sh +683 -0
  53. package/tools/bin/flow-runtime-doctor.sh +97 -0
  54. package/tools/bin/flow-shell-lib.sh +266 -0
  55. package/tools/bin/heartbeat-recovery-preflight.sh +106 -0
  56. package/tools/bin/heartbeat-safe-auto.sh +551 -0
  57. package/tools/bin/install-dashboard-launchd.sh +152 -0
  58. package/tools/bin/install-project-launchd.sh +219 -0
  59. package/tools/bin/issue-publish-scope-guard.sh +242 -0
  60. package/tools/bin/issue-requires-local-workspace-install.sh +31 -0
  61. package/tools/bin/issue-resource-class.sh +12 -0
  62. package/tools/bin/kick-scheduler.sh +75 -0
  63. package/tools/bin/label-follow-up-issues.sh +14 -0
  64. package/tools/bin/new-pr-worktree.sh +50 -0
  65. package/tools/bin/new-worktree.sh +49 -0
  66. package/tools/bin/pr-risk.sh +12 -0
  67. package/tools/bin/prepare-worktree.sh +140 -0
  68. package/tools/bin/profile-activate.sh +109 -0
  69. package/tools/bin/profile-adopt.sh +219 -0
  70. package/tools/bin/profile-smoke.sh +461 -0
  71. package/tools/bin/project-init.sh +189 -0
  72. package/tools/bin/project-launchd-bootstrap.sh +54 -0
  73. package/tools/bin/project-remove.sh +155 -0
  74. package/tools/bin/project-runtime-supervisor.sh +56 -0
  75. package/tools/bin/project-runtimectl.sh +586 -0
  76. package/tools/bin/provider-cooldown-state.sh +166 -0
  77. package/tools/bin/publish-issue-worker.sh +31 -0
  78. package/tools/bin/reconcile-issue-worker.sh +34 -0
  79. package/tools/bin/reconcile-pr-worker.sh +34 -0
  80. package/tools/bin/record-verification.sh +71 -0
  81. package/tools/bin/render-architecture-infographics.sh +110 -0
  82. package/tools/bin/render-dashboard-demo-media.sh +333 -0
  83. package/tools/bin/render-dashboard-snapshot.py +16 -0
  84. package/tools/bin/render-flow-config.sh +86 -0
  85. package/tools/bin/retry-state.sh +31 -0
  86. package/tools/bin/reuse-issue-worktree.sh +75 -0
  87. package/tools/bin/run-codex-bypass.sh +3 -0
  88. package/tools/bin/run-codex-safe.sh +3 -0
  89. package/tools/bin/run-codex-task.sh +231 -0
  90. package/tools/bin/scaffold-profile.sh +374 -0
  91. package/tools/bin/serve-dashboard.sh +5 -0
  92. package/tools/bin/split-retained-slice.sh +124 -0
  93. package/tools/bin/start-issue-worker.sh +796 -0
  94. package/tools/bin/start-pr-fix-worker.sh +458 -0
  95. package/tools/bin/start-pr-merge-repair-worker.sh +8 -0
  96. package/tools/bin/start-pr-review-worker.sh +227 -0
  97. package/tools/bin/start-resident-issue-loop.sh +908 -0
  98. package/tools/bin/sync-agent-repo.sh +52 -0
  99. package/tools/bin/sync-dependency-baseline.sh +247 -0
  100. package/tools/bin/sync-pr-labels.sh +12 -0
  101. package/tools/bin/sync-recurring-issue-checklist.sh +274 -0
  102. package/tools/bin/sync-shared-agent-home.sh +214 -0
  103. package/tools/bin/sync-vscode-workspace.sh +157 -0
  104. package/tools/bin/test-smoke.sh +63 -0
  105. package/tools/bin/uninstall-project-launchd.sh +55 -0
  106. package/tools/bin/update-github-labels.sh +14 -0
  107. package/tools/bin/worker-status.sh +19 -0
  108. package/tools/bin/workflow-catalog.sh +77 -0
  109. package/tools/dashboard/app.js +286 -0
  110. package/tools/dashboard/dashboard_snapshot.py +466 -0
  111. package/tools/dashboard/index.html +41 -0
  112. package/tools/dashboard/server.py +64 -0
  113. package/tools/dashboard/styles.css +351 -0
  114. package/tools/templates/issue-prompt-template.md +109 -0
  115. package/tools/templates/pr-fix-template.md +120 -0
  116. package/tools/templates/pr-merge-repair-template.md +91 -0
  117. package/tools/templates/pr-review-template.md +62 -0
  118. package/tools/templates/scheduled-issue-prompt-template.md +62 -0
  119. package/tools/tests/test-agent-control-plane-npm-cli.sh +279 -0
  120. package/tools/tests/test-agent-github-update-labels-falls-back-to-repository-id.sh +56 -0
  121. package/tools/tests/test-agent-project-claude-session-wrapper-clears-stale-sandbox-artifacts.sh +89 -0
  122. package/tools/tests/test-agent-project-claude-session-wrapper-does-not-retry-provider-quota.sh +82 -0
  123. package/tools/tests/test-agent-project-claude-session-wrapper-retries-transient-failures.sh +90 -0
  124. package/tools/tests/test-agent-project-claude-session-wrapper-times-out.sh +73 -0
  125. package/tools/tests/test-agent-project-claude-session-wrapper.sh +103 -0
  126. package/tools/tests/test-agent-project-cleanup-session-orphan-fallback.sh +90 -0
  127. package/tools/tests/test-agent-project-cleanup-session-skip-worktree-cleanup.sh +90 -0
  128. package/tools/tests/test-agent-project-codex-live-thread-persist.sh +76 -0
  129. package/tools/tests/test-agent-project-codex-recovery.sh +731 -0
  130. package/tools/tests/test-agent-project-codex-session-wrapper-clears-stale-sandbox-artifacts.sh +105 -0
  131. package/tools/tests/test-agent-project-codex-session-wrapper.sh +97 -0
  132. package/tools/tests/test-agent-project-open-pr-worktree-config-prefix.sh +81 -0
  133. package/tools/tests/test-agent-project-openclaw-session-wrapper-clears-stale-sandbox-artifacts.sh +109 -0
  134. package/tools/tests/test-agent-project-openclaw-session-wrapper-infers-blocked-result-contract.sh +89 -0
  135. package/tools/tests/test-agent-project-openclaw-session-wrapper-recovers-literal-env-artifacts.sh +113 -0
  136. package/tools/tests/test-agent-project-openclaw-session-wrapper-recovers-version-mismatch.sh +135 -0
  137. package/tools/tests/test-agent-project-openclaw-session-wrapper-resident.sh +179 -0
  138. package/tools/tests/test-agent-project-openclaw-session-wrapper-reuses-existing-agent-after-add-race.sh +119 -0
  139. package/tools/tests/test-agent-project-openclaw-session-wrapper-terminates-rate-limit-hang.sh +91 -0
  140. package/tools/tests/test-agent-project-openclaw-session-wrapper.sh +117 -0
  141. package/tools/tests/test-agent-project-publish-issue-pr-prunes-stale-worktree-entry.sh +148 -0
  142. package/tools/tests/test-agent-project-publish-issue-pr-reads-archived-session.sh +146 -0
  143. package/tools/tests/test-agent-project-publish-issue-pr-recovers-final-head.sh +145 -0
  144. package/tools/tests/test-agent-project-publish-issue-pr-reuses-existing-worktree.sh +147 -0
  145. package/tools/tests/test-agent-project-reconcile-failure-reason.sh +456 -0
  146. package/tools/tests/test-agent-project-reconcile-issue-archived-session-fallback.sh +96 -0
  147. package/tools/tests/test-agent-project-reconcile-issue-before-blocked.sh +90 -0
  148. package/tools/tests/test-agent-project-reconcile-issue-host-verification-recovery-uses-recovered-worktree.sh +212 -0
  149. package/tools/tests/test-agent-project-reconcile-issue-host-verification-recovery.sh +207 -0
  150. package/tools/tests/test-agent-project-reconcile-issue-provider-quota-schedules-provider-cooldown.sh +101 -0
  151. package/tools/tests/test-agent-project-reconcile-issue-session-backfills-lane-metadata-from-worker-key.sh +113 -0
  152. package/tools/tests/test-agent-project-reconcile-issue-session-clears-stale-failed-summary.sh +117 -0
  153. package/tools/tests/test-agent-project-reconcile-issue-session-initializes-shared-agent-home.sh +55 -0
  154. package/tools/tests/test-agent-project-reconcile-issue-session-normalizes-runner-state.sh +125 -0
  155. package/tools/tests/test-agent-project-reconcile-issue-session-records-invalid-contract-summary.sh +118 -0
  156. package/tools/tests/test-agent-project-reconcile-issue-session-skips-duplicate-blocked-comment.sh +144 -0
  157. package/tools/tests/test-agent-project-reconcile-issue-session-standardizes-no-commits-blocker.sh +145 -0
  158. package/tools/tests/test-agent-project-reconcile-issue-session-synthesizes-blocked-comment.sh +139 -0
  159. package/tools/tests/test-agent-project-reconcile-pr-blocked-host-recovery.sh +242 -0
  160. package/tools/tests/test-agent-project-reconcile-pr-guard-blocked-no-commit.sh +142 -0
  161. package/tools/tests/test-agent-project-reconcile-pr-provider-quota-schedules-provider-cooldown.sh +106 -0
  162. package/tools/tests/test-agent-project-reconcile-pr-session-initializes-shared-agent-home.sh +66 -0
  163. package/tools/tests/test-agent-project-reconcile-pr-updated-branch-noop.sh +129 -0
  164. package/tools/tests/test-audit-agent-worktrees-active-launch-skips-git-inspection.sh +69 -0
  165. package/tools/tests/test-audit-agent-worktrees-broken-worktree.sh +43 -0
  166. package/tools/tests/test-audit-agent-worktrees-pending-launch-owner.sh +46 -0
  167. package/tools/tests/test-audit-agent-worktrees-unreconciled-owner.sh +79 -0
  168. package/tools/tests/test-audit-issue-routing-managed-branch-globs.sh +56 -0
  169. package/tools/tests/test-branch-verification-guard-generated-artifacts.sh +72 -0
  170. package/tools/tests/test-branch-verification-guard-targeted-coverage.sh +125 -0
  171. package/tools/tests/test-codex-quota-manager-failure-driven-rotation.sh +178 -0
  172. package/tools/tests/test-codex-quota-wrapper.sh +37 -0
  173. package/tools/tests/test-contribution-docs.sh +18 -0
  174. package/tools/tests/test-control-plane-dashboard-runtime-smoke.sh +343 -0
  175. package/tools/tests/test-create-follow-up-issue.sh +73 -0
  176. package/tools/tests/test-dashboard-launchd-bootstrap.sh +55 -0
  177. package/tools/tests/test-flow-export-execution-env-exports-repo-id.sh +30 -0
  178. package/tools/tests/test-flow-export-github-cli-auth-env-prefers-git-credential.sh +48 -0
  179. package/tools/tests/test-flow-github-api-repo-fallback-preserves-input.sh +85 -0
  180. package/tools/tests/test-flow-github-api-repo-prefers-explicit-repository-id.sh +60 -0
  181. package/tools/tests/test-flow-github-issue-list-falls-back-to-repository-id.sh +64 -0
  182. package/tools/tests/test-flow-github-pr-list-falls-back-to-repository-id.sh +77 -0
  183. package/tools/tests/test-flow-resident-can-reuse-does-not-leak-metadata.sh +52 -0
  184. package/tools/tests/test-flow-resident-reap-stale-controllers.sh +63 -0
  185. package/tools/tests/test-flow-resolve-codex-quota-tools.sh +104 -0
  186. package/tools/tests/test-flow-runtime-doctor-profile-selection.sh +27 -0
  187. package/tools/tests/test-heartbeat-codex-pr-linked-issue-exclusion.sh +79 -0
  188. package/tools/tests/test-heartbeat-hooks-enqueue-resident-issue-for-idle-controller.sh +115 -0
  189. package/tools/tests/test-heartbeat-hooks-enqueue-resident-issue-for-live-lane-controller.sh +117 -0
  190. package/tools/tests/test-heartbeat-hooks-start-resident-issue-loop-claude.sh +96 -0
  191. package/tools/tests/test-heartbeat-hooks-start-resident-issue-loop-codex.sh +96 -0
  192. package/tools/tests/test-heartbeat-hooks-start-resident-issue-loop.sh +96 -0
  193. package/tools/tests/test-heartbeat-loop-auth-wait-does-not-consume-capacity.sh +170 -0
  194. package/tools/tests/test-heartbeat-loop-blocked-recovery-lane.sh +201 -0
  195. package/tools/tests/test-heartbeat-loop-blocked-recovery-vs-pr-reservation.sh +201 -0
  196. package/tools/tests/test-heartbeat-loop-idle-resident-controller-does-not-block-launches.sh +160 -0
  197. package/tools/tests/test-heartbeat-loop-pr-launch-dedup.sh +133 -0
  198. package/tools/tests/test-heartbeat-loop-provider-cooldown-suppresses-launches.sh +157 -0
  199. package/tools/tests/test-heartbeat-loop-reaps-stale-resident-controller.sh +181 -0
  200. package/tools/tests/test-heartbeat-loop-waiting-provider-resident-controller-does-not-block-launches.sh +160 -0
  201. package/tools/tests/test-heartbeat-ready-issues-blocked-recovery.sh +134 -0
  202. package/tools/tests/test-heartbeat-safe-auto-dynamic-concurrency.sh +162 -0
  203. package/tools/tests/test-heartbeat-safe-auto-no-tmux-sessions.sh +136 -0
  204. package/tools/tests/test-heartbeat-safe-auto-openclaw-skips-codex-quota.sh +139 -0
  205. package/tools/tests/test-heartbeat-safe-auto-quota-health-signal.sh +119 -0
  206. package/tools/tests/test-heartbeat-safe-auto-stale-shared-loop-pid-does-not-skip.sh +140 -0
  207. package/tools/tests/test-heartbeat-safe-auto-static-capacity-without-quota-cache.sh +142 -0
  208. package/tools/tests/test-heartbeat-safe-auto-zero-healthy-pools.sh +141 -0
  209. package/tools/tests/test-heartbeat-sync-issue-labels-empty-schedule.sh +65 -0
  210. package/tools/tests/test-heartbeat-sync-open-agent-prs-terminal-clears-running.sh +179 -0
  211. package/tools/tests/test-install-dashboard-launchd.sh +78 -0
  212. package/tools/tests/test-install-project-launchd-adds-tool-paths.sh +87 -0
  213. package/tools/tests/test-install-project-launchd.sh +110 -0
  214. package/tools/tests/test-issue-local-workspace-install-policy.sh +81 -0
  215. package/tools/tests/test-issue-publish-scope-guard-docs-signal.sh +70 -0
  216. package/tools/tests/test-issue-reconcile-hooks-success-clears-blocked.sh +36 -0
  217. package/tools/tests/test-kick-scheduler-requires-explicit-profile.sh +47 -0
  218. package/tools/tests/test-label-follow-up-issues-falls-back-to-repository-id.sh +132 -0
  219. package/tools/tests/test-manual-operator-entrypoints-require-explicit-profile.sh +64 -0
  220. package/tools/tests/test-package-funding-metadata.sh +21 -0
  221. package/tools/tests/test-package-public-metadata.sh +62 -0
  222. package/tools/tests/test-placeholder-worker-adapters.sh +38 -0
  223. package/tools/tests/test-pr-reconcile-hooks-refreshes-recurring-issue-checklist.sh +110 -0
  224. package/tools/tests/test-pr-risk-cohesive-mobile-locale-scope.sh +70 -0
  225. package/tools/tests/test-pr-risk-fix-label-semantics.sh +114 -0
  226. package/tools/tests/test-pr-risk-local-first-no-checks.sh +70 -0
  227. package/tools/tests/test-prepare-worktree-simple-repo-baseline.sh +67 -0
  228. package/tools/tests/test-profile-activate.sh +33 -0
  229. package/tools/tests/test-profile-adopt-allow-missing-repo.sh +68 -0
  230. package/tools/tests/test-profile-adopt-skip-workspace-sync-missing-file.sh +61 -0
  231. package/tools/tests/test-profile-adopt-syncs-anchor-and-workspace.sh +90 -0
  232. package/tools/tests/test-profile-smoke-collision.sh +44 -0
  233. package/tools/tests/test-profile-smoke-invalid-claude-config.sh +31 -0
  234. package/tools/tests/test-profile-smoke-invalid-provider-pool.sh +68 -0
  235. package/tools/tests/test-profile-smoke-repo-slug-mismatch.sh +36 -0
  236. package/tools/tests/test-profile-smoke.sh +45 -0
  237. package/tools/tests/test-project-init-force-and-skip-sync.sh +61 -0
  238. package/tools/tests/test-project-init-repo-slug-mismatch.sh +29 -0
  239. package/tools/tests/test-project-init.sh +66 -0
  240. package/tools/tests/test-project-launchd-bootstrap.sh +66 -0
  241. package/tools/tests/test-project-remove.sh +150 -0
  242. package/tools/tests/test-project-runtime-supervisor.sh +47 -0
  243. package/tools/tests/test-project-runtimectl-launchd.sh +115 -0
  244. package/tools/tests/test-project-runtimectl-missing-profile.sh +54 -0
  245. package/tools/tests/test-project-runtimectl-start-falls-back-to-bootstrap.sh +108 -0
  246. package/tools/tests/test-project-runtimectl-status-reports-supervisor-as-heartbeat-parent.sh +95 -0
  247. package/tools/tests/test-project-runtimectl-status-supervisor-running.sh +59 -0
  248. package/tools/tests/test-project-runtimectl-stop-cancels-pending-kick.sh +85 -0
  249. package/tools/tests/test-project-runtimectl-stop-clears-running-labels.sh +78 -0
  250. package/tools/tests/test-project-runtimectl.sh +212 -0
  251. package/tools/tests/test-provider-cooldown-state-prefers-runtime-worker-context.sh +39 -0
  252. package/tools/tests/test-provider-cooldown-state.sh +59 -0
  253. package/tools/tests/test-public-repo-docs.sh +159 -0
  254. package/tools/tests/test-reconcile-pr-worker-acp-config-routing.sh +75 -0
  255. package/tools/tests/test-render-dashboard-snapshot.sh +149 -0
  256. package/tools/tests/test-render-flow-config-demo-profile.sh +36 -0
  257. package/tools/tests/test-render-flow-config-provider-pool-fallback.sh +81 -0
  258. package/tools/tests/test-render-flow-config.sh +52 -0
  259. package/tools/tests/test-run-codex-task-claude-routing.sh +125 -0
  260. package/tools/tests/test-run-codex-task-codex-resident-routing.sh +108 -0
  261. package/tools/tests/test-run-codex-task-kilo-routing.sh +98 -0
  262. package/tools/tests/test-run-codex-task-openclaw-resident-routing.sh +117 -0
  263. package/tools/tests/test-run-codex-task-openclaw-routing.sh +113 -0
  264. package/tools/tests/test-run-codex-task-opencode-routing.sh +98 -0
  265. package/tools/tests/test-run-codex-task-provider-pool-fallback-routing.sh +146 -0
  266. package/tools/tests/test-scaffold-profile.sh +108 -0
  267. package/tools/tests/test-serve-dashboard.sh +93 -0
  268. package/tools/tests/test-start-issue-worker-blocked-context.sh +129 -0
  269. package/tools/tests/test-start-issue-worker-blocks-complete-recurring-checklist.sh +189 -0
  270. package/tools/tests/test-start-issue-worker-local-install-routing.sh +157 -0
  271. package/tools/tests/test-start-issue-worker-profile-template-routing.sh +149 -0
  272. package/tools/tests/test-start-issue-worker-recurring-resident-reuse-codex.sh +212 -0
  273. package/tools/tests/test-start-issue-worker-recurring-resident-reuse.sh +219 -0
  274. package/tools/tests/test-start-issue-worker-renders-verification-snippet.sh +155 -0
  275. package/tools/tests/test-start-issue-worker-resident-reuse-falls-back-to-new-worktree.sh +199 -0
  276. package/tools/tests/test-start-pr-fix-worker-host-blocker-context.sh +275 -0
  277. package/tools/tests/test-start-resident-issue-loop-adopts-next-recurring-issue.sh +185 -0
  278. package/tools/tests/test-start-resident-issue-loop-clears-pending-while-waiting-due.sh +152 -0
  279. package/tools/tests/test-start-resident-issue-loop-consumes-queued-lease.sh +186 -0
  280. package/tools/tests/test-start-resident-issue-loop-fails-over-provider-pool.sh +212 -0
  281. package/tools/tests/test-start-resident-issue-loop-immediate-cycles.sh +148 -0
  282. package/tools/tests/test-start-resident-issue-loop-waits-for-provider.sh +194 -0
  283. package/tools/tests/test-start-resident-issue-loop-waits-for-terminal-reconcile-status.sh +198 -0
  284. package/tools/tests/test-start-resident-issue-loop-yields-to-live-lane-controller.sh +145 -0
  285. package/tools/tests/test-sync-pr-labels-fix-lane-uses-repair-queued.sh +67 -0
  286. package/tools/tests/test-sync-recurring-issue-checklist-backfills-workflow-complete-blocker.sh +70 -0
  287. package/tools/tests/test-sync-recurring-issue-checklist.sh +95 -0
  288. package/tools/tests/test-sync-shared-agent-home-local-source-root.sh +66 -0
  289. package/tools/tests/test-sync-shared-agent-home-preserves-unrelated-workflow-catalog-skill.sh +47 -0
  290. package/tools/tests/test-test-smoke.sh +86 -0
  291. package/tools/tests/test-uninstall-project-launchd.sh +37 -0
  292. package/tools/tests/test-update-github-labels-prefers-sibling-helper.sh +49 -0
  293. package/tools/tests/test-workflow-catalog.sh +43 -0
  294. package/tools/vendor/codex-quota/LICENSE +21 -0
  295. package/tools/vendor/codex-quota/README.md +459 -0
  296. package/tools/vendor/codex-quota/codex-quota.js +261 -0
  297. package/tools/vendor/codex-quota/lib/claude-accounts.js +226 -0
  298. package/tools/vendor/codex-quota/lib/claude-oauth.js +174 -0
  299. package/tools/vendor/codex-quota/lib/claude-tokens.js +471 -0
  300. package/tools/vendor/codex-quota/lib/claude-usage.js +929 -0
  301. package/tools/vendor/codex-quota/lib/codex-accounts.js +205 -0
  302. package/tools/vendor/codex-quota/lib/codex-tokens.js +326 -0
  303. package/tools/vendor/codex-quota/lib/codex-usage.js +32 -0
  304. package/tools/vendor/codex-quota/lib/color.js +72 -0
  305. package/tools/vendor/codex-quota/lib/constants.js +57 -0
  306. package/tools/vendor/codex-quota/lib/container.js +143 -0
  307. package/tools/vendor/codex-quota/lib/display.js +1111 -0
  308. package/tools/vendor/codex-quota/lib/fs.js +63 -0
  309. package/tools/vendor/codex-quota/lib/handlers.js +2060 -0
  310. package/tools/vendor/codex-quota/lib/jwt.js +33 -0
  311. package/tools/vendor/codex-quota/lib/oauth.js +486 -0
  312. package/tools/vendor/codex-quota/lib/paths.js +34 -0
  313. package/tools/vendor/codex-quota/lib/prompts.js +44 -0
  314. package/tools/vendor/codex-quota/lib/sync.js +1438 -0
  315. package/tools/vendor/codex-quota/lib/token-match.js +96 -0
  316. package/tools/vendor/codex-quota-manager/scripts/auto-switch.sh +500 -0
  317. package/tools/vendor/codex-quota-manager/scripts/batch-add.sh +123 -0
@@ -0,0 +1,657 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+
4
+ usage() {
5
+ cat <<'EOF'
6
+ Usage:
7
+ agent-project-run-claude-session --mode safe|bypass --session <id> --worktree <path> --prompt-file <path> --runs-root <path> --adapter-id <id> --task-kind <kind> --task-id <id> [options]
8
+
9
+ Launch a Claude Code worker session inside tmux for a project adapter and
10
+ persist the standard run artifacts.
11
+
12
+ Options:
13
+ --claude-model <name> Claude model alias or full name
14
+ --claude-permission-mode <mode> Claude permission mode (e.g. dontAsk, bypassPermissions)
15
+ --claude-effort <level> Claude effort level (low, medium, high, max)
16
+ --claude-timeout-seconds <secs> Claude command timeout (default: 900)
17
+ --claude-max-attempts <count> Retry transient failures this many times (default: 3)
18
+ --claude-retry-backoff-seconds <s>
19
+ Sleep between transient retries (default: 30)
20
+ --env-prefix <prefix> Export prefixed runtime/context env vars inside the worker
21
+ --context <KEY=VALUE> Extra metadata written to run.env and exported to the worker
22
+ --collect-file <name> Copy sandbox artifact file into the host run dir after execution
23
+ --reconcile-command <cmd> Host-side command queued after the worker exits
24
+ --sandbox-subdir <name> Subdir under the worktree for worker artifacts (default: .openclaw-artifacts)
25
+ --help Show this help
26
+ EOF
27
+ }
28
+
29
+ mode=""
30
+ session=""
31
+ worktree=""
32
+ prompt_file=""
33
+ runs_root=""
34
+ adapter_id=""
35
+ task_kind=""
36
+ task_id=""
37
+ claude_model="${ACP_CLAUDE_MODEL:-${F_LOSNING_CLAUDE_MODEL:-sonnet}}"
38
+ claude_permission_mode="${ACP_CLAUDE_PERMISSION_MODE:-${F_LOSNING_CLAUDE_PERMISSION_MODE:-dontAsk}}"
39
+ claude_effort="${ACP_CLAUDE_EFFORT:-${F_LOSNING_CLAUDE_EFFORT:-medium}}"
40
+ claude_timeout_seconds="${ACP_CLAUDE_TIMEOUT_SECONDS:-${F_LOSNING_CLAUDE_TIMEOUT_SECONDS:-900}}"
41
+ claude_max_attempts="${ACP_CLAUDE_MAX_ATTEMPTS:-${F_LOSNING_CLAUDE_MAX_ATTEMPTS:-3}}"
42
+ claude_retry_backoff_seconds="${ACP_CLAUDE_RETRY_BACKOFF_SECONDS:-${F_LOSNING_CLAUDE_RETRY_BACKOFF_SECONDS:-30}}"
43
+ env_prefix=""
44
+ sandbox_subdir=".openclaw-artifacts"
45
+ reconcile_command=""
46
+ declare -a context_items=()
47
+ declare -a collect_files=()
48
+
49
+ resolve_claude_bin() {
50
+ local configured_bin="${CLAUDE_BIN:-${ACP_CLAUDE_BIN:-${F_LOSNING_CLAUDE_BIN:-}}}"
51
+
52
+ if [[ -n "${configured_bin}" && -x "${configured_bin}" ]]; then
53
+ printf '%s\n' "${configured_bin}"
54
+ return 0
55
+ fi
56
+
57
+ if command -v claude >/dev/null 2>&1; then
58
+ command -v claude
59
+ return 0
60
+ fi
61
+
62
+ return 1
63
+ }
64
+
65
+ resolve_python_bin() {
66
+ if command -v python3 >/dev/null 2>&1; then
67
+ command -v python3
68
+ return 0
69
+ fi
70
+ if [[ -x /opt/homebrew/bin/python3 ]]; then
71
+ printf '%s\n' "/opt/homebrew/bin/python3"
72
+ return 0
73
+ fi
74
+ if command -v python >/dev/null 2>&1; then
75
+ command -v python
76
+ return 0
77
+ fi
78
+ return 1
79
+ }
80
+
81
+ while [[ $# -gt 0 ]]; do
82
+ case "$1" in
83
+ --mode) mode="${2:-}"; shift 2 ;;
84
+ --session) session="${2:-}"; shift 2 ;;
85
+ --worktree) worktree="${2:-}"; shift 2 ;;
86
+ --prompt-file) prompt_file="${2:-}"; shift 2 ;;
87
+ --runs-root) runs_root="${2:-}"; shift 2 ;;
88
+ --adapter-id) adapter_id="${2:-}"; shift 2 ;;
89
+ --task-kind) task_kind="${2:-}"; shift 2 ;;
90
+ --task-id) task_id="${2:-}"; shift 2 ;;
91
+ --claude-model) claude_model="${2:-}"; shift 2 ;;
92
+ --claude-permission-mode) claude_permission_mode="${2:-}"; shift 2 ;;
93
+ --claude-effort) claude_effort="${2:-}"; shift 2 ;;
94
+ --claude-timeout-seconds) claude_timeout_seconds="${2:-}"; shift 2 ;;
95
+ --claude-max-attempts) claude_max_attempts="${2:-}"; shift 2 ;;
96
+ --claude-retry-backoff-seconds) claude_retry_backoff_seconds="${2:-}"; shift 2 ;;
97
+ --env-prefix) env_prefix="${2:-}"; shift 2 ;;
98
+ --context) context_items+=("${2:-}"); shift 2 ;;
99
+ --collect-file) collect_files+=("${2:-}"); shift 2 ;;
100
+ --reconcile-command) reconcile_command="${2:-}"; shift 2 ;;
101
+ --sandbox-subdir) sandbox_subdir="${2:-}"; shift 2 ;;
102
+ --help|-h) usage; exit 0 ;;
103
+ *) echo "Unknown argument: $1" >&2; usage >&2; exit 1 ;;
104
+ esac
105
+ done
106
+
107
+ if [[ -z "$mode" || -z "$session" || -z "$worktree" || -z "$prompt_file" || -z "$runs_root" || -z "$adapter_id" || -z "$task_kind" || -z "$task_id" ]]; then
108
+ usage >&2
109
+ exit 1
110
+ fi
111
+
112
+ case "$mode" in
113
+ safe|bypass) ;;
114
+ *)
115
+ echo "--mode must be safe or bypass" >&2
116
+ exit 1
117
+ ;;
118
+ esac
119
+
120
+ case "$claude_effort" in
121
+ low|medium|high|max) ;;
122
+ *)
123
+ echo "--claude-effort must be one of: low, medium, high, max" >&2
124
+ exit 1
125
+ ;;
126
+ esac
127
+
128
+ case "$claude_timeout_seconds" in
129
+ ''|*[!0-9]*|0) echo "--claude-timeout-seconds must be a positive integer" >&2; exit 1 ;;
130
+ esac
131
+
132
+ case "$claude_max_attempts" in
133
+ ''|*[!0-9]*|0) echo "--claude-max-attempts must be a positive integer" >&2; exit 1 ;;
134
+ esac
135
+
136
+ case "$claude_retry_backoff_seconds" in
137
+ ''|*[!0-9]*) echo "--claude-retry-backoff-seconds must be numeric" >&2; exit 1 ;;
138
+ esac
139
+
140
+ claude_bin="$(resolve_claude_bin || true)"
141
+ if [[ -z "${claude_bin}" || ! -x "${claude_bin}" ]]; then
142
+ echo "unable to resolve a runnable claude binary" >&2
143
+ exit 1
144
+ fi
145
+
146
+ python_bin="$(resolve_python_bin || true)"
147
+ if [[ -z "${python_bin}" || ! -x "${python_bin}" ]]; then
148
+ echo "unable to resolve a runnable python interpreter for claude timeout control" >&2
149
+ exit 1
150
+ fi
151
+
152
+ artifact_dir="${runs_root}/${session}"
153
+ output_file="${artifact_dir}/${session}.log"
154
+ inner_script="${artifact_dir}/${session}.sh"
155
+ meta_file="${artifact_dir}/run.env"
156
+ result_file="${artifact_dir}/result.env"
157
+ runner_state_file="${artifact_dir}/runner.env"
158
+ sandbox_run_dir="${worktree%/}/${sandbox_subdir}/${session}"
159
+ started_at="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
160
+
161
+ mkdir -p "$artifact_dir"
162
+ mkdir -p "$sandbox_run_dir"
163
+
164
+ if tmux has-session -t "$session" 2>/dev/null; then
165
+ echo "tmux session already exists: $session" >&2
166
+ exit 1
167
+ fi
168
+
169
+ branch_name="$(git -C "$worktree" branch --show-current 2>/dev/null || true)"
170
+
171
+ printf -v session_q '%q' "$session"
172
+ printf -v task_kind_q '%q' "$task_kind"
173
+ printf -v task_id_q '%q' "$task_id"
174
+ printf -v mode_q '%q' "$mode"
175
+ printf -v worktree_q '%q' "$worktree"
176
+ printf -v prompt_q '%q' "$prompt_file"
177
+ printf -v output_q '%q' "$output_file"
178
+ printf -v artifact_dir_q '%q' "$artifact_dir"
179
+ printf -v script_q '%q' "$inner_script"
180
+ printf -v result_q '%q' "$result_file"
181
+ printf -v meta_file_q '%q' "$meta_file"
182
+ printf -v runner_state_q '%q' "$runner_state_file"
183
+ printf -v branch_q '%q' "$branch_name"
184
+ printf -v sandbox_run_dir_q '%q' "$sandbox_run_dir"
185
+ printf -v adapter_id_q '%q' "$adapter_id"
186
+ printf -v started_at_q '%q' "$started_at"
187
+ printf -v claude_bin_q '%q' "$claude_bin"
188
+ printf -v claude_model_q '%q' "$claude_model"
189
+ printf -v claude_permission_mode_q '%q' "$claude_permission_mode"
190
+ printf -v claude_effort_q '%q' "$claude_effort"
191
+ printf -v claude_timeout_q '%q' "$claude_timeout_seconds"
192
+ printf -v claude_max_attempts_q '%q' "$claude_max_attempts"
193
+ printf -v claude_retry_backoff_q '%q' "$claude_retry_backoff_seconds"
194
+ printf -v python_bin_q '%q' "$python_bin"
195
+ printf -v sandbox_subdir_q '%q' "$sandbox_subdir"
196
+ printf -v claude_thread_id_q '%q' "claude-print-${session}"
197
+
198
+ {
199
+ printf 'TASK_KIND=%s\n' "$task_kind_q"
200
+ printf 'TASK_ID=%s\n' "$task_id_q"
201
+ printf 'SESSION=%s\n' "$session_q"
202
+ printf 'MODE=%s\n' "$mode_q"
203
+ printf 'WORKTREE=%s\n' "$worktree_q"
204
+ printf 'PROMPT_FILE=%s\n' "$prompt_q"
205
+ printf 'OUTPUT_FILE=%s\n' "$output_q"
206
+ printf 'SCRIPT=%s\n' "$script_q"
207
+ printf 'BRANCH=%s\n' "$branch_q"
208
+ printf 'RESULT_FILE=%s\n' "$result_q"
209
+ printf 'RUNNER_STATE_FILE=%s\n' "$runner_state_q"
210
+ printf 'SANDBOX_RUN_DIR=%s\n' "$sandbox_run_dir_q"
211
+ printf 'ADAPTER_ID=%s\n' "$adapter_id_q"
212
+ printf 'STARTED_AT=%s\n' "$started_at_q"
213
+ printf 'CLAUDE_BIN=%s\n' "$claude_bin_q"
214
+ printf 'CLAUDE_MODEL=%s\n' "$claude_model_q"
215
+ printf 'CLAUDE_PERMISSION_MODE=%s\n' "$claude_permission_mode_q"
216
+ printf 'CLAUDE_EFFORT=%s\n' "$claude_effort_q"
217
+ printf 'CLAUDE_TIMEOUT_SECONDS=%s\n' "$claude_timeout_q"
218
+ printf 'CLAUDE_MAX_ATTEMPTS=%s\n' "$claude_max_attempts_q"
219
+ printf 'CLAUDE_RETRY_BACKOFF_SECONDS=%s\n' "$claude_retry_backoff_q"
220
+ printf 'PYTHON_BIN=%s\n' "$python_bin_q"
221
+ } >"$meta_file"
222
+
223
+ context_exports=""
224
+ if ((${#context_items[@]} > 0)); then
225
+ for item in "${context_items[@]}"; do
226
+ if [[ "$item" != *=* ]]; then
227
+ echo "--context must use KEY=VALUE syntax: $item" >&2
228
+ exit 1
229
+ fi
230
+ key="${item%%=*}"
231
+ value="${item#*=}"
232
+ if [[ ! "$key" =~ ^[A-Z0-9_]+$ ]]; then
233
+ echo "Invalid context key: $key" >&2
234
+ exit 1
235
+ fi
236
+ printf -v value_q '%q' "$value"
237
+ printf '%s=%s\n' "$key" "$value_q" >>"$meta_file"
238
+ if [[ -n "$env_prefix" ]]; then
239
+ context_exports+="export ${env_prefix}${key}=${value_q}"$'\n'
240
+ fi
241
+ context_exports+="export ACP_${key}=${value_q}"$'\n'
242
+ if [[ "$env_prefix" != "F_LOSNING_" ]]; then
243
+ context_exports+="export F_LOSNING_${key}=${value_q}"$'\n'
244
+ fi
245
+ done
246
+ fi
247
+
248
+ runtime_exports=$(
249
+ cat <<EOF
250
+ export AGENT_PROJECT_SESSION=${session_q}
251
+ export AGENT_PROJECT_RUN_DIR=${sandbox_run_dir_q}
252
+ export AGENT_PROJECT_HOST_RUN_DIR=${artifact_dir_q}
253
+ export AGENT_PROJECT_RESULT_FILE=${sandbox_run_dir_q}/result.env
254
+ export AGENT_PROJECT_CLAUDE_BIN=${claude_bin_q}
255
+ export ACP_SESSION=${session_q}
256
+ export ACP_RUN_DIR=${sandbox_run_dir_q}
257
+ export ACP_HOST_RUN_DIR=${artifact_dir_q}
258
+ export ACP_RESULT_FILE=${sandbox_run_dir_q}/result.env
259
+ export ACP_CLAUDE_BIN=${claude_bin_q}
260
+ export ACP_CLAUDE_MODEL=${claude_model_q}
261
+ export ACP_CLAUDE_PERMISSION_MODE=${claude_permission_mode_q}
262
+ export ACP_CLAUDE_EFFORT=${claude_effort_q}
263
+ export ACP_CLAUDE_TIMEOUT_SECONDS=${claude_timeout_q}
264
+ export ACP_CLAUDE_MAX_ATTEMPTS=${claude_max_attempts_q}
265
+ export ACP_CLAUDE_RETRY_BACKOFF_SECONDS=${claude_retry_backoff_q}
266
+ export F_LOSNING_SESSION=${session_q}
267
+ export F_LOSNING_RUN_DIR=${sandbox_run_dir_q}
268
+ export F_LOSNING_HOST_RUN_DIR=${artifact_dir_q}
269
+ export F_LOSNING_RESULT_FILE=${sandbox_run_dir_q}/result.env
270
+ export F_LOSNING_CLAUDE_BIN=${claude_bin_q}
271
+ export F_LOSNING_CLAUDE_MODEL=${claude_model_q}
272
+ export F_LOSNING_CLAUDE_PERMISSION_MODE=${claude_permission_mode_q}
273
+ export F_LOSNING_CLAUDE_EFFORT=${claude_effort_q}
274
+ export F_LOSNING_CLAUDE_TIMEOUT_SECONDS=${claude_timeout_q}
275
+ export F_LOSNING_CLAUDE_MAX_ATTEMPTS=${claude_max_attempts_q}
276
+ export F_LOSNING_CLAUDE_RETRY_BACKOFF_SECONDS=${claude_retry_backoff_q}
277
+ EOF
278
+ )
279
+
280
+ if [[ -n "$env_prefix" ]]; then
281
+ runtime_exports+=$'\n'
282
+ runtime_exports+=$(cat <<EOF
283
+ export ${env_prefix}SESSION=${session_q}
284
+ export ${env_prefix}RUN_DIR=${sandbox_run_dir_q}
285
+ export ${env_prefix}HOST_RUN_DIR=${artifact_dir_q}
286
+ export ${env_prefix}RESULT_FILE=${sandbox_run_dir_q}/result.env
287
+ export ${env_prefix}CLAUDE_BIN=${claude_bin_q}
288
+ export ${env_prefix}CLAUDE_MODEL=${claude_model_q}
289
+ export ${env_prefix}CLAUDE_PERMISSION_MODE=${claude_permission_mode_q}
290
+ export ${env_prefix}CLAUDE_EFFORT=${claude_effort_q}
291
+ export ${env_prefix}CLAUDE_TIMEOUT_SECONDS=${claude_timeout_q}
292
+ export ${env_prefix}CLAUDE_MAX_ATTEMPTS=${claude_max_attempts_q}
293
+ export ${env_prefix}CLAUDE_RETRY_BACKOFF_SECONDS=${claude_retry_backoff_q}
294
+ EOF
295
+ )
296
+ fi
297
+
298
+ collect_copy_snippet=""
299
+ if ((${#collect_files[@]} > 0)); then
300
+ for artifact_name in "${collect_files[@]}"; do
301
+ if [[ -z "$artifact_name" ]]; then
302
+ continue
303
+ fi
304
+ printf -v artifact_q '%q' "$artifact_name"
305
+ collect_copy_snippet+=$(
306
+ cat <<EOF
307
+ if [[ -f ${sandbox_run_dir_q}/${artifact_q} ]]; then
308
+ cp ${sandbox_run_dir_q}/${artifact_q} ${artifact_dir_q}/${artifact_q}
309
+ fi
310
+ EOF
311
+ )
312
+ collect_copy_snippet+=$'\n'
313
+ done
314
+ fi
315
+
316
+ reconcile_snippet=""
317
+ if [[ -n "$reconcile_command" ]]; then
318
+ printf -v delayed_reconcile_q '%q' "sleep 2; $reconcile_command"
319
+ reconcile_snippet="nohup bash -lc ${delayed_reconcile_q} >> ${output_q} 2>&1 </dev/null &"
320
+ fi
321
+
322
+ cat >"$inner_script" <<EOF
323
+ #!/usr/bin/env bash
324
+ set -euo pipefail
325
+ ${runtime_exports}
326
+ ${context_exports}cd ${worktree_q}
327
+
328
+ runner_state_file=${runner_state_q}
329
+ output_file=${output_q}
330
+ sandbox_run_dir=${sandbox_run_dir_q}
331
+ artifact_dir=${artifact_dir_q}
332
+ result_file_path=${sandbox_run_dir_q}/result.env
333
+ host_result_file=${result_q}
334
+ claude_bin=${claude_bin_q}
335
+ claude_model=${claude_model_q}
336
+ claude_permission_mode=${claude_permission_mode_q}
337
+ claude_effort=${claude_effort_q}
338
+ claude_timeout_seconds=${claude_timeout_q}
339
+ claude_max_attempts=${claude_max_attempts_q}
340
+ claude_retry_backoff_seconds=${claude_retry_backoff_q}
341
+ python_bin=${python_bin_q}
342
+ worktree_root=${worktree_q}
343
+ sandbox_subdir=${sandbox_subdir_q}
344
+ prompt_file=${prompt_q}
345
+
346
+ write_state() {
347
+ local runner_state="\${1:?runner state required}"
348
+ local last_exit_code="\${2:-}"
349
+ local failure_reason="\${3:-}"
350
+ local attempt="\${4:-1}"
351
+ local resume_count="\${5:-0}"
352
+ local updated_at tmp_file
353
+
354
+ updated_at="\$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
355
+ tmp_file="\${runner_state_file}.tmp.\$\$"
356
+ {
357
+ printf 'RUNNER_STATE=%q\n' "\${runner_state}"
358
+ printf 'THREAD_ID=%q\n' ${claude_thread_id_q}
359
+ printf 'ATTEMPT=%q\n' "\${attempt}"
360
+ printf 'RESUME_COUNT=%q\n' "\${resume_count}"
361
+ printf 'LAST_EXIT_CODE=%q\n' "\${last_exit_code}"
362
+ printf 'LAST_FAILURE_REASON=%q\n' "\${failure_reason}"
363
+ printf 'LAST_TRIGGER_REASON=%q\n' ''
364
+ printf 'AUTH_WAIT_STARTED_AT=%q\n' ''
365
+ printf 'LAST_AUTH_FINGERPRINT=%q\n' ''
366
+ printf 'UPDATED_AT=%q\n' "\${updated_at}"
367
+ } >"\${tmp_file}"
368
+ mv "\${tmp_file}" "\${runner_state_file}"
369
+ }
370
+
371
+ run_with_timeout() {
372
+ local timeout_seconds="\${1:?timeout seconds required}"
373
+ shift
374
+
375
+ "\${python_bin}" - "\${timeout_seconds}" "\$@" <<'PY'
376
+ import os
377
+ import signal
378
+ import subprocess
379
+ import sys
380
+
381
+ timeout_seconds = float(sys.argv[1])
382
+ argv = sys.argv[2:]
383
+
384
+ if not argv:
385
+ sys.exit(64)
386
+
387
+ proc = subprocess.Popen(argv, start_new_session=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
388
+
389
+ try:
390
+ stdout, stderr = proc.communicate(timeout=timeout_seconds)
391
+ except subprocess.TimeoutExpired:
392
+ try:
393
+ os.killpg(proc.pid, signal.SIGTERM)
394
+ except ProcessLookupError:
395
+ pass
396
+ try:
397
+ stdout, stderr = proc.communicate(timeout=2)
398
+ except subprocess.TimeoutExpired:
399
+ try:
400
+ os.killpg(proc.pid, signal.SIGKILL)
401
+ except ProcessLookupError:
402
+ pass
403
+ stdout, stderr = proc.communicate()
404
+ if stdout:
405
+ sys.stdout.buffer.write(stdout)
406
+ if stderr:
407
+ sys.stderr.buffer.write(stderr)
408
+ sys.exit(124)
409
+
410
+ if stdout:
411
+ sys.stdout.buffer.write(stdout)
412
+ if stderr:
413
+ sys.stderr.buffer.write(stderr)
414
+ sys.exit(proc.returncode)
415
+ PY
416
+ }
417
+
418
+ record_final_git_state() {
419
+ local final_head final_branch tmp_file
420
+
421
+ final_head="\$(git -C ${worktree_q} rev-parse HEAD 2>/dev/null || true)"
422
+ final_branch="\$(git -C ${worktree_q} branch --show-current 2>/dev/null || true)"
423
+ tmp_file=${meta_file_q}.tmp.final.\$\$
424
+ grep -vE '^(FINAL_HEAD|FINAL_BRANCH)=' ${meta_file_q} >"\${tmp_file}" 2>/dev/null || true
425
+ {
426
+ printf 'FINAL_HEAD=%q\n' "\${final_head}"
427
+ printf 'FINAL_BRANCH=%q\n' "\${final_branch}"
428
+ } >>"\${tmp_file}"
429
+ mv "\${tmp_file}" ${meta_file_q}
430
+ }
431
+
432
+ ensure_workspace_excludes() {
433
+ local exclude_file line sandbox_pattern
434
+ exclude_file="\$(git -C ${worktree_q} config --worktree --get core.excludesFile 2>/dev/null || true)"
435
+ if [[ -z "\${exclude_file}" ]]; then
436
+ exclude_file="\${sandbox_run_dir}/git-exclude"
437
+ git -C ${worktree_q} config extensions.worktreeConfig true >/dev/null 2>&1 || true
438
+ git -C ${worktree_q} config --worktree core.excludesFile "\${exclude_file}"
439
+ fi
440
+
441
+ mkdir -p "\$(dirname "\${exclude_file}")"
442
+ touch "\${exclude_file}"
443
+ sandbox_pattern="\${sandbox_subdir#./}"
444
+ sandbox_pattern="\${sandbox_pattern#/}"
445
+ while IFS= read -r line; do
446
+ [[ -n "\${line}" ]] || continue
447
+ if ! grep -Fqx "\${line}" "\${exclude_file}" 2>/dev/null; then
448
+ printf '%s\n' "\${line}" >>"\${exclude_file}"
449
+ fi
450
+ done <<PATTERNS
451
+ \${sandbox_pattern}
452
+ SOUL.md
453
+ TOOLS.md
454
+ IDENTITY.md
455
+ USER.md
456
+ HEARTBEAT.md
457
+ BOOTSTRAP.md
458
+ .agent-session.env
459
+ PATTERNS
460
+ }
461
+
462
+ install_pre_commit_scope_hook() {
463
+ local hooks_dir="\$(git -C ${worktree_q} rev-parse --git-path hooks 2>/dev/null || true)"
464
+ if [[ -z "\${hooks_dir}" ]]; then
465
+ hooks_dir="\$(git -C ${worktree_q} config --get core.hooksPath 2>/dev/null || true)"
466
+ fi
467
+ if [[ -z "\${hooks_dir}" ]]; then
468
+ hooks_dir="${worktree_q}/.git-hooks"
469
+ fi
470
+ if [[ "\${hooks_dir}" != /* ]]; then
471
+ hooks_dir="${worktree_q}/\${hooks_dir}"
472
+ fi
473
+ mkdir -p "\${hooks_dir}"
474
+ cat >"\${hooks_dir}/pre-commit" <<'HOOK_EOF'
475
+ #!/usr/bin/env bash
476
+ set -euo pipefail
477
+
478
+ changed_files="\$(
479
+ git diff --cached --name-only --diff-filter=ACMR 2>/dev/null || true
480
+ )"
481
+
482
+ if [[ -z "\${changed_files}" ]]; then
483
+ exit 0
484
+ fi
485
+
486
+ api_count=0
487
+ web_count=0
488
+ mobile_count=0
489
+ package_count=0
490
+ doc_count=0
491
+ other_count=0
492
+
493
+ while IFS= read -r file; do
494
+ [[ -n "\${file}" ]] || continue
495
+ case "\${file}" in
496
+ apps/api/*) api_count=\$((api_count + 1)) ;;
497
+ apps/web/*) web_count=\$((web_count + 1)) ;;
498
+ apps/mobile/*) mobile_count=\$((mobile_count + 1)) ;;
499
+ packages/*) package_count=\$((package_count + 1)) ;;
500
+ openspec/*|docs/*|*.md) doc_count=\$((doc_count + 1)) ;;
501
+ *) other_count=\$((other_count + 1)) ;;
502
+ esac
503
+ done <<< "\${changed_files}"
504
+
505
+ surfaces=0
506
+ [[ \$api_count -gt 0 ]] && surfaces=\$((surfaces + 1))
507
+ [[ \$web_count -gt 0 ]] && surfaces=\$((surfaces + 1))
508
+ [[ \$mobile_count -gt 0 ]] && surfaces=\$((surfaces + 1))
509
+
510
+ if [[ \$surfaces -ge 3 ]]; then
511
+ echo "[pre-commit scope warning] This commit touches \$surfaces product surfaces (api=\$api_count web=\$web_count mobile=\$mobile_count). Consider splitting into focused commits." >&2
512
+ fi
513
+
514
+ total_product=\$((api_count + web_count + mobile_count + package_count + other_count))
515
+ if [[ \$total_product -gt 20 ]]; then
516
+ echo "[pre-commit scope BLOCK] This commit touches \$total_product product files across \$surfaces surfaces. Split into smaller focused commits." >&2
517
+ exit 1
518
+ fi
519
+
520
+ if [[ \$mobile_count -gt 8 ]]; then
521
+ echo "[pre-commit scope BLOCK] This commit touches \$mobile_count mobile product files. Keep mobile changes to one focused route family (max 8 files)." >&2
522
+ exit 1
523
+ fi
524
+
525
+ exit 0
526
+ HOOK_EOF
527
+ chmod +x "\${hooks_dir}/pre-commit"
528
+ }
529
+
530
+ classify_failure_reason() {
531
+ local log_file="\${1:-\${output_file}}"
532
+
533
+ if grep -Eiq 'authentication|unauthorized|login required|invalid api key|api key' "\${log_file}" 2>/dev/null; then
534
+ printf 'auth-failure\n'
535
+ return 0
536
+ fi
537
+ if grep -Eiq 'rate limit|quota exceeded|insufficient credits|payment required|429' "\${log_file}" 2>/dev/null; then
538
+ printf 'provider-quota-limit\n'
539
+ return 0
540
+ fi
541
+ if grep -Eiq 'model .* not available|unsupported model|invalid model|model not found' "\${log_file}" 2>/dev/null; then
542
+ printf 'model-unavailable\n'
543
+ return 0
544
+ fi
545
+ if grep -Eiq 'connection reset|connection error|network error|temporarily unavailable|ECONNRESET|ECONNREFUSED|ENOTFOUND|EAI_AGAIN' "\${log_file}" 2>/dev/null; then
546
+ printf 'network-connection\n'
547
+ return 0
548
+ fi
549
+ if grep -Eiq 'timeout|timed out|ETIMEDOUT' "\${log_file}" 2>/dev/null; then
550
+ printf 'timeout\n'
551
+ return 0
552
+ fi
553
+ printf 'claude-exit-failed\n'
554
+ }
555
+
556
+ is_retryable_failure_reason() {
557
+ case "\${1:-}" in
558
+ network-connection|timeout) return 0 ;;
559
+ *) return 1 ;;
560
+ esac
561
+ }
562
+
563
+ write_result_fallback() {
564
+ local detail="\${1:-missing-result-contract}"
565
+ cat >"\${host_result_file}" <<RESULT
566
+ OUTCOME=blocked
567
+ ACTION=host-comment-blocker
568
+ DETAIL=\${detail}
569
+ RESULT
570
+ cp "\${host_result_file}" "\${result_file_path}" 2>/dev/null || true
571
+ }
572
+
573
+ reset_sandbox_run_dir() {
574
+ mkdir -p "\${sandbox_run_dir}"
575
+ find "\${sandbox_run_dir}" -mindepth 1 -maxdepth 1 -exec rm -rf {} + 2>/dev/null || true
576
+ }
577
+
578
+ mkdir -p "\${sandbox_run_dir}" "\${artifact_dir}"
579
+ reset_sandbox_run_dir
580
+ ensure_workspace_excludes
581
+ install_pre_commit_scope_hook
582
+
583
+ prompt_payload="\$(cat "\${prompt_file}")"
584
+ claude_args=(
585
+ -p
586
+ --output-format text
587
+ --no-session-persistence
588
+ --permission-mode "\${claude_permission_mode}"
589
+ --model "\${claude_model}"
590
+ --effort "\${claude_effort}"
591
+ --add-dir ${worktree_q}
592
+ )
593
+ if [[ "\${claude_permission_mode}" == "bypassPermissions" ]]; then
594
+ claude_args+=(--allow-dangerously-skip-permissions)
595
+ fi
596
+
597
+ status=1
598
+ attempt=1
599
+ failure_reason=""
600
+ set +e
601
+ while (( attempt <= claude_max_attempts )); do
602
+ attempt_log_file="\${artifact_dir}/claude-attempt-\${attempt}.log"
603
+ write_state running '' '' "\${attempt}" "\$((attempt - 1))"
604
+ printf '\n[claude-attempt] %s/%s\n' "\${attempt}" "\${claude_max_attempts}" | tee -a "\${output_file}" >/dev/null
605
+ run_with_timeout "\${claude_timeout_seconds}" "\${claude_bin}" "\${claude_args[@]}" "\${prompt_payload}" >"\${attempt_log_file}" 2>&1
606
+ status=\$?
607
+ cat "\${attempt_log_file}" >>"\${output_file}"
608
+ if [[ "\${status}" -eq 0 ]]; then
609
+ failure_reason=""
610
+ break
611
+ fi
612
+ if [[ "\${status}" -eq 124 ]]; then
613
+ failure_reason="timeout"
614
+ else
615
+ failure_reason="\$(classify_failure_reason "\${attempt_log_file}")"
616
+ fi
617
+ if (( attempt >= claude_max_attempts )) || ! is_retryable_failure_reason "\${failure_reason}"; then
618
+ break
619
+ fi
620
+ printf '[claude-retry] reason=%s backoff=%ss\n' "\${failure_reason}" "\${claude_retry_backoff_seconds}" | tee -a "\${output_file}" >/dev/null
621
+ sleep "\${claude_retry_backoff_seconds}"
622
+ attempt=\$((attempt + 1))
623
+ done
624
+ set -e
625
+
626
+ record_final_git_state
627
+ if [[ -f "\${result_file_path}" ]]; then
628
+ cp "\${result_file_path}" "\${host_result_file}"
629
+ else
630
+ if [[ "\${status}" -eq 0 ]]; then
631
+ write_result_fallback "missing-result-contract"
632
+ else
633
+ write_result_fallback "worker-exit-\${status}"
634
+ fi
635
+ fi
636
+
637
+ ${collect_copy_snippet}
638
+ if [[ "\${status}" -eq 0 ]]; then
639
+ write_state completed "\${status}" '' "\${attempt}" "\$((attempt - 1))"
640
+ else
641
+ write_state failed "\${status}" "\${failure_reason}" "\${attempt}" "\$((attempt - 1))"
642
+ fi
643
+ ${reconcile_snippet}
644
+ printf '\n__CLAUDE_EXIT__:%s\n' "\${status}" | tee -a "\${output_file}"
645
+ exit "\${status}"
646
+ EOF
647
+
648
+ chmod +x "$inner_script"
649
+ tmux new-session -d -s "$session" "$inner_script"
650
+
651
+ printf 'SESSION=%s\n' "$session"
652
+ printf 'TASK_KIND=%s\n' "$task_kind"
653
+ printf 'TASK_ID=%s\n' "$task_id"
654
+ printf 'WORKTREE=%s\n' "$worktree"
655
+ printf 'OUTPUT=%s\n' "$output_file"
656
+ printf 'SCRIPT=%s\n' "$inner_script"
657
+ printf 'META=%s\n' "$meta_file"