@temmo/cli 0.0.0-stage → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +14 -0
- package/.claude-plugin/plugin.json +16 -0
- package/.codex-plugin/plugin.json +30 -0
- package/.cursor-plugin/plugin.json +22 -0
- package/.mcp.json +18 -0
- package/LICENSE +202 -0
- package/README.md +82 -2
- package/THIRD_PARTY_NOTICES +32 -0
- package/commands/afk.md +17 -0
- package/deploy/build-ui.mjs +65 -0
- package/deploy/claude/README.md +16 -0
- package/deploy/claude/auto-mode.json +31 -0
- package/deploy/postgres/compose.yaml +26 -0
- package/deploy/temmo-runner.service +24 -0
- package/deploy/verify-ui.mjs +15 -0
- package/harness-core/README.md +27 -0
- package/harness-core/bin/harness.mjs +17 -0
- package/harness-core/package.json +10 -0
- package/harness-core/stage.mjs +108 -0
- package/harness-core/stages/commit.md +7 -0
- package/harness-core/stages/pr.md +7 -0
- package/harness-core/stages/push.md +7 -0
- package/hooks/codex-hooks.json +14 -0
- package/hooks/cursor-hooks.json +17 -0
- package/hooks/hooks.json +27 -0
- package/lib/acp/permission.js +72 -0
- package/lib/acp/sessionConfig.js +109 -0
- package/lib/activity.js +244 -0
- package/lib/agent-guard.js +72 -0
- package/lib/agents-route.js +55 -0
- package/lib/agents.js +213 -0
- package/lib/agui.js +255 -0
- package/lib/alerts.js +36 -0
- package/lib/analyze-otlp.js +46 -0
- package/lib/analyze-run-code.js +397 -0
- package/lib/analyze-sources.js +372 -0
- package/lib/approval-card.js +54 -0
- package/lib/approval-cli.js +53 -0
- package/lib/approval-request.js +81 -0
- package/lib/approval-signer.js +48 -0
- package/lib/approvals.js +295 -0
- package/lib/attachment-cache.js +236 -0
- package/lib/attachment.js +63 -0
- package/lib/blocked.js +20 -0
- package/lib/build-review.js +90 -0
- package/lib/builder/cli.js +90 -0
- package/lib/builder/policy.js +176 -0
- package/lib/builder/recover.js +189 -0
- package/lib/builder/run.js +240 -0
- package/lib/builder/settle.js +198 -0
- package/lib/builder/stage.js +240 -0
- package/lib/builder/state.js +73 -0
- package/lib/builder/supervisor.js +289 -0
- package/lib/builder-clean.js +223 -0
- package/lib/builder-code.cjs +67 -0
- package/lib/builder.js +9 -0
- package/lib/bus-postgres.js +281 -0
- package/lib/bus.js +349 -0
- package/lib/catalog/author.js +278 -0
- package/lib/catalog/dot.js +426 -0
- package/lib/catalog/pattern-model.js +193 -0
- package/lib/catalog/patterns.js +324 -0
- package/lib/catalog/projects.js +83 -0
- package/lib/catalog/records.js +2 -0
- package/lib/catalog/roster.js +70 -0
- package/lib/catalog/team-files.js +49 -0
- package/lib/catalog/teams.js +109 -0
- package/lib/claude-socket.js +333 -0
- package/lib/cli.js +741 -0
- package/lib/code-mode.js +45 -0
- package/lib/code-read-policy.yaml +9 -0
- package/lib/code-run.js +461 -0
- package/lib/codex-compact.js +141 -0
- package/lib/compaction.js +21 -0
- package/lib/config.js +424 -0
- package/lib/cursor-trace.js +248 -0
- package/lib/deep-link.js +20 -0
- package/lib/deliver-push.js +120 -0
- package/lib/delivery.js +28 -0
- package/lib/deploy-tree.js +227 -0
- package/lib/durable.js +404 -0
- package/lib/factory-tick.js +32 -0
- package/lib/family.js +63 -0
- package/lib/fetch-origin.js +53 -0
- package/lib/format.js +133 -0
- package/lib/harness-surface-cli.js +153 -0
- package/lib/harness-surface.js +486 -0
- package/lib/harness.js +271 -0
- package/lib/hook.mjs +291 -0
- package/lib/idle.js +40 -0
- package/lib/install-cursor.js +115 -0
- package/lib/install-opencode.js +105 -0
- package/lib/job.js +82 -0
- package/lib/land-cli.js +319 -0
- package/lib/land-member.js +64 -0
- package/lib/land.js +320 -0
- package/lib/lessons.js +223 -0
- package/lib/limit-alert.js +423 -0
- package/lib/limits.js +284 -0
- package/lib/machine-prompt.js +19 -0
- package/lib/merge-sweep.js +149 -0
- package/lib/messaging.js +1002 -0
- package/lib/mission-cli.js +103 -0
- package/lib/mission.js +241 -0
- package/lib/mode.js +215 -0
- package/lib/models.js +114 -0
- package/lib/names.js +86 -0
- package/lib/opencode-api.js +57 -0
- package/lib/opencode-plugin.mjs +313 -0
- package/lib/operator-token.js +78 -0
- package/lib/operator.js +187 -0
- package/lib/pad-activity.js +143 -0
- package/lib/pad-fresh.js +44 -0
- package/lib/pad.js +198 -0
- package/lib/pattern-cli.js +120 -0
- package/lib/pattern-compiler.js +68 -0
- package/lib/pattern-runs.js +795 -0
- package/lib/pattern-stall.js +58 -0
- package/lib/pattern-tick.js +45 -0
- package/lib/pattern-workflow.js +69 -0
- package/lib/pi-extension.mjs +98 -0
- package/lib/pi.js +57 -0
- package/lib/plugin-copy.js +76 -0
- package/lib/pm-review.js +32 -0
- package/lib/pm-workspace.js +87 -0
- package/lib/postgres.js +115 -0
- package/lib/pr-card.js +34 -0
- package/lib/probe.js +295 -0
- package/lib/process.js +67 -0
- package/lib/project-pause.js +23 -0
- package/lib/prune.js +793 -0
- package/lib/questions.js +250 -0
- package/lib/receipts.js +63 -0
- package/lib/reflect.js +962 -0
- package/lib/rename-migration.js +118 -0
- package/lib/run-terminal.js +437 -0
- package/lib/runner-request.js +33 -0
- package/lib/sandbox-app.js +120 -0
- package/lib/sandbox-claims.js +258 -0
- package/lib/sandbox-cli.js +188 -0
- package/lib/sandbox-hosts-cli.js +68 -0
- package/lib/sandbox-hosts.js +94 -0
- package/lib/sandbox-pr.js +102 -0
- package/lib/sandbox-proposal.js +367 -0
- package/lib/sandbox-tools.js +143 -0
- package/lib/sandbox.js +501 -0
- package/lib/schedule-session.js +68 -0
- package/lib/session-activity.js +67 -0
- package/lib/session-events.js +94 -0
- package/lib/sessions/control.js +244 -0
- package/lib/sessions/git-refs.js +265 -0
- package/lib/sessions/launch.js +359 -0
- package/lib/sessions/registry.js +450 -0
- package/lib/sessions/scope.js +111 -0
- package/lib/setup.js +377 -0
- package/lib/signed-approval.js +225 -0
- package/lib/slash.js +167 -0
- package/lib/spawn-cli.js +45 -0
- package/lib/spawn.js +107 -0
- package/lib/start-limit.js +58 -0
- package/lib/state.js +24 -0
- package/lib/stop.js +308 -0
- package/lib/store-read.js +62 -0
- package/lib/store.js +252 -0
- package/lib/subagent-guard.js +240 -0
- package/lib/surfaces/channel-post.js +43 -0
- package/lib/surfaces/instructions.js +15 -0
- package/lib/surfaces/mcp-reload.js +144 -0
- package/lib/surfaces/ops.js +629 -0
- package/lib/surfaces/render-mcp.js +33 -0
- package/lib/surfaces/run-terminal-ops.js +40 -0
- package/lib/surfaces/sandbox-ops.js +36 -0
- package/lib/surfaces/schema.js +7 -0
- package/lib/surfaces/task-view.js +90 -0
- package/lib/surfaces/types.js +4 -0
- package/lib/task-cli.js +274 -0
- package/lib/task-order.js +34 -0
- package/lib/task-order.mjs +2 -0
- package/lib/task-route.js +125 -0
- package/lib/tasks.js +631 -0
- package/lib/team-permission.js +34 -0
- package/lib/team-templates/blitz/team.dot +9 -0
- package/lib/team-templates/factory/team.dot +26 -0
- package/lib/team-templates/feature/team.dot +14 -0
- package/lib/team-templates/fix-and-review/team.dot +12 -0
- package/lib/team-templates/product/team.dot +16 -0
- package/lib/team-templates/release/team.dot +7 -0
- package/lib/team-templates/review/team.dot +6 -0
- package/lib/terminal-draft.js +42 -0
- package/lib/thread-branch.js +82 -0
- package/lib/thread-channel-store.js +181 -0
- package/lib/thread-channels.js +116 -0
- package/lib/thread-listening.js +20 -0
- package/lib/thread-routes.js +91 -0
- package/lib/threads.js +441 -0
- package/lib/tool-api.js +33 -0
- package/lib/transcript.js +98 -0
- package/lib/ui-build-output.js +19 -0
- package/lib/ui-capabilities.js +90 -0
- package/lib/usage.js +182 -0
- package/lib/vendor/mcp-sdk.d.mts +4 -0
- package/lib/vendor/mcp-sdk.mjs +17046 -0
- package/lib/waiter.js +127 -0
- package/lib/workflows.js +546 -0
- package/lib/worktree-setup.js +86 -0
- package/package.json +110 -4
- package/runner/adapters/claude.js +465 -0
- package/runner/adapters/cli-sandbox.js +277 -0
- package/runner/adapters/codex.js +675 -0
- package/runner/adapters/cursor.mjs +937 -0
- package/runner/adapters/fake.js +119 -0
- package/runner/adapters/opencode-sandbox.js +370 -0
- package/runner/adapters/opencode.js +486 -0
- package/runner/adapters/pi-sandbox.js +134 -0
- package/runner/adapters/pi.js +364 -0
- package/runner/client.js +40 -0
- package/runner/contract.js +129 -0
- package/runner/control.js +178 -0
- package/runner/delivery.js +184 -0
- package/runner/factory-tick.mjs +10 -0
- package/runner/lock.js +41 -0
- package/runner/main.mjs +118 -0
- package/runner/pattern-tick.mjs +16 -0
- package/runner/publish.js +113 -0
- package/runner/runner.js +2612 -0
- package/runner/sandbox-run.mjs +239 -0
- package/runner/schedules.js +294 -0
- package/runner/task-wake.js +59 -0
- package/scripts/ai-collab +3 -0
- package/scripts/ai-collab-bus.mjs +4 -0
- package/scripts/ai-collab-code-mcp.mjs +3 -0
- package/scripts/ai-collab-hook +3 -0
- package/scripts/ai-collab-hook.mjs +3 -0
- package/scripts/ai-collab-mcp-tools.mjs +2 -0
- package/scripts/ai-collab-mcp.mjs +3 -0
- package/scripts/ai-collab.mjs +3 -0
- package/scripts/install-cursor.mjs +11 -0
- package/scripts/install-opencode.mjs +10 -0
- package/scripts/temmo +3 -0
- package/scripts/temmo-bus.mjs +151 -0
- package/scripts/temmo-code-mcp.mjs +63 -0
- package/scripts/temmo-hook +3 -0
- package/scripts/temmo-hook.mjs +201 -0
- package/scripts/temmo-mcp-tools.mjs +22 -0
- package/scripts/temmo-mcp.mjs +27 -0
- package/scripts/temmo.mjs +4 -0
- package/skills/antislop/SKILL.md +701 -0
- package/skills/antislop/VERSION +1 -0
- package/skills/antislop-code/SKILL.md +128 -0
- package/skills/antislop-human/LICENSE-web-quality-skills +21 -0
- package/skills/antislop-human/SKILL.md +204 -0
- package/skills/antislop-human/contrast-check.py +168 -0
- package/skills/antislop-human/contrast-mcp.py +125 -0
- package/skills/antislop-human/references/A11Y-PATTERNS.md +249 -0
- package/skills/antislop-human/references/WCAG.md +127 -0
- package/skills/antislop-layoutmobile/SKILL.md +170 -0
- package/skills/antislop-ui/LICENSE-web-quality-skills +21 -0
- package/skills/antislop-ui/SKILL.md +351 -0
- package/skills/antislop-ui/references/CORE-WEB-VITALS.md +181 -0
- package/skills/antislop-ui/references/WEB-BASELINE.md +28 -0
- package/skills/harness-surface/SKILL.md +42 -0
- package/skills/land/SKILL.md +32 -0
- package/skills/memory-file/SKILL.md +39 -0
- package/skills/temmo/SKILL.md +101 -0
- package/skills/temmo/references/role-programs.md +47 -0
- package/skills/ui-sdk/SKILL.md +56 -0
- package/skills/ui-sdk/references/agent-ready.md +136 -0
- package/skills/ui-sdk/references/build-interface.md +187 -0
- package/skills/ui-sdk/references/create-component.md +88 -0
- package/skills/ui-sdk/references/patterns.md +246 -0
- package/skills/ui-sdk/references/styles.md +66 -0
- package/skills/ui-sdk/references/testing.md +172 -0
- package/skills/ui-sdk/references/web-components.md +78 -0
- package/tools/screen-state.mjs +119 -0
- package/tools/smoke-ui.mjs +540 -0
- package/web/app/activity.jsx +251 -0
- package/web/app/agent-mark.tsx +43 -0
- package/web/app/alert-level.jsx +16 -0
- package/web/app/api/[action]/route.js +8 -0
- package/web/app/api/auth/[step]/route.js +12 -0
- package/web/app/api/events/route.js +8 -0
- package/web/app/api/file/[name]/route.js +8 -0
- package/web/app/api/land/route.js +12 -0
- package/web/app/api/lessons/route.js +8 -0
- package/web/app/api/push/key/route.js +8 -0
- package/web/app/api/push/subscribe/route.js +12 -0
- package/web/app/api/push/test/route.js +8 -0
- package/web/app/api/refs/route.js +8 -0
- package/web/app/api/review-diff/route.js +8 -0
- package/web/app/api/schedules/route.js +8 -0
- package/web/app/api/thread-diff/route.js +8 -0
- package/web/app/api/version/route.js +6 -0
- package/web/app/cards.jsx +189 -0
- package/web/app/chat-api.js +82 -0
- package/web/app/chat-composer.jsx +313 -0
- package/web/app/chat-console.jsx +691 -0
- package/web/app/chat-image.tsx +61 -0
- package/web/app/chat-message.jsx +122 -0
- package/web/app/chat-shared.jsx +59 -0
- package/web/app/chat-sidebar.jsx +25 -0
- package/web/app/chat-stream.js +127 -0
- package/web/app/chat.jsx +31 -0
- package/web/app/compactions.jsx +57 -0
- package/web/app/composer-actions.tsx +87 -0
- package/web/app/decisions.jsx +53 -0
- package/web/app/fonts/OFL-hanken-grotesk.txt +93 -0
- package/web/app/fonts/OFL-ibm-plex-mono.txt +93 -0
- package/web/app/fonts/hanken-grotesk-latin-wght-normal.woff2 +0 -0
- package/web/app/fonts/ibm-plex-mono-latin-400-normal.woff2 +0 -0
- package/web/app/fonts/ibm-plex-mono-latin-500-normal.woff2 +0 -0
- package/web/app/globals.css +199 -0
- package/web/app/home.jsx +246 -0
- package/web/app/icon.svg +1 -0
- package/web/app/land-panel.jsx +63 -0
- package/web/app/land-reviews.jsx +20 -0
- package/web/app/layout.jsx +17 -0
- package/web/app/lessons.tsx +76 -0
- package/web/app/logo.tsx +13 -0
- package/web/app/logos.jsx +16 -0
- package/web/app/manifest.js +8 -0
- package/web/app/mode-menu.tsx +46 -0
- package/web/app/new-session.jsx +275 -0
- package/web/app/notify.jsx +180 -0
- package/web/app/outcomes.tsx +94 -0
- package/web/app/page.jsx +9 -0
- package/web/app/pattern-graph.jsx +75 -0
- package/web/app/patterns-page.jsx +293 -0
- package/web/app/phone-nav.tsx +36 -0
- package/web/app/progress-feed.tsx +33 -0
- package/web/app/providers.tsx +27 -0
- package/web/app/quote.jsx +15 -0
- package/web/app/run-card.tsx +200 -0
- package/web/app/run-requests.tsx +148 -0
- package/web/app/scheduled.jsx +105 -0
- package/web/app/session-panel.tsx +174 -0
- package/web/app/session-toolbar.tsx +180 -0
- package/web/app/shadcn.css +638 -0
- package/web/app/shipped.jsx +49 -0
- package/web/app/subagent-runs.jsx +31 -0
- package/web/app/task-graph.jsx +67 -0
- package/web/app/tasks-strip.tsx +36 -0
- package/web/app/tasks.jsx +375 -0
- package/web/app/team-author.jsx +145 -0
- package/web/app/team-editor.jsx +88 -0
- package/web/app/thread-agents.tsx +32 -0
- package/web/app/thread-place.tsx +9 -0
- package/web/app/thread-sessions.tsx +149 -0
- package/web/app/thread-sidebar.tsx +418 -0
- package/web/app/thread-switcher.tsx +27 -0
- package/web/app/tool-diff.jsx +94 -0
- package/web/components/a2ui/controls.jsx +36 -0
- package/web/components/a2ui/option-picker.jsx +23 -0
- package/web/components/a2ui/script.tsx +121 -0
- package/web/components/a2ui/surface.jsx +81 -0
- package/web/components/chat/thread.jsx +50 -0
- package/web/components/script-text.tsx +42 -0
- package/web/components/ui/avatar.jsx +115 -0
- package/web/components/ui/badge.jsx +47 -0
- package/web/components/ui/button.jsx +63 -0
- package/web/components/ui/chat-container.jsx +52 -0
- package/web/components/ui/choice-list.tsx +72 -0
- package/web/components/ui/code-block.jsx +60 -0
- package/web/components/ui/copy-button.tsx +41 -0
- package/web/components/ui/dropdown-menu.jsx +257 -0
- package/web/components/ui/markdown.jsx +84 -0
- package/web/components/ui/message.jsx +28 -0
- package/web/components/ui/popover.jsx +33 -0
- package/web/components/ui/scroll-button.jsx +34 -0
- package/web/components/ui/select.jsx +189 -0
- package/web/components/ui/sheet.jsx +152 -0
- package/web/components/ui/sonner.jsx +10 -0
- package/web/components/ui/tabs.jsx +88 -0
- package/web/components/ui/textarea.jsx +20 -0
- package/web/components/ui/tooltip.jsx +56 -0
- package/web/components.json +24 -0
- package/web/instrumentation.js +8 -0
- package/web/lib/a2ui-catalog.js +34 -0
- package/web/lib/a2ui-pr.js +76 -0
- package/web/lib/a2ui-run-catalog.js +18 -0
- package/web/lib/a2ui-run.js +93 -0
- package/web/lib/a2ui.js +262 -0
- package/web/lib/activity-rows.js +275 -0
- package/web/lib/agent-detail.js +5 -0
- package/web/lib/alert-level.js +44 -0
- package/web/lib/approval-mode.js +22 -0
- package/web/lib/chat-turns.js +174 -0
- package/web/lib/child-subagents.js +16 -0
- package/web/lib/compaction-view.js +48 -0
- package/web/lib/composer-actions.js +40 -0
- package/web/lib/context-view.js +57 -0
- package/web/lib/fuzzy.js +26 -0
- package/web/lib/highlight.js +44 -0
- package/web/lib/lessons-view.js +23 -0
- package/web/lib/mentions.js +16 -0
- package/web/lib/message-routing.js +105 -0
- package/web/lib/new-session.js +85 -0
- package/web/lib/operator-messages.js +28 -0
- package/web/lib/pattern-graph.js +165 -0
- package/web/lib/pattern-view.js +33 -0
- package/web/lib/pending-summary.js +18 -0
- package/web/lib/permission-modes.js +30 -0
- package/web/lib/project-feed.js +107 -0
- package/web/lib/quote.js +7 -0
- package/web/lib/reply-quote.js +31 -0
- package/web/lib/scheduled.js +79 -0
- package/web/lib/selection.js +110 -0
- package/web/lib/send-confirmation.js +22 -0
- package/web/lib/session-dead.js +10 -0
- package/web/lib/session-state.js +132 -0
- package/web/lib/sidebar.js +15 -0
- package/web/lib/slash-commands.js +164 -0
- package/web/lib/start-default.js +35 -0
- package/web/lib/start-mode.js +35 -0
- package/web/lib/stream-views.js +49 -0
- package/web/lib/subagent-state.js +20 -0
- package/web/lib/task-board.js +54 -0
- package/web/lib/task-graph.js +183 -0
- package/web/lib/task-tags.js +41 -0
- package/web/lib/team-editor.js +10 -0
- package/web/lib/thread-groups.js +86 -0
- package/web/lib/threads.js +28 -0
- package/web/lib/tool-diff.js +273 -0
- package/web/lib/ui-events.js +58 -0
- package/web/lib/utils.js +5 -0
- package/web/lib/view-memory.js +25 -0
- package/web/next.config.mjs +37 -0
- package/web/postcss.config.mjs +1 -0
- package/web/proxy.js +25 -0
- package/web/public/sw.js +39 -0
- package/web/server/auth.js +178 -0
- package/web/server/dispatch.js +293 -0
- package/web/server/history.js +56 -0
- package/web/server/hosted.js +58 -0
- package/web/server/hub.js +126 -0
- package/web/server/instance.js +5 -0
- package/web/server/land-reviews.js +64 -0
- package/web/server/push.js +148 -0
- package/web/server/run-terminal.js +79 -0
- package/web/server/schedules.js +38 -0
- package/web/server/shipped.js +51 -0
- package/web/server/task-board.js +71 -0
- package/web/server/threads.js +209 -0
- package/web/server/ui.js +2937 -0
- package/web/tsconfig.base.json +16 -0
- package/web/tsconfig.json +9 -0
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
// Host-side staging and upload: the clone, tools tree, sandbox home and policy for one builder.
|
|
2
|
+
import * as S from "../sandbox.js";
|
|
3
|
+
import { appendFileSync, cpSync, existsSync, mkdirSync, realpathSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { basename, dirname, join } from "node:path";
|
|
6
|
+
import { cursorBinary } from "../harness.js";
|
|
7
|
+
import { findExecutable } from "../sessions/scope.js";
|
|
8
|
+
import { openTasks } from "../tasks.js";
|
|
9
|
+
import { HOME_IN, NTFY_TOKEN_IN, OUT, SANDBOX, TOOLS, WORK } from "../sandbox.js";
|
|
10
|
+
import { APP_ROOT, BASE_REF, GIT_TIMEOUT_MS, SYNC_TIMEOUT_MS, updateState } from "./state.js";
|
|
11
|
+
import { APP_IN, CLAUDE_SETTINGS_IN, CLAUDE_MCP_IN, CURSOR_DIR, CODEX_DIR, OPENCODE_DIR, CLAUDE_DIR, CODE_MCP, CLAUDE_ENV, CURSOR_ENV, builderPolicy, claudeSettings, claudeMcpConfig, captureHooks, hostCaptureEvents, sandboxDefaults } from "./policy.js";
|
|
12
|
+
import { stageToolchain } from "../sandbox-tools.js";
|
|
13
|
+
import { codexFeaturesToml, stageApp } from "../sandbox-app.js";
|
|
14
|
+
const TEMMO_SHIM = `#!/bin/sh
|
|
15
|
+
# Inside a builder sandbox: records task set and task progress for the host to apply after the run.
|
|
16
|
+
case "$1 $2" in
|
|
17
|
+
"task set"|"task progress") ;;
|
|
18
|
+
"task show") cat "\${TEMMO_BUILDER_TASKS:-${TOOLS}/tasks}/$3.json" 2>/dev/null || { echo "temmo: no snapshot of task $3 in this sandbox" >&2; exit 1; }; exit 0 ;;
|
|
19
|
+
*) echo "temmo: inside the builder sandbox only 'task set', 'task progress' and 'task show' work" >&2; exit 2 ;;
|
|
20
|
+
esac
|
|
21
|
+
d="\${TEMMO_BUILDER_OUT:-${OUT}}/temmo"; mkdir -p "$d" || exit 1
|
|
22
|
+
printf '%s\\0' "$@" > "$d/$(date +%s%N)-$$"
|
|
23
|
+
echo "temmo: recorded; the host applies it when this builder exits"
|
|
24
|
+
`;
|
|
25
|
+
const CAPTURE_SHIM = `#!/bin/sh
|
|
26
|
+
# Saves one hook payload; the host replays it into \`oncebitten capture\` after the run.
|
|
27
|
+
d="\${TEMMO_BUILDER_OUT:-${OUT}}/oncebitten"; mkdir -p "$d" || exit 0
|
|
28
|
+
cat > "$d/$(date +%s%N)-$$-\${1:-unknown}.json"
|
|
29
|
+
exit 0
|
|
30
|
+
`;
|
|
31
|
+
const SANDBOX_PATH = `${TOOLS}/bin:${TOOLS}/node/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin`;
|
|
32
|
+
// glibc libraries the sandbox image provides itself; host copies would not match its loader.
|
|
33
|
+
const GLIBC_LIB = /^(libc|libm|libdl|libpthread|librt|ld-linux[^.]*)\.so/;
|
|
34
|
+
export async function git(cwd, args, env) {
|
|
35
|
+
const res = await S.runTool("git", ["-C", cwd, ...args], { env, timeoutMs: GIT_TIMEOUT_MS });
|
|
36
|
+
if (res.code !== 0)
|
|
37
|
+
throw new Error(`git ${args[0]} failed: ${(res.stderr || "").trim() || `exit ${res.code}`}`);
|
|
38
|
+
return res.stdout;
|
|
39
|
+
}
|
|
40
|
+
export function must(bin, args, env, what) {
|
|
41
|
+
return S.must(bin, args, { env, timeoutMs: SYNC_TIMEOUT_MS, what });
|
|
42
|
+
}
|
|
43
|
+
// Deletes the sandboxes labelled as this builder's (its project, kind and id); its name counts only when unlabelled.
|
|
44
|
+
export function builderSandboxGone(state, { openshell, env }) {
|
|
45
|
+
return S.destroyOwnedSandboxes({ openshell, env, project: state.project, kind: "builder", owner: state.id, legacyNames: [state.sandbox] });
|
|
46
|
+
}
|
|
47
|
+
// Creates this builder's own sandbox; an existing one with the name is never reused or touched.
|
|
48
|
+
export async function createSandbox({ openshell, env, name, policy, labels }) {
|
|
49
|
+
const list = await S.listAllSandboxes({ openshell, env });
|
|
50
|
+
if (list.code !== 0)
|
|
51
|
+
throw new Error(`openshell sandbox list failed: ${list.stderr.trim() || `exit ${list.code}`}`);
|
|
52
|
+
if (list.records.some((s) => s?.name === name))
|
|
53
|
+
throw new Error(`sandbox ${name} already exists`);
|
|
54
|
+
await must(openshell, ["sandbox", "create", "--name", name, "--no-tty", "--detach", "--policy", policy, ...S.labelArgs(labels)], env, "sandbox create");
|
|
55
|
+
}
|
|
56
|
+
export async function uploadBuilderSandbox(state, paths, staged, context) {
|
|
57
|
+
const { openshell, env } = context;
|
|
58
|
+
await createSandbox({ openshell, env, name: state.sandbox, policy: paths.policy, labels: S.sandboxLabels({ project: state.project, kind: "builder", owner: state.id }) });
|
|
59
|
+
updateState(paths, { sandboxCreated: true });
|
|
60
|
+
await upload(state, paths, staged, context);
|
|
61
|
+
}
|
|
62
|
+
export async function upload(state, paths, staged, { openshell, env, log }) {
|
|
63
|
+
const { src, layout } = staged;
|
|
64
|
+
const uploaded = await S.stageAndUpload({
|
|
65
|
+
openshell, env, sandbox: state.sandbox, log,
|
|
66
|
+
staged: { clone: staged.clone, tools: staged.tools, home: staged.home, workdir: staged.workdir, node: src.node, gitCore: src.gitCore, npm: src.npm, nodeModules: src.nodeModules, ntfyToken: src.ntfyToken },
|
|
67
|
+
layout, style: {}, authMissing: `no host ${state.harness} credentials; the harness will ask to log in`, secretTmp: "temmo-builder-auth-",
|
|
68
|
+
});
|
|
69
|
+
if (layout.auth)
|
|
70
|
+
staged.authHash = uploaded.authHash;
|
|
71
|
+
}
|
|
72
|
+
// Host sources of what the sandbox needs; an TEMMO_BUILDER_* override of "none" leaves one out.
|
|
73
|
+
export function toolSources(state, env) {
|
|
74
|
+
const path = env.PATH || "";
|
|
75
|
+
const pick = (key, fallback) => (env[key] === "none" ? "" : env[key] || fallback());
|
|
76
|
+
const real = (p) => { try {
|
|
77
|
+
return p ? realpathSync(p) : "";
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
return "";
|
|
81
|
+
} };
|
|
82
|
+
const node = pick("TEMMO_BUILDER_NODE", () => real(process.execPath));
|
|
83
|
+
const npm = node ? join(dirname(dirname(node)), "lib", "node_modules", "npm") : "";
|
|
84
|
+
const home = env.HOME || homedir();
|
|
85
|
+
const configHome = env.XDG_CONFIG_HOME || join(home, ".config");
|
|
86
|
+
const codexHome = env.CODEX_HOME || join(home, ".codex");
|
|
87
|
+
const cursorBin = findExecutable(cursorBinary(path), path);
|
|
88
|
+
const codexBin = findExecutable("codex", path);
|
|
89
|
+
const claudeHome = env.CLAUDE_CONFIG_DIR || join(home, ".claude");
|
|
90
|
+
const hostHooks = { cursor: join(home, ".cursor", "hooks.json"), codex: join(codexHome, "hooks.json"), claude: join(claudeHome, "settings.json") };
|
|
91
|
+
return {
|
|
92
|
+
git: pick("TEMMO_BUILDER_GIT", () => real(findExecutable("git", path))),
|
|
93
|
+
gitCore: pick("TEMMO_BUILDER_GIT_CORE", () => (existsSync("/usr/lib/git-core") ? "/usr/lib/git-core" : "")),
|
|
94
|
+
oncebitten: pick("TEMMO_BUILDER_ONCEBITTEN", () => real(findOncebitten(env))),
|
|
95
|
+
node, npm: node && existsSync(npm) ? npm : "",
|
|
96
|
+
nodeModules: pick("TEMMO_BUILDER_NODE_MODULES", () => (existsSync(join(state.root, "node_modules")) ? real(join(state.root, "node_modules")) : "")),
|
|
97
|
+
cursorTree: pick("TEMMO_CURSOR_TREE", () => { const r = real(cursorBin); return r && existsSync(join(dirname(r), "node")) ? dirname(r) : ""; }),
|
|
98
|
+
codexTree: pick("TEMMO_CODEX_TREE", () => { const r = real(codexBin); return r ? dirname(dirname(r)) : ""; }),
|
|
99
|
+
cursorAuth: [join(configHome, "cursor", "auth.json"), join(home, ".cursor", "auth.json")].find((p) => existsSync(p)) || "",
|
|
100
|
+
// A CODEX_HOME without a login (a test shell's temp dir, say) falls back to the user's own ~/.codex login.
|
|
101
|
+
codexAuth: [join(codexHome, "auth.json"), join(homedir(), ".codex", "auth.json")].find((p) => existsSync(p)) || "",
|
|
102
|
+
opencodeBin: pick("TEMMO_OPENCODE_BIN", () => real(findExecutable("opencode", path))),
|
|
103
|
+
opencodeAuth: pick("TEMMO_BUILDER_OPENCODE_AUTH", () => {
|
|
104
|
+
const file = join(env.XDG_DATA_HOME || join(home, ".local", "share"), "opencode", "auth.json");
|
|
105
|
+
return existsSync(file) ? file : "";
|
|
106
|
+
}),
|
|
107
|
+
claudeBin: pick("TEMMO_CLAUDE_BIN", () => real(findExecutable("claude", path))),
|
|
108
|
+
claudeAuth: existsSync(join(claudeHome, ".credentials.json")) ? join(claudeHome, ".credentials.json") : "",
|
|
109
|
+
ntfyToken: pick("TEMMO_BUILDER_NTFY_TOKEN", () => S.hostTokenFile(env)),
|
|
110
|
+
hostHooks: hostHooks[state.harness],
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
// Where each harness's install lives on the host and in the sandbox, its launcher, auth file and hook file.
|
|
114
|
+
function harnessLayout(harness, src) {
|
|
115
|
+
const all = (inTree) => (dir) => S.executables(dir).map((f) => `${inTree}/${f}`);
|
|
116
|
+
if (harness === "cursor") {
|
|
117
|
+
return { tree: src.cursorTree, dir: CURSOR_DIR, launcher: "/cursor-agent", name: "cursor-agent", hookDir: ".cursor", auth: src.cursorAuth, authDir: `${HOME_IN}/.config/cursor`, binaries: (inTree) => all(inTree)(src.cursorTree) };
|
|
118
|
+
}
|
|
119
|
+
if (harness === "codex") {
|
|
120
|
+
return { tree: src.codexTree, dir: CODEX_DIR, launcher: "/bin/codex", name: "codex", hookDir: ".codex", auth: src.codexAuth, authDir: `${HOME_IN}/.codex`, binaries: (inTree) => all(`${inTree}/bin`)(join(src.codexTree, "bin")) };
|
|
121
|
+
}
|
|
122
|
+
// Claude Code is one native binary, uploaded as a file into /sandbox/claude.
|
|
123
|
+
if (harness === "opencode") {
|
|
124
|
+
return { tree: src.opencodeBin, file: true, dir: OPENCODE_DIR, launcher: "", name: "opencode", hookDir: ".config/opencode", auth: src.opencodeAuth, authDir: `${HOME_IN}/.local/share/opencode`, binaries: (inTree) => [inTree] };
|
|
125
|
+
}
|
|
126
|
+
return { tree: src.claudeBin, file: true, dir: CLAUDE_DIR, launcher: "", name: "claude", hookDir: ".claude", auth: src.claudeAuth, authDir: `${HOME_IN}/.claude`, binaries: (inTree) => [inTree] };
|
|
127
|
+
}
|
|
128
|
+
export function findOncebitten(env) {
|
|
129
|
+
const cargoBin = join(env.CARGO_HOME || join(env.HOME || homedir(), ".cargo"), "bin");
|
|
130
|
+
return findExecutable("oncebitten", env.PATH) || findExecutable("oncebitten", cargoBin);
|
|
131
|
+
}
|
|
132
|
+
/** The shared libraries `ldd` resolves for a binary, minus glibc's own, which must match the image's loader. */
|
|
133
|
+
export async function sharedLibs(bin, env = process.env) {
|
|
134
|
+
const res = await S.runTool("ldd", [bin], { env });
|
|
135
|
+
if (res.code !== 0)
|
|
136
|
+
return [];
|
|
137
|
+
return [...res.stdout.matchAll(/=>\s+(\/\S+)/g)].map((m) => m[1].replace(/\/+$/, "")).filter((p) => {
|
|
138
|
+
if (GLIBC_LIB.test(basename(p)))
|
|
139
|
+
return false;
|
|
140
|
+
try {
|
|
141
|
+
return statSync(realpathSync(p)).isFile();
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
// Whether the host has the harness's install, by the same lookup the sandbox staging uses.
|
|
149
|
+
export function hasInstall(harness, state, env) {
|
|
150
|
+
return Boolean(harnessLayout(harness, toolSources({ ...state, harness }, env)).tree);
|
|
151
|
+
}
|
|
152
|
+
// Builds the host-side staging: a self-contained clone, the tools tree, the sandbox home and the policy.
|
|
153
|
+
export async function stage(state, paths, { env, log, resume }) {
|
|
154
|
+
rmSync(paths.stage, { recursive: true, force: true });
|
|
155
|
+
mkdirSync(paths.stage, { recursive: true });
|
|
156
|
+
const name = basename(state.worktree);
|
|
157
|
+
const clone = join(paths.stage, name);
|
|
158
|
+
// The worktree's .git is a file pointing into the host repo; the sandbox gets a clone with its own .git.
|
|
159
|
+
resume ??= state.resume;
|
|
160
|
+
if (resume)
|
|
161
|
+
cpSync(resume, clone, { recursive: true, verbatimSymlinks: true });
|
|
162
|
+
else
|
|
163
|
+
await git(paths.stage, ["clone", "--quiet", "--no-hardlinks", "--single-branch", "--branch", state.branch, state.root, clone], env);
|
|
164
|
+
if (!resume)
|
|
165
|
+
await git(clone, ["remote", "remove", "origin"], env);
|
|
166
|
+
await git(clone, ["branch", "--force", BASE_REF, state.base], env);
|
|
167
|
+
// The sandbox's node_modules is a symlink, which the repo's "node_modules/" pattern does not ignore.
|
|
168
|
+
mkdirSync(join(clone, ".git", "info"), { recursive: true });
|
|
169
|
+
appendFileSync(join(clone, ".git", "info", "exclude"), "\nnode_modules\n");
|
|
170
|
+
for (const key of ["user.name", "user.email"]) {
|
|
171
|
+
const value = (await S.runTool("git", ["-C", state.root, "config", key], { env })).stdout.trim();
|
|
172
|
+
if (value)
|
|
173
|
+
await git(clone, ["config", key, value], env);
|
|
174
|
+
}
|
|
175
|
+
const tools = join(paths.stage, "tools");
|
|
176
|
+
mkdirSync(join(tools, "bin"), { recursive: true });
|
|
177
|
+
mkdirSync(join(tools, "tasks"), { recursive: true });
|
|
178
|
+
writeFileSync(join(tools, "bin", "temmo"), TEMMO_SHIM, { mode: 0o755 });
|
|
179
|
+
writeFileSync(join(tools, "bin", "ob-capture"), CAPTURE_SHIM, { mode: 0o755 });
|
|
180
|
+
for (const t of openTasks(state.project, env).list())
|
|
181
|
+
writeFileSync(join(tools, "tasks", `${t.id}.json`), `${JSON.stringify(t, null, 2)}\n`);
|
|
182
|
+
const src = toolSources(state, env);
|
|
183
|
+
const toolchain = await stageToolchain(paths.stage, env, log);
|
|
184
|
+
const layout = harnessLayout(state.harness, src);
|
|
185
|
+
if (!layout.tree)
|
|
186
|
+
throw new Error(`no ${state.harness} install found on the host`);
|
|
187
|
+
const inTree = `${layout.dir}/${basename(layout.tree)}`;
|
|
188
|
+
// Relative, so the link resolves wherever the sandbox root is mounted.
|
|
189
|
+
symlinkSync(`../..${inTree.slice(SANDBOX.length)}${layout.launcher}`, join(tools, "bin", layout.name));
|
|
190
|
+
const home = join(paths.stage, "home");
|
|
191
|
+
const hookDir = join(home, layout.hookDir);
|
|
192
|
+
mkdirSync(hookDir, { recursive: true });
|
|
193
|
+
const events = state.harness === "opencode" ? [] : hostCaptureEvents(state.harness, src.hostHooks);
|
|
194
|
+
const workdir = `${WORK}/${name}`;
|
|
195
|
+
const builderCode = Boolean(src.node && ["cursor", "opencode"].includes(state.harness));
|
|
196
|
+
if (src.node && (builderCode || state.harness === "claude")) {
|
|
197
|
+
stageApp(tools);
|
|
198
|
+
}
|
|
199
|
+
if (builderCode) {
|
|
200
|
+
const command = `${TOOLS}/node/bin/node`;
|
|
201
|
+
const args = [`${APP_IN}/${CODE_MCP}`, "--builder-local", workdir];
|
|
202
|
+
const servers = { "temmo-code": state.harness === "cursor"
|
|
203
|
+
? { command, args }
|
|
204
|
+
: { type: "local", command: [command, ...args], codemode: false, timeout: { execution: 150_000 } } };
|
|
205
|
+
const cursor = state.harness === "cursor";
|
|
206
|
+
writeFileSync(join(hookDir, cursor ? "mcp.json" : "opencode.json"), JSON.stringify(cursor ? { mcpServers: servers } : { mcp: { servers } }));
|
|
207
|
+
}
|
|
208
|
+
if (state.harness === "claude") {
|
|
209
|
+
const codeMode = existsSync(join(APP_ROOT, CODE_MCP));
|
|
210
|
+
writeFileSync(join(hookDir, basename(CLAUDE_SETTINGS_IN)), `${JSON.stringify(claudeSettings(events), null, 2)}\n`);
|
|
211
|
+
writeFileSync(join(hookDir, basename(CLAUDE_MCP_IN)), `${JSON.stringify(claudeMcpConfig({ node: src.node, project: state.project, codeMode }), null, 2)}\n`);
|
|
212
|
+
log(`claude: lean settings, MCP servers ${Object.keys(claudeMcpConfig({ node: src.node, project: state.project, codeMode }).mcpServers).join(", ") || "none"}`);
|
|
213
|
+
}
|
|
214
|
+
else if (state.harness !== "opencode") {
|
|
215
|
+
writeFileSync(join(hookDir, "hooks.json"), `${JSON.stringify(captureHooks(state.harness, events), null, 2)}\n`);
|
|
216
|
+
}
|
|
217
|
+
// Hooks on and the worktree trusted, as the host's own config has them; whether `codex exec` then fires them is UNVERIFIED.
|
|
218
|
+
if (state.harness === "codex")
|
|
219
|
+
writeFileSync(join(hookDir, "config.toml"), `${codexFeaturesToml({ hooks: true })}\n[projects."${workdir}"]\ntrust_level = "trusted"\n`);
|
|
220
|
+
// Every publishing sandbox gets the token, not only Claude. tokenFile is the uploaded path, never the host path.
|
|
221
|
+
if (src.ntfyToken || (state.harness === "claude" && src.node)) {
|
|
222
|
+
const config = join(home, ".config", "temmo");
|
|
223
|
+
mkdirSync(config, { recursive: true });
|
|
224
|
+
writeFileSync(join(config, "defaults.json"), sandboxDefaults({ tokenFile: src.ntfyToken ? NTFY_TOKEN_IN : undefined }));
|
|
225
|
+
}
|
|
226
|
+
const binaries = layout.binaries(inTree);
|
|
227
|
+
const ntfyBinaries = src.node ? [`${TOOLS}/node/bin/node`] : [];
|
|
228
|
+
writeFileSync(paths.policy, `${JSON.stringify(builderPolicy({ harness: state.harness, binaries, ntfyBinaries }), null, 2)}\n`);
|
|
229
|
+
// No SSL_CERT_FILE: the login shell points it at the CA that OpenShell terminates TLS with.
|
|
230
|
+
const execEnv = {
|
|
231
|
+
HOME: HOME_IN, XDG_CONFIG_HOME: `${HOME_IN}/.config`, CODEX_HOME: `${HOME_IN}/.codex`, PATH: SANDBOX_PATH, TMPDIR: "/tmp",
|
|
232
|
+
TEMMO_PROJECT: state.project,
|
|
233
|
+
TEMMO_BUILDER_OUT: OUT, TEMMO_BUILDER_TASKS: `${TOOLS}/tasks`, TEMMO_BUILDER: state.id, TEMMO_BUILDER_TASK: state.task,
|
|
234
|
+
...(src.gitCore ? { GIT_EXEC_PATH: `${TOOLS}/git-core` } : {}),
|
|
235
|
+
...(toolchain.libs.length ? { LD_LIBRARY_PATH: `${TOOLS}/lib` } : {}),
|
|
236
|
+
...(state.harness === "claude" ? CLAUDE_ENV : {}),
|
|
237
|
+
...(state.harness === "cursor" ? CURSOR_ENV : {}),
|
|
238
|
+
};
|
|
239
|
+
return { clone, tools, home, src, layout, workdir, execEnv, builderCode };
|
|
240
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// Builder identity, state file and constants: the paths and phases every other builder module shares.
|
|
2
|
+
import { writeJson } from "../store.js";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
import { readFileSync } from "node:fs";
|
|
5
|
+
import { dirname, join } from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { stateDir } from "../config.js";
|
|
8
|
+
import { FALLBACK_MODEL } from "../harness.js";
|
|
9
|
+
export const CLI_SCRIPT = fileURLToPath(new URL("../../scripts/temmo.mjs", import.meta.url));
|
|
10
|
+
// The temmo checkout this CLI runs from supplies the builder MCP servers.
|
|
11
|
+
export const APP_ROOT = dirname(dirname(CLI_SCRIPT));
|
|
12
|
+
export const BUILDER_HARNESSES = Object.freeze(["cursor", "codex", "claude", "opencode"]);
|
|
13
|
+
export const DEFAULT_MODEL = { cursor: FALLBACK_MODEL, codex: "gpt-6.2-sol", claude: "haiku", opencode: "opencode/deepseek-v4.1-flash-free" };
|
|
14
|
+
const UNIT_PREFIX = "builder-";
|
|
15
|
+
export const ID_RE = /^[a-z0-9][a-z0-9-]{0,62}$/;
|
|
16
|
+
export const BASE_REF = "main";
|
|
17
|
+
// openshell 0.1.9 refuses a sandbox name over 19 characters (measured 2026-11-22).
|
|
18
|
+
const SANDBOX_PREFIX_MAX = 8;
|
|
19
|
+
export const BUILDER_START_TIMEOUT_MS = 15 * 60_000;
|
|
20
|
+
export const GIT_TIMEOUT_MS = 600_000;
|
|
21
|
+
export const SYNC_TIMEOUT_MS = 900_000;
|
|
22
|
+
export const FINAL_PHASES = ["finished", "failed", "stopped"];
|
|
23
|
+
export const LIVE_STATES = ["active", "activating", "deactivating", "reloading"];
|
|
24
|
+
/** Unit, sandbox, worktree and state paths for one builder id. */
|
|
25
|
+
export function builderPaths(id, env = process.env) {
|
|
26
|
+
const dir = join(stateDir(env), "builders", id);
|
|
27
|
+
return {
|
|
28
|
+
unit: `${UNIT_PREFIX}${id}`,
|
|
29
|
+
sandbox: builderSandbox(id),
|
|
30
|
+
worktree: join(env.TEMMO_BUILDER_WORKTREES || join(stateDir(env), "worktrees"), `ai-collab-wt-${id}`),
|
|
31
|
+
dir, state: join(dir, "state.json"), brief: join(dir, "brief.md"), policy: join(dir, "policy.yaml"),
|
|
32
|
+
stage: join(dir, "stage"), back: join(dir, "back"), out: join(dir, "out"),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/** A builder's sandbox name: b-<first 8 of the id>-<6 hex of its hash>, at most 17 characters. */
|
|
36
|
+
export function builderSandbox(id) {
|
|
37
|
+
const hash = createHash("sha256").update(id).digest("hex").slice(0, 6);
|
|
38
|
+
return `b-${id.slice(0, SANDBOX_PREFIX_MAX).replace(/-+$/, "")}-${hash}`;
|
|
39
|
+
}
|
|
40
|
+
/** The builder id for a task id: lowercase letters, digits and dashes, at most 63 characters. */
|
|
41
|
+
export function builderId(taskId) {
|
|
42
|
+
return String(taskId).toLowerCase().replace(/[^a-z0-9-]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 63);
|
|
43
|
+
}
|
|
44
|
+
export function readState(paths) {
|
|
45
|
+
return JSON.parse(readFileSync(paths.state, "utf8"));
|
|
46
|
+
}
|
|
47
|
+
export function writeState(paths, state) {
|
|
48
|
+
writeJson(paths.state, state, { indent: 2 });
|
|
49
|
+
}
|
|
50
|
+
export function updateState(paths, patch) {
|
|
51
|
+
let state = {};
|
|
52
|
+
try {
|
|
53
|
+
state = readState(paths);
|
|
54
|
+
}
|
|
55
|
+
catch { /* first write */ }
|
|
56
|
+
writeState(paths, { ...state, ...patch, updated: Date.now() });
|
|
57
|
+
}
|
|
58
|
+
export function builderExited(state) {
|
|
59
|
+
if (FINAL_PHASES.includes(state.phase))
|
|
60
|
+
return true;
|
|
61
|
+
if (state.owned || !Number.isSafeInteger(state.pid) || state.pid <= 0)
|
|
62
|
+
return false;
|
|
63
|
+
try {
|
|
64
|
+
process.kill(state.pid, 0);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
return error.code === "ESRCH";
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
export function oneLine(text) {
|
|
72
|
+
return String(text).replace(/\s+/g, " ").trim();
|
|
73
|
+
}
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
// Unit launch and the durable supervisor that runs a builder inside a systemd unit.
|
|
2
|
+
import * as S from "../sandbox.js";
|
|
3
|
+
import { isProjectPaused, PROJECT_PAUSED } from "../project-pause.js";
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { basename, join } from "node:path";
|
|
7
|
+
import { autoDefaults } from "../config.js";
|
|
8
|
+
import { projectRoot, PROMPT_MAX_CHARS } from "../sessions/launch.js";
|
|
9
|
+
import { modelShape } from "../harness.js";
|
|
10
|
+
import { runnerRequest, runnerSocket } from "../runner-request.js";
|
|
11
|
+
import { RUN_ID } from "../pattern-runs.js";
|
|
12
|
+
import { openTasks } from "../tasks.js";
|
|
13
|
+
import { BUILDER_DIR_CAP, builderLiveRefusal, cleanBuilders } from "../builder-clean.js";
|
|
14
|
+
import { WORK } from "../sandbox.js";
|
|
15
|
+
import { CLI_SCRIPT, BUILDER_HARNESSES, DEFAULT_MODEL, ID_RE, BASE_REF, BUILDER_START_TIMEOUT_MS, GIT_TIMEOUT_MS, FINAL_PHASES, LIVE_STATES, builderPaths, builderId, readState, writeState, updateState, oneLine } from "./state.js";
|
|
16
|
+
import { git, createSandbox, upload, stage } from "./stage.js";
|
|
17
|
+
import { briefNotes, cursorLimitDetail, retryLimitedBuilder, runSandboxAgent, collect, cleanupBuilderSandbox } from "./run.js";
|
|
18
|
+
import { prepareSettlement, publishSettlement } from "./settle.js";
|
|
19
|
+
export async function startBuilder(flags, { env, cfg, readBrief }) {
|
|
20
|
+
const defaults = autoDefaults(env);
|
|
21
|
+
const order = defaults?.builderOrder;
|
|
22
|
+
const preferred = Array.isArray(order) ? order.find((name) => BUILDER_HARNESSES.includes(name)) : undefined;
|
|
23
|
+
const harness = flags.harness ?? preferred ?? "codex";
|
|
24
|
+
if (!BUILDER_HARNESSES.includes(harness))
|
|
25
|
+
throw new Error("Unknown builder harness");
|
|
26
|
+
if (flags.run !== undefined && !RUN_ID.test(flags.run))
|
|
27
|
+
throw new Error("invalid run id");
|
|
28
|
+
if (!cfg.project)
|
|
29
|
+
throw new Error("TEMMO_PROJECT is not set and no project matches this folder");
|
|
30
|
+
if (isProjectPaused(cfg))
|
|
31
|
+
throw new Error(PROJECT_PAUSED);
|
|
32
|
+
const task = openTasks(cfg.project, env).list().find((t) => t.id === flags.task);
|
|
33
|
+
if (!task)
|
|
34
|
+
throw new Error(`no task ${flags.task} in project ${cfg.project}`);
|
|
35
|
+
const id = builderId(flags.task);
|
|
36
|
+
if (!ID_RE.test(id))
|
|
37
|
+
throw new Error(`task id ${flags.task} gives no usable builder id`);
|
|
38
|
+
const paths = builderPaths(id, env);
|
|
39
|
+
const brief = (readBrief ? readBrief() : flags.brief).trim();
|
|
40
|
+
if (!brief)
|
|
41
|
+
throw new Error("Builder brief is empty");
|
|
42
|
+
const branch = flags.branch || `builder/${id}`;
|
|
43
|
+
const model = flags.model || DEFAULT_MODEL[harness];
|
|
44
|
+
const limited = cursorLimitDetail(harness, model, env);
|
|
45
|
+
if (limited)
|
|
46
|
+
throw new Error(limited);
|
|
47
|
+
const workdir = `${WORK}/${basename(paths.worktree)}`;
|
|
48
|
+
const prompt = `${brief}\n${briefNotes({ task: task.id, branch, workdir })}`;
|
|
49
|
+
if (!readBrief) {
|
|
50
|
+
if (!modelShape(harness, model))
|
|
51
|
+
throw new Error("Invalid builder model");
|
|
52
|
+
if (brief.startsWith("-") || brief.includes("\0"))
|
|
53
|
+
throw new Error("Start the builder brief with a heading or sentence and omit NUL characters");
|
|
54
|
+
if (harness === "codex" && prompt.trim().length > PROMPT_MAX_CHARS) {
|
|
55
|
+
throw new Error(`Builder brief and sandbox notes must fit within ${PROMPT_MAX_CHARS} characters`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (existsSync(paths.worktree))
|
|
59
|
+
throw new Error(`worktree ${paths.worktree} already exists`);
|
|
60
|
+
if (LIVE_STATES.includes(await unitState(paths.unit, env)))
|
|
61
|
+
throw new Error(`unit ${paths.unit} is already running`);
|
|
62
|
+
const crowded = builderLiveRefusal(env);
|
|
63
|
+
if (crowded)
|
|
64
|
+
throw new Error(crowded);
|
|
65
|
+
const capped = await S.sandboxCapRefusal({ openshell: env.TEMMO_OPENSHELL_BIN || "openshell", env, name: paths.sandbox });
|
|
66
|
+
if (capped)
|
|
67
|
+
throw new Error(capped);
|
|
68
|
+
// One free slot: this start adds a directory, and the count must still end at the cap.
|
|
69
|
+
await cleanBuilders(env, { cap: BUILDER_DIR_CAP - 1 });
|
|
70
|
+
const root = projectRoot(cfg.project, env);
|
|
71
|
+
await git(root, ["worktree", "add", "--quiet", paths.worktree, "-b", branch, BASE_REF], env);
|
|
72
|
+
const nodeModules = join(root, "node_modules");
|
|
73
|
+
if (existsSync(nodeModules))
|
|
74
|
+
symlinkSync(nodeModules, join(paths.worktree, "node_modules"));
|
|
75
|
+
const base = (await git(root, ["rev-parse", BASE_REF], env)).trim();
|
|
76
|
+
rmSync(paths.dir, { recursive: true, force: true });
|
|
77
|
+
mkdirSync(paths.dir, { recursive: true });
|
|
78
|
+
writeFileSync(paths.brief, prompt);
|
|
79
|
+
return launchBuilder(paths, {
|
|
80
|
+
...(flags.run ? { run: flags.run } : {}), id, task: task.id, project: cfg.project, harness, model, branch, base, root,
|
|
81
|
+
worktree: paths.worktree, unit: paths.unit, sandbox: paths.sandbox,
|
|
82
|
+
}, { env });
|
|
83
|
+
}
|
|
84
|
+
export async function launchBuilder(paths, state, { env }) {
|
|
85
|
+
const socket = runnerSocket(env);
|
|
86
|
+
const owned = state.harness === "codex" && await runnerRequest("GET", "/v1/health", undefined, { socket, timeoutMs: 1000 }).then(() => true, (error) => {
|
|
87
|
+
if (["ENOENT", "ECONNREFUSED"].includes(error.code))
|
|
88
|
+
return false;
|
|
89
|
+
throw error;
|
|
90
|
+
});
|
|
91
|
+
writeState(paths, { ...state, owned, phase: "starting", started: Date.now(), attempt: randomUUID() });
|
|
92
|
+
try {
|
|
93
|
+
return await dispatchBuilder(paths, state, { env, owned, socket });
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
const { status } = error;
|
|
97
|
+
if (readState(paths).phase === "starting" && (!owned || (status && status !== 504))) {
|
|
98
|
+
updateState(paths, { phase: "failed", ended: Date.now(), error: error.message });
|
|
99
|
+
}
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
async function dispatchBuilder(paths, state, { env, owned, socket }) {
|
|
104
|
+
const { id, task, project, model, branch, run } = state;
|
|
105
|
+
if (owned) {
|
|
106
|
+
const { status, body } = await runnerRequest("POST", "/v1/sessions", {
|
|
107
|
+
project, harness: "codex", place: "openshell", model, taskId: task,
|
|
108
|
+
builder: id, prompt: state.prompt ?? readFileSync(paths.brief, "utf8"),
|
|
109
|
+
}, { socket, timeoutMs: BUILDER_START_TIMEOUT_MS + 10_000 });
|
|
110
|
+
if (status !== 201)
|
|
111
|
+
throw Object.assign(new Error(body?.error || "runner start returned HTTP " + status), { status });
|
|
112
|
+
return { session: body.session, task, sandbox: paths.sandbox, worktree: paths.worktree, branch };
|
|
113
|
+
}
|
|
114
|
+
const started = await S.runTool(env.TEMMO_SYSTEMD_RUN_BIN || "systemd-run", unitArgv(paths, project, task, env), { env, timeoutMs: GIT_TIMEOUT_MS });
|
|
115
|
+
if (started.code !== 0) {
|
|
116
|
+
updateState(paths, { phase: "failed", error: `systemd-run: ${(started.stderr || "").trim()}` });
|
|
117
|
+
throw new Error(`systemd-run failed: ${(started.stderr || "").trim() || `exit ${started.code}`}`);
|
|
118
|
+
}
|
|
119
|
+
return { unit: paths.unit, task, sandbox: paths.sandbox, worktree: paths.worktree, branch };
|
|
120
|
+
}
|
|
121
|
+
// systemd-run argv: a transient user unit, so a UI or runner restart cannot kill the builder.
|
|
122
|
+
function unitArgv(paths, project, taskId, env) {
|
|
123
|
+
const pass = Object.entries(env).filter(([k]) => k === "PATH" || k === "HOME" || k === "CARGO_HOME" || k.startsWith("TEMMO_") || k.startsWith("XDG_") || k === "CODEX_HOME");
|
|
124
|
+
const setenv = [...pass.filter(([k]) => k !== "TEMMO_PROJECT" && k !== "TEMMO_BUILDER_TASK"), ["TEMMO_PROJECT", project], ["TEMMO_BUILDER_TASK", taskId]].map(([k, v]) => `--setenv=${k}=${v}`);
|
|
125
|
+
return [
|
|
126
|
+
"--user", "--collect", "--quiet", "--property=TimeoutStopSec=40min", "--property=KillMode=mixed", "--property=Restart=on-abnormal", `--unit=${paths.unit}`, `--working-directory=${paths.worktree}`,
|
|
127
|
+
`--description=temmo builder ${basename(paths.dir)} (sandboxed)`, ...setenv,
|
|
128
|
+
process.execPath, CLI_SCRIPT, "builder", "run", basename(paths.dir),
|
|
129
|
+
];
|
|
130
|
+
}
|
|
131
|
+
// The supervisor inside the unit: sandbox, run, copy back, commit, task status, cleanup.
|
|
132
|
+
export async function runBuilder(id, { env, out, err }) {
|
|
133
|
+
const paths = builderPaths(id, env);
|
|
134
|
+
const state = readState(paths);
|
|
135
|
+
if (state.owned)
|
|
136
|
+
throw new Error("This builder is managed by the runner");
|
|
137
|
+
const log = (line) => out.write(`builder ${id}: ${line}\n`);
|
|
138
|
+
const openshell = env.TEMMO_OPENSHELL_BIN || "openshell";
|
|
139
|
+
let child = null;
|
|
140
|
+
let stopping = false;
|
|
141
|
+
const onTerm = () => {
|
|
142
|
+
stopping = true;
|
|
143
|
+
child?.kill("SIGTERM");
|
|
144
|
+
};
|
|
145
|
+
const { openDurable } = await import("../durable.js");
|
|
146
|
+
const { acquireLock } = await import("../../runner/lock.js");
|
|
147
|
+
const release = acquireLock(join(paths.dir, "workflow.lock"));
|
|
148
|
+
let durable;
|
|
149
|
+
process.once("SIGTERM", onTerm);
|
|
150
|
+
try {
|
|
151
|
+
const journal = state.attempt ? "workflow-" + state.attempt + ".db" : "workflow.db";
|
|
152
|
+
durable = openDurable(join(paths.dir, journal));
|
|
153
|
+
durable.register("builder", async (wf) => {
|
|
154
|
+
const context = { openshell, env, log, onChild: (proc) => { child = proc; } };
|
|
155
|
+
let exitCode = null;
|
|
156
|
+
try {
|
|
157
|
+
const current = readState(paths);
|
|
158
|
+
if (!current.settlement && !FINAL_PHASES.includes(current.phase)) {
|
|
159
|
+
updateState(paths, { pid: process.pid });
|
|
160
|
+
const staged = await wf.step("stage", async () => {
|
|
161
|
+
updateState(paths, { phase: "staging" });
|
|
162
|
+
return stage(state, paths, context);
|
|
163
|
+
});
|
|
164
|
+
await wf.step("sandbox.create", async () => {
|
|
165
|
+
await createSandbox({ openshell, env, name: state.sandbox, policy: paths.policy, labels: S.sandboxLabels({ project: state.project, kind: "builder", owner: state.id }) });
|
|
166
|
+
updateState(paths, { phase: "uploading", sandboxCreated: true });
|
|
167
|
+
}, { onCut: () => {
|
|
168
|
+
if (!readState(paths).sandboxCreated)
|
|
169
|
+
throw new Error("builder interrupted during sandbox creation; inspect " + state.sandbox);
|
|
170
|
+
} });
|
|
171
|
+
const authHash = await wf.step("sandbox.upload", async () => {
|
|
172
|
+
await upload(state, paths, staged, context);
|
|
173
|
+
updateState(paths, { workspaceUploaded: true });
|
|
174
|
+
return staged.authHash;
|
|
175
|
+
});
|
|
176
|
+
staged.authHash = authHash;
|
|
177
|
+
await wf.step("builder.ready", async () => {
|
|
178
|
+
if (stopping)
|
|
179
|
+
return;
|
|
180
|
+
const tasks = openTasks(state.project, env);
|
|
181
|
+
if (state.prompt || state.run || tasks.get(state.task).status !== "building") {
|
|
182
|
+
await tasks.update(state.task, { status: "building", ...(state.prompt ? { owner: undefined } : {}), ...(state.run ? { run: state.run } : {}) });
|
|
183
|
+
}
|
|
184
|
+
if (!stopping)
|
|
185
|
+
updateState(paths, { phase: "running" });
|
|
186
|
+
});
|
|
187
|
+
const attempt = await wf.step("agent.run", async () => {
|
|
188
|
+
if (stopping)
|
|
189
|
+
return { exitCode: 143 };
|
|
190
|
+
const result = await runSandboxAgent(state, paths, staged, context);
|
|
191
|
+
return stopping ? { exitCode: 143 } : result;
|
|
192
|
+
}, {
|
|
193
|
+
onCut: () => ({ exitCode: 143 }),
|
|
194
|
+
});
|
|
195
|
+
exitCode = attempt.exitCode;
|
|
196
|
+
await wf.step("sandbox.collect", async () => {
|
|
197
|
+
updateState(paths, { phase: "collecting", exitCode });
|
|
198
|
+
await collect(state, paths, staged, context);
|
|
199
|
+
});
|
|
200
|
+
await wf.step("prepare settlement", async () => {
|
|
201
|
+
if (attempt.usageLimit && exitCode !== 143)
|
|
202
|
+
await retryLimitedBuilder(state, paths, context);
|
|
203
|
+
else
|
|
204
|
+
await prepareSettlement(state, paths, exitCode, { ...context, limitDetail: attempt.limitDetail });
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
catch (error) {
|
|
209
|
+
err.write("builder " + id + ": " + error.message + "\n");
|
|
210
|
+
const detail = "builder " + state.unit + " failed: " + oneLine(error.message);
|
|
211
|
+
if (!readState(paths).settlement) {
|
|
212
|
+
updateState(paths, { exitCode, settlement: { status: "blocked", detail, at: Date.now(), error: error.message } });
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
// The persisted verdict makes publication repeatable even after the staging files are removed.
|
|
216
|
+
try {
|
|
217
|
+
const final = readState(paths);
|
|
218
|
+
if (final.settlement && !FINAL_PHASES.includes(final.phase)) {
|
|
219
|
+
await publishSettlement(state, paths, env);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
finally {
|
|
223
|
+
await cleanupBuilderSandbox(state, paths, context);
|
|
224
|
+
}
|
|
225
|
+
return Number(Boolean(readState(paths).settlement?.error));
|
|
226
|
+
});
|
|
227
|
+
if (!durable.status(id))
|
|
228
|
+
return await durable.startWorkflow("builder", id, {});
|
|
229
|
+
durable.recover();
|
|
230
|
+
const result = await durable.result(id);
|
|
231
|
+
const status = durable.status(id);
|
|
232
|
+
if (status?.status === "ERROR")
|
|
233
|
+
throw new Error(String(status.error));
|
|
234
|
+
return result;
|
|
235
|
+
}
|
|
236
|
+
finally {
|
|
237
|
+
process.removeListener("SIGTERM", onTerm);
|
|
238
|
+
try {
|
|
239
|
+
await durable?.close();
|
|
240
|
+
}
|
|
241
|
+
finally {
|
|
242
|
+
release();
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
// Stops a builder this command started (it has a state file), then deletes its sandbox.
|
|
247
|
+
export async function stopBuilder(id, { env, out, err }) {
|
|
248
|
+
const paths = builderPaths(builderId(id), env);
|
|
249
|
+
let state;
|
|
250
|
+
try {
|
|
251
|
+
state = readState(paths);
|
|
252
|
+
}
|
|
253
|
+
catch {
|
|
254
|
+
err.write(`temmo: builder stop: no builder ${id} started by temmo builder start\n`);
|
|
255
|
+
return 1;
|
|
256
|
+
}
|
|
257
|
+
if (state.owned) {
|
|
258
|
+
if (!state.address)
|
|
259
|
+
throw new Error("builder session has not been claimed");
|
|
260
|
+
const route = state.address.replace("#", "/");
|
|
261
|
+
const { status, body } = await runnerRequest("DELETE", "/v1/sessions/" + route, undefined, { socket: runnerSocket(env) });
|
|
262
|
+
if (status !== 200)
|
|
263
|
+
throw new Error(body?.error || "runner stop returned HTTP " + status);
|
|
264
|
+
out.write([state.address, body.status].join("\t") + "\n");
|
|
265
|
+
return 0;
|
|
266
|
+
}
|
|
267
|
+
const stopped = await S.runTool(env.TEMMO_SYSTEMCTL_BIN || "systemctl", ["--user", "stop", state.unit], { env, timeoutMs: 41 * 60_000 });
|
|
268
|
+
// A unit already gone (exited, or unloaded after a reboot) refuses the stop; its supervisor never ran the sandbox cleanup, so run it here.
|
|
269
|
+
const gone = stopped.code !== 0 && ["inactive", "failed"].includes(await unitState(state.unit, env));
|
|
270
|
+
if (gone)
|
|
271
|
+
await cleanupBuilderSandbox(state, paths, { openshell: env.TEMMO_OPENSHELL_BIN || "openshell", env, log: (line) => out.write(`${line}\n`) });
|
|
272
|
+
const current = readState(paths);
|
|
273
|
+
if (stopped.code === 0 || gone) {
|
|
274
|
+
if (current.started === state.started && !FINAL_PHASES.includes(current.phase)) {
|
|
275
|
+
updateState(paths, { phase: "stopped", ended: Date.now() });
|
|
276
|
+
}
|
|
277
|
+
await cleanBuilders(env);
|
|
278
|
+
const sandbox = current.sandboxDeleted ? "sandbox deleted" : current.sandboxCreated ? "sandbox retained" : "no sandbox";
|
|
279
|
+
out.write([state.unit, "stopped", sandbox].join("\t") + "\n");
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
out.write([state.unit, "stop failed: " + (stopped.stderr || "").trim()].join("\t") + "\n");
|
|
283
|
+
}
|
|
284
|
+
return stopped.code === 0 || gone ? 0 : 1;
|
|
285
|
+
}
|
|
286
|
+
export async function unitState(unit, env) {
|
|
287
|
+
const res = await S.runTool(env.TEMMO_SYSTEMCTL_BIN || "systemctl", ["--user", "show", "-p", "ActiveState", "--value", unit], { env });
|
|
288
|
+
return res.code === 0 ? res.stdout.trim() || "unknown" : "unknown";
|
|
289
|
+
}
|