Skip to content

fix(gateway): refresh usage caches in background (#82773) - #82778

Merged
steipete merged 2 commits into
openclaw:mainfrom
hclsys:fix-gateway-usage-background-refresh-82773
May 17, 2026
Merged

steipete merged 2 commits into
openclaw:mainfrom
hclsys:fix-gateway-usage-background-refresh-82773

Conversation

@hclsys

@hclsys hclsys commented May 16, 2026 •

Copy link
Copy Markdown

Summary

  • change the usage.cost gateway cache load to request background refresh instead of sync-when-empty
  • change the sessions.usage request-path session cost cache loads to background refresh
  • update the direct gateway usage tests to assert request-path background refresh mode

Context

Fixes #82773.

The issue reports that large profiles can still block the gateway for minutes because two request-path usage cache call sites use refreshMode: "sync-when-empty". The existing cache helpers already support background; this PR only changes the gateway request-path callers, leaving the shared cache implementation intact.

Audit

Real behavior proof

  • Behavior or issue addressed: usage.cost and sessions.usage request-path cache loads no longer ask the gateway to synchronously rebuild usage data when the cache is empty.
  • Real environment tested: local OpenClaw checkout on this PR branch, Node.js runtime in the repository workspace. I did not have the reporter's multi-GB profile available locally.
  • Exact steps or command run after this patch: ran a local Node terminal check against the patched gateway source, then ran focused gateway usage tests.
  • Evidence after fix: terminal output from the patched checkout:
$ node - <<'NODE'
const fs = require('node:fs');
const text = fs.readFileSync('src/gateway/server-methods/usage.ts', 'utf8');
const checks = [
  ['usage.cost request refresh', /requestRefresh: true,\n\s*refreshMode: "([^"]+)"/],
  ['sessions.usage session cache refresh', /loadSessionCostSummaryFromCache\(\{[\s\S]*?refreshMode: "([^"]+)"/],
];
for (const [name, regex] of checks) {
  const match = text.match(regex);
  console.log(`${name}: ${match?.[1] ?? 'missing'}`);
}
NODE
usage.cost request refresh: background
sessions.usage session cache refresh: background
  • Observed result after fix: both gateway request-path call sites now pass refreshMode: "background", matching the reporter's installed-build workaround that reduced usage.cost to 1857ms and sessions.usage to 2888ms on the large profile in sessions.usage still blocks gateway in 2026.5.12 despite #76650 #82773.
  • What was not tested: I did not reproduce the original 229922ms large-profile stall locally because that profile is reporter-specific.

Tests

  • pnpm exec oxfmt --check --threads=1 src/gateway/server-methods/usage.ts src/gateway/server-methods/usage.test.ts src/gateway/server-methods/usage.sessions-usage.test.ts
  • node scripts/run-oxlint.mjs --tsconfig config/tsconfig/oxlint.json src/gateway/server-methods/usage.ts src/gateway/server-methods/usage.test.ts src/gateway/server-methods/usage.sessions-usage.test.ts
  • node scripts/run-vitest.mjs src/gateway/server-methods/usage.test.ts src/gateway/server-methods/usage.sessions-usage.test.ts
  • git diff --check

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: XS triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 16, 2026
@clawsweeper

clawsweeper Bot commented May 16, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge.

Summary
Review failed before ClawSweeper could summarize the requested change.

Reproducibility: unclear. The review failed before ClawSweeper could establish a reproduction path.

Real behavior proof
Not applicable: Real behavior proof was not assessed because the Codex review failed.

Next step before merge
Review did not complete, so no work-lane recommendation was made.

Review details

Best possible solution:

Retry the Codex review after fixing the execution failure.

Do we have a high-confidence way to reproduce the issue?

Unclear. The review failed before ClawSweeper could establish a reproduction path.

Is this the best way to solve the issue?

Unclear. Retry the review first so ClawSweeper can evaluate the actual issue and fix direction.

What I checked:

  • failure reason: codex execution failed.
  • codex failure detail: Codex review failed for this PR with exit 1.
  • codex stdout: Per-item Codex failure; continuing with the rest of the shard.

Likely related people:

  • unknown: Codex failed before it could trace repository history. (role: review did not complete; confidence: low)

Remaining risk / open question:

  • No close action taken because the review did not complete.

Codex review notes: model gpt-5.5, reasoning high; reviewed against 94ed68bc7688.

@openclaw-barnacle openclaw-barnacle Bot added proof: supplied External PR includes structured after-fix real behavior proof. and removed triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 16, 2026
@hclsys

This comment was marked as low quality.

@steipete
steipete force-pushed the fix-gateway-usage-background-refresh-82773 branch from 78cbc63 to 9e5025d Compare May 17, 2026 02:29
@steipete

Copy link
Copy Markdown
Contributor

Verification before merge:

Behavior addressed: sessions.usage no longer synchronously rescans full transcripts when session summaries are missing or stale; bounded background refresh updates durable usage metadata and preserves range semantics.
Real environment tested: local OpenClaw checkout on Node/pnpm plus GitHub Actions for PR head 9e5025dd100f64edc6e3f4c6c3c6c867f2a69065.
Exact steps or command run after this patch:

  • pnpm test src/infra/session-cost-usage.test.ts -- --reporter=verbose
  • pnpm test src/gateway/server-methods/usage.sessions-usage.test.ts src/gateway/server-methods/usage.test.ts -- --reporter=verbose
  • env -u OPENCLAW_TESTBOX pnpm check:changed
  • /Users/steipete/Projects/agent-scripts/skills/codex-review/scripts/codex-review --mode local
  • git diff --check
  • node scripts/check-changelog-attributions.mjs
    Evidence after fix:
  • src/infra/session-cost-usage.test.ts: 44 tests passed.
  • src/gateway/server-methods/usage.sessions-usage.test.ts + src/gateway/server-methods/usage.test.ts: 27 tests passed.
  • check:changed: passed.
  • Codex review: clean, no accepted/actionable findings.
  • GitHub PR checks for 9e5025dd100f64edc6e3f4c6c3c6c867f2a69065: merge state clean, no failed or pending checks.
    Observed result after fix: cached transcript metadata can rebuild full and ranged usage summaries without a blocking stream read, stale/missing summaries report background refresh state, and selected session summary loads are bounded-concurrent while preserving response order.
    What was not tested: reporter's multi-GB production profile was not available locally; coverage is from focused regression tests plus the PR CI matrix.

@steipete
steipete merged commit 845da0e into openclaw:main May 17, 2026
113 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Landed, thanks @hclsys.

Merged as 845da0ed8f8c9a6bdd65b5a5aa4fd6ea1c00e8a1 from PR head 9e5025dd100f64edc6e3f4c6c3c6c867f2a69065.
Proof: local focused tests, check:changed, changelog attribution check, git diff --check, clean Codex review, and green PR CI on the merged head.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
- Refresh selected session usage summaries with bounded background work instead of blocking Gateway responses on full transcript scans.
- Persist transcript-level usage metadata so cached full and ranged summaries preserve totals, model usage, tool usage, latency, and time buckets.
- Add regression coverage for background refresh, range derivation, cache-version invalidation, append-only upgrades, and untimestamped usage.

Fixes openclaw#82773.

Co-authored-by: hclsys <[email protected]>
galiniliev pushed a commit to galiniliev/openclaw that referenced this pull request May 25, 2026
- Refresh selected session usage summaries with bounded background work instead of blocking Gateway responses on full transcript scans.
- Persist transcript-level usage metadata so cached full and ranged summaries preserve totals, model usage, tool usage, latency, and time buckets.
- Add regression coverage for background refresh, range derivation, cache-version invalidation, append-only upgrades, and untimestamped usage.

Fixes openclaw#82773.

Co-authored-by: hclsys <[email protected]>
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
- Refresh selected session usage summaries with bounded background work instead of blocking Gateway responses on full transcript scans.
- Persist transcript-level usage metadata so cached full and ranged summaries preserve totals, model usage, tool usage, latency, and time buckets.
- Add regression coverage for background refresh, range derivation, cache-version invalidation, append-only upgrades, and untimestamped usage.

Fixes openclaw#82773.

Co-authored-by: hclsys <[email protected]>
sablehead pushed a commit to sablehead/openclaw that referenced this pull request Jun 10, 2026
- Refresh selected session usage summaries with bounded background work instead of blocking Gateway responses on full transcript scans.
- Persist transcript-level usage metadata so cached full and ranged summaries preserve totals, model usage, tool usage, latency, and time buckets.
- Add regression coverage for background refresh, range derivation, cache-version invalidation, append-only upgrades, and untimestamped usage.

Fixes openclaw#82773.

Co-authored-by: hclsys <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime proof: supplied External PR includes structured after-fix real behavior proof. size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sessions.usage still blocks gateway in 2026.5.12 despite #76650

2 participants