UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

129 lines • 4.52 kB
import { z } from "zod"; import { catalogDigest, storedBytes } from "./compact-catalog-codec.js"; import { COMPACT_LEAF_CODE_UNITS } from "./compact-content-store.js"; import { CompactReadError } from "./compact-read-lease.js"; import { parseContentDigest } from "./compact-recovery-ids.js"; const openLeaf = (height) => ({ tokens: [], units: `{"h":${height},"v":[]}`.length }); const headToken = (head) => `{"s":${head.side},"d":"${head.digest}"}`; export class CatalogPointerWriter { constructor(create) { this.open = []; this.create = create; } node(node, head) { this.push(0, `{"n":${node},"s":${head.side},"d":"${head.digest}"}`); } component(leaves) { this.push(0, `{"c":${leaves.length}}`); for (const leaf of leaves) this.push(0, String(leaf)); } finish() { this.level(0); for (let height = 0;; height++) { if (height === this.open.length - 1) return this.seal(height); this.push(height + 1, headToken(this.seal(height))); } } level(height) { return this.open[height] ?? (this.open[height] = openLeaf(height)); } push(height, token) { const open = this.level(height); const units = open.units + token.length + Number(open.tokens.length > 0); if (units > COMPACT_LEAF_CODE_UNITS) { this.push(height + 1, headToken(this.seal(height))); return this.push(height, token); } open.tokens.push(token); open.units = units; } seal(height) { const text = `{"h":${height},"v":[${this.level(height).tokens.join(",")}]}`; this.open[height] = openLeaf(height); const side = this.create(text); return { side, digest: catalogDigest("pointer", storedBytes(text)) }; } } const Id = z.number().int().positive(); const SideLeafSchema = z.object({ h: z.number().int().nonnegative(), v: z.array(z.unknown()) }).strict(); const DataSchema = z.array(z.union([ Id, z.object({ c: z.number().int().nonnegative() }).strict(), z.object({ n: Id, s: Id, d: z.string() }).strict(), ])); const IndexSchema = z.array(z.object({ s: Id, d: z.string() }).strict()).min(1); const corrupt = () => new CompactReadError("integrity"); function digest(raw) { const parsed = parseContentDigest(raw); if (!parsed.ok) throw corrupt(); return parsed.value; } function decode(schema, value) { const parsed = schema.safeParse(value); if (!parsed.success) throw corrupt(); return parsed.data; } async function* tokens(read, head, height) { const text = await read(head.side); if (catalogDigest("pointer", storedBytes(text)) !== head.digest) throw corrupt(); let json; try { json = JSON.parse(text); } catch (cause) { if (cause instanceof SyntaxError) throw corrupt(); throw cause; } const leaf = decode(SideLeafSchema, json); if (height !== undefined && leaf.h !== height) throw corrupt(); if (leaf.h === 0) { yield* decode(DataSchema, leaf.v); return; } for (const below of decode(IndexSchema, leaf.v)) yield* tokens(read, { side: below.s, digest: digest(below.d) }, leaf.h - 1); } /** The pointer of child `step`, or `undefined` past the last child; a malformed list is `integrity`. */ export async function catalogPointer(read, head, step) { let child = 0; let skip = 0; let wanted = 0; let leaves; for await (const token of tokens(read, head)) { if (leaves || skip > 0) { if (typeof token !== "number") throw corrupt(); if (!leaves) skip--; else if (leaves.push(token) === wanted) return { kind: "component", leaves }; continue; } if (typeof token === "number") throw corrupt(); const here = child++; if ("n" in token) { if (here === step) return { kind: "node", node: token.n, head: { side: token.s, digest: digest(token.d) } }; } else if (here !== step) skip = token.c; else if (token.c === 0) return { kind: "component", leaves: [] }; else { leaves = []; wanted = token.c; } } if (leaves || skip > 0) throw corrupt(); return undefined; } //# sourceMappingURL=compact-catalog-pointers.js.map