- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/experimental/projection-root.ts?version=261&format=json
121 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 143, 1 version changed it
You're reading version 261. The newest is 318, and no version since then changed this file.
1import type { GraphStateStorage } from "../state-source.ts";
3/** Internal experiment only. No production writer selects this format. */
4export interface ProjectionIdentity {
5 readonly database: string;
6 readonly namespace: string;
7 readonly sourceGeneration: number;
8 readonly sourceLength: number;
9 readonly sourceDigest: string;
10 readonly membership: string;
11 readonly registry: string;
12 readonly defaultFile: string;
13}
14export interface ProjectionRoots {
15 readonly sequence: number | null;
16 readonly sequenceRoot: number | null;
17 readonly sequenceNext: number;
18 readonly locator: number | null;
19 readonly children: number | null;
20 readonly membership: number | null;
21 readonly attributes: number | null;
22}
23export interface ProjectionHandle {
24 readonly format: 1;
25 readonly id: number;
26 readonly identity: ProjectionIdentity;
27}
28interface RootRecord extends ProjectionHandle {
29 readonly roots: ProjectionRoots;
30}
31const fields = [
32 "database",
33 "namespace",
34 "sourceGeneration",
35 "sourceLength",
36 "sourceDigest",
37 "membership",
38 "registry",
39 "defaultFile",
40] as const;
41const rootFields = [
42 "sequence",
43 "sequenceRoot",
44 "sequenceNext",
45 "locator",
46 "children",
47 "membership",
48 "attributes",
49] as const;
50const integer = (value: unknown): value is number => Number.isSafeInteger(value) && Number(value) >= 0;
51const object = (value: unknown): value is Record<string, unknown> =>
52 value !== null && typeof value === "object" && !Array.isArray(value);
53function identity(value: unknown): asserts value is ProjectionIdentity {
54 if (!object(value) || Object.keys(value).length !== fields.length) throw Error("invalid projection identity");
55 for (const field of fields) {
56 const item = value[field];
57 if (field === "sourceGeneration" || field === "sourceLength") {
58 if (!integer(item)) throw Error("invalid projection source position");
59 } else if (typeof item !== "string" || item.length === 0 || item.length > 4096)
60 throw Error("invalid projection identity field");
61 }
62 if (!/^[a-f0-9]{64}$/.test(value.sourceDigest as string)) throw Error("invalid projection source digest");
63}
64function roots(value: unknown): asserts value is ProjectionRoots {
65 if (!object(value) || Object.keys(value).length !== rootFields.length) throw Error("invalid projection roots");
66 for (const field of rootFields)
67 if (value[field] !== null && !integer(value[field])) throw Error("invalid projection page reference");
68 if (
69 !integer(value.sequenceNext) ||
70 value.sequenceNext > 2 ** 32 ||
71 (value.sequenceRoot !== null && (!integer(value.sequenceRoot) || value.sequenceRoot >= value.sequenceNext)) ||
72 (value.sequenceRoot === null) !== (value.sequenceNext === 0) ||
73 (value.sequence === null) !== (value.sequenceRoot === null)
74 )
75 throw Error("invalid projection sequence state");
76}
77export function assertProjectionIdentity(actual: ProjectionIdentity, expected: ProjectionIdentity): void {
78 identity(actual);
79 identity(expected);
80 for (const field of fields)
81 if (actual[field] !== expected[field]) throw Error(`projection identity mismatch: ${field}`);
82}
83function collection(expected: ProjectionIdentity): string {
84 return `experimental-projection:${JSON.stringify([expected.database, expected.namespace])}:roots`;
85}
86/** Identity checks precede storage access. The host supplies trusted expected identity. */
87export function readProjectionRoot(
88 storage: GraphStateStorage,
89 handle: ProjectionHandle,
90 expected: ProjectionIdentity,
91): ProjectionRoots {
92 if (!object(handle) || handle.format !== 1 || !integer(handle.id)) throw Error("invalid projection handle");
93 assertProjectionIdentity(handle.identity, expected);
94 const record = storage.read(collection(expected), String(handle.id));
95 if (!object(record) || record.format !== 1 || record.id !== handle.id || Object.keys(record).length !== 4)
96 throw Error("invalid projection root record");
97 identity(record.identity);
98 assertProjectionIdentity(record.identity, expected);
99 roots(record.roots);
100 return structuredClone(record.roots);
101}
102/** The caller's enclosing transaction must include canonical state and its receipt. */
103export function createProjectionRoot(
104 storage: GraphStateStorage,
105 expected: ProjectionIdentity,
106 id: number,
107 pages: ProjectionRoots,
108): ProjectionHandle {
109 identity(expected);
110 roots(pages);
111 if (!integer(id)) throw Error("invalid projection root id");
112 const frozenIdentity = structuredClone(expected),
113 frozenPages = structuredClone(pages);
114 return storage.transaction(() => {
115 const name = collection(frozenIdentity);
116 if (storage.read(name, String(id)) !== undefined) throw Error("projection root already exists");
117 const record: RootRecord = { format: 1, id, identity: frozenIdentity, roots: frozenPages };
118 storage.write(name, String(id), record);
119 return { format: 1, id, identity: structuredClone(frozenIdentity) };
120 });
121}