In this section
- Keys render · gridatlas / tools/twin/keys.mjs lines 1–237
- GridAtlas — Twin ingest · gridatlas / tools/twin/lib.mjs lines 1–71
- Preserve ingest · gridatlas / tools/twin/preserve.mjs lines 1–172
Keys
A JavaScript module in the twin folder of GridAtlas that reads sources, grid substations and the Renewable Energy Planning Database (REPD) register, using SHA-256.
rendergridatlas / tools/twin/keys.mjs · lines 1–237 of 237 · Entry point · pattern keyText (shared by 2 files)
- What it does
- It loads sources, grid substations and the REPD register. Defines 6 functions or classes, including claim, n, grab, nearest, get. Uses libraries node:fs, node:path, node:vm. Side effects: changes the web page (DOM); reads or writes files; starts processes; uses storage (browser storage, DuckDB or Parquet).
- Why it exists in the grid workflow
- Domain stage upstream_foresight (repository name word 'gridatlas'): upstream engineering foresight over GB transmission and distribution topology (TSO/DNO, 132kV and above, the REPD pipeline, data centres), which Ventus Ltd states it models for foresight. Domain terms in the code: the REPD register.
- Inputs & outputs
- In: JSON file current.json; JSON file sources.json; GeoJSON file grid_substations.geojson.
- Downstream impact
- Used directly by 2 files: CI PR Gate (gridatlas); GridAtlas Verify Live Workflow (gridatlas). Reached from 2 other entry points, including GridAtlas PR File Size Guard Workflow (gridatlas); Map Promotion Rehearsal (gridatlas).
- What it achieves
- It is built from the claim step (`claim`), the grab step (`grab`) and the nearest step (`nearest`). The register of keys of a generation, in the Kuiper manner (Ventusltd/kuiper-belt `README.md`, "The first law: no claim without a key": a statement with a key is a fact anyone can recompute; a statement without one is a candidate).
- Why it matters
- Run by CI PR Gate and GridAtlas Verify Live Workflow.
- Application
- gridatlas · tools/twin · JavaScript module
- Uses
- GridAtlas — Twin (import), Loader Composition (import), node:fs, node:path, node:vm, ./data/${manifest.browser_registry.path}
- Used by
- run by CI PR Gate (script call), run by GridAtlas Verify Live Workflow (workflow run)
- Produces
- claim, n, grab, nearest, get, keyText
- Defines here
- claim, n, grab, nearest, get, keyText
- Cannot conclude
- Whether this code runs, is used by a live app today, or is correct. This is a static reading.
- Evidence
- gridatlas@98b5004c5eda9f2ed915fb6fca4b9bbcaa426ca5 tools/twin/keys.mjs:1-237 · blob af95cfd2da5833852325f72234cf56a8e92c505e · GitHub at these lines
- State
- achieves: comment · context: readme_derived · stage: output · effects: dom, filesystem, process, storage · domain stage: upstream_foresight · users: IPP_developer, ICP_grid_engineer · canonical: production_core
#!/usr/bin/env node
/**
* The register of keys of a generation, in the Kuiper manner (Ventusltd/kuiper-belt
* README.md, "The first law: no claim without a key": a statement with a key
* is a fact anyone can recompute; a statement without one is a candidate).
* Every claim about the generation is measured here from the files in the
* repository and carries the file and line, or the command, that proves it.
*
* node tools/twin/keys.mjs --check # measure again; fail on a claim that no longer holds, or on a
* # register that differs from the committed one (the CI check)
* node tools/twin/keys.mjs --write # write tools/twin/keys/<G>.json and <G>.txt
* node tools/twin/keys.mjs --pipeline-refs DIR | --pipeline-refs-network
* # rebuild the Pipeline News ref list from a local release folder,
* # or from the commit-pinned URLs, verifying every sha256; with
* # --check it must equal the committed list, with --write it is written
* --generation G # default: the newest generation that has a register, else the live one
*
* The register carries no clock and no machine path, so two runs on the same
* tree are byte-identical. Node built-ins only; no network unless
* --pipeline-refs-network is given (offline first, then network).
*/
import fs from 'node:fs';
import path from 'node:path';
import vm from 'node:vm';
import { STAMP, EXIT, abs, exists, sha256, sha256Of, readJson, sources, liveGeneration, tmpDir, arg, has, run, keyOf, composition } from './lib.mjs';
import { compose } from '../proofs/loader-composition.mjs';
const registers = fs.existsSync(abs('tools/twin/keys')) ? fs.readdirSync(abs('tools/twin/keys')).filter(f => /^\d{12}\.json$/.test(f)).map(f => f.slice(0, 12)).sort() : [];
const G = arg('--generation', registers.at(-1) || liveGeneration());
const WRITE = has('--write'), CHECK = has('--check');
if (!STAMP.test(G) || WRITE === CHECK) { console.error('usage: keys.mjs --check | --write [--generation G] [--pipeline-refs DIR | --pipeline-refs-network]'); process.exit(EXIT.REFUSED); }
const pin = sources()[G];
if (!pin) { console.error(`refused: tools/twin/sources.json has no entry for ${G}, so there is nothing to key`); process.exit(EXIT.REFUSED); }
const claims = [];
/** keys: [{file,line}|{command}]; a null key (needle not found) makes the claim fail. */
function claim(id, text, holds, keys, measured = {}) {
const missing = keys.some(k => !k);
claims.push({ id, claim: text, holds: Boolean(holds) && !missing, measured, keys: keys.filter(Boolean), ...(missing ? { key_missing: true } : {}) });
}
const n = v => Number(v).toLocaleString('en-GB');
const P = `atlas/v/${G}/current.json`;
const comp = composition(G), prevGen = pin.previous_generation, prev = composition(prevGen);
const manifest = readJson(pin.data_manifest);
const registry = readJson(`data/${manifest.browser_registry.path}`);
const records = new Map(registry.records.map(r => [String(r.repd_ref), r]));
// ---------------------------------------------------------------- K01 source
claim('K01', `The records of ${G} are compiled from ${pin.source.name}, sha256 ${pin.source.sha256}, ${n(pin.source.bytes)} bytes, and from no other register.`,
manifest.source.sha256 === pin.source.sha256 && registry.source.sha256 === pin.source.sha256,
[keyOf('tools/twin/sources.json', pin.source.sha256), keyOf(pin.compiler, `SOURCE_SHA256 = "${pin.source.sha256}"`), keyOf(pin.data_manifest, pin.source.sha256),
{ command: `curl -fsSL ${pin.source.url} | sha256sum` }],
{ sha256: pin.source.sha256, bytes: pin.source.bytes });
// ---------------------------------------------------------------- K02 rows
{
const rows = registry.records.length;
claim('K02', `The record snapshot holds ${n(manifest.closure.rows)} rows, one per REPD ref.`,
rows === manifest.closure.rows && records.size === rows && manifest.closure.unique_repd_refs === rows && manifest.parquet.typed_readback[0] === rows,
[keyOf(pin.data_manifest, `"rows": ${manifest.closure.rows}`), keyOf(pin.data_manifest, `"unique_repd_refs": ${manifest.closure.unique_repd_refs}`),
{ command: `node -e "console.log(require('./data/${manifest.browser_registry.path}').records.length)"` }],
{ manifest_rows: manifest.closure.rows, registry_records: rows, distinct_refs: records.size, status_counts: manifest.closure.status_counts });
}
// ---------------------------------------------------------------- K03 data files
{
const files = [['parquet', `data/${manifest.parquet.path}`, manifest.parquet.sha256], ['browser_registry', `data/${manifest.browser_registry.path}`, manifest.browser_registry.sha256], ['manifest', pin.data_manifest, null]];
const measured = Object.fromEntries(files.map(([k, f]) => [k, { file: f, sha256: sha256Of(f), bytes: fs.statSync(abs(f)).size }]));
const own = comp.cartridges.filter(c => String(c.generation) === G);
const carriers = own.filter(c => [measured.manifest.sha256, measured.parquet.sha256].every(h => fs.readFileSync(abs(c.file), 'utf8').includes(h)));
claim('K03', `The three data files hash to the values the manifest records, and ${carriers.length} cartridge(s) cut at ${G} refuse any other manifest or parquet.`,
files.every(([k, , want]) => !want || measured[k].sha256 === want) && carriers.length > 0 && carriers.length === own.length,
[keyOf(pin.data_manifest, measured.parquet.sha256), keyOf(pin.data_manifest, measured.browser_registry.sha256),
...carriers.flatMap(c => [keyOf(c.file, measured.manifest.sha256), keyOf(c.file, measured.parquet.sha256)]),
{ command: `sha256sum ${files.map(f => f[1]).join(' ')}` }],
{ ...measured, carried_by: carriers.map(c => c.id) });
}
// ---------------------------------------------------------------- K04 cartridges
{
const rows = comp.cartridges.map(c => ({ id: c.id, generation: String(c.generation), file: c.file, pinned: c.sha256, sha256: exists(c.file) ? sha256Of(c.file) : null }));
claim('K04', `The ${rows.length} cartridges of ${G} are in the repository with the sha256 its pointer records.`,
rows.length > 0 && rows.every(r => r.sha256 === r.pinned),
[...rows.map(r => keyOf(P, `"sha256": "${r.pinned}"`)), { command: 'node tools/twin/preserve.mjs' }],
{ cartridges: rows.map(({ pinned, ...r }) => r) });
}
// ---------------------------------------------------------------- K05 it loads
{
const out = await compose({ loaderHtml: fs.readFileSync(abs(`atlas/v/${G}/index.html`), 'utf8'), route: `atlas/v/${G}/` });
claim('K05', `${G} composes through its own committed loader: every cartridge is fetched, hash-verified and handed to the shell.`,
!out.failed && out.scripts.length === comp.cartridges.length,
[keyOf(P, `"generation": "${G}"`), { command: `node tools/twin/keys.mjs --check # compose() of tools/proofs/loader-composition.mjs on atlas/v/${G}/` }],
out.failed ? { failed: String(out.message).slice(0, 200) } : { cartridges_composed: out.scripts.length, html_bytes: out.html.length, html_sha256: sha256(Buffer.from(out.html)) });
}
// ---------------------------------------------------------------- K06 the older GridAtlas stays a version
{
const files = [`atlas/v/${prevGen}/current.json`, `atlas/v/${prevGen}/index.html`];
const out = files.every(exists) ? await compose({ loaderHtml: fs.readFileSync(abs(files[1]), 'utf8'), route: `atlas/v/${prevGen}/` }) : { failed: true, message: 'not published' };
claim('K06', `The previous live generation ${prevGen} is still published under atlas/v/${prevGen}/ with the bytes recorded here, and still composes through its own loader.`,
!out.failed && String(comp.pointer.previous_generation) === prevGen,
[keyOf(P, `"previous_generation": "${prevGen}"`), { command: `sha256sum ${files.join(' ')}` }, { command: 'node tools/twin/preserve.mjs' }],
out.failed ? { failed: String(out.message).slice(0, 200) }
: { files: files.map(f => ({ file: f, sha256: sha256Of(f), bytes: fs.statSync(abs(f)).size })), cartridges_composed: out.scripts.length, html_sha256: sha256(Buffer.from(out.html)) });
}
// ---------------------------------------------------------------- K07 the grid engine is the same bytes
const SUBS = 'atlas/releases/202608300453-atlas-v9/data/grid_substations.geojson';
{
const before = new Map(prev.cartridges.map(c => [c.id, c]));
const carried = comp.cartridges.filter(c => String(c.generation) !== G);
const rows = carried.map(c => ({ id: c.id, file: c.file, sha256: c.sha256, same_as_previous: before.get(c.id)?.sha256 === c.sha256 && before.get(c.id)?.file === c.file }));
const recut = comp.cartridges.filter(c => String(c.generation) === G).map(c => c.id);
const subsSha = sha256Of(SUBS);
claim('K07', `The grid engine did not change: ${rows.length} cartridges (${rows.map(r => r.id).join(', ')}) are the same files with the same sha256 in ${prevGen} and ${G}, and the substation payload is the released file. Only ${recut.join(' and ')} were re-cut.`,
rows.length > 0 && rows.every(r => r.same_as_previous) && rows.some(r => r.id === 'sld-sandbox'),
[...rows.flatMap(r => [keyOf(P, `"sha256": "${r.sha256}"`), keyOf(prev.rel, `"sha256": "${r.sha256}"`)]),
keyOf('atlas/releases/202608300453-atlas-v9/sha256sums.txt', `${subsSha} data/grid_substations.geojson`)],
{ carried: rows, recut, substations: { file: SUBS, sha256: subsSha } });
}
// ---------------------------------------------------------------- K08 nearest substation, retained refs
{
// The shipped code, not a copy of it: distanceKm from the geodesy module, voltagesKv, representativePoint and the
// constants from the composed sld-sandbox cartridge (as FIXES/NL-016 tools/nearest_substation_check.mjs does).
const sldFile = comp.cartridges.find(c => c.id === 'sld-sandbox').file, sld = fs.readFileSync(abs(sldFile), 'utf8');
const ctx = { window: {}, Math, Number, String, Array, Object, JSON };
vm.createContext(ctx);
vm.runInContext(fs.readFileSync(abs('atlas/modules/202609011950-geodesy.js'), 'utf8'), ctx);
const distanceKm = ctx.window.__GRIDATLAS_MODULES__.geodesy.distanceKm;
const grab = name => { const i = sld.indexOf(`function ${name}(`); let d = 0; for (let k = sld.indexOf('{', i); k < sld.length; k++) { if (sld[k] === '{') d++; else if (sld[k] === '}' && --d === 0) return sld.slice(i, k + 1); } };
const voltagesKv = new Function(`${grab('voltagesKv')}; return voltagesKv;`)(), representativePoint = new Function(`${grab('representativePoint')}; return representativePoint;`)();
const MIN_KV = Number(sld.match(/const MIN_KV = (\d+);/)[1]);
const subs = [];
for (const f of readJson(SUBS).features) {
const kv = voltagesKv(f.properties); if (!kv.length || Math.max(...kv) < MIN_KV - 0.5) continue;
const at = representativePoint(f.geometry); if (!at) continue;
subs.push({ at, kv: Math.max(...kv), name: f.properties?.name || 'Unnamed substation' });
}
const nearest = (lon, lat) => { let best = null, km = Infinity; for (const s of subs) { const d = distanceKm(lon, lat, s.at[0], s.at[1]); if (d < km) { km = d; best = s; } } return { name: best.name, kv: best.kv, km: Number(km.toFixed(3)) }; };
const oldReg = readJson(`data/repd_browser_registry_${pin.previous_records}.json`).records;
const retained = oldReg.filter(r => records.has(String(r.repd_ref)));
const moved = [];
for (const o of retained) {
const r = records.get(String(o.repd_ref));
if (o.longitude === r.longitude && o.latitude === r.latitude) continue;
const a = nearest(o.longitude, o.latitude), b = nearest(r.longitude, r.latitude);
moved.push({ repd_ref: String(o.repd_ref), moved_km: Number(distanceKm(o.longitude, o.latitude, r.longitude, r.latitude).toFixed(3)), before: a, after: b, same_substation: a.name === b.name && a.kv === b.kv && Math.abs(a.km - b.km) < 0.0005 });
}
moved.sort((x, y) => Number(x.repd_ref) - Number(y.repd_ref));
const changed = moved.filter(m => !m.same_substation);
const still = retained.length - moved.length;
// Evidence that the engine really ran: one retained ref measured from its unchanged coordinate.
const golden = records.get(manifest.golden_case.repd_ref);
claim('K08', `Nearest substation for retained REPD refs: of ${n(retained.length)} refs in both ${pin.previous_records} and ${G}, ${n(still)} have the identical coordinate, so the unchanged engine (K07) returns the identical answer; ${moved.length} were moved by the source register, and ${changed.length} of those now measure to a different nearest substation (listed).`,
retained.length > 0 && subs.length > 0 && still + moved.length === retained.length,
[keyOf(sldFile, `const MIN_KV = ${MIN_KV};`), keyOf(sldFile, 'function voltagesKv(properties)'), keyOf('atlas/releases/202608300453-atlas-v9/sha256sums.txt', 'data/grid_substations.geojson'),
{ command: 'node tools/twin/keys.mjs --check # K08 recomputes every moved ref with the shipped geodesy and sld-sandbox functions' }],
{ retained: retained.length, added: records.size - retained.length, dropped: oldReg.length - retained.length, identical_coordinate: still, moved: moved.length, moved_nearest_changed: changed.length,
substations_qualifying: subs.length, min_kv: MIN_KV, golden: { repd_ref: manifest.golden_case.repd_ref, nearest: nearest(golden.longitude, golden.latitude) }, moved_refs: moved });
}
// ---------------------------------------------------------------- K09 Pipeline News refs
{
const pn = pin.pipeline_news, ACTIVE = new Set(Object.keys(manifest.closure.status_counts));
// --pipeline-refs DIR / --pipeline-refs-network: rebuild the list from the release itself, every file checked against the pinned manifest.
const dir = arg('--pipeline-refs');
let rebuilt = null;
if (dir || has('--pipeline-refs-network')) {
const work = tmpDir('keys');
const get = rel => {
if (dir) return fs.readFileSync(path.join(path.resolve(dir), rel));
const to = path.join(work, path.basename(rel)), r = run(['curl', '-fsSL', '--retry', '2', '--max-time', '120', '-o', to, pn.base_url + rel]);
if (r.code !== 0) { console.error(`BLOCKED_DEPENDENCY: ${pn.base_url + rel} did not answer (curl exit ${r.code})`); process.exit(EXIT.BLOCKED_DEPENDENCY); }
return fs.readFileSync(to);
};
const mBytes = get(pn.manifest);
if (sha256(mBytes) !== pn.manifest_sha256) { console.error(`FAIL: ${pn.manifest} is sha256 ${sha256(mBytes)}, the pin is ${pn.manifest_sha256}. Not used.`); process.exit(EXIT.FAIL); }
const rows = [];
for (const part of JSON.parse(mBytes).project_partitions) {
const b = get(part.path);
if (sha256(b) !== part.sha256) { console.error(`FAIL: ${part.path} is not the sha256 its manifest records. Not used.`); process.exit(EXIT.FAIL); }
for (const p of JSON.parse(b).projects) rows.push([String(p.repd_ref), p.status]);
}
fs.rmSync(work, { recursive: true, force: true });
rows.sort((a, b) => Number(a[0]) - Number(b[0]) || a[0].localeCompare(b[0]));
rebuilt = `# Pipeline News ${pn.release}: repd_ref<TAB>status, every project of ${pn.manifest} (sha256 ${pn.manifest_sha256}), sorted by ref. Written by tools/twin/keys.mjs; do not edit.\n` + rows.map(r => r.join('\t')).join('\n') + '\n';
if (WRITE) fs.writeFileSync(abs(pn.refs), rebuilt);
console.log(`pipeline refs: ${rows.length} rows rebuilt from ${dir ? 'the local release folder' : 'the pinned URLs'}, every file hash-verified`);
}
const listed = exists(pn.refs) ? fs.readFileSync(abs(pn.refs), 'utf8') : '';
const rows = listed.split('\n').filter(l => l && !l.startsWith('#')).map(l => l.split('\t'));
const resolved = rows.filter(([ref]) => records.has(ref));
const absent = rows.filter(([ref]) => !records.has(ref));
const absentActive = absent.filter(([, status]) => ACTIVE.has(String(status).toLowerCase())).map(([ref, status]) => ({ repd_ref: ref, status }));
const byStatus = {};
for (const [, s] of absent) byStatus[s] = (byStatus[s] || 0) + 1;
claim('K09', `Pipeline News ${pn.release} lists ${n(rows.length)} REPD refs. ${n(resolved.length)} resolve to a record of ${G} by exact ref. ${n(absent.length)} do not, because the snapshot keeps the four active statuses only; ${absentActive.length} of those have an active status in Pipeline News and still have no record (${absentActive.map(a => a.repd_ref).join(', ') || 'none'}).`,
rows.length > 0 && new Set(rows.map(r => r[0])).size === rows.length && resolved.length + absent.length === rows.length && (rebuilt === null || rebuilt === listed),
[keyOf(pn.refs, `sha256 ${pn.manifest_sha256}`), keyOf('tools/twin/sources.json', pn.manifest_sha256),
{ command: `node tools/twin/keys.mjs --check --pipeline-refs-network # rebuilds the list from ${pn.base_url}${pn.manifest}` }],
{ release: pn.release, refs: rows.length, resolved: resolved.length, not_in_snapshot: absent.length, not_in_snapshot_by_status: Object.fromEntries(Object.entries(byStatus).sort()),
active_without_record: absentActive, list: { file: pn.refs, sha256: listed ? sha256(Buffer.from(listed)) : null }, ...(rebuilt !== null && rebuilt !== listed ? { list_differs_from_release: true } : {}) });
}
// ---------------------------------------------------------------- K10 the twin (proved by its own command)
claim('K10', `${G} is reproducible: its three data files and the cartridges it cut rebuild byte for byte from the pinned source with the repository's own compiler and cut tools.`,
true, [{ command: `node tools/twin/replicate-generation.mjs --generation ${G} --network` }],
{ proved_by: 'the twin job, not by this register: a claim whose only key is a command holds when that command exits 0' });
// ---------------------------------------------------------------- write or check
const register = { schema: 'gridatlas.keys.v1', generation: G, law: 'no claim without a key: every claim is measured by tools/twin/keys.mjs and names the file and line, or the command, that proves it', claims };
const json = JSON.stringify(register, null, 1) + '\n';
const keyText = k => (k.file ? `${k.file}:${k.line}` : `$ ${k.command}`);
const text = [`Keys of GridAtlas generation ${G}`, 'Written by node tools/twin/keys.mjs --write; checked in CI by --check. Do not edit.', '',
...claims.flatMap(c => [`${c.id} ${c.holds ? 'HOLDS' : 'DOES NOT HOLD'} ${c.claim}`, ...c.keys.map(k => ` key ${keyText(k)}`), ''])].join('\n');
const jsonRel = `tools/twin/keys/${G}.json`, textRel = `tools/twin/keys/${G}.txt`;
for (const c of claims) console.log(`${c.holds ? 'PASS ' : 'FAIL '} ${c.id} ${c.claim}`);
const broken = claims.filter(c => !c.holds);
if (WRITE) {
fs.mkdirSync(abs('tools/twin/keys'), { recursive: true });
fs.writeFileSync(abs(jsonRel), json);
fs.writeFileSync(abs(textRel), text);
console.log(`\nwrote ${jsonRel} (${json.length} B) and ${textRel}; ${claims.length - broken.length}/${claims.length} claims hold`);
} else {
const drift = [];
if (!exists(jsonRel) || fs.readFileSync(abs(jsonRel), 'utf8') !== json) drift.push(jsonRel);
if (!exists(textRel) || fs.readFileSync(abs(textRel), 'utf8') !== text) drift.push(textRel);
for (const d of drift) console.error(`FAIL ${d} is not what the repository now measures; a fact behind a claim moved. Read the claims above, then: node tools/twin/keys.mjs --write`);
console.log(`\nkeys ${G}: ${claims.length - broken.length}/${claims.length} claims hold; register ${drift.length ? 'DIFFERS from' : 'equals'} the committed one`);
if (drift.length) process.exit(EXIT.FAIL);
}
process.exit(broken.length ? EXIT.FAIL : EXIT.PASS);GridAtlas — Twin
A JavaScript module in the tools folder that reads sources, using SHA-256. It reads JSON (`readJson`) and is built from the abs step (`abs`) and the exists step (`exists`).
ingestgridatlas / tools/twin/lib.mjs · lines 1–71 of 71 · pattern composition (shared by 2 files)
- What it does
- It loads sources. Defines 15 functions or classes, including abs, exists, sha256, sha256Of, readJson. Uses libraries node:fs, node:os, node:path, node:crypto, node:child_process. Side effects: reads or writes files; starts processes.
- Why it exists in the grid workflow
- Domain stage upstream_foresight (repository name word 'gridatlas'): upstream engineering foresight over GB transmission and distribution topology (TSO/DNO, 132kV and above, the REPD pipeline, data centres), which Ventus Ltd states it models for foresight.
- Inputs & outputs
- In: JSON file sources.json; JSON file current.json; command-line arguments.
- Downstream impact
- Used directly by 3 files: Keys (gridatlas); Preserve (gridatlas); Replicate Generation (gridatlas). Reached from 4 other entry points, including GridAtlas PR File Size Guard Workflow (gridatlas); GridAtlas Verify Live Workflow (gridatlas); CI PR Gate (gridatlas).
- What it achieves
- Shared by the three twin tools. It reads `tools/twin/sources.json`.
- Why it matters
- Imported by Keys, Preserve and Replicate Generation.
- Application
- gridatlas · tools/twin · JavaScript module
- Uses
- node:fs, node:os, node:path, node:crypto, node:child_process, node:url
- Used by
- imported by Keys (import), imported by Preserve (import), imported by Replicate Generation (import)
- Produces
- ROOT, STAMP, abs, exists, sha256, sha256Of, readJson, sources, liveGeneration, tmpDir, arg, has
- Defines here
- abs, exists, sha256, sha256Of, readJson, sources, liveGeneration, tmpDir, arg, has, run, keyOf, composition, reporter, say
- Cannot conclude
- Whether this code runs, is used by a live app today, or is correct. This is a static reading.
- Evidence
- gridatlas@98b5004c5eda9f2ed915fb6fca4b9bbcaa426ca5 tools/twin/lib.mjs:1-71 · blob a8b3c4bc6e11c1deed6f0a872c184a5ffc76920a · GitHub at these lines
- State
- achieves: comment · context: readme_derived · stage: ingest · effects: filesystem, process · domain stage: upstream_foresight · users: IPP_developer, ICP_grid_engineer · canonical: production_core
// Shared by the three twin tools. Node built-ins only.
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { createHash } from 'node:crypto';
import { spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
export const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..');
export const STAMP = /^\d{12}$/;
export const abs = rel => path.join(ROOT, rel);
export const exists = rel => fs.existsSync(abs(rel));
export const sha256 = bytes => createHash('sha256').update(bytes).digest('hex');
export const sha256Of = file => sha256(fs.readFileSync(path.isAbsolute(file) ? file : abs(file)));
export const readJson = rel => JSON.parse(fs.readFileSync(path.isAbsolute(rel) ? rel : abs(rel), 'utf8'));
export const sources = () => readJson('tools/twin/sources.json').generations;
export const liveGeneration = () => String(readJson('atlas/current.json').generation);
export const tmpDir = tag => fs.mkdtempSync(path.join(os.tmpdir(), `gridatlas-${tag}-`));
export function arg(flag, fallback = null) {
const i = process.argv.indexOf(flag);
return i < 0 ? fallback : process.argv[i + 1];
}
export const has = flag => process.argv.includes(flag);
export function run(argv, opts = {}) {
const r = spawnSync(argv[0], argv.slice(1), { cwd: ROOT, encoding: 'utf8', maxBuffer: 256 * 1024 * 1024, ...opts });
return { code: r.status ?? 1, stdout: r.stdout || '', stderr: r.stderr || (r.error ? String(r.error.message) : '') };
}
/** The key of a statement: the first line of a file that contains the needle,
as { file, line }. A needle that is not there has no key, and the caller
must treat the claim as not holding (kuiper-belt README, the first law). */
export function keyOf(rel, needle) {
const lines = fs.readFileSync(abs(rel), 'utf8').split('\n');
const i = lines.findIndex(l => l.includes(needle));
return i < 0 ? null : { file: rel, line: i + 1 };
}
/** The composition a generation pins, and each cartridge's file in the repository. */
export function composition(gen) {
const rel = `atlas/v/${gen}/current.json`;
const pointer = readJson(rel);
const byId = new Map((pointer.cartridges || []).map(c => [c.id, c]));
const cartridges = (pointer.cartridge_order || []).map(id => {
const c = byId.get(id);
return c ? { ...c, file: path.posix.normalize(path.posix.join(`atlas/v/${gen}`, c.path)) } : { id, missing: true };
});
return { rel, pointer, cartridges };
}
/** Outcome classes, kept apart as docs/CI-CD-PLAN.md keeps them. */
export const EXIT = Object.freeze({ PASS: 0, FAIL: 1, REFUSED: 2, BLOCKED_HARNESS: 3, BLOCKED_DEPENDENCY: 4 });
export function reporter(title) {
const rows = [];
const say = (state, name, detail = '') => { rows.push({ state, name, detail }); console.log(`${state.padEnd(5)} ${name}${detail ? ` ${detail}` : ''}`); };
return {
rows,
pass: (n, d) => say('PASS', n, d),
fail: (n, d) => say('FAIL', n, d),
note: (n, d) => say('note', n, d),
failed: () => rows.filter(r => r.state === 'FAIL'),
finish(extra = '') {
const bad = rows.filter(r => r.state === 'FAIL'), ok = rows.filter(r => r.state === 'PASS');
console.log(`\n${title}: ${ok.length} passed, ${bad.length} failed${extra ? `; ${extra}` : ''}`);
for (const b of bad) console.error(` FAIL ${b.name} ${b.detail}`);
return bad.length ? EXIT.FAIL : EXIT.PASS;
}
};
}Preserve
A JavaScript module in the twin folder of GridAtlas that reads parts and composition, using SHA-256.
ingestgridatlas / tools/twin/preserve.mjs · lines 1–172 of 172 · Entry point
- What it does
- It loads parts and composition. Uses libraries node:fs, node:path. Side effects: reads or writes files; starts processes.
- Why it exists in the grid workflow
- Domain stage upstream_foresight (repository name word 'gridatlas'): upstream engineering foresight over GB transmission and distribution topology (TSO/DNO, 132kV and above, the REPD pipeline, data centres), which Ventus Ltd states it models for foresight.
- Inputs & outputs
- In: JSON file current.json; JSON file -parts.json; JSON file -composition.json.
- Downstream impact
- Used directly by 2 files: CI PR Gate (gridatlas); GridAtlas Verify Live Workflow (gridatlas). Reached from 2 other entry points, including GridAtlas PR File Size Guard Workflow (gridatlas); Map Promotion Rehearsal (gridatlas).
- What it achieves
- Preservation: every published generation stays a version. It reads `atlas/current.json`.
- Why it matters
- Run by CI PR Gate and GridAtlas Verify Live Workflow.
- Application
- gridatlas · tools/twin · JavaScript module
- Uses
- GridAtlas — Twin (import), GridAtlas — Scope (import), Loader Composition (import), node:fs, node:path
- Used by
- run by CI PR Gate (script call), run by GridAtlas Verify Live Workflow (workflow run)
- Produces
- none declared
- Defines here
- none
- Cannot conclude
- Whether this code runs, is used by a live app today, or is correct. This is a static reading.
- Evidence
- gridatlas@98b5004c5eda9f2ed915fb6fca4b9bbcaa426ca5 tools/twin/preserve.mjs:1-172 · blob a792b9266aa2fa399aeaf2fa2172692514005f6e · GitHub at these lines
- State
- achieves: comment · context: readme_derived · stage: ingest · effects: filesystem, process · domain stage: upstream_foresight · users: IPP_developer, ICP_grid_engineer · canonical: production_core
#!/usr/bin/env node
/**
* Preservation: every published generation stays a version. Node built-ins
* only, no network. Nothing is written.
*
* node tools/twin/preserve.mjs # the working tree
* node tools/twin/preserve.mjs --base <rev> # and nothing published at <rev> was changed or removed
*
* Fails when any of these does not hold:
*
* pinned cartridges every cartridge of every atlas/v/<G>/current.json is in
* the repository with the sha256 that pointer records
* loaders every atlas/v/<G>/ has its index.html, a registered loader
* release shells every atlas/releases/<id>/sha256sums.txt verifies
* data manifests every data/repd_v9_manifest_<G>.json describes the parquet
* and registry beside it
* assembled every atlas/manifests/*-parts.json names a cartridge file
* with the sha256 it records
* previous live the generation the live pointer superseded is still
* published under atlas/v/, and its own committed loader,
* run in node:vm (tools/proofs/loader-composition.mjs),
* composes it: every cartridge fetched and hash-verified
* immutable (--base) no file that existed at <rev> under atlas/v/,
* atlas/releases/, atlas/cartridges/, atlas/modules/,
* atlas/parts/, atlas/manifests/ or data/ is modified,
* deleted or renamed. New files are welcome: a change is
* a new generation (kuiper-belt editions/README.md:
* "Earlier dated folders are kept unchanged as history").
*
* Reported, not failed: part files and composition manifests whose recorded
* sha256 no longer matches. Those drifts predate this check (175 parts and 3
* composition manifests at main b0a3a938); the --base rule stops new ones.
*/
import fs from 'node:fs';
import path from 'node:path';
import { STAMP, abs, exists, sha256Of, readJson, liveGeneration, arg, run, composition, reporter } from './lib.mjs';
import { sha256PublishedFile, registeredLoader, slotsUsed } from '../scope/lib.mjs';
import { compose } from '../proofs/loader-composition.mjs';
const BASE = arg('--base');
const R = reporter('preservation');
const gens = fs.readdirSync(abs('atlas/v')).filter(d => STAMP.test(d)).sort();
const live = liveGeneration();
// ---------------------------------------------------------------- pinned cartridges, loaders
{
const bad = [], badLoader = [];
let n = 0;
for (const g of gens) {
if (!exists(`atlas/v/${g}/current.json`)) { bad.push(`atlas/v/${g}/current.json missing`); continue; }
let comp;
try { comp = composition(g); } catch (e) { bad.push(`atlas/v/${g}/current.json unreadable: ${e.message}`); continue; }
if (String(comp.pointer.generation) !== g) bad.push(`atlas/v/${g}/current.json declares generation ${comp.pointer.generation}`);
for (const c of comp.cartridges) {
n++;
if (c.missing) bad.push(`${g} ${c.id}: ordered but not described`);
else if (!exists(c.file)) bad.push(`${g} ${c.id}: ${c.file} missing`);
else if (sha256PublishedFile(abs(c.file)) !== c.sha256) bad.push(`${g} ${c.id}: ${c.file} is not the sha256 atlas/v/${g}/current.json records`);
}
const index = `atlas/v/${g}/index.html`;
if (!exists(index)) { badLoader.push(`${index} missing`); continue; }
const loader = registeredLoader(abs(index));
if (!loader) badLoader.push(`${index} is not a registered loader`);
else for (const s of slotsUsed(comp.pointer)) if (!loader.slots.includes(s)) badLoader.push(`${index} (loader ${loader.name}) cannot compose slot ${s}`);
}
if (!gens.length) R.fail('pinned cartridges', 'no generation under atlas/v/: nothing examined');
else if (bad.length) R.fail('pinned cartridges', bad.join('; '));
else R.pass('pinned cartridges', `${n} cartridge pins of ${gens.length} generations (${gens[0]} to ${gens.at(-1)}) hold`);
if (badLoader.length) R.fail('loaders', badLoader.join('; '));
else R.pass('loaders', `${gens.length} generations each have a registered loader that supports their slots`);
}
// ---------------------------------------------------------------- release shells
{
const bad = [];
let files = 0, sums = 0;
for (const id of fs.readdirSync(abs('atlas/releases')).sort()) {
const rel = `atlas/releases/${id}/sha256sums.txt`;
if (!exists(rel)) continue;
sums++;
for (const line of fs.readFileSync(abs(rel), 'utf8').split('\n').filter(Boolean)) {
const m = line.match(/^([0-9a-f]{64})\s+\*?(.+)$/);
if (!m) { bad.push(`${rel}: unreadable line`); continue; }
files++;
const f = `atlas/releases/${id}/${m[2].trim()}`;
if (!exists(f)) bad.push(`${f} missing`);
else if (sha256PublishedFile(abs(f)) !== m[1] && sha256Of(f) !== m[1]) bad.push(`${f} is not the sha256 in ${rel}`);
}
}
if (!files) R.fail('release shells', 'no sha256sums.txt lines: nothing examined');
else if (bad.length) R.fail('release shells', bad.join('; '));
else R.pass('release shells', `${files} files of ${sums} release sha256sums.txt verify`);
}
// ---------------------------------------------------------------- data manifests
{
const bad = [], seen = [];
for (const f of fs.readdirSync(abs('data')).filter(f => /^repd_v9_manifest_\d{12}\.json$/.test(f)).sort()) {
const m = readJson(`data/${f}`);
seen.push(m.generation);
for (const k of ['parquet', 'browser_registry']) {
const rel = `data/${m[k].path}`;
if (!exists(rel)) bad.push(`${rel} missing`);
else if (sha256Of(rel) !== m[k].sha256) bad.push(`${rel} is not the sha256 in data/${f}`);
}
}
if (!seen.length) R.fail('data manifests', 'no data/repd_v9_manifest_*.json: nothing examined');
else if (bad.length) R.fail('data manifests', bad.join('; '));
else R.pass('data manifests', `record snapshots ${seen.join(', ')}: parquet and registry are the files their manifests describe`);
}
// ---------------------------------------------------------------- assembled cartridges; drift notes
{
const bad = [], partDrift = [], compDrift = [];
let n = 0;
for (const f of fs.readdirSync(abs('atlas/manifests')).sort()) {
const rel = `atlas/manifests/${f}`;
if (f.endsWith('-parts.json')) {
const m = readJson(rel), cart = path.posix.normalize(path.posix.join('atlas', m.cartridge));
n++;
if (!exists(cart)) bad.push(`${cart} missing (${rel})`);
else if (sha256PublishedFile(abs(cart)) !== m.sha256) bad.push(`${cart} is not the sha256 in ${rel}`);
for (const p of m.assembled_from || []) if (!exists(p.path) || sha256PublishedFile(abs(p.path)) !== p.sha256) partDrift.push(`${f}:${p.path}`);
} else if (f.endsWith('-composition.json')) {
for (const c of readJson(rel).cartridges || []) {
if (!c.path || !c.sha256) continue;
const file = path.posix.normalize(path.posix.join('atlas', c.path));
if (!exists(file) || sha256PublishedFile(abs(file)) !== c.sha256) compDrift.push(`${f}:${c.id}`);
}
}
}
if (!n) R.fail('assembled', 'no parts manifest: nothing examined');
else if (bad.length) R.fail('assembled', bad.join('; '));
else R.pass('assembled', `${n} parts manifests each name a cartridge with the sha256 they record`);
if (partDrift.length) R.note('part drift', `${partDrift.length} part or module files no longer hash to what an older parts manifest recorded (the assembled cartridges themselves hold); first: ${partDrift[0]}`);
if (compDrift.length) R.note('composition drift', `${compDrift.length} cartridge entries of composition manifests never published under atlas/v/ no longer match: ${compDrift.slice(0, 3).join(', ')}`);
}
// ---------------------------------------------------------------- previous live generation
{
const pointer = readJson('atlas/current.json');
const prev = String(pointer.previous_generation || pointer.superseded_generation || '');
const bad = [];
let detail = '';
if (!STAMP.test(prev)) bad.push(`atlas/current.json (generation ${live}) names no previous_generation`);
else if (!exists(`atlas/v/${prev}/current.json`) || !exists(`atlas/v/${prev}/index.html`)) bad.push(`atlas/v/${prev}/ is not published (current.json and index.html)`);
else {
const out = await compose({ loaderHtml: fs.readFileSync(abs(`atlas/v/${prev}/index.html`), 'utf8'), route: `atlas/v/${prev}/` });
const order = composition(prev).pointer.cartridge_order;
if (out.failed) bad.push(`atlas/v/${prev}/ does not compose through its own loader: ${String(out.message).replace(/<[^>]+>/g, ' ').trim().slice(0, 200)}`);
else if (out.scripts.length !== order.length) bad.push(`atlas/v/${prev}/ composed ${out.scripts.length} cartridges, its pointer orders ${order.length}`);
else detail = `${prev} is published at atlas/v/${prev}/ and composes through its own loader: ${out.scripts.length} cartridges fetched and hash-verified, ${out.html.length} B of HTML`;
}
if (bad.length) R.fail('previous live', bad.join('; '));
else R.pass('previous live', detail);
}
// ---------------------------------------------------------------- immutable against the base
if (BASE) {
const roots = ['atlas/v/', 'atlas/releases/', 'atlas/cartridges/', 'atlas/modules/', 'atlas/parts/', 'atlas/manifests/', 'data/'];
const d = run(['git', 'diff', '--name-status', '--no-renames', BASE, '--', ...roots]);
if (d.code !== 0) R.fail('immutable', `cannot diff against ${BASE}: ${d.stderr.trim().slice(0, 200)}`);
else {
const rows = d.stdout.split('\n').filter(Boolean).map(l => l.split('\t'));
const touched = rows.filter(([s]) => s !== 'A');
if (touched.length) R.fail('immutable', `published at ${BASE} and changed or removed: ${touched.map(([s, f]) => `${s} ${f}`).join('; ')}. Published files are never edited; cut a new generation.`);
else R.pass('immutable', `against ${BASE}: ${rows.length} file(s) added under the published roots, none modified or removed`);
}
} else R.note('immutable', 'NOT RUN: no --base given, so the working tree was not compared with an earlier commit');
process.exit(R.finish());