pi-crew 0.1.41 → 0.1.44

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 (191) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/README.md +51 -0
  3. package/agents/analyst.md +11 -11
  4. package/agents/critic.md +11 -11
  5. package/agents/executor.md +11 -11
  6. package/agents/explorer.md +11 -11
  7. package/agents/planner.md +11 -11
  8. package/agents/reviewer.md +11 -11
  9. package/agents/security-reviewer.md +11 -11
  10. package/agents/test-engineer.md +11 -11
  11. package/agents/verifier.md +11 -11
  12. package/agents/writer.md +11 -11
  13. package/docs/refactor-tasks-phase3.md +394 -394
  14. package/docs/refactor-tasks-phase4.md +564 -564
  15. package/docs/refactor-tasks-phase5.md +402 -402
  16. package/docs/refactor-tasks-phase6.md +662 -662
  17. package/docs/research-extension-examples.md +297 -297
  18. package/docs/research-extension-system.md +324 -324
  19. package/docs/research-optimization-plan.md +548 -548
  20. package/docs/research-phase10-distillation.md +199 -0
  21. package/docs/research-phase11-distillation.md +201 -0
  22. package/docs/research-pi-coding-agent.md +357 -357
  23. package/docs/research-source-pi-crew-reference.md +174 -174
  24. package/docs/runtime-flow.md +148 -148
  25. package/docs/source-runtime-refactor-map.md +83 -83
  26. package/index.ts +6 -6
  27. package/package.json +1 -1
  28. package/src/agents/agent-serializer.ts +34 -34
  29. package/src/agents/discover-agents.ts +5 -4
  30. package/src/config/config.ts +28 -4
  31. package/src/extension/cross-extension-rpc.ts +82 -82
  32. package/src/extension/management.ts +37 -8
  33. package/src/extension/notification-router.ts +2 -2
  34. package/src/extension/register.ts +130 -8
  35. package/src/extension/registration/commands.ts +11 -9
  36. package/src/extension/registration/compaction-guard.ts +125 -125
  37. package/src/extension/registration/subagent-tools.ts +28 -19
  38. package/src/extension/registration/team-tool.ts +2 -1
  39. package/src/extension/result-watcher.ts +4 -4
  40. package/src/extension/run-bundle-schema.ts +8 -4
  41. package/src/extension/run-import.ts +4 -0
  42. package/src/extension/run-index.ts +23 -1
  43. package/src/extension/run-maintenance.ts +43 -24
  44. package/src/extension/team-tool/api.ts +2 -2
  45. package/src/extension/team-tool/cancel.ts +76 -4
  46. package/src/extension/team-tool/context.ts +1 -0
  47. package/src/extension/team-tool/doctor.ts +8 -1
  48. package/src/extension/team-tool/handle-settings.ts +188 -0
  49. package/src/extension/team-tool/inspect.ts +41 -41
  50. package/src/extension/team-tool/lifecycle-actions.ts +79 -79
  51. package/src/extension/team-tool/plan.ts +19 -19
  52. package/src/extension/team-tool/respond.ts +67 -0
  53. package/src/extension/team-tool/run.ts +6 -4
  54. package/src/extension/team-tool/status.ts +99 -93
  55. package/src/extension/team-tool-types.ts +4 -0
  56. package/src/extension/team-tool.ts +5 -1
  57. package/src/i18n.ts +184 -0
  58. package/src/observability/correlation.ts +2 -2
  59. package/src/observability/event-to-metric.ts +10 -3
  60. package/src/observability/exporters/adapter.ts +7 -1
  61. package/src/observability/exporters/otlp-exporter.ts +14 -2
  62. package/src/observability/exporters/prometheus-exporter.ts +9 -2
  63. package/src/observability/metric-registry.ts +18 -3
  64. package/src/observability/metric-retention.ts +11 -3
  65. package/src/observability/metric-sink.ts +9 -4
  66. package/src/observability/metrics-primitives.ts +4 -3
  67. package/src/prompt/prompt-runtime.ts +72 -68
  68. package/src/runtime/agent-control.ts +63 -63
  69. package/src/runtime/agent-memory.ts +72 -72
  70. package/src/runtime/agent-observability.ts +114 -114
  71. package/src/runtime/async-marker.ts +26 -26
  72. package/src/runtime/attention-events.ts +28 -23
  73. package/src/runtime/background-runner.ts +53 -53
  74. package/src/runtime/child-pi.ts +4 -4
  75. package/src/runtime/completion-guard.ts +95 -4
  76. package/src/runtime/concurrency.ts +1 -1
  77. package/src/runtime/crash-recovery.ts +32 -1
  78. package/src/runtime/crew-agent-runtime.ts +59 -58
  79. package/src/runtime/deadletter.ts +14 -4
  80. package/src/runtime/delivery-coordinator.ts +143 -0
  81. package/src/runtime/direct-run.ts +35 -35
  82. package/src/runtime/foreground-control.ts +82 -82
  83. package/src/runtime/green-contract.ts +46 -46
  84. package/src/runtime/group-join.ts +106 -106
  85. package/src/runtime/heartbeat-gradient.ts +28 -28
  86. package/src/runtime/heartbeat-watcher.ts +48 -4
  87. package/src/runtime/live-agent-control.ts +87 -87
  88. package/src/runtime/live-agent-manager.ts +85 -85
  89. package/src/runtime/live-control-realtime.ts +36 -36
  90. package/src/runtime/live-session-runtime.ts +305 -305
  91. package/src/runtime/manifest-cache.ts +2 -2
  92. package/src/runtime/model-fallback.ts +272 -261
  93. package/src/runtime/overflow-recovery.ts +157 -0
  94. package/src/runtime/parallel-research.ts +44 -44
  95. package/src/runtime/parallel-utils.ts +1 -1
  96. package/src/runtime/pi-json-output.ts +111 -111
  97. package/src/runtime/policy-engine.ts +79 -78
  98. package/src/runtime/post-exit-stdio-guard.ts +2 -2
  99. package/src/runtime/process-status.ts +56 -56
  100. package/src/runtime/progress-event-coalescer.ts +43 -43
  101. package/src/runtime/recovery-recipes.ts +74 -74
  102. package/src/runtime/retry-executor.ts +5 -0
  103. package/src/runtime/role-permission.ts +39 -39
  104. package/src/runtime/runtime-resolver.ts +1 -1
  105. package/src/runtime/session-resources.ts +25 -0
  106. package/src/runtime/session-snapshot.ts +59 -0
  107. package/src/runtime/session-usage.ts +79 -79
  108. package/src/runtime/sidechain-output.ts +29 -29
  109. package/src/runtime/stale-reconciler.ts +179 -0
  110. package/src/runtime/subagent-manager.ts +3 -3
  111. package/src/runtime/supervisor-contact.ts +59 -0
  112. package/src/runtime/task-display.ts +38 -38
  113. package/src/runtime/task-output-context.ts +127 -127
  114. package/src/runtime/task-runner/live-executor.ts +101 -101
  115. package/src/runtime/task-runner/progress.ts +119 -111
  116. package/src/runtime/task-runner/result-utils.ts +14 -14
  117. package/src/runtime/task-runner/state-helpers.ts +22 -22
  118. package/src/runtime/task-runner.ts +14 -0
  119. package/src/runtime/team-runner.ts +9 -10
  120. package/src/runtime/worker-heartbeat.ts +21 -21
  121. package/src/runtime/worker-startup.ts +57 -57
  122. package/src/schema/config-schema.ts +2 -1
  123. package/src/schema/team-tool-schema.ts +115 -109
  124. package/src/state/artifact-store.ts +4 -2
  125. package/src/state/atomic-write.ts +12 -4
  126. package/src/state/contracts.ts +109 -105
  127. package/src/state/event-log.ts +3 -4
  128. package/src/state/jsonl-writer.ts +4 -1
  129. package/src/state/locks.ts +9 -1
  130. package/src/state/task-claims.ts +44 -42
  131. package/src/state/usage.ts +29 -29
  132. package/src/subagents/async-entry.ts +1 -1
  133. package/src/subagents/index.ts +3 -3
  134. package/src/subagents/live/control.ts +1 -1
  135. package/src/subagents/live/manager.ts +1 -1
  136. package/src/subagents/live/realtime.ts +1 -1
  137. package/src/subagents/live/session-runtime.ts +1 -1
  138. package/src/subagents/manager.ts +1 -1
  139. package/src/subagents/spawn.ts +1 -1
  140. package/src/teams/discover-teams.ts +2 -2
  141. package/src/teams/team-serializer.ts +38 -38
  142. package/src/types/diff.d.ts +18 -18
  143. package/src/ui/crew-footer.ts +101 -101
  144. package/src/ui/crew-select-list.ts +111 -111
  145. package/src/ui/crew-widget.ts +5 -4
  146. package/src/ui/dashboard-panes/metrics-pane.ts +34 -34
  147. package/src/ui/dynamic-border.ts +25 -25
  148. package/src/ui/layout-primitives.ts +106 -106
  149. package/src/ui/live-run-sidebar.ts +1 -1
  150. package/src/ui/loaders.ts +158 -158
  151. package/src/ui/mascot.ts +3 -2
  152. package/src/ui/powerbar-publisher.ts +7 -6
  153. package/src/ui/render-diff.ts +119 -119
  154. package/src/ui/render-scheduler.ts +54 -14
  155. package/src/ui/run-dashboard.ts +39 -11
  156. package/src/ui/run-snapshot-cache.ts +336 -36
  157. package/src/ui/spinner.ts +17 -17
  158. package/src/ui/status-colors.ts +58 -54
  159. package/src/ui/syntax-highlight.ts +116 -116
  160. package/src/ui/theme-adapter.ts +1 -1
  161. package/src/ui/transcript-viewer.ts +7 -2
  162. package/src/utils/atomic-write.ts +33 -0
  163. package/src/utils/completion-dedupe.ts +63 -63
  164. package/src/utils/file-coalescer.ts +5 -3
  165. package/src/utils/frontmatter.ts +68 -36
  166. package/src/utils/git.ts +262 -262
  167. package/src/utils/ids.ts +12 -12
  168. package/src/utils/internal-error.ts +1 -1
  169. package/src/utils/names.ts +27 -26
  170. package/src/utils/paths.ts +1 -1
  171. package/src/utils/redaction.ts +44 -41
  172. package/src/utils/safe-paths.ts +47 -34
  173. package/src/utils/sleep.ts +2 -2
  174. package/src/utils/timings.ts +2 -0
  175. package/src/utils/visual.ts +9 -1
  176. package/src/workflows/discover-workflows.ts +4 -1
  177. package/src/workflows/validate-workflow.ts +40 -40
  178. package/src/worktree/branch-freshness.ts +45 -45
  179. package/src/worktree/worktree-manager.ts +6 -1
  180. package/teams/default.team.md +12 -12
  181. package/teams/fast-fix.team.md +11 -11
  182. package/teams/implementation.team.md +18 -18
  183. package/teams/parallel-research.team.md +14 -14
  184. package/teams/research.team.md +11 -11
  185. package/teams/review.team.md +12 -12
  186. package/workflows/default.workflow.md +29 -29
  187. package/workflows/fast-fix.workflow.md +22 -22
  188. package/workflows/implementation.workflow.md +38 -38
  189. package/workflows/parallel-research.workflow.md +46 -46
  190. package/workflows/research.workflow.md +22 -22
  191. package/workflows/review.workflow.md +30 -30
