Skip to content

Commit a7151b0

Browse files
loganjLarryclaude
committed
fix(relay): keep the rows held at a grant for its catch-up
The browser broker writes live events at once but can hold an established report under backpressure, so a live row from a grant's re-sent batch can reach the window before that batch's report. The report-time snapshot then held a row newer than the gap the relay left while access was gone, and the catch-up stopped above it. The session now tells the store when a 44100 grant for a channel arrives, before anything the re-sent batch delivers. The window keeps the rows it holds then; a continuous report does not resume it until a catch-up takes them, and the next catch-up (or the next drop's) pages back to them. A report with no grant since still takes its current rows, so a routine re-send after a continuous resume reads only the head. The stale-thread test now returns the dropped reply and asserts it. Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Logan Johnson <[email protected]>
1 parent 00890d5 commit a7151b0

4 files changed

Lines changed: 153 additions & 38 deletions

File tree

‎docs/relay-queries.md‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,12 +1044,16 @@ When access ends, the relay leaves the channel out of its batch with no frame,
10441044
and the channel's cursor stays. A grant that restores access may reach the
10451045
broker after the batch's EOSE, for example in the membership replay of the
10461046
same reconnect, so the batch can first report that channel continuous. The grant then re-sends the batch, whose EOSE reports it
1047-
again with nothing continuous. A report with nothing continuous makes its
1048-
catch-up page back to the rows the window holds when the report arrives, unless
1049-
a drop kept older ones: while the relay left the channel out, the stream
1050-
delivered nothing for it, so those are the rows of the drop, and rows it
1051-
delivers after the report are newer than the gap. After a resume that was
1052-
continuous, a later re-send of the batch therefore reads only the head.
1047+
again with nothing continuous. The grant reaches the session before anything
1048+
the re-sent batch delivers, and before any report the browser broker holds back
1049+
under backpressure (it writes live events at once). The window keeps the rows it
1050+
holds when the grant arrives: while the relay left the channel out, the stream
1051+
delivered nothing for it and a read was denied. Until a catch-up takes those
1052+
rows, a continuous report does not resume the window, and
1053+
the next catch-up (or the next drop's) pages back to them, past the rows the
1054+
re-sent batch delivered. A report with nothing continuous and no grant since
1055+
pages back to the rows held when it arrives, so a routine re-send after a
1056+
continuous resume reads only the head.
10531057

10541058
A retained window that was verified when the stream dropped, and a thread view
10551059
that was settled then, each holding rows from a read started at most 15 minutes

‎src/features/relay/live-session.test.ts‎

Lines changed: 93 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,31 @@ function reconnectHarness(
520520
live.established("c");
521521
await flush();
522522
};
523+
/** The relay's grant notice for `channelId`, delivered live. Any roster
524+
* check it schedules is answered with c's roster. */
525+
const grant = async (channelId = "c") => {
526+
live.receive([
527+
signed(relay, {
528+
kind: 44100,
529+
content: "",
530+
created_at: 55,
531+
tags: [
532+
["p", viewer.pubkey],
533+
["h", channelId],
534+
],
535+
}),
536+
]);
537+
await flush();
538+
for (const entry of wire.pending.splice(0))
539+
if (entry.filters[0]?.top_level === true) wire.pending.push(entry);
540+
else
541+
entry.respond(
542+
entry.filters[0]?.kinds?.includes(0)
543+
? []
544+
: [roster(relay, "c", [viewer.pubkey]), metadata(relay, "c", "C")],
545+
);
546+
await flush();
547+
};
523548
/** Drop the stream, with no reconnect yet. */
524549
const drop = async () => {
525550
live.state({ status: "retrying", routes: [] });
@@ -556,6 +581,7 @@ function reconnectHarness(
556581
list,
557582
open,
558583
reconnect,
584+
grant,
559585
drop,
560586
connectAndReplay,
561587
dropAndReplay,
@@ -1862,20 +1888,22 @@ describe("a reconnect whose replay was continuous", () => {
18621888
}
18631889
});
18641890

1865-
it("catches up from the rows held at the report when a resumed channel is reported again with nothing continuous", async () => {
1891+
it("catches up from the rows held at a grant when a resumed channel is reported again with nothing continuous", async () => {
18661892
const h = reconnectHarness();
18671893
try {
18681894
const seen = await verifiedThenDropped(h);
18691895
await resume(h, [], true);
18701896
expect(h.reads()).toHaveLength(0);
1871-
// A grant the broker learned of after the replay: the relay may have
1872-
// left the channel out, so the stream delivered nothing for it since.
1873-
h.live.established("c");
1874-
await flush();
1875-
// A live event after the re-sent batch is live, before the catch-up reads.
1897+
// Access ended during the outage, so the relay left c out of the batch,
1898+
// and the stream has delivered nothing for it since. A grant re-sends it.
1899+
await h.grant();
1900+
expect(h.reads()).toHaveLength(0);
1901+
// The re-sent batch delivers a live event before its report.
18761902
const fresh = message(h.alice, "c", "fresh", 60);
18771903
h.live.receive([fresh]);
18781904
await flush();
1905+
h.live.established("c");
1906+
await flush();
18791907
const hidden = message(h.alice, "c", "hidden", 30),
18801908
newer = message(h.alice, "c", "newer", 50);
18811909
// The head holds the live row; the gap lies below it, above `seen`.
@@ -1892,21 +1920,58 @@ describe("a reconnect whose replay was continuous", () => {
18921920
}
18931921
});
18941922

1895-
it("keeps the rows held at the report when the stream drops again before a retracted resume catches up", async () => {
1923+
it("catches up from the drop's rows when a delayed continuous report arrives after a grant and its traffic", async () => {
1924+
// The browser broker writes live events at once but holds a report under
1925+
// backpressure: the grant and the re-sent batch's first event reach the
1926+
// session before the report of the replay that left c out.
18961927
const h = reconnectHarness();
18971928
try {
18981929
const seen = await verifiedThenDropped(h);
1899-
await resume(h, [], true);
1930+
h.live.state({ status: "connected", routes: [] });
1931+
await flush();
1932+
await h.grant();
1933+
const fresh = message(h.alice, "c", "fresh", 60);
1934+
h.live.receive([fresh]);
1935+
await flush();
1936+
h.live.established("c", undefined, ["c"]);
1937+
await flush();
1938+
expect(h.window().freshness).toBe("cached");
1939+
const hidden = message(h.alice, "c", "hidden", 30),
1940+
newer = message(h.alice, "c", "newer", 50);
1941+
h.read().respond(h.page("head", [fresh, newer], newer));
1942+
await flush();
1943+
const older = h.read();
1944+
expect(older.filters[0]).toMatchObject({ before_id: newer.id });
1945+
older.respond(h.page(h.cursor(newer), [hidden, seen], seen));
1946+
await flush();
1947+
expect(h.rows()).toEqual([seen.id, hidden.id, newer.id, fresh.id]);
1948+
expect(h.window().freshness).toBe("verified");
1949+
// The re-sent batch's own report then needs only the head.
19001950
h.live.established("c");
19011951
await flush();
1952+
h.read().respond(h.page("head", [fresh, newer], newer));
1953+
await flush();
1954+
expect(h.reads()).toHaveLength(0);
1955+
expect(h.window().freshness).toBe("verified");
1956+
} finally {
1957+
h.owner.dispose();
1958+
}
1959+
});
1960+
1961+
it("keeps the rows held at a grant when the stream drops again before its catch-up", async () => {
1962+
const h = reconnectHarness();
1963+
try {
1964+
const seen = await verifiedThenDropped(h);
1965+
await resume(h, [], true);
1966+
await h.grant();
19021967
const fresh = message(h.alice, "c", "fresh", 60);
19031968
h.live.receive([fresh]);
19041969
await flush();
1905-
// The retraction's head is still outstanding when the stream drops again.
1906-
h.read();
1970+
// The stream drops before the re-sent batch reports.
19071971
await h.drop();
19081972
const later = message(h.alice, "c", "later", 70);
1909-
await resume(h, [later], false);
1973+
await resume(h, [later], true);
1974+
expect(h.window().freshness).toBe("cached");
19101975
const hidden = message(h.alice, "c", "hidden", 30),
19111976
newer = message(h.alice, "c", "newer", 50);
19121977
h.read().respond(h.page("head", [later, fresh, newer], newer));
@@ -2212,15 +2277,20 @@ describe("a reconnect whose replay was continuous", () => {
22122277
await verifiedThenDropped(h);
22132278
await resume(h, [], true);
22142279
const thread = h.owner.session.thread("c", root.id);
2280+
/** Replies the relay holds for the root. */
2281+
let replies: RelayEvent[] = [];
22152282
const answer = async () => {
22162283
for (let quiet = 0; quiet < 3; ) {
22172284
await flush();
22182285
if (!h.wire.pending.length) quiet++;
22192286
else quiet = 0;
22202287
for (const entry of h.wire.pending.splice(0))
2221-
entry.respond(
2222-
entry.filters[0]?.ids?.includes(root.id) ? [root] : [],
2223-
);
2288+
entry.respond([
2289+
...(entry.filters[0]?.ids?.includes(root.id) ? [root] : []),
2290+
...(entry.filters.some((f) => f["#e"]?.includes(root.id))
2291+
? replies
2292+
: []),
2293+
]);
22242294
}
22252295
};
22262296
const threadReads = () =>
@@ -2232,19 +2302,28 @@ describe("a reconnect whose replay was continuous", () => {
22322302
void thread.refresh();
22332303
await answer();
22342304
expect(thread.snapshot().status).toBe("ready");
2305+
// A reply the live stream dropped before the replay window.
2306+
const reply = message(h.alice, "c", "reply", 20, [
2307+
["e", root.id, "", "reply"],
2308+
]);
2309+
replies = [reply];
22352310
clock += 15 * 60_000;
22362311
await h.drop();
22372312
h.queries.length = 0;
22382313
await resume(h, [], true);
22392314
await answer();
22402315
expect(threadReads()).toEqual([]);
2316+
expect(thread.snapshot().replies).toEqual([]);
22412317
clock += 1;
22422318
await h.drop();
22432319
h.queries.length = 0;
22442320
await resume(h, [], true);
22452321
await answer();
22462322
expect(threadReads().length).toBeGreaterThan(0);
22472323
expect(thread.snapshot().status).toBe("ready");
2324+
expect(thread.snapshot().replies.map((row) => row.id)).toEqual([
2325+
reply.id,
2326+
]);
22482327
thread.dispose();
22492328
} finally {
22502329
h.owner.dispose();

‎src/features/relay/session.ts‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3592,16 +3592,20 @@ export function createRelaySession(
35923592
);
35933593
// Signed membership notifications are hints, not roster authority. Schedule
35943594
// before visibility filtering, because a newly granted channel may be denied locally.
3595-
confirmMembershipHints(
3596-
events.filter(
3597-
(event) =>
3598-
[44100, 44101].includes(event.kind) &&
3599-
event.pubkey === transport.relayAuthor &&
3600-
event.tags.some(
3601-
([name, value]) => name === "p" && value === transport.viewer,
3602-
),
3603-
),
3595+
const hints = events.filter(
3596+
(event) =>
3597+
[44100, 44101].includes(event.kind) &&
3598+
event.pubkey === transport.relayAuthor &&
3599+
event.tags.some(
3600+
([name, value]) => name === "p" && value === transport.viewer,
3601+
),
36043602
);
3603+
confirmMembershipHints(hints);
3604+
// Before anything the batch it re-sends delivers (see `granted`).
3605+
for (const hint of hints)
3606+
if (hint.kind === 44100)
3607+
for (const [name, id] of hint.tags)
3608+
if (name === "h" && id) channels.granted(id);
36053609
const epoch = accessEpoch;
36063610
const generation = liveGeneration;
36073611
const visible = accept(

‎src/features/relay/store.ts‎

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ type WindowState = {
9292
* the live stream then. A continuous replay of the channel closes the gap
9393
* from them, so `resume` needs no read. */
9494
keptLive?: boolean | undefined;
95+
/** The rows held when a grant for the channel reached the session. The
96+
* relay may have left the channel out of the stream until that grant, with
97+
* no notice, and a read meanwhile is denied, so these all precede any gap:
98+
* the next catch-up must reach them. Rows the re-sent batch delivers before
99+
* its report are newer than the gap. */
100+
grantKept?: Set<string> | undefined;
95101
/** When the head read whose rows the window holds started. A head read the
96102
* window never took (a prefetch, a cancelled catch-up) does not count. */
97103
readAt?: number | undefined;
@@ -739,6 +745,7 @@ export function createChannelStore(
739745
state.kept = rowIds(state);
740746
state.keptLive = false;
741747
}
748+
if (state.grantKept) state.grantKept = rowIds(state);
742749
setWindow(state, patchFromHead(head));
743750
void fillNotices(state, true);
744751
}
@@ -1055,6 +1062,7 @@ export function createChannelStore(
10551062
state.kept = rowIds(state);
10561063
state.keptLive = false;
10571064
}
1065+
if (state.grantKept && !cursor) state.grantKept = rowIds(state);
10581066
setWindow(state, {
10591067
status: "ready",
10601068
rows,
@@ -1145,6 +1153,7 @@ export function createChannelStore(
11451153
state.ceiling = ceiling ?? undefined;
11461154
state.floor = undefined;
11471155
state.kept = undefined;
1156+
state.grantKept = undefined;
11481157
state.restored = false;
11491158
moveCursor(state, page.cursor);
11501159
setWindow(state, {
@@ -4152,7 +4161,11 @@ export function createChannelStore(
41524161
for (const [id, head] of heads.entries())
41534162
heads.set(id, { ...head, cached: true });
41544163
for (const state of windows.values()) {
4155-
if (!state.kept) {
4164+
if (state.grantKept) {
4165+
state.kept ??= state.grantKept;
4166+
state.keptLive = false;
4167+
state.grantKept = undefined;
4168+
} else if (!state.kept) {
41564169
state.kept = rowIds(state);
41574170
state.keptLive =
41584171
state.snapshot.freshness === "verified" &&
@@ -4311,7 +4324,13 @@ export function createChannelStore(
43114324
* read. Any other window returns false and catches up. */
43124325
resume(channelId: string): boolean {
43134326
const state = windows.get(channelId);
4314-
if (disposed || !state?.keptLive || !authorized(channelId)) return false;
4327+
if (
4328+
disposed ||
4329+
!state?.keptLive ||
4330+
state.grantKept ||
4331+
!authorized(channelId)
4332+
)
4333+
return false;
43154334
dropped = false;
43164335
const head = heads.peek(channelId);
43174336
if (head) heads.set(channelId, { ...head, cached: true });
@@ -4323,6 +4342,16 @@ export function createChannelStore(
43234342
void fillNotices(state, false);
43244343
return true;
43254344
},
4345+
/** A grant for the channel reached the session. The relay may have left
4346+
* the channel out of the stream until now, so a continuous replay before it
4347+
* proves nothing: the next catch-up reaches the rows held now. The grant
4348+
* reaches the session before the batch it re-sends can deliver anything,
4349+
* and before that batch's report. */
4350+
granted(channelId: string) {
4351+
const state = windows.get(channelId);
4352+
if (disposed || !state) return;
4353+
state.grantKept ??= rowIds(state);
4354+
},
43264355
retainedChannels: () => [...windows.keys()],
43274356
retainedEvent(id: string) {
43284357
for (const state of windows.values()) {
@@ -4354,14 +4383,13 @@ export function createChannelStore(
43544383
const state = windows.get(channelId);
43554384
if (state) {
43564385
// This establishment's replay may stop short of the kept rows. A window
4357-
// with none (resumed with no read, or not dropped) takes the rows it
4358-
// holds now: they reached the stream, and rows it delivers from here on
4359-
// are newer than any gap. While the relay leaves a channel out (access
4360-
// ended, and the grant reached the broker after its replay), the stream
4361-
// delivers none for it, so these are still the rows of the drop. A drop
4362-
// before the catch-up settles keeps them.
4363-
state.kept ??= rowIds(state);
4386+
// with none (resumed with no read, or not dropped) takes the rows held
4387+
// at a grant since, or else the rows it holds now: they reached the
4388+
// stream, and rows it delivers from here on are newer than any gap. A
4389+
// drop before the catch-up settles keeps them.
4390+
state.kept ??= state.grantKept ?? rowIds(state);
43644391
state.keptLive = false;
4392+
state.grantKept = undefined;
43654393
setWindow(state, {
43664394
freshness: "cached",
43674395
error: undefined,

0 commit comments

Comments
 (0)