-
Notifications
You must be signed in to change notification settings - Fork 35
feat(tasks): show upgrade prompt for over-limit cloud task creation #2523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
apps/code/src/renderer/features/billing/preflightCloudUsage.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import type { UsageOutput } from "@main/services/llm-gateway/schemas"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const refresh = vi.fn(); | ||
| const getLatest = vi.fn(); | ||
| const track = vi.fn(); | ||
|
|
||
| vi.mock("@renderer/trpc/client", () => ({ | ||
| trpcClient: { | ||
| usageMonitor: { | ||
| refresh: { mutate: () => refresh() }, | ||
| getLatest: { query: () => getLatest() }, | ||
| }, | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock("@utils/analytics", () => ({ | ||
| track: (...args: unknown[]) => track(...args), | ||
| })); | ||
|
|
||
| import { ANALYTICS_EVENTS } from "@shared/types/analytics"; | ||
| import { assertCloudUsageAvailable } from "./preflightCloudUsage"; | ||
| import { useUsageLimitStore } from "./stores/usageLimitStore"; | ||
|
|
||
| function makeUsage( | ||
| overrides: Partial<{ | ||
| sustained: boolean; | ||
| burst: boolean; | ||
| isRateLimited: boolean; | ||
| isPro: boolean; | ||
| }> = {}, | ||
| ): UsageOutput { | ||
| return { | ||
| product: "posthog_code", | ||
| user_id: 1, | ||
| sustained: { | ||
| used_percent: 50, | ||
| reset_at: "2026-05-01T13:00:00.000Z", | ||
| exceeded: overrides.sustained ?? false, | ||
| }, | ||
| burst: { | ||
| used_percent: 30, | ||
| reset_at: "2026-05-01T12:10:00.000Z", | ||
| exceeded: overrides.burst ?? false, | ||
| }, | ||
| is_rate_limited: overrides.isRateLimited ?? false, | ||
| is_pro: overrides.isPro ?? false, | ||
| }; | ||
| } | ||
|
|
||
| interface Case { | ||
| name: string; | ||
| arrange: () => void; | ||
| available: boolean; | ||
| modal: { | ||
| isOpen: boolean; | ||
| bucket?: "burst" | "sustained" | null; | ||
| resetAt?: string | null; | ||
| isPro?: boolean | null; | ||
| }; | ||
| trackPayload?: { bucket: "burst" | "sustained" | null; is_pro: boolean }; | ||
| } | ||
|
|
||
| const cases: Case[] = [ | ||
| { | ||
| name: "allows creation and shows no modal when under limit", | ||
| arrange: () => refresh.mockResolvedValue(makeUsage()), | ||
| available: true, | ||
| modal: { isOpen: false }, | ||
| }, | ||
| { | ||
| name: "blocks and shows the burst modal when the daily limit is exceeded", | ||
| arrange: () => | ||
| refresh.mockResolvedValue( | ||
| makeUsage({ burst: true, isRateLimited: true, isPro: true }), | ||
| ), | ||
| available: false, | ||
| modal: { | ||
| isOpen: true, | ||
| bucket: "burst", | ||
| resetAt: "2026-05-01T12:10:00.000Z", | ||
| isPro: true, | ||
| }, | ||
| trackPayload: { bucket: "burst", is_pro: true }, | ||
| }, | ||
| { | ||
| name: "falls back to the latest snapshot when refresh fails", | ||
| arrange: () => { | ||
| refresh.mockRejectedValue(new Error("network")); | ||
| getLatest.mockResolvedValue(makeUsage({ sustained: true })); | ||
| }, | ||
| available: false, | ||
| modal: { isOpen: true, bucket: "sustained" }, | ||
| trackPayload: { bucket: "sustained", is_pro: false }, | ||
| }, | ||
| { | ||
| name: "falls back to the monthly bucket when only is_rate_limited is set", | ||
| arrange: () => | ||
| refresh.mockResolvedValue(makeUsage({ isRateLimited: true })), | ||
| available: false, | ||
| modal: { | ||
| isOpen: true, | ||
| bucket: "sustained", | ||
| resetAt: "2026-05-01T13:00:00.000Z", | ||
| }, | ||
| trackPayload: { bucket: "sustained", is_pro: false }, | ||
| }, | ||
| { | ||
| name: "fails open (allows creation) when usage cannot be fetched", | ||
| arrange: () => { | ||
| refresh.mockRejectedValue(new Error("network")); | ||
| getLatest.mockRejectedValue(new Error("network")); | ||
| }, | ||
| available: true, | ||
| modal: { isOpen: false }, | ||
| }, | ||
| ]; | ||
|
|
||
| describe("assertCloudUsageAvailable", () => { | ||
| beforeEach(() => { | ||
| refresh.mockReset(); | ||
| getLatest.mockReset(); | ||
| track.mockReset(); | ||
| useUsageLimitStore.getState().hide(); | ||
| }); | ||
|
|
||
| it.each(cases)( | ||
| "$name", | ||
| async ({ arrange, available, modal, trackPayload }) => { | ||
| arrange(); | ||
|
|
||
| expect(await assertCloudUsageAvailable()).toBe(available); | ||
|
|
||
| const state = useUsageLimitStore.getState(); | ||
| expect(state.isOpen).toBe(modal.isOpen); | ||
| if (modal.bucket !== undefined) expect(state.bucket).toBe(modal.bucket); | ||
| if (modal.resetAt !== undefined) | ||
| expect(state.resetAt).toBe(modal.resetAt); | ||
| if (modal.isPro !== undefined) expect(state.isPro).toBe(modal.isPro); | ||
|
|
||
| if (trackPayload) { | ||
| expect(track).toHaveBeenCalledWith( | ||
| ANALYTICS_EVENTS.CLOUD_TASK_USAGE_BLOCKED, | ||
| trackPayload, | ||
| ); | ||
| } else { | ||
| expect(track).not.toHaveBeenCalled(); | ||
| } | ||
| }, | ||
| ); | ||
| }); |
62 changes: 62 additions & 0 deletions
62
apps/code/src/renderer/features/billing/preflightCloudUsage.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { | ||
| type UsageLimitBucket, | ||
| useUsageLimitStore, | ||
| } from "@features/billing/stores/usageLimitStore"; | ||
| import { isUsageExceeded } from "@features/billing/utils"; | ||
| import type { UsageOutput } from "@main/services/llm-gateway/schemas"; | ||
| import { trpcClient } from "@renderer/trpc/client"; | ||
| import { ANALYTICS_EVENTS } from "@shared/types/analytics"; | ||
| import { track } from "@utils/analytics"; | ||
| import { logger } from "@utils/logger"; | ||
|
|
||
| const log = logger.scope("preflight-cloud-usage"); | ||
|
|
||
| function usageLimitArgs(usage: UsageOutput): { | ||
| bucket: UsageLimitBucket; | ||
| resetAt: string; | ||
| isPro: boolean; | ||
| } { | ||
| // Prefer the bucket that's actually exceeded (burst/daily takes priority); if neither | ||
| // is flagged (is_rate_limited via a server-side valve), fall back to the monthly bucket | ||
| // so the modal still shows a title and reset time rather than a bare prompt. | ||
| const bucket: UsageLimitBucket = usage.burst.exceeded ? "burst" : "sustained"; | ||
| return { bucket, resetAt: usage[bucket].reset_at, isPro: usage.is_pro }; | ||
| } | ||
|
|
||
| async function fetchUsageSnapshot(): Promise<UsageOutput | null> { | ||
| const fresh = await trpcClient.usageMonitor.refresh | ||
| .mutate() | ||
| .catch((error) => { | ||
| log.warn("Usage refresh failed; falling back to latest snapshot", { | ||
| error, | ||
| }); | ||
| return null; | ||
| }); | ||
| if (fresh) return fresh; | ||
|
|
||
| return trpcClient.usageMonitor.getLatest.query().catch((error) => { | ||
| log.warn("Usage lookup failed; allowing cloud creation", { error }); | ||
| return null; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Pre-flight gate for cloud task creation. Returns false (and shows the upgrade | ||
| * modal) when the team is over its usage limit, so no cloud task/run is created. | ||
| * | ||
| * Best-effort: if usage can't be fetched, returns true (fail open) — a usage | ||
| * service hiccup must never block task creation. | ||
| */ | ||
| export async function assertCloudUsageAvailable(): Promise<boolean> { | ||
| const usage = await fetchUsageSnapshot(); | ||
| if (usage && isUsageExceeded(usage)) { | ||
| const args = usageLimitArgs(usage); | ||
| track(ANALYTICS_EVENTS.CLOUD_TASK_USAGE_BLOCKED, { | ||
| bucket: args.bucket, | ||
| is_pro: usage.is_pro, | ||
| }); | ||
| useUsageLimitStore.getState().show(args); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.