Skip to content

Commit ac604af

Browse files
feat(web): the limits view forecasts whether an allowance lasts until its reset
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 54d1514 commit ac604af

3 files changed

Lines changed: 122 additions & 2 deletions

File tree

‎apps/web/src/components/usage/UsageLimitTrendSection.tsx‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function UsageLimitTrendSection({
5656
>
5757
<h3 className="truncate text-sm font-medium text-foreground">{account.name}</h3>
5858
{account.lines.map((line) => (
59-
<div key={line.windowId} className="flex items-center gap-3">
59+
<div key={line.windowId} className="flex flex-wrap items-center gap-x-3">
6060
<span className="w-28 shrink-0 truncate text-xs text-muted-foreground">
6161
{line.label}
6262
</span>
@@ -78,6 +78,19 @@ export function UsageLimitTrendSection({
7878
<span className="w-16 shrink-0 text-right text-xs tabular-nums text-foreground">
7979
{Math.round(line.remainingPercent)}% left
8080
</span>
81+
{line.forecast ? (
82+
<span
83+
className={
84+
line.forecast.type === "runs-out"
85+
? "basis-full pl-31 text-xs text-warning-foreground"
86+
: "basis-full pl-31 text-xs text-muted-foreground"
87+
}
88+
>
89+
{line.forecast.type === "runs-out"
90+
? `At this pace empty ${resetTimeFormat.format(new Date(line.forecast.at))}, before the reset ${resetTimeFormat.format(new Date(line.forecast.resetsAt))}`
91+
: `At this pace ${line.forecast.unusedPercent}% stays unused at the reset ${resetTimeFormat.format(new Date(line.forecast.resetsAt))}`}
92+
</span>
93+
) : null}
8194
</div>
8295
))}
8396
{account.resets.length > 0 ? (

‎apps/web/src/components/usage/usageLimitTrend.test.ts‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ProviderDriverKind, ProviderInstanceId, type UsageLimitHistory } from "@t3tools/contracts";
22
import { describe, expect, it } from "vite-plus/test";
33

4-
import { buildUsageLimitTrend } from "./usageLimitTrend";
4+
import { buildUsageLimitTrend, forecastUsageWindow } from "./usageLimitTrend";
55

66
const SINCE = Date.parse("2026-10-10T00:00:00.000Z");
77
const NOW = Date.parse("2026-10-11T00:00:00.000Z");
@@ -80,3 +80,56 @@ describe("buildUsageLimitTrend", () => {
8080
expect(account?.resets.map((entry) => entry.unusedPercentBefore)).toEqual([12, 38]);
8181
});
8282
});
83+
84+
describe("forecastUsageWindow", () => {
85+
const RESET = "2026-10-12T00:00:00.000Z";
86+
const reading = (at: string, usedPercent: number, resetsAt: string | null = RESET) => ({
87+
...point("codex", at, usedPercent),
88+
...(resetsAt === null ? {} : { resetsAt }),
89+
});
90+
91+
it("says when the allowance runs out before the reset", () => {
92+
// 60 points in 12 hours: the remaining 40 are gone 8 hours from now.
93+
expect(
94+
forecastUsageWindow(
95+
[reading("2026-10-10T12:00:00.000Z", 0), reading("2026-10-10T23:00:00.000Z", 60)],
96+
NOW,
97+
),
98+
).toEqual({ type: "runs-out", at: "2026-10-11T08:00:00.000Z", resetsAt: RESET });
99+
});
100+
101+
it("says what stays unused when the pace is slow enough", () => {
102+
// 12 points in 12 hours: 24 more by the reset a day later, 36 used, 64 unused.
103+
expect(
104+
forecastUsageWindow(
105+
[reading("2026-10-10T12:00:00.000Z", 0), reading("2026-10-10T23:00:00.000Z", 12)],
106+
NOW,
107+
),
108+
).toEqual({ type: "lasts", unusedPercent: 64, resetsAt: RESET });
109+
});
110+
111+
it("measures the pace from the last reset, not from before it", () => {
112+
expect(
113+
forecastUsageWindow(
114+
[
115+
reading("2026-10-10T00:00:00.000Z", 90),
116+
reading("2026-10-10T12:00:00.000Z", 0),
117+
reading("2026-10-10T23:00:00.000Z", 12),
118+
],
119+
NOW,
120+
),
121+
).toEqual({ type: "lasts", unusedPercent: 64, resetsAt: RESET });
122+
});
123+
124+
it("stays silent without a reset ahead or with too little of the cycle recorded", () => {
125+
expect(forecastUsageWindow([reading("2026-10-10T12:00:00.000Z", 10, null)], NOW)).toBeNull();
126+
expect(
127+
forecastUsageWindow(
128+
[reading("2026-10-10T12:00:00.000Z", 10, "2026-10-10T20:00:00.000Z")],
129+
NOW,
130+
),
131+
).toBeNull();
132+
expect(forecastUsageWindow([reading("2026-10-10T23:30:00.000Z", 10)], NOW)).toBeNull();
133+
expect(forecastUsageWindow([], NOW)).toBeNull();
134+
});
135+
});

‎apps/web/src/components/usage/usageLimitTrend.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,61 @@ import type {
1212
} from "@t3tools/contracts";
1313
import { resolveProviderInstanceDisplayName } from "@t3tools/client-runtime/state/provider-instance-display";
1414

15+
/**
16+
* Where a window ends up if the account keeps its pace since the last reset:
17+
* empty before the reset, or at the reset with allowance to spare.
18+
*/
19+
export type UsageLimitForecast =
20+
| { readonly type: "runs-out"; readonly at: string; readonly resetsAt: string }
21+
| { readonly type: "lasts"; readonly unusedPercent: number; readonly resetsAt: string };
22+
23+
/** A pace measured over less than this says nothing yet. */
24+
const FORECAST_MIN_ELAPSED_MS = 60 * 60_000;
25+
26+
/**
27+
* `points` are one window's readings, oldest first. The pace is taken from the
28+
* readings since the last drop, which is the current cycle. No forecast
29+
* without an announced reset still ahead, or before an hour of the cycle has
30+
* been recorded.
31+
*/
32+
export function forecastUsageWindow(
33+
points: ReadonlyArray<UsageLimitHistoryPoint>,
34+
nowMs: number,
35+
): UsageLimitForecast | null {
36+
const last = points.at(-1);
37+
if (last === undefined || last.resetsAt === undefined) return null;
38+
const resetsAtMs = Date.parse(last.resetsAt);
39+
if (!Number.isFinite(resetsAtMs) || resetsAtMs <= nowMs) return null;
40+
let start = last;
41+
for (let index = points.length - 2; index >= 0; index -= 1) {
42+
const point = points[index]!;
43+
if (point.usedPercent > start.usedPercent) break;
44+
start = point;
45+
}
46+
const elapsedMs = nowMs - Date.parse(start.at);
47+
if (!(elapsedMs >= FORECAST_MIN_ELAPSED_MS)) return null;
48+
const pacePerMs = (last.usedPercent - start.usedPercent) / elapsedMs;
49+
const usedAtReset = last.usedPercent + pacePerMs * (resetsAtMs - nowMs);
50+
if (usedAtReset < 100) {
51+
return {
52+
type: "lasts",
53+
unusedPercent: Math.round(100 - usedAtReset),
54+
resetsAt: last.resetsAt,
55+
};
56+
}
57+
const emptyAtMs = pacePerMs <= 0 ? nowMs : nowMs + (100 - last.usedPercent) / pacePerMs;
58+
return {
59+
type: "runs-out",
60+
at: new Date(Math.min(emptyAtMs, resetsAtMs)).toISOString(),
61+
resetsAt: last.resetsAt,
62+
};
63+
}
64+
1565
export interface UsageLimitTrendLine {
1666
readonly windowId: string;
1767
readonly label: string;
68+
/** Absent when there is not enough to go on. */
69+
readonly forecast?: UsageLimitForecast;
1870
/** Remaining allowance now, by the last recorded reading. */
1971
readonly remainingPercent: number;
2072
/**
@@ -95,7 +147,9 @@ export function buildUsageLimitTrend(input: {
95147
(left, right) => Date.parse(left.at) - Date.parse(right.at),
96148
);
97149
const last = sorted.at(-1)!;
150+
const forecast = forecastUsageWindow(sorted, input.nowMs);
98151
return {
152+
...(forecast === null ? {} : { forecast }),
99153
windowId,
100154
label: last.label,
101155
remainingPercent: Math.max(0, 100 - last.usedPercent),

0 commit comments

Comments
 (0)