|
| 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 | +// false-positive proxies: |
| 8 | +// |
| 9 | +// editedWork — stops that fired on a run which had already edited files. |
| 10 | +// A stop claiming nothing shipped, on a run that shipped. |
| 11 | +// earlyBudget — stops that fired before half the turn budget was spent. |
| 12 | +// A run declared stuck while most of its budget was unused. |
| 13 | +// |
| 14 | +// Neither proxy is proof on its own; both are cheap and directional, which is |
| 15 | +// what the tuning history has been missing. |
| 16 | +// |
| 17 | +// Run: bun run scripts/intervention-forensics.ts |
| 18 | +// |
| 19 | +// Prints only aggregate counts and the `detail` field's first token, never turn |
| 20 | +// content, so it is safe to run without pulling trace data into a context window. |
| 21 | + |
| 22 | +import { readdirSync, lstatSync, readFileSync } from "node:fs"; |
| 23 | +import { join } from "node:path"; |
| 24 | +import { homedir } from "node:os"; |
| 25 | + |
| 26 | +import { INTERVENTION_FILE, type InterventionRecord } from "../src/subagent/intervention-log.js"; |
| 27 | + |
| 28 | +// lstat, and skip symlinks: session dirs carry a `latest` symlink to a real |
| 29 | +// session, and following it double-counts every record in that session. |
| 30 | +function findAll(dir: string, name: string, out: string[]): void { |
| 31 | + let entries: string[]; |
| 32 | + try { |
| 33 | + entries = readdirSync(dir); |
| 34 | + } catch { |
| 35 | + return; |
| 36 | + } |
| 37 | + for (const entry of entries) { |
| 38 | + const path = join(dir, entry); |
| 39 | + let info: ReturnType<typeof lstatSync>; |
| 40 | + try { |
| 41 | + info = lstatSync(path); |
| 42 | + } catch { |
| 43 | + continue; |
| 44 | + } |
| 45 | + if (info.isSymbolicLink()) continue; |
| 46 | + if (info.isDirectory()) findAll(path, name, out); |
| 47 | + else if (entry === name) out.push(path); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +function percentile(sorted: readonly number[], p: number): number { |
| 52 | + if (sorted.length === 0) return 0; |
| 53 | + const index = Math.min(sorted.length - 1, Math.floor((p / 100) * sorted.length)); |
| 54 | + return sorted[index]!; |
| 55 | +} |
| 56 | + |
| 57 | +interface Bucket { |
| 58 | + count: number; |
| 59 | + byFamily: Map<string, number>; |
| 60 | + values: number[]; |
| 61 | + thresholds: Set<number>; |
| 62 | + editedWork: number; |
| 63 | + earlyBudget: number; |
| 64 | +} |
| 65 | + |
| 66 | +function emptyBucket(): Bucket { |
| 67 | + return { |
| 68 | + count: 0, |
| 69 | + byFamily: new Map(), |
| 70 | + values: [], |
| 71 | + thresholds: new Set(), |
| 72 | + editedWork: 0, |
| 73 | + earlyBudget: 0, |
| 74 | + }; |
| 75 | +} |
| 76 | + |
| 77 | +const root = join(homedir(), ".corbits", "projects"); |
| 78 | +const files: string[] = []; |
| 79 | +findAll(root, INTERVENTION_FILE, files); |
| 80 | + |
| 81 | +const buckets = new Map<string, Bucket>(); |
| 82 | +let records = 0; |
| 83 | +let malformed = 0; |
| 84 | + |
| 85 | +for (const file of files) { |
| 86 | + let lines: string[]; |
| 87 | + try { |
| 88 | + lines = readFileSync(file, "utf8").split("\n"); |
| 89 | + } catch { |
| 90 | + continue; |
| 91 | + } |
| 92 | + for (const line of lines) { |
| 93 | + if (line.trim().length === 0) continue; |
| 94 | + let record: InterventionRecord; |
| 95 | + try { |
| 96 | + record = JSON.parse(line) as InterventionRecord; |
| 97 | + } catch { |
| 98 | + malformed++; |
| 99 | + continue; |
| 100 | + } |
| 101 | + if (typeof record.id !== "string") { |
| 102 | + malformed++; |
| 103 | + continue; |
| 104 | + } |
| 105 | + records++; |
| 106 | + const key = `${record.class ?? "?"}/${record.id}`; |
| 107 | + let bucket = buckets.get(key); |
| 108 | + if (bucket === undefined) { |
| 109 | + bucket = emptyBucket(); |
| 110 | + buckets.set(key, bucket); |
| 111 | + } |
| 112 | + bucket.count++; |
| 113 | + const family = record.family ?? record.model ?? "unknown"; |
| 114 | + bucket.byFamily.set(family, (bucket.byFamily.get(family) ?? 0) + 1); |
| 115 | + if (record.measurement !== undefined) { |
| 116 | + bucket.values.push(record.measurement.value); |
| 117 | + if (record.measurement.threshold !== undefined) { |
| 118 | + bucket.thresholds.add(record.measurement.threshold); |
| 119 | + } |
| 120 | + } |
| 121 | + const state = record.state; |
| 122 | + if (record.class === "stop" && state !== undefined) { |
| 123 | + if ((state.editedPaths ?? 0) > 0) bucket.editedWork++; |
| 124 | + const turns = state.turnsCompleted ?? 0; |
| 125 | + const max = state.maxTurns ?? 0; |
| 126 | + if (max > 0 && turns < max / 2) bucket.earlyBudget++; |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +console.log(`intervention logs: ${files.length}`); |
| 132 | +console.log(`records: ${records}${malformed > 0 ? ` (${malformed} malformed, skipped)` : ""}`); |
| 133 | +if (records === 0) { |
| 134 | + console.log("\nNo interventions logged yet. Run some sessions first."); |
| 135 | + process.exit(0); |
| 136 | +} |
| 137 | + |
| 138 | +const rows = [...buckets.entries()].sort((a, b) => b[1].count - a[1].count); |
| 139 | +console.log( |
| 140 | + "\nintervention n value p50/p90/max threshold edited early", |
| 141 | +); |
| 142 | +for (const [key, bucket] of rows) { |
| 143 | + const sorted = [...bucket.values].sort((a, b) => a - b); |
| 144 | + const dist = |
| 145 | + sorted.length === 0 |
| 146 | + ? "-" |
| 147 | + : `${percentile(sorted, 50)}/${percentile(sorted, 90)}/${sorted[sorted.length - 1]!}`; |
| 148 | + const thresholds = bucket.thresholds.size === 0 ? "-" : [...bucket.thresholds].join(","); |
| 149 | + console.log( |
| 150 | + `${key.padEnd(33)} ${String(bucket.count).padStart(3)} ${dist.padEnd(16)} ${thresholds.padEnd(10)} ${String(bucket.editedWork).padStart(5)} ${String(bucket.earlyBudget).padStart(5)}`, |
| 151 | + ); |
| 152 | +} |
| 153 | + |
| 154 | +console.log("\nby family"); |
| 155 | +for (const [key, bucket] of rows) { |
| 156 | + const families = [...bucket.byFamily.entries()] |
| 157 | + .sort((a, b) => b[1] - a[1]) |
| 158 | + .map(([family, count]) => `${family}=${count}`) |
| 159 | + .join(" "); |
| 160 | + console.log(`${key.padEnd(33)} ${families}`); |
| 161 | +} |
| 162 | + |
| 163 | +console.log( |
| 164 | + "\nedited = stops on runs that had already edited files; early = stops before half the turn budget.", |
| 165 | +); |
0 commit comments