@pi-agents/orchid 0.1.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/LICENSE +21 -0
  3. package/README.md +246 -0
  4. package/agents/AGENTS-MANIFEST.md +42 -0
  5. package/agents/brain.md +42 -0
  6. package/agents/context-builder.md +46 -0
  7. package/agents/delegate.md +12 -0
  8. package/agents/dev-1.md +42 -0
  9. package/agents/oracle.md +73 -0
  10. package/agents/planner.md +55 -0
  11. package/agents/researcher.md +52 -0
  12. package/agents/reviewer.md +79 -0
  13. package/agents/scout.md +50 -0
  14. package/agents/tester.md +45 -0
  15. package/agents/worker.md +55 -0
  16. package/extensions/ralph.ts +1 -0
  17. package/extensions/reviewer-extension.ts +125 -0
  18. package/extensions/task-orchestrator.ts +28 -0
  19. package/package.json +63 -0
  20. package/prompts/gather-context-and-clarify.md +13 -0
  21. package/prompts/parallel-cleanup.md +59 -0
  22. package/prompts/parallel-context-build.md +53 -0
  23. package/prompts/parallel-handoff-plan.md +59 -0
  24. package/prompts/parallel-research.md +50 -0
  25. package/prompts/parallel-review.md +54 -0
  26. package/prompts/review-loop.md +41 -0
  27. package/skills/orchid/SKILL.md +214 -0
  28. package/skills/orchid/orchid-cleanup/SKILL.md +122 -0
  29. package/skills/orchid/orchid-converge/SKILL.md +124 -0
  30. package/skills/orchid/orchid-decompose/SKILL.md +201 -0
  31. package/skills/orchid/orchid-doctor/SKILL.md +162 -0
  32. package/skills/orchid/orchid-investigate/SKILL.md +102 -0
  33. package/skills/orchid/orchid-launch/SKILL.md +147 -0
  34. package/skills/ralph/SKILL.md +73 -0
  35. package/skills/subagents/pi-subagents/SKILL.md +813 -0
  36. package/src/index.ts +7 -0
  37. package/src/orchestrator/abort.ts +534 -0
  38. package/src/orchestrator/agent-bridge-extension.ts +1020 -0
  39. package/src/orchestrator/agent-host.ts +954 -0
  40. package/src/orchestrator/cleanup.ts +776 -0
  41. package/src/orchestrator/config-loader.ts +1412 -0
  42. package/src/orchestrator/config-schema.ts +690 -0
  43. package/src/orchestrator/config.ts +81 -0
  44. package/src/orchestrator/context-window.ts +66 -0
  45. package/src/orchestrator/diagnostic-reports.ts +475 -0
  46. package/src/orchestrator/diagnostics.ts +394 -0
  47. package/src/orchestrator/discovery.ts +1833 -0
  48. package/src/orchestrator/engine-worker.ts +415 -0
  49. package/src/orchestrator/engine.ts +5940 -0
  50. package/src/orchestrator/execution.ts +3104 -0
  51. package/src/orchestrator/extension.ts +5934 -0
  52. package/src/orchestrator/formatting.ts +785 -0
  53. package/src/orchestrator/git.ts +88 -0
  54. package/src/orchestrator/index.ts +28 -0
  55. package/src/orchestrator/lane-runner.ts +1787 -0
  56. package/src/orchestrator/mailbox.ts +780 -0
  57. package/src/orchestrator/merge.ts +3414 -0
  58. package/src/orchestrator/messages.ts +1062 -0
  59. package/src/orchestrator/migrations.ts +278 -0
  60. package/src/orchestrator/naming.ts +117 -0
  61. package/src/orchestrator/path-resolver.ts +275 -0
  62. package/src/orchestrator/persistence.ts +2625 -0
  63. package/src/orchestrator/process-registry.ts +452 -0
  64. package/src/orchestrator/quality-gate.ts +1085 -0
  65. package/src/orchestrator/resume.ts +3488 -0
  66. package/src/orchestrator/sessions.ts +57 -0
  67. package/src/orchestrator/settings-loader.ts +136 -0
  68. package/src/orchestrator/settings-tui.ts +2208 -0
  69. package/src/orchestrator/sidecar-telemetry.ts +267 -0
  70. package/src/orchestrator/supervisor.ts +4548 -0
  71. package/src/orchestrator/task-executor-core.ts +675 -0
  72. package/src/orchestrator/tmux-compat.ts +37 -0
  73. package/src/orchestrator/tool-allowlist-constants.ts +37 -0
  74. package/src/orchestrator/types.ts +4465 -0
  75. package/src/orchestrator/verification.ts +547 -0
  76. package/src/orchestrator/waves.ts +1564 -0
  77. package/src/orchestrator/workspace.ts +707 -0
  78. package/src/orchestrator/worktree.ts +2725 -0
  79. package/src/ralph/index.ts +825 -0
  80. package/src/subagents/agents/agent-management.ts +648 -0
  81. package/src/subagents/agents/agent-scope.ts +6 -0
  82. package/src/subagents/agents/agent-selection.ts +23 -0
  83. package/src/subagents/agents/agent-serializer.ts +86 -0
  84. package/src/subagents/agents/agents.ts +832 -0
  85. package/src/subagents/agents/chain-serializer.ts +137 -0
  86. package/src/subagents/agents/frontmatter.ts +29 -0
  87. package/src/subagents/agents/identity.ts +30 -0
  88. package/src/subagents/agents/skills.ts +632 -0
  89. package/src/subagents/extension/config.ts +16 -0
  90. package/src/subagents/extension/control-notices.ts +92 -0
  91. package/src/subagents/extension/doctor.ts +199 -0
  92. package/src/subagents/extension/fanout-child.ts +170 -0
  93. package/src/subagents/extension/index.ts +573 -0
  94. package/src/subagents/extension/schemas.ts +168 -0
  95. package/src/subagents/intercom/intercom-bridge.ts +379 -0
  96. package/src/subagents/intercom/result-intercom.ts +377 -0
  97. package/src/subagents/runs/background/async-execution.ts +712 -0
  98. package/src/subagents/runs/background/async-job-tracker.ts +310 -0
  99. package/src/subagents/runs/background/async-resume.ts +345 -0
  100. package/src/subagents/runs/background/async-status.ts +325 -0
  101. package/src/subagents/runs/background/completion-dedupe.ts +63 -0
  102. package/src/subagents/runs/background/notify.ts +108 -0
  103. package/src/subagents/runs/background/parallel-groups.ts +45 -0
  104. package/src/subagents/runs/background/result-watcher.ts +307 -0
  105. package/src/subagents/runs/background/run-id-resolver.ts +83 -0
  106. package/src/subagents/runs/background/run-status.ts +269 -0
  107. package/src/subagents/runs/background/stale-run-reconciler.ts +336 -0
  108. package/src/subagents/runs/background/subagent-runner.ts +1808 -0
  109. package/src/subagents/runs/background/top-level-async.ts +13 -0
  110. package/src/subagents/runs/foreground/chain-clarify.ts +1333 -0
  111. package/src/subagents/runs/foreground/chain-execution.ts +938 -0
  112. package/src/subagents/runs/foreground/execution.ts +918 -0
  113. package/src/subagents/runs/foreground/subagent-executor.ts +2527 -0
  114. package/src/subagents/runs/shared/completion-guard.ts +147 -0
  115. package/src/subagents/runs/shared/long-running-guard.ts +175 -0
  116. package/src/subagents/runs/shared/mcp-direct-tool-allowlist.ts +365 -0
  117. package/src/subagents/runs/shared/model-fallback.ts +103 -0
  118. package/src/subagents/runs/shared/nested-events.ts +819 -0
  119. package/src/subagents/runs/shared/nested-path.ts +52 -0
  120. package/src/subagents/runs/shared/nested-render.ts +115 -0
  121. package/src/subagents/runs/shared/parallel-utils.ts +109 -0
  122. package/src/subagents/runs/shared/pi-args.ts +220 -0
  123. package/src/subagents/runs/shared/pi-spawn.ts +115 -0
  124. package/src/subagents/runs/shared/run-history.ts +60 -0
  125. package/src/subagents/runs/shared/single-output.ts +164 -0
  126. package/src/subagents/runs/shared/subagent-control.ts +226 -0
  127. package/src/subagents/runs/shared/subagent-prompt-runtime.ts +170 -0
  128. package/src/subagents/runs/shared/worktree.ts +577 -0
  129. package/src/subagents/shared/artifacts.ts +98 -0
  130. package/src/subagents/shared/atomic-json.ts +16 -0
  131. package/src/subagents/shared/file-coalescer.ts +40 -0
  132. package/src/subagents/shared/fork-context.ts +76 -0
  133. package/src/subagents/shared/formatters.ts +133 -0
  134. package/src/subagents/shared/jsonl-writer.ts +81 -0
  135. package/src/subagents/shared/model-info.ts +78 -0
  136. package/src/subagents/shared/post-exit-stdio-guard.ts +85 -0
  137. package/src/subagents/shared/session-identity.ts +10 -0
  138. package/src/subagents/shared/session-tokens.ts +44 -0
  139. package/src/subagents/shared/settings.ts +397 -0
  140. package/src/subagents/shared/status-format.ts +49 -0
  141. package/src/subagents/shared/types.ts +822 -0
  142. package/src/subagents/shared/utils.ts +450 -0
  143. package/src/subagents/slash/prompt-template-bridge.ts +397 -0
  144. package/src/subagents/slash/slash-bridge.ts +174 -0
  145. package/src/subagents/slash/slash-commands.ts +528 -0
  146. package/src/subagents/slash/slash-live-state.ts +292 -0
  147. package/src/subagents/tui/render-helpers.ts +80 -0
  148. package/src/subagents/tui/render.ts +1358 -0
  149. package/templates/agents/local/supervisor.md +33 -0
  150. package/templates/agents/local/task-merger.md +27 -0
  151. package/templates/agents/local/task-reviewer.md +30 -0
  152. package/templates/agents/local/task-worker.md +34 -0
  153. package/templates/agents/supervisor-routing.md +92 -0
  154. package/templates/agents/supervisor.md +229 -0
  155. package/templates/agents/task-merger.md +214 -0
  156. package/templates/agents/task-reviewer.md +260 -0
  157. package/templates/agents/task-worker-segment.md +44 -0
  158. package/templates/agents/task-worker.md +557 -0
  159. package/templates/tasks/CONTEXT.md +30 -0
  160. package/templates/tasks/EXAMPLE-001-hello-world/PROMPT.md +98 -0
  161. package/templates/tasks/EXAMPLE-001-hello-world/STATUS.md +73 -0
  162. package/templates/tasks/EXAMPLE-002-parallel-smoke/PROMPT.md +97 -0
  163. package/templates/tasks/EXAMPLE-002-parallel-smoke/STATUS.md +73 -0
