The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 12:00 UTC
fix(client): dead connection routes are probed 30x less often (#27101)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 29, 2026 at 13:42 UTC
65b0ab8c91c1d2c6495460f13b91166c57068076
4 files changed
+208
-17
packages/client-runtime/src/connection/driver.ts
+10
-8
@@ -40,6 +40,8 @@ export interface EnvironmentConnectionLease {
40
}
41
42
export type RouteCheck = "answered" | "silent" | "unchecked";
43
+/** "unprepared" means the route answered its check but could not be prepared. */
44
+export type RoutePreflight = "ready" | "unprepared" | "silent" | "unchecked";
45
46
const ROUTE_CHECK_TIMEOUT_MS = 2_500;
47
// Preferred direct routes share one head start, so a relay connection does not
@@ -57,7 +59,7 @@ export class ConnectionDriver extends Context.Service<
59
readonly preflight: (
60
entry: ConnectionCatalogEntry,
61
route: ConnectionRoute,
60
- ) => Effect.Effect<boolean>;
62
+ ) => Effect.Effect<RoutePreflight>;
63
/** Asks the host behind a live session to answer a fresh request on the session's own origin. */
64
readonly checkHost: (prepared: PreparedConnection) => Effect.Effect<RouteCheck>;
65
}
@@ -204,13 +206,13 @@ export const make = Effect.gen(function* () {
206
? Effect.succeed("silent" as const)
207
: checkRoute(entry, route)
208
).pipe(
207
- Effect.flatMap((check) =>
208
- check === "answered"
209
- ? resolver.prepare(routeEntry(entry, route)).pipe(
210
- Effect.as(true),
211
- Effect.orElseSucceed(() => false),
212
- )
213
- : Effect.succeed(false),
209
+ Effect.filterOrElse(
210
+ (check): check is Exclude<RouteCheck, "answered"> => check !== "answered",
211
+ () =>
212
+ resolver.prepare(routeEntry(entry, route)).pipe(
213
+ Effect.as<RoutePreflight>("ready"),
214
+ Effect.orElseSucceed((): RoutePreflight => "unprepared"),
215
+ ),
216
),
217
Effect.withSpan("ConnectionDriver.preflight"),
218
);
packages/client-runtime/src/connection/registry.test.ts
+5
-1
@@ -431,7 +431,11 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
431
),
432
),
433
preflight: (_entry, route) =>
434
- checkRoute(route).pipe(Effect.map((check) => check === "answered")),
434
+ checkRoute(route).pipe(
435
+ Effect.map((check): ConnectionDriver.RoutePreflight =>
436
+ check === "answered" ? "ready" : check,
437
+ ),
438
+ ),
439
checkHost: () => Effect.succeed<ConnectionDriver.RouteCheck>("unchecked"),
440
});
441
packages/client-runtime/src/connection/supervisor.test.ts
+134
-1
@@ -148,6 +148,7 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?:
148
readonly initialConfig?: (attempt: number) => Effect.Effect<ServerConfig, ConnectionAttemptError>;
149
readonly checkRoute?: (route: ConnectionRoute) => Effect.Effect<ConnectionDriver.RouteCheck>;
150
readonly checkHost?: (prepared: PreparedConnection) => Effect.Effect<ConnectionDriver.RouteCheck>;
151
+ readonly preflight?: (route: ConnectionRoute) => Effect.Effect<ConnectionDriver.RoutePreflight>;
152
readonly inboundFrames?: Effect.Effect<number>;
153
}) {
154
const networkStatus = yield* SubscriptionRef.make<NetworkStatus>(
@@ -244,7 +245,12 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?:
245
ConnectionDriver.ConnectionDriver.of({
246
connect,
247
preflight: (_entry, route) =>
247
- checkRoute(route).pipe(Effect.map((check) => check === "answered")),
248
+ options?.preflight?.(route) ??
249
+ checkRoute(route).pipe(
250
+ Effect.map((check): ConnectionDriver.RoutePreflight =>
251
+ check === "answered" ? "ready" : check,
252
+ ),
253
+ ),
254
checkHost: (prepared) =>
255
options?.checkHost?.(prepared) ??
256
Effect.succeed<ConnectionDriver.RouteCheck>("unchecked"),
@@ -1614,6 +1620,133 @@ describe("EnvironmentSupervisor routes", () => {
1620
}),
1621
);
1622
1623
+ it.effect("checks a dead LAN route less often until the network changes", () =>
1624
+ Effect.gen(function* () {
1625
+ const lanReachable = yield* Ref.make(false);
1626
+ const lanChecks = yield* Ref.make(0);
1627
+ const harness = yield* makeHarness({
1628
+ checkRoute: (route) =>
1629
+ route.target._tag === "BearerConnectionTarget"
1630
+ ? Ref.update(lanChecks, (count) => count + 1).pipe(
1631
+ Effect.andThen(Ref.get(lanReachable)),
1632
+ Effect.map((reachable) => (reachable ? "answered" : "silent")),
1633
+ )
1634
+ : Effect.succeed("unchecked"),
1635
+ prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
1636
+ });
1637
+ const supervisor = yield* EnvironmentSupervisor.make(LAN_THEN_RELAY_ENTRY, {
1638
+ initiallyDesired: true,
1639
+ }).pipe(Effect.provide(harness.dependencies));
1640
+
1641
+ yield* awaitState(
1642
+ supervisor.state,
1643
+ (state) => state.phase === "connected" && state.generation === 1,
1644
+ );
1645
+ yield* Ref.set(lanChecks, 0);
1646
+
1647
+ // Checked at 1, 3 and 7 minutes instead of every minute.
1648
+ for (let minute = 0; minute < 10; minute += 1) {
1649
+ yield* TestClock.adjust("60 seconds");
1650
+ }
1651
+ expect(yield* Ref.get(lanChecks)).toBe(3);
1652
+
1653
+ yield* Ref.set(lanReachable, true);
1654
+ yield* harness.wake("network-changed");
1655
+ yield* awaitState(
1656
+ supervisor.state,
1657
+ (state) => state.phase === "connected" && state.generation === 2,
1658
+ );
1659
+ expect(Option.getOrThrow(yield* SubscriptionRef.get(supervisor.prepared)).target._tag).toBe(
1660
+ "BearerConnectionTarget",
1661
+ );
1662
+ }),
1663
+ );
1664
+
1665
+ it.effect("keeps checking a LAN route every minute when it answers but cannot be prepared", () =>
1666
+ Effect.gen(function* () {
1667
+ const lanPreflights = yield* Ref.make(0);
1668
+ const harness = yield* makeHarness({
1669
+ checkRoute: (route) =>
1670
+ Effect.succeed(route.target._tag === "BearerConnectionTarget" ? "silent" : "unchecked"),
1671
+ preflight: (route) =>
1672
+ route.target._tag === "BearerConnectionTarget"
1673
+ ? Ref.update(lanPreflights, (count) => count + 1).pipe(Effect.as("unprepared" as const))
1674
+ : Effect.succeed("unchecked"),
1675
+ prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
1676
+ });
1677
+ const supervisor = yield* EnvironmentSupervisor.make(LAN_THEN_RELAY_ENTRY, {
1678
+ initiallyDesired: true,
1679
+ }).pipe(Effect.provide(harness.dependencies));
1680
+
1681
+ yield* awaitState(
1682
+ supervisor.state,
1683
+ (state) => state.phase === "connected" && state.generation === 1,
1684
+ );
1685
+ for (let minute = 0; minute < 10; minute += 1) {
1686
+ yield* TestClock.adjust("60 seconds");
1687
+ }
1688
+ expect(yield* Ref.get(lanPreflights)).toBe(10);
1689
+ }),
1690
+ );
1691
+
1692
+ it.effect("ignores a dead LAN check that was running when the network changed", () =>
1693
+ Effect.gen(function* () {
1694
+ const lanChecks = yield* Ref.make(0);
1695
+ const checkStarted = yield* Deferred.make<void>();
1696
+ const finishCheck = yield* Deferred.make<void>();
1697
+ const woke = yield* Ref.make(false);
1698
+ const wakeupHandled = yield* Deferred.make<void>();
1699
+ const harness = yield* makeHarness({
1700
+ checkRoute: (route) =>
1701
+ route.target._tag === "BearerConnectionTarget"
1702
+ ? Ref.updateAndGet(lanChecks, (count) => count + 1).pipe(
1703
+ Effect.flatMap((check) =>
1704
+ check === 1
1705
+ ? Effect.succeed("silent" as const)
1706
+ : check === 2
1707
+ ? Deferred.succeed(checkStarted, undefined).pipe(
1708
+ Effect.andThen(Deferred.await(finishCheck)),
1709
+ Effect.as("silent" as const),
1710
+ )
1711
+ : Effect.succeed("answered" as const),
1712
+ ),
1713
+ )
1714
+ : Effect.succeed("unchecked"),
1715
+ prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
1716
+ // The wakeup probes the session only after it forgets the silent routes.
1717
+ probe: () =>
1718
+ Ref.get(woke).pipe(
1719
+ Effect.flatMap((afterWake) =>
1720
+ afterWake ? Deferred.succeed(wakeupHandled, undefined) : Effect.void,
1721
+ ),
1722
+ Effect.asVoid,
1723
+ ),
1724
+ });
1725
+ const supervisor = yield* EnvironmentSupervisor.make(LAN_THEN_RELAY_ENTRY, {
1726
+ initiallyDesired: true,
1727
+ }).pipe(Effect.provide(harness.dependencies));
1728
+
1729
+ yield* awaitState(
1730
+ supervisor.state,
1731
+ (state) => state.phase === "connected" && state.generation === 1,
1732
+ );
1733
+ yield* TestClock.adjust("60 seconds");
1734
+ yield* Deferred.await(checkStarted);
1735
+ yield* Ref.set(woke, true);
1736
+ yield* harness.wake("network-changed");
1737
+ yield* Deferred.await(wakeupHandled);
1738
+ yield* Deferred.succeed(finishCheck, undefined);
1739
+
1740
+ yield* awaitState(
1741
+ supervisor.state,
1742
+ (state) => state.phase === "connected" && state.generation === 2,
1743
+ );
1744
+ expect(Option.getOrThrow(yield* SubscriptionRef.get(supervisor.prepared)).target._tag).toBe(
1745
+ "BearerConnectionTarget",
1746
+ );
1747
+ }),
1748
+ );
1749
+
1750
it.effect("moves off a dead LAN socket when the phone switches to cellular", () =>
1751
Effect.gen(function* () {
1752
const lanReachable = yield* Ref.make(true);
packages/client-runtime/src/connection/supervisor.ts
+59
-7
@@ -58,7 +58,11 @@ const SILENT_SOCKET_RECHECK_EVERY = "1 second";
58
// instead of timing out.
59
const REMOTE_SILENT_SOCKET_AFTER = "2500 millis";
60
const BACKOFF_RESET_AFTER_MS = 30_000;
61
-const BETTER_ROUTE_CHECK_INTERVAL = "60 seconds";
61
+const BETTER_ROUTE_CHECK_INTERVAL_MS = 60_000;
62
+// Each check of a dead route can wait out the full probe timeout, so a route
63
+// that keeps failing is checked half as often each time, until the network
64
+// changes and it may answer again.
65
+const SILENT_ROUTE_BACKOFF_MAX_MS = 1_800_000;
66
// A better route that answered the check but then failed to connect is not
67
// tried again for this long, so a flaky LAN cannot bounce the connection.
68
const BETTER_ROUTE_COOLDOWN_MS = 300_000;
@@ -139,6 +143,20 @@ function retryDelayMs(failureCount: number, random: number): number {
143
return Math.round(ceiling / 2 + (ceiling / 2) * random);
144
}
145
146
+interface SilentRoute {
147
+ readonly failures: number;
148
+ readonly until: bigint;
149
+}
150
+
151
+function backOffSilentRoute(previous: SilentRoute | undefined, now: bigint): SilentRoute {
152
+ const failures = (previous?.failures ?? 0) + 1;
153
+ const delayMs = Math.min(
154
+ SILENT_ROUTE_BACKOFF_MAX_MS,
155
+ BETTER_ROUTE_CHECK_INTERVAL_MS * 2 ** failures,
156
+ );
157
+ return { failures, until: now + BigInt(delayMs) * 1_000_000n };
158
+}
159
+
160
function annotateTarget(target: ConnectionTarget) {
161
return Effect.annotateCurrentSpan({
162
"environment.id": target.environmentId,
@@ -288,6 +306,16 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
306
const currentRoutes = Ref.get(currentEntry).pipe(Effect.map(connectionRoutes));
307
const preferredRouteId = yield* Ref.make(Option.none<string>());
308
const routeCooldowns = yield* Ref.make<ReadonlyMap<string, bigint>>(new Map());
309
+ // The generation keeps a check that started before a reset from recording
310
+ // its stale failures after it.
311
+ const silentRoutes = yield* Ref.make({
312
+ generation: 0,
313
+ routes: new Map<string, SilentRoute>() as ReadonlyMap<string, SilentRoute>,
314
+ });
315
+ const forgetSilentRoutes = Ref.update(silentRoutes, ({ generation }) => ({
316
+ generation: generation + 1,
317
+ routes: new Map(),
318
+ }));
319
320
const attemptEntry = Effect.gen(function* () {
321
const latest = yield* Ref.get(currentEntry);
@@ -338,15 +366,29 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
366
if (current <= 0) return;
367
const now = yield* Clock.monotonicTimeNanos;
368
const cooldowns = yield* Ref.get(routeCooldowns);
369
+ const silent = yield* Ref.get(silentRoutes);
370
const better = connectionRoutes(latest)
371
.slice(0, current)
343
- .filter((route) => (cooldowns.get(connectionRouteId(route.target)) ?? 0n) <= now);
344
- const passed = yield* Effect.forEach(
372
+ .filter((route) => {
373
+ const id = connectionRouteId(route.target);
374
+ return (cooldowns.get(id) ?? 0n) <= now && (silent.routes.get(id)?.until ?? 0n) <= now;
375
+ });
376
+ const results = yield* Effect.forEach(
377
better,
378
(route: ConnectionRoute) => driver.preflight(latest, route),
379
{ concurrency: "unbounded" },
380
);
349
- const index = passed.indexOf(true);
381
+ yield* Ref.update(silentRoutes, (previous) => {
382
+ if (previous.generation !== silent.generation) return previous;
383
+ const routes = new Map(previous.routes);
384
+ for (const [position, route] of better.entries()) {
385
+ const id = connectionRouteId(route.target);
386
+ if (results[position] !== "silent") routes.delete(id);
387
+ else routes.set(id, backOffSilentRoute(previous.routes.get(id), now));
388
+ }
389
+ return { generation: previous.generation, routes };
390
+ });
391
+ const index = results.indexOf("ready");
392
if (index === -1) return;
393
const live = yield* SubscriptionRef.get(session);
394
if (Option.isNone(live) || live.value !== lease.session) return;
@@ -668,7 +710,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
710
);
711
// Ticks always run: the check skips the preferred route itself, and the
712
// route list can grow while connected (learned routes).
671
- yield* Stream.tick(BETTER_ROUTE_CHECK_INTERVAL).pipe(
713
+ yield* Stream.tick(Duration.millis(BETTER_ROUTE_CHECK_INTERVAL_MS)).pipe(
714
Stream.drop(1),
715
Stream.runForEach(() => requestBetterRouteCheck(lease)),
716
Effect.forkScoped,
@@ -1114,14 +1156,24 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
1156
current.network === network ? [false, current] : ([true, { ...current, network }] as const),
1157
).pipe(
1158
Effect.flatMap((changed) =>
1117
- changed ? signal({ _tag: "NetworkChanged", network }) : Effect.void,
1159
+ changed
1160
+ ? (network === "offline" ? Effect.void : forgetSilentRoutes).pipe(
1161
+ Effect.andThen(signal({ _tag: "NetworkChanged", network })),
1162
+ )
1163
+ : Effect.void,
1164
),
1165
),
1166
),
1167
Effect.forkScoped,
1168
);
1169
yield* wakeups.changes.pipe(
1124
- Stream.runForEach((reason) => signal({ _tag: "Wakeup", reason })),
1170
+ Stream.runForEach((reason) =>
1171
+ // A long suspension can hide a network change the platform never reported.
1172
+ (reason === "network-changed" || reason === "application-active-long"
1173
+ ? forgetSilentRoutes
1174
+ : Effect.void
1175
+ ).pipe(Effect.andThen(signal({ _tag: "Wakeup", reason }))),
1176
+ ),
1177
Effect.forkScoped,
1178
);
1179
yield* Queue.take(betterRouteChecks).pipe(