pi-crew 0.5.5 → 0.5.6

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 (72) hide show
  1. package/CHANGELOG.md +116 -0
  2. package/README.md +17 -1
  3. package/docs/architecture.md +2 -0
  4. package/docs/migration-v0.4-v0.5.md +19 -2
  5. package/docs/pi-crew-v0.5.5-audit-fix-plan.md +133 -0
  6. package/package.json +7 -5
  7. package/src/benchmark/benchmark-runner.ts +45 -0
  8. package/src/benchmark/feedback-loop.ts +5 -0
  9. package/src/config/config.ts +10 -0
  10. package/src/config/suggestions.ts +8 -0
  11. package/src/extension/async-notifier.ts +10 -1
  12. package/src/extension/cross-extension-rpc.ts +1 -1
  13. package/src/extension/notification-router.ts +18 -0
  14. package/src/extension/register.ts +13 -17
  15. package/src/extension/registration/subagent-tools.ts +1 -1
  16. package/src/extension/team-tool/anchor.ts +201 -0
  17. package/src/extension/team-tool/api.ts +2 -1
  18. package/src/extension/team-tool/auto-summarize.ts +154 -0
  19. package/src/extension/team-tool/run.ts +37 -2
  20. package/src/extension/team-tool.ts +44 -2
  21. package/src/hooks/registry.ts +1 -3
  22. package/src/observability/event-bus.ts +13 -4
  23. package/src/observability/event-to-metric.ts +0 -2
  24. package/src/runtime/anchor-manager.ts +473 -0
  25. package/src/runtime/async-runner.ts +8 -4
  26. package/src/runtime/auto-summarize.ts +350 -0
  27. package/src/runtime/background-runner.ts +2 -1
  28. package/src/runtime/budget-tracker.ts +354 -0
  29. package/src/runtime/chain-runner.ts +507 -0
  30. package/src/runtime/child-pi.ts +1 -1
  31. package/src/runtime/crash-recovery.ts +5 -4
  32. package/src/runtime/custom-tools/irc-tool.ts +13 -0
  33. package/src/runtime/custom-tools/submit-result-tool.ts +3 -2
  34. package/src/runtime/delivery-coordinator.ts +10 -3
  35. package/src/runtime/dynamic-script-runner.ts +482 -0
  36. package/src/runtime/handoff-manager.ts +589 -0
  37. package/src/runtime/hidden-handoff.ts +424 -0
  38. package/src/runtime/live-agent-manager.ts +20 -4
  39. package/src/runtime/live-session-runtime.ts +39 -4
  40. package/src/runtime/manifest-cache.ts +2 -1
  41. package/src/runtime/model-resolver.ts +16 -4
  42. package/src/runtime/phase-tracker.ts +373 -0
  43. package/src/runtime/pipeline-runner.ts +514 -0
  44. package/src/runtime/retry-runner.ts +354 -0
  45. package/src/runtime/sandbox.ts +252 -0
  46. package/src/runtime/scheduler.ts +7 -2
  47. package/src/runtime/subagent-manager.ts +1 -1
  48. package/src/runtime/task-graph.ts +11 -1
  49. package/src/runtime/task-runner.ts +1 -1
  50. package/src/runtime/team-runner.ts +4 -3
  51. package/src/schema/team-tool-schema.ts +30 -0
  52. package/src/skills/discover-skills.ts +5 -0
  53. package/src/state/active-run-registry.ts +9 -2
  54. package/src/state/contracts.ts +9 -0
  55. package/src/state/crew-init.ts +3 -3
  56. package/src/state/decision-ledger.ts +26 -32
  57. package/src/state/event-log-rotation.ts +2 -2
  58. package/src/state/event-log.ts +9 -1
  59. package/src/state/mailbox.ts +10 -0
  60. package/src/state/run-cache.ts +18 -8
  61. package/src/tools/safe-bash-extension.ts +1 -0
  62. package/src/tools/safe-bash.ts +152 -20
  63. package/src/ui/overlays/mailbox-detail-overlay.ts +13 -2
  64. package/src/ui/powerbar-publisher.ts +1 -0
  65. package/src/ui/transcript-cache.ts +13 -0
  66. package/src/utils/bm25-search.ts +16 -8
  67. package/src/utils/env-filter.ts +8 -5
  68. package/src/utils/redaction.ts +169 -15
  69. package/src/utils/sse-parser.ts +10 -1
  70. package/src/worktree/cleanup.ts +6 -1
  71. package/workflows/chain.workflow.md +252 -0
  72. package/workflows/pipeline.workflow.md +27 -0
