Repository navigation
fix(gateway): read usage-cost cache once per agent in sessions.usage - #100054
Conversation
|
Codex review: needs maintainer review before merge. Reviewed July 4, 2026, 6:36 PM ET / 22:36 UTC. Summary PR surface: Source -48, Tests -24. Total -72 across 2 files. Reproducibility: yes. at source/proof level. Current main builds one visible-session cache-summary task per included visible session and that helper reads/parses the durable cache, while Testbox proof showed the validated head eliminating the RSS spike on a real session/cache setup. Review metrics: 1 noteworthy metric.
Stored data model Root-cause cluster Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Merge readiness Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch. Risk before merge
Maintainer options:
Next step before merge
Security Review detailsBest possible solution: Land the current-head batched-loader fix after normal gates, and keep broader gateway usage isolation or cache-storage redesign work in separate follow-up items. Do we have a high-confidence way to reproduce the issue? Yes, at source/proof level. Current main builds one visible-session cache-summary task per included visible session and that helper reads/parses the durable cache, while Testbox proof showed the validated head eliminating the RSS spike on a real session/cache setup. Is this the best way to solve the issue? Yes. Reusing the existing batched helper is the narrowest maintainable fix for this bug; threshold tuning, dashboard debounce, a new memoization layer, or broader gateway isolation would be less direct for the reported false-positive RSS growth. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 65e12328aa20. Label changesLabel justifications:
Evidence reviewedPR surface: Source -48, Tests -24. Total -72 across 2 files. View PR surface stats
What I checked:
Likely related people:
What the crustacean ranks mean
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics. How this review workflow works
Review history (5 earlier review cycles)
|
|
maintainer validation is complete.
@clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. |
The sessions.usage RPC loaded each visible session's summary via loadSessionCostSummaryFromCache, which re-reads and re-parses the entire .usage-cost-cache.json on every call. At the dashboard's default limit=50 that is ~50 full parses per connect, spiking RSS 200-600 MiB and tripping the gateway rss_growth memory-pressure alarm with a false positive. Unify the visible and hidden session paths onto the batched loadSessionCostSummariesFromCache loader, which reads and parses the cache at most once per agent regardless of limit. Fixes openclaw#100041
c61b347 to
1d19215
Compare
|
Merged via squash.
|
…penclaw#100054) The sessions.usage RPC loaded each visible session's summary via loadSessionCostSummaryFromCache, which re-reads and re-parses the entire .usage-cost-cache.json on every call. At the dashboard's default limit=50 that is ~50 full parses per connect, spiking RSS 200-600 MiB and tripping the gateway rss_growth memory-pressure alarm with a false positive. Unify the visible and hidden session paths onto the batched loadSessionCostSummariesFromCache loader, which reads and parses the cache at most once per agent regardless of limit. Fixes openclaw#100041
Closes #100041
What Problem This Solves
Resolves an issue where operators running the openclaw-control-ui dashboard get chronic false-positive
memory pressure: level=warning reason=rss_growthwarnings that advise restarting the gateway when nothing is wrong. On every dashboard connect/reconnect, the bootstrap RPC batch callssessions.usage(defaultlimit=50), which spikes gateway RSS 200-600 MiB in a single ~30s window and trips therss_growthdiagnostic (threshold 512 MiB). GC reclaims it immediately, so it is a transient over-allocation, not a leak.Why This Change Was Made
The
sessions.usagehandler loaded each visible session's summary throughloadSessionCostSummaryFromCache, and that function unconditionally does a fullfs.readFile+JSON.parseof the entire.usage-cost-cache.jsonon every call with no request-scoped memoization. Atlimit=50that is ~50 full re-parses of the whole cache per connect — the burst tracks thelimitparameter, not on-disk store size (the reporter confirmedlimit=1→ ~19 MiB vslimit=50→ ~450 MiB on an identical store).The hidden-session path already avoided this by using the batched
loadSessionCostSummariesFromCache, which reads and parses the cache at most once per agent. This change unifies the visible and hidden session paths onto that batched loader, so the cache is read once per agent regardless oflimit. The visible path already ran inbackgroundrefresh mode, so no refresh semantics change.User Impact
A routine dashboard connect no longer allocates hundreds of MiB or trips the memory-pressure alarm. Cache read/parse cost for
sessions.usageis now O(agents) instead of O(sessions requested), and no longer grows with thelimitrequest parameter.Evidence
node scripts/run-vitest.mjs src/gateway/server-methods/usage.sessions-usage.test.ts→ 36 passed. Tests updated to assert the whole cache is read once per agent (loadSessionCostSummariesFromCachecalled once) rather than once per session.pnpm tsgo:coreandpnpm check:test-types→ clean.git diff --numstat: net -49 prod LOC (removes the duplicate per-session load path).fills every calendar day ... spring-forward) fails on this macOS checkout at the base commit too, unrelated to this change (timezone/Intlenvironment dependent; CI truth is Linux Node 24).Maintainer Validation
tbx_01kwqd945cj8jmjdffd0j8g40t(jade-prawn), exact base vs exact head with 50 real session transcripts and a ~20 MiB durable usage cache: base transient Gateway RSS growth+1431.01 MiB; head+0.11 MiB. Both RPC calls returned all 50 sessions; the head stays safely below the512 MiBrss_growthwarning threshold.corepack pnpm test:serial src/gateway/server-methods/usage.sessions-usage.test.ts-> 36/36 passed.corepack pnpm check:changed -- src/gateway/server-methods/usage.ts src/gateway/server-methods/usage.sessions-usage.test.ts-> passed on the same Testbox.