Skip to content

Commit c29a554

Browse files
tulsi-builderBasilBotclaude
committed
fix(inbox): renew discovery for verified restored channels; read old roots
- Discovery skips restored (cached or metadata-pending) channels. When one of them is verified, demanded discovery renews, the same way a follow change does. Joining a channel still does not renew it. - Exact root reads ignore the 30-day window, so an old root with a reply inside the window still groups as a thread (thread-scoped Mark unread). Co-authored-by: BasilBot <399581b6165ab01343e3a1324774b1024d76942aea563468f93c975e5ee5193c@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: tulsi <[email protected]>
1 parent 2aa2808 commit c29a554

5 files changed

Lines changed: 151 additions & 45 deletions

File tree

‎docs/inbox.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,11 @@ page as exhausted history. Explicit Retry can settle it once existing shared
163163
readers have admitted the missing reference; Inbox adds no reference-resolution loop.
164164
Thread discovery separately pages viewer-authored posts/replies and explicit
165165
follow choices in current, unarchived channels, then batches up to 100
166-
candidate roots across channels for targeted reply reads. Every discovery read,
167-
Solo included, stays within the last 30 days. Seeds stay outside unread retention until a thread has incoming
166+
candidate roots across channels for targeted reply reads. Every discovery walk,
167+
Solo included, stays within the last 30 days; the exact root read for an
168+
in-window reply does not, so an older root still groups its thread. Restored
169+
channels still awaiting verification are skipped, and demanded discovery
170+
renews once one is verified; joining a channel does not renew it. Seeds stay outside unread retention until a thread has incoming
168171
activity. The existing unread projection decides relevance and survival after
169172
edit/delete closure; a root leaves the pending query only once incoming evidence
170173
survives that fold. Self-only or deleted pages continue with the composite

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,61 @@ it("discovers participation in someone else's thread without a mention", async (
17151715
expect(rows(h).map((row) => row.messageId)).toContain(reply.id);
17161716
});
17171717

