fix(mobile): refetch host agent lists on focus and land on the new terminal tab

Merged
SA
3mo ago
+96-47

What & why

Two composer/session bugs:

Agent list never updates. React Query on native cannot see app focus unless focusManager is wired to AppState — it never was, so no query in the app ever refetched on foregrounding. The composer's agent list (fetched once at launch for the default target, usually your own machine) stayed frozen until an app restart. This wires focusManager in RootLayout, and moves the three copy-pasted host-agent-config queries into one useHostAgentConfigs hook with refetchOnWindowFocus: "always" — mirroring desktop's useV2AgentConfigs reasoning: agent edits happen on another machine while this app is backgrounded, so refocusing is the earliest moment the fresh list can matter.

New terminal doesn't activate. The workspace + sheet lands back via dismissTo with the new session in ?tab= before the terminals query has heard of the session. The adopt effect set the fresh id, but the pin-active effect (added so sessions-sheet reordering doesn't switch the watched terminal) immediately re-asserted the old tab, and by the time the row arrived the adoption was already lost — the new session never activated. The two effects are now one: a ?tab= is adopted exactly once, when its row arrives, and outranks the pin within the same commit.

How I tested it

  • bun test in apps/mobile (37 pass)
  • bun run lint clean; mobile typecheck errors identical to main's known shared-package leaks (none in app code)

Summary by cubic

Fixes two mobile session bugs: agent lists now refresh when the app regains focus, and a newly created terminal activates its tab once the row appears; a manual tab pick now cancels that pending auto-adoption. Previously lists stayed frozen after backgrounding and the new tab often failed to activate.

  • Wire @tanstack/react-query focusManager to AppState in RootLayout so queries refetch on foreground; this unconditionally refetches on focus and may increase network calls.
  • Replace three copy-pasted agent-config queries with a shared useHostAgentConfigs hook (refetchOnWindowFocus: "always", networkMode: "always") and a stable getHostAgentConfigsQueryKey; adopt in NewChatWidget, AgentPickerScreen, and NewSessionSheet; skip cloud targets.
  • In WorkspaceScreen, merge tab adoption and pinning into one effect: adopt ?tab= exactly once when its row arrives; a manual tab pick consumes a pending adoption; then pin the active tab to prevent reordering from switching focus.

Summary by CodeRabbit

  • New Features

    • Added consistent loading of host agent presets across chat and session creation flows.
    • Agent configurations refresh when returning to the app or changing host connections.
    • Improved synchronization of app data when the mobile app becomes active.
  • Bug Fixes

    • Improved deep-linked workspace tabs so manual terminal selections are preserved.
    • Prevented agent configuration requests for unsupported cloud targets.