@@ -1,111 +1,119 @@
1
- import type { UsageState } from "../../state/types.ts";
2
- import type { CrewAgentProgress } from "../crew-agent-runtime.ts";
3
- import { emptyCrewAgentProgress } from "../crew-agent-records.ts";
4
- import type { ProgressEventSummary } from "../progress-event-coalescer.ts";
5
- import type { TeamTaskState } from "../../state/types.ts";
6
-
7
- function asRecord(value: unknown): Record<string, unknown> | undefined {
8
- return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : undefined;
9
- }
10
-
11
- function textFromContent(content: unknown): string[] {
12
- if (typeof content === "string") return [content];
13
- if (!Array.isArray(content)) return [];
14
- const text: string[] = [];
15
- for (const part of content) {
16
- const obj = asRecord(part);
17
- if (!obj) continue;
18
- if (obj.type === "text" && typeof obj.text === "string") text.push(obj.text);
19
- else if (typeof obj.content === "string") text.push(obj.content);
20
- }
21
- return text;
22
- }
23
-
24
- function eventText(event: unknown): string[] {
25
- const obj = asRecord(event);
26
- if (!obj) return [];
27
- const text: string[] = [];
28
- if (typeof obj.text === "string") text.push(obj.text);
29
- if (typeof obj.output === "string") text.push(obj.output);
30
- text.push(...textFromContent(obj.content));
31
- const message = asRecord(obj.message);
32
- if (message) text.push(...textFromContent(message.content));
33
- return text.filter((entry) => entry.trim());
34
- }
35
-
36
- function numberField(obj: Record<string, unknown>, keys: string[]): number | undefined {
37
- for (const key of keys) {
38
- const value = obj[key];
39
- if (typeof value === "number" && Number.isFinite(value)) return value;
40
- }
41
- return undefined;
42
- }
43
-
44
- function eventUsage(event: unknown): { input?: number; output?: number; turns?: number } | undefined {
45
- const obj = asRecord(event);
46
- if (!obj) return undefined;
47
- const direct = { input: numberField(obj, ["input", "inputTokens", "input_tokens"]), output: numberField(obj, ["output", "outputTokens", "output_tokens"]), turns: numberField(obj, ["turns", "turnCount", "turn_count"]) };
48
- if (Object.values(direct).some((value) => value !== undefined)) return direct;
49
- for (const key of ["usage", "tokenUsage", "tokens", "stats"]) {
50
- const nested = eventUsage(obj[key]);
51
- if (nested) return nested;
52
- }
53
- const message = asRecord(obj.message);
54
- return message ? eventUsage(message.usage) : undefined;
55
- }
56
-
57
- function previewArgs(args: unknown): string | undefined {
58
- if (!args) return undefined;
59
- try {
60
- const text = typeof args === "string" ? args : JSON.stringify(args);
61
- return text.length > 240 ? `${text.slice(0, 240)}…` : text;
62
- } catch {
63
- return undefined;
64
- }
65
- }
66
-
67
- export function applyUsageToProgress(progress: CrewAgentProgress | undefined, usage: UsageState | undefined): CrewAgentProgress | undefined {
68
- if (!usage) return progress;
69
- const base = progress ?? emptyCrewAgentProgress();
70
- return { ...base, tokens: (usage.input ?? 0) + (usage.output ?? 0) + (usage.cacheRead ?? 0) + (usage.cacheWrite ?? 0), turns: usage.turns ?? base.turns };
71
- }
72
-
73
- export function shouldFlushProgressEvent(event: unknown): boolean {
74
- const type = asRecord(event)?.type;
75
- return type === "tool_execution_start" || type === "tool_execution_end" || type === "message_end" || type === "tool_result_end";
76
- }
77
-
78
- export function progressEventSummary(task: TeamTaskState, event: unknown): ProgressEventSummary {
79
- const type = asRecord(event)?.type;
80
- return { eventType: typeof type === "string" ? type : "event", currentTool: task.agentProgress?.currentTool, toolCount: task.agentProgress?.toolCount, tokens: task.agentProgress?.tokens, turns: task.agentProgress?.turns, activityState: task.agentProgress?.activityState, lastActivityAt: task.agentProgress?.lastActivityAt };
81
- }
82
-
83
- export function applyAgentProgressEvent(progress: CrewAgentProgress, event: unknown, startedAt: string | undefined): CrewAgentProgress {
84
- const obj = asRecord(event);
85
- const now = new Date().toISOString();
86
- const next: CrewAgentProgress = { ...progress, recentTools: [...progress.recentTools], recentOutput: [...progress.recentOutput], lastActivityAt: now, activityState: "active" };
87
- if (startedAt) next.durationMs = Date.now() - new Date(startedAt).getTime();
88
- if (obj?.type === "tool_execution_start") {
89
- next.toolCount += 1;
90
- next.currentTool = typeof obj.toolName === "string" ? obj.toolName : typeof obj.name === "string" ? obj.name : "tool";
91
- next.currentToolArgs = previewArgs(obj.args);
92
- next.currentToolStartedAt = now;
93
- }
94
- if (obj?.type === "tool_execution_end") {
95
- if (next.currentTool) next.recentTools.push({ tool: next.currentTool, args: next.currentToolArgs, endedAt: now });
96
- next.currentTool = undefined;
97
- next.currentToolArgs = undefined;
98
- next.currentToolStartedAt = undefined;
99
- }
100
- if ((obj?.type === "tool_execution_error" || obj?.type === "tool_execution_failed") && next.currentTool) next.failedTool = next.currentTool;
101
- const usage = eventUsage(event);
102
- if (usage) {
103
- next.tokens = (usage.input ?? 0) + (usage.output ?? 0);
104
- next.turns = usage.turns ?? next.turns;
105
- }
106
- const text = eventText(event);
107
- if (text.length > 0) next.recentOutput.push(...text.flatMap((entry) => entry.split(/\r?\n/)).filter(Boolean).slice(-10));
108
- if (next.recentTools.length > 25) next.recentTools.splice(0, next.recentTools.length - 25);
109
- if (next.recentOutput.length > 50) next.recentOutput.splice(0, next.recentOutput.length - 50);
110
- return next;
111
- }
1
+ import type { UsageState } from "../../state/types.ts";
2
+ import type { CrewAgentProgress } from "../crew-agent-runtime.ts";
3
+ import { emptyCrewAgentProgress } from "../crew-agent-records.ts";
4
+ import type { ProgressEventSummary } from "../progress-event-coalescer.ts";
5
+ import type { TeamTaskState } from "../../state/types.ts";
6
+
7
+ function asRecord(value: unknown): Record<string, unknown> | undefined {
8
+ return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : undefined;
9
+ }
10
+
11
+ function safeNum(v: number | undefined): number {
12
+ return Number.isFinite(v) ? v! : 0;
13
+ }
14
+
15
+ function textFromContent(content: unknown): string[] {
16
+ if (typeof content === "string") return [content];
17
+ if (!Array.isArray(content)) return [];
18
+ const text: string[] = [];
19
+ for (const part of content) {
20
+ const obj = asRecord(part);
21
+ if (!obj) continue;
22
+ if (obj.type === "text" && typeof obj.text === "string") text.push(obj.text);
23
+ else if (typeof obj.content === "string") text.push(obj.content);
24
+ }
25
+ return text;
26
+ }
27
+
28
+ function eventText(event: unknown): string[] {
29
+ const obj = asRecord(event);
30
+ if (!obj) return [];
31
+ const text: string[] = [];
32
+ if (typeof obj.text === "string") text.push(obj.text);
33
+ if (typeof obj.output === "string") text.push(obj.output);
34
+ text.push(...textFromContent(obj.content));
35
+ const message = asRecord(obj.message);
36
+ if (message) text.push(...textFromContent(message.content));
37
+ return text.filter((entry) => entry.trim());
38
+ }
39
+
40
+ function numberField(obj: Record<string, unknown>, keys: string[]): number | undefined {
41
+ for (const key of keys) {
42
+ const value = obj[key];
43
+ if (typeof value === "number" && Number.isFinite(value)) return value;
44
+ }
45
+ return undefined;
46
+ }
47
+
48
+ function eventUsage(event: unknown): { input?: number; output?: number; turns?: number } | undefined {
49
+ const obj = asRecord(event);
50
+ if (!obj) return undefined;
51
+ const direct = { input: numberField(obj, ["input", "inputTokens", "input_tokens"]), output: numberField(obj, ["output", "outputTokens", "output_tokens"]), turns: numberField(obj, ["turns", "turnCount", "turn_count"]) };
52
+ if (Object.values(direct).some((value) => value !== undefined)) return direct;
53
+ for (const key of ["usage", "tokenUsage", "tokens", "stats"]) {
54
+ const nested = eventUsage(obj[key]);
55
+ if (nested) return nested;
56
+ }
57
+ const message = asRecord(obj.message);
58
+ return message ? eventUsage(message.usage) : undefined;
59
+ }
60
+
61
+ function previewArgs(args: unknown): string | undefined {
62
+ if (!args) return undefined;
63
+ try {
64
+ const text = typeof args === "string" ? args : JSON.stringify(args);
65
+ return text.length > 240 ? `${text.slice(0, 240)}…` : text;
66
+ } catch {
67
+ return undefined;
68
+ }
69
+ }
70
+
71
+ export function applyUsageToProgress(progress: CrewAgentProgress | undefined, usage: UsageState | undefined): CrewAgentProgress | undefined {
72
+ if (!usage) return progress;
73
+ const base = progress ?? emptyCrewAgentProgress();
74
+ const tokens = safeNum(usage.input) + safeNum(usage.output) + safeNum(usage.cacheRead) + safeNum(usage.cacheWrite);
75
+ return { ...base, tokens, turns: usage.turns ?? base.turns };
76
+ }
77
+
78
+ export function shouldFlushProgressEvent(event: unknown): boolean {
79
+ const type = asRecord(event)?.type;
80
+ return type === "tool_execution_start" || type === "tool_execution_end" || type === "message_end" || type === "tool_result_end";
81
+ }
82
+
83
+ export function progressEventSummary(task: TeamTaskState, event: unknown): ProgressEventSummary {
84
+ const type = asRecord(event)?.type;
85
+ return { eventType: typeof type === "string" ? type : "event", currentTool: task.agentProgress?.currentTool, toolCount: task.agentProgress?.toolCount, tokens: task.agentProgress?.tokens, turns: task.agentProgress?.turns, activityState: task.agentProgress?.activityState, lastActivityAt: task.agentProgress?.lastActivityAt };
86
+ }
87
+
88
+ export function applyAgentProgressEvent(progress: CrewAgentProgress, event: unknown, startedAt: string | undefined): CrewAgentProgress {
89
+ const obj = asRecord(event);
90
+ const now = new Date().toISOString();
91
+ const next: CrewAgentProgress = { ...progress, recentTools: [...progress.recentTools], recentOutput: [...progress.recentOutput], lastActivityAt: now, activityState: "active" };
92
+ if (startedAt) {
93
+ const startMs = new Date(startedAt).getTime();
94
+ next.durationMs = Number.isFinite(startMs) ? Date.now() - startMs : undefined;
95
+ }
96
+ if (obj?.type === "tool_execution_start") {
97
+ next.toolCount += 1;
98
+ next.currentTool = typeof obj.toolName === "string" ? obj.toolName : typeof obj.name === "string" ? obj.name : "tool";
99
+ next.currentToolArgs = previewArgs(obj.args);
100
+ next.currentToolStartedAt = now;
101
+ }
102
+ if (obj?.type === "tool_execution_end") {
103
+ if (next.currentTool) next.recentTools.push({ tool: next.currentTool, args: next.currentToolArgs, endedAt: now });
104
+ next.currentTool = undefined;
105
+ next.currentToolArgs = undefined;
106
+ next.currentToolStartedAt = undefined;
107
+ }
108
+ if ((obj?.type === "tool_execution_error" || obj?.type === "tool_execution_failed") && next.currentTool) next.failedTool = next.currentTool;
109
+ const usage = eventUsage(event);
110
+ if (usage) {
111
+ next.tokens = safeNum(usage.input) + safeNum(usage.output);
112
+ next.turns = usage.turns ?? next.turns;
113
+ }
114
+ const text = eventText(event);
115
+ if (text.length > 0) next.recentOutput.push(...text.flatMap((entry) => entry.split(/\r?\n/)).filter(Boolean).slice(-10));
116
+ if (next.recentTools.length > 25) next.recentTools.splice(0, next.recentTools.length - 25);
117
+ if (next.recentOutput.length > 50) next.recentOutput.splice(0, next.recentOutput.length - 50);
118
+ return next;
119
+ }
@@ -1,14 +1,14 @@
1
- export function cleanResultText(text: string | undefined): string | undefined {
2
- const trimmed = text?.trim();
3
- if (!trimmed) return undefined;
4
- const doneIndex = trimmed.lastIndexOf("\nDONE\n");
5
- if (doneIndex >= 0) return trimmed.slice(doneIndex + 1).trim();
6
- if (trimmed === "DONE" || trimmed.startsWith("DONE\n")) return trimmed;
7
- const fencedPromptIndex = trimmed.lastIndexOf("</file>");
8
- if (fencedPromptIndex >= 0 && fencedPromptIndex < trimmed.length - 7) return trimmed.slice(fencedPromptIndex + 7).trim() || trimmed;
9
- return trimmed;
10
- }
11
-
12
- export function isFinalChildEvent(event: unknown): boolean {
13
- return Boolean(event && typeof event === "object" && !Array.isArray(event) && (event as Record<string, unknown>).type === "message_end");
14
- }
1
+ export function cleanResultText(text: string | undefined): string | undefined {
2
+ const trimmed = text?.trim();
3
+ if (!trimmed) return undefined;
4
+ const doneIndex = trimmed.lastIndexOf("\nDONE\n");
5
+ if (doneIndex >= 0) return trimmed.slice(doneIndex + 1).trim();
6
+ if (trimmed === "DONE" || trimmed.startsWith("DONE\n")) return trimmed;
7
+ const fencedPromptIndex = trimmed.lastIndexOf("</file>");
8
+ if (fencedPromptIndex >= 0 && fencedPromptIndex < trimmed.length - 7) return trimmed.slice(fencedPromptIndex + 7).trim() || trimmed;
9
+ return trimmed;
10
+ }
11
+
12
+ export function isFinalChildEvent(event: unknown): boolean {
13
+ return Boolean(event && typeof event === "object" && !Array.isArray(event) && (event as Record<string, unknown>).type === "message_end");
14
+ }
@@ -1,22 +1,22 @@
1
- import type { TaskCheckpointState, TeamRunManifest, TeamTaskState } from "../../state/types.ts";
2
- import { loadRunManifestById, saveRunTasks } from "../../state/state-store.ts";
3
- import { recordFromTask, upsertCrewAgent } from "../crew-agent-records.ts";
4
-
5
- export function updateTask(tasks: TeamTaskState[], updated: TeamTaskState): TeamTaskState[] {
6
- return tasks.map((task) => task.id === updated.id ? updated : task);
7
- }
8
-
9
- export function persistSingleTaskUpdate(manifest: TeamRunManifest, fallbackTasks: TeamTaskState[], updated: TeamTaskState): TeamTaskState[] {
10
- const latest = loadRunManifestById(manifest.cwd, manifest.runId)?.tasks ?? fallbackTasks;
11
- const merged = updateTask(latest, updated);
12
- saveRunTasks(manifest, merged);
13
- return merged;
14
- }
15
-
16
- export function checkpointTask(manifest: TeamRunManifest, tasks: TeamTaskState[], task: TeamTaskState, phase: TaskCheckpointState["phase"], childPid?: number): { task: TeamTaskState; tasks: TeamTaskState[] } {
17
- const checkpoint: TaskCheckpointState = { phase, updatedAt: new Date().toISOString(), ...(childPid ? { childPid } : task.checkpoint?.childPid ? { childPid: task.checkpoint.childPid } : {}) };
18
- const nextTask = { ...task, checkpoint };
19
- const nextTasks = persistSingleTaskUpdate(manifest, updateTask(tasks, nextTask), nextTask);
20
- upsertCrewAgent(manifest, recordFromTask(manifest, nextTask, "child-process"));
21
- return { task: nextTask, tasks: nextTasks };
22
- }
1
+ import type { TaskCheckpointState, TeamRunManifest, TeamTaskState } from "../../state/types.ts";
2
+ import { loadRunManifestById, saveRunTasks } from "../../state/state-store.ts";
3
+ import { recordFromTask, upsertCrewAgent } from "../crew-agent-records.ts";
4
+
5
+ export function updateTask(tasks: TeamTaskState[], updated: TeamTaskState): TeamTaskState[] {
6
+ return tasks.map((task) => task.id === updated.id ? updated : task);
7
+ }
8
+
9
+ export function persistSingleTaskUpdate(manifest: TeamRunManifest, fallbackTasks: TeamTaskState[], updated: TeamTaskState): TeamTaskState[] {
10
+ const latest = loadRunManifestById(manifest.cwd, manifest.runId)?.tasks ?? fallbackTasks;
11
+ const merged = updateTask(latest, updated);
12
+ saveRunTasks(manifest, merged);
13
+ return merged;
14
+ }
15
+
16
+ export function checkpointTask(manifest: TeamRunManifest, tasks: TeamTaskState[], task: TeamTaskState, phase: TaskCheckpointState["phase"], childPid?: number): { task: TeamTaskState; tasks: TeamTaskState[] } {
17
+ const checkpoint: TaskCheckpointState = { phase, updatedAt: new Date().toISOString(), ...(childPid ? { childPid } : task.checkpoint?.childPid ? { childPid: task.checkpoint.childPid } : {}) };
18
+ const nextTask = { ...task, checkpoint };
19
+ const nextTasks = persistSingleTaskUpdate(manifest, updateTask(tasks, nextTask), nextTask);
20
+ upsertCrewAgent(manifest, recordFromTask(manifest, nextTask, "child-process"));
21
+ return { task: nextTask, tasks: nextTasks };
22
+ }
@@ -27,6 +27,7 @@ import { checkpointTask, persistSingleTaskUpdate, updateTask } from "./task-runn
27
27
  import { cleanResultText, isFinalChildEvent } from "./task-runner/result-utils.ts";
