From a2c6a7a4a6117f4d60ae39ea8a1cc2b77ae40ff2 Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 29 Oct 2026 02:29:57 +0000 Subject: [PATCH 1/6] FRONT-3684 fix(billing): report a failed background balance read instead of leaving it unhandled The account menus and the Credits settings section start a balance read that nothing awaits. When it fails, the rejection is unhandled and Sentry files it as a new high-priority error (both events of CLOUD-FRONTEND-PROD-2V0 are onunhandledrejection after a 503 from /customers/balance). The billing context already keeps the error for the balance UI, so report it once as a billing warning instead. --- .../topbar/CurrentUserPopoverLegacy.test.ts | 17 ++++++++++ .../topbar/CurrentUserPopoverLegacy.vue | 3 +- .../CurrentUserPopoverWorkspace.test.ts | 17 ++++++++++ .../CurrentUserPopoverWorkspace.vue | 3 +- .../refreshBalanceInBackground.test.ts | 31 +++++++++++++++++ .../billing/refreshBalanceInBackground.ts | 18 ++++++++++ .../settings/components/SettingDialog.test.ts | 34 +++++++++++++++++++ .../settings/components/SettingDialog.vue | 3 +- 8 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 src/platform/billing/refreshBalanceInBackground.test.ts create mode 100644 src/platform/billing/refreshBalanceInBackground.ts diff --git a/src/components/topbar/CurrentUserPopoverLegacy.test.ts b/src/components/topbar/CurrentUserPopoverLegacy.test.ts index 6e74b09e6c9..cec98f42530 100644 --- a/src/components/topbar/CurrentUserPopoverLegacy.test.ts +++ b/src/components/topbar/CurrentUserPopoverLegacy.test.ts @@ -1,6 +1,7 @@ import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities' import { useBillingDialogs } from '@/platform/billing/composables/useBillingDialogs' import { useBillingContext } from '@/platform/billing/composables/useBillingContext' +import { reportError } from '@comfyorg/telemetry/reportError' import { getActivePinia } from 'pinia' import { render, screen } from '@testing-library/vue' import userEvent from '@testing-library/user-event' @@ -140,6 +141,22 @@ describe('CurrentUserPopoverLegacy', () => { expect(useBillingContext().fetchBalance).toHaveBeenCalled() }) + it('reports a failed balance read once instead of leaving it unhandled', async () => { + const failure = new Error('Balance temporarily unavailable') + vi.mocked(useBillingContext().fetchBalance).mockRejectedValueOnce(failure) + + renderComponent() + + await vi.waitFor(() => + expect(reportError).toHaveBeenCalledWith(failure, { + surface: 'billing', + errorType: 'error_refreshing_balance', + level: 'warning' + }) + ) + expect(reportError).toHaveBeenCalledOnce() + }) + describe('subscription tier badge', () => { it('renders the tier name derived from the facade tier', () => { renderComponent() diff --git a/src/components/topbar/CurrentUserPopoverLegacy.vue b/src/components/topbar/CurrentUserPopoverLegacy.vue index 68493ba5134..b287f9d7fd3 100644 --- a/src/components/topbar/CurrentUserPopoverLegacy.vue +++ b/src/components/topbar/CurrentUserPopoverLegacy.vue @@ -163,6 +163,7 @@ import PopoverContent from '@comfyorg/design-system/popover/PopoverContent' import Skeleton from '@comfyorg/design-system/skeleton/Skeleton' import { useCurrentUser } from '@/composables/auth/useCurrentUser' import { useBillingContext } from '@/platform/billing/composables/useBillingContext' +import { refreshBalanceInBackground } from '@/platform/billing/refreshBalanceInBackground' import { useExternalLink } from '@/composables/useExternalLink' import { useTelemetry } from '@comfyorg/telemetry' import { paymentIntentSourceForAddCreditsClick } from '@/platform/telemetry/utils/paymentIntentSource' @@ -253,6 +254,6 @@ const handleLogout = async () => { } onMounted(() => { - void fetchBalance() + refreshBalanceInBackground(fetchBalance) }) diff --git a/src/platform/account/components/CurrentUserPopoverWorkspace.test.ts b/src/platform/account/components/CurrentUserPopoverWorkspace.test.ts index 7fd18fc4830..e780a540340 100644 --- a/src/platform/account/components/CurrentUserPopoverWorkspace.test.ts +++ b/src/platform/account/components/CurrentUserPopoverWorkspace.test.ts @@ -15,6 +15,7 @@ import { setImmediate } from 'node:timers/promises' import { useCurrentUser } from '@/composables/auth/useCurrentUser' import { useBillingContext } from '@/platform/billing/composables/useBillingContext' +import { reportError } from '@comfyorg/telemetry/reportError' import { useBillingRouting } from '@/platform/billing/composables/useBillingRouting' import type { BalanceInfo, @@ -268,6 +269,22 @@ describe('CurrentUserPopoverWorkspace', () => { expect(screen.queryByRole('tooltip')).not.toBeInTheDocument() }) + it('reports a failed balance read once instead of leaving it unhandled', async () => { + const failure = new Error('Balance temporarily unavailable') + vi.mocked(useBillingContext().fetchBalance).mockRejectedValueOnce(failure) + + renderComponent() + + await vi.waitFor(() => + expect(reportError).toHaveBeenCalledWith(failure, { + surface: 'billing', + errorType: 'error_refreshing_balance', + level: 'warning' + }) + ) + expect(reportError).toHaveBeenCalledOnce() + }) + it('keeps account actions available without workspace context', () => { renderComponent('personal', true) diff --git a/src/platform/account/components/CurrentUserPopoverWorkspace.vue b/src/platform/account/components/CurrentUserPopoverWorkspace.vue index 4a75758ae96..c165a5da87b 100644 --- a/src/platform/account/components/CurrentUserPopoverWorkspace.vue +++ b/src/platform/account/components/CurrentUserPopoverWorkspace.vue @@ -268,6 +268,7 @@ import { import { useExternalLink } from '@/composables/useExternalLink' import { useBillingContext } from '@/platform/billing/composables/useBillingContext' +import { refreshBalanceInBackground } from '@/platform/billing/refreshBalanceInBackground' import SubscribeButton from '@/platform/billing/subscription/components/SubscribeButton.vue' import { useSubscriptionDialog } from '@/platform/billing/subscription/composables/useSubscriptionDialog' import { isCloud } from '@/platform/distribution/types' @@ -450,6 +451,6 @@ const handleCreateWorkspace = () => { } onMounted(() => { - if (!accountActionsOnly) void fetchBalance() + if (!accountActionsOnly) refreshBalanceInBackground(fetchBalance) }) diff --git a/src/platform/billing/refreshBalanceInBackground.test.ts b/src/platform/billing/refreshBalanceInBackground.test.ts new file mode 100644 index 00000000000..c7b07fd6919 --- /dev/null +++ b/src/platform/billing/refreshBalanceInBackground.test.ts @@ -0,0 +1,31 @@ +import { reportError } from '@comfyorg/telemetry/reportError' +import { describe, expect, it, vi } from 'vitest' + +import { refreshBalanceInBackground } from '@/platform/billing/refreshBalanceInBackground' + +describe('refreshBalanceInBackground', () => { + it('reports nothing when the read succeeds', async () => { + const fetchBalance = vi.fn().mockResolvedValue(undefined) + + refreshBalanceInBackground(fetchBalance) + await vi.waitFor(() => expect(fetchBalance).toHaveBeenCalledOnce()) + await Promise.resolve() + + expect(reportError).not.toHaveBeenCalled() + }) + + it('reports a failed read once as a billing warning', async () => { + const failure = new Error('Balance temporarily unavailable') + + refreshBalanceInBackground(() => Promise.reject(failure)) + + await vi.waitFor(() => + expect(reportError).toHaveBeenCalledWith(failure, { + surface: 'billing', + errorType: 'error_refreshing_balance', + level: 'warning' + }) + ) + expect(reportError).toHaveBeenCalledOnce() + }) +}) diff --git a/src/platform/billing/refreshBalanceInBackground.ts b/src/platform/billing/refreshBalanceInBackground.ts new file mode 100644 index 00000000000..7cb3b7bdd41 --- /dev/null +++ b/src/platform/billing/refreshBalanceInBackground.ts @@ -0,0 +1,18 @@ +import { reportError } from '@comfyorg/telemetry/reportError' + +/** + * Starts a balance read that nothing awaits. The billing context keeps the + * failure for the balance UI to show; this reports it once instead of letting + * it escape as an unhandled rejection. + */ +export function refreshBalanceInBackground( + fetchBalance: () => Promise +): void { + void fetchBalance().catch((error: unknown) => { + reportError(error, { + surface: 'billing', + errorType: 'error_refreshing_balance', + level: 'warning' + }) + }) +} diff --git a/src/platform/settings/components/SettingDialog.test.ts b/src/platform/settings/components/SettingDialog.test.ts index b5e8eecd0c5..e2ba0cc3795 100644 --- a/src/platform/settings/components/SettingDialog.test.ts +++ b/src/platform/settings/components/SettingDialog.test.ts @@ -5,6 +5,8 @@ import { nextTick, ref } from 'vue' import type { Ref } from 'vue' import { i18n } from '@/i18n' +import { useBillingContext } from '@/platform/billing/composables/useBillingContext' +import { reportError } from '@comfyorg/telemetry/reportError' import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore' import type { NavGroupData } from '@comfyorg/design-system/lib/navigation/types' @@ -297,3 +299,35 @@ it('restores the collapsed header when switching sections', async () => { await screen.findByRole('heading', { name: 'Acme Team' }) ).toBeInTheDocument() }) + +it('reports a failed balance read from the Credits section once instead of leaving it unhandled', async () => { + const failure = new Error('Balance temporarily unavailable') + const billing = useBillingContext() + vi.mocked(billing.fetchBalance).mockRejectedValueOnce(failure) + vi.mocked(useBillingContext).mockReturnValue(billing) + settingUiMocks.navGroups.value = [ + { + title: 'Workspace', + items: [ + { + id: 'workspace-allowlist', + label: 'Allowlist', + icon: 'icon-[lucide--list-checks]' + }, + { id: 'credits', label: 'Credits', icon: 'icon-[lucide--coins]' } + ] + } + ] + renderDialog() + + await userEvent.click(screen.getByRole('button', { name: 'Credits' })) + + await vi.waitFor(() => + expect(reportError).toHaveBeenCalledWith(failure, { + surface: 'billing', + errorType: 'error_refreshing_balance', + level: 'warning' + }) + ) + expect(reportError).toHaveBeenCalledOnce() +}) diff --git a/src/platform/settings/components/SettingDialog.vue b/src/platform/settings/components/SettingDialog.vue index 89f46a8e4ae..ebc9ba5a9dc 100644 --- a/src/platform/settings/components/SettingDialog.vue +++ b/src/platform/settings/components/SettingDialog.vue @@ -126,6 +126,7 @@ import BaseModalLayout from '@comfyorg/design-system/layout/BaseModalLayout' import NavItem from '@comfyorg/design-system/navigation/NavItem' import NavTitle from '@comfyorg/design-system/navigation/NavTitle' import { useBillingContext } from '@/platform/billing/composables/useBillingContext' +import { refreshBalanceInBackground } from '@/platform/billing/refreshBalanceInBackground' import { useWorkspaceBillingStateStore } from '@/platform/billing/stores/workspaceBillingStateStore' import { useSettingsHeaderCollapse } from '@/platform/settings/composables/useSettingsHeaderCollapse' import { cn } from '@comfyorg/tailwind-utils' @@ -307,7 +308,7 @@ watch(activeCategoryKey, (newKey, oldKey) => { activeCategoryKey.value = oldKey } if (newKey === 'credits') { - void fetchBalance() + refreshBalanceInBackground(fetchBalance) } if (newKey) { void nextTick(() => { From 72ff01cefa7e5d7dd036a9113faedcd64bcb49f9 Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 29 Oct 2026 03:09:13 +0000 Subject: [PATCH 2/6] fix(billing): report a run of background balance failures once During a balance outage every account-menu open or Credits visit sent another warning. Report the first failure of a run and stay quiet until a read succeeds again. --- .../refreshBalanceInBackground.test.ts | 46 +++++++++++++------ .../billing/refreshBalanceInBackground.ts | 29 ++++++++---- 2 files changed, 52 insertions(+), 23 deletions(-) diff --git a/src/platform/billing/refreshBalanceInBackground.test.ts b/src/platform/billing/refreshBalanceInBackground.test.ts index c7b07fd6919..21cfe8dab1c 100644 --- a/src/platform/billing/refreshBalanceInBackground.test.ts +++ b/src/platform/billing/refreshBalanceInBackground.test.ts @@ -1,31 +1,49 @@ import { reportError } from '@comfyorg/telemetry/reportError' -import { describe, expect, it, vi } from 'vitest' +import { beforeEach, describe, expect, it } from 'vitest' import { refreshBalanceInBackground } from '@/platform/billing/refreshBalanceInBackground' +const fail = () => Promise.reject(new Error('Balance temporarily unavailable')) +const settle = () => new Promise((resolve) => setTimeout(resolve)) + describe('refreshBalanceInBackground', () => { - it('reports nothing when the read succeeds', async () => { - const fetchBalance = vi.fn().mockResolvedValue(undefined) + beforeEach(async () => { + // A success ends any run of failures an earlier test left behind. + refreshBalanceInBackground(() => Promise.resolve()) + await settle() + }) - refreshBalanceInBackground(fetchBalance) - await vi.waitFor(() => expect(fetchBalance).toHaveBeenCalledOnce()) - await Promise.resolve() + it('reports nothing when the read succeeds', async () => { + refreshBalanceInBackground(() => Promise.resolve()) + await settle() expect(reportError).not.toHaveBeenCalled() }) - it('reports a failed read once as a billing warning', async () => { + it('reports a failed read as a billing warning', async () => { const failure = new Error('Balance temporarily unavailable') refreshBalanceInBackground(() => Promise.reject(failure)) + await settle() - await vi.waitFor(() => - expect(reportError).toHaveBeenCalledWith(failure, { - surface: 'billing', - errorType: 'error_refreshing_balance', - level: 'warning' - }) - ) + expect(reportError).toHaveBeenCalledExactlyOnceWith(failure, { + surface: 'billing', + errorType: 'error_refreshing_balance', + level: 'warning' + }) + }) + + it('reports a run of failures once, and again after a success', async () => { + refreshBalanceInBackground(fail) + refreshBalanceInBackground(fail) + await settle() expect(reportError).toHaveBeenCalledOnce() + + refreshBalanceInBackground(() => Promise.resolve()) + await settle() + refreshBalanceInBackground(fail) + await settle() + + expect(reportError).toHaveBeenCalledTimes(2) }) }) diff --git a/src/platform/billing/refreshBalanceInBackground.ts b/src/platform/billing/refreshBalanceInBackground.ts index 7cb3b7bdd41..923a4162926 100644 --- a/src/platform/billing/refreshBalanceInBackground.ts +++ b/src/platform/billing/refreshBalanceInBackground.ts @@ -1,18 +1,29 @@ import { reportError } from '@comfyorg/telemetry/reportError' +/** Whether the current run of failures has been reported; a success clears it. */ +let failureReported = false + /** * Starts a balance read that nothing awaits. The billing context keeps the - * failure for the balance UI to show; this reports it once instead of letting - * it escape as an unhandled rejection. + * failure for the balance UI to show; this reports it as a warning instead of + * letting it escape as an unhandled rejection, once per run of failures so a + * user reopening the account menu during an outage does not repeat it. */ export function refreshBalanceInBackground( fetchBalance: () => Promise ): void { - void fetchBalance().catch((error: unknown) => { - reportError(error, { - surface: 'billing', - errorType: 'error_refreshing_balance', - level: 'warning' - }) - }) + void fetchBalance().then( + () => { + failureReported = false + }, + (error: unknown) => { + if (failureReported) return + failureReported = true + reportError(error, { + surface: 'billing', + errorType: 'error_refreshing_balance', + level: 'warning' + }) + } + ) } From 76ad01f3604afea72dd51b4444df692cc8e6e073 Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 29 Oct 2026 03:28:34 +0000 Subject: [PATCH 3/6] fix(billing): settle a superseded workspace balance read instead of rejecting A newer read owns the balance, so an older read's failure is not a failure: it already skipped error.value, but still rejected, so a background refresh racing the popover, the Credits section and initialize could report a failure the billing state had thrown away. Match the rail path, which treats SUPERSEDED as no result. Also cut the helper's JSDoc to one contract sentence per the repo comment rule. --- .../composables/useWorkspaceBilling.test.ts | 16 ++++++++++++++++ .../billing/composables/useWorkspaceBilling.ts | 8 ++++---- .../billing/refreshBalanceInBackground.ts | 7 +------ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/platform/billing/composables/useWorkspaceBilling.test.ts b/src/platform/billing/composables/useWorkspaceBilling.test.ts index d908b11062a..065501e4752 100644 --- a/src/platform/billing/composables/useWorkspaceBilling.test.ts +++ b/src/platform/billing/composables/useWorkspaceBilling.test.ts @@ -796,6 +796,22 @@ describe('useWorkspaceBilling', () => { expect(billing.balance.value?.amountMicros).toBe(5_000_000) }) + + it('settles an older failed balance read quietly once a newer read replaced it', async () => { + const olderBalance = createDeferred() + mockWorkspaceApi.getBillingBalance + .mockReturnValueOnce(olderBalance.promise) + .mockResolvedValueOnce(positiveBalance) + const billing = setupBilling() + + const olderRequest = billing.fetchBalance() + await billing.fetchBalance() + olderBalance.reject(new Error('stale balance failed')) + + await expect(olderRequest).resolves.toBeUndefined() + expect(billing.error.value).toBeNull() + expect(billing.balance.value?.amountMicros).toBe(5_000_000) + }) }) describe('subscribe', () => { diff --git a/src/platform/billing/composables/useWorkspaceBilling.ts b/src/platform/billing/composables/useWorkspaceBilling.ts index 717f29e69c2..bb2793549b0 100644 --- a/src/platform/billing/composables/useWorkspaceBilling.ts +++ b/src/platform/billing/composables/useWorkspaceBilling.ts @@ -417,10 +417,10 @@ export function useWorkspaceBilling(): WorkspaceBilling { balanceData.value = balance } } catch (err) { - if (requestId === latestBillingReadIds.balance) { - error.value = - err instanceof Error ? err.message : 'Failed to fetch balance' - } + // A newer read owns the balance now, so this failure is not one. + if (requestId !== latestBillingReadIds.balance) return + error.value = + err instanceof Error ? err.message : 'Failed to fetch balance' throw err } finally { if (requestId === latestBillingReadIds.balance) isLoading.value = false diff --git a/src/platform/billing/refreshBalanceInBackground.ts b/src/platform/billing/refreshBalanceInBackground.ts index 923a4162926..11ccc40bc85 100644 --- a/src/platform/billing/refreshBalanceInBackground.ts +++ b/src/platform/billing/refreshBalanceInBackground.ts @@ -3,12 +3,7 @@ import { reportError } from '@comfyorg/telemetry/reportError' /** Whether the current run of failures has been reported; a success clears it. */ let failureReported = false -/** - * Starts a balance read that nothing awaits. The billing context keeps the - * failure for the balance UI to show; this reports it as a warning instead of - * letting it escape as an unhandled rejection, once per run of failures so a - * user reopening the account menu during an outage does not repeat it. - */ +/** Starts a balance read that nothing awaits; reports a run of failures once. */ export function refreshBalanceInBackground( fetchBalance: () => Promise ): void { From e2f55309e217e17f3a0456817a1d53f15e31dffa Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 29 Oct 2026 03:36:20 +0000 Subject: [PATCH 4/6] refactor(billing): let tests await the background balance read Return the never-rejecting promise so tests wait on the read itself rather than a timer, use function declarations for the test helpers, and drop the comments the repo rule does not allow. --- .../topbar/CurrentUserPopoverLegacy.vue | 2 +- .../CurrentUserPopoverWorkspace.vue | 2 +- .../composables/useWorkspaceBilling.ts | 1 - .../refreshBalanceInBackground.test.ts | 32 +++++++++---------- .../billing/refreshBalanceInBackground.ts | 7 ++-- .../settings/components/SettingDialog.vue | 2 +- 6 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/components/topbar/CurrentUserPopoverLegacy.vue b/src/components/topbar/CurrentUserPopoverLegacy.vue index b287f9d7fd3..7788888c48d 100644 --- a/src/components/topbar/CurrentUserPopoverLegacy.vue +++ b/src/components/topbar/CurrentUserPopoverLegacy.vue @@ -254,6 +254,6 @@ const handleLogout = async () => { } onMounted(() => { - refreshBalanceInBackground(fetchBalance) + void refreshBalanceInBackground(fetchBalance) }) diff --git a/src/platform/account/components/CurrentUserPopoverWorkspace.vue b/src/platform/account/components/CurrentUserPopoverWorkspace.vue index c165a5da87b..6695b9d717d 100644 --- a/src/platform/account/components/CurrentUserPopoverWorkspace.vue +++ b/src/platform/account/components/CurrentUserPopoverWorkspace.vue @@ -451,6 +451,6 @@ const handleCreateWorkspace = () => { } onMounted(() => { - if (!accountActionsOnly) refreshBalanceInBackground(fetchBalance) + if (!accountActionsOnly) void refreshBalanceInBackground(fetchBalance) }) diff --git a/src/platform/billing/composables/useWorkspaceBilling.ts b/src/platform/billing/composables/useWorkspaceBilling.ts index bb2793549b0..397640d9311 100644 --- a/src/platform/billing/composables/useWorkspaceBilling.ts +++ b/src/platform/billing/composables/useWorkspaceBilling.ts @@ -417,7 +417,6 @@ export function useWorkspaceBilling(): WorkspaceBilling { balanceData.value = balance } } catch (err) { - // A newer read owns the balance now, so this failure is not one. if (requestId !== latestBillingReadIds.balance) return error.value = err instanceof Error ? err.message : 'Failed to fetch balance' diff --git a/src/platform/billing/refreshBalanceInBackground.test.ts b/src/platform/billing/refreshBalanceInBackground.test.ts index 21cfe8dab1c..8c6a6528044 100644 --- a/src/platform/billing/refreshBalanceInBackground.test.ts +++ b/src/platform/billing/refreshBalanceInBackground.test.ts @@ -3,19 +3,21 @@ import { beforeEach, describe, expect, it } from 'vitest' import { refreshBalanceInBackground } from '@/platform/billing/refreshBalanceInBackground' -const fail = () => Promise.reject(new Error('Balance temporarily unavailable')) -const settle = () => new Promise((resolve) => setTimeout(resolve)) +function fail() { + return Promise.reject(new Error('Balance temporarily unavailable')) +} + +function succeed() { + return Promise.resolve() +} describe('refreshBalanceInBackground', () => { beforeEach(async () => { - // A success ends any run of failures an earlier test left behind. - refreshBalanceInBackground(() => Promise.resolve()) - await settle() + await refreshBalanceInBackground(succeed) }) it('reports nothing when the read succeeds', async () => { - refreshBalanceInBackground(() => Promise.resolve()) - await settle() + await refreshBalanceInBackground(succeed) expect(reportError).not.toHaveBeenCalled() }) @@ -23,8 +25,7 @@ describe('refreshBalanceInBackground', () => { it('reports a failed read as a billing warning', async () => { const failure = new Error('Balance temporarily unavailable') - refreshBalanceInBackground(() => Promise.reject(failure)) - await settle() + await refreshBalanceInBackground(() => Promise.reject(failure)) expect(reportError).toHaveBeenCalledExactlyOnceWith(failure, { surface: 'billing', @@ -34,15 +35,14 @@ describe('refreshBalanceInBackground', () => { }) it('reports a run of failures once, and again after a success', async () => { - refreshBalanceInBackground(fail) - refreshBalanceInBackground(fail) - await settle() + await Promise.all([ + refreshBalanceInBackground(fail), + refreshBalanceInBackground(fail) + ]) expect(reportError).toHaveBeenCalledOnce() - refreshBalanceInBackground(() => Promise.resolve()) - await settle() - refreshBalanceInBackground(fail) - await settle() + await refreshBalanceInBackground(succeed) + await refreshBalanceInBackground(fail) expect(reportError).toHaveBeenCalledTimes(2) }) diff --git a/src/platform/billing/refreshBalanceInBackground.ts b/src/platform/billing/refreshBalanceInBackground.ts index 11ccc40bc85..2e4d6abf557 100644 --- a/src/platform/billing/refreshBalanceInBackground.ts +++ b/src/platform/billing/refreshBalanceInBackground.ts @@ -1,13 +1,12 @@ import { reportError } from '@comfyorg/telemetry/reportError' -/** Whether the current run of failures has been reported; a success clears it. */ let failureReported = false -/** Starts a balance read that nothing awaits; reports a run of failures once. */ +/** Reads the balance for a caller that does not await it; reports a run of failures once and never rejects. */ export function refreshBalanceInBackground( fetchBalance: () => Promise -): void { - void fetchBalance().then( +): Promise { + return fetchBalance().then( () => { failureReported = false }, diff --git a/src/platform/settings/components/SettingDialog.vue b/src/platform/settings/components/SettingDialog.vue index ebc9ba5a9dc..12315f7af84 100644 --- a/src/platform/settings/components/SettingDialog.vue +++ b/src/platform/settings/components/SettingDialog.vue @@ -308,7 +308,7 @@ watch(activeCategoryKey, (newKey, oldKey) => { activeCategoryKey.value = oldKey } if (newKey === 'credits') { - refreshBalanceInBackground(fetchBalance) + void refreshBalanceInBackground(fetchBalance) } if (newKey) { void nextTick(() => { From ed5bb2275b7f36f05d2d2c644f13eb040098cb19 Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 29 Oct 2026 04:12:29 +0000 Subject: [PATCH 5/6] fix(billing): budget background balance reports by time, not by success Clearing the budget on a successful background read missed successes on the direct read paths and counted a superseded read as one. Report at most one failure every five minutes instead, whatever happens between. --- .../billing/refreshBalanceInBackground.test.ts | 17 ++++++++++++----- .../billing/refreshBalanceInBackground.ts | 14 +++++++------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/platform/billing/refreshBalanceInBackground.test.ts b/src/platform/billing/refreshBalanceInBackground.test.ts index 8c6a6528044..3b2fcd1a607 100644 --- a/src/platform/billing/refreshBalanceInBackground.test.ts +++ b/src/platform/billing/refreshBalanceInBackground.test.ts @@ -1,8 +1,10 @@ import { reportError } from '@comfyorg/telemetry/reportError' -import { beforeEach, describe, expect, it } from 'vitest' +import { beforeEach, describe, expect, it, vi } from 'vitest' import { refreshBalanceInBackground } from '@/platform/billing/refreshBalanceInBackground' +const FIVE_MINUTES_MS = 5 * 60_000 + function fail() { return Promise.reject(new Error('Balance temporarily unavailable')) } @@ -12,8 +14,11 @@ function succeed() { } describe('refreshBalanceInBackground', () => { - beforeEach(async () => { - await refreshBalanceInBackground(succeed) + let testStart = Date.now() + + beforeEach(() => { + testStart += 2 * FIVE_MINUTES_MS + vi.setSystemTime(testStart) }) it('reports nothing when the read succeeds', async () => { @@ -34,14 +39,16 @@ describe('refreshBalanceInBackground', () => { }) }) - it('reports a run of failures once, and again after a success', async () => { + it('reports at most one failure every five minutes, whatever succeeds in between', async () => { await Promise.all([ refreshBalanceInBackground(fail), refreshBalanceInBackground(fail) ]) + await refreshBalanceInBackground(succeed) + await refreshBalanceInBackground(fail) expect(reportError).toHaveBeenCalledOnce() - await refreshBalanceInBackground(succeed) + vi.setSystemTime(Date.now() + FIVE_MINUTES_MS) await refreshBalanceInBackground(fail) expect(reportError).toHaveBeenCalledTimes(2) diff --git a/src/platform/billing/refreshBalanceInBackground.ts b/src/platform/billing/refreshBalanceInBackground.ts index 2e4d6abf557..35476071649 100644 --- a/src/platform/billing/refreshBalanceInBackground.ts +++ b/src/platform/billing/refreshBalanceInBackground.ts @@ -1,18 +1,18 @@ import { reportError } from '@comfyorg/telemetry/reportError' -let failureReported = false +const REPORT_INTERVAL_MS = 5 * 60_000 +let lastReportedAt = Number.NEGATIVE_INFINITY -/** Reads the balance for a caller that does not await it; reports a run of failures once and never rejects. */ +/** Reads the balance for a caller that does not await it; reports a failure at most every 5 minutes and never rejects. */ export function refreshBalanceInBackground( fetchBalance: () => Promise ): Promise { return fetchBalance().then( - () => { - failureReported = false - }, + () => undefined, (error: unknown) => { - if (failureReported) return - failureReported = true + const now = Date.now() + if (now - lastReportedAt < REPORT_INTERVAL_MS) return + lastReportedAt = now reportError(error, { surface: 'billing', errorType: 'error_refreshing_balance', From 39694bbaec01d85a29355180099458a50e94e125 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Wed, 28 Oct 2026 22:37:33 -0700 Subject: [PATCH 6/6] fix(billing): guard a balance read by workspace, as the status read does A balance read for the previous workspace could still land after a switch replaced the billing adapter. Resolving, it wrote that workspace's balance into the discarded adapter; rejecting, it reached refreshBalanceInBackground and was reported as a billing warning for a read the billing state had already dropped, which the new report is meant to exclude. fetchStatus already captured the workspace id and checked it, so the guard is generalised over both reads rather than duplicated. --- .../composables/useWorkspaceBilling.test.ts | 34 +++++++++++++++++++ .../composables/useWorkspaceBilling.ts | 18 +++++++--- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/platform/billing/composables/useWorkspaceBilling.test.ts b/src/platform/billing/composables/useWorkspaceBilling.test.ts index 065501e4752..5f150c28a3e 100644 --- a/src/platform/billing/composables/useWorkspaceBilling.test.ts +++ b/src/platform/billing/composables/useWorkspaceBilling.test.ts @@ -812,6 +812,40 @@ describe('useWorkspaceBilling', () => { expect(billing.error.value).toBeNull() expect(billing.balance.value?.amountMicros).toBe(5_000_000) }) + + it('settles a failed balance read quietly once the workspace changed', async () => { + const balanceRead = createDeferred() + mockWorkspaceApi.getBillingBalance.mockReturnValueOnce( + balanceRead.promise + ) + const billing = setupBilling() + + const read = billing.fetchBalance() + Object.assign(useTeamWorkspaceStore(), { + activeWorkspace: { id: 'workspace-2' } + }) + balanceRead.reject(new Error('stale workspace balance failed')) + + await expect(read).resolves.toBeUndefined() + expect(billing.error.value).toBeNull() + }) + + it('does not keep a balance from a stale workspace response', async () => { + const balanceRead = createDeferred() + mockWorkspaceApi.getBillingBalance.mockReturnValueOnce( + balanceRead.promise + ) + const billing = setupBilling() + + const read = billing.fetchBalance() + Object.assign(useTeamWorkspaceStore(), { + activeWorkspace: { id: 'workspace-2' } + }) + balanceRead.resolve(zeroBalance) + await read + + expect(billing.balance.value).toBeNull() + }) }) describe('subscribe', () => { diff --git a/src/platform/billing/composables/useWorkspaceBilling.ts b/src/platform/billing/composables/useWorkspaceBilling.ts index 397640d9311..0a47666011b 100644 --- a/src/platform/billing/composables/useWorkspaceBilling.ts +++ b/src/platform/billing/composables/useWorkspaceBilling.ts @@ -355,12 +355,13 @@ export function useWorkspaceBilling(): WorkspaceBilling { return true } - function isStaleStatusRead( + function isStaleBillingRead( + billingResource: keyof typeof latestBillingReadIds, requestId: number, workspaceId: string | undefined ): boolean { return ( - requestId !== latestBillingReadIds.status || + requestId !== latestBillingReadIds[billingResource] || workspaceId !== workspaceStore.activeWorkspace?.id ) } @@ -381,7 +382,10 @@ export function useWorkspaceBilling(): WorkspaceBilling { const status = rail ? await readOnRail(rail.readStatus, 'getBillingStatus') : await billingApi.getBillingStatus() - if (status === undefined || isStaleStatusRead(requestId, workspaceId)) + if ( + status === undefined || + isStaleBillingRead('status', requestId, workspaceId) + ) return seatCapacity.value = seatCapacityFrom(status) @@ -406,6 +410,7 @@ export function useWorkspaceBilling(): WorkspaceBilling { async function fetchBalance(): Promise { const requestId = ++latestBillingReadIds.balance + const workspaceId = workspaceStore.activeWorkspace?.id const rail: BillingReadRail | null = useBillingReadRail() isLoading.value = true error.value = null @@ -413,11 +418,14 @@ export function useWorkspaceBilling(): WorkspaceBilling { const balance = rail ? await readOnRail(rail.readBalance, 'getBillingBalance') : await billingApi.getBillingBalance() - if (balance !== undefined && requestId === latestBillingReadIds.balance) { + if ( + balance !== undefined && + !isStaleBillingRead('balance', requestId, workspaceId) + ) { balanceData.value = balance } } catch (err) { - if (requestId !== latestBillingReadIds.balance) return + if (isStaleBillingRead('balance', requestId, workspaceId)) return error.value = err instanceof Error ? err.message : 'Failed to fetch balance' throw err