{"version":247,"agent":"capacity","message":"A kept reader settles after each read; line texts weigh what V8 holds (abso Capacity, issue 664).\n\nWhat a kept reader held on abso, measured per question without the answer itself (heap snapshot after tree with authors, 56.9 MB held): the view cache 38.3 MB against its 24 MB budget, the agents read 6.5 to 7.7 MB, the move skeleton 2.4 MB, the state cache 2.3 MB; the rest is the engine's indexes.\n\n- Project.afterRead: the view cache trimmed to its budget (ViewCache.trim) and the agents the read decoded let go (WriterAgents.release). Views grow while a call lays files out and the cache weighed them only when asked for again; a read-only reader never flushed its agents, so it held every agent's news told.\n- View weight: a laid-out line's text counts a 16-byte header and a byte a character (two outside Latin-1), rounded to 8, as V8 holds it. It counted 8 and two a character, so abso's whole shared layout (22.7 MB held) weighed about 28 MB, over the budget alone: once trimmed, it was laid out again for every question (blame folder 2.5 s became 14.4 s).\n\nFull warm run on the abso export: held heap at most 41.7 MB (was 56.9), 24 to 29 MB between the big reads (was 32 to 37); every answer the same. Status and drafts take about 30 ms more (agents read again); blame folder 1.7 s instead of 2.5 s.\n\nTests: view-cache (trim reweighs growth with no further hit), after-read (new: reads before and after it, and a write after it, against the in-memory project), and the collision, path and compare tests: 21 of 21.","time":1792081233526,"timeIso":"2026-10-15T16:20:33.526Z","changes":6,"change":3839,"files":["src/engine/view.ts","worker/project/project.ts","worker/project/test/after-read.test.ts","worker/project/test/view-cache.test.ts","worker/project/views.ts","worker/project/writer-agents.ts"],"draft":null,"diff":[{"path":"src/engine/view.ts","added":10,"removed":1,"diff":"--- a/src/engine/view.ts\n+++ b/src/engine/view.ts\n@@ -74,7 +74,7 @@\n \t// FileState and FileAttrs are per laid-out file; typed-array headers are separate from their payload byte lengths.\n \tn = OBJECT_BYTES + OBJECT_BYTES + 2 * ARRAY_HEADER_BYTES;\n \tn += f.seq.byteLength + f.vis.byteLength;\n-\tif (f.visText) for (const text of f.visText) n += REFERENCE_BYTES + 2 * (text?.length ?? 0);\n+\tif (f.visText) for (const text of f.visText) n += REFERENCE_BYTES + (text === null ? 0 : stringBytes(text));\n \tif (f.visWriter) n += 4 * f.visWriter.length;\n \tif (f.visK) n += 4 * f.visK.length;\n \tif (f.seqWriter) n += 4 * f.seqWriter.length;\n@@ -131,7 +131,16 @@\n const MAP_ENTRY_BYTES = 40;\n const SET_ENTRY_BYTES = 32;\n const REFERENCE_BYTES = 8;\n+const STRING_HEADER_BYTES = 16;\n /**\n+ * What V8 holds for a string of its own (abso Capacity, measured on abso's layouts): a header, then one byte a\n+ * character when every character fits in Latin-1 and two otherwise, rounded up to 8.\n+ */\n+function stringBytes(text: string): number {\n+\tconst chars = /[^\\u0000-\\u00ff]/.test(text) ? 2 * text.length : text.length;\n+\treturn (STRING_HEADER_BYTES + chars + 7) & ~7;\n+}\n+/**\n  * Moved lines a view resolves once and keeps (abso Capacity): a stored graph otherwise resolved every move again on\n  * disk for each file it laid out. Game's whole history has 290; past this many, the view goes back to that.\n  */"},{"path":"worker/project/project.ts","added":11,"removed":0,"diff":"--- a/worker/project/project.ts\n+++ b/worker/project/project.ts\n@@ -2811,6 +2811,17 @@\n \t\treturn this.#viewAt(this.#shared, last(this.#shared));\n \t}\n \n+\t/**\n+\t * After each read of a reader kept between calls, one that changed nothing (abso Capacity): the cached views back\n+\t * under their budget, since views grow while a call lays files out and the cache weighs them only when they are\n+\t * asked for again (on abso, a tree with authors left 38 MB of views held against the 24 MB budget), and the agents\n+\t * the read decoded let go, read again from storage by the next call (7 MB on abso, every agent's news told).\n+\t */\n+\tafterRead(): void {\n+\t\tthis.#views.trim();\n+\t\tif (this.#agents instanceof WriterAgents) this.#agents.release();\n+\t}\n+\n \t/** A view of a set, cached by the set's exact contents (views.ts). */\n \t#view(set: ReadonlySet<AtomId>): ReadView {\n \t\tthis.#views.assertUnkeyedAllowed();"},{"path":"worker/project/test/after-read.test.ts","added":57,"removed":0,"diff":"--- /dev/null\n+++ b/worker/project/test/after-read.test.ts\n@@ -0,0 +1,57 @@\n+// A reader kept between calls settles after each read (abso Capacity, Project.afterRead): the cached views trimmed\n+// to their budget and the agents it decoded let go. The next reads must answer as before, from storage, and a\n+// write after it must still see every agent as stored.\n+\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { storedArray } from \"../../../src/engine/state-source.ts\";\n+import { SqlState } from \"../../../src/engine/test/storage-helper.ts\";\n+import { MemoryStore, Project } from \"../project.ts\";\n+import type { Entry, ProjectStore } from \"../project-types.ts\";\n+\n+function writer() {\n+\tconst storage = new SqlState();\n+\tconst entries = storedArray<Entry>(storage, \"canonical-log\");\n+\tconst store: ProjectStore = {\n+\t\tappend(batch) {\n+\t\t\tfor (const entry of batch) entries.push(entry);\n+\t\t},\n+\t};\n+\treturn new Project(store, { writerStorage: storage });\n+}\n+\n+const SHAPES = \"export function area(r: number) {\\n  return 3.14 * r * r;\\n}\\n\";\n+const IMPORT = 'import { area } from \"./shapes\";\\n';\n+\n+function history(project: Project) {\n+\tproject.init({ \"src/shapes.ts\": SHAPES, \"src/main.ts\": IMPORT, \"notes.md\": \"notes\\n\" }, 0);\n+\tproject.edit(\"ann\", { file: \"src/shapes.ts\", old: \"area(r: number)\", new: \"area(r: number, scale = 1)\" }, 1);\n+\tproject.record(\"bob\", { \"src/main.ts\": `${IMPORT}export const a = area(2);\\n` }, 2);\n+\tproject.record(\"cy\", { \"notes.md\": \"notes\\nmore\\n\" }, 3);\n+}\n+\n+const reads = (project: Project) => ({\n+\tstatus: project.status(),\n+\tcollisions: project.collisions(),\n+\ttree: project.tree(\"\", { authors: true }),\n+\tbob: project.draftOf(\"bob\")!.view.text(\"src/main.ts\"),\n+});\n+\n+test(\"reads after afterRead answer as before it, and a write after it sees every agent\", () => {\n+\tconst oracle = new Project(new MemoryStore());\n+\tconst project = writer();\n+\thistory(oracle);\n+\thistory(project);\n+\tconst expected = reads(oracle);\n+\tassert.deepEqual(reads(project), expected);\n+\tproject.afterRead();\n+\tassert.deepEqual(reads(project), expected, \"after afterRead\");\n+\tproject.afterRead();\n+\toracle.afterRead();\n+\tfor (const p of [oracle, project]) {\n+\t\tp.record(\"cy\", { \"notes.md\": \"notes\\nmore\\nand more\\n\" }, 4);\n+\t\tassert.equal(p.publish(\"ann\", \"Add a scale\", 5).ok, true);\n+\t}\n+\tassert.deepEqual(reads(project), reads(oracle), \"a write after afterRead\");\n+\tassert.ok(expected.collisions.collisions.some((c) => c.kind === \"definition\"));\n+});"},{"path":"worker/project/test/view-cache.test.ts","added":14,"removed":0,"diff":"--- a/worker/project/test/view-cache.test.ts\n+++ b/worker/project/test/view-cache.test.ts\n@@ -103,6 +103,20 @@\n \t});\n });\n \n+test(\"trim reweighs growth with no further hit and evicts until the views fit (abso Capacity)\", () => {\n+\twithControlledWeights((control) => {\n+\t\tconst cache = new ViewCache(new Graph());\n+\t\tcontrol.setIncomingWeights([10_000_000, 10_000_000]);\n+\t\tconst growing = get(cache, \"growing\");\n+\t\tconst newer = get(cache, \"newer\");\n+\t\t// A call lays files out in the older view after the cache last weighed it, and nothing asks for it again.\n+\t\tcontrol.setWeight(growing, 16_000_000);\n+\t\tcache.trim();\n+\t\tassert.equal(get(cache, \"newer\"), newer);\n+\t\tassert.notEqual(get(cache, \"growing\"), growing);\n+\t});\n+});\n+\n test(\"an oversized view is returned but not retained\", () => {\n \twithControlledWeights((control) => {\n \t\tconst cache = new ViewCache(new Graph());"},{"path":"worker/project/views.ts","added":8,"removed":0,"diff":"--- a/worker/project/views.ts\n+++ b/worker/project/views.ts\n@@ -39,6 +39,14 @@\n \t}\n \n \t/**\n+\t * Weigh every cached view again and evict until they fit (abso Capacity): views grow while a call lays files out,\n+\t * and #get weighs a view only when it is asked for again. A kept reader trims after each call.\n+\t */\n+\ttrim(): void {\n+\t\tthis.#trim();\n+\t}\n+\n+\t/**\n \t * Keep the cached views under VIEW_BUDGET bytes (the website project, #43,\n \t * #46): each view holds every line its files ever had, so a few views of a\n \t * large generated file add up. At most VIEW_COUNT."},{"path":"worker/project/writer-agents.ts","added":5,"removed":0,"diff":"--- a/worker/project/writer-agents.ts\n+++ b/worker/project/writer-agents.ts\n@@ -47,6 +47,11 @@\n \t\t\tyield [key, value];\n \t\t}\n \t}\n+\t/** Let go of the agents read, when nothing changed them: a reader kept between calls (abso Capacity). */\n+\trelease(): void {\n+\t\tthis.#touched.clear();\n+\t}\n+\n \tflush(): void {\n \t\tfor (const [key, value] of this.#touched) super.set(key, value);\n \t\tthis.#touched.clear();"}],"ci":null}