Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
blame on this site's /mcp
CLI
abso blame worker/project/markers.ts --version 55 --json
JSON
/christopher/abso-core/blame/worker/project/markers.ts?version=55&format=json
364 linesFirst import 98% · engine 1% · infra-split 1%Last change engine in version 55,

364 lines from 4 changes in 4 versions.

init0Start the project1// Collision markers (#11, round 1, ruled 2026-10-05): when a sync finds both
2// sides changed the same lines differently, it writes the file diff3 style,
3// so the collision can't pass for finished code. Helpers write, find and settle
4// blocks, and read the blocks still in an agent's files (status, sync, publish).
5//
6// <<<<<<< yours (unpublished)
7// ...the agent's lines...
8// ||||||| before (version 276)
9// ...the lines both started from...
10// =======
11// ...their lines...
12// >>>>>>> theirs: writer1, version 277 "Boiler gauge says what it multiplies"
13//
14// Settled means the block is gone. These helpers also say what an agent kept
15// once it settled one.
17import { type Atom, type AtomId, diffLines, type Graph, type Hunk, type View } from "../../src/engine/index.ts";
18import type { AgentState, AgentStatus, Collision } from "./project-types.ts";
20export const YOURS = "<<<<<<< yours (unpublished)";
21const BEFORE = "||||||| before (version ";
22export const SPLIT = "=======";
23const THEIRS = ">>>>>>> theirs: ";
24const THEIRS_LABEL = /^>>>>>>> theirs: (.+), version (\d+) "(.*)"$/;
26/** One side of a block: who published theirs. */
27export interface TheirSide {
28 readonly agent: string;
29 readonly version: number;
30 readonly message: string;
33/** A block in a file: 0-based indexes of its first and last marker lines, and each part. */
34export interface Block extends TheirSide {
35 readonly start: number;
36 readonly end: number;
37 readonly yours: readonly string[];
38 readonly base: readonly string[];
39 readonly theirs: readonly string[];
42/** A collision a sync wrote markers for, kept until the agent publishes: what it says the agent kept. */
43export interface MarkedRegion extends TheirSide {
44 readonly file: string;
45 readonly yours: readonly string[];
46 readonly base: readonly string[];
47 readonly theirs: readonly string[];
48 /** The changes that collided (the draft's, then theirs): the edit that takes the markers out settles them. */
49 readonly settles?: readonly string[];
52export type Kept = "yours" | "theirs" | "both" | "rewrote";
54/** A publish message as a marker label: its first line, at most 100 characters, with no double quotes. */
55function label(message: string): string {
56 const first = message.split("\n")[0]!.trim().replace(/"/g, "'");
57 return first.length > 100 ? `${first.slice(0, 99)}…` : first;
60export function markerBlock(
61 yours: readonly string[],
62 base: readonly string[],
63 baseVersion: number,
64 theirs: readonly string[],
65 who: TheirSide,
66): string[] {
67 return [
68 YOURS,
69 ...yours,
70 `${BEFORE}${baseVersion})`,
71 ...base,
72 SPLIT,
73 ...theirs,
74 `${THEIRS}${who.agent}, version ${who.version} "${label(who.message)}"`,
75 ];
78/** A line the engine writes as a marker (the split line alone is not one: `=======` is ordinary Markdown). */
79export function isMarkerLine(line: string): boolean {
80 return line === YOURS || (line.startsWith(BEFORE) && line.endsWith(")")) || THEIRS_LABEL.test(line);
83/** The well-formed blocks in a file's lines, in order. */
84export function blocksIn(lines: readonly string[]): Block[] {
85 const out: Block[] = [];
86 for (let i = 0; i < lines.length; i++) {
87 if (lines[i] !== YOURS) continue;
88 let b = -1;
89 let s = -1;
90 let e = -1;
91 for (let j = i + 1; j < lines.length; j++) {
92 const l = lines[j]!;
93 if (l === YOURS) break;
94 if (b < 0 && l.startsWith(BEFORE)) b = j;
95 else if (b >= 0 && s < 0 && l === SPLIT) s = j;
96 else if (s >= 0 && THEIRS_LABEL.test(l)) {
97 e = j;
98 break;
99 }
100 }
101 if (e < 0) continue;
102 const m = THEIRS_LABEL.exec(lines[e]!)!;
103 out.push({
104 start: i,
105 end: e,
106 yours: lines.slice(i + 1, b),
107 base: lines.slice(b + 1, s),
108 theirs: lines.slice(s + 1, e),
109 agent: m[1]!,
110 version: Number(m[2]),
111 message: m[3]!,
112 });
113 i = e;
114 }
115 return out;
118/** The lines with every block replaced by the side (or sides) kept. */
119export function settleLines(
120 lines: readonly string[],
121 keep: "yours" | "theirs" | "both",
122): { lines: string[]; blocks: number } {
123 const blocks = blocksIn(lines);
124 const out: string[] = [];
125 let pos = 0;
126 for (const b of blocks) {
127 out.push(...lines.slice(pos, b.start));
128 if (keep !== "theirs") out.push(...b.yours);
129 if (keep !== "yours") out.push(...b.theirs);
130 pos = b.end + 1;
131 }
132 out.push(...lines.slice(pos));
133 return { lines: out, blocks: blocks.length };
136/**
137 * The edit from their text to the merged text that keeps their lines inside
138 * each block as their lines: a plain diff would pair a `}` of theirs with a
139 * `}` of yours, so their definition would read as partly the agent's, and the
140 * duplicate check would miss it. Between blocks it is a plain diff. `blocks`
141 * gives each block's place in the merged lines (at, length) and in theirs
142 * (theirsAt, theirs: how many of their lines it holds).
143 */
144export function markedHunks(
145 theirs: readonly string[],
146 merged: readonly string[],
147 blocks: readonly { at: number; length: number; theirsAt: number; theirs: number }[],
148): Hunk[] {
149 const out: Hunk[] = [];
150 const push = (h: Hunk): void => {
151 const last = out[out.length - 1];
152 // Hunks that meet are one hunk, so no two edits touch the same gap.
153 if (last && last.end === h.start)
154 out[out.length - 1] = { start: last.start, end: h.end, lines: [...last.lines, ...h.lines] };
155 else out.push(h);
156 };
157 let t = 0;
158 let m = 0;
159 for (const b of blocks) {
160 for (const h of diffLines(theirs.slice(t, b.theirsAt), merged.slice(m, b.at)))
161 push({ start: h.start + t, end: h.end + t, lines: h.lines });
162 // The block is: its opening lines, their lines as they are, the closing marker.
163 const head = b.length - b.theirs - 1;
164 push({ start: b.theirsAt, end: b.theirsAt, lines: merged.slice(b.at, b.at + head) });
165 push({ start: b.theirsAt + b.theirs, end: b.theirsAt + b.theirs, lines: [merged[b.at + b.length - 1]!] });
166 t = b.theirsAt + b.theirs;
167 m = b.at + b.length;
168 }
169 for (const h of diffLines(theirs.slice(t), merged.slice(m)))
170 push({ start: h.start + t, end: h.end + t, lines: h.lines });
171 return out;
174/** A line's words, lowercased, without a checklist box: what "the same line, reworded" compares. */
175function words(line: string): Set<string> {
176 return new Set(
177 line
178 .replace(/^\s*[-*]\s*\[[ xX]\]\s*/, "")
179 .toLowerCase()
180 .match(/[a-z0-9]+/g) ?? [],
181 );
184/** Whether `b` reads as `a` reworded: at least half their words in common. */
185function alike(a: string, b: string): boolean {
186 if (a === b) return true;
187 const x = words(a);
188 const y = words(b);
189 if (x.size === 0 || y.size === 0) return false;
190 let both = 0;
191 for (const w of x) if (y.has(w)) both++;
192 return both / (x.size + y.size - both) >= 0.5;
195/**
infra-split39More comments and test names give their reason without naming who raised it The same cleanup as version 37, for notes it missed because they were wrapped or worded differently: "(#37, Playtester 1)" becomes "(#37)", "(#39, agreed with Engine)" becomes "(#39)", and a commit hash after a name goes with the name. Issue numbers stay. No behavior changes.Compare196 * Lines both sides still had that the settled file has lost (#37): neither side deleted them, yet neither side's version, nor the base's,
init0Start the project197 * nor anything reading like a rewording of one, is left. When both sides
198 * changed line for line, each position is one line in three versions;
199 * otherwise only lines both sides kept word for word count. Answers each
200 * lost line as one side gave it.
201 */
202export function lostLines(lines: readonly string[], region: MarkedRegion): string[] {
203 const solid = (l: string | undefined): l is string => l !== undefined && /[A-Za-z0-9]/.test(l);
204 const groups: string[][] =
205 region.yours.length === region.theirs.length
206 ? region.yours.map((y, i) => [
207 y,
208 region.theirs[i]!,
209 ...(region.base.length === region.yours.length ? [region.base[i]!] : []),
210 ])
211 : region.yours.filter((l) => region.theirs.includes(l)).map((l) => [l]);
212 const now = lines.filter((l) => solid(l));
213 const out: string[] = [];
214 for (const g of groups) {
215 const versions = g.filter(solid);
216 if (versions.length === 0) continue;
217 if (versions.some((v) => now.some((l) => alike(v, l)))) continue;
218 out.push(versions[0]!);
219 }
220 return out;
223/**
224 * What a settled region kept, judged from the file as it is now, by each
infra-split37Comments and test names give each reason in plain words, without naming who raised it Notes like "(#37, Builder 5)" or "(AVCS - Security)" become "(#37)" or go, and a private commit hash after a name goes with it. Issue numbers and references such as git's CVE-2019-1353 stay. Only comments and test names change; no behavior changes.Compare225 * side's own changes (#37): the lines it added, and
init0Start the project226 * the base lines only it removed. A side's block also holds lines it left as
227 * they were, and an edit to one of those must not hide that its change was
228 * kept. Lines both sides added alike, and lines with no letters or digits
229 * (`}`, blank), say nothing about whose change survived.
230 */
231export function keptOf(lines: readonly string[], region: MarkedRegion): Kept {
232 const now = new Set(lines);
233 const base = new Set(region.base);
234 const solid = (l: string) => /[A-Za-z0-9]/.test(l);
235 const yours = new Set(region.yours);
236 const theirs = new Set(region.theirs);
237 // Whether each of a side's changes is in the file: all of them, none, some, or it made none of its own.
238 const score = (
239 mine: Set<string>,
240 other: Set<string>,
241 side: readonly string[],
242 ): "all" | "none" | "some" | "nothing" => {
243 const checks = [
244 ...side.filter((l) => solid(l) && !base.has(l) && !other.has(l)).map((l) => now.has(l)),
245 ...region.base.filter((l) => solid(l) && !mine.has(l) && other.has(l)).map((l) => !now.has(l)),
246 ];
247 if (checks.length === 0) return "nothing";
248 if (checks.every(Boolean)) return "all";
249 return checks.some(Boolean) ? "some" : "none";
250 };
251 const y = score(yours, theirs, region.yours);
252 const t = score(theirs, yours, region.theirs);
253 if (y === "some" || t === "some") return "rewrote";
254 if (y === "all" && t === "all") return "both";
255 if (y === "all") return "yours";
256 if (t === "all") return "theirs";
257 // One side made no change of its own and the other's is gone: what's left is the first side's.
258 if (y === "none" && t === "nothing") return "theirs";
259 if (t === "none" && y === "nothing") return "yours";
260 return y === "nothing" && t === "nothing" ? "both" : "rewrote";
263/** What the marker reads need from a project. */
264export interface MarkerHost {
265 readonly graph: Graph;
266 agentView(agent: AgentState): View;
267 draft(agent: AgentState): AtomId[];
268 draftFiles(agent: AgentState): Set<string>;
271/** Files where a sync wrote the agent collision markers in its current space (#11). */
272export function markedFiles(agent: AgentState): string[] {
273 return [...new Set(agent.marked.filter((r) => r.space === agent.space).map((r) => r.file))].toSorted();
276export function blockCollision(file: string, b: Block): Collision {
277 const lines: [number, number] = [b.start + 1, b.end + 1];
278 const text = (ls: readonly string[]) => (ls.length > 0 ? `${ls.join("\n")}\n` : "");
279 return {
280 kind: "lines",
281 marked: true,
282 file,
283 yours: { lines, text: text(b.yours) },
284 theirs: { lines: [...lines], text: text(b.theirs), agent: b.agent, message: b.message, version: b.version },
285 };
288/** The blocks of markers still in the agent's files, as collisions (#11): listed by sync and status. */
289export function markedCollisions(host: MarkerHost, agent: AgentState): Collision[] {
engine55Issue 6: status no longer builds every agent's view. On abso's log (13,148 entries, 48 agents) status went from about 330 ms to 1 ms median (30 ms max) in Node. - markedCollisions skips the view when the agent has no marked files (most have none). This was most of the cost. - draftFiles skips the view when the draft is empty, and Project keeps each agent's draft files under their view key plus the draft, so they're worked out again only when either moves. Overlaps and overlap news use the kept ones too. - New test (draft-files-kept): status from a warm project equals a cold load after every step, in shared and with two agents in one named draft (Pinch's case). A weaker key fails it.Compare290 const files = markedFiles(agent);
291 // No marked files, no view to build (abso-core issue 6): most agents have none.
292 if (files.length === 0) return [];
init0Start the project293 const view = host.agentView(agent);
294 const out: Collision[] = [];
engine55Issue 6: status no longer builds every agent's view. On abso's log (13,148 entries, 48 agents) status went from about 330 ms to 1 ms median (30 ms max) in Node. - markedCollisions skips the view when the agent has no marked files (most have none). This was most of the cost. - draftFiles skips the view when the draft is empty, and Project keeps each agent's draft files under their view key plus the draft, so they're worked out again only when either moves. Overlaps and overlap news use the kept ones too. - New test (draft-files-kept): status from a warm project equals a cold load after every step, in shared and with two agents in one named draft (Pinch's case). A weaker key fails it.Compare295 for (const file of files) {
init0Start the project296 if (!view.hasFile(file)) continue;
297 for (const b of blocksIn(view.lines(file).map((l) => l.text))) out.push(blockCollision(file, b));
298 }
299 return out;
302export function openCollisions(host: MarkerHost, agent: AgentState): { collisions?: AgentStatus["collisions"] } {
303 const open = markedCollisions(host, agent).map((c) => ({
304 file: c.file,
305 lines: c.yours.lines,
306 theirs: { agent: c.theirs.agent, version: c.theirs.version, message: c.theirs.message },
307 }));
308 return open.length > 0 ? { collisions: open } : {};
311/** The collided changes behind each block whose marker lines `atom` deletes (#11). */
312export function markersSettled(host: MarkerHost, agent: AgentState, view: View, atom: Atom): AtomId[] {
313 if (agent.marked.length === 0) return [];
314 const out = new Set<AtomId>();
315 const blocksBy = new Map<string, Block[]>();
316 for (const id of atom.deletes) {
317 const info = host.graph.line(id);
318 if (!info || !isMarkerLine(info.text)) continue;
319 const file = view.fileOf(id);
320 const lines = view.lines(file);
321 if (!blocksBy.has(file)) blocksBy.set(file, blocksIn(lines.map((l) => l.text)));
322 const at = lines.findIndex((l) => l.id === id);
323 const b = blocksBy.get(file)!.find((x) => at >= x.start && at <= x.end);
324 if (!b) continue;
325 const r = agent.marked.find(
326 (m) => m.space === agent.space && m.file === file && m.agent === b.agent && m.version === b.version,
327 );
328 for (const s of r?.settles ?? []) out.add(s);
329 }
330 // Only changes still in the view can be named: the draft's own side was replaced when the sync wrote the markers.
331 return [...out].filter((id) => view.set.has(id));
334/**
335 * Marker lines the agent's draft adds (#11), as collisions: each
336 * well-formed block, and any marker line left outside one. Only the labeled
337 * lines the engine writes count, so a document that shows markers can be
338 * published.
339 */
340export function markerCollisions(host: MarkerHost, agent: AgentState): Collision[] {
341 const mine = new Set(host.draft(agent));
342 const view = host.agentView(agent);
343 const out: Collision[] = [];
344 for (const file of [...host.draftFiles(agent)].toSorted()) {
345 if (!view.hasFile(file)) continue;
346 const lines = view.lines(file);
347 const added = lines.map((l, i) => (mine.has(l.atom) && isMarkerLine(l.text) ? i : -1)).filter((i) => i >= 0);
348 if (added.length === 0) continue;
349 const blocks = blocksIn(lines.map((l) => l.text));
350 for (const b of blocks) out.push(blockCollision(file, b));
351 for (const i of added) {
352 if (blocks.some((b) => i >= b.start && i <= b.end)) continue;
353 const at: [number, number] = [i + 1, i + 1];
354 out.push({
355 kind: "lines",
356 marked: true,
357 file,
358 yours: { lines: at, text: `${lines[i]!.text}\n` },
359 theirs: { lines: [...at], text: "", agent: "", message: "a marker line left from a collision", version: 0 },
360 });
361 }
362 }
363 return out;