- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/projection-provider.ts?format=json
86 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 162, 2 versions changed it
1import { ExperimentalMarkerProjection } from "../../src/engine/experimental/projection-marker.ts";
2import { ProjectionReader } from "../../src/engine/experimental/projection-reader.ts";
3import type { ProjectionHandle, ProjectionIdentity } from "../../src/engine/experimental/projection-root.ts";
4import { ProjectionSnapshots } from "../../src/engine/experimental/projection-snapshots.ts";
5import type { Graph } from "../../src/engine/graph.ts";
6import type { GraphStateStorage } from "../../src/engine/state-source.ts";
7import type { Published } from "./spaces.ts";
9/** Internal host contract; canonical proofs and version authority are independent of saved handles. */
10export interface ProjectionReadBinding {
11 readonly graph: Graph;
12 readonly context: Pick<ProjectionIdentity, "database" | "namespace" | "registry" | "defaultFile">;
13 readonly sourceGeneration: number;
14 sourceLength(): number;
15 prefixDigest(length: number): string | undefined;
16 version(): number;
17 committed(): { version: number; handle: ProjectionHandle };
18 at(version: number): ProjectionHandle | undefined;
19 delta(version: number): Published | undefined;
20 namedVersion?(name: string, version: number): { sharedBase: number; atoms: Iterable<string> } | undefined;
21 /** Read-through overlay owned and cleaned by the current transaction. */
22 allocate(): GraphStateStorage;
23 /** Checks this exact transaction, graph revision and verified source binding. */
24 guard(): () => void;
25}
27/** Explicit private route. Unsupported memberships never fall back to historical View construction. */
28export function projectedReader(binding: ProjectionReadBinding, key: string): ProjectionReader {
29 const scopeGuard = binding.guard();
30 scopeGuard();
31 const sourceLength = binding.sourceLength(),
32 sourceGeneration = binding.sourceGeneration;
33 const sourceDigest = binding.prefixDigest(sourceLength);
34 if (sourceDigest === undefined) throw Error("missing verified current canonical prefix");
35 const assertLive = () => {
36 scopeGuard();
37 if (
38 binding.sourceGeneration !== sourceGeneration ||
39 binding.sourceLength() !== sourceLength ||
40 binding.prefixDigest(sourceLength) !== sourceDigest
41 )
42 throw Error("expired projection source binding");
43 };
44 assertLive();
45 const match = /^(v|a) (.*)@(0|[1-9]\d*)(?:\+(.+))?$/.exec(key);
46 if (!match || (match[1] === "v" && match[4])) throw Error(`unsupported projection key: ${key}`);
47 const requested = Number(match[3]);
48 if (!Number.isSafeInteger(requested)) throw Error("projection version outside authority");
49 const named = match[2] ? binding.namedVersion?.(match[2], requested) : undefined;
50 if (match[2] && !named) throw Error("named projection version outside authority");
51 const version = named?.sharedBase ?? requested;
52 if (!Number.isSafeInteger(version) || version > binding.version())
53 throw Error("projection version outside authority");
54 const exact = binding.at(version);
55 const anchor = exact ? { version, handle: exact } : binding.committed();
56 if (!Number.isSafeInteger(anchor.version) || anchor.version < 0 || (!exact && version <= anchor.version))
57 throw Error("missing historical version projection");
58 const length = anchor.handle.identity.sourceLength;
59 if (!Number.isSafeInteger(length) || length < 0 || length > binding.sourceLength())
60 throw Error("projection source outside authority");
61 const digest = binding.prefixDigest(length);
62 if (digest === undefined) throw Error("missing verified canonical prefix");
63 const storage = binding.allocate();
64 const context = { ...binding.context };
65 const adapter = new ExperimentalMarkerProjection(storage, binding.graph, context.defaultFile, context.namespace);
66 const snapshots = new ProjectionSnapshots(storage, adapter, context);
67 let state = snapshots.load(anchor.handle, {
68 ...context,
69 sourceGeneration: binding.sourceGeneration,
70 sourceLength: length,
71 sourceDigest: digest,
72 membership: String(anchor.version),
73 });
74 for (let next = anchor.version + 1; next <= version; next++) {
75 const delta = binding.delta(next);
76 if (!delta || delta.version !== next) throw Error("missing transaction-local version delta");
77 for (const atom of [...(delta.dropped ?? [])].map((id) => binding.graph.atom(id)).sort((a, b) => b.seq - a.seq))
78 state = adapter.removeAtom(state, atom);
79 for (const atom of delta.atoms.map((id) => binding.graph.atom(id)).sort((a, b) => a.seq - b.seq))
80 state = adapter.apply(state, atom);
81 }
82 for (const id of named?.atoms ?? []) state = adapter.apply(state, binding.graph.atom(id));
83 for (const id of match[4]?.split(",") ?? []) state = adapter.apply(state, binding.graph.atom(id));
84 assertLive();
85 return new ProjectionReader(adapter, state, assertLive);
86}