@ferris1225/pi-subagents 4.1.13 → 4.1.16

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/src/spawn.ts CHANGED
@@ -1,642 +1,650 @@
1
- /**
2
- * Sub-agent result handling and resilient RPC launch orchestration.
3
- *
4
- * The process transport itself lives in rpc-run.ts. Each attempt starts pi in
5
- * persistent `--mode rpc`, sends commands over strict LF-delimited JSONL, and
6
- * settles only on `agent_settled`. This module owns startup-race recovery,
7
- * selected-to-main model handoff, capability-clamped thinking, accounting, and
8
- * result formatting around those attempts.
9
- */
10
-
11
- import { createHash, randomUUID } from "node:crypto";
12
- import { type Dirent, mkdirSync, readdirSync, realpathSync, rmSync, statSync, writeFileSync } from "node:fs";
13
- import { mkdir, mkdtemp, rm } from "node:fs/promises";
14
- import { tmpdir } from "node:os";
15
- import { basename, join, resolve } from "node:path";
16
- import type { Message } from "@earendil-works/pi-ai";
17
- import type { AgentConfig } from "./agents.ts";
18
- import { DEFAULT_THINKING_LEVEL, type ThinkingLevel } from "./config.ts";
19
- import {
20
- currentSubagentDepth,
21
- DEPTH_ENV_VAR,
22
- emptyUsage,
23
- extractToolErrorText,
24
- getPiInvocation,
25
- isRpcCommandTimeoutError,
26
- RpcRunControl,
27
- runRpcAgentAttempt,
28
- sessionExists,
29
- writeChildRetryPolicyExtension,
30
- SUBAGENT_KILL_GRACE_MS,
31
- type RpcSingleResult,
32
- type SubagentLiveEvent,
33
- type UsageStats,
34
- } from "./rpc-run.ts";
35
-
36
- export {
37
- currentSubagentDepth,
38
- DEPTH_ENV_VAR,
39
- extractToolErrorText,
40
- getPiInvocation,
41
- isRpcCommandTimeoutError,
42
- RpcRunControl,
43
- sessionExists,
44
- SUBAGENT_KILL_GRACE_MS,
45
- writeChildRetryPolicyExtension,
46
- };
47
- export type { SubagentLiveEvent, UsageStats };
48
-
49
- export const SUBAGENT_THINKING_LEVEL: ThinkingLevel = DEFAULT_THINKING_LEVEL;
50
- /** 0 disables the watchdog; dispatch supplies the configured timeout. */
51
- export const SUBAGENT_DEFAULT_IDLE_TIMEOUT_MS = 0;
52
- /** Base delays cover Pi's stale-lock window and leave headroom beyond the
53
- * default four-way launch fan-out. Additive jitter below reduces the chance
54
- * that contenders retry in the same lockstep waves. */
55
- export const SUBAGENT_STARTUP_RETRY_DELAYS_MS = [250, 750, 1500, 3000, 6000] as const;
56
- export const MAX_SUBAGENT_STARTUP_FAILURE_DURATION_MS = 2000;
57
- export const MAX_SUBAGENT_STARTUP_RETRY_JITTER_MS = 1000;
58
-
59
- function normalizeStartupRetryDelay(delayMs: number): number {
60
- return Number.isFinite(delayMs) && delayMs > 0 ? delayMs : 0;
61
- }
62
-
63
- export function addStartupRetryJitter(delayMs: number, randomValue = Math.random()): number {
64
- const baseDelay = Math.floor(normalizeStartupRetryDelay(delayMs));
65
- if (baseDelay === 0) return 0;
66
- const boundedRandom = Number.isFinite(randomValue) ? Math.max(0, Math.min(1, randomValue)) : 0;
67
- return baseDelay + Math.floor(Math.min(baseDelay, MAX_SUBAGENT_STARTUP_RETRY_JITTER_MS) * boundedRandom);
68
- }
69
-
70
- export interface SingleResult extends RpcSingleResult {}
71
-
72
- export interface SubagentDetails {
73
- mode: "single" | "parallel";
74
- results: SingleResult[];
75
- background?: boolean;
76
- }
77
-
78
- export function getFinalOutput(messages: Message[]): string {
79
- for (let i = messages.length - 1; i >= 0; i--) {
80
- const msg = messages[i];
81
- if (msg.role === "assistant") {
82
- for (const part of msg.content) {
83
- if (part.type === "text") return part.text;
84
- }
85
- }
86
- }
87
- return "";
88
- }
89
-
90
- /** Only the last standalone reviewer verdict line counts. */
91
- export function reviewVerdict(output: string): "pass" | "fail" | undefined {
92
- const lines = output.split("\n");
93
- for (let index = lines.length - 1; index >= 0; index--) {
94
- const match = /^\s*VERDICT:\s*REVIEW_(PASS|FAIL)\s*$/i.exec(lines[index]);
95
- if (match) return match[1].toUpperCase() === "PASS" ? "pass" : "fail";
96
- }
97
- return undefined;
98
- }
99
-
100
- export const RESULT_LINE_MAX = 200;
101
-
102
- export interface TruncatedOutput {
103
- text: string;
104
- truncated: boolean;
105
- }
106
-
107
- export function truncateResultOutput(output: string, maxLines: number): TruncatedOutput {
108
- const lines = output.split("\n");
109
- if (lines.length <= maxLines && lines.every((line) => line.length <= RESULT_LINE_MAX)) {
110
- return { text: output, truncated: false };
111
- }
112
- const kept = lines.slice(0, maxLines).map((line) =>
113
- line.length > RESULT_LINE_MAX ? `${line.slice(0, RESULT_LINE_MAX)}…` : line,
114
- );
115
- return { text: kept.join("\n"), truncated: true };
116
- }
117
-
118
- export const RESULT_ARTIFACT_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1_000;
119
- export const RESULT_ARTIFACT_MAX_FILES_PER_PROJECT = 50;
120
- // Explicit current prefix plus the strict timestamp/token convention used by 1.1.0.
121
- const RESULT_ARTIFACT_NAME = /^(?:pi-subagent-\d{13,}-[0-9a-f]{12}|\d{13,}-[a-z0-9]{6})-[\w.-]+\.md$/;
122
-
123
- interface ResultArtifactRetentionOptions {
124
- now?: number;
125
- maxAgeMs?: number;
126
- maxFilesPerProject?: number;
127
- }
128
-
129
- /** Remove only stale/overflow Markdown result artifacts. Unknown files and
130
- * symlinks are never touched. Called on each artifact write, so storage stays
131
- * bounded without deleting a result that the current completion just linked. */
132
- export function pruneResultArtifacts(
133
- rootDir: string = join(tmpdir(), "pi-subagents-results"),
134
- options: ResultArtifactRetentionOptions = {},
135
- ): void {
136
- const now = options.now ?? Date.now();
137
- const maxAgeMs = Math.max(0, options.maxAgeMs ?? RESULT_ARTIFACT_MAX_AGE_MS);
138
- const maxFiles = Math.max(0, Math.floor(options.maxFilesPerProject ?? RESULT_ARTIFACT_MAX_FILES_PER_PROJECT));
139
- let projects: Dirent[];
140
- try {
141
- projects = readdirSync(rootDir, { withFileTypes: true });
142
- } catch {
143
- return;
144
- }
145
-
146
- for (const project of projects) {
147
- if (!project.isDirectory() || project.isSymbolicLink()) continue;
148
- const projectDir = join(rootDir, project.name);
149
- let entries: Dirent[];
150
- try {
151
- entries = readdirSync(projectDir, { withFileTypes: true });
152
- } catch {
153
- continue;
154
- }
155
- const artifacts = entries
156
- .filter((entry) => entry.isFile() && !entry.isSymbolicLink() && RESULT_ARTIFACT_NAME.test(entry.name))
157
- .flatMap((entry) => {
158
- const path = join(projectDir, entry.name);
159
- try {
160
- return [{ path, mtimeMs: statSync(path).mtimeMs }];
161
- } catch {
162
- return [];
163
- }
164
- })
165
- .sort((left, right) => right.mtimeMs - left.mtimeMs);
166
-
167
- for (const [index, artifact] of artifacts.entries()) {
168
- if (index < maxFiles && now - artifact.mtimeMs <= maxAgeMs) continue;
169
- try {
170
- rmSync(artifact.path, { force: true });
171
- } catch {
172
- // Temp cleanup is best-effort; result delivery must still succeed.
173
- }
174
- }
175
- }
176
- }
177
-
178
- export function resultArtifactProjectKey(cwd?: string): string {
179
- if (!cwd) return "default";
180
- let canonical: string;
181
- try {
182
- canonical = realpathSync.native(cwd);
183
- } catch {
184
- canonical = resolve(cwd);
185
- }
186
- if (process.platform === "win32") canonical = canonical.toLowerCase();
187
- const slug = basename(canonical).replace(/[^\w.-]+/g, "_") || "project";
188
- const digest = createHash("sha256").update(canonical).digest("hex").slice(0, 12);
189
- return `${slug}-${digest}`;
190
- }
191
-
192
- export function writeResultArtifact(output: string, agentName: string, cwd?: string): string {
193
- const rootDir = join(tmpdir(), "pi-subagents-results");
194
- const dir = join(rootDir, resultArtifactProjectKey(cwd));
195
- mkdirSync(dir, { recursive: true });
196
- const safeName = agentName.replace(/[^\w.-]+/g, "_") || "agent";
197
- const unique = `pi-subagent-${Date.now()}-${randomUUID().replaceAll("-", "").slice(0, 12)}`;
198
- const filePath = join(dir, `${unique}-${safeName}.md`);
199
- writeFileSync(filePath, output, "utf8");
200
- pruneResultArtifacts(rootDir);
201
- return filePath;
202
- }
203
-
204
- export function isFailedResult(result: SingleResult): boolean {
205
-
206
- return result.exitCode !== 0 || result.stopReason === "error" || result.stopReason === "aborted";
207
- }
208
-
209
- function lastAssistantMessage(messages: Message[]): Extract<Message, { role: "assistant" }> | undefined {
210
- for (let index = messages.length - 1; index >= 0; index--) {
211
- const message = messages[index];
212
- if (message.role === "assistant") return message;
213
- }
214
- return undefined;
215
- }
216
-
217
- export function isModelLevelFailure(result: SingleResult): boolean {
218
- if (!isFailedResult(result)) return false;
219
- if (result.stopReason === "aborted") return false;
220
- if (result.dispatchFailed) return false;
221
- if (result.rpcStartupFailed) return false;
222
- // A negative RPC response proves Pi rejected the prompt before execution. It
223
- // remains safe to hand off even though dispatch was attempted; local write,
224
- // close, timeout, and lost-ACK failures never set this explicit flag.
225
- if (result.rpcPromptRejected) return true;
226
- // The prompt write completed but its ACK never arrived. With no later activity
227
- // we cannot know whether Pi started the model or tools, so neither startup
228
- // retry nor selected→main fallback may replay this objective.
229
- if (result.rpcPromptDispatched && !result.rpcPromptAccepted && !result.rpcActivity) return false;
230
- if (isRpcCommandTimeoutError(result.errorMessage)) return false;
231
- if (result.integrationStatus === "retained") return false;
232
- if (result.errorMessage?.includes("idle timeout")) return true;
233
-
234
- // Classification belongs to the final assistant turn, not the whole attempt.
235
- // Earlier useful text or failed tool calls are retained session history and
236
- // must not hide a later provider error (for example a second-turn 503).
237
- const finalAssistant = lastAssistantMessage(result.messages);
238
- if (finalAssistant) {
239
- // Provider streams may preserve partial text on a terminal error. The stop
240
- // reason, not content emptiness, is the transport boundary; ordinary tool or
241
- // task failures settle with a non-error assistant stop reason.
242
- return finalAssistant.stopReason === "error";
243
- }
244
-
245
- if ((result.failedTools?.length ?? 0) > 0) return false;
246
- // No accepted prompt, no activity, and no assistant turn means the provider
247
- // was never reached. Stderr or an exit error here is a startup/transport miss.
248
- if (!result.rpcPromptAccepted && !result.rpcActivity) return false;
249
- return Boolean(
250
- result.rpcPromptAccepted ||
251
- result.rpcActivity ||
252
- result.errorMessage?.trim() ||
253
- result.stderr.trim(),
254
- );
255
- }
256
-
257
- export function isRetryableStartupFailure(result: SingleResult, durationMs: number): boolean {
258
- if (result.exitCode === 0) return false;
259
- if (result.stopReason === "aborted") return false;
260
- if (result.dispatchFailed) return false;
261
- if (result.rpcPromptDispatched || result.rpcPromptAccepted || result.rpcActivity) return false;
262
- if (result.errorMessage?.includes("idle timeout")) return false;
263
- if (getFinalOutput(result.messages)) return false;
264
- if (result.messages.length > 0) return false;
265
- const usage = result.usage;
266
- if (usage.turns || usage.input || usage.output || usage.cacheRead || usage.cacheWrite || usage.cost) return false;
267
- if (result.rpcStartupFailed) return true;
268
- if (durationMs > MAX_SUBAGENT_STARTUP_FAILURE_DURATION_MS) return false;
269
- if (result.stderr.trim().length > 0) return false;
270
- if (result.errorMessage && result.errorMessage.trim().length > 0) return false;
271
- return true;
272
- }
273
-
274
- export function formatStartupRetryExhaustedError(model: string, attempts: number): string {
275
- return `Subagent failed to start after ${attempts} attempt${attempts === 1 ? "" : "s"} on ${model}: the child failed before its initial RPC prompt was dispatched and produced no model, tool, output, or usage activity. This is typically a concurrent pi startup race (several sub-agents starting at once). Retry the dispatch, or dispatch fewer sub-agents at once.`;
276
- }
277
-
278
- export async function waitForStartupRetry(delayMs: number, signal?: AbortSignal): Promise<boolean> {
279
- const normalizedDelay = normalizeStartupRetryDelay(delayMs);
280
- if (normalizedDelay === 0) return !signal?.aborted;
281
- if (!signal) {
282
- return new Promise<boolean>((resolve) => {
283
- const timer = setTimeout(() => resolve(true), normalizedDelay);
284
- if (typeof timer.unref === "function") timer.unref();
285
- });
286
- }
287
- if (signal.aborted) return false;
288
- return new Promise<boolean>((resolve) => {
289
- let settled = false;
290
- const finish = (shouldRetry: boolean): void => {
291
- if (settled) return;
292
- settled = true;
293
- clearTimeout(timer);
294
- signal.removeEventListener("abort", onAbort);
295
- resolve(shouldRetry);
296
- };
297
- const onAbort = (): void => finish(false);
298
- const timer = setTimeout(() => finish(true), normalizedDelay);
299
- if (typeof timer.unref === "function") timer.unref();
300
- signal.addEventListener("abort", onAbort, { once: true });
301
- });
302
- }
303
-
304
- async function waitForControlledRetry(
305
- delayMs: number,
306
- signal: AbortSignal | undefined,
307
- control: RpcRunControl | undefined,
308
- ): Promise<boolean> {
309
- let remaining = normalizeStartupRetryDelay(delayMs);
310
- while (remaining > 0) {
311
- if (control?.isStopRequested()) return false;
312
- const slice = Math.min(remaining, 50);
313
- if (!(await waitForStartupRetry(slice, signal))) return false;
314
- remaining -= slice;
315
- }
316
- return !signal?.aborted && !control?.isStopRequested();
317
- }
318
-
319
- export function getResultOutput(result: SingleResult): string {
320
- if (isFailedResult(result)) {
321
- const error = result.errorMessage || result.stderr;
322
- const partial = getFinalOutput(result.messages);
323
- if (error && partial) return `${error}\n\n--- Partial output ---\n${partial}`;
324
- return error || partial || "(no output)";
325
- }
326
- return getFinalOutput(result.messages) || "(no output)";
327
- }
328
-
329
- export function buildResumePrompt(task: string, reason: string): string {
330
- return `You are resuming an earlier sub-agent session after ${reason}. Your earlier work — searches, reads, edits, and reasoning — is preserved in this session's history above; review it before acting. Current objective: ${task}. Pick up exactly where you left off and finish it. Do NOT redo searches, reads, or edits you already completed unless a step clearly failed. Continue now.`;
331
- }
332
-
333
- /** Create a fresh private session directory under the given root. */
334
- export async function createSessionDir(root: string = tmpdir()): Promise<string> {
335
- await mkdir(root, { recursive: true });
336
- return mkdtemp(join(root, "pi-subagent-session-"));
337
- }
338
-
339
- export function buildFallbackResumeReason(fromModel?: string): string {
340
- return fromModel
341
- ? `the selected model (${fromModel}) failed at the model/provider level, so the current main model is continuing`
342
- : "the selected model failed at the model/provider level, so the current main model is continuing";
343
- }
344
-
345
- export interface RunSingleOptions {
346
- defaultCwd: string;
347
- agent: AgentConfig;
348
- agentName: string;
349
- task: string;
350
- cwd?: string;
351
- thinkingLevel?: ThinkingLevel;
352
- /** Resolve the effective level for each runtime model candidate. */
353
- thinkingLevelForModel?: (modelRef?: string) => ThinkingLevel;
354
- idleTimeoutMs?: number;
355
- startupRetryDelaysMs?: readonly number[];
356
- sessionDir?: string;
357
- sessionId?: string;
358
- /** Parent directory for a fresh session directory. Defaults to the OS temp
359
- * dir; dispatch passes the durable state root so retained sessions survive
360
- * reloads and restarts. */
361
- sessionRoot?: string;
362
- /** Initial RPC prompt. Kept under the old name to limit caller churn. */
363
- stdinText?: string;
364
- /** Refresh parent-derived tools immediately before every startup retry and
365
- * selected-to-main fallback process is spawned. */
366
- resolveAgentForAttempt?: (agent: AgentConfig) => AgentConfig;
367
- signal?: AbortSignal;
368
- onLive?: (event: SubagentLiveEvent) => void;
369
- makeDetails: (results: SingleResult[]) => SubagentDetails;
370
- env?: NodeJS.ProcessEnv;
371
- /** Stable logical-generation controller shared across retry attempts. */
372
- control?: RpcRunControl;
373
- rpcReadyTimeoutMs?: number;
374
- rpcCommandTimeoutMs?: number;
375
- }
376
-
377
- function controlledDisposition(options: RunSingleOptions, base?: SingleResult): SingleResult | undefined {
378
- const control = options.control;
379
- if (!control?.isStopRequested()) return undefined;
380
- const result: SingleResult = base ?? {
381
- agent: options.agentName,
382
- task: control.getObjective(),
383
- exitCode: 0,
384
- messages: [],
385
- stderr: "",
386
- usage: emptyUsage(),
387
- model: options.agent.model,
388
- thinking: options.thinkingLevel,
389
- sessionId: options.sessionId,
390
- sessionDir: options.sessionDir,
391
- };
392
- result.task = control.getObjective();
393
- result.exitCode = 1;
394
- result.stopReason = "aborted";
395
- result.errorMessage = control.getStopMessage();
396
- return result;
397
- }
398
-
399
- function signalAbortDisposition(options: RunSingleOptions, base: SingleResult): SingleResult | undefined {
400
- if (!options.signal?.aborted) return undefined;
401
- base.exitCode = 1;
402
- base.stopReason = "aborted";
403
- base.errorMessage = "Subagent was aborted";
404
- return base;
405
- }
406
-
407
- /** Spawn one RPC attempt and wait for stable settlement. */
408
- export async function runSingleAgent(options: RunSingleOptions): Promise<SingleResult> {
409
- const {
410
- agent,
411
- agentName,
412
- thinkingLevel = SUBAGENT_THINKING_LEVEL,
413
- idleTimeoutMs = SUBAGENT_DEFAULT_IDLE_TIMEOUT_MS,
414
- control,
415
- } = options;
416
- const disposition = controlledDisposition(options);
417
- if (disposition) return disposition;
418
- const objective = control?.getObjective() ?? options.task;
419
- let prompt = options.stdinText ?? `Task: ${objective}`;
420
- if (control && objective !== options.task) {
421
- prompt = options.sessionDir && sessionExists(options.sessionDir, options.sessionId ?? "")
422
- ? `Abandon the previous objective. New objective: ${objective}`
423
- : `Task: ${objective}`;
424
- }
425
- const result = await runRpcAgentAttempt({
426
- defaultCwd: options.defaultCwd,
427
- agent,
428
- agentName,
429
- task: objective,
430
- cwd: options.cwd,
431
- thinkingLevel,
432
- idleTimeoutMs,
433
- sessionDir: options.sessionDir,
434
- sessionId: options.sessionId,
435
- prompt,
436
- signal: options.signal,
437
- onLive: options.onLive,
438
- env: options.env,
439
- control,
440
- rpcReadyTimeoutMs: options.rpcReadyTimeoutMs,
441
- rpcCommandTimeoutMs: options.rpcCommandTimeoutMs,
442
- });
443
- result.task = control?.getObjective() ?? result.task;
444
- return result;
445
- }
446
-
447
- /**
448
- * Run one logical generation on the selected model, then hand directly to the
449
- * current main model after any model/provider-level failure. Startup-race retries
450
- * remain process-level recovery; provider/model retries and extra candidates do not.
451
- * Both attempts resume the same retained Pi session.
452
- */
453
- export async function runSingleAgentWithMainFallback(
454
- options: RunSingleOptions,
455
- mainFallbackRef?: string,
456
- ): Promise<SingleResult> {
457
- const agent = options.agent;
458
- const launchedRef = agent?.model;
459
- const customStartupDelays = options.startupRetryDelaysMs;
460
- const startupDelays = customStartupDelays ?? SUBAGENT_STARTUP_RETRY_DELAYS_MS;
461
-
462
- const sessionId = options.sessionId ?? randomUUID();
463
- const sessionDir = options.sessionDir ?? (await createSessionDir(options.sessionRoot));
464
- const baseOptions: RunSingleOptions = { ...options, sessionDir, sessionId };
465
- if (!options.sessionDir) {
466
- // Surface the fresh session immediately so the dispatching thread can
467
- // persist a durable checkpoint before the child settles.
468
- try {
469
- options.onLive?.({ kind: "session", sessionId, sessionDir });
470
- } catch {
471
- /* never throw from event handling */
472
- }
473
- }
474
-
475
- const dispatchFailure = async (error: unknown): Promise<SingleResult> => {
476
- const errorMessage = error instanceof Error ? error.message : String(error);
477
- const hasSession = sessionExists(sessionDir, sessionId);
478
- if (!hasSession && !options.sessionDir) {
479
- await rm(sessionDir, { recursive: true, force: true }).catch(() => undefined);
480
- }
481
- return {
482
- agent: options.agentName,
483
- task: options.control?.getObjective() ?? options.task,
484
- exitCode: 1,
485
- messages: [],
486
- stderr: errorMessage,
487
- usage: emptyUsage(),
488
- model: options.agent.model,
489
- thinking: options.thinkingLevel,
490
- stopReason: "error",
491
- errorMessage,
492
- dispatchFailed: true,
493
- ...(hasSession || options.sessionDir ? { sessionId, sessionDir } : {}),
494
- };
495
- };
496
-
497
- const runWithStartupRetry = async (opts: RunSingleOptions): Promise<SingleResult> => {
498
- let lastResult: SingleResult;
499
- let retries = 0;
500
- for (let attempt = 0; ; attempt++) {
501
- const immediate = controlledDisposition(opts);
502
- if (immediate) return immediate;
503
- const start = Date.now();
504
- try {
505
- const attemptOptions = opts.resolveAgentForAttempt
506
- ? { ...opts, agent: opts.resolveAgentForAttempt(opts.agent) }
507
- : opts;
508
- lastResult = await runSingleAgent(attemptOptions);
509
- } catch (error) {
510
- const failed = await dispatchFailure(error);
511
- return controlledDisposition(opts, failed) ?? failed;
512
- }
513
- const durationMs = Date.now() - start;
514
- const controlled = controlledDisposition(opts, lastResult);
515
- if (controlled) return controlled;
516
- if (lastResult.stopReason === "aborted") return lastResult;
517
- if (!isRetryableStartupFailure(lastResult, durationMs)) {
518
- if (retries > 0 && !isFailedResult(lastResult)) lastResult.startupRetries = retries;
519
- return lastResult;
520
- }
521
- const delay = startupDelays[attempt];
522
- if (delay === undefined) {
523
- lastResult.errorMessage = formatStartupRetryExhaustedError(
524
- lastResult.model ?? opts.agent.model ?? "default",
525
- attempt + 1,
526
- );
527
- lastResult.stopReason ??= "error";
528
- lastResult.dispatchFailed = true;
529
- return lastResult;
530
- }
531
- opts.control?.markRetrying();
532
- try {
533
- opts.onLive?.({ kind: "status", status: "running" });
534
- } catch {
535
- /* never throw from event handling */
536
- }
537
- const retryDelay = customStartupDelays ? delay : addStartupRetryJitter(delay);
538
- if (!(await waitForControlledRetry(retryDelay, opts.signal, opts.control))) {
539
- return controlledDisposition(opts, lastResult) ?? signalAbortDisposition(opts, lastResult) ?? lastResult;
540
- }
541
- retries++;
542
- }
543
- };
544
-
545
- const selectedRef = launchedRef?.trim() || undefined;
546
- const normalizedMainRef = mainFallbackRef?.trim() || undefined;
547
- const candidates: Array<{ agent: AgentConfig; ref?: string }> = [
548
- { agent, ref: selectedRef },
549
- ];
550
- if (normalizedMainRef && normalizedMainRef !== selectedRef) {
551
- candidates.push({ agent: { ...agent, model: normalizedMainRef }, ref: normalizedMainRef });
552
- }
553
-
554
- let fallbackUsed = false;
555
- let result: SingleResult | undefined;
556
- const priorFailedTools: NonNullable<SingleResult["failedTools"]> = [];
557
- const priorUsage = emptyUsage();
558
-
559
- const retainAttemptDiagnostics = (attempt: SingleResult): void => {
560
- priorFailedTools.push(...(attempt.failedTools ?? []));
561
- priorUsage.input += attempt.usage.input;
562
- priorUsage.output += attempt.usage.output;
563
- priorUsage.cacheRead += attempt.usage.cacheRead;
564
- priorUsage.cacheWrite += attempt.usage.cacheWrite;
565
- priorUsage.cost += attempt.usage.cost;
566
- priorUsage.turns += attempt.usage.turns;
567
- priorUsage.contextTokens = attempt.usage.contextTokens || priorUsage.contextTokens;
568
- };
569
-
570
- const finish = async (settled: SingleResult): Promise<SingleResult> => {
571
- if (priorFailedTools.length > 0) {
572
- settled.failedTools = [...priorFailedTools, ...(settled.failedTools ?? [])];
573
- }
574
- if (
575
- priorUsage.turns || priorUsage.input || priorUsage.output || priorUsage.cacheRead ||
576
- priorUsage.cacheWrite || priorUsage.cost || priorUsage.contextTokens
577
- ) {
578
- settled.usage = {
579
- input: priorUsage.input + settled.usage.input,
580
- output: priorUsage.output + settled.usage.output,
581
- cacheRead: priorUsage.cacheRead + settled.usage.cacheRead,
582
- cacheWrite: priorUsage.cacheWrite + settled.usage.cacheWrite,
583
- cost: priorUsage.cost + settled.usage.cost,
584
- turns: priorUsage.turns + settled.usage.turns,
585
- contextTokens: settled.usage.contextTokens || priorUsage.contextTokens,
586
- };
587
- }
588
- const persistedSession = sessionExists(sessionDir, sessionId);
589
- if (!settled.dispatchFailed || persistedSession || options.sessionDir) {
590
- settled.sessionId ??= sessionId;
591
- settled.sessionDir ??= sessionDir;
592
- } else {
593
- await rm(sessionDir, { recursive: true, force: true }).catch(() => undefined);
594
- settled.sessionId = undefined;
595
- settled.sessionDir = undefined;
596
- }
597
- settled.task = options.control?.getObjective() ?? settled.task;
598
- if (fallbackUsed && launchedRef) settled.modelFallbackFrom = launchedRef;
599
- options.control?.markSettled();
600
- return settled;
601
- };
602
-
603
- for (let candidateIndex = 0; candidateIndex < candidates.length; candidateIndex++) {
604
- const candidate = candidates[candidateIndex];
605
- fallbackUsed ||= candidateIndex > 0;
606
- const previousModel = result?.model ?? candidates[candidateIndex - 1]?.ref;
607
- const candidateThinking = options.thinkingLevelForModel?.(candidate.ref) ?? options.thinkingLevel;
608
- const candidateOptions: RunSingleOptions = {
609
- ...baseOptions,
610
- agent: candidate.agent,
611
- thinkingLevel: candidateThinking,
612
- ...(candidateIndex > 0
613
- ? {
614
- stdinText: buildResumePrompt(
615
- options.control?.getObjective() ?? options.task,
616
- buildFallbackResumeReason(previousModel),
617
- ),
618
- }
619
- : {}),
620
- };
621
- try {
622
- options.onLive?.({
623
- kind: "model",
624
- model: candidate.ref,
625
- thinking: candidateThinking,
626
- ...(candidateIndex > 0 && launchedRef ? { fallbackFrom: launchedRef } : {}),
627
- });
628
- } catch {
629
- /* never throw from event handling */
630
- }
631
-
632
- result = await runWithStartupRetry(candidateOptions);
633
- if (result.stopReason === "aborted") return finish(result);
634
- if (!isModelLevelFailure(result)) return finish(result);
635
- // Any model-level failure advances immediately to the sole fallback (the
636
- // current main model). Retain selected-attempt tool diagnostics and usage;
637
- // ordinary task/tool failures returned above without a handoff.
638
- if (candidateIndex < candidates.length - 1) retainAttemptDiagnostics(result);
639
- }
640
-
641
- return finish(result!);
642
- }
1
+ /**
2
+ * Sub-agent result handling and resilient RPC launch orchestration.
3
+ *
4
+ * The process transport itself lives in rpc-run.ts. Each attempt starts pi in
5
+ * persistent `--mode rpc`, sends commands over strict LF-delimited JSONL, and
6
+ * settles only on `agent_settled`. This module owns startup-race recovery,
7
+ * selected-to-main model handoff, capability-clamped thinking, accounting, and
8
+ * result formatting around those attempts.
9
+ */
10
+
11
+ import { createHash, randomUUID } from "node:crypto";
12
+ import { type Dirent, mkdirSync, readdirSync, realpathSync, rmSync, statSync, writeFileSync } from "node:fs";
13
+ import { mkdir, mkdtemp, rm } from "node:fs/promises";
14
+ import { basename, dirname, join, resolve } from "node:path";
15
+ import type { Message } from "@earendil-works/pi-ai";
16
+ import type { AgentConfig } from "./agents.ts";
17
+ import { DEFAULT_THINKING_LEVEL, type ThinkingLevel } from "./config.ts";
18
+ import {
19
+ currentSubagentDepth,
20
+ DEPTH_ENV_VAR,
21
+ emptyUsage,
22
+ extractToolErrorText,
23
+ getPiInvocation,
24
+ isRpcCommandTimeoutError,
25
+ RpcRunControl,
26
+ runRpcAgentAttempt,
27
+ sessionExists,
28
+ writeChildRetryPolicyExtension,
29
+ SUBAGENT_KILL_GRACE_MS,
30
+ type RpcSingleResult,
31
+ type SubagentLiveEvent,
32
+ type UsageStats,
33
+ } from "./rpc-run.ts";
34
+
35
+ export {
36
+ currentSubagentDepth,
37
+ DEPTH_ENV_VAR,
38
+ extractToolErrorText,
39
+ getPiInvocation,
40
+ isRpcCommandTimeoutError,
41
+ RpcRunControl,
42
+ sessionExists,
43
+ SUBAGENT_KILL_GRACE_MS,
44
+ writeChildRetryPolicyExtension,
45
+ };
46
+ export type { SubagentLiveEvent, UsageStats };
47
+
48
+ export const SUBAGENT_THINKING_LEVEL: ThinkingLevel = DEFAULT_THINKING_LEVEL;
49
+ /** 0 disables the watchdog; dispatch supplies the configured timeout. */
50
+ export const SUBAGENT_DEFAULT_IDLE_TIMEOUT_MS = 0;
51
+ /** Base delays cover Pi's stale-lock window and leave headroom beyond the
52
+ * default four-way launch fan-out. Additive jitter below reduces the chance
53
+ * that contenders retry in the same lockstep waves. */
54
+ export const SUBAGENT_STARTUP_RETRY_DELAYS_MS = [250, 750, 1500, 3000, 6000] as const;
55
+ export const MAX_SUBAGENT_STARTUP_FAILURE_DURATION_MS = 2000;
56
+ export const MAX_SUBAGENT_STARTUP_RETRY_JITTER_MS = 1000;
57
+
58
+ function normalizeStartupRetryDelay(delayMs: number): number {
59
+ return Number.isFinite(delayMs) && delayMs > 0 ? delayMs : 0;
60
+ }
61
+
62
+ export function addStartupRetryJitter(delayMs: number, randomValue = Math.random()): number {
63
+ const baseDelay = Math.floor(normalizeStartupRetryDelay(delayMs));
64
+ if (baseDelay === 0) return 0;
65
+ const boundedRandom = Number.isFinite(randomValue) ? Math.max(0, Math.min(1, randomValue)) : 0;
66
+ return baseDelay + Math.floor(Math.min(baseDelay, MAX_SUBAGENT_STARTUP_RETRY_JITTER_MS) * boundedRandom);
67
+ }
68
+
69
+ export interface SingleResult extends RpcSingleResult {}
70
+
71
+ export interface SubagentDetails {
72
+ mode: "single" | "parallel";
73
+ results: SingleResult[];
74
+ background?: boolean;
75
+ }
76
+
77
+ export function getFinalOutput(messages: Message[]): string {
78
+ for (let i = messages.length - 1; i >= 0; i--) {
79
+ const msg = messages[i];
80
+ if (msg.role === "assistant") {
81
+ for (const part of msg.content) {
82
+ if (part.type === "text") return part.text;
83
+ }
84
+ }
85
+ }
86
+ return "";
87
+ }
88
+
89
+ /** Only the last standalone reviewer verdict line counts. */
90
+ export function reviewVerdict(output: string): "pass" | "fail" | undefined {
91
+ const lines = output.split("\n");
92
+ for (let index = lines.length - 1; index >= 0; index--) {
93
+ const match = /^\s*VERDICT:\s*REVIEW_(PASS|FAIL)\s*$/i.exec(lines[index]);
94
+ if (match) return match[1].toUpperCase() === "PASS" ? "pass" : "fail";
95
+ }
96
+ return undefined;
97
+ }
98
+
99
+ export const RESULT_LINE_MAX = 200;
100
+
101
+ export interface TruncatedOutput {
102
+ text: string;
103
+ truncated: boolean;
104
+ }
105
+
106
+ export function truncateResultOutput(output: string, maxLines: number): TruncatedOutput {
107
+ const lines = output.split("\n");
108
+ if (lines.length <= maxLines && lines.every((line) => line.length <= RESULT_LINE_MAX)) {
109
+ return { text: output, truncated: false };
110
+ }
111
+ const kept = lines.slice(0, maxLines).map((line) =>
112
+ line.length > RESULT_LINE_MAX ? `${line.slice(0, RESULT_LINE_MAX)}…` : line,
113
+ );
114
+ return { text: kept.join("\n"), truncated: true };
115
+ }
116
+
117
+ export const RESULT_ARTIFACT_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1_000;
118
+ export const RESULT_ARTIFACT_MAX_FILES_PER_PROJECT = 50;
119
+ // Explicit current prefix plus the strict timestamp/token convention used by 1.1.0.
120
+ const RESULT_ARTIFACT_NAME = /^(?:pi-subagent-\d{13,}-[0-9a-f]{12}|\d{13,}-[a-z0-9]{6})-[\w.-]+\.md$/;
121
+
122
+ /** Name of the single per-extension root under the pi home directory that
123
+ * holds every project-scoped artifact (sessions, worktrees, result excerpts).
124
+ * Nothing long-lived is written to the OS temp directory. */
125
+ export const PROJECT_ROOTS_DIR_NAME = "ferris-pi-subagents";
126
+
127
+ /** Per-project directory that groups every durable artifact of one checkout:
128
+ * `<pi home>/ferris-pi-subagents/<project-slug-hash>/{sessions,worktrees,results}`.
129
+ * Callers join the kind-specific subdirectory themselves. */
130
+ export function getProjectRoot(configPath: string, cwd?: string): string {
131
+ return join(dirname(configPath), PROJECT_ROOTS_DIR_NAME, resultArtifactProjectKey(cwd));
132
+ }
133
+
134
+ interface ResultArtifactRetentionOptions {
135
+ now?: number;
136
+ maxAgeMs?: number;
137
+ maxFilesPerProject?: number;
138
+ }
139
+
140
+ /** Remove only stale/overflow Markdown result artifacts. Unknown files and
141
+ * symlinks are never touched. Called on each artifact write, so storage stays
142
+ * bounded without deleting a result that the current completion just linked. */
143
+ export function pruneResultArtifacts(
144
+ rootDir: string,
145
+ options: ResultArtifactRetentionOptions = {},
146
+ ): void {
147
+ const now = options.now ?? Date.now();
148
+ const maxAgeMs = Math.max(0, options.maxAgeMs ?? RESULT_ARTIFACT_MAX_AGE_MS);
149
+ const maxFiles = Math.max(0, Math.floor(options.maxFilesPerProject ?? RESULT_ARTIFACT_MAX_FILES_PER_PROJECT));
150
+ let projects: Dirent[];
151
+ try {
152
+ projects = readdirSync(rootDir, { withFileTypes: true });
153
+ } catch {
154
+ return;
155
+ }
156
+
157
+ for (const project of projects) {
158
+ if (!project.isDirectory() || project.isSymbolicLink()) continue;
159
+ const projectDir = join(rootDir, project.name);
160
+ let entries: Dirent[];
161
+ try {
162
+ entries = readdirSync(projectDir, { withFileTypes: true });
163
+ } catch {
164
+ continue;
165
+ }
166
+ const artifacts = entries
167
+ .filter((entry) => entry.isFile() && !entry.isSymbolicLink() && RESULT_ARTIFACT_NAME.test(entry.name))
168
+ .flatMap((entry) => {
169
+ const path = join(projectDir, entry.name);
170
+ try {
171
+ return [{ path, mtimeMs: statSync(path).mtimeMs }];
172
+ } catch {
173
+ return [];
174
+ }
175
+ })
176
+ .sort((left, right) => right.mtimeMs - left.mtimeMs);
177
+
178
+ for (const [index, artifact] of artifacts.entries()) {
179
+ if (index < maxFiles && now - artifact.mtimeMs <= maxAgeMs) continue;
180
+ try {
181
+ rmSync(artifact.path, { force: true });
182
+ } catch {
183
+ // Temp cleanup is best-effort; result delivery must still succeed.
184
+ }
185
+ }
186
+ }
187
+ }
188
+
189
+ export function resultArtifactProjectKey(cwd?: string): string {
190
+ if (!cwd) return "default";
191
+ let canonical: string;
192
+ try {
193
+ canonical = realpathSync.native(cwd);
194
+ } catch {
195
+ canonical = resolve(cwd);
196
+ }
197
+ if (process.platform === "win32") canonical = canonical.toLowerCase();
198
+ const slug = basename(canonical).replace(/[^\w.-]+/g, "_") || "project";
199
+ const digest = createHash("sha256").update(canonical).digest("hex").slice(0, 12);
200
+ return `${slug}-${digest}`;
201
+ }
202
+
203
+ export function writeResultArtifact(output: string, agentName: string, resultsRoot: string): string {
204
+ mkdirSync(resultsRoot, { recursive: true });
205
+ const safeName = agentName.replace(/[^\w.-]+/g, "_") || "agent";
206
+ const unique = `pi-subagent-${Date.now()}-${randomUUID().replaceAll("-", "").slice(0, 12)}`;
207
+ const filePath = join(resultsRoot, `${unique}-${safeName}.md`);
208
+ writeFileSync(filePath, output, "utf8");
209
+ pruneResultArtifacts(resultsRoot);
210
+ return filePath;
211
+ }
212
+
213
+ export function isFailedResult(result: SingleResult): boolean {
214
+
215
+ return result.exitCode !== 0 || result.stopReason === "error" || result.stopReason === "aborted";
216
+ }
217
+
218
+ function lastAssistantMessage(messages: Message[]): Extract<Message, { role: "assistant" }> | undefined {
219
+ for (let index = messages.length - 1; index >= 0; index--) {
220
+ const message = messages[index];
221
+ if (message.role === "assistant") return message;
222
+ }
223
+ return undefined;
224
+ }
225
+
226
+ export function isModelLevelFailure(result: SingleResult): boolean {
227
+ if (!isFailedResult(result)) return false;
228
+ if (result.stopReason === "aborted") return false;
229
+ if (result.dispatchFailed) return false;
230
+ if (result.rpcStartupFailed) return false;
231
+ // A negative RPC response proves Pi rejected the prompt before execution. It
232
+ // remains safe to hand off even though dispatch was attempted; local write,
233
+ // close, timeout, and lost-ACK failures never set this explicit flag.
234
+ if (result.rpcPromptRejected) return true;
235
+ // The prompt write completed but its ACK never arrived. With no later activity
236
+ // we cannot know whether Pi started the model or tools, so neither startup
237
+ // retry nor selected→main fallback may replay this objective.
238
+ if (result.rpcPromptDispatched && !result.rpcPromptAccepted && !result.rpcActivity) return false;
239
+ if (isRpcCommandTimeoutError(result.errorMessage)) return false;
240
+ if (result.integrationStatus === "retained") return false;
241
+ if (result.errorMessage?.includes("idle timeout")) return true;
242
+
243
+ // Classification belongs to the final assistant turn, not the whole attempt.
244
+ // Earlier useful text or failed tool calls are retained session history and
245
+ // must not hide a later provider error (for example a second-turn 503).
246
+ const finalAssistant = lastAssistantMessage(result.messages);
247
+ if (finalAssistant) {
248
+ // Provider streams may preserve partial text on a terminal error. The stop
249
+ // reason, not content emptiness, is the transport boundary; ordinary tool or
250
+ // task failures settle with a non-error assistant stop reason.
251
+ return finalAssistant.stopReason === "error";
252
+ }
253
+
254
+ if ((result.failedTools?.length ?? 0) > 0) return false;
255
+ // No accepted prompt, no activity, and no assistant turn means the provider
256
+ // was never reached. Stderr or an exit error here is a startup/transport miss.
257
+ if (!result.rpcPromptAccepted && !result.rpcActivity) return false;
258
+ return Boolean(
259
+ result.rpcPromptAccepted ||
260
+ result.rpcActivity ||
261
+ result.errorMessage?.trim() ||
262
+ result.stderr.trim(),
263
+ );
264
+ }
265
+
266
+ export function isRetryableStartupFailure(result: SingleResult, durationMs: number): boolean {
267
+ if (result.exitCode === 0) return false;
268
+ if (result.stopReason === "aborted") return false;
269
+ if (result.dispatchFailed) return false;
270
+ if (result.rpcPromptDispatched || result.rpcPromptAccepted || result.rpcActivity) return false;
271
+ if (result.errorMessage?.includes("idle timeout")) return false;
272
+ if (getFinalOutput(result.messages)) return false;
273
+ if (result.messages.length > 0) return false;
274
+ const usage = result.usage;
275
+ if (usage.turns || usage.input || usage.output || usage.cacheRead || usage.cacheWrite || usage.cost) return false;
276
+ if (result.rpcStartupFailed) return true;
277
+ if (durationMs > MAX_SUBAGENT_STARTUP_FAILURE_DURATION_MS) return false;
278
+ if (result.stderr.trim().length > 0) return false;
279
+ if (result.errorMessage && result.errorMessage.trim().length > 0) return false;
280
+ return true;
281
+ }
282
+
283
+ export function formatStartupRetryExhaustedError(model: string, attempts: number): string {
284
+ return `Subagent failed to start after ${attempts} attempt${attempts === 1 ? "" : "s"} on ${model}: the child failed before its initial RPC prompt was dispatched and produced no model, tool, output, or usage activity. This is typically a concurrent pi startup race (several sub-agents starting at once). Retry the dispatch, or dispatch fewer sub-agents at once.`;
285
+ }
286
+
287
+ export async function waitForStartupRetry(delayMs: number, signal?: AbortSignal): Promise<boolean> {
288
+ const normalizedDelay = normalizeStartupRetryDelay(delayMs);
289
+ if (normalizedDelay === 0) return !signal?.aborted;
290
+ if (!signal) {
291
+ return new Promise<boolean>((resolve) => {
292
+ const timer = setTimeout(() => resolve(true), normalizedDelay);
293
+ if (typeof timer.unref === "function") timer.unref();
294
+ });
295
+ }
296
+ if (signal.aborted) return false;
297
+ return new Promise<boolean>((resolve) => {
298
+ let settled = false;
299
+ const finish = (shouldRetry: boolean): void => {
300
+ if (settled) return;
301
+ settled = true;
302
+ clearTimeout(timer);
303
+ signal.removeEventListener("abort", onAbort);
304
+ resolve(shouldRetry);
305
+ };
306
+ const onAbort = (): void => finish(false);
307
+ const timer = setTimeout(() => finish(true), normalizedDelay);
308
+ if (typeof timer.unref === "function") timer.unref();
309
+ signal.addEventListener("abort", onAbort, { once: true });
310
+ });
311
+ }
312
+
313
+ async function waitForControlledRetry(
314
+ delayMs: number,
315
+ signal: AbortSignal | undefined,
316
+ control: RpcRunControl | undefined,
317
+ ): Promise<boolean> {
318
+ let remaining = normalizeStartupRetryDelay(delayMs);
319
+ while (remaining > 0) {
320
+ if (control?.isStopRequested()) return false;
321
+ const slice = Math.min(remaining, 50);
322
+ if (!(await waitForStartupRetry(slice, signal))) return false;
323
+ remaining -= slice;
324
+ }
325
+ return !signal?.aborted && !control?.isStopRequested();
326
+ }
327
+
328
+ export function getResultOutput(result: SingleResult): string {
329
+ if (isFailedResult(result)) {
330
+ const error = result.errorMessage || result.stderr;
331
+ const partial = getFinalOutput(result.messages);
332
+ if (error && partial) return `${error}\n\n--- Partial output ---\n${partial}`;
333
+ return error || partial || "(no output)";
334
+ }
335
+ return getFinalOutput(result.messages) || "(no output)";
336
+ }
337
+
338
+ export function buildResumePrompt(task: string, reason: string): string {
339
+ return `You are resuming an earlier sub-agent session after ${reason}. Your earlier work — searches, reads, edits, and reasoning — is preserved in this session's history above; review it before acting. Current objective: ${task}. Pick up exactly where you left off and finish it. Do NOT redo searches, reads, or edits you already completed unless a step clearly failed. Continue now.`;
340
+ }
341
+
342
+ /** Create a fresh private session directory under the given root. */
343
+ export async function createSessionDir(root: string): Promise<string> {
344
+ await mkdir(root, { recursive: true });
345
+ return mkdtemp(join(root, "pi-subagent-session-"));
346
+ }
347
+
348
+ export function buildFallbackResumeReason(fromModel?: string): string {
349
+ return fromModel
350
+ ? `the selected model (${fromModel}) failed at the model/provider level, so the current main model is continuing`
351
+ : "the selected model failed at the model/provider level, so the current main model is continuing";
352
+ }
353
+
354
+ export interface RunSingleOptions {
355
+ defaultCwd: string;
356
+ agent: AgentConfig;
357
+ agentName: string;
358
+ task: string;
359
+ cwd?: string;
360
+ thinkingLevel?: ThinkingLevel;
361
+ /** Resolve the effective level for each runtime model candidate. */
362
+ thinkingLevelForModel?: (modelRef?: string) => ThinkingLevel;
363
+ idleTimeoutMs?: number;
364
+ startupRetryDelaysMs?: readonly number[];
365
+ sessionDir?: string;
366
+ sessionId?: string;
367
+ /** Parent directory for a fresh session directory: the project-scoped
368
+ * sessions root, so retained sessions survive reloads and restarts. */
369
+ sessionRoot: string;
370
+ /** Initial RPC prompt. Kept under the old name to limit caller churn. */
371
+ stdinText?: string;
372
+ /** Refresh parent-derived tools immediately before every startup retry and
373
+ * selected-to-main fallback process is spawned. */
374
+ resolveAgentForAttempt?: (agent: AgentConfig) => AgentConfig;
375
+ signal?: AbortSignal;
376
+ onLive?: (event: SubagentLiveEvent) => void;
377
+ makeDetails: (results: SingleResult[]) => SubagentDetails;
378
+ env?: NodeJS.ProcessEnv;
379
+ /** Stable logical-generation controller shared across retry attempts. */
380
+ control?: RpcRunControl;
381
+ rpcReadyTimeoutMs?: number;
382
+ rpcCommandTimeoutMs?: number;
383
+ }
384
+
385
+ function controlledDisposition(options: RunSingleOptions, base?: SingleResult): SingleResult | undefined {
386
+ const control = options.control;
387
+ if (!control?.isStopRequested()) return undefined;
388
+ const result: SingleResult = base ?? {
389
+ agent: options.agentName,
390
+ task: control.getObjective(),
391
+ exitCode: 0,
392
+ messages: [],
393
+ stderr: "",
394
+ usage: emptyUsage(),
395
+ model: options.agent.model,
396
+ thinking: options.thinkingLevel,
397
+ sessionId: options.sessionId,
398
+ sessionDir: options.sessionDir,
399
+ };
400
+ result.task = control.getObjective();
401
+ result.exitCode = 1;
402
+ result.stopReason = "aborted";
403
+ result.errorMessage = control.getStopMessage();
404
+ return result;
405
+ }
406
+
407
+ function signalAbortDisposition(options: RunSingleOptions, base: SingleResult): SingleResult | undefined {
408
+ if (!options.signal?.aborted) return undefined;
409
+ base.exitCode = 1;
410
+ base.stopReason = "aborted";
411
+ base.errorMessage = "Subagent was aborted";
412
+ return base;
413
+ }
414
+
415
+ /** Spawn one RPC attempt and wait for stable settlement. */
416
+ export async function runSingleAgent(options: RunSingleOptions): Promise<SingleResult> {
417
+ const {
418
+ agent,
419
+ agentName,
420
+ thinkingLevel = SUBAGENT_THINKING_LEVEL,
421
+ idleTimeoutMs = SUBAGENT_DEFAULT_IDLE_TIMEOUT_MS,
422
+ control,
423
+ } = options;
424
+ const disposition = controlledDisposition(options);
425
+ if (disposition) return disposition;
426
+ const objective = control?.getObjective() ?? options.task;
427
+ let prompt = options.stdinText ?? `Task: ${objective}`;
428
+ if (control && objective !== options.task) {
429
+ prompt = options.sessionDir && sessionExists(options.sessionDir, options.sessionId ?? "")
430
+ ? `Abandon the previous objective. New objective: ${objective}`
431
+ : `Task: ${objective}`;
432
+ }
433
+ const result = await runRpcAgentAttempt({
434
+ defaultCwd: options.defaultCwd,
435
+ agent,
436
+ agentName,
437
+ task: objective,
438
+ cwd: options.cwd,
439
+ thinkingLevel,
440
+ idleTimeoutMs,
441
+ sessionDir: options.sessionDir,
442
+ sessionId: options.sessionId,
443
+ prompt,
444
+ signal: options.signal,
445
+ onLive: options.onLive,
446
+ env: options.env,
447
+ control,
448
+ rpcReadyTimeoutMs: options.rpcReadyTimeoutMs,
449
+ rpcCommandTimeoutMs: options.rpcCommandTimeoutMs,
450
+ });
451
+ result.task = control?.getObjective() ?? result.task;
452
+ return result;
453
+ }
454
+
455
+ /**
456
+ * Run one logical generation on the selected model, then hand directly to the
457
+ * current main model after any model/provider-level failure. Startup-race retries
458
+ * remain process-level recovery; provider/model retries and extra candidates do not.
459
+ * Both attempts resume the same retained Pi session.
460
+ */
461
+ export async function runSingleAgentWithMainFallback(
462
+ options: RunSingleOptions,
463
+ mainFallbackRef?: string,
464
+ ): Promise<SingleResult> {
465
+ const agent = options.agent;
466
+ const launchedRef = agent?.model;
467
+ const customStartupDelays = options.startupRetryDelaysMs;
468
+ const startupDelays = customStartupDelays ?? SUBAGENT_STARTUP_RETRY_DELAYS_MS;
469
+
470
+ const sessionId = options.sessionId ?? randomUUID();
471
+ const sessionDir = options.sessionDir ?? (await createSessionDir(options.sessionRoot));
472
+ const baseOptions: RunSingleOptions = { ...options, sessionDir, sessionId };
473
+ if (!options.sessionDir) {
474
+ // Surface the fresh session immediately so the dispatching thread can
475
+ // persist a durable checkpoint before the child settles.
476
+ try {
477
+ options.onLive?.({ kind: "session", sessionId, sessionDir });
478
+ } catch {
479
+ /* never throw from event handling */
480
+ }
481
+ }
482
+
483
+ const dispatchFailure = async (error: unknown): Promise<SingleResult> => {
484
+ const errorMessage = error instanceof Error ? error.message : String(error);
485
+ const hasSession = sessionExists(sessionDir, sessionId);
486
+ if (!hasSession && !options.sessionDir) {
487
+ await rm(sessionDir, { recursive: true, force: true }).catch(() => undefined);
488
+ }
489
+ return {
490
+ agent: options.agentName,
491
+ task: options.control?.getObjective() ?? options.task,
492
+ exitCode: 1,
493
+ messages: [],
494
+ stderr: errorMessage,
495
+ usage: emptyUsage(),
496
+ model: options.agent.model,
497
+ thinking: options.thinkingLevel,
498
+ stopReason: "error",
499
+ errorMessage,
500
+ dispatchFailed: true,
501
+ ...(hasSession || options.sessionDir ? { sessionId, sessionDir } : {}),
502
+ };
503
+ };
504
+
505
+ const runWithStartupRetry = async (opts: RunSingleOptions): Promise<SingleResult> => {
506
+ let lastResult: SingleResult;
507
+ let retries = 0;
508
+ for (let attempt = 0; ; attempt++) {
509
+ const immediate = controlledDisposition(opts);
510
+ if (immediate) return immediate;
511
+ const start = Date.now();
512
+ try {
513
+ const attemptOptions = opts.resolveAgentForAttempt
514
+ ? { ...opts, agent: opts.resolveAgentForAttempt(opts.agent) }
515
+ : opts;
516
+ lastResult = await runSingleAgent(attemptOptions);
517
+ } catch (error) {
518
+ const failed = await dispatchFailure(error);
519
+ return controlledDisposition(opts, failed) ?? failed;
520
+ }
521
+ const durationMs = Date.now() - start;
522
+ const controlled = controlledDisposition(opts, lastResult);
523
+ if (controlled) return controlled;
524
+ if (lastResult.stopReason === "aborted") return lastResult;
525
+ if (!isRetryableStartupFailure(lastResult, durationMs)) {
526
+ if (retries > 0 && !isFailedResult(lastResult)) lastResult.startupRetries = retries;
527
+ return lastResult;
528
+ }
529
+ const delay = startupDelays[attempt];
530
+ if (delay === undefined) {
531
+ lastResult.errorMessage = formatStartupRetryExhaustedError(
532
+ lastResult.model ?? opts.agent.model ?? "default",
533
+ attempt + 1,
534
+ );
535
+ lastResult.stopReason ??= "error";
536
+ lastResult.dispatchFailed = true;
537
+ return lastResult;
538
+ }
539
+ opts.control?.markRetrying();
540
+ try {
541
+ opts.onLive?.({ kind: "status", status: "running" });
542
+ } catch {
543
+ /* never throw from event handling */
544
+ }
545
+ const retryDelay = customStartupDelays ? delay : addStartupRetryJitter(delay);
546
+ if (!(await waitForControlledRetry(retryDelay, opts.signal, opts.control))) {
547
+ return controlledDisposition(opts, lastResult) ?? signalAbortDisposition(opts, lastResult) ?? lastResult;
548
+ }
549
+ retries++;
550
+ }
551
+ };
552
+
553
+ const selectedRef = launchedRef?.trim() || undefined;
554
+ const normalizedMainRef = mainFallbackRef?.trim() || undefined;
555
+ const candidates: Array<{ agent: AgentConfig; ref?: string }> = [
556
+ { agent, ref: selectedRef },
557
+ ];
558
+ if (normalizedMainRef && normalizedMainRef !== selectedRef) {
559
+ candidates.push({ agent: { ...agent, model: normalizedMainRef }, ref: normalizedMainRef });
560
+ }
561
+
562
+ let fallbackUsed = false;
563
+ let result: SingleResult | undefined;
564
+ const priorFailedTools: NonNullable<SingleResult["failedTools"]> = [];
565
+ const priorUsage = emptyUsage();
566
+
567
+ const retainAttemptDiagnostics = (attempt: SingleResult): void => {
568
+ priorFailedTools.push(...(attempt.failedTools ?? []));
569
+ priorUsage.input += attempt.usage.input;
570
+ priorUsage.output += attempt.usage.output;
571
+ priorUsage.cacheRead += attempt.usage.cacheRead;
572
+ priorUsage.cacheWrite += attempt.usage.cacheWrite;
573
+ priorUsage.cost += attempt.usage.cost;
574
+ priorUsage.turns += attempt.usage.turns;
575
+ priorUsage.contextTokens = attempt.usage.contextTokens || priorUsage.contextTokens;
576
+ };
577
+
578
+ const finish = async (settled: SingleResult): Promise<SingleResult> => {
579
+ if (priorFailedTools.length > 0) {
580
+ settled.failedTools = [...priorFailedTools, ...(settled.failedTools ?? [])];
581
+ }
582
+ if (
583
+ priorUsage.turns || priorUsage.input || priorUsage.output || priorUsage.cacheRead ||
584
+ priorUsage.cacheWrite || priorUsage.cost || priorUsage.contextTokens
585
+ ) {
586
+ settled.usage = {
587
+ input: priorUsage.input + settled.usage.input,
588
+ output: priorUsage.output + settled.usage.output,
589
+ cacheRead: priorUsage.cacheRead + settled.usage.cacheRead,
590
+ cacheWrite: priorUsage.cacheWrite + settled.usage.cacheWrite,
591
+ cost: priorUsage.cost + settled.usage.cost,
592
+ turns: priorUsage.turns + settled.usage.turns,
593
+ contextTokens: settled.usage.contextTokens || priorUsage.contextTokens,
594
+ };
595
+ }
596
+ const persistedSession = sessionExists(sessionDir, sessionId);
597
+ if (!settled.dispatchFailed || persistedSession || options.sessionDir) {
598
+ settled.sessionId ??= sessionId;
599
+ settled.sessionDir ??= sessionDir;
600
+ } else {
601
+ await rm(sessionDir, { recursive: true, force: true }).catch(() => undefined);
602
+ settled.sessionId = undefined;
603
+ settled.sessionDir = undefined;
604
+ }
605
+ settled.task = options.control?.getObjective() ?? settled.task;
606
+ if (fallbackUsed && launchedRef) settled.modelFallbackFrom = launchedRef;
607
+ options.control?.markSettled();
608
+ return settled;
609
+ };
610
+
611
+ for (let candidateIndex = 0; candidateIndex < candidates.length; candidateIndex++) {
612
+ const candidate = candidates[candidateIndex];
613
+ fallbackUsed ||= candidateIndex > 0;
614
+ const previousModel = result?.model ?? candidates[candidateIndex - 1]?.ref;
615
+ const candidateThinking = options.thinkingLevelForModel?.(candidate.ref) ?? options.thinkingLevel;
616
+ const candidateOptions: RunSingleOptions = {
617
+ ...baseOptions,
618
+ agent: candidate.agent,
619
+ thinkingLevel: candidateThinking,
620
+ ...(candidateIndex > 0
621
+ ? {
622
+ stdinText: buildResumePrompt(
623
+ options.control?.getObjective() ?? options.task,
624
+ buildFallbackResumeReason(previousModel),
625
+ ),
626
+ }
627
+ : {}),
628
+ };
629
+ try {
630
+ options.onLive?.({
631
+ kind: "model",
632
+ model: candidate.ref,
633
+ thinking: candidateThinking,
634
+ ...(candidateIndex > 0 && launchedRef ? { fallbackFrom: launchedRef } : {}),
635
+ });
636
+ } catch {
637
+ /* never throw from event handling */
638
+ }
639
+
640
+ result = await runWithStartupRetry(candidateOptions);
641
+ if (result.stopReason === "aborted") return finish(result);
642
+ if (!isModelLevelFailure(result)) return finish(result);
643
+ // Any model-level failure advances immediately to the sole fallback (the
644
+ // current main model). Retain selected-attempt tool diagnostics and usage;
645
+ // ordinary task/tool failures returned above without a handoff.
646
+ if (candidateIndex < candidates.length - 1) retainAttemptDiagnostics(result);
647
+ }
648
+
649
+ return finish(result!);
650
+ }