From 32b88bcdc55a91ec6d7439d219b30e64df5c8827 Mon Sep 17 00:00:00 2001 From: "buf0-bot[bot]" <252831055+buf0-bot[bot]@users.noreply.github.com> Date: Thu, 7 May 2026 23:11:06 -0700 Subject: [PATCH] fix: pr-bug-scan findings from #1481 (#1566) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Split session screen's clearTerminalCache cleanup into a mount-only effect; included client identity in home-screen wireUp dep key via a WeakMap-derived clientKey so forceReconnect re-attaches subs. Findings addressed: - mobile/app/h/[hostId]/session/[worktreeId].tsx:436 — clearTerminalCache fires on every client identity change - mobile/app/index.tsx — useEffect deps array uses inline expression — runs every render Rebased onto current main. Co-authored-by: orca-bot --- .../app/h/[hostId]/session/[worktreeId].tsx | 10 +++++++- mobile/app/index.tsx | 25 ++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/mobile/app/h/[hostId]/session/[worktreeId].tsx b/mobile/app/h/[hostId]/session/[worktreeId].tsx index c1be03e96..3ddf7c523 100644 --- a/mobile/app/h/[hostId]/session/[worktreeId].tsx +++ b/mobile/app/h/[hostId]/session/[worktreeId].tsx @@ -578,10 +578,18 @@ export default function SessionScreen() { // (clientRef.current.sendRequest...) keep working without churn. useEffect(() => { clientRef.current = client + }, [client]) + + // Why: only clear terminal cache on actual unmount. Running it whenever + // `client` changes — including the initial null → real-client transition + // from useHostClient's async open path — would unsubscribe terminals and + // wipe xterm state mid-subscribe on a normal session-screen mount. + useEffect(() => { return () => { clearTerminalCache() } - }, [client, clearTerminalCache]) + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []) // Why: deviceToken is read from host record so feature code can pass // `client.id` on subscribe/send for driver-state-machine identity. diff --git a/mobile/app/index.tsx b/mobile/app/index.tsx index d25d4b04b..1981d24db 100644 --- a/mobile/app/index.tsx +++ b/mobile/app/index.tsx @@ -88,6 +88,21 @@ function formatDuration(ms: number): string { return `${totalMinutes}m` } +// Why: derive a stable per-instance identity for RpcClient so the wireUp +// effect's dep key changes when forceReconnect swaps the underlying client +// for a host (without this, listeners stay attached to the closed client +// and notifications/accounts subs never re-attach). +const clientIdentities = new WeakMap() +let nextClientIdentity = 1 +function clientKey(client: RpcClient): number { + let id = clientIdentities.get(client) + if (id == null) { + id = nextClientIdentity++ + clientIdentities.set(client, id) + } + return id +} + function fetchStats( client: RpcClient, setStats: (s: StatsSummary) => void, @@ -423,14 +438,16 @@ export default function HomeScreen() { return () => { for (const c of cleanups) c() } - // Why: depend on the host-id set, not the whole allClients array, so + // Why: depend on the host-id set AND each entry's client identity, so // resubscriptions don't fire on every render that produces a new - // array reference. Client identity is stable per hostId for the - // lifetime of the underlying transport. + // array reference, but DO fire when forceReconnect swaps the + // underlying client for a host (otherwise wireUp would keep firing + // on a closed client and never re-attach to the fresh one, leaving + // notifications/accounts subs broken until the user navigates). // eslint-disable-next-line react-hooks/exhaustive-deps }, [ allClients - .map((e) => e.hostId) + .map((e) => `${e.hostId}:${clientKey(e.client)}`) .sort() .join(',') ])