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
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/server/test/history-host.test.ts?format=json
180 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 { SqliteLog } from "../src/engine-project.ts";
5import { SqliteHistoryHost } from "../src/history-host.ts";
6import { advanceWriterRoot, SqliteWriterHost, SqliteWriterState } from "../src/writer-host.ts";
7import { memoryStorage } from "./helpers.ts";
8
9test("historical host binds paged snapshots to the verified canonical root", (t) => {
10 const storage = memoryStorage();
11 t.after(() => storage.db.close());
12 const memory = new MemoryStore();
13 const project = new Project(memory);
14 project.init({ "a.txt": "one\n" }, 0);
15 for (let i = 1; i <= 18; i++) {
16 project.record("alice", { "a.txt": `${i}\n` }, i * 2);
17 project.publish("alice", `Change ${i}`, i * 2 + 1);
18 }
19 const expected = memory.entries.map((_, index) =>
20 Project.load(memory.entries.slice(0, index + 1), new MemoryStore()).status(),
21 );
22 const log = new SqliteLog(storage);
23 log.append(memory.entries);
24 SqliteWriterState.initialize(storage);
25 const writer = new SqliteWriterHost(storage, "writer");
26 let from = 0;
27 for (;;) {
28 const page = writer.adoptPage(from, { maxEntries: 32, maxBytes: 1_048_576 }, () => {});
29 from = page.next;
30 if (page.ready) break;
31 }
32 const root = writer.root();
33 t.mock.method(Project, "load", () => {
34 throw Error("full-history replay forbidden");
35 });
36 const history = () => new SqliteHistoryHost(storage, "history");
37 let pages = 0;
38 while (
39 !history().prepare(
40 log,
41 root,
42 () => writer.root(),
43 () => {},
44 ).ready
45 ) {
46 pages++;
47 assert.throws(
48 () =>
49 history().read(
50 root,
51 () => writer.root(),
52 () => {},
53 1,
54 (p) => p.status(),
55 ),
56 /not ready/,
57 );
58 }
59 assert.ok(pages > 0);
60 let authorized = true;
61 assert.throws(
62 () =>
63 history().read(
64 root,
65 () => root,
66 () => {
67 if (!authorized) throw Error("authority expired during read");
68 },
69 1,
70 (p) => {
71 const result = p.status();
72 authorized = false;
73 return result;
74 },
75 ),
76 /authority expired during read/,
77 );
78 let observedRoot = root;
79 assert.throws(
80 () =>
81 history().read(
82 root,
83 () => observedRoot,
84 () => {},
85 1,
86 (p) => {
87 const result = p.status();
88 observedRoot = { ...root, generation: root.generation + 1 };
89 return result;
90 },
91 ),
92 /root changed/,
93 );
94 for (let at = 1; at <= root.logLength; at++) {
95 assert.deepEqual(
96 history().read(
97 root,
98 () => writer.root(),
99 () => {},
100 at,
101 (p) => p.status(),
102 ),
103 expected[at - 1],
104 );
105 }
106 assert.throws(
107 () =>
108 history().read(
109 root,
110 () => ({ ...root, generation: root.generation + 1 }),
111 () => {},
112 1,
113 (p) => p.status(),
114 ),
115 /root changed/,
116 );
117 assert.throws(
118 () =>
119 history().read(
120 root,
121 () => root,
122 () => {
123 throw Error("revoked");
124 },
125 1,
126 (p) => p.status(),
127 ),
128 /revoked/,
129 );
130 const rows = () => ({
131 values: storage.db.prepare("SELECT * FROM writer_state ORDER BY namespace,collection,key,part").all(),
132 history: storage.db.prepare("SELECT * FROM writer_history_keys ORDER BY namespace,collection,key,revision").all(),
133 heads: storage.db.prepare("SELECT * FROM writer_history_heads ORDER BY namespace").all(),
134 });
135 const before = rows();
136 project.record("alice", { "a.txt": "tail\n" }, 100);
137 project.publish("alice", "Tail", 101);
138 log.append(memory.entries.slice(root.logLength));
139 const committed = advanceWriterRoot(log, root, writer.source());
140 const wrong = { ...committed, digest: "0".repeat(64) };
141 assert.throws(
142 () =>
143 history().advance(
144 log,
145 root,
146 wrong,
147 () => wrong,
148 () => {},
149 ),
150 /tail digest mismatch/,
151 );
152 assert.deepEqual(rows(), before);
153 history().advance(
154 log,
155 root,
156 committed,
157 () => committed,
158 () => {},
159 );
160 assert.deepEqual(
161 history().read(
162 committed,
163 () => committed,
164 () => {},
165 committed.logLength,
166 (p) => p.status(),
167 ),
168 project.status(),
169 );
170 assert.deepEqual(
171 history().read(
172 committed,
173 () => committed,
174 () => {},
175 root.logLength,
176 (p) => p.status(),
177 ),
178 expected.at(-1),
179 );
180});