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 73 to 318

Compare

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