Skip to content

Commit 2d38f00

Browse files
StiensWoutcodexclaudeGrok 4.7
committed
fix(client-runtime): reuse usage history on reopen and refresh stale totals
Keep usage summaries for 30 idle minutes and use existing remount revalidation so reopening shows loaded totals while stale data refreshes in the background. Co-authored-by: GPT 6.1 Sol <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Grok 4.7 <[email protected]>
1 parent fc2b5de commit 2d38f00

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,12 +1496,14 @@ export function createServerEnvironmentAtoms<R, E>(
14961496
label: "environment-data:server:scheduled-tasks:live",
14971497
tag: WS_METHODS.scheduledTasksSubscribe,
14981498
}),
1499-
// A cold transcript scan is measured in seconds, so keep the result around
1500-
// long enough that switching windows or re-rendering does not rescan.
1499+
// A cold transcript scan is measured in seconds, so reopening Usage shows the
1500+
// kept history at once and reads it again in the background once stale.
15011501
usageSummary: createEnvironmentRpcQueryAtomFamily(runtime, {
15021502
label: "environment-data:server:usage-summary",
15031503
tag: WS_METHODS.serverGetUsageSummary,
15041504
staleTimeMs: 60_000,
1505+
idleTtlMs: 30 * 60_000,
1506+
revalidateOnRemount: true,
15051507
refreshTrigger: ({ environmentId }) => usageScanSettingsAtom(environmentId),
15061508
execute: (input, emit) =>
15071509
request(WS_METHODS.serverGetUsageSummary, input).pipe(

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
type UsageSummary,
1313
type UsageSummaryInput,
1414
} from "@t3tools/contracts";
15-
import { expect, it } from "@effect/vitest";
15+
import { expect, it, vi } from "@effect/vitest";
1616
import * as Deferred from "effect/Deferred";
1717
import * as Effect from "effect/Effect";
1818
import * as Layer from "effect/Layer";
@@ -300,3 +300,45 @@ it.effect("keeps the cached usage summary when the awaited refresh fails", () =>
300300
}),
301301
),
302302
);
303+
304+
it.effect("reopening usage history shows it at once and reads again only once stale", () =>
305+
Effect.scoped(
306+
Effect.gen(function* () {
307+
const OPENED_AT_MS = Date.UTC(2026, 8, 4, 12);
308+
// Staleness is measured on Date.now, so only Date is faked.
309+
yield* Effect.acquireRelease(
310+
Effect.sync(() => vi.useFakeTimers({ toFake: ["Date"], now: OPENED_AT_MS })),
311+
() => Effect.sync(() => vi.useRealTimers()),
312+
);
313+
const reread = yield* Deferred.make<void>();
314+
const harness = yield* makeHarness((request) =>
315+
request === 2 ? Deferred.succeed(reread, undefined).pipe(Effect.asVoid) : Effect.void,
316+
);
317+
const summary = harness.summary();
318+
const afterScheduledTasks = Effect.callback<void>((resume) => {
319+
harness.registry.schedulerAsync.makeDispatcher().scheduleTask(() => resume(Effect.void), 0);
320+
});
321+
// Closing drops the refresh-trigger wrapper, then the stale check under it, one task each.
322+
const closeView = (close: () => void) =>
323+
Effect.sync(close).pipe(
324+
Effect.andThen(afterScheduledTasks.pipe(Effect.repeat({ times: 1 }))),
325+
);
326+
const openAt = (seconds: number) => {
327+
vi.setSystemTime(OPENED_AT_MS + seconds * 1_000);
328+
const close = harness.registry.mount(summary);
329+
expect(AsyncResult.getOrElse(harness.registry.get(summary), () => null)).not.toBeNull();
330+
return close;
331+
};
332+
333+
const closeFirst = harness.registry.mount(summary);
334+
yield* settledSummary(harness.registry, summary);
335+
yield* closeView(closeFirst);
336+
yield* closeView(openAt(30));
337+
const close = openAt(91);
338+
yield* Deferred.await(reread);
339+
yield* settledSummary(harness.registry, summary);
340+
expect(harness.requests()).toBe(2);
341+
close();
342+
}),
343+
),
344+
);

0 commit comments

Comments
 (0)