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 148 to 149

Compare

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