@dirgha/code 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +101 -0
- package/README.md +674 -0
- package/dist/dirgha.mjs +2185 -0
- package/dist_v2/acp/index.d.ts +1 -0
- package/dist_v2/acp/index.js +2 -0
- package/dist_v2/acp/server.d.ts +31 -0
- package/dist_v2/acp/server.js +137 -0
- package/dist_v2/cli/auth-cmd.d.ts +10 -0
- package/dist_v2/cli/auth-cmd.js +69 -0
- package/dist_v2/cli/config.d.ts +46 -0
- package/dist_v2/cli/config.js +70 -0
- package/dist_v2/cli/flags.d.ts +10 -0
- package/dist_v2/cli/flags.js +49 -0
- package/dist_v2/cli/flows/full-cycle.d.ts +30 -0
- package/dist_v2/cli/flows/full-cycle.js +39 -0
- package/dist_v2/cli/flows/index.d.ts +2 -0
- package/dist_v2/cli/flows/index.js +3 -0
- package/dist_v2/cli/flows/tarball.d.ts +10 -0
- package/dist_v2/cli/flows/tarball.js +47 -0
- package/dist_v2/cli/index.d.ts +4 -0
- package/dist_v2/cli/index.js +5 -0
- package/dist_v2/cli/interactive.d.ts +21 -0
- package/dist_v2/cli/interactive.js +207 -0
- package/dist_v2/cli/main.d.ts +10 -0
- package/dist_v2/cli/main.js +173 -0
- package/dist_v2/cli/models-cmd.d.ts +6 -0
- package/dist_v2/cli/models-cmd.js +50 -0
- package/dist_v2/cli/setup.d.ts +13 -0
- package/dist_v2/cli/setup.js +128 -0
- package/dist_v2/cli/slash/account.d.ts +10 -0
- package/dist_v2/cli/slash/account.js +59 -0
- package/dist_v2/cli/slash/clear.d.ts +8 -0
- package/dist_v2/cli/slash/clear.js +15 -0
- package/dist_v2/cli/slash/compact.d.ts +7 -0
- package/dist_v2/cli/slash/compact.js +13 -0
- package/dist_v2/cli/slash/config.d.ts +8 -0
- package/dist_v2/cli/slash/config.js +49 -0
- package/dist_v2/cli/slash/exit.d.ts +7 -0
- package/dist_v2/cli/slash/exit.js +16 -0
- package/dist_v2/cli/slash/fleet.d.ts +7 -0
- package/dist_v2/cli/slash/fleet.js +22 -0
- package/dist_v2/cli/slash/help.d.ts +9 -0
- package/dist_v2/cli/slash/help.js +31 -0
- package/dist_v2/cli/slash/history.d.ts +7 -0
- package/dist_v2/cli/slash/history.js +42 -0
- package/dist_v2/cli/slash/index.d.ts +30 -0
- package/dist_v2/cli/slash/index.js +52 -0
- package/dist_v2/cli/slash/init.d.ts +8 -0
- package/dist_v2/cli/slash/init.js +57 -0
- package/dist_v2/cli/slash/keys.d.ts +8 -0
- package/dist_v2/cli/slash/keys.js +101 -0
- package/dist_v2/cli/slash/login.d.ts +10 -0
- package/dist_v2/cli/slash/login.js +47 -0
- package/dist_v2/cli/slash/memory.d.ts +8 -0
- package/dist_v2/cli/slash/memory.js +101 -0
- package/dist_v2/cli/slash/mode.d.ts +8 -0
- package/dist_v2/cli/slash/mode.js +30 -0
- package/dist_v2/cli/slash/models.d.ts +8 -0
- package/dist_v2/cli/slash/models.js +73 -0
- package/dist_v2/cli/slash/resume.d.ts +8 -0
- package/dist_v2/cli/slash/resume.js +24 -0
- package/dist_v2/cli/slash/session.d.ts +8 -0
- package/dist_v2/cli/slash/session.js +74 -0
- package/dist_v2/cli/slash/setup.d.ts +8 -0
- package/dist_v2/cli/slash/setup.js +50 -0
- package/dist_v2/cli/slash/status.d.ts +7 -0
- package/dist_v2/cli/slash/status.js +37 -0
- package/dist_v2/cli/slash/theme.d.ts +9 -0
- package/dist_v2/cli/slash/theme.js +53 -0
- package/dist_v2/cli/slash/types.d.ts +14 -0
- package/dist_v2/cli/slash/types.js +9 -0
- package/dist_v2/cli/slash/upgrade.d.ts +9 -0
- package/dist_v2/cli/slash/upgrade.js +66 -0
- package/dist_v2/cli/slash.d.ts +72 -0
- package/dist_v2/cli/slash.js +77 -0
- package/dist_v2/cli/subcommands/ask.d.ts +17 -0
- package/dist_v2/cli/subcommands/ask.js +76 -0
- package/dist_v2/cli/subcommands/audit.d.ts +15 -0
- package/dist_v2/cli/subcommands/audit.js +128 -0
- package/dist_v2/cli/subcommands/chat.d.ts +13 -0
- package/dist_v2/cli/subcommands/chat.js +72 -0
- package/dist_v2/cli/subcommands/compact.d.ts +14 -0
- package/dist_v2/cli/subcommands/compact.js +114 -0
- package/dist_v2/cli/subcommands/doctor.d.ts +11 -0
- package/dist_v2/cli/subcommands/doctor.js +103 -0
- package/dist_v2/cli/subcommands/export-session.d.ts +10 -0
- package/dist_v2/cli/subcommands/export-session.js +60 -0
- package/dist_v2/cli/subcommands/import-session.d.ts +11 -0
- package/dist_v2/cli/subcommands/import-session.js +77 -0
- package/dist_v2/cli/subcommands/index.d.ts +40 -0
- package/dist_v2/cli/subcommands/index.js +38 -0
- package/dist_v2/cli/subcommands/init.d.ts +10 -0
- package/dist_v2/cli/subcommands/init.js +74 -0
- package/dist_v2/cli/subcommands/keys.d.ts +12 -0
- package/dist_v2/cli/subcommands/keys.js +136 -0
- package/dist_v2/cli/subcommands/login.d.ts +10 -0
- package/dist_v2/cli/subcommands/login.js +60 -0
- package/dist_v2/cli/subcommands/logout.d.ts +10 -0
- package/dist_v2/cli/subcommands/logout.js +21 -0
- package/dist_v2/cli/subcommands/models.d.ts +14 -0
- package/dist_v2/cli/subcommands/models.js +144 -0
- package/dist_v2/cli/subcommands/setup.d.ts +11 -0
- package/dist_v2/cli/subcommands/setup.js +107 -0
- package/dist_v2/cli/subcommands/stats.d.ts +13 -0
- package/dist_v2/cli/subcommands/stats.js +148 -0
- package/dist_v2/cli/subcommands/status.d.ts +14 -0
- package/dist_v2/cli/subcommands/status.js +91 -0
- package/dist_v2/cli/submit-paper.d.ts +15 -0
- package/dist_v2/cli/submit-paper.js +68 -0
- package/dist_v2/context/_fts.d.ts +62 -0
- package/dist_v2/context/_fts.js +195 -0
- package/dist_v2/context/branch.d.ts +18 -0
- package/dist_v2/context/branch.js +66 -0
- package/dist_v2/context/compaction.d.ts +26 -0
- package/dist_v2/context/compaction.js +120 -0
- package/dist_v2/context/index.d.ts +5 -0
- package/dist_v2/context/index.js +6 -0
- package/dist_v2/context/knowledge.d.ts +38 -0
- package/dist_v2/context/knowledge.js +146 -0
- package/dist_v2/context/memory.d.ts +80 -0
- package/dist_v2/context/memory.js +276 -0
- package/dist_v2/context/mode.d.ts +25 -0
- package/dist_v2/context/mode.js +81 -0
- package/dist_v2/context/session.d.ts +56 -0
- package/dist_v2/context/session.js +108 -0
- package/dist_v2/daemon/index.d.ts +2 -0
- package/dist_v2/daemon/index.js +3 -0
- package/dist_v2/daemon/protocol.d.ts +73 -0
- package/dist_v2/daemon/protocol.js +6 -0
- package/dist_v2/daemon/server.d.ts +35 -0
- package/dist_v2/daemon/server.js +191 -0
- package/dist_v2/eval/index.d.ts +5 -0
- package/dist_v2/eval/index.js +6 -0
- package/dist_v2/eval/regression.d.ts +14 -0
- package/dist_v2/eval/regression.js +106 -0
- package/dist_v2/eval/reporter.d.ts +7 -0
- package/dist_v2/eval/reporter.js +34 -0
- package/dist_v2/eval/swe-bench.d.ts +18 -0
- package/dist_v2/eval/swe-bench.js +23 -0
- package/dist_v2/eval/terminal-bench.d.ts +13 -0
- package/dist_v2/eval/terminal-bench.js +19 -0
- package/dist_v2/eval/types.d.ts +36 -0
- package/dist_v2/eval/types.js +5 -0
- package/dist_v2/fleet/apply-back.d.ts +27 -0
- package/dist_v2/fleet/apply-back.js +191 -0
- package/dist_v2/fleet/cli-command.d.ts +61 -0
- package/dist_v2/fleet/cli-command.js +329 -0
- package/dist_v2/fleet/index.d.ts +21 -0
- package/dist_v2/fleet/index.js +21 -0
- package/dist_v2/fleet/runner.d.ts +28 -0
- package/dist_v2/fleet/runner.js +347 -0
- package/dist_v2/fleet/tripleshot.d.ts +21 -0
- package/dist_v2/fleet/tripleshot.js +222 -0
- package/dist_v2/fleet/types.d.ts +193 -0
- package/dist_v2/fleet/types.js +24 -0
- package/dist_v2/fleet/worktree.d.ts +51 -0
- package/dist_v2/fleet/worktree.js +176 -0
- package/dist_v2/hooks/index.d.ts +1 -0
- package/dist_v2/hooks/index.js +2 -0
- package/dist_v2/hooks/registry.d.ts +21 -0
- package/dist_v2/hooks/registry.js +40 -0
- package/dist_v2/index.d.ts +12 -0
- package/dist_v2/index.js +11 -0
- package/dist_v2/integrations/arniko-client.d.ts +42 -0
- package/dist_v2/integrations/arniko-client.js +86 -0
- package/dist_v2/integrations/auth.d.ts +45 -0
- package/dist_v2/integrations/auth.js +54 -0
- package/dist_v2/integrations/billing.d.ts +71 -0
- package/dist_v2/integrations/billing.js +107 -0
- package/dist_v2/integrations/bucky-client.d.ts +105 -0
- package/dist_v2/integrations/bucky-client.js +76 -0
- package/dist_v2/integrations/deploy-client.d.ts +60 -0
- package/dist_v2/integrations/deploy-client.js +87 -0
- package/dist_v2/integrations/device-auth.d.ts +48 -0
- package/dist_v2/integrations/device-auth.js +192 -0
- package/dist_v2/integrations/entitlements.d.ts +34 -0
- package/dist_v2/integrations/entitlements.js +43 -0
- package/dist_v2/integrations/http.d.ts +22 -0
- package/dist_v2/integrations/http.js +114 -0
- package/dist_v2/integrations/index.d.ts +8 -0
- package/dist_v2/integrations/index.js +15 -0
- package/dist_v2/intelligence/cost.d.ts +28 -0
- package/dist_v2/intelligence/cost.js +58 -0
- package/dist_v2/intelligence/error-classifier.d.ts +8 -0
- package/dist_v2/intelligence/error-classifier.js +71 -0
- package/dist_v2/intelligence/index.d.ts +5 -0
- package/dist_v2/intelligence/index.js +6 -0
- package/dist_v2/intelligence/prices.d.ts +13 -0
- package/dist_v2/intelligence/prices.js +26 -0
- package/dist_v2/intelligence/smart-router.d.ts +21 -0
- package/dist_v2/intelligence/smart-router.js +59 -0
- package/dist_v2/intelligence/telemetry.d.ts +22 -0
- package/dist_v2/intelligence/telemetry.js +31 -0
- package/dist_v2/kernel/agent-loop.d.ts +32 -0
- package/dist_v2/kernel/agent-loop.js +151 -0
- package/dist_v2/kernel/event-stream.d.ts +18 -0
- package/dist_v2/kernel/event-stream.js +102 -0
- package/dist_v2/kernel/index.d.ts +4 -0
- package/dist_v2/kernel/index.js +5 -0
- package/dist_v2/kernel/message.d.ts +29 -0
- package/dist_v2/kernel/message.js +118 -0
- package/dist_v2/kernel/types.d.ts +206 -0
- package/dist_v2/kernel/types.js +9 -0
- package/dist_v2/mcp/client.d.ts +44 -0
- package/dist_v2/mcp/client.js +63 -0
- package/dist_v2/mcp/index.d.ts +3 -0
- package/dist_v2/mcp/index.js +4 -0
- package/dist_v2/mcp/tool-bridge.d.ts +11 -0
- package/dist_v2/mcp/tool-bridge.js +33 -0
- package/dist_v2/mcp/transport.d.ts +29 -0
- package/dist_v2/mcp/transport.js +73 -0
- package/dist_v2/parity/index.d.ts +3 -0
- package/dist_v2/parity/index.js +4 -0
- package/dist_v2/parity/mock-openai-compat.d.ts +15 -0
- package/dist_v2/parity/mock-openai-compat.js +35 -0
- package/dist_v2/parity/runner.d.ts +21 -0
- package/dist_v2/parity/runner.js +76 -0
- package/dist_v2/parity/scenarios.d.ts +20 -0
- package/dist_v2/parity/scenarios.js +70 -0
- package/dist_v2/providers/anthropic.d.ts +21 -0
- package/dist_v2/providers/anthropic.js +191 -0
- package/dist_v2/providers/dispatch.d.ts +11 -0
- package/dist_v2/providers/dispatch.js +45 -0
- package/dist_v2/providers/fireworks.d.ts +18 -0
- package/dist_v2/providers/fireworks.js +44 -0
- package/dist_v2/providers/gemini.d.ts +19 -0
- package/dist_v2/providers/gemini.js +188 -0
- package/dist_v2/providers/http.d.ts +41 -0
- package/dist_v2/providers/http.js +188 -0
- package/dist_v2/providers/iface.d.ts +18 -0
- package/dist_v2/providers/iface.js +18 -0
- package/dist_v2/providers/index.d.ts +39 -0
- package/dist_v2/providers/index.js +51 -0
- package/dist_v2/providers/nvidia.d.ts +27 -0
- package/dist_v2/providers/nvidia.js +142 -0
- package/dist_v2/providers/ollama.d.ts +15 -0
- package/dist_v2/providers/ollama.js +37 -0
- package/dist_v2/providers/openai-compat.d.ts +29 -0
- package/dist_v2/providers/openai-compat.js +239 -0
- package/dist_v2/providers/openai.d.ts +20 -0
- package/dist_v2/providers/openai.js +107 -0
- package/dist_v2/providers/openrouter.d.ts +24 -0
- package/dist_v2/providers/openrouter.js +69 -0
- package/dist_v2/safety/approval-bus.d.ts +26 -0
- package/dist_v2/safety/approval-bus.js +48 -0
- package/dist_v2/safety/audit-log.d.ts +27 -0
- package/dist_v2/safety/audit-log.js +71 -0
- package/dist_v2/safety/index.d.ts +4 -0
- package/dist_v2/safety/index.js +5 -0
- package/dist_v2/safety/policy.d.ts +32 -0
- package/dist_v2/safety/policy.js +131 -0
- package/dist_v2/safety/sandbox/bwrap.d.ts +11 -0
- package/dist_v2/safety/sandbox/bwrap.js +52 -0
- package/dist_v2/safety/sandbox/iface.d.ts +29 -0
- package/dist_v2/safety/sandbox/iface.js +8 -0
- package/dist_v2/safety/sandbox/index.d.ts +7 -0
- package/dist_v2/safety/sandbox/index.js +8 -0
- package/dist_v2/safety/sandbox/landlock.d.ts +17 -0
- package/dist_v2/safety/sandbox/landlock.js +26 -0
- package/dist_v2/safety/sandbox/noop.d.ts +12 -0
- package/dist_v2/safety/sandbox/noop.js +52 -0
- package/dist_v2/safety/sandbox/seatbelt.d.ts +11 -0
- package/dist_v2/safety/sandbox/seatbelt.js +49 -0
- package/dist_v2/safety/sandbox/select.d.ts +6 -0
- package/dist_v2/safety/sandbox/select.js +42 -0
- package/dist_v2/safety/sandbox/windows.d.ts +13 -0
- package/dist_v2/safety/sandbox/windows.js +22 -0
- package/dist_v2/skills/index.d.ts +3 -0
- package/dist_v2/skills/index.js +4 -0
- package/dist_v2/skills/loader.d.ts +38 -0
- package/dist_v2/skills/loader.js +102 -0
- package/dist_v2/skills/matcher.d.ts +14 -0
- package/dist_v2/skills/matcher.js +72 -0
- package/dist_v2/skills/runtime.d.ts +13 -0
- package/dist_v2/skills/runtime.js +40 -0
- package/dist_v2/subagents/delegator.d.ts +36 -0
- package/dist_v2/subagents/delegator.js +82 -0
- package/dist_v2/subagents/index.d.ts +2 -0
- package/dist_v2/subagents/index.js +3 -0
- package/dist_v2/subagents/pool.d.ts +18 -0
- package/dist_v2/subagents/pool.js +31 -0
- package/dist_v2/tools/browser.d.ts +27 -0
- package/dist_v2/tools/browser.js +179 -0
- package/dist_v2/tools/checkpoint.d.ts +25 -0
- package/dist_v2/tools/checkpoint.js +222 -0
- package/dist_v2/tools/cron.d.ts +32 -0
- package/dist_v2/tools/cron.js +160 -0
- package/dist_v2/tools/diff.d.ts +18 -0
- package/dist_v2/tools/diff.js +117 -0
- package/dist_v2/tools/exec.d.ts +19 -0
- package/dist_v2/tools/exec.js +49 -0
- package/dist_v2/tools/fs-edit.d.ts +11 -0
- package/dist_v2/tools/fs-edit.js +81 -0
- package/dist_v2/tools/fs-ls.d.ts +7 -0
- package/dist_v2/tools/fs-ls.js +57 -0
- package/dist_v2/tools/fs-read.d.ts +7 -0
- package/dist_v2/tools/fs-read.js +60 -0
- package/dist_v2/tools/fs-write.d.ts +8 -0
- package/dist_v2/tools/fs-write.js +46 -0
- package/dist_v2/tools/git.d.ts +8 -0
- package/dist_v2/tools/git.js +57 -0
- package/dist_v2/tools/index.d.ts +25 -0
- package/dist_v2/tools/index.js +39 -0
- package/dist_v2/tools/multimodal.d.ts +41 -0
- package/dist_v2/tools/multimodal.js +381 -0
- package/dist_v2/tools/permission.d.ts +27 -0
- package/dist_v2/tools/permission.js +43 -0
- package/dist_v2/tools/registry.d.ts +44 -0
- package/dist_v2/tools/registry.js +62 -0
- package/dist_v2/tools/search-glob.d.ts +7 -0
- package/dist_v2/tools/search-glob.js +98 -0
- package/dist_v2/tools/search-grep.d.ts +7 -0
- package/dist_v2/tools/search-grep.js +109 -0
- package/dist_v2/tools/shell.d.ts +10 -0
- package/dist_v2/tools/shell.js +76 -0
- package/dist_v2/tools/task.d.ts +8 -0
- package/dist_v2/tools/task.js +37 -0
- package/dist_v2/tui/approval.d.ts +7 -0
- package/dist_v2/tui/approval.js +55 -0
- package/dist_v2/tui/index.d.ts +4 -0
- package/dist_v2/tui/index.js +5 -0
- package/dist_v2/tui/ink/App.d.ts +39 -0
- package/dist_v2/tui/ink/App.js +206 -0
- package/dist_v2/tui/ink/components/AtFileComplete.d.ts +25 -0
- package/dist_v2/tui/ink/components/AtFileComplete.js +158 -0
- package/dist_v2/tui/ink/components/HelpOverlay.d.ts +24 -0
- package/dist_v2/tui/ink/components/HelpOverlay.js +130 -0
- package/dist_v2/tui/ink/components/InputBox.d.ts +35 -0
- package/dist_v2/tui/ink/components/InputBox.js +153 -0
- package/dist_v2/tui/ink/components/Logo.d.ts +12 -0
- package/dist_v2/tui/ink/components/Logo.js +27 -0
- package/dist_v2/tui/ink/components/ModelPicker.d.ts +26 -0
- package/dist_v2/tui/ink/components/ModelPicker.js +80 -0
- package/dist_v2/tui/ink/components/PasteCollapse.d.ts +36 -0
- package/dist_v2/tui/ink/components/PasteCollapse.js +59 -0
- package/dist_v2/tui/ink/components/StatusBar.d.ts +18 -0
- package/dist_v2/tui/ink/components/StatusBar.js +41 -0
- package/dist_v2/tui/ink/components/StreamingText.d.ts +14 -0
- package/dist_v2/tui/ink/components/StreamingText.js +11 -0
- package/dist_v2/tui/ink/components/ThinkingBlock.d.ts +13 -0
- package/dist_v2/tui/ink/components/ThinkingBlock.js +13 -0
- package/dist_v2/tui/ink/components/ToolBox.d.ts +20 -0
- package/dist_v2/tui/ink/components/ToolBox.js +63 -0
- package/dist_v2/tui/ink/components/vim-bindings.d.ts +43 -0
- package/dist_v2/tui/ink/components/vim-bindings.js +135 -0
- package/dist_v2/tui/ink/index.d.ts +39 -0
- package/dist_v2/tui/ink/index.js +47 -0
- package/dist_v2/tui/ink/use-event-projection.d.ts +51 -0
- package/dist_v2/tui/ink/use-event-projection.js +126 -0
- package/dist_v2/tui/ink/use-overlays.d.ts +24 -0
- package/dist_v2/tui/ink/use-overlays.js +50 -0
- package/dist_v2/tui/renderer.d.ts +19 -0
- package/dist_v2/tui/renderer.js +64 -0
- package/dist_v2/tui/status-bar.d.ts +12 -0
- package/dist_v2/tui/status-bar.js +16 -0
- package/dist_v2/tui/theme.d.ts +27 -0
- package/dist_v2/tui/theme.js +56 -0
- package/dist_v2/utils/json-repair.d.ts +7 -0
- package/dist_v2/utils/json-repair.js +45 -0
- package/package.json +113 -0
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* fleet/runner.ts — Spawn and orchestrate N subagents in parallel worktrees.
|
|
3
|
+
*
|
|
4
|
+
* Each agent runs v2's agent-loop directly, NOT as a subprocess. cwd is
|
|
5
|
+
* swapped per-agent via the ToolExecutor's `cwd`, so file operations are
|
|
6
|
+
* isolated to the worktree without process isolation overhead.
|
|
7
|
+
*
|
|
8
|
+
* Flow:
|
|
9
|
+
* 1. Resolve subtasks (decompose goal if not supplied).
|
|
10
|
+
* 2. Create one worktree per subtask.
|
|
11
|
+
* 3. Spawn agents under a bounded-concurrency semaphore.
|
|
12
|
+
* 4. Forward per-agent AgentEvents + FleetEvents onto the parent stream.
|
|
13
|
+
* 5. Gather results, compute totals, return FleetResult.
|
|
14
|
+
* 6. Optional cleanup of worktrees.
|
|
15
|
+
*/
|
|
16
|
+
import { runAgentLoop } from '../kernel/agent-loop.js';
|
|
17
|
+
import { createEventStream } from '../kernel/event-stream.js';
|
|
18
|
+
import { extractText } from '../kernel/message.js';
|
|
19
|
+
import { ProviderRegistry } from '../providers/index.js';
|
|
20
|
+
import { builtInTools } from '../tools/index.js';
|
|
21
|
+
import { createToolRegistry } from '../tools/registry.js';
|
|
22
|
+
import { createToolExecutor } from '../tools/exec.js';
|
|
23
|
+
import { repairJSON } from '../utils/json-repair.js';
|
|
24
|
+
import { createWorktree, destroyWorktree, getRepoRoot, slug } from './worktree.js';
|
|
25
|
+
import { AGENT_TYPE_TOOLS, } from './types.js';
|
|
26
|
+
const DECOMPOSE_SYSTEM = `You are a task decomposer. Given a user goal, split it into 2-5 INDEPENDENT subtasks that can run in PARALLEL without conflicting with each other (no shared-file edits).
|
|
27
|
+
|
|
28
|
+
For each subtask, pick ONE agent type:
|
|
29
|
+
- explore: read-only codebase investigation
|
|
30
|
+
- plan: produces a step-by-step plan (no code changes)
|
|
31
|
+
- verify: reads code + runs tests/checks
|
|
32
|
+
- code: makes code changes
|
|
33
|
+
- research: web search + browse
|
|
34
|
+
|
|
35
|
+
Output STRICT JSON only — no prose, no markdown fences:
|
|
36
|
+
{
|
|
37
|
+
"subtasks": [
|
|
38
|
+
{ "id": "short-kebab-id", "title": "human title", "task": "precise agent prompt with file paths", "type": "code" }
|
|
39
|
+
]
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
Rules:
|
|
43
|
+
- Max 5 subtasks
|
|
44
|
+
- IDs are kebab-case, unique, <30 chars
|
|
45
|
+
- Each task prompt is self-contained (no "see above")
|
|
46
|
+
- Prefer explore+plan before code agents when goal is vague
|
|
47
|
+
- If the goal is ALREADY a single atomic task, return 1 subtask`;
|
|
48
|
+
/**
|
|
49
|
+
* Run a fleet. Accepts either explicit `subtasks` or a bare `goal` that
|
|
50
|
+
* will be decomposed via the planner model.
|
|
51
|
+
*/
|
|
52
|
+
export async function runFleet(config) {
|
|
53
|
+
const started = Date.now();
|
|
54
|
+
const cwd = config.cwd ?? process.cwd();
|
|
55
|
+
const repoRoot = await getRepoRoot(cwd);
|
|
56
|
+
const model = config.model ?? defaultModel();
|
|
57
|
+
const plannerModel = config.plannerModel ?? model;
|
|
58
|
+
const events = config.events ?? createEventStream();
|
|
59
|
+
const providers = new ProviderRegistry();
|
|
60
|
+
const subtasks = config.subtasks && config.subtasks.length > 0
|
|
61
|
+
? config.subtasks.map(normalizeSubtask)
|
|
62
|
+
: await decomposeGoal(config.goal, plannerModel, providers);
|
|
63
|
+
const worktrees = [];
|
|
64
|
+
const agents = [];
|
|
65
|
+
const goalSlug = slug(config.goal);
|
|
66
|
+
for (const sub of subtasks) {
|
|
67
|
+
const branch = `fleet/${goalSlug}/${sub.id}`;
|
|
68
|
+
const handle = await createWorktree(branch, {
|
|
69
|
+
repoRoot,
|
|
70
|
+
worktreeBase: config.worktreeBase,
|
|
71
|
+
});
|
|
72
|
+
worktrees.push(handle);
|
|
73
|
+
agents.push({
|
|
74
|
+
id: sub.id,
|
|
75
|
+
subtask: sub,
|
|
76
|
+
status: 'pending',
|
|
77
|
+
worktreePath: handle.path,
|
|
78
|
+
branchName: handle.branch,
|
|
79
|
+
startedAt: 0,
|
|
80
|
+
output: '',
|
|
81
|
+
bytesWritten: 0,
|
|
82
|
+
usage: { inputTokens: 0, outputTokens: 0, cachedTokens: 0, costUsd: 0 },
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
emitFleetEvent(events, { type: 'fleet_start', goal: config.goal, agents: [...agents] });
|
|
86
|
+
for (const a of agents)
|
|
87
|
+
config.onAgent?.(a);
|
|
88
|
+
await runWithConcurrency(agents, {
|
|
89
|
+
providers,
|
|
90
|
+
model,
|
|
91
|
+
maxTurns: config.maxTurns ?? 15,
|
|
92
|
+
timeoutMs: config.timeoutMs ?? 10 * 60 * 1000,
|
|
93
|
+
concurrency: Math.max(1, config.concurrency ?? 3),
|
|
94
|
+
events,
|
|
95
|
+
signal: config.signal,
|
|
96
|
+
verbose: config.verbose ?? false,
|
|
97
|
+
onAgent: config.onAgent,
|
|
98
|
+
});
|
|
99
|
+
const successCount = agents.filter(a => a.status === 'completed').length;
|
|
100
|
+
const failCount = agents.filter(a => a.status === 'failed' || a.status === 'cancelled').length;
|
|
101
|
+
const durationMs = Date.now() - started;
|
|
102
|
+
const totalTokens = agents.reduce((acc, a) => ({
|
|
103
|
+
inputTokens: acc.inputTokens + a.usage.inputTokens,
|
|
104
|
+
outputTokens: acc.outputTokens + a.usage.outputTokens,
|
|
105
|
+
cachedTokens: acc.cachedTokens + a.usage.cachedTokens,
|
|
106
|
+
costUsd: acc.costUsd + a.usage.costUsd,
|
|
107
|
+
}), { inputTokens: 0, outputTokens: 0, cachedTokens: 0, costUsd: 0 });
|
|
108
|
+
emitFleetEvent(events, { type: 'fleet_end', goal: config.goal, successCount, failCount, durationMs });
|
|
109
|
+
if (config.autoCleanup) {
|
|
110
|
+
await Promise.allSettled(worktrees.map(wt => destroyWorktree(wt)));
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
goal: config.goal,
|
|
114
|
+
agents,
|
|
115
|
+
worktrees,
|
|
116
|
+
successCount,
|
|
117
|
+
failCount,
|
|
118
|
+
durationMs,
|
|
119
|
+
totalTokens,
|
|
120
|
+
failed: agents.filter(a => a.status === 'failed' || a.status === 'cancelled'),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
async function runWithConcurrency(agents, opts) {
|
|
124
|
+
const queue = [...agents];
|
|
125
|
+
const inflight = new Set();
|
|
126
|
+
while (queue.length > 0 || inflight.size > 0) {
|
|
127
|
+
while (inflight.size < opts.concurrency && queue.length > 0) {
|
|
128
|
+
const agent = queue.shift();
|
|
129
|
+
const p = runOneAgent(agent, opts).finally(() => { inflight.delete(p); });
|
|
130
|
+
inflight.add(p);
|
|
131
|
+
}
|
|
132
|
+
if (inflight.size > 0)
|
|
133
|
+
await Promise.race(inflight);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
async function runOneAgent(agent, opts) {
|
|
137
|
+
agent.status = 'running';
|
|
138
|
+
agent.startedAt = Date.now();
|
|
139
|
+
opts.onAgent?.(agent);
|
|
140
|
+
const sessionId = `fleet-${agent.id}-${Date.now().toString(36)}`;
|
|
141
|
+
agent.sessionId = sessionId;
|
|
142
|
+
emitFleetEvent(opts.events, {
|
|
143
|
+
type: 'fleet_agent_start',
|
|
144
|
+
agentId: agent.id,
|
|
145
|
+
subtask: agent.subtask,
|
|
146
|
+
worktreePath: agent.worktreePath,
|
|
147
|
+
branch: agent.branchName,
|
|
148
|
+
});
|
|
149
|
+
const controller = new AbortController();
|
|
150
|
+
const parentListener = () => { controller.abort(); };
|
|
151
|
+
opts.signal?.addEventListener('abort', parentListener, { once: true });
|
|
152
|
+
const timer = setTimeout(() => { controller.abort(); }, opts.timeoutMs);
|
|
153
|
+
const registry = agentRegistry(agent.subtask);
|
|
154
|
+
const sanitized = registry.sanitize({ descriptionLimit: 200 });
|
|
155
|
+
const executor = createToolExecutor({ registry, cwd: agent.worktreePath, sessionId });
|
|
156
|
+
// Per-agent local event stream — we relay into parent + collect usage.
|
|
157
|
+
const localEvents = createEventStream();
|
|
158
|
+
const unsubscribe = localEvents.subscribe(ev => {
|
|
159
|
+
opts.events.emit(ev);
|
|
160
|
+
onLocalEvent(agent, ev, opts);
|
|
161
|
+
});
|
|
162
|
+
const messages = [
|
|
163
|
+
{ role: 'system', content: agentSystemPrompt(agent.subtask) },
|
|
164
|
+
{ role: 'user', content: agent.subtask.task },
|
|
165
|
+
];
|
|
166
|
+
try {
|
|
167
|
+
const result = await runAgentLoop({
|
|
168
|
+
sessionId,
|
|
169
|
+
model: agent.subtask.model ?? opts.model,
|
|
170
|
+
messages,
|
|
171
|
+
tools: sanitized.definitions,
|
|
172
|
+
maxTurns: opts.maxTurns,
|
|
173
|
+
provider: opts.providers.forModel(agent.subtask.model ?? opts.model),
|
|
174
|
+
toolExecutor: executor,
|
|
175
|
+
events: localEvents,
|
|
176
|
+
signal: controller.signal,
|
|
177
|
+
});
|
|
178
|
+
agent.transcript = result.messages;
|
|
179
|
+
agent.stopReason = result.stopReason;
|
|
180
|
+
agent.usage = result.usage;
|
|
181
|
+
const lastAssistant = [...result.messages].reverse().find(m => m.role === 'assistant');
|
|
182
|
+
if (lastAssistant)
|
|
183
|
+
agent.output = extractText(lastAssistant);
|
|
184
|
+
if (result.stopReason === 'aborted') {
|
|
185
|
+
agent.status = 'cancelled';
|
|
186
|
+
agent.error = controller.signal.aborted && !opts.signal?.aborted
|
|
187
|
+
? `timeout after ${opts.timeoutMs}ms`
|
|
188
|
+
: 'cancelled';
|
|
189
|
+
}
|
|
190
|
+
else if (result.stopReason === 'error') {
|
|
191
|
+
agent.status = 'failed';
|
|
192
|
+
agent.error = agent.error ?? 'agent-loop reported error';
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
agent.status = 'completed';
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
catch (err) {
|
|
199
|
+
agent.status = 'failed';
|
|
200
|
+
agent.error = err instanceof Error ? err.message : String(err);
|
|
201
|
+
}
|
|
202
|
+
finally {
|
|
203
|
+
clearTimeout(timer);
|
|
204
|
+
opts.signal?.removeEventListener('abort', parentListener);
|
|
205
|
+
unsubscribe();
|
|
206
|
+
localEvents.close();
|
|
207
|
+
}
|
|
208
|
+
agent.completedAt = Date.now();
|
|
209
|
+
opts.onAgent?.(agent);
|
|
210
|
+
emitFleetEvent(opts.events, {
|
|
211
|
+
type: 'fleet_agent_end',
|
|
212
|
+
agentId: agent.id,
|
|
213
|
+
status: agent.status,
|
|
214
|
+
error: agent.error,
|
|
215
|
+
usage: agent.usage,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
/* Per-event side effects: text accumulation + verbose mirroring. */
|
|
219
|
+
function onLocalEvent(agent, ev, opts) {
|
|
220
|
+
if (ev.type === 'text_delta') {
|
|
221
|
+
agent.output += ev.delta;
|
|
222
|
+
agent.bytesWritten += ev.delta.length;
|
|
223
|
+
if (opts.verbose)
|
|
224
|
+
process.stderr.write(`[${agent.id}] ${ev.delta}`);
|
|
225
|
+
emitFleetEvent(opts.events, {
|
|
226
|
+
type: 'fleet_agent_progress',
|
|
227
|
+
agentId: agent.id,
|
|
228
|
+
status: agent.status,
|
|
229
|
+
bytes: agent.bytesWritten,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
else if (ev.type === 'error') {
|
|
233
|
+
agent.error = ev.message;
|
|
234
|
+
}
|
|
235
|
+
else if (ev.type === 'usage') {
|
|
236
|
+
agent.usage.inputTokens += ev.inputTokens;
|
|
237
|
+
agent.usage.outputTokens += ev.outputTokens;
|
|
238
|
+
agent.usage.cachedTokens += ev.cachedTokens ?? 0;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function agentRegistry(subtask) {
|
|
242
|
+
const type = subtask.type ?? 'code';
|
|
243
|
+
const allow = subtask.toolAllowlist ?? AGENT_TYPE_TOOLS[type];
|
|
244
|
+
const allowSet = new Set(allow);
|
|
245
|
+
const scoped = builtInTools.filter(t => allowSet.size === 0 ? true : allowSet.has(t.name));
|
|
246
|
+
return createToolRegistry(scoped);
|
|
247
|
+
}
|
|
248
|
+
function agentSystemPrompt(subtask) {
|
|
249
|
+
return `You are a fleet subagent focused on a single subtask.
|
|
250
|
+
Subtask: ${subtask.title}
|
|
251
|
+
Type: ${subtask.type ?? 'code'}
|
|
252
|
+
|
|
253
|
+
Stay inside your worktree (your cwd). Make the smallest change that
|
|
254
|
+
solves the subtask. When done, respond with a short summary of what you
|
|
255
|
+
changed or found — no apologies, no filler.`;
|
|
256
|
+
}
|
|
257
|
+
function emitFleetEvent(events, ev) {
|
|
258
|
+
// Fleet events are cast onto the AgentEvent stream so parent UIs that
|
|
259
|
+
// understand extended events can render them; ones that don't will
|
|
260
|
+
// treat them as opaque custom events via the `type` field.
|
|
261
|
+
events.emit(ev);
|
|
262
|
+
}
|
|
263
|
+
function defaultModel() {
|
|
264
|
+
return process.env['DIRGHA_MODEL'] ?? 'nvidia/minimaxai/minimax-m2.7';
|
|
265
|
+
}
|
|
266
|
+
function normalizeSubtask(s) {
|
|
267
|
+
return {
|
|
268
|
+
id: slug(s.id),
|
|
269
|
+
title: s.title.slice(0, 80),
|
|
270
|
+
task: s.task,
|
|
271
|
+
type: s.type ?? 'code',
|
|
272
|
+
model: s.model,
|
|
273
|
+
toolAllowlist: s.toolAllowlist,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
/* ------------------------- decomposition -------------------------- */
|
|
277
|
+
/**
|
|
278
|
+
* Decompose a goal into subtasks via an LLM call. Single-shot; falls back
|
|
279
|
+
* to a 1-element list ({ id:slug(goal), task:goal, type:'code' }) on any
|
|
280
|
+
* error or unparseable response.
|
|
281
|
+
*/
|
|
282
|
+
export async function decomposeGoal(goal, model, providers) {
|
|
283
|
+
const provider = providers.forModel(model);
|
|
284
|
+
const events = [];
|
|
285
|
+
const stream = provider.stream({
|
|
286
|
+
model,
|
|
287
|
+
messages: [
|
|
288
|
+
{ role: 'system', content: DECOMPOSE_SYSTEM },
|
|
289
|
+
{ role: 'user', content: `Goal: ${goal}\n\nDecompose into parallel subtasks as JSON.` },
|
|
290
|
+
],
|
|
291
|
+
});
|
|
292
|
+
try {
|
|
293
|
+
for await (const ev of stream)
|
|
294
|
+
events.push(ev);
|
|
295
|
+
}
|
|
296
|
+
catch {
|
|
297
|
+
return fallbackSubtask(goal);
|
|
298
|
+
}
|
|
299
|
+
const text = events
|
|
300
|
+
.filter((e) => e.type === 'text_delta')
|
|
301
|
+
.map(e => e.delta)
|
|
302
|
+
.join('');
|
|
303
|
+
return parseSubtasks(text, goal);
|
|
304
|
+
}
|
|
305
|
+
function parseSubtasks(raw, goal) {
|
|
306
|
+
const match = /\{[\s\S]*"subtasks"[\s\S]*\}/m.exec(raw);
|
|
307
|
+
if (!match)
|
|
308
|
+
return fallbackSubtask(goal);
|
|
309
|
+
const parsed = repairJSON(match[0]);
|
|
310
|
+
if (!parsed || typeof parsed !== 'object')
|
|
311
|
+
return fallbackSubtask(goal);
|
|
312
|
+
const raw2 = parsed.subtasks;
|
|
313
|
+
if (!Array.isArray(raw2) || raw2.length === 0)
|
|
314
|
+
return fallbackSubtask(goal);
|
|
315
|
+
const out = [];
|
|
316
|
+
const seenIds = new Set();
|
|
317
|
+
for (const item of raw2.slice(0, 5)) {
|
|
318
|
+
if (!item || typeof item !== 'object')
|
|
319
|
+
continue;
|
|
320
|
+
const o = item;
|
|
321
|
+
const baseId = typeof o.id === 'string' ? o.id : `task-${out.length + 1}`;
|
|
322
|
+
let id = slug(baseId, 30);
|
|
323
|
+
while (seenIds.has(id))
|
|
324
|
+
id = `${id}-${out.length + 1}`;
|
|
325
|
+
seenIds.add(id);
|
|
326
|
+
const title = typeof o.title === 'string'
|
|
327
|
+
? o.title.slice(0, 80)
|
|
328
|
+
: (typeof o.task === 'string' ? o.task.slice(0, 80) : `Subtask ${out.length + 1}`);
|
|
329
|
+
const task = typeof o.task === 'string' ? o.task : (typeof o.title === 'string' ? o.title : goal);
|
|
330
|
+
const type = isAgentType(o.type) ? o.type : 'code';
|
|
331
|
+
out.push({ id, title, task, type });
|
|
332
|
+
}
|
|
333
|
+
return out.length > 0 ? out : fallbackSubtask(goal);
|
|
334
|
+
}
|
|
335
|
+
function isAgentType(v) {
|
|
336
|
+
return v === 'explore' || v === 'plan' || v === 'verify'
|
|
337
|
+
|| v === 'code' || v === 'research' || v === 'custom';
|
|
338
|
+
}
|
|
339
|
+
function fallbackSubtask(goal) {
|
|
340
|
+
return [{
|
|
341
|
+
id: slug(goal) || 'main',
|
|
342
|
+
title: goal.slice(0, 80),
|
|
343
|
+
task: goal,
|
|
344
|
+
type: 'code',
|
|
345
|
+
}];
|
|
346
|
+
}
|
|
347
|
+
//# sourceMappingURL=runner.js.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* fleet/tripleshot.ts — Spawn 3 stylistic variants, ask a judge, pick
|
|
3
|
+
* the winner.
|
|
4
|
+
*
|
|
5
|
+
* For high-stakes tasks: the same goal is sent to three agents with
|
|
6
|
+
* different stylistic framings (conservative / balanced / bold), each
|
|
7
|
+
* in its own worktree. A judge agent is then asked to read the diffs
|
|
8
|
+
* and pick the best one. Optionally auto-applies the winner back.
|
|
9
|
+
*
|
|
10
|
+
* The judge prompt is intentionally small and produces strict JSON so
|
|
11
|
+
* parsing is deterministic; we fall back to the first completed variant
|
|
12
|
+
* when the judge misbehaves.
|
|
13
|
+
*/
|
|
14
|
+
import type { FleetConfig, TripleshotResult } from './types.js';
|
|
15
|
+
export interface TripleshotConfig extends Omit<FleetConfig, 'subtasks' | 'concurrency'> {
|
|
16
|
+
/** Auto-apply the winner's diff to the parent tree via applyBack. */
|
|
17
|
+
autoMerge?: boolean;
|
|
18
|
+
/** Override the judge model; defaults to `plannerModel` or `model`. */
|
|
19
|
+
judgeModel?: string;
|
|
20
|
+
}
|
|
21
|
+
export declare function runTripleshot(goal: string, config: TripleshotConfig): Promise<TripleshotResult>;
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* fleet/tripleshot.ts — Spawn 3 stylistic variants, ask a judge, pick
|
|
3
|
+
* the winner.
|
|
4
|
+
*
|
|
5
|
+
* For high-stakes tasks: the same goal is sent to three agents with
|
|
6
|
+
* different stylistic framings (conservative / balanced / bold), each
|
|
7
|
+
* in its own worktree. A judge agent is then asked to read the diffs
|
|
8
|
+
* and pick the best one. Optionally auto-applies the winner back.
|
|
9
|
+
*
|
|
10
|
+
* The judge prompt is intentionally small and produces strict JSON so
|
|
11
|
+
* parsing is deterministic; we fall back to the first completed variant
|
|
12
|
+
* when the judge misbehaves.
|
|
13
|
+
*/
|
|
14
|
+
import { runFleet } from './runner.js';
|
|
15
|
+
import { applyBack } from './apply-back.js';
|
|
16
|
+
import { getHeadSha } from './worktree.js';
|
|
17
|
+
import { execFile } from 'node:child_process';
|
|
18
|
+
import { promisify } from 'node:util';
|
|
19
|
+
import { ProviderRegistry } from '../providers/index.js';
|
|
20
|
+
import { createEventStream } from '../kernel/event-stream.js';
|
|
21
|
+
import { repairJSON } from '../utils/json-repair.js';
|
|
22
|
+
const pexec = promisify(execFile);
|
|
23
|
+
const VARIATIONS = [
|
|
24
|
+
{
|
|
25
|
+
id: 'conservative',
|
|
26
|
+
style: 'Prioritize minimal changes, safety, and backward compatibility. Prefer the smallest diff that satisfies the goal.',
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
id: 'balanced',
|
|
30
|
+
style: 'Balance correctness with cleanliness. Refactor only when it clearly improves readability and stay focused on the stated goal.',
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
id: 'bold',
|
|
34
|
+
style: 'Optimize for long-term code quality. Aggressively refactor adjacent code that is clearly improved by the change, without breaking public APIs.',
|
|
35
|
+
},
|
|
36
|
+
];
|
|
37
|
+
const JUDGE_SYSTEM = `You are a strict code-review judge. You will be shown a goal and up to 3 candidate diffs from variants named "conservative", "balanced", and "bold".
|
|
38
|
+
|
|
39
|
+
Pick the BEST one by:
|
|
40
|
+
- Correctness — does it actually solve the goal?
|
|
41
|
+
- Simplicity — smallest diff that is still correct.
|
|
42
|
+
- Style fit — consistent with the apparent repo conventions.
|
|
43
|
+
- No regressions — don't break adjacent code.
|
|
44
|
+
|
|
45
|
+
Output STRICT JSON only, no prose, no markdown:
|
|
46
|
+
{ "winner": "conservative|balanced|bold", "runner_up": "conservative|balanced|bold", "reason": "<1-2 sentences>" }`;
|
|
47
|
+
export async function runTripleshot(goal, config) {
|
|
48
|
+
const started = Date.now();
|
|
49
|
+
const subtasks = VARIATIONS.map(v => ({
|
|
50
|
+
id: `${v.id}`,
|
|
51
|
+
title: `[${v.id}] ${goal}`.slice(0, 80),
|
|
52
|
+
task: `${goal}\n\nStylistic guidance: ${v.style}`,
|
|
53
|
+
type: 'code',
|
|
54
|
+
}));
|
|
55
|
+
const fleetResult = await runFleet({
|
|
56
|
+
...config,
|
|
57
|
+
goal,
|
|
58
|
+
subtasks,
|
|
59
|
+
concurrency: 3,
|
|
60
|
+
});
|
|
61
|
+
const providers = new ProviderRegistry();
|
|
62
|
+
const judgeModel = config.judgeModel ?? config.plannerModel ?? config.model ?? defaultModel();
|
|
63
|
+
const events = config.events ?? createEventStream();
|
|
64
|
+
const shots = [];
|
|
65
|
+
const repoRoot = fleetResult.worktrees[0]?.repoRoot;
|
|
66
|
+
const parentHead = repoRoot ? await getHeadSha(repoRoot) : '';
|
|
67
|
+
for (const a of fleetResult.agents) {
|
|
68
|
+
if (a.status !== 'completed')
|
|
69
|
+
continue;
|
|
70
|
+
const variant = VARIATIONS.find(v => a.id === v.id)?.id;
|
|
71
|
+
if (!variant)
|
|
72
|
+
continue;
|
|
73
|
+
let diff = '';
|
|
74
|
+
try {
|
|
75
|
+
await pexec('git', ['add', '-A'], { cwd: a.worktreePath });
|
|
76
|
+
const { stdout } = await pexec('git', ['diff', parentHead, '--'], {
|
|
77
|
+
cwd: a.worktreePath,
|
|
78
|
+
maxBuffer: 20 * 1024 * 1024,
|
|
79
|
+
});
|
|
80
|
+
diff = stdout;
|
|
81
|
+
}
|
|
82
|
+
catch { /* skip empty */ }
|
|
83
|
+
if (diff.trim()) {
|
|
84
|
+
shots.push({ variant, agent: a, diff: diff.slice(0, 8000) });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const tokens = { ...fleetResult.totalTokens };
|
|
88
|
+
if (shots.length === 0) {
|
|
89
|
+
return {
|
|
90
|
+
goal,
|
|
91
|
+
winner: null,
|
|
92
|
+
runnerUp: null,
|
|
93
|
+
reason: 'No variant produced a diff',
|
|
94
|
+
shots: [],
|
|
95
|
+
agents: fleetResult.agents,
|
|
96
|
+
worktrees: fleetResult.worktrees,
|
|
97
|
+
totalTokens: tokens,
|
|
98
|
+
durationMs: Date.now() - started,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
if (shots.length === 1) {
|
|
102
|
+
const only = shots[0];
|
|
103
|
+
const ab = config.autoMerge
|
|
104
|
+
? await applyBack(handleFor(fleetResult.worktrees, only.agent.worktreePath), {
|
|
105
|
+
message: `triple: ${only.variant} (${goal.slice(0, 40)})`,
|
|
106
|
+
})
|
|
107
|
+
: undefined;
|
|
108
|
+
return {
|
|
109
|
+
goal,
|
|
110
|
+
winner: only.variant,
|
|
111
|
+
runnerUp: null,
|
|
112
|
+
reason: 'Only one variant completed',
|
|
113
|
+
shots,
|
|
114
|
+
agents: fleetResult.agents,
|
|
115
|
+
worktrees: fleetResult.worktrees,
|
|
116
|
+
apply: ab,
|
|
117
|
+
totalTokens: tokens,
|
|
118
|
+
durationMs: Date.now() - started,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
const { winner, runnerUp, reason, usage: judgeUsage } = await askJudge(goal, shots, judgeModel, providers, events);
|
|
122
|
+
accumulate(tokens, judgeUsage);
|
|
123
|
+
let apply;
|
|
124
|
+
if (config.autoMerge && winner) {
|
|
125
|
+
const winShot = shots.find(s => s.variant === winner);
|
|
126
|
+
if (winShot) {
|
|
127
|
+
const handle = handleFor(fleetResult.worktrees, winShot.agent.worktreePath);
|
|
128
|
+
apply = await applyBack(handle, { message: `triple: ${winner} (${goal.slice(0, 40)})` });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
goal,
|
|
133
|
+
winner,
|
|
134
|
+
runnerUp,
|
|
135
|
+
reason,
|
|
136
|
+
shots,
|
|
137
|
+
agents: fleetResult.agents,
|
|
138
|
+
worktrees: fleetResult.worktrees,
|
|
139
|
+
apply,
|
|
140
|
+
totalTokens: tokens,
|
|
141
|
+
durationMs: Date.now() - started,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
async function askJudge(goal, shots, judgeModel, providers, events) {
|
|
145
|
+
const prompt = `GOAL: ${goal}\n\n` +
|
|
146
|
+
shots.map(s => `=== ${s.variant.toUpperCase()} ===\n${s.diff}\n`).join('\n') +
|
|
147
|
+
`\nPick the best variant. Output JSON only.`;
|
|
148
|
+
const provider = providers.forModel(judgeModel);
|
|
149
|
+
const streamEvents = [];
|
|
150
|
+
try {
|
|
151
|
+
for await (const ev of provider.stream({
|
|
152
|
+
model: judgeModel,
|
|
153
|
+
messages: [
|
|
154
|
+
{ role: 'system', content: JUDGE_SYSTEM },
|
|
155
|
+
{ role: 'user', content: prompt },
|
|
156
|
+
],
|
|
157
|
+
})) {
|
|
158
|
+
streamEvents.push(ev);
|
|
159
|
+
events.emit(ev);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
catch (err) {
|
|
163
|
+
return {
|
|
164
|
+
winner: shots[0]?.variant ?? null,
|
|
165
|
+
runnerUp: shots[1]?.variant ?? null,
|
|
166
|
+
reason: `Judge error: ${err instanceof Error ? err.message : String(err)}`,
|
|
167
|
+
usage: { inputTokens: 0, outputTokens: 0, cachedTokens: 0, costUsd: 0 },
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
const text = streamEvents
|
|
171
|
+
.filter((e) => e.type === 'text_delta')
|
|
172
|
+
.map(e => e.delta)
|
|
173
|
+
.join('');
|
|
174
|
+
const usage = streamEvents
|
|
175
|
+
.filter((e) => e.type === 'usage')
|
|
176
|
+
.reduce((acc, e) => ({
|
|
177
|
+
inputTokens: acc.inputTokens + e.inputTokens,
|
|
178
|
+
outputTokens: acc.outputTokens + e.outputTokens,
|
|
179
|
+
cachedTokens: acc.cachedTokens + (e.cachedTokens ?? 0),
|
|
180
|
+
costUsd: acc.costUsd,
|
|
181
|
+
}), { inputTokens: 0, outputTokens: 0, cachedTokens: 0, costUsd: 0 });
|
|
182
|
+
return parseVerdict(text, shots, usage);
|
|
183
|
+
}
|
|
184
|
+
function parseVerdict(text, shots, usage) {
|
|
185
|
+
const defaultVerdict = {
|
|
186
|
+
winner: shots[0]?.variant ?? null,
|
|
187
|
+
runnerUp: shots[1]?.variant ?? null,
|
|
188
|
+
reason: 'Judge output unparseable; defaulted to first completed variant.',
|
|
189
|
+
usage,
|
|
190
|
+
};
|
|
191
|
+
const match = /\{[\s\S]*?\}/.exec(text);
|
|
192
|
+
if (!match)
|
|
193
|
+
return defaultVerdict;
|
|
194
|
+
const parsed = repairJSON(match[0]);
|
|
195
|
+
if (!parsed || typeof parsed !== 'object')
|
|
196
|
+
return defaultVerdict;
|
|
197
|
+
const rec = parsed;
|
|
198
|
+
const winner = isVariant(rec.winner) ? rec.winner : defaultVerdict.winner;
|
|
199
|
+
const runnerUp = isVariant(rec.runner_up) ? rec.runner_up : defaultVerdict.runnerUp;
|
|
200
|
+
const reason = typeof rec.reason === 'string' ? rec.reason : 'No reason given.';
|
|
201
|
+
return { winner, runnerUp: winner === runnerUp ? null : runnerUp, reason, usage };
|
|
202
|
+
}
|
|
203
|
+
function isVariant(v) {
|
|
204
|
+
return v === 'conservative' || v === 'balanced' || v === 'bold';
|
|
205
|
+
}
|
|
206
|
+
function accumulate(total, add) {
|
|
207
|
+
total.inputTokens += add.inputTokens;
|
|
208
|
+
total.outputTokens += add.outputTokens;
|
|
209
|
+
total.cachedTokens += add.cachedTokens;
|
|
210
|
+
total.costUsd += add.costUsd;
|
|
211
|
+
}
|
|
212
|
+
function handleFor(worktrees, path) {
|
|
213
|
+
const wt = worktrees.find(w => w.path === path);
|
|
214
|
+
if (!wt) {
|
|
215
|
+
throw new Error(`tripleshot: no worktree handle for path ${path}`);
|
|
216
|
+
}
|
|
217
|
+
return wt;
|
|
218
|
+
}
|
|
219
|
+
function defaultModel() {
|
|
220
|
+
return process.env['DIRGHA_MODEL'] ?? 'nvidia/minimaxai/minimax-m2.7';
|
|
221
|
+
}
|
|
222
|
+
//# sourceMappingURL=tripleshot.js.map
|