diff --git a/browser_tests/tests/agent/agentFreeUsePlacement.spec.ts b/browser_tests/tests/agent/agentFreeUsePlacement.spec.ts index fbc14558d26..db2f6b63d2c 100644 --- a/browser_tests/tests/agent/agentFreeUsePlacement.spec.ts +++ b/browser_tests/tests/agent/agentFreeUsePlacement.spec.ts @@ -2,11 +2,17 @@ import { expect, mergeTests } from '@playwright/test' import type { BillingStatusResponse } from '@comfyorg/ingest-types' +import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' -import { agentTest, bootAgentApp } from '@e2e/fixtures/agentPanelFixture' +import { + agentTest, + bootAgentApp, + mockAgentTurnApi +} from '@e2e/fixtures/agentPanelFixture' import { DEFAULT_BILLING_STATUS } from '@e2e/fixtures/utils/cloudBillingMocks' import { hostTelemetryFixture } from '@e2e/fixtures/hostTelemetryFixture' import { expectElementScreenshot } from '@e2e/fixtures/utils/screenshotRegion' +import { webSocketFixture } from '@e2e/fixtures/ws' const FLAG = 'agent-free-use-message-placement' const PLACEMENT = 'near-composer' @@ -18,12 +24,13 @@ function billingStatus({ }): BillingStatusResponse { return { ...DEFAULT_BILLING_STATUS, + billing_rail: 'metronome', scoped_effective_has_funds: { agent: true }, scoped_has_funds: { agent: grantHeld } } } -const test = mergeTests(agentTest, hostTelemetryFixture) +const test = mergeTests(agentTest, hostTelemetryFixture, webSocketFixture) test.describe( 'Agent free-use placement experiment', @@ -117,3 +124,78 @@ test.describe( }) } ) + +test.describe( + 'Local Agent free-use notice', + { tag: ['@oss', '@agent', '@ui'] }, + () => { + test('shows the local default only while an Agent grant is available', async ({ + page + }) => { + await bootAgentApp(page, true, { + features: { enable_telemetry: true }, + billingStatus: billingStatus({ grantHeld: true }) + }) + const agentPanel = new AgentPanel(page) + + await agentPanel.open() + + const notice = agentPanel.root.getByRole('note', { + name: 'Free use notice' + }) + await expect(notice).toBeVisible() + await expect(notice).toHaveAttribute('data-placement', 'near-composer') + }) + + test('removes the free-use claim after the local Agent grant is spent', async ({ + getAgentSocket, + page + }) => { + const accepted = { + thread_id: '783eff5c-2133-45bb-9733-2b9637d3e79c', + message_id: 'fb9d7a52-bc14-43c5-9039-52a346507e96', + workflow_id: 'a385d379-9f50-4495-975d-b310a62370f6' + } + await mockAgentTurnApi(page, accepted) + await bootAgentApp(page, true, { + features: { enable_telemetry: true }, + billingStatus: billingStatus({ grantHeld: true }) + }) + const agentPanel = new AgentPanel(page) + + await agentPanel.open() + const notice = agentPanel.root.getByRole('note', { + name: 'Free use notice' + }) + await expect(notice).toBeVisible() + + await page.route('**/api/billing/status', (route) => + route.fulfill({ json: billingStatus({ grantHeld: false }) }) + ) + const billingRead = page.waitForResponse( + (response) => + response.request().method() === 'GET' && + new URL(response.url()).pathname === '/api/billing/status' + ) + const turnAccepted = page.waitForResponse( + (response) => + response.request().method() === 'POST' && + /^\/api\/agent\/threads\/[^/]+\/messages$/.test( + new URL(response.url()).pathname + ) + ) + await agentPanel.sendMessage('Use the current workflow') + const socket = await getAgentSocket() + await turnAccepted + await expect(agentPanel.stopButton).toBeVisible() + const completed: AgentWsEvent = { + type: 'agent_message_done', + data: { ...accepted, usage: null } + } + socket.send(JSON.stringify(completed)) + await billingRead + + await expect(notice).toBeHidden() + }) + } +) diff --git a/src/platform/billing/composables/types.ts b/src/platform/billing/composables/types.ts index c46a65be90b..55cc5b659f3 100644 --- a/src/platform/billing/composables/types.ts +++ b/src/platform/billing/composables/types.ts @@ -86,6 +86,8 @@ export interface SubscriptionInfo { * disabled, absent when the server does not report it. */ agentScopedHasFunds?: boolean + /** True only when the billing status names the rail that supplied the grant. */ + agentScopedHasFundsAuthoritative?: boolean } /** diff --git a/src/platform/billing/composables/useBillingContext.test.ts b/src/platform/billing/composables/useBillingContext.test.ts index 3861198b108..8d4f3de9b59 100644 --- a/src/platform/billing/composables/useBillingContext.test.ts +++ b/src/platform/billing/composables/useBillingContext.test.ts @@ -210,7 +210,9 @@ describe('useBillingContext', () => { endDate: null, isCancelled: false, hasFunds: true, - agentHasFunds: true + agentHasFunds: true, + agentScopedHasFunds: undefined, + agentScopedHasFundsAuthoritative: false }) }) @@ -416,7 +418,7 @@ describe('useBillingContext', () => { await context.fetchStatus() expect(mockFetchPlans).toHaveBeenCalledOnce() - expect(useSubscription().fetchStatus).toHaveBeenCalled() + expect(useSubscription().fetchStatusDirect).toHaveBeenCalled() expect(workspaceApi.getBillingStatus).not.toHaveBeenCalled() await context.previewSubscribe('creator-annual') @@ -463,7 +465,7 @@ describe('useBillingContext', () => { const context = useBillingContext() await vi.waitFor(() => { - expect(useSubscription().fetchStatus).toHaveBeenCalled() + expect(useSubscription().fetchStatusDirect).toHaveBeenCalled() expect(useAuthStore().fetchBalance).toHaveBeenCalled() }) vi.clearAllMocks() @@ -476,7 +478,7 @@ describe('useBillingContext', () => { expect(context.type.value).toBe('workspace') expect(workspaceApi.getBillingStatus).toHaveBeenCalled() expect(workspaceApi.getBillingBalance).toHaveBeenCalled() - expect(useSubscription().fetchStatus).not.toHaveBeenCalled() + expect(useSubscription().fetchStatusDirect).not.toHaveBeenCalled() expect(useAuthStore().fetchBalance).not.toHaveBeenCalled() }) @@ -485,7 +487,7 @@ describe('useBillingContext', () => { const context = useBillingContext() await vi.waitFor(() => { - expect(useSubscription().fetchStatus).toHaveBeenCalled() + expect(useSubscription().fetchStatusDirect).toHaveBeenCalled() expect(useAuthStore().fetchBalance).toHaveBeenCalled() }) vi.clearAllMocks() @@ -554,7 +556,9 @@ describe('useBillingContext', () => { await refreshBilling(scope) - expect(useSubscription().fetchStatus).toHaveBeenCalledTimes(status) + expect(useSubscription().fetchStatusDirect).toHaveBeenCalledTimes( + status + ) expect(useAuthStore().fetchBalance).toHaveBeenCalledTimes(balance) } ) @@ -630,7 +634,7 @@ describe('useBillingContext', () => { await context.fetchBalance() expect(context.type.value).toBe('unknown') - expect(useSubscription().fetchStatus).not.toHaveBeenCalled() + expect(useSubscription().fetchStatusDirect).not.toHaveBeenCalled() expect(useAuthStore().fetchBalance).not.toHaveBeenCalled() expect(workspaceApi.getBillingStatus).not.toHaveBeenCalled() @@ -638,7 +642,7 @@ describe('useBillingContext', () => { await initializing expect(context.isInitialized.value).toBe(true) - expect(useSubscription().fetchStatus).toHaveBeenCalled() + expect(useSubscription().fetchStatusDirect).toHaveBeenCalled() expect(context.type.value).toBe('legacy') expect(workspaceApi.getBillingStatus).not.toHaveBeenCalled() }) @@ -653,14 +657,14 @@ describe('useBillingContext', () => { expect(context.type.value).toBe('unknown') expect(workspaceApi.getBillingStatus).not.toHaveBeenCalled() - expect(useSubscription().fetchStatus).not.toHaveBeenCalled() + expect(useSubscription().fetchStatusDirect).not.toHaveBeenCalled() loaded.value = true await initializing expect(workspaceApi.getBillingStatus).toHaveBeenCalled() expect(context.type.value).toBe('workspace') - expect(useSubscription().fetchStatus).not.toHaveBeenCalled() + expect(useSubscription().fetchStatusDirect).not.toHaveBeenCalled() }) it('reports a presentable error once if the workspace never loads', async () => { diff --git a/src/platform/billing/composables/useLegacyBilling.test.ts b/src/platform/billing/composables/useLegacyBilling.test.ts index a5d69b6370d..a8e40f7875e 100644 --- a/src/platform/billing/composables/useLegacyBilling.test.ts +++ b/src/platform/billing/composables/useLegacyBilling.test.ts @@ -181,6 +181,35 @@ describe('useLegacyBilling', () => { }) } ) + + it('stops treating a cached Agent grant as authoritative after the current read fails', async () => { + useSubscription().subscriptionStatus.value = statusWith({ + billing_rail: 'stripe', + scoped_has_funds: { agent: true } + }) + let rejectStatusRead!: (reason: unknown) => void + const statusRead = new Promise( + (_resolve, reject) => { + rejectStatusRead = (reason) => reject(reason) + } + ) + vi.mocked(useSubscription().fetchStatusDirect).mockReturnValueOnce( + statusRead + ) + const billing = useLegacyBilling() + + const refresh = billing.fetchStatus() + expect(billing.subscription.value?.agentScopedHasFundsAuthoritative).toBe( + true + ) + + rejectStatusRead(new Error('status unavailable')) + await expect(refresh).rejects.toThrow('status unavailable') + expect(billing.subscription.value?.agentScopedHasFundsAuthoritative).toBe( + false + ) + expect(billing.subscription.value?.agentScopedHasFunds).toBe(true) + }) }) describe('initialize', () => { @@ -212,9 +241,32 @@ describe('useLegacyBilling', () => { await useLegacyBilling().initialize() - expect(useSubscription().fetchStatus).toHaveBeenCalledTimes(reads) + expect(useSubscription().fetchStatusDirect).toHaveBeenCalledTimes(reads) } ) + + it('finishes initialization and reports a failed status read', async () => { + const failure = new Error('status unavailable') + useSubscription().subscriptionStatus.value = statusWith({ + billing_rail: 'stripe', + scoped_has_funds: { agent: true } + }) + vi.mocked(useSubscription().fetchStatusDirect).mockRejectedValueOnce( + failure + ) + vi.mocked(useAuthStore().fetchBalance).mockResolvedValueOnce(null) + const billing = useLegacyBilling() + + await expect(billing.initialize()).resolves.toBeUndefined() + + expect(billing.isInitialized.value).toBe(true) + expect(billing.subscription.value?.agentScopedHasFundsAuthoritative).toBe( + false + ) + expect(useAuthActions().reportError).toHaveBeenCalledExactlyOnceWith( + failure + ) + }) }) describe('resubscribe', () => { diff --git a/src/platform/billing/composables/useLegacyBilling.ts b/src/platform/billing/composables/useLegacyBilling.ts index 50108603703..b760b8b7d4c 100644 --- a/src/platform/billing/composables/useLegacyBilling.ts +++ b/src/platform/billing/composables/useLegacyBilling.ts @@ -31,13 +31,19 @@ type RuntimeLegacyBalance = Omit< amount_micros?: number effective_balance_micros?: number } +type StatusReadResult = { ok: true } | { ok: false; cause: unknown } -function fundsFromStatus(status: BillingStatusResponse | null) { +function fundsFromStatus( + status: BillingStatusResponse | null, + statusReadAuthoritative: boolean +) { const hasFunds = status?.has_funds ?? false return { hasFunds, agentHasFunds: status?.scoped_effective_has_funds?.agent ?? hasFunds, - agentScopedHasFunds: status?.scoped_has_funds?.agent + agentScopedHasFunds: status?.scoped_has_funds?.agent, + agentScopedHasFundsAuthoritative: + statusReadAuthoritative && status?.billing_rail !== undefined } } @@ -53,7 +59,6 @@ export function useLegacyBilling(): BillingState & BillingActions { subscriptionDuration, subscriptionStatus: legacySubscriptionStatus, isCancelled, - fetchStatus: legacyFetchStatus, fetchStatusDirect: legacyFetchStatusDirect, manageSubscription: legacyManageSubscription, subscribeDirect: legacySubscribeDirect @@ -65,6 +70,8 @@ export function useLegacyBilling(): BillingState & BillingActions { const isInitialized = ref(false) const isLoading = ref(false) const error = ref(null) + const statusReadAuthoritative = ref(true) + let latestStatusReadId = 0 // The backend refuses legacy calls for a workspace that moved to workspace // billing. Refresh the status so the next click routes correctly and ask the @@ -128,7 +135,7 @@ export function useLegacyBilling(): BillingState & BillingActions { renewalDate: status?.renewal_date ?? null, endDate: status?.cancel_at ?? null, isCancelled: isCancelled.value, - ...fundsFromStatus(status) + ...fundsFromStatus(status, statusReadAuthoritative.value) } }) @@ -181,9 +188,14 @@ export function useLegacyBilling(): BillingState & BillingActions { isLoading.value = true error.value = null try { - await Promise.all([fetchStatus(), fetchBalance()]) + const [statusRead] = await Promise.all([readStatus(), fetchBalance()]) + reportStatusReadFailure(statusRead) if (isFreeTier.value && balance.value?.amountMicros === 0) { - await Promise.all([fetchBalance(), fetchStatus()]) + const [, repeatedStatusRead] = await Promise.all([ + fetchBalance(), + readStatus() + ]) + reportStatusReadFailure(repeatedStatusRead) } isInitialized.value = true } catch (err) { @@ -195,20 +207,35 @@ export function useLegacyBilling(): BillingState & BillingActions { } } - async function fetchStatus(): Promise { + function reportStatusReadFailure(result: StatusReadResult): void { + if (!result.ok) authActions.reportError(result.cause) + } + + async function readStatus(): Promise { + const requestId = ++latestStatusReadId isLoading.value = true error.value = null try { - await legacyFetchStatus() - } catch (err) { + const status = await legacyFetchStatusDirect() + if (requestId === latestStatusReadId) + statusReadAuthoritative.value = status?.billing_rail !== undefined + return { ok: true } + } catch (cause) { + if (requestId === latestStatusReadId) + statusReadAuthoritative.value = false error.value = - err instanceof Error ? err.message : 'Failed to fetch subscription' - throw err + cause instanceof Error ? cause.message : 'Failed to fetch subscription' + return { ok: false, cause } } finally { isLoading.value = false } } + async function fetchStatus(): Promise { + const result = await readStatus() + if (!result.ok) throw result.cause + } + async function fetchBalance(): Promise { isLoading.value = true error.value = null diff --git a/src/platform/billing/composables/useWorkspaceBilling.test.ts b/src/platform/billing/composables/useWorkspaceBilling.test.ts index 705241af554..ba7b5c1a5a9 100644 --- a/src/platform/billing/composables/useWorkspaceBilling.test.ts +++ b/src/platform/billing/composables/useWorkspaceBilling.test.ts @@ -667,6 +667,30 @@ describe('useWorkspaceBilling', () => { expect(billing.occupiedSeats.value).toBe(75) }) + it('stops treating a cached Agent grant as authoritative after the current read fails', async () => { + mockWorkspaceApi.getBillingStatus.mockResolvedValueOnce({ + ...activeStatus, + billing_rail: 'stripe', + scoped_has_funds: { agent: true } + }) + const billing = setupBilling() + await billing.fetchStatus() + + const statusRead = createDeferred() + mockWorkspaceApi.getBillingStatus.mockReturnValueOnce(statusRead.promise) + const refresh = billing.fetchStatus() + expect(billing.subscription.value?.agentScopedHasFundsAuthoritative).toBe( + true + ) + + statusRead.reject(new Error('status unavailable')) + await expect(refresh).rejects.toThrow('status unavailable') + expect(billing.subscription.value?.agentScopedHasFundsAuthoritative).toBe( + false + ) + expect(billing.subscription.value?.agentScopedHasFunds).toBe(true) + }) + it('accepts missing seat capacity and clears the last known value', async () => { mockWorkspaceApi.getBillingStatus.mockResolvedValueOnce(activeStatus) const billing = setupBilling() diff --git a/src/platform/billing/composables/useWorkspaceBilling.ts b/src/platform/billing/composables/useWorkspaceBilling.ts index e90f0c54f36..5f313e54a99 100644 --- a/src/platform/billing/composables/useWorkspaceBilling.ts +++ b/src/platform/billing/composables/useWorkspaceBilling.ts @@ -213,6 +213,7 @@ export function useWorkspaceBilling(): WorkspaceBilling { const error = ref(null) const statusData = shallowRef(null) + const statusReadAuthoritative = ref(true) let adoption: Promise = Promise.resolve(false) const seatCapacity = shallowRef(null) const balanceData = shallowRef(null) @@ -242,7 +243,9 @@ export function useWorkspaceBilling(): WorkspaceBilling { hasFunds: status.has_funds, agentHasFunds: status.scoped_effective_has_funds?.agent ?? status.has_funds, - agentScopedHasFunds: status.scoped_has_funds?.agent + agentScopedHasFunds: status.scoped_has_funds?.agent, + agentScopedHasFundsAuthoritative: + statusReadAuthoritative.value && status.billing_rail !== undefined } }) @@ -387,12 +390,14 @@ export function useWorkspaceBilling(): WorkspaceBilling { seatCapacity.value = seatCapacityFrom(status) statusData.value = status + statusReadAuthoritative.value = true if (workspaceId && status.billing_rail) { workspaceStore.setWorkspaceBillingRail(workspaceId, status.billing_rail) } adoption = resumePendingOperation(status) } catch (err) { if (requestId === latestBillingReadIds.status) { + statusReadAuthoritative.value = false error.value = err instanceof Error ? err.message : 'Failed to fetch billing status' } diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index f9d7e10f73c..d42877a84aa 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -267,6 +267,7 @@ const paywallBilling = vi.hoisted(() => ({ const paywallHasFunds = ref(false) const paywallAgentHasFunds = ref() const paywallAgentScopedHasFunds = ref() +const paywallAgentScopedHasFundsAuthoritative = ref(false) vi.mock(import('@/platform/workspace/composables/useWorkspaceUI'), { spy: true @@ -390,7 +391,9 @@ beforeEach(() => { hasFunds: paywallHasFunds.value, agentHasFunds: paywallAgentHasFunds.value ?? paywallHasFunds.value, - agentScopedHasFunds: paywallAgentScopedHasFunds.value + agentScopedHasFunds: paywallAgentScopedHasFunds.value, + agentScopedHasFundsAuthoritative: + paywallAgentScopedHasFundsAuthoritative.value }) ), tier: computed(() => paywallBilling.tier), @@ -518,6 +521,7 @@ beforeEach(() => { paywallHasFunds.value = false paywallAgentHasFunds.value = undefined paywallAgentScopedHasFunds.value = undefined + paywallAgentScopedHasFundsAuthoritative.value = false }) const zAgentWsEventForTest = (raw: unknown): AgentChatEvent => @@ -710,6 +714,7 @@ describe('AgentPanelRoot first-use experience', () => { function placeFundedFreeUseNotice() { paywallHasFunds.value = true paywallAgentScopedHasFunds.value = true + paywallAgentScopedHasFundsAuthoritative.value = true vi.mocked(useFreeUsePlacement).mockReturnValueOnce( fromPartial({ variant: ref('top-banner'), @@ -750,6 +755,17 @@ describe('AgentPanelRoot first-use experience', () => { expect(reportFreeUseNotice).not.toHaveBeenCalled() }) + it('rejects a billing-disabled fallback that reports a grant without a rail', async () => { + placeFundedFreeUseNotice() + paywallAgentScopedHasFundsAuthoritative.value = false + + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + + expect(screen.queryByRole('note', { name: 'Free use notice' })).toBeNull() + expect(reportFreeUseNotice).not.toHaveBeenCalled() + }) + it('hides the notice while any inline paywall remains unresolved', async () => { placeFundedFreeUseNotice() const conversation = useAgentConversationStore() @@ -8593,6 +8609,42 @@ describe('AgentPanelRoot workflow binding', () => { await waitFor(() => expect(bodies).toHaveLength(1)) }) + it.for(['workspace', 'legacy'] as const)( + 'refreshes $ billing after successful consent before resuming the first send', + async (billingType) => { + makeTab('wf-42') + mockMessagesEndpoint('wf-42') + paywallBilling.type = billingType + paywallAgentHasFunds.value = true + Object.assign(useAgentConsentStore(), { accepted: false }) + withConsent.mockImplementationOnce(async (_trigger, onAccept) => { + Object.assign(useAgentConsentStore(), { accepted: true }) + onAccept() + }) + renderWithSelectedTarget() + + await sendFromComposer('build me a workflow') + + expect(paywallBilling.fetchStatus).toHaveBeenCalledOnce() + } + ) + + it.for(['workspace', 'legacy'] as const)( + 'refreshes $ billing when consent succeeds outside the first send', + async (billingType) => { + paywallBilling.type = billingType + Object.assign(useAgentConsentStore(), { accepted: false }) + renderWithSelectedTarget() + paywallBilling.fetchStatus.mockClear() + + Object.assign(useAgentConsentStore(), { accepted: true }) + + await waitFor(() => + expect(paywallBilling.fetchStatus).toHaveBeenCalledOnce() + ) + } + ) + it('asks signed-out users in a browser tab on a local Core to sign in before consent', async () => { distribution.isCloud = false makeTab('wf-42') @@ -8602,7 +8654,6 @@ describe('AgentPanelRoot workflow binding', () => { Object.assign(useAgentConsentStore(), { accepted: false }) const bodies = mockMessagesEndpoint('wf-42') renderWithSelectedTarget() - await userEvent.click(screen.getByRole('textbox')) await userEvent.paste('build me a workflow') await userEvent.click(screen.getByRole('button', { name: 'Send' })) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index b0bac994b68..993db4a2e29 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -1280,8 +1280,7 @@ const canvasEmptyAfterTurn = computed( boundCanvasNodeCount.value === 0 ) -function refreshBillingAfterTurn(): void { - if (billingType.value !== 'workspace') return +function refreshAgentBillingStatus(): void { void refreshBillingStatus().catch((error: unknown) => { reportError(error, { surface: 'agent', @@ -1290,6 +1289,15 @@ function refreshBillingAfterTurn(): void { }) } +watch( + [consentAccepted, consentIdentity], + ([accepted, identity], [previousAccepted, previousIdentity]) => { + if (accepted && (!previousAccepted || identity !== previousIdentity)) + refreshAgentBillingStatus() + }, + { flush: 'sync' } +) + // Adoption (onWorkflowAdopted) and tab activation (onAgentActiveTab) are the // primary spinner setters; the non-idle branch only re-arms it after the // stash/resume flip of a panel remount, where those setters never run. @@ -1310,7 +1318,7 @@ watch( } if (completedTurn) { graphActivity.finishTurn() - refreshBillingAfterTurn() + if (billingType.value === 'workspace') refreshAgentBillingStatus() } }, { immediate: true, flush: 'sync' } @@ -1636,21 +1644,17 @@ const { copy } = useClipboard({ legacy: true }) // DES-1221. Read while the panel is on screen, so the experiment's denominator // is the panel openers the hypothesis is about. const { variant: freeUseVariant, reportNotice: onFreeUseNotice } = - useFreeUsePlacement() + useFreeUsePlacement(isCloud) const { assignment: starterPromptAssignment, attributeExperiment: attributeStarterPromptExperiment, expose: exposeStarterPromptSet } = useStarterPromptSet() -/** - * `Try your first chat for free!` is a claim about the Agent's own granted - * allowance, so it renders on `scoped_has_funds.agent` as received. `=== true` - * is the whole gate: an absent field, an unsettled read and a rail that reports - * no scoped balance all fail closed without a separate branch. - */ const freeUseGrantActive = computed( - () => subscription.value?.agentScopedHasFunds === true + () => + subscription.value?.agentScopedHasFundsAuthoritative === true && + subscription.value.agentScopedHasFunds === true ) const freeUsePlacement = computed(() => diff --git a/src/workbench/extensions/agent/experiments/freeUsePlacement.test.ts b/src/workbench/extensions/agent/experiments/freeUsePlacement.test.ts index 95b4326da2d..f2d00f440ef 100644 --- a/src/workbench/extensions/agent/experiments/freeUsePlacement.test.ts +++ b/src/workbench/extensions/agent/experiments/freeUsePlacement.test.ts @@ -13,9 +13,12 @@ import { getSessionOverride } from '@/utils/sessionFeatureFlagOverride' import { useAgentPanelStore } from '@/workbench/extensions/agent/stores/agent/agentPanelStore' import { FREE_USE_PLACEMENT_FLAG, + LOCAL_FREE_USE_PLACEMENT, useFreeUsePlacement as useFreeUsePlacementImpl } from './freeUsePlacement' +const distribution = { isCloud: true } + vi.mock(import('@/platform/remoteConfig/remoteConfig')) vi.mock(import('@/platform/telemetry')) vi.mock(import('@/utils/devFeatureFlagOverride')) @@ -26,7 +29,7 @@ let placementScope: EffectScope function useFreeUsePlacement() { let placement: ReturnType | undefined placementScope.run(() => { - placement = useFreeUsePlacementImpl() + placement = useFreeUsePlacementImpl(distribution.isCloud) }) return placement! } @@ -34,6 +37,7 @@ function useFreeUsePlacement() { describe('useFreeUsePlacement', () => { beforeEach(() => { placementScope = effectScope() + distribution.isCloud = true authenticatedRemoteConfigState.value = 'unloaded' remoteConfig.value = {} }) @@ -61,6 +65,31 @@ describe('useFreeUsePlacement', () => { }) }) + it('reports Local notice telemetry without Cloud experiment exposure', () => { + distribution.isCloud = false + const telemetry = useTelemetry()! + const { variant, reportNotice } = useFreeUsePlacement() + + expect(variant.value).toBe(LOCAL_FREE_USE_PLACEMENT) + expect(telemetry.trackAgentFreeUseExposure).not.toHaveBeenCalled() + + reportNotice({ action: 'shown', placement: LOCAL_FREE_USE_PLACEMENT }) + reportNotice({ + action: 'learn_more_clicked', + placement: LOCAL_FREE_USE_PLACEMENT + }) + expect(telemetry.trackAgentFreeUseNotice).toHaveBeenNthCalledWith(1, { + action: 'shown', + placement: LOCAL_FREE_USE_PLACEMENT, + deployment: 'local' + }) + expect(telemetry.trackAgentFreeUseNotice).toHaveBeenNthCalledWith(2, { + action: 'learn_more_clicked', + placement: LOCAL_FREE_USE_PLACEMENT, + deployment: 'local' + }) + }) + it('reports a served control arm, which is an arm like any other', () => { remoteConfig.value = { 'agent-free-use-message-placement': 'control' } authenticatedRemoteConfigState.value = 'authenticated' diff --git a/src/workbench/extensions/agent/experiments/freeUsePlacement.ts b/src/workbench/extensions/agent/experiments/freeUsePlacement.ts index 1fd7b65e91a..455a4acf584 100644 --- a/src/workbench/extensions/agent/experiments/freeUsePlacement.ts +++ b/src/workbench/extensions/agent/experiments/freeUsePlacement.ts @@ -24,11 +24,13 @@ export const FREE_USE_VARIANTS = [ export type FreeUseVariant = AgentFreeUseVariant +export const LOCAL_FREE_USE_PLACEMENT = 'near-composer' satisfies FreeUseVariant + function isFreeUseVariant(value: unknown): value is FreeUseVariant { return FREE_USE_VARIANTS.some((variant) => variant === value) } -export function useFreeUsePlacement() { +export function useFreeUsePlacement(isCloudDistribution: boolean) { const panelStore = useAgentPanelStore() const assigned = ref('control') @@ -58,6 +60,8 @@ export function useFreeUsePlacement() { if (hasQaOverride) { if (isFreeUseVariant(override)) assigned.value = override + } else if (!isCloudDistribution) { + assigned.value = LOCAL_FREE_USE_PLACEMENT } else if (isAuthenticatedConfigLoaded.value) { assign() } else { @@ -68,7 +72,7 @@ export function useFreeUsePlacement() { }) } - if (!hasQaOverride) { + if (!hasQaOverride && isCloudDistribution) { watch( () => remoteConfig.value[FREE_USE_PLACEMENT_FLAG], () => { @@ -90,7 +94,9 @@ export function useFreeUsePlacement() { const telemetry = useTelemetry() if (!telemetry) return if (metadata.action === 'shown') panelStore.reportedFreeUseImpression = true - telemetry.trackAgentFreeUseNotice(metadata) + telemetry.trackAgentFreeUseNotice( + isCloudDistribution ? metadata : { ...metadata, deployment: 'local' } + ) } return {