Files
DeepTutor/web/lib/session-load.ts
Bingxi Zhao (Frank) 7cc213f24d fix(chat): end a stuck session load in a retryable state
#923 fixed the blank transcript in #912 — an orphaned message tree. The
same report's other screenshot is a different failure that survived it: the
overlay stuck on "Loading conversation / Still loading…" with the request
already answered in devtools.

Two paths led there, and both told the user their history was gone:

* The wait was unbounded ("the overlay stays until the API responds (no
  timeout)"). A fetch that never settled span forever, and the only exit was
  ✕ — which abandons the conversation. The wait is now bounded, and the 8s
  hint is no longer the last thing that ever happens.
* A cold open that *failed* ran router.replace("/home"), dropping the
  session id from the URL. A transient error was indistinguishable from a
  lost conversation, and the id needed to retry was gone.

Both now end in one terminal, retryable state with the id still in the URL:
the overlay shows the failure and a Retry, ✕ still means "leave". A failed
background revalidate is unchanged — the cached transcript stays on screen
rather than being replaced by an error.

Which of the three aborts counts as a failure is the whole subtlety (a
newer load and the user's ✕ own the state that replaces this one; a timeout
owns nothing), so that decision lives in a tested predicate rather than
inline in the component.
2026-08-20 00:56:34 +08:00

38 lines
1.2 KiB
TypeScript

/**
* 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;
}