// Fetch (read-only, no build) the transitive closure of OAI.* imports of a root module in openai/math // at a pinned commit, and flag constructs that could weaken or bypass a proof. // usage: node closure.js const fs = require("fs"), path = require("path"), https = require("https"); const [cache, C, root] = process.argv.slice(2); const get = (url) => new Promise((res, rej) => { https.get(url, (r) => { if (r.statusCode !== 200) { r.resume(); return res(null); } let d = ""; r.setEncoding("utf8"); r.on("data", (c) => (d += c)); r.on("end", () => res(d)); }).on("error", rej); }); async function fetchMod(mod) { const rel = mod.split(".").join("/") + ".lean", local = path.join(cache, rel); if (fs.existsSync(local)) return fs.readFileSync(local, "utf8"); for (let t = 0; t < 4; t++) { try { const s = await get(`https://raw.githubusercontent.com/openai/math/${C}/lean/${rel}`); if (s === null) return null; fs.mkdirSync(path.dirname(local), { recursive: true }); fs.writeFileSync(local, s); return s; } catch (e) { await new Promise((r) => setTimeout(r, 1000 * (t + 1))); } } throw new Error("failed " + mod); } const FLAG = /\bsorry\b|^\s*admit\b|\badmit\s*$|^\s*(private\s+)?axiom\b|native_decide|implemented_by|@\[extern|\bunsafe\b|ofReduceBool|trustCompiler|#eval|run_cmd|^\s*(syntax|macro|elab|elab_rules|macro_rules)\b|^\s*initialize\b|\bdecide\s*:=\s*true/; (async () => { const seen = new Set([root]), queue = [root], missing = [], flagged = [], external = new Set(); let lines = 0, idx = 0, active = 0; const worker = async () => { for (;;) { if (idx >= queue.length) { if (active === 0) return; await new Promise((r) => setTimeout(r, 50)); continue; } const mod = queue[idx++]; active++; let src; try { src = await fetchMod(mod); } finally { active--; } if (src === null) { missing.push(mod); continue; } const ls = src.split("\n"); lines += ls.length; ls.forEach((l, i) => { if (FLAG.test(l)) flagged.push(`${mod}:${i + 1}: ${l.trim().slice(0, 160)}`); const m = l.match(/^(?:public\s+)?import\s+(\S+)/); if (m) { if (m[1].startsWith("OAI.")) { if (!seen.has(m[1])) { seen.add(m[1]); queue.push(m[1]); } } else external.add(m[1].split(".")[0]); } }); } }; await Promise.all(Array.from({ length: 24 }, worker)); console.log("modules in closure:", seen.size, "lines:", lines); console.log("external import roots:", [...external].sort().join(", ")); console.log("missing:", missing.length ? missing.join(", ") : "none"); console.log("flagged:", flagged.length); fs.writeFileSync(path.join(cache, "..", "flagged.txt"), flagged.join("\n")); fs.writeFileSync(path.join(cache, "..", "modules.txt"), [...seen].join("\n")); })();