Skip to content

The agent that grows with you

fix(desktop): keep the open chat bound across a gateway reconnect

Teknium381876c64293
diff --git a/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.test.ts b/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.test.ts
index b3fa07674db..e13ab26d5b9 100644
--- a/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.test.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.test.ts
@@ -633,7 +633,15 @@ describe('useSessionTileDelegate resumeTile', () => {
setSessions([row({ id: 'stored-c', profile: 'default' })])
const liveState = { busy: false, messages: [{ id: 'm1' }], storedSessionId: 'stored-c' }
- const runtimeIdByStoredSessionIdRef = { current: new Map([['stored-c', 'runtime-dead']]) }
+
+ const runtimeIdByStoredSessionIdRef = {
+ current: new Map([
+ ['stored-c', 'runtime-dead'],
+ // A canonical binding: the runtime id IS the durable id, which no backend can re-mint.
+ ['local-open', 'local-open']
+ ])
+ }
+
const sessionStateByRuntimeIdRef = { current: new Map([['runtime-dead', liveState]]) }
const requestGateway = vi.fn(async () => ({}) as never)
@@ -644,7 +652,7 @@ describe('useSessionTileDelegate resumeTile', () => {
// Gateway reconnect (what resetTileRuntimeBindings calls on wake):
sessionTileDelegate()!.invalidateRuntimeBindings!()
- expect(runtimeIdByStoredSessionIdRef.current.size).toBe(0)
+ expect([...runtimeIdByStoredSessionIdRef.current]).toEqual([['local-open', 'local-open']])
// The next resume goes cold instead of reusing the dead binding.
const runtimeId = await sessionTileDelegate()!.resumeTile('stored-c')
diff --git a/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.ts b/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.ts
index a1effde9c31..d03877689a9 100644
--- a/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.ts
@@ -306,10 +306,14 @@ export function useSessionTileDelegate({
// binding recorded pre-reconnect points at a runtime id the respawned
// backend no longer knows. Drop the map so resumeTile's warm path can't
// re-bind a tile to a dead runtime; live bindings re-record from
- // post-reconnect events and fresh resumes.
+ // post-reconnect events and fresh resumes. A binding whose runtime id IS
+ // the durable id (the canonical gateway's) cannot be re-minted or
+ // recycled into another chat, and the client re-attaches it on its next
+ // request; dropping it made the next send re-resume the open chat, which
+ // blanked its status stack and transcript until the resume answered.
invalidateRuntimeBindings: preserveStoredSessionIds => {
- for (const storedSessionId of runtimeIdByStoredSessionIdRef.current.keys()) {
- if (!preserveStoredSessionIds?.has(storedSessionId)) {
+ for (const [storedSessionId, runtimeId] of runtimeIdByStoredSessionIdRef.current) {
+ if (runtimeId !== storedSessionId && !preserveStoredSessionIds?.has(storedSessionId)) {
runtimeIdByStoredSessionIdRef.current.delete(storedSessionId)
}
}
diff --git a/apps/desktop/src/store/session-states.ts b/apps/desktop/src/store/session-states.ts
index 2937b77345b..93809ba4ecd 100644
--- a/apps/desktop/src/store/session-states.ts
+++ b/apps/desktop/src/store/session-states.ts
@@ -2089,17 +2089,17 @@ export function resetTileRuntimeBindings(
const preservedStoredIds = new Set(
tiles
.filter(
- // Any tile with an EXACT owner route — bot tabs always, and a
- // sessions tile whose opener stamped one (a branch child on its
- // parent's connection). Its runtime lives on that owner's socket,
- // not the ambient gateway, so an unrelated connection's reconnect
- // must not drop the binding: each drop re-arms the tile's resume,
- // and a flapping sibling connection turns that into 4+ re-resumes
- // inside the storm window — latching the "keeps losing its backend
- // runtime" card over a session that is actually healthy.
+ // A tile bound to its own durable id (canonical gateway) survives any
+ // reconnect: see invalidateRuntimeBindings. So does any tile with an
+ // EXACT owner route — bot tabs always, and a sessions tile whose opener
+ // stamped one (a branch child on its parent's connection): its runtime
+ // lives on that owner's socket, so an unrelated connection's reconnect
+ // must not drop the binding (each drop re-arms the tile's resume; a
+ // flapping sibling latched "keeps losing its backend runtime").
tile =>
- Boolean(tile.ownerRoute?.connectionId) &&
- (!(reconnected || liveConnectionIds) || !belongsToReconnectedRuntime(tile))
+ tile.runtimeId === tile.storedSessionId ||
+ (Boolean(tile.ownerRoute?.connectionId) &&
+ (!(reconnected || liveConnectionIds) || !belongsToReconnectedRuntime(tile)))
)
.map(tile => tile.storedSessionId)
)