- MCP
- show on this site's /mcp
- CLI
- abso show 109 --json
- JSON
- /christopher/abso-core/versions/109?format=json
Add opt-in persistent writer state, checked reopen and bounded adoption
of shared·codex-capacity-chair··Not checked
Version 108 · Files at version 109
16 files, 1455 lines added, 58 removed.
--- a/src/engine/graph.ts
+++ b/src/engine/graph.ts
@@ -15,6 +15,7 @@
18+import { type GraphStateStorage, type StateMap, storedArray, storedMap } from "./state-source.ts";
@@ -76,21 +77,25 @@
79−function pushOne<K, V>(map: Map<K, V | V[]>, key: K, value: V): void {
82− else if (Array.isArray(had)) had.push(value);
83− else map.set(key, [had, value]);
8588
8992
90−function pushIndex<K, V>(map: Map<K, V[]>, key: K, value: V): void {
92− if (list) list.push(value);
93− else map.set(key, [value]);
95100
@@ -176,7 +181,14 @@
177182
178183/** A growable array of 32-bit integers: a line's fields cost 4 bytes each, not an object and its strings (#46). */
179−class Ints {
190+
@@ -232,6 +244,7 @@
233245
237250 // Lines, packed (#46): line n is the k-th line atom seq wrote, n = #lineBase[seq] + k. Each holds its
@@ -239,12 +252,14 @@
239252 // path's index for a file's start). Its text stays in the atom. Ids and LineInfo objects are made only
240253 // when asked for, so a line costs about 16 bytes here instead of a record, its id and its parent's id.
242− #lineAtom = new Ints();
243− #lineFile = new Ints();
244− #lineParent = new Ints();
245258 /** Lines' text, when the graph keeps it; without it, it comes from `texts` (#46, text on disk). */
247− #lineTotal = 0;
248263 /** Where an atom's lines' text is when the graph doesn't keep it: each run's lines, by the atom's seq. */
@@ -257,13 +272,69 @@
260− options: { texts?: (seq: number) => readonly (readonly string[])[]; lists?: (seq: number) => AtomLists } = {},
268339
269340 /** Where an atom's key and delete lists are when the graph doesn't keep them (#46): the stored atom. */
@@ -352,30 +423,30 @@
355− #pathIndex = new Map<string, number>();
356427 /** The first atom that deletes each line (its seq), or -1; a line deleted by more than one has the rest in #moreDeleters. */
357− #deleter = new Ints();
358− #moreDeleters = new Map<number, number[]>();
359− #writers = new Map<Key | number, AtomId | AtomId[]>();
360− #readers = new Map<Key | number, AtomId | AtomId[]>();
361− #holders = new Map<Key | number, AtomId | AtomId[]>();
362− #dependents = new Map<AtomId, AtomId | AtomId[]>();
363− #resolvers = new Map<AtomId, AtomId | AtomId[]>();
364− #together = new Map<AtomId, TogetherLink[]>();
365− #fileAtoms = new Map<string, AtomId[]>();
366437 /** First lines of runs (their n), by the node they were written after: a line's n, or -1 - a path's index. */
367− #runStarts = new Map<number, number | number[]>();
368439 /** 1 for the first line of a run whose atom deleted the line it hangs off: the run replaces it. */
369− #replacing = new Ints(true);
370− #relocations = new Map<LineId, RelocationInfo[]>();
371− #relocatedTo = new Map<NodeId, RelocationInfo[]>();
372443 /** The same relocations by node code (a line's n, or -1 - a path's index), for views laid out by number. */
373− #relocatedToCode = new Map<number, RelocationInfo[]>();
377− #atomDeletes: (Int32Array | undefined)[] = [];
378− #labels = new Map<string, Label>();
381452
@@ -776,6 +847,11 @@
853+
@@ -850,6 +926,7 @@
@@ -857,7 +934,7 @@
860− this.#atoms.push(frozen);
@@ -866,7 +943,7 @@
866943 // The atom as given: with lists kept elsewhere, the frozen one reads them back from the log.
869− this.#atomDeletes.push(deletedN.length > 0 ? Int32Array.from(deletedN) : undefined);
946+ this.#atomDeletes.push(deletedN.length > 0 ? (this.#storage ? deletedN : Int32Array.from(deletedN)) : undefined);
@@ -874,7 +951,7 @@
877− const n = this.#lineTotal++;
@@ -974,6 +1051,10 @@
1054+ return this.#storage ? this.#storage.transaction(() => this.#removeLabel(name)) : this.#removeLabel(name);
1056+
--- /dev/null
+++ b/src/engine/state-source.ts
@@ -0,0 +1,274 @@
15+
43+
64+
82+
85+
87+export function storedArray<T>(storage: GraphStateStorage, name: string, decode?: (value: T) => T): T[] {
125+
133+
135+export function storedMap<K extends string | number, V>(storage: GraphStateStorage, name: string): StateMap<K, V> {
155+
181+ return this.#storage.read(this.#name, this.#key(key)) as { slot: number; value: unknown } | undefined;
231+ override forEach(callback: (value: V, key: K, map: Map<K, V>) => void, thisArg?: unknown): void {
235+
--- /dev/null
+++ b/src/engine/test/state-source.test.ts
@@ -0,0 +1,96 @@
6+
46+
50+ const right = oracle.draft({ edits: [{ file: "a.txt", old: "one", new: "uno" }], author: "right" }, ["a0"]);
55+ assert.deepEqual(checkDraft(new Graph({ storage }), oracle.shared, right), oracle.check(right));
59+ assert.deepEqual(new Graph({ storage }).deletedLines(right.id), oracle.graph.deletedLines(right.id));
62+
79+
--- /dev/null
+++ b/src/engine/test/storage-helper.ts
@@ -0,0 +1,43 @@
3+
11+ this.db.exec("CREATE TABLE state (collection TEXT, key TEXT, value TEXT, PRIMARY KEY(collection,key))");
15+ const row = this.db.prepare("SELECT value FROM state WHERE collection = ? AND key = ?").get(collection, key);
20+ this.db.prepare("INSERT OR REPLACE INTO state VALUES (?, ?, ?)").run(collection, key, JSON.stringify({ value }));
--- a/src/engine/test/view.test.ts
+++ b/src/engine/test/view.test.ts
@@ -3,9 +3,38 @@
79
27+ for (let i = 1; i <= 40; i++) repo.edit({ edits: [{ file: "a.txt", old: `${i - 1}\n`, new: `${i}\n` }] });
30+ for (let i = 41; i <= 80; i++) repo.edit({ edits: [{ file: "a.txt", old: `${i - 1}\n`, new: `${i}\n` }] });
32+ assert.equal(second.weight(), weight, "membership resident size does not increase with historical atom count");
--- a/src/engine/view.ts
+++ b/src/engine/view.ts
@@ -11,6 +11,7 @@
1111
1617
@@ -90,7 +91,8 @@
9293 /** The set, made from #in the first time it's asked for: a view keeps a byte per atom, not a copy of the set (#46). */
93− #set: Set<AtomId> | undefined;
@@ -109,14 +111,34 @@
110112
113− const atoms: { readonly id: AtomId; readonly deps: readonly AtomId[] }[] = [];
117− atoms.push(atom);
119− for (const atom of atoms) {
@@ -141,6 +163,7 @@
141163
146169
@@ -272,7 +295,8 @@
275− if (relocs.get(n)?.r !== r) continue;
@@ -336,8 +360,12 @@
339− for (let seq = 0; seq < this.#in.length; seq++)
340− if (this.#in[seq] === 1) for (const file of this.graph.filesOf(this.graph.idOfSeq(seq))) known.add(file);
367+ if (this.#in[seq] === 1) for (const file of this.graph.filesOf(this.graph.idOfSeq(seq))) known.add(file);
@@ -470,7 +498,8 @@
473− if (this.#set) n += OBJECT_BYTES + SET_ENTRY_BYTES * this.#set.size;
476505 if (this.#relocsN) n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + OBJECT_BYTES) * this.#relocsN.size;
--- a/worker/project/apply-entry.ts
+++ b/worker/project/apply-entry.ts
@@ -141,12 +141,21 @@
144− if (entry.change !== undefined && host.changes.has(entry.change)) host.changes.get(entry.change)!.last = at;
149− if (note) note.closed = { agent: entry.agent, how: entry.how, time: entry.time };
@@ -249,6 +258,7 @@
--- a/worker/project/change-records.ts
+++ b/worker/project/change-records.ts
@@ -73,6 +73,7 @@
7879
@@ -101,6 +102,7 @@
102103 if (entry.type === "publish") for (const s of entry.settled ?? []) rec.settled.push({ ...s, version: entry.version });
--- a/worker/project/notes.ts
+++ b/worker/project/notes.ts
@@ -321,5 +321,6 @@
324− return host.noteAnswer(note);
--- a/worker/project/project-types.ts
+++ b/worker/project/project-types.ts
@@ -1,4 +1,5 @@
@@ -565,6 +566,14 @@
565566
568577 /** Whether the bytes for a SHA-256 have been uploaded. Without it, no blob is held, so binary files are refused. */
570579 /** The time now, for start notes that end after 2 hours idle (#44). Without it, the newest entry's time. */
--- a/worker/project/project.ts
+++ b/worker/project/project.ts
@@ -13,6 +13,14 @@
1414
1515import { type Atom, type AtomId, EngineError, Graph, type LineId, type View } from "../../src/engine/index.ts";
1826import { type AnnounceHost, announce as hostedAnnounce, announcePerson as hostedAnnouncePerson } from "./announce.ts";
@@ -191,6 +199,7 @@
194203
@@ -204,9 +213,21 @@
204213
207227
@@ -248,7 +269,16 @@
251− #lastTime = 0;
254284 /** Each version's change, by "<draft or ''> <version>" (#44): what a note on its lines belongs to. */
@@ -260,12 +290,39 @@
263− #lastNote = 0;
264− #queuedId = 0;
265313 /** Queued ids in the batch being checked now: not in the log, only for refusing an unqueue mid-check. */
267315 /** How many entries the store holds: the log position of the next one (#41, the website's feed and replay). */
268− #stored = 0;
269326 /** Names on the lines a change replaced, by "<atom> <last replaced line>" (atoms never change). */
@@ -273,13 +330,53 @@
276− #announced = 0;
278344
280346
281− constructor(store: ProjectStore, options: ProjectOptions = {}) {
347+ constructor(store: ProjectStore, options: ProjectOptions = {}, checkpoint?: typeof WRITER_REOPEN) {
359+ throw new Error("writer indexes already contain history; Project checkpoint reopen is required");
377+ this.#tells = new Map(tells.map(([id, tell]) => [id, { ...tell, heard: new Map(tell.heard) }]));
@@ -301,9 +398,11 @@
303400 // Line text and each atom's deletes and claim keys stay in the log (#46), read back when asked for.
304− this.#graph = entryAt
305− ? new Graph({ texts: (seq) => stored(seq).inserts.map((r) => r.lines), lists: (seq) => stored(seq) })
306− : new Graph();
404+ ? new Graph({ texts: (seq) => stored(seq).inserts.map((r) => r.lines), lists: (seq) => stored(seq) })
@@ -330,12 +429,135 @@
337438
439+ /** Reopen committed writer indexes. The host supplies the authoritative canonical log length. */
450+
460+ const length = (name: string) => (storage.read(`${name}:meta`, "length") as number | undefined) ?? 0;
477+
496+ a.generation === b.generation && a.logLength === b.logLength && a.sharedVersion === b.sharedVersion;
498+ ![input.source.generation, input.source.logLength, input.source.sharedVersion, input.from].every(
515+ (previous && (!same(previous.source, input.source) || previous.ready || previous.next !== input.from)) ||
555+ storage.write("project:meta", "adoption", { source: input.source, next, ready } satisfies WriterAdoption);
559+
@@ -1267,11 +1489,34 @@
1270− this.#pending = [];
12741498
1507+
1519+
@@ -2217,7 +2462,12 @@
22182463
2220− return this.#viewKeyed(this.#agentViewKey(agent), () => this.#agentSet(agent));
22222472
22232473 /** An agent's view, keyed by what the set is made of, not by sorting every atom in it (#46): that cost more than the work. */
--- a/worker/project/spaces.ts
+++ b/worker/project/spaces.ts
@@ -4,6 +4,14 @@
55
816
@@ -27,6 +35,8 @@
2735
@@ -39,7 +49,19 @@
4151
42−export function newSpace(name: string): Space {
4365 return { name, versions: [], own: new Set(), publishedIn: new Map(), droppedIn: new Map(), atCache: new Map() };
4567
@@ -53,8 +75,95 @@
5780
87+
104+
105+function indexMembership(storage: GraphStateStorage, prefix: string, version: Published): void {
120+
162+ forEach(callback: (value: AtomId, key: AtomId, set: ReadonlySet<AtomId>) => void, thisArg?: unknown): void {
166+
@@ -65,7 +174,26 @@
65174
68− readonly spaces = new Map<string, Space>([[SHARED, newSpace(SHARED)]]);
195+ if (!this.spaces.has(SHARED)) storage.transaction(() => this.spaces.set(SHARED, newSpace(SHARED, storage)));
69197
@@ -73,6 +201,11 @@
73201
207+
@@ -108,13 +241,15 @@
111− const p = this.spaces.get(entry.space ?? SHARED)!.versions[entry.version]!;
117− const space = newSpace(entry.name);
@@ -170,6 +305,18 @@
309+ const own = new StoredVersionSet(this.#storage, `membership:${JSON.stringify(space.name)}`, version);
--- /dev/null
+++ b/worker/project/test/project-writer-storage.test.ts
@@ -0,0 +1,142 @@
7+
32+
33+test("actual Project record and publish use persistent graph, membership and positional indexes", (t) => {
51+ assert.deepEqual(project.files(), oracle.files(), "reopened checkpoint preserves published files");
61+ run((p) => p.note("writer", { file: "a.txt", from: 1, to: 1, text: "Review this", kind: "objection" }, 2));
76+ assert.throws(() => new Project(store, { writerStorage: storage }), /checkpoint reopen is required/);
80+
85+ storage.transaction(() => project.init({ "avcs.json": '{"queue":true}\n', "a.txt": "one\n" }, 0));
99+
104+ const snapshot = () => storage.db.prepare("SELECT * FROM state ORDER BY collection, key").all();
122+
127+ const snapshot = () => storage.db.prepare("SELECT * FROM state ORDER BY collection, key").all();
134+ assert.deepEqual(snapshot(), before, "log, graph, membership and positional state roll back together");
140+ assert.deepEqual(Project.openWriter(store, storage, entries.length).files(), { "a.txt": "ONE\n" });
--- /dev/null
+++ b/worker/project/test/stored-membership.test.ts
@@ -0,0 +1,129 @@
7+
8+test("persistent collections preserve insertion order, deletion, reinsert and undefined values", () => {
30+
31+test("membership reopens stored versions and preserves historical drop and amendment semantics", () => {
47+ assert.equal(storage.reads - reads, 1, "reopen only checks the space directory; it does not read version history");
54+ assert.deepEqual([...reopened.at(reopened.shared, version)], [...oracle.at(oracle.shared, version)]);
56+ const amend = { type: "amend" as const, agent: "writer", version: 1, message: "Corrected", time: 4 };
63+
81+
107+
123+ assert.equal(storage.reads - reads, 0, "opening a historical membership does not read historical state");
127+ assert.equal(shared.atCache.size, 0, "storage-backed shared history never fills the in-memory set cache");
--- /dev/null
+++ b/worker/project/test/writer-adoption.test.ts
@@ -0,0 +1,101 @@
5+
14+ const source: WriterSource = { generation: 8, logLength: log.entries.length, sharedVersion: project.version };
18+
19+test("nonempty canonical history adopts in bounded restartable pages without Project.load", (t) => {
38+ if (!result.ready) assert.throws(() => Project.openWriter(log, storage, next), /adoption is incomplete/);
40+ assert.equal(JSON.stringify(log.entries), before, "adoption never rewrites or appends canonical entries");
47+ assert.equal(Project.openWriter(log, storage, log.entries.length).files()["a.txt"], "after adoption\n");
50+
51+test("adoption rejects stale source and rolls back a page if the source changes before commit", () => {
54+ const snapshot = () => storage.db.prepare("SELECT * FROM state ORDER BY collection, key").all();
61+ currentSource: () => (++calls === 1 ? source : { ...source, generation: source.generation + 1 }),
85+
95+ () => Project.adoptWriterPage(storage, { source, from: 0, currentSource: () => source, ...page }),