The best way to control Claude Code, Codex and more from any computer or phone
fix(client-runtime): slow foreground probes keep live sessions connected (#24309)
Julius Marminge committed
Nov 11, 2026 at 15:44 UTC
8851e307d84bbcbe77ee9cf4390a294e3c18a41d
2 files changed
+117
-2
packages/client-runtime/src/connection/supervisor.test.ts
+98
@@ -10,6 +10,7 @@ import * as Effect from "effect/Effect";
10
import * as Fiber from "effect/Fiber";
11
import * as Layer from "effect/Layer";
12
import * as Option from "effect/Option";
13
+import * as Queue from "effect/Queue";
14
import * as Random from "effect/Random";
15
import * as Ref from "effect/Ref";
16
import * as Stream from "effect/Stream";
@@ -1324,6 +1325,103 @@ describe("EnvironmentSupervisor", () => {
1325
}),
1326
);
1327
1328
+ it.effect(
1329
+ "keeps a live session when the automatic follow-up to a slow foreground probe answers",
1330
+ () =>
1331
+ Effect.gen(function* () {
1332
+ const probeCount = yield* Ref.make(0);
1333
+ const probeStarted = yield* Queue.unbounded<number>();
1334
+ const harness = yield* makeHarness({
1335
+ probe: () =>
1336
+ Ref.updateAndGet(probeCount, (count) => count + 1).pipe(
1337
+ Effect.tap((call) => Queue.offer(probeStarted, call)),
1338
+ Effect.flatMap((call) => (call === 1 ? Effect.never : Effect.void)),
1339
+ ),
1340
+ });
1341
+ const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
1342
+ initiallyDesired: true,
1343
+ }).pipe(Effect.provide(harness.dependencies));
1344
+
1345
+ yield* awaitState(
1346
+ supervisor.state,
1347
+ (state) => state.phase === "connected" && state.generation === 1,
1348
+ );
1349
+ yield* harness.wake("application-active");
1350
+ expect(yield* Queue.take(probeStarted)).toBe(1);
1351
+ // A busy server misses the foreground probe deadline once while the
1352
+ // socket stays open; the supervisor asks again on its own.
1353
+ yield* TestClock.adjust("15 seconds");
1354
+ expect(yield* Queue.take(probeStarted)).toBe(2);
1355
+ yield* TestClock.adjust("30 seconds");
1356
+
1357
+ expect(yield* Ref.get(harness.releaseCount)).toBe(0);
1358
+ expect(yield* Ref.get(harness.sessionCount)).toBe(1);
1359
+ expect(yield* SubscriptionRef.get(supervisor.state)).toMatchObject({
1360
+ phase: "connected",
1361
+ generation: 1,
1362
+ });
1363
+ }).pipe(Effect.provide(TestClock.layer())),
1364
+ );
1365
+
1366
+ it.effect("replaces a session when the automatic follow-up probe also goes unanswered", () =>
1367
+ Effect.gen(function* () {
1368
+ const probeCount = yield* Ref.make(0);
1369
+ const harness = yield* makeHarness({
1370
+ probe: () =>
1371
+ Ref.update(probeCount, (count) => count + 1).pipe(Effect.andThen(Effect.never)),
1372
+ });
1373
+ const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
1374
+ initiallyDesired: true,
1375
+ }).pipe(Effect.provide(harness.dependencies));
1376
+
1377
+ yield* awaitState(
1378
+ supervisor.state,
1379
+ (state) => state.phase === "connected" && state.generation === 1,
1380
+ );
1381
+ // One foreground event and no other signal: the follow-up's miss alone
1382
+ // replaces the session.
1383
+ yield* harness.wake("application-active");
1384
+ yield* TestClock.adjust("15 seconds");
1385
+ yield* TestClock.adjust("15 seconds");
1386
+ yield* awaitState(
1387
+ supervisor.state,
1388
+ (state) => state.phase === "connected" && state.generation === 2,
1389
+ );
1390
+
1391
+ expect(yield* Ref.get(probeCount)).toBe(2);
1392
+ expect(yield* Ref.get(harness.releaseCount)).toBe(1);
1393
+ }).pipe(Effect.provide(TestClock.layer())),
1394
+ );
1395
+
1396
+ it.effect("replaces a session when a retry lands late in a slow foreground probe", () =>
1397
+ Effect.gen(function* () {
1398
+ const probeStarted = yield* Deferred.make<void>();
1399
+ const harness = yield* makeHarness({
1400
+ probe: () => Deferred.succeed(probeStarted, undefined).pipe(Effect.andThen(Effect.never)),
1401
+ });
1402
+ const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
1403
+ initiallyDesired: true,
1404
+ }).pipe(Effect.provide(harness.dependencies));
1405
+
1406
+ yield* awaitState(
1407
+ supervisor.state,
1408
+ (state) => state.phase === "connected" && state.generation === 1,
1409
+ );
1410
+ yield* harness.wake("application-active");
1411
+ yield* Deferred.await(probeStarted);
1412
+ yield* TestClock.adjust("13 seconds");
1413
+ // The retry's own deadline falls after the foreground one, but it still
1414
+ // asks for a definite answer, so the first miss is not forgiven.
1415
+ yield* supervisor.retryNow;
1416
+ yield* TestClock.adjust("5 seconds");
1417
+ yield* awaitState(
1418
+ supervisor.state,
1419
+ (state) => state.phase === "connected" && state.generation === 2,
1420
+ );
1421
+ expect(yield* Ref.get(harness.releaseCount)).toBe(1);
1422
+ }).pipe(Effect.provide(TestClock.layer())),
1423
+ );
1424
+
1425
it.effect("does not churn a healthy session when credentials change", () =>
1426
Effect.gen(function* () {
1427
const harness = yield* makeHarness();
packages/client-runtime/src/connection/supervisor.ts
+19
-2
@@ -691,6 +691,9 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
691
}
692
};
693
694
+ const isForegroundProbe = (next: SupervisorSignal) =>
695
+ next._tag === "Wakeup" && next.reason === "application-active";
696
+
697
// Ends "silent" when the host answers a fresh request while the socket
698
// delivers nothing, as when iOS kills a suspended socket without a close
699
// event, and "alive" once the socket delivers a frame. A slow or
@@ -772,7 +775,11 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
775
continue;
776
}
777
yield* Ref.set(probeUnanswered, true);
775
- const probe = yield* Effect.forkChild(lease.session.probe);
778
+ let probe = yield* Effect.forkChild(lease.session.probe);
779
+ // A busy server can miss one foreground probe while the socket is still
780
+ // healthy, so the first miss asks again and only the follow-up's miss
781
+ // replaces the session.
782
+ let toleratesMiss = isForegroundProbe(next);
783
let silentSocket: Fiber.Fiber<"silent" | "alive"> | undefined;
784
// A signal during a running check, such as a network change that kills
785
// the socket, needs a fresh check only if the running one sees a frame.
@@ -785,7 +792,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
792
// The web and desktop foreground probe allows a busy host 15 seconds,
793
// and a busy host can answer a fresh request before a queued probe.
794
const armSilentSocketCheck = Effect.fnUntraced(function* (signal: SupervisorSignal) {
788
- if (signal._tag === "Wakeup" && signal.reason === "application-active") return;
795
+ if (isForegroundProbe(signal)) return;
796
if (silentSocket !== undefined) {
797
recheck = true;
798
return;
@@ -837,6 +844,13 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
844
return true;
845
}
846
if (probeEvent._tag === "TimedOut") {
847
+ if (toleratesMiss) {
848
+ toleratesMiss = false;
849
+ yield* Fiber.interrupt(probe);
850
+ probe = yield* Effect.forkChild(lease.session.probe);
851
+ deadline = (yield* Clock.monotonicTimeNanos) + Duration.toNanosUnsafe(probeTimeout);
852
+ continue;
853
+ }
854
yield* stopProbe;
855
// A long suspension leaves the socket suspect even when the network
856
// reports offline, so it is replaced like a silent one.
@@ -861,6 +875,9 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
875
}
876
const signalTimeout = probeTimeoutFor(probeEvent.signal, routeKind);
877
if (signalTimeout !== undefined) {
878
+ // A retry or network signal wants a definite answer, even when its
879
+ // own deadline lands after the foreground one.
880
+ if (!isForegroundProbe(probeEvent.signal)) toleratesMiss = false;
881
if (isLongResume(probeEvent.signal) && !longResume) {
882
longResume = true;
883
longResumeStartedAt = yield* Clock.monotonicTimeNanos;