@@ -0,0 +1,307 @@
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import { buildCompletionKey, markSeenWithTtl } from "./completion-dedupe.ts";
4
+ import { createFileCoalescer } from "../../shared/file-coalescer.ts";
5
+ import {
6
+ SUBAGENT_ASYNC_COMPLETE_EVENT,
7
+ type IntercomEventBus,
8
+ type NestedRunSummary,
9
+ type SubagentResultIntercomChild,
10
+ type SubagentState,
11
+ } from "../../shared/types.ts";
12
+ import {
13
+ attachNestedChildrenToResultChildren,
14
+ buildSubagentResultIntercomPayload,
15
+ compactNestedResultChildren,
16
+ deliverSubagentResultIntercomEvent,
17
+ resolveSubagentResultStatus,
18
+ } from "../../intercom/result-intercom.ts";
19
+ import { projectNestedRegistryForRoot, sanitizeSummary } from "../shared/nested-events.ts";
20
+
21
+ const WATCHER_RESTART_DELAY_MS = 3000;
22
+ const POLL_INTERVAL_MS = 3000;
23
+
24
+ type ResultWatcherFs = Pick<typeof fs, "existsSync" | "readFileSync" | "unlinkSync" | "readdirSync" | "mkdirSync" | "watch">;
25
+
26
+ type ResultWatcherTimers = {
27
+ setTimeout: typeof setTimeout;
28
+ clearTimeout: typeof clearTimeout;
29
+ setInterval: typeof setInterval;
30
+ clearInterval: typeof clearInterval;
31
+ };
32
+
33
+ type ResultWatcherDeps = {
34
+ fs?: ResultWatcherFs;
35
+ timers?: ResultWatcherTimers;
36
+ };
37
+
38
+ type ResultFileChild = {
39
+ agent?: string;
40
+ output?: string;
41
+ error?: string;
42
+ success?: boolean;
43
+ sessionFile?: string;
44
+ artifactPaths?: { outputPath?: string };
45
+ intercomTarget?: string;
46
+ children?: unknown;
47
+ };
48
+
49
+ type ResultFileData = {
50
+ id?: string;
51
+ runId?: string;
52
+ agent?: string;
53
+ success?: boolean;
54
+ state?: string;
55
+ mode?: string;
56
+ summary?: string;
57
+ results?: ResultFileChild[];
58
+ nestedChildren?: unknown;
59
+ sessionId?: string;
60
+ cwd?: string;
61
+ sessionFile?: string;
62
+ asyncDir?: string;
63
+ intercomTarget?: string;
64
+ };
65
+
66
+ function sanitizeNestedResultChildren(value: unknown, resultPath: string, label: string): NestedRunSummary[] | undefined {
67
+ if (value === undefined) return undefined;
68
+ if (!Array.isArray(value)) {
69
+ console.error(`Ignoring invalid nested children in subagent result file '${resultPath}' at ${label}: expected an array.`);
70
+ return undefined;
71
+ }
72
+ const children = value.map((child) => sanitizeSummary(child)).filter((child): child is NestedRunSummary => Boolean(child));
73
+ if (children.length !== value.length) {
74
+ console.error(`Ignoring ${value.length - children.length} invalid nested child record(s) in subagent result file '${resultPath}' at ${label}.`);
75
+ }
76
+ return children.length ? children : undefined;
77
+ }
78
+
79
+ function getErrorCode(error: unknown): string | undefined {
80
+ return typeof error === "object" && error !== null && "code" in error
81
+ ? (error as NodeJS.ErrnoException).code
82
+ : undefined;
83
+ }
84
+
85
+ function isNotFoundError(error: unknown): boolean {
86
+ return getErrorCode(error) === "ENOENT";
87
+ }
88
+
89
+ function shouldFallBackToPolling(error: unknown): boolean {
90
+ const code = getErrorCode(error);
91
+ return code === "EMFILE" || code === "ENOSPC";
92
+ }
93
+
94
+ export function createResultWatcher(
95
+ pi: { events: IntercomEventBus },
96
+ state: SubagentState,
97
+ resultsDir: string,
98
+ completionTtlMs: number,
99
+ deps: ResultWatcherDeps = {},
100
+ ): {
101
+ startResultWatcher: () => void;
102
+ primeExistingResults: () => void;
103
+ stopResultWatcher: () => void;
104
+ } {
105
+ const fsApi = deps.fs ?? fs;
106
+ const timers = deps.timers ?? { setTimeout, clearTimeout, setInterval, clearInterval };
107
+
108
+ const handleResult = async (file: string) => {
109
+ const resultPath = path.join(resultsDir, file);
110
+ if (!fsApi.existsSync(resultPath)) return;
111
+ try {
112
+ const data = JSON.parse(fsApi.readFileSync(resultPath, "utf-8")) as ResultFileData;
113
+ if (data.sessionId && data.sessionId !== state.currentSessionId) return;
114
+ if (!data.sessionId && data.cwd && (!state.baseCwd || data.cwd !== state.baseCwd)) return;
115
+
116
+ const runId = data.runId ?? data.id ?? file.replace(/\.json$/i, "");
117
+ const hasExplicitNestedChildren = data.nestedChildren !== undefined;
118
+ let nestedChildren = compactNestedResultChildren(sanitizeNestedResultChildren(data.nestedChildren, resultPath, "nestedChildren"));
119
+ if (!nestedChildren?.length && !hasExplicitNestedChildren) {
120
+ try {
121
+ nestedChildren = compactNestedResultChildren(projectNestedRegistryForRoot(runId)?.children);
122
+ } catch (error) {
123
+ console.error(`Failed to enrich subagent result file '${resultPath}' with nested registry children; will retry later:`, error);
124
+ return;
125
+ }
126
+ }
127
+ const now = Date.now();
128
+ const completionKey = buildCompletionKey(data, `result:${file}`);
129
+ if (markSeenWithTtl(state.completionSeen, completionKey, now, completionTtlMs)) {
130
+ fsApi.unlinkSync(resultPath);
131
+ return;
132
+ }
133
+
134
+ const hasResultChildren = Array.isArray(data.results) && data.results.length > 0;
135
+ const resultChildren = hasResultChildren
136
+ ? data.results!
137
+ : [{
138
+ agent: data.agent,
139
+ output: data.summary,
140
+ success: data.success,
141
+ }];
142
+ const normalizedChildren = attachNestedChildrenToResultChildren(runId, resultChildren.map((result = {}, index): SubagentResultIntercomChild => {
143
+ const baseOutput = result.output ?? data.summary;
144
+ const hasRealOutput = typeof baseOutput === "string" && baseOutput.trim().length > 0;
145
+ const output = hasRealOutput ? baseOutput : "(no output)";
146
+ const summary = result.success === false && result.error
147
+ ? `${result.error}${hasRealOutput ? `\n\nOutput:\n${baseOutput}` : ""}`
148
+ : output;
149
+ const sessionPath = result.sessionFile ?? (resultChildren.length === 1 ? data.sessionFile : undefined);
150
+ const childNestedChildren = sanitizeNestedResultChildren(result.children, resultPath, `results[${index}].children`);
151
+ return {
152
+ agent: result.agent ?? data.agent ?? `step-${index + 1}`,
153
+ status: resolveSubagentResultStatus({
154
+ success: result.success,
155
+ state: data.state === "paused" || typeof result.success !== "boolean" ? data.state : undefined,
156
+ }),
157
+ summary,
158
+ index,
159
+ artifactPath: result.artifactPaths?.outputPath,
160
+ ...(typeof sessionPath === "string" && fsApi.existsSync(sessionPath) ? { sessionPath } : {}),
161
+ ...(result.intercomTarget ? { intercomTarget: result.intercomTarget } : {}),
162
+ ...(childNestedChildren ? { children: childNestedChildren } : {}),
163
+ };
164
+ }), nestedChildren);
165
+
166
+ const intercomTarget = data.intercomTarget?.trim();
167
+ if (intercomTarget) {
168
+ const mode = data.mode === "single" || data.mode === "parallel" || data.mode === "chain"
169
+ ? data.mode
170
+ : resultChildren.length > 1 ? "chain" : "single";
171
+ const payload = buildSubagentResultIntercomPayload({
172
+ to: intercomTarget,
173
+ runId,
174
+ mode,
175
+ source: "async",
176
+ children: normalizedChildren,
177
+ asyncId: data.id,
178
+ asyncDir: data.asyncDir,
179
+ });
180
+ const delivered = await deliverSubagentResultIntercomEvent(pi.events, payload);
181
+ if (!delivered) {
182
+ console.error(`Subagent async grouped result intercom delivery was not acknowledged for '${resultPath}'.`);
183
+ }
184
+ }
185
+
186
+ pi.events.emit(SUBAGENT_ASYNC_COMPLETE_EVENT, {
187
+ ...data,
188
+ runId,
189
+ ...(nestedChildren?.length ? { nestedChildren } : {}),
190
+ ...(Array.isArray(data.results) ? {
191
+ results: hasResultChildren
192
+ ? normalizedChildren.map((child, index) => ({
193
+ ...data.results![index],
194
+ agent: child.agent,
195
+ status: child.status,
196
+ summary: child.summary,
197
+ index: child.index,
198
+ artifactPath: child.artifactPath,
199
+ sessionPath: child.sessionPath,
200
+ children: child.children,
201
+ }))
202
+ : [],
203
+ } : {}),
204
+ });
205
+ fsApi.unlinkSync(resultPath);
206
+ } catch (error) {
207
+ if (isNotFoundError(error)) return;
208
+ console.error(`Failed to process subagent result file '${resultPath}':`, error);
209
+ }
210
+ };
211
+
212
+ state.resultFileCoalescer = createFileCoalescer((file) => {
213
+ void handleResult(file);
214
+ }, 50);
215
+
216
+ const primeExistingResults = () => {
217
+ try {
218
+ fsApi.readdirSync(resultsDir)
219
+ .filter((f) => f.endsWith(".json"))
220
+ .forEach((file) => state.resultFileCoalescer.schedule(file, 0));
221
+ } catch (error) {
222
+ if (isNotFoundError(error)) return;
223
+ console.error(`Failed to scan subagent result directory '${resultsDir}':`, error);
224
+ }
225
+ };
226
+
227
+ const startPollingFallback = (reason: unknown) => {
228
+ state.watcher?.close();
229
+ state.watcher = null;
230
+ if (state.watcherRestartTimer) return;
231
+
232
+ console.error(
233
+ `Subagent result watcher for '${resultsDir}' fell back to polling because native fs.watch is unavailable (${getErrorCode(reason) ?? "unknown error"}).`,
234
+ );
235
+ primeExistingResults();
236
+ state.watcherRestartTimer = timers.setInterval(primeExistingResults, POLL_INTERVAL_MS);
237
+ state.watcherRestartTimer.unref?.();
238
+ };
239
+
240
+ const scheduleRestart = () => {
241
+ if (state.watcherRestartTimer) return;
242
+ state.watcherRestartTimer = timers.setTimeout(() => {
243
+ state.watcherRestartTimer = null;
244
+ try {
245
+ fsApi.mkdirSync(resultsDir, { recursive: true });
246
+ startResultWatcher();
247
+ } catch (error) {
248
+ if (shouldFallBackToPolling(error)) {
249
+ startPollingFallback(error);
250
+ return;
251
+ }
252
+ console.error(`Failed to restart subagent result watcher for '${resultsDir}':`, error);
253
+ scheduleRestart();
254
+ }
255
+ }, WATCHER_RESTART_DELAY_MS);
256
+ state.watcherRestartTimer.unref?.();
257
+ };
258
+
259
+ const startResultWatcher = () => {
260
+ if (state.watcher) return;
261
+ if (state.watcherRestartTimer) {
262
+ timers.clearTimeout(state.watcherRestartTimer);
263
+ timers.clearInterval(state.watcherRestartTimer);
264
+ state.watcherRestartTimer = null;
265
+ }
266
+ try {
267
+ state.watcher = fsApi.watch(resultsDir, (ev, file) => {
268
+ if (ev !== "rename" || !file) return;
269
+ const fileName = file.toString();
270
+ if (!fileName.endsWith(".json")) return;
271
+ state.resultFileCoalescer.schedule(fileName);
272
+ });
273
+ state.watcher.on("error", (error) => {
274
+ if (shouldFallBackToPolling(error)) {
275
+ startPollingFallback(error);
276
+ return;
277
+ }
278
+ console.error(`Subagent result watcher failed for '${resultsDir}':`, error);
279
+ state.watcher?.close();
280
+ state.watcher = null;
281
+ scheduleRestart();
282
+ });
283
+ state.watcher.unref?.();
284
+ } catch (error) {
285
+ if (shouldFallBackToPolling(error)) {
286
+ startPollingFallback(error);
287
+ return;
288
+ }
289
+ console.error(`Failed to start subagent result watcher for '${resultsDir}':`, error);
290
+ state.watcher = null;
291
+ scheduleRestart();
292
+ }
293
+ };
294
+
295
+ const stopResultWatcher = () => {
296
+ state.watcher?.close();
297
+ state.watcher = null;
298
+ if (state.watcherRestartTimer) {
299
+ timers.clearTimeout(state.watcherRestartTimer);
300
+ timers.clearInterval(state.watcherRestartTimer);
301
+ }
302
+ state.watcherRestartTimer = null;
303
+ state.resultFileCoalescer.clear();
304
+ };
305
+
306
+ return { startResultWatcher, primeExistingResults, stopResultWatcher };
307
+ }
@@ -0,0 +1,83 @@
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import { ASYNC_DIR, RESULTS_DIR, type SubagentState } from "../../shared/types.ts";
4
+ import { findAsyncRunPrefixMatches, type AsyncRunLocation } from "./async-resume.ts";
5
+ import { assertSafeNestedId, findNestedRunMatchesById, type NestedRoute, type NestedRunMatch, type NestedRunResolutionScope } from "../shared/nested-events.ts";
6
+
7
+ export type ResolvedSubagentRunId =
8
+ | { kind: "foreground"; id: string }
9
+ | { kind: "async"; id: string; location: AsyncRunLocation }
10
+ | { kind: "nested"; id: string; match: NestedRunMatch };
11
+
12
+ export interface ResolveSubagentRunIdDeps {
13
+ state?: SubagentState;
14
+ asyncDirRoot?: string;
15
+ resultsDir?: string;
16
+ nested?: NestedRunResolutionScope;
17
+ }
18
+
19
+ function exactAsyncLocation(id: string, asyncDirRoot: string, resultsDir: string): AsyncRunLocation | undefined {
20
+ const asyncDir = path.join(asyncDirRoot, id);
21
+ const resultPath = path.join(resultsDir, `${id}.json`);
22
+ if (!fs.existsSync(asyncDir) && !fs.existsSync(resultPath)) return undefined;
23
+ return {
24
+ asyncDir: fs.existsSync(asyncDir) ? asyncDir : null,
25
+ resultPath: fs.existsSync(resultPath) ? resultPath : null,
26
+ resolvedId: id,
27
+ };
28
+ }
29
+
30
+ function foregroundIds(state: SubagentState | undefined): string[] {
31
+ return state ? [...state.foregroundControls.keys()] : [];
32
+ }
33
+
34
+ function nestedScopeFromState(state: SubagentState | undefined): NestedRunResolutionScope | undefined {
35
+ if (!state) return undefined;
36
+ const routes: NestedRoute[] = [];
37
+ const seen = new Set<string>();
38
+ const add = (route: NestedRoute | undefined) => {
39
+ if (!route) return;
40
+ const key = `${route.rootRunId}:${route.eventSink}:${route.controlInbox}`;
41
+ if (seen.has(key)) return;
42
+ seen.add(key);
43
+ routes.push(route);
44
+ };
45
+ for (const control of state.foregroundControls.values()) add(control.nestedRoute as NestedRoute | undefined);
46
+ for (const job of state.asyncJobs.values()) add(job.nestedRoute as NestedRoute | undefined);
47
+ return { routes };
48
+ }
49
+
50
+ function asyncPrefixMatches(prefix: string, asyncDirRoot: string, resultsDir: string): Array<{ id: string; location: AsyncRunLocation }> {
51
+ return findAsyncRunPrefixMatches(prefix, asyncDirRoot, resultsDir);
52
+ }
53
+
54
+ export function resolveSubagentRunId(id: string, deps: ResolveSubagentRunIdDeps = {}): ResolvedSubagentRunId | undefined {
55
+ assertSafeNestedId("id", id);
56
+ const asyncDirRoot = deps.asyncDirRoot ?? ASYNC_DIR;
57
+ const resultsDir = deps.resultsDir ?? RESULTS_DIR;
58
+
59
+ const nestedScope = deps.nested ?? nestedScopeFromState(deps.state);
60
+ if (deps.state?.foregroundControls.has(id)) return { kind: "foreground", id };
61
+ const exactAsync = exactAsyncLocation(id, asyncDirRoot, resultsDir);
62
+ if (exactAsync) return { kind: "async", id, location: exactAsync };
63
+ const exactNested = findNestedRunMatchesById(id, nestedScope ? { scope: nestedScope } : {});
64
+ if (exactNested.length > 1) throw new Error(`Nested run id '${id}' is ambiguous across authorized registries. Provide the full id after stale registries are cleaned up.`);
65
+ if (exactNested[0]) return { kind: "nested", id, match: exactNested[0] };
66
+
67
+ const matches: ResolvedSubagentRunId[] = [];
68
+ for (const foregroundId of foregroundIds(deps.state).filter((candidate) => candidate.startsWith(id))) {
69
+ matches.push({ kind: "foreground", id: foregroundId });
70
+ }
71
+ for (const match of asyncPrefixMatches(id, asyncDirRoot, resultsDir)) {
72
+ matches.push({ kind: "async", id: match.id, location: match.location });
73
+ }
74
+ for (const match of findNestedRunMatchesById(id, nestedScope ? { prefix: true, scope: nestedScope } : { prefix: true })) {
75
+ matches.push({ kind: "nested", id: match.run.id, match });
76
+ }
77
+ const unique = new Map(matches.map((match) => [`${match.kind}:${match.id}`, match]));
78
+ const values = [...unique.values()];
79
+ if (values.length > 1) {
80
+ throw new Error(`Ambiguous subagent run id prefix '${id}' matched: ${values.map((match) => `${match.kind}:${match.id}`).join(", ")}. Provide a longer id.`);
81
+ }
82
+ return values[0];
83
+ }
@@ -0,0 +1,269 @@
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import type { AgentToolResult } from "@earendil-works/pi-agent-core";
4
+ import { formatAsyncRunList, formatAsyncRunOutputPath, formatAsyncRunProgressLabel, listAsyncRuns } from "./async-status.ts";
5
+ import { formatNestedRunStatusLines } from "../shared/nested-render.ts";
6
+ import { formatModelThinking } from "../../shared/formatters.ts";
7
+ import { formatActivityLabel } from "../../shared/status-format.ts";
8
+ import { ASYNC_DIR, RESULTS_DIR, type AsyncStatus, type Details, type NestedRunSummary, type SubagentState } from "../../shared/types.ts";
9
+ import { resolveSubagentIntercomTarget } from "../../intercom/intercom-bridge.ts";
10
+ import { resolveAsyncRunLocation } from "./async-resume.ts";
11
+ import { resolveSubagentRunId } from "./run-id-resolver.ts";
12
+ import { flatToLogicalStepIndex, normalizeParallelGroups } from "./parallel-groups.ts";
13
+ import { reconcileAsyncRun, reconcileNestedAsyncDescendants } from "./stale-run-reconciler.ts";
14
+ import { attachRootChildrenToSteps, findNestedRouteForRootId, projectNestedRegistryForRoot, type NestedRunResolutionScope } from "../shared/nested-events.ts";
15
+
16
+ interface RunStatusParams {
17
+ action?: "status";
18
+ id?: string;
19
+ runId?: string;
20
+ dir?: string;
21
+ }
22
+
23
+ interface RunStatusDeps {
24
+ asyncDirRoot?: string;
25
+ resultsDir?: string;
26
+ kill?: (pid: number, signal?: NodeJS.Signals | 0) => boolean;
27
+ now?: () => number;
28
+ state?: SubagentState;
29
+ nested?: NestedRunResolutionScope;
30
+ }
31
+
32
+ function hasExistingSessionFile(value: unknown): value is string {
33
+ return typeof value === "string" && fs.existsSync(value);
34
+ }
35
+
36
+ function formatResumeGuidance(runId: string | undefined, children: Array<{ agent?: unknown; sessionFile?: unknown }>, fallbackSessionFile?: unknown): string {
37
+ const knownChildren = children
38
+ .map((child, index) => ({ child, index }))
39
+ .filter(({ child }) => typeof child.agent === "string");
40
+ if (!runId || knownChildren.length === 0) return "Resume: unavailable; no child session file was persisted.";
41
+ const singleSessionFile = knownChildren[0]?.child.sessionFile ?? fallbackSessionFile;
42
+ if (children.length === 1 && knownChildren.length === 1 && hasExistingSessionFile(singleSessionFile)) {
43
+ return `Revive: subagent({ action: "resume", id: "${runId}", message: "..." })`;
44
+ }
45
+ const childWithSession = knownChildren.find(({ child }) => hasExistingSessionFile(child.sessionFile));
46
+ if (childWithSession) {
47
+ return `Revive child: subagent({ action: "resume", id: "${runId}", index: ${childWithSession.index}, message: "..." })`;
48
+ }
49
+ return "Resume: unavailable; no child session file was persisted.";
50
+ }
51
+
52
+ function stepLineLabel(status: AsyncStatus, index: number): string {
53
+ const steps = status.steps ?? [];
54
+ if (status.mode === "parallel") return `Agent ${index + 1}/${steps.length || 1}`;
55
+ if (status.mode === "chain") {
56
+ const chainStepCount = status.chainStepCount ?? (steps.length || 1);
57
+ const groups = normalizeParallelGroups(status.parallelGroups, steps.length, chainStepCount);
58
+ const group = groups.find((candidate) => index >= candidate.start && index < candidate.start + candidate.count);
59
+ if (group) return `Step ${group.stepIndex + 1}/${chainStepCount} Agent ${index - group.start + 1}/${group.count}`;
60
+ return `Step ${flatToLogicalStepIndex(index, chainStepCount, groups) + 1}/${chainStepCount}`;
61
+ }
62
+ return `Step ${index + 1}`;
63
+ }
64
+
65
+ function nestedRunDisplayName(run: NestedRunSummary): string {
66
+ if (run.agent) return run.agent;
67
+ if (run.agents?.length) return run.agents.join(", ");
68
+ return run.id;
69
+ }
70
+
71
+ function formatNestedExactStatus(rootRunId: string, run: NestedRunSummary): string {
72
+ const lines = [
73
+ `Nested run: ${run.id}`,
74
+ `Root: ${rootRunId}`,
75
+ `Parent: ${run.parentRunId}${run.parentStepIndex !== undefined ? ` step ${run.parentStepIndex + 1}` : ""}`,
76
+ `State: ${run.state}`,
77
+ run.activityState || run.lastActivityAt ? `Activity: ${formatActivityLabel(run.lastActivityAt, run.activityState)}` : undefined,
78
+ run.mode ? `Mode: ${run.mode}` : undefined,
79
+ `Agent: ${nestedRunDisplayName(run)}`,
80
+ run.currentStep !== undefined ? `Progress: step ${run.currentStep + 1}/${run.chainStepCount ?? run.steps?.length ?? 1}` : undefined,
81
+ run.asyncDir ? `Dir: ${run.asyncDir}` : undefined,
82
+ run.sessionFile ? `Session: ${run.sessionFile}` : undefined,
83
+ run.error ? `Error: ${run.error}` : undefined,
84
+ ].filter((line): line is string => Boolean(line));
85
+ if (run.path.length) {
86
+ lines.push(`Path: ${run.path.map((part) => `${part.runId}${part.stepIndex !== undefined ? `:${part.stepIndex + 1}` : ""}${part.agent ? `:${part.agent}` : ""}`).join(" > ")} > ${run.id}`);
87
+ }
88
+ if (run.steps?.length) {
89
+ lines.push("Steps:");
90
+ for (const [index, step] of run.steps.entries()) {
91
+ const activity = step.status === "running" ? formatActivityLabel(step.lastActivityAt, step.activityState) : undefined;
92
+ lines.push(` ${index + 1}. ${step.agent} ${step.status}${activity ? `, ${activity}` : ""}${step.error ? `, error: ${step.error}` : ""}`);
93
+ lines.push(...formatNestedRunStatusLines(step.children, { indent: " ", commandHints: true }));
94
+ }
95
+ }
96
+ lines.push(...formatNestedRunStatusLines(run.children, { indent: " ", commandHints: true }));
97
+ lines.push("Commands:", ` Status: subagent({ action: "status", id: "${run.id}" })`, ` Interrupt: subagent({ action: "interrupt", id: "${run.id}" })`, ` Resume: subagent({ action: "resume", id: "${run.id}", message: "..." })`, ` Root status: subagent({ action: "status", id: "${rootRunId}" })`);
98
+ return lines.join("\n");
99
+ }
100
+
101
+ export function inspectSubagentStatus(params: RunStatusParams, deps: RunStatusDeps = {}): AgentToolResult<Details> {
102
+ const asyncDirRoot = deps.asyncDirRoot ?? ASYNC_DIR;
103
+ const resultsDir = deps.resultsDir ?? RESULTS_DIR;
104
+ if (!params.id && !params.runId && !params.dir) {
105
+ if (deps.nested) {
106
+ return {
107
+ content: [{ type: "text", text: "Child-safe subagent status requires an id when no foreground run is active." }],
108
+ isError: true,
109
+ details: { mode: "single", results: [] },
110
+ };
111
+ }
112
+ try {
113
+ const runs = listAsyncRuns(asyncDirRoot, { states: ["queued", "running"], resultsDir, kill: deps.kill, now: deps.now });
114
+ return {
115
+ content: [{ type: "text", text: formatAsyncRunList(runs) }],
116
+ details: { mode: "single", results: [] },
117
+ };
118
+ } catch (error) {
119
+ const message = error instanceof Error ? error.message : String(error);
120
+ return {
121
+ content: [{ type: "text", text: message }],
122
+ isError: true,
123
+ details: { mode: "single", results: [] },
124
+ };
125
+ }
126
+ }
127
+
128
+ let location;
129
+ try {
130
+ const requestedId = params.id ?? params.runId;
131
+ if (!params.dir && requestedId) {
132
+ const resolved = resolveSubagentRunId(requestedId, { asyncDirRoot, resultsDir, state: deps.state, nested: deps.nested });
133
+ if (resolved?.kind === "nested") {
134
+ reconcileNestedAsyncDescendants(resolved.match.route, { resultsDir, kill: deps.kill, now: deps.now });
135
+ const refreshed = resolveSubagentRunId(requestedId, { asyncDirRoot, resultsDir, state: deps.state, nested: deps.nested });
136
+ const nested = refreshed?.kind === "nested" ? refreshed : resolved;
137
+ return { content: [{ type: "text", text: formatNestedExactStatus(nested.match.rootRunId, nested.match.run) }], details: { mode: "single", results: [] } };
138
+ }
139
+ if (resolved?.kind === "async") location = resolved.location;
140
+ else location = { asyncDir: null, resultPath: null, resolvedId: requestedId };
141
+ } else {
142
+ location = resolveAsyncRunLocation(params, asyncDirRoot, resultsDir);
143
+ }
144
+ } catch (error) {
145
+ const message = error instanceof Error ? error.message : String(error);
146
+ return {
147
+ content: [{ type: "text", text: message }],
148
+ isError: true,
149
+ details: { mode: "single", results: [] },
150
+ };
151
+ }
152
+ const { asyncDir, resultPath, resolvedId } = location;
153
+
154
+ if (!asyncDir && !resultPath) {
155
+ return {
156
+ content: [{ type: "text", text: "Async run not found. Provide id or dir." }],
157
+ isError: true,
158
+ details: { mode: "single", results: [] },
159
+ };
160
+ }
161
+
162
+ if (asyncDir) {
163
+ let reconciliation;
164
+ try {
165
+ reconciliation = reconcileAsyncRun(asyncDir, { resultsDir, kill: deps.kill, now: deps.now });
166
+ } catch (error) {
167
+ const message = error instanceof Error ? error.message : String(error);
168
+ return {
169
+ content: [{ type: "text", text: message }],
170
+ isError: true,
171
+ details: { mode: "single", results: [] },
172
+ };
173
+ }
174
+ const status = reconciliation.status;
175
+ const effectiveRunId = status?.runId ?? resolvedId ?? "unknown";
176
+ const logPath = path.join(asyncDir, `subagent-log-${effectiveRunId}.md`);
177
+ const eventsPath = path.join(asyncDir, "events.jsonl");
178
+ if (status) {
179
+ let nestedChildren: NestedRunSummary[] = [];
180
+ let nestedWarning: string | undefined;
181
+ try {
182
+ const nestedRoute = findNestedRouteForRootId(status.runId);
183
+ if (nestedRoute) reconcileNestedAsyncDescendants(nestedRoute, { resultsDir, kill: deps.kill, now: deps.now });
184
+ nestedChildren = projectNestedRegistryForRoot(status.runId)?.children ?? [];
185
+ attachRootChildrenToSteps(status.runId, status.steps, nestedChildren);
186
+ } catch (error) {
187
+ nestedWarning = `Nested status unavailable: ${error instanceof Error ? error.message : String(error)}`;
188
+ }
189
+ const outputPath = formatAsyncRunOutputPath({ asyncDir, outputFile: status.outputFile });
190
+ const progressLabel = formatAsyncRunProgressLabel({
191
+ mode: status.mode,
192
+ state: status.state,
193
+ currentStep: status.currentStep,
194
+ chainStepCount: status.chainStepCount,
195
+ parallelGroups: status.parallelGroups,
196
+ steps: (status.steps ?? []).map((step, index) => ({ index, agent: step.agent, status: step.status })),
197
+ });
198
+ const started = new Date(status.startedAt).toISOString();
199
+ const updated = status.lastUpdate ? new Date(status.lastUpdate).toISOString() : "n/a";
200
+ const statusActivityText = status.state === "running" ? formatActivityLabel(status.lastActivityAt, status.activityState) : undefined;
201
+
202
+ const lines = [
203
+ `Run: ${status.runId}`,
204
+ `State: ${status.state}`,
205
+ statusActivityText ? `Activity: ${statusActivityText}` : undefined,
206
+ `Mode: ${status.mode}`,
207
+ `Progress: ${progressLabel}`,
208
+ `Started: ${started}`,
209
+ `Updated: ${updated}`,
210
+ `Dir: ${asyncDir}`,
211
+ outputPath ? `Output: ${outputPath}` : undefined,
212
+ reconciliation.message ? `Diagnosis: ${reconciliation.message}` : undefined,
213
+ reconciliation.resultPath && fs.existsSync(reconciliation.resultPath) ? `Result: ${reconciliation.resultPath}` : undefined,
214
+ ].filter((line): line is string => Boolean(line));
215
+ for (const [index, step] of (status.steps ?? []).entries()) {
216
+ const stepActivityText = step.status === "running" ? formatActivityLabel(step.lastActivityAt, step.activityState) : undefined;
217
+ const modelThinking = formatModelThinking(step.model, step.thinking);
218
+ const modelText = modelThinking ? ` (${modelThinking})` : "";
219
+ const errorText = step.error ? `, error: ${step.error}` : "";
220
+ lines.push(`${stepLineLabel(status, index)}: ${step.agent} ${step.status}${modelText}${stepActivityText ? `, ${stepActivityText}` : ""}${errorText}`);
221
+ lines.push(...formatNestedRunStatusLines(step.children, { indent: " ", commandHints: true, maxLines: 20 }));
222
+ const stepOutputPath = path.join(asyncDir, `output-${index}.log`);
223
+ if (stepOutputPath !== outputPath && fs.existsSync(stepOutputPath)) lines.push(` Output: ${stepOutputPath}`);
224
+ if (step.status === "running") {
225
+ lines.push(` Intercom target: ${resolveSubagentIntercomTarget(status.runId, step.agent, index)} (if registered)`);
226
+ }
227
+ }
228
+ const attached = new Set((status.steps ?? []).flatMap((step) => step.children?.map((child) => child.id) ?? []));
229
+ const unattached = nestedChildren.filter((child) => !attached.has(child.id));
230
+ lines.push(...formatNestedRunStatusLines(unattached, { indent: "", commandHints: true, maxLines: 20 }));
231
+ if (nestedWarning) lines.push(`Warning: ${nestedWarning}`);
232
+ if (status.sessionFile) lines.push(`Session: ${status.sessionFile}`);
233
+ if (status.state !== "running") {
234
+ lines.push(formatResumeGuidance(status.runId, status.steps ?? [], status.sessionFile));
235
+ }
236
+ if (fs.existsSync(logPath)) lines.push(`Log: ${logPath}`);
237
+ if (fs.existsSync(eventsPath)) lines.push(`Events: ${eventsPath}`);
238
+
239
+ return { content: [{ type: "text", text: lines.join("\n") }], details: { mode: "single", results: [] } };
240
+ }
241
+ }
242
+
243
+ if (resultPath) {
244
+ try {
245
+ const raw = fs.readFileSync(resultPath, "utf-8");
246
+ const data = JSON.parse(raw) as { id?: string; runId?: string; agent?: string; success?: boolean; summary?: string; exitCode?: number; state?: string; sessionFile?: string; results?: Array<{ agent?: string; sessionFile?: string }> };
247
+ const status = data.success ? "complete" : data.state === "paused" || data.exitCode === 0 ? "paused" : "failed";
248
+ const runId = data.runId ?? data.id ?? resolvedId;
249
+ const lines = [`Run: ${runId}`, `State: ${status}`, `Result: ${resultPath}`];
250
+ const children = Array.isArray(data.results) ? data.results : data.agent ? [{ agent: data.agent, sessionFile: data.sessionFile }] : [];
251
+ lines.push(formatResumeGuidance(runId, children, data.sessionFile));
252
+ if (data.summary) lines.push("", data.summary);
253
+ return { content: [{ type: "text", text: lines.join("\n") }], details: { mode: "single", results: [] } };
254
+ } catch (error) {
255
+ const message = error instanceof Error ? error.message : String(error);
256
+ return {
257
+ content: [{ type: "text", text: `Failed to read async result file: ${message}` }],
258
+ isError: true,
259
+ details: { mode: "single", results: [] },
260
+ };
261
+ }
262
+ }
263
+
264
+ return {
265
+ content: [{ type: "text", text: "Status file not found." }],
266
+ isError: true,
267
+ details: { mode: "single", results: [] },
268
+ };
269
+ }