- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/test/told-buckets.test.ts?format=json
221 linescapacity 100%Last change capacity in version 262, 2 versions changed it
1// Each agent's told keys in bucket rows of their own (abso Capacity, issue 664; Security yes on ask 2102;
2// writer-agents.ts). News on a stored writer must match the in-memory project's, every step answered twice so no news
3// repeats: for agents whose keys are in buckets, and for an agent read mid-move, its keys half in an old row's list and
4// half in its buckets. A write moves an old row's list into the buckets; one agent never reads another's; a rollback
5// leaves the buckets as they were.
7import assert from "node:assert/strict";
8import { test } from "node:test";
9import { storedArray } from "../../../src/engine/state-source.ts";
10import { SqlState } from "../../../src/engine/test/storage-helper.ts";
11import { MemoryStore, Project } from "../project.ts";
12import type { Entry, ProjectStore } from "../project-types.ts";
13import { StoredTold, TOLD_BUCKETS, WriterAgents } from "../writer-agents.ts";
15const TOLD = "project:agents:told";
16const AGENTS = "project:agents";
17const agentKey = (name: string) => JSON.stringify(["string", name]);
18const toldRow = (name: string, bucket: number) => JSON.stringify([name, bucket]);
20function writer() {
21 const storage = new SqlState();
22 const entries = storedArray<Entry>(storage, "canonical-log");
23 const store: ProjectStore = {
24 append(batch) {
25 for (const entry of batch) entries.push(entry);
26 },
27 };
28 return { storage, entries, store, project: new Project(store, { writerStorage: storage }) };
29}
31/** Each step's answer, and the news each agent has then, asked twice: as JSON, as a client gets them. */
32function step(project: Project, f: (p: Project) => unknown): unknown {
33 const answer = f(project);
34 // The news texts (each item's time is the clock when it was written, which differs between the two).
35 const news = ["ann", "bob", "cy"].map((a) => [project.news(a).news, project.news(a).news]);
36 return JSON.parse(JSON.stringify({ answer, news }));
37}
39const FIRST: ((p: Project) => unknown)[] = [
40 (p) => p.init({ "a.ts": "one\ntwo\nthree\n", "b.ts": "export function f() {}\n", "c.ts": "c\n" }, 0),
41 (p) => p.edit("ann", { file: "a.ts", old: "two", new: "TWO" }, 1),
42 (p) => p.edit("bob", { file: "a.ts", old: "two", new: "Two" }, 2),
43 (p) => p.edit("cy", { file: "c.ts", old: "c", new: "C" }, 3),
44 (p) => p.publish("ann", "Shout two", 4),
45 (p) => p.record("bob", { "a.ts": "one\nTwo\nthree\nfour\n" }, 5),
46 (p) => p.record("bob", { "a.ts": "one\nTwo\nthree\nfour\nfive\n" }, 6),
47 (p) => p.announce("ann", "First announcement", undefined, 7),
48 (p) => p.announce("ann", "Second announcement", ["bob"], 8),
49];
50const THEN: ((p: Project) => unknown)[] = [
51 (p) => p.record("bob", { "a.ts": "one\nTwo\nthree\nfour\nfive\nsix\n" }, 9),
52 (p) => p.edit("ann", { file: "a.ts", old: "three", new: "THREE" }, 10),
53 (p) => p.edit("bob", { file: "a.ts", old: "three", new: "Three" }, 11),
54 (p) => p.publish("ann", "Shout three", 12),
55 (p) => p.record("bob", { "a.ts": "one\nTwo\nThree\nfour\nfive\nsix\nseven\n" }, 13),
56 (p) => p.announce("cy", "Third announcement", undefined, 14),
57 (p) => p.sync("cy"),
58];
60const bucketKeys = (storage: SqlState, name: string) => {
61 const out: string[] = [];
62 for (let b = 0; b < TOLD_BUCKETS; b++)
63 out.push(...((storage.read(TOLD, toldRow(name, b)) as string[] | undefined) ?? []));
64 return out.sort();
65};
67test("news on a stored writer matches the in-memory project, before and after an agent's told keys are half moved", () => {
68 const oracle = new Project(new MemoryStore());
69 const { storage, entries, store, project: first } = writer();
70 try {
71 for (const f of FIRST) assert.deepEqual(step(first, f), step(oracle, f));
72 // bob's keys are in buckets, and its row lists none.
73 const keys = bucketKeys(storage, "bob");
74 assert.ok(keys.length >= 2, `bob was told ${keys.length} keys`);
75 const row = storage.read(AGENTS, agentKey("bob")) as { slot: number; value: Record<string, unknown> };
76 assert.equal(row.value.told, undefined);
77 // Make bob's row an old one mid-move: half its keys back in the row's list, out of its buckets.
78 const moved = keys.filter((_, i) => i % 2 === 0);
79 storage.write(AGENTS, agentKey("bob"), { ...row, value: { ...row.value, told: moved } });
80 const told = new StoredTold(storage, "bob");
81 for (const key of moved) told.delete(key);
82 assert.deepEqual(
83 bucketKeys(storage, "bob"),
84 keys.filter((_, i) => i % 2 === 1),
85 );
86 // A writer opened afresh reads bob's told keys from both, and news goes on as in memory.
87 const project = Project.openWriter(store, storage, entries.length);
88 assert.deepEqual([...new StoredTold(storage, "bob", moved)].sort(), keys);
89 for (const f of THEN) assert.deepEqual(step(project, f), step(oracle, f));
90 // bob was written since: its row lists nothing, and its buckets hold every key it had and the new ones.
91 const after = storage.read(AGENTS, agentKey("bob")) as { value: Record<string, unknown> };
92 assert.equal(after.value.told, undefined);
93 const now = bucketKeys(storage, "bob");
94 for (const key of keys) assert.ok(now.includes(key), key);
95 assert.ok(now.length > keys.length, "bob was told more since");
96 } finally {
97 storage.db.close();
98 }
99});
101test("an agent's told keys are its own, and a rollback leaves the buckets as they were", () => {
102 const storage = new SqlState();
103 try {
104 const bob = new StoredTold(storage, "bob");
105 const ann = new StoredTold(storage, "ann");
106 bob.add("ann bob");
107 bob.add("announce 1");
108 assert.equal(bob.has("ann bob"), true);
109 assert.equal(ann.has("ann bob"), false);
110 assert.equal(ann.has("announce 1"), false);
111 assert.throws(
112 () =>
113 storage.transaction(() => {
114 bob.add("announce 2");
115 assert.equal(bob.has("announce 2"), true);
116 throw new Error("rollback");
117 }),
118 /rollback/,
119 );
120 assert.equal(bob.has("announce 2"), false);
121 assert.deepEqual([...bob].sort(), ["ann bob", "announce 1"]);
122 bob.clear();
123 assert.deepEqual([...bob], []);
124 } finally {
125 storage.db.close();
126 }
127});
129test("a project written before the buckets moves every agent's told list over a few writes, news unchanged", () => {
130 const oracle = new Project(new MemoryStore());
131 const { storage, entries, store, project: first } = writer();
132 const names = Array.from({ length: 9 }, (_, i) => `agent${i}`);
133 const setup: ((p: Project) => unknown)[] = [
134 (p) => p.init({ "a.ts": "one\n" }, 0),
135 ...names.map((name, i) => (p: Project) => p.record(name, { [`${name}.ts`]: `${i}\n` }, i + 1)),
136 (p) => p.record("boss", { "boss.ts": "b\n" }, 19),
137 // An announcer isn't told its own announcement: someone else announces to the nine.
138 (p) => p.announce("boss", "To everyone", names, 20),
139 ];
140 try {
141 for (const f of setup) {
142 f(first);
143 f(oracle);
144 }
145 // Every agent's row back to the old form: its told keys in the row's list, none in buckets.
146 const told = new Map<string, string[]>();
147 for (const name of names) {
148 const keys = bucketKeys(storage, name);
149 assert.ok(keys.length > 0, `${name} was told something`);
150 told.set(name, keys);
151 const row = storage.read(AGENTS, agentKey(name)) as { slot: number; value: Record<string, unknown> };
152 storage.write(AGENTS, agentKey(name), { ...row, value: { ...row.value, told: keys } });
153 for (let b = 0; b < TOLD_BUCKETS; b++) storage.remove(TOLD, toldRow(name, b));
154 }
155 // As a project adopted before the buckets: no move started.
156 storage.remove("project:agents:told:meta", "cursor");
157 const oldRows = () =>
158 names.filter((n) => (storage.read(AGENTS, agentKey(n)) as { value: { told?: unknown } }).value.told);
159 assert.equal(oldRows().length, 9);
160 const project = Project.openWriter(store, storage, entries.length);
161 const writes = [
162 (p: Project) => p.record("agent8", { "agent8.ts": "8\nmore\n" }, 30),
163 (p: Project) => p.record("agent8", { "agent8.ts": "8\nmore\nagain\n" }, 31),
164 (p: Project) => p.announce("boss", "Once more", names, 32),
165 ];
166 const left = [];
167 for (const f of writes) {
168 assert.deepEqual(step(project, f), step(oracle, f));
169 left.push(oldRows().length);
170 }
171 // Every row moved within the writes (each write rewrites the agents it touches, and the cursor the rest).
172 assert.equal(left.at(-1), 0);
173 for (const name of names)
174 for (const key of told.get(name)!) assert.ok(bucketKeys(storage, name).includes(key), key);
175 // Past every agent in the order they joined.
176 assert.equal(
177 storage.read("project:agents:told:meta", "cursor"),
178 storedArray(storage, "project:agents:order").length,
179 );
180 } finally {
181 storage.db.close();
182 }
183});
185test("the move takes a few untouched agents' old rows at a time, in the order they joined, once", () => {
186 const storage = new SqlState();
187 try {
188 const agents = new WriterAgents(storage);
189 const names = Array.from({ length: 10 }, (_, i) => `a${i}`);
190 for (const name of names) {
191 const agent = {
192 name,
193 space: "",
194 base: 0,
195 mine: [],
196 news: [],
197 newsAt: [],
198 told: new Set([`k ${name}`]),
199 parked: new Map(),
200 };
201 agents.set(name, agent as unknown as Parameters<typeof agents.set>[1]);
202 }
203 agents.flush();
204 // Back to old rows, with no move started.
205 for (const name of names) {
206 const row = storage.read(AGENTS, agentKey(name)) as { slot: number; value: Record<string, unknown> };
207 storage.write(AGENTS, agentKey(name), { ...row, value: { ...row.value, told: [`k ${name}`] } });
208 for (let b = 0; b < TOLD_BUCKETS; b++) storage.remove(TOLD, toldRow(name, b));
209 }
210 const fresh = new WriterAgents(storage);
211 const moves = [fresh.moveOldTold(4), fresh.moveOldTold(4), fresh.moveOldTold(4), fresh.moveOldTold(4)];
212 assert.deepEqual(moves, [4, 4, 2, 0]);
213 for (const name of names) {
214 assert.deepEqual(bucketKeys(storage, name), [`k ${name}`]);
215 assert.equal((storage.read(AGENTS, agentKey(name)) as { value: { told?: unknown } }).value.told, undefined);
216 assert.equal(new WriterAgents(storage).get(name)!.told.has(`k ${name}`), true);
217 }
218 } finally {
219 storage.db.close();
220 }
221});