Skip to content

Commit 00e61ea

Browse files
authored
fix(desktop): surface channel history load failures (#7013)
## Summary A failed initial channel-history request no longer appears as an authoritative empty channel. The timeline now shows an announced error with a Retry action, while cached messages remain visible when a later refresh fails; successful empty channels continue to use their normal intro state. ### Related issue None found. ### Testing - Full desktop unit suite (`pnpm test`) - Desktop TypeScript check (`pnpm exec tsc --noEmit`) - Biome checks for changed files - Repository file-size ratchet - Full pre-push desktop checks and tests - Desktop app launched successfully against local Postgres and Redis for manual testing No screenshot is included because the new UI is only shown after a terminal relay-history failure; the regression test pins the error/empty/list precedence directly. --------- Signed-off-by: Thomas Petersen <[email protected]>
1 parent 8dbc65d commit 00e61ea

12 files changed

Lines changed: 350 additions & 51 deletions

‎desktop/src/features/channels/ui/ChannelPane.helpers.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getChannelDetail } from "@/features/channels/lib/channelDescription";
22
import { isEphemeralChannel } from "@/features/channels/lib/ephemeralChannel";
33
import type { TimelineMessage } from "@/features/messages/types";
4+
import type { TypingIndicatorEntry } from "@/features/messages/useChannelTyping";
45
import type { Channel } from "@/shared/api/types";
56
import { KIND_SYSTEM_MESSAGE } from "@/shared/constants/kinds";
67

@@ -106,3 +107,19 @@ export function mentionsKnownAgent(
106107
knownAgentPubkeys.has(pubkey.toLowerCase()),
107108
);
108109
}
110+
111+
export function selectThreadComposerBotTypingPubkeys(
112+
entries: TypingIndicatorEntry[],
113+
threadHeadId: string | null,
114+
) {
115+
if (!threadHeadId) return [];
116+
return entries
117+
.filter((entry) => entry.threadHeadId === threadHeadId)
118+
.map((entry) => entry.pubkey)
119+
.filter(
120+
(pubkey, index, all) =>
121+
all.findIndex(
122+
(candidate) => candidate.toLowerCase() === pubkey.toLowerCase(),
123+
) === index,
124+
);
125+
}

