Skip to content

Commit c4493e3

Browse files
committed
fix(web): recover uncached threads when disconnected
1 parent b5c2253 commit c4493e3

3 files changed

Lines changed: 90 additions & 10 deletions

File tree

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import { SidebarInset } from "./ui/sidebar";
1212
import { Button } from "./ui/button";
1313
import { Empty, EmptyContent, EmptyDescription, EmptyHeader, EmptyTitle } from "./ui/empty";
1414
import { useComposerDraftStore } from "../composerDraftStore";
15+
import { environmentCatalog } from "../connection/catalog";
16+
import { readEnvironment } from "../state/environments";
17+
import { useEnvironmentQuery } from "../state/query";
18+
import { useAtomCommand } from "../state/use-atom-command";
1519
import { useSidebarPendingFileDropStore } from "../sidebarPendingFileDropStore";
1620
import {
1721
useEnvironmentThreadRefs,
@@ -45,6 +49,8 @@ export function ThreadRouteView({
4549
background?: boolean;
4650
}) {
4751
const navigate = useNavigate();
52+
const retryConnection = useAtomCommand(environmentCatalog.retryNow);
53+
const enableEnvironment = useAtomCommand(environmentCatalog.setEnabled);
4854
const draftId = target.kind === "draft" ? target.draftId : null;
4955
const draftSession = useComposerDraftStore((store) =>
5056
draftId === null ? null : store.getDraftSession(draftId),
@@ -94,6 +100,19 @@ export function ThreadRouteView({
94100
unlistedThreadRef?.environmentId ?? null,
95101
unlistedThreadRef?.threadId ?? null,
96102
);
103+
const connection = useEnvironmentQuery(
104+
unlistedThreadRef === null
105+
? null
106+
: environmentCatalog.stateAtom(unlistedThreadRef.environmentId),
107+
);
108+
const connectionPhase =
109+
connection.error !== null
110+
? "blocked"
111+
: connection.isPending
112+
? null
113+
: (connection.data?.phase ?? null);
114+
const connectionUnavailable =
115+
connectionPhase !== null && connectionPhase !== "connected" && connectionPhase !== "connecting";
97116
const hiddenKeys = useAtomValue(environmentThreadShells.hiddenThreadKeysAtom);
98117
const unlistedThread = Option.getOrNull(Option.map(unlistedThreadDetail.data, (p) => p.thread));
99118
const unlistedParentKey =
@@ -118,6 +137,7 @@ export function ThreadRouteView({
118137
);
119138
const renderState = resolveThreadRouteRenderState({
120139
bootstrapComplete,
140+
connectionPhase,
121141
serverThreadExists: serverThreadShell !== null,
122142
serverThreadDeleted: serverThreadShell?.deletedAt != null,
123143
unlistedThreadDetail,
@@ -200,14 +220,29 @@ export function ThreadRouteView({
200220
<Empty role="alert">
201221
<EmptyHeader>
202222
<EmptyTitle>Could not load thread</EmptyTitle>
203-
<EmptyDescription>{Option.getOrNull(unlistedThreadDetail.error)}</EmptyDescription>
223+
<EmptyDescription>
224+
{Option.getOrNull(unlistedThreadDetail.error) ??
225+
connection.data?.lastFailure?.message ??
226+
connection.error ??
227+
"This environment is not connected."}
228+
</EmptyDescription>
204229
</EmptyHeader>
205230
<EmptyContent>
206231
<div className="flex items-center gap-2">
207232
<Button
208233
variant="outline"
209234
size="sm"
210-
onClick={() => retryEnvironmentThread(target.threadRef)}
235+
onClick={() => {
236+
retryEnvironmentThread(target.threadRef);
237+
if (connectionUnavailable) {
238+
const environmentId = target.threadRef.environmentId;
239+
if (readEnvironment(environmentId)?.entry.enabled === false) {
240+
void enableEnvironment({ environmentId, enabled: true });
241+
} else {
242+
void retryConnection(environmentId);
243+
}
244+
}
245+
}}
211246
>
212247
Retry
213248
</Button>

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ describe("threadRoutes", () => {
6363
expect(
6464
resolveThreadRouteRenderState({
6565
bootstrapComplete: isThreadRouteSnapshotAuthoritative(status),
66+
connectionPhase: "offline",
6667
serverThreadExists: source === "server thread",
6768
serverThreadDeleted: false,
6869
unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
@@ -76,6 +77,7 @@ describe("threadRoutes", () => {
7677
expect(
7778
resolveThreadRouteRenderState({
7879
bootstrapComplete: false,
80+
connectionPhase: "connecting",
7981
serverThreadExists: false,
8082
serverThreadDeleted: false,
8183
unlistedThreadDetail: { ...EMPTY_ENVIRONMENT_THREAD_STATE, status: "deleted" },
@@ -85,6 +87,7 @@ describe("threadRoutes", () => {
8587
expect(
8688
resolveThreadRouteRenderState({
8789
bootstrapComplete: true,
90+
connectionPhase: "connected",
8891
serverThreadExists: false,
8992
serverThreadDeleted: false,
9093
unlistedThreadDetail: { ...EMPTY_ENVIRONMENT_THREAD_STATE, status: "deleted" },
@@ -96,6 +99,7 @@ describe("threadRoutes", () => {
9699
it("opens an unlisted server thread once its detail arrives, without waiting for the shell", () => {
97100
const route = {
98101
bootstrapComplete: isThreadRouteSnapshotAuthoritative("live"),
102+
connectionPhase: "connected" as const,
99103
serverThreadExists: false,
100104
serverThreadDeleted: false,
101105
draftThreadExists: false,
@@ -130,6 +134,13 @@ describe("threadRoutes", () => {
130134
...EMPTY_ENVIRONMENT_THREAD_STATE,
131135
error: Option.some("Connection interrupted"),
132136
};
137+
expect(
138+
resolveThreadRouteRenderState({
139+
...route,
140+
connectionPhase: "connecting",
141+
unlistedThreadDetail: failedDetail,
142+
}),
143+
).toBe("loading");
133144
expect(
134145
resolveThreadRouteRenderState({
135146
...route,
@@ -155,11 +166,40 @@ describe("threadRoutes", () => {
155166
).toBe("ready");
156167
});
157168

169+
it.each(["blocked", "offline", "backoff", "available"] as const)(
170+
"offers recovery for an uncached thread while its connection is %s without a detail error",
171+
(connectionPhase) => {
172+
const route = {
173+
bootstrapComplete: false,
174+
connectionPhase,
175+
serverThreadExists: false,
176+
serverThreadDeleted: false,
177+
draftThreadExists: false,
178+
};
179+
expect(
180+
resolveThreadRouteRenderState({
181+
...route,
182+
unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,
183+
}),
184+
).toBe("error");
185+
expect(
186+
resolveThreadRouteRenderState({
187+
...route,
188+
unlistedThreadDetail: {
189+
...EMPTY_ENVIRONMENT_THREAD_STATE,
190+
status: "synchronizing",
191+
},
192+
}),
193+
).toBe("error");
194+
},
195+
);
196+
158197
it.each(["missing", "deleted", "archived"] as const)(
159198
"redirects an unlisted thread only after detail confirms it is %s",
160199
(outcome) => {
161200
const route = {
162201
bootstrapComplete: isThreadRouteSnapshotAuthoritative("live"),
202+
connectionPhase: "connected" as const,
163203
serverThreadExists: false,
164204
serverThreadDeleted: false,
165205
draftThreadExists: false,
@@ -203,6 +243,7 @@ describe("threadRoutes", () => {
203243
expect(
204244
resolveThreadRouteRenderState({
205245
bootstrapComplete: isThreadRouteSnapshotAuthoritative("cached"),
246+
connectionPhase: "offline",
206247
serverThreadExists: true,
207248
serverThreadDeleted: true,
208249
unlistedThreadDetail: EMPTY_ENVIRONMENT_THREAD_STATE,

‎apps/web/src/threadRoutes.ts‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2+
import type { SupervisorConnectionPhase } from "@t3tools/client-runtime/connection";
23
import type { EnvironmentShellStatus } from "@t3tools/client-runtime/state/shell";
34
import type { EnvironmentThreadState } from "@t3tools/client-runtime/state/threads";
45
import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts";
@@ -31,6 +32,7 @@ export function isThreadRouteSnapshotAuthoritative(
3132

3233
export function resolveThreadRouteRenderState(input: {
3334
bootstrapComplete: boolean;
35+
connectionPhase: SupervisorConnectionPhase | null;
3436
serverThreadExists: boolean;
3537
serverThreadDeleted: boolean;
3638
unlistedThreadDetail: EnvironmentThreadState;
@@ -41,14 +43,16 @@ export function resolveThreadRouteRenderState(input: {
4143
}
4244
const detail = input.unlistedThreadDetail;
4345
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";
46+
if (!input.serverThreadExists && projection === null && detail.status !== "deleted") {
47+
if (input.connectionPhase === "connecting") {
48+
return "loading";
49+
}
50+
if (
51+
(input.connectionPhase !== null && input.connectionPhase !== "connected") ||
52+
(detail.status !== "synchronizing" && Option.isSome(detail.error))
53+
) {
54+
return "error";
55+
}
5256
}
5357
if (!input.bootstrapComplete) {
5458
return "loading";

0 commit comments

Comments
 (0)