fix(mobile): probe network changes and refresh activity leases #5154
Closedt3dotgg(GitHub profile, opens in a new tab)wants to merge 4 commits into
main fromt3code/mobile-reconnect-hardeningopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- packages/client-runtime/src/state/shell.ts line 227OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -187,3 +187,15 @@187187 yield* Ref.set(awaitingCompletion, supportsCompletionMarker);188188 yield* setSynchronizing;189189190+ // A warm snapshot resumes via afterSequence alone; the server replays191+ // the gap or falls back to a fresh socket snapshot when it is too192+ // large (SHELL_RESUME_MAX_GAP), so the HTTP fetch is only needed on a193+ // truly cold start. This mirrors the thread-detail guard and keeps194+ // foreground resubscribes off the HTTP path.195+ const cached = yield* SubscriptionRef.get(state);196+ if (Option.isSome(cached.snapshot)) {197+ return {198+ afterSequence: cached.snapshot.value.snapshotSequence,199+ ...(supportsCompletionMarker ? { requestCompletionMarker: true as const } : {}),200+ };201+ }packages/client-runtime/src/connection/supervisor.ts line 733OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -710,4 +723,3 @@710723 }711724712725 failureCount += 1;713- const delayMs = retryDelayMs(failureCount - 1);apps/mobile/src/connection/platform.ts line 130Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -102,2 +104,21 @@102104 }103105 });106+ // WiFi <-> cellular keeps isConnected true while invalidating the107+ // socket's path, so the coarse online/offline signal never fires.108+ // Emit an advisory wakeup on interface-type changes while active;109+ // the supervisor probes the session rather than blindly replacing110+ // it, which keeps flapping paths cheap.111+ let networkType: string | null = null;112+ const networkSubscription = Network.addNetworkStateListener((state) => {113+ const nextType = state.type ?? null;114+ const previousType = networkType;115+ networkType = nextType;116+ if (117+ previousType !== null &&118+ nextType !== null &&119+ nextType !== previousType &&120+ state.isConnected === true &&121+ AppState.currentState === "active"122+ ) {123+ Queue.offerUnsafe(queue, "network-path-changed");124+ }
- apps/mobile/src/connection/background-activity.ts lines 131 to 131OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -105,0 +111,21 @@111+ registry.followStream(112+ environmentId,113+ Stream.unwrap(114+ Effect.map(EnvironmentSupervisor, (supervisor) =>115+ // The generation dedup must live inside the per-supervisor stream:116+ // a replacement supervisor restarts generations at 1, so deduping117+ // outside followStream would suppress its first connected event.118+ SubscriptionRef.changes(supervisor.state).pipe(119+ Stream.filter((state) => state.phase === "connected"),120+ Stream.map((state) => state.generation),121+ Stream.changes,122+ ),123+ ),124+ ),125+ );126+ yield* Stream.concat(127+ Stream.fromEffect(SubscriptionRef.get(registry.entries)),128+ SubscriptionRef.changes(registry.entries),129+ ).pipe(130+ Stream.map((entries) => [...entries.keys()].sort()),131+ Stream.changesWith((a, b) => a.join(",") === b.join(",")),apps/mobile/src/connection/background-activity.ts line 137OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -105,0 +117,21 @@117+ // outside followStream would suppress its first connected event.118+ SubscriptionRef.changes(supervisor.state).pipe(119+ Stream.filter((state) => state.phase === "connected"),120+ Stream.map((state) => state.generation),121+ Stream.changes,122+ ),123+ ),124+ ),125+ );126+ yield* Stream.concat(127+ Stream.fromEffect(SubscriptionRef.get(registry.entries)),128+ SubscriptionRef.changes(registry.entries),129+ ).pipe(130+ Stream.map((entries) => [...entries.keys()].sort()),131+ Stream.changesWith((a, b) => a.join(",") === b.join(",")),132+ Stream.switchMap(133+ (environmentIds) =>134+ Stream.mergeAll(environmentIds.map(connectedGenerations), {135+ concurrency: "unbounded",136+ }),137+ { concurrency: "unbounded" },
- @@ -875,0 +917,21 @@917+ const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {918+ initiallyDesired: true,919+ }).pipe(Effect.provide(harness.dependencies));920+921+ yield* awaitState(922+ supervisor.state,923+ (state) => state.phase === "backoff" && state.attempt === 1,924+ );925+ expect(yield* Ref.get(harness.prepareCount)).toBe(1);926+927+ // Advisory path-change wakeups have no session to probe during backoff928+ // and must not trigger an early retry.929+ yield* harness.wake("network-path-changed");930+ yield* harness.wake("network-path-changed");931+ for (let attempt = 0; attempt < 20; attempt += 1) {932+ yield* Effect.yieldNow;933+ }934+ expect(yield* Ref.get(harness.prepareCount)).toBe(1);935+ expect((yield* SubscriptionRef.get(supervisor.state)).phase).toBe("backoff");936+ }),937+ );@@ -328,18 +403,17 @@328403 }329-330- expect(yield* Ref.get(loaderCalls)).toBe(2);404+ expect(yield* Ref.get(loaderCalls)).toBe(0);331405 expect(yield* Ref.get(subscriptionCount)).toBe(2);332406333407 yield* Queue.offer(wakeups, "application-active-probe");334408 for (let attempt = 0; attempt < 100; attempt += 1) {335409 if ((yield* Ref.get(subscriptionCount)) >= 3) break;336410 yield* Effect.yieldNow;337411 }338- expect(yield* Ref.get(loaderCalls)).toBe(3);412+ expect(yield* Ref.get(loaderCalls)).toBe(0);339413 expect(yield* Ref.get(subscriptionCount)).toBe(3);340414341415 yield* Queue.offer(wakeups, "application-active-reconnect");342416 for (let attempt = 0; attempt < 10; attempt += 1) {343417 yield* Effect.yieldNow;344418 }345- expect(yield* Ref.get(loaderCalls)).toBe(3);419+ expect(yield* Ref.get(loaderCalls)).toBe(0);
t3dotgg(GitHub profile, opens in a new tab) changed the title
- apps/mobile/src/connection/platform.ts line 113OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -101,3 +103,11 @@101103 backgroundedAtMs = null;102104 }103105 });106+ // WiFi <-> cellular keeps isConnected true while invalidating the107+ // socket's path, so the coarse online/offline signal never fires.108+ // Emit an advisory wakeup on interface-type changes while active;109+ // the supervisor probes the session rather than blindly replacing110+ // it, which keeps flapping paths cheap. Seed the current type so111+ // the first flip after startup is detected; the listener only112+ // reports changes.113+ let networkType: string | null = null;
- apps/server/src/httpServerOptions.ts lines 2 to 4OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,4 @@1+export const bunWebSocketOptions = {2+ // Negotiate permessage-deflate with clients that offer it. A dedicated3+ // compressor keeps a per-connection sliding window, while the shared4+ // decompressor avoids uWebSockets' dedicated-decompressor aborts.
t3dotgg(GitHub profile, opens in a new tab) changed the title
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


🚥 Pre-merge checks | ✅ 3 | ❌ 2
❌ Failed checks (2 warnings)#
✅ Passed checks (3 passed)
Full details: Description check
Explanation
The description explains the main mobile issue, the intended fix, testing, limitations, and lack of UI changes. However, it omits several changes in the pull request, including warm shell resume, jittered backoff, RPC ping-timeout logging, and the 30-second Bun WebSocket idle timeout. It also conflicts with the changeset by stating that the Bun idle-timeout change was removed.
Resolution
Update the description using the repository template. Add explicit What Changed, Why, UI Changes, and Checklist sections. Document all current changes, including warm shell resume, jittered reconnect backoff, network-path wakeups, activity reporting, RPC ping-timeout logging, and the 30-second Bun WebSocket idle timeout. Resolve the conflicting statements about retry delays and the Bun idle timeout.
Comment `@coderabbitai help` to get the list of available commands.