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, 08:30 UTC
feat(server): configurable usage history retention (#28111)
Co-authored-by: chomamateusz-agent <chomamateusz-agent@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Mateusz Choma committed
Nov 17, 2026 at 06:08 UTC
428d365932a7331361b9e64485158a42ba9d34e1
9 files changed
+316
-49
apps/server/src/serverSettings.test.ts
+5
-2
@@ -2365,7 +2365,10 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
2365
}).pipe(Effect.provide(layerServerSettings())),
2366
);
2367
2368
- it.effect("leaves an unreadable settings.json untouched instead of folding over it", () =>
2368
+ it.effect.each([
2369
+ { label: "malformed JSON", broken: '{"defaultAutoPull": tru' },
2370
+ { label: "invalid retention", broken: '{"usageHistoryRetentionDays": 0}' },
2371
+ ])("leaves settings.json with $label untouched and disables destructive defaults", ({ broken }) =>
2372
Effect.gen(function* () {
2373
const serverConfig = yield* ServerConfig.ServerConfig;
2374
const fileSystem = yield* FileSystem.FileSystem;
@@ -2380,12 +2383,12 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
2383
${"2026-08-25T00:00:00.000Z"}, ${"2026-08-25T00:00:00.000Z"}
2384
)
2385
`;
2383
- const broken = '{"defaultAutoPull": tru';
2386
yield* fileSystem.writeFileString(serverConfig.settingsPath, broken);
2387
2388
const settings = yield* serverSettings.getSettings;
2389
assert.isFalse(settings.projectSettingsFolded);
2390
assert.isFalse(settings.continueThreadsAfterServerUpdate);
2391
+ assert.equal(settings.usageHistoryRetentionDays, "unlimited");
2392
assert.deepEqual(settings.projectSettingsOverrides, {});
2393
2394
assert.equal(yield* fileSystem.readFileString(serverConfig.settingsPath), broken);
apps/server/src/serverSettings.ts
+5
-1
@@ -806,7 +806,11 @@ const make = Effect.gen(function* () {
806
if (decoded._tag === "Failure" || legacySettings._tag === "Failure") {
807
const failure = decoded._tag === "Failure" ? decoded : legacySettings;
808
// The file may hold an opt-out we can no longer read; never resume threads on a guess.
809
- settings = { ...managed.defaults, continueThreadsAfterServerUpdate: false };
809
+ settings = {
810
+ ...managed.defaults,
811
+ continueThreadsAfterServerUpdate: false,
812
+ usageHistoryRetentionDays: "unlimited",
813
+ };
814
if (failure._tag === "Failure") {
815
parseFailure = Option.some(Cause.squash(failure.cause));
816
yield* Effect.logWarning("failed to parse settings.json, using defaults", {
apps/server/src/usage/UsageService.test.ts
+218
-31
@@ -25,6 +25,7 @@ import {
25
} from "@t3tools/contracts";
26
import * as Duration from "effect/Duration";
27
import * as Clock from "effect/Clock";
28
+import * as DateTime from "effect/DateTime";
29
import * as Deferred from "effect/Deferred";
30
import * as Effect from "effect/Effect";
31
import * as Exit from "effect/Exit";
@@ -289,12 +290,14 @@ function makeFakeCursor() {
290
events: [] as { readonly timestampMs: number; readonly outputTokens: number }[],
291
error: null as string | null,
292
gate: undefined as Deferred.Deferred<void> | undefined,
293
+ started: undefined as Deferred.Deferred<void> | undefined,
294
calls: [] as { readonly sinceMs: number; readonly untilMs: number }[],
295
credentials: [] as unknown[],
296
};
297
const read = (credential: unknown, sinceMs: number, untilMs: number) =>
298
Effect.gen(function* () {
299
state.calls.push({ sinceMs, untilMs });
300
+ if (state.started !== undefined) yield* Deferred.succeed(state.started, undefined);
301
state.credentials.push(credential);
302
if (state.gate !== undefined) yield* Deferred.await(state.gate);
303
const { accountKey, error } = state;
@@ -1864,45 +1867,119 @@ describe("UsageService", () => {
1867
}),
1868
);
1869
1867
- it.effect("restores the Cursor account cache after a restart", () =>
1870
+ it.effect.each([
1871
+ { label: "default", retention: undefined, cutoff: CURSOR_NOW - 92 * 24 * HOUR_MS },
1872
+ { label: "unlimited", retention: "unlimited" as const, cutoff: 0 },
1873
+ { label: "36500 days", retention: 36500, cutoff: 0 },
1874
+ ])(
1875
+ "propagates the $label cutoff and restores the Cursor account cache after a restart",
1876
+ ({ retention, cutoff }) =>
1877
+ Effect.gen(function* () {
1878
+ const { settings, home } = yield* setup;
1879
+ yield* TestClock.setTime(CURSOR_NOW);
1880
+ const before = makeFakeCursor();
1881
+ before.state.events = [
1882
+ { timestampMs: CURSOR_NOW - 30 * HOUR_MS, outputTokens: 5 },
1883
+ { timestampMs: CURSOR_NOW - 3 * HOUR_MS, outputTokens: 7 },
1884
+ ];
1885
+ yield* Effect.gen(function* () {
1886
+ const first = yield* makeWithCursor(before.read);
1887
+ const original = yield* first.readSummary({ ...WINDOW, awaitRefresh: true });
1888
+ assert.deepStrictEqual(before.state.calls, [{ sinceMs: cutoff, untilMs: CURSOR_NOW }]);
1889
+ yield* first.awaitPersisted;
1890
+
1891
+ const after = makeFakeCursor();
1892
+ after.state.events = before.state.events;
1893
+ const restarted = yield* makeWithCursor(after.read);
1894
+ const restored = yield* restarted.readSummary(WINDOW);
1895
+ assert.isUndefined(cursorSource(restored)?.refreshing);
1896
+ assert.deepStrictEqual(restored.buckets, original.buckets);
1897
+ assert.deepStrictEqual(cursorSource(restored), cursorSource(original));
1898
+ assert.strictEqual(after.state.calls.length, 0);
1899
+
1900
+ yield* TestClock.adjust(Duration.minutes(2));
1901
+ const refreshed = yield* restarted.readSummary({ ...WINDOW, awaitRefresh: true });
1902
+ assert.deepStrictEqual(refreshed.buckets, original.buckets);
1903
+ assert.deepStrictEqual(after.state.calls, [
1904
+ { sinceMs: CURSOR_NOW - HOUR_MS, untilMs: CURSOR_NOW + 2 * 60 * 1000 },
1905
+ ]);
1906
+ }).pipe(
1907
+ Effect.provide(
1908
+ layerService({
1909
+ prefix: "usage-service-cursor-restart",
1910
+ environment: { CURSOR_API_KEY: "test-key" },
1911
+ home,
1912
+ settings: {
1913
+ ...settings,
1914
+ ...(retention === undefined ? {} : { usageHistoryRetentionDays: retention }),
1915
+ },
1916
+ }),
1917
+ ),
1918
+ );
1919
+ }),
1920
+ );
1921
+
1922
+ it.effect("widens Cursor history after awaiting a shared narrower refresh", () =>
1923
Effect.gen(function* () {
1924
const { settings, home } = yield* setup;
1925
yield* TestClock.setTime(CURSOR_NOW);
1871
- const before = makeFakeCursor();
1872
- before.state.events = [
1873
- { timestampMs: CURSOR_NOW - 30 * HOUR_MS, outputTokens: 5 },
1926
+ const cursor = makeFakeCursor();
1927
+ cursor.state.gate = yield* Deferred.make<void>();
1928
+ cursor.state.started = yield* Deferred.make<void>();
1929
+ cursor.state.events = [
1930
+ { timestampMs: CURSOR_NOW - 120 * 24 * HOUR_MS, outputTokens: 5 },
1931
{ timestampMs: CURSOR_NOW - 3 * HOUR_MS, outputTokens: 7 },
1932
];
1876
- yield* Effect.gen(function* () {
1877
- const first = yield* makeWithCursor(before.read);
1878
- const original = yield* first.readSummary({ ...WINDOW, awaitRefresh: true });
1879
- yield* first.awaitPersisted;
1880
-
1881
- const after = makeFakeCursor();
1882
- after.state.events = before.state.events;
1883
- const restarted = yield* makeWithCursor(after.read);
1884
- const restored = yield* restarted.readSummary(WINDOW);
1885
- assert.isUndefined(cursorSource(restored)?.refreshing);
1886
- assert.deepStrictEqual(restored.buckets, original.buckets);
1887
- assert.deepStrictEqual(cursorSource(restored), cursorSource(original));
1888
- assert.strictEqual(after.state.calls.length, 0);
1889
-
1890
- yield* TestClock.adjust(Duration.minutes(2));
1891
- const refreshed = yield* restarted.readSummary({ ...WINDOW, awaitRefresh: true });
1892
- assert.deepStrictEqual(refreshed.buckets, original.buckets);
1893
- assert.deepStrictEqual(after.state.calls, [
1894
- { sinceMs: CURSOR_NOW - HOUR_MS, untilMs: CURSOR_NOW + 2 * 60 * 1000 },
1895
- ]);
1896
- }).pipe(
1897
- Effect.provide(
1898
- layerService({
1899
- prefix: "usage-service-cursor-restart",
1900
- home,
1901
- settings,
1933
+ const context = yield* Layer.build(
1934
+ layerCursorUsageAccounts(
1935
+ Layer.succeed(CursorAccountReader.CursorAccountReader, { read: cursor.read }),
1936
+ ).pipe(
1937
+ Layer.provide(
1938
+ layerService({
1939
+ prefix: "usage-service-cursor-shared-refresh",
1940
+ home,
1941
+ settings,
1942
+ platform: "darwin",
1943
+ }),
1944
+ ),
1945
+ ),
1946
+ );
1947
+ const accounts = Context.get(context, CursorUsageAccounts.CursorUsageAccounts);
1948
+ const input = {
1949
+ instances: [
1950
+ {
1951
+ instanceId: ProviderInstanceId.make("cursor"),
1952
environment: { CURSOR_API_KEY: "test-key" },
1903
- }),
1953
+ },
1954
+ ],
1955
+ windowStartMs: 0,
1956
+ retentionCutoffMs: CURSOR_NOW - 92 * 24 * HOUR_MS,
1957
+ awaitRefresh: false,
1958
+ };
1959
+ const narrow = yield* accounts.scan(input);
1960
+ assert.isTrue(narrow[0]?.refreshing);
1961
+ yield* Deferred.await(cursor.state.started);
1962
+ const wider = yield* accounts
1963
+ .scan({ ...input, retentionCutoffMs: 0, awaitRefresh: true })
1964
+ .pipe(Effect.forkChild({ startImmediately: true }));
1965
+ assert.strictEqual(cursor.state.calls.length, 1);
1966
+ yield* Deferred.succeed(cursor.state.gate, undefined);
1967
+ const sources = yield* Fiber.join(wider);
1968
+ assert.deepStrictEqual(cursor.state.calls, [
1969
+ { sinceMs: input.retentionCutoffMs, untilMs: CURSOR_NOW },
1970
+ { sinceMs: 0, untilMs: CURSOR_NOW },
1971
+ ]);
1972
+ assert.strictEqual(sources[0]?.status, "ok");
1973
+ assert.deepStrictEqual(
1974
+ sources[0]?.files?.flatMap((file) =>
1975
+ file.records.map(({ timestampMs, totals }) => ({
1976
+ timestampMs,
1977
+ outputTokens: totals.outputTokens,
1978
+ })),
1979
),
1980
+ cursor.state.events,
1981
);
1982
+ yield* accounts.awaitPersisted;
1983
}),
1984
);
1985
@@ -3536,6 +3613,116 @@ describe("UsageService", () => {
3613
}),
3614
);
3615
3616
+ it.effect.each([
3617
+ { label: "the default 92 days", retention: undefined, expected: 1 },
3618
+ { label: "unlimited", retention: "unlimited" as const, expected: 15 },
3619
+ ])(
3620
+ "retains deleted transcripts according to $label across restart and settings changes",
3621
+ ({ retention, expected }) =>
3622
+ Effect.gen(function* () {
3623
+ const { transcript, settings, home } = yield* setup;
3624
+ const now = Date.parse("2026-08-02T12:00:00Z");
3625
+ yield* TestClock.setTime(now);
3626
+ const window = { ...WINDOW, sinceDay: UsageDay.make("2024-01-01") };
3627
+ const files = [92, 93, 180, 730].map((days, index) => ({
3628
+ path: `${transcript}-${index}.jsonl`,
3629
+ timestamp: now - days * 86400000,
3630
+ content: claudeLine(index, 2 ** index).replace(
3631
+ "2026-08-01T10:00:00Z",
3632
+ DateTime.formatIso(DateTime.makeUnsafe(now - days * 86400000)),
3633
+ ),
3634
+ }));
3635
+ yield* Effect.promise(async () => {
3636
+ for (const file of files) {
3637
+ await NodeFSP.writeFile(file.path, file.content);
3638
+ await NodeFSP.utimes(file.path, file.timestamp / 1000, file.timestamp / 1000);
3639
+ }
3640
+ });
3641
+ yield* Effect.gen(function* () {
3642
+ const service = yield* UsageService.make;
3643
+ const first = yield* service.readSummary(window);
3644
+ assert.strictEqual(totalOutputTokens(first), 15);
3645
+ yield* service.awaitPersisted;
3646
+ yield* Effect.promise(() => Promise.all(files.map((file) => NodeFSP.rm(file.path))));
3647
+ const restarted = yield* UsageService.make;
3648
+ const saved = yield* restarted.readSummary(window);
3649
+ assert.strictEqual(totalOutputTokens(saved), expected);
3650
+ assert.deepStrictEqual(saved.sources[0]?.fingerprint, first.sources[0]?.fingerprint);
3651
+ const narrow = yield* restarted.readSummary(WINDOW);
3652
+ assert.strictEqual(totalOutputTokens(narrow), 0);
3653
+ assert.strictEqual(narrow.sources[0]?.distinctSessions, 0);
3654
+
3655
+ const config = yield* ServerSettings.ServerSettingsService;
3656
+ yield* config.updateSettings({ usageHistoryRetentionDays: 1 });
3657
+ assert.strictEqual(totalOutputTokens(yield* restarted.readSummary(window)), 0);
3658
+ yield* restarted.awaitPersisted;
3659
+ yield* config.updateSettings({ usageHistoryRetentionDays: "unlimited" });
3660
+ const afterPruning = yield* UsageService.make;
3661
+ assert.strictEqual(totalOutputTokens(yield* afterPruning.readSummary(window)), 0);
3662
+ }).pipe(
3663
+ Effect.provide(
3664
+ layerService({
3665
+ prefix: "usage-service-retention",
3666
+ home,
3667
+ settings: {
3668
+ ...settings,
3669
+ ...(retention === undefined ? {} : { usageHistoryRetentionDays: retention }),
3670
+ },
3671
+ }),
3672
+ ),
3673
+ );
3674
+ }),
3675
+ );
3676
+
3677
+ it.effect("keeps retained history when an obsolete scan finishes after retention widens", () =>
3678
+ Effect.gen(function* () {
3679
+ const { transcript, settings, home } = yield* setup;
3680
+ yield* TestClock.setTime(CURSOR_NOW);
3681
+ const timestamp = CURSOR_NOW - 180 * 24 * HOUR_MS;
3682
+ yield* Effect.promise(async () => {
3683
+ await NodeFSP.writeFile(
3684
+ transcript,
3685
+ claudeLine(1, 11).replace(
3686
+ "2026-08-01T10:00:00Z",
3687
+ DateTime.formatIso(DateTime.makeUnsafe(timestamp)),
3688
+ ),
3689
+ );
3690
+ await NodeFSP.utimes(transcript, timestamp / 1000, timestamp / 1000);
3691
+ });
3692
+ const started = yield* Deferred.make<void>();
3693
+ const release = yield* Deferred.make<void>();
3694
+ let scans = 0;
3695
+ yield* Effect.gen(function* () {
3696
+ const config = yield* ServerSettings.ServerSettingsService;
3697
+ const service = yield* UsageService.make.pipe(
3698
+ Effect.provideService(CursorUsageAccounts.CursorUsageAccounts, {
3699
+ scan: () =>
3700
+ Effect.gen(function* () {
3701
+ if (++scans === 1) {
3702
+ yield* Deferred.succeed(started, undefined);
3703
+ yield* Deferred.await(release);
3704
+ }
3705
+ return [];
3706
+ }),
3707
+ awaitPersisted: Effect.void,
3708
+ }),
3709
+ );
3710
+ const window = { ...WINDOW, sinceDay: UsageDay.make("2024-01-01") };
3711
+ const oldScan = yield* service.readSummary(window).pipe(Effect.forkChild);
3712
+ yield* Deferred.await(started);
3713
+ yield* config.updateSettings({ usageHistoryRetentionDays: "unlimited" });
3714
+ assert.strictEqual(totalOutputTokens(yield* service.readSummary(window)), 11);
3715
+ yield* service.awaitPersisted;
3716
+ yield* Effect.promise(() => NodeFSP.rm(transcript));
3717
+ yield* Deferred.succeed(release, undefined);
3718
+ yield* Fiber.join(oldScan);
3719
+ yield* service.awaitPersisted;
3720
+ const restarted = yield* UsageService.make;
3721
+ assert.strictEqual(totalOutputTokens(yield* restarted.readSummary(window)), 11);
3722
+ }).pipe(Effect.provide(layerService({ prefix: "usage-retention-race", home, settings })));
3723
+ }),
3724
+ );
3725
+
3726
it.effect("credits the same copy of a duplicate after its transcripts are deleted", () =>
3727
Effect.gen(function* () {
3728
const { transcript, settings, home } = yield* setup;
apps/server/src/usage/UsageService.ts
+24
-10
@@ -77,7 +77,6 @@ const RATES_REFRESH_FLOOR_MS = 60 * 1000;
77
const MTIME_SLACK_MS = 36 * 60 * 60 * 1000;
78
const MAX_HOURLY_WINDOW_MS = 24 * 60 * 60 * 1000;
79
80
-const CACHE_RETENTION_DAYS = 92;
80
const STALE_TEMP_DIRECTORY_MS = 60 * 60 * 1000;
81
82
const TRANSCRIPT_READ_CONCURRENCY = 4;
@@ -872,7 +871,10 @@ export const make = Effect.gen(function* () {
871
const windowStartMs =
872
(hourlyWindow?.sinceTimeMs ?? DateTime.toEpochMillis(windowStart.value)) - MTIME_SLACK_MS;
873
875
- const retentionCutoffMs = startedAtMs - CACHE_RETENTION_DAYS * 24 * 60 * 60 * 1000;
874
+ const retentionCutoffMs =
875
+ settings.usageHistoryRetentionDays === "unlimited"
876
+ ? 0
877
+ : Math.max(0, startedAtMs - settings.usageHistoryRetentionDays * 24 * 60 * 60 * 1000);
878
879
const awaitRefresh = input.awaitRefresh === true;
880
const collect = Effect.gen(function* () {
@@ -1144,14 +1146,26 @@ export const make = Effect.gen(function* () {
1146
});
1147
}
1148
1147
- const pruned = pruneScanCache(fileCache, retentionCutoffMs);
1148
- if (pruned > 0) {
1149
- cacheDirty = true;
1150
- // Pruned files are no longer saved, so nothing before the cutoff stays covered.
1151
- for (const [key, since] of coveredSince) {
1152
- if (since < retentionCutoffMs) coveredSince.set(key, retentionCutoffMs);
1153
- }
1154
- }
1149
+ yield* settingsService
1150
+ .withSettingsSnapshot((currentSettings) =>
1151
+ Effect.sync(() => {
1152
+ if (settings.usageHistoryRetentionDays === currentSettings.usageHistoryRetentionDays) {
1153
+ const pruned = pruneScanCache(fileCache, retentionCutoffMs);
1154
+ if (pruned > 0) {
1155
+ cacheDirty = true;
1156
+ // Pruned files are no longer saved, so nothing before the cutoff stays covered.
1157
+ for (const [key, since] of coveredSince) {
1158
+ if (since < retentionCutoffMs) coveredSince.set(key, retentionCutoffMs);
1159
+ }
1160
+ }
1161
+ }
1162
+ }),
1163
+ )
1164
+ .pipe(
1165
+ Effect.catchCause((cause) =>
1166
+ Effect.logWarning("failed to read settings before pruning the usage cache", { cause }),
1167
+ ),
1168
+ );
1169
1170
const aggregated = aggregator.finish();
1171
const readAt = yield* DateTime.now;
docs/user/usage.md
+7
@@ -20,6 +20,13 @@ record are missing from the totals.
20
Text generation (thread titles, commit messages, PR text, branch names and review guides) is not in
21
the totals for Codex or Claude Code 2.0.63 and newer, because those runs save no session history.
22
23
+The server keeps scanned usage history for 92 days by default. Claude Code deletes its own
24
+transcripts after 30 days, so for older Claude usage this history is the only record left. To keep
25
+it longer, set `usageHistoryRetentionDays` in `<T3 home>/userdata/settings.json` to a number of days
26
+such as `365`, or to `"unlimited"`. Usage views still show at most 90 days. Lowering the value
27
+prunes older history on the next refresh, and raising it again does not bring pruned history back.
28
+While `settings.json` cannot be read, nothing is pruned.
29
+
30
OpenCode reads its SQLite database and older JSON history. Antigravity reads local conversation
31
databases, including T3-managed profiles. Set `OPENCODE_DATA_DIR` or `ANTIGRAVITY_DATA_DIR` on the
32
server to read a different data directory; comma-separated paths read multiple directories.
packages/contracts/src/settings.ts
+11
@@ -1348,6 +1348,11 @@ export const StorageCleanupSettings = Schema.Struct({
1348
});
1349
export type StorageCleanupSettings = typeof StorageCleanupSettings.Type;
1350
1351
+const UsageHistoryRetentionDays = Schema.Union([
1352
+ Schema.Int.check(Schema.isGreaterThan(0)),
1353
+ Schema.Literal("unlimited"),
1354
+]);
1355
+
1356
const CloudSpeechCredential = SettingsSecretValue.pipe(
1357
Schema.withDecodingDefault(Effect.succeed("")),
1358
);
@@ -1583,6 +1588,10 @@ export const ServerSettings = Schema.Struct({
1588
Schema.withDecodingDefault(Effect.succeed({})),
1589
),
1590
1591
+ usageHistoryRetentionDays: UsageHistoryRetentionDays.pipe(
1592
+ Schema.withDecodingDefault(Effect.succeed(92)),
1593
+ ),
1594
+
1595
usagePriceOverrides: Schema.Record(TrimmedNonEmptyString, UsageModelPriceOverride).pipe(
1596
Schema.withDecodingDefault(Effect.succeed({})),
1597
),
@@ -1971,6 +1980,8 @@ export const ServerSettingsPatch = Schema.Struct({
1980
Schema.Record(SecretProviderId, Schema.NullOr(SecretProviderConfigWrite)),
1981
),
1982
1983
+ usageHistoryRetentionDays: Schema.optionalKey(UsageHistoryRetentionDays),
1984
+
1985
usagePriceOverrides: Schema.optionalKey(
1986
Schema.Record(TrimmedNonEmptyString, Schema.NullOr(UsageModelPriceOverride)),
1987
),
packages/provider-cursor/src/server/CursorUsageAccounts.ts
+11
-3
@@ -198,11 +198,19 @@ const make = Effect.gen(function* () {
198
let refreshing = false;
199
if (
200
(recentFailure === undefined || nowMs - recentFailure.atMs >= CURSOR_ACCOUNT_TTL_MS) &&
201
- !isCursorCacheFresh(caches.get(credentialKey), nowMs)
201
+ !isCursorCacheFresh(caches.get(credentialKey), nowMs, retentionStartMs)
202
) {
203
const refresh = yield* startRefresh(credential, credentialKey, retentionStartMs);
204
- if (awaitRefresh) yield* Deferred.await(refresh);
205
- else refreshing = true;
204
+ if (awaitRefresh) {
205
+ yield* Deferred.await(refresh);
206
+ if (
207
+ !failures.has(credentialKey) &&
208
+ !isCursorCacheFresh(caches.get(credentialKey), nowMs, retentionStartMs)
209
+ ) {
210
+ const retry = yield* startRefresh(credential, credentialKey, retentionStartMs);
211
+ yield* Deferred.await(retry);
212
+ }
213
+ } else refreshing = true;
214
}
215
216
const cache = caches.get(credentialKey);
packages/provider-cursor/src/server/accountCache.test.ts
+28
-1
@@ -1,6 +1,6 @@
1
import { assert, describe, it } from "@effect/vitest";
2
3
-import { cursorFetchRange, mergeCursorFetch } from "./accountCache.ts";
3
+import { cursorFetchRange, isCursorCacheFresh, mergeCursorFetch } from "./accountCache.ts";
4
import type { UsageRecord } from "@t3tools/provider-core/server/usage";
5
6
const DAY_MS = 24 * 60 * 60 * 1000;
@@ -31,6 +31,20 @@ describe("cursorAccountCache", () => {
31
fetchedAtMs: 10 * DAY_MS,
32
records: [record(5 * DAY_MS)],
33
};
34
+ assert.isTrue(isCursorCacheFresh(cache, 10 * DAY_MS, 0));
35
+ const narrow = { ...cache, sinceMs: DAY_MS };
36
+ assert.isFalse(isCursorCacheFresh(narrow, 10 * DAY_MS, 0));
37
+ const widenedRange = cursorFetchRange(narrow, 0, 10 * DAY_MS);
38
+ assert.deepStrictEqual(widenedRange, {
39
+ sinceMs: 0,
40
+ untilMs: 10 * DAY_MS,
41
+ });
42
+ const widened = mergeCursorFetch(narrow, "a", widenedRange, cache.records, 10 * DAY_MS, 0);
43
+ assert.isTrue(isCursorCacheFresh(widened.cache, 10 * DAY_MS, 0));
44
+ assert.deepStrictEqual(cursorFetchRange(widened.cache, 0, 11 * DAY_MS), {
45
+ sinceMs: 10 * DAY_MS - 60 * 60 * 1000,
46
+ untilMs: 11 * DAY_MS,
47
+ });
48
const nowMs = 100 * DAY_MS;
49
const range = cursorFetchRange(cache, 10 * DAY_MS, nowMs);
50
assert.deepStrictEqual(range, { sinceMs: 10 * DAY_MS, untilMs: nowMs });
@@ -75,6 +89,19 @@ describe("cursorAccountCache", () => {
89
);
90
assert.isTrue(pruned.changed);
91
assert.deepStrictEqual(pruned.cache.records, [record(10 * DAY_MS)]);
92
+ assert.isTrue(
93
+ mergeCursorFetch(cache, "a", range, [record(10 * DAY_MS)], 11 * DAY_MS, 2 * DAY_MS).changed,
94
+ );
95
+ assert.isTrue(
96
+ mergeCursorFetch(
97
+ { ...cache, sinceMs: DAY_MS, records: [] },
98
+ "a",
99
+ { sinceMs: 0, untilMs: 11 * DAY_MS },
100
+ [],
101
+ 11 * DAY_MS,
102
+ 0,
103
+ ).changed,
104
+ );
105
assert.isTrue(
106
mergeCursorFetch(
107
cache,
packages/provider-cursor/src/server/accountCache.ts
+7
-1
@@ -25,8 +25,13 @@ interface CursorFetchRange {
25
export function isCursorCacheFresh(
26
cache: CursorAccountCache | undefined,
27
nowMs: number,
28
+ retentionStartMs: number,
29
): cache is CursorAccountCache {
29
- return cache !== undefined && nowMs - cache.fetchedAtMs < CURSOR_ACCOUNT_TTL_MS;
30
+ return (
31
+ cache !== undefined &&
32
+ cache.sinceMs <= retentionStartMs &&
33
+ nowMs - cache.fetchedAtMs < CURSOR_ACCOUNT_TTL_MS
34
+ );
35
}
36
37
export function cursorFetchRange(
@@ -61,6 +66,7 @@ export function mergeCursorFetch(
66
}
67
const changed =
68
cache === undefined ||
69
+ cache.sinceMs > retentionStartMs ||
70
pruned ||
71
replacedKeys.size !== added.length ||
72
added.some((record) => !replacedKeys.has(record.dedupeKey));