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

Compare

+145 −14 in 9 places
Lines 30–60 at 318 (were 30–41 at 205)
3030 readonly #maxBytes: number;
3131 readonly #maxEntries: number;
3232 #bytes = 0;
33− constructor(source: GraphStateStorage, maxBytes = 1_048_576, maxEntries = 4096) {
34− if (!Number.isSafeInteger(maxBytes) || maxBytes < 0 || !Number.isSafeInteger(maxEntries) || maxEntries < 0)
33+ /**
34+ * The keys written or removed in each open transaction, innermost last (abso Capacity). A rollback drops only those:
35+ * every projection scratch scope ends in a rollback, and dropping the whole cache there read the same rows from
36+ * storage again within a call and on every call of a kept reader.
37+ */
38+ readonly #touched: Set<string>[] = [];
39+ /**
40+ * The biggest one row may be and still be kept (abso Capacity). A few big rows read on every call (each agent with
41+ * all the news it was told: 4.3 MB for abso's 109 agents) otherwise push every small page out of the cache.
42+ */
43+ readonly #maxEntryBytes: number;
44+ constructor(source: GraphStateStorage, maxBytes = 1_048_576, maxEntries = 4096, maxEntryBytes = maxBytes) {
45+ if (
46+ !Number.isSafeInteger(maxBytes) ||
47+ maxBytes < 0 ||
48+ !Number.isSafeInteger(maxEntries) ||
49+ maxEntries < 0 ||
50+ !Number.isSafeInteger(maxEntryBytes) ||
51+ maxEntryBytes < 0
52+ )
3553 throw new Error("invalid writer cache limits");
3654 this.#source = source;
3755 this.#maxBytes = maxBytes;
3856 this.#maxEntries = maxEntries;
57+ this.#maxEntryBytes = Math.min(maxEntryBytes, maxBytes);
3958 }
4059 get retainedBytes(): number {
4160 return this.#bytes;
Lines 108–121 at 318 (were 89–100 at 205)
89108 entry = { kind: "json", json, bytes: overhead + 2 * json.length };
90109 }
91110 this.#remember(cacheKey, entry);
111+ // A projection scope's own rows are never read again once it ends in its rollback: forget them with it.
112+ if (collection.startsWith("projection-scratch:")) this.#touched.at(-1)?.add(cacheKey);
92113 return value;
93114 }
94115 #remember(cacheKey: string, entry: CachedValue): void {
95116 this.#drop(cacheKey);
96117 // Payload accounting is bounded, not a measurement of actual isolate heap.
97− if (entry.bytes <= this.#maxBytes && this.#maxEntries > 0) {
118+ if (entry.bytes <= this.#maxEntryBytes && this.#maxEntries > 0) {
98119 while (this.#bytes + entry.bytes > this.#maxBytes || this.#cache.size >= this.#maxEntries)
99120 this.#drop(this.#cache.keys().next().value!);
100121 this.#cache.set(cacheKey, entry);
Lines 125–131 at 318 (were 104–109 at 205)
104125 write(collection: string, key: string, value: unknown): void {
105126 const cacheKey = JSON.stringify([collection, key]);
106127 this.#drop(cacheKey);
128+ this.#touched.at(-1)?.add(cacheKey);
107129 this.#source.write(collection, key, value);
108130 // Allocation counters are immutable scalars. Keep successful writes within
109131 // the existing cache budget instead of immediately reading them back.
Lines 133–160 at 318 (were 111–127 at 205)
111133 this.#remember(cacheKey, { kind: "scalar", value, bytes: 2 * cacheKey.length + 176 });
112134 }
113135 remove(collection: string, key: string): void {
114− this.#drop(JSON.stringify([collection, key]));
136+ const cacheKey = JSON.stringify([collection, key]);
137+ this.#drop(cacheKey);
138+ this.#touched.at(-1)?.add(cacheKey);
115139 this.#source.remove(collection, key);
116140 }
117141 entries(collection: string): Iterable<readonly [string, unknown]> {
118142 return this.#source.entries(collection);
119143 }
120144 transaction<T>(body: () => T): T {
145+ const touched = new Set<string>();
146+ this.#touched.push(touched);
121147 try {
122− return this.#source.transaction(body);
148+ const result = this.#source.transaction(body);
149+ this.#touched.pop();
150+ // Committed into the outer transaction, which may still roll back.
151+ const outer = this.#touched.at(-1);
152+ if (outer) for (const key of touched) outer.add(key);
153+ return result;
123154 } catch (error) {
124− this.clear();
155+ this.#touched.pop();
156+ // What the transaction wrote, and anything read back after it wrote it, is gone with the rollback.
157+ for (const key of touched) this.#drop(key);
125158 throw error;
126159 }
127160 }
Lines 348–356 at 318 (were 315–321 at 205)
315348 return new Proxy([] as T[], {
316349 get(target, key, receiver) {
317350 if (key === "length") return length();
318− if (indexKey(key)) return Number(key) < length() ? read(key) : undefined;
351+ // No length read first (abso Capacity): set writes the row and the length together and nothing shrinks, so no
352+ // row lies past the length. Half of a cold layout's reads were this length.
353+ if (indexKey(key)) return read(key);
319354 return Reflect.get(target, key, receiver);
320355 },
321356 set(_target, key, value) {
Lines 421–434 at 318 (were 386–391 at 205)
386421 }
387422 for (const [key] of storage.entries(`${name}:lengths`)) yield key;
388423 },
424+ /** Values [index * PAGE, (index + 1) * PAGE) of `key`'s list: one page, not the whole list (abso Capacity). */
425+ pageAt(key: string, index: number): readonly V[] {
426+ const count = length(key);
427+ const values = page(key, index);
428+ if (values.length !== Math.min(pageSize, count - index * pageSize))
429+ throw new Error(`incomplete writer list page in ${name}`);
430+ return values;
431+ },
389432 *values(key: string): IterableIterator<V> {
390433 const count = length(key);
391434 for (let start = 0; start < count; start += pageSize) {
Lines 456–478 at 318 (were 413–418 at 205)
413456 storage.write(`${name}:pages`, pageKey(key, index), values);
414457 storage.write(`${name}:lengths`, key, count + 1);
415458 },
459+ /** Append several values, writing each page they touch once. */
460+ appendAll(key: string, more: readonly V[]): void {
461+ if (more.length === 0) return;
462+ const count = length(key);
463+ let index = Math.floor(count / pageSize);
464+ let values = count % pageSize === 0 ? [] : page(key, index);
465+ if (values.length !== count % pageSize) throw new Error(`invalid writer list length in ${name}`);
466+ for (const value of more) {
467+ if (values.length === pageSize) {
468+ storage.write(`${name}:pages`, pageKey(key, index++), values);
469+ values = [];
470+ }
471+ values.push(value);
472+ }
473+ storage.write(`${name}:pages`, pageKey(key, index), values);
474+ storage.write(`${name}:lengths`, key, count + more.length);
475+ },
416476 };
417477}
418478
Lines 626–695 at 318 (were 566–571 at 205)
566626}
567627
568628/** Read-only array access to individually stored values, without copying the list. */
629+/** How many values an atom's long list keeps to a row (readonlyPagedArray). */
630+export const LIST_PAGE = 256;
632+/** Write a list as readonlyPagedArray reads it: its values LIST_PAGE to a row, and its length. */
633+export function writePagedArray(storage: GraphStateStorage, name: string, values: readonly unknown[]): void {
634+ for (let start = 0; start < values.length; start += LIST_PAGE)
635+ storage.write(`${name}:pages`, String(start / LIST_PAGE), values.slice(start, start + LIST_PAGE));
636+ storage.write(`${name}:meta`, "length", values.length);
637+}
639+/**
640+ * A stored list read a page of LIST_PAGE values at a time (abso Capacity): an atom's long lists, which
641+ * readonlyStoredArray reads a row per value. Collisions read every key a draft's atoms write; a draft deleting a big
642+ * file has thousands, and that was a row each.
643+ */
644+export function readonlyPagedArray<T>(storage: GraphStateStorage, name: string): readonly T[] {
645+ const length = () => {
646+ const count = storage.read(`${name}:meta`, "length");
647+ if (!Number.isSafeInteger(count) || (count as number) < 0) throw Error("invalid stored list length");
648+ return count as number;
649+ };
650+ length();
651+ let held = -1;
652+ let page: readonly unknown[] = [];
653+ const at = (index: number): T => {
654+ const p = Math.floor(index / LIST_PAGE);
655+ if (p !== held) {
656+ const raw = storage.read(`${name}:pages`, String(p));
657+ if (!Array.isArray(raw)) throw Error("missing stored list page");
658+ page = raw;
659+ held = p;
660+ }
661+ const value = page[index % LIST_PAGE];
662+ if (value === undefined) throw Error("missing stored list element");
663+ return (value && typeof value === "object" ? Object.freeze(value) : value) as T;
664+ };
665+ function* inOrder(): IterableIterator<T> {
666+ const count = length();
667+ for (let index = 0; index < count; index++) yield at(index);
668+ }
669+ return new Proxy([] as T[], {
670+ get(target, key, receiver) {
671+ if (key === "length") return length();
672+ if (key === Symbol.iterator || key === "values") return inOrder;
673+ if (indexKey(key)) return Number(key) >= length() ? undefined : at(Number(key));
674+ return Reflect.get(target, key, receiver);
675+ },
676+ has(target, key) {
677+ return indexKey(key) ? Number(key) < length() : Reflect.has(target, key);
678+ },
679+ ownKeys() {
680+ return [...Array.from({ length: length() }, (_, index) => String(index)), "length"];
681+ },
682+ getOwnPropertyDescriptor(target, key) {
683+ if (indexKey(key) && Number(key) < length())
684+ return { configurable: true, enumerable: true, writable: false, value: at(Number(key)) };
685+ return Reflect.getOwnPropertyDescriptor(target, key);
686+ },
687+ set() {
688+ throw Error("stored lists are read-only");
689+ },
690+ });
691+}
569693export function readonlyStoredArray<T>(
570694 storage: GraphStateStorage,
571695 name: string,
Lines 701–723 at 318 (were 577–592 at 205)
577701 return count as number;
578702 };
579703 length();
704+ const at = (index: number): T => {
705+ const raw = storage.read(name, String(index));
706+ const value = raw === undefined ? undefined : decode ? decode(raw, index) : (raw as T);
707+ if (value === undefined) throw Error("missing stored list element");
708+ return value && typeof value === "object" ? (Object.freeze(value) as T) : value;
709+ };
710+ // In order, with the length read once (abso Capacity): the array iterator reads it again before every element, a
711+ // second disk read per element of a list too long for the cache.
712+ function* inOrder(): IterableIterator<T> {
713+ const count = length();
714+ for (let index = 0; index < count; index++) yield at(index);
715+ }
580716 return new Proxy([] as T[], {
581717 get(target, key, receiver) {
582718 if (key === "length") return length();
583− if (indexKey(key)) {
584− if (Number(key) >= length()) return undefined;
585− const raw = storage.read(name, key);
586− const value = raw === undefined ? undefined : decode ? decode(raw, Number(key)) : (raw as T);
587− if (value === undefined) throw Error("missing stored list element");
588− return value && typeof value === "object" ? Object.freeze(value) : value;
589− }
719+ if (key === Symbol.iterator || key === "values") return inOrder;
720+ if (indexKey(key)) return Number(key) >= length() ? undefined : at(Number(key));
590721 return Reflect.get(target, key, receiver);
591722 },
592723 has(target, key) {
MCP
compare on this site's /mcp
CLI
abso diff 205 318 src/engine/state-source.ts --json
JSON
/christopher/abso-core/compare/205..318?file=src%2Fengine%2Fstate-source.ts&format=json