From 7a3310ba7cc8dd1d8511dd96b57c513c83d71337 Mon Sep 17 00:00:00 2001 From: Wei Hai Date: Tue, 4 Aug 2026 12:51:55 -0700 Subject: [PATCH 1/4] fix(billing): don't show an error when a checkout is superseded Picking a different plan mid-checkout makes the API replace the pending subscription operation. The replaced operation terminates as failed even though nothing went wrong, so the user saw "We couldn't update your subscription" while the replacement checkout was still succeeding. Suppress the error toast for that case, keyed on the API's `checkout_superseded` reason, and report it as `stale_operation` rather than `unknown` so it stops counting as an unexplained billing failure. Genuine failures are unaffected. Co-Authored-By: Claude Opus 5 (1M context) --- .../stores/billingOperationStore.test.ts | 26 +++++++++++++++++++ .../workspace/stores/billingOperationStore.ts | 9 +++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/platform/workspace/stores/billingOperationStore.test.ts b/src/platform/workspace/stores/billingOperationStore.test.ts index 7f8f4c0eb34..84b6fa7d211 100644 --- a/src/platform/workspace/stores/billingOperationStore.test.ts +++ b/src/platform/workspace/stores/billingOperationStore.test.ts @@ -438,6 +438,32 @@ describe('billingOperationStore', () => { }) }) + it('stays silent when a checkout was superseded by a new plan choice', async () => { + vi.mocked(workspaceApi.getBillingOpStatus).mockResolvedValue({ + id: 'op-1', + status: 'failed', + error_message: 'checkout_superseded', + started_at: new Date().toISOString() + }) + + const store = useBillingOperationStore() + void store.startOperation('op-1', 'subscription') + + await vi.advanceTimersByTimeAsync(0) + + expect(store.getOperation('op-1')?.status).toBe('failed') + expect(mockToastAdd).not.toHaveBeenCalledWith( + expect.objectContaining({ severity: 'error' }) + ) + expect(mockTrackBillingEvent).toHaveBeenCalledWith( + expect.objectContaining({ + stage: 'failed', + billing_op_id: 'op-1', + failure_category: 'stale_operation' + }) + ) + }) + it('uses default message when no error_message in response', async () => { vi.mocked(workspaceApi.getBillingOpStatus).mockResolvedValue({ id: 'op-1', diff --git a/src/platform/workspace/stores/billingOperationStore.ts b/src/platform/workspace/stores/billingOperationStore.ts index d2300037614..70a83403506 100644 --- a/src/platform/workspace/stores/billingOperationStore.ts +++ b/src/platform/workspace/stores/billingOperationStore.ts @@ -25,6 +25,10 @@ const BACKOFF_MULTIPLIER = 1.5 const TIMEOUT_MS = 120_000 const SUBSCRIPTION_ACTION_DISCOVERY_TIMEOUT_MS = 5 * 60_000 const AUTHENTICATION_TIMEOUT_MS = 23 * 60 * 60_000 +// Failure reason for a checkout the user replaced by picking a different plan +// mid-flow. The operation is terminal-failed only because it never completed — +// its replacement is proceeding normally, so there is nothing to report. +const CHECKOUT_SUPERSEDED_REASON = 'checkout_superseded' type OperationType = 'subscription' | 'topup' | 'cancel' type OperationStatus = 'pending' | 'succeeded' | 'failed' | 'timeout' @@ -356,6 +360,7 @@ export const useBillingOperationStore = defineStore('billingOperation', () => { const operation = operations.value.get(opId) if (!operation) return + const superseded = errorMessage === CHECKOUT_SUPERSEDED_REASON const defaultMessage = failureMessage(operation.type) const detail = operation.type === 'subscription' @@ -376,7 +381,7 @@ export const useBillingOperationStore = defineStore('billingOperation', () => { cycle: operation.cycle, checkout_type: operation.checkoutType, payment_intent_source: operation.paymentIntentSource, - failure_category: 'unknown' + failure_category: superseded ? 'stale_operation' : 'unknown' }) if (operation.downgradeToPersonal) { telemetry?.trackBillingEvent({ @@ -391,7 +396,7 @@ export const useBillingOperationStore = defineStore('billingOperation', () => { }) } - if (operation.type !== 'cancel') { + if (operation.type !== 'cancel' && !superseded) { useToastStore().add({ severity: 'error', summary: defaultMessage, From 28ace603229cf09d84522de5228f4763020f60aa Mon Sep 17 00:00:00 2001 From: Wei Hai Date: Tue, 4 Aug 2026 13:25:25 -0700 Subject: [PATCH 2/4] test(billing): use createTestingPinia in the billing operation store test Matches the documented convention in docs/testing/vitest-patterns.md. All 48 tests in the file pass unchanged. Co-Authored-By: Claude Opus 5 (1M context) --- src/platform/workspace/stores/billingOperationStore.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/platform/workspace/stores/billingOperationStore.test.ts b/src/platform/workspace/stores/billingOperationStore.test.ts index 84b6fa7d211..f082720b7e7 100644 --- a/src/platform/workspace/stores/billingOperationStore.test.ts +++ b/src/platform/workspace/stores/billingOperationStore.test.ts @@ -1,4 +1,5 @@ -import { setActivePinia, createPinia } from 'pinia' +import { createTestingPinia } from '@pinia/testing' +import { setActivePinia } from 'pinia' import { beforeEach, describe, expect, it, vi, afterEach } from 'vitest' import { ref } from 'vue' @@ -80,7 +81,7 @@ import { useBillingOperationStore } from './billingOperationStore' describe('billingOperationStore', () => { beforeEach(() => { - setActivePinia(createPinia()) + setActivePinia(createTestingPinia({ stubActions: false })) vi.clearAllMocks() vi.useFakeTimers() mockActiveWorkspaceId.value = 'workspace-1' From 62550d97ce06d3e93233021cd6c0fb0995e54576 Mon Sep 17 00:00:00 2001 From: Wei Hai Date: Tue, 4 Aug 2026 13:30:45 -0700 Subject: [PATCH 3/4] fix(billing): categorise the downgrade failure event too A superseded operation emits two failure events when it carries downgrade-to-personal metadata. Only the generic one was reclassified, so a replaced downgrade checkout kept reporting `unknown` and inflating the unexplained-failure count this change exists to reduce. Hoist the category and use it for both emissions. Co-Authored-By: Claude Opus 5 (1M context) --- .../stores/billingOperationStore.test.ts | 33 +++++++++++++++++++ .../workspace/stores/billingOperationStore.ts | 5 +-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/platform/workspace/stores/billingOperationStore.test.ts b/src/platform/workspace/stores/billingOperationStore.test.ts index f082720b7e7..765925e3e2b 100644 --- a/src/platform/workspace/stores/billingOperationStore.test.ts +++ b/src/platform/workspace/stores/billingOperationStore.test.ts @@ -465,6 +465,39 @@ describe('billingOperationStore', () => { ) }) + it('categorises both events when a superseded op was a downgrade', async () => { + vi.mocked(workspaceApi.getBillingOpStatus).mockResolvedValue({ + id: 'op-1', + status: 'failed', + error_message: 'checkout_superseded', + started_at: new Date().toISOString() + }) + + const store = useBillingOperationStore() + void store.startOperation('op-1', 'subscription', { + downgradeToPersonal: { + memberRemovalCount: 2, + memberRemovalFailures: 0, + targetTier: 'free' + } + }) + + await vi.advanceTimersByTimeAsync(0) + + // Both emissions must agree: a downgrade that was merely replaced is not + // an unexplained billing failure in either event stream. + expect(mockTrackBillingEvent).toHaveBeenCalledWith( + expect.objectContaining({ + operation: 'downgrade_to_personal', + stage: 'failed', + failure_category: 'stale_operation' + }) + ) + expect(mockTrackBillingEvent).not.toHaveBeenCalledWith( + expect.objectContaining({ failure_category: 'unknown' }) + ) + }) + it('uses default message when no error_message in response', async () => { vi.mocked(workspaceApi.getBillingOpStatus).mockResolvedValue({ id: 'op-1', diff --git a/src/platform/workspace/stores/billingOperationStore.ts b/src/platform/workspace/stores/billingOperationStore.ts index 70a83403506..7e51afc69ed 100644 --- a/src/platform/workspace/stores/billingOperationStore.ts +++ b/src/platform/workspace/stores/billingOperationStore.ts @@ -361,6 +361,7 @@ export const useBillingOperationStore = defineStore('billingOperation', () => { if (!operation) return const superseded = errorMessage === CHECKOUT_SUPERSEDED_REASON + const failureCategory = superseded ? 'stale_operation' : 'unknown' const defaultMessage = failureMessage(operation.type) const detail = operation.type === 'subscription' @@ -381,7 +382,7 @@ export const useBillingOperationStore = defineStore('billingOperation', () => { cycle: operation.cycle, checkout_type: operation.checkoutType, payment_intent_source: operation.paymentIntentSource, - failure_category: superseded ? 'stale_operation' : 'unknown' + failure_category: failureCategory }) if (operation.downgradeToPersonal) { telemetry?.trackBillingEvent({ @@ -392,7 +393,7 @@ export const useBillingOperationStore = defineStore('billingOperation', () => { member_removal_failures: operation.downgradeToPersonal.memberRemovalFailures, target_tier: operation.downgradeToPersonal.targetTier, - failure_category: 'unknown' + failure_category: failureCategory }) } From 59fbc49c070df0f5295b9862e2abccd57c2cd1bf Mon Sep 17 00:00:00 2001 From: Wei Hai Date: Tue, 4 Aug 2026 13:39:06 -0700 Subject: [PATCH 4/4] test(billing): assert the primary failure category too The test asserted only the downgrade emission plus a negative on "unknown", so a primary event carrying some other wrong category still passed. Assert it explicitly. Co-Authored-By: Claude Opus 5 (1M context) --- .../workspace/stores/billingOperationStore.test.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/platform/workspace/stores/billingOperationStore.test.ts b/src/platform/workspace/stores/billingOperationStore.test.ts index 765925e3e2b..3510ef38f83 100644 --- a/src/platform/workspace/stores/billingOperationStore.test.ts +++ b/src/platform/workspace/stores/billingOperationStore.test.ts @@ -486,6 +486,13 @@ describe('billingOperationStore', () => { // Both emissions must agree: a downgrade that was merely replaced is not // an unexplained billing failure in either event stream. + expect(mockTrackBillingEvent).toHaveBeenCalledWith( + expect.objectContaining({ + operation: 'operation', + stage: 'failed', + failure_category: 'stale_operation' + }) + ) expect(mockTrackBillingEvent).toHaveBeenCalledWith( expect.objectContaining({ operation: 'downgrade_to_personal',