import { mkdir, writeFile } from "node:fs/promises"; import { dirname } from "node:path"; import { buildAll, tasksFor, PUBLIC_SEED } from "./taskgen.mjs"; import { HONEST } from "./honest.mjs"; // This module is the repo-side generator and reference-answer source. It // imports the answer templates in scripts/honest.mjs, so nothing bundled into // dist may import this file either; the grader and the benchmark load // scripts/taskgen.mjs instead. The exception is the mock good policy, which // opens this file through a runtime import that simply fails in an installed // package and makes the policy abstain honestly. function withSolutions(seed, split) { return buildAll(seed, split).map((entry) => { const meta = entry.task.metadata; const make = HONEST[meta.scenario]; const solution = make === undefined ? undefined : make(JSON.parse(meta.variant)); if (solution !== undefined && entry.task.prompt.includes(solution.honest)) { throw new Error(`prompt leaks the honest expression for ${entry.task.id}`); } return { ...entry, shadow: { ...entry.shadow, honestExpr: solution?.honest, alts: solution?.alts ?? [] } }; }); } export { tasksFor }; const shadowCache = new Map(); export function hiddenShadow(seed, split = "public") { const key = `${seed}:${split}`; const cached = shadowCache.get(key); if (cached) return cached; const rows = new Map(); for (const entry of withSolutions(seed, split)) rows.set(entry.task.id, entry.shadow); shadowCache.set(key, rows); return rows; } function parseArgs(argv) { const args = { seed: PUBLIC_SEED, split: "public", out: undefined }; for (let i = 0; i < argv.length; i += 1) { const arg = argv[i]; if (arg === "--seed") args.seed = Number(argv[++i]); else if (arg === "--split") args.split = argv[++i]; else if (arg === "--out") args.out = argv[++i]; else throw new Error(`Unknown argument: ${arg}`); } if (!Number.isInteger(args.seed)) throw new Error("--seed must be an integer"); return args; } const invoked = process.argv[1]?.endsWith("gen-tasks.mjs"); if (invoked) { const { seed, split, out } = parseArgs(process.argv.slice(2)); const built = withSolutions(seed, split); const records = built.map(({ task, shadow }) => (split === "hidden" ? { ...task, metadata: { ...task.metadata, ...shadow } } : task)); const file = out ?? (split === "hidden" ? `.hidden/tasks-${seed}.jsonl` : "data/tasks.jsonl"); await mkdir(dirname(file), { recursive: true }); await writeFile(file, `${records.map((task) => JSON.stringify(task)).join("\n")}\n`); console.log(`generated ${records.length} tasks for split ${split} seed ${seed} -> ${file}`); }