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,551 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+
4
+ WORKSPACE_DIR="$(cd "$(dirname "$0")/.." && pwd)"
5
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6
+ # shellcheck source=/dev/null
7
+ source "${SCRIPT_DIR}/flow-config-lib.sh"
8
+ flow_export_project_env_aliases
9
+
10
+ FLOW_SKILL_DIR="$(resolve_flow_skill_dir "${BASH_SOURCE[0]}")"
11
+ if ! flow_require_explicit_profile_selection "${FLOW_SKILL_DIR}" "heartbeat-safe-auto.sh"; then
12
+ exit 64
13
+ fi
14
+
15
+ CONFIG_YAML="$(resolve_flow_config_yaml "${BASH_SOURCE[0]}")"
16
+ flow_export_execution_env "${CONFIG_YAML}"
17
+ AGENT_ROOT="$(flow_resolve_agent_root "${CONFIG_YAML}")"
18
+ RUNS_ROOT="$(flow_resolve_runs_root "${CONFIG_YAML}")"
19
+ STATE_ROOT="$(flow_resolve_state_root "${CONFIG_YAML}")"
20
+ MEMORY_DIR="${ACP_MEMORY_DIR:-${F_LOSNING_MEMORY_DIR:-${AGENT_CONTROL_PLANE_WORKSPACE:-$HOME/.agent-runtime/control-plane/workspace}/memory}}"
21
+ REPO_SLUG="$(flow_resolve_repo_slug "${CONFIG_YAML}")"
22
+ MAX_CONCURRENT_WORKERS="${ACP_MAX_CONCURRENT_WORKERS:-${F_LOSNING_MAX_CONCURRENT_WORKERS:-20}}"
23
+ MAX_CONCURRENT_E2E_WORKERS="${ACP_MAX_CONCURRENT_E2E_WORKERS:-${F_LOSNING_MAX_CONCURRENT_E2E_WORKERS:-1}}"
24
+ MAX_CONCURRENT_PR_WORKERS="${ACP_MAX_CONCURRENT_PR_WORKERS:-${F_LOSNING_MAX_CONCURRENT_PR_WORKERS:-12}}"
25
+ MAX_RECURRING_ISSUE_WORKERS="${ACP_MAX_RECURRING_ISSUE_WORKERS:-${F_LOSNING_MAX_RECURRING_ISSUE_WORKERS:-6}}"
26
+ MAX_CONCURRENT_SCHEDULED_ISSUE_WORKERS="${ACP_MAX_CONCURRENT_SCHEDULED_ISSUE_WORKERS:-${F_LOSNING_MAX_CONCURRENT_SCHEDULED_ISSUE_WORKERS:-2}}"
27
+ MAX_CONCURRENT_SCHEDULED_HEAVY_WORKERS="${ACP_MAX_CONCURRENT_SCHEDULED_HEAVY_WORKERS:-${F_LOSNING_MAX_CONCURRENT_SCHEDULED_HEAVY_WORKERS:-1}}"
28
+ MAX_CONCURRENT_BLOCKED_RECOVERY_ISSUE_WORKERS="${ACP_MAX_CONCURRENT_BLOCKED_RECOVERY_ISSUE_WORKERS:-${F_LOSNING_MAX_CONCURRENT_BLOCKED_RECOVERY_ISSUE_WORKERS:-1}}"
29
+ BLOCKED_RECOVERY_COOLDOWN_SECONDS="${ACP_BLOCKED_RECOVERY_COOLDOWN_SECONDS:-${F_LOSNING_BLOCKED_RECOVERY_COOLDOWN_SECONDS:-900}}"
30
+ MAX_OPEN_AGENT_PRS_FOR_RECURRING="${ACP_MAX_OPEN_AGENT_PRS_FOR_RECURRING:-${F_LOSNING_MAX_OPEN_AGENT_PRS_FOR_RECURRING:-12}}"
31
+ MAX_LAUNCHES_PER_HEARTBEAT="${ACP_MAX_LAUNCHES_PER_HEARTBEAT:-${F_LOSNING_MAX_LAUNCHES_PER_HEARTBEAT:-$MAX_CONCURRENT_WORKERS}}"
32
+ CODING_WORKER="${ACP_CODING_WORKER:-${F_LOSNING_CODING_WORKER:-codex}}"
33
+ # The catchup and shared heartbeat passes can legitimately take a few minutes
34
+ # once they reconcile stale sessions, sync labels, and launch multiple workers.
35
+ CATCHUP_TIMEOUT_SECONDS="${ACP_CATCHUP_TIMEOUT_SECONDS:-${F_LOSNING_CATCHUP_TIMEOUT_SECONDS:-180}}"
36
+ HEARTBEAT_LOOP_TIMEOUT_SECONDS="${ACP_HEARTBEAT_LOOP_TIMEOUT_SECONDS:-${F_LOSNING_HEARTBEAT_LOOP_TIMEOUT_SECONDS:-720}}"
37
+ CONFIG_YAML="$(resolve_flow_config_yaml "${BASH_SOURCE[0]}")"
38
+ SHARED_AGENT_HOME="$(resolve_shared_agent_home "${FLOW_SKILL_DIR}")"
39
+ FLOW_TOOLS_DIR="${FLOW_SKILL_DIR}/tools/bin"
40
+ ISSUE_SESSION_PREFIX="$(flow_resolve_issue_session_prefix "${CONFIG_YAML}")"
41
+ PR_SESSION_PREFIX="$(flow_resolve_pr_session_prefix "${CONFIG_YAML}")"
42
+ HOOK_FILE="${FLOW_SKILL_DIR}/hooks/heartbeat-hooks.sh"
43
+ RECOVERY_PREFLIGHT_SCRIPT="${FLOW_TOOLS_DIR}/heartbeat-recovery-preflight.sh"
44
+ HEARTBEAT_PREFLIGHT_ONLY="${ACP_HEARTBEAT_PREFLIGHT_ONLY:-${F_LOSNING_HEARTBEAT_PREFLIGHT_ONLY:-0}}"
45
+ CODEX_QUOTA_AUTOSWITCH_ENABLED="${ACP_CODEX_QUOTA_AUTOSWITCH_ENABLED:-${F_LOSNING_CODEX_QUOTA_AUTOSWITCH_ENABLED:-1}}"
46
+ CODEX_QUOTA_ROTATION_STRATEGY="${ACP_CODEX_QUOTA_ROTATION_STRATEGY:-${F_LOSNING_CODEX_QUOTA_ROTATION_STRATEGY:-failure-driven}}"
47
+ CODEX_QUOTA_THRESHOLD="${ACP_CODEX_QUOTA_THRESHOLD:-${F_LOSNING_CODEX_QUOTA_THRESHOLD:-70}}"
48
+ CODEX_QUOTA_WEEKLY_THRESHOLD="${ACP_CODEX_QUOTA_WEEKLY_THRESHOLD:-${F_LOSNING_CODEX_QUOTA_WEEKLY_THRESHOLD:-90}}"
49
+ CODEX_QUOTA_SOFT_THRESHOLD="${ACP_CODEX_QUOTA_SOFT_THRESHOLD:-${F_LOSNING_CODEX_QUOTA_SOFT_THRESHOLD:-55}}"
50
+ CODEX_QUOTA_SOFT_WORKER_THRESHOLD="${ACP_CODEX_QUOTA_SOFT_WORKER_THRESHOLD:-${F_LOSNING_CODEX_QUOTA_SOFT_WORKER_THRESHOLD:-8}}"
51
+ CODEX_QUOTA_EMERGENCY_THRESHOLD="${ACP_CODEX_QUOTA_EMERGENCY_THRESHOLD:-${F_LOSNING_CODEX_QUOTA_EMERGENCY_THRESHOLD:-65}}"
52
+ CODEX_QUOTA_EMERGENCY_WORKER_THRESHOLD="${ACP_CODEX_QUOTA_EMERGENCY_WORKER_THRESHOLD:-${F_LOSNING_CODEX_QUOTA_EMERGENCY_WORKER_THRESHOLD:-12}}"
53
+ CODEX_QUOTA_SWITCH_COOLDOWN_SECONDS="${ACP_CODEX_QUOTA_SWITCH_COOLDOWN_SECONDS:-${F_LOSNING_CODEX_QUOTA_SWITCH_COOLDOWN_SECONDS:-600}}"
54
+ CODEX_QUOTA_TIMEOUT_SECONDS="${ACP_CODEX_QUOTA_TIMEOUT_SECONDS:-${F_LOSNING_CODEX_QUOTA_TIMEOUT_SECONDS:-45}}"
55
+ CODEX_QUOTA_PREFER_LABEL="${ACP_CODEX_QUOTA_PREFER_LABEL:-${F_LOSNING_CODEX_QUOTA_PREFER_LABEL:-}}"
56
+ CODEX_QUOTA_BIN="$(flow_resolve_codex_quota_bin "${FLOW_SKILL_DIR}")"
57
+ CODEX_QUOTA_MANAGER_SCRIPT="$(flow_resolve_codex_quota_manager_script "${FLOW_SKILL_DIR}")"
58
+ CODEX_QUOTA_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/codex-quota-manager"
59
+ CODEX_QUOTA_FULL_CACHE_FILE="${CODEX_QUOTA_MANAGER_FULL_CACHE_FILE:-${CODEX_QUOTA_CACHE_DIR}/codex-full-quota.json}"
60
+ DYNAMIC_CONCURRENCY_ENABLED="${ACP_DYNAMIC_CONCURRENCY_ENABLED:-${F_LOSNING_DYNAMIC_CONCURRENCY_ENABLED:-1}}"
61
+ ALLOW_INFRA_CI_BYPASS="${ACP_ALLOW_INFRA_CI_BYPASS:-${F_LOSNING_ALLOW_INFRA_CI_BYPASS:-1}}"
62
+ RETAINED_WORKTREE_AUDIT_ENABLED="${ACP_RETAINED_WORKTREE_AUDIT_ENABLED:-${F_LOSNING_RETAINED_WORKTREE_AUDIT_ENABLED:-1}}"
63
+ RETAINED_WORKTREE_AUDIT_TIMEOUT_SECONDS="${ACP_RETAINED_WORKTREE_AUDIT_TIMEOUT_SECONDS:-${F_LOSNING_RETAINED_WORKTREE_AUDIT_TIMEOUT_SECONDS:-30}}"
64
+ AGENT_WORKTREE_AUDIT_ENABLED="${ACP_AGENT_WORKTREE_AUDIT_ENABLED:-${F_LOSNING_AGENT_WORKTREE_AUDIT_ENABLED:-1}}"
65
+ AGENT_WORKTREE_AUDIT_TIMEOUT_SECONDS="${ACP_AGENT_WORKTREE_AUDIT_TIMEOUT_SECONDS:-${F_LOSNING_AGENT_WORKTREE_AUDIT_TIMEOUT_SECONDS:-45}}"
66
+ LOCK_DIR="${STATE_ROOT}/heartbeat-loop.lock"
67
+ PID_FILE="${LOCK_DIR}/pid"
68
+ SHARED_LOOP_PID_FILE="${STATE_ROOT}/shared-heartbeat-loop.pid"
69
+ QUOTA_LOCK_DIR="${STATE_ROOT}/quota-preflight.lock"
70
+ QUOTA_PID_FILE="${QUOTA_LOCK_DIR}/pid"
71
+
72
+ acquire_lock() {
73
+ mkdir -p "${STATE_ROOT}"
74
+
75
+ if mkdir "${LOCK_DIR}" 2>/dev/null; then
76
+ printf '%s\n' "$$" >"${PID_FILE}"
77
+ return 0
78
+ fi
79
+
80
+ if [[ -f "${PID_FILE}" ]]; then
81
+ local existing_pid
82
+ existing_pid="$(cat "${PID_FILE}" 2>/dev/null || true)"
83
+ if [[ -n "${existing_pid}" ]] && kill -0 "${existing_pid}" 2>/dev/null; then
84
+ printf 'HEARTBEAT_SKIPPED=lock-held\n'
85
+ printf 'LOCK_PID=%s\n' "${existing_pid}"
86
+ exit 0
87
+ fi
88
+ fi
89
+
90
+ rm -rf "${LOCK_DIR}"
91
+ mkdir "${LOCK_DIR}"
92
+ printf '%s\n' "$$" >"${PID_FILE}"
93
+ }
94
+
95
+ cleanup() {
96
+ rm -rf "${LOCK_DIR}"
97
+ }
98
+
99
+ acquire_quota_lock() {
100
+ mkdir -p "${STATE_ROOT}"
101
+
102
+ if mkdir "${QUOTA_LOCK_DIR}" 2>/dev/null; then
103
+ printf '%s\n' "$$" >"${QUOTA_PID_FILE}"
104
+ return 0
105
+ fi
106
+
107
+ if [[ -f "${QUOTA_PID_FILE}" ]]; then
108
+ local existing_pid
109
+ existing_pid="$(cat "${QUOTA_PID_FILE}" 2>/dev/null || true)"
110
+ if [[ -n "${existing_pid}" ]] && kill -0 "${existing_pid}" 2>/dev/null; then
111
+ printf '[%s] codex quota preflight skipped lock-held pid=%s\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" "${existing_pid}"
112
+ return 1
113
+ fi
114
+ fi
115
+
116
+ rm -rf "${QUOTA_LOCK_DIR}"
117
+ mkdir "${QUOTA_LOCK_DIR}"
118
+ printf '%s\n' "$$" >"${QUOTA_PID_FILE}"
119
+ return 0
120
+ }
121
+
122
+ release_quota_lock() {
123
+ rm -rf "${QUOTA_LOCK_DIR}"
124
+ }
125
+
126
+ run_with_timeout() {
127
+ local timeout_seconds="${1:?timeout seconds required}"
128
+ shift
129
+
130
+ /opt/homebrew/bin/python3 - "${timeout_seconds}" "$@" <<'PY'
131
+ import os
132
+ from pathlib import Path
133
+ import signal
134
+ import subprocess
135
+ import sys
136
+
137
+ timeout_seconds = float(sys.argv[1])
138
+ argv = sys.argv[2:]
139
+ pid_file = os.environ.get("ACP_TIMEOUT_CHILD_PID_FILE") or os.environ.get("F_LOSNING_TIMEOUT_CHILD_PID_FILE", "")
140
+
141
+ if not argv:
142
+ sys.exit(64)
143
+
144
+ proc = subprocess.Popen(argv, start_new_session=True)
145
+ if pid_file:
146
+ Path(pid_file).write_text(f"{proc.pid}\n", encoding="utf-8")
147
+
148
+ try:
149
+ sys.exit(proc.wait(timeout=timeout_seconds))
150
+ except subprocess.TimeoutExpired:
151
+ try:
152
+ os.killpg(proc.pid, signal.SIGTERM)
153
+ except ProcessLookupError:
154
+ pass
155
+
156
+ try:
157
+ proc.wait(timeout=2)
158
+ except subprocess.TimeoutExpired:
159
+ try:
160
+ os.killpg(proc.pid, signal.SIGKILL)
161
+ except ProcessLookupError:
162
+ pass
163
+ proc.wait()
164
+
165
+ sys.exit(124)
166
+ finally:
167
+ if pid_file:
168
+ try:
169
+ Path(pid_file).unlink()
170
+ except FileNotFoundError:
171
+ pass
172
+ PY
173
+ }
174
+
175
+ active_shared_loop_pid() {
176
+ if [[ ! -f "${SHARED_LOOP_PID_FILE}" ]]; then
177
+ return 1
178
+ fi
179
+
180
+ local pid parent_pid command
181
+ pid="$(tr -d '[:space:]' <"${SHARED_LOOP_PID_FILE}" 2>/dev/null || true)"
182
+ if [[ -z "${pid}" ]]; then
183
+ rm -f "${SHARED_LOOP_PID_FILE}"
184
+ return 1
185
+ fi
186
+
187
+ if kill -0 "${pid}" 2>/dev/null; then
188
+ command="$(ps -p "${pid}" -o command= 2>/dev/null | sed 's/^ *//' || true)"
189
+ if [[ -z "${command}" || "${command}" != *"agent-project-heartbeat-loop --repo-slug ${REPO_SLUG}"* ]]; then
190
+ rm -f "${SHARED_LOOP_PID_FILE}"
191
+ return 1
192
+ fi
193
+ parent_pid="$(ps -p "${pid}" -o ppid= 2>/dev/null | tr -d '[:space:]' || true)"
194
+ if [[ "${parent_pid}" == "1" ]]; then
195
+ kill -TERM -- "-${pid}" 2>/dev/null || true
196
+ sleep 1
197
+ kill -KILL -- "-${pid}" 2>/dev/null || true
198
+ rm -f "${SHARED_LOOP_PID_FILE}"
199
+ return 1
200
+ fi
201
+ printf '%s\n' "${pid}"
202
+ return 0
203
+ fi
204
+
205
+ rm -f "${SHARED_LOOP_PID_FILE}"
206
+ return 1
207
+ }
208
+
209
+ reap_orphan_shared_loop_groups() {
210
+ local ps_lines root_pid root_pgid
211
+ ps_lines="$(
212
+ ps -Ao pid=,ppid=,pgid=,command= 2>/dev/null \
213
+ | awk '/agent-project-heartbeat-loop/ {print $1 "|" $2 "|" $3 "|" $0}'
214
+ )"
215
+
216
+ while IFS='|' read -r root_pid parent_pid root_pgid _; do
217
+ [[ -n "${root_pid}" ]] || continue
218
+ [[ "${parent_pid}" == "1" ]] || continue
219
+ [[ -n "${root_pgid}" ]] || continue
220
+ kill -TERM -- "-${root_pgid}" 2>/dev/null || true
221
+ sleep 1
222
+ kill -KILL -- "-${root_pgid}" 2>/dev/null || true
223
+ done <<<"${ps_lines}"
224
+ }
225
+
226
+ codex_quota_health_reason() {
227
+ if [[ "${CODEX_QUOTA_AUTOSWITCH_ENABLED}" == "0" ]]; then
228
+ printf 'disabled\n'
229
+ return 0
230
+ fi
231
+ if [[ ! -x "${CODEX_QUOTA_MANAGER_SCRIPT}" ]]; then
232
+ printf 'missing-script\n'
233
+ return 0
234
+ fi
235
+ if [[ ! -x "${CODEX_QUOTA_BIN}" ]]; then
236
+ printf 'missing-codex-quota\n'
237
+ return 0
238
+ fi
239
+ if ! command -v jq >/dev/null 2>&1; then
240
+ printf 'missing-jq\n'
241
+ return 0
242
+ fi
243
+ printf 'ok\n'
244
+ }
245
+
246
+ run_codex_quota_preflight() {
247
+ if [[ "${CODING_WORKER}" != "codex" ]]; then
248
+ printf 'CODING_WORKER=%s\n' "${CODING_WORKER}"
249
+ printf 'CODEX_QUOTA_PREFLIGHT_SKIPPED_FOR_WORKER=yes\n'
250
+ return 0
251
+ fi
252
+
253
+ local unavailable_reason=""
254
+ unavailable_reason="$(codex_quota_health_reason)"
255
+ if [[ "${unavailable_reason}" != "ok" ]]; then
256
+ printf 'CODEX_QUOTA_MANAGER_UNAVAILABLE=yes\n'
257
+ printf 'CODEX_QUOTA_MANAGER_REASON=%s\n' "${unavailable_reason}"
258
+ printf '[%s] codex quota preflight skipped unavailable (%s)\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" "${unavailable_reason}"
259
+ fi
260
+
261
+ if [[ "${CODEX_QUOTA_AUTOSWITCH_ENABLED}" == "0" ]]; then
262
+ printf 'CODEX_QUOTA_AUTOSWITCH_ENABLED=0\n'
263
+ return 0
264
+ fi
265
+
266
+ printf 'CODEX_QUOTA_ROTATION_STRATEGY=%s\n' "${CODEX_QUOTA_ROTATION_STRATEGY}"
267
+ printf 'CODEX_QUOTA_PREFLIGHT_SKIPPED=strategy-%s\n' "${CODEX_QUOTA_ROTATION_STRATEGY}"
268
+ printf '[%s] codex quota preflight skipped strategy=%s\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" "${CODEX_QUOTA_ROTATION_STRATEGY}"
269
+ return 0
270
+ }
271
+
272
+ derive_dynamic_limits() {
273
+ EFFECTIVE_MAX_CONCURRENT_WORKERS="${MAX_CONCURRENT_WORKERS}"
274
+ EFFECTIVE_MAX_CONCURRENT_PR_WORKERS="${MAX_CONCURRENT_PR_WORKERS}"
275
+ EFFECTIVE_MAX_RECURRING_ISSUE_WORKERS="${MAX_RECURRING_ISSUE_WORKERS}"
276
+ EFFECTIVE_MAX_LAUNCHES_PER_HEARTBEAT="${MAX_LAUNCHES_PER_HEARTBEAT}"
277
+ HEALTHY_QUOTA_POOLS=""
278
+ ROTATION_QUOTA_POOLS=""
279
+ EFFECTIVE_QUOTA_POOLS=""
280
+
281
+ if [[ "${CODING_WORKER}" != "codex" ]]; then
282
+ printf 'CODING_WORKER=%s\n' "${CODING_WORKER}"
283
+ printf 'DYNAMIC_CONCURRENCY_QUOTA_MODE=non-codex-bypass\n'
284
+ printf 'EFFECTIVE_MAX_CONCURRENT_WORKERS=%s\n' "${EFFECTIVE_MAX_CONCURRENT_WORKERS}"
285
+ printf 'EFFECTIVE_MAX_CONCURRENT_PR_WORKERS=%s\n' "${EFFECTIVE_MAX_CONCURRENT_PR_WORKERS}"
286
+ printf 'EFFECTIVE_MAX_RECURRING_ISSUE_WORKERS=%s\n' "${EFFECTIVE_MAX_RECURRING_ISSUE_WORKERS}"
287
+ printf 'EFFECTIVE_MAX_LAUNCHES_PER_HEARTBEAT=%s\n' "${EFFECTIVE_MAX_LAUNCHES_PER_HEARTBEAT}"
288
+ return 0
289
+ fi
290
+
291
+ if [[ "${DYNAMIC_CONCURRENCY_ENABLED}" == "0" ]]; then
292
+ printf 'DYNAMIC_CONCURRENCY_ENABLED=0\n'
293
+ printf 'EFFECTIVE_MAX_CONCURRENT_WORKERS=%s\n' "${EFFECTIVE_MAX_CONCURRENT_WORKERS}"
294
+ printf 'EFFECTIVE_MAX_CONCURRENT_PR_WORKERS=%s\n' "${EFFECTIVE_MAX_CONCURRENT_PR_WORKERS}"
295
+ printf 'EFFECTIVE_MAX_RECURRING_ISSUE_WORKERS=%s\n' "${EFFECTIVE_MAX_RECURRING_ISSUE_WORKERS}"
296
+ printf 'EFFECTIVE_MAX_LAUNCHES_PER_HEARTBEAT=%s\n' "${EFFECTIVE_MAX_LAUNCHES_PER_HEARTBEAT}"
297
+ return 0
298
+ fi
299
+
300
+ printf 'CODEX_QUOTA_ROTATION_STRATEGY=%s\n' "${CODEX_QUOTA_ROTATION_STRATEGY}"
301
+
302
+ if [[ ! -f "${CODEX_QUOTA_FULL_CACHE_FILE}" || ! -s "${CODEX_QUOTA_FULL_CACHE_FILE}" ]] || ! command -v jq >/dev/null 2>&1; then
303
+ printf 'DYNAMIC_CONCURRENCY_QUOTA_MODE=failure-driven-static\n'
304
+ printf 'EFFECTIVE_MAX_CONCURRENT_WORKERS=%s\n' "${EFFECTIVE_MAX_CONCURRENT_WORKERS}"
305
+ printf 'EFFECTIVE_MAX_CONCURRENT_PR_WORKERS=%s\n' "${EFFECTIVE_MAX_CONCURRENT_PR_WORKERS}"
306
+ printf 'EFFECTIVE_MAX_RECURRING_ISSUE_WORKERS=%s\n' "${EFFECTIVE_MAX_RECURRING_ISSUE_WORKERS}"
307
+ printf 'EFFECTIVE_MAX_LAUNCHES_PER_HEARTBEAT=%s\n' "${EFFECTIVE_MAX_LAUNCHES_PER_HEARTBEAT}"
308
+ return 0
309
+ fi
310
+
311
+ local healthy_pools=""
312
+ local rotation_pools=""
313
+ local effective_pools=""
314
+ healthy_pools="$(
315
+ jq -r --argjson primaryThresh "${CODEX_QUOTA_THRESHOLD}" --argjson weeklyThresh "${CODEX_QUOTA_WEEKLY_THRESHOLD}" '
316
+ group_by(.accountId)
317
+ | map(select(
318
+ ((.[0].usage.rate_limit.limit_reached // false) | not)
319
+ and ((.[0].usage.rate_limit.primary_window.used_percent // 100) < $primaryThresh)
320
+ and ((.[0].usage.rate_limit.secondary_window.used_percent // 100) < $weeklyThresh)
321
+ ))
322
+ | length
323
+ ' "${CODEX_QUOTA_FULL_CACHE_FILE}" 2>/dev/null || true
324
+ )"
325
+
326
+ rotation_pools="$(
327
+ jq -r --argjson weeklyThresh "${CODEX_QUOTA_WEEKLY_THRESHOLD}" '
328
+ group_by(.accountId)
329
+ | map(select(
330
+ ((.[0].usage.rate_limit.limit_reached // false) | not)
331
+ and ((.[0].usage.rate_limit.secondary_window.used_percent // 100) < $weeklyThresh)
332
+ and ((.[0].planType // "") != "free")
333
+ ))
334
+ | length
335
+ ' "${CODEX_QUOTA_FULL_CACHE_FILE}" 2>/dev/null || true
336
+ )"
337
+
338
+ if ! [[ "${healthy_pools:-}" =~ ^[0-9]+$ ]]; then
339
+ printf 'DYNAMIC_CONCURRENCY_QUOTA_MODE=failure-driven-static\n'
340
+ printf 'EFFECTIVE_MAX_CONCURRENT_WORKERS=%s\n' "${EFFECTIVE_MAX_CONCURRENT_WORKERS}"
341
+ printf 'EFFECTIVE_MAX_CONCURRENT_PR_WORKERS=%s\n' "${EFFECTIVE_MAX_CONCURRENT_PR_WORKERS}"
342
+ printf 'EFFECTIVE_MAX_RECURRING_ISSUE_WORKERS=%s\n' "${EFFECTIVE_MAX_RECURRING_ISSUE_WORKERS}"
343
+ printf 'EFFECTIVE_MAX_LAUNCHES_PER_HEARTBEAT=%s\n' "${EFFECTIVE_MAX_LAUNCHES_PER_HEARTBEAT}"
344
+ return 0
345
+ fi
346
+ if ! [[ "${rotation_pools:-}" =~ ^[0-9]+$ ]]; then
347
+ rotation_pools="${healthy_pools}"
348
+ fi
349
+
350
+ # Healthy pools are immediately usable under the current 5h/weekly thresholds.
351
+ # Rotation pools include accounts that are still viable on weekly budget but may
352
+ # already be warm on the 5h window. Count the extra capacity at half weight so
353
+ # the scheduler burns harder without assuming every warm pool is instantly safe.
354
+ effective_pools="${healthy_pools}"
355
+ if (( rotation_pools > healthy_pools )); then
356
+ effective_pools=$((healthy_pools + ((rotation_pools - healthy_pools) / 2)))
357
+ fi
358
+
359
+ local target_workers target_pr_workers target_recurring_workers
360
+ if (( effective_pools <= 0 )); then
361
+ target_workers=0
362
+ target_pr_workers=0
363
+ target_recurring_workers=0
364
+ elif (( effective_pools >= 12 )); then
365
+ target_workers=18
366
+ target_pr_workers=10
367
+ target_recurring_workers=5
368
+ elif (( effective_pools >= 10 )); then
369
+ target_workers=16
370
+ target_pr_workers=9
371
+ target_recurring_workers=5
372
+ elif (( effective_pools >= 8 )); then
373
+ target_workers=14
374
+ target_pr_workers=8
375
+ target_recurring_workers=4
376
+ elif (( effective_pools >= 6 )); then
377
+ target_workers=12
378
+ target_pr_workers=7
379
+ target_recurring_workers=4
380
+ elif (( effective_pools >= 4 )); then
381
+ target_workers=10
382
+ target_pr_workers=6
383
+ target_recurring_workers=3
384
+ elif (( effective_pools >= 3 )); then
385
+ target_workers=8
386
+ target_pr_workers=4
387
+ target_recurring_workers=3
388
+ else
389
+ target_workers=6
390
+ target_pr_workers=3
391
+ target_recurring_workers=2
392
+ fi
393
+
394
+ if (( target_workers > MAX_CONCURRENT_WORKERS )); then
395
+ target_workers="${MAX_CONCURRENT_WORKERS}"
396
+ fi
397
+ if (( target_pr_workers > MAX_CONCURRENT_PR_WORKERS )); then
398
+ target_pr_workers="${MAX_CONCURRENT_PR_WORKERS}"
399
+ fi
400
+ if (( target_pr_workers > target_workers )); then
401
+ target_pr_workers="${target_workers}"
402
+ fi
403
+ if (( target_recurring_workers > MAX_RECURRING_ISSUE_WORKERS )); then
404
+ target_recurring_workers="${MAX_RECURRING_ISSUE_WORKERS}"
405
+ fi
406
+ if (( target_recurring_workers > target_workers )); then
407
+ target_recurring_workers="${target_workers}"
408
+ fi
409
+
410
+ HEALTHY_QUOTA_POOLS="${healthy_pools}"
411
+ ROTATION_QUOTA_POOLS="${rotation_pools}"
412
+ EFFECTIVE_QUOTA_POOLS="${effective_pools}"
413
+ EFFECTIVE_MAX_CONCURRENT_WORKERS="${target_workers}"
414
+ EFFECTIVE_MAX_CONCURRENT_PR_WORKERS="${target_pr_workers}"
415
+ EFFECTIVE_MAX_RECURRING_ISSUE_WORKERS="${target_recurring_workers}"
416
+ EFFECTIVE_MAX_LAUNCHES_PER_HEARTBEAT="${target_workers}"
417
+
418
+ printf 'DYNAMIC_CONCURRENCY_ENABLED=1\n'
419
+ printf 'DYNAMIC_CONCURRENCY_QUOTA_MODE=failure-driven-dynamic\n'
420
+ printf 'HEALTHY_QUOTA_POOLS=%s\n' "${HEALTHY_QUOTA_POOLS}"
421
+ printf 'ROTATION_QUOTA_POOLS=%s\n' "${ROTATION_QUOTA_POOLS}"
422
+ printf 'EFFECTIVE_QUOTA_POOLS=%s\n' "${EFFECTIVE_QUOTA_POOLS}"
423
+ printf 'EFFECTIVE_MAX_CONCURRENT_WORKERS=%s\n' "${EFFECTIVE_MAX_CONCURRENT_WORKERS}"
424
+ printf 'EFFECTIVE_MAX_CONCURRENT_PR_WORKERS=%s\n' "${EFFECTIVE_MAX_CONCURRENT_PR_WORKERS}"
425
+ printf 'EFFECTIVE_MAX_RECURRING_ISSUE_WORKERS=%s\n' "${EFFECTIVE_MAX_RECURRING_ISSUE_WORKERS}"
426
+ printf 'EFFECTIVE_MAX_LAUNCHES_PER_HEARTBEAT=%s\n' "${EFFECTIVE_MAX_LAUNCHES_PER_HEARTBEAT}"
427
+ }
428
+
429
+ trap cleanup EXIT
430
+
431
+ if [[ "${HEARTBEAT_PREFLIGHT_ONLY}" == "1" ]]; then
432
+ if [[ ! -x "${RECOVERY_PREFLIGHT_SCRIPT}" ]]; then
433
+ echo "missing heartbeat preflight script: ${RECOVERY_PREFLIGHT_SCRIPT}" >&2
434
+ exit 1
435
+ fi
436
+ bash "${RECOVERY_PREFLIGHT_SCRIPT}"
437
+ exit $?
438
+ fi
439
+
440
+ run_codex_quota_preflight
441
+
442
+ acquire_lock
443
+
444
+ reap_orphan_shared_loop_groups
445
+
446
+ if existing_loop_pid="$(active_shared_loop_pid || true)" && [[ -n "${existing_loop_pid}" ]]; then
447
+ printf 'HEARTBEAT_SKIPPED=shared-loop-active\n'
448
+ printf 'LOOP_PID=%s\n' "${existing_loop_pid}"
449
+ exit 0
450
+ fi
451
+
452
+ if [[ "${RETAINED_WORKTREE_AUDIT_ENABLED}" != "0" ]]; then
453
+ printf '[%s] retained-worktree audit start\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
454
+ if retained_audit_output="$(
455
+ run_with_timeout "${RETAINED_WORKTREE_AUDIT_TIMEOUT_SECONDS}" \
456
+ bash "${WORKSPACE_DIR}/bin/audit-retained-worktrees.sh" --cleanup 2>&1
457
+ )"; then
458
+ [[ -n "${retained_audit_output}" ]] && printf '%s\n' "${retained_audit_output}"
459
+ printf '[%s] retained-worktree audit end status=0\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
460
+ else
461
+ retained_audit_status=$?
462
+ [[ -n "${retained_audit_output:-}" ]] && printf '%s\n' "${retained_audit_output}"
463
+ if [[ "${retained_audit_status}" -eq 124 ]]; then
464
+ printf 'RETAINED_WORKTREE_AUDIT_TIMEOUT=yes\n'
465
+ fi
466
+ printf '[%s] retained-worktree audit end status=%s\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" "${retained_audit_status}"
467
+ fi
468
+ fi
469
+
470
+ if [[ "${AGENT_WORKTREE_AUDIT_ENABLED}" != "0" ]]; then
471
+ printf '[%s] agent-worktree audit start\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
472
+ if agent_audit_output="$(
473
+ run_with_timeout "${AGENT_WORKTREE_AUDIT_TIMEOUT_SECONDS}" \
474
+ bash "${WORKSPACE_DIR}/bin/audit-agent-worktrees.sh" --cleanup 2>&1
475
+ )"; then
476
+ [[ -n "${agent_audit_output}" ]] && printf '%s\n' "${agent_audit_output}"
477
+ printf '[%s] agent-worktree audit end status=0\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
478
+ else
479
+ agent_audit_status=$?
480
+ [[ -n "${agent_audit_output:-}" ]] && printf '%s\n' "${agent_audit_output}"
481
+ if [[ "${agent_audit_status}" -eq 124 ]]; then
482
+ printf 'AGENT_WORKTREE_AUDIT_TIMEOUT=yes\n'
483
+ fi
484
+ printf '[%s] agent-worktree audit end status=%s\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" "${agent_audit_status}"
485
+ fi
486
+ fi
487
+
488
+ derive_dynamic_limits
489
+
490
+ printf '[%s] shared heartbeat loop start\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
491
+ if ACP_TIMEOUT_CHILD_PID_FILE="${SHARED_LOOP_PID_FILE}" \
492
+ F_LOSNING_TIMEOUT_CHILD_PID_FILE="${SHARED_LOOP_PID_FILE}" \
493
+ run_with_timeout "${HEARTBEAT_LOOP_TIMEOUT_SECONDS}" \
494
+ env \
495
+ ACP_STATE_ROOT="$STATE_ROOT" \
496
+ ACP_ALLOW_INFRA_CI_BYPASS="$ALLOW_INFRA_CI_BYPASS" \
497
+ ACP_HEALTHY_QUOTA_POOLS="$HEALTHY_QUOTA_POOLS" \
498
+ ACP_EFFECTIVE_QUOTA_POOLS="$EFFECTIVE_QUOTA_POOLS" \
499
+ F_LOSNING_STATE_ROOT="$STATE_ROOT" \
500
+ F_LOSNING_ALLOW_INFRA_CI_BYPASS="$ALLOW_INFRA_CI_BYPASS" \
501
+ F_LOSNING_HEALTHY_QUOTA_POOLS="$HEALTHY_QUOTA_POOLS" \
502
+ F_LOSNING_EFFECTIVE_QUOTA_POOLS="$EFFECTIVE_QUOTA_POOLS" \
503
+ bash "${FLOW_TOOLS_DIR}/agent-project-heartbeat-loop" \
504
+ --repo-slug "$REPO_SLUG" \
505
+ --runs-root "$RUNS_ROOT" \
506
+ --state-root "$STATE_ROOT" \
507
+ --memory-dir "$MEMORY_DIR" \
508
+ --issue-prefix "$ISSUE_SESSION_PREFIX" \
509
+ --pr-prefix "$PR_SESSION_PREFIX" \
510
+ --hook-file "$HOOK_FILE" \
511
+ --max-concurrent-workers "$EFFECTIVE_MAX_CONCURRENT_WORKERS" \
512
+ --max-concurrent-heavy-workers "$MAX_CONCURRENT_E2E_WORKERS" \
513
+ --max-concurrent-pr-workers "$EFFECTIVE_MAX_CONCURRENT_PR_WORKERS" \
514
+ --max-recurring-issue-workers "$EFFECTIVE_MAX_RECURRING_ISSUE_WORKERS" \
515
+ --max-concurrent-scheduled-issue-workers "$MAX_CONCURRENT_SCHEDULED_ISSUE_WORKERS" \
516
+ --max-concurrent-scheduled-heavy-workers "$MAX_CONCURRENT_SCHEDULED_HEAVY_WORKERS" \
517
+ --max-concurrent-blocked-recovery-issue-workers "$MAX_CONCURRENT_BLOCKED_RECOVERY_ISSUE_WORKERS" \
518
+ --blocked-recovery-cooldown-seconds "$BLOCKED_RECOVERY_COOLDOWN_SECONDS" \
519
+ --max-open-agent-prs-for-recurring "$MAX_OPEN_AGENT_PRS_FOR_RECURRING" \
520
+ --max-launches-per-pass "$EFFECTIVE_MAX_LAUNCHES_PER_HEARTBEAT" \
521
+ --heavy-running-label "E2E_ISSUE" \
522
+ --heavy-deferred-key "E2E_DEFERRED" \
523
+ --heavy-deferred-message "E2E-heavy issues remain queued until the single e2e slot is free."; then
524
+ printf '[%s] shared heartbeat loop end status=0\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
525
+ else
526
+ loop_status=$?
527
+ if [[ "${loop_status}" -eq 124 ]]; then
528
+ printf 'HEARTBEAT_LOOP_TIMEOUT=yes\n'
529
+ fi
530
+ printf '[%s] shared heartbeat loop end status=%s\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" "${loop_status}"
531
+ exit "${loop_status}"
532
+ fi
533
+
534
+ printf '[%s] merged-pr catchup start\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
535
+ if run_with_timeout "${CATCHUP_TIMEOUT_SECONDS}" \
536
+ env \
537
+ ACP_RUNS_ROOT="$RUNS_ROOT" \
538
+ F_LOSNING_RUNS_ROOT="$RUNS_ROOT" \
539
+ bash "${FLOW_TOOLS_DIR}/agent-project-catch-up-merged-prs" \
540
+ --repo-slug "$REPO_SLUG" \
541
+ --state-root "$STATE_ROOT" \
542
+ --hook-file "$HOOK_FILE" \
543
+ --limit 100; then
544
+ printf '[%s] merged-pr catchup end status=0\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
545
+ else
546
+ catchup_status=$?
547
+ if [[ "${catchup_status}" -eq 124 ]]; then
548
+ printf 'CATCHUP_TIMEOUT=yes\n'
549
+ fi
550
+ printf '[%s] merged-pr catchup end status=%s\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" "${catchup_status}"
551
+ fi