- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/scripts/autosettle/extract.ts?version=190&format=json
138 linesautosettle 100%Last change autosettle in version 190, 1 version changed it
You're reading version 190. The newest is 318, and 2 versions since then changed this file.
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// 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.
11import { execFileSync } from "node:child_process";
12import { writeFileSync } from "node:fs";
14const [seat, out] = process.argv.slice(2);
15if (!seat || !out) {
16 console.error("usage: node scripts/autosettle/extract.ts <seat folder> <cases.json> [--since 8d]");
17 process.exit(2);
18}
19const since = process.argv.includes("--since") ? process.argv[process.argv.indexOf("--since") + 1]! : "8d";
20const abso = (...args: string[]): string =>
21 execFileSync("abso", args, { cwd: seat, encoding: "utf8", maxBuffer: 1 << 28, shell: process.platform === "win32" });
23const fileAt = (version: number, file: string): string[] | null => {
24 try {
25 return abso("show", `${version}:${file}`).split(/\r?\n/);
26 } catch {
27 return null;
28 }
29};
31interface Settled {
32 file: string;
33 kept: "yours" | "theirs" | "both" | "rewrote";
34 theirs: { agent: string; version: number };
35 version: number;
36 held?: string;
37}
38const review = JSON.parse(abso("review", "--since", since, "--json")) as {
39 changes: {
40 agent?: string;
41 author?: string;
42 message?: string;
43 settled: Settled[];
44 versions?: { version: number; message: string }[];
45 }[];
46};
47const messages = new Map<number, string>();
48for (const c of review.changes) for (const v of c.versions ?? []) messages.set(v.version, v.message);
49const messageOf = (v: number): string | undefined => {
50 if (!messages.has(v)) {
51 try {
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]!);
54 } catch {
55 return undefined;
56 }
57 }
58 return messages.get(v);
59};
61/** The span where `a` and `b` differ, by common prefix and suffix. */
62function hunk(a: string[], b: string[]): { start: number; aEnd: number; bEnd: number } {
63 let start = 0;
64 while (start < a.length && start < b.length && a[start] === b[start]) start++;
65 let tail = 0;
66 while (tail < a.length - start && tail < b.length - start && a[a.length - 1 - tail] === b[b.length - 1 - tail])
67 tail++;
68 return { start, aEnd: a.length - tail, bEnd: b.length - tail };
69}
71/** The lines of `r` between the context above and below a hunk of `t`, found by a few anchor lines. */
72function 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;
78 return -2;
79 };
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);
86}
88const cases = [];
89for (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}`);
112 cases.push({
113 id: `${s.version}-${s.file}`,
114 file: s.file,
115 kind: "lines",
116 settler: {
117 agent: c.agent ?? c.author ?? "?",
118 heldMessage: s.held,
119 settleVersion: s.version,
120 settleMessage: messageOf(s.version),
121 },
122 theirs: { agent: s.theirs.agent, version: s.theirs.version, message: messageOf(s.theirs.version) },
123 base,
124 yours,
125 theirsLines,
126 context,
127 result: region,
128 kept: s.kept,
129 issue: null,
130 notes: notes.join("; "),
131 });
132 }
133}
134writeFileSync(out, JSON.stringify(cases, null, "\t"));
135const count = (k: string) => cases.filter((c) => c.kept === k).length;
136console.log(
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.`,
138);