fix(desktop): split channel history fetch by kind so limit buys visible depth

The GUI history filter requested the broad CHANNEL_EVENT_KINDS set (reactions,
edits, deletions alongside messages), so a 200-event "limit" budget bought only
visible-message depth diluted by aux events — measured 136 messages / 200 events
on a reaction-heavy channel, deterministic across runs. Scrollback inherited the
same dilution, and same-second events could shuffle position when merged from
two REQs (history vs live-sub).

Fix, per the invariant converged on in #buzz-bugs:
1. Base history filter = visible content kinds only (CHANNEL_TIMELINE_CONTENT_KINDS,
   mirroring isTimelineContentEvent), so the limit budget maps to visible rows.
2. Backfill reactions/edits/deletions by `#e` over the loaded message ids — by
   reference, not by time window, so a late edit/delete for a visible old message
   still applies (avoids the silent-stale-state trap).
3. Keep the broad-kind live subscription for future aux events; cold/scrollback
   correctness comes from the #e backfill, not the tail.
4. Sort GUI merges by (created_at, id) so same-second merges are deterministic —
   both the cache sort (sortMessages) and the history-resolve sort (sortEvents)
   carry the id tiebreaker, keeping the two on one invariant.

Extracted the channel filter-builders into relayChannelFilters.ts to keep
relayClientSession.ts under its size cap. Aux backfill lives in its own module
(auxBackfill.ts) to avoid coupling useFetchOlderMessages to hooks.ts.

Regression tests prove the reducer applies aux by `#e` reference regardless of
timestamp (far-future edit rewrites an old body; far-future deletion hides an
old message), plus sort order-independence for both sort functions.

Co-authored-by: Tyler Longwell <tlongwell@block.xyz>
Signed-off-by: Tyler Longwell <tlongwell@block.xyz>
This commit is contained in:
npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d
2026-06-19 19:54:39 -04:00
parent c488d8452c
commit 022edf3fc4
13 changed files with 457 additions and 54 deletions

View File

@@ -7,8 +7,6 @@ import {
} from "@/shared/api/tauri";
import type { PresenceStatus, RelayEvent } from "@/shared/api/types";
import {
CHANNEL_EVENT_KINDS,
HOME_MENTION_EVENT_KINDS,
KIND_STREAM_MESSAGE,
KIND_TYPING_INDICATOR,
KIND_USER_STATUS,
@@ -21,6 +19,14 @@ import {
type RelaySubscription,
type RelaySubscriptionFilter,
} from "@/shared/api/relayClientShared";
import {
AUX_BACKFILL_CHUNK_SIZE,
buildChannelAuxFilter,
buildChannelFilter,
buildChannelHistoryFilter,
buildChannelMentionFilter,
buildGlobalStreamFilter,
} from "@/shared/api/relayChannelFilters";
import { replayLiveSubscriptions } from "@/shared/api/relayReconnectReplay";
import { RelayConnectionStateEmitter } from "@/shared/api/relayConnectionStateEmitter";
import {
@@ -148,7 +154,7 @@ export class RelayClient {
}
async fetchChannelHistory(channelId: string, limit = 50) {
return this.fetchHistory(this.buildChannelFilter(channelId, limit));
return this.fetchHistory(buildChannelHistoryFilter(channelId, limit));
}
async fetchChannelHistoryBefore(
@@ -156,7 +162,48 @@ export class RelayClient {
before: number,
limit = 50,
) {
return this.fetchHistory(this.buildChannelFilter(channelId, limit, before));
return this.fetchHistory(
buildChannelHistoryFilter(channelId, limit, before),
);
}
/**
* Fetch the auxiliary events (reactions, edits, diffs, deletions) that
* reference a set of already-loaded message ids, keyed by `#e` rather than a
* time window.
*
* History fetches deliberately request message kinds only, so the `limit`
* budget buys visible message depth instead of being diluted by aux events
* (on a reaction-heavy channel a 200-event window was only ~136 messages).
* The trade-off is that an edit/deletion for a visible message can fall
* outside any message time window — so we must pull aux by reference, not by
* time, or a visible message would render stale (un-edited / not-deleted).
*
* Batched: `#e` filters can grow large, so message ids are chunked to keep
* each REQ within relay filter limits. Results across chunks are merged.
*/
async fetchAuxEventsForMessages(
channelId: string,
messageIds: string[],
): Promise<RelayEvent[]> {
if (messageIds.length === 0) {
return [];
}
await this.ensureConnected();
const chunks: string[][] = [];
for (let i = 0; i < messageIds.length; i += AUX_BACKFILL_CHUNK_SIZE) {
chunks.push(messageIds.slice(i, i + AUX_BACKFILL_CHUNK_SIZE));
}
const batches = await Promise.all(
chunks.map((ids) =>
this.requestHistory(buildChannelAuxFilter(channelId, ids)),
),
);
return batches.flat();
}
async fetchEvents(filter: RelaySubscriptionFilter): Promise<RelayEvent[]> {
@@ -269,7 +316,7 @@ export class RelayClient {
channelId: string,
onEvent: (event: RelayEvent) => void,
) {
return this.subscribe(this.buildChannelFilter(channelId, 50), onEvent);
return this.subscribe(buildChannelFilter(channelId, 50), onEvent);
}
/**
@@ -357,7 +404,7 @@ export class RelayClient {
}
async subscribeToAllStreamMessages(onEvent: (event: RelayEvent) => void) {
return this.subscribe(this.buildGlobalStreamFilter(50), onEvent);
return this.subscribe(buildGlobalStreamFilter(50), onEvent);
}
async subscribeLive(
@@ -373,7 +420,7 @@ export class RelayClient {
onEvent: (event: RelayEvent) => void,
) {
return this.subscribe(
this.buildChannelMentionFilter(channelId, pubkey, 50),
buildChannelMentionFilter(channelId, pubkey, 50),
onEvent,
);
}
@@ -487,45 +534,6 @@ export class RelayClient {
this.emitReconnectIfNeeded();
}
private buildChannelFilter(
channelId: string,
limit: number,
until?: number,
): RelaySubscriptionFilter {
const filter: RelaySubscriptionFilter = {
kinds: [...CHANNEL_EVENT_KINDS],
"#h": [channelId],
limit,
};
if (until !== undefined) {
filter.until = until;
}
return filter;
}
private buildGlobalStreamFilter(limit: number): RelaySubscriptionFilter {
return {
kinds: [...CHANNEL_EVENT_KINDS],
limit,
};
}
private buildChannelMentionFilter(
channelId: string,
pubkey: string,
limit: number,
): RelaySubscriptionFilter {
return {
kinds: [...HOME_MENTION_EVENT_KINDS],
"#h": [channelId],
"#p": [pubkey],
limit,
since: Math.floor(Date.now() / 1_000),
};
}
private async subscribe(
filter: RelaySubscriptionFilter,
onEvent: (event: RelayEvent) => void,