corporateai 0.0.1

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 (704) hide show
  1. package/.dockerignore +10 -0
  2. package/.env.example +3 -0
  3. package/.github/workflows/publish-cli.yml +49 -0
  4. package/.mailmap +1 -0
  5. package/AGENTS.md +148 -0
  6. package/CONTRIBUTING.md +75 -0
  7. package/Dockerfile +59 -0
  8. package/Dockerfile.onboard-smoke +42 -0
  9. package/LICENSE +21 -0
  10. package/README.md +93 -0
  11. package/cli/esbuild.config.mjs +11 -0
  12. package/cli/package.json +24 -0
  13. package/cli/scripts/build-cli.mjs +5 -0
  14. package/cli/src/index.ts +27 -0
  15. package/docker-compose.quickstart.yml +18 -0
  16. package/docker-compose.untrusted-review.yml +33 -0
  17. package/docker-compose.yml +38 -0
  18. package/package.json +56 -0
  19. package/patches/embedded-postgres@18.1.0-beta.16.patch +0 -0
  20. package/pnpm-workspace.yaml +4 -0
  21. package/releases/.gitkeep +0 -0
  22. package/releases/v0.0.1.md +36 -0
  23. package/releases/v0.2.7.md +15 -0
  24. package/releases/v0.3.0.md +54 -0
  25. package/releases/v0.3.1.md +55 -0
  26. package/releases/v2026.318.0.md +66 -0
  27. package/releases/v2026.325.0.md +78 -0
  28. package/report/2026-03-13-08-46-token-optimization-implementation.md +48 -0
  29. package/scripts/backup-db.sh +17 -0
  30. package/scripts/build-npm.sh +80 -0
  31. package/scripts/check-forbidden-tokens.mjs +115 -0
  32. package/scripts/clean-onboard-git.sh +14 -0
  33. package/scripts/clean-onboard-npm.sh +13 -0
  34. package/scripts/clean-onboard-ref.sh +86 -0
  35. package/scripts/create-github-release.sh +99 -0
  36. package/scripts/dev-runner-paths.mjs +38 -0
  37. package/scripts/dev-runner.mjs +606 -0
  38. package/scripts/docker-onboard-smoke.sh +306 -0
  39. package/scripts/ensure-plugin-build-deps.mjs +47 -0
  40. package/scripts/generate-company-assets.ts +365 -0
  41. package/scripts/generate-npm-package-json.mjs +113 -0
  42. package/scripts/generate-org-chart-images.ts +694 -0
  43. package/scripts/generate-org-chart-satori-comparison.ts +225 -0
  44. package/scripts/generate-ui-package-json.mjs +31 -0
  45. package/scripts/kill-dev.sh +71 -0
  46. package/scripts/migrate-inline-env-secrets.ts +126 -0
  47. package/scripts/prepare-server-ui-dist.sh +22 -0
  48. package/scripts/provision-worktree.sh +333 -0
  49. package/scripts/release-lib.sh +306 -0
  50. package/scripts/release-package-map.mjs +169 -0
  51. package/scripts/release.sh +312 -0
  52. package/scripts/rollback-latest.sh +111 -0
  53. package/scripts/smoke/openclaw-docker-ui.sh +329 -0
  54. package/scripts/smoke/openclaw-gateway-e2e.sh +954 -0
  55. package/scripts/smoke/openclaw-join.sh +295 -0
  56. package/scripts/smoke/openclaw-sse-standalone.sh +146 -0
  57. package/scripts/workspace-compat.mjs +60 -0
  58. package/server/CHANGELOG.md +130 -0
  59. package/server/package.json +96 -0
  60. package/server/scripts/copy-onboarding-assets.mjs +10 -0
  61. package/server/scripts/dev-watch.ts +33 -0
  62. package/server/src/__tests__/activity-routes.test.ts +70 -0
  63. package/server/src/__tests__/adapter-models.test.ts +105 -0
  64. package/server/src/__tests__/adapter-session-codecs.test.ts +194 -0
  65. package/server/src/__tests__/agent-auth-jwt.test.ts +79 -0
  66. package/server/src/__tests__/agent-instructions-routes.test.ts +318 -0
  67. package/server/src/__tests__/agent-instructions-service.test.ts +361 -0
  68. package/server/src/__tests__/agent-permissions-routes.test.ts +275 -0
  69. package/server/src/__tests__/agent-shortname-collision.test.ts +69 -0
  70. package/server/src/__tests__/agent-skill-contract.test.ts +50 -0
  71. package/server/src/__tests__/agent-skills-routes.test.ts +462 -0
  72. package/server/src/__tests__/app-hmr-port.test.ts +19 -0
  73. package/server/src/__tests__/approval-routes-idempotency.test.ts +110 -0
  74. package/server/src/__tests__/approvals-service.test.ts +107 -0
  75. package/server/src/__tests__/assets.test.ts +250 -0
  76. package/server/src/__tests__/attachment-types.test.ts +97 -0
  77. package/server/src/__tests__/board-mutation-guard.test.ts +105 -0
  78. package/server/src/__tests__/budgets-service.test.ts +311 -0
  79. package/server/src/__tests__/claude-local-adapter-environment.test.ts +92 -0
  80. package/server/src/__tests__/claude-local-adapter.test.ts +31 -0
  81. package/server/src/__tests__/claude-local-skill-sync.test.ts +111 -0
  82. package/server/src/__tests__/cli-auth-routes.test.ts +230 -0
  83. package/server/src/__tests__/codex-local-adapter-environment.test.ts +143 -0
  84. package/server/src/__tests__/codex-local-adapter.test.ts +253 -0
  85. package/server/src/__tests__/codex-local-execute.test.ts +391 -0
  86. package/server/src/__tests__/codex-local-skill-injection.test.ts +175 -0
  87. package/server/src/__tests__/codex-local-skill-sync.test.ts +123 -0
  88. package/server/src/__tests__/companies-route-path-guard.test.ts +56 -0
  89. package/server/src/__tests__/company-branding-route.test.ts +196 -0
  90. package/server/src/__tests__/company-portability-routes.test.ts +175 -0
  91. package/server/src/__tests__/company-portability.test.ts +2186 -0
  92. package/server/src/__tests__/company-skills-routes.test.ts +113 -0
  93. package/server/src/__tests__/company-skills.test.ts +229 -0
  94. package/server/src/__tests__/costs-service.test.ts +226 -0
  95. package/server/src/__tests__/cursor-local-adapter-environment.test.ts +196 -0
  96. package/server/src/__tests__/cursor-local-adapter.test.ts +406 -0
  97. package/server/src/__tests__/cursor-local-execute.test.ts +263 -0
  98. package/server/src/__tests__/cursor-local-skill-injection.test.ts +104 -0
  99. package/server/src/__tests__/cursor-local-skill-sync.test.ts +145 -0
  100. package/server/src/__tests__/dev-runner-paths.test.ts +25 -0
  101. package/server/src/__tests__/dev-server-status.test.ts +66 -0
  102. package/server/src/__tests__/dev-watch-ignore.test.ts +42 -0
  103. package/server/src/__tests__/documents.test.ts +29 -0
  104. package/server/src/__tests__/error-handler.test.ts +53 -0
  105. package/server/src/__tests__/execution-workspace-policy.test.ts +170 -0
  106. package/server/src/__tests__/forbidden-tokens.test.ts +77 -0
  107. package/server/src/__tests__/gemini-local-adapter-environment.test.ts +135 -0
  108. package/server/src/__tests__/gemini-local-adapter.test.ts +190 -0
  109. package/server/src/__tests__/gemini-local-execute.test.ts +172 -0
  110. package/server/src/__tests__/gemini-local-skill-sync.test.ts +90 -0
  111. package/server/src/__tests__/health.test.ts +16 -0
  112. package/server/src/__tests__/heartbeat-process-recovery.test.ts +256 -0
  113. package/server/src/__tests__/heartbeat-run-summary.test.ts +33 -0
  114. package/server/src/__tests__/heartbeat-workspace-session.test.ts +334 -0
  115. package/server/src/__tests__/helpers/embedded-postgres.ts +7 -0
  116. package/server/src/__tests__/hire-hook.test.ts +181 -0
  117. package/server/src/__tests__/instance-settings-routes.test.ts +156 -0
  118. package/server/src/__tests__/invite-accept-gateway-defaults.test.ts +119 -0
  119. package/server/src/__tests__/invite-accept-replay.test.ts +92 -0
  120. package/server/src/__tests__/invite-expiry.test.ts +10 -0
  121. package/server/src/__tests__/invite-join-grants.test.ts +57 -0
  122. package/server/src/__tests__/invite-join-manager.test.ts +33 -0
  123. package/server/src/__tests__/invite-onboarding-text.test.ts +116 -0
  124. package/server/src/__tests__/issue-comment-reopen-routes.test.ts +146 -0
  125. package/server/src/__tests__/issue-goal-fallback.test.ts +99 -0
  126. package/server/src/__tests__/issues-checkout-wakeup.test.ts +48 -0
  127. package/server/src/__tests__/issues-goal-context-routes.test.ts +187 -0
  128. package/server/src/__tests__/issues-service.test.ts +317 -0
  129. package/server/src/__tests__/issues-user-context.test.ts +113 -0
  130. package/server/src/__tests__/log-redaction.test.ts +74 -0
  131. package/server/src/__tests__/monthly-spend-service.test.ts +90 -0
  132. package/server/src/__tests__/normalize-agent-mention-token.test.ts +41 -0
  133. package/server/src/__tests__/openclaw-gateway-adapter.test.ts +626 -0
  134. package/server/src/__tests__/openclaw-invite-prompt-route.test.ts +192 -0
  135. package/server/src/__tests__/opencode-local-adapter-environment.test.ts +97 -0
  136. package/server/src/__tests__/opencode-local-adapter.test.ts +226 -0
  137. package/server/src/__tests__/opencode-local-skill-sync.test.ts +91 -0
  138. package/server/src/__tests__/paperclip-env.test.ts +58 -0
  139. package/server/src/__tests__/paperclip-skill-utils.test.ts +63 -0
  140. package/server/src/__tests__/pi-local-adapter-environment.test.ts +102 -0
  141. package/server/src/__tests__/pi-local-skill-sync.test.ts +95 -0
  142. package/server/src/__tests__/plugin-dev-watcher.test.ts +68 -0
  143. package/server/src/__tests__/plugin-worker-manager.test.ts +43 -0
  144. package/server/src/__tests__/private-hostname-guard.test.ts +56 -0
  145. package/server/src/__tests__/project-shortname-resolution.test.ts +45 -0
  146. package/server/src/__tests__/quota-windows-service.test.ts +56 -0
  147. package/server/src/__tests__/quota-windows.test.ts +1109 -0
  148. package/server/src/__tests__/redaction.test.ts +66 -0
  149. package/server/src/__tests__/routines-e2e.test.ts +276 -0
  150. package/server/src/__tests__/routines-routes.test.ts +271 -0
  151. package/server/src/__tests__/routines-service.test.ts +424 -0
  152. package/server/src/__tests__/storage-local-provider.test.ts +78 -0
  153. package/server/src/__tests__/ui-branding.test.ts +82 -0
  154. package/server/src/__tests__/work-products.test.ts +95 -0
  155. package/server/src/__tests__/workspace-runtime.test.ts +1131 -0
  156. package/server/src/__tests__/worktree-config.test.ts +426 -0
  157. package/server/src/adapters/codex-models.ts +105 -0
  158. package/server/src/adapters/cursor-models.ts +171 -0
  159. package/server/src/adapters/http/execute.ts +42 -0
  160. package/server/src/adapters/http/index.ts +21 -0
  161. package/server/src/adapters/http/test.ts +116 -0
  162. package/server/src/adapters/index.ts +18 -0
  163. package/server/src/adapters/process/execute.ts +77 -0
  164. package/server/src/adapters/process/index.ts +24 -0
  165. package/server/src/adapters/process/test.ts +89 -0
  166. package/server/src/adapters/registry.ts +225 -0
  167. package/server/src/adapters/server-utils-compat.ts +57 -0
  168. package/server/src/adapters/types.ts +30 -0
  169. package/server/src/adapters/utils.ts +48 -0
  170. package/server/src/agent-auth-jwt.ts +141 -0
  171. package/server/src/app.ts +321 -0
  172. package/server/src/attachment-types.ts +74 -0
  173. package/server/src/auth/better-auth.ts +148 -0
  174. package/server/src/board-claim.ts +150 -0
  175. package/server/src/config-file.ts +17 -0
  176. package/server/src/config.ts +260 -0
  177. package/server/src/dev-server-status.ts +103 -0
  178. package/server/src/dev-watch-ignore.ts +36 -0
  179. package/server/src/errors.ts +34 -0
  180. package/server/src/home-paths.ts +95 -0
  181. package/server/src/index.ts +799 -0
  182. package/server/src/log-redaction.ts +146 -0
  183. package/server/src/middleware/auth.ts +178 -0
  184. package/server/src/middleware/board-mutation-guard.ts +66 -0
  185. package/server/src/middleware/error-handler.ts +71 -0
  186. package/server/src/middleware/index.ts +3 -0
  187. package/server/src/middleware/logger.ts +90 -0
  188. package/server/src/middleware/private-hostname-guard.ts +92 -0
  189. package/server/src/middleware/validate.ts +9 -0
  190. package/server/src/onboarding-assets/ceo/AGENTS.md +54 -0
  191. package/server/src/onboarding-assets/ceo/HEARTBEAT.md +72 -0
  192. package/server/src/onboarding-assets/ceo/SOUL.md +33 -0
  193. package/server/src/onboarding-assets/ceo/TOOLS.md +3 -0
  194. package/server/src/onboarding-assets/default/AGENTS.md +3 -0
  195. package/server/src/paths.ts +34 -0
  196. package/server/src/realtime/live-events-ws.ts +274 -0
  197. package/server/src/redaction.ts +59 -0
  198. package/server/src/routes/access.ts +2888 -0
  199. package/server/src/routes/activity.ts +89 -0
  200. package/server/src/routes/agents.ts +2313 -0
  201. package/server/src/routes/approvals.ts +346 -0
  202. package/server/src/routes/assets.ts +341 -0
  203. package/server/src/routes/authz.ts +52 -0
  204. package/server/src/routes/companies.ts +343 -0
  205. package/server/src/routes/company-skills.ts +300 -0
  206. package/server/src/routes/costs.ts +335 -0
  207. package/server/src/routes/dashboard.ts +19 -0
  208. package/server/src/routes/execution-workspaces.ts +182 -0
  209. package/server/src/routes/goals.ts +107 -0
  210. package/server/src/routes/health.ts +94 -0
  211. package/server/src/routes/index.ts +17 -0
  212. package/server/src/routes/instance-settings.ts +95 -0
  213. package/server/src/routes/issues-checkout-wakeup.ts +14 -0
  214. package/server/src/routes/issues.ts +1680 -0
  215. package/server/src/routes/llms.ts +86 -0
  216. package/server/src/routes/org-chart-svg.ts +777 -0
  217. package/server/src/routes/plugin-ui-static.ts +497 -0
  218. package/server/src/routes/plugins.ts +2220 -0
  219. package/server/src/routes/projects.ts +295 -0
  220. package/server/src/routes/routines.ts +300 -0
  221. package/server/src/routes/secrets.ts +166 -0
  222. package/server/src/routes/sidebar-badges.ts +52 -0
  223. package/server/src/secrets/external-stub-providers.ts +32 -0
  224. package/server/src/secrets/local-encrypted-provider.ts +135 -0
  225. package/server/src/secrets/provider-registry.ts +31 -0
  226. package/server/src/secrets/types.ts +23 -0
  227. package/server/src/services/access.ts +381 -0
  228. package/server/src/services/activity-log.ts +95 -0
  229. package/server/src/services/activity.ts +164 -0
  230. package/server/src/services/agent-instructions.ts +735 -0
  231. package/server/src/services/agent-permissions.ts +27 -0
  232. package/server/src/services/agents.ts +694 -0
  233. package/server/src/services/approvals.ts +273 -0
  234. package/server/src/services/assets.ts +23 -0
  235. package/server/src/services/board-auth.ts +355 -0
  236. package/server/src/services/budgets.ts +959 -0
  237. package/server/src/services/companies.ts +313 -0
  238. package/server/src/services/company-export-readme.ts +173 -0
  239. package/server/src/services/company-portability.ts +4263 -0
  240. package/server/src/services/company-skills.ts +2356 -0
  241. package/server/src/services/costs.ts +365 -0
  242. package/server/src/services/cron.ts +373 -0
  243. package/server/src/services/dashboard.ts +110 -0
  244. package/server/src/services/default-agent-instructions.ts +27 -0
  245. package/server/src/services/documents.ts +434 -0
  246. package/server/src/services/execution-workspace-policy.ts +210 -0
  247. package/server/src/services/execution-workspaces.ts +100 -0
  248. package/server/src/services/finance.ts +135 -0
  249. package/server/src/services/goals.ts +81 -0
  250. package/server/src/services/heartbeat-run-summary.ts +35 -0
  251. package/server/src/services/heartbeat.ts +3863 -0
  252. package/server/src/services/hire-hook.ts +114 -0
  253. package/server/src/services/index.ts +32 -0
  254. package/server/src/services/instance-settings.ts +138 -0
  255. package/server/src/services/issue-approvals.ts +175 -0
  256. package/server/src/services/issue-assignment-wakeup.ts +48 -0
  257. package/server/src/services/issue-goal-fallback.ts +56 -0
  258. package/server/src/services/issues.ts +1828 -0
  259. package/server/src/services/live-events.ts +55 -0
  260. package/server/src/services/plugin-capability-validator.ts +450 -0
  261. package/server/src/services/plugin-config-validator.ts +55 -0
  262. package/server/src/services/plugin-dev-watcher.ts +339 -0
  263. package/server/src/services/plugin-event-bus.ts +413 -0
  264. package/server/src/services/plugin-host-service-cleanup.ts +59 -0
  265. package/server/src/services/plugin-host-services.ts +1132 -0
  266. package/server/src/services/plugin-job-coordinator.ts +261 -0
  267. package/server/src/services/plugin-job-scheduler.ts +753 -0
  268. package/server/src/services/plugin-job-store.ts +466 -0
  269. package/server/src/services/plugin-lifecycle.ts +822 -0
  270. package/server/src/services/plugin-loader.ts +1955 -0
  271. package/server/src/services/plugin-log-retention.ts +87 -0
  272. package/server/src/services/plugin-manifest-validator.ts +164 -0
  273. package/server/src/services/plugin-registry.ts +683 -0
  274. package/server/src/services/plugin-runtime-sandbox.ts +222 -0
  275. package/server/src/services/plugin-secrets-handler.ts +355 -0
  276. package/server/src/services/plugin-state-store.ts +238 -0
  277. package/server/src/services/plugin-stream-bus.ts +81 -0
  278. package/server/src/services/plugin-tool-dispatcher.ts +449 -0
  279. package/server/src/services/plugin-tool-registry.ts +450 -0
  280. package/server/src/services/plugin-worker-manager.ts +1343 -0
  281. package/server/src/services/projects.ts +860 -0
  282. package/server/src/services/quota-windows.ts +65 -0
  283. package/server/src/services/routines.ts +1269 -0
  284. package/server/src/services/run-log-store.ts +156 -0
  285. package/server/src/services/secrets.ts +370 -0
  286. package/server/src/services/sidebar-badges.ts +56 -0
  287. package/server/src/services/work-products.ts +124 -0
  288. package/server/src/services/workspace-operation-log-store.ts +156 -0
  289. package/server/src/services/workspace-operations.ts +262 -0
  290. package/server/src/services/workspace-runtime.ts +1565 -0
  291. package/server/src/startup-banner.ts +176 -0
  292. package/server/src/storage/index.ts +35 -0
  293. package/server/src/storage/local-disk-provider.ts +89 -0
  294. package/server/src/storage/provider-registry.ts +18 -0
  295. package/server/src/storage/s3-provider.ts +153 -0
  296. package/server/src/storage/service.ts +131 -0
  297. package/server/src/storage/types.ts +63 -0
  298. package/server/src/ui-branding.ts +217 -0
  299. package/server/src/version.ts +10 -0
  300. package/server/src/worktree-config.ts +468 -0
  301. package/server/tsconfig.json +9 -0
  302. package/server/vitest.config.ts +7 -0
  303. package/skills/paperclip/SKILL.md +365 -0
  304. package/skills/paperclip/references/api-reference.md +647 -0
  305. package/skills/paperclip/references/company-skills.md +193 -0
  306. package/skills/paperclip-create-agent/SKILL.md +142 -0
  307. package/skills/paperclip-create-agent/references/api-reference.md +105 -0
  308. package/skills/paperclip-create-plugin/SKILL.md +102 -0
  309. package/skills/para-memory-files/SKILL.md +104 -0
  310. package/skills/para-memory-files/references/schemas.md +35 -0
  311. package/tests/e2e/onboarding.spec.ts +142 -0
  312. package/tests/e2e/playwright.config.ts +35 -0
  313. package/tests/release-smoke/docker-auth-onboarding.spec.ts +141 -0
  314. package/tests/release-smoke/playwright.config.ts +28 -0
  315. package/tsconfig.base.json +18 -0
  316. package/tsconfig.json +18 -0
  317. package/ui/README.md +12 -0
  318. package/ui/components.json +21 -0
  319. package/ui/index.html +47 -0
  320. package/ui/package.json +73 -0
  321. package/ui/public/android-chrome-192x192.png +0 -0
  322. package/ui/public/android-chrome-512x512.png +0 -0
  323. package/ui/public/apple-touch-icon.png +0 -0
  324. package/ui/public/brands/opencode-logo-dark-square.svg +18 -0
  325. package/ui/public/brands/opencode-logo-light-square.svg +18 -0
  326. package/ui/public/favicon-16x16.png +0 -0
  327. package/ui/public/favicon-32x32.png +0 -0
  328. package/ui/public/favicon.ico +0 -0
  329. package/ui/public/favicon.svg +9 -0
  330. package/ui/public/site.webmanifest +30 -0
  331. package/ui/public/sprites/1-D-1.png +0 -0
  332. package/ui/public/sprites/1-D-2.png +0 -0
  333. package/ui/public/sprites/1-D-3.png +0 -0
  334. package/ui/public/sprites/1-L-1.png +0 -0
  335. package/ui/public/sprites/1-R-1.png +0 -0
  336. package/ui/public/sprites/10-D-1.png +0 -0
  337. package/ui/public/sprites/10-D-2.png +0 -0
  338. package/ui/public/sprites/10-D-3.png +0 -0
  339. package/ui/public/sprites/10-L-1.png +0 -0
  340. package/ui/public/sprites/10-R-1.png +0 -0
  341. package/ui/public/sprites/11-D-1.png +0 -0
  342. package/ui/public/sprites/11-D-2.png +0 -0
  343. package/ui/public/sprites/11-D-3.png +0 -0
  344. package/ui/public/sprites/11-L-1.png +0 -0
  345. package/ui/public/sprites/11-R-1.png +0 -0
  346. package/ui/public/sprites/12-D-1.png +0 -0
  347. package/ui/public/sprites/12-D-2.png +0 -0
  348. package/ui/public/sprites/12-D-3.png +0 -0
  349. package/ui/public/sprites/12-L-1.png +0 -0
  350. package/ui/public/sprites/12-R-1.png +0 -0
  351. package/ui/public/sprites/13-D-1.png +0 -0
  352. package/ui/public/sprites/13-D-2.png +0 -0
  353. package/ui/public/sprites/13-D-3.png +0 -0
  354. package/ui/public/sprites/13-L-1.png +0 -0
  355. package/ui/public/sprites/13-R-1.png +0 -0
  356. package/ui/public/sprites/14-D-1.png +0 -0
  357. package/ui/public/sprites/14-D-2.png +0 -0
  358. package/ui/public/sprites/14-D-3.png +0 -0
  359. package/ui/public/sprites/14-L-1.png +0 -0
  360. package/ui/public/sprites/14-R-1.png +0 -0
  361. package/ui/public/sprites/2-D-1.png +0 -0
  362. package/ui/public/sprites/2-D-2.png +0 -0
  363. package/ui/public/sprites/2-D-3.png +0 -0
  364. package/ui/public/sprites/2-L-1.png +0 -0
  365. package/ui/public/sprites/2-R-1.png +0 -0
  366. package/ui/public/sprites/3-D-1.png +0 -0
  367. package/ui/public/sprites/3-D-2.png +0 -0
  368. package/ui/public/sprites/3-D-3.png +0 -0
  369. package/ui/public/sprites/3-L-1.png +0 -0
  370. package/ui/public/sprites/3-R-1.png +0 -0
  371. package/ui/public/sprites/4-D-1.png +0 -0
  372. package/ui/public/sprites/4-D-2.png +0 -0
  373. package/ui/public/sprites/4-D-3.png +0 -0
  374. package/ui/public/sprites/4-L-1.png +0 -0
  375. package/ui/public/sprites/4-R-1.png +0 -0
  376. package/ui/public/sprites/5-D-1.png +0 -0
  377. package/ui/public/sprites/5-D-2.png +0 -0
  378. package/ui/public/sprites/5-D-3.png +0 -0
  379. package/ui/public/sprites/5-L-1.png +0 -0
  380. package/ui/public/sprites/5-R-1.png +0 -0
  381. package/ui/public/sprites/6-D-1.png +0 -0
  382. package/ui/public/sprites/6-D-2.png +0 -0
  383. package/ui/public/sprites/6-D-3.png +0 -0
  384. package/ui/public/sprites/6-L-1.png +0 -0
  385. package/ui/public/sprites/6-R-1.png +0 -0
  386. package/ui/public/sprites/7-D-1.png +0 -0
  387. package/ui/public/sprites/7-D-2.png +0 -0
  388. package/ui/public/sprites/7-D-3.png +0 -0
  389. package/ui/public/sprites/7-L-1.png +0 -0
  390. package/ui/public/sprites/7-R-1.png +0 -0
  391. package/ui/public/sprites/8-D-1.png +0 -0
  392. package/ui/public/sprites/8-D-2.png +0 -0
  393. package/ui/public/sprites/8-D-3.png +0 -0
  394. package/ui/public/sprites/8-L-1.png +0 -0
  395. package/ui/public/sprites/8-R-1.png +0 -0
  396. package/ui/public/sprites/9-D-1.png +0 -0
  397. package/ui/public/sprites/9-D-2.png +0 -0
  398. package/ui/public/sprites/9-D-3.png +0 -0
  399. package/ui/public/sprites/9-L-1.png +0 -0
  400. package/ui/public/sprites/9-R-1.png +0 -0
  401. package/ui/public/sprites/ceo-lobster.png +0 -0
  402. package/ui/public/sw.js +42 -0
  403. package/ui/public/worktree-favicon-16x16.png +0 -0
  404. package/ui/public/worktree-favicon-32x32.png +0 -0
  405. package/ui/public/worktree-favicon.ico +0 -0
  406. package/ui/public/worktree-favicon.svg +9 -0
  407. package/ui/src/App.tsx +354 -0
  408. package/ui/src/adapters/claude-local/config-fields.tsx +138 -0
  409. package/ui/src/adapters/claude-local/index.ts +13 -0
  410. package/ui/src/adapters/codex-local/config-fields.tsx +104 -0
  411. package/ui/src/adapters/codex-local/index.ts +13 -0
  412. package/ui/src/adapters/cursor/config-fields.tsx +49 -0
  413. package/ui/src/adapters/cursor/index.ts +13 -0
  414. package/ui/src/adapters/gemini-local/config-fields.tsx +51 -0
  415. package/ui/src/adapters/gemini-local/index.ts +13 -0
  416. package/ui/src/adapters/http/build-config.ts +9 -0
  417. package/ui/src/adapters/http/config-fields.tsx +38 -0
  418. package/ui/src/adapters/http/index.ts +12 -0
  419. package/ui/src/adapters/http/parse-stdout.ts +5 -0
  420. package/ui/src/adapters/index.ts +9 -0
  421. package/ui/src/adapters/local-workspace-runtime-fields.tsx +5 -0
  422. package/ui/src/adapters/openclaw-gateway/config-fields.tsx +237 -0
  423. package/ui/src/adapters/openclaw-gateway/index.ts +13 -0
  424. package/ui/src/adapters/opencode-local/config-fields.tsx +72 -0
  425. package/ui/src/adapters/opencode-local/index.ts +13 -0
  426. package/ui/src/adapters/pi-local/config-fields.tsx +49 -0
  427. package/ui/src/adapters/pi-local/index.ts +13 -0
  428. package/ui/src/adapters/process/build-config.ts +18 -0
  429. package/ui/src/adapters/process/config-fields.tsx +77 -0
  430. package/ui/src/adapters/process/index.ts +12 -0
  431. package/ui/src/adapters/process/parse-stdout.ts +5 -0
  432. package/ui/src/adapters/registry.ts +34 -0
  433. package/ui/src/adapters/runtime-json-fields.tsx +122 -0
  434. package/ui/src/adapters/transcript.test.ts +30 -0
  435. package/ui/src/adapters/transcript.ts +62 -0
  436. package/ui/src/adapters/types.ts +34 -0
  437. package/ui/src/api/access.ts +160 -0
  438. package/ui/src/api/activity.ts +37 -0
  439. package/ui/src/api/agents.ts +194 -0
  440. package/ui/src/api/approvals.ts +25 -0
  441. package/ui/src/api/assets.ts +30 -0
  442. package/ui/src/api/auth.ts +74 -0
  443. package/ui/src/api/budgets.ts +21 -0
  444. package/ui/src/api/client.ts +50 -0
  445. package/ui/src/api/companies.ts +59 -0
  446. package/ui/src/api/companySkills.ts +55 -0
  447. package/ui/src/api/costs.ts +60 -0
  448. package/ui/src/api/dashboard.ts +7 -0
  449. package/ui/src/api/execution-workspaces.ts +27 -0
  450. package/ui/src/api/goals.ts +12 -0
  451. package/ui/src/api/health.ts +41 -0
  452. package/ui/src/api/heartbeats.ts +62 -0
  453. package/ui/src/api/index.ts +18 -0
  454. package/ui/src/api/instanceSettings.ts +19 -0
  455. package/ui/src/api/issues.ts +115 -0
  456. package/ui/src/api/plugins.ts +424 -0
  457. package/ui/src/api/projects.ts +34 -0
  458. package/ui/src/api/routines.ts +59 -0
  459. package/ui/src/api/secrets.ts +26 -0
  460. package/ui/src/api/sidebarBadges.ts +7 -0
  461. package/ui/src/components/AccountingModelCard.tsx +69 -0
  462. package/ui/src/components/ActiveAgentsPanel.tsx +157 -0
  463. package/ui/src/components/ActivityCharts.tsx +264 -0
  464. package/ui/src/components/ActivityRow.tsx +147 -0
  465. package/ui/src/components/AgentActionButtons.tsx +51 -0
  466. package/ui/src/components/AgentConfigForm.tsx +1468 -0
  467. package/ui/src/components/AgentIconPicker.tsx +81 -0
  468. package/ui/src/components/AgentProperties.tsx +107 -0
  469. package/ui/src/components/ApprovalCard.tsx +107 -0
  470. package/ui/src/components/ApprovalPayload.tsx +134 -0
  471. package/ui/src/components/AsciiArtAnimation.tsx +355 -0
  472. package/ui/src/components/BillerSpendCard.tsx +146 -0
  473. package/ui/src/components/BreadcrumbBar.tsx +113 -0
  474. package/ui/src/components/BudgetIncidentCard.tsx +101 -0
  475. package/ui/src/components/BudgetPolicyCard.tsx +220 -0
  476. package/ui/src/components/BudgetSidebarMarker.tsx +13 -0
  477. package/ui/src/components/ClaudeSubscriptionPanel.tsx +141 -0
  478. package/ui/src/components/CodexSubscriptionPanel.tsx +158 -0
  479. package/ui/src/components/CommandPalette.tsx +239 -0
  480. package/ui/src/components/CommentThread.tsx +503 -0
  481. package/ui/src/components/CompanyPatternIcon.tsx +212 -0
  482. package/ui/src/components/CompanyRail.tsx +329 -0
  483. package/ui/src/components/CompanySwitcher.tsx +81 -0
  484. package/ui/src/components/CopyText.tsx +56 -0
  485. package/ui/src/components/DevRestartBanner.tsx +89 -0
  486. package/ui/src/components/EmptyState.tsx +27 -0
  487. package/ui/src/components/EntityRow.tsx +69 -0
  488. package/ui/src/components/FilterBar.tsx +39 -0
  489. package/ui/src/components/FinanceBillerCard.tsx +45 -0
  490. package/ui/src/components/FinanceKindCard.tsx +44 -0
  491. package/ui/src/components/FinanceTimelineCard.tsx +72 -0
  492. package/ui/src/components/GoalProperties.tsx +165 -0
  493. package/ui/src/components/GoalTree.tsx +118 -0
  494. package/ui/src/components/Identity.tsx +39 -0
  495. package/ui/src/components/InlineEditor.tsx +248 -0
  496. package/ui/src/components/InlineEntitySelector.tsx +206 -0
  497. package/ui/src/components/InstanceSidebar.tsx +53 -0
  498. package/ui/src/components/IssueDocumentsSection.tsx +892 -0
  499. package/ui/src/components/IssueProperties.tsx +621 -0
  500. package/ui/src/components/IssueRow.tsx +149 -0
  501. package/ui/src/components/IssueWorkspaceCard.tsx +404 -0
  502. package/ui/src/components/IssuesList.tsx +889 -0
  503. package/ui/src/components/JsonSchemaForm.tsx +1048 -0
  504. package/ui/src/components/KanbanBoard.tsx +275 -0
  505. package/ui/src/components/Layout.tsx +441 -0
  506. package/ui/src/components/LiveRunWidget.tsx +160 -0
  507. package/ui/src/components/MarkdownBody.test.tsx +50 -0
  508. package/ui/src/components/MarkdownBody.tsx +152 -0
  509. package/ui/src/components/MarkdownEditor.tsx +622 -0
  510. package/ui/src/components/MetricCard.tsx +53 -0
  511. package/ui/src/components/MobileBottomNav.tsx +123 -0
  512. package/ui/src/components/NewAgentDialog.tsx +223 -0
  513. package/ui/src/components/NewGoalDialog.tsx +283 -0
  514. package/ui/src/components/NewIssueDialog.tsx +1473 -0
  515. package/ui/src/components/NewProjectDialog.tsx +451 -0
  516. package/ui/src/components/OnboardingWizard.tsx +1392 -0
  517. package/ui/src/components/OpenCodeLogoIcon.tsx +22 -0
  518. package/ui/src/components/PackageFileTree.tsx +318 -0
  519. package/ui/src/components/PageSkeleton.tsx +180 -0
  520. package/ui/src/components/PageTabBar.tsx +45 -0
  521. package/ui/src/components/PathInstructionsModal.tsx +143 -0
  522. package/ui/src/components/PriorityIcon.tsx +77 -0
  523. package/ui/src/components/ProjectProperties.tsx +1127 -0
  524. package/ui/src/components/PropertiesPanel.tsx +29 -0
  525. package/ui/src/components/ProviderQuotaCard.tsx +417 -0
  526. package/ui/src/components/QuotaBar.tsx +65 -0
  527. package/ui/src/components/ReportsToPicker.tsx +127 -0
  528. package/ui/src/components/ScheduleEditor.tsx +344 -0
  529. package/ui/src/components/ScrollToBottom.tsx +79 -0
  530. package/ui/src/components/Sidebar.tsx +130 -0
  531. package/ui/src/components/SidebarAgents.tsx +146 -0
  532. package/ui/src/components/SidebarNavItem.tsx +92 -0
  533. package/ui/src/components/SidebarProjects.tsx +234 -0
  534. package/ui/src/components/SidebarSection.tsx +17 -0
  535. package/ui/src/components/StatusBadge.tsx +15 -0
  536. package/ui/src/components/StatusIcon.tsx +71 -0
  537. package/ui/src/components/SwipeToArchive.tsx +152 -0
  538. package/ui/src/components/ToastViewport.tsx +99 -0
  539. package/ui/src/components/WorktreeBanner.tsx +25 -0
  540. package/ui/src/components/agent-config-defaults.ts +31 -0
  541. package/ui/src/components/agent-config-primitives.tsx +476 -0
  542. package/ui/src/components/transcript/RunTranscriptView.test.tsx +84 -0
  543. package/ui/src/components/transcript/RunTranscriptView.tsx +1015 -0
  544. package/ui/src/components/transcript/useLiveRunTranscripts.ts +297 -0
  545. package/ui/src/components/ui/avatar.tsx +107 -0
  546. package/ui/src/components/ui/badge.tsx +48 -0
  547. package/ui/src/components/ui/breadcrumb.tsx +109 -0
  548. package/ui/src/components/ui/button.tsx +64 -0
  549. package/ui/src/components/ui/card.tsx +92 -0
  550. package/ui/src/components/ui/checkbox.tsx +32 -0
  551. package/ui/src/components/ui/collapsible.tsx +33 -0
  552. package/ui/src/components/ui/command.tsx +194 -0
  553. package/ui/src/components/ui/dialog.tsx +156 -0
  554. package/ui/src/components/ui/dropdown-menu.tsx +257 -0
  555. package/ui/src/components/ui/input.tsx +21 -0
  556. package/ui/src/components/ui/label.tsx +22 -0
  557. package/ui/src/components/ui/popover.tsx +88 -0
  558. package/ui/src/components/ui/scroll-area.tsx +56 -0
  559. package/ui/src/components/ui/select.tsx +188 -0
  560. package/ui/src/components/ui/separator.tsx +28 -0
  561. package/ui/src/components/ui/sheet.tsx +143 -0
  562. package/ui/src/components/ui/skeleton.tsx +13 -0
  563. package/ui/src/components/ui/tabs.tsx +89 -0
  564. package/ui/src/components/ui/textarea.tsx +18 -0
  565. package/ui/src/components/ui/tooltip.tsx +57 -0
  566. package/ui/src/components/visual-office/AgentAvatar.tsx +99 -0
  567. package/ui/src/components/visual-office/OfficeViewExact.tsx +417 -0
  568. package/ui/src/components/visual-office/i18n.ts +52 -0
  569. package/ui/src/components/visual-office/office-view/CliUsagePanel.tsx +240 -0
  570. package/ui/src/components/visual-office/office-view/VirtualPadOverlay.tsx +104 -0
  571. package/ui/src/components/visual-office/office-view/buildScene-break-room.ts +248 -0
  572. package/ui/src/components/visual-office/office-view/buildScene-ceo-hallway.ts +345 -0
  573. package/ui/src/components/visual-office/office-view/buildScene-department-agent.ts +242 -0
  574. package/ui/src/components/visual-office/office-view/buildScene-departments.ts +360 -0
  575. package/ui/src/components/visual-office/office-view/buildScene-final-layers.ts +113 -0
  576. package/ui/src/components/visual-office/office-view/buildScene-types.ts +91 -0
  577. package/ui/src/components/visual-office/office-view/buildScene.ts +232 -0
  578. package/ui/src/components/visual-office/office-view/drawing-core.ts +374 -0
  579. package/ui/src/components/visual-office/office-view/drawing-furniture-a.ts +338 -0
  580. package/ui/src/components/visual-office/office-view/drawing-furniture-b.ts +241 -0
  581. package/ui/src/components/visual-office/office-view/model.ts +301 -0
  582. package/ui/src/components/visual-office/office-view/officeTicker.ts +455 -0
  583. package/ui/src/components/visual-office/office-view/officeTickerRoomAndDelivery.ts +133 -0
  584. package/ui/src/components/visual-office/office-view/themes-locale.ts +460 -0
  585. package/ui/src/components/visual-office/office-view/useCliUsage.ts +37 -0
  586. package/ui/src/components/visual-office/office-view/useOfficeDeliveryEffects.ts +465 -0
  587. package/ui/src/components/visual-office/office-view/useOfficePixiRuntime.ts +282 -0
  588. package/ui/src/components/visual-office/types.ts +123 -0
  589. package/ui/src/context/BreadcrumbContext.tsx +44 -0
  590. package/ui/src/context/CompanyContext.tsx +151 -0
  591. package/ui/src/context/DialogContext.tsx +135 -0
  592. package/ui/src/context/LiveUpdatesProvider.test.ts +119 -0
  593. package/ui/src/context/LiveUpdatesProvider.tsx +760 -0
  594. package/ui/src/context/PanelContext.tsx +73 -0
  595. package/ui/src/context/SidebarContext.tsx +43 -0
  596. package/ui/src/context/ThemeContext.tsx +83 -0
  597. package/ui/src/context/ToastContext.tsx +172 -0
  598. package/ui/src/fixtures/runTranscriptFixtures.ts +226 -0
  599. package/ui/src/hooks/useAgentOrder.ts +105 -0
  600. package/ui/src/hooks/useAutosaveIndicator.ts +72 -0
  601. package/ui/src/hooks/useCompanyPageMemory.test.ts +90 -0
  602. package/ui/src/hooks/useCompanyPageMemory.ts +79 -0
  603. package/ui/src/hooks/useDateRange.ts +120 -0
  604. package/ui/src/hooks/useInboxBadge.ts +132 -0
  605. package/ui/src/hooks/useKeyboardShortcuts.ts +40 -0
  606. package/ui/src/hooks/useProjectOrder.ts +106 -0
  607. package/ui/src/index.css +770 -0
  608. package/ui/src/lib/agent-icons.ts +99 -0
  609. package/ui/src/lib/agent-order.ts +107 -0
  610. package/ui/src/lib/agent-skills-state.test.ts +90 -0
  611. package/ui/src/lib/agent-skills-state.ts +41 -0
  612. package/ui/src/lib/assignees.test.ts +92 -0
  613. package/ui/src/lib/assignees.ts +82 -0
  614. package/ui/src/lib/color-contrast.ts +107 -0
  615. package/ui/src/lib/company-export-selection.test.ts +41 -0
  616. package/ui/src/lib/company-export-selection.ts +57 -0
  617. package/ui/src/lib/company-page-memory.ts +65 -0
  618. package/ui/src/lib/company-portability-sidebar.test.ts +101 -0
  619. package/ui/src/lib/company-portability-sidebar.ts +62 -0
  620. package/ui/src/lib/company-routes.ts +88 -0
  621. package/ui/src/lib/company-selection.test.ts +34 -0
  622. package/ui/src/lib/company-selection.ts +18 -0
  623. package/ui/src/lib/groupBy.ts +11 -0
  624. package/ui/src/lib/inbox.test.ts +404 -0
  625. package/ui/src/lib/inbox.ts +292 -0
  626. package/ui/src/lib/instance-settings.test.ts +26 -0
  627. package/ui/src/lib/instance-settings.ts +25 -0
  628. package/ui/src/lib/issueDetailBreadcrumb.ts +24 -0
  629. package/ui/src/lib/legacy-agent-config.test.ts +40 -0
  630. package/ui/src/lib/legacy-agent-config.ts +17 -0
  631. package/ui/src/lib/mention-aware-link-node.test.ts +50 -0
  632. package/ui/src/lib/mention-aware-link-node.ts +67 -0
  633. package/ui/src/lib/mention-chips.ts +168 -0
  634. package/ui/src/lib/mention-deletion.test.ts +87 -0
  635. package/ui/src/lib/mention-deletion.ts +143 -0
  636. package/ui/src/lib/model-utils.ts +16 -0
  637. package/ui/src/lib/onboarding-goal.test.ts +22 -0
  638. package/ui/src/lib/onboarding-goal.ts +18 -0
  639. package/ui/src/lib/onboarding-launch.test.ts +131 -0
  640. package/ui/src/lib/onboarding-launch.ts +54 -0
  641. package/ui/src/lib/onboarding-route.test.ts +80 -0
  642. package/ui/src/lib/onboarding-route.ts +51 -0
  643. package/ui/src/lib/portable-files.ts +42 -0
  644. package/ui/src/lib/project-order.ts +71 -0
  645. package/ui/src/lib/queryKeys.ts +140 -0
  646. package/ui/src/lib/recent-assignees.ts +36 -0
  647. package/ui/src/lib/router.tsx +76 -0
  648. package/ui/src/lib/routine-trigger-patch.test.ts +72 -0
  649. package/ui/src/lib/routine-trigger-patch.ts +31 -0
  650. package/ui/src/lib/status-colors.ts +108 -0
  651. package/ui/src/lib/timeAgo.ts +31 -0
  652. package/ui/src/lib/utils.ts +168 -0
  653. package/ui/src/lib/worktree-branding.ts +65 -0
  654. package/ui/src/lib/zip.test.ts +289 -0
  655. package/ui/src/lib/zip.ts +284 -0
  656. package/ui/src/main.tsx +67 -0
  657. package/ui/src/pages/Activity.tsx +141 -0
  658. package/ui/src/pages/AgentDetail.tsx +4053 -0
  659. package/ui/src/pages/Agents.tsx +415 -0
  660. package/ui/src/pages/ApprovalDetail.tsx +369 -0
  661. package/ui/src/pages/Approvals.tsx +132 -0
  662. package/ui/src/pages/Auth.tsx +180 -0
  663. package/ui/src/pages/BoardClaim.tsx +125 -0
  664. package/ui/src/pages/CliAuth.tsx +184 -0
  665. package/ui/src/pages/Companies.tsx +297 -0
  666. package/ui/src/pages/CompanyExport.tsx +1019 -0
  667. package/ui/src/pages/CompanyImport.tsx +1355 -0
  668. package/ui/src/pages/CompanySettings.tsx +661 -0
  669. package/ui/src/pages/CompanySkills.tsx +1171 -0
  670. package/ui/src/pages/Costs.tsx +1103 -0
  671. package/ui/src/pages/Dashboard.tsx +388 -0
  672. package/ui/src/pages/DesignGuide.tsx +1330 -0
  673. package/ui/src/pages/ExecutionWorkspaceDetail.tsx +82 -0
  674. package/ui/src/pages/GoalDetail.tsx +197 -0
  675. package/ui/src/pages/Goals.tsx +63 -0
  676. package/ui/src/pages/Inbox.tsx +1291 -0
  677. package/ui/src/pages/InstanceExperimentalSettings.tsx +139 -0
  678. package/ui/src/pages/InstanceGeneralSettings.tsx +104 -0
  679. package/ui/src/pages/InstanceSettings.tsx +284 -0
  680. package/ui/src/pages/InviteLanding.tsx +320 -0
  681. package/ui/src/pages/IssueDetail.tsx +1201 -0
  682. package/ui/src/pages/Issues.tsx +116 -0
  683. package/ui/src/pages/MyIssues.tsx +72 -0
  684. package/ui/src/pages/NewAgent.tsx +353 -0
  685. package/ui/src/pages/NotFound.tsx +66 -0
  686. package/ui/src/pages/Org.tsx +132 -0
  687. package/ui/src/pages/OrgChart.tsx +447 -0
  688. package/ui/src/pages/PluginManager.tsx +510 -0
  689. package/ui/src/pages/PluginPage.tsx +156 -0
  690. package/ui/src/pages/PluginSettings.tsx +836 -0
  691. package/ui/src/pages/ProjectDetail.tsx +633 -0
  692. package/ui/src/pages/Projects.tsx +87 -0
  693. package/ui/src/pages/RoutineDetail.tsx +1022 -0
  694. package/ui/src/pages/Routines.tsx +661 -0
  695. package/ui/src/pages/RunTranscriptUxLab.tsx +334 -0
  696. package/ui/src/pages/VisualOffice.tsx +243 -0
  697. package/ui/src/plugins/bridge-init.ts +69 -0
  698. package/ui/src/plugins/bridge.ts +476 -0
  699. package/ui/src/plugins/launchers.tsx +834 -0
  700. package/ui/src/plugins/slots.tsx +855 -0
  701. package/ui/tsconfig.json +21 -0
  702. package/ui/vite.config.ts +23 -0
  703. package/ui/vitest.config.ts +14 -0
  704. package/vitest.config.ts +11 -0
