pi-crew 0.5.0 → 0.5.2

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 (69) hide show
  1. package/CHANGELOG.md +51 -1
  2. package/README.md +1 -1
  3. package/docs/actions-reference.md +87 -0
  4. package/docs/commands-reference.md +5 -0
  5. package/docs/pi-crew-bugs.md +6 -0
  6. package/index.ts +1 -1
  7. package/package.json +18 -16
  8. package/src/benchmark/benchmark-runner.ts +245 -0
  9. package/src/benchmark/feedback-loop.ts +66 -0
  10. package/src/extension/async-notifier.ts +1 -1
  11. package/src/extension/autonomous-policy.ts +1 -1
  12. package/src/extension/cross-extension-rpc.ts +1 -1
  13. package/src/extension/plan-orchestrate.ts +322 -0
  14. package/src/extension/register.ts +31 -41
  15. package/src/extension/registration/command-utils.ts +1 -1
  16. package/src/extension/registration/commands.ts +1 -1
  17. package/src/extension/registration/compaction-guard.ts +1 -1
  18. package/src/extension/registration/subagent-helpers.ts +1 -1
  19. package/src/extension/registration/subagent-tools.ts +1 -1
  20. package/src/extension/registration/team-tool.ts +1 -1
  21. package/src/extension/registration/viewers.ts +1 -1
  22. package/src/extension/session-summary.ts +1 -1
  23. package/src/extension/team-manager-command.ts +1 -1
  24. package/src/extension/team-onboard.ts +1 -3
  25. package/src/extension/team-tool/context.ts +1 -1
  26. package/src/extension/team-tool/handle-schedule.ts +183 -0
  27. package/src/extension/team-tool/orchestrate.ts +102 -0
  28. package/src/extension/team-tool/run.ts +215 -28
  29. package/src/extension/team-tool.ts +115 -0
  30. package/src/extension/tool-result.ts +1 -1
  31. package/src/i18n.ts +1 -1
  32. package/src/observability/event-to-metric.ts +1 -1
  33. package/src/prompt/prompt-runtime.ts +1 -1
  34. package/src/runtime/background-runner.ts +27 -5
  35. package/src/runtime/crash-recovery.ts +1 -1
  36. package/src/runtime/crew-hooks.ts +240 -0
  37. package/src/runtime/custom-tools/irc-tool.ts +1 -1
  38. package/src/runtime/custom-tools/submit-result-tool.ts +1 -1
  39. package/src/runtime/diagnostic-export.ts +38 -2
  40. package/src/runtime/foreground-watchdog.ts +1 -1
  41. package/src/runtime/live-session-runtime.ts +1 -1
  42. package/src/runtime/mcp-proxy.ts +1 -1
  43. package/src/runtime/pi-spawn.ts +20 -4
  44. package/src/runtime/process-status.ts +15 -2
  45. package/src/runtime/runtime-resolver.ts +1 -1
  46. package/src/runtime/session-resources.ts +1 -1
  47. package/src/runtime/task-runner.ts +31 -1
  48. package/src/runtime/team-runner.ts +6 -0
  49. package/src/schema/team-tool-schema.ts +36 -1
  50. package/src/state/crew-init.ts +56 -38
  51. package/src/state/decision-ledger.ts +295 -0
  52. package/src/state/hook-instinct-bridge.ts +90 -0
  53. package/src/state/hook-integrations.ts +51 -0
  54. package/src/state/instinct-store.ts +249 -0
  55. package/src/state/run-graph.ts +5 -24
  56. package/src/state/run-metrics.ts +135 -0
  57. package/src/state/tiered-eval.ts +471 -0
  58. package/src/state/types-eval.ts +58 -0
  59. package/src/state/types.ts +3 -0
  60. package/src/tools/safe-bash-extension.ts +5 -5
  61. package/src/ui/crew-widget.ts +1 -1
  62. package/src/ui/pi-ui-compat.ts +1 -1
  63. package/src/ui/run-action-dispatcher.ts +1 -1
  64. package/src/ui/tool-render.ts +2 -2
  65. package/src/utils/bm25-search.ts +0 -2
  66. package/src/utils/project-detector.ts +160 -0
  67. package/test-bugs-all.mjs +1 -1
  68. package/skills/.gitkeep +0 -0
  69. package/skills/REFERENCE.md +0 -136
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Hook-to-instinct bridge - connects crewHooks events to instinct formation.
3
+ * Auto-initializes when imported.
4
+ */
5
+
6
+ import { crewHooks } from "../runtime/crew-hooks.ts";
7
+
8
+ // Lazy-initialized store and paths
9
+ let storeInstance: import("./instinct-store").InstinctStore | null = null;
10
+ let pathsInstance: typeof import("../utils/paths") | null = null;
11
+
12
+ async function getStore() {
13
+ if (!storeInstance) {
14
+ const { InstinctStore } = await import("./instinct-store");
15
+ const paths = await import("../utils/paths");
16
+ storeInstance = new InstinctStore(paths.projectCrewRoot(process.cwd()));
17
+ }
18
+ return storeInstance;
19
+ }
20
+
21
+ async function getPaths() {
22
+ if (!pathsInstance) {
23
+ pathsInstance = await import("../utils/paths");
24
+ }
25
+ return pathsInstance;
26
+ }
27
+
28
+ // Subscribe to events
29
+ crewHooks.register("task_completed", async (event) => {
30
+ try {
31
+ const store = await getStore();
32
+ if (event.data?.role) {
33
+ store.saveInstinct({
34
+ trigger: `role:${event.data.role}`,
35
+ action: "prefer",
36
+ confidence: 0.6,
37
+ scope: "global",
38
+ evidence: [`task:${event.taskId} completed`],
39
+ });
40
+ }
41
+ } catch {
42
+ // Best-effort - don't crash on instinct formation failures
43
+ }
44
+ });
45
+
46
+ crewHooks.register("task_failed", async (event) => {
47
+ try {
48
+ const store = await getStore();
49
+ if (event.data?.role) {
50
+ store.saveInstinct({
51
+ trigger: `role:${event.data.role}`,
52
+ action: "avoid",
53
+ confidence: 0.3,
54
+ scope: "global",
55
+ evidence: [`task:${event.taskId} failed`],
56
+ });
57
+ }
58
+ } catch {
59
+ // Best-effort
60
+ }
61
+ });
62
+
63
+ crewHooks.register("run_completed", async (event) => {
64
+ try {
65
+ const store = await getStore();
66
+ if (event.data?.taskCount) {
67
+ store.saveInstinct({
68
+ trigger: "run_completed",
69
+ action: `completed:${event.data.taskCount}tasks`,
70
+ confidence: 0.6,
71
+ scope: "global",
72
+ evidence: [`run:${event.runId}`],
73
+ });
74
+ }
75
+ } catch {
76
+ // Best-effort
77
+ }
78
+ });
79
+
80
+ /**
81
+ * Get instinct-based recommendations.
82
+ */
83
+ export async function getInstinctRecommendations() {
84
+ try {
85
+ const store = await getStore();
86
+ return store.getInstincts().filter((i: { confidence: number }) => i.confidence >= 0.6);
87
+ } catch {
88
+ return [];
89
+ }
90
+ }
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Hook integrations - subscribes to crewHooks and provides observability.
3
+ * Auto-initializes when imported.
4
+ */
5
+
6
+ import { crewHooks } from "../runtime/crew-hooks.ts";
7
+
8
+ // Statistics
9
+ let tasksCompleted = 0;
10
+ let tasksFailed = 0;
11
+ let runsCompleted = 0;
12
+ let runsFailed = 0;
13
+
14
+ // Subscribe to events (fire-and-forget)
15
+ crewHooks.register("task_completed", () => {
16
+ tasksCompleted++;
17
+ });
18
+
19
+ crewHooks.register("task_failed", () => {
20
+ tasksFailed++;
21
+ });
22
+
23
+ crewHooks.register("run_completed", () => {
24
+ runsCompleted++;
25
+ });
26
+
27
+ crewHooks.register("run_failed", () => {
28
+ runsFailed++;
29
+ });
30
+
31
+ /**
32
+ * Get current hook statistics.
33
+ */
34
+ export function getHookStats(): {
35
+ tasksCompleted: number;
36
+ tasksFailed: number;
37
+ runsCompleted: number;
38
+ runsFailed: number;
39
+ } {
40
+ return { tasksCompleted, tasksFailed, runsCompleted, runsFailed };
41
+ }
42
+
43
+ /**
44
+ * Reset statistics (useful for testing).
45
+ */
46
+ export function resetHookStats(): void {
47
+ tasksCompleted = 0;
48
+ tasksFailed = 0;
49
+ runsCompleted = 0;
50
+ runsFailed = 0;
51
+ }
@@ -0,0 +1,249 @@
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import { randomUUID } from "node:crypto";
4
+
5
+ /**
6
+ * Represents a learned instinct that guides agent behavior.
7
+ * Instincts can be project-scoped or global.
8
+ */
9
+ export interface Instinct {
10
+ /** Unique identifier for this instinct */
11
+ id: string;
12
+ /** What triggers this instinct */
13
+ trigger: string;
14
+ /** What action to take when triggered */
15
+ action: string;
16
+ /** Confidence level: 0.3 (low), 0.6 (medium), 0.9 (high) */
17
+ confidence: 0.3 | 0.6 | 0.9;
18
+ /** Whether this instinct applies to a project or globally */
19
+ scope: "project" | "global";
20
+ /** Project identifier (undefined for global instincts) */
21
+ projectId?: string;
22
+ /** ISO timestamp of when this instinct was created */
23
+ createdAt: string;
24
+ /** Examples/evidence supporting this instinct */
25
+ evidence: string[];
26
+ }
27
+
28
+ /** Input type for creating a new instinct (excludes auto-generated fields) */
29
+ export type NewInstinct = Omit<Instinct, "id" | "createdAt">;
30
+
31
+ const INSTINCT_FILE = "instincts.jsonl";
32
+
33
+ /**
34
+ * InstinctStore manages persistence of learned instincts using JSONL files.
35
+ * - Project instincts: `.crew/instincts/projects/{projectId}/instincts.jsonl`
36
+ * - Global instincts: `.crew/instincts/global/instincts.jsonl`
37
+ */
38
+ export class InstinctStore {
39
+ private readonly crewRoot: string;
40
+
41
+ constructor(crewRoot: string) {
42
+ this.crewRoot = crewRoot;
43
+ }
44
+
45
+ /**
46
+ * Get the file path for project instincts
47
+ */
48
+ private getProjectInstinctPath(projectId: string): string {
49
+ return path.join(this.crewRoot, "instincts", "projects", projectId, INSTINCT_FILE);
50
+ }
51
+
52
+ /**
53
+ * Get the file path for global instincts
54
+ */
55
+ private getGlobalInstinctPath(): string {
56
+ return path.join(this.crewRoot, "instincts", "global", INSTINCT_FILE);
57
+ }
58
+
59
+ /**
60
+ * Ensure a directory exists, creating it recursively if needed
61
+ */
62
+ private ensureDir(dirPath: string): void {
63
+ fs.mkdirSync(dirPath, { recursive: true });
64
+ }
65
+
66
+ /**
67
+ * Parse a JSONL file and return all instincts
68
+ */
69
+ private readInstinctsFromFile(filePath: string): Instinct[] {
70
+ if (!fs.existsSync(filePath)) {
71
+ return [];
72
+ }
73
+ try {
74
+ const content = fs.readFileSync(filePath, "utf-8");
75
+ const lines = content.split("\n").filter((line) => line.trim() !== "");
76
+ return lines.map((line) => JSON.parse(line) as Instinct);
77
+ } catch {
78
+ // If file is corrupted, return empty array
79
+ return [];
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Append a single instinct to a JSONL file
85
+ */
86
+ private appendInstinctToFile(filePath: string, instinct: Instinct): void {
87
+ const dir = path.dirname(filePath);
88
+ this.ensureDir(dir);
89
+ fs.appendFileSync(filePath, `${JSON.stringify(instinct)}\n`, "utf-8");
90
+ }
91
+
92
+ /**
93
+ * Rewrite a JSONL file with the given instincts
94
+ */
95
+ private rewriteFile(filePath: string, instincts: Instinct[]): void {
96
+ const dir = path.dirname(filePath);
97
+ this.ensureDir(dir);
98
+ const content = instincts.map((i) => JSON.stringify(i)).join("\n") + "\n";
99
+ fs.writeFileSync(filePath, content, "utf-8");
100
+ }
101
+
102
+ /**
103
+ * Save a new instinct with auto-generated id and timestamp.
104
+ * Direct scope is determined by the instinct's scope field.
105
+ *
106
+ * @param instinct - The instinct to save (without id and createdAt)
107
+ * @returns The saved instinct with generated id and createdAt
108
+ */
109
+ saveInstinct(instinct: NewInstinct): Instinct {
110
+ const savedInstinct: Instinct = {
111
+ ...instinct,
112
+ id: randomUUID(),
113
+ createdAt: new Date().toISOString(),
114
+ };
115
+
116
+ if (savedInstinct.scope === "global") {
117
+ savedInstinct.projectId = undefined;
118
+ this.appendInstinctToFile(this.getGlobalInstinctPath(), savedInstinct);
119
+ } else {
120
+ if (!savedInstinct.projectId) {
121
+ throw new Error("Project-scoped instinct requires a projectId");
122
+ }
123
+ this.appendInstinctToFile(this.getProjectInstinctPath(savedInstinct.projectId), savedInstinct);
124
+ }
125
+
126
+ return savedInstinct;
127
+ }
128
+
129
+ /**
130
+ * Get all instincts, optionally filtered by scope.
131
+ *
132
+ * @param scope - Optional filter: 'project' or 'global'
133
+ * @returns Array of instincts matching the filter
134
+ */
135
+ getInstincts(scope?: "project" | "global"): Instinct[] {
136
+ const results: Instinct[] = [];
137
+
138
+ if (!scope || scope === "project") {
139
+ const projectsDir = path.join(this.crewRoot, "instincts", "projects");
140
+ if (fs.existsSync(projectsDir)) {
141
+ for (const projectId of fs.readdirSync(projectsDir, { withFileTypes: true }).filter((e) => e.isDirectory()).map((e) => e.name)) {
142
+ const filePath = path.join(projectsDir, projectId, INSTINCT_FILE);
143
+ results.push(...this.readInstinctsFromFile(filePath));
144
+ }
145
+ }
146
+ }
147
+
148
+ if (!scope || scope === "global") {
149
+ results.push(...this.readInstinctsFromFile(this.getGlobalInstinctPath()));
150
+ }
151
+
152
+ return results;
153
+ }
154
+
155
+ /**
156
+ * Get all instincts for a specific project.
157
+ * Includes both project-scoped instincts and global instincts.
158
+ *
159
+ * @param projectId - The project identifier
160
+ * @returns Array of instincts for the project
161
+ */
162
+ getProjectInstincts(projectId: string): Instinct[] {
163
+ const projectInstincts = this.readInstinctsFromFile(this.getProjectInstinctPath(projectId));
164
+ const globalInstincts = this.readInstinctsFromFile(this.getGlobalInstinctPath());
165
+ return [...projectInstincts, ...globalInstincts];
166
+ }
167
+
168
+ /**
169
+ * Promote a project-scoped instinct to global scope.
170
+ * Creates a copy in global instincts and removes from project.
171
+ *
172
+ * @param instinctId - The instinct id to promote
173
+ * @returns The promoted instinct, or null if not found
174
+ */
175
+ promoteInstinct(instinctId: string): Instinct | null {
176
+ // Search in all project instinct files
177
+ const projectsDir = path.join(this.crewRoot, "instincts", "projects");
178
+ if (fs.existsSync(projectsDir)) {
179
+ for (const projectId of fs.readdirSync(projectsDir, { withFileTypes: true }).filter((e) => e.isDirectory()).map((e) => e.name)) {
180
+ const filePath = path.join(projectsDir, projectId, INSTINCT_FILE);
181
+ const instincts = this.readInstinctsFromFile(filePath);
182
+ const index = instincts.findIndex((i) => i.id === instinctId);
183
+
184
+ if (index !== -1) {
185
+ const instinct = instincts[index];
186
+
187
+ // Create promoted version (global)
188
+ const promotedInstinct: Instinct = {
189
+ ...instinct,
190
+ id: randomUUID(), // New id for promoted instinct
191
+ scope: "global",
192
+ projectId: undefined,
193
+ createdAt: new Date().toISOString(),
194
+ };
195
+
196
+ // Add to global instincts
197
+ this.appendInstinctToFile(this.getGlobalInstinctPath(), promotedInstinct);
198
+
199
+ // Remove from project instincts
200
+ const updatedInstincts = instincts.filter((i) => i.id !== instinctId);
201
+ this.rewriteFile(filePath, updatedInstincts);
202
+
203
+ return promotedInstinct;
204
+ }
205
+ }
206
+ }
207
+
208
+ return null;
209
+ }
210
+
211
+ /**
212
+ * Delete an instinct by id.
213
+ *
214
+ * @param instinctId - The instinct id to delete
215
+ * @returns true if deleted, false if not found
216
+ */
217
+ deleteInstinct(instinctId: string): boolean {
218
+ // Search in global instincts first
219
+ const globalPath = this.getGlobalInstinctPath();
220
+ let instincts = this.readInstinctsFromFile(globalPath);
221
+ let index = instincts.findIndex((i) => i.id === instinctId);
222
+
223
+ if (index !== -1) {
224
+ const updatedInstincts = instincts.filter((i) => i.id !== instinctId);
225
+ this.rewriteFile(globalPath, updatedInstincts);
226
+ return true;
227
+ }
228
+
229
+ // Search in project instincts
230
+ const projectsDir = path.join(this.crewRoot, "instincts", "projects");
231
+ if (fs.existsSync(projectsDir)) {
232
+ for (const projectId of fs.readdirSync(projectsDir, { withFileTypes: true }).filter((e) => e.isDirectory()).map((e) => e.name)) {
233
+ const filePath = path.join(projectsDir, projectId, INSTINCT_FILE);
234
+ instincts = this.readInstinctsFromFile(filePath);
235
+ index = instincts.findIndex((i) => i.id === instinctId);
236
+
237
+ if (index !== -1) {
238
+ const updatedInstincts = instincts.filter((i) => i.id !== instinctId);
239
+ this.rewriteFile(filePath, updatedInstincts);
240
+ return true;
241
+ }
242
+ }
243
+ }
244
+
245
+ return false;
246
+ }
247
+ }
248
+
249
+ export { getGlobalStorageDir, getProjectStorageDir } from "../utils/project-detector.ts";
@@ -57,7 +57,6 @@ export function buildRunGraph(
57
57
  workflow: manifest.workflow,
58
58
  status: manifest.status,
59
59
  createdAt: manifest.createdAt,
60
- completedAt: (manifest as Record<string, unknown>).completedAt,
61
60
  },
62
61
  });
