pi-crew 0.1.43 → 0.1.45

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 (158) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/agents/analyst.md +11 -11
  3. package/agents/critic.md +11 -11
  4. package/agents/executor.md +11 -11
  5. package/agents/explorer.md +11 -11
  6. package/agents/planner.md +11 -11
  7. package/agents/reviewer.md +11 -11
  8. package/agents/security-reviewer.md +11 -11
  9. package/agents/test-engineer.md +11 -11
  10. package/agents/verifier.md +11 -11
  11. package/agents/writer.md +11 -11
  12. package/docs/refactor-tasks-phase3.md +394 -394
  13. package/docs/refactor-tasks-phase4.md +564 -564
  14. package/docs/refactor-tasks-phase5.md +402 -402
  15. package/docs/refactor-tasks-phase6.md +662 -662
  16. package/docs/research-extension-examples.md +297 -297
  17. package/docs/research-extension-system.md +324 -324
  18. package/docs/research-optimization-plan.md +548 -548
  19. package/docs/research-phase10-distillation.md +199 -0
  20. package/docs/research-phase11-distillation.md +201 -0
  21. package/docs/research-pi-coding-agent.md +357 -357
  22. package/docs/research-source-pi-crew-reference.md +174 -174
  23. package/docs/runtime-flow.md +148 -148
  24. package/docs/source-runtime-refactor-map.md +83 -83
  25. package/index.ts +6 -6
  26. package/package.json +1 -1
  27. package/src/agents/agent-serializer.ts +34 -34
  28. package/src/agents/discover-agents.ts +1 -0
  29. package/src/config/config.ts +19 -0
  30. package/src/extension/cross-extension-rpc.ts +82 -82
  31. package/src/extension/register.ts +134 -8
  32. package/src/extension/registration/commands.ts +18 -2
  33. package/src/extension/registration/compaction-guard.ts +125 -125
  34. package/src/extension/registration/subagent-tools.ts +148 -148
  35. package/src/extension/registration/team-tool.ts +27 -8
  36. package/src/extension/run-bundle-schema.ts +89 -89
  37. package/src/extension/run-index.ts +19 -0
  38. package/src/extension/run-maintenance.ts +43 -43
  39. package/src/extension/team-tool/api.ts +1 -1
  40. package/src/extension/team-tool/cancel.ts +79 -4
  41. package/src/extension/team-tool/context.ts +2 -0
  42. package/src/extension/team-tool/handle-settings.ts +188 -188
  43. package/src/extension/team-tool/inspect.ts +41 -41
  44. package/src/extension/team-tool/lifecycle-actions.ts +79 -79
  45. package/src/extension/team-tool/plan.ts +19 -19
  46. package/src/extension/team-tool/respond.ts +84 -0
  47. package/src/extension/team-tool/run.ts +3 -2
  48. package/src/extension/team-tool/status.ts +7 -1
  49. package/src/extension/team-tool-types.ts +4 -0
  50. package/src/extension/team-tool.ts +2 -0
  51. package/src/i18n.ts +184 -184
  52. package/src/observability/event-to-metric.ts +6 -0
  53. package/src/observability/exporters/otlp-exporter.ts +77 -77
  54. package/src/prompt/prompt-runtime.ts +72 -72
  55. package/src/runtime/agent-control.ts +63 -63
  56. package/src/runtime/agent-memory.ts +72 -72
  57. package/src/runtime/agent-observability.ts +114 -114
  58. package/src/runtime/async-marker.ts +26 -26
  59. package/src/runtime/attention-events.ts +28 -28
  60. package/src/runtime/background-runner.ts +53 -53
  61. package/src/runtime/child-pi.ts +444 -444
  62. package/src/runtime/completion-guard.ts +87 -0
  63. package/src/runtime/crash-recovery.ts +30 -0
  64. package/src/runtime/crew-agent-records.ts +8 -0
  65. package/src/runtime/crew-agent-runtime.ts +2 -1
  66. package/src/runtime/delivery-coordinator.ts +154 -0
  67. package/src/runtime/direct-run.ts +35 -35
  68. package/src/runtime/foreground-control.ts +82 -82
  69. package/src/runtime/green-contract.ts +46 -46
  70. package/src/runtime/group-join.ts +106 -106
  71. package/src/runtime/heartbeat-gradient.ts +28 -28
  72. package/src/runtime/heartbeat-watcher.ts +124 -124
  73. package/src/runtime/live-agent-control.ts +87 -87
  74. package/src/runtime/live-agent-manager.ts +85 -85
  75. package/src/runtime/live-control-realtime.ts +36 -36
  76. package/src/runtime/live-session-runtime.ts +305 -305
  77. package/src/runtime/model-fallback.ts +5 -2
  78. package/src/runtime/overflow-recovery.ts +176 -0
  79. package/src/runtime/parallel-research.ts +44 -44
  80. package/src/runtime/pi-json-output.ts +111 -111
  81. package/src/runtime/policy-engine.ts +79 -79
  82. package/src/runtime/process-status.ts +1 -1
  83. package/src/runtime/progress-event-coalescer.ts +43 -43
  84. package/src/runtime/recovery-recipes.ts +74 -74
  85. package/src/runtime/retry-executor.ts +64 -64
  86. package/src/runtime/role-permission.ts +39 -39
  87. package/src/runtime/session-resources.ts +25 -0
  88. package/src/runtime/session-snapshot.ts +59 -0
  89. package/src/runtime/session-usage.ts +79 -79
  90. package/src/runtime/sidechain-output.ts +29 -29
  91. package/src/runtime/stale-reconciler.ts +199 -0
  92. package/src/runtime/supervisor-contact.ts +59 -0
  93. package/src/runtime/task-display.ts +38 -38
  94. package/src/runtime/task-output-context.ts +127 -127
  95. package/src/runtime/task-runner/live-executor.ts +101 -101
  96. package/src/runtime/task-runner/progress.ts +119 -119
  97. package/src/runtime/task-runner/result-utils.ts +14 -14
  98. package/src/runtime/task-runner/state-helpers.ts +22 -22
  99. package/src/runtime/task-runner.ts +14 -0
  100. package/src/runtime/team-runner.ts +19 -8
  101. package/src/runtime/worker-heartbeat.ts +21 -21
  102. package/src/runtime/worker-startup.ts +57 -57
  103. package/src/schema/config-schema.ts +1 -0
  104. package/src/schema/team-tool-schema.ts +6 -1
  105. package/src/state/contracts.ts +6 -2
  106. package/src/state/state-store.ts +43 -0
  107. package/src/state/task-claims.ts +44 -44
  108. package/src/state/types.ts +2 -0
  109. package/src/state/usage.ts +29 -29
  110. package/src/subagents/async-entry.ts +1 -1
  111. package/src/subagents/index.ts +3 -3
  112. package/src/subagents/live/control.ts +1 -1
  113. package/src/subagents/live/manager.ts +1 -1
  114. package/src/subagents/live/realtime.ts +1 -1
  115. package/src/subagents/live/session-runtime.ts +1 -1
  116. package/src/subagents/manager.ts +1 -1
  117. package/src/subagents/spawn.ts +1 -1
  118. package/src/teams/team-serializer.ts +38 -38
  119. package/src/types/diff.d.ts +18 -18
  120. package/src/ui/crew-footer.ts +101 -101
  121. package/src/ui/crew-select-list.ts +111 -111
  122. package/src/ui/crew-widget.ts +10 -5
  123. package/src/ui/dashboard-panes/mailbox-pane.ts +2 -1
  124. package/src/ui/dashboard-panes/metrics-pane.ts +34 -34
  125. package/src/ui/dynamic-border.ts +25 -25
  126. package/src/ui/layout-primitives.ts +106 -106
  127. package/src/ui/loaders.ts +158 -158
  128. package/src/ui/powerbar-publisher.ts +4 -4
  129. package/src/ui/render-diff.ts +119 -119
  130. package/src/ui/render-scheduler.ts +143 -143
  131. package/src/ui/run-snapshot-cache.ts +310 -17
  132. package/src/ui/snapshot-types.ts +5 -0
  133. package/src/ui/spinner.ts +17 -17
  134. package/src/ui/status-colors.ts +58 -54
  135. package/src/ui/syntax-highlight.ts +116 -116
  136. package/src/utils/atomic-write.ts +33 -0
  137. package/src/utils/completion-dedupe.ts +63 -63
  138. package/src/utils/frontmatter.ts +68 -68
  139. package/src/utils/git.ts +262 -262
  140. package/src/utils/ids.ts +12 -12
  141. package/src/utils/names.ts +27 -27
  142. package/src/utils/redaction.ts +44 -44
  143. package/src/utils/safe-paths.ts +47 -47
  144. package/src/utils/sleep.ts +32 -32
  145. package/src/workflows/validate-workflow.ts +40 -40
  146. package/src/worktree/branch-freshness.ts +45 -45
  147. package/teams/default.team.md +12 -12
  148. package/teams/fast-fix.team.md +11 -11
  149. package/teams/implementation.team.md +18 -18
  150. package/teams/parallel-research.team.md +14 -14
  151. package/teams/research.team.md +11 -11
  152. package/teams/review.team.md +12 -12
  153. package/workflows/default.workflow.md +29 -29
  154. package/workflows/fast-fix.workflow.md +22 -22
  155. package/workflows/implementation.workflow.md +38 -38
  156. package/workflows/parallel-research.workflow.md +46 -46
  157. package/workflows/research.workflow.md +22 -22
  158. package/workflows/review.workflow.md +30 -30