@@ -0,0 +1,3863 @@
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { execFile as execFileCallback } from "node:child_process";
4
+ import { promisify } from "node:util";
5
+ import { and, asc, desc, eq, gt, inArray, sql } from "drizzle-orm";
6
+ import type { Db } from "@corporateai/db";
7
+ import type { BillingType } from "@corporateai/shared";
8
+ import {
9
+ agents,
10
+ agentRuntimeState,
11
+ agentTaskSessions,
12
+ agentWakeupRequests,
13
+ heartbeatRunEvents,
14
+ heartbeatRuns,
15
+ issues,
16
+ projects,
17
+ projectWorkspaces,
18
+ } from "@corporateai/db";
19
+ import { conflict, notFound } from "../errors.js";
20
+ import { logger } from "../middleware/logger.js";
21
+ import { publishLiveEvent } from "./live-events.js";
22
+ import { getRunLogStore, type RunLogHandle } from "./run-log-store.js";
23
+ import { getServerAdapter, runningProcesses } from "../adapters/index.js";
24
+ import type { AdapterExecutionResult, AdapterInvocationMeta, AdapterSessionCodec, UsageSummary } from "../adapters/index.js";
25
+ import { createLocalAgentJwt } from "../agent-auth-jwt.js";
26
+ import { parseObject, asBoolean, asNumber, appendWithCap, MAX_EXCERPT_BYTES } from "../adapters/utils.js";
27
+ import { costService } from "./costs.js";
28
+ import { companySkillService } from "./company-skills.js";
29
+ import { budgetService, type BudgetEnforcementScope } from "./budgets.js";
30
+ import { secretService } from "./secrets.js";
31
+ import { resolveDefaultAgentWorkspaceDir, resolveManagedProjectWorkspaceDir } from "../home-paths.js";
32
+ import { summarizeHeartbeatRunResultJson } from "./heartbeat-run-summary.js";
33
+ import {
34
+ buildWorkspaceReadyComment,
35
+ cleanupExecutionWorkspaceArtifacts,
36
+ ensureRuntimeServicesForRun,
37
+ persistAdapterManagedRuntimeServices,
38
+ realizeExecutionWorkspace,
39
+ releaseRuntimeServicesForRun,
40
+ sanitizeRuntimeServiceBaseEnv,
41
+ } from "./workspace-runtime.js";
42
+ import { issueService } from "./issues.js";
43
+ import { executionWorkspaceService } from "./execution-workspaces.js";
44
+ import { workspaceOperationService } from "./workspace-operations.js";
45
+ import {
46
+ buildExecutionWorkspaceAdapterConfig,
47
+ gateProjectExecutionWorkspacePolicy,
48
+ issueExecutionWorkspaceModeForPersistedWorkspace,
49
+ parseIssueExecutionWorkspaceSettings,
50
+ parseProjectExecutionWorkspacePolicy,
51
+ resolveExecutionWorkspaceMode,
52
+ } from "./execution-workspace-policy.js";
53
+ import { instanceSettingsService } from "./instance-settings.js";
54
+ import { redactCurrentUserText, redactCurrentUserValue } from "../log-redaction.js";
55
+ import {
56
+ hasSessionCompactionThresholds,
57
+ resolveSessionCompactionPolicy,
58
+ type SessionCompactionPolicy,
59
+ } from "@corporateai/adapter-utils";
60
+
61
+ const MAX_LIVE_LOG_CHUNK_BYTES = 8 * 1024;
62
+ const HEARTBEAT_MAX_CONCURRENT_RUNS_DEFAULT = 1;
63
+ const HEARTBEAT_MAX_CONCURRENT_RUNS_MAX = 10;
64
+ const DEFERRED_WAKE_CONTEXT_KEY = "_paperclipWakeContext";
65
+ const DETACHED_PROCESS_ERROR_CODE = "process_detached";
66
+ const startLocksByAgent = new Map<string, Promise<void>>();
67
+ const REPO_ONLY_CWD_SENTINEL = "/__paperclip_repo_only__";
68
+ const MANAGED_WORKSPACE_GIT_CLONE_TIMEOUT_MS = 10 * 60 * 1000;
69
+ const execFile = promisify(execFileCallback);
70
+ const SESSIONED_LOCAL_ADAPTERS = new Set([
71
+ "claude_local",
72
+ "codex_local",
73
+ "cursor",
74
+ "gemini_local",
75
+ "opencode_local",
76
+ "pi_local",
77
+ ]);
78
+
79
+ function deriveRepoNameFromRepoUrl(repoUrl: string | null): string | null {
80
+ const trimmed = repoUrl?.trim() ?? "";
81
+ if (!trimmed) return null;
82
+ try {
83
+ const parsed = new URL(trimmed);
84
+ const cleanedPath = parsed.pathname.replace(/\/+$/, "");
85
+ const repoName = cleanedPath.split("/").filter(Boolean).pop()?.replace(/\.git$/i, "") ?? "";
86
+ return repoName || null;
87
+ } catch {
88
+ return null;
89
+ }
90
+ }
91
+
92
+ async function ensureManagedProjectWorkspace(input: {
93
+ companyId: string;
94
+ projectId: string;
95
+ repoUrl: string | null;
96
+ }): Promise<{ cwd: string; warning: string | null }> {
97
+ const cwd = resolveManagedProjectWorkspaceDir({
98
+ companyId: input.companyId,
99
+ projectId: input.projectId,
100
+ repoName: deriveRepoNameFromRepoUrl(input.repoUrl),
101
+ });
102
+ await fs.mkdir(path.dirname(cwd), { recursive: true });
103
+ const stats = await fs.stat(cwd).catch(() => null);
104
+
105
+ if (!input.repoUrl) {
106
+ if (!stats) {
107
+ await fs.mkdir(cwd, { recursive: true });
108
+ }
109
+ return { cwd, warning: null };
110
+ }
111
+
112
+ const gitDirExists = await fs
113
+ .stat(path.resolve(cwd, ".git"))
114
+ .then((entry) => entry.isDirectory())
115
+ .catch(() => false);
116
+ if (gitDirExists) {
117
+ return { cwd, warning: null };
118
+ }
119
+
120
+ if (stats) {
121
+ const entries = await fs.readdir(cwd).catch(() => []);
122
+ if (entries.length > 0) {
123
+ return {
124
+ cwd,
125
+ warning: `Managed workspace path "${cwd}" already exists but is not a git checkout. Using it as-is.`,
126
+ };
127
+ }
128
+ await fs.rm(cwd, { recursive: true, force: true });
129
+ }
130
+
131
+ try {
132
+ await execFile("git", ["clone", input.repoUrl, cwd], {
133
+ env: sanitizeRuntimeServiceBaseEnv(process.env),
134
+ timeout: MANAGED_WORKSPACE_GIT_CLONE_TIMEOUT_MS,
135
+ });
136
+ return { cwd, warning: null };
137
+ } catch (error) {
138
+ const reason = error instanceof Error ? error.message : String(error);
139
+ throw new Error(`Failed to prepare managed checkout for "${input.repoUrl}" at "${cwd}": ${reason}`);
140
+ }
141
+ }
142
+
143
+ const heartbeatRunListColumns = {
144
+ id: heartbeatRuns.id,
145
+ companyId: heartbeatRuns.companyId,
146
+ agentId: heartbeatRuns.agentId,
147
+ invocationSource: heartbeatRuns.invocationSource,
148
+ triggerDetail: heartbeatRuns.triggerDetail,
149
+ status: heartbeatRuns.status,
150
+ startedAt: heartbeatRuns.startedAt,
151
+ finishedAt: heartbeatRuns.finishedAt,
152
+ error: heartbeatRuns.error,
153
+ wakeupRequestId: heartbeatRuns.wakeupRequestId,
154
+ exitCode: heartbeatRuns.exitCode,
155
+ signal: heartbeatRuns.signal,
156
+ usageJson: heartbeatRuns.usageJson,
157
+ resultJson: heartbeatRuns.resultJson,
158
+ sessionIdBefore: heartbeatRuns.sessionIdBefore,
159
+ sessionIdAfter: heartbeatRuns.sessionIdAfter,
160
+ logStore: heartbeatRuns.logStore,
161
+ logRef: heartbeatRuns.logRef,
162
+ logBytes: heartbeatRuns.logBytes,
163
+ logSha256: heartbeatRuns.logSha256,
164
+ logCompressed: heartbeatRuns.logCompressed,
165
+ stdoutExcerpt: sql<string | null>`NULL`.as("stdoutExcerpt"),
166
+ stderrExcerpt: sql<string | null>`NULL`.as("stderrExcerpt"),
167
+ errorCode: heartbeatRuns.errorCode,
168
+ externalRunId: heartbeatRuns.externalRunId,
169
+ processPid: heartbeatRuns.processPid,
170
+ processStartedAt: heartbeatRuns.processStartedAt,
171
+ retryOfRunId: heartbeatRuns.retryOfRunId,
172
+ processLossRetryCount: heartbeatRuns.processLossRetryCount,
173
+ contextSnapshot: heartbeatRuns.contextSnapshot,
174
+ createdAt: heartbeatRuns.createdAt,
175
+ updatedAt: heartbeatRuns.updatedAt,
176
+ } as const;
177
+
178
+ function appendExcerpt(prev: string, chunk: string) {
179
+ return appendWithCap(prev, chunk, MAX_EXCERPT_BYTES);
180
+ }
181
+
182
+ function normalizeMaxConcurrentRuns(value: unknown) {
183
+ const parsed = Math.floor(asNumber(value, HEARTBEAT_MAX_CONCURRENT_RUNS_DEFAULT));
184
+ if (!Number.isFinite(parsed)) return HEARTBEAT_MAX_CONCURRENT_RUNS_DEFAULT;
185
+ return Math.max(HEARTBEAT_MAX_CONCURRENT_RUNS_DEFAULT, Math.min(HEARTBEAT_MAX_CONCURRENT_RUNS_MAX, parsed));
186
+ }
187
+
188
+ async function withAgentStartLock<T>(agentId: string, fn: () => Promise<T>) {
189
+ const previous = startLocksByAgent.get(agentId) ?? Promise.resolve();
190
+ const run = previous.then(fn);
191
+ const marker = run.then(
192
+ () => undefined,
193
+ () => undefined,
194
+ );
195
+ startLocksByAgent.set(agentId, marker);
196
+ try {
197
+ return await run;
198
+ } finally {
199
+ if (startLocksByAgent.get(agentId) === marker) {
200
+ startLocksByAgent.delete(agentId);
201
+ }
202
+ }
203
+ }
204
+
205
+ interface WakeupOptions {
206
+ source?: "timer" | "assignment" | "on_demand" | "automation";
207
+ triggerDetail?: "manual" | "ping" | "callback" | "system";
208
+ reason?: string | null;
209
+ payload?: Record<string, unknown> | null;
210
+ idempotencyKey?: string | null;
211
+ requestedByActorType?: "user" | "agent" | "system";
212
+ requestedByActorId?: string | null;
213
+ contextSnapshot?: Record<string, unknown>;
214
+ }
215
+
216
+ type UsageTotals = {
217
+ inputTokens: number;
218
+ cachedInputTokens: number;
219
+ outputTokens: number;
220
+ };
221
+
222
+ type SessionCompactionDecision = {
223
+ rotate: boolean;
224
+ reason: string | null;
225
+ handoffMarkdown: string | null;
226
+ previousRunId: string | null;
227
+ };
228
+
229
+ interface ParsedIssueAssigneeAdapterOverrides {
230
+ adapterConfig: Record<string, unknown> | null;
231
+ useProjectWorkspace: boolean | null;
232
+ }
233
+
234
+ export type ResolvedWorkspaceForRun = {
235
+ cwd: string;
236
+ source: "project_primary" | "task_session" | "agent_home";
237
+ projectId: string | null;
238
+ workspaceId: string | null;
239
+ repoUrl: string | null;
240
+ repoRef: string | null;
241
+ workspaceHints: Array<{
242
+ workspaceId: string;
243
+ cwd: string | null;
244
+ repoUrl: string | null;
245
+ repoRef: string | null;
246
+ }>;
247
+ warnings: string[];
248
+ };
249
+
250
+ type ProjectWorkspaceCandidate = {
251
+ id: string;
252
+ };
253
+
254
+ export function prioritizeProjectWorkspaceCandidatesForRun<T extends ProjectWorkspaceCandidate>(
255
+ rows: T[],
256
+ preferredWorkspaceId: string | null | undefined,
257
+ ): T[] {
258
+ if (!preferredWorkspaceId) return rows;
259
+ const preferredIndex = rows.findIndex((row) => row.id === preferredWorkspaceId);
260
+ if (preferredIndex <= 0) return rows;
261
+ return [rows[preferredIndex]!, ...rows.slice(0, preferredIndex), ...rows.slice(preferredIndex + 1)];
262
+ }
263
+
264
+ function readNonEmptyString(value: unknown): string | null {
265
+ return typeof value === "string" && value.trim().length > 0 ? value : null;
266
+ }
267
+
268
+ function normalizeLedgerBillingType(value: unknown): BillingType {
269
+ const raw = readNonEmptyString(value);
270
+ switch (raw) {
271
+ case "api":
272
+ case "metered_api":
273
+ return "metered_api";
274
+ case "subscription":
275
+ case "subscription_included":
276
+ return "subscription_included";
277
+ case "subscription_overage":
278
+ return "subscription_overage";
279
+ case "credits":
280
+ return "credits";
281
+ case "fixed":
282
+ return "fixed";
283
+ default:
284
+ return "unknown";
285
+ }
286
+ }
287
+
288
+ function resolveLedgerBiller(result: AdapterExecutionResult): string {
289
+ return readNonEmptyString(result.biller) ?? readNonEmptyString(result.provider) ?? "unknown";
290
+ }
291
+
292
+ function normalizeBilledCostCents(costUsd: number | null | undefined, billingType: BillingType): number {
293
+ if (billingType === "subscription_included") return 0;
294
+ if (typeof costUsd !== "number" || !Number.isFinite(costUsd)) return 0;
295
+ return Math.max(0, Math.round(costUsd * 100));
296
+ }
297
+
298
+ async function resolveLedgerScopeForRun(
299
+ db: Db,
300
+ companyId: string,
301
+ run: typeof heartbeatRuns.$inferSelect,
302
+ ) {
303
+ const context = parseObject(run.contextSnapshot);
304
+ const contextIssueId = readNonEmptyString(context.issueId);
305
+ const contextProjectId = readNonEmptyString(context.projectId);
306
+
307
+ if (!contextIssueId) {
308
+ return {
309
+ issueId: null,
310
+ projectId: contextProjectId,
311
+ };
312
+ }
313
+
314
+ const issue = await db
315
+ .select({
316
+ id: issues.id,
317
+ projectId: issues.projectId,
318
+ })
319
+ .from(issues)
320
+ .where(and(eq(issues.id, contextIssueId), eq(issues.companyId, companyId)))
321
+ .then((rows) => rows[0] ?? null);
322
+
323
+ return {
324
+ issueId: issue?.id ?? null,
325
+ projectId: issue?.projectId ?? contextProjectId,
326
+ };
327
+ }
328
+
329
+ type ResumeSessionRow = {
330
+ sessionParamsJson: Record<string, unknown> | null;
331
+ sessionDisplayId: string | null;
332
+ lastRunId: string | null;
333
+ };
334
+
335
+ export function buildExplicitResumeSessionOverride(input: {
336
+ resumeFromRunId: string;
337
+ resumeRunSessionIdBefore: string | null;
338
+ resumeRunSessionIdAfter: string | null;
339
+ taskSession: ResumeSessionRow | null;
340
+ sessionCodec: AdapterSessionCodec;
341
+ }) {
342
+ const desiredDisplayId = truncateDisplayId(
343
+ input.resumeRunSessionIdAfter ?? input.resumeRunSessionIdBefore,
344
+ );
345
+ const taskSessionParams = normalizeSessionParams(
346
+ input.sessionCodec.deserialize(input.taskSession?.sessionParamsJson ?? null),
347
+ );
348
+ const taskSessionDisplayId = truncateDisplayId(
349
+ input.taskSession?.sessionDisplayId ??
350
+ (input.sessionCodec.getDisplayId ? input.sessionCodec.getDisplayId(taskSessionParams) : null) ??
351
+ readNonEmptyString(taskSessionParams?.sessionId),
352
+ );
353
+ const canReuseTaskSessionParams =
354
+ input.taskSession != null &&
355
+ (
356
+ input.taskSession.lastRunId === input.resumeFromRunId ||
357
+ (!!desiredDisplayId && taskSessionDisplayId === desiredDisplayId)
358
+ );
359
+ const sessionParams =
360
+ canReuseTaskSessionParams
361
+ ? taskSessionParams
362
+ : desiredDisplayId
363
+ ? { sessionId: desiredDisplayId }
364
+ : null;
365
+ const sessionDisplayId = desiredDisplayId ?? (canReuseTaskSessionParams ? taskSessionDisplayId : null);
366
+
367
+ if (!sessionDisplayId && !sessionParams) return null;
368
+ return {
369
+ sessionDisplayId,
370
+ sessionParams,
371
+ };
372
+ }
373
+
374
+ function normalizeUsageTotals(usage: UsageSummary | null | undefined): UsageTotals | null {
375
+ if (!usage) return null;
376
+ return {
377
+ inputTokens: Math.max(0, Math.floor(asNumber(usage.inputTokens, 0))),
378
+ cachedInputTokens: Math.max(0, Math.floor(asNumber(usage.cachedInputTokens, 0))),
379
+ outputTokens: Math.max(0, Math.floor(asNumber(usage.outputTokens, 0))),
380
+ };
381
+ }
382
+
383
+ function readRawUsageTotals(usageJson: unknown): UsageTotals | null {
384
+ const parsed = parseObject(usageJson);
385
+ if (Object.keys(parsed).length === 0) return null;
386
+
387
+ const inputTokens = Math.max(
388
+ 0,
389
+ Math.floor(asNumber(parsed.rawInputTokens, asNumber(parsed.inputTokens, 0))),
390
+ );
391
+ const cachedInputTokens = Math.max(
392
+ 0,
393
+ Math.floor(asNumber(parsed.rawCachedInputTokens, asNumber(parsed.cachedInputTokens, 0))),
394
+ );
395
+ const outputTokens = Math.max(
396
+ 0,
397
+ Math.floor(asNumber(parsed.rawOutputTokens, asNumber(parsed.outputTokens, 0))),
398
+ );
399
+
400
+ if (inputTokens <= 0 && cachedInputTokens <= 0 && outputTokens <= 0) {
401
+ return null;
402
+ }
403
+
404
+ return {
405
+ inputTokens,
406
+ cachedInputTokens,
407
+ outputTokens,
408
+ };
409
+ }
410
+
411
+ function deriveNormalizedUsageDelta(current: UsageTotals | null, previous: UsageTotals | null): UsageTotals | null {
412
+ if (!current) return null;
413
+ if (!previous) return { ...current };
414
+
415
+ const inputTokens = current.inputTokens >= previous.inputTokens
416
+ ? current.inputTokens - previous.inputTokens
417
+ : current.inputTokens;
418
+ const cachedInputTokens = current.cachedInputTokens >= previous.cachedInputTokens
419
+ ? current.cachedInputTokens - previous.cachedInputTokens
420
+ : current.cachedInputTokens;
421
+ const outputTokens = current.outputTokens >= previous.outputTokens
422
+ ? current.outputTokens - previous.outputTokens
423
+ : current.outputTokens;
424
+
425
+ return {
426
+ inputTokens: Math.max(0, inputTokens),
427
+ cachedInputTokens: Math.max(0, cachedInputTokens),
428
+ outputTokens: Math.max(0, outputTokens),
429
+ };
430
+ }
431
+
432
+ function formatCount(value: number | null | undefined) {
433
+ if (typeof value !== "number" || !Number.isFinite(value)) return "0";
434
+ return value.toLocaleString("en-US");
435
+ }
436
+
437
+ export function parseSessionCompactionPolicy(agent: typeof agents.$inferSelect): SessionCompactionPolicy {
438
+ return resolveSessionCompactionPolicy(agent.adapterType, agent.runtimeConfig).policy;
439
+ }
440
+
441
+ export function resolveRuntimeSessionParamsForWorkspace(input: {
442
+ agentId: string;
443
+ previousSessionParams: Record<string, unknown> | null;
444
+ resolvedWorkspace: ResolvedWorkspaceForRun;
445
+ }) {
446
+ const { agentId, previousSessionParams, resolvedWorkspace } = input;
447
+ const previousSessionId = readNonEmptyString(previousSessionParams?.sessionId);
448
+ const previousCwd = readNonEmptyString(previousSessionParams?.cwd);
449
+ if (!previousSessionId || !previousCwd) {
450
+ return {
451
+ sessionParams: previousSessionParams,
452
+ warning: null as string | null,
453
+ };
454
+ }
455
+ if (resolvedWorkspace.source !== "project_primary") {
456
+ return {
457
+ sessionParams: previousSessionParams,
458
+ warning: null as string | null,
459
+ };
460
+ }
461
+ const projectCwd = readNonEmptyString(resolvedWorkspace.cwd);
462
+ if (!projectCwd) {
463
+ return {
464
+ sessionParams: previousSessionParams,
465
+ warning: null as string | null,
466
+ };
467
+ }
468
+ const fallbackAgentHomeCwd = resolveDefaultAgentWorkspaceDir(agentId);
469
+ if (path.resolve(previousCwd) !== path.resolve(fallbackAgentHomeCwd)) {
470
+ return {
471
+ sessionParams: previousSessionParams,
472
+ warning: null as string | null,
473
+ };
474
+ }
475
+ if (path.resolve(projectCwd) === path.resolve(previousCwd)) {
476
+ return {
477
+ sessionParams: previousSessionParams,
478
+ warning: null as string | null,
479
+ };
480
+ }
481
+ const previousWorkspaceId = readNonEmptyString(previousSessionParams?.workspaceId);
482
+ if (
483
+ previousWorkspaceId &&
484
+ resolvedWorkspace.workspaceId &&
485
+ previousWorkspaceId !== resolvedWorkspace.workspaceId
486
+ ) {
487
+ return {
488
+ sessionParams: previousSessionParams,
489
+ warning: null as string | null,
490
+ };
491
+ }
492
+
493
+ const migratedSessionParams: Record<string, unknown> = {
494
+ ...(previousSessionParams ?? {}),
495
+ cwd: projectCwd,
496
+ };
497
+ if (resolvedWorkspace.workspaceId) migratedSessionParams.workspaceId = resolvedWorkspace.workspaceId;
498
+ if (resolvedWorkspace.repoUrl) migratedSessionParams.repoUrl = resolvedWorkspace.repoUrl;
499
+ if (resolvedWorkspace.repoRef) migratedSessionParams.repoRef = resolvedWorkspace.repoRef;
500
+
501
+ return {
502
+ sessionParams: migratedSessionParams,
503
+ warning:
504
+ `Project workspace "${projectCwd}" is now available. ` +
505
+ `Attempting to resume session "${previousSessionId}" that was previously saved in fallback workspace "${previousCwd}".`,
506
+ };
507
+ }
508
+
509
+ function parseIssueAssigneeAdapterOverrides(
510
+ raw: unknown,
511
+ ): ParsedIssueAssigneeAdapterOverrides | null {
512
+ const parsed = parseObject(raw);
513
+ const parsedAdapterConfig = parseObject(parsed.adapterConfig);
514
+ const adapterConfig =
515
+ Object.keys(parsedAdapterConfig).length > 0 ? parsedAdapterConfig : null;
516
+ const useProjectWorkspace =
517
+ typeof parsed.useProjectWorkspace === "boolean"
518
+ ? parsed.useProjectWorkspace
519
+ : null;
520
+ if (!adapterConfig && useProjectWorkspace === null) return null;
521
+ return {
522
+ adapterConfig,
523
+ useProjectWorkspace,
524
+ };
525
+ }
526
+
527
+ function deriveTaskKey(
528
+ contextSnapshot: Record<string, unknown> | null | undefined,
529
+ payload: Record<string, unknown> | null | undefined,
530
+ ) {
531
+ return (
532
+ readNonEmptyString(contextSnapshot?.taskKey) ??
533
+ readNonEmptyString(contextSnapshot?.taskId) ??
534
+ readNonEmptyString(contextSnapshot?.issueId) ??
535
+ readNonEmptyString(payload?.taskKey) ??
536
+ readNonEmptyString(payload?.taskId) ??
537
+ readNonEmptyString(payload?.issueId) ??
538
+ null
539
+ );
540
+ }
541
+
542
+ export function shouldResetTaskSessionForWake(
543
+ contextSnapshot: Record<string, unknown> | null | undefined,
544
+ ) {
545
+ if (contextSnapshot?.forceFreshSession === true) return true;
546
+
547
+ const wakeReason = readNonEmptyString(contextSnapshot?.wakeReason);
548
+ if (wakeReason === "issue_assigned") return true;
549
+ return false;
550
+ }
551
+
552
+ export function formatRuntimeWorkspaceWarningLog(warning: string) {
553
+ return {
554
+ stream: "stdout" as const,
555
+ chunk: `[paperclip] ${warning}\n`,
556
+ };
557
+ }
558
+
559
+ function describeSessionResetReason(
560
+ contextSnapshot: Record<string, unknown> | null | undefined,
561
+ ) {
562
+ if (contextSnapshot?.forceFreshSession === true) return "forceFreshSession was requested";
563
+
564
+ const wakeReason = readNonEmptyString(contextSnapshot?.wakeReason);
565
+ if (wakeReason === "issue_assigned") return "wake reason is issue_assigned";
566
+ return null;
567
+ }
568
+
569
+ function deriveCommentId(
570
+ contextSnapshot: Record<string, unknown> | null | undefined,
571
+ payload: Record<string, unknown> | null | undefined,
572
+ ) {
573
+ return (
574
+ readNonEmptyString(contextSnapshot?.wakeCommentId) ??
575
+ readNonEmptyString(contextSnapshot?.commentId) ??
576
+ readNonEmptyString(payload?.commentId) ??
577
+ null
578
+ );
579
+ }
580
+
581
+ function enrichWakeContextSnapshot(input: {
582
+ contextSnapshot: Record<string, unknown>;
583
+ reason: string | null;
584
+ source: WakeupOptions["source"];
585
+ triggerDetail: WakeupOptions["triggerDetail"] | null;
586
+ payload: Record<string, unknown> | null;
587
+ }) {
588
+ const { contextSnapshot, reason, source, triggerDetail, payload } = input;
589
+ const issueIdFromPayload = readNonEmptyString(payload?.["issueId"]);
590
+ const commentIdFromPayload = readNonEmptyString(payload?.["commentId"]);
591
+ const taskKey = deriveTaskKey(contextSnapshot, payload);
592
+ const wakeCommentId = deriveCommentId(contextSnapshot, payload);
593
+
594
+ if (!readNonEmptyString(contextSnapshot["wakeReason"]) && reason) {
595
+ contextSnapshot.wakeReason = reason;
596
+ }
597
+ if (!readNonEmptyString(contextSnapshot["issueId"]) && issueIdFromPayload) {
598
+ contextSnapshot.issueId = issueIdFromPayload;
599
+ }
600
+ if (!readNonEmptyString(contextSnapshot["taskId"]) && issueIdFromPayload) {
601
+ contextSnapshot.taskId = issueIdFromPayload;
602
+ }
603
+ if (!readNonEmptyString(contextSnapshot["taskKey"]) && taskKey) {
604
+ contextSnapshot.taskKey = taskKey;
605
+ }
606
+ if (!readNonEmptyString(contextSnapshot["commentId"]) && commentIdFromPayload) {
607
+ contextSnapshot.commentId = commentIdFromPayload;
608
+ }
609
+ if (!readNonEmptyString(contextSnapshot["wakeCommentId"]) && wakeCommentId) {
610
+ contextSnapshot.wakeCommentId = wakeCommentId;
611
+ }
612
+ if (!readNonEmptyString(contextSnapshot["wakeSource"]) && source) {
613
+ contextSnapshot.wakeSource = source;
614
+ }
615
+ if (!readNonEmptyString(contextSnapshot["wakeTriggerDetail"]) && triggerDetail) {
616
+ contextSnapshot.wakeTriggerDetail = triggerDetail;
617
+ }
618
+
619
+ return {
620
+ contextSnapshot,
621
+ issueIdFromPayload,
622
+ commentIdFromPayload,
623
+ taskKey,
624
+ wakeCommentId,
625
+ };
626
+ }
627
+
628
+ function mergeCoalescedContextSnapshot(
629
+ existingRaw: unknown,
630
+ incoming: Record<string, unknown>,
631
+ ) {
632
+ const existing = parseObject(existingRaw);
633
+ const merged: Record<string, unknown> = {
634
+ ...existing,
635
+ ...incoming,
636
+ };
637
+ const commentId = deriveCommentId(incoming, null);
638
+ if (commentId) {
639
+ merged.commentId = commentId;
640
+ merged.wakeCommentId = commentId;
641
+ }
642
+ return merged;
643
+ }
644
+
645
+ function runTaskKey(run: typeof heartbeatRuns.$inferSelect) {
646
+ return deriveTaskKey(run.contextSnapshot as Record<string, unknown> | null, null);
647
+ }
648
+
649
+ function isSameTaskScope(left: string | null, right: string | null) {
650
+ return (left ?? null) === (right ?? null);
651
+ }
652
+
653
+ function isTrackedLocalChildProcessAdapter(adapterType: string) {
654
+ return SESSIONED_LOCAL_ADAPTERS.has(adapterType);
655
+ }
656
+
657
+ // A positive liveness check means some process currently owns the PID.
658
+ // On Linux, PIDs can be recycled, so this is a best-effort signal rather
659
+ // than proof that the original child is still alive.
660
+ function isProcessAlive(pid: number | null | undefined) {
661
+ if (typeof pid !== "number" || !Number.isInteger(pid) || pid <= 0) return false;
662
+ try {
663
+ process.kill(pid, 0);
664
+ return true;
665
+ } catch (error) {
666
+ const code = (error as NodeJS.ErrnoException | undefined)?.code;
667
+ if (code === "EPERM") return true;
668
+ if (code === "ESRCH") return false;
669
+ return false;
670
+ }
671
+ }
672
+
673
+ function truncateDisplayId(value: string | null | undefined, max = 128) {
674
+ if (!value) return null;
675
+ return value.length > max ? value.slice(0, max) : value;
676
+ }
677
+
678
+ function normalizeAgentNameKey(value: string | null | undefined) {
679
+ if (typeof value !== "string") return null;
680
+ const normalized = value.trim().toLowerCase();
681
+ return normalized.length > 0 ? normalized : null;
682
+ }
683
+
684
+ const defaultSessionCodec: AdapterSessionCodec = {
685
+ deserialize(raw: unknown) {
686
+ const asObj = parseObject(raw);
687
+ if (Object.keys(asObj).length > 0) return asObj;
688
+ const sessionId = readNonEmptyString((raw as Record<string, unknown> | null)?.sessionId);
689
+ if (sessionId) return { sessionId };
690
+ return null;
691
+ },
692
+ serialize(params: Record<string, unknown> | null) {
693
+ if (!params || Object.keys(params).length === 0) return null;
694
+ return params;
695
+ },
696
+ getDisplayId(params: Record<string, unknown> | null) {
697
+ return readNonEmptyString(params?.sessionId);
698
+ },
699
+ };
700
+
701
+ function getAdapterSessionCodec(adapterType: string) {
702
+ const adapter = getServerAdapter(adapterType);
703
+ return adapter.sessionCodec ?? defaultSessionCodec;
704
+ }
705
+
706
+ function normalizeSessionParams(params: Record<string, unknown> | null | undefined) {
707
+ if (!params) return null;
708
+ return Object.keys(params).length > 0 ? params : null;
709
+ }
710
+
711
+ function resolveNextSessionState(input: {
712
+ codec: AdapterSessionCodec;
713
+ adapterResult: AdapterExecutionResult;
714
+ previousParams: Record<string, unknown> | null;
715
+ previousDisplayId: string | null;
716
+ previousLegacySessionId: string | null;
717
+ }) {
718
+ const { codec, adapterResult, previousParams, previousDisplayId, previousLegacySessionId } = input;
719
+
720
+ if (adapterResult.clearSession) {
721
+ return {
722
+ params: null as Record<string, unknown> | null,
723
+ displayId: null as string | null,
724
+ legacySessionId: null as string | null,
725
+ };
726
+ }
727
+
728
+ const explicitParams = adapterResult.sessionParams;
729
+ const hasExplicitParams = adapterResult.sessionParams !== undefined;
730
+ const hasExplicitSessionId = adapterResult.sessionId !== undefined;
731
+ const explicitSessionId = readNonEmptyString(adapterResult.sessionId);
732
+ const hasExplicitDisplay = adapterResult.sessionDisplayId !== undefined;
733
+ const explicitDisplayId = readNonEmptyString(adapterResult.sessionDisplayId);
734
+ const shouldUsePrevious = !hasExplicitParams && !hasExplicitSessionId && !hasExplicitDisplay;
735
+
736
+ const candidateParams =
737
+ hasExplicitParams
738
+ ? explicitParams
739
+ : hasExplicitSessionId
740
+ ? (explicitSessionId ? { sessionId: explicitSessionId } : null)
741
+ : previousParams;
742
+
743
+ const serialized = normalizeSessionParams(codec.serialize(normalizeSessionParams(candidateParams) ?? null));
744
+ const deserialized = normalizeSessionParams(codec.deserialize(serialized));
745
+
746
+ const displayId = truncateDisplayId(
747
+ explicitDisplayId ??
748
+ (codec.getDisplayId ? codec.getDisplayId(deserialized) : null) ??
749
+ readNonEmptyString(deserialized?.sessionId) ??
750
+ (shouldUsePrevious ? previousDisplayId : null) ??
751
+ explicitSessionId ??
752
+ (shouldUsePrevious ? previousLegacySessionId : null),
753
+ );
754
+
755
+ const legacySessionId =
756
+ explicitSessionId ??
757
+ readNonEmptyString(deserialized?.sessionId) ??
758
+ displayId ??
759
+ (shouldUsePrevious ? previousLegacySessionId : null);
760
+
761
+ return {
762
+ params: serialized,
763
+ displayId,
764
+ legacySessionId,
765
+ };
766
+ }
767
+
768
+ export function heartbeatService(db: Db) {
769
+ const instanceSettings = instanceSettingsService(db);
770
+ const getCurrentUserRedactionOptions = async () => ({
771
+ enabled: (await instanceSettings.getGeneral()).censorUsernameInLogs,
772
+ });
773
+
774
+ const runLogStore = getRunLogStore();
775
+ const secretsSvc = secretService(db);
776
+ const companySkills = companySkillService(db);
777
+ const issuesSvc = issueService(db);
778
+ const executionWorkspacesSvc = executionWorkspaceService(db);
779
+ const workspaceOperationsSvc = workspaceOperationService(db);
780
+ const activeRunExecutions = new Set<string>();
781
+ const budgetHooks = {
782
+ cancelWorkForScope: cancelBudgetScopeWork,
783
+ };
784
+ const budgets = budgetService(db, budgetHooks);
785
+
786
+ async function getAgent(agentId: string) {
787
+ return db
788
+ .select()
789
+ .from(agents)
790
+ .where(eq(agents.id, agentId))
791
+ .then((rows) => rows[0] ?? null);
792
+ }
793
+
794
+ async function getRun(runId: string) {
795
+ return db
796
+ .select()
797
+ .from(heartbeatRuns)
798
+ .where(eq(heartbeatRuns.id, runId))
799
+ .then((rows) => rows[0] ?? null);
800
+ }
801
+
802
+ async function getRuntimeState(agentId: string) {
803
+ return db
804
+ .select()
805
+ .from(agentRuntimeState)
806
+ .where(eq(agentRuntimeState.agentId, agentId))
807
+ .then((rows) => rows[0] ?? null);
808
+ }
809
+
810
+ async function getTaskSession(
811
+ companyId: string,
812
+ agentId: string,
813
+ adapterType: string,
814
+ taskKey: string,
815
+ ) {
816
+ return db
817
+ .select()
818
+ .from(agentTaskSessions)
819
+ .where(
820
+ and(
821
+ eq(agentTaskSessions.companyId, companyId),
822
+ eq(agentTaskSessions.agentId, agentId),
823
+ eq(agentTaskSessions.adapterType, adapterType),
824
+ eq(agentTaskSessions.taskKey, taskKey),
825
+ ),
826
+ )
827
+ .then((rows) => rows[0] ?? null);
828
+ }
829
+
830
+ async function getLatestRunForSession(
831
+ agentId: string,
832
+ sessionId: string,
833
+ opts?: { excludeRunId?: string | null },
834
+ ) {
835
+ const conditions = [
836
+ eq(heartbeatRuns.agentId, agentId),
837
+ eq(heartbeatRuns.sessionIdAfter, sessionId),
838
+ ];
839
+ if (opts?.excludeRunId) {
840
+ conditions.push(sql`${heartbeatRuns.id} <> ${opts.excludeRunId}`);
841
+ }
842
+ return db
843
+ .select()
844
+ .from(heartbeatRuns)
845
+ .where(and(...conditions))
846
+ .orderBy(desc(heartbeatRuns.createdAt))
847
+ .limit(1)
848
+ .then((rows) => rows[0] ?? null);
849
+ }
850
+
851
+ async function getOldestRunForSession(agentId: string, sessionId: string) {
852
+ return db
853
+ .select({
854
+ id: heartbeatRuns.id,
855
+ createdAt: heartbeatRuns.createdAt,
856
+ })
857
+ .from(heartbeatRuns)
858
+ .where(and(eq(heartbeatRuns.agentId, agentId), eq(heartbeatRuns.sessionIdAfter, sessionId)))
859
+ .orderBy(asc(heartbeatRuns.createdAt), asc(heartbeatRuns.id))
860
+ .limit(1)
861
+ .then((rows) => rows[0] ?? null);
862
+ }
863
+
864
+ async function resolveNormalizedUsageForSession(input: {
865
+ agentId: string;
866
+ runId: string;
867
+ sessionId: string | null;
868
+ rawUsage: UsageTotals | null;
869
+ }) {
870
+ const { agentId, runId, sessionId, rawUsage } = input;
871
+ if (!sessionId || !rawUsage) {
872
+ return {
873
+ normalizedUsage: rawUsage,
874
+ previousRawUsage: null as UsageTotals | null,
875
+ derivedFromSessionTotals: false,
876
+ };
877
+ }
878
+
879
+ const previousRun = await getLatestRunForSession(agentId, sessionId, { excludeRunId: runId });
880
+ const previousRawUsage = readRawUsageTotals(previousRun?.usageJson);
881
+ return {
882
+ normalizedUsage: deriveNormalizedUsageDelta(rawUsage, previousRawUsage),
883
+ previousRawUsage,
884
+ derivedFromSessionTotals: previousRawUsage !== null,
885
+ };
886
+ }
887
+
888
+ async function evaluateSessionCompaction(input: {
889
+ agent: typeof agents.$inferSelect;
890
+ sessionId: string | null;
891
+ issueId: string | null;
892
+ }): Promise<SessionCompactionDecision> {
893
+ const { agent, sessionId, issueId } = input;
894
+ if (!sessionId) {
895
+ return {
896
+ rotate: false,
897
+ reason: null,
898
+ handoffMarkdown: null,
899
+ previousRunId: null,
900
+ };
901
+ }
902
+
903
+ const policy = parseSessionCompactionPolicy(agent);
904
+ if (!policy.enabled || !hasSessionCompactionThresholds(policy)) {
905
+ return {
906
+ rotate: false,
907
+ reason: null,
908
+ handoffMarkdown: null,
909
+ previousRunId: null,
910
+ };
911
+ }
912
+
913
+ const fetchLimit = Math.max(policy.maxSessionRuns > 0 ? policy.maxSessionRuns + 1 : 0, 4);
914
+ const runs = await db
915
+ .select({
916
+ id: heartbeatRuns.id,
917
+ createdAt: heartbeatRuns.createdAt,
918
+ usageJson: heartbeatRuns.usageJson,
919
+ resultJson: heartbeatRuns.resultJson,
920
+ error: heartbeatRuns.error,
921
+ })
922
+ .from(heartbeatRuns)
923
+ .where(and(eq(heartbeatRuns.agentId, agent.id), eq(heartbeatRuns.sessionIdAfter, sessionId)))
924
+ .orderBy(desc(heartbeatRuns.createdAt))
925
+ .limit(fetchLimit);
926
+
927
+ if (runs.length === 0) {
928
+ return {
929
+ rotate: false,
930
+ reason: null,
931
+ handoffMarkdown: null,
932
+ previousRunId: null,
933
+ };
934
+ }
935
+
936
+ const latestRun = runs[0] ?? null;
937
+ const oldestRun =
938
+ policy.maxSessionAgeHours > 0
939
+ ? await getOldestRunForSession(agent.id, sessionId)
940
+ : runs[runs.length - 1] ?? latestRun;
941
+ const latestRawUsage = readRawUsageTotals(latestRun?.usageJson);
942
+ const sessionAgeHours =
943
+ latestRun && oldestRun
944
+ ? Math.max(
945
+ 0,
946
+ (new Date(latestRun.createdAt).getTime() - new Date(oldestRun.createdAt).getTime()) / (1000 * 60 * 60),
947
+ )
948
+ : 0;
949
+
950
+ let reason: string | null = null;
951
+ if (policy.maxSessionRuns > 0 && runs.length > policy.maxSessionRuns) {
952
+ reason = `session exceeded ${policy.maxSessionRuns} runs`;
953
+ } else if (
954
+ policy.maxRawInputTokens > 0 &&
955
+ latestRawUsage &&
956
+ latestRawUsage.inputTokens >= policy.maxRawInputTokens
957
+ ) {
958
+ reason =
959
+ `session raw input reached ${formatCount(latestRawUsage.inputTokens)} tokens ` +
960
+ `(threshold ${formatCount(policy.maxRawInputTokens)})`;
961
+ } else if (policy.maxSessionAgeHours > 0 && sessionAgeHours >= policy.maxSessionAgeHours) {
962
+ reason = `session age reached ${Math.floor(sessionAgeHours)} hours`;
963
+ }
964
+
965
+ if (!reason || !latestRun) {
966
+ return {
967
+ rotate: false,
968
+ reason: null,
969
+ handoffMarkdown: null,
970
+ previousRunId: latestRun?.id ?? null,
971
+ };
972
+ }
973
+
974
+ const latestSummary = summarizeHeartbeatRunResultJson(latestRun.resultJson);
975
+ const latestTextSummary =
976
+ readNonEmptyString(latestSummary?.summary) ??
977
+ readNonEmptyString(latestSummary?.result) ??
978
+ readNonEmptyString(latestSummary?.message) ??
979
+ readNonEmptyString(latestRun.error);
980
+
981
+ const handoffMarkdown = [
982
+ "Corporate session handoff:",
983
+ `- Previous session: ${sessionId}`,
984
+ issueId ? `- Issue: ${issueId}` : "",
985
+ `- Rotation reason: ${reason}`,
986
+ latestTextSummary ? `- Last run summary: ${latestTextSummary}` : "",
987
+ "Continue from the current task state. Rebuild only the minimum context you need.",
988
+ ]
989
+ .filter(Boolean)
990
+ .join("\n");
991
+
992
+ return {
993
+ rotate: true,
994
+ reason,
995
+ handoffMarkdown,
996
+ previousRunId: latestRun.id,
997
+ };
998
+ }
999
+
1000
+ async function resolveSessionBeforeForWakeup(
1001
+ agent: typeof agents.$inferSelect,
1002
+ taskKey: string | null,
1003
+ ) {
1004
+ if (taskKey) {
1005
+ const codec = getAdapterSessionCodec(agent.adapterType);
1006
+ const existingTaskSession = await getTaskSession(
1007
+ agent.companyId,
1008
+ agent.id,
1009
+ agent.adapterType,
1010
+ taskKey,
1011
+ );
1012
+ const parsedParams = normalizeSessionParams(
1013
+ codec.deserialize(existingTaskSession?.sessionParamsJson ?? null),
1014
+ );
1015
+ return truncateDisplayId(
1016
+ existingTaskSession?.sessionDisplayId ??
1017
+ (codec.getDisplayId ? codec.getDisplayId(parsedParams) : null) ??
1018
+ readNonEmptyString(parsedParams?.sessionId),
1019
+ );
1020
+ }
1021
+
1022
+ const runtimeForRun = await getRuntimeState(agent.id);
1023
+ return runtimeForRun?.sessionId ?? null;
1024
+ }
1025
+
1026
+ async function resolveExplicitResumeSessionOverride(
1027
+ agent: typeof agents.$inferSelect,
1028
+ payload: Record<string, unknown> | null,
1029
+ taskKey: string | null,
1030
+ ) {
1031
+ const resumeFromRunId = readNonEmptyString(payload?.resumeFromRunId);
1032
+ if (!resumeFromRunId) return null;
1033
+
1034
+ const resumeRun = await db
1035
+ .select({
1036
+ id: heartbeatRuns.id,
1037
+ contextSnapshot: heartbeatRuns.contextSnapshot,
1038
+ sessionIdBefore: heartbeatRuns.sessionIdBefore,
1039
+ sessionIdAfter: heartbeatRuns.sessionIdAfter,
1040
+ })
1041
+ .from(heartbeatRuns)
1042
+ .where(
1043
+ and(
1044
+ eq(heartbeatRuns.id, resumeFromRunId),
1045
+ eq(heartbeatRuns.companyId, agent.companyId),
1046
+ eq(heartbeatRuns.agentId, agent.id),
1047
+ ),
1048
+ )
1049
+ .then((rows) => rows[0] ?? null);
1050
+ if (!resumeRun) return null;
1051
+
1052
+ const resumeContext = parseObject(resumeRun.contextSnapshot);
1053
+ const resumeTaskKey = deriveTaskKey(resumeContext, null) ?? taskKey;
1054
+ const resumeTaskSession = resumeTaskKey
1055
+ ? await getTaskSession(agent.companyId, agent.id, agent.adapterType, resumeTaskKey)
1056
+ : null;
1057
+ const sessionCodec = getAdapterSessionCodec(agent.adapterType);
1058
+ const sessionOverride = buildExplicitResumeSessionOverride({
1059
+ resumeFromRunId,
1060
+ resumeRunSessionIdBefore: resumeRun.sessionIdBefore,
1061
+ resumeRunSessionIdAfter: resumeRun.sessionIdAfter,
1062
+ taskSession: resumeTaskSession,
1063
+ sessionCodec,
1064
+ });
1065
+ if (!sessionOverride) return null;
1066
+
1067
+ return {
1068
+ resumeFromRunId,
1069
+ taskKey: resumeTaskKey,
1070
+ issueId: readNonEmptyString(resumeContext.issueId),
1071
+ taskId: readNonEmptyString(resumeContext.taskId) ?? readNonEmptyString(resumeContext.issueId),
1072
+ sessionDisplayId: sessionOverride.sessionDisplayId,
1073
+ sessionParams: sessionOverride.sessionParams,
1074
+ };
1075
+ }
1076
+
1077
+ async function resolveWorkspaceForRun(
1078
+ agent: typeof agents.$inferSelect,
1079
+ context: Record<string, unknown>,
1080
+ previousSessionParams: Record<string, unknown> | null,
1081
+ opts?: { useProjectWorkspace?: boolean | null },
1082
+ ): Promise<ResolvedWorkspaceForRun> {
1083
+ const issueId = readNonEmptyString(context.issueId);
1084
+ const contextProjectId = readNonEmptyString(context.projectId);
1085
+ const contextProjectWorkspaceId = readNonEmptyString(context.projectWorkspaceId);
1086
+ const issueProjectRef = issueId
1087
+ ? await db
1088
+ .select({
1089
+ projectId: issues.projectId,
1090
+ projectWorkspaceId: issues.projectWorkspaceId,
1091
+ })
1092
+ .from(issues)
1093
+ .where(and(eq(issues.id, issueId), eq(issues.companyId, agent.companyId)))
1094
+ .then((rows) => rows[0] ?? null)
1095
+ : null;
1096
+ const issueProjectId = issueProjectRef?.projectId ?? null;
1097
+ const preferredProjectWorkspaceId =
1098
+ issueProjectRef?.projectWorkspaceId ?? contextProjectWorkspaceId ?? null;
1099
+ const resolvedProjectId = issueProjectId ?? contextProjectId;
1100
+ const useProjectWorkspace = opts?.useProjectWorkspace !== false;
1101
+ const workspaceProjectId = useProjectWorkspace ? resolvedProjectId : null;
1102
+
1103
+ const unorderedProjectWorkspaceRows = workspaceProjectId
1104
+ ? await db
1105
+ .select()
1106
+ .from(projectWorkspaces)
1107
+ .where(
1108
+ and(
1109
+ eq(projectWorkspaces.companyId, agent.companyId),
1110
+ eq(projectWorkspaces.projectId, workspaceProjectId),
1111
+ ),
1112
+ )
1113
+ .orderBy(asc(projectWorkspaces.createdAt), asc(projectWorkspaces.id))
1114
+ : [];
1115
+ const projectWorkspaceRows = prioritizeProjectWorkspaceCandidatesForRun(
1116
+ unorderedProjectWorkspaceRows,
1117
+ preferredProjectWorkspaceId,
1118
+ );
1119
+
1120
+ const workspaceHints = projectWorkspaceRows.map((workspace) => ({
1121
+ workspaceId: workspace.id,
1122
+ cwd: readNonEmptyString(workspace.cwd),
1123
+ repoUrl: readNonEmptyString(workspace.repoUrl),
1124
+ repoRef: readNonEmptyString(workspace.repoRef),
1125
+ }));
1126
+
1127
+ if (projectWorkspaceRows.length > 0) {
1128
+ const preferredWorkspace = preferredProjectWorkspaceId
1129
+ ? projectWorkspaceRows.find((workspace) => workspace.id === preferredProjectWorkspaceId) ?? null
1130
+ : null;
1131
+ const missingProjectCwds: string[] = [];
1132
+ let hasConfiguredProjectCwd = false;
1133
+ let preferredWorkspaceWarning: string | null = null;
1134
+ if (preferredProjectWorkspaceId && !preferredWorkspace) {
1135
+ preferredWorkspaceWarning =
1136
+ `Selected project workspace "${preferredProjectWorkspaceId}" is not available on this project.`;
1137
+ }
1138
+ for (const workspace of projectWorkspaceRows) {
1139
+ let projectCwd = readNonEmptyString(workspace.cwd);
1140
+ let managedWorkspaceWarning: string | null = null;
1141
+ if (!projectCwd || projectCwd === REPO_ONLY_CWD_SENTINEL) {
1142
+ try {
1143
+ const managedWorkspace = await ensureManagedProjectWorkspace({
1144
+ companyId: agent.companyId,
1145
+ projectId: workspaceProjectId ?? resolvedProjectId ?? workspace.projectId,
1146
+ repoUrl: readNonEmptyString(workspace.repoUrl),
1147
+ });
1148
+ projectCwd = managedWorkspace.cwd;
1149
+ managedWorkspaceWarning = managedWorkspace.warning;
1150
+ } catch (error) {
1151
+ if (preferredWorkspace?.id === workspace.id) {
1152
+ preferredWorkspaceWarning = error instanceof Error ? error.message : String(error);
1153
+ }
1154
+ continue;
1155
+ }
1156
+ }
1157
+ hasConfiguredProjectCwd = true;
1158
+ const projectCwdExists = await fs
1159
+ .stat(projectCwd)
1160
+ .then((stats) => stats.isDirectory())
1161
+ .catch(() => false);
1162
+ if (projectCwdExists) {
1163
+ return {
1164
+ cwd: projectCwd,
1165
+ source: "project_primary" as const,
1166
+ projectId: resolvedProjectId,
1167
+ workspaceId: workspace.id,
1168
+ repoUrl: workspace.repoUrl,
1169
+ repoRef: workspace.repoRef,
1170
+ workspaceHints,
1171
+ warnings: [preferredWorkspaceWarning, managedWorkspaceWarning].filter(
1172
+ (value): value is string => Boolean(value),
1173
+ ),
1174
+ };
1175
+ }
1176
+ if (preferredWorkspace?.id === workspace.id) {
1177
+ preferredWorkspaceWarning =
1178
+ `Selected project workspace path "${projectCwd}" is not available yet.`;
1179
+ }
1180
+ missingProjectCwds.push(projectCwd);
1181
+ }
1182
+
1183
+ const fallbackCwd = resolveDefaultAgentWorkspaceDir(agent.id);
1184
+ await fs.mkdir(fallbackCwd, { recursive: true });
1185
+ const warnings: string[] = [];
1186
+ if (preferredWorkspaceWarning) {
1187
+ warnings.push(preferredWorkspaceWarning);
1188
+ }
1189
+ if (missingProjectCwds.length > 0) {
1190
+ const firstMissing = missingProjectCwds[0];
1191
+ const extraMissingCount = Math.max(0, missingProjectCwds.length - 1);
1192
+ warnings.push(
1193
+ extraMissingCount > 0
1194
+ ? `Project workspace path "${firstMissing}" and ${extraMissingCount} other configured path(s) are not available yet. Using fallback workspace "${fallbackCwd}" for this run.`
1195
+ : `Project workspace path "${firstMissing}" is not available yet. Using fallback workspace "${fallbackCwd}" for this run.`,
1196
+ );
1197
+ } else if (!hasConfiguredProjectCwd) {
1198
+ warnings.push(
1199
+ `Project workspace has no local cwd configured. Using fallback workspace "${fallbackCwd}" for this run.`,
1200
+ );
1201
+ }
1202
+ return {
1203
+ cwd: fallbackCwd,
1204
+ source: "project_primary" as const,
1205
+ projectId: resolvedProjectId,
1206
+ workspaceId: projectWorkspaceRows[0]?.id ?? null,
1207
+ repoUrl: projectWorkspaceRows[0]?.repoUrl ?? null,
1208
+ repoRef: projectWorkspaceRows[0]?.repoRef ?? null,
1209
+ workspaceHints,
1210
+ warnings,
1211
+ };
1212
+ }
1213
+
1214
+ if (workspaceProjectId) {
1215
+ const managedWorkspace = await ensureManagedProjectWorkspace({
1216
+ companyId: agent.companyId,
1217
+ projectId: workspaceProjectId,
1218
+ repoUrl: null,
1219
+ });
1220
+ return {
1221
+ cwd: managedWorkspace.cwd,
1222
+ source: "project_primary" as const,
1223
+ projectId: resolvedProjectId,
1224
+ workspaceId: null,
1225
+ repoUrl: null,
1226
+ repoRef: null,
1227
+ workspaceHints,
1228
+ warnings: managedWorkspace.warning ? [managedWorkspace.warning] : [],
1229
+ };
1230
+ }
1231
+
1232
+ const sessionCwd = readNonEmptyString(previousSessionParams?.cwd);
1233
+ if (sessionCwd) {
1234
+ const sessionCwdExists = await fs
1235
+ .stat(sessionCwd)
1236
+ .then((stats) => stats.isDirectory())
1237
+ .catch(() => false);
1238
+ if (sessionCwdExists) {
1239
+ return {
1240
+ cwd: sessionCwd,
1241
+ source: "task_session" as const,
1242
+ projectId: resolvedProjectId,
1243
+ workspaceId: readNonEmptyString(previousSessionParams?.workspaceId),
1244
+ repoUrl: readNonEmptyString(previousSessionParams?.repoUrl),
1245
+ repoRef: readNonEmptyString(previousSessionParams?.repoRef),
1246
+ workspaceHints,
1247
+ warnings: [],
1248
+ };
1249
+ }
1250
+ }
1251
+
1252
+ const cwd = resolveDefaultAgentWorkspaceDir(agent.id);
1253
+ await fs.mkdir(cwd, { recursive: true });
1254
+ const warnings: string[] = [];
1255
+ if (sessionCwd) {
1256
+ warnings.push(
1257
+ `Saved session workspace "${sessionCwd}" is not available. Using fallback workspace "${cwd}" for this run.`,
1258
+ );
1259
+ } else if (resolvedProjectId) {
1260
+ warnings.push(
1261
+ `No project workspace directory is currently available for this issue. Using fallback workspace "${cwd}" for this run.`,
1262
+ );
1263
+ } else {
1264
+ warnings.push(
1265
+ `No project or prior session workspace was available. Using fallback workspace "${cwd}" for this run.`,
1266
+ );
1267
+ }
1268
+ return {
1269
+ cwd,
1270
+ source: "agent_home" as const,
1271
+ projectId: resolvedProjectId,
1272
+ workspaceId: null,
1273
+ repoUrl: null,
1274
+ repoRef: null,
1275
+ workspaceHints,
1276
+ warnings,
1277
+ };
1278
+ }
1279
+
1280
+ async function upsertTaskSession(input: {
1281
+ companyId: string;
1282
+ agentId: string;
1283
+ adapterType: string;
1284
+ taskKey: string;
1285
+ sessionParamsJson: Record<string, unknown> | null;
1286
+ sessionDisplayId: string | null;
1287
+ lastRunId: string | null;
1288
+ lastError: string | null;
1289
+ }) {
1290
+ const existing = await getTaskSession(
1291
+ input.companyId,
1292
+ input.agentId,
1293
+ input.adapterType,
1294
+ input.taskKey,
1295
+ );
1296
+ if (existing) {
1297
+ return db
1298
+ .update(agentTaskSessions)
1299
+ .set({
1300
+ sessionParamsJson: input.sessionParamsJson,
1301
+ sessionDisplayId: input.sessionDisplayId,
1302
+ lastRunId: input.lastRunId,
1303
+ lastError: input.lastError,
1304
+ updatedAt: new Date(),
1305
+ })
1306
+ .where(eq(agentTaskSessions.id, existing.id))
1307
+ .returning()
1308
+ .then((rows) => rows[0] ?? null);
1309
+ }
1310
+
1311
+ return db
1312
+ .insert(agentTaskSessions)
1313
+ .values({
1314
+ companyId: input.companyId,
1315
+ agentId: input.agentId,
1316
+ adapterType: input.adapterType,
1317
+ taskKey: input.taskKey,
1318
+ sessionParamsJson: input.sessionParamsJson,
1319
+ sessionDisplayId: input.sessionDisplayId,
1320
+ lastRunId: input.lastRunId,
1321
+ lastError: input.lastError,
1322
+ })
1323
+ .returning()
1324
+ .then((rows) => rows[0] ?? null);
1325
+ }
1326
+
1327
+ async function clearTaskSessions(
1328
+ companyId: string,
1329
+ agentId: string,
1330
+ opts?: { taskKey?: string | null; adapterType?: string | null },
1331
+ ) {
1332
+ const conditions = [
1333
+ eq(agentTaskSessions.companyId, companyId),
1334
+ eq(agentTaskSessions.agentId, agentId),
1335
+ ];
1336
+ if (opts?.taskKey) {
1337
+ conditions.push(eq(agentTaskSessions.taskKey, opts.taskKey));
1338
+ }
1339
+ if (opts?.adapterType) {
1340
+ conditions.push(eq(agentTaskSessions.adapterType, opts.adapterType));
1341
+ }
1342
+
1343
+ return db
1344
+ .delete(agentTaskSessions)
1345
+ .where(and(...conditions))
1346
+ .returning()
1347
+ .then((rows) => rows.length);
1348
+ }
1349
+
1350
+ async function ensureRuntimeState(agent: typeof agents.$inferSelect) {
1351
+ const existing = await getRuntimeState(agent.id);
1352
+ if (existing) return existing;
1353
+
1354
+ return db
1355
+ .insert(agentRuntimeState)
1356
+ .values({
1357
+ agentId: agent.id,
1358
+ companyId: agent.companyId,
1359
+ adapterType: agent.adapterType,
1360
+ stateJson: {},
1361
+ })
1362
+ .returning()
1363
+ .then((rows) => rows[0]);
1364
+ }
1365
+
1366
+ async function setRunStatus(
1367
+ runId: string,
1368
+ status: string,
1369
+ patch?: Partial<typeof heartbeatRuns.$inferInsert>,
1370
+ ) {
1371
+ const updated = await db
1372
+ .update(heartbeatRuns)
1373
+ .set({ status, ...patch, updatedAt: new Date() })
1374
+ .where(eq(heartbeatRuns.id, runId))
1375
+ .returning()
1376
+ .then((rows) => rows[0] ?? null);
1377
+
1378
+ if (updated) {
1379
+ publishLiveEvent({
1380
+ companyId: updated.companyId,
1381
+ type: "heartbeat.run.status",
1382
+ payload: {
1383
+ runId: updated.id,
1384
+ agentId: updated.agentId,
1385
+ status: updated.status,
1386
+ invocationSource: updated.invocationSource,
1387
+ triggerDetail: updated.triggerDetail,
1388
+ error: updated.error ?? null,
1389
+ errorCode: updated.errorCode ?? null,
1390
+ startedAt: updated.startedAt ? new Date(updated.startedAt).toISOString() : null,
1391
+ finishedAt: updated.finishedAt ? new Date(updated.finishedAt).toISOString() : null,
1392
+ },
1393
+ });
1394
+ }
1395
+
1396
+ return updated;
1397
+ }
1398
+
1399
+ async function setWakeupStatus(
1400
+ wakeupRequestId: string | null | undefined,
1401
+ status: string,
1402
+ patch?: Partial<typeof agentWakeupRequests.$inferInsert>,
1403
+ ) {
1404
+ if (!wakeupRequestId) return;
1405
+ await db
1406
+ .update(agentWakeupRequests)
1407
+ .set({ status, ...patch, updatedAt: new Date() })
1408
+ .where(eq(agentWakeupRequests.id, wakeupRequestId));
1409
+ }
1410
+
1411
+ async function appendRunEvent(
1412
+ run: typeof heartbeatRuns.$inferSelect,
1413
+ seq: number,
1414
+ event: {
1415
+ eventType: string;
1416
+ stream?: "system" | "stdout" | "stderr";
1417
+ level?: "info" | "warn" | "error";
1418
+ color?: string;
1419
+ message?: string;
1420
+ payload?: Record<string, unknown>;
1421
+ },
1422
+ ) {
1423
+ const currentUserRedactionOptions = await getCurrentUserRedactionOptions();
1424
+ const sanitizedMessage = event.message
1425
+ ? redactCurrentUserText(event.message, currentUserRedactionOptions)
1426
+ : event.message;
1427
+ const sanitizedPayload = event.payload
1428
+ ? redactCurrentUserValue(event.payload, currentUserRedactionOptions)
1429
+ : event.payload;
1430
+
1431
+ await db.insert(heartbeatRunEvents).values({
1432
+ companyId: run.companyId,
1433
+ runId: run.id,
1434
+ agentId: run.agentId,
1435
+ seq,
1436
+ eventType: event.eventType,
1437
+ stream: event.stream,
1438
+ level: event.level,
1439
+ color: event.color,
1440
+ message: sanitizedMessage,
1441
+ payload: sanitizedPayload,
1442
+ });
1443
+
1444
+ publishLiveEvent({
1445
+ companyId: run.companyId,
1446
+ type: "heartbeat.run.event",
1447
+ payload: {
1448
+ runId: run.id,
1449
+ agentId: run.agentId,
1450
+ seq,
1451
+ eventType: event.eventType,
1452
+ stream: event.stream ?? null,
1453
+ level: event.level ?? null,
1454
+ color: event.color ?? null,
1455
+ message: sanitizedMessage ?? null,
1456
+ payload: sanitizedPayload ?? null,
1457
+ },
1458
+ });
1459
+ }
1460
+
1461
+ async function nextRunEventSeq(runId: string) {
1462
+ const [row] = await db
1463
+ .select({ maxSeq: sql<number | null>`max(${heartbeatRunEvents.seq})` })
1464
+ .from(heartbeatRunEvents)
1465
+ .where(eq(heartbeatRunEvents.runId, runId));
1466
+ return Number(row?.maxSeq ?? 0) + 1;
1467
+ }
1468
+
1469
+ async function persistRunProcessMetadata(
1470
+ runId: string,
1471
+ meta: { pid: number; startedAt: string },
1472
+ ) {
1473
+ const startedAt = new Date(meta.startedAt);
1474
+ return db
1475
+ .update(heartbeatRuns)
1476
+ .set({
1477
+ processPid: meta.pid,
1478
+ processStartedAt: Number.isNaN(startedAt.getTime()) ? new Date() : startedAt,
1479
+ updatedAt: new Date(),
1480
+ })
1481
+ .where(eq(heartbeatRuns.id, runId))
1482
+ .returning()
1483
+ .then((rows) => rows[0] ?? null);
1484
+ }
1485
+
1486
+ async function clearDetachedRunWarning(runId: string) {
1487
+ const updated = await db
1488
+ .update(heartbeatRuns)
1489
+ .set({
1490
+ error: null,
1491
+ errorCode: null,
1492
+ updatedAt: new Date(),
1493
+ })
1494
+ .where(and(eq(heartbeatRuns.id, runId), eq(heartbeatRuns.status, "running"), eq(heartbeatRuns.errorCode, DETACHED_PROCESS_ERROR_CODE)))
1495
+ .returning()
1496
+ .then((rows) => rows[0] ?? null);
1497
+ if (!updated) return null;
1498
+
1499
+ await appendRunEvent(updated, await nextRunEventSeq(updated.id), {
1500
+ eventType: "lifecycle",
1501
+ stream: "system",
1502
+ level: "info",
1503
+ message: "Detached child process reported activity; cleared detached warning",
1504
+ });
1505
+ return updated;
1506
+ }
1507
+
1508
+ async function enqueueProcessLossRetry(
1509
+ run: typeof heartbeatRuns.$inferSelect,
1510
+ agent: typeof agents.$inferSelect,
1511
+ now: Date,
1512
+ ) {
1513
+ const contextSnapshot = parseObject(run.contextSnapshot);
1514
+ const issueId = readNonEmptyString(contextSnapshot.issueId);
1515
+ const taskKey = deriveTaskKey(contextSnapshot, null);
1516
+ const sessionBefore = await resolveSessionBeforeForWakeup(agent, taskKey);
1517
+ const retryContextSnapshot = {
1518
+ ...contextSnapshot,
1519
+ retryOfRunId: run.id,
1520
+ wakeReason: "process_lost_retry",
1521
+ retryReason: "process_lost",
1522
+ };
1523
+
1524
+ const queued = await db.transaction(async (tx) => {
1525
+ const wakeupRequest = await tx
1526
+ .insert(agentWakeupRequests)
1527
+ .values({
1528
+ companyId: run.companyId,
1529
+ agentId: run.agentId,
1530
+ source: "automation",
1531
+ triggerDetail: "system",
1532
+ reason: "process_lost_retry",
1533
+ payload: {
1534
+ ...(issueId ? { issueId } : {}),
1535
+ retryOfRunId: run.id,
1536
+ },
1537
+ status: "queued",
1538
+ requestedByActorType: "system",
1539
+ requestedByActorId: null,
1540
+ updatedAt: now,
1541
+ })
1542
+ .returning()
1543
+ .then((rows) => rows[0]);
1544
+
1545
+ const retryRun = await tx
1546
+ .insert(heartbeatRuns)
1547
+ .values({
1548
+ companyId: run.companyId,
1549
+ agentId: run.agentId,
1550
+ invocationSource: "automation",
1551
+ triggerDetail: "system",
1552
+ status: "queued",
1553
+ wakeupRequestId: wakeupRequest.id,
1554
+ contextSnapshot: retryContextSnapshot,
1555
+ sessionIdBefore: sessionBefore,
1556
+ retryOfRunId: run.id,
1557
+ processLossRetryCount: (run.processLossRetryCount ?? 0) + 1,
1558
+ updatedAt: now,
1559
+ })
1560
+ .returning()
1561
+ .then((rows) => rows[0]);
1562
+
1563
+ await tx
1564
+ .update(agentWakeupRequests)
1565
+ .set({
1566
+ runId: retryRun.id,
1567
+ updatedAt: now,
1568
+ })
1569
+ .where(eq(agentWakeupRequests.id, wakeupRequest.id));
1570
+
1571
+ if (issueId) {
1572
+ await tx
1573
+ .update(issues)
1574
+ .set({
1575
+ executionRunId: retryRun.id,
1576
+ executionAgentNameKey: normalizeAgentNameKey(agent.name),
1577
+ executionLockedAt: now,
1578
+ updatedAt: now,
1579
+ })
1580
+ .where(and(eq(issues.id, issueId), eq(issues.companyId, run.companyId), eq(issues.executionRunId, run.id)));
1581
+ }
1582
+
1583
+ return retryRun;
1584
+ });
1585
+
1586
+ publishLiveEvent({
1587
+ companyId: queued.companyId,
1588
+ type: "heartbeat.run.queued",
1589
+ payload: {
1590
+ runId: queued.id,
1591
+ agentId: queued.agentId,
1592
+ invocationSource: queued.invocationSource,
1593
+ triggerDetail: queued.triggerDetail,
1594
+ wakeupRequestId: queued.wakeupRequestId,
1595
+ },
1596
+ });
1597
+
1598
+ await appendRunEvent(queued, 1, {
1599
+ eventType: "lifecycle",
1600
+ stream: "system",
1601
+ level: "warn",
1602
+ message: "Queued automatic retry after orphaned child process was confirmed dead",
1603
+ payload: {
1604
+ retryOfRunId: run.id,
1605
+ },
1606
+ });
1607
+
1608
+ return queued;
1609
+ }
1610
+
1611
+ function parseHeartbeatPolicy(agent: typeof agents.$inferSelect) {
1612
+ const runtimeConfig = parseObject(agent.runtimeConfig);
1613
+ const heartbeat = parseObject(runtimeConfig.heartbeat);
1614
+
1615
+ return {
1616
+ enabled: asBoolean(heartbeat.enabled, true),
1617
+ intervalSec: Math.max(0, asNumber(heartbeat.intervalSec, 0)),
1618
+ wakeOnDemand: asBoolean(heartbeat.wakeOnDemand ?? heartbeat.wakeOnAssignment ?? heartbeat.wakeOnOnDemand ?? heartbeat.wakeOnAutomation, true),
1619
+ maxConcurrentRuns: normalizeMaxConcurrentRuns(heartbeat.maxConcurrentRuns),
1620
+ };
1621
+ }
1622
+
1623
+ async function countRunningRunsForAgent(agentId: string) {
1624
+ const [{ count }] = await db
1625
+ .select({ count: sql<number>`count(*)` })
1626
+ .from(heartbeatRuns)
1627
+ .where(and(eq(heartbeatRuns.agentId, agentId), eq(heartbeatRuns.status, "running")));
1628
+ return Number(count ?? 0);
1629
+ }
1630
+
1631
+ async function claimQueuedRun(run: typeof heartbeatRuns.$inferSelect) {
1632
+ if (run.status !== "queued") return run;
1633
+ const agent = await getAgent(run.agentId);
1634
+ if (!agent) {
1635
+ await cancelRunInternal(run.id, "Cancelled because the agent no longer exists");
1636
+ return null;
1637
+ }
1638
+ if (agent.status === "paused" || agent.status === "terminated" || agent.status === "pending_approval") {
1639
+ await cancelRunInternal(run.id, "Cancelled because the agent is not invokable");
1640
+ return null;
1641
+ }
1642
+
1643
+ const context = parseObject(run.contextSnapshot);
1644
+ const budgetBlock = await budgets.getInvocationBlock(run.companyId, run.agentId, {
1645
+ issueId: readNonEmptyString(context.issueId),
1646
+ projectId: readNonEmptyString(context.projectId),
1647
+ });
1648
+ if (budgetBlock) {
1649
+ await cancelRunInternal(run.id, budgetBlock.reason);
1650
+ return null;
1651
+ }
1652
+
1653
+ const claimedAt = new Date();
1654
+ const claimed = await db
1655
+ .update(heartbeatRuns)
1656
+ .set({
1657
+ status: "running",
1658
+ startedAt: run.startedAt ?? claimedAt,
1659
+ updatedAt: claimedAt,
1660
+ })
1661
+ .where(and(eq(heartbeatRuns.id, run.id), eq(heartbeatRuns.status, "queued")))
1662
+ .returning()
1663
+ .then((rows) => rows[0] ?? null);
1664
+ if (!claimed) return null;
1665
+
1666
+ publishLiveEvent({
1667
+ companyId: claimed.companyId,
1668
+ type: "heartbeat.run.status",
1669
+ payload: {
1670
+ runId: claimed.id,
1671
+ agentId: claimed.agentId,
1672
+ status: claimed.status,
1673
+ invocationSource: claimed.invocationSource,
1674
+ triggerDetail: claimed.triggerDetail,
1675
+ error: claimed.error ?? null,
1676
+ errorCode: claimed.errorCode ?? null,
1677
+ startedAt: claimed.startedAt ? new Date(claimed.startedAt).toISOString() : null,
1678
+ finishedAt: claimed.finishedAt ? new Date(claimed.finishedAt).toISOString() : null,
1679
+ },
1680
+ });
1681
+
1682
+ await setWakeupStatus(claimed.wakeupRequestId, "claimed", { claimedAt });
1683
+ return claimed;
1684
+ }
1685
+
1686
+ async function finalizeAgentStatus(
1687
+ agentId: string,
1688
+ outcome: "succeeded" | "failed" | "cancelled" | "timed_out",
1689
+ ) {
1690
+ const existing = await getAgent(agentId);
1691
+ if (!existing) return;
1692
+
1693
+ if (existing.status === "paused" || existing.status === "terminated") {
1694
+ return;
1695
+ }
1696
+
1697
+ const runningCount = await countRunningRunsForAgent(agentId);
1698
+ const nextStatus =
1699
+ runningCount > 0
1700
+ ? "running"
1701
+ : outcome === "succeeded" || outcome === "cancelled"
1702
+ ? "idle"
1703
+ : "error";
1704
+
1705
+ const updated = await db
1706
+ .update(agents)
1707
+ .set({
1708
+ status: nextStatus,
1709
+ lastHeartbeatAt: new Date(),
1710
+ updatedAt: new Date(),
1711
+ })
1712
+ .where(eq(agents.id, agentId))
1713
+ .returning()
1714
+ .then((rows) => rows[0] ?? null);
1715
+
1716
+ if (updated) {
1717
+ publishLiveEvent({
1718
+ companyId: updated.companyId,
1719
+ type: "agent.status",
1720
+ payload: {
1721
+ agentId: updated.id,
1722
+ status: updated.status,
1723
+ lastHeartbeatAt: updated.lastHeartbeatAt
1724
+ ? new Date(updated.lastHeartbeatAt).toISOString()
1725
+ : null,
1726
+ outcome,
1727
+ },
1728
+ });
1729
+ }
1730
+ }
1731
+
1732
+ async function reapOrphanedRuns(opts?: { staleThresholdMs?: number }) {
1733
+ const staleThresholdMs = opts?.staleThresholdMs ?? 0;
1734
+ const now = new Date();
1735
+
1736
+ // Find all runs stuck in "running" state (queued runs are legitimately waiting; resumeQueuedRuns handles them)
1737
+ const activeRuns = await db
1738
+ .select({
1739
+ run: heartbeatRuns,
1740
+ adapterType: agents.adapterType,
1741
+ })
1742
+ .from(heartbeatRuns)
1743
+ .innerJoin(agents, eq(heartbeatRuns.agentId, agents.id))
1744
+ .where(eq(heartbeatRuns.status, "running"));
1745
+
1746
+ const reaped: string[] = [];
1747
+
1748
+ for (const { run, adapterType } of activeRuns) {
1749
+ if (runningProcesses.has(run.id) || activeRunExecutions.has(run.id)) continue;
1750
+
1751
+ // Apply staleness threshold to avoid false positives
1752
+ if (staleThresholdMs > 0) {
1753
+ const refTime = run.updatedAt ? new Date(run.updatedAt).getTime() : 0;
1754
+ if (now.getTime() - refTime < staleThresholdMs) continue;
1755
+ }
1756
+
1757
+ const tracksLocalChild = isTrackedLocalChildProcessAdapter(adapterType);
1758
+ if (tracksLocalChild && run.processPid && isProcessAlive(run.processPid)) {
1759
+ if (run.errorCode !== DETACHED_PROCESS_ERROR_CODE) {
1760
+ const detachedMessage = `Lost in-memory process handle, but child pid ${run.processPid} is still alive`;
1761
+ const detachedRun = await setRunStatus(run.id, "running", {
1762
+ error: detachedMessage,
1763
+ errorCode: DETACHED_PROCESS_ERROR_CODE,
1764
+ });
1765
+ if (detachedRun) {
1766
+ await appendRunEvent(detachedRun, await nextRunEventSeq(detachedRun.id), {
1767
+ eventType: "lifecycle",
1768
+ stream: "system",
1769
+ level: "warn",
1770
+ message: detachedMessage,
1771
+ payload: {
1772
+ processPid: run.processPid,
1773
+ },
1774
+ });
1775
+ }
1776
+ }
1777
+ continue;
1778
+ }
1779
+
1780
+ const shouldRetry = tracksLocalChild && !!run.processPid && (run.processLossRetryCount ?? 0) < 1;
1781
+ const baseMessage = run.processPid
1782
+ ? `Process lost -- child pid ${run.processPid} is no longer running`
1783
+ : "Process lost -- server may have restarted";
1784
+
1785
+ let finalizedRun = await setRunStatus(run.id, "failed", {
1786
+ error: shouldRetry ? `${baseMessage}; retrying once` : baseMessage,
1787
+ errorCode: "process_lost",
1788
+ finishedAt: now,
1789
+ });
1790
+ await setWakeupStatus(run.wakeupRequestId, "failed", {
1791
+ finishedAt: now,
1792
+ error: shouldRetry ? `${baseMessage}; retrying once` : baseMessage,
1793
+ });
1794
+ if (!finalizedRun) finalizedRun = await getRun(run.id);
1795
+ if (!finalizedRun) continue;
1796
+
1797
+ let retriedRun: typeof heartbeatRuns.$inferSelect | null = null;
1798
+ if (shouldRetry) {
1799
+ const agent = await getAgent(run.agentId);
1800
+ if (agent) {
1801
+ retriedRun = await enqueueProcessLossRetry(finalizedRun, agent, now);
1802
+ }
1803
+ } else {
1804
+ await releaseIssueExecutionAndPromote(finalizedRun);
1805
+ }
1806
+
1807
+ await appendRunEvent(finalizedRun, await nextRunEventSeq(finalizedRun.id), {
1808
+ eventType: "lifecycle",
1809
+ stream: "system",
1810
+ level: "error",
1811
+ message: shouldRetry
1812
+ ? `${baseMessage}; queued retry ${retriedRun?.id ?? ""}`.trim()
1813
+ : baseMessage,
1814
+ payload: {
1815
+ ...(run.processPid ? { processPid: run.processPid } : {}),
1816
+ ...(retriedRun ? { retryRunId: retriedRun.id } : {}),
1817
+ },
1818
+ });
1819
+
1820
+ await finalizeAgentStatus(run.agentId, "failed");
1821
+ await startNextQueuedRunForAgent(run.agentId);
1822
+ runningProcesses.delete(run.id);
1823
+ reaped.push(run.id);
1824
+ }
1825
+
1826
+ if (reaped.length > 0) {
1827
+ logger.warn({ reapedCount: reaped.length, runIds: reaped }, "reaped orphaned heartbeat runs");
1828
+ }
1829
+ return { reaped: reaped.length, runIds: reaped };
1830
+ }
1831
+
1832
+ async function resumeQueuedRuns() {
1833
+ const queuedRuns = await db
1834
+ .select({ agentId: heartbeatRuns.agentId })
1835
+ .from(heartbeatRuns)
1836
+ .where(eq(heartbeatRuns.status, "queued"));
1837
+
1838
+ const agentIds = [...new Set(queuedRuns.map((r) => r.agentId))];
1839
+ for (const agentId of agentIds) {
1840
+ await startNextQueuedRunForAgent(agentId);
1841
+ }
1842
+ }
1843
+
1844
+ async function updateRuntimeState(
1845
+ agent: typeof agents.$inferSelect,
1846
+ run: typeof heartbeatRuns.$inferSelect,
1847
+ result: AdapterExecutionResult,
1848
+ session: { legacySessionId: string | null },
1849
+ normalizedUsage?: UsageTotals | null,
1850
+ ) {
1851
+ await ensureRuntimeState(agent);
1852
+ const usage = normalizedUsage ?? normalizeUsageTotals(result.usage);
1853
+ const inputTokens = usage?.inputTokens ?? 0;
1854
+ const outputTokens = usage?.outputTokens ?? 0;
1855
+ const cachedInputTokens = usage?.cachedInputTokens ?? 0;
1856
+ const billingType = normalizeLedgerBillingType(result.billingType);
1857
+ const additionalCostCents = normalizeBilledCostCents(result.costUsd, billingType);
1858
+ const hasTokenUsage = inputTokens > 0 || outputTokens > 0 || cachedInputTokens > 0;
1859
+ const provider = result.provider ?? "unknown";
1860
+ const biller = resolveLedgerBiller(result);
1861
+ const ledgerScope = await resolveLedgerScopeForRun(db, agent.companyId, run);
1862
+
1863
+ await db
1864
+ .update(agentRuntimeState)
1865
+ .set({
1866
+ adapterType: agent.adapterType,
1867
+ sessionId: session.legacySessionId,
1868
+ lastRunId: run.id,
1869
+ lastRunStatus: run.status,
1870
+ lastError: result.errorMessage ?? null,
1871
+ totalInputTokens: sql`${agentRuntimeState.totalInputTokens} + ${inputTokens}`,
1872
+ totalOutputTokens: sql`${agentRuntimeState.totalOutputTokens} + ${outputTokens}`,
1873
+ totalCachedInputTokens: sql`${agentRuntimeState.totalCachedInputTokens} + ${cachedInputTokens}`,
1874
+ totalCostCents: sql`${agentRuntimeState.totalCostCents} + ${additionalCostCents}`,
1875
+ updatedAt: new Date(),
1876
+ })
1877
+ .where(eq(agentRuntimeState.agentId, agent.id));
1878
+
1879
+ if (additionalCostCents > 0 || hasTokenUsage) {
1880
+ const costs = costService(db, budgetHooks);
1881
+ await costs.createEvent(agent.companyId, {
1882
+ heartbeatRunId: run.id,
1883
+ agentId: agent.id,
1884
+ issueId: ledgerScope.issueId,
1885
+ projectId: ledgerScope.projectId,
1886
+ provider,
1887
+ biller,
1888
+ billingType,
1889
+ model: result.model ?? "unknown",
1890
+ inputTokens,
1891
+ cachedInputTokens,
1892
+ outputTokens,
1893
+ costCents: additionalCostCents,
1894
+ occurredAt: new Date(),
1895
+ });
1896
+ }
1897
+ }
1898
+
1899
+ async function startNextQueuedRunForAgent(agentId: string) {
1900
+ return withAgentStartLock(agentId, async () => {
1901
+ const agent = await getAgent(agentId);
1902
+ if (!agent) return [];
1903
+ if (agent.status === "paused" || agent.status === "terminated" || agent.status === "pending_approval") {
1904
+ return [];
1905
+ }
1906
+ const policy = parseHeartbeatPolicy(agent);
1907
+ const runningCount = await countRunningRunsForAgent(agentId);
1908
+ const availableSlots = Math.max(0, policy.maxConcurrentRuns - runningCount);
1909
+ if (availableSlots <= 0) return [];
1910
+
1911
+ const queuedRuns = await db
1912
+ .select()
1913
+ .from(heartbeatRuns)
1914
+ .where(and(eq(heartbeatRuns.agentId, agentId), eq(heartbeatRuns.status, "queued")))
1915
+ .orderBy(asc(heartbeatRuns.createdAt))
1916
+ .limit(availableSlots);
1917
+ if (queuedRuns.length === 0) return [];
1918
+
1919
+ const claimedRuns: Array<typeof heartbeatRuns.$inferSelect> = [];
1920
+ for (const queuedRun of queuedRuns) {
1921
+ const claimed = await claimQueuedRun(queuedRun);
1922
+ if (claimed) claimedRuns.push(claimed);
1923
+ }
1924
+ if (claimedRuns.length === 0) return [];
1925
+
1926
+ for (const claimedRun of claimedRuns) {
1927
+ void executeRun(claimedRun.id).catch((err) => {
1928
+ logger.error({ err, runId: claimedRun.id }, "queued heartbeat execution failed");
1929
+ });
1930
+ }
1931
+ return claimedRuns;
1932
+ });
1933
+ }
1934
+
1935
+ async function executeRun(runId: string) {
1936
+ let run = await getRun(runId);
1937
+ if (!run) return;
1938
+ if (run.status !== "queued" && run.status !== "running") return;
1939
+
1940
+ if (run.status === "queued") {
1941
+ const claimed = await claimQueuedRun(run);
1942
+ if (!claimed) {
1943
+ // Another worker has already claimed or finalized this run.
1944
+ return;
1945
+ }
1946
+ run = claimed;
1947
+ }
1948
+
1949
+ activeRunExecutions.add(run.id);
1950
+
1951
+ try {
1952
+ const agent = await getAgent(run.agentId);
1953
+ if (!agent) {
1954
+ await setRunStatus(runId, "failed", {
1955
+ error: "Agent not found",
1956
+ errorCode: "agent_not_found",
1957
+ finishedAt: new Date(),
1958
+ });
1959
+ await setWakeupStatus(run.wakeupRequestId, "failed", {
1960
+ finishedAt: new Date(),
1961
+ error: "Agent not found",
1962
+ });
1963
+ const failedRun = await getRun(runId);
1964
+ if (failedRun) await releaseIssueExecutionAndPromote(failedRun);
1965
+ return;
1966
+ }
1967
+
1968
+ const runtime = await ensureRuntimeState(agent);
1969
+ const context = parseObject(run.contextSnapshot);
1970
+ const taskKey = deriveTaskKey(context, null);
1971
+ const sessionCodec = getAdapterSessionCodec(agent.adapterType);
1972
+ const issueId = readNonEmptyString(context.issueId);
1973
+ const issueContext = issueId
1974
+ ? await db
1975
+ .select({
1976
+ id: issues.id,
1977
+ identifier: issues.identifier,
1978
+ title: issues.title,
1979
+ projectId: issues.projectId,
1980
+ projectWorkspaceId: issues.projectWorkspaceId,
1981
+ executionWorkspaceId: issues.executionWorkspaceId,
1982
+ executionWorkspacePreference: issues.executionWorkspacePreference,
1983
+ assigneeAgentId: issues.assigneeAgentId,
1984
+ assigneeAdapterOverrides: issues.assigneeAdapterOverrides,
1985
+ executionWorkspaceSettings: issues.executionWorkspaceSettings,
1986
+ })
1987
+ .from(issues)
1988
+ .where(and(eq(issues.id, issueId), eq(issues.companyId, agent.companyId)))
1989
+ .then((rows) => rows[0] ?? null)
1990
+ : null;
1991
+ const issueAssigneeOverrides =
1992
+ issueContext && issueContext.assigneeAgentId === agent.id
1993
+ ? parseIssueAssigneeAdapterOverrides(
1994
+ issueContext.assigneeAdapterOverrides,
1995
+ )
1996
+ : null;
1997
+ const isolatedWorkspacesEnabled = (await instanceSettings.getExperimental()).enableIsolatedWorkspaces;
1998
+ const issueExecutionWorkspaceSettings = isolatedWorkspacesEnabled
1999
+ ? parseIssueExecutionWorkspaceSettings(issueContext?.executionWorkspaceSettings)
2000
+ : null;
2001
+ const contextProjectId = readNonEmptyString(context.projectId);
2002
+ const executionProjectId = issueContext?.projectId ?? contextProjectId;
2003
+ const projectExecutionWorkspacePolicy = executionProjectId
2004
+ ? await db
2005
+ .select({ executionWorkspacePolicy: projects.executionWorkspacePolicy })
2006
+ .from(projects)
2007
+ .where(and(eq(projects.id, executionProjectId), eq(projects.companyId, agent.companyId)))
2008
+ .then((rows) =>
2009
+ gateProjectExecutionWorkspacePolicy(
2010
+ parseProjectExecutionWorkspacePolicy(rows[0]?.executionWorkspacePolicy),
2011
+ isolatedWorkspacesEnabled,
2012
+ ))
2013
+ : null;
2014
+ const taskSession = taskKey
2015
+ ? await getTaskSession(agent.companyId, agent.id, agent.adapterType, taskKey)
2016
+ : null;
2017
+ const resetTaskSession = shouldResetTaskSessionForWake(context);
2018
+ const sessionResetReason = describeSessionResetReason(context);
2019
+ const taskSessionForRun = resetTaskSession ? null : taskSession;
2020
+ const explicitResumeSessionParams = normalizeSessionParams(
2021
+ sessionCodec.deserialize(parseObject(context.resumeSessionParams)),
2022
+ );
2023
+ const explicitResumeSessionDisplayId = truncateDisplayId(
2024
+ readNonEmptyString(context.resumeSessionDisplayId) ??
2025
+ (sessionCodec.getDisplayId ? sessionCodec.getDisplayId(explicitResumeSessionParams) : null) ??
2026
+ readNonEmptyString(explicitResumeSessionParams?.sessionId),
2027
+ );
2028
+ const previousSessionParams =
2029
+ explicitResumeSessionParams ??
2030
+ (explicitResumeSessionDisplayId ? { sessionId: explicitResumeSessionDisplayId } : null) ??
2031
+ normalizeSessionParams(sessionCodec.deserialize(taskSessionForRun?.sessionParamsJson ?? null));
2032
+ const config = parseObject(agent.adapterConfig);
2033
+ const executionWorkspaceMode = resolveExecutionWorkspaceMode({
2034
+ projectPolicy: projectExecutionWorkspacePolicy,
2035
+ issueSettings: issueExecutionWorkspaceSettings,
2036
+ legacyUseProjectWorkspace: issueAssigneeOverrides?.useProjectWorkspace ?? null,
2037
+ });
2038
+ const resolvedWorkspace = await resolveWorkspaceForRun(
2039
+ agent,
2040
+ context,
2041
+ previousSessionParams,
2042
+ { useProjectWorkspace: executionWorkspaceMode !== "agent_default" },
2043
+ );
2044
+ const workspaceManagedConfig = buildExecutionWorkspaceAdapterConfig({
2045
+ agentConfig: config,
2046
+ projectPolicy: projectExecutionWorkspacePolicy,
2047
+ issueSettings: issueExecutionWorkspaceSettings,
2048
+ mode: executionWorkspaceMode,
2049
+ legacyUseProjectWorkspace: issueAssigneeOverrides?.useProjectWorkspace ?? null,
2050
+ });
2051
+ const mergedConfig = issueAssigneeOverrides?.adapterConfig
2052
+ ? { ...workspaceManagedConfig, ...issueAssigneeOverrides.adapterConfig }
2053
+ : workspaceManagedConfig;
2054
+ const { config: resolvedConfig, secretKeys } = await secretsSvc.resolveAdapterConfigForRuntime(
2055
+ agent.companyId,
2056
+ mergedConfig,
2057
+ );
2058
+ const runtimeSkillEntries = await companySkills.listRuntimeSkillEntries(agent.companyId);
2059
+ const runtimeConfig = {
2060
+ ...resolvedConfig,
2061
+ paperclipRuntimeSkills: runtimeSkillEntries,
2062
+ };
2063
+ const issueRef = issueContext
2064
+ ? {
2065
+ id: issueContext.id,
2066
+ identifier: issueContext.identifier,
2067
+ title: issueContext.title,
2068
+ projectId: issueContext.projectId,
2069
+ projectWorkspaceId: issueContext.projectWorkspaceId,
2070
+ executionWorkspaceId: issueContext.executionWorkspaceId,
2071
+ executionWorkspacePreference: issueContext.executionWorkspacePreference,
2072
+ }
2073
+ : null;
2074
+ const existingExecutionWorkspace =
2075
+ issueRef?.executionWorkspaceId ? await executionWorkspacesSvc.getById(issueRef.executionWorkspaceId) : null;
2076
+ const workspaceOperationRecorder = workspaceOperationsSvc.createRecorder({
2077
+ companyId: agent.companyId,
2078
+ heartbeatRunId: run.id,
2079
+ executionWorkspaceId: existingExecutionWorkspace?.id ?? null,
2080
+ });
2081
+ const executionWorkspace = await realizeExecutionWorkspace({
2082
+ base: {
2083
+ baseCwd: resolvedWorkspace.cwd,
2084
+ source: resolvedWorkspace.source,
2085
+ projectId: resolvedWorkspace.projectId,
2086
+ workspaceId: resolvedWorkspace.workspaceId,
2087
+ repoUrl: resolvedWorkspace.repoUrl,
2088
+ repoRef: resolvedWorkspace.repoRef,
2089
+ },
2090
+ config: runtimeConfig,
2091
+ issue: issueRef,
2092
+ agent: {
2093
+ id: agent.id,
2094
+ name: agent.name,
2095
+ companyId: agent.companyId,
2096
+ },
2097
+ recorder: workspaceOperationRecorder,
2098
+ });
2099
+ const resolvedProjectId = executionWorkspace.projectId ?? issueRef?.projectId ?? executionProjectId ?? null;
2100
+ const resolvedProjectWorkspaceId = issueRef?.projectWorkspaceId ?? resolvedWorkspace.workspaceId ?? null;
2101
+ const shouldReuseExisting =
2102
+ issueRef?.executionWorkspacePreference === "reuse_existing" &&
2103
+ existingExecutionWorkspace &&
2104
+ existingExecutionWorkspace.status !== "archived";
2105
+ let persistedExecutionWorkspace = null;
2106
+ try {
2107
+ persistedExecutionWorkspace = shouldReuseExisting && existingExecutionWorkspace
2108
+ ? await executionWorkspacesSvc.update(existingExecutionWorkspace.id, {
2109
+ cwd: executionWorkspace.cwd,
2110
+ repoUrl: executionWorkspace.repoUrl,
2111
+ baseRef: executionWorkspace.repoRef,
2112
+ branchName: executionWorkspace.branchName,
2113
+ providerType: executionWorkspace.strategy === "git_worktree" ? "git_worktree" : "local_fs",
2114
+ providerRef: executionWorkspace.worktreePath,
2115
+ status: "active",
2116
+ lastUsedAt: new Date(),
2117
+ metadata: {
2118
+ ...(existingExecutionWorkspace.metadata ?? {}),
2119
+ source: executionWorkspace.source,
2120
+ createdByRuntime: executionWorkspace.created,
2121
+ },
2122
+ })
2123
+ : resolvedProjectId
2124
+ ? await executionWorkspacesSvc.create({
2125
+ companyId: agent.companyId,
2126
+ projectId: resolvedProjectId,
2127
+ projectWorkspaceId: resolvedProjectWorkspaceId,
2128
+ sourceIssueId: issueRef?.id ?? null,
2129
+ mode:
2130
+ executionWorkspaceMode === "isolated_workspace"
2131
+ ? "isolated_workspace"
2132
+ : executionWorkspaceMode === "operator_branch"
2133
+ ? "operator_branch"
2134
+ : executionWorkspaceMode === "agent_default"
2135
+ ? "adapter_managed"
2136
+ : "shared_workspace",
2137
+ strategyType: executionWorkspace.strategy === "git_worktree" ? "git_worktree" : "project_primary",
2138
+ name: executionWorkspace.branchName ?? issueRef?.identifier ?? `workspace-${agent.id.slice(0, 8)}`,
2139
+ status: "active",
2140
+ cwd: executionWorkspace.cwd,
2141
+ repoUrl: executionWorkspace.repoUrl,
2142
+ baseRef: executionWorkspace.repoRef,
2143
+ branchName: executionWorkspace.branchName,
2144
+ providerType: executionWorkspace.strategy === "git_worktree" ? "git_worktree" : "local_fs",
2145
+ providerRef: executionWorkspace.worktreePath,
2146
+ lastUsedAt: new Date(),
2147
+ openedAt: new Date(),
2148
+ metadata: {
2149
+ source: executionWorkspace.source,
2150
+ createdByRuntime: executionWorkspace.created,
2151
+ },
2152
+ })
2153
+ : null;
2154
+ } catch (error) {
2155
+ if (executionWorkspace.created) {
2156
+ try {
2157
+ await cleanupExecutionWorkspaceArtifacts({
2158
+ workspace: {
2159
+ id: existingExecutionWorkspace?.id ?? `transient-${run.id}`,
2160
+ cwd: executionWorkspace.cwd,
2161
+ providerType: executionWorkspace.strategy === "git_worktree" ? "git_worktree" : "local_fs",
2162
+ providerRef: executionWorkspace.worktreePath,
2163
+ branchName: executionWorkspace.branchName,
2164
+ repoUrl: executionWorkspace.repoUrl,
2165
+ baseRef: executionWorkspace.repoRef,
2166
+ projectId: resolvedProjectId,
2167
+ projectWorkspaceId: resolvedProjectWorkspaceId,
2168
+ sourceIssueId: issueRef?.id ?? null,
2169
+ metadata: {
2170
+ createdByRuntime: true,
2171
+ source: executionWorkspace.source,
2172
+ },
2173
+ },
2174
+ projectWorkspace: {
2175
+ cwd: resolvedWorkspace.cwd,
2176
+ cleanupCommand: null,
2177
+ },
2178
+ teardownCommand: projectExecutionWorkspacePolicy?.workspaceStrategy?.teardownCommand ?? null,
2179
+ recorder: workspaceOperationRecorder,
2180
+ });
2181
+ } catch (cleanupError) {
2182
+ logger.warn(
2183
+ {
2184
+ runId: run.id,
2185
+ issueId,
2186
+ executionWorkspaceCwd: executionWorkspace.cwd,
2187
+ cleanupError: cleanupError instanceof Error ? cleanupError.message : String(cleanupError),
2188
+ },
2189
+ "Failed to cleanup realized execution workspace after persistence failure",
2190
+ );
2191
+ }
2192
+ }
2193
+ throw error;
2194
+ }
2195
+ await workspaceOperationRecorder.attachExecutionWorkspaceId(persistedExecutionWorkspace?.id ?? null);
2196
+ if (
2197
+ existingExecutionWorkspace &&
2198
+ persistedExecutionWorkspace &&
2199
+ existingExecutionWorkspace.id !== persistedExecutionWorkspace.id &&
2200
+ existingExecutionWorkspace.status === "active"
2201
+ ) {
2202
+ await executionWorkspacesSvc.update(existingExecutionWorkspace.id, {
2203
+ status: "idle",
2204
+ cleanupReason: null,
2205
+ });
2206
+ }
2207
+ if (issueId && persistedExecutionWorkspace) {
2208
+ const nextIssueWorkspaceMode = issueExecutionWorkspaceModeForPersistedWorkspace(persistedExecutionWorkspace.mode);
2209
+ const shouldSwitchIssueToExistingWorkspace =
2210
+ issueRef?.executionWorkspacePreference === "reuse_existing" ||
2211
+ executionWorkspaceMode === "isolated_workspace" ||
2212
+ executionWorkspaceMode === "operator_branch";
2213
+ const nextIssuePatch: Record<string, unknown> = {};
2214
+ if (issueRef?.executionWorkspaceId !== persistedExecutionWorkspace.id) {
2215
+ nextIssuePatch.executionWorkspaceId = persistedExecutionWorkspace.id;
2216
+ }
2217
+ if (resolvedProjectWorkspaceId && issueRef?.projectWorkspaceId !== resolvedProjectWorkspaceId) {
2218
+ nextIssuePatch.projectWorkspaceId = resolvedProjectWorkspaceId;
2219
+ }
2220
+ if (shouldSwitchIssueToExistingWorkspace) {
2221
+ nextIssuePatch.executionWorkspacePreference = "reuse_existing";
2222
+ nextIssuePatch.executionWorkspaceSettings = {
2223
+ ...(issueExecutionWorkspaceSettings ?? {}),
2224
+ mode: nextIssueWorkspaceMode,
2225
+ };
2226
+ }
2227
+ if (Object.keys(nextIssuePatch).length > 0) {
2228
+ await issuesSvc.update(issueId, nextIssuePatch);
2229
+ }
2230
+ }
2231
+ if (persistedExecutionWorkspace) {
2232
+ context.executionWorkspaceId = persistedExecutionWorkspace.id;
2233
+ await db
2234
+ .update(heartbeatRuns)
2235
+ .set({
2236
+ contextSnapshot: context,
2237
+ updatedAt: new Date(),
2238
+ })
2239
+ .where(eq(heartbeatRuns.id, run.id));
2240
+ }
2241
+ const runtimeSessionResolution = resolveRuntimeSessionParamsForWorkspace({
2242
+ agentId: agent.id,
2243
+ previousSessionParams,
2244
+ resolvedWorkspace: {
2245
+ ...resolvedWorkspace,
2246
+ cwd: executionWorkspace.cwd,
2247
+ },
2248
+ });
2249
+ const runtimeSessionParams = runtimeSessionResolution.sessionParams;
2250
+ const runtimeWorkspaceWarnings = [
2251
+ ...resolvedWorkspace.warnings,
2252
+ ...executionWorkspace.warnings,
2253
+ ...(runtimeSessionResolution.warning ? [runtimeSessionResolution.warning] : []),
2254
+ ...(resetTaskSession && sessionResetReason
2255
+ ? [
2256
+ taskKey
2257
+ ? `Skipping saved session resume for task "${taskKey}" because ${sessionResetReason}.`
2258
+ : `Skipping saved session resume because ${sessionResetReason}.`,
2259
+ ]
2260
+ : []),
2261
+ ];
2262
+ context.paperclipWorkspace = {
2263
+ cwd: executionWorkspace.cwd,
2264
+ source: executionWorkspace.source,
2265
+ mode: executionWorkspaceMode,
2266
+ strategy: executionWorkspace.strategy,
2267
+ projectId: executionWorkspace.projectId,
2268
+ workspaceId: executionWorkspace.workspaceId,
2269
+ repoUrl: executionWorkspace.repoUrl,
2270
+ repoRef: executionWorkspace.repoRef,
2271
+ branchName: executionWorkspace.branchName,
2272
+ worktreePath: executionWorkspace.worktreePath,
2273
+ agentHome: await (async () => {
2274
+ const home = resolveDefaultAgentWorkspaceDir(agent.id);
2275
+ await fs.mkdir(home, { recursive: true });
2276
+ return home;
2277
+ })(),
2278
+ };
2279
+ context.paperclipWorkspaces = resolvedWorkspace.workspaceHints;
2280
+ const runtimeServiceIntents = (() => {
2281
+ const runtimeConfig = parseObject(resolvedConfig.workspaceRuntime);
2282
+ return Array.isArray(runtimeConfig.services)
2283
+ ? runtimeConfig.services.filter(
2284
+ (value): value is Record<string, unknown> => typeof value === "object" && value !== null,
2285
+ )
2286
+ : [];
2287
+ })();
2288
+ if (runtimeServiceIntents.length > 0) {
2289
+ context.paperclipRuntimeServiceIntents = runtimeServiceIntents;
2290
+ } else {
2291
+ delete context.paperclipRuntimeServiceIntents;
2292
+ }
2293
+ if (executionWorkspace.projectId && !readNonEmptyString(context.projectId)) {
2294
+ context.projectId = executionWorkspace.projectId;
2295
+ }
2296
+ const runtimeSessionFallback = taskKey || resetTaskSession ? null : runtime.sessionId;
2297
+ let previousSessionDisplayId = truncateDisplayId(
2298
+ explicitResumeSessionDisplayId ??
2299
+ taskSessionForRun?.sessionDisplayId ??
2300
+ (sessionCodec.getDisplayId ? sessionCodec.getDisplayId(runtimeSessionParams) : null) ??
2301
+ readNonEmptyString(runtimeSessionParams?.sessionId) ??
2302
+ runtimeSessionFallback,
2303
+ );
2304
+ let runtimeSessionIdForAdapter =
2305
+ readNonEmptyString(runtimeSessionParams?.sessionId) ?? runtimeSessionFallback;
2306
+ let runtimeSessionParamsForAdapter = runtimeSessionParams;
2307
+
2308
+ const sessionCompaction = await evaluateSessionCompaction({
2309
+ agent,
2310
+ sessionId: previousSessionDisplayId ?? runtimeSessionIdForAdapter,
2311
+ issueId,
2312
+ });
2313
+ if (sessionCompaction.rotate) {
2314
+ context.paperclipSessionHandoffMarkdown = sessionCompaction.handoffMarkdown;
2315
+ context.paperclipSessionRotationReason = sessionCompaction.reason;
2316
+ context.paperclipPreviousSessionId = previousSessionDisplayId ?? runtimeSessionIdForAdapter;
2317
+ runtimeSessionIdForAdapter = null;
2318
+ runtimeSessionParamsForAdapter = null;
2319
+ previousSessionDisplayId = null;
2320
+ if (sessionCompaction.reason) {
2321
+ runtimeWorkspaceWarnings.push(
2322
+ `Starting a fresh session because ${sessionCompaction.reason}.`,
2323
+ );
2324
+ }
2325
+ } else {
2326
+ delete context.paperclipSessionHandoffMarkdown;
2327
+ delete context.paperclipSessionRotationReason;
2328
+ delete context.paperclipPreviousSessionId;
2329
+ }
2330
+
2331
+ const runtimeForAdapter = {
2332
+ sessionId: runtimeSessionIdForAdapter,
2333
+ sessionParams: runtimeSessionParamsForAdapter,
2334
+ sessionDisplayId: previousSessionDisplayId,
2335
+ taskKey,
2336
+ };
2337
+
2338
+ let seq = 1;
2339
+ let handle: RunLogHandle | null = null;
2340
+ let stdoutExcerpt = "";
2341
+ let stderrExcerpt = "";
2342
+ try {
2343
+ const startedAt = run.startedAt ?? new Date();
2344
+ const runningWithSession = await db
2345
+ .update(heartbeatRuns)
2346
+ .set({
2347
+ startedAt,
2348
+ sessionIdBefore: runtimeForAdapter.sessionDisplayId ?? runtimeForAdapter.sessionId,
2349
+ contextSnapshot: context,
2350
+ updatedAt: new Date(),
2351
+ })
2352
+ .where(eq(heartbeatRuns.id, run.id))
2353
+ .returning()
2354
+ .then((rows) => rows[0] ?? null);
2355
+ if (runningWithSession) run = runningWithSession;
2356
+
2357
+ const runningAgent = await db
2358
+ .update(agents)
2359
+ .set({ status: "running", updatedAt: new Date() })
2360
+ .where(eq(agents.id, agent.id))
2361
+ .returning()
2362
+ .then((rows) => rows[0] ?? null);
2363
+
2364
+ if (runningAgent) {
2365
+ publishLiveEvent({
2366
+ companyId: runningAgent.companyId,
2367
+ type: "agent.status",
2368
+ payload: {
2369
+ agentId: runningAgent.id,
2370
+ status: runningAgent.status,
2371
+ outcome: "running",
2372
+ },
2373
+ });
2374
+ }
2375
+
2376
+ const currentRun = run;
2377
+ await appendRunEvent(currentRun, seq++, {
2378
+ eventType: "lifecycle",
2379
+ stream: "system",
2380
+ level: "info",
2381
+ message: "run started",
2382
+ });
2383
+
2384
+ handle = await runLogStore.begin({
2385
+ companyId: run.companyId,
2386
+ agentId: run.agentId,
2387
+ runId,
2388
+ });
2389
+
2390
+ await db
2391
+ .update(heartbeatRuns)
2392
+ .set({
2393
+ logStore: handle.store,
2394
+ logRef: handle.logRef,
2395
+ updatedAt: new Date(),
2396
+ })
2397
+ .where(eq(heartbeatRuns.id, runId));
2398
+
2399
+ const currentUserRedactionOptions = await getCurrentUserRedactionOptions();
2400
+ const onLog = async (stream: "stdout" | "stderr", chunk: string) => {
2401
+ const sanitizedChunk = redactCurrentUserText(chunk, currentUserRedactionOptions);
2402
+ if (stream === "stdout") stdoutExcerpt = appendExcerpt(stdoutExcerpt, sanitizedChunk);
2403
+ if (stream === "stderr") stderrExcerpt = appendExcerpt(stderrExcerpt, sanitizedChunk);
2404
+ const ts = new Date().toISOString();
2405
+
2406
+ if (handle) {
2407
+ await runLogStore.append(handle, {
2408
+ stream,
2409
+ chunk: sanitizedChunk,
2410
+ ts,
2411
+ });
2412
+ }
2413
+
2414
+ const payloadChunk =
2415
+ sanitizedChunk.length > MAX_LIVE_LOG_CHUNK_BYTES
2416
+ ? sanitizedChunk.slice(sanitizedChunk.length - MAX_LIVE_LOG_CHUNK_BYTES)
2417
+ : sanitizedChunk;
2418
+
2419
+ publishLiveEvent({
2420
+ companyId: run.companyId,
2421
+ type: "heartbeat.run.log",
2422
+ payload: {
2423
+ runId: run.id,
2424
+ agentId: run.agentId,
2425
+ ts,
2426
+ stream,
2427
+ chunk: payloadChunk,
2428
+ truncated: payloadChunk.length !== sanitizedChunk.length,
2429
+ },
2430
+ });
2431
+ };
2432
+ for (const warning of runtimeWorkspaceWarnings) {
2433
+ const logEntry = formatRuntimeWorkspaceWarningLog(warning);
2434
+ await onLog(logEntry.stream, logEntry.chunk);
2435
+ }
2436
+ const adapterEnv = Object.fromEntries(
2437
+ Object.entries(parseObject(resolvedConfig.env)).filter(
2438
+ (entry): entry is [string, string] => typeof entry[0] === "string" && typeof entry[1] === "string",
2439
+ ),
2440
+ );
2441
+ const runtimeServices = await ensureRuntimeServicesForRun({
2442
+ db,
2443
+ runId: run.id,
2444
+ agent: {
2445
+ id: agent.id,
2446
+ name: agent.name,
2447
+ companyId: agent.companyId,
2448
+ },
2449
+ issue: issueRef,
2450
+ workspace: executionWorkspace,
2451
+ executionWorkspaceId: persistedExecutionWorkspace?.id ?? issueRef?.executionWorkspaceId ?? null,
2452
+ config: resolvedConfig,
2453
+ adapterEnv,
2454
+ onLog,
2455
+ });
2456
+ if (runtimeServices.length > 0) {
2457
+ context.paperclipRuntimeServices = runtimeServices;
2458
+ context.paperclipRuntimePrimaryUrl =
2459
+ runtimeServices.find((service) => readNonEmptyString(service.url))?.url ?? null;
2460
+ await db
2461
+ .update(heartbeatRuns)
2462
+ .set({
2463
+ contextSnapshot: context,
2464
+ updatedAt: new Date(),
2465
+ })
2466
+ .where(eq(heartbeatRuns.id, run.id));
2467
+ }
2468
+ if (issueId && (executionWorkspace.created || runtimeServices.some((service) => !service.reused))) {
2469
+ try {
2470
+ await issuesSvc.addComment(
2471
+ issueId,
2472
+ buildWorkspaceReadyComment({
2473
+ workspace: executionWorkspace,
2474
+ runtimeServices,
2475
+ }),
2476
+ { agentId: agent.id },
2477
+ );
2478
+ } catch (err) {
2479
+ await onLog(
2480
+ "stderr",
2481
+ `[paperclip] Failed to post workspace-ready comment: ${err instanceof Error ? err.message : String(err)}\n`,
2482
+ );
2483
+ }
2484
+ }
2485
+ const onAdapterMeta = async (meta: AdapterInvocationMeta) => {
2486
+ if (meta.env && secretKeys.size > 0) {
2487
+ for (const key of secretKeys) {
2488
+ if (key in meta.env) meta.env[key] = "***REDACTED***";
2489
+ }
2490
+ }
2491
+ await appendRunEvent(currentRun, seq++, {
2492
+ eventType: "adapter.invoke",
2493
+ stream: "system",
2494
+ level: "info",
2495
+ message: "adapter invocation",
2496
+ payload: meta as unknown as Record<string, unknown>,
2497
+ });
2498
+ };
2499
+
2500
+ const adapter = getServerAdapter(agent.adapterType);
2501
+ const authToken = adapter.supportsLocalAgentJwt
2502
+ ? createLocalAgentJwt(agent.id, agent.companyId, agent.adapterType, run.id)
2503
+ : null;
2504
+ if (adapter.supportsLocalAgentJwt && !authToken) {
2505
+ logger.warn(
2506
+ {
2507
+ companyId: agent.companyId,
2508
+ agentId: agent.id,
2509
+ runId: run.id,
2510
+ adapterType: agent.adapterType,
2511
+ },
2512
+ "local agent jwt secret missing or invalid; running without injected PAPERCLIP_API_KEY",
2513
+ );
2514
+ }
2515
+ const adapterResult = await adapter.execute({
2516
+ runId: run.id,
2517
+ agent,
2518
+ runtime: runtimeForAdapter,
2519
+ config: runtimeConfig,
2520
+ context,
2521
+ onLog,
2522
+ onMeta: onAdapterMeta,
2523
+ onSpawn: async (meta) => {
2524
+ await persistRunProcessMetadata(run.id, meta);
2525
+ },
2526
+ authToken: authToken ?? undefined,
2527
+ });
2528
+ const adapterManagedRuntimeServices = adapterResult.runtimeServices
2529
+ ? await persistAdapterManagedRuntimeServices({
2530
+ db,
2531
+ adapterType: agent.adapterType,
2532
+ runId: run.id,
2533
+ agent: {
2534
+ id: agent.id,
2535
+ name: agent.name,
2536
+ companyId: agent.companyId,
2537
+ },
2538
+ issue: issueRef,
2539
+ workspace: executionWorkspace,
2540
+ reports: adapterResult.runtimeServices,
2541
+ })
2542
+ : [];
2543
+ if (adapterManagedRuntimeServices.length > 0) {
2544
+ const combinedRuntimeServices = [
2545
+ ...runtimeServices,
2546
+ ...adapterManagedRuntimeServices,
2547
+ ];
2548
+ context.paperclipRuntimeServices = combinedRuntimeServices;
2549
+ context.paperclipRuntimePrimaryUrl =
2550
+ combinedRuntimeServices.find((service) => readNonEmptyString(service.url))?.url ?? null;
2551
+ await db
2552
+ .update(heartbeatRuns)
2553
+ .set({
2554
+ contextSnapshot: context,
2555
+ updatedAt: new Date(),
2556
+ })
2557
+ .where(eq(heartbeatRuns.id, run.id));
2558
+ if (issueId) {
2559
+ try {
2560
+ await issuesSvc.addComment(
2561
+ issueId,
2562
+ buildWorkspaceReadyComment({
2563
+ workspace: executionWorkspace,
2564
+ runtimeServices: adapterManagedRuntimeServices,
2565
+ }),
2566
+ { agentId: agent.id },
2567
+ );
2568
+ } catch (err) {
2569
+ await onLog(
2570
+ "stderr",
2571
+ `[paperclip] Failed to post adapter-managed runtime comment: ${err instanceof Error ? err.message : String(err)}\n`,
2572
+ );
2573
+ }
2574
+ }
2575
+ }
2576
+ const nextSessionState = resolveNextSessionState({
2577
+ codec: sessionCodec,
2578
+ adapterResult,
2579
+ previousParams: previousSessionParams,
2580
+ previousDisplayId: runtimeForAdapter.sessionDisplayId,
2581
+ previousLegacySessionId: runtimeForAdapter.sessionId,
2582
+ });
2583
+ const rawUsage = normalizeUsageTotals(adapterResult.usage);
2584
+ const sessionUsageResolution = await resolveNormalizedUsageForSession({
2585
+ agentId: agent.id,
2586
+ runId: run.id,
2587
+ sessionId: nextSessionState.displayId ?? nextSessionState.legacySessionId,
2588
+ rawUsage,
2589
+ });
2590
+ const normalizedUsage = sessionUsageResolution.normalizedUsage;
2591
+
2592
+ let outcome: "succeeded" | "failed" | "cancelled" | "timed_out";
2593
+ const latestRun = await getRun(run.id);
2594
+ if (latestRun?.status === "cancelled") {
2595
+ outcome = "cancelled";
2596
+ } else if (adapterResult.timedOut) {
2597
+ outcome = "timed_out";
2598
+ } else if ((adapterResult.exitCode ?? 0) === 0 && !adapterResult.errorMessage) {
2599
+ outcome = "succeeded";
2600
+ } else {
2601
+ outcome = "failed";
2602
+ }
2603
+
2604
+ let logSummary: { bytes: number; sha256?: string; compressed: boolean } | null = null;
2605
+ if (handle) {
2606
+ logSummary = await runLogStore.finalize(handle);
2607
+ }
2608
+
2609
+ const status =
2610
+ outcome === "succeeded"
2611
+ ? "succeeded"
2612
+ : outcome === "cancelled"
2613
+ ? "cancelled"
2614
+ : outcome === "timed_out"
2615
+ ? "timed_out"
2616
+ : "failed";
2617
+
2618
+ const usageJson =
2619
+ normalizedUsage || adapterResult.costUsd != null
2620
+ ? ({
2621
+ ...(normalizedUsage ?? {}),
2622
+ ...(rawUsage ? {
2623
+ rawInputTokens: rawUsage.inputTokens,
2624
+ rawCachedInputTokens: rawUsage.cachedInputTokens,
2625
+ rawOutputTokens: rawUsage.outputTokens,
2626
+ } : {}),
2627
+ ...(sessionUsageResolution.derivedFromSessionTotals ? { usageSource: "session_delta" } : {}),
2628
+ ...((nextSessionState.displayId ?? nextSessionState.legacySessionId)
2629
+ ? { persistedSessionId: nextSessionState.displayId ?? nextSessionState.legacySessionId }
2630
+ : {}),
2631
+ sessionReused: runtimeForAdapter.sessionId != null || runtimeForAdapter.sessionDisplayId != null,
2632
+ taskSessionReused: taskSessionForRun != null,
2633
+ freshSession: runtimeForAdapter.sessionId == null && runtimeForAdapter.sessionDisplayId == null,
2634
+ sessionRotated: sessionCompaction.rotate,
2635
+ sessionRotationReason: sessionCompaction.reason,
2636
+ provider: readNonEmptyString(adapterResult.provider) ?? "unknown",
2637
+ biller: resolveLedgerBiller(adapterResult),
2638
+ model: readNonEmptyString(adapterResult.model) ?? "unknown",
2639
+ ...(adapterResult.costUsd != null ? { costUsd: adapterResult.costUsd } : {}),
2640
+ billingType: normalizeLedgerBillingType(adapterResult.billingType),
2641
+ } as Record<string, unknown>)
2642
+ : null;
2643
+
2644
+ await setRunStatus(run.id, status, {
2645
+ finishedAt: new Date(),
2646
+ error:
2647
+ outcome === "succeeded"
2648
+ ? null
2649
+ : redactCurrentUserText(
2650
+ adapterResult.errorMessage ?? (outcome === "timed_out" ? "Timed out" : "Adapter failed"),
2651
+ currentUserRedactionOptions,
2652
+ ),
2653
+ errorCode:
2654
+ outcome === "timed_out"
2655
+ ? "timeout"
2656
+ : outcome === "cancelled"
2657
+ ? "cancelled"
2658
+ : outcome === "failed"
2659
+ ? (adapterResult.errorCode ?? "adapter_failed")
2660
+ : null,
2661
+ exitCode: adapterResult.exitCode,
2662
+ signal: adapterResult.signal,
2663
+ usageJson,
2664
+ resultJson: adapterResult.resultJson ?? null,
2665
+ sessionIdAfter: nextSessionState.displayId ?? nextSessionState.legacySessionId,
2666
+ stdoutExcerpt,
2667
+ stderrExcerpt,
2668
+ logBytes: logSummary?.bytes,
2669
+ logSha256: logSummary?.sha256,
2670
+ logCompressed: logSummary?.compressed ?? false,
2671
+ });
2672
+
2673
+ await setWakeupStatus(run.wakeupRequestId, outcome === "succeeded" ? "completed" : status, {
2674
+ finishedAt: new Date(),
2675
+ error: adapterResult.errorMessage ?? null,
2676
+ });
2677
+
2678
+ const finalizedRun = await getRun(run.id);
2679
+ if (finalizedRun) {
2680
+ await appendRunEvent(finalizedRun, seq++, {
2681
+ eventType: "lifecycle",
2682
+ stream: "system",
2683
+ level: outcome === "succeeded" ? "info" : "error",
2684
+ message: `run ${outcome}`,
2685
+ payload: {
2686
+ status,
2687
+ exitCode: adapterResult.exitCode,
2688
+ },
2689
+ });
2690
+ await releaseIssueExecutionAndPromote(finalizedRun);
2691
+ }
2692
+
2693
+ if (finalizedRun) {
2694
+ await updateRuntimeState(agent, finalizedRun, adapterResult, {
2695
+ legacySessionId: nextSessionState.legacySessionId,
2696
+ }, normalizedUsage);
2697
+ if (taskKey) {
2698
+ if (adapterResult.clearSession || (!nextSessionState.params && !nextSessionState.displayId)) {
2699
+ await clearTaskSessions(agent.companyId, agent.id, {
2700
+ taskKey,
2701
+ adapterType: agent.adapterType,
2702
+ });
2703
+ } else {
2704
+ await upsertTaskSession({
2705
+ companyId: agent.companyId,
2706
+ agentId: agent.id,
2707
+ adapterType: agent.adapterType,
2708
+ taskKey,
2709
+ sessionParamsJson: nextSessionState.params,
2710
+ sessionDisplayId: nextSessionState.displayId,
2711
+ lastRunId: finalizedRun.id,
2712
+ lastError: outcome === "succeeded" ? null : (adapterResult.errorMessage ?? "run_failed"),
2713
+ });
2714
+ }
2715
+ }
2716
+ }
2717
+ await finalizeAgentStatus(agent.id, outcome);
2718
+ } catch (err) {
2719
+ const message = redactCurrentUserText(
2720
+ err instanceof Error ? err.message : "Unknown adapter failure",
2721
+ await getCurrentUserRedactionOptions(),
2722
+ );
2723
+ logger.error({ err, runId }, "heartbeat execution failed");
2724
+
2725
+ let logSummary: { bytes: number; sha256?: string; compressed: boolean } | null = null;
2726
+ if (handle) {
2727
+ try {
2728
+ logSummary = await runLogStore.finalize(handle);
2729
+ } catch (finalizeErr) {
2730
+ logger.warn({ err: finalizeErr, runId }, "failed to finalize run log after error");
2731
+ }
2732
+ }
2733
+
2734
+ const failedRun = await setRunStatus(run.id, "failed", {
2735
+ error: message,
2736
+ errorCode: "adapter_failed",
2737
+ finishedAt: new Date(),
2738
+ stdoutExcerpt,
2739
+ stderrExcerpt,
2740
+ logBytes: logSummary?.bytes,
2741
+ logSha256: logSummary?.sha256,
2742
+ logCompressed: logSummary?.compressed ?? false,
2743
+ });
2744
+ await setWakeupStatus(run.wakeupRequestId, "failed", {
2745
+ finishedAt: new Date(),
2746
+ error: message,
2747
+ });
2748
+
2749
+ if (failedRun) {
2750
+ await appendRunEvent(failedRun, seq++, {
2751
+ eventType: "error",
2752
+ stream: "system",
2753
+ level: "error",
2754
+ message,
2755
+ });
2756
+ await releaseIssueExecutionAndPromote(failedRun);
2757
+
2758
+ await updateRuntimeState(agent, failedRun, {
2759
+ exitCode: null,
2760
+ signal: null,
2761
+ timedOut: false,
2762
+ errorMessage: message,
2763
+ }, {
2764
+ legacySessionId: runtimeForAdapter.sessionId,
2765
+ });
2766
+
2767
+ if (taskKey && (previousSessionParams || previousSessionDisplayId || taskSession)) {
2768
+ await upsertTaskSession({
2769
+ companyId: agent.companyId,
2770
+ agentId: agent.id,
2771
+ adapterType: agent.adapterType,
2772
+ taskKey,
2773
+ sessionParamsJson: previousSessionParams,
2774
+ sessionDisplayId: previousSessionDisplayId,
2775
+ lastRunId: failedRun.id,
2776
+ lastError: message,
2777
+ });
2778
+ }
2779
+ }
2780
+
2781
+ await finalizeAgentStatus(agent.id, "failed");
2782
+ }
2783
+ } catch (outerErr) {
2784
+ // Setup code before adapter.execute threw (e.g. ensureRuntimeState, resolveWorkspaceForRun).
2785
+ // The inner catch did not fire, so we must record the failure here.
2786
+ const message = outerErr instanceof Error ? outerErr.message : "Unknown setup failure";
2787
+ logger.error({ err: outerErr, runId }, "heartbeat execution setup failed");
2788
+ await setRunStatus(runId, "failed", {
2789
+ error: message,
2790
+ errorCode: "adapter_failed",
2791
+ finishedAt: new Date(),
2792
+ }).catch(() => undefined);
2793
+ await setWakeupStatus(run.wakeupRequestId, "failed", {
2794
+ finishedAt: new Date(),
2795
+ error: message,
2796
+ }).catch(() => undefined);
2797
+ const failedRun = await getRun(runId).catch(() => null);
2798
+ if (failedRun) {
2799
+ // Emit a run-log event so the failure is visible in the run timeline,
2800
+ // consistent with what the inner catch block does for adapter failures.
2801
+ await appendRunEvent(failedRun, 1, {
2802
+ eventType: "error",
2803
+ stream: "system",
2804
+ level: "error",
2805
+ message,
2806
+ }).catch(() => undefined);
2807
+ await releaseIssueExecutionAndPromote(failedRun).catch(() => undefined);
2808
+ }
2809
+ // Ensure the agent is not left stuck in "running" if the inner catch handler's
2810
+ // DB calls threw (e.g. a transient DB error in finalizeAgentStatus).
2811
+ await finalizeAgentStatus(run.agentId, "failed").catch(() => undefined);
2812
+ } finally {
2813
+ await releaseRuntimeServicesForRun(run.id).catch(() => undefined);
2814
+ activeRunExecutions.delete(run.id);
2815
+ await startNextQueuedRunForAgent(run.agentId);
2816
+ }
2817
+ }
2818
+
2819
+ async function releaseIssueExecutionAndPromote(run: typeof heartbeatRuns.$inferSelect) {
2820
+ const promotedRun = await db.transaction(async (tx) => {
2821
+ await tx.execute(
2822
+ sql`select id from issues where company_id = ${run.companyId} and execution_run_id = ${run.id} for update`,
2823
+ );
2824
+
2825
+ const issue = await tx
2826
+ .select({
2827
+ id: issues.id,
2828
+ companyId: issues.companyId,
2829
+ })
2830
+ .from(issues)
2831
+ .where(and(eq(issues.companyId, run.companyId), eq(issues.executionRunId, run.id)))
2832
+ .then((rows) => rows[0] ?? null);
2833
+
2834
+ if (!issue) return;
2835
+
2836
+ await tx
2837
+ .update(issues)
2838
+ .set({
2839
+ executionRunId: null,
2840
+ executionAgentNameKey: null,
2841
+ executionLockedAt: null,
2842
+ updatedAt: new Date(),
2843
+ })
2844
+ .where(eq(issues.id, issue.id));
2845
+
2846
+ while (true) {
2847
+ const deferred = await tx
2848
+ .select()
2849
+ .from(agentWakeupRequests)
2850
+ .where(
2851
+ and(
2852
+ eq(agentWakeupRequests.companyId, issue.companyId),
2853
+ eq(agentWakeupRequests.status, "deferred_issue_execution"),
2854
+ sql`${agentWakeupRequests.payload} ->> 'issueId' = ${issue.id}`,
2855
+ ),
2856
+ )
2857
+ .orderBy(asc(agentWakeupRequests.requestedAt))
2858
+ .limit(1)
2859
+ .then((rows) => rows[0] ?? null);
2860
+
2861
+ if (!deferred) return null;
2862
+
2863
+ const deferredAgent = await tx
2864
+ .select()
2865
+ .from(agents)
2866
+ .where(eq(agents.id, deferred.agentId))
2867
+ .then((rows) => rows[0] ?? null);
2868
+
2869
+ if (
2870
+ !deferredAgent ||
2871
+ deferredAgent.companyId !== issue.companyId ||
2872
+ deferredAgent.status === "paused" ||
2873
+ deferredAgent.status === "terminated" ||
2874
+ deferredAgent.status === "pending_approval"
2875
+ ) {
2876
+ await tx
2877
+ .update(agentWakeupRequests)
2878
+ .set({
2879
+ status: "failed",
2880
+ finishedAt: new Date(),
2881
+ error: "Deferred wake could not be promoted: agent is not invokable",
2882
+ updatedAt: new Date(),
2883
+ })
2884
+ .where(eq(agentWakeupRequests.id, deferred.id));
2885
+ continue;
2886
+ }
2887
+
2888
+ const deferredPayload = parseObject(deferred.payload);
2889
+ const deferredContextSeed = parseObject(deferredPayload[DEFERRED_WAKE_CONTEXT_KEY]);
2890
+ const promotedContextSeed: Record<string, unknown> = { ...deferredContextSeed };
2891
+ const promotedReason = readNonEmptyString(deferred.reason) ?? "issue_execution_promoted";
2892
+ const promotedSource =
2893
+ (readNonEmptyString(deferred.source) as WakeupOptions["source"]) ?? "automation";
2894
+ const promotedTriggerDetail =
2895
+ (readNonEmptyString(deferred.triggerDetail) as WakeupOptions["triggerDetail"]) ?? null;
2896
+ const promotedPayload = deferredPayload;
2897
+ delete promotedPayload[DEFERRED_WAKE_CONTEXT_KEY];
2898
+
2899
+ const {
2900
+ contextSnapshot: promotedContextSnapshot,
2901
+ taskKey: promotedTaskKey,
2902
+ } = enrichWakeContextSnapshot({
2903
+ contextSnapshot: promotedContextSeed,
2904
+ reason: promotedReason,
2905
+ source: promotedSource,
2906
+ triggerDetail: promotedTriggerDetail,
2907
+ payload: promotedPayload,
2908
+ });
2909
+
2910
+ const sessionBefore =
2911
+ readNonEmptyString(promotedContextSnapshot.resumeSessionDisplayId) ??
2912
+ await resolveSessionBeforeForWakeup(deferredAgent, promotedTaskKey);
2913
+ const now = new Date();
2914
+ const newRun = await tx
2915
+ .insert(heartbeatRuns)
2916
+ .values({
2917
+ companyId: deferredAgent.companyId,
2918
+ agentId: deferredAgent.id,
2919
+ invocationSource: promotedSource,
2920
+ triggerDetail: promotedTriggerDetail,
2921
+ status: "queued",
2922
+ wakeupRequestId: deferred.id,
2923
+ contextSnapshot: promotedContextSnapshot,
2924
+ sessionIdBefore: sessionBefore,
2925
+ })
2926
+ .returning()
2927
+ .then((rows) => rows[0]);
2928
+
2929
+ await tx
2930
+ .update(agentWakeupRequests)
2931
+ .set({
2932
+ status: "queued",
2933
+ reason: "issue_execution_promoted",
2934
+ runId: newRun.id,
2935
+ claimedAt: null,
2936
+ finishedAt: null,
2937
+ error: null,
2938
+ updatedAt: now,
2939
+ })
2940
+ .where(eq(agentWakeupRequests.id, deferred.id));
2941
+
2942
+ await tx
2943
+ .update(issues)
2944
+ .set({
2945
+ executionRunId: newRun.id,
2946
+ executionAgentNameKey: normalizeAgentNameKey(deferredAgent.name),
2947
+ executionLockedAt: now,
2948
+ updatedAt: now,
2949
+ })
2950
+ .where(eq(issues.id, issue.id));
2951
+
2952
+ return newRun;
2953
+ }
2954
+ });
2955
+
2956
+ if (!promotedRun) return;
2957
+
2958
+ publishLiveEvent({
2959
+ companyId: promotedRun.companyId,
2960
+ type: "heartbeat.run.queued",
2961
+ payload: {
2962
+ runId: promotedRun.id,
2963
+ agentId: promotedRun.agentId,
2964
+ invocationSource: promotedRun.invocationSource,
2965
+ triggerDetail: promotedRun.triggerDetail,
2966
+ wakeupRequestId: promotedRun.wakeupRequestId,
2967
+ },
2968
+ });
2969
+
2970
+ await startNextQueuedRunForAgent(promotedRun.agentId);
2971
+ }
2972
+
2973
+ async function enqueueWakeup(agentId: string, opts: WakeupOptions = {}) {
2974
+ const source = opts.source ?? "on_demand";
2975
+ const triggerDetail = opts.triggerDetail ?? null;
2976
+ const contextSnapshot: Record<string, unknown> = { ...(opts.contextSnapshot ?? {}) };
2977
+ const reason = opts.reason ?? null;
2978
+ const payload = opts.payload ?? null;
2979
+ const {
2980
+ contextSnapshot: enrichedContextSnapshot,
2981
+ issueIdFromPayload,
2982
+ taskKey,
2983
+ wakeCommentId,
2984
+ } = enrichWakeContextSnapshot({
2985
+ contextSnapshot,
2986
+ reason,
2987
+ source,
2988
+ triggerDetail,
2989
+ payload,
2990
+ });
2991
+ let issueId = readNonEmptyString(enrichedContextSnapshot.issueId) ?? issueIdFromPayload;
2992
+
2993
+ const agent = await getAgent(agentId);
2994
+ if (!agent) throw notFound("Agent not found");
2995
+ const explicitResumeSession = await resolveExplicitResumeSessionOverride(agent, payload, taskKey);
2996
+ if (explicitResumeSession) {
2997
+ enrichedContextSnapshot.resumeFromRunId = explicitResumeSession.resumeFromRunId;
2998
+ enrichedContextSnapshot.resumeSessionDisplayId = explicitResumeSession.sessionDisplayId;
2999
+ enrichedContextSnapshot.resumeSessionParams = explicitResumeSession.sessionParams;
3000
+ if (!readNonEmptyString(enrichedContextSnapshot.issueId) && explicitResumeSession.issueId) {
3001
+ enrichedContextSnapshot.issueId = explicitResumeSession.issueId;
3002
+ }
3003
+ if (!readNonEmptyString(enrichedContextSnapshot.taskId) && explicitResumeSession.taskId) {
3004
+ enrichedContextSnapshot.taskId = explicitResumeSession.taskId;
3005
+ }
3006
+ if (!readNonEmptyString(enrichedContextSnapshot.taskKey) && explicitResumeSession.taskKey) {
3007
+ enrichedContextSnapshot.taskKey = explicitResumeSession.taskKey;
3008
+ }
3009
+ issueId = readNonEmptyString(enrichedContextSnapshot.issueId) ?? issueId;
3010
+ }
3011
+ const effectiveTaskKey = readNonEmptyString(enrichedContextSnapshot.taskKey) ?? taskKey;
3012
+ const sessionBefore =
3013
+ explicitResumeSession?.sessionDisplayId ??
3014
+ await resolveSessionBeforeForWakeup(agent, effectiveTaskKey);
3015
+
3016
+ const writeSkippedRequest = async (skipReason: string) => {
3017
+ await db.insert(agentWakeupRequests).values({
3018
+ companyId: agent.companyId,
3019
+ agentId,
3020
+ source,
3021
+ triggerDetail,
3022
+ reason: skipReason,
3023
+ payload,
3024
+ status: "skipped",
3025
+ requestedByActorType: opts.requestedByActorType ?? null,
3026
+ requestedByActorId: opts.requestedByActorId ?? null,
3027
+ idempotencyKey: opts.idempotencyKey ?? null,
3028
+ finishedAt: new Date(),
3029
+ });
3030
+ };
3031
+
3032
+ let projectId = readNonEmptyString(enrichedContextSnapshot.projectId);
3033
+ if (!projectId && issueId) {
3034
+ projectId = await db
3035
+ .select({ projectId: issues.projectId })
3036
+ .from(issues)
3037
+ .where(and(eq(issues.id, issueId), eq(issues.companyId, agent.companyId)))
3038
+ .then((rows) => rows[0]?.projectId ?? null);
3039
+ }
3040
+
3041
+ const budgetBlock = await budgets.getInvocationBlock(agent.companyId, agentId, {
3042
+ issueId,
3043
+ projectId,
3044
+ });
3045
+ if (budgetBlock) {
3046
+ await writeSkippedRequest("budget.blocked");
3047
+ throw conflict(budgetBlock.reason, {
3048
+ scopeType: budgetBlock.scopeType,
3049
+ scopeId: budgetBlock.scopeId,
3050
+ });
3051
+ }
3052
+
3053
+ if (
3054
+ agent.status === "paused" ||
3055
+ agent.status === "terminated" ||
3056
+ agent.status === "pending_approval"
3057
+ ) {
3058
+ throw conflict("Agent is not invokable in its current state", { status: agent.status });
3059
+ }
3060
+
3061
+ const policy = parseHeartbeatPolicy(agent);
3062
+
3063
+ if (source === "timer" && !policy.enabled) {
3064
+ await writeSkippedRequest("heartbeat.disabled");
3065
+ return null;
3066
+ }
3067
+ if (source !== "timer" && !policy.wakeOnDemand) {
3068
+ await writeSkippedRequest("heartbeat.wakeOnDemand.disabled");
3069
+ return null;
3070
+ }
3071
+
3072
+ const bypassIssueExecutionLock =
3073
+ reason === "issue_comment_mentioned" ||
3074
+ readNonEmptyString(enrichedContextSnapshot.wakeReason) === "issue_comment_mentioned";
3075
+
3076
+ if (issueId && !bypassIssueExecutionLock) {
3077
+ const agentNameKey = normalizeAgentNameKey(agent.name);
3078
+
3079
+ const outcome = await db.transaction(async (tx) => {
3080
+ await tx.execute(
3081
+ sql`select id from issues where id = ${issueId} and company_id = ${agent.companyId} for update`,
3082
+ );
3083
+
3084
+ const issue = await tx
3085
+ .select({
3086
+ id: issues.id,
3087
+ companyId: issues.companyId,
3088
+ executionRunId: issues.executionRunId,
3089
+ executionAgentNameKey: issues.executionAgentNameKey,
3090
+ })
3091
+ .from(issues)
3092
+ .where(and(eq(issues.id, issueId), eq(issues.companyId, agent.companyId)))
3093
+ .then((rows) => rows[0] ?? null);
3094
+
3095
+ if (!issue) {
3096
+ await tx.insert(agentWakeupRequests).values({
3097
+ companyId: agent.companyId,
3098
+ agentId,
3099
+ source,
3100
+ triggerDetail,
3101
+ reason: "issue_execution_issue_not_found",
3102
+ payload,
3103
+ status: "skipped",
3104
+ requestedByActorType: opts.requestedByActorType ?? null,
3105
+ requestedByActorId: opts.requestedByActorId ?? null,
3106
+ idempotencyKey: opts.idempotencyKey ?? null,
3107
+ finishedAt: new Date(),
3108
+ });
3109
+ return { kind: "skipped" as const };
3110
+ }
3111
+
3112
+ let activeExecutionRun = issue.executionRunId
3113
+ ? await tx
3114
+ .select()
3115
+ .from(heartbeatRuns)
3116
+ .where(eq(heartbeatRuns.id, issue.executionRunId))
3117
+ .then((rows) => rows[0] ?? null)
3118
+ : null;
3119
+
3120
+ if (activeExecutionRun && activeExecutionRun.status !== "queued" && activeExecutionRun.status !== "running") {
3121
+ activeExecutionRun = null;
3122
+ }
3123
+
3124
+ if (!activeExecutionRun && issue.executionRunId) {
3125
+ await tx
3126
+ .update(issues)
3127
+ .set({
3128
+ executionRunId: null,
3129
+ executionAgentNameKey: null,
3130
+ executionLockedAt: null,
3131
+ updatedAt: new Date(),
3132
+ })
3133
+ .where(eq(issues.id, issue.id));
3134
+ }
3135
+
3136
+ if (!activeExecutionRun) {
3137
+ const legacyRun = await tx
3138
+ .select()
3139
+ .from(heartbeatRuns)
3140
+ .where(
3141
+ and(
3142
+ eq(heartbeatRuns.companyId, issue.companyId),
3143
+ inArray(heartbeatRuns.status, ["queued", "running"]),
3144
+ sql`${heartbeatRuns.contextSnapshot} ->> 'issueId' = ${issue.id}`,
3145
+ ),
3146
+ )
3147
+ .orderBy(
3148
+ sql`case when ${heartbeatRuns.status} = 'running' then 0 else 1 end`,
3149
+ asc(heartbeatRuns.createdAt),
3150
+ )
3151
+ .limit(1)
3152
+ .then((rows) => rows[0] ?? null);
3153
+
3154
+ if (legacyRun) {
3155
+ activeExecutionRun = legacyRun;
3156
+ const legacyAgent = await tx
3157
+ .select({ name: agents.name })
3158
+ .from(agents)
3159
+ .where(eq(agents.id, legacyRun.agentId))
3160
+ .then((rows) => rows[0] ?? null);
3161
+ await tx
3162
+ .update(issues)
3163
+ .set({
3164
+ executionRunId: legacyRun.id,
3165
+ executionAgentNameKey: normalizeAgentNameKey(legacyAgent?.name),
3166
+ executionLockedAt: new Date(),
3167
+ updatedAt: new Date(),
3168
+ })
3169
+ .where(eq(issues.id, issue.id));
3170
+ }
3171
+ }
3172
+
3173
+ if (activeExecutionRun) {
3174
+ const executionAgent = await tx
3175
+ .select({ name: agents.name })
3176
+ .from(agents)
3177
+ .where(eq(agents.id, activeExecutionRun.agentId))
3178
+ .then((rows) => rows[0] ?? null);
3179
+ const executionAgentNameKey =
3180
+ normalizeAgentNameKey(issue.executionAgentNameKey) ??
3181
+ normalizeAgentNameKey(executionAgent?.name);
3182
+ const isSameExecutionAgent =
3183
+ Boolean(executionAgentNameKey) && executionAgentNameKey === agentNameKey;
3184
+ const shouldQueueFollowupForCommentWake =
3185
+ Boolean(wakeCommentId) &&
3186
+ activeExecutionRun.status === "running" &&
3187
+ isSameExecutionAgent;
3188
+
3189
+ if (isSameExecutionAgent && !shouldQueueFollowupForCommentWake) {
3190
+ const mergedContextSnapshot = mergeCoalescedContextSnapshot(
3191
+ activeExecutionRun.contextSnapshot,
3192
+ enrichedContextSnapshot,
3193
+ );
3194
+ const mergedRun = await tx
3195
+ .update(heartbeatRuns)
3196
+ .set({
3197
+ contextSnapshot: mergedContextSnapshot,
3198
+ updatedAt: new Date(),
3199
+ })
3200
+ .where(eq(heartbeatRuns.id, activeExecutionRun.id))
3201
+ .returning()
3202
+ .then((rows) => rows[0] ?? activeExecutionRun);
3203
+
3204
+ await tx.insert(agentWakeupRequests).values({
3205
+ companyId: agent.companyId,
3206
+ agentId,
3207
+ source,
3208
+ triggerDetail,
3209
+ reason: "issue_execution_same_name",
3210
+ payload,
3211
+ status: "coalesced",
3212
+ coalescedCount: 1,
3213
+ requestedByActorType: opts.requestedByActorType ?? null,
3214
+ requestedByActorId: opts.requestedByActorId ?? null,
3215
+ idempotencyKey: opts.idempotencyKey ?? null,
3216
+ runId: mergedRun.id,
3217
+ finishedAt: new Date(),
3218
+ });
3219
+
3220
+ return { kind: "coalesced" as const, run: mergedRun };
3221
+ }
3222
+
3223
+ const deferredPayload = {
3224
+ ...(payload ?? {}),
3225
+ issueId,
3226
+ [DEFERRED_WAKE_CONTEXT_KEY]: enrichedContextSnapshot,
3227
+ };
3228
+
3229
+ const existingDeferred = await tx
3230
+ .select()
3231
+ .from(agentWakeupRequests)
3232
+ .where(
3233
+ and(
3234
+ eq(agentWakeupRequests.companyId, agent.companyId),
3235
+ eq(agentWakeupRequests.agentId, agentId),
3236
+ eq(agentWakeupRequests.status, "deferred_issue_execution"),
3237
+ sql`${agentWakeupRequests.payload} ->> 'issueId' = ${issue.id}`,
3238
+ ),
3239
+ )
3240
+ .orderBy(asc(agentWakeupRequests.requestedAt))
3241
+ .limit(1)
3242
+ .then((rows) => rows[0] ?? null);
3243
+
3244
+ if (existingDeferred) {
3245
+ const existingDeferredPayload = parseObject(existingDeferred.payload);
3246
+ const existingDeferredContext = parseObject(existingDeferredPayload[DEFERRED_WAKE_CONTEXT_KEY]);
3247
+ const mergedDeferredContext = mergeCoalescedContextSnapshot(
3248
+ existingDeferredContext,
3249
+ enrichedContextSnapshot,
3250
+ );
3251
+ const mergedDeferredPayload = {
3252
+ ...existingDeferredPayload,
3253
+ ...(payload ?? {}),
3254
+ issueId,
3255
+ [DEFERRED_WAKE_CONTEXT_KEY]: mergedDeferredContext,
3256
+ };
3257
+
3258
+ await tx
3259
+ .update(agentWakeupRequests)
3260
+ .set({
3261
+ payload: mergedDeferredPayload,
3262
+ coalescedCount: (existingDeferred.coalescedCount ?? 0) + 1,
3263
+ updatedAt: new Date(),
3264
+ })
3265
+ .where(eq(agentWakeupRequests.id, existingDeferred.id));
3266
+
3267
+ return { kind: "deferred" as const };
3268
+ }
3269
+
3270
+ await tx.insert(agentWakeupRequests).values({
3271
+ companyId: agent.companyId,
3272
+ agentId,
3273
+ source,
3274
+ triggerDetail,
3275
+ reason: "issue_execution_deferred",
3276
+ payload: deferredPayload,
3277
+ status: "deferred_issue_execution",
3278
+ requestedByActorType: opts.requestedByActorType ?? null,
3279
+ requestedByActorId: opts.requestedByActorId ?? null,
3280
+ idempotencyKey: opts.idempotencyKey ?? null,
3281
+ });
3282
+
3283
+ return { kind: "deferred" as const };
3284
+ }
3285
+
3286
+ const wakeupRequest = await tx
3287
+ .insert(agentWakeupRequests)
3288
+ .values({
3289
+ companyId: agent.companyId,
3290
+ agentId,
3291
+ source,
3292
+ triggerDetail,
3293
+ reason,
3294
+ payload,
3295
+ status: "queued",
3296
+ requestedByActorType: opts.requestedByActorType ?? null,
3297
+ requestedByActorId: opts.requestedByActorId ?? null,
3298
+ idempotencyKey: opts.idempotencyKey ?? null,
3299
+ })
3300
+ .returning()
3301
+ .then((rows) => rows[0]);
3302
+
3303
+ const newRun = await tx
3304
+ .insert(heartbeatRuns)
3305
+ .values({
3306
+ companyId: agent.companyId,
3307
+ agentId,
3308
+ invocationSource: source,
3309
+ triggerDetail,
3310
+ status: "queued",
3311
+ wakeupRequestId: wakeupRequest.id,
3312
+ contextSnapshot: enrichedContextSnapshot,
3313
+ sessionIdBefore: sessionBefore,
3314
+ })
3315
+ .returning()
3316
+ .then((rows) => rows[0]);
3317
+
3318
+ await tx
3319
+ .update(agentWakeupRequests)
3320
+ .set({
3321
+ runId: newRun.id,
3322
+ updatedAt: new Date(),
3323
+ })
3324
+ .where(eq(agentWakeupRequests.id, wakeupRequest.id));
3325
+
3326
+ await tx
3327
+ .update(issues)
3328
+ .set({
3329
+ executionRunId: newRun.id,
3330
+ executionAgentNameKey: agentNameKey,
3331
+ executionLockedAt: new Date(),
3332
+ updatedAt: new Date(),
3333
+ })
3334
+ .where(eq(issues.id, issue.id));
3335
+
3336
+ return { kind: "queued" as const, run: newRun };
3337
+ });
3338
+
3339
+ if (outcome.kind === "deferred" || outcome.kind === "skipped") return null;
3340
+ if (outcome.kind === "coalesced") return outcome.run;
3341
+
3342
+ const newRun = outcome.run;
3343
+ publishLiveEvent({
3344
+ companyId: newRun.companyId,
3345
+ type: "heartbeat.run.queued",
3346
+ payload: {
3347
+ runId: newRun.id,
3348
+ agentId: newRun.agentId,
3349
+ invocationSource: newRun.invocationSource,
3350
+ triggerDetail: newRun.triggerDetail,
3351
+ wakeupRequestId: newRun.wakeupRequestId,
3352
+ },
3353
+ });
3354
+
3355
+ await startNextQueuedRunForAgent(agent.id);
3356
+ return newRun;
3357
+ }
3358
+
3359
+ const activeRuns = await db
3360
+ .select()
3361
+ .from(heartbeatRuns)
3362
+ .where(and(eq(heartbeatRuns.agentId, agentId), inArray(heartbeatRuns.status, ["queued", "running"])))
3363
+ .orderBy(desc(heartbeatRuns.createdAt));
3364
+
3365
+ const sameScopeQueuedRun = activeRuns.find(
3366
+ (candidate) => candidate.status === "queued" && isSameTaskScope(runTaskKey(candidate), taskKey),
3367
+ );
3368
+ const sameScopeRunningRun = activeRuns.find(
3369
+ (candidate) => candidate.status === "running" && isSameTaskScope(runTaskKey(candidate), taskKey),
3370
+ );
3371
+ const shouldQueueFollowupForCommentWake =
3372
+ Boolean(wakeCommentId) && Boolean(sameScopeRunningRun) && !sameScopeQueuedRun;
3373
+
3374
+ const coalescedTargetRun =
3375
+ sameScopeQueuedRun ??
3376
+ (shouldQueueFollowupForCommentWake ? null : sameScopeRunningRun ?? null);
3377
+
3378
+ if (coalescedTargetRun) {
3379
+ const mergedContextSnapshot = mergeCoalescedContextSnapshot(
3380
+ coalescedTargetRun.contextSnapshot,
3381
+ contextSnapshot,
3382
+ );
3383
+ const mergedRun = await db
3384
+ .update(heartbeatRuns)
3385
+ .set({
3386
+ contextSnapshot: mergedContextSnapshot,
3387
+ updatedAt: new Date(),
3388
+ })
3389
+ .where(eq(heartbeatRuns.id, coalescedTargetRun.id))
3390
+ .returning()
3391
+ .then((rows) => rows[0] ?? coalescedTargetRun);
3392
+
3393
+ await db.insert(agentWakeupRequests).values({
3394
+ companyId: agent.companyId,
3395
+ agentId,
3396
+ source,
3397
+ triggerDetail,
3398
+ reason,
3399
+ payload,
3400
+ status: "coalesced",
3401
+ coalescedCount: 1,
3402
+ requestedByActorType: opts.requestedByActorType ?? null,
3403
+ requestedByActorId: opts.requestedByActorId ?? null,
3404
+ idempotencyKey: opts.idempotencyKey ?? null,
3405
+ runId: mergedRun.id,
3406
+ finishedAt: new Date(),
3407
+ });
3408
+ return mergedRun;
3409
+ }
3410
+
3411
+ const wakeupRequest = await db
3412
+ .insert(agentWakeupRequests)
3413
+ .values({
3414
+ companyId: agent.companyId,
3415
+ agentId,
3416
+ source,
3417
+ triggerDetail,
3418
+ reason,
3419
+ payload,
3420
+ status: "queued",
3421
+ requestedByActorType: opts.requestedByActorType ?? null,
3422
+ requestedByActorId: opts.requestedByActorId ?? null,
3423
+ idempotencyKey: opts.idempotencyKey ?? null,
3424
+ })
3425
+ .returning()
3426
+ .then((rows) => rows[0]);
3427
+
3428
+ const newRun = await db
3429
+ .insert(heartbeatRuns)
3430
+ .values({
3431
+ companyId: agent.companyId,
3432
+ agentId,
3433
+ invocationSource: source,
3434
+ triggerDetail,
3435
+ status: "queued",
3436
+ wakeupRequestId: wakeupRequest.id,
3437
+ contextSnapshot: enrichedContextSnapshot,
3438
+ sessionIdBefore: sessionBefore,
3439
+ })
3440
+ .returning()
3441
+ .then((rows) => rows[0]);
3442
+
3443
+ await db
3444
+ .update(agentWakeupRequests)
3445
+ .set({
3446
+ runId: newRun.id,
3447
+ updatedAt: new Date(),
3448
+ })
3449
+ .where(eq(agentWakeupRequests.id, wakeupRequest.id));
3450
+
3451
+ publishLiveEvent({
3452
+ companyId: newRun.companyId,
3453
+ type: "heartbeat.run.queued",
3454
+ payload: {
3455
+ runId: newRun.id,
3456
+ agentId: newRun.agentId,
3457
+ invocationSource: newRun.invocationSource,
3458
+ triggerDetail: newRun.triggerDetail,
3459
+ wakeupRequestId: newRun.wakeupRequestId,
3460
+ },
3461
+ });
3462
+
3463
+ await startNextQueuedRunForAgent(agent.id);
3464
+
3465
+ return newRun;
3466
+ }
3467
+
3468
+ async function listProjectScopedRunIds(companyId: string, projectId: string) {
3469
+ const runIssueId = sql<string | null>`${heartbeatRuns.contextSnapshot} ->> 'issueId'`;
3470
+ const effectiveProjectId = sql<string | null>`coalesce(${heartbeatRuns.contextSnapshot} ->> 'projectId', ${issues.projectId}::text)`;
3471
+
3472
+ const rows = await db
3473
+ .selectDistinctOn([heartbeatRuns.id], { id: heartbeatRuns.id })
3474
+ .from(heartbeatRuns)
3475
+ .leftJoin(
3476
+ issues,
3477
+ and(
3478
+ eq(issues.companyId, companyId),
3479
+ sql`${issues.id}::text = ${runIssueId}`,
3480
+ ),
3481
+ )
3482
+ .where(
3483
+ and(
3484
+ eq(heartbeatRuns.companyId, companyId),
3485
+ inArray(heartbeatRuns.status, ["queued", "running"]),
3486
+ sql`${effectiveProjectId} = ${projectId}`,
3487
+ ),
3488
+ );
3489
+
3490
+ return rows.map((row) => row.id);
3491
+ }
3492
+
3493
+ async function listProjectScopedWakeupIds(companyId: string, projectId: string) {
3494
+ const wakeIssueId = sql<string | null>`${agentWakeupRequests.payload} ->> 'issueId'`;
3495
+ const effectiveProjectId = sql<string | null>`coalesce(${agentWakeupRequests.payload} ->> 'projectId', ${issues.projectId}::text)`;
3496
+
3497
+ const rows = await db
3498
+ .selectDistinctOn([agentWakeupRequests.id], { id: agentWakeupRequests.id })
3499
+ .from(agentWakeupRequests)
3500
+ .leftJoin(
3501
+ issues,
3502
+ and(
3503
+ eq(issues.companyId, companyId),
3504
+ sql`${issues.id}::text = ${wakeIssueId}`,
3505
+ ),
3506
+ )
3507
+ .where(
3508
+ and(
3509
+ eq(agentWakeupRequests.companyId, companyId),
3510
+ inArray(agentWakeupRequests.status, ["queued", "deferred_issue_execution"]),
3511
+ sql`${agentWakeupRequests.runId} is null`,
3512
+ sql`${effectiveProjectId} = ${projectId}`,
3513
+ ),
3514
+ );
3515
+
3516
+ return rows.map((row) => row.id);
3517
+ }
3518
+
3519
+ async function cancelPendingWakeupsForBudgetScope(scope: BudgetEnforcementScope) {
3520
+ const now = new Date();
3521
+ let wakeupIds: string[] = [];
3522
+
3523
+ if (scope.scopeType === "company") {
3524
+ wakeupIds = await db
3525
+ .select({ id: agentWakeupRequests.id })
3526
+ .from(agentWakeupRequests)
3527
+ .where(
3528
+ and(
3529
+ eq(agentWakeupRequests.companyId, scope.companyId),
3530
+ inArray(agentWakeupRequests.status, ["queued", "deferred_issue_execution"]),
3531
+ sql`${agentWakeupRequests.runId} is null`,
3532
+ ),
3533
+ )
3534
+ .then((rows) => rows.map((row) => row.id));
3535
+ } else if (scope.scopeType === "agent") {
3536
+ wakeupIds = await db
3537
+ .select({ id: agentWakeupRequests.id })
3538
+ .from(agentWakeupRequests)
3539
+ .where(
3540
+ and(
3541
+ eq(agentWakeupRequests.companyId, scope.companyId),
3542
+ eq(agentWakeupRequests.agentId, scope.scopeId),
3543
+ inArray(agentWakeupRequests.status, ["queued", "deferred_issue_execution"]),
3544
+ sql`${agentWakeupRequests.runId} is null`,
3545
+ ),
3546
+ )
3547
+ .then((rows) => rows.map((row) => row.id));
3548
+ } else {
3549
+ wakeupIds = await listProjectScopedWakeupIds(scope.companyId, scope.scopeId);
3550
+ }
3551
+
3552
+ if (wakeupIds.length === 0) return 0;
3553
+
3554
+ await db
3555
+ .update(agentWakeupRequests)
3556
+ .set({
3557
+ status: "cancelled",
3558
+ finishedAt: now,
3559
+ error: "Cancelled due to budget pause",
3560
+ updatedAt: now,
3561
+ })
3562
+ .where(inArray(agentWakeupRequests.id, wakeupIds));
3563
+
3564
+ return wakeupIds.length;
3565
+ }
3566
+
3567
+ async function cancelRunInternal(runId: string, reason = "Cancelled by control plane") {
3568
+ const run = await getRun(runId);
3569
+ if (!run) throw notFound("Heartbeat run not found");
3570
+ if (run.status !== "running" && run.status !== "queued") return run;
3571
+
3572
+ const running = runningProcesses.get(run.id);
3573
+ if (running) {
3574
+ running.child.kill("SIGTERM");
3575
+ const graceMs = Math.max(1, running.graceSec) * 1000;
3576
+ setTimeout(() => {
3577
+ if (!running.child.killed) {
3578
+ running.child.kill("SIGKILL");
3579
+ }
3580
+ }, graceMs);
3581
+ }
3582
+
3583
+ const cancelled = await setRunStatus(run.id, "cancelled", {
3584
+ finishedAt: new Date(),
3585
+ error: reason,
3586
+ errorCode: "cancelled",
3587
+ });
3588
+
3589
+ await setWakeupStatus(run.wakeupRequestId, "cancelled", {
3590
+ finishedAt: new Date(),
3591
+ error: reason,
3592
+ });
3593
+
3594
+ if (cancelled) {
3595
+ await appendRunEvent(cancelled, 1, {
3596
+ eventType: "lifecycle",
3597
+ stream: "system",
3598
+ level: "warn",
3599
+ message: "run cancelled",
3600
+ });
3601
+ await releaseIssueExecutionAndPromote(cancelled);
3602
+ }
3603
+
3604
+ runningProcesses.delete(run.id);
3605
+ await finalizeAgentStatus(run.agentId, "cancelled");
3606
+ await startNextQueuedRunForAgent(run.agentId);
3607
+ return cancelled;
3608
+ }
3609
+
3610
+ async function cancelActiveForAgentInternal(agentId: string, reason = "Cancelled due to agent pause") {
3611
+ const runs = await db
3612
+ .select()
3613
+ .from(heartbeatRuns)
3614
+ .where(and(eq(heartbeatRuns.agentId, agentId), inArray(heartbeatRuns.status, ["queued", "running"])));
3615
+
3616
+ for (const run of runs) {
3617
+ await setRunStatus(run.id, "cancelled", {
3618
+ finishedAt: new Date(),
3619
+ error: reason,
3620
+ errorCode: "cancelled",
3621
+ });
3622
+
3623
+ await setWakeupStatus(run.wakeupRequestId, "cancelled", {
3624
+ finishedAt: new Date(),
3625
+ error: reason,
3626
+ });
3627
+
3628
+ const running = runningProcesses.get(run.id);
3629
+ if (running) {
3630
+ running.child.kill("SIGTERM");
3631
+ runningProcesses.delete(run.id);
3632
+ }
3633
+ await releaseIssueExecutionAndPromote(run);
3634
+ }
3635
+
3636
+ return runs.length;
3637
+ }
3638
+
3639
+ async function cancelBudgetScopeWork(scope: BudgetEnforcementScope) {
3640
+ if (scope.scopeType === "agent") {
3641
+ await cancelActiveForAgentInternal(scope.scopeId, "Cancelled due to budget pause");
3642
+ await cancelPendingWakeupsForBudgetScope(scope);
3643
+ return;
3644
+ }
3645
+
3646
+ const runIds =
3647
+ scope.scopeType === "company"
3648
+ ? await db
3649
+ .select({ id: heartbeatRuns.id })
3650
+ .from(heartbeatRuns)
3651
+ .where(
3652
+ and(
3653
+ eq(heartbeatRuns.companyId, scope.companyId),
3654
+ inArray(heartbeatRuns.status, ["queued", "running"]),
3655
+ ),
3656
+ )
3657
+ .then((rows) => rows.map((row) => row.id))
3658
+ : await listProjectScopedRunIds(scope.companyId, scope.scopeId);
3659
+
3660
+ for (const runId of runIds) {
3661
+ await cancelRunInternal(runId, "Cancelled due to budget pause");
3662
+ }
3663
+
3664
+ await cancelPendingWakeupsForBudgetScope(scope);
3665
+ }
3666
+
3667
+ return {
3668
+ list: async (companyId: string, agentId?: string, limit?: number) => {
3669
+ const query = db
3670
+ .select(heartbeatRunListColumns)
3671
+ .from(heartbeatRuns)
3672
+ .where(
3673
+ agentId
3674
+ ? and(eq(heartbeatRuns.companyId, companyId), eq(heartbeatRuns.agentId, agentId))
3675
+ : eq(heartbeatRuns.companyId, companyId),
3676
+ )
3677
+ .orderBy(desc(heartbeatRuns.createdAt));
3678
+
3679
+ const rows = limit ? await query.limit(limit) : await query;
3680
+ return rows.map((row) => ({
3681
+ ...row,
3682
+ resultJson: summarizeHeartbeatRunResultJson(row.resultJson),
3683
+ }));
3684
+ },
3685
+
3686
+ getRun,
3687
+
3688
+ getRuntimeState: async (agentId: string) => {
3689
+ const state = await getRuntimeState(agentId);
3690
+ const agent = await getAgent(agentId);
3691
+ if (!agent) return null;
3692
+ const ensured = state ?? (await ensureRuntimeState(agent));
3693
+ const latestTaskSession = await db
3694
+ .select()
3695
+ .from(agentTaskSessions)
3696
+ .where(and(eq(agentTaskSessions.companyId, agent.companyId), eq(agentTaskSessions.agentId, agent.id)))
3697
+ .orderBy(desc(agentTaskSessions.updatedAt))
3698
+ .limit(1)
3699
+ .then((rows) => rows[0] ?? null);
3700
+ return {
3701
+ ...ensured,
3702
+ sessionDisplayId: latestTaskSession?.sessionDisplayId ?? ensured.sessionId,
3703
+ sessionParamsJson: latestTaskSession?.sessionParamsJson ?? null,
3704
+ };
3705
+ },
3706
+
3707
+ listTaskSessions: async (agentId: string) => {
3708
+ const agent = await getAgent(agentId);
3709
+ if (!agent) throw notFound("Agent not found");
3710
+
3711
+ return db
3712
+ .select()
3713
+ .from(agentTaskSessions)
3714
+ .where(and(eq(agentTaskSessions.companyId, agent.companyId), eq(agentTaskSessions.agentId, agentId)))
3715
+ .orderBy(desc(agentTaskSessions.updatedAt), desc(agentTaskSessions.createdAt));
3716
+ },
3717
+
3718
+ resetRuntimeSession: async (agentId: string, opts?: { taskKey?: string | null }) => {
3719
+ const agent = await getAgent(agentId);
3720
+ if (!agent) throw notFound("Agent not found");
3721
+ await ensureRuntimeState(agent);
3722
+ const taskKey = readNonEmptyString(opts?.taskKey);
3723
+ const clearedTaskSessions = await clearTaskSessions(
3724
+ agent.companyId,
3725
+ agent.id,
3726
+ taskKey ? { taskKey, adapterType: agent.adapterType } : undefined,
3727
+ );
3728
+ const runtimePatch: Partial<typeof agentRuntimeState.$inferInsert> = {
3729
+ sessionId: null,
3730
+ lastError: null,
3731
+ updatedAt: new Date(),
3732
+ };
3733
+ if (!taskKey) {
3734
+ runtimePatch.stateJson = {};
3735
+ }
3736
+
3737
+ const updated = await db
3738
+ .update(agentRuntimeState)
3739
+ .set(runtimePatch)
3740
+ .where(eq(agentRuntimeState.agentId, agentId))
3741
+ .returning()
3742
+ .then((rows) => rows[0] ?? null);
3743
+
3744
+ if (!updated) return null;
3745
+ return {
3746
+ ...updated,
3747
+ sessionDisplayId: null,
3748
+ sessionParamsJson: null,
3749
+ clearedTaskSessions,
3750
+ };
3751
+ },
3752
+
3753
+ listEvents: (runId: string, afterSeq = 0, limit = 200) =>
3754
+ db
3755
+ .select()
3756
+ .from(heartbeatRunEvents)
3757
+ .where(and(eq(heartbeatRunEvents.runId, runId), gt(heartbeatRunEvents.seq, afterSeq)))
3758
+ .orderBy(asc(heartbeatRunEvents.seq))
3759
+ .limit(Math.max(1, Math.min(limit, 1000))),
3760
+
3761
+ readLog: async (runId: string, opts?: { offset?: number; limitBytes?: number }) => {
3762
+ const run = await getRun(runId);
3763
+ if (!run) throw notFound("Heartbeat run not found");
3764
+ if (!run.logStore || !run.logRef) throw notFound("Run log not found");
3765
+
3766
+ const result = await runLogStore.read(
3767
+ {
3768
+ store: run.logStore as "local_file",
3769
+ logRef: run.logRef,
3770
+ },
3771
+ opts,
3772
+ );
3773
+
3774
+ return {
3775
+ runId,
3776
+ store: run.logStore,
3777
+ logRef: run.logRef,
3778
+ ...result,
3779
+ content: redactCurrentUserText(result.content, await getCurrentUserRedactionOptions()),
3780
+ };
3781
+ },
3782
+
3783
+ invoke: async (
3784
+ agentId: string,
3785
+ source: "timer" | "assignment" | "on_demand" | "automation" = "on_demand",
3786
+ contextSnapshot: Record<string, unknown> = {},
3787
+ triggerDetail: "manual" | "ping" | "callback" | "system" = "manual",
3788
+ actor?: { actorType?: "user" | "agent" | "system"; actorId?: string | null },
3789
+ ) =>
3790
+ enqueueWakeup(agentId, {
3791
+ source,
3792
+ triggerDetail,
3793
+ contextSnapshot,
3794
+ requestedByActorType: actor?.actorType,
3795
+ requestedByActorId: actor?.actorId ?? null,
3796
+ }),
3797
+
3798
+ wakeup: enqueueWakeup,
3799
+
3800
+ reportRunActivity: clearDetachedRunWarning,
3801
+
3802
+ reapOrphanedRuns,
3803
+
3804
+ resumeQueuedRuns,
3805
+
3806
+ tickTimers: async (now = new Date()) => {
3807
+ const allAgents = await db.select().from(agents);
3808
+ let checked = 0;
3809
+ let enqueued = 0;
3810
+ let skipped = 0;
3811
+
3812
+ for (const agent of allAgents) {
3813
+ if (agent.status === "paused" || agent.status === "terminated" || agent.status === "pending_approval") continue;
3814
+ const policy = parseHeartbeatPolicy(agent);
3815
+ if (!policy.enabled || policy.intervalSec <= 0) continue;
3816
+
3817
+ checked += 1;
3818
+ const baseline = new Date(agent.lastHeartbeatAt ?? agent.createdAt).getTime();
3819
+ const elapsedMs = now.getTime() - baseline;
3820
+ if (elapsedMs < policy.intervalSec * 1000) continue;
3821
+
3822
+ const run = await enqueueWakeup(agent.id, {
3823
+ source: "timer",
3824
+ triggerDetail: "system",
3825
+ reason: "heartbeat_timer",
3826
+ requestedByActorType: "system",
3827
+ requestedByActorId: "heartbeat_scheduler",
3828
+ contextSnapshot: {
3829
+ source: "scheduler",
3830
+ reason: "interval_elapsed",
3831
+ now: now.toISOString(),
3832
+ },
3833
+ });
3834
+ if (run) enqueued += 1;
3835
+ else skipped += 1;
3836
+ }
3837
+
3838
+ return { checked, enqueued, skipped };
3839
+ },
3840
+
3841
+ cancelRun: (runId: string) => cancelRunInternal(runId),
3842
+
3843
+ cancelActiveForAgent: (agentId: string) => cancelActiveForAgentInternal(agentId),
3844
+
3845
+ cancelBudgetScopeWork,
3846
+
3847
+ getActiveRunForAgent: async (agentId: string) => {
3848
+ const [run] = await db
3849
+ .select()
3850
+ .from(heartbeatRuns)
3851
+ .where(
3852
+ and(
3853
+ eq(heartbeatRuns.agentId, agentId),
3854
+ eq(heartbeatRuns.status, "running"),
3855
+ ),
3856
+ )
3857
+ .orderBy(desc(heartbeatRuns.startedAt))
3858
+ .limit(1);
3859
+ return run ?? null;
3860
+ },
3861
+ };
3862
+ }
3863
+