28
28
  import { evaluateCompletionMutationGuard } from "./completion-guard.ts";
29
29
  import { appendTaskAttentionEvent } from "./attention-events.ts";
30
+ import { parseSupervisorContactFromLine, recordSupervisorContact } from "./supervisor-contact.ts";
30
31
 
31
32
  export interface TaskRunnerInput {
32
33
  manifest: TeamRunManifest;
@@ -44,6 +45,8 @@ export interface TaskRunnerInput {
44
45
  modelOverride?: string;
45
46
  limits?: CrewLimitsConfig;
46
47
  dependencyContextText?: string;
48
+ /** Optional callback for JSON events from child Pi. Used for overflow recovery tracking. */
49
+ onJsonEvent?: (taskId: string, runId: string, event: unknown) => void;
47
50
  }
48
51
 
49
52
  export async function runTeamTask(input: TaskRunnerInput): Promise<{ manifest: TeamRunManifest; tasks: TeamTaskState[] }> {
@@ -154,12 +157,23 @@ export async function runTeamTask(input: TaskRunnerInput): Promise<{ manifest: T
154
157
  onStdoutLine: (line) => {
155
158
  appendCrewAgentOutput(manifest, task.id, line);
156
159
  persistHeartbeat();
160
+ // Check for supervisor contact requests from child Pi
161
+ const contact = parseSupervisorContactFromLine(line);
162
+ if (contact) {
163
+ recordSupervisorContact(manifest, { runId: manifest.runId, ...contact });
164
+ }
157
165
  },
158
166
  onJsonEvent: (event) => {
159
167
  appendCrewAgentEvent(manifest, task.id, event);
160
168
  persistHeartbeat();
161
169
  task = { ...task, agentProgress: applyAgentProgressEvent(task.agentProgress ?? emptyCrewAgentProgress(), event, task.startedAt) };
162
170
  tasks = updateTask(tasks, task);
171
+ // Feed overflow recovery tracker
172
+ if (input.onJsonEvent) {
173
+ try {
174
+ input.onJsonEvent(task.id, manifest.runId, event);
175
+ } catch { /* overflow tracking errors should not affect task */ }
176
+ }
163
177
  if (!finalCheckpointWritten && isFinalChildEvent(event)) {
164
178
  finalCheckpointWritten = true;
165
179
  ({ task, tasks } = checkpointTask(manifest, tasks, task, "child-stdout-final"));
@@ -11,7 +11,7 @@ import { aggregateUsage, formatUsage } from "../state/usage.ts";
11
11
  import type { WorkflowConfig, WorkflowStep } from "../workflows/workflow-config.ts";
12
12
  import { evaluateCrewPolicy, summarizePolicyDecisions } from "./policy-engine.ts";
13
13
  import { buildRecoveryLedger } from "./recovery-recipes.ts";
14
- import { buildTaskGraphIndex, getReadyTasks, refreshTaskGraphQueues, taskGraphSnapshot } from "./task-graph-scheduler.ts";
14
+ import { buildTaskGraphIndex, refreshTaskGraphQueues, taskGraphSnapshot } from "./task-graph-scheduler.ts";
15
15
  import { checkBranchFreshness } from "../worktree/branch-freshness.ts";
16
16
  import { aggregateTaskOutputs } from "./task-output-context.ts";
17
17
  import { saveCrewAgents } from "./crew-agent-records.ts";
@@ -43,10 +43,8 @@ export interface ExecuteTeamRunInput {
43
43
  signal?: AbortSignal;
44
44
  reliability?: CrewReliabilityConfig;
45
45
  metricRegistry?: MetricRegistry;
46
- }
47
-
48
- function findReadyTask(tasks: TeamTaskState[]): TeamTaskState | undefined {
49
- return getReadyTasks(tasks, 1)[0];
46
+ /** Optional callback for JSON events from child Pi. Used for overflow recovery tracking. */
47
+ onJsonEvent?: (taskId: string, runId: string, event: unknown) => void;
50
48
  }
51
49
 
52
50
  function findStep(workflow: WorkflowConfig, task: TeamTaskState): WorkflowStep {
@@ -72,7 +70,7 @@ function mergeArtifacts(items: ArtifactDescriptor[]): ArtifactDescriptor[] {
72
70
  }
73
71
 
74
72
  function isNonTerminalTaskStatus(status: TeamTaskState["status"]): boolean {
75
- return status === "queued" || status === "running";
73
+ return status === "queued" || status === "running" || status === "waiting";
76
74
  }
77
75
 
78
76
  function shouldMergeTaskUpdate(current: TeamTaskState, updated: TeamTaskState): boolean {
@@ -487,7 +485,7 @@ function ensurePlanApprovalRequested(manifest: TeamRunManifest, tasks: TeamTaskS
487
485
  }
488
486
 
489
487
  function cancelPlanTasks(tasks: TeamTaskState[], reason: string): TeamTaskState[] {
490
- return tasks.map((task) => task.status === "queued" || task.status === "running" ? { ...task, status: "cancelled", finishedAt: new Date().toISOString(), error: reason, graph: task.graph ? { ...task.graph, queue: "done" } : undefined } : task);
488
+ return tasks.map((task) => task.status === "queued" || task.status === "running" || task.status === "waiting" ? { ...task, status: "cancelled", finishedAt: new Date().toISOString(), error: reason, graph: task.graph ? { ...task.graph, queue: "done" } : undefined } : task);
491
489
  }
492
490
 
493
491
  function hasPendingMutatingAdaptiveTask(tasks: TeamTaskState[]): boolean {
@@ -537,7 +535,7 @@ export async function executeTeamRun(input: ExecuteTeamRunInput): Promise<{ mani
537
535
 
538
536
  while (tasks.some((task) => task.status === "queued")) {
539
537
  if (input.signal?.aborted) {
540
- tasks = tasks.map((task) => task.status === "queued" || task.status === "running" ? { ...task, status: "cancelled", finishedAt: new Date().toISOString(), error: "Run cancelled." } : task);
538
+ tasks = tasks.map((task) => task.status === "queued" || task.status === "running" || task.status === "waiting" ? { ...task, status: "cancelled", finishedAt: new Date().toISOString(), error: "Run cancelled." } : task);
541
539
  await saveRunTasksAsync(manifest, tasks);
542
540
  manifest = updateRunStatus(manifest, "cancelled", "Run cancelled.");
543
541
  return { manifest, tasks };
@@ -559,7 +557,8 @@ export async function executeTeamRun(input: ExecuteTeamRunInput): Promise<{ mani
559
557
  appendEvent(manifest.eventsPath, { type: "limits.unbounded", runId: manifest.runId, message: "Unbounded worker concurrency was explicitly enabled for this run.", data: { concurrencyReason: concurrency.reason, maxConcurrent: concurrency.maxConcurrent } });
560
558
  }
561
559
  const approvalPending = isPlanApprovalPending(manifest);
562
- const candidateBatch = approvalPending ? getReadyTasks(tasks, tasks.length, queueIndex) : getReadyTasks(tasks, concurrency.selectedCount, queueIndex);
560
+ const readyIds = approvalPending ? snapshot.ready : snapshot.ready.slice(0, concurrency.selectedCount);
561
+ const candidateBatch = readyIds.map((id) => tasks.find((task) => task.id === id)).filter((task): task is TeamTaskState => Boolean(task));
563
562
  const readyBatch = approvalPending ? candidateBatch.filter((task) => !isMutatingTask(task)).slice(0, concurrency.selectedCount) : candidateBatch;
564
563
  if (readyBatch.length === 0) {
565
564
  if (approvalPending && candidateBatch.some(isMutatingTask)) {
@@ -582,7 +581,7 @@ export async function executeTeamRun(input: ExecuteTeamRunInput): Promise<{ mani
582
581
  async (task) => {
583
582
  const step = findStep(workflow, task);
584
583
  const agent = findAgent(input.agents, task);
585
- const baseInput = { manifest, tasks, task, step, agent, signal: input.signal, executeWorkers: input.executeWorkers, runtimeKind: input.runtime?.kind, runtimeConfig: input.runtimeConfig, parentContext: input.parentContext, parentModel: input.parentModel, modelRegistry: input.modelRegistry, modelOverride: input.modelOverride, limits: input.limits };
584
+ const baseInput = { manifest, tasks, task, step, agent, signal: input.signal, executeWorkers: input.executeWorkers, runtimeKind: input.runtime?.kind, runtimeConfig: input.runtimeConfig, parentContext: input.parentContext, parentModel: input.parentModel, modelRegistry: input.modelRegistry, modelOverride: input.modelOverride, limits: input.limits, onJsonEvent: input.onJsonEvent };
586
585
  if (input.reliability?.autoRetry !== true) return withCorrelation(childCorrelation(manifest.runId, task.id), () => runTeamTask(baseInput));
587
586
  let lastFailed: { manifest: TeamRunManifest; tasks: TeamTaskState[] } | undefined;
588
587
  const attemptsSoFar: TaskAttemptState[] = [...(task.attempts ?? [])];
@@ -1,21 +1,21 @@
1
- export interface WorkerHeartbeatState {
2
- workerId: string;
3
- pid?: number;
4
- lastSeenAt: string;
5
- lastStdoutAt?: string;
6
- lastEventAt?: string;
7
- turnCount?: number;
8
- alive?: boolean;
9
- }
10
-
11
- export function createWorkerHeartbeat(workerId: string, pid?: number, now = new Date()): WorkerHeartbeatState {
12
- return { workerId, pid, lastSeenAt: now.toISOString(), alive: true };
13
- }
14
-
15
- export function touchWorkerHeartbeat(heartbeat: WorkerHeartbeatState, updates: Partial<Omit<WorkerHeartbeatState, "workerId">> = {}, now = new Date()): WorkerHeartbeatState {
16
- return { ...heartbeat, ...updates, lastSeenAt: now.toISOString() };
17
- }
18
-
19
- export function isWorkerHeartbeatStale(heartbeat: WorkerHeartbeatState, staleMs: number, now = new Date()): boolean {
20
- return now.getTime() - Date.parse(heartbeat.lastSeenAt) > staleMs;
21
- }
1
+ export interface WorkerHeartbeatState {
2
+ workerId: string;
3
+ pid?: number;
4
+ lastSeenAt: string;
5
+ lastStdoutAt?: string;
6
+ lastEventAt?: string;
7
+ turnCount?: number;
8
+ alive?: boolean;
9
+ }
10
+
11
+ export function createWorkerHeartbeat(workerId: string, pid?: number, now = new Date()): WorkerHeartbeatState {
12
+ return { workerId, pid, lastSeenAt: now.toISOString(), alive: true };
13
+ }
14
+
15
+ export function touchWorkerHeartbeat(heartbeat: WorkerHeartbeatState, updates: Partial<Omit<WorkerHeartbeatState, "workerId">> = {}, now = new Date()): WorkerHeartbeatState {
16
+ return { ...heartbeat, ...updates, lastSeenAt: now.toISOString() };
17
+ }
18
+
19
+ export function isWorkerHeartbeatStale(heartbeat: WorkerHeartbeatState, staleMs: number, now = new Date()): boolean {
20
+ return now.getTime() - Date.parse(heartbeat.lastSeenAt) > staleMs;
21
+ }
@@ -1,57 +1,57 @@
1
- export type WorkerLifecycleState = "spawning" | "trust_required" | "ready_for_prompt" | "running" | "finished" | "failed";
2
- export type StartupFailureClassification = "trust_required" | "prompt_misdelivery" | "prompt_acceptance_timeout" | "transport_dead" | "worker_crashed" | "unknown";
3
-
4
- export interface WorkerStartupEvidence {
5
- lastLifecycleState: WorkerLifecycleState;
6
- command: string;
7
- promptSentAt?: string;
8
- promptAccepted: boolean;
9
- trustPromptDetected: boolean;
10
- transportHealthy: boolean;
11
- childProcessAlive: boolean;
12
- elapsedMs: number;
13
- classification: StartupFailureClassification;
14
- stderrPreview?: string;
15
- }
16
-
17
- export function detectTrustPrompt(text: string): boolean {
18
- const lowered = text.toLowerCase();
19
- return lowered.includes("do you trust") || lowered.includes("trust this") || lowered.includes("untrusted") || lowered.includes("workspace trust") || lowered.includes("allow this folder");
20
- }
21
-
22
- export function classifyStartupFailure(evidence: Omit<WorkerStartupEvidence, "classification">): StartupFailureClassification {
23
- if (!evidence.transportHealthy) return "transport_dead";
24
- if (evidence.trustPromptDetected || evidence.lastLifecycleState === "trust_required") return "trust_required";
25
- if (evidence.promptSentAt && !evidence.promptAccepted && evidence.childProcessAlive) return "prompt_acceptance_timeout";
26
- if (evidence.promptSentAt && !evidence.promptAccepted && !evidence.childProcessAlive) return "worker_crashed";
27
- if (evidence.stderrPreview?.toLowerCase().includes("command not found") || evidence.stderrPreview?.toLowerCase().includes("not recognized")) return "prompt_misdelivery";
28
- if (!evidence.childProcessAlive && evidence.lastLifecycleState !== "finished") return "worker_crashed";
29
- return "unknown";
30
- }
31
-
32
- export function createStartupEvidence(input: {
33
- command: string;
34
- startedAt: Date;
35
- finishedAt?: Date;
36
- promptSentAt?: Date;
37
- promptAccepted?: boolean;
38
- stderr?: string;
39
- error?: string;
40
- exitCode?: number | null;
41
- }): WorkerStartupEvidence {
42
- const stderrPreview = (input.error || input.stderr || "").slice(0, 500) || undefined;
43
- const trustPromptDetected = detectTrustPrompt(stderrPreview ?? "");
44
- const childProcessAlive = input.exitCode === undefined || input.exitCode === null ? !input.finishedAt : false;
45
- const base: Omit<WorkerStartupEvidence, "classification"> = {
46
- lastLifecycleState: input.error || (input.exitCode !== undefined && input.exitCode !== null && input.exitCode !== 0) ? "failed" : input.finishedAt ? "finished" : "running",
47
- command: input.command,
48
- promptSentAt: input.promptSentAt?.toISOString(),
49
- promptAccepted: input.promptAccepted ?? !input.error,
50
- trustPromptDetected,
51
- transportHealthy: !input.error || !/enoent|spawn|transport/i.test(input.error),
52
- childProcessAlive,
53
- elapsedMs: Math.max(0, (input.finishedAt ?? new Date()).getTime() - input.startedAt.getTime()),
54
- stderrPreview,
55
- };
56
- return { ...base, classification: classifyStartupFailure(base) };
57
- }
1
+ export type WorkerLifecycleState = "spawning" | "trust_required" | "ready_for_prompt" | "running" | "finished" | "failed";
2
+ export type StartupFailureClassification = "trust_required" | "prompt_misdelivery" | "prompt_acceptance_timeout" | "transport_dead" | "worker_crashed" | "unknown";
3
+
4
+ export interface WorkerStartupEvidence {
5
+ lastLifecycleState: WorkerLifecycleState;
6
+ command: string;
7
+ promptSentAt?: string;
8
+ promptAccepted: boolean;
9
+ trustPromptDetected: boolean;
10
+ transportHealthy: boolean;
11
+ childProcessAlive: boolean;
12
+ elapsedMs: number;
13
+ classification: StartupFailureClassification;
14
+ stderrPreview?: string;
15
+ }
16
+
17
+ export function detectTrustPrompt(text: string): boolean {
18
+ const lowered = text.toLowerCase();
19
+ return lowered.includes("do you trust") || lowered.includes("trust this") || lowered.includes("untrusted") || lowered.includes("workspace trust") || lowered.includes("allow this folder");
20
+ }
21
+
22
+ export function classifyStartupFailure(evidence: Omit<WorkerStartupEvidence, "classification">): StartupFailureClassification {
23
+ if (!evidence.transportHealthy) return "transport_dead";
24
+ if (evidence.trustPromptDetected || evidence.lastLifecycleState === "trust_required") return "trust_required";
25
+ if (evidence.promptSentAt && !evidence.promptAccepted && evidence.childProcessAlive) return "prompt_acceptance_timeout";
26
+ if (evidence.promptSentAt && !evidence.promptAccepted && !evidence.childProcessAlive) return "worker_crashed";
27
+ if (evidence.stderrPreview?.toLowerCase().includes("command not found") || evidence.stderrPreview?.toLowerCase().includes("not recognized")) return "prompt_misdelivery";
28
+ if (!evidence.childProcessAlive && evidence.lastLifecycleState !== "finished") return "worker_crashed";
29
+ return "unknown";
30
+ }
31
+
32
+ export function createStartupEvidence(input: {
33
+ command: string;
34
+ startedAt: Date;
35
+ finishedAt?: Date;
36
+ promptSentAt?: Date;
37
+ promptAccepted?: boolean;
38
+ stderr?: string;
39
+ error?: string;
40
+ exitCode?: number | null;
41
+ }): WorkerStartupEvidence {
42
+ const stderrPreview = (input.error || input.stderr || "").slice(0, 500) || undefined;
43
+ const trustPromptDetected = detectTrustPrompt(stderrPreview ?? "");
44
+ const childProcessAlive = input.exitCode === undefined || input.exitCode === null ? !input.finishedAt : false;
45
+ const base: Omit<WorkerStartupEvidence, "classification"> = {
46
+ lastLifecycleState: input.error || (input.exitCode !== undefined && input.exitCode !== null && input.exitCode !== 0) ? "failed" : input.finishedAt ? "finished" : "running",
47
+ command: input.command,
48
+ promptSentAt: input.promptSentAt?.toISOString(),
49
+ promptAccepted: input.promptAccepted ?? !input.error,
50
+ trustPromptDetected,
51
+ transportHealthy: !input.error || !/enoent|spawn|transport/i.test(input.error),
52
+ childProcessAlive,
53
+ elapsedMs: Math.max(0, (input.finishedAt ?? new Date()).getTime() - input.startedAt.getTime()),
54
+ stderrPreview,
55
+ };
56
+ return { ...base, classification: classifyStartupFailure(base) };
57
+ }
@@ -57,7 +57,8 @@ export const AgentOverrideSchema = Type.Object({
57
57
  model: Type.Optional(Type.Union([Type.String({ minLength: 1 }), Type.Literal(false)])),
58
58
  fallbackModels: Type.Optional(Type.Union([Type.Array(Type.String({ minLength: 1 })), Type.Literal(false)])),
59
59
  thinking: Type.Optional(Type.Union([Type.String({ minLength: 1 }), Type.Literal(false)])),
60
- tools: Type.Optional(Type.Array(Type.String({ minLength: 1 }))),
60
+ tools: Type.Optional(Type.Union([Type.Array(Type.String({ minLength: 1 })), Type.Literal(false)])),
61
+ skills: Type.Optional(Type.Union([Type.Array(Type.String({ minLength: 1 })), Type.Literal(false)])),
61
62
  }, { additionalProperties: false });
62
63
 
63
64
  export const PiTeamsAgentsConfigSchema = Type.Object({