File size: 3,700 Bytes
46f490b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env node
// BudgetBench writer's checker: every case's shape, every turn's meaning through the oracle, and a review sheet
// (each turn with the answer the oracle expects). Exit 1 on any error.
//   node tools/validate.mjs cases.jsonl [household dir]      (in a writer's packet)
//   node budgetbench/writers/validate.mjs <cases.jsonl> <household dir>
import fs from "node:fs";
import path from "node:path";
import {fileURLToPath, pathToFileURL} from "node:url";

const HERE = path.dirname(fileURLToPath(import.meta.url));
const oraclePath = fs.existsSync(path.join(HERE, "oracle.mjs")) ? path.join(HERE, "oracle.mjs") : path.join(HERE, "../oracle/oracle.mjs");
const {loadHousehold, goldOf, describe, monthLabel} = await import(pathToFileURL(oraclePath).href);

export const CATEGORIES = ["totals", "merchants", "by_month", "comparisons", "exclusions", "chains", "howto", "refusals", "currency"];

const [file, hhDir0] = process.argv.slice(2);
if (!file) { console.error("usage: validate.mjs <cases.jsonl> [household dir]"); process.exit(2); }
const hhDir = hhDir0 ?? path.join(path.dirname(path.resolve(file)), "household");
const hh = loadHousehold(hhDir);
const errors = [];
const counts = {};
const ids = new Set();
const fmt = (g) => {
    const cur = g.currency ? ` ${g.currency}` : "";
    if (g.type === "money") return `${g.value}${cur}${g.alt?.length ? ` (also ${g.alt.join(", ")})` : ""}`;
    if (g.type === "money_set") return g.values.map((v, i) => `${g.months ? monthLabel(g.months[i]) + " " : ""}${v}${g.currencies ? " " + g.currencies[i] : cur}`).join("; ");
    if (g.type === "count" || g.type === "percent") return `${g.value}${g.type === "percent" ? "%" : ""}${g.alt?.length ? ` (also ${g.alt.join(", ")})` : ""}`;
    if (g.type === "names") return `names ${g.all.map((a) => a[0]).join(" + ")}`;
    if (g.type === "howto") return `how-to: ${g.concepts.map((c) => c.join("/")).join(" + ")}`;
    return g.type;
};
const lines = fs.readFileSync(file, "utf8").split("\n").filter((l) => l.trim());
const sheet = [];
for (const [i, l] of lines.entries()) {
    let c;
    try { c = JSON.parse(l); } catch (e) { errors.push(`line ${i + 1}: not JSON (${e.message})`); continue; }
    const where = c.id ?? `line ${i + 1}`;
    if (!c.id || ids.has(c.id)) errors.push(`${where}: missing or repeated id`);
    ids.add(c.id);
    if (!CATEGORIES.includes(c.category)) errors.push(`${where}: category must be one of ${CATEGORIES.join(", ")}`);
    counts[c.category] = (counts[c.category] ?? 0) + 1;
    if (!Array.isArray(c.turns) || c.turns.length < 1 || c.turns.length > 4) { errors.push(`${where}: 1 to 4 turns`); continue; }
    if (c.category === "chains" && c.turns.length < 2) errors.push(`${where}: a chain has 2 to 4 turns`);
    if (c.category !== "chains" && c.turns.length > 1) errors.push(`${where}: only chains have more than one turn`);
    for (const [k, t] of c.turns.entries()) {
        if (typeof t.q !== "string" || !t.q.trim()) { errors.push(`${where} turn ${k + 1}: empty q`); continue; }
        try {
            const g = goldOf(hh, t.meaning ?? {});
            sheet.push(`${where} t${k + 1}: ${t.q}\n      means: ${describe(t.meaning)}\n      expected: ${fmt(g.gold)}${g.accept === "clarify" ? " (or a clarifying question)" : ""}`);
        } catch (e) { errors.push(`${where} turn ${k + 1} ("${t.q}"): ${e.message}`); }
    }
}
if (process.env.QUIET !== "1") console.log(sheet.join("\n"));
console.log(`\n${lines.length} cases: ${CATEGORIES.map((k) => `${k} ${counts[k] ?? 0}`).join(", ")}`);
if (errors.length) { console.log(`\n${errors.length} ERRORS:\n` + errors.map((e) => "  " + e).join("\n")); process.exit(1); }
console.log("no errors");