pi-subagents 0.24.3 → 0.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/CHANGELOG.md +26 -5
  2. package/README.md +19 -11
  3. package/package.json +4 -8
  4. package/prompts/review-loop.md +1 -1
  5. package/skills/pi-subagents/SKILL.md +46 -10
  6. package/src/agents/agent-management.ts +5 -0
  7. package/src/agents/agent-serializer.ts +2 -0
  8. package/src/agents/agents.ts +30 -6
  9. package/src/agents/skills.ts +25 -23
  10. package/src/extension/config.ts +16 -0
  11. package/src/extension/fanout-child.ts +170 -0
  12. package/src/extension/index.ts +13 -25
  13. package/src/intercom/intercom-bridge.ts +2 -1
  14. package/src/intercom/result-intercom.ts +108 -0
  15. package/src/runs/background/async-execution.ts +107 -7
  16. package/src/runs/background/async-job-tracker.ts +57 -14
  17. package/src/runs/background/async-resume.ts +28 -15
  18. package/src/runs/background/async-status.ts +60 -30
  19. package/src/runs/background/result-watcher.ts +111 -54
  20. package/src/runs/background/run-id-resolver.ts +83 -0
  21. package/src/runs/background/run-status.ts +79 -3
  22. package/src/runs/background/stale-run-reconciler.ts +46 -1
  23. package/src/runs/background/subagent-runner.ts +66 -18
  24. package/src/runs/foreground/chain-execution.ts +6 -0
  25. package/src/runs/foreground/execution.ts +21 -5
  26. package/src/runs/foreground/subagent-executor.ts +314 -18
  27. package/src/runs/shared/completion-guard.ts +23 -1
  28. package/src/runs/shared/mcp-direct-tool-allowlist.ts +365 -0
  29. package/src/runs/shared/nested-events.ts +819 -0
  30. package/src/runs/shared/nested-path.ts +52 -0
  31. package/src/runs/shared/nested-render.ts +115 -0
  32. package/src/runs/shared/parallel-utils.ts +1 -0
  33. package/src/runs/shared/pi-args.ts +67 -5
  34. package/src/runs/shared/run-history.ts +12 -7
  35. package/src/runs/shared/single-output.ts +12 -2
  36. package/src/runs/shared/subagent-prompt-runtime.ts +25 -5
  37. package/src/shared/artifacts.ts +2 -2
  38. package/src/shared/types.ts +95 -0
  39. package/src/shared/utils.ts +11 -1
  40. package/src/tui/render.ts +254 -153
