Skip to content

Commit 12a4c39

Browse files
loganjLarryclaude
authored
fix(messages): read membership notices apart from history pages (#1983)
Signed-off-by: Logan Johnson <[email protected]> Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent ccd7002 commit 12a4c39

9 files changed

Lines changed: 500 additions & 37 deletions

File tree

‎docs/channels.md‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,8 +1249,16 @@ guarantee of general profile retry after every cache clear/network failure. See
12491249

12501250
## Membership activity
12511251

1252-
Channel history and the existing live route include relay-signed kind-40099
1253-
`member_joined`, `member_left` and `member_removed` summaries. Only recognized,
1252+
The live route includes relay-signed kind-40099 `member_joined`, `member_left` and
1253+
`member_removed` summaries. History pages do not: a NIP-CW page counts every row kind
1254+
against its limit, so a burst of joins could fill pages and hold messages back. After
1255+
each page or head read, the window reads the relay's notices apart, newest first in
1256+
pages of 500, down to its oldest loaded message, so the messages show first and the
1257+
activity between them follows. A head read re-reads the newest notices; when they do
1258+
not meet the ones held, the descent starts over. A window holds at most 2,000 notices
1259+
read this way, outside its row budget; older pages past that show without activity.
1260+
A failed notice read leaves activity out until the next page or head read and never
1261+
errors the window. Saved heads carry no notices. Only recognized,
12541262
channel-scoped payloads from the connected relay become activity rows; malformed,
12551263
unknown and other authors' summaries are not rendered as JSON. These events do not
12561264
grant/revoke access: the existing signed roster remains authoritative.
@@ -1259,7 +1267,8 @@ The timeline groups adjacent arrivals/departures into compact avatar-and-text ro
12591267
Messages, local-day changes and gaps over an hour break groups; removals by different
12601268
actors stay separate. Same-adder additions use “added by you” for the viewer;
12611269
mixed arrivals do not invent an adder. Grouping is presentation-only: signed event
1262-
IDs, pagination cursors and retention budgets remain per event. Profiles reuse the
1270+
IDs and cursors remain per event, and live activity counts against the row budget.
1271+
Profiles reuse the
12631272
shared background directory/cache, and reading anchors can resolve a member of a
12641273
group. Activity has no message actions, thread, unread evidence or chat preview.
12651274
A Solo chat omits its owner's additions and removals of agents before its first

‎src/features/relay/diff-messages.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ it("reads diff history, live roots/replies and exact targets through the real se
5050
f["#e"]?.includes(value),
5151
)),
5252
);
53-
return f.kinds?.includes(40099) && f["#h"]
53+
return f.top_level && f["#h"]
5454
? [
5555
...matched,
5656
bounds(relay, "a", "head", {

‎src/features/relay/membership.test.ts‎

Lines changed: 228 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
signed,
1414
} from "./testing";
1515
import type { LiveCallbacks } from "./live";
16+
import type { ReadFilter } from "./events";
1617
import type { HeadPersistence, SavedHead } from "./persistence";
1718
import { rowProfileIds } from "./membership";
1819

@@ -84,10 +85,27 @@ it("admits only recognized, channel-scoped relay-authored membership payloads",
8485
).toEqual([]);
8586
});
8687

