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

Versions 110 to 318

Compare

+60 −0 in 1 place
new file, 60 lines
1+import type { GraphStateStorage } from "./state-source.ts";
2+
3+let nextScope = 0;
4+
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. */
8+export 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+}
MCP
compare on this site's /mcp
CLI
abso diff 110 318 src/engine/projection-scratch.ts --json
JSON
/christopher/abso-core/compare/110..318?file=src%2Fengine%2Fprojection-scratch.ts&format=json