- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/server/test/history-state.test.ts?format=json
254 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 162, 1 version changed it
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { MemoryStore, Project } from "../../worker/project/project.ts";
4import { where } from "../../worker/project/where.ts";
5import { EngineProject } from "../src/engine-project.ts";
6import { SqliteHistoryState } from "../src/history-state.ts";
7import { SqliteWriterState } from "../src/writer-host.ts";
8import { memoryStorage } from "./helpers.ts";
10test("bounded adoption checkpoints reopen every historical Project prefix without replay", (t) => {
11 const storage = memoryStorage();
12 t.after(() => storage.db.close());
13 SqliteHistoryState.initialize(storage);
14 const log = new MemoryStore();
15 const project = new Project(log);
16 project.init({ "a.txt": "one\ntwo\n" }, 0);
17 project.record("alice", { "a.txt": "ONE\ntwo\n" }, 1);
18 project.publish("alice", "First", 2);
19 project.move("alice", "a.txt", "b.txt", 3);
20 project.publish("alice", "Move", 4);
21 const expected = log.entries.map((_, i) => {
22 const prefix = Project.load(log.entries.slice(0, i + 1), new MemoryStore());
23 return { status: prefix.status(), where: where(prefix) };
24 });
25 t.mock.method(Project, "load", () => {
26 throw Error("full-history replay forbidden");
27 });
28 const source = { generation: 1, logLength: log.entries.length, sharedVersion: project.version };
29 let from = 0;
30 while (from < log.entries.length) {
31 const state = new SqliteHistoryState(storage, "project-history", from);
32 const frames = log.entries
33 .slice(from, from + 2)
34 .map((entry, i) => ({ seq: from + i, json: JSON.stringify(entry) }));
35 const page = Project.adoptWriterPage(state, {
36 source,
37 from,
38 frames,
39 maxEntries: 2,
40 maxBytes: 16384,
41 currentSource: () => source,
42 beginRevision: (revision) => state.beginRevision(revision),
43 });
44 from = page.next;
45 }
46 const state = new SqliteHistoryState(storage, "project-history", from);
47 state.seal();
48 let active = true;
49 const calls = new EngineProject(project, undefined, undefined, (at, operation) => {
50 const validate = () => {
51 if (!active) throw Error("historical scope expired");
52 };
53 validate();
54 return operation(
55 at === 0
56 ? new Project(new MemoryStore())
57 : Project.openHistorical(state.at(at, validate), at, new SqliteWriterState(storage, "history-scratch")),
58 );
59 });
60 assert.deepEqual(calls.statusAt(0), { at: 0, version: -1, agents: [] });
61 assert.deepEqual(calls.whereAt(0), { at: 0, version: -1, files: {} });
62 for (let at = 1; at <= from; at++) {
63 const historical = Project.openHistorical(state.at(at), at, new SqliteWriterState(storage, "history-scratch"));
64 assert.deepEqual(historical.status(), expected[at - 1]!.status, `status at ${at}`);
65 assert.deepEqual(where(historical), expected[at - 1]!.where, `where at ${at}`);
66 assert.deepEqual(calls.statusAt(at), { at, ...expected[at - 1]!.status });
67 assert.deepEqual(calls.whereAt(at), { at, ...expected[at - 1]!.where });
68 }
69 assert.deepEqual(calls.statusAt(from + 10), { at: from, ...expected[from - 1]!.status });
70 active = false;
71 assert.throws(() => calls.statusAt(1), /scope expired/);
72 assert.throws(() => calls.whereAt(1), /scope expired/);
73 assert.equal(
74 storage.db.prepare("SELECT COUNT(*) AS n FROM writer_state WHERE namespace = 'history-scratch'").get()!.n,
75 0,
76 );
77});
79test("historical state preserves old values, deletions and undefined across reopen", (t) => {
80 const storage = memoryStorage();
81 t.after(() => storage.db.close());
82 SqliteHistoryState.initialize(storage);
83 let state = new SqliteHistoryState(storage, "history");
84 state.beginRevision(1);
85 state.write("values", "", "empty key");
86 for (let i = 0; i < 150; i++) state.write("values", `key-${i.toString().padStart(3, "0")}`, { value: i });
87 state.write("values", "undefined", undefined);
88 state.beginRevision(2);
89 state.write("values", "key-000", { value: "changed" });
90 state.remove("values", "key-001");
91 state.remove("values", "undefined");
92 state.seal();
93 state = new SqliteHistoryState(storage, "history", 2);
94 assert.deepEqual(state.at(1).read("values", "key-000"), { value: 0 });
95 assert.deepEqual(state.at(2).read("values", "key-000"), { value: "changed" });
96 const old = new Map(state.at(1).entries("values"));
97 const current = new Map(state.at(2).entries("values"));
98 assert.equal(old.size, 152);
99 assert.equal(current.size, 150);
100 assert.equal(old.has("undefined"), true);
101 assert.equal(current.has("undefined"), false);
102 assert.equal(current.has("key-001"), false);
103 assert.equal(current.get(""), "empty key");
104 assert.throws(() => state.at(3), /outside authority/);
105 assert.throws(() => state.at(1).write("values", "bad", 1), /read-only/);
106 assert.throws(() => state.write("values", "bad", 1), /sealed/);
107 const reopened = new SqliteHistoryState(storage, "history", 2);
108 assert.throws(() => reopened.write("values", "key-000", "overwrite"), /sealed/);
109 reopened.beginRevision(3);
110 reopened.write("values", "key-000", "new revision");
111 reopened.seal();
112 assert.deepEqual(reopened.at(2).read("values", "key-000"), { value: "changed" });
113});
115test("new canonical tails extend historical snapshots atomically without replaying old entries", (t) => {
116 const storage = memoryStorage();
117 t.after(() => storage.db.close());
118 SqliteHistoryState.initialize(storage);
119 const log = new MemoryStore();
120 const live = new Project(log);
121 live.init({ "a.txt": "one\ntwo\n" }, 0);
122 const initialLength = log.entries.length;
123 const initial = live.status();
124 const state = new SqliteHistoryState(storage, "tail");
125 const source = { generation: 1, logLength: initialLength, sharedVersion: live.version };
126 Project.adoptWriterPage(state, {
127 source,
128 from: 0,
129 maxEntries: 256,
130 maxBytes: 16384,
131 frames: log.entries.map((entry, seq) => ({ seq, json: JSON.stringify(entry) })),
132 currentSource: () => source,
133 beginRevision: (revision) => state.beginRevision(revision),
134 });
135 state.seal();
136 live.record("alice", { "a.txt": "ONE\ntwo\n" }, 1);
137 live.publish("alice", "First", 2);
138 const expected = log.entries.map((_, i) => {
139 const p = Project.load(log.entries.slice(0, i + 1), new MemoryStore());
140 return { status: p.status(), where: where(p) };
141 });
142 t.mock.method(Project, "load", () => {
143 throw Error("full-history replay forbidden");
144 });
145 const rows = () => ({
146 values: storage.db.prepare("SELECT * FROM writer_state ORDER BY namespace,collection,key,part").all(),
147 history: storage.db.prepare("SELECT * FROM writer_history_keys ORDER BY namespace,collection,key,revision").all(),
148 heads: storage.db.prepare("SELECT * FROM writer_history_heads ORDER BY namespace").all(),
149 });
150 const before = rows();
151 const frames = log.entries
152 .slice(initialLength)
153 .map((entry, i) => ({ seq: initialLength + i, json: JSON.stringify(entry) }));
154 let validations = 0;
155 assert.throws(
156 () =>
157 Project.extendHistoricalPage(state, {
158 from: initialLength,
159 frames,
160 maxEntries: 256,
161 maxBytes: 16384,
162 beginRevision: (revision) => state.beginRevision(revision),
163 validateAuthority: () => {
164 if (++validations === 2) throw Error("canonical tail changed");
165 },
166 }),
167 /canonical tail changed/,
168 );
169 assert.deepEqual(rows(), before);
170 let from = initialLength;
171 while (from < log.entries.length) {
172 const reopened = new SqliteHistoryState(storage, "tail", from);
173 const page = Project.extendHistoricalPage(reopened, {
174 from,
175 frames: frames.filter((frame) => frame.seq >= from).slice(0, 2),
176 maxEntries: 2,
177 maxBytes: 16384,
178 beginRevision: (revision) => reopened.beginRevision(revision),
179 validateAuthority: () => {},
180 });
181 reopened.seal();
182 from = page.next;
183 }
184 const reopened = new SqliteHistoryState(storage, "tail", from);
185 const scratch = new SqliteWriterState(storage, "tail-scratch");
186 assert.deepEqual(Project.openHistorical(reopened.at(initialLength), initialLength, scratch).status(), initial);
187 for (let at = initialLength + 1; at <= from; at++) {
188 const historical = Project.openHistorical(reopened.at(at), at, scratch);
189 assert.deepEqual(historical.status(), expected[at - 1]!.status);
190 assert.deepEqual(where(historical), expected[at - 1]!.where);
191 }
192});
194test("current state and its history roll back together", (t) => {
195 const storage = memoryStorage();
196 t.after(() => storage.db.close());
197 SqliteHistoryState.initialize(storage);
198 const state = new SqliteHistoryState(storage, "rollback");
199 state.beginRevision(1);
200 state.write("values", "a", { before: true });
201 state.seal();
202 const rows = () => ({
203 values: storage.db.prepare("SELECT * FROM writer_state ORDER BY namespace,collection,key,part").all(),
204 history: storage.db.prepare("SELECT * FROM writer_history_keys ORDER BY namespace,collection,key,revision").all(),
205 heads: storage.db.prepare("SELECT * FROM writer_history_heads ORDER BY namespace").all(),
206 });
207 const before = rows();
208 assert.throws(
209 () =>
210 state.transaction(() => {
211 state.beginRevision(2);
212 state.write("values", "a", { after: true });
213 state.remove("values", "a");
214 state.seal();
215 throw Error("injected failure");
216 }),
217 /injected failure/,
218 );
219 assert.deepEqual(rows(), before);
220 assert.throws(() => state.at(2), /outside authority/);
221 assert.deepEqual(state.read("values", "a"), { before: true });
222});
224test("historical readers recheck canonical authority after creation and between yielded keys", (t) => {
225 const storage = memoryStorage();
226 t.after(() => storage.db.close());
227 SqliteHistoryState.initialize(storage);
228 const state = new SqliteHistoryState(storage, "authority");
229 state.beginRevision(1);
230 state.write("values", "a", 1);
231 state.write("values", "b", 2);
232 state.seal();
233 let valid = true;
234 const guard = () => {
235 if (!valid) throw Error("canonical authority expired");
236 };
237 const historical = state.at(1, guard);
238 const iterator = historical.entries("values")[Symbol.iterator]();
239 assert.deepEqual(iterator.next().value, ["a", 1]);
240 valid = false;
241 assert.throws(() => iterator.next(), /authority expired/);
242 assert.throws(() => historical.read("values", "a"), /authority expired/);
243 assert.throws(() => historical.entries("values")[Symbol.iterator]().next(), /authority expired/);
244 let called = false;
245 assert.throws(
246 () =>
247 historical.transaction(() => {
248 called = true;
249 }),
250 /authority expired/,
251 );
252 assert.equal(called, false);
253 assert.throws(() => state.at(1, guard), /authority expired/);
254});