Skip to content

Commit ee446d1

Browse files
bostonaholicmboston Implementerclaude
committed
fix(inbox): keep owned reactions through archive and learn authors from received messages
Retaining only displayed rows dropped the viewer's reactions in a channel archived during a refresh; a restore then recovered only the 50 the history read returns. Retain every candidate except those on messages verified as someone else's. Channel messages reach receive() in the same batches as reactions, so their authors now mark foreign targets without a read. Co-authored-by: mboston Implementer <932d3b6ebea6cc83e091551ae46d1c7e7f8f7d0fa8e9cef5e6facc8544af413b@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Matthew Boston <[email protected]>
1 parent 82bdb72 commit ee446d1

4 files changed

Lines changed: 53 additions & 11 deletions

File tree

‎docs/inbox.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,10 @@ relay's `#p: [viewer]` index, which lists untagged reactions only where the rela
268268
derived the target's author, and retains up to 100 candidates, including live
269269
arrivals. A candidate whose target is not the viewer's message is dropped after one
270270
check, and later reactions to that target cost no reads (up to 1,000 remembered
271-
targets). Targets that could not be read are checked again each time. Exact target ownership, current channel eligibility and edit/deletion closure
271+
targets). Messages the client receives in channel batches also mark their authors,
272+
so a reaction to another member's message seen there costs no read. Targets that
273+
could not be read are checked again each time, and candidates on the viewer's
274+
messages stay retained while their channel is archived or unavailable. Exact target ownership, current channel eligibility and edit/deletion closure
272275
are checked before displaying any preview.
273276
Incomplete settlement withholds rows and offers retry rather than showing stale text.
274277

‎docs/relay-queries.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,9 @@ reactions to the viewer's messages. Live reactions are candidates when they carr
12741274
`p` tag or a `p` tag for the viewer; a reaction whose `p` tags name only others costs
12751275
no read. Candidates whose target is not the viewer's message are not retained, and
12761276
up to 1,000 such verified targets are remembered so their later reactions cost no
1277-
read. Unreadable targets are never remembered.
1277+
read; messages received in channel batches are remembered the same way.
1278+
Unreadable targets are never remembered, and candidates on the viewer's messages
1279+
are retained while their channel is unavailable.
12781280
It retains at most 100 candidates, resolves exact message IDs, and reads paginated
12791281
edit/deletion closure using `(until, before_id)`. Closure reads cap at 2,000 events
12801282
or 4 MiB; combined evidence caps at 8 MiB. Incomplete or inaccessible overlay

‎src/features/relay/inbox-reactions.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,38 @@ it("keeps checking a target it could not read, so access can return", async () =
314314
expect(fetchesMissing()).toBe(2);
315315
});
316316

317+
it("reads no target for reactions to other people's messages it already received", async () => {
318+
const h = setup();
319+
await h.feed.ensure();
320+
const others = Array.from({ length: 50 }, (_, index) =>
321+
message(h.bob, "room", `Bob ${index}`, 30 + index),
322+
);
323+
h.emit(others);
324+
const reads = h.query.mock.calls.length;
325+
for (const [index, other] of others.entries()) {
326+
h.emit([h.untagged(h.alice, 100 + index, other.id)]);
327+
await h.feed.ensure();
328+
}
329+
expect(h.query.mock.calls.length).toBe(reads);
330+
});
331+
332+
it("keeps the viewer's reactions in a channel archived during a refresh", async () => {
333+
const h = setup();
334+
const mine = Array.from({ length: 60 }, (_, index) =>
335+
message(h.viewer, "room", `Mine ${index}`, 11 + index),
336+
);
337+
h.events.push(...mine);
338+
for (const [index, own] of mine.entries())
339+
h.emit([h.react(h.alice, 100 + index, own.id)]);
340+
await h.feed.ensure();
341+
expect(h.feed.snapshot().items).toHaveLength(60);
342+
h.archive(true);
343+
await h.feed.refresh();
344+
h.archive(false);
345+
await h.feed.ensure();
346+
expect(h.feed.snapshot().items).toHaveLength(60);
347+
});
348+
317349
it.each(["live", "history"] as const)(
318350
"drops a forged viewer p tag on someone else's message (%s)",
319351
async (path) => {

‎src/features/relay/inbox-reactions.ts‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ export function createInboxReactions({
6767
// else's is never fetched again. Unread targets are not remembered: access
6868
// to them can return.
6969
const foreign = new Set<string>();
70+
const rememberForeign = (event: RelayEvent) => {
71+
if (messageAuthor(event, signingAuthority) === viewer) return;
72+
foreign.add(event.id);
73+
const oldest = foreign.values().next().value;
74+
if (foreign.size > 1000 && oldest) foreign.delete(oldest);
75+
};
7076
// Every channel reaction reaches receive(), so only untagged reactions and
7177
// those tagging the viewer may cost a target fetch.
7278
const eligible = (event: RelayEvent) => {
@@ -155,14 +161,7 @@ export function createInboxReactions({
155161
: [];
156162
signal.throwIfAborted();
157163
for (const event of targets)
158-
if (
159-
ids.includes(event.id) &&
160-
messageAuthor(event, signingAuthority) !== viewer
161-
) {
162-
foreign.add(event.id);
163-
const oldest = foreign.values().next().value;
164-
if (foreign.size > 1000 && oldest) foreign.delete(oldest);
165-
}
164+
if (ids.includes(event.id)) rememberForeign(event);
166165
// Missing, foreign and revoked targets never establish a reaction row.
167166
const owned = targets.filter(
168167
(event) =>
@@ -256,7 +255,9 @@ export function createInboxReactions({
256255
}
257256
items.sort((a, b) => b.createdAt - a.createdAt || a.id.localeCompare(b.id));
258257
known = new Set(evidence.map((event) => event.id));
259-
retained = relevant;
258+
// Keep owned and unresolved targets through archive or access loss, so a
259+
// restore recovers them beyond the 50 the history read returns.
260+
retained = reactions.filter((event) => !foreign.has(targetOf(event) ?? ""));
260261
for (const reaction of reactions) pending.delete(reaction.id);
261262
publish({ status: "ready", items: Object.freeze(items) });
262263
signal.throwIfAborted();
@@ -361,6 +362,10 @@ export function createInboxReactions({
361362
receive(events: readonly RelayEvent[], phase?: "live" | "replay") {
362363
if (closed) return;
363364
let changed = false;
365+
// Channel messages arrive in these same batches; learning their authors
366+
// here spares a target read for reactions to them.
367+
for (const event of events)
368+
if ([9, 40002, 40008].includes(event.kind)) rememberForeign(event);
364369
const ids = new Set([
365370
...known,
366371
...pending.keys(),

0 commit comments

Comments
 (0)