why: reasons pointing at a change indexed, and a moved line looked for among its push (disk-first 2; abso Capacity). Version 242 of shared· capacity· Oct 15 (2026-10-15 13:46 UTC) · Not checked
why on abso, warm, one kept reader: 2.9 s a call with about 24,000 reads, 20,000 of them atoms.
Reasons in: why listed the changes whose reasons point at the line's change by reading every atom in the project. The graph now keeps, for each atom, the atoms whose reasons point at it, in seq order, once each (reasons-in, Graph.reasonsTo), from a fresh stored graph on. A graph made before keeps none, reasonsTo says undefined, and why looks at every atom as before. Moved from: the trail looked for a deleted copy of a line's text among every atom within 5,000 of the change. Only atoms pushed with it can count, so where the change is published its version's atoms are the candidates (Project.versionAtoms, trail's pushmates). A change still in a draft goes with every other draft change, so it looks near by as before. trail takes atoms' numbers from their ids instead of reading each atom. On abso adopted fresh: why 244 ms on the first call, then 86 and 46 ms (was 2.9 s); 1,091 reads, then 469 and 73. It matches replay, which takes 951 ms. Heap after: 22.6 MB.
New test reasons-to: the atoms pointing at an atom, in seq order, once each, whatever the kind and however many reasons one gives, in memory and stored. A stored graph without the index says undefined. Every engine test (292 of 292), every worker/project test (341 of 341); tsc and biome clean.
Version 241 · Files at version 242
5 files, 137 lines added, 9 removed.
--- a/src/engine/graph.ts
+++ b/src/engine/graph.ts
@@ -612,6 +612,7 @@
612 612 this.#dependentsPages = storedLists<AtomId>(storage, "dependents"); 613 613 this.#resolversPages = storedLists<AtomId>(storage, "resolvers"); 614 614 this.#togetherPages = storedLists<TogetherLink>(storage, "together"); 615 + this.#reasonsInPages = storedLists<AtomId>(storage, "reasons-in"); 615 616 this.#fileAtomPages = storedLists<AtomId>(storage, "file-atoms"); 616 617 this.#runStartPages = storedLists<number>(storage, "run-starts", (key) => 617 618 JSON.stringify(["number", Number(key)]), @@ -803,6 +804,16 @@
803 804 #resolversPages: ReturnType<typeof storedLists<AtomId>> | undefined; 804 805 #together: StateMap<AtomId, TogetherLink[]> = new Map(); 805 806 #togetherPages: ReturnType<typeof storedLists<TogetherLink>> | undefined; 807 + /** The atoms whose reasons point at each atom, in seq order, once each (abso Capacity). */ 808 + #reasonsIn = new Map<AtomId, AtomId[]>(); 809 + #reasonsInPages: ReturnType<typeof storedLists<AtomId>> | undefined; 810 + /** Whether a stored graph keeps reasons-in: from a fresh graph on, worked out when first needed. */ 811 + #reasonsInKnown: boolean | undefined; 812 + get #reasonsInKept(): boolean { 813 + if (!this.#storage) return true; 814 + this.#reasonsInKnown ??= this.#storage.read("reasons-in:meta", "kept") === 1; 815 + return this.#reasonsInKnown; 806 817 #fileAtoms: StateMap<string, AtomId[]> = new Map(); 807 818 #fileAtomPages: ReturnType<typeof storedLists<AtomId>> | undefined; 808 819 /** First lines of runs (their n), by the node they were written after: a line's n, or -1 - a path's index. */ @@ -1345,6 +1356,16 @@
1345 1356 else yield* listOf(this.#resolvers.get(id)); 1360 + * The atoms whose reasons point at this one, in seq order, once each; or undefined when a stored graph made before 1361 + * this index keeps none, and the caller looks at every atom (abso Capacity). 1363 + reasonsTo(id: AtomId): readonly AtomId[] | undefined { 1364 + if (!this.#reasonsInKept) return undefined; 1365 + if (this.#reasonsInPages) return [...this.#reasonsInPages.values(id)]; 1366 + return this.#reasonsIn.get(id) ?? []; 1348 1369 /** `together` reasons touching this atom, in either direction. */ 1349 1370 togetherLinks(id: AtomId): readonly TogetherLink[] { 1350 1371 return [...this.eachTogetherLink(id)]; @@ -1546,6 +1567,8 @@
1547 1568 if (this.#storage && this.#atoms.length === 0 && this.#packFiles) { 1548 1569 this.#storage.write("file-lines:meta", "format", 2); 1570 + this.#storage.write("reasons-in:meta", "kept", 1); 1571 + this.#reasonsInKnown = true; 1549 1572 this.#formatKnown = 2; @@ -1723,6 +1746,15 @@
1723 1746 if (this.#fileAtomPages) this.#fileAtomPages.append(file, frozen.id); 1724 1747 else pushIndex(this.#fileAtoms, file, frozen.id); 1749 + if (this.#reasonsInKept) { 1750 + const pointed = new Set<AtomId>(); 1751 + for (const r of frozen.reasons) { 1752 + if (pointed.has(r.to)) continue; 1754 + if (this.#reasonsInPages) this.#reasonsInPages.append(r.to, frozen.id); 1755 + else pushIndex(this.#reasonsIn, r.to, frozen.id); 1726 1758 for (const r of frozen.reasons) { 1727 1759 if (r.kind !== "together" || r.to === frozen.id) continue; --- /dev/null
+++ b/src/engine/test/reasons-to.test.ts
@@ -0,0 +1,56 @@
1 + // The atoms whose reasons point at an atom (abso Capacity, Graph.reasonsTo): why's "reasons in" reads them instead of 2 + // every atom. In seq order, once each, whatever the kind and however many reasons one atom gives; kept from a fresh 3 + // stored graph on, and undefined on a stored graph made before, where the caller looks at every atom. 5 + import assert from "node:assert/strict"; 6 + import { test } from "node:test"; 7 + import { Graph } from "../index.ts"; 8 + import { lines, Repo } from "./helpers.ts"; 9 + import { SqlState } from "./storage-helper.ts"; 12 + const repo = Repo.init({ "f.txt": lines("one", "two", "three") }); 13 + const x = repo.edit({ author: "ann", message: "x", edits: [{ file: "f.txt", old: "one", new: "ONE" }] }); 17 + edits: [{ file: "f.txt", old: "two", new: "TWO" }], 19 + { to: x.id, kind: "together", text: "same idea" }, 20 + { to: x.id, kind: "because-of", text: "and because of it" }, 23 + repo.edit({ author: "cy", message: "unrelated", edits: [{ file: "f.txt", old: "three", new: "3" }] }); 27 + edits: [{ file: "f.txt", old: "3", new: "THREE" }], 28 + reasons: [{ to: x.id, kind: "supersedes", text: "replaces it" }], 33 + /** Every atom whose reasons point at id, by looking at each one: what reasonsTo must give. */ 34 + const scanned = (graph: Graph, id: string) => 35 + [...graph.atoms()].filter((a) => a.reasons.some((r) => r.to === id)).map((a) => a.id); 37 + test("reasonsTo lists the atoms pointing at an atom, in seq order, once each", () => { 38 + const { repo, x } = history(); 39 + const want = scanned(repo.graph, x.id); 40 + assert.equal(want.length, 2, "bob's two reasons and dee's"); 41 + assert.deepEqual(repo.graph.reasonsTo(x.id), want); 42 + const storage = new SqlState(); 43 + const stored = new Graph({ storage }); 44 + for (const atom of repo.graph.atoms()) stored.add(atom); 45 + assert.deepEqual(new Graph({ storage }).reasonsTo(x.id), want); 46 + assert.deepEqual(new Graph({ storage }).reasonsTo("a0"), []); 49 + test("a stored graph made before the index keeps none, and says so", () => { 50 + const { repo, x } = history(); 51 + const storage = new SqlState(); 52 + const stored = new Graph({ storage }); 53 + for (const atom of repo.graph.atoms()) stored.add(atom); 54 + storage.remove("reasons-in:meta", "kept"); 55 + assert.equal(new Graph({ storage }).reasonsTo(x.id), undefined); --- a/worker/project/blame.ts
+++ b/worker/project/blame.ts
@@ -226,7 +226,9 @@
226 226 toMessage: graph.atom(r.to).message, 228 228 const reasonsIn: { from: AtomId; kind: string; text: string; fromAuthor: string; fromMessage: string }[] = []; 229 − for (const other of graph.atoms()) {
229 + // The atoms that point at it, where the graph keeps them (abso Capacity): looking at every atom read each one. 230 + const pointing = graph.reasonsTo(atom.id); 231 + for (const other of pointing ? pointing.map((id) => graph.atom(id)) : graph.atoms()) { 230 232 for (const r of other.reasons) { 231 233 if (r.to === atom.id) @@ -305,7 +307,13 @@
305 307 const graph = project.graph; 306 308 // Changes pushed together: the same version of the same space, or both still in one agent's draft. 307 309 const samePush = (a: Atom, b: Atom) => samePublish(publishOf(project, a.id), publishOf(project, b.id)); 308 − const { steps, more } = trail(graph, view, file, line, undefined, samePush);
310 + // What was pushed with a published atom is its version's atoms; an atom still in a draft goes with every other one, 311 + // so the trail looks near by as before. 312 + const pushmates = (a: Atom) => { 313 + const p = publishOf(project, a.id); 314 + return p ? project.versionAtoms(p.version, p.draft) : undefined; 316 + const { steps, more } = trail(graph, view, file, line, undefined, samePush, pushmates); 309 317 const groups: { steps: TrailStep[]; publish: Publish | null }[] = []; 310 318 for (const s of steps) { 311 319 const publish = publishOf(project, s.atom); --- a/worker/project/project.ts
+++ b/worker/project/project.ts
@@ -834,6 +834,12 @@
834 834 return p && { agent: p.agent, message: p.message, time: p.time }; 837 + /** The atoms a version of shared, or of a named draft, added to its space: what was pushed together (blame's trail). */ 838 + versionAtoms(version: number, draft: string | null = null): readonly AtomId[] { 839 + const space = draft === null ? this.#shared : this.#spaces.get(draft); 840 + return space?.versions[version]?.atoms ?? []; 837 843 /** The graph, for reading (blame, history). Don't add to it. */ 838 844 get graph(): Graph { 839 845 return this.#graph; --- a/worker/project/trail.ts
+++ b/worker/project/trail.ts
@@ -12,7 +12,15 @@
12 12 // in another (a file split in two), which the trail follows into the file it 15 − import type { Atom, AtomId, Graph, LineId, ReadView, SeqLine } from "../../src/engine/index.ts";
23 + } from "../../src/engine/index.ts"; 17 25 /** One change in a line's past, newest first. */ 18 26 export interface TrailStep { @@ -194,6 +202,7 @@
196 204 samePush: (a: Atom, b: Atom) => boolean = (a, b) => a.id === b.id, 205 + pushmates?: (atom: Atom) => Iterable<AtomId> | undefined, 197 206 ): { steps: TrailStep[]; more: boolean } { 198 207 const files = new Map<string, { seq: readonly SeqLine[]; index: Map<LineId, number> }>(); 199 208 const sequenceOf = (path: string) => { @@ -218,7 +227,7 @@
218 227 const { seq, index } = sequenceOf(path); 219 228 const at = index.get(id); 220 229 if (at === undefined) continue; 221 − const hunk = hunkOf(graph, seq, at, graph.atom(seq[at]!.atom));
230 + const hunk = hunkOf(seq, at, graph.atom(seq[at]!.atom)); 223 232 hunk.before.map((i) => seq[i]!.text), 224 233 hunk.after.map((i) => seq[i]!.text), @@ -234,7 +243,15 @@
234 243 let formatting = pair?.formatting ?? false; 235 244 let searched = false; 236 245 if (replaced === undefined) { 237 − const moved = movedFrom(graph, view, atom, seq[at]!.text, new Set(hunk.before.map((i) => seq[i]!.id)), samePush);
246 + const moved = movedFrom( 251 + new Set(hunk.before.map((i) => seq[i]!.id)), 238 255 if (moved) [replaced, formatting, searched] = [moved, true, true]; 240 257 const maybeMoved = searched || (replaced !== undefined && graph.lineFile(replaced) !== graph.lineFile(id)); @@ -277,14 +294,24 @@
278 295 skip: ReadonlySet<LineId>, 279 296 samePush: (a: Atom, b: Atom) => boolean, 297 + pushmates?: (atom: Atom) => Iterable<AtomId> | undefined, 280 298 ): LineId | undefined { 281 299 const want = normalize([text]); 282 300 if (want.length < 12) return undefined; 283 301 const all = graph.atoms(); 284 302 const exact: LineId[] = []; 285 303 const same: LineId[] = []; 286 − for (let s = Math.max(0, atom.seq - NEAR); s <= Math.min(all.length - 1, atom.seq + NEAR); s++) {
287 − const other = all[s]!;
304 + // Only atoms pushed with this one can count (samePush), so where the caller can list them, those are the candidates 305 + // (abso Capacity): reading every atom within NEAR of it was 10,000 atom reads a step on abso. Otherwise, every atom 306 + // near by, as before. 307 + const mates = pushmates?.(atom); 308 + const near = (s: number) => s >= atom.seq - NEAR && s <= atom.seq + NEAR; 309 + const candidates: Iterable<Atom> = mates 310 + ? [...mates].filter((id) => near(seqOf(id))).map((id) => graph.atom(id)) 312 + for (let s = Math.max(0, atom.seq - NEAR); s <= Math.min(all.length - 1, atom.seq + NEAR); s++) yield all[s]!; 314 + for (const other of candidates) { 288 315 if (other.author !== atom.author || !view.set.has(other.id) || !samePush(other, atom)) continue; 289 316 for (const d of other.deletes) { 290 317 if (skip.has(d)) continue; @@ -304,8 +331,7 @@
304 331 * Lines nobody could see when it was made (written later, or already 305 332 * deleted) are passed over; a line it left alone ends the hunk. 307 − function hunkOf(graph: Graph, seq: readonly SeqLine[], at: number, atom: Atom): { before: number[]; after: number[] } {
308 − const seqOf = (id: AtomId) => graph.atom(id).seq;
334 + function hunkOf(seq: readonly SeqLine[], at: number, atom: Atom): { before: number[]; after: number[] } { 309 335 const role = (s: SeqLine): "wrote" | "deleted" | "unseen" | "kept" => { 310 336 if (s.atom === atom.id) return "wrote"; 311 337 if (seqOf(s.atom) > atom.seq) return "unseen";