‎desktop/src/features/channels/ui/ChannelPane.tsx‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import {
5252
import { useWelcomeComposerBanner } from "@/features/channels/ui/useWelcomeComposerBanner";
5353
import {
5454
mentionsKnownAgent,
55+
selectThreadComposerBotTypingPubkeys,
5556
shouldPrioritizeIdleAuxiliary,
5657
shouldUseFocusIdleDrawer,
5758
} from "@/features/channels/ui/ChannelPane.helpers";
@@ -103,7 +104,9 @@ export const ChannelPane = React.memo(function ChannelPane({
103104
isJoining = false,
104105
isSinglePanelView = false,
105106
isSending,
107+
isTimelineError = false,
106108
isTimelineLoading,
109+
onRetryTimeline,
107110
entranceMessageId = null,
108111
onEntranceMessageComplete,
109112
welcomeKickoffStage = null,
@@ -334,18 +337,11 @@ export const ChannelPane = React.memo(function ChannelPane({
334337
const hasCardMintActivity = useCardMintJobs().length > 0;
335338
const hasComposerBottomActivity =
336339
hasComposerBotActivity || hasTypingActivity || hasCardMintActivity;
337-
const threadComposerBotTypingPubkeys = React.useMemo(() => {
338-
if (!openThreadHeadId) return [];
339-
return botTypingEntries
340-
.filter((entry) => entry.threadHeadId === openThreadHeadId)
341-
.map((entry) => entry.pubkey)
342-
.filter(
343-
(pubkey, index, all) =>
344-
all.findIndex(
345-
(candidate) => candidate.toLowerCase() === pubkey.toLowerCase(),
346-
) === index,
347-
);
348-
}, [botTypingEntries, openThreadHeadId]);
340+
const threadComposerBotTypingPubkeys = React.useMemo(
341+
() =>
342+
selectThreadComposerBotTypingPubkeys(botTypingEntries, openThreadHeadId),
343+
[botTypingEntries, openThreadHeadId],
344+
);
349345
const hasThreadComposerBotActivity =
350346
threadComposerBotTypingPubkeys.length > 0;
351347
const directMessageIntro = React.useMemo(
@@ -662,7 +658,9 @@ export const ChannelPane = React.memo(function ChannelPane({
662658
: "No messages yet"
663659
: "No channel selected"
664660
}
661+
isError={isTimelineError}
665662
isLoading={isHuddleTranscript ? false : isTimelineLoading}
663+
onRetry={onRetryTimeline}
666664
entranceMessageId={entranceMessageId}
667665
onEntranceMessageComplete={onEntranceMessageComplete}
668666
mainEntries={mainTimelineEntries}

‎desktop/src/features/channels/ui/ChannelPane.types.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ export type ChannelPaneProps = {
5959
isJoining?: boolean;
6060
isSinglePanelView?: boolean;
6161
isSending: boolean;
62+
/** Terminal channel-history failure. Cached messages remain visible when present. */
63+
isTimelineError?: boolean;
6264
isTimelineLoading: boolean;
65+
onRetryTimeline?: () => void;
6366
/** Newly-created message that should receive the one-shot conversation arrival motion. */
6467
entranceMessageId?: string | null;
6568
onEntranceMessageComplete?: (messageId: string) => void;

‎desktop/src/features/channels/ui/ChannelScreen.tsx‎

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ import {
5050
isThreadReply,
5151
} from "@/features/messages/lib/threading";
5252
import { hasPersistedHydratedChannel } from "@/features/messages/lib/channelHeadCache";
53-
import {
54-
resolveTimelineLoadingLatch,
55-
selectTimelineLoadingState,
56-
} from "@/features/messages/lib/timelineLoadingState";
53+
import { resolveTimelineQueryLoadingState } from "@/features/messages/lib/timelineLoadingState";
5754
import { useFetchOlderMessages } from "@/features/messages/useFetchOlderMessages";
5855
import { useIndependentThreadPanel } from "@/features/messages/useIndependentThreadPanel";
5956
import { useThreadReplies } from "@/features/messages/useThreadReplies";
@@ -616,27 +613,21 @@ export function ChannelScreen({
616613
setThreadScrollTargetId,
617614
});
618615
const settledChannelIdRef = React.useRef<string | null>(null);
619-
const hasSettledThisChannel =
620-
activeChannelId !== null && settledChannelIdRef.current === activeChannelId;
621-
const timelineLoadingNow =
622-
activeChannel !== null &&
623-
activeChannel.channelType !== "forum" &&
624-
selectTimelineLoadingState(
616+
const { settledChannelId, isLoading: isTimelineLoading } =
617+
resolveTimelineQueryLoadingState(
618+
settledChannelIdRef.current,
619+
activeChannelId,
625620
{
621+
isEnabled:
622+
activeChannel !== null && activeChannel.channelType !== "forum",
626623
isPending: messagesQuery.isPending,
627624
isFetching: messagesQuery.isFetching,
628625
isPlaceholderData: messagesQuery.isPlaceholderData,
629626
dataLength: messagesQuery.data?.length ?? null,
627+
isError: messagesQuery.isError,
630628
},
631-
hasSettledThisChannel ||
632-
(activeChannelId !== null &&
633-
hasPersistedHydratedChannel(queryClient, activeChannelId)),
634-
);
635-
const { settledChannelId, isLoading: isTimelineLoading } =
636-
resolveTimelineLoadingLatch(
637-
settledChannelIdRef.current,
638-
activeChannelId,
639-
timelineLoadingNow,
629+
activeChannelId !== null &&
630+
hasPersistedHydratedChannel(queryClient, activeChannelId),
640631
);
641632
settledChannelIdRef.current = settledChannelId;
642633
const { welcomeKickoffStage, welcomeKickoffSettingUp } =
@@ -890,8 +881,8 @@ export function ChannelScreen({
890881
isFollowingThread={isNotifiedForEffectiveThread}
891882
isSending={sendMessageMutation.isPending}
892883
isSinglePanelView={isSinglePanelView}
893-
isTimelineLoading={isTimelineLoading}
894-
messages={timelineMessages}
884+
isTimelineError={messagesQuery.isError} isTimelineLoading={isTimelineLoading}
885+
onRetryTimeline={() => void messagesQuery.refetch()} messages={timelineMessages}
895886
threadSummaries={threadSummaries}
896887
huddleThreadRepliesError={huddleThreadRepliesError}
897888
onRetryHuddleThreadReplies={onRetryHuddleThreadReplies}

‎desktop/src/features/messages/lib/projectChannelWindow.test.mjs‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,74 @@ test("test_concurrent_refreshes_after_seeded_snapshot_share_one_authoritative_fe
416416
unsubscribe();
417417
}
418418
});
419+
420+
test("test_subscription_refresh_preserves_cold_history_error", async () => {
421+
const client = new QueryClient({
422+
defaultOptions: { queries: { retry: false } },
423+
});
424+
const channelId = "cold-failure";
425+
const queryKey = channelMessagesKey(channelId);
426+
const observer = new QueryObserver(client, {
427+
queryKey,
428+
queryFn: async () => {
429+
throw new Error("history unavailable");
430+
},
431+
});
432+
const unsubscribe = observer.subscribe(() => {});
433+
434+
try {
435+
await observer.refetch();
436+
assert.equal(observer.getCurrentResult().status, "error");
437+
assert.equal(observer.getCurrentResult().data, undefined);
438+
439+
await assert.rejects(
440+
refreshChannelWindowMessages(client, channelId),
441+
/history unavailable/,
442+
);
443+
assert.equal(observer.getCurrentResult().status, "error");
444+
assert.equal(observer.getCurrentResult().data, undefined);
445+
} finally {
446+
unsubscribe();
447+
}
448+
});
449+
450+
test("test_refresh_failure_retains_cached_rows_and_success_clears_error", async () => {
451+
const harness = createHarness();
452+
let shouldFail = true;
453+
const refreshed = event("refreshed", 110);
454+
const observer = new QueryObserver(harness.client, {
455+
queryKey: harness.messagesKey,
456+
queryFn: async ({ signal }) => {
457+
if (shouldFail) {
458+
throw new Error("history unavailable");
459+
}
460+
const previousMessages =
461+
harness.client.getQueryData(harness.messagesKey) ?? [];
462+
return reconcileFetchedChannelWindow(
463+
harness.client,
464+
harness.channelId,
465+
wirePage([refreshed, event("initial", 100)]),
466+
previousMessages,
467+
signal,
468+
);
469+
},
470+
staleTime: Number.POSITIVE_INFINITY,
471+
});
472+
const unsubscribe = observer.subscribe(() => {});
473+
474+
try {
475+
await assert.rejects(
476+
refreshChannelWindowMessages(harness.client, harness.channelId),
477+
/history unavailable/,
478+
);
479+
assert.equal(observer.getCurrentResult().status, "error");
480+
assert.deepEqual(contents(harness), ["initial"]);
481+
482+
shouldFail = false;
483+
await refreshChannelWindowMessages(harness.client, harness.channelId);
484+
assert.equal(observer.getCurrentResult().status, "success");
485+
assert.deepEqual(contents(harness), ["initial", "refreshed"]);
486+
} finally {
487+
unsubscribe();
488+
}
489+
});

‎desktop/src/features/messages/lib/projectChannelWindow.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export async function refreshChannelWindowMessages(
4747
}
4848
await queryClient.invalidateQueries(
4949
{ queryKey, exact: true, refetchType: "active" },
50-
{ cancelRefetch: !seeded },
50+
{ cancelRefetch: !seeded, throwOnError: true },
5151
);
5252
projectChannelWindowMessages(queryClient, channelId);
5353
}

‎desktop/src/features/messages/lib/timelineLoadingState.test.mjs‎

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import assert from "node:assert/strict";
22
import test from "node:test";
3+
import { QueryClient, QueryObserver } from "@tanstack/react-query";
34

4-
import { selectTimelineLoadingState } from "./timelineLoadingState.ts";
5+
import {
6+
resolveTimelineLoadingLatch,
7+
resolveTimelineQueryLoadingState,
8+
selectTimelineLoadingState,
9+
} from "./timelineLoadingState.ts";
510

611
const settled = {
712
isPending: false,
@@ -10,6 +15,27 @@ const settled = {
1015
dataLength: null,
1116
};
1217

18+
function deferred() {
19+
let resolve;
20+
let reject;
21+
const promise = new Promise((resolvePromise, rejectPromise) => {
22+
resolve = resolvePromise;
23+
reject = rejectPromise;
24+
});
25+
return { promise, reject, resolve };
26+
}
27+
28+
function resolveQueryLoading(result, settledChannelId = null) {
29+
return resolveTimelineQueryLoadingState(settledChannelId, "chan-a", {
30+
isEnabled: true,
31+
isPending: result.isPending,
32+
isFetching: result.isFetching,
33+
isPlaceholderData: result.isPlaceholderData,
34+
dataLength: result.data?.length ?? null,
35+
isError: result.isError,
36+
});
37+
}
38+
1339
test("pending first fetch with no cache is loading", () => {
1440
assert.equal(
1541
selectTimelineLoadingState({ ...settled, isPending: true }),
@@ -120,8 +146,6 @@ test("settled channel with rows mid-refetch is not loading", () => {
120146
);
121147
});
122148

123-
import { resolveTimelineLoadingLatch } from "./timelineLoadingState.ts";
124-
125149
test("latch: loading on first entry to a channel", () => {
126150
const r = resolveTimelineLoadingLatch(null, "chan-a", true);
127151
assert.equal(r.isLoading, true);
@@ -158,3 +182,80 @@ test("latch: no active channel passes loadingNow through untouched", () => {
158182
false,
159183
);
160184
});
185+
186+
test("query wiring: cold error retry stays loading until successful empty result", async () => {
187+
const client = new QueryClient({
188+
defaultOptions: { queries: { retry: false } },
189+
});
190+
const first = deferred();
191+
const retry = deferred();
192+
let attempt = 0;
193+
const observer = new QueryObserver(client, {
194+
queryKey: ["messages", "chan-a"],
195+
queryFn: () => [first.promise, retry.promise][attempt++],
196+
});
197+
const unsubscribe = observer.subscribe(() => {});
198+
199+
try {
200+
first.reject(new Error("history unavailable"));
201+
await new Promise((resolve) => setImmediate(resolve));
202+
let loading = resolveQueryLoading(observer.getCurrentResult());
203+
assert.equal(observer.getCurrentResult().status, "error");
204+
assert.deepEqual(loading, { settledChannelId: null, isLoading: false });
205+
206+
const retryResult = observer.refetch();
207+
loading = resolveQueryLoading(observer.getCurrentResult());
208+
assert.equal(observer.getCurrentResult().status, "pending");
209+
assert.deepEqual(loading, { settledChannelId: null, isLoading: true });
210+
211+
retry.resolve([]);
212+
await retryResult;
213+
loading = resolveQueryLoading(observer.getCurrentResult());
214+
assert.equal(observer.getCurrentResult().status, "success");
215+
assert.deepEqual(loading, {
216+
settledChannelId: "chan-a",
217+
isLoading: false,
218+
});
219+
} finally {
220+
unsubscribe();
221+
}
222+
});
223+
224+
test("query wiring: repeated cold retry failure never settles as empty", async () => {
225+
const client = new QueryClient({
226+
defaultOptions: { queries: { retry: false } },
227+
});
228+
const first = deferred();
229+
const retry = deferred();
230+
let attempt = 0;
231+
const observer = new QueryObserver(client, {
232+
queryKey: ["messages", "chan-a"],
233+
queryFn: () => [first.promise, retry.promise][attempt++],
234+
});
235+
const unsubscribe = observer.subscribe(() => {});
236+
237+
try {
238+
first.reject(new Error("history unavailable"));
239+
await new Promise((resolve) => setImmediate(resolve));
240+
assert.deepEqual(resolveQueryLoading(observer.getCurrentResult()), {
241+
settledChannelId: null,
242+
isLoading: false,
243+
});
244+
245+
const retryResult = observer.refetch();
246+
assert.deepEqual(resolveQueryLoading(observer.getCurrentResult()), {
247+
settledChannelId: null,
248+
isLoading: true,
249+
});
250+
251+
retry.reject(new Error("still unavailable"));
252+
await retryResult;
253+
assert.equal(observer.getCurrentResult().status, "error");
254+
assert.deepEqual(resolveQueryLoading(observer.getCurrentResult()), {
255+
settledChannelId: null,
256+
isLoading: false,
257+
});
258+
} finally {
259+
unsubscribe();
260+
}
261+
});

0 commit comments

Comments
 (0)