- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/scripts/autosettle/extract.ts?format=json
196 linesautosettle 100%Last change autosettle in version 195, 3 versions changed it
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).
3//
4// node scripts/autosettle/extract.ts <seat folder> <cases.json> [--since 8d]
5//
6// For each settle: the file before theirs (their version - 1, the best guess at
7// what both started from), theirs, and the settle's result. git's line diff
8// finds each of their hunks that the settler's change touched. The held agent's
9// own lines are only recoverable from the result when it kept yours or both;
10// otherwise "yours" is null and score.ts skips the case.
12import { execFileSync } from "node:child_process";
13import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
14import { tmpdir } from "node:os";
15import { join } from "node:path";
17const [seat, out] = process.argv.slice(2);
18if (!seat || !out) {
19 console.error("usage: node scripts/autosettle/extract.ts <seat folder> <cases.json> [--since 8d]");
20 process.exit(2);
21}
22const since = process.argv.includes("--since") ? process.argv[process.argv.indexOf("--since") + 1]! : "8d";
23const abso = (...args: string[]): string =>
24 execFileSync("abso", args, { cwd: seat, encoding: "utf8", maxBuffer: 1 << 28, shell: process.platform === "win32" });
26const fileAt = (version: number, file: string): string[] | null => {
27 try {
28 return abso("show", `${version}:${file}`).split(/\r?\n/);
29 } catch {
30 return null;
31 }
32};
34interface Settled {
35 file: string;
36 kept: "yours" | "theirs" | "both" | "rewrote";
37 theirs: { agent: string; version: number };
38 version: number;
39 held?: string;
40}
41interface Change {
42 agent?: string;
43 author?: string;
44 message?: string;
45 settled: Settled[];
46 versions?: { version: number; message: string }[];
47}
48const review = JSON.parse(abso("review", "--since", since, "--json")) as { changes: Change[] };
49const messages = new Map<number, string>();
50for (const c of review.changes) for (const v of c.versions ?? []) messages.set(v.version, v.message);
51const messageOf = (v: number): string | undefined => {
52 if (!messages.has(v)) {
53 try {
54 const head = abso("show", String(v)).split(/\r?\n/).slice(0, 6).join("\n");
55 messages.set(v, /"([^"]*)"/.exec(head)?.[1] ?? head.split("\n")[0]!);
56 } catch {
57 return undefined;
58 }
59 }
60 return messages.get(v);
61};
63interface Hunk {
64 /** 0-based start and length on each side; a length of 0 is an insertion before (or deletion at) start. */
65 aStart: number;
66 aLen: number;
67 bStart: number;
68 bLen: number;
69}
71/** Line hunks from a to b, by git's diff. */
72function hunks(a: string[], b: string[]): Hunk[] {
73 const dir = mkdtempSync(join(tmpdir(), "autosettle-"));
74 writeFileSync(join(dir, "a"), a.join("\n"));
75 writeFileSync(join(dir, "b"), b.join("\n"));
76 let text = "";
77 try {
78 text = execFileSync("git", ["diff", "--no-index", "-U0", "--no-color", join(dir, "a"), join(dir, "b")], {
79 encoding: "utf8",
80 maxBuffer: 1 << 28,
81 });
82 } catch (e) {
83 // git diff --no-index exits 1 when the files differ.
84 text = (e as { stdout?: string }).stdout ?? "";
85 }
86 rmSync(dir, { recursive: true, force: true });
87 const list: Hunk[] = [];
88 for (const m of text.matchAll(/^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/gm)) {
89 const aLen = m[2] === undefined ? 1 : Number(m[2]);
90 const bLen = m[4] === undefined ? 1 : Number(m[4]);
91 list.push({
92 aStart: aLen === 0 ? Number(m[1]) : Number(m[1]) - 1,
93 aLen,
94 bStart: bLen === 0 ? Number(m[3]) : Number(m[3]) - 1,
95 bLen,
96 });
97 }
98 return list;
99}
101/** Where a line of b sits in a, given the hunks from a to b. */
102const backTo = (hs: Hunk[], bAt: number) =>
103 hs
104 .filter((h) => h.bStart + h.bLen <= bAt && !(h.bLen === 0 && h.bStart === bAt))
105 .reduce((n, h) => n - (h.bLen - h.aLen), bAt);
107/**
108 * The collisions a settle went through in one file: each of their hunks (before to theirs) that the
109 * settler's own change (theirs to result) touches, with the lines on every side.
110 */
111function regions(before: string[], theirs: string[], result: string[]) {
112 const d1 = hunks(before, theirs);
113 const d2 = hunks(theirs, result);
114 const touches = (h: Hunk, g: Hunk) => g.aStart <= h.bStart + h.bLen && h.bStart <= g.aStart + g.aLen;
115 const found = [];
116 for (const h of d1) {
117 const near = d2.filter((g) => touches(h, g));
118 if (near.length === 0) continue;
119 const tStart = Math.min(h.bStart, ...near.map((g) => g.aStart));
120 const tEnd = Math.max(h.bStart + h.bLen, ...near.map((g) => g.aStart + g.aLen));
121 const rStart = d2
122 .filter((g) => g.aStart + g.aLen <= tStart && !near.includes(g))
123 .reduce((n, g) => n + (g.bLen - g.aLen), tStart);
124 const rEnd = rStart + (tEnd - tStart) + near.reduce((n, g) => n + (g.bLen - g.aLen), 0);
125 const bStart = backTo(d1, tStart);
126 const bEnd = bStart + (tEnd - tStart) - (h.bLen - h.aLen);
127 found.push({
128 base: before.slice(bStart, bEnd),
129 theirs: theirs.slice(tStart, tEnd),
130 result: result.slice(rStart, rEnd),
131 context: { before: theirs.slice(Math.max(0, tStart - 15), tStart), after: theirs.slice(tEnd, tEnd + 15) },
132 });
133 }
134 return found;
135}
137const cases: { kept: string; yours: string[] | null }[] = [];
138const groups = new Map<string, { change: Change; settled: Settled[] }>();
139for (const c of review.changes)
140 for (const s of c.settled) {
141 const key = `${s.version} ${s.file}`;
142 const g = groups.get(key) ?? { change: c, settled: [] };
143 g.settled.push(s);
144 groups.set(key, g);
145 }
146for (const { change: c, settled } of groups.values()) {
147 const first = settled[0]!;
148 const before = fileAt(first.theirs.version - 1, first.file);
149 const theirs = fileAt(first.theirs.version, first.file);
150 const result = fileAt(first.version, first.file);
151 const found = before && theirs && result ? regions(before, theirs, result) : [];
152 // Marker blocks the settler changed, in file order; a block where it kept theirs left no trace to find.
153 const changed = settled.filter((s) => s.kept !== "theirs");
154 settled.forEach((s, i) => {
155 const notes: string[] = [];
156 const r = s.kept === "theirs" ? undefined : found[changed.indexOf(s)];
157 if (!before || !theirs || !result) notes.push("a version of the file couldn't be read");
158 else if (s.kept === "theirs")
159 notes.push("kept theirs: the region and yours aren't recoverable from versions alone");
160 else if (!r) notes.push(`found ${found.length} changed regions for ${changed.length} settled blocks`);
161 if (r && found.length !== changed.length)
162 notes.push(`found ${found.length} changed regions for ${changed.length} settled blocks: the pairing may be off`);
163 let yours: string[] | null = null;
164 if (r && s.kept === "yours") yours = r.result;
165 else if (r && s.kept === "both") {
166 const tail = r.result.slice(r.result.length - r.theirs.length);
167 if (tail.join("\n") === r.theirs.join("\n")) yours = r.result.slice(0, r.result.length - r.theirs.length);
168 else notes.push("kept both, but the result doesn't end with theirs");
169 } else if (r) notes.push(`yours isn't recoverable from the result when the agent kept ${s.kept}`);
170 cases.push({
171 id: `${s.version}-${s.file}#${i + 1}`,
172 file: s.file,
173 kind: "lines",
174 settler: {
175 agent: c.agent ?? c.author ?? "?",
176 heldMessage: s.held,
177 settleVersion: s.version,
178 settleMessage: messageOf(s.version),
179 },
180 theirs: { agent: s.theirs.agent, version: s.theirs.version, message: messageOf(s.theirs.version) },
181 base: r?.base ?? null,
182 yours,
183 theirsLines: r?.theirs ?? null,
184 context: r?.context ?? { before: [], after: [] },
185 result: r?.result ?? null,
186 kept: s.kept,
187 issue: null,
188 notes: notes.join("; "),
189 } as (typeof cases)[number]);
190 });
191}
192writeFileSync(out, JSON.stringify(cases, null, "\t"));
193const count = (k: string) => cases.filter((c) => c.kept === k).length;
194console.log(
195 `${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.`,
196);