Skip to content

Commit 99e8cf2

Browse files
committed
fix(web): bound tool cache expiry clocks
1 parent e780a6b commit 99e8cf2

2 files changed

Lines changed: 63 additions & 6 deletions

File tree

‎src/agents/tools/web-shared.test.ts‎

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
import { describe, expect, it } from "vitest";
1+
import { afterEach, describe, expect, it, vi } from "vitest";
22
import { MAX_TIMER_TIMEOUT_SECONDS } from "../../shared/number-coercion.js";
3-
import { resolvePositiveTimeoutSeconds, resolveTimeoutSeconds } from "./web-shared.js";
3+
import {
4+
readCache,
5+
resolvePositiveTimeoutSeconds,
6+
resolveTimeoutSeconds,
7+
writeCache,
8+
type CacheEntry,
9+
} from "./web-shared.js";
10+
11+
afterEach(() => {
12+
vi.restoreAllMocks();
13+
});
414

515
describe("web shared timeout seconds", () => {
616
it("caps timeoutSeconds at the shared timer-safe ceiling", () => {
@@ -16,4 +26,41 @@ describe("web shared timeout seconds", () => {
1626
expect(resolvePositiveTimeoutSeconds(0, 30)).toBe(30);
1727
expect(resolvePositiveTimeoutSeconds(1.9, 30)).toBe(1);
1828
});
29+
30+
it("drops cached values while the process clock is invalid", () => {
31+
const cache = new Map<string, CacheEntry<string>>();
32+
writeCache(cache, "key", "old", 60_000);
33+
expect(readCache(cache, "key")?.value).toBe("old");
34+
35+
vi.spyOn(Date, "now").mockReturnValue(8_640_000_000_000_001);
36+
expect(readCache(cache, "key")).toBeNull();
37+
38+
vi.mocked(Date.now).mockReturnValue(1_000);
39+
expect(readCache(cache, "key")).toBeNull();
40+
});
41+
42+
it("does not write cache values when expiry would exceed the Date range", () => {
43+
const cache = new Map<string, CacheEntry<string>>();
44+
vi.spyOn(Date, "now").mockReturnValue(8_640_000_000_000_000);
45+
46+
writeCache(cache, "key", "value", 60_000);
47+
48+
expect(cache.size).toBe(0);
49+
expect(readCache(cache, "key")).toBeNull();
50+
});
51+
52+
it("does not evict valid entries when an invalid expiry cannot be cached", () => {
53+
const cache = new Map<string, CacheEntry<string>>();
54+
for (let index = 0; index < 100; index += 1) {
55+
writeCache(cache, `key-${index}`, `value-${index}`, 60_000);
56+
}
57+
expect(cache.get("key-0")?.value).toBe("value-0");
58+
59+
vi.spyOn(Date, "now").mockReturnValue(8_640_000_000_000_000);
60+
writeCache(cache, "invalid", "value", 60_000);
61+
62+
expect(cache.size).toBe(100);
63+
expect(cache.get("key-0")?.value).toBe("value-0");
64+
expect(cache.has("invalid")).toBe(false);
65+
});
1966
});

‎src/agents/tools/web-shared.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { MAX_TIMER_TIMEOUT_SECONDS } from "../../shared/number-coercion.js";
1+
import {
2+
asDateTimestampMs,
3+
MAX_TIMER_TIMEOUT_SECONDS,
4+
resolveExpiresAtMsFromDurationMs,
5+
} from "../../shared/number-coercion.js";
26
import { normalizeLowercaseStringOrEmpty } from "../../shared/string-coerce.js";
37

48
export type CacheEntry<T> = {
@@ -40,7 +44,8 @@ export function readCache<T>(
4044
if (!entry) {
4145
return null;
4246
}
43-
if (Date.now() > entry.expiresAt) {
47+
const now = asDateTimestampMs(Date.now());
48+
if (now === undefined || now > entry.expiresAt) {
4449
cache.delete(key);
4550
return null;
4651
}
@@ -56,6 +61,11 @@ export function writeCache<T>(
5661
if (ttlMs <= 0) {
5762
return;
5863
}
64+
const now = Date.now();
65+
const expiresAt = resolveExpiresAtMsFromDurationMs(ttlMs, { nowMs: now });
66+
if (expiresAt === undefined) {
67+
return;
68+
}
5969
if (cache.size >= DEFAULT_CACHE_MAX_ENTRIES) {
6070
const oldest = cache.keys().next();
6171
if (!oldest.done) {
@@ -64,8 +74,8 @@ export function writeCache<T>(
6474
}
6575
cache.set(key, {
6676
value,
67-
expiresAt: Date.now() + ttlMs,
68-
insertedAt: Date.now(),
77+
expiresAt,
78+
insertedAt: now,
6979
});
7080
}
7181

0 commit comments

Comments
 (0)