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
438 linescodex-capacity-policy 100%Last change codex-capacity-policy in version 105, 1 version changed it
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import {
4 LifecycleAuthorityModel,
5 type LifecycleFence,
6 type LifecyclePermit,
7 LocalInstallStoreModel,
8 type VerifiedReplayResult,
9} from "../lifecycle-permit-contract.ts";
11function setup() {
12 const authority = new LifecycleAuthorityModel("authority-test");
13 const incarnation = authority.createProject("tenant", "project", ["alice", "bob"]);
14 const fence: LifecycleFence = {
15 tenant: "tenant",
16 project: "project",
17 incarnation,
18 actor: "alice",
19 operationId: "operation-1",
20 requestDigest: "request-digest",
21 inputDigest: "canonical-input-digest",
22 rootDigest: "root-0",
23 generation: 0,
24 logHead: 10,
25 sharedHead: 4,
26 actorHead: 6,
27 queueSequence: 3,
28 authorizationRevision: 1,
29 policyRevision: 1,
30 build: "engine-build-1",
31 };
32 const beforeGrant = authority.snapshot();
33 const permit = authority.grant(fence);
34 const result: VerifiedReplayResult = {
35 permitId: permit.id,
36 operationId: fence.operationId,
37 requestDigest: fence.requestDigest,
38 inputDigest: fence.inputDigest,
39 actor: fence.actor,
40 incarnation,
41 authorizationRevision: fence.authorizationRevision,
42 policyRevision: fence.policyRevision,
43 build: fence.build,
44 outputDigest: "output-digest",
45 nextRootDigest: "root-1",
46 nextGeneration: 1,
47 nextLogHead: 12,
48 nextSharedHead: 5,
49 nextActorHead: 7,
50 nextQueueSequence: 4,
51 };
52 const local = new LocalInstallStoreModel();
53 local.seed("tenant", "project", incarnation, "root-0", 0, 10, 4, 6, 3);
54 return { authority, beforeGrant, fence, incarnation, permit, result, local };
57function install(permit: LifecyclePermit, result: VerifiedReplayResult, local: LocalInstallStoreModel) {
58 return local.install(permit, result);
61test("every new safety mutation advances the modeled rollback floor", () => {
62 const { authority, beforeGrant, fence, permit, result, local } = setup();
63 const afterGrant = authority.snapshot();
64 assert.equal(afterGrant.revision, beforeGrant.revision + 1);
65 assert.equal(permit.authorityRevision, afterGrant.revision);
66 assert.deepEqual(authority.grant(fence), permit);
67 assert.equal(authority.snapshot().revision, afterGrant.revision);
69 const beforeReplay = authority.snapshot();
70 authority.registerReplayResult(permit, result);
71 const afterReplay = authority.snapshot().revision;
72 assert.equal(afterReplay, beforeReplay.revision + 1);
73 authority.registerReplayResult(permit, result);
74 assert.equal(authority.snapshot().revision, afterReplay);
76 const beforeTransition = authority.snapshot();
77 const transition = authority.requestTransition("tenant", "project", "delete-floor", "delete");
78 const afterTransition = authority.snapshot();
79 assert.equal(transition.status, "draining");
80 assert.equal(afterTransition.revision, beforeTransition.revision + 1);
81 assert.equal(afterTransition.projects["tenant\u0000project"]?.authorityRevision, afterTransition.revision);
82 assert.deepEqual(authority.requestTransition("tenant", "project", "delete-floor", "delete"), transition);
83 assert.equal(authority.snapshot().revision, afterTransition.revision);
85 const receipt = install(permit, result, local);
86 const beforeInstallResolution = authority.snapshot().revision;
87 authority.resolveInstall(receipt);
88 const afterInstallResolution = authority.snapshot().revision;
89 assert.equal(afterInstallResolution, beforeInstallResolution + 2);
90 assert.equal(authority.snapshot().projects["tenant\u0000project"]?.authorityRevision, afterInstallResolution);
91 authority.resolveInstall(receipt);
92 assert.equal(authority.snapshot().revision, afterInstallResolution);
93});
95test("rollback floors reject snapshots taken before grant, replay registration, or drain", () => {
96 const { authority, beforeGrant, fence, permit, result, local } = setup();
97 const grantRevision = authority.snapshot().revision;
98 const afterGrantRestart = LifecycleAuthorityModel.restart(beforeGrant, grantRevision);
99 assert.equal(afterGrantRestart.snapshot().blocked, true);
100 assert.equal(afterGrantRestart.snapshot().projects["tenant\u0000project"]?.permit, null);
101 const receipt = install(permit, result, local);
102 const blockedBeforeGrantActions = afterGrantRestart.snapshot();
103 assert.throws(() => afterGrantRestart.grant({ ...fence, operationId: "after-rollback" }), /rollback blocks/);
104 assert.throws(
105 () => afterGrantRestart.requestTransition("tenant", "project", "after-rollback", "delete"),
106 /rollback blocks/,
107 );
108 assert.throws(() => afterGrantRestart.resolveInstall(receipt), /rollback blocks/);
109 assert.deepEqual(afterGrantRestart.snapshot(), blockedBeforeGrantActions);
111 const beforeReplay = authority.snapshot();
112 authority.registerReplayResult(permit, result);
113 const afterReplayRevision = authority.snapshot().revision;
114 assert.equal(afterReplayRevision, beforeReplay.revision + 1);
115 const afterReplayRollback = LifecycleAuthorityModel.restart(beforeReplay, afterReplayRevision);
116 assert.equal(afterReplayRollback.snapshot().blocked, true);
117 const blockedBeforeReplayRegistration = afterReplayRollback.snapshot();
118 assert.throws(() => afterReplayRollback.registerReplayResult(permit, result), /rollback blocks/);
119 assert.deepEqual(afterReplayRollback.snapshot(), blockedBeforeReplayRegistration);
121 const beforeDrain = authority.snapshot();
122 authority.requestTransition("tenant", "project", "delete-floor", "delete");
123 const drainRevision = authority.snapshot().revision;
124 assert.equal(drainRevision, beforeDrain.revision + 1);
125 const afterDrainRollback = LifecycleAuthorityModel.restart(beforeDrain, drainRevision);
126 assert.equal(afterDrainRollback.snapshot().blocked, true);
127 assert.equal(afterDrainRollback.snapshot().projects["tenant\u0000project"]?.permit?.id, permit.id);
128 const blockedBeforeDrainActions = afterDrainRollback.snapshot();
129 assert.throws(
130 () => afterDrainRollback.grant({ ...permit.fence, operationId: "during-stale-drain" }),
131 /rollback blocks/,
132 );
133 assert.throws(
134 () => afterDrainRollback.requestTransition("tenant", "project", "second-transition", "migration"),
135 /rollback blocks/,
136 );
137 assert.throws(() => afterDrainRollback.resolveInstall(receipt), /rollback blocks/);
138 assert.deepEqual(afterDrainRollback.snapshot(), blockedBeforeDrainActions);
139});
141test("grant-first serializes deletion behind an exact installation receipt", () => {
142 const { authority, permit, result, local } = setup();
143 assert.equal(authority.requestTransition("tenant", "project", "delete-1", "delete").status, "draining");
144 assert.throws(() => authority.grant({ ...permit.fence, operationId: "operation-2" }), /transition blocks/);
145 assert.equal(authority.snapshot().projects["tenant\u0000project"]?.phase, "draining");
147 authority.registerReplayResult(permit, result);
148 const receipt = install(permit, result, local);
149 authority.resolveInstall(receipt);
150 assert.equal(authority.snapshot().projects["tenant\u0000project"]?.phase, "deleted");
151 assert.equal(authority.snapshot().tombstones["tenant\u0000project"], 2);
152 assert.throws(() => authority.grant({ ...permit.fence, operationId: "operation-2" }), /transition blocks/);
153});
155test("transition-first blocks permits and restore rotates the retained incarnation", () => {
156 const authority = new LifecycleAuthorityModel();
157 const firstIncarnation = authority.createProject("tenant", "project", ["alice"]);
158 assert.equal(authority.requestTransition("tenant", "project", "delete-1", "delete").status, "complete");
159 const beforeRestore = authority.snapshot();
160 assert.throws(() => authority.grant({ ...setup().fence, incarnation: firstIncarnation }), /transition blocks/);
161 assert.equal(authority.requestTransition("tenant", "project", "restore-1", "restore").status, "complete");
162 const restored = authority.snapshot();
163 assert.equal(restored.projects["tenant\u0000project"]?.incarnation, 3);
164 assert.equal(restored.tombstones["tenant\u0000project"], 3);
165 assert.equal(beforeRestore.tombstones["tenant\u0000project"], 2);
166 assert.throws(() => authority.grant({ ...setup().fence, incarnation: firstIncarnation }), /stale lifecycle fence/);
167});
169test("restart preserves an active hold and a lost install acknowledgement resolves by exact receipt", () => {
170 const { authority, permit, result, local } = setup();
171 const transition = authority.requestTransition("tenant", "project", "migration-1", "migration");
172 assert.equal(transition.status, "draining");
173 authority.registerReplayResult(permit, result);
175 const restartedAuthority = LifecycleAuthorityModel.restart(authority.snapshot(), authority.snapshot().revision);
176 const restartedLocal = LocalInstallStoreModel.restart(local.snapshot());
177 assert.throws(() => restartedAuthority.grant({ ...permit.fence, operationId: "operation-2" }), /transition blocks/);
178 assert.equal(restartedAuthority.snapshot().projects["tenant\u0000project"]?.permit?.id, permit.id);
179 assert.equal(restartedLocal.readReceipt("tenant", "project", permit.fence.operationId), null);
181 const receipt = install(permit, result, restartedLocal);
182 const afterLocalCommit = LocalInstallStoreModel.restart(restartedLocal.snapshot());
183 const afterAuthorityCommit = LifecycleAuthorityModel.restart(
184 restartedAuthority.snapshot(),
185 restartedAuthority.snapshot().revision,
186 );
187 const recoveredReceipt = afterLocalCommit.readReceipt("tenant", "project", permit.fence.operationId);
188 assert.deepEqual(recoveredReceipt, receipt);
189 afterAuthorityCommit.resolveInstall(recoveredReceipt!);
190 assert.equal(afterAuthorityCommit.snapshot().projects["tenant\u0000project"]?.phase, "active");
191 assert.equal(afterAuthorityCommit.snapshot().projects["tenant\u0000project"]?.incarnation, 2);
192 assert.deepEqual(
193 afterAuthorityCommit.snapshot().projects["tenant\u0000project"]?.installReceipts["operation-1"],
194 receipt,
195 );
196});
198test("no install outcome leaves the hold intact across restart; there is no timeout release", () => {
199 const { authority, permit } = setup();
200 authority.requestTransition("tenant", "project", "delete-1", "delete");
201 const restarted = LifecycleAuthorityModel.restart(authority.snapshot(), authority.snapshot().revision);
202 assert.equal(restarted.snapshot().projects["tenant\u0000project"]?.permit?.id, permit.id);
203 assert.equal(restarted.snapshot().projects["tenant\u0000project"]?.transition?.status, "draining");
204 assert.throws(() => restarted.grant({ ...permit.fence, operationId: "retry-with-new-id" }), /transition blocks/);
205 assert.throws(
206 () =>
207 restarted.resolveInstall({
208 permitId: permit.id,
209 operationId: permit.fence.operationId,
210 tenant: permit.fence.tenant,
211 project: permit.fence.project,
212 incarnation: permit.fence.incarnation,
213 requestDigest: permit.fence.requestDigest,
214 outputDigest: "unknown",
215 previousRootDigest: permit.fence.rootDigest,
216 nextRootDigest: "unknown",
217 previousGeneration: permit.fence.generation,
218 nextGeneration: 1,
219 previousLogHead: permit.fence.logHead,
220 nextLogHead: permit.fence.logHead,
221 previousSharedHead: permit.fence.sharedHead,
222 nextSharedHead: permit.fence.sharedHead,
223 previousActorHead: permit.fence.actorHead,
224 nextActorHead: permit.fence.actorHead,
225 previousQueueSequence: permit.fence.queueSequence,
226 nextQueueSequence: permit.fence.queueSequence,
227 }),
228 /no registered replay result/,
229 );
230 assert.equal(restarted.snapshot().projects["tenant\u0000project"]?.permit?.id, permit.id);
231});
233test("local install commits heads and exact receipt together and retries idempotently", () => {
234 const { permit, result, local } = setup();
235 const receipt = install(permit, result, local);
236 assert.deepEqual(install(permit, result, local), receipt);
237 const state = local.snapshot().projects["tenant\u0000project"];
238 assert.equal(state?.rootDigest, "root-1");
239 assert.equal(state?.generation, 1);
240 assert.equal(state?.logHead, 12);
241 assert.equal(state?.sharedHead, 5);
242 assert.equal(state?.actorHead, 7);
243 assert.equal(state?.queueSequence, 4);
244 assert.deepEqual(local.readReceipt("tenant", "project", "operation-1"), receipt);
245 assert.throws(() => install(permit, { ...result, outputDigest: "conflict" }, local), /conflicting install receipt/);
246});
248test("a stale local snapshot or mismatched challenge changes neither state nor receipt", () => {
249 const { permit, result, local } = setup();
250 local.seed("tenant", "project", permit.fence.incarnation, "changed-root", 0, 10, 4, 6, 3);
251 const before = local.snapshot();
252 assert.throws(() => install(permit, result, local), /changed after snapshot/);
253 assert.deepEqual(local.snapshot(), before);
255 const fresh = new LocalInstallStoreModel();
256 fresh.seed("tenant", "project", permit.fence.incarnation, permit.fence.rootDigest, 0, 10, 4, 6, 3);
257 assert.throws(() => install(permit, { ...result, build: "unreviewed-build" }, fresh), /does not match its permit/);
258 assert.equal(fresh.readReceipt("tenant", "project", "operation-1"), null);
259});
261test("a same-ID permit clone cannot redefine the challenge used for replay registration", () => {
262 const { authority, permit, result } = setup();
263 const before = authority.snapshot();
264 const alteredPermit: LifecyclePermit = {
265 ...permit,
266 fence: {
267 ...permit.fence,
268 actor: "bob",
269 authorizationRevision: 2,
270 policyRevision: 7,
271 build: "different-build",
272 },
273 };
274 const alteredResult = {
275 ...result,
276 actor: "bob",
277 authorizationRevision: 2,
278 policyRevision: 7,
279 build: "different-build",
280 };
282 assert.throws(
283 () => authority.registerReplayResult(alteredPermit, alteredResult),
284 /does not match the authoritative challenge/,
285 );
286 assert.deepEqual(authority.snapshot(), before);
287});
289test("completed lifecycle transitions are not replayed by later installs after restart", () => {
290 const initial = setup();
291 let authority = initial.authority;
292 let local = initial.local;
293 let actor = "alice";
294 let operationNumber = 0;
295 let rootDigest = initial.fence.rootDigest;
296 let generation = initial.fence.generation;
297 let logHead = initial.fence.logHead;
298 let sharedHead = initial.fence.sharedHead;
299 let actorHead = initial.fence.actorHead;
300 let queueSequence = initial.fence.queueSequence;
302 const perform = (transition?: { kind: "delete" | "migration" | "revoke-actor"; actor?: string }) => {
303 const project = authority.snapshot().projects["tenant\u0000project"]!;
304 const operationId = `operation-${++operationNumber}`;
305 const fence: LifecycleFence = {
306 ...initial.fence,
307 operationId,
308 actor,
309 incarnation: project.incarnation,
310 authorizationRevision: project.authorizationRevision,
311 policyRevision: project.policyRevision,
312 rootDigest,
313 generation,
314 logHead,
315 sharedHead,
316 actorHead,
317 queueSequence,
318 };
319 const permit = authority.grant(fence);
320 if (transition)
321 authority.requestTransition(
322 "tenant",
323 "project",
324 `transition-${operationNumber}`,
325 transition.kind,
326 transition.actor,
327 );
328 const result: VerifiedReplayResult = {
329 permitId: permit.id,
330 operationId,
331 requestDigest: fence.requestDigest,
332 inputDigest: fence.inputDigest,
333 actor: fence.actor,
334 incarnation: fence.incarnation,
335 authorizationRevision: fence.authorizationRevision,
336 policyRevision: fence.policyRevision,
337 build: fence.build,
338 outputDigest: `output-${operationNumber}`,
339 nextRootDigest: `root-${operationNumber}`,
340 nextGeneration: generation + 1,
341 nextLogHead: logHead + 2,
342 nextSharedHead: sharedHead + 1,
343 nextActorHead: actorHead + 1,
344 nextQueueSequence: queueSequence,
345 };
346 authority.registerReplayResult(permit, result);
347 const receipt = local.install(permit, result);
348 authority.resolveInstall(receipt);
349 rootDigest = result.nextRootDigest;
350 generation = result.nextGeneration;
351 logHead = result.nextLogHead;
352 sharedHead = result.nextSharedHead;
353 actorHead = result.nextActorHead;
354 queueSequence = result.nextQueueSequence;
355 return authority.snapshot().projects["tenant\u0000project"]!;
356 };
358 const restart = () => {
359 const snapshot = authority.snapshot();
360 authority = LifecycleAuthorityModel.restart(snapshot, snapshot.revision);
361 local = LocalInstallStoreModel.restart(local.snapshot());
362 };
363 const rebindLocalIncarnation = (incarnation: number) => {
364 const snapshot = local.snapshot();
365 const projectKey = "tenant\u0000project";
366 const projectState = snapshot.projects[projectKey]!;
367 local = LocalInstallStoreModel.restart({
368 ...snapshot,
369 projects: { ...snapshot.projects, [projectKey]: { ...projectState, incarnation } },
370 });
371 };
373 let project = perform({ kind: "migration" });
374 assert.equal(project.incarnation, 2);
375 assert.equal(project.authorityRevision, authority.snapshot().revision);
376 restart();
377 rebindLocalIncarnation(project.incarnation);
378 project = perform();
379 assert.equal(project.incarnation, 2);
381 project = perform({ kind: "revoke-actor", actor: "alice" });
382 assert.equal(project.authorizationRevision, 2);
383 assert.equal(project.actors.includes("alice"), false);
384 actor = "bob";
385 restart();
386 project = perform();
387 assert.equal(project.incarnation, 2);
388 assert.equal(project.authorizationRevision, 2);
390 project = perform({ kind: "delete" });
391 assert.equal(project.phase, "deleted");
392 assert.equal(project.incarnation, 3);
393 restart();
394 authority.requestTransition("tenant", "project", "restore-final", "restore");
395 project = authority.snapshot().projects["tenant\u0000project"]!;
396 assert.equal(project.phase, "active");
397 assert.equal(project.incarnation, 4);
398 restart();
399 local = new LocalInstallStoreModel();
400 local.seed(
401 "tenant",
402 "project",
403 project.incarnation,
404 rootDigest,
405 generation,
406 logHead,
407 sharedHead,
408 actorHead,
409 queueSequence,
410 );
411 project = perform();
412 assert.equal(project.incarnation, 4);
413 assert.equal(project.authorizationRevision, 2);
414});
416test("actor revocation drains an active permit and invalidates the old authorization revision", () => {
417 const { authority, permit, result, local } = setup();
418 assert.equal(
419 authority.requestTransition("tenant", "project", "revoke-alice", "revoke-actor", "alice").status,
420 "draining",
421 );
422 authority.registerReplayResult(permit, result);
423 const receipt = install(permit, result, local);
424 authority.resolveInstall(receipt);
425 const state = authority.snapshot().projects["tenant\u0000project"];
426 assert.equal(state?.authorizationRevision, 2);
427 assert.equal(state?.actors.includes("alice"), false);
428 assert.throws(() => authority.grant({ ...permit.fence, operationId: "operation-2" }), /actor is not authorized/);
429});
431test("authority rollback behind the independent high-water floor fails closed", () => {
432 const { authority, permit } = setup();
433 const behind = LifecycleAuthorityModel.restart(authority.snapshot(), authority.snapshot().revision + 1);
434 assert.throws(() => behind.grant({ ...permit.fence, operationId: "operation-2" }), /rollback blocks/);
435 assert.throws(() => behind.requestTransition("tenant", "project", "delete-1", "delete"), /rollback blocks/);
436 assert.throws(() => behind.createProject("tenant", "other", ["alice"]), /rollback blocks/);
437 assert.equal(behind.snapshot().projects["tenant\u0000project"]?.phase, "blocked");
438});