@xzzpig/pi-subagents 0.4.2 → 0.5.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 +168 -0
- package/README.md +4 -0
- package/agents/oracle.md +3 -1
- package/agents/reviewer.md +1 -0
- package/agents/scout.md +2 -2
- package/agents/worker.md +2 -1
- package/async-retention-discovery-worker.mjs +180 -0
- package/docs/agents.md +40 -2
- package/docs/configuration.md +41 -13
- package/docs/extension-api.md +45 -2
- package/docs/models.md +2 -0
- package/docs/observability.md +41 -5
- package/docs/tool-reference.md +38 -39
- package/docs/workflows.md +173 -3
- package/package.json +13 -10
- package/prompts/council.md +48 -0
- package/skills/council-mode/SKILL.md +230 -0
- package/skills/pi-subagents/SKILL.md +8 -4
- package/skills/pi-subagents/references/constraints-and-recipes.md +13 -6
- package/skills/pi-subagents/references/execution-controls.md +40 -15
- package/skills/pi-subagents/references/management-authoring-rpc.md +3 -3
- package/skills/pi-subagents/references/multi-lane-orchestration.md +39 -0
- package/skills/pi-subagents/references/prompting-and-roles.md +4 -2
- package/src/agents/agent-management.ts +158 -357
- package/src/agents/agent-serializer.ts +2 -0
- package/src/agents/agents.ts +186 -44
- package/src/agents/builtin-names.ts +9 -0
- package/src/agents/runtime-agent-registry.ts +418 -0
- package/src/agents/skills.ts +1 -1
- package/src/api/agents.ts +7 -0
- package/src/api/external-job-provider.ts +186 -0
- package/src/api/preflight.ts +37 -9
- package/src/api/shared-types.ts +2 -0
- package/src/extension/config.ts +6 -3
- package/src/extension/doctor.ts +4 -6
- package/src/extension/fanout-child.ts +2 -2
- package/src/extension/index.ts +183 -90
- package/src/extension/public-execution.ts +31 -2
- package/src/extension/rpc.ts +41 -1
- package/src/extension/schemas.ts +17 -37
- package/src/extension/tool-description.ts +35 -24
- package/src/inspectors/herdr/actions.ts +2 -2
- package/src/inspectors/herdr/inspector-runner.ts +2 -1
- package/src/inspectors/herdr/project-panes.ts +2 -2
- package/src/intercom/native-supervisor-channel.ts +32 -10
- package/src/missions/lifecycle.ts +6 -1
- package/src/missions/store.ts +4 -9
- package/src/missions/workflow-state.ts +2 -2
- package/src/profiles/profiles.ts +3 -1
- package/src/runs/background/active-run-index.ts +31 -8
- package/src/runs/background/async-execution.ts +70 -41
- package/src/runs/background/async-job-tracker.ts +24 -7
- package/src/runs/background/async-resume.ts +49 -16
- package/src/runs/background/async-retention.ts +886 -0
- package/src/runs/background/async-status-snapshot.ts +14 -5
- package/src/runs/background/async-status.ts +39 -53
- package/src/runs/background/auto-drain.ts +1 -0
- package/src/runs/background/chain-append.ts +3 -33
- package/src/runs/background/completion-dedupe.ts +5 -1
- package/src/runs/background/completion-replay.ts +22 -12
- package/src/runs/background/control-channel.ts +14 -68
- package/src/runs/background/fleet-view.ts +68 -19
- package/src/runs/background/index-segment.ts +59 -0
- package/src/runs/background/inspect-rpc.ts +443 -0
- package/src/runs/background/notify.ts +31 -5
- package/src/runs/background/result-files.ts +158 -90
- package/src/runs/background/result-watcher.ts +124 -20
- package/src/runs/background/resume-guidance.ts +8 -5
- package/src/runs/background/retained-children.ts +13 -3
- package/src/runs/background/run-id-query.ts +7 -0
- package/src/runs/background/run-id-resolver.ts +11 -9
- package/src/runs/background/run-status.ts +27 -18
- package/src/runs/background/scheduled-runs.ts +24 -4
- package/src/runs/background/stale-run-reconciler.ts +6 -3
- package/src/runs/background/steering.ts +11 -1
- package/src/runs/background/subagent-runner.ts +260 -144
- package/src/runs/background/subagent-wait.ts +17 -13
- package/src/runs/background/terminal-run-index.ts +138 -0
- package/src/runs/background/wait-completions.ts +21 -4
- package/src/runs/background/wait-subscriptions.ts +81 -2
- package/src/runs/background/wait-tool.ts +1 -0
- package/src/runs/foreground/async-steering-action.ts +21 -14
- package/src/runs/foreground/execution.ts +8 -2
- package/src/runs/foreground/subagent-executor.ts +816 -1593
- package/src/runs/foreground/workflow-detach-reconcile.ts +262 -0
- package/src/runs/foreground/workflow-foreground-steering.ts +6 -5
- package/src/runs/shared/acceptance.ts +48 -5
- package/src/runs/shared/chain-outputs.ts +1 -3
- package/src/runs/shared/external-job-bridge.ts +444 -0
- package/src/runs/shared/external-job-runner.ts +286 -0
- package/src/runs/shared/mcp-direct-tool-allowlist.ts +14 -0
- package/src/runs/shared/model-exclusions.ts +242 -0
- package/src/runs/shared/model-fallback.ts +153 -40
- package/src/runs/shared/orca-progress-tabs.ts +84 -22
- package/src/runs/shared/parallel-handoff.ts +46 -4
- package/src/runs/shared/parallel-utils.ts +4 -15
- package/src/runs/shared/permissions.ts +5 -1
- package/src/runs/shared/pi-args.ts +8 -1
- package/src/runs/shared/session-lease.ts +0 -6
- package/src/runs/shared/subagent-control.ts +51 -7
- package/src/runs/shared/subagent-prompt-runtime.ts +12 -6
- package/src/runs/shared/workflow-graph.ts +1 -23
- package/src/runs/shared/worktree.ts +14 -2
- package/src/shared/atomic-json.ts +22 -2
- package/src/shared/capacity-resilient-json.ts +102 -0
- package/src/shared/completion-owner.ts +14 -0
- package/src/shared/file-system-retry.ts +49 -1
- package/src/shared/fork-context.ts +59 -1
- package/src/shared/model-info.ts +20 -0
- package/src/shared/prompt-resources.ts +0 -40
- package/src/shared/settings.ts +5 -29
- package/src/shared/types.ts +106 -26
- package/src/shared/utils.ts +8 -0
- package/src/shared/watch-strategy.ts +10 -0
- package/src/slash/slash-bridge.ts +2 -1
- package/src/slash/slash-commands.ts +47 -7
- package/src/slash/slash-live-state.ts +3 -3
- package/src/tui/fleet-status.ts +86 -1
- package/src/tui/fleet.ts +117 -16
- package/src/tui/render.ts +73 -3
- package/src/watchdog/change-signature.ts +1 -1
- package/src/watchdog/lsp-diagnostics.ts +1 -0
- package/src/workflows/chat-progress.ts +2 -2
- package/src/workflows/scripted-workflow.ts +320 -79
- package/src/workflows/workflow-receipt.ts +140 -0
- package/src/runs/foreground/chain-clarify.ts +0 -1354
- package/src/runs/foreground/chain-execution.ts +0 -1581
|
@@ -7,7 +7,9 @@ import type { Message } from "@earendil-works/pi-ai";
|
|
|
7
7
|
import { writeAtomicJson } from "../../shared/atomic-json.ts";
|
|
8
8
|
import { writeAsyncResultFile, writePendingAsyncResultFile } from "./result-files.ts";
|
|
9
9
|
import { createFileCoalescer } from "../../shared/file-coalescer.ts";
|
|
10
|
-
import {
|
|
10
|
+
import { createCapacityResilientJsonWriter } from "../../shared/capacity-resilient-json.ts";
|
|
11
|
+
import { isStorageCapacityError } from "../../shared/file-system-retry.ts";
|
|
12
|
+
import { updateActiveRunIndex } from "./active-run-index.ts";
|
|
11
13
|
import { createChildTranscriptWriter, type ChildTranscriptWriter } from "../../shared/child-transcript.ts";
|
|
12
14
|
import { closeSteerInbox, consumeInterruptRequest, consumeSteerRequests, deliverInterruptRequest, deliverStopRequest, deliverTimeoutRequest, enqueueStepSteer, steerAcksDir, steerCapabilityPath, stepSteerInboxDir, watchAsyncControlInbox, type SteerAck, type SteerCapability, type SteerRequest } from "./control-channel.ts";
|
|
13
15
|
import { appendJsonl as appendRawJsonl, formatOutputArtifactContent, getArtifactPaths, writeArtifact, writeMetadata } from "../../shared/artifacts.ts";
|
|
@@ -17,6 +19,8 @@ import {
|
|
|
17
19
|
type ActivityState,
|
|
18
20
|
type ArtifactConfig,
|
|
19
21
|
type ExternalCliRunnerStatus,
|
|
22
|
+
type ExternalJobRunnerStatus,
|
|
23
|
+
type ExternalJobStatus,
|
|
20
24
|
type ExternalProcessStatus,
|
|
21
25
|
type ArtifactPaths,
|
|
22
26
|
type AsyncParallelGroupStatus,
|
|
@@ -63,7 +67,6 @@ import {
|
|
|
63
67
|
import {
|
|
64
68
|
type RunnerSubagentStep as SubagentStep,
|
|
65
69
|
type RunnerStep,
|
|
66
|
-
isCheckpointRunnerStep,
|
|
67
70
|
isDynamicRunnerGroup,
|
|
68
71
|
isParallelGroup,
|
|
69
72
|
flattenSteps,
|
|
@@ -82,7 +85,7 @@ import { readChildToolDiagnosticError } from "../shared/tool-availability.ts";
|
|
|
82
85
|
import { collectDynamicResults, DynamicFanoutError, materializeDynamicParallelStep, validateDynamicCollection } from "../shared/dynamic-fanout.ts";
|
|
83
86
|
import { claimRunFanoutBatch, getRunFanoutBudgetSnapshot } from "../shared/run-fanout-budget.ts";
|
|
84
87
|
import { nestedSummaryFromAsyncStatus, projectNestedEvents, resolveNestedAsyncDir, writeNestedEvent } from "../shared/nested-events.ts";
|
|
85
|
-
import { formatModelAttemptNote, isRetryableModelFailure } from "../shared/model-fallback.ts";
|
|
88
|
+
import { formatModelAttemptNote, isRetryableModelFailure, recordRetryableModelFailure } from "../shared/model-fallback.ts";
|
|
86
89
|
import {
|
|
87
90
|
SUBAGENT_STARTUP_RETRY_DELAYS_MS,
|
|
88
91
|
formatSubagentExtensionConflictError,
|
|
@@ -136,6 +139,7 @@ import { resolveWatchdogConfig } from "../../watchdog/settings.ts";
|
|
|
136
139
|
import { createBoundedByteTail, createBoundedLineReader, formatProtocolOutputLimit, MAX_CHILD_STDERR_BYTES, PI_AGGREGATE_EVENT_PROJECTOR, projectChildLifecycle, type ChildLifecycleAction, type ProtocolOutputLimit } from "../shared/child-protocol.ts";
|
|
137
140
|
import { acquireSessionLease, type SessionLeaseRequest } from "../shared/session-lease.ts";
|
|
138
141
|
import { buildExternalCliPrompt, runExternalCli } from "../shared/external-cli-runner.ts";
|
|
142
|
+
import { runExternalJob } from "../shared/external-job-runner.ts";
|
|
139
143
|
import { createOrcaProgressTab, type OrcaProgressTab } from "../shared/orca-progress-tabs.ts";
|
|
140
144
|
import { decodeSubagentCapabilityCeiling, SUBAGENT_CAPABILITY_CEILING_ENV, type ResolvedSubagentCapabilityCeiling } from "../shared/capability-ceiling.ts";
|
|
141
145
|
import {
|
|
@@ -165,6 +169,7 @@ interface SubagentRunConfig {
|
|
|
165
169
|
sessionDir?: string;
|
|
166
170
|
asyncDir: string;
|
|
167
171
|
sessionId?: string | null;
|
|
172
|
+
completionOwnerId?: string;
|
|
168
173
|
piPackageRoot?: string;
|
|
169
174
|
piArgv1?: string;
|
|
170
175
|
worktreeSetupHook?: string;
|
|
@@ -217,6 +222,7 @@ interface StepResult {
|
|
|
217
222
|
savedOutputPath?: string;
|
|
218
223
|
skipped?: boolean;
|
|
219
224
|
interrupted?: boolean;
|
|
225
|
+
detached?: boolean;
|
|
220
226
|
timedOut?: boolean;
|
|
221
227
|
stopped?: boolean;
|
|
222
228
|
processSignal?: string | null;
|
|
@@ -249,8 +255,9 @@ interface StepResult {
|
|
|
249
255
|
watchdog?: import("../../shared/types.ts").ChildWatchdogProgress;
|
|
250
256
|
writerProcesses?: PiWriterProcessInstanceExitV1[];
|
|
251
257
|
writerAttemptCount?: number;
|
|
252
|
-
runner?: ExternalCliRunnerStatus;
|
|
258
|
+
runner?: ExternalCliRunnerStatus | ExternalJobRunnerStatus;
|
|
253
259
|
externalProcess?: ExternalProcessStatus;
|
|
260
|
+
externalJob?: ExternalJobStatus;
|
|
254
261
|
}
|
|
255
262
|
|
|
256
263
|
function persistStepArtifacts(input: {
|
|
@@ -1057,7 +1064,7 @@ async function exportSessionHtml(sessionFile: string, outputDir: string, piPacka
|
|
|
1057
1064
|
|
|
1058
1065
|
function createShareLink(htmlPath: string): { shareUrl: string; gistUrl: string } | { error: string } {
|
|
1059
1066
|
try {
|
|
1060
|
-
const auth = spawnSync("gh", ["auth", "status"], { encoding: "utf-8" });
|
|
1067
|
+
const auth = spawnSync("gh", ["auth", "status"], { encoding: "utf-8", windowsHide: true });
|
|
1061
1068
|
if (auth.status !== 0) {
|
|
1062
1069
|
return { error: "GitHub CLI is not logged in. Run 'gh auth login' first." };
|
|
1063
1070
|
}
|
|
@@ -1066,7 +1073,7 @@ function createShareLink(htmlPath: string): { shareUrl: string; gistUrl: string
|
|
|
1066
1073
|
}
|
|
1067
1074
|
|
|
1068
1075
|
try {
|
|
1069
|
-
const result = spawnSync("gh", ["gist", "create", htmlPath], { encoding: "utf-8" });
|
|
1076
|
+
const result = spawnSync("gh", ["gist", "create", htmlPath], { encoding: "utf-8", windowsHide: true });
|
|
1070
1077
|
if (result.status !== 0) {
|
|
1071
1078
|
const err = (result.stderr || "").trim() || "Failed to create gist.";
|
|
1072
1079
|
return { error: err };
|
|
@@ -1109,6 +1116,7 @@ function writeRunLog(
|
|
|
1109
1116
|
shareUrl?: string;
|
|
1110
1117
|
shareError?: string;
|
|
1111
1118
|
},
|
|
1119
|
+
write: (filePath: string, content: string) => void = (filePath, content) => fs.writeFileSync(filePath, content, "utf-8"),
|
|
1112
1120
|
): void {
|
|
1113
1121
|
const lines: string[] = [];
|
|
1114
1122
|
lines.push(`# Subagent run ${input.id}`);
|
|
@@ -1138,7 +1146,7 @@ function writeRunLog(
|
|
|
1138
1146
|
}
|
|
1139
1147
|
lines.push(input.summary.trim() || "(no output)");
|
|
1140
1148
|
lines.push("");
|
|
1141
|
-
|
|
1149
|
+
write(logPath, lines.join("\n"));
|
|
1142
1150
|
}
|
|
1143
1151
|
|
|
1144
1152
|
/** Context for running a single step */
|
|
@@ -1183,6 +1191,7 @@ interface SingleStepContext {
|
|
|
1183
1191
|
onChildEvent?: (event: ChildEvent) => void;
|
|
1184
1192
|
onWriterProcess?: (writer: { state: "none" | "spawning" } | { state: "running"; pid: number }) => void;
|
|
1185
1193
|
onExternalProcess?: (process: ExternalProcessStatus) => void;
|
|
1194
|
+
onExternalJob?: (status: ExternalJobStatus) => void;
|
|
1186
1195
|
skipAcceptance?: () => boolean;
|
|
1187
1196
|
orcaProgressTab?: OrcaProgressTab;
|
|
1188
1197
|
}
|
|
@@ -1360,8 +1369,71 @@ async function runSingleStepInner(
|
|
|
1360
1369
|
});
|
|
1361
1370
|
}
|
|
1362
1371
|
|
|
1363
|
-
|
|
1364
|
-
|
|
1372
|
+
if (step.runner?.type === "external-job") {
|
|
1373
|
+
const runner: ExternalJobRunnerStatus = {
|
|
1374
|
+
type: "external-job",
|
|
1375
|
+
provider: step.runner.provider,
|
|
1376
|
+
options: step.runner.options ?? {},
|
|
1377
|
+
capabilities: { stop: false, steer: false, resume: false, structuredOutput: false, toolEvents: false },
|
|
1378
|
+
};
|
|
1379
|
+
const outputSnapshot = captureSingleOutputSnapshot(step.outputPath);
|
|
1380
|
+
const external = await runExternalJob(omitUndefinedProperties({
|
|
1381
|
+
provider: runner.provider,
|
|
1382
|
+
options: runner.options,
|
|
1383
|
+
cwd: step.cwd ?? ctx.cwd,
|
|
1384
|
+
prompt: buildExternalCliPrompt(step.systemPrompt ?? "", task),
|
|
1385
|
+
asyncDir: path.dirname(ctx.outputFile),
|
|
1386
|
+
stepIndex: ctx.flatIndex,
|
|
1387
|
+
runId: ctx.id,
|
|
1388
|
+
agent: step.agent,
|
|
1389
|
+
sessionId: step.parentSessionId,
|
|
1390
|
+
registerTimeout: ctx.registerTimeout,
|
|
1391
|
+
registerStop: ctx.registerStop,
|
|
1392
|
+
timeoutMessage: ctx.timeoutMessage,
|
|
1393
|
+
stopMessage: ctx.stopMessage,
|
|
1394
|
+
onExternalJob: ctx.onExternalJob,
|
|
1395
|
+
}));
|
|
1396
|
+
try { fs.writeFileSync(ctx.outputFile, external.output, "utf-8"); } catch { /* Observability output is best-effort. */ }
|
|
1397
|
+
const resolvedOutput = step.outputPath && external.exitCode === 0
|
|
1398
|
+
? resolveSingleOutput(step.outputPath, external.output, outputSnapshot)
|
|
1399
|
+
: { fullOutput: external.output };
|
|
1400
|
+
const outputReference = resolvedOutput.savedPath ? formatSavedOutputReference(resolvedOutput.savedPath, resolvedOutput.fullOutput) : undefined;
|
|
1401
|
+
const finalizedOutput = finalizeSingleOutput(omitUndefinedProperties({
|
|
1402
|
+
fullOutput: resolvedOutput.fullOutput,
|
|
1403
|
+
outputPath: step.outputPath,
|
|
1404
|
+
outputMode: step.outputMode,
|
|
1405
|
+
exitCode: external.exitCode,
|
|
1406
|
+
savedPath: resolvedOutput.savedPath,
|
|
1407
|
+
outputReference,
|
|
1408
|
+
saveError: resolvedOutput.saveError,
|
|
1409
|
+
}));
|
|
1410
|
+
const artifactErrors = artifactPaths && ctx.artifactConfig?.enabled !== false
|
|
1411
|
+
? persistStepArtifacts({
|
|
1412
|
+
artifactPaths,
|
|
1413
|
+
artifactConfig: ctx.artifactConfig,
|
|
1414
|
+
output: formatOutputArtifactContent(omitUndefinedProperties({ output: resolvedOutput.fullOutput, error: external.error, metadataPath: ctx.artifactConfig?.includeMetadata === false ? undefined : artifactPaths.metadataPath })),
|
|
1415
|
+
metadata: { runId: ctx.id, agent: step.agent, task: PROMPT_REDACTED, runner, externalJob: external.externalJob, exitCode: external.exitCode, error: external.error, timestamp: Date.now() },
|
|
1416
|
+
})
|
|
1417
|
+
: {};
|
|
1418
|
+
return omitUndefinedProperties({
|
|
1419
|
+
agent: step.agent,
|
|
1420
|
+
context: step.context,
|
|
1421
|
+
output: finalizedOutput.displayOutput,
|
|
1422
|
+
outputState: external.output.trim() ? "present" : "absent",
|
|
1423
|
+
exitCode: external.exitCode,
|
|
1424
|
+
error: external.error,
|
|
1425
|
+
timedOut: external.timedOut,
|
|
1426
|
+
stopped: external.stopped,
|
|
1427
|
+
artifactPaths,
|
|
1428
|
+
outputSaveError: artifactErrors.outputSaveError,
|
|
1429
|
+
metadataSaveError: artifactErrors.metadataSaveError,
|
|
1430
|
+
runner,
|
|
1431
|
+
externalJob: external.externalJob,
|
|
1432
|
+
});
|
|
1433
|
+
}
|
|
1434
|
+
|
|
1435
|
+
const candidates = step.modelCandidates !== undefined
|
|
1436
|
+
? step.modelCandidates.length > 0 ? step.modelCandidates : [undefined]
|
|
1365
1437
|
: step.model
|
|
1366
1438
|
? [step.model]
|
|
1367
1439
|
: [undefined];
|
|
@@ -1697,7 +1769,9 @@ async function runSingleStepInner(
|
|
|
1697
1769
|
finalResult.finalOutput = startupError;
|
|
1698
1770
|
break modelAttemptsLoop;
|
|
1699
1771
|
}
|
|
1700
|
-
|
|
1772
|
+
const retryableModelFailure = isRetryableModelFailure(error);
|
|
1773
|
+
if (retryableModelFailure) recordRetryableModelFailure(candidate ?? run.model ?? step.model, error);
|
|
1774
|
+
if (!retryableModelFailure || modelIndex === candidates.length - 1) break modelAttemptsLoop;
|
|
1701
1775
|
attemptNotes.push(formatModelAttemptNote(attempt, candidates[modelIndex + 1]));
|
|
1702
1776
|
modelIndex += 1;
|
|
1703
1777
|
startupAttemptIndex = 0;
|
|
@@ -1891,15 +1965,25 @@ type RunnerStatusStep = NonNullable<AsyncStatus["steps"]>[number] & {
|
|
|
1891
1965
|
description?: string;
|
|
1892
1966
|
};
|
|
1893
1967
|
|
|
1894
|
-
function externalRunnerStatus(runner: SubagentStep["runner"]): ExternalCliRunnerStatus | undefined {
|
|
1895
|
-
if (runner?.type
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1968
|
+
function externalRunnerStatus(runner: SubagentStep["runner"]): ExternalCliRunnerStatus | ExternalJobRunnerStatus | undefined {
|
|
1969
|
+
if (runner?.type === "external-cli") {
|
|
1970
|
+
return {
|
|
1971
|
+
type: "external-cli",
|
|
1972
|
+
command: runner.command,
|
|
1973
|
+
args: runner.args ?? [],
|
|
1974
|
+
promptDelivery: runner.promptDelivery ?? "stdin",
|
|
1975
|
+
capabilities: { stop: true, steer: false, resume: false, structuredOutput: false, toolEvents: false },
|
|
1976
|
+
};
|
|
1977
|
+
}
|
|
1978
|
+
if (runner?.type === "external-job") {
|
|
1979
|
+
return {
|
|
1980
|
+
type: "external-job",
|
|
1981
|
+
provider: runner.provider,
|
|
1982
|
+
options: runner.options ?? {},
|
|
1983
|
+
capabilities: { stop: false, steer: false, resume: false, structuredOutput: false, toolEvents: false },
|
|
1984
|
+
};
|
|
1985
|
+
}
|
|
1986
|
+
return undefined;
|
|
1903
1987
|
}
|
|
1904
1988
|
|
|
1905
1989
|
function appendCapabilityCeilingAppliedEvent(eventsPath: string, runId: string, stepIndex: number, agent: string, result: StepResult): void {
|
|
@@ -1948,6 +2032,7 @@ function markParallelGroupSetupFailure(input: {
|
|
|
1948
2032
|
asyncDir: string;
|
|
1949
2033
|
runId: string;
|
|
1950
2034
|
stepIndex: number;
|
|
2035
|
+
writeStatus: (status: RunnerStatusPayload) => void;
|
|
1951
2036
|
}): void {
|
|
1952
2037
|
for (let taskIndex = 0; taskIndex < input.group.parallel.length; taskIndex++) {
|
|
1953
2038
|
const flatTaskIndex = input.groupStartFlatIndex + taskIndex;
|
|
@@ -1964,7 +2049,7 @@ function markParallelGroupSetupFailure(input: {
|
|
|
1964
2049
|
input.statusPayload.currentStep = input.groupStartFlatIndex;
|
|
1965
2050
|
input.statusPayload.lastUpdate = input.failedAt;
|
|
1966
2051
|
input.statusPayload.outputFile = path.join(input.asyncDir, `output-${input.groupStartFlatIndex}.log`);
|
|
1967
|
-
|
|
2052
|
+
input.writeStatus(input.statusPayload);
|
|
1968
2053
|
appendJsonl(input.eventsPath, JSON.stringify({
|
|
1969
2054
|
type: "subagent.parallel.completed",
|
|
1970
2055
|
ts: input.failedAt,
|
|
@@ -1984,6 +2069,7 @@ function markParallelGroupRunning(input: {
|
|
|
1984
2069
|
asyncDir: string;
|
|
1985
2070
|
runId: string;
|
|
1986
2071
|
stepIndex: number;
|
|
2072
|
+
writeStatus: (status: RunnerStatusPayload) => void;
|
|
1987
2073
|
}): void {
|
|
1988
2074
|
for (let taskIndex = 0; taskIndex < input.group.parallel.length; taskIndex++) {
|
|
1989
2075
|
const flatTaskIndex = input.groupStartFlatIndex + taskIndex;
|
|
@@ -2001,7 +2087,7 @@ function markParallelGroupRunning(input: {
|
|
|
2001
2087
|
input.statusPayload.lastActivityAt = input.groupStartTime;
|
|
2002
2088
|
input.statusPayload.lastUpdate = input.groupStartTime;
|
|
2003
2089
|
input.statusPayload.outputFile = path.join(input.asyncDir, `output-${input.groupStartFlatIndex}.log`);
|
|
2004
|
-
|
|
2090
|
+
input.writeStatus(input.statusPayload);
|
|
2005
2091
|
appendJsonl(input.eventsPath, JSON.stringify({
|
|
2006
2092
|
type: "subagent.parallel.started",
|
|
2007
2093
|
ts: input.groupStartTime,
|
|
@@ -2151,9 +2237,6 @@ async function runSubagent(
|
|
|
2151
2237
|
let stopped = false;
|
|
2152
2238
|
let turnBudgetExceeded = false;
|
|
2153
2239
|
let usageBudgetExceeded = false;
|
|
2154
|
-
let checkpointRejected = false;
|
|
2155
|
-
let pendingCheckpointDecision: "approved" | "rejected" | undefined;
|
|
2156
|
-
let wakeCheckpointDecision: (() => void) | undefined;
|
|
2157
2240
|
const timeoutMessage = config.timeoutMs !== undefined ? `Subagent timed out after ${config.timeoutMs}ms.` : undefined;
|
|
2158
2241
|
const stopMessage = "Subagent stopped by user.";
|
|
2159
2242
|
const timeoutAbortController = new AbortController();
|
|
@@ -2168,19 +2251,6 @@ async function runSubagent(
|
|
|
2168
2251
|
let flatStepCount = 0;
|
|
2169
2252
|
for (let stepIndex = 0; stepIndex < steps.length; stepIndex++) {
|
|
2170
2253
|
const step = steps[stepIndex]!;
|
|
2171
|
-
if (isCheckpointRunnerStep(step)) {
|
|
2172
|
-
initialStatusSteps.push(omitUndefinedProperties({
|
|
2173
|
-
agent: `checkpoint:${step.checkpoint}`,
|
|
2174
|
-
phase: step.phase,
|
|
2175
|
-
label: step.label ?? step.checkpoint,
|
|
2176
|
-
status: "pending",
|
|
2177
|
-
checkpoint: { name: step.checkpoint, ...(step.message ? { message: step.message } : {}), status: "pending", stepIndex },
|
|
2178
|
-
recentTools: [],
|
|
2179
|
-
recentOutput: [],
|
|
2180
|
-
}));
|
|
2181
|
-
flatStepCount++;
|
|
2182
|
-
continue;
|
|
2183
|
-
}
|
|
2184
2254
|
if (isParallelGroup(step)) {
|
|
2185
2255
|
parallelGroups.push({ start: flatStepCount, count: step.parallel.length, stepIndex });
|
|
2186
2256
|
for (const task of step.parallel) {
|
|
@@ -2272,8 +2342,9 @@ async function runSubagent(
|
|
|
2272
2342
|
lifecycleArtifactVersion: SUBAGENT_LIFECYCLE_ARTIFACT_VERSION,
|
|
2273
2343
|
runId: id,
|
|
2274
2344
|
...(config.sessionId ? { sessionId: config.sessionId } : {}),
|
|
2345
|
+
...(config.completionOwnerId ? { completionOwnerId: config.completionOwnerId } : {}),
|
|
2275
2346
|
mode: config.resultMode ?? (flatSteps.length > 1 ? "chain" : "single"),
|
|
2276
|
-
...(config.
|
|
2347
|
+
...(config.nestedSelf ? { isNested: true } : {}),
|
|
2277
2348
|
state: "running",
|
|
2278
2349
|
steering: createSteeringStatus(),
|
|
2279
2350
|
lastActivityAt: overallStartTime,
|
|
@@ -2303,9 +2374,37 @@ async function runSubagent(
|
|
|
2303
2374
|
outputFile: path.join(asyncDir, "output-0.log"),
|
|
2304
2375
|
});
|
|
2305
2376
|
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2377
|
+
let lastIndexedStatusState: AsyncStatus["state"] | undefined;
|
|
2378
|
+
const indexPersistence = createCapacityResilientJsonWriter({
|
|
2379
|
+
keepAlive: true,
|
|
2380
|
+
onSuccess: (_filePath, payload) => {
|
|
2381
|
+
lastIndexedStatusState = (payload as { state: AsyncStatus["state"] }).state;
|
|
2382
|
+
},
|
|
2383
|
+
onError: (error, filePath) => console.error(`Failed to update async run index '${filePath}':`, error),
|
|
2384
|
+
});
|
|
2385
|
+
const queueActiveRunIndex = (): void => {
|
|
2386
|
+
const state = statusPayload.state;
|
|
2387
|
+
if (state === lastIndexedStatusState && indexPersistence.pendingCount() === 0) return;
|
|
2388
|
+
indexPersistence.write(asyncDir, { state, toolCallId: statusPayload.toolCallId }, (_filePath, payload) => {
|
|
2389
|
+
const indexPayload = payload as { state: AsyncStatus["state"]; toolCallId?: string };
|
|
2390
|
+
updateActiveRunIndex(asyncDir, indexPayload.state, indexPayload.toolCallId, { retryCapacityErrors: true });
|
|
2391
|
+
});
|
|
2392
|
+
};
|
|
2393
|
+
const runPersistence = createCapacityResilientJsonWriter({
|
|
2394
|
+
keepAlive: true,
|
|
2395
|
+
onSuccess: (filePath) => {
|
|
2396
|
+
if (filePath === statusPath) queueActiveRunIndex();
|
|
2397
|
+
},
|
|
2398
|
+
onError: (error, filePath) => console.error(`Failed to persist async run state '${filePath}':`, error),
|
|
2399
|
+
});
|
|
2400
|
+
try {
|
|
2401
|
+
fs.mkdirSync(asyncDir, { recursive: true });
|
|
2402
|
+
} catch (error) {
|
|
2403
|
+
if (!isStorageCapacityError(error)) throw error;
|
|
2404
|
+
console.error(`Failed to prepare async run storage '${asyncDir}' while storage is full:`, error);
|
|
2405
|
+
}
|
|
2406
|
+
runPersistence.write(statusPath, statusPayload);
|
|
2407
|
+
|
|
2309
2408
|
let pendingParallelUsageCost: CostSummary = { inputTokens: 0, outputTokens: 0, costUsd: 0 };
|
|
2310
2409
|
const currentUsageTotals = (): CostSummary => {
|
|
2311
2410
|
const cost = results.reduce<CostSummary>((sum, result) => ({
|
|
@@ -2360,7 +2459,6 @@ async function runSubagent(
|
|
|
2360
2459
|
node.status = normalize(step.status);
|
|
2361
2460
|
setOptionalProperty(node, "error", step.error);
|
|
2362
2461
|
setOptionalProperty(node, "acceptanceStatus", step.acceptance?.status);
|
|
2363
|
-
setOptionalProperty(node, "checkpoint", step.checkpoint);
|
|
2364
2462
|
}
|
|
2365
2463
|
if (statusPayload.currentStep === node.flatIndex) graph.currentNodeId = node.id;
|
|
2366
2464
|
}
|
|
@@ -2379,17 +2477,15 @@ async function runSubagent(
|
|
|
2379
2477
|
statusPayload.workflowGraph = graph;
|
|
2380
2478
|
};
|
|
2381
2479
|
let finalResultCommitted = false;
|
|
2382
|
-
let lastIndexedActiveState = isActiveAsyncState(statusPayload.state);
|
|
2383
2480
|
const statusResultState = (): AsyncStatus["state"] | undefined => {
|
|
2384
2481
|
if (statusPayload.state === "running" || statusPayload.state === "queued") return undefined;
|
|
2385
|
-
if (statusPayload.state === "paused" && statusPayload.checkpoint?.status === "pending") return undefined;
|
|
2386
2482
|
return statusPayload.state;
|
|
2387
2483
|
};
|
|
2388
2484
|
const statusResultSummary = (state: AsyncStatus["state"]): string => {
|
|
2389
2485
|
if (statusPayload.error) return statusPayload.error;
|
|
2390
2486
|
if (state === "paused") return "Paused after interrupt. Waiting for explicit next action.";
|
|
2391
2487
|
if (state === "stopped") return stopMessage;
|
|
2392
|
-
if (state === "rejected") return
|
|
2488
|
+
if (state === "rejected") return "Subagent rejected.";
|
|
2393
2489
|
return state === "complete" ? "Subagent completed." : "Subagent failed.";
|
|
2394
2490
|
};
|
|
2395
2491
|
const statusResultSuccess = (state: AsyncStatus["state"], step: RunnerStatusStep): boolean | undefined => {
|
|
@@ -2404,7 +2500,7 @@ async function runSubagent(
|
|
|
2404
2500
|
if (!state || finalResultCommitted || !config.sessionId) return;
|
|
2405
2501
|
const now = statusPayload.endedAt ?? statusPayload.lastUpdate ?? Date.now();
|
|
2406
2502
|
const summary = statusResultSummary(state);
|
|
2407
|
-
|
|
2503
|
+
runPersistence.write(resultPath, omitUndefinedProperties({
|
|
2408
2504
|
lifecycleArtifactVersion: SUBAGENT_LIFECYCLE_ARTIFACT_VERSION,
|
|
2409
2505
|
id,
|
|
2410
2506
|
runId: id,
|
|
@@ -2415,7 +2511,6 @@ async function runSubagent(
|
|
|
2415
2511
|
summary,
|
|
2416
2512
|
error: state === "failed" || state === "stopped" || state === "rejected" ? summary : undefined,
|
|
2417
2513
|
stopped: state === "stopped" ? true : undefined,
|
|
2418
|
-
checkpoint: statusPayload.checkpoint,
|
|
2419
2514
|
results: statusPayload.steps.map((step) => omitUndefinedProperties({
|
|
2420
2515
|
agent: step.agent,
|
|
2421
2516
|
output: step.status === "complete" || step.status === "completed" ? "" : step.error ?? summary,
|
|
@@ -2432,18 +2527,14 @@ async function runSubagent(
|
|
|
2432
2527
|
asyncDir,
|
|
2433
2528
|
cwd,
|
|
2434
2529
|
sessionId: config.sessionId,
|
|
2530
|
+
completionOwnerId: config.completionOwnerId,
|
|
2435
2531
|
sessionFile: statusPayload.sessionFile ?? latestSessionFile,
|
|
2436
|
-
}));
|
|
2532
|
+
}), (filePath, payload) => writePendingAsyncResultFile(filePath, payload as Record<string, unknown>));
|
|
2437
2533
|
};
|
|
2438
2534
|
const writeStatusPayloadNow = (): void => {
|
|
2439
2535
|
refreshWorkflowGraph();
|
|
2440
2536
|
writeRecoverableStatusResult();
|
|
2441
|
-
|
|
2442
|
-
const activeState = isActiveAsyncState(statusPayload.state);
|
|
2443
|
-
if (activeState !== lastIndexedActiveState) {
|
|
2444
|
-
updateActiveRunIndex(asyncDir, statusPayload.state, statusPayload.toolCallId);
|
|
2445
|
-
}
|
|
2446
|
-
lastIndexedActiveState = activeState;
|
|
2537
|
+
runPersistence.write(statusPath, statusPayload);
|
|
2447
2538
|
emitNestedSelfEvent(statusPayload.state === "running" || statusPayload.state === "queued" ? "subagent.nested.updated" : "subagent.nested.completed");
|
|
2448
2539
|
};
|
|
2449
2540
|
const statusWriteCoalescer = createFileCoalescer(writeStatusPayloadNow, 100);
|
|
@@ -2459,6 +2550,11 @@ async function runSubagent(
|
|
|
2459
2550
|
statusPayload.lastUpdate = Date.now();
|
|
2460
2551
|
writeStatusPayload();
|
|
2461
2552
|
};
|
|
2553
|
+
const updateExternalJob = (index: number, externalJob: ExternalJobStatus): void => {
|
|
2554
|
+
requiredStatusStep(statusPayload, index).externalJob = externalJob;
|
|
2555
|
+
statusPayload.lastUpdate = Date.now();
|
|
2556
|
+
writeStatusPayload();
|
|
2557
|
+
};
|
|
2462
2558
|
const registerStepInterrupt = (flatIndex: number, interrupt: (() => void) | undefined): void => {
|
|
2463
2559
|
if (!interrupt) {
|
|
2464
2560
|
activeChildInterrupts.delete(flatIndex);
|
|
@@ -2754,8 +2850,16 @@ async function runSubagent(
|
|
|
2754
2850
|
.sort((left, right) => left.startedAt - right.startedAt)[0];
|
|
2755
2851
|
const supervisorAttentionSteps = new Map<number, ActivityState | undefined>();
|
|
2756
2852
|
const mutatingFailureWindowMs = 5 * 60_000;
|
|
2757
|
-
const appendControlEvent = (
|
|
2853
|
+
const appendControlEvent = (rawEvent: ReturnType<typeof buildControlEvent>) => {
|
|
2758
2854
|
if (!controlConfig.enabled) return;
|
|
2855
|
+
const contextStep = statusPayload.steps[rawEvent.index ?? statusPayload.currentStep ?? 0];
|
|
2856
|
+
const event = {
|
|
2857
|
+
...rawEvent,
|
|
2858
|
+
...(contextStep?.workflowKey ?? statusPayload.workflowKey ? { workflowKey: contextStep?.workflowKey ?? statusPayload.workflowKey } : {}),
|
|
2859
|
+
...(contextStep?.phase ? { phase: contextStep.phase } : {}),
|
|
2860
|
+
...(contextStep?.label ? { label: contextStep.label } : {}),
|
|
2861
|
+
...(contextStep?.description ? { taskPreview: contextStep.description } : {}),
|
|
2862
|
+
};
|
|
2759
2863
|
const childIntercomTarget = config.childIntercomTargets?.[event.index ?? statusPayload.currentStep];
|
|
2760
2864
|
const channels = event.type === "active_long_running"
|
|
2761
2865
|
? controlConfig.notifyChannels.filter((channel) => channel !== "intercom")
|
|
@@ -3228,6 +3332,7 @@ async function runSubagent(
|
|
|
3228
3332
|
startedAt: step.startedAt ?? overallStartTime,
|
|
3229
3333
|
lastActivityAt,
|
|
3230
3334
|
currentTool: step.currentTool,
|
|
3335
|
+
thinking: step.thinking,
|
|
3231
3336
|
now,
|
|
3232
3337
|
}));
|
|
3233
3338
|
if (idleState === "needs_attention") {
|
|
@@ -3378,10 +3483,6 @@ async function runSubagent(
|
|
|
3378
3483
|
onInterrupt: interruptRunner,
|
|
3379
3484
|
onTimeout: timeoutRunner,
|
|
3380
3485
|
onStop: stopRunner,
|
|
3381
|
-
onCheckpointDecision: (decision) => {
|
|
3382
|
-
pendingCheckpointDecision = decision;
|
|
3383
|
-
wakeCheckpointDecision?.();
|
|
3384
|
-
},
|
|
3385
3486
|
onSteer: (request) => {
|
|
3386
3487
|
const targetStep = request.targetIndex !== undefined ? statusPayload.steps[request.targetIndex] : undefined;
|
|
3387
3488
|
if (targetStep?.status === "pending") {
|
|
@@ -3432,20 +3533,6 @@ async function runSubagent(
|
|
|
3432
3533
|
|
|
3433
3534
|
let flatIndex = 0;
|
|
3434
3535
|
let stepCursor = 0;
|
|
3435
|
-
const waitForCheckpointDecision = async (): Promise<"approved" | "rejected" | undefined> => {
|
|
3436
|
-
while (!pendingCheckpointDecision && !interrupted && !timedOut && !stopped) {
|
|
3437
|
-
await new Promise<void>((resolve) => {
|
|
3438
|
-
wakeCheckpointDecision = resolve;
|
|
3439
|
-
const timer = setTimeout(resolve, POLL_INTERVAL_MS);
|
|
3440
|
-
timer.unref?.();
|
|
3441
|
-
});
|
|
3442
|
-
wakeCheckpointDecision = undefined;
|
|
3443
|
-
}
|
|
3444
|
-
const decision = pendingCheckpointDecision;
|
|
3445
|
-
pendingCheckpointDecision = undefined;
|
|
3446
|
-
return decision;
|
|
3447
|
-
};
|
|
3448
|
-
|
|
3449
3536
|
while (true) {
|
|
3450
3537
|
if (interrupted || timedOut || stopped || turnBudgetExceeded) break;
|
|
3451
3538
|
consumePendingAppendRequests();
|
|
@@ -3463,64 +3550,6 @@ async function runSubagent(
|
|
|
3463
3550
|
const stepIndex = stepCursor++;
|
|
3464
3551
|
const step = steps[stepIndex]!;
|
|
3465
3552
|
|
|
3466
|
-
if (isCheckpointRunnerStep(step)) {
|
|
3467
|
-
const now = Date.now();
|
|
3468
|
-
const statusStep = statusPayload.steps[flatIndex];
|
|
3469
|
-
const checkpoint = { name: step.checkpoint, ...(step.message ? { message: step.message } : {}), status: "pending" as const, stepIndex };
|
|
3470
|
-
statusPayload.state = "paused";
|
|
3471
|
-
statusPayload.currentStep = flatIndex;
|
|
3472
|
-
statusPayload.checkpoint = checkpoint;
|
|
3473
|
-
delete statusPayload.activityState;
|
|
3474
|
-
statusPayload.lastUpdate = now;
|
|
3475
|
-
if (statusStep) {
|
|
3476
|
-
statusStep.status = "paused";
|
|
3477
|
-
statusStep.startedAt = now;
|
|
3478
|
-
statusStep.checkpoint = checkpoint;
|
|
3479
|
-
}
|
|
3480
|
-
writeStatusPayload();
|
|
3481
|
-
appendJsonl(eventsPath, JSON.stringify({ type: "subagent.checkpoint.paused", ts: now, runId: id, stepIndex, checkpoint }));
|
|
3482
|
-
const decision = await waitForCheckpointDecision();
|
|
3483
|
-
if (decision === "approved") {
|
|
3484
|
-
const approvedAt = Date.now();
|
|
3485
|
-
const approved = { ...checkpoint, status: "approved" as const, approvedAt };
|
|
3486
|
-
statusPayload.state = "running";
|
|
3487
|
-
statusPayload.checkpoint = approved;
|
|
3488
|
-
if (statusStep) {
|
|
3489
|
-
statusStep.status = "complete";
|
|
3490
|
-
statusStep.endedAt = approvedAt;
|
|
3491
|
-
statusStep.durationMs = approvedAt - now;
|
|
3492
|
-
statusStep.exitCode = 0;
|
|
3493
|
-
statusStep.checkpoint = approved;
|
|
3494
|
-
}
|
|
3495
|
-
statusPayload.lastUpdate = approvedAt;
|
|
3496
|
-
writeStatusPayload();
|
|
3497
|
-
appendJsonl(eventsPath, JSON.stringify({ type: "subagent.checkpoint.approved", ts: approvedAt, runId: id, stepIndex, checkpoint: approved }));
|
|
3498
|
-
flatIndex++;
|
|
3499
|
-
continue;
|
|
3500
|
-
}
|
|
3501
|
-
if (decision === "rejected") {
|
|
3502
|
-
const rejectedAt = Date.now();
|
|
3503
|
-
const rejected = { ...checkpoint, status: "rejected" as const, rejectedAt };
|
|
3504
|
-
checkpointRejected = true;
|
|
3505
|
-
statusPayload.state = "rejected";
|
|
3506
|
-
statusPayload.error = `Checkpoint '${step.checkpoint}' rejected.`;
|
|
3507
|
-
statusPayload.checkpoint = rejected;
|
|
3508
|
-
if (statusStep) {
|
|
3509
|
-
statusStep.status = "rejected";
|
|
3510
|
-
statusStep.error = statusPayload.error;
|
|
3511
|
-
statusStep.endedAt = rejectedAt;
|
|
3512
|
-
statusStep.durationMs = rejectedAt - now;
|
|
3513
|
-
statusStep.exitCode = 1;
|
|
3514
|
-
statusStep.checkpoint = rejected;
|
|
3515
|
-
}
|
|
3516
|
-
statusPayload.lastUpdate = rejectedAt;
|
|
3517
|
-
writeStatusPayload();
|
|
3518
|
-
appendJsonl(eventsPath, JSON.stringify({ type: "subagent.checkpoint.rejected", ts: rejectedAt, runId: id, stepIndex, checkpoint: rejected }));
|
|
3519
|
-
break;
|
|
3520
|
-
}
|
|
3521
|
-
break;
|
|
3522
|
-
}
|
|
3523
|
-
|
|
3524
3553
|
if (isDynamicRunnerGroup(step)) {
|
|
3525
3554
|
const groupStartFlatIndex = flatIndex;
|
|
3526
3555
|
let materialized: ReturnType<typeof materializeDynamicParallelStep>;
|
|
@@ -3814,6 +3843,7 @@ async function runSubagent(
|
|
|
3814
3843
|
onChildEvent: (event) => updateStepFromChildEvent(fi, event),
|
|
3815
3844
|
onWriterProcess,
|
|
3816
3845
|
onExternalProcess: (process) => updateExternalProcess(fi, process),
|
|
3846
|
+
onExternalJob: (externalJob) => updateExternalJob(fi, externalJob),
|
|
3817
3847
|
skipAcceptance: () => timedOut || stopped,
|
|
3818
3848
|
}), config.deadlineAt);
|
|
3819
3849
|
const taskEndTime = Date.now();
|
|
@@ -4039,6 +4069,7 @@ async function runSubagent(
|
|
|
4039
4069
|
asyncDir,
|
|
4040
4070
|
runId: id,
|
|
4041
4071
|
stepIndex,
|
|
4072
|
+
writeStatus: () => writeStatusPayload(),
|
|
4042
4073
|
});
|
|
4043
4074
|
flatIndex += group.parallel.length;
|
|
4044
4075
|
break;
|
|
@@ -4066,6 +4097,7 @@ async function runSubagent(
|
|
|
4066
4097
|
asyncDir,
|
|
4067
4098
|
runId: id,
|
|
4068
4099
|
stepIndex,
|
|
4100
|
+
writeStatus: () => writeStatusPayload(),
|
|
4069
4101
|
});
|
|
4070
4102
|
flatIndex += group.parallel.length;
|
|
4071
4103
|
break;
|
|
@@ -4097,6 +4129,7 @@ async function runSubagent(
|
|
|
4097
4129
|
asyncDir,
|
|
4098
4130
|
runId: id,
|
|
4099
4131
|
stepIndex,
|
|
4132
|
+
writeStatus: () => writeStatusPayload(),
|
|
4100
4133
|
});
|
|
4101
4134
|
const parallelResults = await mapConcurrent(
|
|
4102
4135
|
group.parallel,
|
|
@@ -4198,6 +4231,7 @@ async function runSubagent(
|
|
|
4198
4231
|
onChildEvent: (event) => updateStepFromChildEvent(fi, event),
|
|
4199
4232
|
onWriterProcess,
|
|
4200
4233
|
onExternalProcess: (process) => updateExternalProcess(fi, process),
|
|
4234
|
+
onExternalJob: (externalJob) => updateExternalJob(fi, externalJob),
|
|
4201
4235
|
skipAcceptance: () => timedOut || stopped,
|
|
4202
4236
|
}), config.deadlineAt);
|
|
4203
4237
|
if (task.sessionFile) {
|
|
@@ -4435,6 +4469,32 @@ async function runSubagent(
|
|
|
4435
4469
|
}
|
|
4436
4470
|
} else {
|
|
4437
4471
|
const seqStep = step as SubagentStep;
|
|
4472
|
+
let singleWorktreeSetup: WorktreeSetup | undefined;
|
|
4473
|
+
if (seqStep.worktree) {
|
|
4474
|
+
try {
|
|
4475
|
+
singleWorktreeSetup = createWorktrees(cwd, `${id}-s${stepIndex}`, 1, omitUndefinedProperties({
|
|
4476
|
+
agents: [seqStep.agent],
|
|
4477
|
+
setupHook: config.worktreeSetupHook
|
|
4478
|
+
? omitUndefinedProperties({ hookPath: config.worktreeSetupHook, timeoutMs: config.worktreeSetupHookTimeoutMs })
|
|
4479
|
+
: undefined,
|
|
4480
|
+
baseDir: config.worktreeBaseDir,
|
|
4481
|
+
}));
|
|
4482
|
+
writePendingParallelHandoff({
|
|
4483
|
+
manifestPath: parallelHandoffPath(asyncDir),
|
|
4484
|
+
runId: id,
|
|
4485
|
+
mode: "single",
|
|
4486
|
+
source: "async",
|
|
4487
|
+
cwd,
|
|
4488
|
+
stepIndex,
|
|
4489
|
+
flatStartIndex: flatIndex,
|
|
4490
|
+
setup: singleWorktreeSetup,
|
|
4491
|
+
});
|
|
4492
|
+
} catch (error) {
|
|
4493
|
+
if (singleWorktreeSetup) cleanupWorktrees(singleWorktreeSetup);
|
|
4494
|
+
throw error;
|
|
4495
|
+
}
|
|
4496
|
+
}
|
|
4497
|
+
const singleCwd = singleWorktreeSetup?.worktrees[0]?.agentCwd ?? cwd;
|
|
4438
4498
|
const stepStartTime = Date.now();
|
|
4439
4499
|
statusPayload.currentStep = flatIndex;
|
|
4440
4500
|
requiredStatusStep(statusPayload, flatIndex).status = "running";
|
|
@@ -4458,8 +4518,11 @@ async function runSubagent(
|
|
|
4458
4518
|
}));
|
|
4459
4519
|
|
|
4460
4520
|
flushPendingStepSteers(flatIndex);
|
|
4461
|
-
const
|
|
4462
|
-
|
|
4521
|
+
const executionStep = singleWorktreeSetup ? { ...seqStep, cwd: singleCwd } : seqStep;
|
|
4522
|
+
let singleResult: Awaited<ReturnType<typeof runSingleStepWithTimeout>>;
|
|
4523
|
+
try {
|
|
4524
|
+
singleResult = await runSingleStepWithTimeout(executionStep, compactOptional<SingleStepContext>({
|
|
4525
|
+
previousOutput, placeholder, cwd: singleCwd, sessionEnabled,
|
|
4463
4526
|
outputs: statusPayload.mode === "single" ? undefined : outputs,
|
|
4464
4527
|
sessionDir: config.sessionDir,
|
|
4465
4528
|
artifactsDir, artifactConfig, id,
|
|
@@ -4489,8 +4552,13 @@ async function runSubagent(
|
|
|
4489
4552
|
onChildEvent: (event) => updateStepFromChildEvent(flatIndex, event),
|
|
4490
4553
|
onWriterProcess,
|
|
4491
4554
|
onExternalProcess: (process) => updateExternalProcess(flatIndex, process),
|
|
4555
|
+
onExternalJob: (externalJob) => updateExternalJob(flatIndex, externalJob),
|
|
4492
4556
|
skipAcceptance: () => timedOut || stopped,
|
|
4493
|
-
|
|
4557
|
+
}), config.deadlineAt);
|
|
4558
|
+
} catch (error) {
|
|
4559
|
+
if (singleWorktreeSetup) cleanupWorktrees(singleWorktreeSetup);
|
|
4560
|
+
throw error;
|
|
4561
|
+
}
|
|
4494
4562
|
if (seqStep.sessionFile) {
|
|
4495
4563
|
latestSessionFile = seqStep.sessionFile;
|
|
4496
4564
|
}
|
|
@@ -4539,6 +4607,7 @@ async function runSubagent(
|
|
|
4539
4607
|
toolBudgetBlocked: singleResult.toolBudgetBlocked,
|
|
4540
4608
|
runner: singleResult.runner,
|
|
4541
4609
|
externalProcess: singleResult.externalProcess,
|
|
4610
|
+
externalJob: singleResult.externalJob,
|
|
4542
4611
|
}));
|
|
4543
4612
|
if (seqStep.outputName) {
|
|
4544
4613
|
outputs[seqStep.outputName] = outputEntryFromAsyncResult({
|
|
@@ -4629,6 +4698,48 @@ async function runSubagent(
|
|
|
4629
4698
|
durationMs: stepEndTime - stepStartTime,
|
|
4630
4699
|
tokens: stepTokens,
|
|
4631
4700
|
}));
|
|
4701
|
+
if (singleWorktreeSetup && !singleResult.detached) {
|
|
4702
|
+
const diffs = diffWorktrees(singleWorktreeSetup, [seqStep.agent], path.join(asyncDir, "worktree-diffs", `step-${stepIndex}`));
|
|
4703
|
+
const diffSummary = formatWorktreeDiffSummary(diffs);
|
|
4704
|
+
const manifestPath = parallelHandoffPath(asyncDir);
|
|
4705
|
+
const handoff = {
|
|
4706
|
+
manifestPath,
|
|
4707
|
+
runId: id,
|
|
4708
|
+
mode: "single" as const,
|
|
4709
|
+
source: "async" as const,
|
|
4710
|
+
cwd,
|
|
4711
|
+
stepIndex,
|
|
4712
|
+
flatStartIndex: flatIndex,
|
|
4713
|
+
setup: singleWorktreeSetup,
|
|
4714
|
+
diffs,
|
|
4715
|
+
results: [{
|
|
4716
|
+
agent: singleResult.agent,
|
|
4717
|
+
status: singleResult.stopped ? "stopped" as const : singleResult.interrupted ? "paused" as const : singleResult.exitCode === 0 ? "completed" as const : "failed" as const,
|
|
4718
|
+
summary: singleResult.output || singleResult.error || "(no output)",
|
|
4719
|
+
...(singleResult.artifactPaths?.outputPath ? { outputPath: singleResult.artifactPaths.outputPath } : {}),
|
|
4720
|
+
...(singleResult.structuredOutput !== undefined ? { structuredOutput: singleResult.structuredOutput } : {}),
|
|
4721
|
+
...(singleResult.structuredOutputPath ? { structuredOutputPath: singleResult.structuredOutputPath } : {}),
|
|
4722
|
+
...(singleResult.sessionFile ? { sessionPath: singleResult.sessionFile } : {}),
|
|
4723
|
+
}],
|
|
4724
|
+
};
|
|
4725
|
+
try {
|
|
4726
|
+
writeParallelHandoffGroup(handoff);
|
|
4727
|
+
const cleanup = cleanupWorktrees(singleWorktreeSetup, {
|
|
4728
|
+
kind: "preserve",
|
|
4729
|
+
capturedDiffs: diffs,
|
|
4730
|
+
handoffManifestPath: manifestPath,
|
|
4731
|
+
...(config.parentWorkflowRunId && singleResult.sessionFile && fs.existsSync(singleResult.sessionFile) && !singleResult.stopped
|
|
4732
|
+
? { cleanupBlocker: "retained child resume requires managed worktree cwd" }
|
|
4733
|
+
: {}),
|
|
4734
|
+
});
|
|
4735
|
+
statusPayload.parallelHandoff = writeParallelHandoffGroup({ ...handoff, cleanup });
|
|
4736
|
+
previousOutput = [previousOutput, diffSummary, formatParallelHandoffReference(statusPayload.parallelHandoff)].filter(Boolean).join("\n\n");
|
|
4737
|
+
} catch (error) {
|
|
4738
|
+
previousOutput = [previousOutput, diffSummary, formatParallelHandoffError(error)].filter(Boolean).join("\n\n");
|
|
4739
|
+
}
|
|
4740
|
+
writeStatusPayload();
|
|
4741
|
+
}
|
|
4742
|
+
|
|
4632
4743
|
if (singleResult.completionGuardTriggered) {
|
|
4633
4744
|
const event = buildControlEvent(omitUndefinedProperties({
|
|
4634
4745
|
from: requiredStatusStep(statusPayload, flatIndex).activityState,
|
|
@@ -4730,8 +4841,8 @@ async function runSubagent(
|
|
|
4730
4841
|
stopped: result.stopped,
|
|
4731
4842
|
turnBudgetExceeded: result.turnBudgetExceeded,
|
|
4732
4843
|
})));
|
|
4733
|
-
statusPayload.state =
|
|
4734
|
-
closeSteerInbox(asyncDir, statusPayload.state);
|
|
4844
|
+
statusPayload.state = stopped || signalTerminated ? "stopped" : timedOut || turnBudgetExceeded || usageBudgetExceeded || statusPayload.error ? "failed" : interrupted ? "paused" : results.every((r) => r.success) ? "complete" : "failed";
|
|
4845
|
+
closeSteerInbox(asyncDir, statusPayload.state, (filePath, payload) => runPersistence.write(filePath, payload));
|
|
4735
4846
|
disposeControlInbox();
|
|
4736
4847
|
for (const request of consumeSteerRequests(asyncDir)) deliverSteerRequest(request);
|
|
4737
4848
|
const effectiveSessionFile = sessionFile ?? latestSessionFile;
|
|
@@ -4782,15 +4893,16 @@ async function runSubagent(
|
|
|
4782
4893
|
statusPayload.error = `Step failed: ${failedStep.agent}`;
|
|
4783
4894
|
}
|
|
4784
4895
|
}
|
|
4896
|
+
writeStatusPayload();
|
|
4785
4897
|
try {
|
|
4786
|
-
|
|
4898
|
+
runPersistence.write(resultPath, {
|
|
4787
4899
|
lifecycleArtifactVersion: SUBAGENT_LIFECYCLE_ARTIFACT_VERSION,
|
|
4788
4900
|
id,
|
|
4789
4901
|
agent: agentName,
|
|
4790
4902
|
mode: resultMode,
|
|
4791
|
-
success: !
|
|
4792
|
-
state:
|
|
4793
|
-
summary:
|
|
4903
|
+
success: !stopped && !timedOut && !turnBudgetExceeded && !usageBudgetExceeded && !interrupted && !signalTerminated && results.every((r) => r.success),
|
|
4904
|
+
state: stopped || signalTerminated ? "stopped" : timedOut || turnBudgetExceeded || usageBudgetExceeded ? "failed" : interrupted ? "paused" : results.every((r) => r.success) ? "complete" : "failed",
|
|
4905
|
+
summary: stopped ? stopMessage : signalTerminated ? (statusPayload.error ?? "Subagent process terminated by signal.") : timedOut ? (timeoutMessage ?? "Subagent timed out.") : turnBudgetExceeded ? (statusPayload.error ?? "Subagent exceeded turn budget.") : usageBudgetExceeded ? (statusPayload.error ?? "Usage budget exhausted.") : interrupted ? "Paused after interrupt. Waiting for explicit next action." : summary,
|
|
4794
4906
|
...(config.timeoutMs !== undefined ? { timeoutMs: config.timeoutMs } : {}),
|
|
4795
4907
|
...(config.deadlineAt !== undefined ? { deadlineAt: config.deadlineAt } : {}),
|
|
4796
4908
|
...(statusPayload.turnBudget ? { turnBudget: statusPayload.turnBudget } : {}),
|
|
@@ -4799,7 +4911,6 @@ async function runSubagent(
|
|
|
4799
4911
|
...(statusPayload.toolBudget ? { toolBudget: statusPayload.toolBudget } : {}),
|
|
4800
4912
|
...(statusPayload.toolBudgetBlocked ? { toolBudgetBlocked: true } : {}),
|
|
4801
4913
|
...(statusPayload.usageBudget ? { usageBudget: statusPayload.usageBudget } : {}),
|
|
4802
|
-
...(statusPayload.checkpoint ? { checkpoint: statusPayload.checkpoint } : {}),
|
|
4803
4914
|
...(stopped ? { stopped: true, error: stopMessage } : timedOut ? { timedOut: true, error: timeoutMessage ?? "Subagent timed out." } : turnBudgetExceeded ? { error: statusPayload.error ?? "Subagent exceeded turn budget." } : usageBudgetExceeded ? { error: statusPayload.error ?? "Usage budget exhausted." } : {}),
|
|
4804
4915
|
results: results.map((r) => omitUndefinedProperties({
|
|
4805
4916
|
agent: r.agent,
|
|
@@ -4837,6 +4948,7 @@ async function runSubagent(
|
|
|
4837
4948
|
runtimeAcknowledgedExtensions: r.runtimeAcknowledgedExtensions,
|
|
4838
4949
|
runner: r.runner,
|
|
4839
4950
|
externalProcess: r.externalProcess,
|
|
4951
|
+
externalJob: r.externalJob,
|
|
4840
4952
|
execution: r.execution,
|
|
4841
4953
|
review: r.review,
|
|
4842
4954
|
effects: r.effects,
|
|
@@ -4855,13 +4967,12 @@ async function runSubagent(
|
|
|
4855
4967
|
capabilityAudit: statusPayload.capabilityAudit,
|
|
4856
4968
|
...(config.parentWorkflowRunId ? { parentWorkflowRunId: config.parentWorkflowRunId } : {}),
|
|
4857
4969
|
...(config.workflowKey ? { workflowKey: config.workflowKey } : {}),
|
|
4858
|
-
exitCode:
|
|
4970
|
+
exitCode: stopped || timedOut || turnBudgetExceeded || usageBudgetExceeded ? 1 : interrupted || results.every((r) => r.success) ? 0 : 1,
|
|
4859
4971
|
timestamp: runEndedAt,
|
|
4860
4972
|
durationMs: runEndedAt - overallStartTime,
|
|
4861
4973
|
totalTokens: statusPayload.totalTokens,
|
|
4862
4974
|
totalCost: finalTotalCost,
|
|
4863
4975
|
usageBudget: statusPayload.usageBudget,
|
|
4864
|
-
checkpoint: statusPayload.checkpoint,
|
|
4865
4976
|
truncated,
|
|
4866
4977
|
artifactsDir,
|
|
4867
4978
|
cwd,
|
|
@@ -4870,6 +4981,7 @@ async function runSubagent(
|
|
|
4870
4981
|
launchResolvedExtensions: config.launchResolvedExtensions,
|
|
4871
4982
|
runtimeAcknowledgedExtensions: singleRuntimeAcknowledgedExtensions,
|
|
4872
4983
|
sessionId: config.sessionId,
|
|
4984
|
+
completionOwnerId: config.completionOwnerId,
|
|
4873
4985
|
sessionFile: effectiveSessionFile,
|
|
4874
4986
|
intercomTarget: config.controlIntercomTarget,
|
|
4875
4987
|
shareUrl,
|
|
@@ -4877,7 +4989,7 @@ async function runSubagent(
|
|
|
4877
4989
|
shareError,
|
|
4878
4990
|
...(taskIndex !== undefined && { taskIndex }),
|
|
4879
4991
|
...(totalTasks !== undefined && { totalTasks }),
|
|
4880
|
-
});
|
|
4992
|
+
}, (filePath, payload) => { writeAsyncResultFile(filePath, payload as Record<string, unknown>); });
|
|
4881
4993
|
finalResultCommitted = true;
|
|
4882
4994
|
} catch (err) {
|
|
4883
4995
|
const message = `Failed to write result file ${resultPath}: ${err instanceof Error ? err.message : String(err)}`;
|
|
@@ -4918,7 +5030,11 @@ async function runSubagent(
|
|
|
4918
5030
|
sessionFile: effectiveSessionFile,
|
|
4919
5031
|
shareUrl,
|
|
4920
5032
|
shareError,
|
|
5033
|
+
}), (filePath, content) => runPersistence.write(filePath, { content }, (_path, payload) => {
|
|
5034
|
+
fs.writeFileSync(_path, (payload as { content: string }).content, "utf-8");
|
|
4921
5035
|
}));
|
|
5036
|
+
if (runPersistence.pendingCount() === 0) runPersistence.dispose();
|
|
5037
|
+
if (indexPersistence.pendingCount() === 0) indexPersistence.dispose();
|
|
4922
5038
|
if (config.runnerProcessInstanceId) {
|
|
4923
5039
|
const writers: Record<string, PiWriterProcessInstanceExitV1[]> = {};
|
|
4924
5040
|
const expectedWriters: Record<string, number> = {};
|