The best way to control Claude Code, Codex and more from any computer or phone
fix(client-runtime): new T3 Connect machines appear without signing in again (#29788)
Julius Marminge committed
Nov 13, 2026 at 17:51 UTC
757f71a7f666cecd1a0e82200c73734b6a0c9300
5 files changed
+273
-4
apps/web/src/connection/platform.ts
+18
@@ -142,6 +142,24 @@ const layerWakeups = Wakeups.layer({
142
}),
143
).pipe(Effect.asVoid),
144
),
145
+ // A window left visible beside another app never fires visibilitychange when refocused.
146
+ Stream.callback<"application-focused">((queue) =>
147
+ Effect.acquireRelease(
148
+ Effect.sync(() => {
149
+ const listener = () => {
150
+ if (document.visibilityState === "visible") {
151
+ Queue.offerUnsafe(queue, "application-focused");
152
+ }
153
+ };
154
+ window.addEventListener("focus", listener);
155
+ return listener;
156
+ }),
157
+ (listener) =>
158
+ Effect.sync(() => {
159
+ window.removeEventListener("focus", listener);
160
+ }),
161
+ ).pipe(Effect.asVoid),
162
+ ),
163
managedRelayAccountChanges(appAtomRegistry).pipe(
164
Stream.map(() => "credentials-changed" as const),
165
),
packages/client-runtime/src/connection/supervisor.ts
+1
@@ -1281,6 +1281,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
1281
Effect.forkScoped,
1282
);
1283
yield* wakeups.changes.pipe(
1284
+ Stream.filter(ConnectionWakeups.reachesConnectionSupervisor),
1285
Stream.runForEach((reason) =>
1286
// A long suspension can hide a network change the platform never reported.
1287
(reason === "network-changed" || reason === "application-active-long"
packages/client-runtime/src/connection/wakeups.ts
+15
@@ -6,6 +6,7 @@ export type ConnectionWakeup =
6
| "application-active"
7
| "application-active-probe"
8
| "application-active-long"
9
+ | "application-focused"
10
| "credentials-changed"
11
| "network-changed";
12
@@ -25,6 +26,20 @@ export function shouldResubscribeAfterWakeup(reason: ConnectionWakeup): boolean
26
return isApplicationActiveWakeup(reason);
27
}
28
29
+// A brief app switch (the mobile probe) does not justify listing the whole account again.
30
+export function shouldRelistAccountAfterWakeup(reason: ConnectionWakeup): boolean {
31
+ return (
32
+ reason === "application-active" ||
33
+ reason === "application-active-long" ||
34
+ reason === "application-focused"
35
+ );
36
+}
37
+
38
+// Focus fires on every window switch; any wakeup would arm a running probe's silent-socket check.
39
+export function reachesConnectionSupervisor(reason: ConnectionWakeup): boolean {
40
+ return reason !== "application-focused";
41
+}
42
+
43
export class ConnectionWakeups extends Context.Service<
44
ConnectionWakeups,
45
{
packages/client-runtime/src/relay/discovery.test.ts
+188
-3
@@ -61,6 +61,7 @@ function status(
61
const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
62
const networkStatus = yield* SubscriptionRef.make<NetworkStatus>("online");
63
const listCalls = yield* Ref.make(0);
64
+ const statusCalls = yield* Ref.make(0);
65
const listedEnvironments =
66
yield* Ref.make<ReadonlyArray<RelayClientEnvironmentRecord>>(environments);
67
const listFailure = yield* Ref.make<ManagedRelay.ManagedRelayClientError | null>(null);
@@ -69,7 +70,7 @@ const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
70
const sessionIdentity = { accountId: "account-1" };
71
const wakeups = yield* SubscriptionRef.make<{
72
readonly sequence: number;
72
- readonly reason: "application-active" | "credentials-changed";
73
+ readonly reason: ConnectionWakeups.ConnectionWakeup;
74
}>({
75
sequence: 0,
76
reason: "application-active",
@@ -107,7 +108,8 @@ const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
108
return yield* Ref.get(listedEnvironments);
109
}),
110
getEnvironmentStatus: ({ environmentId }) =>
110
- Ref.get(statusRequests).pipe(
111
+ Ref.update(statusCalls, (current) => current + 1).pipe(
112
+ Effect.andThen(Ref.get(statusRequests)),
113
Effect.flatMap((requests) => Deferred.await(requests.get(environmentId)!)),
114
),
115
listDevices: () => Effect.die("unused"),
@@ -174,8 +176,9 @@ const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
176
clerkToken,
177
networkStatus,
178
secondListCall,
179
+ statusCalls,
180
statusRequests,
178
- wake: (reason: "application-active" | "credentials-changed") =>
181
+ wake: (reason: ConnectionWakeups.ConnectionWakeup) =>
182
SubscriptionRef.update(wakeups, (event) => ({
183
sequence: event.sequence + 1,
184
reason,
@@ -471,6 +474,188 @@ describe("RelayEnvironmentDiscovery", () => {
474
}),
475
);
476
477
+ it.effect.each(["application-active", "application-active-long", "application-focused"] as const)(
478
+ "lists the account again when the app returns to the foreground (%s)",
479
+ (reason) =>
480
+ Effect.gen(function* () {
481
+ const harness = yield* makeHarness();
482
+ yield* Ref.set(harness.listedEnvironments, [environments[0]!]);
483
+ yield* Effect.gen(function* () {
484
+ const discovery = yield* RelayEnvironmentDiscovery.RelayEnvironmentDiscovery;
485
+ const requests = yield* Ref.get(harness.statusRequests);
486
+ for (const environment of environments) {
487
+ yield* Deferred.succeed(
488
+ requests.get(environment.environmentId)!,
489
+ status(environment, "online"),
490
+ );
491
+ }
492
+ // Let the scoped connectivity and wakeup subscriptions start before signing in.
493
+ yield* Effect.yieldNow;
494
+ yield* harness.wake("credentials-changed");
495
+ const signedIn = yield* SubscriptionRef.changes(discovery.state).pipe(
496
+ Stream.filter((state) => state.listed && !state.refreshing),
497
+ Stream.runHead,
498
+ Effect.map(Option.getOrThrow),
499
+ );
500
+ expect([...signedIn.environments.keys()]).toEqual([environments[0]!.environmentId]);
501
+
502
+ yield* Ref.set(harness.listedEnvironments, environments);
503
+ yield* harness.wake(reason);
504
+ const relisted = yield* SubscriptionRef.changes(discovery.state).pipe(
505
+ Stream.filter(
506
+ (state) => state.environments.size === environments.length && !state.refreshing,
507
+ ),
508
+ Stream.runHead,
509
+ Effect.map(Option.getOrThrow),
510
+ );
511
+ expect([...relisted.environments.keys()]).toEqual(
512
+ environments.map((environment) => environment.environmentId),
513
+ );
514
+ }).pipe(Effect.provide(harness.layer), Effect.scoped);
515
+ }),
516
+ );
517
+
518
+ it.effect("lists a registration that lands while an earlier refresh checks statuses", () =>
519
+ Effect.gen(function* () {
520
+ const harness = yield* makeHarness();
521
+ yield* Ref.set(harness.listedEnvironments, [environments[0]!]);
522
+ yield* Effect.gen(function* () {
523
+ const discovery = yield* RelayEnvironmentDiscovery.RelayEnvironmentDiscovery;
524
+ yield* Effect.yieldNow;
525
+ const refreshFiber = yield* Effect.forkChild(discovery.refresh);
526
+ yield* SubscriptionRef.changes(discovery.state).pipe(
527
+ Stream.filter((state) => state.listed),
528
+ Stream.runHead,
529
+ );
530
+
531
+ yield* Ref.set(harness.listedEnvironments, environments);
532
+ yield* harness.wake("application-active");
533
+ // Let the wakeup reach discovery while the first refresh still holds the lock.
534
+ yield* Effect.yieldNow;
535
+ const requests = yield* Ref.get(harness.statusRequests);
536
+ for (const environment of environments) {
537
+ yield* Deferred.succeed(
538
+ requests.get(environment.environmentId)!,
539
+ status(environment, "online"),
540
+ );
541
+ }
542
+ yield* Fiber.join(refreshFiber);
543
+
544
+ const relisted = yield* SubscriptionRef.changes(discovery.state).pipe(
545
+ Stream.filter(
546
+ (state) => state.environments.size === environments.length && !state.refreshing,
547
+ ),
548
+ Stream.runHead,
549
+ Effect.map(Option.getOrThrow),
550
+ );
551
+ expect([...relisted.environments.keys()]).toEqual(
552
+ environments.map((environment) => environment.environmentId),
553
+ );
554
+ }).pipe(Effect.provide(harness.layer), Effect.scoped);
555
+ }),
556
+ );
557
+
558
+ it.effect("coalesces foreground checks that arrive during an in-flight refresh", () =>
559
+ Effect.gen(function* () {
560
+ const harness = yield* makeHarness();
561
+ yield* Effect.gen(function* () {
562
+ const discovery = yield* RelayEnvironmentDiscovery.RelayEnvironmentDiscovery;
563
+ yield* Effect.yieldNow;
564
+ const refreshFiber = yield* Effect.forkChild(discovery.refresh);
565
+ yield* SubscriptionRef.changes(discovery.state).pipe(
566
+ Stream.filter((state) => state.listed),
567
+ Stream.runHead,
568
+ );
569
+
570
+ for (const reason of [
571
+ "application-active",
572
+ "application-focused",
573
+ "application-active",
574
+ "application-focused",
575
+ "application-active-long",
576
+ ] as const) {
577
+ yield* harness.wake(reason);
578
+ yield* Effect.yieldNow;
579
+ }
580
+ // Waits on the lock behind every refresh the wakeups queued.
581
+ const lastRefresh = yield* Effect.forkChild(discovery.refresh);
582
+ yield* Effect.yieldNow;
583
+ const requests = yield* Ref.get(harness.statusRequests);
584
+ for (const environment of environments) {
585
+ yield* Deferred.succeed(
586
+ requests.get(environment.environmentId)!,
587
+ status(environment, "online"),
588
+ );
589
+ }
590
+ yield* Fiber.join(refreshFiber);
591
+ yield* Fiber.join(lastRefresh);
592
+
593
+ // The first refresh, one queued refresh, one trailing check's listing, and the last refresh.
594
+ expect(yield* Ref.get(harness.listCalls)).toBe(4);
595
+ }).pipe(Effect.provide(harness.layer), Effect.scoped);
596
+ }),
597
+ );
598
+
599
+ it.effect("keeps listed rows when the foreground check cannot list the account", () =>
600
+ Effect.gen(function* () {
601
+ const harness = yield* makeHarness();
602
+ yield* Effect.gen(function* () {
603
+ const discovery = yield* RelayEnvironmentDiscovery.RelayEnvironmentDiscovery;
604
+ const requests = yield* Ref.get(harness.statusRequests);
605
+ for (const environment of environments) {
606
+ yield* Deferred.succeed(
607
+ requests.get(environment.environmentId)!,
608
+ status(environment, "online"),
609
+ );
610
+ }
611
+ yield* Effect.yieldNow;
612
+ yield* discovery.refresh;
613
+ const settled = yield* SubscriptionRef.get(discovery.state);
614
+
615
+ yield* Ref.set(
616
+ harness.listFailure,
617
+ new ManagedRelay.ManagedRelayRequestFailedError({
618
+ action: "list relay-managed environments",
619
+ cause: new Error("Relay request failed."),
620
+ }),
621
+ );
622
+ yield* harness.wake("application-active");
623
+ yield* Deferred.await(harness.secondListCall);
624
+
625
+ const after = yield* SubscriptionRef.get(discovery.state);
626
+ expect(after).toBe(settled);
627
+ expect(after.environments.size).toBe(environments.length);
628
+ expect(Option.isNone(after.error)).toBe(true);
629
+ }).pipe(Effect.provide(harness.layer), Effect.scoped);
630
+ }),
631
+ );
632
+
633
+ it.effect("leaves an unchanged account list untouched when the app returns", () =>
634
+ Effect.gen(function* () {
635
+ const harness = yield* makeHarness();
636
+ yield* Effect.gen(function* () {
637
+ const discovery = yield* RelayEnvironmentDiscovery.RelayEnvironmentDiscovery;
638
+ const requests = yield* Ref.get(harness.statusRequests);
639
+ for (const environment of environments) {
640
+ yield* Deferred.succeed(
641
+ requests.get(environment.environmentId)!,
642
+ status(environment, "online"),
643
+ );
644
+ }
645
+ yield* Effect.yieldNow;
646
+ yield* discovery.refresh;
647
+ const settled = yield* SubscriptionRef.get(discovery.state);
648
+
649
+ yield* harness.wake("application-active");
650
+ yield* Deferred.await(harness.secondListCall);
651
+
652
+ expect(yield* SubscriptionRef.get(discovery.state)).toBe(settled);
653
+ expect(yield* Ref.get(harness.listCalls)).toBe(2);
654
+ expect(yield* Ref.get(harness.statusCalls)).toBe(environments.length);
655
+ }).pipe(Effect.provide(harness.layer), Effect.scoped);
656
+ }),
657
+ );
658
+
659
it.effect("settles to a clean empty state when refreshed while signed out", () =>
660
Effect.gen(function* () {
661
const harness = yield* makeHarness();
packages/client-runtime/src/relay/discovery.ts
+51
-1
@@ -121,6 +121,8 @@ export const make = Effect.fn("RelayEnvironmentDiscovery.make")(function* () {
121
const wakeups = yield* ConnectionWakeups.ConnectionWakeups;
122
const state = yield* SubscriptionRef.make(EMPTY_RELAY_ENVIRONMENT_DISCOVERY_STATE);
123
const refreshLock = yield* Semaphore.make(1);
124
+ const foregroundCheckLock = yield* Semaphore.make(1);
125
+ const registrationCheckRequested = yield* Ref.make(false);
126
const hasRefreshed = yield* Ref.make(false);
127
const accountGeneration = yield* Ref.make(0);
128
const activeAccountId = yield* Ref.make<Option.Option<string>>(Option.none());
@@ -320,6 +322,52 @@ export const make = Effect.fn("RelayEnvironmentDiscovery.make")(function* () {
322
),
323
);
324
325
+ // Refreshes only when the account's registrations changed, so foregrounding keeps unchanged rows.
326
+ const checkRegistrations = Effect.fn("RelayEnvironmentDiscovery.checkRegistrations")(
327
+ function* () {
328
+ if (
329
+ !(yield* Ref.get(hasRefreshed)) ||
330
+ (yield* connectivity.status) === "offline" ||
331
+ Option.isNone(yield* session.identity)
332
+ ) {
333
+ return;
334
+ }
335
+ const current = yield* SubscriptionRef.get(state);
336
+ if (!current.listed || current.refreshing || Option.isSome(current.error)) {
337
+ return yield* refresh;
338
+ }
339
+ const generation = yield* Ref.get(accountGeneration);
340
+ const clerkToken = yield* session.clerkToken;
341
+ const listed = yield* relay.listEnvironments({ clerkToken });
342
+ if ((yield* Ref.get(accountGeneration)) !== generation) {
343
+ return;
344
+ }
345
+ const known = (yield* SubscriptionRef.get(state)).environments;
346
+ if (
347
+ listed.length === known.size &&
348
+ listed.every((environment) => known.has(environment.environmentId))
349
+ ) {
350
+ return;
351
+ }
352
+ yield* refresh;
353
+ },
354
+ Effect.catch((error) =>
355
+ Effect.logDebug("Relay foreground registration check failed", { error }),
356
+ ),
357
+ );
358
+ // A wakeup during a running check is answered by one more check once that one finishes.
359
+ const requestRegistrationCheck = Ref.set(registrationCheckRequested, true).pipe(
360
+ Effect.andThen(
361
+ foregroundCheckLock.withPermitsIfAvailable(1)(
362
+ Effect.gen(function* () {
363
+ while (yield* Ref.getAndSet(registrationCheckRequested, false)) {
364
+ yield* checkRegistrations();
365
+ }
366
+ }),
367
+ ),
368
+ ),
369
+ );
370
+
371
yield* connectivity.changes.pipe(
372
Stream.changes,
373
Stream.runForEach((networkStatus) =>
@@ -345,7 +393,9 @@ export const make = Effect.fn("RelayEnvironmentDiscovery.make")(function* () {
393
yield* SubscriptionRef.set(state, EMPTY_RELAY_ENVIRONMENT_DISCOVERY_STATE);
394
yield* refresh.pipe(Effect.forkScoped);
395
})
348
- : Effect.void,
396
+ : ConnectionWakeups.shouldRelistAccountAfterWakeup(reason)
397
+ ? requestRegistrationCheck.pipe(Effect.forkScoped)
398
+ : Effect.void,
399
),
400
Effect.forkScoped,
401
);