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/child-counts.ts?format=json
72 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 137, 1 version changed it
1import type { GraphStateStorage } from "./state-source.ts";
2
3const PAGE_SIZE = 256;
4type Page = { format: 1; page: number; counts: number[]; digest: string };
5
6/** Two independent 32-bit checksums over page identity and contents. This
7 * detects inconsistent storage corruption, not an adversarial coherent rewrite. */
8function digest(page: number, counts: readonly number[]): string {
9 const text = JSON.stringify([1, page, counts]);
10 let a = 2166136261;
11 let b = 5381;
12 for (let i = 0; i < text.length; i++) {
13 a = Math.imul(a ^ text.charCodeAt(i), 16777619) >>> 0;
14 b = (Math.imul(b, 33) ^ text.charCodeAt(i)) >>> 0;
15 }
16 return a.toString(16).padStart(8, "0") + b.toString(16).padStart(8, "0");
19/** Dense line-node counts. Graph append owns the enclosing transaction. */
20export class ChildCounts {
21 readonly #storage: GraphStateStorage;
22 constructor(storage: GraphStateStorage) {
23 this.#storage = storage;
24 }
25 #read(page: number, total: number): Page {
26 const value = this.#storage.read("ordered-child-counts", String(page)) as Page | undefined;
27 const expected = Math.min(PAGE_SIZE, total - page * PAGE_SIZE);
28 if (
29 !value ||
30 value.format !== 1 ||
31 value.page !== page ||
32 !Array.isArray(value.counts) ||
33 expected < 1 ||
34 value.counts.length !== expected ||
35 Object.keys(value.counts).length !== expected ||
36 value.counts.some((count) => !Number.isSafeInteger(count) || count < 0) ||
37 value.digest !== digest(page, value.counts)
38 )
39 throw new Error("invalid ordered child count page");
40 return value;
41 }
42 #write(page: number, counts: number[]): void {
43 this.#storage.write("ordered-child-counts", String(page), {
44 format: 1,
45 page,
46 counts,
47 digest: digest(page, counts),
48 });
49 }
50 at(index: number, total: number): number {
51 if (!Number.isSafeInteger(index) || index < 0 || index >= total)
52 throw new Error("ordered child count out of bounds");
53 return this.#read(Math.floor(index / PAGE_SIZE), total).counts[index % PAGE_SIZE]!;
54 }
55 append(total: number): void {
56 const page = Math.floor(total / PAGE_SIZE);
57 const offset = total % PAGE_SIZE;
58 const counts = offset === 0 ? [] : this.#read(page, total).counts;
59 if (offset === 0 && this.#storage.read("ordered-child-counts", String(page)) !== undefined)
60 throw new Error("unexpected existing ordered child count page");
61 counts.push(0);
62 this.#write(page, counts);
63 }
64 increment(index: number, total: number): void {
65 const count = this.at(index, total);
66 if (count === Number.MAX_SAFE_INTEGER) throw new Error("ordered child count exhausted");
67 const page = Math.floor(index / PAGE_SIZE);
68 const counts = this.#read(page, total).counts;
69 counts[index % PAGE_SIZE] = count + 1;
70 this.#write(page, counts);
71 }