← back to Sanderson Onboard
scripts/cadence_healthcheck.mjs
17 lines
// cadence_healthcheck.mjs — compute the last run's compliance pass-rate from create-audit.jsonl.
// Pass-rate = CREATED / (CREATED + HELD + ERR_CREATE) over the most recent run window (since last CAP/DONE marker).
// Exit 0 if >= threshold, exit 1 (halt) otherwise. Reads only.
import fs from 'node:fs';
const THRESHOLD = parseFloat((process.argv.find(a => a.startsWith('--threshold=')) || '').split('=')[1] || '0.85');
const AUDIT = new URL('../pilot/create-audit.jsonl', import.meta.url).pathname;
if (!fs.existsSync(AUDIT)) { console.log('no audit yet — pass'); process.exit(0); }
const lines = fs.readFileSync(AUDIT, 'utf8').trim().split('\n').filter(Boolean).map(l => { try { return JSON.parse(l); } catch { return null; } }).filter(Boolean);
// window = last ~250 events (a day's worth); good enough for the trend
const win = lines.slice(-300);
let created = 0, held = 0, err = 0;
for (const r of win) { if (r.action === 'CREATED') created++; else if (r.action === 'HELD') held++; else if (r.action === 'ERR_CREATE') err++; }
const denom = created + held + err;
const rate = denom ? created / denom : 1;
console.log(`healthcheck: created=${created} held=${held} err=${err} rate=${rate.toFixed(3)} threshold=${THRESHOLD}`);
process.exit(rate >= THRESHOLD ? 0 : 1);