Skip to content

fix(usage): keep cost usage report refresh deadline on the monotonic clock - #186388

Merged
obviyus merged 2 commits into
openclaw:mainfrom
SunnyShu0925:fix/session-cost-usage-report-refresh-monotonic-241
Oct 28, 2026
Merged

obviyus merged 2 commits into
openclaw:mainfrom
SunnyShu0925:fix/session-cost-usage-report-refresh-monotonic-241

Conversation

@SunnyShu0925

@SunnyShu0925 SunnyShu0925 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

A wall-clock rollback during a stale cost/usage report read can stretch its intended ten-second publication wait by the amount of the rollback. A normal cost report can therefore stall for a minute or more behind a busy refresh owner.

Why This Change Was Made

SunnyShu's fix seeds the report-refresh deadline with performance.now() and computes its remaining budget with the same monotonic clock. Both reads surround awaited aggregate I/O, so mixing wall-clock corrections into that budget was incorrect.

The follow-up moves the rollback regression into the existing publication-budget table and reuses fixtures. Controlled monotonic timers and an independent Date.now() rewind replace the former ten-/hundred-second unit-test waits. Existing completion, stale/partial response, cancellation, and independent refresh-owner contracts remain covered.

User Impact

Usage reports retain the same committed totals and partial-result semantics when refresh publication remains busy, but their wait no longer inherits a wall-clock offset. Calendar ranges, persisted timestamps, refresh ownership, queue behavior, schemas and public APIs are unchanged. This bounds publication waiting, not report computation.

Evidence

Built Gateway proof and measured outputs, comparing main a055ff33bbe74d95357f5d77bedc2cebdbd89c48 with the candidate through the shipped usage.cost RPC:

Case Main Candidate
90-second process-local rollback, run 1 100.843 s 10.610 s
90-second process-local rollback, run 2 100.918 s 10.678 s
No-step control 10.537 s 10.529 s

Every report returned the same 30 tokens / 0.03 cost with refreshing status and one stale file while the actual built refresh-lock owner held publication. The fixture uses isolated synthetic state and a process-local clock preload; no system/VM clock or report callback was changed. Full setup and cleanup limitations are disclosed in the linked proof.

  • Both focused cost/usage suites pass: 54 tests, plus focused lint and formatting.
  • The controlled regression fails on main only for rollback, in about 1.1 seconds; completion, slow-publication and cancellation controls pass.
  • Full-PR delta: production +2/-2, tests +29/-29. No test ceiling or baseline change.

Merge Risk

Two-way door: reverting restores the prior elapsed-wait calculation without a storage or configuration migration. Blast radius is stale cost/usage aggregate reports waiting for refresh publication; ordinary timestamp and report-value semantics are unchanged.

@clawsweeper

clawsweeper Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Oct 27, 2026
@clawsweeper

clawsweeper Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

Measure the usage-report publication wait with monotonic time so wall-clock changes cannot extend its deadline.

Example: A stale usage.cost report encounters a busy refresh owner while Date.now moves backward by 90 seconds.

  • Before: The supplied Gateway runs returned after about 101 seconds.
  • After: The candidate returned after about 11 seconds with the same 30 tokens, 0.03 cost, and refreshing status.

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) Minimal owner-level correction with shipped Gateway comparisons, controlled regression coverage, and both prior findings resolved; existing CI remains pending.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The supplied Linux/Node 24.22.1 built-Gateway usage.cost output exercises the actual report owner and refresh lock under a process-local 90-second rollback: candidate waits remain near ten seconds, baseline waits grow near one hundred seconds, and committed totals and cache status agree; no-step controls also agree.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: A clock rollback can make a stale usage report wait roughly the rollback duration beyond its intended publication budget.
Reason: The change restores the existing ten-second wait contract, and the feature contributor supplied matching Gateway proof and a focused test follow-up.

Regression provenance

Possible regression — probable (reviewed change; reproduction). No predecessor PR is attributed.

Merge readiness

✅ Ready for maintainer review

Keep this PR: it fixes a current-main deadline bug with a minimal clock correction, and both prior findings are resolved; no introduced defects were found.

Priority: P2
Reviewed head: d642b555b1534c7a6fbf662ab83b4c97fcd07af4

Before merge

None.

Findings

None.

Agent review details

How this fits together

Gateway usage requests pass calendar ranges to the shared cost-cache runtime, which reads committed worker aggregates, requests stale-session refreshes, and returns totals after a bounded publication wait.

flowchart TD
  A[Usage client] --> B[Gateway usage RPC]
  B --> C[Cost report cache]
  C --> D[Shared report deadline]
  D --> E[Aggregate worker]
  D --> F[Refresh publication owner]
  E --> G[Committed usage totals]
  F --> G
Loading

Technical review

Best possible solution:

Merge the two-line monotonic deadline correction and consolidated regression once the existing checks pass.

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

Current-main source seeds a wall-clock deadline before awaited aggregate I/O and subtracts the corrected clock afterward; the supplied built-Gateway comparison demonstrates the resulting rollback delay, although this read-only review ran no reproduction.

Is this the best way to solve the issue?

This is the smallest complete fix: both elapsed-budget reads use the existing monotonic clock while calendar ranges, persisted timestamps, refresh ownership, and result semantics remain unchanged.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against e7f054619274.

