File size: 4,747 Bytes
46f490b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66f16a0
46f490b
66f16a0
 
46f490b
 
 
 
 
 
 
 
 
 
 
66f16a0
 
 
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
58
59
60
61
62
63
#!/usr/bin/env node
// Build the Hugging Face dataset folder of BudgetBench from the COMMITTED tree only (git archive of HEAD), so nothing
// untracked or git-ignored can leak: the sealed split (sealed/), a sealed run folder (results/*/sealed/) and partial
// runs never reach it. README.md's front matter is the dataset card. Then it adds one derived file for the Hub's
// viewer (viewer/public.jsonl: one row per public case, the turns as a JSON string, because the frozen cases mix
// types inside "meaning" and Arrow cannot read them as columns), and refuses to finish when any sealed case id or
// sealed question appears in the folder, or when check.mjs fails inside it.
//   node budgetbench/hf/export.mjs <empty output folder>
// Upload is the owner's step (README "Publishing").
import {execSync} from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import {fileURLToPath} from "node:url";

const BB = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const OUT = path.resolve(process.argv[2] ?? "");
if (!process.argv[2]) { console.error("usage: export.mjs <empty output folder>"); process.exit(2); }
if (fs.existsSync(OUT) && fs.readdirSync(OUT).length) { console.error(`${OUT} is not empty`); process.exit(2); }
fs.mkdirSync(OUT, {recursive: true});

const top = execSync("git rev-parse --show-toplevel", {cwd: BB, encoding: "utf8"}).trim();
const rel = path.relative(top, BB);
const head = execSync("git rev-parse HEAD", {cwd: BB, encoding: "utf8"}).trim();
if (execSync(`git status --porcelain -- ${JSON.stringify(rel)}`, {cwd: top, encoding: "utf8"}).split("\n").some((l) => l && !l.startsWith("??")))
    console.error("note: budgetbench has uncommitted changes; the export holds the COMMITTED tree only");
execSync(`git archive --format=tar ${head}:${rel} | tar -x -C ${JSON.stringify(OUT)}`, {cwd: top, shell: "/bin/bash"});

// the viewer file
const rows = [];
for (const f of fs.readdirSync(path.join(OUT, "cases/public")).filter((x) => x.endsWith(".jsonl")).sort())
    for (const l of fs.readFileSync(path.join(OUT, "cases/public", f), "utf8").split("\n").filter(Boolean)) {
        const c = JSON.parse(l);
        rows.push({id: c.id, household: c.household, category: c.category, source: c.source ?? (c.id.startsWith("pub-classic") ? "eval-v0" : "writer"),
            n_turns: c.turns.length, questions: c.turns.map((t) => t.q), turns_json: JSON.stringify(c.turns)});
    }
fs.mkdirSync(path.join(OUT, "viewer"), {recursive: true});
fs.writeFileSync(path.join(OUT, "viewer/public.jsonl"), rows.map((r) => JSON.stringify(r)).join("\n") + "\n");

// no sealed content: no sealed folder, no sealed run folder, no sealed case id or question anywhere
const walk = (d) => fs.readdirSync(d, {withFileTypes: true}).flatMap((e) => e.isDirectory() ? walk(path.join(d, e.name)) : [path.join(d, e.name)]);
const files = walk(OUT);
const bad = files.filter((f) => /(^|\/)sealed(-v\d+)?\//.test(path.relative(OUT, f)));
if (bad.length) { console.error(`REFUSED: sealed paths in the export: ${bad.slice(0, 5).join(", ")}`); process.exit(1); }
// (every sealed split on this machine: v1 "sealed", v2 "sealed-v2", and any later one)
for (const sealedDir of fs.readdirSync(BB).filter((d) => /^sealed(-v\d+)?$/.test(d)).map((d) => path.join(BB, d, "cases"))) {
if (fs.existsSync(sealedDir)) {
    // (a generic question asked in both splits, "How much did I spend in June?", is no leak: only wordings no public question holds count)
    const publicQs = rows.flatMap((r) => r.questions).join("\n").toLowerCase();
    const ids = [], qs = [];
    for (const f of fs.readdirSync(sealedDir).filter((x) => x.endsWith(".jsonl")))
        for (const l of fs.readFileSync(path.join(sealedDir, f), "utf8").split("\n").filter(Boolean)) { const c = JSON.parse(l); ids.push(c.id); for (const t of c.turns) if (t.q.length >= 24 && !publicQs.includes(t.q.toLowerCase())) qs.push(t.q); }
    for (const f of files) {
        const text = fs.readFileSync(f, "utf8");
        const id = ids.find((x) => new RegExp(`\\b${x}\\b`).test(text)), q = qs.find((x) => text.includes(x));
        if (id || q) { console.error(`REFUSED: ${path.relative(OUT, f)} holds sealed ${id ? `case id ${id}` : `question "${q}"`}`); process.exit(1); }
    }
    console.log(`${path.relative(BB, sealedDir)}: no sealed case id (${ids.length}) or question (${qs.length}) in ${files.length} files`);
} else console.error(`note: ${path.relative(BB, sealedDir)} is not on this machine; its id and question scan was skipped`);
}

execSync(`node ${JSON.stringify(path.join(OUT, "check.mjs"))}`, {cwd: OUT, stdio: "inherit"});
console.log(`\nexported ${head.slice(0, 9)} to ${OUT} (${files.length + 1} files, ${rows.length} viewer rows)`);