@uniqbit/mate-core 0.18.5 → 1.0.0-canary.2
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-capability-plugins/companion-mentions/.claude-plugin/marketplace.json +14 -0
- package/claude-capability-plugins/companion-mentions/.claude-plugin/plugin.json +8 -0
- package/claude-capability-plugins/companion-mentions/hooks/companion.ts +457 -0
- package/claude-capability-plugins/companion-mentions/hooks/hooks.json +1 -0
- package/claude-capability-plugins/companion-mentions/hooks/register.ts +403 -0
- package/claude-plugin/hooks/companion-instructions.mjs +9 -0
- package/claude-plugin/hooks/hooks.json +21 -2
- package/claude-plugin/hooks/tokensave-route.mjs +52 -0
- package/opencode-plugin/src/server.ts +4 -0
- package/opencode-plugin/src/tui.tsx +2 -0
- package/opencode-plugin/tui.tsx +2 -0
- package/package.json +52 -15
- package/src/brand/art.generated.ts +69 -0
- package/src/brand/index.ts +12 -0
- package/src/brand/palette.ts +19 -0
- package/src/brand/svg.generated.ts +8 -0
- package/src/brand/terminal.ts +88 -0
- package/src/brand/types.ts +33 -0
- package/src/cli/commands/artifact/apply-context.ts +275 -0
- package/src/cli/commands/artifact/artifact.ts +12 -0
- package/src/cli/commands/artifact/delivery/archive.ts +125 -0
- package/src/cli/commands/artifact/delivery/branch-flags.ts +55 -0
- package/src/cli/commands/artifact/delivery/environment.ts +127 -0
- package/src/cli/commands/artifact/delivery/link.ts +174 -0
- package/src/cli/commands/artifact/delivery/output.ts +89 -0
- package/src/cli/commands/artifact/finish/command.ts +83 -8
- package/src/cli/commands/artifact/finish/engine.ts +91 -14
- package/src/cli/commands/artifact/finish/finisher.ts +10 -0
- package/src/cli/commands/artifact/finish/git.ts +132 -19
- package/src/cli/commands/artifact/finish/openspec.ts +182 -138
- package/src/cli/commands/artifact/finish/secret-scan.ts +43 -0
- package/src/cli/commands/artifact/finish/worktree-cleanup.ts +92 -0
- package/src/cli/commands/artifact/pending/command.ts +31 -3
- package/src/cli/commands/artifact/pending/discovery.ts +21 -57
- package/src/cli/commands/cap/graphify.ts +52 -3
- package/src/cli/commands/cap/index-cmd.ts +71 -15
- package/src/cli/commands/cap/openspec.ts +13 -25
- package/src/cli/commands/cap/tokensave.ts +7 -5
- package/src/cli/commands/companion/link.ts +14 -1
- package/src/cli/commands/companion/open.ts +68 -46
- package/src/cli/commands/companion/sync.ts +10 -0
- package/src/cli/commands/companion/tui.ts +17 -7
- package/src/cli/commands/companion/update.ts +143 -48
- package/src/cli/commands/doctor.ts +195 -7
- package/src/cli/commands/factory/factory.ts +301 -0
- package/src/cli/commands/install.ts +133 -9
- package/src/cli/commands/launch/shared.ts +16 -11
- package/src/cli/commands/plugin/install.ts +9 -4
- package/src/cli/commands/plugin/plugin.ts +3 -2
- package/src/cli/commands/report/adapter.ts +11 -2
- package/src/cli/commands/report/contract.ts +21 -0
- package/src/cli/commands/report/highlight.ts +34 -4
- package/src/cli/commands/report/index.ts +20 -1
- package/src/cli/commands/report/markdown.ts +110 -0
- package/src/cli/commands/report/palette.ts +118 -0
- package/src/cli/commands/report/renderer.ts +456 -153
- package/src/cli/commands/report/types.ts +16 -0
- package/src/cli/commands/session/context.ts +102 -0
- package/src/cli/commands/setup.ts +50 -24
- package/src/cli/commands/studio/factory-credential.ts +49 -0
- package/src/cli/commands/studio/index.ts +29 -2
- package/src/cli/commands/studio/openspec-cli.ts +10 -58
- package/src/cli/commands/studio/payload.ts +249 -15
- package/src/cli/commands/studio/routes.ts +41 -6
- package/src/cli/commands/studio/selection.ts +161 -17
- package/src/cli/commands/studio/server.ts +404 -324
- package/src/cli/commands/studio/services.ts +60 -0
- package/src/cli/commands/studio/studio-app.ts +67 -0
- package/src/cli/commands/studio/terminal-assets.ts +21 -33
- package/src/cli/commands/studio/terminal.ts +85 -15
- package/src/cli/commands/studio/views/model.ts +18 -0
- package/src/cli/commands/studio/views/vault/markdown-tree.ts +267 -0
- package/src/cli/commands/studio/views/vault/view.ts +185 -0
- package/src/cli/commands/studio/workset-session.ts +131 -0
- package/src/cli/commands/unwrap.ts +8 -0
- package/src/cli/commands/update.ts +27 -1
- package/src/cli/commands/working/cleanup.ts +10 -1
- package/src/cli/commands/working/link.ts +76 -0
- package/src/cli/commands/working/working.ts +10 -0
- package/src/cli/commands/working/worktree.ts +356 -0
- package/src/cli/commands/wrap.ts +5 -13
- package/src/cli/companion-link-wizard.tsx +2 -0
- package/src/cli/companion-selector.tsx +2 -0
- package/src/cli/confirm.ts +2 -0
- package/src/cli/install-plan.tsx +127 -160
- package/src/cli/launch-selector.tsx +2 -0
- package/src/cli/main.ts +22 -1
- package/src/cli/parse-flags.ts +32 -6
- package/src/cli/repo-list-table.tsx +2 -0
- package/src/cli/setup-selector.tsx +86 -151
- package/src/cli/usage.ts +14 -0
- package/src/create-mate.ts +5 -4
- package/src/hooks/companion-instructions.ts +190 -0
- package/src/hooks/hook-io.ts +63 -0
- package/src/hooks/session-banner.ts +21 -29
- package/src/hooks/session-guidance.ts +9 -17
- package/src/hooks/session-prepare.ts +115 -28
- package/src/hooks/tokensave-route.ts +26 -0
- package/src/hooks/validate-artifact-path.ts +82 -24
- package/src/index.ts +4 -0
- package/src/lib/apply-context/fingerprint.ts +174 -0
- package/src/lib/apply-context/protocol.ts +193 -0
- package/src/lib/change-layout.ts +154 -0
- package/src/lib/components/confirm-prompt.tsx +21 -9
- package/src/lib/components/select-menu.tsx +10 -3
- package/src/lib/components/spinner.tsx +4 -2
- package/src/lib/components/startup-progress.tsx +227 -41
- package/src/lib/components/text-input-display.tsx +2 -0
- package/src/lib/components/wizard-footer.tsx +2 -0
- package/src/lib/components/wizard-header.tsx +16 -12
- package/src/lib/context-mode-package.ts +24 -11
- package/src/lib/delivery/archive-change.ts +166 -0
- package/src/lib/delivery/branches.ts +132 -0
- package/src/lib/delivery/environment.ts +35 -0
- package/src/lib/delivery/forge.ts +279 -0
- package/src/lib/delivery/merge-git.ts +261 -0
- package/src/lib/delivery/overlap.ts +209 -0
- package/src/lib/delivery/record-delivery.ts +283 -0
- package/src/lib/delivery/record.ts +403 -0
- package/src/lib/delivery/refresh.ts +268 -0
- package/src/lib/delivery/repository-access.ts +141 -0
- package/src/lib/delivery/resolve-branch.ts +162 -0
- package/src/lib/delivery/stages.ts +111 -0
- package/src/lib/delivery/token.ts +24 -0
- package/src/lib/factory/attach-client.ts +113 -0
- package/src/lib/factory/attach.ts +178 -0
- package/src/lib/factory/broker.ts +190 -0
- package/src/lib/factory/bun-commands.ts +39 -0
- package/src/lib/factory/client.ts +92 -0
- package/src/lib/factory/command.ts +61 -0
- package/src/lib/factory/credential-store.ts +153 -0
- package/src/lib/factory/frames.ts +77 -0
- package/src/lib/factory/ids.ts +58 -0
- package/src/lib/factory/keyed-serial.ts +27 -0
- package/src/lib/factory/outcomes/actions.ts +114 -0
- package/src/lib/factory/outcomes/index.ts +381 -0
- package/src/lib/factory/outcomes/lifecycle.ts +205 -0
- package/src/lib/factory/outcomes/runs.ts +81 -0
- package/src/lib/factory/outcomes/sessions.ts +38 -0
- package/src/lib/factory/prepare.ts +49 -0
- package/src/lib/factory/pty.ts +103 -0
- package/src/lib/factory/runner/companion-context.ts +178 -0
- package/src/lib/factory/runner/control.ts +92 -0
- package/src/lib/factory/runner/harness-agent.ts +250 -0
- package/src/lib/factory/runner/live.ts +159 -0
- package/src/lib/factory/runner/questions.ts +22 -0
- package/src/lib/factory/runner/rank.ts +26 -0
- package/src/lib/factory/runner/run-policy.ts +12 -0
- package/src/lib/factory/runner/run.ts +199 -0
- package/src/lib/factory/runner/scheduler.ts +416 -0
- package/src/lib/factory/runner/state.ts +233 -0
- package/src/lib/factory/runner/workflow.ts +542 -0
- package/src/lib/factory/runtime.ts +972 -0
- package/src/lib/factory/sandbox.ts +292 -0
- package/src/lib/factory/worktree-branch.ts +17 -0
- package/src/lib/frontmatter.ts +18 -0
- package/src/lib/install.ts +66 -14
- package/src/lib/live-progress.ts +36 -0
- package/src/lib/member-choices.ts +67 -0
- package/src/lib/opencode-plugin-package.ts +5 -5
- package/src/lib/openspec-cli.ts +116 -0
- package/src/lib/openspec-scopes.ts +86 -0
- package/src/lib/orchestrator/adapters/base.ts +55 -22
- package/src/lib/orchestrator/adapters/claude-args.ts +50 -0
- package/src/lib/orchestrator/adapters/claude.ts +54 -41
- package/src/lib/orchestrator/adapters/opencode.ts +2 -0
- package/src/lib/orchestrator/config-store.ts +3 -2
- package/src/lib/orchestrator/editor.ts +31 -25
- package/src/lib/orchestrator/openspec-store.ts +14 -54
- package/src/lib/orchestrator/projection-claude-commands.ts +36 -0
- package/src/lib/orchestrator/projection-claude-links.ts +159 -0
- package/src/lib/orchestrator/projection-claude-skills.ts +19 -137
- package/src/lib/orchestrator/projection-entries.ts +100 -34
- package/src/lib/orchestrator/projection-opencode-copies.ts +32 -15
- package/src/lib/orchestrator/projection-openspec-store.ts +54 -9
- package/src/lib/orchestrator/projection-runtime-documents.ts +15 -6
- package/src/lib/orchestrator/projection-types.ts +36 -1
- package/src/lib/orchestrator/refresh-working-claude-links.ts +87 -0
- package/src/lib/orchestrator/session-context-link.ts +67 -0
- package/src/lib/orchestrator/session-prepare.ts +45 -5
- package/src/lib/orchestrator/setup-compatibilities.ts +56 -70
- package/src/lib/orchestrator/types.ts +7 -2
- package/src/lib/orchestrator/working-repo-projection.ts +50 -7
- package/src/lib/orchestrator/workspace-document.ts +105 -0
- package/src/lib/orchestrator/wrap-pass.ts +22 -1
- package/src/lib/package-paths.ts +37 -0
- package/src/lib/runtime-directory-refresh.ts +17 -1
- package/src/lib/workset/record.ts +513 -0
- package/src/lib/workset/repositories.ts +55 -0
- package/src/lib/workset/stage.ts +148 -0
- package/src/lib/workset/stages.ts +113 -0
- package/src/lib/workset/worktree-index.ts +116 -0
- package/src/lib/workset/worktree.ts +830 -0
- package/src/opencode/companion-hooks.ts +109 -7
- package/src/opencode/companion-policy.ts +37 -18
- package/src/opencode/enter-worktree.ts +72 -0
- package/src/opencode/projected-guidance.ts +4 -1
- package/src/opencode/session-prepare.ts +2 -1
- package/src/opencode/tui.tsx +13 -6
- package/src/plugins.ts +1 -0
- package/src/runtime/artifact-refusal.ts +63 -0
- package/src/runtime/claude-delivery-plan.ts +107 -8
- package/src/runtime/commit-attribution-guard.ts +181 -0
- package/src/runtime/companion-git.ts +52 -10
- package/src/runtime/companion-guidance.ts +20 -8
- package/src/runtime/companion-instructions.ts +340 -0
- package/src/runtime/companion-lock.ts +30 -7
- package/src/runtime/env.ts +48 -3
- package/src/runtime/error-message.ts +4 -0
- package/src/runtime/git-layout.ts +219 -0
- package/src/runtime/index.ts +29 -0
- package/src/runtime/managed-block.ts +117 -0
- package/src/runtime/markdown-scan.ts +266 -0
- package/src/runtime/physical-path.ts +111 -0
- package/src/runtime/policy.ts +50 -2
- package/src/runtime/projected-flag.ts +7 -0
- package/src/runtime/projected-guidance.ts +1 -0
- package/src/runtime/runtime-directory.ts +5 -0
- package/src/runtime/runtime-document-manifest.ts +62 -11
- package/src/runtime/secret-guard.ts +162 -0
- package/src/runtime/secret-patterns.ts +397 -0
- package/src/runtime/secret-scan.ts +90 -0
- package/src/runtime/session-context.ts +98 -0
- package/src/runtime/session-kind.ts +13 -0
- package/src/runtime/shell-heredoc.ts +81 -0
- package/src/runtime/tokensave-route.ts +152 -0
- package/src/runtime/wrap-state.ts +44 -2
- package/src/runtime/write-atomic.ts +64 -0
- package/src/studio.ts +124 -0
- package/src/templates/capabilities/openspec-cap/mate-minimal/schema.yaml +14 -10
- package/src/templates/capabilities/openspec-cap/mate-minimal/templates/spec.md +3 -5
- package/src/templates/capabilities/openspec-cap/mate-v1/schema.yaml +18 -102
- package/src/templates/capabilities/openspec-cap/mate-v1/templates/spec.md +3 -5
- package/src/templates/capabilities/openspec-cap/openspec-conventions.yaml +20 -14
- package/src/templates/mate-skills/THIRD-PARTY-NOTICES.md +40 -0
- package/src/templates/mate-skills/agents/mate-apply-change/SKILL.md +134 -0
- package/src/templates/mate-skills/agents/mate-apply-change/references/mate-compact-apply.md +47 -0
- package/src/templates/mate-skills/agents/mate-apply-change/references/openspec-apply-change.md +200 -0
- package/src/templates/mate-skills/agents/mate-archive-change/SKILL.md +86 -0
- package/src/templates/mate-skills/{claude/mate-openspec-backfill → agents/mate-backfill-spec}/SKILL.md +11 -4
- package/src/templates/mate-skills/agents/mate-clarify-intent/SKILL.md +110 -0
- package/src/templates/mate-skills/agents/mate-explore-change/SKILL.md +66 -0
- package/src/templates/mate-skills/agents/mate-explore-change/references/openspec-explore.md +350 -0
- package/src/templates/mate-skills/agents/mate-grill-me/SKILL.md +11 -4
- package/src/templates/mate-skills/agents/mate-grill-with-docs/SKILL.md +2 -2
- package/src/templates/mate-skills/agents/mate-interview-me/SKILL.md +12 -5
- package/src/templates/mate-skills/agents/mate-propose-change/SKILL.md +108 -0
- package/src/templates/mate-skills/agents/mate-propose-change/references/mate-revise.md +47 -0
- package/src/templates/mate-skills/agents/mate-propose-change/references/openspec-propose.md +169 -0
- package/src/templates/mate-skills/agents/mate-propose-change/references/openspec-update-change.md +103 -0
- package/src/templates/mate-skills/agents/{mate-artifact-publish → mate-publish-artifact}/SKILL.md +111 -24
- package/src/templates/mate-skills/{claude/mate-artifact-publish → agents/mate-publish-artifact}/references/openspec.md +8 -5
- package/src/templates/mate-skills/agents/mate-show-me/SKILL.md +95 -16
- package/src/templates/mate-skills/agents/mate-simplify-code/SKILL.md +87 -68
- package/src/templates/mate-skills/claude/mate-apply-change/SKILL.md +134 -0
- package/src/templates/mate-skills/claude/mate-apply-change/references/mate-compact-apply.md +47 -0
- package/src/templates/mate-skills/claude/mate-apply-change/references/openspec-apply-change.md +200 -0
- package/src/templates/mate-skills/claude/mate-archive-change/SKILL.md +86 -0
- package/src/templates/mate-skills/{agents/mate-openspec-backfill → claude/mate-backfill-spec}/SKILL.md +11 -4
- package/src/templates/mate-skills/claude/mate-clarify-intent/SKILL.md +110 -0
- package/src/templates/mate-skills/claude/mate-explore-change/SKILL.md +66 -0
- package/src/templates/mate-skills/claude/mate-explore-change/references/openspec-explore.md +350 -0
- package/src/templates/mate-skills/claude/mate-grill-me/SKILL.md +11 -4
- package/src/templates/mate-skills/claude/mate-grill-with-docs/SKILL.md +2 -2
- package/src/templates/mate-skills/claude/mate-interview-me/SKILL.md +12 -5
- package/src/templates/mate-skills/claude/mate-propose-change/SKILL.md +108 -0
- package/src/templates/mate-skills/claude/mate-propose-change/references/mate-revise.md +47 -0
- package/src/templates/mate-skills/claude/mate-propose-change/references/openspec-propose.md +169 -0
- package/src/templates/mate-skills/claude/mate-propose-change/references/openspec-update-change.md +103 -0
- package/src/templates/mate-skills/claude/{mate-artifact-publish → mate-publish-artifact}/SKILL.md +111 -24
- package/src/templates/mate-skills/{agents/mate-artifact-publish → claude/mate-publish-artifact}/references/openspec.md +8 -5
- package/src/templates/mate-skills/claude/mate-show-me/SKILL.md +95 -16
- package/src/templates/mate-skills/claude/mate-simplify-code/SKILL.md +87 -68
- package/src/templates/mate-skills/openspec-references.ts +142 -0
- package/src/templates/root/TEMPLATE_AGENTS.md +2 -2
- package/src/tools/setup/__snapshots__/runtime-surface-golden.test.ts.snap +3938 -391
- package/src/tools/setup/capabilities/companion-mentions.ts +27 -0
- package/src/tools/setup/capabilities/context-mode.ts +56 -7
- package/src/tools/setup/capabilities/graphify-gate.ts +31 -0
- package/src/tools/setup/capabilities/graphify-secrets.ts +243 -0
- package/src/tools/setup/capabilities/graphify.ts +46 -7
- package/src/tools/setup/capabilities/openspec.ts +163 -272
- package/src/tools/setup/capabilities/react-doctor.ts +8 -1
- package/src/tools/setup/capabilities/rtk.ts +4 -0
- package/src/tools/setup/capabilities/tokensave.ts +113 -71
- package/src/tools/setup/context-services.ts +9 -17
- package/src/tools/setup/dynamic-plugins/install.ts +24 -28
- package/src/tools/setup/engine.ts +100 -23
- package/src/tools/setup/install-contract.ts +8 -0
- package/src/tools/setup/managed-output.ts +51 -0
- package/src/tools/setup/mate-skill-copies.ts +51 -0
- package/src/tools/setup/mate.ts +86 -11
- package/src/tools/setup/package-managers/uv.ts +73 -11
- package/src/tools/setup/plugin.ts +40 -0
- package/src/tools/setup/plugins/guidance.ts +7 -4
- package/src/tools/setup/providers/agent-file-sections.ts +4 -2
- package/src/tools/setup/providers/claude-attribution.ts +65 -0
- package/src/tools/setup/providers/claude-plugin-folders.ts +625 -0
- package/src/tools/setup/providers/claude-plugin-hooks.ts +51 -19
- package/src/tools/setup/providers/claude-worktree-plugins.ts +122 -0
- package/src/tools/setup/providers/claude.ts +290 -8
- package/src/tools/setup/providers/opencode.ts +180 -1
- package/src/tools/setup/utils.ts +18 -4
- package/src/tools/setup/working-repo-cleanup.ts +29 -1
- package/src/tools/setup/working-repo-local-state.ts +252 -75
- package/src/tools/setup.ts +183 -26
- package/studio-dist/client/assets/companion-view-Csva26Ir.js +1 -0
- package/studio-dist/client/assets/delivery-path-DJMpRmiY.js +1 -0
- package/studio-dist/client/assets/dist-Dm7MtyHc.js +5 -0
- package/studio-dist/client/assets/factory-B7l9ZQyx.js +4 -0
- package/studio-dist/client/assets/index-n6uo_92S.js +40 -0
- package/studio-dist/client/assets/preload-helper-BLdHbfdN.js +1 -0
- package/studio-dist/client/assets/prepaint-CD5OpmTM.js +24 -0
- package/studio-dist/client/assets/queries-CN3JwMBX.js +1 -0
- package/studio-dist/client/assets/react-CabXf2Ds.js +1 -0
- package/studio-dist/client/assets/report-time-Dc-KOqQu.js +1 -0
- package/studio-dist/client/assets/reports-C01RsnMA.js +1 -0
- package/studio-dist/client/assets/rolldown-runtime-WkwZ5CU-.js +1 -0
- package/studio-dist/client/assets/routes-4pbC-2Hj.js +1 -0
- package/studio-dist/client/assets/skills-CihQganl.js +1 -0
- package/studio-dist/client/assets/spec-areas-C2L48N4C.js +1 -0
- package/studio-dist/client/assets/specs-D24_35_K.js +1 -0
- package/studio-dist/client/assets/studio-DKxfi691.css +1 -0
- package/studio-dist/client/assets/studio-queries-rvbJupCy.js +3 -0
- package/studio-dist/client/assets/terminal-eU5fzQzv.css +1 -0
- package/studio-dist/client/assets/vault-Bd4CQiTK.js +1 -0
- package/studio-dist/client/assets/warnings-V_Ys51wJ.js +1 -0
- package/studio-dist/client/assets/wasm-inline-C2en61B5.js +1 -0
- package/studio-dist/client/assets/workflow-BJejeVwQ.js +1 -0
- package/studio-dist/server/assets/__23tanstack-start-server-fn-resolver-BQKuMdJW.js +3881 -0
- package/studio-dist/server/assets/_tanstack-start-manifest_v.js +85 -0
- package/studio-dist/server/assets/actions-Bvgj9eb8.js +54 -0
- package/studio-dist/server/assets/actions-D8erOphH.js +44 -0
- package/studio-dist/server/assets/companion-view-aBady57v.js +33 -0
- package/studio-dist/server/assets/createServerFn-F5sAOF8V.js +459 -0
- package/studio-dist/server/assets/createServerRpc-tK2lE9R2.js +11 -0
- package/studio-dist/server/assets/delivery-path-BkKNjgkf.js +778 -0
- package/studio-dist/server/assets/dist-DpJWKyCm.js +5582 -0
- package/studio-dist/server/assets/empty-plugin-adapters-Cipd4ZH4.js +4 -0
- package/studio-dist/server/assets/factory-D6xnjedJ.js +2385 -0
- package/studio-dist/server/assets/factory-DT3UzHlh.js +68 -0
- package/studio-dist/server/assets/model-PyPALjii.js +18 -0
- package/studio-dist/server/assets/prepaint-BMke0fo1.js +41 -0
- package/studio-dist/server/assets/queries-Ag2iL8q2.js +107 -0
- package/studio-dist/server/assets/react-COs0LHn-.js +51 -0
- package/studio-dist/server/assets/report-time-DAfzln-N.js +8 -0
- package/studio-dist/server/assets/reports-BnUNJVtx.js +126 -0
- package/studio-dist/server/assets/rolldown-runtime-DYf_mQCB.js +34 -0
- package/studio-dist/server/assets/router-BH--drIx.js +4298 -0
- package/studio-dist/server/assets/routes-khSDK1bV.js +361 -0
- package/studio-dist/server/assets/search-JAKurDNh.js +36 -0
- package/studio-dist/server/assets/shell-CboT2f66.js +51 -0
- package/studio-dist/server/assets/skills-MWYnWoKs.js +106 -0
- package/studio-dist/server/assets/spec-areas-DcnO6vW-.js +16 -0
- package/studio-dist/server/assets/specs-DE3w8hZP.js +155 -0
- package/studio-dist/server/assets/start-BW5ovN0k.js +33 -0
- package/studio-dist/server/assets/studio-BeltxxKt.js +216 -0
- package/studio-dist/server/assets/studio-queries-CLnP9kGu.js +3365 -0
- package/studio-dist/server/assets/utils-C9CX1kcy.js +221 -0
- package/studio-dist/server/assets/vault-BGulVoNh.js +70 -0
- package/studio-dist/server/assets/vault-CuyyhLyB.js +832 -0
- package/studio-dist/server/assets/views-Bd7cRr27.js +82 -0
- package/studio-dist/server/assets/warnings-BH05rOuG.js +15 -0
- package/studio-dist/server/assets/wasm-inline-CNeBKGwC.js +3 -0
- package/studio-dist/server/assets/workflow-BSuIKWqH.js +42 -0
- package/studio-dist/server/server.js +16220 -0
- package/wrappers/bin/graphify +52 -4
- package/wrappers/bin/openspec +33 -9
- package/wrappers/lib/graphify-gate.mjs +8 -0
- package/src/cli/commands/studio/views/client.ts +0 -654
- package/src/cli/commands/studio/views/companion-picker.tsx +0 -95
- package/src/cli/commands/studio/views/companion-selector.tsx +0 -94
- package/src/cli/commands/studio/views/dashboard/changes.tsx +0 -107
- package/src/cli/commands/studio/views/dashboard/index.tsx +0 -19
- package/src/cli/commands/studio/views/document.tsx +0 -842
- package/src/cli/commands/studio/views/error.tsx +0 -20
- package/src/cli/commands/studio/views/reports/index.tsx +0 -49
- package/src/cli/commands/studio/views/selection-fields.tsx +0 -14
- package/src/cli/commands/studio/views/skills/index.tsx +0 -87
- package/src/cli/commands/studio/views/specs/index.tsx +0 -157
- package/src/cli/commands/studio/views/styles.ts +0 -519
- package/src/cli/commands/studio/views/terminal.tsx +0 -484
- package/src/cli/commands/studio/views/vault/state.ts +0 -43
- package/src/cli/commands/studio/views/warnings.tsx +0 -21
- package/src/cli/commands/studio/views/workflow/index.tsx +0 -21
- package/src/cli/commands/studio/views/workflow/steps.ts +0 -337
- package/src/cli/commands/studio/views/workflow/transcript.tsx +0 -190
- package/src/lib/components/theme.ts +0 -2
- package/src/tools/setup/git-utils.ts +0 -30
- package/src/tools/setup/runtime-documents.ts +0 -291
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
import { activeChanges, changesDirOf } from "../../change-layout";
|
|
4
|
+
import { findWorkset, type WorksetRecord } from "../../workset/record";
|
|
5
|
+
import { FactoryError } from "../ids";
|
|
6
|
+
import { KeyedSerial } from "../keyed-serial";
|
|
7
|
+
import { LiveTurns } from "./live";
|
|
8
|
+
import type { Baseline } from "./control";
|
|
9
|
+
import { compareRanked } from "./rank";
|
|
10
|
+
import type { Run } from "./run";
|
|
11
|
+
import type { RunStage } from "./state";
|
|
12
|
+
import {
|
|
13
|
+
BUILD_STAGES,
|
|
14
|
+
linkCreatedChanges,
|
|
15
|
+
PROMPTS,
|
|
16
|
+
RETURN_NOTE,
|
|
17
|
+
type StageOutcome,
|
|
18
|
+
StageError,
|
|
19
|
+
STAGES,
|
|
20
|
+
taskCounts,
|
|
21
|
+
type WorkflowDeps,
|
|
22
|
+
} from "./workflow";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Runs Worksets' agent stages, a bounded number at once and the rest by their Workset's
|
|
26
|
+
* rank (see `rank.ts`); Later Outcomes' requests go last, in request order.
|
|
27
|
+
* Every stage has a time budget; cancelling or exceeding it aborts the turn, which
|
|
28
|
+
* closes the harness bridge's attach so the broker stops the agent in its container.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
export type RunnableStage = Exclude<RunStage, "archive">;
|
|
32
|
+
|
|
33
|
+
const PICKABLE = {
|
|
34
|
+
"clarify-intent": "clarify",
|
|
35
|
+
explore: "explore",
|
|
36
|
+
ready: "propose",
|
|
37
|
+
} as const satisfies Record<WorksetRecord["stage"], RunnableStage & keyof typeof PROMPTS>;
|
|
38
|
+
|
|
39
|
+
/** The stage the runner starts by itself for a started Outcome, if any. */
|
|
40
|
+
export function pickableStage(
|
|
41
|
+
record: WorksetRecord,
|
|
42
|
+
): (typeof PICKABLE)[WorksetRecord["stage"]] | null {
|
|
43
|
+
if (!record.queuedAt || record.changes.length > 0) return null;
|
|
44
|
+
return PICKABLE[record.stage];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface RunnerOptions {
|
|
48
|
+
runs: Run;
|
|
49
|
+
maxRuns: number;
|
|
50
|
+
/** How many of the running stages may be builds; `maxRuns` when absent. */
|
|
51
|
+
maxBuilds?: number;
|
|
52
|
+
stageMs: number;
|
|
53
|
+
/** Everything a stage needs except its abort signal and log. */
|
|
54
|
+
depsFor: (id: string) => Omit<WorkflowDeps, "signal" | "log">;
|
|
55
|
+
/** The Workset's rank when its request is queued; `null` while Later. Defaults to all Later. */
|
|
56
|
+
rankFor?: (id: string) => Promise<number | null>;
|
|
57
|
+
/** The Workset's identity for prompts the runner builds itself. */
|
|
58
|
+
recordFor?: (id: string) => Promise<Pick<WorksetRecord, "id" | "title"> | undefined>;
|
|
59
|
+
/** Whether a person works on the Workset in a live session; pickup and Return to automation wait for it. */
|
|
60
|
+
sessionLive?: (id: string) => boolean;
|
|
61
|
+
/** Told once a Workset's run has ended, whatever its outcome; pickup hangs off this. */
|
|
62
|
+
onSettled?: (id: string) => void;
|
|
63
|
+
setTimer?: (callback: () => void, ms: number) => unknown;
|
|
64
|
+
clearTimer?: (handle: unknown) => void;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** How often runs waiting for a Workset container slot try again, besides settles and session ends. */
|
|
68
|
+
export const SLOT_RETRY_MS = 30_000;
|
|
69
|
+
|
|
70
|
+
function isSlotBusy(error: unknown): boolean {
|
|
71
|
+
return error instanceof FactoryError && error.code === "busy";
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Why a stage failed, as its run records it. */
|
|
75
|
+
function failureReason(signal: AbortSignal, error: unknown): string {
|
|
76
|
+
if (signal.aborted) return (signal.reason as Error)?.message ?? "aborted";
|
|
77
|
+
if (error instanceof StageError) return error.message;
|
|
78
|
+
return `the stage failed: ${(error as Error).message}`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
interface Request {
|
|
82
|
+
id: string;
|
|
83
|
+
stage: RunnableStage;
|
|
84
|
+
/** A person's answer, sent as the first turn before the stage continues. */
|
|
85
|
+
answer?: string;
|
|
86
|
+
/** A person's feedback for a revise stage, which builds its own prompt from it. */
|
|
87
|
+
feedback?: string;
|
|
88
|
+
rank: number | null;
|
|
89
|
+
/** Refused a Workset container slot; skipped until `retrySlots`. */
|
|
90
|
+
slotWait?: boolean;
|
|
91
|
+
/** Recorded as waiting for a build slot, so the wait is written once. */
|
|
92
|
+
buildWait?: boolean;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export class FactoryRunner {
|
|
96
|
+
private readonly queue: Request[] = [];
|
|
97
|
+
private readonly active = new Map<string, AbortController>();
|
|
98
|
+
/** The active runs whose stage is a build. */
|
|
99
|
+
private readonly building = new Set<string>();
|
|
100
|
+
/** Build-slot waits being recorded; a run starting meanwhile writes its state after them. */
|
|
101
|
+
private readonly waitWrites = new Map<string, Promise<void>>();
|
|
102
|
+
private readonly settled = new Map<string, Promise<void>>();
|
|
103
|
+
/** Each Workset's tasks run after every earlier one has settled. */
|
|
104
|
+
private readonly locks = new KeyedSerial();
|
|
105
|
+
private slotRetry: unknown = null;
|
|
106
|
+
/** What running turns show, for people following a run. */
|
|
107
|
+
readonly live: LiveTurns;
|
|
108
|
+
|
|
109
|
+
constructor(private readonly options: RunnerOptions) {
|
|
110
|
+
this.live = new LiveTurns(options.runs);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
isActive(id: string): boolean {
|
|
114
|
+
return this.active.has(id) || this.queue.some((request) => request.id === id);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Resolves once the Workset's current run has ended; for tests and shutdown. */
|
|
118
|
+
async idle(id: string): Promise<void> {
|
|
119
|
+
if (!this.isActive(id)) return;
|
|
120
|
+
await (this.settled.get(id) ?? new Promise((resolve) => setTimeout(resolve, 5)));
|
|
121
|
+
return this.idle(id);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async request(
|
|
125
|
+
id: string,
|
|
126
|
+
stage: RunnableStage,
|
|
127
|
+
answer?: string,
|
|
128
|
+
feedback?: string,
|
|
129
|
+
): Promise<void> {
|
|
130
|
+
if (this.isActive(id)) throw new StageError(`Workset ${id} already has a run`);
|
|
131
|
+
await this.options.runs.control.requireAutomatic(id);
|
|
132
|
+
const rank = (await this.options.rankFor?.(id)) ?? null;
|
|
133
|
+
// The rank lookup awaited; another request may have won meanwhile.
|
|
134
|
+
if (this.isActive(id)) throw new StageError(`Workset ${id} already has a run`);
|
|
135
|
+
this.queue.push({
|
|
136
|
+
id,
|
|
137
|
+
stage,
|
|
138
|
+
rank,
|
|
139
|
+
...(answer === undefined ? {} : { answer }),
|
|
140
|
+
...(feedback === undefined ? {} : { feedback }),
|
|
141
|
+
});
|
|
142
|
+
await this.options.runs.queue(id, stage);
|
|
143
|
+
this.pump();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Answers to the waiting questions, in their order; one answer for one question. With
|
|
148
|
+
* `round`, only an answer to that round counts: card and terminal answers race here, and
|
|
149
|
+
* the second is told the question was already answered.
|
|
150
|
+
*/
|
|
151
|
+
answer(id: string, answers: string | string[], round?: string): Promise<void> {
|
|
152
|
+
return this.locks.run(id, async () => {
|
|
153
|
+
const { stage, text } = await this.options.runs.prepareAnswer(id, answers, round);
|
|
154
|
+
await this.request(id, stage, text);
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* A person takes over the Outcome's conversation: no further stage starts, so a queued one
|
|
160
|
+
* is dropped, and a running one finishes first; until then control is requested. Never
|
|
161
|
+
* cancels work.
|
|
162
|
+
*/
|
|
163
|
+
takeControl(id: string): Promise<void> {
|
|
164
|
+
return this.locks.run(id, async () => {
|
|
165
|
+
const { control } = this.options.runs;
|
|
166
|
+
if (await control.withPerson(id)) return;
|
|
167
|
+
await this.dequeue(id);
|
|
168
|
+
await control.take(id, this.active.has(id), this.baseline(id));
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Starts the stage pickup chose unless the Outcome is busy, not idle or with a person; true
|
|
174
|
+
* when it was requested. Under the Workset's lock, so it never races Return to automation.
|
|
175
|
+
*/
|
|
176
|
+
pickUp(id: string, stage: RunnableStage): Promise<boolean> {
|
|
177
|
+
return this.locks.run(id, async () => {
|
|
178
|
+
if (this.isActive(id) || this.options.sessionLive?.(id)) return false;
|
|
179
|
+
const { execution, control } = await this.options.runs.facts(id);
|
|
180
|
+
// A person has the conversation; only Return to automation continues it.
|
|
181
|
+
if (execution.state !== "idle" || control.mode !== "automatic") return false;
|
|
182
|
+
await this.request(id, stage);
|
|
183
|
+
return true;
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Return to automation, refused while the Outcome's session is live: links the changes the
|
|
189
|
+
* person proposed, logs task progress and hands control back, then starts the stage the
|
|
190
|
+
* factory would start by itself, told that a person worked in the conversation. One step
|
|
191
|
+
* under the Workset's lock, so no pickup starts that stage in between. A waiting question
|
|
192
|
+
* stays waiting; failed and later stages wait for a person. On failure the person keeps
|
|
193
|
+
* control, with the reason recorded.
|
|
194
|
+
*/
|
|
195
|
+
returnToAutomation(id: string): Promise<void> {
|
|
196
|
+
return this.locks.run(id, async () => {
|
|
197
|
+
if (this.options.sessionLive?.(id)) {
|
|
198
|
+
throw new StageError(
|
|
199
|
+
"exit Claude in the Outcome's terminal first, then return to automation",
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
const { runs } = this.options;
|
|
203
|
+
const { companionPath } = this.options.depsFor(id);
|
|
204
|
+
const log = (text: string) => void this.live.log(id, text);
|
|
205
|
+
const returned = await runs.control.giveBack(id, async (changesBefore) => {
|
|
206
|
+
log("\nReturned to automation.\n");
|
|
207
|
+
await linkCreatedChanges({ companionPath, log }, id, changesBefore);
|
|
208
|
+
const record = await findWorkset(companionPath, id);
|
|
209
|
+
const changes = record?.changes ?? [];
|
|
210
|
+
const counts = await Promise.all(
|
|
211
|
+
changes.map((change) => taskCounts(path.join(changesDirOf(companionPath), change))),
|
|
212
|
+
);
|
|
213
|
+
counts.forEach(({ open, done }, index) => {
|
|
214
|
+
if (open + done > 0) log(`${changes[index]}: ${done} of ${open + done} tasks done\n`);
|
|
215
|
+
});
|
|
216
|
+
await this.live.flushed(id);
|
|
217
|
+
});
|
|
218
|
+
if (!returned || this.isActive(id)) return;
|
|
219
|
+
if ((await runs.facts(id)).execution.state !== "idle") return;
|
|
220
|
+
const record = await findWorkset(companionPath, id);
|
|
221
|
+
const stage = record ? pickableStage(record) : null;
|
|
222
|
+
if (!record || !stage) return;
|
|
223
|
+
await this.request(id, stage, `${RETURN_NOTE}\n\n${PROMPTS[stage](record)}`);
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** The companion's active changes, the baseline of what a person in control adds. */
|
|
228
|
+
private baseline(id: string): Baseline {
|
|
229
|
+
return () => activeChanges(this.options.depsFor(id).companionPath);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* A person's feedback on the Outcome: revise its active changes, or, once they are archived,
|
|
234
|
+
* propose a follow-up change in the same conversation.
|
|
235
|
+
*/
|
|
236
|
+
async feedback(id: string, text: string, followUp = false): Promise<void> {
|
|
237
|
+
if (text.trim() === "") throw new StageError("feedback cannot be empty");
|
|
238
|
+
if (!followUp) return this.request(id, "revise", undefined, text);
|
|
239
|
+
const record = await this.options.recordFor?.(id);
|
|
240
|
+
if (!record) throw new StageError(`Workset ${id} has no valid record`);
|
|
241
|
+
await this.request(id, "propose", PROMPTS.followUp(record, text));
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
async retry(id: string): Promise<void> {
|
|
245
|
+
await this.request(id, await this.options.runs.retryStage(id));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Re-reads a queued request's rank after its Outcome was started. */
|
|
249
|
+
async rerank(id: string): Promise<void> {
|
|
250
|
+
const request = this.queue.find((entry) => entry.id === id);
|
|
251
|
+
if (request) request.rank = (await this.options.rankFor?.(id)) ?? null;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Drops a queued request, leaving the Workset idle; a running one is left to finish. */
|
|
255
|
+
async dequeue(id: string): Promise<boolean> {
|
|
256
|
+
const queued = this.queue.findIndex((request) => request.id === id);
|
|
257
|
+
if (queued === -1) return false;
|
|
258
|
+
this.queue.splice(queued, 1);
|
|
259
|
+
await this.options.runs.settle(id);
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async cancel(id: string): Promise<void> {
|
|
264
|
+
const queued = this.queue.findIndex((request) => request.id === id);
|
|
265
|
+
if (queued !== -1) {
|
|
266
|
+
const [request] = this.queue.splice(queued, 1);
|
|
267
|
+
await this.options.runs.fail(id, request!.stage, "cancelled");
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
const controller = this.active.get(id);
|
|
271
|
+
if (!controller) throw new StageError(`Workset ${id} has no run to cancel`);
|
|
272
|
+
controller.abort(new StageError("cancelled"));
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Runs waiting for a Workset container slot try again; called when a run settles, a
|
|
277
|
+
* session ends, and on a timer while any waits.
|
|
278
|
+
*/
|
|
279
|
+
retrySlots(except?: string): void {
|
|
280
|
+
for (const request of this.queue) {
|
|
281
|
+
if (request.id !== except) request.slotWait = false;
|
|
282
|
+
}
|
|
283
|
+
this.pump();
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
private setTimer(callback: () => void, ms: number): unknown {
|
|
287
|
+
return (this.options.setTimer ?? setTimeout)(callback, ms);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
private scheduleSlotRetry(): void {
|
|
291
|
+
if (this.slotRetry !== null) return;
|
|
292
|
+
this.slotRetry = this.setTimer(() => {
|
|
293
|
+
this.slotRetry = null;
|
|
294
|
+
this.retrySlots();
|
|
295
|
+
if (this.queue.some((request) => request.slotWait)) this.scheduleSlotRetry();
|
|
296
|
+
}, SLOT_RETRY_MS);
|
|
297
|
+
(this.slotRetry as { unref?: () => void }).unref?.();
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
private pump(): void {
|
|
301
|
+
const maxBuilds = this.options.maxBuilds ?? this.options.maxRuns;
|
|
302
|
+
while (this.active.size < this.options.maxRuns) {
|
|
303
|
+
const buildsFull = this.building.size >= maxBuilds;
|
|
304
|
+
// Stable: equal ranks and Notes keep their request order.
|
|
305
|
+
let next = -1;
|
|
306
|
+
for (let index = 0; index < this.queue.length; index += 1) {
|
|
307
|
+
const candidate = this.queue[index]!;
|
|
308
|
+
if (candidate.slotWait) continue;
|
|
309
|
+
if (buildsFull && BUILD_STAGES.has(candidate.stage)) {
|
|
310
|
+
this.markBuildWait(candidate);
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
if (next === -1 || compareRanked(candidate, this.queue[next]!) < 0) next = index;
|
|
314
|
+
}
|
|
315
|
+
if (next === -1) return;
|
|
316
|
+
const [request] = this.queue.splice(next, 1) as [Request];
|
|
317
|
+
const controller = new AbortController();
|
|
318
|
+
this.active.set(request.id, controller);
|
|
319
|
+
if (BUILD_STAGES.has(request.stage)) this.building.add(request.id);
|
|
320
|
+
this.settled.set(
|
|
321
|
+
request.id,
|
|
322
|
+
this.execute(request, controller)
|
|
323
|
+
.then((requeued) => (requeued ? undefined : this.handOver(request.id)))
|
|
324
|
+
.catch(() => {})
|
|
325
|
+
.finally(() => {
|
|
326
|
+
this.active.delete(request.id);
|
|
327
|
+
this.building.delete(request.id);
|
|
328
|
+
this.settled.delete(request.id);
|
|
329
|
+
this.live.publish(request.id, { type: "settled" });
|
|
330
|
+
// A settled run may have freed a container; the one just refused waits on.
|
|
331
|
+
this.retrySlots(request.id);
|
|
332
|
+
this.options.onSettled?.(request.id);
|
|
333
|
+
}),
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** Records once that a queued build waits for a build slot; it keeps its place. */
|
|
339
|
+
private markBuildWait(request: Request): void {
|
|
340
|
+
if (request.buildWait) return;
|
|
341
|
+
request.buildWait = true;
|
|
342
|
+
const written = this.options.runs
|
|
343
|
+
.queue(request.id, request.stage, "build-slot")
|
|
344
|
+
.then(() => this.live.log(request.id, "\nwaiting for a build slot\n"))
|
|
345
|
+
.catch(() => {});
|
|
346
|
+
this.waitWrites.set(request.id, written);
|
|
347
|
+
void written.then(() => {
|
|
348
|
+
if (this.waitWrites.get(request.id) === written) this.waitWrites.delete(request.id);
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** The stage a takeover waited on has ended: the person has control now. */
|
|
353
|
+
private handOver(id: string): Promise<void> {
|
|
354
|
+
return this.locks.run(id, () => this.options.runs.control.stageEnded(id, this.baseline(id)));
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** True when the run went back to the queue to wait for a Workset container slot. */
|
|
358
|
+
private async execute(request: Request, controller: AbortController): Promise<boolean> {
|
|
359
|
+
const { runs } = this.options;
|
|
360
|
+
await this.waitWrites.get(request.id);
|
|
361
|
+
const clearTimer =
|
|
362
|
+
this.options.clearTimer ??
|
|
363
|
+
((handle) => clearTimeout(handle as ReturnType<typeof setTimeout>));
|
|
364
|
+
const minutes = Math.round(this.options.stageMs / 60_000);
|
|
365
|
+
const budget = this.setTimer(
|
|
366
|
+
() => controller.abort(new StageError(`the stage exceeded its budget of ${minutes} minutes`)),
|
|
367
|
+
this.options.stageMs,
|
|
368
|
+
);
|
|
369
|
+
await runs.start(request.id, request.stage);
|
|
370
|
+
const deps: WorkflowDeps = {
|
|
371
|
+
...this.options.depsFor(request.id),
|
|
372
|
+
stage: request.stage,
|
|
373
|
+
signal: controller.signal,
|
|
374
|
+
log: (text, streamed) => void this.live.log(request.id, text, streamed),
|
|
375
|
+
onEvent: (event) => this.live.publish(request.id, event),
|
|
376
|
+
onPushed: async (branch) => {
|
|
377
|
+
await runs.rememberPush(request.id, branch);
|
|
378
|
+
},
|
|
379
|
+
};
|
|
380
|
+
try {
|
|
381
|
+
// The stage runs the answer as its first turn, so it can count what that turn did.
|
|
382
|
+
const outcome: StageOutcome = await STAGES[request.stage](
|
|
383
|
+
deps,
|
|
384
|
+
request.id,
|
|
385
|
+
request.answer,
|
|
386
|
+
request.feedback,
|
|
387
|
+
);
|
|
388
|
+
if (outcome.kind === "question")
|
|
389
|
+
await runs.wait(request.id, request.stage, outcome.questions);
|
|
390
|
+
else await runs.settle(request.id);
|
|
391
|
+
return false;
|
|
392
|
+
} catch (error) {
|
|
393
|
+
if (!controller.signal.aborted && isSlotBusy(error)) {
|
|
394
|
+
// No turn ran, so a requested takeover hands over now instead of waiting for a slot.
|
|
395
|
+
if (await runs.control.withPerson(request.id)) {
|
|
396
|
+
await runs.settle(request.id);
|
|
397
|
+
return false;
|
|
398
|
+
}
|
|
399
|
+
// Back in its place: the queue orders by rank, and the run keeps its own.
|
|
400
|
+
this.queue.push({ ...request, slotWait: true });
|
|
401
|
+
await runs.queue(request.id, request.stage, "workset-slot");
|
|
402
|
+
void this.live.log(
|
|
403
|
+
request.id,
|
|
404
|
+
`\nwaiting for a Workset slot: ${(error as Error).message}\n`,
|
|
405
|
+
);
|
|
406
|
+
this.scheduleSlotRetry();
|
|
407
|
+
return true;
|
|
408
|
+
}
|
|
409
|
+
await runs.fail(request.id, request.stage, failureReason(controller.signal, error));
|
|
410
|
+
return false;
|
|
411
|
+
} finally {
|
|
412
|
+
clearTimer(budget);
|
|
413
|
+
await this.live.flushed(request.id);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
import { CLAUDE_SESSION_ID } from "../attach-client";
|
|
6
|
+
import type { AgentQuestion } from "./questions";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* One run state and one bounded log per Workset, on the factory state volume. They
|
|
10
|
+
* describe this appliance's execution, not the Workset's intent, so they live beside
|
|
11
|
+
* the clones rather than in the companion.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export type RunStage =
|
|
15
|
+
| "clarify"
|
|
16
|
+
| "explore"
|
|
17
|
+
| "propose"
|
|
18
|
+
| "revise"
|
|
19
|
+
| "apply"
|
|
20
|
+
| "archive"
|
|
21
|
+
| "publish";
|
|
22
|
+
|
|
23
|
+
export { type AgentQuestion, questionsOf } from "./questions";
|
|
24
|
+
|
|
25
|
+
export type RunState =
|
|
26
|
+
| { state: "idle"; updatedAt: string }
|
|
27
|
+
| {
|
|
28
|
+
state: "queued";
|
|
29
|
+
stage: RunStage;
|
|
30
|
+
/**
|
|
31
|
+
* Why the run waits: every running Workset container is busy (`workset-slot`), or
|
|
32
|
+
* the build stages are at their cap (`build-slot`).
|
|
33
|
+
*/
|
|
34
|
+
waiting?: "workset-slot" | "build-slot";
|
|
35
|
+
updatedAt: string;
|
|
36
|
+
}
|
|
37
|
+
| { state: "running"; stage: RunStage; startedAt: string; updatedAt: string }
|
|
38
|
+
| {
|
|
39
|
+
state: "waiting";
|
|
40
|
+
stage: RunStage;
|
|
41
|
+
/** Asked together, in order; a person answers them in one go. */
|
|
42
|
+
questions: AgentQuestion[];
|
|
43
|
+
/**
|
|
44
|
+
* This question round; an answer names it, and only one answer per round counts.
|
|
45
|
+
* Missing only in states written before rounds; `RunStore.read` fills it in.
|
|
46
|
+
*/
|
|
47
|
+
round?: string;
|
|
48
|
+
updatedAt: string;
|
|
49
|
+
}
|
|
50
|
+
| { state: "failed"; stage: RunStage; reason: string; updatedAt: string }
|
|
51
|
+
| { state: "interrupted"; stage: RunStage; updatedAt: string };
|
|
52
|
+
|
|
53
|
+
const UNANSWERED = "no answer; take your recommendation";
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* What a person's answers say to the agent: one answer as written, several as each
|
|
57
|
+
* question with its answer. `null` when every answer is empty.
|
|
58
|
+
*/
|
|
59
|
+
export function answerText(questions: AgentQuestion[], answers: string[]): string | null {
|
|
60
|
+
const trimmed = questions.map((_, index) => answers[index]?.trim() ?? "");
|
|
61
|
+
if (trimmed.every((answer) => answer === "")) return null;
|
|
62
|
+
if (questions.length <= 1) return trimmed[0] ?? null;
|
|
63
|
+
return questions
|
|
64
|
+
.map(
|
|
65
|
+
(asked, index) => `${index + 1}. ${asked.question}\nAnswer: ${trimmed[index] || UNANSWERED}`,
|
|
66
|
+
)
|
|
67
|
+
.join("\n\n");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** One check the agent ran, as it reported it. */
|
|
71
|
+
export interface SituationCheck {
|
|
72
|
+
name: string;
|
|
73
|
+
passed: boolean;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** What the agent told a person about the Outcome at the end of a stage. */
|
|
77
|
+
export interface Situation {
|
|
78
|
+
text: string;
|
|
79
|
+
at: string;
|
|
80
|
+
stage?: RunStage;
|
|
81
|
+
checks?: SituationCheck[];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export const SITUATION_HISTORY = 10;
|
|
85
|
+
|
|
86
|
+
/** The memory with `situation` as its newest situation, keeping the last ten. */
|
|
87
|
+
export function withSituation(memory: RunMemory, situation: Situation): RunMemory {
|
|
88
|
+
return {
|
|
89
|
+
...memory,
|
|
90
|
+
situations: [situation, ...(memory.situations ?? [])].slice(0, SITUATION_HISTORY),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** What a later turn needs to continue the Workset's conversation; opaque to Mate. */
|
|
95
|
+
export interface RunMemory {
|
|
96
|
+
/** The harness's resume state from the last turn, when it could be read. */
|
|
97
|
+
resume?: unknown;
|
|
98
|
+
/** The branch the last push reached, for the card's compare link. */
|
|
99
|
+
pushedBranch?: string;
|
|
100
|
+
/** Studio's stored review report per proposed change. */
|
|
101
|
+
reviewReports?: Record<string, string>;
|
|
102
|
+
/** What the agent reported for a person, newest first. */
|
|
103
|
+
situations?: Situation[];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Who drives the Outcome's conversation: the factory, a person once the running stage ends,
|
|
108
|
+
* or a person now. Only Return to automation hands it back.
|
|
109
|
+
*/
|
|
110
|
+
export type ControlMode = "automatic" | "takeover-requested" | "manual";
|
|
111
|
+
|
|
112
|
+
export interface ControlState {
|
|
113
|
+
mode: ControlMode;
|
|
114
|
+
/** Why Return to automation failed, kept while the person has control. */
|
|
115
|
+
reason?: string;
|
|
116
|
+
/** The companion's active changes when the person got control; what they add is linked on return. */
|
|
117
|
+
changesBefore?: string[];
|
|
118
|
+
updatedAt: string;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** The Claude session the factory's turns continue, read from the harness's resume data. */
|
|
122
|
+
export function claudeSessionOf(memory: RunMemory): string | null {
|
|
123
|
+
const resume = memory.resume as { data?: { claudeSessionId?: unknown } } | undefined;
|
|
124
|
+
const id = resume?.data?.claudeSessionId;
|
|
125
|
+
return typeof id === "string" && CLAUDE_SESSION_ID.test(id) ? id : null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export const LOG_LIMIT_BYTES = 256 * 1024;
|
|
129
|
+
const IDLE: RunState = { state: "idle", updatedAt: new Date(0).toISOString() };
|
|
130
|
+
const AUTOMATIC: ControlState = { mode: "automatic", updatedAt: new Date(0).toISOString() };
|
|
131
|
+
|
|
132
|
+
export class RunStore {
|
|
133
|
+
constructor(
|
|
134
|
+
private readonly dir: string,
|
|
135
|
+
private readonly now: () => Date = () => new Date(),
|
|
136
|
+
) {}
|
|
137
|
+
|
|
138
|
+
private file(id: string, suffix: string): string {
|
|
139
|
+
return path.join(this.dir, `${id}.${suffix}`);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private async writeJson(file: string, value: unknown): Promise<void> {
|
|
143
|
+
await fs.mkdir(this.dir, { recursive: true });
|
|
144
|
+
const temp = `${file}.${randomBytes(4).toString("hex")}.tmp`;
|
|
145
|
+
await fs.writeFile(temp, `${JSON.stringify(value, null, 2)}\n`, "utf8");
|
|
146
|
+
await fs.rename(temp, file);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private async readJson<T>(file: string, fallback: T): Promise<T> {
|
|
150
|
+
try {
|
|
151
|
+
return JSON.parse(await fs.readFile(file, "utf8")) as T;
|
|
152
|
+
} catch {
|
|
153
|
+
return fallback;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
stamp(): string {
|
|
158
|
+
return this.now().toISOString();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async read(id: string): Promise<RunState> {
|
|
162
|
+
const state = await this.readJson(this.file(id, "json"), IDLE);
|
|
163
|
+
// A legacy round's id derives from the state itself, so every reader agrees without a write.
|
|
164
|
+
if (state.state === "waiting" && !state.round)
|
|
165
|
+
return { ...state, round: `legacy-${state.updatedAt}` };
|
|
166
|
+
return state;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async write(id: string, state: RunState): Promise<void> {
|
|
170
|
+
await this.writeJson(this.file(id, "json"), state);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async hasState(id: string): Promise<boolean> {
|
|
174
|
+
return (await fs.stat(this.file(id, "json")).catch(() => null)) !== null;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
readMemory(id: string): Promise<RunMemory> {
|
|
178
|
+
return this.readJson(this.file(id, "memory.json"), {});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async writeMemory(id: string, memory: RunMemory): Promise<void> {
|
|
182
|
+
await this.writeJson(this.file(id, "memory.json"), memory);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Automatic unless a person took control; records from before control read automatic. */
|
|
186
|
+
readControl(id: string): Promise<ControlState> {
|
|
187
|
+
return this.readJson(this.file(id, "control.json"), AUTOMATIC);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async writeControl(id: string, control: ControlState): Promise<void> {
|
|
191
|
+
await this.writeJson(this.file(id, "control.json"), control);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Appends to the log, keeping only its newest bytes. */
|
|
195
|
+
async log(id: string, text: string): Promise<void> {
|
|
196
|
+
await fs.mkdir(this.dir, { recursive: true });
|
|
197
|
+
const file = this.file(id, "log");
|
|
198
|
+
await fs.appendFile(file, text, "utf8");
|
|
199
|
+
const stat = await fs.stat(file);
|
|
200
|
+
if (stat.size > LOG_LIMIT_BYTES) {
|
|
201
|
+
const content = await fs.readFile(file);
|
|
202
|
+
await fs.writeFile(file, content.subarray(content.byteLength - LOG_LIMIT_BYTES));
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Forgets the Workset's run: its state, memory, control and log. */
|
|
207
|
+
async remove(id: string): Promise<void> {
|
|
208
|
+
await Promise.all(
|
|
209
|
+
["json", "memory.json", "control.json", "log"].map((suffix) =>
|
|
210
|
+
fs.rm(this.file(id, suffix), { force: true }),
|
|
211
|
+
),
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
async hasLog(id: string): Promise<boolean> {
|
|
216
|
+
const stat = await fs.stat(this.file(id, "log")).catch(() => null);
|
|
217
|
+
return (stat?.size ?? 0) > 0;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async readLog(id: string): Promise<string> {
|
|
221
|
+
return fs.readFile(this.file(id, "log"), "utf8").catch(() => "");
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
async all(): Promise<Map<string, RunState>> {
|
|
225
|
+
const entries = await fs.readdir(this.dir).catch((): string[] => []);
|
|
226
|
+
const ids = entries.flatMap((name) =>
|
|
227
|
+
name.endsWith(".json") && !name.endsWith(".memory.json") && !name.endsWith(".control.json")
|
|
228
|
+
? [name.slice(0, -".json".length)]
|
|
229
|
+
: [],
|
|
230
|
+
);
|
|
231
|
+
return new Map(await Promise.all(ids.map(async (id) => [id, await this.read(id)] as const)));
|
|
232
|
+
}
|
|
233
|
+
}
|