Jev auto-settle (#19): extract.ts builds test cases from a project's past settles through a seat's own abso client (review --json, show <version>:<file>). Version 190 of shared· autosettle· Oct 14 (2026-10-14 22:35 UTC) · Not checked
Version 189 · Files at version 190
1 file, 138 lines added, 0 removed.
--- /dev/null
+++ b/scripts/autosettle/extract.ts
@@ -0,0 +1,138 @@
1 + // Build scored test cases from a project's past settles (#19), read through the 2 + // abso client in a seat folder joined to that project (a reader seat is enough). 4 + // node scripts/autosettle/extract.ts <seat folder> <cases.json> [--since 8d] 6 + // Each settled collision gives: the file before theirs (their version - 1, the 7 + // best guess at what both started from), theirs, and the settle's result. 8 + // The held agent's own lines are only recoverable from the result when it kept 9 + // yours or both; otherwise "yours" is null and score.ts skips the case. 11 + import { execFileSync } from "node:child_process"; 12 + import { writeFileSync } from "node:fs"; 14 + const [seat, out] = process.argv.slice(2); 16 + console.error("usage: node scripts/autosettle/extract.ts <seat folder> <cases.json> [--since 8d]"); 19 + const since = process.argv.includes("--since") ? process.argv[process.argv.indexOf("--since") + 1]! : "8d"; 20 + const abso = (...args: string[]): string => 21 + execFileSync("abso", args, { cwd: seat, encoding: "utf8", maxBuffer: 1 << 28, shell: process.platform === "win32" }); 23 + const fileAt = (version: number, file: string): string[] | null => { 25 + return abso("show", `${version}:${file}`).split(/\r?\n/); 33 + kept: "yours" | "theirs" | "both" | "rewrote"; 34 + theirs: { agent: string; version: number }; 38 + const review = JSON.parse(abso("review", "--since", since, "--json")) as { 44 + versions?: { version: number; message: string }[]; 47 + const messages = new Map<number, string>(); 48 + for (const c of review.changes) for (const v of c.versions ?? []) messages.set(v.version, v.message); 49 + const messageOf = (v: number): string | undefined => { 50 + if (!messages.has(v)) { 52 + const head = abso("show", String(v)).split(/\r?\n/).slice(0, 6).join("\n"); 53 + messages.set(v, /"([^"]*)"/.exec(head)?.[1] ?? head.split("\n")[0]!); 58 + return messages.get(v); 61 + /** The span where `a` and `b` differ, by common prefix and suffix. */ 62 + function hunk(a: string[], b: string[]): { start: number; aEnd: number; bEnd: number } { 64 + while (start < a.length && start < b.length && a[start] === b[start]) start++; 66 + while (tail < a.length - start && tail < b.length - start && a[a.length - 1 - tail] === b[b.length - 1 - tail]) 68 + return { start, aEnd: a.length - tail, bEnd: b.length - tail }; 71 + /** The lines of `r` between the context above and below a hunk of `t`, found by a few anchor lines. */ 72 + function regionIn(r: string[], t: string[], start: number, end: number): string[] | null { 73 + const above = t.slice(Math.max(0, start - 3), start); 74 + const below = t.slice(end, end + 3); 75 + const find = (needle: string[], from: number) => { 76 + if (needle.length === 0) return -1; 77 + for (let i = from; i + needle.length <= r.length; i++) if (needle.every((l, j) => r[i + j] === l)) return i; 80 + const a = find(above, 0); 81 + const from = above.length === 0 ? 0 : a < 0 ? -1 : a + above.length; 82 + if (from < 0) return null; 83 + const b = find(below, from); 84 + const to = below.length === 0 ? r.length : b < 0 ? -1 : b; 85 + return to < 0 ? null : r.slice(from, to); 89 + for (const c of review.changes) { 90 + for (const s of c.settled) { 91 + const before = fileAt(s.theirs.version - 1, s.file); 92 + const theirs = fileAt(s.theirs.version, s.file); 93 + const result = fileAt(s.version, s.file); 94 + const notes: string[] = []; 95 + let base: string[] | null = null; 96 + let theirsLines: string[] | null = null; 97 + let region: string[] | null = null; 98 + let context = { before: [] as string[], after: [] as string[] }; 99 + if (before && theirs && result) { 100 + const h = hunk(before, theirs); 101 + base = before.slice(h.start, h.aEnd); 102 + theirsLines = theirs.slice(h.start, h.bEnd); 103 + context = { before: theirs.slice(Math.max(0, h.start - 15), h.start), after: theirs.slice(h.bEnd, h.bEnd + 15) }; 104 + region = regionIn(result, theirs, h.start, h.bEnd); 105 + if (region === null) notes.push("couldn't find the region in the settled file"); 106 + if (theirsLines.length > 80) notes.push(`their change spans ${theirsLines.length} lines`); 107 + } else notes.push("a version of the file couldn't be read"); 108 + let yours: string[] | null = null; 109 + if (region && s.kept === "yours") yours = region; 110 + else if (region && theirsLines && s.kept === "both") yours = region.slice(0, region.length - theirsLines.length); 111 + else notes.push(`yours isn't recoverable from the result when the agent kept ${s.kept}`); 113 + id: `${s.version}-${s.file}`, 117 + agent: c.agent ?? c.author ?? "?", 118 + heldMessage: s.held, 119 + settleVersion: s.version, 120 + settleMessage: messageOf(s.version), 122 + theirs: { agent: s.theirs.agent, version: s.theirs.version, message: messageOf(s.theirs.version) }, 130 + notes: notes.join("; "), 134 + writeFileSync(out, JSON.stringify(cases, null, "\t")); 135 + const count = (k: string) => cases.filter((c) => c.kept === k).length; 137 + `${cases.length} settled collisions: yours ${count("yours")}, theirs ${count("theirs")}, both ${count("both")}, rewrote ${count("rewrote")}; ${cases.filter((c) => c.yours !== null).length} with both sides recoverable.`,