Packed layout follows lines written in another file (disk-first 2; abso Capacity). Version 221 of shared· capacity· Oct 15 (2026-10-15 07:19 UTC) · Not checked
where {} on typescript-rs left 11 lines out of erasable_syntax.rs in cloud-ts-12's draft. cloud-ts-12 moved one block between files several times, wrote lines under its last line while it sat in uninitialized_props_forms.rs, then moved the block on. Those lines followed their parent with no move of their own. The packed layout read only the file's own lines and the files its moved-in lines were written in, so it never read them.
Each file's packed history also keeps the runs written in another file under one of its lines (file-crossings). The packed layout follows them until no new file turns up. New adoptions write packed format 2. A graph packed at format 1 has no such list, so it lays files out line by line: correct, but slower until it is adopted again. packed-crossings.test.ts moves c.ts to d.ts, adds a line in d.ts, moves d.ts to e.ts, and checks files, where and every draft's text against the in-memory project. Named tests: packed-crossings, packed-files, state-source, draft-reads-stored, version-files, project-writer-storage, writer-adoption, moves, queued-moves, files-reads, move-reconstruction (60 of 60); tsc and biome clean.
Version 220 · Files at version 221
3 files, 103 lines added, 11 removed.
--- a/src/engine/graph.ts
+++ b/src/engine/graph.ts
@@ -262,6 +262,8 @@
262 262 export type PackedDelete = [number, number]; 263 263 /** An attribute set on a file: [seq, name, value]. */ 264 264 export type PackedAttr = [number, string, string | null]; 265 + /** A run written in another file under one of a file's lines: [the parent's n, the run's file index]. */ 266 + export type PackedCrossing = [number, number]; 265 267 const EMPTY_NUMBERS: readonly number[] = Object.freeze([]); 266 268 /** Lines longer than this aren't packed with their file; their text is read on its own. */ 267 269 const PACKED_TEXT = 4096; @@ -599,13 +601,16 @@
599 601 this.#fileTexts = storedLists<string | null>(storage, "file-texts"); 600 602 this.#fileDeletes = storedLists<PackedDelete>(storage, "file-deletes"); 601 603 this.#fileAttrs = storedLists<PackedAttr>(storage, "file-attrs"); 604 + this.#fileCrossings = storedLists<PackedCrossing>(storage, "file-crossings"); 602 605 this.#packFiles = options.packFiles ?? true; 606 609 // Each file's line history packed, in pages (abso Capacity) --------------------------------------------------- 607 610 // For each file, by its path index: every line written in it ([n, parent code, writer seq, k, replaces]) and its 608 − // text, every delete of one of those lines ([n, deleter seq]), and every attribute set on it ([seq, name, value]).
611 + // text, every delete of one of those lines ([n, deleter seq]), every attribute set on it ([seq, name, value]), and 612 + // every run written in another file under one of its lines ([parent n, the run's file]): a line follows its parent, 613 + // so a run written while its parent was moved elsewhere lays out wherever the parent is. 609 614 // Appended as atoms are added, so a view lays a file out from a few pages instead of a row read per line. Kept from 610 615 // a fresh adoption on; a graph adopted before has none, and views lay out as before. @@ -613,19 +618,39 @@
613 618 #fileTexts: ReturnType<typeof storedLists<string | null>> | undefined; 614 619 #fileDeletes: ReturnType<typeof storedLists<PackedDelete>> | undefined; 615 620 #fileAttrs: ReturnType<typeof storedLists<PackedAttr>> | undefined; 621 + #fileCrossings: ReturnType<typeof storedLists<PackedCrossing>> | undefined; 616 622 /** Whether a new stored graph starts packing (GraphOptions.packFiles). */ 617 623 #packFiles = true; 618 − /** Whether this graph's files are packed: read once, when first asked, so opening reads nothing. */
619 − #packedKnown: boolean | undefined;
625 + * How this graph's files are packed, read once when first asked so opening reads nothing: 0 not at all, 1 lines, 626 + * deletes and attributes, 2 the runs written across files as well. 628 + #formatKnown: number | undefined; 629 + get #format(): number { 630 + if (this.#formatKnown === undefined) { 631 + const format = this.#storage?.read("file-lines:meta", "format"); 632 + this.#formatKnown = format === 1 || format === 2 ? format : 0; 634 + return this.#formatKnown; 620 636 get #packed(): boolean { 621 − this.#packedKnown ??= !!this.#storage && this.#storage.read("file-lines:meta", "format") === 1;
622 − return this.#packedKnown;
637 + return this.#format >= 1; 639 + get #crossings(): boolean { 640 + return this.#format >= 2; 625 643 /** Whether this graph keeps each file's line history packed. */ 626 644 get packedFiles(): boolean { 627 645 return this.#packed; 648 + * Whether a file can be laid out from packed history alone: it also keeps the runs written across files. A graph 649 + * packed before those were kept lays files out line by line. 651 + get packedLayout(): boolean { 652 + return this.#crossings; 629 654 /** A path's index, or undefined when nothing was ever written or set in it. */ 630 655 pathIndexOf(path: string): number | undefined { 631 656 return this.#pathIndex.get(path); @@ -646,6 +671,9 @@
646 671 *packedAttrs(file: number): IterableIterator<PackedAttr> { 647 672 yield* this.#fileAttrs!.values(String(file)); 674 + *packedCrossings(file: number): IterableIterator<PackedCrossing> { 675 + yield* this.#fileCrossings!.values(String(file)); 650 678 /** Where an atom's key and delete lists are when the graph doesn't keep them (#46): the stored atom. */ 651 679 #lists: ((seq: number) => AtomLists) | undefined; @@ -1449,8 +1477,8 @@
1449 1477 this.#storage.write("ordered-children", "format", 2); 1451 1479 if (this.#storage && this.#atoms.length === 0 && this.#packFiles) { 1452 − this.#storage.write("file-lines:meta", "format", 1);
1453 − this.#packedKnown = true;
1480 + this.#storage.write("file-lines:meta", "format", 2); 1481 + this.#formatKnown = 2; 1456 1484 if (this.#storage) this.withProjectionScratch((scratch) => this.#validateAcceptedAtom(atom, scratch)); @@ -1535,6 +1563,10 @@
1535 1563 else pushOne(this.#runStarts, parent, first); 1536 1564 const replacesParent = deletes(run.parent) ? 1 : 0; 1537 1565 if (this.#packed) { 1566 + if (this.#crossings && parent >= 0) { 1567 + const from = this.#lineFile.at(parent); 1568 + if (from !== file) this.#fileCrossings!.append(String(from), [parent, file]); 1538 1570 const base = this.#lineBase[seq]!; 1539 1571 this.#fileLines!.appendAll( --- a/src/engine/view.ts
+++ b/src/engine/view.ts
@@ -597,7 +597,7 @@
597 597 /** Lay out one file: its line tree from the set's atoms, linearized. */ 598 598 #layout(path: string, complete: boolean): FileState { 599 599 const graph = this.graph; 600 − if (graph.packedFiles && !this.#scratchMoves()) return this.#layoutPacked(path, complete);
600 + if (graph.packedLayout && !this.#scratchMoves()) return this.#layoutPacked(path, complete); 601 601 if (this.#scratchMoves()) 602 602 return graph.withProjectionScratch((scratch) => 603 603 this.#layoutResolved( @@ -632,9 +632,9 @@
635 − * Lay one file out from its packed history (abso Capacity, Graph.packedLines): the lines written in it, and in the
636 − * files its moved-in lines came from, read a page at a time; the same tree and order as #layoutResolved, built in
637 − * memory with no read per line.
635 + * Lay one file out from its packed history (abso Capacity, Graph.packedLines): the lines written in it, in the files 636 + * its moved-in lines came from, and in the files runs under any of those lines were written in, read a page at a 637 + * time; the same tree and order as #layoutResolved, built in memory with no read per line. 639 639 #layoutPacked(path: string, complete: boolean): FileState { 640 640 const graph = this.graph; @@ -670,6 +670,8 @@
670 670 if (by) by.push(deleter); 671 671 else deleters.set(n, [deleter]); 673 + // Runs written elsewhere under this file's lines follow them; the loop reads their files too. 674 + for (const [parent, into] of graph.packedCrossings(file)) if (texts.has(parent)) files.add(into); 674 676 const order = (a: Child, b: Child) => 675 677 b.replaces - a.replaces || b.lamport - a.lamport || b.seq - a.seq || a.k - b.k; --- /dev/null
+++ b/worker/project/test/packed-crossings.test.ts
@@ -0,0 +1,58 @@
1 + // Packed file layout and lines written across files (abso Capacity). A line can be written in one file under a 2 + // parent written in another: the parent was moved there. When a later move takes the parent on, the line follows 3 + // it without a move of its own. Laid out from packed history, the file must still hold those lines, as the 4 + // in-memory project's does. 6 + import assert from "node:assert/strict"; 7 + import { test } from "node:test"; 8 + import { storedArray } from "../../../src/engine/state-source.ts"; 9 + import { SqlState } from "../../../src/engine/test/storage-helper.ts"; 10 + import { MemoryStore, Project } from "../project.ts"; 11 + import type { Entry, ProjectStore } from "../project-types.ts"; 12 + import { where } from "../where.ts"; 15 + const storage = new SqlState(); 16 + const entries = storedArray<Entry>(storage, "canonical-log"); 17 + const store: ProjectStore = { 19 + for (const entry of batch) entries.push(entry); 22 + return new Project(store, { writerStorage: storage }); 25 + const C = "const x = 1;\nconst y = 2;\n"; 27 + function history(project: Project) { 29 + project.init({ "c.ts": C, "other.ts": "other\n" }, t); 31 + project.move("ann", "c.ts", "d.ts", ++t); 32 + assert.equal(project.publish("ann", "Move c to d", ++t).ok, true); 33 + // bob adds a line at the end of d.ts: written in d.ts, under a line written in c.ts. 35 + project.record("bob", { "d.ts": `${C}const z = 3;\n` }, ++t); 36 + assert.equal(project.publish("bob", "Add z", ++t).ok, true); 37 + // dee moves d.ts on to e.ts. bob's line follows its parent there with no move of its own. 39 + project.move("dee", "d.ts", "e.ts", ++t); 40 + project.record("dee", { "other.ts": "other\nmore\n" }, ++t); 41 + project.sync("idle"); 44 + const text = (project: Project, agent: string, file: string) => 45 + (project.draftOf(agent)?.view ?? project.sharedView()).text(file); 47 + test("a file laid out from packed history keeps lines written in another file under its own lines", () => { 48 + const oracle = new Project(new MemoryStore()); 49 + const project = writer(); 52 + assert.equal(text(oracle, "dee", "e.ts"), `${C}const z = 3;\n`); 53 + assert.deepEqual(project.files(), oracle.files()); 54 + assert.deepEqual(where(project), where(oracle)); 55 + for (const agent of ["ann", "bob", "cy", "dee", "idle"]) 56 + for (const file of ["c.ts", "d.ts", "e.ts"]) 57 + assert.equal(text(project, agent, file), text(oracle, agent, file), `${agent} ${file}`);