pi-evals 0.4.0 → 0.5.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.
@@ -1,4 +1,5 @@
1
- import type { ReportModel } from "../model.ts";
1
+ import { FAILURE_TYPE_META, type FailureType, ruleLabel } from "../labels.ts";
2
+ import { type ReportModel, SMALL_SAMPLE } from "../model.ts";
2
3
  import { type concepts, lesson, selfCheck } from "./concepts.ts";
3
4
  import { duration, esc, type Lang, num, pct, table, usd, words } from "./format.ts";
4
5
  import { workSection } from "./work.ts";
@@ -151,21 +152,52 @@ export function sections(m: ReportModel, lang: Lang, cards: ReturnType<typeof co
151
152
  } bytes</p>`
152
153
  : `<p>${L("Coverage not measured; keep cleanup off.", "沒有索引覆蓋資料,請勿開啟清理。")}</p>`,
153
154
  );
155
+ const lb = m.labels, rule = (r: string) => ruleLabel(r, lang);
156
+ const pair = (c: { agree: number; n: number }) => (c.n ? `${num(c.agree)} / ${num(c.n)}` : "—");
154
157
  const labels = section(
155
158
  "labels",
156
159
  L("Your labels", "你的標記"),
157
160
  meta.agreement.meaning[lang],
158
- m.labels.needsIndex ? `<p>${L("Run /evals extract to count labels in this period.", "請執行 /evals extract 以計算這段期間的標記。")}</p>` : m.labels.present && m.labels.n
159
- ? T(
160
- [L("Check", "檢查"), L("Labelled", "已標記"), L("Agreement", "一致比例"), L("Cannot tell", "看不出來")],
161
- m.labels.agreement.map((r) => [m.meta.detectors[r.detector]?.label[lang] ?? r.detector, r.n, pct(r.rate), r.unclear]),
162
- ) + `<p class="muted">${L(`Cannot tell ${num(m.labels.unclear)}: labels marked unclear are left out of the agreement rate.`, `看不出來 ${num(m.labels.unclear)}:標成「看不出來」的不算進一致比例。`)}</p>`
161
+ lb.needsIndex ? `<p>${L("Run /evals extract to count labels in this period.", "請執行 /evals extract 以計算這段期間的標記。")}</p>` : lb.present && lb.n
162
+ ? `<p>${L(`After the fact ${num(lb.bySource.batch)} (${num(lb.sessions.batch)} sessions) / In the moment ${num(lb.bySource.live)} (${num(lb.sessions.live)} sessions)`, `事後標 ${num(lb.bySource.batch)} 筆(${num(lb.sessions.batch)} 段工作) / 當下標 ${num(lb.bySource.live)} 筆(${num(lb.sessions.live)} 段工作)`)}${
163
+ lb.fromWords ? L(`; ${num(lb.fromWords)} of them the agent wrote from your words`, `;其中 ${num(lb.fromWords)} 筆是代理人依你的話整理的`) : ""
164
+ }</p>` +
165
+ T(
166
+ [L("Check", "檢查"), L("Agreement", "一致比例"), L("After the fact: agreed / compared", "事後標:一致 / 比對"), L("In the moment: agreed / compared", "當下標:一致 / 比對"), L("Cannot tell", "看不出來")],
167
+ lb.agreement.map((r) => [rule(r.detector), `${pct(r.rate)}(${num(r.n)})`, pair(r.bySource.batch), pair(r.bySource.live), r.unclear]),
168
+ ) +
169
+ `<p class="muted">${L(`Cannot tell ${num(lb.unclear)}: labels marked unclear are left out of the agreement rate.`, `看不出來 ${num(lb.unclear)}:標成「看不出來」的不算進一致比例。`)}${
170
+ lb.noCode ? L(` ${num(lb.noCode)} labels have no code verdict to compare: the session is not extracted yet (run /evals extract), or the rule did not apply to it.`, `另有 ${num(lb.noCode)} 筆沒有程式判斷可比:那段工作還沒整理(執行 /evals extract 後就能比),或這條規則不適用於它。`) : ""
171
+ }${lb.other ? L(` ${num(lb.other)} labels are on "Other", which no code check covers.`, `${num(lb.other)} 筆標在「其他」,沒有程式檢查可比。`) : ""}</p>` + lesson(cards.labelTiming, L)
163
172
  : `<p>${
164
173
  L(
165
- "No labels yet. In phase 2 you will judge sessions yourself and compare your answers with the code checks.",
166
- "還沒有標記。第二階段會讓你親自判讀工作紀錄,再對照程式與你的判斷是否一致。",
174
+ "No labels yet. Type /evals label in any session to say, while the work is in front of you, whether the agent followed a rule.",
175
+ "還沒有標記。在任何工作階段輸入 /evals label,就能趁工作還在眼前,標記代理人有沒有照規則做。",
176
+ )
177
+ }</p>` + lesson(cards.labelTiming, L),
178
+ );
179
+ const c = lb.corrections, few = c.n < SMALL_SAMPLE;
180
+ const corrections = section(
181
+ "corrections",
182
+ L("Times the agent was corrected", "被糾正"),
183
+ L(
184
+ "When you correct how the agent worked, it logs one line with evals_label. This counts the corrections it logged, not all its mistakes: a slip nobody caught, or one it forgot to log, is not here.",
185
+ "你糾正代理人的做法時,它會用 evals_label 自己記一筆。這裡數的是它記下的被糾正次數,不是所有錯誤:沒人發現、沒被糾正,或它忘了記的,都不在這裡。",
186
+ ),
187
+ c.n
188
+ ? `<p class="big">${num(c.n)} <small>${L("corrections", "次")} · ${num(c.sessions)} ${L("sessions", "段工作")}</small></p>${
189
+ few ? `<p class="stale">${L(`Only ${num(c.n)}: too few to show a trend. Read it as a hint of where slips may recur.`, `只有 ${num(c.n)} 次,太少,看不出趨勢;只能當提醒,看哪裡可能常出錯。`)}</p>` : ""
190
+ }<div class="pair"><article class="panel"><h3>${L("By rule", "依規則")}</h3>${T([L("Rule", "規則"), L("Times", "次數")], c.byRule.map((r) => [rule(r.rule), r.n]))}</article><article class="panel"><h3>${L("By kind of slip", "依錯誤類型")}</h3>${
191
+ T([L("Kind", "類型"), L("Times", "次數")], c.byType.map((r) => [FAILURE_TYPE_META[r.type as FailureType]?.[lang] ?? r.type, r.n]))
192
+ }</article></div>` +
193
+ more(
194
+ L("By skill loaded and by anchorloop version", "依載入的技能、anchorloop 版本"),
195
+ `<p class="muted">${L("Skills the session had loaded before the correction. One correction counts for each of them; this does not mean the skill caused it.", "被糾正時這段工作已載入的技能。一次糾正會算進每個已載入的技能,不代表是技能造成的。")}</p>` +
196
+ T([L("Skill", "技能"), L("Times", "次數"), L("Sessions", "工作段數")], c.bySkill.map((r) => [r.skill || L("(none loaded)", "(沒載入技能)"), r.n, r.sessions])) +
197
+ `<p class="muted">${L("The anchorloop version in use when the correction was logged.", "依糾正當時使用的 anchorloop 版本。")}</p>` +
198
+ T([L("Version", "版本"), L("Times", "次數"), L("Sessions", "工作段數")], c.byVersion.map((r) => [r.version === "unknown" ? L("unknown", "不明") : r.version, r.n, r.sessions])),
167
199
  )
168
- }</p>`,
200
+ : `<p>${L("None logged in this period. When you correct the agent, it logs one line here.", "這段期間沒有紀錄。你糾正代理人時,它會在這裡記一筆。")}</p>`,
169
201
  );
