Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(chat): end a stuck session load in a retryable state

Bingxi Zhao (Frank)7cc213f24dd2
diff --git a/web/app/(workspace)/home/[[...sessionId]]/page.tsx b/web/app/(workspace)/home/[[...sessionId]]/page.tsx
index adedcd0b8..c1d234f10 100644
--- a/web/app/(workspace)/home/[[...sessionId]]/page.tsx
+++ b/web/app/(workspace)/home/[[...sessionId]]/page.tsx
@@ -39,6 +39,10 @@ import type { ContextBudget } from "@/components/chat/home/ContextBudgetChip";
import { ChatMessageList } from "@/components/chat/home/ChatMessages";
import { TurnNavigator } from "@/components/chat/home/TurnNavigator";
import SessionLoadingView from "@/components/chat/home/SessionLoadingView";
+import {
+ SESSION_LOAD_TIMEOUT_MS,
+ shouldSurfaceLoadFailure,
+} from "@/lib/session-load";
import StarterSuggestions from "@/components/chat/home/StarterSuggestions";
import MasteryPathStrip from "@/components/chat/home/MasteryPathStrip";
// Imported eagerly so the drawer shell is always mounted off-screen —
@@ -598,6 +602,10 @@ export default function ChatPage() {
// Session-loading overlay: shown while navigating from chat-history →
// session detail. Holds an AbortController so the user can cancel.
const [sessionLoading, setSessionLoading] = useState(false);
+ // A load that ended without a session: terminal, and retryable. Kept
+ // separate from `sessionLoading` so the overlay can tell "still
+ // arriving" apart from "never arrived".
+ const [sessionLoadFailed, setSessionLoadFailed] = useState(false);
const loadAbortRef = useRef<AbortController | null>(null);
// Bridge ref: ``ChatComposer`` writes a prefill function into this on
// mount; ``ChatMessageList`` reads it via ``handlePrefillComposer`` so an
@@ -1039,17 +1047,23 @@ export default function ChatPage() {
loadAbortRef.current?.abort();
loadAbortRef.current = null;
setSessionLoading(false);
+ setSessionLoadFailed(false);
navigateToHome();
}, [navigateToHome]);
/**
- * Shared helper: kick off a load. The user can cancel via the ✕ button;
- * otherwise the loading overlay stays until the API responds (no timeout).
+ * Shared helper: kick off a load. The user can cancel via the ✕ button.
*
* A session we already hold in memory is painted right away and refreshed
* in the background — switching back to a conversation read earlier in this
* visit costs nothing, and the overlay is reserved for the case where we
* genuinely have nothing to show.
+ *
+ * The wait is bounded. A fetch that never settles used to leave the overlay
+ * spinning forever with no way out but abandoning the conversation, and a
+ * fetch that *failed* used to replace the URL with /home — dropping the
+ * session id, so a transient error read as "my history is gone". Both now
+ * end in the same terminal, retryable state with the id still in the URL.
*/
const startSessionLoad = useCallback(
(sid: string) => {
@@ -1058,9 +1072,20 @@ export default function ChatPage() {
loadAbortRef.current = ctrl;
const cached = showCachedSession(sid);
setSessionLoading(!cached);
+ setSessionLoadFailed(false);
+
+ // Aborting is how the timeout stops waiting, so it has to be
+ // distinguishable from the user's ✕ and from a newer load taking over:
+ // those two own the resulting state, a timeout does not.
+ let timedOut = false;
+ const timeout = setTimeout(() => {
+ timedOut = true;
+ ctrl.abort();
+ }, SESSION_LOAD_TIMEOUT_MS);
void loadSession(sid, { signal: ctrl.signal, revalidate: cached })
.then(() => {
+ clearTimeout(timeout);
if (!ctrl.signal.aborted) {
loadAbortRef.current = null;
setSessionLoading(false);
@@ -1087,24 +1112,27 @@ export default function ChatPage() {
}
})
.catch(() => {
- if (!ctrl.signal.aborted) {
- loadAbortRef.current = null;
- setSessionLoading(false);
- // A background refresh that fails leaves the cached copy on
- // screen; only a cold open has nothing to fall back to.
- if (!cached) navigateToHome();
- }
+ clearTimeout(timeout);
+ const surface = shouldSurfaceLoadFailure({
+ aborted: ctrl.signal.aborted,
+ timedOut,
+ cached,
+ });
+ // A newer load (or the user's ✕) owns the state from here, and a
+ // failed background refresh leaves the cached copy on screen.
+ if (!surface) return;
+ loadAbortRef.current = null;
+ setSessionLoading(false);
+ setSessionLoadFailed(true);
});
},
- [
- loadSession,
- navigateToHome,
- showCachedSession,
- scrollToBottom,
- shouldAutoScrollRef,
- ],
+ [loadSession, showCachedSession, scrollToBottom, shouldAutoScrollRef],
);
+ const retrySessionLoad = useCallback(() => {
+ if (sessionIdParam) startSessionLoad(sessionIdParam);
+ }, [sessionIdParam, startSessionLoad]);
+
// Initial mount — load the session from the URL.
// Uses a ref-based flag so Strict Mode double-mount doesn't break the flow:
// when React tears down + re-mounts in dev, we reset initialLoadRef in
@@ -1135,12 +1163,14 @@ export default function ChatPage() {
if (sessionIdParam) {
if (sessionIdParam === state.sessionId) {
setSessionLoading(false);
+ setSessionLoadFailed(false);
return;
}
startSessionLoad(sessionIdParam);
} else {
newSession();
setSessionLoading(false);
+ setSessionLoadFailed(false);
}
}, [sessionIdParam, startSessionLoad, newSession, state.sessionId]);
@@ -2088,10 +2118,14 @@ export default function ChatPage() {
</div>
</div>
<div className="flex w-full flex-1 min-h-0 flex-col">
- {sessionLoading ? (
+ {sessionLoading || sessionLoadFailed ? (
<div className="flex w-full flex-1 min-h-0 justify-center px-6">
<div className="h-full w-full max-w-[960px]">
- <SessionLoadingView onCancel={cancelSessionLoad} />
+ <SessionLoadingView
+ onCancel={cancelSessionLoad}
+ failed={sessionLoadFailed}
+ onRetry={retrySessionLoad}
+ />
</div>
</div>
) : !hasMessages ? (
diff --git a/web/components/chat/home/SessionLoadingView.tsx b/web/components/chat/home/SessionLoadingView.tsx
index bcbf2ca8c..f2feb8c44 100644
--- a/web/components/chat/home/SessionLoadingView.tsx
+++ b/web/components/chat/home/SessionLoadingView.tsx
@@ -2,7 +2,7 @@
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
-import { Loader2, X } from "lucide-react";
+import { Loader2, RotateCw, TriangleAlert, X } from "lucide-react";
/**
* Indeterminate loading overlay shown while a chat session is fetched from
@@ -12,9 +12,17 @@ import { Loader2, X } from "lucide-react";
* The indicator is deliberately indeterminate: a session fetch reports no
* real progress, so a spinner is honest where a percentage bar would be
* fabricated. After a while we surface a reassurance hint.
+ *
+ * A load that fails or times out ends here too, as a terminal state with a
+ * retry: a conversation whose fetch did not arrive is not the same thing as
+ * one that is still arriving, and the difference has to be visible or the
+ * spinner becomes a lie the user cannot act on.
*/
interface SessionLoadingViewProps {
onCancel?: () => void;
+ /** Render the terminal failure state instead of the spinner. */
+ failed?: boolean;
+ onRetry?: () => void;
}
// After this long with no response, reassure the user it is still working.
@@ -22,14 +30,17 @@ const STILL_LOADING_AFTER_MS = 8000;
export default function SessionLoadingView({
onCancel,
+ failed = false,
+ onRetry,
}: SessionLoadingViewProps) {
const { t } = useTranslation();
const [showHint, setShowHint] = useState(false);
useEffect(() => {
+ if (failed) return;
const timer = setTimeout(() => setShowHint(true), STILL_LOADING_AFTER_MS);
return () => clearTimeout(timer);
- }, []);
+ }, [failed]);
return (
<div className="animate-fade-in relative flex h-full flex-col items-center justify-center gap-4 px-6">
@@ -45,7 +56,7 @@ export default function SessionLoadingView({
</button>
) : null}
- {/* Logo + spinner */}
+ {/* Logo + spinner (or the failure mark) */}
<div className="flex items-center gap-3">
<img
src="/logo_black.png"
@@ -55,14 +66,30 @@ export default function SessionLoadingView({
className="h-8 w-8 select-none"
draggable={false}
/>
- <Loader2 className="h-5 w-5 animate-spin text-[var(--primary)]" />
+ {failed ? (
+ <TriangleAlert className="h-5 w-5 text-[var(--destructive)]" />
+ ) : (
+ <Loader2 className="h-5 w-5 animate-spin text-[var(--primary)]" />
+ )}
</div>
{/* Primary message */}
<p className="text-sm font-medium text-[var(--foreground)]">
- {t("Loading conversation")}
+ {failed ? t("Failed to load session") : t("Loading conversation")}
</p>
+ {/* Terminal state: the one action that can still succeed */}
+ {failed && onRetry ? (
+ <button
+ type="button"
+ onClick={onRetry}
+ className="inline-flex items-center gap-2 rounded-lg border border-[var(--border)] px-3 py-1.5 text-[13px] font-medium text-[var(--foreground)] transition hover:bg-[var(--muted)]"
+ >
+ <RotateCw className="h-3.5 w-3.5" />
+ {t("Retry")}
+ </button>
+ ) : null}
+
{/* Slow-load hint */}
{showHint ? (
<p className="animate-fade-in text-[12px] text-[var(--muted-foreground)]">
diff --git a/web/lib/session-load.ts b/web/lib/session-load.ts
new file mode 100644
index 000000000..76942b5c9
--- /dev/null
+++ b/web/lib/session-load.ts
@@ -0,0 +1,37 @@
+/**
+ * When a session fetch ends without a session, what the user should see.
+ *
+ * Three different things abort a load and only one of them is a failure, so
+ * the decision is worth stating on its own: a newer load and the user's ✕ both
+ * own the state that replaces this one, while a timeout owns nothing and has
+ * to surface, or the overlay spins forever with no way out.
+ */
+
+/** How long to wait for a session fetch before calling it failed. */
+export const SESSION_LOAD_TIMEOUT_MS = 30_000;
+
+export interface LoadOutcome {
+ /** The load's AbortSignal fired. */
+ aborted: boolean;
+ /** …and it fired because the wait ran out, not because a caller cancelled. */
+ timedOut: boolean;
+ /** A copy of this session was already painted from memory. */
+ cached: boolean;
+}
+
+/**
+ * True when the failure must be shown as a terminal, retryable state.
+ *
+ * A background revalidate keeps the cached transcript on screen — replacing a
+ * readable conversation with an error because a refresh failed would be a
+ * regression, not a fix.
+ */
+export function shouldSurfaceLoadFailure({
+ aborted,
+ timedOut,
+ cached,
+}: LoadOutcome): boolean {
+ if (cached) return false;
+ if (aborted && !timedOut) return false;
+ return true;
+}
diff --git a/web/tests/session-load-failure.test.ts b/web/tests/session-load-failure.test.ts
new file mode 100644
index 000000000..6edf589db
--- /dev/null
+++ b/web/tests/session-load-failure.test.ts
@@ -0,0 +1,47 @@
+import test from "node:test";
+import assert from "node:assert/strict";
+import {
+ SESSION_LOAD_TIMEOUT_MS,
+ shouldSurfaceLoadFailure,
+} from "../lib/session-load";
+
+// Issue #912: a session fetch that never settled left the overlay spinning
+// with no way out but abandoning the conversation, and one that failed
+// replaced the URL with /home — dropping the session id, so a transient
+// error read as "my history is gone". Both must end in a retryable state.
+
+test("a plain failure on a cold open surfaces", () => {
+ assert.equal(
+ shouldSurfaceLoadFailure({ aborted: false, timedOut: false, cached: false }),
+ true,
+ );
+});
+
+test("a timeout surfaces even though it aborted the request itself", () => {
+ assert.equal(
+ shouldSurfaceLoadFailure({ aborted: true, timedOut: true, cached: false }),
+ true,
+ );
+});
+
+test("an abort that is not a timeout belongs to whoever cancelled it", () => {
+ // The user's ✕ and a newer load both set the state that replaces this one;
+ // reporting a failure here would overwrite it.
+ assert.equal(
+ shouldSurfaceLoadFailure({ aborted: true, timedOut: false, cached: false }),
+ false,
+ );
+});
+
+test("a failed background revalidate keeps the cached transcript on screen", () => {
+ for (const outcome of [
+ { aborted: false, timedOut: false, cached: true },
+ { aborted: true, timedOut: true, cached: true },
+ ]) {
+ assert.equal(shouldSurfaceLoadFailure(outcome), false);
+ }
+});
+
+test("the wait is bounded", () => {
+ assert.ok(SESSION_LOAD_TIMEOUT_MS > 0 && SESSION_LOAD_TIMEOUT_MS <= 60_000);
+});