- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/server/src/history-state.ts?format=json
180 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 162, 1 version changed it
1import type { GraphStateStorage } from "../../src/engine/state-source.ts";
2import type { SqliteStorage } from "./engine-project.ts";
3import { SqliteWriterState } from "./writer-host.ts";
5/** Derived history for a freshly adopted writer namespace. The host supplies
6 * canonical log positions and commits this state with the canonical transaction. */
7export class SqliteHistoryState implements GraphStateStorage {
8 readonly #storage: SqliteStorage;
9 readonly #namespace: string;
10 readonly #current: SqliteWriterState;
11 #revision: number;
12 #sealedThrough = -1;
13 static initialize(storage: SqliteStorage): void {
14 SqliteWriterState.initialize(storage);
15 storage.sql.exec(
16 "CREATE TABLE IF NOT EXISTS writer_history_keys (namespace TEXT NOT NULL, collection TEXT NOT NULL, key TEXT NOT NULL, revision INTEGER NOT NULL, deleted INTEGER NOT NULL, PRIMARY KEY(namespace, collection, key, revision))",
17 );
18 storage.sql.exec(
19 "CREATE TABLE IF NOT EXISTS writer_history_heads (namespace TEXT PRIMARY KEY, sealed INTEGER NOT NULL)",
20 );
21 }
22 constructor(storage: SqliteStorage, namespace: string, revision = 0) {
23 if (!namespace || namespace.length > 80) throw Error("invalid history namespace");
24 if (!Number.isSafeInteger(revision) || revision < 0) throw Error("invalid history revision");
25 this.#storage = storage;
26 this.#namespace = namespace;
27 this.#current = new SqliteWriterState(storage, namespace);
28 this.#revision = revision;
29 }
30 beginRevision(revision: number): void {
31 if (!Number.isSafeInteger(revision) || revision < this.#revision)
32 throw Error("history revision must not go backwards");
33 if (revision > this.#revision) this.seal();
34 this.#revision = revision;
35 }
36 #sealed(): number {
37 return (
38 (
39 this.#storage.sql
40 .exec("SELECT sealed FROM writer_history_heads WHERE namespace = ?", this.#namespace)
41 .toArray()[0] as { sealed: number } | undefined
42 )?.sealed ?? -1
43 );
44 }
45 seal(): void {
46 this.#storage.sql.exec(
47 "INSERT INTO writer_history_heads VALUES (?, ?) ON CONFLICT(namespace) DO UPDATE SET sealed = MAX(sealed, excluded.sealed)",
48 this.#namespace,
49 this.#revision,
50 );
51 this.#sealedThrough = this.#revision;
52 }
53 #savedNamespace(revision: number): string {
54 return `${this.#namespace}:history:${revision}`;
55 }
56 read(collection: string, key: string): unknown {
57 return this.#current.read(collection, key);
58 }
59 entries(collection: string): Iterable<readonly [string, unknown]> {
60 return this.#current.entries(collection);
61 }
62 write(collection: string, key: string, value: unknown): void {
63 if (this.#revision <= Math.max(this.#sealedThrough, this.#sealed())) throw Error("historical revision is sealed");
64 this.transaction(() => {
65 this.#current.write(collection, key, value);
66 const saved = this.#savedNamespace(this.#revision);
67 this.#storage.sql.exec(
68 "DELETE FROM writer_state WHERE namespace = ? AND collection = ? AND key = ?",
69 saved,
70 collection,
71 key,
72 );
73 this.#storage.sql.exec(
74 "INSERT INTO writer_state SELECT ?, collection, key, part, value FROM writer_state WHERE namespace = ? AND collection = ? AND key = ?",
75 saved,
76 this.#namespace,
77 collection,
78 key,
79 );
80 this.#storage.sql.exec(
81 "INSERT OR REPLACE INTO writer_history_keys VALUES (?, ?, ?, ?, 0)",
82 this.#namespace,
83 collection,
84 key,
85 this.#revision,
86 );
87 });
88 }
89 remove(collection: string, key: string): void {
90 if (this.#revision <= Math.max(this.#sealedThrough, this.#sealed())) throw Error("historical revision is sealed");
91 this.transaction(() => {
92 this.#current.remove(collection, key);
93 new SqliteWriterState(this.#storage, this.#savedNamespace(this.#revision)).remove(collection, key);
94 this.#storage.sql.exec(
95 "INSERT OR REPLACE INTO writer_history_keys VALUES (?, ?, ?, ?, 1)",
96 this.#namespace,
97 collection,
98 key,
99 this.#revision,
100 );
101 });
102 }
103 transaction<T>(body: () => T): T {
104 const revision = this.#revision;
105 const sealed = this.#sealedThrough;
106 try {
107 return this.#storage.transactionSync(body);
108 } catch (error) {
109 this.#revision = revision;
110 this.#sealedThrough = sealed;
111 throw error;
112 }
113 }
114 /** Read-only historical values, with bounded key pages and value decoding. */
115 at(revision: number, assertAuthority: () => void = () => {}): GraphStateStorage {
116 assertAuthority();
117 if (!Number.isSafeInteger(revision) || revision < 0 || revision > this.#revision)
118 throw Error("history position outside authority");
119 if (revision > this.#sealed()) throw Error("historical revision is not sealed");
120 const storage = this.#storage,
121 namespace = this.#namespace;
122 const read = (collection: string, key: string): unknown => {
123 assertAuthority();
124 const row = storage.sql
125 .exec(
126 "SELECT revision, deleted FROM writer_history_keys WHERE namespace = ? AND collection = ? AND key = ? AND revision <= ? ORDER BY revision DESC LIMIT 1",
127 namespace,
128 collection,
129 key,
130 revision,
131 )
132 .toArray()[0] as { revision: number; deleted: number } | undefined;
133 if (!row || row.deleted) return undefined;
134 return new SqliteWriterState(storage, this.#savedNamespace(row.revision)).read(collection, key);
135 };
136 const refuse = (): never => {
137 throw Error("historical state is read-only");
138 };
139 return {
140 read,
141 *entries(collection) {
142 let after: string | undefined;
143 for (;;) {
144 assertAuthority();
145 const rows = storage.sql
146 .exec(
147 `SELECT key FROM writer_history_keys WHERE namespace = ? AND collection = ? AND revision <= ? ${after === undefined ? "" : "AND key > ?"} GROUP BY key ORDER BY key LIMIT 64`,
148 namespace,
149 collection,
150 revision,
151 ...(after === undefined ? [] : [after]),
152 )
153 .toArray() as { key: string }[];
154 if (!rows.length) return;
155 for (const row of rows) {
156 const value = read(collection, row.key);
157 // Stored undefined is a value; deletion is determined by the ledger.
158 const latest = storage.sql
159 .exec(
160 "SELECT deleted FROM writer_history_keys WHERE namespace = ? AND collection = ? AND key = ? AND revision <= ? ORDER BY revision DESC LIMIT 1",
161 namespace,
162 collection,
163 row.key,
164 revision,
165 )
166 .toArray()[0] as { deleted: number };
167 if (!latest.deleted) yield [row.key, value] as const;
168 }
169 after = rows.at(-1)!.key;
170 }
171 },
172 write: refuse,
173 remove: refuse,
174 transaction: (body) => {
175 assertAuthority();
176 return storage.transactionSync(body);
177 },
178 };
179 }
180}