@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,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one-time move from ai-collab to temmo that setup runs (docs/rename.md): each old XDG dir moves to the new
|
|
3
|
+
* name and leaves a symlink behind, and each ai-collab-<role> user unit is stopped, disabled and replaced by
|
|
4
|
+
* temmo-<role> with its drop-ins. Every step is a no-op once done, so reruns change nothing.
|
|
5
|
+
*/
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { LEGACY_NAME, NAME, PRODUCT, SERVICE_ROLES, legacyServiceName, renamedDirs, serviceName, unitsDir } from "./names.js";
|
|
9
|
+
/** Moves one dir, also across filesystems. */
|
|
10
|
+
function move(from, to) {
|
|
11
|
+
fs.mkdirSync(path.dirname(to), { recursive: true });
|
|
12
|
+
try {
|
|
13
|
+
fs.renameSync(from, to);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
if (error.code !== "EXDEV")
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
fs.cpSync(from, to, { recursive: true, preserveTimestamps: true, verbatimSymlinks: true });
|
|
21
|
+
fs.rmSync(from, { recursive: true, force: true });
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Moves ~/.config, ~/.local/state and ~/.local/share ai-collab to temmo and links each old path to its new one.
|
|
25
|
+
* Sessions started before the rename keep reading the old paths, so the link is the step that must not fail.
|
|
26
|
+
*/
|
|
27
|
+
export function migrateDirs(env) {
|
|
28
|
+
const notes = [];
|
|
29
|
+
for (const { from, to } of renamedDirs(env)) {
|
|
30
|
+
let stat = null;
|
|
31
|
+
try {
|
|
32
|
+
stat = fs.lstatSync(from);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
if (stat.isSymbolicLink())
|
|
38
|
+
continue;
|
|
39
|
+
if (!stat.isDirectory()) {
|
|
40
|
+
notes.push(`${from} is not a directory; left alone`);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (fs.existsSync(to)) {
|
|
44
|
+
notes.push(`${from} and ${to} both exist; merge them by hand, then rerun setup`);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
move(from, to);
|
|
49
|
+
fs.symlinkSync(to, from, "dir");
|
|
50
|
+
notes.push(`moved ${from} to ${to} and linked the old path`);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
notes.push(`moving ${from} failed: ${error.message}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return notes;
|
|
57
|
+
}
|
|
58
|
+
/** An old unit or drop-in with the renamed scripts, env names, dirs and description; the checkout path is kept. */
|
|
59
|
+
export function renameUnitText(text) {
|
|
60
|
+
return text
|
|
61
|
+
.replace(/AI_COLLAB_/g, "TEMMO_")
|
|
62
|
+
.replace(new RegExp(`scripts/${LEGACY_NAME}-`, "g"), `scripts/${NAME}-`)
|
|
63
|
+
.replace(new RegExp(`(\\.config|\\.local/state|\\.local/share)/${LEGACY_NAME}(?=/|\\s|$)`, "gm"), `$1/${NAME}`)
|
|
64
|
+
.replace(/\bAI Collab\b/g, PRODUCT)
|
|
65
|
+
.replace(new RegExp(`\\b${LEGACY_NAME} (deploy|setup)\\b`, "g"), `${NAME} $1`);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Replaces each installed ai-collab-<role>.service (and its .service.d drop-ins) with temmo-<role>: writes the
|
|
69
|
+
* new unit, stops and disables the old one, removes it, runs beforeStart, then enables and starts the new one.
|
|
70
|
+
*/
|
|
71
|
+
export function migrateUnits({ env, run, beforeStart = () => [] }) {
|
|
72
|
+
const dir = unitsDir(env);
|
|
73
|
+
const notes = [];
|
|
74
|
+
const moved = [];
|
|
75
|
+
for (const role of SERVICE_ROLES) {
|
|
76
|
+
const oldName = legacyServiceName(role);
|
|
77
|
+
const newName = serviceName(role);
|
|
78
|
+
const oldUnit = path.join(dir, `${oldName}.service`);
|
|
79
|
+
const newUnit = path.join(dir, `${newName}.service`);
|
|
80
|
+
if (!fs.existsSync(oldUnit))
|
|
81
|
+
continue;
|
|
82
|
+
if (fs.existsSync(newUnit)) {
|
|
83
|
+
notes.push(`${oldName} and ${newName} are both installed; remove ${oldUnit} by hand`);
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
fs.writeFileSync(newUnit, renameUnitText(fs.readFileSync(oldUnit, "utf8")));
|
|
87
|
+
const oldDropIns = path.join(dir, `${oldName}.service.d`);
|
|
88
|
+
if (fs.existsSync(oldDropIns)) {
|
|
89
|
+
const newDropIns = path.join(dir, `${newName}.service.d`);
|
|
90
|
+
fs.mkdirSync(newDropIns, { recursive: true });
|
|
91
|
+
for (const file of fs.readdirSync(oldDropIns)) {
|
|
92
|
+
const from = path.join(oldDropIns, file);
|
|
93
|
+
if (fs.statSync(from).isFile())
|
|
94
|
+
fs.writeFileSync(path.join(newDropIns, file), renameUnitText(fs.readFileSync(from, "utf8")));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const disabled = run("systemctl", ["--user", "disable", "--now", oldName]);
|
|
98
|
+
if (disabled.code !== 0)
|
|
99
|
+
notes.push(`systemctl disable ${oldName} failed: ${disabled.stderr.trim()}`);
|
|
100
|
+
fs.rmSync(oldUnit, { force: true });
|
|
101
|
+
fs.rmSync(oldDropIns, { recursive: true, force: true });
|
|
102
|
+
moved.push([oldName, newName]);
|
|
103
|
+
}
|
|
104
|
+
// Every old unit is stopped here, so nothing writes to the old dirs while beforeStart moves them.
|
|
105
|
+
notes.push(...beforeStart());
|
|
106
|
+
if (!moved.length)
|
|
107
|
+
return notes;
|
|
108
|
+
run("systemctl", ["--user", "daemon-reload"]);
|
|
109
|
+
for (const [oldName, name] of moved) {
|
|
110
|
+
const r = run("systemctl", ["--user", "enable", "--now", name]);
|
|
111
|
+
notes.push(r.code === 0 ? `replaced ${oldName} with ${name}` : `systemctl enable ${name} failed: ${r.stderr.trim()}`);
|
|
112
|
+
}
|
|
113
|
+
return notes;
|
|
114
|
+
}
|
|
115
|
+
/** Whether any old unit is still installed (setup checks for runner turns before stopping it). */
|
|
116
|
+
export function legacyUnitsInstalled(env) {
|
|
117
|
+
return SERVICE_ROLES.some((role) => fs.existsSync(path.join(unitsDir(env), `${legacyServiceName(role)}.service`)));
|
|
118
|
+
}
|
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run in terminal: an agent proposes a script, the operator reads it in the UI and clicks Run, and the UI server runs it.
|
|
3
|
+
* Requests live under <stateDir>/<project>/run-terminal/<id>/; decision.json is created once (O_EXCL), so a request runs
|
|
4
|
+
* at most once. Nothing here reads the bus: the only path to decideRun is the UI server's authenticated HTTP route.
|
|
5
|
+
*/
|
|
6
|
+
import { spawn } from "node:child_process";
|
|
7
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
8
|
+
import fs from "node:fs";
|
|
9
|
+
import os from "node:os";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import { autoDefaults } from "./config.js";
|
|
12
|
+
import { readJson, writeFileAtomic } from "./store.js";
|
|
13
|
+
export const RUN_ID = /^[a-f0-9]{24}$/;
|
|
14
|
+
export const SCRIPT_MAX_BYTES = 64 * 1024;
|
|
15
|
+
export const REASON_MAX = 300;
|
|
16
|
+
export const TIMEOUT_DEFAULT_S = 300;
|
|
17
|
+
export const TIMEOUT_MAX_S = 3600;
|
|
18
|
+
export const TTL_DEFAULT_S = 15 * 60;
|
|
19
|
+
const TTL_MAX_S = 60 * 60;
|
|
20
|
+
const CLOCK_SKEW_MS = 5000;
|
|
21
|
+
const TAIL_BYTES = 16 * 1024;
|
|
22
|
+
const FLUSH_MS = 250;
|
|
23
|
+
const KILL_GRACE_MS = 5000;
|
|
24
|
+
const LIST_MAX = 20;
|
|
25
|
+
const LIST_AGE_MS = 24 * 60 * 60 * 1000;
|
|
26
|
+
const ADDRESS = /^[a-z0-9-]{1,24}#[a-f0-9]{16}$/;
|
|
27
|
+
// Control, format, separator and default-ignorable characters other than tab, newline and space: they hide or fake bytes.
|
|
28
|
+
const HIDDEN = /(?![\t\n ])[\p{C}\p{Z}\p{Default_Ignorable_Code_Point}]/u;
|
|
29
|
+
// More blanks in a row than this can push the rest of a wrapped line out of the card's view.
|
|
30
|
+
const BLANK_RUN = /[ \t]{65}/;
|
|
31
|
+
// The UI server's own variables; a script gets the rest of its environment.
|
|
32
|
+
const SERVER_ENV = /^(TEMMO_|NEXT_|__NEXT|NODE_ENV$|NODE_OPTIONS$)/;
|
|
33
|
+
// The request fields the card shows; a stored request that no longer matches what was shown cannot run.
|
|
34
|
+
export const SHOWN = ["session", "cwd", "reason", "script", "sha256", "timeoutS", "createdAt", "expiresAt"];
|
|
35
|
+
export const FINAL = ["done", "lost", "rejected", "expired", "refused"];
|
|
36
|
+
// The wrapper records the script's exit status to a file, then exits the way the script did, so a server that
|
|
37
|
+
// restarted mid-run still learns the outcome. Arguments: $1 the script, $2 the exit file.
|
|
38
|
+
const WRAPPER = 'bash --noprofile --norc -c "$1"; code=$?; printf "%s\\n" "$code" > "$2.tmp" && mv -f "$2.tmp" "$2"; '
|
|
39
|
+
+ 'if [ "$code" -gt 128 ]; then trap - "$((code - 128))" 2>/dev/null; kill -s "$((code - 128))" $$; fi; exit "$code"';
|
|
40
|
+
/** On when TEMMO_RUN_IN_TERMINAL=1, or defaults.json sets "runInTerminal": true and the variable is not 0. */
|
|
41
|
+
export function runInTerminalEnabled(env = process.env) {
|
|
42
|
+
if (env.TEMMO_RUN_IN_TERMINAL === "1")
|
|
43
|
+
return true;
|
|
44
|
+
if (env.TEMMO_RUN_IN_TERMINAL === "0")
|
|
45
|
+
return false;
|
|
46
|
+
return autoDefaults(env)?.runInTerminal === true;
|
|
47
|
+
}
|
|
48
|
+
/** False inside an OpenShell sandbox or a builder, where the tool is never offered. */
|
|
49
|
+
export const onHost = (env) => !env.OPENSHELL_SANDBOX && !env.TEMMO_BUILDER && !env.TEMMO_SANDBOX_OUT;
|
|
50
|
+
export const sha256 = (script) => createHash("sha256").update(script, "utf8").digest("hex");
|
|
51
|
+
/** Why a script cannot be shown byte for byte, or null. */
|
|
52
|
+
export function scriptProblem(script) {
|
|
53
|
+
if (typeof script !== "string" || !script.trim())
|
|
54
|
+
return "script must be non-empty text";
|
|
55
|
+
if (!script.isWellFormed())
|
|
56
|
+
return "script must be valid Unicode";
|
|
57
|
+
if (Buffer.byteLength(script, "utf8") > SCRIPT_MAX_BYTES)
|
|
58
|
+
return `script exceeds ${SCRIPT_MAX_BYTES} bytes`;
|
|
59
|
+
if (HIDDEN.test(script))
|
|
60
|
+
return "script contains control, bidi or zero-width characters the UI cannot show";
|
|
61
|
+
if (BLANK_RUN.test(script))
|
|
62
|
+
return "script has a run of more than 64 blanks that could push text off screen";
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
function cwdProblem(cwd) {
|
|
66
|
+
if (typeof cwd !== "string" || !path.isAbsolute(cwd) || HIDDEN.test(cwd))
|
|
67
|
+
return "cwd must be an absolute path";
|
|
68
|
+
try {
|
|
69
|
+
if (!fs.statSync(cwd).isDirectory())
|
|
70
|
+
return "cwd is not a directory";
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return "cwd does not exist";
|
|
74
|
+
}
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
export const runRoot = (stateDir, project) => path.join(stateDir, project, "run-terminal");
|
|
78
|
+
const dirOf = (stateDir, project, id) => path.join(runRoot(stateDir, project), id);
|
|
79
|
+
const auditFile = (stateDir) => path.join(stateDir, "run-terminal-audit.jsonl");
|
|
80
|
+
/** Appends one line to the audit log: every request, decision, refusal and finish. */
|
|
81
|
+
export function audit(stateDir, entry) {
|
|
82
|
+
fs.mkdirSync(stateDir, { recursive: true, mode: 0o700 });
|
|
83
|
+
fs.appendFileSync(auditFile(stateDir), `${JSON.stringify({ ts: new Date().toISOString(), ...entry })}\n`, { mode: 0o600 });
|
|
84
|
+
}
|
|
85
|
+
/** Records a pending request for the operator to read; returns its id, hash and expiry. Throws on bad input or off the host. */
|
|
86
|
+
export function createRunRequest(cfg, { script, cwd, reason, timeoutS }, env = process.env, now = Date.now()) {
|
|
87
|
+
if (!onHost(env))
|
|
88
|
+
throw new Error("run_in_terminal runs only on the host, not in a sandbox or builder");
|
|
89
|
+
if (!runInTerminalEnabled(env))
|
|
90
|
+
throw new Error("run_in_terminal is off; set runInTerminal in defaults.json");
|
|
91
|
+
const problem = scriptProblem(script) || cwdProblem(cwd);
|
|
92
|
+
if (problem)
|
|
93
|
+
throw new Error(problem);
|
|
94
|
+
if (typeof reason !== "string" || !reason.trim() || reason.length > REASON_MAX || HIDDEN.test(reason))
|
|
95
|
+
throw new Error(`reason must be 1 to ${REASON_MAX} printable characters`);
|
|
96
|
+
const timeout = timeoutS === undefined ? TIMEOUT_DEFAULT_S : timeoutS;
|
|
97
|
+
if (!Number.isInteger(timeout) || timeout < 1 || timeout > TIMEOUT_MAX_S)
|
|
98
|
+
throw new Error(`timeout_s must be 1 to ${TIMEOUT_MAX_S}`);
|
|
99
|
+
const session = `${cfg.peer}#${cfg.session}`;
|
|
100
|
+
if (!ADDRESS.test(session))
|
|
101
|
+
throw new Error("Pass the session label supplied by the temmo hook");
|
|
102
|
+
const ttl = Math.min(Number(env.TEMMO_RUN_TTL_S) || TTL_DEFAULT_S, TTL_MAX_S);
|
|
103
|
+
const id = randomBytes(12).toString("hex");
|
|
104
|
+
const request = { id, project: cfg.project, session, script, cwd, reason: reason.trim(), sha256: sha256(script), timeoutS: timeout, createdAt: now, expiresAt: now + ttl * 1000 };
|
|
105
|
+
writeFileAtomic(path.join(dirOf(cfg.stateDir, cfg.project, id), "request.json"), `${JSON.stringify(request)}\n`);
|
|
106
|
+
audit(cfg.stateDir, { event: "requested", id, project: cfg.project, session, sha256: request.sha256, cwd, timeoutS: timeout });
|
|
107
|
+
return { id, status: "pending", sha256: request.sha256, expiresAt: new Date(request.expiresAt).toISOString() };
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* The stored request, checked again: the file is writable by any same-user process, so its hash is recomputed and its
|
|
111
|
+
* expiry is clamped to TTL_MAX_S after a creation time that is not in the future. Null when missing or malformed.
|
|
112
|
+
*/
|
|
113
|
+
export function readRunRequest(stateDir, project, id, now = Date.now()) {
|
|
114
|
+
if (!RUN_ID.test(id || ""))
|
|
115
|
+
return null;
|
|
116
|
+
const r = readJson(path.join(dirOf(stateDir, project, id), "request.json"));
|
|
117
|
+
if (!r || r.id !== id || r.project !== project || !ADDRESS.test(r.session || "") || scriptProblem(r.script))
|
|
118
|
+
return null;
|
|
119
|
+
if (typeof r.cwd !== "string" || !path.isAbsolute(r.cwd) || typeof r.reason !== "string")
|
|
120
|
+
return null;
|
|
121
|
+
if (!Number.isFinite(r.createdAt) || r.createdAt > now + CLOCK_SKEW_MS || !Number.isInteger(r.timeoutS) || r.timeoutS < 1 || r.timeoutS > TIMEOUT_MAX_S)
|
|
122
|
+
return null;
|
|
123
|
+
const expiresAt = Math.min(Number(r.expiresAt) || 0, r.createdAt + TTL_MAX_S * 1000);
|
|
124
|
+
return { id, project, session: r.session, script: r.script, cwd: r.cwd, reason: r.reason.slice(0, REASON_MAX), sha256: sha256(r.script), timeoutS: r.timeoutS, createdAt: r.createdAt, expiresAt };
|
|
125
|
+
}
|
|
126
|
+
// Claims the request for one decision; false when another decision got there first.
|
|
127
|
+
function claim(stateDir, project, id, record) {
|
|
128
|
+
try {
|
|
129
|
+
fs.writeFileSync(path.join(dirOf(stateDir, project, id), "decision.json"), `${JSON.stringify(record)}\n`, { flag: "wx", mode: 0o600 });
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
if (error.code === "EEXIST")
|
|
134
|
+
return false;
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const readDecision = (stateDir, project, id) => readJson(path.join(dirOf(stateDir, project, id), "decision.json"));
|
|
139
|
+
const writeResult = (stateDir, project, id, result) => writeFileAtomic(path.join(dirOf(stateDir, project, id), "result.json"), `${JSON.stringify(result)}\n`);
|
|
140
|
+
/** Closes an unanswered request whose expiry passed, once, so later reads and clicks see "expired". */
|
|
141
|
+
function expireIfDue(request, stateDir, now) {
|
|
142
|
+
if (now <= request.expiresAt)
|
|
143
|
+
return false;
|
|
144
|
+
if (claim(stateDir, request.project, request.id, { decision: "expired", at: now })) {
|
|
145
|
+
audit(stateDir, { event: "expired", id: request.id, project: request.project, session: request.session, sha256: request.sha256 });
|
|
146
|
+
}
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
/** Closes an undecided request as refused, once, with an audit line; false when it was already decided. */
|
|
150
|
+
export function closeRun(stateDir, project, id, why, by = "operator") {
|
|
151
|
+
if (!claim(stateDir, project, id, { decision: "refused", why, by, at: Date.now() }))
|
|
152
|
+
return false;
|
|
153
|
+
audit(stateDir, { event: "refused", id, project, by, why });
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Settles runs a previous UI server left at running: one whose script finished records its exit, one still running
|
|
158
|
+
* (its scope outlived the server) is followed again, and one whose process is gone without an exit is lost.
|
|
159
|
+
*/
|
|
160
|
+
export function recoverRuns(stateDir, project, onUpdate = () => { }, now = Date.now()) {
|
|
161
|
+
for (const request of listRunRequests(stateDir, project, now).filter((item) => item.status === "running")) {
|
|
162
|
+
const result = readJson(path.join(dirOf(stateDir, project, request.id), "result.json")) || {};
|
|
163
|
+
const by = readDecision(stateDir, project, request.id)?.by || "operator";
|
|
164
|
+
if (exitOf(stateDir, request) || alive(result.pid, request.id)) {
|
|
165
|
+
follow(stateDir, request, by, onUpdate, { pid: result.pid, startedAt: Number(result.startedAt) || now, unit: result.unit });
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
writeResult(stateDir, project, request.id, { ...result, ...tails(stateDir, request), status: "lost", lostAt: now });
|
|
169
|
+
audit(stateDir, { event: "lost", id: request.id, project, session: request.session, sha256: request.sha256 });
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** Status and result of a request: pending, running, done, lost, rejected, expired or refused. */
|
|
173
|
+
export function runStatus(stateDir, project, id, now = Date.now()) {
|
|
174
|
+
const request = readRunRequest(stateDir, project, id, now);
|
|
175
|
+
if (!request)
|
|
176
|
+
throw new Error(`No run request ${id}`);
|
|
177
|
+
let decision = readDecision(stateDir, project, id);
|
|
178
|
+
if (!decision && expireIfDue(request, stateDir, now))
|
|
179
|
+
decision = readDecision(stateDir, project, id);
|
|
180
|
+
const base = { id, sha256: request.sha256, expiresAt: new Date(request.expiresAt).toISOString() };
|
|
181
|
+
if (!decision)
|
|
182
|
+
return { ...base, status: "pending" };
|
|
183
|
+
if (decision.decision === "run") {
|
|
184
|
+
const result = readJson(path.join(dirOf(stateDir, project, id), "result.json")) || { status: "running" };
|
|
185
|
+
return { ...base, ...result, status: ["done", "lost"].includes(result.status) ? result.status : "running", by: decision.by };
|
|
186
|
+
}
|
|
187
|
+
// oxlint-disable-next-line quality/no-known-value-widening -- looked up by the stored decision
|
|
188
|
+
const status = { reject: "rejected", expired: "expired", refused: "refused" }[decision.decision] || "refused";
|
|
189
|
+
return { ...base, status, ...(decision.by ? { by: decision.by } : {}), ...(decision.why ? { why: decision.why } : {}) };
|
|
190
|
+
}
|
|
191
|
+
/** Polls the request until it reaches a final state, the deadline passes, or the signal aborts. */
|
|
192
|
+
export async function waitForRun(cfg, id, seconds = 25, signal) {
|
|
193
|
+
const deadline = Date.now() + Math.max(0, Math.min(45, seconds)) * 1000;
|
|
194
|
+
for (;;) {
|
|
195
|
+
const status = runStatus(cfg.stateDir, cfg.project, id);
|
|
196
|
+
if (FINAL.includes(status.status) || Date.now() >= deadline || signal?.aborted)
|
|
197
|
+
return status;
|
|
198
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/** Recent requests of a project, newest first, with status and script for the UI. */
|
|
202
|
+
export function listRunRequests(stateDir, project, now = Date.now()) {
|
|
203
|
+
let ids;
|
|
204
|
+
try {
|
|
205
|
+
ids = fs.readdirSync(runRoot(stateDir, project)).filter((id) => RUN_ID.test(id));
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
return [];
|
|
209
|
+
}
|
|
210
|
+
const out = [];
|
|
211
|
+
for (const id of ids) {
|
|
212
|
+
const request = readRunRequest(stateDir, project, id, now);
|
|
213
|
+
if (!request || now - request.createdAt > LIST_AGE_MS)
|
|
214
|
+
continue;
|
|
215
|
+
out.push({ ...request, ...runStatus(stateDir, project, id, now), expiresAt: request.expiresAt });
|
|
216
|
+
}
|
|
217
|
+
return out.sort((a, b) => b.createdAt - a.createdAt).slice(0, LIST_MAX);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* The operator's decision from the UI server. Run executes the stored bytes only when their recomputed sha256 equals
|
|
221
|
+
* the hash the browser computed from what it showed, and, given the pinned fields the UI first showed, only when the
|
|
222
|
+
* stored request still matches them; a mismatch closes the request. Returns the status at once; onUpdate fires once the run starts, as
|
|
223
|
+
* output arrives and when the script ends.
|
|
224
|
+
*/
|
|
225
|
+
export function decideRun({ stateDir, project, id, decision, sha256: shown, pinned, by = "operator", onUpdate = () => { }, env = process.env, now = Date.now() }) {
|
|
226
|
+
const request = readRunRequest(stateDir, project, id, now);
|
|
227
|
+
const fields = { id, project, session: request?.session, sha256: request?.sha256, by };
|
|
228
|
+
const refuse = (why, status = 409, close = false) => {
|
|
229
|
+
// The request folder watch sees no write inside a request's own folder, so a closed card is pushed here.
|
|
230
|
+
if (close && request && claim(stateDir, project, id, { decision: "refused", why, by, at: now }))
|
|
231
|
+
onUpdate();
|
|
232
|
+
audit(stateDir, { event: "refused", ...fields, why });
|
|
233
|
+
const error = new Error(`Run refused: ${why}`);
|
|
234
|
+
error.status = status;
|
|
235
|
+
throw error;
|
|
236
|
+
};
|
|
237
|
+
if (!request)
|
|
238
|
+
return refuse("unknown request", 404);
|
|
239
|
+
if (!["run", "reject"].includes(decision))
|
|
240
|
+
refuse("unknown decision", 400);
|
|
241
|
+
if (readDecision(stateDir, project, id))
|
|
242
|
+
refuse("already decided");
|
|
243
|
+
if (expireIfDue(request, stateDir, now))
|
|
244
|
+
refuse("expired");
|
|
245
|
+
if (decision === "reject") {
|
|
246
|
+
if (!claim(stateDir, project, id, { decision: "reject", by, at: now }))
|
|
247
|
+
refuse("already decided");
|
|
248
|
+
audit(stateDir, { event: "rejected", ...fields, decision });
|
|
249
|
+
onUpdate();
|
|
250
|
+
return { id, status: "rejected" };
|
|
251
|
+
}
|
|
252
|
+
if (pinned && SHOWN.some((key) => pinned[key] !== request[key]))
|
|
253
|
+
refuse("the request changed after it was shown", 409, true);
|
|
254
|
+
if (typeof shown !== "string" || shown !== request.sha256)
|
|
255
|
+
refuse("the script hash does not match what was shown", 409, true);
|
|
256
|
+
const problem = cwdProblem(request.cwd);
|
|
257
|
+
if (problem)
|
|
258
|
+
refuse(problem, 409, true);
|
|
259
|
+
if (!claim(stateDir, project, id, { decision: "run", by, at: now }))
|
|
260
|
+
refuse("already decided");
|
|
261
|
+
audit(stateDir, { event: "run", ...fields, decision, cwd: request.cwd, script: request.script });
|
|
262
|
+
execute(stateDir, request, by, onUpdate, env);
|
|
263
|
+
// The folder watch cannot see result.json, so push the running card now rather than at the first output or the exit.
|
|
264
|
+
onUpdate();
|
|
265
|
+
return { id, status: "running" };
|
|
266
|
+
}
|
|
267
|
+
const logFile = (stateDir, request, key) => path.join(dirOf(stateDir, request.project, request.id), `${key}.log`);
|
|
268
|
+
const exitFile = (stateDir, request) => path.join(dirOf(stateDir, request.project, request.id), "exit");
|
|
269
|
+
/** The last TAIL_BYTES of a log file, "" when it is missing. */
|
|
270
|
+
function readTail(file) {
|
|
271
|
+
let fd;
|
|
272
|
+
try {
|
|
273
|
+
fd = fs.openSync(file, "r");
|
|
274
|
+
}
|
|
275
|
+
catch {
|
|
276
|
+
return "";
|
|
277
|
+
}
|
|
278
|
+
try {
|
|
279
|
+
const size = fs.fstatSync(fd).size;
|
|
280
|
+
const length = Math.min(size, TAIL_BYTES);
|
|
281
|
+
const buffer = Buffer.alloc(length);
|
|
282
|
+
fs.readSync(fd, buffer, 0, length, size - length);
|
|
283
|
+
return buffer.toString("utf8");
|
|
284
|
+
}
|
|
285
|
+
finally {
|
|
286
|
+
fs.closeSync(fd);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
const tails = (stateDir, request) => ({ stdoutTail: readTail(logFile(stateDir, request, "stdout")), stderrTail: readTail(logFile(stateDir, request, "stderr")) });
|
|
290
|
+
/** The script's exit as the wrapper recorded it, or null before it ends. */
|
|
291
|
+
function exitOf(stateDir, request) {
|
|
292
|
+
let text;
|
|
293
|
+
try {
|
|
294
|
+
text = fs.readFileSync(exitFile(stateDir, request), "utf8").trim();
|
|
295
|
+
}
|
|
296
|
+
catch {
|
|
297
|
+
return null;
|
|
298
|
+
}
|
|
299
|
+
const code = Number(text);
|
|
300
|
+
if (!/^\d+$/.test(text))
|
|
301
|
+
return { exitCode: null, signal: null };
|
|
302
|
+
const signal = code > 128 ? Object.entries(os.constants.signals).find(([, number]) => number === code - 128)?.[0] : undefined;
|
|
303
|
+
return signal ? { exitCode: null, signal } : { exitCode: code, signal: null };
|
|
304
|
+
}
|
|
305
|
+
/** True while pid is this run's wrapper: the process exists and its command line names the run's folder. */
|
|
306
|
+
function alive(pid, id) {
|
|
307
|
+
if (!Number.isInteger(pid) || pid < 2)
|
|
308
|
+
return false;
|
|
309
|
+
try {
|
|
310
|
+
return fs.readFileSync(`/proc/${pid}/cmdline`, "utf8").includes(`/${id}/exit`);
|
|
311
|
+
}
|
|
312
|
+
catch {
|
|
313
|
+
return false;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* The command that starts the wrapper. Under systemd the run gets its own transient scope, so a restart of the UI
|
|
318
|
+
* service (which kills the service's control group) leaves it running; TEMMO_RUN_SCOPE=0 turns that off.
|
|
319
|
+
*/
|
|
320
|
+
function launcher(stateDir, request, env) {
|
|
321
|
+
const args = ["--noprofile", "--norc", "-c", WRAPPER, "temmo-run", request.script, exitFile(stateDir, request)];
|
|
322
|
+
const systemdRun = env.INVOCATION_ID && env.TEMMO_RUN_SCOPE !== "0" ? findOnPath("systemd-run", env.PATH) : "";
|
|
323
|
+
if (!systemdRun)
|
|
324
|
+
return { file: "bash", args, unit: undefined };
|
|
325
|
+
const unit = `temmo-run-${request.id}`;
|
|
326
|
+
return { file: systemdRun, args: ["--user", "--scope", "--quiet", "--collect", `--unit=${unit}`, "--", "bash", ...args], unit };
|
|
327
|
+
}
|
|
328
|
+
function findOnPath(name, pathEnv) {
|
|
329
|
+
for (const dir of (pathEnv || "").split(path.delimiter).filter(Boolean)) {
|
|
330
|
+
const file = path.join(dir, name);
|
|
331
|
+
try {
|
|
332
|
+
fs.accessSync(file, fs.constants.X_OK);
|
|
333
|
+
return file;
|
|
334
|
+
}
|
|
335
|
+
catch { /* not here */ }
|
|
336
|
+
}
|
|
337
|
+
return "";
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* The script's environment: the server's minus its own variables, with this node's folder and ~/.local/bin put in
|
|
341
|
+
* front of PATH when missing, because the UI service unit sets no PATH and a script calling node or temmo exits 127.
|
|
342
|
+
*/
|
|
343
|
+
export function scriptEnv(env) {
|
|
344
|
+
const out = Object.fromEntries(Object.entries(env).filter(([name]) => !SERVER_ENV.test(name)));
|
|
345
|
+
const dirs = (out.PATH || "").split(path.delimiter).filter(Boolean);
|
|
346
|
+
const add = [path.dirname(process.execPath), path.join(out.HOME || os.homedir(), ".local", "bin")].filter((dir) => !dirs.includes(dir));
|
|
347
|
+
return { ...out, PATH: [...new Set(add), ...dirs].join(path.delimiter) };
|
|
348
|
+
}
|
|
349
|
+
// Runs the request's script with bash in its cwd, as this process's user without its own variables. Output goes to
|
|
350
|
+
// files in the run's folder, not pipes, so the script keeps writing when the server that started it exits.
|
|
351
|
+
function execute(stateDir, request, by, onUpdate, env) {
|
|
352
|
+
const startedAt = Date.now();
|
|
353
|
+
const out = fs.openSync(logFile(stateDir, request, "stdout"), "w", 0o600);
|
|
354
|
+
const err = fs.openSync(logFile(stateDir, request, "stderr"), "w", 0o600);
|
|
355
|
+
const { file, args, unit } = launcher(stateDir, request, env);
|
|
356
|
+
let child;
|
|
357
|
+
try {
|
|
358
|
+
child = spawn(file, args, { cwd: request.cwd, env: scriptEnv(env), stdio: ["ignore", out, err], detached: true });
|
|
359
|
+
}
|
|
360
|
+
finally {
|
|
361
|
+
fs.closeSync(out);
|
|
362
|
+
fs.closeSync(err);
|
|
363
|
+
}
|
|
364
|
+
follow(stateDir, request, by, onUpdate, { pid: child.pid, startedAt, unit }, child);
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Tracks a started run until it ends: refreshes the output tails while it runs, kills its process group at the
|
|
368
|
+
* timeout, and writes the final result. child is set when this server started the run; an adopted run is polled.
|
|
369
|
+
*/
|
|
370
|
+
function follow(stateDir, request, by, onUpdate, run, child) {
|
|
371
|
+
const { pid, startedAt, unit } = run;
|
|
372
|
+
let timedOut = false;
|
|
373
|
+
let finished = false;
|
|
374
|
+
let shown = "";
|
|
375
|
+
const result = (extra) => ({ status: "running", startedAt, ...(pid ? { pid } : {}), ...(unit ? { unit } : {}), ...tails(stateDir, request), ...extra });
|
|
376
|
+
const refresh = () => {
|
|
377
|
+
const current = result({});
|
|
378
|
+
const key = current.stdoutTail + "\0" + current.stderrTail;
|
|
379
|
+
if (key === shown)
|
|
380
|
+
return;
|
|
381
|
+
shown = key;
|
|
382
|
+
writeResult(stateDir, request.project, request.id, current);
|
|
383
|
+
onUpdate();
|
|
384
|
+
};
|
|
385
|
+
const kill = (signal) => { try {
|
|
386
|
+
if (pid)
|
|
387
|
+
process.kill(-pid, signal);
|
|
388
|
+
}
|
|
389
|
+
catch { /* the group already exited */ } };
|
|
390
|
+
const finish = (exitCode, signal, error) => {
|
|
391
|
+
if (finished)
|
|
392
|
+
return;
|
|
393
|
+
finished = true;
|
|
394
|
+
clearInterval(poll);
|
|
395
|
+
clearTimeout(timer);
|
|
396
|
+
const finishedAt = Date.now();
|
|
397
|
+
const done = result({ status: "done", exitCode, signal, timedOut, finishedAt, durationMs: finishedAt - startedAt, ...(error ? { error } : {}) });
|
|
398
|
+
writeResult(stateDir, request.project, request.id, done);
|
|
399
|
+
audit(stateDir, { event: "finished", id: request.id, project: request.project, session: request.session, sha256: request.sha256, by, exitCode, signal, timedOut, durationMs: done.durationMs });
|
|
400
|
+
onUpdate();
|
|
401
|
+
};
|
|
402
|
+
const initial = result({});
|
|
403
|
+
shown = initial.stdoutTail + "\0" + initial.stderrTail;
|
|
404
|
+
writeResult(stateDir, request.project, request.id, initial);
|
|
405
|
+
const poll = setInterval(() => {
|
|
406
|
+
if (!child) {
|
|
407
|
+
const exit = exitOf(stateDir, request);
|
|
408
|
+
if (exit)
|
|
409
|
+
return finish(exit.exitCode, exit.signal, undefined);
|
|
410
|
+
if (!alive(pid, request.id))
|
|
411
|
+
return timedOut ? finish(null, "SIGTERM", undefined) : lost();
|
|
412
|
+
}
|
|
413
|
+
refresh();
|
|
414
|
+
}, FLUSH_MS);
|
|
415
|
+
const timer = setTimeout(() => {
|
|
416
|
+
timedOut = true;
|
|
417
|
+
kill("SIGTERM");
|
|
418
|
+
setTimeout(() => kill("SIGKILL"), KILL_GRACE_MS).unref();
|
|
419
|
+
}, Math.max(0, startedAt + request.timeoutS * 1000 - Date.now()));
|
|
420
|
+
// An adopted run that vanished without an exit record died outside this server: it is lost, as before.
|
|
421
|
+
const lost = () => {
|
|
422
|
+
finished = true;
|
|
423
|
+
clearInterval(poll);
|
|
424
|
+
clearTimeout(timer);
|
|
425
|
+
writeResult(stateDir, request.project, request.id, result({ status: "lost", lostAt: Date.now() }));
|
|
426
|
+
audit(stateDir, { event: "lost", id: request.id, project: request.project, session: request.session, sha256: request.sha256 });
|
|
427
|
+
onUpdate();
|
|
428
|
+
};
|
|
429
|
+
if (!child) {
|
|
430
|
+
// An adopted run does not hold the server open, as a child it started does.
|
|
431
|
+
poll.unref();
|
|
432
|
+
timer.unref();
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
child.on("error", (error) => finish(null, null, error.message));
|
|
436
|
+
child.on("close", (code, signal) => finish(code, signal, undefined));
|
|
437
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Local runner transport shared with the dependency-free CLI and MCP plugin.
|
|
2
|
+
import http from "node:http";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { stateDir } from "./config.js";
|
|
5
|
+
import { productDir } from "./names.js";
|
|
6
|
+
/** The control socket path: TEMMO_RUNNER_SOCKET, else $XDG_RUNTIME_DIR/temmo/runner.sock, else the state dir. */
|
|
7
|
+
export function runnerSocket(env = process.env) {
|
|
8
|
+
if (env.TEMMO_RUNNER_SOCKET)
|
|
9
|
+
return env.TEMMO_RUNNER_SOCKET;
|
|
10
|
+
return env.XDG_RUNTIME_DIR ? join(productDir(env.XDG_RUNTIME_DIR), "runner.sock") : join(stateDir(env), "runner.sock");
|
|
11
|
+
}
|
|
12
|
+
export const CLIENT_TIMEOUT_MS = 70_000;
|
|
13
|
+
/** One JSON request over the unix socket; resolves {status, body}; rejects when the runner is not listening. */
|
|
14
|
+
export function runnerRequest(method, path, body, { socket = runnerSocket(), timeoutMs = CLIENT_TIMEOUT_MS } = {}) {
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
const data = body === undefined ? undefined : JSON.stringify(body);
|
|
17
|
+
const req = http.request({ socketPath: socket, method, path, timeout: timeoutMs, headers: data ? { "content-type": "application/json", "content-length": Buffer.byteLength(data) } : {} }, (res) => {
|
|
18
|
+
const chunks = [];
|
|
19
|
+
res.on("data", (c) => chunks.push(c));
|
|
20
|
+
res.on("end", () => {
|
|
21
|
+
let value = null;
|
|
22
|
+
try {
|
|
23
|
+
value = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
24
|
+
}
|
|
25
|
+
catch { /* not JSON */ }
|
|
26
|
+
resolve({ status: res.statusCode, body: value });
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
req.on("timeout", () => req.destroy(new Error("runner request timed out")));
|
|
30
|
+
req.on("error", reject);
|
|
31
|
+
req.end(data);
|
|
32
|
+
});
|
|
33
|
+
}
|