fix(client): recover when cached metadata is stale

This commit is contained in:
Theo Browne committed 2026-08-04 22:10:50 -07:00
1 parent d2cb3d016f
commit 34e6f78fe7
2 files changed
+71 -27

No files matched your search

@@ -245,8 +245,8 @@ describe("RemoteEnvironmentAuthorization", () => {
const harness = yield* makeHarness({
initialToken: cached,
responses: [
Response.json({ ...DESCRIPTOR, serverVersion: refreshedServerVersion }),
websocketTicket("cached-ticket"),
Response.json({ ...DESCRIPTOR, serverVersion: refreshedServerVersion }),
],
});
@@ -263,10 +263,10 @@ describe("RemoteEnvironmentAuthorization", () => {
expect(yield* Ref.get(harness.bootstrapCalls)).toBe(0);
expect(harness.fetch.calls).toHaveLength(2);
expect(String(harness.fetch.calls[0]?.[0])).toBe(
"https://environment.example.test/.well-known/t3/environment",
"https://environment.example.test/api/auth/websocket-ticket",
);
expect(String(harness.fetch.calls[1]?.[0])).toBe(
"https://environment.example.test/api/auth/websocket-ticket",
"https://environment.example.test/.well-known/t3/environment",
);
}),
);
@@ -324,7 +324,6 @@ describe("RemoteEnvironmentAuthorization", () => {
const harness = yield* makeHarness({
initialToken: cached,
responses: [
Response.json(DESCRIPTOR),
authInvalid(),
Response.json(DESCRIPTOR),
accessToken("replacement-access-token"),
@@ -347,7 +346,7 @@ describe("RemoteEnvironmentAuthorization", () => {
accessToken: "replacement-access-token",
}),
);
expect(harness.fetch.calls).toHaveLength(5);
expect(harness.fetch.calls).toHaveLength(4);
}),
);
@@ -364,7 +363,46 @@ describe("RemoteEnvironmentAuthorization", () => {
const harness = yield* makeHarness({
initialToken: cached,
responses: [
new Response("endpoint unavailable", { status: 503 }),
Response.json(DESCRIPTOR),
accessToken("replacement-access-token"),
websocketTicket("replacement-ticket"),
],
});
const authorized = yield* Effect.gen(function* () {
const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
return yield* remote.authorizeDpop({
expectedEnvironmentId: ENVIRONMENT_ID,
obtainBootstrap: harness.obtainBootstrap,
});
}).pipe(Effect.provide(harness.layer));
expect(authorized.socketUrl).toContain("wsTicket=replacement-ticket");
expect(yield* Ref.get(harness.bootstrapCalls)).toBe(1);
expect((yield* Ref.get(harness.tokens)).get(ENVIRONMENT_ID)).toEqual(
expect.objectContaining({
accessToken: "replacement-access-token",
}),
);
expect(harness.fetch.calls).toHaveLength(4);
}),
);
it.effect("refreshes a cached endpoint after descriptor discovery fails", () =>
Effect.gen(function* () {
const cached = new TokenStore.RemoteDpopAccessToken({
environmentId: ENVIRONMENT_ID,
label: DESCRIPTOR.label,
endpoint: ENDPOINT,
accessToken: "cached-access-token",
expiresAtEpochMs: Number.MAX_SAFE_INTEGER,
dpopThumbprint: "thumbprint-1",
});
const harness = yield* makeHarness({
initialToken: cached,
responses: [
websocketTicket("cached-ticket"),
new Response("endpoint unavailable", { status: 503 }),
Response.json(DESCRIPTOR),
accessToken("replacement-access-token"),
@@ -209,33 +209,39 @@ export const make = Effect.gen(function* () {
yield* Effect.annotateCurrentSpan({
"connection.remote_token_cache": "hit",
});
const descriptor = yield* fetchDescriptor(cached.value.endpoint.httpBaseUrl).pipe(
Effect.provideService(HttpClient.HttpClient, httpClient),
);
if (descriptor.environmentId !== input.expectedEnvironmentId) {
return yield* environmentMismatchError({
expected: input.expectedEnvironmentId,
actual: descriptor.environmentId,
});
}
const cachedSocket = yield* createDpopSocketUrl(
cached.value,
CACHED_ENDPOINT_SOCKET_TIMEOUT_MS,
).pipe(Effect.result);
if (Result.isSuccess(cachedSocket)) {
return {
environmentId: descriptor.environmentId,
label: descriptor.label,
serverVersion: descriptor.serverVersion,
httpBaseUrl: cached.value.endpoint.httpBaseUrl,
socketUrl: cachedSocket.success,
httpAuthorization: {
_tag: "Dpop" as const,
accessToken: cached.value.accessToken,
},
};
}
if (cachedSocket.failure._tag === "ConnectionBlockedError") {
const cachedDescriptor = yield* fetchDescriptor(cached.value.endpoint.httpBaseUrl).pipe(
Effect.provideService(HttpClient.HttpClient, httpClient),
Effect.result,
);
if (Result.isSuccess(cachedDescriptor)) {
const descriptor = cachedDescriptor.success;
if (descriptor.environmentId !== input.expectedEnvironmentId) {
return yield* environmentMismatchError({
expected: input.expectedEnvironmentId,
actual: descriptor.environmentId,
});
}
return {
environmentId: descriptor.environmentId,
label: descriptor.label,
serverVersion: descriptor.serverVersion,
httpBaseUrl: cached.value.endpoint.httpBaseUrl,
socketUrl: cachedSocket.success,
httpAuthorization: {
_tag: "Dpop" as const,
accessToken: cached.value.accessToken,
},
};
}
if (cachedDescriptor.failure._tag === "ConnectionBlockedError") {
return yield* cachedDescriptor.failure;
}
} else if (cachedSocket.failure._tag === "ConnectionBlockedError") {
return yield* mapDpopSocketError(cachedSocket.failure);
}
yield* tokenStore