@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,64 @@
|
|
|
1
|
+
// `land finish` for a pattern member in an OpenShell sandbox: it asks with a LAND_FINISH reply line, the runner finishes its run branch on the host.
|
|
2
|
+
import { realpathSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { execRun, finishBranch, LandRefused } from "./land.js";
|
|
5
|
+
import { sessionsOfRun } from "./pattern-runs.js";
|
|
6
|
+
import { passJs } from "./surfaces/types.js";
|
|
7
|
+
export const LAND_FINISH_PREFIX = "LAND_FINISH ";
|
|
8
|
+
const PROOF_MAX = 3000;
|
|
9
|
+
const PROTECTED = new Set(["main", "master"]);
|
|
10
|
+
const realpath = (path) => { try {
|
|
11
|
+
return realpathSync(path);
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return "";
|
|
15
|
+
} };
|
|
16
|
+
/** The LAND_FINISH request in one of a reply's last two lines (PATTERN_ADVANCE may follow it), or undefined when there is none. */
|
|
17
|
+
export function landFinishRequest(reply) {
|
|
18
|
+
const line = (reply ?? "").trimEnd().split("\n").slice(-2).find((l) => l.startsWith(LAND_FINISH_PREFIX));
|
|
19
|
+
if (!line)
|
|
20
|
+
return undefined;
|
|
21
|
+
try {
|
|
22
|
+
const { proof } = JSON.parse(line.slice(LAND_FINISH_PREFIX.length)) ?? {};
|
|
23
|
+
if (proof === undefined || typeof proof === "string")
|
|
24
|
+
return { proof: (proof ?? "").trim().slice(0, PROOF_MAX) };
|
|
25
|
+
}
|
|
26
|
+
catch { /* malformed JSON */ }
|
|
27
|
+
return { error: 'LAND_FINISH takes JSON: {"proof":"<commands run and their output>"}' };
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Finishes a sandboxed member's run branch on the host. The branch, worktree and landing policy come from the host's
|
|
31
|
+
* records, never from the member: the session must be a member of the running run, run in OpenShell, own the run's
|
|
32
|
+
* worktree, and that worktree must be on team/<run>. syncBack copies the sandbox commits to the worktree first.
|
|
33
|
+
*/
|
|
34
|
+
export async function finishMemberBranch({ record, run, root, proof, syncBack, tasks, finish = finishBranch, exec = execRun }) {
|
|
35
|
+
if (record.place !== "openshell")
|
|
36
|
+
throw new LandRefused("only a sandboxed member finishes through the runner; run temmo land finish");
|
|
37
|
+
if (!run || run.id !== record.patternRun)
|
|
38
|
+
throw new LandRefused("the session is not in this pattern run");
|
|
39
|
+
if (run.status !== "running")
|
|
40
|
+
throw new LandRefused(`run ${run.id} is ${run.status}`);
|
|
41
|
+
if (!sessionsOfRun(run).includes(record.address))
|
|
42
|
+
throw new LandRefused(`${record.address} is not a member of run ${run.id}`);
|
|
43
|
+
const worktree = join(root, ".claude", "worktrees", `team-${run.id}`);
|
|
44
|
+
if (!record.cwd || !realpath(worktree) || realpath(record.cwd) !== realpath(worktree))
|
|
45
|
+
throw new LandRefused("the session does not own its run's worktree");
|
|
46
|
+
const branch = `team/${run.id}`;
|
|
47
|
+
const head = await exec("git", ["symbolic-ref", "--quiet", "--short", "HEAD"], { cwd: worktree, encoding: "utf8" });
|
|
48
|
+
const current = head.status === 0 ? head.stdout.trim() : "";
|
|
49
|
+
if (PROTECTED.has(current) || current !== branch)
|
|
50
|
+
throw new LandRefused(`the run worktree is on ${current || "no branch"}, not ${branch}`);
|
|
51
|
+
const landing = run.definition?.landing;
|
|
52
|
+
if (!proof && landing !== "branch")
|
|
53
|
+
throw new LandRefused("LAND_FINISH needs a proof unless the team's landing is branch");
|
|
54
|
+
await syncBack();
|
|
55
|
+
return finish(branch, passJs({ ...(landing ? { landing } : {}), proof, cwd: worktree, tasks, by: record.address }));
|
|
56
|
+
}
|
|
57
|
+
/** One line for the operator: the PR, the packet's task, or the pushed branch. */
|
|
58
|
+
export function describeFinish(result) {
|
|
59
|
+
if (result.landing === "pr")
|
|
60
|
+
return `pr: ${result.url}${result.created ? "" : " (existing)"}`;
|
|
61
|
+
if (result.landing === "packet")
|
|
62
|
+
return `packet: task ${result.task}`;
|
|
63
|
+
return `branch: pushed ${result.branch}`;
|
|
64
|
+
}
|
package/lib/land.js
ADDED
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
// Landing through GitHub PRs: list and merge green PRs, and deploy origin/main to the host checkout.
|
|
2
|
+
// gh and git run through an injected `run(file, argv, options)` that resolves {status, stdout, stderr}.
|
|
3
|
+
import { execFile, spawnSync } from "node:child_process";
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
6
|
+
import { dirname, isAbsolute, join } from "node:path";
|
|
7
|
+
import { stateDir } from "./config.js";
|
|
8
|
+
import { SERVICE_ROLES, serviceName } from "./names.js";
|
|
9
|
+
import { findExecutable } from "./sessions/scope.js";
|
|
10
|
+
import { writeJson } from "./store.js";
|
|
11
|
+
import { reviewPacket, TITLE_MAX } from "./tasks.js";
|
|
12
|
+
export const PR_FIELDS = "number,title,url,headRefName,headRefOid,additions,deletions,statusCheckRollup";
|
|
13
|
+
export const SERVICES = SERVICE_ROLES.map(serviceName);
|
|
14
|
+
const SHA = /^[0-9a-f]{40}$/;
|
|
15
|
+
const SHORT = 12;
|
|
16
|
+
const AUTO_DEPLOY_EVERY_MS = 5 * 60_000;
|
|
17
|
+
// A kill mid-install breaks node_modules on the host, so the cap leaves room for npm ci and the build.
|
|
18
|
+
export const AUTO_DEPLOY_TIMEOUT_MS = 20 * 60_000;
|
|
19
|
+
// oxlint-disable-next-line quality/no-known-value-widening -- looked up by a runtime check state
|
|
20
|
+
const CHECK_WORDS = { failure: "failing", pending: "pending", none: "missing" };
|
|
21
|
+
export const HOST_CHECKOUT = "/home/b/ai-collab";
|
|
22
|
+
// The UI deploy step: publishes the build and verifies the served build (deploy/build-ui.mjs).
|
|
23
|
+
export const UI_DEPLOY_ARGS = ["run", "build", "--", "--deploy-and-verify"];
|
|
24
|
+
export const BRANCH = /^[a-zA-Z0-9][a-zA-Z0-9._/-]*$/;
|
|
25
|
+
export const LANDING_POLICIES = ["pr", "packet", "branch"];
|
|
26
|
+
const GITHUB_REMOTE = /^(https:\/\/github\.com\/|git@github\.com:)/;
|
|
27
|
+
const PROOF_CLIP = 400;
|
|
28
|
+
const PR_BODY_MAX = 60_000;
|
|
29
|
+
// The npm to run for a Node install: its bundled npm, else the npm on PATH.
|
|
30
|
+
export function npmCommand(node, path) {
|
|
31
|
+
const npmCli = bundledNpm(node);
|
|
32
|
+
const [npm, ...npmArgs] = existsSync(npmCli) ? [node, npmCli] : [findExecutable("npm", path)];
|
|
33
|
+
if (!npm)
|
|
34
|
+
throw new Error(`npm was not found for Node at ${node}`);
|
|
35
|
+
return [npm, ...npmArgs];
|
|
36
|
+
}
|
|
37
|
+
function bundledNpm(node) {
|
|
38
|
+
return join(dirname(node), "..", "lib", "node_modules", "npm", "bin", "npm-cli.js");
|
|
39
|
+
}
|
|
40
|
+
export function mergeNode(nodePath, cwd, env) {
|
|
41
|
+
const node = realpathSync(nodePath);
|
|
42
|
+
if (existsSync(bundledNpm(node)))
|
|
43
|
+
return node;
|
|
44
|
+
const mise = findExecutable("mise", env.PATH || "");
|
|
45
|
+
if (!mise)
|
|
46
|
+
return node;
|
|
47
|
+
// Cursor carries Node without npm. Resolve the installation before replacing HOME.
|
|
48
|
+
const result = spawnSync(mise, ["which", "node"], { cwd, env, encoding: "utf8" });
|
|
49
|
+
const installed = result.stdout?.trim();
|
|
50
|
+
if (result.status !== 0 || !installed || !isAbsolute(installed) || !existsSync(installed))
|
|
51
|
+
return node;
|
|
52
|
+
const resolved = realpathSync(installed);
|
|
53
|
+
return existsSync(bundledNpm(resolved)) ? resolved : node;
|
|
54
|
+
}
|
|
55
|
+
export class LandRefused extends Error {
|
|
56
|
+
}
|
|
57
|
+
/** The default run: execFile resolving {status, stdout, stderr}; rejects only when the file cannot start. */
|
|
58
|
+
export function execRun(file, argv, options = {}) {
|
|
59
|
+
return new Promise((resolve, reject) => {
|
|
60
|
+
execFile(file, argv, { encoding: "utf8", maxBuffer: 16 * 1024 * 1024, ...options }, (error, stdout, stderr) => {
|
|
61
|
+
if (error && typeof error.code === "string")
|
|
62
|
+
return reject(error);
|
|
63
|
+
resolve({ status: error ? error.code ?? null : 0, stdout, stderr });
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
const lastLine = (text = "") => text.trim().split("\n").pop() ?? "";
|
|
68
|
+
const logSubjects = (out = "") => out.split("\n").map((line) => line.trim()).filter(Boolean);
|
|
69
|
+
async function gh(run, cwd, argv) {
|
|
70
|
+
const result = await run("gh", argv, { cwd, encoding: "utf8" });
|
|
71
|
+
if (result.status !== 0)
|
|
72
|
+
throw new Error(`gh ${argv.slice(0, 2).join(" ")} failed: ${lastLine(result.stderr) || `exit ${result.status}`}`);
|
|
73
|
+
return result.stdout.trim();
|
|
74
|
+
}
|
|
75
|
+
// One check: a CheckRun has a status; a StatusContext has a state. Only SUCCESS counts as green.
|
|
76
|
+
function checkOutcome(check) {
|
|
77
|
+
if ("status" in check) {
|
|
78
|
+
if (check.status !== "COMPLETED")
|
|
79
|
+
return "pending";
|
|
80
|
+
return check.conclusion === "SUCCESS" ? "success" : "failure";
|
|
81
|
+
}
|
|
82
|
+
if (check.state === "SUCCESS")
|
|
83
|
+
return "success";
|
|
84
|
+
return check.state === "PENDING" || check.state === "EXPECTED" ? "pending" : "failure";
|
|
85
|
+
}
|
|
86
|
+
/** success only when every check succeeded; none when the PR reports no checks. */
|
|
87
|
+
export function checkState(rollup = []) {
|
|
88
|
+
const outcomes = rollup.map(checkOutcome);
|
|
89
|
+
if (!outcomes.length)
|
|
90
|
+
return "none";
|
|
91
|
+
if (outcomes.includes("failure"))
|
|
92
|
+
return "failure";
|
|
93
|
+
return outcomes.includes("pending") ? "pending" : "success";
|
|
94
|
+
}
|
|
95
|
+
const toPr = (pr) => ({
|
|
96
|
+
number: pr.number, title: pr.title, url: pr.url, branch: pr.headRefName, headSha: pr.headRefOid,
|
|
97
|
+
additions: pr.additions, deletions: pr.deletions, checks: checkState(pr.statusCheckRollup),
|
|
98
|
+
});
|
|
99
|
+
/** Open PRs into main, each with its check state. */
|
|
100
|
+
export async function listOpenPrs({ run = execRun, cwd = HOST_CHECKOUT } = {}) {
|
|
101
|
+
const prs = JSON.parse(await gh(run, cwd, ["pr", "list", "--state", "open", "--base", "main", "--limit", "50", "--json", PR_FIELDS]));
|
|
102
|
+
return prs.map(toPr);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Pushes branch (never main) and returns its open PR into main, creating one when none is open.
|
|
106
|
+
* The PR is ready for review. Without proof or author text its title and body come from the commits (--fill);
|
|
107
|
+
* proof puts the commit list and the proof in the body, and title/description let the author choose them instead.
|
|
108
|
+
*/
|
|
109
|
+
export async function requestLand(branch, { run = execRun, cwd = process.cwd(), proof, title: chosen, description } = {}) {
|
|
110
|
+
if (!BRANCH.test(branch) || branch === "main")
|
|
111
|
+
throw new LandRefused(`refusing to land ${branch}`);
|
|
112
|
+
const pushed = await run("git", ["push", "origin", `refs/heads/${branch}:refs/heads/${branch}`], { cwd, encoding: "utf8" });
|
|
113
|
+
if (pushed.status !== 0)
|
|
114
|
+
throw new Error(`git push ${branch} failed: ${lastLine(pushed.stderr) || `exit ${pushed.status}`}`);
|
|
115
|
+
const open = JSON.parse(await gh(run, cwd, ["pr", "list", "--head", branch, "--base", "main", "--state", "open", "--json", "url", "--limit", "1"]));
|
|
116
|
+
if (open.length)
|
|
117
|
+
return { url: open[0].url, created: false };
|
|
118
|
+
if (proof === undefined && chosen === undefined && description === undefined) {
|
|
119
|
+
const url = lastLine(await gh(run, cwd, ["pr", "create", "--base", "main", "--head", branch, "--fill"]));
|
|
120
|
+
return { url, created: true };
|
|
121
|
+
}
|
|
122
|
+
const title = (chosen ? chosen.replace(/\s+/g, " ").trim() : lastLine(await gitOut(run, cwd, ["log", "-1", "--format=%s", `refs/heads/${branch}`]))).slice(0, TITLE_MAX);
|
|
123
|
+
const commits = logSubjects(await gitOut(run, cwd, ["log", "--format=%s", `refs/remotes/origin/main..refs/heads/${branch}`]));
|
|
124
|
+
const url = lastLine(await gh(run, cwd, ["pr", "create", "--base", "main", "--head", branch, "--title", title, "--body", prBody({ subjects: commits, proof, description })]));
|
|
125
|
+
return { url, created: true };
|
|
126
|
+
}
|
|
127
|
+
async function gitOut(run, cwd, argv) {
|
|
128
|
+
const result = await run("git", argv, { cwd, encoding: "utf8" });
|
|
129
|
+
if (result.status !== 0)
|
|
130
|
+
throw new Error(`git ${argv[0]} failed: ${lastLine(result.stderr) || `exit ${result.status}`}`);
|
|
131
|
+
return result.stdout;
|
|
132
|
+
}
|
|
133
|
+
/** Whether this checkout can land through GitHub: origin is on github.com and gh is signed in there. */
|
|
134
|
+
export async function githubReady({ run = execRun, cwd = process.cwd() } = {}) {
|
|
135
|
+
const remote = await run("git", ["remote", "get-url", "origin"], { cwd, encoding: "utf8" });
|
|
136
|
+
if (remote.status !== 0 || !GITHUB_REMOTE.test(remote.stdout.trim()))
|
|
137
|
+
return false;
|
|
138
|
+
const auth = await run("gh", ["auth", "status", "--hostname", "github.com"], { cwd, encoding: "utf8" });
|
|
139
|
+
return auth.status === 0;
|
|
140
|
+
}
|
|
141
|
+
// The draft PR body: the branch's commit subjects, then the session proof fenced as text.
|
|
142
|
+
// An author-chosen description replaces the commit list; the proof section still follows when proof was given.
|
|
143
|
+
function prBody({ subjects, proof, description }) {
|
|
144
|
+
const proofSection = proof === undefined ? "" : `\n\n## Proof\n\n\`\`\`text\n${proof.trim() || "No proof supplied."}\n\`\`\`\n`;
|
|
145
|
+
if (description !== undefined)
|
|
146
|
+
return `${description.trim()}${proofSection}`.slice(0, PR_BODY_MAX);
|
|
147
|
+
const commits = subjects.map((subject) => `- ${subject}`).join("\n");
|
|
148
|
+
const body = `## Commits\n${commits}\n\n## Proof\n\n\`\`\`text\n${(proof ?? "").trim() || "No proof supplied."}\n\`\`\`\n`;
|
|
149
|
+
return body.slice(0, PR_BODY_MAX);
|
|
150
|
+
}
|
|
151
|
+
/** Finishes branch as a draft PR ("pr"), a review packet ("packet") or a bare push ("branch"); defaults to pr when githubReady, else packet. */
|
|
152
|
+
export async function finishBranch(branch, { landing, proof = "", title: chosen, description, run = execRun, cwd = process.cwd(), tasks, task, by = "cli", now = Date.now() } = {}) {
|
|
153
|
+
if (!BRANCH.test(branch) || branch === "main")
|
|
154
|
+
throw new LandRefused(`refusing to land ${branch}`);
|
|
155
|
+
if (landing !== undefined && !LANDING_POLICIES.includes(landing))
|
|
156
|
+
throw new LandRefused(`unknown landing policy ${landing}`);
|
|
157
|
+
const git = async (...argv) => {
|
|
158
|
+
const result = await run("git", argv, { cwd, encoding: "utf8" });
|
|
159
|
+
if (result.status !== 0)
|
|
160
|
+
throw new Error(`git ${argv.join(" ")} failed: ${lastLine(result.stderr) || `exit ${result.status}`}`);
|
|
161
|
+
return result.stdout;
|
|
162
|
+
};
|
|
163
|
+
const push = async () => {
|
|
164
|
+
const pushed = await run("git", ["push", "origin", `refs/heads/${branch}:refs/heads/${branch}`], { cwd, encoding: "utf8" });
|
|
165
|
+
if (pushed.status !== 0)
|
|
166
|
+
throw new Error(`git push ${branch} failed: ${lastLine(pushed.stderr) || `exit ${pushed.status}`}`);
|
|
167
|
+
};
|
|
168
|
+
if (landing === "branch") {
|
|
169
|
+
await push();
|
|
170
|
+
return { landing: "branch", branch };
|
|
171
|
+
}
|
|
172
|
+
const ready = await githubReady({ run, cwd });
|
|
173
|
+
if (landing === "pr" && !ready)
|
|
174
|
+
throw new LandRefused("landing=pr needs a GitHub origin and a signed-in gh");
|
|
175
|
+
if ((landing ?? (ready ? "pr" : "packet")) === "pr") {
|
|
176
|
+
await push();
|
|
177
|
+
const open = JSON.parse(await gh(run, cwd, ["pr", "list", "--head", branch, "--base", "main", "--state", "open", "--json", "url", "--limit", "1"]));
|
|
178
|
+
if (open.length)
|
|
179
|
+
return { landing: "pr", url: open[0].url, created: false };
|
|
180
|
+
const title = (chosen ? chosen.replace(/\s+/g, " ").trim() : lastLine(await git("log", "-1", "--format=%s", `refs/heads/${branch}`))).slice(0, TITLE_MAX);
|
|
181
|
+
const commits = logSubjects(await git("log", "--format=%s", `refs/remotes/origin/main..refs/heads/${branch}`));
|
|
182
|
+
const url = lastLine(await gh(run, cwd, ["pr", "create", "--draft", "--base", "main", "--head", branch, "--title", title, "--body", prBody({ subjects: commits, proof, description })]));
|
|
183
|
+
return { landing: "pr", url, created: true };
|
|
184
|
+
}
|
|
185
|
+
const commit = lastLine(await git("rev-parse", "--verify", `refs/heads/${branch}^{commit}`));
|
|
186
|
+
const remoteBase = await run("git", ["merge-base", "refs/remotes/origin/main", `refs/heads/${branch}`], { cwd, encoding: "utf8" });
|
|
187
|
+
const base = remoteBase.status === 0 ? lastLine(remoteBase.stdout) : lastLine(await git("merge-base", "refs/heads/main", `refs/heads/${branch}`));
|
|
188
|
+
const commits = logSubjects(await git("log", "--format=%s", `${base}..${commit}`));
|
|
189
|
+
const packet = reviewPacket({
|
|
190
|
+
submission: randomUUID(), commit, base, branch, criteria: commits,
|
|
191
|
+
check: { code: 2, text: `not run by the host; session proof: ${proof.trim().slice(0, PROOF_CLIP).replace(/[\r\n]+/g, " | ")}` },
|
|
192
|
+
numstat: await git("diff", "--numstat", `${base}..${commit}`),
|
|
193
|
+
at: now,
|
|
194
|
+
});
|
|
195
|
+
const id = task ?? (await tasks.add({ title: `Review ${branch}: ${chosen ? chosen.replace(/\s+/g, " ").trim() : commits[0] || ""}`.trim().slice(0, TITLE_MAX) }, now)).id;
|
|
196
|
+
await tasks.submitReview(id, packet, now);
|
|
197
|
+
return { landing: "packet", task: id };
|
|
198
|
+
}
|
|
199
|
+
/** Merges PR number at headSha when every check succeeded; --match-head-commit refuses a head pushed since the operator looked. */
|
|
200
|
+
export async function mergePr(number, headSha, { run = execRun, cwd = HOST_CHECKOUT } = {}) {
|
|
201
|
+
if (!Number.isInteger(number) || number < 1 || !SHA.test(headSha))
|
|
202
|
+
throw new LandRefused("Give the PR number and its head commit");
|
|
203
|
+
const pr = JSON.parse(await gh(run, cwd, ["pr", "view", String(number), "--json", "state,headRefOid,statusCheckRollup"]));
|
|
204
|
+
if (pr.state !== "OPEN")
|
|
205
|
+
throw new LandRefused(`#${number} is ${pr.state.toLowerCase()}`);
|
|
206
|
+
if (pr.headRefOid !== headSha)
|
|
207
|
+
throw new LandRefused(`#${number} moved after you looked; reload and check its checks`);
|
|
208
|
+
const checks = checkState(pr.statusCheckRollup);
|
|
209
|
+
if (checks !== "success")
|
|
210
|
+
throw new LandRefused(`#${number} checks are ${CHECK_WORDS[checks]}`);
|
|
211
|
+
await gh(run, cwd, ["pr", "merge", String(number), "--merge", "--match-head-commit", headSha]);
|
|
212
|
+
return { merged: true, number };
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Pulls the host checkout to origin/main, then installs dependencies and runs build (default: the UI deploy step).
|
|
216
|
+
* returning (back from another tree) deploys an unmoved HEAD; check runs before npm and a reason it returns skips.
|
|
217
|
+
* Returns {status: "deployed" | "skipped" | "failed" | "unchanged", line}; ifMoved returns "unchanged" when the last shipped commit (else HEAD) matches origin/main.
|
|
218
|
+
* deployed is the commit the last deploy shipped: with it, a HEAD that a manual pull already moved still deploys.
|
|
219
|
+
*/
|
|
220
|
+
export async function deployHost({ run = execRun, hostCheckout = HOST_CHECKOUT, env = process.env, ifMoved = false, nodePath = process.execPath, npm, deployed, build = UI_DEPLOY_ARGS, returning = false, check = async () => null, } = {}) {
|
|
221
|
+
const git = (...argv) => run("git", argv, { cwd: hostCheckout, encoding: "utf8" });
|
|
222
|
+
const revParse = async (ref) => (await git("rev-parse", ref)).stdout.trim();
|
|
223
|
+
const skip = (reason) => ({ status: "skipped", reason, line: `skipped: ${reason}` });
|
|
224
|
+
if (ifMoved) {
|
|
225
|
+
const fetched = await git("fetch", "--quiet", "origin", "+refs/heads/main:refs/remotes/origin/main");
|
|
226
|
+
if (fetched.status !== 0)
|
|
227
|
+
return { status: "failed", reason: "fetch origin main", line: "failed: fetch origin main" };
|
|
228
|
+
if ((deployed ?? await revParse("HEAD")) === await revParse("refs/remotes/origin/main"))
|
|
229
|
+
return { status: "unchanged" };
|
|
230
|
+
}
|
|
231
|
+
const branch = (await git("symbolic-ref", "--short", "HEAD")).stdout.trim();
|
|
232
|
+
if (branch !== "main")
|
|
233
|
+
return skip(`the host checkout is on ${branch || "no branch"}, not on main`);
|
|
234
|
+
if ((await git("status", "--porcelain", "--untracked-files=no")).stdout.trim())
|
|
235
|
+
return skip("the host checkout has local changes");
|
|
236
|
+
const before = deployed ?? await revParse("HEAD");
|
|
237
|
+
const pull = await git("pull", "--ff-only");
|
|
238
|
+
if (pull.status !== 0)
|
|
239
|
+
return skip(`git pull --ff-only: ${lastLine(pull.stderr || pull.stdout) || `exit ${pull.status}`}`);
|
|
240
|
+
const after = await revParse("HEAD");
|
|
241
|
+
// Returning from another tree with main already built: point back without a rebuild.
|
|
242
|
+
if (returning && after === deployed)
|
|
243
|
+
return { status: "deployed", built: false, from: before, to: after, line: `deployed main ${after.slice(0, SHORT)}` };
|
|
244
|
+
if (after === before && !returning)
|
|
245
|
+
return skip(`HEAD unchanged at ${before.slice(0, SHORT)}`);
|
|
246
|
+
const held = await check();
|
|
247
|
+
if (held)
|
|
248
|
+
return skip(held);
|
|
249
|
+
const [npmFile, ...npmArgs] = npm ?? npmCommand(mergeNode(nodePath, hostCheckout, env), env.PATH);
|
|
250
|
+
const npmRun = (argv) => run(npmFile, [...npmArgs, ...argv], { cwd: hostCheckout, env, encoding: "utf8" });
|
|
251
|
+
const ci = await npmRun(["ci", "--include=dev"]);
|
|
252
|
+
if (ci.status !== 0)
|
|
253
|
+
return { status: "failed", from: before, to: after, line: `failed: npm ci --include=dev exited ${ci.status}` };
|
|
254
|
+
const built = await npmRun(build);
|
|
255
|
+
if (built.status !== 0)
|
|
256
|
+
return { status: "failed", from: before, to: after, line: `failed: UI deploy exited ${built.status}` };
|
|
257
|
+
return { status: "deployed", from: before, to: after, line: `deployed ${before.slice(0, SHORT)}..${after.slice(0, SHORT)}` };
|
|
258
|
+
}
|
|
259
|
+
/** Restarts services (default: bus, UI and runner) as one transient unit, so restarting the runner does not stop this command. */
|
|
260
|
+
export function restartServices({ run = execRun, stamp = Date.now(), services = SERVICES } = {}) {
|
|
261
|
+
return run("systemd-run", ["--user", "--collect", `--unit=temmo-restart-${stamp}`, "systemctl", "--user", "restart", ...services], { encoding: "utf8" });
|
|
262
|
+
}
|
|
263
|
+
/** Starts `temmo deploy` detached as its own transient unit and returns once systemd has started it. */
|
|
264
|
+
export async function startDeploy({ run = execRun, nodePath = process.execPath, hostCheckout = HOST_CHECKOUT, stamp = Date.now() } = {}) {
|
|
265
|
+
const result = await run("systemd-run", ["--user", "--collect", `--unit=temmo-deploy-${stamp}`, nodePath, join(hostCheckout, "scripts/temmo.mjs"), "deploy"], { encoding: "utf8" });
|
|
266
|
+
if (result.status !== 0)
|
|
267
|
+
throw new Error(`systemd-run failed: ${lastLine(result.stderr) || `exit ${result.status}`}`);
|
|
268
|
+
return result;
|
|
269
|
+
}
|
|
270
|
+
/** The runner schedule that runs `deploy --if-moved` every 5 minutes. */
|
|
271
|
+
export function autoDeployEntry({ nodePath = process.execPath, hostCheckout = HOST_CHECKOUT } = {}) {
|
|
272
|
+
return {
|
|
273
|
+
name: "auto-deploy", everyMs: AUTO_DEPLOY_EVERY_MS, cwd: hostCheckout, timeoutMs: AUTO_DEPLOY_TIMEOUT_MS,
|
|
274
|
+
command: [nodePath, join(hostCheckout, "scripts/temmo.mjs"), "deploy", "--if-moved"],
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
const lastDeployPath = (env) => join(stateDir(env), "deploy-last.json");
|
|
278
|
+
/** The last deploy result the CLI recorded, or null before any deploy or when the file holds invalid JSON. */
|
|
279
|
+
export function readLastDeploy(env = process.env) {
|
|
280
|
+
let text;
|
|
281
|
+
try {
|
|
282
|
+
text = readFileSync(lastDeployPath(env), "utf8");
|
|
283
|
+
}
|
|
284
|
+
catch (error) {
|
|
285
|
+
if (error.code === "ENOENT")
|
|
286
|
+
return null;
|
|
287
|
+
throw error;
|
|
288
|
+
}
|
|
289
|
+
try {
|
|
290
|
+
return JSON.parse(text);
|
|
291
|
+
}
|
|
292
|
+
catch (error) {
|
|
293
|
+
// A crash mid-write used to break deploy --if-moved until the file was deleted; now it reads as no record.
|
|
294
|
+
process.stderr.write(`temmo: deploy: ignoring ${lastDeployPath(env)}: ${error.message}\n`);
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
/** Records the last deploy result atomically, so a crash mid-write cannot leave a half-written file. */
|
|
299
|
+
export function writeLastDeploy(env, record) {
|
|
300
|
+
writeJson(lastDeployPath(env), record);
|
|
301
|
+
}
|
|
302
|
+
/** {deployed: ref} while a tree other than main serves the services, else {}; an unreadable record counts as main. */
|
|
303
|
+
export function deployedField(env = process.env) {
|
|
304
|
+
let ref;
|
|
305
|
+
try {
|
|
306
|
+
ref = readLastDeploy(env)?.ref;
|
|
307
|
+
}
|
|
308
|
+
catch {
|
|
309
|
+
return {};
|
|
310
|
+
}
|
|
311
|
+
return typeof ref === "string" && ref && ref !== "main" ? { deployed: ref } : {};
|
|
312
|
+
}
|
|
313
|
+
/** The UI's adapter: the host's gh and systemd calls. */
|
|
314
|
+
export const hostLand = {
|
|
315
|
+
list: () => listOpenPrs(),
|
|
316
|
+
merge: (number, headSha) => mergePr(number, headSha),
|
|
317
|
+
open: (branch, cwd, proof) => requestLand(branch, { cwd, proof }),
|
|
318
|
+
startDeploy: () => startDeploy(),
|
|
319
|
+
lastDeploy: (env) => readLastDeploy(env),
|
|
320
|
+
};
|
package/lib/lessons.js
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Harness lesson library (Harness² harnessing-level recursion, docs/harness-improvement.md):
|
|
3
|
+
* <stateDir>/<project>/harness-lessons/<harness>/ holds lessons.json (machine-readable entries) and
|
|
4
|
+
* EXPERIENCE.md (the same entries as prose, regenerated on every write). Per-harness because each harness's
|
|
5
|
+
* editable surface differs. Harness² evidence rule: a lesson that cites no observation is never stored.
|
|
6
|
+
* Each entry carries Harness²'s experience fields: the cue (the shape of situation it applies to), the lesson,
|
|
7
|
+
* its evidence, and an optional confidence. A start reads the library back as a cue-keyed note (lessonsNote).
|
|
8
|
+
*/
|
|
9
|
+
import { readdirSync, readFileSync } from "node:fs";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { autoDefaults, isValidName } from "./config.js";
|
|
12
|
+
import { writeState } from "./state.js";
|
|
13
|
+
import { writeFileAtomic } from "./store.js";
|
|
14
|
+
/** Entries kept per harness; oldest drop first when the cap is passed. */
|
|
15
|
+
export const LESSONS_CAP = 40;
|
|
16
|
+
/** Length caps so one verbose reflection cannot crowd out the library. */
|
|
17
|
+
export const LESSON_MAX = 400;
|
|
18
|
+
export const EVIDENCE_MAX = 500;
|
|
19
|
+
export const CUE_MAX = 200;
|
|
20
|
+
export const CONFIDENCE_MAX = 160;
|
|
21
|
+
/** Lessons one reflection session may post at most (Harness²: "at most a few"). */
|
|
22
|
+
export const LESSONS_PER_SESSION = 3;
|
|
23
|
+
/** The start note: newest lessons first, at most this many and this long. */
|
|
24
|
+
export const NOTE_LESSONS_MAX = 8;
|
|
25
|
+
export const NOTE_MAX_CHARS = 1_600;
|
|
26
|
+
/** The note's wrapper; the reflector strips it, so a session never relearns lessons it was handed. */
|
|
27
|
+
export const NOTE_TAG = "temmo-lessons";
|
|
28
|
+
/** Evidence lines kept from the sessions that reinforced a lesson, newest last. */
|
|
29
|
+
export const ALSO_MAX = 2;
|
|
30
|
+
const cleanText = (value, max) => typeof value === "string" ? value.replace(/\s+/g, " ").trim().slice(0, max) : "";
|
|
31
|
+
/**
|
|
32
|
+
* The stored shape of one lesson, or null when it breaks the evidence rule or the caps. A fresh reflection
|
|
33
|
+
* requires a cue (requireCue); entries stored before cues existed still read.
|
|
34
|
+
*/
|
|
35
|
+
export function cleanLesson(entry, { requireCue = false } = {}) {
|
|
36
|
+
if (!entry || typeof entry !== "object")
|
|
37
|
+
return null;
|
|
38
|
+
const lesson = cleanText(entry.lesson, LESSON_MAX);
|
|
39
|
+
const evidence = cleanText(entry.evidence, EVIDENCE_MAX);
|
|
40
|
+
const cue = cleanText(entry.cue, CUE_MAX);
|
|
41
|
+
const confidence = cleanText(entry.confidence, CONFIDENCE_MAX);
|
|
42
|
+
if (!lesson || !evidence || (requireCue && !cue))
|
|
43
|
+
return null;
|
|
44
|
+
const at = Number.isSafeInteger(entry.at) && entry.at > 0 ? entry.at : Date.now();
|
|
45
|
+
const seen = Number.isSafeInteger(entry.seen) && entry.seen > 1 ? entry.seen : 0;
|
|
46
|
+
const lastAt = Number.isSafeInteger(entry.lastAt) && entry.lastAt >= at ? entry.lastAt : 0;
|
|
47
|
+
const also = Array.isArray(entry.also) ? entry.also.map((e) => cleanText(e, EVIDENCE_MAX)).filter(Boolean).slice(-ALSO_MAX) : [];
|
|
48
|
+
const promoted = cleanText(entry.promoted, CUE_MAX);
|
|
49
|
+
return {
|
|
50
|
+
...(cue ? { cue } : {}), lesson, evidence, ...(confidence ? { confidence } : {}), at,
|
|
51
|
+
...(seen ? { seen } : {}), ...(lastAt ? { lastAt } : {}), ...(also.length ? { also } : {}), ...(promoted ? { promoted } : {}),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** Dedupe key: case and whitespace folded, so a restated repeat of the same wording is skipped. */
|
|
55
|
+
export const lessonKey = (lesson) => lesson.toLowerCase().replace(/[^a-z0-9]+/g, " ").trim();
|
|
56
|
+
const dedupeKey = lessonKey;
|
|
57
|
+
const lastSeen = (l) => l.lastAt ?? l.at;
|
|
58
|
+
/** Most-reinforced first, then most recently seen: the order a start note and the reflector's library list use. */
|
|
59
|
+
export function rankLessons(lessons) {
|
|
60
|
+
return [...lessons].sort((a, b) => (b.seen ?? 1) - (a.seen ?? 1) || lastSeen(b) - lastSeen(a));
|
|
61
|
+
}
|
|
62
|
+
function reinforce(lesson, evidence, at) {
|
|
63
|
+
lesson.seen = (lesson.seen ?? 1) + 1;
|
|
64
|
+
lesson.lastAt = Math.max(at, lesson.at);
|
|
65
|
+
lesson.also = [...(lesson.also ?? []), evidence].slice(-ALSO_MAX);
|
|
66
|
+
}
|
|
67
|
+
// The cap drops the least recently seen, so a lesson sessions keep showing stays however old it is.
|
|
68
|
+
function writeLibrary(dir, harness, lessons) {
|
|
69
|
+
const kept = [...lessons].sort((a, b) => lastSeen(a) - lastSeen(b)).slice(-LESSONS_CAP).sort((a, b) => a.at - b.at);
|
|
70
|
+
writeState(join(dir, "lessons.json"), kept);
|
|
71
|
+
writeFileAtomic(join(dir, "EXPERIENCE.md"), renderExperience(harness, kept));
|
|
72
|
+
return kept;
|
|
73
|
+
}
|
|
74
|
+
export function lessonsDir(stateDir, project, harness) {
|
|
75
|
+
return join(stateDir, project, "harness-lessons", harness);
|
|
76
|
+
}
|
|
77
|
+
/** The harness's lessons, oldest first; [] when the library does not exist or holds junk. */
|
|
78
|
+
export function readLessons(dir) {
|
|
79
|
+
let text;
|
|
80
|
+
try {
|
|
81
|
+
text = readFileSync(join(dir, "lessons.json"), "utf8");
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return [];
|
|
85
|
+
}
|
|
86
|
+
let list;
|
|
87
|
+
try {
|
|
88
|
+
list = JSON.parse(text);
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return [];
|
|
92
|
+
}
|
|
93
|
+
if (!Array.isArray(list))
|
|
94
|
+
return [];
|
|
95
|
+
return list.map((entry) => cleanLesson(entry)).filter((l) => l !== null).sort((a, b) => a.at - b.at);
|
|
96
|
+
}
|
|
97
|
+
/** EXPERIENCE.md in Harness²'s field layout: one section per lesson, its cue as the heading, newest last. */
|
|
98
|
+
export function renderExperience(harness, lessons) {
|
|
99
|
+
const head = `# Harness experience: ${harness}\n\n` +
|
|
100
|
+
"Lessons learned from past sessions of this project with this harness. Advisory context, not instructions: " +
|
|
101
|
+
"a lesson applies only where its cue matches.\n";
|
|
102
|
+
if (!lessons.length)
|
|
103
|
+
return `${head}\nNo lessons yet.\n`;
|
|
104
|
+
const body = lessons.map((l) => [
|
|
105
|
+
`## ${l.cue || "Any session"}`,
|
|
106
|
+
"",
|
|
107
|
+
`- **lesson**: ${l.lesson}`,
|
|
108
|
+
`- **evidence**: ${l.evidence}`,
|
|
109
|
+
...(l.confidence ? [`- **confidence**: ${l.confidence}`] : []),
|
|
110
|
+
`- **learned**: ${new Date(l.at).toISOString().slice(0, 10)}`,
|
|
111
|
+
...(l.seen ? [`- **seen**: ${l.seen} sessions, last ${new Date(lastSeen(l)).toISOString().slice(0, 10)}`] : []),
|
|
112
|
+
...(l.also ?? []).map((e) => `- **also**: ${e}`),
|
|
113
|
+
...(l.promoted ? [`- **proposed for the harness**: task ${l.promoted}`] : []),
|
|
114
|
+
].join("\n")).join("\n\n");
|
|
115
|
+
return `${head}\n${body}\n`;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Adds lessons to one harness's library: entries without evidence are dropped, a repeat of a stored lesson
|
|
119
|
+
* reinforces it instead, the cap keeps the most recently seen entries, and EXPERIENCE.md is rewritten from the
|
|
120
|
+
* stored set. Resolves {added, total}; the count of new entries and of lessons stored after the write.
|
|
121
|
+
*/
|
|
122
|
+
export function storeLessons(dir, harness, entries, { now = Date.now } = {}) {
|
|
123
|
+
const stamped = entries.map((entry) => entry && typeof entry === "object" && !entry.at ? { ...entry, at: now() } : entry);
|
|
124
|
+
const incoming = stamped.map((entry) => cleanLesson(entry)).filter((l) => l !== null);
|
|
125
|
+
const stored = readLessons(dir);
|
|
126
|
+
const byKey = new Map(stored.map((l) => [dedupeKey(l.lesson), l]));
|
|
127
|
+
const fresh = [];
|
|
128
|
+
for (const entry of incoming) {
|
|
129
|
+
const key = dedupeKey(entry.lesson);
|
|
130
|
+
const known = byKey.get(key);
|
|
131
|
+
if (known) {
|
|
132
|
+
if (stored.includes(known))
|
|
133
|
+
reinforce(known, entry.evidence, now());
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
byKey.set(key, entry);
|
|
137
|
+
fresh.push(entry);
|
|
138
|
+
}
|
|
139
|
+
const kept = writeLibrary(dir, harness, [...stored, ...fresh]);
|
|
140
|
+
return { added: fresh.length, total: kept.length };
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Records that more sessions showed stored lessons' contrasts (Harness²'s compose rule across sessions: a lesson
|
|
144
|
+
* gains standing only from new observations). Each hit names a lesson by lessonKey and carries this session's
|
|
145
|
+
* evidence; hits on lessons no longer stored are dropped. Resolves {reinforced}.
|
|
146
|
+
*/
|
|
147
|
+
export function reinforceLessons(dir, harness, hits, { now = Date.now } = {}) {
|
|
148
|
+
const stored = readLessons(dir);
|
|
149
|
+
let reinforced = 0;
|
|
150
|
+
for (const hit of hits) {
|
|
151
|
+
const lesson = stored.find((l) => dedupeKey(l.lesson) === hit.key);
|
|
152
|
+
const evidence = cleanText(hit.evidence, EVIDENCE_MAX);
|
|
153
|
+
if (!lesson || !evidence)
|
|
154
|
+
continue;
|
|
155
|
+
reinforce(lesson, evidence, now());
|
|
156
|
+
reinforced += 1;
|
|
157
|
+
}
|
|
158
|
+
if (reinforced)
|
|
159
|
+
writeLibrary(dir, harness, stored);
|
|
160
|
+
return { reinforced };
|
|
161
|
+
}
|
|
162
|
+
/** Records the task that proposes a stored lesson for a harness file; false when the lesson is gone. */
|
|
163
|
+
export function markPromoted(dir, harness, key, taskId) {
|
|
164
|
+
const stored = readLessons(dir);
|
|
165
|
+
const lesson = stored.find((l) => dedupeKey(l.lesson) === key);
|
|
166
|
+
if (!lesson)
|
|
167
|
+
return false;
|
|
168
|
+
lesson.promoted = taskId;
|
|
169
|
+
writeLibrary(dir, harness, stored);
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
/** Every harness name with a library under a project's harness-lessons folder. */
|
|
173
|
+
export function listLessonHarnesses(dir) {
|
|
174
|
+
let names;
|
|
175
|
+
try {
|
|
176
|
+
names = readdirSync(dir);
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
return [];
|
|
180
|
+
}
|
|
181
|
+
return names.filter((name) => isValidName(name));
|
|
182
|
+
}
|
|
183
|
+
/** Reads one project's whole library as {harness: lessons}; empty object when nothing is stored. */
|
|
184
|
+
export function readProjectLessons(stateDir, project) {
|
|
185
|
+
const root = join(stateDir, project, "harness-lessons");
|
|
186
|
+
const out = {};
|
|
187
|
+
for (const harness of listLessonHarnesses(root)) {
|
|
188
|
+
const lessons = readLessons(join(root, harness));
|
|
189
|
+
if (lessons.length)
|
|
190
|
+
out[harness] = lessons;
|
|
191
|
+
}
|
|
192
|
+
return out;
|
|
193
|
+
}
|
|
194
|
+
/** Starts carry the lesson note unless defaults.json sets lessonsAtStart false or TEMMO_LESSONS names off. */
|
|
195
|
+
export function lessonsAtStart(env = process.env) {
|
|
196
|
+
const raw = env.TEMMO_LESSONS;
|
|
197
|
+
if (raw !== undefined && raw !== "")
|
|
198
|
+
return !/^(0|false|off|no)$/i.test(raw.trim());
|
|
199
|
+
return autoDefaults(env)?.lessonsAtStart !== false;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* The note a start injects as system context (Harness² phase 3): the harness's most-reinforced, then newest,
|
|
203
|
+
* lessons as cue-keyed lines, capped, wrapped in NOTE_TAG. Harness² never auto-inherits library entries, so the note asks the agent to
|
|
204
|
+
* apply a lesson only where its cue matches. "" when the library is empty or the note is switched off.
|
|
205
|
+
*/
|
|
206
|
+
export function lessonsNote(stateDir, project, harness, env = process.env) {
|
|
207
|
+
if (!stateDir || !project || !isValidName(harness) || !lessonsAtStart(env))
|
|
208
|
+
return "";
|
|
209
|
+
// An entry with no cue predates Harness²'s experience fields and cannot say when it applies, so it stays in the library but out of the prompt.
|
|
210
|
+
const lessons = rankLessons(readLessons(lessonsDir(stateDir, project, harness)).filter((l) => l.cue));
|
|
211
|
+
const open = `<${NOTE_TAG}>\nLessons from past ${harness} sessions on this project. Advisory: use one only where its situation matches yours.`;
|
|
212
|
+
const close = `</${NOTE_TAG}>`;
|
|
213
|
+
const lines = [];
|
|
214
|
+
let used = open.length + close.length + 2;
|
|
215
|
+
for (const l of lessons.slice(0, NOTE_LESSONS_MAX)) {
|
|
216
|
+
const line = `- ${l.cue ? `When ${l.cue.replace(/^when\s+/i, "")}: ` : ""}${l.lesson}`;
|
|
217
|
+
if (used + line.length + 1 > NOTE_MAX_CHARS)
|
|
218
|
+
break;
|
|
219
|
+
lines.push(line);
|
|
220
|
+
used += line.length + 1;
|
|
221
|
+
}
|
|
222
|
+
return lines.length ? `\n\n${open}\n${lines.join("\n")}\n${close}` : "";
|
|
223
|
+
}
|