@@ -0,0 +1,176 @@
1
+ import { logInternalError } from "../utils/internal-error.ts";
2
+
3
+ export type OverflowPhase = "none" | "compaction" | "retrying" | "recovered" | "failed";
4
+
5
+ export interface OverflowRecoveryState {
6
+ taskId: string;
7
+ runId: string;
8
+ phase: OverflowPhase;
9
+ startedAt: number;
10
+ lastEventAt: number;
11
+ compactionCount: number;
12
+ retryCount: number;
13
+ }
14
+
15
+ export interface OverflowRecoveryCallbacks {
16
+ onPhaseChange?: (state: OverflowRecoveryState, previousPhase: OverflowPhase) => void;
17
+ onTimeout?: (state: OverflowRecoveryState) => void;
18
+ }
19
+
20
+ const PHASE_TIMEOUT_MS = 120_000; // 120 seconds per phase
21
+ const TERMINAL_STATE_TTL_MS = 5 * 60_000;
22
+
23
+ export class OverflowRecoveryTracker {
24
+ private states = new Map<string, OverflowRecoveryState>();
25
+ private timers = new Map<string, ReturnType<typeof setTimeout>>();
26
+ private callbacks: OverflowRecoveryCallbacks;
27
+
28
+ constructor(callbacks: OverflowRecoveryCallbacks = {}) {
29
+ this.callbacks = callbacks;
30
+ }
31
+
32
+ feedEvent(taskId: string, runId: string, eventType: string): OverflowPhase {
33
+ const key = this.keyFor(taskId, runId);
34
+ const existing = this.states.get(key);
35
+ const now = Date.now();
36
+
37
+ if (existing && existing.phase === "recovered") {
38
+ existing.lastEventAt = now;
39
+ return "recovered";
40
+ }
41
+ if (existing && existing.phase === "failed") {
42
+ existing.lastEventAt = now;
43
+ return "failed";
44
+ }
45
+
46
+ let phase: OverflowPhase = existing?.phase ?? "none";
47
+ let compactionCount = existing?.compactionCount ?? 0;
48
+ let retryCount = existing?.retryCount ?? 0;
49
+ const previousPhase = phase;
50
+
51
+ switch (eventType) {
52
+ case "compaction_start":
53
+ phase = "compaction";
54
+ compactionCount++;
55
+ break;
56
+ case "compaction_end":
57
+ // After compaction, we expect a retry; stay in compaction until retry starts
58
+ break;
59
+ case "auto_retry_start":
60
+ phase = "retrying";
61
+ retryCount++;
62
+ break;
63
+ case "auto_retry_end":
64
+ // After retry completes, the agent should produce a response
65
+ // We consider this recovered but don't finalize until agent_end
66
+ phase = "recovered";
67
+ break;
68
+ case "agent_end":
69
+ // If we were recovering and agent ends, we're recovered or failed
70
+ if (phase === "compaction" || phase === "retrying") {
71
+ phase = "failed";
72
+ }
73
+ break;
74
+ default:
75
+ // Unknown event type — no phase change
76
+ break;
77
+ }
78
+
79
+ const state: OverflowRecoveryState = {
80
+ taskId,
81
+ runId,
82
+ phase,
83
+ startedAt: existing?.startedAt ?? now,
84
+ lastEventAt: now,
85
+ compactionCount,
86
+ retryCount,
87
+ };
88
+
89
+ this.states.set(key, state);
90
+ this.resetTimeout(key);
91
+
92
+ if (previousPhase !== phase && this.callbacks.onPhaseChange) {
93
+ try {
94
+ this.callbacks.onPhaseChange(state, previousPhase);
95
+ } catch (error) {
96
+ logInternalError("overflow-recovery.onPhaseChange", error, `taskId=${taskId}`);
97
+ }
98
+ }
99
+
100
+ return phase;
101
+ }
102
+
103
+ getState(taskId: string, runId?: string): OverflowRecoveryState | undefined {
104
+ if (runId) return this.states.get(this.keyFor(taskId, runId));
105
+ return [...this.states.values()].find((state) => state.taskId === taskId);
106
+ }
107
+
108
+ getPhase(taskId: string, runId?: string): OverflowPhase {
109
+ return this.getState(taskId, runId)?.phase ?? "none";
110
+ }
111
+
112
+ removeTask(taskId: string, runId?: string): void {
113
+ const keys = runId
114
+ ? [this.keyFor(taskId, runId)]
115
+ : [...this.states.entries()].filter(([, state]) => state.taskId === taskId).map(([key]) => key);
116
+ for (const key of keys) this.removeKey(key);
117
+ }
118
+
119
+ dispose(): void {
120
+ for (const timer of this.timers.values()) clearTimeout(timer);
121
+ this.timers.clear();
122
+ this.states.clear();
123
+ }
124
+
125
+ private keyFor(taskId: string, runId: string): string {
126
+ return `${runId}\u0000${taskId}`;
127
+ }
128
+
129
+ private removeKey(key: string): void {
130
+ this.states.delete(key);
131
+ const timer = this.timers.get(key);
132
+ if (timer) clearTimeout(timer);
133
+ this.timers.delete(key);
134
+ }
135
+
136
+ private resetTimeout(key: string): void {
137
+ const existing = this.timers.get(key);
138
+ if (existing) clearTimeout(existing);
139
+ const current = this.states.get(key);
140
+ const timeoutMs = current?.phase === "recovered" || current?.phase === "failed" || current?.phase === "none"
141
+ ? TERMINAL_STATE_TTL_MS
142
+ : PHASE_TIMEOUT_MS;
143
+
144
+ const timer = setTimeout(() => {
145
+ this.timers.delete(key);
146
+ const state = this.states.get(key);
147
+ if (!state) return;
148
+ if (state.phase === "recovered" || state.phase === "failed" || state.phase === "none") {
149
+ this.states.delete(key);
150
+ return;
151
+ }
152
+
153
+ const previousPhase = state.phase;
154
+ state.phase = "failed";
155
+ state.lastEventAt = Date.now();
156
+
157
+ if (this.callbacks.onTimeout) {
158
+ try {
159
+ this.callbacks.onTimeout(state);
160
+ } catch (error) {
161
+ logInternalError("overflow-recovery.onTimeout", error, `taskId=${state.taskId}`);
162
+ }
163
+ }
164
+ if (this.callbacks.onPhaseChange) {
165
+ try {
166
+ this.callbacks.onPhaseChange(state, previousPhase);
167
+ } catch (error) {
168
+ logInternalError("overflow-recovery.onPhaseChange-timeout", error, `taskId=${state.taskId}`);
169
+ }
170
+ }
171
+ }, timeoutMs);
172
+
173
+ timer.unref();
174
+ this.timers.set(key, timer);
175
+ }
176
+ }
@@ -1,44 +1,44 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
- import type { WorkflowConfig, WorkflowStep } from "../workflows/workflow-config.ts";
4
-
5
- export function sourcePiProjects(cwd: string): string[] {
6
- const sourceDir = path.join(cwd, "Source");
7
- try {
8
- return fs.readdirSync(sourceDir, { withFileTypes: true })
9
- .filter((entry) => entry.isDirectory() && entry.name.startsWith("pi-"))
10
- .map((entry) => `Source/${entry.name}`)
11
- .sort();
12
- } catch {
13
- return [];
14
- }
15
- }
16
-
17
- export function chunkProjects(projects: string[], target = 6): string[][] {
18
- const chunks = Array.from({ length: Math.min(Math.max(1, target), Math.max(1, projects.length)) }, () => [] as string[]);
19
- projects.forEach((project, index) => chunks[index % chunks.length]!.push(project));
20
- return chunks.filter((chunk) => chunk.length > 0);
21
- }
22
-
23
- export function expandParallelResearchWorkflow(workflow: WorkflowConfig, cwd: string): WorkflowConfig {
24
- if (workflow.name !== "parallel-research") return workflow;
25
- const projects = sourcePiProjects(cwd);
26
- if (projects.length === 0) return workflow;
27
- const chunks = chunkProjects(projects, Math.min(8, Math.max(4, Math.ceil(projects.length / 3))));
28
- const exploreSteps: WorkflowStep[] = chunks.map((paths, index) => ({
29
- id: `explore-shard-${index + 1}`,
30
- role: "explorer",
31
- parallelGroup: "explore",
32
- reads: paths,
33
- task: [`Explore this dynamic shard for: {goal}`, "", "Paths:", ...paths.map((item) => `- ${item}`), "", "Focus on purpose, architecture, runtime/UI patterns, package config, docs, and lessons for pi-crew."].join("\n"),
34
- }));
35
- return {
36
- ...workflow,
37
- steps: [
38
- { id: "discover", role: "explorer", parallelGroup: "inventory", task: `Quickly inventory and validate ${projects.length} pi-* projects for: {goal}\n\nProjects:\n${projects.map((item) => `- ${item}`).join("\n")}\n\nDo not block shard work; summarize routing notes only.` },
39
- ...exploreSteps,
40
- { id: "synthesize", role: "analyst", dependsOn: exploreSteps.map((step) => step.id), task: "Synthesize all dynamic shard findings. Identify common patterns, gaps, and concrete recommendations. Use discover output if available, but prioritize completed shard outputs." },
41
- { id: "write", role: "writer", dependsOn: ["synthesize"], output: "research-summary.md", task: "Write a concise final summary with evidence, risks, and actionable next steps." },
42
- ],
43
- };
44
- }
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import type { WorkflowConfig, WorkflowStep } from "../workflows/workflow-config.ts";
4
+
5
+ export function sourcePiProjects(cwd: string): string[] {
6
+ const sourceDir = path.join(cwd, "Source");
7
+ try {
8
+ return fs.readdirSync(sourceDir, { withFileTypes: true })
9
+ .filter((entry) => entry.isDirectory() && entry.name.startsWith("pi-"))
10
+ .map((entry) => `Source/${entry.name}`)
11
+ .sort();
12
+ } catch {
13
+ return [];
14
+ }
15
+ }
16
+
17
+ export function chunkProjects(projects: string[], target = 6): string[][] {
18
+ const chunks = Array.from({ length: Math.min(Math.max(1, target), Math.max(1, projects.length)) }, () => [] as string[]);
19
+ projects.forEach((project, index) => chunks[index % chunks.length]!.push(project));
20
+ return chunks.filter((chunk) => chunk.length > 0);
21
+ }
22
+
23
+ export function expandParallelResearchWorkflow(workflow: WorkflowConfig, cwd: string): WorkflowConfig {
24
+ if (workflow.name !== "parallel-research") return workflow;
25
+ const projects = sourcePiProjects(cwd);
26
+ if (projects.length === 0) return workflow;
27
+ const chunks = chunkProjects(projects, Math.min(8, Math.max(4, Math.ceil(projects.length / 3))));
28
+ const exploreSteps: WorkflowStep[] = chunks.map((paths, index) => ({
29
+ id: `explore-shard-${index + 1}`,
30
+ role: "explorer",
31
+ parallelGroup: "explore",
32
+ reads: paths,
33
+ task: [`Explore this dynamic shard for: {goal}`, "", "Paths:", ...paths.map((item) => `- ${item}`), "", "Focus on purpose, architecture, runtime/UI patterns, package config, docs, and lessons for pi-crew."].join("\n"),
34
+ }));
35
+ return {
36
+ ...workflow,
37
+ steps: [
38
+ { id: "discover", role: "explorer", parallelGroup: "inventory", task: `Quickly inventory and validate ${projects.length} pi-* projects for: {goal}\n\nProjects:\n${projects.map((item) => `- ${item}`).join("\n")}\n\nDo not block shard work; summarize routing notes only.` },
39
+ ...exploreSteps,
40
+ { id: "synthesize", role: "analyst", dependsOn: exploreSteps.map((step) => step.id), task: "Synthesize all dynamic shard findings. Identify common patterns, gaps, and concrete recommendations. Use discover output if available, but prioritize completed shard outputs." },
41
+ { id: "write", role: "writer", dependsOn: ["synthesize"], output: "research-summary.md", task: "Write a concise final summary with evidence, risks, and actionable next steps." },
42
+ ],
43
+ };
44
+ }
@@ -1,111 +1,111 @@
1
- export interface ParsedPiUsage {
2
- input?: number;
3
- output?: number;
4
- cacheRead?: number;
5
- cacheWrite?: number;
6
- cost?: number;
7
- turns?: number;
8
- }
9
-
10
- export interface ParsedPiJsonOutput {
11
- jsonEvents: number;
12
- textEvents: string[];
13
- finalText?: string;
14
- usage?: ParsedPiUsage;
15
- }
16
-
17
- function asRecord(value: unknown): Record<string, unknown> | undefined {
18
- return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : undefined;
19
- }
20
-
21
- function numberField(obj: Record<string, unknown>, keys: string[]): number | undefined {
22
- for (const key of keys) {
23
- const value = obj[key];
24
- if (typeof value === "number" && Number.isFinite(value)) return value;
25
- }
26
- return undefined;
27
- }
28
-
29
- function mergeUsage(target: ParsedPiUsage, source: ParsedPiUsage): ParsedPiUsage {
30
- return {
31
- input: source.input ?? target.input,
32
- output: source.output ?? target.output,
33
- cacheRead: source.cacheRead ?? target.cacheRead,
34
- cacheWrite: source.cacheWrite ?? target.cacheWrite,
35
- cost: source.cost ?? target.cost,
36
- turns: source.turns ?? target.turns,
37
- };
38
- }
39
-
40
- function extractUsage(value: unknown): ParsedPiUsage | undefined {
41
- const obj = asRecord(value);
42
- if (!obj) return undefined;
43
- const direct: ParsedPiUsage = {
44
- input: numberField(obj, ["input", "inputTokens", "input_tokens"]),
45
- output: numberField(obj, ["output", "outputTokens", "output_tokens"]),
46
- cacheRead: numberField(obj, ["cacheRead", "cache_read", "cacheReadTokens", "cache_read_tokens"]),
47
- cacheWrite: numberField(obj, ["cacheWrite", "cache_write", "cacheWriteTokens", "cache_write_tokens"]),
48
- cost: numberField(obj, ["cost", "costUsd", "cost_usd"]),
49
- turns: numberField(obj, ["turns", "turnCount", "turn_count"]),
50
- };
51
- if (Object.values(direct).some((entry) => entry !== undefined)) return direct;
52
- for (const key of ["usage", "tokenUsage", "tokens", "stats"]) {
53
- const nested = extractUsage(obj[key]);
54
- if (nested) return nested;
55
- }
56
- return undefined;
57
- }
58
-
59
- function textFromContent(content: unknown): string[] {
60
- if (typeof content === "string") return [content];
61
- if (!Array.isArray(content)) return [];
62
- const text: string[] = [];
63
- for (const part of content) {
64
- const obj = asRecord(part);
65
- if (!obj) continue;
66
- if (obj.type === "text" && typeof obj.text === "string") text.push(obj.text);
67
- else if (typeof obj.content === "string") text.push(obj.content);
68
- }
69
- return text;
70
- }
71
-
72
- function extractText(value: unknown): string[] {
73
- const obj = asRecord(value);
74
- if (!obj) return [];
75
- const message = asRecord(obj.message);
76
- if (message?.role !== undefined && message.role !== "assistant") return [];
77
- const text: string[] = [];
78
- if (typeof obj.text === "string") text.push(obj.text);
79
- if (typeof obj.output === "string") text.push(obj.output);
80
- if (typeof obj.finalOutput === "string") text.push(obj.finalOutput);
81
- if (typeof obj.final_output === "string") text.push(obj.final_output);
82
- if (!message) text.push(...textFromContent(obj.content));
83
- if (message) text.push(...textFromContent(message.content));
84
- return text.filter((entry) => entry.trim().length > 0);
85
- }
86
-
87
- export function parsePiJsonOutput(stdout: string): ParsedPiJsonOutput {
88
- let jsonEvents = 0;
89
- const textEvents: string[] = [];
90
- let usage: ParsedPiUsage | undefined;
91
- for (const line of stdout.split("\n")) {
92
- const trimmed = line.trim();
93
- if (!trimmed) continue;
94
- let event: unknown;
95
- try {
96
- event = JSON.parse(trimmed) as unknown;
97
- } catch {
98
- continue;
99
- }
100
- jsonEvents++;
101
- textEvents.push(...extractText(event));
102
- const eventUsage = extractUsage(event);
103
- if (eventUsage) usage = mergeUsage(usage ?? {}, eventUsage);
104
- }
105
- return {
106
- jsonEvents,
107
- textEvents,
108
- finalText: textEvents.length > 0 ? textEvents[textEvents.length - 1] : undefined,
109
- usage,
110
- };
111
- }
1
+ export interface ParsedPiUsage {
2
+ input?: number;
3
+ output?: number;
4
+ cacheRead?: number;
5
+ cacheWrite?: number;
6
+ cost?: number;
7
+ turns?: number;
8
+ }
9
+
10
+ export interface ParsedPiJsonOutput {
11
+ jsonEvents: number;
12
+ textEvents: string[];
13
+ finalText?: string;
14
+ usage?: ParsedPiUsage;
15
+ }
16
+
17
+ function asRecord(value: unknown): Record<string, unknown> | undefined {
18
+ return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : undefined;
19
+ }
20
+
21
+ function numberField(obj: Record<string, unknown>, keys: string[]): number | undefined {
22
+ for (const key of keys) {
23
+ const value = obj[key];
24
+ if (typeof value === "number" && Number.isFinite(value)) return value;
25
+ }
26
+ return undefined;
27
+ }
28
+
29
+ function mergeUsage(target: ParsedPiUsage, source: ParsedPiUsage): ParsedPiUsage {
30
+ return {
31
+ input: source.input ?? target.input,
32
+ output: source.output ?? target.output,
33
+ cacheRead: source.cacheRead ?? target.cacheRead,
34
+ cacheWrite: source.cacheWrite ?? target.cacheWrite,
35
+ cost: source.cost ?? target.cost,
36
+ turns: source.turns ?? target.turns,
37
+ };
38
+ }
39
+
40
+ function extractUsage(value: unknown): ParsedPiUsage | undefined {
41
+ const obj = asRecord(value);
42
+ if (!obj) return undefined;
43
+ const direct: ParsedPiUsage = {
44
+ input: numberField(obj, ["input", "inputTokens", "input_tokens"]),
45
+ output: numberField(obj, ["output", "outputTokens", "output_tokens"]),
46
+ cacheRead: numberField(obj, ["cacheRead", "cache_read", "cacheReadTokens", "cache_read_tokens"]),
47
+ cacheWrite: numberField(obj, ["cacheWrite", "cache_write", "cacheWriteTokens", "cache_write_tokens"]),
48
+ cost: numberField(obj, ["cost", "costUsd", "cost_usd"]),
49
+ turns: numberField(obj, ["turns", "turnCount", "turn_count"]),
50
+ };
51
+ if (Object.values(direct).some((entry) => entry !== undefined)) return direct;
52
+ for (const key of ["usage", "tokenUsage", "tokens", "stats"]) {
53
+ const nested = extractUsage(obj[key]);
54
+ if (nested) return nested;
55
+ }
56
+ return undefined;
57
+ }
58
+
59
+ function textFromContent(content: unknown): string[] {
60
+ if (typeof content === "string") return [content];
61
+ if (!Array.isArray(content)) return [];
62
+ const text: string[] = [];
63
+ for (const part of content) {
64
+ const obj = asRecord(part);
65
+ if (!obj) continue;
66
+ if (obj.type === "text" && typeof obj.text === "string") text.push(obj.text);
67
+ else if (typeof obj.content === "string") text.push(obj.content);
68
+ }
69
+ return text;
70
+ }
71
+
72
+ function extractText(value: unknown): string[] {
73
+ const obj = asRecord(value);
74
+ if (!obj) return [];
75
+ const message = asRecord(obj.message);
76
+ if (message?.role !== undefined && message.role !== "assistant") return [];
77
+ const text: string[] = [];
78
+ if (typeof obj.text === "string") text.push(obj.text);
79
+ if (typeof obj.output === "string") text.push(obj.output);
80
+ if (typeof obj.finalOutput === "string") text.push(obj.finalOutput);
81
+ if (typeof obj.final_output === "string") text.push(obj.final_output);
82
+ if (!message) text.push(...textFromContent(obj.content));
83
+ if (message) text.push(...textFromContent(message.content));
84
+ return text.filter((entry) => entry.trim().length > 0);
85
+ }
86
+
87
+ export function parsePiJsonOutput(stdout: string): ParsedPiJsonOutput {
88
+ let jsonEvents = 0;
89
+ const textEvents: string[] = [];
90
+ let usage: ParsedPiUsage | undefined;
91
+ for (const line of stdout.split("\n")) {
92
+ const trimmed = line.trim();
93
+ if (!trimmed) continue;
94
+ let event: unknown;
95
+ try {
96
+ event = JSON.parse(trimmed) as unknown;
97
+ } catch {
98
+ continue;
99
+ }
100
+ jsonEvents++;
101
+ textEvents.push(...extractText(event));
102
+ const eventUsage = extractUsage(event);
103
+ if (eventUsage) usage = mergeUsage(usage ?? {}, eventUsage);
104
+ }
105
+ return {
106
+ jsonEvents,
107
+ textEvents,
108
+ finalText: textEvents.length > 0 ? textEvents[textEvents.length - 1] : undefined,
109
+ usage,
110
+ };
111
+ }
@@ -1,79 +1,79 @@
1
- import type { CrewLimitsConfig } from "../config/config.ts";
2
- import type { PolicyDecision, PolicyDecisionAction, PolicyDecisionReason, TeamRunManifest, TeamTaskState } from "../state/types.ts";
3
- import { evaluateGreenContract } from "./green-contract.ts";
4
- import { isWorkerHeartbeatStale } from "./worker-heartbeat.ts";
5
-
6
- export interface PolicyEngineInput {
7
- manifest: TeamRunManifest;
8
- tasks: TeamTaskState[];
9
- limits?: CrewLimitsConfig;
10
- now?: Date;
11
- }
12
-
13
- function decision(action: PolicyDecisionAction, reason: PolicyDecisionReason, message: string, taskId?: string): PolicyDecision {
14
- return {
15
- action,
16
- reason,
17
- message,
18
- taskId,
19
- createdAt: new Date().toISOString(),
20
- };
21
- }
22
-
23
- function taskDepth(task: TeamTaskState, tasksById: Map<string, TeamTaskState>): number {
24
- let depth = 0;
25
- let current = task.graph?.parentId;
26
- const seen = new Set<string>();
27
- while (current && !seen.has(current)) {
28
- seen.add(current);
29
- depth += 1;
30
- current = tasksById.get(current)?.graph?.parentId;
31
- }
32
- return depth;
33
- }
34
-
35
- export function evaluateCrewPolicy(input: PolicyEngineInput): PolicyDecision[] {
36
- const decisions: PolicyDecision[] = [];
37
- const maxTasksPerRun = Number.isFinite(input.limits?.maxTasksPerRun) ? input.limits!.maxTasksPerRun : undefined;
38
- if (maxTasksPerRun !== undefined && input.tasks.length > maxTasksPerRun) {
39
- decisions.push(decision("block", "limit_exceeded", `Run has ${input.tasks.length} tasks, exceeding maxTasksPerRun=${maxTasksPerRun}.`));
40
- }
41
- const runningCount = input.tasks.filter((task) => task.status === "running").length;
42
- const maxConcurrentWorkers = Number.isFinite(input.limits?.maxConcurrentWorkers) ? input.limits!.maxConcurrentWorkers : undefined;
43
- if (maxConcurrentWorkers !== undefined && runningCount > maxConcurrentWorkers) {
44
- decisions.push(decision("block", "limit_exceeded", `Run has ${runningCount} running workers, exceeding maxConcurrentWorkers=${maxConcurrentWorkers}.`));
45
- }
46
- const tasksById = new Map(input.tasks.map((task) => [task.id, task]));
47
-
48
- for (const task of input.tasks) {
49
- if (input.limits?.maxChildrenPerTask !== undefined && (task.graph?.children.length ?? 0) > input.limits.maxChildrenPerTask) {
50
- decisions.push(decision("block", "limit_exceeded", `Task has ${task.graph?.children.length ?? 0} children, exceeding maxChildrenPerTask=${input.limits.maxChildrenPerTask}.`, task.id));
51
- }
52
- if (input.limits?.maxTaskDepth !== undefined && taskDepth(task, tasksById) > input.limits.maxTaskDepth) {
53
- decisions.push(decision("block", "limit_exceeded", `Task graph depth exceeds maxTaskDepth=${input.limits.maxTaskDepth}.`, task.id));
54
- }
55
- if (task.status === "failed") {
56
- const retryCount = task.policy?.retryCount ?? 0;
57
- const maxRetries = input.limits?.maxRetriesPerTask ?? 0;
58
- decisions.push(decision(retryCount < maxRetries ? "retry" : "escalate", "task_failed", task.error ? `Task failed: ${task.error}` : "Task failed.", task.id));
59
- }
60
- if ((task.status === "running" || task.status === "queued") && task.heartbeat && task.heartbeat.alive !== false && isWorkerHeartbeatStale(task.heartbeat, input.limits?.heartbeatStaleMs ?? 60_000, input.now)) {
61
- decisions.push(decision("escalate", "worker_stale", "Worker heartbeat is stale.", task.id));
62
- }
63
- if (task.taskPacket?.verification) {
64
- const outcome = evaluateGreenContract(task.taskPacket.verification, task.verification);
65
- if (!outcome.satisfied && task.status === "completed") {
66
- decisions.push(decision("block", "green_unsatisfied", `Green contract unsatisfied: required=${outcome.requiredGreenLevel}, observed=${outcome.observedGreenLevel}.`, task.id));
67
- }
68
- }
69
- }
70
-
71
- if (decisions.length === 0 && input.tasks.length > 0 && input.tasks.every((task) => task.status === "completed")) {
72
- decisions.push(decision("closeout", "run_complete", "All tasks completed and no policy blockers were found."));
73
- }
74
- return decisions;
75
- }
76
-
77
- export function summarizePolicyDecisions(decisions: PolicyDecision[]): string[] {
78
- return decisions.map((item) => `- ${item.action} (${item.reason})${item.taskId ? ` ${item.taskId}` : ""}: ${item.message}`);
79
- }
1
+ import type { CrewLimitsConfig } from "../config/config.ts";
2
+ import type { PolicyDecision, PolicyDecisionAction, PolicyDecisionReason, TeamRunManifest, TeamTaskState } from "../state/types.ts";
3
+ import { evaluateGreenContract } from "./green-contract.ts";
4
+ import { isWorkerHeartbeatStale } from "./worker-heartbeat.ts";
5
+
6
+ export interface PolicyEngineInput {
7
+ manifest: TeamRunManifest;
8
+ tasks: TeamTaskState[];
9
+ limits?: CrewLimitsConfig;
10
+ now?: Date;
11
+ }
12
+
13
+ function decision(action: PolicyDecisionAction, reason: PolicyDecisionReason, message: string, taskId?: string): PolicyDecision {
14
+ return {
15
+ action,
16
+ reason,
17
+ message,
18
+ taskId,
19
+ createdAt: new Date().toISOString(),
20
+ };
21
+ }
22
+
23
+ function taskDepth(task: TeamTaskState, tasksById: Map<string, TeamTaskState>): number {
24
+ let depth = 0;
25
+ let current = task.graph?.parentId;
26
+ const seen = new Set<string>();
27
+ while (current && !seen.has(current)) {
28
+ seen.add(current);
29
+ depth += 1;
30
+ current = tasksById.get(current)?.graph?.parentId;
31
+ }
32
+ return depth;
33
+ }
34
+
35
+ export function evaluateCrewPolicy(input: PolicyEngineInput): PolicyDecision[] {
36
+ const decisions: PolicyDecision[] = [];
37
+ const maxTasksPerRun = Number.isFinite(input.limits?.maxTasksPerRun) ? input.limits!.maxTasksPerRun : undefined;
38
+ if (maxTasksPerRun !== undefined && input.tasks.length > maxTasksPerRun) {
39
+ decisions.push(decision("block", "limit_exceeded", `Run has ${input.tasks.length} tasks, exceeding maxTasksPerRun=${maxTasksPerRun}.`));
40
+ }
41
+ const runningCount = input.tasks.filter((task) => task.status === "running").length;
42
+ const maxConcurrentWorkers = Number.isFinite(input.limits?.maxConcurrentWorkers) ? input.limits!.maxConcurrentWorkers : undefined;
43
+ if (maxConcurrentWorkers !== undefined && runningCount > maxConcurrentWorkers) {
44
+ decisions.push(decision("block", "limit_exceeded", `Run has ${runningCount} running workers, exceeding maxConcurrentWorkers=${maxConcurrentWorkers}.`));
45
+ }
46
+ const tasksById = new Map(input.tasks.map((task) => [task.id, task]));
47
+
48
+ for (const task of input.tasks) {
49
+ if (input.limits?.maxChildrenPerTask !== undefined && (task.graph?.children.length ?? 0) > input.limits.maxChildrenPerTask) {
50
+ decisions.push(decision("block", "limit_exceeded", `Task has ${task.graph?.children.length ?? 0} children, exceeding maxChildrenPerTask=${input.limits.maxChildrenPerTask}.`, task.id));
51
+ }
52
+ if (input.limits?.maxTaskDepth !== undefined && taskDepth(task, tasksById) > input.limits.maxTaskDepth) {
53
+ decisions.push(decision("block", "limit_exceeded", `Task graph depth exceeds maxTaskDepth=${input.limits.maxTaskDepth}.`, task.id));
54
+ }
55
+ if (task.status === "failed") {
56
+ const retryCount = task.policy?.retryCount ?? 0;
57
+ const maxRetries = input.limits?.maxRetriesPerTask ?? 0;
58
+ decisions.push(decision(retryCount < maxRetries ? "retry" : "escalate", "task_failed", task.error ? `Task failed: ${task.error}` : "Task failed.", task.id));
59
+ }
60
+ if ((task.status === "running" || task.status === "queued") && task.heartbeat && task.heartbeat.alive !== false && isWorkerHeartbeatStale(task.heartbeat, input.limits?.heartbeatStaleMs ?? 60_000, input.now)) {
61
+ decisions.push(decision("escalate", "worker_stale", "Worker heartbeat is stale.", task.id));
62
+ }
63
+ if (task.taskPacket?.verification) {
64
+ const outcome = evaluateGreenContract(task.taskPacket.verification, task.verification);
65
+ if (!outcome.satisfied && task.status === "completed") {
66
+ decisions.push(decision("block", "green_unsatisfied", `Green contract unsatisfied: required=${outcome.requiredGreenLevel}, observed=${outcome.observedGreenLevel}.`, task.id));
67
+ }
68
+ }
69
+ }
70
+
71
+ if (decisions.length === 0 && input.tasks.length > 0 && input.tasks.every((task) => task.status === "completed")) {
72
+ decisions.push(decision("closeout", "run_complete", "All tasks completed and no policy blockers were found."));
73
+ }
74
+ return decisions;
75
+ }
76
+
77
+ export function summarizePolicyDecisions(decisions: PolicyDecision[]): string[] {
78
+ return decisions.map((item) => `- ${item.action} (${item.reason})${item.taskId ? ` ${item.taskId}` : ""}: ${item.message}`);
79
+ }
@@ -27,7 +27,7 @@ export function checkProcessLiveness(pid: number | undefined): ProcessLiveness {
27
27
  }
28
28
 
29
29
  export function isActiveRunStatus(status: string): boolean {
30
- return status === "queued" || status === "planning" || status === "running";
30
+ return status === "queued" || status === "planning" || status === "running" || status === "waiting";
31
31
  }
32
32
 
33
33
  export function isLikelyOrphanedActiveRun(run: TeamRunManifest, agents: CrewAgentRecord[] = [], now = Date.now(), staleMs = ORPHANED_ACTIVE_RUN_MS): boolean {