1718+
it("renews demanded discovery when a restored channel is verified", async () => {
1719+
const viewer = keypair().pubkey;
1720+
const listeners = new Set<() => void>();
1721+
let cached = true;
1722+
const room = () => ({
1723+
id: "room",
1724+
name: "Room",
1725+
members: [viewer],
1726+
...(cached ? { cached: true } : {}),
1727+
});
1728+
const channels = {
1729+
list: () => ({ channels: [room()] }),
1730+
subscribeList(listener: () => void) {
1731+
listeners.add(listener);
1732+
return () => listeners.delete(listener);
1733+
},
1734+
} as unknown as Parameters<typeof createInboxFeed>[0]["channels"];
1735+
const discovered: (readonly string[] | undefined)[] = [];
1736+
const feed = createInboxFeed({
1737+
viewer,
1738+
channels,
1739+
reader: { read: async () => [] },
1740+
addressedRead: async () => [],
1741+
retainedEvent: () => undefined,
1742+
retainedEditIds: () => [],
1743+
threadHistory: {
1744+
choices: () => new Map(),
1745+
incomingIds: () => [],
1746+
read: async (filter) => {
1747+
if (filter.authors) discovered.push(filter["#h"]);
1748+
return [];
1749+
},
1750+
admit: (events) => events,
1751+
},
1752+
});
1753+
try {
1754+
// The roster is still held: discovery completes without the room.
1755+
await feed.ensure();
1756+
expect(feed.snapshot().status).toBe("ready");
1757+
expect(discovered).toEqual([]);
1758+
// Releasing it verifies the room; no caller needs to refresh.
1759+
cached = false;
1760+
for (const listener of listeners) listener();
1761+
await vi.waitFor(() => expect(discovered).toEqual([["room"]]));
1762+
await vi.waitFor(() => expect(feed.snapshot().status).toBe("ready"));
1763+
// A later list change with nothing newly verified does not walk again.
1764+
for (const listener of listeners) listener();
1765+
await feed.ensure();
1766+
expect(discovered).toEqual([["room"]]);
1767+
} finally {
1768+
feed.dispose();
1769+
}
1770+
expect(listeners.size).toBe(0);
1771+
});
1772+
17181773
it("discovers an explicit follow added after the feed settled", async () => {
17191774
threadWindowNow();
17201775
vi.stubGlobal("localStorage", { getItem: () => null, setItem: vi.fn() });

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

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,21 @@ export function createInboxFeed({
164164
signal,
165165
);
166166
}
167+
/** Channels discovery may read: verified, current, unarchived membership. */
168+
const discoveryChannels = () =>
169+
channels
170+
.list()
171+
.channels.filter(
172+
(channel) =>
173+
!channel.huddle &&
174+
!channel.cached &&
175+
!channel.archived &&
176+
!channel.metadataPending &&
177+
channel.members?.includes(viewer),
178+
)
179+
.sort((a, b) => a.id.localeCompare(b.id));
180+
/** Restored channels the latest discovery skipped until verified. */
181+
let unverified: ReadonlySet<string> | undefined;
167182
async function refresh() {
168183
if (closed) return;
169184
if (work) return work;
@@ -207,34 +222,22 @@ export function createInboxFeed({
207222
await settle(targets, owned.signal);
208223
if (closed || generation !== epoch) return;
209224
publish({ incomplete: [] });
210-
if (threadHistory)
211-
await discoverInboxThreads({
212-
viewer,
213-
channelIds: channels
214-
.list()
215-
.channels.filter(
216-
(channel) =>
217-
!channel.huddle &&
218-
!channel.cached &&
219-
!channel.archived &&
220-
!channel.metadataPending &&
221-
channel.members?.includes(viewer),
222-
)
223-
.map((channel) => channel.id)
224-
.sort(),
225-
soloChannelIds: channels
225+
if (threadHistory) {
226+
const eligible = discoveryChannels();
227+
unverified = new Set(
228+
channels
226229
.list()
227230
.channels.filter(
228-
(channel) =>
229-
channel.channelType === "solo" &&
230-
!channel.huddle &&
231-
!channel.cached &&
232-
!channel.archived &&
233-
!channel.metadataPending &&
234-
channel.members?.includes(viewer),
231+
(channel) => channel.cached || channel.metadataPending,
235232
)
236-
.map((channel) => channel.id)
237-
.sort(),
233+
.map((channel) => channel.id),
234+
);
235+
await discoverInboxThreads({
236+
viewer,
237+
channelIds: eligible.map((channel) => channel.id),
238+
soloChannelIds: eligible
239+
.filter((channel) => channel.channelType === "solo")
240+
.map((channel) => channel.id),
238241
choices: threadHistory.choices(),
239242
since: Math.floor(Date.now() / 1000) - INBOX_THREAD_WINDOW_SECONDS,
240243
read: threadHistory.read,
@@ -264,6 +267,7 @@ export function createInboxFeed({
264267
return threadHistory.incomingIds(flatOnly);
265268
},
266269
});
270+
}
267271
if (closed || generation !== epoch) return;
268272
publish({
269273
status: "ready",
@@ -295,9 +299,38 @@ export function createInboxFeed({
295299
controller?.abort();
296300
controller = undefined;
297301
work = undefined;
302+
unverified = undefined;
298303
// Previously demanded data recovers explicitly or through reconnect.
299304
publish({ status: "idle", incomplete, error: undefined });
300305
}
306+
function stale() {
307+
epoch++;
308+
controller?.abort();
309+
controller = undefined;
310+
work = undefined;
311+
unverified = undefined;
312+
publish({
313+
status: "idle",
314+
incomplete: retainedIncomplete(),
315+
error: undefined,
316+
});
317+
}
318+
function reconnect() {
319+
if (requested) void refresh();
320+
}
321+
// A restored channel joins discovery once verified. Demanded discovery that
322+
// skipped it renews, as a follow change does. A newly joined channel does
323+
// not: its pre-membership history needs fresh shared admission instead.
324+
const stopChannels = threadHistory
325+
? channels.subscribeList(() => {
326+
const skipped = unverified;
327+
if (closed || !skipped?.size) return;
328+
if (discoveryChannels().some((channel) => skipped.has(channel.id))) {
329+
stale();
330+
reconnect();
331+
}
332+
})
333+
: undefined;
301334
const retainedIncomplete = () =>
302335
Object.freeze(snapshot.incomplete.filter((id) => retainedEvent(id)));
303336
return Object.freeze({
@@ -311,24 +344,13 @@ export function createInboxFeed({
311344
ensure: () =>
312345
work ?? (snapshot.status === "idle" ? refresh() : Promise.resolve()),
313346
refresh,
314-
stale() {
315-
epoch++;
316-
controller?.abort();
317-
controller = undefined;
318-
work = undefined;
319-
publish({
320-
status: "idle",
321-
incomplete: retainedIncomplete(),
322-
error: undefined,
323-
});
324-
},
325-
reconnect() {
326-
if (requested) void refresh();
327-
},
347+
stale,
348+
reconnect,
328349
clear: () => clear(),
329350
purge: () => clear(retainedIncomplete()),
330351
dispose() {
331352
closed = true;
353+
stopChannels?.();
332354
epoch++;
333355
controller?.abort();
334356
controller = undefined;

‎src/features/relay/inbox-thread-history.test.ts‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,32 @@ it("reads replies for many channels together and stays in the window", async ()
252252
expect(
253253
f.admit.mock.calls.flatMap(([events]) => events).map((event) => event.id),
254254
).toEqual(expect.arrayContaining([root.id, reply.id]));
255-
expect(f.read.mock.calls.every(([filter]) => filter.since === 100)).toBe(
256-
true,
257-
);
255+
// Every walk stays in the window; only exact root reads do not.
256+
expect(
257+
f.read.mock.calls.every(
258+
([filter]) => (filter.since === 100) === !filter.ids,
259+
),
260+
).toBe(true);
258261
expect(f.read.mock.calls.length).toBeLessThan(40);
259262
});
263+
264+
it("reads an out-of-window root for an in-window reply", async () => {
265+
const viewer = keypair(),
266+
other = keypair();
267+
const root = message(other, "room", "old root", 40);
268+
const tags = [["e", root.id, "", "reply"]];
269+
const mine = message(viewer, "room", "recent participation", 150, tags);
270+
const reply = message(other, "room", "reply today", 200, tags);
271+
const f = fixture([root, mine, reply]);
272+
await discoverInboxThreads({
273+
...f,
274+
viewer: viewer.pubkey,
275+
channelIds: ["room"],
276+
choices: new Map(),
277+
since: 100,
278+
signal: new AbortController().signal,
279+
});
280+
expect(
281+
f.admit.mock.calls.flatMap(([events]) => events).map((event) => event.id),
282+
).toEqual(expect.arrayContaining([root.id, reply.id]));
283+
});

‎src/features/relay/inbox-thread-history.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ async function walk({
6868
const page = async (filter: ReadFilter, cursor?: Cursor) => {
6969
signal.throwIfAborted();
7070
if (++requests > 256) throw limited();
71-
const window = since === undefined ? {} : { since };
71+
// The window bounds discovery walks; an exact root read is not a walk, and
72+
// an old root with an in-window reply must still arrive.
73+
const window = since === undefined || filter.ids ? {} : { since };
7274
const rows = [
7375
...(await read({ ...filter, ...window, ...cursor }, signal)),
7476
].sort((a, b) => b.created_at - a.created_at || a.id.localeCompare(b.id));

0 commit comments

Comments
 (0)