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-atom-index.ts?format=json
181 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 165, 3 versions changed it
1import { createHash } from "node:crypto";
2import { jsonValueChunks } from "../json-value-chunks.ts";
3import type { GraphStateStorage } from "../state-source.ts";
4import type { Atom, AttrSet, Relocation } from "../types.ts";
5
6type Field = "deps" | "inserts" | "deletes" | "attrs" | "moves" | "files";
7interface Run {
8 file: string;
9 parent: string;
10 lines: number;
12interface Header {
13 format: 1;
14 id: string;
15 seq: number;
16 digest: string;
17 counts: Record<Field, number>;
18 checksum: string;
20const fields: Field[] = ["deps", "inserts", "deletes", "attrs", "moves", "files"];
22// Checksums detect record damage; authority comes from the canonical writer transaction.
23const checksum = (value: unknown): string => createHash("sha256").update(JSON.stringify(value)).digest("hex");
24const headerChecksum = (header: Omit<Header, "checksum">): string =>
25 checksum([header.format, header.id, header.seq, header.digest, fields.map((field) => header.counts[field])]);
27function shape(value: unknown, keys: readonly string[]): asserts value is Record<string, unknown> {
28 if (
29 !value ||
30 typeof value !== "object" ||
31 Array.isArray(value) ||
32 Object.keys(value).length !== keys.length ||
33 keys.some((key) => !Object.hasOwn(value, key))
34 )
35 throw Error("invalid projection atom record");
37const integer = (value: unknown): value is number => Number.isSafeInteger(value) && Number(value) >= 0;
38const atomId = (value: unknown): value is string =>
39 typeof value === "string" && /^a(0|[1-9][0-9]*)$/.test(value) && integer(Number(value.slice(1)));
40const lineId = (value: unknown): value is string =>
41 typeof value === "string" &&
42 /^a(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$/.test(value) &&
43 value.split(".").every((part, i) => integer(Number(i === 0 ? part.slice(1) : part)));
44const parent = (value: unknown) => typeof value === "string" && (value.startsWith("^") || lineId(value));
45function itemValue(field: Field, value: unknown): unknown {
46 if (field === "deps" || field === "deletes" || field === "files") {
47 if (!(field === "deps" ? atomId(value) : field === "deletes" ? lineId(value) : typeof value === "string"))
48 throw Error("invalid projection atom item");
49 } else if (field === "inserts") {
50 shape(value, ["file", "parent", "lines"]);
51 if (typeof value.file !== "string" || !parent(value.parent) || !integer(value.lines))
52 throw Error("invalid projection atom run");
53 } else if (field === "moves") {
54 shape(value, ["line", "parent"]);
55 if (!lineId(value.line) || !parent(value.parent)) throw Error("invalid projection atom move");
56 } else {
57 shape(value, ["file", "name", "value"]);
58 if (
59 typeof value.file !== "string" ||
60 !["exists", "exec", "link", "blob"].includes(String(value.name)) ||
61 (value.value !== null && typeof value.value !== "string")
62 )
63 throw Error("invalid projection atom attribute");
64 }
65 // Every validated record field is scalar; detach proxy-backed records without cloning a container graph.
66 return value && typeof value === "object" ? { ...value } : value;
69/** Derived index only. The caller supplies an already validated canonical atom and
70 * its digest inside the canonical indexing transaction. This builder does not
71 * claim bounded decoding of the original atom. */
72export class ProjectionAtomIndex {
73 readonly storage: GraphStateStorage;
74 constructor(storage: GraphStateStorage) {
75 this.storage = storage;
76 }
77 put(atom: Atom, digest: string, files: readonly string[]): void {
78 if (!/^[a-f0-9]{64}$/.test(digest) || !Number.isSafeInteger(atom.seq) || atom.seq < 0 || atom.id !== `a${atom.seq}`)
79 throw Error("invalid projection atom identity");
80 const name = `projection-atom-index:${atom.id}`;
81 this.storage.transaction(() => {
82 if (this.storage.read(name, "header") !== undefined) throw Error("projection atom index already exists");
83 const counts = {} as Record<Field, number>;
84 for (const field of fields) {
85 const values = field === "files" ? files : (atom[field] ?? []);
86 counts[field] = values.length;
87 for (let i = 0; i < values.length; i++) {
88 const value =
89 field === "inserts"
90 ? { file: atom.inserts[i]!.file, parent: atom.inserts[i]!.parent, lines: atom.inserts[i]!.lines.length }
91 : values[i];
92 const item = itemValue(field, value);
93 this.storage.write(name, `${field}:${i}`, {
94 value: item,
95 checksum: checksum([atom.id, digest, field, i, item]),
96 });
97 }
98 }
99 const header = { format: 1 as const, id: atom.id, seq: atom.seq, digest, counts };
100 this.storage.write(name, "header", { ...header, checksum: headerChecksum(header) } satisfies Header);
101 });
102 }
103 /** Read a descriptor from a source-fenced, atomically maintained writer index. */
104 openAccepted(id: string): ProjectionAtomDescriptor {
105 if (!atomId(id)) throw Error("invalid projection atom identity");
106 const header = this.storage.read(`projection-atom-index:${id}`, "header");
107 shape(header, ["format", "id", "seq", "digest", "counts", "checksum"]);
108 if (typeof header.digest !== "string") throw Error("invalid projection atom digest");
109 return this.open(id, header.digest);
110 }
112 open(id: string, digest: string): ProjectionAtomDescriptor {
113 if (!/^a(0|[1-9][0-9]*)$/.test(id) || !/^[a-f0-9]{64}$/.test(digest))
114 throw Error("invalid projection atom identity");
115 const name = `projection-atom-index:${id}`;
116 const raw = this.storage.read(name, "header");
117 shape(raw, ["format", "id", "seq", "digest", "counts", "checksum"]);
118 shape(raw.counts, fields);
119 const header = raw as unknown as Header;
120 if (
121 header.checksum !== headerChecksum(header) ||
122 header.format !== 1 ||
123 header.id !== id ||
124 header.digest !== digest ||
125 !Number.isSafeInteger(header.seq) ||
126 header.seq < 0 ||
127 id !== `a${header.seq}` ||
128 !header.counts ||
129 Object.keys(header.counts).length !== fields.length ||
130 fields.some((field) => !Number.isSafeInteger(header.counts[field]) || header.counts[field] < 0)
131 )
132 throw Error("missing or invalid projection atom index");
133 return new ProjectionAtomDescriptor(this.storage, name, structuredClone(header));
134 }
136export class ProjectionAtomDescriptor {
137 readonly id: string;
138 readonly seq: number;
139 readonly digest: string;
140 readonly #storage: GraphStateStorage;
141 readonly #name: string;
142 readonly #counts: Record<Field, number>;
143 constructor(storage: GraphStateStorage, name: string, header: Header) {
144 this.#storage = storage;
145 this.#name = name;
146 this.id = header.id;
147 this.seq = header.seq;
148 this.digest = header.digest;
149 this.#counts = { ...header.counts };
150 }
151 related(id: string): ProjectionAtomDescriptor {
152 return new ProjectionAtomIndex(this.#storage).openAccepted(id);
153 }
155 count(field: Field): number {
156 if (!fields.includes(field)) throw Error("invalid projection atom field");
157 return this.#counts[field];
158 }
159 item(field: "inserts", index: number): Run;
160 item(field: "attrs", index: number): AttrSet;
161 item(field: "moves", index: number): Relocation;
162 item(field: "deps" | "deletes" | "files", index: number): string;
163 item(field: Field, index: number): unknown {
164 if (!Number.isSafeInteger(index) || index < 0 || index >= this.count(field))
165 throw Error("projection atom item out of range");
166 const value = this.#storage.read(this.#name, `${field}:${index}`);
167 if (value === undefined) throw Error("missing projection atom item");
168 shape(value, ["value", "checksum"]);
169 if (value.checksum !== checksum([this.id, this.digest, field, index, value.value]))
170 throw Error("invalid projection atom item checksum");
171 return itemValue(field, value.value);
172 }
175/** Opt-in persistent Graph callback: indexes the just-accepted canonical atom. */
176export function indexAcceptedProjectionAtom(atom: Atom, files: readonly string[], storage: GraphStateStorage): void {
177 const hash = createHash("sha256");
178 for (const chunk of jsonValueChunks(atom)) hash.update(chunk);
179 const digest = hash.digest("hex");
180 new ProjectionAtomIndex(storage).put(atom, digest, files);