@indigoai-us/hq-cli 5.314.0 → 5.315.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 (1178) hide show
  1. package/CHANGELOG.md +4311 -1
  2. package/README.md +113 -0
  3. package/assets/bot-workers/setup/context/USER-GUIDE.md +404 -0
  4. package/assets/bot-workers/setup/context/quick-reference.md +199 -0
  5. package/assets/bot-workers/setup/skills/agents-and-team.md +167 -0
  6. package/assets/bot-workers/setup/skills/explain-hq.md +35 -0
  7. package/assets/bot-workers/setup/skills/first-company.md +74 -0
  8. package/assets/bot-workers/setup/skills/joining-a-team.md +125 -0
  9. package/assets/bot-workers/setup/skills/pricing.md +92 -0
  10. package/assets/bot-workers/setup/skills/standing-help.md +81 -0
  11. package/assets/bot-workers/setup/skills/your-business.md +281 -0
  12. package/assets/bot-workers/setup/worker.yaml +791 -0
  13. package/assets/lanes-hooks/hq-lanes-drain.json +15 -0
  14. package/assets/lanes-hooks/hq-lanes-drain.mjs +275 -0
  15. package/assets/lanes-hooks/hq-lanes-drain.sh +21 -0
  16. package/assets/lanes-reconcile/hq-lanes-reconcile.service +11 -0
  17. package/assets/lanes-reconcile/hq-lanes-reconcile.timer +12 -0
  18. package/assets/scaffold/.claude/hooks/validate-policy-frontmatter.sh +485 -0
  19. package/assets/scaffold/core/scripts/audit-log.sh +384 -0
  20. package/assets/scaffold/core/scripts/checkpoint-stop-gate.sh +281 -43
  21. package/assets/scaffold/core/scripts/clean-worktree-reconcile-handoff.sh +171 -0
  22. package/assets/scaffold/core/scripts/eval-trigger.sh +193 -0
  23. package/assets/scaffold/core/scripts/git-pack-extension-check.sh +164 -0
  24. package/assets/scaffold/core/scripts/handoff-finalize.sh +688 -0
  25. package/assets/scaffold/core/scripts/handoff-open-steps.sh +81 -0
  26. package/assets/scaffold/core/scripts/handoff-post.sh +252 -0
  27. package/assets/scaffold/core/scripts/hook-lib.sh +345 -50
  28. package/assets/scaffold/core/scripts/hq-delegate-bundle.sh +307 -0
  29. package/assets/scaffold/core/scripts/hq-delegate-grant.sh +252 -0
  30. package/assets/scaffold/core/scripts/hq-delegate-pickup.sh +99 -0
  31. package/assets/scaffold/core/scripts/hq-delegate-publish.sh +91 -0
  32. package/assets/scaffold/core/scripts/hq-delegate-repo.sh +158 -0
  33. package/assets/scaffold/core/scripts/hq-delegate-resolve.sh +145 -0
  34. package/assets/scaffold/core/scripts/hq-delegate-secrets.sh +129 -0
  35. package/assets/scaffold/core/scripts/hq-delegate-send.sh +259 -0
  36. package/assets/scaffold/core/scripts/hq-delegate-transfer.sh +175 -0
  37. package/assets/scaffold/core/scripts/hq-delegate-verify.sh +238 -0
  38. package/assets/scaffold/core/scripts/hq-dm-bind.sh +585 -0
  39. package/assets/scaffold/core/scripts/hq-job-notify.sh +389 -0
  40. package/assets/scaffold/core/scripts/hq-job-probe.sh +835 -0
  41. package/assets/scaffold/core/scripts/hq-job-remediate.sh +359 -0
  42. package/assets/scaffold/core/scripts/hq-job-run.sh +1025 -0
  43. package/assets/scaffold/core/scripts/hq-pin.sh +118 -0
  44. package/assets/scaffold/core/scripts/hq-session.sh +184 -23
  45. package/assets/scaffold/core/scripts/jobs-schedule-parse.sh +310 -0
  46. package/assets/scaffold/core/scripts/jobs-validate.sh +451 -0
  47. package/assets/scaffold/core/scripts/knowledge-prefs.sh +55 -0
  48. package/assets/scaffold/core/scripts/lib/portable.sh +211 -0
  49. package/assets/scaffold/core/scripts/lib/secret-patterns.sh +58 -0
  50. package/assets/scaffold/core/scripts/lib/session-id.sh +3 -2
  51. package/assets/scaffold/core/scripts/lib/session-scope-capability.sh +1 -1
  52. package/assets/scaffold/core/scripts/lib/work-mesh-enqueue.sh +2 -1
  53. package/assets/scaffold/core/scripts/lib/work-mesh-live-hook.sh +378 -0
  54. package/assets/scaffold/core/scripts/lib/work-mesh-live-rebind.sh +141 -0
  55. package/assets/scaffold/core/scripts/migrate-policy-triggers.sh +378 -0
  56. package/assets/scaffold/core/scripts/ontology-audience-grant.sh +81 -0
  57. package/assets/scaffold/core/scripts/ontology-brief.mjs +93 -0
  58. package/assets/scaffold/core/scripts/ontology-candidate.sh +100 -0
  59. package/assets/scaffold/core/scripts/ontology-garden.mjs +238 -0
  60. package/assets/scaffold/core/scripts/ontology-source-worklist.sh +79 -0
  61. package/assets/scaffold/core/scripts/outpost-jobs-reconcile.sh +834 -0
  62. package/assets/scaffold/core/scripts/policy-age-report.sh +156 -0
  63. package/assets/scaffold/core/scripts/policy-retire.sh +199 -0
  64. package/assets/scaffold/core/scripts/qmd-reindex-after-sync.sh +10 -87
  65. package/assets/scaffold/core/scripts/qmd-reindex-bg.sh +40 -0
  66. package/assets/scaffold/core/scripts/read-policy-frontmatter.sh +32 -0
  67. package/assets/scaffold/core/scripts/rebuild-company-knowledge-index.sh +36 -3
  68. package/assets/scaffold/core/scripts/rebuild-projects-index.sh +99 -12
  69. package/assets/scaffold/core/scripts/refresh-vault-access.sh +188 -0
  70. package/assets/scaffold/core/scripts/register-project.sh +262 -0
  71. package/assets/scaffold/core/scripts/resolve-company.sh +199 -0
  72. package/assets/scaffold/core/scripts/resume-thread-lock.sh +285 -0
  73. package/assets/scaffold/core/scripts/source-yaml-validate.sh +36 -0
  74. package/assets/scaffold/core/scripts/token-usage-report.sh +81 -20
  75. package/assets/scaffold/core/scripts/verify-story-deliverables.sh +148 -0
  76. package/assets/scaffold/core/scripts/work-mesh-live-bind-trusted.sh +152 -0
  77. package/assets/scaffold/core/scripts/work-mesh-live-rebind.sh +59 -0
  78. package/assets/scaffold/core/scripts/worktree-gc.sh +362 -0
  79. package/contracts/preflight/v1/fixtures.json +87 -0
  80. package/contracts/preflight/v1/schema.json +58 -0
  81. package/dist/bin/hq-auth-refresh.d.ts +2 -0
  82. package/dist/bin/hq-auth-refresh.js +12 -6
  83. package/dist/command-catalog.generated.d.ts +10753 -0
  84. package/dist/command-catalog.generated.js +13859 -0
  85. package/dist/command-registration-plan.d.ts +474 -0
  86. package/dist/command-registration-plan.js +136 -0
  87. package/dist/commands/__fixtures__/access-vault.d.ts +2 -0
  88. package/dist/commands/__fixtures__/access-vault.js +1 -1
  89. package/dist/commands/_patterns.d.ts +16 -0
  90. package/dist/commands/_patterns.js +20 -3
  91. package/dist/commands/access.d.ts +2 -1
  92. package/dist/commands/access.js +58 -49
  93. package/dist/commands/agent-enroll.d.ts +146 -0
  94. package/dist/commands/agent-enroll.js +401 -0
  95. package/dist/commands/agent-inbox.d.ts +9 -0
  96. package/dist/commands/agent-inbox.js +102 -0
  97. package/dist/commands/agent-kit.d.ts +87 -0
  98. package/dist/commands/agent-kit.js +480 -0
  99. package/dist/commands/agent-mcp.d.ts +24 -0
  100. package/dist/commands/agent-mcp.js +127 -0
  101. package/dist/commands/agent-probe.d.ts +77 -0
  102. package/dist/commands/agent-probe.js +335 -0
  103. package/dist/commands/agent.d.ts +13 -0
  104. package/dist/commands/agent.js +40 -0
  105. package/dist/commands/agents.d.ts +106 -4
  106. package/dist/commands/agents.js +924 -45
  107. package/dist/commands/auth.d.ts +1 -0
  108. package/dist/commands/auth.js +189 -19
  109. package/dist/commands/billing-sponsor.d.ts +83 -0
  110. package/dist/commands/billing-sponsor.js +344 -0
  111. package/dist/commands/billing.d.ts +3 -0
  112. package/dist/commands/billing.js +6 -1
  113. package/dist/commands/bot-companies.d.ts +40 -0
  114. package/dist/commands/bot-companies.js +64 -0
  115. package/dist/commands/bot-continuity.d.ts +28 -0
  116. package/dist/commands/bot-continuity.js +68 -0
  117. package/dist/commands/bot-jobs.d.ts +127 -0
  118. package/dist/commands/bot-jobs.js +454 -0
  119. package/dist/commands/bot.d.ts +278 -0
  120. package/dist/commands/bot.js +1982 -0
  121. package/dist/commands/channels.js +56 -1
  122. package/dist/commands/cloud-demote.js +3 -2
  123. package/dist/commands/cloud-provision.d.ts +14 -0
  124. package/dist/commands/cloud-provision.js +232 -58
  125. package/dist/commands/cloud-retire.d.ts +51 -0
  126. package/dist/commands/cloud-retire.js +154 -0
  127. package/dist/commands/cloud.d.ts +49 -15
  128. package/dist/commands/cloud.js +357 -122
  129. package/dist/commands/company-rename-flag.d.ts +4 -0
  130. package/dist/commands/company-rename-flag.js +7 -0
  131. package/dist/commands/company-rename.d.ts +4 -0
  132. package/dist/commands/company-rename.js +90 -0
  133. package/dist/commands/company-transfer.d.ts +4 -28
  134. package/dist/commands/company-transfer.js +5 -49
  135. package/dist/commands/company.js +68 -3
  136. package/dist/commands/core-audit-log-native.d.ts +9 -0
  137. package/dist/commands/core-audit-log-native.js +257 -0
  138. package/dist/commands/core-checkpoint-gate-setting.d.ts +59 -0
  139. package/dist/commands/core-checkpoint-gate-setting.js +163 -0
  140. package/dist/commands/core-checkpoint-stop-gate-native.d.ts +34 -0
  141. package/dist/commands/core-checkpoint-stop-gate-native.js +725 -0
  142. package/dist/commands/core-checkpoint.d.ts +19 -9
  143. package/dist/commands/core-checkpoint.js +301 -37
  144. package/dist/commands/core-command-catalog.d.ts +34 -0
  145. package/dist/commands/core-command-catalog.js +62 -0
  146. package/dist/commands/core-company-index-scope-flag.d.ts +3 -0
  147. package/dist/commands/core-company-index-scope-flag.js +6 -0
  148. package/dist/commands/core-delegate-w1b.d.ts +10 -0
  149. package/dist/commands/core-delegate-w1b.js +1219 -0
  150. package/dist/commands/core-delegate.d.ts +46 -0
  151. package/dist/commands/core-delegate.js +1040 -0
  152. package/dist/commands/core-dm-bind-native.d.ts +32 -0
  153. package/dist/commands/core-dm-bind-native.js +668 -0
  154. package/dist/commands/core-eval-trigger-native.d.ts +4 -0
  155. package/dist/commands/core-eval-trigger-native.js +12 -0
  156. package/dist/commands/core-git-pack-extension-check-native.d.ts +22 -0
  157. package/dist/commands/core-git-pack-extension-check-native.js +190 -0
  158. package/dist/commands/core-handoff-fast.d.ts +18 -0
  159. package/dist/commands/core-handoff-fast.js +104 -0
  160. package/dist/commands/core-handoff-open-steps.d.ts +21 -0
  161. package/dist/commands/core-handoff-open-steps.js +264 -0
  162. package/dist/commands/core-handoff.d.ts +24 -0
  163. package/dist/commands/core-handoff.js +1144 -0
  164. package/dist/commands/core-hq-session-native.d.ts +39 -0
  165. package/dist/commands/core-hq-session-native.js +579 -0
  166. package/dist/commands/core-job-native-utils.d.ts +6 -0
  167. package/dist/commands/core-job-native-utils.js +80 -0
  168. package/dist/commands/core-job-native.d.ts +5 -0
  169. package/dist/commands/core-job-native.js +16 -0
  170. package/dist/commands/core-job-notify-native.d.ts +36 -0
  171. package/dist/commands/core-job-notify-native.js +359 -0
  172. package/dist/commands/core-job-probe-native.d.ts +45 -0
  173. package/dist/commands/core-job-probe-native.js +564 -0
  174. package/dist/commands/core-job-remediate-native.d.ts +26 -0
  175. package/dist/commands/core-job-remediate-native.js +374 -0
  176. package/dist/commands/core-job-remote-launcher.d.ts +7 -0
  177. package/dist/commands/core-job-remote-launcher.js +51 -0
  178. package/dist/commands/core-job-run-native.d.ts +47 -0
  179. package/dist/commands/core-job-run-native.js +1215 -0
  180. package/dist/commands/core-jobs-native.d.ts +25 -0
  181. package/dist/commands/core-jobs-native.js +1339 -0
  182. package/dist/commands/core-knowledge-prefs.d.ts +11 -0
  183. package/dist/commands/core-knowledge-prefs.js +137 -0
  184. package/dist/commands/core-migrate-policy-triggers-native.d.ts +21 -0
  185. package/dist/commands/core-migrate-policy-triggers-native.js +374 -0
  186. package/dist/commands/core-native-handoff-flag.d.ts +4 -0
  187. package/dist/commands/core-native-handoff-flag.js +7 -0
  188. package/dist/commands/core-native-hook-helper-commands.d.ts +4 -0
  189. package/dist/commands/core-native-hook-helper-commands.js +56 -0
  190. package/dist/commands/core-native-hook-helpers-flag.d.ts +4 -0
  191. package/dist/commands/core-native-hook-helpers-flag.js +7 -0
  192. package/dist/commands/core-native-jobs-flag.d.ts +4 -0
  193. package/dist/commands/core-native-jobs-flag.js +7 -0
  194. package/dist/commands/core-native-policy-flag.d.ts +9 -0
  195. package/dist/commands/core-native-policy-flag.js +12 -0
  196. package/dist/commands/core-native-session-flag.d.ts +4 -0
  197. package/dist/commands/core-native-session-flag.js +7 -0
  198. package/dist/commands/core-native-utilities-flag.d.ts +4 -0
  199. package/dist/commands/core-native-utilities-flag.js +7 -0
  200. package/dist/commands/core-native-utilities.d.ts +29 -0
  201. package/dist/commands/core-native-utilities.js +54 -0
  202. package/dist/commands/core-native-worker-skills-flag.d.ts +4 -0
  203. package/dist/commands/core-native-worker-skills-flag.js +7 -0
  204. package/dist/commands/core-ontology-native.d.ts +17 -0
  205. package/dist/commands/core-ontology-native.js +916 -0
  206. package/dist/commands/core-ontology.d.ts +66 -0
  207. package/dist/commands/core-ontology.js +430 -0
  208. package/dist/commands/core-pin-native.d.ts +14 -0
  209. package/dist/commands/core-pin-native.js +236 -0
  210. package/dist/commands/core-policy-frontmatter.d.ts +8 -0
  211. package/dist/commands/core-policy-frontmatter.js +52 -0
  212. package/dist/commands/core-policy.d.ts +14 -0
  213. package/dist/commands/core-policy.js +1018 -0
  214. package/dist/commands/core-refresh-vault-access-native.d.ts +20 -0
  215. package/dist/commands/core-refresh-vault-access-native.js +229 -0
  216. package/dist/commands/core-register-project-native.d.ts +30 -0
  217. package/dist/commands/core-register-project-native.js +347 -0
  218. package/dist/commands/core-resolve-company-command.d.ts +4 -0
  219. package/dist/commands/core-resolve-company-command.js +42 -0
  220. package/dist/commands/core-resolve-company-native.d.ts +28 -0
  221. package/dist/commands/core-resolve-company-native.js +408 -0
  222. package/dist/commands/core-work-mesh-bind-trusted-native.d.ts +32 -0
  223. package/dist/commands/core-work-mesh-bind-trusted-native.js +190 -0
  224. package/dist/commands/core-work-mesh-live-rebind-native.d.ts +18 -0
  225. package/dist/commands/core-work-mesh-live-rebind-native.js +262 -0
  226. package/dist/commands/core-worker-lint-native.d.ts +4 -0
  227. package/dist/commands/core-worker-lint-native.js +160 -0
  228. package/dist/commands/core-worker-share-native.d.ts +4 -0
  229. package/dist/commands/core-worker-share-native.js +260 -0
  230. package/dist/commands/core-worker-skills-command.d.ts +8 -0
  231. package/dist/commands/core-worker-skills-command.js +33 -0
  232. package/dist/commands/core-worker-skills-runtime.d.ts +33 -0
  233. package/dist/commands/core-worker-skills-runtime.js +86 -0
  234. package/dist/commands/core.d.ts +4 -0
  235. package/dist/commands/core.js +277 -80
  236. package/dist/commands/crm.js +52 -3
  237. package/dist/commands/daemon.d.ts +63 -0
  238. package/dist/commands/daemon.js +877 -0
  239. package/dist/commands/db-provision.js +4 -4
  240. package/dist/commands/db-sql.js +7 -21
  241. package/dist/commands/deploy-comments.d.ts +3 -0
  242. package/dist/commands/deploy-comments.js +162 -0
  243. package/dist/commands/dm.d.ts +119 -12
  244. package/dist/commands/dm.js +388 -38
  245. package/dist/commands/doctor.d.ts +12 -0
  246. package/dist/commands/doctor.js +158 -12
  247. package/dist/commands/feedback.d.ts +6 -0
  248. package/dist/commands/feedback.js +86 -37
  249. package/dist/commands/files-browse.d.ts +32 -12
  250. package/dist/commands/files-browse.js +176 -60
  251. package/dist/commands/files.d.ts +17 -0
  252. package/dist/commands/files.js +300 -47
  253. package/dist/commands/group-grants.js +3 -2
  254. package/dist/commands/groups.js +6 -5
  255. package/dist/commands/index-cmd.d.ts +19 -1
  256. package/dist/commands/index-cmd.js +74 -7
  257. package/dist/commands/integrations-api.d.ts +15 -10
  258. package/dist/commands/integrations-api.js +27 -22
  259. package/dist/commands/integrations-connect.d.ts +51 -13
  260. package/dist/commands/integrations-connect.js +86 -71
  261. package/dist/commands/integrations-core.d.ts +6 -6
  262. package/dist/commands/integrations-core.js +25 -11
  263. package/dist/commands/integrations-import.d.ts +1 -1
  264. package/dist/commands/integrations-import.js +43 -24
  265. package/dist/commands/integrations-manage.js +20 -13
  266. package/dist/commands/integrations.d.ts +8 -0
  267. package/dist/commands/integrations.js +94 -7
  268. package/dist/commands/lanes-accounts.d.ts +28 -0
  269. package/dist/commands/lanes-accounts.js +595 -0
  270. package/dist/commands/lanes-autopilot.d.ts +11 -0
  271. package/dist/commands/lanes-autopilot.js +336 -0
  272. package/dist/commands/lanes-checks.d.ts +51 -0
  273. package/dist/commands/lanes-checks.js +941 -0
  274. package/dist/commands/lanes-hold.d.ts +43 -0
  275. package/dist/commands/lanes-hold.js +655 -0
  276. package/dist/commands/lanes-keep-alive-audit.d.ts +10 -0
  277. package/dist/commands/lanes-keep-alive-audit.js +19 -0
  278. package/dist/commands/lanes-kpis.d.ts +11 -0
  279. package/dist/commands/lanes-kpis.js +34 -0
  280. package/dist/commands/lanes-message.d.ts +9 -0
  281. package/dist/commands/lanes-message.js +946 -0
  282. package/dist/commands/lanes-move.d.ts +12 -0
  283. package/dist/commands/lanes-move.js +397 -0
  284. package/dist/commands/lanes-questions.d.ts +36 -0
  285. package/dist/commands/lanes-questions.js +1005 -0
  286. package/dist/commands/lanes-report.d.ts +11 -0
  287. package/dist/commands/lanes-report.js +333 -0
  288. package/dist/commands/lanes-resume.d.ts +6 -0
  289. package/dist/commands/lanes-resume.js +564 -0
  290. package/dist/commands/lanes-review.d.ts +10 -0
  291. package/dist/commands/lanes-review.js +56 -0
  292. package/dist/commands/lanes-shared.d.ts +449 -0
  293. package/dist/commands/lanes-shared.js +170 -0
  294. package/dist/commands/lanes-stories.d.ts +15 -0
  295. package/dist/commands/lanes-stories.js +347 -0
  296. package/dist/commands/lanes-watch.d.ts +7 -0
  297. package/dist/commands/lanes-watch.js +216 -0
  298. package/dist/commands/lanes.d.ts +149 -0
  299. package/dist/commands/lanes.js +8983 -0
  300. package/dist/commands/login.js +49 -6
  301. package/dist/commands/mcp-registration.d.ts +32 -25
  302. package/dist/commands/mcp-registration.js +256 -23
  303. package/dist/commands/mcp-status.js +1 -0
  304. package/dist/commands/meetings.js +55 -34
  305. package/dist/commands/members.js +5 -1
  306. package/dist/commands/mesh.d.ts +11 -0
  307. package/dist/commands/mesh.js +1086 -76
  308. package/dist/commands/monitor.d.ts +58 -0
  309. package/dist/commands/monitor.js +980 -0
  310. package/dist/commands/onboard-bot-membership.d.ts +32 -0
  311. package/dist/commands/onboard-bot-membership.js +40 -0
  312. package/dist/commands/onboard-create-identity.d.ts +26 -0
  313. package/dist/commands/onboard-create-identity.js +23 -0
  314. package/dist/commands/onboard-identity-guard.d.ts +1 -1
  315. package/dist/commands/onboard.js +24 -5
  316. package/dist/commands/outpost-pruner.d.ts +3 -0
  317. package/dist/commands/outpost-pruner.js +125 -0
  318. package/dist/commands/pack-install.js +3 -1
  319. package/dist/commands/packs.js +11 -1
  320. package/dist/commands/people.d.ts +4 -0
  321. package/dist/commands/people.js +37 -3
  322. package/dist/commands/personal-connector-settings-flag.d.ts +4 -0
  323. package/dist/commands/personal-connector-settings-flag.js +7 -0
  324. package/dist/commands/personal-targeted-push-flag.d.ts +4 -0
  325. package/dist/commands/personal-targeted-push-flag.js +7 -0
  326. package/dist/commands/personal-targeted-push-paths.d.ts +3 -0
  327. package/dist/commands/personal-targeted-push-paths.js +67 -0
  328. package/dist/commands/publish.d.ts +8 -1
  329. package/dist/commands/publish.js +17 -4
  330. package/dist/commands/reindex.js +56 -6
  331. package/dist/commands/report.d.ts +19 -0
  332. package/dist/commands/report.js +537 -0
  333. package/dist/commands/rescue.d.ts +4 -0
  334. package/dist/commands/rescue.js +27 -17
  335. package/dist/commands/run-feature-telemetry.d.ts +3 -0
  336. package/dist/commands/run-feature-telemetry.js +6 -0
  337. package/dist/commands/run.js +18 -4
  338. package/dist/commands/safe-extract.d.ts +11 -2
  339. package/dist/commands/safe-extract.js +36 -8
  340. package/dist/commands/secrets.d.ts +20 -2
  341. package/dist/commands/secrets.js +175 -71
  342. package/dist/commands/skill.d.ts +35 -4
  343. package/dist/commands/skill.js +357 -18
  344. package/dist/commands/staff/reset-test-account.d.ts +110 -0
  345. package/dist/commands/staff/reset-test-account.js +268 -0
  346. package/dist/commands/staff/test-account.d.ts +51 -0
  347. package/dist/commands/staff/test-account.js +196 -0
  348. package/dist/commands/sync-mode.d.ts +11 -0
  349. package/dist/commands/sync-mode.js +30 -0
  350. package/dist/commands/sync-narrow.js +36 -5
  351. package/dist/commands/team-sync.js +71 -10
  352. package/dist/commands/update.js +6 -0
  353. package/dist/commands/vault-retention-flag.d.ts +3 -0
  354. package/dist/commands/vault-retention-flag.js +9 -0
  355. package/dist/commands/vault-retention.d.ts +11 -0
  356. package/dist/commands/vault-retention.js +69 -0
  357. package/dist/commands/version.d.ts +10 -0
  358. package/dist/commands/version.js +11 -0
  359. package/dist/commands/whoami.d.ts +22 -0
  360. package/dist/commands/whoami.js +137 -4
  361. package/dist/commands/workers.d.ts +2 -14
  362. package/dist/commands/workers.js +7 -10
  363. package/dist/fast-core.d.ts +6 -7
  364. package/dist/fast-core.js +87 -24
  365. package/dist/fast-scaffold.d.ts +86 -0
  366. package/dist/fast-scaffold.js +364 -0
  367. package/dist/index.d.ts +3 -4
  368. package/dist/index.js +59 -9
  369. package/dist/lanes-drain-hook.d.ts +14 -0
  370. package/dist/lanes-drain-hook.js +203 -0
  371. package/dist/lazy-commands.d.ts +20 -44
  372. package/dist/lazy-commands.js +60 -58
  373. package/dist/lib/agent-kit/adopt-identity.d.ts +27 -0
  374. package/dist/lib/agent-kit/adopt-identity.js +60 -0
  375. package/dist/lib/agent-kit/attachment-download.d.ts +24 -0
  376. package/dist/lib/agent-kit/attachment-download.js +47 -0
  377. package/dist/lib/agent-kit/creds.d.ts +106 -0
  378. package/dist/lib/agent-kit/creds.js +163 -0
  379. package/dist/lib/agent-kit/fallback.d.ts +78 -0
  380. package/dist/lib/agent-kit/fallback.js +210 -0
  381. package/dist/lib/agent-kit/inbox-state.d.ts +83 -0
  382. package/dist/lib/agent-kit/inbox-state.js +159 -0
  383. package/dist/lib/agent-kit/kit-config.d.ts +56 -0
  384. package/dist/lib/agent-kit/kit-config.js +65 -0
  385. package/dist/lib/agent-kit/log.d.ts +16 -0
  386. package/dist/lib/agent-kit/log.js +46 -0
  387. package/dist/lib/agent-kit/mcp/attachments.d.ts +126 -0
  388. package/dist/lib/agent-kit/mcp/attachments.js +222 -0
  389. package/dist/lib/agent-kit/mcp/jsonrpc.d.ts +75 -0
  390. package/dist/lib/agent-kit/mcp/jsonrpc.js +164 -0
  391. package/dist/lib/agent-kit/mcp/tools.d.ts +64 -0
  392. package/dist/lib/agent-kit/mcp/tools.js +397 -0
  393. package/dist/lib/agent-kit/paths.d.ts +65 -0
  394. package/dist/lib/agent-kit/paths.js +118 -0
  395. package/dist/lib/agent-kit/private-file.d.ts +29 -0
  396. package/dist/lib/agent-kit/private-file.js +91 -0
  397. package/dist/lib/agent-kit/run/heartbeat.d.ts +72 -0
  398. package/dist/lib/agent-kit/run/heartbeat.js +128 -0
  399. package/dist/lib/agent-kit/run/inbox.d.ts +76 -0
  400. package/dist/lib/agent-kit/run/inbox.js +205 -0
  401. package/dist/lib/agent-kit/run/supervisor.d.ts +35 -0
  402. package/dist/lib/agent-kit/run/supervisor.js +85 -0
  403. package/dist/lib/agent-kit/run/sync.d.ts +33 -0
  404. package/dist/lib/agent-kit/run/sync.js +58 -0
  405. package/dist/lib/agent-kit/services.d.ts +31 -0
  406. package/dist/lib/agent-kit/services.js +55 -0
  407. package/dist/lib/agent-kit/skills.d.ts +18 -0
  408. package/dist/lib/agent-kit/skills.js +151 -0
  409. package/dist/lib/agent-kit/wake.d.ts +83 -0
  410. package/dist/lib/agent-kit/wake.js +243 -0
  411. package/dist/lib/agents/model-catalog.d.ts +62 -0
  412. package/dist/lib/agents/model-catalog.js +275 -0
  413. package/dist/lib/auth-login-provider-state.d.ts +8 -0
  414. package/dist/lib/auth-login-provider-state.js +63 -0
  415. package/dist/lib/aws-sdk-node-warning-output.d.ts +12 -0
  416. package/dist/lib/aws-sdk-node-warning-output.js +22 -0
  417. package/dist/lib/billing/free-month.d.ts +18 -0
  418. package/dist/lib/billing/free-month.js +31 -0
  419. package/dist/lib/billing/plan-limit-refusals.d.ts +69 -0
  420. package/dist/lib/billing/plan-limit-refusals.js +187 -0
  421. package/dist/lib/billing/plan-limit-upgrade-url.d.ts +11 -0
  422. package/dist/lib/billing/plan-limit-upgrade-url.js +66 -0
  423. package/dist/lib/billing/plan-lock.d.ts +143 -0
  424. package/dist/lib/billing/plan-lock.js +439 -0
  425. package/dist/lib/bot/api.d.ts +349 -0
  426. package/dist/lib/bot/api.js +316 -0
  427. package/dist/lib/bot/attachments.d.ts +45 -0
  428. package/dist/lib/bot/attachments.js +89 -0
  429. package/dist/lib/bot/cli-update.d.ts +138 -0
  430. package/dist/lib/bot/cli-update.js +278 -0
  431. package/dist/lib/bot/company-bind.d.ts +27 -0
  432. package/dist/lib/bot/company-bind.js +62 -0
  433. package/dist/lib/bot/config.d.ts +153 -0
  434. package/dist/lib/bot/config.js +233 -0
  435. package/dist/lib/bot/continuity-download.d.ts +28 -0
  436. package/dist/lib/bot/continuity-download.js +75 -0
  437. package/dist/lib/bot/continuity-install.d.ts +14 -0
  438. package/dist/lib/bot/continuity-install.js +101 -0
  439. package/dist/lib/bot/continuity.d.ts +67 -0
  440. package/dist/lib/bot/continuity.js +301 -0
  441. package/dist/lib/bot/creds.d.ts +24 -0
  442. package/dist/lib/bot/creds.js +51 -0
  443. package/dist/lib/bot/daemon.d.ts +173 -0
  444. package/dist/lib/bot/daemon.js +552 -0
  445. package/dist/lib/bot/inbox-poll-backoff-flag.d.ts +24 -0
  446. package/dist/lib/bot/inbox-poll-backoff-flag.js +135 -0
  447. package/dist/lib/bot/inbox-state.d.ts +18 -0
  448. package/dist/lib/bot/inbox-state.js +51 -0
  449. package/dist/lib/bot/index.d.ts +23 -0
  450. package/dist/lib/bot/index.js +23 -0
  451. package/dist/lib/bot/inflight.d.ts +119 -0
  452. package/dist/lib/bot/inflight.js +83 -0
  453. package/dist/lib/bot/job-completions.d.ts +89 -0
  454. package/dist/lib/bot/job-completions.js +132 -0
  455. package/dist/lib/bot/job-run.d.ts +109 -0
  456. package/dist/lib/bot/job-run.js +246 -0
  457. package/dist/lib/bot/local-config.d.ts +61 -0
  458. package/dist/lib/bot/local-config.js +145 -0
  459. package/dist/lib/bot/local-jobs.d.ts +89 -0
  460. package/dist/lib/bot/local-jobs.js +459 -0
  461. package/dist/lib/bot/local-name.d.ts +54 -0
  462. package/dist/lib/bot/local-name.js +114 -0
  463. package/dist/lib/bot/log.d.ts +13 -0
  464. package/dist/lib/bot/log.js +59 -0
  465. package/dist/lib/bot/owner-context.d.ts +79 -0
  466. package/dist/lib/bot/owner-context.js +175 -0
  467. package/dist/lib/bot/paths.d.ts +76 -0
  468. package/dist/lib/bot/paths.js +131 -0
  469. package/dist/lib/bot/progress.d.ts +132 -0
  470. package/dist/lib/bot/progress.js +289 -0
  471. package/dist/lib/bot/promote.d.ts +16 -0
  472. package/dist/lib/bot/promote.js +106 -0
  473. package/dist/lib/bot/promotion-hold.d.ts +24 -0
  474. package/dist/lib/bot/promotion-hold.js +103 -0
  475. package/dist/lib/bot/promotion-receipt.d.ts +9 -0
  476. package/dist/lib/bot/promotion-receipt.js +56 -0
  477. package/dist/lib/bot/promotion-upload.d.ts +16 -0
  478. package/dist/lib/bot/promotion-upload.js +80 -0
  479. package/dist/lib/bot/prompt.d.ts +136 -0
  480. package/dist/lib/bot/prompt.js +433 -0
  481. package/dist/lib/bot/room-policy.d.ts +63 -0
  482. package/dist/lib/bot/room-policy.js +91 -0
  483. package/dist/lib/bot/run.d.ts +217 -0
  484. package/dist/lib/bot/run.js +1869 -0
  485. package/dist/lib/bot/runnable.d.ts +51 -0
  486. package/dist/lib/bot/runnable.js +65 -0
  487. package/dist/lib/bot/runtime/claude.d.ts +50 -0
  488. package/dist/lib/bot/runtime/claude.js +170 -0
  489. package/dist/lib/bot/runtime/codex.d.ts +28 -0
  490. package/dist/lib/bot/runtime/codex.js +153 -0
  491. package/dist/lib/bot/runtime/grok-acp.d.ts +40 -0
  492. package/dist/lib/bot/runtime/grok-acp.js +265 -0
  493. package/dist/lib/bot/runtime/grok.d.ts +41 -0
  494. package/dist/lib/bot/runtime/grok.js +169 -0
  495. package/dist/lib/bot/runtime/index.d.ts +36 -0
  496. package/dist/lib/bot/runtime/index.js +417 -0
  497. package/dist/lib/bot/runtime/messages-stream.d.ts +56 -0
  498. package/dist/lib/bot/runtime/messages-stream.js +194 -0
  499. package/dist/lib/bot/runtime/sanitize.d.ts +18 -0
  500. package/dist/lib/bot/runtime/sanitize.js +28 -0
  501. package/dist/lib/bot/runtime/types.d.ts +159 -0
  502. package/dist/lib/bot/runtime/types.js +51 -0
  503. package/dist/lib/bot/runtime/windows.d.ts +10 -0
  504. package/dist/lib/bot/runtime/windows.js +53 -0
  505. package/dist/lib/bot/runtime-sign-in.d.ts +24 -0
  506. package/dist/lib/bot/runtime-sign-in.js +31 -0
  507. package/dist/lib/bot/scaffold.d.ts +38 -0
  508. package/dist/lib/bot/scaffold.js +94 -0
  509. package/dist/lib/bot/self-heal.d.ts +52 -0
  510. package/dist/lib/bot/self-heal.js +79 -0
  511. package/dist/lib/bot/self-process.d.ts +19 -0
  512. package/dist/lib/bot/self-process.js +35 -0
  513. package/dist/lib/bot/session.d.ts +19 -0
  514. package/dist/lib/bot/session.js +39 -0
  515. package/dist/lib/bot/setup-explain.d.ts +64 -0
  516. package/dist/lib/bot/setup-explain.js +141 -0
  517. package/dist/lib/bot/split.d.ts +32 -0
  518. package/dist/lib/bot/split.js +241 -0
  519. package/dist/lib/bot/status.d.ts +57 -0
  520. package/dist/lib/bot/status.js +66 -0
  521. package/dist/lib/bot/worker-source.d.ts +66 -0
  522. package/dist/lib/bot/worker-source.js +283 -0
  523. package/dist/lib/checkpoint-ontology/aliases.d.ts +17 -0
  524. package/dist/lib/checkpoint-ontology/aliases.js +44 -0
  525. package/dist/lib/checkpoint-ontology/apply.d.ts +110 -0
  526. package/dist/lib/checkpoint-ontology/apply.js +777 -0
  527. package/dist/lib/checkpoint-ontology/company-brief.d.ts +18 -0
  528. package/dist/lib/checkpoint-ontology/company-brief.js +337 -0
  529. package/dist/lib/checkpoint-ontology/company.d.ts +44 -0
  530. package/dist/lib/checkpoint-ontology/company.js +118 -0
  531. package/dist/lib/checkpoint-ontology/drop-aliases.d.ts +41 -0
  532. package/dist/lib/checkpoint-ontology/drop-aliases.js +76 -0
  533. package/dist/lib/checkpoint-ontology/entity-file.d.ts +109 -0
  534. package/dist/lib/checkpoint-ontology/entity-file.js +371 -0
  535. package/dist/lib/checkpoint-ontology/identity.d.ts +38 -0
  536. package/dist/lib/checkpoint-ontology/identity.js +95 -0
  537. package/dist/lib/checkpoint-ontology/merge-copies.d.ts +199 -0
  538. package/dist/lib/checkpoint-ontology/merge-copies.js +1086 -0
  539. package/dist/lib/checkpoint-ontology/multi-writer.d.ts +101 -0
  540. package/dist/lib/checkpoint-ontology/multi-writer.js +394 -0
  541. package/dist/lib/checkpoint-ontology/plan.d.ts +193 -0
  542. package/dist/lib/checkpoint-ontology/plan.js +504 -0
  543. package/dist/lib/checkpoint-ontology/queued-touches.d.ts +29 -0
  544. package/dist/lib/checkpoint-ontology/queued-touches.js +193 -0
  545. package/dist/lib/checkpoint-ontology/sync-client.d.ts +38 -0
  546. package/dist/lib/checkpoint-ontology/sync-client.js +84 -0
  547. package/dist/lib/checkpoint-ontology/tree.d.ts +55 -0
  548. package/dist/lib/checkpoint-ontology/tree.js +198 -0
  549. package/dist/lib/company-errors-sdk-warning-flag.d.ts +5 -0
  550. package/dist/lib/company-errors-sdk-warning-flag.js +8 -0
  551. package/dist/lib/company-skill-wrapper.d.ts +1 -0
  552. package/dist/lib/company-skill-wrapper.js +121 -29
  553. package/dist/lib/core-utils/codex-skill-bridge.d.ts +2 -1
  554. package/dist/lib/core-utils/hook-timeout-debug-context-flag.d.ts +20 -0
  555. package/dist/lib/core-utils/hook-timeout-debug-context-flag.js +81 -0
  556. package/dist/lib/core-utils/jq-json-serializer.d.ts +5 -0
  557. package/dist/lib/core-utils/jq-json-serializer.js +10 -0
  558. package/dist/lib/core-utils/qmd-reindex-after-sync.d.ts +14 -1
  559. package/dist/lib/core-utils/qmd-reindex-after-sync.js +101 -17
  560. package/dist/lib/core-utils/secret-patterns.d.ts +43 -0
  561. package/dist/lib/core-utils/secret-patterns.js +34 -0
  562. package/dist/lib/core-utils/sentry-report.d.ts +114 -0
  563. package/dist/lib/core-utils/sentry-report.js +1055 -0
  564. package/dist/lib/core-utils/soft-timeout.js +1 -1
  565. package/dist/lib/core-utils/timeout-guard-native.d.ts +7 -0
  566. package/dist/lib/core-utils/timeout-guard-native.js +40 -0
  567. package/dist/lib/core-utils/timeout-guard.d.ts +3 -3
  568. package/dist/lib/core-utils/timeout-guard.js +28 -13
  569. package/dist/lib/core-utils/token-usage-report.js +17 -3
  570. package/dist/lib/core-utils/worktree-resume.d.ts +20 -0
  571. package/dist/lib/core-utils/worktree-resume.js +1198 -0
  572. package/dist/lib/daemon/catalog.d.ts +134 -0
  573. package/dist/lib/daemon/catalog.js +358 -0
  574. package/dist/lib/daemon/config.d.ts +35 -0
  575. package/dist/lib/daemon/config.js +108 -0
  576. package/dist/lib/daemon/flag.d.ts +25 -0
  577. package/dist/lib/daemon/flag.js +59 -0
  578. package/dist/lib/daemon/handoff.d.ts +56 -0
  579. package/dist/lib/daemon/handoff.js +161 -0
  580. package/dist/lib/daemon/hq-cloud.d.ts +79 -0
  581. package/dist/lib/daemon/hq-cloud.js +183 -0
  582. package/dist/lib/daemon/install.d.ts +85 -0
  583. package/dist/lib/daemon/install.js +320 -0
  584. package/dist/lib/daemon/legacy-sync-env.d.ts +10 -0
  585. package/dist/lib/daemon/legacy-sync-env.js +58 -0
  586. package/dist/lib/daemon/legacy.d.ts +45 -0
  587. package/dist/lib/daemon/legacy.js +133 -0
  588. package/dist/lib/daemon/lock.d.ts +47 -0
  589. package/dist/lib/daemon/lock.js +138 -0
  590. package/dist/lib/daemon/machine-type.d.ts +50 -0
  591. package/dist/lib/daemon/machine-type.js +159 -0
  592. package/dist/lib/daemon/migrate.d.ts +77 -0
  593. package/dist/lib/daemon/migrate.js +265 -0
  594. package/dist/lib/daemon/paths.d.ts +24 -0
  595. package/dist/lib/daemon/paths.js +43 -0
  596. package/dist/lib/daemon/protocol-tail.d.ts +33 -0
  597. package/dist/lib/daemon/protocol-tail.js +131 -0
  598. package/dist/lib/daemon/remote-control.d.ts +74 -0
  599. package/dist/lib/daemon/remote-control.js +153 -0
  600. package/dist/lib/daemon/requests.d.ts +28 -0
  601. package/dist/lib/daemon/requests.js +65 -0
  602. package/dist/lib/daemon/run.d.ts +79 -0
  603. package/dist/lib/daemon/run.js +755 -0
  604. package/dist/lib/daemon/succession.d.ts +118 -0
  605. package/dist/lib/daemon/succession.js +263 -0
  606. package/dist/lib/daemon/supervisor.d.ts +132 -0
  607. package/dist/lib/daemon/supervisor.js +836 -0
  608. package/dist/lib/daemon/sync-runner.d.ts +58 -0
  609. package/dist/lib/daemon/sync-runner.js +277 -0
  610. package/dist/lib/daemon/tools.d.ts +52 -0
  611. package/dist/lib/daemon/tools.js +95 -0
  612. package/dist/lib/daemon/updater.d.ts +57 -0
  613. package/dist/lib/daemon/updater.js +163 -0
  614. package/dist/lib/db/control-plane.js +6 -0
  615. package/dist/lib/db/local.d.ts +0 -5
  616. package/dist/lib/db/local.js +1 -1
  617. package/dist/lib/db/migrate.d.ts +0 -12
  618. package/dist/lib/db/migrate.js +5 -5
  619. package/dist/lib/db/remote-sql.d.ts +2 -2
  620. package/dist/lib/db/remote-sql.js +3 -3
  621. package/dist/lib/db/sql.d.ts +0 -10
  622. package/dist/lib/db/sql.js +4 -8
  623. package/dist/lib/deploy-api.d.ts +5 -0
  624. package/dist/lib/deploy-api.js +21 -0
  625. package/dist/lib/deploy-apps.d.ts +13 -0
  626. package/dist/lib/deploy-apps.js +29 -0
  627. package/dist/lib/doctor/__testing__/fake-hq-tree.d.ts +1 -1
  628. package/dist/lib/doctor/__testing__/fake-hq-tree.js +1 -1
  629. package/dist/lib/doctor/__testing__/run-doctor.d.ts +8 -0
  630. package/dist/lib/doctor/__testing__/run-doctor.js +52 -0
  631. package/dist/lib/doctor/checks/bots.d.ts +13 -0
  632. package/dist/lib/doctor/checks/bots.js +189 -0
  633. package/dist/lib/doctor/checks/claude-wiring.js +70 -1
  634. package/dist/lib/doctor/checks/codex-wiring.d.ts +0 -4
  635. package/dist/lib/doctor/checks/codex-wiring.js +3 -35
  636. package/dist/lib/doctor/checks/grok-wiring.js +1 -34
  637. package/dist/lib/doctor/checks/integrations.js +11 -0
  638. package/dist/lib/doctor/checks/runtime-health.d.ts +4 -17
  639. package/dist/lib/doctor/checks/runtime-health.js +20 -7
  640. package/dist/lib/doctor/checks/scaffold-cli-floor.d.ts +6 -0
  641. package/dist/lib/doctor/checks/scaffold-cli-floor.js +73 -0
  642. package/dist/lib/doctor/checks/sync-health.d.ts +21 -0
  643. package/dist/lib/doctor/checks/sync-health.js +72 -2
  644. package/dist/lib/doctor/checks/sync-runtime.d.ts +11 -0
  645. package/dist/lib/doctor/checks/sync-runtime.js +216 -0
  646. package/dist/lib/doctor/checks/work-context.js +20 -6
  647. package/dist/lib/doctor/client-health-flag-token.d.ts +15 -0
  648. package/dist/lib/doctor/client-health-flag-token.js +66 -0
  649. package/dist/lib/doctor/client-health-reason-codes-flag.d.ts +27 -0
  650. package/dist/lib/doctor/client-health-reason-codes-flag.js +106 -0
  651. package/dist/lib/doctor/compat.d.ts +0 -16
  652. package/dist/lib/doctor/compat.js +4 -3
  653. package/dist/lib/doctor/deep/effects.js +1 -3
  654. package/dist/lib/doctor/deep/executor.d.ts +4 -10
  655. package/dist/lib/doctor/deep/executor.js +1 -1
  656. package/dist/lib/doctor/deep/parity.d.ts +2 -29
  657. package/dist/lib/doctor/deep/parity.js +7 -7
  658. package/dist/lib/doctor/deep/sandbox.d.ts +7 -25
  659. package/dist/lib/doctor/deep/sandbox.js +5 -7
  660. package/dist/lib/doctor/fix/apply.d.ts +79 -8
  661. package/dist/lib/doctor/fix/apply.js +292 -80
  662. package/dist/lib/doctor/fix/json-output.d.ts +28 -0
  663. package/dist/lib/doctor/fix/json-output.js +33 -0
  664. package/dist/lib/doctor/fix/refresh-sync-outcome.d.ts +12 -0
  665. package/dist/lib/doctor/fix/refresh-sync-outcome.js +25 -0
  666. package/dist/lib/doctor/fix/remediation.d.ts +14 -6
  667. package/dist/lib/doctor/fix/remediation.js +49 -9
  668. package/dist/lib/doctor/fixtures/schema.js +1 -3
  669. package/dist/lib/doctor/json-output.d.ts +17 -1
  670. package/dist/lib/doctor/json-output.js +7 -2
  671. package/dist/lib/doctor/platform.d.ts +0 -26
  672. package/dist/lib/doctor/platform.js +5 -5
  673. package/dist/lib/doctor/registry.d.ts +14 -0
  674. package/dist/lib/doctor/registry.js +71 -0
  675. package/dist/lib/doctor/report.js +1 -1
  676. package/dist/lib/doctor/shared.d.ts +8 -0
  677. package/dist/lib/doctor/shared.js +38 -0
  678. package/dist/lib/doctor/stray-gate-entries.d.ts +35 -0
  679. package/dist/lib/doctor/stray-gate-entries.js +83 -0
  680. package/dist/lib/doctor/types.d.ts +5 -0
  681. package/dist/lib/doctor/types.js +20 -0
  682. package/dist/lib/first-sync-next-step.d.ts +34 -0
  683. package/dist/lib/first-sync-next-step.js +179 -0
  684. package/dist/lib/flag-registry-endpoint.d.ts +5 -0
  685. package/dist/lib/flag-registry-endpoint.js +9 -0
  686. package/dist/lib/flag-registry-worker-source.generated.d.ts +2 -0
  687. package/dist/lib/flag-registry-worker-source.generated.js +3 -0
  688. package/dist/lib/flag-registry-worker.js +12 -1
  689. package/dist/lib/flag-registry.d.ts +26 -6
  690. package/dist/lib/flag-registry.js +133 -16
  691. package/dist/lib/flag-snapshot-file.d.ts +20 -0
  692. package/dist/lib/flag-snapshot-file.js +91 -0
  693. package/dist/lib/heartbeat-identity.d.ts +30 -0
  694. package/dist/lib/heartbeat-identity.js +76 -0
  695. package/dist/lib/index-render/companies.d.ts +4 -1
  696. package/dist/lib/index-render/companies.js +66 -3
  697. package/dist/lib/index-render/company-knowledge.js +80 -17
  698. package/dist/lib/index-render/index.d.ts +1 -0
  699. package/dist/lib/index-render/index.js +2 -1
  700. package/dist/lib/index-render/projects.js +82 -5
  701. package/dist/lib/index-render/shared.d.ts +13 -1
  702. package/dist/lib/index-render/shared.js +41 -2
  703. package/dist/lib/lanes/account-home.d.ts +8 -0
  704. package/dist/lib/lanes/account-home.js +10 -0
  705. package/dist/lib/lanes/accounts.d.ts +117 -0
  706. package/dist/lib/lanes/accounts.js +366 -0
  707. package/dist/lib/lanes/admission-host.d.ts +22 -0
  708. package/dist/lib/lanes/admission-host.js +37 -0
  709. package/dist/lib/lanes/admission.d.ts +26 -0
  710. package/dist/lib/lanes/admission.js +210 -0
  711. package/dist/lib/lanes/approval-gate.d.ts +8 -0
  712. package/dist/lib/lanes/approval-gate.js +90 -0
  713. package/dist/lib/lanes/autopilot-metrics.d.ts +99 -0
  714. package/dist/lib/lanes/autopilot-metrics.js +301 -0
  715. package/dist/lib/lanes/autopilot-registry.d.ts +62 -0
  716. package/dist/lib/lanes/autopilot-registry.js +192 -0
  717. package/dist/lib/lanes/autopilot.d.ts +133 -0
  718. package/dist/lib/lanes/autopilot.js +1222 -0
  719. package/dist/lib/lanes/blocker-freshness.d.ts +31 -0
  720. package/dist/lib/lanes/blocker-freshness.js +168 -0
  721. package/dist/lib/lanes/checked-company-path.d.ts +3 -0
  722. package/dist/lib/lanes/checked-company-path.js +39 -0
  723. package/dist/lib/lanes/claude-balance.d.ts +97 -0
  724. package/dist/lib/lanes/claude-balance.js +319 -0
  725. package/dist/lib/lanes/compaction.d.ts +110 -0
  726. package/dist/lib/lanes/compaction.js +293 -0
  727. package/dist/lib/lanes/config.d.ts +94 -0
  728. package/dist/lib/lanes/config.js +606 -0
  729. package/dist/lib/lanes/constants.d.ts +3 -0
  730. package/dist/lib/lanes/constants.js +3 -0
  731. package/dist/lib/lanes/delivery-core.d.ts +15 -0
  732. package/dist/lib/lanes/delivery-core.js +21 -0
  733. package/dist/lib/lanes/delivery.d.ts +180 -0
  734. package/dist/lib/lanes/delivery.js +443 -0
  735. package/dist/lib/lanes/disk-guard.d.ts +66 -0
  736. package/dist/lib/lanes/disk-guard.js +172 -0
  737. package/dist/lib/lanes/dropbox.d.ts +129 -0
  738. package/dist/lib/lanes/dropbox.js +601 -0
  739. package/dist/lib/lanes/effort.d.ts +51 -0
  740. package/dist/lib/lanes/effort.js +113 -0
  741. package/dist/lib/lanes/envelope-repair-dispatch.d.ts +26 -0
  742. package/dist/lib/lanes/envelope-repair-dispatch.js +74 -0
  743. package/dist/lib/lanes/errors.d.ts +39 -0
  744. package/dist/lib/lanes/errors.js +72 -0
  745. package/dist/lib/lanes/flag-keys.d.ts +22 -0
  746. package/dist/lib/lanes/flag-keys.js +22 -0
  747. package/dist/lib/lanes/grants.d.ts +30 -0
  748. package/dist/lib/lanes/grants.js +171 -0
  749. package/dist/lib/lanes/hire-grant-dispatch.d.ts +29 -0
  750. package/dist/lib/lanes/hire-grant-dispatch.js +315 -0
  751. package/dist/lib/lanes/hooks-install.d.ts +82 -0
  752. package/dist/lib/lanes/hooks-install.js +479 -0
  753. package/dist/lib/lanes/host-capacity.d.ts +69 -0
  754. package/dist/lib/lanes/host-capacity.js +254 -0
  755. package/dist/lib/lanes/hq-root.d.ts +19 -0
  756. package/dist/lib/lanes/hq-root.js +28 -0
  757. package/dist/lib/lanes/index.d.ts +76 -0
  758. package/dist/lib/lanes/index.js +42 -0
  759. package/dist/lib/lanes/keep-alive-audit.d.ts +46 -0
  760. package/dist/lib/lanes/keep-alive-audit.js +370 -0
  761. package/dist/lib/lanes/keep-alive.d.ts +68 -0
  762. package/dist/lib/lanes/keep-alive.js +343 -0
  763. package/dist/lib/lanes/kr-freshness.d.ts +76 -0
  764. package/dist/lib/lanes/kr-freshness.js +413 -0
  765. package/dist/lib/lanes/lane-company.d.ts +10 -0
  766. package/dist/lib/lanes/lane-company.js +41 -0
  767. package/dist/lib/lanes/liveness.d.ts +108 -0
  768. package/dist/lib/lanes/liveness.js +396 -0
  769. package/dist/lib/lanes/machine-identity.d.ts +34 -0
  770. package/dist/lib/lanes/machine-identity.js +92 -0
  771. package/dist/lib/lanes/manager-status-gate.d.ts +17 -0
  772. package/dist/lib/lanes/manager-status-gate.js +133 -0
  773. package/dist/lib/lanes/merge-command.d.ts +15 -0
  774. package/dist/lib/lanes/merge-command.js +629 -0
  775. package/dist/lib/lanes/mesh-events.d.ts +127 -0
  776. package/dist/lib/lanes/mesh-events.js +564 -0
  777. package/dist/lib/lanes/message-replay.d.ts +21 -0
  778. package/dist/lib/lanes/message-replay.js +122 -0
  779. package/dist/lib/lanes/models.d.ts +86 -0
  780. package/dist/lib/lanes/models.js +234 -0
  781. package/dist/lib/lanes/monitor.d.ts +48 -0
  782. package/dist/lib/lanes/monitor.js +428 -0
  783. package/dist/lib/lanes/move-account.d.ts +56 -0
  784. package/dist/lib/lanes/move-account.js +193 -0
  785. package/dist/lib/lanes/orphans.d.ts +25 -0
  786. package/dist/lib/lanes/orphans.js +102 -0
  787. package/dist/lib/lanes/outpost-dispatch.d.ts +147 -0
  788. package/dist/lib/lanes/outpost-dispatch.js +271 -0
  789. package/dist/lib/lanes/outpost-hire-cache.d.ts +22 -0
  790. package/dist/lib/lanes/outpost-hire-cache.js +153 -0
  791. package/dist/lib/lanes/outpost-identity.d.ts +33 -0
  792. package/dist/lib/lanes/outpost-identity.js +59 -0
  793. package/dist/lib/lanes/outpost-profile-repin.d.ts +17 -0
  794. package/dist/lib/lanes/outpost-profile-repin.js +84 -0
  795. package/dist/lib/lanes/outpost-pruner-install.d.ts +37 -0
  796. package/dist/lib/lanes/outpost-pruner-install.js +238 -0
  797. package/dist/lib/lanes/outpost-registry-worker-approval.d.ts +18 -0
  798. package/dist/lib/lanes/outpost-registry-worker-approval.js +109 -0
  799. package/dist/lib/lanes/outpost-snapshot-memo.d.ts +6 -0
  800. package/dist/lib/lanes/outpost-snapshot-memo.js +14 -0
  801. package/dist/lib/lanes/outpost-worktree-pruner-flag.d.ts +19 -0
  802. package/dist/lib/lanes/outpost-worktree-pruner-flag.js +86 -0
  803. package/dist/lib/lanes/outpost-worktree-pruner.d.ts +62 -0
  804. package/dist/lib/lanes/outpost-worktree-pruner.js +1201 -0
  805. package/dist/lib/lanes/profile.d.ts +69 -0
  806. package/dist/lib/lanes/profile.js +552 -0
  807. package/dist/lib/lanes/provider-rejection.d.ts +86 -0
  808. package/dist/lib/lanes/provider-rejection.js +286 -0
  809. package/dist/lib/lanes/questions.d.ts +90 -0
  810. package/dist/lib/lanes/questions.js +384 -0
  811. package/dist/lib/lanes/quota.d.ts +134 -0
  812. package/dist/lib/lanes/quota.js +471 -0
  813. package/dist/lib/lanes/reconcile-install.d.ts +90 -0
  814. package/dist/lib/lanes/reconcile-install.js +257 -0
  815. package/dist/lib/lanes/reconcile.d.ts +161 -0
  816. package/dist/lib/lanes/reconcile.js +3343 -0
  817. package/dist/lib/lanes/registry-flag.d.ts +35 -0
  818. package/dist/lib/lanes/registry-flag.js +164 -0
  819. package/dist/lib/lanes/registry-persona.d.ts +20 -0
  820. package/dist/lib/lanes/registry-persona.js +329 -0
  821. package/dist/lib/lanes/registry.d.ts +122 -0
  822. package/dist/lib/lanes/registry.js +570 -0
  823. package/dist/lib/lanes/reviews.d.ts +38 -0
  824. package/dist/lib/lanes/reviews.js +395 -0
  825. package/dist/lib/lanes/runaway-tool-guard.d.ts +38 -0
  826. package/dist/lib/lanes/runaway-tool-guard.js +284 -0
  827. package/dist/lib/lanes/session-relink.d.ts +25 -0
  828. package/dist/lib/lanes/session-relink.js +216 -0
  829. package/dist/lib/lanes/spawn.d.ts +355 -0
  830. package/dist/lib/lanes/spawn.js +2668 -0
  831. package/dist/lib/lanes/ssm-command-context.d.ts +12 -0
  832. package/dist/lib/lanes/ssm-command-context.js +52 -0
  833. package/dist/lib/lanes/store.d.ts +91 -0
  834. package/dist/lib/lanes/store.js +1822 -0
  835. package/dist/lib/lanes/stories.d.ts +23 -0
  836. package/dist/lib/lanes/stories.js +92 -0
  837. package/dist/lib/lanes/story-ledger.d.ts +80 -0
  838. package/dist/lib/lanes/story-ledger.js +815 -0
  839. package/dist/lib/lanes/story-switch.d.ts +21 -0
  840. package/dist/lib/lanes/story-switch.js +75 -0
  841. package/dist/lib/lanes/text-scan.d.ts +11 -0
  842. package/dist/lib/lanes/text-scan.js +103 -0
  843. package/dist/lib/lanes/throttle.d.ts +116 -0
  844. package/dist/lib/lanes/throttle.js +437 -0
  845. package/dist/lib/lanes/timestamp.d.ts +3 -0
  846. package/dist/lib/lanes/timestamp.js +26 -0
  847. package/dist/lib/lanes/tracker-checks.d.ts +77 -0
  848. package/dist/lib/lanes/tracker-checks.js +134 -0
  849. package/dist/lib/lanes/tracker-report-state.d.ts +10 -0
  850. package/dist/lib/lanes/tracker-report-state.js +124 -0
  851. package/dist/lib/lanes/tracker-report.d.ts +203 -0
  852. package/dist/lib/lanes/tracker-report.js +522 -0
  853. package/dist/lib/lanes/tracker-store.d.ts +18 -0
  854. package/dist/lib/lanes/tracker-store.js +106 -0
  855. package/dist/lib/lanes/types.d.ts +1030 -0
  856. package/dist/lib/lanes/types.js +418 -0
  857. package/dist/lib/lanes/wait.d.ts +39 -0
  858. package/dist/lib/lanes/wait.js +159 -0
  859. package/dist/lib/lanes/watch.d.ts +39 -0
  860. package/dist/lib/lanes/watch.js +249 -0
  861. package/dist/lib/lanes/watcher-main.d.ts +11 -0
  862. package/dist/lib/lanes/watcher-main.js +108 -0
  863. package/dist/lib/lanes/watcher.d.ts +287 -0
  864. package/dist/lib/lanes/watcher.js +1972 -0
  865. package/dist/lib/mesh/api.d.ts +103 -10
  866. package/dist/lib/mesh/api.js +334 -64
  867. package/dist/lib/mesh/client.d.ts +3 -2
  868. package/dist/lib/mesh/client.js +26 -7
  869. package/dist/lib/mesh/denial-codes.d.ts +5 -0
  870. package/dist/lib/mesh/denial-codes.js +14 -0
  871. package/dist/lib/mesh/live/backfill-held.d.ts +67 -16
  872. package/dist/lib/mesh/live/backfill-held.js +415 -61
  873. package/dist/lib/mesh/live/backoff.d.ts +2 -2
  874. package/dist/lib/mesh/live/backoff.js +16 -3
  875. package/dist/lib/mesh/live/daemon/board-refresh.d.ts +7 -1
  876. package/dist/lib/mesh/live/daemon/board-refresh.js +78 -30
  877. package/dist/lib/mesh/live/daemon/config.d.ts +36 -0
  878. package/dist/lib/mesh/live/daemon/config.js +120 -0
  879. package/dist/lib/mesh/live/daemon/credentials.d.ts +64 -10
  880. package/dist/lib/mesh/live/daemon/credentials.js +270 -20
  881. package/dist/lib/mesh/live/daemon/doctor.d.ts +53 -0
  882. package/dist/lib/mesh/live/daemon/doctor.js +190 -6
  883. package/dist/lib/mesh/live/daemon/index.d.ts +13 -10
  884. package/dist/lib/mesh/live/daemon/index.js +7 -6
  885. package/dist/lib/mesh/live/daemon/install.d.ts +255 -1
  886. package/dist/lib/mesh/live/daemon/install.js +811 -13
  887. package/dist/lib/mesh/live/daemon/legacy-listen.d.ts +43 -0
  888. package/dist/lib/mesh/live/daemon/legacy-listen.js +89 -0
  889. package/dist/lib/mesh/live/daemon/live-cache.d.ts +26 -0
  890. package/dist/lib/mesh/live/daemon/live-cache.js +116 -0
  891. package/dist/lib/mesh/live/daemon/mqtt-events.d.ts +7 -0
  892. package/dist/lib/mesh/live/daemon/mqtt-events.js +2 -0
  893. package/dist/lib/mesh/live/daemon/mqtt-events.test-d.d.ts +2 -0
  894. package/dist/lib/mesh/live/daemon/mqtt-events.test-d.js +10 -0
  895. package/dist/lib/mesh/live/daemon/paths.d.ts +2 -0
  896. package/dist/lib/mesh/live/daemon/paths.js +2 -0
  897. package/dist/lib/mesh/live/daemon/presence.d.ts +24 -5
  898. package/dist/lib/mesh/live/daemon/presence.js +79 -10
  899. package/dist/lib/mesh/live/daemon/run.d.ts +48 -2
  900. package/dist/lib/mesh/live/daemon/run.js +485 -33
  901. package/dist/lib/mesh/live/daemon/state.d.ts +19 -2
  902. package/dist/lib/mesh/live/daemon/state.js +34 -2
  903. package/dist/lib/mesh/live/daemon/transcript-watch.d.ts +45 -9
  904. package/dist/lib/mesh/live/daemon/transcript-watch.js +227 -31
  905. package/dist/lib/mesh/live/daemon/version-drift.d.ts +5 -0
  906. package/dist/lib/mesh/live/daemon/version-drift.js +47 -0
  907. package/dist/lib/mesh/live/daemon/work-subscribe.d.ts +32 -0
  908. package/dist/lib/mesh/live/daemon/work-subscribe.js +262 -0
  909. package/dist/lib/mesh/live/emit-client.d.ts +8 -0
  910. package/dist/lib/mesh/live/emit-client.js +23 -8
  911. package/dist/lib/mesh/live/emit-evidence.d.ts +19 -0
  912. package/dist/lib/mesh/live/emit-evidence.js +20 -2
  913. package/dist/lib/mesh/live/emit.d.ts +9 -0
  914. package/dist/lib/mesh/live/emit.js +28 -2
  915. package/dist/lib/mesh/live/enqueue.d.ts +5 -0
  916. package/dist/lib/mesh/live/enqueue.js +19 -0
  917. package/dist/lib/mesh/live/flush.d.ts +17 -5
  918. package/dist/lib/mesh/live/flush.js +391 -38
  919. package/dist/lib/mesh/live/format-spool-line.d.ts +5 -2
  920. package/dist/lib/mesh/live/format-spool-line.js +10 -0
  921. package/dist/lib/mesh/live/paths.d.ts +2 -0
  922. package/dist/lib/mesh/live/paths.js +4 -0
  923. package/dist/lib/mesh/live/report-summary.d.ts +45 -0
  924. package/dist/lib/mesh/live/report-summary.js +234 -0
  925. package/dist/lib/mesh/live/session-event.schema.json +24 -1
  926. package/dist/lib/mesh/live/session-events-client.d.ts +2 -0
  927. package/dist/lib/mesh/live/session-events-client.js +26 -0
  928. package/dist/lib/mesh/live/session-seq.d.ts +4 -0
  929. package/dist/lib/mesh/live/session-seq.js +33 -0
  930. package/dist/lib/mesh/live/spool.d.ts +19 -0
  931. package/dist/lib/mesh/live/spool.js +269 -3
  932. package/dist/lib/mesh/live/validate-session-event.d.ts +1 -1
  933. package/dist/lib/mesh/live/validate-session-event.js +1 -1
  934. package/dist/lib/mesh/project-board.d.ts +65 -0
  935. package/dist/lib/mesh/project-board.js +181 -0
  936. package/dist/lib/mesh/story-status.d.ts +22 -0
  937. package/dist/lib/mesh/story-status.js +49 -0
  938. package/dist/lib/monitor.d.ts +149 -0
  939. package/dist/lib/monitor.js +519 -0
  940. package/dist/lib/node-binary.d.ts +50 -0
  941. package/dist/lib/node-binary.js +133 -0
  942. package/dist/lib/notify-recipient.d.ts +7 -0
  943. package/dist/lib/notify-recipient.js +11 -0
  944. package/dist/lib/onboarding/checkpoint.d.ts +13 -0
  945. package/dist/lib/onboarding/checkpoint.js +44 -0
  946. package/dist/lib/onboarding/cli/onboard.d.ts +53 -0
  947. package/dist/lib/onboarding/cli/onboard.js +135 -0
  948. package/dist/lib/onboarding/cli/prompts.d.ts +28 -0
  949. package/dist/lib/onboarding/cli/prompts.js +83 -0
  950. package/dist/lib/onboarding/errors.d.ts +34 -0
  951. package/dist/lib/onboarding/errors.js +59 -0
  952. package/dist/lib/onboarding/index.d.ts +13 -0
  953. package/dist/lib/onboarding/index.js +13 -0
  954. package/dist/lib/onboarding/orchestrator.d.ts +47 -0
  955. package/dist/lib/onboarding/orchestrator.js +569 -0
  956. package/dist/lib/onboarding/types.d.ts +79 -0
  957. package/dist/lib/onboarding/types.js +8 -0
  958. package/dist/lib/plan-limit-nag.d.ts +102 -6
  959. package/dist/lib/plan-limit-nag.js +348 -43
  960. package/dist/lib/policy-validator.d.ts +7 -0
  961. package/dist/lib/policy-validator.js +187 -0
  962. package/dist/lib/process-flag-company-context.d.ts +8 -0
  963. package/dist/lib/process-flag-company-context.js +67 -0
  964. package/dist/lib/report-publish.d.ts +175 -0
  965. package/dist/lib/report-publish.js +421 -0
  966. package/dist/lib/report-run.d.ts +62 -0
  967. package/dist/lib/report-run.js +123 -0
  968. package/dist/lib/report-snapshot.d.ts +12 -0
  969. package/dist/lib/report-snapshot.js +135 -0
  970. package/dist/lib/report-templates.d.ts +9 -0
  971. package/dist/lib/report-templates.js +21 -0
  972. package/dist/lib/scaffold-cli-floor.d.ts +8 -0
  973. package/dist/lib/scaffold-cli-floor.js +50 -0
  974. package/dist/lib/scan-packages/index.d.ts +2 -0
  975. package/dist/lib/scan-packages/index.js +168 -6
  976. package/dist/lib/search-index/background.d.ts +8 -1
  977. package/dist/lib/search-index/background.js +55 -200
  978. package/dist/lib/search-index/collection-path-repair-flag.d.ts +4 -0
  979. package/dist/lib/search-index/collection-path-repair-flag.js +7 -0
  980. package/dist/lib/search-index/embed-lock.d.ts +34 -0
  981. package/dist/lib/search-index/embed-lock.js +295 -0
  982. package/dist/lib/search-index/index.d.ts +12 -0
  983. package/dist/lib/search-index/index.js +153 -7
  984. package/dist/lib/search-index/load-gate.d.ts +57 -0
  985. package/dist/lib/search-index/load-gate.js +270 -0
  986. package/dist/lib/search-index/max-doc-bytes.d.ts +219 -0
  987. package/dist/lib/search-index/max-doc-bytes.js +463 -0
  988. package/dist/lib/search-index/qmd-exit-diagnostics.d.ts +5 -0
  989. package/dist/lib/search-index/qmd-exit-diagnostics.js +40 -0
  990. package/dist/lib/search-index/reindex-single-flight-flag.d.ts +5 -0
  991. package/dist/lib/search-index/reindex-single-flight-flag.js +8 -0
  992. package/dist/lib/service-manager/index.d.ts +42 -0
  993. package/dist/lib/service-manager/index.js +105 -0
  994. package/dist/lib/service-manager/launchd.d.ts +81 -0
  995. package/dist/lib/service-manager/launchd.js +212 -0
  996. package/dist/lib/service-manager/systemd.d.ts +19 -0
  997. package/dist/lib/service-manager/systemd.js +72 -0
  998. package/dist/lib/service-manager/types.d.ts +34 -0
  999. package/dist/lib/service-manager/types.js +26 -0
  1000. package/dist/lib/test-utils/isolated-flag-company-context.d.ts +3 -0
  1001. package/dist/lib/test-utils/isolated-flag-company-context.js +18 -0
  1002. package/dist/lib/work-context/bind-best.d.ts +66 -0
  1003. package/dist/lib/work-context/bind-best.js +344 -0
  1004. package/dist/lib/work-context/bind-calling-session.d.ts +26 -0
  1005. package/dist/lib/work-context/bind-calling-session.js +63 -0
  1006. package/dist/lib/work-context/company-rebind.d.ts +23 -0
  1007. package/dist/lib/work-context/company-rebind.js +59 -0
  1008. package/dist/lib/work-context/company.d.ts +22 -20
  1009. package/dist/lib/work-context/company.js +124 -40
  1010. package/dist/lib/work-context/config.d.ts +58 -11
  1011. package/dist/lib/work-context/config.js +334 -83
  1012. package/dist/lib/work-context/contract.d.ts +13 -0
  1013. package/dist/lib/work-context/contract.schema.json +3 -1
  1014. package/dist/lib/work-context/default-company.d.ts +43 -0
  1015. package/dist/lib/work-context/default-company.js +83 -0
  1016. package/dist/lib/work-context/outbox-quarantine-status-flag.d.ts +4 -0
  1017. package/dist/lib/work-context/outbox-quarantine-status-flag.js +7 -0
  1018. package/dist/lib/work-context/outbox.d.ts +27 -5
  1019. package/dist/lib/work-context/outbox.js +135 -6
  1020. package/dist/lib/work-context/prd-sync.d.ts +56 -0
  1021. package/dist/lib/work-context/prd-sync.js +197 -0
  1022. package/dist/lib/work-context/preflight-contract.d.ts +28 -0
  1023. package/dist/lib/work-context/preflight-contract.js +85 -0
  1024. package/dist/lib/work-context/prompt-rebind.d.ts +25 -0
  1025. package/dist/lib/work-context/prompt-rebind.js +85 -0
  1026. package/dist/lib/work-context/reconcile.d.ts +15 -0
  1027. package/dist/lib/work-context/reconcile.js +405 -29
  1028. package/dist/lib/work-context/state.d.ts +93 -10
  1029. package/dist/lib/work-context/state.js +342 -29
  1030. package/dist/lib/work-context/windows-acl.d.ts +123 -0
  1031. package/dist/lib/work-context/windows-acl.js +425 -0
  1032. package/dist/lib/workers-registry/index.js +62 -8
  1033. package/dist/lib/workers-registry/read.d.ts +15 -0
  1034. package/dist/lib/workers-registry/read.js +17 -0
  1035. package/dist/main.d.ts +32 -0
  1036. package/dist/main.js +273 -73
  1037. package/dist/native-fast-route.d.ts +7 -0
  1038. package/dist/native-fast-route.js +45 -0
  1039. package/dist/node-network-compat-core.d.ts +12 -0
  1040. package/dist/node-network-compat-core.js +41 -0
  1041. package/dist/node-network-compat-fast.d.ts +7 -0
  1042. package/dist/node-network-compat-fast.js +23 -0
  1043. package/dist/node-network-compat.d.ts +2 -30
  1044. package/dist/node-network-compat.js +4 -51
  1045. package/dist/node-preflight.d.ts +7 -1
  1046. package/dist/node-preflight.js +5 -5
  1047. package/dist/register-all.d.ts +4 -29
  1048. package/dist/register-all.js +4 -233
  1049. package/dist/run/discover-schemas.d.ts +2 -1
  1050. package/dist/run/env-graph-guard.d.ts +2 -2
  1051. package/dist/run/env-graph-guard.js +5 -2
  1052. package/dist/run/hq-plugin.d.ts +4 -0
  1053. package/dist/run/hq-plugin.js +36 -8
  1054. package/dist/secrets-env-fast-path.d.ts +5 -0
  1055. package/dist/secrets-env-fast-path.js +16 -0
  1056. package/dist/secrets-env-fast-route.d.ts +18 -0
  1057. package/dist/secrets-env-fast-route.js +54 -0
  1058. package/dist/sentry-before-send.js +80 -0
  1059. package/dist/sentry.d.ts +16 -4
  1060. package/dist/sentry.js +179 -18
  1061. package/dist/startup-registration.d.ts +26 -2
  1062. package/dist/startup-registration.js +116 -15
  1063. package/dist/utils/checkpoint-stamp-write-error.d.ts +28 -0
  1064. package/dist/utils/checkpoint-stamp-write-error.js +99 -0
  1065. package/dist/utils/cli-network-lifecycle.d.ts +4 -0
  1066. package/dist/utils/cli-network-lifecycle.js +11 -0
  1067. package/dist/utils/cli-telemetry.d.ts +75 -1
  1068. package/dist/utils/cli-telemetry.js +433 -7
  1069. package/dist/utils/client-health.d.ts +0 -4
  1070. package/dist/utils/client-health.js +2 -2
  1071. package/dist/utils/cognito-refresh-sentry.d.ts +3 -0
  1072. package/dist/utils/cognito-refresh-sentry.js +49 -0
  1073. package/dist/utils/cognito-session.d.ts +71 -0
  1074. package/dist/utils/cognito-session.js +481 -27
  1075. package/dist/utils/console-origin.d.ts +9 -0
  1076. package/dist/utils/console-origin.js +28 -0
  1077. package/dist/utils/contribution-table.d.ts +1 -1
  1078. package/dist/utils/core-eval-trigger.d.ts +4 -0
  1079. package/dist/utils/core-eval-trigger.js +88 -0
  1080. package/dist/utils/environmental-error.d.ts +18 -0
  1081. package/dist/utils/environmental-error.js +240 -7
  1082. package/dist/utils/exclusive-lock.d.ts +32 -0
  1083. package/dist/utils/exclusive-lock.js +75 -0
  1084. package/dist/utils/expected-cli-error.d.ts +2 -0
  1085. package/dist/utils/expected-cli-error.js +4 -0
  1086. package/dist/utils/fast-stream-errors.d.ts +12 -0
  1087. package/dist/utils/fast-stream-errors.js +18 -0
  1088. package/dist/utils/feedback-diagnostics.d.ts +2 -1
  1089. package/dist/utils/feedback-log-bundle.d.ts +51 -26
  1090. package/dist/utils/feedback-log-bundle.js +211 -31
  1091. package/dist/utils/feedback-logs.d.ts +18 -47
  1092. package/dist/utils/feedback-logs.js +50 -21
  1093. package/dist/utils/feedback-screenshots.d.ts +0 -1
  1094. package/dist/utils/feedback-screenshots.js +1 -1
  1095. package/dist/utils/files-browse-acl.d.ts +5 -0
  1096. package/dist/utils/files-browse-acl.js +11 -0
  1097. package/dist/utils/hq-core-yaml.d.ts +10 -0
  1098. package/dist/utils/hq-core-yaml.js +40 -0
  1099. package/dist/utils/hq-root-resolver.d.ts +13 -0
  1100. package/dist/utils/hq-root-resolver.js +103 -0
  1101. package/dist/utils/hq-roots.d.ts +25 -2
  1102. package/dist/utils/hq-roots.js +67 -7
  1103. package/dist/utils/incomplete-install-error.d.ts +87 -19
  1104. package/dist/utils/incomplete-install-error.js +319 -68
  1105. package/dist/utils/install-tree-torn.d.ts +260 -24
  1106. package/dist/utils/install-tree-torn.js +998 -51
  1107. package/dist/utils/large-file-guard.d.ts +0 -10
  1108. package/dist/utils/large-file-guard.js +1 -1
  1109. package/dist/utils/local-files-overview.d.ts +4 -8
  1110. package/dist/utils/local-files-overview.js +48 -7
  1111. package/dist/utils/login-provider.d.ts +7 -0
  1112. package/dist/utils/login-provider.js +34 -0
  1113. package/dist/utils/pack-contributions.d.ts +2 -14
  1114. package/dist/utils/pack-contributions.js +7 -44
  1115. package/dist/utils/package-root-diagnostics.d.ts +1 -1
  1116. package/dist/utils/people.d.ts +7 -11
  1117. package/dist/utils/people.js +5 -20
  1118. package/dist/utils/plan-gate-error.d.ts +52 -10
  1119. package/dist/utils/plan-gate-error.js +364 -41
  1120. package/dist/utils/qmd-model-download-error.d.ts +0 -8
  1121. package/dist/utils/qmd-model-download-error.js +1 -1
  1122. package/dist/utils/qmd-query-document-error.d.ts +0 -6
  1123. package/dist/utils/qmd-query-document-error.js +1 -1
  1124. package/dist/utils/redact-error-text.d.ts +0 -2
  1125. package/dist/utils/redact-error-text.js +3 -2
  1126. package/dist/utils/redact-url.d.ts +14 -0
  1127. package/dist/utils/redact-url.js +218 -0
  1128. package/dist/utils/run-bundled-script.d.ts +6 -0
  1129. package/dist/utils/run-bundled-script.js +117 -4
  1130. package/dist/utils/sandbox-runner-client.d.ts +2 -1
  1131. package/dist/utils/scaffold-fast-import-guard.d.ts +2 -0
  1132. package/dist/utils/scaffold-fast-import-guard.js +40 -0
  1133. package/dist/utils/scaffold-packlist-guard.d.ts +7 -0
  1134. package/dist/utils/scaffold-packlist-guard.js +23 -0
  1135. package/dist/utils/scaffold-sibling-references.d.ts +22 -0
  1136. package/dist/utils/scaffold-sibling-references.js +156 -0
  1137. package/dist/utils/secret-load-availability.d.ts +8 -0
  1138. package/dist/utils/secret-load-availability.js +54 -0
  1139. package/dist/utils/secrets-cache.d.ts +7 -3
  1140. package/dist/utils/secrets-cache.js +162 -14
  1141. package/dist/utils/self-update.d.ts +34 -1
  1142. package/dist/utils/self-update.js +660 -42
  1143. package/dist/utils/sentry-fingerprint.d.ts +23 -1
  1144. package/dist/utils/sentry-fingerprint.js +223 -31
  1145. package/dist/utils/staged-install.d.ts +34 -0
  1146. package/dist/utils/staged-install.js +346 -0
  1147. package/dist/utils/staged-npm-install.d.ts +16 -0
  1148. package/dist/utils/staged-npm-install.js +264 -0
  1149. package/dist/utils/sync-log-sources.d.ts +15 -0
  1150. package/dist/utils/sync-log-sources.js +36 -0
  1151. package/dist/utils/table-width.d.ts +7 -0
  1152. package/dist/utils/table-width.js +15 -0
  1153. package/dist/utils/team-upgrade.d.ts +1 -1
  1154. package/dist/utils/team-upgrade.js +4 -3
  1155. package/dist/utils/update-command-supervisor.cjs +247 -0
  1156. package/dist/utils/upload-author.d.ts +15 -0
  1157. package/dist/utils/upload-author.js +39 -0
  1158. package/dist/utils/vault-api.d.ts +21 -1
  1159. package/dist/utils/vault-api.js +251 -84
  1160. package/dist/utils/version-check.d.ts +11 -1
  1161. package/dist/utils/version-check.js +32 -3
  1162. package/dist/utils/version-gate.d.ts +101 -17
  1163. package/dist/utils/version-gate.js +645 -70
  1164. package/dist/utils/version-report.d.ts +27 -0
  1165. package/dist/utils/version-report.js +105 -0
  1166. package/dist/utils/windows-spawn.d.ts +6 -0
  1167. package/dist/utils/windows-spawn.js +37 -0
  1168. package/dist/version-request.d.ts +14 -0
  1169. package/dist/version-request.js +36 -0
  1170. package/package.json +20 -8
  1171. package/scripts/mesh-daemon-if-stale.mjs +20 -0
  1172. package/templates/reports/blank.md +3 -0
  1173. package/templates/reports/daily-digest.md +11 -0
  1174. package/templates/runbooks/update-report.md +21 -0
  1175. package/dist/commands/scaffold-fast.d.ts +0 -41
  1176. package/dist/commands/scaffold-fast.js +0 -57
  1177. package/dist/lib/work-context/index.d.ts +0 -25
  1178. package/dist/lib/work-context/index.js +0 -21
