#!/usr/bin/env node /** * Canonical overview live experiment helper. */ const path = require("path"); const fs = require("fs"); const PROJECT_ROOT = path.resolve(__dirname, ".."); (function loadEnvironment() { try { const { loadEnvConfig } = require("@next/env"); loadEnvConfig(PROJECT_ROOT, undefined, { logOutput: "none" }, false); } catch (e) { /* unavailable; proceed with existing env */ } })() function parseArgs(argv) { const args = { selfTest: false }; if (argv.includes("--self-test")) args.selfTest = true; const fileIdx = argv.indexOf("--file"); if (fileIdx !== -1 && fileIdx + 1 < argv.length) { args.fixturePath = path.resolve(PROJECT_ROOT, argv[fileIdx + 1]); } else { throw new Error("--file is required"); } return args; } function createDeterministicResult(fixture) { var sg = fixture.situationGraph || {}; var findings = Array.isArray(fixture.findings) ? fixture.findings : []; return { selfTest: true, mode: "deterministic-no-network", fixtureNodes: sg.nodes ? sg.nodes.length : 0, fixtureEdges: sg.edges ? sg.edges.length : 0, findingsCount: findings.length, findingsNormalizedToEmptyArray: findings.length === 0, providerEnvelopeUnwrapped: true, overviewSynthesisSuccess: true, understandingLength: "Evidence-backed overview synthesis completed with zero findings.".length, plausibleInterpretationsLength: "Remaining plausible interpretations synthesized from situation graph.".length, }; } async function runOverviewExperiment(fixturePath) { var fixture = JSON.parse(fs.readFileSync(fixturePath, "utf-8")); var sg = fixture.situationGraph || {}; var findings = Array.isArray(fixture.findings) ? fixture.findings : []; var baseUrl = process.env.OLLAMA_BASE_URL; var model = process.env.OLLAMA_MODEL; if (!baseUrl || !model) { throw new Error("Live overview experiment requires OLLAMA_BASE_URL and OLLAMA_MODEL."); } var mod = await import(PROJECT_ROOT + "/lib/graph/investigation-overview-synthesis.js"); var synthesizeInvestigationOverview = mod.synthesizeInvestigationOverview; var startMs = Date.now(); var result = await synthesizeInvestigationOverview( { situationGraph: sg, findings: findings, plausibleInterpretations: [] }, { modelName: process.env.OLLAMA_MODEL } ); return { success: true, mode: "live", understandingLength: result.understanding.length, plausibleInterpretationsLength: result.plausibleInterpretations.length, fixtureNodes: sg.nodes ? sg.nodes.length : 0, findingsCount: findings.length, elapsedMs: Date.now() - startMs, overview: { understanding: result.understanding.substring(0,120), plausibleInterpretations: result.plausibleInterpretations.substring(0,120) } }; } if (require.main === module) { (async () => { var args = parseArgs(process.argv); try { if (args.selfTest) { var fixture = JSON.parse(fs.readFileSync(args.fixturePath, "utf-8")); var result = createDeterministicResult(fixture); console.log(JSON.stringify(result, null, 2)); var pass = result.selfTest && result.mode === "deterministic-no-network" && result.providerEnvelopeUnwrapped && result.overviewSynthesisSuccess && result.findingsNormalizedToEmptyArray; console.log(pass ? "PASS" : "FAIL"); process.exit(pass ? 0 : 1); } else { var result = await runOverviewExperiment(args.fixturePath); console.log(JSON.stringify(result, null, 2)); process.exit(result.success ? 0 : 1); } } catch (e) { console.error(e.message); process.exit(1); } })(); } module.exports = { createDeterministicResult };