@@ -0,0 +1,373 @@
1
+ /**
2
+ * Phase Tracker — marks phase transitions with timestamps and metrics.
3
+ *
4
+ * Tracks workflow phases (assessment, implementation, verification, etc.)
5
+ * with start/complete/skip lifecycle and phase-level metrics.
6
+ *
7
+ * @file src/runtime/phase-tracker.ts
8
+ */
9
+
10
+ import { EventEmitter } from "node:events";
11
+
12
+ /** Phase status. */
13
+ export type PhaseStatus = "active" | "completed" | "skipped" | "failed";
14
+
15
+ /** Metrics collected for a phase. */
16
+ export interface PhaseMetrics {
17
+ /** Number of tasks completed in this phase. */
18
+ tasksCompleted?: number;
19
+ /** Number of tasks failed in this phase. */
20
+ tasksFailed?: number;
21
+ /** Total tokens used in this phase. */
22
+ tokensUsed?: number;
23
+ /** Number of subagents spawned in this phase. */
24
+ subagentsSpawned?: number;
25
+ /** Custom metadata key-value pairs. */
26
+ custom?: Record<string, unknown>;
27
+ }
28
+
29
+ /** A tracked phase. */
30
+ export interface Phase {
31
+ /** Unique phase name/identifier. */
32
+ name: string;
33
+ /** ISO timestamp when phase started. */
34
+ startTime: string;
35
+ /** ISO timestamp when phase ended (if ended). */
36
+ endTime?: string;
37
+ /** Duration in milliseconds (if ended). */
38
+ durationMs?: number;
39
+ /** Current phase status. */
40
+ status: PhaseStatus;
41
+ /** Collected metrics for this phase. */
42
+ metrics?: PhaseMetrics;
43
+ /** Order index (0-based). */
44
+ index: number;
45
+ }
46
+
47
+ /** Event emitted on phase lifecycle changes. */
48
+ export interface PhaseLifecycleEvent {
49
+ type: "phase:started" | "phase:completed" | "phase:skipped" | "phase:failed";
50
+ phase: Phase;
51
+ }
52
+
53
+ /** Default empty metrics. */
54
+ function emptyMetrics(): PhaseMetrics {
55
+ return {
56
+ tasksCompleted: 0,
57
+ tasksFailed: 0,
58
+ tokensUsed: 0,
59
+ subagentsSpawned: 0,
60
+ };
61
+ }
62
+
63
+ /**
64
+ * PhaseTracker manages workflow phase lifecycle.
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * const tracker = new PhaseTracker();
69
+ * tracker.start("assessment");
70
+ * // ... do work ...
71
+ * tracker.complete("assessment", { tasksCompleted: 5, tokensUsed: 12000 });
72
+ * tracker.start("implementation");
73
+ * ```
74
+ */
75
+ export class PhaseTracker extends EventEmitter {
76
+ private phases: Phase[] = [];
77
+ private currentPhaseName: string | null = null;
78
+ private phaseMetrics: Map<string, PhaseMetrics> = new Map();
79
+
80
+ /**
81
+ * Start a new phase, completing the previous one if any.
82
+ *
83
+ * @param name - Phase name (e.g., "assessment", "implementation").
84
+ * @param metrics - Optional initial metrics for the phase.
85
+ * @returns The started Phase object.
86
+ */
87
+ start(name: string, metrics?: PhaseMetrics): Phase {
88
+ // HIGH-8: Prevent duplicate phases - check if phase already exists
89
+ if (this.phases.some((p) => p.name === name)) {
90
+ throw new Error(`Phase "${name}" already exists. Duplicate phases are not allowed.`);
91
+ }
92
+
93
+ // Complete previous phase before starting new one (only if active)
94
+ if (this.currentPhaseName !== null) {
95
+ this.completeIfActive(this.currentPhaseName);
96
+ }
97
+
98
+ const phase: Phase = {
99
+ name,
100
+ startTime: new Date().toISOString(),
101
+ status: "active",
102
+ index: this.phases.length,
103
+ metrics: metrics ?? emptyMetrics(),
104
+ };
105
+
106
+ this.phases.push(phase);
107
+ this.currentPhaseName = name;
108
+ this.phaseMetrics.set(name, phase.metrics!);
109
+
110
+ const event: PhaseLifecycleEvent = { type: "phase:started", phase };
111
+ this.emit("phase:started", event);
112
+ return phase;
113
+ }
114
+
115
+ /**
116
+ * Complete a phase with optional metrics update.
117
+ *
118
+ * @param name - Phase name to complete.
119
+ * @param metrics - Optional metrics to merge/update.
120
+ */
121
+ complete(name: string, metrics?: Partial<PhaseMetrics>): void {
122
+ const phase = this.phases.find((p) => p.name === name);
123
+ if (!phase) {
124
+ throw new Error(`Phase "${name}" not found`);
125
+ }
126
+ if (phase.status !== "active") {
127
+ throw new Error(`Phase "${name}" is not active (status: ${phase.status})`);
128
+ }
129
+
130
+ const now = new Date();
131
+ const startMs = new Date(phase.startTime).getTime();
132
+ const endMs = now.getTime();
133
+
134
+ phase.endTime = now.toISOString();
135
+ phase.durationMs = endMs - startMs;
136
+ phase.status = "completed";
137
+
138
+ // Merge provided metrics with existing
139
+ if (metrics) {
140
+ const existing = this.phaseMetrics.get(name) ?? emptyMetrics();
141
+ phase.metrics = {
142
+ tasksCompleted: metrics.tasksCompleted ?? existing.tasksCompleted,
143
+ tasksFailed: metrics.tasksFailed ?? existing.tasksFailed,
144
+ tokensUsed: metrics.tokensUsed ?? existing.tokensUsed,
145
+ subagentsSpawned: metrics.subagentsSpawned ?? existing.subagentsSpawned,
146
+ custom: { ...existing.custom, ...metrics.custom },
147
+ };
148
+ this.phaseMetrics.set(name, phase.metrics);
149
+ }
150
+
151
+ this.emit("phase:completed", { type: "phase:completed", phase } as PhaseLifecycleEvent);
152
+ }
153
+
154
+ /**
155
+ * Skip the current active phase without metrics.
156
+ *
157
+ * @param name - Phase name to skip.
158
+ * @param reason - Optional reason for skipping.
159
+ */
160
+ skip(name: string, reason?: string): void {
161
+ const phase = this.phases.find((p) => p.name === name);
162
+ if (!phase) {
163
+ throw new Error(`Phase "${name}" not found`);
164
+ }
165
+ if (phase.status !== "active") {
166
+ throw new Error(`Phase "${name}" is not active (status: ${phase.status})`);
167
+ }
168
+
169
+ const now = new Date();
170
+ const startMs = new Date(phase.startTime).getTime();
171
+ const endMs = now.getTime();
172
+
173
+ phase.endTime = now.toISOString();
174
+ phase.durationMs = endMs - startMs;
175
+ phase.status = "skipped";
176
+
177
+ // Clear current phase since we're done with it
178
+ if (this.currentPhaseName === name) {
179
+ this.currentPhaseName = null;
180
+ }
181
+
182
+ this.emit("phase:skipped", { type: "phase:skipped", phase } as PhaseLifecycleEvent);
183
+ }
184
+
185
+ /**
186
+ * Mark a phase as failed.
187
+ *
188
+ * @param name - Phase name to fail.
189
+ * @param error - Optional error information.
190
+ */
191
+ fail(name: string, error?: string): void {
192
+ const phase = this.phases.find((p) => p.name === name);
193
+ if (!phase) {
194
+ throw new Error(`Phase "${name}" not found`);
195
+ }
196
+ if (phase.status !== "active") {
197
+ throw new Error(`Phase "${name}" is not active (status: ${phase.status})`);
198
+ }
199
+
200
+ const now = new Date();
201
+ const startMs = new Date(phase.startTime).getTime();
202
+ const endMs = now.getTime();
203
+
204
+ phase.endTime = now.toISOString();
205
+ phase.durationMs = endMs - startMs;
206
+ phase.status = "failed";
207
+
208
+ if (error) {
209
+ const existing = this.phaseMetrics.get(name) ?? emptyMetrics();
210
+ phase.metrics = { ...existing, custom: { ...existing.custom, error } };
211
+ this.phaseMetrics.set(name, phase.metrics);
212
+ }
213
+
214
+ // Clear current phase since we're done with it
215
+ if (this.currentPhaseName === name) {
216
+ this.currentPhaseName = null;
217
+ }
218
+
219
+ this.emit("phase:failed", { type: "phase:failed", phase } as PhaseLifecycleEvent);
220
+ }
221
+
222
+ /**
223
+ * Complete a phase only if it is currently active. Does not throw if the
224
+ * phase is already completed, skipped, or failed.
225
+ *
226
+ * @param name - Phase name to complete.
227
+ * @param metrics - Optional metrics to merge/update.
228
+ */
229
+ completeIfActive(name: string, metrics?: Partial<PhaseMetrics>): void {
230
+ const phase = this.phases.find((p) => p.name === name);
231
+ if (phase && phase.status === "active") {
232
+ this.complete(name, metrics);
233
+ }
234
+ }
235
+
236
+ /**
237
+ * Get all phases.
238
+ * @returns Copy of phases array.
239
+ */
240
+ getPhases(): Phase[] {
241
+ return [...this.phases];
242
+ }
243
+
244
+ /**
245
+ * Get phases filtered by status.
246
+ * @param status - Status to filter by.
247
+ * @returns Filtered phases.
248
+ */
249
+ getPhasesByStatus(status: PhaseStatus): Phase[] {
250
+ return this.phases.filter((p) => p.status === status);
251
+ }
252
+
253
+ /**
254
+ * Get the current active phase.
255
+ * @returns Current phase or null if none active.
256
+ */
257
+ getCurrentPhase(): Phase | null {
258
+ return this.phases.find((p) => p.status === "active") ?? null;
259
+ }
260
+
261
+ /**
262
+ * Get a specific phase by name.
263
+ * @param name - Phase name.
264
+ * @returns Phase or undefined.
265
+ */
266
+ getPhase(name: string): Phase | undefined {
267
+ return this.phases.find((p) => p.name === name);
268
+ }
269
+
270
+ /**
271
+ * Get metrics for a phase.
272
+ * @param name - Phase name.
273
+ * @returns Metrics or undefined.
274
+ */
275
+ getMetrics(name: string): PhaseMetrics | undefined {
276
+ return this.phaseMetrics.get(name);
277
+ }
278
+
279
+ /**
280
+ * Update metrics for the current phase.
281
+ * @param updates - Partial metrics to merge.
282
+ */
283
+ updateCurrentMetrics(updates: Partial<PhaseMetrics>): void {
284
+ if (!this.currentPhaseName) return;
285
+ const existing = this.phaseMetrics.get(this.currentPhaseName) ?? emptyMetrics();
286
+ const updated: PhaseMetrics = {
287
+ tasksCompleted: updates.tasksCompleted ?? existing.tasksCompleted,
288
+ tasksFailed: updates.tasksFailed ?? existing.tasksFailed,
289
+ tokensUsed: updates.tokensUsed ?? existing.tokensUsed,
290
+ subagentsSpawned: updates.subagentsSpawned ?? existing.subagentsSpawned,
291
+ custom: { ...existing.custom, ...updates.custom },
292
+ };
293
+ this.phaseMetrics.set(this.currentPhaseName, updated);
294
+ const phase = this.getPhase(this.currentPhaseName);
295
+ if (phase) {
296
+ phase.metrics = updated;
297
+ }
298
+ }
299
+
300
+ /**
301
+ * Add tokens to the current phase's metrics.
302
+ * @param tokens - Number of tokens to add.
303
+ */
304
+ addTokensToCurrent(tokens: number): void {
305
+ if (!this.currentPhaseName) return;
306
+ const existing = this.phaseMetrics.get(this.currentPhaseName) ?? emptyMetrics();
307
+ existing.tokensUsed = (existing.tokensUsed ?? 0) + tokens;
308
+ this.phaseMetrics.set(this.currentPhaseName, existing);
309
+ const phase = this.getPhase(this.currentPhaseName);
310
+ if (phase) {
311
+ phase.metrics = existing;
312
+ }
313
+ }
314
+
315
+ /**
316
+ * Get total duration across all completed phases.
317
+ * @returns Total milliseconds or 0.
318
+ */
319
+ totalDuration(): number {
320
+ return this.phases.reduce((sum, p) => sum + (p.durationMs ?? 0), 0);
321
+ }
322
+
323
+ /**
324
+ * Get summary statistics for all phases.
325
+ * @returns Summary object.
326
+ */
327
+ summary(): {
328
+ totalPhases: number;
329
+ active: number;
330
+ completed: number;
331
+ skipped: number;
332
+ failed: number;
333
+ totalDurationMs: number;
334
+ } {
335
+ return {
336
+ totalPhases: this.phases.length,
337
+ active: this.phases.filter((p) => p.status === "active").length,
338
+ completed: this.phases.filter((p) => p.status === "completed").length,
339
+ skipped: this.phases.filter((p) => p.status === "skipped").length,
340
+ failed: this.phases.filter((p) => p.status === "failed").length,
341
+ totalDurationMs: this.totalDuration(),
342
+ };
343
+ }
344
+
345
+ /**
346
+ * Check if a phase exists.
347
+ * @param name - Phase name.
348
+ * @returns True if phase exists.
349
+ */
350
+ hasPhase(name: string): boolean {
351
+ return this.phases.some((p) => p.name === name);
352
+ }
353
+
354
+ /**
355
+ * Reset all phases (for testing or recovery).
356
+ */
357
+ reset(): void {
358
+ this.phases = [];
359
+ this.currentPhaseName = null;
360
+ this.phaseMetrics.clear();
361
+ }
362
+
363
+ /**
364
+ * Dispose of resources (EventEmitter listeners).
365
+ * Call this when the tracker is no longer needed.
366
+ */
367
+ dispose(): void {
368
+ this.removeAllListeners();
369
+ this.phases = [];
370
+ this.currentPhaseName = null;
371
+ this.phaseMetrics.clear();
372
+ }
373
+ }