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 161 to 162

Compare

+150 −0 in 1 place
new file, 150 lines
1+import type { GraphStateStorage } from "../../src/engine/state-source.ts";
2+import { StoredText } from "../../src/engine/stored-text.ts";
3+import { type JsonEvent, jsonEvents } from "./json-events.ts";
4+
5+/** Temporary decoded JSON on disk. A root is published only after complete parsing. */
6+export function storeJson(storage: GraphStateStorage, namespace: string, chunks: Iterable<string>): number {
7+ return storage.transaction(() => {
8+ if (storage.read(namespace, "root") !== undefined) throw Error("decoded JSON already exists");
9+ const events = jsonEvents(chunks);
10+ let nextId = 0;
11+ const take = (): JsonEvent => {
12+ const event = events.next();
13+ if (event.done) throw Error("truncated JSON events");
14+ return event.value;
15+ };
16+ const text = new StoredText(storage, `${namespace}:text`);
17+ const value = (event = take()): number => {
18+ const id = nextId++;
19+ if (!Number.isSafeInteger(nextId)) throw Error("decoded JSON identity overflow");
20+ if (event.kind === "value")
21+ storage.write(namespace, String(id), {
22+ kind: "value",
23+ value: typeof event.value === "number" ? String(event.value) : event.value,
24+ number: typeof event.value === "number",
25+ negativeZero: Object.is(event.value, -0),
26+ });
27+ else if (event.kind === "string") {
28+ text.writeChunks(
29+ String(id),
30+ (function* () {
31+ for (;;) {
32+ const part = take();
33+ if (part.kind === "string-end") return;
34+ if (part.kind !== "text") throw Error("invalid JSON string events");
35+ yield part.value;
36+ }
37+ })(),
38+ );
39+ storage.write(namespace, String(id), { kind: "string" });
40+ } else if (event.kind === "array" || event.kind === "object") {
41+ let count = 0;
42+ for (;;) {
43+ const child = take();
44+ if (child.kind === "end") break;
45+ if (event.kind === "object" && child.kind !== "key") throw Error("invalid JSON object events");
46+ const key = child.kind === "key" ? child.value : String(count);
47+ const childId = value(event.kind === "object" ? take() : child);
48+ const edge = JSON.stringify([id, key]);
49+ if (event.kind === "object" && storage.read(`${namespace}:edges`, edge) === undefined) {
50+ storage.write(`${namespace}:keys`, JSON.stringify([id, count]), key);
51+ count++;
52+ }
53+ storage.write(`${namespace}:edges`, edge, childId);
54+ if (event.kind === "array") count++;
55+ }
56+ storage.write(namespace, String(id), { kind: event.kind, count });
57+ } else throw Error("invalid JSON value event");
58+ return id;
59+ };
60+ try {
61+ const root = value();
62+ if (!events.next().done) throw Error("trailing JSON events");
63+ storage.write(namespace, "root", { format: 1, id: root, nodes: nextId });
64+ return root;
65+ } finally {
66+ events.return?.();
67+ }
68+ });
69+}
71+/** A disk-backed JSON node. Child lookup does not decode siblings or string payloads. */
72+export class StoredJson {
73+ readonly storage: GraphStateStorage;
74+ readonly namespace: string;
75+ readonly id: number;
76+ readonly nodes: number;
77+ constructor(storage: GraphStateStorage, namespace: string, id?: number) {
78+ this.storage = storage;
79+ this.namespace = namespace;
80+ const root = storage.read(namespace, "root") as { format: number; id: number; nodes: number } | undefined;
81+ if (root?.format !== 1 || !Number.isSafeInteger(root.nodes) || root.nodes < 1 || root.id !== 0)
82+ throw Error("invalid stored JSON root");
83+ this.nodes = root.nodes;
84+ this.id = id ?? root.id;
85+ if (!Number.isSafeInteger(this.id) || this.id < 0 || this.id >= this.nodes)
86+ throw Error("invalid stored JSON identity");
87+ }
88+ private node(): { kind: string; count?: number; value?: unknown; number?: boolean; negativeZero?: boolean } {
89+ const node = this.storage.read(this.namespace, String(this.id)) as
90+ | { kind: string; count?: number; value?: unknown; number?: boolean; negativeZero?: boolean }
91+ | undefined;
92+ if (!node || !["array", "object", "string", "value"].includes(node.kind)) throw Error("invalid stored JSON node");
93+ return node;
94+ }
95+ get kind(): string {
96+ return this.node().kind;
97+ }
98+ get length(): number {
99+ const node = this.node();
100+ if (node.kind !== "array" || !Number.isSafeInteger(node.count) || node.count! < 0)
101+ throw Error("invalid stored JSON array");
102+ return node.count!;
103+ }
104+ child(key: string | number): StoredJson | undefined {
105+ const node = this.node();
106+ if (node.kind !== "object" && node.kind !== "array") throw Error("stored JSON node is not a container");
107+ if (
108+ node.kind === "array" &&
109+ (typeof key !== "number" || !Number.isSafeInteger(key) || key < 0 || key >= this.length)
110+ )
111+ return undefined;
112+ const id = this.storage.read(`${this.namespace}:edges`, JSON.stringify([this.id, String(key)]));
113+ if (id === undefined) {
114+ if (node.kind === "array") throw Error("missing stored JSON array element");
115+ return undefined;
116+ }
117+ if (typeof id !== "number") throw Error("invalid stored JSON edge");
118+ return new StoredJson(this.storage, this.namespace, id);
119+ }
120+ *elements(): IterableIterator<StoredJson> {
121+ const length = this.length;
122+ for (let index = 0; index < length; index++) yield this.child(index)!;
123+ }
124+ *properties(): IterableIterator<[string, StoredJson]> {
125+ const node = this.node();
126+ if (node.kind !== "object" || !Number.isSafeInteger(node.count) || node.count! < 0)
127+ throw Error("invalid stored JSON object");
128+ for (let index = 0; index < node.count!; index++) {
129+ const key = this.storage.read(`${this.namespace}:keys`, JSON.stringify([this.id, index]));
130+ if (typeof key !== "string") throw Error("missing stored JSON object key");
131+ const child = this.child(key);
132+ if (!child) throw Error("missing stored JSON object member");
133+ yield [key, child];
134+ }
135+ }
136+ *textChunks(): IterableIterator<string> {
137+ if (this.kind !== "string") throw Error("stored JSON node is not text");
138+ yield* new StoredText(this.storage, `${this.namespace}:text`).chunks(String(this.id));
139+ }
140+ scalar(): number | boolean | null {
141+ const node = this.node();
142+ if (node.kind !== "value") throw Error("stored JSON node is not a scalar");
143+ if (node.number) {
144+ if (typeof node.value !== "string" || Number.isNaN(Number(node.value))) throw Error("invalid stored JSON number");
145+ return node.negativeZero ? -0 : Number(node.value);
146+ }
147+ if (node.value === null || typeof node.value === "boolean" || typeof node.value === "number") return node.value;
148+ throw Error("invalid stored JSON scalar");
149+ }
150+}
MCP
compare on this site's /mcp
CLI
abso diff 161 162 server/src/stored-json.ts --json
JSON
/christopher/abso-core/compare/161..162?file=server%2Fsrc%2Fstored-json.ts&format=json