Skip to content

Commit 8136513

Browse files
author
Carl
committed
fix(relay): confirm template setup and agent removal on writer
Signed-off-by: Carl <32a2e2c9d428ee08902cab75d956da2c1d235a22d4766b0dd4138bf6e2e5db1d@buzz.block.builderlab.xyz>
1 parent f19fd77 commit 8136513

6 files changed

Lines changed: 144 additions & 11 deletions

File tree

‎docs/relay-queries.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ eligible. Details/member-admin dialogs use writer-backed state for their shared
8585
load/preflight/confirmation reads. Work-session membership preflights (including
8686
session sends and canvas saves) also use the writer, so a just-added member does
8787
not fail the next operation.
88+
Template setup also confirms exact Canvas/member events and selected Canvas heads
89+
against the writer without replaying accepted commands. Agent deletion discovers
90+
member channels and confirms each removal with writer-backed rosters; unreadable
91+
rosters still fail closed. Ordinary Canvas browsing and standalone Canvas/recipe
92+
save confirmation routing are unchanged by this policy.
8893

8994
## Community emoji
9095

‎src/bundled/channel-templates/agent-selection.test.tsx‎

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { createAgentControl } from "../../features/agents/control";
2626
import { controlFixture } from "../../features/agents/control-testing";
2727
import { keypair, signed, roster } from "../../features/relay/testing";
2828
import { matchesEvent } from "../../features/relay/projection";
29-
import type { RelayEvent } from "../../features/relay/events";
29+
import type { ReadFilter, RelayEvent } from "../../features/relay/events";
3030
import { createOutbox, type OutgoingEvent } from "../../features/relay/outbox";
3131
import type { EventTemplate } from "nostr-tools";
3232
import type { KitRecord } from "../../features/channel-templates/model";
@@ -105,6 +105,8 @@ function harness(
105105
});
106106
const stored = new Map<string, KitRecord>();
107107
const published: RelayEvent[] = [];
108+
const replica = { lag: false };
109+
const reads: ReadFilter[] = [];
108110
const sign = vi.fn(async (value: EventTemplate) => signed(viewer, value));
109111
let journal: readonly OutgoingEvent[] = [];
110112
const channels = new Map<string, string[]>([
@@ -177,6 +179,7 @@ function harness(
177179
},
178180
},
179181
query: async (filters) => {
182+
reads.push(...filters);
180183
if (
181184
filters.some((filter) =>
182185
filter.kinds?.some((kind) => [39000, 39002].includes(kind)),
@@ -211,7 +214,13 @@ function harness(
211214
),
212215
];
213216
return events.filter((event) =>
214-
filters.some((filter) => matchesEvent(event, filter)),
217+
filters.some(
218+
(filter) =>
219+
matchesEvent(event, filter) &&
220+
(!replica.lag ||
221+
filter.consistency === "strong" ||
222+
![9007, 9000, 40100, 39000, 39002].includes(event.kind)),
223+
),
215224
);
216225
},
217226
},
@@ -231,6 +240,8 @@ function harness(
231240
native,
232241
fixture,
233242
published,
243+
replica,
244+
reads,
234245
sign,
235246
viewer,
236247
journal: () => journal,
@@ -1256,6 +1267,60 @@ it.each([
12561267
},
12571268
);
12581269

1270+
it.each(["", "# Seed plan"])(
1271+
"finishes template setup against the writer with stale replicas and no live echoes (Canvas: %s)",
1272+
async (canvas) => {
1273+
const test = harness();
1274+
test.replica.lag = true;
1275+
try {
1276+
const id = await test.owner.session.channelCreation.create({
1277+
name: "Writer-confirmed setup",
1278+
visibility: "private",
1279+
setup: {
1280+
agents: [test.fixture.agent.pubkey],
1281+
canvas,
1282+
groupId: "",
1283+
templateId: "saved",
1284+
},
1285+
});
1286+
const receipt = `buzz-channel-setup.v2:https://relay.example.test:${test.viewer.pubkey}:${id}`;
1287+
// Receipt retirement is the completion barrier, not admission or publish ACK.
1288+
await waitFor(() => expect(localStorage.getItem(receipt)).toBeNull());
1289+
expect(test.owner.session.channelCreation.notices()).toEqual([]);
1290+
expect(test.published.map((event) => event.kind)).toEqual(
1291+
canvas ? [9007, 40100, 9000] : [9007, 9000],
1292+
);
1293+
expect(test.sign).toHaveBeenCalledTimes(test.published.length);
1294+
expect(test.journal()).toEqual([]);
1295+
expect(test.owner.session.channels.get?.(id)?.members).toContain(
1296+
test.fixture.agent.pubkey,
1297+
);
1298+
for (const event of test.published.filter((event) => event.kind !== 9007))
1299+
expect(test.reads).toContainEqual({
1300+
ids: [event.id],
1301+
limit: 1,
1302+
consistency: "strong",
1303+
});
1304+
if (canvas) {
1305+
const heads = test.reads.filter((filter) =>
1306+
filter.kinds?.includes(40100),
1307+
);
1308+
expect(heads).toHaveLength(3); // Before seeding, after save, before members.
1309+
expect(heads.every((filter) => filter.consistency === "strong")).toBe(
1310+
true,
1311+
);
1312+
// Ordinary browsing still uses the lagging replica, not the writer.
1313+
await expect(
1314+
test.owner.session.canvas.read(id),
1315+
).resolves.toBeUndefined();
1316+
expect(test.reads.at(-1)?.consistency).toBeUndefined();
1317+
}
1318+
} finally {
1319+
test.dispose();
1320+
}
1321+
},
1322+
);
1323+
12591324
it("retains a failed group placement independently and permits another Create", async () => {
12601325
const test = harness();
12611326
try {

‎src/bundled/profiles/ProfileAgentArchive.test.tsx‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { afterEach, expect, it, vi } from "vitest";
1919
import { createRelaySession } from "../../features/relay/session";
2020
import type { LiveCallbacks } from "../../features/relay/live";
2121
import type { RelayData } from "../../features/relay/service";
22+
import { matchesEvent } from "../../features/relay/projection";
2223
import {
2324
archiveRelay,
2425
keypair,
@@ -418,6 +419,49 @@ it("verified owner deletes through the base confirmation: confirmed channel remo
418419
expect(fixture.instance.session.outbox?.snapshot()).toEqual([]),
419420
);
420421
});
422+
it("deletes using writer rosters when replicas miss a recent channel and retain removed members", async () => {
423+
const fixture = mountManaged(viewer, true, {
424+
[room]: [viewer.pubkey, agent.pubkey],
425+
});
426+
const query = fixture.transport.query;
427+
const replica = await query(
428+
[
429+
{
430+
kinds: [39002],
431+
authors: [relay.pubkey],
432+
"#p": [agent.pubkey],
433+
limit: 500,
434+
},
435+
],
436+
new AbortController().signal,
437+
);
438+
// A recent addition exists only on the writer and is not in the loaded list.
439+
fixture.channels[hidden] = [agent.pubkey];
440+
expect(fixture.instance.session.channels.list().channels).not.toContainEqual(
441+
expect.objectContaining({ id: hidden }),
442+
);
443+
fixture.transport.query = async (filters, ...rest) =>
444+
(
445+
await Promise.all(
446+
filters.map((filter) =>
447+
filter.kinds?.includes(39002) && filter.consistency !== "strong"
448+
? replica.filter((event) => matchesEvent(event, filter))
449+
: query([filter], ...rest),
450+
),
451+
)
452+
).flat();
453+
await confirmDelete(userEvent.setup());
454+
await vi.waitFor(() => expect(fixture.close).toHaveBeenCalledOnce());
455+
expect(fixture.published.map((event) => event.kind)).toEqual([
456+
9001, 9001, 9035,
457+
]);
458+
expect(fixture.channels).toEqual({ [room]: [viewer.pubkey], [hidden]: [] });
459+
expect(deletes(fixture)).toHaveLength(1);
460+
await vi.waitFor(() =>
461+
expect(fixture.instance.session.outbox?.snapshot()).toEqual([]),
462+
);
463+
});
464+
421465
it("cancel leaves the agent untouched", async () => {
422466
const fixture = mountManaged();
423467
const user = userEvent.setup();

‎src/features/channel-templates/capability.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,14 @@ export function createChannelKit({
227227
});
228228
const canvas = Object.freeze({
229229
available: !!outbox?.supports(40100),
230-
async read(channel: string) {
230+
async read(channel: string, options: Pick<ReadFilter, "consistency"> = {}) {
231231
signal.throwIfAborted();
232232
if (!canWrite(channel))
233233
throw new Error("Canvas is unavailable after channel access changed");
234234
return selectedHead(
235-
await fresh([{ kinds: [40100], "#h": [channel], limit: 1 }]),
235+
await fresh([
236+
{ kinds: [40100], "#h": [channel], limit: 1, ...options },
237+
]),
236238
);
237239
},
238240
async save(channel: string, content: string, expected: string | undefined) {

‎src/features/relay/session.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,16 +1190,17 @@ export function createRelaySession(
11901190
await workSessions.delivered(id, undefined, false, false);
11911191
if (
11921192
!(
1193-
await verified.read([{ ids: [id], limit: 1 }], {
1194-
signal: lifetime.signal,
1195-
fresh: true,
1196-
})
1193+
await verified.read(
1194+
[{ ids: [id], limit: 1, consistency: "strong" }],
1195+
{ signal: lifetime.signal, fresh: true },
1196+
)
11971197
).some((event) => event.id === id)
11981198
)
11991199
throw new Error("Setup is awaiting exact relay confirmation");
12001200
},
12011201
refresh: (id, member) => workSessions.refresh(id, { member }, false),
1202-
canvasHead: async (id) => (await channelKit.canvas.read(id))?.id,
1202+
canvasHead: async (id) =>
1203+
(await channelKit.canvas.read(id, { consistency: "strong" }))?.id,
12031204
async preflight(input) {
12041205
if (
12051206
!input.name.trim() ||

‎src/features/relay/work-sessions.ts‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,15 @@ export function createWorkSessions(
316316
if (!relayAuthor) throw new Error("Channel membership is unavailable.");
317317
const limit = 500;
318318
const events = await discovery.read(
319-
[{ kinds: [39002], authors: [relayAuthor], "#p": [pubkey], limit }],
319+
[
320+
{
321+
kinds: [39002],
322+
authors: [relayAuthor],
323+
"#p": [pubkey],
324+
limit,
325+
consistency: "strong",
326+
},
327+
],
320328
{
321329
signal: AbortSignal.any([signal, caller]),
322330
fresh: true,
@@ -343,7 +351,15 @@ export function createWorkSessions(
343351
async function listsMember(id: string, pubkey: string, caller: AbortSignal) {
344352
if (!relayAuthor) throw new Error("Channel membership is unavailable.");
345353
const events = await discovery.read(
346-
[{ kinds: [39002], authors: [relayAuthor], "#d": [id], limit: 1 }],
354+
[
355+
{
356+
kinds: [39002],
357+
authors: [relayAuthor],
358+
"#d": [id],
359+
limit: 1,
360+
consistency: "strong",
361+
},
362+
],
347363
{
348364
signal: AbortSignal.any([signal, caller]),
349365
fresh: true,

0 commit comments

Comments
 (0)