{"draft":null,"from":4,"to":318,"files":[{"path":"scripts/autosettle/extract.ts","added":196,"removed":0,"diff":"--- /dev/null\n+++ b/scripts/autosettle/extract.ts\n@@ -0,0 +1,196 @@\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+// For each settle: the file before theirs (their version - 1, the best guess at\n+// what both started from), theirs, and the settle's result. git's line diff\n+// finds each of their hunks that the settler's change touched. The held agent's\n+// own lines are only recoverable from the result when it kept yours or both;\n+// otherwise \"yours\" is null and score.ts skips the case.\n+\n+import { execFileSync } from \"node:child_process\";\n+import { mkdtempSync, rmSync, writeFileSync } from \"node:fs\";\n+import { tmpdir } from \"node:os\";\n+import { join } from \"node:path\";\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+interface Change {\n+\tagent?: string;\n+\tauthor?: string;\n+\tmessage?: string;\n+\tsettled: Settled[];\n+\tversions?: { version: number; message: string }[];\n+}\n+const review = JSON.parse(abso(\"review\", \"--since\", since, \"--json\")) as { changes: Change[] };\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+interface Hunk {\n+\t/** 0-based start and length on each side; a length of 0 is an insertion before (or deletion at) start. */\n+\taStart: number;\n+\taLen: number;\n+\tbStart: number;\n+\tbLen: number;\n+}\n+\n+/** Line hunks from a to b, by git's diff. */\n+function hunks(a: string[], b: string[]): Hunk[] {\n+\tconst dir = mkdtempSync(join(tmpdir(), \"autosettle-\"));\n+\twriteFileSync(join(dir, \"a\"), a.join(\"\\n\"));\n+\twriteFileSync(join(dir, \"b\"), b.join(\"\\n\"));\n+\tlet text = \"\";\n+\ttry {\n+\t\ttext = execFileSync(\"git\", [\"diff\", \"--no-index\", \"-U0\", \"--no-color\", join(dir, \"a\"), join(dir, \"b\")], {\n+\t\t\tencoding: \"utf8\",\n+\t\t\tmaxBuffer: 1 << 28,\n+\t\t});\n+\t} catch (e) {\n+\t\t// git diff --no-index exits 1 when the files differ.\n+\t\ttext = (e as { stdout?: string }).stdout ?? \"\";\n+\t}\n+\trmSync(dir, { recursive: true, force: true });\n+\tconst list: Hunk[] = [];\n+\tfor (const m of text.matchAll(/^@@ -(\\d+)(?:,(\\d+))? \\+(\\d+)(?:,(\\d+))? @@/gm)) {\n+\t\tconst aLen = m[2] === undefined ? 1 : Number(m[2]);\n+\t\tconst bLen = m[4] === undefined ? 1 : Number(m[4]);\n+\t\tlist.push({\n+\t\t\taStart: aLen === 0 ? Number(m[1]) : Number(m[1]) - 1,\n+\t\t\taLen,\n+\t\t\tbStart: bLen === 0 ? Number(m[3]) : Number(m[3]) - 1,\n+\t\t\tbLen,\n+\t\t});\n+\t}\n+\treturn list;\n+}\n+\n+/** Where a line of b sits in a, given the hunks from a to b. */\n+const backTo = (hs: Hunk[], bAt: number) =>\n+\ths\n+\t\t.filter((h) => h.bStart + h.bLen <= bAt && !(h.bLen === 0 && h.bStart === bAt))\n+\t\t.reduce((n, h) => n - (h.bLen - h.aLen), bAt);\n+\n+/**\n+ * The collisions a settle went through in one file: each of their hunks (before to theirs) that the\n+ * settler's own change (theirs to result) touches, with the lines on every side.\n+ */\n+function regions(before: string[], theirs: string[], result: string[]) {\n+\tconst d1 = hunks(before, theirs);\n+\tconst d2 = hunks(theirs, result);\n+\tconst touches = (h: Hunk, g: Hunk) => g.aStart <= h.bStart + h.bLen && h.bStart <= g.aStart + g.aLen;\n+\tconst found = [];\n+\tfor (const h of d1) {\n+\t\tconst near = d2.filter((g) => touches(h, g));\n+\t\tif (near.length === 0) continue;\n+\t\tconst tStart = Math.min(h.bStart, ...near.map((g) => g.aStart));\n+\t\tconst tEnd = Math.max(h.bStart + h.bLen, ...near.map((g) => g.aStart + g.aLen));\n+\t\tconst rStart = d2\n+\t\t\t.filter((g) => g.aStart + g.aLen <= tStart && !near.includes(g))\n+\t\t\t.reduce((n, g) => n + (g.bLen - g.aLen), tStart);\n+\t\tconst rEnd = rStart + (tEnd - tStart) + near.reduce((n, g) => n + (g.bLen - g.aLen), 0);\n+\t\tconst bStart = backTo(d1, tStart);\n+\t\tconst bEnd = bStart + (tEnd - tStart) - (h.bLen - h.aLen);\n+\t\tfound.push({\n+\t\t\tbase: before.slice(bStart, bEnd),\n+\t\t\ttheirs: theirs.slice(tStart, tEnd),\n+\t\t\tresult: result.slice(rStart, rEnd),\n+\t\t\tcontext: { before: theirs.slice(Math.max(0, tStart - 15), tStart), after: theirs.slice(tEnd, tEnd + 15) },\n+\t\t});\n+\t}\n+\treturn found;\n+}\n+\n+const cases: { kept: string; yours: string[] | null }[] = [];\n+const groups = new Map<string, { change: Change; settled: Settled[] }>();\n+for (const c of review.changes)\n+\tfor (const s of c.settled) {\n+\t\tconst key = `${s.version} ${s.file}`;\n+\t\tconst g = groups.get(key) ?? { change: c, settled: [] };\n+\t\tg.settled.push(s);\n+\t\tgroups.set(key, g);\n+\t}\n+for (const { change: c, settled } of groups.values()) {\n+\tconst first = settled[0]!;\n+\tconst before = fileAt(first.theirs.version - 1, first.file);\n+\tconst theirs = fileAt(first.theirs.version, first.file);\n+\tconst result = fileAt(first.version, first.file);\n+\tconst found = before && theirs && result ? regions(before, theirs, result) : [];\n+\t// Marker blocks the settler changed, in file order; a block where it kept theirs left no trace to find.\n+\tconst changed = settled.filter((s) => s.kept !== \"theirs\");\n+\tsettled.forEach((s, i) => {\n+\t\tconst notes: string[] = [];\n+\t\tconst r = s.kept === \"theirs\" ? undefined : found[changed.indexOf(s)];\n+\t\tif (!before || !theirs || !result) notes.push(\"a version of the file couldn't be read\");\n+\t\telse if (s.kept === \"theirs\")\n+\t\t\tnotes.push(\"kept theirs: the region and yours aren't recoverable from versions alone\");\n+\t\telse if (!r) notes.push(`found ${found.length} changed regions for ${changed.length} settled blocks`);\n+\t\tif (r && found.length !== changed.length)\n+\t\t\tnotes.push(`found ${found.length} changed regions for ${changed.length} settled blocks: the pairing may be off`);\n+\t\tlet yours: string[] | null = null;\n+\t\tif (r && s.kept === \"yours\") yours = r.result;\n+\t\telse if (r && s.kept === \"both\") {\n+\t\t\tconst tail = r.result.slice(r.result.length - r.theirs.length);\n+\t\t\tif (tail.join(\"\\n\") === r.theirs.join(\"\\n\")) yours = r.result.slice(0, r.result.length - r.theirs.length);\n+\t\t\telse notes.push(\"kept both, but the result doesn't end with theirs\");\n+\t\t} else if (r) 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}#${i + 1}`,\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: r?.base ?? null,\n+\t\t\tyours,\n+\t\t\ttheirsLines: r?.theirs ?? null,\n+\t\t\tcontext: r?.context ?? { before: [], after: [] },\n+\t\t\tresult: r?.result ?? null,\n+\t\t\tkept: s.kept,\n+\t\t\tissue: null,\n+\t\t\tnotes: notes.join(\"; \"),\n+\t\t} as (typeof cases)[number]);\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+);"}],"unmatched":[]}