From 853a95f24e380a768ff6a0c528c0e04b7d93144b Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Wed, 16 Sep 2026 09:48:38 -0400 Subject: [PATCH] fix(account): report a reader failure that lands after a scope change as superseded --- .../src/core/billing/capabilities.test.ts | 46 +++++++++++++++++++ .../account/src/core/billing/capabilities.ts | 20 ++++---- .../account/src/core/billing/credits.test.ts | 46 +++++++++++++++++++ packages/account/src/core/billing/credits.ts | 19 +++----- .../account/src/core/billing/status.test.ts | 46 +++++++++++++++++++ packages/account/src/core/billing/status.ts | 18 ++++---- 6 files changed, 161 insertions(+), 34 deletions(-) diff --git a/packages/account/src/core/billing/capabilities.test.ts b/packages/account/src/core/billing/capabilities.test.ts index 3569990776d..eae4070e578 100644 --- a/packages/account/src/core/billing/capabilities.test.ts +++ b/packages/account/src/core/billing/capabilities.test.ts @@ -136,6 +136,11 @@ function fakeTransport(answers: BillingResult[]) { return { transport, calls } } +interface SupersedeContext { + readonly host: ReturnType + readonly reader: ReturnType +} + describe('createCapabilitiesReader', () => { it('decodes the server answer and reports the resolved scope', async () => { const { scopeSource } = fakeSession() @@ -577,6 +582,47 @@ describe('createCapabilitiesReader', () => { expect(result).toEqual({ status: 'error', code: 'REQUEST_FAILED' }) }) + it.for([ + [ + 'the workspace changes', + ({ host }: SupersedeContext) => + host.moveTo( + authenticated( + credential({ + workspace: { id: 'ws-2', name: 'Team', type: 'team' } + }) + ) + ) + ], + [ + 'the reader is disposed', + ({ reader }: SupersedeContext) => reader.dispose() + ] + ] as const)( + 'reports SUPERSEDED when a failure lands after %s', + async ([, supersede]) => { + const host = fakeSession() + let release = () => {} + const gate = new Promise((resolve) => { + release = resolve + }) + const transport: BillingTransport = vi.fn(async () => { + await gate + return httpStatus(500) + }) + const reader = createCapabilitiesReader({ + transport, + scopeSource: host.scopeSource + }) + + const pending = reader.read() + supersede({ host, reader }) + release() + + expect(await pending).toEqual({ status: 'error', code: 'SUPERSEDED' }) + } + ) + it('drops a prior snapshot when the server denies the current actor', async () => { const { scopeSource } = fakeSession() const { transport } = fakeTransport([ diff --git a/packages/account/src/core/billing/capabilities.ts b/packages/account/src/core/billing/capabilities.ts index 5712da0edeb..b6c155b2736 100644 --- a/packages/account/src/core/billing/capabilities.ts +++ b/packages/account/src/core/billing/capabilities.ts @@ -264,23 +264,19 @@ export function createCapabilitiesReader( // waiting on it. Each caller's signal releases only that caller, below. const promise = (async (): Promise> => { const result = await requestCapabilities(scope) - if (result.status !== 'ok') { - if ( - result.code === 'ACCESS_DENIED' && - scopeTracker.isCurrent(context) - ) { - snapshot = undefined - } - return result - } - // The publish guard. Between issuing the request and settling it the - // host may have changed workspace or signed out, and a snapshot cached - // now would be attributed to whoever is signed in next. + // host may have changed workspace or signed out, so a snapshot cached + // now would be attributed to whoever is signed in next, and a failure + // says nothing about the scope that has since taken over. if (lifetime.disposed || !scopeTracker.isCurrent(context)) { return { status: 'error', code: 'SUPERSEDED' } } + if (result.status !== 'ok') { + if (result.code === 'ACCESS_DENIED') snapshot = undefined + return result + } + // A mutation committed while this read was in flight, and the revision // the read returns cannot rule out having missed it: the server mints // that number when it serializes, not when it read. The value is still diff --git a/packages/account/src/core/billing/credits.test.ts b/packages/account/src/core/billing/credits.test.ts index 331165cb715..f0640f39434 100644 --- a/packages/account/src/core/billing/credits.test.ts +++ b/packages/account/src/core/billing/credits.test.ts @@ -97,6 +97,11 @@ function fakeTransport(answers: BillingResult[]) { return { transport, calls } } +interface SupersedeContext { + readonly host: ReturnType + readonly reader: ReturnType +} + describe('createCreditsReader', () => { it('reads the balance from the billing balance route', async () => { const { scopeSource } = fakeSession() @@ -395,6 +400,47 @@ describe('createCreditsReader', () => { expect(reader.getSnapshot()?.balance.amount_micros).toBe(12_500_000) }) + it.for([ + [ + 'the workspace changes', + ({ host }: SupersedeContext) => + host.moveTo( + authenticated( + credential({ + workspace: { id: 'ws-2', name: 'Team', type: 'team' } + }) + ) + ) + ], + [ + 'the reader is disposed', + ({ reader }: SupersedeContext) => reader.dispose() + ] + ] as const)( + 'reports SUPERSEDED when a failure lands after %s', + async ([, supersede]) => { + const host = fakeSession() + let release = () => {} + const gate = new Promise((resolve) => { + release = resolve + }) + const transport: BillingTransport = vi.fn(async () => { + await gate + return httpStatus(500) + }) + const reader = createCreditsReader({ + transport, + scopeSource: host.scopeSource + }) + + const pending = reader.read() + supersede({ host, reader }) + release() + + expect(await pending).toEqual({ status: 'error', code: 'SUPERSEDED' }) + } + ) + it('drops the last good balance when a later read is denied', async () => { const { scopeSource } = fakeSession() const { transport } = fakeTransport([httpOk(BALANCE), httpStatus(401)]) diff --git a/packages/account/src/core/billing/credits.ts b/packages/account/src/core/billing/credits.ts index 01b16e24df5..3994a00b01c 100644 --- a/packages/account/src/core/billing/credits.ts +++ b/packages/account/src/core/billing/credits.ts @@ -117,23 +117,18 @@ export function createCreditsReader( context, promise: (async () => { const result = await requestBalance(scope) - if (result.status !== 'ok') { - if ( - result.code === 'ACCESS_DENIED' && - scopeTracker.isCurrent(context) - ) { - snapshot = undefined - } - return result - } - // The publish guard: a balance that arrives after the host moved to - // another workspace or signed out belongs to neither, and showing it - // would state one account's credits under another's name. + // another workspace or signed out belongs to neither, and a failure + // says nothing about the scope that has since taken over. if (lifetime.disposed || !scopeTracker.isCurrent(context)) { return { status: 'error', code: 'SUPERSEDED' } } + if (result.status !== 'ok') { + if (result.code === 'ACCESS_DENIED') snapshot = undefined + return result + } + snapshot = result.value return result })() diff --git a/packages/account/src/core/billing/status.test.ts b/packages/account/src/core/billing/status.test.ts index 9961f8ab2f5..f86b40b4f56 100644 --- a/packages/account/src/core/billing/status.test.ts +++ b/packages/account/src/core/billing/status.test.ts @@ -100,6 +100,11 @@ function fakeTransport(answers: BillingResult[]) { return { transport, calls } } +interface SupersedeContext { + readonly host: ReturnType + readonly reader: ReturnType +} + describe('createBillingStatusReader', () => { it('reads and validates the authoritative billing status', async () => { const { scopeSource } = fakeSession() @@ -271,6 +276,47 @@ describe('createBillingStatusReader', () => { expect(reader.getSnapshot()).toBeUndefined() }) + it.for([ + [ + 'the workspace changes', + ({ host }: SupersedeContext) => + host.moveTo( + authenticated( + credential({ + workspace: { id: 'ws-2', name: 'Team', type: 'team' } + }) + ) + ) + ], + [ + 'the reader is disposed', + ({ reader }: SupersedeContext) => reader.dispose() + ] + ] as const)( + 'reports SUPERSEDED when a failure lands after %s', + async ([, supersede]) => { + const host = fakeSession() + let release = () => {} + const gate = new Promise((resolve) => { + release = resolve + }) + const transport: BillingTransport = vi.fn(async () => { + await gate + return httpStatus(500) + }) + const reader = createBillingStatusReader({ + transport, + scopeSource: host.scopeSource + }) + + const pending = reader.read() + supersede({ host, reader }) + release() + + expect(await pending).toEqual({ status: 'error', code: 'SUPERSEDED' }) + } + ) + it('normalizes a throwing host transport to a coded failure', async () => { const { scopeSource } = fakeSession() const transport: BillingTransport = vi.fn(() => { diff --git a/packages/account/src/core/billing/status.ts b/packages/account/src/core/billing/status.ts index 177cf301963..ba11910aa51 100644 --- a/packages/account/src/core/billing/status.ts +++ b/packages/account/src/core/billing/status.ts @@ -98,20 +98,18 @@ export function createBillingStatusReader( context, promise: (async () => { const result = await requestStatus(scope) - if (result.status !== 'ok') { - if ( - result.code === 'ACCESS_DENIED' && - scopeTracker.isCurrent(context) - ) { - snapshot = undefined - } - return result - } - + // A status that arrives after the host moved to another workspace + // describes neither, and a failure says nothing about the scope that + // has since taken over. if (lifetime.disposed || !scopeTracker.isCurrent(context)) { return { status: 'error', code: 'SUPERSEDED' } } + if (result.status !== 'ok') { + if (result.code === 'ACCESS_DENIED') snapshot = undefined + return result + } + snapshot = result.value return result })()