Skip to content

Commit 7d55f39

Browse files
saphidclaude
andcommitted
fix(web): offline usage respects the usage model filter
Main's per-environment model filter applied only to live usage reads, so an offline environment could restore a saved read taken under another filter and show models the filter excludes. Saved reads now record the filter they were requested with, and only a read under the same filter answers offline. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0d08670 commit 7d55f39

3 files changed

Lines changed: 25 additions & 23 deletions

File tree

‎apps/web/src/state/savedUsage.test.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,18 @@ describe("pickSavedUsage", () => {
106106
expect(pickSavedUsage([week], shifted)).toBeNull();
107107
});
108108

109-
it("finds nothing outside the saved days or in another time zone", () => {
109+
it("finds nothing outside the saved days, in another time zone, or under another model filter", () => {
110110
expect(pickSavedUsage([month], days("2026-10-01", "2026-10-07"))).toBeNull();
111111
expect(
112112
pickSavedUsage([month], { ...days("2026-09-01", "2026-09-30"), timeZone: "Asia/Tokyo" }),
113113
).toBeNull();
114+
const filter = { exclude: [{ model: "claude-opus-5-5" }] };
115+
const filtered = { ...days("2026-09-01", "2026-09-30"), modelFilter: filter };
116+
expect(pickSavedUsage([month], filtered)).toBeNull();
117+
expect(
118+
pickSavedUsage([{ ...month, input: filtered }], days("2026-09-01", "2026-09-30")),
119+
).toBeNull();
120+
expect(pickSavedUsage([{ ...month, input: filtered }], filtered)).not.toBeNull();
114121
});
115122
});
116123

‎apps/web/src/state/savedUsage.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ function overlapOf(saved: UsageSummaryInput, input: UsageSummaryInput): number |
174174

175175
/**
176176
* The saved read that best covers a window, cut to it. Only a read at the
177-
* window's own resolution answers it, and an hourly read only on the same
178-
* hour grid.
177+
* window's own resolution and model filter answers it, and an hourly read
178+
* only on the same hour grid.
179179
*/
180180
export function pickSavedUsage(
181181
saved: readonly SavedUsage[],
@@ -185,6 +185,8 @@ export function pickSavedUsage(
185185
for (const entry of saved) {
186186
if (entry.input.timeZone !== input.timeZone) continue;
187187
if ((entry.input.resolution ?? "day") !== (input.resolution ?? "day")) continue;
188+
// A read under another model filter counts models this one excludes, or misses some.
189+
if (JSON.stringify(entry.input.modelFilter) !== JSON.stringify(input.modelFilter)) continue;
188190
const overlap = overlapOf(entry.input, input);
189191
if (overlap === null || !(overlap > 0)) continue;
190192
if (best === null || overlap > best.amount) best = { entry, amount: overlap };

‎apps/web/src/state/usage.ts‎

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export interface EnvironmentUsageStatus {
5757
readonly offline: boolean;
5858
/** When the shown summary was read, if it is not a live read. */
5959
readonly savedAt: string | null;
60-
/** The window `summary` answers, for saving it. */
60+
/** The window and model filter `summary` answers, for saving it. */
6161
readonly window: UsageSummaryInput;
6262
}
6363

@@ -184,22 +184,15 @@ const usageByWindowAtom = Atom.family((windowKey: string) =>
184184
continue;
185185
}
186186
const modelFilter = get(serverEnvironment.usageModelFilterAtom(environmentId));
187-
let window = input;
188-
let result = get(
189-
serverEnvironment.usageSummary({
190-
environmentId,
191-
input: usageSummaryInput(input, modelFilter),
192-
}),
193-
);
187+
const requested = usageSummaryInput(input, modelFilter);
188+
const fallbackRequested =
189+
fallbackInput === null ? null : usageSummaryInput(fallbackInput, modelFilter);
190+
let window = requested;
191+
let result = get(serverEnvironment.usageSummary({ environmentId, input: requested }));
194192
let readByDay = false;
195-
if (fallbackInput !== null && isRejectedWindow(result)) {
196-
window = fallbackInput;
197-
result = get(
198-
serverEnvironment.usageSummary({
199-
environmentId,
200-
input: usageSummaryInput(fallbackInput, modelFilter),
201-
}),
202-
);
193+
if (fallbackRequested !== null && isRejectedWindow(result)) {
194+
window = fallbackRequested;
195+
result = get(serverEnvironment.usageSummary({ environmentId, input: fallbackRequested }));
203196
readByDay = true;
204197
}
205198
let summary = AsyncResult.getOrElse(result, () => null);
@@ -210,13 +203,13 @@ const usageByWindowAtom = Atom.family((windowKey: string) =>
210203
AsyncResult.value(get(savedUsageAtom(environmentId))),
211204
() => [],
212205
);
213-
summary = pickSavedUsage(saved, input);
206+
summary = pickSavedUsage(saved, requested);
214207
// A server that answered this span only by day saved it by day.
215-
if (summary === null && fallbackInput !== null) {
216-
summary = pickSavedUsage(saved, fallbackInput);
208+
if (summary === null && fallbackRequested !== null) {
209+
summary = pickSavedUsage(saved, fallbackRequested);
217210
if (summary !== null) {
218211
readByDay = true;
219-
window = fallbackInput;
212+
window = fallbackRequested;
220213
}
221214
}
222215
}

0 commit comments

Comments
 (0)