Skip to content
Closed
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
36 changes: 21 additions & 15 deletions apps/mobile/src/features/usage/UsageLimitsPooled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
collectLimitPools,
cursorUsageWindowDetails,
displayLimitWindows,
formatPaceHeadroom,
formatResetsIn,
paceRemainingPercent,
remainingPercent,
Expand All @@ -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;
Expand Down Expand Up @@ -193,29 +192,36 @@ function PoolWindowCard({
});
const summary = (
<>
<View className={wide ? "gap-1" : "flex-row items-start justify-between gap-3"}>
<View className="gap-1">
<View className={wide ? "gap-1" : "flex-row flex-wrap items-start justify-between gap-3"}>
<View className="min-w-0 max-w-full gap-1">
<Text className="text-sm font-t3-medium text-foreground">{label ?? pool.label}</Text>
<View className="flex-row items-baseline gap-1.5">
<View className="flex-row flex-wrap items-baseline gap-1.5">
<Text className="text-3xl font-t3-bold tabular-nums text-foreground">
{pool.remainingPercent}%
</Text>
<Text className="text-sm text-foreground-muted">left</Text>
{nextRefill ? (
<Text
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
accessibilityLabel={`${pool.columns.length > 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}%
</Text>
) : null}
</View>
</View>
{pool.pace ? (
<Text className="text-xs text-foreground-tertiary">{PACE_LABEL[pool.pace]}</Text>
{pool.paceHeadroomPercent !== null ? (
<Text className="text-xs tabular-nums text-foreground-tertiary">
{formatPaceHeadroom(pool.paceHeadroomPercent)}
</Text>
) : null}
</View>
{description ? <Text className="text-xs text-foreground-muted">{description}</Text> : null}
{nextRefill ? (
<Text className="text-xs tabular-nums text-foreground-muted">
↻ +{nextRefill.restoresPercent}%{" "}
{formatResetsIn(nextRefill.member.window, now)
?.replace("resets in ", "in ")
.replace("resets now", "now")}
</Text>
) : null}
</>
);
const bar = (
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/usage/UsageLimits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Tooltip>
Expand Down
36 changes: 27 additions & 9 deletions apps/web/src/components/usage/UsageLimitsPooled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
collectLimitPools,
cursorUsageWindowDetails,
displayLimitWindows,
formatPaceHeadroom,
formatResetsIn,
type LimitAccount,
type LimitPool,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -546,18 +547,35 @@ function PoolWindowCard({
<div className="grid items-center gap-x-6 gap-y-3 rounded-lg border border-border/60 p-4 md:grid-cols-[11rem_minmax(0,1fr)]">
<div className="flex flex-col gap-1">
<span className="text-sm font-medium text-foreground">{label ?? pool.label}</span>
<span className="flex items-baseline gap-2">
<span className="flex flex-wrap items-baseline gap-x-2">
<span className="text-3xl font-semibold text-foreground tabular-nums">
{pool.remainingPercent}%
</span>
<span className="inline-flex items-center gap-2 text-sm text-muted-foreground">
<span>left</span>
{pool.pace ? <PaceIcon pace={pool.pace} /> : null}
</span>
<span className="text-sm text-muted-foreground">left</span>
{nextRefill ? (
<Tooltip>
<TooltipTrigger
render={
<span
tabIndex={0}
className="rounded-sm text-xs text-muted-foreground tabular-nums outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"
/>
}
>
↻ +{nextRefill.restoresPercent}%
</TooltipTrigger>
<TooltipPopup side="top">
{pool.columns.length > 1 ? "Pooled usage" : "Usage"} +{nextRefill.restoresPercent}%{" "}
{formatResetsIn(nextRefill.member.window, now)
?.replace("resets in ", "in ")
.replace("resets now", "now")}
</TooltipPopup>
</Tooltip>
) : null}
</span>
{nextRefill && pool.columns.length > 1 ? (
<span className="text-xs font-medium text-foreground tabular-nums">
↻ +{nextRefill.restoresPercent}%
{pool.paceHeadroomPercent !== null ? (
<span className="text-xs text-muted-foreground tabular-nums">
{formatPaceHeadroom(pool.paceHeadroomPercent)}
</span>
) : null}
</div>
Expand Down
20 changes: 17 additions & 3 deletions packages/shared/src/usageLimits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
collectLimitNotices,
collectLimitPools,
elapsedShare,
formatPaceHeadroom,
exhaustedUsageWindow,
paceOf,
paceRemainingPercent,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]),
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 18 additions & 4 deletions packages/shared/src/usageLimits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
resets,
};
});
Expand Down Expand Up @@ -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);
Expand Down
Loading