63
62
  nodeIds.add(`run:${runId}`);
@@ -73,10 +72,7 @@ export function buildRunGraph(
73
72
  type: "task",
74
73
  name: task.role,
75
74
  metadata: {
76
- phase: (task as Record<string, unknown>).phase,
77
75
  status: task.status,
78
- agentModel: (task as Record<string, unknown>).agentModel,
79
- usage: (task as Record<string, unknown>).usage,
80
76
  startedAt: task.startedAt,
81
77
  finishedAt: task.finishedAt,
82
78
  },
@@ -99,29 +95,14 @@ export function buildRunGraph(
99
95
  });
100
96
  }
101
97
 
102
- // Edge from task to agent (if we have agent model info)
103
- const agentModel = (task as Record<string, unknown>).agentModel as string | undefined;
104
- if (agentModel) {
105
- const agentId = `agent:${agentModel.replace(/[^a-zA-Z0-9-_]/g, "_")}`;
106
- if (!nodeIds.has(agentId)) {
107
- nodeIds.add(agentId);
108
- nodes.push({ id: agentId, type: "agent", name: agentModel });
109
- }
110
- edges.push({
111
- source: agentId,
112
- target: taskId,
113
- type: "runs",
114
- weight: 0.9,
115
- });
116
- }
117
98
  }
