The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 16:00 UTC
perf(usage): show saved totals before scanning history (#25711)
Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Hieu Le committed
Nov 14, 2026 at 10:48 UTC
02e29ce8fa57eff37b85afef0e9077658118c276
2 files changed
+69
-1
apps/server/src/usage/UsageService.test.ts
+67
@@ -522,6 +522,73 @@ describe("Codex usage history", () => {
522
});
523
524
describe("UsageService", () => {
525
+ it.effect("keeps pricing metadata paired with the rates used during a concurrent refresh", () =>
526
+ Effect.gen(function* () {
527
+ const { home, settings } = yield* setup;
528
+ const sessions = NodePath.join(home, "codex", "sessions");
529
+ yield* Effect.promise(async () => {
530
+ await NodeFSP.mkdir(sessions, { recursive: true });
531
+ await NodeFSP.writeFile(
532
+ NodePath.join(sessions, "session.jsonl"),
533
+ [
534
+ { type: "session_meta", payload: { id: "session", model_provider: "openai" } },
535
+ { type: "turn_context", payload: { model: "gpt-6-astra", turn_id: "turn" } },
536
+ {
537
+ type: "event_msg",
538
+ timestamp: "2026-08-01T10:00:00Z",
539
+ payload: {
540
+ type: "token_count",
541
+ info: { last_token_usage: { input_tokens: 0, output_tokens: 1 } },
542
+ },
543
+ },
544
+ ]
545
+ .map((record) => encodeUnknownJsonString(record))
546
+ .join("\n") + "\n",
547
+ );
548
+ });
549
+ let rate = 0;
550
+ yield* Effect.gen(function* () {
551
+ const resolving = yield* Deferred.make<void>();
552
+ const release = yield* Deferred.make<void>();
553
+ const service = yield* UsageService.make.pipe(
554
+ Effect.provideService(CodexUsageHistory.CodexUsageHistory, {
555
+ resolve: () =>
556
+ Deferred.succeed(resolving, undefined).pipe(
557
+ Effect.andThen(Deferred.await(release)),
558
+ Effect.as(new Map()),
559
+ ),
560
+ }),
561
+ );
562
+ const initialPricing = yield* service.refreshRates;
563
+ yield* TestClock.adjust(61_000);
564
+ const pending = yield* service.readSummary(WINDOW).pipe(Effect.forkChild);
565
+ yield* Deferred.await(resolving);
566
+ yield* service.refreshRates;
567
+ yield* Deferred.succeed(release, undefined);
568
+ const original = yield* Fiber.join(pending);
569
+ assert.strictEqual(original.buckets[0]?.costUsd, 1);
570
+ assert.strictEqual(original.pricing.fetchedAt, initialPricing.fetchedAt);
571
+ const updated = yield* service.readSummary(WINDOW);
572
+ assert.strictEqual(updated.buckets[0]?.costUsd, 2);
573
+ assert.notStrictEqual(updated.pricing.fetchedAt, initialPricing.fetchedAt);
574
+ }).pipe(
575
+ Effect.provide(
576
+ layerService({
577
+ prefix: "concurrent-rates",
578
+ home,
579
+ settings,
580
+ ratesResponse: () =>
581
+ Effect.succeed(
582
+ Response.json({
583
+ "gpt-6-astra": { input_cost_per_token: 0, output_cost_per_token: ++rate },
584
+ }),
585
+ ),
586
+ }),
587
+ ),
588
+ );
589
+ }).pipe(Effect.scoped),
590
+ );
591
+
592
it.effect("counts compressed Codex rollouts across refresh, rewrites, and cache restarts", () =>
593
Effect.gen(function* () {
594
const { home, settings } = yield* setup;
apps/server/src/usage/UsageService.ts
+2
-1
@@ -881,6 +881,7 @@ export const make = Effect.gen(function* () {
881
{ concurrency: 2 },
882
);
883
884
+ const summaryPricing = pricing();
885
const aggregator = new UsageAggregator({
886
timeZone: input.timeZone,
887
sinceDay: input.sinceDay,
@@ -1116,7 +1117,7 @@ export const make = Effect.gen(function* () {
1117
untilDay: input.untilDay,
1118
buckets: aggregated.buckets,
1119
sources,
1119
- pricing: pricing(),
1120
+ pricing: summaryPricing,
1121
scanDurationMs: Math.max(0, finishedAtMs - startedAtMs),
1122
...(input.modelFilter === undefined ? {} : { modelFilter: input.modelFilter }),
1123
} satisfies UsageSummary;