{"version":195,"agent":"autosettle","message":"Auto-settle (#19): score it on project abso's own past settles. extract.ts now finds each collided region with git's line diff (before theirs, theirs, the settle's result) instead of one span over the whole file. rules.ts turns down a keep-both that holds one statement in two versions (an import from the same module with different names): on abso's history the model picked keep both there at 1.00, which wouldn't have compiled.","time":1792021015753,"timeIso":"2026-10-14T23:36:55.753Z","changes":4,"change":3295,"files":["scripts/autosettle/extract.ts","scripts/autosettle/rules.ts"],"draft":null,"diff":[{"path":"scripts/autosettle/extract.ts","added":122,"removed":64,"diff":"--- a/scripts/autosettle/extract.ts\n+++ b/scripts/autosettle/extract.ts\n@@ -3,13 +3,16 @@\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+// 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 { writeFileSync } from \"node:fs\";\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@@ -35,15 +38,14 @@\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+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@@ -58,59 +60,115 @@\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+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-/** 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+/** 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-for (const c of review.changes) {\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 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 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\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\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 (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\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}`,\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@@ -120,16 +178,16 @@\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\tbase: r?.base ?? null,\n \t\t\tyours,\n-\t\t\ttheirsLines,\n-\t\t\tcontext,\n-\t\t\tresult: region,\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});\n-\t}\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;"},{"path":"scripts/autosettle/rules.ts","added":11,"removed":1,"diff":"--- a/scripts/autosettle/rules.ts\n+++ b/scripts/autosettle/rules.ts\n@@ -31,5 +31,15 @@\n \tconst clash = solid(h.yours)\n \t\t.map(key)\n \t\t.find((k) => k && theirKeys.has(k));\n-\treturn clash ? `keeping both would set ${clash} twice` : undefined;\n+\tif (clash) return `keeping both would set ${clash} twice`;\n+\t// Two versions of one statement, such as an import from the same module with a different list of names:\n+\t// most of the names on one side are on the other too.\n+\tconst names = (lines: readonly string[]) => new Set(lines.join(\" \").match(/[A-Za-z_$][\\w$]{2,}/g) ?? []);\n+\tconst a = names(h.yours);\n+\tconst b = names(h.theirs);\n+\tconst shared = [...a].filter((n) => b.has(n)).length;\n+\tconst share = shared / Math.max(1, Math.min(a.size, b.size));\n+\treturn share >= 0.5\n+\t\t? `keeping both would repeat one statement in two versions (${shared} names on both sides)`\n+\t\t: undefined;\n }"}],"ci":null}