@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,119 @@
|
|
|
1
|
+
// Scripted in-process adapter for tests: "approve: <cmd>" asks for approval, "hang" runs until interrupted, else it echoes.
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { EventType } from "@ag-ui/core";
|
|
4
|
+
import { sessionLabel } from "../../lib/config.js";
|
|
5
|
+
import { APPROVAL_REQUESTED, PERMISSION_MODES } from "../contract.js";
|
|
6
|
+
/** A fake Adapter; calls records every adapter call for assertions. */
|
|
7
|
+
export function createFakeAdapter({ harness = "codex", capabilities = {}, openError = null, compact = true } = {}) {
|
|
8
|
+
const calls = [];
|
|
9
|
+
let runs = 0;
|
|
10
|
+
function makeHandle(nativeId, ctx) {
|
|
11
|
+
const threadId = `${harness}#${sessionLabel(nativeId)}`;
|
|
12
|
+
let running = null;
|
|
13
|
+
const steered = [];
|
|
14
|
+
async function run(runId, input, settled) {
|
|
15
|
+
ctx.emit({
|
|
16
|
+
type: EventType.RUN_STARTED, threadId, runId,
|
|
17
|
+
input: { threadId, runId, tools: [], context: [], messages: [{ id: `${runId}:input`, role: "user", content: input.text }] },
|
|
18
|
+
});
|
|
19
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
20
|
+
const approve = /approve: (.+)/.exec(input.text);
|
|
21
|
+
let outcome = "success";
|
|
22
|
+
if (approve) {
|
|
23
|
+
const toolCallId = `c-${runId}`;
|
|
24
|
+
ctx.emit({ type: EventType.TOOL_CALL_CHUNK, toolCallId, toolCallName: "Bash", delta: JSON.stringify({ command: approve[1] }, null, 2) });
|
|
25
|
+
running.interruptId = `i-${runId}`;
|
|
26
|
+
ctx.emit({
|
|
27
|
+
type: EventType.CUSTOM, name: APPROVAL_REQUESTED,
|
|
28
|
+
value: { interruptId: running.interruptId, reason: "tool_approval", toolCallId, toolCallName: "Bash", summary: approve[1] },
|
|
29
|
+
});
|
|
30
|
+
const answer = await settled;
|
|
31
|
+
if (answer.interrupted)
|
|
32
|
+
outcome = "cancelled";
|
|
33
|
+
else {
|
|
34
|
+
const failed = answer.behavior !== "allow";
|
|
35
|
+
ctx.emit({
|
|
36
|
+
type: EventType.TOOL_CALL_RESULT, toolCallId, messageId: `${toolCallId}:result`, role: "tool",
|
|
37
|
+
content: failed ? "denied by the operator" : `ran ${approve[1]}`,
|
|
38
|
+
metadata: { aiCollab: { toolCallName: "Bash", failed } },
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else if (/\bhang\b/.test(input.text)) {
|
|
43
|
+
if ((await settled).interrupted)
|
|
44
|
+
outcome = "cancelled";
|
|
45
|
+
}
|
|
46
|
+
if (outcome === "success")
|
|
47
|
+
ctx.emit({
|
|
48
|
+
type: EventType.TEXT_MESSAGE_CHUNK, messageId: `m-${runId}`, role: "assistant", delta: `echo: ${input.text.slice(0, 200)}`,
|
|
49
|
+
});
|
|
50
|
+
running = null;
|
|
51
|
+
ctx.emit({ type: EventType.RUN_FINISHED, threadId, runId, outcome: { type: outcome } });
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
nativeId,
|
|
55
|
+
steered,
|
|
56
|
+
async send(input) {
|
|
57
|
+
calls.push({ op: "send", nativeId, text: input.text });
|
|
58
|
+
if (running)
|
|
59
|
+
throw new Error("fake: a run is already running");
|
|
60
|
+
const runId = `r${++runs}`;
|
|
61
|
+
let release;
|
|
62
|
+
const settled = new Promise((resolve) => { release = resolve; });
|
|
63
|
+
running = { runId, release };
|
|
64
|
+
setImmediate(() => run(runId, input, settled));
|
|
65
|
+
return { runId };
|
|
66
|
+
},
|
|
67
|
+
async steer(input) {
|
|
68
|
+
calls.push({ op: "steer", nativeId, text: input.text });
|
|
69
|
+
steered.push(input.text);
|
|
70
|
+
},
|
|
71
|
+
async interrupt() {
|
|
72
|
+
calls.push({ op: "interrupt", nativeId });
|
|
73
|
+
running?.release({ interrupted: true });
|
|
74
|
+
},
|
|
75
|
+
async respond(interruptId, decision) {
|
|
76
|
+
calls.push({ op: "respond", nativeId, interruptId, decision });
|
|
77
|
+
if (running?.interruptId === interruptId)
|
|
78
|
+
running.release(decision);
|
|
79
|
+
},
|
|
80
|
+
async setPermissionMode(mode) { calls.push({ op: "setPermissionMode", nativeId, mode }); },
|
|
81
|
+
async contextUsage() { return { tokens: 1234, window: 200000 }; },
|
|
82
|
+
// A harness "compaction": reports before/after counts and leaves a compact step in the timeline.
|
|
83
|
+
...(compact ? {
|
|
84
|
+
async compact() {
|
|
85
|
+
calls.push({ op: "compact", nativeId, running: Boolean(running) });
|
|
86
|
+
if (running)
|
|
87
|
+
throw new Error(`${harness}: finish the running turn before compacting`);
|
|
88
|
+
ctx.emit({ type: EventType.STEP_FINISHED, stepName: "compact", metadata: { aiCollab: { compaction: { trigger: "manual", phase: "finished", tokensBefore: 90000, tokensAfter: 20000 } } } });
|
|
89
|
+
return { before: 90000, after: 20000 };
|
|
90
|
+
},
|
|
91
|
+
} : {}),
|
|
92
|
+
resumeData: () => ({ fake: true }),
|
|
93
|
+
async close() {
|
|
94
|
+
calls.push({ op: "close", nativeId });
|
|
95
|
+
running?.release({ interrupted: true });
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
const adapter = {
|
|
100
|
+
harness,
|
|
101
|
+
calls,
|
|
102
|
+
capabilities: { steer: true, images: false, resume: "transcript", permissionModes: PERMISSION_MODES, ...capabilities },
|
|
103
|
+
async available() { return { ok: true }; },
|
|
104
|
+
async open(options, ctx) {
|
|
105
|
+
calls.push({ op: "open", options });
|
|
106
|
+
if (openError)
|
|
107
|
+
throw new Error(openError);
|
|
108
|
+
return makeHandle(`fake-${randomUUID()}`, ctx);
|
|
109
|
+
},
|
|
110
|
+
async resume(record, ctx) {
|
|
111
|
+
calls.push({ op: "resume", nativeId: record.nativeId, resume: record.resume });
|
|
112
|
+
return makeHandle(record.nativeId, ctx);
|
|
113
|
+
},
|
|
114
|
+
async listModels() { return [{ id: "fake-1", default: true }]; },
|
|
115
|
+
async listCommands(cwd) { return [{ name: "/fake", description: `in ${cwd}` }]; },
|
|
116
|
+
async shutdown() { calls.push({ op: "shutdown" }); },
|
|
117
|
+
};
|
|
118
|
+
return adapter;
|
|
119
|
+
}
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
// OpenCode in OpenShell: one `opencode serve` per project sandbox, reached through `openshell forward service`.
|
|
2
|
+
// Measured 2026-11-28 (openshell 0.1.9, opencode 2.2.6): the sandboxed server answers /api through the forward, and
|
|
3
|
+
// reaches a host provider on 127.0.0.1:<port> as host.openshell.internal:<port> once the policy names that port.
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
import { randomBytes, randomUUID } from "node:crypto";
|
|
6
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { homedir, tmpdir } from "node:os";
|
|
8
|
+
import { basename, join } from "node:path";
|
|
9
|
+
import { builderPolicy, NTFY_HOST, OPENCODE_DIR } from "../../lib/builder/policy.js";
|
|
10
|
+
import { NODE_IN, opencodeSandboxMcpEntry, uploadApp } from "../../lib/sandbox-app.js";
|
|
11
|
+
import { opencodeRequest } from "../../lib/opencode-api.js";
|
|
12
|
+
import { ensureSandbox, execArgv, HOME_IN, opencodeSandboxName, refreshSandboxPolicies, SANDBOX, sandboxLabels, uploadSecret, uploadTree, WORK, } from "../../lib/sandbox.js";
|
|
13
|
+
import { claimableRecord, createClaimLedger, ensureSandboxBinary, fetchIntoSandbox, linkDeps, makeExec, releaseClaim, syncBackWork, uploadClone } from "../../lib/sandbox-claims.js";
|
|
14
|
+
import { findExecutable } from "../../lib/sessions/scope.js";
|
|
15
|
+
import { TOOL_ENV, uploadToolchain } from "../../lib/sandbox-tools.js";
|
|
16
|
+
// The port `opencode serve` listens on inside the sandbox; only the forward reaches it.
|
|
17
|
+
export const SERVE_PORT = 4096;
|
|
18
|
+
export const OPENCODE_IN = `${OPENCODE_DIR}/opencode`;
|
|
19
|
+
const SERVE_DIR = `${HOME_IN}/.opencode-serve`;
|
|
20
|
+
const PASSWORD_IN = `${SERVE_DIR}/password`;
|
|
21
|
+
const PID_IN = `${SERVE_DIR}/pid`;
|
|
22
|
+
// Host addresses a provider baseURL may name; inside the sandbox they become host.openshell.internal.
|
|
23
|
+
const LOOPBACK_HOST = /^(127\.0\.0\.1|localhost|\[::1\]|0\.0\.0\.0)$/;
|
|
24
|
+
// openshell 0.1.9 prints `✓ Forwarding 127.0.0.1:41593 -> 127.0.0.1:4096 in sandbox <name> via gRPC` (measured 2026-11-28).
|
|
25
|
+
const FORWARD_LINE = /Forwarding\s+(\S+):(\d+)\s+->/;
|
|
26
|
+
// Host config keys the sandbox server gets; the rest names host paths (plugins, MCP commands, skills) the sandbox lacks.
|
|
27
|
+
const CONFIG_KEYS = ["provider", "permission", "permissions", "model", "small_model", "disabled_providers", "enabled_providers"];
|
|
28
|
+
const READY_TIMEOUT_MS = 60_000;
|
|
29
|
+
const READY_POLL_MS = 250;
|
|
30
|
+
const STDERR_TAIL_MAX = 4000;
|
|
31
|
+
const LABEL = "opencode";
|
|
32
|
+
const TRAILER = "Assisted-by: OpenCode:opencode";
|
|
33
|
+
// Stops a server an earlier runner left in the sandbox, so the new one can take the port.
|
|
34
|
+
const STOP_OLD = `p=${PID_IN}; if [ -f "$p" ]; then k=$(cat "$p"); kill "$k" 2>/dev/null; i=0; while kill -0 "$k" 2>/dev/null && [ $i -lt 50 ]; do sleep 0.1; i=$((i+1)); done; rm -f "$p"; fi; true`;
|
|
35
|
+
// The password stays in a sandbox file: it never reaches argv, --env or a log.
|
|
36
|
+
const SERVE = `echo $$ > ${PID_IN}; export OPENCODE_SERVER_PASSWORD="$(cat ${PASSWORD_IN})"; exec ${OPENCODE_IN} serve --hostname 127.0.0.1 --port ${SERVE_PORT}`;
|
|
37
|
+
// The image has no git, so the server and its shell tools get the host's under /sandbox/tools, as Claude and Codex do.
|
|
38
|
+
const SERVE_ENV = {
|
|
39
|
+
...TOOL_ENV, HOME: HOME_IN, XDG_CONFIG_HOME: `${HOME_IN}/.config`, XDG_DATA_HOME: `${HOME_IN}/.local/share`, XDG_STATE_HOME: `${HOME_IN}/.local/state`, TEMMO_OWNED: "opencode",
|
|
40
|
+
};
|
|
41
|
+
/** JSON with // and /* comments and trailing commas, as opencode.json allows; strings are left alone. */
|
|
42
|
+
export function parseJsonc(text) {
|
|
43
|
+
let out = "";
|
|
44
|
+
for (let i = 0; i < text.length; i++) {
|
|
45
|
+
const c = text[i];
|
|
46
|
+
if (c === '"') {
|
|
47
|
+
let j = i + 1;
|
|
48
|
+
while (j < text.length && text[j] !== '"')
|
|
49
|
+
j += text[j] === "\\" ? 2 : 1;
|
|
50
|
+
out += text.slice(i, j + 1);
|
|
51
|
+
i = j;
|
|
52
|
+
}
|
|
53
|
+
else if (c === "/" && text[i + 1] === "/") {
|
|
54
|
+
while (i < text.length && text[i] !== "\n")
|
|
55
|
+
i++;
|
|
56
|
+
out += "\n";
|
|
57
|
+
}
|
|
58
|
+
else if (c === "/" && text[i + 1] === "*") {
|
|
59
|
+
const end = text.indexOf("*/", i + 2);
|
|
60
|
+
i = end < 0 ? text.length : end + 1;
|
|
61
|
+
}
|
|
62
|
+
else
|
|
63
|
+
out += c;
|
|
64
|
+
}
|
|
65
|
+
return JSON.parse(out.replace(/,(\s*[}\]])/g, "$1"));
|
|
66
|
+
}
|
|
67
|
+
/** The host's OpenCode config ($XDG_CONFIG_HOME/opencode/opencode.json or .jsonc), or {} when absent or unreadable. */
|
|
68
|
+
export function hostOpencodeConfig(env, home = env.HOME || homedir()) {
|
|
69
|
+
const dir = join(env.XDG_CONFIG_HOME || join(home, ".config"), "opencode");
|
|
70
|
+
for (const file of ["opencode.json", "opencode.jsonc"]) {
|
|
71
|
+
try {
|
|
72
|
+
return parseJsonc(readFileSync(join(dir, file), "utf8"));
|
|
73
|
+
}
|
|
74
|
+
catch { /* next */ }
|
|
75
|
+
}
|
|
76
|
+
return {};
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The sandbox server's config from the host's: providers, permissions and models only, autoupdate off. A provider
|
|
80
|
+
* baseURL on a host loopback address is rewritten to host.openshell.internal; `ports` lists those ports for the policy.
|
|
81
|
+
*/
|
|
82
|
+
export function sandboxConfig(host) {
|
|
83
|
+
const config = { $schema: "https://opencode.ai/config.json", autoupdate: false };
|
|
84
|
+
for (const key of CONFIG_KEYS)
|
|
85
|
+
if (host?.[key] !== undefined)
|
|
86
|
+
config[key] = structuredClone(host[key]);
|
|
87
|
+
const ports = new Set();
|
|
88
|
+
for (const provider of Object.values(config.provider ?? {})) {
|
|
89
|
+
const raw = provider?.options?.baseURL;
|
|
90
|
+
if (typeof raw !== "string")
|
|
91
|
+
continue;
|
|
92
|
+
let url;
|
|
93
|
+
try {
|
|
94
|
+
url = new URL(raw);
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (!LOOPBACK_HOST.test(url.hostname))
|
|
100
|
+
continue;
|
|
101
|
+
const port = Number(url.port || (url.protocol === "https:" ? 443 : 80));
|
|
102
|
+
url.hostname = NTFY_HOST;
|
|
103
|
+
provider.options.baseURL = raw.endsWith("/") ? url.href : url.href.replace(/\/$/, "");
|
|
104
|
+
ports.add(port);
|
|
105
|
+
}
|
|
106
|
+
return { config, ports: [...ports].sort((a, b) => a - b) };
|
|
107
|
+
}
|
|
108
|
+
/** The network policy: OpenCode's hosts and the bus (builderPolicy), plus each host provider port, for the uploaded binary. */
|
|
109
|
+
export function opencodePolicy(ports) {
|
|
110
|
+
// The bus policy also covers the staged node: the temmo MCP server runs under it, not under the OpenCode binary.
|
|
111
|
+
const policy = builderPolicy({ harness: "opencode", binaries: [OPENCODE_IN], ntfyBinaries: [NODE_IN], packages: true });
|
|
112
|
+
if (ports.length) {
|
|
113
|
+
policy.network_policies.host_providers = {
|
|
114
|
+
name: "host-providers",
|
|
115
|
+
endpoints: ports.map((port) => ({ host: NTFY_HOST, port, protocol: "rest", access: "full" })),
|
|
116
|
+
binaries: [{ path: OPENCODE_IN }],
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
return policy;
|
|
120
|
+
}
|
|
121
|
+
/** The local port in the line `openshell forward service` prints once it listens, or 0. */
|
|
122
|
+
export function forwardPort(text) {
|
|
123
|
+
const m = FORWARD_LINE.exec(text);
|
|
124
|
+
return m ? Number(m[2]) : 0;
|
|
125
|
+
}
|
|
126
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
127
|
+
const ownerOf = (project) => `opencode-${project}`;
|
|
128
|
+
/**
|
|
129
|
+
* OpenCode sessions in OpenShell. makeAdapter builds the HTTP adapter for one sandboxed server's endpoint.
|
|
130
|
+
* One sandbox per project, shared by its sessions and deleted when the last one closes; each session's worktree is
|
|
131
|
+
* uploaded as a git clone and landed on the host worktree on close, as Cursor's are.
|
|
132
|
+
*/
|
|
133
|
+
export function createSandboxedOpenCode({ env = process.env, makeAdapter, readyTimeoutMs = READY_TIMEOUT_MS }) {
|
|
134
|
+
const openshell = env.TEMMO_OPENSHELL_BIN || "openshell";
|
|
135
|
+
const boxes = new Map(); // sandbox name -> its running server
|
|
136
|
+
const ledger = createClaimLedger(); // which sessions (and restored records) hold each sandbox
|
|
137
|
+
const landing = { label: LABEL, trailer: TRAILER };
|
|
138
|
+
const opencodeBin = () => {
|
|
139
|
+
const found = env.TEMMO_OPENCODE_BIN || findExecutable("opencode", env.PATH ?? process.env.PATH ?? "");
|
|
140
|
+
if (!found)
|
|
141
|
+
throw new Error("opencode is not installed on the runner host; set TEMMO_OPENCODE_BIN");
|
|
142
|
+
try {
|
|
143
|
+
return realpathSync(found);
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
return found;
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
const exec = makeExec(openshell, env);
|
|
150
|
+
function stopBox(box) {
|
|
151
|
+
for (const child of [box.forward, box.serve])
|
|
152
|
+
if (child && child.exitCode === null)
|
|
153
|
+
child.kill("SIGTERM");
|
|
154
|
+
box.forward = box.serve = null;
|
|
155
|
+
if (boxes.get(box.name) === box)
|
|
156
|
+
boxes.delete(box.name);
|
|
157
|
+
if (box.policyDir)
|
|
158
|
+
rmSync(box.policyDir, { recursive: true, force: true });
|
|
159
|
+
box.policyDir = "";
|
|
160
|
+
return Promise.resolve(box.inner?.shutdown()).catch(() => { });
|
|
161
|
+
}
|
|
162
|
+
// Uploads the binary, config, login and a fresh password, then starts the server and its forward and waits for /api/info.
|
|
163
|
+
async function startBox(box, home, log) {
|
|
164
|
+
const host = hostOpencodeConfig(env, home);
|
|
165
|
+
const { config, ports } = sandboxConfig(host);
|
|
166
|
+
// The host's mcp entries name host paths the sandbox lacks; the sandbox server gets only the bus server.
|
|
167
|
+
// The entry sits directly under mcp: opencode 2.2.6 ignores the newer mcp.servers form entirely
|
|
168
|
+
// (measured: `opencode mcp list` on a servers-form config reports none configured), so servers would ship no tools.
|
|
169
|
+
config.mcp = { "temmo": opencodeSandboxMcpEntry(box.project) };
|
|
170
|
+
box.policyDir = mkdtempSync(join(tmpdir(), "temmo-opencode-policy-"));
|
|
171
|
+
const policy = join(box.policyDir, "policy.yaml");
|
|
172
|
+
writeFileSync(policy, `${JSON.stringify(opencodePolicy(ports), null, 2)}\n`, { mode: 0o600 });
|
|
173
|
+
await ensureSandbox({ openshell, env, name: box.name, policy, labels: sandboxLabels({ project: box.project, kind: "session", owner: box.owner }) });
|
|
174
|
+
await uploadToolchain({ openshell, env, sandbox: box.name, label: "opencode", log });
|
|
175
|
+
// The app, its bus config and the token: the sandboxed server's temmo MCP entry runs them against the host bus.
|
|
176
|
+
await uploadApp({ openshell, env, sandbox: box.name, project: box.project, harness: "opencode" });
|
|
177
|
+
// The binary is about 250 MB, so a sandbox that already has it keeps it; opencodeBin is only named when it is missing.
|
|
178
|
+
if ((await exec(box.name, ["test", "-x", OPENCODE_IN])).code !== 0) {
|
|
179
|
+
await ensureSandboxBinary({ openshell, env, exec, sandbox: box.name, local: opencodeBin(), remoteDir: OPENCODE_DIR, bin: OPENCODE_IN, harnessName: LABEL });
|
|
180
|
+
}
|
|
181
|
+
const dir = mkdtempSync(join(tmpdir(), "temmo-opencode-stage-"));
|
|
182
|
+
const password = randomBytes(24).toString("hex");
|
|
183
|
+
try {
|
|
184
|
+
mkdirSync(join(dir, "opencode"));
|
|
185
|
+
writeFileSync(join(dir, "opencode", "opencode.json"), `${JSON.stringify(config, null, 2)}\n`, { mode: 0o600 });
|
|
186
|
+
await uploadTree({ openshell, env, sandbox: box.name, local: join(dir, "opencode"), remote: `${HOME_IN}/.config`, gitIgnore: false });
|
|
187
|
+
writeFileSync(join(dir, "password"), password, { mode: 0o600 });
|
|
188
|
+
await uploadSecret({ openshell, env, sandbox: box.name, file: join(dir, "password"), remoteDir: SERVE_DIR, tmpPrefix: "temmo-opencode-secret-" });
|
|
189
|
+
}
|
|
190
|
+
finally {
|
|
191
|
+
rmSync(dir, { recursive: true, force: true });
|
|
192
|
+
}
|
|
193
|
+
const auth = join(env.XDG_DATA_HOME || join(home, ".local", "share"), "opencode", "auth.json");
|
|
194
|
+
if (existsSync(auth)) {
|
|
195
|
+
await uploadSecret({ openshell, env, sandbox: box.name, file: auth, remoteDir: `${HOME_IN}/.local/share/opencode`, tmpPrefix: "temmo-opencode-secret-" })
|
|
196
|
+
.catch((error) => log(`opencode: could not copy the login into ${box.name}: ${error.message.split("\n")[0]}`));
|
|
197
|
+
}
|
|
198
|
+
await exec(box.name, ["sh", "-c", STOP_OLD]);
|
|
199
|
+
let tail = "";
|
|
200
|
+
const keep = (chunk) => { tail = (tail + chunk.toString("utf8")).slice(-STDERR_TAIL_MAX); };
|
|
201
|
+
const serve = spawn(openshell, execArgv({ openshell, sandbox: box.name, workdir: SANDBOX, env: SERVE_ENV, command: ["sh", "-c", SERVE] }).slice(1), { env, stdio: ["ignore", "pipe", "pipe"] });
|
|
202
|
+
box.serve = serve;
|
|
203
|
+
serve.stdout?.on("data", keep);
|
|
204
|
+
serve.stderr?.on("data", keep);
|
|
205
|
+
const served = new Promise((_, reject) => {
|
|
206
|
+
serve.on("error", reject);
|
|
207
|
+
serve.on("exit", (code, signal) => {
|
|
208
|
+
if (boxes.get(box.name) === box) {
|
|
209
|
+
boxes.delete(box.name);
|
|
210
|
+
log(`opencode: the server in ${box.name} exited (${signal ?? `code ${code}`})`);
|
|
211
|
+
}
|
|
212
|
+
reject(new Error(`opencode serve in ${box.name} exited (${signal ?? `code ${code}`}): ${tail.trim().slice(-500)}`));
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
served.catch(() => { });
|
|
216
|
+
const forward = spawn(openshell, ["forward", "service", box.name, "--target-port", String(SERVE_PORT), "--local", "127.0.0.1:0"], { env, stdio: ["ignore", "pipe", "pipe"] });
|
|
217
|
+
box.forward = forward;
|
|
218
|
+
let fwdText = "";
|
|
219
|
+
const port = await Promise.race([served, new Promise((resolve, reject) => {
|
|
220
|
+
const read = (chunk) => {
|
|
221
|
+
fwdText += chunk.toString("utf8");
|
|
222
|
+
const found = forwardPort(fwdText);
|
|
223
|
+
if (found)
|
|
224
|
+
resolve(found);
|
|
225
|
+
};
|
|
226
|
+
forward.stdout?.on("data", read);
|
|
227
|
+
forward.stderr?.on("data", read);
|
|
228
|
+
forward.on("error", reject);
|
|
229
|
+
forward.on("exit", (code) => reject(new Error(`openshell forward service exited (code ${code}): ${fwdText.trim().slice(-500)}`)));
|
|
230
|
+
})]);
|
|
231
|
+
box.endpoint = { url: `http://127.0.0.1:${port}`, auth: `Basic ${Buffer.from(`opencode:${password}`).toString("base64")}` };
|
|
232
|
+
const deadline = Date.now() + readyTimeoutMs;
|
|
233
|
+
let last = "";
|
|
234
|
+
for (;;) {
|
|
235
|
+
const r = await Promise.race([served, opencodeRequest("GET", "/api/info", { env, endpoint: box.endpoint, timeoutMs: 5000 })]);
|
|
236
|
+
if (r.ok)
|
|
237
|
+
break;
|
|
238
|
+
last = r.error;
|
|
239
|
+
if (Date.now() > deadline)
|
|
240
|
+
throw new Error(`the OpenCode server in ${box.name} did not answer within ${readyTimeoutMs} ms: ${last}`);
|
|
241
|
+
await Promise.race([served, sleep(READY_POLL_MS)]);
|
|
242
|
+
}
|
|
243
|
+
box.inner = makeAdapter(() => box.endpoint);
|
|
244
|
+
}
|
|
245
|
+
// The running server for a project's sandbox, started once and shared.
|
|
246
|
+
async function boxFor(project, home, log) {
|
|
247
|
+
const name = opencodeSandboxName(project);
|
|
248
|
+
const known = boxes.get(name);
|
|
249
|
+
if (known) {
|
|
250
|
+
await known.ready;
|
|
251
|
+
return known;
|
|
252
|
+
}
|
|
253
|
+
const box = { name, project, owner: ownerOf(project), endpoint: null, inner: null, serve: null, forward: null, uploaded: new Set(), policyDir: "", ready: Promise.resolve() };
|
|
254
|
+
boxes.set(name, box);
|
|
255
|
+
box.ready = startBox(box, home, log);
|
|
256
|
+
try {
|
|
257
|
+
await box.ready;
|
|
258
|
+
}
|
|
259
|
+
catch (error) {
|
|
260
|
+
await stopBox(box);
|
|
261
|
+
throw error;
|
|
262
|
+
}
|
|
263
|
+
return box;
|
|
264
|
+
}
|
|
265
|
+
// Uploads a session's worktree as a clone under /sandbox/work; a resume keeps the one already there.
|
|
266
|
+
async function stageWork(box, cwd, keepExisting) {
|
|
267
|
+
const workdir = `${WORK}/${basename(cwd)}`;
|
|
268
|
+
if (box.uploaded.has(cwd))
|
|
269
|
+
return workdir;
|
|
270
|
+
const present = keepExisting && (await exec(box.name, ["test", "-d", workdir])).code === 0;
|
|
271
|
+
if (!present) {
|
|
272
|
+
if (existsSync(join(cwd, ".git")))
|
|
273
|
+
await uploadClone({ openshell, env, sandbox: box.name, cwd, remote: WORK });
|
|
274
|
+
else
|
|
275
|
+
await uploadTree({ openshell, env, sandbox: box.name, local: cwd, remote: WORK });
|
|
276
|
+
}
|
|
277
|
+
await linkDeps({ openshell, env, exec, sandbox: box.name, cwd, workdir });
|
|
278
|
+
box.uploaded.add(cwd);
|
|
279
|
+
return workdir;
|
|
280
|
+
}
|
|
281
|
+
// Lands the session's work on its host worktree, drops its claim, and deletes the sandbox with the last claim.
|
|
282
|
+
const releaseClaimFor = (name, id, project, cwd, log) => releaseClaim({
|
|
283
|
+
openshell, env, ledger, name, id, project, owner: ownerOf(project), cwd, log, landing,
|
|
284
|
+
forgetWork: (work) => { boxes.get(name)?.uploaded.delete(work); },
|
|
285
|
+
stopBox: async () => { const box = boxes.get(name); if (box)
|
|
286
|
+
await stopBox(box); },
|
|
287
|
+
});
|
|
288
|
+
// The inner handle with a close that lands the work and releases the sandbox, and a syncBack for land finish.
|
|
289
|
+
function wrap(handle, box, id, cwd, ctx) {
|
|
290
|
+
let closed = false;
|
|
291
|
+
return {
|
|
292
|
+
...handle,
|
|
293
|
+
syncBack: (opts) => syncBackWork({ openshell, env, exec, sandbox: box.name, cwd, log: ctx.log, landing, ...opts }),
|
|
294
|
+
fetchOrigin: (ref) => fetchIntoSandbox({ openshell, env, exec, sandbox: box.name, cwd, log: ctx.log, ref }),
|
|
295
|
+
async close() {
|
|
296
|
+
if (closed)
|
|
297
|
+
return;
|
|
298
|
+
closed = true;
|
|
299
|
+
await handle.close();
|
|
300
|
+
await releaseClaimFor(box.name, id, box.project, cwd, ctx.log);
|
|
301
|
+
},
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
function refuseTeam(patternRun) {
|
|
305
|
+
if (patternRun)
|
|
306
|
+
throw new Error("OpenCode cannot join an OpenShell team run yet");
|
|
307
|
+
}
|
|
308
|
+
return {
|
|
309
|
+
async open(options, ctx) {
|
|
310
|
+
refuseTeam(options.patternRun);
|
|
311
|
+
const name = opencodeSandboxName(options.project);
|
|
312
|
+
const id = randomUUID();
|
|
313
|
+
ledger.claim(name, id);
|
|
314
|
+
let box;
|
|
315
|
+
try {
|
|
316
|
+
box = await boxFor(options.project, options.env?.HOME || env.HOME || homedir(), ctx.log);
|
|
317
|
+
const workdir = await stageWork(box, options.cwd, false);
|
|
318
|
+
// The server runs in the sandbox, so the session is created in the sandbox's copy of the worktree.
|
|
319
|
+
return wrap(await box.inner.open({ ...options, cwd: workdir }, ctx), box, id, options.cwd, ctx);
|
|
320
|
+
}
|
|
321
|
+
catch (error) {
|
|
322
|
+
await releaseClaimFor(name, id, options.project, box ? options.cwd : "", ctx.log);
|
|
323
|
+
throw error;
|
|
324
|
+
}
|
|
325
|
+
},
|
|
326
|
+
async resume(record, ctx, runEnv = {}) {
|
|
327
|
+
refuseTeam(record.patternRun);
|
|
328
|
+
const name = opencodeSandboxName(record.project);
|
|
329
|
+
// A restored record already holds its claim under its address; resuming takes that claim over.
|
|
330
|
+
// A failed resume keeps the claim: the sandbox holds the session's work until release lands it.
|
|
331
|
+
const id = record.address || randomUUID();
|
|
332
|
+
ledger.claim(name, id);
|
|
333
|
+
const box = await boxFor(record.project, runEnv.HOME || env.HOME || homedir(), ctx.log);
|
|
334
|
+
await stageWork(box, record.cwd, true);
|
|
335
|
+
return wrap(await box.inner.resume(record, ctx), box, id, record.cwd, ctx);
|
|
336
|
+
},
|
|
337
|
+
/** A runner start replaces the policy on the project sandboxes that already exist, so a deploy reaches idle ones too. */
|
|
338
|
+
async reapplyPolicy(projects) {
|
|
339
|
+
const names = projects.map((project) => opencodeSandboxName(project));
|
|
340
|
+
if (!names.length)
|
|
341
|
+
return;
|
|
342
|
+
const { ports } = sandboxConfig(hostOpencodeConfig(env, env.HOME || homedir()));
|
|
343
|
+
const dir = mkdtempSync(join(tmpdir(), "temmo-opencode-policy-"));
|
|
344
|
+
try {
|
|
345
|
+
const policy = join(dir, "policy.yaml");
|
|
346
|
+
writeFileSync(policy, `${JSON.stringify(opencodePolicy(ports), null, 2)}\n`, { mode: 0o600 });
|
|
347
|
+
await refreshSandboxPolicies({ openshell, env, names, policy });
|
|
348
|
+
}
|
|
349
|
+
finally {
|
|
350
|
+
rmSync(dir, { recursive: true, force: true });
|
|
351
|
+
}
|
|
352
|
+
},
|
|
353
|
+
/** A record restored after a runner restart holds its sandbox until it resumes or is released. */
|
|
354
|
+
restore(record) {
|
|
355
|
+
if (!claimableRecord(record))
|
|
356
|
+
return;
|
|
357
|
+
ledger.claim(opencodeSandboxName(record.project), record.address);
|
|
358
|
+
},
|
|
359
|
+
/** Ends a restored record that never resumed: lands its work and deletes the sandbox when no other session holds it. */
|
|
360
|
+
async release(record) {
|
|
361
|
+
if (!claimableRecord(record))
|
|
362
|
+
return true;
|
|
363
|
+
return releaseClaimFor(opencodeSandboxName(record.project), record.address, record.project, record.cwd, () => { });
|
|
364
|
+
},
|
|
365
|
+
// Stops every server and forward; the sandboxes stay, so their sessions resume after a restart.
|
|
366
|
+
async shutdown() {
|
|
367
|
+
await Promise.allSettled([...boxes.values()].map((box) => stopBox(box)));
|
|
368
|
+
},
|
|
369
|
+
};
|
|
370
|
+
}
|