@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,250 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
import type { FactoryClient } from "../client";
|
|
4
|
+
import { readCompanionConfig } from "../../orchestrator/config-store";
|
|
5
|
+
import { findWorkset } from "../../workset/record";
|
|
6
|
+
import { createWorksetSandbox } from "../sandbox";
|
|
7
|
+
import { companionSession, companionSkills } from "./companion-context";
|
|
8
|
+
import type { TurnEvent } from "./live";
|
|
9
|
+
import type { Run } from "./run";
|
|
10
|
+
import type { Situation } from "./state";
|
|
11
|
+
import { type AgentQuestion, type StageAgent, StageError, type TurnOutcome } from "./workflow";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Agent turns through `HarnessAgent` and the Claude Code harness, inside the Workset's
|
|
15
|
+
* container through the broker-backed sandbox. The harness packages load only here, on
|
|
16
|
+
* the first turn, so Studio without factory mode never imports them.
|
|
17
|
+
*
|
|
18
|
+
* The credential is never held: the adapter gets an empty auth environment, so it sends
|
|
19
|
+
* no credential to the bridge, the sandbox starts the bridge with `agent: true`, and the
|
|
20
|
+
* broker sets the value in the bridge's own environment.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export const BRIDGE_PORT = 7681;
|
|
24
|
+
const ASK_HUMAN_REPLY =
|
|
25
|
+
"Question recorded. Stop now and wait; the answer arrives as the next message.";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The factory's whole asking policy, appended to the companion's instructions on every
|
|
29
|
+
* factory turn, resumed ones included. Skills carry no factory text: outside the factory
|
|
30
|
+
* this never appears, so the same skills behave as in a person's session.
|
|
31
|
+
*/
|
|
32
|
+
export function factoryRunInstructions(): string {
|
|
33
|
+
return [
|
|
34
|
+
"You run in the Mate factory, unattended, inside a Workset container on a person's Outcome: what they want, in their words. The person sees your questions and your situation in Studio.",
|
|
35
|
+
"Never use the runtime's own question tool; nobody answers it. Ask a person only through ask_human, then end your turn; the answers arrive as the next message.",
|
|
36
|
+
"Ask together: every question you can already ask in one call, up to five. A follow-up that depends on an answer waits for the next round. This overrides a skill's one question at a time; a skill's confirmation step, such as \"Yes, no, or refine?\", is a question too.",
|
|
37
|
+
"When an answer is a choice, give 2-4 short options and the one you recommend, and say why in the question.",
|
|
38
|
+
"Ask only for authorization, missing facts without a reasonable default, anything hard to undo, or a choice between real alternatives. For everything else take the answer you would recommend, continue, and record it as an assumption.",
|
|
39
|
+
"Write each assumption as `Assumed: <choice> (<why>)`, under Assumptions in a PRE-EXPLORE HANDOFF, in a proposal's Impact section and in your final message. Prefer the reversible, smaller choice; the person corrects it at the review.",
|
|
40
|
+
"Before you end a stage, call report_situation once with one plain sentence the person understands without knowing the factory: what now exists or was decided. Never name a skill, an agent or a stage. After verification, pass each check you ran and whether it passed. When you stop on a question, report nothing.",
|
|
41
|
+
"A proposed change always waits for a person's Approve. Write only what the stage you run writes.",
|
|
42
|
+
].join("\n");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface HarnessStageAgentOptions {
|
|
46
|
+
client: FactoryClient;
|
|
47
|
+
attachSocket: string;
|
|
48
|
+
runs: Run;
|
|
49
|
+
/** The companion whose skills and AGENTS.md each turn gets, as `mate claude` gives a session. */
|
|
50
|
+
companionPath: string;
|
|
51
|
+
worktreeOf: (id: string) => string;
|
|
52
|
+
/** The unprivileged user's home inside Workset containers. */
|
|
53
|
+
home: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The stream parts a turn reads; every other part type is ignored. */
|
|
57
|
+
export type TurnPart =
|
|
58
|
+
| { type: "text-delta"; id?: string; text: string }
|
|
59
|
+
| { type: "text-end"; id: string }
|
|
60
|
+
| { type: "tool-call"; toolName: string; input?: unknown }
|
|
61
|
+
| { type: "tool-result"; toolName: string }
|
|
62
|
+
| { type: "tool-error"; toolName: string }
|
|
63
|
+
| { type: "error"; error: unknown }
|
|
64
|
+
| { type: "finish"; finishReason: string }
|
|
65
|
+
| { type: string };
|
|
66
|
+
|
|
67
|
+
function describe(error: unknown): string {
|
|
68
|
+
if (error instanceof Error) return error.message;
|
|
69
|
+
if (typeof error === "string") return error;
|
|
70
|
+
return JSON.stringify(error) ?? String(error);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const SUMMARY_CHARS = 80;
|
|
74
|
+
|
|
75
|
+
/** A tool input's first text value, one line and short: a command, a path or a pattern. */
|
|
76
|
+
export function toolSummary(input: unknown): string {
|
|
77
|
+
if (input === null || typeof input !== "object") return "";
|
|
78
|
+
const first = Object.values(input).find((value) => typeof value === "string");
|
|
79
|
+
const line = (first ?? "").replace(/\s+/g, " ").trim();
|
|
80
|
+
return line.length > SUMMARY_CHARS ? `${line.slice(0, SUMMARY_CHARS - 1)}…` : line;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Logs the turn's text and returns why it failed, or `null` when it finished. Text
|
|
85
|
+
* blocks can stream side by side, so each is logged whole when it ends. `onEvent` sees
|
|
86
|
+
* the text as it streams and the tool calls; text it saw is logged as `streamed`.
|
|
87
|
+
*/
|
|
88
|
+
export async function followTurn(
|
|
89
|
+
parts: AsyncIterable<TurnPart>,
|
|
90
|
+
onText: (text: string, streamed?: boolean) => void,
|
|
91
|
+
onEvent?: (event: TurnEvent) => void,
|
|
92
|
+
): Promise<string | null> {
|
|
93
|
+
const streamed = onEvent !== undefined;
|
|
94
|
+
let failure: string | null = null;
|
|
95
|
+
const blocks = new Map<string, string>();
|
|
96
|
+
const flush = (id: string) => {
|
|
97
|
+
const text = blocks.get(id);
|
|
98
|
+
blocks.delete(id);
|
|
99
|
+
onEvent?.({ type: "text-end", id });
|
|
100
|
+
if (text) onText(text.endsWith("\n") ? text : `${text}\n`, streamed);
|
|
101
|
+
};
|
|
102
|
+
for await (const part of parts) {
|
|
103
|
+
if (part.type === "text-delta" && "text" in part) {
|
|
104
|
+
onEvent?.({
|
|
105
|
+
type: "text",
|
|
106
|
+
...(part.id === undefined ? {} : { id: part.id }),
|
|
107
|
+
delta: part.text,
|
|
108
|
+
});
|
|
109
|
+
if (part.id === undefined) onText(part.text, streamed);
|
|
110
|
+
else blocks.set(part.id, (blocks.get(part.id) ?? "") + part.text);
|
|
111
|
+
} else if (part.type === "text-end" && "id" in part) flush(part.id);
|
|
112
|
+
else if (part.type === "tool-call" && "toolName" in part) {
|
|
113
|
+
onEvent?.({ type: "tool", name: part.toolName, summary: toolSummary(part.input) });
|
|
114
|
+
} else if ((part.type === "tool-result" || part.type === "tool-error") && "toolName" in part) {
|
|
115
|
+
onEvent?.({ type: "tool-done", name: part.toolName, failed: part.type === "tool-error" });
|
|
116
|
+
} else if (part.type === "error" && "error" in part) failure = describe(part.error);
|
|
117
|
+
else if (part.type === "finish" && "finishReason" in part && part.finishReason === "error")
|
|
118
|
+
failure ??= "the agent ended with an error";
|
|
119
|
+
}
|
|
120
|
+
for (const id of blocks.keys()) flush(id);
|
|
121
|
+
return failure;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function createHarnessStageAgent(options: HarnessStageAgentOptions): StageAgent {
|
|
125
|
+
return {
|
|
126
|
+
async turn({ worksetId, prompt, signal, onText, onEvent, env: turnEnv }): Promise<TurnOutcome> {
|
|
127
|
+
const [{ HarnessAgent }, { createClaudeCode }, { tool }, { z }] = await Promise.all([
|
|
128
|
+
import("@ai-sdk/harness/agent"),
|
|
129
|
+
import("@ai-sdk/harness-claude-code"),
|
|
130
|
+
import("ai"),
|
|
131
|
+
import("zod"),
|
|
132
|
+
]);
|
|
133
|
+
let asked: AgentQuestion[] | null = null;
|
|
134
|
+
const turnAbort = new AbortController();
|
|
135
|
+
const forward = () => turnAbort.abort(signal.reason);
|
|
136
|
+
signal.addEventListener("abort", forward, { once: true });
|
|
137
|
+
|
|
138
|
+
const askHuman = tool({
|
|
139
|
+
description:
|
|
140
|
+
"Ask the engineer what you cannot answer from the Workset, the code or the companion. Put every question you can already ask into one call; ask a follow-up only once it depends on an answer. When an answer is a choice, give 2-4 short options and the one you recommend. The turn ends; the answers arrive as the next message.",
|
|
141
|
+
inputSchema: z.object({
|
|
142
|
+
questions: z
|
|
143
|
+
.array(
|
|
144
|
+
z.object({
|
|
145
|
+
question: z.string().min(1),
|
|
146
|
+
options: z.array(z.string().min(1)).max(5).optional(),
|
|
147
|
+
recommended: z.string().min(1).optional(),
|
|
148
|
+
}),
|
|
149
|
+
)
|
|
150
|
+
.min(1)
|
|
151
|
+
.max(5),
|
|
152
|
+
}),
|
|
153
|
+
execute: async (input) => {
|
|
154
|
+
asked = input.questions.map((entry) => ({
|
|
155
|
+
question: entry.question,
|
|
156
|
+
...(entry.options?.length ? { options: entry.options } : {}),
|
|
157
|
+
...(entry.recommended ? { recommended: entry.recommended } : {}),
|
|
158
|
+
}));
|
|
159
|
+
// The tool result reaches the agent first, then the turn is cut.
|
|
160
|
+
setTimeout(() => turnAbort.abort(new Error("waiting for a person")), 0);
|
|
161
|
+
return ASK_HUMAN_REPLY;
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
let situation: Omit<Situation, "at" | "stage"> | null = null;
|
|
165
|
+
const reportSituation = tool({
|
|
166
|
+
description:
|
|
167
|
+
"Tell the person, in one plain sentence, what now exists or was decided for this Outcome; never name a skill or agent. After verification, pass the checks you ran and whether each passed.",
|
|
168
|
+
inputSchema: z.object({
|
|
169
|
+
situation: z.string().min(1),
|
|
170
|
+
checks: z
|
|
171
|
+
.array(z.object({ name: z.string().min(1), passed: z.boolean() }))
|
|
172
|
+
.max(10)
|
|
173
|
+
.optional(),
|
|
174
|
+
}),
|
|
175
|
+
execute: async (reported) => {
|
|
176
|
+
situation = {
|
|
177
|
+
text: reported.situation.trim(),
|
|
178
|
+
...(reported.checks?.length ? { checks: reported.checks } : {}),
|
|
179
|
+
};
|
|
180
|
+
return "Situation recorded.";
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
const worktree = options.worktreeOf(worksetId);
|
|
184
|
+
const record = await findWorkset(options.companionPath, worksetId);
|
|
185
|
+
const config = await readCompanionConfig(options.companionPath);
|
|
186
|
+
const [skills, { instructions, env }] = await Promise.all([
|
|
187
|
+
companionSkills(options.companionPath, config),
|
|
188
|
+
companionSession(
|
|
189
|
+
options.companionPath,
|
|
190
|
+
{ id: record?.repositories[0] ?? worksetId, path: worktree },
|
|
191
|
+
config,
|
|
192
|
+
),
|
|
193
|
+
]);
|
|
194
|
+
const agent = new HarnessAgent({
|
|
195
|
+
// Any credential named here would reach the bridge per session and override the
|
|
196
|
+
// broker's real one, so none is named.
|
|
197
|
+
harness: createClaudeCode({ auth: {}, port: BRIDGE_PORT, env: { ...env, ...turnEnv } }),
|
|
198
|
+
tools: { ask_human: askHuman, report_situation: reportSituation },
|
|
199
|
+
// Nobody answers Claude's own question tool in an unattended turn; ask_human is the way to ask.
|
|
200
|
+
inactiveTools: ["askUserQuestions"],
|
|
201
|
+
skills,
|
|
202
|
+
instructions: `${instructions}\n\n${factoryRunInstructions()}`,
|
|
203
|
+
permissionMode: "allow-all",
|
|
204
|
+
// The worktree itself, not a per-session subdirectory of it.
|
|
205
|
+
sandboxConfig: { workDir: "." },
|
|
206
|
+
});
|
|
207
|
+
const sandbox = createWorksetSandbox({
|
|
208
|
+
id: worksetId,
|
|
209
|
+
worktree,
|
|
210
|
+
home: options.home,
|
|
211
|
+
client: options.client,
|
|
212
|
+
attachSocket: options.attachSocket,
|
|
213
|
+
bridgePort: BRIDGE_PORT,
|
|
214
|
+
agentSpawns: true,
|
|
215
|
+
sessionId: () => randomUUID(),
|
|
216
|
+
});
|
|
217
|
+
const resumeFrom = await options.runs.resume(worksetId);
|
|
218
|
+
const session = await agent.createSession({
|
|
219
|
+
sandboxSession: sandbox,
|
|
220
|
+
...(resumeFrom ? { resumeFrom: resumeFrom as never } : {}),
|
|
221
|
+
abortSignal: turnAbort.signal,
|
|
222
|
+
});
|
|
223
|
+
try {
|
|
224
|
+
const result = await agent.stream({
|
|
225
|
+
session,
|
|
226
|
+
prompt,
|
|
227
|
+
abortSignal: turnAbort.signal,
|
|
228
|
+
} as never);
|
|
229
|
+
// The full stream, not only its text: a failed API call ends the turn without
|
|
230
|
+
// throwing, and would otherwise read as a finished stage.
|
|
231
|
+
const failure = await followTurn(
|
|
232
|
+
(result as { fullStream: AsyncIterable<TurnPart> }).fullStream,
|
|
233
|
+
onText,
|
|
234
|
+
onEvent,
|
|
235
|
+
);
|
|
236
|
+
if (failure !== null) throw new StageError(`the agent turn failed: ${failure}`);
|
|
237
|
+
} catch (error) {
|
|
238
|
+
if (asked === null) throw error;
|
|
239
|
+
} finally {
|
|
240
|
+
signal.removeEventListener("abort", forward);
|
|
241
|
+
const resume = await session.stop().catch(() => undefined);
|
|
242
|
+
const reported = situation as Omit<Situation, "at" | "stage"> | null;
|
|
243
|
+
await options.runs.saveTurn(worksetId, resume, reported ?? undefined);
|
|
244
|
+
await sandbox.stop();
|
|
245
|
+
}
|
|
246
|
+
const questions = asked as AgentQuestion[] | null;
|
|
247
|
+
return questions === null ? { kind: "done" } : { kind: "question", questions };
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import type { RunStore } from "./state";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A running turn's text and tool activity, for people following the run. Kept in memory
|
|
5
|
+
* only: the stored log stays the record. Each viewer gets a bounded queue, so a slow or
|
|
6
|
+
* lost viewer never holds a turn back; one that falls behind is dropped.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** What a turn shows while it runs; tool inputs only as a name and a short summary. */
|
|
10
|
+
export type TurnEvent =
|
|
11
|
+
| { type: "text"; id?: string; delta: string }
|
|
12
|
+
| { type: "text-end"; id: string }
|
|
13
|
+
| { type: "tool"; name: string; summary: string }
|
|
14
|
+
| { type: "tool-done"; name: string; failed: boolean };
|
|
15
|
+
|
|
16
|
+
export type LiveEvent =
|
|
17
|
+
| TurnEvent
|
|
18
|
+
/** A log line nobody saw streaming, such as the prompt or a linked change. */
|
|
19
|
+
| { type: "log"; text: string }
|
|
20
|
+
/** The Workset's run ended; its state says what comes next. */
|
|
21
|
+
| { type: "settled" }
|
|
22
|
+
/** The viewer fell behind and was dropped; nothing more arrives. */
|
|
23
|
+
| { type: "lost" };
|
|
24
|
+
|
|
25
|
+
/** Events a viewer may hold unread before it is dropped. */
|
|
26
|
+
export const VIEWER_BUFFER = 2000;
|
|
27
|
+
|
|
28
|
+
export interface LiveViewer {
|
|
29
|
+
/** The stored log and the text blocks still streaming when the viewer joined. */
|
|
30
|
+
replay: Promise<{ log: string; open: { id: string; text: string }[] }>;
|
|
31
|
+
/** The next event after the replay, or `null` once closed. */
|
|
32
|
+
next(): Promise<LiveEvent | null>;
|
|
33
|
+
close(): void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface Viewer {
|
|
37
|
+
queue: LiveEvent[];
|
|
38
|
+
wake: (() => void) | null;
|
|
39
|
+
closed: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface Feed {
|
|
43
|
+
viewers: Set<Viewer>;
|
|
44
|
+
/** Text blocks streaming now, by id; logged whole once they end. */
|
|
45
|
+
open: Map<string, string>;
|
|
46
|
+
/** Log writes and replay reads, in order. */
|
|
47
|
+
chain: Promise<unknown>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export class LiveTurns {
|
|
51
|
+
private readonly feeds = new Map<string, Feed>();
|
|
52
|
+
private readonly settledListeners = new Set<(id: string) => void>();
|
|
53
|
+
|
|
54
|
+
constructor(private readonly store: Pick<RunStore, "log" | "readLog">) {}
|
|
55
|
+
|
|
56
|
+
private feed(id: string): Feed {
|
|
57
|
+
let feed = this.feeds.get(id);
|
|
58
|
+
if (!feed) {
|
|
59
|
+
feed = { viewers: new Set(), open: new Map(), chain: Promise.resolve() };
|
|
60
|
+
this.feeds.set(id, feed);
|
|
61
|
+
}
|
|
62
|
+
return feed;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Forgets the feed once its writes are stored and nobody watches or streams. */
|
|
66
|
+
private release(id: string, feed: Feed): void {
|
|
67
|
+
const chain = feed.chain;
|
|
68
|
+
void chain.then(() => {
|
|
69
|
+
const idle = feed.chain === chain && feed.viewers.size === 0 && feed.open.size === 0;
|
|
70
|
+
if (idle && this.feeds.get(id) === feed) this.feeds.delete(id);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
private send(feed: Feed, event: LiveEvent): void {
|
|
75
|
+
for (const viewer of feed.viewers) {
|
|
76
|
+
if (viewer.queue.length >= VIEWER_BUFFER) {
|
|
77
|
+
viewer.queue = [{ type: "lost" }];
|
|
78
|
+
viewer.closed = true;
|
|
79
|
+
feed.viewers.delete(viewer);
|
|
80
|
+
} else viewer.queue.push(event);
|
|
81
|
+
viewer.wake?.();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
publish(id: string, event: TurnEvent | { type: "settled" }): void {
|
|
86
|
+
const feed = this.feed(id);
|
|
87
|
+
if (event.type === "text" && event.id !== undefined) {
|
|
88
|
+
feed.open.set(event.id, (feed.open.get(event.id) ?? "") + event.delta);
|
|
89
|
+
} else if (event.type === "text-end") feed.open.delete(event.id);
|
|
90
|
+
// A turn cut short may leave blocks open; they end with the run.
|
|
91
|
+
else if (event.type === "settled") feed.open.clear();
|
|
92
|
+
this.send(feed, event);
|
|
93
|
+
if (event.type === "settled") {
|
|
94
|
+
this.release(id, feed);
|
|
95
|
+
for (const listener of this.settledListeners) listener(id);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Calls `listener` whenever any Workset's run ends, so a board can update without watching each. */
|
|
100
|
+
onSettled(listener: (id: string) => void): () => void {
|
|
101
|
+
this.settledListeners.add(listener);
|
|
102
|
+
return () => this.settledListeners.delete(listener);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Appends to the stored log. `streamed` text reached viewers already as it streamed;
|
|
107
|
+
* anything else is sent as a log line.
|
|
108
|
+
*/
|
|
109
|
+
log(id: string, text: string, streamed = false): Promise<void> {
|
|
110
|
+
const feed = this.feed(id);
|
|
111
|
+
if (!streamed) this.send(feed, { type: "log", text });
|
|
112
|
+
const write = feed.chain.then(() => this.store.log(id, text)).catch(() => {});
|
|
113
|
+
feed.chain = write;
|
|
114
|
+
this.release(id, feed);
|
|
115
|
+
return write;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Resolves once every log write queued so far is stored. */
|
|
119
|
+
async flushed(id: string): Promise<void> {
|
|
120
|
+
await this.feed(id).chain;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Joins the Workset's feed. The replay is read after every write queued before joining
|
|
125
|
+
* and before any queued after, and the open blocks are taken at the same moment, so the
|
|
126
|
+
* replay and the events that follow show each part exactly once.
|
|
127
|
+
*/
|
|
128
|
+
watch(id: string): LiveViewer {
|
|
129
|
+
const feed = this.feed(id);
|
|
130
|
+
const viewer: Viewer = { queue: [], wake: null, closed: false };
|
|
131
|
+
feed.viewers.add(viewer);
|
|
132
|
+
const open = [...feed.open].map(([block, text]) => ({ id: block, text }));
|
|
133
|
+
const replay = feed.chain.then(async () => ({ log: await this.store.readLog(id), open }));
|
|
134
|
+
// The chain must not hold the replayed log.
|
|
135
|
+
feed.chain = replay.then(
|
|
136
|
+
() => {},
|
|
137
|
+
() => {},
|
|
138
|
+
);
|
|
139
|
+
return {
|
|
140
|
+
replay,
|
|
141
|
+
next: async () => {
|
|
142
|
+
while (viewer.queue.length === 0 && !viewer.closed) {
|
|
143
|
+
// oxlint-disable-next-line no-await-in-loop -- waits for the next event
|
|
144
|
+
await new Promise<void>((resolve) => {
|
|
145
|
+
viewer.wake = resolve;
|
|
146
|
+
});
|
|
147
|
+
viewer.wake = null;
|
|
148
|
+
}
|
|
149
|
+
return viewer.queue.shift() ?? null;
|
|
150
|
+
},
|
|
151
|
+
close: () => {
|
|
152
|
+
viewer.closed = true;
|
|
153
|
+
feed.viewers.delete(viewer);
|
|
154
|
+
viewer.wake?.();
|
|
155
|
+
this.release(id, feed);
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { RunState } from "./state";
|
|
2
|
+
|
|
3
|
+
// Kept free of Node modules, so Studio's browser code can read a run's questions.
|
|
4
|
+
|
|
5
|
+
/** One question the agent asked, with the choices it offered. */
|
|
6
|
+
export interface AgentQuestion {
|
|
7
|
+
question: string;
|
|
8
|
+
/** Choices the agent offered, if the answer is one of a few. */
|
|
9
|
+
options?: string[];
|
|
10
|
+
/** The choice the agent would take. */
|
|
11
|
+
recommended?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A waiting state's questions; a state written before questions were grouped holds one
|
|
16
|
+
* question in its own fields.
|
|
17
|
+
*/
|
|
18
|
+
export function questionsOf(state: Extract<RunState, { state: "waiting" }>): AgentQuestion[] {
|
|
19
|
+
if (Array.isArray(state.questions) && state.questions.length > 0) return state.questions;
|
|
20
|
+
const single = state as unknown as AgentQuestion;
|
|
21
|
+
return typeof single.question === "string" ? [single] : [];
|
|
22
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { WorksetRecord } from "../../workset/record";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The factory work queue's one ordering rule: started Outcomes run in the order they were
|
|
5
|
+
* started. A Later Outcome has no rank.
|
|
6
|
+
*/
|
|
7
|
+
export function rankOf(record: Pick<WorksetRecord, "queuedAt">): number | null {
|
|
8
|
+
if (!record.queuedAt) return null;
|
|
9
|
+
const queued = Date.parse(record.queuedAt);
|
|
10
|
+
return Number.isNaN(queued) ? null : queued;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface Ranked {
|
|
14
|
+
id: string;
|
|
15
|
+
/** `null` for a Later Outcome: after every started entry, in the order given. */
|
|
16
|
+
rank: number | null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Lower rank first, then identifier; Later Outcomes keep their relative order at the end. */
|
|
20
|
+
export function compareRanked(a: Ranked, b: Ranked): number {
|
|
21
|
+
if (a.rank === null || b.rank === null) {
|
|
22
|
+
if (a.rank === b.rank) return 0;
|
|
23
|
+
return a.rank === null ? 1 : -1;
|
|
24
|
+
}
|
|
25
|
+
return a.rank - b.rank || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
26
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { RunStage, RunState } from "./state";
|
|
2
|
+
|
|
3
|
+
/** Shared run predicates; safe for the browser's Outcome policy. */
|
|
4
|
+
export function retryRefusal(id: string, state: RunState["state"]): string | null {
|
|
5
|
+
return state === "failed" || state === "interrupted"
|
|
6
|
+
? null
|
|
7
|
+
: `Workset ${id} has no failed or interrupted run to retry`;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function runnableStage(stage: RunStage): Exclude<RunStage, "archive"> {
|
|
11
|
+
return stage === "archive" ? "apply" : stage;
|
|
12
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
import { KeyedSerial } from "../keyed-serial";
|
|
4
|
+
import { OutcomeControl } from "./control";
|
|
5
|
+
import { retryRefusal, runnableStage } from "./run-policy";
|
|
6
|
+
import {
|
|
7
|
+
type AgentQuestion,
|
|
8
|
+
answerText,
|
|
9
|
+
claudeSessionOf,
|
|
10
|
+
type ControlState,
|
|
11
|
+
questionsOf,
|
|
12
|
+
type RunMemory,
|
|
13
|
+
type RunStage,
|
|
14
|
+
type RunState,
|
|
15
|
+
type RunStore,
|
|
16
|
+
type Situation,
|
|
17
|
+
withSituation,
|
|
18
|
+
} from "./state";
|
|
19
|
+
import { StageError } from "./workflow";
|
|
20
|
+
|
|
21
|
+
export interface RunFacts {
|
|
22
|
+
execution: RunState;
|
|
23
|
+
/** An idle default does not imply a persisted run. */
|
|
24
|
+
recorded: boolean;
|
|
25
|
+
control: ControlState;
|
|
26
|
+
claudeSession: string | null;
|
|
27
|
+
conversation: boolean;
|
|
28
|
+
memory: Omit<RunMemory, "resume">;
|
|
29
|
+
hasLog: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
type ReportedSituation = Omit<Situation, "at" | "stage">;
|
|
33
|
+
|
|
34
|
+
/** Owns a companion's Run transitions and memory; scheduling remains process-local, control is `control`'s. */
|
|
35
|
+
export class Run {
|
|
36
|
+
private readonly memoryUpdates = new KeyedSerial();
|
|
37
|
+
|
|
38
|
+
/** Who drives each Outcome's conversation; the only writer of control state. */
|
|
39
|
+
readonly control: OutcomeControl;
|
|
40
|
+
|
|
41
|
+
constructor(private readonly store: RunStore) {
|
|
42
|
+
this.control = new OutcomeControl(store);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async facts(id: string): Promise<RunFacts> {
|
|
46
|
+
const [execution, recorded, control, memory, hasLog] = await Promise.all([
|
|
47
|
+
this.store.read(id),
|
|
48
|
+
this.store.hasState(id),
|
|
49
|
+
this.store.readControl(id),
|
|
50
|
+
this.store.readMemory(id),
|
|
51
|
+
this.store.hasLog(id),
|
|
52
|
+
]);
|
|
53
|
+
const { resume: _, ...visible } = memory;
|
|
54
|
+
const claudeSession = claudeSessionOf(memory);
|
|
55
|
+
return {
|
|
56
|
+
execution,
|
|
57
|
+
recorded,
|
|
58
|
+
control,
|
|
59
|
+
claudeSession,
|
|
60
|
+
conversation: claudeSession !== null,
|
|
61
|
+
memory: visible,
|
|
62
|
+
hasLog,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async allFacts(ids?: readonly string[]): Promise<Map<string, RunFacts>> {
|
|
67
|
+
const entries = ids ?? [...(await this.store.all()).keys()];
|
|
68
|
+
return new Map(
|
|
69
|
+
await Promise.all(entries.map(async (id) => [id, await this.facts(id)] as const)),
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async resume(id: string): Promise<unknown> {
|
|
74
|
+
return (await this.store.readMemory(id)).resume;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
queue(id: string, stage: RunStage, waiting?: "workset-slot" | "build-slot"): Promise<void> {
|
|
78
|
+
return this.store.write(id, {
|
|
79
|
+
state: "queued",
|
|
80
|
+
stage,
|
|
81
|
+
...(waiting ? { waiting } : {}),
|
|
82
|
+
updatedAt: this.store.stamp(),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
start(id: string, stage: RunStage): Promise<void> {
|
|
87
|
+
const startedAt = this.store.stamp();
|
|
88
|
+
return this.store.write(id, { state: "running", stage, startedAt, updatedAt: startedAt });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
wait(id: string, stage: RunStage, questions: AgentQuestion[]): Promise<void> {
|
|
92
|
+
return this.store.write(id, {
|
|
93
|
+
state: "waiting",
|
|
94
|
+
stage,
|
|
95
|
+
questions,
|
|
96
|
+
round: randomUUID(),
|
|
97
|
+
updatedAt: this.store.stamp(),
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
settle(id: string): Promise<void> {
|
|
102
|
+
return this.store.write(id, { state: "idle", updatedAt: this.store.stamp() });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
fail(id: string, stage: RunStage, reason: string): Promise<void> {
|
|
106
|
+
return this.store.write(id, { state: "failed", stage, reason, updatedAt: this.store.stamp() });
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The scheduler consumes this under its per-Workset answer lock. */
|
|
110
|
+
async prepareAnswer(id: string, answers: string | string[], round?: string) {
|
|
111
|
+
const state = await this.store.read(id);
|
|
112
|
+
if (round !== undefined && (state.state !== "waiting" || state.round !== round)) {
|
|
113
|
+
throw new StageError("this question was already answered");
|
|
114
|
+
}
|
|
115
|
+
if (state.state !== "waiting")
|
|
116
|
+
throw new StageError(`Workset ${id} is not waiting for an answer`);
|
|
117
|
+
const text = answerText(questionsOf(state), typeof answers === "string" ? [answers] : answers);
|
|
118
|
+
if (text === null) throw new StageError("an answer cannot be empty");
|
|
119
|
+
return { stage: runnableStage(state.stage), text };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async retryStage(id: string) {
|
|
123
|
+
const state = await this.store.read(id);
|
|
124
|
+
const refusal = retryRefusal(id, state.state);
|
|
125
|
+
if (refusal) throw new StageError(refusal);
|
|
126
|
+
return runnableStage((state as Extract<RunState, { state: "failed" | "interrupted" }>).stage);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Restart never resumes a stage: running and queued runs become interrupted. */
|
|
130
|
+
async interruptActive(): Promise<string[]> {
|
|
131
|
+
const interrupted: string[] = [];
|
|
132
|
+
await Promise.all(
|
|
133
|
+
[...(await this.store.all())].map(async ([id, state]) => {
|
|
134
|
+
if (state.state !== "running" && state.state !== "queued") return;
|
|
135
|
+
interrupted.push(id);
|
|
136
|
+
await this.store.write(id, {
|
|
137
|
+
state: "interrupted",
|
|
138
|
+
stage: state.stage,
|
|
139
|
+
updatedAt: this.store.stamp(),
|
|
140
|
+
});
|
|
141
|
+
}),
|
|
142
|
+
);
|
|
143
|
+
return interrupted.toSorted();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private updateMemory(
|
|
147
|
+
id: string,
|
|
148
|
+
update: (memory: RunMemory) => RunMemory | Promise<RunMemory>,
|
|
149
|
+
): Promise<void> {
|
|
150
|
+
return this.memoryUpdates.run(id, async () => {
|
|
151
|
+
await this.store.writeMemory(id, await update(await this.store.readMemory(id)));
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
saveTurn(id: string, resume?: unknown, situation?: ReportedSituation): Promise<void> {
|
|
156
|
+
return this.updateMemory(id, async (memory) => {
|
|
157
|
+
const next = { ...memory, ...(resume ? { resume } : {}) };
|
|
158
|
+
if (!situation) return next;
|
|
159
|
+
const state = await this.store.read(id);
|
|
160
|
+
return withSituation(next, {
|
|
161
|
+
...situation,
|
|
162
|
+
at: this.store.stamp(),
|
|
163
|
+
...(state.state === "idle" ? {} : { stage: state.stage }),
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
rememberPush(id: string, branch: string): Promise<void> {
|
|
169
|
+
return this.updateMemory(id, (memory) => ({ ...memory, pushedBranch: branch }));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
rememberReview(id: string, change: string, report: string): Promise<void> {
|
|
173
|
+
return this.updateMemory(id, (memory) => ({
|
|
174
|
+
...memory,
|
|
175
|
+
reviewReports: { ...memory.reviewReports, [change]: report },
|
|
176
|
+
}));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
recordSituation(id: string, situation: ReportedSituation, stage?: RunStage): Promise<void> {
|
|
180
|
+
return this.updateMemory(id, (memory) =>
|
|
181
|
+
withSituation(memory, {
|
|
182
|
+
...situation,
|
|
183
|
+
at: this.store.stamp(),
|
|
184
|
+
...(stage ? { stage } : {}),
|
|
185
|
+
}),
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
forget(id: string): Promise<void> {
|
|
190
|
+
return this.store.remove(id);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
log(id: string, text: string): Promise<void> {
|
|
194
|
+
return this.store.log(id, text);
|
|
195
|
+
}
|
|
196
|
+
readLog(id: string): Promise<string> {
|
|
197
|
+
return this.store.readLog(id);
|
|
198
|
+
}
|
|
199
|
+
}
|