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

Versions 153 to 318

Compare

+153 −24 in 6 places
Lines 9–18 at 318 (were 9–16 at 153)
99 conflictKeys,
1010 type Graph,
1111 joinLines,
12+ type Key,
1213 parseKey,
1314 type ReadView,
15+ seqOf,
1416} from "../../src/engine/index.ts";
1517import { differenceSet, StableOverlaySet } from "../../src/engine/state-source.ts";
1618import { definedTwice } from "./definitions.ts";
Lines 46–65 at 318 (were 44–50 at 153)
4446 against: ReadonlySet<AtomId>,
4547 settled: ReadonlySet<AtomId>,
4648): Pair[] {
47− const out: Pair[] = [];
49+ return pairsFrom(g, claimantsIn(g, draft, against), against, settled);
50+}
52+/**
53+ * Each draft change's claimants in `against`, in seq order: the atoms whose claims meet its keys, before the
54+ * engine's rule is applied. A stored atom's keys are read from disk, so a caller checking one draft against many
55+ * (draft-collisions.ts) gathers once against all of them and filters with pairsFrom.
56+ */
57+export function claimantsIn(
58+ g: Graph,
59+ draft: readonly AtomId[],
60+ against: ReadonlySet<AtomId>,
61+): ReadonlyMap<AtomId, readonly AtomId[]> {
62+ const out = new Map<AtomId, AtomId[]>();
4863 for (const yours of draft) {
4964 const atom = g.atom(yours);
5065 const streams: IterableIterator<AtomId>[] = [];
Lines 67–128 at 318 (were 52–58 at 153)
5267 streams.push(g.eachWriterOf(key), g.eachReaderOf(key), g.eachHolderOf(key));
5368 }
5469 for (const key of claimsOf(atom)) streams.push(g.eachWriterOf(key));
55− for (const theirs of orderedClaimants(g, streams)) {
70+ const theirs: AtomId[] = [];
71+ for (const id of orderedClaimants(streams)) if (against.has(id)) theirs.push(id);
72+ out.set(yours, theirs);
73+ }
74+ return out;
75+}
77+/**
78+ * claimantsIn for a few atoms against each other: each atom's claimants among `atoms`, in seq order, from their own
79+ * keys held in memory. Walking each key's history of claimants read four lists from disk per key, and a draft deleting
80+ * a big file has thousands of keys (abso Capacity).
81+ */
82+export function claimantsAmong(g: Graph, atoms: readonly AtomId[]): ReadonlyMap<AtomId, readonly AtomId[]> {
83+ const claimers = new Map<Key, AtomId[]>();
84+ const writers = new Map<Key, AtomId[]>();
85+ const keys = new Map<AtomId, { writes: readonly Key[]; claims: readonly Key[] }>();
86+ const add = (index: Map<Key, AtomId[]>, key: Key, id: AtomId) => {
87+ const list = index.get(key);
88+ if (list) list.push(id);
89+ else index.set(key, [id]);
90+ };
91+ for (const id of atoms) {
92+ const atom = g.atom(id);
93+ const writes = [...atom.writes];
94+ // claimsOf, with the writes already read.
95+ const claims = [...new Set([...writes, ...atom.reads, ...atom.holds])].sort();
96+ keys.set(id, { writes, claims });
97+ for (const key of writes) add(writers, key, id);
98+ for (const key of claims) add(claimers, key, id);
99+ }
100+ const out = new Map<AtomId, AtomId[]>();
101+ for (const id of atoms) {
102+ const own = keys.get(id)!;
103+ const found = new Set<AtomId>();
104+ // As claimantsIn's streams: what claims a key it writes, and what writes a key it claims.
105+ for (const key of own.writes) for (const other of claimers.get(key) ?? []) found.add(other);
106+ for (const key of own.claims) for (const other of writers.get(key) ?? []) found.add(other);
107+ found.delete(id);
108+ out.set(
109+ id,
110+ [...found].sort((a, b) => seqOf(a) - seqOf(b)),
111+ );
112+ }
113+ return out;
114+}
116+/** The claimants in `against` that collide by the engine's rule and that no atom of `settled` resolves. */
117+export function pairsFrom(
118+ g: Graph,
119+ claimants: ReadonlyMap<AtomId, readonly AtomId[]>,
120+ against: ReadonlySet<AtomId>,
121+ settled: ReadonlySet<AtomId>,
122+): Pair[] {
123+ const out: Pair[] = [];
124+ for (const [yours, theirsList] of claimants) {
125+ for (const theirs of theirsList) {
56126 if (!against.has(theirs)) continue;
57127 const [lo, hi] = g.atom(yours).seq < g.atom(theirs).seq ? [yours, theirs] : [theirs, yours];
58128 const keys = conflictKeys(g.atom(lo), g.atom(hi), g.dependsOn(hi, lo));
Lines 136–183 at 318 (were 66–93 at 153)
66136
67137/** Claim indexes append in canonical seq order. Merge one cursor per current
68138 * atom key, retaining no candidate set proportional to historical claims. */
69−function* orderedClaimants(g: Graph, streams: IterableIterator<AtomId>[]): IterableIterator<AtomId> {
139+function* orderedClaimants(streams: IterableIterator<AtomId>[]): IterableIterator<AtomId> {
70140 try {
71− const heads = streams.map((stream) => stream.next());
141+ type Head = { id: AtomId; seq: number; stream: number };
142+ const heap: Head[] = [];
143+ const less = (a: Head, b: Head) => a.seq < b.seq || (a.seq === b.seq && a.stream < b.stream);
144+ const push = (head: Head) => {
145+ let at = heap.length;
146+ heap.push(head);
147+ while (at > 0) {
148+ const parent = (at - 1) >>> 1;
149+ if (!less(head, heap[parent]!)) break;
150+ heap[at] = heap[parent]!;
151+ at = parent;
152+ }
153+ heap[at] = head;
154+ };
155+ // Each claimant's seq from its id, as the graph reads it (abso 738): loading the stored atom for it read a row a
156+ // claimant, most of a warm draft-state's pairing on abso.
157+ const advance = (stream: number) => {
158+ const next = streams[stream]!.next();
159+ if (!next.done) push({ id: next.value, seq: seqOf(next.value), stream });
160+ };
161+ for (let stream = 0; stream < streams.length; stream++) advance(stream);
72162 let previous = -1;
73− for (;;) {
74− let best = -1;
75− let seq = Infinity;
76− for (let i = 0; i < heads.length; i++) {
77− const head = heads[i]!;
78− if (head.done) continue;
79− const current = g.atom(head.value).seq;
80− if (current < seq) {
81− best = i;
82− seq = current;
163+ while (heap.length) {
164+ const head = heap[0]!;
165+ const tail = heap.pop()!;
166+ if (heap.length) {
167+ let at = 0;
168+ while (at * 2 + 1 < heap.length) {
169+ let child = at * 2 + 1;
170+ if (child + 1 < heap.length && less(heap[child + 1]!, heap[child]!)) child++;
171+ if (!less(heap[child]!, tail)) break;
172+ heap[at] = heap[child]!;
173+ at = child;
83174 }
175+ heap[at] = tail;
84176 }
85− if (best < 0) return;
86− const id = heads[best]!.value!;
87− heads[best] = streams[best]!.next();
88− if (seq === previous) continue;
89− previous = seq;
90− yield id;
177+ advance(head.stream);
178+ if (head.seq === previous) continue;
179+ previous = head.seq;
180+ yield head.id;
91181 }
92182 } finally {
93183 for (const stream of streams) stream.return?.();
Lines 192–199 at 318 (were 102–109 at 153)
102192 let a = left.next();
103193 let b = right.next();
104194 while (!a.done && !b.done) {
105− const x = g.atom(a.value).seq;
106− const y = g.atom(b.value).seq;
195+ const x = seqOf(a.value);
196+ const y = seqOf(b.value);
107197 if (x === y && settled.has(a.value)) return true;
108198 if (x <= y) a = left.next();
109199 if (y <= x) b = right.next();
Lines 284–339 at 318 (were 194–210 at 153)
194284 for (const file of g.filesOf(id)) out.add(file);
195285 if (moved.length === 0) continue;
196286 const atom = g.atom(id);
197− const lines: string[] = [...g.deletedLines(id)];
287+ const lines: string[] = [];
198288 for (const run of atom.inserts) if (!run.parent.startsWith("^")) lines.push(run.parent);
199289 for (const m of atom.moves ?? []) {
200290 lines.push(m.line);
201291 if (!m.parent.startsWith("^")) lines.push(m.parent);
202292 }
203− for (const v of moved) for (const line of lines) out.add(v.fileOf(line));
293+ for (const v of moved) {
294+ for (const line of lines) out.add(v.fileOf(line));
295+ // Deleted lines by number where the view can (abso Capacity): a draft deleting a big file has thousands, and
296+ // making each one's id only to look its number up again was most of collisions on website.
297+ const byNumber = (v as { fileOfNumber?: (n: number) => string }).fileOfNumber;
298+ if (byNumber) for (const n of g.deletedNumbers(id)) out.add(byNumber.call(v, n));
299+ else for (const line of g.deletedLines(id)) out.add(v.fileOf(line));
300+ }
204301 }
205302 return out;
206303}
207304
305+/**
306+ * The files a view over `below` plus `mine` can lay out differently from `below` (abso Capacity, View.shareFiles), or
307+ * undefined when any of it moves lines and `placedDifferently` can't say where those land differently. A view's set is closed under dependencies, so it holds `mine` and whatever
308+ * `mine` depends on that `below` lacks; each of those can change a file, so all of them count. Counting `mine` alone
309+ * missed the files of work the draft depends on (Testing's typescript-rs diff for cloud-ts-01, core 235).
310+ */
311+export function touchedBeyond(
312+ g: Graph,
313+ below: ReadonlySet<AtomId>,
314+ mine: Iterable<AtomId>,
315+ views: readonly ReadView[],
316+ placedDifferently?: () => ReadonlySet<string> | undefined,
317+): Set<string> | undefined {
318+ const beyond = new Set<AtomId>();
319+ const stack = [...mine];
320+ while (stack.length) {
321+ const id = stack.pop()!;
322+ if (beyond.has(id) || below.has(id)) continue;
323+ beyond.add(id);
324+ for (const dep of g.atom(id).deps) if (!beyond.has(dep) && !below.has(dep)) stack.push(dep);
325+ }
326+ const files = touchedFiles(g, beyond, views);
327+ // Moves change more than the files their atoms name only where a moved line lands differently (View.placedDifferently,
328+ // abso Capacity); without that comparison, a draft that moves lines shares nothing.
329+ if ([...beyond].some((id) => (g.atom(id).moves?.length ?? 0) > 0)) {
330+ const placed = placedDifferently?.();
331+ if (!placed) return undefined;
332+ for (const file of placed) files.add(file);
333+ }
334+ return files;
335+}
208337/** The atom's visible lines in a file of a view, or where it acted when none are left. */
209338export function describeSide(view: ReadView, atom: AtomId, file: string, keys: readonly string[]): CollisionSide {
210339 const visible = view.lines(file);
MCP
compare on this site's /mcp
CLI
abso diff 153 318 worker/project/collisions.ts --json
JSON
/christopher/abso-core/compare/153..318?file=worker%2Fproject%2Fcollisions.ts&format=json