@devries-cameron20 / t3code Mirror · read-only / commits / ca7a9448b8
Fork 0

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 21, 16:00 UTC

fix(client-runtime): Reconnect works while the network reports offline (#24932)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Victor committed Nov 14, 2026 at 02:01 UTC ca7a9448b887a70170d1d0c90d199fe3939ff066
2 files changed +77 -7
packages/client-runtime/src/connection/supervisor.test.ts
+60
@@ -361,6 +361,66 @@ describe("EnvironmentSupervisor", () => {
361 }),
362 );
363
364 + it.effect("explicit retry connects while the network still reports offline", () =>
365 + Effect.gen(function* () {
366 + const harness = yield* makeHarness({
367 + networkStatus: "offline",
368 + prepare: () => Effect.sleep("1 second").pipe(Effect.as(PREPARED_CONNECTION)),
369 + });
370 + const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
371 + initiallyDesired: true,
372 + }).pipe(Effect.provide(harness.dependencies));
373 +
374 + yield* awaitState(supervisor.state, (state) => state.phase === "offline");
375 + // The loop wakes on the earlier signal and takes the retry's flag while
376 + // RetryRequested is still queued, so that signal must not cancel the attempt.
377 + yield* supervisor.connect;
378 + yield* supervisor.retryNow;
379 + yield* TestClock.adjust("1 second");
380 + const settled = yield* awaitState(
381 + supervisor.state,
382 + (state) => state.phase === "connected" || state.phase === "offline",
383 + );
384 +
385 + expect(settled.phase).toBe("connected");
386 + expect(yield* Ref.get(harness.prepareCount)).toBe(1);
387 + expect(yield* Ref.get(harness.releaseCount)).toBe(0);
388 + }),
389 + );
390 +
391 + it.effect.each([
392 + { label: "cancels connection setup", retry: false, phase: "offline", sessions: 0 },
393 + { label: "does not cancel an explicit retry", retry: true, phase: "connected", sessions: 1 },
394 + ])("an offline report during setup $label", ({ retry, phase, sessions }) =>
395 + Effect.gen(function* () {
396 + const firstAttemptStarted = yield* Deferred.make<void>();
397 + const harness = yield* makeHarness({
398 + prepare: () =>
399 + Deferred.succeed(firstAttemptStarted, undefined).pipe(
400 + Effect.andThen(Effect.sleep("1 second")),
401 + Effect.as(PREPARED_CONNECTION),
402 + ),
403 + });
404 + const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
405 + initiallyDesired: true,
406 + }).pipe(Effect.provide(harness.dependencies));
407 +
408 + yield* Deferred.await(firstAttemptStarted);
409 + if (retry) {
410 + yield* supervisor.retryNow;
411 + }
412 + yield* harness.setNetworkStatus("offline");
413 + yield* TestClock.adjust("1 second");
414 + const settled = yield* awaitState(
415 + supervisor.state,
416 + (state) => state.phase === "connected" || state.phase === "offline",
417 + );
418 +
419 + expect(settled.phase).toBe(phase);
420 + expect(yield* Ref.get(harness.sessionCount)).toBe(sessions);
421 + }),
422 + );
423 +
424 it.effect("connects to the primary environment while the device reports offline", () =>
425 Effect.gen(function* () {
426 const harness = yield* makeHarness({ networkStatus: "offline" });
packages/client-runtime/src/connection/supervisor.ts
+17 -7
@@ -583,7 +583,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
583 );
584 });
585
586 - const waitForEstablishmentInterrupt = Effect.fnUntraced(function* () {
586 + const waitForEstablishmentInterrupt = Effect.fnUntraced(function* (ignoreOffline: boolean) {
587 // A primary attempt keeps running through an offline report, so the
588 // network returning restarts it in case it stalled meanwhile.
589 let deviceOffline = (yield* connectivity.status) === "offline";
@@ -591,11 +591,17 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
591 const next = yield* Queue.take(signals);
592 switch (next._tag) {
593 case "DisconnectRequested":
594 - case "RetryRequested":
594 return false;
595 + case "RetryRequested":
596 + // Once the loop has taken this retry's flag, the running attempt is
597 + // the one the retry asked for.
598 + if (yield* Ref.get(resetRetryState)) {
599 + return false;
600 + }
601 + break;
602 case "NetworkChanged":
603 if (target._tag !== "PrimaryConnectionTarget") {
598 - if (next.network === "offline") {
604 + if (next.network === "offline" && !ignoreOffline) {
605 return false;
606 }
607 } else if (next.network === "offline") {
@@ -920,7 +926,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
926 exit,
927 })),
928 ),
923 - waitForEstablishmentInterrupt().pipe(
929 + waitForEstablishmentInterrupt(ignoreOffline).pipe(
930 Effect.map((resetRetry): EstablishmentEvent => ({
931 _tag: "Interrupted",
932 resetRetry,
@@ -1137,7 +1143,8 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
1143 let replacing = false;
1144
1145 for (;;) {
1140 - if (yield* Ref.getAndSet(resetRetryState, false)) {
1146 + const retryRequested = yield* Ref.getAndSet(resetRetryState, false);
1147 + if (retryRequested) {
1148 failureCount = 0;
1149 backoffFailures = 0;
1150 approvalWaits = 0;
@@ -1153,7 +1160,10 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
1160 yield* waitForSignal;
1161 continue;
1162 }
1156 - if (currentIntent.network === "offline" && !replacing) {
1163 + // The platform's offline report can stay wrong after the network is back,
1164 + // so an explicit retry still tries instead of waiting for it to change.
1165 + const ignoreOffline = replacing || retryRequested;
1166 + if (currentIntent.network === "offline" && !ignoreOffline) {
1167 yield* clearLease;
1168 yield* setState(offlineState(currentIntent, generation, failureCount + 1, latestFailure));
1169 const applicationActivated = yield* waitForSignal;
@@ -1166,7 +1176,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
1176 const attempt = failureCount + 1;
1177 const nextGeneration = generation + 1;
1178 const outcome: AttemptOutcome = yield* Effect.scoped(
1169 - runAttempt(attempt, nextGeneration, latestFailure, pendingRetry, replacing),
1179 + runAttempt(attempt, nextGeneration, latestFailure, pendingRetry, ignoreOffline),
1180 );
1181 replacing = false;
1182 const failedProbe = yield* Ref.getAndSet(probeUnanswered, false);