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.
- package/.dockerignore +10 -0
- package/.env.example +3 -0
- package/.github/workflows/publish-cli.yml +49 -0
- package/.mailmap +1 -0
- package/AGENTS.md +148 -0
- package/CONTRIBUTING.md +75 -0
- package/Dockerfile +59 -0
- package/Dockerfile.onboard-smoke +42 -0
- package/LICENSE +21 -0
- package/README.md +93 -0
- package/cli/esbuild.config.mjs +11 -0
- package/cli/package.json +24 -0
- package/cli/scripts/build-cli.mjs +5 -0
- package/cli/src/index.ts +27 -0
- package/docker-compose.quickstart.yml +18 -0
- package/docker-compose.untrusted-review.yml +33 -0
- package/docker-compose.yml +38 -0
- package/package.json +56 -0
- package/patches/embedded-postgres@18.1.0-beta.16.patch +0 -0
- package/pnpm-workspace.yaml +4 -0
- package/releases/.gitkeep +0 -0
- package/releases/v0.0.1.md +36 -0
- package/releases/v0.2.7.md +15 -0
- package/releases/v0.3.0.md +54 -0
- package/releases/v0.3.1.md +55 -0
- package/releases/v2026.318.0.md +66 -0
- package/releases/v2026.325.0.md +78 -0
- package/report/2026-03-13-08-46-token-optimization-implementation.md +48 -0
- package/scripts/backup-db.sh +17 -0
- package/scripts/build-npm.sh +80 -0
- package/scripts/check-forbidden-tokens.mjs +115 -0
- package/scripts/clean-onboard-git.sh +14 -0
- package/scripts/clean-onboard-npm.sh +13 -0
- package/scripts/clean-onboard-ref.sh +86 -0
- package/scripts/create-github-release.sh +99 -0
- package/scripts/dev-runner-paths.mjs +38 -0
- package/scripts/dev-runner.mjs +606 -0
- package/scripts/docker-onboard-smoke.sh +306 -0
- package/scripts/ensure-plugin-build-deps.mjs +47 -0
- package/scripts/generate-company-assets.ts +365 -0
- package/scripts/generate-npm-package-json.mjs +113 -0
- package/scripts/generate-org-chart-images.ts +694 -0
- package/scripts/generate-org-chart-satori-comparison.ts +225 -0
- package/scripts/generate-ui-package-json.mjs +31 -0
- package/scripts/kill-dev.sh +71 -0
- package/scripts/migrate-inline-env-secrets.ts +126 -0
- package/scripts/prepare-server-ui-dist.sh +22 -0
- package/scripts/provision-worktree.sh +333 -0
- package/scripts/release-lib.sh +306 -0
- package/scripts/release-package-map.mjs +169 -0
- package/scripts/release.sh +312 -0
- package/scripts/rollback-latest.sh +111 -0
- package/scripts/smoke/openclaw-docker-ui.sh +329 -0
- package/scripts/smoke/openclaw-gateway-e2e.sh +954 -0
- package/scripts/smoke/openclaw-join.sh +295 -0
- package/scripts/smoke/openclaw-sse-standalone.sh +146 -0
- package/scripts/workspace-compat.mjs +60 -0
- package/server/CHANGELOG.md +130 -0
- package/server/package.json +96 -0
- package/server/scripts/copy-onboarding-assets.mjs +10 -0
- package/server/scripts/dev-watch.ts +33 -0
- package/server/src/__tests__/activity-routes.test.ts +70 -0
- package/server/src/__tests__/adapter-models.test.ts +105 -0
- package/server/src/__tests__/adapter-session-codecs.test.ts +194 -0
- package/server/src/__tests__/agent-auth-jwt.test.ts +79 -0
- package/server/src/__tests__/agent-instructions-routes.test.ts +318 -0
- package/server/src/__tests__/agent-instructions-service.test.ts +361 -0
- package/server/src/__tests__/agent-permissions-routes.test.ts +275 -0
- package/server/src/__tests__/agent-shortname-collision.test.ts +69 -0
- package/server/src/__tests__/agent-skill-contract.test.ts +50 -0
- package/server/src/__tests__/agent-skills-routes.test.ts +462 -0
- package/server/src/__tests__/app-hmr-port.test.ts +19 -0
- package/server/src/__tests__/approval-routes-idempotency.test.ts +110 -0
- package/server/src/__tests__/approvals-service.test.ts +107 -0
- package/server/src/__tests__/assets.test.ts +250 -0
- package/server/src/__tests__/attachment-types.test.ts +97 -0
- package/server/src/__tests__/board-mutation-guard.test.ts +105 -0
- package/server/src/__tests__/budgets-service.test.ts +311 -0
- package/server/src/__tests__/claude-local-adapter-environment.test.ts +92 -0
- package/server/src/__tests__/claude-local-adapter.test.ts +31 -0
- package/server/src/__tests__/claude-local-skill-sync.test.ts +111 -0
- package/server/src/__tests__/cli-auth-routes.test.ts +230 -0
- package/server/src/__tests__/codex-local-adapter-environment.test.ts +143 -0
- package/server/src/__tests__/codex-local-adapter.test.ts +253 -0
- package/server/src/__tests__/codex-local-execute.test.ts +391 -0
- package/server/src/__tests__/codex-local-skill-injection.test.ts +175 -0
- package/server/src/__tests__/codex-local-skill-sync.test.ts +123 -0
- package/server/src/__tests__/companies-route-path-guard.test.ts +56 -0
- package/server/src/__tests__/company-branding-route.test.ts +196 -0
- package/server/src/__tests__/company-portability-routes.test.ts +175 -0
- package/server/src/__tests__/company-portability.test.ts +2186 -0
- package/server/src/__tests__/company-skills-routes.test.ts +113 -0
- package/server/src/__tests__/company-skills.test.ts +229 -0
- package/server/src/__tests__/costs-service.test.ts +226 -0
- package/server/src/__tests__/cursor-local-adapter-environment.test.ts +196 -0
- package/server/src/__tests__/cursor-local-adapter.test.ts +406 -0
- package/server/src/__tests__/cursor-local-execute.test.ts +263 -0
- package/server/src/__tests__/cursor-local-skill-injection.test.ts +104 -0
- package/server/src/__tests__/cursor-local-skill-sync.test.ts +145 -0
- package/server/src/__tests__/dev-runner-paths.test.ts +25 -0
- package/server/src/__tests__/dev-server-status.test.ts +66 -0
- package/server/src/__tests__/dev-watch-ignore.test.ts +42 -0
- package/server/src/__tests__/documents.test.ts +29 -0
- package/server/src/__tests__/error-handler.test.ts +53 -0
- package/server/src/__tests__/execution-workspace-policy.test.ts +170 -0
- package/server/src/__tests__/forbidden-tokens.test.ts +77 -0
- package/server/src/__tests__/gemini-local-adapter-environment.test.ts +135 -0
- package/server/src/__tests__/gemini-local-adapter.test.ts +190 -0
- package/server/src/__tests__/gemini-local-execute.test.ts +172 -0
- package/server/src/__tests__/gemini-local-skill-sync.test.ts +90 -0
- package/server/src/__tests__/health.test.ts +16 -0
- package/server/src/__tests__/heartbeat-process-recovery.test.ts +256 -0
- package/server/src/__tests__/heartbeat-run-summary.test.ts +33 -0
- package/server/src/__tests__/heartbeat-workspace-session.test.ts +334 -0
- package/server/src/__tests__/helpers/embedded-postgres.ts +7 -0
- package/server/src/__tests__/hire-hook.test.ts +181 -0
- package/server/src/__tests__/instance-settings-routes.test.ts +156 -0
- package/server/src/__tests__/invite-accept-gateway-defaults.test.ts +119 -0
- package/server/src/__tests__/invite-accept-replay.test.ts +92 -0
- package/server/src/__tests__/invite-expiry.test.ts +10 -0
- package/server/src/__tests__/invite-join-grants.test.ts +57 -0
- package/server/src/__tests__/invite-join-manager.test.ts +33 -0
- package/server/src/__tests__/invite-onboarding-text.test.ts +116 -0
- package/server/src/__tests__/issue-comment-reopen-routes.test.ts +146 -0
- package/server/src/__tests__/issue-goal-fallback.test.ts +99 -0
- package/server/src/__tests__/issues-checkout-wakeup.test.ts +48 -0
- package/server/src/__tests__/issues-goal-context-routes.test.ts +187 -0
- package/server/src/__tests__/issues-service.test.ts +317 -0
- package/server/src/__tests__/issues-user-context.test.ts +113 -0
- package/server/src/__tests__/log-redaction.test.ts +74 -0
- package/server/src/__tests__/monthly-spend-service.test.ts +90 -0
- package/server/src/__tests__/normalize-agent-mention-token.test.ts +41 -0
- package/server/src/__tests__/openclaw-gateway-adapter.test.ts +626 -0
- package/server/src/__tests__/openclaw-invite-prompt-route.test.ts +192 -0
- package/server/src/__tests__/opencode-local-adapter-environment.test.ts +97 -0
- package/server/src/__tests__/opencode-local-adapter.test.ts +226 -0
- package/server/src/__tests__/opencode-local-skill-sync.test.ts +91 -0
- package/server/src/__tests__/paperclip-env.test.ts +58 -0
- package/server/src/__tests__/paperclip-skill-utils.test.ts +63 -0
- package/server/src/__tests__/pi-local-adapter-environment.test.ts +102 -0
- package/server/src/__tests__/pi-local-skill-sync.test.ts +95 -0
- package/server/src/__tests__/plugin-dev-watcher.test.ts +68 -0
- package/server/src/__tests__/plugin-worker-manager.test.ts +43 -0
- package/server/src/__tests__/private-hostname-guard.test.ts +56 -0
- package/server/src/__tests__/project-shortname-resolution.test.ts +45 -0
- package/server/src/__tests__/quota-windows-service.test.ts +56 -0
- package/server/src/__tests__/quota-windows.test.ts +1109 -0
- package/server/src/__tests__/redaction.test.ts +66 -0
- package/server/src/__tests__/routines-e2e.test.ts +276 -0
- package/server/src/__tests__/routines-routes.test.ts +271 -0
- package/server/src/__tests__/routines-service.test.ts +424 -0
- package/server/src/__tests__/storage-local-provider.test.ts +78 -0
- package/server/src/__tests__/ui-branding.test.ts +82 -0
- package/server/src/__tests__/work-products.test.ts +95 -0
- package/server/src/__tests__/workspace-runtime.test.ts +1131 -0
- package/server/src/__tests__/worktree-config.test.ts +426 -0
- package/server/src/adapters/codex-models.ts +105 -0
- package/server/src/adapters/cursor-models.ts +171 -0
- package/server/src/adapters/http/execute.ts +42 -0
- package/server/src/adapters/http/index.ts +21 -0
- package/server/src/adapters/http/test.ts +116 -0
- package/server/src/adapters/index.ts +18 -0
- package/server/src/adapters/process/execute.ts +77 -0
- package/server/src/adapters/process/index.ts +24 -0
- package/server/src/adapters/process/test.ts +89 -0
- package/server/src/adapters/registry.ts +225 -0
- package/server/src/adapters/server-utils-compat.ts +57 -0
- package/server/src/adapters/types.ts +30 -0
- package/server/src/adapters/utils.ts +48 -0
- package/server/src/agent-auth-jwt.ts +141 -0
- package/server/src/app.ts +321 -0
- package/server/src/attachment-types.ts +74 -0
- package/server/src/auth/better-auth.ts +148 -0
- package/server/src/board-claim.ts +150 -0
- package/server/src/config-file.ts +17 -0
- package/server/src/config.ts +260 -0
- package/server/src/dev-server-status.ts +103 -0
- package/server/src/dev-watch-ignore.ts +36 -0
- package/server/src/errors.ts +34 -0
- package/server/src/home-paths.ts +95 -0
- package/server/src/index.ts +799 -0
- package/server/src/log-redaction.ts +146 -0
- package/server/src/middleware/auth.ts +178 -0
- package/server/src/middleware/board-mutation-guard.ts +66 -0
- package/server/src/middleware/error-handler.ts +71 -0
- package/server/src/middleware/index.ts +3 -0
- package/server/src/middleware/logger.ts +90 -0
- package/server/src/middleware/private-hostname-guard.ts +92 -0
- package/server/src/middleware/validate.ts +9 -0
- package/server/src/onboarding-assets/ceo/AGENTS.md +54 -0
- package/server/src/onboarding-assets/ceo/HEARTBEAT.md +72 -0
- package/server/src/onboarding-assets/ceo/SOUL.md +33 -0
- package/server/src/onboarding-assets/ceo/TOOLS.md +3 -0
- package/server/src/onboarding-assets/default/AGENTS.md +3 -0
- package/server/src/paths.ts +34 -0
- package/server/src/realtime/live-events-ws.ts +274 -0
- package/server/src/redaction.ts +59 -0
- package/server/src/routes/access.ts +2888 -0
- package/server/src/routes/activity.ts +89 -0
- package/server/src/routes/agents.ts +2313 -0
- package/server/src/routes/approvals.ts +346 -0
- package/server/src/routes/assets.ts +341 -0
- package/server/src/routes/authz.ts +52 -0
- package/server/src/routes/companies.ts +343 -0
- package/server/src/routes/company-skills.ts +300 -0
- package/server/src/routes/costs.ts +335 -0
- package/server/src/routes/dashboard.ts +19 -0
- package/server/src/routes/execution-workspaces.ts +182 -0
- package/server/src/routes/goals.ts +107 -0
- package/server/src/routes/health.ts +94 -0
- package/server/src/routes/index.ts +17 -0
- package/server/src/routes/instance-settings.ts +95 -0
- package/server/src/routes/issues-checkout-wakeup.ts +14 -0
- package/server/src/routes/issues.ts +1680 -0
- package/server/src/routes/llms.ts +86 -0
- package/server/src/routes/org-chart-svg.ts +777 -0
- package/server/src/routes/plugin-ui-static.ts +497 -0
- package/server/src/routes/plugins.ts +2220 -0
- package/server/src/routes/projects.ts +295 -0
- package/server/src/routes/routines.ts +300 -0
- package/server/src/routes/secrets.ts +166 -0
- package/server/src/routes/sidebar-badges.ts +52 -0
- package/server/src/secrets/external-stub-providers.ts +32 -0
- package/server/src/secrets/local-encrypted-provider.ts +135 -0
- package/server/src/secrets/provider-registry.ts +31 -0
- package/server/src/secrets/types.ts +23 -0
- package/server/src/services/access.ts +381 -0
- package/server/src/services/activity-log.ts +95 -0
- package/server/src/services/activity.ts +164 -0
- package/server/src/services/agent-instructions.ts +735 -0
- package/server/src/services/agent-permissions.ts +27 -0
- package/server/src/services/agents.ts +694 -0
- package/server/src/services/approvals.ts +273 -0
- package/server/src/services/assets.ts +23 -0
- package/server/src/services/board-auth.ts +355 -0
- package/server/src/services/budgets.ts +959 -0
- package/server/src/services/companies.ts +313 -0
- package/server/src/services/company-export-readme.ts +173 -0
- package/server/src/services/company-portability.ts +4263 -0
- package/server/src/services/company-skills.ts +2356 -0
- package/server/src/services/costs.ts +365 -0
- package/server/src/services/cron.ts +373 -0
- package/server/src/services/dashboard.ts +110 -0
- package/server/src/services/default-agent-instructions.ts +27 -0
- package/server/src/services/documents.ts +434 -0
- package/server/src/services/execution-workspace-policy.ts +210 -0
- package/server/src/services/execution-workspaces.ts +100 -0
- package/server/src/services/finance.ts +135 -0
- package/server/src/services/goals.ts +81 -0
- package/server/src/services/heartbeat-run-summary.ts +35 -0
- package/server/src/services/heartbeat.ts +3863 -0
- package/server/src/services/hire-hook.ts +114 -0
- package/server/src/services/index.ts +32 -0
- package/server/src/services/instance-settings.ts +138 -0
- package/server/src/services/issue-approvals.ts +175 -0
- package/server/src/services/issue-assignment-wakeup.ts +48 -0
- package/server/src/services/issue-goal-fallback.ts +56 -0
- package/server/src/services/issues.ts +1828 -0
- package/server/src/services/live-events.ts +55 -0
- package/server/src/services/plugin-capability-validator.ts +450 -0
- package/server/src/services/plugin-config-validator.ts +55 -0
- package/server/src/services/plugin-dev-watcher.ts +339 -0
- package/server/src/services/plugin-event-bus.ts +413 -0
- package/server/src/services/plugin-host-service-cleanup.ts +59 -0
- package/server/src/services/plugin-host-services.ts +1132 -0
- package/server/src/services/plugin-job-coordinator.ts +261 -0
- package/server/src/services/plugin-job-scheduler.ts +753 -0
- package/server/src/services/plugin-job-store.ts +466 -0
- package/server/src/services/plugin-lifecycle.ts +822 -0
- package/server/src/services/plugin-loader.ts +1955 -0
- package/server/src/services/plugin-log-retention.ts +87 -0
- package/server/src/services/plugin-manifest-validator.ts +164 -0
- package/server/src/services/plugin-registry.ts +683 -0
- package/server/src/services/plugin-runtime-sandbox.ts +222 -0
- package/server/src/services/plugin-secrets-handler.ts +355 -0
- package/server/src/services/plugin-state-store.ts +238 -0
- package/server/src/services/plugin-stream-bus.ts +81 -0
- package/server/src/services/plugin-tool-dispatcher.ts +449 -0
- package/server/src/services/plugin-tool-registry.ts +450 -0
- package/server/src/services/plugin-worker-manager.ts +1343 -0
- package/server/src/services/projects.ts +860 -0
- package/server/src/services/quota-windows.ts +65 -0
- package/server/src/services/routines.ts +1269 -0
- package/server/src/services/run-log-store.ts +156 -0
- package/server/src/services/secrets.ts +370 -0
- package/server/src/services/sidebar-badges.ts +56 -0
- package/server/src/services/work-products.ts +124 -0
- package/server/src/services/workspace-operation-log-store.ts +156 -0
- package/server/src/services/workspace-operations.ts +262 -0
- package/server/src/services/workspace-runtime.ts +1565 -0
- package/server/src/startup-banner.ts +176 -0
- package/server/src/storage/index.ts +35 -0
- package/server/src/storage/local-disk-provider.ts +89 -0
- package/server/src/storage/provider-registry.ts +18 -0
- package/server/src/storage/s3-provider.ts +153 -0
- package/server/src/storage/service.ts +131 -0
- package/server/src/storage/types.ts +63 -0
- package/server/src/ui-branding.ts +217 -0
- package/server/src/version.ts +10 -0
- package/server/src/worktree-config.ts +468 -0
- package/server/tsconfig.json +9 -0
- package/server/vitest.config.ts +7 -0
- package/skills/paperclip/SKILL.md +365 -0
- package/skills/paperclip/references/api-reference.md +647 -0
- package/skills/paperclip/references/company-skills.md +193 -0
- package/skills/paperclip-create-agent/SKILL.md +142 -0
- package/skills/paperclip-create-agent/references/api-reference.md +105 -0
- package/skills/paperclip-create-plugin/SKILL.md +102 -0
- package/skills/para-memory-files/SKILL.md +104 -0
- package/skills/para-memory-files/references/schemas.md +35 -0
- package/tests/e2e/onboarding.spec.ts +142 -0
- package/tests/e2e/playwright.config.ts +35 -0
- package/tests/release-smoke/docker-auth-onboarding.spec.ts +141 -0
- package/tests/release-smoke/playwright.config.ts +28 -0
- package/tsconfig.base.json +18 -0
- package/tsconfig.json +18 -0
- package/ui/README.md +12 -0
- package/ui/components.json +21 -0
- package/ui/index.html +47 -0
- package/ui/package.json +73 -0
- package/ui/public/android-chrome-192x192.png +0 -0
- package/ui/public/android-chrome-512x512.png +0 -0
- package/ui/public/apple-touch-icon.png +0 -0
- package/ui/public/brands/opencode-logo-dark-square.svg +18 -0
- package/ui/public/brands/opencode-logo-light-square.svg +18 -0
- package/ui/public/favicon-16x16.png +0 -0
- package/ui/public/favicon-32x32.png +0 -0
- package/ui/public/favicon.ico +0 -0
- package/ui/public/favicon.svg +9 -0
- package/ui/public/site.webmanifest +30 -0
- package/ui/public/sprites/1-D-1.png +0 -0
- package/ui/public/sprites/1-D-2.png +0 -0
- package/ui/public/sprites/1-D-3.png +0 -0
- package/ui/public/sprites/1-L-1.png +0 -0
- package/ui/public/sprites/1-R-1.png +0 -0
- package/ui/public/sprites/10-D-1.png +0 -0
- package/ui/public/sprites/10-D-2.png +0 -0
- package/ui/public/sprites/10-D-3.png +0 -0
- package/ui/public/sprites/10-L-1.png +0 -0
- package/ui/public/sprites/10-R-1.png +0 -0
- package/ui/public/sprites/11-D-1.png +0 -0
- package/ui/public/sprites/11-D-2.png +0 -0
- package/ui/public/sprites/11-D-3.png +0 -0
- package/ui/public/sprites/11-L-1.png +0 -0
- package/ui/public/sprites/11-R-1.png +0 -0
- package/ui/public/sprites/12-D-1.png +0 -0
- package/ui/public/sprites/12-D-2.png +0 -0
- package/ui/public/sprites/12-D-3.png +0 -0
- package/ui/public/sprites/12-L-1.png +0 -0
- package/ui/public/sprites/12-R-1.png +0 -0
- package/ui/public/sprites/13-D-1.png +0 -0
- package/ui/public/sprites/13-D-2.png +0 -0
- package/ui/public/sprites/13-D-3.png +0 -0
- package/ui/public/sprites/13-L-1.png +0 -0
- package/ui/public/sprites/13-R-1.png +0 -0
- package/ui/public/sprites/14-D-1.png +0 -0
- package/ui/public/sprites/14-D-2.png +0 -0
- package/ui/public/sprites/14-D-3.png +0 -0
- package/ui/public/sprites/14-L-1.png +0 -0
- package/ui/public/sprites/14-R-1.png +0 -0
- package/ui/public/sprites/2-D-1.png +0 -0
- package/ui/public/sprites/2-D-2.png +0 -0
- package/ui/public/sprites/2-D-3.png +0 -0
- package/ui/public/sprites/2-L-1.png +0 -0
- package/ui/public/sprites/2-R-1.png +0 -0
- package/ui/public/sprites/3-D-1.png +0 -0
- package/ui/public/sprites/3-D-2.png +0 -0
- package/ui/public/sprites/3-D-3.png +0 -0
- package/ui/public/sprites/3-L-1.png +0 -0
- package/ui/public/sprites/3-R-1.png +0 -0
- package/ui/public/sprites/4-D-1.png +0 -0
- package/ui/public/sprites/4-D-2.png +0 -0
- package/ui/public/sprites/4-D-3.png +0 -0
- package/ui/public/sprites/4-L-1.png +0 -0
- package/ui/public/sprites/4-R-1.png +0 -0
- package/ui/public/sprites/5-D-1.png +0 -0
- package/ui/public/sprites/5-D-2.png +0 -0
- package/ui/public/sprites/5-D-3.png +0 -0
- package/ui/public/sprites/5-L-1.png +0 -0
- package/ui/public/sprites/5-R-1.png +0 -0
- package/ui/public/sprites/6-D-1.png +0 -0
- package/ui/public/sprites/6-D-2.png +0 -0
- package/ui/public/sprites/6-D-3.png +0 -0
- package/ui/public/sprites/6-L-1.png +0 -0
- package/ui/public/sprites/6-R-1.png +0 -0
- package/ui/public/sprites/7-D-1.png +0 -0
- package/ui/public/sprites/7-D-2.png +0 -0
- package/ui/public/sprites/7-D-3.png +0 -0
- package/ui/public/sprites/7-L-1.png +0 -0
- package/ui/public/sprites/7-R-1.png +0 -0
- package/ui/public/sprites/8-D-1.png +0 -0
- package/ui/public/sprites/8-D-2.png +0 -0
- package/ui/public/sprites/8-D-3.png +0 -0
- package/ui/public/sprites/8-L-1.png +0 -0
- package/ui/public/sprites/8-R-1.png +0 -0
- package/ui/public/sprites/9-D-1.png +0 -0
- package/ui/public/sprites/9-D-2.png +0 -0
- package/ui/public/sprites/9-D-3.png +0 -0
- package/ui/public/sprites/9-L-1.png +0 -0
- package/ui/public/sprites/9-R-1.png +0 -0
- package/ui/public/sprites/ceo-lobster.png +0 -0
- package/ui/public/sw.js +42 -0
- package/ui/public/worktree-favicon-16x16.png +0 -0
- package/ui/public/worktree-favicon-32x32.png +0 -0
- package/ui/public/worktree-favicon.ico +0 -0
- package/ui/public/worktree-favicon.svg +9 -0
- package/ui/src/App.tsx +354 -0
- package/ui/src/adapters/claude-local/config-fields.tsx +138 -0
- package/ui/src/adapters/claude-local/index.ts +13 -0
- package/ui/src/adapters/codex-local/config-fields.tsx +104 -0
- package/ui/src/adapters/codex-local/index.ts +13 -0
- package/ui/src/adapters/cursor/config-fields.tsx +49 -0
- package/ui/src/adapters/cursor/index.ts +13 -0
- package/ui/src/adapters/gemini-local/config-fields.tsx +51 -0
- package/ui/src/adapters/gemini-local/index.ts +13 -0
- package/ui/src/adapters/http/build-config.ts +9 -0
- package/ui/src/adapters/http/config-fields.tsx +38 -0
- package/ui/src/adapters/http/index.ts +12 -0
- package/ui/src/adapters/http/parse-stdout.ts +5 -0
- package/ui/src/adapters/index.ts +9 -0
- package/ui/src/adapters/local-workspace-runtime-fields.tsx +5 -0
- package/ui/src/adapters/openclaw-gateway/config-fields.tsx +237 -0
- package/ui/src/adapters/openclaw-gateway/index.ts +13 -0
- package/ui/src/adapters/opencode-local/config-fields.tsx +72 -0
- package/ui/src/adapters/opencode-local/index.ts +13 -0
- package/ui/src/adapters/pi-local/config-fields.tsx +49 -0
- package/ui/src/adapters/pi-local/index.ts +13 -0
- package/ui/src/adapters/process/build-config.ts +18 -0
- package/ui/src/adapters/process/config-fields.tsx +77 -0
- package/ui/src/adapters/process/index.ts +12 -0
- package/ui/src/adapters/process/parse-stdout.ts +5 -0
- package/ui/src/adapters/registry.ts +34 -0
- package/ui/src/adapters/runtime-json-fields.tsx +122 -0
- package/ui/src/adapters/transcript.test.ts +30 -0
- package/ui/src/adapters/transcript.ts +62 -0
- package/ui/src/adapters/types.ts +34 -0
- package/ui/src/api/access.ts +160 -0
- package/ui/src/api/activity.ts +37 -0
- package/ui/src/api/agents.ts +194 -0
- package/ui/src/api/approvals.ts +25 -0
- package/ui/src/api/assets.ts +30 -0
- package/ui/src/api/auth.ts +74 -0
- package/ui/src/api/budgets.ts +21 -0
- package/ui/src/api/client.ts +50 -0
- package/ui/src/api/companies.ts +59 -0
- package/ui/src/api/companySkills.ts +55 -0
- package/ui/src/api/costs.ts +60 -0
- package/ui/src/api/dashboard.ts +7 -0
- package/ui/src/api/execution-workspaces.ts +27 -0
- package/ui/src/api/goals.ts +12 -0
- package/ui/src/api/health.ts +41 -0
- package/ui/src/api/heartbeats.ts +62 -0
- package/ui/src/api/index.ts +18 -0
- package/ui/src/api/instanceSettings.ts +19 -0
- package/ui/src/api/issues.ts +115 -0
- package/ui/src/api/plugins.ts +424 -0
- package/ui/src/api/projects.ts +34 -0
- package/ui/src/api/routines.ts +59 -0
- package/ui/src/api/secrets.ts +26 -0
- package/ui/src/api/sidebarBadges.ts +7 -0
- package/ui/src/components/AccountingModelCard.tsx +69 -0
- package/ui/src/components/ActiveAgentsPanel.tsx +157 -0
- package/ui/src/components/ActivityCharts.tsx +264 -0
- package/ui/src/components/ActivityRow.tsx +147 -0
- package/ui/src/components/AgentActionButtons.tsx +51 -0
- package/ui/src/components/AgentConfigForm.tsx +1468 -0
- package/ui/src/components/AgentIconPicker.tsx +81 -0
- package/ui/src/components/AgentProperties.tsx +107 -0
- package/ui/src/components/ApprovalCard.tsx +107 -0
- package/ui/src/components/ApprovalPayload.tsx +134 -0
- package/ui/src/components/AsciiArtAnimation.tsx +355 -0
- package/ui/src/components/BillerSpendCard.tsx +146 -0
- package/ui/src/components/BreadcrumbBar.tsx +113 -0
- package/ui/src/components/BudgetIncidentCard.tsx +101 -0
- package/ui/src/components/BudgetPolicyCard.tsx +220 -0
- package/ui/src/components/BudgetSidebarMarker.tsx +13 -0
- package/ui/src/components/ClaudeSubscriptionPanel.tsx +141 -0
- package/ui/src/components/CodexSubscriptionPanel.tsx +158 -0
- package/ui/src/components/CommandPalette.tsx +239 -0
- package/ui/src/components/CommentThread.tsx +503 -0
- package/ui/src/components/CompanyPatternIcon.tsx +212 -0
- package/ui/src/components/CompanyRail.tsx +329 -0
- package/ui/src/components/CompanySwitcher.tsx +81 -0
- package/ui/src/components/CopyText.tsx +56 -0
- package/ui/src/components/DevRestartBanner.tsx +89 -0
- package/ui/src/components/EmptyState.tsx +27 -0
- package/ui/src/components/EntityRow.tsx +69 -0
- package/ui/src/components/FilterBar.tsx +39 -0
- package/ui/src/components/FinanceBillerCard.tsx +45 -0
- package/ui/src/components/FinanceKindCard.tsx +44 -0
- package/ui/src/components/FinanceTimelineCard.tsx +72 -0
- package/ui/src/components/GoalProperties.tsx +165 -0
- package/ui/src/components/GoalTree.tsx +118 -0
- package/ui/src/components/Identity.tsx +39 -0
- package/ui/src/components/InlineEditor.tsx +248 -0
- package/ui/src/components/InlineEntitySelector.tsx +206 -0
- package/ui/src/components/InstanceSidebar.tsx +53 -0
- package/ui/src/components/IssueDocumentsSection.tsx +892 -0
- package/ui/src/components/IssueProperties.tsx +621 -0
- package/ui/src/components/IssueRow.tsx +149 -0
- package/ui/src/components/IssueWorkspaceCard.tsx +404 -0
- package/ui/src/components/IssuesList.tsx +889 -0
- package/ui/src/components/JsonSchemaForm.tsx +1048 -0
- package/ui/src/components/KanbanBoard.tsx +275 -0
- package/ui/src/components/Layout.tsx +441 -0
- package/ui/src/components/LiveRunWidget.tsx +160 -0
- package/ui/src/components/MarkdownBody.test.tsx +50 -0
- package/ui/src/components/MarkdownBody.tsx +152 -0
- package/ui/src/components/MarkdownEditor.tsx +622 -0
- package/ui/src/components/MetricCard.tsx +53 -0
- package/ui/src/components/MobileBottomNav.tsx +123 -0
- package/ui/src/components/NewAgentDialog.tsx +223 -0
- package/ui/src/components/NewGoalDialog.tsx +283 -0
- package/ui/src/components/NewIssueDialog.tsx +1473 -0
- package/ui/src/components/NewProjectDialog.tsx +451 -0
- package/ui/src/components/OnboardingWizard.tsx +1392 -0
- package/ui/src/components/OpenCodeLogoIcon.tsx +22 -0
- package/ui/src/components/PackageFileTree.tsx +318 -0
- package/ui/src/components/PageSkeleton.tsx +180 -0
- package/ui/src/components/PageTabBar.tsx +45 -0
- package/ui/src/components/PathInstructionsModal.tsx +143 -0
- package/ui/src/components/PriorityIcon.tsx +77 -0
- package/ui/src/components/ProjectProperties.tsx +1127 -0
- package/ui/src/components/PropertiesPanel.tsx +29 -0
- package/ui/src/components/ProviderQuotaCard.tsx +417 -0
- package/ui/src/components/QuotaBar.tsx +65 -0
- package/ui/src/components/ReportsToPicker.tsx +127 -0
- package/ui/src/components/ScheduleEditor.tsx +344 -0
- package/ui/src/components/ScrollToBottom.tsx +79 -0
- package/ui/src/components/Sidebar.tsx +130 -0
- package/ui/src/components/SidebarAgents.tsx +146 -0
- package/ui/src/components/SidebarNavItem.tsx +92 -0
- package/ui/src/components/SidebarProjects.tsx +234 -0
- package/ui/src/components/SidebarSection.tsx +17 -0
- package/ui/src/components/StatusBadge.tsx +15 -0
- package/ui/src/components/StatusIcon.tsx +71 -0
- package/ui/src/components/SwipeToArchive.tsx +152 -0
- package/ui/src/components/ToastViewport.tsx +99 -0
- package/ui/src/components/WorktreeBanner.tsx +25 -0
- package/ui/src/components/agent-config-defaults.ts +31 -0
- package/ui/src/components/agent-config-primitives.tsx +476 -0
- package/ui/src/components/transcript/RunTranscriptView.test.tsx +84 -0
- package/ui/src/components/transcript/RunTranscriptView.tsx +1015 -0
- package/ui/src/components/transcript/useLiveRunTranscripts.ts +297 -0
- package/ui/src/components/ui/avatar.tsx +107 -0
- package/ui/src/components/ui/badge.tsx +48 -0
- package/ui/src/components/ui/breadcrumb.tsx +109 -0
- package/ui/src/components/ui/button.tsx +64 -0
- package/ui/src/components/ui/card.tsx +92 -0
- package/ui/src/components/ui/checkbox.tsx +32 -0
- package/ui/src/components/ui/collapsible.tsx +33 -0
- package/ui/src/components/ui/command.tsx +194 -0
- package/ui/src/components/ui/dialog.tsx +156 -0
- package/ui/src/components/ui/dropdown-menu.tsx +257 -0
- package/ui/src/components/ui/input.tsx +21 -0
- package/ui/src/components/ui/label.tsx +22 -0
- package/ui/src/components/ui/popover.tsx +88 -0
- package/ui/src/components/ui/scroll-area.tsx +56 -0
- package/ui/src/components/ui/select.tsx +188 -0
- package/ui/src/components/ui/separator.tsx +28 -0
- package/ui/src/components/ui/sheet.tsx +143 -0
- package/ui/src/components/ui/skeleton.tsx +13 -0
- package/ui/src/components/ui/tabs.tsx +89 -0
- package/ui/src/components/ui/textarea.tsx +18 -0
- package/ui/src/components/ui/tooltip.tsx +57 -0
- package/ui/src/components/visual-office/AgentAvatar.tsx +99 -0
- package/ui/src/components/visual-office/OfficeViewExact.tsx +417 -0
- package/ui/src/components/visual-office/i18n.ts +52 -0
- package/ui/src/components/visual-office/office-view/CliUsagePanel.tsx +240 -0
- package/ui/src/components/visual-office/office-view/VirtualPadOverlay.tsx +104 -0
- package/ui/src/components/visual-office/office-view/buildScene-break-room.ts +248 -0
- package/ui/src/components/visual-office/office-view/buildScene-ceo-hallway.ts +345 -0
- package/ui/src/components/visual-office/office-view/buildScene-department-agent.ts +242 -0
- package/ui/src/components/visual-office/office-view/buildScene-departments.ts +360 -0
- package/ui/src/components/visual-office/office-view/buildScene-final-layers.ts +113 -0
- package/ui/src/components/visual-office/office-view/buildScene-types.ts +91 -0
- package/ui/src/components/visual-office/office-view/buildScene.ts +232 -0
- package/ui/src/components/visual-office/office-view/drawing-core.ts +374 -0
- package/ui/src/components/visual-office/office-view/drawing-furniture-a.ts +338 -0
- package/ui/src/components/visual-office/office-view/drawing-furniture-b.ts +241 -0
- package/ui/src/components/visual-office/office-view/model.ts +301 -0
- package/ui/src/components/visual-office/office-view/officeTicker.ts +455 -0
- package/ui/src/components/visual-office/office-view/officeTickerRoomAndDelivery.ts +133 -0
- package/ui/src/components/visual-office/office-view/themes-locale.ts +460 -0
- package/ui/src/components/visual-office/office-view/useCliUsage.ts +37 -0
- package/ui/src/components/visual-office/office-view/useOfficeDeliveryEffects.ts +465 -0
- package/ui/src/components/visual-office/office-view/useOfficePixiRuntime.ts +282 -0
- package/ui/src/components/visual-office/types.ts +123 -0
- package/ui/src/context/BreadcrumbContext.tsx +44 -0
- package/ui/src/context/CompanyContext.tsx +151 -0
- package/ui/src/context/DialogContext.tsx +135 -0
- package/ui/src/context/LiveUpdatesProvider.test.ts +119 -0
- package/ui/src/context/LiveUpdatesProvider.tsx +760 -0
- package/ui/src/context/PanelContext.tsx +73 -0
- package/ui/src/context/SidebarContext.tsx +43 -0
- package/ui/src/context/ThemeContext.tsx +83 -0
- package/ui/src/context/ToastContext.tsx +172 -0
- package/ui/src/fixtures/runTranscriptFixtures.ts +226 -0
- package/ui/src/hooks/useAgentOrder.ts +105 -0
- package/ui/src/hooks/useAutosaveIndicator.ts +72 -0
- package/ui/src/hooks/useCompanyPageMemory.test.ts +90 -0
- package/ui/src/hooks/useCompanyPageMemory.ts +79 -0
- package/ui/src/hooks/useDateRange.ts +120 -0
- package/ui/src/hooks/useInboxBadge.ts +132 -0
- package/ui/src/hooks/useKeyboardShortcuts.ts +40 -0
- package/ui/src/hooks/useProjectOrder.ts +106 -0
- package/ui/src/index.css +770 -0
- package/ui/src/lib/agent-icons.ts +99 -0
- package/ui/src/lib/agent-order.ts +107 -0
- package/ui/src/lib/agent-skills-state.test.ts +90 -0
- package/ui/src/lib/agent-skills-state.ts +41 -0
- package/ui/src/lib/assignees.test.ts +92 -0
- package/ui/src/lib/assignees.ts +82 -0
- package/ui/src/lib/color-contrast.ts +107 -0
- package/ui/src/lib/company-export-selection.test.ts +41 -0
- package/ui/src/lib/company-export-selection.ts +57 -0
- package/ui/src/lib/company-page-memory.ts +65 -0
- package/ui/src/lib/company-portability-sidebar.test.ts +101 -0
- package/ui/src/lib/company-portability-sidebar.ts +62 -0
- package/ui/src/lib/company-routes.ts +88 -0
- package/ui/src/lib/company-selection.test.ts +34 -0
- package/ui/src/lib/company-selection.ts +18 -0
- package/ui/src/lib/groupBy.ts +11 -0
- package/ui/src/lib/inbox.test.ts +404 -0
- package/ui/src/lib/inbox.ts +292 -0
- package/ui/src/lib/instance-settings.test.ts +26 -0
- package/ui/src/lib/instance-settings.ts +25 -0
- package/ui/src/lib/issueDetailBreadcrumb.ts +24 -0
- package/ui/src/lib/legacy-agent-config.test.ts +40 -0
- package/ui/src/lib/legacy-agent-config.ts +17 -0
- package/ui/src/lib/mention-aware-link-node.test.ts +50 -0
- package/ui/src/lib/mention-aware-link-node.ts +67 -0
- package/ui/src/lib/mention-chips.ts +168 -0
- package/ui/src/lib/mention-deletion.test.ts +87 -0
- package/ui/src/lib/mention-deletion.ts +143 -0
- package/ui/src/lib/model-utils.ts +16 -0
- package/ui/src/lib/onboarding-goal.test.ts +22 -0
- package/ui/src/lib/onboarding-goal.ts +18 -0
- package/ui/src/lib/onboarding-launch.test.ts +131 -0
- package/ui/src/lib/onboarding-launch.ts +54 -0
- package/ui/src/lib/onboarding-route.test.ts +80 -0
- package/ui/src/lib/onboarding-route.ts +51 -0
- package/ui/src/lib/portable-files.ts +42 -0
- package/ui/src/lib/project-order.ts +71 -0
- package/ui/src/lib/queryKeys.ts +140 -0
- package/ui/src/lib/recent-assignees.ts +36 -0
- package/ui/src/lib/router.tsx +76 -0
- package/ui/src/lib/routine-trigger-patch.test.ts +72 -0
- package/ui/src/lib/routine-trigger-patch.ts +31 -0
- package/ui/src/lib/status-colors.ts +108 -0
- package/ui/src/lib/timeAgo.ts +31 -0
- package/ui/src/lib/utils.ts +168 -0
- package/ui/src/lib/worktree-branding.ts +65 -0
- package/ui/src/lib/zip.test.ts +289 -0
- package/ui/src/lib/zip.ts +284 -0
- package/ui/src/main.tsx +67 -0
- package/ui/src/pages/Activity.tsx +141 -0
- package/ui/src/pages/AgentDetail.tsx +4053 -0
- package/ui/src/pages/Agents.tsx +415 -0
- package/ui/src/pages/ApprovalDetail.tsx +369 -0
- package/ui/src/pages/Approvals.tsx +132 -0
- package/ui/src/pages/Auth.tsx +180 -0
- package/ui/src/pages/BoardClaim.tsx +125 -0
- package/ui/src/pages/CliAuth.tsx +184 -0
- package/ui/src/pages/Companies.tsx +297 -0
- package/ui/src/pages/CompanyExport.tsx +1019 -0
- package/ui/src/pages/CompanyImport.tsx +1355 -0
- package/ui/src/pages/CompanySettings.tsx +661 -0
- package/ui/src/pages/CompanySkills.tsx +1171 -0
- package/ui/src/pages/Costs.tsx +1103 -0
- package/ui/src/pages/Dashboard.tsx +388 -0
- package/ui/src/pages/DesignGuide.tsx +1330 -0
- package/ui/src/pages/ExecutionWorkspaceDetail.tsx +82 -0
- package/ui/src/pages/GoalDetail.tsx +197 -0
- package/ui/src/pages/Goals.tsx +63 -0
- package/ui/src/pages/Inbox.tsx +1291 -0
- package/ui/src/pages/InstanceExperimentalSettings.tsx +139 -0
- package/ui/src/pages/InstanceGeneralSettings.tsx +104 -0
- package/ui/src/pages/InstanceSettings.tsx +284 -0
- package/ui/src/pages/InviteLanding.tsx +320 -0
- package/ui/src/pages/IssueDetail.tsx +1201 -0
- package/ui/src/pages/Issues.tsx +116 -0
- package/ui/src/pages/MyIssues.tsx +72 -0
- package/ui/src/pages/NewAgent.tsx +353 -0
- package/ui/src/pages/NotFound.tsx +66 -0
- package/ui/src/pages/Org.tsx +132 -0
- package/ui/src/pages/OrgChart.tsx +447 -0
- package/ui/src/pages/PluginManager.tsx +510 -0
- package/ui/src/pages/PluginPage.tsx +156 -0
- package/ui/src/pages/PluginSettings.tsx +836 -0
- package/ui/src/pages/ProjectDetail.tsx +633 -0
- package/ui/src/pages/Projects.tsx +87 -0
- package/ui/src/pages/RoutineDetail.tsx +1022 -0
- package/ui/src/pages/Routines.tsx +661 -0
- package/ui/src/pages/RunTranscriptUxLab.tsx +334 -0
- package/ui/src/pages/VisualOffice.tsx +243 -0
- package/ui/src/plugins/bridge-init.ts +69 -0
- package/ui/src/plugins/bridge.ts +476 -0
- package/ui/src/plugins/launchers.tsx +834 -0
- package/ui/src/plugins/slots.tsx +855 -0
- package/ui/tsconfig.json +21 -0
- package/ui/vite.config.ts +23 -0
- package/ui/vitest.config.ts +14 -0
- package/vitest.config.ts +11 -0
|
@@ -0,0 +1,1565 @@
|
|
|
1
|
+
import { spawn, type ChildProcess } from "node:child_process";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import net from "node:net";
|
|
4
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
7
|
+
import type { AdapterRuntimeServiceReport } from "@corporateai/adapter-utils";
|
|
8
|
+
import type { Db } from "@corporateai/db";
|
|
9
|
+
import { workspaceRuntimeServices } from "@corporateai/db";
|
|
10
|
+
import { and, desc, eq, inArray } from "drizzle-orm";
|
|
11
|
+
import { asNumber, asString, parseObject, renderTemplate } from "../adapters/utils.js";
|
|
12
|
+
import { resolveHomeAwarePath } from "../home-paths.js";
|
|
13
|
+
import type { WorkspaceOperationRecorder } from "./workspace-operations.js";
|
|
14
|
+
|
|
15
|
+
export interface ExecutionWorkspaceInput {
|
|
16
|
+
baseCwd: string;
|
|
17
|
+
source: "project_primary" | "task_session" | "agent_home";
|
|
18
|
+
projectId: string | null;
|
|
19
|
+
workspaceId: string | null;
|
|
20
|
+
repoUrl: string | null;
|
|
21
|
+
repoRef: string | null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface ExecutionWorkspaceIssueRef {
|
|
25
|
+
id: string;
|
|
26
|
+
identifier: string | null;
|
|
27
|
+
title: string | null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface ExecutionWorkspaceAgentRef {
|
|
31
|
+
id: string;
|
|
32
|
+
name: string;
|
|
33
|
+
companyId: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface RealizedExecutionWorkspace extends ExecutionWorkspaceInput {
|
|
37
|
+
strategy: "project_primary" | "git_worktree";
|
|
38
|
+
cwd: string;
|
|
39
|
+
branchName: string | null;
|
|
40
|
+
worktreePath: string | null;
|
|
41
|
+
warnings: string[];
|
|
42
|
+
created: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface RuntimeServiceRef {
|
|
46
|
+
id: string;
|
|
47
|
+
companyId: string;
|
|
48
|
+
projectId: string | null;
|
|
49
|
+
projectWorkspaceId: string | null;
|
|
50
|
+
executionWorkspaceId: string | null;
|
|
51
|
+
issueId: string | null;
|
|
52
|
+
serviceName: string;
|
|
53
|
+
status: "starting" | "running" | "stopped" | "failed";
|
|
54
|
+
lifecycle: "shared" | "ephemeral";
|
|
55
|
+
scopeType: "project_workspace" | "execution_workspace" | "run" | "agent";
|
|
56
|
+
scopeId: string | null;
|
|
57
|
+
reuseKey: string | null;
|
|
58
|
+
command: string | null;
|
|
59
|
+
cwd: string | null;
|
|
60
|
+
port: number | null;
|
|
61
|
+
url: string | null;
|
|
62
|
+
provider: "local_process" | "adapter_managed";
|
|
63
|
+
providerRef: string | null;
|
|
64
|
+
ownerAgentId: string | null;
|
|
65
|
+
startedByRunId: string | null;
|
|
66
|
+
lastUsedAt: string;
|
|
67
|
+
startedAt: string;
|
|
68
|
+
stoppedAt: string | null;
|
|
69
|
+
stopPolicy: Record<string, unknown> | null;
|
|
70
|
+
healthStatus: "unknown" | "healthy" | "unhealthy";
|
|
71
|
+
reused: boolean;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface RuntimeServiceRecord extends RuntimeServiceRef {
|
|
75
|
+
db?: Db;
|
|
76
|
+
child: ChildProcess | null;
|
|
77
|
+
leaseRunIds: Set<string>;
|
|
78
|
+
idleTimer: ReturnType<typeof globalThis.setTimeout> | null;
|
|
79
|
+
envFingerprint: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const runtimeServicesById = new Map<string, RuntimeServiceRecord>();
|
|
83
|
+
const runtimeServicesByReuseKey = new Map<string, string>();
|
|
84
|
+
const runtimeServiceLeasesByRun = new Map<string, string[]>();
|
|
85
|
+
|
|
86
|
+
function stableStringify(value: unknown): string {
|
|
87
|
+
if (Array.isArray(value)) {
|
|
88
|
+
return `[${value.map((entry) => stableStringify(entry)).join(",")}]`;
|
|
89
|
+
}
|
|
90
|
+
if (value && typeof value === "object") {
|
|
91
|
+
const rec = value as Record<string, unknown>;
|
|
92
|
+
return `{${Object.keys(rec).sort().map((key) => `${JSON.stringify(key)}:${stableStringify(rec[key])}`).join(",")}}`;
|
|
93
|
+
}
|
|
94
|
+
return JSON.stringify(value);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function sanitizeRuntimeServiceBaseEnv(baseEnv: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
|
|
98
|
+
const env: NodeJS.ProcessEnv = { ...baseEnv };
|
|
99
|
+
for (const key of Object.keys(env)) {
|
|
100
|
+
if (key.startsWith("PAPERCLIP_")) {
|
|
101
|
+
delete env[key];
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
delete env.DATABASE_URL;
|
|
105
|
+
return env;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function stableRuntimeServiceId(input: {
|
|
109
|
+
adapterType: string;
|
|
110
|
+
runId: string;
|
|
111
|
+
scopeType: RuntimeServiceRef["scopeType"];
|
|
112
|
+
scopeId: string | null;
|
|
113
|
+
serviceName: string;
|
|
114
|
+
reportId: string | null;
|
|
115
|
+
providerRef: string | null;
|
|
116
|
+
reuseKey: string | null;
|
|
117
|
+
}) {
|
|
118
|
+
if (input.reportId) return input.reportId;
|
|
119
|
+
const digest = createHash("sha256")
|
|
120
|
+
.update(
|
|
121
|
+
stableStringify({
|
|
122
|
+
adapterType: input.adapterType,
|
|
123
|
+
runId: input.runId,
|
|
124
|
+
scopeType: input.scopeType,
|
|
125
|
+
scopeId: input.scopeId,
|
|
126
|
+
serviceName: input.serviceName,
|
|
127
|
+
providerRef: input.providerRef,
|
|
128
|
+
reuseKey: input.reuseKey,
|
|
129
|
+
}),
|
|
130
|
+
)
|
|
131
|
+
.digest("hex")
|
|
132
|
+
.slice(0, 32);
|
|
133
|
+
return `${input.adapterType}-${digest}`;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function toRuntimeServiceRef(record: RuntimeServiceRecord, overrides?: Partial<RuntimeServiceRef>): RuntimeServiceRef {
|
|
137
|
+
return {
|
|
138
|
+
id: record.id,
|
|
139
|
+
companyId: record.companyId,
|
|
140
|
+
projectId: record.projectId,
|
|
141
|
+
projectWorkspaceId: record.projectWorkspaceId,
|
|
142
|
+
executionWorkspaceId: record.executionWorkspaceId,
|
|
143
|
+
issueId: record.issueId,
|
|
144
|
+
serviceName: record.serviceName,
|
|
145
|
+
status: record.status,
|
|
146
|
+
lifecycle: record.lifecycle,
|
|
147
|
+
scopeType: record.scopeType,
|
|
148
|
+
scopeId: record.scopeId,
|
|
149
|
+
reuseKey: record.reuseKey,
|
|
150
|
+
command: record.command,
|
|
151
|
+
cwd: record.cwd,
|
|
152
|
+
port: record.port,
|
|
153
|
+
url: record.url,
|
|
154
|
+
provider: record.provider,
|
|
155
|
+
providerRef: record.providerRef,
|
|
156
|
+
ownerAgentId: record.ownerAgentId,
|
|
157
|
+
startedByRunId: record.startedByRunId,
|
|
158
|
+
lastUsedAt: record.lastUsedAt,
|
|
159
|
+
startedAt: record.startedAt,
|
|
160
|
+
stoppedAt: record.stoppedAt,
|
|
161
|
+
stopPolicy: record.stopPolicy,
|
|
162
|
+
healthStatus: record.healthStatus,
|
|
163
|
+
reused: record.reused,
|
|
164
|
+
...overrides,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function sanitizeSlugPart(value: string | null | undefined, fallback: string): string {
|
|
169
|
+
const raw = (value ?? "").trim().toLowerCase();
|
|
170
|
+
const normalized = raw
|
|
171
|
+
.replace(/[^a-z0-9/_-]+/g, "-")
|
|
172
|
+
.replace(/-+/g, "-")
|
|
173
|
+
.replace(/^[-/]+|[-/]+$/g, "");
|
|
174
|
+
return normalized.length > 0 ? normalized : fallback;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function renderWorkspaceTemplate(template: string, input: {
|
|
178
|
+
issue: ExecutionWorkspaceIssueRef | null;
|
|
179
|
+
agent: ExecutionWorkspaceAgentRef;
|
|
180
|
+
projectId: string | null;
|
|
181
|
+
repoRef: string | null;
|
|
182
|
+
}) {
|
|
183
|
+
const issueIdentifier = input.issue?.identifier ?? input.issue?.id ?? "issue";
|
|
184
|
+
const slug = sanitizeSlugPart(input.issue?.title, sanitizeSlugPart(issueIdentifier, "issue"));
|
|
185
|
+
return renderTemplate(template, {
|
|
186
|
+
issue: {
|
|
187
|
+
id: input.issue?.id ?? "",
|
|
188
|
+
identifier: input.issue?.identifier ?? "",
|
|
189
|
+
title: input.issue?.title ?? "",
|
|
190
|
+
},
|
|
191
|
+
agent: {
|
|
192
|
+
id: input.agent.id,
|
|
193
|
+
name: input.agent.name,
|
|
194
|
+
},
|
|
195
|
+
project: {
|
|
196
|
+
id: input.projectId ?? "",
|
|
197
|
+
},
|
|
198
|
+
workspace: {
|
|
199
|
+
repoRef: input.repoRef ?? "",
|
|
200
|
+
},
|
|
201
|
+
slug,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function sanitizeBranchName(value: string): string {
|
|
206
|
+
return value
|
|
207
|
+
.trim()
|
|
208
|
+
.replace(/[^A-Za-z0-9._/-]+/g, "-")
|
|
209
|
+
.replace(/-+/g, "-")
|
|
210
|
+
.replace(/^[-/.]+|[-/.]+$/g, "")
|
|
211
|
+
.slice(0, 120) || "paperclip-work";
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function isAbsolutePath(value: string) {
|
|
215
|
+
return path.isAbsolute(value) || value.startsWith("~");
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function resolveConfiguredPath(value: string, baseDir: string): string {
|
|
219
|
+
if (isAbsolutePath(value)) {
|
|
220
|
+
return resolveHomeAwarePath(value);
|
|
221
|
+
}
|
|
222
|
+
return path.resolve(baseDir, value);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function formatCommandForDisplay(command: string, args: string[]) {
|
|
226
|
+
return [command, ...args]
|
|
227
|
+
.map((part) => (/^[A-Za-z0-9_./:-]+$/.test(part) ? part : JSON.stringify(part)))
|
|
228
|
+
.join(" ");
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
async function executeProcess(input: {
|
|
232
|
+
command: string;
|
|
233
|
+
args: string[];
|
|
234
|
+
cwd: string;
|
|
235
|
+
env?: NodeJS.ProcessEnv;
|
|
236
|
+
}): Promise<{ stdout: string; stderr: string; code: number | null }> {
|
|
237
|
+
const proc = await new Promise<{ stdout: string; stderr: string; code: number | null }>((resolve, reject) => {
|
|
238
|
+
const child = spawn(input.command, input.args, {
|
|
239
|
+
cwd: input.cwd,
|
|
240
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
241
|
+
env: input.env ?? process.env,
|
|
242
|
+
});
|
|
243
|
+
let stdout = "";
|
|
244
|
+
let stderr = "";
|
|
245
|
+
child.stdout?.on("data", (chunk) => {
|
|
246
|
+
stdout += String(chunk);
|
|
247
|
+
});
|
|
248
|
+
child.stderr?.on("data", (chunk) => {
|
|
249
|
+
stderr += String(chunk);
|
|
250
|
+
});
|
|
251
|
+
child.on("error", reject);
|
|
252
|
+
child.on("close", (code) => resolve({ stdout, stderr, code }));
|
|
253
|
+
});
|
|
254
|
+
return proc;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
async function runGit(args: string[], cwd: string): Promise<string> {
|
|
258
|
+
const proc = await executeProcess({
|
|
259
|
+
command: "git",
|
|
260
|
+
args,
|
|
261
|
+
cwd,
|
|
262
|
+
});
|
|
263
|
+
if (proc.code !== 0) {
|
|
264
|
+
throw new Error(proc.stderr.trim() || proc.stdout.trim() || `git ${args.join(" ")} failed`);
|
|
265
|
+
}
|
|
266
|
+
return proc.stdout.trim();
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function gitErrorIncludes(error: unknown, needle: string) {
|
|
270
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
271
|
+
return message.toLowerCase().includes(needle.toLowerCase());
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
async function directoryExists(value: string) {
|
|
275
|
+
return fs.stat(value).then((stats) => stats.isDirectory()).catch(() => false);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function terminateChildProcess(child: ChildProcess) {
|
|
279
|
+
if (!child.pid) return;
|
|
280
|
+
if (process.platform !== "win32") {
|
|
281
|
+
try {
|
|
282
|
+
process.kill(-child.pid, "SIGTERM");
|
|
283
|
+
return;
|
|
284
|
+
} catch {
|
|
285
|
+
// Fall through to the direct child kill.
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
if (!child.killed) {
|
|
289
|
+
child.kill("SIGTERM");
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function buildWorkspaceCommandEnv(input: {
|
|
294
|
+
base: ExecutionWorkspaceInput;
|
|
295
|
+
repoRoot: string;
|
|
296
|
+
worktreePath: string;
|
|
297
|
+
branchName: string;
|
|
298
|
+
issue: ExecutionWorkspaceIssueRef | null;
|
|
299
|
+
agent: ExecutionWorkspaceAgentRef;
|
|
300
|
+
created: boolean;
|
|
301
|
+
}) {
|
|
302
|
+
const env: NodeJS.ProcessEnv = { ...process.env };
|
|
303
|
+
env.PAPERCLIP_WORKSPACE_CWD = input.worktreePath;
|
|
304
|
+
env.PAPERCLIP_WORKSPACE_PATH = input.worktreePath;
|
|
305
|
+
env.PAPERCLIP_WORKSPACE_WORKTREE_PATH = input.worktreePath;
|
|
306
|
+
env.PAPERCLIP_WORKSPACE_BRANCH = input.branchName;
|
|
307
|
+
env.PAPERCLIP_WORKSPACE_BASE_CWD = input.base.baseCwd;
|
|
308
|
+
env.PAPERCLIP_WORKSPACE_REPO_ROOT = input.repoRoot;
|
|
309
|
+
env.PAPERCLIP_WORKSPACE_SOURCE = input.base.source;
|
|
310
|
+
env.PAPERCLIP_WORKSPACE_REPO_REF = input.base.repoRef ?? "";
|
|
311
|
+
env.PAPERCLIP_WORKSPACE_REPO_URL = input.base.repoUrl ?? "";
|
|
312
|
+
env.PAPERCLIP_WORKSPACE_CREATED = input.created ? "true" : "false";
|
|
313
|
+
env.PAPERCLIP_PROJECT_ID = input.base.projectId ?? "";
|
|
314
|
+
env.PAPERCLIP_PROJECT_WORKSPACE_ID = input.base.workspaceId ?? "";
|
|
315
|
+
env.PAPERCLIP_AGENT_ID = input.agent.id;
|
|
316
|
+
env.PAPERCLIP_AGENT_NAME = input.agent.name;
|
|
317
|
+
env.PAPERCLIP_COMPANY_ID = input.agent.companyId;
|
|
318
|
+
env.PAPERCLIP_ISSUE_ID = input.issue?.id ?? "";
|
|
319
|
+
env.PAPERCLIP_ISSUE_IDENTIFIER = input.issue?.identifier ?? "";
|
|
320
|
+
env.PAPERCLIP_ISSUE_TITLE = input.issue?.title ?? "";
|
|
321
|
+
return env;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
async function runWorkspaceCommand(input: {
|
|
325
|
+
command: string;
|
|
326
|
+
cwd: string;
|
|
327
|
+
env: NodeJS.ProcessEnv;
|
|
328
|
+
label: string;
|
|
329
|
+
}) {
|
|
330
|
+
const shell = process.env.SHELL?.trim() || "/bin/sh";
|
|
331
|
+
const proc = await executeProcess({
|
|
332
|
+
command: shell,
|
|
333
|
+
args: ["-c", input.command],
|
|
334
|
+
cwd: input.cwd,
|
|
335
|
+
env: input.env,
|
|
336
|
+
});
|
|
337
|
+
if (proc.code === 0) return;
|
|
338
|
+
|
|
339
|
+
const details = [proc.stderr.trim(), proc.stdout.trim()].filter(Boolean).join("\n");
|
|
340
|
+
throw new Error(
|
|
341
|
+
details.length > 0
|
|
342
|
+
? `${input.label} failed: ${details}`
|
|
343
|
+
: `${input.label} failed with exit code ${proc.code ?? -1}`,
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
async function recordGitOperation(
|
|
348
|
+
recorder: WorkspaceOperationRecorder | null | undefined,
|
|
349
|
+
input: {
|
|
350
|
+
phase: "worktree_prepare" | "worktree_cleanup";
|
|
351
|
+
args: string[];
|
|
352
|
+
cwd: string;
|
|
353
|
+
metadata?: Record<string, unknown> | null;
|
|
354
|
+
successMessage?: string | null;
|
|
355
|
+
failureLabel?: string | null;
|
|
356
|
+
},
|
|
357
|
+
): Promise<string> {
|
|
358
|
+
if (!recorder) {
|
|
359
|
+
return runGit(input.args, input.cwd);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
let stdout = "";
|
|
363
|
+
let stderr = "";
|
|
364
|
+
let code: number | null = null;
|
|
365
|
+
await recorder.recordOperation({
|
|
366
|
+
phase: input.phase,
|
|
367
|
+
command: formatCommandForDisplay("git", input.args),
|
|
368
|
+
cwd: input.cwd,
|
|
369
|
+
metadata: input.metadata ?? null,
|
|
370
|
+
run: async () => {
|
|
371
|
+
const result = await executeProcess({
|
|
372
|
+
command: "git",
|
|
373
|
+
args: input.args,
|
|
374
|
+
cwd: input.cwd,
|
|
375
|
+
});
|
|
376
|
+
stdout = result.stdout;
|
|
377
|
+
stderr = result.stderr;
|
|
378
|
+
code = result.code;
|
|
379
|
+
return {
|
|
380
|
+
status: result.code === 0 ? "succeeded" : "failed",
|
|
381
|
+
exitCode: result.code,
|
|
382
|
+
stdout: result.stdout,
|
|
383
|
+
stderr: result.stderr,
|
|
384
|
+
system: result.code === 0 ? input.successMessage ?? null : null,
|
|
385
|
+
};
|
|
386
|
+
},
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
if (code !== 0) {
|
|
390
|
+
const details = [stderr.trim(), stdout.trim()].filter(Boolean).join("\n");
|
|
391
|
+
throw new Error(
|
|
392
|
+
details.length > 0
|
|
393
|
+
? `${input.failureLabel ?? `git ${input.args.join(" ")}`} failed: ${details}`
|
|
394
|
+
: `${input.failureLabel ?? `git ${input.args.join(" ")}`} failed with exit code ${code ?? -1}`,
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
return stdout.trim();
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
async function recordWorkspaceCommandOperation(
|
|
401
|
+
recorder: WorkspaceOperationRecorder | null | undefined,
|
|
402
|
+
input: {
|
|
403
|
+
phase: "workspace_provision" | "workspace_teardown";
|
|
404
|
+
command: string;
|
|
405
|
+
cwd: string;
|
|
406
|
+
env: NodeJS.ProcessEnv;
|
|
407
|
+
label: string;
|
|
408
|
+
metadata?: Record<string, unknown> | null;
|
|
409
|
+
successMessage?: string | null;
|
|
410
|
+
},
|
|
411
|
+
) {
|
|
412
|
+
if (!recorder) {
|
|
413
|
+
await runWorkspaceCommand(input);
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
let stdout = "";
|
|
418
|
+
let stderr = "";
|
|
419
|
+
let code: number | null = null;
|
|
420
|
+
await recorder.recordOperation({
|
|
421
|
+
phase: input.phase,
|
|
422
|
+
command: input.command,
|
|
423
|
+
cwd: input.cwd,
|
|
424
|
+
metadata: input.metadata ?? null,
|
|
425
|
+
run: async () => {
|
|
426
|
+
const shell = process.env.SHELL?.trim() || "/bin/sh";
|
|
427
|
+
const result = await executeProcess({
|
|
428
|
+
command: shell,
|
|
429
|
+
args: ["-c", input.command],
|
|
430
|
+
cwd: input.cwd,
|
|
431
|
+
env: input.env,
|
|
432
|
+
});
|
|
433
|
+
stdout = result.stdout;
|
|
434
|
+
stderr = result.stderr;
|
|
435
|
+
code = result.code;
|
|
436
|
+
return {
|
|
437
|
+
status: result.code === 0 ? "succeeded" : "failed",
|
|
438
|
+
exitCode: result.code,
|
|
439
|
+
stdout: result.stdout,
|
|
440
|
+
stderr: result.stderr,
|
|
441
|
+
system: result.code === 0 ? input.successMessage ?? null : null,
|
|
442
|
+
};
|
|
443
|
+
},
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
if (code === 0) return;
|
|
447
|
+
|
|
448
|
+
const details = [stderr.trim(), stdout.trim()].filter(Boolean).join("\n");
|
|
449
|
+
throw new Error(
|
|
450
|
+
details.length > 0
|
|
451
|
+
? `${input.label} failed: ${details}`
|
|
452
|
+
: `${input.label} failed with exit code ${code ?? -1}`,
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
async function provisionExecutionWorktree(input: {
|
|
457
|
+
strategy: Record<string, unknown>;
|
|
458
|
+
base: ExecutionWorkspaceInput;
|
|
459
|
+
repoRoot: string;
|
|
460
|
+
worktreePath: string;
|
|
461
|
+
branchName: string;
|
|
462
|
+
issue: ExecutionWorkspaceIssueRef | null;
|
|
463
|
+
agent: ExecutionWorkspaceAgentRef;
|
|
464
|
+
created: boolean;
|
|
465
|
+
recorder?: WorkspaceOperationRecorder | null;
|
|
466
|
+
}) {
|
|
467
|
+
const provisionCommand = asString(input.strategy.provisionCommand, "").trim();
|
|
468
|
+
if (!provisionCommand) return;
|
|
469
|
+
|
|
470
|
+
await recordWorkspaceCommandOperation(input.recorder, {
|
|
471
|
+
phase: "workspace_provision",
|
|
472
|
+
command: provisionCommand,
|
|
473
|
+
cwd: input.worktreePath,
|
|
474
|
+
env: buildWorkspaceCommandEnv({
|
|
475
|
+
base: input.base,
|
|
476
|
+
repoRoot: input.repoRoot,
|
|
477
|
+
worktreePath: input.worktreePath,
|
|
478
|
+
branchName: input.branchName,
|
|
479
|
+
issue: input.issue,
|
|
480
|
+
agent: input.agent,
|
|
481
|
+
created: input.created,
|
|
482
|
+
}),
|
|
483
|
+
label: `Execution workspace provision command "${provisionCommand}"`,
|
|
484
|
+
metadata: {
|
|
485
|
+
repoRoot: input.repoRoot,
|
|
486
|
+
worktreePath: input.worktreePath,
|
|
487
|
+
branchName: input.branchName,
|
|
488
|
+
created: input.created,
|
|
489
|
+
},
|
|
490
|
+
successMessage: `Provisioned workspace at ${input.worktreePath}\n`,
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function buildExecutionWorkspaceCleanupEnv(input: {
|
|
495
|
+
workspace: {
|
|
496
|
+
cwd: string | null;
|
|
497
|
+
providerRef: string | null;
|
|
498
|
+
branchName: string | null;
|
|
499
|
+
repoUrl: string | null;
|
|
500
|
+
baseRef: string | null;
|
|
501
|
+
projectId: string | null;
|
|
502
|
+
projectWorkspaceId: string | null;
|
|
503
|
+
sourceIssueId: string | null;
|
|
504
|
+
};
|
|
505
|
+
projectWorkspaceCwd?: string | null;
|
|
506
|
+
}) {
|
|
507
|
+
const env: NodeJS.ProcessEnv = sanitizeRuntimeServiceBaseEnv(process.env);
|
|
508
|
+
env.PAPERCLIP_WORKSPACE_CWD = input.workspace.cwd ?? "";
|
|
509
|
+
env.PAPERCLIP_WORKSPACE_PATH = input.workspace.cwd ?? "";
|
|
510
|
+
env.PAPERCLIP_WORKSPACE_WORKTREE_PATH =
|
|
511
|
+
input.workspace.providerRef ?? input.workspace.cwd ?? "";
|
|
512
|
+
env.PAPERCLIP_WORKSPACE_BRANCH = input.workspace.branchName ?? "";
|
|
513
|
+
env.PAPERCLIP_WORKSPACE_BASE_CWD = input.projectWorkspaceCwd ?? "";
|
|
514
|
+
env.PAPERCLIP_WORKSPACE_REPO_ROOT = input.projectWorkspaceCwd ?? "";
|
|
515
|
+
env.PAPERCLIP_WORKSPACE_REPO_URL = input.workspace.repoUrl ?? "";
|
|
516
|
+
env.PAPERCLIP_WORKSPACE_REPO_REF = input.workspace.baseRef ?? "";
|
|
517
|
+
env.PAPERCLIP_PROJECT_ID = input.workspace.projectId ?? "";
|
|
518
|
+
env.PAPERCLIP_PROJECT_WORKSPACE_ID = input.workspace.projectWorkspaceId ?? "";
|
|
519
|
+
env.PAPERCLIP_ISSUE_ID = input.workspace.sourceIssueId ?? "";
|
|
520
|
+
return env;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
async function resolveGitRepoRootForWorkspaceCleanup(
|
|
524
|
+
worktreePath: string,
|
|
525
|
+
projectWorkspaceCwd: string | null,
|
|
526
|
+
): Promise<string | null> {
|
|
527
|
+
if (projectWorkspaceCwd) {
|
|
528
|
+
const resolvedProjectWorkspaceCwd = path.resolve(projectWorkspaceCwd);
|
|
529
|
+
const gitDir = await runGit(["rev-parse", "--git-common-dir"], resolvedProjectWorkspaceCwd)
|
|
530
|
+
.catch(() => null);
|
|
531
|
+
if (gitDir) {
|
|
532
|
+
const resolvedGitDir = path.resolve(resolvedProjectWorkspaceCwd, gitDir);
|
|
533
|
+
return path.dirname(resolvedGitDir);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
const gitDir = await runGit(["rev-parse", "--git-common-dir"], worktreePath).catch(() => null);
|
|
538
|
+
if (!gitDir) return null;
|
|
539
|
+
const resolvedGitDir = path.resolve(worktreePath, gitDir);
|
|
540
|
+
return path.dirname(resolvedGitDir);
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
export async function realizeExecutionWorkspace(input: {
|
|
544
|
+
base: ExecutionWorkspaceInput;
|
|
545
|
+
config: Record<string, unknown>;
|
|
546
|
+
issue: ExecutionWorkspaceIssueRef | null;
|
|
547
|
+
agent: ExecutionWorkspaceAgentRef;
|
|
548
|
+
recorder?: WorkspaceOperationRecorder | null;
|
|
549
|
+
}): Promise<RealizedExecutionWorkspace> {
|
|
550
|
+
const rawStrategy = parseObject(input.config.workspaceStrategy);
|
|
551
|
+
const strategyType = asString(rawStrategy.type, "project_primary");
|
|
552
|
+
if (strategyType !== "git_worktree") {
|
|
553
|
+
return {
|
|
554
|
+
...input.base,
|
|
555
|
+
strategy: "project_primary",
|
|
556
|
+
cwd: input.base.baseCwd,
|
|
557
|
+
branchName: null,
|
|
558
|
+
worktreePath: null,
|
|
559
|
+
warnings: [],
|
|
560
|
+
created: false,
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
const repoRoot = await runGit(["rev-parse", "--show-toplevel"], input.base.baseCwd);
|
|
565
|
+
const branchTemplate = asString(rawStrategy.branchTemplate, "{{issue.identifier}}-{{slug}}");
|
|
566
|
+
const renderedBranch = renderWorkspaceTemplate(branchTemplate, {
|
|
567
|
+
issue: input.issue,
|
|
568
|
+
agent: input.agent,
|
|
569
|
+
projectId: input.base.projectId,
|
|
570
|
+
repoRef: input.base.repoRef,
|
|
571
|
+
});
|
|
572
|
+
const branchName = sanitizeBranchName(renderedBranch);
|
|
573
|
+
const configuredParentDir = asString(rawStrategy.worktreeParentDir, "");
|
|
574
|
+
const worktreeParentDir = configuredParentDir
|
|
575
|
+
? resolveConfiguredPath(configuredParentDir, repoRoot)
|
|
576
|
+
: path.join(repoRoot, ".paperclip", "worktrees");
|
|
577
|
+
const worktreePath = path.join(worktreeParentDir, branchName);
|
|
578
|
+
const baseRef = asString(rawStrategy.baseRef, input.base.repoRef ?? "HEAD");
|
|
579
|
+
|
|
580
|
+
await fs.mkdir(worktreeParentDir, { recursive: true });
|
|
581
|
+
|
|
582
|
+
const existingWorktree = await directoryExists(worktreePath);
|
|
583
|
+
if (existingWorktree) {
|
|
584
|
+
const existingGitDir = await runGit(["rev-parse", "--git-dir"], worktreePath).catch(() => null);
|
|
585
|
+
if (existingGitDir) {
|
|
586
|
+
if (input.recorder) {
|
|
587
|
+
await input.recorder.recordOperation({
|
|
588
|
+
phase: "worktree_prepare",
|
|
589
|
+
cwd: repoRoot,
|
|
590
|
+
metadata: {
|
|
591
|
+
repoRoot,
|
|
592
|
+
worktreePath,
|
|
593
|
+
branchName,
|
|
594
|
+
baseRef,
|
|
595
|
+
created: false,
|
|
596
|
+
reused: true,
|
|
597
|
+
},
|
|
598
|
+
run: async () => ({
|
|
599
|
+
status: "succeeded",
|
|
600
|
+
exitCode: 0,
|
|
601
|
+
system: `Reused existing git worktree at ${worktreePath}\n`,
|
|
602
|
+
}),
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
await provisionExecutionWorktree({
|
|
606
|
+
strategy: rawStrategy,
|
|
607
|
+
base: input.base,
|
|
608
|
+
repoRoot,
|
|
609
|
+
worktreePath,
|
|
610
|
+
branchName,
|
|
611
|
+
issue: input.issue,
|
|
612
|
+
agent: input.agent,
|
|
613
|
+
created: false,
|
|
614
|
+
recorder: input.recorder ?? null,
|
|
615
|
+
});
|
|
616
|
+
return {
|
|
617
|
+
...input.base,
|
|
618
|
+
strategy: "git_worktree",
|
|
619
|
+
cwd: worktreePath,
|
|
620
|
+
branchName,
|
|
621
|
+
worktreePath,
|
|
622
|
+
warnings: [],
|
|
623
|
+
created: false,
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
throw new Error(`Configured worktree path "${worktreePath}" already exists and is not a git worktree.`);
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
try {
|
|
630
|
+
await recordGitOperation(input.recorder, {
|
|
631
|
+
phase: "worktree_prepare",
|
|
632
|
+
args: ["worktree", "add", "-b", branchName, worktreePath, baseRef],
|
|
633
|
+
cwd: repoRoot,
|
|
634
|
+
metadata: {
|
|
635
|
+
repoRoot,
|
|
636
|
+
worktreePath,
|
|
637
|
+
branchName,
|
|
638
|
+
baseRef,
|
|
639
|
+
created: true,
|
|
640
|
+
},
|
|
641
|
+
successMessage: `Created git worktree at ${worktreePath}\n`,
|
|
642
|
+
failureLabel: `git worktree add ${worktreePath}`,
|
|
643
|
+
});
|
|
644
|
+
} catch (error) {
|
|
645
|
+
if (!gitErrorIncludes(error, "already exists")) {
|
|
646
|
+
throw error;
|
|
647
|
+
}
|
|
648
|
+
await recordGitOperation(input.recorder, {
|
|
649
|
+
phase: "worktree_prepare",
|
|
650
|
+
args: ["worktree", "add", worktreePath, branchName],
|
|
651
|
+
cwd: repoRoot,
|
|
652
|
+
metadata: {
|
|
653
|
+
repoRoot,
|
|
654
|
+
worktreePath,
|
|
655
|
+
branchName,
|
|
656
|
+
baseRef,
|
|
657
|
+
created: false,
|
|
658
|
+
reusedExistingBranch: true,
|
|
659
|
+
},
|
|
660
|
+
successMessage: `Attached existing branch ${branchName} at ${worktreePath}\n`,
|
|
661
|
+
failureLabel: `git worktree add ${worktreePath}`,
|
|
662
|
+
});
|
|
663
|
+
}
|
|
664
|
+
await provisionExecutionWorktree({
|
|
665
|
+
strategy: rawStrategy,
|
|
666
|
+
base: input.base,
|
|
667
|
+
repoRoot,
|
|
668
|
+
worktreePath,
|
|
669
|
+
branchName,
|
|
670
|
+
issue: input.issue,
|
|
671
|
+
agent: input.agent,
|
|
672
|
+
created: true,
|
|
673
|
+
recorder: input.recorder ?? null,
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
return {
|
|
677
|
+
...input.base,
|
|
678
|
+
strategy: "git_worktree",
|
|
679
|
+
cwd: worktreePath,
|
|
680
|
+
branchName,
|
|
681
|
+
worktreePath,
|
|
682
|
+
warnings: [],
|
|
683
|
+
created: true,
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
export async function cleanupExecutionWorkspaceArtifacts(input: {
|
|
688
|
+
workspace: {
|
|
689
|
+
id: string;
|
|
690
|
+
cwd: string | null;
|
|
691
|
+
providerType: string;
|
|
692
|
+
providerRef: string | null;
|
|
693
|
+
branchName: string | null;
|
|
694
|
+
repoUrl: string | null;
|
|
695
|
+
baseRef: string | null;
|
|
696
|
+
projectId: string | null;
|
|
697
|
+
projectWorkspaceId: string | null;
|
|
698
|
+
sourceIssueId: string | null;
|
|
699
|
+
metadata?: Record<string, unknown> | null;
|
|
700
|
+
};
|
|
701
|
+
projectWorkspace?: {
|
|
702
|
+
cwd: string | null;
|
|
703
|
+
cleanupCommand: string | null;
|
|
704
|
+
} | null;
|
|
705
|
+
teardownCommand?: string | null;
|
|
706
|
+
recorder?: WorkspaceOperationRecorder | null;
|
|
707
|
+
}) {
|
|
708
|
+
const warnings: string[] = [];
|
|
709
|
+
const workspacePath = input.workspace.providerRef ?? input.workspace.cwd;
|
|
710
|
+
const cleanupEnv = buildExecutionWorkspaceCleanupEnv({
|
|
711
|
+
workspace: input.workspace,
|
|
712
|
+
projectWorkspaceCwd: input.projectWorkspace?.cwd ?? null,
|
|
713
|
+
});
|
|
714
|
+
const createdByRuntime = input.workspace.metadata?.createdByRuntime === true;
|
|
715
|
+
const cleanupCommands = [
|
|
716
|
+
input.projectWorkspace?.cleanupCommand ?? null,
|
|
717
|
+
input.teardownCommand ?? null,
|
|
718
|
+
]
|
|
719
|
+
.map((value) => asString(value, "").trim())
|
|
720
|
+
.filter(Boolean);
|
|
721
|
+
|
|
722
|
+
for (const command of cleanupCommands) {
|
|
723
|
+
try {
|
|
724
|
+
await recordWorkspaceCommandOperation(input.recorder, {
|
|
725
|
+
phase: "workspace_teardown",
|
|
726
|
+
command,
|
|
727
|
+
cwd: workspacePath ?? input.projectWorkspace?.cwd ?? process.cwd(),
|
|
728
|
+
env: cleanupEnv,
|
|
729
|
+
label: `Execution workspace cleanup command "${command}"`,
|
|
730
|
+
metadata: {
|
|
731
|
+
workspaceId: input.workspace.id,
|
|
732
|
+
workspacePath,
|
|
733
|
+
branchName: input.workspace.branchName,
|
|
734
|
+
providerType: input.workspace.providerType,
|
|
735
|
+
},
|
|
736
|
+
successMessage: `Completed cleanup command "${command}"\n`,
|
|
737
|
+
});
|
|
738
|
+
} catch (err) {
|
|
739
|
+
warnings.push(err instanceof Error ? err.message : String(err));
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
if (input.workspace.providerType === "git_worktree" && workspacePath) {
|
|
744
|
+
const repoRoot = await resolveGitRepoRootForWorkspaceCleanup(
|
|
745
|
+
workspacePath,
|
|
746
|
+
input.projectWorkspace?.cwd ?? null,
|
|
747
|
+
);
|
|
748
|
+
const worktreeExists = await directoryExists(workspacePath);
|
|
749
|
+
if (worktreeExists) {
|
|
750
|
+
if (!repoRoot) {
|
|
751
|
+
warnings.push(`Could not resolve git repo root for "${workspacePath}".`);
|
|
752
|
+
} else {
|
|
753
|
+
try {
|
|
754
|
+
await recordGitOperation(input.recorder, {
|
|
755
|
+
phase: "worktree_cleanup",
|
|
756
|
+
args: ["worktree", "remove", "--force", workspacePath],
|
|
757
|
+
cwd: repoRoot,
|
|
758
|
+
metadata: {
|
|
759
|
+
workspaceId: input.workspace.id,
|
|
760
|
+
workspacePath,
|
|
761
|
+
branchName: input.workspace.branchName,
|
|
762
|
+
cleanupAction: "worktree_remove",
|
|
763
|
+
},
|
|
764
|
+
successMessage: `Removed git worktree ${workspacePath}\n`,
|
|
765
|
+
failureLabel: `git worktree remove ${workspacePath}`,
|
|
766
|
+
});
|
|
767
|
+
} catch (err) {
|
|
768
|
+
warnings.push(err instanceof Error ? err.message : String(err));
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
if (createdByRuntime && input.workspace.branchName) {
|
|
773
|
+
if (!repoRoot) {
|
|
774
|
+
warnings.push(`Could not resolve git repo root to delete branch "${input.workspace.branchName}".`);
|
|
775
|
+
} else {
|
|
776
|
+
try {
|
|
777
|
+
await recordGitOperation(input.recorder, {
|
|
778
|
+
phase: "worktree_cleanup",
|
|
779
|
+
args: ["branch", "-d", input.workspace.branchName],
|
|
780
|
+
cwd: repoRoot,
|
|
781
|
+
metadata: {
|
|
782
|
+
workspaceId: input.workspace.id,
|
|
783
|
+
workspacePath,
|
|
784
|
+
branchName: input.workspace.branchName,
|
|
785
|
+
cleanupAction: "branch_delete",
|
|
786
|
+
},
|
|
787
|
+
successMessage: `Deleted branch ${input.workspace.branchName}\n`,
|
|
788
|
+
failureLabel: `git branch -d ${input.workspace.branchName}`,
|
|
789
|
+
});
|
|
790
|
+
} catch (err) {
|
|
791
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
792
|
+
warnings.push(`Skipped deleting branch "${input.workspace.branchName}": ${message}`);
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
} else if (input.workspace.providerType === "local_fs" && createdByRuntime && workspacePath) {
|
|
797
|
+
const projectWorkspaceCwd = input.projectWorkspace?.cwd ? path.resolve(input.projectWorkspace.cwd) : null;
|
|
798
|
+
const resolvedWorkspacePath = path.resolve(workspacePath);
|
|
799
|
+
const containsProjectWorkspace = projectWorkspaceCwd
|
|
800
|
+
? (
|
|
801
|
+
resolvedWorkspacePath === projectWorkspaceCwd ||
|
|
802
|
+
projectWorkspaceCwd.startsWith(`${resolvedWorkspacePath}${path.sep}`)
|
|
803
|
+
)
|
|
804
|
+
: false;
|
|
805
|
+
if (containsProjectWorkspace) {
|
|
806
|
+
warnings.push(`Refusing to remove path "${workspacePath}" because it contains the project workspace.`);
|
|
807
|
+
} else {
|
|
808
|
+
await fs.rm(resolvedWorkspacePath, { recursive: true, force: true });
|
|
809
|
+
if (input.recorder) {
|
|
810
|
+
await input.recorder.recordOperation({
|
|
811
|
+
phase: "workspace_teardown",
|
|
812
|
+
cwd: projectWorkspaceCwd ?? process.cwd(),
|
|
813
|
+
metadata: {
|
|
814
|
+
workspaceId: input.workspace.id,
|
|
815
|
+
workspacePath: resolvedWorkspacePath,
|
|
816
|
+
cleanupAction: "remove_local_fs",
|
|
817
|
+
},
|
|
818
|
+
run: async () => ({
|
|
819
|
+
status: "succeeded",
|
|
820
|
+
exitCode: 0,
|
|
821
|
+
system: `Removed local workspace directory ${resolvedWorkspacePath}\n`,
|
|
822
|
+
}),
|
|
823
|
+
});
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
const cleaned =
|
|
829
|
+
!workspacePath ||
|
|
830
|
+
!(await directoryExists(workspacePath));
|
|
831
|
+
|
|
832
|
+
return {
|
|
833
|
+
cleanedPath: workspacePath,
|
|
834
|
+
cleaned,
|
|
835
|
+
warnings,
|
|
836
|
+
};
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
async function allocatePort(): Promise<number> {
|
|
840
|
+
return await new Promise<number>((resolve, reject) => {
|
|
841
|
+
const server = net.createServer();
|
|
842
|
+
server.listen(0, "127.0.0.1", () => {
|
|
843
|
+
const address = server.address();
|
|
844
|
+
server.close((err) => {
|
|
845
|
+
if (err) {
|
|
846
|
+
reject(err);
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
if (!address || typeof address === "string") {
|
|
850
|
+
reject(new Error("Failed to allocate port"));
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
resolve(address.port);
|
|
854
|
+
});
|
|
855
|
+
});
|
|
856
|
+
server.on("error", reject);
|
|
857
|
+
});
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
function buildTemplateData(input: {
|
|
861
|
+
workspace: RealizedExecutionWorkspace;
|
|
862
|
+
agent: ExecutionWorkspaceAgentRef;
|
|
863
|
+
issue: ExecutionWorkspaceIssueRef | null;
|
|
864
|
+
adapterEnv: Record<string, string>;
|
|
865
|
+
port: number | null;
|
|
866
|
+
}) {
|
|
867
|
+
return {
|
|
868
|
+
workspace: {
|
|
869
|
+
cwd: input.workspace.cwd,
|
|
870
|
+
branchName: input.workspace.branchName ?? "",
|
|
871
|
+
worktreePath: input.workspace.worktreePath ?? "",
|
|
872
|
+
repoUrl: input.workspace.repoUrl ?? "",
|
|
873
|
+
repoRef: input.workspace.repoRef ?? "",
|
|
874
|
+
env: input.adapterEnv,
|
|
875
|
+
},
|
|
876
|
+
issue: {
|
|
877
|
+
id: input.issue?.id ?? "",
|
|
878
|
+
identifier: input.issue?.identifier ?? "",
|
|
879
|
+
title: input.issue?.title ?? "",
|
|
880
|
+
},
|
|
881
|
+
agent: {
|
|
882
|
+
id: input.agent.id,
|
|
883
|
+
name: input.agent.name,
|
|
884
|
+
},
|
|
885
|
+
port: input.port ?? "",
|
|
886
|
+
};
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
function resolveServiceScopeId(input: {
|
|
890
|
+
service: Record<string, unknown>;
|
|
891
|
+
workspace: RealizedExecutionWorkspace;
|
|
892
|
+
executionWorkspaceId?: string | null;
|
|
893
|
+
issue: ExecutionWorkspaceIssueRef | null;
|
|
894
|
+
runId: string;
|
|
895
|
+
agent: ExecutionWorkspaceAgentRef;
|
|
896
|
+
}): {
|
|
897
|
+
scopeType: "project_workspace" | "execution_workspace" | "run" | "agent";
|
|
898
|
+
scopeId: string | null;
|
|
899
|
+
} {
|
|
900
|
+
const scopeTypeRaw = asString(input.service.reuseScope, input.service.lifecycle === "shared" ? "project_workspace" : "run");
|
|
901
|
+
const scopeType =
|
|
902
|
+
scopeTypeRaw === "project_workspace" ||
|
|
903
|
+
scopeTypeRaw === "execution_workspace" ||
|
|
904
|
+
scopeTypeRaw === "agent"
|
|
905
|
+
? scopeTypeRaw
|
|
906
|
+
: "run";
|
|
907
|
+
if (scopeType === "project_workspace") return { scopeType, scopeId: input.workspace.workspaceId ?? input.workspace.projectId };
|
|
908
|
+
if (scopeType === "execution_workspace") {
|
|
909
|
+
return { scopeType, scopeId: input.executionWorkspaceId ?? input.workspace.cwd };
|
|
910
|
+
}
|
|
911
|
+
if (scopeType === "agent") return { scopeType, scopeId: input.agent.id };
|
|
912
|
+
return { scopeType: "run" as const, scopeId: input.runId };
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
async function waitForReadiness(input: {
|
|
916
|
+
service: Record<string, unknown>;
|
|
917
|
+
url: string | null;
|
|
918
|
+
}) {
|
|
919
|
+
const readiness = parseObject(input.service.readiness);
|
|
920
|
+
const readinessType = asString(readiness.type, "");
|
|
921
|
+
if (readinessType !== "http" || !input.url) return;
|
|
922
|
+
const timeoutSec = Math.max(1, asNumber(readiness.timeoutSec, 30));
|
|
923
|
+
const intervalMs = Math.max(100, asNumber(readiness.intervalMs, 500));
|
|
924
|
+
const deadline = Date.now() + timeoutSec * 1000;
|
|
925
|
+
let lastError = "service did not become ready";
|
|
926
|
+
while (Date.now() < deadline) {
|
|
927
|
+
try {
|
|
928
|
+
const response = await fetch(input.url);
|
|
929
|
+
if (response.ok) return;
|
|
930
|
+
lastError = `received HTTP ${response.status}`;
|
|
931
|
+
} catch (err) {
|
|
932
|
+
lastError = err instanceof Error ? err.message : String(err);
|
|
933
|
+
}
|
|
934
|
+
await delay(intervalMs);
|
|
935
|
+
}
|
|
936
|
+
throw new Error(`Readiness check failed for ${input.url}: ${lastError}`);
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
function toPersistedWorkspaceRuntimeService(record: RuntimeServiceRecord): typeof workspaceRuntimeServices.$inferInsert {
|
|
940
|
+
return {
|
|
941
|
+
id: record.id,
|
|
942
|
+
companyId: record.companyId,
|
|
943
|
+
projectId: record.projectId,
|
|
944
|
+
projectWorkspaceId: record.projectWorkspaceId,
|
|
945
|
+
executionWorkspaceId: record.executionWorkspaceId,
|
|
946
|
+
issueId: record.issueId,
|
|
947
|
+
scopeType: record.scopeType,
|
|
948
|
+
scopeId: record.scopeId,
|
|
949
|
+
serviceName: record.serviceName,
|
|
950
|
+
status: record.status,
|
|
951
|
+
lifecycle: record.lifecycle,
|
|
952
|
+
reuseKey: record.reuseKey,
|
|
953
|
+
command: record.command,
|
|
954
|
+
cwd: record.cwd,
|
|
955
|
+
port: record.port,
|
|
956
|
+
url: record.url,
|
|
957
|
+
provider: record.provider,
|
|
958
|
+
providerRef: record.providerRef,
|
|
959
|
+
ownerAgentId: record.ownerAgentId,
|
|
960
|
+
startedByRunId: record.startedByRunId,
|
|
961
|
+
lastUsedAt: new Date(record.lastUsedAt),
|
|
962
|
+
startedAt: new Date(record.startedAt),
|
|
963
|
+
stoppedAt: record.stoppedAt ? new Date(record.stoppedAt) : null,
|
|
964
|
+
stopPolicy: record.stopPolicy,
|
|
965
|
+
healthStatus: record.healthStatus,
|
|
966
|
+
updatedAt: new Date(),
|
|
967
|
+
};
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
async function persistRuntimeServiceRecord(db: Db | undefined, record: RuntimeServiceRecord) {
|
|
971
|
+
if (!db) return;
|
|
972
|
+
const values = toPersistedWorkspaceRuntimeService(record);
|
|
973
|
+
await db
|
|
974
|
+
.insert(workspaceRuntimeServices)
|
|
975
|
+
.values(values)
|
|
976
|
+
.onConflictDoUpdate({
|
|
977
|
+
target: workspaceRuntimeServices.id,
|
|
978
|
+
set: {
|
|
979
|
+
projectId: values.projectId,
|
|
980
|
+
projectWorkspaceId: values.projectWorkspaceId,
|
|
981
|
+
executionWorkspaceId: values.executionWorkspaceId,
|
|
982
|
+
issueId: values.issueId,
|
|
983
|
+
scopeType: values.scopeType,
|
|
984
|
+
scopeId: values.scopeId,
|
|
985
|
+
serviceName: values.serviceName,
|
|
986
|
+
status: values.status,
|
|
987
|
+
lifecycle: values.lifecycle,
|
|
988
|
+
reuseKey: values.reuseKey,
|
|
989
|
+
command: values.command,
|
|
990
|
+
cwd: values.cwd,
|
|
991
|
+
port: values.port,
|
|
992
|
+
url: values.url,
|
|
993
|
+
provider: values.provider,
|
|
994
|
+
providerRef: values.providerRef,
|
|
995
|
+
ownerAgentId: values.ownerAgentId,
|
|
996
|
+
startedByRunId: values.startedByRunId,
|
|
997
|
+
lastUsedAt: values.lastUsedAt,
|
|
998
|
+
startedAt: values.startedAt,
|
|
999
|
+
stoppedAt: values.stoppedAt,
|
|
1000
|
+
stopPolicy: values.stopPolicy,
|
|
1001
|
+
healthStatus: values.healthStatus,
|
|
1002
|
+
updatedAt: values.updatedAt,
|
|
1003
|
+
},
|
|
1004
|
+
});
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
function clearIdleTimer(record: RuntimeServiceRecord) {
|
|
1008
|
+
if (!record.idleTimer) return;
|
|
1009
|
+
clearTimeout(record.idleTimer);
|
|
1010
|
+
record.idleTimer = null;
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
export function normalizeAdapterManagedRuntimeServices(input: {
|
|
1014
|
+
adapterType: string;
|
|
1015
|
+
runId: string;
|
|
1016
|
+
agent: ExecutionWorkspaceAgentRef;
|
|
1017
|
+
issue: ExecutionWorkspaceIssueRef | null;
|
|
1018
|
+
workspace: RealizedExecutionWorkspace;
|
|
1019
|
+
executionWorkspaceId?: string | null;
|
|
1020
|
+
reports: AdapterRuntimeServiceReport[];
|
|
1021
|
+
now?: Date;
|
|
1022
|
+
}): RuntimeServiceRef[] {
|
|
1023
|
+
const nowIso = (input.now ?? new Date()).toISOString();
|
|
1024
|
+
return input.reports.map((report) => {
|
|
1025
|
+
const scopeType = report.scopeType ?? "run";
|
|
1026
|
+
const scopeId =
|
|
1027
|
+
report.scopeId ??
|
|
1028
|
+
(scopeType === "project_workspace"
|
|
1029
|
+
? input.workspace.workspaceId
|
|
1030
|
+
: scopeType === "execution_workspace"
|
|
1031
|
+
? input.executionWorkspaceId ?? input.workspace.cwd
|
|
1032
|
+
: scopeType === "agent"
|
|
1033
|
+
? input.agent.id
|
|
1034
|
+
: input.runId) ??
|
|
1035
|
+
null;
|
|
1036
|
+
const serviceName = asString(report.serviceName, "").trim() || "service";
|
|
1037
|
+
const status = report.status ?? "running";
|
|
1038
|
+
const lifecycle = report.lifecycle ?? "ephemeral";
|
|
1039
|
+
const healthStatus =
|
|
1040
|
+
report.healthStatus ??
|
|
1041
|
+
(status === "running" ? "healthy" : status === "failed" ? "unhealthy" : "unknown");
|
|
1042
|
+
return {
|
|
1043
|
+
id: stableRuntimeServiceId({
|
|
1044
|
+
adapterType: input.adapterType,
|
|
1045
|
+
runId: input.runId,
|
|
1046
|
+
scopeType,
|
|
1047
|
+
scopeId,
|
|
1048
|
+
serviceName,
|
|
1049
|
+
reportId: report.id ?? null,
|
|
1050
|
+
providerRef: report.providerRef ?? null,
|
|
1051
|
+
reuseKey: report.reuseKey ?? null,
|
|
1052
|
+
}),
|
|
1053
|
+
companyId: input.agent.companyId,
|
|
1054
|
+
projectId: report.projectId ?? input.workspace.projectId,
|
|
1055
|
+
projectWorkspaceId: report.projectWorkspaceId ?? input.workspace.workspaceId,
|
|
1056
|
+
executionWorkspaceId: input.executionWorkspaceId ?? null,
|
|
1057
|
+
issueId: report.issueId ?? input.issue?.id ?? null,
|
|
1058
|
+
serviceName,
|
|
1059
|
+
status,
|
|
1060
|
+
lifecycle,
|
|
1061
|
+
scopeType,
|
|
1062
|
+
scopeId,
|
|
1063
|
+
reuseKey: report.reuseKey ?? null,
|
|
1064
|
+
command: report.command ?? null,
|
|
1065
|
+
cwd: report.cwd ?? null,
|
|
1066
|
+
port: report.port ?? null,
|
|
1067
|
+
url: report.url ?? null,
|
|
1068
|
+
provider: "adapter_managed",
|
|
1069
|
+
providerRef: report.providerRef ?? null,
|
|
1070
|
+
ownerAgentId: report.ownerAgentId ?? input.agent.id,
|
|
1071
|
+
startedByRunId: input.runId,
|
|
1072
|
+
lastUsedAt: nowIso,
|
|
1073
|
+
startedAt: nowIso,
|
|
1074
|
+
stoppedAt: status === "running" || status === "starting" ? null : nowIso,
|
|
1075
|
+
stopPolicy: report.stopPolicy ?? null,
|
|
1076
|
+
healthStatus,
|
|
1077
|
+
reused: false,
|
|
1078
|
+
};
|
|
1079
|
+
});
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
async function startLocalRuntimeService(input: {
|
|
1083
|
+
db?: Db;
|
|
1084
|
+
runId: string;
|
|
1085
|
+
agent: ExecutionWorkspaceAgentRef;
|
|
1086
|
+
issue: ExecutionWorkspaceIssueRef | null;
|
|
1087
|
+
workspace: RealizedExecutionWorkspace;
|
|
1088
|
+
executionWorkspaceId?: string | null;
|
|
1089
|
+
adapterEnv: Record<string, string>;
|
|
1090
|
+
service: Record<string, unknown>;
|
|
1091
|
+
onLog?: (stream: "stdout" | "stderr", chunk: string) => Promise<void>;
|
|
1092
|
+
reuseKey: string | null;
|
|
1093
|
+
scopeType: "project_workspace" | "execution_workspace" | "run" | "agent";
|
|
1094
|
+
scopeId: string | null;
|
|
1095
|
+
}): Promise<RuntimeServiceRecord> {
|
|
1096
|
+
const serviceName = asString(input.service.name, "service");
|
|
1097
|
+
const lifecycle = asString(input.service.lifecycle, "shared") === "ephemeral" ? "ephemeral" : "shared";
|
|
1098
|
+
const command = asString(input.service.command, "");
|
|
1099
|
+
if (!command) throw new Error(`Runtime service "${serviceName}" is missing command`);
|
|
1100
|
+
const serviceCwdTemplate = asString(input.service.cwd, ".");
|
|
1101
|
+
const portConfig = parseObject(input.service.port);
|
|
1102
|
+
const port = asString(portConfig.type, "") === "auto" ? await allocatePort() : null;
|
|
1103
|
+
const envConfig = parseObject(input.service.env);
|
|
1104
|
+
const templateData = buildTemplateData({
|
|
1105
|
+
workspace: input.workspace,
|
|
1106
|
+
agent: input.agent,
|
|
1107
|
+
issue: input.issue,
|
|
1108
|
+
adapterEnv: input.adapterEnv,
|
|
1109
|
+
port,
|
|
1110
|
+
});
|
|
1111
|
+
const serviceCwd = resolveConfiguredPath(renderTemplate(serviceCwdTemplate, templateData), input.workspace.cwd);
|
|
1112
|
+
const env: Record<string, string> = {
|
|
1113
|
+
...sanitizeRuntimeServiceBaseEnv(process.env),
|
|
1114
|
+
...input.adapterEnv,
|
|
1115
|
+
} as Record<string, string>;
|
|
1116
|
+
for (const [key, value] of Object.entries(envConfig)) {
|
|
1117
|
+
if (typeof value === "string") {
|
|
1118
|
+
env[key] = renderTemplate(value, templateData);
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
if (port) {
|
|
1122
|
+
const portEnvKey = asString(portConfig.envKey, "PORT");
|
|
1123
|
+
env[portEnvKey] = String(port);
|
|
1124
|
+
}
|
|
1125
|
+
const shell = process.env.SHELL?.trim() || "/bin/sh";
|
|
1126
|
+
const child = spawn(shell, ["-lc", command], {
|
|
1127
|
+
cwd: serviceCwd,
|
|
1128
|
+
env,
|
|
1129
|
+
detached: process.platform !== "win32",
|
|
1130
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
1131
|
+
});
|
|
1132
|
+
let stderrExcerpt = "";
|
|
1133
|
+
let stdoutExcerpt = "";
|
|
1134
|
+
child.stdout?.on("data", async (chunk) => {
|
|
1135
|
+
const text = String(chunk);
|
|
1136
|
+
stdoutExcerpt = (stdoutExcerpt + text).slice(-4096);
|
|
1137
|
+
if (input.onLog) await input.onLog("stdout", `[service:${serviceName}] ${text}`);
|
|
1138
|
+
});
|
|
1139
|
+
child.stderr?.on("data", async (chunk) => {
|
|
1140
|
+
const text = String(chunk);
|
|
1141
|
+
stderrExcerpt = (stderrExcerpt + text).slice(-4096);
|
|
1142
|
+
if (input.onLog) await input.onLog("stderr", `[service:${serviceName}] ${text}`);
|
|
1143
|
+
});
|
|
1144
|
+
|
|
1145
|
+
const expose = parseObject(input.service.expose);
|
|
1146
|
+
const readiness = parseObject(input.service.readiness);
|
|
1147
|
+
const urlTemplate =
|
|
1148
|
+
asString(expose.urlTemplate, "") ||
|
|
1149
|
+
asString(readiness.urlTemplate, "");
|
|
1150
|
+
const url = urlTemplate ? renderTemplate(urlTemplate, templateData) : null;
|
|
1151
|
+
|
|
1152
|
+
try {
|
|
1153
|
+
await waitForReadiness({ service: input.service, url });
|
|
1154
|
+
} catch (err) {
|
|
1155
|
+
terminateChildProcess(child);
|
|
1156
|
+
throw new Error(
|
|
1157
|
+
`Failed to start runtime service "${serviceName}": ${err instanceof Error ? err.message : String(err)}${stderrExcerpt ? ` | stderr: ${stderrExcerpt.trim()}` : ""}`,
|
|
1158
|
+
);
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
const envFingerprint = createHash("sha256").update(stableStringify(envConfig)).digest("hex");
|
|
1162
|
+
return {
|
|
1163
|
+
id: randomUUID(),
|
|
1164
|
+
companyId: input.agent.companyId,
|
|
1165
|
+
projectId: input.workspace.projectId,
|
|
1166
|
+
projectWorkspaceId: input.workspace.workspaceId,
|
|
1167
|
+
executionWorkspaceId: input.executionWorkspaceId ?? null,
|
|
1168
|
+
issueId: input.issue?.id ?? null,
|
|
1169
|
+
serviceName,
|
|
1170
|
+
status: "running",
|
|
1171
|
+
lifecycle,
|
|
1172
|
+
scopeType: input.scopeType,
|
|
1173
|
+
scopeId: input.scopeId,
|
|
1174
|
+
reuseKey: input.reuseKey,
|
|
1175
|
+
command,
|
|
1176
|
+
cwd: serviceCwd,
|
|
1177
|
+
port,
|
|
1178
|
+
url,
|
|
1179
|
+
provider: "local_process",
|
|
1180
|
+
providerRef: child.pid ? String(child.pid) : null,
|
|
1181
|
+
ownerAgentId: input.agent.id,
|
|
1182
|
+
startedByRunId: input.runId,
|
|
1183
|
+
lastUsedAt: new Date().toISOString(),
|
|
1184
|
+
startedAt: new Date().toISOString(),
|
|
1185
|
+
stoppedAt: null,
|
|
1186
|
+
stopPolicy: parseObject(input.service.stopPolicy),
|
|
1187
|
+
healthStatus: "healthy",
|
|
1188
|
+
reused: false,
|
|
1189
|
+
db: input.db,
|
|
1190
|
+
child,
|
|
1191
|
+
leaseRunIds: new Set([input.runId]),
|
|
1192
|
+
idleTimer: null,
|
|
1193
|
+
envFingerprint,
|
|
1194
|
+
};
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
function scheduleIdleStop(record: RuntimeServiceRecord) {
|
|
1198
|
+
clearIdleTimer(record);
|
|
1199
|
+
const stopType = asString(record.stopPolicy?.type, "manual");
|
|
1200
|
+
if (stopType !== "idle_timeout") return;
|
|
1201
|
+
const idleSeconds = Math.max(1, asNumber(record.stopPolicy?.idleSeconds, 1800));
|
|
1202
|
+
record.idleTimer = setTimeout(() => {
|
|
1203
|
+
stopRuntimeService(record.id).catch(() => undefined);
|
|
1204
|
+
}, idleSeconds * 1000);
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
async function stopRuntimeService(serviceId: string) {
|
|
1208
|
+
const record = runtimeServicesById.get(serviceId);
|
|
1209
|
+
if (!record) return;
|
|
1210
|
+
clearIdleTimer(record);
|
|
1211
|
+
record.status = "stopped";
|
|
1212
|
+
record.lastUsedAt = new Date().toISOString();
|
|
1213
|
+
record.stoppedAt = new Date().toISOString();
|
|
1214
|
+
if (record.child && record.child.pid) {
|
|
1215
|
+
terminateChildProcess(record.child);
|
|
1216
|
+
}
|
|
1217
|
+
runtimeServicesById.delete(serviceId);
|
|
1218
|
+
if (record.reuseKey) {
|
|
1219
|
+
runtimeServicesByReuseKey.delete(record.reuseKey);
|
|
1220
|
+
}
|
|
1221
|
+
await persistRuntimeServiceRecord(record.db, record);
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
async function markPersistedRuntimeServicesStoppedForExecutionWorkspace(input: {
|
|
1225
|
+
db: Db;
|
|
1226
|
+
executionWorkspaceId: string;
|
|
1227
|
+
}) {
|
|
1228
|
+
const now = new Date();
|
|
1229
|
+
await input.db
|
|
1230
|
+
.update(workspaceRuntimeServices)
|
|
1231
|
+
.set({
|
|
1232
|
+
status: "stopped",
|
|
1233
|
+
healthStatus: "unknown",
|
|
1234
|
+
stoppedAt: now,
|
|
1235
|
+
lastUsedAt: now,
|
|
1236
|
+
updatedAt: now,
|
|
1237
|
+
})
|
|
1238
|
+
.where(
|
|
1239
|
+
and(
|
|
1240
|
+
eq(workspaceRuntimeServices.executionWorkspaceId, input.executionWorkspaceId),
|
|
1241
|
+
inArray(workspaceRuntimeServices.status, ["starting", "running"]),
|
|
1242
|
+
),
|
|
1243
|
+
);
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
function registerRuntimeService(db: Db | undefined, record: RuntimeServiceRecord) {
|
|
1247
|
+
record.db = db;
|
|
1248
|
+
runtimeServicesById.set(record.id, record);
|
|
1249
|
+
if (record.reuseKey) {
|
|
1250
|
+
runtimeServicesByReuseKey.set(record.reuseKey, record.id);
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
record.child?.on("exit", (code, signal) => {
|
|
1254
|
+
const current = runtimeServicesById.get(record.id);
|
|
1255
|
+
if (!current) return;
|
|
1256
|
+
clearIdleTimer(current);
|
|
1257
|
+
current.status = code === 0 || signal === "SIGTERM" ? "stopped" : "failed";
|
|
1258
|
+
current.healthStatus = current.status === "failed" ? "unhealthy" : "unknown";
|
|
1259
|
+
current.lastUsedAt = new Date().toISOString();
|
|
1260
|
+
current.stoppedAt = new Date().toISOString();
|
|
1261
|
+
runtimeServicesById.delete(current.id);
|
|
1262
|
+
if (current.reuseKey && runtimeServicesByReuseKey.get(current.reuseKey) === current.id) {
|
|
1263
|
+
runtimeServicesByReuseKey.delete(current.reuseKey);
|
|
1264
|
+
}
|
|
1265
|
+
void persistRuntimeServiceRecord(db, current);
|
|
1266
|
+
});
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
export async function ensureRuntimeServicesForRun(input: {
|
|
1270
|
+
db?: Db;
|
|
1271
|
+
runId: string;
|
|
1272
|
+
agent: ExecutionWorkspaceAgentRef;
|
|
1273
|
+
issue: ExecutionWorkspaceIssueRef | null;
|
|
1274
|
+
workspace: RealizedExecutionWorkspace;
|
|
1275
|
+
executionWorkspaceId?: string | null;
|
|
1276
|
+
config: Record<string, unknown>;
|
|
1277
|
+
adapterEnv: Record<string, string>;
|
|
1278
|
+
onLog?: (stream: "stdout" | "stderr", chunk: string) => Promise<void>;
|
|
1279
|
+
}): Promise<RuntimeServiceRef[]> {
|
|
1280
|
+
const runtime = parseObject(input.config.workspaceRuntime);
|
|
1281
|
+
const rawServices = Array.isArray(runtime.services)
|
|
1282
|
+
? runtime.services.filter((entry): entry is Record<string, unknown> => typeof entry === "object" && entry !== null)
|
|
1283
|
+
: [];
|
|
1284
|
+
const acquiredServiceIds: string[] = [];
|
|
1285
|
+
const refs: RuntimeServiceRef[] = [];
|
|
1286
|
+
runtimeServiceLeasesByRun.set(input.runId, acquiredServiceIds);
|
|
1287
|
+
|
|
1288
|
+
try {
|
|
1289
|
+
for (const service of rawServices) {
|
|
1290
|
+
const lifecycle = asString(service.lifecycle, "shared") === "ephemeral" ? "ephemeral" : "shared";
|
|
1291
|
+
const { scopeType, scopeId } = resolveServiceScopeId({
|
|
1292
|
+
service,
|
|
1293
|
+
workspace: input.workspace,
|
|
1294
|
+
executionWorkspaceId: input.executionWorkspaceId,
|
|
1295
|
+
issue: input.issue,
|
|
1296
|
+
runId: input.runId,
|
|
1297
|
+
agent: input.agent,
|
|
1298
|
+
});
|
|
1299
|
+
const envConfig = parseObject(service.env);
|
|
1300
|
+
const envFingerprint = createHash("sha256").update(stableStringify(envConfig)).digest("hex");
|
|
1301
|
+
const serviceName = asString(service.name, "service");
|
|
1302
|
+
const reuseKey =
|
|
1303
|
+
lifecycle === "shared"
|
|
1304
|
+
? [scopeType, scopeId ?? "", serviceName, envFingerprint].join(":")
|
|
1305
|
+
: null;
|
|
1306
|
+
|
|
1307
|
+
if (reuseKey) {
|
|
1308
|
+
const existingId = runtimeServicesByReuseKey.get(reuseKey);
|
|
1309
|
+
const existing = existingId ? runtimeServicesById.get(existingId) : null;
|
|
1310
|
+
if (existing && existing.status === "running") {
|
|
1311
|
+
existing.leaseRunIds.add(input.runId);
|
|
1312
|
+
existing.lastUsedAt = new Date().toISOString();
|
|
1313
|
+
existing.stoppedAt = null;
|
|
1314
|
+
clearIdleTimer(existing);
|
|
1315
|
+
await persistRuntimeServiceRecord(input.db, existing);
|
|
1316
|
+
acquiredServiceIds.push(existing.id);
|
|
1317
|
+
refs.push(toRuntimeServiceRef(existing, { reused: true }));
|
|
1318
|
+
continue;
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
const record = await startLocalRuntimeService({
|
|
1323
|
+
db: input.db,
|
|
1324
|
+
runId: input.runId,
|
|
1325
|
+
agent: input.agent,
|
|
1326
|
+
issue: input.issue,
|
|
1327
|
+
workspace: input.workspace,
|
|
1328
|
+
executionWorkspaceId: input.executionWorkspaceId,
|
|
1329
|
+
adapterEnv: input.adapterEnv,
|
|
1330
|
+
service,
|
|
1331
|
+
onLog: input.onLog,
|
|
1332
|
+
reuseKey,
|
|
1333
|
+
scopeType,
|
|
1334
|
+
scopeId,
|
|
1335
|
+
});
|
|
1336
|
+
registerRuntimeService(input.db, record);
|
|
1337
|
+
await persistRuntimeServiceRecord(input.db, record);
|
|
1338
|
+
acquiredServiceIds.push(record.id);
|
|
1339
|
+
refs.push(toRuntimeServiceRef(record));
|
|
1340
|
+
}
|
|
1341
|
+
} catch (err) {
|
|
1342
|
+
await releaseRuntimeServicesForRun(input.runId);
|
|
1343
|
+
throw err;
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
return refs;
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1349
|
+
export async function releaseRuntimeServicesForRun(runId: string) {
|
|
1350
|
+
const acquired = runtimeServiceLeasesByRun.get(runId) ?? [];
|
|
1351
|
+
runtimeServiceLeasesByRun.delete(runId);
|
|
1352
|
+
for (const serviceId of acquired) {
|
|
1353
|
+
const record = runtimeServicesById.get(serviceId);
|
|
1354
|
+
if (!record) continue;
|
|
1355
|
+
record.leaseRunIds.delete(runId);
|
|
1356
|
+
record.lastUsedAt = new Date().toISOString();
|
|
1357
|
+
const stopType = asString(record.stopPolicy?.type, record.lifecycle === "ephemeral" ? "on_run_finish" : "manual");
|
|
1358
|
+
await persistRuntimeServiceRecord(record.db, record);
|
|
1359
|
+
if (record.leaseRunIds.size === 0) {
|
|
1360
|
+
if (record.lifecycle === "ephemeral" || stopType === "on_run_finish") {
|
|
1361
|
+
await stopRuntimeService(serviceId);
|
|
1362
|
+
continue;
|
|
1363
|
+
}
|
|
1364
|
+
scheduleIdleStop(record);
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
export async function stopRuntimeServicesForExecutionWorkspace(input: {
|
|
1370
|
+
db?: Db;
|
|
1371
|
+
executionWorkspaceId: string;
|
|
1372
|
+
workspaceCwd?: string | null;
|
|
1373
|
+
}) {
|
|
1374
|
+
const normalizedWorkspaceCwd = input.workspaceCwd ? path.resolve(input.workspaceCwd) : null;
|
|
1375
|
+
const matchingServiceIds = Array.from(runtimeServicesById.values())
|
|
1376
|
+
.filter((record) => {
|
|
1377
|
+
if (record.executionWorkspaceId === input.executionWorkspaceId) return true;
|
|
1378
|
+
if (!normalizedWorkspaceCwd || !record.cwd) return false;
|
|
1379
|
+
const resolvedCwd = path.resolve(record.cwd);
|
|
1380
|
+
return (
|
|
1381
|
+
resolvedCwd === normalizedWorkspaceCwd ||
|
|
1382
|
+
resolvedCwd.startsWith(`${normalizedWorkspaceCwd}${path.sep}`)
|
|
1383
|
+
);
|
|
1384
|
+
})
|
|
1385
|
+
.map((record) => record.id);
|
|
1386
|
+
|
|
1387
|
+
for (const serviceId of matchingServiceIds) {
|
|
1388
|
+
await stopRuntimeService(serviceId);
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1391
|
+
if (input.db) {
|
|
1392
|
+
await markPersistedRuntimeServicesStoppedForExecutionWorkspace({
|
|
1393
|
+
db: input.db,
|
|
1394
|
+
executionWorkspaceId: input.executionWorkspaceId,
|
|
1395
|
+
});
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
|
|
1399
|
+
export async function listWorkspaceRuntimeServicesForProjectWorkspaces(
|
|
1400
|
+
db: Db,
|
|
1401
|
+
companyId: string,
|
|
1402
|
+
projectWorkspaceIds: string[],
|
|
1403
|
+
) {
|
|
1404
|
+
if (projectWorkspaceIds.length === 0) return new Map<string, typeof workspaceRuntimeServices.$inferSelect[]>();
|
|
1405
|
+
const rows = await db
|
|
1406
|
+
.select()
|
|
1407
|
+
.from(workspaceRuntimeServices)
|
|
1408
|
+
.where(
|
|
1409
|
+
and(
|
|
1410
|
+
eq(workspaceRuntimeServices.companyId, companyId),
|
|
1411
|
+
inArray(workspaceRuntimeServices.projectWorkspaceId, projectWorkspaceIds),
|
|
1412
|
+
),
|
|
1413
|
+
)
|
|
1414
|
+
.orderBy(desc(workspaceRuntimeServices.updatedAt), desc(workspaceRuntimeServices.createdAt));
|
|
1415
|
+
|
|
1416
|
+
const grouped = new Map<string, typeof workspaceRuntimeServices.$inferSelect[]>();
|
|
1417
|
+
for (const row of rows) {
|
|
1418
|
+
if (!row.projectWorkspaceId) continue;
|
|
1419
|
+
const existing = grouped.get(row.projectWorkspaceId);
|
|
1420
|
+
if (existing) existing.push(row);
|
|
1421
|
+
else grouped.set(row.projectWorkspaceId, [row]);
|
|
1422
|
+
}
|
|
1423
|
+
return grouped;
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
export async function reconcilePersistedRuntimeServicesOnStartup(db: Db) {
|
|
1427
|
+
const staleRows = await db
|
|
1428
|
+
.select({ id: workspaceRuntimeServices.id })
|
|
1429
|
+
.from(workspaceRuntimeServices)
|
|
1430
|
+
.where(
|
|
1431
|
+
and(
|
|
1432
|
+
eq(workspaceRuntimeServices.provider, "local_process"),
|
|
1433
|
+
inArray(workspaceRuntimeServices.status, ["starting", "running"]),
|
|
1434
|
+
),
|
|
1435
|
+
);
|
|
1436
|
+
|
|
1437
|
+
if (staleRows.length === 0) return { reconciled: 0 };
|
|
1438
|
+
|
|
1439
|
+
const now = new Date();
|
|
1440
|
+
await db
|
|
1441
|
+
.update(workspaceRuntimeServices)
|
|
1442
|
+
.set({
|
|
1443
|
+
status: "stopped",
|
|
1444
|
+
healthStatus: "unknown",
|
|
1445
|
+
stoppedAt: now,
|
|
1446
|
+
lastUsedAt: now,
|
|
1447
|
+
updatedAt: now,
|
|
1448
|
+
})
|
|
1449
|
+
.where(
|
|
1450
|
+
and(
|
|
1451
|
+
eq(workspaceRuntimeServices.provider, "local_process"),
|
|
1452
|
+
inArray(workspaceRuntimeServices.status, ["starting", "running"]),
|
|
1453
|
+
),
|
|
1454
|
+
);
|
|
1455
|
+
|
|
1456
|
+
return { reconciled: staleRows.length };
|
|
1457
|
+
}
|
|
1458
|
+
|
|
1459
|
+
export async function persistAdapterManagedRuntimeServices(input: {
|
|
1460
|
+
db: Db;
|
|
1461
|
+
adapterType: string;
|
|
1462
|
+
runId: string;
|
|
1463
|
+
agent: ExecutionWorkspaceAgentRef;
|
|
1464
|
+
issue: ExecutionWorkspaceIssueRef | null;
|
|
1465
|
+
workspace: RealizedExecutionWorkspace;
|
|
1466
|
+
executionWorkspaceId?: string | null;
|
|
1467
|
+
reports: AdapterRuntimeServiceReport[];
|
|
1468
|
+
}) {
|
|
1469
|
+
const refs = normalizeAdapterManagedRuntimeServices(input);
|
|
1470
|
+
if (refs.length === 0) return refs;
|
|
1471
|
+
|
|
1472
|
+
const existingRows = await input.db
|
|
1473
|
+
.select()
|
|
1474
|
+
.from(workspaceRuntimeServices)
|
|
1475
|
+
.where(inArray(workspaceRuntimeServices.id, refs.map((ref) => ref.id)));
|
|
1476
|
+
const existingById = new Map(existingRows.map((row) => [row.id, row]));
|
|
1477
|
+
|
|
1478
|
+
for (const ref of refs) {
|
|
1479
|
+
const existing = existingById.get(ref.id);
|
|
1480
|
+
const startedAt = existing?.startedAt ?? new Date(ref.startedAt);
|
|
1481
|
+
const createdAt = existing?.createdAt ?? new Date();
|
|
1482
|
+
await input.db
|
|
1483
|
+
.insert(workspaceRuntimeServices)
|
|
1484
|
+
.values({
|
|
1485
|
+
id: ref.id,
|
|
1486
|
+
companyId: ref.companyId,
|
|
1487
|
+
projectId: ref.projectId,
|
|
1488
|
+
projectWorkspaceId: ref.projectWorkspaceId,
|
|
1489
|
+
executionWorkspaceId: ref.executionWorkspaceId,
|
|
1490
|
+
issueId: ref.issueId,
|
|
1491
|
+
scopeType: ref.scopeType,
|
|
1492
|
+
scopeId: ref.scopeId,
|
|
1493
|
+
serviceName: ref.serviceName,
|
|
1494
|
+
status: ref.status,
|
|
1495
|
+
lifecycle: ref.lifecycle,
|
|
1496
|
+
reuseKey: ref.reuseKey,
|
|
1497
|
+
command: ref.command,
|
|
1498
|
+
cwd: ref.cwd,
|
|
1499
|
+
port: ref.port,
|
|
1500
|
+
url: ref.url,
|
|
1501
|
+
provider: ref.provider,
|
|
1502
|
+
providerRef: ref.providerRef,
|
|
1503
|
+
ownerAgentId: ref.ownerAgentId,
|
|
1504
|
+
startedByRunId: ref.startedByRunId,
|
|
1505
|
+
lastUsedAt: new Date(ref.lastUsedAt),
|
|
1506
|
+
startedAt,
|
|
1507
|
+
stoppedAt: ref.stoppedAt ? new Date(ref.stoppedAt) : null,
|
|
1508
|
+
stopPolicy: ref.stopPolicy,
|
|
1509
|
+
healthStatus: ref.healthStatus,
|
|
1510
|
+
createdAt,
|
|
1511
|
+
updatedAt: new Date(),
|
|
1512
|
+
})
|
|
1513
|
+
.onConflictDoUpdate({
|
|
1514
|
+
target: workspaceRuntimeServices.id,
|
|
1515
|
+
set: {
|
|
1516
|
+
projectId: ref.projectId,
|
|
1517
|
+
projectWorkspaceId: ref.projectWorkspaceId,
|
|
1518
|
+
executionWorkspaceId: ref.executionWorkspaceId,
|
|
1519
|
+
issueId: ref.issueId,
|
|
1520
|
+
scopeType: ref.scopeType,
|
|
1521
|
+
scopeId: ref.scopeId,
|
|
1522
|
+
serviceName: ref.serviceName,
|
|
1523
|
+
status: ref.status,
|
|
1524
|
+
lifecycle: ref.lifecycle,
|
|
1525
|
+
reuseKey: ref.reuseKey,
|
|
1526
|
+
command: ref.command,
|
|
1527
|
+
cwd: ref.cwd,
|
|
1528
|
+
port: ref.port,
|
|
1529
|
+
url: ref.url,
|
|
1530
|
+
provider: ref.provider,
|
|
1531
|
+
providerRef: ref.providerRef,
|
|
1532
|
+
ownerAgentId: ref.ownerAgentId,
|
|
1533
|
+
startedByRunId: ref.startedByRunId,
|
|
1534
|
+
lastUsedAt: new Date(ref.lastUsedAt),
|
|
1535
|
+
startedAt,
|
|
1536
|
+
stoppedAt: ref.stoppedAt ? new Date(ref.stoppedAt) : null,
|
|
1537
|
+
stopPolicy: ref.stopPolicy,
|
|
1538
|
+
healthStatus: ref.healthStatus,
|
|
1539
|
+
updatedAt: new Date(),
|
|
1540
|
+
},
|
|
1541
|
+
});
|
|
1542
|
+
}
|
|
1543
|
+
|
|
1544
|
+
return refs;
|
|
1545
|
+
}
|
|
1546
|
+
|
|
1547
|
+
export function buildWorkspaceReadyComment(input: {
|
|
1548
|
+
workspace: RealizedExecutionWorkspace;
|
|
1549
|
+
runtimeServices: RuntimeServiceRef[];
|
|
1550
|
+
}) {
|
|
1551
|
+
const lines = ["## Workspace Ready", ""];
|
|
1552
|
+
lines.push(`- Strategy: \`${input.workspace.strategy}\``);
|
|
1553
|
+
if (input.workspace.branchName) lines.push(`- Branch: \`${input.workspace.branchName}\``);
|
|
1554
|
+
lines.push(`- CWD: \`${input.workspace.cwd}\``);
|
|
1555
|
+
if (input.workspace.worktreePath && input.workspace.worktreePath !== input.workspace.cwd) {
|
|
1556
|
+
lines.push(`- Worktree: \`${input.workspace.worktreePath}\``);
|
|
1557
|
+
}
|
|
1558
|
+
for (const service of input.runtimeServices) {
|
|
1559
|
+
const detail = service.url ? `${service.serviceName}: ${service.url}` : `${service.serviceName}: running`;
|
|
1560
|
+
const suffix = service.reused ? " (reused)" : "";
|
|
1561
|
+
lines.push(`- Service: ${detail}${suffix}`);
|
|
1562
|
+
}
|
|
1563
|
+
return lines.join("\n");
|
|
1564
|
+
}
|
|
1565
|
+
|