{"version":239,"agent":"capacity","message":"An atom's long lists kept 256 values to a row (list format 2; disk-first 2; abso Capacity).\n\nCollisions reads every key each draft atom writes. An atom's lists over 256 values were stored a row per value (readonlyStoredArray). On website, collisions read 75,000 of those rows (a draft deleting a big file has thousands of keys).\n\n- New atoms store such lists 256 values to a row (writePagedArray) and mark it listFormat 2. readonlyPagedArray reads them a page at a time, in order or by index.\n- An atom stored before keeps listFormat 1 and its row per value, and reads as before. The format is per atom record, so old and new atoms sit side by side. Only atoms stored from this version on, or a fresh adoption, get pages.\n\nOn website adopted fresh, cold, one opened writer: collisions 9.6 s to 4.7 s (79,000 reads to 3,900), matching replay (0.4 s). Adoption took 87 s instead of 119 s.\n\nTests: graph-atom-list-pages (new: a new atom's list is paged and reads whole; an atom rewritten as list format 1 reads whole). graph-atom-list-rollback now injects its failure on the list's second page. Every engine test (290 of 290), every worker/project test (341 of 341); tsc and biome clean.","time":1792066099506,"timeIso":"2026-10-15T12:08:19.506Z","changes":4,"change":3712,"files":["src/engine/graph.ts","src/engine/state-source.ts","src/engine/test/graph-atom-list-pages.test.ts","src/engine/test/graph-atom-list-rollback.test.ts"],"draft":null,"diff":[{"path":"src/engine/graph.ts","added":21,"removed":11,"diff":"--- a/src/engine/graph.ts\n+++ b/src/engine/graph.ts\n@@ -21,12 +21,14 @@\n import { withProjectionScratch } from \"./projection-scratch.ts\";\n import {\n \ttype GraphStateStorage,\n+\treadonlyPagedArray,\n \treadonlyStoredArray,\n \ttype StateMap,\n \tstoredArray,\n \tstoredLists,\n \tstoredMap,\n \tstoredNumbers,\n+\twritePagedArray,\n } from \"./state-source.ts\";\n import { StoredText } from \"./stored-text.ts\";\n import {\n@@ -353,7 +355,7 @@\n \t\t\t\tinsertionFormat?: 1;\n \t\t\t\tinsertionRuns?: string;\n \t\t\t\ttextFormat?: 1;\n-\t\t\t\tlistFormat?: 1;\n+\t\t\t\tlistFormat?: 1 | 2;\n \t\t\t\tlistCount?: number;\n \t\t\t\tlists?: Partial<Record<(typeof listFields)[number], string>>;\n \t\t\t\tatom: Atom;\n@@ -365,9 +367,9 @@\n \t\t\t\t\tconst encoded = value as unknown as Partial<EncodedAtom>;\n \t\t\t\t\tif (encoded.textFormat !== undefined && encoded.textFormat !== 1)\n \t\t\t\t\t\tthrow Error(\"invalid stored atom text format\");\n-\t\t\t\t\tif (encoded.listFormat !== undefined && encoded.listFormat !== 1)\n+\t\t\t\t\tif (encoded.listFormat !== undefined && encoded.listFormat !== 1 && encoded.listFormat !== 2)\n \t\t\t\t\t\tthrow Error(\"invalid stored atom list format\");\n-\t\t\t\t\tif (encoded.listFormat === 1) {\n+\t\t\t\t\tif (encoded.listFormat === 1 || encoded.listFormat === 2) {\n \t\t\t\t\t\tif (\n \t\t\t\t\t\t\t!encoded.lists ||\n \t\t\t\t\t\t\ttypeof encoded.lists !== \"object\" ||\n@@ -382,7 +384,10 @@\n \t\t\t\t\tif (encoded.insertionFormat !== undefined && encoded.insertionFormat !== 1)\n \t\t\t\t\t\tthrow Error(\"invalid stored insertion format\");\n \t\t\t\t\tconst atom =\n-\t\t\t\t\t\tencoded.textFormat === 1 || encoded.listFormat === 1 || encoded.insertionFormat === 1\n+\t\t\t\t\t\tencoded.textFormat === 1 ||\n+\t\t\t\t\t\tencoded.listFormat === 1 ||\n+\t\t\t\t\t\tencoded.listFormat === 2 ||\n+\t\t\t\t\t\tencoded.insertionFormat === 1\n \t\t\t\t\t\t\t? encoded.atom!\n \t\t\t\t\t\t\t: value;\n \t\t\t\t\tif (encoded.insertionFormat === 1 && (!Array.isArray(atom.inserts) || atom.inserts.length !== 0))\n@@ -462,7 +467,8 @@\n \t\t\t\t\t\t(author) => author,\n \t\t\t\t\t\ttext,\n \t\t\t\t\t);\n-\t\t\t\t\tif (encoded.listFormat !== 1 && encoded.insertionFormat !== 1) return streamed(frozen);\n+\t\t\t\t\tif (encoded.listFormat !== 1 && encoded.listFormat !== 2 && encoded.insertionFormat !== 1)\n+\t\t\t\t\t\treturn streamed(frozen);\n \t\t\t\t\tconst result = { ...frozen };\n \t\t\t\t\tif (encoded.insertionFormat === 1) {\n \t\t\t\t\t\tconst collection = `atom-inserts:${atom.seq}`;\n@@ -492,7 +498,11 @@\n \t\t\t\t\t\tif (collection === undefined) continue;\n \t\t\t\t\t\tif (typeof collection !== \"string\" || collection !== `atom-list:${atom.seq}:${field}`)\n \t\t\t\t\t\t\tthrow Error(\"invalid stored atom list reference\");\n-\t\t\t\t\t\tObject.defineProperty(result, field, { value: readonlyStoredArray(storage, collection), enumerable: true });\n+\t\t\t\t\t\tconst list =\n+\t\t\t\t\t\t\tencoded.listFormat === 2\n+\t\t\t\t\t\t\t\t? readonlyPagedArray(storage, collection)\n+\t\t\t\t\t\t\t\t: readonlyStoredArray(storage, collection);\n+\t\t\t\t\t\tObject.defineProperty(result, field, { value: list, enumerable: true });\n \t\t\t\t\t}\n \t\t\t\t\treturn streamed(Object.freeze(result));\n \t\t\t\t},\n@@ -502,9 +512,9 @@\n \t\t\t\t\tfor (const field of listFields) {\n \t\t\t\t\t\tconst values = input[field];\n \t\t\t\t\t\tif (!values || values.length <= 256) continue;\n+\t\t\t\t\t\t// 256 to a row from list format 2 on (abso Capacity); an atom stored before keeps a row per value.\n \t\t\t\t\t\tconst collection = `atom-list:${input.seq}:${field}`;\n-\t\t\t\t\t\tconst saved = storedArray<unknown>(storage, collection);\n-\t\t\t\t\t\tfor (const value of values) saved.push(value);\n+\t\t\t\t\t\twritePagedArray(storage, collection, values);\n \t\t\t\t\t\tlists[field] = collection;\n \t\t\t\t\t\tObject.defineProperty(atom, field, { value: [], enumerable: true });\n \t\t\t\t\t}\n@@ -535,12 +545,12 @@\n \t\t\t\t\t\treturn {\n \t\t\t\t\t\t\tinsertionFormat: 1,\n \t\t\t\t\t\t\tinsertionRuns: collection,\n-\t\t\t\t\t\t\t...(hasLists ? { listFormat: 1, listCount, lists } : {}),\n+\t\t\t\t\t\t\t...(hasLists ? { listFormat: 2, listCount, lists } : {}),\n \t\t\t\t\t\t\tatom: { ...atom, inserts: [] },\n \t\t\t\t\t\t};\n \t\t\t\t\t}\n \t\t\t\t\tif (!atom.inserts.some((run) => run.lines.some((line) => line.length > StoredText.chunkCharacters)))\n-\t\t\t\t\t\treturn hasLists ? { listFormat: 1, listCount, lists, atom } : atom;\n+\t\t\t\t\t\treturn hasLists ? { listFormat: 2, listCount, lists, atom } : atom;\n \t\t\t\t\tlet index = 0;\n \t\t\t\t\tconst inserts = atom.inserts.map((run) => ({\n \t\t\t\t\t\t...run,\n@@ -553,7 +563,7 @@\n \t\t\t\t\t}));\n \t\t\t\t\treturn {\n \t\t\t\t\t\ttextFormat: 1,\n-\t\t\t\t\t\t...(hasLists ? { listFormat: 1, listCount, lists } : {}),\n+\t\t\t\t\t\t...(hasLists ? { listFormat: 2, listCount, lists } : {}),\n \t\t\t\t\t\tatom: { ...atom, inserts },\n \t\t\t\t\t};\n \t\t\t\t},"},{"path":"src/engine/state-source.ts","added":64,"removed":0,"diff":"--- a/src/engine/state-source.ts\n+++ b/src/engine/state-source.ts\n@@ -583,6 +583,70 @@\n }\n \n /** Read-only array access to individually stored values, without copying the list. */\n+/** How many values an atom's long list keeps to a row (readonlyPagedArray). */\n+export const LIST_PAGE = 256;\n+\n+/** Write a list as readonlyPagedArray reads it: its values LIST_PAGE to a row, and its length. */\n+export function writePagedArray(storage: GraphStateStorage, name: string, values: readonly unknown[]): void {\n+\tfor (let start = 0; start < values.length; start += LIST_PAGE)\n+\t\tstorage.write(`${name}:pages`, String(start / LIST_PAGE), values.slice(start, start + LIST_PAGE));\n+\tstorage.write(`${name}:meta`, \"length\", values.length);\n+}\n+\n+/**\n+ * A stored list read a page of LIST_PAGE values at a time (abso Capacity): an atom's long lists, which\n+ * readonlyStoredArray reads a row per value. Collisions read every key a draft's atoms write; a draft deleting a big\n+ * file has thousands, and that was a row each.\n+ */\n+export function readonlyPagedArray<T>(storage: GraphStateStorage, name: string): readonly T[] {\n+\tconst length = () => {\n+\t\tconst count = storage.read(`${name}:meta`, \"length\");\n+\t\tif (!Number.isSafeInteger(count) || (count as number) < 0) throw Error(\"invalid stored list length\");\n+\t\treturn count as number;\n+\t};\n+\tlength();\n+\tlet held = -1;\n+\tlet page: readonly unknown[] = [];\n+\tconst at = (index: number): T => {\n+\t\tconst p = Math.floor(index / LIST_PAGE);\n+\t\tif (p !== held) {\n+\t\t\tconst raw = storage.read(`${name}:pages`, String(p));\n+\t\t\tif (!Array.isArray(raw)) throw Error(\"missing stored list page\");\n+\t\t\tpage = raw;\n+\t\t\theld = p;\n+\t\t}\n+\t\tconst value = page[index % LIST_PAGE];\n+\t\tif (value === undefined) throw Error(\"missing stored list element\");\n+\t\treturn (value && typeof value === \"object\" ? Object.freeze(value) : value) as T;\n+\t};\n+\tfunction* inOrder(): IterableIterator<T> {\n+\t\tconst count = length();\n+\t\tfor (let index = 0; index < count; index++) yield at(index);\n+\t}\n+\treturn new Proxy([] as T[], {\n+\t\tget(target, key, receiver) {\n+\t\t\tif (key === \"length\") return length();\n+\t\t\tif (key === Symbol.iterator || key === \"values\") return inOrder;\n+\t\t\tif (indexKey(key)) return Number(key) >= length() ? undefined : at(Number(key));\n+\t\t\treturn Reflect.get(target, key, receiver);\n+\t\t},\n+\t\thas(target, key) {\n+\t\t\treturn indexKey(key) ? Number(key) < length() : Reflect.has(target, key);\n+\t\t},\n+\t\townKeys() {\n+\t\t\treturn [...Array.from({ length: length() }, (_, index) => String(index)), \"length\"];\n+\t\t},\n+\t\tgetOwnPropertyDescriptor(target, key) {\n+\t\t\tif (indexKey(key) && Number(key) < length())\n+\t\t\t\treturn { configurable: true, enumerable: true, writable: false, value: at(Number(key)) };\n+\t\t\treturn Reflect.getOwnPropertyDescriptor(target, key);\n+\t\t},\n+\t\tset() {\n+\t\t\tthrow Error(\"stored lists are read-only\");\n+\t\t},\n+\t});\n+}\n+\n export function readonlyStoredArray<T>(\n \tstorage: GraphStateStorage,\n \tname: string,"},{"path":"src/engine/test/graph-atom-list-pages.test.ts","added":71,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/graph-atom-list-pages.test.ts\n@@ -0,0 +1,71 @@\n+// An atom's long lists (abso Capacity, readonlyPagedArray): a new atom keeps them 256 values to a row (list format 2),\n+// and an atom stored before keeps a row per value (list format 1). Both read back whole, in order and by index.\n+\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { buildBase, Graph } from \"../index.ts\";\n+import { lineKey } from \"../keys.ts\";\n+import { storedArray } from \"../state-source.ts\";\n+import type { Atom } from \"../types.ts\";\n+import { SqlState } from \"./storage-helper.ts\";\n+\n+function listAtom(keys: string[]): Atom {\n+\treturn {\n+\t\tid: \"a1\",\n+\t\tseq: 1,\n+\t\tauthor: \"fixture\",\n+\t\tmessage: \"lists\",\n+\t\ttime: 1,\n+\t\tinserts: [],\n+\t\tdeletes: [],\n+\t\twrites: keys,\n+\t\treads: [],\n+\t\tholds: [],\n+\t\tneeds: [],\n+\t\tresolves: [],\n+\t\treasons: [],\n+\t\tdeps: [\"a0\"],\n+\t};\n+}\n+\n+const keys = Array.from({ length: 600 }, () => lineKey(\"a0.0\"));\n+\n+test(\"a new atom's long list is kept 256 values to a row and reads back whole\", () => {\n+\tconst storage = new SqlState();\n+\ttry {\n+\t\tconst graph = new Graph({ storage });\n+\t\tgraph.add(buildBase({ \"a.txt\": \"base\" }, { author: \"fixture\", message: \"base\", time: 0 }));\n+\t\tgraph.add(listAtom(keys));\n+\t\tassert.equal((storage.read(\"atoms\", \"1\") as { listFormat?: number }).listFormat, 2);\n+\t\tassert.equal((storage.read(\"atom-list:1:writes:pages\", \"2\") as unknown[]).length, 600 - 512);\n+\t\tassert.equal(storage.read(\"atom-list:1:writes\", \"0\"), undefined, \"no row per value\");\n+\t\tconst writes = new Graph({ storage }).atom(\"a1\").writes;\n+\t\tassert.deepEqual([...writes], keys);\n+\t\tassert.equal(writes.length, 600);\n+\t\tassert.equal(writes[300], keys[300]);\n+\t\tassert.equal(writes[600], undefined);\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});\n+\n+test(\"an atom stored before pages, a row per value, still reads back whole\", () => {\n+\tconst storage = new SqlState();\n+\ttry {\n+\t\tconst graph = new Graph({ storage });\n+\t\tgraph.add(buildBase({ \"a.txt\": \"base\" }, { author: \"fixture\", message: \"base\", time: 0 }));\n+\t\tgraph.add(listAtom(keys));\n+\t\t// Rewrite it as list format 1 kept it: a row per value and no pages.\n+\t\tconst record = storage.read(\"atoms\", \"1\") as Record<string, unknown>;\n+\t\tfor (let page = 0; page < 3; page++) storage.remove(\"atom-list:1:writes:pages\", String(page));\n+\t\tstorage.remove(\"atom-list:1:writes:meta\", \"length\");\n+\t\tconst rows = storedArray<string>(storage, \"atom-list:1:writes\");\n+\t\tfor (const key of keys) rows.push(key);\n+\t\tstorage.write(\"atoms\", \"1\", { ...record, listFormat: 1 });\n+\t\tconst writes = new Graph({ storage }).atom(\"a1\").writes;\n+\t\tassert.deepEqual([...writes], keys);\n+\t\tassert.equal(writes[599], keys[599]);\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});"},{"path":"src/engine/test/graph-atom-list-rollback.test.ts","added":2,"removed":1,"diff":"--- a/src/engine/test/graph-atom-list-rollback.test.ts\n+++ b/src/engine/test/graph-atom-list-rollback.test.ts\n@@ -31,7 +31,8 @@\n \t\tconst write = storage.write.bind(storage);\n \t\tstorage.write = (collection, key, value) => {\n \t\t\twrite(collection, key, value);\n-\t\t\tif (collection === \"atom-list:1:writes\" && key === \"128\") throw Error(\"injected list persistence failure\");\n+\t\t\t// Partway through: the second of the list's two pages (256 values to a page).\n+\t\t\tif (collection === \"atom-list:1:writes:pages\" && key === \"1\") throw Error(\"injected list persistence failure\");\n \t\t};\n \t\tassert.throws(() => graph.add(atom), /injected list persistence failure/);\n \t\tassert.deepEqual(storage.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all(), before);"}],"ci":null}