- MCP
- blame on this site's /mcp
- CLI
- abso blame src/engine/projection-scratch.ts --json
- JSON
- /christopher/abso-core/blame/src/engine/projection-scratch.ts?format=json
60 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 132,
60 lines from 1 change in 1 version.
codex-capacity-chair132Bound persistent relocation projection memory with rollback-only scratchCompare1import type { GraphStateStorage } from "./state-source.ts";
3let nextScope = 0;
5/** Synchronous derived-state work only. All scratch writes roll back, even on
6 * success. The outer canonical transaction is left intact. Returned values
7 * must be detached data, never iterators, promises or storage handles. */
8export function withProjectionScratch<T>(
9 storage: GraphStateStorage,
10 validateIdentity: () => void,
11 operation: (scratch: GraphStateStorage) => T,
12): T {
13 if (nextScope >= Number.MAX_SAFE_INTEGER) throw new Error("projection scope identity exhausted");
14 const prefix = `projection-scratch:${++nextScope}:`;
15 const finished = {};
16 let active = true;
17 let result: T;
18 const collection = (name: string): string => {
19 if (!active) throw new Error("projection scratch scope has ended");
20 return prefix + name;
21 };
22 const scratch: GraphStateStorage = {
23 read: (name, key) => storage.read(collection(name), key),
24 write: (name, key, value) => storage.write(collection(name), key, value),
25 remove: (name, key) => storage.remove(collection(name), key),
26 *entries(name) {
27 for (const entry of storage.entries(collection(name))) {
28 collection(name);
29 yield entry;
30 }
31 },
32 transaction: (body) => {
33 collection("");
34 return storage.transaction(body);
35 },
36 };
37 try {
38 storage.transaction(() => {
39 validateIdentity();
40 const value = operation(scratch);
41 if (
42 value &&
43 (typeof (value as { then?: unknown }).then === "function" ||
44 typeof (value as { next?: unknown }).next === "function")
45 )
46 throw new Error("projection must complete synchronously");
47 // Detach successful output before rollback; rejects embedded functions,
48 // storage capabilities and generators instead of leaking their scope.
49 result = structuredClone(value);
50 validateIdentity();
51 throw finished;
52 });
53 } catch (error) {
54 if (error !== finished) throw error;
55 return result!;
56 } finally {
57 active = false;
58 }
59 throw new Error("storage did not propagate projection rollback");
60}