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
blame on this site's /mcp
CLI
abso blame src/engine/stored-text.ts --json
JSON
/christopher/abso-core/blame/src/engine/stored-text.ts?format=json
70 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 162,

70 lines from 1 change in 1 version.

codex-capacity-chair162Add reviewed disk-first history and indexed graph storage for core issue 16; retain runtime activation gateCompare1import type { GraphStateStorage } from "./state-source.ts";
2
3/** Exact UTF-16 text storage with bounded values; callers choose streaming or full reads. */
4export class StoredText {
5 static readonly chunkCharacters = 16384;
6 readonly storage: GraphStateStorage;
7 readonly collection: string;
8 constructor(storage: GraphStateStorage, collection: string) {
9 this.storage = storage;
10 this.collection = collection;
11 }
12 write(key: string, text: string): void {
13 this.writeChunks(
14 key,
15 (function* () {
16 for (let offset = 0; offset < text.length; offset += StoredText.chunkCharacters)
17 yield text.slice(offset, offset + StoredText.chunkCharacters);
18 })(),
19 );
20 }
21 /** Input pieces and the retained buffer are bounded independently of total text length. */
22 writeChunks(key: string, chunks: Iterable<string>): void {
23 this.storage.transaction(() => {
24 if (this.storage.read(this.collection, key) !== undefined) throw Error("stored text already exists");
25 let pending = "",
26 characters = 0,
27 parts = 0;
28 const persist = (chunk: string) =>
29 this.storage.write(`${this.collection}:chunks`, JSON.stringify([key, parts++]), chunk);
30 for (const chunk of chunks) {
31 if (typeof chunk !== "string" || chunk.length > StoredText.chunkCharacters)
32 throw Error("stored text input chunk exceeds bound");
33 characters += chunk.length;
34 if (!Number.isSafeInteger(characters)) throw Error("stored text length overflow");
35 pending += chunk;
36 if (pending.length >= StoredText.chunkCharacters) {
37 persist(pending.slice(0, StoredText.chunkCharacters));
38 pending = pending.slice(StoredText.chunkCharacters);
39 }
40 }
41 if (pending) persist(pending);
42 this.storage.write(this.collection, key, { format: 1, characters, parts });
43 });
44 }
46 *chunks(key: string): IterableIterator<string> {
47 const header = this.storage.read(this.collection, key) as
48 | { format: number; characters: number; parts: number }
49 | undefined;
50 if (
51 header?.format !== 1 ||
52 !Number.isSafeInteger(header.characters) ||
53 header.characters < 0 ||
54 header.parts !== Math.ceil(header.characters / StoredText.chunkCharacters)
55 )
56 throw Error("invalid stored text header");
57 for (let part = 0; part < header.parts; part++) {
58 const chunk = this.storage.read(`${this.collection}:chunks`, JSON.stringify([key, part]));
59 if (
60 typeof chunk !== "string" ||
61 chunk.length !== Math.min(StoredText.chunkCharacters, header.characters - part * StoredText.chunkCharacters)
62 )
63 throw Error("incomplete stored text");
64 yield chunk;
65 }
66 }
67 read(key: string): string {
68 return [...this.chunks(key)].join("");
69 }