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 110 to 111

Compare

+77 −0 in 1 place
Lines 13–95 at 111 (were 13–18 at 110)
1313 transaction<T>(body: () => T): T;
1414}
1515
16+/**
17+ * Operation-local read cache with fixed payload and entry limits. Serialized
18+ * values preserve the storage copy contract. Discard it after an outer host
19+ * rollback or any mutation made through another storage instance.
20+ */
21+export class CachedStateStorage implements GraphStateStorage {
22+ readonly #source: GraphStateStorage;
23+ readonly #cache = new Map<string, { json: string; bytes: number }>();
24+ readonly #maxBytes: number;
25+ readonly #maxEntries: number;
26+ #bytes = 0;
27+ constructor(source: GraphStateStorage, maxBytes = 1_048_576, maxEntries = 4096) {
28+ if (!Number.isSafeInteger(maxBytes) || maxBytes < 0 || !Number.isSafeInteger(maxEntries) || maxEntries < 0)
29+ throw new Error("invalid writer cache limits");
30+ this.#source = source;
31+ this.#maxBytes = maxBytes;
32+ this.#maxEntries = maxEntries;
33+ }
34+ get retainedBytes(): number {
35+ return this.#bytes;
36+ }
37+ get retainedEntries(): number {
38+ return this.#cache.size;
39+ }
40+ clear(): void {
41+ this.#cache.clear();
42+ this.#bytes = 0;
43+ }
44+ #drop(key: string): void {
45+ const previous = this.#cache.get(key);
46+ if (previous) {
47+ this.#cache.delete(key);
48+ this.#bytes -= previous.bytes;
49+ }
50+ }
51+ read(collection: string, key: string): unknown {
52+ const cacheKey = JSON.stringify([collection, key]);
53+ const hit = this.#cache.get(cacheKey);
54+ if (hit) {
55+ this.#cache.delete(cacheKey);
56+ this.#cache.set(cacheKey, hit);
57+ return (JSON.parse(hit.json) as { value?: unknown }).value;
58+ }
59+ const value = this.#source.read(collection, key);
60+ const json = JSON.stringify({ value });
61+ // Charge two bytes per UTF-16 unit and a conservative fixed entry cost.
62+ // This bounds retained payload; it is not an isolate heap measurement.
63+ const bytes = 2 * (json.length + cacheKey.length) + 128;
64+ if (bytes <= this.#maxBytes && this.#maxEntries > 0) {
65+ while (this.#bytes + bytes > this.#maxBytes || this.#cache.size >= this.#maxEntries)
66+ this.#drop(this.#cache.keys().next().value!);
67+ this.#cache.set(cacheKey, { json, bytes });
68+ this.#bytes += bytes;
69+ }
70+ return value;
71+ }
72+ write(collection: string, key: string, value: unknown): void {
73+ this.#drop(JSON.stringify([collection, key]));
74+ this.#source.write(collection, key, value);
75+ }
76+ remove(collection: string, key: string): void {
77+ this.#drop(JSON.stringify([collection, key]));
78+ this.#source.remove(collection, key);
79+ }
80+ entries(collection: string): Iterable<readonly [string, unknown]> {
81+ return this.#source.entries(collection);
82+ }
83+ transaction<T>(body: () => T): T {
84+ try {
85+ return this.#source.transaction(body);
86+ } catch (error) {
87+ this.clear();
88+ throw error;
89+ }
90+ }
91+}
1693/** Immutable membership whose implementation does not retain its enumerated keys. */
1794export abstract class StableSet<T> implements ReadonlySet<T> {
1895 abstract has(value: T): boolean;
MCP
compare on this site's /mcp
CLI
abso diff 110 111 src/engine/state-source.ts --json
JSON
/christopher/abso-core/compare/110..111?file=src%2Fengine%2Fstate-source.ts&format=json