@temmo/cli 0.0.0 → 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 +112 -1
- 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,265 @@
|
|
|
1
|
+
// Git ref and worktree lookups for session start: branch and worktree pickers, and validation of caller-supplied refs.
|
|
2
|
+
import { execFile } from "node:child_process";
|
|
3
|
+
import { lstat } from "node:fs/promises";
|
|
4
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
5
|
+
import { findExecutable } from "./scope.js";
|
|
6
|
+
/** Most refs a picker shows. */
|
|
7
|
+
export const REF_LIMIT = 50;
|
|
8
|
+
/** Longest ref name accepted from a caller. */
|
|
9
|
+
export const REF_NAME_MAX = 200;
|
|
10
|
+
const REF_FORMAT = "%(refname)%00%(committerdate:unix)%00%(objectname:short)";
|
|
11
|
+
const NOT_A_WORKTREE = "worktree is not one of this project's worktrees";
|
|
12
|
+
const FULL_SHA = /^[0-9a-f]{40}([0-9a-f]{24})?$/;
|
|
13
|
+
/** Most untracked files one diff lists. */
|
|
14
|
+
const UNTRACKED_MAX = 200;
|
|
15
|
+
const given = (value) => value !== undefined && value !== null && value !== "";
|
|
16
|
+
/** Records from `git worktree list --porcelain`; the first record is the main worktree. */
|
|
17
|
+
export function parseWorktreeList(text) {
|
|
18
|
+
const rows = [];
|
|
19
|
+
let row = null;
|
|
20
|
+
for (const line of text.split("\n")) {
|
|
21
|
+
if (line === "") {
|
|
22
|
+
if (row)
|
|
23
|
+
rows.push(row);
|
|
24
|
+
row = null;
|
|
25
|
+
}
|
|
26
|
+
else if (line.startsWith("worktree ")) {
|
|
27
|
+
row = { path: line.slice("worktree ".length), head: "", branch: null, detached: false, locked: false, prunable: false };
|
|
28
|
+
}
|
|
29
|
+
else if (row) {
|
|
30
|
+
if (line.startsWith("HEAD "))
|
|
31
|
+
row.head = line.slice("HEAD ".length);
|
|
32
|
+
else if (line.startsWith("branch "))
|
|
33
|
+
row.branch = line.slice("branch ".length);
|
|
34
|
+
else if (line === "detached")
|
|
35
|
+
row.detached = true;
|
|
36
|
+
else if (line === "locked" || line.startsWith("locked "))
|
|
37
|
+
row.locked = true;
|
|
38
|
+
else if (line === "prunable" || line.startsWith("prunable "))
|
|
39
|
+
row.prunable = true;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (row)
|
|
43
|
+
rows.push(row);
|
|
44
|
+
return rows;
|
|
45
|
+
}
|
|
46
|
+
/** A GitRun over execFile with no shell; a failed spawn or non-zero exit resolves with its code and empty stdout. */
|
|
47
|
+
export function gitRunner(gitFile, env) {
|
|
48
|
+
return (args, cwd) => new Promise((done) => {
|
|
49
|
+
execFile(gitFile, args, { cwd, env, shell: false, timeout: 30_000, maxBuffer: 16 * 1024 * 1024, encoding: "utf8" }, (error, stdout) => {
|
|
50
|
+
const code = error?.code;
|
|
51
|
+
if (error)
|
|
52
|
+
done({ code: typeof code === "number" ? code : 1, stdout: "" });
|
|
53
|
+
else
|
|
54
|
+
done({ code: 0, stdout });
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/** Refs from `git for-each-ref` output in REF_FORMAT; anything outside refs/heads and refs/remotes is ignored. */
|
|
59
|
+
export function parseRefs(text) {
|
|
60
|
+
const refs = [];
|
|
61
|
+
for (const line of text.split("\n")) {
|
|
62
|
+
if (line.trim() === "")
|
|
63
|
+
continue;
|
|
64
|
+
const [refname, when, sha] = line.split("\0");
|
|
65
|
+
if (!refname || !sha)
|
|
66
|
+
continue;
|
|
67
|
+
const remote = refname.startsWith("refs/remotes/");
|
|
68
|
+
const prefix = remote ? "refs/remotes/" : "refs/heads/";
|
|
69
|
+
const name = refname.startsWith(prefix) ? refname.slice(prefix.length) : "";
|
|
70
|
+
if (name === "" || (remote && name.endsWith("/HEAD")))
|
|
71
|
+
continue;
|
|
72
|
+
const date = Number(when);
|
|
73
|
+
refs.push({ name, remote, date: Number.isFinite(date) ? date : 0, sha });
|
|
74
|
+
}
|
|
75
|
+
return refs;
|
|
76
|
+
}
|
|
77
|
+
/** Case-insensitive name filter: exact, prefix, last-segment prefix, then the rest; first `limit` kept. */
|
|
78
|
+
export function filterRefs(refs, query, limit = REF_LIMIT) {
|
|
79
|
+
const q = query.trim().toLowerCase();
|
|
80
|
+
const matches = q === "" ? refs.slice() : refs.filter((ref) => ref.name.toLowerCase().includes(q));
|
|
81
|
+
if (q !== "") {
|
|
82
|
+
const rank = (name) => {
|
|
83
|
+
const n = name.toLowerCase();
|
|
84
|
+
if (n === q)
|
|
85
|
+
return 0;
|
|
86
|
+
if (n.startsWith(q))
|
|
87
|
+
return 1;
|
|
88
|
+
return n.slice(n.lastIndexOf("/") + 1).startsWith(q) ? 2 : 3;
|
|
89
|
+
};
|
|
90
|
+
// Array#sort is stable, so input order survives inside each rank.
|
|
91
|
+
matches.sort((a, b) => rank(a.name) - rank(b.name));
|
|
92
|
+
}
|
|
93
|
+
return { items: matches.slice(0, limit), truncated: matches.length > limit };
|
|
94
|
+
}
|
|
95
|
+
/** Branch picker data: filtered refs and the current branch ("" when HEAD is detached). */
|
|
96
|
+
export async function listBranches(run, root, { query = "", limit = REF_LIMIT } = {}) {
|
|
97
|
+
const [refs, head] = await Promise.all([
|
|
98
|
+
run(["for-each-ref", "--sort=-committerdate", `--format=${REF_FORMAT}`, "refs/heads", "refs/remotes"], root),
|
|
99
|
+
run(["symbolic-ref", "--quiet", "--short", "HEAD"], root),
|
|
100
|
+
]);
|
|
101
|
+
const { items, truncated } = filterRefs(parseRefs(refs.stdout), query, limit);
|
|
102
|
+
return { current: head.code === 0 ? head.stdout.trim() : "", branches: items, truncated };
|
|
103
|
+
}
|
|
104
|
+
/** Non-main worktrees filtered on path or branch; only the kept rows get a dirty check. */
|
|
105
|
+
export async function listWorktrees(run, root, { query = "", limit = REF_LIMIT } = {}) {
|
|
106
|
+
const listing = await run(["worktree", "list", "--porcelain"], root);
|
|
107
|
+
const q = query.trim().toLowerCase();
|
|
108
|
+
const rows = parseWorktreeList(listing.stdout)
|
|
109
|
+
.slice(1)
|
|
110
|
+
.filter((rec) => !rec.prunable)
|
|
111
|
+
.map((rec) => ({
|
|
112
|
+
path: rec.path,
|
|
113
|
+
branch: rec.branch?.replace(/^refs\/heads\//, "") ?? null,
|
|
114
|
+
head: rec.head,
|
|
115
|
+
locked: rec.locked,
|
|
116
|
+
dirty: false,
|
|
117
|
+
}));
|
|
118
|
+
const matches = q === "" ? rows : rows.filter((row) => row.path.toLowerCase().includes(q) || (row.branch ?? "").toLowerCase().includes(q));
|
|
119
|
+
const worktrees = matches.slice(0, limit);
|
|
120
|
+
await Promise.all(worktrees.map(async (row) => {
|
|
121
|
+
const status = await run(["--no-optional-locks", "status", "--porcelain", "--ignore-submodules=dirty"], row.path);
|
|
122
|
+
row.dirty = status.code === 0 && status.stdout.trim() !== "";
|
|
123
|
+
}));
|
|
124
|
+
return { worktrees, truncated: matches.length > limit };
|
|
125
|
+
}
|
|
126
|
+
/** Why `value` cannot go on a git command line as a ref name; "" when fine. */
|
|
127
|
+
export function refInputError(value, what) {
|
|
128
|
+
if (typeof value !== "string")
|
|
129
|
+
return `${what} must be a git ref name`;
|
|
130
|
+
const name = value.trim();
|
|
131
|
+
if (name === "")
|
|
132
|
+
return `${what} must be a git ref name`;
|
|
133
|
+
if (name.length > REF_NAME_MAX)
|
|
134
|
+
return `${what} must be ${REF_NAME_MAX} characters or fewer`;
|
|
135
|
+
if (name.startsWith("-"))
|
|
136
|
+
return `${what} cannot start with "-"`;
|
|
137
|
+
if (/[\s\x00-\x1f\x7f]/.test(name))
|
|
138
|
+
return `${what} cannot contain whitespace or control characters`;
|
|
139
|
+
if (name.includes("@{"))
|
|
140
|
+
return `${what} cannot contain "@{"`;
|
|
141
|
+
return "";
|
|
142
|
+
}
|
|
143
|
+
/** Why `name` cannot be a new branch in this repo; "" when it can. */
|
|
144
|
+
export async function checkBranchName(run, root, name) {
|
|
145
|
+
const error = refInputError(name, "branch");
|
|
146
|
+
if (error !== "")
|
|
147
|
+
return error;
|
|
148
|
+
const branch = String(name).trim();
|
|
149
|
+
if ((await run(["check-ref-format", "--branch", branch], root)).code !== 0)
|
|
150
|
+
return `branch ${branch} is not a valid branch name`;
|
|
151
|
+
if ((await run(["show-ref", "--verify", "--quiet", `refs/heads/${branch}`], root)).code === 0)
|
|
152
|
+
return `branch ${branch} already exists`;
|
|
153
|
+
return "";
|
|
154
|
+
}
|
|
155
|
+
/** Full commit sha for `ref`, or why it cannot be a base commit. */
|
|
156
|
+
export async function resolveBase(run, root, ref) {
|
|
157
|
+
const error = refInputError(ref, "base");
|
|
158
|
+
if (error !== "")
|
|
159
|
+
return { error };
|
|
160
|
+
const name = String(ref).trim();
|
|
161
|
+
const res = await run(["rev-parse", "--verify", "--quiet", "--end-of-options", `${name}^{commit}`], root);
|
|
162
|
+
const sha = res.stdout.trim();
|
|
163
|
+
if (!FULL_SHA.test(sha))
|
|
164
|
+
return { error: `base ${name} is not a commit here` };
|
|
165
|
+
return { sha };
|
|
166
|
+
}
|
|
167
|
+
/** The registered worktree `path` points at, or why it is not one of this project's worktrees. */
|
|
168
|
+
export async function checkWorktreePath(run, root, path) {
|
|
169
|
+
if (typeof path !== "string" || !isAbsolute(path) || /[\x00-\x1f\x7f]/.test(path))
|
|
170
|
+
return { error: NOT_A_WORKTREE };
|
|
171
|
+
const listing = await run(["worktree", "list", "--porcelain"], root);
|
|
172
|
+
for (const rec of parseWorktreeList(listing.stdout).slice(1)) {
|
|
173
|
+
if (!rec.prunable && resolve(path) === resolve(rec.path))
|
|
174
|
+
return { path: rec.path };
|
|
175
|
+
}
|
|
176
|
+
return { error: NOT_A_WORKTREE };
|
|
177
|
+
}
|
|
178
|
+
/** What a worktree start targets: an existing worktree, or a base sha and a new branch name. */
|
|
179
|
+
export async function worktreeTarget(run, root, { base, branch, worktreePath }) {
|
|
180
|
+
if (given(worktreePath)) {
|
|
181
|
+
if (given(base) || given(branch))
|
|
182
|
+
return { error: "pick an existing worktree or a base, not both" };
|
|
183
|
+
const found = await checkWorktreePath(run, root, worktreePath);
|
|
184
|
+
return found.error !== undefined ? { error: found.error } : { existing: found.path };
|
|
185
|
+
}
|
|
186
|
+
const target = {};
|
|
187
|
+
if (given(base)) {
|
|
188
|
+
const resolved = await resolveBase(run, root, base);
|
|
189
|
+
if (resolved.error !== undefined)
|
|
190
|
+
return { error: resolved.error };
|
|
191
|
+
target.base = resolved.sha;
|
|
192
|
+
}
|
|
193
|
+
if (given(branch)) {
|
|
194
|
+
const error = await checkBranchName(run, root, branch);
|
|
195
|
+
if (error !== "")
|
|
196
|
+
return { error };
|
|
197
|
+
target.branch = String(branch).trim();
|
|
198
|
+
}
|
|
199
|
+
return target;
|
|
200
|
+
}
|
|
201
|
+
/** worktreeTarget against the project's git; {} when nothing was picked, so a plain start needs no git lookup. */
|
|
202
|
+
export async function pickedWorktree(root, pick = {}, env = process.env) {
|
|
203
|
+
if (!given(pick.base) && !given(pick.branch) && !given(pick.worktreePath))
|
|
204
|
+
return {};
|
|
205
|
+
const gitFile = findExecutable("git", env.PATH);
|
|
206
|
+
if (!gitFile)
|
|
207
|
+
return { error: "git is not installed" };
|
|
208
|
+
return worktreeTarget(gitRunner(gitFile, env), root, pick);
|
|
209
|
+
}
|
|
210
|
+
/** PR title, head branch, and a base this repo can check out; null when gh fails or the number is out of range. */
|
|
211
|
+
export async function prHead(ghFile, run, root, number, env) {
|
|
212
|
+
if (!Number.isSafeInteger(number) || number < 1 || number > 9_999_999)
|
|
213
|
+
return null;
|
|
214
|
+
let data;
|
|
215
|
+
try {
|
|
216
|
+
const stdout = await new Promise((ok, fail) => {
|
|
217
|
+
execFile(ghFile, ["pr", "view", String(number), "--json", "number,title,headRefName,headRefOid,isCrossRepository"], { cwd: root, env, shell: false, timeout: 10_000, encoding: "utf8" }, (error, out) => (error ? fail(error) : ok(out)));
|
|
218
|
+
});
|
|
219
|
+
data = JSON.parse(stdout);
|
|
220
|
+
}
|
|
221
|
+
catch {
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
const { title, headRefName, headRefOid, isCrossRepository } = (data ?? {});
|
|
225
|
+
if (typeof title !== "string" || typeof headRefName !== "string" || typeof headRefOid !== "string" || !FULL_SHA.test(headRefOid))
|
|
226
|
+
return null;
|
|
227
|
+
let base = null;
|
|
228
|
+
if ((await run(["cat-file", "-e", `${headRefOid}^{commit}`], root)).code === 0)
|
|
229
|
+
base = headRefOid;
|
|
230
|
+
else if (isCrossRepository !== true && (await run(["rev-parse", "--verify", "--quiet", "--end-of-options", `refs/remotes/origin/${headRefName}^{commit}`], root)).code === 0)
|
|
231
|
+
base = `origin/${headRefName}`;
|
|
232
|
+
return { number, title, head: headRefName, base };
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Untracked, non-ignored files of a worktree as new-file diffs, read with `git diff --no-index` so the index stays as it is.
|
|
236
|
+
* A file over fileBytes is left out, and listing stops before the text would pass totalBytes.
|
|
237
|
+
*/
|
|
238
|
+
export async function untrackedDiff(gitFile, tree, env, { fileBytes, totalBytes }) {
|
|
239
|
+
const listed = await gitText(gitFile, ["ls-files", "-z", "--others", "--exclude-standard"], tree, env, 4 * 1024 * 1024);
|
|
240
|
+
const files = (listed ?? "").split("\0").filter(name => name && !name.endsWith("/")).slice(0, UNTRACKED_MAX);
|
|
241
|
+
let out = "";
|
|
242
|
+
let bytes = 0;
|
|
243
|
+
for (const name of files) {
|
|
244
|
+
const stat = await lstat(join(tree, name)).catch(() => null);
|
|
245
|
+
if (!stat || !(stat.isFile() || stat.isSymbolicLink()) || stat.size > fileBytes)
|
|
246
|
+
continue;
|
|
247
|
+
// --no-index exits 1 when the paths differ, which a new file always does.
|
|
248
|
+
const diff = await gitText(gitFile, ["diff", "--no-index", "--no-color", "--no-ext-diff", "--no-textconv", "--", "/dev/null", name], tree, env, fileBytes * 2 + 4096, [0, 1]);
|
|
249
|
+
if (!diff)
|
|
250
|
+
continue;
|
|
251
|
+
bytes += Buffer.byteLength(diff);
|
|
252
|
+
if (bytes > totalBytes)
|
|
253
|
+
break;
|
|
254
|
+
out += diff;
|
|
255
|
+
}
|
|
256
|
+
return out;
|
|
257
|
+
}
|
|
258
|
+
function gitText(gitFile, args, cwd, env, maxBuffer, ok = [0]) {
|
|
259
|
+
return new Promise(done => {
|
|
260
|
+
execFile(gitFile, args, { cwd, env, shell: false, timeout: 10_000, maxBuffer, encoding: "utf8" }, (error, stdout) => {
|
|
261
|
+
const code = error ? error.code : 0;
|
|
262
|
+
done(typeof code === "number" && ok.includes(code) ? stdout : null);
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
}
|
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
// Launch of a headless session: refusals, placement, the child environment, and startSession.
|
|
2
|
+
import { execFile, spawn } from "node:child_process";
|
|
3
|
+
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, readSync, statSync } from "node:fs";
|
|
4
|
+
import { basename, delimiter, isAbsolute, join, resolve, sep } from "node:path";
|
|
5
|
+
import { ALLOW_HOST_AGENT_ENV, hostAgentRefusal } from "../agent-guard.js";
|
|
6
|
+
import { claudePermissionSetting, listProjects, sessionLabel, startPlaceSetting } from "../config.js";
|
|
7
|
+
import { REQUIRE_RUN_CODE_ENV, resolveCodeMode } from "../code-mode.js";
|
|
8
|
+
import { SYSTEM_PROMPT_ENV } from "../machine-prompt.js";
|
|
9
|
+
import { recordedMode, WAKE_ENV } from "../mode.js";
|
|
10
|
+
import { isOwnEnvKey } from "../names.js";
|
|
11
|
+
import { isProjectPaused, PROJECT_PAUSED } from "../project-pause.js";
|
|
12
|
+
import { runnerSocket } from "../runner-request.js";
|
|
13
|
+
import { argv, childPath, claudePermissionError, claudePermissionFlag, CURSOR_BINARIES, cursorCommandFor, HARNESSES, LAUNCH_BINARY, modelShape } from "../harness.js";
|
|
14
|
+
import { modeFlags } from "../../web/lib/start-mode.js";
|
|
15
|
+
import { placeCommand, SANDBOX_NAME } from "./control.js";
|
|
16
|
+
import { findExecutable, scopeLaunch, utcStamp } from "./scope.js";
|
|
17
|
+
import { recordSession } from "./registry.js";
|
|
18
|
+
import { lessonsNote } from "../lessons.js";
|
|
19
|
+
import { setupNote, setupWorktree } from "../worktree-setup.js";
|
|
20
|
+
import { noSubagentArgs, noSubagentEnv } from "../subagent-guard.js";
|
|
21
|
+
import { pickedWorktree } from "./git-refs.js";
|
|
22
|
+
export const START_HARNESSES = HARNESSES;
|
|
23
|
+
const START_PLACES = ["folder", "worktree", "openshell"];
|
|
24
|
+
// Harnesses the runner can start in its own per-project OpenShell sandbox (runner/adapters capabilities.places).
|
|
25
|
+
const RUNNER_SANDBOX_HARNESSES = ["claude", "codex", "cursor", "opencode", "pi"];
|
|
26
|
+
const GIT_TIMEOUT_MS = 30_000;
|
|
27
|
+
export const PROMPT_MAX_CHARS = 4000;
|
|
28
|
+
const ID_WAIT_MS = 15_000;
|
|
29
|
+
const ID_POLL_MS = 200;
|
|
30
|
+
const LOG_READ_MAX = 64 * 1024;
|
|
31
|
+
const LOG_END_MAX = 256 * 1024;
|
|
32
|
+
const UUID_IN_TEXT = /\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/i;
|
|
33
|
+
/** Why a start is refused before spawning: an unknown harness, a mode it lacks, or a model it cannot pass; "" when fine. */
|
|
34
|
+
export function startRefusal(harness, mode = "agent", model) {
|
|
35
|
+
if (!LAUNCH_BINARY[harness])
|
|
36
|
+
return "Unknown harness";
|
|
37
|
+
if (!modeFlags(harness, mode))
|
|
38
|
+
return `Unknown ${harness} mode`;
|
|
39
|
+
if (model !== undefined && !modelShape(harness, model))
|
|
40
|
+
return "Unknown harness or model";
|
|
41
|
+
return "";
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Fixed argv per harness: [file, args]; worktree names Claude's own --worktree; model must pass modelShape.
|
|
45
|
+
* The mode comes from the new-agent dialogue's Agent / Ask / Plan pill: its flags are the harness's own read-only
|
|
46
|
+
* switches (web/lib/start-mode.js), so "agent" adds nothing and a mode the harness lacks returns null.
|
|
47
|
+
*/
|
|
48
|
+
export function startCommand(harness, root, prompt, { worktree = "", model, mode, permission, codeMode, requireRunCode = false, managed = false } = {}) {
|
|
49
|
+
return argv(harness, "start", { root, prompt, worktree, model, mode, permission, codeMode, requireRunCode, ...(managed ? { noSubagents: noSubagentArgs(harness) } : {}) });
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The --permission-mode a start of this harness passes: a named setting (a team's or an instance's) replaces the
|
|
53
|
+
* configured one. Claude only; {flag: undefined} passes nothing, {error} refuses the start. "auto-afk" is default while
|
|
54
|
+
* AFK is on or quiet. bypassPermissions runs only in OpenShell (sandboxed) or with TEMMO_ALLOW_HOST_AGENT=1.
|
|
55
|
+
*/
|
|
56
|
+
export function claudePermissionFor(harness, cfg, env = process.env, { sandboxed = false, setting: named } = {}) {
|
|
57
|
+
if (harness !== "claude")
|
|
58
|
+
return {};
|
|
59
|
+
if (named !== undefined && claudePermissionError(named))
|
|
60
|
+
return { error: claudePermissionError(named) };
|
|
61
|
+
const configured = named === undefined ? claudePermissionSetting(env) : undefined;
|
|
62
|
+
if (configured?.error)
|
|
63
|
+
return { error: configured.error };
|
|
64
|
+
const mode = named ?? configured?.mode ?? "auto";
|
|
65
|
+
const afk = ["on", "quiet"].includes(recordedMode(cfg)?.mode);
|
|
66
|
+
const flag = claudePermissionFlag(mode, afk);
|
|
67
|
+
const refusal = bypassRefusal(flag, { sandboxed }, env);
|
|
68
|
+
if (refusal)
|
|
69
|
+
return { flag: undefined, error: refusal };
|
|
70
|
+
return { flag };
|
|
71
|
+
}
|
|
72
|
+
/** Why a Claude permission of bypassPermissions may not start here, or "" when it may: outside OpenShell it needs TEMMO_ALLOW_HOST_AGENT=1. */
|
|
73
|
+
export function bypassRefusal(setting, { sandboxed = false } = {}, env = process.env) {
|
|
74
|
+
if (setting !== "bypassPermissions" || sandboxed || env[ALLOW_HOST_AGENT_ENV] === "1")
|
|
75
|
+
return "";
|
|
76
|
+
return `bypassPermissions runs only in openshell; set ${ALLOW_HOST_AGENT_ENV}=1 to allow it on the host`;
|
|
77
|
+
}
|
|
78
|
+
/** Maps a startCommand's cursor binary onto this machine's name: `agent` when installed, else `cursor-agent`. */
|
|
79
|
+
export function resolveStartBinary(harness, cmd, path = "") {
|
|
80
|
+
if (!cmd)
|
|
81
|
+
return cmd;
|
|
82
|
+
if (harness !== "cursor")
|
|
83
|
+
return cmd;
|
|
84
|
+
return [cursorCommandFor({ path }), cmd[1]];
|
|
85
|
+
}
|
|
86
|
+
/** The child's environment: no TEMMO_* settings, the usual install dirs on PATH, and marked as not the operator typing. */
|
|
87
|
+
export function childEnv(env = process.env, execPath = process.execPath) {
|
|
88
|
+
const out = Object.fromEntries(Object.entries(env).filter(([key]) => !isOwnEnvKey(key)));
|
|
89
|
+
// The caller's PATH wins; the install dirs only fill in for a service started with a bare PATH.
|
|
90
|
+
out.PATH = childPath(env, execPath);
|
|
91
|
+
out[WAKE_ENV] = "1"; // the phone prompt is not the operator at a terminal, so it must not turn AFK off
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
/** Harnesses this project may start: configured as a peer (or no peer list) and installed. */
|
|
95
|
+
export function availableHarnesses(cfg, env = process.env) {
|
|
96
|
+
const { PATH } = childEnv(env);
|
|
97
|
+
return START_HARNESSES.filter((h) => (!cfg.peers.length || cfg.peers.includes(h))
|
|
98
|
+
&& (h === "cursor" ? CURSOR_BINARIES.some((name) => findExecutable(name, PATH)) : findExecutable(LAUNCH_BINARY[h], PATH)));
|
|
99
|
+
}
|
|
100
|
+
/** The sandbox from TEMMO_OPENSHELL_SANDBOX, the runner wake uses, or null. */
|
|
101
|
+
export function openshellRunner(env = process.env) {
|
|
102
|
+
const sandbox = env.TEMMO_OPENSHELL_SANDBOX || "";
|
|
103
|
+
return SANDBOX_NAME.test(sandbox) ? { kind: "openshell", sandbox } : null;
|
|
104
|
+
}
|
|
105
|
+
/** Whether the runner can take an OpenShell start: openshell installed and the runner's control socket present. */
|
|
106
|
+
function runnerSandbox(env = process.env) {
|
|
107
|
+
return Boolean(findExecutable("openshell", childEnv(env).PATH) && existsSync(runnerSocket(env)));
|
|
108
|
+
}
|
|
109
|
+
/** Harnesses an openshell start may name: any, in the TEMMO_OPENSHELL_SANDBOX sandbox; else the runner's sandbox harnesses. */
|
|
110
|
+
export function openshellHarnesses(env = process.env) {
|
|
111
|
+
return openshellRunner(env) ? START_HARNESSES : RUNNER_SANDBOX_HARNESSES;
|
|
112
|
+
}
|
|
113
|
+
/** The registered root folder of a project name; throws when it is not in projects.json or is not a directory. */
|
|
114
|
+
export function projectRoot(name, env = process.env) {
|
|
115
|
+
const entry = listProjects(env).find((p) => p.project === name);
|
|
116
|
+
if (!entry)
|
|
117
|
+
throw Object.assign(new Error("Project is not registered in projects.json"), { status: 400 });
|
|
118
|
+
try {
|
|
119
|
+
if (statSync(entry.root).isDirectory())
|
|
120
|
+
return entry.root;
|
|
121
|
+
}
|
|
122
|
+
catch { /* missing */ }
|
|
123
|
+
throw Object.assign(new Error("The project folder is unavailable"), { status: 400 });
|
|
124
|
+
}
|
|
125
|
+
/** Where this project can start a session: its folder, a new git worktree (git repo and git installed), an openshell sandbox. */
|
|
126
|
+
export function availablePlaces(cfg, env = process.env) {
|
|
127
|
+
const { PATH } = childEnv(env);
|
|
128
|
+
const places = ["folder"];
|
|
129
|
+
let root = "";
|
|
130
|
+
try {
|
|
131
|
+
root = projectRoot(cfg.project, env);
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
return places;
|
|
135
|
+
}
|
|
136
|
+
if (existsSync(join(root, ".git")) && findExecutable("git", PATH))
|
|
137
|
+
places.push("worktree");
|
|
138
|
+
if ((openshellRunner(env) && findExecutable("openshell", PATH)) || (existsSync(join(root, ".git")) && runnerSandbox(env)))
|
|
139
|
+
places.push("openshell");
|
|
140
|
+
return places;
|
|
141
|
+
}
|
|
142
|
+
/** The place a start gets when it names none: defaults.json startPlace when offered and this harness can run there, else folder. */
|
|
143
|
+
export function defaultStartPlace(places, harness, env = process.env) {
|
|
144
|
+
const preferred = startPlaceSetting(env);
|
|
145
|
+
if (!places.includes(preferred))
|
|
146
|
+
return "folder";
|
|
147
|
+
if (preferred === "openshell" && !openshellHarnesses(env).includes(harness))
|
|
148
|
+
return "folder";
|
|
149
|
+
return preferred;
|
|
150
|
+
}
|
|
151
|
+
/** The configured default place for a start that startSession launches itself: OpenShell only with a shared sandbox. */
|
|
152
|
+
export function sessionStartPlace(cfg, harness, env = process.env) {
|
|
153
|
+
return defaultStartPlace(availablePlaces(cfg, env).filter((place) => place !== "openshell" || openshellRunner(env)), harness, env);
|
|
154
|
+
}
|
|
155
|
+
// Worktree name and branch for one start: temmo-<harness>-<UTC stamp> on branch temmo/<harness>-<stamp>.
|
|
156
|
+
export function worktreeNames(harness, at) {
|
|
157
|
+
const stamp = utcStamp(at);
|
|
158
|
+
return { name: `temmo-${harness}-${stamp}`, branch: `temmo/${harness}-${stamp}` };
|
|
159
|
+
}
|
|
160
|
+
// Runs git with a fixed argv and no shell; resolves its exit code.
|
|
161
|
+
function runGit(file, args, options) {
|
|
162
|
+
return new Promise((resolve) => {
|
|
163
|
+
execFile(file, args, { ...options, shell: false, timeout: GIT_TIMEOUT_MS }, (error) => resolve(error ? error.code ?? 1 : 0));
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
/** The folder a start runs in: the root, or the origin folder inside it if it still exists; a missing origin is refused. */
|
|
167
|
+
export function startFolder(root, origin = "") {
|
|
168
|
+
if (!origin)
|
|
169
|
+
return { cwd: root };
|
|
170
|
+
const folder = resolve(origin);
|
|
171
|
+
if (folder !== root && !folder.startsWith(root + sep))
|
|
172
|
+
return { error: `The original folder is outside the project: ${folder}` };
|
|
173
|
+
try {
|
|
174
|
+
if (statSync(folder).isDirectory())
|
|
175
|
+
return { cwd: folder };
|
|
176
|
+
}
|
|
177
|
+
catch { /* gone */ }
|
|
178
|
+
return { error: `The original folder is gone: ${folder}; start the session by hand` };
|
|
179
|
+
}
|
|
180
|
+
/** The prompt trimmed, or an error message: 1-4000 characters, no NUL, and no leading "-" that a CLI could read as a flag. */
|
|
181
|
+
export function checkPrompt(prompt) {
|
|
182
|
+
if (typeof prompt !== "string" || !prompt.trim() || prompt.trim().length > PROMPT_MAX_CHARS)
|
|
183
|
+
return { error: "Enter a first prompt of 1–4000 characters" };
|
|
184
|
+
const text = prompt.trim();
|
|
185
|
+
if (text.includes("\0"))
|
|
186
|
+
return { error: "The prompt contains a NUL character" };
|
|
187
|
+
if (text.startsWith("-"))
|
|
188
|
+
return { error: "The prompt cannot start with \"-\"" };
|
|
189
|
+
return { text };
|
|
190
|
+
}
|
|
191
|
+
/** Session or thread id in a harness's first output; "" when none is printed yet. */
|
|
192
|
+
export function outputId(harness, text) {
|
|
193
|
+
for (const line of text.split("\n")) {
|
|
194
|
+
if (harness === "claude") {
|
|
195
|
+
const id = UUID_IN_TEXT.exec(line)?.[0];
|
|
196
|
+
if (id)
|
|
197
|
+
return id;
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
let event;
|
|
201
|
+
try {
|
|
202
|
+
event = JSON.parse(line);
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
const id = harness === "codex" ? event?.thread_id : harness === "opencode" ? event?.sessionID : event?.session_id;
|
|
208
|
+
if (typeof id === "string" && id && id.length <= 128)
|
|
209
|
+
return id;
|
|
210
|
+
}
|
|
211
|
+
return "";
|
|
212
|
+
}
|
|
213
|
+
// The whole log of a short session; "" past the cap, since a long session is not on its first turn.
|
|
214
|
+
function readWhole(file) {
|
|
215
|
+
try {
|
|
216
|
+
return statSync(file).size <= LOG_END_MAX ? readFileSync(file, "utf8") : "";
|
|
217
|
+
}
|
|
218
|
+
catch {
|
|
219
|
+
return "";
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function readHead(file) {
|
|
223
|
+
try {
|
|
224
|
+
const fd = openSync(file, "r");
|
|
225
|
+
try {
|
|
226
|
+
const data = Buffer.alloc(LOG_READ_MAX);
|
|
227
|
+
return data.subarray(0, readSync(fd, data)).toString("utf8");
|
|
228
|
+
}
|
|
229
|
+
finally {
|
|
230
|
+
closeSync(fd);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
catch {
|
|
234
|
+
return "";
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
/** Spawns the harness detached with its log file, waits up to waitMs for its session id, and reports onEnd at exit. */
|
|
238
|
+
export async function startSession(cfg, { harness = "", prompt = "", model, mode = "agent", place = "folder", origin = "", codeMode, requireRunCode = false, system = false, claudePermission, managed = false, pick, env = process.env, run: spawnChild = spawn, git = runGit, now = Date.now, waitMs = ID_WAIT_MS, onEnd } = {}) {
|
|
239
|
+
if (isProjectPaused(cfg))
|
|
240
|
+
return { status: "failed", detail: PROJECT_PAUSED };
|
|
241
|
+
const root = projectRoot(cfg.project, env);
|
|
242
|
+
if (!START_PLACES.includes(place))
|
|
243
|
+
return { status: "failed", detail: "Unknown start place" };
|
|
244
|
+
const refused = startRefusal(harness, mode, model);
|
|
245
|
+
if (refused)
|
|
246
|
+
return { status: "failed", detail: refused };
|
|
247
|
+
// OpenShell is the boundary, as in the runner: an unnamed Claude start there runs with approvals off.
|
|
248
|
+
const permission = claudePermissionFor(harness, cfg, env, { sandboxed: place === "openshell", setting: claudePermission ?? (place === "openshell" ? "bypassPermissions" : undefined) });
|
|
249
|
+
if (permission.error)
|
|
250
|
+
return { status: "failed", detail: permission.error };
|
|
251
|
+
const childVars = childEnv(env);
|
|
252
|
+
if (system)
|
|
253
|
+
childVars[SYSTEM_PROMPT_ENV] = "1";
|
|
254
|
+
if (requireRunCode)
|
|
255
|
+
childVars[REQUIRE_RUN_CODE_ENV] = "1";
|
|
256
|
+
// A session temmo starts (a team member) runs with native subagents off (lib/subagent-guard.ts).
|
|
257
|
+
if (managed && START_HARNESSES.includes(harness)) {
|
|
258
|
+
try {
|
|
259
|
+
Object.assign(childVars, noSubagentEnv(harness, childVars));
|
|
260
|
+
}
|
|
261
|
+
catch (error) {
|
|
262
|
+
return { status: "failed", detail: error.message };
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
const tree = place === "worktree" ? worktreeNames(harness, now()) : null;
|
|
266
|
+
const folder = startFolder(root, origin);
|
|
267
|
+
if (folder.error)
|
|
268
|
+
return { status: "failed", detail: folder.error };
|
|
269
|
+
let cwd = folder.cwd;
|
|
270
|
+
const picked = tree ? await pickedWorktree(root, pick, childVars) : {};
|
|
271
|
+
if (picked.error)
|
|
272
|
+
return { status: "failed", detail: picked.error };
|
|
273
|
+
if (picked.existing)
|
|
274
|
+
cwd = picked.existing;
|
|
275
|
+
// Claude's --worktree takes no base, so a picked base or branch gets a worktree git creates, as other harnesses do.
|
|
276
|
+
const ownTree = Boolean(tree && harness === "claude" && !picked.existing && !picked.base && !picked.branch);
|
|
277
|
+
let setup = { ran: false };
|
|
278
|
+
if (tree && !ownTree && !picked.existing && START_HARNESSES.includes(harness)) {
|
|
279
|
+
const gitFile = findExecutable("git", childVars.PATH);
|
|
280
|
+
if (!gitFile)
|
|
281
|
+
return { status: "failed", detail: "git is not installed" };
|
|
282
|
+
cwd = join(root, ".claude", "worktrees", tree.name);
|
|
283
|
+
const code = await git(gitFile, ["-C", root, "worktree", "add", "-b", picked.branch ?? tree.branch, cwd, ...(picked.base ? [picked.base] : [])], { cwd: root, env: childVars });
|
|
284
|
+
if (code !== 0)
|
|
285
|
+
return { status: "failed", detail: `git worktree add exited ${code}` };
|
|
286
|
+
setup = await setupWorktree(cwd, root, { env: childVars });
|
|
287
|
+
}
|
|
288
|
+
// `opencode run` takes its session folder from PWD, so an inherited PWD would start it outside the project.
|
|
289
|
+
childVars.PWD = cwd;
|
|
290
|
+
// The lesson note rides the hook's SessionStart context now; an OpenShell sandbox cannot read the library on the host, so it keeps the prompt append.
|
|
291
|
+
const lessons = place === "openshell" ? lessonsNote(cfg.stateDir, cfg.project, harness, env) : "";
|
|
292
|
+
const base = startCommand(harness, cwd, prompt + lessons + setupNote(setup), { worktree: ownTree && tree ? tree.name : "", model, mode, permission: permission.flag, requireRunCode, codeMode: resolveCodeMode(codeMode, env), managed });
|
|
293
|
+
if (!base)
|
|
294
|
+
return { status: "failed", detail: startRefusal(harness, mode, model) || "Unknown harness" };
|
|
295
|
+
const sandbox = place === "openshell" ? openshellRunner(env) : null;
|
|
296
|
+
if (place === "openshell" && !sandbox)
|
|
297
|
+
return { status: "failed", detail: "TEMMO_OPENSHELL_SANDBOX is not set" };
|
|
298
|
+
const cmd = resolveStartBinary(harness, base, childVars.PATH);
|
|
299
|
+
const refusal = hostAgentRefusal(cmd, { sandboxed: Boolean(sandbox), env });
|
|
300
|
+
if (refusal)
|
|
301
|
+
return { status: "failed", detail: refusal };
|
|
302
|
+
const command = sandbox ? placeCommand(cmd, cwd, sandbox) : cmd;
|
|
303
|
+
const file = findExecutable(command[0], childVars.PATH);
|
|
304
|
+
if (!file)
|
|
305
|
+
return { status: "failed", detail: `${command[0]} is not installed` };
|
|
306
|
+
const dir = join(cfg.stateDir, cfg.project, "starts");
|
|
307
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
308
|
+
const log = join(dir, `${new Date(now()).toISOString().replace(/[:.]/g, "-")}-${harness}.log`);
|
|
309
|
+
// A scope keeps the session alive across a UI or runner restart; the harness still gets the log file as its stdio.
|
|
310
|
+
const launch = scopeLaunch([file, ...command[1]], {
|
|
311
|
+
env, path: childVars.PATH, kind: "session", label: harness, description: `Temmo ${harness} session`,
|
|
312
|
+
});
|
|
313
|
+
const fd = openSync(log, "a", 0o600);
|
|
314
|
+
let child;
|
|
315
|
+
try {
|
|
316
|
+
child = spawnChild(launch.file, launch.args, { cwd, env: childVars, detached: true, shell: false, stdio: ["ignore", fd, fd] });
|
|
317
|
+
}
|
|
318
|
+
finally {
|
|
319
|
+
closeSync(fd);
|
|
320
|
+
}
|
|
321
|
+
const where = tree ? `${basename(root)}, worktree ${picked.existing ? basename(picked.existing) : tree.name}` : sandbox ? `${basename(root)}, openshell ${sandbox.sandbox}` : basename(root);
|
|
322
|
+
let exit = null;
|
|
323
|
+
const failed = new Promise((resolve) => {
|
|
324
|
+
child.once("error", (error) => resolve({ status: "failed", detail: error.code === "ENOENT" ? `${command[0]} is not installed` : `${command[0]} could not start` }));
|
|
325
|
+
child.once("exit", (code, signal) => {
|
|
326
|
+
exit = { code, signal };
|
|
327
|
+
// The caller learns how the session ended from its log; a callback that throws never reaches the process.
|
|
328
|
+
if (typeof onEnd === "function") {
|
|
329
|
+
try {
|
|
330
|
+
void Promise.resolve(onEnd({ code, signal, log: readWhole(log) })).catch(() => { });
|
|
331
|
+
}
|
|
332
|
+
catch { /* best effort */ }
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
child.unref();
|
|
337
|
+
const deadline = now() + waitMs;
|
|
338
|
+
for (;;) {
|
|
339
|
+
const early = await Promise.race([failed, new Promise((r) => setTimeout(r, ID_POLL_MS, null))]);
|
|
340
|
+
if (early)
|
|
341
|
+
return { ...early, log: basename(log) };
|
|
342
|
+
const id = outputId(harness, readHead(log));
|
|
343
|
+
if (id) {
|
|
344
|
+
// An OpenShell start keeps its place marker across the harness's own hook writes, so /permission and the UI know where it runs.
|
|
345
|
+
if (sandbox)
|
|
346
|
+
await recordSession({ ...cfg, peer: harness, session: sessionLabel(id) }, { id, cwd, harness, event: "started", sandbox: true }).catch(() => { });
|
|
347
|
+
return {
|
|
348
|
+
status: "started", detail: `${harness} started in ${where}`, log: basename(log),
|
|
349
|
+
session: `${harness}#${sessionLabel(id)}`,
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
const ended = exit;
|
|
353
|
+
if (ended && ended.code !== 0)
|
|
354
|
+
return { status: "failed", detail: `${command[0]} exited ${ended.code ?? ended.signal}; see ${basename(log)}`, log: basename(log) };
|
|
355
|
+
if (ended || now() >= deadline) {
|
|
356
|
+
return { status: "started", detail: `${harness} started in ${where}; no session id printed yet`, log: basename(log) };
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|