The best way to control Claude Code, Codex and more from any computer or phone
fix(client-runtime): idle clients stop tracing unchanged platform registrations (#26511)
Co-authored-by: PR Batch Tester <agent@local.test> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net>
shivam committed
Oct 31, 2026 at 07:10 UTC
f736543c1e4d195f0b093b42bc1860804f691c63
2 files changed
+245
-60
packages/client-runtime/src/connection/registry.test.ts
+191
-32
@@ -20,6 +20,7 @@ import * as Scheduler from "effect/Scheduler";
20
import * as Scope from "effect/Scope";
21
import * as Stream from "effect/Stream";
22
import * as SubscriptionRef from "effect/SubscriptionRef";
23
+import * as Tracer from "effect/Tracer";
24
25
import * as ClientCapabilities from "../platform/capabilities.ts";
26
import * as TokenStore from "../authorization/tokenStore.ts";
@@ -157,6 +158,9 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
158
readonly beforeRegistrationRemove?: (
159
environmentId: EnvironmentId,
160
) => Effect.Effect<void, Persistence.ConnectionPersistenceError>;
161
+ readonly beforeCredentialPut?: (
162
+ connectionId: string,
163
+ ) => Effect.Effect<void, ConnectionTransientError>;
164
readonly checkRoute?: (route: ConnectionRoute) => RouteCheck;
165
readonly prepareRoute?: (target: ConnectionTarget) => ConnectionBlockedError | undefined;
166
readonly beforePrepareRoute?: (
@@ -337,11 +341,15 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
341
Effect.map((current) => Option.fromUndefinedOr(current.get(connectionId))),
342
),
343
put: (connectionId, credential) =>
340
- Ref.update(storedCredentials, (current) => {
341
- const next = new Map(current);
342
- next.set(connectionId, credential);
343
- return next;
344
- }),
344
+ (options?.beforeCredentialPut?.(connectionId) ?? Effect.void).pipe(
345
+ Effect.andThen(
346
+ Ref.update(storedCredentials, (current) => {
347
+ const next = new Map(current);
348
+ next.set(connectionId, credential);
349
+ return next;
350
+ }),
351
+ ),
352
+ ),
353
remove: (connectionId) =>
354
Ref.update(storedCredentials, (current) => {
355
const next = new Map(current);
@@ -503,6 +511,20 @@ function replaceRoutes(
511
return [...others.slice(0, insertAt), ...routes, ...others.slice(insertAt)];
512
}
513
514
+function registrySpanNames<A, E, R>(effect: Effect.Effect<A, E, R>) {
515
+ const names: Array<string> = [];
516
+ const tracer = Tracer.make({
517
+ span: (options) => {
518
+ if (options.name.startsWith("EnvironmentRegistry.")) names.push(options.name);
519
+ return new Tracer.NativeSpan(options);
520
+ },
521
+ });
522
+ return effect.pipe(
523
+ Effect.withTracer(tracer),
524
+ Effect.map(() => names),
525
+ );
526
+}
527
+
528
function awaitConnectionState(
529
registry: EnvironmentRegistry.EnvironmentRegistry["Service"],
530
environmentId: EnvironmentId,
@@ -1434,7 +1456,18 @@ describe("EnvironmentRegistry", () => {
1456
1457
it.effect("a replaced platform bearer reaches a connected session's HTTP requests", () =>
1458
Effect.gen(function* () {
1437
- const harness = yield* makeHarness([]);
1459
+ let failCredentialWrites = false;
1460
+ const harness = yield* makeHarness([], [], [], {
1461
+ beforeCredentialPut: () =>
1462
+ failCredentialWrites
1463
+ ? Effect.fail(
1464
+ new ConnectionTransientError({
1465
+ reason: "remote-unavailable",
1466
+ detail: "Storage unavailable",
1467
+ }),
1468
+ )
1469
+ : Effect.void,
1470
+ });
1471
1472
yield* Effect.gen(function* () {
1473
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
@@ -1463,6 +1496,11 @@ describe("EnvironmentRegistry", () => {
1496
);
1497
expect(yield* preparedToken).toBe("old-bearer");
1498
1499
+ failCredentialWrites = true;
1500
+ yield* registry.reconcilePlatform([registration("new-bearer")]);
1501
+ expect(yield* preparedToken).toBe("old-bearer");
1502
+
1503
+ failCredentialWrites = false;
1504
yield* registry.reconcilePlatform([registration("new-bearer")]);
1505
1506
expect(yield* preparedToken).toBe("new-bearer");
@@ -1554,36 +1592,36 @@ describe("EnvironmentRegistry", () => {
1592
}),
1593
);
1594
1557
- it.effect("gives a primary platform registration precedence over persisted registrations", () =>
1595
+ it.effect("applies overlapping platform snapshots in order", () =>
1596
Effect.gen(function* () {
1559
- const shadowedTarget = new RelayConnectionTarget({
1560
- environmentId: TARGET.environmentId,
1561
- label: "Shadowed relay environment",
1562
- });
1563
- const harness = yield* makeHarness([shadowedTarget]);
1597
+ const started = yield* Deferred.make<void>();
1598
+ const resume = yield* Deferred.make<void>();
1599
+ const harness = yield* makeHarness([TARGET]);
1600
const permissions = yield* makePullRequestRoutingPermissions({
1565
- read: Effect.succeed([]),
1566
- write: () => Effect.void,
1601
+ read: Effect.succeed([
1602
+ { environmentId: TARGET.environmentId, connectionKey: "saved", permission: "read-write" },
1603
+ ]),
1604
+ write: () =>
1605
+ Deferred.succeed(started, undefined).pipe(Effect.andThen(Deferred.await(resume))),
1606
});
1568
- const shadowedEntry = { target: shadowedTarget, profile: Option.none(), enabled: true };
1569
- yield* permissions.set(shadowedEntry, "read-write");
1607
1608
yield* Effect.gen(function* () {
1609
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
1573
- yield* registry.reconcilePlatform([new PrimaryConnectionRegistration({ target: TARGET })]);
1574
- expect(yield* permissions.get(shadowedEntry)).toBe("off");
1575
-
1576
- expect(
1577
- (yield* SubscriptionRef.get(registry.entries)).get(TARGET.environmentId)?.target,
1578
- ).toEqual(TARGET);
1579
- expect(hasRoutes(yield* Ref.get(harness.storedTargets), TARGET.environmentId)).toBe(false);
1580
-
1581
- yield* registry.register(new RelayConnectionRegistration({ target: shadowedTarget }));
1610
+ const installing = yield* registry
1611
+ .reconcilePlatform([new PrimaryConnectionRegistration({ target: TARGET })])
1612
+ .pipe(Effect.forkChild({ startImmediately: true }));
1613
+ yield* Deferred.await(started);
1614
+ const removing = yield* registry
1615
+ .reconcilePlatform([])
1616
+ .pipe(Effect.forkChild({ startImmediately: true }));
1617
+ yield* Deferred.succeed(resume, undefined);
1618
+ yield* Fiber.join(installing);
1619
+ yield* Fiber.join(removing);
1620
1583
- expect(
1584
- (yield* SubscriptionRef.get(registry.entries)).get(TARGET.environmentId)?.target,
1585
- ).toEqual(TARGET);
1586
- expect(hasRoutes(yield* Ref.get(harness.storedTargets), TARGET.environmentId)).toBe(false);
1621
+ expect((yield* SubscriptionRef.get(registry.entries)).has(TARGET.environmentId)).toBe(
1622
+ false,
1623
+ );
1624
+ expect(yield* registrySpanNames(registry.reconcilePlatform([]))).toEqual([]);
1625
}).pipe(
1626
Effect.provide(harness.layer),
1627
Effect.provideService(PullRequestRoutingPermissions, permissions),
@@ -1592,6 +1630,110 @@ describe("EnvironmentRegistry", () => {
1630
}),
1631
);
1632
1633
+ it.effect("gives platform registrations precedence over persisted registrations", () =>
1634
+ Effect.gen(function* () {
1635
+ for (const registration of [
1636
+ new PrimaryConnectionRegistration({ target: TARGET }),
1637
+ new BearerConnectionRegistration({
1638
+ target: BEARER_TARGET,
1639
+ profile: BEARER_PROFILE,
1640
+ credential: new BearerConnectionCredential({ token: "platform-bearer" }),
1641
+ }),
1642
+ ]) {
1643
+ const target = registration.target;
1644
+ const shadowedTarget =
1645
+ registration._tag === "BearerConnectionRegistration"
1646
+ ? registration.target
1647
+ : new RelayConnectionTarget({
1648
+ environmentId: target.environmentId,
1649
+ label: "Shadowed relay environment",
1650
+ });
1651
+ let failRemoval = true;
1652
+ const harness = yield* makeHarness(
1653
+ [shadowedTarget],
1654
+ registration._tag === "BearerConnectionRegistration" ? [registration.profile] : [],
1655
+ registration._tag === "BearerConnectionRegistration"
1656
+ ? [
1657
+ [
1658
+ registration.target.connectionId,
1659
+ new BearerConnectionCredential({ token: "saved-bearer" }),
1660
+ ],
1661
+ ]
1662
+ : [],
1663
+ {
1664
+ beforeRegistrationRemove: () =>
1665
+ failRemoval
1666
+ ? Effect.fail(
1667
+ new Persistence.ConnectionPersistenceError({
1668
+ operation: "remove-connection",
1669
+ message: "Storage unavailable",
1670
+ }),
1671
+ )
1672
+ : Effect.void,
1673
+ },
1674
+ );
1675
+ const permissions = yield* makePullRequestRoutingPermissions({
1676
+ read: Effect.succeed([]),
1677
+ write: () => Effect.void,
1678
+ });
1679
+ const shadowedEntry = {
1680
+ target: shadowedTarget,
1681
+ profile:
1682
+ registration._tag === "BearerConnectionRegistration"
1683
+ ? Option.some(registration.profile)
1684
+ : Option.none(),
1685
+ enabled: true,
1686
+ };
1687
+ yield* permissions.set(shadowedEntry, "read-write");
1688
+
1689
+ yield* Effect.gen(function* () {
1690
+ const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
1691
+ yield* registry.reconcilePlatform([registration]);
1692
+ expect(yield* permissions.get(shadowedEntry)).toBe("off");
1693
+ expect(hasRoutes(yield* Ref.get(harness.storedTargets), target.environmentId)).toBe(true);
1694
+
1695
+ failRemoval = false;
1696
+ yield* registry.reconcilePlatform([registration]);
1697
+
1698
+ expect(
1699
+ (yield* SubscriptionRef.get(registry.entries)).get(target.environmentId)?.target,
1700
+ ).toEqual(target);
1701
+ expect(hasRoutes(yield* Ref.get(harness.storedTargets), target.environmentId)).toBe(
1702
+ false,
1703
+ );
1704
+ if (registration._tag === "BearerConnectionRegistration") {
1705
+ expect(
1706
+ (yield* Ref.get(harness.storedCredentials)).get(registration.target.connectionId),
1707
+ ).toEqual(registration.credential);
1708
+ expect(yield* registrySpanNames(registry.reconcilePlatform([registration]))).toEqual(
1709
+ [],
1710
+ );
1711
+ }
1712
+
1713
+ yield* registry.register(
1714
+ new RelayConnectionRegistration({
1715
+ target: new RelayConnectionTarget({
1716
+ environmentId: target.environmentId,
1717
+ label: "Shadowed relay environment",
1718
+ }),
1719
+ }),
1720
+ );
1721
+
1722
+ expect(
1723
+ (yield* SubscriptionRef.get(registry.entries)).get(target.environmentId)?.target,
1724
+ ).toEqual(target);
1725
+ expect(hasRoutes(yield* Ref.get(harness.storedTargets), target.environmentId)).toBe(
1726
+ false,
1727
+ );
1728
+ }).pipe(
1729
+ Effect.provide(harness.layer),
1730
+ Effect.provideService(PullRequestRoutingPermissions, permissions),
1731
+ Effect.scoped,
1732
+ );
1733
+ }
1734
+ }),
1735
+ );
1736
+
1737
it.effect("rechecks platform ownership after waiting for the environment lease", () =>
1738
Effect.gen(function* () {
1739
const registrationStarted = yield* Deferred.make<void>();
@@ -1798,8 +1940,13 @@ describe("EnvironmentRegistry", () => {
1940
);
1941
yield* permissions.set(entry, "read-write");
1942
1801
- yield* registry.reconcilePlatform([registration]);
1943
+ const repeated = yield* registrySpanNames(
1944
+ registry.reconcilePlatform([
1945
+ new PrimaryConnectionRegistration({ target: new PrimaryConnectionTarget(TARGET) }),
1946
+ ]),
1947
+ );
1948
1949
+ expect(repeated).toEqual([]);
1950
expect(yield* Ref.get(harness.sessions)).toHaveLength(1);
1951
expect(yield* permissions.get(entry)).toBe("read-write");
1952
@@ -1814,18 +1961,30 @@ describe("EnvironmentRegistry", () => {
1961
expect(
1962
(yield* SubscriptionRef.get(registry.entries)).get(TARGET.environmentId)?.target,
1963
).toEqual(TARGET);
1817
- yield* registry.reconcilePlatform([]);
1818
- expect((yield* SubscriptionRef.get(registry.entries)).has(TARGET.environmentId)).toBe(true);
1964
1965
failWrites = false;
1966
yield* registry.reconcilePlatform([
1967
new PrimaryConnectionRegistration({ target: changedTarget }),
1968
]);
1969
+ expect(
1970
+ (yield* SubscriptionRef.get(registry.entries)).get(TARGET.environmentId)?.target,
1971
+ ).toEqual(changedTarget);
1972
expect(yield* permissions.get(entry)).toBe("off");
1973
yield* registry.reconcilePlatform([registration]);
1974
expect(yield* permissions.get(entry)).toBe("off");
1975
+
1976
yield* permissions.set(entry, "read-write");
1977
+ failWrites = true;
1978
+ yield* registry.reconcilePlatform([]);
1979
+ expect((yield* SubscriptionRef.get(registry.entries)).has(TARGET.environmentId)).toBe(true);
1980
+ failWrites = false;
1981
yield* registry.reconcilePlatform([]);
1982
+ expect((yield* SubscriptionRef.get(registry.entries)).has(TARGET.environmentId)).toBe(
1983
+ false,
1984
+ );
1985
+ expect(yield* permissions.get(entry)).toBe("off");
1986
+ expect(yield* registrySpanNames(registry.reconcilePlatform([]))).toEqual([]);
1987
+
1988
yield* registry.reconcilePlatform([registration]);
1989
expect(yield* permissions.get(entry)).toBe("off");
1990
}).pipe(
packages/client-runtime/src/connection/registry.ts
+54
-28
@@ -626,9 +626,10 @@ export const make = Effect.gen(function* () {
626
function* (registration: PlatformConnectionRegistration) {
627
const registered = connectionRegistrationCatalogEntry(registration);
628
const target = registered.target;
629
- yield* withLeaseLock(
629
+ return yield* withLeaseLock(
630
target.environmentId,
631
Effect.gen(function* () {
632
+ let applied = true;
633
const previous = (yield* SubscriptionRef.get(entries)).get(target.environmentId);
634
const entry: ConnectionCatalogEntry =
635
previous?.unsupportedReason !== undefined &&
@@ -654,7 +655,7 @@ export const make = Effect.gen(function* () {
655
),
656
Effect.exit,
657
);
657
- if (Exit.isFailure(revoked)) return;
658
+ if (Exit.isFailure(revoked)) return false;
659
}
660
yield* Ref.update(platformEnvironmentIds, (current) => {
661
const next = new Set(current);
@@ -666,30 +667,17 @@ export const make = Effect.gen(function* () {
667
// on their own loopback origin, so they authenticate with a bearer
668
// token instead of the primary's same-origin cookie. Stash it where
669
// the resolver's bearer broker looks it up.
669
- let bearerReplaced = false;
670
+ let bearerChanged = false;
671
if (registration._tag === "BearerConnectionRegistration") {
672
const stored = yield* credentials
673
.get(registration.target.connectionId)
674
.pipe(Effect.orElseSucceed(() => Option.none<ConnectionCredential>()));
674
- const changed =
675
+ bearerChanged =
676
Option.isSome(stored) && !Equal.equals(stored.value, registration.credential);
676
- // Only a bearer that was actually stored can be retried with; a
677
- // failed write leaves the rejected one in place.
678
- bearerReplaced = yield* credentials
679
- .put(registration.target.connectionId, registration.credential)
680
- .pipe(
681
- Effect.as(changed),
682
- Effect.catch((error) =>
683
- Effect.logWarning("Could not store the platform bearer credential.", {
684
- environmentId: target.environmentId,
685
- error,
686
- }).pipe(Effect.as(false)),
687
- ),
688
- );
677
}
678
679
if (persisted) {
692
- yield* registrations.remove(target.environmentId).pipe(
680
+ const removed = yield* registrations.remove(target.environmentId).pipe(
681
Effect.tap(() =>
682
Ref.update(persistedEnvironmentIds, (current) => {
683
const next = new Set(current);
@@ -697,6 +685,7 @@ export const make = Effect.gen(function* () {
685
return next;
686
}),
687
),
688
+ Effect.as(true),
689
Effect.catch((error) =>
690
Effect.logWarning(
691
"Could not remove a persisted registration shadowed by a platform environment.",
@@ -704,9 +693,29 @@ export const make = Effect.gen(function* () {
693
environmentId: target.environmentId,
694
error,
695
},
707
- ),
696
+ ).pipe(Effect.as(false)),
697
),
698
);
699
+ applied &&= removed;
700
+ }
701
+
702
+ let bearerReplaced = false;
703
+ if (registration._tag === "BearerConnectionRegistration") {
704
+ const written = yield* credentials
705
+ .put(registration.target.connectionId, registration.credential)
706
+ .pipe(
707
+ Effect.as(true),
708
+ Effect.catch((error) =>
709
+ Effect.logWarning("Could not store the platform bearer credential.", {
710
+ environmentId: target.environmentId,
711
+ error,
712
+ }).pipe(Effect.as(false)),
713
+ ),
714
+ );
715
+ // Only a bearer that was actually stored can be retried with; a
716
+ // failed write leaves the rejected one in place.
717
+ bearerReplaced = written && bearerChanged;
718
+ applied &&= written;
719
}
720
721
yield* installEntryLocked(entry, { retainEquivalentRuntime: true });
@@ -743,6 +752,7 @@ export const make = Effect.gen(function* () {
752
}
753
}
754
}
755
+ return applied;
756
}),
757
);
758
},
@@ -750,7 +760,7 @@ export const make = Effect.gen(function* () {
760
761
const removePlatformEnvironment = Effect.fn("EnvironmentRegistry.removePlatformEnvironment")(
762
function* (environmentId: EnvironmentId) {
753
- yield* withLeaseLock(
763
+ return yield* withLeaseLock(
764
environmentId,
765
Effect.gen(function* () {
766
const entry = (yield* SubscriptionRef.get(entries)).get(environmentId);
@@ -766,7 +776,7 @@ export const make = Effect.gen(function* () {
776
),
777
Effect.exit,
778
);
769
- if (Exit.isFailure(revoked)) return;
779
+ if (Exit.isFailure(revoked)) return false;
780
yield* Ref.update(platformEnvironmentIds, (current) => {
781
const next = new Set(current);
782
next.delete(environmentId);
@@ -802,27 +812,43 @@ export const make = Effect.gen(function* () {
812
],
813
{ concurrency: "unbounded", discard: true },
814
);
815
+ return true;
816
}),
817
);
818
},
819
);
820
810
- const reconcilePlatform = Effect.fn("EnvironmentRegistry.reconcilePlatform")(function* (
821
+ /** Succeeds with `false` when a best-effort write failed, so an identical repeat retries it. */
822
+ const applyPlatformRegistrations = Effect.fn("EnvironmentRegistry.reconcilePlatform")(function* (
823
platformRegistrations: ReadonlyArray<PlatformConnectionRegistration>,
824
) {
825
const desiredIds = new Set(
826
platformRegistrations.map((registration) => registration.target.environmentId),
827
);
828
const currentPlatformIds = yield* Ref.get(platformEnvironmentIds);
817
- yield* Effect.forEach(
818
- currentPlatformIds,
819
- (environmentId) =>
820
- desiredIds.has(environmentId) ? Effect.void : removePlatformEnvironment(environmentId),
821
- { discard: true },
829
+ const removed = yield* Effect.forEach(currentPlatformIds, (environmentId) =>
830
+ desiredIds.has(environmentId)
831
+ ? Effect.succeed(true)
832
+ : removePlatformEnvironment(environmentId),
833
);
823
- yield* Effect.forEach(platformRegistrations, installPlatformRegistration, { discard: true });
834
+ const installed = yield* Effect.forEach(platformRegistrations, installPlatformRegistration);
835
+ return [...removed, ...installed].every(Boolean);
836
});
837
838
+ const reconcileLock = yield* Semaphore.make(1);
839
+ let appliedPlatformRegistrations: ReadonlyArray<PlatformConnectionRegistration> | undefined;
840
+ // Untraced: platforms repeat unchanged registrations every few seconds, and a
841
+ // span per poll would make idle clients export traces continuously.
842
+ const reconcilePlatform = Effect.fnUntraced(function* (
843
+ platformRegistrations: ReadonlyArray<PlatformConnectionRegistration>,
844
+ ) {
845
+ if (Equal.equals(appliedPlatformRegistrations, platformRegistrations)) return;
846
+ appliedPlatformRegistrations = undefined;
847
+ if (yield* applyPlatformRegistrations(platformRegistrations)) {
848
+ appliedPlatformRegistrations = platformRegistrations;
849
+ }
850
+ }, reconcileLock.withPermits(1));
851
+
852
/** Callers hold its lease lock. */
853
const removeLocked = Effect.fn("EnvironmentRegistry.removeLocked")(function* (
854
environmentId: EnvironmentId,