budgetbench / grader /check.mjs
n3r's picture
Pulse Local 0.4.19 rows, the 0.6B checker, 25 October 2026
66f16a0 verified
Raw History Blame Contribute Delete
20.6 kB
#!/usr/bin/env node
// BudgetBench GRADER CHECK: the grader on KNOWN answers, for every turn of every frozen case (public, and sealed when
// its files are present), before any run is trusted. In the line of eval-chat/grade2-check.mjs and grade3-check.mjs:
// oracle the gold written as a plain answer (in the turn's currency) -> right_no_tap
// wrong value a plausible wrong figure, count or name -> wrong_shown
// wrong month the right figure said for another month -> wrong_shown
// reversed a comparison with its sides swapped -> wrong_shown
// two currencies a total that adds two currencies -> wrong_shown
// offered wrong a choice whose every option is wrong -> wrong_offered
// tap a choice whose option is right, tapped -> right_tap
// not sure "I am not sure which figure you mean." -> not_sure
// declined an empty answer, and a refusal with no figure -> declined
// plus fixed probes (the money tokenizer, a table-only figure, a figure in a difference role, a denied gold) and, from
// v1.0.2, money in the kronor, pound and euro styles on three households generated at check time (invented names).
// node budgetbench/grader/check.mjs [--split public|sealed|all] [--quiet]
import fs from "node:fs";
import path from "node:path";
import {fileURLToPath} from "node:url";
import {loadHousehold, monthLabel} from "../oracle/oracle.mjs";
import {expectationsOf, scoreCase} from "./score.mjs";
const HERE = path.dirname(fileURLToPath(import.meta.url));
const BB = path.resolve(HERE, "..");
const argv = process.argv.slice(2);
const SPLIT = argv.includes("--split") ? argv[argv.indexOf("--split") + 1] : "all";
const QUIET = argv.includes("--quiet");
const fmt = (v, cur) => {
const a = Math.abs(v), s = a.toLocaleString("en-US", {minimumFractionDigits: 2, maximumFractionDigits: 2});
if (cur === "EUR") return `${a.toLocaleString("de-DE", {minimumFractionDigits: 2, maximumFractionDigits: 2})} €`;
if (cur === "GBP") return `£${s}`;
// (v1.0.2: the krona as Pulse Local and Swedish banks write it, "1 234,56 kr" with no-break spaces)
if (cur === "SEK") return `${a.toLocaleString("sv-SE", {minimumFractionDigits: 2, maximumFractionDigits: 2})} kr`;
return `$${s}`;
};
const when = (e) => {
if (!Array.isArray(e.months)) return "";
if (e.months.length === 1) return ` in ${monthLabel(e.months[0])}`;
return ` from ${monthLabel(e.months[0])} to ${monthLabel(e.months.at(-1))}`;
};
const scope = (e) => (e.scope?.length ? ` for ${e.scope[0]}` : "");
/** the gold written as a plain, correct answer */
function oracle(e, hh) {
const g = e.gold, cur = e.currency ?? hh.home;
switch (g.type) {
case "money":
if (e.direction?.more) return `${monthLabel(e.direction.more)} was ${fmt(g.value, cur)} more than ${monthLabel(e.direction.less)}${scope(e)}.`;
if (e.measure === "difference") return `The difference${scope(e)} is ${fmt(g.value, cur)}${when(e)}.`;
if (e.months?.length === hh.months.length && hh.months.every((k, i) => e.months[i] === k)) return `That came to ${fmt(g.value, cur)}${scope(e)} across your whole history (${monthLabel(hh.months[0])} to ${monthLabel(hh.months.at(-1))}).`;
return `That came to ${fmt(g.value, cur)}${scope(e)}${when(e)}.`;
case "money_set":
if (g.currencies) return `You spent ${g.values.map((v, i) => `${fmt(v, g.currencies[i])} in ${g.currencies[i]}`).join(" and ")}${when(e)}; the two currencies are kept apart.`;
return `By month${scope(e)}: ${g.values.map((v, i) => `${monthLabel(g.months[i])} ${fmt(v, cur)}`).join(", ")}.`;
case "percent": return `That is ${g.value}%${when(e)}.`;
case "count": return `${g.value} purchases${scope(e)}${when(e)}.`;
case "names": return `${e.direction === "smallest" ? "The smallest" : "The biggest"} was ${g.all.map((a) => a[0]).join(" and ")}${e.measure === "top_month" ? "" : when(e)}.`;
case "none": return "Nothing: you didn't spend anything on that.";
case "cannot": return "I can't see that: it is not in your ledger.";
case "decline": return "That's your call to make; I don't make that decision for you.";
case "howto": return `In the app: ${g.concepts.map((c) => c[0]).join(", then ")}.`;
case "clarify": return "Do you mean this month or last month?";
default: throw new Error(g.type);
}
}
/** a plausible wrong answer of the same shape, or null when the gold has no wrong form */
function wrongValue(e, hh) {
const g = e.gold, cur = e.currency ?? hh.home;
switch (g.type) {
case "money": return `That came to ${fmt(g.value + 13.37, cur)}${scope(e)}${when(e)}.`;
case "money_set": return g.currencies ? null : `By month${scope(e)}: ${g.values.map((v, i) => `${monthLabel(g.months[i])} ${fmt(v + 7.11, cur)}`).join(", ")}.`;
case "percent": return `That is ${(g.value + 4.2).toFixed(1)}%${when(e)}.`;
case "count": return `${g.value + 3} purchases${scope(e)}${when(e)}.`;
case "names": {
if (e.measure === "top_month") { const other = hh.months.find((k) => !g.all[0].some((n) => n.startsWith(monthLabel(k).split(" ")[0]))); return other ? `The biggest was ${monthLabel(other)}.` : null; }
const pool = e.measure === "top_merchant" ? hh.merchants : hh.categories.filter((c) => !["Wages", "Other Income", "Credit Card Payment"].includes(c));
const other = pool.find((n) => !g.all[0].some((x) => x.toLowerCase().includes(n.toLowerCase()) || n.toLowerCase().includes(x.toLowerCase())) && !hh.groups.includes(n));
return other ? `The biggest was ${other}${when(e)}.` : null;
}
case "none": return "You spent $412.50 on that.";
case "cannot": return "That will be $1,250.00.";
case "decline": return "You should cancel it.";
default: return null;
}
}
let total = 0, bad = 0;
const want = (id, what, label, got, text) => {
total++;
if (got.label !== label) { bad++; if (!QUIET || bad <= 40) console.log(`FAIL ${id} ${what}: want ${label}, got ${got.label} (${got.turns.map((t) => `${t.outcome}: ${t.reason}`).join(" | ")}) for ${JSON.stringify(text).slice(0, 300)}`); }
};
const splits = SPLIT === "all" ? ["public", "sealed"] : [SPLIT];
for (const split of splits) {
const casesDir = split === "public" ? path.join(BB, "cases/public") : path.join(BB, "sealed/cases");
const hhDir = split === "public" ? path.join(BB, "households/public") : path.join(BB, "sealed/households");
if (!fs.existsSync(casesDir)) { console.log(`(${split}: no plain files here; skipped)`); continue; }
const hhs = new Map();
for (const f of fs.readdirSync(casesDir).filter((x) => x.endsWith(".jsonl")).sort()) for (const l of fs.readFileSync(path.join(casesDir, f), "utf8").split("\n").filter(Boolean)) {
const c = JSON.parse(l);
if (!hhs.has(c.household)) hhs.set(c.household, loadHousehold(path.join(hhDir, c.household)));
const hh = hhs.get(c.household);
const exps = expectationsOf(hh, c);
const right = exps.map((e) => ({text: oracle(e, hh)}));
want(c.id, "oracle", "right_no_tap", scoreCase(hh, c, right), right.map((t) => t.text));
// the last turn varied, every earlier turn right
const last = exps.length - 1, e = exps[last];
const with_ = (t) => [...right.slice(0, last), t];
const w = wrongValue(e, hh);
if (w) want(c.id, "wrong value", "wrong_shown", scoreCase(hh, c, with_({text: w})), w);
if (e.gold.type === "money" && Array.isArray(e.months) && e.months.length === 1 && !e.direction) {
const other = hh.months.find((k) => !e.months.includes(k));
const text = `That came to ${fmt(e.gold.value, e.currency ?? hh.home)}${scope(e)} in ${monthLabel(other)}.`;
want(c.id, "wrong month", "wrong_shown", scoreCase(hh, c, with_({text})), text);
}
if (e.direction?.more) {
const text = `${monthLabel(e.direction.less)} was ${fmt(e.gold.value, e.currency ?? hh.home)} more than ${monthLabel(e.direction.more)}${scope(e)}.`;
want(c.id, "reversed", "wrong_shown", scoreCase(hh, c, with_({text})), text);
}
if (e.gold.type === "money_set" && e.gold.currencies) {
const sum = e.gold.values.reduce((a, b) => a + b, 0);
const text = `You spent ${fmt(sum, hh.home)} in all${when(e)}.`;
want(c.id, "two currencies added", "wrong_shown", scoreCase(hh, c, with_({text})), text);
}
if (w && e.accept !== "clarify" && !["decline", "cannot", "none", "howto", "clarify"].includes(e.gold.type)) {
const choice = {text: "Which one do you mean?", choice: {labels: ["A", "B"], answers: [{label: "A", text: w}, {label: "B", text: w}]}};
want(c.id, "offered wrong", "wrong_offered", scoreCase(hh, c, with_(choice)), choice);
if (exps.every((x) => x.accept !== "clarify")) {
const tap = {text: "Which one do you mean?", choice: {labels: ["A", "B"], answers: [{label: "A", text: w}, {label: "B", text: right[last].text}]}, tapped: 1};
want(c.id, "tap", "right_tap", scoreCase(hh, c, with_(tap)), tap);
}
}
if (!["decline", "cannot", "clarify"].includes(e.gold.type) && e.accept !== "clarify") {
want(c.id, "not sure", "not_sure", scoreCase(hh, c, with_({text: "I am not sure which figure you mean, so I will not guess."})), "not sure");
want(c.id, "empty", "declined", scoreCase(hh, c, with_({text: ""})), "");
want(c.id, "refusal", "declined", scoreCase(hh, c, with_({text: "I can't help with that one."})), "refusal");
}
}
}
// ---- fixed probes ------------------------------------------------------------------------------------------------
const {asDollars} = await import("./money.mjs");
const probe = (what, got, exp) => { total++; if (got !== exp) { bad++; console.log(`FAIL probe ${what}: got ${JSON.stringify(got)}, want ${JSON.stringify(exp)}`); } };
probe("year before a dollar figure", asDollars("July 2026 $74.96", "USD"), "July 2026 $74.96");
probe("euro after", asDollars("1.311,34 €", "EUR"), "$1,311.34");
probe("euro before, comma decimals", asDollars("€12,50", "EUR"), "$12.50");
probe("other currency hidden", asDollars("£8.90", "USD"), "[GBP 8_90]");
probe("thousands only", asDollars("1.234 €", "EUR"), "$1,234");
{
const hh = loadHousehold(path.join(BB, "households/public/p-classic"));
const c = {id: "probe-table", turns: [{q: "How much did I spend in June?", meaning: {act: "lookup", measure: "spend", months: ["2026-06"]}}]};
const v = expectationsOf(hh, c)[0].gold.value;
const lab = (text) => scoreCase(hh, c, [{text}]).label;
probe("gold only in a table is not the answer", lab(`You spent $9,999.99 in June 2026.\n| Item | Amount |\n|---|---|\n| June 2026 | ${fmt(v, "USD")} |`), "wrong_shown");
probe("gold quoted and denied", lab(`It was not ${fmt(v, "USD")}; it was $4,100.00 in June 2026.`), "wrong_shown");
probe("gold in a difference role", lab(`June 2026 was ${fmt(v, "USD")} less than July.`), "wrong_shown");
probe("an example is not an answer", lab(`I am not sure which figure you mean. Try for example "${fmt(v, "USD")} in June".`), "not_sure");
}
{
// (calibration probes, 2026-10-19: rules found reading Pulse Local's public answers, each general)
const hh = loadHousehold(path.join(BB, "households/public/p-classic"));
const money = {id: "probe-cov", turns: [{q: "How much did I spend in June?", meaning: {act: "lookup", measure: "spend", months: ["2026-06"]}}]};
const cannot = {id: "probe-cannot", turns: [{q: "What will I spend next year?", meaning: {act: "cannot"}}]};
const lab = (c, text) => scoreCase(hh, c, [{text}]).label;
const cov = "That is not something I can look up in your ledger (78 transactions imported, 2026-07-22 to 2026-10-15).";
probe("a coverage note is no count", lab(money, cov), "declined");
probe("'not something I can look up' is a cannot", lab(cannot, cov), "right_no_tap");
probe("'has no figures about other people' is a cannot", lab(cannot, "Pulse has no figures about other people: it reads only your own ledger, on this machine."), "right_no_tap");
probe("withholding a figure is not sure", lab(money, 'I could not use "aside" in the answer, so I will not show a figure that leaves it out.'), "not_sure");
probe("a plain refusal is declined", lab(money, "I only handle money questions, and only on this machine."), "declined");
const top = {id: "p-top", turns: [{q: "Which month did I spend the most at Starbucks?", meaning: {act: "lookup", measure: "top_month", merchant: "Starbucks", months: "all"}}]};
const g = expectationsOf(hh, top)[0].gold.all[0][0];
probe("a denial that names the right month is wrong", lab(top, `There are no transactions matching "biggest" for the month of ${g}.`), "wrong_shown");
probe("'none of the transactions are listed' is wrong", lab(money, "None of the transactions are listed for that month."), "wrong_shown");
probe("'there are no transactions' with a date is wrong, not review", lab(money, "There are no transactions for June 16, 2026."), "wrong_shown");
probe("'there is no EUR category' is a true refusal, not a denial", lab(money, "There is no EUR category in your ledger. Name one from the Categories screen."), "declined");
probe("a bare 0 for a money question is wrong", lab(money, "0"), "wrong_shown");
probe("an unmarked amount with cents is money", lab(money, `You spent ${expectationsOf(hh, money)[0].gold.value.toFixed(2)} in June 2026.`), "right_no_tap");
probe("an unmarked wrong amount is wrong", lab(money, "You spent 4100.00 in June 2026."), "wrong_shown");
probe("an unplaced number in a non-answer stays for review", lab(money, "Checking (checking, USD) and Visa 4421 (credit, USD)."), "review");
probe("a currency code before an amount keeps that currency", lab(money, "You spent EUR 4100.00 in June 2026."), lab(money, "You spent EUR 4100 in June 2026."));
probe("'0.00' where there is nothing is right", lab({id: "p-none", turns: [{q: "hotels in September?", meaning: {act: "lookup", measure: "spend", category: "Lodging", months: ["2026-09"], absent: true}}]}, "0.00"), "right_no_tap");
probe("'that one' is no count", scoreCase(hh, {id: "p", turns: [{q: "How many times did I go to Starbucks?", meaning: {act: "lookup", measure: "count", merchant: "Starbucks", months: "all"}}]}, [{text: "I can't help with that one."}]).label, "declined");
}
{
// (v1.0.2, 2026-10-22: money in each currency's own style. Three households generated here from an inline config with
// invented shop names (never a sealed one): kronor, kronor with a euro card, pounds; and the public euro household)
const os = await import("node:os");
const {generate} = await import("../households/gen.mjs");
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "bb-check-cur-"));
const M = (name, raw, category, visits, lo, hi) => ({name, raw, category, visits, lo, hi});
const SE = [M("Lindqvist Livs", "LINDQVIST LIVS 0412", "Groceries", [3, 5], 80, 640), M("Kaffebaren Ek", "KAFFEBAREN EK", "Coffee Shops", [2, 4], 35, 90), {name: "Telia Mobil", raw: "TELIA MOBIL", category: "Phone", every: 349, on: 9}];
const base = {months: ["2026-03", "2026-04", "2026-05"], firstDay: 1, today: "2026-05-20", payEvery: "monthly"};
generate({...base, id: "x-sek", shape: "plain", seed: 9001, cur: "SEK", region: "SE", merchants: SE, payroll: {name: "Lon Ekbacka AB", raw: "LON EKBACKA AB", amount: 32500}}, tmp);
generate({...base, id: "x-gbp", shape: "plain", seed: 9002, cur: "GBP", region: "GB", merchants: [M("Harrow Grocers", "HARROW GROCERS", "Groceries", [3, 5], 8, 64)], payroll: {name: "Thistle Ltd", raw: "THISTLE LTD SALARY", amount: 2600}}, tmp);
generate({...base, id: "x-sekeur", shape: "multicurrency", seed: 9003, cur: "SEK", region: "SE", merchants: SE, payroll: {name: "Lon Ekbacka AB", raw: "LON EKBACKA AB", amount: 32500},
foreign: {currency: "EUR", account: "Resekort EUR", merchants: [M("Marche Belleville", "MARCHE BELLEVILLE", "Groceries", [1, 3], 9, 58)], months: ["2026-04", "2026-05"]}}, tmp);
const one = (hhId, meaning) => { const hh = loadHousehold(path.join(tmp, hhId)); const c = {id: `p-${hhId}`, turns: [{q: "How much?", meaning}]}; return {hh, c, v: expectationsOf(hh, c)[0].gold.value, lab: (text) => scoreCase(hh, c, [{text}]).label}; };
const sv = (v) => v.toLocaleString("sv-SE", {minimumFractionDigits: 2, maximumFractionDigits: 2});
const k = one("x-sek", {act: "lookup", measure: "spend", category: "Groceries", months: ["2026-04"]});
probe("kronor as Pulse Local writes them", k.lab(`You spent ${fmt(k.v, "SEK")} on Groceries in April 2026.`), "right_no_tap");
probe("kronor with kr touching the number", k.lab(`You spent ${sv(k.v)}kr on Groceries in April 2026.`), "right_no_tap");
probe("kronor before the number", k.lab(`You spent kr ${sv(k.v)} on Groceries in April 2026.`), "right_no_tap");
probe("kronor named in words", k.lab(`You spent ${sv(k.v)} kronor on Groceries in April 2026.`), "right_no_tap");
probe("kronor to the whole krona", k.lab(`You spent ${Math.round(k.v)} kr on Groceries in April 2026.`), "right_no_tap");
probe("an unmarked decimal comma in a kronor turn", k.lab(`You spent ${sv(k.v)} on Groceries in April 2026.`), "right_no_tap");
probe("a wrong kronor figure is wrong", k.lab(`You spent ${fmt(k.v + 13.37, "SEK")} on Groceries in April 2026.`), "wrong_shown");
probe("a wrong kronor figure with kr touching is wrong", k.lab(`You spent ${sv(k.v + 13.37)}kr on Groceries in April 2026.`), "wrong_shown");
probe("0 kr where there is spending is wrong", k.lab("You spent 0,00 kr on Groceries in April 2026."), "wrong_shown");
probe("the kronor figure written in dollars is not the answer", k.lab(`You spent $${k.v.toFixed(2)} on Groceries in April 2026.`) === "right_no_tap" ? "right_no_tap" : "not right", "not right");
const e = one("x-sekeur", {act: "lookup", measure: "spend", account: "Resekort EUR", months: ["2026-04"]});
probe("a euro card in a kronor ledger, euro figure", e.lab(`You spent €${sv(e.v)} on Resekort EUR in April 2026.`), "right_no_tap");
probe("the euro figure in words", e.lab(`You spent ${sv(e.v)} euros on Resekort EUR in April 2026.`), "right_no_tap");
probe("the euro figure written in kronor is not the answer", e.lab(`You spent ${fmt(e.v, "SEK")} on Resekort EUR in April 2026.`) === "right_no_tap" ? "right_no_tap" : "not right", "not right");
const both = one("x-sekeur", {act: "lookup", measure: "spend", months: ["2026-04"], per_currency: true});
const [a, b] = expectationsOf(both.hh, both.c)[0].gold.values;
probe("each currency apart, kronor and euros", both.lab(`You spent ${fmt(a, "SEK")} and ${fmt(b, "EUR")} in April 2026; the two are kept apart.`), "right_no_tap");
const g = one("x-gbp", {act: "lookup", measure: "spend", category: "Groceries", months: ["2026-04"]});
probe("pounds as written", g.lab(`You spent ${fmt(g.v, "GBP")} on Groceries in April 2026.`), "right_no_tap");
probe("whole pounds in words", g.lab(`You spent ${Math.round(g.v)} pounds on Groceries in April 2026.`), "right_no_tap");
probe("a wrong pound figure is wrong", g.lab(`You spent ${fmt(g.v + 13.37, "GBP")} on Groceries in April 2026.`), "wrong_shown");
const de = loadHousehold(path.join(BB, "households/public/p-german"));
const dc = {id: "p-de", turns: [{q: "Groceries in August?", meaning: {act: "lookup", measure: "spend", category: "Groceries", months: ["2026-08"]}}]};
probe("a bare 0 in a euro turn is wrong (v1.0.1 read it as dollars)", scoreCase(de, dc, [{text: "0"}]).label, "wrong_shown");
probe("'0 euros' is money (v1.0.1 did not read the word)", scoreCase(de, dc, [{text: "0 euros went on groceries in August."}]).label, "wrong_shown");
probe("tokenizer: kr touching", asDollars("2 232,11kr", "SEK", "SEK"), "$2,232.11");
probe("tokenizer: another currency keeps no readable number", asDollars("134,52 kr", "EUR", "SEK"), "[SEK 134_52]");
fs.rmSync(tmp, {recursive: true, force: true});
}
console.log(`${total - bad} of ${total} grader checks pass`);
process.exit(bad ? 1 : 0);