@@ -0,0 +1,3343 @@
1
+ /**
2
+ * Periodic reconcile pass (spec §5.7, §6.8, §9).
3
+ *
4
+ * Owned by the machine, not by any lane. Reads never mutate; every write is a
5
+ * compare-and-set. The per-lane watcher owns progress/terminal/silence of a
6
+ * live process — this pass owns the transitions no single lane can own.
7
+ */
8
+ import * as fs from "node:fs";
9
+ import * as path from "node:path";
10
+ import { parseUserYaml } from "../../utils/user-yaml-error.js";
11
+ import { compareLaneQueueOrder, hasActiveAdmissionReservation, hasActivePendingResume, laneAdmissionCapReason, laneRunsOnHost, withAllLaneAdmissionLocks, } from "./admission.js";
12
+ import { accountStatus, isCredentialProven, resolveAccount } from "./accounts.js";
13
+ import { effectiveAccountHome } from "./account-home.js";
14
+ import { enforceRegistryApprovalGate } from "./approval-gate.js";
15
+ import { loadLanesConfig, providerQuotaDefaultHoldMs } from "./config.js";
16
+ import { MAX_LANE_DEPTH } from "./constants.js";
17
+ import { AccountResolutionError, LanesError, isLaneVersionConflict } from "./errors.js";
18
+ import { loadWorkerProfile } from "./profile.js";
19
+ import { terminalChildResumeHireGrantError, withHireGrantAdmission, } from "./grants.js";
20
+ import { classifySessionLiveness, defaultLivenessDeps, loadLivenessIndex, readDaemonPresenceConnected, unindexedClaudeSessionLiveness, } from "./liveness.js";
21
+ import { ANSWERED_QUESTIONS_FILE, armLaneWatcher, envelopeRepairMode, isPidRecordAlive, laneRunDir, readPidRecord, readProcStarttime, readSpawnManifest, markSpawnFailure, stopLaneProcesses, spawnEnvelopeRepair, spawnLaneWorker, retryLaneOnAlternateAccount, } from "./spawn.js";
22
+ import { appendCappedHistory, lanesStoreRoot, parseLaneRecord, parseQuestionRecord, readLane, readQuestion, listQuestionIds, updateLane, updateQuestion, } from "./store.js";
23
+ import { applyThrottleDecision, decideAdmission, defaultThrottleDeps, emptyThrottleState, evaluateThrottle, isCpuCheckEnabled, readAvailableMemMb, readThrottleState, resolveCpuBusyFraction, sampleCpuBusyObservation, recordCpuBusyObservation, throttleStatePath, writeThrottleState, } from "./throttle.js";
24
+ import { readLanesRegistryFlag, readLanesRegistryFlagFailOff } from "./registry-flag.js";
25
+ import { LANE_HIRE_GRANT_FINISHED_LANES_FLAG } from "./flag-keys.js";
26
+ import { checkLaneDiskSpace, configuredCodexRolloutHomes, lowDiskGuardEnabledForLane, retainCodexRollouts } from "./disk-guard.js";
27
+ import { laneCompanyUidSource, resolveLaneCompany } from "./lane-company.js";
28
+ import { appendDropboxMessage } from "./dropbox.js";
29
+ import { DEFAULT_RUNAWAY_SEARCH_GUARD_CONFIG, listHostProcesses, runRunawayToolGuard, RUNAWAY_TOOL_GUARD_FLAG, } from "./runaway-tool-guard.js";
30
+ import { LEAD_LANE_KEEP_ALIVE_FLAG, LEAD_LANE_KEEP_ALIVE_RESUME_BLOCKED_FLAG, LEAD_LANE_KEEP_ALIVE_IMMEDIATE_FIRST_RESUME_FLAG, LEAD_LANE_KEEP_ALIVE_FIRST_RESUME_SETTLE_MS, LEAD_LANE_KEEP_ALIVE_NEXT_CHECK_MAX_DELAY_MS, LEAD_LANE_KEEP_ALIVE_BLOCKED_GRACE_MS, LEAD_LANE_KEEP_ALIVE_BLOCKED_RESUME_TEXT, queueLaneTerminalResume, LEAD_LANE_KEEP_ALIVE_INTERRUPTED_RESUME_TEXT, LEAD_LANE_KEEP_ALIVE_MAX_RESUMES, LEAD_LANE_KEEP_ALIVE_RESUME_TEXT, LANE_SUPERSESSION_LINEAGE_AWARE_FLAG, doneKeepAliveVerdict, hasPendingKeepAliveWork, keepAliveExclusion, loadRetiredLaneIds, leadLaneKeepAliveBackoffMs, undeliveredAnsweredQuestions, } from "./keep-alive.js";
31
+ import { providerSupportsSessionResume } from "./delivery.js";
32
+ import { applyDefaultQuotaHold, clearAccountQuotaAfterSuccessfulTurnBestEffort, readQuotaSignal, recordAccountQuota, } from "./quota.js";
33
+ import { answeredQuestionEntries, questionRecordRefusalCode, raiseEnvelopeQuestions, resolveQuestionCompanyWithFallback, } from "./questions.js";
34
+ import { SILENCE_MS, appendWatcherEvent, classifyLaneExit, isPidAlive, missingExpectedEnvelopeFile, readLatestEnvelopeFile, readLatestEnvelopeRecord, watcherEventsPath, watcherOutLogPath, writeWatcherDecision, } from "./watcher.js";
35
+ import { createLaneMesh, emitLaneDecisionEvents, emitLaneTerminalStateEvent, laneMeshCompany, } from "./mesh-events.js";
36
+ import { activeLaneStories, laneStories, terminalEnvelopeStoryStatus } from "./stories.js";
37
+ import { applyStorySwitchEnvelope } from "./story-switch.js";
38
+ import { readProjectStoryStates } from "./story-ledger.js";
39
+ import { decideEnvelopeRepair, envelopeQuestionIds, routeProviderFailure } from "./watcher.js";
40
+ import { PROVIDER_REJECTED_REASON, providerRejectionDetails, readProviderRejection, } from "./provider-rejection.js";
41
+ /** Spec §9: unanswered at the current hop for 60 minutes → one hop. */
42
+ export const QUESTION_AGE_MS = 60 * 60 * 1000;
43
+ /** Spec §6.6 + §9: a depth-3 worker has two hops to age toward the user. */
44
+ export const MAX_AGED_ADVANCES = MAX_LANE_DEPTH - 1;
45
+ /** Aligns with watcher silence: a host that has not reported for this long is dead. */
46
+ export const DEAD_HOST_MS = SILENCE_MS;
47
+ export const RECONCILE_LOCK_STALE_MS = 30_000;
48
+ export const RECONCILE_LOCK_TIMEOUT_MS = 5_000;
49
+ /** A pickup probe that has not cleared its marker within this window is abandoned. */
50
+ export const PICKUP_VALIDATION_MAX_AGE_MS = 15 * 60 * 1000;
51
+ export const PICKUP_VALIDATION_TIMEOUT_REASON = "pickup_validation_timeout";
52
+ export const CLOSE_INTERRUPTED_DONE_STORY_WORKERS_FLAG = "lanes.close-interrupted-done-story-workers";
53
+ const INTERRUPTED_DONE_STORY_MIN_AGE_MS = 24 * 60 * 60 * 1000;
54
+ export const RECONCILE_UNIT_NAME = "hq-lanes-reconcile.service";
55
+ export const RECONCILE_TIMER_NAME = "hq-lanes-reconcile.timer";
56
+ function spawnDepsForHost(deps) {
57
+ return {
58
+ ...(deps.spawnLaneDeps ?? {}),
59
+ env: deps.spawnLaneDeps?.env ?? deps.env ?? process.env,
60
+ hostname: deps.host.hostname,
61
+ admissionHost: deps.host,
62
+ };
63
+ }
64
+ function logSkip(skip) {
65
+ process.stderr.write(`hq lanes reconcile: skipped ${skip.kind}${skip.id ? ` ${skip.id}` : ""}: ${skip.why}\n`);
66
+ }
67
+ function emptySummary(dryRun) {
68
+ return {
69
+ ok: true,
70
+ action: "reconcile",
71
+ dry_run: dryRun,
72
+ admitted: [],
73
+ overdue: [],
74
+ aged: [],
75
+ advanced: [],
76
+ resumed: [],
77
+ dead_host: [],
78
+ heartbeats: [],
79
+ skipped: [],
80
+ keep_alive_blocked: [],
81
+ errors: [],
82
+ heartbeat_at: null,
83
+ };
84
+ }
85
+ function markDirty(summary, skip) {
86
+ summary.skipped.push(skip);
87
+ summary.ok = false;
88
+ logSkip(skip);
89
+ }
90
+ function terminalizeModelUnsupported(hqRoot, lane, err) {
91
+ if (!(err instanceof LanesError) || err.error !== "model_unsupported")
92
+ return;
93
+ try {
94
+ markSpawnFailure(hqRoot, lane.lane_id, err.error, {
95
+ ...err.details,
96
+ message: err.message,
97
+ });
98
+ }
99
+ catch (writeErr) {
100
+ process.stderr.write(`hq lanes reconcile: failed to terminalize ${lane.lane_id} after model validation: ${writeErr instanceof Error ? writeErr.message : String(writeErr)}\n`);
101
+ }
102
+ }
103
+ function markError(summary, err) {
104
+ const message = err instanceof Error ? err.message : String(err);
105
+ const code = err instanceof LanesError ? err.error : undefined;
106
+ summary.errors.push(code ? { message, code } : { message });
107
+ summary.ok = false;
108
+ process.stderr.write(`hq lanes reconcile: ${message}${err instanceof Error && err.stack ? `\n${err.stack}` : ""}\n`);
109
+ }
110
+ export function reconcileHeartbeatPath(hqRoot) {
111
+ return path.join(hqRoot, "workspace", "lanes-runs", "reconcile-heartbeat.json");
112
+ }
113
+ export function timeoutPath(hqRoot, laneId) {
114
+ return path.join(laneRunDir(hqRoot, laneId), "timeout.json");
115
+ }
116
+ export function laneRunsHere(lane, host) {
117
+ return laneRunsOnHost(lane, host);
118
+ }
119
+ function hasFreshAdmissionReservation(lane, nowMs) {
120
+ return hasActiveAdmissionReservation(lane, nowMs);
121
+ }
122
+ export function agedAdvanceCount(question) {
123
+ return question.routing_history.filter((entry) => entry.reason === "unanswered_timeout").length;
124
+ }
125
+ export function currentHopStartedAtMs(question) {
126
+ const atCurrent = [...question.routing_history]
127
+ .reverse()
128
+ .find((entry) => entry.hop === question.hop);
129
+ const raw = atCurrent?.at ?? question.asked_at;
130
+ const ms = Date.parse(raw);
131
+ return Number.isFinite(ms) ? ms : Number.NEGATIVE_INFINITY;
132
+ }
133
+ export function shouldAgeQuestion(question, audience, nowMs, ageWindowMs = QUESTION_AGE_MS) {
134
+ if (question.status !== "pending")
135
+ return false;
136
+ if (audience.kind === "user")
137
+ return false;
138
+ if (agedAdvanceCount(question) >= MAX_AGED_ADVANCES)
139
+ return false;
140
+ const started = currentHopStartedAtMs(question);
141
+ if (started === Number.NEGATIVE_INFINITY)
142
+ return false;
143
+ return nowMs - started >= ageWindowMs;
144
+ }
145
+ export function shouldAdvanceAudienceEnded(audience, liveness) {
146
+ if (audience.kind === "user")
147
+ return false;
148
+ if (audience.kind !== "session")
149
+ return false;
150
+ return liveness === "ended";
151
+ }
152
+ export function readTimeoutDeadline(hqRoot, laneId) {
153
+ const filePath = timeoutPath(hqRoot, laneId);
154
+ let text;
155
+ try {
156
+ text = fs.readFileSync(filePath, "utf8");
157
+ }
158
+ catch (err) {
159
+ const code = err.code;
160
+ if (code === "ENOENT")
161
+ return null;
162
+ throw err;
163
+ }
164
+ let parsed;
165
+ try {
166
+ parsed = JSON.parse(text);
167
+ }
168
+ catch (err) {
169
+ throw new LanesError("timeout_unreadable", `Timeout file for ${laneId} is not valid JSON: ${err instanceof Error ? err.message : String(err)}`, { lane_id: laneId, path: filePath });
170
+ }
171
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
172
+ throw new LanesError("timeout_unreadable", `Timeout file for ${laneId} is not an object`, { lane_id: laneId, path: filePath });
173
+ }
174
+ const deadline = parsed.deadline_at;
175
+ if (typeof deadline !== "string" || !Number.isFinite(Date.parse(deadline))) {
176
+ return null;
177
+ }
178
+ const timeoutSecs = parsed.timeout_secs;
179
+ return {
180
+ deadline_at: deadline,
181
+ timeout_secs: typeof timeoutSecs === "number" && Number.isInteger(timeoutSecs)
182
+ ? timeoutSecs
183
+ : undefined,
184
+ };
185
+ }
186
+ export function isDeadlinePassed(deadlineAt, nowMs) {
187
+ const at = Date.parse(deadlineAt);
188
+ if (!Number.isFinite(at))
189
+ return false;
190
+ return nowMs > at;
191
+ }
192
+ function readDirJsonIds(dir, label) {
193
+ let names;
194
+ try {
195
+ names = fs.readdirSync(dir);
196
+ }
197
+ catch (err) {
198
+ const code = err.code;
199
+ if (code === "ENOENT")
200
+ return [];
201
+ throw new LanesError("store_unreadable", `Failed to read ${label} directory ${dir}: ${err instanceof Error ? err.message : String(err)}`, { path: dir });
202
+ }
203
+ return names.filter((name) => typeof name === "string" &&
204
+ name.endsWith(".json") &&
205
+ !name.startsWith(".") &&
206
+ !name.includes(".conflict-"));
207
+ }
208
+ function loadLaneRecords(hqRoot, summary) {
209
+ const dir = path.join(lanesStoreRoot(hqRoot), "lanes");
210
+ const names = readDirJsonIds(dir, "lane");
211
+ const lanes = [];
212
+ for (const name of names) {
213
+ const filePath = path.join(dir, name);
214
+ const id = name.slice(0, -".json".length);
215
+ try {
216
+ const text = fs.readFileSync(filePath, "utf8");
217
+ const parsed = JSON.parse(text);
218
+ lanes.push(parseLaneRecord(parsed, filePath));
219
+ }
220
+ catch (err) {
221
+ markDirty(summary, {
222
+ kind: "lane",
223
+ id,
224
+ why: `unreadable: ${err instanceof Error ? err.message : String(err)}`,
225
+ });
226
+ }
227
+ }
228
+ return lanes;
229
+ }
230
+ function loadQuestionRecords(hqRoot, summary) {
231
+ const dir = path.join(lanesStoreRoot(hqRoot), "questions");
232
+ const names = readDirJsonIds(dir, "question");
233
+ const questions = [];
234
+ for (const name of names) {
235
+ const filePath = path.join(dir, name);
236
+ const id = name.slice(0, -".json".length);
237
+ try {
238
+ const text = fs.readFileSync(filePath, "utf8");
239
+ const parsed = JSON.parse(text);
240
+ questions.push(parseQuestionRecord(parsed, filePath));
241
+ }
242
+ catch (err) {
243
+ markDirty(summary, {
244
+ kind: "question",
245
+ id,
246
+ why: `unreadable: ${err instanceof Error ? err.message : String(err)}`,
247
+ });
248
+ }
249
+ }
250
+ return questions;
251
+ }
252
+ function defaultReadSessionSenior(hqRoot, sessionId) {
253
+ const filePath = path.join(hqRoot, "workspace", "sessions", sessionId, "meta.yaml");
254
+ let text;
255
+ try {
256
+ text = fs.readFileSync(filePath, "utf8");
257
+ }
258
+ catch (err) {
259
+ const code = err.code;
260
+ if (code === "ENOENT")
261
+ return { kind: "user" };
262
+ throw err;
263
+ }
264
+ const doc = parseUserYaml(text, filePath);
265
+ if (doc === null || typeof doc !== "object" || Array.isArray(doc)) {
266
+ return { kind: "user" };
267
+ }
268
+ const senior = doc.senior;
269
+ if (senior === "user" || senior === undefined || senior === null) {
270
+ return { kind: "user" };
271
+ }
272
+ if (typeof senior === "string") {
273
+ const colon = senior.indexOf(":");
274
+ if (colon <= 0)
275
+ return { kind: "user" };
276
+ const kind = senior.slice(0, colon);
277
+ const id = senior.slice(colon + 1);
278
+ if (kind === "session" || kind === "lane")
279
+ return { kind, id };
280
+ if (kind === "user")
281
+ return { kind: "user" };
282
+ }
283
+ if (typeof senior === "object" && !Array.isArray(senior)) {
284
+ const rec = senior;
285
+ if (rec.kind === "user")
286
+ return { kind: "user" };
287
+ if ((rec.kind === "session" || rec.kind === "lane") &&
288
+ typeof rec.id === "string" &&
289
+ rec.id.length > 0) {
290
+ return { kind: rec.kind, id: rec.id };
291
+ }
292
+ }
293
+ return { kind: "user" };
294
+ }
295
+ export function resolveQuestionAudience(question, readLaneSenior, readSessionSenior) {
296
+ let current = { kind: "lane", id: question.origin.id };
297
+ for (let hop = 0; hop < question.hop; hop += 1) {
298
+ if (current.kind === "user")
299
+ return current;
300
+ if (current.kind === "lane") {
301
+ if (!current.id)
302
+ return current;
303
+ current = readLaneSenior(current.id);
304
+ continue;
305
+ }
306
+ if (!current.id)
307
+ return current;
308
+ current = readSessionSenior(current.id);
309
+ }
310
+ return current;
311
+ }
312
+ function casLaneIf(hqRoot, laneId, shouldWrite, updater, dryRun) {
313
+ for (let i = 0; i < 5; i += 1) {
314
+ const current = readLane(hqRoot, laneId);
315
+ if (!shouldWrite(current))
316
+ return { changed: false, record: current };
317
+ if (dryRun)
318
+ return { changed: true, record: updater(current) };
319
+ try {
320
+ return {
321
+ changed: true,
322
+ record: updateLane(hqRoot, laneId, current.version, "reconcile", updater),
323
+ };
324
+ }
325
+ catch (err) {
326
+ if (!isLaneVersionConflict(err) || i === 4)
327
+ throw err;
328
+ }
329
+ }
330
+ throw new LanesError("version_conflict", `Exhausted CAS retries for lane ${laneId}`, { lane_id: laneId });
331
+ }
332
+ function laneStoryLedgerIsClosed(hqRoot, lane) {
333
+ const stories = laneStories(lane);
334
+ if (stories.length === 0)
335
+ return { closed: false, states: [] };
336
+ const states = [];
337
+ for (const projectId of new Set(stories.map((story) => story.project_id))) {
338
+ const projectStories = stories.filter((story) => story.project_id === projectId);
339
+ const ledgerStates = readProjectStoryStates(hqRoot, projectId, projectStories.map((story) => story.story_id), lane.company?.slug);
340
+ for (const story of projectStories) {
341
+ const state = ledgerStates.get(story.story_id);
342
+ if (state === undefined)
343
+ return { closed: false, states: [...states, "missing"] };
344
+ states.push(state);
345
+ }
346
+ }
347
+ return { closed: states.length > 0 && states.every((state) => state === "done" || state === "dropped"), states };
348
+ }
349
+ async function closeInterruptedDoneStoryWorkers(hqRoot, lanes, questions, summary, deps, now, enabledByCompany) {
350
+ if (deps.dryRun)
351
+ return;
352
+ const nowMs = now.getTime();
353
+ const at = now.toISOString();
354
+ for (const lane of lanes) {
355
+ if (lane.depth < 1 || lane.state !== "interrupted" || lane.keep_alive === true ||
356
+ !laneRunsHere(lane, deps.host) || !lane.interrupted_at ||
357
+ !Number.isFinite(Date.parse(lane.interrupted_at)) ||
358
+ nowMs - Date.parse(lane.interrupted_at) < INTERRUPTED_DONE_STORY_MIN_AGE_MS ||
359
+ hasPendingKeepAliveWork(hqRoot, lane, questions))
360
+ continue;
361
+ let company;
362
+ try {
363
+ company = resolveLaneCompany(hqRoot, lane).company;
364
+ }
365
+ catch (err) {
366
+ markDirty(summary, { kind: "lane", id: lane.lane_id, why: `story close company lookup failed: ${err instanceof Error ? err.message : String(err)}` });
367
+ continue;
368
+ }
369
+ if (!company || enabledByCompany.get(keepAliveCompanyKey(company)) !== true)
370
+ continue;
371
+ let ledger;
372
+ try {
373
+ ledger = laneStoryLedgerIsClosed(hqRoot, lane);
374
+ }
375
+ catch (err) {
376
+ markDirty(summary, { kind: "lane", id: lane.lane_id, why: `story close ledger read failed: ${err instanceof Error ? err.message : String(err)}` });
377
+ continue;
378
+ }
379
+ if (!ledger.closed)
380
+ continue;
381
+ try {
382
+ const result = casLaneIf(hqRoot, lane.lane_id, (current) => {
383
+ const currentCompany = resolveLaneCompany(hqRoot, current).company;
384
+ if (current.state !== "interrupted" || current.depth < 1 || current.keep_alive === true ||
385
+ !currentCompany || enabledByCompany.get(keepAliveCompanyKey(currentCompany)) !== true ||
386
+ !laneRunsHere(current, deps.host) || !current.interrupted_at ||
387
+ !Number.isFinite(Date.parse(current.interrupted_at)) ||
388
+ nowMs - Date.parse(current.interrupted_at) < INTERRUPTED_DONE_STORY_MIN_AGE_MS ||
389
+ hasPendingKeepAliveWork(hqRoot, current, questions))
390
+ return false;
391
+ return laneStoryLedgerIsClosed(hqRoot, current).closed;
392
+ }, (current) => ({
393
+ ...current,
394
+ state: "killed",
395
+ queued_reason: undefined,
396
+ queued_details: undefined,
397
+ updated_at: at,
398
+ terminal_reason: "story_closed",
399
+ terminal_details: { ...(current.terminal_details ?? {}), operator_reason: "story_closed" },
400
+ stop_history: [...(current.stop_history ?? []), { at, reason: "story_closed" }],
401
+ }), false);
402
+ if (!result.changed)
403
+ continue;
404
+ stopLaneProcesses(hqRoot, lane.lane_id, {
405
+ killProcess: deps.spawnLaneDeps?.killProcess,
406
+ isAlive: deps.isAlive,
407
+ });
408
+ for (const question of questions) {
409
+ if (question.origin.id !== lane.lane_id || question.status !== "pending")
410
+ continue;
411
+ casQuestionIf(hqRoot, question.question_id, (current) => current.status === "pending", (current) => ({
412
+ ...current,
413
+ status: "cancelled",
414
+ routing_history: appendCappedHistory(current.routing_history, {
415
+ hop: current.hop,
416
+ audience: { kind: "user" },
417
+ at,
418
+ reason: "cancelled",
419
+ }),
420
+ }), false);
421
+ }
422
+ appendWatcherEvent(watcherEventsPath(hqRoot, lane.lane_id), {
423
+ event: "story_closed",
424
+ at,
425
+ lane_id: lane.lane_id,
426
+ story_state: [...new Set(ledger.states)].join(","),
427
+ terminal_reason: "story_closed",
428
+ });
429
+ const mesh = deps.mesh === undefined ? createLaneMesh({ interactive: false }) : deps.mesh;
430
+ if (mesh) {
431
+ const timeoutMs = deps.meshEmitTimeoutMs ?? 10_000;
432
+ let timer;
433
+ try {
434
+ await Promise.race([
435
+ emitLaneTerminalStateEvent(mesh, result.record, "killed", "story_closed"),
436
+ new Promise((_resolve, reject) => {
437
+ timer = setTimeout(() => reject(new Error(`terminal mesh emit timed out after ${timeoutMs}ms`)), timeoutMs);
438
+ }),
439
+ ]);
440
+ }
441
+ catch (err) {
442
+ markDirty(summary, { kind: "lane", id: lane.lane_id, why: `story close mesh emit failed: ${err instanceof Error ? err.message : String(err)}` });
443
+ }
444
+ finally {
445
+ if (timer)
446
+ clearTimeout(timer);
447
+ }
448
+ }
449
+ }
450
+ catch (err) {
451
+ markDirty(summary, { kind: "lane", id: lane.lane_id, why: `story close failed: ${err instanceof Error ? err.message : String(err)}` });
452
+ }
453
+ }
454
+ }
455
+ function writeQueueReason(hqRoot, lane, reason, dryRun) {
456
+ if (lane.queued_reason === reason)
457
+ return;
458
+ casLaneIf(hqRoot, lane.lane_id, (current) => current.state === "queued" && current.queued_reason !== reason, (current) => ({ ...current, queued_reason: reason, queued_details: undefined, admission_reserved_at: undefined }), dryRun);
459
+ }
460
+ function availableBalancedAccount(hqRoot, lane, occupying, config, host, profileMaxConcurrent, nowMs) {
461
+ if (!lane.account || !lane.admission_accounts?.includes(lane.account))
462
+ return undefined;
463
+ if (laneAdmissionCapReason(occupying, lane, config, host, profileMaxConcurrent, nowMs) !== "account_cap") {
464
+ return undefined;
465
+ }
466
+ const pinnedModel = lane.model_source === undefined || lane.model_source === "cli" || lane.model_source === "preserved"
467
+ ? lane.model
468
+ : lane.model_source === "profile"
469
+ ? lane.model
470
+ : undefined;
471
+ for (const account of lane.admission_accounts) {
472
+ if (account === lane.account)
473
+ continue;
474
+ if (config.providers[lane.provider].accounts[account]?.enabled !== true)
475
+ continue;
476
+ const status = accountStatus(lane.provider, account, config, {
477
+ hostname: host.hostname,
478
+ existsSync: (file) => fs.existsSync(file),
479
+ });
480
+ if (!isCredentialProven(status))
481
+ continue;
482
+ try {
483
+ resolveAccount(lane.provider, account, config, {
484
+ hqRoot,
485
+ hostname: host.hostname,
486
+ ...(pinnedModel !== undefined ? { model: pinnedModel } : {}),
487
+ });
488
+ }
489
+ catch (err) {
490
+ if (err instanceof AccountResolutionError)
491
+ continue;
492
+ throw err;
493
+ }
494
+ const candidate = { ...lane, account };
495
+ if (laneAdmissionCapReason(occupying, candidate, config, host, profileMaxConcurrent, nowMs) === undefined) {
496
+ return account;
497
+ }
498
+ }
499
+ return undefined;
500
+ }
501
+ function writeAdmissionAccount(hqRoot, lane, account, dryRun) {
502
+ return casLaneIf(hqRoot, lane.lane_id, (current) => current.state === "queued" &&
503
+ current.account === lane.account &&
504
+ current.admission_accounts?.includes(account) === true, (current) => ({ ...current, account }), dryRun).record;
505
+ }
506
+ function casQuestionIf(hqRoot, questionId, shouldWrite, updater, dryRun) {
507
+ for (let i = 0; i < 5; i += 1) {
508
+ const current = readQuestion(hqRoot, questionId);
509
+ if (!shouldWrite(current))
510
+ return { changed: false, record: current };
511
+ if (dryRun)
512
+ return { changed: true, record: updater(current) };
513
+ try {
514
+ return {
515
+ changed: true,
516
+ record: updateQuestion(hqRoot, questionId, current.version, updater),
517
+ };
518
+ }
519
+ catch (err) {
520
+ if (!isLaneVersionConflict(err) || i === 4)
521
+ throw err;
522
+ }
523
+ }
524
+ throw new LanesError("version_conflict", `Exhausted CAS retries for question ${questionId}`, { question_id: questionId });
525
+ }
526
+ async function emitAgedQuestionAtUser(summary, deps, lane, question) {
527
+ if (deps.dryRun)
528
+ return;
529
+ const mesh = deps.mesh === undefined ? createLaneMesh({ interactive: false }) : deps.mesh;
530
+ const company = laneMeshCompany(lane);
531
+ if (!mesh || !company || !lane.thread_id)
532
+ return;
533
+ const timeoutMs = deps.meshEmitTimeoutMs ?? 10_000;
534
+ let timer;
535
+ try {
536
+ await Promise.race([
537
+ mesh.appendThreadEvent(company, lane.thread_id, "lane_question_at_user", { lane_id: lane.lane_id, question_id: question.question_id }),
538
+ new Promise((_resolve, reject) => {
539
+ timer = setTimeout(() => reject(new Error(`question-at-user mesh emit timed out after ${timeoutMs}ms`)), timeoutMs);
540
+ }),
541
+ ]);
542
+ }
543
+ catch (err) {
544
+ markDirty(summary, {
545
+ kind: "question",
546
+ id: question.question_id,
547
+ why: `question-at-user mesh emit failed: ${err instanceof Error ? err.message : String(err)}`,
548
+ });
549
+ }
550
+ finally {
551
+ if (timer)
552
+ clearTimeout(timer);
553
+ }
554
+ }
555
+ function pickupValidationPendingAtMs(lane) {
556
+ const raw = lane.pickup_validation_pending_since ?? lane.updated_at;
557
+ const at = Date.parse(raw);
558
+ return Number.isFinite(at) ? at : null;
559
+ }
560
+ function pickupValidationExpired(lane, nowMs) {
561
+ const pendingAtMs = pickupValidationPendingAtMs(lane);
562
+ return pendingAtMs === null || nowMs - pendingAtMs > PICKUP_VALIDATION_MAX_AGE_MS;
563
+ }
564
+ function expireAbandonedPickupValidation(hqRoot, lanes, summary, deps, now) {
565
+ const nowMs = now.getTime();
566
+ for (const lane of lanes) {
567
+ if (lane.state !== "queued" ||
568
+ lane.pickup_validation_pending !== true ||
569
+ !laneRunsHere(lane, deps.host) ||
570
+ !pickupValidationExpired(lane, nowMs)) {
571
+ continue;
572
+ }
573
+ try {
574
+ const result = casLaneIf(hqRoot, lane.lane_id, (current) => current.state === "queued" &&
575
+ current.pickup_validation_pending === true &&
576
+ pickupValidationExpired(current, nowMs), (current) => {
577
+ const pendingAtMs = pickupValidationPendingAtMs(current);
578
+ return {
579
+ ...current,
580
+ state: "failed",
581
+ queued_reason: undefined,
582
+ queued_details: undefined,
583
+ pickup_validation_pending: undefined,
584
+ pickup_validation_pending_since: undefined,
585
+ terminal_reason: PICKUP_VALIDATION_TIMEOUT_REASON,
586
+ terminal_details: {
587
+ pickup_validation_pending_since: current.pickup_validation_pending_since ?? current.updated_at,
588
+ age_ms: pendingAtMs === null ? null : Math.max(0, nowMs - pendingAtMs),
589
+ max_age_ms: PICKUP_VALIDATION_MAX_AGE_MS,
590
+ },
591
+ };
592
+ }, deps.dryRun);
593
+ if (result.changed) {
594
+ summary.aged.push({
595
+ id: lane.lane_id,
596
+ from: "queued",
597
+ to: "failed",
598
+ reason: PICKUP_VALIDATION_TIMEOUT_REASON,
599
+ });
600
+ }
601
+ }
602
+ catch (err) {
603
+ markDirty(summary, {
604
+ kind: "lane",
605
+ id: lane.lane_id,
606
+ why: `pickup validation expiry failed: ${err instanceof Error ? err.message : String(err)}`,
607
+ });
608
+ }
609
+ }
610
+ }
611
+ function fileNonEmpty(filePath) {
612
+ try {
613
+ return fs.statSync(filePath).size > 0;
614
+ }
615
+ catch (err) {
616
+ const code = err.code;
617
+ if (code === "ENOENT")
618
+ return false;
619
+ throw err;
620
+ }
621
+ }
622
+ function lastHostReportMs(lane) {
623
+ const raw = lane.last_host_report_at ?? lane.updated_at;
624
+ const ms = Date.parse(raw);
625
+ return Number.isFinite(ms) ? ms : 0;
626
+ }
627
+ function writeHeartbeatFile(hqRoot, summary, at) {
628
+ const filePath = reconcileHeartbeatPath(hqRoot);
629
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
630
+ const tmp = `${filePath}.${process.pid}.${Date.now()}.tmp`;
631
+ const body = `${JSON.stringify({ at, ok: summary.ok, action: "reconcile" }, null, 2)}\n`;
632
+ try {
633
+ fs.writeFileSync(tmp, body, { encoding: "utf8" });
634
+ fs.renameSync(tmp, filePath);
635
+ }
636
+ catch (err) {
637
+ try {
638
+ fs.unlinkSync(tmp);
639
+ }
640
+ catch (unlinkErr) {
641
+ const code = unlinkErr.code;
642
+ if (code !== "ENOENT") {
643
+ process.stderr.write(`hq lanes reconcile: unlink heartbeat tmp failed: ${unlinkErr instanceof Error ? unlinkErr.message : String(unlinkErr)}\n`);
644
+ }
645
+ }
646
+ throw err;
647
+ }
648
+ }
649
+ function writeAnsweredQuestions(hqRoot, lane, questions) {
650
+ const filePath = path.join(laneRunDir(hqRoot, lane.lane_id), ANSWERED_QUESTIONS_FILE);
651
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
652
+ fs.writeFileSync(filePath, `${JSON.stringify(answeredQuestionEntries(hqRoot, lane, questions), null, 2)}\n`, "utf8");
653
+ }
654
+ async function withReconcileLock(hqRoot, fn) {
655
+ const lockPath = path.join(hqRoot, "workspace", "lanes-runs", ".admission-locks", "reconcile.lock");
656
+ fs.mkdirSync(path.dirname(lockPath), { recursive: true });
657
+ const started = Date.now();
658
+ while (true) {
659
+ let fd;
660
+ try {
661
+ fd = fs.openSync(lockPath, "wx");
662
+ try {
663
+ fs.writeSync(fd, Buffer.from(`${process.pid}\n${Date.now()}\n`, "utf8"));
664
+ }
665
+ catch (err) {
666
+ fs.closeSync(fd);
667
+ fd = undefined;
668
+ throw err;
669
+ }
670
+ try {
671
+ return await fn();
672
+ }
673
+ finally {
674
+ try {
675
+ fs.closeSync(fd);
676
+ }
677
+ catch (err) {
678
+ process.stderr.write(`hq lanes reconcile: lock close failed: ${err instanceof Error ? err.message : String(err)}\n`);
679
+ }
680
+ try {
681
+ fs.unlinkSync(lockPath);
682
+ }
683
+ catch (err) {
684
+ const code = err.code;
685
+ if (code !== "ENOENT") {
686
+ process.stderr.write(`hq lanes reconcile: lock unlink failed: ${err instanceof Error ? err.message : String(err)}\n`);
687
+ }
688
+ }
689
+ }
690
+ }
691
+ catch (err) {
692
+ const code = err.code;
693
+ if (code !== "EEXIST")
694
+ throw err;
695
+ if (Date.now() - started > RECONCILE_LOCK_TIMEOUT_MS) {
696
+ throw new LanesError("store_lock_timeout", `Timed out waiting for reconcile lock ${lockPath}`, { path: lockPath });
697
+ }
698
+ try {
699
+ const raw = fs.readFileSync(lockPath, "utf8");
700
+ const tsLine = raw.split("\n")[1];
701
+ const ts = tsLine ? Number(tsLine) : NaN;
702
+ if (Number.isFinite(ts) && Date.now() - ts > RECONCILE_LOCK_STALE_MS) {
703
+ try {
704
+ fs.unlinkSync(lockPath);
705
+ }
706
+ catch (unlinkErr) {
707
+ const unlinkCode = unlinkErr.code;
708
+ if (unlinkCode !== "ENOENT")
709
+ throw unlinkErr;
710
+ }
711
+ continue;
712
+ }
713
+ }
714
+ catch (statErr) {
715
+ const statCode = statErr.code;
716
+ if (statCode !== "ENOENT") {
717
+ process.stderr.write(`hq lanes reconcile: stale-lock probe failed: ${statErr instanceof Error ? statErr.message : String(statErr)}\n`);
718
+ }
719
+ }
720
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 10);
721
+ }
722
+ }
723
+ }
724
+ function classifyDeadLocalLane(hqRoot, laneId, now) {
725
+ const runDir = laneRunDir(hqRoot, laneId);
726
+ const manifest = readSpawnManifest(runDir);
727
+ const minEnvelopeName = manifest.envelope_path ? path.basename(manifest.envelope_path) : undefined;
728
+ const latest = readLatestEnvelopeRecord(hqRoot, laneId, minEnvelopeName);
729
+ const envelope = latest?.envelope ?? null;
730
+ const hasPartialOutput = [
731
+ path.join(runDir, "logs", "worker.out"),
732
+ path.join(runDir, "logs", "worker.err"),
733
+ path.join(runDir, "logs", "last-message.txt"),
734
+ ].some((file) => fileNonEmpty(file));
735
+ const lane = readLane(hqRoot, laneId);
736
+ // The same routing as the watcher's exit path (watcher.ts): a provider
737
+ // failure on the stream is read first, a 429 or quota-worded one becomes
738
+ // the quota signal, any other becomes provider_rejected, and only a stream
739
+ // with no failure object falls back to the quota reader.
740
+ const workerOutOffset = readSpawnManifest(runDir).worker_out_offset ?? 0;
741
+ const routed = envelope
742
+ ? { quota: null, rejection: null }
743
+ : routeProviderFailure(readProviderRejection(runDir, lane.provider, workerOutOffset), () => readQuotaSignal(runDir, lane.model, now, workerOutOffset), lane.model, now);
744
+ // As in the watcher: a reset-less 429 gets the provider's default hold.
745
+ const quota = routed.quota
746
+ ? applyDefaultQuotaHold(routed.quota, providerQuotaDefaultHoldMs(loadLanesConfig(hqRoot).providers[lane.provider]), now)
747
+ : null;
748
+ return {
749
+ classification: classifyLaneExit({
750
+ envelope,
751
+ hasPartialOutput,
752
+ quota,
753
+ providerRejection: routed.rejection,
754
+ }),
755
+ latest,
756
+ quota,
757
+ providerRejection: routed.rejection,
758
+ lane,
759
+ };
760
+ }
761
+ function runawayGuardCompanyKey(company) {
762
+ return company
763
+ ? `${company.uid ?? ""}\u0000${company.slug ?? ""}`
764
+ : "unresolved-company";
765
+ }
766
+ function reportRunawayGuardError(summary, stage, error) {
767
+ summary.ok = false;
768
+ summary.errors.push({
769
+ code: `runaway_tool_guard_${stage}_failed`,
770
+ message: `runaway search guard ${stage} failed; no additional search processes were stopped`,
771
+ });
772
+ process.stderr.write(`${JSON.stringify({
773
+ event: "lanes.runaway_tool_guard.failed",
774
+ stage,
775
+ error: error instanceof Error ? error.name : "UnknownError",
776
+ fallback: "continue_without_argv",
777
+ })}\n`);
778
+ }
779
+ async function runRunawayToolGuardPass(hqRoot, lanes, config, deps, now, summary) {
780
+ if (deps.dryRun)
781
+ return;
782
+ const localRunningLanes = lanes.filter((lane) => lane.state === "running" && laneRunsHere(lane, deps.host));
783
+ if (localRunningLanes.length === 0)
784
+ return;
785
+ const groups = new Map();
786
+ for (const lane of localRunningLanes) {
787
+ let worker;
788
+ try {
789
+ worker = readPidRecord(path.join(laneRunDir(hqRoot, lane.lane_id), "worker.pid"));
790
+ if (!worker ||
791
+ worker.starttime === undefined ||
792
+ !isPidRecordAlive(worker, deps.isAlive))
793
+ continue;
794
+ }
795
+ catch (error) {
796
+ reportRunawayGuardError(summary, "worker_pid_read", error);
797
+ continue;
798
+ }
799
+ let company;
800
+ try {
801
+ company = resolveLaneCompany(hqRoot, lane).company;
802
+ }
803
+ catch (error) {
804
+ reportRunawayGuardError(summary, "company_resolution", error);
805
+ continue;
806
+ }
807
+ const key = runawayGuardCompanyKey(company);
808
+ const group = groups.get(key) ?? { company, lanes: [] };
809
+ group.lanes.push({ laneId: lane.lane_id, agentPid: worker.pid, agentStarttime: worker.starttime });
810
+ groups.set(key, group);
811
+ }
812
+ if (groups.size === 0)
813
+ return;
814
+ const readRegistryFlag = deps.readRegistryFlag ?? readLanesRegistryFlag;
815
+ const enabledGroups = new Set();
816
+ const env = deps.env ?? process.env;
817
+ await Promise.all([...groups.entries()].map(async ([key, group]) => {
818
+ try {
819
+ const enabled = await readRegistryFlag(RUNAWAY_TOOL_GUARD_FLAG, {
820
+ company: group.company ?? undefined,
821
+ env,
822
+ });
823
+ if (enabled)
824
+ enabledGroups.add(key);
825
+ }
826
+ catch (error) {
827
+ reportRunawayGuardError(summary, "flag_read", error);
828
+ }
829
+ }));
830
+ if (enabledGroups.size === 0)
831
+ return;
832
+ const laneProcesses = [...groups.entries()]
833
+ .filter(([key]) => enabledGroups.has(key))
834
+ .flatMap(([, group]) => group.lanes);
835
+ if (laneProcesses.length === 0)
836
+ return;
837
+ let processes;
838
+ try {
839
+ processes = (deps.runawayToolProcessLister ?? listHostProcesses)();
840
+ }
841
+ catch (error) {
842
+ reportRunawayGuardError(summary, "process_table_read", error);
843
+ return;
844
+ }
845
+ const runawayConfig = config.runaway_search_guard ?? DEFAULT_RUNAWAY_SEARCH_GUARD_CONFIG;
846
+ try {
847
+ runRunawayToolGuard({
848
+ hqRoot,
849
+ lanes: laneProcesses,
850
+ processes,
851
+ config: runawayConfig,
852
+ now,
853
+ currentUid: process.getuid?.() ?? -1,
854
+ isCurrentProcess: deps.runawayToolProcessIsCurrent ??
855
+ ((pid, expectedStarttime) => readProcStarttime(pid) === expectedStarttime),
856
+ signal: deps.runawayToolProcessSignal ?? ((pid, signal) => process.kill(pid, signal)),
857
+ recordEvent: (laneId, event) => appendWatcherEvent(watcherEventsPath(hqRoot, laneId), event, deps.runawayToolEventAppend),
858
+ notifyLane: (laneId, text) => appendDropboxMessage(hqRoot, laneId, {
859
+ at: now.toISOString(),
860
+ message_id: `runaway-search-guard:${laneId}:${now.getTime()}`,
861
+ kind: "system_notice",
862
+ text,
863
+ }),
864
+ reportError: (stage, errorName) => {
865
+ summary.ok = false;
866
+ summary.errors.push({
867
+ code: `runaway_tool_guard_${stage}_failed`,
868
+ message: `runaway search guard could not ${stage} a stopped process (${errorName})`,
869
+ });
870
+ process.stderr.write(`${JSON.stringify({
871
+ event: "lanes.runaway_tool_guard.failed",
872
+ stage,
873
+ error: errorName,
874
+ fallback: "continue_without_argv",
875
+ })}\n`);
876
+ },
877
+ });
878
+ }
879
+ catch (error) {
880
+ reportRunawayGuardError(summary, "execution", error);
881
+ }
882
+ }
883
+ /** Unlocked pass body. `runReconcilePass` takes the process lock around this. */
884
+ export async function runPassBody(hqRoot, deps) {
885
+ const now = deps.now();
886
+ const nowMs = now.getTime();
887
+ const nowIso = now.toISOString();
888
+ const summary = emptySummary(deps.dryRun);
889
+ const livenessDeps = deps.livenessDeps ?? defaultLivenessDeps();
890
+ let lanes;
891
+ let questions;
892
+ try {
893
+ lanes = loadLaneRecords(hqRoot, summary);
894
+ questions = loadQuestionRecords(hqRoot, summary);
895
+ }
896
+ catch (err) {
897
+ markError(summary, err);
898
+ return summary;
899
+ }
900
+ if (deps.terminalKeepAliveCandidate) {
901
+ return runTerminalKeepAliveCandidatePass(hqRoot, lanes, summary, deps, now, deps.terminalKeepAliveCandidate);
902
+ }
903
+ let presenceConnected;
904
+ try {
905
+ presenceConnected =
906
+ deps.presenceConnected ?? readDaemonPresenceConnected(livenessDeps);
907
+ }
908
+ catch (err) {
909
+ markError(summary, err);
910
+ return summary;
911
+ }
912
+ let livenessIndex;
913
+ try {
914
+ livenessIndex = loadLivenessIndex(hqRoot, undefined, livenessDeps, !deps.dryRun);
915
+ }
916
+ catch (err) {
917
+ markError(summary, err);
918
+ return summary;
919
+ }
920
+ const config = loadLanesConfig(hqRoot);
921
+ try {
922
+ const companyRefs = new Map();
923
+ for (const lane of lanes) {
924
+ const company = resolveLaneCompany(hqRoot, lane).company;
925
+ if (company)
926
+ companyRefs.set(company.uid ?? company.slug, company);
927
+ }
928
+ const readFlag = deps.readRegistryFlag ?? readLanesRegistryFlag;
929
+ const homes = configuredCodexRolloutHomes(hqRoot, config, deps.host.hostname);
930
+ let retentionEnabled = false;
931
+ for (const company of homes.length > 0 ? companyRefs.values() : []) {
932
+ retentionEnabled ||= await readFlag("lanes.codex-rollout-retention", { company });
933
+ if (retentionEnabled)
934
+ break;
935
+ }
936
+ if (retentionEnabled) {
937
+ const statePath = path.join(hqRoot, "workspace", "lanes-runs", `.rollout-retention-${deps.host.hostname.replaceAll(/[^a-zA-Z0-9_-]/g, "_")}.json`);
938
+ let lastRunAt;
939
+ try {
940
+ const state = JSON.parse(fs.readFileSync(statePath, "utf8"));
941
+ if (typeof state.last_run_at !== "string" || !Number.isFinite(Date.parse(state.last_run_at))) {
942
+ throw new LanesError("rollout_retention_state_invalid", `Invalid rollout-retention state at ${statePath}`, { path: statePath });
943
+ }
944
+ lastRunAt = Date.parse(state.last_run_at);
945
+ }
946
+ catch (err) {
947
+ if (err.code !== "ENOENT")
948
+ throw err;
949
+ }
950
+ const lowDisk = homes.some((home) => checkLaneDiskSpace(hqRoot, home, deps.spawnLaneDeps?.statfs, config.low_disk_min_free_bytes).low);
951
+ if (lowDisk || lastRunAt === undefined || nowMs - lastRunAt >= 60 * 60 * 1000) {
952
+ const result = await retainCodexRollouts({
953
+ homes,
954
+ lanes,
955
+ nowMs,
956
+ compressAfterMs: config.codex_rollout_compress_after_hours * 60 * 60 * 1000,
957
+ deleteAfterMs: config.codex_rollout_delete_after_days * 24 * 60 * 60 * 1000,
958
+ });
959
+ summary.rollout_retention = result;
960
+ fs.mkdirSync(path.dirname(statePath), { recursive: true });
961
+ const tempPath = `${statePath}.${process.pid}.tmp`;
962
+ fs.writeFileSync(tempPath, `${JSON.stringify({ last_run_at: nowIso }, null, 2)}\n`, { flag: "wx" });
963
+ fs.renameSync(tempPath, statePath);
964
+ }
965
+ }
966
+ }
967
+ catch (err) {
968
+ markDirty(summary, { kind: "store", why: `Codex rollout retention failed: ${err instanceof Error ? err.message : String(err)}` });
969
+ }
970
+ const laneById = new Map(lanes.map((lane) => [lane.lane_id, lane]));
971
+ const readLaneSenior = (laneId) => {
972
+ const lane = laneById.get(laneId);
973
+ if (lane)
974
+ return lane.senior;
975
+ try {
976
+ return readLane(hqRoot, laneId).senior;
977
+ }
978
+ catch (err) {
979
+ throw new LanesError("lane_not_found", `Cannot resolve senior for missing lane ${laneId}: ${err instanceof Error ? err.message : String(err)}`, { lane_id: laneId });
980
+ }
981
+ };
982
+ const readSessionSenior = deps.readSessionSenior ??
983
+ ((sessionId) => defaultReadSessionSenior(hqRoot, sessionId));
984
+ const configuredClaudeHomes = Object.values(config.providers.claude.accounts).map((account) => effectiveAccountHome(account, deps.host.hostname).home);
985
+ const sessionStateCache = new Map();
986
+ const sessionState = (sessionId) => {
987
+ if (sessionStateCache.has(sessionId)) {
988
+ return sessionStateCache.get(sessionId) ?? null;
989
+ }
990
+ const entry = livenessIndex.sessions[sessionId];
991
+ if (!entry) {
992
+ const state = unindexedClaudeSessionLiveness(sessionId, nowMs, config.liveness, livenessDeps, configuredClaudeHomes);
993
+ sessionStateCache.set(sessionId, state);
994
+ return state;
995
+ }
996
+ const state = classifySessionLiveness(entry, nowMs, presenceConnected, config.liveness);
997
+ sessionStateCache.set(sessionId, state);
998
+ return state;
999
+ };
1000
+ const questionAgeWindowMs = (audience) => audience.kind === "session"
1001
+ ? config.session_question_timeout_secs * 1000
1002
+ : QUESTION_AGE_MS;
1003
+ if (deps.afterLoad)
1004
+ await deps.afterLoad();
1005
+ await runRunawayToolGuardPass(hqRoot, lanes, config, deps, now, summary);
1006
+ const startDeps = deps;
1007
+ markOverdueLanes(hqRoot, lanes, summary, deps, now);
1008
+ await heartbeatAndDeadHosts(hqRoot, lanes, summary, startDeps, now);
1009
+ for (const question of questions) {
1010
+ if (question.status !== "pending")
1011
+ continue;
1012
+ let audience;
1013
+ try {
1014
+ audience = resolveQuestionAudience(question, readLaneSenior, readSessionSenior);
1015
+ }
1016
+ catch (err) {
1017
+ markDirty(summary, {
1018
+ kind: "question",
1019
+ id: question.question_id,
1020
+ why: `audience unreadable: ${err instanceof Error ? err.message : String(err)}`,
1021
+ });
1022
+ continue;
1023
+ }
1024
+ try {
1025
+ let liveness;
1026
+ try {
1027
+ liveness =
1028
+ audience.kind === "session" && audience.id
1029
+ ? sessionState(audience.id)
1030
+ : audience.kind === "user"
1031
+ ? "live"
1032
+ : null;
1033
+ }
1034
+ catch (err) {
1035
+ // A failed metadata probe cannot prove that the session ended, but it
1036
+ // must not suppress the independent unanswered-timeout rule below.
1037
+ markError(summary, err);
1038
+ liveness = null;
1039
+ }
1040
+ if (shouldAdvanceAudienceEnded(audience, liveness)) {
1041
+ const nextHop = question.hop + 1;
1042
+ const nextAudience = resolveQuestionAudience({ ...question, hop: nextHop }, readLaneSenior, readSessionSenior);
1043
+ const result = casQuestionIf(hqRoot, question.question_id, (current) => current.status === "pending" && current.hop === question.hop, (current) => {
1044
+ const entry = {
1045
+ hop: nextHop,
1046
+ audience: nextAudience,
1047
+ at: nowIso,
1048
+ reason: "audience_ended",
1049
+ };
1050
+ return {
1051
+ ...current,
1052
+ hop: nextHop,
1053
+ routing_history: appendCappedHistory(current.routing_history, entry),
1054
+ };
1055
+ }, deps.dryRun);
1056
+ if (result.changed) {
1057
+ summary.advanced.push({
1058
+ id: question.question_id,
1059
+ from: String(question.hop),
1060
+ to: String(nextHop),
1061
+ reason: "audience_ended",
1062
+ });
1063
+ }
1064
+ continue;
1065
+ }
1066
+ if (shouldAgeQuestion(question, audience, nowMs, questionAgeWindowMs(audience))) {
1067
+ const nextHop = question.hop + 1;
1068
+ const nextAudience = resolveQuestionAudience({ ...question, hop: nextHop }, readLaneSenior, readSessionSenior);
1069
+ const result = casQuestionIf(hqRoot, question.question_id, (current) => {
1070
+ if (current.status !== "pending" ||
1071
+ agedAdvanceCount(current) >= MAX_AGED_ADVANCES) {
1072
+ return false;
1073
+ }
1074
+ const currentAudience = resolveQuestionAudience(current, readLaneSenior, readSessionSenior);
1075
+ return shouldAgeQuestion(current, currentAudience, nowMs, questionAgeWindowMs(currentAudience));
1076
+ }, (current) => {
1077
+ const entry = {
1078
+ hop: current.hop + 1,
1079
+ audience: nextAudience,
1080
+ at: nowIso,
1081
+ reason: "unanswered_timeout",
1082
+ };
1083
+ return {
1084
+ ...current,
1085
+ hop: current.hop + 1,
1086
+ routing_history: appendCappedHistory(current.routing_history, entry),
1087
+ };
1088
+ }, deps.dryRun);
1089
+ if (result.changed) {
1090
+ summary.aged.push({
1091
+ id: question.question_id,
1092
+ from: String(question.hop),
1093
+ to: String(nextHop),
1094
+ reason: "unanswered_timeout",
1095
+ });
1096
+ if (nextAudience.kind === "user") {
1097
+ const originLane = laneById.get(question.lane_id) ?? readLane(hqRoot, question.lane_id);
1098
+ await emitAgedQuestionAtUser(summary, deps, originLane, result.record);
1099
+ }
1100
+ }
1101
+ }
1102
+ }
1103
+ catch (err) {
1104
+ markDirty(summary, {
1105
+ kind: "question",
1106
+ id: question.question_id,
1107
+ why: `advance failed: ${err instanceof LanesError ? `${err.error}: ` : ""}${err instanceof Error ? err.message : String(err)}`,
1108
+ });
1109
+ }
1110
+ }
1111
+ const readRegistryFlag = deps.readRegistryFlag ?? readLanesRegistryFlag;
1112
+ const storyCloseCompanies = new Map();
1113
+ for (const lane of lanes) {
1114
+ if (lane.depth < 1 || lane.state !== "interrupted" || lane.keep_alive === true ||
1115
+ !laneRunsHere(lane, deps.host) || !lane.interrupted_at ||
1116
+ !Number.isFinite(Date.parse(lane.interrupted_at)) ||
1117
+ nowMs - Date.parse(lane.interrupted_at) < INTERRUPTED_DONE_STORY_MIN_AGE_MS ||
1118
+ hasPendingKeepAliveWork(hqRoot, lane, questions) || laneStories(lane).length === 0)
1119
+ continue;
1120
+ try {
1121
+ const company = resolveLaneCompany(hqRoot, lane).company;
1122
+ if (company)
1123
+ storyCloseCompanies.set(keepAliveCompanyKey(company), company);
1124
+ }
1125
+ catch (err) {
1126
+ markDirty(summary, { kind: "lane", id: lane.lane_id, why: `story close company lookup failed: ${err instanceof Error ? err.message : String(err)}` });
1127
+ }
1128
+ }
1129
+ const storyCloseEnabledByCompany = new Map();
1130
+ const env = deps.env ?? process.env;
1131
+ await Promise.all([...storyCloseCompanies.entries()].map(async ([key, company]) => {
1132
+ try {
1133
+ storyCloseEnabledByCompany.set(key, await readRegistryFlag(CLOSE_INTERRUPTED_DONE_STORY_WORKERS_FLAG, {
1134
+ company: company ?? undefined,
1135
+ env,
1136
+ }));
1137
+ }
1138
+ catch (err) {
1139
+ storyCloseEnabledByCompany.set(key, false);
1140
+ markDirty(summary, { kind: "lane", why: `story close flag read failed for ${company?.slug ?? "unresolved company"}: ${err instanceof Error ? err.message : String(err)}` });
1141
+ }
1142
+ }));
1143
+ const keepAliveFlagCompanies = new Map();
1144
+ for (const lane of lanes) {
1145
+ if (!(lane.state === "done" || lane.state === "interrupted" || lane.state === "blocked" || lane.state === "killed" || lane.state === "cancelled" || lane.state === "failed") ||
1146
+ (lane.keep_alive !== true && lane.worker_profile === undefined) ||
1147
+ (lane.state === "blocked" && lane.keep_alive !== true) ||
1148
+ !laneRunsHere(lane, deps.host) ||
1149
+ (lane.state === "done" && (lane.terminal_reason === PROVIDER_REJECTED_REASON ||
1150
+ lane.terminal_reason === "account_not_logged_in" ||
1151
+ !providerSupportsSessionResume(lane.provider) ||
1152
+ !(lane.provider_session_id ?? lane.last_resume?.session_id))))
1153
+ continue;
1154
+ if (lane.state === "done") {
1155
+ const envelope = readLatestEnvelopeRecord(hqRoot, lane.lane_id)?.envelope;
1156
+ if (!envelope || envelope.decision !== "done")
1157
+ continue;
1158
+ const hasQueuedWork = lane.keep_alive_standing === true && hasPendingKeepAliveWork(hqRoot, lane, questions);
1159
+ if (doneKeepAliveVerdict(lane, envelope, hasQueuedWork, {
1160
+ hasPendingWork: hasPendingKeepAliveWork(hqRoot, lane, questions),
1161
+ nowMs: now.getTime(),
1162
+ nextCheckAnchorMs: Date.parse(lane.last_envelope?.at ?? lane.updated_at),
1163
+ }) !== "eligible")
1164
+ continue;
1165
+ }
1166
+ const company = resolveLaneCompany(hqRoot, lane).company;
1167
+ if (company === null)
1168
+ continue;
1169
+ const key = keepAliveCompanyKey(company);
1170
+ keepAliveFlagCompanies.set(key, company);
1171
+ }
1172
+ const keepAliveFlagByCompany = new Map();
1173
+ const blockedResumeFlagByCompany = new Map();
1174
+ const immediateFirstResumeFlagByCompany = new Map();
1175
+ const supersessionLineageAwareByCompany = new Map();
1176
+ await Promise.all([...keepAliveFlagCompanies.entries()].map(async ([key, company]) => {
1177
+ const options = {
1178
+ company: company ?? undefined,
1179
+ env,
1180
+ };
1181
+ const hasBlockedKeepAliveLane = lanes.some((lane) => lane.state === "blocked" &&
1182
+ lane.keep_alive === true &&
1183
+ laneRunsHere(lane, startDeps.host) &&
1184
+ keepAliveCompanyKey(resolveLaneCompany(hqRoot, lane).company) === key);
1185
+ const [keepAliveEnabled, lineageAware, blockedResumeEnabled, immediateFirstResumeEnabled] = await Promise.all([
1186
+ readRegistryFlag(LEAD_LANE_KEEP_ALIVE_FLAG, options),
1187
+ readRegistryFlag(LANE_SUPERSESSION_LINEAGE_AWARE_FLAG, options),
1188
+ hasBlockedKeepAliveLane
1189
+ ? readRegistryFlag(LEAD_LANE_KEEP_ALIVE_RESUME_BLOCKED_FLAG, options)
1190
+ : Promise.resolve(false),
1191
+ readRegistryFlag(LEAD_LANE_KEEP_ALIVE_IMMEDIATE_FIRST_RESUME_FLAG, options),
1192
+ ]);
1193
+ keepAliveFlagByCompany.set(key, keepAliveEnabled);
1194
+ blockedResumeFlagByCompany.set(key, blockedResumeEnabled);
1195
+ immediateFirstResumeFlagByCompany.set(key, immediateFirstResumeEnabled);
1196
+ supersessionLineageAwareByCompany.set(key, lineageAware);
1197
+ }));
1198
+ const admissionThrottleDeps = startDeps.throttleDeps ?? defaultThrottleDeps(startDeps.env ?? process.env);
1199
+ const hasAdmissionCandidates = lanes.some((lane) => laneRunsHere(lane, startDeps.host) &&
1200
+ (lane.state === "queued" || lane.state === "awaiting_input")) || [...keepAliveFlagByCompany.values()].some(Boolean);
1201
+ const priorThrottleState = readThrottleState(throttleStatePath(hqRoot), admissionThrottleDeps);
1202
+ const priorThrottleSnapshot = priorThrottleState === "missing" || priorThrottleState === "unreadable"
1203
+ ? undefined
1204
+ : priorThrottleState.cpu_snapshot;
1205
+ const cpuObservation = await sampleCpuBusyObservation(admissionThrottleDeps, priorThrottleSnapshot, { measureBusy: hasAdmissionCandidates });
1206
+ const admissionDeps = {
1207
+ ...startDeps,
1208
+ cpuBusyFraction: cpuObservation.busy,
1209
+ cpuObservation,
1210
+ skipAdmissionWithoutCpuSample: !hasAdmissionCandidates,
1211
+ };
1212
+ await withAllLaneAdmissionLocks(hqRoot, async () => {
1213
+ const freshLanes = loadLaneRecords(hqRoot, summary);
1214
+ const freshQuestions = loadQuestionRecords(hqRoot, summary);
1215
+ await closeInterruptedDoneStoryWorkers(hqRoot, freshLanes, freshQuestions, summary, admissionDeps, now, storyCloseEnabledByCompany);
1216
+ await resumeKeepAlive(hqRoot, freshLanes, freshQuestions, summary, admissionDeps, now, keepAliveFlagByCompany, supersessionLineageAwareByCompany, blockedResumeFlagByCompany, immediateFirstResumeFlagByCompany, deps.terminalKeepAliveCandidate);
1217
+ const drainLanes = loadLaneRecords(hqRoot, summary);
1218
+ await admitQueued(hqRoot, drainLanes, summary, admissionDeps, now);
1219
+ await resumeAwaiting(hqRoot, drainLanes, freshQuestions, summary, admissionDeps);
1220
+ });
1221
+ if (!deps.dryRun && cpuObservation.snapshot !== undefined) {
1222
+ const latestThrottleState = readThrottleState(throttleStatePath(hqRoot), admissionThrottleDeps);
1223
+ if (latestThrottleState === "unreadable") {
1224
+ markError(summary, new LanesError("throttle_state_unreadable", "Throttle state became unreadable while reconciling"));
1225
+ }
1226
+ else {
1227
+ const currentThrottleState = latestThrottleState === "missing"
1228
+ ? emptyThrottleState()
1229
+ : latestThrottleState;
1230
+ writeThrottleState(throttleStatePath(hqRoot), recordCpuBusyObservation(currentThrottleState, cpuObservation), admissionThrottleDeps);
1231
+ }
1232
+ }
1233
+ if (!deps.dryRun && summary.errors.length === 0) {
1234
+ try {
1235
+ writeHeartbeatFile(hqRoot, summary, nowIso);
1236
+ summary.heartbeat_at = nowIso;
1237
+ }
1238
+ catch (err) {
1239
+ markError(summary, err);
1240
+ }
1241
+ }
1242
+ return summary;
1243
+ }
1244
+ function markOverdueLanes(hqRoot, lanes, summary, deps, now) {
1245
+ const nowMs = now.getTime();
1246
+ const nowIso = now.toISOString();
1247
+ for (const lane of lanes) {
1248
+ if (lane.overdue === true)
1249
+ continue;
1250
+ if (!laneRunsHere(lane, deps.host))
1251
+ continue;
1252
+ let deadline;
1253
+ try {
1254
+ deadline = readTimeoutDeadline(hqRoot, lane.lane_id);
1255
+ }
1256
+ catch (err) {
1257
+ markDirty(summary, {
1258
+ kind: "lane",
1259
+ id: lane.lane_id,
1260
+ why: `timeout unreadable: ${err instanceof Error ? err.message : String(err)}`,
1261
+ });
1262
+ continue;
1263
+ }
1264
+ if (!deadline || !isDeadlinePassed(deadline.deadline_at, nowMs))
1265
+ continue;
1266
+ try {
1267
+ const result = casLaneIf(hqRoot, lane.lane_id, (current) => current.overdue !== true, (current) => ({
1268
+ ...current,
1269
+ overdue: true,
1270
+ overdue_at: nowIso,
1271
+ }), deps.dryRun);
1272
+ if (result.changed) {
1273
+ summary.overdue.push({
1274
+ id: lane.lane_id,
1275
+ from: "ok",
1276
+ to: "overdue",
1277
+ });
1278
+ }
1279
+ }
1280
+ catch (err) {
1281
+ markDirty(summary, {
1282
+ kind: "lane",
1283
+ id: lane.lane_id,
1284
+ why: `overdue CAS failed: ${err instanceof Error ? err.message : String(err)}`,
1285
+ });
1286
+ }
1287
+ }
1288
+ }
1289
+ async function heartbeatAndDeadHosts(hqRoot, lanes, summary, deps, now) {
1290
+ const nowMs = now.getTime();
1291
+ const nowIso = now.toISOString();
1292
+ const isAlive = deps.isAlive ?? isPidAlive;
1293
+ for (const lane of lanes) {
1294
+ if (lane.state !== "running")
1295
+ continue;
1296
+ const local = laneRunsHere(lane, deps.host);
1297
+ if (local) {
1298
+ const runDir = laneRunDir(hqRoot, lane.lane_id);
1299
+ let workerPid;
1300
+ let watcherPid;
1301
+ let workerAlive;
1302
+ let watcherAlive;
1303
+ try {
1304
+ const workerRec = readPidRecord(path.join(runDir, "worker.pid"));
1305
+ const watcherRec = readPidRecord(path.join(runDir, "watcher.pid"));
1306
+ workerPid = workerRec?.pid ?? null;
1307
+ watcherPid = watcherRec?.pid ?? null;
1308
+ workerAlive = workerRec !== null && isPidRecordAlive(workerRec, isAlive);
1309
+ watcherAlive = watcherRec !== null && isPidRecordAlive(watcherRec, isAlive);
1310
+ }
1311
+ catch (err) {
1312
+ markDirty(summary, {
1313
+ kind: "lane",
1314
+ id: lane.lane_id,
1315
+ why: `pid file unreadable: ${err instanceof Error ? err.message : String(err)}`,
1316
+ });
1317
+ continue;
1318
+ }
1319
+ if (workerAlive) {
1320
+ try {
1321
+ const result = casLaneIf(hqRoot, lane.lane_id, (current) => current.last_host_report_at !== nowIso, (current) => ({ ...current, last_host_report_at: nowIso }), deps.dryRun);
1322
+ if (result.changed)
1323
+ summary.heartbeats.push(lane.lane_id);
1324
+ }
1325
+ catch (err) {
1326
+ markDirty(summary, {
1327
+ kind: "lane",
1328
+ id: lane.lane_id,
1329
+ why: `heartbeat CAS failed: ${err instanceof Error ? err.message : String(err)}`,
1330
+ });
1331
+ }
1332
+ if (!watcherAlive && !deps.dryRun) {
1333
+ try {
1334
+ armLaneWatcher(hqRoot, lane.lane_id, spawnDepsForHost(deps));
1335
+ summary.heartbeats.push(`${lane.lane_id}:watcher-rearmed`);
1336
+ }
1337
+ catch (err) {
1338
+ markDirty(summary, {
1339
+ kind: "lane",
1340
+ id: lane.lane_id,
1341
+ why: `watcher re-arm failed: ${err instanceof Error ? err.message : String(err)}`,
1342
+ });
1343
+ }
1344
+ }
1345
+ continue;
1346
+ }
1347
+ if (watcherAlive) {
1348
+ // Watcher owns terminal classification of a process that has just exited.
1349
+ continue;
1350
+ }
1351
+ const hadPids = workerPid !== null || watcherPid !== null;
1352
+ if (!hadPids && nowMs - lastHostReportMs(lane) < DEAD_HOST_MS) {
1353
+ try {
1354
+ const result = casLaneIf(hqRoot, lane.lane_id, (current) => current.last_host_report_at !== nowIso, (current) => ({ ...current, last_host_report_at: nowIso }), deps.dryRun);
1355
+ if (result.changed)
1356
+ summary.heartbeats.push(lane.lane_id);
1357
+ }
1358
+ catch (err) {
1359
+ markDirty(summary, {
1360
+ kind: "lane",
1361
+ id: lane.lane_id,
1362
+ why: `heartbeat CAS failed: ${err instanceof Error ? err.message : String(err)}`,
1363
+ });
1364
+ }
1365
+ continue;
1366
+ }
1367
+ try {
1368
+ const { classification, latest, quota, providerRejection, lane: classified } = classifyDeadLocalLane(hqRoot, lane.lane_id, now);
1369
+ // Match the watcher's one-repair decision before classifying an
1370
+ // unparseable envelope after both processes have died. As in the
1371
+ // watcher, a provider rejection or a quota signal skips the repair.
1372
+ const runDir = laneRunDir(hqRoot, lane.lane_id);
1373
+ const manifest = readSpawnManifest(runDir);
1374
+ const minEnvelopeName = manifest.envelope_path ? path.basename(manifest.envelope_path) : undefined;
1375
+ const rawLatest = readLatestEnvelopeFile(hqRoot, lane.lane_id, minEnvelopeName);
1376
+ const missingExpected = rawLatest === null
1377
+ ? missingExpectedEnvelopeFile(hqRoot, lane.lane_id, manifest.envelope_path)
1378
+ : null;
1379
+ const repairCandidate = rawLatest ?? missingExpected;
1380
+ let invalidEnvelope = null;
1381
+ if (!deps.dryRun &&
1382
+ !providerRejection &&
1383
+ !quota &&
1384
+ repairCandidate &&
1385
+ repairCandidate.envelope === null &&
1386
+ (!repairCandidate.missing || envelopeRepairMode(classified.provider) === "resume")) {
1387
+ if (await lowDiskGuardEnabledForLane(hqRoot, classified, deps.readRegistryFlag)) {
1388
+ const repairConfig = loadLanesConfig(hqRoot);
1389
+ const repairAccount = classified.account
1390
+ ? repairConfig.providers[classified.provider].accounts[classified.account]
1391
+ : undefined;
1392
+ if (!repairAccount) {
1393
+ throw new LanesError("low_disk_account_unavailable", `Cannot determine provider home for envelope repair of lane ${classified.lane_id}`, { lane_id: classified.lane_id });
1394
+ }
1395
+ const providerHome = effectiveAccountHome(repairAccount, deps.host.hostname).home;
1396
+ const free = checkLaneDiskSpace(hqRoot, providerHome, deps.spawnLaneDeps?.statfs, repairConfig.low_disk_min_free_bytes);
1397
+ if (free.low) {
1398
+ const measuredAt = now.toISOString();
1399
+ updateLane(hqRoot, classified.lane_id, classified.version, "reconcile", (current) => ({
1400
+ ...current,
1401
+ state: "queued",
1402
+ queued_reason: "low_disk",
1403
+ queued_details: { reason: "low_disk", hq_free_bytes: free.hq.free_bytes, provider_free_bytes: free.provider.free_bytes, measured_at: measuredAt },
1404
+ queued_at: current.queued_at ?? measuredAt,
1405
+ admission_reserved_at: undefined,
1406
+ terminal_reason: undefined,
1407
+ terminal_details: undefined,
1408
+ }));
1409
+ summary.skipped.push({ kind: "lane", id: classified.lane_id, why: `low_disk: hq=${free.hq.free_bytes} provider=${free.provider.free_bytes}` });
1410
+ continue;
1411
+ }
1412
+ }
1413
+ const spawnRepair = deps.spawnRepair ??
1414
+ ((root, repairLane, input) => spawnEnvelopeRepair(root, repairLane, input, spawnDepsForHost(deps)));
1415
+ const repair = decideEnvelopeRepair(hqRoot, lane.lane_id, repairCandidate, nowMs, spawnRepair, () => undefined);
1416
+ if (repair.action === "spawned") {
1417
+ armLaneWatcher(hqRoot, lane.lane_id, spawnDepsForHost(deps));
1418
+ summary.heartbeats.push(`${lane.lane_id}:envelope-repair`);
1419
+ continue;
1420
+ }
1421
+ if (!repairCandidate.missing)
1422
+ invalidEnvelope = { errors: repair.errors };
1423
+ }
1424
+ let repairedClassification = invalidEnvelope
1425
+ ? classifyLaneExit({ envelope: null, hasPartialOutput: false, invalidEnvelope })
1426
+ : classification;
1427
+ let questionRefusalCode = null;
1428
+ const accountRetryReason = quota
1429
+ ? "usage_limit"
1430
+ : providerRejection && classification.reason === "account_not_logged_in"
1431
+ ? "account_unavailable"
1432
+ : null;
1433
+ if (!deps.dryRun &&
1434
+ quota &&
1435
+ classified.account &&
1436
+ (classification.reason === "account_quota_exhausted" ||
1437
+ classification.reason === "account_model_unavailable")) {
1438
+ recordAccountQuota(hqRoot, classified.provider, classified.account, quota);
1439
+ }
1440
+ if (!deps.dryRun &&
1441
+ accountRetryReason &&
1442
+ classified.account) {
1443
+ try {
1444
+ const recovered = await retryLaneOnAlternateAccount(hqRoot, classified, accountRetryReason, "reconcile", {
1445
+ ...spawnDepsForHost(deps),
1446
+ });
1447
+ summary.dead_host.push({
1448
+ id: lane.lane_id,
1449
+ from: "running",
1450
+ to: recovered.state,
1451
+ reason: accountRetryReason,
1452
+ });
1453
+ summary.heartbeats.push(`${lane.lane_id}:account-retry`);
1454
+ continue;
1455
+ }
1456
+ catch (err) {
1457
+ const message = err instanceof Error ? err.message : String(err);
1458
+ process.stderr.write(`hq lanes reconcile: account failover failed for ${lane.lane_id}: ${message}\n`);
1459
+ if (readLane(hqRoot, lane.lane_id).state !== "running") {
1460
+ summary.errors.push({ message: `${lane.lane_id}: account failover left a non-running record after ${message}` });
1461
+ continue;
1462
+ }
1463
+ }
1464
+ }
1465
+ if (!deps.dryRun &&
1466
+ classification.state === "awaiting_input" &&
1467
+ latest &&
1468
+ (latest.envelope.questions?.length ?? 0) > 0) {
1469
+ const questionLane = readLane(hqRoot, lane.lane_id);
1470
+ try {
1471
+ const activeCompany = await resolveQuestionCompanyWithFallback(hqRoot, questionLane, deps.resolveQuestionActiveCompany);
1472
+ raiseEnvelopeQuestions(hqRoot, questionLane, latest.envelope, latest.ref, nowIso, undefined, activeCompany);
1473
+ }
1474
+ catch (err) {
1475
+ questionRefusalCode = questionRecordRefusalCode(err);
1476
+ if (questionRefusalCode === null)
1477
+ throw err;
1478
+ repairedClassification = {
1479
+ state: "failed",
1480
+ reason: `question_record_refused:${questionRefusalCode}`,
1481
+ };
1482
+ process.stderr.write(`hq lanes reconcile: terminal ask refused for ${lane.lane_id}: ${questionRefusalCode}\n`);
1483
+ }
1484
+ }
1485
+ let terminalEnvelope = latest?.envelope;
1486
+ let terminalClassification = repairedClassification;
1487
+ const result = casLaneIf(hqRoot, lane.lane_id, (current) => current.state === "running", (current) => {
1488
+ const approvalGate = latest?.envelope
1489
+ ? enforceRegistryApprovalGate(current, latest.envelope)
1490
+ : undefined;
1491
+ terminalEnvelope = approvalGate?.envelope ?? latest?.envelope;
1492
+ terminalClassification = approvalGate?.violation
1493
+ ? { state: "blocked", reason: "approval_gate_violated" }
1494
+ : repairedClassification;
1495
+ const next = {
1496
+ ...current,
1497
+ state: terminalClassification.state,
1498
+ queued_reason: undefined,
1499
+ queued_details: undefined,
1500
+ terminal_reason: terminalClassification.reason ?? "host_lost",
1501
+ interrupted_at: terminalClassification.state === "interrupted" ? nowIso : undefined,
1502
+ ...(providerRejection
1503
+ ? { terminal_details: providerRejectionDetails(providerRejection, current) }
1504
+ : {}),
1505
+ ...(quota?.quota_reset_at ? { quota_reset_at: quota.quota_reset_at } : {}),
1506
+ ...(quota?.marker ? { model_quota_marker: quota.marker } : {}),
1507
+ last_host_report_at: nowIso,
1508
+ };
1509
+ if (approvalGate && !approvalGate.violation && approvalGate.approvedIdsToConsume.length > 0) {
1510
+ next.registry_worker_approval_history = approvalGate.approvedIdsToConsume.reduce((history, approvalId) => appendCappedHistory(history, {
1511
+ event: "consumed",
1512
+ approval_id: approvalId,
1513
+ envelope_ref: latest.ref,
1514
+ at: nowIso,
1515
+ }), current.registry_worker_approval_history ?? []);
1516
+ }
1517
+ if (terminalEnvelope && latest) {
1518
+ const envelope = terminalEnvelope;
1519
+ next.last_envelope = {
1520
+ ref: latest.ref,
1521
+ decision: envelope.decision,
1522
+ at: nowIso,
1523
+ artifacts: envelope.artifacts,
1524
+ ...(envelope.blocked_reason !== undefined ? { blocked_reason: envelope.blocked_reason } : {}),
1525
+ };
1526
+ const storySwitch = applyStorySwitchEnvelope(current, envelope, nowIso, latest.ref);
1527
+ if (storySwitch.clearPending) {
1528
+ next.story_switch_pending = undefined;
1529
+ }
1530
+ if (storySwitch.storySwitchUnacknowledged !== undefined) {
1531
+ next.story_switch_unacknowledged = storySwitch.storySwitchUnacknowledged;
1532
+ next.attribution_history = storySwitch.attributionHistory ?? current.attribution_history;
1533
+ }
1534
+ const storyStatus = storySwitch.suppressCompletion
1535
+ ? "in_progress"
1536
+ : terminalEnvelopeStoryStatus(current, envelope.decision, envelope.story_status);
1537
+ if (storyStatus !== undefined) {
1538
+ next.story_status = storySwitch.suppressCompletion ? "in_progress" : envelope.story_status;
1539
+ next.story_complete = storySwitch.suppressCompletion ? false : envelope.story_complete;
1540
+ next.story_complete_reason = storySwitch.suppressCompletion ? undefined : envelope.story_complete_reason;
1541
+ next.stories = laneStories(current).map((story) => story.project_id === current.project_id && story.story_id === current.story_id
1542
+ ? {
1543
+ ...story,
1544
+ status: storyStatus,
1545
+ updated_at: nowIso,
1546
+ ...(envelope.decision === "blocked"
1547
+ ? { blocked_reason: envelope.blocked_reason ?? "" }
1548
+ : { blocked_reason: undefined }),
1549
+ }
1550
+ : story);
1551
+ }
1552
+ if (approvalGate?.violation) {
1553
+ next.last_envelope = {
1554
+ ref: latest.ref,
1555
+ decision: envelope.decision,
1556
+ at: nowIso,
1557
+ artifacts: envelope.artifacts,
1558
+ blocked_reason: envelope.blocked_reason,
1559
+ };
1560
+ }
1561
+ }
1562
+ return next;
1563
+ }, deps.dryRun);
1564
+ if (!deps.dryRun &&
1565
+ result.changed &&
1566
+ result.record.state === "done" &&
1567
+ terminalClassification.state === "done" &&
1568
+ terminalEnvelope?.decision === "done" &&
1569
+ !quota &&
1570
+ !providerRejection &&
1571
+ result.record.account &&
1572
+ result.record.model) {
1573
+ clearAccountQuotaAfterSuccessfulTurnBestEffort(hqRoot, result.record.provider, result.record.account, result.record.model, new Date(nowIso));
1574
+ }
1575
+ if (result.changed) {
1576
+ summary.dead_host.push({
1577
+ id: lane.lane_id,
1578
+ from: "running",
1579
+ to: terminalClassification.state,
1580
+ reason: terminalClassification.reason ?? "host_lost",
1581
+ });
1582
+ if (terminalEnvelope && latest && questionRefusalCode === null) {
1583
+ const mesh = deps.mesh === undefined ? createLaneMesh({ interactive: false }) : deps.mesh;
1584
+ if (mesh) {
1585
+ const meshEmitTimeoutMs = deps.meshEmitTimeoutMs ?? 10_000;
1586
+ let timer;
1587
+ try {
1588
+ const questionIds = terminalEnvelope.decision === "ask"
1589
+ ? envelopeQuestionIds(lane.lane_id, latest.ref, terminalEnvelope)
1590
+ : undefined;
1591
+ await Promise.race([
1592
+ emitLaneDecisionEvents(mesh, result.record, terminalEnvelope, { questionIds }),
1593
+ new Promise((_resolve, reject) => {
1594
+ timer = setTimeout(() => reject(new Error(`terminal mesh emit timed out after ${meshEmitTimeoutMs}ms`)), meshEmitTimeoutMs);
1595
+ }),
1596
+ ]);
1597
+ }
1598
+ catch (err) {
1599
+ markDirty(summary, {
1600
+ kind: "lane",
1601
+ id: lane.lane_id,
1602
+ why: `terminal mesh emit failed: ${err instanceof Error ? err.message : String(err)}`,
1603
+ });
1604
+ }
1605
+ finally {
1606
+ if (timer)
1607
+ clearTimeout(timer);
1608
+ }
1609
+ }
1610
+ }
1611
+ else if (!deps.dryRun &&
1612
+ (result.record.state === "failed" || result.record.state === "interrupted" ||
1613
+ result.record.state === "cancelled" || result.record.state === "killed")) {
1614
+ const mesh = deps.mesh === undefined ? createLaneMesh({ interactive: false }) : deps.mesh;
1615
+ if (mesh) {
1616
+ const meshEmitTimeoutMs = deps.meshEmitTimeoutMs ?? 10_000;
1617
+ let timer;
1618
+ try {
1619
+ await Promise.race([
1620
+ emitLaneTerminalStateEvent(mesh, result.record, result.record.state, result.record.terminal_reason),
1621
+ new Promise((_resolve, reject) => {
1622
+ timer = setTimeout(() => reject(new Error(`terminal mesh emit timed out after ${meshEmitTimeoutMs}ms`)), meshEmitTimeoutMs);
1623
+ }),
1624
+ ]);
1625
+ }
1626
+ catch (err) {
1627
+ markDirty(summary, {
1628
+ kind: "lane",
1629
+ id: lane.lane_id,
1630
+ why: `terminal mesh emit failed: ${err instanceof Error ? err.message : String(err)}`,
1631
+ });
1632
+ }
1633
+ finally {
1634
+ if (timer)
1635
+ clearTimeout(timer);
1636
+ }
1637
+ }
1638
+ }
1639
+ if (!deps.dryRun && deps.recoverQueuedMessages) {
1640
+ try {
1641
+ await deps.recoverQueuedMessages(result.record);
1642
+ }
1643
+ catch (err) {
1644
+ markDirty(summary, {
1645
+ kind: "lane",
1646
+ id: lane.lane_id,
1647
+ why: `queued message recovery failed: ${err instanceof Error ? err.message : String(err)}`,
1648
+ });
1649
+ }
1650
+ }
1651
+ }
1652
+ }
1653
+ catch (err) {
1654
+ markDirty(summary, {
1655
+ kind: "lane",
1656
+ id: lane.lane_id,
1657
+ why: `dead-host classify failed: ${err instanceof Error ? err.message : String(err)}`,
1658
+ });
1659
+ }
1660
+ continue;
1661
+ }
1662
+ if (nowMs - lastHostReportMs(lane) < DEAD_HOST_MS)
1663
+ continue;
1664
+ try {
1665
+ const result = casLaneIf(hqRoot, lane.lane_id, (current) => current.state === "running" &&
1666
+ nowMs - lastHostReportMs(current) >= DEAD_HOST_MS, (current) => ({
1667
+ ...current,
1668
+ state: "interrupted",
1669
+ queued_reason: undefined,
1670
+ queued_details: undefined,
1671
+ terminal_reason: "host_lost",
1672
+ interrupted_at: nowIso,
1673
+ }), deps.dryRun);
1674
+ if (result.changed) {
1675
+ summary.dead_host.push({
1676
+ id: lane.lane_id,
1677
+ from: "running",
1678
+ to: "interrupted",
1679
+ reason: "host_lost",
1680
+ });
1681
+ const mesh = !deps.dryRun
1682
+ ? deps.mesh === undefined ? createLaneMesh({ interactive: false }) : deps.mesh
1683
+ : null;
1684
+ if (mesh) {
1685
+ const meshEmitTimeoutMs = deps.meshEmitTimeoutMs ?? 10_000;
1686
+ let timer;
1687
+ try {
1688
+ await Promise.race([
1689
+ emitLaneTerminalStateEvent(mesh, result.record, "interrupted", "host_lost"),
1690
+ new Promise((_resolve, reject) => {
1691
+ timer = setTimeout(() => reject(new Error(`terminal mesh emit timed out after ${meshEmitTimeoutMs}ms`)), meshEmitTimeoutMs);
1692
+ }),
1693
+ ]);
1694
+ }
1695
+ catch (err) {
1696
+ markDirty(summary, {
1697
+ kind: "lane",
1698
+ id: lane.lane_id,
1699
+ why: `terminal mesh emit failed: ${err instanceof Error ? err.message : String(err)}`,
1700
+ });
1701
+ }
1702
+ finally {
1703
+ if (timer)
1704
+ clearTimeout(timer);
1705
+ }
1706
+ }
1707
+ }
1708
+ }
1709
+ catch (err) {
1710
+ markDirty(summary, {
1711
+ kind: "lane",
1712
+ id: lane.lane_id,
1713
+ why: `dead-host CAS failed: ${err instanceof Error ? err.message : String(err)}`,
1714
+ });
1715
+ }
1716
+ }
1717
+ }
1718
+ async function admitQueued(hqRoot, lanes, summary, deps, now, onlyLaneIds) {
1719
+ if (onlyLaneIds === undefined)
1720
+ expireAbandonedPickupValidation(hqRoot, lanes, summary, deps, now);
1721
+ // A lane whose pickup probe is still out is not admitted: its record is
1722
+ // about to be bound or failed by the pickup, and a worker spawned now
1723
+ // would keep running behind that write.
1724
+ const localQueued = lanes
1725
+ .filter((lane) => lane.state === "queued" &&
1726
+ (onlyLaneIds === undefined || onlyLaneIds.has(lane.lane_id)) &&
1727
+ lane.pickup_validation_pending !== true &&
1728
+ laneRunsHere(lane, deps.host))
1729
+ .filter((lane) => !hasFreshAdmissionReservation(lane, now.getTime()))
1730
+ .sort(compareLaneQueueOrder);
1731
+ if (localQueued.length === 0 || deps.skipAdmissionWithoutCpuSample === true)
1732
+ return;
1733
+ const config = loadLanesConfig(hqRoot);
1734
+ const throttleDeps = deps.throttleDeps ?? defaultThrottleDeps(deps.env ?? process.env);
1735
+ const filePath = throttleStatePath(hqRoot);
1736
+ const loaded = readThrottleState(filePath, throttleDeps);
1737
+ const throttleState = loaded === "unreadable"
1738
+ ? "unreadable"
1739
+ : loaded === "missing"
1740
+ ? emptyThrottleState()
1741
+ : loaded;
1742
+ const busy = deps.cpuBusyFraction !== undefined
1743
+ ? deps.cpuBusyFraction
1744
+ : await resolveCpuBusyFraction(throttleDeps);
1745
+ const availableMemMb = readAvailableMemMb(throttleDeps);
1746
+ const nowMs = now.getTime();
1747
+ const decision = evaluateThrottle({
1748
+ busy,
1749
+ availableMemMb,
1750
+ cpuCheckEnabled: isCpuCheckEnabled(throttleDeps.env),
1751
+ state: throttleState,
1752
+ throttle: config.throttle,
1753
+ nowMs,
1754
+ });
1755
+ const spawnFn = deps.spawn ??
1756
+ (async (root, lane) => spawnLaneWorker(root, lane, {
1757
+ ...spawnDepsForHost(deps),
1758
+ lowDiskGuardEnabled: await lowDiskGuardEnabledForLane(root, lane, deps.readRegistryFlag),
1759
+ }));
1760
+ const running = lanes.filter((lane) => (lane.state === "running" ||
1761
+ hasFreshAdmissionReservation(lane, now.getTime()) ||
1762
+ hasActivePendingResume(lane, now.getTime())) &&
1763
+ laneRunsHere(lane, deps.host));
1764
+ const spawned = [];
1765
+ if (!decision.admit) {
1766
+ for (const lane of localQueued) {
1767
+ let profileMaxConcurrent;
1768
+ if (lane.worker_profile !== undefined) {
1769
+ try {
1770
+ profileMaxConcurrent = loadWorkerProfile(hqRoot, lane.worker_profile).meta.max_concurrent;
1771
+ }
1772
+ catch (err) {
1773
+ markDirty(summary, {
1774
+ kind: "lane",
1775
+ id: lane.lane_id,
1776
+ why: `profile admission failed: ${err instanceof Error ? err.message : String(err)}`,
1777
+ });
1778
+ continue;
1779
+ }
1780
+ }
1781
+ const capReason = laneAdmissionCapReason(running, lane, config, deps.host, profileMaxConcurrent, now.getTime());
1782
+ writeQueueReason(hqRoot, lane, capReason ?? "throttle", deps.dryRun);
1783
+ }
1784
+ if (!deps.dryRun && throttleState !== "unreadable") {
1785
+ writeThrottleState(filePath, applyThrottleDecision(throttleState, decision, nowMs), throttleDeps);
1786
+ }
1787
+ return;
1788
+ }
1789
+ for (const lane of localQueued) {
1790
+ let profileMaxConcurrent;
1791
+ if (lane.worker_profile !== undefined) {
1792
+ try {
1793
+ profileMaxConcurrent = loadWorkerProfile(hqRoot, lane.worker_profile).meta.max_concurrent;
1794
+ }
1795
+ catch (err) {
1796
+ markDirty(summary, {
1797
+ kind: "lane",
1798
+ id: lane.lane_id,
1799
+ why: `profile admission failed: ${err instanceof Error ? err.message : String(err)}`,
1800
+ });
1801
+ continue;
1802
+ }
1803
+ }
1804
+ const projected = [...running, ...spawned];
1805
+ const alternateAccount = availableBalancedAccount(hqRoot, lane, projected, config, deps.host, profileMaxConcurrent, now.getTime());
1806
+ const admittedLane = alternateAccount !== undefined
1807
+ ? writeAdmissionAccount(hqRoot, lane, alternateAccount, deps.dryRun)
1808
+ : lane;
1809
+ const baseCap = laneAdmissionCapReason(running, admittedLane, config, deps.host, profileMaxConcurrent, now.getTime());
1810
+ const currentCap = laneAdmissionCapReason([...running, ...spawned], admittedLane, config, deps.host, profileMaxConcurrent, now.getTime());
1811
+ let blockedBy;
1812
+ if (currentCap !== undefined) {
1813
+ blockedBy = baseCap ?? "queue_position";
1814
+ }
1815
+ if (!blockedBy && !decision.admit)
1816
+ blockedBy = "throttle";
1817
+ if (blockedBy) {
1818
+ writeQueueReason(hqRoot, admittedLane, blockedBy, deps.dryRun);
1819
+ continue;
1820
+ }
1821
+ try {
1822
+ if (deps.dryRun) {
1823
+ summary.admitted.push({
1824
+ id: lane.lane_id,
1825
+ from: "queued",
1826
+ to: "running",
1827
+ reason: decision.reason,
1828
+ });
1829
+ spawned.push({ ...admittedLane, state: "running" });
1830
+ continue;
1831
+ }
1832
+ const started = await spawnFn(hqRoot, admittedLane);
1833
+ if (decision.reason === "starvation_bound") {
1834
+ casLaneIf(hqRoot, lane.lane_id, (current) => current.admitted_reason !== "starvation_bound", (current) => ({ ...current, admitted_reason: "starvation_bound" }), false);
1835
+ }
1836
+ summary.admitted.push({
1837
+ id: started.lane_id,
1838
+ from: "queued",
1839
+ to: started.state,
1840
+ reason: started.queued_reason ?? decision.reason,
1841
+ ...(started.queued_reason === "provider_binary_missing" &&
1842
+ typeof started.terminal_details?.searched_path === "string"
1843
+ ? {
1844
+ searched_path: started.terminal_details.searched_path,
1845
+ }
1846
+ : {}),
1847
+ });
1848
+ spawned.push(started);
1849
+ }
1850
+ catch (err) {
1851
+ if (err instanceof LanesError && err.error === "worker_profile_hash_mismatch") {
1852
+ const expectedHash = typeof err.details.expected_hash === "string"
1853
+ ? err.details.expected_hash
1854
+ : null;
1855
+ const actualHash = typeof err.details.actual_hash === "string"
1856
+ ? err.details.actual_hash
1857
+ : undefined;
1858
+ const workerProfile = typeof err.details.worker === "string"
1859
+ ? err.details.worker
1860
+ : lane.worker_profile;
1861
+ if (actualHash !== undefined && workerProfile !== undefined) {
1862
+ casLaneIf(hqRoot, lane.lane_id, (current) => current.state === "queued", (current) => ({
1863
+ ...current,
1864
+ queued_reason: "worker_profile_changed",
1865
+ queued_details: {
1866
+ reason: "worker_profile_changed",
1867
+ expected_hash: expectedHash,
1868
+ actual_hash: actualHash,
1869
+ recovery_command: `hq lanes resume ${lane.lane_id} --worker ${workerProfile}`,
1870
+ },
1871
+ admission_reserved_at: undefined,
1872
+ }), false);
1873
+ summary.skipped.push({
1874
+ kind: "lane",
1875
+ id: lane.lane_id,
1876
+ why: "worker_profile_changed",
1877
+ });
1878
+ continue;
1879
+ }
1880
+ }
1881
+ terminalizeModelUnsupported(hqRoot, lane, err);
1882
+ markDirty(summary, {
1883
+ kind: "lane",
1884
+ id: lane.lane_id,
1885
+ why: `admit failed: ${err instanceof Error ? err.message : String(err)}`,
1886
+ });
1887
+ }
1888
+ }
1889
+ if (!deps.dryRun) {
1890
+ const admittedAny = spawned.length > 0;
1891
+ const applied = admittedAny
1892
+ ? { admit: true, reason: decision.reason }
1893
+ : decision;
1894
+ const base = throttleState === "unreadable" ? emptyThrottleState() : throttleState;
1895
+ writeThrottleState(filePath, applyThrottleDecision(base, applied, nowMs), throttleDeps);
1896
+ }
1897
+ }
1898
+ function keepAliveCompanyKey(company) {
1899
+ return JSON.stringify([company?.uid ?? null, company?.slug ?? null]);
1900
+ }
1901
+ function hasKeepAliveProgress(lane) {
1902
+ return (lane.keep_alive_resume_count ?? 0) !== 0 ||
1903
+ lane.keep_alive_last_resume_at !== undefined ||
1904
+ lane.keep_alive_next_eligible_at !== undefined ||
1905
+ lane.keep_alive_exhausted_at !== undefined;
1906
+ }
1907
+ function clearedKeepAliveProgress(lane) {
1908
+ return {
1909
+ ...lane,
1910
+ keep_alive_resume_count: 0,
1911
+ keep_alive_last_resume_at: undefined,
1912
+ keep_alive_next_eligible_at: undefined,
1913
+ keep_alive_exhausted_at: undefined,
1914
+ };
1915
+ }
1916
+ function keepAliveProgressResetByHuman(lane) {
1917
+ const count = lane.keep_alive_resume_count ?? 0;
1918
+ const lastAutoMs = Date.parse(lane.keep_alive_last_resume_at ?? "");
1919
+ if (count > 0 && !Number.isFinite(lastAutoMs))
1920
+ return true;
1921
+ const messageMs = Date.parse(lane.last_message_at ?? "");
1922
+ const resumeMs = lane.last_resume?.text === LEAD_LANE_KEEP_ALIVE_RESUME_TEXT ||
1923
+ lane.last_resume?.text === LEAD_LANE_KEEP_ALIVE_INTERRUPTED_RESUME_TEXT ||
1924
+ lane.last_resume?.text === LEAD_LANE_KEEP_ALIVE_BLOCKED_RESUME_TEXT
1925
+ ? Number.NaN
1926
+ : Date.parse(lane.last_resume?.at ?? "");
1927
+ return Number.isFinite(lastAutoMs) &&
1928
+ ((Number.isFinite(messageMs) && messageMs > lastAutoMs) ||
1929
+ (Number.isFinite(resumeMs) && resumeMs > lastAutoMs));
1930
+ }
1931
+ function recordKeepAliveEvent(hqRoot, laneId, summary, event) {
1932
+ try {
1933
+ writeWatcherDecision(watcherEventsPath(hqRoot, laneId), {
1934
+ lane_id: laneId,
1935
+ ...event,
1936
+ });
1937
+ }
1938
+ catch (err) {
1939
+ markDirty(summary, {
1940
+ kind: "lane",
1941
+ id: laneId,
1942
+ why: `keep-alive event write failed: ${err instanceof Error ? err.message : String(err)}`,
1943
+ });
1944
+ }
1945
+ }
1946
+ function keepAliveAccountSelectionEvidence(selection) {
1947
+ if (selection === undefined)
1948
+ return undefined;
1949
+ return {
1950
+ account: selection.account,
1951
+ selected_at: selection.selected_at,
1952
+ reason: selection.reason,
1953
+ considered: selection.considered.map((row) => ({
1954
+ account: row.account,
1955
+ live_lanes: row.live_lanes,
1956
+ eligible: row.eligible,
1957
+ reason: row.ineligible_reason ?? null,
1958
+ })),
1959
+ };
1960
+ }
1961
+ function keepAliveSkipGuidance(laneId, reason) {
1962
+ switch (reason) {
1963
+ case "missing_opt_in_time":
1964
+ return `if appropriate, run hq lanes keep-alive ${laneId} --on`;
1965
+ case "not_opted_in":
1966
+ return "explicit opt-in is required before keep-alive can resume this lane";
1967
+ case "envelope_before_opt_in":
1968
+ return `the latest envelope predates opt-in; review the lane with hq lanes show ${laneId}`;
1969
+ case "interruption_before_opt_in":
1970
+ return `the interruption predates opt-in; review the lane with hq lanes show ${laneId}`;
1971
+ case "missing_interruption_time":
1972
+ return "the interruption time is unavailable; inspect the watcher classification before resuming";
1973
+ case "retired":
1974
+ return "the lane is retired; resolve its retired status before resuming";
1975
+ case "superseded":
1976
+ return "a newer lead exists for this project; continue on that lane";
1977
+ case "retired_list_invalid":
1978
+ return "the retired lane list is invalid; repair workspace/lane-briefs/juniors.json before retrying";
1979
+ case "terminal_state":
1980
+ return `the lane is in a terminal state; review it with hq lanes show ${laneId} before starting replacement work`;
1981
+ }
1982
+ }
1983
+ function recordKeepAliveSkip(hqRoot, lane, reason, summary, deps, detail) {
1984
+ summary.skipped.push({
1985
+ kind: "lane",
1986
+ id: lane.lane_id,
1987
+ why: `keep_alive_${reason}${detail ? `: ${detail}` : ""}`,
1988
+ });
1989
+ if (lane.keep_alive === true) {
1990
+ summary.keep_alive_blocked.push({ id: lane.lane_id, reason });
1991
+ process.stderr.write(`hq lanes reconcile: keep-alive blocked for lane ${lane.lane_id} (${reason}); ${keepAliveSkipGuidance(lane.lane_id, reason)}\n`);
1992
+ }
1993
+ if (deps.dryRun)
1994
+ return;
1995
+ let laneVersion = lane.version;
1996
+ if (lane.keep_alive_blocked_reason !== reason) {
1997
+ try {
1998
+ const update = casLaneIf(hqRoot, lane.lane_id, (current) => current.version === lane.version && current.state === lane.state, (current) => ({ ...current, keep_alive_blocked_reason: reason }), false);
1999
+ laneVersion = update.record.version;
2000
+ }
2001
+ catch (err) {
2002
+ markDirty(summary, {
2003
+ kind: "lane",
2004
+ id: lane.lane_id,
2005
+ why: `keep-alive skip state write failed: ${err instanceof Error ? err.message : String(err)}`,
2006
+ });
2007
+ }
2008
+ }
2009
+ if (lane.keep_alive === true) {
2010
+ recordKeepAliveSkipEvent(hqRoot, lane, reason, laneVersion, summary, deps, detail);
2011
+ }
2012
+ }
2013
+ function recordDoneKeepAlivePolicySkip(hqRoot, summary, lane, verdict, deps) {
2014
+ summary.skipped.push({
2015
+ kind: "lane",
2016
+ id: lane.lane_id,
2017
+ why: `keep_alive_${verdict}`,
2018
+ });
2019
+ if (lane.keep_alive === true && !deps.dryRun) {
2020
+ recordKeepAliveSkipEvent(hqRoot, lane, verdict, lane.version, summary, deps);
2021
+ }
2022
+ }
2023
+ function recordKeepAliveSkipEvent(hqRoot, lane, reason, laneVersion, summary, deps, detail) {
2024
+ if (lane.keep_alive !== true || deps.dryRun)
2025
+ return;
2026
+ const eventsPath = watcherEventsPath(hqRoot, lane.lane_id);
2027
+ try {
2028
+ let lastSkip;
2029
+ try {
2030
+ const scanBackwards = (body, targetReason, targetVersion) => {
2031
+ const lines = body.split(/\r?\n/);
2032
+ for (let index = lines.length - 1; index >= 0; index -= 1) {
2033
+ if (!lines[index])
2034
+ continue;
2035
+ try {
2036
+ const value = JSON.parse(lines[index]);
2037
+ if (typeof value === "object" && value !== null && !Array.isArray(value)) {
2038
+ const event = value;
2039
+ if (event.event === "keep_alive_skip" &&
2040
+ (targetReason === undefined || (event.reason === targetReason && event.lane_version === targetVersion))) {
2041
+ return event;
2042
+ }
2043
+ }
2044
+ }
2045
+ catch {
2046
+ // A partial concurrent append must not prevent future skip events.
2047
+ }
2048
+ }
2049
+ return undefined;
2050
+ };
2051
+ const maxTailBytes = 64 * 1024;
2052
+ const fd = fs.openSync(eventsPath, "r");
2053
+ let fileSize;
2054
+ let tail;
2055
+ let tailOffset;
2056
+ try {
2057
+ fileSize = fs.fstatSync(fd).size;
2058
+ tailOffset = Math.max(0, fileSize - maxTailBytes);
2059
+ const buffer = Buffer.alloc(fileSize - tailOffset);
2060
+ let bytesRead = 0;
2061
+ while (bytesRead < buffer.length) {
2062
+ const count = fs.readSync(fd, buffer, bytesRead, buffer.length - bytesRead, tailOffset + bytesRead);
2063
+ if (count === 0)
2064
+ break;
2065
+ bytesRead += count;
2066
+ }
2067
+ tail = buffer.subarray(0, bytesRead).toString("utf8");
2068
+ }
2069
+ finally {
2070
+ fs.closeSync(fd);
2071
+ }
2072
+ // When the bounded read starts mid-line, ignore that incomplete prefix.
2073
+ if (tailOffset > 0) {
2074
+ const firstNewline = tail.indexOf("\n");
2075
+ tail = firstNewline < 0 ? "" : tail.slice(firstNewline + 1);
2076
+ }
2077
+ lastSkip = scanBackwards(tail, reason === "next_check_at" ? reason : undefined, laneVersion);
2078
+ // For small logs the tail is not a complete record boundary if a single
2079
+ // record exceeded the bound, so allow one full-file scan as a fallback.
2080
+ if (!lastSkip && fileSize < maxTailBytes) {
2081
+ lastSkip = scanBackwards(fs.readFileSync(eventsPath, "utf8"), reason === "next_check_at" ? reason : undefined, laneVersion);
2082
+ }
2083
+ }
2084
+ catch (err) {
2085
+ if (err.code !== "ENOENT")
2086
+ throw err;
2087
+ }
2088
+ if (lastSkip?.reason === reason && lastSkip.lane_version === laneVersion)
2089
+ return;
2090
+ recordKeepAliveEvent(hqRoot, lane.lane_id, summary, {
2091
+ event: "keep_alive_skip",
2092
+ at: deps.now().toISOString(),
2093
+ reason,
2094
+ ...(detail === undefined ? {} : { detail }),
2095
+ lane_version: laneVersion,
2096
+ lane_state: lane.state,
2097
+ });
2098
+ }
2099
+ catch (err) {
2100
+ markDirty(summary, {
2101
+ kind: "lane",
2102
+ id: lane.lane_id,
2103
+ why: `keep-alive skip event dedupe failed: ${err instanceof Error ? err.message : String(err)}`,
2104
+ });
2105
+ }
2106
+ }
2107
+ function readLatestInterruptedClassificationAt(hqRoot, lane) {
2108
+ const logPath = watcherOutLogPath(hqRoot, lane.lane_id);
2109
+ let body;
2110
+ try {
2111
+ body = fs.readFileSync(logPath, "utf8");
2112
+ }
2113
+ catch (err) {
2114
+ if (err.code === "ENOENT")
2115
+ return undefined;
2116
+ throw err;
2117
+ }
2118
+ let latestClassification;
2119
+ for (const line of body.split(/\r?\n/).filter(Boolean)) {
2120
+ let parsed;
2121
+ try {
2122
+ parsed = JSON.parse(line);
2123
+ }
2124
+ catch {
2125
+ continue;
2126
+ }
2127
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
2128
+ continue;
2129
+ const record = parsed;
2130
+ if (record.event === "classification")
2131
+ latestClassification = record;
2132
+ }
2133
+ const at = latestClassification?.at;
2134
+ if (latestClassification?.state !== "interrupted" ||
2135
+ latestClassification.reason !== lane.terminal_reason ||
2136
+ typeof at !== "string" ||
2137
+ !Number.isFinite(Date.parse(at)) ||
2138
+ new Date(Date.parse(at)).toISOString() !== at)
2139
+ return undefined;
2140
+ return at;
2141
+ }
2142
+ function latestEnvelopeMtimeMs(hqRoot, lane, ref) {
2143
+ const filePath = path.join(laneRunDir(hqRoot, lane.lane_id), ref);
2144
+ try {
2145
+ return fs.statSync(filePath).mtimeMs;
2146
+ }
2147
+ catch (err) {
2148
+ throw new LanesError("keep_alive_envelope_timestamp_unavailable", `Cannot stat latest envelope for lane ${lane.lane_id}: ${err instanceof Error ? err.message : String(err)}`, { lane_id: lane.lane_id, path: filePath });
2149
+ }
2150
+ }
2151
+ async function resumeBlockedKeepAlive(hqRoot, lane, questions, summary, deps, now, keepAliveFlags, blockedFlags, companyForLane, lanes, retiredLaneIds, lineageAwareByCompany) {
2152
+ const company = companyForLane(lane);
2153
+ if (company === null) {
2154
+ summary.skipped.push({ kind: "lane", id: lane.lane_id, why: "keep_alive_company_unresolved" });
2155
+ return;
2156
+ }
2157
+ const key = keepAliveCompanyKey(company);
2158
+ if (keepAliveFlags.get(key) !== true || blockedFlags.get(key) !== true) {
2159
+ summary.skipped.push({ kind: "lane", id: lane.lane_id, why: "keep_alive_blocked" });
2160
+ return;
2161
+ }
2162
+ if (lane.terminal_reason === "approval_gate_violated" || lane.last_envelope?.blocked_reason === "approval_gate_violated") {
2163
+ summary.skipped.push({ kind: "lane", id: lane.lane_id, why: "keep_alive_approval_gate_violated" });
2164
+ return;
2165
+ }
2166
+ if (lane.last_envelope?.decision !== "blocked") {
2167
+ summary.skipped.push({ kind: "lane", id: lane.lane_id, why: "keep_alive_missing_blocked_envelope" });
2168
+ return;
2169
+ }
2170
+ if (retiredLaneIds === null) {
2171
+ recordKeepAliveSkip(hqRoot, lane, "retired_list_invalid", summary, deps);
2172
+ return;
2173
+ }
2174
+ const exclusion = keepAliveExclusion(lane, lanes, retiredLaneIds, lineageAwareByCompany.get(key) === true, companyForLane);
2175
+ if (exclusion !== undefined) {
2176
+ recordKeepAliveSkip(hqRoot, lane, exclusion, summary, deps);
2177
+ return;
2178
+ }
2179
+ const terminalAt = lane.last_envelope.at;
2180
+ const terminalMs = Date.parse(terminalAt ?? "");
2181
+ if (!Number.isFinite(terminalMs)) {
2182
+ recordKeepAliveSkip(hqRoot, lane, "missing_interruption_time", summary, deps, "blocked terminal time is invalid");
2183
+ return;
2184
+ }
2185
+ if (activeLaneStories(lane).length === 0) {
2186
+ summary.skipped.push({ kind: "lane", id: lane.lane_id, why: "keep_alive_no_open_stories" });
2187
+ return;
2188
+ }
2189
+ let pendingQuestion = questions.some((question) => question.lane_id === lane.lane_id && question.status === "pending");
2190
+ try {
2191
+ for (const id of listQuestionIds(hqRoot)) {
2192
+ const question = readQuestion(hqRoot, id);
2193
+ if (question.lane_id === lane.lane_id && question.status === "pending")
2194
+ pendingQuestion = true;
2195
+ }
2196
+ }
2197
+ catch (error) {
2198
+ markDirty(summary, { kind: "lane", id: lane.lane_id, why: "blocked keep-alive question read failed: " + (error instanceof Error ? error.message : String(error)) });
2199
+ return;
2200
+ }
2201
+ const blockedHistory = lane.status_gate_history ?? [];
2202
+ const priorResumes = lane.keep_alive_blocked_resume_streak ?? 0;
2203
+ const alreadyEscalated = blockedHistory.some((entry) => entry.event === "keep_alive_blocked_escalated" && entry.terminal_at === terminalAt);
2204
+ if (pendingQuestion) {
2205
+ appendBlockedKeepAliveHistory(hqRoot, lane, terminalAt, now.toISOString(), "open_question", deps);
2206
+ return;
2207
+ }
2208
+ if (priorResumes >= 2) {
2209
+ if (!alreadyEscalated) {
2210
+ casLaneIf(hqRoot, lane.lane_id, (current) => current.version === lane.version && current.state === "blocked" && current.last_envelope?.at === terminalAt, (current) => ({
2211
+ ...current,
2212
+ status_gate_history: appendCappedHistory(current.status_gate_history ?? [], {
2213
+ event: "keep_alive_blocked_escalated",
2214
+ at: now.toISOString(),
2215
+ terminal_at: terminalAt,
2216
+ }),
2217
+ }), deps.dryRun);
2218
+ }
2219
+ summary.skipped.push({ kind: "lane", id: lane.lane_id, why: "keep_alive_blocked_escalated" });
2220
+ return;
2221
+ }
2222
+ if ((lane.keep_alive_resume_count ?? 0) >= LEAD_LANE_KEEP_ALIVE_MAX_RESUMES || lane.keep_alive_exhausted_at !== undefined) {
2223
+ if (lane.keep_alive_exhausted_at === undefined && !deps.dryRun) {
2224
+ casLaneIf(hqRoot, lane.lane_id, (current) => current.version === lane.version && current.state === "blocked" &&
2225
+ (current.keep_alive_resume_count ?? 0) >= LEAD_LANE_KEEP_ALIVE_MAX_RESUMES, (current) => ({ ...current, keep_alive_exhausted_at: now.toISOString() }), false);
2226
+ }
2227
+ summary.skipped.push({ kind: "lane", id: lane.lane_id, why: "keep_alive_exhausted" });
2228
+ return;
2229
+ }
2230
+ if (now.getTime() - terminalMs < LEAD_LANE_KEEP_ALIVE_BLOCKED_GRACE_MS) {
2231
+ appendBlockedKeepAliveHistory(hqRoot, lane, terminalAt, now.toISOString(), "blocked_grace", deps);
2232
+ return;
2233
+ }
2234
+ const latest = deps.readKeepAliveRecheckLane?.(hqRoot, lane.lane_id) ?? readLane(hqRoot, lane.lane_id);
2235
+ if (latest.version !== lane.version || latest.state !== "blocked" || latest.last_envelope?.decision !== "blocked" || latest.last_envelope.at !== terminalAt)
2236
+ return;
2237
+ let hasOpenQuestion;
2238
+ try {
2239
+ hasOpenQuestion = listQuestionIds(hqRoot).some((id) => {
2240
+ const question = readQuestion(hqRoot, id);
2241
+ return question.lane_id === lane.lane_id && question.status === "pending";
2242
+ });
2243
+ }
2244
+ catch (error) {
2245
+ markDirty(summary, { kind: "lane", id: lane.lane_id, why: "blocked keep-alive question recheck failed: " + (error instanceof Error ? error.message : String(error)) });
2246
+ return;
2247
+ }
2248
+ if (hasOpenQuestion) {
2249
+ appendBlockedKeepAliveHistory(hqRoot, latest, terminalAt, now.toISOString(), "open_question", deps);
2250
+ return;
2251
+ }
2252
+ let current = latest;
2253
+ let accountSelection;
2254
+ if (current.provider === "claude" && deps.prepareKeepAliveClaudeResume !== undefined) {
2255
+ try {
2256
+ const prepared = await deps.prepareKeepAliveClaudeResume(current);
2257
+ current = prepared.lane;
2258
+ accountSelection = prepared.accountSelection;
2259
+ }
2260
+ catch (error) {
2261
+ markDirty(summary, { kind: "lane", id: lane.lane_id, why: `blocked keep-alive account preparation failed: ${error instanceof Error ? error.message : String(error)}` });
2262
+ return;
2263
+ }
2264
+ const afterPreparation = deps.readKeepAliveRecheckLane?.(hqRoot, lane.lane_id) ?? readLane(hqRoot, lane.lane_id);
2265
+ if (afterPreparation.state !== "blocked" || afterPreparation.last_envelope?.decision !== "blocked" || afterPreparation.last_envelope.at !== terminalAt)
2266
+ return;
2267
+ current = afterPreparation;
2268
+ }
2269
+ const expectedVersion = current.version;
2270
+ const result = queueLaneTerminalResume(current, now.toISOString(), LEAD_LANE_KEEP_ALIVE_BLOCKED_RESUME_TEXT, { allowBlockedResume: true, trigger: "resume" });
2271
+ if (!result.resumeQueued) {
2272
+ if (result.resumeSkippedReason !== undefined) {
2273
+ summary.skipped.push({ kind: "lane", id: lane.lane_id, why: "keep_alive_" + result.resumeSkippedReason });
2274
+ }
2275
+ return;
2276
+ }
2277
+ const queued = casLaneIf(hqRoot, latest.lane_id, (latestRecord) => latestRecord.version === expectedVersion && latestRecord.state === "blocked" && latestRecord.last_envelope?.decision === "blocked" && latestRecord.last_envelope.at === terminalAt, (current) => ({
2278
+ ...result.lane,
2279
+ version: current.version,
2280
+ pending_resume: result.lane.pending_resume === undefined
2281
+ ? undefined
2282
+ : { ...result.lane.pending_resume, keep_alive: true },
2283
+ keep_alive_blocked_resume_streak: priorResumes + 1,
2284
+ keep_alive_resume_count: (current.keep_alive_resume_count ?? 0) + 1,
2285
+ keep_alive_history: appendCappedHistory(current.keep_alive_history ?? [], {
2286
+ at: now.toISOString(),
2287
+ resumed_from: "blocked",
2288
+ account: current.account ?? null,
2289
+ }),
2290
+ keep_alive_last_resume_at: now.toISOString(),
2291
+ status_gate_history: appendCappedHistory(current.status_gate_history ?? [], {
2292
+ event: "keep_alive_blocked_resumed",
2293
+ at: now.toISOString(),
2294
+ terminal_at: terminalAt,
2295
+ resume_queued: true,
2296
+ }),
2297
+ }), deps.dryRun);
2298
+ if (queued.changed) {
2299
+ summary.resumed.push({ id: lane.lane_id, from: "blocked", to: "queued", reason: "keep_alive_blocked" });
2300
+ if (!deps.dryRun) {
2301
+ recordKeepAliveEvent(hqRoot, lane.lane_id, summary, {
2302
+ event: "keep_alive_resume",
2303
+ at: now.toISOString(),
2304
+ attempt: (lane.keep_alive_resume_count ?? 0) + 1,
2305
+ resumed_from: "blocked",
2306
+ reason: "backoff",
2307
+ ...(accountSelection !== undefined
2308
+ ? { account_selection: keepAliveAccountSelectionEvidence(accountSelection) }
2309
+ : {}),
2310
+ });
2311
+ }
2312
+ }
2313
+ }
2314
+ function appendBlockedKeepAliveHistory(hqRoot, lane, terminalAt, at, reason, deps) {
2315
+ if (deps.dryRun || lane.status_gate_history?.some((entry) => entry.event === "keep_alive_blocked_grace" && entry.terminal_at === terminalAt && entry.resume_skipped_reason === reason))
2316
+ return;
2317
+ casLaneIf(hqRoot, lane.lane_id, (current) => current.version === lane.version && current.state === "blocked" && current.last_envelope?.at === terminalAt, (current) => ({
2318
+ ...current,
2319
+ status_gate_history: appendCappedHistory(current.status_gate_history ?? [], {
2320
+ event: "keep_alive_blocked_grace",
2321
+ at,
2322
+ terminal_at: terminalAt,
2323
+ resume_skipped_reason: reason,
2324
+ }),
2325
+ }), false);
2326
+ }
2327
+ async function resumeKeepAlive(hqRoot, lanes, questions, summary, deps, now, flagByCompany, supersessionLineageAwareByCompany, blockedResumeFlagByCompany, immediateFirstResumeFlagByCompany, terminalKeepAliveCandidate) {
2328
+ const nowMs = now.getTime();
2329
+ const nowIso = now.toISOString();
2330
+ const config = loadLanesConfig(hqRoot);
2331
+ const resolvedCompanyByLaneVersion = new Map();
2332
+ const companyForLane = (candidate) => {
2333
+ const key = `${candidate.lane_id}\u0000${candidate.version}`;
2334
+ if (resolvedCompanyByLaneVersion.has(key))
2335
+ return resolvedCompanyByLaneVersion.get(key) ?? null;
2336
+ const company = resolveLaneCompany(hqRoot, candidate).company;
2337
+ resolvedCompanyByLaneVersion.set(key, company);
2338
+ return company;
2339
+ };
2340
+ const keepAliveEnabled = [...flagByCompany.values()].some((enabled) => enabled) || [...blockedResumeFlagByCompany.values()].some(Boolean);
2341
+ let retiredLaneIds = null;
2342
+ if (keepAliveEnabled) {
2343
+ try {
2344
+ retiredLaneIds = loadRetiredLaneIds(hqRoot);
2345
+ }
2346
+ catch (err) {
2347
+ markError(summary, err);
2348
+ }
2349
+ }
2350
+ for (const originalLane of lanes) {
2351
+ if (terminalKeepAliveCandidate && (originalLane.lane_id !== terminalKeepAliveCandidate.laneId ||
2352
+ originalLane.version !== terminalKeepAliveCandidate.version ||
2353
+ originalLane.state !== "done"))
2354
+ continue;
2355
+ if (!laneRunsHere(originalLane, deps.host))
2356
+ continue;
2357
+ if (originalLane.state === "killed" ||
2358
+ originalLane.state === "cancelled" ||
2359
+ originalLane.state === "failed") {
2360
+ if (originalLane.keep_alive === true &&
2361
+ flagByCompany.get(keepAliveCompanyKey(companyForLane(originalLane))) === true)
2362
+ recordKeepAliveSkip(hqRoot, originalLane, "terminal_state", summary, deps);
2363
+ continue;
2364
+ }
2365
+ if (originalLane.state === "blocked" && originalLane.keep_alive === true) {
2366
+ await resumeBlockedKeepAlive(hqRoot, originalLane, questions, summary, deps, now, flagByCompany, blockedResumeFlagByCompany, companyForLane, lanes, retiredLaneIds, supersessionLineageAwareByCompany);
2367
+ continue;
2368
+ }
2369
+ if ((originalLane.state !== "done" && originalLane.state !== "interrupted") ||
2370
+ (originalLane.keep_alive !== true && originalLane.worker_profile === undefined))
2371
+ continue;
2372
+ const initialState = originalLane.state;
2373
+ let lane = originalLane;
2374
+ if (initialState === "done") {
2375
+ const envelope = readLatestEnvelopeRecord(hqRoot, lane.lane_id)?.envelope;
2376
+ if (!envelope || envelope.decision !== "done")
2377
+ continue;
2378
+ const hasPendingWork = hasPendingKeepAliveWork(hqRoot, lane, questions);
2379
+ const hasQueuedWork = lane.keep_alive_standing === true && hasPendingWork;
2380
+ const verdict = doneKeepAliveVerdict(lane, envelope, hasQueuedWork, {
2381
+ hasPendingWork,
2382
+ nowMs,
2383
+ nextCheckAnchorMs: Date.parse(lane.last_envelope?.at ?? lane.updated_at),
2384
+ });
2385
+ if (verdict !== "eligible") {
2386
+ let skipLane = lane;
2387
+ if (envelope.story_complete === true && lane.keep_alive_standing !== true && hasKeepAliveProgress(lane)) {
2388
+ skipLane = casLaneIf(hqRoot, lane.lane_id, (current) => current.version === lane.version &&
2389
+ current.state === "done" &&
2390
+ current.keep_alive === lane.keep_alive, (current) => ({ ...clearedKeepAliveProgress(current), keep_alive_blocked_reason: undefined }), deps.dryRun).record;
2391
+ }
2392
+ if (verdict !== null)
2393
+ recordDoneKeepAlivePolicySkip(hqRoot, summary, skipLane, verdict, deps);
2394
+ continue;
2395
+ }
2396
+ }
2397
+ const company = companyForLane(lane);
2398
+ if (company === null) {
2399
+ if (lane.keep_alive === true) {
2400
+ summary.skipped.push({
2401
+ kind: "lane",
2402
+ id: lane.lane_id,
2403
+ why: "keep_alive_company_unresolved",
2404
+ });
2405
+ recordKeepAliveSkipEvent(hqRoot, lane, "company_unresolved", lane.version, summary, deps);
2406
+ }
2407
+ continue;
2408
+ }
2409
+ const companyKey = keepAliveCompanyKey(company);
2410
+ if (flagByCompany.get(companyKey) !== true) {
2411
+ if (lane.keep_alive === true) {
2412
+ summary.skipped.push({
2413
+ kind: "lane",
2414
+ id: lane.lane_id,
2415
+ why: "keep_alive_flag_off",
2416
+ });
2417
+ recordKeepAliveSkipEvent(hqRoot, lane, "keep_alive_flag_off", lane.version, summary, deps);
2418
+ }
2419
+ continue;
2420
+ }
2421
+ // Persist a stable timestamp for legacy interrupted rows before a later
2422
+ // keep-alive status write can advance their general updated_at clock. The
2423
+ // watcher classification is the interruption time; updated_at is only an
2424
+ // upper bound and can postdate explicit opt-in.
2425
+ if (initialState === "interrupted" && lane.keep_alive === true && lane.interrupted_at === undefined) {
2426
+ let interruptedAt;
2427
+ try {
2428
+ interruptedAt = readLatestInterruptedClassificationAt(hqRoot, lane);
2429
+ }
2430
+ catch (err) {
2431
+ const detail = `watcher classification read failed: ${err instanceof Error ? err.message : String(err)}`;
2432
+ recordKeepAliveSkip(hqRoot, lane, "missing_interruption_time", summary, deps, detail);
2433
+ const skip = summary.skipped[summary.skipped.length - 1];
2434
+ if (skip) {
2435
+ summary.ok = false;
2436
+ logSkip(skip);
2437
+ }
2438
+ continue;
2439
+ }
2440
+ if (interruptedAt === undefined) {
2441
+ recordKeepAliveSkip(hqRoot, lane, "missing_interruption_time", summary, deps, "no latest matching watcher classification event");
2442
+ const skip = summary.skipped[summary.skipped.length - 1];
2443
+ if (skip) {
2444
+ summary.ok = false;
2445
+ logSkip(skip);
2446
+ }
2447
+ continue;
2448
+ }
2449
+ try {
2450
+ const backfill = casLaneIf(hqRoot, lane.lane_id, (current) => current.version === lane.version &&
2451
+ current.state === "interrupted" &&
2452
+ current.keep_alive === true &&
2453
+ current.terminal_reason === lane.terminal_reason &&
2454
+ current.interrupted_at === undefined, (current) => ({ ...current, interrupted_at: interruptedAt }), deps.dryRun);
2455
+ lane = backfill.record;
2456
+ if (lane.interrupted_at === undefined) {
2457
+ recordKeepAliveSkip(hqRoot, lane, "missing_interruption_time", summary, deps, "lane changed before watcher classification timestamp could be recorded");
2458
+ const skip = summary.skipped[summary.skipped.length - 1];
2459
+ if (skip) {
2460
+ summary.ok = false;
2461
+ logSkip(skip);
2462
+ }
2463
+ continue;
2464
+ }
2465
+ }
2466
+ catch (err) {
2467
+ markDirty(summary, {
2468
+ kind: "lane",
2469
+ id: lane.lane_id,
2470
+ why: `keep-alive interruption timestamp backfill failed: ${err instanceof Error ? err.message : String(err)}`,
2471
+ });
2472
+ continue;
2473
+ }
2474
+ }
2475
+ if (initialState === "interrupted" && lane.terminal_reason !== "exited_with_partial_output") {
2476
+ recordKeepAliveSkip(hqRoot, lane, "terminal_state", summary, deps, lane.terminal_reason ?? "missing interruption reason");
2477
+ continue;
2478
+ }
2479
+ if (initialState === "done" && (lane.terminal_reason === PROVIDER_REJECTED_REASON ||
2480
+ lane.terminal_reason === "account_not_logged_in")) {
2481
+ recordKeepAliveSkip(hqRoot, lane, "terminal_state", summary, deps, lane.terminal_reason);
2482
+ continue;
2483
+ }
2484
+ if (lane.keep_alive !== true) {
2485
+ recordKeepAliveSkip(hqRoot, lane, "not_opted_in", summary, deps);
2486
+ continue;
2487
+ }
2488
+ const optedInAtMs = Date.parse(lane.keep_alive_opted_in_at ?? "");
2489
+ if (!Number.isFinite(optedInAtMs)) {
2490
+ recordKeepAliveSkip(hqRoot, lane, "missing_opt_in_time", summary, deps);
2491
+ continue;
2492
+ }
2493
+ let eligibilityAtMs;
2494
+ if (initialState === "done") {
2495
+ const latest = readLatestEnvelopeRecord(hqRoot, lane.lane_id);
2496
+ if (!latest || latest.envelope.decision !== "done")
2497
+ continue;
2498
+ const hasPendingWork = hasPendingKeepAliveWork(hqRoot, lane, questions);
2499
+ const hasQueuedWork = lane.keep_alive_standing === true && hasPendingWork;
2500
+ const verdict = doneKeepAliveVerdict(lane, latest.envelope, hasQueuedWork, {
2501
+ hasPendingWork,
2502
+ nowMs,
2503
+ nextCheckAnchorMs: Date.parse(lane.last_envelope?.at ?? lane.updated_at),
2504
+ });
2505
+ if (verdict !== "eligible") {
2506
+ let skipLane = lane;
2507
+ if (latest.envelope.story_complete === true && lane.keep_alive_standing !== true && hasKeepAliveProgress(lane)) {
2508
+ skipLane = casLaneIf(hqRoot, lane.lane_id, (current) => current.version === lane.version && current.state === "done", (current) => ({ ...clearedKeepAliveProgress(current), keep_alive_blocked_reason: undefined }), deps.dryRun).record;
2509
+ }
2510
+ if (verdict !== null)
2511
+ recordDoneKeepAlivePolicySkip(hqRoot, summary, skipLane, verdict, deps);
2512
+ continue;
2513
+ }
2514
+ try {
2515
+ eligibilityAtMs = latestEnvelopeMtimeMs(hqRoot, lane, latest.ref);
2516
+ }
2517
+ catch (err) {
2518
+ markDirty(summary, {
2519
+ kind: "lane",
2520
+ id: lane.lane_id,
2521
+ why: `keep-alive envelope timestamp unavailable: ${err instanceof Error ? err.message : String(err)}`,
2522
+ });
2523
+ continue;
2524
+ }
2525
+ }
2526
+ else {
2527
+ eligibilityAtMs = Date.parse(lane.interrupted_at ?? "");
2528
+ if (!Number.isFinite(eligibilityAtMs)) {
2529
+ recordKeepAliveSkip(hqRoot, lane, "missing_interruption_time", summary, deps);
2530
+ continue;
2531
+ }
2532
+ }
2533
+ const initialTimeBlockReason = initialState === "done"
2534
+ ? "envelope_before_opt_in"
2535
+ : "interruption_before_opt_in";
2536
+ if (eligibilityAtMs <= optedInAtMs) {
2537
+ recordKeepAliveSkip(hqRoot, lane, initialTimeBlockReason, summary, deps);
2538
+ continue;
2539
+ }
2540
+ if (retiredLaneIds === null) {
2541
+ recordKeepAliveSkip(hqRoot, lane, "retired_list_invalid", summary, deps);
2542
+ continue;
2543
+ }
2544
+ const exclusion = keepAliveExclusion(lane, lanes, retiredLaneIds, supersessionLineageAwareByCompany.get(companyKey) === true, companyForLane);
2545
+ if (exclusion !== undefined) {
2546
+ recordKeepAliveSkip(hqRoot, lane, exclusion, summary, deps);
2547
+ continue;
2548
+ }
2549
+ const providerSessionId = lane.provider_session_id ?? lane.last_resume?.session_id;
2550
+ if (!providerSupportsSessionResume(lane.provider) ||
2551
+ typeof providerSessionId !== "string" ||
2552
+ providerSessionId.trim().length === 0) {
2553
+ if (initialState === "interrupted") {
2554
+ recordKeepAliveSkip(hqRoot, lane, "terminal_state", summary, deps, "provider session cannot be resumed");
2555
+ }
2556
+ continue;
2557
+ }
2558
+ // This second read is intentionally independent of the pass snapshot. The
2559
+ // final timestamp comparison below closes the opt-in race before the CAS.
2560
+ const rechecked = deps.readKeepAliveRecheckLane?.(hqRoot, lane.lane_id) ?? readLane(hqRoot, lane.lane_id);
2561
+ // Dry-run backfills only in memory. Carry that simulated legacy timestamp
2562
+ // through the fresh read when the stored lane version has not changed.
2563
+ let current = deps.dryRun &&
2564
+ initialState === "interrupted" &&
2565
+ originalLane.interrupted_at === undefined &&
2566
+ lane.interrupted_at !== undefined &&
2567
+ rechecked.version === lane.version &&
2568
+ rechecked.interrupted_at === undefined
2569
+ ? { ...rechecked, interrupted_at: lane.interrupted_at }
2570
+ : rechecked;
2571
+ if (current.state !== initialState || !laneRunsHere(current, deps.host))
2572
+ continue;
2573
+ if (current.keep_alive !== true) {
2574
+ recordKeepAliveSkip(hqRoot, current, "not_opted_in", summary, deps);
2575
+ continue;
2576
+ }
2577
+ if (initialState === "interrupted" && current.terminal_reason !== "exited_with_partial_output") {
2578
+ recordKeepAliveSkip(hqRoot, current, "terminal_state", summary, deps, current.terminal_reason ?? "missing interruption reason");
2579
+ continue;
2580
+ }
2581
+ let currentEligibilityAtMs;
2582
+ if (initialState === "done") {
2583
+ const currentEnvelope = readLatestEnvelopeRecord(hqRoot, current.lane_id);
2584
+ if (!currentEnvelope || currentEnvelope.envelope.decision !== "done")
2585
+ continue;
2586
+ const hasPendingWork = hasPendingKeepAliveWork(hqRoot, current, questions);
2587
+ const hasQueuedWork = current.keep_alive_standing === true && hasPendingWork;
2588
+ const verdict = doneKeepAliveVerdict(current, currentEnvelope.envelope, hasQueuedWork, {
2589
+ hasPendingWork,
2590
+ nowMs,
2591
+ nextCheckAnchorMs: Date.parse(current.last_envelope?.at ?? current.updated_at),
2592
+ });
2593
+ if (verdict !== "eligible") {
2594
+ let skipLane = current;
2595
+ if (currentEnvelope.envelope.story_complete === true && current.keep_alive_standing !== true && hasKeepAliveProgress(current)) {
2596
+ skipLane = casLaneIf(hqRoot, current.lane_id, (latestLane) => latestLane.version === current.version &&
2597
+ latestLane.state === "done" &&
2598
+ latestLane.keep_alive === true &&
2599
+ latestLane.keep_alive_opted_in_at === current.keep_alive_opted_in_at, (latestLane) => ({ ...clearedKeepAliveProgress(latestLane), keep_alive_blocked_reason: undefined }), deps.dryRun).record;
2600
+ }
2601
+ if (verdict !== null)
2602
+ recordDoneKeepAlivePolicySkip(hqRoot, summary, skipLane, verdict, deps);
2603
+ continue;
2604
+ }
2605
+ try {
2606
+ currentEligibilityAtMs = latestEnvelopeMtimeMs(hqRoot, current, currentEnvelope.ref);
2607
+ }
2608
+ catch (err) {
2609
+ markDirty(summary, {
2610
+ kind: "lane",
2611
+ id: current.lane_id,
2612
+ why: `keep-alive envelope timestamp unavailable: ${err instanceof Error ? err.message : String(err)}`,
2613
+ });
2614
+ continue;
2615
+ }
2616
+ }
2617
+ else {
2618
+ currentEligibilityAtMs = Date.parse(current.interrupted_at ?? "");
2619
+ if (!Number.isFinite(currentEligibilityAtMs)) {
2620
+ recordKeepAliveSkip(hqRoot, current, "missing_interruption_time", summary, deps);
2621
+ continue;
2622
+ }
2623
+ }
2624
+ const currentOptedInAtMs = Date.parse(current.keep_alive_opted_in_at ?? "");
2625
+ if (!Number.isFinite(currentOptedInAtMs)) {
2626
+ recordKeepAliveSkip(hqRoot, current, "missing_opt_in_time", summary, deps);
2627
+ continue;
2628
+ }
2629
+ const currentTimeBlockReason = initialState === "done"
2630
+ ? "envelope_before_opt_in"
2631
+ : "interruption_before_opt_in";
2632
+ if (currentEligibilityAtMs <= currentOptedInAtMs) {
2633
+ recordKeepAliveSkip(hqRoot, current, currentTimeBlockReason, summary, deps);
2634
+ continue;
2635
+ }
2636
+ if (retiredLaneIds === null) {
2637
+ recordKeepAliveSkip(hqRoot, current, "retired_list_invalid", summary, deps);
2638
+ continue;
2639
+ }
2640
+ const currentLanes = loadLaneRecords(hqRoot, summary);
2641
+ const currentCompany = companyForLane(current);
2642
+ if (currentCompany === null) {
2643
+ summary.skipped.push({
2644
+ kind: "lane",
2645
+ id: current.lane_id,
2646
+ why: "keep_alive_company_unresolved",
2647
+ });
2648
+ recordKeepAliveSkipEvent(hqRoot, current, "company_unresolved", current.version, summary, deps);
2649
+ continue;
2650
+ }
2651
+ const currentCompanyKey = keepAliveCompanyKey(currentCompany);
2652
+ if (flagByCompany.get(currentCompanyKey) !== true) {
2653
+ summary.skipped.push({
2654
+ kind: "lane",
2655
+ id: current.lane_id,
2656
+ why: "keep_alive_flag_off",
2657
+ });
2658
+ recordKeepAliveSkipEvent(hqRoot, current, "keep_alive_flag_off", current.version, summary, deps);
2659
+ continue;
2660
+ }
2661
+ const currentExclusion = keepAliveExclusion(current, currentLanes, retiredLaneIds, supersessionLineageAwareByCompany.get(currentCompanyKey) === true, companyForLane);
2662
+ if (currentExclusion !== undefined) {
2663
+ recordKeepAliveSkip(hqRoot, current, currentExclusion, summary, deps);
2664
+ continue;
2665
+ }
2666
+ if (current.keep_alive_blocked_reason === "superseded") {
2667
+ // The fresh exclusion check found no active successor. Clear the stale
2668
+ // reason before any later exit (backoff, queue full, retry cap, hire
2669
+ // refusal) so the watcher keeps treating the lane as resumable.
2670
+ const cleared = casLaneIf(hqRoot, current.lane_id, (latestLane) => latestLane.version === current.version &&
2671
+ latestLane.state === current.state &&
2672
+ latestLane.keep_alive === true &&
2673
+ latestLane.keep_alive_opted_in_at === current.keep_alive_opted_in_at &&
2674
+ latestLane.keep_alive_blocked_reason === "superseded", (latestLane) => ({ ...latestLane, keep_alive_blocked_reason: undefined }), deps.dryRun);
2675
+ if (!cleared.changed)
2676
+ continue;
2677
+ current = cleared.record;
2678
+ }
2679
+ const currentSessionId = current.provider_session_id ?? current.last_resume?.session_id;
2680
+ if (!providerSupportsSessionResume(current.provider) ||
2681
+ typeof currentSessionId !== "string" ||
2682
+ currentSessionId.trim().length === 0)
2683
+ continue;
2684
+ const resetByHuman = keepAliveProgressResetByHuman(current);
2685
+ const attempts = resetByHuman ? 0 : current.keep_alive_resume_count ?? 0;
2686
+ if (attempts >= LEAD_LANE_KEEP_ALIVE_MAX_RESUMES) {
2687
+ if (current.keep_alive_exhausted_at !== undefined)
2688
+ continue;
2689
+ const exhausted = casLaneIf(hqRoot, current.lane_id, (latestLane) => latestLane.version === current.version &&
2690
+ latestLane.state === current.state &&
2691
+ latestLane.keep_alive === true &&
2692
+ latestLane.keep_alive_opted_in_at === current.keep_alive_opted_in_at &&
2693
+ (latestLane.keep_alive_resume_count ?? 0) >= LEAD_LANE_KEEP_ALIVE_MAX_RESUMES &&
2694
+ latestLane.keep_alive_exhausted_at === undefined, (latestLane) => ({ ...latestLane, keep_alive_exhausted_at: nowIso }), deps.dryRun);
2695
+ if (exhausted.changed && !deps.dryRun) {
2696
+ recordKeepAliveEvent(hqRoot, current.lane_id, summary, {
2697
+ event: "keep_alive_exhausted",
2698
+ at: nowIso,
2699
+ attempt: attempts,
2700
+ resumed_from: current.state,
2701
+ });
2702
+ }
2703
+ continue;
2704
+ }
2705
+ const attempt = attempts + 1;
2706
+ // A done turn ends no later than its done envelope. Bookkeeping writes
2707
+ // (such as clearing a stale block) move updated_at but not the envelope,
2708
+ // so they cannot push the retry clock forward.
2709
+ const terminalAtMs = initialState === "done"
2710
+ ? Math.min(Date.parse(current.updated_at), currentEligibilityAtMs)
2711
+ : currentEligibilityAtMs;
2712
+ const immediateFirstResumeEnabled = initialState === "done" &&
2713
+ immediateFirstResumeFlagByCompany.get(keepAliveCompanyKey(companyForLane(current))) === true;
2714
+ const attemptDelayMs = immediateFirstResumeEnabled && attempt === 1
2715
+ ? LEAD_LANE_KEEP_ALIVE_FIRST_RESUME_SETTLE_MS
2716
+ : leadLaneKeepAliveBackoffMs(immediateFirstResumeEnabled ? Math.max(1, attempt - 1) : attempt);
2717
+ const attemptEligibleMs = terminalAtMs + attemptDelayMs;
2718
+ const storedNextMs = resetByHuman
2719
+ ? Number.NaN
2720
+ : Date.parse(current.keep_alive_next_eligible_at ?? "");
2721
+ const nextEligibleMs = Number.isFinite(storedNextMs)
2722
+ ? Math.max(attemptEligibleMs, storedNextMs)
2723
+ : attemptEligibleMs;
2724
+ if (nowMs < nextEligibleMs) {
2725
+ let skipLane = current;
2726
+ if (resetByHuman && hasKeepAliveProgress(current)) {
2727
+ const clearedProgress = casLaneIf(hqRoot, current.lane_id, (latestLane) => latestLane.version === current.version &&
2728
+ latestLane.state === current.state &&
2729
+ latestLane.keep_alive === true &&
2730
+ latestLane.keep_alive_opted_in_at === current.keep_alive_opted_in_at, (latestLane) => clearedKeepAliveProgress(latestLane), deps.dryRun);
2731
+ skipLane = clearedProgress.record;
2732
+ }
2733
+ recordKeepAliveSkipEvent(hqRoot, skipLane, "backoff", skipLane.version, summary, deps, new Date(nextEligibleMs).toISOString());
2734
+ continue;
2735
+ }
2736
+ const nextEligibleAt = new Date(nowMs + leadLaneKeepAliveBackoffMs(immediateFirstResumeEnabled ? attempt : attempt + 1)).toISOString();
2737
+ let resumeReason = "backoff";
2738
+ if (initialState === "done") {
2739
+ if (hasPendingKeepAliveWork(hqRoot, current, questions)) {
2740
+ resumeReason = "pending_work";
2741
+ }
2742
+ else {
2743
+ const finalEnvelope = readLatestEnvelopeRecord(hqRoot, current.lane_id)?.envelope;
2744
+ const nextCheckMs = Date.parse(finalEnvelope?.next_check_at ?? "");
2745
+ const anchorMs = Date.parse(current.last_envelope?.at ?? current.updated_at);
2746
+ if (Number.isFinite(nextCheckMs) && Number.isFinite(anchorMs)) {
2747
+ const cappedNextCheckMs = Math.min(nextCheckMs, anchorMs + LEAD_LANE_KEEP_ALIVE_NEXT_CHECK_MAX_DELAY_MS);
2748
+ if (cappedNextCheckMs > nextEligibleMs && cappedNextCheckMs <= nowMs) {
2749
+ resumeReason = "next_check_at_due";
2750
+ }
2751
+ }
2752
+ }
2753
+ }
2754
+ const queuedCount = loadLaneRecords(hqRoot, summary).filter((queuedLane) => queuedLane.state === "queued" &&
2755
+ queuedLane.provider === current.provider &&
2756
+ laneRunsHere(queuedLane, deps.host) &&
2757
+ !hasFreshAdmissionReservation(queuedLane, nowMs)).length;
2758
+ const maxQueued = config.providers[current.provider].max_queued;
2759
+ if (queuedCount >= maxQueued) {
2760
+ summary.skipped.push({
2761
+ kind: "lane",
2762
+ id: current.lane_id,
2763
+ why: `lane_queue_full: ${current.provider} queue has ${queuedCount}/${maxQueued} lanes`,
2764
+ });
2765
+ recordKeepAliveSkipEvent(hqRoot, current, "lane_queue_full", current.version, summary, deps, `${current.provider} queue has ${queuedCount}/${maxQueued} lanes`);
2766
+ continue;
2767
+ }
2768
+ const seniorId = current.senior.kind === "lane" ? current.senior.id : undefined;
2769
+ let finishedChildrenDoNotCount = false;
2770
+ if (seniorId !== undefined) {
2771
+ let grantPreflight;
2772
+ try {
2773
+ const companyResolution = resolveLaneCompany(hqRoot, current);
2774
+ const company = companyResolution.company ?? undefined;
2775
+ finishedChildrenDoNotCount = await readLanesRegistryFlagFailOff(LANE_HIRE_GRANT_FINISHED_LANES_FLAG, {
2776
+ company,
2777
+ ...(company ? { companyUidSource: laneCompanyUidSource(companyResolution.source) } : {}),
2778
+ env: deps.env,
2779
+ }, deps.readRegistryFlag ?? readLanesRegistryFlag);
2780
+ grantPreflight = withHireGrantAdmission(hqRoot, seniorId, (snapshot) => {
2781
+ const latestLane = readLane(hqRoot, current.lane_id);
2782
+ if (latestLane.version !== current.version ||
2783
+ latestLane.state !== current.state ||
2784
+ latestLane.keep_alive !== true ||
2785
+ latestLane.keep_alive_opted_in_at !== current.keep_alive_opted_in_at ||
2786
+ latestLane.senior.kind !== "lane" ||
2787
+ latestLane.senior.id !== seniorId)
2788
+ return { kind: "stale" };
2789
+ const refusal = terminalChildResumeHireGrantError(latestLane, snapshot, finishedChildrenDoNotCount);
2790
+ return refusal === null
2791
+ ? { kind: "eligible" }
2792
+ : { kind: "refused", error: refusal.error, message: refusal.message };
2793
+ }, undefined, new Set(), finishedChildrenDoNotCount);
2794
+ }
2795
+ catch (err) {
2796
+ markDirty(summary, {
2797
+ kind: "lane",
2798
+ id: current.lane_id,
2799
+ why: `keep-alive hire admission preflight failed: ${err instanceof Error ? err.message : String(err)}`,
2800
+ });
2801
+ continue;
2802
+ }
2803
+ if (grantPreflight.kind === "stale")
2804
+ continue;
2805
+ if (grantPreflight.kind === "refused") {
2806
+ summary.skipped.push({
2807
+ kind: "lane",
2808
+ id: current.lane_id,
2809
+ why: `keep_alive_hire_admission: ${grantPreflight.error}: ${grantPreflight.message}`,
2810
+ });
2811
+ recordKeepAliveSkipEvent(hqRoot, current, "hire_admission", current.version, summary, deps, `${grantPreflight.error}: ${grantPreflight.message}`);
2812
+ continue;
2813
+ }
2814
+ }
2815
+ const resumedFrom = current.state === "done" ? "done" : "interrupted";
2816
+ const resumeText = resumedFrom === "interrupted"
2817
+ ? LEAD_LANE_KEEP_ALIVE_INTERRUPTED_RESUME_TEXT
2818
+ : LEAD_LANE_KEEP_ALIVE_RESUME_TEXT;
2819
+ const carriesAnsweredQuestions = resumedFrom === "done" &&
2820
+ current.keep_alive_standing === true &&
2821
+ undeliveredAnsweredQuestions(current, questions).length > 0;
2822
+ if (carriesAnsweredQuestions && !deps.dryRun) {
2823
+ writeAnsweredQuestions(hqRoot, current, questions);
2824
+ }
2825
+ let accountSelectionFailure;
2826
+ let accountSelection;
2827
+ if (current.provider === "claude" && deps.prepareKeepAliveClaudeResume !== undefined) {
2828
+ const optInBeforePreparation = current.keep_alive_opted_in_at;
2829
+ let preparedLane;
2830
+ try {
2831
+ const prepared = await deps.prepareKeepAliveClaudeResume(current);
2832
+ preparedLane = prepared.lane;
2833
+ accountSelectionFailure = prepared.selectionFailure;
2834
+ accountSelection = prepared.accountSelection;
2835
+ }
2836
+ catch (err) {
2837
+ // Account balancing is best-effort for keep-alive. A failure to read
2838
+ // balance inputs must not prevent the existing session from resuming.
2839
+ try {
2840
+ preparedLane = readLane(hqRoot, current.lane_id);
2841
+ }
2842
+ catch (readErr) {
2843
+ markDirty(summary, {
2844
+ kind: "lane",
2845
+ id: current.lane_id,
2846
+ why: `keep-alive account selection failed and lane could not be re-read: ${readErr instanceof Error ? readErr.message : String(readErr)}`,
2847
+ });
2848
+ continue;
2849
+ }
2850
+ accountSelectionFailure = err instanceof Error ? err.message : String(err);
2851
+ }
2852
+ const preparedSessionId = preparedLane.provider_session_id ?? preparedLane.last_resume?.session_id;
2853
+ if (preparedLane.state !== initialState ||
2854
+ preparedLane.keep_alive !== true ||
2855
+ !laneRunsHere(preparedLane, deps.host) ||
2856
+ preparedSessionId !== currentSessionId)
2857
+ continue;
2858
+ let latest;
2859
+ try {
2860
+ latest = deps.dryRun
2861
+ ? (deps.readKeepAliveRecheckLane?.(hqRoot, current.lane_id) ?? readLane(hqRoot, current.lane_id))
2862
+ : readLane(hqRoot, current.lane_id);
2863
+ }
2864
+ catch (err) {
2865
+ markDirty(summary, {
2866
+ kind: "lane",
2867
+ id: current.lane_id,
2868
+ why: `keep-alive eligibility recheck failed after account selection: ${err instanceof Error ? err.message : String(err)}`,
2869
+ });
2870
+ continue;
2871
+ }
2872
+ if (latest.state !== initialState || !laneRunsHere(latest, deps.host))
2873
+ continue;
2874
+ if (latest.keep_alive !== true) {
2875
+ recordKeepAliveSkip(hqRoot, latest, "not_opted_in", summary, deps);
2876
+ continue;
2877
+ }
2878
+ if (latest.keep_alive_opted_in_at !== optInBeforePreparation) {
2879
+ summary.skipped.push({
2880
+ kind: "lane",
2881
+ id: latest.lane_id,
2882
+ why: "keep_alive_eligibility_changed_during_account_selection",
2883
+ });
2884
+ continue;
2885
+ }
2886
+ const latestSessionId = latest.provider_session_id ?? latest.last_resume?.session_id;
2887
+ if (latestSessionId !== currentSessionId)
2888
+ continue;
2889
+ let latestEligibilityAtMs;
2890
+ if (initialState === "done") {
2891
+ const envelope = readLatestEnvelopeRecord(hqRoot, latest.lane_id);
2892
+ if (!envelope || envelope.envelope.decision !== "done")
2893
+ continue;
2894
+ const hasPendingWork = hasPendingKeepAliveWork(hqRoot, latest, questions);
2895
+ const finalDoneVerdict = doneKeepAliveVerdict(latest, envelope.envelope, latest.keep_alive_standing === true && hasPendingWork, {
2896
+ hasPendingWork,
2897
+ nowMs,
2898
+ nextCheckAnchorMs: Date.parse(latest.last_envelope?.at ?? latest.updated_at),
2899
+ });
2900
+ if (finalDoneVerdict !== "eligible") {
2901
+ if (finalDoneVerdict === "next_check_at") {
2902
+ recordDoneKeepAlivePolicySkip(hqRoot, summary, latest, finalDoneVerdict, deps);
2903
+ }
2904
+ continue;
2905
+ }
2906
+ try {
2907
+ latestEligibilityAtMs = latestEnvelopeMtimeMs(hqRoot, latest, envelope.ref);
2908
+ }
2909
+ catch (err) {
2910
+ markDirty(summary, {
2911
+ kind: "lane",
2912
+ id: latest.lane_id,
2913
+ why: `keep-alive envelope timestamp unavailable after account selection: ${err instanceof Error ? err.message : String(err)}`,
2914
+ });
2915
+ continue;
2916
+ }
2917
+ }
2918
+ else {
2919
+ latestEligibilityAtMs = Date.parse(latest.interrupted_at ?? "");
2920
+ }
2921
+ const latestOptedInAtMs = Date.parse(latest.keep_alive_opted_in_at ?? "");
2922
+ if (!Number.isFinite(latestEligibilityAtMs) || !Number.isFinite(latestOptedInAtMs))
2923
+ continue;
2924
+ if (latestEligibilityAtMs <= latestOptedInAtMs) {
2925
+ recordKeepAliveSkip(hqRoot, latest, initialState === "done" ? "envelope_before_opt_in" : "interruption_before_opt_in", summary, deps);
2926
+ continue;
2927
+ }
2928
+ current = deps.dryRun ? preparedLane : latest;
2929
+ }
2930
+ if (accountSelectionFailure !== undefined) {
2931
+ summary.skipped.push({
2932
+ kind: "lane",
2933
+ id: current.lane_id,
2934
+ why: `keep_alive_account_selection_failed: ${accountSelectionFailure}`,
2935
+ });
2936
+ if (!deps.dryRun) {
2937
+ recordKeepAliveSkipEvent(hqRoot, current, "account_selection_failed", current.version, summary, deps, accountSelectionFailure);
2938
+ recordKeepAliveEvent(hqRoot, current.lane_id, summary, {
2939
+ event: "keep_alive_account_selection_failed",
2940
+ at: nowIso,
2941
+ account: current.account ?? null,
2942
+ reason: accountSelectionFailure,
2943
+ });
2944
+ }
2945
+ }
2946
+ const queue = (expectedSeniorId) => casLaneIf(hqRoot, current.lane_id, (latestLane) => latestLane.version === current.version &&
2947
+ latestLane.state === current.state &&
2948
+ latestLane.keep_alive === true &&
2949
+ latestLane.keep_alive_opted_in_at === current.keep_alive_opted_in_at &&
2950
+ (expectedSeniorId === undefined ||
2951
+ (latestLane.senior.kind === "lane" && latestLane.senior.id === expectedSeniorId)), (latestLane) => ({
2952
+ ...latestLane,
2953
+ state: "queued",
2954
+ queued_reason: undefined,
2955
+ queued_details: undefined,
2956
+ queued_at: nowIso,
2957
+ admission_reserved_at: undefined,
2958
+ terminal_reason: undefined,
2959
+ terminal_details: undefined,
2960
+ pending_resume: {
2961
+ session_id: currentSessionId,
2962
+ trigger: carriesAnsweredQuestions ? "answered_questions" : "resume",
2963
+ keep_alive: true,
2964
+ resumed_from: resumedFrom,
2965
+ text: resumeText,
2966
+ compact: true,
2967
+ at: nowIso,
2968
+ },
2969
+ keep_alive_resume_count: attempt,
2970
+ keep_alive_history: appendCappedHistory(latestLane.keep_alive_history ?? [], {
2971
+ at: nowIso,
2972
+ resumed_from: resumedFrom,
2973
+ account: latestLane.account ?? null,
2974
+ }),
2975
+ keep_alive_last_resume_at: nowIso,
2976
+ keep_alive_next_eligible_at: nextEligibleAt,
2977
+ keep_alive_exhausted_at: undefined,
2978
+ keep_alive_blocked_reason: undefined,
2979
+ }), deps.dryRun);
2980
+ let queued;
2981
+ if (seniorId) {
2982
+ try {
2983
+ queued = withHireGrantAdmission(hqRoot, seniorId, (snapshot) => {
2984
+ const latestLane = readLane(hqRoot, current.lane_id);
2985
+ if (latestLane.version !== current.version ||
2986
+ latestLane.senior.kind !== "lane" ||
2987
+ latestLane.senior.id !== seniorId)
2988
+ return { changed: false, record: latestLane };
2989
+ const refusal = terminalChildResumeHireGrantError(latestLane, snapshot, finishedChildrenDoNotCount);
2990
+ if (refusal !== null) {
2991
+ summary.skipped.push({
2992
+ kind: "lane",
2993
+ id: current.lane_id,
2994
+ why: `keep_alive_hire_admission: ${refusal.error}: ${refusal.message}`,
2995
+ });
2996
+ return { changed: false, record: latestLane };
2997
+ }
2998
+ return queue(seniorId);
2999
+ }, undefined, new Set(), finishedChildrenDoNotCount);
3000
+ }
3001
+ catch (err) {
3002
+ markDirty(summary, {
3003
+ kind: "lane",
3004
+ id: current.lane_id,
3005
+ why: `keep-alive hire admission failed: ${err instanceof Error ? err.message : String(err)}`,
3006
+ });
3007
+ continue;
3008
+ }
3009
+ }
3010
+ else {
3011
+ queued = queue(seniorId);
3012
+ }
3013
+ if (!queued.changed)
3014
+ continue;
3015
+ summary.resumed.push({
3016
+ id: current.lane_id,
3017
+ from: resumedFrom,
3018
+ to: "queued",
3019
+ reason: "keep_alive",
3020
+ });
3021
+ if (!deps.dryRun) {
3022
+ recordKeepAliveEvent(hqRoot, current.lane_id, summary, {
3023
+ event: "keep_alive_resume",
3024
+ at: nowIso,
3025
+ attempt,
3026
+ next_eligible_at: nextEligibleAt,
3027
+ resumed_from: resumedFrom,
3028
+ reason: resumeReason,
3029
+ ...(accountSelection !== undefined
3030
+ ? { account_selection: keepAliveAccountSelectionEvidence(accountSelection) }
3031
+ : {}),
3032
+ });
3033
+ }
3034
+ }
3035
+ }
3036
+ async function resumeAwaiting(hqRoot, lanes, questions, summary, deps, onlyLaneIds) {
3037
+ const config = loadLanesConfig(hqRoot);
3038
+ const awaiting = lanes.filter((lane) => lane.state === "awaiting_input" &&
3039
+ laneRunsHere(lane, deps.host) &&
3040
+ (onlyLaneIds === undefined || onlyLaneIds.has(lane.lane_id)));
3041
+ if (awaiting.length === 0 || deps.skipAdmissionWithoutCpuSample === true)
3042
+ return;
3043
+ const throttleDeps = deps.throttleDeps ?? defaultThrottleDeps(deps.env ?? process.env);
3044
+ let throttleDecision;
3045
+ if (deps.dryRun) {
3046
+ const loaded = readThrottleState(throttleStatePath(hqRoot), throttleDeps);
3047
+ const state = loaded === "missing" ? emptyThrottleState() : loaded;
3048
+ throttleDecision = evaluateThrottle({
3049
+ busy: deps.cpuBusyFraction !== undefined
3050
+ ? deps.cpuBusyFraction
3051
+ : deps.cpuObservation?.busy ?? await resolveCpuBusyFraction(throttleDeps),
3052
+ availableMemMb: readAvailableMemMb(throttleDeps),
3053
+ cpuCheckEnabled: isCpuCheckEnabled(throttleDeps.env),
3054
+ state,
3055
+ throttle: config.throttle,
3056
+ nowMs: deps.now().getTime(),
3057
+ });
3058
+ }
3059
+ else {
3060
+ const throttle = await decideAdmission(hqRoot, config.throttle, throttleDeps, deps.cpuBusyFraction, deps.cpuObservation);
3061
+ throttleDecision = throttle.decision;
3062
+ }
3063
+ const spawnFn = deps.spawn ?? (async (root, lane) => spawnLaneWorker(root, lane, {
3064
+ ...spawnDepsForHost(deps),
3065
+ lowDiskGuardEnabled: await lowDiskGuardEnabledForLane(root, lane, deps.readRegistryFlag),
3066
+ }));
3067
+ const nowMs = deps.now().getTime();
3068
+ const fallbackAnswerAt = new Date(nowMs).toISOString();
3069
+ const answerAtByLane = new Map();
3070
+ for (const question of questions) {
3071
+ if (question.status !== "answered")
3072
+ continue;
3073
+ const at = question.answer?.answered_at ?? question.asked_at;
3074
+ const previous = answerAtByLane.get(question.origin.id);
3075
+ if (previous === undefined || Date.parse(at) > Date.parse(previous)) {
3076
+ answerAtByLane.set(question.origin.id, at);
3077
+ }
3078
+ }
3079
+ const latestAnswerAt = (lane) => answerAtByLane.get(lane.lane_id) ?? fallbackAnswerAt;
3080
+ awaiting.sort((a, b) => {
3081
+ const byAnswerTime = Date.parse(latestAnswerAt(a)) - Date.parse(latestAnswerAt(b));
3082
+ return byAnswerTime || compareLaneQueueOrder(a, b);
3083
+ });
3084
+ const capacityLanes = lanes.map((lane) => {
3085
+ try {
3086
+ return readLane(hqRoot, lane.lane_id);
3087
+ }
3088
+ catch {
3089
+ return lane;
3090
+ }
3091
+ });
3092
+ const running = capacityLanes.filter((lane) => (lane.state === "running" ||
3093
+ hasFreshAdmissionReservation(lane, nowMs) ||
3094
+ hasActivePendingResume(lane, nowMs)) &&
3095
+ laneRunsHere(lane, deps.host));
3096
+ for (const lane of awaiting) {
3097
+ const current = readLane(hqRoot, lane.lane_id);
3098
+ if (current.state !== "awaiting_input")
3099
+ continue;
3100
+ const pending = questions.filter((q) => q.origin.id === lane.lane_id && q.status === "pending");
3101
+ const answered = questions.filter((q) => q.origin.id === lane.lane_id && q.status === "answered");
3102
+ if (pending.length > 0 || answered.length === 0)
3103
+ continue;
3104
+ try {
3105
+ if (current.senior.kind === "lane" && current.senior.id !== undefined) {
3106
+ const companyResolution = resolveLaneCompany(hqRoot, current);
3107
+ const company = companyResolution.company ?? undefined;
3108
+ const finishedChildrenDoNotCount = await readLanesRegistryFlagFailOff(LANE_HIRE_GRANT_FINISHED_LANES_FLAG, {
3109
+ company,
3110
+ ...(company ? { companyUidSource: laneCompanyUidSource(companyResolution.source) } : {}),
3111
+ env: deps.env,
3112
+ }, deps.readRegistryFlag ?? readLanesRegistryFlag);
3113
+ if (finishedChildrenDoNotCount) {
3114
+ const admission = withHireGrantAdmission(hqRoot, current.senior.id, (snapshot) => {
3115
+ const latest = readLane(hqRoot, current.lane_id);
3116
+ if (latest.version !== current.version || latest.state !== "awaiting_input") {
3117
+ return { kind: "stale" };
3118
+ }
3119
+ const refusal = terminalChildResumeHireGrantError(latest, snapshot, true);
3120
+ return refusal === null
3121
+ ? { kind: "eligible" }
3122
+ : { kind: "refused", error: refusal.error, message: refusal.message };
3123
+ }, undefined, new Set(), true);
3124
+ if (admission.kind === "stale")
3125
+ continue;
3126
+ if (admission.kind === "refused") {
3127
+ if (!deps.dryRun)
3128
+ writeAnsweredQuestions(hqRoot, current, questions);
3129
+ summary.skipped.push({
3130
+ kind: "lane",
3131
+ id: current.lane_id,
3132
+ why: `${admission.error}: ${admission.message}`,
3133
+ });
3134
+ continue;
3135
+ }
3136
+ }
3137
+ }
3138
+ let profileMaxConcurrent;
3139
+ if (current.worker_profile !== undefined) {
3140
+ profileMaxConcurrent = loadWorkerProfile(hqRoot, current.worker_profile).meta.max_concurrent;
3141
+ }
3142
+ const capReason = laneAdmissionCapReason(running, current, config, deps.host, profileMaxConcurrent, nowMs);
3143
+ const answeredAt = latestAnswerAt(current);
3144
+ const queueCandidate = { ...current, state: "queued", queued_at: answeredAt };
3145
+ const earlierQueued = capacityLanes.some((queued) => queued.lane_id !== current.lane_id &&
3146
+ queued.state === "queued" &&
3147
+ queued.provider === current.provider &&
3148
+ laneRunsHere(queued, deps.host) &&
3149
+ compareLaneQueueOrder(queued, queueCandidate) < 0);
3150
+ const queueReason = !throttleDecision.admit
3151
+ ? "throttle"
3152
+ : capReason ?? (earlierQueued ? "queue_position" : undefined);
3153
+ if (queueReason !== undefined) {
3154
+ const queuedCount = capacityLanes.filter((queued) => queued.state === "queued" &&
3155
+ queued.provider === current.provider &&
3156
+ laneRunsHere(queued, deps.host) &&
3157
+ !hasFreshAdmissionReservation(queued, nowMs)).length;
3158
+ const maxQueued = config.providers[current.provider].max_queued;
3159
+ if (queuedCount >= maxQueued) {
3160
+ summary.skipped.push({
3161
+ kind: "lane",
3162
+ id: current.lane_id,
3163
+ why: `lane_queue_full: ${current.provider} queue has ${queuedCount}/${maxQueued} lanes`,
3164
+ });
3165
+ continue;
3166
+ }
3167
+ }
3168
+ if (!deps.dryRun)
3169
+ writeAnsweredQuestions(hqRoot, current, questions);
3170
+ if (queueReason !== undefined) {
3171
+ const answerEnv = deps.spawnLaneDeps?.env ?? deps.env ?? process.env;
3172
+ const answerCaller = typeof answerEnv.HQ_LANE_ID === "string"
3173
+ ? answerEnv.HQ_LANE_ID.trim() || "user"
3174
+ : "user";
3175
+ const queued = casLaneIf(hqRoot, current.lane_id, (latest) => latest.state === "awaiting_input", (latest) => ({
3176
+ ...latest,
3177
+ state: "queued",
3178
+ queued_reason: queueReason,
3179
+ queued_details: undefined,
3180
+ queued_at: answeredAt,
3181
+ pending_resume: {
3182
+ ...(latest.provider_session_id
3183
+ ? { session_id: latest.provider_session_id }
3184
+ : {}),
3185
+ trigger: "answered_questions",
3186
+ caller: answerCaller,
3187
+ at: answeredAt,
3188
+ },
3189
+ }), deps.dryRun);
3190
+ summary.resumed.push({
3191
+ id: queued.record.lane_id,
3192
+ from: "awaiting_input",
3193
+ to: "queued",
3194
+ reason: queueReason,
3195
+ });
3196
+ capacityLanes.push(queued.record);
3197
+ continue;
3198
+ }
3199
+ if (deps.dryRun) {
3200
+ summary.resumed.push({
3201
+ id: lane.lane_id,
3202
+ from: "awaiting_input",
3203
+ to: "running",
3204
+ });
3205
+ running.push({ ...current, state: "running" });
3206
+ continue;
3207
+ }
3208
+ const started = await spawnFn(hqRoot, lane);
3209
+ if (started.state === "running")
3210
+ running.push(started);
3211
+ summary.resumed.push({
3212
+ id: started.lane_id,
3213
+ from: "awaiting_input",
3214
+ to: started.state,
3215
+ });
3216
+ }
3217
+ catch (err) {
3218
+ terminalizeModelUnsupported(hqRoot, lane, err);
3219
+ const errorName = err instanceof LanesError ? `${err.error}: ` : "";
3220
+ const errorMessage = err instanceof Error ? err.message : String(err);
3221
+ markDirty(summary, {
3222
+ kind: "lane",
3223
+ id: lane.lane_id,
3224
+ why: `resume failed: ${errorName}${errorMessage}`,
3225
+ });
3226
+ }
3227
+ }
3228
+ }
3229
+ /**
3230
+ * One idempotent reconcile pass. Throws only when the store directory itself
3231
+ * cannot be listed; per-record failures are logged onto the summary and set
3232
+ * `ok: false` rather than being swallowed.
3233
+ */
3234
+ export async function runReconcilePass(hqRoot, deps) {
3235
+ return withReconcileLock(hqRoot, async () => runPassBody(hqRoot, deps));
3236
+ }
3237
+ async function runTerminalKeepAliveCandidatePass(hqRoot, lanes, summary, deps, now, candidate) {
3238
+ const lane = lanes.find((row) => row.lane_id === candidate.laneId);
3239
+ if (!lane || lane.version !== candidate.version || lane.state !== "done" ||
3240
+ lane.keep_alive !== true || !laneRunsHere(lane, deps.host))
3241
+ return summary;
3242
+ let company;
3243
+ try {
3244
+ company = resolveLaneCompany(hqRoot, lane).company;
3245
+ }
3246
+ catch (err) {
3247
+ markDirty(summary, { kind: "lane", id: lane.lane_id, why: `keep-alive company lookup failed: ${err instanceof Error ? err.message : String(err)}` });
3248
+ return summary;
3249
+ }
3250
+ if (company === null)
3251
+ return summary;
3252
+ const readFlag = deps.readRegistryFlag ?? readLanesRegistryFlag;
3253
+ const options = { company, env: deps.env ?? process.env };
3254
+ let keepAliveEnabled;
3255
+ let lineageAware;
3256
+ let immediateFirstResumeEnabled;
3257
+ try {
3258
+ [keepAliveEnabled, lineageAware, immediateFirstResumeEnabled] = await Promise.all([
3259
+ readFlag(LEAD_LANE_KEEP_ALIVE_FLAG, options),
3260
+ readFlag(LANE_SUPERSESSION_LINEAGE_AWARE_FLAG, options),
3261
+ readFlag(LEAD_LANE_KEEP_ALIVE_IMMEDIATE_FIRST_RESUME_FLAG, options),
3262
+ ]);
3263
+ }
3264
+ catch (err) {
3265
+ markDirty(summary, { kind: "lane", id: lane.lane_id, why: `keep-alive flag read failed: ${err instanceof Error ? err.message : String(err)}` });
3266
+ return summary;
3267
+ }
3268
+ if (!keepAliveEnabled)
3269
+ return summary;
3270
+ await withAllLaneAdmissionLocks(hqRoot, async () => {
3271
+ const freshLanes = loadLaneRecords(hqRoot, summary);
3272
+ const freshQuestions = loadQuestionRecords(hqRoot, summary);
3273
+ const current = freshLanes.find((row) => row.lane_id === candidate.laneId);
3274
+ if (!current || current.version !== candidate.version || current.state !== "done")
3275
+ return;
3276
+ await resumeKeepAlive(hqRoot, freshLanes, freshQuestions, summary, deps, now, new Map([[keepAliveCompanyKey(company), true]]), new Map([[keepAliveCompanyKey(company), lineageAware]]), new Map(), new Map([[keepAliveCompanyKey(company), immediateFirstResumeEnabled]]), candidate);
3277
+ await admitQueued(hqRoot, loadLaneRecords(hqRoot, summary), summary, deps, now, new Set([candidate.laneId]));
3278
+ });
3279
+ return summary;
3280
+ }
3281
+ /** Run the reconcile answer-resume path for one lane before questions answer returns. */
3282
+ export async function wakeAnsweredLaneNow(hqRoot, laneId, deps) {
3283
+ const summary = emptySummary(deps.dryRun);
3284
+ const throttleDeps = deps.throttleDeps ?? defaultThrottleDeps(deps.env ?? process.env);
3285
+ await withReconcileLock(hqRoot, async () => {
3286
+ const previousState = readThrottleState(throttleStatePath(hqRoot), throttleDeps);
3287
+ const previousSnapshot = previousState === "missing" || previousState === "unreadable"
3288
+ ? undefined
3289
+ : previousState.cpu_snapshot;
3290
+ const cpuObservation = await sampleCpuBusyObservation(throttleDeps, previousSnapshot);
3291
+ const admissionDeps = {
3292
+ ...deps,
3293
+ cpuBusyFraction: cpuObservation.busy,
3294
+ cpuObservation,
3295
+ skipAdmissionWithoutCpuSample: false,
3296
+ };
3297
+ await withAllLaneAdmissionLocks(hqRoot, async () => {
3298
+ const lanes = loadLaneRecords(hqRoot, summary);
3299
+ const questions = loadQuestionRecords(hqRoot, summary);
3300
+ if (!lanes.some((lane) => lane.lane_id === laneId)) {
3301
+ throw new LanesError("lane_not_found", `Lane ${laneId} was not found`, { lane_id: laneId });
3302
+ }
3303
+ await resumeAwaiting(hqRoot, lanes, questions, summary, admissionDeps, new Set([laneId]));
3304
+ });
3305
+ });
3306
+ const latest = readLane(hqRoot, laneId);
3307
+ const laneQuestions = loadQuestionRecords(hqRoot, emptySummary(false)).filter((question) => question.origin.id === laneId);
3308
+ const remaining = laneQuestions
3309
+ .filter((question) => question.status === "pending")
3310
+ .map((question) => question.question_id)
3311
+ .sort();
3312
+ if (latest.state === "running") {
3313
+ return { status: "resumed", lane_state: latest.state };
3314
+ }
3315
+ if (latest.state === "queued" && latest.pending_resume?.trigger === "answered_questions") {
3316
+ return {
3317
+ status: "queued",
3318
+ lane_state: latest.state,
3319
+ ...(latest.queued_reason !== undefined ? { queued_reason: latest.queued_reason } : {}),
3320
+ };
3321
+ }
3322
+ if (latest.state === "awaiting_input" && remaining.length > 0) {
3323
+ return {
3324
+ status: "awaiting_answers",
3325
+ lane_state: latest.state,
3326
+ remaining_question_ids: remaining,
3327
+ };
3328
+ }
3329
+ const failure = summary.skipped.find((skip) => skip.kind === "lane" && skip.id === laneId);
3330
+ if (latest.state === "awaiting_input" && failure?.why.startsWith("hire_grant_exhausted:")) {
3331
+ return {
3332
+ status: "deferred",
3333
+ lane_state: latest.state,
3334
+ error: failure.why,
3335
+ };
3336
+ }
3337
+ return {
3338
+ status: "failed",
3339
+ lane_state: latest.state,
3340
+ error: failure?.why ?? `Lane ${laneId} did not leave awaiting_input after its questions were answered`,
3341
+ };
3342
+ }
3343
+ //# sourceMappingURL=reconcile.js.map