// Replay the operator-labelled inspections through several inspector settings and compare // accuracy, time and cost. Usage (on the server, in appliance-boot/): // node inspect-replay.mjs [board] [config names...] e.g. node inspect-replay.mjs som-stm32f7-v2a A B C // Results: replays/.json + a table on stdout. Every run is real API calls (costs money). import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'node:fs'; import { join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { analyze, cropToBoard, config } from './inspect-agent.mjs'; import { labels } from './inspect-knowledge.mjs'; const HERE = fileURLToPath(new URL('.', import.meta.url)); const CONFIGS = { A: { label: 'baseline: Opus medium, catalog photo, full frame', model: 'claude-opus-5-5', effort: 'medium', reference: 'catalog', crop: false, brief: false }, B: { label: 'Opus medium, golden, crop, brief', model: 'claude-opus-5-5', effort: 'medium', reference: 'golden', crop: true, brief: true }, C: { label: 'Opus low, golden, crop, brief', model: 'claude-opus-5-5', effort: 'low', reference: 'golden', crop: true, brief: true }, D: { label: 'Sonnet 5.5 low, golden, crop, brief', model: 'claude-sonnet-5-5', effort: 'low', reference: 'golden', crop: true, brief: true }, E: { label: 'Sonnet 5.5 medium, golden, crop, brief', model: 'claude-sonnet-5-5', effort: 'medium', reference: 'golden', crop: true, brief: true }, F: { label: 'Haiku 5.5 medium, golden, crop, brief', model: 'claude-haiku-5-5', effort: 'medium', reference: 'golden', crop: true, brief: true }, }; const PRICE = { 'claude-opus-5-5': [4, 20, 0.2], 'claude-sonnet-5-5': [2, 10, 0.2], 'claude-haiku-5-5': [0.1, 0.5, 0.01] }; // $/MTok in, out, cache read const board = process.argv[2] || 'som-stm32f7-v2a'; const names = process.argv.slice(3).length ? process.argv.slice(3) : ['B', 'C', 'D']; const truth = labels(board); const cases = [...truth].map(([id, v]) => ({ id, truth: v, dir: join(HERE, 'inspections', id.slice(0, 10)) })) .filter((c) => existsSync(join(c.dir, c.id + '.jpg'))); console.log(`${cases.length} labelled boards (${cases.filter((c) => c.truth === 'pass').length} good, ${cases.filter((c) => c.truth === 'fail').length} bad); configs ${names.join(' ')}`); // crop each photo once and reuse it for every config that crops const crops = new Map(); async function cropFor(c) { if (crops.has(c.id)) return crops.get(c.id); const p = join(c.dir, c.id + '-crop.jpg'); const r = existsSync(p) ? { jpeg: readFileSync(p), ms: 0 } : await cropToBoard(readFileSync(join(c.dir, c.id + '.jpg')), config({ crop: true })); if (!existsSync(p)) writeFileSync(p, r.jpeg); crops.set(c.id, r); return r; } const rows = []; for (const name of names) { const cfg = CONFIGS[name]; const res = []; for (const c of cases) { // one at a time: honest per-board latency const jpeg = readFileSync(join(c.dir, c.id + '.jpg')); const over = { ...cfg }; if (cfg.crop) over.cropped = (await cropFor(c)).jpeg; const r = await analyze(jpeg, board, over); const v = r.ok ? r.result.verdict : 'error'; const pr = PRICE[cfg.model] || [0, 0, 0]; const u = r.usage || {}; const cost = ((u.input || 0) * pr[0] + (u.cache_read || 0) * pr[2] + (u.cache_write || 0) * pr[0] * 1.25 + (u.output || 0) * pr[1]) / 1e6; res.push({ id: c.id, truth: c.truth, verdict: v, ms: r.timing?.model_ms || r.ms || 0, cost, usage: u, checks: r.result?.checks?.map((k) => k.status), error: r.error }); process.stdout.write(`${name} ${c.id.slice(11, 19)} truth=${c.truth} got=${v} ${((r.timing?.model_ms || 0) / 1000).toFixed(1)}s\n`); } const n = res.length || 1; const right = res.filter((x) => x.verdict === x.truth).length; rows.push({ name, label: cfg.label, n: res.length, right, falsePass: res.filter((x) => x.truth === 'fail' && x.verdict === 'pass').length, falseFail: res.filter((x) => x.truth === 'pass' && x.verdict !== 'pass').length, meanS: res.reduce((a, x) => a + x.ms, 0) / n / 1000, maxS: Math.max(0, ...res.map((x) => x.ms)) / 1000, cents: (res.reduce((a, x) => a + x.cost, 0) / n) * 100, res, }); } mkdirSync(join(HERE, 'replays'), { recursive: true }); const out = join(HERE, 'replays', new Date().toISOString().replace(/[:.]/g, '-') + '.json'); writeFileSync(out, JSON.stringify(rows, null, 1)); console.log('\nconfig correct missed-bad false-fail mean s max s cents/board settings'); for (const r of rows) console.log(`${r.name.padEnd(7)} ${String(r.right + '/' + r.n).padEnd(9)} ${String(r.falsePass).padEnd(11)} ${String(r.falseFail).padEnd(11)} ${r.meanS.toFixed(1).padEnd(7)} ${r.maxS.toFixed(1).padEnd(6)} ${r.cents.toFixed(1).padEnd(12)} ${r.label}`); console.log('saved', out);