@ferris1225/pi-subagents 0.24.0 → 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.
package/src/index.ts CHANGED
@@ -154,7 +154,13 @@ function formatUsage(usage: UsageStats): string {
154
154
  }
155
155
 
156
156
  function formatCompletionBlock(result: SingleResult, maxResultLines: number, cwd?: string): string {
157
- const status = isFailedResult(result) ? "failed" : "completed";
157
+ const failed = isFailedResult(result);
158
+ const failedTools = result.failedTools ?? [];
159
+ const status = failed
160
+ ? "failed"
161
+ : failedTools.length > 0
162
+ ? `completed with ${failedTools.length} failed tool call${failedTools.length === 1 ? "" : "s"}`
163
+ : "completed";
158
164
  const usage = formatUsage(result.usage);
159
165
  const output = getResultOutput(result);
160
166
  const { text, truncated } = truncateResultOutput(output, maxResultLines);
@@ -165,6 +171,20 @@ function formatCompletionBlock(result: SingleResult, maxResultLines: number, cwd
165
171
  ? ` (recovered after ${result.startupRetries} startup retr${result.startupRetries === 1 ? "y" : "ies"} — concurrent pi startup race)`
166
172
  : "";
167
173
  const lines = [`### [${result.agent}] ${status}${usage ? ` (${usage})` : ""}${fallbackNote}${retryNote}`, "", `Task: ${formatTaskSummary(result.task, 80, false)}`, "", text];
174
+ // A run can exit cleanly while its last tools failed (e.g. a build that broke):
175
+ // the final text alone may claim more than the tools achieved, so surface the
176
+ // failures explicitly and tell the main agent to verify before relying on it.
177
+ if (!failed && failedTools.length > 0) {
178
+ const shown = failedTools.slice(0, 3);
179
+ const more = failedTools.length - shown.length;
180
+ lines.push(
181
+ "",
182
+ `⚠ ${failedTools.length} tool call${failedTools.length === 1 ? "" : "s"} failed during this run — the final text above may not reflect a working state:`,
183
+ ...shown.map((tool) => `- ${tool.toolName}: ${tool.error.trim() || "(no output)"}`),
184
+ );
185
+ if (more > 0) lines.push(`- … and ${more} more`);
186
+ lines.push("Verify the actual artifacts before relying on this report.");
187
+ }
168
188
  if (truncated) {
169
189
  // The full text lives on disk so the main agent can read it on demand.
170
190
  lines.push("", `(output truncated to ${maxResultLines} lines; full result: ${writeResultArtifact(output, result.agent, cwd)})`);
@@ -180,6 +200,16 @@ function modelLevelTakeoverNote(result: SingleResult): string {
180
200
  return `The sub-agent could not complete this task: its model was unavailable or failed (or the run stalled)${retry}. Please execute this task in the main window with your own tools; do not re-dispatch it as a sub-agent.`;
181
201
  }
182
202
 
203
+ /** Resolve a run-id request to actual ids: an exact numeric match always wins
204
+ * (so "1" never fans out to 10, 11, …); only when no exact match exists does a
205
+ * prefix match run, as a convenience for partial ids. Keeps single-digit lookups
206
+ * from returning — or, for subagent_stop, acting on — a whole prefix family. */
207
+ export function matchRunIds(ids: number[], requested: string): number[] {
208
+ const exact = ids.filter((id) => String(id) === requested);
209
+ if (exact.length > 0) return exact;
210
+ return ids.filter((id) => String(id).startsWith(requested));
211
+ }
212
+
183
213
  export default function (pi: ExtensionAPI): void {
184
214
  const configPath = getConfigPath(getAgentDir());
185
215
  // Init-time decisions need the config synchronously; the full (migrating)
@@ -195,7 +225,11 @@ export default function (pi: ExtensionAPI): void {
195
225
  display: true,
196
226
  };
197
227
  if (completionGroupTriggersTurn(items)) {
198
- pi.sendMessage(message, { deliverAs: "followUp", triggerTurn: true });
228
+ // steer: the result is injected after the current tool call even mid-turn, or
229
+ // starts a new turn when idle. followUp would sit in the queue until the whole
230
+ // turn ends — a main agent waiting for the result (sleep/poll) would never see
231
+ // it delivered, which is exactly the "returned but never woken" failure mode.
232
+ pi.sendMessage(message, { deliverAs: "steer", triggerTurn: true });
199
233
  } else {
200
234
  // No-wake delivery: nextTurn rides along with the next user turn and can
201
235
  // never start a continuation by itself. followUp would auto-continue
@@ -205,6 +239,28 @@ export default function (pi: ExtensionAPI): void {
205
239
  };
206
240
  const completionBatcher = createCompletionBatcher<CompletionMessageItem>({ emit: sendCompletionGroup });
207
241
 
242
+ // Abort controllers per active run, so subagent_stop can cancel a run in-turn.
243
+ const runControllers = new Map<number, AbortController>();
244
+
245
+ // Final results keyed by run id, so `subagent_wait` can hand the model the
246
+ // actual result in-turn instead of it sleeping/polling for a wake-up message.
247
+ const settledRuns = new Map<number, SingleResult>();
248
+ const settledListeners = new Map<number, Set<(result: SingleResult) => void>>();
249
+ const registerRunResult = (runId: number, result: SingleResult): void => {
250
+ settledRuns.set(runId, result);
251
+ const listeners = settledListeners.get(runId);
252
+ if (listeners) {
253
+ settledListeners.delete(runId);
254
+ for (const listener of listeners) {
255
+ try {
256
+ listener(result);
257
+ } catch {
258
+ /* listener errors must never break settling */
259
+ }
260
+ }
261
+ }
262
+ };
263
+
208
264
  // Recursion guard: sub-agent children are leaf processes. The `subagent` tool is
209
265
  // excluded from their toolset at spawn (--exclude-tools); this check is defense
210
266
  // in depth so a child can never expose the tool back to its model, even if
@@ -231,6 +287,9 @@ export default function (pi: ExtensionAPI): void {
231
287
  sessionActive = false;
232
288
  completionBatcher.dispose();
233
289
  backgroundQueue.cancelAll();
290
+ settledRuns.clear();
291
+ settledListeners.clear();
292
+ runControllers.clear();
234
293
  // Clear the monitor so stale runs from this session never leak into the
235
294
  // next one (the module-level singleton survives across sessions).
236
295
  monitor.clear();
@@ -244,7 +303,8 @@ export default function (pi: ExtensionAPI): void {
244
303
  "Agents: explore (read-only codebase recon), worker (implement/fix/refactor/test, full tools), reviewer (adversarial pre-commit review, read-only).",
245
304
  "Modes: single ({agent, task}) or parallel ({tasks: [{agent, task}, ...]}).",
246
305
  "It starts agents in the background and immediately returns control to the main window; completion messages automatically wake the main agent to continue.",
247
- "Each agent has no memory of this conversation — brief it fully (goal, exact paths, constraints, expected output)."
306
+ "Each agent has no memory of this conversation — brief it fully (goal, exact paths, constraints, expected output).",
307
+ "To get a result in-turn without sleeping, use the subagent_wait tool."
248
308
  ].join(" "),
249
309
  promptSnippet:
250
310
  "Start background subagents: explore (read-only search), worker (implement), reviewer (adversarial review); completion automatically resumes the main agent. Simple tasks: use direct tools, not subagents.",
@@ -255,7 +315,8 @@ export default function (pi: ExtensionAPI): void {
255
315
  "Use subagent with agent 'reviewer' for a fresh read-only review before reporting work done or committing.",
256
316
  "subagent launches work in the background and ends the current turn; when a result arrives, the main agent is automatically resumed with it.",
257
317
  "Run independent tasks in parallel by passing a tasks array to subagent; let the automatically resumed main agent start dependent work after results arrive.",
258
- "NEVER sleep, wait, poll, or call other tools alongside subagent — it ends the turn immediately. The main agent is auto-resumed when results arrive; manual waiting only blocks the turn and delays delivery.",
318
+ "NEVER sleep, poll, or call other tools alongside subagent — it ends the turn immediately. The main agent is auto-resumed when results arrive; manual waiting only blocks the turn and delays delivery. The one exception is subagent_wait (below): only when you must stay in the turn.",
319
+ "If you must keep the turn for a result, call subagent_wait (blocks in-tool and returns the result) — never bash sleep/timeout to wait for a sub-agent.",
259
320
  ],
260
321
  parameters: SubagentParams,
261
322
 
@@ -424,11 +485,12 @@ export default function (pi: ExtensionAPI): void {
424
485
  sessionRef,
425
486
  );
426
487
  finishRun(runId, isFailedResult(result) ? "failed" : "done");
488
+ registerRunResult(runId, result);
427
489
  return result;
428
490
  } catch (error) {
429
491
  finishRun(runId, "failed");
430
492
  const errorMessage = error instanceof Error ? error.message : String(error);
431
- return {
493
+ const crashed = {
432
494
  ...queuedResult(agent, task, thinkingLevel),
433
495
  exitCode: 1,
434
496
  stderr: errorMessage,
@@ -436,6 +498,8 @@ export default function (pi: ExtensionAPI): void {
436
498
  errorMessage,
437
499
  dispatchFailed: true,
438
500
  };
501
+ registerRunResult(runId, crashed);
502
+ return crashed;
439
503
  }
440
504
  };
441
505
 
@@ -448,7 +512,7 @@ export default function (pi: ExtensionAPI): void {
448
512
  * the chain resolves, so the ↳ rows have an obvious parent.
449
513
  */
450
514
  const startFixLoop = (initialReviewerResult: SingleResult, parentGroupId: string, parentRunId: number): void => {
451
- backgroundQueue.enqueue(
515
+ runControllers.set(parentRunId, backgroundQueue.enqueue(
452
516
  async (signal) => {
453
517
  const chain: SingleResult[] = [initialReviewerResult];
454
518
  let lastReviewer = initialReviewerResult;
@@ -477,7 +541,11 @@ export default function (pi: ExtensionAPI): void {
477
541
  // The chain is done (success, exhaustion, or abort): drop the retained
478
542
  // parent row, then deliver the whole chain as one group. The loop's
479
543
  // outcome always wakes the main agent (a passing chain reports
480
- // success, a stuck one needs a human).
544
+ // success, a stuck one needs a human). Register the parent's final
545
+ // state (the last chain result) before removal so subagent_wait can
546
+ // resolve it.
547
+ registerRunResult(parentRunId, chain[chain.length - 1]);
548
+ runControllers.delete(parentRunId);
481
549
  monitor.removeRun(parentRunId);
482
550
  if (!sessionActive) return;
483
551
  const items: CompletionMessageItem[] = chain.map((r) => {
@@ -500,6 +568,7 @@ export default function (pi: ExtensionAPI): void {
500
568
  () => {
501
569
  // Cancelled before delivery: clean up the retained parent row (each
502
570
  // in-flight chain run was already finished by its launchInLoop path).
571
+ runControllers.delete(parentRunId);
503
572
  monitor.removeRun(parentRunId);
504
573
  },
505
574
  (error) => {
@@ -507,6 +576,8 @@ export default function (pi: ExtensionAPI): void {
507
576
  // launchInLoop and delivered as part of the chain) must not vanish:
508
577
  // drop the retained parent row, notify, and deliver a failed result
509
578
  // so the main agent knows the chain never completed.
579
+ registerRunResult(parentRunId, initialReviewerResult);
580
+ runControllers.delete(parentRunId);
510
581
  monitor.removeRun(parentRunId);
511
582
  if (!sessionActive) return;
512
583
  const errorMessage = error instanceof Error ? error.message : String(error);
@@ -526,7 +597,7 @@ export default function (pi: ExtensionAPI): void {
526
597
  /* a second delivery failure must not throw through the queue */
527
598
  }
528
599
  },
529
- );
600
+ ));
530
601
  };
531
602
 
532
603
  const startBackground = (agentName: string, task: string, cwd?: string): SingleResult => {
@@ -541,7 +612,7 @@ export default function (pi: ExtensionAPI): void {
541
612
  // only its finish is deferred to the queue task (see startFixLoop).
542
613
  const onLive = makeLiveHandler(runId);
543
614
 
544
- backgroundQueue.enqueue(
615
+ runControllers.set(runId, backgroundQueue.enqueue(
545
616
  async (backgroundSignal) => {
546
617
  let result: SingleResult;
547
618
  try {
@@ -573,6 +644,8 @@ export default function (pi: ExtensionAPI): void {
573
644
  // The dedicated dispatch-failure notification below replaces the generic
574
645
  // failure toast for dispatch crashes, so finish silently here.
575
646
  finishRun(runId, "failed", { silent: true });
647
+ registerRunResult(runId, result);
648
+ runControllers.delete(runId);
576
649
  }
577
650
 
578
651
  if (!sessionActive) return;
@@ -600,6 +673,10 @@ export default function (pi: ExtensionAPI): void {
600
673
  const modelLevel = failed && isModelLevelFailure(result);
601
674
  const dispatchFailed = result.dispatchFailed === true;
602
675
  finishRun(runId, failed ? "failed" : "done", modelLevel || dispatchFailed ? { silent: true } : undefined);
676
+ // Register before delivery so a concurrent subagent_wait resolves with
677
+ // the result even though the run row is already gone from the monitor.
678
+ registerRunResult(runId, result);
679
+ runControllers.delete(runId);
603
680
  if (!sessionActive) return;
604
681
  // Model-level failure: the configured model is unavailable or broke
605
682
  // and the retry with the main-window model (when distinct) also
@@ -628,7 +705,10 @@ export default function (pi: ExtensionAPI): void {
628
705
  completionBatcher.push(completion);
629
706
  }
630
707
  },
631
- () => finishRun(runId, "failed"),
708
+ () => {
709
+ runControllers.delete(runId);
710
+ finishRun(runId, "failed");
711
+ },
632
712
  (error) => {
633
713
  // The task body converts sub-agent failures into delivered results; an
634
714
  // exception escaping it (spawn infra, delivery API, ...) must not
@@ -636,6 +716,8 @@ export default function (pi: ExtensionAPI): void {
636
716
  // agent knows the dispatch failed and can re-dispatch.
637
717
  const crashed = dispatchFailedResult(agent, task, error, thinkingLevel);
638
718
  finishRun(runId, "failed", { silent: true });
719
+ registerRunResult(runId, crashed);
720
+ runControllers.delete(runId);
639
721
  if (!sessionActive) return;
640
722
  try {
641
723
  ctx.ui.notify(`✗ ${agent.name} 派发失败: ${crashed.errorMessage}`, "error");
@@ -651,7 +733,7 @@ export default function (pi: ExtensionAPI): void {
651
733
  /* a second delivery failure must not throw through the queue */
652
734
  }
653
735
  },
654
- );
736
+ ));
655
737
 
656
738
  return pending;
657
739
  };
@@ -754,6 +836,407 @@ export default function (pi: ExtensionAPI): void {
754
836
  },
755
837
  });
756
838
 
839
+ // Blocking wait: keeps the turn alive until the targeted run(s) settle, then
840
+ // returns the actual result(s) to the model in-turn. Without it, a model that
841
+ // must stay in the turn falls back to bash sleep/poll — blocking the turn and
842
+ // delaying the very wake-up it is waiting for. Ending the turn and letting the
843
+ // steer-delivered completion wake it is still the preferred path; this tool is
844
+ // for when the result is needed NOW (sequential dependent steps).
845
+ const SUBAGENT_WAIT_DEFAULT_TIMEOUT_MS = 30 * 60 * 1000;
846
+
847
+ const SubagentWaitParams = Type.Object({
848
+ id: Type.Optional(
849
+ Type.String({
850
+ description: "Run id or prefix shown in the subagent widget (#id). Omit to wait for all active runs in this session.",
851
+ }),
852
+ ),
853
+ timeoutMs: Type.Optional(
854
+ Type.Number({
855
+ description: `Give up after this many milliseconds and report the still-running runs (default ${SUBAGENT_WAIT_DEFAULT_TIMEOUT_MS}).`,
856
+ }),
857
+ ),
858
+ });
859
+
860
+ pi.registerTool({
861
+ name: "subagent_wait",
862
+ label: "Subagent Wait",
863
+ description: [
864
+ "Block the current turn until background sub-agent run(s) finish, then return their results.",
865
+ "Use ONLY when you must stay in the turn and act on the result immediately (sequential dependent steps).",
866
+ "Prefer ending your turn after subagent — the result arrives automatically and wakes you.",
867
+ "NEVER sleep, poll, or wait with bash to get a sub-agent result: end the turn, or call this tool.",
868
+ "The same result is also delivered as a completion message that resumes the main agent, so you may see it twice (once here, once as a wake-up) — that is expected, not a duplicate.",
869
+ ].join(" "),
870
+ promptSnippet: "Wait for a background subagent to finish and get its result in-turn (id: run id from the widget; omit for all).",
871
+ promptGuidelines: [
872
+ "Call subagent_wait only when you must keep the turn and need the result now — e.g. the next step depends on it.",
873
+ "After dispatching via subagent, prefer ending the turn: the completion message wakes you automatically (no waiting).",
874
+ "Never use bash sleep/timeout/polling to wait for a sub-agent — it blocks the turn and delays result delivery.",
875
+ "If subagent_wait times out, call it again with a longer timeoutMs or end the turn and wait for the wake-up message.",
876
+ ],
877
+ parameters: SubagentWaitParams,
878
+
879
+ async execute(_toolCallId, params, signal, _onUpdate, ctx) {
880
+ const config = await loadConfig(configPath);
881
+ // A non-finite or negative timeout would produce a nonsensical note
882
+ // ("timed out after Infinitys") or an instant "timeout" that was never
883
+ // asked for; fall back to the default. Zero is honored as an immediate
884
+ // give-up (clamped to 1ms below).
885
+ const timeoutMs =
886
+ typeof params.timeoutMs === "number" && Number.isFinite(params.timeoutMs) && params.timeoutMs >= 0
887
+ ? params.timeoutMs
888
+ : SUBAGENT_WAIT_DEFAULT_TIMEOUT_MS;
889
+ const isActive = (run: { status: string; retained?: boolean }): boolean =>
890
+ run.status === "queued" || run.status === "running" || run.retained === true;
891
+
892
+ const requested = params.id?.trim();
893
+ // A run that already settled resolves immediately with its result.
894
+ if (requested) {
895
+ const settledIds = matchRunIds([...settledRuns.keys()], requested);
896
+ if (settledIds.length > 0) {
897
+ return {
898
+ content: [
899
+ { type: "text", text: settledIds.map((id) => formatCompletionBlock(settledRuns.get(id)!, config.maxResultLines, ctx.cwd)).join("\n\n") },
900
+ ],
901
+ details: {},
902
+ };
903
+ }
904
+ }
905
+
906
+ const activeRuns = monitor.getRuns().filter(isActive);
907
+ const targetIds = requested ? matchRunIds(activeRuns.map((run) => run.id), requested) : activeRuns.map((run) => run.id);
908
+ const targets = activeRuns.filter((run) => targetIds.includes(run.id));
909
+ if (targets.length === 0) {
910
+ const activeList = activeRuns.map((run) => `#${run.id} ${run.agent}`).join(", ");
911
+ return {
912
+ content: [
913
+ {
914
+ type: "text",
915
+ text: requested
916
+ ? `No active subagent run matches "${requested}".${activeList ? ` Active runs: ${activeList}.` : ""}`
917
+ : `No active subagent runs${activeList ? ` (active: ${activeList})` : " right now"}.`,
918
+ },
919
+ ],
920
+ details: {},
921
+ };
922
+ }
923
+
924
+ const waitForRun = (runId: number): Promise<{ result?: SingleResult; note?: string }> => {
925
+ const already = settledRuns.get(runId);
926
+ if (already) return Promise.resolve({ result: already });
927
+ return new Promise((resolve) => {
928
+ let done = false;
929
+ let timer: ReturnType<typeof setTimeout> | undefined;
930
+ let unsub: (() => void) | undefined;
931
+ const cleanup = (): void => {
932
+ if (timer) clearTimeout(timer);
933
+ if (unsub) unsub();
934
+ signal?.removeEventListener("abort", onAbort);
935
+ const listeners = settledListeners.get(runId);
936
+ if (listeners) {
937
+ listeners.delete(onSettled);
938
+ if (listeners.size === 0) settledListeners.delete(runId);
939
+ }
940
+ };
941
+ const finish = (outcome: { result?: SingleResult; note?: string }): void => {
942
+ if (done) return;
943
+ done = true;
944
+ cleanup();
945
+ resolve(outcome);
946
+ };
947
+ const onSettled = (result: SingleResult): void => finish({ result });
948
+ const onMonitor = (): void => {
949
+ const current = settledRuns.get(runId);
950
+ if (current) {
951
+ finish({ result: current });
952
+ return;
953
+ }
954
+ if (!monitor.findRun(runId)) {
955
+ // Removal is followed synchronously by registerRunResult in the
956
+ // finishing task; re-check on the next tick so the result wins.
957
+ setTimeout(() => {
958
+ const late = settledRuns.get(runId);
959
+ if (late) finish({ result: late });
960
+ else finish({ note: `run #${runId} was removed before its result was recorded (cancelled or session ended)` });
961
+ }, 0);
962
+ }
963
+ };
964
+ const onAbort = (): void => finish({ note: "wait aborted" });
965
+ let listeners = settledListeners.get(runId);
966
+ if (!listeners) {
967
+ listeners = new Set();
968
+ settledListeners.set(runId, listeners);
969
+ }
970
+ listeners.add(onSettled);
971
+ unsub = monitor.subscribe(onMonitor);
972
+ timer = setTimeout(
973
+ () =>
974
+ finish({
975
+ note: `wait timed out after ${Math.round(timeoutMs / 1000)}s — run #${runId} is still active; call subagent_wait again or end the turn (the result will wake you when ready)`,
976
+ }),
977
+ Math.max(1, timeoutMs),
978
+ );
979
+ if (signal?.aborted) onAbort();
980
+ else signal?.addEventListener("abort", onAbort, { once: true });
981
+ });
982
+ };
983
+
984
+ const outcomes = await Promise.all(targets.map((run) => waitForRun(run.id)));
985
+ const blocks = outcomes.map((outcome) =>
986
+ outcome.result ? formatCompletionBlock(outcome.result, config.maxResultLines, ctx.cwd) : (outcome.note ?? "(no outcome)"),
987
+ );
988
+ return { content: [{ type: "text", text: blocks.join("\n\n") }], details: {} };
989
+ },
990
+
991
+ renderCall(args, theme) {
992
+ const target = args.id ? `#${args.id}` : "all";
993
+ return new Text(`${theme.fg("toolTitle", theme.bold("subagent_wait "))}${theme.fg("accent", target)}`, 0, 0);
994
+ },
995
+
996
+ renderResult(result, _options, theme) {
997
+ const parts = (result.content ?? []) as Array<{ type: string; text?: string }>;
998
+ const text = parts
999
+ .map((part) => (typeof part.text === "string" ? part.text : ""))
1000
+ .join(" ")
1001
+ .trim();
1002
+ const firstLine = text.split("\n").find((line) => line.trim()) ?? "(no output)";
1003
+ return new Text(
1004
+ `${theme.fg("toolTitle", theme.bold("subagent_wait "))}${theme.fg("dim", firstLine.slice(0, 60))}`,
1005
+ 0,
1006
+ 0,
1007
+ );
1008
+ },
1009
+ });
1010
+
1011
+ // Status overview: what is running right now and what finished this session,
1012
+ // with per-run details (id, agent, model, usage, elapsed, activity) so the
1013
+ // main agent can decide whether to wait, stop, or re-dispatch. Learned from
1014
+ // nicobailon/pi-subagents ({action:"status"} + status files): inspect before
1015
+ // you act, and report run ids when handing off.
1016
+ const SubagentStatusParams = Type.Object({
1017
+ id: Type.Optional(
1018
+ Type.String({
1019
+ description: "Run id or prefix to show the full result for (must already be finished; use subagent_wait to block on an active run).",
1020
+ }),
1021
+ ),
1022
+ });
1023
+
1024
+ pi.registerTool({
1025
+ name: "subagent_status",
1026
+ label: "Subagent Status",
1027
+ description: [
1028
+ "List active background sub-agent runs (id, agent, model, usage, elapsed, current activity) and recently finished results.",
1029
+ "Pass id to read the full result of a finished run; pass no id for the overview.",
1030
+ "Use it to decide whether to subagent_wait, subagent_stop, or re-dispatch — never to poll: results arrive by themselves.",
1031
+ ].join(" "),
1032
+ promptSnippet: "Inspect background subagents: active runs, finished results, full result by id.",
1033
+ promptGuidelines: [
1034
+ "Call subagent_status to see what is running and what already finished; the widget shows the same live state.",
1035
+ "Never poll subagent_status in a loop to wait for a run: end the turn (you will be woken) or call subagent_wait.",
1036
+ "A finished run's id stays available for the session; its full result is one subagent_status call away.",
1037
+ ],
1038
+ parameters: SubagentStatusParams,
1039
+
1040
+ async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
1041
+ const config = await loadConfig(configPath);
1042
+ const requested = params.id?.trim();
1043
+
1044
+ if (requested) {
1045
+ const settledIds = matchRunIds([...settledRuns.keys()], requested);
1046
+ if (settledIds.length > 0) {
1047
+ return {
1048
+ content: [
1049
+ { type: "text", text: settledIds.map((id) => formatCompletionBlock(settledRuns.get(id)!, config.maxResultLines, ctx.cwd)).join("\n\n") },
1050
+ ],
1051
+ details: {},
1052
+ };
1053
+ }
1054
+ const runs = monitor.getRuns();
1055
+ const activeId = matchRunIds(runs.map((run) => run.id), requested)[0];
1056
+ const active = activeId === undefined ? undefined : runs.find((run) => run.id === activeId);
1057
+ if (active) {
1058
+ return {
1059
+ content: [
1060
+ {
1061
+ type: "text",
1062
+ text: `Run #${active.id} ${active.agent} is still active (${active.activity ?? statusLabel(active.status)}). Use subagent_wait to block for its result, or subagent_stop to cancel it.`,
1063
+ },
1064
+ ],
1065
+ details: {},
1066
+ };
1067
+ }
1068
+ return { content: [{ type: "text", text: `No subagent run matches "${requested}".` }], details: {} };
1069
+ }
1070
+
1071
+ const now = Date.now();
1072
+ const activeRuns = monitor.getRuns().filter(
1073
+ (run) => run.status === "queued" || run.status === "running" || run.retained,
1074
+ );
1075
+ const activeLines = activeRuns.map((run) => {
1076
+ const parts = [
1077
+ `#${run.id} ${run.agent}`,
1078
+ run.model ?? "?",
1079
+ formatUsageCompact(run.usage),
1080
+ formatElapsed(run, now),
1081
+ ].filter(Boolean);
1082
+ return `- ${parts.join(" · ")} · ${run.activity ?? statusLabel(run.status)}`;
1083
+ });
1084
+ const completed = [...settledRuns.entries()].slice(-5);
1085
+ const completedLines = completed.map(([id, result]) => {
1086
+ const usage = formatUsage(result.usage);
1087
+ return `- #${id} ${result.agent} · ${isFailedResult(result) ? "failed" : "completed"}${usage ? ` · ${usage}` : ""}`;
1088
+ });
1089
+
1090
+ const sections: string[] = [];
1091
+ sections.push(`### Active subagent runs (${activeRuns.length})`);
1092
+ sections.push(activeLines.length > 0 ? activeLines.join("\n") : "(none)");
1093
+ sections.push(`### Finished this session (${settledRuns.size})`);
1094
+ sections.push(completedLines.length > 0 ? completedLines.join("\n") : "(none)");
1095
+ sections.push("Pass a run id to subagent_status for the full result, or subagent_wait to block for an active run.");
1096
+ return { content: [{ type: "text", text: sections.join("\n\n") }], details: {} };
1097
+ },
1098
+
1099
+ renderCall(args, theme) {
1100
+ return new Text(
1101
+ `${theme.fg("toolTitle", theme.bold("subagent_status "))}${theme.fg("accent", args.id ? `#${args.id}` : "overview")}`,
1102
+ 0,
1103
+ 0,
1104
+ );
1105
+ },
1106
+
1107
+ renderResult(result, _options, theme) {
1108
+ const parts = (result.content ?? []) as Array<{ type: string; text?: string }>;
1109
+ const text = parts
1110
+ .map((part) => (typeof part.text === "string" ? part.text : ""))
1111
+ .join(" ")
1112
+ .trim();
1113
+ const firstLine = text.split("\n").find((line) => line.trim()) ?? "(no output)";
1114
+ return new Text(
1115
+ `${theme.fg("toolTitle", theme.bold("subagent_status "))}${theme.fg("dim", firstLine.slice(0, 60))}`,
1116
+ 0,
1117
+ 0,
1118
+ );
1119
+ },
1120
+ });
1121
+
1122
+ // Cancel one or more active runs: aborts the queue controller, which
1123
+ // terminates the child and delivers an aborted result (with whatever partial
1124
+ // output it produced) so the main agent always knows the run stopped.
1125
+ const SubagentStopParams = Type.Object({
1126
+ id: Type.Optional(
1127
+ Type.String({
1128
+ description: "Run id or prefix to stop (see the widget or subagent_status).",
1129
+ }),
1130
+ ),
1131
+ all: Type.Optional(Type.Boolean({ description: "Stop every active run (default false)." })),
1132
+ });
1133
+
1134
+ pi.registerTool({
1135
+ name: "subagent_stop",
1136
+ label: "Subagent Stop",
1137
+ description: [
1138
+ "Cancel one or more active background sub-agent runs: the child process is terminated and an aborted result (with partial output) is delivered.",
1139
+ "Pass id (run id or prefix) to stop one run, or all: true to stop every active run.",
1140
+ ].join(" "),
1141
+ promptSnippet: "Stop a running background subagent (id from the widget/subagent_status; or all: true).",
1142
+ promptGuidelines: [
1143
+ "Stop a run when its task is obsolete, stuck, or superseded — do not leave it burning tokens.",
1144
+ "A stopped run reports as failed with 'aborted' and its partial output, so the next step knows it did not complete.",
1145
+ ],
1146
+ parameters: SubagentStopParams,
1147
+
1148
+ async execute(_toolCallId, params, _signal, _onUpdate) {
1149
+ const targets =
1150
+ params.all === true
1151
+ ? [...runControllers.keys()]
1152
+ : params.id !== undefined && params.id.trim() !== ""
1153
+ ? matchRunIds([...runControllers.keys()], params.id!.trim())
1154
+ : [];
1155
+
1156
+ if (targets.length === 0) {
1157
+ const activeList = [...runControllers.keys()].map((id) => `#${id}`).join(", ");
1158
+ return {
1159
+ content: [
1160
+ {
1161
+ type: "text",
1162
+ text:
1163
+ params.all === true
1164
+ ? "No active subagent runs to stop."
1165
+ : `No active subagent run matches "${params.id}".${activeList ? ` Active runs: ${activeList}.` : ""}`,
1166
+ },
1167
+ ],
1168
+ details: {},
1169
+ };
1170
+ }
1171
+
1172
+ const stopped: string[] = [];
1173
+ for (const runId of targets) {
1174
+ const run = monitor.findRun(runId);
1175
+ if (!run) {
1176
+ runControllers.delete(runId);
1177
+ continue;
1178
+ }
1179
+ // Abort before registering the synthetic result: abort() only marks the
1180
+ // queue entry (drain delivers the cancellation callback later), so the
1181
+ // has() re-check right after it distinguishes an entry that never ran
1182
+ // from one whose task already started under a stale "queued" status —
1183
+ // a started task owns its own (real, partial-output) result.
1184
+ const controller = runControllers.get(runId);
1185
+ controller?.abort();
1186
+ // A queued run never reaches the child-spawn code path, so its abort
1187
+ // goes through the queue's cancelled callback with no result object;
1188
+ // register a synthetic aborted result so subagent_wait resolves.
1189
+ if (run.status === "queued" && runControllers.has(runId)) {
1190
+ registerRunResult(runId, {
1191
+ agent: run.agent,
1192
+ agentSource: "builtin",
1193
+ task: run.task,
1194
+ exitCode: 1,
1195
+ messages: [],
1196
+ stderr: "Stopped by subagent_stop before the run started.",
1197
+ usage: emptyUsage(),
1198
+ model: run.model,
1199
+ thinking: run.thinking,
1200
+ stopReason: "aborted",
1201
+ errorMessage: "Stopped by subagent_stop before the run started.",
1202
+ });
1203
+ }
1204
+ stopped.push(`#${runId} ${run.agent}${run.status === "queued" ? " (queued)" : ""}`);
1205
+ }
1206
+ return {
1207
+ content: [
1208
+ {
1209
+ type: "text",
1210
+ text: `Stopped ${stopped.length} run${stopped.length === 1 ? "" : "s"}: ${stopped.join(", ")}. An aborted result (with partial output) is delivered.`,
1211
+ },
1212
+ ],
1213
+ details: {},
1214
+ };
1215
+ },
1216
+
1217
+ renderCall(args, theme) {
1218
+ return new Text(
1219
+ `${theme.fg("toolTitle", theme.bold("subagent_stop "))}${theme.fg("accent", args.all === true ? "all" : args.id ? `#${args.id}` : "?")}`,
1220
+ 0,
1221
+ 0,
1222
+ );
1223
+ },
1224
+
1225
+ renderResult(result, _options, theme) {
1226
+ const parts = (result.content ?? []) as Array<{ type: string; text?: string }>;
1227
+ const text = parts
1228
+ .map((part) => (typeof part.text === "string" ? part.text : ""))
1229
+ .join(" ")
1230
+ .trim();
1231
+ const firstLine = text.split("\n").find((line) => line.trim()) ?? "(no output)";
1232
+ return new Text(
1233
+ `${theme.fg("toolTitle", theme.bold("subagent_stop "))}${theme.fg("dim", firstLine.slice(0, 60))}`,
1234
+ 0,
1235
+ 0,
1236
+ );
1237
+ },
1238
+ });
1239
+
757
1240
  pi.registerCommand("subagents-setup", {
758
1241
  description: "Configure pi-subagents: enable agents, pick per-agent models, toggle proactive injection",
759
1242
  handler: async (_args, ctx) => {
package/src/monitor.ts CHANGED
@@ -252,7 +252,12 @@ export function formatDuration(ms: number): string {
252
252
  /** Elapsed wall time of a run: live while running, final once finished. */
253
253
  export function formatElapsed(run: RunView, now: number = Date.now()): string {
254
254
  if (run.startedAt === undefined) return "";
255
- const end = run.endedAt ?? now;
255
+ // A retained row (e.g. an auto-fix chain parent whose chain is still running)
256
+ // must keep ticking: its `endedAt` was stamped when the review itself
257
+ // finished, but the work is ongoing, so show live elapsed until the chain
258
+ // resolves and the row is removed. Without this, subagent_status would show a
259
+ // frozen elapsed for a run the UI otherwise presents as still active.
260
+ const end = run.retained ? now : (run.endedAt ?? now);
256
261
  return formatDuration(end - run.startedAt);
257
262
  }
258
263