Skip to content

Commit db730f7

Browse files
fix(web): a new desktop bootstrap token does not drop a live desktop-local session
Review follow-up. Cached registrations for desktop-local backends were keyed by endpoint plus bootstrap token, so a changed token discarded a bearer that was still valid. If the exchange with the new token then failed, the environment disappeared from the registry, which also clears its composer drafts. The cache is now keyed by endpoint; the token only decides whether a rejected exchange is retried. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 72bdbc9 commit db730f7

2 files changed

Lines changed: 38 additions & 6 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,25 @@ describe("rejected desktop-local bootstrap tokens", () => {
253253
});
254254
});
255255

256+
describe("desktop-local bearer cache across token changes", () => {
257+
it("keeps a live bearer when only the bootstrap token rotates", () => {
258+
// Cached by endpoint: a rotated token reuses the bearer instead of
259+
// dropping the environment (and its drafts) when the new token is rejected.
260+
const cached = {
261+
signature: "http://a|ws://a",
262+
registration: {} as never,
263+
expiresAtEpochMs: 20_000,
264+
refreshAtEpochMs: 15_000,
265+
};
266+
267+
expect(canReuseCachedPlatformRegistration(cached, "http://a|ws://a", 10_000)).toBe(true);
268+
expect(
269+
canRetainCachedPlatformRegistrationAfterRefreshFailure(cached, "http://a|ws://a", 16_000),
270+
).toBe(true);
271+
expect(canReuseCachedPlatformRegistration(cached, "http://b|ws://b", 10_000)).toBe(false);
272+
});
273+
});
274+
256275
describe("primary topology cache", () => {
257276
const registration = {} as never;
258277
const cached = {

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ const loadSecondaryConnectionRegistration = Effect.fn(
418418

419419
// Poll cadence for the desktop bootstrap topology. There is no change event on
420420
// the bridge, so the renderer polls; successful registrations are cached by a
421-
// signature of their endpoint + token until bearer credentials approach expiry.
421+
// signature of their endpoint until bearer credentials approach expiry.
422422
const PLATFORM_POLL_INTERVAL = "3 seconds";
423423
const SECONDARY_BEARER_REFRESH_SKEW_MS = 5_000;
424424

@@ -636,11 +636,16 @@ const platformConnectionSourceLayer = Layer.effect(
636636
const rejected = yield* Ref.get(rejectedRef);
637637
const nextRejected = new Map<string, RejectedSecondaryBootstrap>();
638638
for (const bootstrap of topologyRead.bootstraps) {
639-
const signature = `${bootstrap.httpBaseUrl}|${bootstrap.wsBaseUrl}|${bootstrap.bootstrapToken ?? ""}`;
639+
// The cached bearer belongs to the endpoint, not to the bootstrap
640+
// token it was exchanged for: a new token must not drop a live
641+
// session (its removal also clears the environment's drafts). The
642+
// token only decides whether a rejected exchange is retried.
643+
const endpointSignature = `${bootstrap.httpBaseUrl}|${bootstrap.wsBaseUrl}`;
644+
const signature = `${endpointSignature}|${bootstrap.bootstrapToken ?? ""}`;
640645
const cached = previous.get(bootstrap.id);
641646
if (
642647
cached !== undefined &&
643-
canReuseCachedPlatformRegistration(cached, signature, nowEpochMs)
648+
canReuseCachedPlatformRegistration(cached, endpointSignature, nowEpochMs)
644649
) {
645650
next.set(bootstrap.id, cached);
646651
registrations.push(cached.registration);
@@ -652,7 +657,11 @@ const platformConnectionSourceLayer = Layer.effect(
652657
// The bearer minted before the token died is still good until it expires.
653658
if (
654659
cached !== undefined &&
655-
canRetainCachedPlatformRegistrationAfterRefreshFailure(cached, signature, nowEpochMs)
660+
canRetainCachedPlatformRegistrationAfterRefreshFailure(
661+
cached,
662+
endpointSignature,
663+
nowEpochMs,
664+
)
656665
) {
657666
next.set(bootstrap.id, cached);
658667
registrations.push(cached.registration);
@@ -679,12 +688,16 @@ const platformConnectionSourceLayer = Layer.effect(
679688
Effect.option,
680689
);
681690
if (Option.isSome(built)) {
682-
const cacheEntry = { signature, ...built.value };
691+
const cacheEntry = { signature: endpointSignature, ...built.value };
683692
next.set(bootstrap.id, cacheEntry);
684693
registrations.push(built.value.registration);
685694
} else if (
686695
cached !== undefined &&
687-
canRetainCachedPlatformRegistrationAfterRefreshFailure(cached, signature, nowEpochMs)
696+
canRetainCachedPlatformRegistrationAfterRefreshFailure(
697+
cached,
698+
endpointSignature,
699+
nowEpochMs,
700+
)
688701
) {
689702
next.set(bootstrap.id, cached);
690703
registrations.push(cached.registration);

0 commit comments

Comments
 (0)