fix(desktop): don't treat a mid-switch 404 as session deletion (#88540)

A resume racing a profile/connection swap can 404 on a backend that
does not own the session; the terminal-failure branch then dropped the
window to the blank new-chat route while the target session was alive.

goneSessionVerdict() now gates the draft fallback: a session created
this run, still listed on some profile, or looked up while a gateway
swap is in flight arms the bounded auto-retry latch instead of
discarding the route. Draft remains the calm-conditions path for
verifiably dead ids.
This commit is contained in:
Teknium
2026-08-17 15:15:47 -07:00
parent 8484786832
commit d0b02efa9d
3 changed files with 88 additions and 6 deletions
@@ -13,9 +13,16 @@ import { setSessionYolo } from '@/lib/yolo-session'
import { normalizeChoices, setClarifyRequest } from '@/store/clarify'
import { migrateSessionDraft } from '@/store/composer'
import { clearQueuedPrompts, migrateQueuedPrompts } from '@/store/composer-queue'
import { $gatewaySwitching } from '@/store/gateway-switch'
import { $pinnedSessionIds } from '@/store/layout'
import { clearNotifications, notify, notifyError } from '@/store/notifications'
import { $activeGatewayProfile, $newChatProfile, ensureGatewayProfile, normalizeProfileKey } from '@/store/profile'
import {
$activeGatewayProfile,
$gatewaySwapTarget,
$newChatProfile,
ensureGatewayProfile,
normalizeProfileKey
} from '@/store/profile'
import {
beginSessionMutation,
endSessionMutation,
@@ -85,6 +92,7 @@ import {
type BranchMessage,
chatMessageArraysEquivalent,
dedupeInflightUserAgainstTranscript,
goneSessionVerdict,
isSessionGoneError,
overlayConcurrentMessageChanges,
patchSessionWorkspace,
@@ -1316,11 +1324,39 @@ export function useSessionActions({
// permanently-dead id. (Booting straight into a no-longer-existent
// last-session id is the common trigger.)
if ($messages.get().length === 0 && isSessionGoneError(fallbackError)) {
// A session created THIS run isn't gone — its backend just flapped
// before the turn-less session persisted. Keep the empty view and arm
// the bounded retry to rebind, rather than yanking to a fresh draft.
// Only a stale id from a PRIOR run drops to a draft.
if (createdThisRun.has(storedSessionId)) {
// A 404 is only trustworthy from the backend that OWNS the session.
// A cross-profile open (Bots pane) races the gateway swap, so both
// lookups can land on a backend that never heard of the id (#88540).
// Re-resolve before discarding: a row still listed on any profile —
// or a swap in flight — means "retry once things settle", not gone.
let stillListed = false
try {
stillListed = Boolean(await resolveStoredSession(storedSessionId))
} catch {
// Resolution itself failed — inconclusive, treat as not listed.
}
if (!isCurrentResume()) {
return
}
const verdict = goneSessionVerdict({
createdThisRun: createdThisRun.has(storedSessionId),
stillListed,
switchInFlight:
$gatewaySwitching.get() ||
Boolean($gatewaySwapTarget.get()) ||
// Known owner ≠ active gateway: the 404 came from the wrong
// backend. An UNKNOWN owner must not count — it would block the
// draft fallback for genuinely dead ids on secondary profiles.
Boolean(
sessionProfile?.trim() &&
normalizeProfileKey(sessionProfile) !== normalizeProfileKey($activeGatewayProfile.get())
)
})
if (verdict === 'retry') {
setResumeFailedSessionId(storedSessionId)
return
@@ -23,6 +23,7 @@ import {
chatMessagesEquivalent,
chatPartsEquivalent,
dedupeInflightUserAgainstTranscript,
goneSessionVerdict,
isSessionGoneError,
overlayConcurrentMessageChanges,
preserveLocalPendingTurnMessages,
@@ -239,6 +240,24 @@ describe('isSessionGoneError', () => {
})
})
describe('goneSessionVerdict', () => {
it('drafts only when the id is verifiably gone in calm conditions', () => {
expect(goneSessionVerdict({ createdThisRun: false, stillListed: false, switchInFlight: false })).toBe('draft')
})
it('retries when a profile/connection switch is in flight (#88540 route revert)', () => {
expect(goneSessionVerdict({ createdThisRun: false, stillListed: false, switchInFlight: true })).toBe('retry')
})
it('retries when the session is still listed on some profile', () => {
expect(goneSessionVerdict({ createdThisRun: false, stillListed: true, switchInFlight: false })).toBe('retry')
})
it('never discards a session created by this window in this run', () => {
expect(goneSessionVerdict({ createdThisRun: true, stillListed: false, switchInFlight: false })).toBe('retry')
})
})
describe('sessionMatchesStoredId', () => {
it('matches on live id or lineage root', () => {
expect(sessionMatchesStoredId(session({ id: 'a' }), 'a')).toBe(true)
@@ -1577,6 +1577,33 @@ export function isSessionGoneError(err: unknown): boolean {
return message.includes('404') || /session not found/i.test(message)
}
/**
* What to do when a resume's RPC and REST fallback BOTH came back
* gone-looking (#88540).
*
* A 404 is only proof of deletion when it came from the backend that owns
* the session. During (or moments after) a profile/connection switch the
* request can land on a backend that has never heard of the id — the
* cross-profile Bots-pane open is the reproducer: the route is written
* correctly, the resume races the gateway swap, both lookups 404 on the
* wrong backend, and the "genuinely gone" branch yanks the window to the
* blank new-chat route while the target session is perfectly alive.
*
* `'retry'` keeps the route and arms the bounded auto-retry (which re-runs
* the resume once the swap settles); `'draft'` is reserved for a session
* that is verifiably gone in calm conditions.
*/
export function goneSessionVerdict(options: {
/** The session was created by this window in this run — never discard. */
createdThisRun: boolean
/** A post-failure re-resolve still finds the row on SOME profile. */
stillListed: boolean
/** A profile swap or connection switch is in flight (or just targeted). */
switchInFlight: boolean
}): 'draft' | 'retry' {
return options.createdThisRun || options.stillListed || options.switchInFlight ? 'retry' : 'draft'
}
/**
* The busy value a resume/activate response should land with (#70449).
*