@xzzpig/pi-subagents 0.13.0 → 0.15.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 +243 -0
- package/README.md +6 -5
- package/agents/evidence-auditor.md +34 -0
- package/agents/researcher.md +23 -13
- package/agents/reviewer.md +3 -2
- package/docs/agents.md +56 -15
- package/docs/configuration.md +93 -15
- package/docs/extension-api.md +132 -19
- package/docs/missions.md +10 -2
- package/docs/models.md +11 -22
- package/docs/observability.md +67 -11
- package/docs/standalone-background.md +59 -0
- package/docs/tool-reference.md +42 -23
- package/docs/watchdog.md +39 -10
- package/docs/workflows.md +58 -19
- package/index.ts +5 -2
- package/inspector-runner.mjs +2 -2
- package/node_modules/@xzzpig/pi-components/CHANGELOG.md +19 -0
- package/node_modules/@xzzpig/pi-components/README.md +28 -0
- package/node_modules/@xzzpig/pi-components/dist/src/transcript.d.ts +46 -4
- package/node_modules/@xzzpig/pi-components/dist/src/transcript.js +65 -17
- package/node_modules/@xzzpig/pi-components/package.json +1 -1
- package/node_modules/@xzzpig/pi-components/src/transcript.ts +107 -14
- package/package.json +10 -5
- package/prompts/parallel-review.md +1 -1
- package/runner-peer-loader.mjs +24 -0
- package/runner-peer-preload.mjs +36 -0
- package/skills/pi-subagents/SKILL.md +34 -22
- package/skills/pi-subagents/references/constraints-and-recipes.md +3 -2
- package/skills/pi-subagents/references/execution-controls.md +27 -8
- package/skills/pi-subagents/references/management-authoring-rpc.md +2 -2
- package/skills/pi-subagents/references/multi-lane-orchestration.md +3 -1
- package/skills/pi-subagents/references/prompting-and-roles.md +17 -13
- package/skills/pi-subagents/references/review-and-validation.md +3 -3
- package/src/agents/advertised-agent-prompt.ts +94 -0
- package/src/agents/agent-management.ts +71 -59
- package/src/agents/agent-serializer.ts +6 -3
- package/src/agents/agents.ts +198 -72
- package/src/agents/builtin-names.ts +1 -0
- package/src/agents/chain-serializer.ts +5 -0
- package/src/agents/runtime-agent-registry.ts +7 -6
- package/src/api/delegation.ts +4 -0
- package/src/api/preflight.ts +110 -71
- package/src/api/required-child-extensions.ts +6 -0
- package/src/api/shared-types.ts +3 -1
- package/src/api/workflow-resources.ts +6 -0
- package/src/extension/config.ts +14 -37
- package/src/extension/fanout-child.ts +68 -5
- package/src/extension/herdr-pi-bridge.ts +164 -0
- package/src/extension/index.ts +141 -51
- package/src/extension/public-execution.ts +7 -6
- package/src/extension/rpc.ts +8 -21
- package/src/extension/schemas.ts +86 -84
- package/src/extension/tool-description.ts +32 -79
- package/src/inspectors/actions.ts +148 -0
- package/src/inspectors/ghostty/actions.ts +74 -0
- package/src/inspectors/ghostty/plugin.ts +17 -0
- package/src/inspectors/herdr/actions.ts +99 -179
- package/src/inspectors/herdr/plugin.ts +20 -0
- package/src/inspectors/herdr/project-panes.ts +4 -1
- package/src/inspectors/{herdr/inspector-runner.ts → inspector-runner.ts} +12 -12
- package/src/inspectors/plugins.ts +8 -0
- package/src/inspectors/{herdr/session-roots-codec.ts → session-roots-codec.ts} +3 -14
- package/src/inspectors/types.ts +51 -0
- package/src/integrations/pi-web-session-liveness.ts +73 -0
- package/src/intercom/intercom-bridge.ts +50 -8
- package/src/intercom/native-supervisor-channel.ts +142 -115
- package/src/intercom/supervisor-ui.ts +3 -2
- package/src/missions/workflow-state.ts +37 -16
- package/src/policy/authority.ts +4 -0
- package/src/profiles/profiles.ts +12 -6
- package/src/runs/background/active-async-capacity.ts +22 -18
- package/src/runs/background/active-run-index.ts +17 -1
- package/src/runs/background/async-execution.ts +359 -176
- package/src/runs/background/async-job-tracker.ts +41 -7
- package/src/runs/background/async-resume.ts +19 -12
- package/src/runs/background/async-retention.ts +20 -0
- package/src/runs/background/async-status-snapshot.ts +10 -12
- package/src/runs/background/async-status.ts +32 -13
- package/src/runs/background/auto-drain.ts +59 -35
- package/src/runs/background/binary-bootstrap.ts +38 -0
- package/src/runs/background/chain-append.ts +1 -1
- package/src/runs/background/chain-root-attachment.ts +22 -33
- package/src/runs/background/control-channel.ts +78 -44
- package/src/runs/background/fleet-view.ts +30 -2
- package/src/runs/background/notify.ts +193 -19
- package/src/runs/background/owned-process-tree.ts +35 -8
- package/src/runs/background/process-terminal.ts +23 -23
- package/src/runs/background/result-files.ts +8 -4
- package/src/runs/background/result-watcher.ts +19 -2
- package/src/runs/background/retained-nested-route-tracker.ts +96 -0
- package/src/runs/background/run-child-session.ts +118 -41
- package/src/runs/background/run-status.ts +78 -5
- package/src/runs/background/runner-aliases.ts +22 -5
- package/src/runs/background/runner-child-launch.ts +93 -0
- package/src/runs/background/runner-child-sessions.ts +5 -4
- package/src/runs/background/runner-http-dispatcher.ts +119 -0
- package/src/runs/background/scheduled-runs.ts +51 -18
- package/src/runs/background/stale-run-reconciler.ts +38 -12
- package/src/runs/background/steering.ts +20 -2
- package/src/runs/background/subagent-runner.ts +766 -432
- package/src/runs/background/subagent-wait.ts +179 -28
- package/src/runs/background/wait-completions.ts +79 -27
- package/src/runs/background/wait-subscriptions.ts +9 -3
- package/src/runs/background/wait-tool.ts +5 -3
- package/src/runs/foreground/async-steering-action.ts +37 -7
- package/src/runs/foreground/async-stop-action.ts +93 -3
- package/src/runs/foreground/execution.ts +174 -194
- package/src/runs/foreground/foreground-history.ts +5 -2
- package/src/runs/foreground/prompt-audit.ts +12 -6
- package/src/runs/foreground/subagent-executor.ts +896 -397
- package/src/runs/foreground/workflow-detach-reconcile.ts +10 -5
- package/src/runs/foreground/workflow-foreground-steering.ts +57 -2
- package/src/runs/shared/acceptance.ts +54 -14
- package/src/runs/shared/agent-contract.ts +1 -1
- package/src/runs/shared/async-status-projection.ts +172 -78
- package/src/runs/shared/capability-ceiling.ts +2 -0
- package/src/runs/shared/child-hooks.ts +167 -3
- package/src/runs/shared/child-launch-plan.ts +15 -3
- package/src/runs/shared/child-launch.ts +44 -16
- package/src/runs/shared/child-lifecycle.ts +6 -3
- package/src/runs/shared/child-runtime-config.ts +8 -1
- package/src/runs/shared/child-session.ts +130 -24
- package/src/runs/shared/child-tool-plan.ts +145 -14
- package/src/runs/shared/completion-evidence.ts +2 -2
- package/src/runs/shared/completion-guard.ts +6 -3
- package/src/runs/shared/dynamic-fanout.ts +2 -2
- package/src/runs/shared/effective-system-prompt.ts +33 -0
- package/src/runs/shared/external-cli-contract.ts +11 -1
- package/src/runs/shared/external-cli-preflight.ts +6 -2
- package/src/runs/shared/external-cli-runner.ts +9 -7
- package/src/runs/shared/herdr-connection.ts +134 -0
- package/src/runs/shared/herdr-external-adapters.ts +169 -0
- package/src/runs/shared/herdr-machine.ts +279 -0
- package/src/runs/shared/herdr-pi-protocol.ts +59 -0
- package/src/runs/shared/herdr-placed-run.ts +263 -0
- package/src/runs/shared/host-step-status.ts +11 -11
- package/src/runs/shared/llm-intent-arbiter.ts +40 -21
- package/src/runs/shared/model-resolution-diagnostic.ts +76 -0
- package/src/runs/shared/{model-fallback.ts → model-resolution.ts} +34 -218
- package/src/runs/shared/model-scope.ts +1 -1
- package/src/runs/shared/nested-events.ts +19 -10
- package/src/runs/shared/orca-progress-tabs.ts +7 -1
- package/src/runs/shared/parallel-handoff.ts +57 -12
- package/src/runs/shared/parallel-utils.ts +10 -4
- package/src/runs/shared/pi-spawn.ts +10 -0
- package/src/runs/shared/readonly-drain-observation.ts +42 -0
- package/src/runs/shared/run-fanout-budget.ts +8 -8
- package/src/runs/shared/runtime-acknowledged-extensions.ts +3 -3
- package/src/runs/shared/subagent-prompt-runtime.ts +23 -5
- package/src/runs/shared/task-intent.ts +46 -13
- package/src/runs/shared/workflow-async-child-guidance.ts +18 -0
- package/src/runs/shared/worktree-cleanup-plan.ts +6 -3
- package/src/runs/shared/worktree-setup-command.ts +213 -0
- package/src/runs/shared/worktree.ts +437 -214
- package/src/shared/child-cache-retention.ts +43 -0
- package/src/shared/fork-context.ts +15 -72
- package/src/shared/launch-contract.ts +70 -8
- package/src/shared/model-response-aliases.ts +13 -0
- package/src/shared/opencode-session-headers.ts +30 -0
- package/src/shared/pruned-fork.ts +1 -1
- package/src/shared/required-child-extensions.ts +81 -0
- package/src/shared/settings.ts +5 -2
- package/src/shared/shortcuts.ts +0 -4
- package/src/shared/types.ts +164 -90
- package/src/shared/utils.ts +10 -2
- package/src/shared/watch-strategy.ts +2 -0
- package/src/shared/workflow-child-permit.ts +18 -13
- package/src/slash/delegation-adapters.ts +3 -1
- package/src/slash/delegation-request.ts +14 -0
- package/src/slash/slash-commands.ts +2 -7
- package/src/slash/subagents-admin.ts +31 -14
- package/src/tui/fleet-native-transcript.ts +50 -6
- package/src/tui/fleet-status.ts +165 -20
- package/src/tui/fleet.ts +52 -21
- package/src/tui/render.ts +210 -50
- package/src/watchdog/child-status.ts +28 -28
- package/src/watchdog/lsp-diagnostics.ts +1 -1
- package/src/watchdog/model-selection.ts +21 -1
- package/src/watchdog/permission-arbiter.ts +5 -2
- package/src/watchdog/register-child.ts +10 -2
- package/src/watchdog/register-main.ts +39 -35
- package/src/watchdog/render.ts +1 -1
- package/src/watchdog/review.ts +122 -74
- package/src/watchdog/rules.ts +1 -1
- package/src/watchdog/runtime.ts +100 -27
- package/src/watchdog/scope.ts +1 -1
- package/src/watchdog/settings.ts +3 -0
- package/src/watchdog/tool-actions.ts +13 -12
- package/src/watchdog/turn-delta.ts +23 -0
- package/src/watchdog/types.ts +5 -3
- package/src/watchdog/warning-format.ts +1 -1
- package/src/workflows/chat-progress.ts +3 -3
- package/src/workflows/scripted-workflow.ts +349 -26
- package/src/workflows/workflow-checklist.ts +17 -20
- package/src/workflows/workflow-child-summary.ts +57 -8
- package/src/workflows/workflow-preflight.ts +19 -19
- package/src/workflows/workflow-receipt.ts +24 -6
- package/src/workflows/workflow-resources.ts +108 -22
- package/src/workflows/workflow-settlement.ts +3 -0
- package/src/runs/shared/model-exclusions.ts +0 -373
- /package/src/inspectors/{herdr/shell-command.ts → shell-command.ts} +0 -0
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import { spawnSync } from "node:child_process";
|
|
2
2
|
import * as fs from "node:fs";
|
|
3
3
|
import * as path from "node:path";
|
|
4
|
+
import { performance } from "node:perf_hooks";
|
|
4
5
|
import { pathToFileURL } from "node:url";
|
|
5
6
|
import type { Message } from "@earendil-works/pi-ai";
|
|
7
|
+
import { arbitrateCompletionGuardRescue, createTaskMutationArbiter } from "../shared/llm-intent-arbiter.ts";
|
|
8
|
+
import { installRunnerHttpDispatcher } from "./runner-http-dispatcher.ts";
|
|
9
|
+
|
|
10
|
+
const isRunnerEntrypoint = Boolean(process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href);
|
|
11
|
+
// Detached Node runners skip Pi's CLI dispatcher setup; install the runner's own.
|
|
12
|
+
if (isRunnerEntrypoint) installRunnerHttpDispatcher({ agentDir: getAgentDir(), cwd: process.cwd() });
|
|
6
13
|
import { writeAtomicJson } from "../../shared/atomic-json.ts";
|
|
7
14
|
import { writeAsyncResultFile, writePendingAsyncResultFile } from "./result-files.ts";
|
|
8
15
|
import { createFileCoalescer } from "../../shared/file-coalescer.ts";
|
|
@@ -10,11 +17,12 @@ import { createCapacityResilientJsonWriter } from "../../shared/capacity-resilie
|
|
|
10
17
|
import { isStorageCapacityError } from "../../shared/file-system-retry.ts";
|
|
11
18
|
import { updateActiveRunIndex } from "./active-run-index.ts";
|
|
12
19
|
import { createChildTranscriptWriter, type ChildTranscriptWriter } from "../../shared/child-transcript.ts";
|
|
13
|
-
import { closeSteerInbox, consumeInterruptRequest, consumeSteerRequests, deliverInterruptRequest, deliverStopRequest, deliverTimeoutRequest, watchAsyncControlInbox, type SteerRequest, type StopRequest } from "./control-channel.ts";
|
|
20
|
+
import { closeSteerInbox, consumeInterruptRequest, consumeSteerRequests, consumeStopRequestPayloads, deliverInterruptRequest, deliverStopRequest, deliverTimeoutRequest, watchAsyncControlInbox, type SteerRequest, type StopRequest } from "./control-channel.ts";
|
|
14
21
|
import { appendJsonl as appendRawJsonl, formatOutputArtifactContent, getArtifactPaths, writeArtifact, writeMetadata } from "../../shared/artifacts.ts";
|
|
15
22
|
import { PI_CODING_AGENT_PACKAGE, resolveInstalledPiPackageRoot } from "../shared/pi-spawn.ts";
|
|
16
23
|
import { preflightLaunchCwd } from "../shared/launch-cwd.ts";
|
|
17
24
|
import { captureSingleOutputSnapshot, extractChildWrittenOutput, finalizeSingleOutput, formatSavedOutputReference, injectOutputPathSystemPrompt, injectSingleOutputInstruction, resolveSingleOutput, type SingleOutputSnapshot } from "../shared/single-output.ts";
|
|
25
|
+
import { runSetupCommand } from "../shared/worktree-setup-command.ts";
|
|
18
26
|
import {
|
|
19
27
|
type ActivityState,
|
|
20
28
|
type ArtifactConfig,
|
|
@@ -27,10 +35,9 @@ import {
|
|
|
27
35
|
type AsyncStatus,
|
|
28
36
|
type ChainOutputMap,
|
|
29
37
|
type CostSummary,
|
|
30
|
-
type
|
|
31
|
-
type
|
|
32
|
-
type
|
|
33
|
-
type PiWriterProcessInstanceExitV1,
|
|
38
|
+
type LaunchResolvedChildExtensions,
|
|
39
|
+
type RuntimeAcknowledgedChildExtensions,
|
|
40
|
+
type PiWriterProcessInstanceExit,
|
|
34
41
|
type NestedRouteInfo,
|
|
35
42
|
type NestedRunSummary,
|
|
36
43
|
type ResolvedControlConfig,
|
|
@@ -46,6 +53,7 @@ import {
|
|
|
46
53
|
type SteeringTargetStatus,
|
|
47
54
|
type SubagentChildStatusEvent,
|
|
48
55
|
type WorkflowLaneMetadata,
|
|
56
|
+
type HerdrMachineReference,
|
|
49
57
|
DEFAULT_MAX_OUTPUT,
|
|
50
58
|
type MaxOutputConfig,
|
|
51
59
|
SUBAGENT_LIFECYCLE_ARTIFACT_VERSION,
|
|
@@ -72,10 +80,12 @@ import {
|
|
|
72
80
|
DEFAULT_GLOBAL_CONCURRENCY_LIMIT,
|
|
73
81
|
Semaphore,
|
|
74
82
|
} from "../shared/parallel-utils.ts";
|
|
75
|
-
import { applyThinkingSuffix,
|
|
76
|
-
import {
|
|
77
|
-
import
|
|
78
|
-
import {
|
|
83
|
+
import { applyThinkingSuffix, projectLaunchResolvedChildExtensions, resolvePiLaunchToolPlan } from "../shared/child-tool-plan.ts";
|
|
84
|
+
import type { InheritedChildRuntime } from "../shared/child-launch.ts";
|
|
85
|
+
import { buildRunnerChildLaunch } from "./runner-child-launch.ts";
|
|
86
|
+
import { normalizeExtensionBindings } from "../shared/extension-bindings.ts";
|
|
87
|
+
import type { ChildSessionFactory, DefaultChildSessionFactoryOptions } from "../shared/child-session.ts";
|
|
88
|
+
import { runChildSession, type ChildEvent, type RunChildSessionInput, type RunChildSessionResult, type SteerDelivery, type StepSteerHandler } from "./run-child-session.ts";
|
|
79
89
|
import { loadRunnerChildSessionFactory } from "./runner-child-sessions.ts";
|
|
80
90
|
import { SUBAGENT_CHILD_ENV } from "../shared/child-runtime-config.ts";
|
|
81
91
|
import { deriveChildSessionName } from "../../shared/child-session-name.ts";
|
|
@@ -88,10 +98,10 @@ import { buildTimeoutRecoverySummary, collectTrackedMutationEvidence, snapshotTr
|
|
|
88
98
|
import { collectDynamicResults, DynamicFanoutError, materializeDynamicParallelStep, validateDynamicCollection } from "../shared/dynamic-fanout.ts";
|
|
89
99
|
import { claimRunFanoutBatch, getRunFanoutBudgetSnapshot } from "../shared/run-fanout-budget.ts";
|
|
90
100
|
import { nestedSummaryFromAsyncStatus, projectNestedEvents, resolveNestedAsyncDir, writeNestedEvent } from "../shared/nested-events.ts";
|
|
91
|
-
import {
|
|
92
|
-
import { markProcessTerminalCandidateLeaseRelease, writeProcessTerminalCandidate, type ProcessTerminalCandidate } from "./process-terminal.ts";
|
|
93
|
-
import { createSteeringStatus, recordSteeringRequest, steeringStatus, terminalSteeringNoticeState, updateSteeringTarget } from "./steering.ts";
|
|
94
|
-
import { PROMPT_REDACTED, detectSubagentError, extractTextFromContent, extractToolArgsPreview, formatEmptyTerminalAssistantResponseError, getFinalOutput, hasEmptyTerminalAssistantResponse, readStatus } from "../../shared/utils.ts";
|
|
101
|
+
import { formatSubagentModelVerificationError, isContextOverflow } from "../shared/model-resolution.ts";
|
|
102
|
+
import { markProcessTerminalCandidateLeaseRelease, processTerminalPath, writeProcessTerminalCandidate, type ProcessTerminalCandidate } from "./process-terminal.ts";
|
|
103
|
+
import { createSteeringStatus, recordSteeringRequest, steeringStatus, terminalSteeringNoticeState, unconsumedSteerReason, updateSteeringTarget } from "./steering.ts";
|
|
104
|
+
import { PROMPT_REDACTED, detectSubagentError, extractTextFromContent, extractToolArgsPreview, formatEmptyTerminalAssistantResponseError, getAgentDir, getFinalOutput, hasEmptyTerminalAssistantResponse, readStatus } from "../../shared/utils.ts";
|
|
95
105
|
import { evaluateCompletionMutationGuard, expectsImplementationMutation, hasMutationToolCapability, validateImplementationToolContract } from "../shared/completion-guard.ts";
|
|
96
106
|
import { planCompletionEvidence, projectSettlementDiagnostic } from "../shared/completion-evidence.ts";
|
|
97
107
|
import { planAbortRecovery } from "../shared/abort-recovery.ts";
|
|
@@ -111,6 +121,9 @@ import type { TokenUsage } from "../../shared/types.ts";
|
|
|
111
121
|
import {
|
|
112
122
|
cleanupWorktrees,
|
|
113
123
|
createWorktrees,
|
|
124
|
+
withWorktreeTransaction,
|
|
125
|
+
WorktreeSetupError,
|
|
126
|
+
type WorktreeSetupProgress,
|
|
114
127
|
diffWorktrees,
|
|
115
128
|
findWorktreeTaskCwdConflict,
|
|
116
129
|
formatWorktreeDiffSummary,
|
|
@@ -118,15 +131,14 @@ import {
|
|
|
118
131
|
WORKTREE_AGENT_CWD_PLACEHOLDER,
|
|
119
132
|
type WorktreeSetup,
|
|
120
133
|
} from "../shared/worktree.ts";
|
|
121
|
-
import { findModelInfo, resolveEffectiveThinking } from "../../shared/model-info.ts";
|
|
134
|
+
import { findModelInfo, resolveEffectiveThinking, splitKnownThinkingSuffix } from "../../shared/model-info.ts";
|
|
122
135
|
import { assertThinkingWithinCeiling } from "../../shared/thinking-ceiling.ts";
|
|
123
|
-
import {
|
|
136
|
+
import { resolveLaunchBinding } from "../../shared/launch-contract.ts";
|
|
124
137
|
import { writeInitialProgressFile } from "../../shared/settings.ts";
|
|
125
138
|
import { resolveSubagentIntercomTarget } from "../../intercom/intercom-bridge.ts";
|
|
126
139
|
import { acceptanceFailureMessage, aggregateAcceptanceReport, buildSkippedAcceptanceLedger, evaluateAcceptance, formatAcceptancePrompt, resolveAcceptanceReportMode, resolveEffectiveAcceptance, stripAcceptanceReport } from "../shared/acceptance.ts";
|
|
127
|
-
import { attachContractProjections,
|
|
140
|
+
import { attachContractProjections, isAgentContract } from "../shared/agent-contract.ts";
|
|
128
141
|
import { waitForImportedAsyncRoot } from "./chain-root-attachment.ts";
|
|
129
|
-
import { normalizeExtensionBindings } from "../shared/extension-bindings.ts";
|
|
130
142
|
import { appendRunnerStepsToStatus, consumeChainAppendRequests, countPendingChainAppendRequests, statusStepDescription } from "./chain-append.ts";
|
|
131
143
|
import { asyncStatusChildIdentity } from "../shared/child-identity.ts";
|
|
132
144
|
import { validateSandboxProfileName } from "../../shared/sandbox-profile.ts";
|
|
@@ -134,7 +146,7 @@ import { validatePermissionProfileName } from "../../shared/permission-profile.t
|
|
|
134
146
|
import { initialToolBudgetState, toolBudgetState } from "../shared/tool-budget.ts";
|
|
135
147
|
import { toolTimeoutCallKey } from "../shared/tool-timeout.ts";
|
|
136
148
|
import { usageBudgetExceededMessage, usageBudgetState } from "../shared/usage-budget.ts";
|
|
137
|
-
import { formatParallelHandoffError, formatParallelHandoffReference, parallelHandoffPath, writeParallelHandoffGroup,
|
|
149
|
+
import { formatParallelHandoffError, formatParallelHandoffReference, parallelHandoffPath, writeParallelHandoffGroup, writeWorktreeSetupHandoff } from "../shared/parallel-handoff.ts";
|
|
138
150
|
import { resolveWatchdogConfig } from "../../watchdog/settings.ts";
|
|
139
151
|
import { acquireSessionLease, type SessionLeaseRequest } from "../shared/session-lease.ts";
|
|
140
152
|
import { buildExternalCliPrompt, runExternalCli } from "../shared/external-cli-runner.ts";
|
|
@@ -142,9 +154,10 @@ import { resolveClaudeCodeLaunch } from "../shared/claude-code-adapter.ts";
|
|
|
142
154
|
import { resolveCodexExecLaunch } from "../shared/codex-exec-adapter.ts";
|
|
143
155
|
import { resolveCursorAgentLaunch } from "../shared/cursor-agent-adapter.ts";
|
|
144
156
|
import { resolveExternalCliRunnerStatus } from "../shared/external-cli-contract.ts";
|
|
157
|
+
import { formatHerdrMachineHint, prepareHerdrMachineExternalCliRun } from "../shared/herdr-machine.ts";
|
|
158
|
+
import { HerdrExternalNeedsAttentionError, createHerdrExternalAdapter, prepareInternalHerdrExternalAdapter, type HerdrExternalAdapterId, type HerdrExternalResult } from "../shared/herdr-external-adapters.ts";
|
|
145
159
|
import { runExternalJob } from "../shared/external-job-runner.ts";
|
|
146
160
|
import { createOrcaProgressTab, type OrcaProgressTab } from "../shared/orca-progress-tabs.ts";
|
|
147
|
-
import { clearSandboxStartupDiagnostic, readSandboxStartupDiagnostic } from "../shared/sandbox-startup-diagnostics.ts";
|
|
148
161
|
import type { ResolvedSubagentCapabilityCeiling } from "../shared/capability-ceiling.ts";
|
|
149
162
|
import {
|
|
150
163
|
acceptChildWatchdogEvent,
|
|
@@ -161,7 +174,7 @@ const INTERCOM_DETACH_RECEIPT = "Detached for intercom coordination before task
|
|
|
161
174
|
// child host.
|
|
162
175
|
process.env[SUBAGENT_CHILD_ENV] = "1";
|
|
163
176
|
|
|
164
|
-
interface SubagentRunConfig {
|
|
177
|
+
export interface SubagentRunConfig {
|
|
165
178
|
id: string;
|
|
166
179
|
steps: RunnerStep[];
|
|
167
180
|
resultPath: string;
|
|
@@ -201,6 +214,8 @@ interface SubagentRunConfig {
|
|
|
201
214
|
deadlineAt?: number;
|
|
202
215
|
/** Resolved configured hard per-tool-call timeout (ms); fast tools still have a default when undefined. */
|
|
203
216
|
toolTimeoutMs?: number;
|
|
217
|
+
/** Steer the running steps to checkpoint and stop this many ms before `deadlineAt`; absent = no checkpoint steer. */
|
|
218
|
+
checkpointBeforeDeadlineMs?: number;
|
|
204
219
|
toolBudget?: ResolvedToolBudget;
|
|
205
220
|
usageBudget?: UsageBudgetConfig;
|
|
206
221
|
revivalLease?: SessionLeaseRequest;
|
|
@@ -209,9 +224,11 @@ interface SubagentRunConfig {
|
|
|
209
224
|
globalConcurrencyLimit?: number;
|
|
210
225
|
capabilityCeiling?: ResolvedSubagentCapabilityCeiling;
|
|
211
226
|
runFanoutBudget?: RunFanoutBudgetDescriptor;
|
|
227
|
+
/** Builtin tool names the host runtime provides; used to intersect agent-declared tools. */
|
|
228
|
+
hostAvailableBuiltins?: readonly string[];
|
|
212
229
|
launchContractDigest?: string;
|
|
213
|
-
launchResolvedExtensions?:
|
|
214
|
-
runtimeAcknowledgedExtensions?:
|
|
230
|
+
launchResolvedExtensions?: LaunchResolvedChildExtensions;
|
|
231
|
+
runtimeAcknowledgedExtensions?: RuntimeAcknowledgedChildExtensions;
|
|
215
232
|
runnerProcessInstanceId?: string;
|
|
216
233
|
launchBarrierToken?: string;
|
|
217
234
|
parentWorkflowRunId?: string;
|
|
@@ -228,8 +245,8 @@ interface StepResult {
|
|
|
228
245
|
permissionProfile?: string;
|
|
229
246
|
capabilityCeiling?: ResolvedSubagentCapabilityCeiling;
|
|
230
247
|
capabilityAudit?: import("../shared/capability-ceiling.ts").SubagentCapabilityAudit;
|
|
231
|
-
launchResolvedExtensions?:
|
|
232
|
-
runtimeAcknowledgedExtensions?:
|
|
248
|
+
launchResolvedExtensions?: LaunchResolvedChildExtensions;
|
|
249
|
+
runtimeAcknowledgedExtensions?: RuntimeAcknowledgedChildExtensions;
|
|
233
250
|
output: string;
|
|
234
251
|
outputState?: SubagentOutputState;
|
|
235
252
|
error?: string;
|
|
@@ -249,14 +266,15 @@ interface StepResult {
|
|
|
249
266
|
sessionFile?: string;
|
|
250
267
|
intercomTarget?: string;
|
|
251
268
|
model?: string;
|
|
269
|
+
nativeMachine?: import("../../shared/types.ts").SingleResult["nativeMachine"];
|
|
252
270
|
thinking?: string;
|
|
253
|
-
|
|
254
|
-
modelAttempts?: ModelAttempt[];
|
|
271
|
+
requestedModel?: string;
|
|
255
272
|
/** True when the dispatch failed because the input exceeded the model's context window. */
|
|
256
273
|
contextOverflow?: boolean;
|
|
257
274
|
totalCost?: CostSummary;
|
|
258
275
|
artifactPaths?: ArtifactPaths;
|
|
259
276
|
outputSaveError?: string;
|
|
277
|
+
artifactOutputSaveFailed?: true;
|
|
260
278
|
metadataSaveError?: string;
|
|
261
279
|
truncated?: boolean;
|
|
262
280
|
transcriptPath?: string;
|
|
@@ -419,50 +437,22 @@ function emptyUsage(): Usage {
|
|
|
419
437
|
return { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, turns: 0 };
|
|
420
438
|
}
|
|
421
439
|
|
|
422
|
-
function
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
let output = 0;
|
|
426
|
-
for (const attempt of attempts) {
|
|
427
|
-
input += attempt.usage?.input ?? 0;
|
|
428
|
-
output += attempt.usage?.output ?? 0;
|
|
429
|
-
}
|
|
440
|
+
function tokenUsageFromUsage(usage: Usage | undefined): TokenUsage | null {
|
|
441
|
+
const input = usage?.input ?? 0;
|
|
442
|
+
const output = usage?.output ?? 0;
|
|
430
443
|
const total = input + output;
|
|
431
444
|
return total > 0 ? { input, output, total } : null;
|
|
432
445
|
}
|
|
433
446
|
|
|
434
|
-
function
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
let costUsd = 0;
|
|
439
|
-
for (const attempt of attempts) {
|
|
440
|
-
inputTokens += attempt.usage?.input ?? 0;
|
|
441
|
-
outputTokens += attempt.usage?.output ?? 0;
|
|
442
|
-
costUsd += attempt.usage?.cost ?? 0;
|
|
443
|
-
}
|
|
447
|
+
function costSummaryFromUsage(usage: Usage | undefined): CostSummary | undefined {
|
|
448
|
+
const inputTokens = usage?.input ?? 0;
|
|
449
|
+
const outputTokens = usage?.output ?? 0;
|
|
450
|
+
const costUsd = usage?.cost ?? 0;
|
|
444
451
|
return inputTokens > 0 || outputTokens > 0 || costUsd > 0
|
|
445
452
|
? { inputTokens, outputTokens, costUsd }
|
|
446
453
|
: undefined;
|
|
447
454
|
}
|
|
448
455
|
|
|
449
|
-
function usageFromAttempts(attempts: ModelAttempt[] | undefined): Usage | undefined {
|
|
450
|
-
if (!attempts || attempts.length === 0) return undefined;
|
|
451
|
-
const usage = emptyUsage();
|
|
452
|
-
for (const attempt of attempts) {
|
|
453
|
-
if (!attempt.usage) continue;
|
|
454
|
-
usage.input += attempt.usage.input;
|
|
455
|
-
usage.output += attempt.usage.output;
|
|
456
|
-
usage.cacheRead += attempt.usage.cacheRead;
|
|
457
|
-
usage.cacheWrite += attempt.usage.cacheWrite;
|
|
458
|
-
usage.cost += attempt.usage.cost;
|
|
459
|
-
usage.turns += attempt.usage.turns;
|
|
460
|
-
}
|
|
461
|
-
return usage.input !== 0 || usage.output !== 0 || usage.cacheRead !== 0 || usage.cacheWrite !== 0 || usage.cost !== 0 || usage.turns !== 0
|
|
462
|
-
? usage
|
|
463
|
-
: undefined;
|
|
464
|
-
}
|
|
465
|
-
|
|
466
456
|
function appendRecentStepOutput(step: RunnerStatusStep, lines: string[]): void {
|
|
467
457
|
const nonEmpty = lines.filter((line) => line.trim());
|
|
468
458
|
if (nonEmpty.length === 0) return;
|
|
@@ -656,6 +646,38 @@ function writeRunLog(
|
|
|
656
646
|
write(logPath, lines.join("\n"));
|
|
657
647
|
}
|
|
658
648
|
|
|
649
|
+
function expectedMissingGitEvidence(error: unknown): boolean {
|
|
650
|
+
if (typeof (error as { code?: unknown })?.code !== "number") return false;
|
|
651
|
+
const stderr = (error as { stderr?: unknown }).stderr;
|
|
652
|
+
const detail = Buffer.isBuffer(stderr) ? stderr.toString("utf-8") : String(stderr ?? "");
|
|
653
|
+
return /not a git repository|Needed a single revision/i.test(detail);
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
async function readGitFingerprint(cwd: string, signal: AbortSignal, onError: (error: unknown) => void): Promise<string | undefined> {
|
|
657
|
+
try {
|
|
658
|
+
const runGit = async (args: string[]) => {
|
|
659
|
+
const result = await runSetupCommand("git", args, {
|
|
660
|
+
cwd, signal, maxBuffer: 16 * 1024 * 1024, deadlineAt: Date.now() + 30_000,
|
|
661
|
+
acceptedExitCodes: Array.from({ length: 256 }, (_, code) => code),
|
|
662
|
+
});
|
|
663
|
+
if (result.error) throw result.error;
|
|
664
|
+
if (result.status !== 0) {
|
|
665
|
+
throw Object.assign(new Error(result.stderr || `git exited with ${result.status}`), { code: result.status, stderr: result.stderr });
|
|
666
|
+
}
|
|
667
|
+
return result.stdoutBuffer;
|
|
668
|
+
};
|
|
669
|
+
const headOutput = await runGit(["rev-parse", "--verify", "HEAD"]);
|
|
670
|
+
const head = headOutput.toString("utf-8").trim();
|
|
671
|
+
if (!head) return undefined;
|
|
672
|
+
const status = await runGit(["status", "--porcelain=v1", "-z", "--untracked-files=normal"]);
|
|
673
|
+
return `${head}\0${status.toString("base64")}`;
|
|
674
|
+
} catch (error) {
|
|
675
|
+
if ((error as NodeJS.ErrnoException)?.code === "PROCESS_TREE_UNVERIFIED") throw error;
|
|
676
|
+
if (!signal.aborted && !expectedMissingGitEvidence(error)) onError(error);
|
|
677
|
+
return undefined;
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
|
|
659
681
|
/** Context for running a single step */
|
|
660
682
|
interface SingleStepContext {
|
|
661
683
|
previousOutput: string;
|
|
@@ -681,6 +703,8 @@ interface SingleStepContext {
|
|
|
681
703
|
registerStop?: (stop: (() => void) | undefined) => void;
|
|
682
704
|
/** Receives the live child's steer handler while its session runs. */
|
|
683
705
|
registerSteer?: (steer: StepSteerHandler | undefined) => void;
|
|
706
|
+
/** Reports a live child's later steer consumption or unconsumed settlement. */
|
|
707
|
+
onSteerOutcome?: (request: SteerRequest, delivery: SteerDelivery) => void;
|
|
684
708
|
timeoutSignal?: AbortSignal;
|
|
685
709
|
stopSignal?: AbortSignal;
|
|
686
710
|
timeoutMessage?: string;
|
|
@@ -694,19 +718,43 @@ interface SingleStepContext {
|
|
|
694
718
|
nestedRoute?: NestedRouteInfo;
|
|
695
719
|
capabilityCeiling?: ResolvedSubagentCapabilityCeiling;
|
|
696
720
|
runFanoutBudget?: RunFanoutBudgetDescriptor;
|
|
721
|
+
hostAvailableBuiltins?: readonly string[];
|
|
697
722
|
onAttemptStart?: (attempt: { model?: string; thinking?: string; contextLimit?: number }) => void;
|
|
698
723
|
onChildEvent?: (event: ChildEvent) => void;
|
|
699
724
|
onExternalProcess?: (process: ExternalProcessStatus) => void;
|
|
725
|
+
prepareExternalActivity?: (cwd: string, signal: AbortSignal) => Promise<void>;
|
|
726
|
+
onExternalStreamActivity?: () => void;
|
|
700
727
|
onExternalJob?: (status: ExternalJobStatus) => void;
|
|
701
728
|
skipAcceptance?: () => boolean;
|
|
702
|
-
|
|
729
|
+
/** Authoritative owner decision after event delivery; undefined includes incomplete run-wide usage. */
|
|
730
|
+
usageBudgetExhausted?: () => boolean | undefined;
|
|
731
|
+
/** Existing run-owned budget configuration; cost allowance is not settled by the live token ledger. */
|
|
732
|
+
usageBudget?: UsageBudgetConfig;
|
|
703
733
|
/** False when sibling work in the same Git worktree could have caused the tracked diff. */
|
|
704
734
|
trackedMutationEvidenceForCompletionGuard?: boolean;
|
|
705
735
|
orcaProgressTab?: OrcaProgressTab;
|
|
706
736
|
}
|
|
707
737
|
|
|
708
|
-
/**
|
|
709
|
-
|
|
738
|
+
/** Machine runs: a one-line hint for predictable remote failures, and a note that writer changes live on the machine. */
|
|
739
|
+
function decorateHerdrMachineResult<T extends { output: string; exitCode: number | null; error?: string; externalProcess: { stderrPath: string } }>(result: T, machine: HerdrMachineReference, adapter: string | undefined): T {
|
|
740
|
+
let stderrTail = "";
|
|
741
|
+
try {
|
|
742
|
+
const stderr = fs.readFileSync(result.externalProcess.stderrPath, "utf-8");
|
|
743
|
+
stderrTail = stderr.slice(-4096);
|
|
744
|
+
} catch { /* stderr log is best-effort evidence. */ }
|
|
745
|
+
const hint = result.exitCode === 0 ? undefined : formatHerdrMachineHint(machine, `${result.error ?? ""}\n${stderrTail}\nexit code ${result.exitCode}`);
|
|
746
|
+
const error = hint ? `${result.error ?? `Remote command exited with code ${result.exitCode}.`}\n${hint}` : result.error;
|
|
747
|
+
const note = result.exitCode === 0 && adapter?.endsWith("-writer") ? `Changes made by this run live on ${machine.label ?? machine.id} at ${machine.cwd}; the local checkout is unchanged.` : undefined;
|
|
748
|
+
const output = note ? (result.output.trim() ? `${result.output.trimEnd()}\n\n${note}` : note) : result.output;
|
|
749
|
+
return { ...result, output, ...(error !== undefined ? { error } : {}) };
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
export async function settleHerdrExternalRunnerError(error: unknown, adapter: HerdrExternalAdapterId, evidence: Parameters<ReturnType<typeof createHerdrExternalAdapter>["normalize"]>[0], retain: () => Promise<void>): Promise<HerdrExternalResult> {
|
|
753
|
+
await retain();
|
|
754
|
+
if (!(error instanceof HerdrExternalNeedsAttentionError)) throw error;
|
|
755
|
+
return createHerdrExternalAdapter(adapter).normalize(evidence, error.message);
|
|
756
|
+
}
|
|
757
|
+
export async function runSingleStepInner(
|
|
710
758
|
step: SubagentStep,
|
|
711
759
|
ctx: SingleStepContext,
|
|
712
760
|
): Promise<StepResult & { completionGuardTriggered?: boolean }> {
|
|
@@ -764,8 +812,7 @@ async function runSingleStepInner(
|
|
|
764
812
|
sessionFile: imported.sessionFile,
|
|
765
813
|
intercomTarget: imported.intercomTarget,
|
|
766
814
|
model: imported.model,
|
|
767
|
-
|
|
768
|
-
modelAttempts: imported.modelAttempts,
|
|
815
|
+
requestedModel: imported.requestedModel,
|
|
769
816
|
contextOverflow: imported.contextOverflow,
|
|
770
817
|
totalCost: imported.totalCost,
|
|
771
818
|
usage: imported.usage,
|
|
@@ -799,16 +846,15 @@ async function runSingleStepInner(
|
|
|
799
846
|
subagentOnlyExtensions: step.subagentOnlyExtensions,
|
|
800
847
|
fast: step.fast,
|
|
801
848
|
model: step.model,
|
|
802
|
-
modelCandidates: step.modelCandidates,
|
|
803
849
|
mcpDirectTools: step.mcpDirectTools,
|
|
804
850
|
cwd: step.cwd ?? ctx.cwd,
|
|
805
851
|
requireReadTool: Boolean(step.skills?.length),
|
|
806
852
|
structuredOutput: Boolean(effectiveStructuredOutput),
|
|
807
853
|
capabilityCeiling: step.capabilityCeiling ?? ctx.capabilityCeiling,
|
|
808
854
|
inheritedCapabilityCeiling: ctx.inheritedChildRuntime?.capabilityCeiling,
|
|
855
|
+
requiredExtensions: step.requiredExtensions ?? ctx.inheritedChildRuntime?.requiredExtensions,
|
|
809
856
|
permissionRules: step.permissionRules,
|
|
810
|
-
|
|
811
|
-
permissionProfile: step.permissionProfile,
|
|
857
|
+
hostAvailableBuiltins: ctx.hostAvailableBuiltins,
|
|
812
858
|
}));
|
|
813
859
|
const contractTools = resolvedTaskToolPlan.explicitToolAllowlist ? resolvedTaskToolPlan.effectiveToolAllowlist : undefined;
|
|
814
860
|
const contractError = validateImplementationToolContract({
|
|
@@ -836,8 +882,8 @@ async function runSingleStepInner(
|
|
|
836
882
|
// Derive from the pre-acceptance task so internal acceptance/recovery
|
|
837
883
|
// instructions never leak into the display name.
|
|
838
884
|
const childSessionName = step.sessionName ?? deriveChildSessionName({ agent: step.agent, task, label: step.label });
|
|
839
|
-
if (step.effectiveAcceptance) {
|
|
840
|
-
const acceptancePrompt = formatAcceptancePrompt(step.effectiveAcceptance, { reportOptional:
|
|
885
|
+
if (step.effectiveAcceptance && (step.runner?.type === "external-cli" || step.runner?.type === "external-job")) {
|
|
886
|
+
const acceptancePrompt = formatAcceptancePrompt(step.effectiveAcceptance, { reportOptional: isAgentContract(step.agentContract), structuredOutput: Boolean(step.structuredOutput?.acceptanceReportPath) });
|
|
841
887
|
if (acceptancePrompt) task = `${task}\n${acceptancePrompt}`;
|
|
842
888
|
}
|
|
843
889
|
const sessionEnabled = Boolean(step.sessionFile) || ctx.sessionEnabled;
|
|
@@ -866,7 +912,53 @@ async function runSingleStepInner(
|
|
|
866
912
|
transcriptWriter?.writeInitialUserMessage(`${PROMPT_REDACTED}; live Prompt Audit only.`);
|
|
867
913
|
|
|
868
914
|
if (step.runner?.type === "external-cli") {
|
|
869
|
-
const externalCwd = step.cwd ?? ctx.cwd;
|
|
915
|
+
const externalCwd = step.machine?.cwd ?? step.cwd ?? ctx.cwd;
|
|
916
|
+
const externalAbortSignal = combinedAbortSignal([ctx.timeoutSignal, ctx.stopSignal]);
|
|
917
|
+
if (!step.machine && externalAbortSignal) await ctx.prepareExternalActivity?.(externalCwd, externalAbortSignal);
|
|
918
|
+
if (externalAbortSignal?.aborted) {
|
|
919
|
+
const stopped = ctx.stopSignal?.aborted === true;
|
|
920
|
+
const message = stopped ? ctx.stopMessage ?? "Subagent stopped by user." : ctx.timeoutMessage ?? "Subagent timed out.";
|
|
921
|
+
return omitUndefinedProperties({ agent: step.agent, context: step.context, output: message, error: message, exitCode: 1, stopped: stopped || undefined, timedOut: stopped ? undefined : true });
|
|
922
|
+
}
|
|
923
|
+
if (step.machine) {
|
|
924
|
+
const runner = resolveExternalCliRunnerStatus({ ...step.runner, machine: step.machine });
|
|
925
|
+
const outputSnapshot = captureSingleOutputSnapshot(step.outputPath);
|
|
926
|
+
const placedRunId = `${ctx.id}-${ctx.flatIndex}`;
|
|
927
|
+
const session = await prepareInternalHerdrExternalAdapter({ adapter: step.runner.adapter as HerdrExternalAdapterId, machine: step.machine, runId: placedRunId }, {});
|
|
928
|
+
let timedOut = false, stopped = false, retain = false;
|
|
929
|
+
const evidenceInput = { runId: placedRunId, requestId: `prompt-${ctx.flatIndex}`, task: buildExternalCliPrompt(step.systemPrompt ?? "", task), cwd: externalCwd, nativeSessionId: session.launch.nativeSessionId };
|
|
930
|
+
let resolveInterruption!: (value: "timeout" | "stop") => void, rejectInterruption!: (error: unknown) => void;
|
|
931
|
+
const interruption = new Promise<"timeout" | "stop">((resolve, reject) => { resolveInterruption = resolve; rejectInterruption = reject; });
|
|
932
|
+
ctx.registerTimeout?.(() => { timedOut = true; retain = true; resolveInterruption("timeout"); });
|
|
933
|
+
ctx.registerStop?.(() => { stopped = true; void session.abort().then(() => resolveInterruption("stop"), rejectInterruption); });
|
|
934
|
+
try {
|
|
935
|
+
let settled: HerdrExternalResult;
|
|
936
|
+
try {
|
|
937
|
+
const raced = await Promise.race([session.promptAndSettle(evidenceInput).then((value) => ({ kind: "settled" as const, value })), interruption.then((kind) => ({ kind }))]);
|
|
938
|
+
if (raced.kind === "settled") settled = raced.value;
|
|
939
|
+
else if (raced.kind === "timeout") settled = createHerdrExternalAdapter(step.runner.adapter as HerdrExternalAdapterId).normalize(evidenceInput, "Placed external run timed out; truthful pane retained for inspection.");
|
|
940
|
+
else return { agent: step.agent, context: step.context, output: "Placed external run stopped by user.", outputState: "present", exitCode: 1, stopped: true, runner, execution: { status: "stopped", success: false, exitCode: 1, stopped: true } };
|
|
941
|
+
} catch (error) {
|
|
942
|
+
retain = true;
|
|
943
|
+
settled = await settleHerdrExternalRunnerError(error, step.runner.adapter as HerdrExternalAdapterId, evidenceInput, () => session.retain());
|
|
944
|
+
}
|
|
945
|
+
const output = settled.output;
|
|
946
|
+
try { fs.writeFileSync(ctx.outputFile, output, "utf-8"); } catch { /* Observability output is best-effort. */ }
|
|
947
|
+
const resolvedOutput = step.outputPath ? resolveSingleOutput(step.outputPath, output, outputSnapshot, step.outputClaimPath) : { fullOutput: output };
|
|
948
|
+
const outputReference = resolvedOutput.savedPath ? formatSavedOutputReference(resolvedOutput.savedPath, resolvedOutput.fullOutput) : undefined;
|
|
949
|
+
const finalizedOutput = finalizeSingleOutput(omitUndefinedProperties({ fullOutput: resolvedOutput.fullOutput, outputPath: step.outputPath, outputMode: step.outputMode, exitCode: 1, preserveSavedOutput: true, savedPath: resolvedOutput.savedPath, outputReference, saveError: resolvedOutput.saveError }));
|
|
950
|
+
const artifactErrors = artifactPaths && ctx.artifactConfig?.enabled !== false ? persistStepArtifacts({ artifactPaths, artifactConfig: ctx.artifactConfig, output: formatOutputArtifactContent(omitUndefinedProperties({ output: resolvedOutput.fullOutput, metadataPath: ctx.artifactConfig?.includeMetadata === false ? undefined : artifactPaths.metadataPath })), metadata: { runId: ctx.id, agent: step.agent, task: PROMPT_REDACTED, runner, placement: session.owner.identity, settlement: settled.settlement, outcome: settled.outcome, timestamp: Date.now() } }) : {};
|
|
951
|
+
return omitUndefinedProperties({ agent: step.agent, ...(childSessionName ? { sessionName: childSessionName } : {}), context: step.context, output: finalizedOutput.displayOutput, outputState: output.trim() ? "present" : "absent", exitCode: 1, error: resolvedOutput.fatalError ? resolvedOutput.saveError : undefined, timedOut, stopped, artifactPaths, outputSaveError: [resolvedOutput.saveError, artifactErrors.outputSaveError].filter(Boolean).join("\n") || undefined, metadataSaveError: artifactErrors.metadataSaveError, runner, execution: { status: "partial", success: false, exitCode: 1 } });
|
|
952
|
+
} catch (error) {
|
|
953
|
+
// Any post-allocation uncertainty retains the pane; only explicit stop
|
|
954
|
+
// and successful exact settlement are destructive.
|
|
955
|
+
retain = true;
|
|
956
|
+
throw error;
|
|
957
|
+
} finally {
|
|
958
|
+
ctx.registerTimeout?.(undefined); ctx.registerStop?.(undefined);
|
|
959
|
+
if (retain) await session.retain(); else await session.dispose();
|
|
960
|
+
}
|
|
961
|
+
}
|
|
870
962
|
const adapterLaunch = step.runner.adapter === "codex-exec" || step.runner.adapter === "codex-exec-writer"
|
|
871
963
|
? resolveCodexExecLaunch({ adapter: step.runner.adapter, command: step.runner.command, asyncDir: path.dirname(ctx.outputFile), stepIndex: ctx.flatIndex })
|
|
872
964
|
: step.runner.adapter === "claude-code" || step.runner.adapter === "claude-code-writer"
|
|
@@ -874,9 +966,13 @@ async function runSingleStepInner(
|
|
|
874
966
|
: step.runner.adapter === "cursor-agent" || step.runner.adapter === "cursor-agent-writer"
|
|
875
967
|
? resolveCursorAgentLaunch({ adapter: step.runner.adapter, command: step.runner.command, cwd: externalCwd, asyncDir: path.dirname(ctx.outputFile), stepIndex: ctx.flatIndex })
|
|
876
968
|
: undefined;
|
|
877
|
-
const runner = resolveExternalCliRunnerStatus({ ...step.runner, ...(adapterLaunch ? { args: adapterLaunch.args } : {}) });
|
|
969
|
+
const runner = resolveExternalCliRunnerStatus({ ...step.runner, ...(adapterLaunch ? { args: adapterLaunch.args } : {}), ...(step.machine ? { machine: step.machine } : {}) });
|
|
878
970
|
const outputSnapshot = captureSingleOutputSnapshot(step.outputPath);
|
|
879
|
-
const
|
|
971
|
+
const onExternalOutput = (chunk: Buffer): void => {
|
|
972
|
+
if (chunk.length > 0) ctx.onExternalStreamActivity?.();
|
|
973
|
+
ctx.orcaProgressTab?.append(chunk.toString("utf-8"));
|
|
974
|
+
};
|
|
975
|
+
const externalInput = omitUndefinedProperties({
|
|
880
976
|
command: adapterLaunch?.command ?? runner.command,
|
|
881
977
|
args: adapterLaunch?.args ?? runner.args,
|
|
882
978
|
cwd: externalCwd,
|
|
@@ -894,9 +990,13 @@ async function runSingleStepInner(
|
|
|
894
990
|
timeoutMessage: ctx.timeoutMessage,
|
|
895
991
|
stopMessage: ctx.stopMessage,
|
|
896
992
|
onProcess: ctx.onExternalProcess,
|
|
897
|
-
onStdout:
|
|
898
|
-
onStderr:
|
|
899
|
-
})
|
|
993
|
+
onStdout: onExternalOutput,
|
|
994
|
+
onStderr: onExternalOutput,
|
|
995
|
+
});
|
|
996
|
+
const preparedExternal = prepareHerdrMachineExternalCliRun(externalInput, step.machine ? { machine: step.machine, ...(step.machineEnv ? { env: step.machineEnv } : {}) } : undefined, { localCwd: ctx.cwd });
|
|
997
|
+
const ran = await runExternalCli(preparedExternal.input);
|
|
998
|
+
const externalProcess = preparedExternal.decorateProcess(ran.externalProcess);
|
|
999
|
+
const external = step.machine ? decorateHerdrMachineResult(ran, step.machine, step.runner.adapter) : ran;
|
|
900
1000
|
try { fs.writeFileSync(ctx.outputFile, external.output, "utf-8"); } catch { /* Observability output is best-effort. */ }
|
|
901
1001
|
const resolvedOutput = step.outputPath && external.exitCode === 0
|
|
902
1002
|
? resolveSingleOutput(step.outputPath, external.output, outputSnapshot, step.outputClaimPath)
|
|
@@ -920,7 +1020,7 @@ async function runSingleStepInner(
|
|
|
920
1020
|
artifactPaths,
|
|
921
1021
|
artifactConfig: ctx.artifactConfig,
|
|
922
1022
|
output: formatOutputArtifactContent(omitUndefinedProperties({ output: resolvedOutput.fullOutput, error: external.error, metadataPath: ctx.artifactConfig?.includeMetadata === false ? undefined : artifactPaths.metadataPath })),
|
|
923
|
-
metadata: { runId: ctx.id, agent: step.agent, task: PROMPT_REDACTED, runner, externalProcess:
|
|
1023
|
+
metadata: { runId: ctx.id, agent: step.agent, task: PROMPT_REDACTED, runner, externalProcess: externalProcess, exitCode: external.exitCode, error: external.error, timestamp: Date.now() },
|
|
924
1024
|
})
|
|
925
1025
|
: {};
|
|
926
1026
|
return omitUndefinedProperties({
|
|
@@ -936,9 +1036,10 @@ async function runSingleStepInner(
|
|
|
936
1036
|
processSignal: external.processSignal,
|
|
937
1037
|
artifactPaths,
|
|
938
1038
|
outputSaveError: [resolvedOutput.saveError, artifactErrors.outputSaveError].filter(Boolean).join("\n") || undefined,
|
|
1039
|
+
artifactOutputSaveFailed: artifactErrors.outputSaveError ? true : undefined,
|
|
939
1040
|
metadataSaveError: artifactErrors.metadataSaveError,
|
|
940
1041
|
runner,
|
|
941
|
-
externalProcess:
|
|
1042
|
+
externalProcess: externalProcess,
|
|
942
1043
|
});
|
|
943
1044
|
}
|
|
944
1045
|
|
|
@@ -1005,6 +1106,7 @@ async function runSingleStepInner(
|
|
|
1005
1106
|
stopped: external.stopped,
|
|
1006
1107
|
artifactPaths,
|
|
1007
1108
|
outputSaveError: [resolvedOutput.saveError, artifactErrors.outputSaveError].filter(Boolean).join("\n") || undefined,
|
|
1109
|
+
artifactOutputSaveFailed: artifactErrors.outputSaveError ? true : undefined,
|
|
1008
1110
|
metadataSaveError: artifactErrors.metadataSaveError,
|
|
1009
1111
|
runner,
|
|
1010
1112
|
externalJob: external.externalJob,
|
|
@@ -1018,16 +1120,9 @@ async function runSingleStepInner(
|
|
|
1018
1120
|
alignForkedSessionCwd(step.sessionFile, effectiveCwd);
|
|
1019
1121
|
}
|
|
1020
1122
|
|
|
1021
|
-
const
|
|
1022
|
-
? step.modelCandidates.length > 0 ? step.modelCandidates : [undefined]
|
|
1023
|
-
: step.model
|
|
1024
|
-
? [step.model]
|
|
1025
|
-
: [undefined];
|
|
1026
|
-
const attemptedModels: string[] = [];
|
|
1123
|
+
const candidate = step.model;
|
|
1027
1124
|
let capabilityAudit: import("../shared/capability-ceiling.ts").SubagentCapabilityAudit | undefined;
|
|
1028
1125
|
let launchResolvedExtensions = step.launchResolvedExtensions;
|
|
1029
|
-
const modelAttempts: ModelAttempt[] = [];
|
|
1030
|
-
const attemptNotes: string[] = [];
|
|
1031
1126
|
let finalRequiredOutputMissing: boolean | undefined;
|
|
1032
1127
|
const eventsPath = path.join(path.dirname(ctx.outputFile), "events.jsonl");
|
|
1033
1128
|
let finalResult: RunChildSessionResult | undefined;
|
|
@@ -1038,20 +1133,17 @@ async function runSingleStepInner(
|
|
|
1038
1133
|
let toolBudget = step.toolBudget ? initialToolBudgetState(step.toolBudget) : undefined;
|
|
1039
1134
|
let toolBudgetBlocked = false;
|
|
1040
1135
|
let actualLaunchContractDigest = step.launchContractDigest;
|
|
1041
|
-
const mutationSnapshot = snapshotTrackedMutations(step.cwd ?? ctx.cwd);
|
|
1136
|
+
const mutationSnapshot = step.machine ? { source: "tracked-files" as const, trackedOnly: true as const, cwd: step.cwd ?? ctx.cwd, dirtyFiles: [], fingerprints: {}, unavailable: "Local Git evidence is not authoritative for a pane-native remote run." } : snapshotTrackedMutations(step.cwd ?? ctx.cwd);
|
|
1042
1137
|
let finalMutationEvidence = collectTrackedMutationEvidence(mutationSnapshot, step.cwd ?? ctx.cwd);
|
|
1043
1138
|
|
|
1044
|
-
let modelIndex = 0;
|
|
1045
1139
|
let contextOverflow = false;
|
|
1046
1140
|
let launchWarningsEmitted = false;
|
|
1047
|
-
|
|
1048
|
-
let
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
const
|
|
1053
|
-
const candidate = candidates[modelIndex];
|
|
1054
|
-
const expectedModelForVerification = candidate && !(step.skipPrimaryModelVerification && modelIndex === 0) ? candidate : undefined;
|
|
1141
|
+
const aggregateUsage = emptyUsage();
|
|
1142
|
+
let launched = false;
|
|
1143
|
+
let recoveryTask = task;
|
|
1144
|
+
singleLaunch: for (let attemptIndex = 0; attemptIndex < 2; attemptIndex++) {
|
|
1145
|
+
if (ctx.timeoutSignal?.aborted || ctx.stopSignal?.aborted || ctx.skipAcceptance?.()) break singleLaunch;
|
|
1146
|
+
const expectedModelForVerification = candidate && !step.skipPrimaryModelVerification ? candidate : undefined;
|
|
1055
1147
|
try {
|
|
1056
1148
|
assertThinkingWithinCeiling({ model: candidate, configThinking: step.thinking, ceiling: step.thinkingCeiling, agent: step.agent, runId: ctx.id });
|
|
1057
1149
|
} catch (error) {
|
|
@@ -1081,60 +1173,18 @@ async function runSingleStepInner(
|
|
|
1081
1173
|
})
|
|
1082
1174
|
: undefined;
|
|
1083
1175
|
let watchdogSink: ((event: ChildWatchdogStatusEvent) => void) | undefined;
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
excludeTools: step.excludeTools,
|
|
1097
|
-
allowNestedSubagents: step.allowNestedSubagents,
|
|
1098
|
-
extensions: step.extensions,
|
|
1099
|
-
subagentOnlyExtensions: step.subagentOnlyExtensions,
|
|
1100
|
-
fast: step.fast,
|
|
1101
|
-
modelCandidates: step.modelCandidates,
|
|
1102
|
-
systemPrompt: step.systemPrompt ?? "",
|
|
1103
|
-
systemPromptMode: step.systemPromptMode,
|
|
1104
|
-
mcpDirectTools: step.mcpDirectTools,
|
|
1105
|
-
extensionBindings,
|
|
1106
|
-
capabilityCeiling: step.capabilityCeiling ?? ctx.capabilityCeiling,
|
|
1107
|
-
cwd: step.cwd ?? ctx.cwd,
|
|
1108
|
-
intercomSessionName: ctx.childIntercomTarget,
|
|
1109
|
-
sessionName: childSessionName,
|
|
1110
|
-
orchestratorIntercomTarget: ctx.orchestratorIntercomTarget,
|
|
1111
|
-
runId: ctx.id,
|
|
1112
|
-
childAgentName: step.agent,
|
|
1113
|
-
childIndex: ctx.flatIndex,
|
|
1114
|
-
nestedRoute: ctx.nestedRoute,
|
|
1115
|
-
runFanoutBudget: ctx.runFanoutBudget ? {
|
|
1116
|
-
...ctx.runFanoutBudget,
|
|
1117
|
-
...(step.runFanoutPath ? { parentPath: `${ctx.runFanoutBudget.parentPath ? `${ctx.runFanoutBudget.parentPath}/` : ""}${step.runFanoutPath}` } : {}),
|
|
1118
|
-
} : undefined,
|
|
1119
|
-
structuredOutput: effectiveStructuredOutput,
|
|
1120
|
-
toolBudget: step.toolBudget,
|
|
1121
|
-
permissionRules: step.permissionRules,
|
|
1122
|
-
projectTrusted: step.projectTrusted,
|
|
1123
|
-
trustedProjectCwd: step.trustedProjectCwd,
|
|
1124
|
-
sandbox: step.sandbox,
|
|
1125
|
-
permissionProfile: step.permissionProfile,
|
|
1126
|
-
permissionAuditPath: step.permissionRules && ctx.artifactsDir
|
|
1127
|
-
? path.join(ctx.artifactsDir, "permission-audit", `${ctx.id}-${ctx.flatIndex}.jsonl`)
|
|
1128
|
-
: undefined,
|
|
1129
|
-
childWatchdog,
|
|
1130
|
-
watchdogStatus: (event) => watchdogSink?.(event),
|
|
1131
|
-
waitToolEnabled: step.waitToolEnabled,
|
|
1132
|
-
waitToolDefaultTimeoutMs: step.waitToolDefaultTimeoutMs,
|
|
1133
|
-
thinkingCeiling: step.thinkingCeiling,
|
|
1134
|
-
maxSubagentDepth: step.maxSubagentDepth,
|
|
1135
|
-
inherited: ctx.inheritedChildRuntime,
|
|
1136
|
-
host: "runner",
|
|
1137
|
-
}));
|
|
1176
|
+
let launch: ReturnType<typeof buildRunnerChildLaunch>;
|
|
1177
|
+
try {
|
|
1178
|
+
launch = buildRunnerChildLaunch(step, ctx, {
|
|
1179
|
+
sessionEnabled,
|
|
1180
|
+
sessionDir,
|
|
1181
|
+
model: candidate,
|
|
1182
|
+
sessionName: childSessionName,
|
|
1183
|
+
structuredOutput: effectiveStructuredOutput,
|
|
1184
|
+
childWatchdog,
|
|
1185
|
+
watchdogStatus: (event) => watchdogSink?.(event),
|
|
1186
|
+
});
|
|
1187
|
+
} catch (error) { throw error; }
|
|
1138
1188
|
if (effectiveStructuredOutput && launch.config.structuredOutput) {
|
|
1139
1189
|
// The runner reads the value back from the runtime's files after the run.
|
|
1140
1190
|
launch.config.structuredOutput.capture = createStructuredOutputFileCapture(effectiveStructuredOutput);
|
|
@@ -1153,43 +1203,35 @@ async function runSingleStepInner(
|
|
|
1153
1203
|
subagentOnlyExtensions: step.subagentOnlyExtensions,
|
|
1154
1204
|
fast: step.fast,
|
|
1155
1205
|
model: step.model,
|
|
1156
|
-
modelCandidates: step.modelCandidates,
|
|
1157
1206
|
mcpDirectTools: step.mcpDirectTools,
|
|
1158
1207
|
cwd: step.cwd ?? ctx.cwd,
|
|
1159
1208
|
requireReadTool: Boolean(step.skills?.length),
|
|
1160
1209
|
structuredOutput: Boolean(effectiveStructuredOutput),
|
|
1161
1210
|
capabilityCeiling: step.capabilityCeiling ?? ctx.capabilityCeiling,
|
|
1162
1211
|
inheritedCapabilityCeiling: ctx.inheritedChildRuntime?.capabilityCeiling,
|
|
1212
|
+
requiredExtensions: step.requiredExtensions ?? ctx.inheritedChildRuntime?.requiredExtensions,
|
|
1163
1213
|
permissionRules: step.permissionRules,
|
|
1164
|
-
|
|
1165
|
-
permissionProfile: step.permissionProfile,
|
|
1214
|
+
hostAvailableBuiltins: ctx.hostAvailableBuiltins,
|
|
1166
1215
|
}));
|
|
1167
1216
|
launchResolvedExtensions = projectLaunchResolvedChildExtensions(toolPlan);
|
|
1168
|
-
actualLaunchContractDigest =
|
|
1217
|
+
actualLaunchContractDigest = resolveLaunchBinding({
|
|
1169
1218
|
definitionDigest: step.definitionDigest,
|
|
1170
|
-
task: step.launchBindingTask ?? task,
|
|
1171
|
-
...(candidate ? { model: candidate } : {}),
|
|
1172
|
-
modelCandidates: candidates as string[],
|
|
1173
|
-
...(step.fast !== undefined ? { fast: step.fast } : {}),
|
|
1174
|
-
...(resolveEffectiveThinking(candidate, step.thinking) ? { thinking: resolveEffectiveThinking(candidate, step.thinking) } : {}),
|
|
1175
|
-
...(step.thinkingCeiling ? { thinkingCeiling: step.thinkingCeiling } : {}),
|
|
1176
|
-
systemPrompt: step.systemPrompt ?? "",
|
|
1177
1219
|
systemPromptMode: step.systemPromptMode,
|
|
1178
1220
|
inheritProjectContext: step.inheritProjectContext,
|
|
1179
1221
|
inheritGlobalContext: step.inheritGlobalContext,
|
|
1180
1222
|
inheritSkills: step.inheritSkills,
|
|
1181
|
-
|
|
1182
|
-
|
|
1223
|
+
task: step.launchBindingTask ?? task,
|
|
1224
|
+
model: candidate,
|
|
1225
|
+
fast: step.fast,
|
|
1226
|
+
thinking: resolveEffectiveThinking(candidate, step.thinking),
|
|
1227
|
+
systemPrompt: step.systemPrompt ?? "",
|
|
1183
1228
|
skills: step.skills,
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
...(step.structuredOutputSchema ? { structuredOutputSchema: step.structuredOutputSchema } : {}),
|
|
1191
|
-
...(extensionBindings ? { extensionBindings } : {}),
|
|
1192
|
-
}));
|
|
1229
|
+
toolPlan,
|
|
1230
|
+
outputPath: step.outputPath,
|
|
1231
|
+
outputMode: step.outputMode,
|
|
1232
|
+
structuredOutputSchema: step.structuredOutputSchema,
|
|
1233
|
+
extensionBindings,
|
|
1234
|
+
}).launchContractDigest;
|
|
1193
1235
|
}
|
|
1194
1236
|
capabilityAudit = attemptCapabilityAudit;
|
|
1195
1237
|
// Each attempt rewrites the step output log; synchronous appends keep a
|
|
@@ -1198,7 +1240,7 @@ async function runSingleStepInner(
|
|
|
1198
1240
|
const run = await runChildSession(omitUndefinedProperties({
|
|
1199
1241
|
factory: ctx.childSessions,
|
|
1200
1242
|
launch,
|
|
1201
|
-
prompt: `Task: ${
|
|
1243
|
+
prompt: `Task: ${recoveryTask}`,
|
|
1202
1244
|
childWatchdog,
|
|
1203
1245
|
childEventContext: { runId: ctx.id, stepIndex: ctx.flatIndex, agent: step.agent },
|
|
1204
1246
|
appendChildEvent: (event) => appendDiagnosticJsonl(eventsPath, JSON.stringify(event), typeof event.type === "string" ? event.type : undefined),
|
|
@@ -1214,6 +1256,7 @@ async function runSingleStepInner(
|
|
|
1214
1256
|
registerTimeout: ctx.registerTimeout,
|
|
1215
1257
|
registerStop: ctx.registerStop,
|
|
1216
1258
|
registerSteer: ctx.registerSteer,
|
|
1259
|
+
onSteerOutcome: ctx.onSteerOutcome,
|
|
1217
1260
|
registerWatchdogStatus: (sink) => { watchdogSink = sink; },
|
|
1218
1261
|
timeoutMessage: ctx.timeoutMessage,
|
|
1219
1262
|
stopMessage: ctx.stopMessage,
|
|
@@ -1223,8 +1266,19 @@ async function runSingleStepInner(
|
|
|
1223
1266
|
runDeadlineAt: ctx.deadlineAt,
|
|
1224
1267
|
expectedModelForVerification,
|
|
1225
1268
|
modelVerificationRegistry: step.modelVerificationRegistry,
|
|
1269
|
+
modelResponseAliases: step.modelResponseAliases,
|
|
1226
1270
|
mutationTools: step.mutationTools,
|
|
1227
1271
|
}));
|
|
1272
|
+
launched = true;
|
|
1273
|
+
aggregateUsage.input += run.usage.input;
|
|
1274
|
+
aggregateUsage.output += run.usage.output;
|
|
1275
|
+
aggregateUsage.cacheRead += run.usage.cacheRead;
|
|
1276
|
+
aggregateUsage.cacheWrite += run.usage.cacheWrite;
|
|
1277
|
+
aggregateUsage.cost += run.usage.cost;
|
|
1278
|
+
aggregateUsage.turns += run.usage.turns;
|
|
1279
|
+
// A parked run still owes completion evidence when it actually finishes.
|
|
1280
|
+
// Stopped/timedOut runs already have terminal failures; checking completion evidence there is meaningless.
|
|
1281
|
+
const completionDiagnosticsEligible = !run.interrupted && !run.stopped && !run.timedOut;
|
|
1228
1282
|
const toolDiagnostic = run.exitCode === 0 && !run.error ? launch.capture.toolDiagnostic() : undefined;
|
|
1229
1283
|
const toolAvailabilityError = toolDiagnostic ? formatChildToolDiagnostic(toolDiagnostic) : undefined;
|
|
1230
1284
|
const runtimeAcknowledgedExtensions = launch.capture.runtimeAcknowledgedExtensions();
|
|
@@ -1240,7 +1294,7 @@ async function runSingleStepInner(
|
|
|
1240
1294
|
let structuredOutput: unknown;
|
|
1241
1295
|
let structuredError: string | undefined;
|
|
1242
1296
|
let validatedStructuredOutput = false;
|
|
1243
|
-
if (effectiveStructuredOutput && run.exitCode === 0 && !run.error && !toolAvailabilityError && !midToolExitError) {
|
|
1297
|
+
if (completionDiagnosticsEligible && effectiveStructuredOutput && run.exitCode === 0 && !run.error && !toolAvailabilityError && !midToolExitError) {
|
|
1244
1298
|
if (!run.structuredOutputToolInvoked) {
|
|
1245
1299
|
structuredError = MISSING_STRUCTURED_OUTPUT_CALL_ERROR;
|
|
1246
1300
|
} else {
|
|
@@ -1262,33 +1316,29 @@ async function runSingleStepInner(
|
|
|
1262
1316
|
const errorMessages = validatedStructuredOutput
|
|
1263
1317
|
? run.messages.slice(run.structuredOutputMessageStartIndex ?? run.messages.length)
|
|
1264
1318
|
: run.messages;
|
|
1265
|
-
const hiddenError = run.exitCode === 0 && !run.error && !toolAvailabilityError && !structuredError && !midToolExitError
|
|
1319
|
+
const hiddenError = completionDiagnosticsEligible && run.exitCode === 0 && !run.error && !toolAvailabilityError && !structuredError && !midToolExitError
|
|
1266
1320
|
? detectSubagentError(errorMessages)
|
|
1267
1321
|
: null;
|
|
1268
1322
|
const terminalEmptyAfterUsefulWork = !validatedStructuredOutput
|
|
1269
1323
|
&& hasEmptyTerminalAssistantResponse(run.messages)
|
|
1270
1324
|
&& (run.toolCount > 0 || Boolean(run.finalOutput.trim()));
|
|
1271
|
-
|
|
1272
|
-
// inspect; prefer the reason the blocked child published for the launcher.
|
|
1273
|
-
const sandboxStartupDiagnostic = readSandboxStartupDiagnostic(launch.sandboxDiagnosticsPath);
|
|
1274
|
-
const emptyOutputError = run.exitCode === 0
|
|
1325
|
+
const emptyOutputError = completionDiagnosticsEligible && run.exitCode === 0
|
|
1275
1326
|
&& !run.error
|
|
1276
1327
|
&& !toolAvailabilityError
|
|
1277
1328
|
&& !structuredError
|
|
1278
1329
|
&& !validatedStructuredOutput
|
|
1279
1330
|
&& (!run.finalOutput.trim() || terminalEmptyAfterUsefulWork)
|
|
1280
1331
|
&& (!hiddenError?.hasError || hasEmptyTerminalAssistantResponse(run.messages))
|
|
1281
|
-
?
|
|
1332
|
+
? formatEmptyTerminalAssistantResponseError(run.messages)
|
|
1282
1333
|
: undefined;
|
|
1283
|
-
const
|
|
1284
|
-
if (startupBlocked) clearSandboxStartupDiagnostic(launch.sandboxDiagnosticsPath);
|
|
1285
|
-
const completionGuardEnabled = isAgentContractV1(step.agentContract) ? step.completionGuard === true : step.completionGuard !== false;
|
|
1334
|
+
const completionGuardEnabled = isAgentContract(step.agentContract) ? step.completionGuard === true : step.completionGuard !== false;
|
|
1286
1335
|
const completionToolPlan = resolvedTaskToolPlan;
|
|
1287
1336
|
const completionTools = completionToolPlan ? (completionToolPlan.explicitToolAllowlist ? completionToolPlan.effectiveToolAllowlist : undefined) : step.tools;
|
|
1288
|
-
const
|
|
1337
|
+
const remoteGitChanged = run.nativeMachine?.initialGit && run.nativeMachine.finalGit ? run.nativeMachine.initialGit.head !== run.nativeMachine.finalGit.head || run.nativeMachine.initialGit.dirty !== run.nativeMachine.finalGit.dirty : undefined;
|
|
1338
|
+
const mutationEvidence = run.nativeMachine ? { source: "tracked-files" as const, trackedOnly: true as const, changedFiles: [], attemptedMutation: remoteGitChanged === true, ...(remoteGitChanged === undefined ? { unavailable: "Remote Git before/after evidence was incomplete." } : {}) } : collectTrackedMutationEvidence(mutationSnapshot, step.cwd ?? ctx.cwd);
|
|
1289
1339
|
finalMutationEvidence = mutationEvidence;
|
|
1290
1340
|
const completionMutationEvidence = ctx.trackedMutationEvidenceForCompletionGuard === false ? undefined : mutationEvidence;
|
|
1291
|
-
const completionGuard = run.exitCode === 0 && !run.error && !structuredError && !hiddenError?.hasError && !midToolExitError && !emptyOutputError && completionGuardEnabled
|
|
1341
|
+
const completionGuard = completionDiagnosticsEligible && run.exitCode === 0 && !run.error && !structuredError && !hiddenError?.hasError && !midToolExitError && !emptyOutputError && completionGuardEnabled
|
|
1292
1342
|
? evaluateCompletionMutationGuard(omitUndefinedProperties({
|
|
1293
1343
|
agent: step.agent,
|
|
1294
1344
|
task: taskForCompletionGuard,
|
|
@@ -1301,14 +1351,27 @@ async function runSingleStepInner(
|
|
|
1301
1351
|
}))
|
|
1302
1352
|
: undefined;
|
|
1303
1353
|
const mutationAttemptObserved = run.observedMutationAttempt === true || completionMutationEvidence?.attemptedMutation === true;
|
|
1354
|
+
let arbitration = { triggered: completionGuard?.triggered === true && !mutationAttemptObserved, rescued: false };
|
|
1355
|
+
if (arbitration.triggered) {
|
|
1356
|
+
const modelContext = launch.capture.completionIntentContext?.();
|
|
1357
|
+
arbitration = await arbitrateCompletionGuardRescue({
|
|
1358
|
+
guardTriggered: true,
|
|
1359
|
+
task: taskForCompletionGuard,
|
|
1360
|
+
// Construct lazily too: the shared gate refuses overlength tasks before
|
|
1361
|
+
// registry/auth/model work. The child has already shut down normally.
|
|
1362
|
+
arbiter: modelContext ? async (task) => createTaskMutationArbiter(modelContext)?.(task) ?? "unavailable" : undefined,
|
|
1363
|
+
});
|
|
1364
|
+
}
|
|
1304
1365
|
const completionEvidence = planCompletionEvidence({
|
|
1305
1366
|
guard: completionGuard,
|
|
1367
|
+
guardTriggered: arbitration.triggered,
|
|
1368
|
+
arbiterRescued: arbitration.rescued,
|
|
1306
1369
|
completionGuardEnabled,
|
|
1307
1370
|
mutationCapable: hasMutationToolCapability(completionTools, completionToolPlan?.effectiveMcpTools ?? step.mcpDirectTools),
|
|
1308
1371
|
implementationMutationExpected: expectsImplementationMutation(step.agent, taskForCompletionGuard),
|
|
1309
1372
|
mutationAttemptObserved,
|
|
1310
1373
|
mutationEvidence: completionMutationEvidence,
|
|
1311
|
-
|
|
1374
|
+
agentContractEnabled: isAgentContract(step.agentContract),
|
|
1312
1375
|
});
|
|
1313
1376
|
const finalOutputHasPersistableFileContent = run.exitCode === 0 && !run.error && !emptyOutputError && Boolean(stripAcceptanceReport(run.finalOutput).trim());
|
|
1314
1377
|
const requiredOutput = step.outputMode === "file-only" && step.outputPath
|
|
@@ -1317,7 +1380,7 @@ async function runSingleStepInner(
|
|
|
1317
1380
|
? { kind: "structured" as const, path: effectiveStructuredOutput.outputPath, missing: !fs.existsSync(effectiveStructuredOutput.outputPath) }
|
|
1318
1381
|
: undefined;
|
|
1319
1382
|
finalRequiredOutputMissing = requiredOutput?.missing;
|
|
1320
|
-
const missingRequiredOutputError = formatRequiredOutputError(requiredOutput);
|
|
1383
|
+
const missingRequiredOutputError = completionDiagnosticsEligible ? formatRequiredOutputError(requiredOutput) : undefined;
|
|
1321
1384
|
const missingRequiredOutputAfterMutation = Boolean(missingRequiredOutputError) && (mutationAttemptObserved || Boolean(mutationEvidence.changedFiles.length));
|
|
1322
1385
|
const effectiveExitCode = toolAvailabilityError || completionEvidence.legacyFailureError || midToolExitError || structuredError || emptyOutputError || missingRequiredOutputError
|
|
1323
1386
|
? 1
|
|
@@ -1338,15 +1401,6 @@ async function runSingleStepInner(
|
|
|
1338
1401
|
: `${hiddenError.errorType} failed with exit code ${effectiveExitCode}`
|
|
1339
1402
|
: undefined);
|
|
1340
1403
|
const error = underlyingError ?? missingRequiredOutputError ?? completionEvidence.legacyFailureError;
|
|
1341
|
-
const attempt: ModelAttempt = omitUndefinedProperties({
|
|
1342
|
-
model: candidate ?? run.model ?? step.model ?? "default",
|
|
1343
|
-
success: effectiveExitCode === 0 && !error,
|
|
1344
|
-
exitCode: effectiveExitCode,
|
|
1345
|
-
error,
|
|
1346
|
-
usage: run.usage,
|
|
1347
|
-
});
|
|
1348
|
-
modelAttempts.push(attempt);
|
|
1349
|
-
if (!recoveringAbort && candidate) attemptedModels.push(candidate);
|
|
1350
1404
|
completionGuardTriggeredFinal = completionEvidence.guardTriggered && !underlyingError && !missingRequiredOutputError;
|
|
1351
1405
|
finalOutputSnapshot = outputSnapshot;
|
|
1352
1406
|
if (step.toolBudget) {
|
|
@@ -1364,11 +1418,14 @@ async function runSingleStepInner(
|
|
|
1364
1418
|
});
|
|
1365
1419
|
const fileMutationEffect = completionEvidence.fileMutation ?? (missingRequiredOutputAfterMutation ? { status: "observed" as const, expected: completionEvidence.mutationExpected, attempted: true, evidence: mutationEvidence } : undefined);
|
|
1366
1420
|
finalResult = { ...run, exitCode: effectiveExitCode, model: candidate ?? run.model, error, structuredOutput, runtimeAcknowledgedExtensions, ...(step.agentContract ? { agentContract: step.agentContract } : {}), ...(fileMutationEffect || settlementDiagnostic ? { effects: { ...(fileMutationEffect ? { fileMutation: fileMutationEffect } : {}), ...(settlementDiagnostic ? { settlementDiagnostic } : {}) } } : {}) } as RunChildSessionResult;
|
|
1367
|
-
|
|
1421
|
+
if (run.stopped || run.timedOut || ctx.timeoutSignal?.aborted || ctx.stopSignal?.aborted || ctx.skipAcceptance?.()) break singleLaunch;
|
|
1422
|
+
if (effectiveExitCode === 0 && !error) break singleLaunch;
|
|
1423
|
+
if (completionEvidence.guardTriggered) break singleLaunch;
|
|
1424
|
+
const recovery = planAbortRecovery({
|
|
1368
1425
|
messages: run.messages,
|
|
1369
1426
|
error,
|
|
1370
1427
|
sessionAvailable: Boolean(step.sessionFile && fs.existsSync(step.sessionFile)),
|
|
1371
|
-
alreadyResumed:
|
|
1428
|
+
alreadyResumed: attemptIndex > 0,
|
|
1372
1429
|
stopped: run.stopped || ctx.stopSignal?.aborted || ctx.skipAcceptance?.(),
|
|
1373
1430
|
interrupted: run.interrupted,
|
|
1374
1431
|
timedOut: run.timedOut || ctx.timeoutSignal?.aborted,
|
|
@@ -1378,39 +1435,26 @@ async function runSingleStepInner(
|
|
|
1378
1435
|
acceptanceFailed: false,
|
|
1379
1436
|
currentTool: run.currentTool,
|
|
1380
1437
|
afterCompactionSettlement: run.afterCompactionSettlement,
|
|
1381
|
-
})
|
|
1382
|
-
if (
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
: abortRecovery.diagnostic;
|
|
1386
|
-
if (finalResult) finalResult.abortRecoveryDiagnostic = abortRecovery.diagnostic;
|
|
1438
|
+
});
|
|
1439
|
+
if (recovery.action === "resume") {
|
|
1440
|
+
recoveryTask = recovery.prompt;
|
|
1441
|
+
continue singleLaunch;
|
|
1387
1442
|
}
|
|
1388
|
-
if (
|
|
1389
|
-
|
|
1390
|
-
if (attempt.success) break modelAttemptsLoop;
|
|
1391
|
-
if (recoveringAbort) break modelAttemptsLoop;
|
|
1392
|
-
if (abortRecovery?.action === "resume") {
|
|
1393
|
-
abortRecoveryAttempted = true;
|
|
1394
|
-
nextAttemptTask = abortRecovery.prompt;
|
|
1395
|
-
attemptNotes.push("[abort-recovery] provider/transport abort after useful progress; resuming the retained child session once.");
|
|
1396
|
-
continue;
|
|
1443
|
+
if (recovery.diagnostic) {
|
|
1444
|
+
finalResult.abortRecoveryDiagnostic = recovery.diagnostic;
|
|
1397
1445
|
}
|
|
1398
|
-
if (completionEvidence.guardTriggered) break modelAttemptsLoop;
|
|
1399
1446
|
|
|
1400
|
-
const retryableModelFailure = isRetryableModelFailureAttempt({ error, messages: run.messages, toolCount: run.toolCount, ...(startupBlocked ? { startupBlocked: true } : {}) });
|
|
1401
|
-
if (retryableModelFailure) recordRetryableModelFailure(candidate ?? run.model ?? step.model, error);
|
|
1402
1447
|
if (isContextOverflow(error)) {
|
|
1403
1448
|
contextOverflow = true;
|
|
1404
|
-
|
|
1405
|
-
break modelAttemptsLoop;
|
|
1449
|
+
break singleLaunch;
|
|
1406
1450
|
}
|
|
1407
|
-
|
|
1408
|
-
attemptNotes.push(formatModelAttemptNote(attempt, candidates[modelIndex + 1]));
|
|
1409
|
-
modelIndex += 1;
|
|
1451
|
+
break singleLaunch;
|
|
1410
1452
|
}
|
|
1411
1453
|
|
|
1412
1454
|
const rawOutput = finalResult?.finalOutput ?? "";
|
|
1413
|
-
|
|
1455
|
+
let outputForPersistence = stripAcceptanceReport(rawOutput);
|
|
1456
|
+
if (!outputForPersistence.trim() && finalResult?.structuredOutput !== undefined)
|
|
1457
|
+
outputForPersistence = JSON.stringify(finalResult.structuredOutput, null, 2);
|
|
1414
1458
|
const resolvedOutput = step.outputPath && finalResult?.exitCode === 0
|
|
1415
1459
|
? resolveSingleOutput(step.outputPath, outputForPersistence, finalOutputSnapshot, step.outputClaimPath)
|
|
1416
1460
|
: { fullOutput: outputForPersistence };
|
|
@@ -1423,9 +1467,6 @@ async function runSingleStepInner(
|
|
|
1423
1467
|
const output = stripAcceptanceReport(resolvedOutput.fullOutput);
|
|
1424
1468
|
const outputReference = resolvedOutput.savedPath ? formatSavedOutputReference(resolvedOutput.savedPath, output) : undefined;
|
|
1425
1469
|
let outputForSummary = output;
|
|
1426
|
-
if (attemptNotes.length > 0) {
|
|
1427
|
-
outputForSummary = `${attemptNotes.join("\n")}\n\n${outputForSummary}`.trim();
|
|
1428
|
-
}
|
|
1429
1470
|
if (finalResult?.stopped && !outputForSummary.trim()) {
|
|
1430
1471
|
outputForSummary = ctx.stopMessage ?? "Subagent stopped by user.";
|
|
1431
1472
|
}
|
|
@@ -1478,7 +1519,7 @@ async function runSingleStepInner(
|
|
|
1478
1519
|
cwd: step.cwd ?? ctx.cwd,
|
|
1479
1520
|
signal: combinedAbortSignal([ctx.timeoutSignal, ctx.stopSignal]),
|
|
1480
1521
|
abortMessage: ctx.stopSignal?.aborted ? ctx.stopMessage ?? "Subagent stopped by user." : ctx.timeoutMessage ?? "Subagent timed out.",
|
|
1481
|
-
reportOptional:
|
|
1522
|
+
reportOptional: isAgentContract(step.agentContract),
|
|
1482
1523
|
artifactsDir: ctx.artifactsDir,
|
|
1483
1524
|
runId: ctx.id,
|
|
1484
1525
|
watchdog: finalResult?.watchdog,
|
|
@@ -1494,7 +1535,7 @@ async function runSingleStepInner(
|
|
|
1494
1535
|
: acceptance
|
|
1495
1536
|
: undefined;
|
|
1496
1537
|
const acceptanceFailure = effectiveAcceptance ? acceptanceFailureMessage(effectiveAcceptance) : undefined;
|
|
1497
|
-
const acceptanceCanFailRun = acceptanceFailure && effectiveAcceptance?.explicit && (finalResult?.exitCode ?? 1) === 0 && !finalResult?.interrupted && !timedOutAfterAcceptance && !stoppedAfterAcceptance && !
|
|
1538
|
+
const acceptanceCanFailRun = acceptanceFailure && effectiveAcceptance?.explicit && (finalResult?.exitCode ?? 1) === 0 && !finalResult?.interrupted && !timedOutAfterAcceptance && !stoppedAfterAcceptance && !isAgentContract(step.agentContract);
|
|
1498
1539
|
const effectiveFinalExitCode = timedOutAfterAcceptance || stoppedAfterAcceptance ? 1 : acceptanceCanFailRun ? 1 : finalResult?.exitCode ?? 1;
|
|
1499
1540
|
const intercomDetachReceipt = finalResult?.finalOutput === INTERCOM_DETACH_RECEIPT;
|
|
1500
1541
|
const baseFinalError = stoppedAfterAcceptance
|
|
@@ -1510,7 +1551,7 @@ async function runSingleStepInner(
|
|
|
1510
1551
|
abortRecoveryDiagnostic: effectiveFinalExitCode !== 0 ? finalResult?.abortRecoveryDiagnostic : undefined,
|
|
1511
1552
|
requiredOutput: effectiveFinalExitCode !== 0 ? finalResult?.effects?.settlementDiagnostic?.requiredOutput : undefined,
|
|
1512
1553
|
});
|
|
1513
|
-
const usage =
|
|
1554
|
+
const usage = launched ? aggregateUsage : undefined;
|
|
1514
1555
|
|
|
1515
1556
|
const artifactErrors = artifactPaths && ctx.artifactConfig?.enabled !== false
|
|
1516
1557
|
? persistStepArtifacts({
|
|
@@ -1528,16 +1569,15 @@ async function runSingleStepInner(
|
|
|
1528
1569
|
task: PROMPT_REDACTED,
|
|
1529
1570
|
exitCode: effectiveFinalExitCode,
|
|
1530
1571
|
model: finalResult?.model,
|
|
1531
|
-
|
|
1532
|
-
|
|
1572
|
+
nativeMachine: finalResult?.nativeMachine,
|
|
1573
|
+
requestedModel: step.requestedModel,
|
|
1533
1574
|
usage,
|
|
1534
1575
|
error: effectiveFinalError,
|
|
1535
1576
|
acceptance: effectiveAcceptance,
|
|
1536
1577
|
...(capabilityAudit ? { capabilityCeiling: capabilityAudit.ceiling, capabilityAudit } : {}),
|
|
1537
|
-
sandbox: step.sandbox,
|
|
1538
1578
|
launchContractDigest: actualLaunchContractDigest,
|
|
1539
1579
|
launchResolvedExtensions,
|
|
1540
|
-
...((finalResult as (RunChildSessionResult & { runtimeAcknowledgedExtensions?:
|
|
1580
|
+
...((finalResult as (RunChildSessionResult & { runtimeAcknowledgedExtensions?: RuntimeAcknowledgedChildExtensions }) | undefined)?.runtimeAcknowledgedExtensions ? { runtimeAcknowledgedExtensions: (finalResult as RunChildSessionResult & { runtimeAcknowledgedExtensions?: RuntimeAcknowledgedChildExtensions }).runtimeAcknowledgedExtensions } : {}),
|
|
1541
1581
|
...(transcriptWriter ? { transcriptPath: artifactPaths.transcriptPath } : {}),
|
|
1542
1582
|
transcriptError: transcriptWriter?.getError(),
|
|
1543
1583
|
skills: step.skills,
|
|
@@ -1550,8 +1590,6 @@ async function runSingleStepInner(
|
|
|
1550
1590
|
agent: step.agent,
|
|
1551
1591
|
...(childSessionName ? { sessionName: childSessionName } : {}),
|
|
1552
1592
|
context: step.context,
|
|
1553
|
-
...(step.sandbox ? { sandbox: step.sandbox } : {}),
|
|
1554
|
-
...(step.permissionProfile ? { permissionProfile: step.permissionProfile } : {}),
|
|
1555
1593
|
...(step.agentContract ? { agentContract: step.agentContract } : {}),
|
|
1556
1594
|
launchContractDigest: actualLaunchContractDigest,
|
|
1557
1595
|
output: outputForSummary,
|
|
@@ -1561,14 +1599,16 @@ async function runSingleStepInner(
|
|
|
1561
1599
|
sessionFile: step.sessionFile,
|
|
1562
1600
|
intercomTarget: ctx.childIntercomTarget,
|
|
1563
1601
|
model: finalResult?.model,
|
|
1602
|
+
nativeMachine: finalResult?.nativeMachine,
|
|
1564
1603
|
thinking: resolveEffectiveThinking(finalResult?.model, step.thinking),
|
|
1565
|
-
|
|
1566
|
-
modelAttempts,
|
|
1604
|
+
requestedModel: step.requestedModel,
|
|
1567
1605
|
contextOverflow: contextOverflow || undefined,
|
|
1568
|
-
totalCost:
|
|
1606
|
+
totalCost: costSummaryFromUsage(usage),
|
|
1569
1607
|
usage,
|
|
1570
1608
|
artifactPaths,
|
|
1609
|
+
savedOutputPath: finalizedOutput.savedPath,
|
|
1571
1610
|
outputSaveError: [resolvedOutput.saveError, artifactErrors.outputSaveError].filter(Boolean).join("\n") || undefined,
|
|
1611
|
+
artifactOutputSaveFailed: artifactErrors.outputSaveError ? true : undefined,
|
|
1572
1612
|
metadataSaveError: artifactErrors.metadataSaveError,
|
|
1573
1613
|
transcriptPath: transcriptWriter ? artifactPaths?.transcriptPath : undefined,
|
|
1574
1614
|
transcriptError: transcriptWriter?.getError(),
|
|
@@ -1587,12 +1627,9 @@ async function runSingleStepInner(
|
|
|
1587
1627
|
watchdog: finalResult?.watchdog,
|
|
1588
1628
|
...(capabilityAudit ? { capabilityCeiling: capabilityAudit.ceiling, capabilityAudit } : {}),
|
|
1589
1629
|
launchResolvedExtensions,
|
|
1590
|
-
...((finalResult as (RunChildSessionResult & { runtimeAcknowledgedExtensions?:
|
|
1630
|
+
...((finalResult as (RunChildSessionResult & { runtimeAcknowledgedExtensions?: RuntimeAcknowledgedChildExtensions }) | undefined)?.runtimeAcknowledgedExtensions ? { runtimeAcknowledgedExtensions: (finalResult as RunChildSessionResult & { runtimeAcknowledgedExtensions?: RuntimeAcknowledgedChildExtensions }).runtimeAcknowledgedExtensions } : {}),
|
|
1591
1631
|
});
|
|
1592
|
-
|
|
1593
|
-
// projections are attached to that object and re-narrowed to its own type, so the
|
|
1594
|
-
// assertion only re-states the shape the builders already produced here.
|
|
1595
|
-
return isAgentContractV1(step.agentContract) ? attachContractProjections(result as unknown as import("../../shared/types.ts").SingleResult) as unknown as typeof result : result;
|
|
1632
|
+
return isAgentContract(step.agentContract) ? attachContractProjections(result as unknown as import("../../shared/types.ts").SingleResult) as unknown as typeof result : result;
|
|
1596
1633
|
}
|
|
1597
1634
|
|
|
1598
1635
|
async function runSingleStep(
|
|
@@ -1683,12 +1720,15 @@ function markParallelGroupSetupFailure(input: {
|
|
|
1683
1720
|
const statusStep = requiredStatusStep(input.statusPayload, flatTaskIndex);
|
|
1684
1721
|
const task = input.group.parallel[taskIndex];
|
|
1685
1722
|
if (!task) throw new Error(`Missing parallel task at index ${taskIndex}`);
|
|
1686
|
-
statusStep.
|
|
1723
|
+
const stopped = statusStep.stopped || statusStep.stopRequested || input.statusPayload.stopped;
|
|
1724
|
+
const paused = !stopped && input.statusPayload.state === "paused";
|
|
1725
|
+
statusStep.status = stopped ? "stopped" : paused ? "paused" : "failed";
|
|
1687
1726
|
statusStep.startedAt = input.failedAt;
|
|
1688
1727
|
statusStep.endedAt = input.failedAt;
|
|
1689
1728
|
statusStep.durationMs = 0;
|
|
1690
|
-
statusStep.exitCode = 1;
|
|
1691
|
-
|
|
1729
|
+
statusStep.exitCode = paused ? 0 : 1;
|
|
1730
|
+
statusStep.error ??= input.setupError;
|
|
1731
|
+
input.results.push(omitUndefinedProperties({ agent: task.agent, context: task.context, output: input.setupError, error: input.setupError, success: false, exitCode: paused ? 0 : 1, sessionFile: task.sessionFile, stopped: stopped || undefined, interrupted: paused || undefined, timedOut: input.statusPayload.timedOut || undefined }));
|
|
1692
1732
|
}
|
|
1693
1733
|
input.statusPayload.currentStep = input.groupStartFlatIndex;
|
|
1694
1734
|
input.statusPayload.lastUpdate = input.failedAt;
|
|
@@ -1812,8 +1852,12 @@ function missingRequiredOutputAfterUsefulMutation(result: SingleStepResult): boo
|
|
|
1812
1852
|
}
|
|
1813
1853
|
|
|
1814
1854
|
function partialExecutionWithUsefulMutation(result: SingleStepResult): boolean {
|
|
1855
|
+
if (result.execution?.status !== "partial") return false;
|
|
1856
|
+
// Pane-native external results are deliberately partial from bounded terminal
|
|
1857
|
+
// evidence alone; Pi mutation-based partials retain their existing behavior.
|
|
1858
|
+
if (result.runner?.type === "external-cli" && result.runner.machine) return true;
|
|
1815
1859
|
const fileMutation = result.effects?.fileMutation;
|
|
1816
|
-
return
|
|
1860
|
+
return fileMutation?.attempted === true || Boolean(fileMutation?.evidence?.changedFiles.length);
|
|
1817
1861
|
}
|
|
1818
1862
|
|
|
1819
1863
|
function partialEvidenceResult(result: SingleStepResult): boolean {
|
|
@@ -1845,7 +1889,10 @@ async function runSingleStepWithTimeout(
|
|
|
1845
1889
|
ctx: SingleStepContext,
|
|
1846
1890
|
parentDeadlineAt?: number,
|
|
1847
1891
|
): Promise<SingleStepResult> {
|
|
1848
|
-
if (step.timeoutMs === undefined) return runSingleStep(step, ctx
|
|
1892
|
+
if (step.timeoutMs === undefined) return runSingleStep(step, parentDeadlineAt === undefined ? ctx : {
|
|
1893
|
+
...ctx,
|
|
1894
|
+
deadlineAt: ctx.deadlineAt === undefined ? parentDeadlineAt : Math.min(ctx.deadlineAt, parentDeadlineAt),
|
|
1895
|
+
});
|
|
1849
1896
|
|
|
1850
1897
|
const parentRemainingMs = parentDeadlineAt === undefined ? undefined : Math.max(0, parentDeadlineAt - Date.now());
|
|
1851
1898
|
const timeoutMs = parentRemainingMs === undefined ? step.timeoutMs : Math.min(step.timeoutMs, parentRemainingMs);
|
|
@@ -1882,7 +1929,7 @@ async function runSingleStepWithTimeout(
|
|
|
1882
1929
|
}
|
|
1883
1930
|
}
|
|
1884
1931
|
|
|
1885
|
-
async function runSubagent(
|
|
1932
|
+
export async function runSubagent(
|
|
1886
1933
|
config: SubagentRunConfig,
|
|
1887
1934
|
childSessions: ChildSessionFactory,
|
|
1888
1935
|
): Promise<void> {
|
|
@@ -1913,6 +1960,7 @@ async function runSubagent(
|
|
|
1913
1960
|
let currentActivityState: ActivityState | undefined;
|
|
1914
1961
|
let activityTimer: NodeJS.Timeout | undefined;
|
|
1915
1962
|
let timeoutTimer: NodeJS.Timeout | undefined;
|
|
1963
|
+
let checkpointTimer: NodeJS.Timeout | undefined;
|
|
1916
1964
|
let timedOut = false;
|
|
1917
1965
|
let stopped = false;
|
|
1918
1966
|
let usageBudgetExceeded = false;
|
|
@@ -1920,6 +1968,9 @@ async function runSubagent(
|
|
|
1920
1968
|
const stopMessage = "Subagent stopped by user.";
|
|
1921
1969
|
const timeoutAbortController = new AbortController();
|
|
1922
1970
|
const stopAbortController = new AbortController();
|
|
1971
|
+
const setupInterruptController = new AbortController();
|
|
1972
|
+
const runStopSignal = AbortSignal.any([timeoutAbortController.signal, stopAbortController.signal]);
|
|
1973
|
+
const setupSignal = AbortSignal.any([timeoutAbortController.signal, stopAbortController.signal, setupInterruptController.signal]);
|
|
1923
1974
|
let previousCumulativeTokens: TokenUsage = { input: 0, output: 0, total: 0 };
|
|
1924
1975
|
let latestSessionFile: string | undefined;
|
|
1925
1976
|
|
|
@@ -1961,7 +2012,7 @@ async function runSubagent(
|
|
|
1961
2012
|
model: task.model,
|
|
1962
2013
|
...(task.contextLimit !== undefined ? { contextLimit: task.contextLimit } : {}),
|
|
1963
2014
|
thinking: task.thinking,
|
|
1964
|
-
|
|
2015
|
+
requestedModel: task.requestedModel,
|
|
1965
2016
|
recentTools: [],
|
|
1966
2017
|
recentOutput: [],
|
|
1967
2018
|
}));
|
|
@@ -2017,7 +2068,7 @@ async function runSubagent(
|
|
|
2017
2068
|
model: step.model,
|
|
2018
2069
|
...(step.contextLimit !== undefined ? { contextLimit: step.contextLimit } : {}),
|
|
2019
2070
|
thinking: step.thinking,
|
|
2020
|
-
|
|
2071
|
+
requestedModel: step.requestedModel,
|
|
2021
2072
|
recentTools: [],
|
|
2022
2073
|
recentOutput: [],
|
|
2023
2074
|
}));
|
|
@@ -2093,20 +2144,29 @@ async function runSubagent(
|
|
|
2093
2144
|
},
|
|
2094
2145
|
onError: (error, filePath) => console.error(`Failed to update async run index '${filePath}':`, error),
|
|
2095
2146
|
});
|
|
2096
|
-
const queueActiveRunIndex = (): void => {
|
|
2097
|
-
const state =
|
|
2147
|
+
const queueActiveRunIndex = (status: AsyncStatus): void => {
|
|
2148
|
+
const state = status.state;
|
|
2098
2149
|
if (state === lastIndexedStatusState && indexPersistence.pendingCount() === 0) return;
|
|
2099
|
-
indexPersistence.write(asyncDir, { state, toolCallId:
|
|
2150
|
+
indexPersistence.write(asyncDir, { state, toolCallId: status.toolCallId }, (_filePath, payload) => {
|
|
2100
2151
|
const indexPayload = payload as { state: AsyncStatus["state"]; toolCallId?: string };
|
|
2101
2152
|
updateActiveRunIndex(asyncDir, indexPayload.state, indexPayload.toolCallId, { retryCapacityErrors: true });
|
|
2102
2153
|
});
|
|
2103
2154
|
};
|
|
2155
|
+
let finalResultCommitted = false;
|
|
2156
|
+
let finalResultPublication: { resolve(): void; reject(error: unknown): void } | undefined;
|
|
2104
2157
|
const runPersistence = createCapacityResilientJsonWriter({
|
|
2105
2158
|
keepAlive: true,
|
|
2106
|
-
onSuccess: (filePath) => {
|
|
2107
|
-
if (filePath === statusPath) queueActiveRunIndex();
|
|
2159
|
+
onSuccess: (filePath, payload) => {
|
|
2160
|
+
if (filePath === statusPath) queueActiveRunIndex(payload as AsyncStatus);
|
|
2161
|
+
if (filePath === resultPath && finalResultPublication) {
|
|
2162
|
+
finalResultCommitted = true;
|
|
2163
|
+
finalResultPublication.resolve();
|
|
2164
|
+
}
|
|
2165
|
+
},
|
|
2166
|
+
onError: (error, filePath) => {
|
|
2167
|
+
console.error(`Failed to persist async run state '${filePath}':`, error);
|
|
2168
|
+
if (filePath === resultPath) finalResultPublication?.reject(error);
|
|
2108
2169
|
},
|
|
2109
|
-
onError: (error, filePath) => console.error(`Failed to persist async run state '${filePath}':`, error),
|
|
2110
2170
|
});
|
|
2111
2171
|
try {
|
|
2112
2172
|
fs.mkdirSync(asyncDir, { recursive: true });
|
|
@@ -2114,7 +2174,7 @@ async function runSubagent(
|
|
|
2114
2174
|
if (!isStorageCapacityError(error)) throw error;
|
|
2115
2175
|
console.error(`Failed to prepare async run storage '${asyncDir}' while storage is full:`, error);
|
|
2116
2176
|
}
|
|
2117
|
-
runPersistence.write(statusPath, statusPayload);
|
|
2177
|
+
runPersistence.write(statusPath, { ...statusPayload });
|
|
2118
2178
|
|
|
2119
2179
|
let pendingParallelUsageCost: CostSummary = { inputTokens: 0, outputTokens: 0, costUsd: 0 };
|
|
2120
2180
|
const currentUsageTotals = (): CostSummary => {
|
|
@@ -2133,6 +2193,13 @@ async function runSubagent(
|
|
|
2133
2193
|
setOptionalProperty(statusPayload, "usageBudget", usageBudgetState(config.usageBudget, currentUsageTotals()));
|
|
2134
2194
|
return statusPayload.usageBudget;
|
|
2135
2195
|
};
|
|
2196
|
+
// Continuation admission only: the existing ledger has no in-flight cost or
|
|
2197
|
+
// external/import usage coverage. Never change ordinary budget enforcement.
|
|
2198
|
+
let continuationUsageUncertain = config.steps.some(isDynamicRunnerGroup) || flatSteps.some((step) => Boolean(step.runner || step.importAsyncRoot));
|
|
2199
|
+
const continuationUsageBudgetExhausted = (): boolean | undefined => {
|
|
2200
|
+
const exhausted = refreshUsageBudget()?.exhausted === true;
|
|
2201
|
+
return exhausted ? true : config.usageBudget && continuationUsageUncertain ? undefined : false;
|
|
2202
|
+
};
|
|
2136
2203
|
const emitNestedSelfEvent = (type: "subagent.nested.updated" | "subagent.nested.completed"): void => {
|
|
2137
2204
|
if (!config.nestedRoute || !config.nestedSelf) return;
|
|
2138
2205
|
try {
|
|
@@ -2187,7 +2254,6 @@ async function runSubagent(
|
|
|
2187
2254
|
for (const node of graph.nodes) updateNode(node);
|
|
2188
2255
|
statusPayload.workflowGraph = graph;
|
|
2189
2256
|
};
|
|
2190
|
-
let finalResultCommitted = false;
|
|
2191
2257
|
const statusResultState = (): AsyncStatus["state"] | undefined => {
|
|
2192
2258
|
if (statusPayload.state === "running" || statusPayload.state === "queued") return undefined;
|
|
2193
2259
|
return statusPayload.state;
|
|
@@ -2234,11 +2300,7 @@ async function runSubagent(
|
|
|
2234
2300
|
sessionFile: step.sessionFile,
|
|
2235
2301
|
model: step.model,
|
|
2236
2302
|
thinking: step.thinking,
|
|
2237
|
-
|
|
2238
|
-
attemptedModels: step.attemptedModels,
|
|
2239
|
-
modelAttempts: step.modelAttempts,
|
|
2240
|
-
usage: usageFromAttempts(step.modelAttempts),
|
|
2241
|
-
contextOverflow: step.contextOverflow,
|
|
2303
|
+
requestedModel: step.requestedModel, contextOverflow: step.contextOverflow,
|
|
2242
2304
|
})),
|
|
2243
2305
|
exitCode: state === "complete" || state === "paused" ? 0 : 1,
|
|
2244
2306
|
timestamp: now,
|
|
@@ -2251,14 +2313,15 @@ async function runSubagent(
|
|
|
2251
2313
|
}), (filePath, payload) => writePendingAsyncResultFile(filePath, payload as Record<string, unknown>));
|
|
2252
2314
|
};
|
|
2253
2315
|
const writeStatusPayloadNow = (): void => {
|
|
2316
|
+
if (finalResultPublication) return;
|
|
2254
2317
|
refreshWorkflowGraph();
|
|
2255
2318
|
writeRecoverableStatusResult();
|
|
2256
|
-
runPersistence.write(statusPath, statusPayload);
|
|
2319
|
+
runPersistence.write(statusPath, { ...statusPayload });
|
|
2257
2320
|
emitNestedSelfEvent(statusPayload.state === "running" || statusPayload.state === "queued" ? "subagent.nested.updated" : "subagent.nested.completed");
|
|
2258
2321
|
};
|
|
2259
2322
|
const statusWriteCoalescer = createFileCoalescer(writeStatusPayloadNow, 100);
|
|
2260
2323
|
const writeStatusPayload = (immediate = true): void => {
|
|
2261
|
-
if (immediate || statusPayload.state !== "running"
|
|
2324
|
+
if (immediate || statusPayload.state !== "running") {
|
|
2262
2325
|
if (!statusWriteCoalescer.flush(statusPath)) writeStatusPayloadNow();
|
|
2263
2326
|
return;
|
|
2264
2327
|
}
|
|
@@ -2401,6 +2464,8 @@ async function runSubagent(
|
|
|
2401
2464
|
yield* nestedRuns(child.steps?.flatMap((step) => step.children ?? []));
|
|
2402
2465
|
}
|
|
2403
2466
|
};
|
|
2467
|
+
const isNestedControlDescendant = (run: NestedRunSummary): boolean =>
|
|
2468
|
+
!config.nestedSelf || run.path.some((entry) => entry.runId === id);
|
|
2404
2469
|
const interruptNestedAsyncDescendants = (): void => {
|
|
2405
2470
|
if (!config.nestedRoute) return;
|
|
2406
2471
|
let registry: ReturnType<typeof projectNestedEvents>;
|
|
@@ -2416,7 +2481,7 @@ async function runSubagent(
|
|
|
2416
2481
|
return;
|
|
2417
2482
|
}
|
|
2418
2483
|
for (const run of nestedRuns(registry.children)) {
|
|
2419
|
-
if (run.state !== "running" && run.state !== "queued") continue;
|
|
2484
|
+
if (!isNestedControlDescendant(run) || (run.state !== "running" && run.state !== "queued")) continue;
|
|
2420
2485
|
const nestedAsyncDir = run.asyncDir ?? resolveNestedAsyncDir(config.nestedRoute.rootRunId, run);
|
|
2421
2486
|
if (!nestedAsyncDir) continue;
|
|
2422
2487
|
try {
|
|
@@ -2447,7 +2512,7 @@ async function runSubagent(
|
|
|
2447
2512
|
return;
|
|
2448
2513
|
}
|
|
2449
2514
|
for (const run of nestedRuns(registry.children)) {
|
|
2450
|
-
if (run.state !== "running" && run.state !== "queued") continue;
|
|
2515
|
+
if (!isNestedControlDescendant(run) || (run.state !== "running" && run.state !== "queued")) continue;
|
|
2451
2516
|
const nestedAsyncDir = run.asyncDir ?? resolveNestedAsyncDir(config.nestedRoute.rootRunId, run);
|
|
2452
2517
|
if (!nestedAsyncDir) continue;
|
|
2453
2518
|
try {
|
|
@@ -2478,7 +2543,7 @@ async function runSubagent(
|
|
|
2478
2543
|
return;
|
|
2479
2544
|
}
|
|
2480
2545
|
for (const run of nestedRuns(registry.children)) {
|
|
2481
|
-
if (run.state !== "running" && run.state !== "queued") continue;
|
|
2546
|
+
if (!isNestedControlDescendant(run) || (run.state !== "running" && run.state !== "queued")) continue;
|
|
2482
2547
|
const nestedAsyncDir = run.asyncDir ?? resolveNestedAsyncDir(config.nestedRoute.rootRunId, run);
|
|
2483
2548
|
if (!nestedAsyncDir) continue;
|
|
2484
2549
|
try {
|
|
@@ -2534,6 +2599,7 @@ async function runSubagent(
|
|
|
2534
2599
|
}
|
|
2535
2600
|
const appendedSteps = requests.flatMap((request) => request.steps);
|
|
2536
2601
|
steps.push(...appendedSteps);
|
|
2602
|
+
flatSteps.push(...flattenSteps(appendedSteps));
|
|
2537
2603
|
const now = Date.now();
|
|
2538
2604
|
const pendingAppends = countPendingChainAppendRequests(asyncDir);
|
|
2539
2605
|
const added = appendRunnerStepsToStatus({
|
|
@@ -2567,9 +2633,66 @@ async function runSubagent(
|
|
|
2567
2633
|
setOptionalProperty(groupNode, "acceptanceStatus", acceptance?.status ?? groupNode.acceptanceStatus);
|
|
2568
2634
|
};
|
|
2569
2635
|
|
|
2636
|
+
const EXTERNAL_GIT_PROBE_MIN_INTERVAL_MS = 2_000;
|
|
2637
|
+
type ExternalActivityEvidence = {
|
|
2638
|
+
cwd?: string;
|
|
2639
|
+
lastStreamActivityAt?: number;
|
|
2640
|
+
fingerprint?: string;
|
|
2641
|
+
probeInFlight?: Promise<void>;
|
|
2642
|
+
lastProbeStartedAt?: number;
|
|
2643
|
+
};
|
|
2644
|
+
const externalActivityEvidence = new Map<number, ExternalActivityEvidence>();
|
|
2645
|
+
const gitProbesByCwd = new Map<string, Promise<string | undefined>>();
|
|
2646
|
+
let periodicGitProbeFailure: { index: number; error: Error; message: string } | undefined;
|
|
2647
|
+
const reportedGitProbeErrors = new Set<string>();
|
|
2648
|
+
const reportGitProbeError = (externalCwd: string, error: unknown): void => {
|
|
2649
|
+
if (reportedGitProbeErrors.has(externalCwd)) return;
|
|
2650
|
+
reportedGitProbeErrors.add(externalCwd);
|
|
2651
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
2652
|
+
console.error(`[pi-subagents] Git activity evidence unavailable for '${externalCwd.slice(-300)}'; check the cwd and Git installation: ${detail.slice(0, 500)}`);
|
|
2653
|
+
};
|
|
2654
|
+
const readSharedGitFingerprint = (externalCwd: string): Promise<string | undefined> => {
|
|
2655
|
+
const inFlight = gitProbesByCwd.get(externalCwd);
|
|
2656
|
+
if (inFlight) return inFlight;
|
|
2657
|
+
const probe = readGitFingerprint(externalCwd, runStopSignal, (error) => reportGitProbeError(externalCwd, error)).finally(() => gitProbesByCwd.delete(externalCwd));
|
|
2658
|
+
gitProbesByCwd.set(externalCwd, probe);
|
|
2659
|
+
return probe;
|
|
2660
|
+
};
|
|
2661
|
+
const recordPeriodicGitProbeFailure = (index: number, error: unknown): void => {
|
|
2662
|
+
if (periodicGitProbeFailure) return;
|
|
2663
|
+
const cause = error instanceof Error ? error : new Error(String(error));
|
|
2664
|
+
const message = `PROCESS_TREE_UNVERIFIED: ${cause.message}`;
|
|
2665
|
+
periodicGitProbeFailure = { index, error: cause, message };
|
|
2666
|
+
statusPayload.error = message;
|
|
2667
|
+
const step = statusPayload.steps[index];
|
|
2668
|
+
if (step) step.error = message;
|
|
2669
|
+
statusPayload.lastUpdate = Date.now();
|
|
2670
|
+
writeStatusPayload();
|
|
2671
|
+
appendJsonl(eventsPath, JSON.stringify({
|
|
2672
|
+
type: "subagent.run.process_tree_unverified",
|
|
2673
|
+
ts: statusPayload.lastUpdate,
|
|
2674
|
+
runId: id,
|
|
2675
|
+
index,
|
|
2676
|
+
code: "PROCESS_TREE_UNVERIFIED",
|
|
2677
|
+
message,
|
|
2678
|
+
}));
|
|
2679
|
+
};
|
|
2680
|
+
const prepareExternalActivity = async (index: number, externalCwd: string, signal: AbortSignal): Promise<void> => {
|
|
2681
|
+
if (!controlConfig.enabled) return;
|
|
2682
|
+
const resolvedCwd = path.resolve(externalCwd);
|
|
2683
|
+
const evidence: ExternalActivityEvidence = { cwd: resolvedCwd, lastProbeStartedAt: performance.now() };
|
|
2684
|
+
externalActivityEvidence.set(index, evidence);
|
|
2685
|
+
evidence.fingerprint = await readGitFingerprint(resolvedCwd, signal, (error) => reportGitProbeError(resolvedCwd, error));
|
|
2686
|
+
};
|
|
2687
|
+
const recordExternalStreamActivity = (index: number): void => {
|
|
2688
|
+
const evidence = externalActivityEvidence.get(index) ?? {};
|
|
2689
|
+
externalActivityEvidence.set(index, evidence);
|
|
2690
|
+
evidence.lastStreamActivityAt = Date.now();
|
|
2691
|
+
};
|
|
2570
2692
|
const stepOutputActivityAt = (index: number): number => {
|
|
2571
2693
|
const step = statusPayload.steps[index];
|
|
2572
2694
|
let lastActivityAt = step?.lastActivityAt ?? step?.startedAt ?? overallStartTime;
|
|
2695
|
+
lastActivityAt = Math.max(lastActivityAt, externalActivityEvidence.get(index)?.lastStreamActivityAt ?? 0);
|
|
2573
2696
|
const outputPath = path.join(asyncDir, `output-${index}.log`);
|
|
2574
2697
|
try {
|
|
2575
2698
|
lastActivityAt = Math.max(lastActivityAt, fs.statSync(outputPath).mtimeMs);
|
|
@@ -2861,7 +2984,10 @@ async function runSubagent(
|
|
|
2861
2984
|
const applySteerDelivery = (requestId: string, index: number, delivery: { state: "delivered" | "queued" | "failed"; message: string }): void => {
|
|
2862
2985
|
const lifecycle = steeringStatus(statusPayload);
|
|
2863
2986
|
const request = lifecycle.recent.find((candidate) => candidate.id === requestId);
|
|
2864
|
-
|
|
2987
|
+
const target = request?.targets.find((candidate) => candidate.index === index);
|
|
2988
|
+
if (!request || !target) return;
|
|
2989
|
+
if (target.state === "delivered" || target.state === "late" || target.state === "failed") return;
|
|
2990
|
+
if (target.state === delivery.state) return;
|
|
2865
2991
|
const late = fs.existsSync(steeringMarkerPath(requestId));
|
|
2866
2992
|
const now = Date.now();
|
|
2867
2993
|
if (delivery.state === "delivered") {
|
|
@@ -2914,6 +3040,7 @@ async function runSubagent(
|
|
|
2914
3040
|
const updateStepFromChildEvent = (flatIndex: number, event: ChildEvent): void => {
|
|
2915
3041
|
const step = statusPayload.steps[flatIndex];
|
|
2916
3042
|
if (!step) return;
|
|
3043
|
+
const previousActivityState = step.activityState;
|
|
2917
3044
|
const now = Date.now();
|
|
2918
3045
|
statusPayload.currentStep = flatIndex;
|
|
2919
3046
|
if (isChildWatchdogStatusEvent(event)) {
|
|
@@ -2933,7 +3060,7 @@ async function runSubagent(
|
|
|
2933
3060
|
return;
|
|
2934
3061
|
}
|
|
2935
3062
|
if (event.type === "message_end") {
|
|
2936
|
-
const next = applyChildWatchdogMessage(step.watchdog, event.message
|
|
3063
|
+
const next = applyChildWatchdogMessage(step.watchdog, event.message);
|
|
2937
3064
|
if (next) step.watchdog = next;
|
|
2938
3065
|
if (next && (event.message as { role?: unknown } | undefined)?.role === "custom") {
|
|
2939
3066
|
statusPayload.lastUpdate = now;
|
|
@@ -3048,6 +3175,10 @@ async function runSubagent(
|
|
|
3048
3175
|
appendRecentStepOutput(step, stripAcceptanceReport(extractTextFromContent(event.message.content)).split("\n").slice(-10));
|
|
3049
3176
|
step.turnCount = (step.turnCount ?? 0) + 1;
|
|
3050
3177
|
const usage = event.message.usage;
|
|
3178
|
+
if (config.usageBudget) {
|
|
3179
|
+
const known = (value: unknown) => typeof value === "number" && Number.isFinite(value) && value >= 0;
|
|
3180
|
+
if (!known(usage?.input ?? usage?.inputTokens) || !known(usage?.output ?? usage?.outputTokens)) continuationUsageUncertain = true;
|
|
3181
|
+
}
|
|
3051
3182
|
if (usage) {
|
|
3052
3183
|
const input = usage.input ?? usage.inputTokens ?? 0;
|
|
3053
3184
|
const output = usage.output ?? usage.outputTokens ?? 0;
|
|
@@ -3067,9 +3198,10 @@ async function runSubagent(
|
|
|
3067
3198
|
statusPayload.lastActivityAt = now;
|
|
3068
3199
|
statusPayload.lastUpdate = now;
|
|
3069
3200
|
maybeEmitActiveLongRunning(flatIndex, now);
|
|
3070
|
-
|
|
3201
|
+
// A sibling may keep aggregate attention unchanged; publish this step's transition.
|
|
3202
|
+
writeStatusPayload(step.activityState !== previousActivityState);
|
|
3071
3203
|
};
|
|
3072
|
-
const updateRunnerActivityState = (now: number): boolean => {
|
|
3204
|
+
const updateRunnerActivityState = (now: number, skipExternalProbeIndex?: number): boolean => {
|
|
3073
3205
|
if (!controlConfig.enabled) return false;
|
|
3074
3206
|
let changed = false;
|
|
3075
3207
|
let runLastActivityAt = statusPayload.lastActivityAt ?? overallStartTime;
|
|
@@ -3086,12 +3218,35 @@ async function runSubagent(
|
|
|
3086
3218
|
config: controlConfig,
|
|
3087
3219
|
startedAt: step.startedAt ?? overallStartTime,
|
|
3088
3220
|
lastActivityAt,
|
|
3089
|
-
turnCount: step.turnCount,
|
|
3221
|
+
turnCount: flatSteps[index]?.runner?.type === "external-cli" ? 1 : step.turnCount,
|
|
3090
3222
|
currentTool: step.currentTool,
|
|
3091
3223
|
thinking: step.thinking,
|
|
3092
3224
|
now,
|
|
3093
3225
|
}));
|
|
3094
3226
|
if (idleState === "needs_attention") {
|
|
3227
|
+
const evidence = externalActivityEvidence.get(index);
|
|
3228
|
+
if (index !== skipExternalProbeIndex && evidence?.cwd && evidence.fingerprint) {
|
|
3229
|
+
if (evidence.probeInFlight) continue;
|
|
3230
|
+
if (performance.now() - (evidence.lastProbeStartedAt ?? 0) >= EXTERNAL_GIT_PROBE_MIN_INTERVAL_MS) {
|
|
3231
|
+
evidence.lastProbeStartedAt = performance.now();
|
|
3232
|
+
const baseline = evidence.fingerprint;
|
|
3233
|
+
evidence.probeInFlight = readSharedGitFingerprint(evidence.cwd).then((fingerprint) => {
|
|
3234
|
+
if (requiredStatusStep(statusPayload, index).status !== "running") return;
|
|
3235
|
+
if (fingerprint && fingerprint !== baseline) {
|
|
3236
|
+
evidence.fingerprint = fingerprint;
|
|
3237
|
+
evidence.lastStreamActivityAt = Date.now();
|
|
3238
|
+
updateRunnerActivityState(Date.now());
|
|
3239
|
+
} else {
|
|
3240
|
+
updateRunnerActivityState(Date.now(), index);
|
|
3241
|
+
}
|
|
3242
|
+
}).catch((error: unknown) => {
|
|
3243
|
+
recordPeriodicGitProbeFailure(index, error);
|
|
3244
|
+
}).finally(() => {
|
|
3245
|
+
delete evidence.probeInFlight;
|
|
3246
|
+
});
|
|
3247
|
+
continue;
|
|
3248
|
+
}
|
|
3249
|
+
}
|
|
3095
3250
|
const previous = step.activityState;
|
|
3096
3251
|
step.activityState = "needs_attention";
|
|
3097
3252
|
if (previous !== "needs_attention") {
|
|
@@ -3139,10 +3294,59 @@ async function runSubagent(
|
|
|
3139
3294
|
activityTimer.unref?.();
|
|
3140
3295
|
}
|
|
3141
3296
|
|
|
3297
|
+
const publishSetupUnknown = (progress: WorktreeSetupProgress): void => {
|
|
3298
|
+
if (!progress.unknown) return;
|
|
3299
|
+
const proof = {
|
|
3300
|
+
version: 1 as const, state: "unknown" as const, runId: id,
|
|
3301
|
+
runnerProcessInstanceId: config.runnerProcessInstanceId ?? "unknown",
|
|
3302
|
+
reason: "process-tree-unverified" as const,
|
|
3303
|
+
diagnostic: `Worktree setup settlement unknown: ${progress.unknown}; manual reconciliation required; ${parallelHandoffPath(asyncDir)}`,
|
|
3304
|
+
};
|
|
3305
|
+
statusPayload.processTerminal = proof;
|
|
3306
|
+
// Publish the sticky proof independently of in-process child writer counts.
|
|
3307
|
+
writeAtomicJson(processTerminalPath(asyncDir), proof);
|
|
3308
|
+
};
|
|
3309
|
+
const finalizeWorktree = async (setup: WorktreeSetup, stepIndex: number, flatStartIndex: number, action: () => void, deadlineAt?: number): Promise<void> => {
|
|
3310
|
+
let admitted = false;
|
|
3311
|
+
try {
|
|
3312
|
+
await Promise.all([...externalActivityEvidence.values()].map((evidence) => evidence.probeInFlight).filter((probe): probe is Promise<void> => probe !== undefined));
|
|
3313
|
+
if (periodicGitProbeFailure) throw periodicGitProbeFailure.error;
|
|
3314
|
+
await withWorktreeTransaction(() => {
|
|
3315
|
+
if (deadlineAt !== undefined && Date.now() >= deadlineAt) throw new Error("Run deadline expired before worktree cleanup");
|
|
3316
|
+
admitted = true;
|
|
3317
|
+
action();
|
|
3318
|
+
});
|
|
3319
|
+
}
|
|
3320
|
+
catch (error) {
|
|
3321
|
+
if (admitted) throw error;
|
|
3322
|
+
const reason = `Worktree finalization retained; manual reconciliation required: ${error instanceof Error ? error.message : String(error)}`;
|
|
3323
|
+
statusPayload.parallelHandoff = writeParallelHandoffGroup({
|
|
3324
|
+
manifestPath: parallelHandoffPath(asyncDir), runId: id,
|
|
3325
|
+
mode: (config.resultMode ?? statusPayload.mode) === "parallel" ? "parallel" : (config.resultMode ?? statusPayload.mode) === "single" ? "single" : "chain",
|
|
3326
|
+
source: "async", cwd, stepIndex, flatStartIndex, setup, diffs: [], results: [],
|
|
3327
|
+
cleanup: { state: "partial", pruned: false, errors: [reason], tasks: setup.worktrees.map((worktree) => ({
|
|
3328
|
+
index: worktree.index, path: worktree.path, branch: worktree.branch, provider: worktree.provider, naming: worktree.naming,
|
|
3329
|
+
worktreeRemoved: false, branchRemoved: false, preserved: true, reason,
|
|
3330
|
+
})) },
|
|
3331
|
+
});
|
|
3332
|
+
previousOutput = [previousOutput, reason, formatParallelHandoffReference(statusPayload.parallelHandoff)].filter(Boolean).join("\n\n");
|
|
3333
|
+
writeStatusPayload();
|
|
3334
|
+
}
|
|
3335
|
+
};
|
|
3336
|
+
const cleanupRemainingWorktree = (setup: WorktreeSetup, stepIndex: number, flatStartIndex: number) => finalizeWorktree(setup, stepIndex, flatStartIndex, () => {
|
|
3337
|
+
const cleanup = cleanupWorktrees(setup);
|
|
3338
|
+
statusPayload.parallelHandoff = writeParallelHandoffGroup({
|
|
3339
|
+
manifestPath: parallelHandoffPath(asyncDir), runId: id,
|
|
3340
|
+
mode: (config.resultMode ?? statusPayload.mode) === "parallel" ? "parallel" : (config.resultMode ?? statusPayload.mode) === "single" ? "single" : "chain",
|
|
3341
|
+
source: "async", cwd, stepIndex, flatStartIndex, setup, diffs: [], results: [], cleanup,
|
|
3342
|
+
});
|
|
3343
|
+
writeStatusPayload();
|
|
3344
|
+
}, config.deadlineAt);
|
|
3142
3345
|
const interruptRunner = () => {
|
|
3143
3346
|
consumeInterruptRequest(asyncDir);
|
|
3144
3347
|
if (interrupted || statusPayload.state !== "running") return;
|
|
3145
3348
|
interrupted = true;
|
|
3349
|
+
setupInterruptController.abort();
|
|
3146
3350
|
const now = Date.now();
|
|
3147
3351
|
statusPayload.state = "paused";
|
|
3148
3352
|
currentActivityState = undefined;
|
|
@@ -3167,16 +3371,17 @@ async function runSubagent(
|
|
|
3167
3371
|
interruptActiveChildren();
|
|
3168
3372
|
};
|
|
3169
3373
|
const stopRunner = () => {
|
|
3170
|
-
if (stopped || timedOut ||
|
|
3374
|
+
if (stopped || timedOut || (statusPayload.state !== "running" && statusPayload.state !== "paused")) return;
|
|
3171
3375
|
stopped = true;
|
|
3376
|
+
interrupted = false;
|
|
3172
3377
|
const now = Date.now();
|
|
3173
3378
|
statusPayload.stopped = true;
|
|
3174
3379
|
statusPayload.error = stopMessage;
|
|
3175
3380
|
currentActivityState = undefined;
|
|
3176
3381
|
delete statusPayload.activityState;
|
|
3177
3382
|
statusPayload.lastUpdate = now;
|
|
3178
|
-
for (const step of statusPayload.steps) {
|
|
3179
|
-
if (step.status !== "running" && step.status !== "pending") continue;
|
|
3383
|
+
for (const [index, step] of statusPayload.steps.entries()) {
|
|
3384
|
+
if (step.status !== "running" && step.status !== "pending" && step.status !== "paused") continue;
|
|
3180
3385
|
step.status = "stopped";
|
|
3181
3386
|
step.error = stopMessage;
|
|
3182
3387
|
step.exitCode = 1;
|
|
@@ -3185,6 +3390,8 @@ async function runSubagent(
|
|
|
3185
3390
|
step.endedAt = now;
|
|
3186
3391
|
step.durationMs = step.startedAt ? now - step.startedAt : 0;
|
|
3187
3392
|
step.lastActivityAt = now;
|
|
3393
|
+
const result = results[index];
|
|
3394
|
+
if (result?.interrupted) results[index] = { ...result, output: stopMessage, error: stopMessage, exitCode: 1, success: false, interrupted: false, stopped: true };
|
|
3188
3395
|
}
|
|
3189
3396
|
writeStatusPayload();
|
|
3190
3397
|
appendJsonl(eventsPath, JSON.stringify({
|
|
@@ -3256,6 +3463,30 @@ async function runSubagent(
|
|
|
3256
3463
|
const remainingMs = Math.max(0, config.deadlineAt - Date.now());
|
|
3257
3464
|
timeoutTimer = setTimeout(timeoutRunner, remainingMs);
|
|
3258
3465
|
timeoutTimer.unref?.();
|
|
3466
|
+
// Route the pre-deadline checkpoint like any external steer so its lifecycle records the receipt.
|
|
3467
|
+
const checkpointBeforeDeadlineMs = config.checkpointBeforeDeadlineMs;
|
|
3468
|
+
const checkpointDelayMs = checkpointBeforeDeadlineMs === undefined || !Number.isInteger(checkpointBeforeDeadlineMs) || checkpointBeforeDeadlineMs <= 0
|
|
3469
|
+
? undefined
|
|
3470
|
+
: remainingMs - checkpointBeforeDeadlineMs;
|
|
3471
|
+
if (checkpointDelayMs !== undefined && checkpointDelayMs >= 1_000) {
|
|
3472
|
+
const deadlineAt = config.deadlineAt;
|
|
3473
|
+
checkpointTimer = setTimeout(() => {
|
|
3474
|
+
checkpointTimer = undefined;
|
|
3475
|
+
if (timedOut || stopped || interrupted) return;
|
|
3476
|
+
if (!statusPayload.steps.some((step) => step.status === "running")) return;
|
|
3477
|
+
const now = Date.now();
|
|
3478
|
+
const seconds = Math.round(Math.max(0, deadlineAt - now) / 1000);
|
|
3479
|
+
deliverSteerRequest({
|
|
3480
|
+
type: "steer",
|
|
3481
|
+
id: `deadline-checkpoint-${now}`,
|
|
3482
|
+
ts: now,
|
|
3483
|
+
mode: "steer",
|
|
3484
|
+
source: "deadline-checkpoint",
|
|
3485
|
+
message: `Deadline checkpoint from the runner: this run is killed in about ${seconds} seconds. Finish the current tool call only, then stop and reply with a handoff: changed files, build/test state, remaining work, and commit/PR state. Do not start new work.`,
|
|
3486
|
+
});
|
|
3487
|
+
}, checkpointDelayMs);
|
|
3488
|
+
checkpointTimer.unref?.();
|
|
3489
|
+
}
|
|
3259
3490
|
}
|
|
3260
3491
|
appendJsonl(
|
|
3261
3492
|
eventsPath,
|
|
@@ -3301,14 +3532,7 @@ async function runSubagent(
|
|
|
3301
3532
|
const thinkingOverride = step.thinkingOverrides?.[itemIndex];
|
|
3302
3533
|
const model = thinkingOverride ? applyThinkingSuffix(step.parallel.model, thinkingOverride, true) : step.parallel.model;
|
|
3303
3534
|
const configThinking = thinkingOverride ? thinkingOverride : step.parallel.thinking;
|
|
3304
|
-
|
|
3305
|
-
? step.parallel.modelCandidates.length > 0
|
|
3306
|
-
? step.parallel.modelCandidates.map((candidate) => thinkingOverride ? applyThinkingSuffix(candidate, thinkingOverride, true) ?? candidate : candidate)
|
|
3307
|
-
: [undefined]
|
|
3308
|
-
: model ? [model] : [undefined];
|
|
3309
|
-
for (const candidate of candidates) {
|
|
3310
|
-
assertThinkingWithinCeiling({ model: candidate, configThinking, ceiling: step.parallel.thinkingCeiling, agent: step.parallel.agent, runId: id });
|
|
3311
|
-
}
|
|
3535
|
+
assertThinkingWithinCeiling({ model, configThinking, ceiling: step.parallel.thinkingCeiling, agent: step.parallel.agent, runId: id });
|
|
3312
3536
|
}
|
|
3313
3537
|
if (materialized.collectedOnEmpty) await validateDynamicCollection(step.collect.outputSchema, materialized.collectedOnEmpty);
|
|
3314
3538
|
if (!config.runFanoutBudget) throw new Error("Async runner is missing its run fan-out budget identity.");
|
|
@@ -3376,14 +3600,14 @@ async function runSubagent(
|
|
|
3376
3600
|
cwd,
|
|
3377
3601
|
signal: combinedAbortSignal([timeoutAbortController.signal, stopAbortController.signal]),
|
|
3378
3602
|
abortMessage: stopAbortController.signal.aborted ? stopMessage : timeoutMessage ?? "Subagent timed out.",
|
|
3379
|
-
reportOptional:
|
|
3603
|
+
reportOptional: isAgentContract(step.agentContract),
|
|
3380
3604
|
}))
|
|
3381
3605
|
: undefined;
|
|
3382
3606
|
const groupStopped = stopped || stopAbortController.signal.aborted;
|
|
3383
3607
|
const groupTimedOut = !groupStopped && (timedOut || timeoutAbortController.signal.aborted);
|
|
3384
3608
|
const effectiveGroupAcceptance = groupTimedOut || groupStopped ? undefined : groupAcceptance;
|
|
3385
3609
|
if (placeholder && effectiveGroupAcceptance) placeholder.acceptance = effectiveGroupAcceptance;
|
|
3386
|
-
const groupAcceptanceFailure = effectiveGroupAcceptance && (!
|
|
3610
|
+
const groupAcceptanceFailure = effectiveGroupAcceptance && (!isAgentContract(step.agentContract) || step.gateOn === "acceptance") ? acceptanceFailureMessage(effectiveGroupAcceptance) : undefined;
|
|
3387
3611
|
if (groupTimedOut || groupStopped || groupAcceptanceFailure) {
|
|
3388
3612
|
const errorMessage = groupStopped ? stopMessage : groupTimedOut ? timeoutMessage ?? "Subagent timed out." : groupAcceptanceFailure!;
|
|
3389
3613
|
statusPayload.state = groupStopped ? "stopped" : "failed";
|
|
@@ -3441,10 +3665,6 @@ async function runSubagent(
|
|
|
3441
3665
|
...(thinkingOverride ? {
|
|
3442
3666
|
...(model ? { model } : {}),
|
|
3443
3667
|
...(thinking ? { thinking } : {}),
|
|
3444
|
-
...(step.parallel.modelCandidates ? { modelCandidates: step.parallel.modelCandidates.flatMap((candidate) => {
|
|
3445
|
-
const resolved = applyThinkingSuffix(candidate, thinkingOverride, true);
|
|
3446
|
-
return resolved ? [resolved] : [];
|
|
3447
|
-
}) } : {}),
|
|
3448
3668
|
} : {}),
|
|
3449
3669
|
structuredOutputSchema: step.parallel.structuredOutputSchema ?? step.parallel.structuredOutput?.schema,
|
|
3450
3670
|
});
|
|
@@ -3471,7 +3691,7 @@ async function runSubagent(
|
|
|
3471
3691
|
...(task.contextLimit !== undefined ? { contextLimit: task.contextLimit } : {}),
|
|
3472
3692
|
...(task.thinking ? { thinking: task.thinking } : {}),
|
|
3473
3693
|
...(task.thinkingCeiling ? { thinkingCeiling: task.thinkingCeiling } : {}),
|
|
3474
|
-
...(task.
|
|
3694
|
+
...(task.requestedModel ? { requestedModel: task.requestedModel } : {}),
|
|
3475
3695
|
recentTools: [],
|
|
3476
3696
|
recentOutput: [],
|
|
3477
3697
|
});
|
|
@@ -3585,10 +3805,12 @@ async function runSubagent(
|
|
|
3585
3805
|
nestedRoute: config.nestedRoute,
|
|
3586
3806
|
capabilityCeiling: config.capabilityCeiling,
|
|
3587
3807
|
runFanoutBudget: config.runFanoutBudget,
|
|
3808
|
+
hostAvailableBuiltins: config.hostAvailableBuiltins,
|
|
3588
3809
|
registerInterrupt: (interrupt) => registerStepInterrupt(fi, interrupt),
|
|
3589
3810
|
registerTimeout: (interrupt) => registerStepTimeout(fi, interrupt),
|
|
3590
3811
|
registerStop: (stop) => registerStepStop(fi, stop),
|
|
3591
3812
|
registerSteer: (steer) => registerStepSteer(fi, steer),
|
|
3813
|
+
onSteerOutcome: (request, delivery) => applySteerDelivery(request.id, fi, delivery),
|
|
3592
3814
|
timeoutSignal: timeoutAbortController.signal,
|
|
3593
3815
|
stopSignal: stopAbortController.signal,
|
|
3594
3816
|
trackedMutationEvidenceForCompletionGuard: false,
|
|
@@ -3598,15 +3820,18 @@ async function runSubagent(
|
|
|
3598
3820
|
onAttemptStart: (attempt) => updateStepModel(fi, attempt.model, attempt.thinking, attempt.contextLimit),
|
|
3599
3821
|
onChildEvent: (event) => updateStepFromChildEvent(fi, event),
|
|
3600
3822
|
onExternalProcess: (process) => updateExternalProcess(fi, process),
|
|
3823
|
+
prepareExternalActivity: (externalCwd, signal) => prepareExternalActivity(fi, externalCwd, signal),
|
|
3824
|
+
onExternalStreamActivity: () => recordExternalStreamActivity(fi),
|
|
3601
3825
|
onExternalJob: (externalJob) => updateExternalJob(fi, externalJob),
|
|
3602
3826
|
skipAcceptance: () => timedOut || stopped || childStopRequests.has(fi),
|
|
3603
|
-
usageBudgetExhausted:
|
|
3827
|
+
usageBudgetExhausted: continuationUsageBudgetExhausted,
|
|
3828
|
+
usageBudget: config.usageBudget,
|
|
3604
3829
|
orcaProgressTab,
|
|
3605
3830
|
}), config.deadlineAt);
|
|
3606
3831
|
const taskEndTime = Date.now();
|
|
3607
3832
|
const childInterrupted = singleResult.interrupted === true;
|
|
3608
3833
|
const childStopped = singleResult.stopped === true;
|
|
3609
|
-
requiredStatusStep(statusPayload, fi).status = stopped || childStopped ? "stopped" : timedOut ? "failed" : childInterrupted ? "paused" : singleResult.exitCode === 0 ? "complete" : "failed";
|
|
3834
|
+
requiredStatusStep(statusPayload, fi).status = stopped || childStopped ? "stopped" : timedOut ? "failed" : childInterrupted ? "paused" : singleResult.execution?.status === "partial" ? "partial" : singleResult.exitCode === 0 ? "complete" : "failed";
|
|
3610
3835
|
requiredStatusStep(statusPayload, fi).endedAt = taskEndTime;
|
|
3611
3836
|
requiredStatusStep(statusPayload, fi).durationMs = taskEndTime - taskStartTime;
|
|
3612
3837
|
requiredStatusStep(statusPayload, fi).exitCode = stopped || childStopped ? 1 : timedOut ? 1 : childInterrupted ? 0 : singleResult.exitCode;
|
|
@@ -3619,8 +3844,7 @@ async function runSubagent(
|
|
|
3619
3844
|
setOptionalProperty(requiredStatusStep(statusPayload, fi), "sessionName", singleResult.sessionName);
|
|
3620
3845
|
setOptionalProperty(requiredStatusStep(statusPayload, fi), "model", singleResult.model);
|
|
3621
3846
|
setOptionalProperty(requiredStatusStep(statusPayload, fi), "thinking", resolveEffectiveThinking(singleResult.model, requiredStatusStep(statusPayload, fi).thinking));
|
|
3622
|
-
setOptionalProperty(requiredStatusStep(statusPayload, fi), "
|
|
3623
|
-
setOptionalProperty(requiredStatusStep(statusPayload, fi), "modelAttempts", singleResult.modelAttempts);
|
|
3847
|
+
setOptionalProperty(requiredStatusStep(statusPayload, fi), "requestedModel", singleResult.requestedModel);
|
|
3624
3848
|
setOptionalProperty(requiredStatusStep(statusPayload, fi), "contextOverflow", singleResult.contextOverflow);
|
|
3625
3849
|
setOptionalProperty(requiredStatusStep(statusPayload, fi), "totalCost", singleResult.totalCost);
|
|
3626
3850
|
if (singleResult.totalCost) {
|
|
@@ -3679,7 +3903,7 @@ async function runSubagent(
|
|
|
3679
3903
|
output: pr.output,
|
|
3680
3904
|
outputState: pr.outputState,
|
|
3681
3905
|
error: pr.error,
|
|
3682
|
-
success: pr.stopped !== true && pr.interrupted !== true && pr.exitCode === 0,
|
|
3906
|
+
success: pr.stopped !== true && pr.interrupted !== true && pr.exitCode === 0 && pr.execution?.status !== "partial",
|
|
3683
3907
|
exitCode: pr.interrupted === true ? 0 : pr.exitCode,
|
|
3684
3908
|
skipped: pr.skipped,
|
|
3685
3909
|
interrupted: pr.interrupted,
|
|
@@ -3691,12 +3915,13 @@ async function runSubagent(
|
|
|
3691
3915
|
intercomTarget: pr.intercomTarget,
|
|
3692
3916
|
model: pr.model,
|
|
3693
3917
|
thinking: pr.thinking,
|
|
3694
|
-
|
|
3695
|
-
modelAttempts: pr.modelAttempts,
|
|
3918
|
+
requestedModel: pr.requestedModel,
|
|
3696
3919
|
contextOverflow: pr.contextOverflow,
|
|
3697
3920
|
totalCost: pr.totalCost,
|
|
3698
3921
|
usage: pr.usage,
|
|
3699
3922
|
artifactPaths: pr.artifactPaths,
|
|
3923
|
+
outputSaveError: pr.outputSaveError,
|
|
3924
|
+
artifactOutputSaveFailed: pr.artifactOutputSaveFailed,
|
|
3700
3925
|
transcriptPath: pr.transcriptPath,
|
|
3701
3926
|
transcriptError: pr.transcriptError,
|
|
3702
3927
|
effects: pr.effects,
|
|
@@ -3718,7 +3943,7 @@ async function runSubagent(
|
|
|
3718
3943
|
const failures = parallelResults.filter((result) => result.exitCode !== 0 && result.exitCode !== -1);
|
|
3719
3944
|
const acceptanceFailures = parallelResults
|
|
3720
3945
|
.map((result, originalIndex) => ({ result, originalIndex, task: dynamicSteps[originalIndex] }))
|
|
3721
|
-
.filter(({ result, task }) =>
|
|
3946
|
+
.filter(({ result, task }) => isAgentContract(task?.agentContract ?? step.agentContract) && task?.gateOn === "acceptance" && result.acceptance?.status === "rejected");
|
|
3722
3947
|
if (acceptanceFailures.length > 0) {
|
|
3723
3948
|
const message = acceptanceFailures
|
|
3724
3949
|
.map(({ result, originalIndex }) => `Dynamic item ${originalIndex + 1} (${result.agent}, key ${materialized.items[originalIndex]?.key ?? originalIndex}) acceptance rejected: ${(result.acceptance ? acceptanceFailureMessage(result.acceptance) : undefined) ?? "acceptance rejected"}`)
|
|
@@ -3748,13 +3973,13 @@ async function runSubagent(
|
|
|
3748
3973
|
cwd,
|
|
3749
3974
|
signal: combinedAbortSignal([timeoutAbortController.signal, stopAbortController.signal]),
|
|
3750
3975
|
abortMessage: stopAbortController.signal.aborted ? stopMessage : timeoutMessage ?? "Subagent timed out.",
|
|
3751
|
-
reportOptional:
|
|
3976
|
+
reportOptional: isAgentContract(step.agentContract),
|
|
3752
3977
|
}))
|
|
3753
3978
|
: undefined;
|
|
3754
3979
|
const groupStopped = stopped || stopAbortController.signal.aborted;
|
|
3755
3980
|
const groupTimedOut = !groupStopped && (timedOut || timeoutAbortController.signal.aborted);
|
|
3756
3981
|
const effectiveGroupAcceptance = groupTimedOut || groupStopped ? undefined : groupAcceptance;
|
|
3757
|
-
const groupAcceptanceFailure = effectiveDynamicGroupAcceptance.explicit && effectiveGroupAcceptance && (!
|
|
3982
|
+
const groupAcceptanceFailure = effectiveDynamicGroupAcceptance.explicit && effectiveGroupAcceptance && (!isAgentContract(step.agentContract) || step.gateOn === "acceptance") ? acceptanceFailureMessage(effectiveGroupAcceptance) : undefined;
|
|
3758
3983
|
const groupError = groupStopped ? stopMessage : groupTimedOut ? timeoutMessage ?? "Subagent timed out." : groupAcceptanceFailure;
|
|
3759
3984
|
markDynamicGraphGroup(stepIndex, groupError ? groupStopped ? "stopped" : "failed" : "completed", groupError, effectiveGroupAcceptance);
|
|
3760
3985
|
if (groupError) {
|
|
@@ -3833,7 +4058,9 @@ async function runSubagent(
|
|
|
3833
4058
|
break;
|
|
3834
4059
|
}
|
|
3835
4060
|
try {
|
|
3836
|
-
worktreeSetup = createWorktrees(cwd, `${id}-s${stepIndex}`, group.parallel.length, omitUndefinedProperties({
|
|
4061
|
+
worktreeSetup = await createWorktrees(cwd, `${id}-s${stepIndex}`, group.parallel.length, omitUndefinedProperties({
|
|
4062
|
+
signal: setupSignal,
|
|
4063
|
+
deadlineAt: config.deadlineAt,
|
|
3837
4064
|
agents: group.parallel.map((task) => task.agent),
|
|
3838
4065
|
labels: group.parallel.map((task) => task.lane?.key ?? config.workflowKey ?? task.outputName ?? task.label),
|
|
3839
4066
|
tasks: group.parallel.map((task) => task.task),
|
|
@@ -3844,9 +4071,10 @@ async function runSubagent(
|
|
|
3844
4071
|
? omitUndefinedProperties({ hookPath: config.worktreeSetupHook, timeoutMs: config.worktreeSetupHookTimeoutMs })
|
|
3845
4072
|
: undefined,
|
|
3846
4073
|
baseDir: config.worktreeBaseDir,
|
|
3847
|
-
|
|
3848
|
-
|
|
3849
|
-
const
|
|
4074
|
+
onProgress: (progress) => {
|
|
4075
|
+
publishSetupUnknown(progress);
|
|
4076
|
+
for (const worktree of progress.setup.worktrees) setStatusWorktreeReference(requiredStatusStep(statusPayload, groupStartFlatIndex + worktree.index), worktree);
|
|
4077
|
+
const pendingHandoff = writeWorktreeSetupHandoff({
|
|
3850
4078
|
manifestPath: parallelHandoffPath(asyncDir),
|
|
3851
4079
|
runId: id,
|
|
3852
4080
|
mode: (config.resultMode ?? statusPayload.mode) === "parallel" ? "parallel" : "chain",
|
|
@@ -3854,16 +4082,25 @@ async function runSubagent(
|
|
|
3854
4082
|
cwd,
|
|
3855
4083
|
stepIndex,
|
|
3856
4084
|
flatStartIndex: groupStartFlatIndex,
|
|
3857
|
-
|
|
4085
|
+
progress,
|
|
3858
4086
|
laneBindings: handoffWorkflowKey || config.lane ? [{ index: groupStartFlatIndex, taskIndex: 0, ...(handoffWorkflowKey ? { workflowKey: handoffWorkflowKey } : {}), ...(handoffChildRunId ? { runId: handoffChildRunId } : {}), ...(config.lane ? { lane: config.lane } : {}) }] : undefined,
|
|
3859
4087
|
});
|
|
4088
|
+
if (!pendingHandoff) return;
|
|
3860
4089
|
statusPayload.parallelHandoff = pendingHandoff;
|
|
3861
4090
|
statusPayload.lastUpdate = Date.now();
|
|
3862
4091
|
writeStatusPayload();
|
|
3863
4092
|
},
|
|
3864
4093
|
}));
|
|
4094
|
+
if (config.deadlineAt !== undefined && Date.now() >= config.deadlineAt) timeoutRunner();
|
|
4095
|
+
if (setupSignal.aborted) throw new Error(stopped ? stopMessage : timedOut ? timeoutMessage ?? "Subagent timed out." : "Subagent paused during worktree setup.");
|
|
3865
4096
|
} catch (error) {
|
|
4097
|
+
if (worktreeSetup) await cleanupRemainingWorktree(worktreeSetup, stepIndex, groupStartFlatIndex);
|
|
4098
|
+
if (config.deadlineAt !== undefined && Date.now() >= config.deadlineAt) timeoutRunner();
|
|
3866
4099
|
const setupError = error instanceof Error ? error.message : String(error);
|
|
4100
|
+
if (error instanceof WorktreeSetupError) publishSetupUnknown(error.snapshot);
|
|
4101
|
+
for (let index = groupStartFlatIndex; index < groupStartFlatIndex + group.parallel.length; index++) {
|
|
4102
|
+
if (childStopRequests.has(index)) markChildStopped(index);
|
|
4103
|
+
}
|
|
3867
4104
|
const failedAt = Date.now();
|
|
3868
4105
|
markParallelGroupSetupFailure({
|
|
3869
4106
|
statusPayload,
|
|
@@ -3984,10 +4221,12 @@ async function runSubagent(
|
|
|
3984
4221
|
nestedRoute: config.nestedRoute,
|
|
3985
4222
|
capabilityCeiling: config.capabilityCeiling,
|
|
3986
4223
|
runFanoutBudget: config.runFanoutBudget,
|
|
4224
|
+
hostAvailableBuiltins: config.hostAvailableBuiltins,
|
|
3987
4225
|
registerInterrupt: (interrupt) => registerStepInterrupt(fi, interrupt),
|
|
3988
4226
|
registerTimeout: (interrupt) => registerStepTimeout(fi, interrupt),
|
|
3989
4227
|
registerStop: (stop) => registerStepStop(fi, stop),
|
|
3990
4228
|
registerSteer: (steer) => registerStepSteer(fi, steer),
|
|
4229
|
+
onSteerOutcome: (request, delivery) => applySteerDelivery(request.id, fi, delivery),
|
|
3991
4230
|
timeoutSignal: timeoutAbortController.signal,
|
|
3992
4231
|
stopSignal: stopAbortController.signal,
|
|
3993
4232
|
trackedMutationEvidenceForCompletionGuard: Boolean(worktreeSetup),
|
|
@@ -3997,9 +4236,12 @@ async function runSubagent(
|
|
|
3997
4236
|
onAttemptStart: (attempt) => updateStepModel(fi, attempt.model, attempt.thinking, attempt.contextLimit),
|
|
3998
4237
|
onChildEvent: (event) => updateStepFromChildEvent(fi, event),
|
|
3999
4238
|
onExternalProcess: (process) => updateExternalProcess(fi, process),
|
|
4239
|
+
prepareExternalActivity: (externalCwd, signal) => prepareExternalActivity(fi, externalCwd, signal),
|
|
4240
|
+
onExternalStreamActivity: () => recordExternalStreamActivity(fi),
|
|
4000
4241
|
onExternalJob: (externalJob) => updateExternalJob(fi, externalJob),
|
|
4001
4242
|
skipAcceptance: () => timedOut || stopped || childStopRequests.has(fi),
|
|
4002
|
-
usageBudgetExhausted:
|
|
4243
|
+
usageBudgetExhausted: continuationUsageBudgetExhausted,
|
|
4244
|
+
usageBudget: config.usageBudget,
|
|
4003
4245
|
orcaProgressTab,
|
|
4004
4246
|
}), config.deadlineAt);
|
|
4005
4247
|
if (task.sessionFile) {
|
|
@@ -4011,7 +4253,7 @@ async function runSubagent(
|
|
|
4011
4253
|
const childInterrupted = singleResult.interrupted === true;
|
|
4012
4254
|
const childStopped = singleResult.stopped === true;
|
|
4013
4255
|
|
|
4014
|
-
requiredStatusStep(statusPayload, fi).status = stopped || childStopped ? "stopped" : timedOut ? "failed" : childInterrupted ? "paused" : singleResult.exitCode === 0 ? "complete" : "failed";
|
|
4256
|
+
requiredStatusStep(statusPayload, fi).status = stopped || childStopped ? "stopped" : timedOut ? "failed" : childInterrupted ? "paused" : singleResult.execution?.status === "partial" ? "partial" : singleResult.exitCode === 0 ? "complete" : "failed";
|
|
4015
4257
|
requiredStatusStep(statusPayload, fi).endedAt = taskEndTime;
|
|
4016
4258
|
requiredStatusStep(statusPayload, fi).durationMs = taskDuration;
|
|
4017
4259
|
requiredStatusStep(statusPayload, fi).exitCode = stopped || childStopped ? 1 : timedOut ? 1 : childInterrupted ? 0 : singleResult.exitCode;
|
|
@@ -4024,8 +4266,7 @@ async function runSubagent(
|
|
|
4024
4266
|
setOptionalProperty(requiredStatusStep(statusPayload, fi), "sessionName", singleResult.sessionName);
|
|
4025
4267
|
setOptionalProperty(requiredStatusStep(statusPayload, fi), "model", singleResult.model);
|
|
4026
4268
|
setOptionalProperty(requiredStatusStep(statusPayload, fi), "thinking", resolveEffectiveThinking(singleResult.model, requiredStatusStep(statusPayload, fi).thinking));
|
|
4027
|
-
setOptionalProperty(requiredStatusStep(statusPayload, fi), "
|
|
4028
|
-
setOptionalProperty(requiredStatusStep(statusPayload, fi), "modelAttempts", singleResult.modelAttempts);
|
|
4269
|
+
setOptionalProperty(requiredStatusStep(statusPayload, fi), "requestedModel", singleResult.requestedModel);
|
|
4029
4270
|
setOptionalProperty(requiredStatusStep(statusPayload, fi), "contextOverflow", singleResult.contextOverflow);
|
|
4030
4271
|
setOptionalProperty(requiredStatusStep(statusPayload, fi), "totalCost", singleResult.totalCost);
|
|
4031
4272
|
if (singleResult.totalCost) {
|
|
@@ -4093,7 +4334,7 @@ async function runSubagent(
|
|
|
4093
4334
|
const sessionTokens = config.sessionDir
|
|
4094
4335
|
? parseSessionTokens(path.join(config.sessionDir, `parallel-${t}`))
|
|
4095
4336
|
: null;
|
|
4096
|
-
const fallbackTokens =
|
|
4337
|
+
const fallbackTokens = tokenUsageFromUsage(parallelResults[t]?.usage);
|
|
4097
4338
|
const observedTokens = requiredStatusStep(statusPayload, fi).tokens;
|
|
4098
4339
|
const taskTokens = sessionTokens ?? (fallbackTokens
|
|
4099
4340
|
? { ...fallbackTokens, ...(observedTokens?.window !== undefined ? { window: observedTokens.window } : {}), ...(observedTokens?.windowPeak !== undefined ? { windowPeak: observedTokens.windowPeak } : {}) }
|
|
@@ -4125,7 +4366,7 @@ async function runSubagent(
|
|
|
4125
4366
|
output: pr.output,
|
|
4126
4367
|
outputState: pr.outputState,
|
|
4127
4368
|
error: pr.error,
|
|
4128
|
-
success: pr.stopped !== true && pr.interrupted !== true && pr.exitCode === 0,
|
|
4369
|
+
success: pr.stopped !== true && pr.interrupted !== true && pr.exitCode === 0 && pr.execution?.status !== "partial",
|
|
4129
4370
|
exitCode: pr.interrupted === true ? 0 : pr.exitCode,
|
|
4130
4371
|
skipped: pr.skipped,
|
|
4131
4372
|
interrupted: pr.interrupted,
|
|
@@ -4137,12 +4378,13 @@ async function runSubagent(
|
|
|
4137
4378
|
intercomTarget: pr.intercomTarget,
|
|
4138
4379
|
model: pr.model,
|
|
4139
4380
|
thinking: pr.thinking,
|
|
4140
|
-
|
|
4141
|
-
modelAttempts: pr.modelAttempts,
|
|
4381
|
+
requestedModel: pr.requestedModel,
|
|
4142
4382
|
contextOverflow: pr.contextOverflow,
|
|
4143
4383
|
totalCost: pr.totalCost,
|
|
4144
4384
|
usage: pr.usage,
|
|
4145
4385
|
artifactPaths: pr.artifactPaths,
|
|
4386
|
+
outputSaveError: pr.outputSaveError,
|
|
4387
|
+
artifactOutputSaveFailed: pr.artifactOutputSaveFailed,
|
|
4146
4388
|
transcriptPath: pr.transcriptPath,
|
|
4147
4389
|
transcriptError: pr.transcriptError,
|
|
4148
4390
|
effects: pr.effects,
|
|
@@ -4175,51 +4417,54 @@ async function runSubagent(
|
|
|
4175
4417
|
exitCode: r.exitCode,
|
|
4176
4418
|
error: r.error,
|
|
4177
4419
|
model: r.model,
|
|
4178
|
-
|
|
4420
|
+
requestedModel: r.requestedModel,
|
|
4179
4421
|
})),
|
|
4180
4422
|
);
|
|
4181
4423
|
if (worktreeSetup) {
|
|
4182
|
-
const
|
|
4183
|
-
if (captured.summary) previousOutput = `${previousOutput}\n\n${captured.summary}`;
|
|
4424
|
+
const setup = worktreeSetup;
|
|
4184
4425
|
worktreeFinalized = true;
|
|
4185
|
-
|
|
4186
|
-
|
|
4187
|
-
|
|
4188
|
-
|
|
4189
|
-
|
|
4190
|
-
|
|
4191
|
-
|
|
4192
|
-
|
|
4193
|
-
|
|
4194
|
-
|
|
4195
|
-
|
|
4196
|
-
|
|
4197
|
-
|
|
4198
|
-
|
|
4199
|
-
|
|
4200
|
-
|
|
4201
|
-
|
|
4202
|
-
|
|
4203
|
-
|
|
4204
|
-
|
|
4205
|
-
|
|
4206
|
-
|
|
4207
|
-
|
|
4208
|
-
|
|
4209
|
-
|
|
4210
|
-
|
|
4211
|
-
|
|
4212
|
-
|
|
4213
|
-
|
|
4214
|
-
|
|
4215
|
-
|
|
4216
|
-
|
|
4217
|
-
|
|
4218
|
-
|
|
4219
|
-
|
|
4220
|
-
|
|
4221
|
-
|
|
4222
|
-
|
|
4426
|
+
await finalizeWorktree(setup, stepIndex, groupStartFlatIndex, () => {
|
|
4427
|
+
const captured = captureParallelWorktreeDiffs(setup, asyncDir, stepIndex, group);
|
|
4428
|
+
if (captured.summary) previousOutput = `${previousOutput}\n\n${captured.summary}`;
|
|
4429
|
+
const manifestPath = parallelHandoffPath(asyncDir);
|
|
4430
|
+
const handoff = {
|
|
4431
|
+
manifestPath,
|
|
4432
|
+
runId: id,
|
|
4433
|
+
mode: (config.resultMode ?? statusPayload.mode) === "parallel" ? "parallel" as const : "chain" as const,
|
|
4434
|
+
source: "async" as const,
|
|
4435
|
+
cwd,
|
|
4436
|
+
stepIndex,
|
|
4437
|
+
flatStartIndex: groupStartFlatIndex,
|
|
4438
|
+
setup,
|
|
4439
|
+
diffs: captured.diffs,
|
|
4440
|
+
results: parallelResults.map((result) => ({
|
|
4441
|
+
agent: result.agent,
|
|
4442
|
+
...(handoffWorkflowKey ? { workflowKey: handoffWorkflowKey } : {}),
|
|
4443
|
+
...(handoffChildRunId ? { runId: handoffChildRunId } : {}),
|
|
4444
|
+
...(config.lane ? { lane: config.lane } : {}),
|
|
4445
|
+
status: result.stopped || (result.exitCode !== 0 && isUnexplainedProcessSignal(omitUndefinedProperties({
|
|
4446
|
+
processSignal: result.processSignal,
|
|
4447
|
+
interrupted: result.interrupted,
|
|
4448
|
+
timedOut: result.timedOut,
|
|
4449
|
+
stopped: result.stopped,
|
|
4450
|
+
}))) ? "stopped" as const : result.interrupted ? "paused" as const : result.exitCode === 0 ? "completed" as const : "failed" as const,
|
|
4451
|
+
summary: result.output || result.error || "(no output)",
|
|
4452
|
+
...(result.artifactPaths?.outputPath ? { outputPath: result.artifactPaths.outputPath } : {}),
|
|
4453
|
+
...(result.structuredOutput !== undefined ? { structuredOutput: result.structuredOutput } : {}),
|
|
4454
|
+
...(result.structuredOutputPath ? { structuredOutputPath: result.structuredOutputPath } : {}),
|
|
4455
|
+
...(result.sessionFile ? { sessionPath: result.sessionFile } : {}),
|
|
4456
|
+
})),
|
|
4457
|
+
};
|
|
4458
|
+
try {
|
|
4459
|
+
writeParallelHandoffGroup(handoff);
|
|
4460
|
+
const cleanup = cleanupWorktrees(setup, { kind: "preserve", capturedDiffs: captured.diffs, handoffManifestPath: manifestPath });
|
|
4461
|
+
statusPayload.parallelHandoff = writeParallelHandoffGroup({ ...handoff, cleanup });
|
|
4462
|
+
previousOutput = `${previousOutput}\n\n${formatParallelHandoffReference(statusPayload.parallelHandoff)}`;
|
|
4463
|
+
} catch (error) {
|
|
4464
|
+
previousOutput = `${previousOutput}\n\n${formatParallelHandoffError(error)}`;
|
|
4465
|
+
}
|
|
4466
|
+
writeStatusPayload();
|
|
4467
|
+
});
|
|
4223
4468
|
}
|
|
4224
4469
|
|
|
4225
4470
|
appendJsonl(eventsPath, JSON.stringify({
|
|
@@ -4228,12 +4473,12 @@ async function runSubagent(
|
|
|
4228
4473
|
runId: id,
|
|
4229
4474
|
stepIndex,
|
|
4230
4475
|
success: parallelResults.every((r) => r.exitCode === 0 || r.exitCode === -1)
|
|
4231
|
-
&& parallelResults.every((result, index) => !(
|
|
4476
|
+
&& parallelResults.every((result, index) => !(isAgentContract(group.parallel[index]?.agentContract) && group.parallel[index]?.gateOn === "acceptance" && result.acceptance?.status === "rejected")),
|
|
4232
4477
|
}));
|
|
4233
4478
|
|
|
4234
4479
|
const acceptanceGateFailure = parallelResults
|
|
4235
4480
|
.map((result, index) => ({ result, index, task: group.parallel[index] }))
|
|
4236
|
-
.find(({ result, task }) =>
|
|
4481
|
+
.find(({ result, task }) => isAgentContract(task?.agentContract) && task?.gateOn === "acceptance" && result.acceptance?.status === "rejected");
|
|
4237
4482
|
if (acceptanceGateFailure) {
|
|
4238
4483
|
statusPayload.error = (acceptanceGateFailure.result.acceptance ? acceptanceFailureMessage(acceptanceGateFailure.result.acceptance) : undefined) ?? "Parallel acceptance gate rejected the step.";
|
|
4239
4484
|
writeStatusPayload();
|
|
@@ -4243,7 +4488,7 @@ async function runSubagent(
|
|
|
4243
4488
|
break;
|
|
4244
4489
|
}
|
|
4245
4490
|
} finally {
|
|
4246
|
-
if (worktreeSetup && !worktreeFinalized)
|
|
4491
|
+
if (worktreeSetup && !worktreeFinalized) await cleanupRemainingWorktree(worktreeSetup, stepIndex, groupStartFlatIndex);
|
|
4247
4492
|
}
|
|
4248
4493
|
} else {
|
|
4249
4494
|
const seqStep = step as SubagentStep;
|
|
@@ -4270,7 +4515,9 @@ async function runSubagent(
|
|
|
4270
4515
|
let singleWorktreeSetup: WorktreeSetup | undefined;
|
|
4271
4516
|
if (seqStep.worktree) {
|
|
4272
4517
|
try {
|
|
4273
|
-
singleWorktreeSetup = createWorktrees(cwd, `${id}-s${stepIndex}`, 1, omitUndefinedProperties({
|
|
4518
|
+
singleWorktreeSetup = await createWorktrees(cwd, `${id}-s${stepIndex}`, 1, omitUndefinedProperties({
|
|
4519
|
+
signal: setupSignal,
|
|
4520
|
+
deadlineAt: config.deadlineAt,
|
|
4274
4521
|
agents: [seqStep.agent],
|
|
4275
4522
|
labels: [seqStep.lane?.key ?? config.workflowKey ?? seqStep.outputName ?? seqStep.label],
|
|
4276
4523
|
tasks: [seqStep.task],
|
|
@@ -4281,12 +4528,13 @@ async function runSubagent(
|
|
|
4281
4528
|
? omitUndefinedProperties({ hookPath: config.worktreeSetupHook, timeoutMs: config.worktreeSetupHookTimeoutMs })
|
|
4282
4529
|
: undefined,
|
|
4283
4530
|
baseDir: config.worktreeBaseDir,
|
|
4284
|
-
|
|
4285
|
-
|
|
4531
|
+
onProgress: (progress) => {
|
|
4532
|
+
publishSetupUnknown(progress);
|
|
4533
|
+
const worktree = progress.setup.worktrees[0];
|
|
4286
4534
|
if (worktree) {
|
|
4287
4535
|
setStatusWorktreeReference(requiredStatusStep(statusPayload, flatIndex), worktree);
|
|
4288
4536
|
}
|
|
4289
|
-
const pendingHandoff =
|
|
4537
|
+
const pendingHandoff = writeWorktreeSetupHandoff({
|
|
4290
4538
|
manifestPath: parallelHandoffPath(asyncDir),
|
|
4291
4539
|
runId: id,
|
|
4292
4540
|
mode: "single",
|
|
@@ -4294,19 +4542,47 @@ async function runSubagent(
|
|
|
4294
4542
|
cwd,
|
|
4295
4543
|
stepIndex,
|
|
4296
4544
|
flatStartIndex: flatIndex,
|
|
4297
|
-
|
|
4545
|
+
progress,
|
|
4298
4546
|
laneBindings: handoffWorkflowKey || config.lane ? [{ index: flatIndex, taskIndex: 0, ...(handoffWorkflowKey ? { workflowKey: handoffWorkflowKey } : {}), ...(handoffChildRunId ? { runId: handoffChildRunId } : {}), ...(config.lane ? { lane: config.lane } : {}) }] : undefined,
|
|
4299
4547
|
});
|
|
4548
|
+
if (!pendingHandoff) return;
|
|
4300
4549
|
statusPayload.parallelHandoff = pendingHandoff;
|
|
4301
4550
|
statusPayload.lastUpdate = Date.now();
|
|
4302
4551
|
writeStatusPayload();
|
|
4303
4552
|
},
|
|
4304
4553
|
}));
|
|
4305
4554
|
} catch (error) {
|
|
4306
|
-
if (
|
|
4307
|
-
|
|
4555
|
+
if (error instanceof WorktreeSetupError) publishSetupUnknown(error.snapshot);
|
|
4556
|
+
if (config.deadlineAt !== undefined && Date.now() >= config.deadlineAt) timeoutRunner();
|
|
4557
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
4558
|
+
if (childStopRequests.has(flatIndex)) markChildStopped(flatIndex);
|
|
4559
|
+
const statusStep = requiredStatusStep(statusPayload, flatIndex);
|
|
4560
|
+
statusStep.status = stopped || childStopRequests.has(flatIndex) ? "stopped" : interrupted ? "paused" : "failed";
|
|
4561
|
+
statusStep.error ??= message;
|
|
4562
|
+
statusStep.exitCode = interrupted ? 0 : 1;
|
|
4563
|
+
statusStep.endedAt = Date.now();
|
|
4564
|
+
results.push(stopped ? stoppedStepResult(seqStep.agent, seqStep.context, seqStep.sessionName)
|
|
4565
|
+
: childStopRequests.has(flatIndex) ? childStopResult(flatIndex, seqStep.agent, seqStep.context)
|
|
4566
|
+
: timedOut ? timedOutStepResult(seqStep.agent, seqStep.context, seqStep.sessionName)
|
|
4567
|
+
: interrupted ? pausedStepResult(seqStep.agent, seqStep.context, seqStep.sessionName)
|
|
4568
|
+
: { agent: seqStep.agent, output: message, error: message, exitCode: 1, success: false });
|
|
4569
|
+
statusPayload.error ??= message;
|
|
4570
|
+
writeStatusPayload();
|
|
4571
|
+
flatIndex++;
|
|
4572
|
+
break;
|
|
4308
4573
|
}
|
|
4309
4574
|
}
|
|
4575
|
+
if (singleWorktreeSetup && config.deadlineAt !== undefined && Date.now() >= config.deadlineAt) timeoutRunner();
|
|
4576
|
+
if (singleWorktreeSetup && (timedOut || stopped || interrupted || childStopRequests.has(flatIndex))) {
|
|
4577
|
+
await cleanupRemainingWorktree(singleWorktreeSetup, stepIndex, flatIndex);
|
|
4578
|
+
results.push(stopped ? stoppedStepResult(seqStep.agent, seqStep.context, seqStep.sessionName)
|
|
4579
|
+
: childStopRequests.has(flatIndex) ? childStopResult(flatIndex, seqStep.agent, seqStep.context)
|
|
4580
|
+
: timedOut ? timedOutStepResult(seqStep.agent, seqStep.context, seqStep.sessionName)
|
|
4581
|
+
: pausedStepResult(seqStep.agent, seqStep.context, seqStep.sessionName));
|
|
4582
|
+
if (interrupted) requiredStatusStep(statusPayload, flatIndex).status = "paused";
|
|
4583
|
+
flatIndex++;
|
|
4584
|
+
continue;
|
|
4585
|
+
}
|
|
4310
4586
|
const singleCwd = singleWorktreeSetup?.worktrees[0]?.agentCwd ?? cwd;
|
|
4311
4587
|
const stepStartTime = Date.now();
|
|
4312
4588
|
statusPayload.currentStep = flatIndex;
|
|
@@ -4351,10 +4627,12 @@ async function runSubagent(
|
|
|
4351
4627
|
nestedRoute: config.nestedRoute,
|
|
4352
4628
|
capabilityCeiling: config.capabilityCeiling,
|
|
4353
4629
|
runFanoutBudget: config.runFanoutBudget,
|
|
4630
|
+
hostAvailableBuiltins: config.hostAvailableBuiltins,
|
|
4354
4631
|
registerInterrupt: (interrupt) => registerStepInterrupt(flatIndex, interrupt),
|
|
4355
4632
|
registerTimeout: (interrupt) => registerStepTimeout(flatIndex, interrupt),
|
|
4356
4633
|
registerStop: (stop) => registerStepStop(flatIndex, stop),
|
|
4357
4634
|
registerSteer: (steer) => registerStepSteer(flatIndex, steer),
|
|
4635
|
+
onSteerOutcome: (request, delivery) => applySteerDelivery(request.id, flatIndex, delivery),
|
|
4358
4636
|
timeoutSignal: timeoutAbortController.signal,
|
|
4359
4637
|
stopSignal: stopAbortController.signal,
|
|
4360
4638
|
timeoutMessage,
|
|
@@ -4363,13 +4641,16 @@ async function runSubagent(
|
|
|
4363
4641
|
onAttemptStart: (attempt) => updateStepModel(flatIndex, attempt.model, attempt.thinking, attempt.contextLimit),
|
|
4364
4642
|
onChildEvent: (event) => updateStepFromChildEvent(flatIndex, event),
|
|
4365
4643
|
onExternalProcess: (process) => updateExternalProcess(flatIndex, process),
|
|
4644
|
+
prepareExternalActivity: (externalCwd, signal) => prepareExternalActivity(flatIndex, externalCwd, signal),
|
|
4645
|
+
onExternalStreamActivity: () => recordExternalStreamActivity(flatIndex),
|
|
4366
4646
|
onExternalJob: (externalJob) => updateExternalJob(flatIndex, externalJob),
|
|
4367
4647
|
skipAcceptance: () => timedOut || stopped || childStopRequests.has(flatIndex),
|
|
4368
|
-
usageBudgetExhausted:
|
|
4648
|
+
usageBudgetExhausted: continuationUsageBudgetExhausted,
|
|
4649
|
+
usageBudget: config.usageBudget,
|
|
4369
4650
|
orcaProgressTab,
|
|
4370
4651
|
}), config.deadlineAt);
|
|
4371
4652
|
} catch (error) {
|
|
4372
|
-
if (singleWorktreeSetup)
|
|
4653
|
+
if (singleWorktreeSetup) await cleanupRemainingWorktree(singleWorktreeSetup, stepIndex, flatIndex);
|
|
4373
4654
|
throw error;
|
|
4374
4655
|
}
|
|
4375
4656
|
if (seqStep.sessionFile) {
|
|
@@ -4390,18 +4671,20 @@ async function runSubagent(
|
|
|
4390
4671
|
output: stopped || childStopped ? stopMessage : timedOut ? singleResult.output || (timeoutMessage ?? "Subagent timed out.") : singleResult.output,
|
|
4391
4672
|
outputState: singleResult.outputState,
|
|
4392
4673
|
error: stopped || childStopped ? stopMessage : timedOut ? (timeoutMessage ?? "Subagent timed out.") : singleResult.error,
|
|
4393
|
-
success: !stopped && !childStopped && !timedOut && singleResult.interrupted !== true && singleResult.exitCode === 0,
|
|
4674
|
+
success: !stopped && !childStopped && !timedOut && singleResult.interrupted !== true && singleResult.exitCode === 0 && singleResult.execution?.status !== "partial",
|
|
4394
4675
|
exitCode: stopped || childStopped ? 1 : timedOut ? 1 : singleResult.interrupted === true ? 0 : singleResult.exitCode,
|
|
4395
4676
|
sessionFile: singleResult.sessionFile,
|
|
4396
4677
|
intercomTarget: singleResult.intercomTarget,
|
|
4397
4678
|
model: singleResult.model,
|
|
4398
4679
|
thinking: singleResult.thinking,
|
|
4399
|
-
|
|
4400
|
-
modelAttempts: singleResult.modelAttempts,
|
|
4680
|
+
requestedModel: singleResult.requestedModel,
|
|
4401
4681
|
contextOverflow: singleResult.contextOverflow,
|
|
4402
4682
|
totalCost: singleResult.totalCost,
|
|
4403
4683
|
usage: singleResult.usage,
|
|
4404
4684
|
artifactPaths: singleResult.artifactPaths,
|
|
4685
|
+
savedOutputPath: singleResult.savedOutputPath,
|
|
4686
|
+
outputSaveError: singleResult.outputSaveError,
|
|
4687
|
+
artifactOutputSaveFailed: singleResult.artifactOutputSaveFailed,
|
|
4405
4688
|
transcriptPath: singleResult.transcriptPath,
|
|
4406
4689
|
transcriptError: singleResult.transcriptError,
|
|
4407
4690
|
effects: singleResult.effects,
|
|
@@ -4446,7 +4729,7 @@ async function runSubagent(
|
|
|
4446
4729
|
if (cumulativeTokens) {
|
|
4447
4730
|
previousCumulativeTokens = cumulativeTokens;
|
|
4448
4731
|
} else {
|
|
4449
|
-
const fallbackTokens =
|
|
4732
|
+
const fallbackTokens = tokenUsageFromUsage(singleResult.usage);
|
|
4450
4733
|
const observedTokens = requiredStatusStep(statusPayload, flatIndex).tokens;
|
|
4451
4734
|
stepTokens = fallbackTokens
|
|
4452
4735
|
? { ...fallbackTokens, ...(observedTokens?.window !== undefined ? { window: observedTokens.window } : {}), ...(observedTokens?.windowPeak !== undefined ? { windowPeak: observedTokens.windowPeak } : {}) }
|
|
@@ -4466,7 +4749,7 @@ async function runSubagent(
|
|
|
4466
4749
|
|
|
4467
4750
|
const stepEndTime = Date.now();
|
|
4468
4751
|
const childInterrupted = singleResult.interrupted === true;
|
|
4469
|
-
requiredStatusStep(statusPayload, flatIndex).status = stopped || childStopped ? "stopped" : timedOut ? "failed" : childInterrupted ? "paused" : singleResult.exitCode === 0 ? "complete" : "failed";
|
|
4752
|
+
requiredStatusStep(statusPayload, flatIndex).status = stopped || childStopped ? "stopped" : timedOut ? "failed" : childInterrupted ? "paused" : singleResult.execution?.status === "partial" ? "partial" : singleResult.exitCode === 0 ? "complete" : "failed";
|
|
4470
4753
|
requiredStatusStep(statusPayload, flatIndex).endedAt = stepEndTime;
|
|
4471
4754
|
requiredStatusStep(statusPayload, flatIndex).durationMs = stepEndTime - stepStartTime;
|
|
4472
4755
|
requiredStatusStep(statusPayload, flatIndex).exitCode = stopped || childStopped ? 1 : timedOut ? 1 : childInterrupted ? 0 : singleResult.exitCode;
|
|
@@ -4479,8 +4762,7 @@ async function runSubagent(
|
|
|
4479
4762
|
setOptionalProperty(requiredStatusStep(statusPayload, flatIndex), "sessionName", singleResult.sessionName);
|
|
4480
4763
|
setOptionalProperty(requiredStatusStep(statusPayload, flatIndex), "model", singleResult.model);
|
|
4481
4764
|
setOptionalProperty(requiredStatusStep(statusPayload, flatIndex), "thinking", resolveEffectiveThinking(singleResult.model, requiredStatusStep(statusPayload, flatIndex).thinking));
|
|
4482
|
-
setOptionalProperty(requiredStatusStep(statusPayload, flatIndex), "
|
|
4483
|
-
setOptionalProperty(requiredStatusStep(statusPayload, flatIndex), "modelAttempts", singleResult.modelAttempts);
|
|
4765
|
+
setOptionalProperty(requiredStatusStep(statusPayload, flatIndex), "requestedModel", singleResult.requestedModel);
|
|
4484
4766
|
setOptionalProperty(requiredStatusStep(statusPayload, flatIndex), "contextOverflow", singleResult.contextOverflow);
|
|
4485
4767
|
setOptionalProperty(requiredStatusStep(statusPayload, flatIndex), "totalCost", singleResult.totalCost);
|
|
4486
4768
|
setOptionalProperty(requiredStatusStep(statusPayload, flatIndex), "error", stopped || childStopped ? stopMessage : timedOut ? (timeoutMessage ?? "Subagent timed out.") : singleResult.error);
|
|
@@ -4522,48 +4804,51 @@ async function runSubagent(
|
|
|
4522
4804
|
}));
|
|
4523
4805
|
if (stopped || childStopped) appendTerminalChildStatusEvent(flatIndex, stepEndTime);
|
|
4524
4806
|
if (singleWorktreeSetup && !singleResult.detached) {
|
|
4525
|
-
const
|
|
4526
|
-
|
|
4527
|
-
|
|
4528
|
-
|
|
4529
|
-
manifestPath
|
|
4530
|
-
|
|
4531
|
-
|
|
4532
|
-
|
|
4533
|
-
|
|
4534
|
-
|
|
4535
|
-
|
|
4536
|
-
|
|
4537
|
-
|
|
4538
|
-
|
|
4539
|
-
|
|
4540
|
-
|
|
4541
|
-
|
|
4542
|
-
|
|
4543
|
-
|
|
4544
|
-
|
|
4545
|
-
|
|
4546
|
-
|
|
4547
|
-
|
|
4548
|
-
|
|
4549
|
-
|
|
4550
|
-
|
|
4551
|
-
|
|
4552
|
-
|
|
4553
|
-
|
|
4554
|
-
|
|
4555
|
-
|
|
4556
|
-
|
|
4557
|
-
|
|
4558
|
-
|
|
4559
|
-
|
|
4560
|
-
|
|
4561
|
-
|
|
4562
|
-
|
|
4563
|
-
|
|
4564
|
-
|
|
4565
|
-
|
|
4566
|
-
|
|
4807
|
+
const setup = singleWorktreeSetup;
|
|
4808
|
+
await finalizeWorktree(setup, stepIndex, flatIndex, () => {
|
|
4809
|
+
const diffs = diffWorktrees(setup, [seqStep.agent], path.join(asyncDir, "worktree-diffs", `step-${stepIndex}`));
|
|
4810
|
+
const diffSummary = formatWorktreeDiffSummary(diffs);
|
|
4811
|
+
const manifestPath = parallelHandoffPath(asyncDir);
|
|
4812
|
+
const handoff = {
|
|
4813
|
+
manifestPath,
|
|
4814
|
+
runId: id,
|
|
4815
|
+
mode: "single" as const,
|
|
4816
|
+
source: "async" as const,
|
|
4817
|
+
cwd,
|
|
4818
|
+
stepIndex,
|
|
4819
|
+
flatStartIndex: flatIndex,
|
|
4820
|
+
setup,
|
|
4821
|
+
diffs,
|
|
4822
|
+
results: [{
|
|
4823
|
+
agent: singleResult.agent,
|
|
4824
|
+
...(handoffWorkflowKey ? { workflowKey: handoffWorkflowKey } : {}),
|
|
4825
|
+
...(handoffChildRunId ? { runId: handoffChildRunId } : {}),
|
|
4826
|
+
...(config.lane ? { lane: config.lane } : {}),
|
|
4827
|
+
status: singleResult.stopped ? "stopped" as const : singleResult.interrupted ? "paused" as const : singleResult.exitCode === 0 ? "completed" as const : "failed" as const,
|
|
4828
|
+
summary: singleResult.output || singleResult.error || "(no output)",
|
|
4829
|
+
...(singleResult.artifactPaths?.outputPath ? { outputPath: singleResult.artifactPaths.outputPath } : {}),
|
|
4830
|
+
...(singleResult.structuredOutput !== undefined ? { structuredOutput: singleResult.structuredOutput } : {}),
|
|
4831
|
+
...(singleResult.structuredOutputPath ? { structuredOutputPath: singleResult.structuredOutputPath } : {}),
|
|
4832
|
+
...(singleResult.sessionFile ? { sessionPath: singleResult.sessionFile } : {}),
|
|
4833
|
+
}],
|
|
4834
|
+
};
|
|
4835
|
+
try {
|
|
4836
|
+
writeParallelHandoffGroup(handoff);
|
|
4837
|
+
const cleanup = cleanupWorktrees(setup, {
|
|
4838
|
+
kind: "preserve",
|
|
4839
|
+
capturedDiffs: diffs,
|
|
4840
|
+
handoffManifestPath: manifestPath,
|
|
4841
|
+
...(config.parentWorkflowRunId && singleResult.sessionFile && fs.existsSync(singleResult.sessionFile) && !singleResult.stopped
|
|
4842
|
+
? { cleanupBlocker: "retained child resume requires managed worktree cwd" }
|
|
4843
|
+
: {}),
|
|
4844
|
+
});
|
|
4845
|
+
statusPayload.parallelHandoff = writeParallelHandoffGroup({ ...handoff, cleanup });
|
|
4846
|
+
previousOutput = [previousOutput, diffSummary, formatParallelHandoffReference(statusPayload.parallelHandoff)].filter(Boolean).join("\n\n");
|
|
4847
|
+
} catch (error) {
|
|
4848
|
+
previousOutput = [previousOutput, diffSummary, formatParallelHandoffError(error)].filter(Boolean).join("\n\n");
|
|
4849
|
+
}
|
|
4850
|
+
writeStatusPayload();
|
|
4851
|
+
});
|
|
4567
4852
|
}
|
|
4568
4853
|
|
|
4569
4854
|
if (singleResult.completionGuardTriggered) {
|
|
@@ -4581,7 +4866,7 @@ async function runSubagent(
|
|
|
4581
4866
|
}
|
|
4582
4867
|
|
|
4583
4868
|
flatIndex++;
|
|
4584
|
-
if (
|
|
4869
|
+
if (isAgentContract(seqStep.agentContract) && seqStep.gateOn === "acceptance" && singleResult.acceptance?.status === "rejected") {
|
|
4585
4870
|
statusPayload.error = acceptanceFailureMessage(singleResult.acceptance) ?? "Chain acceptance gate rejected the step.";
|
|
4586
4871
|
writeStatusPayload();
|
|
4587
4872
|
break;
|
|
@@ -4592,6 +4877,34 @@ async function runSubagent(
|
|
|
4592
4877
|
}
|
|
4593
4878
|
}
|
|
4594
4879
|
|
|
4880
|
+
await Promise.all([...externalActivityEvidence.values()].map((evidence) => evidence.probeInFlight).filter((probe): probe is Promise<void> => probe !== undefined));
|
|
4881
|
+
if (periodicGitProbeFailure) {
|
|
4882
|
+
stopped = false;
|
|
4883
|
+
timedOut = false;
|
|
4884
|
+
interrupted = false;
|
|
4885
|
+
delete statusPayload.stopped;
|
|
4886
|
+
delete statusPayload.timedOut;
|
|
4887
|
+
statusPayload.error = periodicGitProbeFailure.message;
|
|
4888
|
+
const step = statusPayload.steps[periodicGitProbeFailure.index];
|
|
4889
|
+
if (step) {
|
|
4890
|
+
step.status = "failed";
|
|
4891
|
+
step.error = periodicGitProbeFailure.message;
|
|
4892
|
+
step.exitCode = 1;
|
|
4893
|
+
delete step.stopped;
|
|
4894
|
+
delete step.timedOut;
|
|
4895
|
+
}
|
|
4896
|
+
const result = results[periodicGitProbeFailure.index];
|
|
4897
|
+
if (result) {
|
|
4898
|
+
result.success = false;
|
|
4899
|
+
result.exitCode = 1;
|
|
4900
|
+
result.error = periodicGitProbeFailure.message;
|
|
4901
|
+
result.output = periodicGitProbeFailure.message;
|
|
4902
|
+
result.stopped = false;
|
|
4903
|
+
result.timedOut = false;
|
|
4904
|
+
result.interrupted = false;
|
|
4905
|
+
}
|
|
4906
|
+
}
|
|
4907
|
+
|
|
4595
4908
|
let summary = results.map((r) => `${r.agent}:\n${r.output || (r.exitCode !== 0 ? r.error : undefined) || "(no output)"}`).join("\n\n");
|
|
4596
4909
|
let truncated = false;
|
|
4597
4910
|
|
|
@@ -4657,9 +4970,15 @@ async function runSubagent(
|
|
|
4657
4970
|
clearTimeout(timeoutTimer);
|
|
4658
4971
|
timeoutTimer = undefined;
|
|
4659
4972
|
}
|
|
4973
|
+
if (checkpointTimer) {
|
|
4974
|
+
clearTimeout(checkpointTimer);
|
|
4975
|
+
checkpointTimer = undefined;
|
|
4976
|
+
}
|
|
4660
4977
|
if (!timedOut && !stopped && !interrupted && config.timeoutMs !== undefined && timeoutMessage !== undefined && results.some((result) => result.timedOut === true && result.error?.startsWith(timeoutMessage))) {
|
|
4661
4978
|
timedOut = true;
|
|
4662
4979
|
}
|
|
4980
|
+
disposeControlInbox();
|
|
4981
|
+
for (const request of consumeStopRequestPayloads(asyncDir)) stopChildStep(request);
|
|
4663
4982
|
const signalTerminated = !stopped && !timedOut && !interrupted && results.some((result) => result.exitCode !== 0 && isUnexplainedProcessSignal(omitUndefinedProperties({
|
|
4664
4983
|
processSignal: result.processSignal,
|
|
4665
4984
|
interrupted: result.interrupted,
|
|
@@ -4667,9 +4986,13 @@ async function runSubagent(
|
|
|
4667
4986
|
stopped: result.stopped,
|
|
4668
4987
|
})));
|
|
4669
4988
|
const partialWithEvidence = !stopped && !signalTerminated && !timedOut && !usageBudgetExceeded && !interrupted && results.some(partialEvidenceResult) && !results.some(concreteFailureResult);
|
|
4989
|
+
// Flush while still nonterminal; deferred status retries retain that state snapshot.
|
|
4990
|
+
statusWriteCoalescer.flush(statusPath);
|
|
4991
|
+
const publication = new Promise<void>((resolve, reject) => {
|
|
4992
|
+
finalResultPublication = { resolve, reject };
|
|
4993
|
+
});
|
|
4670
4994
|
statusPayload.state = stopped || signalTerminated ? "stopped" : timedOut || usageBudgetExceeded ? "failed" : interrupted ? "paused" : results.every((r) => r.success) ? "complete" : partialWithEvidence ? "partial" : "failed";
|
|
4671
4995
|
closeSteerInbox(asyncDir, statusPayload.state, (filePath, payload) => runPersistence.write(filePath, payload));
|
|
4672
|
-
disposeControlInbox();
|
|
4673
4996
|
for (const request of consumeSteerRequests(asyncDir)) deliverSteerRequest(request);
|
|
4674
4997
|
const effectiveSessionFile = sessionFile ?? latestSessionFile;
|
|
4675
4998
|
const steeringLifecycle = steeringStatus(statusPayload);
|
|
@@ -4678,7 +5001,7 @@ async function runSubagent(
|
|
|
4678
5001
|
for (const target of request.targets) {
|
|
4679
5002
|
if (target.state !== "scheduled" && target.state !== "routed" && target.state !== "queued") continue;
|
|
4680
5003
|
changed = true;
|
|
4681
|
-
const reason = target.state === "queued" ?
|
|
5004
|
+
const reason = target.state === "queued" ? unconsumedSteerReason() : "child terminated before steering delivery";
|
|
4682
5005
|
updateSteeringLifecycleTarget(request.id, target.index, "failed", Date.now(), { reason });
|
|
4683
5006
|
markSteeringAttention(target.index);
|
|
4684
5007
|
emitSteeringEvent("subagent.steer.failed", { type: "steer", id: request.id, ts: request.requestedAt, message: reason }, target.index, { reason });
|
|
@@ -4705,7 +5028,7 @@ async function runSubagent(
|
|
|
4705
5028
|
statusPayload.activityState = "needs_attention";
|
|
4706
5029
|
statusPayload.error = partialResult?.error ?? statusPayload.error;
|
|
4707
5030
|
for (const step of statusPayload.steps) {
|
|
4708
|
-
if (step.status === "failed") step.activityState = "needs_attention";
|
|
5031
|
+
if (step.status === "failed" || step.status === "partial") step.activityState = "needs_attention";
|
|
4709
5032
|
}
|
|
4710
5033
|
}
|
|
4711
5034
|
statusPayload.endedAt = runEndedAt;
|
|
@@ -4725,6 +5048,9 @@ async function runSubagent(
|
|
|
4725
5048
|
statusPayload.error = `Step failed: ${failedStep.agent}`;
|
|
4726
5049
|
}
|
|
4727
5050
|
}
|
|
5051
|
+
let childSessionDisposal: Promise<void> | undefined;
|
|
5052
|
+
const disposeChildSessions = (): Promise<void> => childSessionDisposal ??= childSessions.dispose()
|
|
5053
|
+
.catch((error: unknown) => console.error("Failed to dispose runner child sessions:", error));
|
|
4728
5054
|
try {
|
|
4729
5055
|
runPersistence.write(resultPath, {
|
|
4730
5056
|
lifecycleArtifactVersion: SUBAGENT_LIFECYCLE_ARTIFACT_VERSION,
|
|
@@ -4759,13 +5085,14 @@ async function runSubagent(
|
|
|
4759
5085
|
intercomTarget: r.intercomTarget,
|
|
4760
5086
|
model: r.model,
|
|
4761
5087
|
thinking: r.thinking,
|
|
4762
|
-
|
|
4763
|
-
modelAttempts: r.modelAttempts,
|
|
5088
|
+
requestedModel: r.requestedModel,
|
|
4764
5089
|
contextOverflow: r.contextOverflow,
|
|
4765
5090
|
totalCost: r.totalCost,
|
|
4766
5091
|
usage: r.usage,
|
|
4767
5092
|
artifactPaths: r.artifactPaths,
|
|
5093
|
+
savedOutputPath: r.savedOutputPath,
|
|
4768
5094
|
outputSaveError: r.outputSaveError,
|
|
5095
|
+
artifactOutputSaveFailed: r.artifactOutputSaveFailed,
|
|
4769
5096
|
metadataSaveError: r.metadataSaveError,
|
|
4770
5097
|
truncated: r.truncated,
|
|
4771
5098
|
transcriptPath: r.transcriptPath,
|
|
@@ -4820,13 +5147,16 @@ async function runSubagent(
|
|
|
4820
5147
|
...(taskIndex !== undefined && { taskIndex }),
|
|
4821
5148
|
...(totalTasks !== undefined && { totalTasks }),
|
|
4822
5149
|
}, (filePath, payload) => { writeAsyncResultFile(filePath, payload as Record<string, unknown>); });
|
|
4823
|
-
|
|
5150
|
+
// Only capacity deferral releases settled sessions before terminal publication.
|
|
5151
|
+
if (!finalResultCommitted) await Promise.all([publication, disposeChildSessions()]);
|
|
4824
5152
|
} catch (err) {
|
|
4825
5153
|
const message = `Failed to write result file ${resultPath}: ${err instanceof Error ? err.message : String(err)}`;
|
|
4826
5154
|
console.error(message, err);
|
|
4827
5155
|
statusPayload.state = "failed";
|
|
4828
5156
|
statusPayload.error = message;
|
|
4829
5157
|
statusPayload.lastUpdate = Date.now();
|
|
5158
|
+
} finally {
|
|
5159
|
+
finalResultPublication = undefined;
|
|
4830
5160
|
}
|
|
4831
5161
|
writeStatusPayload();
|
|
4832
5162
|
await orcaProgressTab?.finish(statusPayload.state === "complete" ? "completed" : statusPayload.state === "stopped" ? "stopped" : "failed", effectiveSessionFile);
|
|
@@ -4864,9 +5194,8 @@ async function runSubagent(
|
|
|
4864
5194
|
}), (filePath, content) => runPersistence.write(filePath, { content }, (_path, payload) => {
|
|
4865
5195
|
fs.writeFileSync(_path, (payload as { content: string }).content, "utf-8");
|
|
4866
5196
|
}));
|
|
4867
|
-
//
|
|
4868
|
-
|
|
4869
|
-
await childSessions.dispose().catch((error: unknown) => console.error("Failed to dispose runner child sessions:", error));
|
|
5197
|
+
// Preserve normal-success disposal ordering, then drain remaining persistence retries.
|
|
5198
|
+
await disposeChildSessions();
|
|
4870
5199
|
while (runPersistence.pendingCount() + indexPersistence.pendingCount() > 0) {
|
|
4871
5200
|
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
4872
5201
|
}
|
|
@@ -4874,7 +5203,7 @@ async function runSubagent(
|
|
|
4874
5203
|
indexPersistence.dispose();
|
|
4875
5204
|
if (config.runnerProcessInstanceId) {
|
|
4876
5205
|
// Children run inside this process, so no step has writer processes to prove terminal.
|
|
4877
|
-
const writers: Record<string,
|
|
5206
|
+
const writers: Record<string, PiWriterProcessInstanceExit[]> = {};
|
|
4878
5207
|
const expectedWriters: Record<string, number> = {};
|
|
4879
5208
|
for (const index of results.keys()) {
|
|
4880
5209
|
writers[String(index)] = [];
|
|
@@ -4900,7 +5229,7 @@ async function runSubagent(
|
|
|
4900
5229
|
async function waitForStartupControl(
|
|
4901
5230
|
controlPath: string,
|
|
4902
5231
|
token: string,
|
|
4903
|
-
action: "ack" | "proceed",
|
|
5232
|
+
action: "ack" | "confirm" | "proceed",
|
|
4904
5233
|
timeoutMs = 30_000,
|
|
4905
5234
|
): Promise<void> {
|
|
4906
5235
|
const deadline = Date.now() + timeoutMs;
|
|
@@ -4914,14 +5243,15 @@ async function waitForStartupControl(
|
|
|
4914
5243
|
}
|
|
4915
5244
|
if (payload.token !== token) throw new Error("Runner startup control token does not match.");
|
|
4916
5245
|
if (payload.action === action) return;
|
|
4917
|
-
if (payload.action !== "ack" && payload.action !== "proceed") throw new Error("Runner startup control action is invalid.");
|
|
5246
|
+
if (payload.action !== "ack" && payload.action !== "confirm" && payload.action !== "proceed") throw new Error("Runner startup control action is invalid.");
|
|
4918
5247
|
}
|
|
4919
5248
|
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
4920
5249
|
}
|
|
4921
5250
|
throw new Error(`Timed out after ${timeoutMs}ms waiting for runner startup control '${action}'.`);
|
|
4922
5251
|
}
|
|
4923
5252
|
|
|
4924
|
-
|
|
5253
|
+
// Both hosts enter here: startup authorization, revival leases and disposal stay shared.
|
|
5254
|
+
export async function runConfiguredSubagent(config: SubagentRunConfig, options?: DefaultChildSessionFactoryOptions): Promise<void> {
|
|
4925
5255
|
let lease: ReturnType<typeof acquireSessionLease> | undefined;
|
|
4926
5256
|
let startupCommitted = config.revivalLease === undefined && config.launchBarrierToken === undefined;
|
|
4927
5257
|
const startupPath = path.join(config.asyncDir, "runner-startup.json");
|
|
@@ -4950,6 +5280,8 @@ async function runConfiguredSubagent(config: SubagentRunConfig): Promise<void> {
|
|
|
4950
5280
|
writeAtomicJson(startupPath, { state: "ready", token: lease.owner.token, pid: process.pid, owner: lease.owner });
|
|
4951
5281
|
await waitForStartupControl(startupAckPath, lease.owner.token, "ack");
|
|
4952
5282
|
writeAtomicJson(startupPath, { state: "acknowledged", token: lease.owner.token, pid: process.pid });
|
|
5283
|
+
await waitForStartupControl(startupAckPath, lease.owner.token, "confirm");
|
|
5284
|
+
writeAtomicJson(startupPath, { state: "confirmed", token: lease.owner.token, pid: process.pid });
|
|
4953
5285
|
await waitForStartupControl(startupProceedPath, lease.owner.token, "proceed");
|
|
4954
5286
|
startupCommitted = true;
|
|
4955
5287
|
for (const controlPath of [startupAckPath, startupProceedPath]) {
|
|
@@ -4960,7 +5292,7 @@ async function runConfiguredSubagent(config: SubagentRunConfig): Promise<void> {
|
|
|
4960
5292
|
}
|
|
4961
5293
|
}
|
|
4962
5294
|
}
|
|
4963
|
-
const childSessions = await loadRunnerChildSessionFactory(config);
|
|
5295
|
+
const childSessions = await loadRunnerChildSessionFactory(config, options);
|
|
4964
5296
|
try {
|
|
4965
5297
|
await runSubagent(config, childSessions);
|
|
4966
5298
|
} finally {
|
|
@@ -5010,6 +5342,7 @@ function startConfiguredSubagent(config: SubagentRunConfig): void {
|
|
|
5010
5342
|
);
|
|
5011
5343
|
}
|
|
5012
5344
|
|
|
5345
|
+
if (isRunnerEntrypoint) {
|
|
5013
5346
|
const configArg = process.argv[2];
|
|
5014
5347
|
if (configArg) {
|
|
5015
5348
|
try {
|
|
@@ -5041,3 +5374,4 @@ if (configArg) {
|
|
|
5041
5374
|
}
|
|
5042
5375
|
});
|
|
5043
5376
|
}
|
|
5377
|
+
}
|