|
| 1 | +// Aggregate scan over the intervention logs written by src/subagent/intervention-log.ts |
| 2 | +// (~/.corbits/projects/**/interventions.jsonl) — the data that has to exist |
| 3 | +// before any stop/nudge threshold is changed again (CL-6938). |
| 4 | +// |
| 5 | +// Reports, per intervention id: how often it fired, split by model family, with |
| 6 | +// the measured value distribution beside the threshold it crossed, and two |
| 7 | +// context columns. These are NOT a measured false-positive rate — a stop on a |
| 8 | +// run that had already edited files, or one that fired with turn budget still |
| 9 | +// left, is equally consistent with a correct stop or a wrong one: |
| 10 | +// |
| 11 | +// edited — stops that fired on a run which had already edited files. |
| 12 | +// early — stops that fired before half the turn budget was spent. |
| 13 | +// |
| 14 | +// Also aggregates outcome records (CL-6938): what each completed dispatch |
| 15 | +// actually produced (a salvage kind, or clean-complete), by kind. This is the |
| 16 | +// log's only outcome signal, letting a stop record be read alongside what the |
| 17 | +// dispatch it touched actually produced — it is still not gate-pass or |
| 18 | +// retry-success tracking. |
| 19 | +// |
| 20 | +// Run: bun run scripts/intervention-forensics.ts |
| 21 | +// |
| 22 | +// Prints only aggregate counts and the `detail` field's first token, never turn |
| 23 | +// content, so it is safe to run without pulling trace data into a context window. |
| 24 | + |
| 25 | +import { readdirSync, lstatSync, readFileSync } from "node:fs"; |
| 26 | +import { join } from "node:path"; |
| 27 | +import { homedir } from "node:os"; |
| 28 | + |
| 29 | +import { INTERVENTION_FILE, type InterventionRecord } from "../src/subagent/intervention-log.js"; |
| 30 | + |
| 31 | +// lstat, and skip symlinks: session dirs carry a `latest` symlink to a real |
| 32 | +// session, and following it double-counts every record in that session. |
| 33 | +function findAll(dir: string, name: string, out: string[]): void { |
| 34 | + let entries: string[]; |
| 35 | + try { |
| 36 | + entries = readdirSync(dir); |
| 37 | + } catch { |
| 38 | + return; |
| 39 | + } |
| 40 | + for (const entry of entries) { |
| 41 | + const path = join(dir, entry); |
| 42 | + let info: ReturnType<typeof lstatSync>; |
| 43 | + try { |
| 44 | + info = lstatSync(path); |
| 45 | + } catch { |
| 46 | + continue; |
| 47 | + } |
| 48 | + if (info.isSymbolicLink()) continue; |
| 49 | + if (info.isDirectory()) findAll(path, name, out); |
| 50 | + else if (entry === name) out.push(path); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +function percentile(sorted: readonly number[], p: number): number { |
| 55 | + if (sorted.length === 0) return 0; |
| 56 | + const index = Math.min(sorted.length - 1, Math.floor((p / 100) * sorted.length)); |
| 57 | + return sorted[index]!; |
| 58 | +} |
| 59 | + |
| 60 | +interface Bucket { |
| 61 | + count: number; |
| 62 | + byFamily: Map<string, number>; |
| 63 | + values: number[]; |
| 64 | + thresholds: Set<number>; |
| 65 | + editedWork: number; |
| 66 | + earlyBudget: number; |
| 67 | +} |
| 68 | + |
| 69 | +function emptyBucket(): Bucket { |
| 70 | + return { |
| 71 | + count: 0, |
| 72 | + byFamily: new Map(), |
| 73 | + values: [], |
| 74 | + thresholds: new Set(), |
| 75 | + editedWork: 0, |
| 76 | + earlyBudget: 0, |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +const root = join(homedir(), ".corbits", "projects"); |
| 81 | +const files: string[] = []; |
| 82 | +findAll(root, INTERVENTION_FILE, files); |
| 83 | + |
| 84 | +const buckets = new Map<string, Bucket>(); |
| 85 | +const outcomes = new Map<string, number>(); |
| 86 | +let records = 0; |
| 87 | +let malformed = 0; |
| 88 | + |
| 89 | +for (const file of files) { |
| 90 | + let lines: string[]; |
| 91 | + try { |
| 92 | + lines = readFileSync(file, "utf8").split("\n"); |
| 93 | + } catch { |
| 94 | + continue; |
| 95 | + } |
| 96 | + for (const line of lines) { |
| 97 | + if (line.trim().length === 0) continue; |
| 98 | + let record: InterventionRecord; |
| 99 | + try { |
| 100 | + record = JSON.parse(line) as InterventionRecord; |
| 101 | + } catch { |
| 102 | + malformed++; |
| 103 | + continue; |
| 104 | + } |
| 105 | + if (typeof record.id !== "string") { |
| 106 | + malformed++; |
| 107 | + continue; |
| 108 | + } |
| 109 | + records++; |
| 110 | + if (record.class === "outcome" && record.outcome !== undefined) { |
| 111 | + const kind = record.outcome.kind; |
| 112 | + outcomes.set(kind, (outcomes.get(kind) ?? 0) + 1); |
| 113 | + continue; |
| 114 | + } |
| 115 | + const key = `${record.class ?? "?"}/${record.id}`; |
| 116 | + let bucket = buckets.get(key); |
| 117 | + if (bucket === undefined) { |
| 118 | + bucket = emptyBucket(); |
| 119 | + buckets.set(key, bucket); |
| 120 | + } |
| 121 | + bucket.count++; |
| 122 | + const family = record.family ?? record.model ?? "unknown"; |
| 123 | + bucket.byFamily.set(family, (bucket.byFamily.get(family) ?? 0) + 1); |
| 124 | + if (record.measurement !== undefined) { |
| 125 | + bucket.values.push(record.measurement.value); |
| 126 | + if (record.measurement.threshold !== undefined) { |
| 127 | + bucket.thresholds.add(record.measurement.threshold); |
| 128 | + } |
| 129 | + } |
| 130 | + const state = record.state; |
| 131 | + if (record.class === "stop" && state !== undefined) { |
| 132 | + if ((state.editedPaths ?? 0) > 0) bucket.editedWork++; |
| 133 | + const turns = state.turnsCompleted ?? 0; |
| 134 | + const max = state.maxTurns ?? 0; |
| 135 | + if (max > 0 && turns < max / 2) bucket.earlyBudget++; |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +console.log(`intervention logs: ${files.length}`); |
| 141 | +console.log(`records: ${records}${malformed > 0 ? ` (${malformed} malformed, skipped)` : ""}`); |
| 142 | +if (records === 0) { |
| 143 | + console.log("\nNo interventions logged yet. Run some sessions first."); |
| 144 | + process.exit(0); |
| 145 | +} |
| 146 | + |
| 147 | +const rows = [...buckets.entries()].sort((a, b) => b[1].count - a[1].count); |
| 148 | +console.log( |
| 149 | + "\nintervention n value p50/p90/max threshold edited early", |
| 150 | +); |
| 151 | +for (const [key, bucket] of rows) { |
| 152 | + const sorted = [...bucket.values].sort((a, b) => a - b); |
| 153 | + const dist = |
| 154 | + sorted.length === 0 |
| 155 | + ? "-" |
| 156 | + : `${percentile(sorted, 50)}/${percentile(sorted, 90)}/${sorted[sorted.length - 1]!}`; |
| 157 | + const thresholds = bucket.thresholds.size === 0 ? "-" : [...bucket.thresholds].join(","); |
| 158 | + console.log( |
| 159 | + `${key.padEnd(33)} ${String(bucket.count).padStart(3)} ${dist.padEnd(16)} ${thresholds.padEnd(10)} ${String(bucket.editedWork).padStart(5)} ${String(bucket.earlyBudget).padStart(5)}`, |
| 160 | + ); |
| 161 | +} |
| 162 | + |
| 163 | +console.log("\nby family"); |
| 164 | +for (const [key, bucket] of rows) { |
| 165 | + const families = [...bucket.byFamily.entries()] |
| 166 | + .sort((a, b) => b[1] - a[1]) |
| 167 | + .map(([family, count]) => `${family}=${count}`) |
| 168 | + .join(" "); |
| 169 | + console.log(`${key.padEnd(33)} ${families}`); |
| 170 | +} |
| 171 | + |
| 172 | +console.log( |
| 173 | + "\nedited = stops on runs that had already edited files; early = stops before half the turn budget (context, not a false-positive rate).", |
| 174 | +); |
| 175 | + |
| 176 | +if (outcomes.size > 0) { |
| 177 | + console.log("\ndispatch outcomes"); |
| 178 | + const outcomeRows = [...outcomes.entries()].sort((a, b) => b[1] - a[1]); |
| 179 | + for (const [kind, count] of outcomeRows) { |
| 180 | + console.log(`${kind.padEnd(20)} ${count}`); |
| 181 | + } |
| 182 | +} |
0 commit comments