Skip to content

Commit 49cd484

Browse files
fix(web): a new desktop-local token replaces a dead bearer and wakes its connection
Review follow-up. Two gaps let a desktop-local backend stay unreachable after its bearer stopped working (for example a revoked session, or a backend that restarted on the same port): - The poll reused a cached bearer until it neared expiry, up to 30 days, even after the desktop reported a new bootstrap token. A new token now always triggers an exchange; the old bearer stays registered if that fails. - A connection blocked on authentication waited for a manual retry even after the poll installed a fresh bearer, because the catalog entry carries no credential and so the blocked runtime was kept as-is. The registry now retries a supervisor blocked on authentication when its platform bearer changes. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent db730f7 commit 49cd484

3 files changed

Lines changed: 82 additions & 1 deletion

File tree

‎apps/web/src/connection/platform.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ interface CachedPlatformRegistration {
445445
readonly registration: PlatformConnectionRegistration;
446446
readonly expiresAtEpochMs?: number;
447447
readonly refreshAtEpochMs?: number;
448+
/** The bootstrap token a desktop-local bearer was exchanged for. */
449+
readonly bootstrapToken?: string;
448450
}
449451

450452
export type PrimaryEnvironmentTargetRead =
@@ -643,8 +645,12 @@ const platformConnectionSourceLayer = Layer.effect(
643645
const endpointSignature = `${bootstrap.httpBaseUrl}|${bootstrap.wsBaseUrl}`;
644646
const signature = `${endpointSignature}|${bootstrap.bootstrapToken ?? ""}`;
645647
const cached = previous.get(bootstrap.id);
648+
// A new token from the desktop means something changed (rotation, a
649+
// restarted backend): exchange it rather than reusing a bearer that
650+
// may be dead. The old bearer stays registered if that exchange fails.
646651
if (
647652
cached !== undefined &&
653+
cached.bootstrapToken === bootstrap.bootstrapToken &&
648654
canReuseCachedPlatformRegistration(cached, endpointSignature, nowEpochMs)
649655
) {
650656
next.set(bootstrap.id, cached);
@@ -688,7 +694,13 @@ const platformConnectionSourceLayer = Layer.effect(
688694
Effect.option,
689695
);
690696
if (Option.isSome(built)) {
691-
const cacheEntry = { signature: endpointSignature, ...built.value };
697+
const cacheEntry = {
698+
signature: endpointSignature,
699+
...(bootstrap.bootstrapToken === undefined
700+
? {}
701+
: { bootstrapToken: bootstrap.bootstrapToken }),
702+
...built.value,
703+
};
692704
next.set(bootstrap.id, cacheEntry);
693705
registrations.push(built.value.registration);
694706
} else if (

‎packages/client-runtime/src/connection/registry.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,50 @@ describe("EnvironmentRegistry", () => {
12701270
}),
12711271
);
12721272

1273+
it.effect("a replaced platform bearer wakes a supervisor blocked on the old one", () =>
1274+
Effect.gen(function* () {
1275+
let storedCredentials: Ref.Ref<Map<string, ConnectionCredential>> | undefined;
1276+
const harness = yield* makeHarness([], [], [], {
1277+
// Authentication fails while the old bearer is stored, as the server
1278+
// would answer once that session was revoked.
1279+
prepareRoute: (target) => {
1280+
if (target._tag !== "BearerConnectionTarget" || storedCredentials === undefined) {
1281+
return undefined;
1282+
}
1283+
const current = Effect.runSync(Ref.get(storedCredentials)).get(target.connectionId);
1284+
return current?.token === "old-bearer"
1285+
? new ConnectionBlockedError({ reason: "authentication", detail: "revoked" })
1286+
: undefined;
1287+
},
1288+
});
1289+
storedCredentials = harness.storedCredentials;
1290+
1291+
yield* Effect.gen(function* () {
1292+
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
1293+
const registration = (token: string) =>
1294+
new BearerConnectionRegistration({
1295+
target: BEARER_TARGET,
1296+
profile: BEARER_PROFILE,
1297+
credential: new BearerConnectionCredential({ token }),
1298+
});
1299+
yield* registry.reconcilePlatform([registration("old-bearer")]);
1300+
yield* awaitConnectionState(
1301+
registry,
1302+
BEARER_TARGET.environmentId,
1303+
(state) => state.phase === "blocked",
1304+
);
1305+
1306+
yield* registry.reconcilePlatform([registration("new-bearer")]);
1307+
1308+
yield* awaitConnectionState(
1309+
registry,
1310+
BEARER_TARGET.environmentId,
1311+
(state) => state.phase === "connected",
1312+
);
1313+
}).pipe(Effect.provide(harness.layer), Effect.scoped);
1314+
}),
1315+
);
1316+
12731317
it.effect("starts a newly paired bearer environment without re-reading its profile", () =>
12741318
Effect.gen(function* () {
12751319
const harness = yield* makeHarness([]);

‎packages/client-runtime/src/connection/registry.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import * as SubscriptionRef from "effect/SubscriptionRef";
1515
import * as ClientCapabilities from "../platform/capabilities.ts";
1616
import {
1717
type ConnectionCatalogEntry,
18+
type ConnectionCredential,
1819
type ConnectionProfile,
1920
type ConnectionRegistration,
2021
type ConnectionRoute,
@@ -654,7 +655,13 @@ export const make = Effect.gen(function* () {
654655
// on their own loopback origin, so they authenticate with a bearer
655656
// token instead of the primary's same-origin cookie. Stash it where
656657
// the resolver's bearer broker looks it up.
658+
let bearerReplaced = false;
657659
if (registration._tag === "BearerConnectionRegistration") {
660+
const stored = yield* credentials
661+
.get(registration.target.connectionId)
662+
.pipe(Effect.orElseSucceed(() => Option.none<ConnectionCredential>()));
663+
bearerReplaced =
664+
Option.isSome(stored) && !Equal.equals(stored.value, registration.credential);
658665
yield* credentials.put(registration.target.connectionId, registration.credential).pipe(
659666
Effect.catch((error) =>
660667
Effect.logWarning("Could not store the platform bearer credential.", {
@@ -687,6 +694,24 @@ export const make = Effect.gen(function* () {
687694
}
688695

689696
yield* installEntryLocked(entry, { retainEquivalentRuntime: true });
697+
698+
// The catalog entry carries no credential, so a fresh bearer keeps
699+
// the equivalent runtime. A supervisor that stopped on the old
700+
// bearer's authentication failure would otherwise wait for a manual
701+
// retry, so wake it with the new one.
702+
if (bearerReplaced) {
703+
const scope = (yield* SubscriptionRef.get(serviceScopes)).get(target.environmentId);
704+
if (scope !== undefined) {
705+
const state = yield* SubscriptionRef.get(scope.supervisor.state);
706+
if (
707+
state.phase === "blocked" &&
708+
state.lastFailure?._tag === "ConnectionBlockedError" &&
709+
state.lastFailure.reason === "authentication"
710+
) {
711+
yield* scope.supervisor.retryNow;
712+
}
713+
}
714+
}
690715
}),
691716
);
692717
},

0 commit comments

Comments
 (0)