pi-crew 0.5.1 → 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.
- package/CHANGELOG.md +28 -0
- package/README.md +1 -1
- package/docs/actions-reference.md +87 -0
- package/docs/commands-reference.md +5 -0
- package/docs/pi-crew-bugs.md +6 -0
- package/index.ts +1 -1
- package/package.json +18 -16
- package/src/benchmark/benchmark-runner.ts +245 -0
- package/src/benchmark/feedback-loop.ts +66 -0
- package/src/extension/async-notifier.ts +1 -1
- package/src/extension/autonomous-policy.ts +1 -1
- package/src/extension/cross-extension-rpc.ts +1 -1
- package/src/extension/plan-orchestrate.ts +322 -0
- package/src/extension/register.ts +31 -41
- package/src/extension/registration/command-utils.ts +1 -1
- package/src/extension/registration/commands.ts +1 -1
- package/src/extension/registration/compaction-guard.ts +1 -1
- package/src/extension/registration/subagent-helpers.ts +1 -1
- package/src/extension/registration/subagent-tools.ts +1 -1
- package/src/extension/registration/team-tool.ts +1 -1
- package/src/extension/registration/viewers.ts +1 -1
- package/src/extension/session-summary.ts +1 -1
- package/src/extension/team-manager-command.ts +1 -1
- package/src/extension/team-tool/context.ts +1 -1
- package/src/extension/team-tool/handle-schedule.ts +183 -0
- package/src/extension/team-tool/orchestrate.ts +102 -0
- package/src/extension/team-tool/run.ts +215 -28
- package/src/extension/team-tool.ts +10 -0
- package/src/extension/tool-result.ts +1 -1
- package/src/i18n.ts +1 -1
- package/src/observability/event-to-metric.ts +1 -1
- package/src/prompt/prompt-runtime.ts +1 -1
- package/src/runtime/background-runner.ts +27 -5
- package/src/runtime/crash-recovery.ts +1 -1
- package/src/runtime/crew-hooks.ts +240 -0
- package/src/runtime/custom-tools/irc-tool.ts +1 -1
- package/src/runtime/custom-tools/submit-result-tool.ts +1 -1
- package/src/runtime/diagnostic-export.ts +38 -2
- package/src/runtime/foreground-watchdog.ts +1 -1
- package/src/runtime/live-session-runtime.ts +1 -1
- package/src/runtime/mcp-proxy.ts +1 -1
- package/src/runtime/pi-spawn.ts +20 -4
- package/src/runtime/process-status.ts +15 -2
- package/src/runtime/runtime-resolver.ts +1 -1
- package/src/runtime/session-resources.ts +1 -1
- package/src/runtime/task-runner.ts +31 -1
- package/src/runtime/team-runner.ts +6 -0
- package/src/schema/team-tool-schema.ts +24 -1
- package/src/state/crew-init.ts +56 -38
- package/src/state/decision-ledger.ts +295 -0
- package/src/state/hook-instinct-bridge.ts +90 -0
- package/src/state/hook-integrations.ts +51 -0
- package/src/state/instinct-store.ts +249 -0
- package/src/state/run-metrics.ts +135 -0
- package/src/state/tiered-eval.ts +471 -0
- package/src/state/types-eval.ts +58 -0
- package/src/state/types.ts +3 -0
- package/src/tools/safe-bash-extension.ts +5 -5
- package/src/ui/crew-widget.ts +1 -1
- package/src/ui/pi-ui-compat.ts +1 -1
- package/src/ui/run-action-dispatcher.ts +1 -1
- package/src/ui/tool-render.ts +2 -2
- package/src/utils/project-detector.ts +160 -0
- package/test-bugs-all.mjs +1 -1
- package/skills/.gitkeep +0 -0
- package/skills/REFERENCE.md +0 -136
|
@@ -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";
|
|
@@ -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
|
+
}
|