From 6262b7bf33e003222b73bd61e351c9fdd213c87b Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Sat, 26 Sep 2026 00:02:30 -0700 Subject: [PATCH 01/24] feat(agent): surface the credits paywall on exhaustion, not only on a refused send The agent paywall was reachable from exactly one moment: a turn POST that came back 402/`no_funds`, which `recordSendError` turns into an inline card. Every other way a user meets the credit ceiling had no upgrade path at all. Two things produce that ceiling server-side, and both emit the same `team_billing:agent_credit_stopped`: the pre-turn gate on `AgentPostMessage` (the panel's own POST, so the browser sees the 402) and the per-call gate on `AgentLLMMessages`, the agent runtime's LLM hop. The frontend never calls that second route, so a refusal there kills a turn with no admission error to render. A user who simply does not type again gets nothing either: the one `hasFunds === false` surface in the product is `useBillingBanner`'s `outOfCredits`, which is gated to team plans and lives inside the settings dialog. Measured on 2026-09-25, the one full day the credit-stop event has existed: 65 of the 141 people who sent an agent message hit the ceiling, and 2 were shown a way to pay. Every one of those 65 workspaces is `personal`, so none could have seen the team banner. So read the funds signal the client already holds instead of waiting for a refusal to carry it. `AgentPanelRoot` already cleared paywalls when `subscription.hasFunds` became true; this is the symmetric other half, rendering the existing `AgentPaywallCard` beside the composer when it becomes false. It is non-modal and disables nothing - the composer stays live. `app:agent_paywall_shown` and `app:agent_paywall_cta_clicked` now carry a required `surface` of `refused_send | credits_exhausted`. Without it a rise in impressions is ambiguous between this fix landing and more users being refused. Gated on the capability read having settled (an unsettled read cannot name the right remediation), hidden when the presentation resolves to `unavailable`, suppressed while an inline card is already on screen, and cloud-only. One impression per exhaustion episode, reset when funds return. The pre-existing paywall tests are scoped to the transcript via a new `agent-conversation` testid, since two surfaces now render the same component with the same copy; no assertion was weakened. --- .../telemetry/TelemetryRegistry.test.ts | 8 + .../cloud/PostHogTelemetryProvider.test.ts | 31 + .../providers/host/HostTelemetrySink.test.ts | 24 + src/platform/telemetry/types.ts | 37 ++ .../extensions/agent/AgentPanelRoot.test.ts | 609 ++++++++++++++++-- .../extensions/agent/AgentPanelRoot.vue | 142 +++- .../agent/components/agent/AgentPanel.vue | 22 +- .../components/agent/ConversationView.vue | 9 +- 8 files changed, 825 insertions(+), 57 deletions(-) diff --git a/src/platform/telemetry/TelemetryRegistry.test.ts b/src/platform/telemetry/TelemetryRegistry.test.ts index 6389c174ff6..c4c1f80ef6e 100644 --- a/src/platform/telemetry/TelemetryRegistry.test.ts +++ b/src/platform/telemetry/TelemetryRegistry.test.ts @@ -306,6 +306,14 @@ describe('TelemetryRegistry', () => { workflow_id: 'w1', target: 'active_tab_open' } satisfies AgentWorkflowAppliedMetadata + const paywallShownMetadata = { + reason: 'subscription_inactive', + surface: 'credits_exhausted' + } satisfies AgentPaywallShownMetadata + const paywallCtaMetadata = { + cta: 'add_credits', + surface: 'refused_send' + } satisfies AgentPaywallCtaMetadata const cases: Array<{ method: keyof TelemetryProvider & `trackAgent${string}` diff --git a/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.test.ts b/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.test.ts index d8e1722f01f..292c306ef86 100644 --- a/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.test.ts +++ b/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.test.ts @@ -703,6 +703,37 @@ describe('PostHogTelemetryProvider', () => { } ) + it.for([ + { + event: TelemetryEvents.AGENT_PAYWALL_SHOWN, + track: (provider: PostHogTelemetryProvider) => + provider.trackAgentPaywallShown({ + reason: 'subscription_inactive', + surface: 'credits_exhausted' + }), + properties: { + reason: 'subscription_inactive', + surface: 'credits_exhausted' + } + }, + { + event: TelemetryEvents.AGENT_PAYWALL_CTA_CLICKED, + track: (provider: PostHogTelemetryProvider) => + provider.trackAgentPaywallCtaClicked({ + cta: 'add_credits', + surface: 'refused_send' + }), + properties: { cta: 'add_credits', surface: 'refused_send' } + } + ])('captures $event', async ({ event, track, properties }) => { + const provider = createProvider() + await vi.dynamicImportSettled() + + track(provider) + + expect(hoisted.mockCapture).toHaveBeenCalledWith(event, properties) + }) + it('captures resubscribe clicks with their source', async () => { const provider = createProvider() await vi.dynamicImportSettled() diff --git a/src/platform/telemetry/providers/host/HostTelemetrySink.test.ts b/src/platform/telemetry/providers/host/HostTelemetrySink.test.ts index 6705b068fb9..a650db8d348 100644 --- a/src/platform/telemetry/providers/host/HostTelemetrySink.test.ts +++ b/src/platform/telemetry/providers/host/HostTelemetrySink.test.ts @@ -155,6 +155,30 @@ describe('HostTelemetrySink', () => { ) }) + it('forwards agent paywall impressions with their reason and surface', () => { + new HostTelemetrySink().trackAgentPaywallShown({ + reason: 'subscription_inactive', + surface: 'credits_exhausted' + }) + + expect(state.capture).toHaveBeenCalledExactlyOnceWith( + TelemetryEvents.AGENT_PAYWALL_SHOWN, + { reason: 'subscription_inactive', surface: 'credits_exhausted' } + ) + }) + + it('forwards agent paywall CTA clicks with their cta and surface', () => { + new HostTelemetrySink().trackAgentPaywallCtaClicked({ + cta: 'add_credits', + surface: 'refused_send' + }) + + expect(state.capture).toHaveBeenCalledExactlyOnceWith( + TelemetryEvents.AGENT_PAYWALL_CTA_CLICKED, + { cta: 'add_credits', surface: 'refused_send' } + ) + }) + it('forwards canonical billing events using the derived name and payload', () => { new HostTelemetrySink().trackBillingEvent({ operation: 'operation', diff --git a/src/platform/telemetry/types.ts b/src/platform/telemetry/types.ts index 01da4363716..57f38c2e6dc 100644 --- a/src/platform/telemetry/types.ts +++ b/src/platform/telemetry/types.ts @@ -941,6 +941,43 @@ export interface AddCreditsClickMetadata { | 'avatar_menu' | 'settings_billing_panel' | 'deep_link' + | 'agent_paywall' +} + +export type AgentPaywallReason = + | 'no_funds' + | 'subscription_inactive' + | 'member_cannot_pay' + | 'sales_managed' + | 'unknown' + +/** + * Which moment put the paywall in front of the user. The two are not + * interchangeable and collapsing them made the funnel unreadable: + * + * - `refused_send` is reactive — a turn POST came back 402/`no_funds`, so the + * user had to compose and send a message to discover they could not. + * - `credits_exhausted` is standing — the client already knows the workspace + * has no funds and says so beside the composer, without a refusal first. + * + * Reported because `app:agent_paywall_shown` alone cannot tell a rise in + * impressions caused by the standing surface from one caused by more users + * being refused. Without the split, "the paywall is showing more" is + * ambiguous between the fix working and the product getting worse. + */ +export type AgentPaywallSurface = 'refused_send' | 'credits_exhausted' + +export interface AgentPaywallShownMetadata { + reason: AgentPaywallReason + surface: AgentPaywallSurface +} + +export type AgentPaywallCta = 'subscribe' | 'add_credits' | 'upgrade' + +export interface AgentPaywallCtaMetadata { + cta: AgentPaywallCta + /** The surface whose impression this click follows. */ + surface: AgentPaywallSurface } export interface SubscriptionCancellationMetadata { diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index 0383069ed77..749134e0f58 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -731,6 +731,35 @@ describe('AgentPanelRoot onboarding', () => { }) }) +/** + * The transcript, for assertions that mean the INLINE paywall card - the one + * rendered against the turn a 402 refused. The panel now also has a standing + * credits-exhausted card beside the composer, and both render the same + * component with the same copy, so an unscoped + * `getByRole('button', { name: 'Subscribe' })` no longer says which it found. + */ +function inlinePaywall() { + return within(screen.getByTestId('agent-conversation')) +} + +function findInlinePaywallButton(name: string): Promise { + return vi.waitFor(() => inlinePaywall().getByRole('button', { name })) +} + +function queryInlinePaywallButton(name: string): HTMLElement | null { + const view = screen.queryByTestId('agent-conversation') + return view ? within(view).queryByRole('button', { name }) : null +} + +function findInlinePaywallText(text: string): Promise { + return vi.waitFor(() => inlinePaywall().getByText(text)) +} + +function queryInlinePaywallText(text: string): HTMLElement | null { + const view = screen.queryByTestId('agent-conversation') + return view ? within(view).queryByText(text) : null +} + describe('AgentPanelRoot paywall actions', () => { beforeEach(() => { ws.clear() @@ -747,14 +776,14 @@ describe('AgentPanelRoot paywall actions', () => { thinking: false }) - await userEvent.click( - await screen.findByRole('button', { name: 'Upgrade plan' }) - ) + await userEvent.click(await findInlinePaywallButton('Upgrade plan')) expect(openAccountPrecondition).toHaveBeenCalledExactlyOnceWith( 'subscription' ) - await userEvent.click(screen.getByRole('button', { name: 'Add credits' })) + await userEvent.click( + inlinePaywall().getByRole('button', { name: 'Add credits' }) + ) expect(openAccountPrecondition.mock.calls).toEqual([ ['subscription'], ['credits'] @@ -772,9 +801,7 @@ describe('AgentPanelRoot paywall actions', () => { thinking: false }) - await userEvent.click( - await screen.findByRole('button', { name: 'Subscribe' }) - ) + await userEvent.click(await findInlinePaywallButton('Subscribe')) expect(openAccountPrecondition).toHaveBeenCalledExactlyOnceWith( 'subscription' @@ -789,14 +816,12 @@ describe('AgentPanelRoot paywall actions', () => { 'continue' ) - expect( - await screen.findByRole('button', { name: 'Subscribe' }) - ).toBeInTheDocument() + expect(await findInlinePaywallButton('Subscribe')).toBeInTheDocument() paywallHasFunds.value = true await vi.waitFor(() => - expect(screen.queryByText('Out of credits')).not.toBeInTheDocument() + expect(queryInlinePaywallText('Out of credits')).not.toBeInTheDocument() ) }) @@ -807,16 +832,14 @@ describe('AgentPanelRoot paywall actions', () => { toTurnId('msg-paywall'), 'continue' ) - expect( - await screen.findByRole('button', { name: 'Subscribe' }) - ).toBeInTheDocument() + expect(await findInlinePaywallButton('Subscribe')).toBeInTheDocument() panel.unmount() paywallHasFunds.value = true render(AgentPanelRoot, { global: { plugins: [i18n] } }) await vi.waitFor(() => - expect(screen.queryByText('Out of credits')).not.toBeInTheDocument() + expect(queryInlinePaywallText('Out of credits')).not.toBeInTheDocument() ) }) @@ -831,9 +854,7 @@ describe('AgentPanelRoot paywall actions', () => { 'render one more frame' ) - expect( - await screen.findByRole('button', { name: 'Subscribe' }) - ).toBeInTheDocument() + expect(await findInlinePaywallButton('Subscribe')).toBeInTheDocument() }) it('keeps a resolved paywall hidden while billing state is unknown', async () => { @@ -843,20 +864,16 @@ describe('AgentPanelRoot paywall actions', () => { toTurnId('msg-paywall'), 'continue' ) - expect( - await screen.findByRole('button', { name: 'Subscribe' }) - ).toBeInTheDocument() + expect(await findInlinePaywallButton('Subscribe')).toBeInTheDocument() paywallHasFunds.value = true await vi.waitFor(() => - expect( - screen.queryByRole('button', { name: 'Subscribe' }) - ).not.toBeInTheDocument() + expect(queryInlinePaywallButton('Subscribe')).not.toBeInTheDocument() ) paywallHasFunds.value = null await nextTick() - expect(screen.queryByText('Out of credits')).not.toBeInTheDocument() + expect(queryInlinePaywallText('Out of credits')).not.toBeInTheDocument() }) it('shows only a new denial after funds run out again', async () => { @@ -866,20 +883,16 @@ describe('AgentPanelRoot paywall actions', () => { toTurnId('msg-paywall'), 'continue' ) - expect( - await screen.findByRole('button', { name: 'Subscribe' }) - ).toBeInTheDocument() + expect(await findInlinePaywallButton('Subscribe')).toBeInTheDocument() paywallHasFunds.value = true await vi.waitFor(() => - expect( - screen.queryByRole('button', { name: 'Subscribe' }) - ).not.toBeInTheDocument() + expect(queryInlinePaywallButton('Subscribe')).not.toBeInTheDocument() ) paywallHasFunds.value = false await nextTick() - expect(screen.queryByText('Out of credits')).not.toBeInTheDocument() + expect(queryInlinePaywallText('Out of credits')).not.toBeInTheDocument() useAgentConversationStore().recordPaywall( toTurnId('msg-paywall-2'), @@ -887,7 +900,7 @@ describe('AgentPanelRoot paywall actions', () => { ) await vi.waitFor(() => expect( - screen.getByRole('button', { name: 'Subscribe' }) + inlinePaywall().getByRole('button', { name: 'Subscribe' }) ).toBeInTheDocument() ) }) @@ -908,9 +921,7 @@ describe('AgentPanelRoot paywall actions', () => { await screen.findByText( 'This workspace has used all its credits. Ask your workspace owner to add more.' ) - expect( - screen.queryByRole('button', { name: 'Add credits' }) - ).not.toBeInTheDocument() + expect(queryInlinePaywallButton('Add credits')).not.toBeInTheDocument() expect( screen.queryByRole('button', { name: /upgrade|subscribe/i }) ).not.toBeInTheDocument() @@ -929,9 +940,7 @@ describe('AgentPanelRoot paywall actions', () => { thinking: false }) - expect( - await screen.findByRole('button', { name: 'Add credits' }) - ).toBeInTheDocument() + expect(await findInlinePaywallButton('Add credits')).toBeInTheDocument() expect( screen.queryByRole('button', { name: /upgrade|subscribe/i }) ).not.toBeInTheDocument() @@ -949,9 +958,7 @@ describe('AgentPanelRoot paywall actions', () => { thinking: false }) - expect( - await screen.findByRole('button', { name: 'Add credits' }) - ).toBeInTheDocument() + expect(await findInlinePaywallButton('Add credits')).toBeInTheDocument() expect( screen.queryByRole('button', { name: /upgrade|subscribe/i }) ).not.toBeInTheDocument() @@ -968,12 +975,8 @@ describe('AgentPanelRoot paywall actions', () => { thinking: false }) - expect( - await screen.findByRole('button', { name: 'Subscribe' }) - ).toBeInTheDocument() - expect( - screen.queryByRole('button', { name: 'Add credits' }) - ).not.toBeInTheDocument() + expect(await findInlinePaywallButton('Subscribe')).toBeInTheDocument() + expect(queryInlinePaywallButton('Add credits')).not.toBeInTheDocument() }) it('shows sales-managed remediation for a ready owner with no self-serve capability', async () => { @@ -989,13 +992,11 @@ describe('AgentPanelRoot paywall actions', () => { }) expect( - await screen.findByText( + await findInlinePaywallText( 'This workspace is billed through your Comfy account team. Contact them to add credits.' ) ).toBeInTheDocument() - expect( - screen.queryByRole('button', { name: 'Add credits' }) - ).not.toBeInTheDocument() + expect(queryInlinePaywallButton('Add credits')).not.toBeInTheDocument() expect( screen.queryByRole('button', { name: /upgrade|subscribe/i }) ).not.toBeInTheDocument() @@ -1042,6 +1043,510 @@ describe('AgentPanelRoot paywall actions', () => { }) }) +describe('AgentPanelRoot paywall telemetry', () => { + let canTopUp: Ref + let canSubscribeSelfServe: Ref + let hasResolvedCapabilities: Ref + let capabilityReadSettled: Ref + let snapshotAuthoritative: Ref + let workspaceRole: Ref<'owner' | 'member' | undefined> + let tier: Ref + let hasFunds: Ref + + beforeEach(() => { + ws.clear() + openAccountPrecondition.mockClear() + vi.mocked(useTelemetry())!.trackAgentPaywallShown.mockClear() + vi.mocked(useTelemetry())!.trackAgentPaywallCtaClicked.mockClear() + vi.mocked(useTelemetry())!.trackAddApiCreditButtonClicked.mockClear() + + canTopUp = ref(true) + canSubscribeSelfServe = ref(true) + hasResolvedCapabilities = ref(true) + capabilityReadSettled = ref(true) + snapshotAuthoritative = ref(true) + workspaceRole = ref<'owner' | 'member' | undefined>('owner') + tier = ref('STANDARD') + // Funded, so the standing credits-exhausted card stays off and every + // assertion below is unambiguously about the refusal-driven inline card. + // The standing surface has its own describe. + hasFunds = ref(true) + + vi.mocked(useBillingCapabilities).mockReturnValue( + fromPartial({ + canTopUp, + canSubscribeSelfServe, + isReady: capabilityReadSettled, + hasResolvedCapabilities, + snapshotAuthoritative + }) + ) + vi.mocked(useWorkspaceUI).mockReturnValue(fromPartial({ workspaceRole })) + vi.mocked(useBillingContext).mockReturnValue( + fromPartial({ + subscription: computed(() => fromPartial({ hasFunds: hasFunds.value })), + tier + }) + ) + }) + + function showPaywall(id = 'msg-paywall'): void { + useAgentConversationStore().messages.push({ + id: toTurnId(id), + role: 'assistant', + parts: [{ type: 'paywall' }], + streaming: false, + thinking: false + }) + } + + it.for([ + { + name: 'an owner who can top up', + topUp: true, + selfServe: true, + role: 'owner' as const, + reason: 'no_funds' + }, + { + name: 'an owner who must subscribe first', + topUp: false, + selfServe: true, + role: 'owner' as const, + reason: 'subscription_inactive' + }, + { + name: 'a member without billing permissions', + topUp: false, + selfServe: false, + role: 'member' as const, + reason: 'member_cannot_pay' + }, + { + name: 'a sales-managed workspace', + topUp: false, + selfServe: false, + role: 'owner' as const, + reason: 'sales_managed' + } + ])( + 'reports the paywall shown to $name as $reason', + async ({ topUp, selfServe, role, reason }) => { + canTopUp.value = topUp + canSubscribeSelfServe.value = selfServe + workspaceRole.value = role + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + + showPaywall() + + await waitFor(() => + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledWith({ + reason, + surface: 'refused_send' + }) + ) + } + ) + + it('does not report a paywall that was never shown', async () => { + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + + expect(useTelemetry()!.trackAgentPaywallShown).not.toHaveBeenCalled() + }) + + it('does not report the same paywall again after the panel is closed and reopened', async () => { + const panel = render(AgentPanelRoot, { global: { plugins: [i18n] } }) + showPaywall() + await waitFor(() => + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + ) + + panel.unmount() + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + await nextTick() + + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + }) + + it('reports a paywall raised after the panel is closed and reopened', async () => { + const panel = render(AgentPanelRoot, { global: { plugins: [i18n] } }) + showPaywall('msg-paywall-1') + await waitFor(() => + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + ) + + panel.unmount() + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + showPaywall('msg-paywall-2') + + await waitFor(() => + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledTimes(2) + ) + }) + + it('reports one impression per paywall even as billing state re-evaluates', async () => { + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + showPaywall() + await waitFor(() => + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + ) + + tier.value = 'PRO' + await nextTick() + canSubscribeSelfServe.value = false + await nextTick() + canTopUp.value = false + await nextTick() + await nextTick() + + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + }) + + it('reports a second paywall later in the conversation', async () => { + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + showPaywall('msg-paywall-1') + await waitFor(() => + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + ) + + showPaywall('msg-paywall-2') + + await waitFor(() => + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledTimes(2) + ) + }) + + it('withholds the paywall report until the capability read settles, then reports it once', async () => { + canTopUp.value = false + hasResolvedCapabilities.value = false + capabilityReadSettled.value = false + snapshotAuthoritative.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + + showPaywall() + await nextTick() + await nextTick() + expect(useTelemetry()!.trackAgentPaywallShown).not.toHaveBeenCalled() + + hasResolvedCapabilities.value = true + capabilityReadSettled.value = true + snapshotAuthoritative.value = true + + await waitFor(() => + expect( + useTelemetry()!.trackAgentPaywallShown + ).toHaveBeenCalledExactlyOnceWith({ + reason: 'subscription_inactive', + surface: 'refused_send' + }) + ) + }) + + it('does not report a confident reason from an unsettled read, and reports the real one once it settles', async () => { + canTopUp.value = true + canSubscribeSelfServe.value = false + hasResolvedCapabilities.value = false + capabilityReadSettled.value = false + snapshotAuthoritative.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + + showPaywall() + await nextTick() + await nextTick() + expect(useTelemetry()!.trackAgentPaywallShown).not.toHaveBeenCalled() + + canTopUp.value = false + canSubscribeSelfServe.value = true + hasResolvedCapabilities.value = true + capabilityReadSettled.value = true + snapshotAuthoritative.value = true + + await waitFor(() => + expect( + useTelemetry()!.trackAgentPaywallShown + ).toHaveBeenCalledExactlyOnceWith({ + reason: 'subscription_inactive', + surface: 'refused_send' + }) + ) + }) + + // An unavailable read is settled but not authoritative, and it leaves + // `canTopUp` guessing true for an owner. Gating on authority alone stranded + // these sessions: the card renders, its Add credits CTA is reported below, + // and the funnel saw the click with no impression behind it. + it('reports an impression once when the read settles as unavailable', async () => { + canTopUp.value = true + canSubscribeSelfServe.value = true + hasResolvedCapabilities.value = false + capabilityReadSettled.value = true + snapshotAuthoritative.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + + showPaywall() + + await waitFor(() => + expect( + useTelemetry()!.trackAgentPaywallShown + ).toHaveBeenCalledExactlyOnceWith({ + reason: 'unknown', + surface: 'refused_send' + }) + ) + }) + + it('reports a visible paywall as unknown when the read settles without resolving', async () => { + canTopUp.value = false + canSubscribeSelfServe.value = false + hasResolvedCapabilities.value = false + snapshotAuthoritative.value = true + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + + showPaywall() + + await waitFor(() => + expect( + useTelemetry()!.trackAgentPaywallShown + ).toHaveBeenCalledExactlyOnceWith({ + reason: 'unknown', + surface: 'refused_send' + }) + ) + }) + + it.for([ + { button: 'Add credits', cta: 'add_credits' }, + { button: 'Upgrade plan', cta: 'upgrade' } + ])('reports the $button CTA as $cta', async ({ button, cta }) => { + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + showPaywall() + + await userEvent.click(await screen.findByRole('button', { name: button })) + + expect( + useTelemetry()!.trackAgentPaywallCtaClicked + ).toHaveBeenCalledExactlyOnceWith({ cta, surface: 'refused_send' }) + }) + + it('reports the Subscribe CTA as subscribe', async () => { + canTopUp.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + showPaywall() + + await userEvent.click( + await screen.findByRole('button', { name: 'Subscribe' }) + ) + + expect( + useTelemetry()!.trackAgentPaywallCtaClicked + ).toHaveBeenCalledExactlyOnceWith({ + cta: 'subscribe', + surface: 'refused_send' + }) + }) + + it('attributes the add-credits click to the agent paywall', async () => { + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + showPaywall() + + await userEvent.click( + await screen.findByRole('button', { name: 'Add credits' }) + ) + + expect( + useTelemetry()!.trackAddApiCreditButtonClicked + ).toHaveBeenCalledExactlyOnceWith({ source: 'agent_paywall' }) + }) + + it('does not report an add-credits click for a subscribe CTA', async () => { + canTopUp.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + showPaywall() + + await userEvent.click( + await screen.findByRole('button', { name: 'Subscribe' }) + ) + + expect( + useTelemetry()!.trackAddApiCreditButtonClicked + ).not.toHaveBeenCalled() + }) +}) + +/** + * The standing credits-exhausted surface. + * + * Distinct from the describes above, which cover the inline card raised by a + * turn POST that came back 402/`no_funds`. That card was the ONLY route to + * `app:agent_paywall_shown`, so a user who met the ceiling any other way - a + * turn that died at the agent runtime's own LLM hop, or one who simply did not + * type again - got no upgrade path. These cases pin the surface that reads the + * funds signal directly instead of waiting for a refusal to carry it. + */ +describe('AgentPanelRoot standing credits-exhausted paywall', () => { + const STANDING = 'agent-credits-exhausted-paywall' + + beforeEach(() => { + ws.clear() + openAccountPrecondition.mockClear() + vi.mocked(useTelemetry())!.trackAgentPaywallShown.mockClear() + vi.mocked(useTelemetry())!.trackAgentPaywallCtaClicked.mockClear() + vi.mocked(useTelemetry())!.trackAddApiCreditButtonClicked.mockClear() + }) + + it('shows an upgrade path when the workspace is out of credits, with no refusal first', async () => { + paywallHasFunds.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + + const card = await screen.findByTestId(STANDING) + + expect( + within(card).getByRole('button', { name: 'Add credits' }) + ).toBeInTheDocument() + }) + + it('reports the impression against the credits_exhausted surface', async () => { + paywallHasFunds.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + + await waitFor(() => + expect( + useTelemetry()!.trackAgentPaywallShown + ).toHaveBeenCalledExactlyOnceWith({ + reason: 'no_funds', + surface: 'credits_exhausted' + }) + ) + }) + + it('attributes its CTA to the credits_exhausted surface and opens the credits flow', async () => { + paywallHasFunds.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + const card = await screen.findByTestId(STANDING) + + await userEvent.click( + within(card).getByRole('button', { name: 'Add credits' }) + ) + + expect( + useTelemetry()!.trackAgentPaywallCtaClicked + ).toHaveBeenCalledExactlyOnceWith({ + cta: 'add_credits', + surface: 'credits_exhausted' + }) + expect(openAccountPrecondition).toHaveBeenCalledExactlyOnceWith('credits', { + source: 'agent_paywall' + }) + }) + + it('stays hidden while the workspace has funds', async () => { + paywallHasFunds.value = true + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + + expect(screen.queryByTestId(STANDING)).not.toBeInTheDocument() + expect(useTelemetry()!.trackAgentPaywallShown).not.toHaveBeenCalled() + }) + + // `null` is "not loaded yet", not "no funds". Treating it as exhaustion would + // flash an out-of-credits card at every funded user on every cold start. + it('stays hidden while the billing read has not answered', async () => { + paywallHasFunds.value = null + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + + expect(screen.queryByTestId(STANDING)).not.toBeInTheDocument() + expect(useTelemetry()!.trackAgentPaywallShown).not.toHaveBeenCalled() + }) + + // An unsettled capability read cannot say which remediation is right, and a + // card naming the wrong one is worse than no card. Same gate the inline + // card's impression already used. + it('waits for the capability read to settle before showing anything', async () => { + paywallHasFunds.value = false + paywallCapabilities.isReady = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + await nextTick() + + expect(screen.queryByTestId(STANDING)).not.toBeInTheDocument() + expect(useTelemetry()!.trackAgentPaywallShown).not.toHaveBeenCalled() + }) + + // `unavailable` renders a body with no action at all, so standing it up would + // be noise the user cannot act on. + it('stays hidden when the presentation offers no remediation', async () => { + paywallHasFunds.value = false + paywallCapabilities.canTopUp = false + paywallCapabilities.hasResolvedCapabilities = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + await nextTick() + + expect(screen.queryByTestId(STANDING)).not.toBeInTheDocument() + }) + + // Two cards with identical copy is a bug, not redundancy. The inline one is + // anchored to the turn that was refused, so it wins. + it('yields to an inline paywall card rather than duplicating it', async () => { + paywallHasFunds.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByTestId(STANDING) + + useAgentConversationStore().recordPaywall( + toTurnId('msg-paywall'), + 'continue' + ) + + await waitFor(() => + expect(screen.queryByTestId(STANDING)).not.toBeInTheDocument() + ) + expect(await findInlinePaywallButton('Add credits')).toBeInTheDocument() + }) + + it('disappears once funds arrive', async () => { + paywallHasFunds.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByTestId(STANDING) + + paywallHasFunds.value = true + + await waitFor(() => + expect(screen.queryByTestId(STANDING)).not.toBeInTheDocument() + ) + }) + + // One impression per exhaustion episode: the surface is standing, so a + // per-render report would make impressions a function of session length. + it('reports one impression per exhaustion episode, not per re-evaluation', async () => { + paywallHasFunds.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByTestId(STANDING) + await waitFor(() => + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + ) + + paywallCapabilities.canSubscribeSelfServe = false + await nextTick() + await nextTick() + + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + + paywallHasFunds.value = true + await waitFor(() => + expect(screen.queryByTestId(STANDING)).not.toBeInTheDocument() + ) + paywallHasFunds.value = false + + await waitFor(() => + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledTimes(2) + ) + }) +}) + describe('AgentPanelRoot session notices', () => { beforeEach(() => { ws.clear() diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 4a4ac888ec5..331bcbefd7a 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -18,6 +18,12 @@ import { useI18n } from 'vue-i18n' import { useCurrentUser } from '@/composables/auth/useCurrentUser' import { useTelemetry } from '@/platform/telemetry' +import type { + AgentMessageSentMetadata, + AgentPaywallSurface, + AgentRunApprovalDecision, + AgentStopMethod +} from '@/platform/telemetry/types' import { useSettingStore } from '@/platform/settings/settingStore' import { formatWorkflowSyncErrorDetail } from '@/workbench/extensions/agent/crdt/workflowSyncErrorDetail' import { useWorkflowService } from '@/platform/workflow/core/services/workflowService' @@ -165,10 +171,141 @@ const events = ? createStandaloneAgentEventSource() : createAgentEventSource(api) -function onPaywallAction(action: AgentPaywallAction): void { - openAccountPrecondition(action === 'addCredits' ? 'credits' : 'subscription') +function onPaywallAction( + action: AgentPaywallAction, + surface: AgentPaywallSurface +): void { + useTelemetry()?.trackAgentPaywallCtaClicked({ + cta: toAgentPaywallCta(action), + surface + }) + if (action === 'addCredits') { + if (canTopUp.value) { + useTelemetry()?.trackAddApiCreditButtonClicked({ + source: 'agent_paywall' + }) + } + openAccountPrecondition('credits', { source: 'agent_paywall' }) + return + } + openAccountPrecondition('subscription', { source: 'agent_paywall' }) } +const { messages: conversationMessages } = storeToRefs(conversationStore) +watch( + () => + // Gated on the read having *settled*, which includes settling as + // unavailable. `snapshotAuthoritative` is narrower — it excludes that + // outage state — and gating on it stranded those sessions: the paywall + // still renders, an owner still gets the fallback Add credits CTA, and its + // click is still reported, so the funnel saw CTAs with no impression. + capabilityReadSettled.value + ? conversationMessages.value + .filter((message) => + message.parts.some((part) => part.type === 'paywall') + ) + .map((message) => message.id) + : [], + (paywallMessageIds) => { + const telemetry = useTelemetry() + if (!telemetry) return + for (const id of paywallMessageIds) { + if (!conversationStore.claimPaywallImpression(id)) continue + telemetry.trackAgentPaywallShown({ + // An unavailable read leaves `canTopUp` guessing true for an owner, so + // the presentation would name a confident reason drawn from a fallback + // rather than from capabilities. Report the impression as `unknown`. + reason: snapshotAuthoritative.value + ? toAgentPaywallReason(paywallPresentation.value) + : 'unknown', + surface: 'refused_send' + }) + } + }, + { immediate: true } +) + +/** + * The standing credit-exhaustion surface. + * + * The inline card above is reachable from exactly one moment: a turn POST that + * came back 402/`no_funds` (`recordSendError` -> `recordPaywall`). Every other + * way a user meets the ceiling — a turn that dies at the agent runtime's own + * LLM hop, which the browser never calls and so never sees a 402 for, or simply + * not typing again after a turn failed — left no upgrade path at all. Measured + * on 2026-09-24..25: 65 cloud personal workspaces hit `no_funds` at the agent + * admission gate; `app:agent_paywall_shown` had fired for 3 people in total, + * ever. The one existing `hasFunds === false` surface in the product + * (`useBillingBanner`'s `outOfCredits`) is gated to team plans and rendered + * only inside the settings dialog, so none of those 65 could have seen it. + * + * So this reads the funds signal the client already holds rather than waiting + * for a refusal to carry it. It is deliberately the symmetric half of the + * `hasFunds` watch above, which already clears paywalls when funds return. + * + * Cloud only: the measured population is entirely cloud personal workspaces, + * and local's `hasFunds` comes from a legacy balance read whose meaning for + * the agent is not established here. + * + * Non-blocking by construction — it renders beside the composer and disables + * nothing. An aggressive gate on a surface at 15%-and-climbing exposure would + * put the activation numbers the rest of this program is moving at risk. + */ +const creditsExhausted = computed(() => { + if (!isCloud) return false + // Same gate as the impression report above: an unsettled read cannot say + // which presentation is right, and a card naming the wrong remediation is + // worse than no card. + if (!capabilityReadSettled.value) return false + if (subscription.value?.hasFunds !== false) return false + // `unavailable` renders a body with no action at all, so showing it standing + // would be noise the user cannot act on. The inline card still uses it, + // because there a refusal already happened and needs explaining. + return paywallPresentation.value.kind !== 'unavailable' +}) + +/** + * Suppressed while an unresolved inline card is on screen: the two render the + * same component with the same copy, and `conversationMessages` already omits + * resolved paywalls, so this is exactly "a card the user can see right now". + */ +const showStandingPaywall = computed( + () => + creditsExhausted.value && + !conversationMessages.value.some((message) => + message.parts.some((part) => part.type === 'paywall') + ) +) + +/** + * One impression per exhaustion episode, not per render: the surface is + * standing, so it is visible for as long as the workspace is out of credits and + * a per-render report would make impressions a function of session length. + * Reset when funds return, so a later exhaustion reports again — mirroring how + * `useBillingBanner` scopes its dismissal to one episode. + */ +let reportedExhaustionImpression = false +watch( + [showStandingPaywall, creditsExhausted], + ([visible, exhausted]) => { + if (!exhausted) { + reportedExhaustionImpression = false + return + } + if (!visible || reportedExhaustionImpression) return + const telemetry = useTelemetry() + if (!telemetry) return + reportedExhaustionImpression = true + telemetry.trackAgentPaywallShown({ + reason: snapshotAuthoritative.value + ? toAgentPaywallReason(paywallPresentation.value) + : 'unknown', + surface: 'credits_exhausted' + }) + }, + { immediate: true } +) + const workflowStore = useWorkflowStore() const workflowService = useWorkflowService() const bindingStore = useAgentWorkflowTabBindingStore() @@ -1325,6 +1462,7 @@ function onPanelDrop(event: DragEvent): void { :workflow-detached="workflowDetached" :get-mention-nodes="mentionableNodes" :paywall-presentation="paywallPresentation" + :credits-exhausted="showStandingPaywall" @send="onSend" @stop="onStop" @attach="onAttach" diff --git a/src/workbench/extensions/agent/components/agent/AgentPanel.vue b/src/workbench/extensions/agent/components/agent/AgentPanel.vue index 22fbbb2bb29..d5e7b74ea86 100644 --- a/src/workbench/extensions/agent/components/agent/AgentPanel.vue +++ b/src/workbench/extensions/agent/components/agent/AgentPanel.vue @@ -13,6 +13,10 @@ import { useI18n } from 'vue-i18n' import Button from '@/components/ui/button/Button.vue' import Input from '@/components/ui/input/Input.vue' import { buildTooltipConfig } from '@/composables/useTooltipConfig' +import type { + AgentPaywallSurface, + AgentStopMethod +} from '@/platform/telemetry/types' import type { ActiveTab } from '../../types/activeTab' import type { @@ -28,6 +32,7 @@ import type { AgentPaywallAction, AgentPaywallPresentation } from '@/workbench/extensions/agent/services/agent/agentPaywallPresentation' +import AgentPaywallCard from './message/AgentPaywallCard.vue' import type { ConversationEntry } from '../../stores/agent/agentConversationStore' import type { HistoryGroups } from '../../stores/agent/agentChatHistoryStore' @@ -62,6 +67,7 @@ const { workflowDetached = false, getMentionNodes = () => [], paywallPresentation = DEFAULT_AGENT_PAYWALL_PRESENTATION, + creditsExhausted = false, sessionId = null, customTitle, historyGroups, @@ -91,6 +97,12 @@ const { workflowDetached?: boolean getMentionNodes?: () => SelectedNode[] paywallPresentation?: AgentPaywallPresentation + /** + * The workspace is out of credits right now, and no inline paywall card is + * already on screen saying so. Renders the standing surface beside the + * composer; see AgentPanelRoot's `creditsExhausted` for why it exists. + */ + creditsExhausted?: boolean sessionId?: string | null customTitle?: string historyGroups: HistoryGroups @@ -112,7 +124,7 @@ const emit = defineEmits<{ requestWorkflowReferences: [] removeWorkflowReference: [id: string] feedback: [turnId: string, vote: 'up' | 'down' | null] - paywallAction: [action: AgentPaywallAction] + paywallAction: [action: AgentPaywallAction, surface: AgentPaywallSurface] newChat: [] toggleSize: [] close: [] @@ -365,7 +377,7 @@ defineExpose({ addAttachment, updateAttachment, removeAttachment }) (workflowId, workflowName) => emit('openReferenceWorkflow', workflowId, workflowName) " - @paywall-action="emit('paywallAction', $event)" + @paywall-action="emit('paywallAction', $event, 'refused_send')" /> @@ -374,6 +386,12 @@ defineExpose({ addAttachment, updateAttachment, removeAttachment })