Skip to content

Commit 06cbfd6

Browse files
perf(mobile): cold starts feel faster and reconnect more quietly
Remember the route that last worked, hedge hanging routes in parallel, and keep cached threads on screen with a quiet Updating label instead of a loud reconnect banner. Co-authored-by: Cursor <[email protected]>
1 parent 83a3674 commit 06cbfd6

9 files changed

Lines changed: 420 additions & 26 deletions

File tree

‎apps/mobile/src/features/home/workspace-connection-status.test.ts‎

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,157 @@ describe("workspace connection status", () => {
7878
showsProgress: true,
7979
});
8080
});
81+
82+
it("keeps a quiet Updating label while reconnecting over a cached shell", () => {
83+
const state = workspaceState({
84+
hasReadyEnvironment: false,
85+
hasConnectingEnvironment: true,
86+
hasLoadedShellSnapshot: true,
87+
connectionState: "connecting",
88+
connectingEnvironments: [
89+
{
90+
environmentId: "environment-1" as never,
91+
environmentLabel: "MacBook",
92+
displayUrl: "",
93+
isRelayManaged: false,
94+
isEnabled: true,
95+
connectionState: "connecting",
96+
connectionError: null,
97+
connectionErrorTraceId: null,
98+
},
99+
],
100+
});
101+
102+
expect(workspaceConnectionStatusPresentation(state)).toEqual({
103+
label: "Updating...",
104+
showsProgress: true,
105+
});
106+
});
107+
108+
it("names the environment when reconnecting without a cached shell", () => {
109+
const state = workspaceState({
110+
hasReadyEnvironment: false,
111+
hasConnectingEnvironment: true,
112+
hasLoadedShellSnapshot: false,
113+
connectionState: "connecting",
114+
connectingEnvironments: [
115+
{
116+
environmentId: "environment-1" as never,
117+
environmentLabel: "MacBook",
118+
displayUrl: "",
119+
isRelayManaged: false,
120+
isEnabled: true,
121+
connectionState: "connecting",
122+
connectionError: null,
123+
connectionErrorTraceId: null,
124+
},
125+
],
126+
});
127+
128+
expect(workspaceConnectionStatusPresentation(state)).toEqual({
129+
label: "Reconnecting to MacBook",
130+
showsProgress: true,
131+
});
132+
});
133+
134+
it("names the environment when a cached reconnect has an error", () => {
135+
const state = workspaceState({
136+
hasReadyEnvironment: false,
137+
hasConnectingEnvironment: true,
138+
hasLoadedShellSnapshot: true,
139+
connectionState: "reconnecting",
140+
connectionError: "Could not reach MacBook",
141+
connectingEnvironments: [
142+
{
143+
environmentId: "environment-1" as never,
144+
environmentLabel: "MacBook",
145+
displayUrl: "",
146+
isRelayManaged: false,
147+
isEnabled: true,
148+
connectionState: "reconnecting",
149+
connectionError: "Could not reach MacBook",
150+
connectionErrorTraceId: null,
151+
},
152+
],
153+
});
154+
155+
expect(workspaceConnectionStatusPresentation(state)).toEqual({
156+
label: "Reconnecting to MacBook",
157+
showsProgress: false,
158+
});
159+
});
160+
161+
it("ignores a failing second environment while a hopeful one is connecting over cache", () => {
162+
const state = workspaceState({
163+
hasReadyEnvironment: false,
164+
hasConnectingEnvironment: true,
165+
hasLoadedShellSnapshot: true,
166+
connectionState: "reconnecting",
167+
connectionError: "Could not reach sleeping laptop",
168+
connectingEnvironments: [
169+
{
170+
environmentId: "environment-1" as never,
171+
environmentLabel: "MacBook",
172+
displayUrl: "",
173+
isRelayManaged: false,
174+
isEnabled: true,
175+
connectionState: "connecting",
176+
connectionError: null,
177+
connectionErrorTraceId: null,
178+
},
179+
{
180+
environmentId: "environment-2" as never,
181+
environmentLabel: "Sleeping laptop",
182+
displayUrl: "",
183+
isRelayManaged: false,
184+
isEnabled: true,
185+
connectionState: "reconnecting",
186+
connectionError: "Could not reach sleeping laptop",
187+
connectionErrorTraceId: null,
188+
},
189+
],
190+
});
191+
192+
expect(workspaceConnectionStatusPresentation(state)).toEqual({
193+
label: "Updating...",
194+
showsProgress: true,
195+
});
196+
});
197+
198+
it("names only the hopeful environment when reconnecting without a cache", () => {
199+
const state = workspaceState({
200+
hasReadyEnvironment: false,
201+
hasConnectingEnvironment: true,
202+
hasLoadedShellSnapshot: false,
203+
connectionState: "reconnecting",
204+
connectionError: "Could not reach sleeping laptop",
205+
connectingEnvironments: [
206+
{
207+
environmentId: "environment-1" as never,
208+
environmentLabel: "MacBook",
209+
displayUrl: "",
210+
isRelayManaged: false,
211+
isEnabled: true,
212+
connectionState: "connecting",
213+
connectionError: null,
214+
connectionErrorTraceId: null,
215+
},
216+
{
217+
environmentId: "environment-2" as never,
218+
environmentLabel: "Sleeping laptop",
219+
displayUrl: "",
220+
isRelayManaged: false,
221+
isEnabled: true,
222+
connectionState: "reconnecting",
223+
connectionError: "Could not reach sleeping laptop",
224+
connectionErrorTraceId: null,
225+
},
226+
],
227+
});
228+
229+
expect(workspaceConnectionStatusPresentation(state)).toEqual({
230+
label: "Reconnecting to MacBook",
231+
showsProgress: true,
232+
});
233+
});
81234
});