@@ -0,0 +1,819 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import * as fs from "node:fs";
3
+ import * as path from "node:path";
4
+ import {
5
+ ASYNC_DIR,
6
+ RESULTS_DIR,
7
+ TEMP_ROOT_DIR,
8
+ type AsyncJobState,
9
+ type AsyncStatus,
10
+ type NestedRouteInfo,
11
+ type NestedRunSummary,
12
+ type NestedRunState,
13
+ type NestedStepSummary,
14
+ type SubagentRunMode,
15
+ type SubagentState,
16
+ } from "../../shared/types.ts";
17
+ import { isSafeNestedPathId, parseNestedPathEnv, sanitizeNestedPath, type NestedPathEntry } from "./nested-path.ts";
18
+ import {
19
+ SUBAGENT_PARENT_CAPABILITY_TOKEN_ENV,
20
+ SUBAGENT_PARENT_CHILD_INDEX_ENV,
21
+ SUBAGENT_PARENT_CONTROL_INBOX_ENV,
22
+ SUBAGENT_PARENT_DEPTH_ENV,
23
+ SUBAGENT_PARENT_EVENT_SINK_ENV,
24
+ SUBAGENT_PARENT_PATH_ENV,
25
+ SUBAGENT_PARENT_ROOT_RUN_ID_ENV,
26
+ SUBAGENT_PARENT_RUN_ID_ENV,
27
+ } from "./pi-args.ts";
28
+ import { writeAtomicJson } from "../../shared/atomic-json.ts";
29
+
30
+ export const NESTED_EVENTS_DIR = path.join(TEMP_ROOT_DIR, "nested-subagent-events");
31
+ const ROUTE_FILE = "route.json";
32
+ const REGISTRY_FILE = "registry.json";
33
+ const MAX_EVENT_BYTES = 64 * 1024;
34
+ const MAX_STEPS = 12;
35
+ const MAX_CHILDREN = 16;
36
+ const MAX_DEPTH = 3;
37
+
38
+ type NestedStatusEventType = "subagent.nested.started" | "subagent.nested.updated" | "subagent.nested.completed";
39
+ type NestedControlResultEventType = "subagent.nested.control-result";
40
+
41
+ export type NestedRoute = NestedRouteInfo;
42
+
43
+ export interface NestedEventRecord {
44
+ type: NestedStatusEventType;
45
+ ts: number;
46
+ rootRunId: string;
47
+ parentRunId: string;
48
+ parentStepIndex?: number;
49
+ capabilityToken: string;
50
+ child: NestedRunSummary;
51
+ }
52
+
53
+ export interface NestedControlResultRecord {
54
+ type: NestedControlResultEventType;
55
+ ts: number;
56
+ rootRunId: string;
57
+ capabilityToken: string;
58
+ requestId: string;
59
+ targetRunId: string;
60
+ ok: boolean;
61
+ message: string;
62
+ }
63
+
64
+ export interface NestedControlRequestRecord {
65
+ type: "subagent.nested.control-request";
66
+ ts: number;
67
+ rootRunId: string;
68
+ capabilityToken: string;
69
+ requestId: string;
70
+ targetRunId: string;
71
+ action: "interrupt" | "resume";
72
+ message?: string;
73
+ }
74
+
75
+ export interface NestedRegistry {
76
+ rootRunId: string;
77
+ updatedAt: number;
78
+ children: NestedRunSummary[];
79
+ processedEvents: string[];
80
+ }
81
+
82
+ export function isSafeNestedId(value: unknown): value is string {
83
+ return isSafeNestedPathId(value);
84
+ }
85
+
86
+ export function assertSafeNestedId(label: string, value: string): void {
87
+ if (!isSafeNestedId(value)) throw new Error(`${label} must be a non-empty safe id token.`);
88
+ }
89
+
90
+ function assertSafeId(label: string, value: string): void {
91
+ assertSafeNestedId(label, value);
92
+ }
93
+
94
+ function containedPath(base: string, candidate: string): boolean {
95
+ const resolvedBase = path.resolve(base);
96
+ const resolvedCandidate = path.resolve(candidate);
97
+ return resolvedCandidate === resolvedBase || resolvedCandidate.startsWith(`${resolvedBase}${path.sep}`);
98
+ }
99
+
100
+ function commonRouteRoot(route: Pick<NestedRoute, "eventSink" | "controlInbox">): string {
101
+ return path.dirname(path.resolve(route.eventSink));
102
+ }
103
+
104
+ function validateRouteShape(route: NestedRoute): void {
105
+ assertSafeId("rootRunId", route.rootRunId);
106
+ assertSafeId("capabilityToken", route.capabilityToken);
107
+ if (!containedPath(NESTED_EVENTS_DIR, route.eventSink)) throw new Error("Nested event sink is outside the subagent nested event root.");
108
+ if (!containedPath(NESTED_EVENTS_DIR, route.controlInbox)) throw new Error("Nested control inbox is outside the subagent nested event root.");
109
+ if (commonRouteRoot(route) !== path.dirname(path.resolve(route.controlInbox))) throw new Error("Nested event sink and control inbox must share one route root.");
110
+ }
111
+
112
+ export function createNestedRoute(rootRunId: string): NestedRoute {
113
+ assertSafeId("rootRunId", rootRunId);
114
+ const capabilityToken = randomUUID();
115
+ const routeRoot = path.join(NESTED_EVENTS_DIR, `${rootRunId}-${capabilityToken}`);
116
+ const eventSink = path.join(routeRoot, "events");
117
+ const controlInbox = path.join(routeRoot, "controls");
118
+ fs.mkdirSync(eventSink, { recursive: true, mode: 0o700 });
119
+ fs.mkdirSync(controlInbox, { recursive: true, mode: 0o700 });
120
+ fs.writeFileSync(path.join(routeRoot, ROUTE_FILE), `${JSON.stringify({ rootRunId, capabilityToken, createdAt: Date.now() })}\n`, { mode: 0o600 });
121
+ return { rootRunId, eventSink, controlInbox, capabilityToken };
122
+ }
123
+
124
+ export function resolveNestedRouteFromEnv(env: NodeJS.ProcessEnv = process.env): NestedRoute | undefined {
125
+ const rootRunId = env[SUBAGENT_PARENT_ROOT_RUN_ID_ENV];
126
+ const eventSink = env[SUBAGENT_PARENT_EVENT_SINK_ENV];
127
+ const controlInbox = env[SUBAGENT_PARENT_CONTROL_INBOX_ENV];
128
+ const capabilityToken = env[SUBAGENT_PARENT_CAPABILITY_TOKEN_ENV];
129
+ if (!rootRunId || !eventSink || !controlInbox || !capabilityToken) return undefined;
130
+ const route = { rootRunId, eventSink, controlInbox, capabilityToken };
131
+ validateRouteShape(route);
132
+ const routeFile = path.join(commonRouteRoot(route), ROUTE_FILE);
133
+ const metadata = JSON.parse(fs.readFileSync(routeFile, "utf-8")) as { rootRunId?: unknown; capabilityToken?: unknown };
134
+ if (metadata.rootRunId !== rootRunId || metadata.capabilityToken !== capabilityToken) {
135
+ throw new Error("Nested event route metadata does not match the provided root id and capability token.");
136
+ }
137
+ return route;
138
+ }
139
+
140
+ export function resolveInheritedNestedRouteFromEnv(env: NodeJS.ProcessEnv = process.env): NestedRoute | undefined {
141
+ try {
142
+ return resolveNestedRouteFromEnv(env);
143
+ } catch (error) {
144
+ console.error("Ignoring invalid nested subagent event route:", error);
145
+ return undefined;
146
+ }
147
+ }
148
+
149
+ export function resolveNestedParentAddressFromEnv(env: NodeJS.ProcessEnv = process.env): { parentRunId: string; parentStepIndex?: number; depth: number; path: NestedPathEntry[] } | undefined {
150
+ const parentRunId = env[SUBAGENT_PARENT_RUN_ID_ENV];
151
+ if (!isSafeNestedId(parentRunId)) return undefined;
152
+ const rawIndex = env[SUBAGENT_PARENT_CHILD_INDEX_ENV];
153
+ const parentStepIndex = rawIndex && /^\d+$/.test(rawIndex) ? Number(rawIndex) : undefined;
154
+ const depth = Math.min(Math.max(1, clampNumber(Number(env[SUBAGENT_PARENT_DEPTH_ENV])) ?? 1), MAX_DEPTH);
155
+ const parsedPath = parseNestedPathEnv(env[SUBAGENT_PARENT_PATH_ENV]);
156
+ const nestedPath = parsedPath.length ? parsedPath : [{ runId: parentRunId, ...(parentStepIndex !== undefined ? { stepIndex: parentStepIndex } : {}) }];
157
+ return { parentRunId, ...(parentStepIndex !== undefined ? { parentStepIndex } : {}), depth, path: nestedPath };
158
+ }
159
+
160
+ export function resolveNestedAsyncDir(rootRunId: string, run: NestedRunSummary): string | undefined {
161
+ if (!run.asyncDir) return undefined;
162
+ const resolved = path.resolve(run.asyncDir);
163
+ const nestedRoot = path.resolve(TEMP_ROOT_DIR, "nested-subagent-runs", rootRunId, run.id);
164
+ const relative = path.relative(nestedRoot, resolved);
165
+ return resolved === nestedRoot || (!relative.startsWith("..") && !path.isAbsolute(relative)) ? resolved : undefined;
166
+ }
167
+
168
+ function clampNumber(value: unknown): number | undefined {
169
+ return typeof value === "number" && Number.isFinite(value) ? value : undefined;
170
+ }
171
+
172
+ function stringValue(value: unknown, max = 512): string | undefined {
173
+ return typeof value === "string" && value.length > 0 ? value.slice(0, max) : undefined;
174
+ }
175
+
176
+ function sanitizeTokenUsage(value: unknown): NestedRunSummary["totalTokens"] | undefined {
177
+ if (!value || typeof value !== "object") return undefined;
178
+ const raw = value as Record<string, unknown>;
179
+ const input = clampNumber(raw.input);
180
+ const output = clampNumber(raw.output);
181
+ const total = clampNumber(raw.total);
182
+ return input !== undefined && output !== undefined && total !== undefined
183
+ ? { input, output, total }
184
+ : undefined;
185
+ }
186
+
187
+ function sanitizeState(value: unknown, fallback: NestedRunState): NestedRunState {
188
+ return value === "queued" || value === "running" || value === "complete" || value === "failed" || value === "paused"
189
+ ? value
190
+ : fallback;
191
+ }
192
+
193
+ function sanitizeStep(input: unknown, depth: number): NestedStepSummary | undefined {
194
+ if (!input || typeof input !== "object") return undefined;
195
+ const raw = input as Record<string, unknown>;
196
+ const agent = stringValue(raw.agent, 128);
197
+ if (!agent) return undefined;
198
+ const status = raw.status === "pending" || raw.status === "running" || raw.status === "complete" || raw.status === "completed" || raw.status === "failed" || raw.status === "paused"
199
+ ? raw.status
200
+ : "pending";
201
+ return {
202
+ agent,
203
+ status,
204
+ ...(stringValue(raw.sessionFile, 2048) ? { sessionFile: stringValue(raw.sessionFile, 2048) } : {}),
205
+ ...(raw.activityState === "active_long_running" || raw.activityState === "needs_attention" ? { activityState: raw.activityState } : {}),
206
+ ...(clampNumber(raw.lastActivityAt) !== undefined ? { lastActivityAt: clampNumber(raw.lastActivityAt) } : {}),
207
+ ...(stringValue(raw.currentTool, 128) ? { currentTool: stringValue(raw.currentTool, 128) } : {}),
208
+ ...(clampNumber(raw.currentToolStartedAt) !== undefined ? { currentToolStartedAt: clampNumber(raw.currentToolStartedAt) } : {}),
209
+ ...(stringValue(raw.currentPath, 2048) ? { currentPath: stringValue(raw.currentPath, 2048) } : {}),
210
+ ...(clampNumber(raw.turnCount) !== undefined ? { turnCount: clampNumber(raw.turnCount) } : {}),
211
+ ...(clampNumber(raw.toolCount) !== undefined ? { toolCount: clampNumber(raw.toolCount) } : {}),
212
+ ...(clampNumber(raw.startedAt) !== undefined ? { startedAt: clampNumber(raw.startedAt) } : {}),
213
+ ...(clampNumber(raw.endedAt) !== undefined ? { endedAt: clampNumber(raw.endedAt) } : {}),
214
+ ...(stringValue(raw.error, 1024) ? { error: stringValue(raw.error, 1024) } : {}),
215
+ ...(depth < MAX_DEPTH && Array.isArray(raw.children) ? { children: raw.children.map((child) => sanitizeSummary(child, depth + 1)).filter((child): child is NestedRunSummary => Boolean(child)).slice(0, MAX_CHILDREN) } : {}),
216
+ };
217
+ }
218
+
219
+ export function sanitizeSummary(input: unknown, depth = 0): NestedRunSummary | undefined {
220
+ if (!input || typeof input !== "object") return undefined;
221
+ const raw = input as Record<string, unknown>;
222
+ if (!isSafeNestedId(raw.id) || !isSafeNestedId(raw.parentRunId)) return undefined;
223
+ const pathParts = sanitizeNestedPath(raw.path);
224
+ const steps = Array.isArray(raw.steps)
225
+ ? raw.steps.map((step) => sanitizeStep(step, depth + 1)).filter((step): step is NestedStepSummary => Boolean(step)).slice(0, MAX_STEPS)
226
+ : undefined;
227
+ const totalTokens = sanitizeTokenUsage(raw.totalTokens);
228
+ return {
229
+ id: raw.id,
230
+ parentRunId: raw.parentRunId,
231
+ ...(clampNumber(raw.parentStepIndex) !== undefined ? { parentStepIndex: clampNumber(raw.parentStepIndex) } : {}),
232
+ ...(stringValue(raw.parentAgent, 128) ? { parentAgent: stringValue(raw.parentAgent, 128) } : {}),
233
+ depth: Math.min(Math.max(0, clampNumber(raw.depth) ?? 0), MAX_DEPTH),
234
+ path: pathParts,
235
+ state: sanitizeState(raw.state, "running"),
236
+ ...(stringValue(raw.asyncDir, 2048) ? { asyncDir: stringValue(raw.asyncDir, 2048) } : {}),
237
+ ...(clampNumber(raw.pid) !== undefined && clampNumber(raw.pid)! > 0 && Number.isInteger(clampNumber(raw.pid)) ? { pid: clampNumber(raw.pid) } : {}),
238
+ ...(stringValue(raw.sessionId, 256) ? { sessionId: stringValue(raw.sessionId, 256) } : {}),
239
+ ...(stringValue(raw.sessionFile, 2048) ? { sessionFile: stringValue(raw.sessionFile, 2048) } : {}),
240
+ ...(stringValue(raw.intercomTarget, 256) ? { intercomTarget: stringValue(raw.intercomTarget, 256) } : {}),
241
+ ...(stringValue(raw.ownerIntercomTarget, 256) ? { ownerIntercomTarget: stringValue(raw.ownerIntercomTarget, 256) } : {}),
242
+ ...(stringValue(raw.leafIntercomTarget, 256) ? { leafIntercomTarget: stringValue(raw.leafIntercomTarget, 256) } : {}),
243
+ ...(raw.ownerState === "live" || raw.ownerState === "gone" || raw.ownerState === "unknown" ? { ownerState: raw.ownerState } : {}),
244
+ ...(stringValue(raw.controlInbox, 2048) ? { controlInbox: stringValue(raw.controlInbox, 2048) } : {}),
245
+ ...(stringValue(raw.capabilityToken, 128) ? { capabilityToken: stringValue(raw.capabilityToken, 128) } : {}),
246
+ ...(raw.mode === "single" || raw.mode === "parallel" || raw.mode === "chain" ? { mode: raw.mode } : {}),
247
+ ...(stringValue(raw.agent, 128) ? { agent: stringValue(raw.agent, 128) } : {}),
248
+ ...(Array.isArray(raw.agents) ? { agents: raw.agents.map((agent) => stringValue(agent, 128)).filter((agent): agent is string => Boolean(agent)).slice(0, MAX_STEPS) } : {}),
249
+ ...(clampNumber(raw.currentStep) !== undefined ? { currentStep: clampNumber(raw.currentStep) } : {}),
250
+ ...(clampNumber(raw.chainStepCount) !== undefined ? { chainStepCount: clampNumber(raw.chainStepCount) } : {}),
251
+ ...(raw.activityState === "active_long_running" || raw.activityState === "needs_attention" ? { activityState: raw.activityState } : {}),
252
+ ...(clampNumber(raw.lastActivityAt) !== undefined ? { lastActivityAt: clampNumber(raw.lastActivityAt) } : {}),
253
+ ...(stringValue(raw.currentTool, 128) ? { currentTool: stringValue(raw.currentTool, 128) } : {}),
254
+ ...(clampNumber(raw.currentToolStartedAt) !== undefined ? { currentToolStartedAt: clampNumber(raw.currentToolStartedAt) } : {}),
255
+ ...(stringValue(raw.currentPath, 2048) ? { currentPath: stringValue(raw.currentPath, 2048) } : {}),
256
+ ...(clampNumber(raw.turnCount) !== undefined ? { turnCount: clampNumber(raw.turnCount) } : {}),
257
+ ...(clampNumber(raw.toolCount) !== undefined ? { toolCount: clampNumber(raw.toolCount) } : {}),
258
+ ...(totalTokens ? { totalTokens } : {}),
259
+ ...(clampNumber(raw.startedAt) !== undefined ? { startedAt: clampNumber(raw.startedAt) } : {}),
260
+ ...(clampNumber(raw.endedAt) !== undefined ? { endedAt: clampNumber(raw.endedAt) } : {}),
261
+ ...(clampNumber(raw.lastUpdate) !== undefined ? { lastUpdate: clampNumber(raw.lastUpdate) } : {}),
262
+ ...(stringValue(raw.error, 1024) ? { error: stringValue(raw.error, 1024) } : {}),
263
+ ...(steps && steps.length > 0 ? { steps } : {}),
264
+ ...(depth < MAX_DEPTH && Array.isArray(raw.children) ? { children: raw.children.map((child) => sanitizeSummary(child, depth + 1)).filter((child): child is NestedRunSummary => Boolean(child)).slice(0, MAX_CHILDREN) } : {}),
265
+ };
266
+ }
267
+
268
+ function parseRecord(content: string, route: NestedRoute): NestedEventRecord | undefined {
269
+ if (Buffer.byteLength(content, "utf-8") > MAX_EVENT_BYTES) return undefined;
270
+ let parsed: unknown;
271
+ try {
272
+ parsed = JSON.parse(content);
273
+ } catch {
274
+ return undefined;
275
+ }
276
+ if (!parsed || typeof parsed !== "object") return undefined;
277
+ const raw = parsed as Record<string, unknown>;
278
+ if (raw.type !== "subagent.nested.started" && raw.type !== "subagent.nested.updated" && raw.type !== "subagent.nested.completed") return undefined;
279
+ if (raw.rootRunId !== route.rootRunId || raw.capabilityToken !== route.capabilityToken) return undefined;
280
+ if (!isSafeNestedId(raw.parentRunId)) return undefined;
281
+ const ts = clampNumber(raw.ts);
282
+ if (ts === undefined) return undefined;
283
+ const child = sanitizeSummary(raw.child);
284
+ if (!child || child.id === route.rootRunId) return undefined;
285
+ const routedChild: NestedRunSummary = {
286
+ ...child,
287
+ controlInbox: route.controlInbox,
288
+ capabilityToken: route.capabilityToken,
289
+ ownerState: child.ownerState ?? "unknown",
290
+ };
291
+ return {
292
+ type: raw.type,
293
+ ts,
294
+ rootRunId: route.rootRunId,
295
+ parentRunId: raw.parentRunId,
296
+ ...(clampNumber(raw.parentStepIndex) !== undefined ? { parentStepIndex: clampNumber(raw.parentStepIndex) } : {}),
297
+ capabilityToken: route.capabilityToken,
298
+ child: routedChild,
299
+ };
300
+ }
301
+
302
+ export function parseNestedEventRecords(content: string, route: NestedRoute): NestedEventRecord[] {
303
+ if (!content.includes("\n")) {
304
+ const record = parseRecord(content.trim(), route);
305
+ return record ? [record] : [];
306
+ }
307
+ return content.split("\n")
308
+ .slice(0, content.endsWith("\n") ? undefined : -1)
309
+ .map((line) => line.trim() ? parseRecord(line, route) : undefined)
310
+ .filter((event): event is NestedEventRecord => Boolean(event));
311
+ }
312
+
313
+ function terminal(state: NestedRunState): boolean {
314
+ return state === "complete" || state === "failed" || state === "paused";
315
+ }
316
+
317
+ function mergeSummary(existing: NestedRunSummary | undefined, event: NestedEventRecord): NestedRunSummary {
318
+ const incomingState = event.type === "subagent.nested.completed" && event.child.state === "running" ? "complete" : event.child.state;
319
+ const incoming = { ...event.child, state: incomingState, lastUpdate: event.child.lastUpdate ?? event.ts };
320
+ if (!existing) return incoming;
321
+ const existingUpdate = existing.lastUpdate ?? 0;
322
+ const incomingUpdate = incoming.lastUpdate ?? event.ts;
323
+ if (incomingUpdate < existingUpdate) return existing;
324
+ if (terminal(existing.state) && !terminal(incoming.state)) return existing;
325
+ if (terminal(existing.state) && terminal(incoming.state) && incomingUpdate === existingUpdate) return existing;
326
+ return { ...existing, ...incoming, state: incoming.state, lastUpdate: Math.max(existingUpdate, incomingUpdate) };
327
+ }
328
+
329
+ function attachChild(children: NestedRunSummary[], event: NestedEventRecord): NestedRunSummary[] {
330
+ let updated = false;
331
+ const walk = (items: NestedRunSummary[]): NestedRunSummary[] => items.map((item) => {
332
+ if (item.id === event.parentRunId) {
333
+ const existingChildren = item.children ?? [];
334
+ const childIndex = existingChildren.findIndex((child) => child.id === event.child.id);
335
+ const nextChild = mergeSummary(childIndex >= 0 ? existingChildren[childIndex] : undefined, event);
336
+ const nextChildren = childIndex >= 0
337
+ ? existingChildren.map((child, index) => index === childIndex ? nextChild : child)
338
+ : [...existingChildren, nextChild];
339
+ updated = true;
340
+ return { ...item, children: nextChildren.slice(0, MAX_CHILDREN), lastUpdate: Math.max(item.lastUpdate ?? 0, event.ts) };
341
+ }
342
+ if (!item.children?.length) return item;
343
+ const nextChildren = walk(item.children);
344
+ return nextChildren === item.children ? item : { ...item, children: nextChildren };
345
+ });
346
+ const next = walk(children);
347
+ if (updated) return next;
348
+ const childIndex = next.findIndex((child) => child.id === event.child.id);
349
+ const nextChild = mergeSummary(childIndex >= 0 ? next[childIndex] : undefined, event);
350
+ return childIndex >= 0
351
+ ? next.map((child, index) => index === childIndex ? nextChild : child)
352
+ : [...next, nextChild].slice(0, MAX_CHILDREN);
353
+ }
354
+
355
+ export function applyNestedEvent(registry: NestedRegistry, event: NestedEventRecord): NestedRegistry {
356
+ return {
357
+ ...registry,
358
+ updatedAt: Math.max(registry.updatedAt, event.ts),
359
+ children: attachChild(registry.children, event),
360
+ };
361
+ }
362
+
363
+ function registryPath(route: NestedRoute): string {
364
+ return path.join(commonRouteRoot(route), REGISTRY_FILE);
365
+ }
366
+
367
+ export function findNestedRouteForRootId(rootRunId: string): NestedRoute | undefined {
368
+ assertSafeId("rootRunId", rootRunId);
369
+ let entries: string[];
370
+ try {
371
+ entries = fs.readdirSync(NESTED_EVENTS_DIR);
372
+ } catch (error) {
373
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
374
+ throw error;
375
+ }
376
+ for (const entry of entries) {
377
+ if (!entry.startsWith(`${rootRunId}-`)) continue;
378
+ const routeRoot = path.join(NESTED_EVENTS_DIR, entry);
379
+ try {
380
+ const metadata = JSON.parse(fs.readFileSync(path.join(routeRoot, ROUTE_FILE), "utf-8")) as { rootRunId?: unknown; capabilityToken?: unknown };
381
+ if (metadata.rootRunId !== rootRunId || typeof metadata.capabilityToken !== "string") continue;
382
+ const route = {
383
+ rootRunId,
384
+ eventSink: path.join(routeRoot, "events"),
385
+ controlInbox: path.join(routeRoot, "controls"),
386
+ capabilityToken: metadata.capabilityToken,
387
+ };
388
+ validateRouteShape(route);
389
+ return route;
390
+ } catch {
391
+ continue;
392
+ }
393
+ }
394
+ return undefined;
395
+ }
396
+
397
+ export function projectNestedRegistryForRoot(rootRunId: string): NestedRegistry | undefined {
398
+ const route = findNestedRouteForRootId(rootRunId);
399
+ return route ? projectNestedEvents(route) : undefined;
400
+ }
401
+
402
+ export function findNestedRun(children: NestedRunSummary[] | undefined, id: string): NestedRunSummary | undefined {
403
+ if (!children?.length) return undefined;
404
+ for (const child of children) {
405
+ if (child.id === id) return child;
406
+ const nested = findNestedRun(child.children, id) ?? findNestedRun(child.steps?.flatMap((step) => step.children ?? []), id);
407
+ if (nested) return nested;
408
+ }
409
+ return undefined;
410
+ }
411
+
412
+ export interface NestedRunMatch {
413
+ rootRunId: string;
414
+ route: NestedRoute;
415
+ run: NestedRunSummary;
416
+ }
417
+
418
+ export interface NestedRunResolutionScope {
419
+ routes: NestedRoute[];
420
+ descendantOf?: { parentRunId: string; parentStepIndex?: number };
421
+ }
422
+
423
+ function collectNestedRuns(children: NestedRunSummary[] | undefined, output: NestedRunSummary[] = []): NestedRunSummary[] {
424
+ for (const child of children ?? []) {
425
+ output.push(child);
426
+ collectNestedRuns(child.children, output);
427
+ collectNestedRuns(child.steps?.flatMap((step) => step.children ?? []), output);
428
+ }
429
+ return output;
430
+ }
431
+
432
+ function collectScopedNestedRuns(children: NestedRunSummary[] | undefined, scope: NestedRunResolutionScope["descendantOf"], output: NestedRunSummary[] = []): NestedRunSummary[] {
433
+ if (!scope) return collectNestedRuns(children, output);
434
+ for (const child of children ?? []) {
435
+ if (child.parentRunId === scope.parentRunId && (scope.parentStepIndex === undefined || child.parentStepIndex === scope.parentStepIndex)) {
436
+ collectNestedRuns([child], output);
437
+ continue;
438
+ }
439
+ collectScopedNestedRuns(child.children, scope, output);
440
+ collectScopedNestedRuns(child.steps?.flatMap((step) => step.children ?? []), scope, output);
441
+ }
442
+ return output;
443
+ }
444
+
445
+ function listNestedRoutes(): NestedRoute[] {
446
+ let entries: string[];
447
+ try {
448
+ entries = fs.readdirSync(NESTED_EVENTS_DIR);
449
+ } catch (error) {
450
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return [];
451
+ throw error;
452
+ }
453
+ const routes: NestedRoute[] = [];
454
+ for (const entry of entries) {
455
+ const routeRoot = path.join(NESTED_EVENTS_DIR, entry);
456
+ try {
457
+ const metadata = JSON.parse(fs.readFileSync(path.join(routeRoot, ROUTE_FILE), "utf-8")) as { rootRunId?: unknown; capabilityToken?: unknown };
458
+ if (typeof metadata.rootRunId !== "string" || typeof metadata.capabilityToken !== "string") continue;
459
+ const route = {
460
+ rootRunId: metadata.rootRunId,
461
+ eventSink: path.join(routeRoot, "events"),
462
+ controlInbox: path.join(routeRoot, "controls"),
463
+ capabilityToken: metadata.capabilityToken,
464
+ };
465
+ validateRouteShape(route);
466
+ routes.push(route);
467
+ } catch {
468
+ continue;
469
+ }
470
+ }
471
+ return routes;
472
+ }
473
+
474
+ export function findNestedRunMatchesById(id: string, options: { prefix?: boolean; scope?: NestedRunResolutionScope } = {}): NestedRunMatch[] {
475
+ assertSafeId("id", id);
476
+ const matches: NestedRunMatch[] = [];
477
+ for (const route of options.scope?.routes ?? listNestedRoutes()) {
478
+ try {
479
+ const registry = projectNestedEvents(route);
480
+ for (const run of collectScopedNestedRuns(registry.children, options.scope?.descendantOf)) {
481
+ if (options.prefix ? run.id.startsWith(id) : run.id === id) matches.push({ rootRunId: route.rootRunId, route, run });
482
+ }
483
+ } catch {
484
+ continue;
485
+ }
486
+ }
487
+ return matches;
488
+ }
489
+
490
+ export function findNestedRunById(id: string): { rootRunId: string; run: NestedRunSummary } | undefined {
491
+ const match = findNestedRunMatchesById(id)[0];
492
+ return match ? { rootRunId: match.rootRunId, run: match.run } : undefined;
493
+ }
494
+
495
+ export function readNestedRegistry(route: NestedRoute): NestedRegistry {
496
+ validateRouteShape(route);
497
+ try {
498
+ const parsed = JSON.parse(fs.readFileSync(registryPath(route), "utf-8")) as NestedRegistry;
499
+ return {
500
+ rootRunId: route.rootRunId,
501
+ updatedAt: typeof parsed.updatedAt === "number" ? parsed.updatedAt : 0,
502
+ children: Array.isArray(parsed.children) ? parsed.children.map((child) => sanitizeSummary(child)).filter((child): child is NestedRunSummary => Boolean(child)) : [],
503
+ processedEvents: Array.isArray(parsed.processedEvents) ? parsed.processedEvents.filter((item): item is string => typeof item === "string") : [],
504
+ };
505
+ } catch (error) {
506
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
507
+ return { rootRunId: route.rootRunId, updatedAt: 0, children: [], processedEvents: [] };
508
+ }
509
+ }
510
+
511
+ export function projectNestedEvents(route: NestedRoute): NestedRegistry {
512
+ validateRouteShape(route);
513
+ let registry = readNestedRegistry(route);
514
+ const seen = new Set(registry.processedEvents);
515
+ let changed = false;
516
+ let entries: string[] = [];
517
+ try {
518
+ entries = fs.readdirSync(route.eventSink).filter((entry) => entry.endsWith(".json") || entry.endsWith(".jsonl")).sort();
519
+ } catch (error) {
520
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
521
+ }
522
+ for (const entry of entries) {
523
+ if (seen.has(entry)) continue;
524
+ const eventPath = path.join(route.eventSink, entry);
525
+ if (!containedPath(route.eventSink, eventPath)) continue;
526
+ let content: string;
527
+ try {
528
+ const stat = fs.statSync(eventPath);
529
+ if (!stat.isFile() || stat.size > MAX_EVENT_BYTES) continue;
530
+ content = fs.readFileSync(eventPath, "utf-8");
531
+ } catch {
532
+ continue;
533
+ }
534
+ for (const event of parseNestedEventRecords(content, route)) {
535
+ registry = applyNestedEvent(registry, event);
536
+ changed = true;
537
+ }
538
+ seen.add(entry);
539
+ changed = true;
540
+ }
541
+ if (changed) {
542
+ registry = { ...registry, processedEvents: [...seen].slice(-1000) };
543
+ // Parent projection is the only writer to this sidecar registry. Child and
544
+ // runner processes only create immutable event files, so parent status.json
545
+ // remains owned by the existing runner writer and is never rewritten here.
546
+ writeAtomicJson(registryPath(route), registry);
547
+ }
548
+ return registry;
549
+ }
550
+
551
+ function writeRouteRecord(dir: string, ts: number, payload: object): string {
552
+ const content = `${JSON.stringify(payload)}\n`;
553
+ if (Buffer.byteLength(content, "utf-8") > MAX_EVENT_BYTES) throw new Error("Nested route record exceeds the maximum size.");
554
+ fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
555
+ const name = `${String(ts).padStart(13, "0")}-${randomUUID()}.json`;
556
+ const tmp = path.join(dir, `.${name}.tmp`);
557
+ const finalPath = path.join(dir, name);
558
+ fs.writeFileSync(tmp, content, { mode: 0o600 });
559
+ fs.renameSync(tmp, finalPath);
560
+ return finalPath;
561
+ }
562
+
563
+ export function writeNestedEvent(route: NestedRoute, event: Omit<NestedEventRecord, "rootRunId" | "capabilityToken">): void {
564
+ validateRouteShape(route);
565
+ const record: NestedEventRecord = {
566
+ ...event,
567
+ rootRunId: route.rootRunId,
568
+ capabilityToken: route.capabilityToken,
569
+ };
570
+ const sanitized = parseRecord(JSON.stringify(record), route);
571
+ if (!sanitized) throw new Error("Nested event record failed validation.");
572
+ writeRouteRecord(route.eventSink, sanitized.ts, sanitized);
573
+ }
574
+
575
+ function parseControlRequest(content: string, route: NestedRoute): NestedControlRequestRecord | undefined {
576
+ if (Buffer.byteLength(content, "utf-8") > MAX_EVENT_BYTES) return undefined;
577
+ let parsed: unknown;
578
+ try {
579
+ parsed = JSON.parse(content);
580
+ } catch {
581
+ return undefined;
582
+ }
583
+ if (!parsed || typeof parsed !== "object") return undefined;
584
+ const raw = parsed as Record<string, unknown>;
585
+ if (raw.type !== "subagent.nested.control-request") return undefined;
586
+ if (raw.rootRunId !== route.rootRunId || raw.capabilityToken !== route.capabilityToken) return undefined;
587
+ if (!isSafeNestedId(raw.requestId) || !isSafeNestedId(raw.targetRunId)) return undefined;
588
+ if (raw.action !== "interrupt" && raw.action !== "resume") return undefined;
589
+ const ts = clampNumber(raw.ts);
590
+ if (ts === undefined) return undefined;
591
+ return {
592
+ type: "subagent.nested.control-request",
593
+ ts,
594
+ rootRunId: route.rootRunId,
595
+ capabilityToken: route.capabilityToken,
596
+ requestId: raw.requestId,
597
+ targetRunId: raw.targetRunId,
598
+ action: raw.action,
599
+ ...(stringValue(raw.message, 16_000) ? { message: stringValue(raw.message, 16_000) } : {}),
600
+ };
601
+ }
602
+
603
+ function parseControlResult(content: string, route: NestedRoute): NestedControlResultRecord | undefined {
604
+ if (Buffer.byteLength(content, "utf-8") > MAX_EVENT_BYTES) return undefined;
605
+ let parsed: unknown;
606
+ try {
607
+ parsed = JSON.parse(content);
608
+ } catch {
609
+ return undefined;
610
+ }
611
+ if (!parsed || typeof parsed !== "object") return undefined;
612
+ const raw = parsed as Record<string, unknown>;
613
+ if (raw.type !== "subagent.nested.control-result") return undefined;
614
+ if (raw.rootRunId !== route.rootRunId || raw.capabilityToken !== route.capabilityToken) return undefined;
615
+ if (!isSafeNestedId(raw.requestId) || !isSafeNestedId(raw.targetRunId)) return undefined;
616
+ const ts = clampNumber(raw.ts);
617
+ if (ts === undefined || typeof raw.ok !== "boolean") return undefined;
618
+ return {
619
+ type: "subagent.nested.control-result",
620
+ ts,
621
+ rootRunId: route.rootRunId,
622
+ capabilityToken: route.capabilityToken,
623
+ requestId: raw.requestId,
624
+ targetRunId: raw.targetRunId,
625
+ ok: raw.ok,
626
+ message: stringValue(raw.message, 16_000) ?? (raw.ok ? "Control request completed." : "Control request failed."),
627
+ };
628
+ }
629
+
630
+ export function writeNestedControlRequest(route: NestedRoute, request: Omit<NestedControlRequestRecord, "type" | "rootRunId" | "capabilityToken">): string {
631
+ validateRouteShape(route);
632
+ assertSafeId("requestId", request.requestId);
633
+ assertSafeId("targetRunId", request.targetRunId);
634
+ const record: NestedControlRequestRecord = {
635
+ type: "subagent.nested.control-request",
636
+ ...request,
637
+ rootRunId: route.rootRunId,
638
+ capabilityToken: route.capabilityToken,
639
+ };
640
+ const sanitized = parseControlRequest(JSON.stringify(record), route);
641
+ if (!sanitized) throw new Error("Nested control request failed validation.");
642
+ return writeRouteRecord(route.controlInbox, sanitized.ts, sanitized);
643
+ }
644
+
645
+ export function readNestedControlRequests(route: NestedRoute): Array<NestedControlRequestRecord & { filePath: string }> {
646
+ validateRouteShape(route);
647
+ let entries: string[] = [];
648
+ try {
649
+ entries = fs.readdirSync(route.controlInbox).filter((entry) => entry.endsWith(".json")).sort();
650
+ } catch (error) {
651
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
652
+ }
653
+ const requests: Array<NestedControlRequestRecord & { filePath: string }> = [];
654
+ for (const entry of entries) {
655
+ const filePath = path.join(route.controlInbox, entry);
656
+ if (!containedPath(route.controlInbox, filePath)) continue;
657
+ try {
658
+ const stat = fs.statSync(filePath);
659
+ if (!stat.isFile() || stat.size > MAX_EVENT_BYTES) continue;
660
+ const request = parseControlRequest(fs.readFileSync(filePath, "utf-8"), route);
661
+ if (request) requests.push({ ...request, filePath });
662
+ } catch {
663
+ continue;
664
+ }
665
+ }
666
+ return requests;
667
+ }
668
+
669
+ export function writeNestedControlResult(route: NestedRoute, result: Omit<NestedControlResultRecord, "type" | "rootRunId" | "capabilityToken">): void {
670
+ validateRouteShape(route);
671
+ assertSafeId("requestId", result.requestId);
672
+ assertSafeId("targetRunId", result.targetRunId);
673
+ const record: NestedControlResultRecord = {
674
+ type: "subagent.nested.control-result",
675
+ ...result,
676
+ rootRunId: route.rootRunId,
677
+ capabilityToken: route.capabilityToken,
678
+ };
679
+ const sanitized = parseControlResult(JSON.stringify(record), route);
680
+ if (!sanitized) throw new Error("Nested control result failed validation.");
681
+ writeRouteRecord(route.eventSink, sanitized.ts, sanitized);
682
+ }
683
+
684
+ export function readNestedControlResults(route: NestedRoute): NestedControlResultRecord[] {
685
+ validateRouteShape(route);
686
+ let entries: string[] = [];
687
+ try {
688
+ entries = fs.readdirSync(route.eventSink).filter((entry) => entry.endsWith(".json") || entry.endsWith(".jsonl")).sort();
689
+ } catch (error) {
690
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
691
+ }
692
+ const results: NestedControlResultRecord[] = [];
693
+ for (const entry of entries) {
694
+ const eventPath = path.join(route.eventSink, entry);
695
+ if (!containedPath(route.eventSink, eventPath)) continue;
696
+ try {
697
+ const stat = fs.statSync(eventPath);
698
+ if (!stat.isFile() || stat.size > MAX_EVENT_BYTES) continue;
699
+ const content = fs.readFileSync(eventPath, "utf-8");
700
+ const lines = content.includes("\n") ? content.split("\n").filter((line) => line.trim()) : [content];
701
+ for (const line of lines) {
702
+ const result = parseControlResult(line, route);
703
+ if (result) results.push(result);
704
+ }
705
+ } catch {
706
+ continue;
707
+ }
708
+ }
709
+ return results;
710
+ }
711
+
712
+ export function nestedRouteEnv(route: NestedRoute): Record<string, string> {
713
+ return {
714
+ [SUBAGENT_PARENT_EVENT_SINK_ENV]: route.eventSink,
715
+ [SUBAGENT_PARENT_CONTROL_INBOX_ENV]: route.controlInbox,
716
+ [SUBAGENT_PARENT_ROOT_RUN_ID_ENV]: route.rootRunId,
717
+ [SUBAGENT_PARENT_CAPABILITY_TOKEN_ENV]: route.capabilityToken,
718
+ };
719
+ }
720
+
721
+ export function attachRootChildrenToSteps<T extends { children?: NestedRunSummary[]; index?: number }>(rootRunId: string, steps: T[] | undefined, children: NestedRunSummary[] | undefined): void {
722
+ if (!steps?.length) return;
723
+ for (const step of steps) {
724
+ step.children = undefined;
725
+ }
726
+ if (!children?.length) return;
727
+ for (const child of children) {
728
+ if (child.parentRunId !== rootRunId || child.parentStepIndex === undefined) continue;
729
+ const step = steps.find((candidate, index) => (candidate.index ?? index) === child.parentStepIndex);
730
+ if (!step) continue;
731
+ step.children ??= [];
732
+ step.children = [...step.children.filter((existing) => existing.id !== child.id), child].slice(0, MAX_CHILDREN);
733
+ }
734
+ }
735
+
736
+ export function updateAsyncJobNestedProjection(job: AsyncJobState): void {
737
+ if (!job.nestedRoute) return;
738
+ const registry = projectNestedEvents(job.nestedRoute);
739
+ job.nestedChildren = registry.children;
740
+ attachRootChildrenToSteps(job.asyncId, job.steps, registry.children);
741
+ }
742
+
743
+ export function updateForegroundNestedProjection(control: SubagentState["foregroundControls"] extends Map<string, infer T> ? T : never): void {
744
+ if (!control.nestedRoute) return;
745
+ const registry = projectNestedEvents(control.nestedRoute);
746
+ control.nestedChildren = registry.children;
747
+ }
748
+
749
+ export function hasLiveNestedDescendants(children: NestedRunSummary[] | undefined): boolean {
750
+ if (!children?.length) return false;
751
+ for (const child of children) {
752
+ if (!terminal(child.state)) return true;
753
+ if (hasLiveNestedDescendants(child.children)) return true;
754
+ if (hasLiveNestedDescendants(child.steps?.flatMap((step) => step.children ?? []))) return true;
755
+ }
756
+ return false;
757
+ }
758
+
759
+ export function nestedSummaryFromAsyncStatus(status: AsyncStatus, asyncDir: string, fallback: { id: string; parentRunId: string; parentStepIndex?: number; depth: number; path?: Array<{ runId: string; stepIndex?: number; agent?: string }>; mode?: SubagentRunMode; ts: number }): NestedRunSummary {
760
+ return {
761
+ id: status.runId || fallback.id,
762
+ parentRunId: fallback.parentRunId,
763
+ ...(fallback.parentStepIndex !== undefined ? { parentStepIndex: fallback.parentStepIndex } : {}),
764
+ depth: fallback.depth,
765
+ path: fallback.path ?? [{ runId: fallback.parentRunId, ...(fallback.parentStepIndex !== undefined ? { stepIndex: fallback.parentStepIndex } : {}) }],
766
+ asyncDir,
767
+ ...(status.pid ? { pid: status.pid } : {}),
768
+ ...(status.sessionId ? { sessionId: status.sessionId } : {}),
769
+ mode: status.mode ?? fallback.mode,
770
+ state: status.state,
771
+ ...(status.currentStep !== undefined ? { currentStep: status.currentStep } : {}),
772
+ ...(status.chainStepCount !== undefined ? { chainStepCount: status.chainStepCount } : {}),
773
+ ...(status.activityState ? { activityState: status.activityState } : {}),
774
+ ...(status.lastActivityAt !== undefined ? { lastActivityAt: status.lastActivityAt } : {}),
775
+ ...(status.currentTool ? { currentTool: status.currentTool } : {}),
776
+ ...(status.currentToolStartedAt !== undefined ? { currentToolStartedAt: status.currentToolStartedAt } : {}),
777
+ ...(status.currentPath ? { currentPath: status.currentPath } : {}),
778
+ ...(status.turnCount !== undefined ? { turnCount: status.turnCount } : {}),
779
+ ...(status.toolCount !== undefined ? { toolCount: status.toolCount } : {}),
780
+ ...(status.totalTokens ? { totalTokens: status.totalTokens } : {}),
781
+ ...(status.startedAt !== undefined ? { startedAt: status.startedAt } : { startedAt: fallback.ts }),
782
+ ...(status.endedAt !== undefined ? { endedAt: status.endedAt } : {}),
783
+ lastUpdate: status.lastUpdate ?? fallback.ts,
784
+ ...(status.sessionFile ? { sessionFile: status.sessionFile } : {}),
785
+ ...(status.steps?.length ? { steps: status.steps.map((step) => ({
786
+ agent: step.agent,
787
+ status: step.status,
788
+ ...(step.sessionFile ? { sessionFile: step.sessionFile } : {}),
789
+ ...(step.activityState ? { activityState: step.activityState } : {}),
790
+ ...(step.lastActivityAt !== undefined ? { lastActivityAt: step.lastActivityAt } : {}),
791
+ ...(step.currentTool ? { currentTool: step.currentTool } : {}),
792
+ ...(step.currentToolStartedAt !== undefined ? { currentToolStartedAt: step.currentToolStartedAt } : {}),
793
+ ...(step.currentPath ? { currentPath: step.currentPath } : {}),
794
+ ...(step.turnCount !== undefined ? { turnCount: step.turnCount } : {}),
795
+ ...(step.toolCount !== undefined ? { toolCount: step.toolCount } : {}),
796
+ ...(step.startedAt !== undefined ? { startedAt: step.startedAt } : {}),
797
+ ...(step.endedAt !== undefined ? { endedAt: step.endedAt } : {}),
798
+ ...(step.error ? { error: step.error } : {}),
799
+ })).slice(0, MAX_STEPS) } : {}),
800
+ };
801
+ }
802
+
803
+ export function nestedArtifactEnv(rootRunId: string, parentRunId: string): Record<string, string> {
804
+ return {
805
+ PI_SUBAGENT_NESTED_ROOT_RUN_ID: rootRunId,
806
+ PI_SUBAGENT_NESTED_PARENT_RUN_ID: parentRunId,
807
+ };
808
+ }
809
+
810
+ export function isTopLevelAsyncDir(asyncDir: string): boolean {
811
+ const resolved = path.resolve(asyncDir);
812
+ return containedPath(ASYNC_DIR, resolved) && !containedPath(path.join(TEMP_ROOT_DIR, "nested-subagent-runs"), resolved);
813
+ }
814
+
815
+ export function nestedResultsPath(rootRunId: string, id: string): string {
816
+ assertSafeId("rootRunId", rootRunId);
817
+ assertSafeId("id", id);
818
+ return path.join(RESULTS_DIR, "nested", rootRunId, `${id}.json`);
819
+ }