Project abso's memory, cut by a third (abso Capacity, prod abso hit its isolate memory limit at 7:20 and 7:25 PM CT). (1) The in-memory membership cache is one least-recently-used cache of 16 sets across every space, not up to 17 per space: each named draft cached whole copies of shared's set, 37 MB on prod abso with 106 drafts. Keyed by the space object, so a space rebuilt under the same name never gets old sets. (2) An agent keeps at most 200 unheard news lines (trimmed in batches of 50), the oldest dropped behind one line counting them: stopped agents kept every line, 7 MB. Measured on Engine's prod export of abso (81,855 entries) with measure-memory.ts --disk --reads: 96.6 MB after load before, 61.1 MB after; 105.3 MB after the heaviest reads before, 66.3 MB after. New tests membership-cache.test.ts and news-cap.test.ts. Version 200 of shared· capacity· Oct 15 (2026-10-15 00:33 UTC) · Not checked
Project abso's memory, cut by a third (abso Capacity, prod abso hit its isolate memory limit at 7:20 and 7:25 PM CT). (1) The in-memory membership cache is one least-recently-used cache of 16 sets across every space, not up to 17 per space: each named draft cached whole copies of shared's set, 37 MB on prod abso with 106 drafts.
Keyed by the space object, so a space rebuilt under the same name never gets old sets. (2) An agent keeps at most 200 unheard news lines (trimmed in batches of 50), the oldest dropped behind one line counting them: stopped agents kept every line, 7 MB.
Measured on Engine's prod export of abso (81,855 entries) with measure-memory.ts --disk --reads: 96.6 MB after load before, 61.1 MB after; 105.3 MB after the heaviest reads before, 66.3 MB after. New tests membership-cache.test.ts and news-cap.test.ts.
Version 199 · Files at version 200
6 files, 179 lines added, 17 removed.
--- a/worker/project/apply-entry.ts
+++ b/worker/project/apply-entry.ts
@@ -3,7 +3,7 @@
3 3 // which space) is applied first, in spaces.ts. 5 5 import type { AtomId, Graph } from "../../src/engine/index.ts"; 6 − import { outlastsSync } from "./news.ts";
6 + import { capNews, outlastsSync } from "./news.ts"; 7 7 import { ProjectError } from "./project-error.ts"; @@ -201,6 +201,7 @@
201 201 host.setAnnounced(Math.max(host.announced(), id)); 202 202 tellSent(host.tells, id, entry.text[i] ?? entry.text[0] ?? "", agent.name, time); --- a/worker/project/news.ts
+++ b/worker/project/news.ts
@@ -123,6 +123,38 @@
126 + * How many unheard news lines an agent keeps (abso Capacity). An agent that never hears its news, most often one that 127 + * stopped, kept every line: 7 MB of prod project abso's memory. Past this many (and NEWS_TRIM more), the oldest go, 128 + * and one line in front says how many. 130 + export const NEWS_KEPT = 200; 131 + /** How far past NEWS_KEPT the news grows before the oldest are trimmed off. */ 132 + export const NEWS_TRIM = 50; 134 + const DROPPED_NEWS = /^(\d+) older news items? went unheard and (?:was|were) dropped: an agent keeps its newest \d+\.$/; 136 + /** The line in front of an agent's news that counts what was dropped. */ 137 + export function droppedNewsLine(count: number): string { 138 + return `${count} older news ${count === 1 ? "item" : "items"} went unheard and ${count === 1 ? "was" : "were"} dropped: an agent keeps its newest ${NEWS_KEPT}.`; 142 + * Keeps an agent's unheard news to at most NEWS_KEPT + NEWS_TRIM lines (and their times, in step): the oldest go, and the dropped-count 143 + * line in front adds them up. Deterministic, so a replay of the log keeps the same lines. 145 + export function capNews(agent: { news: string[]; newsAt: (number | undefined)[] }): void { 146 + const counted = DROPPED_NEWS.exec(agent.news[0] ?? ""); 147 + const before = counted ? Number(counted[1]) : 0; 148 + const from = counted ? 1 : 0; 149 + // Trimmed in batches of NEWS_TRIM, so a busy agent isn't copied on every line of news. 150 + if (agent.news.length - from <= NEWS_KEPT + NEWS_TRIM) return; 151 + const over = agent.news.length - from - NEWS_KEPT; 152 + const at = agent.newsAt[from + over]; 153 + agent.news = [droppedNewsLine(before + over), ...agent.news.slice(from + over)]; 154 + agent.newsAt = [at, ...agent.newsAt.slice(from + over)]; 126 158 * News a sync doesn't make moot, so it is kept through one: announcements, 127 159 * and what a settle kept (the pull brings the file, not whose side survived). --- a/worker/project/spaces.ts
+++ b/worker/project/spaces.ts
@@ -46,7 +46,6 @@
46 46 readonly publishedIn: Map<AtomId, number>; 47 47 /** Atoms a drop took out, and the version of the drop. */ 48 48 readonly droppedIn: Map<AtomId, number>; 49 − readonly atCache: Map<number, ReadonlySet<AtomId>>;
50 49 /** For a named draft, the log position of the entry that made it: the change its work belongs to (#44). */ @@ -62,10 +61,9 @@
62 61 own: new StoredSet(storage, `${prefix}:own`), 63 62 publishedIn: new StoredMap(storage, `${prefix}:published`), 64 63 droppedIn: new StoredMap(storage, `${prefix}:dropped`), 65 − atCache: new Map(),
68 − return { name, versions: [], own: new Set(), publishedIn: new Map(), droppedIn: new Map(), atCache: new Map() };
66 + return { name, versions: [], own: new Set(), publishedIn: new Map(), droppedIn: new Map() }; 71 69 export function addVersion(space: Space, version: Published): void { @@ -204,10 +202,30 @@
204 202 return space.versions[space.versions.length - 1]!.sharedBase ?? 0; 206 + * How many built membership sets the in-memory path keeps, across every space (abso Capacity). Each named draft's set 207 + * is a whole copy of shared's at its base plus its own atoms, so a cache per space held one copy of shared per draft: 208 + * on prod project abso, 106 drafts kept 37 MB of them. One least-recently-used cache holds at most this many; a set 209 + * that fell out is built again when asked for, in milliseconds at that size. Measured on abso's prod export (81,855 210 + * entries): 96.6 MB after load before, 64.6 MB with 16 kept, 62.0 MB with 8; 16 keeps the load as fast as before. 212 + export const MEMBERSHIP_SETS_KEPT = 16; 214 + /** A number per space object, so a space replaced under the same name (tables/build.ts) never gets the old one's sets. */ 215 + const spaceIds = new WeakMap<Space, number>(); 217 + function spaceId(space: Space): number { 218 + let id = spaceIds.get(space); 219 + if (id === undefined) spaceIds.set(space, (id = nextSpaceId++)); 207 223 /** Every space, and each one's contents at any of its versions. */ 208 224 export class Membership { 209 225 readonly spaces: Map<string, Space>; 210 226 readonly #storage?: GraphStateStorage; 227 + /** Built sets by space and version, oldest use first (MEMBERSHIP_SETS_KEPT). */ 228 + readonly #sets = new Map<string, ReadonlySet<AtomId>>(); 211 229 constructor(storage?: GraphStateStorage) { 212 230 this.#storage = storage; @@ -350,18 +368,26 @@
350 368 return new StableUnionSet(base, own); 352 370 if (space.name === SHARED && version === last(space)) return space.own; 353 − let set = space.atCache.get(version);
354 − if (!set) {
355 − const s = new Set<AtomId>();
356 − if (space.name !== SHARED) for (const id of this.at(this.shared, space.versions[version]!.sharedBase!)) s.add(id);
357 − for (let v = 0; v <= version; v++) {
358 − for (const id of space.versions[v]!.atoms) s.add(id);
359 − for (const id of space.versions[v]!.dropped ?? []) s.delete(id);
360 − }
361 − set = s;
362 − if (space.atCache.size > 16) space.atCache.clear();
363 − space.atCache.set(version, set);
371 + const key = `${spaceId(space)}:${version}`; 372 + const kept = this.#sets.get(key); 374 + this.#sets.delete(key); 375 + this.#sets.set(key, kept); 365 − return set;
378 + const s = new Set<AtomId>(); 379 + if (space.name !== SHARED) for (const id of this.at(this.shared, space.versions[version]!.sharedBase!)) s.add(id); 380 + for (let v = 0; v <= version; v++) { 381 + for (const id of space.versions[v]!.atoms) s.add(id); 382 + for (const id of space.versions[v]!.dropped ?? []) s.delete(id); 384 + this.#sets.set(key, s); 385 + if (this.#sets.size > MEMBERSHIP_SETS_KEPT) this.#sets.delete(this.#sets.keys().next().value!); 389 + /** How many built sets the in-memory cache holds now (for tests). */ 390 + get setsKept(): number { 391 + return this.#sets.size; --- /dev/null
+++ b/worker/project/test/membership-cache.test.ts
@@ -0,0 +1,56 @@
1 + // The in-memory membership cache is bounded across every space (abso Capacity): 106 drafts on prod project abso 2 + // each kept whole copies of shared's set, 37 MB in all. One least-recently-used cache now holds MEMBERSHIP_SETS_KEPT. 4 + import assert from "node:assert/strict"; 5 + import { test } from "node:test"; 6 + import { addVersion, MEMBERSHIP_SETS_KEPT, Membership, newSpace } from "../spaces.ts"; 10 + /** Shared with `versions` versions, version v adding atom s<v>; then `drafts` drafts, each on shared's version 3. */ 11 + function membership(versions: number, drafts: number): Membership { 12 + const m = new Membership(); 13 + for (let v = 0; v < versions; v++) 14 + addVersion(m.shared, { version: v, agent: "a", message: `v${v}`, time: 0, atoms: [`s${v}`] }); 15 + for (let d = 0; d < drafts; d++) { 16 + const space = newSpace(`d${d}`); 17 + m.spaces.set(space.name, space); 18 + addVersion(space, { version: 0, agent: "a", message: "start", time: 0, atoms: [], sharedBase: 3 }); 19 + addVersion(space, { version: 1, agent: "a", message: "work", time: 0, atoms: [`d${d}-x`], sharedBase: 3 }); 24 + test("the cache never holds more than MEMBERSHIP_SETS_KEPT sets, however many drafts are read", () => { 25 + const m = membership(10, MEMBERSHIP_SETS_KEPT * 3); 26 + for (let d = 0; d < MEMBERSHIP_SETS_KEPT * 3; d++) m.at(m.spaces.get(`d${d}`)!, 1); 27 + assert.ok(m.setsKept <= MEMBERSHIP_SETS_KEPT, `kept ${m.setsKept}`); 30 + test("a set that fell out of the cache is built again the same, and a kept one is the same object", () => { 31 + const m = membership(10, MEMBERSHIP_SETS_KEPT * 2); 32 + const first = m.spaces.get("d0")!; 33 + const before = [...m.at(first, 1)].sort(); 34 + assert.deepEqual(before, ["d0-x", "s0", "s1", "s2", "s3"], "shared at its base, plus its own"); 35 + for (let d = 1; d < MEMBERSHIP_SETS_KEPT * 2; d++) m.at(m.spaces.get(`d${d}`)!, 1); 36 + assert.deepEqual([...m.at(first, 1)].sort(), before, "rebuilt after eviction, same contents"); 37 + const again = m.at(first, 1); 38 + assert.equal(m.at(first, 1), again, "while kept, the same set"); 39 + // Shared's history and its newest version answer as before. 40 + assert.deepEqual([...m.at(m.shared, 2)].sort(), ["s0", "s1", "s2"]); 41 + assert.equal(m.at(m.shared, 9), m.shared.own, "shared's newest version is its live set, never cached"); 44 + test("a draft replaced under the same name never gets the old one's set", () => { 45 + const m = membership(10, 1); 46 + const old = m.spaces.get("d0")!; 47 + assert.ok(m.at(old, 1).has("d0-x")); 48 + const replaced = newSpace("d0"); 49 + m.spaces.set("d0", replaced); 50 + addVersion(replaced, { version: 0, agent: "a", message: "start", time: 0, atoms: [], sharedBase: 5 }); 51 + addVersion(replaced, { version: 1, agent: "a", message: "work", time: 0, atoms: ["new"], sharedBase: 5 }); 52 + const now = m.at(replaced, 1); 53 + assert.equal(now.has("d0-x"), false); 54 + assert.deepEqual([...now].sort(), ["new", "s0", "s1", "s2", "s3", "s4", "s5"]); 55 + assert.equal(SHARED, m.shared.name); --- /dev/null
+++ b/worker/project/test/news-cap.test.ts
@@ -0,0 +1,47 @@
1 + // An agent's unheard news is capped (abso Capacity): stopped agents kept every line, 7 MB of prod project abso. 3 + import assert from "node:assert/strict"; 4 + import { test } from "node:test"; 5 + import { capNews, droppedNewsLine, NEWS_KEPT, NEWS_TRIM } from "../news.ts"; 7 + const agentWith = (n: number) => ({ 8 + news: Array.from({ length: n }, (_, i) => `news ${i}`), 9 + newsAt: Array.from({ length: n }, (_, i) => i), 12 + test("news up to NEWS_KEPT plus NEWS_TRIM lines is kept as it is", () => { 13 + const agent = agentWith(NEWS_KEPT + NEWS_TRIM); 15 + assert.equal(agent.news.length, NEWS_KEPT + NEWS_TRIM); 16 + assert.equal(agent.news[0], "news 0"); 19 + test("past that, the oldest go: the newest NEWS_KEPT stay, behind one line counting the dropped, times in step", () => { 20 + const n = NEWS_KEPT + NEWS_TRIM + 1; 21 + const agent = agentWith(n); 23 + assert.equal(agent.news.length, NEWS_KEPT + 1); 24 + assert.equal(agent.news[0], droppedNewsLine(n - NEWS_KEPT)); 25 + assert.equal(agent.news[1], `news ${n - NEWS_KEPT}`); 26 + assert.equal(agent.news.at(-1), `news ${n - 1}`); 27 + assert.equal(agent.newsAt.length, agent.news.length); 28 + assert.equal(agent.newsAt[1], n - NEWS_KEPT); 31 + test("the dropped count adds up across trims", () => { 32 + const agent = agentWith(NEWS_KEPT + NEWS_TRIM + 1); 34 + const first = NEWS_TRIM + 1; 35 + for (let i = 0; i < NEWS_TRIM + 1; i++) { 36 + agent.news.push(`more ${i}`); 37 + agent.newsAt.push(1000 + i); 40 + assert.equal(agent.news[0], droppedNewsLine(first + NEWS_TRIM + 1)); 41 + assert.equal(agent.news.length, NEWS_KEPT + 1); 42 + assert.equal(agent.news.at(-1), `more ${NEWS_TRIM}`); 45 + `1 older news item went unheard and was dropped: an agent keeps its newest ${NEWS_KEPT}.`, --- a/worker/project/test/stored-membership.test.ts
+++ b/worker/project/test/stored-membership.test.ts
@@ -147,6 +147,6 @@
147 147 assert.equal(old.size, 41); 148 148 assert.equal(old.has("a0"), true); 149 149 assert.equal(old.has("a79"), false); 150 − assert.equal(shared.atCache.size, 0, "storage-backed shared history never fills the in-memory set cache");
150 + assert.equal(reopened.setsKept, 0, "storage-backed shared history never fills the in-memory set cache"); 151 151 storage.db.close();