{"version":242,"agent":"capacity","message":"why: reasons pointing at a change indexed, and a moved line looked for among its push (disk-first 2; abso Capacity).\n\nwhy on abso, warm, one kept reader: 2.9 s a call with about 24,000 reads, 20,000 of them atoms.\n\n- 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.\n- 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.\n- trail takes atoms' numbers from their ids instead of reading each atom.\n\nOn 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.\n\nNew 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.","time":1792072012356,"timeIso":"2026-10-15T13:46:52.356Z","changes":5,"change":3739,"files":["src/engine/graph.ts","src/engine/test/reasons-to.test.ts","worker/project/blame.ts","worker/project/project.ts","worker/project/trail.ts"],"draft":null,"diff":[{"path":"src/engine/graph.ts","added":32,"removed":0,"diff":"--- a/src/engine/graph.ts\n+++ b/src/engine/graph.ts\n@@ -612,6 +612,7 @@\n \t\t\tthis.#dependentsPages = storedLists<AtomId>(storage, \"dependents\");\n \t\t\tthis.#resolversPages = storedLists<AtomId>(storage, \"resolvers\");\n \t\t\tthis.#togetherPages = storedLists<TogetherLink>(storage, \"together\");\n+\t\t\tthis.#reasonsInPages = storedLists<AtomId>(storage, \"reasons-in\");\n \t\t\tthis.#fileAtomPages = storedLists<AtomId>(storage, \"file-atoms\");\n \t\t\tthis.#runStartPages = storedLists<number>(storage, \"run-starts\", (key) =>\n \t\t\t\tJSON.stringify([\"number\", Number(key)]),\n@@ -803,6 +804,16 @@\n \t#resolversPages: ReturnType<typeof storedLists<AtomId>> | undefined;\n \t#together: StateMap<AtomId, TogetherLink[]> = new Map();\n \t#togetherPages: ReturnType<typeof storedLists<TogetherLink>> | undefined;\n+\t/** The atoms whose reasons point at each atom, in seq order, once each (abso Capacity). */\n+\t#reasonsIn = new Map<AtomId, AtomId[]>();\n+\t#reasonsInPages: ReturnType<typeof storedLists<AtomId>> | undefined;\n+\t/** Whether a stored graph keeps reasons-in: from a fresh graph on, worked out when first needed. */\n+\t#reasonsInKnown: boolean | undefined;\n+\tget #reasonsInKept(): boolean {\n+\t\tif (!this.#storage) return true;\n+\t\tthis.#reasonsInKnown ??= this.#storage.read(\"reasons-in:meta\", \"kept\") === 1;\n+\t\treturn this.#reasonsInKnown;\n+\t}\n \t#fileAtoms: StateMap<string, AtomId[]> = new Map();\n \t#fileAtomPages: ReturnType<typeof storedLists<AtomId>> | undefined;\n \t/** First lines of runs (their n), by the node they were written after: a line's n, or -1 - a path's index. */\n@@ -1345,6 +1356,16 @@\n \t\telse yield* listOf(this.#resolvers.get(id));\n \t}\n \n+\t/**\n+\t * The atoms whose reasons point at this one, in seq order, once each; or undefined when a stored graph made before\n+\t * this index keeps none, and the caller looks at every atom (abso Capacity).\n+\t */\n+\treasonsTo(id: AtomId): readonly AtomId[] | undefined {\n+\t\tif (!this.#reasonsInKept) return undefined;\n+\t\tif (this.#reasonsInPages) return [...this.#reasonsInPages.values(id)];\n+\t\treturn this.#reasonsIn.get(id) ?? [];\n+\t}\n+\n \t/** `together` reasons touching this atom, in either direction. */\n \ttogetherLinks(id: AtomId): readonly TogetherLink[] {\n \t\treturn [...this.eachTogetherLink(id)];\n@@ -1546,6 +1567,8 @@\n \t\t}\n \t\tif (this.#storage && this.#atoms.length === 0 && this.#packFiles) {\n \t\t\tthis.#storage.write(\"file-lines:meta\", \"format\", 2);\n+\t\t\tthis.#storage.write(\"reasons-in:meta\", \"kept\", 1);\n+\t\t\tthis.#reasonsInKnown = true;\n \t\t\tthis.#formatKnown = 2;\n \t\t}\n \n@@ -1723,6 +1746,15 @@\n \t\t\tif (this.#fileAtomPages) this.#fileAtomPages.append(file, frozen.id);\n \t\t\telse pushIndex(this.#fileAtoms, file, frozen.id);\n \t\t}\n+\t\tif (this.#reasonsInKept) {\n+\t\t\tconst pointed = new Set<AtomId>();\n+\t\t\tfor (const r of frozen.reasons) {\n+\t\t\t\tif (pointed.has(r.to)) continue;\n+\t\t\t\tpointed.add(r.to);\n+\t\t\t\tif (this.#reasonsInPages) this.#reasonsInPages.append(r.to, frozen.id);\n+\t\t\t\telse pushIndex(this.#reasonsIn, r.to, frozen.id);\n+\t\t\t}\n+\t\t}\n \t\tfor (const r of frozen.reasons) {\n \t\t\tif (r.kind !== \"together\" || r.to === frozen.id) continue;\n "},{"path":"src/engine/test/reasons-to.test.ts","added":56,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/reasons-to.test.ts\n@@ -0,0 +1,56 @@\n+// The atoms whose reasons point at an atom (abso Capacity, Graph.reasonsTo): why's \"reasons in\" reads them instead of\n+// every atom. In seq order, once each, whatever the kind and however many reasons one atom gives; kept from a fresh\n+// stored graph on, and undefined on a stored graph made before, where the caller looks at every atom.\n+\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { Graph } from \"../index.ts\";\n+import { lines, Repo } from \"./helpers.ts\";\n+import { SqlState } from \"./storage-helper.ts\";\n+\n+function history() {\n+\tconst repo = Repo.init({ \"f.txt\": lines(\"one\", \"two\", \"three\") });\n+\tconst x = repo.edit({ author: \"ann\", message: \"x\", edits: [{ file: \"f.txt\", old: \"one\", new: \"ONE\" }] });\n+\trepo.edit({\n+\t\tauthor: \"bob\",\n+\t\tmessage: \"y\",\n+\t\tedits: [{ file: \"f.txt\", old: \"two\", new: \"TWO\" }],\n+\t\treasons: [\n+\t\t\t{ to: x.id, kind: \"together\", text: \"same idea\" },\n+\t\t\t{ to: x.id, kind: \"because-of\", text: \"and because of it\" },\n+\t\t],\n+\t});\n+\trepo.edit({ author: \"cy\", message: \"unrelated\", edits: [{ file: \"f.txt\", old: \"three\", new: \"3\" }] });\n+\trepo.edit({\n+\t\tauthor: \"dee\",\n+\t\tmessage: \"z\",\n+\t\tedits: [{ file: \"f.txt\", old: \"3\", new: \"THREE\" }],\n+\t\treasons: [{ to: x.id, kind: \"supersedes\", text: \"replaces it\" }],\n+\t});\n+\treturn { repo, x };\n+}\n+\n+/** Every atom whose reasons point at id, by looking at each one: what reasonsTo must give. */\n+const scanned = (graph: Graph, id: string) =>\n+\t[...graph.atoms()].filter((a) => a.reasons.some((r) => r.to === id)).map((a) => a.id);\n+\n+test(\"reasonsTo lists the atoms pointing at an atom, in seq order, once each\", () => {\n+\tconst { repo, x } = history();\n+\tconst want = scanned(repo.graph, x.id);\n+\tassert.equal(want.length, 2, \"bob's two reasons and dee's\");\n+\tassert.deepEqual(repo.graph.reasonsTo(x.id), want);\n+\tconst storage = new SqlState();\n+\tconst stored = new Graph({ storage });\n+\tfor (const atom of repo.graph.atoms()) stored.add(atom);\n+\tassert.deepEqual(new Graph({ storage }).reasonsTo(x.id), want);\n+\tassert.deepEqual(new Graph({ storage }).reasonsTo(\"a0\"), []);\n+});\n+\n+test(\"a stored graph made before the index keeps none, and says so\", () => {\n+\tconst { repo, x } = history();\n+\tconst storage = new SqlState();\n+\tconst stored = new Graph({ storage });\n+\tfor (const atom of repo.graph.atoms()) stored.add(atom);\n+\tstorage.remove(\"reasons-in:meta\", \"kept\");\n+\tassert.equal(new Graph({ storage }).reasonsTo(x.id), undefined);\n+});"},{"path":"worker/project/blame.ts","added":10,"removed":2,"diff":"--- a/worker/project/blame.ts\n+++ b/worker/project/blame.ts\n@@ -226,7 +226,9 @@\n \t\ttoMessage: graph.atom(r.to).message,\n \t}));\n \tconst reasonsIn: { from: AtomId; kind: string; text: string; fromAuthor: string; fromMessage: string }[] = [];\n-\tfor (const other of graph.atoms()) {\n+\t// The atoms that point at it, where the graph keeps them (abso Capacity): looking at every atom read each one.\n+\tconst pointing = graph.reasonsTo(atom.id);\n+\tfor (const other of pointing ? pointing.map((id) => graph.atom(id)) : graph.atoms()) {\n \t\tfor (const r of other.reasons) {\n \t\t\tif (r.to === atom.id)\n \t\t\t\treasonsIn.push({\n@@ -305,7 +307,13 @@\n \tconst graph = project.graph;\n \t// Changes pushed together: the same version of the same space, or both still in one agent's draft.\n \tconst samePush = (a: Atom, b: Atom) => samePublish(publishOf(project, a.id), publishOf(project, b.id));\n-\tconst { steps, more } = trail(graph, view, file, line, undefined, samePush);\n+\t// What was pushed with a published atom is its version's atoms; an atom still in a draft goes with every other one,\n+\t// so the trail looks near by as before.\n+\tconst pushmates = (a: Atom) => {\n+\t\tconst p = publishOf(project, a.id);\n+\t\treturn p ? project.versionAtoms(p.version, p.draft) : undefined;\n+\t};\n+\tconst { steps, more } = trail(graph, view, file, line, undefined, samePush, pushmates);\n \tconst groups: { steps: TrailStep[]; publish: Publish | null }[] = [];\n \tfor (const s of steps) {\n \t\tconst publish = publishOf(project, s.atom);"},{"path":"worker/project/project.ts","added":6,"removed":0,"diff":"--- a/worker/project/project.ts\n+++ b/worker/project/project.ts\n@@ -834,6 +834,12 @@\n \t\treturn p && { agent: p.agent, message: p.message, time: p.time };\n \t}\n \n+\t/** The atoms a version of shared, or of a named draft, added to its space: what was pushed together (blame's trail). */\n+\tversionAtoms(version: number, draft: string | null = null): readonly AtomId[] {\n+\t\tconst space = draft === null ? this.#shared : this.#spaces.get(draft);\n+\t\treturn space?.versions[version]?.atoms ?? [];\n+\t}\n+\n \t/** The graph, for reading (blame, history). Don't add to it. */\n \tget graph(): Graph {\n \t\treturn this.#graph;"},{"path":"worker/project/trail.ts","added":33,"removed":7,"diff":"--- a/worker/project/trail.ts\n+++ b/worker/project/trail.ts\n@@ -12,7 +12,15 @@\n // in another (a file split in two), which the trail follows into the file it\n // came from.\n \n-import type { Atom, AtomId, Graph, LineId, ReadView, SeqLine } from \"../../src/engine/index.ts\";\n+import {\n+\ttype Atom,\n+\ttype AtomId,\n+\ttype Graph,\n+\ttype LineId,\n+\ttype ReadView,\n+\ttype SeqLine,\n+\tseqOf,\n+} from \"../../src/engine/index.ts\";\n \n /** One change in a line's past, newest first. */\n export interface TrailStep {\n@@ -194,6 +202,7 @@\n \tline: LineId,\n \tmax = TRAIL_MAX,\n \tsamePush: (a: Atom, b: Atom) => boolean = (a, b) => a.id === b.id,\n+\tpushmates?: (atom: Atom) => Iterable<AtomId> | undefined,\n ): { steps: TrailStep[]; more: boolean } {\n \tconst files = new Map<string, { seq: readonly SeqLine[]; index: Map<LineId, number> }>();\n \tconst sequenceOf = (path: string) => {\n@@ -218,7 +227,7 @@\n \t\t\tconst { seq, index } = sequenceOf(path);\n \t\t\tconst at = index.get(id);\n \t\t\tif (at === undefined) continue;\n-\t\t\tconst hunk = hunkOf(graph, seq, at, graph.atom(seq[at]!.atom));\n+\t\t\tconst hunk = hunkOf(seq, at, graph.atom(seq[at]!.atom));\n \t\t\tconst p = pairIn(\n \t\t\t\thunk.before.map((i) => seq[i]!.text),\n \t\t\t\thunk.after.map((i) => seq[i]!.text),\n@@ -234,7 +243,15 @@\n \t\tlet formatting = pair?.formatting ?? false;\n \t\tlet searched = false;\n \t\tif (replaced === undefined) {\n-\t\t\tconst moved = movedFrom(graph, view, atom, seq[at]!.text, new Set(hunk.before.map((i) => seq[i]!.id)), samePush);\n+\t\t\tconst moved = movedFrom(\n+\t\t\t\tgraph,\n+\t\t\t\tview,\n+\t\t\t\tatom,\n+\t\t\t\tseq[at]!.text,\n+\t\t\t\tnew Set(hunk.before.map((i) => seq[i]!.id)),\n+\t\t\t\tsamePush,\n+\t\t\t\tpushmates,\n+\t\t\t);\n \t\t\tif (moved) [replaced, formatting, searched] = [moved, true, true];\n \t\t}\n \t\tconst maybeMoved = searched || (replaced !== undefined && graph.lineFile(replaced) !== graph.lineFile(id));\n@@ -277,14 +294,24 @@\n \ttext: string,\n \tskip: ReadonlySet<LineId>,\n \tsamePush: (a: Atom, b: Atom) => boolean,\n+\tpushmates?: (atom: Atom) => Iterable<AtomId> | undefined,\n ): LineId | undefined {\n \tconst want = normalize([text]);\n \tif (want.length < 12) return undefined;\n \tconst all = graph.atoms();\n \tconst exact: LineId[] = [];\n \tconst same: LineId[] = [];\n-\tfor (let s = Math.max(0, atom.seq - NEAR); s <= Math.min(all.length - 1, atom.seq + NEAR); s++) {\n-\t\tconst other = all[s]!;\n+\t// Only atoms pushed with this one can count (samePush), so where the caller can list them, those are the candidates\n+\t// (abso Capacity): reading every atom within NEAR of it was 10,000 atom reads a step on abso. Otherwise, every atom\n+\t// near by, as before.\n+\tconst mates = pushmates?.(atom);\n+\tconst near = (s: number) => s >= atom.seq - NEAR && s <= atom.seq + NEAR;\n+\tconst candidates: Iterable<Atom> = mates\n+\t\t? [...mates].filter((id) => near(seqOf(id))).map((id) => graph.atom(id))\n+\t\t: (function* () {\n+\t\t\t\tfor (let s = Math.max(0, atom.seq - NEAR); s <= Math.min(all.length - 1, atom.seq + NEAR); s++) yield all[s]!;\n+\t\t\t})();\n+\tfor (const other of candidates) {\n \t\tif (other.author !== atom.author || !view.set.has(other.id) || !samePush(other, atom)) continue;\n \t\tfor (const d of other.deletes) {\n \t\t\tif (skip.has(d)) continue;\n@@ -304,8 +331,7 @@\n  * Lines nobody could see when it was made (written later, or already\n  * deleted) are passed over; a line it left alone ends the hunk.\n  */\n-function hunkOf(graph: Graph, seq: readonly SeqLine[], at: number, atom: Atom): { before: number[]; after: number[] } {\n-\tconst seqOf = (id: AtomId) => graph.atom(id).seq;\n+function hunkOf(seq: readonly SeqLine[], at: number, atom: Atom): { before: number[]; after: number[] } {\n \tconst role = (s: SeqLine): \"wrote\" | \"deleted\" | \"unseen\" | \"kept\" => {\n \t\tif (s.atom === atom.id) return \"wrote\";\n \t\tif (seqOf(s.atom) > atom.seq) return \"unseen\";"}],"ci":null}