scripts/apply-convergence.js
#!/usr/bin/env node
// apply-convergence.js — phase 2, step 2: apply adjudicated convergence verdicts
// (docs/corpus/convergence-decisions.json) to the dataset repos' records.jsonl
// and regenerate the merged index. Idempotent.
import fs from "node:fs";
import path from "node:path";
import { REPO_ROOT } from "./lib.js";
const CORPUS = path.join(REPO_ROOT, "docs/corpus");
const DATASETS = path.join(process.env.HOME, "Documents/fact.ngo");
const DEPRECATED = "meta-llama-3-1-8b-instruct-fp8";
const decisions = JSON.parse(fs.readFileSync(path.join(CORPUS, "convergence-decisions.json"), "utf8"));
const { clusters } = JSON.parse(fs.readFileSync(path.join(CORPUS, "clusters.json"), "utf8"));
const verdicts = new Map(Object.entries(decisions.record_verdicts));
for (const c of clusters) {
const v = decisions.cluster_verdicts[c.cluster_id];
if (v) for (const id of c.record_ids) if (!verdicts.has(id)) verdicts.set(id, v);
}
const models = fs.readdirSync(DATASETS).filter((d) => fs.existsSync(path.join(DATASETS, d, "records")));
const counts = { convergent: 0, divergent: 0, idiosyncratic: 0, pending: 0 };
const merged = [];
for (const m of models) {
const file = path.join(DATASETS, m, "records", "records.jsonl");
const lines = fs.readFileSync(file, "utf8").split("\n").filter(Boolean);
const out = [];
let changed = 0;
for (const line of lines) {
const r = JSON.parse(line);
const v = m === DEPRECATED ? undefined : verdicts.get(r.id);
if (v && r.convergence !== v) { r.convergence = v; changed++; }
counts[r.convergence || "pending"]++;
if (m !== DEPRECATED) { r._model = m; merged.push(r); }
out.push(JSON.stringify(r));
}
if (changed) fs.writeFileSync(file, out.join("\n") + "\n");
console.log(`${m}: ${changed} records updated`);
}
merged.sort((a, b) => (a._model + a.elicitation.domain + a.elicitation.lens).localeCompare(b._model + b.elicitation.domain + b.elicitation.lens));
const dep = [jsonLines(path.join(DATASETS, DEPRECATED, "records", "records.jsonl")).map((r) => ({ ...r, _model: DEPRECATED }))];
fs.writeFileSync(
path.join(CORPUS, "all-records.jsonl"),
[...merged.map((r) => JSON.stringify(r)), ...dep.flat().map((r) => JSON.stringify(r))].join("\n") + "\n"
);
console.log(`\nactive bench: ${counts.convergent} convergent, ${counts.divergent} divergent, ${counts.idiosyncratic} idiosyncratic, ${counts.pending} pending`);
function jsonLines(f) { return fs.readFileSync(f, "utf8").split("\n").filter(Boolean).map((l) => JSON.parse(l)); }