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
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/src/engine/experimental/projection-snapshots.ts?format=json
82 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 143, 1 version changed it
1import type { GraphStateStorage } from "../state-source.ts";
2import type { ExperimentalMarkerProjection, MarkerState } from "./projection-marker.ts";
3import {
4 assertProjectionIdentity,
5 createProjectionRoot,
6 type ProjectionHandle,
7 type ProjectionIdentity,
8 readProjectionRoot,
9} from "./projection-root.ts";
11type Context = Pick<ProjectionIdentity, "database" | "namespace" | "registry" | "defaultFile">;
12/** Internal snapshot boundary. The host supplies verified canonical and membership identities. */
13export class ProjectionSnapshots {
14 readonly #storage: GraphStateStorage;
15 readonly #adapter: ExperimentalMarkerProjection;
16 readonly #context: Context;
17 readonly #collection: string;
18 constructor(storage: GraphStateStorage, adapter: ExperimentalMarkerProjection, context: Context) {
19 this.#storage = storage;
20 this.#adapter = adapter;
21 this.#context = {
22 database: context.database,
23 namespace: context.namespace,
24 registry: context.registry,
25 defaultFile: context.defaultFile,
26 };
27 this.#collection = `experimental-projection:${JSON.stringify([context.database, context.namespace])}:context`;
28 if (adapter.context !== JSON.stringify([context.namespace, context.defaultFile]))
29 throw Error("projection snapshot constructor mismatch");
30 }
31 #expected(expected: ProjectionIdentity): void {
32 assertProjectionIdentity(expected, expected);
33 for (const field of ["database", "namespace", "registry", "defaultFile"] as const)
34 if (expected[field] !== this.#context[field]) throw Error(`projection snapshot context mismatch: ${field}`);
35 }
36 #registry(create: boolean): void {
37 const stored = this.#storage.read(this.#collection, "binding");
38 if (stored === undefined) {
39 if (!create) throw Error("missing projection registry binding");
40 this.#storage.write(this.#collection, "binding", this.#context);
41 return;
42 }
43 if (!stored || typeof stored !== "object" || Array.isArray(stored) || Object.keys(stored).length !== 4)
44 throw Error("invalid projection registry binding");
45 for (const field of ["database", "namespace", "registry", "defaultFile"] as const)
46 if ((stored as Record<string, unknown>)[field] !== this.#context[field])
47 throw Error("projection registry binding mismatch");
48 }
49 save(id: number, expected: ProjectionIdentity, state: MarkerState): ProjectionHandle {
50 this.#expected(expected);
51 this.#adapter.assertContext(state);
52 return this.#storage.transaction(() => {
53 this.#registry(true);
54 if (state.sequence.root !== null) this.#adapter.sequence.rank(state.sequence, state.sequence.root);
55 return createProjectionRoot(this.#storage, expected, id, {
56 sequence: state.sequence.map,
57 sequenceRoot: state.sequence.root,
58 sequenceNext: state.sequence.next,
59 locator: state.locator,
60 children: state.children,
61 membership: state.membership,
62 attributes: state.attributeFiles,
63 });
64 });
65 }
66 load(handle: ProjectionHandle, expected: ProjectionIdentity): MarkerState {
67 this.#expected(expected);
68 assertProjectionIdentity(handle.identity, expected);
69 this.#registry(false);
70 const roots = readProjectionRoot(this.#storage, handle, expected);
71 const state: MarkerState = {
72 context: this.#adapter.context,
73 sequence: { map: roots.sequence, root: roots.sequenceRoot, next: roots.sequenceNext },
74 locator: roots.locator,
75 children: roots.children,
76 membership: roots.membership,
77 attributeFiles: roots.attributes,
78 };
79 if (state.sequence.root !== null) this.#adapter.sequence.rank(state.sequence, state.sequence.root);
80 return state;
81 }