#!/usr/bin/env node // SPDX-License-Identifier: MIT // Generated by metaharness — the `wifi-densepose-sar-harness` CLI entry point. // // This is plain ESM JavaScript on purpose: it runs as-is via `npx wifi-densepose-sar-harness` // with NO build step. `npm run build` (tsc) is only needed if you extend the // TypeScript in src/. The published package ships this file directly (see the // "bin" + "files" fields in package.json), so `npx wifi-densepose-sar-harness` works the moment // `npm install` has resolved @metaharness/kernel + @metaharness/host-claude-code. import { loadKernel } from '@metaharness/kernel'; import adapter from '@metaharness/host-claude-code'; const HARNESS_NAME = 'wifi-densepose-sar-harness'; /** `wifi-densepose-sar-harness init` — boot the kernel + host adapter and report status. */ async function init() { const kernel = await loadKernel(); const info = kernel.kernelInfo(); console.log(`${HARNESS_NAME} — kernel ${info.version} (${kernel.backend})`); console.log(`Host adapter: ${adapter.name}`); console.log(`Run \`${HARNESS_NAME} doctor\` to verify the install.`); return 0; } /** * `wifi-densepose-sar-harness route ` — route a 4-axis * task embedding to the cost-optimal model tier via @metaharness/router. * Needs `npm run build` first (src/router.ts is TypeScript; this command * imports the compiled dist/ output so the base CLI stays build-free). */ async function route(args) { const embedding = args.map(Number); if (embedding.length !== 4 || embedding.some((n) => Number.isNaN(n))) { console.error('Usage: wifi-densepose-sar-harness route (four 0..1 numbers)'); return 2; } let routeSarQuery; try { ({ routeSarQuery } = await import('../dist/router.js')); } catch (err) { console.error(`route: dist/router.js not found — run \`npm run build\` first. (${err.message})`); return 1; } const pick = routeSarQuery(embedding); console.log(`route -> ${pick.id} (predicted quality ${pick.predictedQuality.toFixed(3)}, $${pick.costPerMTok}/MTok, met bar: ${pick.metBar})`); return 0; } /** * `wifi-densepose-sar-harness flywheel [generations]` — run the SYNTHETIC * @metaharness/flywheel demo (see src/flywheel.ts) and print the lift curve * + an independent replay-bundle verification. Needs `npm run build` first. */ async function flywheel(args) { const generations = args[0] ? Number(args[0]) : 3; if (Number.isNaN(generations) || generations < 1) { console.error('Usage: wifi-densepose-sar-harness flywheel [generations>=1]'); return 2; } let runSarFlywheelDemo, verifySarFlywheelDemo; try { ({ runSarFlywheelDemo, verifySarFlywheelDemo } = await import('../dist/flywheel.js')); } catch (err) { console.error(`flywheel: dist/flywheel.js not found — run \`npm run build\` first. (${err.message})`); return 1; } console.log(`Running ${generations}-generation flywheel demo (dataSource: SYNTHETIC — see src/flywheel.ts)...`); const result = await runSarFlywheelDemo(generations); for (const point of result.liftCurve) { console.log(` gen ${point.generation}: primary=${point.primary.toFixed(3)} delta=${point.delta.toFixed(3)} anchor=${point.anchor ?? 'n/a'}`); } const verdict = verifySarFlywheelDemo(result); console.log(`generations run: ${result.generationsRun} · promotions: ${result.promotions.length} · replay verified: ${verdict.pass}`); return verdict.pass ? 0 : 1; } /** `wifi-densepose-sar-harness doctor` — verify the install end-to-end (kernel + host resolve). */ async function doctor() { const kernel = await loadKernel(); const info = kernel.kernelInfo(); const checks = [ ['kernel loads', !!kernel], ['kernel reports a version', typeof info.version === 'string' && info.version.length > 0], ['kernel backend is native|wasm|js', ['native', 'wasm', 'js'].includes(kernel.backend)], ['host adapter has a name', typeof adapter?.name === 'string' && adapter.name.length > 0], ]; let ok = true; for (const [label, pass] of checks) { console.log(`${pass ? 'PASS' : 'FAIL'} ${label}`); if (!pass) ok = false; } console.log( ok ? `\n${HARNESS_NAME}: all checks passed (kernel ${info.version}, ${kernel.backend} backend, host ${adapter.name})` : `\n${HARNESS_NAME}: doctor found problems`, ); return ok ? 0 : 1; } /** * Dispatch one CLI invocation. Exported (not just run on import) so a test can * drive it without spawning a subprocess. Returns the intended exit code. */ export async function run(argv) { const cmd = argv[0] ?? 'init'; switch (cmd) { case 'init': return init(); case 'doctor': return doctor(); case 'route': return route(argv.slice(1)); case 'flywheel': return flywheel(argv.slice(1)); case '--version': case '-v': { const kernel = await loadKernel(); console.log(kernel.version()); return 0; } case '--help': case '-h': console.log(`Usage: ${HARNESS_NAME} \n\n init boot the kernel + host adapter (default)\n doctor verify the install end-to-end\n route route a 4-axis task embedding to a cost-optimal model tier (needs \`npm run build\`)\n flywheel run the SYNTHETIC self-improvement demo loop (needs \`npm run build\`)\n --version print the kernel version`); return 0; default: console.error(`Unknown command: ${cmd}. Try \`${HARNESS_NAME} --help\`.`); return 2; } } // CLI guard: execute only when invoked directly (not when imported by a test). // npm's bin shims pass a NON-normalized argv[1] (e.g. ".../.bin/..//bin/cli.js" // on Windows) and may differ in case, so realpath BOTH sides before comparing — // a naive string === misses the npx/shim path and the CLI silently no-ops. import { fileURLToPath } from 'node:url'; import { realpathSync } from 'node:fs'; import { argv } from 'node:process'; const invokedDirectly = (() => { if (!argv[1]) return false; try { const a = realpathSync(argv[1]); const b = realpathSync(fileURLToPath(import.meta.url)); return process.platform === 'win32' ? a.toLowerCase() === b.toLowerCase() : a === b; } catch { return false; } })(); if (invokedDirectly) { run(argv.slice(2)) .then((code) => process.exit(code)) .catch((err) => { console.error(err); process.exit(1); }); }