‎apps/mobile/src/features/home/workspace-connection-status.ts‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,32 @@ function shouldShowWorkspaceConnectionStatus(state: WorkspaceState): boolean {
1616
);
1717
}
1818

19+
function hopefulConnectingEnvironments(state: WorkspaceState) {
20+
// A sleeping second machine should not steal the header from one that may still connect.
21+
const hopeful = state.connectingEnvironments.filter(
22+
(environment) => environment.connectionError === null,
23+
);
24+
return hopeful.length > 0 ? hopeful : state.connectingEnvironments;
25+
}
26+
1927
function workspaceConnectionStatusLabel(state: WorkspaceState): string {
2028
if (state.networkStatus === "offline") return "You are offline";
2129
if (state.hasReadyEnvironment && state.hasSyncingConnectedEnvironment) {
2230
return state.hasLoadedShellSnapshot ? "Syncing threads..." : "Loading threads...";
2331
}
24-
if (state.connectingEnvironments.length === 1) {
25-
return `Reconnecting to ${state.connectingEnvironments[0]!.environmentLabel}`;
32+
const connecting = hopefulConnectingEnvironments(state);
33+
// Cached threads stay on screen; keep the header quiet while the socket catches up.
34+
if (
35+
state.hasLoadedShellSnapshot &&
36+
connecting.some((environment) => environment.connectionError === null)
37+
) {
38+
return "Updating...";
39+
}
40+
if (connecting.length === 1) {
41+
return `Reconnecting to ${connecting[0]!.environmentLabel}`;
2642
}
27-
if (state.connectingEnvironments.length > 1) {
28-
return `Reconnecting ${state.connectingEnvironments.length} environments`;
43+
if (connecting.length > 1) {
44+
return `Reconnecting ${connecting.length} environments`;
2945
}
3046
if (state.connectionError !== null) return state.connectionError;
3147
if (state.hasPendingShellSnapshot) {
@@ -38,11 +54,13 @@ export function workspaceConnectionStatusPresentation(
3854
state: WorkspaceState,
3955
): WorkspaceConnectionStatusPresentation | null {
4056
if (!shouldShowWorkspaceConnectionStatus(state)) return null;
57+
const hopeful = hopefulConnectingEnvironments(state);
58+
const hasHopefulConnect = hopeful.some((environment) => environment.connectionError === null);
4159
return {
4260
label: workspaceConnectionStatusLabel(state),
4361
showsProgress:
4462
state.networkStatus !== "offline" &&
45-
(state.hasReadyEnvironment || state.connectionError === null) &&
63+
(state.hasReadyEnvironment || state.connectionError === null || hasHopefulConnect) &&
4664
(state.connectingEnvironments.length > 0 || state.hasPendingShellSnapshot),
4765
};
4866
}

‎packages/client-runtime/src/connection/driver.ts‎

Lines changed: 93 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import * as Context from "effect/Context";
2+
import * as Deferred from "effect/Deferred";
23
import * as Effect from "effect/Effect";
34
import * as Exit from "effect/Exit";
45
import * as Fiber from "effect/Fiber";
56
import * as Layer from "effect/Layer";
7+
import * as Option from "effect/Option";
8+
import * as Ref from "effect/Ref";
9+
import * as Result from "effect/Result";
610
import * as Scope from "effect/Scope";
711
import * as FetchHttpClient from "effect/http/FetchHttpClient";
812
import * as HttpClient from "effect/http/HttpClient";
@@ -49,6 +53,9 @@ const ROUTE_CHECK_TIMEOUT_MS = 2_500;
4953
// wait the full probe timeout for a LAN the device has already left. A route
5054
// that misses it connects over relay, then upgrades with a second reconnect.
5155
const ROUTE_CHECK_HEAD_START_MS = 500;
56+
// Give the preferred ready route a short solo window before parallel backups
57+
// start, so a fast LAN does not also open a relay session.
58+
const ROUTE_CONNECT_HEDGE_MS = 250;
5259

5360
export class ConnectionDriver extends Context.Service<
5461
ConnectionDriver,
@@ -93,10 +100,86 @@ export const connectOverRoutes = Effect.fn("ConnectionDriver.connectOverRoutes")
93100
if (result._tag === "Failure") {
94101
if (result.failure._tag === "ConnectionTransientError") transient ??= result.failure;
95102
else blocked ??= result.failure;
103+
return { result, routeScope } as const;
96104
}
105+
return { result, routeScope } as const;
106+
});
107+
108+
// First success wins. Later successes close their scopes so a hedged backup
109+
// cannot leave a second live session behind. A finished failure starts the
110+
// next route immediately; only a still-running preferred attempt waits out
111+
// the hedge before opening a parallel backup.
112+
const raceAttempts = Effect.fnUntraced(function* (candidates: ReadonlyArray<ConnectionRoute>) {
113+
if (candidates.length === 0) {
114+
return undefined as
115+
| Result.Result<EnvironmentConnectionLease, ConnectionAttemptError>
116+
| undefined;
117+
}
118+
if (candidates.length === 1) {
119+
const { result } = yield* attempt(candidates[0]!);
120+
return result;
121+
}
122+
const decided =
123+
yield* Deferred.make<Result.Result<EnvironmentConnectionLease, ConnectionAttemptError>>();
124+
const remaining = yield* Ref.make(candidates.length);
125+
const fibers: Array<Fiber.Fiber<unknown, never>> = [];
126+
let previous = Option.none<Fiber.Fiber<unknown, never>>();
127+
128+
for (const [index, route] of candidates.entries()) {
129+
if (index > 0 && Option.isSome(previous)) {
130+
const gate = yield* Effect.raceFirst(
131+
Deferred.await(decided).pipe(Effect.as("decided" as const)),
132+
Fiber.join(previous.value).pipe(Effect.as("finished" as const)),
133+
).pipe(
134+
Effect.timeoutOrElse({
135+
duration: ROUTE_CONNECT_HEDGE_MS,
136+
orElse: () => Effect.succeed("hedge" as const),
137+
}),
138+
);
139+
if (gate === "decided") break;
140+
}
141+
const fiber = yield* attempt(route).pipe(
142+
Effect.flatMap(({ result, routeScope }) => {
143+
if (result._tag === "Success" || result.failure.reason === "unsupported") {
144+
return Deferred.succeed(decided, result).pipe(
145+
Effect.as(true),
146+
Effect.orElseSucceed(() => false),
147+
Effect.flatMap((claimed) =>
148+
claimed ? Effect.void : Scope.close(routeScope, Exit.void),
149+
),
150+
);
151+
}
152+
return Ref.updateAndGet(remaining, (count) => count - 1).pipe(
153+
Effect.flatMap((count) =>
154+
count === 0 ? Deferred.succeed(decided, result).pipe(Effect.ignore) : Effect.void,
155+
),
156+
);
157+
}),
158+
Effect.forkChild,
159+
);
160+
fibers.push(fiber);
161+
previous = Option.some(fiber);
162+
}
163+
164+
const result = yield* Deferred.await(decided);
165+
yield* Effect.forEach(fibers, (fiber) => Fiber.interrupt(fiber), {
166+
concurrency: "unbounded",
167+
discard: true,
168+
});
97169
return result;
98170
});
171+
172+
const settle = (
173+
result: Result.Result<EnvironmentConnectionLease, ConnectionAttemptError> | undefined,
174+
) => {
175+
if (result === undefined) return Effect.undefined;
176+
if (result._tag === "Success") return Effect.succeed(result.success);
177+
if (result.failure.reason === "unsupported") return Effect.fail(result.failure);
178+
return Effect.undefined;
179+
};
180+
99181
const silent: Array<{ readonly route: ConnectionRoute; readonly index: number }> = [];
182+
const ready: Array<ConnectionRoute> = [];
100183
for (const [index, route] of routes.entries()) {
101184
const check =
102185
checks.length === 0
@@ -109,25 +192,24 @@ export const connectOverRoutes = Effect.fn("ConnectionDriver.connectOverRoutes")
109192
silent.push({ route, index });
110193
continue;
111194
}
112-
const result = yield* attempt(route);
113-
if (result._tag === "Success") return result.success;
114-
// An incompatible server is the same server on every route.
115-
if (result.failure.reason === "unsupported") return yield* result.failure;
195+
ready.push(route);
116196
}
197+
198+
const first = yield* settle(yield* raceAttempts(ready));
199+
if (first !== undefined) return first;
200+
117201
// A slow route that answered after the head start goes before one that is
118202
// still dead, which can hold an attempt open far longer than its probe.
119203
const late = yield* Effect.forEach(silent, ({ index }) => Fiber.join(checks[index]!), {
120204
concurrency: "unbounded",
121205
});
122206
const retry = [
123-
...silent.filter((_, position) => late[position] !== "silent"),
124-
...silent.filter((_, position) => late[position] === "silent"),
207+
...silent.filter((_, position) => late[position] !== "silent").map(({ route }) => route),
208+
...silent.filter((_, position) => late[position] === "silent").map(({ route }) => route),
125209
];
126-
for (const { route } of retry) {
127-
const result = yield* attempt(route);
128-
if (result._tag === "Success") return result.success;
129-
if (result.failure.reason === "unsupported") return yield* result.failure;
130-
}
210+
const second = yield* settle(yield* raceAttempts(retry));
211+
if (second !== undefined) return second;
212+
131213
return yield* (
132214
transient ??
133215
blocked ??

‎packages/client-runtime/src/connection/registry.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,6 +2196,39 @@ describe("EnvironmentRegistry routes", () => {
21962196
}),
21972197
);
21982198

2199+
it.effect("remembers the successful route first without dropping the live session", () =>
2200+
Effect.gen(function* () {
2201+
const harness = yield* makeHarness(
2202+
[LAN_TARGET, RELAY_TARGET],
2203+
[LAN_PROFILE],
2204+
[[LAN_TARGET.connectionId, BEARER_CREDENTIAL]],
2205+
{
2206+
checkRoute: (route) =>
2207+
route.target._tag === "BearerConnectionTarget" ? "silent" : "unchecked",
2208+
},
2209+
);
2210+
yield* Effect.gen(function* () {
2211+
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
2212+
yield* registry.start;
2213+
yield* awaitConnectionState(
2214+
registry,
2215+
LAN_TARGET.environmentId,
2216+
(state) => state.phase === "connected",
2217+
);
2218+
2219+
expect(routesOf(yield* Ref.get(harness.storedTargets), LAN_TARGET.environmentId)).toEqual([
2220+
RELAY_TARGET,
2221+
LAN_TARGET,
2222+
]);
2223+
expect(yield* Ref.get(harness.connectedRoutes)).toEqual(["relay"]);
2224+
expect(yield* Ref.get(harness.releasedSessions)).toBe(0);
2225+
const entry = (yield* SubscriptionRef.get(registry.entries)).get(LAN_TARGET.environmentId);
2226+
expect(entry?.target).toEqual(RELAY_TARGET);
2227+
expect(entry?.alternateRoutes?.map((route) => route.target)).toEqual([LAN_TARGET]);
2228+
}).pipe(Effect.provide(harness.layer), Effect.scoped);
2229+
}),
2230+
);
2231+
21992232
it.effect("removing the last route forgets the environment", () =>
22002233
Effect.gen(function* () {
22012234
const harness = yield* makeHarness(

0 commit comments

Comments
 (0)