@xzzpig/pi-subagents 0.4.1 → 0.5.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 (128) hide show
  1. package/CHANGELOG.md +175 -0
  2. package/README.md +4 -0
  3. package/agents/oracle.md +3 -1
  4. package/agents/reviewer.md +1 -0
  5. package/agents/scout.md +2 -2
  6. package/agents/worker.md +2 -1
  7. package/async-retention-discovery-worker.mjs +180 -0
  8. package/docs/agents.md +41 -3
  9. package/docs/configuration.md +41 -13
  10. package/docs/extension-api.md +65 -2
  11. package/docs/models.md +2 -0
  12. package/docs/observability.md +41 -5
  13. package/docs/tool-reference.md +38 -39
  14. package/docs/workflows.md +173 -3
  15. package/package.json +14 -10
  16. package/prompts/council.md +48 -0
  17. package/skills/council-mode/SKILL.md +230 -0
  18. package/skills/pi-subagents/SKILL.md +8 -4
  19. package/skills/pi-subagents/references/constraints-and-recipes.md +13 -6
  20. package/skills/pi-subagents/references/execution-controls.md +40 -15
  21. package/skills/pi-subagents/references/management-authoring-rpc.md +3 -3
  22. package/skills/pi-subagents/references/multi-lane-orchestration.md +39 -0
  23. package/skills/pi-subagents/references/prompting-and-roles.md +4 -2
  24. package/src/agents/agent-management.ts +471 -377
  25. package/src/agents/agent-serializer.ts +2 -0
  26. package/src/agents/agents.ts +186 -44
  27. package/src/agents/builtin-names.ts +9 -0
  28. package/src/agents/runtime-agent-registry.ts +418 -0
  29. package/src/agents/skills.ts +1 -1
  30. package/src/api/agent-management.ts +9 -0
  31. package/src/api/agents.ts +7 -0
  32. package/src/api/external-job-provider.ts +186 -0
  33. package/src/api/preflight.ts +45 -13
  34. package/src/api/shared-types.ts +2 -0
  35. package/src/extension/config.ts +6 -3
  36. package/src/extension/doctor.ts +4 -6
  37. package/src/extension/fanout-child.ts +2 -2
  38. package/src/extension/index.ts +183 -90
  39. package/src/extension/public-execution.ts +31 -2
  40. package/src/extension/rpc.ts +41 -1
  41. package/src/extension/schemas.ts +17 -37
  42. package/src/extension/tool-description.ts +35 -24
  43. package/src/inspectors/herdr/actions.ts +2 -2
  44. package/src/inspectors/herdr/inspector-runner.ts +2 -1
  45. package/src/inspectors/herdr/project-panes.ts +2 -2
  46. package/src/intercom/native-supervisor-channel.ts +32 -10
  47. package/src/missions/lifecycle.ts +6 -1
  48. package/src/missions/store.ts +4 -9
  49. package/src/missions/workflow-state.ts +2 -2
  50. package/src/profiles/profiles.ts +3 -1
  51. package/src/runs/background/active-run-index.ts +31 -8
  52. package/src/runs/background/async-execution.ts +70 -41
  53. package/src/runs/background/async-job-tracker.ts +24 -7
  54. package/src/runs/background/async-resume.ts +49 -16
  55. package/src/runs/background/async-retention.ts +886 -0
  56. package/src/runs/background/async-status-snapshot.ts +14 -5
  57. package/src/runs/background/async-status.ts +39 -53
  58. package/src/runs/background/auto-drain.ts +1 -0
  59. package/src/runs/background/chain-append.ts +3 -33
  60. package/src/runs/background/completion-dedupe.ts +5 -1
  61. package/src/runs/background/completion-replay.ts +22 -12
  62. package/src/runs/background/control-channel.ts +14 -68
  63. package/src/runs/background/fleet-view.ts +68 -19
  64. package/src/runs/background/index-segment.ts +59 -0
  65. package/src/runs/background/inspect-rpc.ts +443 -0
  66. package/src/runs/background/notify.ts +31 -5
  67. package/src/runs/background/result-files.ts +158 -90
  68. package/src/runs/background/result-watcher.ts +124 -20
  69. package/src/runs/background/resume-guidance.ts +8 -5
  70. package/src/runs/background/retained-children.ts +13 -3
  71. package/src/runs/background/run-id-query.ts +7 -0
  72. package/src/runs/background/run-id-resolver.ts +11 -9
  73. package/src/runs/background/run-status.ts +27 -18
  74. package/src/runs/background/scheduled-runs.ts +24 -4
  75. package/src/runs/background/stale-run-reconciler.ts +6 -3
  76. package/src/runs/background/steering.ts +11 -1
  77. package/src/runs/background/subagent-runner.ts +260 -144
  78. package/src/runs/background/subagent-wait.ts +17 -13
  79. package/src/runs/background/terminal-run-index.ts +138 -0
  80. package/src/runs/background/wait-completions.ts +21 -4
  81. package/src/runs/background/wait-subscriptions.ts +81 -2
  82. package/src/runs/background/wait-tool.ts +1 -0
  83. package/src/runs/foreground/async-steering-action.ts +21 -14
  84. package/src/runs/foreground/execution.ts +104 -71
  85. package/src/runs/foreground/subagent-executor.ts +816 -1593
  86. package/src/runs/foreground/workflow-detach-reconcile.ts +262 -0
  87. package/src/runs/foreground/workflow-foreground-steering.ts +6 -5
  88. package/src/runs/shared/acceptance.ts +48 -5
  89. package/src/runs/shared/chain-outputs.ts +1 -3
  90. package/src/runs/shared/external-job-bridge.ts +444 -0
  91. package/src/runs/shared/external-job-runner.ts +286 -0
  92. package/src/runs/shared/mcp-direct-tool-allowlist.ts +14 -0
  93. package/src/runs/shared/model-exclusions.ts +242 -0
  94. package/src/runs/shared/model-fallback.ts +153 -40
  95. package/src/runs/shared/orca-progress-tabs.ts +84 -22
  96. package/src/runs/shared/parallel-handoff.ts +46 -4
  97. package/src/runs/shared/parallel-utils.ts +4 -15
  98. package/src/runs/shared/permissions.ts +5 -1
  99. package/src/runs/shared/pi-args.ts +8 -1
  100. package/src/runs/shared/session-lease.ts +0 -6
  101. package/src/runs/shared/subagent-control.ts +51 -7
  102. package/src/runs/shared/subagent-prompt-runtime.ts +12 -6
  103. package/src/runs/shared/workflow-graph.ts +1 -23
  104. package/src/runs/shared/worktree.ts +14 -2
  105. package/src/shared/atomic-json.ts +22 -2
  106. package/src/shared/capacity-resilient-json.ts +102 -0
  107. package/src/shared/completion-owner.ts +14 -0
  108. package/src/shared/file-system-retry.ts +49 -1
  109. package/src/shared/fork-context.ts +59 -1
  110. package/src/shared/model-info.ts +20 -0
  111. package/src/shared/prompt-resources.ts +0 -40
  112. package/src/shared/settings.ts +5 -29
  113. package/src/shared/types.ts +106 -26
  114. package/src/shared/utils.ts +8 -0
  115. package/src/shared/watch-strategy.ts +10 -0
  116. package/src/slash/slash-bridge.ts +2 -1
  117. package/src/slash/slash-commands.ts +47 -7
  118. package/src/slash/slash-live-state.ts +3 -3
  119. package/src/tui/fleet-status.ts +86 -1
  120. package/src/tui/fleet.ts +117 -16
  121. package/src/tui/render.ts +73 -3
  122. package/src/watchdog/change-signature.ts +1 -1
  123. package/src/watchdog/lsp-diagnostics.ts +1 -0
  124. package/src/workflows/chat-progress.ts +2 -2
  125. package/src/workflows/scripted-workflow.ts +320 -79
  126. package/src/workflows/workflow-receipt.ts +140 -0
  127. package/src/runs/foreground/chain-clarify.ts +0 -1354
  128. package/src/runs/foreground/chain-execution.ts +0 -1581
