{"version":190,"agent":"autosettle","message":"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>).","time":1792017309129,"timeIso":"2026-10-14T22:35:09.129Z","changes":2,"change":3149,"files":["scripts/autosettle/extract.ts"],"draft":null,"diff":[{"path":"scripts/autosettle/extract.ts","added":138,"removed":0,"diff":"--- /dev/null\n+++ b/scripts/autosettle/extract.ts\n@@ -0,0 +1,138 @@\n+// Build scored test cases from a project's past settles (#19), read through the\n+// abso client in a seat folder joined to that project (a reader seat is enough).\n+//\n+//   node scripts/autosettle/extract.ts <seat folder> <cases.json> [--since 8d]\n+//\n+// Each settled collision gives: the file before theirs (their version - 1, the\n+// best guess at what both started from), theirs, and the settle's result.\n+// The held agent's own lines are only recoverable from the result when it kept\n+// yours or both; otherwise \"yours\" is null and score.ts skips the case.\n+\n+import { execFileSync } from \"node:child_process\";\n+import { writeFileSync } from \"node:fs\";\n+\n+const [seat, out] = process.argv.slice(2);\n+if (!seat || !out) {\n+\tconsole.error(\"usage: node scripts/autosettle/extract.ts <seat folder> <cases.json> [--since 8d]\");\n+\tprocess.exit(2);\n+}\n+const since = process.argv.includes(\"--since\") ? process.argv[process.argv.indexOf(\"--since\") + 1]! : \"8d\";\n+const abso = (...args: string[]): string =>\n+\texecFileSync(\"abso\", args, { cwd: seat, encoding: \"utf8\", maxBuffer: 1 << 28, shell: process.platform === \"win32\" });\n+\n+const fileAt = (version: number, file: string): string[] | null => {\n+\ttry {\n+\t\treturn abso(\"show\", `${version}:${file}`).split(/\\r?\\n/);\n+\t} catch {\n+\t\treturn null;\n+\t}\n+};\n+\n+interface Settled {\n+\tfile: string;\n+\tkept: \"yours\" | \"theirs\" | \"both\" | \"rewrote\";\n+\ttheirs: { agent: string; version: number };\n+\tversion: number;\n+\theld?: string;\n+}\n+const review = JSON.parse(abso(\"review\", \"--since\", since, \"--json\")) as {\n+\tchanges: {\n+\t\tagent?: string;\n+\t\tauthor?: string;\n+\t\tmessage?: string;\n+\t\tsettled: Settled[];\n+\t\tversions?: { version: number; message: string }[];\n+\t}[];\n+};\n+const messages = new Map<number, string>();\n+for (const c of review.changes) for (const v of c.versions ?? []) messages.set(v.version, v.message);\n+const messageOf = (v: number): string | undefined => {\n+\tif (!messages.has(v)) {\n+\t\ttry {\n+\t\t\tconst head = abso(\"show\", String(v)).split(/\\r?\\n/).slice(0, 6).join(\"\\n\");\n+\t\t\tmessages.set(v, /\"([^\"]*)\"/.exec(head)?.[1] ?? head.split(\"\\n\")[0]!);\n+\t\t} catch {\n+\t\t\treturn undefined;\n+\t\t}\n+\t}\n+\treturn messages.get(v);\n+};\n+\n+/** The span where `a` and `b` differ, by common prefix and suffix. */\n+function hunk(a: string[], b: string[]): { start: number; aEnd: number; bEnd: number } {\n+\tlet start = 0;\n+\twhile (start < a.length && start < b.length && a[start] === b[start]) start++;\n+\tlet tail = 0;\n+\twhile (tail < a.length - start && tail < b.length - start && a[a.length - 1 - tail] === b[b.length - 1 - tail])\n+\t\ttail++;\n+\treturn { start, aEnd: a.length - tail, bEnd: b.length - tail };\n+}\n+\n+/** The lines of `r` between the context above and below a hunk of `t`, found by a few anchor lines. */\n+function regionIn(r: string[], t: string[], start: number, end: number): string[] | null {\n+\tconst above = t.slice(Math.max(0, start - 3), start);\n+\tconst below = t.slice(end, end + 3);\n+\tconst find = (needle: string[], from: number) => {\n+\t\tif (needle.length === 0) return -1;\n+\t\tfor (let i = from; i + needle.length <= r.length; i++) if (needle.every((l, j) => r[i + j] === l)) return i;\n+\t\treturn -2;\n+\t};\n+\tconst a = find(above, 0);\n+\tconst from = above.length === 0 ? 0 : a < 0 ? -1 : a + above.length;\n+\tif (from < 0) return null;\n+\tconst b = find(below, from);\n+\tconst to = below.length === 0 ? r.length : b < 0 ? -1 : b;\n+\treturn to < 0 ? null : r.slice(from, to);\n+}\n+\n+const cases = [];\n+for (const c of review.changes) {\n+\tfor (const s of c.settled) {\n+\t\tconst before = fileAt(s.theirs.version - 1, s.file);\n+\t\tconst theirs = fileAt(s.theirs.version, s.file);\n+\t\tconst result = fileAt(s.version, s.file);\n+\t\tconst notes: string[] = [];\n+\t\tlet base: string[] | null = null;\n+\t\tlet theirsLines: string[] | null = null;\n+\t\tlet region: string[] | null = null;\n+\t\tlet context = { before: [] as string[], after: [] as string[] };\n+\t\tif (before && theirs && result) {\n+\t\t\tconst h = hunk(before, theirs);\n+\t\t\tbase = before.slice(h.start, h.aEnd);\n+\t\t\ttheirsLines = theirs.slice(h.start, h.bEnd);\n+\t\t\tcontext = { before: theirs.slice(Math.max(0, h.start - 15), h.start), after: theirs.slice(h.bEnd, h.bEnd + 15) };\n+\t\t\tregion = regionIn(result, theirs, h.start, h.bEnd);\n+\t\t\tif (region === null) notes.push(\"couldn't find the region in the settled file\");\n+\t\t\tif (theirsLines.length > 80) notes.push(`their change spans ${theirsLines.length} lines`);\n+\t\t} else notes.push(\"a version of the file couldn't be read\");\n+\t\tlet yours: string[] | null = null;\n+\t\tif (region && s.kept === \"yours\") yours = region;\n+\t\telse if (region && theirsLines && s.kept === \"both\") yours = region.slice(0, region.length - theirsLines.length);\n+\t\telse notes.push(`yours isn't recoverable from the result when the agent kept ${s.kept}`);\n+\t\tcases.push({\n+\t\t\tid: `${s.version}-${s.file}`,\n+\t\t\tfile: s.file,\n+\t\t\tkind: \"lines\",\n+\t\t\tsettler: {\n+\t\t\t\tagent: c.agent ?? c.author ?? \"?\",\n+\t\t\t\theldMessage: s.held,\n+\t\t\t\tsettleVersion: s.version,\n+\t\t\t\tsettleMessage: messageOf(s.version),\n+\t\t\t},\n+\t\t\ttheirs: { agent: s.theirs.agent, version: s.theirs.version, message: messageOf(s.theirs.version) },\n+\t\t\tbase,\n+\t\t\tyours,\n+\t\t\ttheirsLines,\n+\t\t\tcontext,\n+\t\t\tresult: region,\n+\t\t\tkept: s.kept,\n+\t\t\tissue: null,\n+\t\t\tnotes: notes.join(\"; \"),\n+\t\t});\n+\t}\n+}\n+writeFileSync(out, JSON.stringify(cases, null, \"\\t\"));\n+const count = (k: string) => cases.filter((c) => c.kept === k).length;\n+console.log(\n+\t`${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.`,\n+);"}],"ci":null}