118
99
 
119
- // Group by layer (based on phase)
100
+ // Group by layer (based on phase or role)
120
101
  const layerMap = new Map<string, string[]>();
121
102
  for (const task of tasks) {
122
- const phase = ((task as Record<string, unknown>).phase as string) ?? "unknown";
123
- if (!layerMap.has(phase)) layerMap.set(phase, []);
124
- layerMap.get(phase)!.push(`task:${task.id}`);
103
+ const layerName = task.adaptive?.phase ?? task.role;
104
+ if (!layerMap.has(layerName)) layerMap.set(layerName, []);
105
+ layerMap.get(layerName)!.push(`task:${task.id}`);
125
106
  }
126
107
 
127
108
  const layers: RunGraphLayer[] = [...layerMap.entries()].map(([name, nodeIdList]) => ({
@@ -135,7 +116,7 @@ export function buildRunGraph(
135
116
  team: manifest.team ?? "unknown",
136
117
  workflow: manifest.workflow ?? "unknown",
137
118
  createdAt: manifest.createdAt,
138
- completedAt: (manifest as Record<string, unknown>).completedAt as string | undefined,
119
+ completedAt: manifest.updatedAt,
139
120
  status: manifest.status,
140
121
  nodes,
141
122
  edges,
@@ -0,0 +1,135 @@
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import { loadRunManifestById } from "./state-store.ts";
4
+ import { projectCrewRoot, userCrewRoot } from "../utils/paths.ts";
5
+ import { atomicWriteJson, readJsonFile } from "./atomic-write.ts";
6
+ import { DEFAULT_PATHS } from "../config/defaults.ts";
7
+
8
+ /**
9
+ * Run metrics snapshot captured after a run completes (or on demand).
10
+ */
11
+ export interface RunMetrics {
12
+ runId: string;
13
+ timestamp: string;
14
+ taskCount: number;
15
+ completedCount: number;
16
+ failedCount: number;
17
+ totalTokens: number;
18
+ totalCost: number;
19
+ durationMs: number;
20
+ consistencyScore: number;
21
+ }
22
+
23
+ /** Number of recent metric files to scan when building a summary. */
24
+ const MAX_METRIC_FILES_TO_SCAN = 500;
25
+
26
+ function metricsDir(cwd: string): string {
27
+ const repoRoot = projectCrewRoot(cwd);
28
+ return path.join(repoRoot, "state", "metrics");
29
+ }
30
+
31
+ function metricsFilePath(cwd: string, runId: string): string {
32
+ return path.join(metricsDir(cwd), `${runId}.json`);
33
+ }
34
+
35
+ /**
36
+ * Collect metrics for a run by reading its manifest, tasks, and event log.
37
+ * Returns undefined if the run cannot be loaded.
38
+ */
39
+ export function collectRunMetrics(cwd: string, runId: string): RunMetrics | undefined {
40
+ const result = loadRunManifestById(cwd, runId);
41
+ if (!result) return undefined;
42
+
43
+ const { manifest, tasks } = result;
44
+ const now = new Date().toISOString();
45
+
46
+ // Aggregate token/cost from all tasks that have usage data.
47
+ let totalTokens = 0;
48
+ let totalCost = 0;
49
+ for (const task of tasks) {
50
+ if (task.usage) {
51
+ totalTokens += (task.usage.input ?? 0) + (task.usage.output ?? 0);
52
+ totalCost += task.usage.cost ?? 0;
53
+ }
54
+ }
55
+
56
+ // Count completed vs failed tasks.
57
+ let completedCount = 0;
58
+ let failedCount = 0;
59
+ for (const task of tasks) {
60
+ if (task.status === "completed") completedCount++;
61
+ else if (task.status === "failed") failedCount++;
62
+ }
63
+
64
+ // Duration: from run createdAt to updatedAt (manifest timestamps), or 0 if unavailable.
65
+ const createdAt = new Date(manifest.createdAt).getTime();
66
+ const updatedAt = new Date(manifest.updatedAt).getTime();
67
+ const durationMs = isNaN(createdAt) || isNaN(updatedAt) ? 0 : Math.max(0, updatedAt - createdAt);
68
+
69
+ // Consistency score: proportion of tasks that completed successfully among all non-skipped tasks.
70
+ const nonSkippedTasks = tasks.filter((t) => t.status !== "skipped");
71
+ const consistencyScore = nonSkippedTasks.length > 0 ? completedCount / nonSkippedTasks.length : 1.0;
72
+
73
+ return {
74
+ runId,
75
+ timestamp: now,
76
+ taskCount: tasks.length,
77
+ completedCount,
78
+ failedCount,
79
+ totalTokens,
80
+ totalCost,
81
+ durationMs,
82
+ consistencyScore: Math.round(consistencyScore * 1000) / 1000, // 3 decimal places
83
+ };
84
+ }
85
+
86
+ /**
87
+ * Persist a metrics snapshot to .crew/state/metrics/<runId>.json.
88
+ * Uses atomicWriteJson to ensure safe writes.
89
+ */
90
+ export function saveRunMetrics(cwd: string, metrics: RunMetrics): void {
91
+ const dir = metricsDir(cwd);
92
+ fs.mkdirSync(dir, { recursive: true });
93
+ atomicWriteJson(metricsFilePath(cwd, metrics.runId), metrics);
94
+ }
95
+
96
+ /**
97
+ * Load a previously saved metrics snapshot.
98
+ * Returns undefined if the file does not exist or cannot be parsed.
99
+ */
100
+ export function loadRunMetrics(cwd: string, runId: string): RunMetrics | undefined {
101
+ return readJsonFile<RunMetrics>(metricsFilePath(cwd, runId));
102
+ }
103
+
104
+ /**
105
+ * List recent metrics files up to `limit` entries (newest first).
106
+ * Returns an array of { runId, timestamp, taskCount, completedCount, failedCount, totalTokens, totalCost, durationMs, consistencyScore }.
107
+ * Gracefully skips files that cannot be read or parsed.
108
+ */
109
+ export function getRunMetricsSummary(cwd: string, limit = 25): RunMetrics[] {
110
+ const dir = metricsDir(cwd);
111
+ let entries: fs.Dirent[] = [];
112
+ try {
113
+ entries = fs.readdirSync(dir, { withFileTypes: true });
114
+ } catch {
115
+ return [];
116
+ }
117
+
118
+ const metrics: RunMetrics[] = [];
119
+ for (const entry of entries) {
120
+ if (!entry.isFile() || !entry.name.endsWith(".json")) continue;
121
+ const runId = entry.name.replace(/\.json$/, "");
122
+ const m = loadRunMetrics(cwd, runId);
123
+ if (m) metrics.push(m);
124
+ if (metrics.length >= MAX_METRIC_FILES_TO_SCAN) break;
125
+ }
126
+
127
+ // Sort newest first (by timestamp, then runId as tiebreaker).
128
+ metrics.sort((a, b) => {
129
+ const diff = new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime();
130
+ if (diff !== 0) return diff;
131
+ return b.runId.localeCompare(a.runId);
132
+ });
133
+
134
+ return metrics.slice(0, limit);
135
+ }