Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 84 additions & 2 deletions browser_tests/tests/agent/agentFreeUsePlacement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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',
Expand Down Expand Up @@ -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()
})
}
)
2 changes: 2 additions & 0 deletions src/platform/billing/composables/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
24 changes: 14 additions & 10 deletions src/platform/billing/composables/useBillingContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ describe('useBillingContext', () => {
endDate: null,
isCancelled: false,
hasFunds: true,
agentHasFunds: true
agentHasFunds: true,
agentScopedHasFunds: undefined,
agentScopedHasFundsAuthoritative: false
})
})

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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()
Expand All @@ -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()
})

Expand All @@ -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()
Expand Down Expand Up @@ -554,7 +556,9 @@ describe('useBillingContext', () => {

await refreshBilling(scope)

expect(useSubscription().fetchStatus).toHaveBeenCalledTimes(status)
expect(useSubscription().fetchStatusDirect).toHaveBeenCalledTimes(
status
)
expect(useAuthStore().fetchBalance).toHaveBeenCalledTimes(balance)
}
)
Expand Down Expand Up @@ -630,15 +634,15 @@ 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()

loaded.value = true
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()
})
Expand All @@ -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 () => {
Expand Down
54 changes: 53 additions & 1 deletion src/platform/billing/composables/useLegacyBilling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BillingStatusResponse | null>(
(_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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
49 changes: 38 additions & 11 deletions src/platform/billing/composables/useLegacyBilling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -53,7 +59,6 @@ export function useLegacyBilling(): BillingState & BillingActions {
subscriptionDuration,
subscriptionStatus: legacySubscriptionStatus,
isCancelled,
fetchStatus: legacyFetchStatus,
fetchStatusDirect: legacyFetchStatusDirect,
manageSubscription: legacyManageSubscription,
subscribeDirect: legacySubscribeDirect
Expand All @@ -65,6 +70,8 @@ export function useLegacyBilling(): BillingState & BillingActions {
const isInitialized = ref(false)
const isLoading = ref(false)
const error = ref<string | null>(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
Expand Down Expand Up @@ -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)
}
})

Expand Down Expand Up @@ -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) {
Expand All @@ -195,20 +207,35 @@ export function useLegacyBilling(): BillingState & BillingActions {
}
}

async function fetchStatus(): Promise<void> {
function reportStatusReadFailure(result: StatusReadResult): void {
if (!result.ok) authActions.reportError(result.cause)
}

async function readStatus(): Promise<StatusReadResult> {
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<void> {
const result = await readStatus()
if (!result.ok) throw result.cause
}

async function fetchBalance(): Promise<void> {
isLoading.value = true
error.value = null
Expand Down
Loading
Loading