Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
show on this site's /mcp
CLI
abso show 147 --json
JSON
/christopher/abso-core/versions/147?format=json

Batch immutable locator writes for sequence updates

of shared·codex-capacity-chair··Not checked

Version 146 · Files at version 147

3 files, 107 lines added, 1 removed.

--- a/src/engine/experimental/projection-locator.ts
+++ b/src/engine/experimental/projection-locator.ts
@@ -63,6 +63,46 @@
6363 this.#storage.write(this.#collection, "next", next + 1);
6464 return next;
6565 }
66+ /** Persist only pages reachable from the final root of a bounded update batch. */
67+ setMany(root: Page, values: ReadonlyArray<readonly [number, T]>): Page {
68+ page(root);
69+ if (values.length > 256) throw Error("locator batch limit");
70+ return this.#storage.transaction(() => {
71+ const staged = new Map<string, unknown>();
72+ const storage: GraphStateStorage = {
73+ read: (collection, key) =>
74+ collection === this.#collection && staged.has(key) ? staged.get(key) : this.#storage.read(collection, key),
75+ write: (collection, key, value) => {
76+ if (collection !== this.#collection) throw Error("unexpected batch collection");
77+ staged.set(key, value);
78+ },
79+ remove: () => {
80+ throw Error("unexpected batch removal");
81+ },
82+ entries: () => {
83+ throw Error("unexpected batch scan");
84+ },
85+ transaction: (body) => body(),
86+ };
87+ // The namespace is encoded into the collection at construction.
88+ const namespace = JSON.parse(this.#collection.slice("experimental-locator:".length)) as string;
89+ const index = new ProjectionLocator<T>(storage, namespace, this.#decode);
90+ let result = root;
91+ for (const [id, value] of values) result = index.set(result, id, value);
92+ const retained = new Set<number>();
93+ const visit = (id: Page): void => {
94+ if (id === null || retained.has(id) || !staged.has(String(id))) return;
95+ retained.add(id);
96+ const node = staged.get(String(id)) as Node;
97+ if (node.kind === "branch") for (const child of Object.values(node.children)) visit(child);
98+ };
99+ visit(result);
100+ for (const id of retained) this.#storage.write(this.#collection, String(id), staged.get(String(id)));
101+ if (staged.has("next")) this.#storage.write(this.#collection, "next", staged.get("next"));
102+ return result;
103+ });
104+ }
66106 get(root: Page, id: number): T | undefined {
67107 page(root);
68108 const key = keyOf(id);
--- a/src/engine/experimental/projection-sequence.ts
+++ b/src/engine/experimental/projection-sequence.ts
@@ -300,7 +300,17 @@
300300 save(node);
301301 }
302302 } else throw Error("unknown operation");
303− for (const [id, node] of changed) map = this.set(map, id, node);
303+ // Keep locator staging bounded even if a valid split/merge changes more
304+ // than one batch. The enclosing transaction commits all batches together.
305+ let batch: Array<readonly [number, SequenceNode]> = [];
306+ for (const entry of changed) {
307+ batch.push(entry);
308+ if (batch.length === 256) {
309+ map = this.#locator.setMany(map, batch);
310+ batch = [];
311+ }
312+ }
313+ map = this.#locator.setMany(map, batch);
304314 return { map, root, next };
305315 });
306316 }
--- a/src/engine/test/projection-locator.test.ts
+++ b/src/engine/test/projection-locator.test.ts
@@ -47,3 +47,59 @@
4747 storage.db.close();
4848 }
4949});
51+test("bounded locator batches preserve old roots, discard intermediate pages, and roll back writes", () => {
52+ const db = new SqlState();
53+ try {
54+ const index = new ProjectionLocator(db, "batch", decode);
55+ const old = index.set(null, 0, 1);
56+ const values = Array.from({ length: 64 }, (_, i) => [i, i + 10] as const);
57+ const before = db.writes;
58+ const root = index.setMany(old, values);
59+ const batchWrites = db.writes - before;
60+ assert.equal(index.get(old, 0), 1);
61+ for (const [id, value] of values) assert.equal(index.get(root, id), value);
62+ const plain = new ProjectionLocator(db, "plain", decode);
63+ let sequential: number | null = plain.set(null, 0, 1);
64+ const baseline = db.writes;
65+ for (const [id, value] of values) sequential = plain.set(sequential, id, value);
66+ assert.ok(batchWrites < (db.writes - baseline) / 2);
67+ const rows = db.db.prepare("SELECT * FROM state ORDER BY collection,key").all();
68+ // Fail every persisted page and the final allocation-counter write.
69+ for (let offset = 1; offset <= batchWrites; offset++) {
70+ db.failWrite = db.writes + offset;
71+ assert.throws(() => index.setMany(old, values), /injected/);
72+ assert.deepEqual(db.db.prepare("SELECT * FROM state ORDER BY collection,key").all(), rows);
73+ }
74+ db.failWrite = Infinity;
75+ assert.throws(
76+ () =>
77+ index.setMany(
78+ root,
79+ Array.from({ length: 257 }, () => [0, 1] as const),
80+ ),
81+ /batch limit/,
82+ );
83+ assert.equal(index.setMany(root, []), root);
84+ const duplicate = index.setMany(root, [
85+ [0, 100],
86+ [0, 200],
87+ [0xffffffff, 300],
88+ ]);
89+ assert.equal(index.get(duplicate, 0), 200);
90+ assert.equal(index.get(duplicate, 0xffffffff), 300);
91+ assert.equal(index.get(root, 0), 10);
92+ const stable = db.db.prepare("SELECT * FROM state ORDER BY collection,key").all();
93+ assert.throws(
94+ () =>
95+ index.setMany(duplicate, [
96+ [1, 3],
97+ [2 ** 32, 4],
98+ ]),
99+ /identity limit/,
100+ );
101+ assert.deepEqual(db.db.prepare("SELECT * FROM state ORDER BY collection,key").all(), stable);
102+ } finally {
103+ db.db.close();
104+ }
105+});