@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/operator.js
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
// Operator phone replies: matching, the reply wait (stream or poll), the approval-end envelope, and the wait limit.
|
|
2
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
3
|
+
import { encodeEnvelope, parseWireEnvelope } from "./agui.js";
|
|
4
|
+
import { parseMode } from "./mode.js";
|
|
5
|
+
import { pollCached, streamEvents } from "./messaging.js";
|
|
6
|
+
export const WAIT_MAX_S = 45;
|
|
7
|
+
const RETRY_MAX_MS = 30_000;
|
|
8
|
+
const REPLY = /^reply ([a-f0-9]{12}) ([\s\S]+)$/;
|
|
9
|
+
const MAX_ANSWER_CHARS = 2000;
|
|
10
|
+
const SINCE_DEFAULT = "12h";
|
|
11
|
+
/** A phone reply as {id, answer}, or null when the text is not a reply with a non-blank answer. */
|
|
12
|
+
export function parsePhoneReply(message) {
|
|
13
|
+
const match = REPLY.exec(message);
|
|
14
|
+
if (!match || !match[2].trim())
|
|
15
|
+
return null;
|
|
16
|
+
return { id: match[1], answer: match[2].trim().slice(0, MAX_ANSWER_CHARS) };
|
|
17
|
+
}
|
|
18
|
+
/** The question ID an operator Skip closes, or null. */
|
|
19
|
+
export function skippedQuestionId(message) {
|
|
20
|
+
let e;
|
|
21
|
+
try {
|
|
22
|
+
e = parseWireEnvelope(message);
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
return e?.kind === "approval-end" && e.reason === "skipped" && e.from === "operator" ? e.id : null;
|
|
28
|
+
}
|
|
29
|
+
// One option label matched case-insensitively, else the text itself (the "Other" answer).
|
|
30
|
+
function matchLabel(question, value) {
|
|
31
|
+
const found = question.options.find((o) => o.label.toLowerCase() === value.trim().toLowerCase());
|
|
32
|
+
return found ? found.label : value.trim();
|
|
33
|
+
}
|
|
34
|
+
/** Chosen labels per question from a reply: JSON {question: answer}, or plain text for a single question; null when incomplete. */
|
|
35
|
+
export function choiceLabels(questions, answer) {
|
|
36
|
+
let map = null;
|
|
37
|
+
try {
|
|
38
|
+
const value = JSON.parse(answer);
|
|
39
|
+
if (value && typeof value === "object" && !Array.isArray(value))
|
|
40
|
+
map = value;
|
|
41
|
+
}
|
|
42
|
+
catch { /* plain text */ }
|
|
43
|
+
if (!map) {
|
|
44
|
+
if (questions.length !== 1)
|
|
45
|
+
return null;
|
|
46
|
+
map = { [questions[0].question]: answer };
|
|
47
|
+
}
|
|
48
|
+
const labels = {};
|
|
49
|
+
for (const q of questions) {
|
|
50
|
+
const value = map[q.question];
|
|
51
|
+
if (typeof value !== "string" || !value.trim())
|
|
52
|
+
return null;
|
|
53
|
+
const parts = q.multiSelect ? value.split(",").filter((part) => part.trim()) : [value];
|
|
54
|
+
labels[q.question] = parts.map((part) => matchLabel(q, part));
|
|
55
|
+
}
|
|
56
|
+
return labels;
|
|
57
|
+
}
|
|
58
|
+
/** Answers for a choice from a reply: {question: "label, label"}, or null when incomplete. */
|
|
59
|
+
export function parseChoice(questions, answer) {
|
|
60
|
+
const labels = choiceLabels(questions, answer);
|
|
61
|
+
return labels && Object.fromEntries(Object.entries(labels).map(([question, parts]) => [question, parts.join(", ")]));
|
|
62
|
+
}
|
|
63
|
+
/** Any operator reply or skip, whatever id it names: a skip, or a reply with a non-blank answer; null for anything else. */
|
|
64
|
+
export function readReply(event) {
|
|
65
|
+
const skipped = skippedQuestionId(event.message);
|
|
66
|
+
if (skipped !== null)
|
|
67
|
+
return { skipped: true, id: skipped };
|
|
68
|
+
const reply = parsePhoneReply(event.message);
|
|
69
|
+
if (!reply)
|
|
70
|
+
return null;
|
|
71
|
+
return { skipped: false, id: reply.id, answer: reply.answer, messageId: event.id, ...(event.verified === true ? { verified: true } : {}) };
|
|
72
|
+
}
|
|
73
|
+
/** The one reply parser: a skip of id, or a reply to id (a choice reply must name valid labels); null for anything else. */
|
|
74
|
+
export function matchReply(event, { id, questions }) {
|
|
75
|
+
const match = readReply(event);
|
|
76
|
+
if (!match || match.id !== id)
|
|
77
|
+
return null;
|
|
78
|
+
if (match.skipped)
|
|
79
|
+
return match;
|
|
80
|
+
const labels = questions ? choiceLabels(questions, match.answer) : undefined;
|
|
81
|
+
if (questions && !labels)
|
|
82
|
+
return null;
|
|
83
|
+
return labels ? { ...match, labels } : match;
|
|
84
|
+
}
|
|
85
|
+
/** Throws unless seconds is an integer from 0 to WAIT_MAX_S. */
|
|
86
|
+
export function assertWaitSeconds(seconds) {
|
|
87
|
+
if (!Number.isInteger(seconds) || seconds < 0 || seconds > WAIT_MAX_S) {
|
|
88
|
+
throw new Error(`Wait must be an integer from 0 to ${WAIT_MAX_S} seconds`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/** Backoff for stream reconnects: starts at baseMs, doubles per failed wait up to maxMs, and resets when a stream opens. */
|
|
92
|
+
export function reconnectBackoff(baseMs, maxMs = RETRY_MAX_MS) {
|
|
93
|
+
let ms = baseMs;
|
|
94
|
+
return {
|
|
95
|
+
reset() { ms = baseMs; },
|
|
96
|
+
async wait(signal, capMs = Infinity) {
|
|
97
|
+
await delay(Math.min(ms, capMs), undefined, { signal }).catch(() => { });
|
|
98
|
+
ms = Math.min(ms * 2, maxMs);
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/** The approval-end envelope that closes a card; session, owned, url and why appear only when given. */
|
|
103
|
+
export function approvalEnd(cfg, id, reason, extra = {}) {
|
|
104
|
+
const { session, owned, url, why } = extra;
|
|
105
|
+
return encodeEnvelope("approval-end", {
|
|
106
|
+
from: cfg.peer, ...(session !== undefined ? { session } : {}), id, reason, ...(owned ? { owned: true } : {}),
|
|
107
|
+
...(url ? { url } : {}), ...(why ? { why } : {}), ts: new Date().toISOString(),
|
|
108
|
+
}, cfg.wire);
|
|
109
|
+
}
|
|
110
|
+
// Matches one event against the wait; null keeps waiting (no match, or read rejected the reply).
|
|
111
|
+
function scanEvent(event, o) {
|
|
112
|
+
const match = matchReply(event, { id: o.id, questions: o.questions });
|
|
113
|
+
if (!match)
|
|
114
|
+
return null;
|
|
115
|
+
if (match.skipped)
|
|
116
|
+
return { status: "skipped" };
|
|
117
|
+
const value = o.read(match);
|
|
118
|
+
return value === null ? null : { status: "reply", value, messageId: event.id };
|
|
119
|
+
}
|
|
120
|
+
// Streams the topic; a failed or ended stream reconnects after backoff, resuming from the last event id.
|
|
121
|
+
async function streamWait(o) {
|
|
122
|
+
const stream = o.stream ?? streamEvents;
|
|
123
|
+
const ctrl = new AbortController();
|
|
124
|
+
let reason = "timeout";
|
|
125
|
+
const stop = (why) => { if (!ctrl.signal.aborted) {
|
|
126
|
+
reason = why;
|
|
127
|
+
ctrl.abort();
|
|
128
|
+
} };
|
|
129
|
+
const timer = setTimeout(() => stop("timeout"), Math.max(0, o.deadline - Date.now()));
|
|
130
|
+
const onAbort = () => stop("cancelled");
|
|
131
|
+
o.signal?.addEventListener("abort", onAbort, { once: true });
|
|
132
|
+
if (o.signal?.aborted)
|
|
133
|
+
stop("cancelled");
|
|
134
|
+
const backoff = reconnectBackoff(o.cfg.stopPollMs);
|
|
135
|
+
let since = o.since ?? "";
|
|
136
|
+
try {
|
|
137
|
+
while (!ctrl.signal.aborted) {
|
|
138
|
+
try {
|
|
139
|
+
for await (const event of stream(o.cfg, [o.topic], { since, signal: ctrl.signal, onOpen: () => backoff.reset() })) {
|
|
140
|
+
since = event.id;
|
|
141
|
+
if (o.afterMode !== undefined) {
|
|
142
|
+
const mode = parseMode(event.message);
|
|
143
|
+
if (mode?.mode === "off" && mode.ts > o.afterMode)
|
|
144
|
+
return { status: "afk-off" };
|
|
145
|
+
}
|
|
146
|
+
const outcome = scanEvent(event, o);
|
|
147
|
+
if (outcome)
|
|
148
|
+
return outcome;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
catch { /* outage, idle timeout, or abort */ }
|
|
152
|
+
if (ctrl.signal.aborted)
|
|
153
|
+
break;
|
|
154
|
+
await backoff.wait(ctrl.signal);
|
|
155
|
+
}
|
|
156
|
+
return { status: reason };
|
|
157
|
+
}
|
|
158
|
+
finally {
|
|
159
|
+
clearTimeout(timer);
|
|
160
|
+
o.signal?.removeEventListener("abort", onAbort);
|
|
161
|
+
ctrl.abort();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
// Polls the topic every intervalMs; an abort throws, and a deadline with no match returns timeout.
|
|
165
|
+
async function pollWait(o) {
|
|
166
|
+
const poll = o.poll ?? pollCached;
|
|
167
|
+
const interval = o.intervalMs ?? 4000;
|
|
168
|
+
let since = o.since ?? SINCE_DEFAULT;
|
|
169
|
+
while (true) {
|
|
170
|
+
o.signal?.throwIfAborted();
|
|
171
|
+
const events = await poll(o.cfg, o.topic, since, o.signal);
|
|
172
|
+
since = events.at(-1)?.id ?? since;
|
|
173
|
+
for (const event of events) {
|
|
174
|
+
const outcome = scanEvent(event, o);
|
|
175
|
+
if (outcome)
|
|
176
|
+
return outcome;
|
|
177
|
+
}
|
|
178
|
+
const remaining = o.deadline - Date.now();
|
|
179
|
+
if (remaining <= 0)
|
|
180
|
+
return { status: "timeout" };
|
|
181
|
+
await delay(Math.min(interval, remaining), undefined, { signal: o.signal });
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/** Waits for the operator's reply, skip or afk-off on the topic until the deadline. Stream mode reconnects; poll mode throws on abort. */
|
|
185
|
+
export function waitForOperatorReply(o) {
|
|
186
|
+
return o.mode === "stream" ? streamWait(o) : pollWait(o);
|
|
187
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Derives the pad's runner-owned changes and verification lines from adapter tool-call events
|
|
3
|
+
* (docs/roadmap.md stage 3, piece 3, decision 3). The runner buffers one tracker per session and flushes it at turn end.
|
|
4
|
+
*/
|
|
5
|
+
import { stripControl } from "./format.js";
|
|
6
|
+
// The AG-UI tool-call event types, as the adapters emit them (lib/agui.ts).
|
|
7
|
+
const TOOL_CALL_CHUNK = "TOOL_CALL_CHUNK";
|
|
8
|
+
const TOOL_CALL_RESULT = "TOOL_CALL_RESULT";
|
|
9
|
+
// Tools that write files: Claude Edit/Write/MultiEdit, Codex Edit and apply_patch, OpenCode edit/write.
|
|
10
|
+
const FILE_TOOL = /^(edit|write|multiedit|notebookedit|patch|apply_patch)$/i;
|
|
11
|
+
const WRITE_TOOL = /^write$/i;
|
|
12
|
+
// Tools that run a shell command: Claude and Codex Bash, OpenCode bash and shell, Codex exec, Cursor execute.
|
|
13
|
+
const SHELL_TOOL = /^(bash|shell|exec|execute|terminal|command)$/i;
|
|
14
|
+
// Test, lint and typecheck commands worth a verification line.
|
|
15
|
+
const VERIFY_COMMAND = /(?:\bnode\s+--test\b|\b(?:npm|pnpm|yarn|bun)\s+(?:run\s+)?test\b|\b(?:npm|pnpm|yarn|bun)\s+(?:run\s+)?lint\b|\b(?:npm|pnpm|yarn|bun)\s+run\s+typecheck\b|\btsc\b|\bpytest\b|\bvitest\b|\bjest\b|\bgo\s+test\b)/;
|
|
16
|
+
// A shell command that commits; the subject rides -m or --message when the command shows one.
|
|
17
|
+
const COMMIT_COMMAND = /\bgit\s+commit\b/;
|
|
18
|
+
const COMMIT_SUBJECT = /(?:-m|--message)[=\s]+["']?([^"'\n]+)/;
|
|
19
|
+
// The files an apply_patch envelope names per section.
|
|
20
|
+
const PATCH_FILE = /^\*\*\* (?:Add|Update|Delete) File: (.+)$/gm;
|
|
21
|
+
// Longest command or subject kept in one line, so a giant one-off script cannot crowd the section out.
|
|
22
|
+
const LINE_MAX = 160;
|
|
23
|
+
export function createPadActivity() {
|
|
24
|
+
return { changes: [], verify: new Map(), running: new Map() };
|
|
25
|
+
}
|
|
26
|
+
/** Buffers what one tool-call event says the session did; a result confirms the call actually ran. */
|
|
27
|
+
export function trackEvent(a, e) {
|
|
28
|
+
if (e?.type === TOOL_CALL_CHUNK)
|
|
29
|
+
trackCall(a, e);
|
|
30
|
+
else if (e?.type === TOOL_CALL_RESULT)
|
|
31
|
+
trackResult(a, e);
|
|
32
|
+
}
|
|
33
|
+
/** Drains the buffers: the change lines and the latest verification line per command, newest last. */
|
|
34
|
+
export function takeLines(a) {
|
|
35
|
+
const changes = a.changes.splice(0);
|
|
36
|
+
const verification = [...a.verify.values()];
|
|
37
|
+
a.verify.clear();
|
|
38
|
+
a.running.clear();
|
|
39
|
+
return { changes, verification };
|
|
40
|
+
}
|
|
41
|
+
function trackCall(a, e) {
|
|
42
|
+
const id = e.toolCallId;
|
|
43
|
+
if (typeof id !== "string" || !id)
|
|
44
|
+
return;
|
|
45
|
+
const name = String(e.toolCallName || "");
|
|
46
|
+
if (FILE_TOOL.test(name)) {
|
|
47
|
+
const paths = filePaths(name, e.delta);
|
|
48
|
+
if (paths.length)
|
|
49
|
+
a.running.set(id, { kind: "file", verb: WRITE_TOOL.test(name) ? "Wrote" : "Edited", paths });
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (SHELL_TOOL.test(name)) {
|
|
53
|
+
const command = commandOf(e.delta);
|
|
54
|
+
if (!command)
|
|
55
|
+
return;
|
|
56
|
+
if (COMMIT_COMMAND.test(command))
|
|
57
|
+
a.running.set(id, { kind: "commit", command });
|
|
58
|
+
else if (VERIFY_COMMAND.test(command))
|
|
59
|
+
a.running.set(id, { kind: "verify", command });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function trackResult(a, e) {
|
|
63
|
+
const id = e.toolCallId;
|
|
64
|
+
const entry = typeof id === "string" ? a.running.get(id) : undefined;
|
|
65
|
+
if (!entry)
|
|
66
|
+
return;
|
|
67
|
+
a.running.delete(id);
|
|
68
|
+
const failed = e.metadata?.aiCollab?.failed === true;
|
|
69
|
+
if (entry.kind === "file" && !failed)
|
|
70
|
+
for (const path of entry.paths)
|
|
71
|
+
addChange(a, `${entry.verb} ${path}`);
|
|
72
|
+
else if (entry.kind === "commit" && !failed)
|
|
73
|
+
addChange(a, commitLine(entry.command));
|
|
74
|
+
else if (entry.kind === "verify")
|
|
75
|
+
a.verify.set(entry.command, `${oneLine(entry.command)}: ${outcomeOf(String(e.content ?? ""), failed)}`);
|
|
76
|
+
}
|
|
77
|
+
// One line per change; a repeat moves to the end, where the cap keeps it.
|
|
78
|
+
function addChange(a, line) {
|
|
79
|
+
const at = a.changes.indexOf(line);
|
|
80
|
+
if (at >= 0)
|
|
81
|
+
a.changes.splice(at, 1);
|
|
82
|
+
a.changes.push(line);
|
|
83
|
+
}
|
|
84
|
+
// The files an edit-family call names: a path field, Codex's changes array, or apply_patch sections.
|
|
85
|
+
function filePaths(tool, delta) {
|
|
86
|
+
const text = typeof delta === "string" ? delta : "";
|
|
87
|
+
const value = jsonOf(text);
|
|
88
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
89
|
+
const one = value.file_path ?? value.filePath ?? value.path;
|
|
90
|
+
if (typeof one === "string" && one.trim())
|
|
91
|
+
return [oneLine(one)];
|
|
92
|
+
if (Array.isArray(value.changes))
|
|
93
|
+
return value.changes.map((c) => (typeof c?.path === "string" ? oneLine(c.path) : "")).filter(Boolean);
|
|
94
|
+
const patch = [value.command, value.input, value.patch].find((p) => typeof p === "string" && p.includes("*** "));
|
|
95
|
+
return patch ? patchPaths(patch) : [];
|
|
96
|
+
}
|
|
97
|
+
return /^apply_patch$/i.test(tool) ? patchPaths(text) : [];
|
|
98
|
+
}
|
|
99
|
+
const patchPaths = (text) => [...text.matchAll(PATCH_FILE)].map((m) => oneLine(m[1])).filter(Boolean);
|
|
100
|
+
// The command a shell call runs: its command field, or for a Codex exec script the quoted command that carries the signal.
|
|
101
|
+
function commandOf(delta) {
|
|
102
|
+
const text = typeof delta === "string" ? delta : "";
|
|
103
|
+
const value = jsonOf(text);
|
|
104
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
105
|
+
const command = value.command ?? value.cmd;
|
|
106
|
+
if (typeof command === "string")
|
|
107
|
+
return oneLine(command);
|
|
108
|
+
}
|
|
109
|
+
const quoted = [...text.matchAll(/["'`]([^"'`\n]+)["'`]/g)].map((m) => m[1]);
|
|
110
|
+
return oneLine(quoted.find((q) => VERIFY_COMMAND.test(q) || COMMIT_COMMAND.test(q)) ?? text);
|
|
111
|
+
}
|
|
112
|
+
// A commit line: the -m subject when the command shows one.
|
|
113
|
+
function commitLine(command) {
|
|
114
|
+
const subject = COMMIT_SUBJECT.exec(command)?.[1]?.trim();
|
|
115
|
+
return subject ? `Committed: ${subject}` : "Committed";
|
|
116
|
+
}
|
|
117
|
+
// The outcome a tool result shows: pass and fail counts, else an exit code, else the failed flag.
|
|
118
|
+
function outcomeOf(content, failed) {
|
|
119
|
+
const count = (word) => {
|
|
120
|
+
const m = new RegExp(`(\\d+)\\s+${word}(?:ed)?\\b|\\b${word}(?:ed)?[:\\s]+(\\d+)`, "i").exec(content);
|
|
121
|
+
return m ? Number(m[1] ?? m[2]) : undefined;
|
|
122
|
+
};
|
|
123
|
+
const parts = [count("pass"), count("fail")].map((n, i) => (n === undefined ? "" : `${n} ${i ? "failed" : "passed"}`)).filter(Boolean);
|
|
124
|
+
if (parts.length)
|
|
125
|
+
return parts.join(", ");
|
|
126
|
+
const exit = /exit code (\d+)/i.exec(content)?.[1];
|
|
127
|
+
if (exit !== undefined)
|
|
128
|
+
return exit === "0" ? "passed" : `exit ${exit}`;
|
|
129
|
+
return failed ? "failed" : "passed";
|
|
130
|
+
}
|
|
131
|
+
// One clean line: control characters gone, whitespace collapsed, cut to LINE_MAX.
|
|
132
|
+
function oneLine(text) {
|
|
133
|
+
const flat = stripControl(text).replace(/\s+/g, " ").trim();
|
|
134
|
+
return flat.length > LINE_MAX ? `${flat.slice(0, LINE_MAX - 1)}…` : flat;
|
|
135
|
+
}
|
|
136
|
+
const jsonOf = (text) => {
|
|
137
|
+
try {
|
|
138
|
+
return JSON.parse(text);
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
};
|
package/lib/pad-fresh.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// Most changed files the block lists; the count still says how far HEAD moved.
|
|
2
|
+
const FRESH_FILES_MAX = 10;
|
|
3
|
+
// Short sha length in the block, git's own default.
|
|
4
|
+
const SHORT_SHA = 7;
|
|
5
|
+
// The verb a runner change line starts with; the rest of the line is the path it names.
|
|
6
|
+
const CHANGE_PATH = /^(?:Edited|Wrote)\s+/;
|
|
7
|
+
// A sha as the block shows it.
|
|
8
|
+
const short = (sha) => sha.slice(0, SHORT_SHA);
|
|
9
|
+
// A pad section as text; a missing or non-string one reads as empty.
|
|
10
|
+
const section = (pad, key) => (typeof pad?.[key] === "string" ? pad[key] : "");
|
|
11
|
+
// True when a changes line names the file: equal paths, or one is the other's tail (absolute path vs repo-relative).
|
|
12
|
+
function mentions(changes, file) {
|
|
13
|
+
return changes.split("\n").some((line) => {
|
|
14
|
+
const path = line.replace(CHANGE_PATH, "").trim();
|
|
15
|
+
return path !== "" && (path === file || path.endsWith(`/${file}`) || file.endsWith(`/${path}`));
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The Freshness block for one pad, or "" when there is nothing to say: no stamp, HEAD unchanged, or git failed.
|
|
20
|
+
* At most three git calls: the current HEAD, the commits since the stamp, and the files the two commits differ in.
|
|
21
|
+
* A --ancestry-path count of zero means the stamped commit is off this branch now (or gone): a rewritten branch.
|
|
22
|
+
*/
|
|
23
|
+
export async function padFreshness(pad, git) {
|
|
24
|
+
const stamp = section(pad, "head");
|
|
25
|
+
if (!stamp)
|
|
26
|
+
return "";
|
|
27
|
+
const head = await git(["rev-parse", "HEAD"]);
|
|
28
|
+
const current = head.code === 0 ? head.stdout.trim() : "";
|
|
29
|
+
if (!current || current === stamp)
|
|
30
|
+
return "";
|
|
31
|
+
const count = await git(["rev-list", "--count", "--ancestry-path", `${stamp}..${current}`]);
|
|
32
|
+
const moved = count.code === 0 ? Number(count.stdout.trim()) : -1;
|
|
33
|
+
const lines = [moved > 0
|
|
34
|
+
? `HEAD moved from ${short(stamp)} to ${short(current)} (${moved} commit${moved === 1 ? "" : "s"}) since this pad was written.`
|
|
35
|
+
: `HEAD moved from ${short(stamp)} to ${short(current)} since this pad was written; the branch was rewritten.`];
|
|
36
|
+
const diff = await git(["diff", "--name-only", stamp, current]);
|
|
37
|
+
const touched = diff.code === 0 ? diff.stdout.split("\n").map((line) => line.trim()).filter(Boolean) : [];
|
|
38
|
+
const overlap = touched.filter((file) => mentions(section(pad, "changes"), file)).slice(0, FRESH_FILES_MAX);
|
|
39
|
+
if (overlap.length)
|
|
40
|
+
lines.push(`Files changed since then that also appear in your Changes: ${overlap.join(", ")}`);
|
|
41
|
+
if (section(pad, "verification").trim())
|
|
42
|
+
lines.push("Verification results predate HEAD; rerun them before relying on them.");
|
|
43
|
+
return ["## Freshness", ...lines].join("\n");
|
|
44
|
+
}
|
package/lib/pad.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session pad: one file per session, the recap that keeps a long session on course
|
|
3
|
+
* across compaction, wake and restart (docs/roadmap.md stage 3; arXiv 2611.05306).
|
|
4
|
+
* The agent writes objective, facts, dead ends and plan; changes and verification are runner-owned
|
|
5
|
+
* (piece 3 writes them from activity rows), so updatePad refuses them. Both writers add to constraints (piece 4):
|
|
6
|
+
* the runner copies (brief) and (operator) rule lines, the agent adds (agent) lines, and no writer replaces the others.
|
|
7
|
+
*/
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
import { stripControl } from "./format.js";
|
|
10
|
+
import { readJson, writeJson } from "./store.js";
|
|
11
|
+
// One section's character cap; the capped sections stay small enough to inject at every wake, restart and compaction.
|
|
12
|
+
export const PAD_SECTION_MAX = 2000;
|
|
13
|
+
// The paper's six parts plus the constraint line, in injection order; constraints lead so a compacted session meets its rules first.
|
|
14
|
+
export const PAD_SECTIONS = ["constraints", "objective", "facts", "changes", "verification", "dead_ends", "plan"];
|
|
15
|
+
// The four the agent writes; changes and verification belong to the runner (decision 3).
|
|
16
|
+
export const PAD_AGENT_SECTIONS = ["objective", "facts", "dead_ends", "plan"];
|
|
17
|
+
export const PAD_RUNNER_SECTIONS = ["changes", "verification"];
|
|
18
|
+
// The section both writers add to (piece 4); each line carries its source: (brief), (operator) or (agent).
|
|
19
|
+
export const PAD_CONSTRAINTS = "constraints";
|
|
20
|
+
const ADDRESS = /^[a-z0-9-]+#[a-f0-9]{16}$/;
|
|
21
|
+
// The command a verification line records: everything before the outcome, so a re-run replaces its line.
|
|
22
|
+
const VERIFY_KEY = ": ";
|
|
23
|
+
const HEADINGS = {
|
|
24
|
+
constraints: "Constraints", objective: "Objective", facts: "Facts and decisions", changes: "Changes", verification: "Verification", dead_ends: "Dead ends", plan: "Remaining plan",
|
|
25
|
+
};
|
|
26
|
+
// A line is rule-like when it carries one of these words (piece 4); the runner copies such lines from the brief and operator messages.
|
|
27
|
+
const RULE_LINE = /\b(?:never|always|don['\u2019]t|do not|must not|only)\b/i;
|
|
28
|
+
// The source tag a stored constraint line starts with.
|
|
29
|
+
const CONSTRAINT_SOURCE = /^\((brief|operator|agent)\)\s+/;
|
|
30
|
+
// Longest constraint line kept, tag included; a longer one is cut with an ellipsis.
|
|
31
|
+
const CONSTRAINT_LINE_MAX = 200;
|
|
32
|
+
/** The pad file of one session address, beside the registry in the state dir (decision 6). */
|
|
33
|
+
export function padFile(cfg, address) {
|
|
34
|
+
if (!ADDRESS.test(address || ""))
|
|
35
|
+
throw new Error("invalid session address");
|
|
36
|
+
return join(cfg.stateDir, cfg.project, "pads", `${address}.json`);
|
|
37
|
+
}
|
|
38
|
+
/** The pad of one session address, or null when it is absent, corrupt, or the address is malformed. */
|
|
39
|
+
export function readPad(cfg, address) {
|
|
40
|
+
if (!ADDRESS.test(address || ""))
|
|
41
|
+
return null;
|
|
42
|
+
const pad = readJson(padFile(cfg, address));
|
|
43
|
+
return pad && pad.v === 1 && pad.address === address ? pad : null;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Merge agent-written sections into the session's pad and write it atomically.
|
|
47
|
+
* Refuses another session's label, a runner-owned section, and a section over PAD_SECTION_MAX.
|
|
48
|
+
* constraints is merged, not replaced: the agent adds (agent) lines and cannot remove the runner's lines.
|
|
49
|
+
*/
|
|
50
|
+
export function updatePad(cfg, { label, sections }) {
|
|
51
|
+
if (!cfg.session || label !== cfg.session)
|
|
52
|
+
throw new Error("A session can only write its own pad");
|
|
53
|
+
const address = `${cfg.peer}#${label}`;
|
|
54
|
+
if (!ADDRESS.test(address))
|
|
55
|
+
throw new Error("invalid session address");
|
|
56
|
+
for (const key of Object.keys(sections)) {
|
|
57
|
+
if (PAD_RUNNER_SECTIONS.includes(key))
|
|
58
|
+
throw new Error(`${key} is written by the runner; the pad tool refuses it`);
|
|
59
|
+
if (!PAD_AGENT_SECTIONS.includes(key) && key !== PAD_CONSTRAINTS)
|
|
60
|
+
throw new Error(`${key} is not a pad section you can write`);
|
|
61
|
+
if (typeof sections[key] !== "string")
|
|
62
|
+
throw new Error(`${key} must be a string`);
|
|
63
|
+
}
|
|
64
|
+
const pad = readPad(cfg, address) ?? { v: 1, address, updatedAt: 0 };
|
|
65
|
+
for (const key of Object.keys(sections)) {
|
|
66
|
+
const text = stripControl(sections[key]).trim();
|
|
67
|
+
if (text.length > PAD_SECTION_MAX)
|
|
68
|
+
throw new Error(`${key} is ${text.length} characters; the cap is ${PAD_SECTION_MAX}`);
|
|
69
|
+
// The agent's constraint lines join the section as (agent) lines; a forged source tag is stripped before tagging.
|
|
70
|
+
pad[key] = key === PAD_CONSTRAINTS
|
|
71
|
+
? capConstraints(dedupeLast([...sectionLines(pad[key]), ...agentConstraintLines(text)]))
|
|
72
|
+
: text;
|
|
73
|
+
}
|
|
74
|
+
pad.updatedAt = Date.now();
|
|
75
|
+
writeJson(padFile(cfg, address), pad);
|
|
76
|
+
return pad;
|
|
77
|
+
}
|
|
78
|
+
// The lines of a pad section; a non-string reads as none.
|
|
79
|
+
const sectionLines = (value) => (typeof value === "string" && value !== "" ? value.split("\n") : []);
|
|
80
|
+
// One pad line per entry: control characters and embedded newlines are gone before the line joins the section.
|
|
81
|
+
const cleanRunnerLine = (line) => stripControl(line).replace(/\s*\n\s*/g, " ").trim();
|
|
82
|
+
// Drops earlier copies so a repeated line sits at the end, where the cap keeps it.
|
|
83
|
+
const dedupeLast = (lines) => lines.filter((line, at) => lines.lastIndexOf(line) === at);
|
|
84
|
+
// The command a verification line carries: its part before the outcome separator.
|
|
85
|
+
const verifyKey = (line) => line.split(VERIFY_KEY)[0];
|
|
86
|
+
// Joins section lines under the cap; over it the oldest lines go first, so the newest survive.
|
|
87
|
+
function capLines(lines) {
|
|
88
|
+
const kept = lines.filter((line) => line !== "");
|
|
89
|
+
while (kept.length > 1 && kept.join("\n").length > PAD_SECTION_MAX)
|
|
90
|
+
kept.shift();
|
|
91
|
+
const text = kept.join("\n");
|
|
92
|
+
return text.length > PAD_SECTION_MAX ? text.slice(0, PAD_SECTION_MAX) : text;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Merges runner-derived lines into the session's changes and verification sections and writes the pad atomically
|
|
96
|
+
* (decision 3, piece 3). This is the runner's writer: it bypasses the agent refusal, dedupes lines, replaces the
|
|
97
|
+
* verification line of a repeated command, and keeps the newest lines when a section passes PAD_SECTION_MAX.
|
|
98
|
+
* A head with its stampedAt is stored beside the lines (piece 6): the worktree commit the pad was written at,
|
|
99
|
+
* so an injection can tell whether the pad went stale since. A write with only a stamp still writes.
|
|
100
|
+
*/
|
|
101
|
+
export function recordRunnerSections(cfg, address, { changes = [], verification = [], head = "", stampedAt = 0 }) {
|
|
102
|
+
if (!ADDRESS.test(address || ""))
|
|
103
|
+
throw new Error("invalid session address");
|
|
104
|
+
const pad = readPad(cfg, address);
|
|
105
|
+
if (!changes.length && !verification.length && !head)
|
|
106
|
+
return pad;
|
|
107
|
+
const next = pad ?? { v: 1, address, updatedAt: 0 };
|
|
108
|
+
if (changes.length)
|
|
109
|
+
next.changes = capLines(dedupeLast([...sectionLines(next.changes), ...changes.map(cleanRunnerLine)]));
|
|
110
|
+
if (verification.length) {
|
|
111
|
+
const fresh = verification.map(cleanRunnerLine);
|
|
112
|
+
const keys = new Set(fresh.map(verifyKey));
|
|
113
|
+
next.verification = capLines(dedupeLast([...sectionLines(next.verification).filter((line) => !keys.has(verifyKey(line))), ...fresh]));
|
|
114
|
+
}
|
|
115
|
+
if (head) {
|
|
116
|
+
next.head = head;
|
|
117
|
+
next.stampedAt = stampedAt;
|
|
118
|
+
}
|
|
119
|
+
next.updatedAt = Date.now();
|
|
120
|
+
writeJson(padFile(cfg, address), next);
|
|
121
|
+
return next;
|
|
122
|
+
}
|
|
123
|
+
// One stored constraint line: source tag and clean text, cut to CONSTRAINT_LINE_MAX.
|
|
124
|
+
function constraintLine(source, text) {
|
|
125
|
+
const line = cleanRunnerLine(`(${source}) ${text}`);
|
|
126
|
+
return line.length > CONSTRAINT_LINE_MAX ? `${line.slice(0, CONSTRAINT_LINE_MAX - 1)}…` : line;
|
|
127
|
+
}
|
|
128
|
+
// The agent's constraint lines: each becomes an (agent) line; a forged source tag is stripped before tagging.
|
|
129
|
+
function agentConstraintLines(text) {
|
|
130
|
+
return text.split("\n").map((line) => line.trim()).filter(Boolean).map((line) => constraintLine("agent", line.replace(CONSTRAINT_SOURCE, "")));
|
|
131
|
+
}
|
|
132
|
+
// The source a constraint line carries; an untagged line reads as the agent's.
|
|
133
|
+
const constraintSource = (line) => CONSTRAINT_SOURCE.exec(line)?.[1] ?? "agent";
|
|
134
|
+
// Joins constraint lines under the cap: the oldest operator lines go first, then the oldest agent lines; brief lines never.
|
|
135
|
+
function capConstraints(lines) {
|
|
136
|
+
const kept = lines.filter((line) => line !== "");
|
|
137
|
+
const dropOldest = (source) => {
|
|
138
|
+
const at = kept.findIndex((line) => constraintSource(line) === source);
|
|
139
|
+
if (at < 0)
|
|
140
|
+
return false;
|
|
141
|
+
kept.splice(at, 1);
|
|
142
|
+
return true;
|
|
143
|
+
};
|
|
144
|
+
while (kept.join("\n").length > PAD_SECTION_MAX && (dropOldest("operator") || dropOldest("agent"))) { /* one droppable line went */ }
|
|
145
|
+
const text = kept.join("\n");
|
|
146
|
+
return text.length > PAD_SECTION_MAX ? text.slice(0, PAD_SECTION_MAX) : text;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* The rule-like lines of a text (piece 4): each line or bullet carrying never, always, don't, do not, must not or only,
|
|
150
|
+
* case-insensitive. Bullet markers are stripped, long lines cut, repeats kept once (newest last).
|
|
151
|
+
*/
|
|
152
|
+
export function extractConstraints(text) {
|
|
153
|
+
const lines = String(text ?? "").split("\n")
|
|
154
|
+
.map((line) => line.replace(/^\s*(?:[-*•]|\d+[.)])\s+/, "").trim())
|
|
155
|
+
.filter((line) => line && RULE_LINE.test(line))
|
|
156
|
+
.map((line) => (line.length > CONSTRAINT_LINE_MAX ? `${line.slice(0, CONSTRAINT_LINE_MAX - 1)}…` : line));
|
|
157
|
+
return dedupeLast(lines);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* The runner's writer for the constraints section (piece 4): (brief) lines recorded once at start and (operator)
|
|
161
|
+
* lines as the operator's messages arrive. Lines are tagged and deduped; the cap drops the oldest operator lines
|
|
162
|
+
* first and never a brief line. The agent's own lines are merged by updatePad, never by this path.
|
|
163
|
+
*/
|
|
164
|
+
export function recordRunnerConstraints(cfg, address, { brief = [], operator = [] }) {
|
|
165
|
+
if (!ADDRESS.test(address || ""))
|
|
166
|
+
throw new Error("invalid session address");
|
|
167
|
+
const pad = readPad(cfg, address);
|
|
168
|
+
const fresh = [
|
|
169
|
+
...brief.filter((line) => line.trim()).map((line) => constraintLine("brief", line)),
|
|
170
|
+
...operator.filter((line) => line.trim()).map((line) => constraintLine("operator", line)),
|
|
171
|
+
];
|
|
172
|
+
if (!fresh.length)
|
|
173
|
+
return pad;
|
|
174
|
+
const next = pad ?? { v: 1, address, updatedAt: 0 };
|
|
175
|
+
next.constraints = capConstraints(dedupeLast([...sectionLines(next.constraints), ...fresh]));
|
|
176
|
+
next.updatedAt = Date.now();
|
|
177
|
+
writeJson(padFile(cfg, address), next);
|
|
178
|
+
return next;
|
|
179
|
+
}
|
|
180
|
+
/** The pad as plain fields for the tool result; every section present, "" when unset. */
|
|
181
|
+
export function padView(pad) {
|
|
182
|
+
const text = (key) => { const v = pad?.[key]; return typeof v === "string" ? v : ""; };
|
|
183
|
+
return {
|
|
184
|
+
address: pad?.address || "", updatedAt: pad?.updatedAt || 0,
|
|
185
|
+
constraints: text("constraints"), objective: text("objective"), facts: text("facts"), changes: text("changes"),
|
|
186
|
+
verification: text("verification"), dead_ends: text("dead_ends"), plan: text("plan"),
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
/** The pad as injection text: a header line and each non-empty section; "" when there is nothing to inject. */
|
|
190
|
+
export function padText(pad) {
|
|
191
|
+
const parts = PAD_SECTIONS.map((key) => {
|
|
192
|
+
const text = typeof pad?.[key] === "string" ? pad[key].trim() : "";
|
|
193
|
+
return text ? `## ${HEADINGS[key]}\n${text}` : "";
|
|
194
|
+
}).filter(Boolean);
|
|
195
|
+
if (!parts.length)
|
|
196
|
+
return "";
|
|
197
|
+
return ["[temmo] Your session pad: your own notes and the runner's record, kept across compaction, wake and restart. Trust it over your memory of the conversation.", ...parts].join("\n\n");
|
|
198
|
+
}
|