@selesai/code 0.13.15 → 0.13.18
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 +37 -0
- package/README.md +2 -14
- package/dist/bun/cli.d.ts +3 -1
- package/dist/bun/cli.js +3 -9
- package/dist/bun/{register-bedrock.js → runtime-setup.js} +5 -0
- package/dist/bun/sandbox-env-setup.d.ts +1 -0
- package/dist/bun/sandbox-env-setup.js +3 -0
- package/dist/cli/auth-command.d.ts +23 -0
- package/dist/cli/auth-command.js +102 -0
- package/dist/cli/config-selector.js +2 -1
- package/dist/cli/setup.d.ts +1 -0
- package/dist/cli/setup.js +11 -0
- package/dist/cli.js +2 -14
- package/dist/config.d.ts +3 -2
- package/dist/config.js +9 -4
- package/dist/core/agent-session-runtime.js +15 -7
- package/dist/core/agent-session.d.ts +2 -6
- package/dist/core/agent-session.js +15 -4
- package/dist/core/compaction/branch-summarization.d.ts +1 -1
- package/dist/core/compaction/branch-summarization.js +2 -1
- package/dist/core/extensions/loader.js +3 -0
- package/dist/core/extensions/types.d.ts +16 -2
- package/dist/core/http-dispatcher.js +2 -0
- package/dist/core/keybindings.d.ts +5 -0
- package/dist/core/keybindings.js +4 -0
- package/dist/core/messages.d.ts +1 -1
- package/dist/core/model-config.d.ts +15 -0
- package/dist/core/model-config.js +3 -0
- package/dist/core/model-runtime.d.ts +1 -0
- package/dist/core/model-runtime.js +5 -2
- package/dist/core/provider-composer.js +8 -2
- package/dist/core/session-export.d.ts +3 -0
- package/dist/core/session-export.js +31 -0
- package/dist/core/session-manager.d.ts +3 -2
- package/dist/core/session-manager.js +40 -13
- package/dist/core/skills.d.ts +1 -1
- package/dist/core/skills.js +4 -2
- package/dist/core/system-prompt.js +10 -10
- package/dist/core/tools/bash.js +3 -117
- package/dist/core/tools/edit.d.ts +1 -12
- package/dist/core/tools/edit.js +5 -163
- package/dist/core/tools/find.js +4 -55
- package/dist/core/tools/grep.js +4 -60
- package/dist/core/tools/ls.js +4 -49
- package/dist/core/tools/read.d.ts +4 -0
- package/dist/core/tools/read.js +10 -122
- package/dist/core/tools/renderers/bash.d.ts +11 -0
- package/dist/core/tools/renderers/bash.js +129 -0
- package/dist/core/tools/renderers/edit.d.ts +22 -0
- package/dist/core/tools/renderers/edit.js +166 -0
- package/dist/core/tools/renderers/find.d.ts +9 -0
- package/dist/core/tools/renderers/find.js +63 -0
- package/dist/core/tools/renderers/grep.d.ts +9 -0
- package/dist/core/tools/renderers/grep.js +68 -0
- package/dist/core/tools/renderers/index.d.ts +27 -0
- package/dist/core/tools/renderers/index.js +46 -0
- package/dist/core/tools/renderers/ls.d.ts +9 -0
- package/dist/core/tools/renderers/ls.js +57 -0
- package/dist/core/tools/renderers/read.d.ts +10 -0
- package/dist/core/tools/renderers/read.js +128 -0
- package/dist/core/tools/renderers/write.d.ts +9 -0
- package/dist/core/tools/renderers/write.js +152 -0
- package/dist/core/tools/write.js +5 -146
- package/dist/extensions/capability-gateway/index.ts +12 -1
- package/dist/extensions/capability-gateway/integration.test.ts +23 -0
- package/dist/extensions/pi-hermes-memory/CHANGELOG.md +18 -0
- package/dist/extensions/pi-hermes-memory/README.md +16 -12
- package/dist/extensions/pi-hermes-memory/docs/ROADMAP.md +1 -1
- package/dist/extensions/pi-hermes-memory/package-lock.json +587 -62
- package/dist/extensions/pi-hermes-memory/package.json +4 -4
- package/dist/extensions/pi-hermes-memory/src/config.ts +32 -0
- package/dist/extensions/pi-hermes-memory/src/constants.ts +12 -2
- package/dist/extensions/pi-hermes-memory/src/handlers/background-review.ts +109 -51
- package/dist/extensions/pi-hermes-memory/src/handlers/index-sessions.ts +9 -3
- package/dist/extensions/pi-hermes-memory/src/handlers/review-memory-ops.ts +257 -98
- package/dist/extensions/pi-hermes-memory/src/handlers/session-backfill.ts +9 -2
- package/dist/extensions/pi-hermes-memory/src/handlers/sync-markdown-memories.ts +5 -0
- package/dist/extensions/pi-hermes-memory/src/index.ts +22 -2
- package/dist/extensions/pi-hermes-memory/src/paths.ts +1 -1
- package/dist/extensions/pi-hermes-memory/src/store/db.ts +6 -0
- package/dist/extensions/pi-hermes-memory/src/store/fts-query.ts +58 -1
- package/dist/extensions/pi-hermes-memory/src/store/memory-store.ts +60 -8
- package/dist/extensions/pi-hermes-memory/src/store/session-indexer.ts +169 -7
- package/dist/extensions/pi-hermes-memory/src/store/session-search.ts +7 -21
- package/dist/extensions/pi-hermes-memory/src/store/sqlite-memory-store.ts +134 -15
- package/dist/extensions/pi-hermes-memory/src/tools/memory-search-tool.ts +4 -2
- package/dist/extensions/pi-hermes-memory/src/tools/memory-tool.ts +24 -9
- package/dist/extensions/pi-hermes-memory/src/types.ts +11 -0
- package/dist/extensions/pi-hermes-memory/tests/config.test.ts +42 -0
- package/dist/extensions/pi-hermes-memory/tests/handlers/background-review.test.ts +159 -3
- package/dist/extensions/pi-hermes-memory/tests/handlers/review-memory-ops.test.ts +452 -0
- package/dist/extensions/pi-hermes-memory/tests/handlers/session-backfill.test.ts +39 -0
- package/dist/extensions/pi-hermes-memory/tests/integration/corruption-recovery-integration.test.ts +91 -0
- package/dist/extensions/pi-hermes-memory/tests/store/db.test.ts +29 -0
- package/dist/extensions/pi-hermes-memory/tests/store/fts-query.test.ts +116 -0
- package/dist/extensions/pi-hermes-memory/tests/store/memory-store.test.ts +163 -0
- package/dist/extensions/pi-hermes-memory/tests/store/session-indexer.test.ts +233 -0
- package/dist/extensions/pi-hermes-memory/tests/store/session-search.test.ts +12 -0
- package/dist/extensions/pi-hermes-memory/tests/store/sqlite-memory-store.test.ts +175 -0
- package/dist/extensions/pi-hermes-memory/tests/store/sqlite-native.test.ts +2 -2
- package/dist/extensions/pi-hermes-memory/tests/tools/memory-search-tool.test.ts +22 -0
- package/dist/extensions/pi-hermes-memory/tests/tools/memory-tool.test.ts +37 -1
- package/dist/extensions/pi-intercom/CHANGELOG.md +13 -0
- package/dist/extensions/pi-intercom/README.md +16 -2
- package/dist/extensions/pi-intercom/broker/spawn.test.ts +19 -2
- package/dist/extensions/pi-intercom/broker/spawn.ts +4 -4
- package/dist/extensions/pi-intercom/index.ts +1 -1
- package/dist/extensions/pi-intercom/intercom.integration.test.ts +121 -2
- package/dist/extensions/pi-intercom/package.json +1 -1
- package/dist/extensions/pi-intercom/project-agent.test.ts +1 -1
- package/dist/extensions/pi-intercom/project-agent.ts +1 -6
- package/dist/extensions/pi-intercom/skills/pi-intercom/SKILL.md +7 -6
- package/dist/extensions/pi-rewind-hook/index.test.ts +7 -0
- package/dist/extensions/pi-subagents/.github/workflows/test.yml +36 -4
- package/dist/extensions/pi-subagents/CHANGELOG.md +120 -0
- package/dist/extensions/pi-subagents/README.md +3 -3
- package/dist/extensions/pi-subagents/agents/researcher.md +23 -13
- package/dist/extensions/pi-subagents/agents/reviewer.md +1 -1
- package/dist/extensions/pi-subagents/agents/scout.md +1 -1
- package/dist/extensions/pi-subagents/docs/agents.md +32 -16
- package/dist/extensions/pi-subagents/docs/configuration.md +41 -15
- package/dist/extensions/pi-subagents/docs/extension-api.md +109 -6
- package/dist/extensions/pi-subagents/docs/missions.md +2 -0
- package/dist/extensions/pi-subagents/docs/models.md +58 -1
- package/dist/extensions/pi-subagents/docs/observability.md +50 -12
- package/dist/extensions/pi-subagents/docs/tool-reference.md +17 -10
- package/dist/extensions/pi-subagents/docs/watchdog.md +1 -1
- package/dist/extensions/pi-subagents/docs/workflows.md +26 -9
- package/dist/extensions/pi-subagents/package-lock.json +747 -177
- package/dist/extensions/pi-subagents/package.json +6 -4
- package/dist/extensions/pi-subagents/runner-server-preload.mjs +13 -0
- package/dist/extensions/pi-subagents/skills/pi-subagents/SKILL.md +2 -1
- package/dist/extensions/pi-subagents/skills/pi-subagents/references/constraints-and-recipes.md +1 -1
- package/dist/extensions/pi-subagents/skills/pi-subagents/references/execution-controls.md +21 -4
- package/dist/extensions/pi-subagents/skills/pi-subagents/references/management-authoring-rpc.md +2 -1
- package/dist/extensions/pi-subagents/skills/pi-subagents/references/multi-lane-orchestration.md +2 -0
- package/dist/extensions/pi-subagents/src/agents/advertised-agent-prompt.ts +63 -0
- package/dist/extensions/pi-subagents/src/agents/agent-management.ts +61 -16
- package/dist/extensions/pi-subagents/src/agents/agent-serializer.ts +2 -0
- package/dist/extensions/pi-subagents/src/agents/agents.ts +8 -0
- package/dist/extensions/pi-subagents/src/api/capability-ceiling.ts +0 -1
- package/dist/extensions/pi-subagents/src/api/{pi-args.ts → child-tool-plan.ts} +1 -1
- package/dist/extensions/pi-subagents/src/api/preflight.ts +7 -4
- package/dist/extensions/pi-subagents/src/api/shared-types.ts +2 -2
- package/dist/extensions/pi-subagents/src/api/workflow-resources.ts +6 -0
- package/dist/extensions/pi-subagents/src/extension/config.ts +4 -2
- package/dist/extensions/pi-subagents/src/extension/doctor.ts +2 -10
- package/dist/extensions/pi-subagents/src/extension/fanout-child.ts +9 -11
- package/dist/extensions/pi-subagents/src/extension/index.ts +99 -10
- package/dist/extensions/pi-subagents/src/extension/public-execution.ts +13 -0
- package/dist/extensions/pi-subagents/src/extension/rpc.ts +7 -23
- package/dist/extensions/pi-subagents/src/extension/schemas.ts +11 -8
- package/dist/extensions/pi-subagents/src/extension/tool-description.ts +25 -7
- package/dist/extensions/pi-subagents/src/integrations/pi-web-session-liveness.ts +73 -0
- package/dist/extensions/pi-subagents/src/intercom/native-supervisor-channel.ts +228 -136
- package/dist/extensions/pi-subagents/src/intercom/supervisor-ui.ts +244 -0
- package/dist/extensions/pi-subagents/src/missions/workflow-state.ts +37 -16
- package/dist/extensions/pi-subagents/src/runs/background/active-async-capacity.ts +18 -18
- package/dist/extensions/pi-subagents/src/runs/background/async-execution.ts +51 -25
- package/dist/extensions/pi-subagents/src/runs/background/async-job-tracker.ts +46 -3
- package/dist/extensions/pi-subagents/src/runs/background/async-resume.ts +14 -3
- package/dist/extensions/pi-subagents/src/runs/background/async-retention.ts +9 -0
- package/dist/extensions/pi-subagents/src/runs/background/async-status-snapshot.ts +10 -12
- package/dist/extensions/pi-subagents/src/runs/background/async-status.ts +18 -15
- package/dist/extensions/pi-subagents/src/runs/background/auto-drain.ts +40 -29
- package/dist/extensions/pi-subagents/src/runs/background/chain-root-attachment.ts +8 -0
- package/dist/extensions/pi-subagents/src/runs/background/control-channel.ts +79 -247
- package/dist/extensions/pi-subagents/src/runs/background/notify.ts +88 -12
- package/dist/extensions/pi-subagents/src/runs/background/owned-process-tree.ts +6 -6
- package/dist/extensions/pi-subagents/src/runs/background/process-terminal.ts +24 -24
- package/dist/extensions/pi-subagents/src/runs/background/retained-nested-route-tracker.ts +96 -0
- package/dist/extensions/pi-subagents/src/runs/background/run-child-session.ts +642 -0
- package/dist/extensions/pi-subagents/src/runs/background/run-status.ts +75 -6
- package/dist/extensions/pi-subagents/src/runs/background/runner-aliases.ts +163 -0
- package/dist/extensions/pi-subagents/src/runs/background/runner-child-launch.ts +86 -0
- package/dist/extensions/pi-subagents/src/runs/background/runner-child-sessions.ts +31 -0
- package/dist/extensions/pi-subagents/src/runs/background/scheduled-runs.ts +18 -4
- package/dist/extensions/pi-subagents/src/runs/background/stale-run-reconciler.ts +3 -1
- package/dist/extensions/pi-subagents/src/runs/background/subagent-runner.ts +597 -1092
- package/dist/extensions/pi-subagents/src/runs/background/subagent-wait.ts +3 -0
- package/dist/extensions/pi-subagents/src/runs/background/wait-completions.ts +4 -0
- package/dist/extensions/pi-subagents/src/runs/foreground/async-steering-action.ts +20 -17
- package/dist/extensions/pi-subagents/src/runs/foreground/execution.ts +296 -394
- package/dist/extensions/pi-subagents/src/runs/foreground/foreground-control.ts +4 -0
- package/dist/extensions/pi-subagents/src/runs/foreground/foreground-history.ts +3 -1
- package/dist/extensions/pi-subagents/src/runs/foreground/prompt-audit.ts +9 -5
- package/dist/extensions/pi-subagents/src/runs/foreground/subagent-executor.ts +630 -283
- package/dist/extensions/pi-subagents/src/runs/foreground/workflow-detach-reconcile.ts +8 -5
- package/dist/extensions/pi-subagents/src/runs/foreground/workflow-foreground-steering.ts +78 -98
- package/dist/extensions/pi-subagents/src/runs/shared/abort-recovery.ts +3 -3
- package/dist/extensions/pi-subagents/src/runs/shared/acceptance.ts +16 -3
- package/dist/extensions/pi-subagents/src/runs/shared/agent-contract.ts +1 -1
- package/dist/extensions/pi-subagents/src/runs/shared/async-status-projection.ts +47 -47
- package/dist/extensions/pi-subagents/src/runs/shared/capability-ceiling.ts +1 -2
- package/dist/extensions/pi-subagents/src/runs/shared/child-hooks.ts +174 -0
- package/dist/extensions/pi-subagents/src/runs/shared/child-identity.ts +14 -3
- package/dist/extensions/pi-subagents/src/runs/shared/child-launch.ts +319 -0
- package/dist/extensions/pi-subagents/src/runs/shared/child-lifecycle.ts +25 -0
- package/dist/extensions/pi-subagents/src/runs/shared/child-runtime-config.ts +126 -0
- package/dist/extensions/pi-subagents/src/runs/shared/child-session.ts +373 -0
- package/dist/extensions/pi-subagents/src/runs/shared/child-tool-plan.ts +530 -0
- package/dist/extensions/pi-subagents/src/runs/shared/completion-evidence.ts +2 -2
- package/dist/extensions/pi-subagents/src/runs/shared/completion-guard.ts +2 -1
- package/dist/extensions/pi-subagents/src/runs/shared/external-cli-preflight.ts +16 -0
- package/dist/extensions/pi-subagents/src/runs/shared/host-step-status.ts +11 -11
- package/dist/extensions/pi-subagents/src/runs/shared/llm-intent-arbiter.ts +30 -20
- package/dist/extensions/pi-subagents/src/runs/shared/mcp-direct-tool-allowlist.ts +5 -4
- package/dist/extensions/pi-subagents/src/runs/shared/model-exclusions.ts +84 -15
- package/dist/extensions/pi-subagents/src/runs/shared/model-fallback.ts +88 -12
- package/dist/extensions/pi-subagents/src/runs/shared/nested-events.ts +37 -53
- package/dist/extensions/pi-subagents/src/runs/shared/nested-path.ts +1 -1
- package/dist/extensions/pi-subagents/src/runs/shared/orca-progress-tabs.ts +18 -7
- package/dist/extensions/pi-subagents/src/runs/shared/parallel-handoff.ts +57 -12
- package/dist/extensions/pi-subagents/src/runs/shared/parallel-utils.ts +3 -4
- package/dist/extensions/pi-subagents/src/runs/shared/permissions.ts +0 -11
- package/dist/extensions/pi-subagents/src/runs/shared/process-signal.ts +4 -1
- package/dist/extensions/pi-subagents/src/runs/shared/readonly-drain-observation.ts +42 -0
- package/dist/extensions/pi-subagents/src/runs/shared/readonly-model-continuation.ts +69 -0
- package/dist/extensions/pi-subagents/src/runs/shared/readonly-session-evidence.ts +307 -0
- package/dist/extensions/pi-subagents/src/runs/shared/run-fanout-budget.ts +8 -21
- package/dist/extensions/pi-subagents/src/runs/shared/runtime-acknowledged-extensions.ts +3 -30
- package/dist/extensions/pi-subagents/src/runs/shared/structured-output.ts +17 -4
- package/dist/extensions/pi-subagents/src/runs/shared/subagent-control.ts +2 -0
- package/dist/extensions/pi-subagents/src/runs/shared/subagent-prompt-runtime.ts +99 -386
- package/dist/extensions/pi-subagents/src/runs/shared/tool-availability.ts +18 -62
- package/dist/extensions/pi-subagents/src/runs/shared/tool-budget.ts +0 -14
- package/dist/extensions/pi-subagents/src/runs/shared/worktree-cleanup-plan.ts +31 -9
- package/dist/extensions/pi-subagents/src/runs/shared/worktree-setup-command.ts +190 -0
- package/dist/extensions/pi-subagents/src/runs/shared/worktree.ts +506 -226
- package/dist/extensions/pi-subagents/src/shared/child-session-name.ts +1 -1
- package/dist/extensions/pi-subagents/src/shared/jsonl-writer.ts +11 -0
- package/dist/extensions/pi-subagents/src/shared/model-response-aliases.ts +13 -0
- package/dist/extensions/pi-subagents/src/shared/thinking-ceiling.ts +0 -6
- package/dist/extensions/pi-subagents/src/shared/types.ts +145 -89
- package/dist/extensions/pi-subagents/src/shared/utils.ts +13 -6
- package/dist/extensions/pi-subagents/src/shared/watch-strategy.ts +2 -0
- package/dist/extensions/pi-subagents/src/shared/workflow-child-permit.ts +18 -13
- package/dist/extensions/pi-subagents/src/slash/slash-commands.ts +0 -6
- package/dist/extensions/pi-subagents/src/tui/fleet-status.ts +1 -1
- package/dist/extensions/pi-subagents/src/tui/fleet.ts +11 -6
- package/dist/extensions/pi-subagents/src/tui/render.ts +277 -46
- package/dist/extensions/pi-subagents/src/watchdog/child-status.ts +0 -1
- package/dist/extensions/pi-subagents/src/watchdog/register-child.ts +12 -12
- package/dist/extensions/pi-subagents/src/workflows/chat-progress.ts +3 -3
- package/dist/extensions/pi-subagents/src/workflows/scripted-workflow.ts +116 -15
- package/dist/extensions/pi-subagents/src/workflows/workflow-checklist.ts +15 -18
- package/dist/extensions/pi-subagents/src/workflows/workflow-child-summary.ts +57 -8
- package/dist/extensions/pi-subagents/src/workflows/workflow-preflight.ts +19 -19
- package/dist/extensions/pi-subagents/src/workflows/workflow-receipt.ts +3 -3
- package/dist/extensions/pi-subagents/src/workflows/workflow-resources.ts +97 -22
- package/dist/extensions/pi-subagents/src/workflows/workflow-settlement.ts +3 -0
- package/dist/extensions/pi-subagents/test/integration/acceptance-file-report.test.ts +336 -21
- package/dist/extensions/pi-subagents/test/integration/async-execution.part-1.test.ts +1181 -0
- package/dist/extensions/pi-subagents/test/integration/async-execution.part-2.test.ts +2240 -0
- package/dist/extensions/pi-subagents/test/integration/async-execution.part-3.test.ts +2268 -0
- package/dist/extensions/pi-subagents/test/integration/async-execution.part-4.test.ts +1512 -0
- package/dist/extensions/pi-subagents/test/integration/async-job-tracker.test.ts +231 -16
- package/dist/extensions/pi-subagents/test/integration/error-handling.test.ts +4 -18
- package/dist/extensions/pi-subagents/test/integration/foreign-workflow-steering.test.ts +95 -0
- package/dist/extensions/pi-subagents/test/integration/fork-context-execution.test.ts +9 -15
- package/dist/extensions/pi-subagents/test/integration/in-process-child.test.ts +374 -0
- package/dist/extensions/pi-subagents/test/integration/intercom-result-delivery.test.ts +20 -10
- package/dist/extensions/pi-subagents/test/integration/orca-progress-tabs.test.ts +18 -6
- package/dist/extensions/pi-subagents/test/integration/recovered-assistant-error.test.ts +151 -0
- package/dist/extensions/pi-subagents/test/integration/render-fork-badge.test.ts +42 -1
- package/dist/extensions/pi-subagents/test/integration/render-widget.test.ts +150 -25
- package/dist/extensions/pi-subagents/test/integration/result-publication.test.ts +354 -0
- package/dist/extensions/pi-subagents/test/integration/shared-cwd-terminal-evidence.test.ts +117 -0
- package/dist/extensions/pi-subagents/test/integration/single-execution.part-1.test.ts +3678 -0
- package/dist/extensions/pi-subagents/test/integration/{single-execution.test.ts → single-execution.part-2.test.ts} +500 -4207
- package/dist/extensions/pi-subagents/test/integration/slash-commands.test.ts +1 -2
- package/dist/extensions/pi-subagents/test/integration/workflow-result-publication.test.ts +127 -0
- package/dist/extensions/pi-subagents/test/integration/workflow-steer-inbox.test.ts +365 -0
- package/dist/extensions/pi-subagents/test/smoke/pi085-child.ts +31 -0
- package/dist/extensions/pi-subagents/test/smoke/pi085-clean-install.mjs +74 -0
- package/dist/extensions/pi-subagents/test/smoke/pi085-extension.ts +16 -0
- package/dist/extensions/pi-subagents/test/support/async-execution-fixture.ts +699 -0
- package/dist/extensions/pi-subagents/test/support/fake-child-session.ts +418 -0
- package/dist/extensions/pi-subagents/test/support/foreign-workflow-steer-host.mjs +13 -0
- package/dist/extensions/pi-subagents/test/support/mock-pi.ts +30 -76
- package/dist/extensions/pi-subagents/test/support/notify-diagnostics-fixture.ts +84 -0
- package/dist/extensions/pi-subagents/test/support/result-publication-capacity-preload.mjs +70 -0
- package/dist/extensions/pi-subagents/test/support/runner-child-session-factory.ts +17 -0
- package/dist/extensions/pi-subagents/test/support/single-execution-fixture.ts +390 -0
- package/dist/extensions/pi-subagents/test/unit/acceptance-compaction.test.ts +120 -0
- package/dist/extensions/pi-subagents/test/unit/acceptance.test.ts +24 -3
- package/dist/extensions/pi-subagents/test/unit/advertised-agent-prompt.test.ts +87 -0
- package/dist/extensions/pi-subagents/test/unit/advertised-agent-refresh.test.ts +163 -0
- package/dist/extensions/pi-subagents/test/unit/agent-frontmatter.test.ts +70 -31
- package/dist/extensions/pi-subagents/test/unit/agent-management.test.ts +78 -3
- package/dist/extensions/pi-subagents/test/unit/agent-overrides.test.ts +2 -8
- package/dist/extensions/pi-subagents/test/unit/async-execution.test.ts +1 -1
- package/dist/extensions/pi-subagents/test/unit/async-interrupt-action.test.ts +73 -66
- package/dist/extensions/pi-subagents/test/unit/async-recovery-descriptor.test.ts +51 -1
- package/dist/extensions/pi-subagents/test/unit/async-retention.test.ts +49 -1
- package/dist/extensions/pi-subagents/test/unit/async-spawn-preload.test.ts +103 -0
- package/dist/extensions/pi-subagents/test/unit/async-status-projection.test.ts +3 -3
- package/dist/extensions/pi-subagents/test/unit/capability-ceiling-agent-allowlist.test.ts +9 -8
- package/dist/extensions/pi-subagents/test/unit/capability-ceiling.test.ts +10 -7
- package/dist/extensions/pi-subagents/test/unit/chain-root-attachment.test.ts +48 -6
- package/dist/extensions/pi-subagents/test/unit/child-runtime-config.test.ts +90 -0
- package/dist/extensions/pi-subagents/test/unit/{pi-args-permission-system.test.ts → child-tool-plan-permission-system.test.ts} +48 -86
- package/dist/extensions/pi-subagents/test/unit/child-tool-plan.test.ts +33 -0
- package/dist/extensions/pi-subagents/test/unit/compaction-resume.test.ts +18 -18
- package/dist/extensions/pi-subagents/test/unit/completion-evidence.test.ts +9 -9
- package/dist/extensions/pi-subagents/test/unit/completion-guard.test.ts +8 -7
- package/dist/extensions/pi-subagents/test/unit/control-channel.test.ts +225 -65
- package/dist/extensions/pi-subagents/test/unit/extension-bindings.test.ts +17 -22
- package/dist/extensions/pi-subagents/test/unit/fleet-status.test.ts +11 -1
- package/dist/extensions/pi-subagents/test/unit/fleet.test.ts +47 -3
- package/dist/extensions/pi-subagents/test/unit/foreign-workflow-steering.test.ts +74 -0
- package/dist/extensions/pi-subagents/test/unit/fork-cache-key.test.ts +23 -29
- package/dist/extensions/pi-subagents/test/unit/host-peer-runtime-imports.test.ts +96 -4
- package/dist/extensions/pi-subagents/test/unit/host-step-status.test.ts +4 -4
- package/dist/extensions/pi-subagents/test/unit/index-child-registration.test.ts +116 -55
- package/dist/extensions/pi-subagents/test/unit/index-segment.test.ts +58 -1
- package/dist/extensions/pi-subagents/test/unit/llm-intent-arbiter.test.ts +104 -3
- package/dist/extensions/pi-subagents/test/unit/mission-lifecycle.test.ts +10 -1
- package/dist/extensions/pi-subagents/test/unit/mission-store.test.ts +31 -1
- package/dist/extensions/pi-subagents/test/unit/model-exclusions.test.ts +102 -51
- package/dist/extensions/pi-subagents/test/unit/model-fallback.test.ts +196 -6
- package/dist/extensions/pi-subagents/test/unit/native-supervisor-channel.test.ts +181 -38
- package/dist/extensions/pi-subagents/test/unit/nested-control.test.ts +62 -70
- package/dist/extensions/pi-subagents/test/unit/nested-events.test.ts +58 -53
- package/dist/extensions/pi-subagents/test/unit/notify-diagnostics.test.ts +42 -0
- package/dist/extensions/pi-subagents/test/unit/notify.test.ts +84 -0
- package/dist/extensions/pi-subagents/test/unit/orca-progress-tabs.test.ts +13 -5
- package/dist/extensions/pi-subagents/test/unit/package-manifest.test.ts +21 -5
- package/dist/extensions/pi-subagents/test/unit/parallel-handoff.test.ts +5 -4
- package/dist/extensions/pi-subagents/test/unit/permissions.test.ts +2 -6
- package/dist/extensions/pi-subagents/test/unit/pi-coding-agent-dir.test.ts +28 -2
- package/dist/extensions/pi-subagents/test/unit/pi-web-session-liveness.test.ts +209 -0
- package/dist/extensions/pi-subagents/test/unit/preflight.test.ts +38 -1
- package/dist/extensions/pi-subagents/test/unit/prompt-audit-optional-peers.test.ts +43 -0
- package/dist/extensions/pi-subagents/test/unit/public-execution.test.ts +24 -2
- package/dist/extensions/pi-subagents/test/unit/readonly-drain-observation.test.ts +174 -0
- package/dist/extensions/pi-subagents/test/unit/readonly-model-continuation.test.ts +94 -0
- package/dist/extensions/pi-subagents/test/unit/readonly-session-evidence.test.ts +1329 -0
- package/dist/extensions/pi-subagents/test/unit/recursion-guard.test.ts +29 -80
- package/dist/extensions/pi-subagents/test/unit/render-helpers.test.ts +11 -8
- package/dist/extensions/pi-subagents/test/unit/result-files.test.ts +2 -2
- package/dist/extensions/pi-subagents/test/unit/retained-nested-route-tracker.test.ts +175 -0
- package/dist/extensions/pi-subagents/test/unit/rpc.test.ts +38 -12
- package/dist/extensions/pi-subagents/test/unit/run-fanout-budget.test.ts +2 -5
- package/dist/extensions/pi-subagents/test/unit/run-status.test.ts +89 -1
- package/dist/extensions/pi-subagents/test/unit/runtime-acknowledged-extensions.test.ts +2 -16
- package/dist/extensions/pi-subagents/test/unit/scheduled-runs.test.ts +27 -3
- package/dist/extensions/pi-subagents/test/unit/scheduled-supervisor-demand.test.ts +215 -0
- package/dist/extensions/pi-subagents/test/unit/schemas.test.ts +15 -11
- package/dist/extensions/pi-subagents/test/unit/scripted-workflow.test.ts +237 -1
- package/dist/extensions/pi-subagents/test/unit/steering-action.test.ts +2 -5
- package/dist/extensions/pi-subagents/test/unit/streamed-progress-bounds.test.ts +2 -2
- package/dist/extensions/pi-subagents/test/unit/subagent-guide.test.ts +14 -1
- package/dist/extensions/pi-subagents/test/unit/subagent-prompt-runtime.test.ts +130 -739
- package/dist/extensions/pi-subagents/test/unit/supervisor-ask-registration.test.ts +682 -0
- package/dist/extensions/pi-subagents/test/unit/supervisor-ui.test.ts +137 -0
- package/dist/extensions/pi-subagents/test/unit/tool-budget.test.ts +1 -11
- package/dist/extensions/pi-subagents/test/unit/tool-description.test.ts +23 -3
- package/dist/extensions/pi-subagents/test/unit/wait-completions.test.ts +8 -1
- package/dist/extensions/pi-subagents/test/unit/watchdog-tool-actions.test.ts +19 -1
- package/dist/extensions/pi-subagents/test/unit/workflow-chat-progress.test.ts +72 -4
- package/dist/extensions/pi-subagents/test/unit/workflow-checklist.test.ts +17 -3
- package/dist/extensions/pi-subagents/test/unit/workflow-child-summary.test.ts +34 -0
- package/dist/extensions/pi-subagents/test/unit/workflow-detach-reconcile.test.ts +12 -2
- package/dist/extensions/pi-subagents/test/unit/workflow-launch-params.test.ts +46 -6
- package/dist/extensions/pi-subagents/test/unit/workflow-receipt.test.ts +3 -3
- package/dist/extensions/pi-subagents/test/unit/workflow-resources.test.ts +133 -1
- package/dist/extensions/pi-subagents/test/unit/workflow-resume-hint.test.ts +53 -2
- package/dist/extensions/pi-subagents/test/unit/worktree-cleanup-plan.test.ts +222 -14
- package/dist/extensions/pi-subagents/test/unit/worktree.test.ts +526 -85
- package/dist/extensions/pi-web-agent/src/backends/config.ts +1 -0
- package/dist/extensions/pi-web-agent/src/backends/settings-reader.ts +5 -3
- package/dist/extensions/pi-web-agent/src/search/tokenin.ts +7 -3
- package/dist/extensions/pi-web-agent/src/types.ts +1 -1
- package/dist/extensions/pi-zentui/README.md +17 -0
- package/dist/extensions/pi-zentui/assets/screenshots/footer-status-hyperlink.png +0 -0
- package/dist/extensions/pi-zentui/docs/configuration.md +71 -2
- package/dist/extensions/pi-zentui/extensions/zentui/config.ts +11 -0
- package/dist/extensions/pi-zentui/extensions/zentui/editor-transfer.ts +14 -0
- package/dist/extensions/pi-zentui/extensions/zentui/extension-status.ts +29 -13
- package/dist/extensions/pi-zentui/extensions/zentui/footer-layout.ts +5 -4
- package/dist/extensions/pi-zentui/extensions/zentui/footer-text.ts +11 -0
- package/dist/extensions/pi-zentui/extensions/zentui/footer.ts +11 -10
- package/dist/extensions/pi-zentui/extensions/zentui/index.ts +77 -3
- package/dist/extensions/pi-zentui/extensions/zentui/presets.ts +77 -0
- package/dist/extensions/pi-zentui/extensions/zentui/settings-command.ts +103 -2
- package/dist/extensions/pi-zentui/extensions/zentui/working-line-extension-segments.ts +115 -0
- package/dist/extensions/pi-zentui/extensions/zentui/working-line.ts +87 -5
- package/dist/extensions/pi-zentui/package-lock.json +277 -517
- package/dist/extensions/pi-zentui/package.json +7 -4
- package/dist/extensions/pi-zentui/test/config-presets.test.ts +238 -0
- package/dist/extensions/pi-zentui/test/editor-transfer.test.ts +24 -1
- package/dist/extensions/pi-zentui/test/extension-compliance.test.ts +570 -2
- package/dist/extensions/pi-zentui/test/extension-status.test.ts +90 -0
- package/dist/extensions/pi-zentui/test/footer-hyperlink.test.ts +243 -0
- package/dist/extensions/pi-zentui/test/package-contents.mjs +1 -0
- package/dist/extensions/pi-zentui/test/presets.test.ts +121 -0
- package/dist/extensions/pi-zentui/test/settings-command.test.ts +205 -1
- package/dist/extensions/pi-zentui/test/working-line-extension-segments.test.ts +139 -0
- package/dist/extensions/pi-zentui/test/working-line-lifecycle.integration.test.ts +237 -3
- package/dist/extensions/pi-zentui/test/working-line.test.ts +94 -2
- package/dist/index.d.ts +1 -1
- package/dist/main.js +4 -1
- package/dist/modes/interactive/chat-viewport.d.ts +19 -0
- package/dist/modes/interactive/chat-viewport.js +27 -0
- package/dist/modes/interactive/components/assistant-message.d.ts +6 -2
- package/dist/modes/interactive/components/assistant-message.js +30 -13
- package/dist/modes/interactive/components/custom-editor.d.ts +10 -1
- package/dist/modes/interactive/components/custom-editor.js +39 -1
- package/dist/modes/interactive/components/first-time-setup.js +1 -1
- package/dist/modes/interactive/components/index.d.ts +1 -1
- package/dist/modes/interactive/components/markdown-transform.d.ts +2 -0
- package/dist/modes/interactive/components/markdown-transform.js +18 -0
- package/dist/modes/interactive/components/model-selector.js +19 -17
- package/dist/modes/interactive/components/scoped-models-selector.js +21 -23
- package/dist/modes/interactive/components/settings-selector.d.ts +7 -1
- package/dist/modes/interactive/components/settings-selector.js +128 -20
- package/dist/modes/interactive/components/settings-submenu.d.ts +71 -0
- package/dist/modes/interactive/components/settings-submenu.js +164 -0
- package/dist/modes/interactive/components/status-indicator.d.ts +3 -1
- package/dist/modes/interactive/components/status-indicator.js +10 -3
- package/dist/modes/interactive/components/thinking-selector.d.ts +15 -3
- package/dist/modes/interactive/components/thinking-selector.js +75 -18
- package/dist/modes/interactive/components/tool-execution.d.ts +21 -4
- package/dist/modes/interactive/components/tool-execution.js +35 -33
- package/dist/modes/interactive/components/trust-selector.js +2 -2
- package/dist/modes/interactive/interactive-mode.d.ts +13 -15
- package/dist/modes/interactive/interactive-mode.js +144 -80
- package/dist/modes/interactive/session-share.d.ts +15 -0
- package/dist/modes/interactive/session-share.js +183 -0
- package/dist/modes/interactive/theme/dark.json +2 -1
- package/dist/modes/interactive/theme/light.json +2 -1
- package/dist/modes/interactive/theme/theme-controller.d.ts +1 -0
- package/dist/modes/interactive/theme/theme-controller.js +5 -0
- package/dist/modes/interactive/theme/theme-json.d.ts +83 -0
- package/dist/modes/interactive/theme/theme-json.js +129 -0
- package/dist/modes/interactive/theme/theme-schema.json +9 -1
- package/dist/modes/interactive/theme/theme.d.ts +14 -5
- package/dist/modes/interactive/theme/theme.js +18 -119
- package/dist/modes/interactive/tui-renderer.d.ts +20 -0
- package/dist/modes/interactive/tui-renderer.js +65 -0
- package/dist/utils/exif-orientation.js +2 -3
- package/dist/utils/management-http.d.ts +24 -0
- package/dist/utils/management-http.js +53 -0
- package/dist/utils/text.d.ts +7 -0
- package/dist/utils/text.js +8 -0
- package/dist/utils/tools-manager.d.ts +1 -0
- package/dist/utils/tools-manager.js +43 -21
- package/docs/docs.json +0 -4
- package/package.json +4 -4
- package/dist/extensions/pi-subagents/src/runs/shared/child-protocol.ts +0 -415
- package/dist/extensions/pi-subagents/src/runs/shared/pi-args.ts +0 -1059
- package/dist/extensions/pi-subagents/src/runs/shared/subagent-startup-retry.ts +0 -116
- package/dist/extensions/pi-subagents/src/shared/post-exit-stdio-guard.ts +0 -85
- package/dist/extensions/pi-subagents/test/e2e/real-session-subagent.test.ts +0 -224
- package/dist/extensions/pi-subagents/test/integration/async-execution.test.ts +0 -6986
- package/dist/extensions/pi-subagents/test/support/mock-pi-script.mjs +0 -447
- package/dist/extensions/pi-subagents/test/support/real-session-child-cli.mjs +0 -185
- package/dist/extensions/pi-subagents/test/support/real-session-runner.ts +0 -311
- package/dist/extensions/pi-subagents/test/unit/capability-ceiling-pi-args.test.ts +0 -84
- package/dist/extensions/pi-subagents/test/unit/child-protocol.test.ts +0 -202
- package/dist/extensions/pi-subagents/test/unit/close-grace-timer.test.ts +0 -101
- package/dist/extensions/pi-subagents/test/unit/pi-args.test.ts +0 -2347
- package/dist/extensions/pi-subagents/test/unit/subagent-startup-retry.test.ts +0 -104
- package/dist/extensions/pi-subagents/test/unit/windows-hide-spawn.test.ts +0 -49
- package/dist/skills/imagegen-frontend-mobile/SKILL.md +0 -1465
- package/dist/skills/imagegen-frontend-web/SKILL.md +0 -987
- package/dist/skills/implanger/SKILL.md +0 -70
- package/dist/skills/output/SKILL.md +0 -49
- package/dist/skills/planger/SKILL.md +0 -167
- package/dist/skills/workflow/SKILL.md +0 -95
- package/docs/workflows.md +0 -18
- /package/dist/bun/{register-bedrock.d.ts → runtime-setup.d.ts} +0 -0
|
@@ -0,0 +1,642 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drives one in-process child session for the detached async runner: creates
|
|
3
|
+
* the session through the runner's `ChildSessionFactory`, mirrors its events
|
|
4
|
+
* into `events.jsonl`, the transcript, and the step output log, applies
|
|
5
|
+
* interrupt, timeout, stop, and steer requests, and folds the run into the
|
|
6
|
+
* step result the runner finalizes.
|
|
7
|
+
*/
|
|
8
|
+
import type { Message } from "@earendil-works/pi-ai";
|
|
9
|
+
import type { ChildTranscriptWriter } from "../../shared/child-transcript.ts";
|
|
10
|
+
import { extractTextFromContent, extractToolArgsPreview, getFinalOutput, hasEmptyTerminalAssistantResponse } from "../../shared/utils.ts";
|
|
11
|
+
import type { EffectsProjection, RuntimeAcknowledgedChildExtensions, SubagentOutputState, ToolBudgetState, Usage } from "../../shared/types.ts";
|
|
12
|
+
import {
|
|
13
|
+
acceptChildWatchdogEvent,
|
|
14
|
+
applyChildWatchdogMessage,
|
|
15
|
+
childWatchdogIsActive,
|
|
16
|
+
isChildWatchdogStatusEvent,
|
|
17
|
+
type ChildWatchdogConfig,
|
|
18
|
+
type ChildWatchdogStateSnapshot,
|
|
19
|
+
type ChildWatchdogStatusEvent,
|
|
20
|
+
} from "../../watchdog/child-status.ts";
|
|
21
|
+
import { projectChildLifecycle, type ChildLifecycleAction, type ChildLifecycleState } from "../shared/child-lifecycle.ts";
|
|
22
|
+
import { formatSubagentModelVerificationError } from "../shared/model-fallback.ts";
|
|
23
|
+
import { isMutatingTool, resolveCurrentPath } from "../shared/long-running-guard.ts";
|
|
24
|
+
import { effectiveToolTimeoutMs, formatToolTimeoutMessage, toolTimeoutCallKey } from "../shared/tool-timeout.ts";
|
|
25
|
+
import { createReportedChildSessionInput, type InProcessChildLaunch } from "../shared/child-launch.ts";
|
|
26
|
+
import { projectChildSessionEventForJson, type ChildSession, type ChildSessionEvent, type ChildSessionFactory } from "../shared/child-session.ts";
|
|
27
|
+
import { formatSteerMessage } from "../shared/subagent-prompt-runtime.ts";
|
|
28
|
+
import { getReadonlySessionEvidence, requestReadonlySessionEvidence, type SettledReadonlyEvidence } from "../shared/readonly-session-evidence.ts";
|
|
29
|
+
import type { SteerDeliveryStatus, SteerRequest } from "./control-channel.ts";
|
|
30
|
+
|
|
31
|
+
export interface ChildEventContext {
|
|
32
|
+
runId: string;
|
|
33
|
+
stepIndex: number;
|
|
34
|
+
agent: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface ChildUsage {
|
|
38
|
+
input?: number;
|
|
39
|
+
inputTokens?: number;
|
|
40
|
+
output?: number;
|
|
41
|
+
outputTokens?: number;
|
|
42
|
+
cacheRead?: number;
|
|
43
|
+
cacheReadTokens?: number;
|
|
44
|
+
cacheWrite?: number;
|
|
45
|
+
cost?: { total?: number };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type ChildMessage = Message & {
|
|
49
|
+
model?: string;
|
|
50
|
+
errorMessage?: string;
|
|
51
|
+
usage?: ChildUsage;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export interface ChildEvent {
|
|
55
|
+
type?: string;
|
|
56
|
+
message?: ChildMessage;
|
|
57
|
+
toolName?: string;
|
|
58
|
+
toolCallId?: string;
|
|
59
|
+
args?: Record<string, unknown>;
|
|
60
|
+
willRetry?: unknown;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Outcome of handing a steer request to a live child session. */
|
|
64
|
+
export interface SteerDelivery {
|
|
65
|
+
state: "delivered" | "queued" | "failed";
|
|
66
|
+
deliveryStatus?: SteerDeliveryStatus;
|
|
67
|
+
message: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export type StepSteerHandler = (request: SteerRequest) => Promise<SteerDelivery>;
|
|
71
|
+
|
|
72
|
+
export interface RunChildSessionInput {
|
|
73
|
+
factory: ChildSessionFactory;
|
|
74
|
+
launch: InProcessChildLaunch;
|
|
75
|
+
/** Prompt text; the task with its `Task:` prefix. */
|
|
76
|
+
prompt: string;
|
|
77
|
+
childWatchdog?: ChildWatchdogConfig;
|
|
78
|
+
childEventContext?: ChildEventContext;
|
|
79
|
+
/** Persist one child event into `events.jsonl`; the runner owns the bounded log. */
|
|
80
|
+
appendChildEvent: (event: Record<string, unknown>) => void;
|
|
81
|
+
/** Append one line to the step output log and the progress tab. */
|
|
82
|
+
writeOutputLine: (line: string) => void;
|
|
83
|
+
registerInterrupt?: (interrupt: (() => void) | undefined) => void;
|
|
84
|
+
registerTimeout?: (interrupt: (() => void) | undefined) => void;
|
|
85
|
+
registerStop?: (stop: (() => void) | undefined) => void;
|
|
86
|
+
registerSteer?: (steer: StepSteerHandler | undefined) => void;
|
|
87
|
+
/** Receives the sink the child's watchdog hook reports status through; the launch's `watchdogStatus` must forward to it. */
|
|
88
|
+
registerWatchdogStatus?: (sink: ((event: ChildWatchdogStatusEvent) => void) | undefined) => void;
|
|
89
|
+
timeoutMessage?: string;
|
|
90
|
+
stopMessage?: string;
|
|
91
|
+
onChildEvent?: (event: ChildEvent) => void;
|
|
92
|
+
transcriptWriter?: ChildTranscriptWriter;
|
|
93
|
+
toolTimeoutMs?: number;
|
|
94
|
+
runDeadlineAt?: number;
|
|
95
|
+
expectedModelForVerification?: string;
|
|
96
|
+
modelVerificationRegistry?: Array<{ provider: string; id: string; fullId: string }>;
|
|
97
|
+
modelResponseAliases?: Record<string, string[]>;
|
|
98
|
+
mutationTools?: readonly string[];
|
|
99
|
+
/** Internal guarded continuation handoff; never part of persisted results. */
|
|
100
|
+
readonlyContinuation?: { source: ChildSession; expected: SettledReadonlyEvidence; modelId: string };
|
|
101
|
+
collectReadonlyEvidence?: boolean;
|
|
102
|
+
canContinue?: () => boolean;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const settledChildren = new WeakMap<RunChildSessionResult, ChildSession>();
|
|
106
|
+
export function getSettledReadonlyChild(result: RunChildSessionResult): ChildSession | undefined {
|
|
107
|
+
const child = settledChildren.get(result);
|
|
108
|
+
return child && getReadonlySessionEvidence(child) ? child : undefined;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface RunChildSessionResult {
|
|
112
|
+
exitCode: number;
|
|
113
|
+
messages: Message[];
|
|
114
|
+
usage: Usage;
|
|
115
|
+
toolCount: number;
|
|
116
|
+
durationMs: number;
|
|
117
|
+
model?: string;
|
|
118
|
+
error?: string;
|
|
119
|
+
finalOutput: string;
|
|
120
|
+
outputState: SubagentOutputState;
|
|
121
|
+
interrupted?: boolean;
|
|
122
|
+
timedOut?: boolean;
|
|
123
|
+
stopped?: boolean;
|
|
124
|
+
observedMutationAttempt?: boolean;
|
|
125
|
+
structuredOutputToolInvoked?: boolean;
|
|
126
|
+
structuredOutputMessageStartIndex?: number;
|
|
127
|
+
watchdog?: ChildWatchdogStateSnapshot;
|
|
128
|
+
sessionFile?: string;
|
|
129
|
+
currentTool?: string;
|
|
130
|
+
currentToolArgs?: string;
|
|
131
|
+
currentPath?: string;
|
|
132
|
+
afterCompactionSettlement?: boolean;
|
|
133
|
+
/** Set by the runner while it finalizes the attempt. */
|
|
134
|
+
toolBudget?: ToolBudgetState;
|
|
135
|
+
toolBudgetBlocked?: boolean;
|
|
136
|
+
structuredOutput?: unknown;
|
|
137
|
+
runtimeAcknowledgedExtensions?: RuntimeAcknowledgedChildExtensions;
|
|
138
|
+
abortRecoveryDiagnostic?: string;
|
|
139
|
+
effects?: EffectsProjection;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Events the child emits while the model streams; not persisted into the diagnostic log. */
|
|
143
|
+
function shouldPersistChildEvent(event: Record<string, unknown>): boolean {
|
|
144
|
+
return event.type !== "message_update";
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function assistantStartsToolCall(message: Message): boolean {
|
|
148
|
+
return Array.isArray(message.content)
|
|
149
|
+
&& message.content.some((part) => (part as { type?: string }).type === "toolCall");
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function isTerminalAssistantStop(message: Message): boolean {
|
|
153
|
+
return (message as { stopReason?: string }).stopReason === "stop" && !assistantStartsToolCall(message);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function emptyUsage(): Usage {
|
|
157
|
+
return { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, turns: 0 };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function omitUndefined<T extends object>(value: T): T {
|
|
161
|
+
for (const key of Object.keys(value) as Array<keyof T>) {
|
|
162
|
+
if (value[key] === undefined) delete value[key];
|
|
163
|
+
}
|
|
164
|
+
return value;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const FINAL_STOP_GRACE_MS = 1000;
|
|
168
|
+
const HARD_FINISH_MS = 3000;
|
|
169
|
+
const ABORT_SETTLE_MS = 3000;
|
|
170
|
+
|
|
171
|
+
export function runChildSession(input: RunChildSessionInput): Promise<RunChildSessionResult> {
|
|
172
|
+
return new Promise((resolve) => {
|
|
173
|
+
const startedAt = Date.now();
|
|
174
|
+
const messages: Message[] = [];
|
|
175
|
+
const usage = emptyUsage();
|
|
176
|
+
let model: string | undefined;
|
|
177
|
+
let error: string | undefined;
|
|
178
|
+
let assistantError: string | undefined;
|
|
179
|
+
let interrupted = false;
|
|
180
|
+
let timedOut = false;
|
|
181
|
+
let stopped = false;
|
|
182
|
+
let observedMutationAttempt = false;
|
|
183
|
+
let structuredOutputToolInvoked = false;
|
|
184
|
+
let structuredOutputMessageStartIndex: number | undefined;
|
|
185
|
+
let currentTool: string | undefined;
|
|
186
|
+
let currentToolArgs: string | undefined;
|
|
187
|
+
let currentPath: string | undefined;
|
|
188
|
+
let toolCount = 0;
|
|
189
|
+
let session: ChildSession | undefined;
|
|
190
|
+
let unsubscribe: (() => void) | undefined;
|
|
191
|
+
let settled = false;
|
|
192
|
+
let promptSettled = false;
|
|
193
|
+
let forcedTermination = false;
|
|
194
|
+
let cleanTerminalAssistantStopReceived = false;
|
|
195
|
+
let agentSettledReceived = false;
|
|
196
|
+
let compactionStartedReceived = false;
|
|
197
|
+
let afterCompactionSettlement = false;
|
|
198
|
+
let finalDrainTimer: NodeJS.Timeout | undefined;
|
|
199
|
+
let finalHardFinishTimer: NodeJS.Timeout | undefined;
|
|
200
|
+
let watchdogTailTimer: NodeJS.Timeout | undefined;
|
|
201
|
+
let abortSettleTimer: NodeJS.Timeout | undefined;
|
|
202
|
+
let childWatchdogState: ChildWatchdogStateSnapshot | undefined;
|
|
203
|
+
const childLifecycleState: ChildLifecycleState = { compactionRetryActive: false };
|
|
204
|
+
const timeoutMessage = () => input.timeoutMessage ?? "Subagent timed out.";
|
|
205
|
+
const stopMessage = () => input.stopMessage ?? "Subagent stopped by user.";
|
|
206
|
+
|
|
207
|
+
type ActiveToolCall = { key: string; tool: string; args?: string; path?: string };
|
|
208
|
+
let activeToolSequence = 0;
|
|
209
|
+
const activeToolCalls = new Map<string, ActiveToolCall>();
|
|
210
|
+
const activeToolKeysByName = new Map<string, string[]>();
|
|
211
|
+
const refreshCurrentTool = (): void => {
|
|
212
|
+
const active = [...activeToolCalls.values()].at(-1);
|
|
213
|
+
currentTool = active?.tool;
|
|
214
|
+
currentToolArgs = active?.args;
|
|
215
|
+
currentPath = active?.path;
|
|
216
|
+
};
|
|
217
|
+
const recordActiveToolCall = (event: { toolCallId?: unknown; toolName: string; args?: Record<string, unknown> }): void => {
|
|
218
|
+
const key = toolTimeoutCallKey(event, ++activeToolSequence);
|
|
219
|
+
const active = omitUndefined({
|
|
220
|
+
key,
|
|
221
|
+
tool: event.toolName,
|
|
222
|
+
args: extractToolArgsPreview(event.args ?? {}),
|
|
223
|
+
path: resolveCurrentPath(event.toolName, event.args),
|
|
224
|
+
});
|
|
225
|
+
activeToolCalls.set(key, active);
|
|
226
|
+
const keys = activeToolKeysByName.get(active.tool) ?? [];
|
|
227
|
+
keys.push(key);
|
|
228
|
+
activeToolKeysByName.set(active.tool, keys);
|
|
229
|
+
refreshCurrentTool();
|
|
230
|
+
};
|
|
231
|
+
const removeActiveToolCall = (event: { toolCallId?: unknown; toolName?: unknown }): void => {
|
|
232
|
+
const key = typeof event.toolCallId === "string" && event.toolCallId.length > 0
|
|
233
|
+
? `id:${event.toolCallId}`
|
|
234
|
+
: typeof event.toolName === "string"
|
|
235
|
+
? activeToolKeysByName.get(event.toolName)?.[0]
|
|
236
|
+
: activeToolCalls.size === 1
|
|
237
|
+
? [...activeToolCalls.keys()][0]
|
|
238
|
+
: undefined;
|
|
239
|
+
if (!key) return;
|
|
240
|
+
const active = activeToolCalls.get(key);
|
|
241
|
+
if (!active) return;
|
|
242
|
+
activeToolCalls.delete(key);
|
|
243
|
+
const keys = activeToolKeysByName.get(active.tool)?.filter((candidate) => candidate !== key) ?? [];
|
|
244
|
+
if (keys.length > 0) activeToolKeysByName.set(active.tool, keys);
|
|
245
|
+
else activeToolKeysByName.delete(active.tool);
|
|
246
|
+
refreshCurrentTool();
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
const abortChild = (): void => {
|
|
250
|
+
if (!session || settled || promptSettled) return;
|
|
251
|
+
void session.abort().catch(() => {
|
|
252
|
+
// The run settles through its prompt promise; abort failures are not separately actionable.
|
|
253
|
+
});
|
|
254
|
+
if (!abortSettleTimer) {
|
|
255
|
+
abortSettleTimer = setTimeout(() => {
|
|
256
|
+
abortSettleTimer = undefined;
|
|
257
|
+
if (!settled && !promptSettled) settle(undefined, true);
|
|
258
|
+
}, ABORT_SETTLE_MS);
|
|
259
|
+
abortSettleTimer.unref?.();
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
const writeOutputText = (text: string) => {
|
|
264
|
+
for (const line of text.split("\n")) {
|
|
265
|
+
if (line.trim()) input.writeOutputLine(line);
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
const appendChildEvent = (event: Record<string, unknown>) => {
|
|
269
|
+
if (!input.childEventContext) return;
|
|
270
|
+
if (!shouldPersistChildEvent(event)) return;
|
|
271
|
+
input.appendChildEvent({
|
|
272
|
+
...event,
|
|
273
|
+
subagentSource: "child",
|
|
274
|
+
subagentRunId: input.childEventContext.runId,
|
|
275
|
+
subagentStepIndex: input.childEventContext.stepIndex,
|
|
276
|
+
subagentAgent: input.childEventContext.agent,
|
|
277
|
+
observedAt: Date.now(),
|
|
278
|
+
});
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
const clearWatchdogTailTimer = (): void => {
|
|
282
|
+
if (watchdogTailTimer) {
|
|
283
|
+
clearTimeout(watchdogTailTimer);
|
|
284
|
+
watchdogTailTimer = undefined;
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
const clearFinalDrainTimers = (): void => {
|
|
288
|
+
if (finalDrainTimer) {
|
|
289
|
+
clearTimeout(finalDrainTimer);
|
|
290
|
+
finalDrainTimer = undefined;
|
|
291
|
+
}
|
|
292
|
+
if (finalHardFinishTimer) {
|
|
293
|
+
clearTimeout(finalHardFinishTimer);
|
|
294
|
+
finalHardFinishTimer = undefined;
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
function armWatchdogTail(): void {
|
|
298
|
+
if ((!cleanTerminalAssistantStopReceived && !agentSettledReceived) || watchdogTailTimer || settled || promptSettled) return;
|
|
299
|
+
watchdogTailTimer = setTimeout(() => {
|
|
300
|
+
watchdogTailTimer = undefined;
|
|
301
|
+
childWatchdogState = {
|
|
302
|
+
phase: "stale",
|
|
303
|
+
seq: (childWatchdogState?.seq ?? 0) + 1,
|
|
304
|
+
lastUpdate: Date.now(),
|
|
305
|
+
reason: "child watchdog tail timeout",
|
|
306
|
+
timedOut: true,
|
|
307
|
+
};
|
|
308
|
+
startFinalDrain();
|
|
309
|
+
}, input.childWatchdog?.watchdogTailTimeoutMs ?? 120_000);
|
|
310
|
+
watchdogTailTimer.unref?.();
|
|
311
|
+
}
|
|
312
|
+
// If the child emits its terminal event but its run never settles (a hook
|
|
313
|
+
// is stuck), abort it after a short grace period and then finish without it.
|
|
314
|
+
function startFinalDrain(): void {
|
|
315
|
+
if (childWatchdogIsActive(childWatchdogState)) {
|
|
316
|
+
armWatchdogTail();
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
if (promptSettled || finalDrainTimer || settled) return;
|
|
320
|
+
finalDrainTimer = setTimeout(() => {
|
|
321
|
+
if (settled || promptSettled) return;
|
|
322
|
+
forcedTermination = true;
|
|
323
|
+
if (!cleanTerminalAssistantStopReceived && !agentSettledReceived && !error && !assistantError) {
|
|
324
|
+
error = `Subagent session did not settle within ${FINAL_STOP_GRACE_MS}ms after its terminal event. Aborting it.`;
|
|
325
|
+
}
|
|
326
|
+
abortChild();
|
|
327
|
+
finalHardFinishTimer = setTimeout(() => {
|
|
328
|
+
if (settled || promptSettled) return;
|
|
329
|
+
settle(undefined, true);
|
|
330
|
+
}, HARD_FINISH_MS);
|
|
331
|
+
finalHardFinishTimer.unref?.();
|
|
332
|
+
}, FINAL_STOP_GRACE_MS);
|
|
333
|
+
finalDrainTimer.unref?.();
|
|
334
|
+
}
|
|
335
|
+
const applyChildLifecycle = (action: ChildLifecycleAction): void => {
|
|
336
|
+
if (action === "cancel-drain") {
|
|
337
|
+
clearFinalDrainTimers();
|
|
338
|
+
clearWatchdogTailTimer();
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
if (action === "start-drain") startFinalDrain();
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
let toolTimeoutSequence = 0;
|
|
345
|
+
const activeToolTimeouts = new Map<string, { toolName: string; timer: ReturnType<typeof setTimeout> }>();
|
|
346
|
+
const activeToolTimeoutKeysByName = new Map<string, string[]>();
|
|
347
|
+
const removeToolTimeoutKey = (key: string): void => {
|
|
348
|
+
const active = activeToolTimeouts.get(key);
|
|
349
|
+
if (!active) return;
|
|
350
|
+
clearTimeout(active.timer);
|
|
351
|
+
activeToolTimeouts.delete(key);
|
|
352
|
+
const keys = activeToolTimeoutKeysByName.get(active.toolName)?.filter((candidate) => candidate !== key) ?? [];
|
|
353
|
+
if (keys.length > 0) activeToolTimeoutKeysByName.set(active.toolName, keys);
|
|
354
|
+
else activeToolTimeoutKeysByName.delete(active.toolName);
|
|
355
|
+
};
|
|
356
|
+
const clearActiveToolTimeout = (event: { toolCallId?: unknown; toolName?: unknown }): void => {
|
|
357
|
+
const key = typeof event.toolCallId === "string" && event.toolCallId.length > 0
|
|
358
|
+
? `id:${event.toolCallId}`
|
|
359
|
+
: typeof event.toolName === "string"
|
|
360
|
+
? activeToolTimeoutKeysByName.get(event.toolName)?.[0]
|
|
361
|
+
: activeToolTimeouts.size === 1
|
|
362
|
+
? [...activeToolTimeouts.keys()][0]
|
|
363
|
+
: undefined;
|
|
364
|
+
if (key) removeToolTimeoutKey(key);
|
|
365
|
+
};
|
|
366
|
+
const clearAllToolTimeouts = (): void => {
|
|
367
|
+
for (const key of [...activeToolTimeouts.keys()]) removeToolTimeoutKey(key);
|
|
368
|
+
};
|
|
369
|
+
const terminateForTimeout = (message: string): void => {
|
|
370
|
+
if (settled || promptSettled || timedOut || stopped) return;
|
|
371
|
+
timedOut = true;
|
|
372
|
+
interrupted = false;
|
|
373
|
+
error = message;
|
|
374
|
+
abortChild();
|
|
375
|
+
};
|
|
376
|
+
const armToolTimeout = (event: { toolCallId?: unknown; toolName: string }): void => {
|
|
377
|
+
const timeoutForTool = effectiveToolTimeoutMs(event.toolName, input.toolTimeoutMs);
|
|
378
|
+
if (timeoutForTool === undefined) return;
|
|
379
|
+
const runRemaining = input.runDeadlineAt === undefined ? undefined : Math.max(0, input.runDeadlineAt - Date.now());
|
|
380
|
+
if (runRemaining !== undefined && timeoutForTool >= runRemaining) return;
|
|
381
|
+
const key = toolTimeoutCallKey(event, ++toolTimeoutSequence);
|
|
382
|
+
const toolName = event.toolName;
|
|
383
|
+
const timer = setTimeout(() => {
|
|
384
|
+
removeToolTimeoutKey(key);
|
|
385
|
+
terminateForTimeout(formatToolTimeoutMessage(toolName, timeoutForTool));
|
|
386
|
+
}, timeoutForTool);
|
|
387
|
+
timer.unref?.();
|
|
388
|
+
activeToolTimeouts.set(key, { toolName, timer });
|
|
389
|
+
const keys = activeToolTimeoutKeysByName.get(toolName) ?? [];
|
|
390
|
+
keys.push(key);
|
|
391
|
+
activeToolTimeoutKeysByName.set(toolName, keys);
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
const processEvent = (raw: ChildSessionEvent): void => {
|
|
395
|
+
if (settled) return;
|
|
396
|
+
const event = raw as ChildSessionEvent & ChildEvent;
|
|
397
|
+
appendChildEvent(projectChildSessionEventForJson(raw) as Record<string, unknown>);
|
|
398
|
+
input.transcriptWriter?.writeChildEvent(projectChildSessionEventForJson(raw) as ChildEvent);
|
|
399
|
+
if (event.type === "compaction_start") compactionStartedReceived = true;
|
|
400
|
+
if (event.type === "compaction_end" && event.willRetry === true) {
|
|
401
|
+
compactionStartedReceived = false;
|
|
402
|
+
afterCompactionSettlement = false;
|
|
403
|
+
}
|
|
404
|
+
if (event.type === "agent_start" || event.type === "auto_retry_start") {
|
|
405
|
+
compactionStartedReceived = false;
|
|
406
|
+
afterCompactionSettlement = false;
|
|
407
|
+
}
|
|
408
|
+
const lifecycleAction = projectChildLifecycle(event, false, childLifecycleState);
|
|
409
|
+
if (event.type === "agent_settled" && lifecycleAction === "start-drain") {
|
|
410
|
+
agentSettledReceived = true;
|
|
411
|
+
afterCompactionSettlement = compactionStartedReceived;
|
|
412
|
+
}
|
|
413
|
+
applyChildLifecycle(lifecycleAction);
|
|
414
|
+
|
|
415
|
+
if (isChildWatchdogStatusEvent(event)) {
|
|
416
|
+
if (!input.childWatchdog) return;
|
|
417
|
+
const next = acceptChildWatchdogEvent({
|
|
418
|
+
current: childWatchdogState,
|
|
419
|
+
event,
|
|
420
|
+
...(input.childEventContext ? {
|
|
421
|
+
runId: input.childEventContext.runId,
|
|
422
|
+
agent: input.childEventContext.agent,
|
|
423
|
+
childIndex: input.childEventContext.stepIndex,
|
|
424
|
+
} : {}),
|
|
425
|
+
});
|
|
426
|
+
if (!next) return;
|
|
427
|
+
childWatchdogState = next;
|
|
428
|
+
input.onChildEvent?.(event);
|
|
429
|
+
if (childWatchdogIsActive(next)) {
|
|
430
|
+
clearFinalDrainTimers();
|
|
431
|
+
armWatchdogTail();
|
|
432
|
+
} else {
|
|
433
|
+
clearWatchdogTailTimer();
|
|
434
|
+
if (cleanTerminalAssistantStopReceived || agentSettledReceived) startFinalDrain();
|
|
435
|
+
}
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
input.onChildEvent?.(event);
|
|
440
|
+
|
|
441
|
+
if (event.type === "tool_execution_end") {
|
|
442
|
+
clearActiveToolTimeout(event);
|
|
443
|
+
removeActiveToolCall(event);
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
if (event.type === "tool_execution_start" && event.toolName) {
|
|
448
|
+
toolCount += 1;
|
|
449
|
+
const toolArgs = event.args && typeof event.args === "object" && !Array.isArray(event.args) ? event.args : {};
|
|
450
|
+
armToolTimeout({ toolCallId: event.toolCallId, toolName: event.toolName });
|
|
451
|
+
recordActiveToolCall({ toolCallId: event.toolCallId, toolName: event.toolName, args: toolArgs });
|
|
452
|
+
if (event.toolName === "structured_output") {
|
|
453
|
+
structuredOutputToolInvoked = true;
|
|
454
|
+
structuredOutputMessageStartIndex = messages.length;
|
|
455
|
+
}
|
|
456
|
+
observedMutationAttempt = observedMutationAttempt || isMutatingTool(event.toolName, toolArgs, input.mutationTools);
|
|
457
|
+
const preview = extractToolArgsPreview(toolArgs);
|
|
458
|
+
input.writeOutputLine(preview ? `${event.toolName}: ${preview}` : event.toolName);
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
if ((event.type === "message_end" || event.type === "tool_result_end") && event.message) {
|
|
463
|
+
if (event.type === "tool_result_end") {
|
|
464
|
+
clearActiveToolTimeout(event);
|
|
465
|
+
removeActiveToolCall({
|
|
466
|
+
toolCallId: (event.message as { toolCallId?: unknown }).toolCallId ?? event.toolCallId,
|
|
467
|
+
toolName: (event.message as { toolName?: unknown }).toolName ?? event.toolName,
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
messages.push(event.message);
|
|
471
|
+
const text = extractTextFromContent(event.message.content);
|
|
472
|
+
if (text) writeOutputText(text);
|
|
473
|
+
|
|
474
|
+
if (input.childWatchdog && event.type === "message_end") {
|
|
475
|
+
const next = applyChildWatchdogMessage(childWatchdogState, event.message);
|
|
476
|
+
if (next) childWatchdogState = next;
|
|
477
|
+
}
|
|
478
|
+
if (event.type !== "message_end" || event.message.role !== "assistant") return;
|
|
479
|
+
const hasToolCall = assistantStartsToolCall(event.message);
|
|
480
|
+
if (event.message.model) {
|
|
481
|
+
model = event.message.model;
|
|
482
|
+
if (input.expectedModelForVerification && !hasToolCall) {
|
|
483
|
+
const modelVerificationError = formatSubagentModelVerificationError(input.expectedModelForVerification, event.message.model, input.modelVerificationRegistry, input.modelResponseAliases);
|
|
484
|
+
if (modelVerificationError && !error) error = modelVerificationError;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
if (event.message.errorMessage) assistantError = event.message.errorMessage;
|
|
488
|
+
else if (hasToolCall && event.message.stopReason === "toolUse") {
|
|
489
|
+
// A recovered request can finish via a terminating tool, without a text stop.
|
|
490
|
+
assistantError = undefined;
|
|
491
|
+
}
|
|
492
|
+
const eventUsage = event.message.usage;
|
|
493
|
+
if (eventUsage) {
|
|
494
|
+
usage.turns++;
|
|
495
|
+
usage.input += eventUsage.input ?? eventUsage.inputTokens ?? 0;
|
|
496
|
+
usage.output += eventUsage.output ?? eventUsage.outputTokens ?? 0;
|
|
497
|
+
usage.cacheRead += eventUsage.cacheRead ?? 0;
|
|
498
|
+
usage.cacheWrite += eventUsage.cacheWrite ?? 0;
|
|
499
|
+
usage.cost += eventUsage.cost?.total ?? 0;
|
|
500
|
+
}
|
|
501
|
+
if (isTerminalAssistantStop(event.message)) {
|
|
502
|
+
if (!event.message.errorMessage && extractTextFromContent(event.message.content).trim()) assistantError = undefined;
|
|
503
|
+
cleanTerminalAssistantStopReceived ||= !event.message.errorMessage;
|
|
504
|
+
clearAllToolTimeouts();
|
|
505
|
+
activeToolCalls.clear();
|
|
506
|
+
activeToolKeysByName.clear();
|
|
507
|
+
refreshCurrentTool();
|
|
508
|
+
applyChildLifecycle(projectChildLifecycle(event, true, childLifecycleState));
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
/** Stops observing the child and returns when its extensions have shut down. */
|
|
514
|
+
const finish = (): Promise<void> => {
|
|
515
|
+
clearFinalDrainTimers();
|
|
516
|
+
clearWatchdogTailTimer();
|
|
517
|
+
clearAllToolTimeouts();
|
|
518
|
+
if (abortSettleTimer) {
|
|
519
|
+
clearTimeout(abortSettleTimer);
|
|
520
|
+
abortSettleTimer = undefined;
|
|
521
|
+
}
|
|
522
|
+
input.registerInterrupt?.(undefined);
|
|
523
|
+
input.registerTimeout?.(undefined);
|
|
524
|
+
input.registerStop?.(undefined);
|
|
525
|
+
input.registerSteer?.(undefined);
|
|
526
|
+
input.registerWatchdogStatus?.(undefined);
|
|
527
|
+
unsubscribe?.();
|
|
528
|
+
return Promise.resolve().then(() => session?.dispose()).catch(() => undefined);
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
/** The child run ended (or was forced to end); fold in the outcome once the child's shutdown work is done. */
|
|
532
|
+
const settle = (promptError: unknown, forced = false): void => {
|
|
533
|
+
if (settled) return;
|
|
534
|
+
settled = true;
|
|
535
|
+
const closed = finish();
|
|
536
|
+
const finalOutput = getFinalOutput(messages);
|
|
537
|
+
let finalError = error ?? assistantError;
|
|
538
|
+
if (!finalError && promptError !== undefined) {
|
|
539
|
+
finalError = promptError instanceof Error ? promptError.message : String(promptError);
|
|
540
|
+
}
|
|
541
|
+
const forcedDrainAfterFinalSuccess = (forced || forcedTermination) && (cleanTerminalAssistantStopReceived || agentSettledReceived) && !finalError;
|
|
542
|
+
const forcedDrainAfterEmptyTerminal = forcedDrainAfterFinalSuccess && hasEmptyTerminalAssistantResponse(messages);
|
|
543
|
+
if (!finalError && forced && !forcedDrainAfterFinalSuccess && !interrupted && !timedOut && !stopped) {
|
|
544
|
+
finalError = "Subagent session did not settle after it was aborted.";
|
|
545
|
+
}
|
|
546
|
+
const exitCode = timedOut || stopped
|
|
547
|
+
? 1
|
|
548
|
+
: interrupted || (forcedDrainAfterFinalSuccess && !forcedDrainAfterEmptyTerminal)
|
|
549
|
+
? 0
|
|
550
|
+
: finalError || promptError !== undefined ? 1 : 0;
|
|
551
|
+
void closed.then(() => {
|
|
552
|
+
const result: RunChildSessionResult = omitUndefined({
|
|
553
|
+
exitCode,
|
|
554
|
+
messages,
|
|
555
|
+
usage,
|
|
556
|
+
toolCount,
|
|
557
|
+
durationMs: Date.now() - startedAt,
|
|
558
|
+
model,
|
|
559
|
+
error: stopped ? stopMessage() : timedOut ? (error ?? timeoutMessage()) : interrupted || (forcedDrainAfterFinalSuccess && !forcedDrainAfterEmptyTerminal) ? undefined : finalError,
|
|
560
|
+
finalOutput: (timedOut || stopped) && !finalOutput.trim() ? (stopped ? stopMessage() : error ?? timeoutMessage()) : finalOutput,
|
|
561
|
+
outputState: finalOutput.trim() ? "present" : "absent",
|
|
562
|
+
interrupted: interrupted || undefined,
|
|
563
|
+
timedOut: timedOut || undefined,
|
|
564
|
+
stopped: stopped || undefined,
|
|
565
|
+
observedMutationAttempt,
|
|
566
|
+
structuredOutputToolInvoked,
|
|
567
|
+
structuredOutputMessageStartIndex,
|
|
568
|
+
watchdog: childWatchdogState,
|
|
569
|
+
sessionFile: session?.sessionFile,
|
|
570
|
+
currentTool,
|
|
571
|
+
currentToolArgs,
|
|
572
|
+
currentPath,
|
|
573
|
+
afterCompactionSettlement: afterCompactionSettlement || undefined,
|
|
574
|
+
});
|
|
575
|
+
if (session && !forced && !forcedTermination && !interrupted && !timedOut && !stopped && getReadonlySessionEvidence(session)) settledChildren.set(result, session);
|
|
576
|
+
resolve(result);
|
|
577
|
+
});
|
|
578
|
+
};
|
|
579
|
+
|
|
580
|
+
input.registerInterrupt?.(() => {
|
|
581
|
+
if (settled || promptSettled || timedOut || stopped) return;
|
|
582
|
+
interrupted = true;
|
|
583
|
+
if (!error) error = "Interrupted. Waiting for explicit next action.";
|
|
584
|
+
abortChild();
|
|
585
|
+
});
|
|
586
|
+
input.registerTimeout?.(() => terminateForTimeout(timeoutMessage()));
|
|
587
|
+
input.registerStop?.(() => {
|
|
588
|
+
if (settled || promptSettled || timedOut || stopped) return;
|
|
589
|
+
stopped = true;
|
|
590
|
+
interrupted = false;
|
|
591
|
+
error = stopMessage();
|
|
592
|
+
abortChild();
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
void (async () => {
|
|
596
|
+
try {
|
|
597
|
+
const continuation = input.readonlyContinuation;
|
|
598
|
+
const checkContinuation = () => {
|
|
599
|
+
if (!continuation) return;
|
|
600
|
+
if (interrupted || timedOut || stopped || input.canContinue?.() !== true
|
|
601
|
+
|| (input.runDeadlineAt !== undefined && Date.now() >= input.runDeadlineAt)
|
|
602
|
+
|| getReadonlySessionEvidence(continuation.source) !== continuation.expected
|
|
603
|
+
|| continuation.source.detached || continuation.source.shutDown) throw new Error("Read-only continuation handoff vetoed");
|
|
604
|
+
};
|
|
605
|
+
checkContinuation();
|
|
606
|
+
const createInput = createReportedChildSessionInput(input.launch, input.transcriptWriter);
|
|
607
|
+
if (input.collectReadonlyEvidence || continuation) requestReadonlySessionEvidence(createInput, continuation?.expected);
|
|
608
|
+
const created = await input.factory.create(createInput);
|
|
609
|
+
if (settled) {
|
|
610
|
+
void created.dispose();
|
|
611
|
+
return;
|
|
612
|
+
}
|
|
613
|
+
session = created;
|
|
614
|
+
checkContinuation();
|
|
615
|
+
if (continuation && (created.modelId !== continuation.modelId || input.launch.capture.completionIntentContext?.()?.model?.api !== continuation.expected.api)) throw new Error("Read-only continuation model changed");
|
|
616
|
+
unsubscribe = created.subscribe(processEvent);
|
|
617
|
+
input.registerWatchdogStatus?.((event) => processEvent(event as unknown as ChildSessionEvent));
|
|
618
|
+
input.registerSteer?.(async (request) => {
|
|
619
|
+
const text = formatSteerMessage(request);
|
|
620
|
+
const followUp = request.mode === "follow_up";
|
|
621
|
+
try {
|
|
622
|
+
if (followUp) await created.followUp(text);
|
|
623
|
+
else await created.steer(text);
|
|
624
|
+
} catch (steerError) {
|
|
625
|
+
return { state: "failed", message: steerError instanceof Error ? steerError.message : String(steerError) };
|
|
626
|
+
}
|
|
627
|
+
return followUp
|
|
628
|
+
? { state: "queued", deliveryStatus: "queued", message: "Pi queued the follow-up input." }
|
|
629
|
+
: { state: "delivered", deliveryStatus: "delivered", message: "Pi accepted the steering input." };
|
|
630
|
+
});
|
|
631
|
+
if (interrupted || timedOut || stopped) abortChild();
|
|
632
|
+
checkContinuation();
|
|
633
|
+
await created.prompt(input.prompt);
|
|
634
|
+
promptSettled = true;
|
|
635
|
+
settle(undefined);
|
|
636
|
+
} catch (promptError) {
|
|
637
|
+
promptSettled = true;
|
|
638
|
+
settle(promptError ?? new Error("Child session failed."));
|
|
639
|
+
}
|
|
640
|
+
})();
|
|
641
|
+
});
|
|
642
|
+
}
|