Skip to content

Commit f0dd64a

Browse files
perf(mobile): skip dead LAN paths and defer stale reconnects
Cold start now connects recently used environments first and drops LAN routes when the device is not on a private network, so cellular launches stop racing unreachable homes. Co-authored-by: Cursor <[email protected]>
1 parent 06cbfd6 commit f0dd64a

18 files changed

Lines changed: 344 additions & 14 deletions

File tree

‎apps/desktop/src/app/DesktopConnectionCatalogStore.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ const migrateSavedEnvironmentRecords = Effect.fn(
372372
credentials,
373373
remoteDpopTokens: [],
374374
disabledEnvironmentIds: [],
375+
lastConnected: [],
375376
};
376377
});
377378

‎apps/mobile/src/connection/platform.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ function networkStatus(state: Network.NetworkState): "unknown" | "offline" | "on
4141
return "unknown";
4242
}
4343

44+
function privateNetworkLikely(state: Network.NetworkState): boolean {
45+
if (state.isConnected !== true) return false;
46+
return (
47+
state.type === Network.NetworkStateType.WIFI ||
48+
state.type === Network.NetworkStateType.ETHERNET ||
49+
state.type === Network.NetworkStateType.UNKNOWN
50+
);
51+
}
52+
4453
const layerConnectivity = Connectivity.layer({
4554
status: Effect.tryPromise({
4655
try: () => Network.getNetworkStateAsync(),
@@ -51,6 +60,15 @@ const layerConnectivity = Connectivity.layer({
5160
onSuccess: networkStatus,
5261
}),
5362
),
63+
privateNetworkLikely: Effect.tryPromise({
64+
try: () => Network.getNetworkStateAsync(),
65+
catch: () => undefined,
66+
}).pipe(
67+
Effect.match({
68+
onFailure: () => true,
69+
onSuccess: (state) => (state === undefined ? true : privateNetworkLikely(state)),
70+
}),
71+
),
5472
changes: Stream.callback((queue) =>
5573
Effect.acquireRelease(
5674
Effect.sync(() => {

‎apps/mobile/src/connection/storage.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
removeConnectionFromCatalog,
55
setConnectionEnabledInCatalog,
66
setRoutesInCatalog,
7+
touchEnvironmentConnectedInCatalog,
78
removeCatalogValue,
89
replaceCatalogValue,
910
Persistence,
@@ -27,10 +28,12 @@ function targetPersistenceError(
2728
operation:
2829
| "list-targets"
2930
| "list-disabled-targets"
31+
| "list-last-connected"
3032
| "register-connection"
3133
| "set-connection-routes"
3234
| "remove-connection"
33-
| "set-connection-enabled",
35+
| "set-connection-enabled"
36+
| "touch-connection",
3437
error: ConnectionTransientError,
3538
) {
3639
return new Persistence.ConnectionPersistenceError({
@@ -57,6 +60,10 @@ export const layer = Layer.effectContext(
5760
Effect.map((document) => document.disabledEnvironmentIds),
5861
Effect.mapError((error) => targetPersistenceError("list-disabled-targets", error)),
5962
),
63+
listLastConnected: catalog.read.pipe(
64+
Effect.map((document) => document.lastConnected),
65+
Effect.mapError((error) => targetPersistenceError("list-last-connected", error)),
66+
),
6067
});
6168
const registrationStore = Persistence.ConnectionRegistrationStore.of({
6269
register: Effect.fn("Persistence.ConnectionRegistrationStore.register")(
@@ -86,6 +93,14 @@ export const layer = Layer.effectContext(
8693
Effect.mapError((error) => targetPersistenceError("set-connection-enabled", error)),
8794
),
8895
),
96+
touchConnected: Effect.fn("Persistence.ConnectionRegistrationStore.touchConnected")(
97+
(environmentId, atEpochMs) =>
98+
catalog
99+
.update((document) =>
100+
touchEnvironmentConnectedInCatalog(document, environmentId, atEpochMs),
101+
)
102+
.pipe(Effect.mapError((error) => targetPersistenceError("touch-connection", error))),
103+
),
89104
});
90105
const profileStore = ProfileStore.make({
91106
get: (connectionId) =>

‎apps/web/src/connection/platform.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ function currentNetworkStatus(): "unknown" | "offline" | "online" {
7474

7575
const layerConnectivity = Connectivity.layer({
7676
status: Effect.sync(currentNetworkStatus),
77+
// Browsers rarely expose a reliable cellular/Wi-Fi split; keep LAN routes eligible.
78+
privateNetworkLikely: Effect.succeed(true),
7779
changes: Stream.callback((queue) =>
7880
Effect.acquireRelease(
7981
Effect.sync(() => {

‎apps/web/src/connection/storage.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const emptyCatalog = {
2121
credentials: [],
2222
remoteDpopTokens: [],
2323
disabledEnvironmentIds: [],
24+
lastConnected: [],
2425
} as const;
2526
const decodeCatalog = Schema.decodeUnknownSync(Schema.fromJsonString(ConnectionCatalogDocument));
2627
const encodeCatalog = Schema.encodeSync(Schema.fromJsonString(ConnectionCatalogDocument));

‎apps/web/src/connection/storage.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
removeConnectionFromCatalog,
1414
setConnectionEnabledInCatalog,
1515
setRoutesInCatalog,
16+
touchEnvironmentConnectedInCatalog,
1617
replaceCatalogValue,
1718
Persistence,
1819
makeStoredShellSnapshotEncoder,
@@ -104,10 +105,12 @@ function persistenceError(
104105
operation:
105106
| "list-targets"
106107
| "list-disabled-targets"
108+
| "list-last-connected"
107109
| "register-connection"
108110
| "set-connection-routes"
109111
| "remove-connection"
110112
| "set-connection-enabled"
113+
| "touch-connection"
111114
| "load-shell"
112115
| "save-shell"
113116
| "load-thread"
@@ -733,6 +736,10 @@ export const layer = Layer.effectContext(
733736
Effect.map((document) => document.disabledEnvironmentIds),
734737
Effect.mapError((cause) => persistenceError("list-disabled-targets", cause)),
735738
),
739+
listLastConnected: catalog.read.pipe(
740+
Effect.map((document) => document.lastConnected),
741+
Effect.mapError((cause) => persistenceError("list-last-connected", cause)),
742+
),
736743
});
737744
const registrationStore = Persistence.ConnectionRegistrationStore.of({
738745
register: Effect.fn("Persistence.ConnectionRegistrationStore.register")(
@@ -761,6 +768,15 @@ export const layer = Layer.effectContext(
761768
.pipe(Effect.mapError((cause) => persistenceError("set-connection-enabled", cause)));
762769
},
763770
),
771+
touchConnected: Effect.fn("Persistence.ConnectionRegistrationStore.touchConnected")(
772+
function (environmentId, atEpochMs) {
773+
return catalog
774+
.update((document) =>
775+
touchEnvironmentConnectedInCatalog(document, environmentId, atEpochMs),
776+
)
777+
.pipe(Effect.mapError((cause) => persistenceError("touch-connection", cause)));
778+
},
779+
),
764780
});
765781
const profileStore = ProfileStore.make({
766782
get: (connectionId) =>

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ export class Connectivity extends Context.Service<
1010
{
1111
readonly status: Effect.Effect<NetworkStatus>;
1212
readonly changes: Stream.Stream<NetworkStatus>;
13+
/**
14+
* False on cellular and similar paths where RFC1918 LAN addresses cannot
15+
* be reached. Defaults to true when the platform cannot tell.
16+
*/
17+
readonly privateNetworkLikely: Effect.Effect<boolean>;
1318
}
1419
>()("@t3tools/client-runtime/connection/connectivity") {}
1520

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as FetchHttpClient from "effect/http/FetchHttpClient";
1212
import * as HttpClient from "effect/http/HttpClient";
1313

1414
import type { ConnectionCatalogEntry, ConnectionRoute } from "./catalog.ts";
15+
import * as Connectivity from "./connectivity.ts";
1516
import type {
1617
ConnectionAttemptError,
1718
ConnectionAttemptStage,
@@ -23,8 +24,8 @@ import {
2324
connectionRoutes,
2425
routeEntry,
2526
routeHttpBaseUrl,
27+
routesForNetwork,
2628
sendsTokenInClear,
27-
usableRoutes,
2829
} from "./routes.ts";
2930
import * as RemoteEnvironmentAuthorization from "../authorization/service.ts";
3031
import * as RpcSession from "../rpc/session.ts";
@@ -79,8 +80,9 @@ export const connectOverRoutes = Effect.fn("ConnectionDriver.connectOverRoutes")
7980
connectRoute: (
8081
route: ConnectionRoute,
8182
) => Effect.Effect<EnvironmentConnectionLease, ConnectionAttemptError, R | Scope.Scope>,
83+
privateNetworkLikely = true,
8284
) {
83-
const routes = usableRoutes(entry);
85+
const routes = routesForNetwork(entry, privateNetworkLikely);
8486
const checks =
8587
routes.length === 1
8688
? []
@@ -226,6 +228,7 @@ export const make = Effect.gen(function* () {
226228
const sessions = yield* RpcSession.RpcSessionFactory;
227229
const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
228230
const httpClient = yield* HttpClient.HttpClient;
231+
const connectivity = yield* Connectivity.Connectivity;
229232

230233
// The descriptor is public, so this sends no credential to whatever
231234
// answers at a saved LAN address on a different network.
@@ -271,6 +274,7 @@ export const make = Effect.gen(function* () {
271274
"connection.route.count": connectionRoutes(entry).length,
272275
});
273276
yield* reportProgress({ stage: "preparing" });
277+
const privateNetworkLikely = yield* connectivity.privateNetworkLikely;
274278
return yield* connectOverRoutes(
275279
entry,
276280
(route) => checkRoute(entry, route),
@@ -296,6 +300,7 @@ export const make = Effect.gen(function* () {
296300
}
297301
return { prepared, session } satisfies EnvironmentConnectionLease;
298302
}),
303+
privateNetworkLikely,
299304
);
300305
});
301306

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,12 @@ function layerPairingOnboarding(
169169
Layer.mock(Persistence.ConnectionTargetStore)({
170170
list: Effect.succeed([]),
171171
listDisabled: Effect.succeed([]),
172+
listLastConnected: Effect.succeed([]),
172173
}),
173174
Layer.mock(Persistence.ConnectionRegistrationStore)({
174175
register: () => Effect.void,
175176
setEnabled: () => Effect.void,
177+
touchConnected: () => Effect.void,
176178
}),
177179
Layer.mock(Persistence.EnvironmentCacheStore)({}),
178180
Layer.mock(ConnectionProfileStore.ConnectionProfileStore)({}),
@@ -183,7 +185,11 @@ function layerPairingOnboarding(
183185
Layer.mock(ClientCapabilities.SshEnvironmentGateway)({}),
184186
Layer.succeed(
185187
Connectivity.Connectivity,
186-
Connectivity.Connectivity.of({ status: Effect.succeed("online"), changes: Stream.never }),
188+
Connectivity.Connectivity.of({
189+
status: Effect.succeed("online"),
190+
changes: Stream.never,
191+
privateNetworkLikely: Effect.succeed(true),
192+
}),
187193
),
188194
Layer.mock(ConnectionDriver.ConnectionDriver)({ connect: () => Effect.never }),
189195
Layer.succeed(

0 commit comments

Comments
 (0)