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

Compare

+73 −0 in 1 place
new file, 73 lines
1+import { CachedStateStorage } from "../../src/engine/state-source.ts";
2+import type { WriterAdoptionFrame } from "../../worker/project/project.ts";
3+import type { Entry } from "../../worker/project/project-types.ts";
4+import type { SqliteLog, SqliteStorage } from "./engine-project.ts";
5+import { smallFrameJson } from "./small-frame-json.ts";
6+import { StoredJson, storeJson } from "./stored-json.ts";
7+import { storedJsonView } from "./stored-json-view.ts";
8+import { SqliteWriterState, writerFrameChunksDigest } from "./writer-host.ts";
9+
10+/** Keep decoded views alive through the complete page checkpoint, then remove their rows. */
11+export function withDecodedFrames<T>(
12+ storage: SqliteStorage,
13+ namespace: string,
14+ log: SqliteLog,
15+ input: { from: number; to: number; maxEntries: number; maxBytes: number; digest: string },
16+ consume: (frames: Iterable<WriterAdoptionFrame>) => T,
17+): { result: T; digest: string } {
18+ if (
19+ ![input.from, input.to, input.maxEntries, input.maxBytes].every(Number.isSafeInteger) ||
20+ input.from < 0 ||
21+ input.to < input.from ||
22+ input.maxEntries < 1 ||
23+ input.maxEntries > 256 ||
24+ input.maxBytes < 1
25+ )
26+ throw Error("invalid decoded frame page bounds");
27+ return storage.transactionSync(() => {
28+ SqliteWriterState.initialize(storage);
29+ const state = new SqliteWriterState(storage, namespace);
30+ // Bound cached reads across all disk-backed frames in this page.
31+ const reads = new CachedStateStorage(state, 64 * 1024, 256);
32+ if (
33+ storage.sql.exec("SELECT 1 AS present FROM writer_state WHERE namespace = ? LIMIT 1", namespace).toArray().length
34+ )
35+ throw Error("decoded frame namespace is occupied");
36+ let digest = input.digest;
37+ let active = true;
38+ const frames = function* (): IterableIterator<WriterAdoptionFrame> {
39+ let bytes = 0;
40+ for (let seq = input.from; seq < input.to && seq < input.from + input.maxEntries; seq++) {
41+ if (!active) throw Error("decoded frame page has ended");
42+ const size = log.frameBytes(seq);
43+ if (size > input.maxBytes - bytes) {
44+ if (bytes) return;
45+ throw Error("decoded frame exceeds page byte bound");
46+ }
47+ digest = writerFrameChunksDigest(digest, seq, log.frameChunks(seq));
48+ bytes += size;
49+ if (size <= 4096) {
50+ yield { seq, bytes: size, entry: smallFrameJson(log.frameChunks(seq)) as Entry };
51+ } else {
52+ storeJson(state, String(seq), log.frameChunks(seq), { finiteNumbers: true });
53+ yield { seq, bytes: size, entry: storedJsonView(new StoredJson(reads, String(seq))) as Entry };
54+ }
55+ }
56+ };
57+ try {
58+ const result = consume(frames());
59+ if (
60+ result &&
61+ typeof result === "object" &&
62+ (typeof (result as { then?: unknown }).then === "function" ||
63+ typeof (result as { next?: unknown }).next === "function")
64+ )
65+ throw Error("decoded frame consumption must finish synchronously");
66+ return { result: structuredClone(result), digest };
67+ } finally {
68+ active = false;
69+ reads.clear();
70+ storage.sql.exec("DELETE FROM writer_state WHERE namespace = ?", namespace);
71+ }
72+ });
73+}
MCP
compare on this site's /mcp
CLI
abso diff 57 318 server/src/decoded-frames.ts --json
JSON
/christopher/abso-core/compare/57..318?file=server%2Fsrc%2Fdecoded-frames.ts&format=json