docs(261026-d95): record seeded oracle and paired NO-GO verdict

This commit is contained in:
araharah-executor
2026-10-26 09:43:27 +08:00
parent 92fc2138ec
commit 9a671bd051
2 changed files with 54 additions and 13 deletions
@@ -1,6 +1,6 @@
#!/usr/bin/env node
/* Branch-only directional ALT experiment. Neither production nor integrations import this. */
import { readFileSync, existsSync } from 'node:fs';
import { readFileSync, existsSync, writeFileSync, unlinkSync } from 'node:fs';
import { createHash } from 'node:crypto';
import { hostname } from 'node:os';
import { fileURLToPath } from 'node:url';
@@ -11,6 +11,23 @@ import { routeAstar } from '../src/astar.ts';
import { generateCrossMetroPairs } from './measure-optimality-baseline.mjs';
import { sweepOracle } from './measure-astar-oracle-sweep.mjs';
// Source-identical isolated instrumented clone: only two settled counters are injected.
// The production file is never opened for writing; remove the ephemeral copy in finally.
async function instrumentedShipped() {
const source = new URL('../src/astar.ts', import.meta.url);
const clone = new URL('../src/astar-alt-measure.tmp.ts', import.meta.url);
let code = readFileSync(source, 'utf8');
const forward = 'if (expandForward) fClosed[u] = 1;', backward = 'else bClosed[u] = 1;';
if (!code.includes(forward) || !code.includes(backward)) throw Error('production settled markers changed; instrumentation not valid');
code = code.replace(forward, 'if (expandForward) { fClosed[u] = 1; globalThis.__altControlSettled.forward++; }')
.replace(backward, 'else { bClosed[u] = 1; globalThis.__altControlSettled.backward++; }');
if (existsSync(clone)) throw Error('temporary instrumentation clone already exists');
try {
writeFileSync(clone, code, { flag: 'wx' });
return await import(clone.href);
} finally { if (existsSync(clone)) unlinkSync(clone); }
}
const INF = Infinity, SENTINEL = 0xffff;
class Heap {
a = [];
@@ -154,31 +171,39 @@ if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
const verdict = sweepOracle(graph, pairs, { snap: snapToGraph, astar: routeDirectionSplit, oracle: dijkstra });
console.log(`oracle: compared=${pairs.length} snap-miss=${missing.snap} routed=${verdict.routed} unreachable=${verdict.unreachable} suboptimal=${verdict.suboptimal} oracle-mismatch=${verdict.oracleMismatch} class-break=${verdict.classBreaks} pass=${verdict.pass}`);
if (!verdict.pass) { console.error(verdict.rows.filter(r => !['pass', 'unreachable'].includes(r.verdict))); process.exitCode = 1; }
globalThis.__altControlSettled = { forward: 0, backward: 0 };
const control = (await instrumentedShipped()).routeAstar;
const extended = generateCrossMetroPairs(graph.bbox, { count: 100, seed: 0x08a1, minPairKm: 12, haversineM });
const warm = (fn) => { const a = process.hrtime.bigint(); fn(); return Number(process.hrtime.bigint() - a) / 1e6; };
console.log(`warm-up ms: experimental=${warm(() => routeDirectionSplit(graph, 0, 1)).toFixed(3)} shipped=${warm(() => routeAstar(graph, 0, 1)).toFixed(3)}`);
const times = [[], []], counts = [0, 0], misses = { snap: 0, unreachable: 0 }, active = [0, 0];
const warmExp = warm(() => routeDirectionSplit(graph, 0, 1));
const warmShipped = warm(() => routeAstar(graph, 0, 1));
const warmClone = warm(() => control(graph, 0, 1));
console.log(`warm-up ms (unmeasured): experimental=${warmExp.toFixed(3)} original-shipped=${warmShipped.toFixed(3)} instrumented-clone=${warmClone.toFixed(3)}`);
globalThis.__altControlSettled = { forward: 0, backward: 0 };
const times = [[], [], []], counts = [0, 0], misses = { snap: 0, unreachable: 0 }, active = [0, 0];
for (let round = 0; round < 2; round++) for (let i = 0; i < extended.length; i++) {
const pair = extended[i], s = snapToGraph(graph, pair.origin), t = snapToGraph(graph, pair.destination);
if (!s || !t) { if (round === 0) misses.snap++; continue; }
const results = [];
for (const k of ((i + round) % 2 ? [1, 0] : [0, 1])) {
const st = process.hrtime.bigint(), result = k === 0 ? routeDirectionSplit(graph, s.node, t.node) : routeAstar(graph, s.node, t.node);
for (const k of ((i + round) % 2 ? [2, 1, 0] : [0, 1, 2])) {
const st = process.hrtime.bigint(), result = k === 0 ? routeDirectionSplit(graph, s.node, t.node) : k === 1 ? control(graph, s.node, t.node) : routeAstar(graph, s.node, t.node);
const ms = Number(process.hrtime.bigint() - st) / 1e6; results[k] = { result, ms };
}
if (results[0].result === null || results[1].result === null) {
if (results[0].result !== results[1].result) throw Error(`paired reachability mismatch ${pair.pairId}`);
if (results[0].result === null || results[1].result === null || results[2].result === null) {
if (results[0].result !== results[1].result || results[0].result !== results[2].result) throw Error(`paired reachability mismatch ${pair.pairId}`);
if (round === 0) misses.unreachable++; continue;
}
if (Math.abs(results[0].result.seconds - results[1].result.seconds) > 1e-6) throw Error(`paired cost mismatch ${pair.pairId}`);
for (let k = 0; k < 2; k++) times[k].push(results[k].ms);
if (Math.abs(results[0].result.seconds - results[1].result.seconds) > 1e-6 || Math.abs(results[1].result.seconds - results[2].result.seconds) > 1e-6) throw Error(`paired cost mismatch ${pair.pairId}`);
for (let k = 0; k < 3; k++) times[k].push(results[k].ms);
counts[0] += results[0].result.expansionsForward + results[0].result.expansionsBackward;
active[0] += results[0].result.activeTo; active[1] += results[0].result.activeFrom;
}
const stats = a => { a.sort((x, y) => x - y); return `n=${a.length} median=${a[Math.floor(a.length / 2)].toFixed(3)} mean=${(a.reduce((x,y) => x + y,0)/a.length).toFixed(3)} p95=${a[Math.ceil(a.length * .95)-1].toFixed(3)} ms`; };
console.log(`extended snap-miss=${misses.snap} unreachable=${misses.unreachable} (outside routed p95), 2 alternating rounds`);
console.log(`experimental: ${stats(times[0])} expansions=${counts[0]} active-to/from=${active.join('/')}`);
console.log(`shipped: ${stats(times[1])}; shipped expansions and expansion ratio NOT MEASURED (no production instrumentation)`);
counts[1] = globalThis.__altControlSettled.forward + globalThis.__altControlSettled.backward;
console.log(`shipped isolated instrumented clone: ${stats(times[1])} expansions=${counts[1]} forward/backward=${globalThis.__altControlSettled.forward}/${globalThis.__altControlSettled.backward}; experimental/control expansion ratio=${(counts[0]/counts[1]).toFixed(3)}`);
console.log(`original shipped routeAstar (paired unmodified haversine-only control): ${stats(times[2])}`);
}
}
}