Skip to content

Commit 0a2ba7b

Browse files
fix(client): only unanswered route checks back off, and resets win
A route that answered its check but failed to prepare was backed off like a dead route; preflight now reports that apart and only silent routes back off. A check that was running when the network changed could also re-record its stale failure after the reset; results from before a reset are now dropped. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d062787 commit 0a2ba7b

4 files changed

Lines changed: 113 additions & 19 deletions

File tree

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export interface EnvironmentConnectionLease {
4040
}
4141

4242
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";
4345

4446
const ROUTE_CHECK_TIMEOUT_MS = 2_500;
4547
// Preferred direct routes share one head start, so a relay connection does not
@@ -57,7 +59,7 @@ export class ConnectionDriver extends Context.Service<
5759
readonly preflight: (
5860
entry: ConnectionCatalogEntry,
5961
route: ConnectionRoute,
60-
) => Effect.Effect<boolean>;
62+
) => Effect.Effect<RoutePreflight>;
6163
/** Asks the host behind a live session to answer a fresh request on the session's own origin. */
6264
readonly checkHost: (prepared: PreparedConnection) => Effect.Effect<RouteCheck>;
6365
}
@@ -204,13 +206,13 @@ export const make = Effect.gen(function* () {
204206
? Effect.succeed("silent" as const)
205207
: checkRoute(entry, route)
206208
).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+
),
214216
),
215217
Effect.withSpan("ConnectionDriver.preflight"),
216218
);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,11 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
431431
),
432432
),
433433
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+
),
435439
checkHost: () => Effect.succeed<ConnectionDriver.RouteCheck>("unchecked"),
436440
});
437441

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

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?:
148148
readonly initialConfig?: (attempt: number) => Effect.Effect<ServerConfig, ConnectionAttemptError>;
149149
readonly checkRoute?: (route: ConnectionRoute) => Effect.Effect<ConnectionDriver.RouteCheck>;
150150
readonly checkHost?: (prepared: PreparedConnection) => Effect.Effect<ConnectionDriver.RouteCheck>;
151+
readonly preflight?: (route: ConnectionRoute) => Effect.Effect<ConnectionDriver.RoutePreflight>;
151152
readonly inboundFrames?: Effect.Effect<number>;
152153
}) {
153154
const networkStatus = yield* SubscriptionRef.make<NetworkStatus>(
@@ -244,7 +245,12 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?:
244245
ConnectionDriver.ConnectionDriver.of({
245246
connect,
246247
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+
),
248254
checkHost: (prepared) =>
249255
options?.checkHost?.(prepared) ??
250256
Effect.succeed<ConnectionDriver.RouteCheck>("unchecked"),
@@ -1656,6 +1662,79 @@ describe("EnvironmentSupervisor routes", () => {
16561662
}),
16571663
);
16581664

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 harness = yield* makeHarness({
1698+
checkRoute: (route) =>
1699+
route.target._tag === "BearerConnectionTarget"
1700+
? Ref.updateAndGet(lanChecks, (count) => count + 1).pipe(
1701+
Effect.flatMap((check) =>
1702+
check === 1
1703+
? Effect.succeed("silent" as const)
1704+
: check === 2
1705+
? Deferred.succeed(checkStarted, undefined).pipe(
1706+
Effect.andThen(Deferred.await(finishCheck)),
1707+
Effect.as("silent" as const),
1708+
)
1709+
: Effect.succeed("answered" as const),
1710+
),
1711+
)
1712+
: Effect.succeed("unchecked"),
1713+
prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
1714+
});
1715+
const supervisor = yield* EnvironmentSupervisor.make(LAN_THEN_RELAY_ENTRY, {
1716+
initiallyDesired: true,
1717+
}).pipe(Effect.provide(harness.dependencies));
1718+
1719+
yield* awaitState(
1720+
supervisor.state,
1721+
(state) => state.phase === "connected" && state.generation === 1,
1722+
);
1723+
yield* TestClock.adjust("60 seconds");
1724+
yield* Deferred.await(checkStarted);
1725+
yield* harness.wake("network-changed");
1726+
yield* Deferred.succeed(finishCheck, undefined);
1727+
1728+
yield* awaitState(
1729+
supervisor.state,
1730+
(state) => state.phase === "connected" && state.generation === 2,
1731+
);
1732+
expect(Option.getOrThrow(yield* SubscriptionRef.get(supervisor.prepared)).target._tag).toBe(
1733+
"BearerConnectionTarget",
1734+
);
1735+
}),
1736+
);
1737+
16591738
it.effect("moves off a dead LAN socket when the phone switches to cellular", () =>
16601739
Effect.gen(function* () {
16611740
const lanReachable = yield* Ref.make(true);

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,16 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
306306
const currentRoutes = Ref.get(currentEntry).pipe(Effect.map(connectionRoutes));
307307
const preferredRouteId = yield* Ref.make(Option.none<string>());
308308
const routeCooldowns = yield* Ref.make<ReadonlyMap<string, bigint>>(new Map());
309-
const silentRoutes = yield* Ref.make<ReadonlyMap<string, SilentRoute>>(new Map());
310-
const forgetSilentRoutes = Ref.set(silentRoutes, 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+
}));
311319

312320
const attemptEntry = Effect.gen(function* () {
313321
const latest = yield* Ref.get(currentEntry);
@@ -363,23 +371,24 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
363371
.slice(0, current)
364372
.filter((route) => {
365373
const id = connectionRouteId(route.target);
366-
return (cooldowns.get(id) ?? 0n) <= now && (silent.get(id)?.until ?? 0n) <= now;
374+
return (cooldowns.get(id) ?? 0n) <= now && (silent.routes.get(id)?.until ?? 0n) <= now;
367375
});
368-
const passed = yield* Effect.forEach(
376+
const results = yield* Effect.forEach(
369377
better,
370378
(route: ConnectionRoute) => driver.preflight(latest, route),
371379
{ concurrency: "unbounded" },
372380
);
373381
yield* Ref.update(silentRoutes, (previous) => {
374-
const next = new Map(previous);
382+
if (previous.generation !== silent.generation) return previous;
383+
const routes = new Map(previous.routes);
375384
for (const [position, route] of better.entries()) {
376385
const id = connectionRouteId(route.target);
377-
if (passed[position]) next.delete(id);
378-
else next.set(id, backOffSilentRoute(previous.get(id), now));
386+
if (results[position] !== "silent") routes.delete(id);
387+
else routes.set(id, backOffSilentRoute(previous.routes.get(id), now));
379388
}
380-
return next;
389+
return { generation: previous.generation, routes };
381390
});
382-
const index = passed.indexOf(true);
391+
const index = results.indexOf("ready");
383392
if (index === -1) return;
384393
const live = yield* SubscriptionRef.get(session);
385394
if (Option.isNone(live) || live.value !== lease.session) return;

0 commit comments

Comments
 (0)