@@ -74,6 +74,7 @@ import {
74
74
  buildModelCandidates,
75
75
  formatModelAttemptNote,
76
76
  isRetryableModelFailure,
77
+ recordRetryableModelFailure,
77
78
  } from "../shared/model-fallback.ts";
78
79
  import {
79
80
  SUBAGENT_STARTUP_RETRY_DELAYS_MS,
@@ -700,6 +701,11 @@ async function runSingleAttempt(
700
701
 
701
702
  let activeLongRunningNotified = false;
702
703
  let pendingToolResult: { tool: string; path?: string; mutates: boolean; startedAt?: number } | undefined;
704
+ // Pi host versions may represent one tool result as either a dedicated
705
+ // tool_result_end event or a message_end whose message role is toolResult.
706
+ // Remember correlated ids so normalizing both forms cannot duplicate output.
707
+ const handledToolResultIds = new Set<string>();
708
+ const MAX_HANDLED_TOOL_RESULT_IDS = 1024;
703
709
  type ActiveToolCall = { key: string; tool: string; args: string; startedAt: number; path?: string };
704
710
  let activeToolSequence = 0;
705
711
  const activeToolCalls = new Map<string, ActiveToolCall>();
@@ -783,6 +789,7 @@ async function runSingleAttempt(
783
789
  currentToolDurationMs: input.currentToolDurationMs ?? currentToolDurationMs(now),
784
790
  currentPath: input.currentPath ?? progress.currentPath,
785
791
  recentFailureSummary: input.recentFailureSummary,
792
+ taskPreview: task,
786
793
  });
787
794
  emitControlEvent(event);
788
795
  return previous !== "needs_attention";
@@ -809,6 +816,7 @@ async function runSingleAttempt(
809
816
  currentToolDurationMs: currentToolDurationMs(now),
810
817
  currentPath: progress.currentPath,
811
818
  elapsedMs: now - startTime,
819
+ taskPreview: task,
812
820
  }));
