pi-evals 0.3.0 → 0.4.1
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 +13 -0
- package/README.md +7 -3
- package/dist/build.js +10 -1
- package/dist/extract/run.js +1 -1
- package/dist/extract/session.js +43 -3
- package/dist/findings.js +4 -0
- package/dist/meta.js +43 -1
- package/dist/model.js +15 -5
- package/dist/ops.js +3 -2
- package/dist/paths.js +3 -0
- package/dist/query.js +5 -1
- package/dist/render.js +1 -0
- package/dist/report/concepts.js +23 -1
- package/dist/report/sections.js +4 -2
- package/dist/report/style.js +5 -0
- package/dist/report/work.js +47 -0
- package/dist/schema.js +16 -1
- package/dist/work/join.js +429 -0
- package/dist/work/model.js +145 -0
- package/dist/work/query.js +51 -0
- package/dist/work/sb.js +131 -0
- package/dist/work/track.js +323 -0
- package/dist/work/wording.js +27 -0
- package/docs/schema.md +8 -2
- package/package.json +1 -1
- package/skills/analyzing-evals/SKILL.md +3 -1
- package/src/aggregate.ts +11 -2
- package/src/build.ts +9 -1
- package/src/extract/run.ts +1 -1
- package/src/extract/session.ts +36 -2
- package/src/findings.ts +3 -0
- package/src/meta.ts +45 -1
- package/src/model.ts +19 -6
- package/src/ops.ts +3 -2
- package/src/paths.ts +7 -0
- package/src/query.ts +4 -1
- package/src/render.ts +1 -0
- package/src/report/concepts.ts +30 -1
- package/src/report/sections.ts +6 -4
- package/src/report/style.ts +5 -0
- package/src/report/work.ts +76 -0
- package/src/schema.ts +16 -1
- package/src/work/join.ts +476 -0
- package/src/work/model.ts +179 -0
- package/src/work/query.ts +48 -0
- package/src/work/sb.ts +158 -0
- package/src/work/track.ts +397 -0
- package/src/work/wording.ts +26 -0
package/dist/work/sb.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read-only views of pi-straw-boss state under `<agent dir>/straw-boss`: review threads, car records, and the
|
|
3
|
+
* append-only event ledger. Only ids, names, tags and times are taken; post text is never read past its tag.
|
|
4
|
+
*/
|
|
5
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { safeName } from "../schema.js";
|
|
8
|
+
const obj = (v) => (v && typeof v === "object" ? v : {});
|
|
9
|
+
const str = (v) => (typeof v === "string" ? v : undefined);
|
|
10
|
+
const iso = (ms) => (typeof ms === "number" && Number.isFinite(ms) ? new Date(ms).toISOString() : undefined);
|
|
11
|
+
function readJson(file) {
|
|
12
|
+
try {
|
|
13
|
+
return JSON.parse(readFileSync(file, "utf8"));
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function readLines(file) {
|
|
20
|
+
try {
|
|
21
|
+
return readFileSync(file, "utf8")
|
|
22
|
+
.split("\n")
|
|
23
|
+
.flatMap((line) => {
|
|
24
|
+
if (!line)
|
|
25
|
+
return [];
|
|
26
|
+
try {
|
|
27
|
+
return [obj(JSON.parse(line))];
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return [];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const list = (dir) => {
|
|
39
|
+
try {
|
|
40
|
+
return readdirSync(dir);
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return [];
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
/** Threads of the sessions whose first 8 characters are in `owners`. */
|
|
47
|
+
export function readThreads(sbDir, owners) {
|
|
48
|
+
const out = new Map();
|
|
49
|
+
const root = join(sbDir, "threads");
|
|
50
|
+
for (const owner8 of list(root)) {
|
|
51
|
+
if (!owners.has(owner8))
|
|
52
|
+
continue;
|
|
53
|
+
for (const name of list(join(root, owner8))) {
|
|
54
|
+
const m = name.match(/^(T\d+)\.json$/);
|
|
55
|
+
if (!m)
|
|
56
|
+
continue;
|
|
57
|
+
const meta = obj(readJson(join(root, owner8, name)));
|
|
58
|
+
const owner = str(meta.owner);
|
|
59
|
+
if (!owner)
|
|
60
|
+
continue;
|
|
61
|
+
const posts = readLines(join(root, owner8, `${m[1]}.jsonl`));
|
|
62
|
+
const last = posts.at(-1);
|
|
63
|
+
const lastTag = str(last?.tag);
|
|
64
|
+
const firstReview = posts.find((p) => p.tag === "review" && p.from === "worker");
|
|
65
|
+
const closedAs = str(meta.closedAs) ?? (lastTag === "done" || lastTag === "blocked" ? lastTag : undefined);
|
|
66
|
+
const thread = {
|
|
67
|
+
id: m[1],
|
|
68
|
+
owner: safeName(owner),
|
|
69
|
+
worker: str(meta.worker) ? safeName(meta.worker) : undefined,
|
|
70
|
+
reviewer: str(meta.reviewer) ? safeName(meta.reviewer) : undefined,
|
|
71
|
+
created: iso(meta.created),
|
|
72
|
+
item: str(meta.item) ? safeName(meta.item) : undefined,
|
|
73
|
+
closed: iso(meta.closed) ?? (closedAs && last ? iso(last.at) : undefined),
|
|
74
|
+
closedAs: iso(meta.closed) ? (closedAs ?? "done") : closedAs,
|
|
75
|
+
firstReview: firstReview ? iso(firstReview.at) : undefined,
|
|
76
|
+
rounds: posts.filter((p) => p.tag === "review").length,
|
|
77
|
+
};
|
|
78
|
+
const bucket = out.get(thread.owner) ?? [];
|
|
79
|
+
bucket.push(thread);
|
|
80
|
+
out.set(thread.owner, bucket);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
/** Car records by coordinator session, with their ticket ids and the time they landed. */
|
|
86
|
+
export function readCars(sbDir) {
|
|
87
|
+
const out = new Map();
|
|
88
|
+
const root = join(sbDir, "trainboard");
|
|
89
|
+
for (const project of list(root)) {
|
|
90
|
+
const dir = join(root, project, "cars");
|
|
91
|
+
for (const name of list(dir)) {
|
|
92
|
+
if (!name.endsWith(".json") || name.endsWith(".notice.json"))
|
|
93
|
+
continue;
|
|
94
|
+
const rec = obj(readJson(join(dir, name)));
|
|
95
|
+
const states = Array.isArray(rec.states) ? rec.states.map(obj) : [];
|
|
96
|
+
const coordinator = str(rec.coordinator) ?? str(obj(obj(states[0]).by).session);
|
|
97
|
+
if (!coordinator)
|
|
98
|
+
continue;
|
|
99
|
+
const landed = states.findLast((s) => s.state === "landed");
|
|
100
|
+
const tickets = Array.isArray(obj(rec.car).tickets) ? obj(rec.car).tickets.filter((t) => typeof t === "string").map(safeName) : [];
|
|
101
|
+
const car = { coordinator: safeName(coordinator), tickets, landed: str(landed?.at) };
|
|
102
|
+
const bucket = out.get(car.coordinator) ?? [];
|
|
103
|
+
bucket.push(car);
|
|
104
|
+
out.set(car.coordinator, bucket);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return out;
|
|
108
|
+
}
|
|
109
|
+
/** Ledger events (sb 0.94+) by session, oldest first; an absent ledger is an empty map. */
|
|
110
|
+
export function readLedger(sbDir) {
|
|
111
|
+
const out = new Map();
|
|
112
|
+
const dir = join(sbDir, "ledger");
|
|
113
|
+
if (!existsSync(dir))
|
|
114
|
+
return out;
|
|
115
|
+
for (const name of list(dir).filter((n) => /^\d{4}-\d{2}-\d{2}\.jsonl$/.test(n)).sort()) {
|
|
116
|
+
for (const row of readLines(join(dir, name))) {
|
|
117
|
+
const at = iso(row.at);
|
|
118
|
+
const session = str(row.session);
|
|
119
|
+
if (!at || !session || row.v !== 1)
|
|
120
|
+
continue;
|
|
121
|
+
const opt = (v) => (str(v) ? safeName(v) : undefined);
|
|
122
|
+
const event = { at, kind: safeName(str(row.kind) ?? "unknown"), id: opt(row.id), session: safeName(session), why: opt(row.why), item: opt(row.item), by: opt(row.by) };
|
|
123
|
+
const bucket = out.get(event.session) ?? [];
|
|
124
|
+
bucket.push(event);
|
|
125
|
+
out.set(event.session, bucket);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
for (const events of out.values())
|
|
129
|
+
events.sort((a, b) => a.at.localeCompare(b.at));
|
|
130
|
+
return out;
|
|
131
|
+
}
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Work items of one session, rebuilt from its `todo/v1` snapshots (pi-todo-ledger), plus the facts the join needs:
|
|
3
|
+
* the main's cost over time, its idle gaps and what woke it, and the subagents, review pairs and wraps it started.
|
|
4
|
+
*
|
|
5
|
+
* An item is one todo task from its `in_progress` transition to `completed`, `deleted` or the end of the session.
|
|
6
|
+
* Snapshots are diffed along the entry tree, so a `/tree` branch or a `clear` never merges two tasks that share an id:
|
|
7
|
+
* the branch that ends with the last snapshot in the file wins, and each task id gets a new item whenever it reappears.
|
|
8
|
+
* Only the subject leaves this module as text, cut to 80 characters (`titleOf`); every other string is a name.
|
|
9
|
+
*/
|
|
10
|
+
import { safeName, titleOf } from "../schema.js";
|
|
11
|
+
export { titleOf };
|
|
12
|
+
const obj = (v) => (v && typeof v === "object" ? v : {});
|
|
13
|
+
const str = (v) => (typeof v === "string" ? v : undefined);
|
|
14
|
+
/** A main idle this long or longer is a stall. */
|
|
15
|
+
export const STALL_MS = 120_000;
|
|
16
|
+
const CHILD_REF = /^child:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
17
|
+
const STATUSES = new Set(["pending", "in_progress", "completed", "deleted"]);
|
|
18
|
+
function liteOf(raw) {
|
|
19
|
+
const t = obj(raw);
|
|
20
|
+
if (typeof t.id !== "number" || !Number.isInteger(t.id))
|
|
21
|
+
return undefined;
|
|
22
|
+
const status = (STATUSES.has(String(t.status)) ? t.status : "pending");
|
|
23
|
+
const evidence = Array.isArray(t.evidence) ? t.evidence.map(obj) : [];
|
|
24
|
+
const shas = [];
|
|
25
|
+
for (const e of evidence) {
|
|
26
|
+
const sha = str(e.sha);
|
|
27
|
+
if (sha && /^[0-9a-f]{6,40}$/i.test(sha) && !shas.includes(sha))
|
|
28
|
+
shas.push(sha);
|
|
29
|
+
}
|
|
30
|
+
// Only a `child:<subagent id>` ref is kept: it joins the child to the item. A tool call id needs no storing.
|
|
31
|
+
const refs = evidence.map((e) => str(e.ref)).filter((r) => !!r && CHILD_REF.test(r));
|
|
32
|
+
// A uid or origin that is not name-shaped would become the shared key "redacted": drop it instead.
|
|
33
|
+
const named = (v) => (typeof v === "string" && v && safeName(v) === v ? v : undefined);
|
|
34
|
+
const uid = named(t.uid);
|
|
35
|
+
const origin = named(obj(t.origin).session);
|
|
36
|
+
return {
|
|
37
|
+
id: t.id,
|
|
38
|
+
status,
|
|
39
|
+
title: titleOf(t.subject),
|
|
40
|
+
owner: str(t.owner) ? safeName(t.owner) : undefined,
|
|
41
|
+
uid,
|
|
42
|
+
origin,
|
|
43
|
+
evidence: evidence.length,
|
|
44
|
+
shas,
|
|
45
|
+
refs,
|
|
46
|
+
blockedBy: Array.isArray(t.blockedBy) ? t.blockedBy.length : 0,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/** The kind of stall a waking entry ends, or undefined when the entry does not start a turn. */
|
|
50
|
+
export function wakeOf(entry) {
|
|
51
|
+
const type = entry.type;
|
|
52
|
+
if (type === "thinking_level_change" || type === "model_change")
|
|
53
|
+
return { kind: "human" };
|
|
54
|
+
if (type === "message")
|
|
55
|
+
return obj(entry.message).role === "user" ? { kind: "human" } : undefined;
|
|
56
|
+
if (type !== "custom_message")
|
|
57
|
+
return undefined;
|
|
58
|
+
const details = obj(entry.details);
|
|
59
|
+
switch (entry.customType) {
|
|
60
|
+
case "better-show":
|
|
61
|
+
case "robot-hand-follow-up":
|
|
62
|
+
case "robot-hand-herdr":
|
|
63
|
+
return { kind: "human" };
|
|
64
|
+
case "intercom_message": {
|
|
65
|
+
const from = str(obj(details.from).id);
|
|
66
|
+
return { kind: "peer", on: from ? peerName(from) : undefined };
|
|
67
|
+
}
|
|
68
|
+
case "straw-tower":
|
|
69
|
+
return details.kind === "intercom" ? { kind: "peer", on: peerName(str(obj(details.from).id) ?? "") || undefined } : { kind: "timer" };
|
|
70
|
+
case "straw-link":
|
|
71
|
+
return { kind: "device" };
|
|
72
|
+
case "subagent_result":
|
|
73
|
+
case "subagent_ping":
|
|
74
|
+
case "subagent_status":
|
|
75
|
+
case "subagent_stop":
|
|
76
|
+
case "subagent-accept-check":
|
|
77
|
+
case "straw-boss-orphan-result":
|
|
78
|
+
case "straw-boss-child-revived": {
|
|
79
|
+
const name = str(details.name);
|
|
80
|
+
return { kind: "child", on: name ? safeName(name) : undefined };
|
|
81
|
+
}
|
|
82
|
+
case "pi-loop-monitor": {
|
|
83
|
+
if (details.deliveryGroup === "monitor-started")
|
|
84
|
+
return undefined;
|
|
85
|
+
const source = str(obj(details.trigger).source) ?? "";
|
|
86
|
+
return details.deliveryGroup === "monitor-terminal" || source.startsWith("monitor:") ? { kind: "ci" } : { kind: "timer" };
|
|
87
|
+
}
|
|
88
|
+
case "straw-auto":
|
|
89
|
+
case "straw-boss-wrap":
|
|
90
|
+
case "straw-boss-watch":
|
|
91
|
+
return { kind: "timer" };
|
|
92
|
+
default:
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const UUIDISH = /^[0-9a-f]{8}-[0-9a-f]{4}-/i;
|
|
97
|
+
/** A peer session by its first 13 characters: UUIDv7 sessions started within the same minute share the first 8. */
|
|
98
|
+
const peerName = (id) => (UUIDISH.test(id) ? id.slice(0, 13) : safeName(id));
|
|
99
|
+
export class WorkTracker {
|
|
100
|
+
snaps = [];
|
|
101
|
+
/** entry id → index of the newest todo snapshot on its path (or -1). */
|
|
102
|
+
todoAt = new Map();
|
|
103
|
+
costs = [];
|
|
104
|
+
gaps = [];
|
|
105
|
+
idleFrom;
|
|
106
|
+
launches = [];
|
|
107
|
+
childEvents = [];
|
|
108
|
+
pairs = [];
|
|
109
|
+
wraps = [];
|
|
110
|
+
last = "";
|
|
111
|
+
/** Every entry, in file order, before it is otherwise handled. */
|
|
112
|
+
entry(entry, ts) {
|
|
113
|
+
if (ts && ts > this.last)
|
|
114
|
+
this.last = ts;
|
|
115
|
+
const id = str(entry.id);
|
|
116
|
+
const parent = str(entry.parentId);
|
|
117
|
+
const inherited = parent !== undefined ? (this.todoAt.get(parent) ?? -1) : this.snaps.length - 1;
|
|
118
|
+
if (entry.type === "custom" && entry.customType === "todo/v1") {
|
|
119
|
+
const data = obj(entry.data);
|
|
120
|
+
if (Array.isArray(data.tasks)) {
|
|
121
|
+
const tasks = data.tasks.map(liteOf).filter((t) => !!t);
|
|
122
|
+
this.snaps.push({ entry: id ?? String(this.snaps.length), ts, pred: inherited, tasks });
|
|
123
|
+
if (id)
|
|
124
|
+
this.todoAt.set(id, this.snaps.length - 1);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (id)
|
|
129
|
+
this.todoAt.set(id, inherited);
|
|
130
|
+
if (this.idleFrom && ts) {
|
|
131
|
+
const wake = wakeOf(entry);
|
|
132
|
+
if (wake) {
|
|
133
|
+
this.gap(this.idleFrom, ts, wake.kind, wake.on, "idle");
|
|
134
|
+
this.idleFrom = undefined;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
assistant(ts, cost, stopReason) {
|
|
139
|
+
if (cost)
|
|
140
|
+
this.costs.push([Date.parse(ts), cost]);
|
|
141
|
+
// A turn that started from an entry we do not classify: its idle time is not a stall of a known kind.
|
|
142
|
+
this.idleFrom = typeof stopReason === "string" && stopReason !== "toolUse" ? ts : undefined;
|
|
143
|
+
}
|
|
144
|
+
/** A question tool held the main for `start`..`end`. */
|
|
145
|
+
wait(start, end, kind) {
|
|
146
|
+
this.gap(start, end, kind, undefined, "ask");
|
|
147
|
+
}
|
|
148
|
+
launch(l) {
|
|
149
|
+
this.launches.push(l);
|
|
150
|
+
}
|
|
151
|
+
child(ts, name, what) {
|
|
152
|
+
this.childEvents.push([ts, safeName(name), what]);
|
|
153
|
+
}
|
|
154
|
+
pair(p) {
|
|
155
|
+
this.pairs.push(p);
|
|
156
|
+
}
|
|
157
|
+
wrap(at, wrapped) {
|
|
158
|
+
this.wraps.push({ at, wrapped });
|
|
159
|
+
}
|
|
160
|
+
gap(start, end, kind, on, via) {
|
|
161
|
+
if (Date.parse(end) - Date.parse(start) >= STALL_MS)
|
|
162
|
+
this.gaps.push({ start, end, kind, on, via });
|
|
163
|
+
}
|
|
164
|
+
finish(session, nested) {
|
|
165
|
+
const items = this.items(session, nested);
|
|
166
|
+
if (!items.length && !this.launches.length && !this.pairs.length)
|
|
167
|
+
return undefined;
|
|
168
|
+
const end = Date.parse(this.last);
|
|
169
|
+
const spans = items.map((it) => it.active.map(([a, b]) => [Date.parse(a), b ? Date.parse(b) : end + 1]));
|
|
170
|
+
let unattributed = 0;
|
|
171
|
+
for (const [t, cost] of this.costs) {
|
|
172
|
+
const on = spans.flatMap((s, i) => (s.some(([a, b]) => t >= a && t < b) ? [i] : []));
|
|
173
|
+
if (!on.length)
|
|
174
|
+
unattributed += cost;
|
|
175
|
+
for (const i of on)
|
|
176
|
+
items[i].mainCost += cost / on.length;
|
|
177
|
+
}
|
|
178
|
+
this.gaps.forEach((g, gi) => {
|
|
179
|
+
const a = Date.parse(g.start);
|
|
180
|
+
const b = Date.parse(g.end);
|
|
181
|
+
spans.forEach((s, i) => {
|
|
182
|
+
const ms = s.reduce((sum, [x, y]) => sum + Math.max(0, Math.min(b, y) - Math.max(a, x)), 0);
|
|
183
|
+
if (ms > 0)
|
|
184
|
+
items[i].stalls.push([gi, ms]);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
for (const it of items)
|
|
188
|
+
it.mainCost = Math.round(it.mainCost * 1e6) / 1e6;
|
|
189
|
+
return {
|
|
190
|
+
items,
|
|
191
|
+
gaps: this.gaps,
|
|
192
|
+
launches: this.launches,
|
|
193
|
+
childEvents: this.childEvents,
|
|
194
|
+
pairs: this.pairs,
|
|
195
|
+
wraps: this.wraps,
|
|
196
|
+
idleSince: this.idleFrom,
|
|
197
|
+
lastActivity: this.last,
|
|
198
|
+
unattributed: Math.round(unattributed * 1e6) / 1e6,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
/** Items along each branch's chain of snapshots, the branch holding the file's last snapshot first. */
|
|
202
|
+
items(session, nested) {
|
|
203
|
+
const n = this.snaps.length;
|
|
204
|
+
if (!n)
|
|
205
|
+
return [];
|
|
206
|
+
const hasChild = new Array(n).fill(false);
|
|
207
|
+
for (const s of this.snaps)
|
|
208
|
+
if (s.pred >= 0)
|
|
209
|
+
hasChild[s.pred] = true;
|
|
210
|
+
const leaves = [...Array(n).keys()].filter((i) => !hasChild[i]).sort((a, b) => b - a);
|
|
211
|
+
const main = new Set();
|
|
212
|
+
for (let i = leaves[0]; i >= 0; i = this.snaps[i].pred)
|
|
213
|
+
main.add(i);
|
|
214
|
+
const done = new Map();
|
|
215
|
+
const order = [];
|
|
216
|
+
const reuse = new Map();
|
|
217
|
+
for (const leaf of leaves) {
|
|
218
|
+
const chain = [];
|
|
219
|
+
for (let i = leaf; i >= 0; i = this.snaps[i].pred)
|
|
220
|
+
chain.unshift(i);
|
|
221
|
+
const live = new Map();
|
|
222
|
+
const leafTs = this.snaps[leaf].ts;
|
|
223
|
+
let prevSize = -1;
|
|
224
|
+
for (const si of chain) {
|
|
225
|
+
const snap = this.snaps[si];
|
|
226
|
+
const seen = new Set();
|
|
227
|
+
const fresh = prevSize <= 0;
|
|
228
|
+
prevSize = snap.tasks.length;
|
|
229
|
+
for (const task of snap.tasks) {
|
|
230
|
+
seen.add(task.id);
|
|
231
|
+
let cur = live.get(task.id);
|
|
232
|
+
if (!cur) {
|
|
233
|
+
const identity = `${si}:${task.id}`;
|
|
234
|
+
const known = done.get(identity);
|
|
235
|
+
cur = { identity, item: known ? undefined : this.newItem(session, nested, task, snap, si, main, reuse) };
|
|
236
|
+
if (cur.item && (task.status === "completed" || task.status === "deleted") && (fresh || task.origin))
|
|
237
|
+
cur.item.carried = true;
|
|
238
|
+
live.set(task.id, cur);
|
|
239
|
+
if (cur.item) {
|
|
240
|
+
done.set(identity, cur.item);
|
|
241
|
+
order.push(cur.item);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (cur.item)
|
|
245
|
+
this.step(cur.item, task, snap.ts);
|
|
246
|
+
}
|
|
247
|
+
for (const [id, cur] of live) {
|
|
248
|
+
if (seen.has(id))
|
|
249
|
+
continue;
|
|
250
|
+
// Removed from the list (a clear, or a handover that drops it) while not finished: it was dropped here.
|
|
251
|
+
if (cur.item && (cur.item.status === "pending" || cur.item.status === "in_progress"))
|
|
252
|
+
this.step(cur.item, { ...lastLite(cur.item), status: "deleted" }, snap.ts);
|
|
253
|
+
live.delete(id);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
const sideEnd = main.has(leaf) ? null : leafTs;
|
|
257
|
+
for (const cur of live.values())
|
|
258
|
+
if (cur.item && sideEnd)
|
|
259
|
+
for (const span of cur.item.active)
|
|
260
|
+
if (span[1] === null)
|
|
261
|
+
span[1] = sideEnd;
|
|
262
|
+
}
|
|
263
|
+
return order;
|
|
264
|
+
}
|
|
265
|
+
newItem(session, nested, task, snap, si, main, reuse) {
|
|
266
|
+
let key;
|
|
267
|
+
if (task.uid)
|
|
268
|
+
key = task.uid;
|
|
269
|
+
else if (task.origin)
|
|
270
|
+
key = `${task.origin}#${task.id}`;
|
|
271
|
+
else if (main.has(si)) {
|
|
272
|
+
const count = (reuse.get(task.id) ?? 0) + 1;
|
|
273
|
+
reuse.set(task.id, count);
|
|
274
|
+
key = count === 1 ? `${session}#${task.id}` : `${session}#${task.id}.${count}`;
|
|
275
|
+
}
|
|
276
|
+
else
|
|
277
|
+
key = `${session}#${task.id}@${safeName(snap.entry).slice(0, 8)}`;
|
|
278
|
+
return {
|
|
279
|
+
key,
|
|
280
|
+
id: task.id,
|
|
281
|
+
title: task.title,
|
|
282
|
+
owner: task.owner,
|
|
283
|
+
uid: task.uid,
|
|
284
|
+
origin: task.origin,
|
|
285
|
+
level: nested ? "nested" : "top",
|
|
286
|
+
status: "pending",
|
|
287
|
+
created: snap.ts,
|
|
288
|
+
active: [],
|
|
289
|
+
evidence: 0,
|
|
290
|
+
shas: [],
|
|
291
|
+
refs: [],
|
|
292
|
+
blockedBy: 0,
|
|
293
|
+
mainCost: 0,
|
|
294
|
+
stalls: [],
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
step(item, task, ts) {
|
|
298
|
+
const was = item.status;
|
|
299
|
+
if (task.uid && item.uid !== task.uid)
|
|
300
|
+
item.key = item.uid = task.uid;
|
|
301
|
+
Object.assign(item, { title: task.title || item.title, owner: task.owner ?? item.owner, evidence: task.evidence, shas: task.shas, refs: task.refs, blockedBy: task.blockedBy });
|
|
302
|
+
if (task.status === was)
|
|
303
|
+
return;
|
|
304
|
+
item.status = task.status;
|
|
305
|
+
if (task.status === "in_progress" && was !== "in_progress") {
|
|
306
|
+
item.start ??= ts;
|
|
307
|
+
item.close = undefined;
|
|
308
|
+
item.active.push([ts, null]);
|
|
309
|
+
}
|
|
310
|
+
else if (task.status !== "in_progress" && was === "in_progress") {
|
|
311
|
+
const open = item.active.at(-1);
|
|
312
|
+
if (open && open[1] === null)
|
|
313
|
+
open[1] = ts;
|
|
314
|
+
}
|
|
315
|
+
if (task.status === "completed" || task.status === "deleted")
|
|
316
|
+
item.close = ts;
|
|
317
|
+
else if (task.status === "pending")
|
|
318
|
+
item.close = undefined;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
function lastLite(item) {
|
|
322
|
+
return { id: item.id, status: item.status, title: item.title, owner: item.owner, uid: item.uid, origin: item.origin, evidence: item.evidence, shas: item.shas, refs: item.refs, blockedBy: item.blockedBy };
|
|
323
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export function verdictText(c, v, L, lines = false) {
|
|
2
|
+
const n = (name) => {
|
|
3
|
+
const g = c.groups.find(x => x.value === name);
|
|
4
|
+
return (lines ? g?.nLines : g?.n) ?? 0;
|
|
5
|
+
};
|
|
6
|
+
const what = lines ? L("per 100 changed lines", "每改 100 行") : L("per item", "每件");
|
|
7
|
+
if (v.kind === "single")
|
|
8
|
+
return `${what}: ` + (v.a ? L(`only ${v.a} (${n(v.a)} items)`, `只有 ${v.a}(${n(v.a)} 件)`) : L("no items with line counts", "沒有可計算行數的工作"));
|
|
9
|
+
const counts = L(`${n(v.a)} vs ${n(v.b)} items`, `${n(v.a)} 件 vs ${n(v.b)} 件`);
|
|
10
|
+
if (v.kind === "few")
|
|
11
|
+
return L(`${what}: too few to tell (${v.a} vs ${v.b}: ${counts}; need 5 each)`, `件數太少,還看不出來${what}誰比較省(${v.a} vs ${v.b}:${counts},每組至少 5 件)`);
|
|
12
|
+
if (v.kind === "same")
|
|
13
|
+
return L(`${what}, ${v.a} and ${v.b} cost about the same (under 20% apart; ${counts})`, `${what},${v.a} 和 ${v.b} 花費差不到兩成(${counts})`);
|
|
14
|
+
const pct = Math.round((v.by ?? 0) * 100);
|
|
15
|
+
return L(`${what}, ${v.a} costs ${pct}% less than ${v.b} (${counts})`, `${v.a} ${what}比 ${v.b} 便宜 ${pct}%(${counts})`);
|
|
16
|
+
}
|
|
17
|
+
export function comparisonText(c, L) {
|
|
18
|
+
const a = c.verdict, b = c.verdictLines;
|
|
19
|
+
let text = verdictText(c, a, L) + L("; ", ";") + verdictText(c, b, L, true) + L(".", "。");
|
|
20
|
+
if ([a, b].every(v => v.kind === "same" || v.kind === "cheaper")) {
|
|
21
|
+
if (![a.a, a.b].includes(b.a) || ![a.a, a.b].includes(b.b))
|
|
22
|
+
text += L(" The two measures compare different groups.", "兩種量法比較的不是同一對組別。");
|
|
23
|
+
else if (a.kind !== b.kind || (a.kind === "cheaper" && a.a !== b.a))
|
|
24
|
+
text += L(" Item sizes may differ, so we can't say which is cheaper overall.", "兩種量法不一致,可能是工作大小不同,不能說誰比較省。");
|
|
25
|
+
}
|
|
26
|
+
return text;
|
|
27
|
+
}
|
package/docs/schema.md
CHANGED
|
@@ -16,7 +16,7 @@ The envelope is shaped so a replay to an OTLP backend needs no renaming.
|
|
|
16
16
|
| `trace_id` | 32 hex | One trace per user prompt: everything from one prompt to the next. Stable across re-extraction. |
|
|
17
17
|
| `span_id` | 16 hex | Stable id derived from the session id and the entry or tool call id. |
|
|
18
18
|
| `source` | `"backfill"` \| `"live"` | Which reader wrote it. |
|
|
19
|
-
| `attrs` | object | Attributes. Strings must look like names (letters, digits, `_@./:+#=\|-`, at most 160 characters); anything else is stored as `redacted`. |
|
|
19
|
+
| `attrs` | object | Attributes. Strings must look like names (letters, digits, `_@./:+#=\|-`, at most 160 characters); anything else is stored as `redacted`. The one exception is `pi.item.title`: a todo subject, cut to one line of at most 80 characters. |
|
|
20
20
|
|
|
21
21
|
Every record carries `session.id` (the pi session id from the session header) and `pi.project` (a short project key: the repository name for `~/projects/<repo>/…` and for Herdr worktrees, else the first path segment).
|
|
22
22
|
|
|
@@ -34,6 +34,12 @@ Every record carries `session.id` (the pi session id from the session header) an
|
|
|
34
34
|
| `pi.compaction` | event | A compaction entry in the session. |
|
|
35
35
|
| `pi.tools.snapshot` | event | Live, once per session at the first prompt: `pi.tools`, a sorted list of `tool=owner@version`. |
|
|
36
36
|
| `pi.skills.available` | event | Live, once per session: `pi.skills`, the names of the skills pi offered. |
|
|
37
|
+
| `pi.workitem` | span | One per todo task (written to `index/workitems.jsonl` on every build). `ts` = the first switch to `in_progress` (else creation), `end_ts` = completed or deleted, `duration_ms` = time in progress. `pi.item.key` (todo `uid`, else `<origin session>#<id>` for an import, else `<session id>#<id>`, with `.2`, `.3` when a `clear` reuses an id and `@<entry8>` for a task only on an abandoned `/tree` branch), `pi.item.title`, `pi.item.owner`, `pi.item.level` (`top` \| `nested` for a subagent's own list), `pi.item.status`, `pi.item.created`, `pi.item.close_kind` (`review_done` \| `landed` \| `wrap` \| `evidence` \| `unproven` \| `deleted` \| `open`), `pi.item.thread` (`T<n>`), `pi.item.evidence_n`, `pi.item.blocked_by_n`, `pi.usage.cost_usd` (main cost in the window, each reply split evenly among the items in progress at that moment), `pi.item.child_cost_usd` (exact cost of the joined subagent sessions), `pi.item.children` (their names), `pi.item.lines_changed` (git, from the previous item's last evidence commit in the same repository), `pi.item.sessions` (when a uid or import carried it across sessions). A `nested` item's cost is part of its subagent session, which the parent item already counts in `pi.item.child_cost_usd`: summing across levels counts it twice. A task first seen already completed or deleted (carried in by a list switch or an import) gets no record in that session. |
|
|
38
|
+
| `pi.stall` | span | A main idle of two minutes or more inside an item's window. `pi.item.key`, `pi.stall.kind` by what woke the main (`human` \| `peer` \| `child` \| `timer` \| `ci` \| `device`), `pi.stall.on` (a subagent name, a peer session's first 13 characters, or from the sb ledger `W<n>`, `L<n>`, `T<n>`), `pi.stall.via` (`idle` \| `ask` for a question tool held open). `duration_ms` is the part inside the item's window. |
|
|
39
|
+
|
|
40
|
+
### Work items
|
|
41
|
+
|
|
42
|
+
Back-fill rebuilds each session's items from its `todo/v1` snapshots, diffed along the entry tree. When producers add them, it reads todo `uid`, `origin.session` and evidence `ref` (`child:<id>` joins a subagent), `subagent` `details.item`, `boss_pair` `item`, the sb thread fields `item` and `closed`, and the sb ledger (`~/.pi/agent/straw-boss/ledger/*.jsonl`); without them it joins by names: a subagent whose name is the task owner, a reviewer `<prefix>-review*`, the sb thread whose worker is the owner, and a car whose coordinator is the session (ticket ids in the title must match). Close kind takes the first that holds: the linked thread's `done` within five minutes of the close, a car landed in the window, a successful `boss_wrap` around the close, evidence rows, else `unproven`.
|
|
37
43
|
|
|
38
44
|
### Process detectors
|
|
39
45
|
|
|
@@ -54,7 +60,7 @@ Sessions that started before 2026-11-23T10:51:07Z (the anchorloop 3.0.0 pin) get
|
|
|
54
60
|
{ "session_id": "01a2…", "evaluation_name": "anchor_before_first_edit", "label": "fail", "at": "2026-12-01T09:00:00Z", "note": "optional" }
|
|
55
61
|
```
|
|
56
62
|
|
|
57
|
-
The report joins these with `index/session-evals.json` (the code labels per session) and shows how often they agree.
|
|
63
|
+
`label` is `pass`, `fail`, or `unclear` (the reader could not tell). The report joins these with `index/session-evals.json` (the code labels per session) and shows how often they agree; `unclear` labels are left out of the agreement rate and counted on their own (「看不出來 N」).
|
|
58
64
|
|
|
59
65
|
## Change timeline
|
|
60
66
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-evals",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Usage and process records for pi: how skills, packages, ask-user, monitors and subagents are used, back-filled from past sessions, with a static report and an evals_query tool",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -6,7 +6,7 @@ description: Use when the user asks how their agents, skills or packages are doi
|
|
|
6
6
|
# Analyze agent activity
|
|
7
7
|
|
|
8
8
|
1. Call `evals_query` with `view: "findings"` first. Check the dates and last extraction; default is the latest 30 UTC dates in the index. Use `since` or `project` when the question specifies a narrower scope.
|
|
9
|
-
2. Follow **one** pointer that answers the question. For a weekly change in tool errors use `packages`; for a rule about editing use `process`. Keep the same filters. Stop when the numbers answer the question.
|
|
9
|
+
2. Follow **one** pointer that answers the question. For a weekly change in tool errors use `packages`; for a rule about editing use `process`; for what a piece of work cost, where it closed, where it stalled, or whether one way of working is cheaper, use `items`. Keep the same filters. Stop when the numbers answer the question.
|
|
10
10
|
3. If one detail is still missing, run one script below. Set `SCRIPTS` to the absolute path on the `Scripts:` line or in `details.scriptsDir`. For a default npm install, the fallback is `SCRIPTS="$HOME/.pi/agent/npm/node_modules/pi-evals/scripts"`. Check `test -f "$SCRIPTS/query-lib.mjs"` before running a query; if it fails, stop rather than guessing another path. Use exact identifiers returned by the view; none of these commands reads a transcript.
|
|
11
11
|
|
|
12
12
|
```sh
|
|
@@ -18,4 +18,6 @@ node "$SCRIPTS/package-errors.mjs" PACKAGE [PROJECT]
|
|
|
18
18
|
The first reads the saved `reports/model.json`: check its extract time and project before using the comparison. For a different saved page, prefix the command with `PI_EVALS_REPORT_RANGE=14`, `90`, `all` or `custom`. If the file is missing or out of date, say so; `/evals report` rebuilds the preset pages.
|
|
19
19
|
The other two read the last 14 complete UTC dates in the index. Skill sessions count on their start date; package errors count on the date of the call. Neither script treats zero calls as a 0% error rate.
|
|
20
20
|
|
|
21
|
+
Reading `items`: an item is one todo task from `in_progress` to done or deleted. Subagent cost is exact; main cost is an estimate, split evenly among items in progress at the same moment, so say which part you quote. Compare only like with like (the view already separates work handed to subagents from work the main did itself); when it says "too few items to tell", say that in those words rather than reading the medians. A stall says what woke the main (`human`, `peer`, `child`, `timer`, `ci`, `device`), not why it waited; `on` names the subagent, peer session or sb wait (`W12`, `L340`, `T5`) when known.
|
|
22
|
+
|
|
21
23
|
Lead with what rose, fell or held steady, and give the counts behind the rates. Say “too early to tell” when a side is short or too small; when other changes landed nearby, say “we cannot tell which change mattered.” Even a clear difference is not proof of cause. More sessions or skill loads measure activity, not better work.
|
package/src/aggregate.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/** Additive per-day aggregates. A session summary holds one per UTC day; the index sums them by day and project. */
|
|
2
2
|
import type { DetectorName, NoticeKind } from "./detectors.ts";
|
|
3
3
|
import type { Label } from "./schema.ts";
|
|
4
|
+
import type { SessionWork } from "./work/track.ts";
|
|
4
5
|
|
|
5
6
|
export interface ToolAgg {
|
|
6
7
|
calls: number;
|
|
@@ -146,8 +147,16 @@ export interface SessionSummary {
|
|
|
146
147
|
logicalId?: string;
|
|
147
148
|
/** Parent session id, when the brief's artifact path or a dispatch ledger names it. */
|
|
148
149
|
parentId?: string;
|
|
149
|
-
/** Logical ids of the subagents this session launched, with the role and model it asked for. */
|
|
150
|
-
spawned: { id: string; role: string; model: string }[];
|
|
150
|
+
/** Logical ids of the subagents this session launched, with the role and model it asked for, and its name. */
|
|
151
|
+
spawned: { id: string; role: string; model: string; name?: string }[];
|
|
152
|
+
/** The last model the session used. */
|
|
153
|
+
model?: string;
|
|
154
|
+
/** A subagent's own name, from its brief path. */
|
|
155
|
+
childName?: string;
|
|
156
|
+
/** Working directory, when it is name-shaped: where to look for evidence commits. Internal; never in a record. */
|
|
157
|
+
cwd?: string;
|
|
158
|
+
/** Work items and what the join needs about them (todo transitions, cost, idle gaps, launches). */
|
|
159
|
+
work?: SessionWork;
|
|
151
160
|
cost: number;
|
|
152
161
|
tokens: number;
|
|
153
162
|
toolCalls: number;
|
package/src/build.ts
CHANGED
|
@@ -12,6 +12,7 @@ import type { Checkpoint } from "./extract/run.ts";
|
|
|
12
12
|
import { dayOf, type Layout, type Settings } from "./paths.ts";
|
|
13
13
|
import { type EvalRecord, NAMES, traceId } from "./schema.ts";
|
|
14
14
|
import { liveFiles, readRecords } from "./store.ts";
|
|
15
|
+
import { ANCHORLOOP, joinWork, Lines, sbDirOf, writeWork } from "./work/join.ts";
|
|
15
16
|
|
|
16
17
|
export interface DaysIndex {
|
|
17
18
|
v: 1;
|
|
@@ -28,7 +29,7 @@ export interface DaysIndex {
|
|
|
28
29
|
range: { start: string; end: string };
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
export const ANCHORLOOP_PACKAGES =
|
|
32
|
+
export const ANCHORLOOP_PACKAGES = ANCHORLOOP;
|
|
32
33
|
|
|
33
34
|
function atomicWrite(file: string, text: string) {
|
|
34
35
|
mkdirSync(dirname(file), { recursive: true });
|
|
@@ -245,6 +246,13 @@ export function buildIndex(options: { layout: Layout; settings: Settings; checkp
|
|
|
245
246
|
atomicWrite(layout.days, JSON.stringify(index));
|
|
246
247
|
atomicWrite(layout.sessionRows, JSON.stringify(rows));
|
|
247
248
|
atomicWrite(layout.sessionEvals, JSON.stringify(sessionEvals));
|
|
249
|
+
const now = options.now?.() ?? new Date();
|
|
250
|
+
try {
|
|
251
|
+
const work = joinWork({ summaries, changes, sbDir: sbDirOf(options.agentDir), repos: options.settings.changeRepos, lines: new Lines(layout.lines, now), now });
|
|
252
|
+
writeWork(layout, work);
|
|
253
|
+
} catch (error) {
|
|
254
|
+
errors.push(`work items: ${error instanceof Error ? error.message : String(error)}`);
|
|
255
|
+
}
|
|
248
256
|
return { index, changes, errors };
|
|
249
257
|
}
|
|
250
258
|
|
package/src/extract/run.ts
CHANGED
|
@@ -18,7 +18,7 @@ export interface FileEntry {
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
/** Bump when extraction changes what it derives; a checkpoint from another version re-reads every file. */
|
|
21
|
-
export const EXTRACTOR_VERSION =
|
|
21
|
+
export const EXTRACTOR_VERSION = 4;
|
|
22
22
|
|
|
23
23
|
export interface Checkpoint {
|
|
24
24
|
v: 1;
|