Auto-settle (#19): replay.ts recovers each past hold's lines from a project's raw log (the owner-only export): the held agent's draft edits up to its refused push, theirs' edits, and the groups that wrote the same lines, paired with the settle's blocks in file order. That recovers the held agent's side even when it kept theirs or edited by hand, which no version shows. Tried on a local hold where the agent kept theirs: before 100, yours 64, theirs 1000, all recovered. Version 197 of shared· autosettle· Oct 15 (2026-10-15 00:22 UTC) · Not checked
Auto-settle (#19 ): replay.ts recovers each past hold's lines from a project's raw log (the owner-only export): the held agent's draft edits up to its refused push, theirs' edits, and the groups that wrote the same lines, paired with the settle's blocks in file order. That recovers the held agent's side even when it kept theirs or edited by hand, which no version shows.
Tried on a local hold where the agent kept theirs: before 100, yours 64, theirs 1000, all recovered.
Version 196 · Files at version 197
1 file, 217 lines added, 0 removed.
--- /dev/null
+++ b/scripts/autosettle/replay.ts
@@ -0,0 +1,217 @@
1 + // Recover each past hold's lines from a project's raw log (#19): the held agent's own lines survive in its draft 2 + // entries even after it kept theirs or edited by hand, which no version shows. 4 + // node scripts/autosettle/replay.ts <log.ndjson> <cases.json> <seat folder> <out.json> 6 + // <log.ndjson> is GET /p/<project>/export (one raw log entry per line; owner-only). <cases.json> comes from 7 + // extract.ts, which says which settles to look for and what was kept. The seat folder reads theirs' file text 8 + // for positions and context. For each settle: the held agent's draft edits between its last push and the 9 + // refused push, the edits of theirs' push, and the groups of them that wrote the same lines (abso's own test 10 + // for a collision), in file order, paired with the settle's blocks in order. 12 + import { execFileSync } from "node:child_process"; 13 + import { readFileSync, writeFileSync } from "node:fs"; 25 + inserts: InsertRun[]; 37 + } & Record<string, unknown>; 41 + settler: { agent: string; heldMessage?: string; settleVersion: number; settleMessage?: string }; 42 + theirs: { agent: string; version: number; message?: string }; 44 + [k: string]: unknown; 47 + const [logPath, casesPath, seat, out] = process.argv.slice(2); 48 + if (!logPath || !casesPath || !seat || !out) { 49 + console.error("usage: node scripts/autosettle/replay.ts <log.ndjson> <cases.json> <seat folder> <out.json>"); 52 + const entries: Entry[] = readFileSync(logPath, "utf8") 54 + .filter((l) => l.trim() !== "") 55 + .map((l) => JSON.parse(l) as Entry); 56 + const cases = JSON.parse(readFileSync(casesPath, "utf8")) as Case[]; 57 + const abso = (...args: string[]) => 58 + execFileSync("abso", args, { cwd: seat, encoding: "utf8", maxBuffer: 1 << 28, shell: process.platform === "win32" }); 60 + // Every line any atom inserted: its text and file. 61 + const lineText = new Map<string, { text: string; file: string }>(); 62 + const atoms = new Map<string, Atom>(); 63 + for (const e of entries) 64 + for (const a of [e.atom, e.tie]) { 65 + if (!a?.inserts) continue; 68 + for (const run of a.inserts) for (const text of run.lines) lineText.set(`${a.id}.${k++}`, { text, file: run.file }); 70 + const insertedIds = (a: Atom, file: string) => { 71 + const ids: string[] = []; 73 + for (const run of a.inserts) 74 + for (const _ of run.lines) { 75 + if (run.file === file) ids.push(`${a.id}.${k}`); 80 + const MARKER = /^(<<<<<<< yours|\|\|\|\|\|\|\| before|=======$|>>>>>>> theirs:)/; 82 + /** The published shared version's index in the log, and the agent's draft atoms before an index since its last push. */ 83 + const publishAt = new Map<number, number>(); 84 + entries.forEach((e, i) => { 85 + if ((e.type === "publish" || e.type === "land") && e.space === undefined && typeof e.version === "number") 86 + publishAt.set(e.version, i); 88 + function draftBefore(agent: string, end: number): Atom[] { 90 + for (let i = end - 1; i >= 0; i--) { 91 + const e = entries[i]!; 92 + if (e.agent === agent && (e.type === "publish" || e.type === "land")) { 99 + .filter((e) => e.type === "draft" && e.agent === agent && e.atom) 100 + .map((e) => e.atom!); 103 + /** Atoms touching `file`, grouped into collisions: groups linked by a written key or by one editing another's lines. */ 104 + function collisions(mine: Atom[], theirs: Atom[], file: string) { 105 + const touches = (a: Atom) => 106 + a.inserts.some((r) => r.file === file) || a.deletes.some((d) => lineText.get(d)?.file === file); 108 + ...mine.filter(touches).map((a) => ({ a, side: "yours" as const })), 109 + ...theirs.filter(touches).map((a) => ({ a, side: "theirs" as const })), 111 + const parent = all.map((_, i) => i); 112 + const find = (i: number): number => (parent[i] === i ? i : (parent[i] = find(parent[i]!))); 113 + const keyOwner = new Map<string, number>(); 114 + all.forEach(({ a }, i) => { 115 + const keys = [...a.writes, ...a.deletes.map((d) => `L:${d}`)]; 116 + for (const own of insertedIds(a, file)) keys.push(`own:${own}`); 117 + for (const k of keys) { 118 + const key = k.startsWith("own:") ? `L:${k.slice(4)}` : k; 119 + const j = keyOwner.get(key); 120 + if (j === undefined) keyOwner.set(key, i); 121 + else parent[find(i)] = find(j); 124 + const groups = new Map<number, typeof all>(); 125 + all.forEach((x, i) => groups.set(find(i), [...(groups.get(find(i)) ?? []), x])); 126 + return [...groups.values()].filter((g) => g.some((x) => x.side === "yours") && g.some((x) => x.side === "theirs")); 129 + /** The lines a side's atoms in a group leave standing, in order, without marker lines. */ 130 + function standing(group: { a: Atom; side: string }[], side: string, file: string): string[] { 131 + const atomsOf = group.filter((x) => x.side === side).map((x) => x.a); 132 + const gone = new Set(atomsOf.flatMap((a) => a.deletes)); 134 + .sort((x, y) => x.seq - y.seq) 135 + .flatMap((a) => insertedIds(a, file)) 136 + .filter((id) => !gone.has(id)) 137 + .map((id) => lineText.get(id)!.text) 138 + .filter((t) => !MARKER.test(t)); 141 + const results: unknown[] = []; 142 + const byGroup = new Map<string, Case[]>(); 143 + for (const c of cases) { 144 + const key = `${c.settler.settleVersion} ${c.file}`; 145 + byGroup.set(key, [...(byGroup.get(key) ?? []), c]); 147 + for (const group of byGroup.values()) { 148 + const c0 = group[0]!; 149 + const settleAt = publishAt.get(c0.settler.settleVersion); 150 + const theirsAt = publishAt.get(c0.theirs.version); 151 + const notes: string[] = []; 152 + if (settleAt === undefined || theirsAt === undefined) { 153 + for (const c of group) results.push({ ...c, yours: null, notes: "the settle or theirs isn't in this log" }); 156 + // The held push: the settler's newest refusal naming the file before its settle. 158 + for (let i = settleAt - 1; i >= 0; i--) { 159 + const e = entries[i]!; 160 + if (e.type === "refused" && e.agent === c0.settler.agent) { 161 + const files = ((e.collisions as { file: string }[] | undefined) ?? []).map((x) => x.file); 162 + if (files.includes(c0.file)) { 168 + if (refusedAt < 0) notes.push("no refused push found; used the draft just before the settle"); 169 + const mine = draftBefore(c0.settler.agent, refusedAt >= 0 ? refusedAt : settleAt); 170 + const theirPublish = entries[theirsAt]!; 171 + const theirIds = (theirPublish.changes as string[] | undefined) ?? []; 172 + const theirs = theirIds.map((id) => atoms.get(id)).filter((a): a is Atom => a !== undefined); 173 + const found = collisions(mine, theirs, c0.file); 174 + // File order, by where theirs' first standing line sits in theirs' version. 175 + let theirFile: string[] = []; 177 + theirFile = abso("show", `${c0.theirs.version}:${c0.file}`).split(/\r?\n/); 179 + notes.push("couldn't read theirs' file"); 181 + const placed = found 183 + const t = standing(g, "theirs", c0.file); 184 + const at = t.length > 0 ? theirFile.indexOf(t[0]!) : -1; 185 + return { g, at, yours: standing(g, "yours", c0.file), theirs: t }; 187 + .sort((x, y) => x.at - y.at); 188 + if (placed.length !== group.length) 189 + notes.push(`found ${placed.length} collisions for ${group.length} settled blocks`); 190 + group.forEach((c, i) => { 191 + const p = placed[i]; 193 + ? [...new Set(p.g.flatMap((x) => x.a.deletes))] 194 + .filter((d) => !p.g.some((x) => insertedIds(x.a, c.file).includes(d))) 195 + .map((d) => lineText.get(d)?.text) 196 + .filter((t): t is string => t !== undefined && !MARKER.test(t)) 201 + yours: p ? p.yours : null, 202 + theirsLines: p ? p.theirs : null, 206 + before: theirFile.slice(Math.max(0, p.at - 15), p.at), 207 + after: theirFile.slice(p.at + p.theirs.length, p.at + p.theirs.length + 15), 209 + : { before: [], after: [] }, 210 + notes: [...notes, ...(p ? [] : ["no collision paired with this block"])].join("; "), 211 + recoveredBy: "replay", 215 + writeFileSync(out, JSON.stringify(results, null, "\t")); 216 + const ok = results.filter((r) => (r as { yours: unknown }).yours !== null).length; 217 + console.log(`${results.length} settled blocks; ${ok} with both sides recovered from the log.`);