- MCP
- blame on this site's /mcp
- CLI
- abso blame src/engine/test/state-cache.test.ts --version 258 --json
- JSON
- /christopher/abso-core/blame/src/engine/test/state-cache.test.ts?version=258&format=json
186 linescodex-capacity-chair 58% · capacity 42%Last change capacity in version 258,
186 lines from 4 changes in 4 versions.
codex-capacity-chair111Bound persistent writer read caching and verify rollback isolationCompare1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { CachedStateStorage } from "../state-source.ts";
4import { SqlState } from "./storage-helper.ts";
codex-capacity-chair136Avoid JSON decoding for bounded numeric cache pages and scalar metadataCompare6test("numeric cache pages are detached on first and repeated reads, and rollback invalidates them", () => {
7 const source = new SqlState();
8 try {
9 const cache = new CachedStateStorage(source, 4096, 4);
10 cache.write("numbers", "page", [1, 2, 3]);
11 (cache.read("numbers", "page") as number[])[0] = 99;
12 assert.deepEqual(cache.read("numbers", "page"), [1, 2, 3]);
13 (cache.read("numbers", "page") as number[]).push(4);
14 assert.deepEqual(cache.read("numbers", "page"), [1, 2, 3]);
15 assert.throws(
16 () =>
17 cache.transaction(() => {
18 cache.write("numbers", "page", [8]);
19 assert.deepEqual(cache.read("numbers", "page"), [8]);
20 throw new Error("rollback numeric page");
21 }),
22 /rollback numeric page/,
23 );
24 assert.equal(cache.retainedBytes, 0);
25 assert.deepEqual(cache.read("numbers", "page"), [1, 2, 3]);
26 cache.remove("numbers", "page");
27 assert.equal(cache.read("numbers", "page"), undefined);
28 } finally {
29 source.db.close();
30 }
31});
33test("cached scalar metadata and bounded numeric pages need no JSON decode on hits", (t) => {
34 const source = new SqlState();
35 try {
36 const cache = new CachedStateStorage(source, 8192, 8);
37 const values = [undefined, null, true, "metadata", 42, Array.from({ length: 256 }, (_, n) => n)];
38 for (let i = 0; i < values.length; i++) {
39 cache.write("rows", String(i), values[i]);
40 cache.read("rows", String(i));
41 }
42 const reads = source.reads;
43 t.mock.method(JSON, "parse", () => {
44 throw new Error("cache hit unexpectedly decoded JSON");
45 });
46 for (let i = 0; i < values.length; i++) assert.deepEqual(cache.read("rows", String(i)), values[i]);
47 assert.equal(source.reads, reads);
48 assert.ok(cache.retainedBytes <= 8192);
49 } finally {
50 source.db.close();
51 }
52});
codex-capacity-chair111Bound persistent writer read caching and verify rollback isolationCompare54test("bounded writer cache copies values, invalidates writes and clears after rollback", () => {
55 const source = new SqlState();
56 const cache = new CachedStateStorage(source, 1024, 3);
57 source.write("rows", "a", { list: [1] });
58 const first = cache.read("rows", "a") as { list: number[] };
59 first.list.push(2);
60 const reads = source.reads;
61 assert.deepEqual(cache.read("rows", "a"), { list: [1] });
62 assert.equal(source.reads, reads);
63 cache.write("rows", "a", { list: [3] });
64 assert.deepEqual(cache.read("rows", "a"), { list: [3] });
65 assert.throws(
66 () =>
67 cache.transaction(() => {
68 cache.write("rows", "a", { list: [4] });
69 assert.deepEqual(cache.read("rows", "a"), { list: [4] });
70 throw new Error("rollback");
71 }),
72 /rollback/,
73 );
74 assert.equal(cache.retainedEntries, 0);
75 assert.deepEqual(cache.read("rows", "a"), { list: [3] });
76 cache.remove("rows", "a");
77 assert.equal(cache.read("rows", "a"), undefined);
78 cache.write("rows", "a", "restored");
79 assert.equal(cache.read("rows", "a"), "restored");
80 source.db.close();
81});
83test("writer cache evicts by both payload and entry count and skips oversized values", () => {
84 const source = new SqlState();
85 const cache = new CachedStateStorage(source, 1024, 3);
86 for (let i = 0; i < 100; i++) {
87 cache.write("rows", String(i), "small");
88 assert.equal(cache.read("rows", String(i)), "small");
89 assert.ok(cache.retainedBytes <= 1024);
90 assert.ok(cache.retainedEntries <= 3);
91 }
92 const reads = source.reads;
93 cache.read("rows", "99");
94 assert.equal(source.reads, reads);
95 cache.read("rows", "0");
96 assert.equal(source.reads, reads + 1);
97 cache.write("rows", "large", "x".repeat(1024));
98 const before = cache.retainedBytes;
99 cache.read("rows", "large");
100 assert.equal(cache.retainedBytes, before);
101 const afterLarge = source.reads;
102 cache.read("rows", "large");
103 assert.equal(source.reads, afterLarge + 1);
104 cache.clear();
105 assert.equal(cache.retainedBytes, 0);
106 source.db.close();
107});
capacity253The writer's row cache survives projection scratch and big rows (abso Capacity, issue 664).
- CachedStateStorage dropped its whole cache whenever a transaction rolled back. Every projection scratch scope ends in a rollback on purpose, so the cache emptied many times within a call and at every call of a kept reader. A rollback now drops only the keys written or removed in that transaction (and any read back after), and a scope's own scratch rows; a nested commit's keys go with an outer rollback.
- A row bigger than a per-row limit is read but not kept (the writer passes 16 KB). abso's 109 agents, each with all the news it was told, are 4.3 MB of rows read on every call, and they pushed every small page out of the 4 MB cache.
On a fresh abso adoption (my machine), asked again: where 229 to 237 ms after its second ask (was 0.32 to 0.46 s), drafts 198 ms (0.31 s), collisions 207 ms. A full run matches 33 of 33 answers; held heap at most 39.8 MB.
Tests: state-cache (new: a rollback drops only what its transaction wrote, a nested commit's writes go with an outer rollback; a row over the limit isn't kept and pushes nothing out). projection-scratch's check now expects the canonical value written before a scratch scope to stay cached. All of src/engine/test (294) and worker/project/test (349) pass.
codex-capacity-policy has older unpushed changes in lines 1 to 25 of state-source.ts (and a new test file); this touches the class body only.Compare108
109test("a rollback drops only what its transaction wrote; a nested commit's writes go with an outer rollback (abso Capacity)", () => {
110 const source = new SqlState();
111 try {
112 const cache = new CachedStateStorage(source, 8192, 16);
113 source.write("rows", "kept", "k");
114 source.write("rows", "inner", "i0");
115 source.write("rows", "outer", "o0");
116 for (const key of ["kept", "inner", "outer"]) cache.read("rows", key);
117 const reads = source.reads;
118 // A projection scratch scope: rolled back on purpose, and the rows read around it stay cached.
119 assert.throws(
120 () =>
121 cache.transaction(() => {
122 cache.write("scratch", "x", "temporary");
123 throw new Error("scratch rollback");
124 }),
125 /scratch rollback/,
126 );
127 assert.equal(cache.read("rows", "kept"), "k");
128 assert.equal(source.reads, reads, "the rows read before were not read again");
129 assert.equal(cache.read("scratch", "x"), undefined);
130 // A nested transaction commits, then the outer one rolls back: both writes are undone, and the cache forgets both.
131 assert.throws(
132 () =>
133 cache.transaction(() => {
134 cache.write("rows", "outer", "o1");
135 cache.transaction(() => cache.write("rows", "inner", "i1"));
136 assert.equal(cache.read("rows", "inner"), "i1");
137 throw new Error("outer rollback");
138 }),
139 /outer rollback/,
140 );
141 assert.equal(cache.read("rows", "inner"), "i0");
142 assert.equal(cache.read("rows", "outer"), "o0");
143 assert.equal(cache.read("rows", "kept"), "k");
144 } finally {
145 source.db.close();
146 }
147});
149test("a row over the per-row limit is read but not kept, and pushes nothing out (abso Capacity)", () => {
150 const source = new SqlState();
151 try {
152 const cache = new CachedStateStorage(source, 8192, 64, 1024);
153 source.write("rows", "small", "s");
154 source.write("rows", "big", "x".repeat(2000));
155 cache.read("rows", "small");
156 const reads = source.reads;
157 assert.equal((cache.read("rows", "big") as string).length, 2000);
158 assert.equal(cache.read("rows", "small"), "s");
159 assert.equal(source.reads, reads + 1, "the big row was read from storage once and the small one stayed cached");
160 assert.equal((cache.read("rows", "big") as string).length, 2000);
161 assert.equal(source.reads, reads + 2, "the big row isn't kept");
162 } finally {
163 source.db.close();
164 }
165});
capacity258Told lists out of agent rows, in 64 bucket rows per agent; layout pages read through the writer cache; a laid-out file's authors counted by writer (abso Capacity, issue 664; Security yes on ask 2102; Agent VCS approved).
- writer-agents.ts: each agent's told keys (the news it has been told) live in 64 rows of their own, project:agents:told, keyed by the agent's name and a fixed hash bucket (FNV-1a mod 64), each holding its bucket's keys sorted; the agent row no longer carries them. told.has reads one bucket and told.add rewrites one; status, where and drafts never read them. A row written before keeps its list, read alongside the buckets (StoredTold.listed) until the agent's row is next written, which moves the list into the buckets; a fresh adoption writes the new form from the start. Removing an agent clears its buckets; delete and purge clear the writer state with them. abso's 109 agent rows were 4.3 MB, the largest 100.9 KB.
- CachedStateStorage can read collections straight through; the writer passes file-lines, file-texts and file-deletes pages, which are read only to lay a file out (the view keeps the layout) and pushed out the small rows read on every call.
- authorsInView: a laid-out file's lines by author from its visible lines' writers, each writer's author once (tree with authors, folder blame, and the totals adoption keeps), instead of an atom id and line id per line.
Tests: told-buckets (new: news on a stored writer against the in-memory project, every step's news asked twice, before and after an agent's told keys are half in an old row's list and half in buckets, with a writer opened afresh; the old list moves into the buckets on the agent's next write; an agent never reads another's buckets; a rollback leaves the buckets as they were). state-cache: a collection read straight through is never kept and pushes nothing out. All of src/engine/test (295) and worker/project/test (351, one skipped) pass.Compare166
167test("a collection read straight through is never kept, and pushes nothing out (abso Capacity)", () => {
168 const source = new SqlState();
169 try {
170 const cache = new CachedStateStorage(source, 8192, 2, 8192, (collection) => collection === "pages");
171 source.write("rows", "a", "A");
172 source.write("rows", "b", "B");
173 for (let i = 0; i < 10; i++) source.write("pages", String(i), [i]);
174 cache.read("rows", "a");
175 cache.read("rows", "b");
176 const reads = source.reads;
177 for (let i = 0; i < 10; i++) assert.deepEqual(cache.read("pages", String(i)), [i]);
178 assert.equal(source.reads, reads + 10);
179 assert.equal(cache.read("rows", "a"), "A");
180 assert.equal(cache.read("rows", "b"), "B");
181 assert.equal(source.reads, reads + 10, "the two kept rows were not pushed out by the pages");
182 assert.equal(cache.retainedEntries, 2);
183 } finally {
184 source.db.close();
185 }
186});