@pi-agents/orchid 0.1.0-beta.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 +41 -0
- package/LICENSE +21 -0
- package/README.md +246 -0
- package/agents/AGENTS-MANIFEST.md +42 -0
- package/agents/brain.md +42 -0
- package/agents/context-builder.md +46 -0
- package/agents/delegate.md +12 -0
- package/agents/dev-1.md +42 -0
- package/agents/oracle.md +73 -0
- package/agents/planner.md +55 -0
- package/agents/researcher.md +52 -0
- package/agents/reviewer.md +79 -0
- package/agents/scout.md +50 -0
- package/agents/tester.md +45 -0
- package/agents/worker.md +55 -0
- package/extensions/ralph.ts +1 -0
- package/extensions/reviewer-extension.ts +125 -0
- package/extensions/task-orchestrator.ts +28 -0
- package/package.json +63 -0
- package/prompts/gather-context-and-clarify.md +13 -0
- package/prompts/parallel-cleanup.md +59 -0
- package/prompts/parallel-context-build.md +53 -0
- package/prompts/parallel-handoff-plan.md +59 -0
- package/prompts/parallel-research.md +50 -0
- package/prompts/parallel-review.md +54 -0
- package/prompts/review-loop.md +41 -0
- package/skills/orchid/SKILL.md +214 -0
- package/skills/orchid/orchid-cleanup/SKILL.md +122 -0
- package/skills/orchid/orchid-converge/SKILL.md +124 -0
- package/skills/orchid/orchid-decompose/SKILL.md +201 -0
- package/skills/orchid/orchid-doctor/SKILL.md +162 -0
- package/skills/orchid/orchid-investigate/SKILL.md +102 -0
- package/skills/orchid/orchid-launch/SKILL.md +147 -0
- package/skills/ralph/SKILL.md +73 -0
- package/skills/subagents/pi-subagents/SKILL.md +813 -0
- package/src/index.ts +7 -0
- package/src/orchestrator/abort.ts +534 -0
- package/src/orchestrator/agent-bridge-extension.ts +1020 -0
- package/src/orchestrator/agent-host.ts +954 -0
- package/src/orchestrator/cleanup.ts +776 -0
- package/src/orchestrator/config-loader.ts +1412 -0
- package/src/orchestrator/config-schema.ts +690 -0
- package/src/orchestrator/config.ts +81 -0
- package/src/orchestrator/context-window.ts +66 -0
- package/src/orchestrator/diagnostic-reports.ts +475 -0
- package/src/orchestrator/diagnostics.ts +394 -0
- package/src/orchestrator/discovery.ts +1833 -0
- package/src/orchestrator/engine-worker.ts +415 -0
- package/src/orchestrator/engine.ts +5940 -0
- package/src/orchestrator/execution.ts +3104 -0
- package/src/orchestrator/extension.ts +5934 -0
- package/src/orchestrator/formatting.ts +785 -0
- package/src/orchestrator/git.ts +88 -0
- package/src/orchestrator/index.ts +28 -0
- package/src/orchestrator/lane-runner.ts +1787 -0
- package/src/orchestrator/mailbox.ts +780 -0
- package/src/orchestrator/merge.ts +3414 -0
- package/src/orchestrator/messages.ts +1062 -0
- package/src/orchestrator/migrations.ts +278 -0
- package/src/orchestrator/naming.ts +117 -0
- package/src/orchestrator/path-resolver.ts +275 -0
- package/src/orchestrator/persistence.ts +2625 -0
- package/src/orchestrator/process-registry.ts +452 -0
- package/src/orchestrator/quality-gate.ts +1085 -0
- package/src/orchestrator/resume.ts +3488 -0
- package/src/orchestrator/sessions.ts +57 -0
- package/src/orchestrator/settings-loader.ts +136 -0
- package/src/orchestrator/settings-tui.ts +2208 -0
- package/src/orchestrator/sidecar-telemetry.ts +267 -0
- package/src/orchestrator/supervisor.ts +4548 -0
- package/src/orchestrator/task-executor-core.ts +675 -0
- package/src/orchestrator/tmux-compat.ts +37 -0
- package/src/orchestrator/tool-allowlist-constants.ts +37 -0
- package/src/orchestrator/types.ts +4465 -0
- package/src/orchestrator/verification.ts +547 -0
- package/src/orchestrator/waves.ts +1564 -0
- package/src/orchestrator/workspace.ts +707 -0
- package/src/orchestrator/worktree.ts +2725 -0
- package/src/ralph/index.ts +825 -0
- package/src/subagents/agents/agent-management.ts +648 -0
- package/src/subagents/agents/agent-scope.ts +6 -0
- package/src/subagents/agents/agent-selection.ts +23 -0
- package/src/subagents/agents/agent-serializer.ts +86 -0
- package/src/subagents/agents/agents.ts +832 -0
- package/src/subagents/agents/chain-serializer.ts +137 -0
- package/src/subagents/agents/frontmatter.ts +29 -0
- package/src/subagents/agents/identity.ts +30 -0
- package/src/subagents/agents/skills.ts +632 -0
- package/src/subagents/extension/config.ts +16 -0
- package/src/subagents/extension/control-notices.ts +92 -0
- package/src/subagents/extension/doctor.ts +199 -0
- package/src/subagents/extension/fanout-child.ts +170 -0
- package/src/subagents/extension/index.ts +573 -0
- package/src/subagents/extension/schemas.ts +168 -0
- package/src/subagents/intercom/intercom-bridge.ts +379 -0
- package/src/subagents/intercom/result-intercom.ts +377 -0
- package/src/subagents/runs/background/async-execution.ts +712 -0
- package/src/subagents/runs/background/async-job-tracker.ts +310 -0
- package/src/subagents/runs/background/async-resume.ts +345 -0
- package/src/subagents/runs/background/async-status.ts +325 -0
- package/src/subagents/runs/background/completion-dedupe.ts +63 -0
- package/src/subagents/runs/background/notify.ts +108 -0
- package/src/subagents/runs/background/parallel-groups.ts +45 -0
- package/src/subagents/runs/background/result-watcher.ts +307 -0
- package/src/subagents/runs/background/run-id-resolver.ts +83 -0
- package/src/subagents/runs/background/run-status.ts +269 -0
- package/src/subagents/runs/background/stale-run-reconciler.ts +336 -0
- package/src/subagents/runs/background/subagent-runner.ts +1808 -0
- package/src/subagents/runs/background/top-level-async.ts +13 -0
- package/src/subagents/runs/foreground/chain-clarify.ts +1333 -0
- package/src/subagents/runs/foreground/chain-execution.ts +938 -0
- package/src/subagents/runs/foreground/execution.ts +918 -0
- package/src/subagents/runs/foreground/subagent-executor.ts +2527 -0
- package/src/subagents/runs/shared/completion-guard.ts +147 -0
- package/src/subagents/runs/shared/long-running-guard.ts +175 -0
- package/src/subagents/runs/shared/mcp-direct-tool-allowlist.ts +365 -0
- package/src/subagents/runs/shared/model-fallback.ts +103 -0
- package/src/subagents/runs/shared/nested-events.ts +819 -0
- package/src/subagents/runs/shared/nested-path.ts +52 -0
- package/src/subagents/runs/shared/nested-render.ts +115 -0
- package/src/subagents/runs/shared/parallel-utils.ts +109 -0
- package/src/subagents/runs/shared/pi-args.ts +220 -0
- package/src/subagents/runs/shared/pi-spawn.ts +115 -0
- package/src/subagents/runs/shared/run-history.ts +60 -0
- package/src/subagents/runs/shared/single-output.ts +164 -0
- package/src/subagents/runs/shared/subagent-control.ts +226 -0
- package/src/subagents/runs/shared/subagent-prompt-runtime.ts +170 -0
- package/src/subagents/runs/shared/worktree.ts +577 -0
- package/src/subagents/shared/artifacts.ts +98 -0
- package/src/subagents/shared/atomic-json.ts +16 -0
- package/src/subagents/shared/file-coalescer.ts +40 -0
- package/src/subagents/shared/fork-context.ts +76 -0
- package/src/subagents/shared/formatters.ts +133 -0
- package/src/subagents/shared/jsonl-writer.ts +81 -0
- package/src/subagents/shared/model-info.ts +78 -0
- package/src/subagents/shared/post-exit-stdio-guard.ts +85 -0
- package/src/subagents/shared/session-identity.ts +10 -0
- package/src/subagents/shared/session-tokens.ts +44 -0
- package/src/subagents/shared/settings.ts +397 -0
- package/src/subagents/shared/status-format.ts +49 -0
- package/src/subagents/shared/types.ts +822 -0
- package/src/subagents/shared/utils.ts +450 -0
- package/src/subagents/slash/prompt-template-bridge.ts +397 -0
- package/src/subagents/slash/slash-bridge.ts +174 -0
- package/src/subagents/slash/slash-commands.ts +528 -0
- package/src/subagents/slash/slash-live-state.ts +292 -0
- package/src/subagents/tui/render-helpers.ts +80 -0
- package/src/subagents/tui/render.ts +1358 -0
- package/templates/agents/local/supervisor.md +33 -0
- package/templates/agents/local/task-merger.md +27 -0
- package/templates/agents/local/task-reviewer.md +30 -0
- package/templates/agents/local/task-worker.md +34 -0
- package/templates/agents/supervisor-routing.md +92 -0
- package/templates/agents/supervisor.md +229 -0
- package/templates/agents/task-merger.md +214 -0
- package/templates/agents/task-reviewer.md +260 -0
- package/templates/agents/task-worker-segment.md +44 -0
- package/templates/agents/task-worker.md +557 -0
- package/templates/tasks/CONTEXT.md +30 -0
- package/templates/tasks/EXAMPLE-001-hello-world/PROMPT.md +98 -0
- package/templates/tasks/EXAMPLE-001-hello-world/STATUS.md +73 -0
- package/templates/tasks/EXAMPLE-002-parallel-smoke/PROMPT.md +97 -0
- package/templates/tasks/EXAMPLE-002-parallel-smoke/STATUS.md +73 -0
|
@@ -0,0 +1,938 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chain execution logic for subagent tool
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import * as fs from "node:fs";
|
|
6
|
+
import * as path from "node:path";
|
|
7
|
+
import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
8
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
9
|
+
import type { AgentConfig } from "../../agents/agents.ts";
|
|
10
|
+
import { ChainClarifyComponent, type ChainClarifyResult, type BehaviorOverride } from "./chain-clarify.ts";
|
|
11
|
+
import { toModelInfo, type ModelInfo } from "../../shared/model-info.ts";
|
|
12
|
+
import {
|
|
13
|
+
resolveChainTemplates,
|
|
14
|
+
createChainDir,
|
|
15
|
+
removeChainDir,
|
|
16
|
+
resolveStepBehavior,
|
|
17
|
+
resolveParallelBehaviors,
|
|
18
|
+
buildChainInstructions,
|
|
19
|
+
writeInitialProgressFile,
|
|
20
|
+
createParallelDirs,
|
|
21
|
+
suppressProgressForReadOnlyTask,
|
|
22
|
+
aggregateParallelOutputs,
|
|
23
|
+
isParallelStep,
|
|
24
|
+
type StepOverrides,
|
|
25
|
+
type ChainStep,
|
|
26
|
+
type SequentialStep,
|
|
27
|
+
type ParallelTaskResult,
|
|
28
|
+
type ResolvedStepBehavior,
|
|
29
|
+
type ResolvedTemplates,
|
|
30
|
+
} from "../../shared/settings.ts";
|
|
31
|
+
import { discoverAvailableSkills, normalizeSkillInput } from "../../agents/skills.ts";
|
|
32
|
+
import { INTERCOM_BRIDGE_MARKER } from "../../intercom/intercom-bridge.ts";
|
|
33
|
+
import { runSync } from "./execution.ts";
|
|
34
|
+
import { buildChainSummary } from "../../shared/formatters.ts";
|
|
35
|
+
import { compactForegroundDetails, getSingleResultOutput, mapConcurrent, resolveChildCwd } from "../../shared/utils.ts";
|
|
36
|
+
import { recordRun } from "../shared/run-history.ts";
|
|
37
|
+
import {
|
|
38
|
+
cleanupWorktrees,
|
|
39
|
+
createWorktrees,
|
|
40
|
+
diffWorktrees,
|
|
41
|
+
findWorktreeTaskCwdConflict,
|
|
42
|
+
formatWorktreeDiffSummary,
|
|
43
|
+
formatWorktreeTaskCwdConflict,
|
|
44
|
+
type WorktreeSetup,
|
|
45
|
+
} from "../shared/worktree.ts";
|
|
46
|
+
import {
|
|
47
|
+
type ActivityState,
|
|
48
|
+
type AgentProgress,
|
|
49
|
+
type ArtifactConfig,
|
|
50
|
+
type ArtifactPaths,
|
|
51
|
+
type ControlEvent,
|
|
52
|
+
type Details,
|
|
53
|
+
type IntercomEventBus,
|
|
54
|
+
type NestedRouteInfo,
|
|
55
|
+
type ResolvedControlConfig,
|
|
56
|
+
type SingleResult,
|
|
57
|
+
MAX_CONCURRENCY,
|
|
58
|
+
resolveChildMaxSubagentDepth,
|
|
59
|
+
} from "../../shared/types.ts";
|
|
60
|
+
import { resolveModelCandidate } from "../shared/model-fallback.ts";
|
|
61
|
+
import { validateFileOnlyOutputMode } from "../shared/single-output.ts";
|
|
62
|
+
|
|
63
|
+
interface ChainExecutionDetailsInput {
|
|
64
|
+
results: SingleResult[];
|
|
65
|
+
includeProgress?: boolean;
|
|
66
|
+
allProgress: AgentProgress[];
|
|
67
|
+
allArtifactPaths: ArtifactPaths[];
|
|
68
|
+
artifactsDir: string;
|
|
69
|
+
chainAgents: string[];
|
|
70
|
+
totalSteps: number;
|
|
71
|
+
currentStepIndex?: number;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface ParallelChainRunInput {
|
|
75
|
+
step: Exclude<ChainStep, SequentialStep>;
|
|
76
|
+
parallelTemplates: string[];
|
|
77
|
+
parallelBehaviors: ResolvedStepBehavior[];
|
|
78
|
+
agents: AgentConfig[];
|
|
79
|
+
stepIndex: number;
|
|
80
|
+
availableModels: ModelInfo[];
|
|
81
|
+
chainDir: string;
|
|
82
|
+
prev: string;
|
|
83
|
+
originalTask: string;
|
|
84
|
+
ctx: ExtensionContext;
|
|
85
|
+
intercomEvents?: IntercomEventBus;
|
|
86
|
+
cwd?: string;
|
|
87
|
+
runId: string;
|
|
88
|
+
globalTaskIndex: number;
|
|
89
|
+
sessionDirForIndex: (idx?: number) => string | undefined;
|
|
90
|
+
sessionFileForIndex?: (idx?: number) => string | undefined;
|
|
91
|
+
shareEnabled: boolean;
|
|
92
|
+
artifactConfig: ArtifactConfig;
|
|
93
|
+
artifactsDir: string;
|
|
94
|
+
signal?: AbortSignal;
|
|
95
|
+
onUpdate?: (r: AgentToolResult<Details>) => void;
|
|
96
|
+
onControlEvent?: (event: ControlEvent) => void;
|
|
97
|
+
controlConfig: ResolvedControlConfig;
|
|
98
|
+
childIntercomTarget?: (agent: string, index: number) => string | undefined;
|
|
99
|
+
orchestratorIntercomTarget?: string;
|
|
100
|
+
foregroundControl?: {
|
|
101
|
+
updatedAt: number;
|
|
102
|
+
currentAgent?: string;
|
|
103
|
+
currentIndex?: number;
|
|
104
|
+
currentActivityState?: ActivityState;
|
|
105
|
+
lastActivityAt?: number;
|
|
106
|
+
currentTool?: string;
|
|
107
|
+
currentToolStartedAt?: number;
|
|
108
|
+
interrupt?: () => boolean;
|
|
109
|
+
};
|
|
110
|
+
results: SingleResult[];
|
|
111
|
+
allProgress: AgentProgress[];
|
|
112
|
+
chainAgents: string[];
|
|
113
|
+
totalSteps: number;
|
|
114
|
+
worktreeSetup?: WorktreeSetup;
|
|
115
|
+
maxSubagentDepth: number;
|
|
116
|
+
nestedRoute?: NestedRouteInfo;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function buildChainExecutionDetails(input: ChainExecutionDetailsInput): Details {
|
|
120
|
+
return compactForegroundDetails({
|
|
121
|
+
mode: "chain",
|
|
122
|
+
results: input.results,
|
|
123
|
+
progress: input.includeProgress ? input.allProgress : undefined,
|
|
124
|
+
artifacts: input.allArtifactPaths.length ? { dir: input.artifactsDir, files: input.allArtifactPaths } : undefined,
|
|
125
|
+
chainAgents: input.chainAgents,
|
|
126
|
+
totalSteps: input.totalSteps,
|
|
127
|
+
currentStepIndex: input.currentStepIndex,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function buildChainExecutionErrorResult(message: string, input: ChainExecutionDetailsInput): ChainExecutionResult {
|
|
132
|
+
return {
|
|
133
|
+
content: [{ type: "text", text: message }],
|
|
134
|
+
isError: true,
|
|
135
|
+
details: buildChainExecutionDetails(input),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function ensureParallelProgressFile(
|
|
140
|
+
chainDir: string,
|
|
141
|
+
progressCreated: boolean,
|
|
142
|
+
parallelBehaviors: ResolvedStepBehavior[],
|
|
143
|
+
): boolean {
|
|
144
|
+
if (progressCreated || !parallelBehaviors.some((behavior) => behavior.progress)) {
|
|
145
|
+
return progressCreated;
|
|
146
|
+
}
|
|
147
|
+
writeInitialProgressFile(chainDir);
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function appendParallelWorktreeSummary(
|
|
152
|
+
output: string,
|
|
153
|
+
worktreeSetup: WorktreeSetup | undefined,
|
|
154
|
+
diffsDir: string,
|
|
155
|
+
agents: string[],
|
|
156
|
+
): string {
|
|
157
|
+
if (!worktreeSetup) return output;
|
|
158
|
+
const diffs = diffWorktrees(worktreeSetup, agents, diffsDir);
|
|
159
|
+
const diffSummary = formatWorktreeDiffSummary(diffs);
|
|
160
|
+
if (!diffSummary) return output;
|
|
161
|
+
return `${output}\n\n${diffSummary}`;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async function runParallelChainTasks(input: ParallelChainRunInput): Promise<SingleResult[]> {
|
|
165
|
+
const concurrency = input.step.concurrency ?? MAX_CONCURRENCY;
|
|
166
|
+
const failFast = input.step.failFast ?? false;
|
|
167
|
+
let aborted = false;
|
|
168
|
+
|
|
169
|
+
const parallelResults = await mapConcurrent(
|
|
170
|
+
input.step.parallel,
|
|
171
|
+
concurrency,
|
|
172
|
+
async (task, taskIndex) => {
|
|
173
|
+
if (aborted && failFast) {
|
|
174
|
+
return {
|
|
175
|
+
agent: task.agent,
|
|
176
|
+
task: "(skipped)",
|
|
177
|
+
exitCode: -1,
|
|
178
|
+
messages: [],
|
|
179
|
+
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, turns: 0 },
|
|
180
|
+
error: "Skipped due to fail-fast",
|
|
181
|
+
} as SingleResult;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const taskTemplate = input.parallelTemplates[taskIndex] ?? "{previous}";
|
|
185
|
+
const behavior = suppressProgressForReadOnlyTask(input.parallelBehaviors[taskIndex]!, taskTemplate, input.originalTask);
|
|
186
|
+
const templateHasPrevious = taskTemplate.includes("{previous}");
|
|
187
|
+
const { prefix, suffix } = buildChainInstructions(
|
|
188
|
+
behavior,
|
|
189
|
+
input.chainDir,
|
|
190
|
+
false,
|
|
191
|
+
templateHasPrevious ? undefined : input.prev,
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
let taskStr = taskTemplate;
|
|
195
|
+
taskStr = taskStr.replace(/\{task\}/g, input.originalTask);
|
|
196
|
+
taskStr = taskStr.replace(/\{previous\}/g, input.prev);
|
|
197
|
+
taskStr = taskStr.replace(/\{chain_dir\}/g, input.chainDir);
|
|
198
|
+
const cleanTask = taskStr;
|
|
199
|
+
taskStr = prefix + taskStr + suffix;
|
|
200
|
+
|
|
201
|
+
const taskAgentConfig = input.agents.find((agent) => agent.name === task.agent);
|
|
202
|
+
const effectiveModel =
|
|
203
|
+
(task.model ? resolveModelCandidate(task.model, input.availableModels, input.ctx.model?.provider) : null)
|
|
204
|
+
?? resolveModelCandidate(taskAgentConfig?.model, input.availableModels, input.ctx.model?.provider);
|
|
205
|
+
const maxSubagentDepth = resolveChildMaxSubagentDepth(input.maxSubagentDepth, taskAgentConfig?.maxSubagentDepth);
|
|
206
|
+
|
|
207
|
+
const taskCwd = input.worktreeSetup
|
|
208
|
+
? input.worktreeSetup.worktrees[taskIndex]!.agentCwd
|
|
209
|
+
: resolveChildCwd(input.cwd ?? input.ctx.cwd, task.cwd);
|
|
210
|
+
|
|
211
|
+
const outputPath = typeof behavior.output === "string"
|
|
212
|
+
? (path.isAbsolute(behavior.output) ? behavior.output : path.join(input.chainDir, behavior.output))
|
|
213
|
+
: undefined;
|
|
214
|
+
const interruptController = new AbortController();
|
|
215
|
+
if (input.foregroundControl) {
|
|
216
|
+
input.foregroundControl.currentAgent = task.agent;
|
|
217
|
+
input.foregroundControl.currentIndex = input.globalTaskIndex + taskIndex;
|
|
218
|
+
input.foregroundControl.currentActivityState = undefined;
|
|
219
|
+
input.foregroundControl.updatedAt = Date.now();
|
|
220
|
+
input.foregroundControl.interrupt = () => {
|
|
221
|
+
if (interruptController.signal.aborted) return false;
|
|
222
|
+
interruptController.abort();
|
|
223
|
+
input.foregroundControl!.currentActivityState = undefined;
|
|
224
|
+
input.foregroundControl!.updatedAt = Date.now();
|
|
225
|
+
return true;
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const result = await runSync(input.ctx.cwd, input.agents, task.agent, taskStr, {
|
|
230
|
+
cwd: taskCwd,
|
|
231
|
+
signal: input.signal,
|
|
232
|
+
interruptSignal: interruptController.signal,
|
|
233
|
+
allowIntercomDetach: taskAgentConfig?.systemPrompt?.includes(INTERCOM_BRIDGE_MARKER) === true,
|
|
234
|
+
intercomEvents: input.intercomEvents,
|
|
235
|
+
runId: input.runId,
|
|
236
|
+
index: input.globalTaskIndex + taskIndex,
|
|
237
|
+
sessionDir: input.sessionDirForIndex(input.globalTaskIndex + taskIndex),
|
|
238
|
+
sessionFile: input.sessionFileForIndex?.(input.globalTaskIndex + taskIndex),
|
|
239
|
+
share: input.shareEnabled,
|
|
240
|
+
artifactsDir: input.artifactConfig.enabled ? input.artifactsDir : undefined,
|
|
241
|
+
artifactConfig: input.artifactConfig,
|
|
242
|
+
outputPath,
|
|
243
|
+
outputMode: behavior.outputMode,
|
|
244
|
+
maxSubagentDepth,
|
|
245
|
+
controlConfig: input.controlConfig,
|
|
246
|
+
onControlEvent: input.onControlEvent,
|
|
247
|
+
intercomSessionName: input.childIntercomTarget?.(task.agent, input.globalTaskIndex + taskIndex),
|
|
248
|
+
orchestratorIntercomTarget: input.orchestratorIntercomTarget,
|
|
249
|
+
nestedRoute: input.nestedRoute,
|
|
250
|
+
modelOverride: effectiveModel,
|
|
251
|
+
availableModels: input.availableModels,
|
|
252
|
+
preferredModelProvider: input.ctx.model?.provider,
|
|
253
|
+
skills: behavior.skills === false ? [] : behavior.skills,
|
|
254
|
+
onUpdate: input.onUpdate
|
|
255
|
+
? (progressUpdate) => {
|
|
256
|
+
const stepResults = progressUpdate.details?.results || [];
|
|
257
|
+
const stepProgress = progressUpdate.details?.progress || [];
|
|
258
|
+
if (input.foregroundControl && stepProgress.length > 0) {
|
|
259
|
+
const current = stepProgress[0];
|
|
260
|
+
input.foregroundControl.currentAgent = task.agent;
|
|
261
|
+
input.foregroundControl.currentIndex = input.globalTaskIndex + taskIndex;
|
|
262
|
+
input.foregroundControl.currentActivityState = current?.activityState;
|
|
263
|
+
input.foregroundControl.lastActivityAt = current?.lastActivityAt;
|
|
264
|
+
input.foregroundControl.currentTool = current?.currentTool;
|
|
265
|
+
input.foregroundControl.currentToolStartedAt = current?.currentToolStartedAt;
|
|
266
|
+
input.foregroundControl.currentPath = current?.currentPath;
|
|
267
|
+
input.foregroundControl.turnCount = current?.turnCount;
|
|
268
|
+
input.foregroundControl.tokens = current?.tokens;
|
|
269
|
+
input.foregroundControl.toolCount = current?.toolCount;
|
|
270
|
+
input.foregroundControl.updatedAt = Date.now();
|
|
271
|
+
}
|
|
272
|
+
input.onUpdate?.({
|
|
273
|
+
...progressUpdate,
|
|
274
|
+
details: {
|
|
275
|
+
mode: "chain",
|
|
276
|
+
results: input.results.concat(stepResults),
|
|
277
|
+
progress: input.allProgress.concat(stepProgress),
|
|
278
|
+
controlEvents: progressUpdate.details?.controlEvents,
|
|
279
|
+
chainAgents: input.chainAgents,
|
|
280
|
+
totalSteps: input.totalSteps,
|
|
281
|
+
currentStepIndex: input.stepIndex,
|
|
282
|
+
},
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
: undefined,
|
|
286
|
+
});
|
|
287
|
+
if (input.foregroundControl?.currentIndex === input.globalTaskIndex + taskIndex) {
|
|
288
|
+
input.foregroundControl.interrupt = undefined;
|
|
289
|
+
input.foregroundControl.updatedAt = Date.now();
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (result.exitCode !== 0 && failFast) {
|
|
293
|
+
aborted = true;
|
|
294
|
+
}
|
|
295
|
+
recordRun(task.agent, cleanTask, result.exitCode, result.progressSummary?.durationMs ?? 0);
|
|
296
|
+
return result;
|
|
297
|
+
},
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
return parallelResults;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
interface ChainExecutionParams {
|
|
304
|
+
chain: ChainStep[];
|
|
305
|
+
task?: string;
|
|
306
|
+
agents: AgentConfig[];
|
|
307
|
+
ctx: ExtensionContext;
|
|
308
|
+
intercomEvents?: IntercomEventBus;
|
|
309
|
+
signal?: AbortSignal;
|
|
310
|
+
runId: string;
|
|
311
|
+
cwd?: string;
|
|
312
|
+
shareEnabled: boolean;
|
|
313
|
+
sessionDirForIndex: (idx?: number) => string | undefined;
|
|
314
|
+
sessionFileForIndex?: (idx?: number) => string | undefined;
|
|
315
|
+
artifactsDir: string;
|
|
316
|
+
artifactConfig: ArtifactConfig;
|
|
317
|
+
includeProgress?: boolean;
|
|
318
|
+
clarify?: boolean;
|
|
319
|
+
onUpdate?: (r: AgentToolResult<Details>) => void;
|
|
320
|
+
onControlEvent?: (event: ControlEvent) => void;
|
|
321
|
+
controlConfig: ResolvedControlConfig;
|
|
322
|
+
childIntercomTarget?: (agent: string, index: number) => string | undefined;
|
|
323
|
+
orchestratorIntercomTarget?: string;
|
|
324
|
+
foregroundControl?: {
|
|
325
|
+
updatedAt: number;
|
|
326
|
+
currentAgent?: string;
|
|
327
|
+
currentIndex?: number;
|
|
328
|
+
currentActivityState?: ActivityState;
|
|
329
|
+
lastActivityAt?: number;
|
|
330
|
+
currentTool?: string;
|
|
331
|
+
currentToolStartedAt?: number;
|
|
332
|
+
interrupt?: () => boolean;
|
|
333
|
+
};
|
|
334
|
+
chainSkills?: string[];
|
|
335
|
+
chainDir?: string;
|
|
336
|
+
maxSubagentDepth: number;
|
|
337
|
+
nestedRoute?: NestedRouteInfo;
|
|
338
|
+
worktreeSetupHook?: string;
|
|
339
|
+
worktreeSetupHookTimeoutMs?: number;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
interface ChainExecutionResult {
|
|
343
|
+
content: Array<{ type: "text"; text: string }>;
|
|
344
|
+
details: Details;
|
|
345
|
+
isError?: boolean;
|
|
346
|
+
/** User requested async execution via TUI - caller should dispatch to executeAsyncChain */
|
|
347
|
+
requestedAsync?: {
|
|
348
|
+
chain: ChainStep[];
|
|
349
|
+
chainSkills: string[];
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Execute a chain of subagent steps
|
|
355
|
+
*/
|
|
356
|
+
export async function executeChain(params: ChainExecutionParams): Promise<ChainExecutionResult> {
|
|
357
|
+
const {
|
|
358
|
+
chain: chainSteps,
|
|
359
|
+
agents,
|
|
360
|
+
ctx,
|
|
361
|
+
signal,
|
|
362
|
+
runId,
|
|
363
|
+
cwd,
|
|
364
|
+
shareEnabled,
|
|
365
|
+
sessionDirForIndex,
|
|
366
|
+
sessionFileForIndex,
|
|
367
|
+
artifactsDir,
|
|
368
|
+
artifactConfig,
|
|
369
|
+
includeProgress,
|
|
370
|
+
clarify,
|
|
371
|
+
onUpdate,
|
|
372
|
+
onControlEvent,
|
|
373
|
+
controlConfig,
|
|
374
|
+
childIntercomTarget,
|
|
375
|
+
orchestratorIntercomTarget,
|
|
376
|
+
foregroundControl,
|
|
377
|
+
intercomEvents,
|
|
378
|
+
chainSkills: chainSkillsParam,
|
|
379
|
+
chainDir: chainDirBase,
|
|
380
|
+
} = params;
|
|
381
|
+
const chainSkills = chainSkillsParam ?? [];
|
|
382
|
+
|
|
383
|
+
const allProgress: AgentProgress[] = [];
|
|
384
|
+
const allArtifactPaths: ArtifactPaths[] = [];
|
|
385
|
+
|
|
386
|
+
const chainAgents: string[] = chainSteps.map((step) =>
|
|
387
|
+
isParallelStep(step)
|
|
388
|
+
? `[${step.parallel.map((t) => t.agent).join("+")}]`
|
|
389
|
+
: (step as SequentialStep).agent,
|
|
390
|
+
);
|
|
391
|
+
const totalSteps = chainSteps.length;
|
|
392
|
+
|
|
393
|
+
const firstStep = chainSteps[0]!;
|
|
394
|
+
const originalTask = params.task
|
|
395
|
+
?? (isParallelStep(firstStep) ? firstStep.parallel[0]!.task! : (firstStep as SequentialStep).task!);
|
|
396
|
+
|
|
397
|
+
const chainDir = createChainDir(runId, chainDirBase);
|
|
398
|
+
const hasParallelSteps = chainSteps.some(isParallelStep);
|
|
399
|
+
let templates: ResolvedTemplates = resolveChainTemplates(chainSteps);
|
|
400
|
+
const shouldClarify = clarify !== false && ctx.hasUI && !hasParallelSteps;
|
|
401
|
+
let tuiBehaviorOverrides: (BehaviorOverride | undefined)[] | undefined;
|
|
402
|
+
const availableModels: ModelInfo[] = ctx.modelRegistry.getAvailable().map(toModelInfo);
|
|
403
|
+
const availableSkills = discoverAvailableSkills(cwd ?? ctx.cwd);
|
|
404
|
+
|
|
405
|
+
if (shouldClarify) {
|
|
406
|
+
const seqSteps = chainSteps as SequentialStep[];
|
|
407
|
+
const agentConfigs: AgentConfig[] = [];
|
|
408
|
+
for (const step of seqSteps) {
|
|
409
|
+
const config = agents.find((a) => a.name === step.agent);
|
|
410
|
+
if (!config) {
|
|
411
|
+
removeChainDir(chainDir);
|
|
412
|
+
return {
|
|
413
|
+
content: [{ type: "text", text: `Unknown agent: ${step.agent}` }],
|
|
414
|
+
isError: true,
|
|
415
|
+
details: { mode: "chain" as const, results: [] },
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
agentConfigs.push(config);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const stepOverrides: StepOverrides[] = seqSteps.map((step) => ({
|
|
422
|
+
output: step.output,
|
|
423
|
+
outputMode: step.outputMode,
|
|
424
|
+
reads: step.reads,
|
|
425
|
+
progress: step.progress,
|
|
426
|
+
skills: normalizeSkillInput(step.skill),
|
|
427
|
+
model: step.model,
|
|
428
|
+
}));
|
|
429
|
+
|
|
430
|
+
const resolvedBehaviors = agentConfigs.map((config, i) =>
|
|
431
|
+
resolveStepBehavior(config, stepOverrides[i]!, chainSkills),
|
|
432
|
+
);
|
|
433
|
+
const flatTemplates = templates as string[];
|
|
434
|
+
|
|
435
|
+
const result = await ctx.ui.custom<ChainClarifyResult>(
|
|
436
|
+
(tui, theme, _kb, done) =>
|
|
437
|
+
new ChainClarifyComponent(
|
|
438
|
+
tui,
|
|
439
|
+
theme,
|
|
440
|
+
agentConfigs,
|
|
441
|
+
flatTemplates,
|
|
442
|
+
originalTask,
|
|
443
|
+
chainDir,
|
|
444
|
+
resolvedBehaviors,
|
|
445
|
+
availableModels,
|
|
446
|
+
ctx.model?.provider,
|
|
447
|
+
availableSkills,
|
|
448
|
+
done,
|
|
449
|
+
),
|
|
450
|
+
{
|
|
451
|
+
overlay: true,
|
|
452
|
+
overlayOptions: { anchor: "center", width: 84, maxHeight: "80%" },
|
|
453
|
+
},
|
|
454
|
+
);
|
|
455
|
+
|
|
456
|
+
if (!result || !result.confirmed) {
|
|
457
|
+
removeChainDir(chainDir);
|
|
458
|
+
return {
|
|
459
|
+
content: [{ type: "text", text: "Chain cancelled" }],
|
|
460
|
+
details: { mode: "chain", results: [] },
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
if (result.runInBackground) {
|
|
465
|
+
removeChainDir(chainDir);
|
|
466
|
+
const updatedChain: ChainStep[] = chainSteps.map((step, i) => {
|
|
467
|
+
if (isParallelStep(step)) return step;
|
|
468
|
+
const override = result.behaviorOverrides[i];
|
|
469
|
+
return {
|
|
470
|
+
...step,
|
|
471
|
+
task: result.templates[i]!,
|
|
472
|
+
...(override?.model ? { model: override.model } : {}),
|
|
473
|
+
...(override?.output !== undefined ? { output: override.output } : {}),
|
|
474
|
+
...("outputMode" in step && step.outputMode !== undefined ? { outputMode: step.outputMode } : {}),
|
|
475
|
+
...(override?.reads !== undefined ? { reads: override.reads } : {}),
|
|
476
|
+
...(override?.progress !== undefined ? { progress: override.progress } : {}),
|
|
477
|
+
...(override?.skills !== undefined ? { skill: override.skills } : {}),
|
|
478
|
+
};
|
|
479
|
+
});
|
|
480
|
+
return {
|
|
481
|
+
content: [{ type: "text", text: "Launching in background..." }],
|
|
482
|
+
details: { mode: "chain", results: [] },
|
|
483
|
+
requestedAsync: { chain: updatedChain, chainSkills },
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
templates = result.templates;
|
|
488
|
+
tuiBehaviorOverrides = result.behaviorOverrides;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
const results: SingleResult[] = [];
|
|
492
|
+
let prev = "";
|
|
493
|
+
let globalTaskIndex = 0;
|
|
494
|
+
let progressCreated = false;
|
|
495
|
+
|
|
496
|
+
for (let stepIndex = 0; stepIndex < chainSteps.length; stepIndex++) {
|
|
497
|
+
const step = chainSteps[stepIndex]!;
|
|
498
|
+
const stepTemplates = templates[stepIndex]!;
|
|
499
|
+
|
|
500
|
+
if (isParallelStep(step)) {
|
|
501
|
+
const parallelTemplates = stepTemplates as string[];
|
|
502
|
+
const parallelCwd = resolveChildCwd(cwd ?? ctx.cwd, step.cwd);
|
|
503
|
+
let worktreeSetup: WorktreeSetup | undefined;
|
|
504
|
+
if (step.worktree) {
|
|
505
|
+
const worktreeTaskCwdConflict = findWorktreeTaskCwdConflict(step.parallel, parallelCwd);
|
|
506
|
+
if (worktreeTaskCwdConflict) {
|
|
507
|
+
return buildChainExecutionErrorResult(
|
|
508
|
+
`parallel chain step ${stepIndex + 1}: ${formatWorktreeTaskCwdConflict(worktreeTaskCwdConflict, parallelCwd)}`,
|
|
509
|
+
{
|
|
510
|
+
results,
|
|
511
|
+
includeProgress,
|
|
512
|
+
allProgress,
|
|
513
|
+
allArtifactPaths,
|
|
514
|
+
artifactsDir,
|
|
515
|
+
chainAgents,
|
|
516
|
+
totalSteps,
|
|
517
|
+
currentStepIndex: stepIndex,
|
|
518
|
+
},
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
try {
|
|
522
|
+
worktreeSetup = createWorktrees(parallelCwd, `${runId}-s${stepIndex}`, step.parallel.length, {
|
|
523
|
+
agents: step.parallel.map((task) => task.agent),
|
|
524
|
+
setupHook: params.worktreeSetupHook
|
|
525
|
+
? { hookPath: params.worktreeSetupHook, timeoutMs: params.worktreeSetupHookTimeoutMs }
|
|
526
|
+
: undefined,
|
|
527
|
+
});
|
|
528
|
+
} catch (error) {
|
|
529
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
530
|
+
return buildChainExecutionErrorResult(message, {
|
|
531
|
+
results,
|
|
532
|
+
includeProgress,
|
|
533
|
+
allProgress,
|
|
534
|
+
allArtifactPaths,
|
|
535
|
+
artifactsDir,
|
|
536
|
+
chainAgents,
|
|
537
|
+
totalSteps,
|
|
538
|
+
currentStepIndex: stepIndex,
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
try {
|
|
544
|
+
const agentNames = step.parallel.map((task) => task.agent);
|
|
545
|
+
const parallelBehaviors = resolveParallelBehaviors(step.parallel, agents, stepIndex, chainSkills)
|
|
546
|
+
.map((behavior, taskIndex) => suppressProgressForReadOnlyTask(behavior, parallelTemplates[taskIndex] ?? step.parallel[taskIndex]?.task, originalTask));
|
|
547
|
+
for (let taskIndex = 0; taskIndex < step.parallel.length; taskIndex++) {
|
|
548
|
+
const behavior = parallelBehaviors[taskIndex]!;
|
|
549
|
+
const outputPath = typeof behavior.output === "string"
|
|
550
|
+
? (path.isAbsolute(behavior.output) ? behavior.output : path.join(chainDir, behavior.output))
|
|
551
|
+
: undefined;
|
|
552
|
+
const validationError = validateFileOnlyOutputMode(behavior.outputMode, outputPath, `Parallel chain step ${stepIndex + 1} task ${taskIndex + 1} (${step.parallel[taskIndex]!.agent})`);
|
|
553
|
+
if (validationError) return buildChainExecutionErrorResult(validationError, {
|
|
554
|
+
results,
|
|
555
|
+
includeProgress,
|
|
556
|
+
allProgress,
|
|
557
|
+
allArtifactPaths,
|
|
558
|
+
artifactsDir,
|
|
559
|
+
chainAgents,
|
|
560
|
+
totalSteps,
|
|
561
|
+
currentStepIndex: stepIndex,
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
progressCreated = ensureParallelProgressFile(chainDir, progressCreated, parallelBehaviors);
|
|
565
|
+
createParallelDirs(chainDir, stepIndex, step.parallel.length, agentNames);
|
|
566
|
+
|
|
567
|
+
const parallelResults = await runParallelChainTasks({
|
|
568
|
+
step,
|
|
569
|
+
parallelTemplates,
|
|
570
|
+
parallelBehaviors,
|
|
571
|
+
agents,
|
|
572
|
+
stepIndex,
|
|
573
|
+
availableModels,
|
|
574
|
+
chainDir,
|
|
575
|
+
prev,
|
|
576
|
+
originalTask,
|
|
577
|
+
ctx,
|
|
578
|
+
intercomEvents,
|
|
579
|
+
cwd,
|
|
580
|
+
runId,
|
|
581
|
+
globalTaskIndex,
|
|
582
|
+
sessionDirForIndex,
|
|
583
|
+
sessionFileForIndex,
|
|
584
|
+
shareEnabled,
|
|
585
|
+
artifactConfig,
|
|
586
|
+
artifactsDir,
|
|
587
|
+
signal,
|
|
588
|
+
onUpdate,
|
|
589
|
+
results,
|
|
590
|
+
allProgress,
|
|
591
|
+
chainAgents,
|
|
592
|
+
totalSteps,
|
|
593
|
+
controlConfig,
|
|
594
|
+
onControlEvent,
|
|
595
|
+
childIntercomTarget,
|
|
596
|
+
orchestratorIntercomTarget,
|
|
597
|
+
foregroundControl,
|
|
598
|
+
nestedRoute: params.nestedRoute,
|
|
599
|
+
worktreeSetup,
|
|
600
|
+
maxSubagentDepth: params.maxSubagentDepth,
|
|
601
|
+
});
|
|
602
|
+
globalTaskIndex += step.parallel.length;
|
|
603
|
+
|
|
604
|
+
for (const result of parallelResults) {
|
|
605
|
+
results.push(result);
|
|
606
|
+
if (result.progress) allProgress.push(result.progress);
|
|
607
|
+
if (result.artifactPaths) allArtifactPaths.push(result.artifactPaths);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
const interrupted = parallelResults.find((result) => result.interrupted);
|
|
611
|
+
if (interrupted) {
|
|
612
|
+
return {
|
|
613
|
+
content: [{ type: "text", text: `Chain paused after interrupt at step ${stepIndex + 1} (${interrupted.agent}). Waiting for explicit next action.` }],
|
|
614
|
+
details: buildChainExecutionDetails({
|
|
615
|
+
results,
|
|
616
|
+
includeProgress,
|
|
617
|
+
allProgress,
|
|
618
|
+
allArtifactPaths,
|
|
619
|
+
artifactsDir,
|
|
620
|
+
chainAgents,
|
|
621
|
+
totalSteps,
|
|
622
|
+
currentStepIndex: stepIndex,
|
|
623
|
+
}),
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
const detachedIndexInStep = parallelResults.findIndex((result) => result.detached);
|
|
627
|
+
const detached = detachedIndexInStep >= 0 ? parallelResults[detachedIndexInStep] : undefined;
|
|
628
|
+
if (detached) {
|
|
629
|
+
return {
|
|
630
|
+
content: [{ type: "text", text: `Chain detached for intercom coordination at step ${stepIndex + 1} (${detached.agent}). Reply to the supervisor request first. After the child exits, start a fresh follow-up if needed.` }],
|
|
631
|
+
details: buildChainExecutionDetails({
|
|
632
|
+
results,
|
|
633
|
+
includeProgress,
|
|
634
|
+
allProgress,
|
|
635
|
+
allArtifactPaths,
|
|
636
|
+
artifactsDir,
|
|
637
|
+
chainAgents,
|
|
638
|
+
totalSteps,
|
|
639
|
+
currentStepIndex: stepIndex,
|
|
640
|
+
}),
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
const failures = parallelResults
|
|
645
|
+
.map((result, originalIndex) => ({ ...result, originalIndex }))
|
|
646
|
+
.filter((result) => result.exitCode !== 0 && result.exitCode !== -1);
|
|
647
|
+
if (failures.length > 0) {
|
|
648
|
+
const failureSummary = failures
|
|
649
|
+
.map((failure) => `- Task ${failure.originalIndex + 1} (${failure.agent}): ${failure.error || "failed"}`)
|
|
650
|
+
.join("\n");
|
|
651
|
+
const errorMsg = `Parallel step ${stepIndex + 1} failed:\n${failureSummary}`;
|
|
652
|
+
const summary = buildChainSummary(chainSteps, results, chainDir, "failed", {
|
|
653
|
+
index: stepIndex,
|
|
654
|
+
error: errorMsg,
|
|
655
|
+
});
|
|
656
|
+
return {
|
|
657
|
+
content: [{ type: "text", text: summary }],
|
|
658
|
+
isError: true,
|
|
659
|
+
details: buildChainExecutionDetails({
|
|
660
|
+
results,
|
|
661
|
+
includeProgress,
|
|
662
|
+
allProgress,
|
|
663
|
+
allArtifactPaths,
|
|
664
|
+
artifactsDir,
|
|
665
|
+
chainAgents,
|
|
666
|
+
totalSteps,
|
|
667
|
+
currentStepIndex: stepIndex,
|
|
668
|
+
}),
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
const taskResults: ParallelTaskResult[] = parallelResults.map((result, i) => {
|
|
673
|
+
const outputTarget = parallelBehaviors[i]?.output;
|
|
674
|
+
const outputTargetPath = typeof outputTarget === "string"
|
|
675
|
+
? (path.isAbsolute(outputTarget) ? outputTarget : path.join(chainDir, outputTarget))
|
|
676
|
+
: undefined;
|
|
677
|
+
return {
|
|
678
|
+
agent: result.agent,
|
|
679
|
+
taskIndex: i,
|
|
680
|
+
output: getSingleResultOutput(result),
|
|
681
|
+
exitCode: result.exitCode,
|
|
682
|
+
error: result.error,
|
|
683
|
+
outputTargetPath,
|
|
684
|
+
outputTargetExists: outputTargetPath ? fs.existsSync(outputTargetPath) : undefined,
|
|
685
|
+
};
|
|
686
|
+
});
|
|
687
|
+
prev = aggregateParallelOutputs(taskResults);
|
|
688
|
+
prev = appendParallelWorktreeSummary(
|
|
689
|
+
prev,
|
|
690
|
+
worktreeSetup,
|
|
691
|
+
path.join(chainDir, "worktree-diffs", `step-${stepIndex}`),
|
|
692
|
+
agentNames,
|
|
693
|
+
);
|
|
694
|
+
} finally {
|
|
695
|
+
if (worktreeSetup) cleanupWorktrees(worktreeSetup);
|
|
696
|
+
}
|
|
697
|
+
} else {
|
|
698
|
+
const seqStep = step as SequentialStep;
|
|
699
|
+
const stepTemplate = stepTemplates as string;
|
|
700
|
+
|
|
701
|
+
const agentConfig = agents.find((a) => a.name === seqStep.agent);
|
|
702
|
+
if (!agentConfig) {
|
|
703
|
+
removeChainDir(chainDir);
|
|
704
|
+
return {
|
|
705
|
+
content: [{ type: "text", text: `Unknown agent: ${seqStep.agent}` }],
|
|
706
|
+
isError: true,
|
|
707
|
+
details: { mode: "chain" as const, results: [] },
|
|
708
|
+
};
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
const tuiOverride = tuiBehaviorOverrides?.[stepIndex];
|
|
712
|
+
const stepOverride: StepOverrides = {
|
|
713
|
+
output: tuiOverride?.output !== undefined ? tuiOverride.output : seqStep.output,
|
|
714
|
+
outputMode: seqStep.outputMode,
|
|
715
|
+
reads: tuiOverride?.reads !== undefined ? tuiOverride.reads : seqStep.reads,
|
|
716
|
+
progress: tuiOverride?.progress !== undefined ? tuiOverride.progress : seqStep.progress,
|
|
717
|
+
skills:
|
|
718
|
+
tuiOverride?.skills !== undefined
|
|
719
|
+
? tuiOverride.skills
|
|
720
|
+
: normalizeSkillInput(seqStep.skill),
|
|
721
|
+
};
|
|
722
|
+
const behavior = suppressProgressForReadOnlyTask(resolveStepBehavior(agentConfig, stepOverride, chainSkills), stepTemplate, originalTask);
|
|
723
|
+
|
|
724
|
+
const isFirstProgress = behavior.progress && !progressCreated;
|
|
725
|
+
if (isFirstProgress) {
|
|
726
|
+
progressCreated = true;
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
const templateHasPrevious = stepTemplate.includes("{previous}");
|
|
730
|
+
const { prefix, suffix } = buildChainInstructions(
|
|
731
|
+
behavior,
|
|
732
|
+
chainDir,
|
|
733
|
+
isFirstProgress,
|
|
734
|
+
templateHasPrevious ? undefined : prev,
|
|
735
|
+
);
|
|
736
|
+
|
|
737
|
+
let stepTask = stepTemplate;
|
|
738
|
+
stepTask = stepTask.replace(/\{task\}/g, originalTask);
|
|
739
|
+
stepTask = stepTask.replace(/\{previous\}/g, prev);
|
|
740
|
+
stepTask = stepTask.replace(/\{chain_dir\}/g, chainDir);
|
|
741
|
+
const cleanTask = stepTask;
|
|
742
|
+
stepTask = prefix + stepTask + suffix;
|
|
743
|
+
|
|
744
|
+
const effectiveModel =
|
|
745
|
+
tuiOverride?.model
|
|
746
|
+
?? (seqStep.model ? resolveModelCandidate(seqStep.model, availableModels, ctx.model?.provider) : null)
|
|
747
|
+
?? resolveModelCandidate(agentConfig.model, availableModels, ctx.model?.provider);
|
|
748
|
+
|
|
749
|
+
const outputPath = typeof behavior.output === "string"
|
|
750
|
+
? (path.isAbsolute(behavior.output) ? behavior.output : path.join(chainDir, behavior.output))
|
|
751
|
+
: undefined;
|
|
752
|
+
const validationError = validateFileOnlyOutputMode(behavior.outputMode, outputPath, `Chain step ${stepIndex + 1} (${seqStep.agent})`);
|
|
753
|
+
if (validationError) {
|
|
754
|
+
return buildChainExecutionErrorResult(validationError, {
|
|
755
|
+
results,
|
|
756
|
+
includeProgress,
|
|
757
|
+
allProgress,
|
|
758
|
+
allArtifactPaths,
|
|
759
|
+
artifactsDir,
|
|
760
|
+
chainAgents,
|
|
761
|
+
totalSteps,
|
|
762
|
+
currentStepIndex: stepIndex,
|
|
763
|
+
});
|
|
764
|
+
}
|
|
765
|
+
const maxSubagentDepth = resolveChildMaxSubagentDepth(params.maxSubagentDepth, agentConfig.maxSubagentDepth);
|
|
766
|
+
const interruptController = new AbortController();
|
|
767
|
+
if (foregroundControl) {
|
|
768
|
+
foregroundControl.currentAgent = seqStep.agent;
|
|
769
|
+
foregroundControl.currentIndex = globalTaskIndex;
|
|
770
|
+
foregroundControl.currentActivityState = undefined;
|
|
771
|
+
foregroundControl.updatedAt = Date.now();
|
|
772
|
+
foregroundControl.interrupt = () => {
|
|
773
|
+
if (interruptController.signal.aborted) return false;
|
|
774
|
+
interruptController.abort();
|
|
775
|
+
foregroundControl.currentActivityState = undefined;
|
|
776
|
+
foregroundControl.updatedAt = Date.now();
|
|
777
|
+
return true;
|
|
778
|
+
};
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
const r = await runSync(ctx.cwd, agents, seqStep.agent, stepTask, {
|
|
782
|
+
cwd: resolveChildCwd(cwd ?? ctx.cwd, seqStep.cwd),
|
|
783
|
+
signal,
|
|
784
|
+
interruptSignal: interruptController.signal,
|
|
785
|
+
allowIntercomDetach: agentConfig.systemPrompt?.includes(INTERCOM_BRIDGE_MARKER) === true,
|
|
786
|
+
intercomEvents,
|
|
787
|
+
runId,
|
|
788
|
+
index: globalTaskIndex,
|
|
789
|
+
sessionDir: sessionDirForIndex(globalTaskIndex),
|
|
790
|
+
sessionFile: sessionFileForIndex?.(globalTaskIndex),
|
|
791
|
+
share: shareEnabled,
|
|
792
|
+
artifactsDir: artifactConfig.enabled ? artifactsDir : undefined,
|
|
793
|
+
artifactConfig,
|
|
794
|
+
outputPath,
|
|
795
|
+
outputMode: behavior.outputMode,
|
|
796
|
+
maxSubagentDepth,
|
|
797
|
+
controlConfig,
|
|
798
|
+
onControlEvent,
|
|
799
|
+
intercomSessionName: childIntercomTarget?.(seqStep.agent, globalTaskIndex),
|
|
800
|
+
orchestratorIntercomTarget,
|
|
801
|
+
nestedRoute: params.nestedRoute,
|
|
802
|
+
modelOverride: effectiveModel,
|
|
803
|
+
availableModels,
|
|
804
|
+
preferredModelProvider: ctx.model?.provider,
|
|
805
|
+
skills: behavior.skills === false ? [] : behavior.skills,
|
|
806
|
+
onUpdate: onUpdate
|
|
807
|
+
? (p) => {
|
|
808
|
+
const stepResults = p.details?.results || [];
|
|
809
|
+
const stepProgress = p.details?.progress || [];
|
|
810
|
+
if (foregroundControl && stepProgress.length > 0) {
|
|
811
|
+
const current = stepProgress[0];
|
|
812
|
+
foregroundControl.currentAgent = seqStep.agent;
|
|
813
|
+
foregroundControl.currentIndex = globalTaskIndex;
|
|
814
|
+
foregroundControl.currentActivityState = current?.activityState;
|
|
815
|
+
foregroundControl.lastActivityAt = current?.lastActivityAt;
|
|
816
|
+
foregroundControl.currentTool = current?.currentTool;
|
|
817
|
+
foregroundControl.currentToolStartedAt = current?.currentToolStartedAt;
|
|
818
|
+
foregroundControl.currentPath = current?.currentPath;
|
|
819
|
+
foregroundControl.turnCount = current?.turnCount;
|
|
820
|
+
foregroundControl.tokens = current?.tokens;
|
|
821
|
+
foregroundControl.toolCount = current?.toolCount;
|
|
822
|
+
foregroundControl.updatedAt = Date.now();
|
|
823
|
+
}
|
|
824
|
+
onUpdate({
|
|
825
|
+
...p,
|
|
826
|
+
details: {
|
|
827
|
+
mode: "chain",
|
|
828
|
+
results: results.concat(stepResults),
|
|
829
|
+
progress: allProgress.concat(stepProgress),
|
|
830
|
+
controlEvents: p.details?.controlEvents,
|
|
831
|
+
chainAgents,
|
|
832
|
+
totalSteps,
|
|
833
|
+
currentStepIndex: stepIndex,
|
|
834
|
+
},
|
|
835
|
+
});
|
|
836
|
+
}
|
|
837
|
+
: undefined,
|
|
838
|
+
});
|
|
839
|
+
if (foregroundControl?.currentIndex === globalTaskIndex) {
|
|
840
|
+
foregroundControl.interrupt = undefined;
|
|
841
|
+
foregroundControl.updatedAt = Date.now();
|
|
842
|
+
}
|
|
843
|
+
recordRun(seqStep.agent, cleanTask, r.exitCode, r.progressSummary?.durationMs ?? 0);
|
|
844
|
+
|
|
845
|
+
globalTaskIndex++;
|
|
846
|
+
results.push(r);
|
|
847
|
+
if (r.progress) allProgress.push(r.progress);
|
|
848
|
+
if (r.artifactPaths) allArtifactPaths.push(r.artifactPaths);
|
|
849
|
+
|
|
850
|
+
if (r.interrupted) {
|
|
851
|
+
return {
|
|
852
|
+
content: [{ type: "text", text: `Chain paused after interrupt at step ${stepIndex + 1} (${r.agent}). Waiting for explicit next action.` }],
|
|
853
|
+
details: buildChainExecutionDetails({
|
|
854
|
+
results,
|
|
855
|
+
includeProgress,
|
|
856
|
+
allProgress,
|
|
857
|
+
allArtifactPaths,
|
|
858
|
+
artifactsDir,
|
|
859
|
+
chainAgents,
|
|
860
|
+
totalSteps,
|
|
861
|
+
currentStepIndex: stepIndex,
|
|
862
|
+
}),
|
|
863
|
+
};
|
|
864
|
+
}
|
|
865
|
+
if (r.detached) {
|
|
866
|
+
return {
|
|
867
|
+
content: [{ type: "text", text: `Chain detached for intercom coordination at step ${stepIndex + 1} (${r.agent}). Reply to the supervisor request first. After the child exits, start a fresh follow-up if needed.` }],
|
|
868
|
+
details: buildChainExecutionDetails({
|
|
869
|
+
results,
|
|
870
|
+
includeProgress,
|
|
871
|
+
allProgress,
|
|
872
|
+
allArtifactPaths,
|
|
873
|
+
artifactsDir,
|
|
874
|
+
chainAgents,
|
|
875
|
+
totalSteps,
|
|
876
|
+
currentStepIndex: stepIndex,
|
|
877
|
+
}),
|
|
878
|
+
};
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
if (r.exitCode !== 0) {
|
|
882
|
+
const summary = buildChainSummary(chainSteps, results, chainDir, "failed", {
|
|
883
|
+
index: stepIndex,
|
|
884
|
+
error: r.error || "Chain failed",
|
|
885
|
+
});
|
|
886
|
+
return {
|
|
887
|
+
content: [{ type: "text", text: summary }],
|
|
888
|
+
details: buildChainExecutionDetails({
|
|
889
|
+
results,
|
|
890
|
+
includeProgress,
|
|
891
|
+
allProgress,
|
|
892
|
+
allArtifactPaths,
|
|
893
|
+
artifactsDir,
|
|
894
|
+
chainAgents,
|
|
895
|
+
totalSteps,
|
|
896
|
+
currentStepIndex: stepIndex,
|
|
897
|
+
}),
|
|
898
|
+
isError: true,
|
|
899
|
+
};
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
if (behavior.output) {
|
|
903
|
+
try {
|
|
904
|
+
const expectedPath = path.isAbsolute(behavior.output)
|
|
905
|
+
? behavior.output
|
|
906
|
+
: path.join(chainDir, behavior.output);
|
|
907
|
+
if (!fs.existsSync(expectedPath)) {
|
|
908
|
+
const dirFiles = fs.readdirSync(chainDir);
|
|
909
|
+
const mdFiles = dirFiles.filter((file) => file.endsWith(".md") && file !== "progress.md");
|
|
910
|
+
const warning = mdFiles.length > 0
|
|
911
|
+
? `Agent wrote to different file(s): ${mdFiles.join(", ")} instead of ${behavior.output}`
|
|
912
|
+
: `Agent did not create expected output file: ${behavior.output}`;
|
|
913
|
+
r.error = r.error ? `${r.error}\n${warning}` : warning;
|
|
914
|
+
}
|
|
915
|
+
} catch {
|
|
916
|
+
// Ignore validation errors; this diagnostic should not mask successful chain output.
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
prev = getSingleResultOutput(r);
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
const summary = buildChainSummary(chainSteps, results, chainDir, "completed");
|
|
925
|
+
|
|
926
|
+
return {
|
|
927
|
+
content: [{ type: "text", text: summary }],
|
|
928
|
+
details: buildChainExecutionDetails({
|
|
929
|
+
results,
|
|
930
|
+
includeProgress,
|
|
931
|
+
allProgress,
|
|
932
|
+
allArtifactPaths,
|
|
933
|
+
artifactsDir,
|
|
934
|
+
chainAgents,
|
|
935
|
+
totalSteps,
|
|
936
|
+
}),
|
|
937
|
+
};
|
|
938
|
+
}
|