Skip to content

Commit 145f427

Browse files
fix(web): preserve thread errors during shell loading
- Treat the loading thread shell as the active server thread - Preserve error state across thread promotion and add focused coverage
1 parent 7dd91b5 commit 145f427

3 files changed

Lines changed: 23 additions & 16 deletions

File tree

‎apps/web/src/components/ChatView.logic.test.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,19 +472,24 @@ describe("reconcileRetainedMountedThreadIds", () => {
472472
});
473473

474474
describe("shouldWriteThreadErrorToCurrentServerThread", () => {
475-
it("requires the environment, route thread, and target thread to match", () => {
475+
it("writes errors for a shell-derived active server thread", () => {
476476
const routeThreadRef = { environmentId, threadId };
477477

478478
expect(
479479
shouldWriteThreadErrorToCurrentServerThread({
480-
serverThread: { environmentId, id: threadId },
480+
activeServerThread: { environmentId, id: threadId },
481481
routeThreadRef,
482482
targetThreadId: threadId,
483483
}),
484484
).toBe(true);
485+
});
486+
487+
it("requires an active server thread matching the environment, route, and target", () => {
488+
const routeThreadRef = { environmentId, threadId };
489+
485490
expect(
486491
shouldWriteThreadErrorToCurrentServerThread({
487-
serverThread: null,
492+
activeServerThread: null,
488493
routeThreadRef,
489494
targetThreadId: threadId,
490495
}),

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export function buildLoadingThreadFromShell(shell: ThreadShell): Thread {
107107
}
108108

109109
export function shouldWriteThreadErrorToCurrentServerThread(input: {
110-
serverThread:
110+
activeServerThread:
111111
| {
112112
environmentId: EnvironmentId;
113113
id: ThreadId;
@@ -118,10 +118,10 @@ export function shouldWriteThreadErrorToCurrentServerThread(input: {
118118
targetThreadId: ThreadId;
119119
}): boolean {
120120
return Boolean(
121-
input.serverThread &&
121+
input.activeServerThread &&
122122
input.targetThreadId === input.routeThreadRef.threadId &&
123-
input.serverThread.environmentId === input.routeThreadRef.environmentId &&
124-
input.serverThread.id === input.targetThreadId,
123+
input.activeServerThread.environmentId === input.routeThreadRef.environmentId &&
124+
input.activeServerThread.id === input.targetThreadId,
125125
);
126126
}
127127

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ import {
274274
resolveSendEnvMode,
275275
revokeBlobPreviewUrl,
276276
revokeUserMessagePreviewUrls,
277+
shouldWriteThreadErrorToCurrentServerThread,
277278
startNewThreadForProject,
278279
waitForStartedServerThread,
279280
} from "./ChatView.logic";
@@ -1204,6 +1205,7 @@ function ChatViewContent(props: ChatViewProps) {
12041205
: null,
12051206
[routeServerThreadShell, threadDetailLoading],
12061207
);
1208+
const activeServerThread = serverThread ?? loadingServerThread;
12071209
const markThreadVisited = useUiStateStore((store) => store.markThreadVisited);
12081210
const activeThreadLastVisitedAt = useUiStateStore(
12091211
(store) => store.threadLastVisitedAtById[routeThreadKey],
@@ -1383,7 +1385,7 @@ function ChatViewContent(props: ChatViewProps) {
13831385
? scopeProjectRef(draftThread.environmentId, draftThread.projectId)
13841386
: null;
13851387
const fallbackDraftProject = useProject(fallbackDraftProjectRef);
1386-
const localDraftError = serverThread
1388+
const localDraftError = activeServerThread
13871389
? null
13881390
: ((draftId ? localDraftErrorsByDraftId[draftId]?.message : null) ?? null);
13891391
const localServerError = localServerErrorsByThreadKey[routeThreadKey]?.message ?? null;
@@ -1392,7 +1394,7 @@ function ChatViewContent(props: ChatViewProps) {
13921394
// a failed send would silently disappear on promotion. When both keys hold
13931395
// an entry, the most recent write wins.
13941396
useEffect(() => {
1395-
if (!serverThread || !draftId) {
1397+
if (!activeServerThread || !draftId) {
13961398
return;
13971399
}
13981400
const pendingDraftEntry = localDraftErrorsByDraftId[draftId];
@@ -1421,7 +1423,7 @@ function ChatViewContent(props: ChatViewProps) {
14211423
[routeThreadKey]: pendingDraftEntry,
14221424
};
14231425
});
1424-
}, [draftId, localDraftErrorsByDraftId, routeThreadKey, serverThread]);
1426+
}, [activeServerThread, draftId, localDraftErrorsByDraftId, routeThreadKey]);
14251427
const localDraftThread = useMemo(
14261428
() =>
14271429
draftThread
@@ -1436,7 +1438,6 @@ function ChatViewContent(props: ChatViewProps) {
14361438
// Promotion is data-driven: the draft route keeps rendering while the
14371439
// server thread (same pre-allocated ref) starts, so live state must not
14381440
// depend on which route is mounted.
1439-
const activeServerThread = serverThread ?? loadingServerThread;
14401441
const isServerThread = activeServerThread !== null;
14411442
const activeThread = activeServerThread ?? localDraftThread;
14421443
const threadError = isServerThread
@@ -2506,10 +2507,11 @@ function ChatViewContent(props: ChatViewProps) {
25062507
const nextError = sanitizeThreadErrorMessage(error);
25072508
const nextEntry: LocalThreadErrorEntry = { message: nextError, at: Date.now() };
25082509
if (
2509-
serverThread &&
2510-
targetThreadId === routeThreadRef.threadId &&
2511-
serverThread.environmentId === routeThreadRef.environmentId &&
2512-
serverThread.id === targetThreadId
2510+
shouldWriteThreadErrorToCurrentServerThread({
2511+
activeServerThread,
2512+
routeThreadRef,
2513+
targetThreadId,
2514+
})
25132515
) {
25142516
setLocalServerErrorsByThreadKey((existing) => {
25152517
if ((existing[routeThreadKey]?.message ?? null) === nextError) {
@@ -2533,7 +2535,7 @@ function ChatViewContent(props: ChatViewProps) {
25332535
};
25342536
});
25352537
},
2536-
[draftId, routeThreadKey, routeThreadRef, serverThread],
2538+
[activeServerThread, draftId, routeThreadKey, routeThreadRef],
25372539
);
25382540

25392541
const focusComposer = useCallback(() => {

0 commit comments

Comments
 (0)