88+
/** Take the pending read `match` selects, in whatever order the store issued it. */
89+
const take = (
90+
wire: ReturnType<typeof scriptedTransport>,
91+
match: (filter: ReadFilter) => boolean,
92+
) => {
93+
const index = wire.pending.findIndex((entry) => entry.filters.some(match));
94+
const [entry] = index < 0 ? [] : wire.pending.splice(index, 1);
95+
assert(entry, "No matching pending read");
96+
return entry;
97+
};
98+
const isWindow = (filter: ReadFilter) => filter.top_level === true;
99+
const isNotices = (filter: ReadFilter) =>
100+
filter.kinds?.length === 1 && filter.kinds[0] === 40099;
101+
const isNames = (filter: ReadFilter) => !!filter.kinds?.includes(0);
102+
87103
it.each([false, true])(
88104
"history, live, older pages and optional profiles share the existing session (prepared=%s)",
89105
async (prepared) => {
90-
const wire = scriptedTransport(viewer.pubkey, relay.pubkey);
106+
const wire = scriptedTransport(viewer.pubkey, relay.pubkey, {
107+
notices: true,
108+
});
91109
let live!: LiveCallbacks;
92110
const owner = createRelaySession(
93111
{
@@ -111,73 +129,87 @@ it.each([false, true])(
111129
await flush();
112130
q.ensure("a");
113131
const head = wire.next();
132+
// Pages leave notices out, so joins cannot crowd messages from them.
114133
expect([...(head.filters[0]?.kinds ?? [])].sort((a, b) => a - b)).toEqual(
115-
[9, 40002, 40008, 40099, 48100, 48103],
134+
[9, 40002, 40008, 48100, 48103],
116135
);
117-
const row = addition();
136+
const hello = message(viewer, "a", "Hello", 20);
118137
head.respond([
119-
row,
138+
hello,
120139
bounds(relay, "a", "head", {
121140
has_more: true,
122-
next_cursor: { created_at: row.created_at, id: row.id },
141+
next_cursor: { created_at: hello.created_at, id: hello.id },
123142
}),
124143
]);
125144
await flush();
126-
expect(q.window("a").rows.map((r) => r.id)).toEqual([row.id]);
145+
// Messages show before their notices are read.
146+
expect(q.window("a").rows.map((r) => r.id)).toEqual([hello.id]);
147+
const notices = take(wire, isNotices);
148+
expect(notices.filters).toEqual([
149+
{ kinds: [40099], "#h": ["a"], authors: [relay.pubkey], limit: 500 },
150+
]);
151+
// One below the oldest page waits for the page that reaches it.
152+
const row = addition(pinky.pubkey, 21);
153+
const below = addition(brain.pubkey, 19);
154+
notices.respond([row, below]);
155+
await flush();
156+
expect(q.window("a").rows.map((r) => r.id)).toEqual([hello.id, row.id]);
157+
expect(q.window("a").rows[1]?.membership?.target).toBe(pinky.pubkey);
127158
// Render before enrichment, with no per-row blocking request.
128-
const names = wire.next();
129-
expect([...(names.filters[0]?.authors ?? [])].sort()).toEqual(
130-
[viewer.pubkey, pinky.pubkey].sort(),
131-
);
132-
names.respond([
159+
take(wire, isNames).respond([
133160
profile(viewer, { name: "Wes" }),
134161
profile(pinky, { name: "Pinky" }),
135162
]);
136163
await flush();
137-
const chat = message(pinky, "a", "Conversation preview", 21);
138-
live.receive([chat, addition(brain.pubkey, 22)]);
164+
const chat = message(pinky, "a", "Conversation preview", 22);
165+
live.receive([chat, addition(brain.pubkey, 23)]);
139166
await flush();
140167
expect(q.window("a").rows.map((r) => r.id)).toEqual([
168+
hello.id,
141169
row.id,
142170
chat.id,
143-
addition(brain.pubkey, 22).id,
171+
addition(brain.pubkey, 23).id,
144172
]);
145173
expect(q.list().channels[0]?.preview).toBe("Conversation preview");
146174
expect(
147175
owner.session.unread.snapshot({ kind: "channel", channelId: "a" })
148176
.observedCount,
149177
).toBe(1);
150-
const moreNames = wire.next();
151-
expect(moreNames.filters[0]?.authors).toEqual([brain.pubkey]);
152-
moreNames.respond([profile(brain, { name: "Brain" })]);
178+
take(wire, isNames).respond([profile(brain, { name: "Brain" })]);
153179
await flush();
154180
q.loadOlder("a");
155-
const older = wire.next();
181+
const older = take(wire, isWindow);
156182
expect(older.filters[0]).toMatchObject({
157-
kinds: [40002, 40008, 40099, 48100, 48103, 9],
158183
until: 20,
159-
before_id: row.id,
184+
before_id: hello.id,
160185
});
161-
const old = addition(brain.pubkey, 19);
186+
expect(older.filters[0]?.kinds).not.toContain(40099);
187+
const old = message(pinky, "a", "First", 18);
162188
older.respond([
163189
old,
164-
bounds(relay, "a", `20:${row.id}`, {
190+
bounds(relay, "a", `20:${hello.id}`, {
165191
has_more: false,
166192
next_cursor: null,
167193
}),
168194
]);
169195
await flush();
170196
expect(q.window("a").rows.map((r) => r.id)).toEqual([
171197
old.id,
198+
below.id,
199+
hello.id,
172200
row.id,
173201
chat.id,
174-
addition(brain.pubkey, 22).id,
202+
addition(brain.pubkey, 23).id,
175203
]);
204+
// The first notice read reached the start; nothing is left to read.
205+
expect(wire.pending.some((entry) => entry.filters.some(isNotices))).toBe(
206+
false,
207+
);
176208
const before = q.window("a").rows;
177209
live.receive([
178210
activity(
179211
{ type: "member_joined", actor: viewer.pubkey, target: brain.pubkey },
180-
23,
212+
24,
181213
pinky,
182214
),
183215
]);
@@ -190,6 +222,93 @@ it.each([false, true])(
190222
},
191223
);
192224

225+
it("reads notices in relay pages down to the window floor, and a failed read never errors the window", async () => {
226+
const wire = scriptedTransport(viewer.pubkey, relay.pubkey, {
227+
notices: true,
228+
});
229+
let live!: LiveCallbacks;
230+
const owner = createRelaySession({
231+
...wire.transport,
232+
subscribe(callbacks) {
233+
live = callbacks;
234+
return { update() {}, retry() {}, dispose() {} };
235+
},
236+
});
237+
const q = owner.session.channels;
238+
try {
239+
live.receive([roster(relay, "a", [viewer.pubkey])]);
240+
q.ensure("a");
241+
const hello = message(viewer, "a", "Hello", 1_000);
242+
wire.next().respond([
243+
hello,
244+
bounds(relay, "a", "head", {
245+
has_more: true,
246+
next_cursor: { created_at: hello.created_at, id: hello.id },
247+
}),
248+
]);
249+
await flush();
250+
// A full page of joins newer than every loaded message.
251+
const burst = Array.from({ length: 500 }, (_, index) =>
252+
activity(
253+
{
254+
type: "member_joined",
255+
actor: viewer.pubkey,
256+
target: keypair().pubkey,
257+
},
258+
1_001 + Math.floor(index / 2),
259+
),
260+
);
261+
take(wire, isNotices).respond(burst);
262+
await flush();
263+
expect(q.window("a").rows).toHaveLength(501);
264+
// The page stopped above the floor, so the next one continues from its
265+
// oldest notice by the relay's composite keyset.
266+
const last = [...burst]
267+
.sort((a, b) => b.created_at - a.created_at || a.id.localeCompare(b.id))
268+
.at(-1);
269+
const next = take(wire, isNotices);
270+
expect(next.filters[0]).toMatchObject({
271+
until: last?.created_at,
272+
before_id: last?.id,
273+
});
274+
next.fail(new Error("notice read failed"));
275+
await flush();
276+
expect(q.window("a")).toMatchObject({ status: "ready", error: undefined });
277+
expect(q.window("a").rows).toHaveLength(501);
278+
// The next older page retries the descent from where it stopped.
279+
q.loadOlder("a");
280+
const older = take(wire, isWindow);
281+
const first = message(viewer, "a", "First", 990);
282+
older.respond([
283+
first,
284+
bounds(relay, "a", `1000:${hello.id}`, {
285+
has_more: false,
286+
next_cursor: null,
287+
}),
288+
]);
289+
await flush();
290+
const retry = take(wire, isNotices);
291+
expect(retry.filters[0]).toMatchObject({
292+
until: last?.created_at,
293+
before_id: last?.id,
294+
});
295+
const early = addition(pinky.pubkey, 995);
296+
retry.respond([early]);
297+
await flush();
298+
expect(
299+
q
300+
.window("a")
301+
.rows.slice(0, 3)
302+
.map((r) => r.id),
303+
).toEqual([first.id, early.id, hello.id]);
304+
expect(wire.pending.some((entry) => entry.filters.some(isNotices))).toBe(
305+
false,
306+
);
307+
} finally {
308+
owner.dispose();
309+
}
310+
});
311+
193312
it("live activity obeys the same retained row budget as messages", async () => {
194313
const wire = scriptedTransport(viewer.pubkey, relay.pubkey);
195314
let live!: LiveCallbacks;
@@ -295,3 +414,88 @@ it("prepared disk heads retain signed membership evidence and subject profiles f
295414
second.dispose();
296415
}
297416
});
417+
418+
it("a catch-up head rereads the newest notices, and restarts the descent only when they do not meet the ones held", async () => {
419+
const wire = scriptedTransport(viewer.pubkey, relay.pubkey, {
420+
notices: true,
421+
});
422+
let live!: LiveCallbacks;
423+
const owner = createRelaySession(
424+
{
425+
...wire.transport,
426+
subscribe(callbacks) {
427+
live = callbacks;
428+
return { update() {}, retry() {}, dispose() {} };
429+
},
430+
},
431+
{ prepared: true },
432+
);
433+
const q = owner.session.channels;
434+
const hello = message(viewer, "a", "Hello", 1_000);
435+
const head = () =>
436+
take(wire, isWindow).respond([
437+
hello,
438+
bounds(relay, "a", "head", {
439+
has_more: true,
440+
next_cursor: { created_at: hello.created_at, id: hello.id },
441+
}),
442+
]);
443+
const noticeReads = () =>
444+
wire.pending.filter(
445+
(entry) => !entry.signal?.aborted && entry.filters.some(isNotices),
446+
);
447+
try {
448+
q.ensureList();
449+
wire
450+
.next()
451+
.respond([
452+
roster(relay, "a", [viewer.pubkey]),
453+
metadata(relay, "a", "A"),
454+
]);
455+
await flush();
456+
q.ensure("a");
457+
head();
458+
await flush();
459+
const held = addition(pinky.pubkey, 1_001);
460+
take(wire, isNotices).respond([held]);
461+
await flush();
462+
expect(q.window("a").rows.map((r) => r.id)).toEqual([hello.id, held.id]);
463+
expect(noticeReads()).toHaveLength(0);
464+
// A long drop: more joins than one page landed while the stream was down.
465+
live.state({ status: "connected", routes: [] });
466+
live.established("a");
467+
await flush();
468+
head();
469+
await flush();
470+
const missed = Array.from({ length: 500 }, (_, index) =>
471+
addition(keypair().pubkey, 1_100 + index),
472+
);
473+
const top = take(wire, isNotices);
474+
expect(top.filters[0]).not.toHaveProperty("until");
475+
top.respond(missed);
476+
await flush();
477+
const resumed = take(wire, isNotices);
478+
expect(resumed.filters[0]).toMatchObject({
479+
until: 1_100,
480+
before_id: missed[0]?.id,
481+
});
482+
resumed.respond([held]);
483+
await flush();
484+
expect(q.window("a").rows).toHaveLength(502);
485+
expect(noticeReads()).toHaveLength(0);
486+
// A head whose newest notices meet the ones held reads nothing more.
487+
live.state({ status: "retrying", routes: [] });
488+
await flush();
489+
live.state({ status: "connected", routes: [] });
490+
live.established("a");
491+
await flush();
492+
head();
493+
await flush();
494+
take(wire, isNotices).respond(missed);
495+
await flush();
496+
expect(noticeReads()).toHaveLength(0);
497+
expect(q.window("a").rows).toHaveLength(502);
498+
} finally {
499+
owner.dispose();
500+
}
501+
});

0 commit comments

Comments
 (0)