@wahack/pi-coding-agent 15.11.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/CHANGELOG.md +10031 -0
- package/README.md +36 -0
- package/examples/README.md +21 -0
- package/examples/custom-tools/README.md +104 -0
- package/examples/custom-tools/hello/index.ts +20 -0
- package/examples/extensions/README.md +142 -0
- package/examples/extensions/api-demo.ts +79 -0
- package/examples/extensions/chalk-logger.ts +25 -0
- package/examples/extensions/hello.ts +31 -0
- package/examples/extensions/pirate.ts +43 -0
- package/examples/extensions/plan-mode.ts +549 -0
- package/examples/extensions/reload-runtime.ts +38 -0
- package/examples/extensions/thinking-note.ts +13 -0
- package/examples/extensions/tools.ts +145 -0
- package/examples/extensions/with-deps/index.ts +36 -0
- package/examples/extensions/with-deps/package-lock.json +31 -0
- package/examples/extensions/with-deps/package.json +17 -0
- package/examples/hooks/README.md +56 -0
- package/examples/hooks/auto-commit-on-exit.ts +48 -0
- package/examples/hooks/confirm-destructive.ts +58 -0
- package/examples/hooks/custom-compaction.ts +115 -0
- package/examples/hooks/dirty-repo-guard.ts +51 -0
- package/examples/hooks/file-trigger.ts +40 -0
- package/examples/hooks/git-checkpoint.ts +52 -0
- package/examples/hooks/handoff.ts +149 -0
- package/examples/hooks/permission-gate.ts +33 -0
- package/examples/hooks/protected-paths.ts +29 -0
- package/examples/hooks/qna.ts +118 -0
- package/examples/hooks/status-line.ts +39 -0
- package/examples/sdk/01-minimal.ts +21 -0
- package/examples/sdk/02-custom-model.ts +49 -0
- package/examples/sdk/03-custom-prompt.ts +46 -0
- package/examples/sdk/04-skills.ts +43 -0
- package/examples/sdk/06-extensions.ts +82 -0
- package/examples/sdk/06-hooks.ts +61 -0
- package/examples/sdk/07-context-files.ts +35 -0
- package/examples/sdk/08-prompt-templates.ts +41 -0
- package/examples/sdk/08-slash-commands.ts +46 -0
- package/examples/sdk/09-api-keys-and-oauth.ts +54 -0
- package/examples/sdk/11-sessions.ts +47 -0
- package/examples/sdk/12-redis-sessions.ts +54 -0
- package/examples/sdk/13-sql-sessions.ts +61 -0
- package/examples/sdk/README.md +172 -0
- package/package.json +554 -0
- package/scripts/build-binary.ts +100 -0
- package/scripts/bundle-dist.ts +90 -0
- package/scripts/format-prompts.ts +68 -0
- package/scripts/generate-docs-index.ts +40 -0
- package/scripts/generate-template.ts +33 -0
- package/scripts/omp +42 -0
- package/scripts/omp.ts +19 -0
- package/src/async/index.ts +1 -0
- package/src/async/job-manager.ts +625 -0
- package/src/auto-thinking/classifier.ts +185 -0
- package/src/autoresearch/command-resume.md +14 -0
- package/src/autoresearch/dashboard.ts +436 -0
- package/src/autoresearch/git.ts +319 -0
- package/src/autoresearch/helpers.ts +218 -0
- package/src/autoresearch/index.ts +536 -0
- package/src/autoresearch/prompt-setup.md +43 -0
- package/src/autoresearch/prompt.md +103 -0
- package/src/autoresearch/resume-message.md +10 -0
- package/src/autoresearch/state.ts +273 -0
- package/src/autoresearch/storage.ts +699 -0
- package/src/autoresearch/tools/init-experiment.ts +272 -0
- package/src/autoresearch/tools/log-experiment.ts +524 -0
- package/src/autoresearch/tools/run-experiment.ts +407 -0
- package/src/autoresearch/tools/update-notes.ts +109 -0
- package/src/autoresearch/types.ts +168 -0
- package/src/bun-imports.d.ts +28 -0
- package/src/capability/context-file.ts +44 -0
- package/src/capability/extension-module.ts +34 -0
- package/src/capability/extension.ts +47 -0
- package/src/capability/fs.ts +117 -0
- package/src/capability/hook.ts +40 -0
- package/src/capability/index.ts +436 -0
- package/src/capability/instruction.ts +37 -0
- package/src/capability/mcp.ts +74 -0
- package/src/capability/prompt.ts +35 -0
- package/src/capability/rule-buckets.ts +66 -0
- package/src/capability/rule.ts +261 -0
- package/src/capability/settings.ts +34 -0
- package/src/capability/skill.ts +63 -0
- package/src/capability/slash-command.ts +40 -0
- package/src/capability/ssh.ts +41 -0
- package/src/capability/system-prompt.ts +34 -0
- package/src/capability/tool.ts +38 -0
- package/src/capability/types.ts +168 -0
- package/src/cli/agents-cli.ts +138 -0
- package/src/cli/args.ts +340 -0
- package/src/cli/auth-broker-cli.ts +895 -0
- package/src/cli/auth-gateway-cli.ts +611 -0
- package/src/cli/classify-install-target.ts +76 -0
- package/src/cli/claude-trace-cli.ts +795 -0
- package/src/cli/commands/init-xdg.ts +27 -0
- package/src/cli/completion-gen.ts +550 -0
- package/src/cli/config-cli.ts +418 -0
- package/src/cli/dry-balance-cli.ts +856 -0
- package/src/cli/extension-flags.ts +48 -0
- package/src/cli/file-processor.ts +133 -0
- package/src/cli/gallery-cli.ts +230 -0
- package/src/cli/gallery-fixtures/agentic.ts +407 -0
- package/src/cli/gallery-fixtures/codeintel.ts +187 -0
- package/src/cli/gallery-fixtures/edit.ts +194 -0
- package/src/cli/gallery-fixtures/fs.ts +220 -0
- package/src/cli/gallery-fixtures/index.ts +40 -0
- package/src/cli/gallery-fixtures/interaction.ts +49 -0
- package/src/cli/gallery-fixtures/memory.ts +81 -0
- package/src/cli/gallery-fixtures/misc.ts +250 -0
- package/src/cli/gallery-fixtures/search.ts +213 -0
- package/src/cli/gallery-fixtures/shell.ts +167 -0
- package/src/cli/gallery-fixtures/types.ts +57 -0
- package/src/cli/gallery-fixtures/web.ts +158 -0
- package/src/cli/gallery-screenshot.ts +279 -0
- package/src/cli/grep-cli.ts +160 -0
- package/src/cli/grievances-cli.ts +256 -0
- package/src/cli/initial-message.ts +58 -0
- package/src/cli/list-models.ts +194 -0
- package/src/cli/plugin-cli.ts +996 -0
- package/src/cli/read-cli.ts +57 -0
- package/src/cli/session-picker.ts +79 -0
- package/src/cli/setup-cli.ts +231 -0
- package/src/cli/shell-cli.ts +176 -0
- package/src/cli/ssh-cli.ts +179 -0
- package/src/cli/startup-cwd.ts +68 -0
- package/src/cli/stats-cli.ts +238 -0
- package/src/cli/tiny-models-cli.ts +127 -0
- package/src/cli/update-cli.ts +611 -0
- package/src/cli/usage-cli.ts +603 -0
- package/src/cli/web-search-cli.ts +132 -0
- package/src/cli/worktree-cli.ts +291 -0
- package/src/cli-commands.ts +79 -0
- package/src/cli.ts +200 -0
- package/src/commands/acp.ts +24 -0
- package/src/commands/agents.ts +57 -0
- package/src/commands/auth-broker.ts +99 -0
- package/src/commands/auth-gateway.ts +69 -0
- package/src/commands/commit.ts +46 -0
- package/src/commands/complete.ts +66 -0
- package/src/commands/completions.ts +60 -0
- package/src/commands/config.ts +51 -0
- package/src/commands/dry-balance.ts +43 -0
- package/src/commands/gallery.ts +52 -0
- package/src/commands/grep.ts +48 -0
- package/src/commands/grievances.ts +51 -0
- package/src/commands/install.ts +107 -0
- package/src/commands/launch.ts +169 -0
- package/src/commands/plugin.ts +78 -0
- package/src/commands/read.ts +38 -0
- package/src/commands/setup.ts +67 -0
- package/src/commands/shell.ts +29 -0
- package/src/commands/ssh.ts +60 -0
- package/src/commands/stats.ts +29 -0
- package/src/commands/tiny-models.ts +36 -0
- package/src/commands/update.ts +21 -0
- package/src/commands/usage.ts +35 -0
- package/src/commands/web-search.ts +42 -0
- package/src/commands/worktree.ts +56 -0
- package/src/commit/agentic/agent.ts +317 -0
- package/src/commit/agentic/fallback.ts +96 -0
- package/src/commit/agentic/index.ts +355 -0
- package/src/commit/agentic/prompts/analyze-file.md +22 -0
- package/src/commit/agentic/prompts/session-user.md +25 -0
- package/src/commit/agentic/prompts/split-confirm.md +1 -0
- package/src/commit/agentic/prompts/system.md +38 -0
- package/src/commit/agentic/state.ts +60 -0
- package/src/commit/agentic/tools/analyze-file.ts +146 -0
- package/src/commit/agentic/tools/git-file-diff.ts +191 -0
- package/src/commit/agentic/tools/git-hunk.ts +50 -0
- package/src/commit/agentic/tools/git-overview.ts +81 -0
- package/src/commit/agentic/tools/index.ts +54 -0
- package/src/commit/agentic/tools/propose-changelog.ts +144 -0
- package/src/commit/agentic/tools/propose-commit.ts +109 -0
- package/src/commit/agentic/tools/recent-commits.ts +81 -0
- package/src/commit/agentic/tools/schemas.ts +23 -0
- package/src/commit/agentic/tools/split-commit.ts +245 -0
- package/src/commit/agentic/topo-sort.ts +44 -0
- package/src/commit/agentic/trivial.ts +51 -0
- package/src/commit/agentic/validation.ts +183 -0
- package/src/commit/analysis/conventional.ts +64 -0
- package/src/commit/analysis/index.ts +4 -0
- package/src/commit/analysis/scope.ts +242 -0
- package/src/commit/analysis/summary.ts +105 -0
- package/src/commit/analysis/validation.ts +66 -0
- package/src/commit/changelog/detect.ts +40 -0
- package/src/commit/changelog/generate.ts +97 -0
- package/src/commit/changelog/index.ts +234 -0
- package/src/commit/changelog/parse.ts +44 -0
- package/src/commit/cli.ts +85 -0
- package/src/commit/git/diff.ts +148 -0
- package/src/commit/index.ts +5 -0
- package/src/commit/map-reduce/index.ts +69 -0
- package/src/commit/map-reduce/map-phase.ts +193 -0
- package/src/commit/map-reduce/reduce-phase.ts +49 -0
- package/src/commit/map-reduce/utils.ts +9 -0
- package/src/commit/message.ts +11 -0
- package/src/commit/model-selection.ts +92 -0
- package/src/commit/pipeline.ts +243 -0
- package/src/commit/prompts/analysis-system.md +148 -0
- package/src/commit/prompts/analysis-user.md +38 -0
- package/src/commit/prompts/changelog-system.md +50 -0
- package/src/commit/prompts/changelog-user.md +18 -0
- package/src/commit/prompts/file-observer-system.md +24 -0
- package/src/commit/prompts/file-observer-user.md +8 -0
- package/src/commit/prompts/reduce-system.md +50 -0
- package/src/commit/prompts/reduce-user.md +17 -0
- package/src/commit/prompts/summary-retry.md +3 -0
- package/src/commit/prompts/summary-system.md +38 -0
- package/src/commit/prompts/summary-user.md +13 -0
- package/src/commit/prompts/types-description.md +2 -0
- package/src/commit/shared-llm.ts +77 -0
- package/src/commit/types.ts +118 -0
- package/src/commit/utils/exclusions.ts +42 -0
- package/src/commit/utils.ts +58 -0
- package/src/config/api-key-resolver.ts +60 -0
- package/src/config/append-only-context-mode.ts +31 -0
- package/src/config/config-file.ts +317 -0
- package/src/config/file-lock.ts +164 -0
- package/src/config/keybindings.ts +628 -0
- package/src/config/mcp-schema.json +230 -0
- package/src/config/model-discovery.ts +554 -0
- package/src/config/model-registry.ts +2090 -0
- package/src/config/model-resolver.ts +1502 -0
- package/src/config/model-roles.ts +74 -0
- package/src/config/models-config-schema.ts +226 -0
- package/src/config/models-config.ts +129 -0
- package/src/config/prompt-templates.ts +185 -0
- package/src/config/resolve-config-value.ts +94 -0
- package/src/config/settings-schema.ts +3530 -0
- package/src/config/settings.ts +1178 -0
- package/src/config.ts +242 -0
- package/src/cursor.ts +340 -0
- package/src/dap/client.ts +760 -0
- package/src/dap/config.ts +189 -0
- package/src/dap/defaults.json +212 -0
- package/src/dap/index.ts +4 -0
- package/src/dap/session.ts +1441 -0
- package/src/dap/types.ts +610 -0
- package/src/debug/index.ts +515 -0
- package/src/debug/log-formatting.ts +58 -0
- package/src/debug/log-viewer.ts +908 -0
- package/src/debug/profiler.ts +162 -0
- package/src/debug/protocol-probe.ts +267 -0
- package/src/debug/raw-sse-buffer.ts +273 -0
- package/src/debug/raw-sse.ts +292 -0
- package/src/debug/report-bundle.ts +374 -0
- package/src/debug/system-info.ts +111 -0
- package/src/debug/terminal-info.ts +124 -0
- package/src/discovery/agents-md.ts +67 -0
- package/src/discovery/agents.ts +230 -0
- package/src/discovery/at-imports.ts +273 -0
- package/src/discovery/builtin-defaults.ts +39 -0
- package/src/discovery/builtin-rules/index.ts +54 -0
- package/src/discovery/builtin-rules/rs-box-leak.md +48 -0
- package/src/discovery/builtin-rules/rs-future-prelude.md +23 -0
- package/src/discovery/builtin-rules/rs-lazylock.md +51 -0
- package/src/discovery/builtin-rules/rs-match-ergonomics.md +67 -0
- package/src/discovery/builtin-rules/rs-parking-lot.md +44 -0
- package/src/discovery/builtin-rules/rs-result-type.md +19 -0
- package/src/discovery/builtin-rules/ts-bare-catch.md +38 -0
- package/src/discovery/builtin-rules/ts-import-type.md +42 -0
- package/src/discovery/builtin-rules/ts-no-any.md +56 -0
- package/src/discovery/builtin-rules/ts-no-deprecated-leftovers.md +44 -0
- package/src/discovery/builtin-rules/ts-no-dynamic-import.md +39 -0
- package/src/discovery/builtin-rules/ts-no-return-type.md +45 -0
- package/src/discovery/builtin-rules/ts-no-test-timers.md +55 -0
- package/src/discovery/builtin-rules/ts-no-tiny-functions.md +51 -0
- package/src/discovery/builtin-rules/ts-promise-with-resolvers.md +65 -0
- package/src/discovery/builtin-rules/ts-redundant-clear-guard.md +75 -0
- package/src/discovery/builtin-rules/ts-set-map.md +28 -0
- package/src/discovery/builtin.ts +906 -0
- package/src/discovery/claude-plugins.ts +386 -0
- package/src/discovery/claude.ts +584 -0
- package/src/discovery/cline.ts +83 -0
- package/src/discovery/codex.ts +522 -0
- package/src/discovery/cursor.ts +220 -0
- package/src/discovery/gemini.ts +383 -0
- package/src/discovery/github.ts +154 -0
- package/src/discovery/helpers.ts +1016 -0
- package/src/discovery/index.ts +81 -0
- package/src/discovery/mcp-json.ts +171 -0
- package/src/discovery/omp-extension-roots.ts +190 -0
- package/src/discovery/omp-plugins.ts +383 -0
- package/src/discovery/opencode.ts +398 -0
- package/src/discovery/plugin-dir-roots.ts +28 -0
- package/src/discovery/ssh.ts +153 -0
- package/src/discovery/substitute-plugin-root.ts +29 -0
- package/src/discovery/vscode.ts +105 -0
- package/src/discovery/windsurf.ts +147 -0
- package/src/edit/apply-patch/index.ts +87 -0
- package/src/edit/apply-patch/parser.ts +174 -0
- package/src/edit/diff.ts +999 -0
- package/src/edit/file-snapshot-store.ts +91 -0
- package/src/edit/hashline/block-resolver.ts +33 -0
- package/src/edit/hashline/diff.ts +290 -0
- package/src/edit/hashline/execute.ts +242 -0
- package/src/edit/hashline/filesystem.ts +130 -0
- package/src/edit/hashline/index.ts +5 -0
- package/src/edit/hashline/noop-loop-guard.ts +99 -0
- package/src/edit/hashline/params.ts +18 -0
- package/src/edit/index.ts +571 -0
- package/src/edit/modes/apply-patch.lark +19 -0
- package/src/edit/modes/apply-patch.ts +53 -0
- package/src/edit/modes/patch.ts +1891 -0
- package/src/edit/modes/replace.ts +1137 -0
- package/src/edit/normalize.ts +345 -0
- package/src/edit/notebook.ts +242 -0
- package/src/edit/read-file.ts +25 -0
- package/src/edit/renderer.ts +769 -0
- package/src/edit/streaming.ts +517 -0
- package/src/eval/__tests__/agent-bridge.test.ts +708 -0
- package/src/eval/__tests__/bridge-timeout.test.ts +64 -0
- package/src/eval/__tests__/budget-bridge.test.ts +69 -0
- package/src/eval/__tests__/completion-bridge.test.ts +412 -0
- package/src/eval/__tests__/helpers-local-roots.test.ts +58 -0
- package/src/eval/__tests__/idle-timeout.test.ts +80 -0
- package/src/eval/__tests__/js-context-manager.test.ts +241 -0
- package/src/eval/__tests__/kernel-spawn.test.ts +103 -0
- package/src/eval/agent-bridge.ts +319 -0
- package/src/eval/backend.ts +71 -0
- package/src/eval/bridge-timeout.ts +44 -0
- package/src/eval/budget-bridge.ts +48 -0
- package/src/eval/completion-bridge.ts +207 -0
- package/src/eval/concurrency-bridge.ts +34 -0
- package/src/eval/idle-timeout.ts +91 -0
- package/src/eval/index.ts +4 -0
- package/src/eval/js/context-manager.ts +502 -0
- package/src/eval/js/executor.ts +173 -0
- package/src/eval/js/index.ts +51 -0
- package/src/eval/js/shared/helpers.ts +283 -0
- package/src/eval/js/shared/indirect-eval.ts +30 -0
- package/src/eval/js/shared/local-module-loader.ts +342 -0
- package/src/eval/js/shared/prelude.ts +2 -0
- package/src/eval/js/shared/prelude.txt +246 -0
- package/src/eval/js/shared/rewrite-imports.ts +532 -0
- package/src/eval/js/shared/runtime.ts +352 -0
- package/src/eval/js/shared/types.ts +18 -0
- package/src/eval/js/tool-bridge.ts +162 -0
- package/src/eval/js/worker-core.ts +132 -0
- package/src/eval/js/worker-entry.ts +30 -0
- package/src/eval/js/worker-protocol.ts +47 -0
- package/src/eval/py/__tests__/prelude.test.ts +19 -0
- package/src/eval/py/display.ts +71 -0
- package/src/eval/py/executor.ts +742 -0
- package/src/eval/py/index.ts +68 -0
- package/src/eval/py/kernel.ts +748 -0
- package/src/eval/py/prelude.py +658 -0
- package/src/eval/py/prelude.ts +3 -0
- package/src/eval/py/runner.py +1133 -0
- package/src/eval/py/runtime.ts +276 -0
- package/src/eval/py/spawn-options.ts +126 -0
- package/src/eval/py/tool-bridge.ts +182 -0
- package/src/eval/session-id.ts +8 -0
- package/src/eval/types.ts +48 -0
- package/src/exa/index.ts +2 -0
- package/src/exa/mcp-client.ts +370 -0
- package/src/exa/types.ts +69 -0
- package/src/exec/bash-executor.ts +419 -0
- package/src/exec/exec.ts +53 -0
- package/src/exec/non-interactive-env.ts +48 -0
- package/src/export/custom-share.ts +65 -0
- package/src/export/html/index.ts +164 -0
- package/src/export/html/template.css +1051 -0
- package/src/export/html/template.generated.ts +2 -0
- package/src/export/html/template.html +46 -0
- package/src/export/html/template.js +2271 -0
- package/src/export/html/template.macro.ts +25 -0
- package/src/export/html/vendor/highlight.min.js +1213 -0
- package/src/export/html/vendor/marked.min.js +6 -0
- package/src/export/ttsr.ts +583 -0
- package/src/extensibility/custom-commands/bundled/ci-green/index.ts +54 -0
- package/src/extensibility/custom-commands/bundled/review/index.ts +489 -0
- package/src/extensibility/custom-commands/index.ts +2 -0
- package/src/extensibility/custom-commands/loader.ts +238 -0
- package/src/extensibility/custom-commands/types.ts +113 -0
- package/src/extensibility/custom-tools/index.ts +7 -0
- package/src/extensibility/custom-tools/loader.ts +269 -0
- package/src/extensibility/custom-tools/types.ts +270 -0
- package/src/extensibility/custom-tools/wrapper.ts +47 -0
- package/src/extensibility/extensions/compact-handler.ts +40 -0
- package/src/extensibility/extensions/get-commands-handler.ts +78 -0
- package/src/extensibility/extensions/index.ts +16 -0
- package/src/extensibility/extensions/loader.ts +572 -0
- package/src/extensibility/extensions/runner.ts +922 -0
- package/src/extensibility/extensions/types.ts +1322 -0
- package/src/extensibility/extensions/wrapper.ts +223 -0
- package/src/extensibility/hooks/index.ts +5 -0
- package/src/extensibility/hooks/loader.ts +257 -0
- package/src/extensibility/hooks/runner.ts +425 -0
- package/src/extensibility/hooks/tool-wrapper.ts +107 -0
- package/src/extensibility/hooks/types.ts +606 -0
- package/src/extensibility/legacy-pi-ai-shim.ts +24 -0
- package/src/extensibility/legacy-pi-coding-agent-shim.ts +15 -0
- package/src/extensibility/plugins/doctor.ts +65 -0
- package/src/extensibility/plugins/git-url.ts +367 -0
- package/src/extensibility/plugins/index.ts +9 -0
- package/src/extensibility/plugins/installer.ts +192 -0
- package/src/extensibility/plugins/legacy-pi-compat.ts +682 -0
- package/src/extensibility/plugins/loader.ts +313 -0
- package/src/extensibility/plugins/manager.ts +827 -0
- package/src/extensibility/plugins/marketplace/cache.ts +136 -0
- package/src/extensibility/plugins/marketplace/fetcher.ts +317 -0
- package/src/extensibility/plugins/marketplace/index.ts +6 -0
- package/src/extensibility/plugins/marketplace/manager.ts +770 -0
- package/src/extensibility/plugins/marketplace/registry.ts +196 -0
- package/src/extensibility/plugins/marketplace/source-resolver.ts +147 -0
- package/src/extensibility/plugins/marketplace/types.ts +191 -0
- package/src/extensibility/plugins/marketplace-auto-update.ts +49 -0
- package/src/extensibility/plugins/parser.ts +105 -0
- package/src/extensibility/plugins/types.ts +194 -0
- package/src/extensibility/shared-events.ts +343 -0
- package/src/extensibility/skills.ts +312 -0
- package/src/extensibility/slash-commands.ts +227 -0
- package/src/extensibility/tool-proxy.ts +25 -0
- package/src/extensibility/typebox.ts +418 -0
- package/src/extensibility/utils.ts +44 -0
- package/src/goals/index.ts +3 -0
- package/src/goals/runtime.ts +528 -0
- package/src/goals/state.ts +37 -0
- package/src/goals/tools/goal-tool.ts +251 -0
- package/src/hindsight/backend.ts +354 -0
- package/src/hindsight/bank.ts +156 -0
- package/src/hindsight/client.ts +598 -0
- package/src/hindsight/config.ts +175 -0
- package/src/hindsight/content.ts +210 -0
- package/src/hindsight/index.ts +8 -0
- package/src/hindsight/mental-models.ts +429 -0
- package/src/hindsight/seeds.json +32 -0
- package/src/hindsight/state.ts +488 -0
- package/src/hindsight/transcript.ts +71 -0
- package/src/index.ts +59 -0
- package/src/internal-urls/agent-protocol.ts +146 -0
- package/src/internal-urls/artifact-protocol.ts +107 -0
- package/src/internal-urls/docs-index.generated.ts +106 -0
- package/src/internal-urls/history-protocol.ts +113 -0
- package/src/internal-urls/index.ts +25 -0
- package/src/internal-urls/issue-pr-protocol.ts +584 -0
- package/src/internal-urls/json-query.ts +126 -0
- package/src/internal-urls/local-protocol.ts +287 -0
- package/src/internal-urls/mcp-protocol.ts +151 -0
- package/src/internal-urls/memory-protocol.ts +169 -0
- package/src/internal-urls/omp-protocol.ts +93 -0
- package/src/internal-urls/parse.ts +72 -0
- package/src/internal-urls/registry-helpers.ts +25 -0
- package/src/internal-urls/router.ts +105 -0
- package/src/internal-urls/rule-protocol.ts +45 -0
- package/src/internal-urls/skill-protocol.ts +96 -0
- package/src/internal-urls/types.ts +152 -0
- package/src/internal-urls/vault-protocol.ts +936 -0
- package/src/irc/bus.ts +292 -0
- package/src/lib/xai-http.ts +124 -0
- package/src/lsp/client.ts +1193 -0
- package/src/lsp/clients/biome-client.ts +264 -0
- package/src/lsp/clients/index.ts +50 -0
- package/src/lsp/clients/lsp-linter-client.ts +93 -0
- package/src/lsp/clients/swiftlint-client.ts +120 -0
- package/src/lsp/config.ts +502 -0
- package/src/lsp/defaults.json +493 -0
- package/src/lsp/diagnostics-ledger.ts +51 -0
- package/src/lsp/edits.ts +267 -0
- package/src/lsp/index.ts +2477 -0
- package/src/lsp/lspmux.ts +233 -0
- package/src/lsp/render.ts +694 -0
- package/src/lsp/startup-events.ts +13 -0
- package/src/lsp/types.ts +455 -0
- package/src/lsp/utils.ts +718 -0
- package/src/main.ts +1325 -0
- package/src/mcp/client.ts +484 -0
- package/src/mcp/config-writer.ts +225 -0
- package/src/mcp/config.ts +365 -0
- package/src/mcp/index.ts +29 -0
- package/src/mcp/json-rpc.ts +122 -0
- package/src/mcp/loader.ts +124 -0
- package/src/mcp/manager.ts +1275 -0
- package/src/mcp/oauth-discovery.ts +442 -0
- package/src/mcp/oauth-flow.ts +442 -0
- package/src/mcp/render.ts +132 -0
- package/src/mcp/smithery-auth.ts +104 -0
- package/src/mcp/smithery-connect.ts +145 -0
- package/src/mcp/smithery-registry.ts +477 -0
- package/src/mcp/timeout.ts +59 -0
- package/src/mcp/tool-bridge.ts +426 -0
- package/src/mcp/tool-cache.ts +117 -0
- package/src/mcp/transports/http.ts +519 -0
- package/src/mcp/transports/index.ts +6 -0
- package/src/mcp/transports/stdio.ts +528 -0
- package/src/mcp/types.ts +423 -0
- package/src/memories/index.ts +1150 -0
- package/src/memories/storage.ts +577 -0
- package/src/memory-backend/index.ts +18 -0
- package/src/memory-backend/local-backend.ts +39 -0
- package/src/memory-backend/off-backend.ts +25 -0
- package/src/memory-backend/resolve.ts +25 -0
- package/src/memory-backend/runtime.ts +66 -0
- package/src/memory-backend/types.ts +166 -0
- package/src/mnemopi/backend.ts +547 -0
- package/src/mnemopi/config.ts +160 -0
- package/src/mnemopi/index.ts +3 -0
- package/src/mnemopi/state.ts +584 -0
- package/src/modes/acp/acp-agent.ts +2407 -0
- package/src/modes/acp/acp-client-bridge.ts +154 -0
- package/src/modes/acp/acp-event-mapper.ts +929 -0
- package/src/modes/acp/acp-mode.ts +23 -0
- package/src/modes/acp/index.ts +2 -0
- package/src/modes/acp/terminal-auth.ts +37 -0
- package/src/modes/components/agent-dashboard.ts +1206 -0
- package/src/modes/components/agent-hub.ts +1071 -0
- package/src/modes/components/assistant-message.ts +307 -0
- package/src/modes/components/bash-execution.ts +220 -0
- package/src/modes/components/bordered-loader.ts +41 -0
- package/src/modes/components/branch-summary-message.ts +45 -0
- package/src/modes/components/btw-panel.ts +104 -0
- package/src/modes/components/chat-block.ts +111 -0
- package/src/modes/components/compaction-summary-message.ts +87 -0
- package/src/modes/components/copy-selector.ts +206 -0
- package/src/modes/components/countdown-timer.ts +75 -0
- package/src/modes/components/custom-editor.ts +398 -0
- package/src/modes/components/custom-message.ts +63 -0
- package/src/modes/components/diff.ts +277 -0
- package/src/modes/components/dynamic-border.ts +34 -0
- package/src/modes/components/error-banner.ts +33 -0
- package/src/modes/components/eval-execution.ts +158 -0
- package/src/modes/components/execution-shared.ts +101 -0
- package/src/modes/components/extensions/extension-dashboard.ts +399 -0
- package/src/modes/components/extensions/extension-list.ts +502 -0
- package/src/modes/components/extensions/index.ts +9 -0
- package/src/modes/components/extensions/inspector-panel.ts +317 -0
- package/src/modes/components/extensions/state-manager.ts +627 -0
- package/src/modes/components/extensions/types.ts +186 -0
- package/src/modes/components/footer.ts +274 -0
- package/src/modes/components/history-search.ts +280 -0
- package/src/modes/components/hook-editor.ts +167 -0
- package/src/modes/components/hook-input.ts +87 -0
- package/src/modes/components/hook-message.ts +66 -0
- package/src/modes/components/hook-selector.ts +660 -0
- package/src/modes/components/index.ts +38 -0
- package/src/modes/components/keybinding-hints.ts +65 -0
- package/src/modes/components/late-diagnostics-message.ts +60 -0
- package/src/modes/components/login-dialog.ts +164 -0
- package/src/modes/components/mcp-add-wizard.ts +1340 -0
- package/src/modes/components/message-frame.ts +88 -0
- package/src/modes/components/model-selector.ts +1271 -0
- package/src/modes/components/oauth-selector.ts +368 -0
- package/src/modes/components/omfg-panel.ts +141 -0
- package/src/modes/components/overlay-box.ts +108 -0
- package/src/modes/components/plan-review-overlay.ts +820 -0
- package/src/modes/components/plan-toc.ts +138 -0
- package/src/modes/components/plugin-selector.ts +95 -0
- package/src/modes/components/plugin-settings.ts +722 -0
- package/src/modes/components/queue-mode-selector.ts +56 -0
- package/src/modes/components/read-tool-group.ts +670 -0
- package/src/modes/components/segment-track.ts +52 -0
- package/src/modes/components/session-selector.ts +625 -0
- package/src/modes/components/settings-defs.ts +189 -0
- package/src/modes/components/settings-selector.ts +651 -0
- package/src/modes/components/show-images-selector.ts +45 -0
- package/src/modes/components/skill-message.ts +89 -0
- package/src/modes/components/status-line/component.ts +869 -0
- package/src/modes/components/status-line/context-thresholds.ts +79 -0
- package/src/modes/components/status-line/git-utils.ts +42 -0
- package/src/modes/components/status-line/index.ts +5 -0
- package/src/modes/components/status-line/presets.ts +106 -0
- package/src/modes/components/status-line/segments.ts +584 -0
- package/src/modes/components/status-line/separators.ts +55 -0
- package/src/modes/components/status-line/token-rate.ts +66 -0
- package/src/modes/components/status-line/types.ts +108 -0
- package/src/modes/components/theme-selector.ts +63 -0
- package/src/modes/components/thinking-selector.ts +52 -0
- package/src/modes/components/tiny-title-download-progress.ts +90 -0
- package/src/modes/components/tips.txt +19 -0
- package/src/modes/components/todo-reminder.ts +38 -0
- package/src/modes/components/tool-execution.ts +1024 -0
- package/src/modes/components/transcript-container.ts +608 -0
- package/src/modes/components/tree-selector.ts +978 -0
- package/src/modes/components/ttsr-notification.ts +122 -0
- package/src/modes/components/user-message-selector.ts +227 -0
- package/src/modes/components/user-message.ts +66 -0
- package/src/modes/components/visual-truncate.ts +63 -0
- package/src/modes/components/welcome.ts +493 -0
- package/src/modes/controllers/btw-controller.ts +105 -0
- package/src/modes/controllers/command-controller-shared.ts +109 -0
- package/src/modes/controllers/command-controller.ts +1566 -0
- package/src/modes/controllers/event-controller.ts +1054 -0
- package/src/modes/controllers/extension-ui-controller.ts +886 -0
- package/src/modes/controllers/input-controller.ts +1073 -0
- package/src/modes/controllers/mcp-command-controller.ts +2017 -0
- package/src/modes/controllers/omfg-controller.ts +283 -0
- package/src/modes/controllers/omfg-rule.ts +647 -0
- package/src/modes/controllers/selector-controller.ts +1108 -0
- package/src/modes/controllers/ssh-command-controller.ts +384 -0
- package/src/modes/controllers/streaming-reveal.ts +279 -0
- package/src/modes/controllers/tan-command-controller.ts +173 -0
- package/src/modes/controllers/todo-command-controller.ts +485 -0
- package/src/modes/data/emojis.json +1 -0
- package/src/modes/emoji-autocomplete.ts +285 -0
- package/src/modes/gradient-highlight.ts +87 -0
- package/src/modes/image-references.ts +117 -0
- package/src/modes/index.ts +17 -0
- package/src/modes/interactive-mode.ts +3370 -0
- package/src/modes/internal-url-autocomplete.ts +143 -0
- package/src/modes/loop-limit.ts +140 -0
- package/src/modes/magic-keywords.ts +20 -0
- package/src/modes/markdown-prose.ts +247 -0
- package/src/modes/oauth-manual-input.ts +69 -0
- package/src/modes/orchestrate.ts +42 -0
- package/src/modes/print-mode.ts +126 -0
- package/src/modes/prompt-action-autocomplete.ts +260 -0
- package/src/modes/rpc/host-tools.ts +186 -0
- package/src/modes/rpc/host-uris.ts +235 -0
- package/src/modes/rpc/rpc-client.ts +963 -0
- package/src/modes/rpc/rpc-mode.ts +947 -0
- package/src/modes/rpc/rpc-subagents.ts +265 -0
- package/src/modes/rpc/rpc-types.ts +458 -0
- package/src/modes/runtime-init.ts +116 -0
- package/src/modes/session-observer-registry.ts +146 -0
- package/src/modes/setup-version.ts +11 -0
- package/src/modes/setup-wizard/index.ts +99 -0
- package/src/modes/setup-wizard/lazy.ts +16 -0
- package/src/modes/setup-wizard/scenes/glyph.ts +96 -0
- package/src/modes/setup-wizard/scenes/outro.ts +35 -0
- package/src/modes/setup-wizard/scenes/providers.ts +69 -0
- package/src/modes/setup-wizard/scenes/sign-in.ts +205 -0
- package/src/modes/setup-wizard/scenes/splash.ts +201 -0
- package/src/modes/setup-wizard/scenes/theme.ts +299 -0
- package/src/modes/setup-wizard/scenes/types.ts +48 -0
- package/src/modes/setup-wizard/scenes/web-search.ts +129 -0
- package/src/modes/setup-wizard/wizard-overlay.ts +275 -0
- package/src/modes/shared.ts +47 -0
- package/src/modes/theme/dark.json +95 -0
- package/src/modes/theme/defaults/alabaster.json +93 -0
- package/src/modes/theme/defaults/amethyst.json +96 -0
- package/src/modes/theme/defaults/anthracite.json +93 -0
- package/src/modes/theme/defaults/basalt.json +91 -0
- package/src/modes/theme/defaults/birch.json +95 -0
- package/src/modes/theme/defaults/dark-abyss.json +91 -0
- package/src/modes/theme/defaults/dark-arctic.json +104 -0
- package/src/modes/theme/defaults/dark-aurora.json +95 -0
- package/src/modes/theme/defaults/dark-catppuccin.json +107 -0
- package/src/modes/theme/defaults/dark-cavern.json +91 -0
- package/src/modes/theme/defaults/dark-copper.json +95 -0
- package/src/modes/theme/defaults/dark-cosmos.json +90 -0
- package/src/modes/theme/defaults/dark-cyberpunk.json +102 -0
- package/src/modes/theme/defaults/dark-dracula.json +98 -0
- package/src/modes/theme/defaults/dark-eclipse.json +91 -0
- package/src/modes/theme/defaults/dark-ember.json +95 -0
- package/src/modes/theme/defaults/dark-equinox.json +90 -0
- package/src/modes/theme/defaults/dark-forest.json +96 -0
- package/src/modes/theme/defaults/dark-github.json +105 -0
- package/src/modes/theme/defaults/dark-gruvbox.json +112 -0
- package/src/modes/theme/defaults/dark-lavender.json +95 -0
- package/src/modes/theme/defaults/dark-lunar.json +89 -0
- package/src/modes/theme/defaults/dark-midnight.json +95 -0
- package/src/modes/theme/defaults/dark-monochrome.json +94 -0
- package/src/modes/theme/defaults/dark-monokai.json +98 -0
- package/src/modes/theme/defaults/dark-nebula.json +90 -0
- package/src/modes/theme/defaults/dark-nord.json +97 -0
- package/src/modes/theme/defaults/dark-ocean.json +101 -0
- package/src/modes/theme/defaults/dark-one.json +100 -0
- package/src/modes/theme/defaults/dark-poimandres.json +142 -0
- package/src/modes/theme/defaults/dark-rainforest.json +91 -0
- package/src/modes/theme/defaults/dark-reef.json +91 -0
- package/src/modes/theme/defaults/dark-retro.json +92 -0
- package/src/modes/theme/defaults/dark-rose-pine.json +96 -0
- package/src/modes/theme/defaults/dark-sakura.json +95 -0
- package/src/modes/theme/defaults/dark-slate.json +95 -0
- package/src/modes/theme/defaults/dark-solarized.json +97 -0
- package/src/modes/theme/defaults/dark-solstice.json +90 -0
- package/src/modes/theme/defaults/dark-starfall.json +91 -0
- package/src/modes/theme/defaults/dark-sunset.json +99 -0
- package/src/modes/theme/defaults/dark-swamp.json +90 -0
- package/src/modes/theme/defaults/dark-synthwave.json +103 -0
- package/src/modes/theme/defaults/dark-taiga.json +91 -0
- package/src/modes/theme/defaults/dark-terminal.json +95 -0
- package/src/modes/theme/defaults/dark-tokyo-night.json +101 -0
- package/src/modes/theme/defaults/dark-tundra.json +91 -0
- package/src/modes/theme/defaults/dark-twilight.json +91 -0
- package/src/modes/theme/defaults/dark-volcanic.json +91 -0
- package/src/modes/theme/defaults/graphite.json +92 -0
- package/src/modes/theme/defaults/index.ts +199 -0
- package/src/modes/theme/defaults/light-arctic.json +107 -0
- package/src/modes/theme/defaults/light-aurora-day.json +91 -0
- package/src/modes/theme/defaults/light-canyon.json +91 -0
- package/src/modes/theme/defaults/light-catppuccin.json +106 -0
- package/src/modes/theme/defaults/light-cirrus.json +90 -0
- package/src/modes/theme/defaults/light-coral.json +95 -0
- package/src/modes/theme/defaults/light-cyberpunk.json +96 -0
- package/src/modes/theme/defaults/light-dawn.json +90 -0
- package/src/modes/theme/defaults/light-dunes.json +91 -0
- package/src/modes/theme/defaults/light-eucalyptus.json +95 -0
- package/src/modes/theme/defaults/light-forest.json +100 -0
- package/src/modes/theme/defaults/light-frost.json +95 -0
- package/src/modes/theme/defaults/light-github.json +115 -0
- package/src/modes/theme/defaults/light-glacier.json +91 -0
- package/src/modes/theme/defaults/light-gruvbox.json +108 -0
- package/src/modes/theme/defaults/light-haze.json +90 -0
- package/src/modes/theme/defaults/light-honeycomb.json +95 -0
- package/src/modes/theme/defaults/light-lagoon.json +91 -0
- package/src/modes/theme/defaults/light-lavender.json +95 -0
- package/src/modes/theme/defaults/light-meadow.json +91 -0
- package/src/modes/theme/defaults/light-mint.json +95 -0
- package/src/modes/theme/defaults/light-monochrome.json +101 -0
- package/src/modes/theme/defaults/light-ocean.json +99 -0
- package/src/modes/theme/defaults/light-one.json +99 -0
- package/src/modes/theme/defaults/light-opal.json +91 -0
- package/src/modes/theme/defaults/light-orchard.json +91 -0
- package/src/modes/theme/defaults/light-paper.json +95 -0
- package/src/modes/theme/defaults/light-poimandres.json +142 -0
- package/src/modes/theme/defaults/light-prism.json +90 -0
- package/src/modes/theme/defaults/light-retro.json +98 -0
- package/src/modes/theme/defaults/light-sand.json +95 -0
- package/src/modes/theme/defaults/light-savanna.json +91 -0
- package/src/modes/theme/defaults/light-solarized.json +102 -0
- package/src/modes/theme/defaults/light-soleil.json +90 -0
- package/src/modes/theme/defaults/light-sunset.json +99 -0
- package/src/modes/theme/defaults/light-synthwave.json +98 -0
- package/src/modes/theme/defaults/light-tokyo-night.json +111 -0
- package/src/modes/theme/defaults/light-wetland.json +91 -0
- package/src/modes/theme/defaults/light-zenith.json +89 -0
- package/src/modes/theme/defaults/limestone.json +94 -0
- package/src/modes/theme/defaults/mahogany.json +97 -0
- package/src/modes/theme/defaults/marble.json +93 -0
- package/src/modes/theme/defaults/obsidian.json +91 -0
- package/src/modes/theme/defaults/onyx.json +91 -0
- package/src/modes/theme/defaults/pearl.json +93 -0
- package/src/modes/theme/defaults/porcelain.json +91 -0
- package/src/modes/theme/defaults/quartz.json +96 -0
- package/src/modes/theme/defaults/sandstone.json +95 -0
- package/src/modes/theme/defaults/titanium.json +90 -0
- package/src/modes/theme/light.json +93 -0
- package/src/modes/theme/mermaid-cache.ts +29 -0
- package/src/modes/theme/shimmer.ts +235 -0
- package/src/modes/theme/theme-schema.json +459 -0
- package/src/modes/theme/theme.ts +2676 -0
- package/src/modes/turn-budget.ts +31 -0
- package/src/modes/types.ts +359 -0
- package/src/modes/ultrathink.ts +41 -0
- package/src/modes/utils/context-usage.ts +339 -0
- package/src/modes/utils/copy-targets.ts +360 -0
- package/src/modes/utils/hotkeys-markdown.ts +61 -0
- package/src/modes/utils/keybinding-matchers.ts +51 -0
- package/src/modes/utils/tools-markdown.ts +27 -0
- package/src/modes/utils/ui-helpers.ts +801 -0
- package/src/modes/workflow.ts +42 -0
- package/src/plan-mode/approved-plan.ts +186 -0
- package/src/plan-mode/plan-handoff.ts +37 -0
- package/src/plan-mode/plan-protection.ts +31 -0
- package/src/plan-mode/state.ts +6 -0
- package/src/priority.json +41 -0
- package/src/prompts/agents/designer.md +66 -0
- package/src/prompts/agents/explore.md +58 -0
- package/src/prompts/agents/frontmatter.md +11 -0
- package/src/prompts/agents/init.md +33 -0
- package/src/prompts/agents/librarian.md +119 -0
- package/src/prompts/agents/oracle.md +55 -0
- package/src/prompts/agents/plan.md +48 -0
- package/src/prompts/agents/reviewer.md +140 -0
- package/src/prompts/agents/task.md +16 -0
- package/src/prompts/ci-green-request.md +36 -0
- package/src/prompts/dry-balance-bench.md +8 -0
- package/src/prompts/goals/goal-budget-limit.md +16 -0
- package/src/prompts/goals/goal-continuation.md +28 -0
- package/src/prompts/goals/goal-mode-active.md +23 -0
- package/src/prompts/memories/consolidation.md +30 -0
- package/src/prompts/memories/read-path.md +11 -0
- package/src/prompts/memories/stage_one_input.md +6 -0
- package/src/prompts/memories/stage_one_system.md +21 -0
- package/src/prompts/review-custom-request.md +22 -0
- package/src/prompts/review-headless-request.md +16 -0
- package/src/prompts/review-request.md +69 -0
- package/src/prompts/steering/user-interjection.md +10 -0
- package/src/prompts/system/agent-creation-architect.md +50 -0
- package/src/prompts/system/agent-creation-user.md +6 -0
- package/src/prompts/system/auto-continue.md +1 -0
- package/src/prompts/system/auto-thinking-difficulty-local.md +14 -0
- package/src/prompts/system/auto-thinking-difficulty.md +12 -0
- package/src/prompts/system/background-tan-dispatch.md +8 -0
- package/src/prompts/system/btw-user.md +8 -0
- package/src/prompts/system/commit-message-system.md +14 -0
- package/src/prompts/system/custom-system-prompt.md +64 -0
- package/src/prompts/system/eager-todo.md +13 -0
- package/src/prompts/system/empty-stop-retry.md +6 -0
- package/src/prompts/system/irc-incoming.md +7 -0
- package/src/prompts/system/manual-continue.md +7 -0
- package/src/prompts/system/memory-consolidation-system.md +8 -0
- package/src/prompts/system/memory-extraction-system.md +26 -0
- package/src/prompts/system/omfg-user.md +50 -0
- package/src/prompts/system/orchestrate-notice.md +40 -0
- package/src/prompts/system/plan-mode-active.md +109 -0
- package/src/prompts/system/plan-mode-approved.md +25 -0
- package/src/prompts/system/plan-mode-compact-instructions.md +16 -0
- package/src/prompts/system/plan-mode-reference.md +11 -0
- package/src/prompts/system/plan-mode-subagent.md +33 -0
- package/src/prompts/system/plan-mode-tool-decision-reminder.md +9 -0
- package/src/prompts/system/project-prompt.md +52 -0
- package/src/prompts/system/subagent-system-prompt.md +64 -0
- package/src/prompts/system/subagent-user-prompt.md +3 -0
- package/src/prompts/system/subagent-yield-reminder.md +12 -0
- package/src/prompts/system/system-prompt.md +258 -0
- package/src/prompts/system/tiny-title-system.md +8 -0
- package/src/prompts/system/title-system.md +16 -0
- package/src/prompts/system/ttsr-interrupt.md +7 -0
- package/src/prompts/system/ttsr-tool-reminder.md +5 -0
- package/src/prompts/system/ultrathink-notice.md +3 -0
- package/src/prompts/system/web-search.md +25 -0
- package/src/prompts/system/workflow-notice.md +70 -0
- package/src/prompts/tools/apply-patch.md +65 -0
- package/src/prompts/tools/ask.md +30 -0
- package/src/prompts/tools/ast-edit.md +39 -0
- package/src/prompts/tools/ast-grep.md +42 -0
- package/src/prompts/tools/async-result.md +8 -0
- package/src/prompts/tools/bash.md +46 -0
- package/src/prompts/tools/browser.md +73 -0
- package/src/prompts/tools/checkpoint.md +16 -0
- package/src/prompts/tools/debug.md +34 -0
- package/src/prompts/tools/eval.md +92 -0
- package/src/prompts/tools/find.md +36 -0
- package/src/prompts/tools/github.md +21 -0
- package/src/prompts/tools/goal.md +18 -0
- package/src/prompts/tools/image-gen.md +7 -0
- package/src/prompts/tools/inspect-image-system.md +20 -0
- package/src/prompts/tools/inspect-image.md +32 -0
- package/src/prompts/tools/irc.md +59 -0
- package/src/prompts/tools/job.md +19 -0
- package/src/prompts/tools/lsp-late-diagnostic.md +8 -0
- package/src/prompts/tools/lsp.md +42 -0
- package/src/prompts/tools/memory-edit.md +8 -0
- package/src/prompts/tools/patch.md +70 -0
- package/src/prompts/tools/read.md +84 -0
- package/src/prompts/tools/recall.md +5 -0
- package/src/prompts/tools/reflect.md +5 -0
- package/src/prompts/tools/render-mermaid.md +9 -0
- package/src/prompts/tools/replace.md +30 -0
- package/src/prompts/tools/resolve.md +9 -0
- package/src/prompts/tools/retain.md +6 -0
- package/src/prompts/tools/rewind.md +13 -0
- package/src/prompts/tools/search-tool-bm25.md +32 -0
- package/src/prompts/tools/search.md +24 -0
- package/src/prompts/tools/ssh.md +31 -0
- package/src/prompts/tools/task-summary.md +17 -0
- package/src/prompts/tools/task.md +88 -0
- package/src/prompts/tools/todo.md +62 -0
- package/src/prompts/tools/web-search.md +10 -0
- package/src/prompts/tools/write.md +14 -0
- package/src/registry/agent-lifecycle.ts +218 -0
- package/src/registry/agent-registry.ts +151 -0
- package/src/sdk.ts +2558 -0
- package/src/secrets/index.ts +123 -0
- package/src/secrets/obfuscator.ts +298 -0
- package/src/secrets/regex.ts +21 -0
- package/src/session/agent-session.ts +10121 -0
- package/src/session/agent-storage.ts +455 -0
- package/src/session/artifacts.ts +135 -0
- package/src/session/auth-broker-config.ts +131 -0
- package/src/session/auth-storage.ts +29 -0
- package/src/session/blob-store.ts +255 -0
- package/src/session/client-bridge.ts +85 -0
- package/src/session/history-storage.ts +348 -0
- package/src/session/indexed-session-storage.ts +430 -0
- package/src/session/messages.ts +541 -0
- package/src/session/redis-session-storage.ts +170 -0
- package/src/session/session-dump-format.ts +209 -0
- package/src/session/session-history-format.ts +246 -0
- package/src/session/session-manager.ts +3676 -0
- package/src/session/session-storage.ts +529 -0
- package/src/session/shake-types.ts +43 -0
- package/src/session/sql-session-storage.ts +314 -0
- package/src/session/streaming-output.ts +1330 -0
- package/src/session/tool-choice-queue.ts +213 -0
- package/src/session/yield-queue.ts +173 -0
- package/src/slash-commands/acp-builtins.ts +70 -0
- package/src/slash-commands/builtin-registry.ts +1798 -0
- package/src/slash-commands/helpers/context-report.ts +39 -0
- package/src/slash-commands/helpers/format.ts +46 -0
- package/src/slash-commands/helpers/marketplace-manager.ts +25 -0
- package/src/slash-commands/helpers/mcp.ts +532 -0
- package/src/slash-commands/helpers/parse.ts +85 -0
- package/src/slash-commands/helpers/ssh.ts +195 -0
- package/src/slash-commands/helpers/stats-dashboard.ts +85 -0
- package/src/slash-commands/helpers/todo.ts +279 -0
- package/src/slash-commands/helpers/usage-report.ts +95 -0
- package/src/slash-commands/marketplace-install-parser.ts +99 -0
- package/src/slash-commands/types.ts +135 -0
- package/src/ssh/config-writer.ts +183 -0
- package/src/ssh/connection-manager.ts +509 -0
- package/src/ssh/ssh-executor.ts +189 -0
- package/src/ssh/sshfs-mount.ts +140 -0
- package/src/ssh/utils.ts +8 -0
- package/src/stt/downloader.ts +71 -0
- package/src/stt/index.ts +3 -0
- package/src/stt/recorder.ts +351 -0
- package/src/stt/setup.ts +52 -0
- package/src/stt/stt-controller.ts +160 -0
- package/src/stt/transcribe.py +70 -0
- package/src/stt/transcriber.ts +91 -0
- package/src/stubs/natives/index.ts +814 -0
- package/src/stubs/natives/package.json +7 -0
- package/src/stubs/tui/index.ts +282 -0
- package/src/stubs/tui/package.json +7 -0
- package/src/system-prompt.ts +611 -0
- package/src/task/agents.ts +167 -0
- package/src/task/commands.ts +132 -0
- package/src/task/discovery.ts +122 -0
- package/src/task/executor.ts +2133 -0
- package/src/task/index.ts +1419 -0
- package/src/task/name-generator.ts +1577 -0
- package/src/task/omp-command.ts +26 -0
- package/src/task/output-manager.ts +88 -0
- package/src/task/parallel.ts +116 -0
- package/src/task/render.ts +1381 -0
- package/src/task/repair-args.ts +129 -0
- package/src/task/subprocess-tool-registry.ts +88 -0
- package/src/task/types.ts +336 -0
- package/src/task/worktree.ts +514 -0
- package/src/telemetry-export.ts +144 -0
- package/src/thinking.ts +167 -0
- package/src/tiny/compiled-runtime.ts +179 -0
- package/src/tiny/device.ts +111 -0
- package/src/tiny/dtype.ts +101 -0
- package/src/tiny/models.ts +242 -0
- package/src/tiny/text.ts +165 -0
- package/src/tiny/title-client.ts +543 -0
- package/src/tiny/title-protocol.ts +56 -0
- package/src/tiny/worker.ts +568 -0
- package/src/tool-discovery/mode.ts +24 -0
- package/src/tool-discovery/tool-index.ts +256 -0
- package/src/tools/approval.ts +189 -0
- package/src/tools/archive-reader.ts +721 -0
- package/src/tools/ask.ts +928 -0
- package/src/tools/ast-edit.ts +642 -0
- package/src/tools/ast-grep.ts +452 -0
- package/src/tools/auto-generated-guard.ts +322 -0
- package/src/tools/bash-command-fixup.ts +37 -0
- package/src/tools/bash-interactive.ts +408 -0
- package/src/tools/bash-interceptor.ts +67 -0
- package/src/tools/bash-pty-selection.ts +14 -0
- package/src/tools/bash-skill-urls.ts +248 -0
- package/src/tools/bash.ts +1386 -0
- package/src/tools/browser/attach.ts +175 -0
- package/src/tools/browser/launch.ts +660 -0
- package/src/tools/browser/readable.ts +112 -0
- package/src/tools/browser/registry.ts +197 -0
- package/src/tools/browser/render.ts +216 -0
- package/src/tools/browser/tab-protocol.ts +105 -0
- package/src/tools/browser/tab-supervisor.ts +628 -0
- package/src/tools/browser/tab-worker-entry.ts +21 -0
- package/src/tools/browser/tab-worker.ts +1226 -0
- package/src/tools/browser.ts +343 -0
- package/src/tools/checkpoint.ts +136 -0
- package/src/tools/conflict-detect.ts +718 -0
- package/src/tools/context.ts +39 -0
- package/src/tools/debug.ts +1067 -0
- package/src/tools/eval-backends.ts +27 -0
- package/src/tools/eval-render.ts +752 -0
- package/src/tools/eval.ts +577 -0
- package/src/tools/fetch.ts +1926 -0
- package/src/tools/file-recorder.ts +35 -0
- package/src/tools/find.ts +609 -0
- package/src/tools/fs-cache-invalidation.ts +28 -0
- package/src/tools/gh-cache-invalidation.ts +255 -0
- package/src/tools/gh-format.ts +12 -0
- package/src/tools/gh-renderer.ts +481 -0
- package/src/tools/gh.ts +3720 -0
- package/src/tools/github-cache.ts +637 -0
- package/src/tools/grouped-file-output.ts +210 -0
- package/src/tools/image-gen.ts +1517 -0
- package/src/tools/index.ts +599 -0
- package/src/tools/inspect-image-renderer.ts +132 -0
- package/src/tools/inspect-image.ts +174 -0
- package/src/tools/irc.ts +723 -0
- package/src/tools/job.ts +557 -0
- package/src/tools/json-tree.ts +243 -0
- package/src/tools/jtd-to-json-schema.ts +219 -0
- package/src/tools/jtd-to-typescript.ts +136 -0
- package/src/tools/jtd-utils.ts +102 -0
- package/src/tools/list-limit.ts +40 -0
- package/src/tools/match-line-format.ts +20 -0
- package/src/tools/memory-edit.ts +59 -0
- package/src/tools/memory-recall.ts +100 -0
- package/src/tools/memory-reflect.ts +88 -0
- package/src/tools/memory-render.ts +202 -0
- package/src/tools/memory-retain.ts +91 -0
- package/src/tools/output-meta.ts +754 -0
- package/src/tools/output-schema-validator.ts +132 -0
- package/src/tools/path-utils.ts +1054 -0
- package/src/tools/plan-mode-guard.ts +108 -0
- package/src/tools/puppeteer/00_stealth_tampering.txt +63 -0
- package/src/tools/puppeteer/01_stealth_activity.txt +20 -0
- package/src/tools/puppeteer/02_stealth_hairline.txt +11 -0
- package/src/tools/puppeteer/03_stealth_botd.txt +384 -0
- package/src/tools/puppeteer/04_stealth_iframe.txt +81 -0
- package/src/tools/puppeteer/05_stealth_webgl.txt +75 -0
- package/src/tools/puppeteer/06_stealth_screen.txt +72 -0
- package/src/tools/puppeteer/07_stealth_fonts.txt +97 -0
- package/src/tools/puppeteer/08_stealth_audio.txt +51 -0
- package/src/tools/puppeteer/09_stealth_locale.txt +46 -0
- package/src/tools/puppeteer/10_stealth_plugins.txt +206 -0
- package/src/tools/puppeteer/11_stealth_hardware.txt +8 -0
- package/src/tools/puppeteer/12_stealth_codecs.txt +40 -0
- package/src/tools/puppeteer/13_stealth_worker.txt +74 -0
- package/src/tools/read.ts +2929 -0
- package/src/tools/render-mermaid.ts +69 -0
- package/src/tools/render-utils.ts +838 -0
- package/src/tools/renderers.ts +77 -0
- package/src/tools/report-tool-issue.ts +534 -0
- package/src/tools/resolve.ts +276 -0
- package/src/tools/review.ts +253 -0
- package/src/tools/search-tool-bm25.ts +351 -0
- package/src/tools/search.ts +1580 -0
- package/src/tools/sqlite-reader.ts +828 -0
- package/src/tools/ssh.ts +349 -0
- package/src/tools/todo.ts +982 -0
- package/src/tools/tool-errors.ts +62 -0
- package/src/tools/tool-result.ts +94 -0
- package/src/tools/tool-timeouts.ts +30 -0
- package/src/tools/tts.ts +133 -0
- package/src/tools/write.ts +1217 -0
- package/src/tools/yield.ts +269 -0
- package/src/tui/code-cell.ts +216 -0
- package/src/tui/file-list.ts +55 -0
- package/src/tui/hyperlink.ts +175 -0
- package/src/tui/index.ts +12 -0
- package/src/tui/output-block.ts +240 -0
- package/src/tui/status-line.ts +54 -0
- package/src/tui/tree-list.ts +84 -0
- package/src/tui/types.ts +15 -0
- package/src/tui/utils.ts +103 -0
- package/src/utils/block-context.ts +312 -0
- package/src/utils/changelog.ts +132 -0
- package/src/utils/clipboard.ts +193 -0
- package/src/utils/command-args.ts +76 -0
- package/src/utils/commit-message-generator.ts +151 -0
- package/src/utils/edit-mode.ts +41 -0
- package/src/utils/enhanced-paste.ts +230 -0
- package/src/utils/event-bus.ts +33 -0
- package/src/utils/external-editor.ts +65 -0
- package/src/utils/file-display-mode.ts +45 -0
- package/src/utils/file-mentions.ts +281 -0
- package/src/utils/git.ts +1833 -0
- package/src/utils/image-loading.ts +132 -0
- package/src/utils/image-resize.ts +309 -0
- package/src/utils/jj.ts +248 -0
- package/src/utils/lang-from-path.ts +239 -0
- package/src/utils/markit.ts +89 -0
- package/src/utils/open.ts +55 -0
- package/src/utils/session-color.ts +68 -0
- package/src/utils/shell-snapshot.ts +187 -0
- package/src/utils/sixel.ts +69 -0
- package/src/utils/title-generator.ts +373 -0
- package/src/utils/tool-choice.ts +33 -0
- package/src/utils/tools-manager.ts +363 -0
- package/src/web/kagi.ts +305 -0
- package/src/web/parallel.ts +353 -0
- package/src/web/scrapers/artifacthub.ts +207 -0
- package/src/web/scrapers/arxiv.ts +83 -0
- package/src/web/scrapers/aur.ts +162 -0
- package/src/web/scrapers/biorxiv.ts +133 -0
- package/src/web/scrapers/bluesky.ts +262 -0
- package/src/web/scrapers/brew.ts +172 -0
- package/src/web/scrapers/cheatsh.ts +68 -0
- package/src/web/scrapers/chocolatey.ts +196 -0
- package/src/web/scrapers/choosealicense.ts +95 -0
- package/src/web/scrapers/cisa-kev.ts +87 -0
- package/src/web/scrapers/clojars.ts +154 -0
- package/src/web/scrapers/coingecko.ts +177 -0
- package/src/web/scrapers/crates-io.ts +97 -0
- package/src/web/scrapers/crossref.ts +136 -0
- package/src/web/scrapers/devto.ts +147 -0
- package/src/web/scrapers/discogs.ts +306 -0
- package/src/web/scrapers/discourse.ts +197 -0
- package/src/web/scrapers/dockerhub.ts +138 -0
- package/src/web/scrapers/docs-rs.ts +653 -0
- package/src/web/scrapers/fdroid.ts +134 -0
- package/src/web/scrapers/firefox-addons.ts +191 -0
- package/src/web/scrapers/flathub.ts +223 -0
- package/src/web/scrapers/github-gist.ts +58 -0
- package/src/web/scrapers/github.ts +704 -0
- package/src/web/scrapers/gitlab.ts +401 -0
- package/src/web/scrapers/go-pkg.ts +266 -0
- package/src/web/scrapers/hackage.ts +140 -0
- package/src/web/scrapers/hackernews.ts +189 -0
- package/src/web/scrapers/hex.ts +105 -0
- package/src/web/scrapers/huggingface.ts +321 -0
- package/src/web/scrapers/iacr.ts +89 -0
- package/src/web/scrapers/index.ts +252 -0
- package/src/web/scrapers/jetbrains-marketplace.ts +159 -0
- package/src/web/scrapers/lemmy.ts +203 -0
- package/src/web/scrapers/lobsters.ts +175 -0
- package/src/web/scrapers/mastodon.ts +292 -0
- package/src/web/scrapers/maven.ts +138 -0
- package/src/web/scrapers/mdn.ts +173 -0
- package/src/web/scrapers/metacpan.ts +222 -0
- package/src/web/scrapers/musicbrainz.ts +250 -0
- package/src/web/scrapers/npm.ts +98 -0
- package/src/web/scrapers/nuget.ts +183 -0
- package/src/web/scrapers/nvd.ts +222 -0
- package/src/web/scrapers/ollama.ts +239 -0
- package/src/web/scrapers/open-vsx.ts +106 -0
- package/src/web/scrapers/opencorporates.ts +292 -0
- package/src/web/scrapers/openlibrary.ts +336 -0
- package/src/web/scrapers/orcid.ts +286 -0
- package/src/web/scrapers/osv.ts +176 -0
- package/src/web/scrapers/packagist.ts +160 -0
- package/src/web/scrapers/pub-dev.ts +143 -0
- package/src/web/scrapers/pubmed.ts +211 -0
- package/src/web/scrapers/pypi.ts +112 -0
- package/src/web/scrapers/rawg.ts +110 -0
- package/src/web/scrapers/readthedocs.ts +120 -0
- package/src/web/scrapers/reddit.ts +95 -0
- package/src/web/scrapers/repology.ts +251 -0
- package/src/web/scrapers/rfc.ts +201 -0
- package/src/web/scrapers/rubygems.ts +103 -0
- package/src/web/scrapers/searchcode.ts +189 -0
- package/src/web/scrapers/sec-edgar.ts +261 -0
- package/src/web/scrapers/semantic-scholar.ts +171 -0
- package/src/web/scrapers/snapcraft.ts +187 -0
- package/src/web/scrapers/sourcegraph.ts +336 -0
- package/src/web/scrapers/spdx.ts +108 -0
- package/src/web/scrapers/spotify.ts +198 -0
- package/src/web/scrapers/stackoverflow.ts +120 -0
- package/src/web/scrapers/terraform.ts +277 -0
- package/src/web/scrapers/tldr.ts +47 -0
- package/src/web/scrapers/twitter.ts +94 -0
- package/src/web/scrapers/types.ts +397 -0
- package/src/web/scrapers/utils.ts +109 -0
- package/src/web/scrapers/vimeo.ts +133 -0
- package/src/web/scrapers/vscode-marketplace.ts +187 -0
- package/src/web/scrapers/w3c.ts +156 -0
- package/src/web/scrapers/wikidata.ts +344 -0
- package/src/web/scrapers/wikipedia.ts +84 -0
- package/src/web/scrapers/youtube.ts +325 -0
- package/src/web/search/index.ts +292 -0
- package/src/web/search/provider.ts +157 -0
- package/src/web/search/providers/anthropic.ts +318 -0
- package/src/web/search/providers/base.ts +89 -0
- package/src/web/search/providers/brave.ts +152 -0
- package/src/web/search/providers/codex.ts +591 -0
- package/src/web/search/providers/exa.ts +400 -0
- package/src/web/search/providers/gemini.ts +460 -0
- package/src/web/search/providers/jina.ts +111 -0
- package/src/web/search/providers/kagi.ts +86 -0
- package/src/web/search/providers/kimi.ts +196 -0
- package/src/web/search/providers/parallel.ts +225 -0
- package/src/web/search/providers/perplexity.ts +730 -0
- package/src/web/search/providers/searxng.ts +313 -0
- package/src/web/search/providers/synthetic.ts +114 -0
- package/src/web/search/providers/tavily.ts +176 -0
- package/src/web/search/providers/utils.ts +128 -0
- package/src/web/search/providers/zai.ts +333 -0
- package/src/web/search/render.ts +262 -0
- package/src/web/search/types.ts +482 -0
- package/src/web/search/utils.ts +17 -0
- package/src/workspace-tree.ts +286 -0
|
@@ -0,0 +1,2133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-process execution for subagents.
|
|
3
|
+
*
|
|
4
|
+
* Runs each subagent on the main thread and forwards AgentEvents for progress tracking.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import type { AgentEvent, AgentIdentity, AgentTelemetryConfig, ThinkingLevel } from "@oh-my-pi/pi-agent-core";
|
|
9
|
+
import { recordHandoff, resolveTelemetry } from "@oh-my-pi/pi-agent-core";
|
|
10
|
+
import type { Usage } from "@oh-my-pi/pi-ai";
|
|
11
|
+
import { logger, prompt, untilAborted } from "@oh-my-pi/pi-utils";
|
|
12
|
+
import type { Rule } from "../capability/rule";
|
|
13
|
+
import { ModelRegistry } from "../config/model-registry";
|
|
14
|
+
import { resolveModelOverrideWithAuthFallback } from "../config/model-resolver";
|
|
15
|
+
import type { PromptTemplate } from "../config/prompt-templates";
|
|
16
|
+
import { Settings } from "../config/settings";
|
|
17
|
+
import { SETTINGS_SCHEMA, type SettingPath } from "../config/settings-schema";
|
|
18
|
+
import type { ToolPathWithSource } from "../extensibility/custom-tools";
|
|
19
|
+
import type { CustomTool } from "../extensibility/custom-tools/types";
|
|
20
|
+
import { runExtensionCompact, runExtensionSetModel } from "../extensibility/extensions/compact-handler";
|
|
21
|
+
import { getSessionSlashCommands } from "../extensibility/extensions/get-commands-handler";
|
|
22
|
+
import { buildSkillPromptMessage, type Skill } from "../extensibility/skills";
|
|
23
|
+
import type { HindsightSessionState } from "../hindsight/state";
|
|
24
|
+
import type { LocalProtocolOptions } from "../internal-urls";
|
|
25
|
+
import { callTool } from "../mcp/client";
|
|
26
|
+
import type { MCPManager } from "../mcp/manager";
|
|
27
|
+
import type { MnemopiSessionState } from "../mnemopi/state";
|
|
28
|
+
import subagentSystemPromptTemplate from "../prompts/system/subagent-system-prompt.md" with { type: "text" };
|
|
29
|
+
import submitReminderTemplate from "../prompts/system/subagent-yield-reminder.md" with { type: "text" };
|
|
30
|
+
import { AgentLifecycleManager } from "../registry/agent-lifecycle";
|
|
31
|
+
import { AgentRegistry } from "../registry/agent-registry";
|
|
32
|
+
import { type CreateAgentSessionOptions, createAgentSession, discoverAuthStorage } from "../sdk";
|
|
33
|
+
import type { AgentSession, AgentSessionEvent } from "../session/agent-session";
|
|
34
|
+
import type { ArtifactManager } from "../session/artifacts";
|
|
35
|
+
import type { AuthStorage } from "../session/auth-storage";
|
|
36
|
+
import { SKILL_PROMPT_MESSAGE_TYPE } from "../session/messages";
|
|
37
|
+
import { SessionManager } from "../session/session-manager";
|
|
38
|
+
import { truncateTail } from "../session/streaming-output";
|
|
39
|
+
import type { ContextFileEntry } from "../tools";
|
|
40
|
+
import { isIrcEnabled } from "../tools/irc";
|
|
41
|
+
import { normalizeSchema } from "../tools/jtd-to-json-schema";
|
|
42
|
+
import {
|
|
43
|
+
buildOutputValidator,
|
|
44
|
+
type OutputValidator,
|
|
45
|
+
summarizeValidationFailure,
|
|
46
|
+
} from "../tools/output-schema-validator";
|
|
47
|
+
|
|
48
|
+
import { type ReportFindingDetails, toReviewFinding } from "../tools/review";
|
|
49
|
+
import { ToolAbortError } from "../tools/tool-errors";
|
|
50
|
+
import type { EventBus } from "../utils/event-bus";
|
|
51
|
+
import { buildNamedToolChoice } from "../utils/tool-choice";
|
|
52
|
+
import type { WorkspaceTree } from "../workspace-tree";
|
|
53
|
+
import { subprocessToolRegistry } from "./subprocess-tool-registry";
|
|
54
|
+
import {
|
|
55
|
+
type AgentDefinition,
|
|
56
|
+
type AgentProgress,
|
|
57
|
+
MAX_OUTPUT_BYTES,
|
|
58
|
+
MAX_OUTPUT_LINES,
|
|
59
|
+
type ReviewFinding,
|
|
60
|
+
type SingleResult,
|
|
61
|
+
TASK_SUBAGENT_EVENT_CHANNEL,
|
|
62
|
+
TASK_SUBAGENT_LIFECYCLE_CHANNEL,
|
|
63
|
+
TASK_SUBAGENT_PROGRESS_CHANNEL,
|
|
64
|
+
type TaskToolDetails,
|
|
65
|
+
} from "./types";
|
|
66
|
+
|
|
67
|
+
const MCP_CALL_TIMEOUT_MS = 60_000;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Soft per-agent request budgets (assistant requests per run). When a subagent
|
|
71
|
+
* crosses its budget it receives ONE steering notice asking it to wrap up; at
|
|
72
|
+
* 1.5x the budget the run is aborted gracefully so partial output is salvaged.
|
|
73
|
+
* The `default` key applies to agents without an explicit entry and can be
|
|
74
|
+
* overridden via the `task.softRequestBudget` setting (0 disables the guard).
|
|
75
|
+
*/
|
|
76
|
+
export const SOFT_REQUEST_BUDGET: Record<string, number> = {
|
|
77
|
+
explore: 40,
|
|
78
|
+
quick_task: 40,
|
|
79
|
+
default: 90,
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/** Steering notice injected once when a subagent crosses its soft request budget. */
|
|
83
|
+
export function buildBudgetNotice(requests: number): string {
|
|
84
|
+
return `[budget notice] You have used ${requests} requests in this run. Wrap up now: finish the current step and yield your final report.`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Flatten whitespace and clip salvage text for the cancelled-child summary line. */
|
|
88
|
+
function formatSalvageSnippet(text: string, maxLength = 500): string {
|
|
89
|
+
const flattened = text.replace(/\s+/g, " ").trim();
|
|
90
|
+
return flattened.length > maxLength ? `${flattened.slice(0, maxLength - 1)}…` : flattened;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Agent event types to forward for progress tracking. */
|
|
94
|
+
const agentEventTypes = new Set<AgentEvent["type"]>([
|
|
95
|
+
"agent_start",
|
|
96
|
+
"agent_end",
|
|
97
|
+
"turn_start",
|
|
98
|
+
"turn_end",
|
|
99
|
+
"message_start",
|
|
100
|
+
"message_update",
|
|
101
|
+
"message_end",
|
|
102
|
+
"tool_execution_start",
|
|
103
|
+
"tool_execution_update",
|
|
104
|
+
"tool_execution_end",
|
|
105
|
+
]);
|
|
106
|
+
|
|
107
|
+
const isAgentEvent = (event: AgentSessionEvent): event is AgentEvent =>
|
|
108
|
+
agentEventTypes.has(event.type as AgentEvent["type"]);
|
|
109
|
+
|
|
110
|
+
function normalizeModelPatterns(value: string | string[] | undefined): string[] {
|
|
111
|
+
if (!value) return [];
|
|
112
|
+
if (Array.isArray(value)) {
|
|
113
|
+
return value.map(entry => entry.trim()).filter(Boolean);
|
|
114
|
+
}
|
|
115
|
+
return value
|
|
116
|
+
.split(",")
|
|
117
|
+
.map(entry => entry.trim())
|
|
118
|
+
.filter(Boolean);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function renderIrcPeerRoster(selfId: string): string {
|
|
122
|
+
const peers = AgentRegistry.global()
|
|
123
|
+
.list()
|
|
124
|
+
.filter(ref => ref.id !== selfId && ref.status !== "aborted");
|
|
125
|
+
if (peers.length === 0) return "- (no other agents)";
|
|
126
|
+
const lines = peers.map(peer => `- \`${peer.id}\` — ${peer.displayName} (${peer.kind}, ${peer.status})`);
|
|
127
|
+
if (peers.some(peer => peer.status === "idle" || peer.status === "parked")) {
|
|
128
|
+
lines.push("Idle/parked peers are not gone: messaging them wakes (or revives) them.");
|
|
129
|
+
}
|
|
130
|
+
return lines.join("\n");
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function withAbortTimeout<T>(promise: Promise<T>, timeoutMs: number, signal?: AbortSignal): Promise<T> {
|
|
134
|
+
if (signal?.aborted) {
|
|
135
|
+
return Promise.reject(new ToolAbortError());
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const { promise: wrappedPromise, resolve, reject } = Promise.withResolvers<T>();
|
|
139
|
+
let settled = false;
|
|
140
|
+
const timeoutId = setTimeout(() => {
|
|
141
|
+
if (settled) return;
|
|
142
|
+
settled = true;
|
|
143
|
+
reject(new Error(`MCP tool call timed out after ${timeoutMs}ms`));
|
|
144
|
+
}, timeoutMs);
|
|
145
|
+
|
|
146
|
+
const onAbort = () => {
|
|
147
|
+
if (settled) return;
|
|
148
|
+
settled = true;
|
|
149
|
+
clearTimeout(timeoutId);
|
|
150
|
+
reject(new ToolAbortError());
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
if (signal) {
|
|
154
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
promise.then(resolve, reject).finally(() => {
|
|
158
|
+
if (signal) signal.removeEventListener("abort", onAbort);
|
|
159
|
+
clearTimeout(timeoutId);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
return wrappedPromise;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function getReportFindingKey(value: unknown): string | null {
|
|
166
|
+
if (!value || typeof value !== "object") return null;
|
|
167
|
+
const record = value as Record<string, unknown>;
|
|
168
|
+
const title = typeof record.title === "string" ? record.title : null;
|
|
169
|
+
const filePath = typeof record.file_path === "string" ? record.file_path : null;
|
|
170
|
+
const lineStart = typeof record.line_start === "number" ? record.line_start : null;
|
|
171
|
+
const lineEnd = typeof record.line_end === "number" ? record.line_end : null;
|
|
172
|
+
const priority = typeof record.priority === "string" ? record.priority : null;
|
|
173
|
+
if (!title || !filePath || lineStart === null || lineEnd === null) {
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
return `${filePath}:${lineStart}:${lineEnd}:${priority ?? ""}:${title}`;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Options for subagent execution */
|
|
180
|
+
export interface ExecutorOptions {
|
|
181
|
+
cwd: string;
|
|
182
|
+
worktree?: string;
|
|
183
|
+
agent: AgentDefinition;
|
|
184
|
+
task: string;
|
|
185
|
+
assignment?: string;
|
|
186
|
+
/** Shared background from the task call (`task.batch`), rendered into the subagent's system prompt. */
|
|
187
|
+
context?: string;
|
|
188
|
+
/**
|
|
189
|
+
* The session's active overall plan, handed off so subagents spawned during
|
|
190
|
+
* plan execution share the same plan context as the main agent. Omitted when
|
|
191
|
+
* the session did not start with a plan (or while plan mode is still active).
|
|
192
|
+
*/
|
|
193
|
+
planReference?: { path: string; content: string };
|
|
194
|
+
description?: string;
|
|
195
|
+
index: number;
|
|
196
|
+
id: string;
|
|
197
|
+
parentToolCallId?: string;
|
|
198
|
+
modelOverride?: string | string[];
|
|
199
|
+
/**
|
|
200
|
+
* Active model selector of the parent session, used as an auth-aware fallback
|
|
201
|
+
* if the resolved subagent model has no working credentials. See #985.
|
|
202
|
+
*/
|
|
203
|
+
parentActiveModelPattern?: string;
|
|
204
|
+
thinkingLevel?: ThinkingLevel;
|
|
205
|
+
outputSchema?: unknown;
|
|
206
|
+
/** Parent task recursion depth (0 = top-level, 1 = first child, etc.) */
|
|
207
|
+
taskDepth?: number;
|
|
208
|
+
/**
|
|
209
|
+
* Override the `task.maxRuntimeMs` wall-clock cap for this run. When provided
|
|
210
|
+
* it wins over the settings value; `0` disables the per-subagent wall-clock
|
|
211
|
+
* limit entirely. Used by the eval `agent()` bridge, whose parent cell
|
|
212
|
+
* watchdog is already suspended for the call's duration.
|
|
213
|
+
*/
|
|
214
|
+
maxRuntimeMs?: number;
|
|
215
|
+
enableLsp?: boolean;
|
|
216
|
+
signal?: AbortSignal;
|
|
217
|
+
onProgress?: (progress: AgentProgress) => void;
|
|
218
|
+
sessionFile?: string | null;
|
|
219
|
+
persistArtifacts?: boolean;
|
|
220
|
+
artifactsDir?: string;
|
|
221
|
+
eventBus?: EventBus;
|
|
222
|
+
contextFiles?: ContextFileEntry[];
|
|
223
|
+
skills?: Skill[];
|
|
224
|
+
promptTemplates?: PromptTemplate[];
|
|
225
|
+
workspaceTree?: WorkspaceTree;
|
|
226
|
+
/** Parent-discovered rules, forwarded to skip rule discovery in the subagent. */
|
|
227
|
+
rules?: Rule[];
|
|
228
|
+
/**
|
|
229
|
+
* Parent's discovered extension source paths. Forwarded to skip the
|
|
230
|
+
* extension FS scan in the subagent; the subagent then re-binds each
|
|
231
|
+
* extension against its own `ExtensionAPI` (cwd, eventBus, runtime).
|
|
232
|
+
*/
|
|
233
|
+
preloadedExtensionPaths?: string[];
|
|
234
|
+
/**
|
|
235
|
+
* Parent's discovered custom-tool source paths. Forwarded to skip the
|
|
236
|
+
* `.omp/tools/` FS scan in the subagent; the subagent then re-binds each
|
|
237
|
+
* tool against its own `CustomToolAPI` (cwd, exec, pushPendingAction, UI).
|
|
238
|
+
*/
|
|
239
|
+
preloadedCustomToolPaths?: ToolPathWithSource[];
|
|
240
|
+
mcpManager?: MCPManager;
|
|
241
|
+
authStorage?: AuthStorage;
|
|
242
|
+
modelRegistry?: ModelRegistry;
|
|
243
|
+
settings?: Settings;
|
|
244
|
+
/** Override local:// protocol options so subagent shares parent's local:// root */
|
|
245
|
+
localProtocolOptions?: LocalProtocolOptions;
|
|
246
|
+
/**
|
|
247
|
+
* Parent session's ArtifactManager. Subagent adopts it so artifact IDs are
|
|
248
|
+
* unique across the whole agent tree and all artifacts land in the parent's
|
|
249
|
+
* artifacts directory (no per-subagent subdir).
|
|
250
|
+
*/
|
|
251
|
+
parentArtifactManager?: ArtifactManager;
|
|
252
|
+
parentHindsightSessionState?: HindsightSessionState;
|
|
253
|
+
parentMnemopiSessionState?: MnemopiSessionState;
|
|
254
|
+
/** Parent agent's eval executor session id. Subagents reuse it so eval state is shared. */
|
|
255
|
+
parentEvalSessionId?: string;
|
|
256
|
+
/**
|
|
257
|
+
* Parent agent's OpenTelemetry configuration. When defined, the subagent's
|
|
258
|
+
* loop is started with the same tracer/hooks but its own agent identity
|
|
259
|
+
* stamped, so its `invoke_agent` / `chat` / `execute_tool` spans appear as
|
|
260
|
+
* a sub-tree under the parent's active `execute_tool task` span. A
|
|
261
|
+
* `handoff` span is emitted on dispatch to mark the parent → subagent
|
|
262
|
+
* transition explicitly.
|
|
263
|
+
*/
|
|
264
|
+
parentTelemetry?: AgentTelemetryConfig;
|
|
265
|
+
/** Skills to autoload via sendCustomMessage before the first prompt */
|
|
266
|
+
autoloadSkills?: Skill[];
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function parseStringifiedJson(value: unknown): unknown {
|
|
270
|
+
if (typeof value !== "string") return value;
|
|
271
|
+
const trimmed = value.trim();
|
|
272
|
+
if (!trimmed) return value;
|
|
273
|
+
if (!(trimmed.startsWith("{") || trimmed.startsWith("["))) return value;
|
|
274
|
+
try {
|
|
275
|
+
return JSON.parse(trimmed);
|
|
276
|
+
} catch {
|
|
277
|
+
return value;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function previewOffendingData(value: unknown, maxLength = 500): string {
|
|
282
|
+
let serialized: string;
|
|
283
|
+
try {
|
|
284
|
+
serialized = JSON.stringify(value) ?? "null";
|
|
285
|
+
} catch {
|
|
286
|
+
serialized = String(value);
|
|
287
|
+
}
|
|
288
|
+
return serialized.length > maxLength ? `${serialized.slice(0, maxLength)}…` : serialized;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function tryParseJsonOutput(text: string): unknown | undefined {
|
|
292
|
+
const trimmed = text.trim();
|
|
293
|
+
if (!trimmed) return undefined;
|
|
294
|
+
try {
|
|
295
|
+
return JSON.parse(trimmed);
|
|
296
|
+
} catch {
|
|
297
|
+
return undefined;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function extractCompletionData(parsed: unknown): unknown {
|
|
302
|
+
if (!parsed || typeof parsed !== "object") return parsed;
|
|
303
|
+
const record = parsed as Record<string, unknown>;
|
|
304
|
+
if ("data" in record) {
|
|
305
|
+
return record.data;
|
|
306
|
+
}
|
|
307
|
+
return parsed;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Resolve the final yielded payload, optionally splicing collected
|
|
312
|
+
* `report_finding` entries into a top-level `findings` array.
|
|
313
|
+
*
|
|
314
|
+
* Injection is suppressed when an active validator would reject the augmented
|
|
315
|
+
* payload (e.g. a caller-supplied schema with `additionalProperties: false`
|
|
316
|
+
* that does not declare `findings`). That keeps the in-tool yield validator
|
|
317
|
+
* (which only sees the raw, pre-injection data) in lockstep with this
|
|
318
|
+
* post-mortem validator — honoring the "accepted in-tool ⇒ accepted
|
|
319
|
+
* post-mortem" guarantee documented in `output-schema-validator.ts`. The
|
|
320
|
+
* dropped findings are still preserved verbatim in the agent's progress
|
|
321
|
+
* stream and JSONL artifact, so no information is lost when injection is
|
|
322
|
+
* suppressed.
|
|
323
|
+
*/
|
|
324
|
+
function normalizeCompleteData(
|
|
325
|
+
data: unknown,
|
|
326
|
+
reportFindings: ReviewFinding[] | undefined,
|
|
327
|
+
validator: OutputValidator | undefined,
|
|
328
|
+
): unknown {
|
|
329
|
+
const normalized = parseStringifiedJson(data ?? null);
|
|
330
|
+
if (
|
|
331
|
+
!Array.isArray(reportFindings) ||
|
|
332
|
+
reportFindings.length === 0 ||
|
|
333
|
+
!normalized ||
|
|
334
|
+
typeof normalized !== "object" ||
|
|
335
|
+
Array.isArray(normalized)
|
|
336
|
+
) {
|
|
337
|
+
return normalized;
|
|
338
|
+
}
|
|
339
|
+
const record = normalized as Record<string, unknown>;
|
|
340
|
+
if ("findings" in record) return normalized;
|
|
341
|
+
const injected = { ...record, findings: reportFindings };
|
|
342
|
+
if (validator && !validator.validate(injected).success) return normalized;
|
|
343
|
+
return injected;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function resolveFallbackCompletion(rawOutput: string, outputSchema: unknown): { data: unknown } | null {
|
|
347
|
+
const parsed = tryParseJsonOutput(rawOutput);
|
|
348
|
+
if (parsed === undefined) return null;
|
|
349
|
+
const candidate = parseStringifiedJson(extractCompletionData(parsed));
|
|
350
|
+
if (candidate === undefined) return null;
|
|
351
|
+
const { validator, error } = buildOutputValidator(outputSchema);
|
|
352
|
+
if (error) return null;
|
|
353
|
+
if (validator && !validator.validate(candidate).success) return null;
|
|
354
|
+
return { data: candidate };
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
export interface YieldItem {
|
|
358
|
+
data?: unknown;
|
|
359
|
+
status?: "success" | "aborted";
|
|
360
|
+
error?: string;
|
|
361
|
+
/**
|
|
362
|
+
* Set by the in-tool yield validator when it exhausted its retry budget
|
|
363
|
+
* (MAX_SCHEMA_RETRIES) and accepted a schema-invalid payload anyway.
|
|
364
|
+
* `finalizeSubprocessOutput` honors this by serializing the payload and
|
|
365
|
+
* surfacing a stderr warning, instead of re-emitting `schema_violation`
|
|
366
|
+
* — which would silently swap the subagent's "accepted" view for a
|
|
367
|
+
* different, opaque error blob in the parent's view of the result.
|
|
368
|
+
*/
|
|
369
|
+
schemaOverridden?: boolean;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
interface FinalizeSubprocessOutputArgs {
|
|
373
|
+
rawOutput: string;
|
|
374
|
+
exitCode: number;
|
|
375
|
+
stderr: string;
|
|
376
|
+
doneAborted: boolean;
|
|
377
|
+
signalAborted: boolean;
|
|
378
|
+
yieldItems?: YieldItem[];
|
|
379
|
+
reportFindings?: ReviewFinding[];
|
|
380
|
+
outputSchema: unknown;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
interface FinalizeSubprocessOutputResult {
|
|
384
|
+
rawOutput: string;
|
|
385
|
+
exitCode: number;
|
|
386
|
+
stderr: string;
|
|
387
|
+
abortedViaYield: boolean;
|
|
388
|
+
hasYield: boolean;
|
|
389
|
+
}
|
|
390
|
+
export const SUBAGENT_WARNING_SCHEMA_OVERRIDDEN =
|
|
391
|
+
"SYSTEM WARNING: Subagent exhausted schema-retry budget; result was accepted despite failing the output schema.";
|
|
392
|
+
export const SUBAGENT_WARNING_NULL_YIELD = "SYSTEM WARNING: Subagent called yield with null data.";
|
|
393
|
+
export const SUBAGENT_WARNING_MISSING_YIELD =
|
|
394
|
+
"SYSTEM WARNING: Subagent exited without calling yield tool after 3 reminders.";
|
|
395
|
+
|
|
396
|
+
/** Build a schema_violation outcome — surfaced as a non-zero exit so callers treat it as a failure. */
|
|
397
|
+
function buildSchemaViolationOutcome(
|
|
398
|
+
failure: { message: string; missingRequired: string[] },
|
|
399
|
+
data: unknown,
|
|
400
|
+
): { rawOutput: string; stderr: string; exitCode: number } {
|
|
401
|
+
const missing = failure.missingRequired;
|
|
402
|
+
const headline =
|
|
403
|
+
missing.length > 0
|
|
404
|
+
? `schema_violation: missing required fields: ${missing.join(", ")}`
|
|
405
|
+
: `schema_violation: ${failure.message}`;
|
|
406
|
+
const payload = {
|
|
407
|
+
error: "schema_violation",
|
|
408
|
+
message: failure.message,
|
|
409
|
+
missingRequired: missing,
|
|
410
|
+
data: previewOffendingData(data),
|
|
411
|
+
};
|
|
412
|
+
let rawOutput: string;
|
|
413
|
+
try {
|
|
414
|
+
rawOutput = JSON.stringify(payload, null, 2);
|
|
415
|
+
} catch {
|
|
416
|
+
rawOutput = `{"error":"schema_violation","message":${JSON.stringify(headline)}}`;
|
|
417
|
+
}
|
|
418
|
+
return { rawOutput, stderr: headline, exitCode: 1 };
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
export function finalizeSubprocessOutput(args: FinalizeSubprocessOutputArgs): FinalizeSubprocessOutputResult {
|
|
422
|
+
let { rawOutput, exitCode, stderr } = args;
|
|
423
|
+
const { yieldItems, reportFindings, doneAborted, signalAborted, outputSchema } = args;
|
|
424
|
+
let abortedViaYield = false;
|
|
425
|
+
const hasYield = Array.isArray(yieldItems) && yieldItems.length > 0;
|
|
426
|
+
|
|
427
|
+
if (hasYield) {
|
|
428
|
+
const lastYield = yieldItems[yieldItems.length - 1];
|
|
429
|
+
if (lastYield?.status === "aborted") {
|
|
430
|
+
abortedViaYield = true;
|
|
431
|
+
exitCode = 0;
|
|
432
|
+
stderr = lastYield.error || "Subagent aborted task";
|
|
433
|
+
try {
|
|
434
|
+
rawOutput = JSON.stringify({ aborted: true, error: lastYield.error }, null, 2);
|
|
435
|
+
} catch {
|
|
436
|
+
rawOutput = `{"aborted":true,"error":"${lastYield.error || "Unknown error"}"}`;
|
|
437
|
+
}
|
|
438
|
+
} else {
|
|
439
|
+
const submitData = lastYield?.data;
|
|
440
|
+
if (submitData === null || submitData === undefined) {
|
|
441
|
+
rawOutput = rawOutput ? `${SUBAGENT_WARNING_NULL_YIELD}\n\n${rawOutput}` : SUBAGENT_WARNING_NULL_YIELD;
|
|
442
|
+
} else {
|
|
443
|
+
const { validator, error: schemaError } = buildOutputValidator(outputSchema);
|
|
444
|
+
const overridden = lastYield?.schemaOverridden === true;
|
|
445
|
+
const completeData = normalizeCompleteData(submitData, reportFindings, validator);
|
|
446
|
+
const result =
|
|
447
|
+
schemaError || overridden
|
|
448
|
+
? { success: true as const }
|
|
449
|
+
: (validator?.validate(completeData) ?? { success: true as const });
|
|
450
|
+
if (!result.success) {
|
|
451
|
+
const summary = summarizeValidationFailure(result, completeData, validator?.requiredFields ?? []);
|
|
452
|
+
const outcome = buildSchemaViolationOutcome(summary, completeData);
|
|
453
|
+
rawOutput = outcome.rawOutput;
|
|
454
|
+
stderr = outcome.stderr;
|
|
455
|
+
exitCode = outcome.exitCode;
|
|
456
|
+
} else {
|
|
457
|
+
try {
|
|
458
|
+
rawOutput = JSON.stringify(completeData, null, 2) ?? "null";
|
|
459
|
+
} catch (err) {
|
|
460
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
461
|
+
rawOutput = `{"error":"Failed to serialize yield data: ${errorMessage}"}`;
|
|
462
|
+
}
|
|
463
|
+
exitCode = 0;
|
|
464
|
+
stderr = overridden
|
|
465
|
+
? SUBAGENT_WARNING_SCHEMA_OVERRIDDEN
|
|
466
|
+
: schemaError
|
|
467
|
+
? `invalid output schema: ${schemaError}`
|
|
468
|
+
: "";
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
} else {
|
|
473
|
+
const allowFallback = exitCode === 0 && !doneAborted && !signalAborted;
|
|
474
|
+
const { normalized: normalizedSchema, error: schemaError } = normalizeSchema(outputSchema);
|
|
475
|
+
const hasOutputSchema = normalizedSchema !== undefined && !schemaError;
|
|
476
|
+
const fallback = allowFallback ? resolveFallbackCompletion(rawOutput, outputSchema) : null;
|
|
477
|
+
if (fallback) {
|
|
478
|
+
const { validator } = buildOutputValidator(outputSchema);
|
|
479
|
+
const completeData = normalizeCompleteData(fallback.data, reportFindings, validator);
|
|
480
|
+
const result = validator?.validate(completeData) ?? { success: true as const };
|
|
481
|
+
if (!result.success) {
|
|
482
|
+
const summary = summarizeValidationFailure(result, completeData, validator?.requiredFields ?? []);
|
|
483
|
+
const outcome = buildSchemaViolationOutcome(summary, completeData);
|
|
484
|
+
rawOutput = outcome.rawOutput;
|
|
485
|
+
stderr = outcome.stderr;
|
|
486
|
+
exitCode = outcome.exitCode;
|
|
487
|
+
} else {
|
|
488
|
+
try {
|
|
489
|
+
rawOutput = JSON.stringify(completeData, null, 2) ?? "null";
|
|
490
|
+
} catch (err) {
|
|
491
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
492
|
+
rawOutput = `{"error":"Failed to serialize fallback completion: ${errorMessage}"}`;
|
|
493
|
+
}
|
|
494
|
+
exitCode = 0;
|
|
495
|
+
stderr = "";
|
|
496
|
+
}
|
|
497
|
+
} else if (!hasOutputSchema && allowFallback && rawOutput.trim().length > 0) {
|
|
498
|
+
exitCode = 0;
|
|
499
|
+
stderr = "";
|
|
500
|
+
} else if (exitCode === 0) {
|
|
501
|
+
const hasRawOutput = rawOutput.trim().length > 0;
|
|
502
|
+
rawOutput = rawOutput ? `${SUBAGENT_WARNING_MISSING_YIELD}\n\n${rawOutput}` : SUBAGENT_WARNING_MISSING_YIELD;
|
|
503
|
+
if (hasOutputSchema || !hasRawOutput) {
|
|
504
|
+
exitCode = 1;
|
|
505
|
+
stderr = SUBAGENT_WARNING_MISSING_YIELD;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
return { rawOutput, exitCode, stderr, abortedViaYield, hasYield };
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Extract a short preview from tool args for display.
|
|
515
|
+
*/
|
|
516
|
+
function extractToolArgsPreview(args: Record<string, unknown>): string {
|
|
517
|
+
// Priority order for preview
|
|
518
|
+
const previewKeys = ["command", "file_path", "path", "pattern", "query", "url", "task", "prompt"];
|
|
519
|
+
|
|
520
|
+
for (const key of previewKeys) {
|
|
521
|
+
if (args[key] && typeof args[key] === "string") {
|
|
522
|
+
const value = args[key] as string;
|
|
523
|
+
return value.length > 60 ? `${value.slice(0, 59)}…` : value;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
return "";
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
function getNumberField(record: Record<string, unknown>, key: string): number | undefined {
|
|
531
|
+
if (!Object.hasOwn(record, key)) return undefined;
|
|
532
|
+
const value = record[key];
|
|
533
|
+
return typeof value === "number" && Number.isFinite(value) ? value : 0;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
function firstNumberField(record: Record<string, unknown>, keys: string[]): number | undefined {
|
|
537
|
+
for (const key of keys) {
|
|
538
|
+
const value = getNumberField(record, key);
|
|
539
|
+
if (value !== undefined) return value;
|
|
540
|
+
}
|
|
541
|
+
return undefined;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Tokens for progress display: input + output + cacheWrite per turn.
|
|
546
|
+
*
|
|
547
|
+
* Deliberately excludes cacheRead. With prompt caching, cacheRead in each turn
|
|
548
|
+
* equals the full cached context (potentially hundreds of KB), so summing it
|
|
549
|
+
* across all turns produces a cumulative total that is N×context_size — far
|
|
550
|
+
* larger than the context window and misleading as a "work done" metric.
|
|
551
|
+
* cacheWrite is kept because each byte is written once, not repeated per turn.
|
|
552
|
+
* The cost segment handles billing; dedicated cache_read/cache_write segments
|
|
553
|
+
* handle cache-specific monitoring.
|
|
554
|
+
*/
|
|
555
|
+
function getUsageTokens(usage: unknown): number {
|
|
556
|
+
if (!usage || typeof usage !== "object") return 0;
|
|
557
|
+
const record = usage as Record<string, unknown>;
|
|
558
|
+
|
|
559
|
+
const input = firstNumberField(record, ["input", "input_tokens", "inputTokens"]) ?? 0;
|
|
560
|
+
const output = firstNumberField(record, ["output", "output_tokens", "outputTokens"]) ?? 0;
|
|
561
|
+
const cacheWrite = firstNumberField(record, ["cacheWrite", "cache_write", "cacheWriteTokens"]) ?? 0;
|
|
562
|
+
const computed = input + output + cacheWrite;
|
|
563
|
+
if (computed > 0) return computed;
|
|
564
|
+
// Fallback for providers that only surface a pre-summed total without individual
|
|
565
|
+
// field breakdown. This total includes cacheRead, but returning it is still better
|
|
566
|
+
// than silently showing 0 for those providers.
|
|
567
|
+
return firstNumberField(record, ["totalTokens", "total_tokens"]) ?? 0;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* Create proxy tools that reuse the parent's MCP connections.
|
|
572
|
+
*/
|
|
573
|
+
export function createMCPProxyTools(mcpManager: MCPManager): CustomTool[] {
|
|
574
|
+
return mcpManager.getTools().map(tool => {
|
|
575
|
+
const mcpTool = tool as { mcpToolName?: string; mcpServerName?: string };
|
|
576
|
+
return {
|
|
577
|
+
name: tool.name,
|
|
578
|
+
label: tool.label ?? tool.name,
|
|
579
|
+
description: tool.description ?? "",
|
|
580
|
+
parameters: tool.parameters,
|
|
581
|
+
execute: async (_toolCallId, params, _onUpdate, _ctx, signal) => {
|
|
582
|
+
if (signal?.aborted) {
|
|
583
|
+
throw new ToolAbortError();
|
|
584
|
+
}
|
|
585
|
+
const serverName = mcpTool.mcpServerName ?? "";
|
|
586
|
+
const mcpToolName = mcpTool.mcpToolName ?? "";
|
|
587
|
+
try {
|
|
588
|
+
const result = await withAbortTimeout(
|
|
589
|
+
(async () => {
|
|
590
|
+
const connection = await mcpManager.waitForConnection(serverName);
|
|
591
|
+
return callTool(connection, mcpToolName, params as Record<string, unknown>, { signal });
|
|
592
|
+
})(),
|
|
593
|
+
MCP_CALL_TIMEOUT_MS,
|
|
594
|
+
signal,
|
|
595
|
+
);
|
|
596
|
+
return {
|
|
597
|
+
content: (result.content ?? []).map(item =>
|
|
598
|
+
item.type === "text"
|
|
599
|
+
? { type: "text" as const, text: item.text ?? "" }
|
|
600
|
+
: { type: "text" as const, text: JSON.stringify(item) },
|
|
601
|
+
),
|
|
602
|
+
details: { serverName, mcpToolName, isError: result.isError },
|
|
603
|
+
};
|
|
604
|
+
} catch (error) {
|
|
605
|
+
if (error instanceof ToolAbortError) {
|
|
606
|
+
throw error;
|
|
607
|
+
}
|
|
608
|
+
return {
|
|
609
|
+
content: [
|
|
610
|
+
{
|
|
611
|
+
type: "text" as const,
|
|
612
|
+
text: `MCP error: ${error instanceof Error ? error.message : String(error)}`,
|
|
613
|
+
},
|
|
614
|
+
],
|
|
615
|
+
details: { serverName, mcpToolName, isError: true },
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
},
|
|
619
|
+
};
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
export function createSubagentSettings(
|
|
624
|
+
baseSettings: Settings,
|
|
625
|
+
overrides?: Partial<Record<SettingPath, unknown>>,
|
|
626
|
+
): Settings {
|
|
627
|
+
const snapshot: Partial<Record<SettingPath, unknown>> = {};
|
|
628
|
+
for (const key of Object.keys(SETTINGS_SCHEMA) as SettingPath[]) {
|
|
629
|
+
snapshot[key] = baseSettings.get(key);
|
|
630
|
+
}
|
|
631
|
+
return Settings.isolated({
|
|
632
|
+
...snapshot,
|
|
633
|
+
"async.enabled": false,
|
|
634
|
+
"bash.autoBackground.enabled": false,
|
|
635
|
+
|
|
636
|
+
// Subagents run headless — there is no UI to confirm prompts against, so
|
|
637
|
+
// the parent task approval is the authorization boundary. Use yolo mode
|
|
638
|
+
// to preserve unattended subagent execution. User `tools.approval` policies still apply.
|
|
639
|
+
"tools.approvalMode": "yolo",
|
|
640
|
+
...overrides,
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
type AbortReason = "signal" | "terminate" | "timeout" | "budget";
|
|
645
|
+
|
|
646
|
+
/** Inputs for the run monitor driving one subagent assignment. */
|
|
647
|
+
interface RunMonitorArgs {
|
|
648
|
+
index: number;
|
|
649
|
+
id: string;
|
|
650
|
+
agent: AgentDefinition;
|
|
651
|
+
task: string;
|
|
652
|
+
assignment?: string;
|
|
653
|
+
description?: string;
|
|
654
|
+
modelOverride?: string | string[];
|
|
655
|
+
signal?: AbortSignal;
|
|
656
|
+
onProgress?: (progress: AgentProgress) => void;
|
|
657
|
+
eventBus?: EventBus;
|
|
658
|
+
parentToolCallId?: string;
|
|
659
|
+
sessionFile?: string;
|
|
660
|
+
/** Soft assistant-request budget; 0 disables the guard. */
|
|
661
|
+
softRequestBudget: number;
|
|
662
|
+
/** Wall-clock cap in ms; 0 disables the timer. */
|
|
663
|
+
maxRuntimeMs: number;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
/**
|
|
667
|
+
* The run-monitoring core of {@link runSubprocess}: progress tracking, event
|
|
668
|
+
* processing, abort/budget machinery, usage accumulation, and output capture
|
|
669
|
+
* for one assignment run.
|
|
670
|
+
*/
|
|
671
|
+
interface SubagentRunMonitor {
|
|
672
|
+
readonly progress: AgentProgress;
|
|
673
|
+
/** Fires when the run was asked to stop (caller signal, timeout, budget, terminate). */
|
|
674
|
+
readonly abortSignal: AbortSignal;
|
|
675
|
+
readonly accumulatedUsage: Usage;
|
|
676
|
+
hasUsage(): boolean;
|
|
677
|
+
yieldCalled(): boolean;
|
|
678
|
+
runtimeLimitExceeded(): boolean;
|
|
679
|
+
/** True when the abort carries a precise external reason (signal / wall-clock / budget). */
|
|
680
|
+
hasExplicitAbortReason(): boolean;
|
|
681
|
+
/** Whether the (attempted) abort counts as a cancelled run rather than an internal failure. */
|
|
682
|
+
isAbortedRun(): boolean;
|
|
683
|
+
requestAbort(reason: AbortReason): void;
|
|
684
|
+
resolveSignalAbortReason(): string;
|
|
685
|
+
resolveAbortReasonText(): string;
|
|
686
|
+
setActiveSession(session: AgentSession | null): void;
|
|
687
|
+
/** Return and clear the active session reference. */
|
|
688
|
+
takeActiveSession(): AgentSession | null;
|
|
689
|
+
/** Subscribe the monitor to a session's events. Returns the unsubscribe function. */
|
|
690
|
+
attach(session: AgentSession): () => void;
|
|
691
|
+
/** Best-effort capture of the last assistant text for cancelled-run salvage. */
|
|
692
|
+
captureSalvage(session: AgentSession): void;
|
|
693
|
+
lastAssistantSalvageText(): string | undefined;
|
|
694
|
+
/** Final raw output: end-of-run assistant text when available, else accumulated chunks. */
|
|
695
|
+
rawOutput(): string;
|
|
696
|
+
scheduleProgress(flush?: boolean): void;
|
|
697
|
+
/** Stop processing events and clear listeners/timers. Call once the run settled. */
|
|
698
|
+
finish(): void;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
function createSubagentRunMonitor(args: RunMonitorArgs): SubagentRunMonitor {
|
|
702
|
+
const { index, id, agent, task, assignment, signal, onProgress, softRequestBudget, maxRuntimeMs } = args;
|
|
703
|
+
const startTime = Date.now();
|
|
704
|
+
|
|
705
|
+
const progress: AgentProgress = {
|
|
706
|
+
index,
|
|
707
|
+
id,
|
|
708
|
+
agent: agent.name,
|
|
709
|
+
agentSource: agent.source,
|
|
710
|
+
status: "running",
|
|
711
|
+
task,
|
|
712
|
+
assignment,
|
|
713
|
+
description: args.description,
|
|
714
|
+
lastIntent: undefined,
|
|
715
|
+
recentTools: [],
|
|
716
|
+
recentOutput: [],
|
|
717
|
+
toolCount: 0,
|
|
718
|
+
requests: 0,
|
|
719
|
+
tokens: 0,
|
|
720
|
+
cost: 0,
|
|
721
|
+
durationMs: 0,
|
|
722
|
+
modelOverride: args.modelOverride,
|
|
723
|
+
};
|
|
724
|
+
|
|
725
|
+
const outputChunks: string[] = [];
|
|
726
|
+
const finalOutputChunks: string[] = [];
|
|
727
|
+
const RECENT_OUTPUT_TAIL_BYTES = 8 * 1024;
|
|
728
|
+
let recentOutputTail = "";
|
|
729
|
+
let resolved = false;
|
|
730
|
+
let abortSent = false;
|
|
731
|
+
let abortReason: AbortReason | undefined;
|
|
732
|
+
let runtimeLimitExceeded = false;
|
|
733
|
+
const listenerController = new AbortController();
|
|
734
|
+
const listenerSignal = listenerController.signal;
|
|
735
|
+
const abortController = new AbortController();
|
|
736
|
+
const abortSignal = abortController.signal;
|
|
737
|
+
let activeSession: AgentSession | null = null;
|
|
738
|
+
let yieldCalled = false;
|
|
739
|
+
|
|
740
|
+
// Accumulate usage incrementally from message_end events (no memory for streaming events)
|
|
741
|
+
const accumulatedUsage: Usage = {
|
|
742
|
+
input: 0,
|
|
743
|
+
output: 0,
|
|
744
|
+
cacheRead: 0,
|
|
745
|
+
cacheWrite: 0,
|
|
746
|
+
totalTokens: 0,
|
|
747
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
748
|
+
};
|
|
749
|
+
let hasUsage = false;
|
|
750
|
+
let budgetSteerSent = false;
|
|
751
|
+
let budgetLimitExceeded = false;
|
|
752
|
+
let lastAssistantSalvageText: string | undefined;
|
|
753
|
+
|
|
754
|
+
const requestAbort = (reason: AbortReason) => {
|
|
755
|
+
if (reason === "timeout") {
|
|
756
|
+
runtimeLimitExceeded = true;
|
|
757
|
+
}
|
|
758
|
+
if (reason === "budget") {
|
|
759
|
+
budgetLimitExceeded = true;
|
|
760
|
+
}
|
|
761
|
+
if (abortSent) {
|
|
762
|
+
if (reason === "signal" && abortReason !== "signal" && abortReason !== "timeout") {
|
|
763
|
+
abortReason = "signal";
|
|
764
|
+
}
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
767
|
+
if (resolved) return;
|
|
768
|
+
abortSent = true;
|
|
769
|
+
abortReason = reason;
|
|
770
|
+
abortController.abort();
|
|
771
|
+
if (activeSession) {
|
|
772
|
+
void activeSession.abort();
|
|
773
|
+
}
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
// Handle abort signal
|
|
777
|
+
if (signal) {
|
|
778
|
+
signal.addEventListener(
|
|
779
|
+
"abort",
|
|
780
|
+
() => {
|
|
781
|
+
if (!resolved) requestAbort("signal");
|
|
782
|
+
},
|
|
783
|
+
{ once: true, signal: listenerSignal },
|
|
784
|
+
);
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
// Wall-clock hard limit. Defense-in-depth for the case where a provider stream
|
|
788
|
+
// hang escapes the inference-layer watchdog (see openai-completions
|
|
789
|
+
// `isOpenAICompletionsProgressChunk`). Disabled by default; set
|
|
790
|
+
// `task.maxRuntimeMs > 0` to cap each subagent's lifetime.
|
|
791
|
+
let runtimeTimeoutId: NodeJS.Timeout | undefined;
|
|
792
|
+
if (maxRuntimeMs > 0) {
|
|
793
|
+
runtimeTimeoutId = setTimeout(() => {
|
|
794
|
+
if (!resolved) {
|
|
795
|
+
logger.warn("Subagent runtime limit exceeded; aborting", {
|
|
796
|
+
id,
|
|
797
|
+
agent: agent.name,
|
|
798
|
+
maxRuntimeMs,
|
|
799
|
+
});
|
|
800
|
+
requestAbort("timeout");
|
|
801
|
+
}
|
|
802
|
+
}, maxRuntimeMs);
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
const resolveSignalAbortReason = (): string => {
|
|
806
|
+
const reason = signal?.reason;
|
|
807
|
+
if (reason instanceof Error) {
|
|
808
|
+
const message = reason.message.trim();
|
|
809
|
+
if (message.length > 0) return message;
|
|
810
|
+
} else if (typeof reason === "string") {
|
|
811
|
+
const message = reason.trim();
|
|
812
|
+
if (message.length > 0) return message;
|
|
813
|
+
}
|
|
814
|
+
return "Cancelled by caller";
|
|
815
|
+
};
|
|
816
|
+
const resolveAbortReasonText = (): string => {
|
|
817
|
+
if (runtimeLimitExceeded) {
|
|
818
|
+
return `Subagent runtime limit exceeded (task.maxRuntimeMs=${maxRuntimeMs})`;
|
|
819
|
+
}
|
|
820
|
+
if (budgetLimitExceeded) {
|
|
821
|
+
return `Soft request budget exceeded (${progress.requests} requests; budget ${softRequestBudget})`;
|
|
822
|
+
}
|
|
823
|
+
return resolveSignalAbortReason();
|
|
824
|
+
};
|
|
825
|
+
const PROGRESS_COALESCE_MS = 150;
|
|
826
|
+
let lastProgressEmitMs = 0;
|
|
827
|
+
let progressTimeoutId: NodeJS.Timeout | null = null;
|
|
828
|
+
|
|
829
|
+
const emitProgressNow = () => {
|
|
830
|
+
progress.durationMs = Date.now() - startTime;
|
|
831
|
+
onProgress?.({ ...progress });
|
|
832
|
+
if (args.eventBus) {
|
|
833
|
+
args.eventBus.emit(TASK_SUBAGENT_PROGRESS_CHANNEL, {
|
|
834
|
+
index,
|
|
835
|
+
agent: agent.name,
|
|
836
|
+
agentSource: agent.source,
|
|
837
|
+
task,
|
|
838
|
+
parentToolCallId: args.parentToolCallId,
|
|
839
|
+
assignment,
|
|
840
|
+
progress: { ...progress },
|
|
841
|
+
sessionFile: args.sessionFile,
|
|
842
|
+
});
|
|
843
|
+
}
|
|
844
|
+
lastProgressEmitMs = Date.now();
|
|
845
|
+
};
|
|
846
|
+
|
|
847
|
+
const scheduleProgress = (flush = false) => {
|
|
848
|
+
if (flush) {
|
|
849
|
+
if (progressTimeoutId) {
|
|
850
|
+
clearTimeout(progressTimeoutId);
|
|
851
|
+
progressTimeoutId = null;
|
|
852
|
+
}
|
|
853
|
+
emitProgressNow();
|
|
854
|
+
return;
|
|
855
|
+
}
|
|
856
|
+
const now = Date.now();
|
|
857
|
+
const elapsed = now - lastProgressEmitMs;
|
|
858
|
+
if (lastProgressEmitMs === 0 || elapsed >= PROGRESS_COALESCE_MS) {
|
|
859
|
+
if (progressTimeoutId) {
|
|
860
|
+
clearTimeout(progressTimeoutId);
|
|
861
|
+
progressTimeoutId = null;
|
|
862
|
+
}
|
|
863
|
+
emitProgressNow();
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
866
|
+
if (progressTimeoutId) return;
|
|
867
|
+
progressTimeoutId = setTimeout(() => {
|
|
868
|
+
progressTimeoutId = null;
|
|
869
|
+
emitProgressNow();
|
|
870
|
+
}, PROGRESS_COALESCE_MS - elapsed);
|
|
871
|
+
};
|
|
872
|
+
|
|
873
|
+
const getMessageContent = (message: unknown): unknown => {
|
|
874
|
+
if (message && typeof message === "object" && "content" in message) {
|
|
875
|
+
return (message as { content?: unknown }).content;
|
|
876
|
+
}
|
|
877
|
+
return undefined;
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
const getMessageUsage = (message: unknown): unknown => {
|
|
881
|
+
if (message && typeof message === "object" && "usage" in message) {
|
|
882
|
+
return (message as { usage?: unknown }).usage;
|
|
883
|
+
}
|
|
884
|
+
return undefined;
|
|
885
|
+
};
|
|
886
|
+
|
|
887
|
+
const updateRecentOutputLines = () => {
|
|
888
|
+
const lines = recentOutputTail.split("\n").filter(line => line.trim());
|
|
889
|
+
progress.recentOutput = lines.slice(-8).reverse();
|
|
890
|
+
};
|
|
891
|
+
|
|
892
|
+
const appendRecentOutputTail = (text: string) => {
|
|
893
|
+
if (!text) return;
|
|
894
|
+
recentOutputTail += text;
|
|
895
|
+
if (recentOutputTail.length > RECENT_OUTPUT_TAIL_BYTES) {
|
|
896
|
+
recentOutputTail = recentOutputTail.slice(-RECENT_OUTPUT_TAIL_BYTES);
|
|
897
|
+
}
|
|
898
|
+
updateRecentOutputLines();
|
|
899
|
+
};
|
|
900
|
+
|
|
901
|
+
const replaceRecentOutputFromContent = (content: unknown[]) => {
|
|
902
|
+
recentOutputTail = "";
|
|
903
|
+
for (const block of content) {
|
|
904
|
+
if (!block || typeof block !== "object") continue;
|
|
905
|
+
const record = block as { type?: unknown; text?: unknown };
|
|
906
|
+
if (record.type !== "text" || typeof record.text !== "string") continue;
|
|
907
|
+
if (!record.text) continue;
|
|
908
|
+
recentOutputTail += record.text;
|
|
909
|
+
if (recentOutputTail.length > RECENT_OUTPUT_TAIL_BYTES) {
|
|
910
|
+
recentOutputTail = recentOutputTail.slice(-RECENT_OUTPUT_TAIL_BYTES);
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
updateRecentOutputLines();
|
|
914
|
+
};
|
|
915
|
+
|
|
916
|
+
const resetRecentOutput = () => {
|
|
917
|
+
recentOutputTail = "";
|
|
918
|
+
progress.recentOutput = [];
|
|
919
|
+
};
|
|
920
|
+
|
|
921
|
+
const emitSubagentEvent = (event: AgentSessionEvent) => {
|
|
922
|
+
if (!args.eventBus) return;
|
|
923
|
+
args.eventBus.emit(TASK_SUBAGENT_EVENT_CHANNEL, {
|
|
924
|
+
id,
|
|
925
|
+
event,
|
|
926
|
+
});
|
|
927
|
+
};
|
|
928
|
+
|
|
929
|
+
const processEvent = (event: AgentEvent) => {
|
|
930
|
+
if (resolved) return;
|
|
931
|
+
const now = Date.now();
|
|
932
|
+
let flushProgress = false;
|
|
933
|
+
|
|
934
|
+
switch (event.type) {
|
|
935
|
+
case "message_start":
|
|
936
|
+
if (event.message?.role === "assistant") {
|
|
937
|
+
resetRecentOutput();
|
|
938
|
+
}
|
|
939
|
+
break;
|
|
940
|
+
|
|
941
|
+
case "tool_execution_start": {
|
|
942
|
+
progress.toolCount++;
|
|
943
|
+
progress.currentTool = event.toolName;
|
|
944
|
+
progress.currentToolArgs = extractToolArgsPreview(
|
|
945
|
+
(event as { toolArgs?: Record<string, unknown> }).toolArgs || event.args || {},
|
|
946
|
+
);
|
|
947
|
+
progress.currentToolStartMs = now;
|
|
948
|
+
const intent = event.intent?.trim();
|
|
949
|
+
if (intent) {
|
|
950
|
+
progress.lastIntent = intent;
|
|
951
|
+
}
|
|
952
|
+
// Reset any prior in-flight task snapshot so we don't show stale
|
|
953
|
+
// nested progress when the agent enters a fresh `task` call.
|
|
954
|
+
if (event.toolName === "task") {
|
|
955
|
+
progress.inflightTaskDetails = undefined;
|
|
956
|
+
}
|
|
957
|
+
break;
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
case "tool_execution_end": {
|
|
961
|
+
if (progress.currentTool) {
|
|
962
|
+
progress.recentTools.unshift({
|
|
963
|
+
tool: progress.currentTool,
|
|
964
|
+
args: progress.currentToolArgs || "",
|
|
965
|
+
endMs: now,
|
|
966
|
+
});
|
|
967
|
+
// Keep only last 5
|
|
968
|
+
if (progress.recentTools.length > 5) {
|
|
969
|
+
progress.recentTools.pop();
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
progress.currentTool = undefined;
|
|
973
|
+
progress.currentToolArgs = undefined;
|
|
974
|
+
progress.currentToolStartMs = undefined;
|
|
975
|
+
// The finalized TaskToolDetails will be captured below into
|
|
976
|
+
// `extractedToolData.task`; drop the in-flight snapshot so the
|
|
977
|
+
// renderer doesn't double-count it against the final entry.
|
|
978
|
+
if (event.toolName === "task") {
|
|
979
|
+
progress.inflightTaskDetails = undefined;
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
// Check for registered subagent tool handler
|
|
983
|
+
const handler = subprocessToolRegistry.getHandler(event.toolName);
|
|
984
|
+
const eventArgs = (event as { args?: Record<string, unknown> }).args ?? {};
|
|
985
|
+
if (handler) {
|
|
986
|
+
// Extract data using handler
|
|
987
|
+
if (handler.extractData) {
|
|
988
|
+
const data = handler.extractData({
|
|
989
|
+
toolName: event.toolName,
|
|
990
|
+
toolCallId: event.toolCallId,
|
|
991
|
+
args: eventArgs,
|
|
992
|
+
result: event.result,
|
|
993
|
+
isError: event.isError,
|
|
994
|
+
});
|
|
995
|
+
if (data !== undefined) {
|
|
996
|
+
progress.extractedToolData = progress.extractedToolData || {};
|
|
997
|
+
const existing = progress.extractedToolData[event.toolName] || [];
|
|
998
|
+
const findingKey = event.toolName === "report_finding" ? getReportFindingKey(data) : null;
|
|
999
|
+
if (findingKey) {
|
|
1000
|
+
const existingIndex = existing.findIndex(item => getReportFindingKey(item) === findingKey);
|
|
1001
|
+
if (existingIndex >= 0) {
|
|
1002
|
+
existing[existingIndex] = data;
|
|
1003
|
+
} else {
|
|
1004
|
+
existing.push(data);
|
|
1005
|
+
}
|
|
1006
|
+
} else {
|
|
1007
|
+
existing.push(data);
|
|
1008
|
+
}
|
|
1009
|
+
progress.extractedToolData[event.toolName] = existing;
|
|
1010
|
+
if (event.toolName === "yield") {
|
|
1011
|
+
yieldCalled = true;
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
// Check if handler wants to terminate the session
|
|
1017
|
+
if (
|
|
1018
|
+
handler.shouldTerminate?.({
|
|
1019
|
+
toolName: event.toolName,
|
|
1020
|
+
toolCallId: event.toolCallId,
|
|
1021
|
+
args: eventArgs,
|
|
1022
|
+
result: event.result,
|
|
1023
|
+
isError: event.isError,
|
|
1024
|
+
})
|
|
1025
|
+
) {
|
|
1026
|
+
requestAbort("terminate");
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
flushProgress = true;
|
|
1030
|
+
break;
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
case "tool_execution_update": {
|
|
1034
|
+
// Surface nested-subagent progress mid-flight. The child task
|
|
1035
|
+
// tool emits incremental `onUpdate` calls carrying its current
|
|
1036
|
+
// `TaskToolDetails` (results + progress); we stash the latest
|
|
1037
|
+
// snapshot so the parent UI can render the in-flight subtree
|
|
1038
|
+
// without waiting for the call to finish.
|
|
1039
|
+
if (event.toolName === "task") {
|
|
1040
|
+
const partial = (event as { partialResult?: { details?: unknown } }).partialResult;
|
|
1041
|
+
const details = partial && typeof partial === "object" ? partial.details : undefined;
|
|
1042
|
+
if (details && typeof details === "object" && "results" in (details as TaskToolDetails)) {
|
|
1043
|
+
progress.inflightTaskDetails = details as TaskToolDetails;
|
|
1044
|
+
flushProgress = true;
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
break;
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
case "message_update": {
|
|
1051
|
+
if (event.message?.role !== "assistant") break;
|
|
1052
|
+
const assistantEvent = (
|
|
1053
|
+
event as AgentEvent & {
|
|
1054
|
+
assistantMessageEvent?: { type?: string; delta?: string };
|
|
1055
|
+
}
|
|
1056
|
+
).assistantMessageEvent;
|
|
1057
|
+
if (assistantEvent?.type === "text_delta" && typeof assistantEvent.delta === "string") {
|
|
1058
|
+
appendRecentOutputTail(assistantEvent.delta);
|
|
1059
|
+
break;
|
|
1060
|
+
}
|
|
1061
|
+
if (assistantEvent && assistantEvent.type !== "text_delta") {
|
|
1062
|
+
break;
|
|
1063
|
+
}
|
|
1064
|
+
const updateContent =
|
|
1065
|
+
getMessageContent(event.message) || (event as AgentEvent & { content?: unknown }).content;
|
|
1066
|
+
if (updateContent && Array.isArray(updateContent)) {
|
|
1067
|
+
replaceRecentOutputFromContent(updateContent);
|
|
1068
|
+
}
|
|
1069
|
+
break;
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
case "message_end": {
|
|
1073
|
+
// Extract text from assistant and toolResult messages (not user prompts)
|
|
1074
|
+
const role = event.message?.role;
|
|
1075
|
+
if (role === "assistant") {
|
|
1076
|
+
progress.requests += 1;
|
|
1077
|
+
if (softRequestBudget > 0 && !abortSent) {
|
|
1078
|
+
if (progress.requests >= softRequestBudget * 1.5) {
|
|
1079
|
+
requestAbort("budget");
|
|
1080
|
+
} else if (!budgetSteerSent && progress.requests >= softRequestBudget) {
|
|
1081
|
+
budgetSteerSent = true;
|
|
1082
|
+
const steerSession = activeSession;
|
|
1083
|
+
if (steerSession) {
|
|
1084
|
+
void steerSession
|
|
1085
|
+
.sendUserMessage(buildBudgetNotice(progress.requests), { deliverAs: "steer" })
|
|
1086
|
+
.catch(err => {
|
|
1087
|
+
logger.warn("Subagent budget steer failed", {
|
|
1088
|
+
error: err instanceof Error ? err.message : String(err),
|
|
1089
|
+
});
|
|
1090
|
+
});
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
if (role === "assistant") {
|
|
1096
|
+
const messageContent =
|
|
1097
|
+
getMessageContent(event.message) || (event as AgentEvent & { content?: unknown }).content;
|
|
1098
|
+
if (messageContent && Array.isArray(messageContent)) {
|
|
1099
|
+
for (const block of messageContent) {
|
|
1100
|
+
if (block.type === "text" && block.text) {
|
|
1101
|
+
outputChunks.push(block.text);
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
// Extract and accumulate usage (prefer message.usage, fallback to event.usage)
|
|
1107
|
+
const messageUsage = getMessageUsage(event.message) || (event as AgentEvent & { usage?: unknown }).usage;
|
|
1108
|
+
if (messageUsage && typeof messageUsage === "object") {
|
|
1109
|
+
// Only count assistant messages (not tool results, etc.)
|
|
1110
|
+
if (role === "assistant") {
|
|
1111
|
+
const usageRecord = messageUsage as Record<string, unknown>;
|
|
1112
|
+
const costRecord = (messageUsage as { cost?: Record<string, unknown> }).cost;
|
|
1113
|
+
hasUsage = true;
|
|
1114
|
+
accumulatedUsage.input += getNumberField(usageRecord, "input") ?? 0;
|
|
1115
|
+
accumulatedUsage.output += getNumberField(usageRecord, "output") ?? 0;
|
|
1116
|
+
accumulatedUsage.cacheRead += getNumberField(usageRecord, "cacheRead") ?? 0;
|
|
1117
|
+
accumulatedUsage.cacheWrite += getNumberField(usageRecord, "cacheWrite") ?? 0;
|
|
1118
|
+
accumulatedUsage.totalTokens += getNumberField(usageRecord, "totalTokens") ?? 0;
|
|
1119
|
+
if (costRecord) {
|
|
1120
|
+
accumulatedUsage.cost.input += getNumberField(costRecord, "input") ?? 0;
|
|
1121
|
+
accumulatedUsage.cost.output += getNumberField(costRecord, "output") ?? 0;
|
|
1122
|
+
accumulatedUsage.cost.cacheRead += getNumberField(costRecord, "cacheRead") ?? 0;
|
|
1123
|
+
accumulatedUsage.cost.cacheWrite += getNumberField(costRecord, "cacheWrite") ?? 0;
|
|
1124
|
+
accumulatedUsage.cost.total += getNumberField(costRecord, "total") ?? 0;
|
|
1125
|
+
progress.cost = accumulatedUsage.cost.total;
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
// Accumulate tokens for progress display
|
|
1129
|
+
progress.tokens += getUsageTokens(messageUsage);
|
|
1130
|
+
// Track latest per-turn context size so the UI can show
|
|
1131
|
+
// "current context", not just cumulative billing volume.
|
|
1132
|
+
if (role === "assistant") {
|
|
1133
|
+
const perTurnTotal = getNumberField(messageUsage as Record<string, unknown>, "totalTokens");
|
|
1134
|
+
if (perTurnTotal !== undefined && perTurnTotal > 0) {
|
|
1135
|
+
progress.contextTokens = perTurnTotal;
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
break;
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
case "agent_end":
|
|
1143
|
+
// Extract final content from assistant messages only (not user prompts)
|
|
1144
|
+
if (event.messages && Array.isArray(event.messages)) {
|
|
1145
|
+
for (const msg of event.messages) {
|
|
1146
|
+
if ((msg as { role?: string })?.role !== "assistant") continue;
|
|
1147
|
+
const messageContent = getMessageContent(msg);
|
|
1148
|
+
if (messageContent && Array.isArray(messageContent)) {
|
|
1149
|
+
for (const block of messageContent) {
|
|
1150
|
+
if (block.type === "text" && block.text) {
|
|
1151
|
+
finalOutputChunks.push(block.text);
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
flushProgress = true;
|
|
1158
|
+
break;
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
scheduleProgress(flushProgress);
|
|
1162
|
+
};
|
|
1163
|
+
|
|
1164
|
+
const attach = (session: AgentSession): (() => void) =>
|
|
1165
|
+
session.subscribe(event => {
|
|
1166
|
+
emitSubagentEvent(event);
|
|
1167
|
+
if (event.type === "auto_retry_start") {
|
|
1168
|
+
progress.retryState = {
|
|
1169
|
+
attempt: event.attempt,
|
|
1170
|
+
maxAttempts: event.maxAttempts,
|
|
1171
|
+
delayMs: event.delayMs,
|
|
1172
|
+
errorMessage: event.errorMessage,
|
|
1173
|
+
startedAtMs: Date.now(),
|
|
1174
|
+
};
|
|
1175
|
+
progress.retryFailure = undefined;
|
|
1176
|
+
scheduleProgress(true);
|
|
1177
|
+
return;
|
|
1178
|
+
}
|
|
1179
|
+
if (event.type === "auto_retry_end") {
|
|
1180
|
+
const attempt = progress.retryState?.attempt ?? event.attempt;
|
|
1181
|
+
progress.retryState = undefined;
|
|
1182
|
+
if (!event.success) {
|
|
1183
|
+
progress.retryFailure = {
|
|
1184
|
+
attempt,
|
|
1185
|
+
errorMessage: event.finalError ?? "Auto-retry failed",
|
|
1186
|
+
};
|
|
1187
|
+
}
|
|
1188
|
+
scheduleProgress(true);
|
|
1189
|
+
return;
|
|
1190
|
+
}
|
|
1191
|
+
if (isAgentEvent(event)) {
|
|
1192
|
+
try {
|
|
1193
|
+
processEvent(event);
|
|
1194
|
+
} catch (err) {
|
|
1195
|
+
logger.error("Subagent event processing failed", {
|
|
1196
|
+
error: err instanceof Error ? err.message : String(err),
|
|
1197
|
+
});
|
|
1198
|
+
requestAbort("terminate");
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
});
|
|
1202
|
+
|
|
1203
|
+
const captureSalvage = (session: AgentSession): void => {
|
|
1204
|
+
// Best-effort salvage: capture the last assistant text so
|
|
1205
|
+
// cancelled/aborted children can surface "last activity" instead of
|
|
1206
|
+
// "(no output)".
|
|
1207
|
+
try {
|
|
1208
|
+
const lastContent = session.getLastAssistantMessage()?.content;
|
|
1209
|
+
if (Array.isArray(lastContent)) {
|
|
1210
|
+
const text = lastContent
|
|
1211
|
+
.map(block => (block.type === "text" && typeof block.text === "string" ? block.text : ""))
|
|
1212
|
+
.filter(Boolean)
|
|
1213
|
+
.join("\n");
|
|
1214
|
+
if (text.trim()) {
|
|
1215
|
+
lastAssistantSalvageText = text;
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
} catch {
|
|
1219
|
+
// Salvage is best-effort; partial sessions may not implement it
|
|
1220
|
+
}
|
|
1221
|
+
};
|
|
1222
|
+
|
|
1223
|
+
return {
|
|
1224
|
+
progress,
|
|
1225
|
+
abortSignal,
|
|
1226
|
+
accumulatedUsage,
|
|
1227
|
+
hasUsage: () => hasUsage,
|
|
1228
|
+
yieldCalled: () => yieldCalled,
|
|
1229
|
+
runtimeLimitExceeded: () => runtimeLimitExceeded,
|
|
1230
|
+
hasExplicitAbortReason: () => abortReason === "signal" || runtimeLimitExceeded || budgetLimitExceeded,
|
|
1231
|
+
isAbortedRun: () =>
|
|
1232
|
+
abortReason === "signal" || runtimeLimitExceeded || budgetLimitExceeded || abortReason === undefined,
|
|
1233
|
+
requestAbort,
|
|
1234
|
+
resolveSignalAbortReason,
|
|
1235
|
+
resolveAbortReasonText,
|
|
1236
|
+
setActiveSession: session => {
|
|
1237
|
+
activeSession = session;
|
|
1238
|
+
},
|
|
1239
|
+
takeActiveSession: () => {
|
|
1240
|
+
const session = activeSession;
|
|
1241
|
+
activeSession = null;
|
|
1242
|
+
return session;
|
|
1243
|
+
},
|
|
1244
|
+
attach,
|
|
1245
|
+
captureSalvage,
|
|
1246
|
+
lastAssistantSalvageText: () => lastAssistantSalvageText,
|
|
1247
|
+
rawOutput: () => (finalOutputChunks.length > 0 ? finalOutputChunks.join("") : outputChunks.join("")),
|
|
1248
|
+
scheduleProgress,
|
|
1249
|
+
finish: () => {
|
|
1250
|
+
resolved = true;
|
|
1251
|
+
listenerController.abort();
|
|
1252
|
+
if (runtimeTimeoutId !== undefined) {
|
|
1253
|
+
clearTimeout(runtimeTimeoutId);
|
|
1254
|
+
runtimeTimeoutId = undefined;
|
|
1255
|
+
}
|
|
1256
|
+
if (progressTimeoutId) {
|
|
1257
|
+
clearTimeout(progressTimeoutId);
|
|
1258
|
+
progressTimeoutId = null;
|
|
1259
|
+
}
|
|
1260
|
+
},
|
|
1261
|
+
};
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
interface DriveOutcome {
|
|
1265
|
+
exitCode: number;
|
|
1266
|
+
error?: string;
|
|
1267
|
+
aborted: boolean;
|
|
1268
|
+
abortReasonText?: string;
|
|
1269
|
+
}
|
|
1270
|
+
|
|
1271
|
+
const MAX_YIELD_RETRIES = 3;
|
|
1272
|
+
|
|
1273
|
+
/**
|
|
1274
|
+
* Drive one assignment through a live session: send the prompt, wait for idle,
|
|
1275
|
+
* remind the agent to `yield` (up to {@link MAX_YIELD_RETRIES} times), then
|
|
1276
|
+
* classify the terminal assistant state.
|
|
1277
|
+
*/
|
|
1278
|
+
async function driveSessionToYield(
|
|
1279
|
+
session: AgentSession,
|
|
1280
|
+
monitor: SubagentRunMonitor,
|
|
1281
|
+
task: string,
|
|
1282
|
+
): Promise<DriveOutcome> {
|
|
1283
|
+
const abortSignal = monitor.abortSignal;
|
|
1284
|
+
let exitCode = 0;
|
|
1285
|
+
let error: string | undefined;
|
|
1286
|
+
let aborted = false;
|
|
1287
|
+
let abortReasonText: string | undefined;
|
|
1288
|
+
const checkAbort = () => {
|
|
1289
|
+
if (abortSignal.aborted) {
|
|
1290
|
+
aborted = monitor.isAbortedRun();
|
|
1291
|
+
if (aborted) {
|
|
1292
|
+
abortReasonText ??= monitor.resolveAbortReasonText();
|
|
1293
|
+
}
|
|
1294
|
+
exitCode = 1;
|
|
1295
|
+
throw new ToolAbortError();
|
|
1296
|
+
}
|
|
1297
|
+
};
|
|
1298
|
+
const awaitAbortable = async <T>(promise: Promise<T>): Promise<T> => {
|
|
1299
|
+
checkAbort();
|
|
1300
|
+
const { promise: abortPromise, reject } = Promise.withResolvers<never>();
|
|
1301
|
+
const onAbort = () => {
|
|
1302
|
+
try {
|
|
1303
|
+
checkAbort();
|
|
1304
|
+
} catch (err) {
|
|
1305
|
+
reject(err);
|
|
1306
|
+
}
|
|
1307
|
+
};
|
|
1308
|
+
abortSignal.addEventListener("abort", onAbort, { once: true });
|
|
1309
|
+
try {
|
|
1310
|
+
return await Promise.race([promise, abortPromise]);
|
|
1311
|
+
} finally {
|
|
1312
|
+
abortSignal.removeEventListener("abort", onAbort);
|
|
1313
|
+
}
|
|
1314
|
+
};
|
|
1315
|
+
|
|
1316
|
+
try {
|
|
1317
|
+
await awaitAbortable(session.prompt(task, { attribution: "agent" }));
|
|
1318
|
+
await awaitAbortable(session.waitForIdle());
|
|
1319
|
+
|
|
1320
|
+
const reminderToolChoice = buildNamedToolChoice("yield", session.model);
|
|
1321
|
+
|
|
1322
|
+
let retryCount = 0;
|
|
1323
|
+
while (!monitor.yieldCalled() && retryCount < MAX_YIELD_RETRIES && !abortSignal.aborted) {
|
|
1324
|
+
// Skip reminders when the model returned a terminal error (e.g.
|
|
1325
|
+
// rate-limit cap hit, auth failure). Re-prompting would just
|
|
1326
|
+
// hit the same wall, multiplying the failure noise without
|
|
1327
|
+
// any chance of producing a yield.
|
|
1328
|
+
const lastBeforeReminder = session.getLastAssistantMessage();
|
|
1329
|
+
if (lastBeforeReminder?.stopReason === "error") break;
|
|
1330
|
+
try {
|
|
1331
|
+
retryCount++;
|
|
1332
|
+
const reminder = prompt.render(submitReminderTemplate, {
|
|
1333
|
+
retryCount,
|
|
1334
|
+
maxRetries: MAX_YIELD_RETRIES,
|
|
1335
|
+
});
|
|
1336
|
+
|
|
1337
|
+
const isFinalRetry = retryCount >= MAX_YIELD_RETRIES;
|
|
1338
|
+
await awaitAbortable(
|
|
1339
|
+
session.prompt(reminder, {
|
|
1340
|
+
attribution: "agent",
|
|
1341
|
+
synthetic: true,
|
|
1342
|
+
...(isFinalRetry && reminderToolChoice ? { toolChoice: reminderToolChoice } : {}),
|
|
1343
|
+
}),
|
|
1344
|
+
);
|
|
1345
|
+
await awaitAbortable(session.waitForIdle());
|
|
1346
|
+
} catch (err) {
|
|
1347
|
+
if (abortSignal.aborted || err instanceof ToolAbortError) {
|
|
1348
|
+
// Benign control-flow exit — user cancel (^C) or compaction aborting
|
|
1349
|
+
// pending operations both surface here as ToolAbortError. The outer
|
|
1350
|
+
// catch and finally already mark the run aborted; logging at ERROR
|
|
1351
|
+
// would spam operator dashboards with non-failures.
|
|
1352
|
+
logger.debug("Subagent prompt aborted");
|
|
1353
|
+
} else {
|
|
1354
|
+
logger.error("Subagent prompt failed", {
|
|
1355
|
+
error: err instanceof Error ? err.message : String(err),
|
|
1356
|
+
});
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
|
|
1361
|
+
await awaitAbortable(session.waitForIdle());
|
|
1362
|
+
|
|
1363
|
+
const lastAssistant = session.getLastAssistantMessage();
|
|
1364
|
+
if (lastAssistant) {
|
|
1365
|
+
if (lastAssistant.stopReason === "aborted") {
|
|
1366
|
+
aborted = monitor.isAbortedRun();
|
|
1367
|
+
if (aborted) {
|
|
1368
|
+
// A real caller signal or the wall-clock timer carries a precise
|
|
1369
|
+
// reason (signal.reason / "runtime limit exceeded"). An internal
|
|
1370
|
+
// turn abort does NOT — prefer the assistant message's own
|
|
1371
|
+
// errorMessage ("Request was aborted" or a specific stream error)
|
|
1372
|
+
// over the misleading "Cancelled by caller".
|
|
1373
|
+
abortReasonText ??= monitor.hasExplicitAbortReason()
|
|
1374
|
+
? monitor.resolveAbortReasonText()
|
|
1375
|
+
: lastAssistant.errorMessage?.trim() || monitor.resolveAbortReasonText();
|
|
1376
|
+
}
|
|
1377
|
+
exitCode = 1;
|
|
1378
|
+
} else if (lastAssistant.stopReason === "error") {
|
|
1379
|
+
exitCode = 1;
|
|
1380
|
+
error ??= lastAssistant.errorMessage || "Subagent failed";
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
} catch (err) {
|
|
1384
|
+
exitCode = 1;
|
|
1385
|
+
if (!abortSignal.aborted) {
|
|
1386
|
+
error = err instanceof Error ? err.stack || err.message : String(err);
|
|
1387
|
+
}
|
|
1388
|
+
} finally {
|
|
1389
|
+
if (abortSignal.aborted) {
|
|
1390
|
+
aborted = monitor.isAbortedRun();
|
|
1391
|
+
if (aborted) {
|
|
1392
|
+
abortReasonText ??= monitor.resolveAbortReasonText();
|
|
1393
|
+
}
|
|
1394
|
+
if (exitCode === 0) exitCode = 1;
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1398
|
+
return { exitCode, error, aborted, abortReasonText };
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
interface FinalizeRunArgs {
|
|
1402
|
+
monitor: SubagentRunMonitor;
|
|
1403
|
+
done: { exitCode: number; error?: string; aborted?: boolean; abortReason?: string; durationMs: number };
|
|
1404
|
+
index: number;
|
|
1405
|
+
id: string;
|
|
1406
|
+
agent: AgentDefinition;
|
|
1407
|
+
task: string;
|
|
1408
|
+
assignment?: string;
|
|
1409
|
+
description?: string;
|
|
1410
|
+
modelOverride?: string | string[];
|
|
1411
|
+
outputSchema?: unknown;
|
|
1412
|
+
signal?: AbortSignal;
|
|
1413
|
+
artifactsDir?: string;
|
|
1414
|
+
eventBus?: EventBus;
|
|
1415
|
+
parentToolCallId?: string;
|
|
1416
|
+
sessionFile?: string;
|
|
1417
|
+
startTime: number;
|
|
1418
|
+
}
|
|
1419
|
+
|
|
1420
|
+
/**
|
|
1421
|
+
* Turn a settled run into a {@link SingleResult}: resolve the yield payload via
|
|
1422
|
+
* {@link finalizeSubprocessOutput}, salvage cancelled-run output, write the
|
|
1423
|
+
* `<id>.md` output artifact, flush final progress, and emit the lifecycle end
|
|
1424
|
+
* event.
|
|
1425
|
+
*/
|
|
1426
|
+
async function finalizeRunResult(args: FinalizeRunArgs): Promise<SingleResult> {
|
|
1427
|
+
const { monitor, done, index, id, agent, task, assignment, signal, modelOverride } = args;
|
|
1428
|
+
const progress = monitor.progress;
|
|
1429
|
+
let exitCode = done.exitCode;
|
|
1430
|
+
let stderr = done.error ?? "";
|
|
1431
|
+
|
|
1432
|
+
// Use final output if available, otherwise accumulated output
|
|
1433
|
+
let rawOutput = monitor.rawOutput();
|
|
1434
|
+
const yieldItems = progress.extractedToolData?.yield as YieldItem[] | undefined;
|
|
1435
|
+
const reportFindingDetails = progress.extractedToolData?.report_finding as ReportFindingDetails[] | undefined;
|
|
1436
|
+
const reportFindings: ReviewFinding[] | undefined = reportFindingDetails?.map(toReviewFinding);
|
|
1437
|
+
const finalized = finalizeSubprocessOutput({
|
|
1438
|
+
rawOutput,
|
|
1439
|
+
exitCode,
|
|
1440
|
+
stderr,
|
|
1441
|
+
doneAborted: Boolean(done.aborted),
|
|
1442
|
+
signalAborted: Boolean(signal?.aborted),
|
|
1443
|
+
yieldItems,
|
|
1444
|
+
reportFindings,
|
|
1445
|
+
outputSchema: args.outputSchema,
|
|
1446
|
+
});
|
|
1447
|
+
rawOutput = finalized.rawOutput;
|
|
1448
|
+
exitCode = finalized.exitCode;
|
|
1449
|
+
stderr = finalized.stderr;
|
|
1450
|
+
// Salvage for cancelled/aborted children that produced no completed output:
|
|
1451
|
+
// surface the last assistant text + stats instead of "(no output)" so the
|
|
1452
|
+
// parent doesn't redo work the child already finished.
|
|
1453
|
+
const salvageText = monitor.lastAssistantSalvageText();
|
|
1454
|
+
if (
|
|
1455
|
+
(done.aborted || signal?.aborted || monitor.runtimeLimitExceeded()) &&
|
|
1456
|
+
!rawOutput.trim() &&
|
|
1457
|
+
salvageText !== undefined
|
|
1458
|
+
) {
|
|
1459
|
+
rawOutput = `[cancelled after ${progress.requests} req, ${progress.tokens} tok — last activity: "${formatSalvageSnippet(salvageText)}"]`;
|
|
1460
|
+
}
|
|
1461
|
+
const lastYield = yieldItems?.[yieldItems.length - 1];
|
|
1462
|
+
const yieldAbortReason = lastYield?.status === "aborted" ? lastYield.error || "Subagent aborted task" : undefined;
|
|
1463
|
+
const { abortedViaYield, hasYield } = finalized;
|
|
1464
|
+
const { content: truncatedOutput, truncated } = truncateTail(rawOutput, {
|
|
1465
|
+
maxBytes: MAX_OUTPUT_BYTES,
|
|
1466
|
+
maxLines: MAX_OUTPUT_LINES,
|
|
1467
|
+
});
|
|
1468
|
+
|
|
1469
|
+
// Write output artifact (input and jsonl already written in real-time)
|
|
1470
|
+
// Compute output metadata for agent:// URL integration
|
|
1471
|
+
let outputMeta: { lineCount: number; charCount: number } | undefined;
|
|
1472
|
+
let outputPath: string | undefined;
|
|
1473
|
+
if (args.artifactsDir) {
|
|
1474
|
+
outputPath = path.join(args.artifactsDir, `${id}.md`);
|
|
1475
|
+
try {
|
|
1476
|
+
await Bun.write(outputPath, rawOutput);
|
|
1477
|
+
outputMeta = {
|
|
1478
|
+
lineCount: rawOutput.split("\n").length,
|
|
1479
|
+
charCount: rawOutput.length,
|
|
1480
|
+
};
|
|
1481
|
+
} catch {
|
|
1482
|
+
// Non-fatal
|
|
1483
|
+
}
|
|
1484
|
+
}
|
|
1485
|
+
|
|
1486
|
+
// Update final progress. A wall-clock timeout always wins: if the runtime
|
|
1487
|
+
// limit fired we report aborted/failed regardless of whether a yield landed
|
|
1488
|
+
// while we were tearing the session down. The yield data is still surfaced
|
|
1489
|
+
// to the caller via `progress.extractedToolData`, but the exit status must
|
|
1490
|
+
// reflect the timeout so on-call doesn't mistake a stuck run for success.
|
|
1491
|
+
const runtimeLimitExceeded = monitor.runtimeLimitExceeded();
|
|
1492
|
+
if (runtimeLimitExceeded && exitCode === 0) {
|
|
1493
|
+
exitCode = 1;
|
|
1494
|
+
}
|
|
1495
|
+
const wasAborted =
|
|
1496
|
+
runtimeLimitExceeded || abortedViaYield || (!hasYield && (done.aborted || signal?.aborted || false));
|
|
1497
|
+
const finalAbortReason = wasAborted
|
|
1498
|
+
? runtimeLimitExceeded
|
|
1499
|
+
? monitor.resolveAbortReasonText()
|
|
1500
|
+
: abortedViaYield
|
|
1501
|
+
? yieldAbortReason
|
|
1502
|
+
: (done.abortReason ??
|
|
1503
|
+
(signal?.aborted ? monitor.resolveSignalAbortReason() : monitor.resolveAbortReasonText()))
|
|
1504
|
+
: undefined;
|
|
1505
|
+
progress.status = wasAborted ? "aborted" : exitCode === 0 ? "completed" : "failed";
|
|
1506
|
+
monitor.scheduleProgress(true);
|
|
1507
|
+
|
|
1508
|
+
// Emit lifecycle end event after finalization so yield status is reflected
|
|
1509
|
+
if (args.eventBus) {
|
|
1510
|
+
args.eventBus.emit(TASK_SUBAGENT_LIFECYCLE_CHANNEL, {
|
|
1511
|
+
id,
|
|
1512
|
+
agent: agent.name,
|
|
1513
|
+
parentToolCallId: args.parentToolCallId,
|
|
1514
|
+
agentSource: agent.source,
|
|
1515
|
+
description: args.description,
|
|
1516
|
+
status: progress.status as "completed" | "failed" | "aborted",
|
|
1517
|
+
sessionFile: args.sessionFile,
|
|
1518
|
+
index,
|
|
1519
|
+
});
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
return {
|
|
1523
|
+
index,
|
|
1524
|
+
id,
|
|
1525
|
+
agent: agent.name,
|
|
1526
|
+
agentSource: agent.source,
|
|
1527
|
+
task,
|
|
1528
|
+
assignment,
|
|
1529
|
+
description: args.description,
|
|
1530
|
+
lastIntent: progress.lastIntent,
|
|
1531
|
+
exitCode,
|
|
1532
|
+
output: truncatedOutput,
|
|
1533
|
+
stderr,
|
|
1534
|
+
truncated: Boolean(truncated),
|
|
1535
|
+
durationMs: Date.now() - args.startTime,
|
|
1536
|
+
tokens: progress.tokens,
|
|
1537
|
+
requests: progress.requests,
|
|
1538
|
+
contextTokens: progress.contextTokens,
|
|
1539
|
+
contextWindow: progress.contextWindow,
|
|
1540
|
+
modelOverride,
|
|
1541
|
+
resolvedModel: progress.resolvedModel,
|
|
1542
|
+
error: exitCode !== 0 && stderr ? stderr : undefined,
|
|
1543
|
+
aborted: wasAborted,
|
|
1544
|
+
abortReason: finalAbortReason,
|
|
1545
|
+
usage: monitor.hasUsage() ? monitor.accumulatedUsage : undefined,
|
|
1546
|
+
outputPath,
|
|
1547
|
+
extractedToolData: progress.extractedToolData,
|
|
1548
|
+
retryFailure: progress.retryFailure,
|
|
1549
|
+
outputMeta,
|
|
1550
|
+
};
|
|
1551
|
+
}
|
|
1552
|
+
|
|
1553
|
+
/**
|
|
1554
|
+
* Run a single agent in-process.
|
|
1555
|
+
*/
|
|
1556
|
+
export async function runSubprocess(options: ExecutorOptions): Promise<SingleResult> {
|
|
1557
|
+
const {
|
|
1558
|
+
cwd,
|
|
1559
|
+
agent,
|
|
1560
|
+
task,
|
|
1561
|
+
assignment,
|
|
1562
|
+
index,
|
|
1563
|
+
id,
|
|
1564
|
+
worktree,
|
|
1565
|
+
modelOverride,
|
|
1566
|
+
thinkingLevel,
|
|
1567
|
+
outputSchema,
|
|
1568
|
+
enableLsp,
|
|
1569
|
+
signal,
|
|
1570
|
+
onProgress,
|
|
1571
|
+
} = options;
|
|
1572
|
+
const startTime = Date.now();
|
|
1573
|
+
|
|
1574
|
+
// Check if already aborted
|
|
1575
|
+
if (signal?.aborted) {
|
|
1576
|
+
return {
|
|
1577
|
+
index,
|
|
1578
|
+
id,
|
|
1579
|
+
agent: agent.name,
|
|
1580
|
+
agentSource: agent.source,
|
|
1581
|
+
task,
|
|
1582
|
+
assignment,
|
|
1583
|
+
description: options.description,
|
|
1584
|
+
exitCode: 1,
|
|
1585
|
+
output: "",
|
|
1586
|
+
stderr: "Cancelled before start",
|
|
1587
|
+
truncated: false,
|
|
1588
|
+
durationMs: 0,
|
|
1589
|
+
tokens: 0,
|
|
1590
|
+
requests: 0,
|
|
1591
|
+
modelOverride,
|
|
1592
|
+
error: "Cancelled before start",
|
|
1593
|
+
aborted: true,
|
|
1594
|
+
abortReason: "Cancelled before start",
|
|
1595
|
+
};
|
|
1596
|
+
}
|
|
1597
|
+
|
|
1598
|
+
// Set up artifact paths and write input file upfront if artifacts dir provided
|
|
1599
|
+
let subtaskSessionFile: string | undefined;
|
|
1600
|
+
if (options.artifactsDir) {
|
|
1601
|
+
subtaskSessionFile = path.join(options.artifactsDir, `${id}.jsonl`);
|
|
1602
|
+
}
|
|
1603
|
+
|
|
1604
|
+
const settings = options.settings ?? Settings.isolated();
|
|
1605
|
+
const subagentSettings = createSubagentSettings(
|
|
1606
|
+
settings,
|
|
1607
|
+
agent.readSummarize === false ? { "read.summarize.enabled": false } : undefined,
|
|
1608
|
+
);
|
|
1609
|
+
const maxRecursionDepth = settings.get("task.maxRecursionDepth") ?? 2;
|
|
1610
|
+
const maxRuntimeMs = Math.max(
|
|
1611
|
+
0,
|
|
1612
|
+
Math.trunc(Number(options.maxRuntimeMs ?? settings.get("task.maxRuntimeMs") ?? 0) || 0),
|
|
1613
|
+
);
|
|
1614
|
+
// TTL before an adopted idle subagent is parked by the lifecycle manager.
|
|
1615
|
+
// <= 0 disables parking (the session stays live until process teardown).
|
|
1616
|
+
const agentIdleTtlMs = Math.trunc(Number(settings.get("task.agentIdleTtlMs") ?? 420_000) || 0);
|
|
1617
|
+
const configuredDefaultBudget = Math.max(
|
|
1618
|
+
0,
|
|
1619
|
+
Math.trunc(Number(settings.get("task.softRequestBudget") ?? SOFT_REQUEST_BUDGET.default) || 0),
|
|
1620
|
+
);
|
|
1621
|
+
const softRequestBudget =
|
|
1622
|
+
configuredDefaultBudget === 0 ? 0 : (SOFT_REQUEST_BUDGET[agent.name] ?? configuredDefaultBudget);
|
|
1623
|
+
const parentDepth = options.taskDepth ?? 0;
|
|
1624
|
+
const childDepth = parentDepth + 1;
|
|
1625
|
+
const atMaxDepth = maxRecursionDepth >= 0 && childDepth >= maxRecursionDepth;
|
|
1626
|
+
|
|
1627
|
+
// Add tools if specified
|
|
1628
|
+
let toolNames: string[] | undefined;
|
|
1629
|
+
if (agent.tools && agent.tools.length > 0) {
|
|
1630
|
+
toolNames = agent.tools;
|
|
1631
|
+
// Auto-include task tool if spawns defined but task not in tools
|
|
1632
|
+
if (agent.spawns !== undefined && !toolNames.includes("task") && !atMaxDepth) {
|
|
1633
|
+
toolNames = [...toolNames, "task"];
|
|
1634
|
+
}
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1637
|
+
if (atMaxDepth && toolNames?.includes("task")) {
|
|
1638
|
+
toolNames = toolNames.filter(name => name !== "task");
|
|
1639
|
+
}
|
|
1640
|
+
// IRC is always available; the COOP prompt section advertises it, so a restricted
|
|
1641
|
+
// whitelist must still carry `irc` for the subagent to actually use it.
|
|
1642
|
+
if (toolNames && !toolNames.includes("irc")) {
|
|
1643
|
+
toolNames = [...toolNames, "irc"];
|
|
1644
|
+
}
|
|
1645
|
+
if (toolNames?.includes("exec")) {
|
|
1646
|
+
const allowEvalPy = settings.get("eval.py") ?? true;
|
|
1647
|
+
const allowEvalJs = settings.get("eval.js") ?? true;
|
|
1648
|
+
const expanded = toolNames.filter(name => name !== "exec");
|
|
1649
|
+
if (allowEvalPy || allowEvalJs) expanded.push("eval");
|
|
1650
|
+
expanded.push("bash");
|
|
1651
|
+
toolNames = Array.from(new Set(expanded));
|
|
1652
|
+
}
|
|
1653
|
+
|
|
1654
|
+
const modelPatterns = normalizeModelPatterns(modelOverride ?? agent.model);
|
|
1655
|
+
const sessionFile = subtaskSessionFile ?? null;
|
|
1656
|
+
const spawnsEnv = atMaxDepth
|
|
1657
|
+
? ""
|
|
1658
|
+
: agent.spawns === undefined
|
|
1659
|
+
? ""
|
|
1660
|
+
: agent.spawns === "*"
|
|
1661
|
+
? "*"
|
|
1662
|
+
: agent.spawns.join(",");
|
|
1663
|
+
|
|
1664
|
+
const lspEnabled = enableLsp ?? true;
|
|
1665
|
+
const ircEnabled = isIrcEnabled(subagentSettings, childDepth);
|
|
1666
|
+
const skipPythonPreflight = Array.isArray(toolNames) && !toolNames.includes("eval");
|
|
1667
|
+
|
|
1668
|
+
const monitor = createSubagentRunMonitor({
|
|
1669
|
+
index,
|
|
1670
|
+
id,
|
|
1671
|
+
agent,
|
|
1672
|
+
task,
|
|
1673
|
+
assignment,
|
|
1674
|
+
description: options.description,
|
|
1675
|
+
modelOverride,
|
|
1676
|
+
signal,
|
|
1677
|
+
onProgress,
|
|
1678
|
+
eventBus: options.eventBus,
|
|
1679
|
+
parentToolCallId: options.parentToolCallId,
|
|
1680
|
+
sessionFile: subtaskSessionFile,
|
|
1681
|
+
softRequestBudget,
|
|
1682
|
+
maxRuntimeMs,
|
|
1683
|
+
});
|
|
1684
|
+
const progress = monitor.progress;
|
|
1685
|
+
let unsubscribe: (() => void) | null = null;
|
|
1686
|
+
let reviveSession: (() => Promise<AgentSession>) | null = null;
|
|
1687
|
+
// Adopted (kept-alive) subagents flip registry status from session events on
|
|
1688
|
+
// later turns: revive/wake → running, turn drained → idle. The subscription
|
|
1689
|
+
// intentionally survives this run; a disposed session emits nothing, so it
|
|
1690
|
+
// needs no teardown.
|
|
1691
|
+
const installRegistryStatusSync = (target: AgentSession): void => {
|
|
1692
|
+
target.subscribe(event => {
|
|
1693
|
+
if (event.type === "agent_start") {
|
|
1694
|
+
AgentRegistry.global().setStatus(id, "running");
|
|
1695
|
+
} else if (event.type === "agent_end") {
|
|
1696
|
+
AgentRegistry.global().setStatus(id, "idle");
|
|
1697
|
+
}
|
|
1698
|
+
});
|
|
1699
|
+
};
|
|
1700
|
+
|
|
1701
|
+
const runSubagent = async (): Promise<{
|
|
1702
|
+
exitCode: number;
|
|
1703
|
+
error?: string;
|
|
1704
|
+
aborted?: boolean;
|
|
1705
|
+
abortReason?: string;
|
|
1706
|
+
durationMs: number;
|
|
1707
|
+
}> => {
|
|
1708
|
+
const sessionAbortController = new AbortController();
|
|
1709
|
+
const abortSignal = monitor.abortSignal;
|
|
1710
|
+
let exitCode = 0;
|
|
1711
|
+
let error: string | undefined;
|
|
1712
|
+
let aborted = false;
|
|
1713
|
+
let abortReasonText: string | undefined;
|
|
1714
|
+
const checkAbort = () => {
|
|
1715
|
+
if (abortSignal.aborted) {
|
|
1716
|
+
throw new ToolAbortError();
|
|
1717
|
+
}
|
|
1718
|
+
};
|
|
1719
|
+
const awaitAbortable = async <T>(promise: Promise<T>): Promise<T> => {
|
|
1720
|
+
checkAbort();
|
|
1721
|
+
const { promise: abortPromise, reject } = Promise.withResolvers<never>();
|
|
1722
|
+
const onAbort = () => {
|
|
1723
|
+
try {
|
|
1724
|
+
checkAbort();
|
|
1725
|
+
} catch (err) {
|
|
1726
|
+
reject(err);
|
|
1727
|
+
}
|
|
1728
|
+
};
|
|
1729
|
+
abortSignal.addEventListener("abort", onAbort, { once: true });
|
|
1730
|
+
try {
|
|
1731
|
+
return await Promise.race([promise, abortPromise]);
|
|
1732
|
+
} finally {
|
|
1733
|
+
abortSignal.removeEventListener("abort", onAbort);
|
|
1734
|
+
}
|
|
1735
|
+
};
|
|
1736
|
+
|
|
1737
|
+
try {
|
|
1738
|
+
checkAbort();
|
|
1739
|
+
// Pin authStorage to modelRegistry.authStorage — mirrors the createAgentSession invariant.
|
|
1740
|
+
const registryFromParent = options.modelRegistry !== undefined;
|
|
1741
|
+
const modelRegistry =
|
|
1742
|
+
options.modelRegistry ??
|
|
1743
|
+
new ModelRegistry(options.authStorage ?? (await awaitAbortable(discoverAuthStorage())));
|
|
1744
|
+
const authStorage = modelRegistry.authStorage;
|
|
1745
|
+
if (options.authStorage && options.authStorage !== authStorage) {
|
|
1746
|
+
throw new Error(
|
|
1747
|
+
"options.authStorage and options.modelRegistry.authStorage must be the same instance when both are provided",
|
|
1748
|
+
);
|
|
1749
|
+
}
|
|
1750
|
+
checkAbort();
|
|
1751
|
+
if (!registryFromParent) {
|
|
1752
|
+
await awaitAbortable(modelRegistry.refresh());
|
|
1753
|
+
} else {
|
|
1754
|
+
logger.debug("runSubagent: reusing parent modelRegistry; skipping refresh");
|
|
1755
|
+
}
|
|
1756
|
+
checkAbort();
|
|
1757
|
+
|
|
1758
|
+
const {
|
|
1759
|
+
model,
|
|
1760
|
+
thinkingLevel: resolvedThinkingLevel,
|
|
1761
|
+
explicitThinkingLevel,
|
|
1762
|
+
authFallbackUsed,
|
|
1763
|
+
} = await awaitAbortable(
|
|
1764
|
+
resolveModelOverrideWithAuthFallback(
|
|
1765
|
+
modelPatterns,
|
|
1766
|
+
options.parentActiveModelPattern,
|
|
1767
|
+
modelRegistry,
|
|
1768
|
+
settings,
|
|
1769
|
+
),
|
|
1770
|
+
);
|
|
1771
|
+
if (authFallbackUsed && model) {
|
|
1772
|
+
logger.warn("Subagent model has no working credentials; falling back to parent session model", {
|
|
1773
|
+
requested: modelPatterns,
|
|
1774
|
+
parentModel: options.parentActiveModelPattern,
|
|
1775
|
+
resolvedProvider: model.provider,
|
|
1776
|
+
resolvedModel: model.id,
|
|
1777
|
+
});
|
|
1778
|
+
}
|
|
1779
|
+
if (model?.contextWindow && model.contextWindow > 0) {
|
|
1780
|
+
progress.contextWindow = model.contextWindow;
|
|
1781
|
+
}
|
|
1782
|
+
if (model) {
|
|
1783
|
+
progress.resolvedModel = explicitThinkingLevel
|
|
1784
|
+
? `${model.provider}/${model.id}:${resolvedThinkingLevel}`
|
|
1785
|
+
: `${model.provider}/${model.id}`;
|
|
1786
|
+
}
|
|
1787
|
+
const effectiveThinkingLevel = explicitThinkingLevel
|
|
1788
|
+
? resolvedThinkingLevel
|
|
1789
|
+
: (thinkingLevel ?? resolvedThinkingLevel);
|
|
1790
|
+
|
|
1791
|
+
const sessionManager = sessionFile
|
|
1792
|
+
? await awaitAbortable(SessionManager.open(sessionFile))
|
|
1793
|
+
: SessionManager.inMemory(worktree ?? cwd);
|
|
1794
|
+
if (options.parentArtifactManager) {
|
|
1795
|
+
sessionManager.adoptArtifactManager(options.parentArtifactManager);
|
|
1796
|
+
}
|
|
1797
|
+
|
|
1798
|
+
const mcpProxyTools = options.mcpManager ? createMCPProxyTools(options.mcpManager) : [];
|
|
1799
|
+
const enableMCP = !options.mcpManager;
|
|
1800
|
+
|
|
1801
|
+
// Derive subagent-scoped telemetry from the parent's config so the
|
|
1802
|
+
// child loop's spans nest under the parent's active execute_tool span
|
|
1803
|
+
// (OTEL context propagation handles parent linkage automatically),
|
|
1804
|
+
// carry the subagent's own agent identity, and use the subagent's
|
|
1805
|
+
// own session id for `gen_ai.conversation.id`.
|
|
1806
|
+
const subagentAgentIdentity: AgentIdentity | undefined = options.parentTelemetry
|
|
1807
|
+
? { id, name: agent.name, description: agent.description }
|
|
1808
|
+
: undefined;
|
|
1809
|
+
const subagentTelemetry: AgentTelemetryConfig | undefined =
|
|
1810
|
+
options.parentTelemetry && subagentAgentIdentity
|
|
1811
|
+
? {
|
|
1812
|
+
...options.parentTelemetry,
|
|
1813
|
+
agent: subagentAgentIdentity,
|
|
1814
|
+
// Clear parent's conversationId; the child loop falls back to
|
|
1815
|
+
// its own AgentLoopConfig.sessionId.
|
|
1816
|
+
conversationId: undefined,
|
|
1817
|
+
}
|
|
1818
|
+
: undefined;
|
|
1819
|
+
|
|
1820
|
+
if (options.parentTelemetry && subagentAgentIdentity) {
|
|
1821
|
+
const parentTelemetryHandle = resolveTelemetry(
|
|
1822
|
+
options.parentTelemetry,
|
|
1823
|
+
options.parentTelemetry.conversationId,
|
|
1824
|
+
);
|
|
1825
|
+
recordHandoff(parentTelemetryHandle, {
|
|
1826
|
+
fromAgent: options.parentTelemetry.agent,
|
|
1827
|
+
toAgent: subagentAgentIdentity,
|
|
1828
|
+
});
|
|
1829
|
+
}
|
|
1830
|
+
|
|
1831
|
+
const { normalized: normalizedOutputSchema } = normalizeSchema(outputSchema);
|
|
1832
|
+
|
|
1833
|
+
// Captured by the lifecycle reviver: rebuilding an equivalent session from
|
|
1834
|
+
// the same JSONL file re-invokes createAgentSession with the exact options
|
|
1835
|
+
// of the original run (same agent id, tools, model, system prompt,
|
|
1836
|
+
// artifacts dir) — only the SessionManager differs.
|
|
1837
|
+
const buildSubagentSessionOptions = (sessionManagerForRun: SessionManager): CreateAgentSessionOptions => ({
|
|
1838
|
+
cwd: worktree ?? cwd,
|
|
1839
|
+
authStorage,
|
|
1840
|
+
modelRegistry,
|
|
1841
|
+
settings: subagentSettings,
|
|
1842
|
+
model,
|
|
1843
|
+
thinkingLevel: effectiveThinkingLevel,
|
|
1844
|
+
toolNames,
|
|
1845
|
+
outputSchema,
|
|
1846
|
+
requireYieldTool: true,
|
|
1847
|
+
contextFiles: options.contextFiles,
|
|
1848
|
+
skills: options.skills,
|
|
1849
|
+
promptTemplates: options.promptTemplates,
|
|
1850
|
+
workspaceTree: options.workspaceTree,
|
|
1851
|
+
rules: options.rules,
|
|
1852
|
+
preloadedExtensionPaths: options.preloadedExtensionPaths,
|
|
1853
|
+
preloadedCustomToolPaths: options.preloadedCustomToolPaths,
|
|
1854
|
+
systemPrompt: defaultPrompt => {
|
|
1855
|
+
const subagentPrompt = prompt.render(subagentSystemPromptTemplate, {
|
|
1856
|
+
agent: agent.systemPrompt,
|
|
1857
|
+
context: options.context?.trim() ?? "",
|
|
1858
|
+
planReference: options.planReference?.content ?? "",
|
|
1859
|
+
planReferencePath: options.planReference?.path ?? "",
|
|
1860
|
+
worktree: worktree ?? "",
|
|
1861
|
+
outputSchema: normalizedOutputSchema,
|
|
1862
|
+
ircPeers: ircEnabled ? renderIrcPeerRoster(id) : "",
|
|
1863
|
+
ircSelfId: ircEnabled ? id : "",
|
|
1864
|
+
});
|
|
1865
|
+
return defaultPrompt.length === 0
|
|
1866
|
+
? [subagentPrompt]
|
|
1867
|
+
: [...defaultPrompt.slice(0, -1), subagentPrompt, defaultPrompt[defaultPrompt.length - 1]];
|
|
1868
|
+
},
|
|
1869
|
+
sessionManager: sessionManagerForRun,
|
|
1870
|
+
hasUI: false,
|
|
1871
|
+
spawns: spawnsEnv,
|
|
1872
|
+
taskDepth: childDepth,
|
|
1873
|
+
parentHindsightSessionState: options.parentHindsightSessionState,
|
|
1874
|
+
parentMnemopiSessionState: options.parentMnemopiSessionState,
|
|
1875
|
+
parentTaskPrefix: id,
|
|
1876
|
+
agentId: id,
|
|
1877
|
+
agentDisplayName: agent.name,
|
|
1878
|
+
enableLsp: lspEnabled,
|
|
1879
|
+
skipPythonPreflight,
|
|
1880
|
+
enableMCP,
|
|
1881
|
+
mcpManager: options.mcpManager,
|
|
1882
|
+
customTools: mcpProxyTools.length > 0 ? mcpProxyTools : undefined,
|
|
1883
|
+
localProtocolOptions: options.localProtocolOptions,
|
|
1884
|
+
telemetry: subagentTelemetry,
|
|
1885
|
+
parentEvalSessionId: options.parentEvalSessionId,
|
|
1886
|
+
});
|
|
1887
|
+
|
|
1888
|
+
const sessionPromise = createAgentSession(buildSubagentSessionOptions(sessionManager));
|
|
1889
|
+
let session: AgentSession;
|
|
1890
|
+
try {
|
|
1891
|
+
({ session } = await awaitAbortable(sessionPromise));
|
|
1892
|
+
} catch (err) {
|
|
1893
|
+
// Abort raced session startup. The session may still resolve later
|
|
1894
|
+
// holding live LSP/MCP child processes — dispose it when it does so
|
|
1895
|
+
// a cancelled subagent cannot leak them.
|
|
1896
|
+
void sessionPromise.then(created => created.session.dispose()).catch(() => {});
|
|
1897
|
+
throw err;
|
|
1898
|
+
}
|
|
1899
|
+
|
|
1900
|
+
monitor.setActiveSession(session);
|
|
1901
|
+
installRegistryStatusSync(session);
|
|
1902
|
+
if (sessionFile !== null && worktree === undefined) {
|
|
1903
|
+
// Lifecycle reviver: park closed the JSONL writer, so reopening takes
|
|
1904
|
+
// the single-writer lock cleanly and restores the full message history
|
|
1905
|
+
// (createAgentSession → agent.replaceMessages). Isolated runs are not
|
|
1906
|
+
// resumable (worktree is merged + cleaned) and never get a reviver.
|
|
1907
|
+
reviveSession = async () => {
|
|
1908
|
+
const reopened = await SessionManager.open(sessionFile);
|
|
1909
|
+
if (options.parentArtifactManager) {
|
|
1910
|
+
reopened.adoptArtifactManager(options.parentArtifactManager);
|
|
1911
|
+
}
|
|
1912
|
+
const { session: revived } = await createAgentSession(buildSubagentSessionOptions(reopened));
|
|
1913
|
+
installRegistryStatusSync(revived);
|
|
1914
|
+
return revived;
|
|
1915
|
+
};
|
|
1916
|
+
}
|
|
1917
|
+
|
|
1918
|
+
// Emit lifecycle start event
|
|
1919
|
+
if (options.eventBus) {
|
|
1920
|
+
options.eventBus.emit(TASK_SUBAGENT_LIFECYCLE_CHANNEL, {
|
|
1921
|
+
id,
|
|
1922
|
+
agent: agent.name,
|
|
1923
|
+
parentToolCallId: options.parentToolCallId,
|
|
1924
|
+
agentSource: agent.source,
|
|
1925
|
+
description: options.description,
|
|
1926
|
+
status: "started",
|
|
1927
|
+
sessionFile: subtaskSessionFile,
|
|
1928
|
+
index,
|
|
1929
|
+
});
|
|
1930
|
+
}
|
|
1931
|
+
|
|
1932
|
+
const subagentToolNames = session.getActiveToolNames();
|
|
1933
|
+
const parentOwnedToolNames = new Set(["todo"]);
|
|
1934
|
+
const filteredSubagentTools = subagentToolNames.filter(name => !parentOwnedToolNames.has(name));
|
|
1935
|
+
if (filteredSubagentTools.length !== subagentToolNames.length) {
|
|
1936
|
+
await awaitAbortable(session.setActiveToolsByName(filteredSubagentTools));
|
|
1937
|
+
}
|
|
1938
|
+
|
|
1939
|
+
session.sessionManager.appendSessionInit({
|
|
1940
|
+
systemPrompt: session.agent.state.systemPrompt.join("\n\n"),
|
|
1941
|
+
task,
|
|
1942
|
+
tools: session.getActiveToolNames(),
|
|
1943
|
+
outputSchema,
|
|
1944
|
+
});
|
|
1945
|
+
|
|
1946
|
+
abortSignal.addEventListener(
|
|
1947
|
+
"abort",
|
|
1948
|
+
() => {
|
|
1949
|
+
void session.abort();
|
|
1950
|
+
},
|
|
1951
|
+
{ once: true, signal: sessionAbortController.signal },
|
|
1952
|
+
);
|
|
1953
|
+
// Defensive: if the wall-clock timer (or external signal) fired during
|
|
1954
|
+
// the awaited setup above, the listener registration races the dispatch
|
|
1955
|
+
// and may not observe the already-fired abort event. Mirror it manually.
|
|
1956
|
+
if (abortSignal.aborted) {
|
|
1957
|
+
void session.abort();
|
|
1958
|
+
}
|
|
1959
|
+
|
|
1960
|
+
const extensionRunner = session.extensionRunner;
|
|
1961
|
+
const pendingExtensionMessages: Promise<void>[] = [];
|
|
1962
|
+
if (extensionRunner) {
|
|
1963
|
+
extensionRunner.initialize(
|
|
1964
|
+
{
|
|
1965
|
+
sendMessage: (message, options) => {
|
|
1966
|
+
const sendPromise = session.sendCustomMessage(message, options).catch(e => {
|
|
1967
|
+
logger.error("Extension sendMessage failed", {
|
|
1968
|
+
error: e instanceof Error ? e.message : String(e),
|
|
1969
|
+
});
|
|
1970
|
+
});
|
|
1971
|
+
pendingExtensionMessages.push(sendPromise);
|
|
1972
|
+
},
|
|
1973
|
+
sendUserMessage: (content, options) => {
|
|
1974
|
+
const sendPromise = session.sendUserMessage(content, options).catch(e => {
|
|
1975
|
+
logger.error("Extension sendUserMessage failed", {
|
|
1976
|
+
error: e instanceof Error ? e.message : String(e),
|
|
1977
|
+
});
|
|
1978
|
+
});
|
|
1979
|
+
pendingExtensionMessages.push(sendPromise);
|
|
1980
|
+
},
|
|
1981
|
+
appendEntry: (customType, data) => {
|
|
1982
|
+
session.sessionManager.appendCustomEntry(customType, data);
|
|
1983
|
+
},
|
|
1984
|
+
setLabel: (targetId, label) => {
|
|
1985
|
+
session.sessionManager.appendLabelChange(targetId, label);
|
|
1986
|
+
},
|
|
1987
|
+
getActiveTools: () => session.getActiveToolNames(),
|
|
1988
|
+
getAllTools: () => session.getAllToolNames(),
|
|
1989
|
+
setActiveTools: (toolNames: string[]) =>
|
|
1990
|
+
session.setActiveToolsByName(toolNames.filter(name => !parentOwnedToolNames.has(name))),
|
|
1991
|
+
getCommands: () => getSessionSlashCommands(session),
|
|
1992
|
+
setModel: model => runExtensionSetModel(session, model),
|
|
1993
|
+
getThinkingLevel: () => session.thinkingLevel,
|
|
1994
|
+
setThinkingLevel: level => session.setThinkingLevel(level),
|
|
1995
|
+
getSessionName: () => session.sessionManager.getSessionName(),
|
|
1996
|
+
setSessionName: async name => {
|
|
1997
|
+
await session.sessionManager.setSessionName(name, "user");
|
|
1998
|
+
},
|
|
1999
|
+
},
|
|
2000
|
+
{
|
|
2001
|
+
getModel: () => session.model,
|
|
2002
|
+
isIdle: () => !session.isStreaming,
|
|
2003
|
+
abort: () => session.abort(),
|
|
2004
|
+
hasPendingMessages: () => session.queuedMessageCount > 0,
|
|
2005
|
+
shutdown: () => {},
|
|
2006
|
+
getContextUsage: () => session.getContextUsage(),
|
|
2007
|
+
getSystemPrompt: () => session.systemPrompt,
|
|
2008
|
+
compact: instructionsOrOptions => runExtensionCompact(session, instructionsOrOptions),
|
|
2009
|
+
},
|
|
2010
|
+
);
|
|
2011
|
+
extensionRunner.onError(err => {
|
|
2012
|
+
logger.error("Extension error", { path: err.extensionPath, error: err.error });
|
|
2013
|
+
});
|
|
2014
|
+
await awaitAbortable(extensionRunner.emit({ type: "session_start" }));
|
|
2015
|
+
while (pendingExtensionMessages.length > 0) {
|
|
2016
|
+
await awaitAbortable(Promise.all(pendingExtensionMessages.splice(0)));
|
|
2017
|
+
}
|
|
2018
|
+
}
|
|
2019
|
+
|
|
2020
|
+
unsubscribe = monitor.attach(session);
|
|
2021
|
+
|
|
2022
|
+
checkAbort();
|
|
2023
|
+
// Autoload skills via sendCustomMessage (same mechanic as /skill:<name>)
|
|
2024
|
+
if (options.autoloadSkills?.length) {
|
|
2025
|
+
for (const skill of options.autoloadSkills) {
|
|
2026
|
+
const { message } = await buildSkillPromptMessage(skill, "");
|
|
2027
|
+
await session.sendCustomMessage(
|
|
2028
|
+
{
|
|
2029
|
+
customType: SKILL_PROMPT_MESSAGE_TYPE,
|
|
2030
|
+
content: message,
|
|
2031
|
+
display: false,
|
|
2032
|
+
details: { name: skill.name, path: skill.filePath },
|
|
2033
|
+
},
|
|
2034
|
+
{ triggerTurn: false },
|
|
2035
|
+
);
|
|
2036
|
+
}
|
|
2037
|
+
}
|
|
2038
|
+
|
|
2039
|
+
const outcome = await driveSessionToYield(session, monitor, task);
|
|
2040
|
+
exitCode = outcome.exitCode;
|
|
2041
|
+
error = outcome.error;
|
|
2042
|
+
aborted = outcome.aborted;
|
|
2043
|
+
abortReasonText = outcome.abortReasonText;
|
|
2044
|
+
} catch (err) {
|
|
2045
|
+
exitCode = 1;
|
|
2046
|
+
if (!abortSignal.aborted) {
|
|
2047
|
+
error = err instanceof Error ? err.stack || err.message : String(err);
|
|
2048
|
+
}
|
|
2049
|
+
} finally {
|
|
2050
|
+
if (abortSignal.aborted) {
|
|
2051
|
+
aborted = monitor.isAbortedRun();
|
|
2052
|
+
if (aborted) {
|
|
2053
|
+
abortReasonText ??= monitor.resolveAbortReasonText();
|
|
2054
|
+
}
|
|
2055
|
+
if (exitCode === 0) exitCode = 1;
|
|
2056
|
+
}
|
|
2057
|
+
sessionAbortController.abort();
|
|
2058
|
+
if (unsubscribe) {
|
|
2059
|
+
try {
|
|
2060
|
+
unsubscribe();
|
|
2061
|
+
} catch {
|
|
2062
|
+
// Ignore unsubscribe errors
|
|
2063
|
+
}
|
|
2064
|
+
unsubscribe = null;
|
|
2065
|
+
}
|
|
2066
|
+
const session = monitor.takeActiveSession();
|
|
2067
|
+
if (session) {
|
|
2068
|
+
monitor.captureSalvage(session);
|
|
2069
|
+
const registry = AgentRegistry.global();
|
|
2070
|
+
if (aborted) {
|
|
2071
|
+
// Hard abort (caller signal / wall-clock / budget): terminal teardown.
|
|
2072
|
+
registry.setStatus(id, "aborted");
|
|
2073
|
+
try {
|
|
2074
|
+
await untilAborted(AbortSignal.timeout(5000), () => session.dispose());
|
|
2075
|
+
} catch {
|
|
2076
|
+
// Ignore cleanup errors
|
|
2077
|
+
}
|
|
2078
|
+
} else if (worktree !== undefined) {
|
|
2079
|
+
// Isolated run: the worktree is merged + cleaned after the run, so
|
|
2080
|
+
// the session is not resumable. Park the ref WITHOUT adopting — the
|
|
2081
|
+
// transcript stays reachable (history://), but ensureLive will throw.
|
|
2082
|
+
// Status must flip to "parked" before dispose so the sdk dispose
|
|
2083
|
+
// wrapper skips unregister.
|
|
2084
|
+
registry.setStatus(id, "parked");
|
|
2085
|
+
try {
|
|
2086
|
+
await untilAborted(AbortSignal.timeout(5000), () => session.dispose());
|
|
2087
|
+
} catch {
|
|
2088
|
+
// Ignore cleanup errors
|
|
2089
|
+
}
|
|
2090
|
+
registry.detachSession(id);
|
|
2091
|
+
} else {
|
|
2092
|
+
// Keep-alive: finished and failed subagents both stay interrogable.
|
|
2093
|
+
// The lifecycle manager owns idle-TTL parking + revival from here on.
|
|
2094
|
+
registry.setStatus(id, "idle");
|
|
2095
|
+
AgentLifecycleManager.global().adopt(id, {
|
|
2096
|
+
idleTtlMs: agentIdleTtlMs,
|
|
2097
|
+
revive: reviveSession ?? undefined,
|
|
2098
|
+
});
|
|
2099
|
+
}
|
|
2100
|
+
}
|
|
2101
|
+
}
|
|
2102
|
+
|
|
2103
|
+
return {
|
|
2104
|
+
exitCode,
|
|
2105
|
+
error,
|
|
2106
|
+
aborted,
|
|
2107
|
+
abortReason: aborted ? abortReasonText : undefined,
|
|
2108
|
+
durationMs: Date.now() - startTime,
|
|
2109
|
+
};
|
|
2110
|
+
};
|
|
2111
|
+
|
|
2112
|
+
const done = await runSubagent();
|
|
2113
|
+
monitor.finish();
|
|
2114
|
+
|
|
2115
|
+
return finalizeRunResult({
|
|
2116
|
+
monitor,
|
|
2117
|
+
done,
|
|
2118
|
+
index,
|
|
2119
|
+
id,
|
|
2120
|
+
agent,
|
|
2121
|
+
task,
|
|
2122
|
+
assignment,
|
|
2123
|
+
description: options.description,
|
|
2124
|
+
modelOverride,
|
|
2125
|
+
outputSchema,
|
|
2126
|
+
signal,
|
|
2127
|
+
artifactsDir: options.artifactsDir,
|
|
2128
|
+
eventBus: options.eventBus,
|
|
2129
|
+
parentToolCallId: options.parentToolCallId,
|
|
2130
|
+
sessionFile: subtaskSessionFile,
|
|
2131
|
+
startTime,
|
|
2132
|
+
});
|
|
2133
|
+
}
|