Skip to content

The agent that grows with you

fix(desktop): open a branch child whose route lands after its resume starts

Tekniumfdad262d15a7
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx b/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
index 1e045b1fe06..b50f81ffd21 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
@@ -377,6 +377,77 @@ describe('desktop branch creation idempotency', () => {
expect($sessions.get().filter(session => session.id === 'stored-pandora')).toHaveLength(1)
expect($sessions.get().filter(session => session.id === 'stored-other-box')).toHaveLength(1)
})
+
+ it('opens the branch child even when the router lands on its route after the resume started', async () => {
+ // Branching the open chat navigates to the child and resumes it in the same
+ // tick. The router commits the child route only on its next render, so the
+ // resume starts on the PARENT's route token and then sees the child's. That
+ // change is the navigation this resume is for, not a user leaving: the
+ // child must still be attached, or "Waking up…" never clears.
+ let routeToken = 'route:parent'
+ let routedId: null | string = 'parent'
+
+ const navigate = vi.fn((to: string) => {
+ queueMicrotask(() => {
+ routeToken = `route:${to}`
+ routedId = to === sessionRoute('stored-branch') ? 'stored-branch' : null
+ })
+ })
+
+ const requestGateway = vi.fn(async (method: string) =>
+ (method === 'session.branch_stored'
+ ? { session_id: 'runtime-branch', stored_session_id: 'stored-branch' }
+ : {}) as never
+ )
+
+ // The child's resume is profile-routed (resolveStoredSession stamps it).
+ vi.mocked(requestGatewayForProfile).mockImplementation((async (_profile: string, method: string) =>
+ (method === 'session.resume'
+ ? {
+ info: {},
+ message_count: 0,
+ messages: [],
+ resumed: 'stored-branch',
+ session_id: 'runtime-branch',
+ session_key: 'stored-branch'
+ }
+ : {}) as never) as never)
+
+ let actions: HarnessHandle | null = null
+
+ setSessions([storedSession({ id: 'parent', message_count: 2, title: 'Parent' })])
+
+ render(
+ <Harness
+ getRoutedStoredSessionId={() => routedId}
+ getRouteToken={() => routeToken}
+ navigate={navigate}
+ onReady={value => (actions = value)}
+ requestGateway={requestGateway}
+ selectedStoredSessionId="parent"
+ />
+ )
+ await waitFor(() => expect(actions).not.toBeNull())
+
+ await act(async () => {
+ await expect(actions!.branchStoredSession('parent')).resolves.toBe(true)
+ })
+
+ expect(navigate).toHaveBeenCalledWith(sessionRoute('stored-branch'), { replace: true })
+ // The resume dispatches on the child's profile socket.
+ await waitFor(() =>
+ expect(
+ vi
+ .mocked(requestGatewayForProfile)
+ .mock.calls.filter(([, method, params]) => method === 'session.resume' && params?.session_id === 'stored-branch')
+ ).toHaveLength(1)
+ )
+ await waitFor(() => expect($activeSessionId.get()).toBe('runtime-branch'))
+ expect($resumeFailedSessionId.get()).toBeNull()
+ vi.mocked(requestGatewayForProfile).mockReset()
+ setSelectedStoredSessionId(null)
+ setActiveSessionId(null)
+ })
})
describe('connection-qualified session deletion', () => {
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
index 6971b71e599..8f0d3398c2a 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
@@ -1331,10 +1331,15 @@ export function useSessionActions({
resumeRequestRef.current = requestId
const resumedSameSelectedSession = selectedStoredSessionIdRef.current === storedSessionId
+ // A route change is drift only when it leads AWAY from this session. A
+ // caller that navigates and resumes in one tick (branch → child) starts
+ // here on the previous route; the router commits the target route a
+ // render later, and treating that as drift abandoned the child behind a
+ // "Waking up…" overlay that never cleared.
const isCurrentResume = () =>
resumeRequestRef.current === requestId &&
selectedStoredSessionIdRef.current === storedSessionId &&
- getRouteToken() === routeToken
+ (getRouteToken() === routeToken || getRoutedStoredSessionId() === storedSessionId)
// A reconnect re-resumes the runtime this view is streaming. Let its
// replay land while that runtime still owns the view. Otherwise the REST
@@ -1543,9 +1548,11 @@ export function useSessionActions({
// dial the owning backend without moving $activeGatewayProfile.
if ($showAllProfiles.get()) {
if (resolvedConnectionId) {
- await openGatewayForAgent(resolvedConnectionId, ownerRoute?.profile || sessionProfile || 'default')
+ await openGatewayForAgent(resolvedConnectionId, ownerRoute?.profile || sessionProfile || 'default', {
+ spawnPriority: 'foreground'
+ })
} else if (sessionProfile) {
- await openGatewayForProfile(normalizeProfileKey(sessionProfile))
+ await openGatewayForProfile(normalizeProfileKey(sessionProfile), { spawnPriority: 'foreground' })
}
} else if (resolvedConnectionId) {
await ensureGatewayAgent(resolvedConnectionId, ownerRoute?.profile || sessionProfile || 'default')
@@ -2759,6 +2766,7 @@ export function useSessionActions({
busyRef,
copy,
getRouteToken,
+ getRoutedStoredSessionId,
holdSessionTranscriptView,
requestGateway,
resetViewSync,