@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,310 @@
|
|
|
1
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import { renderWidget, widgetRenderKey } from "../../tui/render.ts";
|
|
5
|
+
import { formatControlNoticeMessage } from "../shared/subagent-control.ts";
|
|
6
|
+
import {
|
|
7
|
+
type AsyncJobState,
|
|
8
|
+
type AsyncStartedEvent,
|
|
9
|
+
type ControlEvent,
|
|
10
|
+
type SubagentState,
|
|
11
|
+
POLL_INTERVAL_MS,
|
|
12
|
+
RESULTS_DIR,
|
|
13
|
+
SUBAGENT_CONTROL_EVENT,
|
|
14
|
+
SUBAGENT_CONTROL_INTERCOM_EVENT,
|
|
15
|
+
} from "../../shared/types.ts";
|
|
16
|
+
import { readStatus } from "../../shared/utils.ts";
|
|
17
|
+
import { normalizeParallelGroups } from "./parallel-groups.ts";
|
|
18
|
+
import { reconcileAsyncRun, reconcileNestedAsyncDescendants } from "./stale-run-reconciler.ts";
|
|
19
|
+
import { hasLiveNestedDescendants, updateAsyncJobNestedProjection } from "../shared/nested-events.ts";
|
|
20
|
+
|
|
21
|
+
interface AsyncJobTrackerOptions {
|
|
22
|
+
completionRetentionMs?: number;
|
|
23
|
+
pollIntervalMs?: number;
|
|
24
|
+
resultsDir?: string;
|
|
25
|
+
kill?: (pid: number, signal?: NodeJS.Signals | 0) => boolean;
|
|
26
|
+
now?: () => number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function createAsyncJobTracker(pi: Pick<ExtensionAPI, "events">, state: SubagentState, asyncDirRoot: string, options: AsyncJobTrackerOptions = {}): {
|
|
30
|
+
ensurePoller: () => void;
|
|
31
|
+
handleStarted: (data: unknown) => void;
|
|
32
|
+
handleComplete: (data: unknown) => void;
|
|
33
|
+
resetJobs: (ctx?: ExtensionContext) => void;
|
|
34
|
+
} {
|
|
35
|
+
const completionRetentionMs = options.completionRetentionMs ?? 10000;
|
|
36
|
+
const pollIntervalMs = options.pollIntervalMs ?? POLL_INTERVAL_MS;
|
|
37
|
+
const resultsDir = options.resultsDir ?? RESULTS_DIR;
|
|
38
|
+
const rerenderWidget = (ctx: ExtensionContext, jobs = Array.from(state.asyncJobs.values())) => {
|
|
39
|
+
renderWidget(ctx, jobs);
|
|
40
|
+
ctx.ui.requestRender?.();
|
|
41
|
+
};
|
|
42
|
+
const cancelCleanup = (asyncId: string) => {
|
|
43
|
+
const existingTimer = state.cleanupTimers.get(asyncId);
|
|
44
|
+
if (!existingTimer) return;
|
|
45
|
+
clearTimeout(existingTimer);
|
|
46
|
+
state.cleanupTimers.delete(asyncId);
|
|
47
|
+
};
|
|
48
|
+
const scheduleCleanup = (asyncId: string) => {
|
|
49
|
+
cancelCleanup(asyncId);
|
|
50
|
+
const timer = setTimeout(() => {
|
|
51
|
+
state.cleanupTimers.delete(asyncId);
|
|
52
|
+
state.asyncJobs.delete(asyncId);
|
|
53
|
+
if (state.lastUiContext) {
|
|
54
|
+
rerenderWidget(state.lastUiContext);
|
|
55
|
+
}
|
|
56
|
+
}, completionRetentionMs);
|
|
57
|
+
state.cleanupTimers.set(asyncId, timer);
|
|
58
|
+
};
|
|
59
|
+
const emitNewControlEvents = (job: AsyncJobState) => {
|
|
60
|
+
const eventsPath = path.join(job.asyncDir, "events.jsonl");
|
|
61
|
+
let fd: number;
|
|
62
|
+
try {
|
|
63
|
+
fd = fs.openSync(eventsPath, "r");
|
|
64
|
+
} catch (error) {
|
|
65
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return;
|
|
66
|
+
console.error(`Failed to open async control events for '${job.asyncDir}':`, error);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
const stat = fs.fstatSync(fd);
|
|
71
|
+
const cursor = stat.size < (job.controlEventCursor ?? 0) ? 0 : (job.controlEventCursor ?? 0);
|
|
72
|
+
if (stat.size <= cursor) return;
|
|
73
|
+
const buffer = Buffer.alloc(stat.size - cursor);
|
|
74
|
+
fs.readSync(fd, buffer, 0, buffer.length, cursor);
|
|
75
|
+
const lastNewline = buffer.lastIndexOf(0x0a);
|
|
76
|
+
if (lastNewline === -1) return;
|
|
77
|
+
job.controlEventCursor = cursor + lastNewline + 1;
|
|
78
|
+
for (const line of buffer.subarray(0, lastNewline).toString("utf-8").split("\n")) {
|
|
79
|
+
if (!line.trim()) continue;
|
|
80
|
+
let parsed: unknown;
|
|
81
|
+
try {
|
|
82
|
+
parsed = JSON.parse(line);
|
|
83
|
+
} catch (error) {
|
|
84
|
+
console.error(`Ignoring malformed async control event in '${eventsPath}':`, error);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (!parsed || typeof parsed !== "object" || (parsed as { type?: unknown }).type !== "subagent.control") continue;
|
|
88
|
+
const record = parsed as { event?: ControlEvent; channels?: string[]; childIntercomTarget?: string; noticeText?: string; intercom?: { to?: string; message?: string } };
|
|
89
|
+
if (!record.event || !Array.isArray(record.channels)) continue;
|
|
90
|
+
const payload = {
|
|
91
|
+
event: record.event,
|
|
92
|
+
source: "async" as const,
|
|
93
|
+
asyncDir: job.asyncDir,
|
|
94
|
+
childIntercomTarget: record.childIntercomTarget,
|
|
95
|
+
noticeText: record.noticeText ?? formatControlNoticeMessage(record.event, record.childIntercomTarget),
|
|
96
|
+
};
|
|
97
|
+
if (record.channels.includes("event")) {
|
|
98
|
+
pi.events.emit(SUBAGENT_CONTROL_EVENT, payload);
|
|
99
|
+
}
|
|
100
|
+
if (record.event.type !== "active_long_running" && record.channels.includes("intercom") && record.intercom?.to && record.intercom.message) {
|
|
101
|
+
pi.events.emit(SUBAGENT_CONTROL_INTERCOM_EVENT, {
|
|
102
|
+
...payload,
|
|
103
|
+
to: record.intercom.to,
|
|
104
|
+
message: record.intercom.message,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
} catch (error) {
|
|
109
|
+
console.error(`Failed to read async control events for '${job.asyncDir}':`, error);
|
|
110
|
+
} finally {
|
|
111
|
+
fs.closeSync(fd);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const ensurePoller = () => {
|
|
116
|
+
if (state.poller) return;
|
|
117
|
+
state.poller = setInterval(() => {
|
|
118
|
+
if (state.asyncJobs.size === 0) {
|
|
119
|
+
if (state.lastUiContext?.hasUI) rerenderWidget(state.lastUiContext, []);
|
|
120
|
+
if (state.poller) {
|
|
121
|
+
clearInterval(state.poller);
|
|
122
|
+
state.poller = null;
|
|
123
|
+
}
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
let widgetChanged = false;
|
|
128
|
+
for (const job of state.asyncJobs.values()) {
|
|
129
|
+
const widgetStateBefore = widgetRenderKey(job);
|
|
130
|
+
let nestedRefreshFailed = false;
|
|
131
|
+
const refreshNestedProjection = () => {
|
|
132
|
+
try {
|
|
133
|
+
updateAsyncJobNestedProjection(job);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
nestedRefreshFailed = true;
|
|
136
|
+
console.error(`Failed to refresh nested async descendants for '${job.asyncDir}':`, error);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
const reconcileNestedDescendants = () => {
|
|
140
|
+
try {
|
|
141
|
+
if (job.nestedRoute) reconcileNestedAsyncDescendants(job.nestedRoute, { resultsDir, kill: options.kill, now: options.now });
|
|
142
|
+
} catch (error) {
|
|
143
|
+
nestedRefreshFailed = true;
|
|
144
|
+
console.error(`Failed to refresh nested async descendants for '${job.asyncDir}':`, error);
|
|
145
|
+
}
|
|
146
|
+
refreshNestedProjection();
|
|
147
|
+
};
|
|
148
|
+
try {
|
|
149
|
+
emitNewControlEvents(job);
|
|
150
|
+
reconcileNestedDescendants();
|
|
151
|
+
const reconciliation = reconcileAsyncRun(job.asyncDir, {
|
|
152
|
+
resultsDir,
|
|
153
|
+
kill: options.kill,
|
|
154
|
+
now: options.now,
|
|
155
|
+
startedRun: {
|
|
156
|
+
runId: job.asyncId,
|
|
157
|
+
pid: job.pid,
|
|
158
|
+
sessionId: job.sessionId,
|
|
159
|
+
mode: job.mode,
|
|
160
|
+
agents: job.agents,
|
|
161
|
+
chainStepCount: job.chainStepCount,
|
|
162
|
+
parallelGroups: job.parallelGroups,
|
|
163
|
+
startedAt: job.startedAt,
|
|
164
|
+
sessionFile: job.sessionFile,
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
const status = reconciliation.status ?? readStatus(job.asyncDir);
|
|
168
|
+
if (status) {
|
|
169
|
+
const previousStatus = job.status;
|
|
170
|
+
job.status = status.state;
|
|
171
|
+
if (job.status !== "complete" && job.status !== "failed" && job.status !== "paused") cancelCleanup(job.asyncId);
|
|
172
|
+
job.sessionId = status.sessionId ?? job.sessionId;
|
|
173
|
+
job.activityState = status.activityState;
|
|
174
|
+
job.lastActivityAt = status.lastActivityAt ?? job.lastActivityAt;
|
|
175
|
+
job.currentTool = status.currentTool;
|
|
176
|
+
job.currentToolStartedAt = status.currentToolStartedAt;
|
|
177
|
+
job.currentPath = status.currentPath;
|
|
178
|
+
job.turnCount = status.turnCount ?? job.turnCount;
|
|
179
|
+
job.toolCount = status.toolCount ?? job.toolCount;
|
|
180
|
+
job.mode = status.mode;
|
|
181
|
+
job.currentStep = status.currentStep ?? job.currentStep;
|
|
182
|
+
job.chainStepCount = status.chainStepCount ?? job.chainStepCount;
|
|
183
|
+
job.startedAt = status.startedAt ?? job.startedAt;
|
|
184
|
+
if (status.lastUpdate !== undefined) job.updatedAt = status.lastUpdate;
|
|
185
|
+
if (status.steps?.length) {
|
|
186
|
+
const groups = normalizeParallelGroups(status.parallelGroups, status.steps.length, status.chainStepCount ?? status.steps.length);
|
|
187
|
+
job.parallelGroups = groups.length ? groups : job.parallelGroups;
|
|
188
|
+
job.hasParallelGroups = groups.length > 0 || job.hasParallelGroups;
|
|
189
|
+
const activeGroup = status.currentStep !== undefined
|
|
190
|
+
? groups.find((group) => status.currentStep! >= group.start && status.currentStep! < group.start + group.count)
|
|
191
|
+
: undefined;
|
|
192
|
+
const visibleSteps = activeGroup
|
|
193
|
+
? status.steps.slice(activeGroup.start, activeGroup.start + activeGroup.count).map((step, index) => ({ ...step, index: activeGroup.start + index }))
|
|
194
|
+
: status.steps.map((step, index) => ({ ...step, index }));
|
|
195
|
+
job.activeParallelGroup = Boolean(activeGroup);
|
|
196
|
+
job.agents = visibleSteps.map((step) => step.agent);
|
|
197
|
+
job.steps = visibleSteps;
|
|
198
|
+
refreshNestedProjection();
|
|
199
|
+
job.stepsTotal = visibleSteps.length;
|
|
200
|
+
job.runningSteps = visibleSteps.filter((step) => step.status === "running").length;
|
|
201
|
+
job.completedSteps = visibleSteps.filter((step) => step.status === "complete" || step.status === "completed").length;
|
|
202
|
+
if (status.state === "complete") job.completedSteps = visibleSteps.length;
|
|
203
|
+
}
|
|
204
|
+
job.sessionDir = status.sessionDir ?? job.sessionDir;
|
|
205
|
+
job.outputFile = status.outputFile ?? job.outputFile;
|
|
206
|
+
job.totalTokens = status.totalTokens ?? job.totalTokens;
|
|
207
|
+
job.sessionFile = status.sessionFile ?? job.sessionFile;
|
|
208
|
+
if ((job.status === "complete" || job.status === "failed" || job.status === "paused") && !nestedRefreshFailed && !hasLiveNestedDescendants(job.nestedChildren) && (previousStatus !== job.status || !state.cleanupTimers.has(job.asyncId))) {
|
|
209
|
+
scheduleCleanup(job.asyncId);
|
|
210
|
+
}
|
|
211
|
+
if (widgetRenderKey(job) !== widgetStateBefore) widgetChanged = true;
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
if (job.status === "queued") {
|
|
215
|
+
job.status = "running";
|
|
216
|
+
job.updatedAt = Date.now();
|
|
217
|
+
}
|
|
218
|
+
} catch (error) {
|
|
219
|
+
if (job.status !== "failed") {
|
|
220
|
+
console.error(`Failed to read async status for '${job.asyncDir}':`, error);
|
|
221
|
+
job.status = "failed";
|
|
222
|
+
job.updatedAt = Date.now();
|
|
223
|
+
}
|
|
224
|
+
if (!hasLiveNestedDescendants(job.nestedChildren) && !state.cleanupTimers.has(job.asyncId)) {
|
|
225
|
+
scheduleCleanup(job.asyncId);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (widgetRenderKey(job) !== widgetStateBefore) widgetChanged = true;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (widgetChanged && state.lastUiContext?.hasUI) rerenderWidget(state.lastUiContext);
|
|
232
|
+
}, pollIntervalMs);
|
|
233
|
+
state.poller.unref?.();
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const handleStarted = (data: unknown) => {
|
|
237
|
+
const info = data as AsyncStartedEvent;
|
|
238
|
+
if (!info.id) return;
|
|
239
|
+
const now = Date.now();
|
|
240
|
+
const asyncDir = info.asyncDir ?? path.join(asyncDirRoot, info.id);
|
|
241
|
+
const rawAgents = info.agents?.length ? info.agents : info.chain && info.chain.length > 0 ? info.chain : info.agent ? [info.agent] : undefined;
|
|
242
|
+
const validParallelGroups = normalizeParallelGroups(info.parallelGroups, Number.MAX_SAFE_INTEGER, info.chainStepCount ?? Number.MAX_SAFE_INTEGER);
|
|
243
|
+
const firstGroup = validParallelGroups.find((group) => group.start === 0);
|
|
244
|
+
const firstGroupCount = firstGroup?.count;
|
|
245
|
+
const agents = firstGroupCount && firstGroupCount > 0
|
|
246
|
+
? rawAgents?.slice(0, firstGroupCount)
|
|
247
|
+
: rawAgents;
|
|
248
|
+
state.asyncJobs.set(info.id, {
|
|
249
|
+
asyncId: info.id,
|
|
250
|
+
asyncDir,
|
|
251
|
+
status: "queued",
|
|
252
|
+
pid: typeof info.pid === "number" ? info.pid : undefined,
|
|
253
|
+
...(typeof info.sessionId === "string" ? { sessionId: info.sessionId } : {}),
|
|
254
|
+
mode: info.mode ?? (info.chain ? "chain" : "single"),
|
|
255
|
+
agents,
|
|
256
|
+
chainStepCount: info.chainStepCount,
|
|
257
|
+
parallelGroups: validParallelGroups,
|
|
258
|
+
nestedRoute: info.nestedRoute,
|
|
259
|
+
stepsTotal: firstGroupCount ?? agents?.length,
|
|
260
|
+
hasParallelGroups: validParallelGroups.length > 0,
|
|
261
|
+
activeParallelGroup: Boolean(firstGroupCount && firstGroupCount > 0),
|
|
262
|
+
startedAt: now,
|
|
263
|
+
updatedAt: now,
|
|
264
|
+
});
|
|
265
|
+
ensurePoller();
|
|
266
|
+
if (state.lastUiContext) {
|
|
267
|
+
rerenderWidget(state.lastUiContext);
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
const handleComplete = (data: unknown) => {
|
|
272
|
+
const result = data as { id?: string; success?: boolean; asyncDir?: string };
|
|
273
|
+
const asyncId = result.id;
|
|
274
|
+
if (!asyncId) return;
|
|
275
|
+
const job = state.asyncJobs.get(asyncId);
|
|
276
|
+
let nestedRefreshFailed = false;
|
|
277
|
+
if (job) {
|
|
278
|
+
job.status = result.success ? "complete" : "failed";
|
|
279
|
+
job.updatedAt = Date.now();
|
|
280
|
+
if (result.asyncDir) job.asyncDir = result.asyncDir;
|
|
281
|
+
try {
|
|
282
|
+
updateAsyncJobNestedProjection(job);
|
|
283
|
+
} catch (error) {
|
|
284
|
+
nestedRefreshFailed = true;
|
|
285
|
+
console.error(`Failed to refresh nested async descendants for '${job.asyncDir}':`, error);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
if (state.lastUiContext) {
|
|
289
|
+
rerenderWidget(state.lastUiContext);
|
|
290
|
+
}
|
|
291
|
+
if (!nestedRefreshFailed && !hasLiveNestedDescendants(job?.nestedChildren)) scheduleCleanup(asyncId);
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
const resetJobs = (ctx?: ExtensionContext) => {
|
|
295
|
+
for (const timer of state.cleanupTimers.values()) {
|
|
296
|
+
clearTimeout(timer);
|
|
297
|
+
}
|
|
298
|
+
state.cleanupTimers.clear();
|
|
299
|
+
state.asyncJobs.clear();
|
|
300
|
+
state.foregroundControls?.clear();
|
|
301
|
+
state.lastForegroundControlId = null;
|
|
302
|
+
state.resultFileCoalescer.clear();
|
|
303
|
+
if (ctx?.hasUI) {
|
|
304
|
+
state.lastUiContext = ctx;
|
|
305
|
+
rerenderWidget(ctx, []);
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
return { ensurePoller, handleStarted, handleComplete, resetJobs };
|
|
310
|
+
}
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { ASYNC_DIR, RESULTS_DIR, type AsyncStatus } from "../../shared/types.ts";
|
|
4
|
+
import { resolveSubagentIntercomTarget } from "../../intercom/intercom-bridge.ts";
|
|
5
|
+
import { reconcileAsyncRun } from "./stale-run-reconciler.ts";
|
|
6
|
+
|
|
7
|
+
export interface AsyncResumeParams {
|
|
8
|
+
id?: string;
|
|
9
|
+
runId?: string;
|
|
10
|
+
dir?: string;
|
|
11
|
+
index?: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface AsyncResumeDeps {
|
|
15
|
+
asyncDirRoot?: string;
|
|
16
|
+
resultsDir?: string;
|
|
17
|
+
kill?: (pid: number, signal?: NodeJS.Signals | 0) => boolean;
|
|
18
|
+
now?: () => number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type AsyncResumeTarget = {
|
|
22
|
+
kind: "live" | "revive";
|
|
23
|
+
runId: string;
|
|
24
|
+
asyncDir?: string;
|
|
25
|
+
state: AsyncStatus["state"];
|
|
26
|
+
agent: string;
|
|
27
|
+
index: number;
|
|
28
|
+
intercomTarget: string;
|
|
29
|
+
cwd?: string;
|
|
30
|
+
sessionFile?: string;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
interface AsyncResultFile {
|
|
34
|
+
id?: string;
|
|
35
|
+
runId?: string;
|
|
36
|
+
agent?: string;
|
|
37
|
+
mode?: string;
|
|
38
|
+
state?: string;
|
|
39
|
+
success?: boolean;
|
|
40
|
+
cwd?: string;
|
|
41
|
+
sessionFile?: string;
|
|
42
|
+
results?: Array<{ agent?: string; success?: boolean; sessionFile?: string; intercomTarget?: string }>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface AsyncRunLocation {
|
|
46
|
+
asyncDir: string | null;
|
|
47
|
+
resultPath: string | null;
|
|
48
|
+
resolvedId?: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function getErrorMessage(error: unknown): string {
|
|
52
|
+
return error instanceof Error ? error.message : String(error);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function ensureObject(value: unknown, source: string): Record<string, unknown> {
|
|
56
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
57
|
+
throw new Error(`Async result file '${source}' must contain a JSON object.`);
|
|
58
|
+
}
|
|
59
|
+
return value as Record<string, unknown>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function validateOptionalString(value: Record<string, unknown>, field: string, source: string, displayField = field): string | undefined {
|
|
63
|
+
const fieldValue = value[field];
|
|
64
|
+
if (fieldValue === undefined) return undefined;
|
|
65
|
+
if (typeof fieldValue !== "string") throw new Error(`Invalid async result file '${source}': ${displayField} must be a string.`);
|
|
66
|
+
return fieldValue;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function validateResultFile(value: unknown, resultPath: string): AsyncResultFile {
|
|
70
|
+
const data = ensureObject(value, resultPath);
|
|
71
|
+
const resultsValue = data.results;
|
|
72
|
+
let results: AsyncResultFile["results"];
|
|
73
|
+
if (resultsValue !== undefined) {
|
|
74
|
+
if (!Array.isArray(resultsValue)) throw new Error(`Invalid async result file '${resultPath}': results must be an array.`);
|
|
75
|
+
results = resultsValue.map((entry, index) => {
|
|
76
|
+
const child = ensureObject(entry, `${resultPath} results[${index}]`);
|
|
77
|
+
const agent = validateOptionalString(child, "agent", resultPath, `results[${index}].agent`);
|
|
78
|
+
const sessionFile = validateOptionalString(child, "sessionFile", resultPath, `results[${index}].sessionFile`);
|
|
79
|
+
const intercomTarget = validateOptionalString(child, "intercomTarget", resultPath, `results[${index}].intercomTarget`);
|
|
80
|
+
const success = child.success;
|
|
81
|
+
if (success !== undefined && typeof success !== "boolean") throw new Error(`Invalid async result file '${resultPath}': results[${index}].success must be a boolean.`);
|
|
82
|
+
return { agent, sessionFile, intercomTarget, ...(typeof success === "boolean" ? { success } : {}) };
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
const success = data.success;
|
|
86
|
+
if (success !== undefined && typeof success !== "boolean") throw new Error(`Invalid async result file '${resultPath}': success must be a boolean.`);
|
|
87
|
+
return {
|
|
88
|
+
id: validateOptionalString(data, "id", resultPath),
|
|
89
|
+
runId: validateOptionalString(data, "runId", resultPath),
|
|
90
|
+
agent: validateOptionalString(data, "agent", resultPath),
|
|
91
|
+
mode: validateOptionalString(data, "mode", resultPath),
|
|
92
|
+
state: validateOptionalString(data, "state", resultPath),
|
|
93
|
+
cwd: validateOptionalString(data, "cwd", resultPath),
|
|
94
|
+
sessionFile: validateOptionalString(data, "sessionFile", resultPath),
|
|
95
|
+
...(typeof success === "boolean" ? { success } : {}),
|
|
96
|
+
...(results ? { results } : {}),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function readResultFile(resultPath: string): AsyncResultFile {
|
|
101
|
+
let raw: string;
|
|
102
|
+
try {
|
|
103
|
+
raw = fs.readFileSync(resultPath, "utf-8");
|
|
104
|
+
} catch (error) {
|
|
105
|
+
throw new Error(`Failed to read async result file '${resultPath}': ${getErrorMessage(error)}`, {
|
|
106
|
+
cause: error instanceof Error ? error : undefined,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
return validateResultFile(JSON.parse(raw), resultPath);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
if (error instanceof SyntaxError) {
|
|
113
|
+
throw new Error(`Failed to parse async result file '${resultPath}': ${getErrorMessage(error)}`, {
|
|
114
|
+
cause: error,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function assertRunId(value: string | undefined, field: "id" | "runId"): string | undefined {
|
|
122
|
+
if (value === undefined) return undefined;
|
|
123
|
+
if (value.trim() === "") throw new Error(`${field} must not be empty.`);
|
|
124
|
+
if (path.isAbsolute(value) || /[\\/]/.test(value) || value.includes("..")) {
|
|
125
|
+
throw new Error(`${field} must be an async run id or prefix, not a path.`);
|
|
126
|
+
}
|
|
127
|
+
return value;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function assertInsideRoot(root: string, target: string, label: string): void {
|
|
131
|
+
const rootPath = path.resolve(root);
|
|
132
|
+
const targetPath = path.resolve(target);
|
|
133
|
+
const relative = path.relative(rootPath, targetPath);
|
|
134
|
+
if (relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative))) return;
|
|
135
|
+
throw new Error(`${label} must be inside ${rootPath}.`);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function prefixedRunIds(dir: string, prefix: string, suffix = ""): string[] {
|
|
139
|
+
if (!fs.existsSync(dir)) return [];
|
|
140
|
+
return fs.readdirSync(dir)
|
|
141
|
+
.filter((entry) => entry.startsWith(prefix) && (!suffix || entry.endsWith(suffix)))
|
|
142
|
+
.map((entry) => suffix ? entry.slice(0, -suffix.length) : entry)
|
|
143
|
+
.sort();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function exactResultPath(resultsDir: string, runId: string): string | null {
|
|
147
|
+
const resultPath = path.join(resultsDir, `${runId}.json`);
|
|
148
|
+
assertInsideRoot(resultsDir, resultPath, "Async result file");
|
|
149
|
+
return fs.existsSync(resultPath) ? resultPath : null;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function findAsyncRunPrefixMatches(prefix: string, asyncDirRoot: string, resultsDir: string): Array<{ id: string; location: AsyncRunLocation }> {
|
|
153
|
+
const requestedId = assertRunId(prefix, "id");
|
|
154
|
+
if (!requestedId) return [];
|
|
155
|
+
const asyncRoot = path.resolve(asyncDirRoot);
|
|
156
|
+
const resultRoot = path.resolve(resultsDir);
|
|
157
|
+
const matchingIds = [...new Set([
|
|
158
|
+
...prefixedRunIds(asyncRoot, requestedId),
|
|
159
|
+
...prefixedRunIds(resultRoot, requestedId, ".json"),
|
|
160
|
+
])].sort();
|
|
161
|
+
return matchingIds.map((id) => {
|
|
162
|
+
const asyncDir = path.join(asyncRoot, id);
|
|
163
|
+
assertInsideRoot(asyncRoot, asyncDir, "Async run directory");
|
|
164
|
+
return {
|
|
165
|
+
id,
|
|
166
|
+
location: {
|
|
167
|
+
asyncDir: fs.existsSync(asyncDir) ? asyncDir : null,
|
|
168
|
+
resultPath: exactResultPath(resultRoot, id),
|
|
169
|
+
resolvedId: id,
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function resolveAsyncRunLocation(params: AsyncResumeParams, asyncDirRoot: string, resultsDir: string): AsyncRunLocation {
|
|
176
|
+
const asyncRoot = path.resolve(asyncDirRoot);
|
|
177
|
+
const resultRoot = path.resolve(resultsDir);
|
|
178
|
+
const requestedId = assertRunId(params.id, "id") ?? assertRunId(params.runId, "runId");
|
|
179
|
+
if (params.dir) {
|
|
180
|
+
const asyncDir = path.resolve(params.dir);
|
|
181
|
+
assertInsideRoot(asyncRoot, asyncDir, "Async run directory");
|
|
182
|
+
const resolvedId = requestedId ?? path.basename(asyncDir);
|
|
183
|
+
if (requestedId && requestedId !== path.basename(asyncDir)) {
|
|
184
|
+
throw new Error(`Async run id '${requestedId}' does not match directory '${path.basename(asyncDir)}'.`);
|
|
185
|
+
}
|
|
186
|
+
return { asyncDir, resultPath: exactResultPath(resultRoot, resolvedId), resolvedId };
|
|
187
|
+
}
|
|
188
|
+
if (!requestedId) return { asyncDir: null, resultPath: null };
|
|
189
|
+
|
|
190
|
+
const directAsyncDir = path.join(asyncRoot, requestedId);
|
|
191
|
+
assertInsideRoot(asyncRoot, directAsyncDir, "Async run directory");
|
|
192
|
+
const directResultPath = exactResultPath(resultRoot, requestedId);
|
|
193
|
+
if (fs.existsSync(directAsyncDir) || directResultPath) {
|
|
194
|
+
return {
|
|
195
|
+
asyncDir: fs.existsSync(directAsyncDir) ? directAsyncDir : null,
|
|
196
|
+
resultPath: directResultPath,
|
|
197
|
+
resolvedId: requestedId,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const matching = findAsyncRunPrefixMatches(requestedId, asyncRoot, resultRoot);
|
|
202
|
+
if (matching.length === 0) return { asyncDir: null, resultPath: null, resolvedId: requestedId };
|
|
203
|
+
if (matching.length > 1) {
|
|
204
|
+
throw new Error(`Ambiguous async run id prefix '${requestedId}' matched: ${matching.map((match) => match.id).join(", ")}. Provide a longer id.`);
|
|
205
|
+
}
|
|
206
|
+
return matching[0]!.location;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function resultState(result: AsyncResultFile): AsyncStatus["state"] {
|
|
210
|
+
if (result.state === "complete" || result.state === "failed" || result.state === "paused" || result.state === "running" || result.state === "queued") {
|
|
211
|
+
return result.state;
|
|
212
|
+
}
|
|
213
|
+
return result.success ? "complete" : "failed";
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function validateStatusForResume(status: AsyncStatus | null, source: string): void {
|
|
217
|
+
if (!status) return;
|
|
218
|
+
if (typeof status.runId !== "string") throw new Error(`Invalid async status '${source}': runId must be a string.`);
|
|
219
|
+
if (status.sessionId !== undefined && typeof status.sessionId !== "string") throw new Error(`Invalid async status '${source}': sessionId must be a string.`);
|
|
220
|
+
if (status.cwd !== undefined && typeof status.cwd !== "string") throw new Error(`Invalid async status '${source}': cwd must be a string.`);
|
|
221
|
+
if (status.sessionFile !== undefined && typeof status.sessionFile !== "string") throw new Error(`Invalid async status '${source}': sessionFile must be a string.`);
|
|
222
|
+
if (status.steps !== undefined) {
|
|
223
|
+
if (!Array.isArray(status.steps)) throw new Error(`Invalid async status '${source}': steps must be an array.`);
|
|
224
|
+
status.steps.forEach((step, index) => {
|
|
225
|
+
if (!step || typeof step !== "object" || Array.isArray(step)) throw new Error(`Invalid async status '${source}': steps[${index}] must be an object.`);
|
|
226
|
+
if (typeof step.agent !== "string") throw new Error(`Invalid async status '${source}': steps[${index}].agent must be a string.`);
|
|
227
|
+
if (step.sessionFile !== undefined && typeof step.sessionFile !== "string") throw new Error(`Invalid async status '${source}': steps[${index}].sessionFile must be a string.`);
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function validateResumeSessionFile(runId: string, sessionFile: string): string {
|
|
233
|
+
if (path.extname(sessionFile) !== ".jsonl") throw new Error(`Async run '${runId}' session file must be a .jsonl file: ${sessionFile}`);
|
|
234
|
+
const resolved = path.resolve(sessionFile);
|
|
235
|
+
if (!fs.existsSync(resolved)) throw new Error(`Async run '${runId}' session file does not exist: ${sessionFile}`);
|
|
236
|
+
return resolved;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function resolveAsyncResumeTarget(params: AsyncResumeParams, deps: AsyncResumeDeps = {}): AsyncResumeTarget {
|
|
240
|
+
const asyncDirRoot = deps.asyncDirRoot ?? ASYNC_DIR;
|
|
241
|
+
const resultsDir = deps.resultsDir ?? RESULTS_DIR;
|
|
242
|
+
const location = resolveAsyncRunLocation(params, asyncDirRoot, resultsDir);
|
|
243
|
+
if (!location.asyncDir && !location.resultPath) {
|
|
244
|
+
throw new Error("Async run not found. Provide id or dir.");
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const reconciliation = location.asyncDir
|
|
248
|
+
? reconcileAsyncRun(location.asyncDir, { resultsDir, kill: deps.kill, now: deps.now })
|
|
249
|
+
: undefined;
|
|
250
|
+
const status = reconciliation?.status ?? null;
|
|
251
|
+
validateStatusForResume(status, location.asyncDir ? path.join(location.asyncDir, "status.json") : "status.json");
|
|
252
|
+
const result = location.resultPath ? readResultFile(location.resultPath) : undefined;
|
|
253
|
+
const runId = status?.runId ?? result?.runId ?? result?.id ?? location.resolvedId ?? (location.asyncDir ? path.basename(location.asyncDir) : "unknown");
|
|
254
|
+
const state = status?.state ?? (result ? resultState(result) : undefined);
|
|
255
|
+
if (!state) throw new Error(`Status file not found for async run '${runId}'.`);
|
|
256
|
+
|
|
257
|
+
const statusSteps = status?.steps ?? [];
|
|
258
|
+
const resultSteps = result?.results ?? [];
|
|
259
|
+
const stepCount = statusSteps.length || resultSteps.length || (result?.agent ? 1 : 0);
|
|
260
|
+
const requestedIndex = params.index;
|
|
261
|
+
if (requestedIndex !== undefined && !Number.isInteger(requestedIndex)) throw new Error(`Async run '${runId}' index must be an integer.`);
|
|
262
|
+
const terminalStepStatuses = new Set(["complete", "completed", "failed", "paused"]);
|
|
263
|
+
|
|
264
|
+
if (state === "running") {
|
|
265
|
+
if (requestedIndex !== undefined) {
|
|
266
|
+
if (requestedIndex < 0 || requestedIndex >= stepCount) throw new Error(`Async run '${runId}' has ${stepCount} children. Index ${requestedIndex} is out of range.`);
|
|
267
|
+
const selectedStep = statusSteps[requestedIndex];
|
|
268
|
+
if (selectedStep?.status === "running") {
|
|
269
|
+
return {
|
|
270
|
+
kind: "live",
|
|
271
|
+
runId,
|
|
272
|
+
asyncDir: location.asyncDir ?? undefined,
|
|
273
|
+
state,
|
|
274
|
+
agent: selectedStep.agent,
|
|
275
|
+
index: requestedIndex,
|
|
276
|
+
intercomTarget: resolveSubagentIntercomTarget(runId, selectedStep.agent, requestedIndex),
|
|
277
|
+
cwd: status?.cwd ?? result?.cwd,
|
|
278
|
+
sessionFile: selectedStep.sessionFile ?? status?.sessionFile ?? result?.sessionFile,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
if (selectedStep?.status === "pending") throw new Error(`Async run '${runId}' child ${requestedIndex} is pending and has not started yet. Wait for it to run or complete before resuming.`);
|
|
282
|
+
if (selectedStep && !terminalStepStatuses.has(selectedStep.status)) throw new Error(`Async run '${runId}' child ${requestedIndex} is ${selectedStep.status} and cannot be revived yet.`);
|
|
283
|
+
} else {
|
|
284
|
+
const running = statusSteps
|
|
285
|
+
.map((step, index) => ({ step, index }))
|
|
286
|
+
.filter(({ step }) => step.status === "running");
|
|
287
|
+
const selected = running.length === 1 ? running[0] : undefined;
|
|
288
|
+
if (!selected) {
|
|
289
|
+
throw new Error(`Async run '${runId}' has ${running.length} running children. Provide index to choose one.`);
|
|
290
|
+
}
|
|
291
|
+
return {
|
|
292
|
+
kind: "live",
|
|
293
|
+
runId,
|
|
294
|
+
asyncDir: location.asyncDir ?? undefined,
|
|
295
|
+
state,
|
|
296
|
+
agent: selected.step.agent,
|
|
297
|
+
index: selected.index,
|
|
298
|
+
intercomTarget: resolveSubagentIntercomTarget(runId, selected.step.agent, selected.index),
|
|
299
|
+
cwd: status?.cwd ?? result?.cwd,
|
|
300
|
+
sessionFile: selected.step.sessionFile ?? status?.sessionFile ?? result?.sessionFile,
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
if (stepCount > 1 && requestedIndex === undefined) {
|
|
306
|
+
throw new Error(`Async run '${runId}' has ${stepCount} children. Provide index to choose one.`);
|
|
307
|
+
}
|
|
308
|
+
const index = requestedIndex ?? 0;
|
|
309
|
+
if (!Number.isInteger(index)) throw new Error(`Async run '${runId}' index must be an integer.`);
|
|
310
|
+
if (index < 0 || index >= stepCount) throw new Error(`Async run '${runId}' has ${stepCount} children. Index ${index} is out of range.`);
|
|
311
|
+
const agent = statusSteps[index]?.agent ?? resultSteps[index]?.agent ?? result?.agent;
|
|
312
|
+
if (!agent) throw new Error(`Could not determine child agent for async run '${runId}'.`);
|
|
313
|
+
const sessionFile = statusSteps[index]?.sessionFile
|
|
314
|
+
?? resultSteps[index]?.sessionFile
|
|
315
|
+
?? (stepCount === 1 ? status?.sessionFile ?? result?.sessionFile : undefined);
|
|
316
|
+
if (!sessionFile) throw new Error(`Async run '${runId}' child ${index} does not have a persisted session file to resume from.`);
|
|
317
|
+
const resolvedSessionFile = validateResumeSessionFile(runId, sessionFile);
|
|
318
|
+
|
|
319
|
+
return {
|
|
320
|
+
kind: "revive",
|
|
321
|
+
runId,
|
|
322
|
+
asyncDir: location.asyncDir ?? undefined,
|
|
323
|
+
state,
|
|
324
|
+
agent,
|
|
325
|
+
index,
|
|
326
|
+
intercomTarget: resolveSubagentIntercomTarget(runId, agent, index),
|
|
327
|
+
cwd: status?.cwd ?? result?.cwd,
|
|
328
|
+
sessionFile: resolvedSessionFile,
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export function buildRevivedAsyncTask(target: AsyncResumeTarget, message: string): string {
|
|
333
|
+
return [
|
|
334
|
+
"You are reviving a previous subagent conversation.",
|
|
335
|
+
"",
|
|
336
|
+
`Original run: ${target.runId}`,
|
|
337
|
+
`Original agent: ${target.agent}`,
|
|
338
|
+
target.sessionFile ? `Original session file: ${target.sessionFile}` : undefined,
|
|
339
|
+
"",
|
|
340
|
+
"Use the stored session context as background. Answer the orchestrator's follow-up below. Do not assume the original child process is still alive.",
|
|
341
|
+
"",
|
|
342
|
+
"Follow-up:",
|
|
343
|
+
message,
|
|
344
|
+
].filter((line): line is string => line !== undefined).join("\n");
|
|
345
|
+
}
|