- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/test/collision-streaming.test.ts?format=json
157 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 162, 3 versions changed it
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { buildBase, Graph } from "../../../src/engine/index.ts";
4import { CachedStateStorage, StableOverlaySet, unionOverlay } from "../../../src/engine/state-source.ts";
5import { SqlState } from "../../../src/engine/test/storage-helper.ts";
6import { type CollisionHost, openPairs, pairsIn } from "../collisions.ts";
7import type { AgentState } from "../project-types.ts";
8import { newSpace } from "../spaces.ts";
10test("persistent conflict candidates and resolver intersections stream in exact canonical order", (t) => {
11 const source = new SqlState();
12 try {
13 const graph = new Graph({ storage: new CachedStateStorage(source, 8192, 32) });
14 graph.add(buildBase({ "a.txt": "one\n" }, { author: "fixture", message: "base", time: 0 }));
15 const count = 513;
16 for (let seq = 1; seq <= count + 1; seq++)
17 graph.add({
18 id: `a${seq}`,
19 seq,
20 author: "fixture",
21 message: "claim",
22 time: seq,
23 inserts: [],
24 deletes: [],
25 writes: ["L:a0.0"],
26 reads: ["G:^a.txt"],
27 holds: [],
28 needs: [],
29 resolves: [],
30 reasons: [],
31 deps: ["a0"],
32 });
33 const draft = `a${count + 1}`;
34 const against = new Set(Array.from({ length: count }, (_, n) => `a${n + 1}`));
35 const settled = new Set<string>();
36 for (let n = 1; n <= count; n += 2) {
37 const seq = graph.size;
38 const id = `a${seq}`;
39 graph.add({
40 id,
41 seq,
42 author: "fixture",
43 message: "resolve",
44 time: seq,
45 inserts: [],
46 deletes: [],
47 writes: [],
48 reads: [],
49 holds: [],
50 needs: [],
51 resolves: [`a${n}`, draft],
52 reasons: [],
53 deps: [`a${n}`, draft],
54 });
55 settled.add(id);
56 }
57 const reopened = new Graph({ storage: new CachedStateStorage(source, 8192, 32) });
58 for (const method of ["writersOf", "readersOf", "holdersOf", "resolversOf"] as const)
59 t.mock.method(reopened, method, () => {
60 throw new Error(`materialized ${method} forbidden`);
61 });
62 assert.deepEqual(
63 pairsIn(reopened, [draft], against, new Set()).map((pair) => pair.theirs),
64 [...against],
65 );
66 assert.deepEqual(
67 pairsIn(reopened, [draft], against, settled).map((pair) => pair.theirs),
68 [...against].filter((id) => Number(id.slice(1)) % 2 === 0),
69 );
70 const selected = new Set(["a1", "a257", "a513"]);
71 assert.deepEqual(pairsIn(reopened, [draft], selected, settled), []);
72 const base = new StableOverlaySet(against);
73 t.mock.method(base, "values", () => {
74 throw Error("enumerating the complete base membership is forbidden");
75 });
76 const actor: AgentState = {
77 name: "fixture",
78 space: "",
79 base: 0,
80 synced: true,
81 mine: ["a1", draft],
82 news: [],
83 newsAt: [],
84 told: new Set(),
85 parked: new Map(),
86 marked: [],
87 };
88 const space = newSpace("");
89 const host: CollisionHost = {
90 graph: reopened,
91 spaces: new Map([["", space]]),
92 version: () => 0,
93 spaceOf: () => space,
94 agentSet: (agent) => unionOverlay(base, agent.mine),
95 draft: () => [draft],
96 at: () => base,
97 shared: () => space,
98 agentView: () => {
99 throw Error("open pairs must not build a View");
100 },
101 };
102 assert.deepEqual(
103 openPairs(host, actor).map((pair) => pair.theirs),
104 [...against].filter((id) => id !== "a1"),
105 );
106 } finally {
107 source.db.close();
108 }
109});
111test("wide claim merging reads each stream head once and closes streams on failure", () => {
112 const count = 256;
113 let reads = 0,
114 opened = 0,
115 closed = 0;
116 let fail = false;
117 const draft = {
118 seq: 3,
119 writes: Array.from({ length: count }, (_, n) => `L:a0.${n}`),
120 reads: [],
121 holds: [],
122 inserts: [],
123 deletes: [],
124 deps: [],
125 resolves: [],
126 needs: [],
127 reasons: [],
128 };
129 const stream = function* () {
130 opened++;
131 try {
132 yield "a0";
133 yield "a1";
134 if (fail) throw Error("claim stream failure");
135 yield "a2";
136 } finally {
137 closed++;
138 }
139 };
140 const graph = {
141 atom: (id: string) => {
142 reads++;
143 return id === "a3" ? draft : { seq: Number(id.slice(1)) };
144 },
145 eachWriterOf: stream,
146 eachReaderOf: stream,
147 eachHolderOf: stream,
148 } as unknown as Graph;
149 assert.deepEqual(pairsIn(graph, ["a3"], new Set(), new Set()), []);
150 assert.ok(reads <= count * 12 + 1, `Unexpected repeated head reads: ${reads}`);
151 assert.equal(closed, opened);
152 fail = true;
153 opened = 0;
154 closed = 0;
155 assert.throws(() => pairsIn(graph, ["a3"], new Set(), new Set()), /claim stream failure/);
156 assert.equal(closed, opened);
157});