Provenance checked

  • Aggregate report publication deadline keeps the original intent (858395b: Wait for current usage projections within a ten-second publication budget before returning a partial result.)
  • Publication-budget test table keeps the original intent (c27f3b8: Consolidate repeated test scaffolding while preserving lifecycle and cancellation contracts.)
  • Aggregate and direct-session test fixtures keeps the original intent (f8dcd1d: Reduce duplicated fixtures and assertions while retaining owner-level coverage.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 851a56b adds src/infra/session-cost-usage-cache-runtime.ts:385 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 851a56b068e7; files: src/infra/session-cost-usage-cache-runtime.ts)
  • SunnyShu: Raw commit 97765db adds src/infra/session-cost-usage-cache-runtime.ts:478 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 97765dbb8bf2; files: src/infra/session-cost-usage-cache-runtime.ts)

PR surface

Source 0, Tests 0. Total 0 across 3 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 2 2 0
Tests 2 29 29 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 31 31 0

Review metrics

Metric Value Why it matters
Production versus test delta Production +2/-2; tests +29/-29 The complete introduced diff fixes the existing owner without production growth and adds rollback coverage without net test-line growth.

Labels

Label changes:

  • add P2: Wall-clock rollback can substantially delay stale usage reports, with limited impact outside this reporting path.
  • add proof: sufficient: Contributor real behavior proof is sufficient.
  • add rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • P2: Wall-clock rollback can substantially delay stale usage reports, with limited impact outside this reporting path.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (1 earlier review cycle)
  • reviewed 2026-10-27T23:47:53.412Z sha 3fc7215 :: needs real behavior proof before merge. :: [P2] Keep the rollback regression within the enforced test-line budget | [P3] Drive the deadline regression with controlled elapsed time

Reviewed October 27, 2026, 9:05 PM ET / October 28, 2026, 01:05 UTC (Revision 2).

@obviyus

obviyus commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

Built Gateway clock-step proof

Baseline main: a055ff33bbe74d95357f5d77bedc2cebdbd89c48. Candidate: d642b555b1534 (production code unchanged from contributor head 3fc72155659a67f4c79c9ceaec4e998e4f117bb7; the follow-up consolidates tests only).

The experiment runs real built Gateways in secretless Linux/Node 24.22.1 isolation. A task-owned synthetic transcript is primed to 30 tokens / 0.03 cost. The actual built refresh-lock owner then holds publication while another 70-token entry makes the source stale. The shipped usage.cost Gateway RPC is measured from an independent monotonic client clock. A preload changes only that Gateway process's Date.now() by -90 seconds when the report's first real aggregate worker request is observed, after the refresh deadline was seeded. The system/VM clock is never changed. No report or refresh callback is mocked.

Ref / case RPC elapsed Returned tokens / cost Cache state
Main, rollback 1 100.843 s 30 / 0.03 refreshing; 1 stale file
Main, rollback 2 100.918 s 30 / 0.03 refreshing; 1 stale file
Candidate, rollback 1 10.610 s 30 / 0.03 refreshing; 1 stale file
Candidate, rollback 2 10.678 s 30 / 0.03 refreshing; 1 stale file
Main, no step 10.537 s 30 / 0.03 refreshing; 1 stale file
Candidate, no step 10.529 s 30 / 0.03 refreshing; 1 stale file

The candidate retains the same partial committed projection and totals while respecting the ten-second publication wait. This does not claim to bound report computation itself. The first baseline response was captured successfully before an auxiliary cleanup client hit a stale keep-alive socket; the proof control transport was corrected, and subsequent runs released the real lock and shut down their Gateways. That cleanup issue is not counted as a product failure.

The former standalone real-time rollback test was folded into the existing publication-budget table. It now advances fake monotonic timers while independently rewinding Date.now(), after warming real aggregate I/O and observing the held refresh. The baseline fails only the rollback case in about 1.1 seconds; its completes, slow, and cancellation controls pass. The candidate passes both focused suites: 54 tests, focused lint and formatting. Existing partial-response and independent refresh-owner assertions remain.

Whole-PR line delta: production +2/-2 (net 0), tests +29/-29 (net 0). No baseline or ceiling change. The production fix remains SunnyShu's two-line clock-domain correction; fixture reuse removes duplicated setup rather than adding another clock seam.

@obviyus

obviyus commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 28, 2026
@obviyus
obviyus merged commit 2e25064 into openclaw:main Oct 28, 2026
128 of 129 checks passed
@obviyus

obviyus commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

Landed in 2e25064. Thanks @SunnyShu0925. Cost usage reports no longer wait out a clock rollback.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 28, 2026
…clock (openclaw#186388)

fix(usage): keep the cost usage report refresh deadline on the monotonic clock

The cost usage report's refresh deadline was measured with the wall clock, so after a backward clock step a report waited for the full size of the step before returning. The deadline now uses monotonic time; totals and partial-cache behavior are unchanged.

Evidence: built Gateway usage.cost reports with Date.now stepped back 90 seconds inside the process only. On main the report took 100.8s and 100.9s (twice); with this change 10.6s and 10.7s (twice), matching the no-step runs of about 10.5s on both refs. Details: openclaw#186388 (comment).

Thanks @SunnyShu0925.

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

Labels

P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants