Skip to content

Commit c0bdd51

Browse files
fix(usage): refresh thread costs after price edits
1 parent b863316 commit c0bdd51

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

‎packages/client-runtime/src/state/server.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,7 @@ export function createServerEnvironmentAtoms<R, E>(
10541054
label: "environment-data:server:usage-thread-breakdown",
10551055
tag: WS_METHODS.serverGetUsageThreadBreakdown,
10561056
staleTimeMs: 60_000,
1057+
refreshTrigger: ({ environmentId }) => usagePricesAtom(environmentId),
10571058
}),
10581059
configProjection,
10591060
welcome,

‎packages/client-runtime/src/state/serverUsage.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
type ServerConfig,
88
type ServerConfigStreamEvent,
99
type ServerSettings,
10+
type UsageThreadBreakdown,
1011
type UsageSummary,
1112
type UsageSummaryInput,
1213
} from "@t3tools/contracts";
@@ -57,7 +58,21 @@ const makeHarness = Effect.fn("ServerUsageTest.makeHarness")(function* (
5758
const events = yield* Queue.unbounded<ServerConfigStreamEvent>();
5859
let settings = DEFAULT_SERVER_SETTINGS;
5960
let requests = 0;
61+
let threadRequests = 0;
6062
const client = {
63+
[WS_METHODS.serverGetUsageThreadBreakdown]: () =>
64+
Effect.sync(() => {
65+
threadRequests += 1;
66+
return {
67+
...INPUT,
68+
contractVersion: USAGE_CONTRACT_VERSION,
69+
readAt: "2026-09-04T12:00:00Z",
70+
rows: [],
71+
truncatedRows: 0,
72+
scanDurationMs:
73+
settings.usagePriceOverrides["custom-model"]?.inputCostPerMillionTokens ?? 0,
74+
} satisfies UsageThreadBreakdown;
75+
}),
6176
[WS_METHODS.subscribeServerConfig]: () =>
6277
Stream.concat(
6378
Stream.make({ version: 1 as const, type: "snapshot" as const, config: CONFIG }),
@@ -168,6 +183,8 @@ const makeHarness = Effect.fn("ServerUsageTest.makeHarness")(function* (
168183
return {
169184
registry,
170185
requests: () => requests,
186+
threadRequests: () => threadRequests,
187+
threads: atoms.usageThreadBreakdown({ environmentId: TARGET.environmentId, input: INPUT }),
171188
updateSettings,
172189
summary: (input = INPUT) => atoms.usageSummary({ environmentId: TARGET.environmentId, input }),
173190
};
@@ -261,3 +278,36 @@ it.effect("restarts a pending usage read after a price change", () =>
261278
}),
262279
),
263280
);
281+
282+
it.effect("refreshes mounted thread breakdowns when override prices change", () =>
283+
Effect.scoped(
284+
Effect.gen(function* () {
285+
const harness = yield* makeHarness();
286+
const unmount = harness.registry.mount(harness.threads);
287+
const waitForPrice = (price: number) =>
288+
AtomRegistry.toStream(harness.registry, harness.threads).pipe(
289+
Stream.filter(
290+
(result) =>
291+
AsyncResult.isSuccess(result) &&
292+
!result.waiting &&
293+
result.value.scanDurationMs === price,
294+
),
295+
Stream.runHead,
296+
);
297+
yield* waitForPrice(0);
298+
expect(harness.threadRequests()).toBe(1);
299+
yield* harness.updateSettings({
300+
...DEFAULT_SERVER_SETTINGS,
301+
usagePriceOverrides: {
302+
"custom-model": { inputCostPerMillionTokens: 3, outputCostPerMillionTokens: 9 },
303+
},
304+
});
305+
yield* waitForPrice(3);
306+
expect(harness.threadRequests()).toBe(2);
307+
yield* harness.updateSettings(DEFAULT_SERVER_SETTINGS);
308+
yield* waitForPrice(0);
309+
expect(harness.threadRequests()).toBe(3);
310+
unmount();
311+
}),
312+
),
313+
);

0 commit comments

Comments
 (0)