Files
openclaw/src
Feelw00 8bf57e8bde fix(gateway): bound costUsageCache with MAX + FIFO eviction
Regression: `costUsageCache` in `src/gateway/server-methods/usage.ts` had no
delete/prune/evict path. The TTL check at L310 only gates stale reads — on a
miss after expiry, `set()` overwrites the same key but never removes stale
keys. `parseDateRange` derives cacheKey from `getTodayStartMs`, so cacheKey
rolls at every UTC 00:00, and additional axes (days / startDate / endDate /
utcOffset) multiply cardinality. The macOS menu polls `usage.cost` every ~45s
with no params, exercising `parseDateRange`'s default branch every day. Over
gateway uptime the map grows monotonically.

Three sibling caches in the same subsystem already implement MAX + FIFO
eviction (resolvedSessionKeyByRunId, TRANSCRIPT_SESSION_KEY_CACHE,
sessionTitleFieldsCache). This change mirrors their pattern:

- `COST_USAGE_CACHE_MAX = 256` (matches RUN_LOOKUP_CACHE_LIMIT and
  TRANSCRIPT_SESSION_KEY_CACHE_MAX).
- New `setCostUsageCache(cacheKey, entry)` helper checks size + evicts
  `keys().next().value` when adding a new key would exceed the cap.
- The three existing `costUsageCache.set(...)` call sites now route through
  the helper. TTL-on-read, in-flight dedup, and overwrite-on-same-key
  semantics are preserved.

Adds `src/gateway/server-methods/usage.cost-usage-cache.test.ts` which drives
growth through `__test.loadCostUsageSummaryCached` with 600 distinct
(startMs, endMs) pairs (mirrors day rollover + range switches). Pre-fix the
Map grows to 600; post-fix it plateaus, the last key is retained, and the
first key is evicted (FIFO).

AI-assisted (fully tested). 432 server-methods tests pass, pnpm check +
pnpm build clean.
2026-04-21 07:53:44 +01:00
..
2026-04-20 23:33:21 +01:00
2026-04-21 12:01:06 +05:30
2026-04-20 20:30:16 +01:00
2026-04-18 23:16:47 +01:00
2026-04-20 19:55:44 +01:00
2026-04-18 20:35:20 +01:00
2026-04-18 20:35:20 +01:00