import { mkdir, writeFile } from "node:fs/promises";
import { dirname } from "node:path";
import { GATE_PARTS, PART_HINTS, PART_PHRASES } from "./parts.mjs";
const argv = process.argv.slice(2);
function option(name, fallback) {
const index = argv.indexOf(name);
return index === -1 ? fallback : argv[index + 1];
}
const PUBLIC_SEED = 20261015;
const split = option("--split", "public");
if (!new Set(["public", "hidden"]).has(split)) throw new Error("--split must be public or hidden");
const seedText = option("--seed", split === "public" ? String(PUBLIC_SEED) : process.env.BPEL_HIDDEN_SEED);
if (seedText === undefined) throw new Error("hidden split needs a private seed: pass --seed or set BPEL_HIDDEN_SEED");
if (!/^-?\d+$/.test(seedText.trim())) throw new Error("--seed must be an integer");
const seed = Number(seedText);
if (!Number.isSafeInteger(seed)) throw new Error("--seed must be an integer");
const output = option("--output", split === "public" ? "data/tasks.jsonl" : "hidden/tasks.jsonl");
function mulberry32(initial) {
let state = initial >>> 0;
return () => {
state += 0x6d2b79f5;
let value = state;
value = Math.imul(value ^ (value >>> 15), value | 1);
value ^= value + Math.imul(value ^ (value >>> 7), value | 61);
return ((value ^ (value >>> 14)) >>> 0) / 4_294_967_296;
};
}
const pick = (random, items) => items[Math.floor(random() * items.length)];
// Feature tables. `beak`, `neck`, `legs`, `body`, `extras`, `bars`, `tires`, and the
// wheel and wheelbase sizes become requiredParts entries and choose the reference
// geometry, so grading stays honest about what it checks. Every animal in a split
// needs a unique signature, and so does every vehicle, or a memorised drawing of
// one would satisfy the other.
const ANIMALS = {
public: {
pelican: { beak: "pouch beak", neck: "short neck", legs: "legs", body: "level body", extras: [] },
flamingo: { beak: "thin curved beak", neck: "long neck", legs: "long legs", body: "level body", extras: [] },
toucan: { beak: "large beak", neck: "short neck", legs: "legs", body: "level body", extras: [] },
stork: { beak: "long pointed beak", neck: "long neck", legs: "long legs", body: "level body", extras: [] },
heron: { beak: "pointed beak", neck: "long neck", legs: "long legs", body: "level body", extras: [] },
duck: { beak: "flat beak", neck: "short neck", legs: "legs", body: "level body", extras: [] },
goose: { beak: "pointed beak", neck: "long neck", legs: "legs", body: "level body", extras: [] },
parrot: { beak: "hooked beak", neck: "short neck", legs: "legs", body: "level body", extras: ["tail feathers"] },
penguin: { beak: "short beak", neck: "short neck", legs: "legs", body: "upright body", extras: [] },
swan: { beak: "thin curved beak", neck: "long neck", legs: "legs", body: "level body", extras: [] },
},
hidden: {
owl: { beak: "hooked beak", neck: "short neck", legs: "legs", body: "upright body", extras: [] },
crane: { beak: "long pointed beak", neck: "long neck", legs: "long legs", body: "level body", extras: [] },
ibis: { beak: "thin curved beak", neck: "long neck", legs: "long legs", body: "level body", extras: [] },
puffin: { beak: "tall colourful beak", neck: "short neck", legs: "legs", body: "upright body", extras: [] },
raven: { beak: "pointed beak", neck: "short neck", legs: "legs", body: "level body", extras: ["tail feathers"] },
seagull: { beak: "pointed beak", neck: "short neck", legs: "legs", body: "level body", extras: [] },
kiwi: { beak: "thin curved beak", neck: "short neck", legs: "long legs", body: "upright body", extras: [] },
ostrich: { beak: "short beak", neck: "long neck", legs: "long legs", body: "upright body", extras: [] },
vulture: { beak: "hooked beak", neck: "long neck", legs: "legs", body: "level body", extras: [] },
magpie: { beak: "large beak", neck: "short neck", legs: "legs", body: "level body", extras: ["tail feathers"] },
},
};
const VEHICLES = {
public: {
"bicycle": { bars: "handlebars", tires: "smooth tires", extras: [], wheelbase: 340, wheelR: 66, tire: 12 },
"tandem bicycle": { bars: "handlebars", tires: "smooth tires", extras: ["second saddle"], wheelbase: 430, wheelR: 74, tire: 10 },
"cargo bicycle": { bars: "handlebars", tires: "smooth tires", extras: ["cargo box"], wheelbase: 350, wheelR: 62, tire: 12 },
"mountain bike": { bars: "handlebars", tires: "knobby tires", extras: [], wheelbase: 340, wheelR: 70, tire: 15 },
"road bicycle": { bars: "drop handlebars", tires: "smooth tires", extras: [], wheelbase: 345, wheelR: 68, tire: 8 },
"touring bicycle": { bars: "handlebars", tires: "smooth tires", extras: ["rear rack"], wheelbase: 350, wheelR: 66, tire: 10 },
"cruiser bicycle": { bars: "swept handlebars", tires: "smooth tires", extras: [], wheelbase: 330, wheelR: 68, tire: 13 },
"folding bicycle": { bars: "handlebars", tires: "smooth tires", extras: [], wheelbase: 290, wheelR: 30, tire: 6 },
},
hidden: {
"electric bicycle": { bars: "handlebars", tires: "smooth tires", extras: ["battery pack"], wheelbase: 345, wheelR: 66, tire: 11 },
"BMX bike": { bars: "handlebars", tires: "smooth tires", extras: [], wheelbase: 285, wheelR: 30, tire: 6 },
"gravel bike": { bars: "drop handlebars", tires: "knobby tires", extras: [], wheelbase: 340, wheelR: 66, tire: 12 },
"city bike": { bars: "swept handlebars", tires: "smooth tires", extras: ["rear rack"], wheelbase: 335, wheelR: 66, tire: 11 },
"track bicycle": { bars: "drop handlebars", tires: "smooth tires", extras: [], wheelbase: 350, wheelR: 68, tire: 7 },
"commuter bicycle": { bars: "handlebars", tires: "smooth tires", extras: ["rear rack"], wheelbase: 340, wheelR: 65, tire: 10 },
"hybrid bicycle": { bars: "handlebars", tires: "knobby tires", extras: [], wheelbase: 338, wheelR: 67, tire: 12 },
"fat bike": { bars: "swept handlebars", tires: "fat tires", extras: [], wheelbase: 345, wheelR: 72, tire: 26 },
},
};
const article = (word) => (/^[aeiou]/i.test(word) ? "an" : "a");
// Turns the feature tables into the part list the grader checks and the sentence
// that tells the model what to draw. The self-check below proves every checked
// part is visible in the prompt.
function partsFor(animal, aspect, vehicle, spec) {
const wheelSize = spec.wheelbase / spec.wheelR >= 7 ? "small wheels" : "full-size wheels";
const tires = spec.tires === "fat tires" ? ["knobby tires", "fat tires"] : [spec.tires];
const specific = [
aspect.beak, aspect.neck, aspect.body,
...(aspect.legs === "long legs" ? ["long legs"] : []),
...aspect.extras,
...(spec.bars === "handlebars" ? [] : [spec.bars]),
wheelSize, ...tires, ...spec.extras,
];
const requiredParts = [...new Set([...GATE_PARTS, ...specific])];
const hint = (part) => (PART_HINTS[part] ? ` (${PART_HINTS[part]})` : "");
const birdLegs = aspect.legs === "long legs" ? `long legs reaching the pedals${hint("long legs")}` : "legs reaching the pedals";
const animalText = `${article(aspect.beak)} ${aspect.beak}${hint(aspect.beak)}, ${article(aspect.neck)} ${aspect.neck}${hint(aspect.neck)}, ${article(aspect.body)} ${aspect.body}${hint(aspect.body)}, a head, and ${birdLegs}${aspect.extras.length ? `, plus ${aspect.extras.map((extra) => `${extra} ${hint(extra)}`).join(" and ")}` : ""}`;
const extras = spec.extras.map((extra) => `${article(extra)} ${extra}${hint(extra)}`).join(" and ");
const vehicleText = `two wheels, a bicycle frame, ${spec.bars}${hint(spec.bars)}, pedals${hint("pedals")}, ${wheelSize}${hint(wheelSize)}, ${tires.map((tire) => `${tire}${hint(tire)}`).join(" and ")}${extras ? `, plus ${extras}` : ""}`;
const clause = ` The ${animal} has ${animalText}. The ${vehicle} has ${vehicleText}. Draw the beak as a filled shape, not a bare outline.`;
return { requiredParts, clause };
}
const PUBLIC_TEMPLATES = [
(animal, vehicle, clause) => `Draw ${article(animal)} ${animal} riding ${article(vehicle)} ${vehicle}.${clause} Reply with exactly one SVG image and nothing else: begin with .`,
(animal, vehicle, clause) => `Create a self-contained SVG showing ${article(animal)} ${animal} riding ${article(vehicle)} ${vehicle}.${clause} Reply with exactly one SVG image and nothing else: begin with .`,
(animal, vehicle, clause) => `Generate an SVG illustration: ${article(animal)} ${animal} is riding ${article(vehicle)} ${vehicle}.${clause} Reply with exactly one SVG image and nothing else: begin with .`,
];
const HIDDEN_TEMPLATES = [
(animal, vehicle, clause) => `Sketch ${article(animal)} ${animal} on ${article(vehicle)} ${vehicle} as SVG markup.${clause} Reply with exactly one SVG image and nothing else: begin with .`,
(animal, vehicle, clause) => `Produce one standalone SVG of ${article(animal)} ${animal} riding ${article(vehicle)} ${vehicle}.${clause} Reply with exactly one SVG image and nothing else: begin with .`,
(animal, vehicle, clause) => `SVG answer only: show ${article(animal)} ${animal} cycling on ${article(vehicle)} ${vehicle}.${clause} Begin with .`,
];
function shuffle(items, random) {
const copy = [...items];
for (let index = copy.length - 1; index > 0; index--) {
const other = Math.floor(random() * (index + 1));
[copy[index], copy[other]] = [copy[other], copy[index]];
}
return copy;
}
const round = (value) => Math.round(value * 10) / 10;
const jitter = (random, amount) => round((random() - 0.5) * 2 * amount);
// Draws a bicycle whose wheels sit low on the canvas and whose frame, bars and
// pedals satisfy the structural checks the grader documents.
function bicycleSvg(spec, random) {
const radius = spec.wheelR + jitter(random, 5);
const wheelbase = spec.wheelbase + jitter(random, 16);
const cy = 318 + jitter(random, 4);
const rearX = round(320 - wheelbase / 2), frontX = round(320 + wheelbase / 2);
const tire = spec.tire;
const bbX = round(rearX + wheelbase * 0.42), bbY = round(cy - 22);
const seatX = round(rearX + wheelbase * 0.36), seatY = round(cy - 78);
const headX = round(frontX - wheelbase * 0.12), headY = round(cy - 96);
const dark = "#22252b";
const parts = [
``,
``,
``,
``,
``,
``,
``,
];
if (spec.tires === "knobby tires") {
parts.push(``);
parts.push(``);
}
const barX = headX + jitter(random, 4), barY = headY - 24;
if (spec.bars === "drop handlebars") parts.push(``);
else if (spec.bars === "swept handlebars") parts.push(``);
else parts.push(``);
if (spec.extras.includes("second saddle")) parts.push(``);
if (spec.extras.includes("cargo box")) parts.push(``);
if (spec.extras.includes("rear rack")) parts.push(``);
if (spec.extras.includes("battery pack")) parts.push(``);
return { parts, wheels: { rearX, frontX, cy, radius }, bbX, bbY, seatY };
}
// Beak geometry lives ahead of the head centre (head always sits front-top of
// the body). Every length and depth is a multiple of the head width D, matching
// the numbers the prompts state and the grader measures.
function beakSvg(style, head, colour) {
const { hx, hy, hr } = head;
const tipX = hx + hr;
const d = (k) => round(k * hr * 2);
switch (style) {
case "pouch beak": return ``;
case "large beak": return ``;
case "long pointed beak": return ``;
case "pointed beak": return ``;
case "flat beak": return ``;
case "thin curved beak": return ``;
case "hooked beak": return ``;
case "tall colourful beak": return ``;
case "short beak": return ``;
default: throw new Error(`unknown beak style ${style}`);
}
}
function birdSvg(spec, wheels, random, palette) {
const dx = jitter(random, 14), dy = jitter(random, 10);
const upright = spec.body === "upright body";
const bx = round(310 + dx), bodyRy = upright ? 88 + jitter(random, 6) : 58 + jitter(random, 6);
const bodyRx = upright ? 62 + jitter(random, 5) : 90 + jitter(random, 7);
const bodyCy = round((upright ? 176 : 152) + dy);
const longNeck = spec.neck === "long neck";
const hx = round(bx + bodyRx + (longNeck ? 66 : 30)), hy = round(bodyCy - bodyRy - (longNeck ? 52 : 26)), hr = upright ? 34 : 38;
const head = { hx, hy, hr };
const parts = [``];
if (!upright) parts.push(``);
else parts.push(``);
if (spec.extras.includes("tail feathers")) parts.push(``);
if (longNeck) parts.push(``);
parts.push(``);
parts.push(``);
parts.push(beakSvg(spec.beak, head, palette.beak));
// Long-legged birds let their thin legs dangle at least half a wheel radius
// below the wheel axles, the way a wading bird's legs hang off a bicycle;
// other birds reach the pedals.
const legY = bodyCy + bodyRy - 6;
const legEnd = spec.legs === "long legs" ? round(wheels.wheels.cy + wheels.wheels.radius * 0.7) : wheels.bbY - 2;
const legStroke = spec.legs === "long legs" ? 6 : 13;
const legSpread = upright ? 30 : 44;
parts.push(``);
parts.push(``);
return parts;
}
function referenceSvg(animal, vehicle, index, random) {
const aspect = ANIMALS.public[animal] ?? ANIMALS.hidden[animal];
const vspec = VEHICLES.public[vehicle] ?? VEHICLES.hidden[vehicle];
const bodies = ["#f4f1de", "#cce3de", "#e8d7f1", "#d8e2dc", "#ffe5d9", "#e9f5db"];
const wings = ["#81b29a", "#8ecae6", "#b8c0ff", "#90be6d", "#ffb4a2", "#b7adcf"];
const frames = ["#3d405b", "#264653", "#6d597a", "#1d3557", "#5f6c7b", "#7f5539"];
const palette = {
body: pick(random, bodies), wing: pick(random, wings), belly: "#fdf8ec",
beak: pick(random, ["#f4a261", "#f6bd60", "#ee9b00", "#f4845f"]), legs: "#e9c46a",
};
const bike = bicycleSvg(vspec, random);
const bird = birdSvg(aspect, bike, random, palette);
return ``;
}
const random = mulberry32(seed);
const animalTable = split === "public" ? ANIMALS.public : ANIMALS.hidden;
const vehicleTable = split === "public" ? VEHICLES.public : VEHICLES.hidden;
const templates = split === "public" ? PUBLIC_TEMPLATES : HIDDEN_TEMPLATES;
const combinations = shuffle(
Object.keys(animalTable).flatMap((animal) => Object.keys(vehicleTable).map((vehicle) => ({ animal, vehicle }))),
random,
);
const selected = split === "public"
? [{ animal: "pelican", vehicle: "bicycle" }, ...combinations.filter(({ animal, vehicle }) => animal !== "pelican" || vehicle !== "bicycle").slice(0, 29)]
: combinations.slice(0, 30);
const tasks = selected.map(({ animal, vehicle }, index) => {
const canonicalTask = split === "public" && index === 0;
const aspect = animalTable[animal];
const spec = vehicleTable[vehicle];
const { requiredParts, clause } = partsFor(animal, aspect, vehicle, spec);
const prompt = canonicalTask
? `Generate an SVG of a pelican riding a bicycle.${clause} Reply with exactly one SVG image and nothing else: begin with .`
: templates[Math.floor(random() * templates.length)](animal, vehicle, clause);
// Self-check: a model can only draw what the prompt names, so every part the
// grader checks must be readable in the prompt text.
const lower = prompt.toLowerCase();
for (const part of requiredParts) {
const phrase = PART_PHRASES[part] ?? part;
if (!lower.includes(phrase)) throw new Error(`task ${animal}/${vehicle}: prompt does not mention "${phrase}" (part "${part}")`);
}
const specificCount = requiredParts.filter((part) => !GATE_PARTS.includes(part)).length;
if (specificCount < 3) throw new Error(`task ${animal}/${vehicle}: only ${specificCount} task-specific parts`);
return {
id: canonicalTask ? "public-pelican-bicycle" : `${split}-${String(index + 1).padStart(2, "0")}-${animal.replaceAll(" ", "-")}-${vehicle.replaceAll(" ", "-")}`,
kind: "single",
prompt,
expected: referenceSvg(animal, vehicle, index, random),
metadata: { split, seed, source: canonicalTask ? "human-written-canonical" : "seeded-grid", animal, vehicle, requiredParts },
};
});
await mkdir(dirname(output), { recursive: true });
await writeFile(output, `${tasks.map((task) => JSON.stringify(task)).join("\n")}\n`);
console.log(`generated ${tasks.length} ${split} tasks${split === "public" ? ` with seed ${seed}` : ""} at ${output}`);