170
202
  return [
171
203
  workSection(m, lang, cards, lesson),
@@ -178,6 +210,7 @@ export function sections(m: ReportModel, lang: Lang, cards: ReturnType<typeof co
178
210
  cost,
179
211
  coverage,
180
212
  labels,
213
+ corrections,
181
214
  section(
182
215
  "concepts",
183
216
  L("Concepts", "概念"),
@@ -1,6 +1,7 @@
1
+ import { comparisonText } from "../work/wording.ts";
1
2
  /** The 「工作」 section: one row per work item, a like-with-like comparison, and the items still open. */
2
3
  import type { ReportModel } from "../model.ts";
3
- import type { Comparison, Verdict, WorkModel } from "../work/model.ts";
4
+ import type { WorkModel } from "../work/model.ts";
4
5
  import { esc, type Lang, num, usd, type Words, words } from "./format.ts";
5
6
 
6
7
  const SHOWN = 15;
@@ -11,18 +12,6 @@ export function span(minutes: number, L: Words) {
11
12
  return L(`${(minutes / 1440).toFixed(1)} days`, `${(minutes / 1440).toFixed(1)} 天`);
12
13
  }
13
14
 
14
- const pct = (v: number) => `${Math.round(v * 100)}%`;
15
-
16
- export function verdictText(c: Comparison, v: Verdict, L: Words, lines = false) {
17
- const g = (value?: string) => c.groups.find((x) => x.value === value);
18
- const n = (value?: string) => (lines ? g(value)?.nLines : g(value)?.n) ?? 0;
19
- const what = lines ? L("per 100 changed lines", "每百行") : L("per item", "每件");
20
- if (v.kind === "single") return v.a ? L(`Only one group (${v.a}): nothing to compare it with.`, `只有一組(${v.a}),沒有可比的對象。`) : L("No items with a line count.", "沒有算得出行數的工作。");
21
- if (v.kind === "few") return L(`Too few items to tell yet (${v.a} ${n(v.a)}, ${v.b} ${n(v.b)}; each group needs 5).`, `件數太少,還看不出來(${v.a} ${n(v.a)} 件、${v.b} ${n(v.b)} 件,每組至少要 5 件)。`);
22
- if (v.kind === "same") return L(`${v.a} and ${v.b} cost about the same ${what} (less than 20% apart).`, `${v.a} 和 ${v.b} ${what}花費差不到兩成,看不出差別。`);
23
- return L(`${v.a} costs ${pct(v.by ?? 0)} less ${what} than ${v.b} (median of ${n(v.a)} and ${n(v.b)} items of the same kind). This is a difference, not its cause.`, `${v.a} ${what}比 ${v.b} 便宜 ${pct(v.by ?? 0)}(同類工作的中位數,各 ${n(v.a)}、${n(v.b)} 件)。這是差異,不是原因。`);
24
- }
25
-
26
15
  function stallText(stalls: Record<string, number>, m: ReportModel, lang: Lang) {
27
16
  const L = words(lang);
28
17
  const parts = Object.entries(stalls)
@@ -55,7 +44,7 @@ export function workSection(m: ReportModel, lang: Lang, cards: { attribution: st
55
44
  (c) =>
56
45
  `<article class="panel"><h3>${esc(kinds[c.kind])} / ${L("by", "依")}${esc(dims[c.dimension])}</h3><div class="scroll"><table><thead><tr><th scope="col">${esc(dims[c.dimension])}</th><th scope="col">${L("Items", "件數")}</th><th scope="col">${L("Median per item", "每件中位數")}</th><th scope="col">${L("Per 100 lines (items)", "每百行(件數)")}</th></tr></thead><tbody>${
57
46
  c.groups.map((g) => `<tr><td>${esc(g.value)}</td><td>${num(g.n)}</td><td>${usd(g.median)}</td><td>${g.per100 === null ? "—" : `${usd(g.per100)}(${num(g.nLines)})`}</td></tr>`).join("")
58
- }</tbody></table></div><p class="verdict">${esc(verdictText(c, c.verdict, L))}</p><p class="muted">${esc(verdictText(c, c.verdictLines, L, true))}</p></article>`,
47
+ }</tbody></table></div><p class="verdict">${esc(comparisonText(c, L))} ${L("This is a difference, not its cause.", "這是差異,不是原因。")}</p></article>`,
59
48
  )
60
49
  .join("")
61
50
  : `<p class="muted">${L("No finished items to compare.", "沒有可比較的已完成工作。")}</p>`;
package/src/work/query.ts CHANGED
@@ -1,20 +1,15 @@
1
+ import { comparisonText } from "./wording.ts";
1
2
  /** The `items` view of `evals_query` and its line in `findings`: numbers, names, and titles in double quotes. */
2
- import type { Comparison, Verdict, WorkModel } from "./model.ts";
3
+ import type { Comparison, WorkModel } from "./model.ts";
3
4
 
4
5
  const usd = (v: number | null) => (v === null ? "n/a" : `$${v.toFixed(2)}`);
5
6
  const mins = (m: number) => (m < 90 ? `${Math.round(m)}m` : m < 2880 ? `${(m / 60).toFixed(1)}h` : `${(m / 1440).toFixed(1)}d`);
6
7
  /** A title for agent output: quoted, inner quotes swapped, at most 50 characters. */
7
8
  const quote = (title: string) => `"${Array.from(title.replace(/"/g, "'")).slice(0, 50).join("")}"`;
8
9
 
9
- function verdict(c: Comparison, v: Verdict, lines = false): string {
10
- const n = (value?: string) => {
11
- const g = c.groups.find((x) => x.value === value);
12
- return (lines ? g?.nLines : g?.n) ?? 0;
13
- };
14
- if (v.kind === "single") return "one group only";
15
- if (v.kind === "few") return `too few items to tell (${n(v.a)}/${n(v.b)}, need 5 each)`;
16
- if (v.kind === "same") return "no clear difference (<20%)";
17
- return `${v.a} ${Math.round((v.by ?? 0) * 100)}% less than ${v.b} (n ${n(v.a)}/${n(v.b)})`;
10
+ function summary(c: Comparison) {
11
+ const kind = c.kind === "delegated" ? "Work handed to subagents" : "Work done by the main agent";
12
+ return `${kind} by ${c.dimension}: ${comparisonText(c, en => en)}`;
18
13
  }
19
14
 
20
15
  export function itemsView(w: WorkModel | null): string[] {
@@ -28,9 +23,9 @@ export function itemsView(w: WorkModel | null): string[] {
28
23
  `median item ${usd(w.cost.median)}, ${w.cost.medianMinutes ?? "n/a"} min in progress`,
29
24
  `closed by: ${kinds || "none"}`,
30
25
  `stall time in finished items, by what woke the main: ${stalls || "none"}`,
31
- "cheaper? same kind only, per item (per 100 changed lines):",
26
+ "Median costs for similar work (differences, not causes):",
32
27
  ];
33
- for (const c of w.compare) out.push(` ${c.kind} by ${c.dimension}: ${verdict(c, c.verdict)} (${verdict(c, c.verdictLines, true)})`);
28
+ for (const c of w.compare) out.push(summary(c));
34
29
  if (w.open.length) out.push("open, stopped where / waiting on / how long:");
35
30
  for (const o of w.open.slice(0, 4)) out.push(`- ${quote(o.title || o.key)} ${o.kind}${o.on ? ` on ${o.on}` : ""}, ${mins(o.waitMinutes)}`);
36
31
  const costly = [...w.list].sort((a, b) => b.main + b.child - (a.main + a.child)).slice(0, 3);
@@ -44,10 +39,10 @@ export function itemsFinding(w: WorkModel | null): string | undefined {
44
39
  if (!w) return undefined;
45
40
  // Model first: a role split mixes kinds of work (planning vs building), and versions overlap other changes.
46
41
  const order = ["model", "anchorloop", "role"];
47
- const cheaper = w.compare.filter((c) => c.verdict.kind === "cheaper").sort((a, b) => (a.kind === b.kind ? 0 : a.kind === "delegated" ? -1 : 1) || order.indexOf(a.dimension) - order.indexOf(b.dimension))[0];
42
+ const cheaper = w.compare.filter((c) => c.verdict.kind === "cheaper" || c.verdictLines.kind === "cheaper").sort((a, b) => (a.kind === b.kind ? 0 : a.kind === "delegated" ? -1 : 1) || order.indexOf(a.dimension) - order.indexOf(b.dimension))[0];
48
43
  const waits = w.open.filter((o) => o.kind !== "human" && o.kind !== "working" && o.waitMinutes >= 60);
49
44
  const parts: string[] = [];
50
- if (cheaper) parts.push(`${cheaper.kind} items by ${cheaper.dimension}: ${verdict(cheaper, cheaper.verdict)}`);
45
+ if (cheaper) parts.push(`${summary(cheaper)} This is a difference, not its cause.`);
51
46
  if (waits.length) parts.push(`${waits.length} open item(s) waiting over 1h, longest ${mins(Math.max(...waits.map((o) => o.waitMinutes)))} on ${waits.sort((a, b) => b.waitMinutes - a.waitMinutes)[0].on ?? waits[0].kind}`);
52
47
  return parts.length ? `Work items: ${parts.join("; ")} → items` : undefined;
53
48
  }
package/src/work/track.ts CHANGED
@@ -142,6 +142,15 @@ function liteOf(raw: unknown): TaskLite | undefined {
142
142
  };
143
143
  }
144
144
 
145
+ /** A task's own key: its uid, else `<origin session>#<id>` for a task handed over from another session. */
146
+ const ownKey = (task: TaskLite) => task.uid ?? (task.origin ? `${task.origin}#${task.id}` : undefined);
147
+
148
+ /** The key of the task in progress in a live todo list, as the back-fill keys it on the session's main branch. */
149
+ export function liveItemKey(tasks: readonly unknown[], session: string): string | undefined {
150
+ const task = tasks.map(liteOf).find((x) => x?.status === "in_progress");
151
+ return task && (ownKey(task) ?? `${session}#${task.id}`);
152
+ }
153
+
145
154
  /** The kind of stall a waking entry ends, or undefined when the entry does not start a turn. */
146
155
  export function wakeOf(entry: Obj): { kind: StallKind; on?: string } | undefined {
147
156
  const type = entry.type;
@@ -346,8 +355,8 @@ export class WorkTracker {
346
355
 
347
356
  private newItem(session: string, nested: boolean, task: TaskLite, snap: Snapshot, si: number, main: Set<number>, reuse: Map<number, number>): ItemRaw {
348
357
  let key: string;
349
- if (task.uid) key = task.uid;
350
- else if (task.origin) key = `${task.origin}#${task.id}`;
358
+ const own = ownKey(task);
359
+ if (own) key = own;
351
360
  else if (main.has(si)) {
352
361
  const count = (reuse.get(task.id) ?? 0) + 1;
353
362
  reuse.set(task.id, count);
@@ -0,0 +1,26 @@
1
+ import type { Comparison, Verdict } from "./model.ts";
2
+ import type { Words } from "../report/format.ts";
3
+
4
+ export function verdictText(c: Comparison, v: Verdict, L: Words, lines = false) {
5
+ const n = (name?: string) => {
6
+ const g = c.groups.find(x => x.value === name);
7
+ return (lines ? g?.nLines : g?.n) ?? 0;
8
+ };
9
+ const what = lines ? L("per 100 changed lines", "每改 100 行") : L("per item", "每件");
10
+ if (v.kind === "single") return `${what}: ` + (v.a ? L(`only ${v.a} (${n(v.a)} items)`, `只有 ${v.a}(${n(v.a)} 件)`) : L("no items with line counts", "沒有可計算行數的工作"));
11
+ const counts = L(`${n(v.a)} vs ${n(v.b)} items`, `${n(v.a)} 件 vs ${n(v.b)} 件`);
12
+ if (v.kind === "few") return L(`${what}: too few to tell (${v.a} vs ${v.b}: ${counts}; need 5 each)`, `件數太少,還看不出來${what}誰比較省(${v.a} vs ${v.b}:${counts},每組至少 5 件)`);
13
+ if (v.kind === "same") 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
+
18
+ export function comparisonText(c: Comparison, L: Words) {
19
+ const a = c.verdict, b = c.verdictLines;
20
+ let text = verdictText(c, a, L) + L("; ", ";") + verdictText(c, b, L, true) + L(".", "。");
21
+ if ([a, b].every(v => v.kind === "same" || v.kind === "cheaper")) {
22
+ if (![a.a, a.b].includes(b.a) || ![a.a, a.b].includes(b.b)) text += L(" The two measures compare different groups.", "兩種量法比較的不是同一對組別。");
23
+ else if (a.kind !== b.kind || (a.kind === "cheaper" && a.a !== b.a)) text += L(" Item sizes may differ, so we can't say which is cheaper overall.", "兩種量法不一致,可能是工作大小不同,不能說誰比較省。");
24
+ }
25
+ return text;
26
+ }