@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
package/lib/harness.js
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Harness names, their binaries, the `harness:model` text form, the fallback model and every per-harness command line.
|
|
3
|
+
* A leaf module: start, controls, builder, team authoring, delivery, interrupt and the Cursor runner take them from here.
|
|
4
|
+
*/
|
|
5
|
+
import { accessSync, constants, existsSync, readdirSync, realpathSync } from "node:fs";
|
|
6
|
+
import { homedir } from "node:os";
|
|
7
|
+
import { delimiter, dirname, isAbsolute, join } from "node:path";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
import { modeFlags } from "../web/lib/start-mode.js";
|
|
10
|
+
export const HARNESSES = ["claude", "codex", "cursor", "opencode", "pi"];
|
|
11
|
+
// The Cursor model every built-in default and fallback uses.
|
|
12
|
+
export const FALLBACK_MODEL = "deepseek-v41-flash-1017-high";
|
|
13
|
+
// Shape of a model name a start may pass; membership in lib/models.ts's allowlist is checked by the caller.
|
|
14
|
+
export const MODEL_NAME = /^[a-zA-Z0-9][a-zA-Z0-9._:[\]-]{0,79}$/;
|
|
15
|
+
// OpenCode names a model provider/model#variant (`opencode run --help`); the model part may hold more slashes.
|
|
16
|
+
export const OPENCODE_MODEL_NAME = /^[a-zA-Z0-9][a-zA-Z0-9._-]{0,39}\/[a-zA-Z0-9][a-zA-Z0-9._:@/[\]-]{0,119}(#[a-zA-Z0-9._-]{1,40})?$/;
|
|
17
|
+
// pi names a model provider/id with an optional :thinking level (`pi --help`, --model); the id may hold slashes and colons.
|
|
18
|
+
export const PI_MODEL_NAME = /^[a-zA-Z0-9][a-zA-Z0-9._-]{0,39}\/[a-zA-Z0-9][a-zA-Z0-9._:@/[\]-]{0,119}$/;
|
|
19
|
+
// Cursor renamed its CLI to `agent`; `cursor-agent` is kept as the fallback for installs that predate it.
|
|
20
|
+
// The Grok Build unified binary symlinks both names to its own ACP-less binary, so a name on PATH only
|
|
21
|
+
// counts as the Cursor CLI when the file behind it is (see cursorBinary).
|
|
22
|
+
export const CURSOR_BINARIES = ["agent", "cursor-agent"];
|
|
23
|
+
// The executable each harness starts as on a host; the Cursor name on a given PATH is cursorBinary's.
|
|
24
|
+
export const LAUNCH_BINARY = { claude: "claude", codex: "codex", cursor: "cursor-agent", opencode: "opencode", pi: "pi" };
|
|
25
|
+
// Lets `codex exec` run plugin hooks without persisted trust, which only the interactive `/hooks` review writes.
|
|
26
|
+
export const CODEX_HOOK_TRUST = "--dangerously-bypass-hook-trust";
|
|
27
|
+
// Claude Code's --permission-mode values (`claude --help`; the Agent SDK's PermissionMode). "manual" is an alias of "default".
|
|
28
|
+
export const CLAUDE_PERMISSION_MODES = ["acceptEdits", "auto", "bypassPermissions", "default", "dontAsk", "plan"];
|
|
29
|
+
// The defaults.json claudePermissionMode that is "default" while AFK is on and passes nothing otherwise.
|
|
30
|
+
export const CLAUDE_PERMISSION_AUTO = "auto-afk";
|
|
31
|
+
// The defaults.json claudePermissionMode that passes nothing, so the user's own Claude settings apply.
|
|
32
|
+
export const CLAUDE_PERMISSION_INHERIT = "inherit";
|
|
33
|
+
// Every value a Claude permission setting may name: defaults.json, a team.dot attribute, or a start option.
|
|
34
|
+
export const CLAUDE_PERMISSION_VALUES = [CLAUDE_PERMISSION_AUTO, CLAUDE_PERMISSION_INHERIT, ...CLAUDE_PERMISSION_MODES];
|
|
35
|
+
export const REQUIRE_RUN_CODE_ENV = "TEMMO_REQUIRE_RUN_CODE";
|
|
36
|
+
export const CODEX_CODE_MODE_KEY = "features.code_mode";
|
|
37
|
+
// The harness each agent binary runs as, by the binary's name. A Map, so a name like "constructor" finds nothing.
|
|
38
|
+
const BINARY_HARNESS = new Map([
|
|
39
|
+
["agent", "cursor"],
|
|
40
|
+
["cursor-agent", "cursor"],
|
|
41
|
+
["claude", "claude"],
|
|
42
|
+
["codex", "codex"],
|
|
43
|
+
["opencode", "opencode"],
|
|
44
|
+
["pi", "pi"],
|
|
45
|
+
]);
|
|
46
|
+
/** Whether a name is one of the five harnesses. */
|
|
47
|
+
export function isHarness(name) {
|
|
48
|
+
return typeof name === "string" && HARNESSES.includes(name);
|
|
49
|
+
}
|
|
50
|
+
/** The harness an agent binary runs as, by its name; undefined for any other program. */
|
|
51
|
+
export function harnessOfBinary(name) {
|
|
52
|
+
return BINARY_HARNESS.get(name);
|
|
53
|
+
}
|
|
54
|
+
/** Whether a model name has the shape this harness's start may pass. */
|
|
55
|
+
export function modelShape(harness, model) {
|
|
56
|
+
if (typeof model !== "string")
|
|
57
|
+
return false;
|
|
58
|
+
if (harness === "pi")
|
|
59
|
+
return PI_MODEL_NAME.test(model);
|
|
60
|
+
return (harness === "opencode" ? OPENCODE_MODEL_NAME : MODEL_NAME).test(model);
|
|
61
|
+
}
|
|
62
|
+
/** Parses "harness" or "harness:model"; the model must pass modelShape. Null for anything else, a bare harness gives model "". */
|
|
63
|
+
export function parseHarnessModel(text) {
|
|
64
|
+
if (typeof text !== "string")
|
|
65
|
+
return null;
|
|
66
|
+
const at = text.indexOf(":");
|
|
67
|
+
const harness = at < 0 ? text : text.slice(0, at);
|
|
68
|
+
const model = at < 0 ? "" : text.slice(at + 1);
|
|
69
|
+
if (!isHarness(harness) || (at >= 0 && !modelShape(harness, model)))
|
|
70
|
+
return null;
|
|
71
|
+
return { harness, model };
|
|
72
|
+
}
|
|
73
|
+
/** "harness:model", or "harness" when the model is empty. */
|
|
74
|
+
export function formatHarnessModel({ harness, model }) {
|
|
75
|
+
return model ? `${harness}:${model}` : harness;
|
|
76
|
+
}
|
|
77
|
+
/** `-c` arguments for a Codex CLI child. */
|
|
78
|
+
export function codexCodeModeArgs(on) {
|
|
79
|
+
return ["-c", `${CODEX_CODE_MODE_KEY}=${on ? "true" : "false"}`];
|
|
80
|
+
}
|
|
81
|
+
/** Register the local MCP scripts for tagged Codex starts, with the session folder as their cwd. */
|
|
82
|
+
export function codexRequiredRunCodeArgs() {
|
|
83
|
+
const script = (name) => JSON.stringify(fileURLToPath(new URL(`../scripts/${name}-mcp.mjs`, import.meta.url)));
|
|
84
|
+
return [
|
|
85
|
+
"-c", `mcp_servers.temmo={command="node",args=[${script("temmo")},"codex"],env_vars=["${REQUIRE_RUN_CODE_ENV}"]}`,
|
|
86
|
+
"-c", `mcp_servers.temmo-code={command="node",args=[${script("temmo-code")},"codex"],tool_timeout_sec=150}`,
|
|
87
|
+
];
|
|
88
|
+
}
|
|
89
|
+
/** The child's PATH: the caller's first, then node's directory and ~/.local/bin. */
|
|
90
|
+
export function childPath(env = process.env, execPath = process.execPath) {
|
|
91
|
+
return [env.PATH, dirname(execPath), join(env.HOME || homedir(), ".local", "bin")].filter(Boolean).join(delimiter);
|
|
92
|
+
}
|
|
93
|
+
/** Whether a PATH candidate is the Cursor CLI: a plain file (a wrapper, a test fake) always is; a symlink
|
|
94
|
+
* must land in a Cursor version tree, the directory holding the CLI and its bundled node. The Grok Build
|
|
95
|
+
* unified binary took the launcher names without either. */
|
|
96
|
+
function isCursorCli(candidate, real) {
|
|
97
|
+
return real === candidate || existsSync(join(dirname(real), "node"));
|
|
98
|
+
}
|
|
99
|
+
const isExecutable = (file) => {
|
|
100
|
+
try {
|
|
101
|
+
accessSync(file, constants.X_OK);
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
/** The newest Cursor version tree under `home` (`~/.local/share/cursor-agent/versions/<v>/`, CLI plus bundled node), or "". */
|
|
109
|
+
function cursorVersionTree(home) {
|
|
110
|
+
const root = join(home, ".local", "share", "cursor-agent", "versions");
|
|
111
|
+
let versions;
|
|
112
|
+
try {
|
|
113
|
+
versions = readdirSync(root).sort().reverse();
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
return "";
|
|
117
|
+
}
|
|
118
|
+
return versions.map((v) => join(root, v)).find((dir) => existsSync(join(dir, "node")) && CURSOR_BINARIES.some((name) => isExecutable(join(dir, name)))) || "";
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* The Cursor CLI to run: the first PATH directory holding a real `agent` or `cursor-agent` decides, by name.
|
|
122
|
+
* When those names belong to another build (the Grok Build unified binary symlinks all three of `agent`,
|
|
123
|
+
* `cursor-agent` and `grok` to its own ACP-less binary), fall back to the newest version tree under `home`
|
|
124
|
+
* and return its absolute path, so a service started with a bare PATH still finds the CLI. With neither,
|
|
125
|
+
* the first launcher name is returned and the start fails the way a missing CLI fails.
|
|
126
|
+
*/
|
|
127
|
+
export function cursorBinary(path = "", home = homedir()) {
|
|
128
|
+
let lookalike = "";
|
|
129
|
+
for (const dir of path.split(delimiter).filter((d) => isAbsolute(d))) {
|
|
130
|
+
for (const name of CURSOR_BINARIES) {
|
|
131
|
+
const candidate = join(dir, name);
|
|
132
|
+
if (!isExecutable(candidate))
|
|
133
|
+
continue;
|
|
134
|
+
let real = candidate;
|
|
135
|
+
try {
|
|
136
|
+
real = realpathSync(candidate);
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (isCursorCli(candidate, real))
|
|
142
|
+
return name;
|
|
143
|
+
if (!lookalike)
|
|
144
|
+
lookalike = name;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const tree = cursorVersionTree(home);
|
|
148
|
+
const treeBin = tree && CURSOR_BINARIES.find((name) => isExecutable(join(tree, name)));
|
|
149
|
+
if (treeBin)
|
|
150
|
+
return join(tree, treeBin);
|
|
151
|
+
return lookalike || CURSOR_BINARIES[0];
|
|
152
|
+
}
|
|
153
|
+
/** The Cursor binary a start or wake runs: the name cursorBinary finds on `path`. */
|
|
154
|
+
export function cursorCommandFor({ path = "" } = {}) {
|
|
155
|
+
return cursorBinary(path);
|
|
156
|
+
}
|
|
157
|
+
/** "" when the value is a Claude permission setting, else the message naming the valid ones. */
|
|
158
|
+
export function claudePermissionError(value) {
|
|
159
|
+
return CLAUDE_PERMISSION_VALUES.includes(value) ? "" : `claudePermissionMode must be one of ${CLAUDE_PERMISSION_VALUES.join(", ")}; got ${JSON.stringify(value)}`;
|
|
160
|
+
}
|
|
161
|
+
/** The --permission-mode a Claude start passes: the setting itself, "default" for auto-afk while AFK is on, else undefined. */
|
|
162
|
+
export function claudePermissionFlag(setting, afk) {
|
|
163
|
+
if (setting === CLAUDE_PERMISSION_INHERIT)
|
|
164
|
+
return undefined;
|
|
165
|
+
if (setting !== CLAUDE_PERMISSION_AUTO)
|
|
166
|
+
return setting;
|
|
167
|
+
return afk ? "default" : undefined;
|
|
168
|
+
}
|
|
169
|
+
// A start's argv: the mode flags, Codex code mode, the model after the mode flags, and a managed start's no-subagent args
|
|
170
|
+
// last before the prompt. Null for a model or mode it cannot pass.
|
|
171
|
+
function startArgv(harness, o) {
|
|
172
|
+
if (o.model !== undefined && !modelShape(harness, o.model))
|
|
173
|
+
return null;
|
|
174
|
+
const chosen = modeFlags(harness, o.mode ?? "agent");
|
|
175
|
+
if (!chosen)
|
|
176
|
+
return null;
|
|
177
|
+
const flag = (name) => (o.model === undefined ? [] : [name, o.model]);
|
|
178
|
+
const off = o.noSubagents ?? [];
|
|
179
|
+
switch (harness) {
|
|
180
|
+
// A plan mode's own --permission-mode wins over the configured permission mode.
|
|
181
|
+
case "claude": return [LAUNCH_BINARY.claude, ["--bg", ...(o.worktree ? ["--worktree", o.worktree] : []), ...(chosen.length ? chosen : o.permission ? ["--permission-mode", o.permission] : []), ...flag("--model"), ...off, o.prompt]];
|
|
182
|
+
// Codex code mode is a feature flag; Claude --bg refuses code mode (background job), so it gets nothing.
|
|
183
|
+
case "codex": return [LAUNCH_BINARY.codex, ["exec", "--json", CODEX_HOOK_TRUST, ...(typeof o.codeMode === "boolean" ? codexCodeModeArgs(o.codeMode) : []), ...(o.requireRunCode ? codexRequiredRunCodeArgs() : []), ...chosen, ...flag("-m"), ...off, o.prompt]];
|
|
184
|
+
case "cursor": return [LAUNCH_BINARY.cursor, ["-p", "--trust", "--output-format", "stream-json", "--workspace", o.root, ...chosen, ...flag("--model"), o.prompt]];
|
|
185
|
+
// `opencode run` works in its cwd (the project root or a worktree); --format json prints one event per line.
|
|
186
|
+
case "opencode": return [LAUNCH_BINARY.opencode, ["run", "--format", "json", ...chosen, ...flag("-m"), o.prompt]];
|
|
187
|
+
default: return null;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
// A wake resumes a saved session with the wake prompt; Cursor's binary is the one cursorBinary finds on the wake's PATH.
|
|
191
|
+
function wakeArgv(harness, o) {
|
|
192
|
+
const off = o.noSubagents ?? [];
|
|
193
|
+
switch (harness) {
|
|
194
|
+
// A stored /permission value is the session's next-wake mode; without one the wake passes no flag.
|
|
195
|
+
case "claude": return [LAUNCH_BINARY.claude, ["--print", "--resume", o.id, ...(o.permission ? ["--permission-mode", o.permission] : []), ...off, o.prompt]];
|
|
196
|
+
case "codex": return [LAUNCH_BINARY.codex, ["exec", ...(o.requireRunCode ? codexRequiredRunCodeArgs() : []), ...off, "resume", o.id, o.prompt]];
|
|
197
|
+
case "cursor": return [cursorBinary(o.path ?? ""), ["--print", "--resume", o.id, o.prompt]];
|
|
198
|
+
case "opencode": return [LAUNCH_BINARY.opencode, ["run", "--session", o.id, o.prompt]];
|
|
199
|
+
default: return null;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
// OpenCode's server compacts a session through its API, running or not (operation session.compact).
|
|
203
|
+
function compactArgv(harness, o) {
|
|
204
|
+
if (harness !== "opencode")
|
|
205
|
+
return null;
|
|
206
|
+
return [LAUNCH_BINARY.opencode, ["api", "session.compact", "--param", `sessionID=${o.id}`, "--data", "{}"]];
|
|
207
|
+
}
|
|
208
|
+
// The one queued message `codex queue` takes for a thread; other harnesses deliver through their own API.
|
|
209
|
+
function queueArgv(harness, o) {
|
|
210
|
+
if (harness !== "codex")
|
|
211
|
+
return null;
|
|
212
|
+
return [LAUNCH_BINARY.codex, ["queue", "--thread", o.id, "--message", o.text, "--resume"]];
|
|
213
|
+
}
|
|
214
|
+
// The app-server proxy a Codex interrupt runs over; `codex queue` has no interrupt.
|
|
215
|
+
function interruptArgv(harness) {
|
|
216
|
+
if (harness !== "codex")
|
|
217
|
+
return null;
|
|
218
|
+
return [LAUNCH_BINARY.codex, ["app-server", "proxy"]];
|
|
219
|
+
}
|
|
220
|
+
// Argv that runs the harness inside the sandbox; the outer sandbox is the isolation, so the harness's own is off.
|
|
221
|
+
// Variadic flags (--tools, --mcp-config) would swallow the prompt, so --settings comes last before it.
|
|
222
|
+
function builderArgv(harness, o) {
|
|
223
|
+
const s = o.sandbox;
|
|
224
|
+
switch (harness) {
|
|
225
|
+
case "cursor": return [LAUNCH_BINARY.cursor, ["-p", "--force", "--trust", "--sandbox", "disabled", "--model", o.model, ...(o.builderCode ? ["--approve-mcps", "--output-format", "stream-json"] : []), o.brief]];
|
|
226
|
+
case "opencode": return [LAUNCH_BINARY.opencode, ["run", "--standalone", "--auto", "--format", "json", "--model", o.model, o.brief]];
|
|
227
|
+
case "codex": return [LAUNCH_BINARY.codex, ["exec", "-m", o.model, "--dangerously-bypass-approvals-and-sandbox", CODEX_HOOK_TRUST, "--skip-git-repo-check", "-C", o.workdir, o.brief]];
|
|
228
|
+
case "claude": return [
|
|
229
|
+
o.launcher || `${s.tools}/bin/claude`, ["-p", "--model", o.model, "--dangerously-skip-permissions", "--output-format", "stream-json", "--verbose",
|
|
230
|
+
"--tools", s.claudeTools, "--strict-mcp-config", "--mcp-config", s.mcpIn, "--disable-slash-commands", "--no-chrome",
|
|
231
|
+
"--setting-sources", "", "--settings", s.settingsIn, o.brief],
|
|
232
|
+
];
|
|
233
|
+
default: return null;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
// One non-interactive writing-agent turn; the prompt is one element, so no shell quoting applies.
|
|
237
|
+
function authorArgv(harness, o) {
|
|
238
|
+
const model = o.model ?? "";
|
|
239
|
+
const flag = model ? ["--model", model] : [];
|
|
240
|
+
switch (harness) {
|
|
241
|
+
case "cursor": return [LAUNCH_BINARY.cursor, ["-p", "--trust", ...flag, o.prompt]];
|
|
242
|
+
case "claude": return [LAUNCH_BINARY.claude, ["-p", ...flag, o.prompt]];
|
|
243
|
+
case "codex": return [LAUNCH_BINARY.codex, ["exec", ...(model ? ["-m", model] : []), o.prompt]];
|
|
244
|
+
case "opencode": return [LAUNCH_BINARY.opencode, ["run", ...(model ? ["-m", model] : []), o.prompt]];
|
|
245
|
+
default: return null;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
// The Cursor ACP server: `--force` when the mode asks for it, `--model` when one is named, then `acp`.
|
|
249
|
+
// Both are top-level options; `agent acp --help` lists only -h (agent 2026.11.22-e9042b2).
|
|
250
|
+
function acpArgv(harness, o) {
|
|
251
|
+
if (harness !== "cursor")
|
|
252
|
+
return null;
|
|
253
|
+
return [LAUNCH_BINARY.cursor, [...(o.force ? ["--force"] : []), ...(o.model ? ["--model", o.model] : []), "acp"]];
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* The argv one harness takes for one kind of run: [file, args], or null when that harness has no such run or the
|
|
257
|
+
* options name a model or mode it cannot pass. The one owner of per-harness command lines.
|
|
258
|
+
*/
|
|
259
|
+
export function argv(harness, kind, opts) {
|
|
260
|
+
switch (kind) {
|
|
261
|
+
case "start": return startArgv(harness, opts);
|
|
262
|
+
case "wake": return wakeArgv(harness, opts);
|
|
263
|
+
case "compact": return compactArgv(harness, opts);
|
|
264
|
+
case "queue": return queueArgv(harness, opts);
|
|
265
|
+
case "interrupt": return interruptArgv(harness);
|
|
266
|
+
case "builder": return builderArgv(harness, opts);
|
|
267
|
+
case "author": return authorArgv(harness, opts);
|
|
268
|
+
case "acp": return acpArgv(harness, opts);
|
|
269
|
+
default: return null;
|
|
270
|
+
}
|
|
271
|
+
}
|
package/lib/hook.mjs
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook events past the read gate (scripts/temmo-hook.mjs): delivery, activity, rewake, session records.
|
|
3
|
+
* Loaded only by calls that need it, so a gate-only call skips these modules.
|
|
4
|
+
*/
|
|
5
|
+
import { loadConfig } from "./config.js";
|
|
6
|
+
import { claimInbox, publishStatus, sendMessage } from "./messaging.js";
|
|
7
|
+
import { childHint, exportParent, greetParent, parentLine } from "./family.js";
|
|
8
|
+
import { blockedHint, deniedNote } from "./blocked.js";
|
|
9
|
+
import { formatInbox, hookOutput, identityLine, stopOutput } from "./format.js";
|
|
10
|
+
import { lessonsNote } from "./lessons.js";
|
|
11
|
+
import { surfaceAlwaysOnNote, surfaceNote } from "./harness-surface.js";
|
|
12
|
+
import { currentMode, operatorPrompt, storedMode, takeModeNote } from "./mode.js";
|
|
13
|
+
import { SYSTEM_PROMPT_ENV } from "./machine-prompt.js";
|
|
14
|
+
import { decideStop, markStopRunning, noteHeld, takeHeld } from "./stop.js";
|
|
15
|
+
import { closeQuestion, openQuestions } from "./questions.js";
|
|
16
|
+
import { REQUIRE_RUN_CODE_ENV } from "./code-mode.js";
|
|
17
|
+
import { recordSession, sessionName, standsDown, readOwned } from "./sessions/registry.js";
|
|
18
|
+
import { padText, readPad } from "./pad.js";
|
|
19
|
+
import { MANAGED_ENV, MANAGED_START_REFUSED, managedStartAllowed } from "./subagent-guard.js";
|
|
20
|
+
import { harnessProcess, isRunning, processStart } from "./process.js";
|
|
21
|
+
import { CLAIM_SETTLE_MS, STOP_GRACE_MS, WAITER_MAX_S, runWaiter, takeWaiterSlot } from "./waiter.js";
|
|
22
|
+
import { ON_DEMAND_LOADS, contextUsage, dropOnDemandFiles, readContext, recordInstructions, reportContext } from "./usage.js";
|
|
23
|
+
import { activityEvents, publishActivity } from "./activity.js";
|
|
24
|
+
import { compactionFields } from "./compaction.js";
|
|
25
|
+
import { clearFields } from "./session-events.js";
|
|
26
|
+
import { sendReceipts, settleReceipts } from "./receipts.js";
|
|
27
|
+
import { closeTerminalCards, decideApproval, decideQuestion } from "./approvals.js";
|
|
28
|
+
import { classifyLimit } from "./limits.js";
|
|
29
|
+
import { alertLimit } from "./limit-alert.js";
|
|
30
|
+
import { hookTrace } from "./cursor-trace.js";
|
|
31
|
+
const EVENTS = new Map([
|
|
32
|
+
["sessionstart", "SessionStart"],
|
|
33
|
+
["startup", "SessionStart"],
|
|
34
|
+
["userpromptsubmit", "UserPromptSubmit"],
|
|
35
|
+
["beforesubmitprompt", "UserPromptSubmit"],
|
|
36
|
+
["posttooluse", "PostToolUse"],
|
|
37
|
+
["stop", "Stop"],
|
|
38
|
+
["sessionend", "SessionEnd"],
|
|
39
|
+
["precompact", "PreCompact"],
|
|
40
|
+
["postcompact", "PostCompact"],
|
|
41
|
+
["permissionrequest", "PermissionRequest"],
|
|
42
|
+
["permissiondenied", "PermissionDenied"],
|
|
43
|
+
["beforeshellexecution", "PermissionRequest"],
|
|
44
|
+
["beforemcpexecution", "PermissionRequest"],
|
|
45
|
+
]);
|
|
46
|
+
// Events that refresh the recorded session name; Codex names a thread after its first prompt.
|
|
47
|
+
const NAMED_EVENTS = new Set(["SessionStart", "UserPromptSubmit", "Stop"]);
|
|
48
|
+
// Events that show the session working: the first one after a published "hold" publishes "start".
|
|
49
|
+
const WORK_EVENTS = new Set(["userpromptsubmit", "beforesubmitprompt", "pretooluse", "posttooluse", "posttoolusefailure", "subagentstart", "precompact"]);
|
|
50
|
+
// temmo tools that wait for the operator inside a hold: calling them keeps the hold status.
|
|
51
|
+
const HOLD_TOOLS = new Set(["wait_for_answer", "wait_for_operator"]);
|
|
52
|
+
// Stop continuations that put the session back to work after a hold.
|
|
53
|
+
const WORK_AFTER_HOLD = new Set(["messages", "answer"]);
|
|
54
|
+
// Cursor permission hooks fall through to Cursor's own prompt with "ask".
|
|
55
|
+
const CURSOR_PERMISSION = new Set(["beforeshellexecution", "beforemcpexecution"]);
|
|
56
|
+
// Harnesses whose temmo plugin or extension places the lesson note on the system prompt itself; the note
|
|
57
|
+
// reaches them in a lessons field. On the others additionalContext is already the harness's system channel.
|
|
58
|
+
const EXTENSION_HOSTS = new Set(["opencode", "pi"]);
|
|
59
|
+
// The Harness² start note (lib/lessons.ts) plus the editable surface note (lib/harness-surface.ts).
|
|
60
|
+
// A resume continues a conversation that already carries them, so it gets none.
|
|
61
|
+
function startNote(cfg, host, msg) {
|
|
62
|
+
if (msg?.source === "resume")
|
|
63
|
+
return "";
|
|
64
|
+
return [lessonsNote(cfg.stateDir, cfg.project, host, process.env), surfaceNote(cfg.root || "")].filter(Boolean).join("");
|
|
65
|
+
}
|
|
66
|
+
// UserPromptSubmit overlay: always-on surface so AGENTS.local.md / memory edits land mid-session.
|
|
67
|
+
// Extension hosts (opencode, pi) put this on the lessons field, which replaces the session instruction
|
|
68
|
+
// entry — so re-include the lesson library note whenever the surface note is non-empty.
|
|
69
|
+
function turnSurfaceNote(cfg, host) {
|
|
70
|
+
const surface = surfaceAlwaysOnNote(cfg.root || "");
|
|
71
|
+
if (!surface)
|
|
72
|
+
return "";
|
|
73
|
+
if (!EXTENSION_HOSTS.has(host))
|
|
74
|
+
return surface;
|
|
75
|
+
return [lessonsNote(cfg.stateDir, cfg.project, host, process.env), surface].filter(Boolean).join("");
|
|
76
|
+
}
|
|
77
|
+
// A compaction start or clear carries the meter's last reading; a compaction start also counts the on-demand files it drops.
|
|
78
|
+
function stampContext(cfg, events) {
|
|
79
|
+
const context = readContext(cfg);
|
|
80
|
+
const tokensBefore = context.tokens ?? undefined;
|
|
81
|
+
const filesDropped = context.files.filter(entry => ON_DEMAND_LOADS.has(entry.load)).length;
|
|
82
|
+
return events.map(event => {
|
|
83
|
+
if (event.clear)
|
|
84
|
+
return { ...event, clear: clearFields({ tokensBefore }) };
|
|
85
|
+
if (event.compaction?.phase === "started")
|
|
86
|
+
return { ...event, compaction: compactionFields({ ...event.compaction, tokensBefore, filesDropped }) };
|
|
87
|
+
return event;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
// A temmo wait tool, by its MCP name (mcp__temmo__wait_for_answer, mcp__plugin_temmo_temmo__…) or bare name.
|
|
91
|
+
function holdTool(name) {
|
|
92
|
+
if (typeof name !== "string")
|
|
93
|
+
return false;
|
|
94
|
+
const parts = name.split("__");
|
|
95
|
+
return HOLD_TOOLS.has(parts.at(-1)) && (parts.length === 1 || parts.slice(0, -1).some((p) => p.endsWith("temmo") || p.endsWith("ai-collab")));
|
|
96
|
+
}
|
|
97
|
+
// Output for an activity-only registration: Cursor's beforeSubmitPrompt must allow the prompt explicitly.
|
|
98
|
+
function neutral(host, key) {
|
|
99
|
+
if (host === "cursor" && CURSOR_PERMISSION.has(key))
|
|
100
|
+
return { permission: "ask" };
|
|
101
|
+
return host === "cursor" && key === "beforesubmitprompt" ? { continue: true } : {};
|
|
102
|
+
}
|
|
103
|
+
// After compaction, Claude fires SessionStart with source "compact"; its context carries the session's pad (PostCompact output is ignored).
|
|
104
|
+
function compactPad(cfg, msg) {
|
|
105
|
+
if (msg?.source !== "compact" || readOwned(cfg, cfg.session)?.padOff === true)
|
|
106
|
+
return "";
|
|
107
|
+
return padText(readPad(cfg, `${cfg.peer}#${cfg.session}`));
|
|
108
|
+
}
|
|
109
|
+
async function deliver(cfg, host, event, msg, key) {
|
|
110
|
+
if (event === "PermissionRequest") {
|
|
111
|
+
// The harness may kill a waiting hook, for example when the prompt is answered in the terminal; close the request.
|
|
112
|
+
const ctrl = new AbortController();
|
|
113
|
+
for (const name of ["SIGTERM", "SIGINT", "SIGHUP"])
|
|
114
|
+
process.once(name, () => ctrl.abort());
|
|
115
|
+
return decideApproval(cfg, { host, key, input: msg, signal: ctrl.signal });
|
|
116
|
+
}
|
|
117
|
+
// An auto-mode denial never prompts, so in AFK mode on or quiet the operator hears of it on the channel.
|
|
118
|
+
if (event === "PermissionDenied") {
|
|
119
|
+
const { mode } = await currentMode(cfg).catch(() => storedMode(cfg));
|
|
120
|
+
if (mode !== "off" && cfg.session)
|
|
121
|
+
await sendMessage(cfg, "all", deniedNote(msg, `${cfg.peer}#${cfg.session}`)).catch(() => { });
|
|
122
|
+
return {};
|
|
123
|
+
}
|
|
124
|
+
// A subagent shares its parent's session id; its messages stay queued for the root session.
|
|
125
|
+
if (typeof msg.agent_id === "string" && msg.agent_id)
|
|
126
|
+
return neutral(host, key);
|
|
127
|
+
if (["PostToolUse", "Stop", "PreCompact"].includes(event)) {
|
|
128
|
+
const usage = contextUsage(host, msg);
|
|
129
|
+
await reportContext(cfg, usage.tokens, { window: usage.window }).catch(() => { });
|
|
130
|
+
}
|
|
131
|
+
if (event === "PreCompact")
|
|
132
|
+
return {};
|
|
133
|
+
if (event === "Stop") {
|
|
134
|
+
const onHold = async () => {
|
|
135
|
+
await publishStatus(cfg, { event: "hold", harness: host });
|
|
136
|
+
noteHeld(cfg);
|
|
137
|
+
};
|
|
138
|
+
const decision = await decideStop(cfg, { host, input: msg, onHold, marked: stopMarked });
|
|
139
|
+
// A message or an answer ends the hold: the session works again.
|
|
140
|
+
if (decision.action === "continue" && WORK_AFTER_HOLD.has(decision.why) && takeHeld(cfg)) {
|
|
141
|
+
await publishStatus(cfg, { event: "start", harness: host }).catch(() => { });
|
|
142
|
+
}
|
|
143
|
+
if (decision.action === "continue") {
|
|
144
|
+
const note = await takeModeNote(cfg);
|
|
145
|
+
return stopOutput(host, [note, decision.text].filter(Boolean).join("\n"));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (event === "Stop" || event === "SessionEnd") {
|
|
149
|
+
takeHeld(cfg);
|
|
150
|
+
await publishStatus(cfg, { event: "stop", harness: host }).catch(() => { });
|
|
151
|
+
return {};
|
|
152
|
+
}
|
|
153
|
+
let family = "";
|
|
154
|
+
if (event === "SessionStart") {
|
|
155
|
+
await publishStatus(cfg, { event: "start", harness: host }).catch(() => { });
|
|
156
|
+
await currentMode(cfg).catch(() => { });
|
|
157
|
+
if (host === "claude")
|
|
158
|
+
exportParent(cfg, process.env.CLAUDE_ENV_FILE);
|
|
159
|
+
await greetParent(cfg, { harness: host, cwd: msg.cwd }).catch(() => { });
|
|
160
|
+
family = [compactPad(cfg, msg), parentLine(cfg), childHint(cfg), blockedHint(cfg, host)].filter(Boolean).join("\n");
|
|
161
|
+
}
|
|
162
|
+
const messages = await claimInbox(cfg, { throttle: event !== "SessionStart", limit: 4 });
|
|
163
|
+
sendReceipts(cfg, messages.map((m) => m.id));
|
|
164
|
+
const note = await takeModeNote(cfg, { force: event === "SessionStart" });
|
|
165
|
+
const identity = event === "SessionStart" ? identityLine(cfg) : "";
|
|
166
|
+
const lessons = event === "SessionStart" ? startNote(cfg, host, msg) : (event === "UserPromptSubmit" ? turnSurfaceNote(cfg, host) : "");
|
|
167
|
+
const text = [note, identity, family, EXTENSION_HOSTS.has(host) ? "" : lessons, formatInbox(messages, { session: cfg.session, peer: cfg.peer })].filter(Boolean).join("\n");
|
|
168
|
+
const out = hookOutput(host, event, text);
|
|
169
|
+
return EXTENSION_HOSTS.has(host) && lessons ? { ...out, lessons } : out;
|
|
170
|
+
}
|
|
171
|
+
// Claude asyncRewake registration: waits for a peer message and exits 2 with it, which wakes an idle session.
|
|
172
|
+
async function rewake(cfg, host, msg, env) {
|
|
173
|
+
if (host !== "claude" || !cfg.session || (typeof msg.agent_id === "string" && msg.agent_id))
|
|
174
|
+
return;
|
|
175
|
+
// Grok runs Claude plugin hooks but ignores asyncRewake, so the waiter would hold its prompt or stop in the foreground.
|
|
176
|
+
if (env.GROK_HOOK_EVENT)
|
|
177
|
+
return;
|
|
178
|
+
const harness = harnessProcess(host);
|
|
179
|
+
// A headless run is never idle, and an exit 2 at UserPromptSubmit blocks its prompt; its own hooks deliver messages.
|
|
180
|
+
if (harness?.headless)
|
|
181
|
+
return;
|
|
182
|
+
const release = await takeWaiterSlot(cfg);
|
|
183
|
+
const ctrl = new AbortController();
|
|
184
|
+
for (const name of ["SIGTERM", "SIGINT", "SIGHUP"])
|
|
185
|
+
process.once(name, () => ctrl.abort());
|
|
186
|
+
let result = { code: 0 };
|
|
187
|
+
try {
|
|
188
|
+
result = await runWaiter(cfg, {
|
|
189
|
+
maxMs: (Number(process.env.TEMMO_WAITER_MAX_S) || WAITER_MAX_S) * 1000, signal: ctrl.signal,
|
|
190
|
+
graceMs: String(msg.hook_event_name || "").toLowerCase() === "stop" ? STOP_GRACE_MS : 0,
|
|
191
|
+
settleMs: CLAIM_SETTLE_MS,
|
|
192
|
+
alive: () => !harness || isRunning(harness),
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
finally {
|
|
196
|
+
release();
|
|
197
|
+
}
|
|
198
|
+
if (result.code !== 2)
|
|
199
|
+
process.exit(0);
|
|
200
|
+
process.stderr.write(`${result.text}\n`, () => process.exit(2));
|
|
201
|
+
}
|
|
202
|
+
let tracedExport = null;
|
|
203
|
+
// Set once main() has written this Stop hook's marker; deliver() then leaves the marker to the exit handler.
|
|
204
|
+
let stopMarked = false;
|
|
205
|
+
/** The Cursor trace export started by run(), awaited after the output so it never delays it. */
|
|
206
|
+
export const traced = () => tracedExport;
|
|
207
|
+
/** Handles one hook call that passed the gate; emit writes its one JSON output. */
|
|
208
|
+
export async function run({ argv, host, activityOnly, msg, cwd, env, sessionId, session, key, emit }) {
|
|
209
|
+
// Backstop for a shell start the command check misses (bash -c, npx): a managed env without a ticket stops the harness.
|
|
210
|
+
if ((key === "sessionstart" || key === "startup") && !argv.includes("--rewake")
|
|
211
|
+
&& !managedStartAllowed(env, harnessProcess(host) ?? { pid: process.ppid, start: processStart(process.ppid) })) {
|
|
212
|
+
return emit({ continue: false, stopReason: MANAGED_START_REFUSED });
|
|
213
|
+
}
|
|
214
|
+
// Every Cursor run, joined project or not, records its turn for the OTLP trace (lib/cursor-trace.ts); never awaited before output.
|
|
215
|
+
tracedExport = host === "cursor" ? hookTrace(msg, { env }).catch(() => null) : null;
|
|
216
|
+
const cfg = loadConfig(env, { cwd, host, session });
|
|
217
|
+
if (!cfg.enabled)
|
|
218
|
+
return emit(neutral(host, key));
|
|
219
|
+
// The runner drives an owned session and publishes everything itself (docs/owned-sessions-contract.md).
|
|
220
|
+
// An owned session's runner cannot inject system context itself, so this hook still carries the lesson note and the compaction pad.
|
|
221
|
+
if (standsDown(cfg, { label: session })) {
|
|
222
|
+
if (key !== "sessionstart")
|
|
223
|
+
return emit(neutral(host, key));
|
|
224
|
+
const lessons = startNote(cfg, host, msg);
|
|
225
|
+
const text = [compactPad(cfg, msg), EXTENSION_HOSTS.has(host) ? "" : lessons].filter(Boolean).join("\n");
|
|
226
|
+
const out = text ? hookOutput(host, "SessionStart", text) : neutral(host, key);
|
|
227
|
+
return emit(EXTENSION_HOSTS.has(host) && lessons ? { ...out, lessons } : out);
|
|
228
|
+
}
|
|
229
|
+
if (argv.includes("--rewake"))
|
|
230
|
+
return rewake(cfg, host, msg, env);
|
|
231
|
+
// Marked before the session record and context report, so the rewake waiter yields from this hook's first moment.
|
|
232
|
+
if (key === "stop" && !activityOnly && !(typeof msg.agent_id === "string" && msg.agent_id)) {
|
|
233
|
+
process.once("exit", markStopRunning(cfg));
|
|
234
|
+
stopMarked = true;
|
|
235
|
+
}
|
|
236
|
+
// Started first and awaited last, so delivery never waits on it.
|
|
237
|
+
const events = key === "precompact" || key === "sessionend" ? stampContext(cfg, activityEvents(host, key, msg)) : activityEvents(host, key, msg);
|
|
238
|
+
if (env[SYSTEM_PROMPT_ENV] === "1") {
|
|
239
|
+
for (const event of events)
|
|
240
|
+
if (event.type === "prompt")
|
|
241
|
+
event.system = true;
|
|
242
|
+
}
|
|
243
|
+
const activity = publishActivity(cfg, host, events).catch(() => 0);
|
|
244
|
+
// A finished compaction has dropped the on-demand files, so the next one must not count them again.
|
|
245
|
+
const dropped = key === "postcompact" ? dropOnDemandFiles(cfg).catch(() => false) : Promise.resolve(false);
|
|
246
|
+
// A view-only approval card (AFK off) closes once a later event shows the terminal prompt resolved.
|
|
247
|
+
const closing = closeTerminalCards(cfg, { host, key, input: msg }).catch(() => []);
|
|
248
|
+
// Claude's StopFailure with error rate_limit: a limit card for this session, one per window (lib/limit-alert.ts).
|
|
249
|
+
const limit = key === "stopfailure" && host === "claude" && !(typeof msg.agent_id === "string" && msg.agent_id) ? classifyLimit("claude", { kind: "stop_failure", ...msg }) : null;
|
|
250
|
+
const limited = limit ? alertLimit(cfg, { harness: "claude", limit, auto: true }).catch(() => ({})) : Promise.resolve({});
|
|
251
|
+
const event = EVENTS.get(key);
|
|
252
|
+
let output = neutral(host, key);
|
|
253
|
+
// A prompt the operator typed (not a wake run, agent hand-back or temmo's own text); recorded, AFK unchanged.
|
|
254
|
+
const typed = (key === "userpromptsubmit" || key === "beforesubmitprompt") && operatorPrompt(msg.prompt);
|
|
255
|
+
// A subagent's hooks carry the parent's session id; its cwd (often a worktree) must not rename the parent.
|
|
256
|
+
const subagent = typeof msg.agent_id === "string" && msg.agent_id !== "";
|
|
257
|
+
// The operator answered at the terminal, so the session's open phone questions no longer hold its Stop.
|
|
258
|
+
if (typed && !subagent)
|
|
259
|
+
for (const q of openQuestions(cfg))
|
|
260
|
+
await closeQuestion(cfg, q.id).catch(() => { });
|
|
261
|
+
// The PreToolUse matcher for AskUserQuestion blocks until the phone answers or the terminal dialog takes over.
|
|
262
|
+
if (key === "pretooluse" && msg.tool_name === "AskUserQuestion" && !activityOnly) {
|
|
263
|
+
const ctrl = new AbortController();
|
|
264
|
+
for (const name of ["SIGTERM", "SIGINT", "SIGHUP"])
|
|
265
|
+
process.once(name, () => ctrl.abort());
|
|
266
|
+
return emit(subagent ? {} : await decideQuestion(cfg, { host, input: msg, signal: ctrl.signal }));
|
|
267
|
+
}
|
|
268
|
+
// A turn start or a tool call after a hold: the session works again, so the hold status ends.
|
|
269
|
+
const resumed = !subagent && WORK_EVENTS.has(key) && !holdTool(msg.tool_name) && takeHeld(cfg) ? publishStatus(cfg, { event: "start", harness: host }).catch(() => { }) : Promise.resolve();
|
|
270
|
+
// Claude's InstructionsLoaded: an instruction file entered the context; recorded for the session's context view.
|
|
271
|
+
if (key === "instructionsloaded" && !subagent)
|
|
272
|
+
await recordInstructions(cfg, msg).catch(() => { });
|
|
273
|
+
if (event || events.length) {
|
|
274
|
+
try {
|
|
275
|
+
if (!subagent)
|
|
276
|
+
await recordSession(cfg, {
|
|
277
|
+
id: sessionId, cwd, harness: host, event: event || key.slice(0, 32), proc: harnessProcess(host), parent: cfg.parent,
|
|
278
|
+
name: NAMED_EVENTS.has(event) ? sessionName(host, msg, env) : "", model: msg.model,
|
|
279
|
+
requireRunCode: env[REQUIRE_RUN_CODE_ENV] === "1", managed: env[MANAGED_ENV] === "1",
|
|
280
|
+
...(typed ? { operatorPromptAt: Date.now() } : {}),
|
|
281
|
+
}).catch(() => { });
|
|
282
|
+
if (event && !activityOnly)
|
|
283
|
+
output = await deliver(cfg, host, event, msg, key);
|
|
284
|
+
}
|
|
285
|
+
catch {
|
|
286
|
+
output = neutral(host, key);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
emit(output);
|
|
290
|
+
await Promise.all([activity, closing, dropped, limited, resumed, settleReceipts()]);
|
|
291
|
+
}
|
package/lib/idle.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Idle signal per harness, for `wait idle:<address>` and the dispatcher.
|
|
3
|
+
* Claude: the status in ~/.claude/sessions/<pid>.json. OpenCode: /api/session/active. Codex and Cursor expose none
|
|
4
|
+
* to outside processes, so the last hook event the registry recorded decides: Stop (or a start or end) means idle.
|
|
5
|
+
*/
|
|
6
|
+
import { readFileSync } from "node:fs";
|
|
7
|
+
import { homedir } from "node:os";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
import { activeOpencodeSessions } from "./opencode-api.js";
|
|
10
|
+
const IDLE_EVENTS = new Set(["Stop", "SessionStart", "SessionEnd"]);
|
|
11
|
+
function claudeStatus(session, home) {
|
|
12
|
+
try {
|
|
13
|
+
const s = JSON.parse(readFileSync(join(home, ".claude", "sessions", `${session.pid}.json`), "utf8"));
|
|
14
|
+
if (s.sessionId !== session.sessionId || typeof s.status !== "string")
|
|
15
|
+
return "unknown";
|
|
16
|
+
return s.status === "idle" ? "idle" : "busy";
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return "unknown";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** "idle", "busy" or "unknown" for a registry session. active is a precomputed set of running OpenCode session ids. */
|
|
23
|
+
export async function sessionIdleState(session, { env = process.env, home = env.HOME || homedir(), active, fetchFn } = {}) {
|
|
24
|
+
if (session.runningJobs > 0)
|
|
25
|
+
return "busy";
|
|
26
|
+
switch (session.harness) {
|
|
27
|
+
case "claude": return Number.isInteger(session.pid) ? claudeStatus(session, home) : "unknown";
|
|
28
|
+
case "opencode": {
|
|
29
|
+
const ids = active === undefined ? await activeOpencodeSessions({ env, fetchFn }) : active;
|
|
30
|
+
if (!ids)
|
|
31
|
+
return "unknown";
|
|
32
|
+
return ids.has(session.sessionId) ? "busy" : "idle";
|
|
33
|
+
}
|
|
34
|
+
case "codex":
|
|
35
|
+
case "cursor":
|
|
36
|
+
case "pi":
|
|
37
|
+
return IDLE_EVENTS.has(session.event) ? "idle" : "busy";
|
|
38
|
+
default: return "unknown";
|
|
39
|
+
}
|
|
40
|
+
}
|