813
821
  return true;
814
822
  };
@@ -870,6 +878,7 @@ async function runSingleAttempt(
870
878
  startedAt: startTime,
871
879
  lastActivityAt: progress.lastActivityAt,
872
880
  currentTool: progress.currentTool,
881
+ thinking: resolvedThinking,
873
882
  now,
874
883
  });
875
884
  if (idleState === "needs_attention") {
@@ -919,6 +928,53 @@ async function runSingleAttempt(
919
928
  emitUpdateSnapshot(output || "(running...)");
920
929
  };
921
930
 
931
+ const toolResultCallId = (message: Message): string | undefined => {
932
+ const value = (message as { toolCallId?: unknown }).toolCallId;
933
+ return typeof value === "string" && value.length > 0 ? value : undefined;
934
+ };
935
+
936
+ const handleToolResult = (message: Message, now: number): void => {
937
+ const callId = toolResultCallId(message);
938
+ if (callId) {
939
+ if (handledToolResultIds.has(callId)) return;
940
+ if (handledToolResultIds.size >= MAX_HANDLED_TOOL_RESULT_IDS) {
941
+ const oldest = handledToolResultIds.values().next().value;
942
+ if (typeof oldest === "string") handledToolResultIds.delete(oldest);
943
+ }
944
+ handledToolResultIds.add(callId);
945
+ }
946
+ result.messages!.push(message);
947
+ const resultText = extractTextFromContent(message.content);
948
+ if (options.toolBudget && pendingToolResult && resultText.includes("Tool budget hard limit reached")) {
949
+ result.toolBudgetBlocked = true;
950
+ result.toolBudget = toolBudgetState(options.toolBudget, progress.toolCount, pendingToolResult.tool);
951
+ }
952
+ appendRecentOutput(progress, resultText.split("\n").slice(-10));
953
+ const toolSnapshot = pendingToolResult;
954
+ pendingToolResult = undefined;
955
+ if (toolSnapshot?.mutates && didMutatingToolFail(resultText)) {
956
+ recordMutatingFailure(mutatingFailures, {
957
+ tool: toolSnapshot.tool,
958
+ path: toolSnapshot.path,
959
+ error: resultText.split("\n").find((line) => line.trim())?.trim().slice(0, 180) ?? "mutating tool failed",
960
+ ts: now,
961
+ }, mutatingFailureWindowMs);
962
+ if (shouldEscalateMutatingFailures(mutatingFailures, controlConfig.failedToolAttemptsBeforeAttention)) {
963
+ emitNeedsAttention(now, {
964
+ message: `${agent.name} needs attention after repeated mutating tool failures`,
965
+ reason: "tool_failures",
966
+ currentTool: toolSnapshot.tool,
967
+ currentPath: toolSnapshot.path,
968
+ currentToolDurationMs: toolSnapshot.startedAt ? Math.max(0, now - toolSnapshot.startedAt) : undefined,
969
+ recentFailureSummary: summarizeRecentMutatingFailures(mutatingFailures),
970
+ });
971
+ }
972
+ } else if (toolSnapshot?.mutates) {
973
+ resetMutatingFailureState(mutatingFailures);
974
+ }
975
+ fireUpdate();
976
+ };
977
+
922
978
  const rawStdoutTail = createBoundedByteTail();
923
979
  const processLine = (line: string) => {
924
980
  if (!line.trim()) return;
@@ -1003,80 +1059,55 @@ async function runSingleAttempt(
1003
1059
  }
1004
1060
 
1005
1061
  if (evt.type === "message_end" && evt.message) {
1006
- result.messages!.push(evt.message);
1007
- if (evt.message.role === "assistant") {
1008
- result.usage.turns++;
1009
- progress.turnCount = result.usage.turns;
1010
- const stopReason = (evt.message as { stopReason?: string }).stopReason;
1011
- const toolCalls = Array.isArray(evt.message.content)
1012
- ? evt.message.content.filter((part) => (part as { type?: string }).type === "toolCall")
1013
- : [];
1014
- const hasToolCall = toolCalls.length > 0;
1015
- const terminalAssistantStop = stopReason === "stop" && !hasToolCall;
1016
- const terminalStructuredOutputCall = Boolean(options.structuredOutput)
1017
- && toolCalls.length === 1
1018
- && (toolCalls[0] as { name?: string }).name === "structured_output";
1019
- updateTurnBudget(result.usage.turns, terminalAssistantStop || terminalStructuredOutputCall, hasToolCall || Boolean(progress.currentTool));
1020
- const u = evt.message.usage;
1021
- if (u) {
1022
- result.usage.input += u.input || 0;
1023
- result.usage.output += u.output || 0;
1024
- result.usage.cacheRead += u.cacheRead || 0;
1025
- result.usage.cacheWrite += u.cacheWrite || 0;
1026
- result.usage.cost += u.cost?.total || 0;
1027
- progress.tokens = result.usage.input + result.usage.output;
1028
- progress.inputTokens = result.usage.input;
1029
- progress.outputTokens = result.usage.output;
1030
- }
1031
- if (evt.message.model) {
1032
- progress.model = evt.message.model;
1033
- if (!result.model) result.model = evt.message.model;
1034
- }
1035
- if (evt.message.errorMessage) assistantError = evt.message.errorMessage;
1036
- const assistantText = extractTextFromContent(evt.message.content);
1037
- appendRecentOutput(progress, assistantText.split("\n").slice(-10));
1038
- // Final assistant message: start the exit drain window.
1039
- if (terminalAssistantStop) {
1040
- if (!evt.message.errorMessage && assistantText.trim()) assistantError = undefined;
1041
- cleanTerminalAssistantStopReceived ||= !evt.message.errorMessage;
1042
- applyChildLifecycle(projectChildLifecycle(evt, true));
1062
+ if (evt.message.role === "toolResult") {
1063
+ handleToolResult(evt.message, now);
1064
+ } else {
1065
+ result.messages!.push(evt.message);
1066
+ if (evt.message.role === "assistant") {
1067
+ result.usage.turns++;
1068
+ progress.turnCount = result.usage.turns;
1069
+ const stopReason = (evt.message as { stopReason?: string }).stopReason;
1070
+ const toolCalls = Array.isArray(evt.message.content)
1071
+ ? evt.message.content.filter((part) => (part as { type?: string }).type === "toolCall")
1072
+ : [];
1073
+ const hasToolCall = toolCalls.length > 0;
1074
+ const terminalAssistantStop = stopReason === "stop" && !hasToolCall;
1075
+ const terminalStructuredOutputCall = Boolean(options.structuredOutput)
1076
+ && toolCalls.length === 1
1077
+ && (toolCalls[0] as { name?: string }).name === "structured_output";
1078
+ updateTurnBudget(result.usage.turns, terminalAssistantStop || terminalStructuredOutputCall, hasToolCall || Boolean(progress.currentTool));
1079
+ const u = evt.message.usage;
1080
+ if (u) {
1081
+ result.usage.input += u.input || 0;
1082
+ result.usage.output += u.output || 0;
1083
+ result.usage.cacheRead += u.cacheRead || 0;
1084
+ result.usage.cacheWrite += u.cacheWrite || 0;
1085
+ result.usage.cost += u.cost?.total || 0;
1086
+ progress.tokens = result.usage.input + result.usage.output;
1087
+ progress.inputTokens = result.usage.input;
1088
+ progress.outputTokens = result.usage.output;
1089
+ }
1090
+ if (evt.message.model) {
1091
+ progress.model = evt.message.model;
1092
+ if (!result.model) result.model = evt.message.model;
1093
+ }
1094
+ if (evt.message.errorMessage) assistantError = evt.message.errorMessage;
1095
+ const assistantText = extractTextFromContent(evt.message.content);
1096
+ appendRecentOutput(progress, assistantText.split("\n").slice(-10));
1097
+ // Final assistant message: start the exit drain window.
1098
+ if (terminalAssistantStop) {
1099
+ if (!evt.message.errorMessage && assistantText.trim()) assistantError = undefined;
1100
+ cleanTerminalAssistantStopReceived ||= !evt.message.errorMessage;
1101
+ applyChildLifecycle(projectChildLifecycle(evt, true));
1102
+ }
1043
1103
  }
1104
+ updateActivityState(now);
1105
+ fireUpdate();
1044
1106
  }
1045
- updateActivityState(now);
1046
- fireUpdate();
1047
1107
  }
1048
1108
 
1049
1109
  if (evt.type === "tool_result_end" && evt.message) {
1050
- result.messages!.push(evt.message);
1051
- const resultText = extractTextFromContent(evt.message.content);
1052
- if (options.toolBudget && pendingToolResult && resultText.includes("Tool budget hard limit reached")) {
1053
- result.toolBudgetBlocked = true;
1054
- result.toolBudget = toolBudgetState(options.toolBudget, progress.toolCount, pendingToolResult.tool);
1055
- }
1056
- appendRecentOutput(progress, resultText.split("\n").slice(-10));
1057
- const toolSnapshot = pendingToolResult;
1058
- pendingToolResult = undefined;
1059
- if (toolSnapshot?.mutates && didMutatingToolFail(resultText)) {
1060
- recordMutatingFailure(mutatingFailures, {
1061
- tool: toolSnapshot.tool,
1062
- path: toolSnapshot.path,
1063
- error: resultText.split("\n").find((line) => line.trim())?.trim().slice(0, 180) ?? "mutating tool failed",
1064
- ts: now,
1065
- }, mutatingFailureWindowMs);
1066
- if (shouldEscalateMutatingFailures(mutatingFailures, controlConfig.failedToolAttemptsBeforeAttention)) {
1067
- emitNeedsAttention(now, {
1068
- message: `${agent.name} needs attention after repeated mutating tool failures`,
1069
- reason: "tool_failures",
1070
- currentTool: toolSnapshot.tool,
1071
- currentPath: toolSnapshot.path,
1072
- currentToolDurationMs: toolSnapshot.startedAt ? Math.max(0, now - toolSnapshot.startedAt) : undefined,
1073
- recentFailureSummary: summarizeRecentMutatingFailures(mutatingFailures),
1074
- });
1075
- }
1076
- } else if (toolSnapshot?.mutates) {
1077
- resetMutatingFailureState(mutatingFailures);
1078
- }
1079
- fireUpdate();
1110
+ handleToolResult(evt.message, now);
1080
1111
  }
1081
1112
  };
1082
1113
 
@@ -1671,7 +1702,7 @@ async function runSyncCompletionInner(
1671
1702
  agent.fallbackModels,
1672
1703
  options.availableModels,
1673
1704
  options.preferredModelProvider,
1674
- { scope: options.modelScope },
1705
+ { scope: options.modelScope, primaryModelFromParent: options.modelOverrideFromParent },
1675
1706
  );
1676
1707
  const attemptedModels: string[] = [];
1677
1708
  const modelAttempts: ModelAttempt[] = [];
@@ -1858,7 +1889,9 @@ async function runSyncCompletionInner(
1858
1889
  attempt.error = startupError;
1859
1890
  break modelAttemptsLoop;
1860
1891
  }
1861
- if (!isRetryableModelFailure(result.error) || modelIndex === modelsToTry.length - 1) break modelAttemptsLoop;
1892
+ const retryableModelFailure = isRetryableModelFailure(result.error);
1893
+ if (retryableModelFailure) recordRetryableModelFailure(result.model ?? candidate, result.error);
1894
+ if (!retryableModelFailure || modelIndex === modelsToTry.length - 1) break modelAttemptsLoop;
1862
1895
  attemptNotes.push(formatModelAttemptNote(attempt, modelsToTry[modelIndex + 1]));
1863
1896
  break;
1864
1897
  }