sakura-infinity 0.0.0-stage → 0.1.0
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/AGENT-GUIDE.md +104 -0
- package/LICENSE +21 -0
- package/README.md +275 -2
- package/dist/bin/flags.js +110 -0
- package/dist/bin/infinity.js +98 -0
- package/dist/bin/load-task.js +39 -0
- package/dist/bin/model.js +37 -0
- package/dist/bin/scaffold.js +121 -0
- package/dist/src/agent-worker.js +156 -0
- package/dist/src/backoff.js +12 -0
- package/dist/src/budget.js +49 -0
- package/dist/src/capacity.js +103 -0
- package/dist/src/clock.js +16 -0
- package/dist/src/code-leaderboard.js +48 -0
- package/dist/src/code-report.js +157 -0
- package/dist/src/command-class.js +59 -0
- package/dist/src/command-limits.js +46 -0
- package/dist/src/commit-plugin.js +36 -0
- package/dist/src/component.js +1 -0
- package/dist/src/csv.js +8 -0
- package/dist/src/defaults.js +35 -0
- package/dist/src/error-cluster.js +20 -0
- package/dist/src/evaluation.js +84 -0
- package/dist/src/form.js +191 -0
- package/dist/src/gate.js +111 -0
- package/dist/src/gpu-shim.js +165 -0
- package/dist/src/hidden-keeper.js +183 -0
- package/dist/src/host.js +180 -0
- package/dist/src/jsonl.js +30 -0
- package/dist/src/ladder.js +118 -0
- package/dist/src/launch.js +123 -0
- package/dist/src/lean-kit.js +42 -0
- package/dist/src/lean-runner.js +84 -0
- package/dist/src/lean-setup.js +23 -0
- package/dist/src/lean-tool.js +57 -0
- package/dist/src/leftover.js +59 -0
- package/dist/src/machine-config.js +43 -0
- package/dist/src/machine-sampler.js +103 -0
- package/dist/src/machine-summary.js +64 -0
- package/dist/src/mode-factory.js +25 -0
- package/dist/src/mode.js +1 -0
- package/dist/src/network.js +12 -0
- package/dist/src/orchestrator.js +375 -0
- package/dist/src/planner.js +98 -0
- package/dist/src/platform.js +21 -0
- package/dist/src/pool.js +73 -0
- package/dist/src/pressure-brake.js +26 -0
- package/dist/src/proc-readings.js +207 -0
- package/dist/src/prompts.js +46 -0
- package/dist/src/records.js +2 -0
- package/dist/src/report.js +81 -0
- package/dist/src/review-loop.js +141 -0
- package/dist/src/review-rules.js +104 -0
- package/dist/src/review-run.js +110 -0
- package/dist/src/review-schedule.js +53 -0
- package/dist/src/review-snapshot.js +150 -0
- package/dist/src/run-layout.js +29 -0
- package/dist/src/run-notes.js +61 -0
- package/dist/src/run-recorder.js +51 -0
- package/dist/src/run-state.js +17 -0
- package/dist/src/run-status.js +37 -0
- package/dist/src/rung-meter.js +39 -0
- package/dist/src/safety-review.js +43 -0
- package/dist/src/sakura-tools.js +130 -0
- package/dist/src/shell-kit.js +113 -0
- package/dist/src/slot-gate.js +136 -0
- package/dist/src/slot-leases.js +64 -0
- package/dist/src/stuck.js +47 -0
- package/dist/src/submit-tool.js +99 -0
- package/dist/src/task-mode.js +170 -0
- package/dist/src/task.js +85 -0
- package/dist/src/verifier.js +115 -0
- package/dist/src/work-repo.js +50 -0
- package/dist/src/worker-policy.js +16 -0
- package/dist/tasks/math-odd-sum/task.js +6 -0
- package/dist/tasks/math-parity/task.js +6 -0
- package/dist/viewer/index.html +397 -0
- package/dist/viewer/run-reader.js +600 -0
- package/dist/viewer/server.js +160 -0
- package/dist/viewer/view.js +50 -0
- package/package.json +52 -4
|
@@ -0,0 +1,600 @@
|
|
|
1
|
+
// Reads the files of one run directory into a model for the viewer. Read-only; from src/ it takes only pure functions (the ranking), so the page and the report rank the same way.
|
|
2
|
+
// Every file is optional; every line may be half written, unknown or missing fields; the model never throws on bad input.
|
|
3
|
+
import { closeSync, existsSync, openSync, readdirSync, readFileSync, readSync, statSync } from "node:fs";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { rankCodeAgents } from "../src/code-leaderboard.js";
|
|
6
|
+
const STALE_MS = 3 * 60_000;
|
|
7
|
+
const CLIP = 600;
|
|
8
|
+
function isRec(v) {
|
|
9
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
10
|
+
}
|
|
11
|
+
function num(v) {
|
|
12
|
+
return typeof v === "number" && Number.isFinite(v) ? v : null;
|
|
13
|
+
}
|
|
14
|
+
function str(v) {
|
|
15
|
+
return typeof v === "string" ? v : "";
|
|
16
|
+
}
|
|
17
|
+
function pick(...values) {
|
|
18
|
+
for (const v of values) {
|
|
19
|
+
const n = num(v);
|
|
20
|
+
if (n !== null)
|
|
21
|
+
return n;
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
function clip(s) {
|
|
26
|
+
return s.length > CLIP ? `${s.slice(0, CLIP)}...` : s;
|
|
27
|
+
}
|
|
28
|
+
/** Turns a flat or nested machine record into one shape. Returns null without a usable time. */
|
|
29
|
+
export function normalizeMachine(raw) {
|
|
30
|
+
if (!isRec(raw))
|
|
31
|
+
return null;
|
|
32
|
+
const ts = num(raw.ts);
|
|
33
|
+
if (ts === null)
|
|
34
|
+
return null;
|
|
35
|
+
const load = isRec(raw.load) ? raw.load : {};
|
|
36
|
+
const memory = isRec(raw.memory) ? raw.memory : {};
|
|
37
|
+
const gpu = isRec(raw.gpu) ? raw.gpu : {};
|
|
38
|
+
const run = isRec(raw.run) ? raw.run : {};
|
|
39
|
+
const cores = num(raw.cores);
|
|
40
|
+
const load1 = pick(raw.load1, load.one);
|
|
41
|
+
return {
|
|
42
|
+
ts,
|
|
43
|
+
cores,
|
|
44
|
+
load1,
|
|
45
|
+
loadPerCore: load1 !== null && cores !== null && cores > 0 ? load1 / cores : null,
|
|
46
|
+
cpuBusy: num(raw.cpuBusy),
|
|
47
|
+
cpuPressure10: num(raw.cpuPressure10),
|
|
48
|
+
cpuPressure60: num(raw.cpuPressure60),
|
|
49
|
+
memUsedMb: pick(raw.memUsedMb, memory.usedMb),
|
|
50
|
+
memTotalMb: pick(raw.memTotalMb, memory.totalMb),
|
|
51
|
+
gpuUtil: pick(raw.gpuUtilPercent, gpu.utilPercent),
|
|
52
|
+
gpuMemMb: pick(raw.gpuMemUsedMb, gpu.memoryUsedMb),
|
|
53
|
+
slotsInUse: num(raw.slotsInUse),
|
|
54
|
+
slotsWaiting: num(raw.slotsWaiting),
|
|
55
|
+
gpuSlotsInUse: num(raw.gpuSlotsInUse),
|
|
56
|
+
gpuSlotsWaiting: num(raw.gpuSlotsWaiting),
|
|
57
|
+
runCpuCores: num(raw.runCpuCores),
|
|
58
|
+
runRssMb: num(raw.runRssMb),
|
|
59
|
+
agents: pick(raw.agents, run.agentsLive),
|
|
60
|
+
rung: num(raw.rung),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** Splits CSV text into rows of cells. Handles quoted cells, doubled quotes and a missing last newline. */
|
|
64
|
+
export function parseCsv(text) {
|
|
65
|
+
const rows = [];
|
|
66
|
+
let row = [];
|
|
67
|
+
let cell = "";
|
|
68
|
+
let quoted = false;
|
|
69
|
+
for (let i = 0; i < text.length; i++) {
|
|
70
|
+
const c = text[i];
|
|
71
|
+
if (quoted) {
|
|
72
|
+
if (c === '"') {
|
|
73
|
+
if (text[i + 1] === '"') {
|
|
74
|
+
cell += '"';
|
|
75
|
+
i++;
|
|
76
|
+
}
|
|
77
|
+
else
|
|
78
|
+
quoted = false;
|
|
79
|
+
}
|
|
80
|
+
else
|
|
81
|
+
cell += c;
|
|
82
|
+
}
|
|
83
|
+
else if (c === '"')
|
|
84
|
+
quoted = true;
|
|
85
|
+
else if (c === ",") {
|
|
86
|
+
row.push(cell);
|
|
87
|
+
cell = "";
|
|
88
|
+
}
|
|
89
|
+
else if (c === "\n" || c === "\r") {
|
|
90
|
+
if (c === "\r" && text[i + 1] === "\n")
|
|
91
|
+
i++;
|
|
92
|
+
row.push(cell);
|
|
93
|
+
cell = "";
|
|
94
|
+
if (row.length > 1 || row[0] !== "")
|
|
95
|
+
rows.push(row);
|
|
96
|
+
row = [];
|
|
97
|
+
}
|
|
98
|
+
else
|
|
99
|
+
cell += c;
|
|
100
|
+
}
|
|
101
|
+
if (cell !== "" || row.length > 0) {
|
|
102
|
+
row.push(cell);
|
|
103
|
+
if (row.length > 1 || row[0] !== "")
|
|
104
|
+
rows.push(row);
|
|
105
|
+
}
|
|
106
|
+
return rows;
|
|
107
|
+
}
|
|
108
|
+
/** Shrinks one event for the page: long text clipped, the review snapshot reduced to its summary fields. */
|
|
109
|
+
function trimEvent(raw) {
|
|
110
|
+
const out = {};
|
|
111
|
+
for (const [k, v] of Object.entries(raw)) {
|
|
112
|
+
if (k === "snapshot" && isRec(v)) {
|
|
113
|
+
out.summary = clip(str(v.summary));
|
|
114
|
+
out.assessment = str(v.assessment);
|
|
115
|
+
out.steer = clip(str(v.steer));
|
|
116
|
+
out.reason = clip(str(v.reason));
|
|
117
|
+
}
|
|
118
|
+
else if (typeof v === "string")
|
|
119
|
+
out[k] = clip(v);
|
|
120
|
+
else if (typeof v === "number" || typeof v === "boolean" || v === null)
|
|
121
|
+
out[k] = v;
|
|
122
|
+
else if (isRec(v) && Object.keys(v).length < 20)
|
|
123
|
+
out[k] = v;
|
|
124
|
+
}
|
|
125
|
+
return out;
|
|
126
|
+
}
|
|
127
|
+
export class RunReader {
|
|
128
|
+
dir;
|
|
129
|
+
name;
|
|
130
|
+
now;
|
|
131
|
+
machineOverride;
|
|
132
|
+
tails = new Map();
|
|
133
|
+
fileStamp = new Map();
|
|
134
|
+
version = 0;
|
|
135
|
+
events = [];
|
|
136
|
+
agents = new Map();
|
|
137
|
+
submits = [];
|
|
138
|
+
liveDeltas = [];
|
|
139
|
+
machine = [];
|
|
140
|
+
machinePath = null;
|
|
141
|
+
state = null;
|
|
142
|
+
facts = null;
|
|
143
|
+
/** The ledger lines the ranking needs (every agent's work directory, and the results), read as they arrive. */
|
|
144
|
+
ranked = [];
|
|
145
|
+
lastLedgerTs = null;
|
|
146
|
+
lastEventTs = null;
|
|
147
|
+
constructor(dir, options = {}) {
|
|
148
|
+
this.dir = dir;
|
|
149
|
+
this.name = dir.split(/[\\/]/).filter(Boolean).pop() ?? dir;
|
|
150
|
+
this.machineOverride = options.machineFile ?? null;
|
|
151
|
+
this.now = options.now ?? Date.now;
|
|
152
|
+
}
|
|
153
|
+
/** Reads whatever is new in the files. Returns true when the model changed. */
|
|
154
|
+
poll() {
|
|
155
|
+
const before = this.version;
|
|
156
|
+
this.readLines(join(this.dir, "events.jsonl"), (r) => this.onEvent(r));
|
|
157
|
+
this.readLines(join(this.dir, "ledger.jsonl"), (r) => this.onLedger(r));
|
|
158
|
+
this.readWhole(join(this.dir, "state.json"), (text) => {
|
|
159
|
+
const parsed = tryJson(text);
|
|
160
|
+
this.state = isRec(parsed) ? parsed : this.state;
|
|
161
|
+
});
|
|
162
|
+
this.readWhole(join(this.dir, "run-facts.json"), (text) => {
|
|
163
|
+
const parsed = tryJson(text);
|
|
164
|
+
this.facts = isRec(parsed) ? parsed : this.facts;
|
|
165
|
+
});
|
|
166
|
+
const machinePath = this.machineOverride ?? this.findMachineFile();
|
|
167
|
+
if (machinePath) {
|
|
168
|
+
if (machinePath !== this.machinePath) {
|
|
169
|
+
this.machinePath = machinePath;
|
|
170
|
+
this.machine = [];
|
|
171
|
+
this.tails.delete(machinePath);
|
|
172
|
+
}
|
|
173
|
+
this.readLines(machinePath, (r) => {
|
|
174
|
+
const sample = normalizeMachine(r);
|
|
175
|
+
if (sample) {
|
|
176
|
+
this.machine.push(sample);
|
|
177
|
+
this.version++;
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
return this.version !== before;
|
|
182
|
+
}
|
|
183
|
+
findMachineFile() {
|
|
184
|
+
const own = join(this.dir, "machine.jsonl");
|
|
185
|
+
if (existsSync(own))
|
|
186
|
+
return own;
|
|
187
|
+
// Older runs: the monitor wrote one file next to the runs directory (<root>/monitor/*.jsonl). Take the one that overlaps this run in time.
|
|
188
|
+
const start = this.startedAt();
|
|
189
|
+
if (start === null)
|
|
190
|
+
return null;
|
|
191
|
+
const monitorDir = join(dirname(dirname(this.dir)), "monitor");
|
|
192
|
+
let names = [];
|
|
193
|
+
try {
|
|
194
|
+
names = readdirSync(monitorDir).filter((n) => n.endsWith(".jsonl"));
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
const end = this.endedAt() ?? this.now();
|
|
200
|
+
for (const n of names) {
|
|
201
|
+
const path = join(monitorDir, n);
|
|
202
|
+
const range = jsonlRange(path);
|
|
203
|
+
if (range && range[0] <= end && range[1] >= start)
|
|
204
|
+
return path;
|
|
205
|
+
}
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
startedAt() {
|
|
209
|
+
const s = num(this.state?.startedAt);
|
|
210
|
+
if (s !== null)
|
|
211
|
+
return s;
|
|
212
|
+
const e = this.events.find((x) => x.type === "run_start");
|
|
213
|
+
return e ? num(e.ts) : null;
|
|
214
|
+
}
|
|
215
|
+
endedAt() {
|
|
216
|
+
const boundary = this.events.findLast((x) => x.type === "run_start" || x.type === "run_end");
|
|
217
|
+
return boundary?.type === "run_end" ? num(boundary.ts) : null;
|
|
218
|
+
}
|
|
219
|
+
metricDirection() {
|
|
220
|
+
const start = this.events.findLast((event) => event.type === "run_start" && typeof event.metricDirection === "string");
|
|
221
|
+
return start?.metricDirection === "min" ? "min" : "max";
|
|
222
|
+
}
|
|
223
|
+
onEvent(r) {
|
|
224
|
+
const ts = num(r.ts);
|
|
225
|
+
if (typeof r.type !== "string")
|
|
226
|
+
return;
|
|
227
|
+
if (ts !== null)
|
|
228
|
+
this.lastEventTs = Math.max(this.lastEventTs ?? 0, ts);
|
|
229
|
+
this.events.push(trimEvent(r));
|
|
230
|
+
this.version++;
|
|
231
|
+
const agent = num(r.agent);
|
|
232
|
+
if (agent !== null && ts !== null && (r.type === "review" || r.type === "action_denied")) {
|
|
233
|
+
const row = this.agentRow(agent);
|
|
234
|
+
if (r.type === "review")
|
|
235
|
+
row.reviews++;
|
|
236
|
+
else
|
|
237
|
+
row.denied++;
|
|
238
|
+
if (row.lastTs === null || ts >= row.lastTs) {
|
|
239
|
+
row.lastTs = ts;
|
|
240
|
+
row.lastKind = r.type === "review" ? `review: ${str(r.outcome)}` : `denied: ${str(r.tool)}`;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
agentRow(agent) {
|
|
245
|
+
let row = this.agents.get(agent);
|
|
246
|
+
if (!row) {
|
|
247
|
+
row = {
|
|
248
|
+
agent,
|
|
249
|
+
startedAt: null,
|
|
250
|
+
endedAt: null,
|
|
251
|
+
endDetail: "",
|
|
252
|
+
commits: 0,
|
|
253
|
+
submits: 0,
|
|
254
|
+
bestScore: null,
|
|
255
|
+
lastTs: null,
|
|
256
|
+
lastKind: "",
|
|
257
|
+
reviews: 0,
|
|
258
|
+
denied: 0,
|
|
259
|
+
};
|
|
260
|
+
this.agents.set(agent, row);
|
|
261
|
+
}
|
|
262
|
+
return row;
|
|
263
|
+
}
|
|
264
|
+
onLedger(r) {
|
|
265
|
+
const ts = num(r.ts);
|
|
266
|
+
const agent = num(r.agent);
|
|
267
|
+
if (typeof r.type !== "string" || agent === null)
|
|
268
|
+
return;
|
|
269
|
+
this.version++;
|
|
270
|
+
if (ts !== null)
|
|
271
|
+
this.lastLedgerTs = Math.max(this.lastLedgerTs ?? 0, ts);
|
|
272
|
+
this.ranked.push({
|
|
273
|
+
type: r.type === "result" ? "result" : "lifecycle",
|
|
274
|
+
agent,
|
|
275
|
+
workDir: str(r.workDir),
|
|
276
|
+
status: str(r.status),
|
|
277
|
+
score: num(r.score),
|
|
278
|
+
selfReported: r.selfReported === true,
|
|
279
|
+
commit: typeof r.commit === "string" ? r.commit : null,
|
|
280
|
+
ts,
|
|
281
|
+
command: str(r.command),
|
|
282
|
+
files: Array.isArray(r.files) ? r.files.filter((f) => typeof f === "string") : [],
|
|
283
|
+
});
|
|
284
|
+
const row = this.agentRow(agent);
|
|
285
|
+
const touch = (kind) => {
|
|
286
|
+
if (ts !== null && (row.lastTs === null || ts >= row.lastTs)) {
|
|
287
|
+
row.lastTs = ts;
|
|
288
|
+
row.lastKind = kind;
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
if (r.type === "lifecycle") {
|
|
292
|
+
const event = str(r.event);
|
|
293
|
+
if (event === "started") {
|
|
294
|
+
if (ts !== null && (row.startedAt === null || row.endedAt !== null))
|
|
295
|
+
this.liveDeltas.push([ts, 1]);
|
|
296
|
+
row.startedAt = ts;
|
|
297
|
+
row.endedAt = null;
|
|
298
|
+
row.endDetail = "";
|
|
299
|
+
}
|
|
300
|
+
else if (event === "ended" || event === "stopped") {
|
|
301
|
+
row.endedAt = ts;
|
|
302
|
+
row.endDetail = clip(str(r.detail));
|
|
303
|
+
if (ts !== null)
|
|
304
|
+
this.liveDeltas.push([ts, -1]);
|
|
305
|
+
}
|
|
306
|
+
touch(`${event}${str(r.detail) ? `: ${clip(str(r.detail)).slice(0, 80)}` : ""}`);
|
|
307
|
+
}
|
|
308
|
+
else if (r.type === "commit") {
|
|
309
|
+
row.commits++;
|
|
310
|
+
touch("commit");
|
|
311
|
+
}
|
|
312
|
+
else if (r.type === "result") {
|
|
313
|
+
row.submits++;
|
|
314
|
+
const score = num(r.score);
|
|
315
|
+
const ok = str(r.status) === "ok" && score !== null;
|
|
316
|
+
this.submits.push({ ts: ts ?? 0, agent, score: ok ? score : null, status: str(r.status), selfReported: r.selfReported === true });
|
|
317
|
+
touch(ok ? `submit ${score}` : `submit ${str(r.status)}`);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
/** Reads new complete lines of a JSONL file from the stored offset. A half-written last line is left for the next poll. */
|
|
321
|
+
readLines(path, onRecord) {
|
|
322
|
+
let size;
|
|
323
|
+
try {
|
|
324
|
+
size = statSync(path).size;
|
|
325
|
+
}
|
|
326
|
+
catch {
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
const tail = this.tails.get(path) ?? { offset: 0 };
|
|
330
|
+
if (size < tail.offset) {
|
|
331
|
+
// The file shrank: it was replaced. Start again for this file only; rebuilding everything is the caller's job (new reader).
|
|
332
|
+
tail.offset = 0;
|
|
333
|
+
}
|
|
334
|
+
if (size === tail.offset) {
|
|
335
|
+
this.tails.set(path, tail);
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
let text;
|
|
339
|
+
try {
|
|
340
|
+
const fd = openSync(path, "r");
|
|
341
|
+
try {
|
|
342
|
+
const buffer = Buffer.alloc(size - tail.offset);
|
|
343
|
+
const read = readSync(fd, buffer, 0, buffer.length, tail.offset);
|
|
344
|
+
text = buffer.subarray(0, read).toString("utf8");
|
|
345
|
+
}
|
|
346
|
+
finally {
|
|
347
|
+
closeSync(fd);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
catch {
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
let consumed = 0;
|
|
354
|
+
while (consumed < text.length) {
|
|
355
|
+
const nl = text.indexOf("\n", consumed);
|
|
356
|
+
const line = nl === -1 ? text.slice(consumed) : text.slice(consumed, nl);
|
|
357
|
+
if (nl === -1) {
|
|
358
|
+
// No newline yet: take the line only when it is already complete JSON.
|
|
359
|
+
const parsed = line.trim() === "" ? undefined : tryJson(line);
|
|
360
|
+
if (isRec(parsed)) {
|
|
361
|
+
onRecord(parsed);
|
|
362
|
+
consumed = text.length;
|
|
363
|
+
}
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
consumed = nl + 1;
|
|
367
|
+
if (line.trim() === "")
|
|
368
|
+
continue;
|
|
369
|
+
const parsed = tryJson(line);
|
|
370
|
+
if (isRec(parsed))
|
|
371
|
+
onRecord(parsed);
|
|
372
|
+
}
|
|
373
|
+
tail.offset += Buffer.byteLength(text.slice(0, consumed), "utf8");
|
|
374
|
+
this.tails.set(path, tail);
|
|
375
|
+
}
|
|
376
|
+
/** Reads a small whole file again when its size or time changed. */
|
|
377
|
+
readWhole(path, onText) {
|
|
378
|
+
let stamp;
|
|
379
|
+
try {
|
|
380
|
+
const s = statSync(path);
|
|
381
|
+
stamp = `${s.size}:${s.mtimeMs}`;
|
|
382
|
+
}
|
|
383
|
+
catch {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
if (this.fileStamp.get(path) === stamp)
|
|
387
|
+
return;
|
|
388
|
+
try {
|
|
389
|
+
onText(readFileSync(path, "utf8"));
|
|
390
|
+
this.fileStamp.set(path, stamp);
|
|
391
|
+
this.version++;
|
|
392
|
+
}
|
|
393
|
+
catch {
|
|
394
|
+
// Being replaced right now: try again at the next poll.
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
// ----- views -----
|
|
398
|
+
summary() {
|
|
399
|
+
const start = this.startedAt();
|
|
400
|
+
const boundary = this.events.findLast((x) => x.type === "run_start" || x.type === "run_end");
|
|
401
|
+
const runEnd = boundary?.type === "run_end" ? boundary : undefined;
|
|
402
|
+
const runStart = this.events.findLast((x) => x.type === "run_start");
|
|
403
|
+
const metricDirection = this.metricDirection();
|
|
404
|
+
const better = metricDirection === "min" ? Math.min : Math.max;
|
|
405
|
+
const rungEvents = this.events.filter((x) => x.type === "rung");
|
|
406
|
+
const ladder = isRec(this.state?.ladder) ? this.state.ladder : null;
|
|
407
|
+
const ladderRungs = Array.isArray(ladder?.rungs) ? ladder.rungs.filter((v) => typeof v === "number") : [];
|
|
408
|
+
const lastRung = rungEvents.at(-1);
|
|
409
|
+
const rung = lastRung ? num(lastRung.to) : ladderRungs.length > 0 ? ladderRungs[0] : null;
|
|
410
|
+
const scored = this.submits.flatMap(({ score }) => (score === null ? [] : [score]));
|
|
411
|
+
const best = pick(this.state?.bestScore, runEnd?.bestScore, scored.reduce((best, score) => (best === null ? score : better(best, score)), null));
|
|
412
|
+
const lastTs = Math.max(this.lastEventTs ?? 0, this.lastLedgerTs ?? 0, this.machine.at(-1)?.ts ?? 0) || null;
|
|
413
|
+
const ended = runEnd ? num(runEnd.ts) : null;
|
|
414
|
+
const hasData = this.events.length > 0 || this.agents.size > 0 || this.state !== null;
|
|
415
|
+
let state = "empty";
|
|
416
|
+
const endReason = str(runEnd?.reason) || (boundary === undefined ? str(this.state?.finished) : "");
|
|
417
|
+
if (hasData) {
|
|
418
|
+
if (runEnd || endReason)
|
|
419
|
+
state = "finished";
|
|
420
|
+
else if (lastTs !== null && this.now() - lastTs > STALE_MS)
|
|
421
|
+
state = "stopped";
|
|
422
|
+
else
|
|
423
|
+
state = "running";
|
|
424
|
+
}
|
|
425
|
+
let live = 0;
|
|
426
|
+
let started = 0;
|
|
427
|
+
for (const a of this.agents.values()) {
|
|
428
|
+
if (a.startedAt !== null) {
|
|
429
|
+
started++;
|
|
430
|
+
if (a.endedAt === null)
|
|
431
|
+
live++;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
const leaderboard = rankCodeAgents(this.ranked, metricDirection).map(({ rank, agent, score, selfReported, submits, errors }) => ({
|
|
435
|
+
rank,
|
|
436
|
+
agent,
|
|
437
|
+
score,
|
|
438
|
+
selfReported,
|
|
439
|
+
submits,
|
|
440
|
+
errors,
|
|
441
|
+
}));
|
|
442
|
+
return {
|
|
443
|
+
dir: this.dir,
|
|
444
|
+
name: this.name,
|
|
445
|
+
task: str(runStart?.task) || str(this.state?.task),
|
|
446
|
+
state,
|
|
447
|
+
endReason,
|
|
448
|
+
startedAt: start,
|
|
449
|
+
endedAt: ended,
|
|
450
|
+
lastTs,
|
|
451
|
+
ceiling: num(runStart?.ceiling) ?? num(this.facts?.ceiling),
|
|
452
|
+
tokensUsed: pick(runEnd?.tokensUsed, this.state?.tokensUsed),
|
|
453
|
+
requests: num(this.state?.requests),
|
|
454
|
+
bestScore: best,
|
|
455
|
+
metricDirection,
|
|
456
|
+
agentsStarted: started,
|
|
457
|
+
agentsLive: state === "running" ? live : 0,
|
|
458
|
+
rung,
|
|
459
|
+
rungs: ladderRungs,
|
|
460
|
+
submits: this.submits.length,
|
|
461
|
+
scoredSubmits: scored.length,
|
|
462
|
+
reviews: this.events.filter((x) => x.type === "review").length,
|
|
463
|
+
refusals: this.events.filter((x) => x.type === "action_denied").length,
|
|
464
|
+
throttles: this.events.filter((x) => x.type === "throttle").length,
|
|
465
|
+
warnings: this.events.filter((x) => x.type === "warning").map((x) => str(x.message)),
|
|
466
|
+
facts: this.facts,
|
|
467
|
+
machineSource: this.machinePath ?? "",
|
|
468
|
+
machineSamples: this.machine.length,
|
|
469
|
+
// computed from the ledger at every read, so it is never behind the scores (report/leaderboard.csv is only a snapshot)
|
|
470
|
+
leaderboard,
|
|
471
|
+
best: this.bestResult(leaderboard),
|
|
472
|
+
tokenBudget: num(runStart?.tokenBudget),
|
|
473
|
+
wallMinutes: num(runStart?.wallMinutes),
|
|
474
|
+
version: this.version,
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
/** The result line behind the top of the ranking: its command, files, folder and commit. */
|
|
478
|
+
bestResult(leaderboard) {
|
|
479
|
+
const top = leaderboard[0];
|
|
480
|
+
if (!top || top.score === null)
|
|
481
|
+
return null;
|
|
482
|
+
const line = this.ranked.findLast((entry) => entry.type === "result" && entry.agent === top.agent && entry.score === top.score);
|
|
483
|
+
if (!line)
|
|
484
|
+
return null;
|
|
485
|
+
return {
|
|
486
|
+
agent: top.agent,
|
|
487
|
+
score: top.score,
|
|
488
|
+
selfReported: top.selfReported,
|
|
489
|
+
ts: line.ts,
|
|
490
|
+
command: line.command,
|
|
491
|
+
files: line.files,
|
|
492
|
+
workDir: line.workDir,
|
|
493
|
+
commit: line.commit,
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
agentRows() {
|
|
497
|
+
const better = this.metricDirection() === "min" ? Math.min : Math.max;
|
|
498
|
+
const best = new Map();
|
|
499
|
+
for (const { agent, score } of this.submits) {
|
|
500
|
+
if (score !== null)
|
|
501
|
+
best.set(agent, better(best.get(agent) ?? score, score));
|
|
502
|
+
}
|
|
503
|
+
return [...this.agents.values()].sort((a, b) => a.agent - b.agent).map((row) => ({ ...row, bestScore: best.get(row.agent) ?? null }));
|
|
504
|
+
}
|
|
505
|
+
/** Everything the charts need. Machine readings are thinned to at most `maxPoints`. */
|
|
506
|
+
series(maxPoints = 2000) {
|
|
507
|
+
const start = this.startedAt();
|
|
508
|
+
const rungs = this.events
|
|
509
|
+
.filter((x) => x.type === "rung")
|
|
510
|
+
.map((x) => ({ ts: num(x.ts) ?? 0, from: num(x.from) ?? 0, to: num(x.to) ?? 0, reason: str(x.reason) }));
|
|
511
|
+
const throttles = this.events.filter((x) => x.type === "throttle").map((x) => ({ ts: num(x.ts) ?? 0, delayMs: num(x.delayMs) ?? 0, cause: str(x.cause) }));
|
|
512
|
+
const brakes = this.events.filter((x) => x.type === "pressure_hold").map((x) => ({ ts: num(x.ts) ?? 0, on: x.on === true }));
|
|
513
|
+
const warnings = this.events.filter((x) => x.type === "warning").map((x) => ({ ts: num(x.ts) ?? 0, message: str(x.message) }));
|
|
514
|
+
const slotWaits = this.events
|
|
515
|
+
.filter((x) => x.type === "slot_wait")
|
|
516
|
+
.map((x) => ({ ts: num(x.ts) ?? 0, kind: str(x.kind), agent: num(x.agent) ?? 0, waitMs: num(x.waitMs) ?? 0 }));
|
|
517
|
+
const ordered = [...this.liveDeltas].sort((a, b) => a[0] - b[0]);
|
|
518
|
+
let live = 0;
|
|
519
|
+
const agentsLive = ordered.map(([ts, d]) => {
|
|
520
|
+
live += d;
|
|
521
|
+
return [ts, live];
|
|
522
|
+
});
|
|
523
|
+
const stride = Math.max(1, Math.ceil(this.machine.length / maxPoints));
|
|
524
|
+
const machine = this.machine.filter((_, i) => i % stride === 0 || i === this.machine.length - 1);
|
|
525
|
+
const summary = this.summary();
|
|
526
|
+
const end = summary.endedAt ?? summary.lastTs ?? start;
|
|
527
|
+
return {
|
|
528
|
+
startedAt: start,
|
|
529
|
+
endAt: end,
|
|
530
|
+
initialRung: summary.rungs[0] ?? rungs[0]?.from ?? 1,
|
|
531
|
+
rungs,
|
|
532
|
+
submits: this.submits,
|
|
533
|
+
throttles,
|
|
534
|
+
brakes,
|
|
535
|
+
warnings,
|
|
536
|
+
slotWaits,
|
|
537
|
+
agentsLive,
|
|
538
|
+
machine,
|
|
539
|
+
version: this.version,
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
/** Events after time `since`, optionally of given types, at most `limit` (the newest). */
|
|
543
|
+
eventList(options = {}) {
|
|
544
|
+
const since = options.since ?? 0;
|
|
545
|
+
const types = options.types && options.types.length > 0 ? new Set(options.types) : null;
|
|
546
|
+
const out = this.events.filter((e) => (num(e.ts) ?? 0) > since && (types === null || types.has(str(e.type))));
|
|
547
|
+
return out.slice(-(options.limit ?? 500));
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
function tryJson(text) {
|
|
551
|
+
try {
|
|
552
|
+
return JSON.parse(text);
|
|
553
|
+
}
|
|
554
|
+
catch {
|
|
555
|
+
return undefined;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
function jsonlRange(path) {
|
|
559
|
+
try {
|
|
560
|
+
const lines = readFileSync(path, "utf8")
|
|
561
|
+
.split("\n")
|
|
562
|
+
.filter((l) => l.trim() !== "");
|
|
563
|
+
const first = num(tryJson(lines[0] ?? "")?.ts);
|
|
564
|
+
let last = null;
|
|
565
|
+
for (let i = lines.length - 1; i >= 0 && last === null; i--)
|
|
566
|
+
last = num(tryJson(lines[i])?.ts);
|
|
567
|
+
return first !== null && last !== null ? [first, last] : null;
|
|
568
|
+
}
|
|
569
|
+
catch {
|
|
570
|
+
return null;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
/** True when the directory looks like a run directory (has one of the run files). */
|
|
574
|
+
export function isRunDir(dir) {
|
|
575
|
+
return ["events.jsonl", "ledger.jsonl", "state.json"].some((f) => existsSync(join(dir, f)));
|
|
576
|
+
}
|
|
577
|
+
/** Run directories directly under `dir`, newest first (by the newest modification time of their main files). */
|
|
578
|
+
export function listRuns(dir) {
|
|
579
|
+
let names;
|
|
580
|
+
try {
|
|
581
|
+
names = readdirSync(dir);
|
|
582
|
+
}
|
|
583
|
+
catch {
|
|
584
|
+
return [];
|
|
585
|
+
}
|
|
586
|
+
const runs = [];
|
|
587
|
+
for (const name of names) {
|
|
588
|
+
const path = join(dir, name);
|
|
589
|
+
try {
|
|
590
|
+
if (!statSync(path).isDirectory() || !isRunDir(path))
|
|
591
|
+
continue;
|
|
592
|
+
const mtime = Math.max(...["events.jsonl", "ledger.jsonl", "state.json"].map((f) => (existsSync(join(path, f)) ? statSync(join(path, f)).mtimeMs : 0)));
|
|
593
|
+
runs.push({ name, path, mtime });
|
|
594
|
+
}
|
|
595
|
+
catch {
|
|
596
|
+
// vanished while listing
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
return runs.sort((a, b) => b.mtime - a.mtime || (a.name < b.name ? 1 : -1));
|
|
600
|
+
}
|