@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,24 @@
|
|
|
1
|
+
/** The only environment variable a forge token is read from. */
|
|
2
|
+
export const FORGE_TOKEN_ENV = "MATE_GIT_TOKEN";
|
|
3
|
+
|
|
4
|
+
export interface ForgeToken {
|
|
5
|
+
token: string;
|
|
6
|
+
source: typeof FORGE_TOKEN_ENV;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface ForgeTokenDeps {
|
|
10
|
+
env?: Record<string, string | undefined>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The forge token from `MATE_GIT_TOKEN`, or null. Other variables such as `GITHUB_TOKEN`
|
|
15
|
+
* are deliberately ignored so a broadly scoped CI token is never picked up. No stored
|
|
16
|
+
* credential (GitHub CLI, keychain, Git credential helper) is ever read: endpoint security
|
|
17
|
+
* reports that as credential access. The returned value is for the forge request only:
|
|
18
|
+
* callers never print, log, or store it.
|
|
19
|
+
*/
|
|
20
|
+
export async function resolveForgeToken(deps: ForgeTokenDeps = {}): Promise<ForgeToken | null> {
|
|
21
|
+
const env = deps.env ?? process.env;
|
|
22
|
+
const fromEnv = env[FORGE_TOKEN_ENV]?.trim();
|
|
23
|
+
return fromEnv ? { token: fromEnv, source: FORGE_TOKEN_ENV } : null;
|
|
24
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import net from "node:net";
|
|
2
|
+
|
|
3
|
+
import { readWorksets } from "../workset/record";
|
|
4
|
+
import { encodeControl, encodeData, FRAME_CONTROL, FrameDecoder } from "./frames";
|
|
5
|
+
import { FactoryError } from "./ids";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The attach helper Studio's terminal runs in its PTY: it asks the broker to start the
|
|
9
|
+
* Workset's agent and relays the terminal both ways. It holds no credential; the broker
|
|
10
|
+
* adds it. Closing the helper closes the attach, which makes the broker stop the agent.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export const ATTACH_STARTS = ["clarify", "explore", "continue", "resume"] as const;
|
|
14
|
+
export type AttachStart = (typeof ATTACH_STARTS)[number];
|
|
15
|
+
|
|
16
|
+
export interface AgentLaunchOptions {
|
|
17
|
+
/** Skip the companion's Git synchronization, as Studio's own launches do. */
|
|
18
|
+
noGit?: boolean;
|
|
19
|
+
/** The factory's Claude session a `resume` start continues. */
|
|
20
|
+
resumeSession?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** A Claude session id; anything else never reaches the command line. */
|
|
24
|
+
export const CLAUDE_SESSION_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
25
|
+
|
|
26
|
+
/** The managed launch inside the Workset container, with the server-built first turn. */
|
|
27
|
+
export function agentCommand(
|
|
28
|
+
start: AttachStart,
|
|
29
|
+
intent: string,
|
|
30
|
+
options: AgentLaunchOptions = {},
|
|
31
|
+
): string[] {
|
|
32
|
+
const launch = ["mate", "claude", "--", "--yes", ...(options.noGit ? ["--no-git"] : [])];
|
|
33
|
+
if (start === "continue") return [...launch, "--continue"];
|
|
34
|
+
if (start === "resume") {
|
|
35
|
+
if (!options.resumeSession || !CLAUDE_SESSION_ID.test(options.resumeSession)) {
|
|
36
|
+
throw new FactoryError("a resume start needs the factory's Claude session id");
|
|
37
|
+
}
|
|
38
|
+
return [...launch, "--resume", options.resumeSession];
|
|
39
|
+
}
|
|
40
|
+
if (start === "explore") return [...launch, "/mate-explore-change"];
|
|
41
|
+
const text = intent.trim();
|
|
42
|
+
return [...launch, text ? `/mate-clarify-intent ${text}` : "/mate-clarify-intent"];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export async function worksetIntent(companionPath: string, id: string): Promise<string> {
|
|
46
|
+
const { worksets } = await readWorksets(companionPath);
|
|
47
|
+
const record = worksets.find((workset) => workset.id === id);
|
|
48
|
+
if (!record) throw new FactoryError(`no valid Workset ${id} in ${companionPath}`);
|
|
49
|
+
return record.intent;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface AttachStreams {
|
|
53
|
+
input: NodeJS.ReadableStream & { setRawMode?: (raw: boolean) => void; isTTY?: boolean };
|
|
54
|
+
output: NodeJS.WritableStream & {
|
|
55
|
+
columns?: number;
|
|
56
|
+
rows?: number;
|
|
57
|
+
on(event: "resize", listener: () => void): unknown;
|
|
58
|
+
};
|
|
59
|
+
error: NodeJS.WritableStream;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Resolves with the agent's exit status; an attach the broker refuses resolves 1. */
|
|
63
|
+
export function runAttach(
|
|
64
|
+
socketPath: string,
|
|
65
|
+
start: { id: string; session: string; command: string[] },
|
|
66
|
+
streams: AttachStreams,
|
|
67
|
+
connect: (path: string) => net.Socket = (path) => net.connect(path),
|
|
68
|
+
): Promise<number> {
|
|
69
|
+
return new Promise((resolve) => {
|
|
70
|
+
const socket = connect(socketPath);
|
|
71
|
+
const decoder = new FrameDecoder();
|
|
72
|
+
let status: number | null = null;
|
|
73
|
+
const size = () => ({ cols: streams.output.columns ?? 80, rows: streams.output.rows ?? 24 });
|
|
74
|
+
|
|
75
|
+
const finish = (code: number) => {
|
|
76
|
+
if (streams.input.isTTY) streams.input.setRawMode?.(false);
|
|
77
|
+
streams.input.removeListener("data", onInput);
|
|
78
|
+
resolve(code);
|
|
79
|
+
};
|
|
80
|
+
const onInput = (chunk: Buffer | string) => {
|
|
81
|
+
if (!socket.destroyed)
|
|
82
|
+
socket.write(encodeData(typeof chunk === "string" ? chunk : new Uint8Array(chunk)));
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
socket.on("connect", () => {
|
|
86
|
+
socket.write(encodeControl({ type: "start", ...start, agent: true, ...size() }));
|
|
87
|
+
if (streams.input.isTTY) streams.input.setRawMode?.(true);
|
|
88
|
+
streams.input.on("data", onInput);
|
|
89
|
+
streams.output.on("resize", () => {
|
|
90
|
+
if (!socket.destroyed) socket.write(encodeControl({ type: "resize", ...size() }));
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
socket.on("data", (chunk: Buffer) => {
|
|
94
|
+
for (const frame of decoder.push(new Uint8Array(chunk))) {
|
|
95
|
+
if (frame.type !== FRAME_CONTROL) {
|
|
96
|
+
streams.output.write(frame.data);
|
|
97
|
+
} else if (frame.message.type === "exit") {
|
|
98
|
+
status = typeof frame.message.status === "number" ? frame.message.status : 1;
|
|
99
|
+
} else if (frame.message.type === "error") {
|
|
100
|
+
streams.error.write(`mate factory attach: ${String(frame.message.reason)}\r\n`);
|
|
101
|
+
status = 1;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
socket.on("error", (error) => {
|
|
106
|
+
streams.error.write(
|
|
107
|
+
`mate factory attach: the factory broker is not reachable: ${error.message}\r\n`,
|
|
108
|
+
);
|
|
109
|
+
status = 1;
|
|
110
|
+
});
|
|
111
|
+
socket.on("close", () => finish(status ?? 1));
|
|
112
|
+
});
|
|
113
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import net from "node:net";
|
|
3
|
+
|
|
4
|
+
import { type AgentCredentials, NO_CREDENTIAL } from "./credential-store";
|
|
5
|
+
import {
|
|
6
|
+
encodeControl,
|
|
7
|
+
encodeData,
|
|
8
|
+
encodeStderr,
|
|
9
|
+
type Frame,
|
|
10
|
+
FRAME_CONTROL,
|
|
11
|
+
FrameDecoder,
|
|
12
|
+
FrameError,
|
|
13
|
+
} from "./frames";
|
|
14
|
+
import { FactoryError } from "./ids";
|
|
15
|
+
import {
|
|
16
|
+
bunPtySpawn,
|
|
17
|
+
nodePipeSpawn,
|
|
18
|
+
type PipeProcess,
|
|
19
|
+
type PipeSpawn,
|
|
20
|
+
type PtyProcess,
|
|
21
|
+
type PtySpawn,
|
|
22
|
+
} from "./pty";
|
|
23
|
+
import { SESSION_MARKER, type WorksetRuntime } from "./runtime";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The broker's attach socket: an interactive agent in a Workset container, relayed
|
|
27
|
+
* as frames over a Unix socket. The first frame names the Workset, the session and
|
|
28
|
+
* the command; the broker runs `docker exec -it` in its own PTY with the agent
|
|
29
|
+
* credential and the session marker in that client's environment only. When the
|
|
30
|
+
* connection closes for any reason, the broker stops the marked processes inside
|
|
31
|
+
* the container.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
const START_FIELDS = new Set(["type", "id", "session", "command", "agent", "cols", "rows", "tty"]);
|
|
35
|
+
const SIZE = { cols: [2, 500], rows: [1, 300] } as const;
|
|
36
|
+
|
|
37
|
+
function inRange(value: unknown, [min, max]: readonly [number, number]): value is number {
|
|
38
|
+
return typeof value === "number" && Number.isInteger(value) && value >= min && value <= max;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface AttachServerOptions {
|
|
42
|
+
runtime: WorksetRuntime;
|
|
43
|
+
credentials: AgentCredentials;
|
|
44
|
+
spawn?: PtySpawn;
|
|
45
|
+
/** For `tty: false` attaches, such as the harness bridge. */
|
|
46
|
+
spawnPipe?: PipeSpawn;
|
|
47
|
+
/** The broker's environment for the `docker` client, credential already taken out. */
|
|
48
|
+
env?: NodeJS.ProcessEnv;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Handles one attach connection; exposed so tests can drive it with an in-memory socket. */
|
|
52
|
+
export function handleAttach(socket: net.Socket, options: AttachServerOptions): void {
|
|
53
|
+
const spawn = options.spawn ?? bunPtySpawn;
|
|
54
|
+
const spawnPipe = options.spawnPipe ?? nodePipeSpawn;
|
|
55
|
+
const decoder = new FrameDecoder();
|
|
56
|
+
let pty: PtyProcess | PipeProcess | null = null;
|
|
57
|
+
let started: { id: string; session: string } | null = null;
|
|
58
|
+
// An attached session keeps its Workset container from an idle stop.
|
|
59
|
+
let endSession: (() => void) | null = null;
|
|
60
|
+
let starting = false;
|
|
61
|
+
let closed = false;
|
|
62
|
+
|
|
63
|
+
const fail = (reason: string) => {
|
|
64
|
+
if (!socket.destroyed) socket.end(encodeControl({ type: "error", reason }));
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const start = async (message: Record<string, unknown>) => {
|
|
68
|
+
starting = true;
|
|
69
|
+
const extra = Object.keys(message).filter((key) => !START_FIELDS.has(key));
|
|
70
|
+
if (extra.length > 0) return fail(`the broker does not accept ${extra.join(", ")}`);
|
|
71
|
+
if (message.tty !== undefined && typeof message.tty !== "boolean") {
|
|
72
|
+
return fail("tty must be true or false");
|
|
73
|
+
}
|
|
74
|
+
const tty = message.tty !== false;
|
|
75
|
+
if (tty && (!inRange(message.cols, SIZE.cols) || !inRange(message.rows, SIZE.rows))) {
|
|
76
|
+
return fail("terminal size out of range");
|
|
77
|
+
}
|
|
78
|
+
if (message.agent !== undefined && typeof message.agent !== "boolean") {
|
|
79
|
+
return fail("agent must be true or false");
|
|
80
|
+
}
|
|
81
|
+
const credential = message.agent === true ? options.credentials.current() : null;
|
|
82
|
+
if (message.agent === true && credential === null) return fail(NO_CREDENTIAL);
|
|
83
|
+
let argv: string[];
|
|
84
|
+
try {
|
|
85
|
+
argv = await options.runtime.attachCommand({
|
|
86
|
+
id: message.id as string,
|
|
87
|
+
command: message.command as string[],
|
|
88
|
+
session: message.session as string,
|
|
89
|
+
envNames: credential ? [credential.name] : [],
|
|
90
|
+
tty,
|
|
91
|
+
});
|
|
92
|
+
} catch (error) {
|
|
93
|
+
return fail(error instanceof FactoryError ? error.message : "the attach could not start");
|
|
94
|
+
}
|
|
95
|
+
if (closed) return;
|
|
96
|
+
started = { id: message.id as string, session: message.session as string };
|
|
97
|
+
endSession = options.runtime.beginSession(started.id);
|
|
98
|
+
const env: Record<string, string> = {};
|
|
99
|
+
for (const [key, value] of Object.entries(options.env ?? process.env)) {
|
|
100
|
+
if (value !== undefined) env[key] = value;
|
|
101
|
+
}
|
|
102
|
+
Object.assign(env, options.runtime.dockerEnv(), {
|
|
103
|
+
[SESSION_MARKER]: started.session,
|
|
104
|
+
TERM: "xterm-256color",
|
|
105
|
+
});
|
|
106
|
+
if (credential) env[credential.name] = credential.value;
|
|
107
|
+
const onData = (data: Uint8Array) => {
|
|
108
|
+
if (!socket.destroyed) socket.write(encodeData(data));
|
|
109
|
+
};
|
|
110
|
+
pty = tty
|
|
111
|
+
? spawn({ argv, env, cols: message.cols as number, rows: message.rows as number, onData })
|
|
112
|
+
: spawnPipe({
|
|
113
|
+
argv,
|
|
114
|
+
env,
|
|
115
|
+
cols: 0,
|
|
116
|
+
rows: 0,
|
|
117
|
+
onData,
|
|
118
|
+
onStderr: (data) => {
|
|
119
|
+
if (!socket.destroyed) socket.write(encodeStderr(data));
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
void pty.exited.then((status) => {
|
|
123
|
+
if (!socket.destroyed) socket.end(encodeControl({ type: "exit", status }));
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const frame = (entry: Frame) => {
|
|
128
|
+
if (entry.type === FRAME_CONTROL) {
|
|
129
|
+
if (entry.message.type === "start" && !starting) {
|
|
130
|
+
void start(entry.message);
|
|
131
|
+
} else if (entry.message.type === "eof" && pty && "end" in pty) {
|
|
132
|
+
pty.end();
|
|
133
|
+
} else if (entry.message.type === "resize" && pty) {
|
|
134
|
+
if (inRange(entry.message.cols, SIZE.cols) && inRange(entry.message.rows, SIZE.rows)) {
|
|
135
|
+
pty.resize(entry.message.cols, entry.message.rows);
|
|
136
|
+
}
|
|
137
|
+
} else if (!starting) {
|
|
138
|
+
fail("the first frame must start the attach");
|
|
139
|
+
}
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (pty) pty.write(entry.data);
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
socket.on("data", (chunk: Buffer) => {
|
|
146
|
+
let frames: Frame[];
|
|
147
|
+
try {
|
|
148
|
+
frames = decoder.push(new Uint8Array(chunk));
|
|
149
|
+
} catch (error) {
|
|
150
|
+
fail(error instanceof FrameError ? error.message : "malformed frame");
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
for (const entry of frames) frame(entry);
|
|
154
|
+
});
|
|
155
|
+
socket.on("error", () => {});
|
|
156
|
+
socket.on("close", () => {
|
|
157
|
+
closed = true;
|
|
158
|
+
pty?.kill("SIGHUP");
|
|
159
|
+
endSession?.();
|
|
160
|
+
if (started) {
|
|
161
|
+
void options.runtime.stopMarked(started.id, started.session).catch((error: unknown) => {
|
|
162
|
+
process.stderr.write(
|
|
163
|
+
`mate factory: stopping session ${started!.session} in ${started!.id} failed: ${(error as Error).message}\n`,
|
|
164
|
+
);
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function serveAttach(
|
|
171
|
+
socketPath: string,
|
|
172
|
+
options: AttachServerOptions,
|
|
173
|
+
): { stop: () => void } {
|
|
174
|
+
fs.rmSync(socketPath, { force: true });
|
|
175
|
+
const server = net.createServer((socket) => handleAttach(socket, options));
|
|
176
|
+
server.listen(socketPath);
|
|
177
|
+
return { stop: () => server.close() };
|
|
178
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AGENT_CREDENTIAL_NAMES,
|
|
3
|
+
checkedCredential,
|
|
4
|
+
credentialKind,
|
|
5
|
+
type CredentialStore,
|
|
6
|
+
NO_CREDENTIAL,
|
|
7
|
+
} from "./credential-store";
|
|
8
|
+
import { FactoryError } from "./ids";
|
|
9
|
+
import type { WorksetRuntime } from "./runtime";
|
|
10
|
+
|
|
11
|
+
export { AGENT_CREDENTIAL_NAMES, takeAgentCredential } from "./credential-store";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The factory broker's API: the Workset operations and the Workset agent
|
|
15
|
+
* credential, over HTTP on a Unix socket, and nothing else. It is the boundary
|
|
16
|
+
* between processes running as the unprivileged user and the inner Docker
|
|
17
|
+
* daemon, so every request is checked field by field and an unexpected field is
|
|
18
|
+
* refused rather than ignored: a caller cannot ask for a mount, an image or a
|
|
19
|
+
* privilege. No answer carries the credential's value.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
type Body = Record<string, unknown>;
|
|
23
|
+
|
|
24
|
+
function only(body: Body, allowed: string[]): void {
|
|
25
|
+
const accepted = new Set(allowed);
|
|
26
|
+
const extra = Object.keys(body).filter((key) => !accepted.has(key));
|
|
27
|
+
if (extra.length > 0) {
|
|
28
|
+
throw new FactoryError(
|
|
29
|
+
`the broker does not accept ${extra.map((key) => `"${key}"`).join(", ")}`,
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function stringRecord(value: unknown, what: string): Record<string, string> | undefined {
|
|
35
|
+
if (value === undefined) return undefined;
|
|
36
|
+
if (
|
|
37
|
+
value === null ||
|
|
38
|
+
typeof value !== "object" ||
|
|
39
|
+
Array.isArray(value) ||
|
|
40
|
+
!Object.values(value).every((entry) => typeof entry === "string")
|
|
41
|
+
) {
|
|
42
|
+
throw new FactoryError(`${what} must map names to strings`);
|
|
43
|
+
}
|
|
44
|
+
return value as Record<string, string>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface BrokerHandler {
|
|
48
|
+
(operation: string, body: Body): Promise<unknown>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function brokerHandler(
|
|
52
|
+
runtime: WorksetRuntime,
|
|
53
|
+
credentials: CredentialStore,
|
|
54
|
+
): BrokerHandler {
|
|
55
|
+
return async (operation, body) => {
|
|
56
|
+
switch (operation) {
|
|
57
|
+
case "spawn":
|
|
58
|
+
only(body, ["id", "repository", "remoteUrl", "companion", "size"]);
|
|
59
|
+
return runtime.spawn(body as never);
|
|
60
|
+
case "pause":
|
|
61
|
+
only(body, ["id"]);
|
|
62
|
+
await runtime.pause(body.id as string);
|
|
63
|
+
return {};
|
|
64
|
+
case "resume":
|
|
65
|
+
only(body, ["id"]);
|
|
66
|
+
await runtime.resume(body.id as string);
|
|
67
|
+
return {};
|
|
68
|
+
case "delete":
|
|
69
|
+
only(body, ["id", "force"]);
|
|
70
|
+
if (body.force !== undefined && typeof body.force !== "boolean") {
|
|
71
|
+
throw new FactoryError("force must be true or false");
|
|
72
|
+
}
|
|
73
|
+
await runtime.delete(body.id as string, { force: body.force === true });
|
|
74
|
+
return {};
|
|
75
|
+
case "list":
|
|
76
|
+
only(body, []);
|
|
77
|
+
return { worksets: await runtime.list() };
|
|
78
|
+
case "address":
|
|
79
|
+
only(body, ["id"]);
|
|
80
|
+
return { address: await runtime.address(body.id as string) };
|
|
81
|
+
case "info": {
|
|
82
|
+
only(body, []);
|
|
83
|
+
// The kind only: callers may know whether an agent can start, never the value.
|
|
84
|
+
const credential = credentials.current();
|
|
85
|
+
return { credential: credential ? credentialKind(credential.name) : null };
|
|
86
|
+
}
|
|
87
|
+
case "credential.status":
|
|
88
|
+
only(body, []);
|
|
89
|
+
return credentials.status();
|
|
90
|
+
case "credential.set":
|
|
91
|
+
only(body, ["kind", "value"]);
|
|
92
|
+
credentials.set(checkedCredential(body.kind, body.value));
|
|
93
|
+
return {};
|
|
94
|
+
case "credential.clear":
|
|
95
|
+
only(body, []);
|
|
96
|
+
credentials.clear();
|
|
97
|
+
return {};
|
|
98
|
+
case "exec": {
|
|
99
|
+
only(body, ["id", "command", "env", "cwd", "agent", "stdin"]);
|
|
100
|
+
if (body.stdin !== undefined && typeof body.stdin !== "string") {
|
|
101
|
+
throw new FactoryError("stdin must be base64 text");
|
|
102
|
+
}
|
|
103
|
+
const env = { ...stringRecord(body.env, "exec's environment") };
|
|
104
|
+
for (const name of AGENT_CREDENTIAL_NAMES) {
|
|
105
|
+
if (name in env) {
|
|
106
|
+
throw new FactoryError(
|
|
107
|
+
`${name} is set by the broker for agent commands, not by callers`,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (body.agent !== undefined && typeof body.agent !== "boolean") {
|
|
112
|
+
throw new FactoryError("agent must be true or false");
|
|
113
|
+
}
|
|
114
|
+
if (body.cwd !== undefined && typeof body.cwd !== "string") {
|
|
115
|
+
throw new FactoryError("cwd must be a path relative to the worktree");
|
|
116
|
+
}
|
|
117
|
+
if (body.agent === true) {
|
|
118
|
+
const credential = credentials.current();
|
|
119
|
+
if (credential === null) throw new FactoryError(NO_CREDENTIAL);
|
|
120
|
+
env[credential.name] = credential.value;
|
|
121
|
+
}
|
|
122
|
+
const result = await runtime.exec({
|
|
123
|
+
id: body.id as string,
|
|
124
|
+
command: body.command as string[],
|
|
125
|
+
env,
|
|
126
|
+
cwd: body.cwd as string | undefined,
|
|
127
|
+
...(typeof body.stdin === "string"
|
|
128
|
+
? { stdin: new Uint8Array(Buffer.from(body.stdin, "base64")) }
|
|
129
|
+
: {}),
|
|
130
|
+
});
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
default:
|
|
134
|
+
throw new FactoryError(`the broker has no operation ${JSON.stringify(operation)}`);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** `POST /v1/<operation>` with a JSON object; answers `{ ok, result | error }`. */
|
|
140
|
+
export async function handleRequest(handler: BrokerHandler, request: Request): Promise<Response> {
|
|
141
|
+
const url = new URL(request.url);
|
|
142
|
+
const match = /^\/v1\/([a-z]+(?:\.[a-z]+)*)$/.exec(url.pathname);
|
|
143
|
+
if (request.method !== "POST" || !match) {
|
|
144
|
+
return Response.json({ ok: false, error: "not found" }, { status: 404 });
|
|
145
|
+
}
|
|
146
|
+
let body: unknown;
|
|
147
|
+
try {
|
|
148
|
+
body = await request.json();
|
|
149
|
+
} catch {
|
|
150
|
+
return Response.json({ ok: false, error: "the request is not JSON" }, { status: 400 });
|
|
151
|
+
}
|
|
152
|
+
if (body === null || typeof body !== "object" || Array.isArray(body)) {
|
|
153
|
+
return Response.json(
|
|
154
|
+
{ ok: false, error: "the request must be a JSON object" },
|
|
155
|
+
{ status: 400 },
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
try {
|
|
159
|
+
return Response.json({ ok: true, result: await handler(match[1]!, body as Body) });
|
|
160
|
+
} catch (error) {
|
|
161
|
+
if (error instanceof FactoryError) {
|
|
162
|
+
return Response.json(
|
|
163
|
+
{ ok: false, error: error.message, ...(error.code ? { code: error.code } : {}) },
|
|
164
|
+
{ status: 400 },
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
// Never echo an unexpected error: it could carry anything, the credential included.
|
|
168
|
+
process.stderr.write(
|
|
169
|
+
`mate factory: unexpected error in ${match[1]}: ${(error as Error).name}\n`,
|
|
170
|
+
);
|
|
171
|
+
return Response.json({ ok: false, error: "the broker failed unexpectedly" }, { status: 500 });
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** The broker serves on Bun, as Studio does; named here rather than typed globally. */
|
|
176
|
+
interface BunRuntime {
|
|
177
|
+
serve(options: { unix: string; fetch: (request: Request) => Promise<Response> }): {
|
|
178
|
+
stop(closeActiveConnections?: boolean): void;
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function serveBroker(socket: string, handler: BrokerHandler): { stop: () => void } {
|
|
183
|
+
const runtime = (globalThis as unknown as { Bun?: BunRuntime }).Bun;
|
|
184
|
+
if (!runtime) throw new FactoryError("the factory broker requires the Bun runtime");
|
|
185
|
+
const server = runtime.serve({
|
|
186
|
+
unix: socket,
|
|
187
|
+
fetch: (request: Request) => handleRequest(handler, request),
|
|
188
|
+
});
|
|
189
|
+
return { stop: () => server.stop(true) };
|
|
190
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workset sandboxes use Bun as their only JavaScript runtime and package manager: a
|
|
3
|
+
* command the harness sends with node, npm, npx, pnpm or yarn is rewritten to Bun before
|
|
4
|
+
* it reaches the container. The image ships no other package manager.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** Where the Workset image pre-installs the Claude Code harness bridge at the pinned version. */
|
|
8
|
+
export const BAKED_BRIDGE_DIR = "/opt/mate/harness-bridge";
|
|
9
|
+
|
|
10
|
+
/** Every word is matched whole; the rest of the command is kept as it was sent. */
|
|
11
|
+
const INSTALL = /^(?:pnpm|npm|yarn)\s+(?:install|i|ci)(?=\s|$)(.*)$/s;
|
|
12
|
+
const RUN = /^(?:pnpm|npm|yarn)\s+run(?=\s)/;
|
|
13
|
+
const EXEC = /^(?:pnpm\s+(?:exec|dlx)|yarn\s+dlx|npx)(?=\s)/;
|
|
14
|
+
const NODE = /^node(?=\s)/;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* An install reuses the baked bridge's packages when the directory's manifest is the
|
|
18
|
+
* baked one, which is how the harness's bootstrap finishes without a download.
|
|
19
|
+
* Package-manager options such as `--store-dir` have no Bun meaning and are dropped.
|
|
20
|
+
*/
|
|
21
|
+
function bunInstall(args: string, ci: boolean): string {
|
|
22
|
+
const frozen = ci || /(?:^|\s)--frozen-lockfile(?=\s|$)/.test(args) ? " --frozen-lockfile" : "";
|
|
23
|
+
return (
|
|
24
|
+
`if cmp -s package.json ${BAKED_BRIDGE_DIR}/package.json && test -d ${BAKED_BRIDGE_DIR}/node_modules; ` +
|
|
25
|
+
`then ln -sfn ${BAKED_BRIDGE_DIR}/node_modules node_modules; ` +
|
|
26
|
+
`else bun install${frozen}; fi`
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The command as the sandbox runs it; anything that names no Node tool is unchanged. */
|
|
31
|
+
export function bunCommand(command: string): string {
|
|
32
|
+
const trimmed = command.trimStart();
|
|
33
|
+
const install = INSTALL.exec(trimmed);
|
|
34
|
+
if (install) return bunInstall(install[1] ?? "", /^npm\s+ci(?=\s|$)/.test(trimmed));
|
|
35
|
+
if (RUN.test(trimmed)) return trimmed.replace(RUN, "bun run");
|
|
36
|
+
if (EXEC.test(trimmed)) return trimmed.replace(EXEC, "bunx");
|
|
37
|
+
if (NODE.test(trimmed)) return trimmed.replace(NODE, "bun");
|
|
38
|
+
return command;
|
|
39
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
import type { CommandResult } from "./command";
|
|
4
|
+
import type { CredentialKind, CredentialStatus } from "./credential-store";
|
|
5
|
+
import { FactoryError } from "./ids";
|
|
6
|
+
import type { ExecRequest, SpawnRequest, WorksetInfo } from "./runtime";
|
|
7
|
+
|
|
8
|
+
/** Where the factory supervisor tells Studio the broker listens; unset outside factory mode. */
|
|
9
|
+
export const FACTORY_BROKER_ENV = "MATE_FACTORY_BROKER";
|
|
10
|
+
|
|
11
|
+
export interface FactoryClient {
|
|
12
|
+
spawn(request: SpawnRequest): Promise<WorksetInfo>;
|
|
13
|
+
pause(id: string): Promise<void>;
|
|
14
|
+
resume(id: string): Promise<void>;
|
|
15
|
+
delete(id: string, options?: { force?: boolean }): Promise<void>;
|
|
16
|
+
exec(
|
|
17
|
+
request: Omit<ExecRequest, "stdin"> & { agent?: boolean; stdin?: Uint8Array },
|
|
18
|
+
): Promise<CommandResult>;
|
|
19
|
+
/** The Workset container's address on the inner network, for reaching a port inside it. */
|
|
20
|
+
address(id: string): Promise<string>;
|
|
21
|
+
list(): Promise<WorksetInfo[]>;
|
|
22
|
+
/** Which kind of Workset agent credential the broker holds; null when none. */
|
|
23
|
+
info(): Promise<{ credential: CredentialKind | null }>;
|
|
24
|
+
credential: {
|
|
25
|
+
status(): Promise<CredentialStatus>;
|
|
26
|
+
set(kind: CredentialKind, value: string): Promise<void>;
|
|
27
|
+
clear(): Promise<void>;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The broker serves attaches on a second socket beside its own. */
|
|
32
|
+
export function attachSocketFor(brokerSocket: string): string {
|
|
33
|
+
return path.join(path.dirname(brokerSocket), "attach.sock");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The broker's client, or null outside factory mode. */
|
|
37
|
+
export function factoryClient(
|
|
38
|
+
socket: string | undefined = process.env[FACTORY_BROKER_ENV],
|
|
39
|
+
fetcher: typeof fetch = fetch,
|
|
40
|
+
): FactoryClient | null {
|
|
41
|
+
if (!socket) return null;
|
|
42
|
+
|
|
43
|
+
const call = async <T>(operation: string, body: object): Promise<T> => {
|
|
44
|
+
let response: Response;
|
|
45
|
+
try {
|
|
46
|
+
response = await fetcher(`http://factory/v1/${operation}`, {
|
|
47
|
+
method: "POST",
|
|
48
|
+
headers: { "content-type": "application/json" },
|
|
49
|
+
body: JSON.stringify(body),
|
|
50
|
+
unix: socket,
|
|
51
|
+
} as RequestInit);
|
|
52
|
+
} catch (error) {
|
|
53
|
+
throw new FactoryError(
|
|
54
|
+
`the factory broker at ${socket} is not reachable: ${(error as Error).message}`,
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
const answer = (await response.json()) as {
|
|
58
|
+
ok: boolean;
|
|
59
|
+
result?: T;
|
|
60
|
+
error?: string;
|
|
61
|
+
code?: string;
|
|
62
|
+
};
|
|
63
|
+
if (!answer.ok) {
|
|
64
|
+
throw new FactoryError(
|
|
65
|
+
answer.error ?? "the factory broker refused the request",
|
|
66
|
+
answer.code === "busy" ? "busy" : undefined,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
return answer.result as T;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
spawn: (request) => call("spawn", request),
|
|
74
|
+
pause: async (id) => void (await call("pause", { id })),
|
|
75
|
+
resume: async (id) => void (await call("resume", { id })),
|
|
76
|
+
delete: async (id, options = {}) =>
|
|
77
|
+
void (await call(
|
|
78
|
+
"delete",
|
|
79
|
+
options.force === undefined ? { id } : { id, force: options.force },
|
|
80
|
+
)),
|
|
81
|
+
exec: ({ stdin, ...request }) =>
|
|
82
|
+
call("exec", stdin ? { ...request, stdin: Buffer.from(stdin).toString("base64") } : request),
|
|
83
|
+
address: async (id) => (await call<{ address: string }>("address", { id })).address,
|
|
84
|
+
list: async () => (await call<{ worksets: WorksetInfo[] }>("list", {})).worksets,
|
|
85
|
+
info: () => call("info", {}),
|
|
86
|
+
credential: {
|
|
87
|
+
status: () => call("credential.status", {}),
|
|
88
|
+
set: async (kind, value) => void (await call("credential.set", { kind, value })),
|
|
89
|
+
clear: async () => void (await call("credential.clear", {})),
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|