{"version":117,"agent":"codex-capacity-chair","message":"Avoid full membership enumeration for indexed view cache keys","time":1791830705289,"timeIso":"2026-10-12T18:45:05.289Z","changes":3,"change":1284,"files":["worker/project/test/view-cache.test.ts","worker/project/views.ts"],"draft":null,"diff":[{"path":"worker/project/test/view-cache.test.ts","added":24,"removed":0,"diff":"--- a/worker/project/test/view-cache.test.ts\n+++ b/worker/project/test/view-cache.test.ts\n@@ -1,10 +1,34 @@\n import assert from \"node:assert/strict\";\n import { test } from \"node:test\";\n import { buildBase, Graph, View } from \"../../../src/engine/index.ts\";\n+import { StableOverlaySet } from \"../../../src/engine/state-source.ts\";\n+import type { AtomId } from \"../../../src/engine/types.ts\";\n import { ViewCache } from \"../views.ts\";\n \n const VIEW_BUDGET = 24_000_000;\n \n+test(\"indexed view cache keys never enumerate membership and remain bounded by eviction\", () => {\n+\tclass Counted extends StableOverlaySet<AtomId> {\n+\t\titerations = 0;\n+\t\toverride values(): SetIterator<AtomId> {\n+\t\t\tthis.iterations++;\n+\t\t\treturn super.values();\n+\t\t}\n+\t}\n+\tconst graph = new Graph();\n+\tgraph.add(buildBase({ \"a.txt\": \"one\\n\" }, { author: \"test\", message: \"base\" }));\n+\tconst cache = new ViewCache(graph);\n+\tconst set = new Counted([\"a0\"]);\n+\tconst view = cache.view(set);\n+\tassert.equal(set.iterations, 1, \"only View closure validation enumerates membership\");\n+\tassert.equal(cache.view(set), view);\n+\tassert.equal(set.iterations, 1, \"cache hit must not construct a full-history key\");\n+\tassert.equal(view.text(\"a.txt\"), \"one\\n\");\n+\tfor (let i = 0; i < 33; i++) cache.view(new Counted([\"a0\"]));\n+\tassert.notEqual(cache.view(set), view, \"indexed identities obey the same cache eviction limit\");\n+\tassert.equal(set.iterations, 2);\n+});\n+\n type WeightControls = {\n \tsetIncomingWeights(values: number[]): void;\n \tsetWeight(view: View, weight: number): void;"},{"path":"worker/project/views.ts","added":12,"removed":3,"diff":"--- a/worker/project/views.ts\n+++ b/worker/project/views.ts\n@@ -1,7 +1,9 @@\n // The views a project lays out from sets of atoms, cached under a byte budget (#46, disk-first 1). Published\n-// versions never change, so their views are kept by name; any other set is kept by its exact contents.\n+// versions never change, so their views are kept by name. Immutable indexed sets use their identity;\n+// ordinary in-memory sets use their exact contents.\n \n import { type AtomId, type Graph, View } from \"../../src/engine/index.ts\";\n+import { StableSet } from \"../../src/engine/state-source.ts\";\n \n /**\n  * About how many bytes the cached views may hold together (#46). A laid-out\n@@ -13,7 +15,7 @@\n /** Views by key, most recently used last, held under VIEW_BUDGET bytes and 32 views. */\n export class ViewCache {\n \treadonly #graph: Graph;\n-\treadonly #cache = new Map<string, View>();\n+\treadonly #cache = new Map<string | StableSet<AtomId>, View>();\n \n \tconstructor(graph: Graph) {\n \t\tthis.#graph = graph;\n@@ -34,8 +36,11 @@\n \t\t}\n \t}\n \n-\t/** A view of a set, cached by the set's exact contents. */\n+\t/** Indexed snapshots use identity; ordinary sets use their exact contents. */\n \tview(set: ReadonlySet<AtomId>): View {\n+\t\t// Immutable indexed membership is already a stable identity. Building\n+\t\t// a sorted history-sized key defeats its external-storage contract.\n+\t\tif (set instanceof StableSet) return this.#get(set, () => set);\n \t\tconst key = [...set]\n \t\t\t.map((id) => this.#graph.atom(id).seq)\n \t\t\t.sort((a, b) => a - b)\n@@ -45,6 +50,10 @@\n \n \t/** A view cached under `key`, made from `set()` when it isn't there. */\n \tkeyed(key: string, set: () => ReadonlySet<AtomId>): View {\n+\t\treturn this.#get(key, set);\n+\t}\n+\n+\t#get(key: string | StableSet<AtomId>, set: () => ReadonlySet<AtomId>): View {\n \t\tlet view = this.#cache.get(key);\n \t\tif (view) {\n \t\t\t// Most recently used last, so eviction takes the oldest."}],"ci":null}