Keep current-file views and key scans from retaining historical state
of shared·codex-capacity-chair··Not checked
Version 109 · Files at version 110
4 files, 48 lines added, 12 removed.
--- a/src/engine/state-source.ts
+++ b/src/engine/state-source.ts
@@ -220,7 +220,11 @@
222222 override *keys(): MapIterator<K> { 223− for (const [key] of this.entries()) yield key;
223+ const keys = storedArray<K>(this.#storage, `${this.#name}:order`); 224+ for (let slot = 0; slot < keys.length; slot++) { 225+ const key = keys[slot]!; 226+ if (this.#row(key)?.slot === slot) yield key; 225229 override *values(): MapIterator<V> { 226230 for (const [, value] of this.entries()) yield value; --- a/src/engine/test/view.test.ts
+++ b/src/engine/test/view.test.ts
@@ -32,6 +32,12 @@
3232 assert.equal(second.weight(), weight, "membership resident size does not increase with historical atom count"); 3333 assert.equal(first.text("a.txt"), "40\n"); 3434 assert.equal(second.text("a.txt"), "80\n"); 35+ assert.equal(second.weight(), first.weight(), "current text retains visible lines, not historical tombstones"); 36+ const currentWeight = second.weight(); 37+ assert.equal(second.sequence("a.txt").length, 81); 38+ assert.equal(second.sequence("a.txt").filter((line) => line.visible).length, 1); 39+ assert.ok(second.weight() > currentWeight, "explicit history reads may retain their requested sequence"); 40+ assert.equal(second.text("a.txt"), "80\n"); 3541 assert.equal(first.set.has("a80"), false); 3642 assert.throws(() => new View(repo.graph, new StableOverlaySet<AtomId>(["a1"])), /not closed/); --- a/src/engine/view.ts
+++ b/src/engine/view.ts
@@ -49,6 +49,8 @@
5050interface FileState { 5151 readonly attrs: FileAttrs; 52+ /** Full history is retained only when a sequence caller explicitly requests it. */ 53+ readonly complete: boolean; 5254 /** The file's sequence, tombstones included: each line's n. */ 5355 readonly seq: Int32Array; 5456 /** Its visible lines' n, in order. */ @@ -70,7 +72,7 @@
7274const EMPTY_INTS = new Int32Array(0); 73−const NO_FILE: FileState = { attrs: NO_ATTRS, seq: EMPTY_INTS, vis: EMPTY_INTS, deletedBy: new Map() };
75+const NO_FILE: FileState = { attrs: NO_ATTRS, complete: true, seq: EMPTY_INTS, vis: EMPTY_INTS, deletedBy: new Map() }; 7577// Approximate retained-container costs for View.weight(): reference slots, 7678// object headers, and map/set entries. These are cache-budget units, not V8 @@ -314,7 +316,7 @@
316318 /** Lay out one file: its line tree from the set's atoms, linearized. */ 317− #layout(path: string): FileState {
319+ #layout(path: string, complete: boolean): FileState { 318320 const graph = this.graph; 319321 // For each attribute, the highest-priority setter in the set wins. 320322 const setters = new Map<string, { id: AtomId; value: string | null }>(); @@ -343,9 +345,10 @@
343345 const n = stack.pop()!; 344346 const deleters = graph.deleterSeqsOf(n); 345347 const by = deleters.length === 0 ? NONE : deleters.filter((d) => this.#has(d)); 346− if (by.length > 0) deletedBy.set(seq.length, by.length > 1 ? [...by].sort(priority) : by);
347− else vis.push(n);
348− seq.push(n);
348+ if (by.length > 0) { 349+ if (complete) deletedBy.set(seq.length, by.length > 1 ? [...by].sort(priority) : by); 351+ if (complete) seq.push(n); 351354 const attrs: FileAttrs = { @@ -354,7 +357,7 @@
354357 link: setters.get("link")?.value ?? null, 355358 blob: setters.get("blob")?.value ?? null, 357− return { attrs, seq: Int32Array.from(seq), vis: Int32Array.from(vis), deletedBy };
360+ return { attrs, complete, seq: Int32Array.from(seq), vis: Int32Array.from(vis), deletedBy }; 360363 get #known(): string[] { @@ -431,7 +434,7 @@
431434 sequence(path: string): readonly SeqLine[] { 432435 if (this.#seqCache?.path === path) return this.#seqCache.seq; 433436 const graph = this.graph; 434− const f = this.#file(path);
437+ const f = this.#file(path, true); 435438 const out: SeqLine[] = new Array(f.seq.length); 436439 for (let i = 0; i < f.seq.length; i++) { 437440 const n = f.seq[i]!; @@ -450,7 +453,7 @@
451454 /** Index of a line in `sequence(path)`, or -1. */ 452455 sequenceIndex(path: string, line: LineId): number { 453− const f = this.#file(path);
456+ const f = this.#file(path, true); 454457 if (!f.seqIndex) { 455458 f.seqIndex = new Map(); 456459 for (let i = 0; i < f.seq.length; i++) f.seqIndex.set(f.seq[i]!, i); @@ -528,10 +531,10 @@
531− #file(path: string): FileState {
534+ #file(path: string, complete = false): FileState { 532535 let state = this.#files.get(path); 533− if (!state) {
534− state = this.graph.atomsIn(path).length === 0 ? NO_FILE : this.#layout(path);
536+ if (!state || (complete && !state.complete)) { 537+ state = this.graph.atomsIn(path).length === 0 ? NO_FILE : this.#layout(path, complete); 535538 this.#files.set(path, state); --- a/worker/project/test/stored-membership.test.ts
+++ b/worker/project/test/stored-membership.test.ts
@@ -5,6 +5,29 @@
55import { MemoryStore, Project } from "../project.ts"; 66import { Membership } from "../spaces.ts"; 8+test("key-only scans do not decode or retain values through overridden entry iterators", () => { 9+ const storage = new SqlState(); 10+ const seed = new StoredMap<string, number>(storage, "keys-only"); 11+ seed.set("old", 1).set("kept", 2); 14+ class ValuesForbidden extends StoredMap<string, number> { 15+ override entries(): MapIterator<[string, number]> { 16+ throw new Error("key scans must not invoke retaining value iterators"); 19+ const map = new ValuesForbidden(storage, "keys-only", { 20+ encode: (value) => value, 22+ throw new Error("value decoding forbidden"); 25+ assert.deepEqual([...map.keys()], ["kept", "old"]); 27+ assert.equal(map.size, 0); 831test("persistent collections preserve insertion order, deletion, reinsert and undefined values", () => { 932 const storage = new SqlState(); 1033 const map = new StoredMap<string, number | undefined>(storage, "map");