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, 05:45 UTC
fix(client-runtime): connect to the primary environment while the browser reports offline (#29347)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 2, 2026 at 18:29 UTC
1436c4571fa0030ec114cbfa3ad7d62f07b642a3
2 files changed
+96
-8
packages/client-runtime/src/connection/supervisor.test.ts
+73
-2
@@ -324,7 +324,7 @@ describe("EnvironmentSupervisor", () => {
324
it.effect("waits while offline and connects immediately when the network returns", () =>
325
Effect.gen(function* () {
326
const harness = yield* makeHarness({ networkStatus: "offline" });
327
- const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
327
+ const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
328
initiallyDesired: true,
329
}).pipe(Effect.provide(harness.dependencies));
330
@@ -346,6 +346,54 @@ describe("EnvironmentSupervisor", () => {
346
}),
347
);
348
349
+ it.effect("connects to the primary environment while the device reports offline", () =>
350
+ Effect.gen(function* () {
351
+ const harness = yield* makeHarness({ networkStatus: "offline" });
352
+ const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
353
+ initiallyDesired: true,
354
+ }).pipe(Effect.provide(harness.dependencies));
355
+
356
+ const settled = yield* awaitState(
357
+ supervisor.state,
358
+ (state) => state.phase === "connected" || state.phase === "offline",
359
+ );
360
+
361
+ expect(settled).toMatchObject({
362
+ phase: "connected",
363
+ network: "unknown",
364
+ attempt: 1,
365
+ lastFailure: null,
366
+ });
367
+ expect(yield* Ref.get(harness.prepareCount)).toBe(1);
368
+ }),
369
+ );
370
+
371
+ it.effect(
372
+ "keeps a primary attempt through an offline report and restarts it when the network returns",
373
+ () =>
374
+ Effect.gen(function* () {
375
+ const started = yield* Deferred.make<void>();
376
+ const harness = yield* makeHarness({
377
+ prepare: (attempt) =>
378
+ attempt === 1
379
+ ? Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never))
380
+ : Effect.succeed(PREPARED_CONNECTION),
381
+ });
382
+ const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
383
+ initiallyDesired: true,
384
+ }).pipe(Effect.provide(harness.dependencies));
385
+
386
+ yield* Deferred.await(started);
387
+ yield* harness.setNetworkStatus("offline");
388
+ yield* TestClock.adjust("1 second");
389
+ expect(yield* Ref.get(harness.prepareCount)).toBe(1);
390
+
391
+ yield* harness.setNetworkStatus("online");
392
+ yield* awaitState(supervisor.state, (state) => state.phase === "connected");
393
+ expect(yield* Ref.get(harness.prepareCount)).toBe(2);
394
+ }).pipe(Effect.provide(TestClock.layer())),
395
+ );
396
+
397
it.effect("keeps the latest failure visible throughout the next connection attempt", () =>
398
Effect.gen(function* () {
399
const harness = yield* makeHarness({
@@ -763,6 +811,29 @@ describe("EnvironmentSupervisor", () => {
811
}).pipe(Effect.provide(TestClock.layer())),
812
);
813
814
+ it.effect("checks the primary environment's socket when the device reports offline", () =>
815
+ Effect.gen(function* () {
816
+ const harness = yield* makeSilentSocketHarness();
817
+ const target = new PrimaryConnectionTarget({
818
+ ...TARGET,
819
+ httpBaseUrl: "http://127.0.0.1:3773",
820
+ wsBaseUrl: "ws://127.0.0.1:3773",
821
+ });
822
+ const supervisor = yield* EnvironmentSupervisor.make(
823
+ { ...TARGET_ENTRY, target },
824
+ { initiallyDesired: true },
825
+ ).pipe(Effect.provide(harness.dependencies));
826
+
827
+ yield* awaitState(supervisor.state, (state) => state.phase === "connected");
828
+ yield* harness.setNetworkStatus("offline");
829
+ yield* TestClock.adjust("500 millis");
830
+ yield* awaitState(
831
+ supervisor.state,
832
+ (state) => state.phase === "connected" && state.generation === 2,
833
+ );
834
+ }).pipe(Effect.provide(TestClock.layer())),
835
+ );
836
+
837
it.effect("replaces a remote socket after a long resume while the network reports offline", () =>
838
Effect.gen(function* () {
839
const harness = yield* makeSilentSocketHarness();
@@ -971,7 +1042,7 @@ describe("EnvironmentSupervisor", () => {
1042
const harness = yield* makeHarness({
1043
probe: (attempt) => (attempt === 1 ? Effect.never : Effect.void),
1044
});
974
- const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
1045
+ const supervisor = yield* EnvironmentSupervisor.make(LAN_ENTRY, {
1046
initiallyDesired: true,
1047
}).pipe(Effect.provide(harness.dependencies));
1048
packages/client-runtime/src/connection/supervisor.ts
+23
-6
@@ -288,9 +288,14 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
288
const connectivity = yield* Connectivity.Connectivity;
289
const driver = yield* ConnectionDriver.ConnectionDriver;
290
const wakeups = yield* ConnectionWakeups.ConnectionWakeups;
291
+ // The primary environment served this client or runs beside it on loopback,
292
+ // so the device reporting offline (often wrongly) never parks it. The report
293
+ // still probes its live session, like any other network change.
294
+ const targetNetwork = (network: NetworkStatus): NetworkStatus =>
295
+ network === "offline" && target._tag === "PrimaryConnectionTarget" ? "unknown" : network;
296
const initialIntent: SupervisorIntent = {
297
desired: options?.initiallyDesired ?? false,
293
- network: yield* connectivity.status,
298
+ network: targetNetwork(yield* connectivity.status),
299
};
300
const intent = yield* Ref.make(initialIntent);
301
const signals = yield* Queue.unbounded<SupervisorSignal>();
@@ -570,6 +575,9 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
575
});
576
577
const waitForEstablishmentInterrupt = Effect.fnUntraced(function* () {
578
+ // A primary attempt keeps running through an offline report, so the
579
+ // network returning restarts it in case it stalled meanwhile.
580
+ let deviceOffline = (yield* connectivity.status) === "offline";
581
for (;;) {
582
const next = yield* Queue.take(signals);
583
switch (next._tag) {
@@ -577,8 +585,14 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
585
case "RetryRequested":
586
return false;
587
case "NetworkChanged":
580
- if (next.network === "offline") {
581
- return false;
588
+ if (target._tag !== "PrimaryConnectionTarget") {
589
+ if (next.network === "offline") {
590
+ return false;
591
+ }
592
+ } else if (next.network === "offline") {
593
+ deviceOffline = true;
594
+ } else if (deviceOffline) {
595
+ return true;
596
}
597
break;
598
case "ConnectRequested":
@@ -1169,9 +1183,12 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
1183
1184
yield* connectivity.changes.pipe(
1185
Stream.runForEach((network) =>
1172
- Ref.modify(intent, (current) =>
1173
- current.network === network ? [false, current] : ([true, { ...current, network }] as const),
1174
- ).pipe(
1186
+ Ref.modify(intent, (current) => {
1187
+ const next = targetNetwork(network);
1188
+ return current.network === next
1189
+ ? [false, current]
1190
+ : ([true, { ...current, network: next }] as const);
1191
+ }).pipe(
1192
Effect.flatMap((changed) =>
1193
changed
1194
? (network === "offline" ? Effect.void : forgetSilentRoutes).pipe(