Skip to content

Commit b5c2253

Browse files
committed
fix(web): recover from uncached thread load failures
1 parent f107cf0 commit b5c2253

4 files changed

Lines changed: 72 additions & 6 deletions

File tree

‎apps/web/src/components/ThreadRouteView.tsx‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import { scopedThreadKey } from "@t3tools/client-runtime/environment";
33
import { parseThreadKey, threadKey } from "@t3tools/client-runtime/state/entities";
44
import { visibilityParentKey } from "@t3tools/client-runtime/state/threads";
55
import * as Option from "effect/Option";
6-
import { Navigate, useNavigate } from "@tanstack/react-router";
6+
import { Link, Navigate, useNavigate } from "@tanstack/react-router";
77
import { useEffect, useState } from "react";
88

99
import ChatView from "./ChatView";
1010
import { BackgroundThreadContext } from "./chat/backgroundThread";
1111
import { SidebarInset } from "./ui/sidebar";
12+
import { Button } from "./ui/button";
13+
import { Empty, EmptyContent, EmptyDescription, EmptyHeader, EmptyTitle } from "./ui/empty";
1214
import { useComposerDraftStore } from "../composerDraftStore";
1315
import { useSidebarPendingFileDropStore } from "../sidebarPendingFileDropStore";
1416
import {
@@ -17,7 +19,11 @@ import {
1719
useQuietThreadShell,
1820
} from "../state/entities";
1921
import { useEnvironmentShellStatus } from "../state/shell";
20-
import { environmentThreadShells, useEnvironmentThread } from "../state/threads";
22+
import {
23+
environmentThreadShells,
24+
retryEnvironmentThread,
25+
useEnvironmentThread,
26+
} from "../state/threads";
2127
import {
2228
buildThreadRouteParams,
2329
isThreadRouteSnapshotAuthoritative,
@@ -189,6 +195,29 @@ export function ThreadRouteView({
189195
}
190196
} else if (hiddenByTagFilter) {
191197
redirect = <Navigate to="/" replace />;
198+
} else if (renderState === "error") {
199+
view = (
200+
<Empty role="alert">
201+
<EmptyHeader>
202+
<EmptyTitle>Could not load thread</EmptyTitle>
203+
<EmptyDescription>{Option.getOrNull(unlistedThreadDetail.error)}</EmptyDescription>
204+
</EmptyHeader>
205+
<EmptyContent>
206+
<div className="flex items-center gap-2">
207+
<Button
208+
variant="outline"
209+
size="sm"
210+
onClick={() => retryEnvironmentThread(target.threadRef)}
211+
>
212+
Retry
213+
</Button>
214+
<Button variant="ghost" size="sm" render={<Link to="/" />}>
215+
Back to threads
216+
</Button>
217+
</div>
218+
</EmptyContent>
219+
</Empty>
220+
);
192221
} else if (offShellBlocked) {
193222
// Before the shell bootstraps, a listed visible thread can still look off-shell.
194223
if (bootstrapComplete && unlistedThread !== null) redirect = <Navigate to="/" replace />;

‎apps/web/src/state/threads.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ import {
1212
createThreadEnvironmentAtoms,
1313
createThreadFamilyAtoms,
1414
} from "@t3tools/client-runtime/state/threads";
15-
import type { EnvironmentId, OrchestrationV2ThreadShell, ThreadId } from "@t3tools/contracts";
15+
import type {
16+
EnvironmentId,
17+
OrchestrationV2ThreadShell,
18+
ScopedThreadRef,
19+
ThreadId,
20+
} from "@t3tools/contracts";
1621
import * as Option from "effect/Option";
1722
import { AsyncResult, Atom } from "effect/reactivity";
1823

1924
import { environmentCatalog } from "../connection/catalog";
2025
import { connectionAtomRuntime } from "../connection/runtime";
26+
import { appAtomRegistry } from "../rpc/atomRegistry";
2127
import { serverEnvironment } from "./server";
2228
import { environmentShell } from "./shell";
2329

@@ -64,6 +70,10 @@ export function useEnvironmentThread(
6470
return state;
6571
}
6672

73+
export function retryEnvironmentThread(ref: ScopedThreadRef) {
74+
appAtomRegistry.refresh(environmentThreads.stateAtom(ref.environmentId, ref.threadId));
75+
}
76+
6777
const isRunning = (status: string) =>
6878
status === "preparing" || status === "queued" || status === "starting" || status === "running";
6979

‎apps/web/src/threadRoutes.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,23 @@ describe("threadRoutes", () => {
126126
},
127127
}),
128128
).toBe("loading");
129+
const failedDetail = {
130+
...EMPTY_ENVIRONMENT_THREAD_STATE,
131+
error: Option.some("Connection interrupted"),
132+
};
133+
expect(
134+
resolveThreadRouteRenderState({
135+
...route,
136+
unlistedThreadDetail: failedDetail,
137+
}),
138+
).toBe("error");
139+
expect(
140+
resolveThreadRouteRenderState({
141+
...route,
142+
bootstrapComplete: false,
143+
unlistedThreadDetail: failedDetail,
144+
}),
145+
).toBe("error");
129146
expect(
130147
resolveThreadRouteRenderState({
131148
...route,

‎apps/web/src/threadRoutes.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type DraftThreadRouteState = {
2121
promotedTo?: ScopedThreadRef | null;
2222
};
2323

24-
export type ThreadRouteRenderState = "loading" | "ready" | "missing";
24+
export type ThreadRouteRenderState = "loading" | "ready" | "missing" | "error";
2525

2626
export function isThreadRouteSnapshotAuthoritative(
2727
status: EnvironmentShellStatus | null | undefined,
@@ -39,14 +39,24 @@ export function resolveThreadRouteRenderState(input: {
3939
if (input.draftThreadExists || (input.serverThreadExists && !input.serverThreadDeleted)) {
4040
return "ready";
4141
}
42+
const detail = input.unlistedThreadDetail;
43+
const projection = Option.getOrNull(detail.data);
44+
if (
45+
!input.serverThreadExists &&
46+
projection === null &&
47+
detail.status !== "synchronizing" &&
48+
detail.status !== "deleted" &&
49+
Option.isSome(detail.error)
50+
) {
51+
return "error";
52+
}
4253
if (!input.bootstrapComplete) {
4354
return "loading";
4455
}
45-
if (input.serverThreadExists || input.unlistedThreadDetail.status === "deleted") {
56+
if (input.serverThreadExists || detail.status === "deleted") {
4657
return "missing";
4758
}
4859
// Shell membership can lag a new thread; its detail decides.
49-
const projection = Option.getOrNull(input.unlistedThreadDetail.data);
5060
if (projection === null) {
5161
return "loading";
5262
}

0 commit comments

Comments
 (0)