@devries-cameron20 / t3code-1 / commits / 145fba7ffc
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(client-runtime): a dropped session no longer reads as a failed connection (#18469)

Otavio Salvador committed Oct 18, 2026 at 16:26 UTC 145fba7ffc859834ce8a0bdefeaa3cd50964872e
2 files changed +79 -4
packages/client-runtime/src/connection/supervisor.test.ts
+69
@@ -36,6 +36,7 @@ import {
36 type SupervisorConnectionState,
37 } from "./model.ts";
38 import * as RpcSession from "../rpc/session.ts";
39 +import { presentConnectionState } from "./presentation.ts";
40 import * as EnvironmentSupervisor from "./supervisor.ts";
41 import * as ConnectionWakeups from "./wakeups.ts";
42 import { connectionRouteId, entryWithRoutes, isLearned, mergeLearnedRoutes } from "./routes.ts";
@@ -985,6 +986,73 @@ describe("EnvironmentSupervisor", () => {
986 }).pipe(Effect.provide(TestClock.layer())),
987 );
988
989 + it.effect("does not report a session lost while suspended as a failed connection", () =>
990 + Effect.gen(function* () {
991 + const allowReconnect = yield* Deferred.make<void>();
992 + const harness = yield* makeHarness({
993 + prepare: (attempt) =>
994 + attempt === 2
995 + ? Deferred.await(allowReconnect).pipe(Effect.as(PREPARED_CONNECTION))
996 + : Effect.succeed(PREPARED_CONNECTION),
997 + });
998 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
999 + initiallyDesired: true,
1000 + }).pipe(Effect.provide(harness.dependencies));
1001 +
1002 + yield* awaitState(supervisor.state, (state) => state.phase === "connected");
1003 + // The OS killed the socket while the app was suspended; its close arrives
1004 + // before the app reports it is active again.
1005 + yield* harness.closeLatestSession();
1006 + const waiting = yield* awaitState(supervisor.state, (state) => state.phase === "backoff");
1007 + expect(presentConnectionState(waiting)).toEqual({
1008 + phase: "reconnecting",
1009 + error: null,
1010 + traceId: null,
1011 + });
1012 +
1013 + yield* harness.wake("application-active");
1014 + const replacing = yield* awaitState(
1015 + supervisor.state,
1016 + (state) => state.phase === "connecting" && state.stage === "preparing",
1017 + );
1018 + expect(presentConnectionState(replacing).error).toBeNull();
1019 +
1020 + yield* Deferred.succeed(allowReconnect, undefined);
1021 + yield* awaitState(
1022 + supervisor.state,
1023 + (state) => state.phase === "connected" && state.generation === 2,
1024 + );
1025 + }).pipe(Effect.provide(TestClock.layer())),
1026 + );
1027 +
1028 + it.effect("does not show an earlier setup failure while replacing a short session", () =>
1029 + Effect.gen(function* () {
1030 + const harness = yield* makeHarness({
1031 + prepare: (attempt) =>
1032 + attempt === 1
1033 + ? Effect.fail(transient("Relay connection timed out."))
1034 + : attempt === 3
1035 + ? Effect.never
1036 + : Effect.succeed(PREPARED_CONNECTION),
1037 + });
1038 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
1039 + initiallyDesired: true,
1040 + }).pipe(Effect.provide(harness.dependencies));
1041 +
1042 + yield* awaitState(supervisor.state, (state) => state.phase === "backoff");
1043 + yield* TestClock.adjust("3 seconds");
1044 + yield* awaitState(supervisor.state, (state) => state.phase === "connected");
1045 + // A long resume replaces the session before it counts as stable.
1046 + yield* harness.wake("application-active-reconnect");
1047 + const replacing = yield* awaitState(
1048 + supervisor.state,
1049 + (state) => state.phase === "connecting" && state.generation === 2,
1050 + );
1051 +
1052 + expect(presentConnectionState(replacing).error).toBeNull();
1053 + }).pipe(Effect.provide(TestClock.layer())),
1054 + );
1055 +
1056 it.effect("keeps escalating backoff when a newly opened session flaps", () =>
1057 Effect.gen(function* () {
1058 const harness = yield* makeHarness();
@@ -1245,6 +1313,7 @@ describe("EnvironmentSupervisor", () => {
1313 (state) => state.phase === "connecting",
1314 );
1315 expect(reconnecting.attempt).toBe(1);
1316 + expect(reconnecting.lastFailure).toBeNull();
1317 expect(Option.isNone(yield* SubscriptionRef.get(supervisor.session))).toBe(true);
1318
1319 yield* Deferred.succeed(allowReconnect, undefined);
packages/client-runtime/src/connection/supervisor.ts
+10 -4
@@ -885,9 +885,13 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
885 const failedProbe = yield* Ref.getAndSet(probeUnanswered, false);
886 if (outcome.established) {
887 generation = nextGeneration;
888 + // A session came up, so no earlier failure describes the connection.
889 + // Losing a live session is not a failed connection either, for example
890 + // a socket the OS killed while the app was suspended: its replacement
891 + // reports no failure until it fails itself.
892 + latestFailure = null;
893 if (outcome.stable) {
894 resetRetryLadder();
890 - latestFailure = null;
895 }
896 }
897 if (outcome._tag === "Interrupted") {
@@ -900,7 +904,9 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
904
905 const attemptSpan: Option.Option<Tracer.Span> = outcome.failure.attemptSpan;
906 const error: ConnectionAttemptError = outcome.failure.error;
903 - latestFailure = error;
907 + if (!outcome.established) {
908 + latestFailure = error;
909 + }
910 if (error._tag === "ConnectionBlockedError") {
911 const blockedIntent = yield* Ref.get(intent);
912 yield* setState({
@@ -922,7 +928,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
928
929 if (failedProbe) {
930 resetRetryLadder();
925 - yield* setState(connectingState(yield* Ref.get(intent), generation, 1, error));
931 + yield* setState(connectingState(yield* Ref.get(intent), generation, 1, latestFailure));
932 continue;
933 }
934
@@ -942,7 +948,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
948 stage: null,
949 attempt,
950 generation,
945 - lastFailure: error,
951 + lastFailure: latestFailure,
952 retryAt: (yield* Clock.currentTimeMillis) + delayMs,
953 });
954 const applicationActivated = yield* waitForRetrySignal(delayMs);