@ferris1225/pi-subagents 4.2.8 → 4.2.13

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,658 +1,663 @@
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
- import { writeTempOwnerMarker } from "./temp-hygiene.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
- export const RESULT_LINE_MAX = 200;
91
-
92
- export interface TruncatedOutput {
93
- text: string;
94
- truncated: boolean;
95
- }
96
-
97
- export function truncateResultOutput(output: string, maxLines: number): TruncatedOutput {
98
- const lines = output.split("\n");
99
- if (lines.length <= maxLines && lines.every((line) => line.length <= RESULT_LINE_MAX)) {
100
- return { text: output, truncated: false };
101
- }
102
- const kept = lines.slice(0, maxLines).map((line) =>
103
- line.length > RESULT_LINE_MAX ? `${line.slice(0, RESULT_LINE_MAX)}…` : line,
104
- );
105
- return { text: kept.join("\n"), truncated: true };
106
- }
107
-
108
- export const RESULT_ARTIFACT_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1_000;
109
- export const RESULT_ARTIFACT_MAX_FILES_PER_PROJECT = 50;
110
- // Explicit current prefix plus the strict timestamp/token convention used by 1.1.0.
111
- const RESULT_ARTIFACT_NAME = /^(?:pi-subagent-\d{13,}-[0-9a-f]{12}|\d{13,}-[a-z0-9]{6})-[\w.-]+\.md$/;
112
-
113
- /** Name of the single per-extension root under the pi home directory that
114
- * holds every project-scoped artifact (sessions, worktrees, result excerpts).
115
- * Nothing long-lived is written to the OS temp directory. */
116
- export const PROJECT_ROOTS_DIR_NAME = "ferris-pi-subagents";
117
-
118
- /** Per-project directory that groups every durable artifact of one checkout:
119
- * `<pi home>/ferris-pi-subagents/<project-slug-hash>/{sessions,worktrees,results}`.
120
- * Callers join the kind-specific subdirectory themselves. */
121
- export function getProjectRoot(configPath: string, cwd?: string): string {
122
- return join(dirname(configPath), PROJECT_ROOTS_DIR_NAME, resultArtifactProjectKey(cwd));
123
- }
124
-
125
- interface ResultArtifactRetentionOptions {
126
- now?: number;
127
- maxAgeMs?: number;
128
- maxFilesPerProject?: number;
129
- }
130
-
131
- /** Remove stale and overflowing Markdown result artifacts from one project's
132
- * results directory, newest kept first. Unknown files and symlinks are never
133
- * touched. Runs on every artifact write, so an active project stays bounded
134
- * without ever deleting the result the current completion just linked. */
135
- export function pruneResultArtifacts(
136
- resultsDir: string,
137
- options: ResultArtifactRetentionOptions = {},
138
- ): void {
139
- const now = options.now ?? Date.now();
140
- const maxAgeMs = Math.max(0, options.maxAgeMs ?? RESULT_ARTIFACT_MAX_AGE_MS);
141
- const maxFiles = Math.max(0, Math.floor(options.maxFilesPerProject ?? RESULT_ARTIFACT_MAX_FILES_PER_PROJECT));
142
- let entries: Dirent[];
143
- try {
144
- entries = readdirSync(resultsDir, { withFileTypes: true });
145
- } catch {
146
- return;
147
- }
148
- const artifacts = entries
149
- .filter((entry) => entry.isFile() && !entry.isSymbolicLink() && RESULT_ARTIFACT_NAME.test(entry.name))
150
- .flatMap((entry) => {
151
- const path = join(resultsDir, entry.name);
152
- try {
153
- return [{ path, mtimeMs: statSync(path).mtimeMs }];
154
- } catch {
155
- return [];
156
- }
157
- })
158
- .sort((left, right) => right.mtimeMs - left.mtimeMs);
159
-
160
- for (const [index, artifact] of artifacts.entries()) {
161
- if (index < maxFiles && now - artifact.mtimeMs <= maxAgeMs) continue;
162
- try {
163
- rmSync(artifact.path, { force: true });
164
- } catch {
165
- // Retention is best-effort; result delivery must still succeed.
166
- }
167
- }
168
- }
169
-
170
- /** Apply result retention to every project under the durable root. The write
171
- * path only bounds the project being written to, so this is what ages out the
172
- * results of projects that are no longer producing any. */
173
- export function sweepProjectResultArtifacts(
174
- durableRoot: string,
175
- options: ResultArtifactRetentionOptions = {},
176
- ): void {
177
- let projects: Dirent[];
178
- try {
179
- projects = readdirSync(durableRoot, { withFileTypes: true });
180
- } catch {
181
- return;
182
- }
183
- for (const project of projects) {
184
- if (!project.isDirectory() || project.isSymbolicLink()) continue;
185
- pruneResultArtifacts(join(durableRoot, project.name, "results"), options);
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. The owner
343
- * marker is what lets a later load tell a session this process still owns from
344
- * one a crash abandoned. */
345
- export async function createSessionDir(root: string): Promise<string> {
346
- await mkdir(root, { recursive: true });
347
- const dir = await mkdtemp(join(root, "pi-subagent-session-"));
348
- writeTempOwnerMarker(dir);
349
- return dir;
350
- }
351
-
352
- export function buildFallbackResumeReason(fromModel?: string): string {
353
- return fromModel
354
- ? `the selected model (${fromModel}) failed at the model/provider level, so the current main model is continuing`
355
- : "the selected model failed at the model/provider level, so the current main model is continuing";
356
- }
357
-
358
- export interface RunSingleOptions {
359
- defaultCwd: string;
360
- agent: AgentConfig;
361
- agentName: string;
362
- task: string;
363
- cwd?: string;
364
- thinkingLevel?: ThinkingLevel;
365
- /** Resolve the effective level for each runtime model candidate. */
366
- thinkingLevelForModel?: (modelRef?: string) => ThinkingLevel;
367
- idleTimeoutMs?: number;
368
- startupRetryDelaysMs?: readonly number[];
369
- sessionDir?: string;
370
- sessionId?: string;
371
- /** Parent directory for a fresh session directory: the project-scoped
372
- * sessions root, so retained sessions survive reloads and restarts. */
373
- sessionRoot: string;
374
- /** Parent directory for per-attempt transient files (child prompt, retry
375
- * policy): the project-scoped tmp root, so nothing lands in the OS temp. */
376
- scratchRoot: string;
377
- /** Initial RPC prompt. Kept under the old name to limit caller churn. */
378
- stdinText?: string;
379
- /** Refresh parent-derived tools immediately before every startup retry and
380
- * selected-to-main fallback process is spawned. */
381
- resolveAgentForAttempt?: (agent: AgentConfig) => AgentConfig;
382
- signal?: AbortSignal;
383
- onLive?: (event: SubagentLiveEvent) => void;
384
- makeDetails: (results: SingleResult[]) => SubagentDetails;
385
- env?: NodeJS.ProcessEnv;
386
- /** Stable logical-generation controller shared across retry attempts. */
387
- control?: RpcRunControl;
388
- rpcReadyTimeoutMs?: number;
389
- rpcCommandTimeoutMs?: number;
390
- }
391
-
392
- function controlledDisposition(options: RunSingleOptions, base?: SingleResult): SingleResult | undefined {
393
- const control = options.control;
394
- if (!control?.isStopRequested()) return undefined;
395
- const result: SingleResult = base ?? {
396
- agent: options.agentName,
397
- task: control.getObjective(),
398
- exitCode: 0,
399
- messages: [],
400
- stderr: "",
401
- usage: emptyUsage(),
402
- model: options.agent.model,
403
- thinking: options.thinkingLevel,
404
- sessionId: options.sessionId,
405
- sessionDir: options.sessionDir,
406
- };
407
- result.task = control.getObjective();
408
- result.exitCode = 1;
409
- result.stopReason = "aborted";
410
- result.errorMessage = control.getStopMessage();
411
- return result;
412
- }
413
-
414
- function signalAbortDisposition(options: RunSingleOptions, base: SingleResult): SingleResult | undefined {
415
- if (!options.signal?.aborted) return undefined;
416
- base.exitCode = 1;
417
- base.stopReason = "aborted";
418
- base.errorMessage = "Subagent was aborted";
419
- return base;
420
- }
421
-
422
- /** Spawn one RPC attempt and wait for stable settlement. */
423
- export async function runSingleAgent(options: RunSingleOptions): Promise<SingleResult> {
424
- const {
425
- agent,
426
- agentName,
427
- thinkingLevel = SUBAGENT_THINKING_LEVEL,
428
- idleTimeoutMs = SUBAGENT_DEFAULT_IDLE_TIMEOUT_MS,
429
- control,
430
- } = options;
431
- const disposition = controlledDisposition(options);
432
- if (disposition) return disposition;
433
- const objective = control?.getObjective() ?? options.task;
434
- let prompt = options.stdinText ?? `Task: ${objective}`;
435
- if (control && objective !== options.task) {
436
- prompt = options.sessionDir && sessionExists(options.sessionDir, options.sessionId ?? "")
437
- ? `Abandon the previous objective. New objective: ${objective}`
438
- : `Task: ${objective}`;
439
- }
440
- const result = await runRpcAgentAttempt({
441
- defaultCwd: options.defaultCwd,
442
- agent,
443
- agentName,
444
- task: objective,
445
- cwd: options.cwd,
446
- thinkingLevel,
447
- idleTimeoutMs,
448
- sessionDir: options.sessionDir,
449
- sessionId: options.sessionId,
450
- scratchRoot: options.scratchRoot,
451
- prompt,
452
- signal: options.signal,
453
- onLive: options.onLive,
454
- env: options.env,
455
- control,
456
- rpcReadyTimeoutMs: options.rpcReadyTimeoutMs,
457
- rpcCommandTimeoutMs: options.rpcCommandTimeoutMs,
458
- });
459
- result.task = control?.getObjective() ?? result.task;
460
- return result;
461
- }
462
-
463
- /**
464
- * Run one logical generation on the selected model, then hand directly to the
465
- * current main model after any model/provider-level failure. Startup-race retries
466
- * remain process-level recovery; provider/model retries and extra candidates do not.
467
- * Both attempts resume the same retained Pi session.
468
- */
469
- export async function runSingleAgentWithMainFallback(
470
- options: RunSingleOptions,
471
- mainFallbackRef?: string,
472
- ): Promise<SingleResult> {
473
- const agent = options.agent;
474
- const launchedRef = agent?.model;
475
- const customStartupDelays = options.startupRetryDelaysMs;
476
- const startupDelays = customStartupDelays ?? SUBAGENT_STARTUP_RETRY_DELAYS_MS;
477
-
478
- const sessionId = options.sessionId ?? randomUUID();
479
- const sessionDir = options.sessionDir ?? (await createSessionDir(options.sessionRoot));
480
- const baseOptions: RunSingleOptions = { ...options, sessionDir, sessionId };
481
- if (!options.sessionDir) {
482
- // Surface the fresh session immediately so the dispatching thread can
483
- // persist a durable checkpoint before the child settles.
484
- try {
485
- options.onLive?.({ kind: "session", sessionId, sessionDir });
486
- } catch {
487
- /* never throw from event handling */
488
- }
489
- }
490
-
491
- const dispatchFailure = async (error: unknown): Promise<SingleResult> => {
492
- const errorMessage = error instanceof Error ? error.message : String(error);
493
- const hasSession = sessionExists(sessionDir, sessionId);
494
- if (!hasSession && !options.sessionDir) {
495
- await rm(sessionDir, { recursive: true, force: true }).catch(() => undefined);
496
- }
497
- return {
498
- agent: options.agentName,
499
- task: options.control?.getObjective() ?? options.task,
500
- exitCode: 1,
501
- messages: [],
502
- stderr: errorMessage,
503
- usage: emptyUsage(),
504
- model: options.agent.model,
505
- thinking: options.thinkingLevel,
506
- stopReason: "error",
507
- errorMessage,
508
- dispatchFailed: true,
509
- ...(hasSession || options.sessionDir ? { sessionId, sessionDir } : {}),
510
- };
511
- };
512
-
513
- const runWithStartupRetry = async (opts: RunSingleOptions): Promise<SingleResult> => {
514
- let lastResult: SingleResult;
515
- let retries = 0;
516
- for (let attempt = 0; ; attempt++) {
517
- const immediate = controlledDisposition(opts);
518
- if (immediate) return immediate;
519
- const start = Date.now();
520
- try {
521
- const attemptOptions = opts.resolveAgentForAttempt
522
- ? { ...opts, agent: opts.resolveAgentForAttempt(opts.agent) }
523
- : opts;
524
- lastResult = await runSingleAgent(attemptOptions);
525
- } catch (error) {
526
- const failed = await dispatchFailure(error);
527
- return controlledDisposition(opts, failed) ?? failed;
528
- }
529
- const durationMs = Date.now() - start;
530
- const controlled = controlledDisposition(opts, lastResult);
531
- if (controlled) return controlled;
532
- if (lastResult.stopReason === "aborted") return lastResult;
533
- if (!isRetryableStartupFailure(lastResult, durationMs)) {
534
- if (retries > 0 && !isFailedResult(lastResult)) lastResult.startupRetries = retries;
535
- return lastResult;
536
- }
537
- const delay = startupDelays[attempt];
538
- if (delay === undefined) {
539
- lastResult.errorMessage = formatStartupRetryExhaustedError(
540
- lastResult.model ?? opts.agent.model ?? "default",
541
- attempt + 1,
542
- );
543
- lastResult.stopReason ??= "error";
544
- lastResult.dispatchFailed = true;
545
- return lastResult;
546
- }
547
- opts.control?.markRetrying();
548
- try {
549
- opts.onLive?.({ kind: "status", status: "running" });
550
- } catch {
551
- /* never throw from event handling */
552
- }
553
- const retryDelay = customStartupDelays ? delay : addStartupRetryJitter(delay);
554
- if (!(await waitForControlledRetry(retryDelay, opts.signal, opts.control))) {
555
- return controlledDisposition(opts, lastResult) ?? signalAbortDisposition(opts, lastResult) ?? lastResult;
556
- }
557
- retries++;
558
- }
559
- };
560
-
561
- const selectedRef = launchedRef?.trim() || undefined;
562
- const normalizedMainRef = mainFallbackRef?.trim() || undefined;
563
- const candidates: Array<{ agent: AgentConfig; ref?: string }> = [
564
- { agent, ref: selectedRef },
565
- ];
566
- if (normalizedMainRef && normalizedMainRef !== selectedRef) {
567
- candidates.push({ agent: { ...agent, model: normalizedMainRef }, ref: normalizedMainRef });
568
- }
569
-
570
- let fallbackUsed = false;
571
- let result: SingleResult | undefined;
572
- const priorFailedTools: NonNullable<SingleResult["failedTools"]> = [];
573
- const priorUsage = emptyUsage();
574
-
575
- const retainAttemptDiagnostics = (attempt: SingleResult): void => {
576
- priorFailedTools.push(...(attempt.failedTools ?? []));
577
- priorUsage.input += attempt.usage.input;
578
- priorUsage.output += attempt.usage.output;
579
- priorUsage.cacheRead += attempt.usage.cacheRead;
580
- priorUsage.cacheWrite += attempt.usage.cacheWrite;
581
- priorUsage.cost += attempt.usage.cost;
582
- priorUsage.turns += attempt.usage.turns;
583
- priorUsage.contextTokens = attempt.usage.contextTokens || priorUsage.contextTokens;
584
- };
585
-
586
- const finish = async (settled: SingleResult): Promise<SingleResult> => {
587
- if (priorFailedTools.length > 0) {
588
- settled.failedTools = [...priorFailedTools, ...(settled.failedTools ?? [])];
589
- }
590
- if (
591
- priorUsage.turns || priorUsage.input || priorUsage.output || priorUsage.cacheRead ||
592
- priorUsage.cacheWrite || priorUsage.cost || priorUsage.contextTokens
593
- ) {
594
- settled.usage = {
595
- input: priorUsage.input + settled.usage.input,
596
- output: priorUsage.output + settled.usage.output,
597
- cacheRead: priorUsage.cacheRead + settled.usage.cacheRead,
598
- cacheWrite: priorUsage.cacheWrite + settled.usage.cacheWrite,
599
- cost: priorUsage.cost + settled.usage.cost,
600
- turns: priorUsage.turns + settled.usage.turns,
601
- contextTokens: settled.usage.contextTokens || priorUsage.contextTokens,
602
- };
603
- }
604
- const persistedSession = sessionExists(sessionDir, sessionId);
605
- if (!settled.dispatchFailed || persistedSession || options.sessionDir) {
606
- settled.sessionId ??= sessionId;
607
- settled.sessionDir ??= sessionDir;
608
- } else {
609
- await rm(sessionDir, { recursive: true, force: true }).catch(() => undefined);
610
- settled.sessionId = undefined;
611
- settled.sessionDir = undefined;
612
- }
613
- settled.task = options.control?.getObjective() ?? settled.task;
614
- if (fallbackUsed && launchedRef) settled.modelFallbackFrom = launchedRef;
615
- options.control?.markSettled();
616
- return settled;
617
- };
618
-
619
- for (let candidateIndex = 0; candidateIndex < candidates.length; candidateIndex++) {
620
- const candidate = candidates[candidateIndex];
621
- fallbackUsed ||= candidateIndex > 0;
622
- const previousModel = result?.model ?? candidates[candidateIndex - 1]?.ref;
623
- const candidateThinking = options.thinkingLevelForModel?.(candidate.ref) ?? options.thinkingLevel;
624
- const candidateOptions: RunSingleOptions = {
625
- ...baseOptions,
626
- agent: candidate.agent,
627
- thinkingLevel: candidateThinking,
628
- ...(candidateIndex > 0
629
- ? {
630
- stdinText: buildResumePrompt(
631
- options.control?.getObjective() ?? options.task,
632
- buildFallbackResumeReason(previousModel),
633
- ),
634
- }
635
- : {}),
636
- };
637
- try {
638
- options.onLive?.({
639
- kind: "model",
640
- model: candidate.ref,
641
- thinking: candidateThinking,
642
- ...(candidateIndex > 0 && launchedRef ? { fallbackFrom: launchedRef } : {}),
643
- });
644
- } catch {
645
- /* never throw from event handling */
646
- }
647
-
648
- result = await runWithStartupRetry(candidateOptions);
649
- if (result.stopReason === "aborted") return finish(result);
650
- if (!isModelLevelFailure(result)) return finish(result);
651
- // Any model-level failure advances immediately to the sole fallback (the
652
- // current main model). Retain selected-attempt tool diagnostics and usage;
653
- // ordinary task/tool failures returned above without a handoff.
654
- if (candidateIndex < candidates.length - 1) retainAttemptDiagnostics(result);
655
- }
656
-
657
- return finish(result!);
658
- }
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
+ import { writeTempOwnerMarker } from "./temp-hygiene.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
+ export const RESULT_LINE_MAX = 200;
91
+
92
+ export interface TruncatedOutput {
93
+ text: string;
94
+ truncated: boolean;
95
+ shownLines: number;
96
+ totalLines: number;
97
+ widthClipped: boolean;
98
+ }
99
+
100
+ export function truncateResultOutput(output: string, maxLines: number): TruncatedOutput {
101
+ const lines = output.split("\n");
102
+ const totalLines = lines.length;
103
+ const widthClipped = lines.some((line) => line.length > RESULT_LINE_MAX);
104
+ if (totalLines <= maxLines && !widthClipped) {
105
+ return { text: output, truncated: false, shownLines: totalLines, totalLines, widthClipped: false };
106
+ }
107
+ const kept = lines.slice(0, maxLines).map((line) =>
108
+ line.length > RESULT_LINE_MAX ? `${line.slice(0, RESULT_LINE_MAX)}…` : line,
109
+ );
110
+ return { text: kept.join("\n"), truncated: true, shownLines: kept.length, totalLines, widthClipped };
111
+ }
112
+
113
+ export const RESULT_ARTIFACT_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1_000;
114
+ export const RESULT_ARTIFACT_MAX_FILES_PER_PROJECT = 50;
115
+ // Explicit current prefix plus the strict timestamp/token convention used by 1.1.0.
116
+ const RESULT_ARTIFACT_NAME = /^(?:pi-subagent-\d{13,}-[0-9a-f]{12}|\d{13,}-[a-z0-9]{6})-[\w.-]+\.md$/;
117
+
118
+ /** Name of the single per-extension root under the pi home directory that
119
+ * holds every project-scoped artifact (sessions, worktrees, result excerpts).
120
+ * Nothing long-lived is written to the OS temp directory. */
121
+ export const PROJECT_ROOTS_DIR_NAME = "ferris-pi-subagents";
122
+
123
+ /** Per-project directory that groups every durable artifact of one checkout:
124
+ * `<pi home>/ferris-pi-subagents/<project-slug-hash>/{sessions,worktrees,results}`.
125
+ * Callers join the kind-specific subdirectory themselves. */
126
+ export function getProjectRoot(configPath: string, cwd?: string): string {
127
+ return join(dirname(configPath), PROJECT_ROOTS_DIR_NAME, resultArtifactProjectKey(cwd));
128
+ }
129
+
130
+ interface ResultArtifactRetentionOptions {
131
+ now?: number;
132
+ maxAgeMs?: number;
133
+ maxFilesPerProject?: number;
134
+ }
135
+
136
+ /** Remove stale and overflowing Markdown result artifacts from one project's
137
+ * results directory, newest kept first. Unknown files and symlinks are never
138
+ * touched. Runs on every artifact write, so an active project stays bounded
139
+ * without ever deleting the result the current completion just linked. */
140
+ export function pruneResultArtifacts(
141
+ resultsDir: string,
142
+ options: ResultArtifactRetentionOptions = {},
143
+ ): void {
144
+ const now = options.now ?? Date.now();
145
+ const maxAgeMs = Math.max(0, options.maxAgeMs ?? RESULT_ARTIFACT_MAX_AGE_MS);
146
+ const maxFiles = Math.max(0, Math.floor(options.maxFilesPerProject ?? RESULT_ARTIFACT_MAX_FILES_PER_PROJECT));
147
+ let entries: Dirent[];
148
+ try {
149
+ entries = readdirSync(resultsDir, { withFileTypes: true });
150
+ } catch {
151
+ return;
152
+ }
153
+ const artifacts = entries
154
+ .filter((entry) => entry.isFile() && !entry.isSymbolicLink() && RESULT_ARTIFACT_NAME.test(entry.name))
155
+ .flatMap((entry) => {
156
+ const path = join(resultsDir, entry.name);
157
+ try {
158
+ return [{ path, mtimeMs: statSync(path).mtimeMs }];
159
+ } catch {
160
+ return [];
161
+ }
162
+ })
163
+ .sort((left, right) => right.mtimeMs - left.mtimeMs);
164
+
165
+ for (const [index, artifact] of artifacts.entries()) {
166
+ if (index < maxFiles && now - artifact.mtimeMs <= maxAgeMs) continue;
167
+ try {
168
+ rmSync(artifact.path, { force: true });
169
+ } catch {
170
+ // Retention is best-effort; result delivery must still succeed.
171
+ }
172
+ }
173
+ }
174
+
175
+ /** Apply result retention to every project under the durable root. The write
176
+ * path only bounds the project being written to, so this is what ages out the
177
+ * results of projects that are no longer producing any. */
178
+ export function sweepProjectResultArtifacts(
179
+ durableRoot: string,
180
+ options: ResultArtifactRetentionOptions = {},
181
+ ): void {
182
+ let projects: Dirent[];
183
+ try {
184
+ projects = readdirSync(durableRoot, { withFileTypes: true });
185
+ } catch {
186
+ return;
187
+ }
188
+ for (const project of projects) {
189
+ if (!project.isDirectory() || project.isSymbolicLink()) continue;
190
+ pruneResultArtifacts(join(durableRoot, project.name, "results"), options);
191
+ }
192
+ }
193
+
194
+ export function resultArtifactProjectKey(cwd?: string): string {
195
+ if (!cwd) return "default";
196
+ let canonical: string;
197
+ try {
198
+ canonical = realpathSync.native(cwd);
199
+ } catch {
200
+ canonical = resolve(cwd);
201
+ }
202
+ if (process.platform === "win32") canonical = canonical.toLowerCase();
203
+ const slug = basename(canonical).replace(/[^\w.-]+/g, "_") || "project";
204
+ const digest = createHash("sha256").update(canonical).digest("hex").slice(0, 12);
205
+ return `${slug}-${digest}`;
206
+ }
207
+
208
+ export function writeResultArtifact(output: string, agentName: string, resultsRoot: string): string {
209
+ mkdirSync(resultsRoot, { recursive: true });
210
+ const safeName = agentName.replace(/[^\w.-]+/g, "_") || "agent";
211
+ const unique = `pi-subagent-${Date.now()}-${randomUUID().replaceAll("-", "").slice(0, 12)}`;
212
+ const filePath = join(resultsRoot, `${unique}-${safeName}.md`);
213
+ writeFileSync(filePath, output, "utf8");
214
+ pruneResultArtifacts(resultsRoot);
215
+ return filePath;
216
+ }
217
+
218
+ export function isFailedResult(result: SingleResult): boolean {
219
+
220
+ return result.exitCode !== 0 || result.stopReason === "error" || result.stopReason === "aborted";
221
+ }
222
+
223
+ function lastAssistantMessage(messages: Message[]): Extract<Message, { role: "assistant" }> | undefined {
224
+ for (let index = messages.length - 1; index >= 0; index--) {
225
+ const message = messages[index];
226
+ if (message.role === "assistant") return message;
227
+ }
228
+ return undefined;
229
+ }
230
+
231
+ export function isModelLevelFailure(result: SingleResult): boolean {
232
+ if (!isFailedResult(result)) return false;
233
+ if (result.stopReason === "aborted") return false;
234
+ if (result.dispatchFailed) return false;
235
+ if (result.rpcStartupFailed) return false;
236
+ // A negative RPC response proves Pi rejected the prompt before execution. It
237
+ // remains safe to hand off even though dispatch was attempted; local write,
238
+ // close, timeout, and lost-ACK failures never set this explicit flag.
239
+ if (result.rpcPromptRejected) return true;
240
+ // The prompt write completed but its ACK never arrived. With no later activity
241
+ // we cannot know whether Pi started the model or tools, so neither startup
242
+ // retry nor selected→main fallback may replay this objective.
243
+ if (result.rpcPromptDispatched && !result.rpcPromptAccepted && !result.rpcActivity) return false;
244
+ if (isRpcCommandTimeoutError(result.errorMessage)) return false;
245
+ if (result.integrationStatus === "retained") return false;
246
+ if (result.errorMessage?.includes("idle timeout")) return true;
247
+
248
+ // Classification belongs to the final assistant turn, not the whole attempt.
249
+ // Earlier useful text or failed tool calls are retained session history and
250
+ // must not hide a later provider error (for example a second-turn 503).
251
+ const finalAssistant = lastAssistantMessage(result.messages);
252
+ if (finalAssistant) {
253
+ // Provider streams may preserve partial text on a terminal error. The stop
254
+ // reason, not content emptiness, is the transport boundary; ordinary tool or
255
+ // task failures settle with a non-error assistant stop reason.
256
+ return finalAssistant.stopReason === "error";
257
+ }
258
+
259
+ if ((result.failedTools?.length ?? 0) > 0) return false;
260
+ // No accepted prompt, no activity, and no assistant turn means the provider
261
+ // was never reached. Stderr or an exit error here is a startup/transport miss.
262
+ if (!result.rpcPromptAccepted && !result.rpcActivity) return false;
263
+ return Boolean(
264
+ result.rpcPromptAccepted ||
265
+ result.rpcActivity ||
266
+ result.errorMessage?.trim() ||
267
+ result.stderr.trim(),
268
+ );
269
+ }
270
+
271
+ export function isRetryableStartupFailure(result: SingleResult, durationMs: number): boolean {
272
+ if (result.exitCode === 0) return false;
273
+ if (result.stopReason === "aborted") return false;
274
+ if (result.dispatchFailed) return false;
275
+ if (result.rpcPromptDispatched || result.rpcPromptAccepted || result.rpcActivity) return false;
276
+ if (result.errorMessage?.includes("idle timeout")) return false;
277
+ if (getFinalOutput(result.messages)) return false;
278
+ if (result.messages.length > 0) return false;
279
+ const usage = result.usage;
280
+ if (usage.turns || usage.input || usage.output || usage.cacheRead || usage.cacheWrite || usage.cost) return false;
281
+ if (result.rpcStartupFailed) return true;
282
+ if (durationMs > MAX_SUBAGENT_STARTUP_FAILURE_DURATION_MS) return false;
283
+ if (result.stderr.trim().length > 0) return false;
284
+ if (result.errorMessage && result.errorMessage.trim().length > 0) return false;
285
+ return true;
286
+ }
287
+
288
+ export function formatStartupRetryExhaustedError(model: string, attempts: number): string {
289
+ 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.`;
290
+ }
291
+
292
+ export async function waitForStartupRetry(delayMs: number, signal?: AbortSignal): Promise<boolean> {
293
+ const normalizedDelay = normalizeStartupRetryDelay(delayMs);
294
+ if (normalizedDelay === 0) return !signal?.aborted;
295
+ if (!signal) {
296
+ return new Promise<boolean>((resolve) => {
297
+ const timer = setTimeout(() => resolve(true), normalizedDelay);
298
+ if (typeof timer.unref === "function") timer.unref();
299
+ });
300
+ }
301
+ if (signal.aborted) return false;
302
+ return new Promise<boolean>((resolve) => {
303
+ let settled = false;
304
+ const finish = (shouldRetry: boolean): void => {
305
+ if (settled) return;
306
+ settled = true;
307
+ clearTimeout(timer);
308
+ signal.removeEventListener("abort", onAbort);
309
+ resolve(shouldRetry);
310
+ };
311
+ const onAbort = (): void => finish(false);
312
+ const timer = setTimeout(() => finish(true), normalizedDelay);
313
+ if (typeof timer.unref === "function") timer.unref();
314
+ signal.addEventListener("abort", onAbort, { once: true });
315
+ });
316
+ }
317
+
318
+ async function waitForControlledRetry(
319
+ delayMs: number,
320
+ signal: AbortSignal | undefined,
321
+ control: RpcRunControl | undefined,
322
+ ): Promise<boolean> {
323
+ let remaining = normalizeStartupRetryDelay(delayMs);
324
+ while (remaining > 0) {
325
+ if (control?.isStopRequested()) return false;
326
+ const slice = Math.min(remaining, 50);
327
+ if (!(await waitForStartupRetry(slice, signal))) return false;
328
+ remaining -= slice;
329
+ }
330
+ return !signal?.aborted && !control?.isStopRequested();
331
+ }
332
+
333
+ export function getResultOutput(result: SingleResult): string {
334
+ if (isFailedResult(result)) {
335
+ const error = result.errorMessage || result.stderr;
336
+ const partial = getFinalOutput(result.messages);
337
+ if (error && partial) return `${error}\n\n--- Partial output ---\n${partial}`;
338
+ return error || partial || "(no output)";
339
+ }
340
+ return getFinalOutput(result.messages) || "(no output)";
341
+ }
342
+
343
+ export function buildResumePrompt(task: string, reason: string): string {
344
+ 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.`;
345
+ }
346
+
347
+ /** Create a fresh private session directory under the given root. The owner
348
+ * marker is what lets a later load tell a session this process still owns from
349
+ * one a crash abandoned. */
350
+ export async function createSessionDir(root: string): Promise<string> {
351
+ await mkdir(root, { recursive: true });
352
+ const dir = await mkdtemp(join(root, "pi-subagent-session-"));
353
+ writeTempOwnerMarker(dir);
354
+ return dir;
355
+ }
356
+
357
+ export function buildFallbackResumeReason(fromModel?: string): string {
358
+ return fromModel
359
+ ? `the selected model (${fromModel}) failed at the model/provider level, so the current main model is continuing`
360
+ : "the selected model failed at the model/provider level, so the current main model is continuing";
361
+ }
362
+
363
+ export interface RunSingleOptions {
364
+ defaultCwd: string;
365
+ agent: AgentConfig;
366
+ agentName: string;
367
+ task: string;
368
+ cwd?: string;
369
+ thinkingLevel?: ThinkingLevel;
370
+ /** Resolve the effective level for each runtime model candidate. */
371
+ thinkingLevelForModel?: (modelRef?: string) => ThinkingLevel;
372
+ idleTimeoutMs?: number;
373
+ startupRetryDelaysMs?: readonly number[];
374
+ sessionDir?: string;
375
+ sessionId?: string;
376
+ /** Parent directory for a fresh session directory: the project-scoped
377
+ * sessions root, so retained sessions survive reloads and restarts. */
378
+ sessionRoot: string;
379
+ /** Parent directory for per-attempt transient files (child prompt, retry
380
+ * policy): the project-scoped tmp root, so nothing lands in the OS temp. */
381
+ scratchRoot: string;
382
+ /** Initial RPC prompt. Kept under the old name to limit caller churn. */
383
+ stdinText?: string;
384
+ /** Refresh parent-derived tools immediately before every startup retry and
385
+ * selected-to-main fallback process is spawned. */
386
+ resolveAgentForAttempt?: (agent: AgentConfig) => AgentConfig;
387
+ signal?: AbortSignal;
388
+ onLive?: (event: SubagentLiveEvent) => void;
389
+ makeDetails: (results: SingleResult[]) => SubagentDetails;
390
+ env?: NodeJS.ProcessEnv;
391
+ /** Stable logical-generation controller shared across retry attempts. */
392
+ control?: RpcRunControl;
393
+ rpcReadyTimeoutMs?: number;
394
+ rpcCommandTimeoutMs?: number;
395
+ }
396
+
397
+ function controlledDisposition(options: RunSingleOptions, base?: SingleResult): SingleResult | undefined {
398
+ const control = options.control;
399
+ if (!control?.isStopRequested()) return undefined;
400
+ const result: SingleResult = base ?? {
401
+ agent: options.agentName,
402
+ task: control.getObjective(),
403
+ exitCode: 0,
404
+ messages: [],
405
+ stderr: "",
406
+ usage: emptyUsage(),
407
+ model: options.agent.model,
408
+ thinking: options.thinkingLevel,
409
+ sessionId: options.sessionId,
410
+ sessionDir: options.sessionDir,
411
+ };
412
+ result.task = control.getObjective();
413
+ result.exitCode = 1;
414
+ result.stopReason = "aborted";
415
+ result.errorMessage = control.getStopMessage();
416
+ return result;
417
+ }
418
+
419
+ function signalAbortDisposition(options: RunSingleOptions, base: SingleResult): SingleResult | undefined {
420
+ if (!options.signal?.aborted) return undefined;
421
+ base.exitCode = 1;
422
+ base.stopReason = "aborted";
423
+ base.errorMessage = "Subagent was aborted";
424
+ return base;
425
+ }
426
+
427
+ /** Spawn one RPC attempt and wait for stable settlement. */
428
+ export async function runSingleAgent(options: RunSingleOptions): Promise<SingleResult> {
429
+ const {
430
+ agent,
431
+ agentName,
432
+ thinkingLevel = SUBAGENT_THINKING_LEVEL,
433
+ idleTimeoutMs = SUBAGENT_DEFAULT_IDLE_TIMEOUT_MS,
434
+ control,
435
+ } = options;
436
+ const disposition = controlledDisposition(options);
437
+ if (disposition) return disposition;
438
+ const objective = control?.getObjective() ?? options.task;
439
+ let prompt = options.stdinText ?? `Task: ${objective}`;
440
+ if (control && objective !== options.task) {
441
+ prompt = options.sessionDir && sessionExists(options.sessionDir, options.sessionId ?? "")
442
+ ? `Abandon the previous objective. New objective: ${objective}`
443
+ : `Task: ${objective}`;
444
+ }
445
+ const result = await runRpcAgentAttempt({
446
+ defaultCwd: options.defaultCwd,
447
+ agent,
448
+ agentName,
449
+ task: objective,
450
+ cwd: options.cwd,
451
+ thinkingLevel,
452
+ idleTimeoutMs,
453
+ sessionDir: options.sessionDir,
454
+ sessionId: options.sessionId,
455
+ scratchRoot: options.scratchRoot,
456
+ prompt,
457
+ signal: options.signal,
458
+ onLive: options.onLive,
459
+ env: options.env,
460
+ control,
461
+ rpcReadyTimeoutMs: options.rpcReadyTimeoutMs,
462
+ rpcCommandTimeoutMs: options.rpcCommandTimeoutMs,
463
+ });
464
+ result.task = control?.getObjective() ?? result.task;
465
+ return result;
466
+ }
467
+
468
+ /**
469
+ * Run one logical generation on the selected model, then hand directly to the
470
+ * current main model after any model/provider-level failure. Startup-race retries
471
+ * remain process-level recovery; provider/model retries and extra candidates do not.
472
+ * Both attempts resume the same retained Pi session.
473
+ */
474
+ export async function runSingleAgentWithMainFallback(
475
+ options: RunSingleOptions,
476
+ mainFallbackRef?: string,
477
+ ): Promise<SingleResult> {
478
+ const agent = options.agent;
479
+ const launchedRef = agent?.model;
480
+ const customStartupDelays = options.startupRetryDelaysMs;
481
+ const startupDelays = customStartupDelays ?? SUBAGENT_STARTUP_RETRY_DELAYS_MS;
482
+
483
+ const sessionId = options.sessionId ?? randomUUID();
484
+ const sessionDir = options.sessionDir ?? (await createSessionDir(options.sessionRoot));
485
+ const baseOptions: RunSingleOptions = { ...options, sessionDir, sessionId };
486
+ if (!options.sessionDir) {
487
+ // Surface the fresh session immediately so the dispatching thread can
488
+ // persist a durable checkpoint before the child settles.
489
+ try {
490
+ options.onLive?.({ kind: "session", sessionId, sessionDir });
491
+ } catch {
492
+ /* never throw from event handling */
493
+ }
494
+ }
495
+
496
+ const dispatchFailure = async (error: unknown): Promise<SingleResult> => {
497
+ const errorMessage = error instanceof Error ? error.message : String(error);
498
+ const hasSession = sessionExists(sessionDir, sessionId);
499
+ if (!hasSession && !options.sessionDir) {
500
+ await rm(sessionDir, { recursive: true, force: true }).catch(() => undefined);
501
+ }
502
+ return {
503
+ agent: options.agentName,
504
+ task: options.control?.getObjective() ?? options.task,
505
+ exitCode: 1,
506
+ messages: [],
507
+ stderr: errorMessage,
508
+ usage: emptyUsage(),
509
+ model: options.agent.model,
510
+ thinking: options.thinkingLevel,
511
+ stopReason: "error",
512
+ errorMessage,
513
+ dispatchFailed: true,
514
+ ...(hasSession || options.sessionDir ? { sessionId, sessionDir } : {}),
515
+ };
516
+ };
517
+
518
+ const runWithStartupRetry = async (opts: RunSingleOptions): Promise<SingleResult> => {
519
+ let lastResult: SingleResult;
520
+ let retries = 0;
521
+ for (let attempt = 0; ; attempt++) {
522
+ const immediate = controlledDisposition(opts);
523
+ if (immediate) return immediate;
524
+ const start = Date.now();
525
+ try {
526
+ const attemptOptions = opts.resolveAgentForAttempt
527
+ ? { ...opts, agent: opts.resolveAgentForAttempt(opts.agent) }
528
+ : opts;
529
+ lastResult = await runSingleAgent(attemptOptions);
530
+ } catch (error) {
531
+ const failed = await dispatchFailure(error);
532
+ return controlledDisposition(opts, failed) ?? failed;
533
+ }
534
+ const durationMs = Date.now() - start;
535
+ const controlled = controlledDisposition(opts, lastResult);
536
+ if (controlled) return controlled;
537
+ if (lastResult.stopReason === "aborted") return lastResult;
538
+ if (!isRetryableStartupFailure(lastResult, durationMs)) {
539
+ if (retries > 0 && !isFailedResult(lastResult)) lastResult.startupRetries = retries;
540
+ return lastResult;
541
+ }
542
+ const delay = startupDelays[attempt];
543
+ if (delay === undefined) {
544
+ lastResult.errorMessage = formatStartupRetryExhaustedError(
545
+ lastResult.model ?? opts.agent.model ?? "default",
546
+ attempt + 1,
547
+ );
548
+ lastResult.stopReason ??= "error";
549
+ lastResult.dispatchFailed = true;
550
+ return lastResult;
551
+ }
552
+ opts.control?.markRetrying();
553
+ try {
554
+ opts.onLive?.({ kind: "status", status: "running" });
555
+ } catch {
556
+ /* never throw from event handling */
557
+ }
558
+ const retryDelay = customStartupDelays ? delay : addStartupRetryJitter(delay);
559
+ if (!(await waitForControlledRetry(retryDelay, opts.signal, opts.control))) {
560
+ return controlledDisposition(opts, lastResult) ?? signalAbortDisposition(opts, lastResult) ?? lastResult;
561
+ }
562
+ retries++;
563
+ }
564
+ };
565
+
566
+ const selectedRef = launchedRef?.trim() || undefined;
567
+ const normalizedMainRef = mainFallbackRef?.trim() || undefined;
568
+ const candidates: Array<{ agent: AgentConfig; ref?: string }> = [
569
+ { agent, ref: selectedRef },
570
+ ];
571
+ if (normalizedMainRef && normalizedMainRef !== selectedRef) {
572
+ candidates.push({ agent: { ...agent, model: normalizedMainRef }, ref: normalizedMainRef });
573
+ }
574
+
575
+ let fallbackUsed = false;
576
+ let result: SingleResult | undefined;
577
+ const priorFailedTools: NonNullable<SingleResult["failedTools"]> = [];
578
+ const priorUsage = emptyUsage();
579
+
580
+ const retainAttemptDiagnostics = (attempt: SingleResult): void => {
581
+ priorFailedTools.push(...(attempt.failedTools ?? []));
582
+ priorUsage.input += attempt.usage.input;
583
+ priorUsage.output += attempt.usage.output;
584
+ priorUsage.cacheRead += attempt.usage.cacheRead;
585
+ priorUsage.cacheWrite += attempt.usage.cacheWrite;
586
+ priorUsage.cost += attempt.usage.cost;
587
+ priorUsage.turns += attempt.usage.turns;
588
+ priorUsage.contextTokens = attempt.usage.contextTokens || priorUsage.contextTokens;
589
+ };
590
+
591
+ const finish = async (settled: SingleResult): Promise<SingleResult> => {
592
+ if (priorFailedTools.length > 0) {
593
+ settled.failedTools = [...priorFailedTools, ...(settled.failedTools ?? [])];
594
+ }
595
+ if (
596
+ priorUsage.turns || priorUsage.input || priorUsage.output || priorUsage.cacheRead ||
597
+ priorUsage.cacheWrite || priorUsage.cost || priorUsage.contextTokens
598
+ ) {
599
+ settled.usage = {
600
+ input: priorUsage.input + settled.usage.input,
601
+ output: priorUsage.output + settled.usage.output,
602
+ cacheRead: priorUsage.cacheRead + settled.usage.cacheRead,
603
+ cacheWrite: priorUsage.cacheWrite + settled.usage.cacheWrite,
604
+ cost: priorUsage.cost + settled.usage.cost,
605
+ turns: priorUsage.turns + settled.usage.turns,
606
+ contextTokens: settled.usage.contextTokens || priorUsage.contextTokens,
607
+ };
608
+ }
609
+ const persistedSession = sessionExists(sessionDir, sessionId);
610
+ if (!settled.dispatchFailed || persistedSession || options.sessionDir) {
611
+ settled.sessionId ??= sessionId;
612
+ settled.sessionDir ??= sessionDir;
613
+ } else {
614
+ await rm(sessionDir, { recursive: true, force: true }).catch(() => undefined);
615
+ settled.sessionId = undefined;
616
+ settled.sessionDir = undefined;
617
+ }
618
+ settled.task = options.control?.getObjective() ?? settled.task;
619
+ if (fallbackUsed && launchedRef) settled.modelFallbackFrom = launchedRef;
620
+ options.control?.markSettled();
621
+ return settled;
622
+ };
623
+
624
+ for (let candidateIndex = 0; candidateIndex < candidates.length; candidateIndex++) {
625
+ const candidate = candidates[candidateIndex];
626
+ fallbackUsed ||= candidateIndex > 0;
627
+ const previousModel = result?.model ?? candidates[candidateIndex - 1]?.ref;
628
+ const candidateThinking = options.thinkingLevelForModel?.(candidate.ref) ?? options.thinkingLevel;
629
+ const candidateOptions: RunSingleOptions = {
630
+ ...baseOptions,
631
+ agent: candidate.agent,
632
+ thinkingLevel: candidateThinking,
633
+ ...(candidateIndex > 0
634
+ ? {
635
+ stdinText: buildResumePrompt(
636
+ options.control?.getObjective() ?? options.task,
637
+ buildFallbackResumeReason(previousModel),
638
+ ),
639
+ }
640
+ : {}),
641
+ };
642
+ try {
643
+ options.onLive?.({
644
+ kind: "model",
645
+ model: candidate.ref,
646
+ thinking: candidateThinking,
647
+ ...(candidateIndex > 0 && launchedRef ? { fallbackFrom: launchedRef } : {}),
648
+ });
649
+ } catch {
650
+ /* never throw from event handling */
651
+ }
652
+
653
+ result = await runWithStartupRetry(candidateOptions);
654
+ if (result.stopReason === "aborted") return finish(result);
655
+ if (!isModelLevelFailure(result)) return finish(result);
656
+ // Any model-level failure advances immediately to the sole fallback (the
657
+ // current main model). Retain selected-attempt tool diagnostics and usage;
658
+ // ordinary task/tool failures returned above without a handoff.
659
+ if (candidateIndex < candidates.length - 1) retainAttemptDiagnostics(result);
660
+ }
661
+
662
+ return finish(result!);
663
+ }