diff --git a/apps/mobile/src/features/usage/UsageLimitsPooled.tsx b/apps/mobile/src/features/usage/UsageLimitsPooled.tsx index 2f0ad0f89dfc0..b863dfc9fed04 100644 --- a/apps/mobile/src/features/usage/UsageLimitsPooled.tsx +++ b/apps/mobile/src/features/usage/UsageLimitsPooled.tsx @@ -10,6 +10,7 @@ import { collectLimitPools, cursorUsageWindowDetails, displayLimitWindows, + formatPaceHeadroom, formatResetsIn, paceRemainingPercent, remainingPercent, @@ -30,8 +31,6 @@ import { environmentPresentations } from "../../state/presentation"; import { ResetCredits } from "./UsageLimitsSection"; import { usageProviderKind, useProviderColors } from "./usageProviders"; -const PACE_LABEL = { ahead: "Ahead of pace", on: "On pace", under: "Under pace" } as const; - function accountName(account: LimitAccount) { if (account.displayName) return account.displayName; if (!account.email) return providerClients.get(account.driver)?.label ?? account.driver; @@ -193,29 +192,36 @@ function PoolWindowCard({ }); const summary = ( <> - - + + {label ?? pool.label} - + {pool.remainingPercent}% left + {nextRefill ? ( + 1 ? "Pooled usage" : "Usage"} +${nextRefill.restoresPercent}% ${formatResetsIn( + nextRefill.member.window, + now, + ) + ?.replace("resets in ", "in ") + .replace("resets now", "now")}`} + className="text-xs tabular-nums text-foreground-muted" + > + ↻ +{nextRefill.restoresPercent}% + + ) : null} - {pool.pace ? ( - {PACE_LABEL[pool.pace]} + {pool.paceHeadroomPercent !== null ? ( + + {formatPaceHeadroom(pool.paceHeadroomPercent)} + ) : null} {description ? {description} : null} - {nextRefill ? ( - - ↻ +{nextRefill.restoresPercent}%{" "} - {formatResetsIn(nextRefill.member.window, now) - ?.replace("resets in ", "in ") - .replace("resets now", "now")} - - ) : null} ); const bar = ( diff --git a/apps/web/src/components/usage/UsageLimits.tsx b/apps/web/src/components/usage/UsageLimits.tsx index 43ee023af2400..53a7e2ecc97d4 100644 --- a/apps/web/src/components/usage/UsageLimits.tsx +++ b/apps/web/src/components/usage/UsageLimits.tsx @@ -55,7 +55,7 @@ export function barColor(driver: ServerProvider["driver"]): string { return presentation?.color ?? "var(--foreground)"; } -export function PaceIcon({ pace }: { readonly pace: LimitPace }) { +function PaceIcon({ pace }: { readonly pace: LimitPace }) { const Icon = PACE[pace].icon; return ( diff --git a/apps/web/src/components/usage/UsageLimitsPooled.tsx b/apps/web/src/components/usage/UsageLimitsPooled.tsx index 00f437246584f..1822f9e1bddcf 100644 --- a/apps/web/src/components/usage/UsageLimitsPooled.tsx +++ b/apps/web/src/components/usage/UsageLimitsPooled.tsx @@ -6,6 +6,7 @@ import { collectLimitPools, cursorUsageWindowDetails, displayLimitWindows, + formatPaceHeadroom, formatResetsIn, type LimitAccount, type LimitPool, @@ -38,9 +39,9 @@ import { RedactedSensitiveText } from "../settings/RedactedSensitiveText"; import { Button } from "../ui/button"; import { OpenAI } from "../Icons"; import { Alert, AlertTitle } from "../ui/alert"; +import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; import { Popover, PopoverCreateHandle, PopoverPopup, PopoverTrigger } from "../ui/popover"; import { - PaceIcon, ResetCreditDialog, barColor, usePaceMark, @@ -546,18 +547,35 @@ function PoolWindowCard({
{label ?? pool.label} - + {pool.remainingPercent}% - - left - {pool.pace ? : null} - + left + {nextRefill ? ( + + + } + > + ↻ +{nextRefill.restoresPercent}% + + + {pool.columns.length > 1 ? "Pooled usage" : "Usage"} +{nextRefill.restoresPercent}%{" "} + {formatResetsIn(nextRefill.member.window, now) + ?.replace("resets in ", "in ") + .replace("resets now", "now")} + + + ) : null} - {nextRefill && pool.columns.length > 1 ? ( - - ↻ +{nextRefill.restoresPercent}% + {pool.paceHeadroomPercent !== null ? ( + + {formatPaceHeadroom(pool.paceHeadroomPercent)} ) : null}
diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index e9d5f6e604f31..735a6c3045d88 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -18,6 +18,7 @@ import { collectLimitNotices, collectLimitPools, elapsedShare, + formatPaceHeadroom, exhaustedUsageWindow, paceOf, paceRemainingPercent, @@ -154,6 +155,15 @@ describe("new thread model fallback", () => { }); describe("pace", () => { + it("phrases distance from even spending", () => { + expect(formatPaceHeadroom(24)).toBe("24% behind pace"); + expect(formatPaceHeadroom(-7)).toBe("7% ahead of pace"); + expect(formatPaceHeadroom(0)).toBe("On pace"); + expect(formatPaceHeadroom(3)).toBe("On pace"); + expect(formatPaceHeadroom(-3)).toBe("On pace"); + expect(formatPaceHeadroom(4)).toBe("4% behind pace"); + }); + it("places the clock three fifths through a five-hour window with two hours left", () => { expect(elapsedShare(window, now)).toBeCloseTo(0.6); expect(paceRemainingPercent(window, now)).toBe(40); @@ -873,12 +883,12 @@ describe("pools", () => { ), now, ); - expect(untimed[0]?.windows[0]?.pace).toBe("on"); + expect(untimed[0]?.windows[0]?.paceHeadroomPercent).toBeNull(); expect(session).toMatchObject({ id: "five_hour", remainingPercent: 40, usedPercent: 60, - pace: "under", + paceHeadroomPercent: 10, }); expect( session?.resets.map((reset) => [reset.member.account.key, reset.restoresPercent]), @@ -1046,7 +1056,11 @@ describe("pooled account columns", () => { ], now, ); - expect(pool!.windows[0]).toMatchObject({ remainingPercent: 0, pace: null, resets: [] }); + expect(pool!.windows[0]).toMatchObject({ + remainingPercent: 0, + paceHeadroomPercent: null, + resets: [], + }); expect(pool!.windows[0]!.columns.map((column) => column.window?.resetsAt)).toEqual([ undefined, undefined, diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index 1707deb735a49..bd7de50a1813d 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -428,7 +428,12 @@ export interface LimitPoolWindow { >; readonly remainingPercent: number; readonly usedPercent: number; - readonly pace: LimitPace | null; + /** + * Points between what is left and what even spending would leave by now: + * positive is quota to spare, negative is spending faster than the clock. + * Null unless every member reports a window length and reset. + */ + readonly paceHeadroomPercent: number | null; readonly resets: ReadonlyArray<{ readonly member: LimitPoolMember; readonly at: number; @@ -575,14 +580,17 @@ function poolWindows(accounts: readonly LimitAccount[], now: number): readonly L const first = members[0]!.window; const id = members[0]!.account.driver === "codex" ? key : first.id; const usedPercent = members.reduce((sum, m) => sum + m.window.usedPercent, 0) / members.length; - // Judge pace only for windows with reset clocks; others add spend without elapsed time. const timed = members.flatMap((m) => { const share = elapsedShare(m.window, now); return share === null ? [] : [{ used: m.window.usedPercent, elapsed: share }]; }); const timedUsed = timed.reduce((sum, t) => sum + t.used, 0) / timed.length; const meanElapsed = - timed.length > 0 ? timed.reduce((sum, t) => sum + t.elapsed, 0) / timed.length : null; + timed.length === members.length + ? timed.reduce((sum, t) => sum + t.elapsed, 0) / timed.length + : null; + // Headroom is taken between rounded figures so it agrees with the bar's pace marks. + const paceRemaining = meanElapsed === null ? null : Math.round(100 - meanElapsed * 100); const resets = members .flatMap((member) => { const at = resetMillis(member.window); @@ -607,7 +615,8 @@ function poolWindows(accounts: readonly LimitAccount[], now: number): readonly L ), usedPercent: Math.round(usedPercent), remainingPercent: Math.round(100 - usedPercent), - pace: meanElapsed === null ? null : paceOfShares(timedUsed, meanElapsed), + paceHeadroomPercent: + paceRemaining === null ? null : Math.round(100 - timedUsed) - paceRemaining, resets, }; }); @@ -665,6 +674,11 @@ function paceOfShares(usedPercent: number, elapsed: number): LimitPace { return "on"; } +export function formatPaceHeadroom(headroomPercent: number): string { + if (Math.abs(headroomPercent) <= 3) return "On pace"; + return `${Math.abs(headroomPercent)}% ${headroomPercent > 0 ? "behind" : "ahead of"} pace`; +} + export function formatDuration(ms: number): string { const remaining = Math.max(0, ms); const days = Math.floor(remaining / DAY);