Skip to content

fix(agents): keep the shared TPM retry window on the monotonic clock - #186390

Merged
obviyus merged 5 commits into
openclaw:mainfrom
SunnyShu0925:fix/shared-tpm-retry-window-monotonic-244
Oct 28, 2026
Merged

obviyus merged 5 commits into
openclaw:mainfrom
SunnyShu0925:fix/shared-tpm-retry-window-monotonic-244

Conversation

@SunnyShu0925

@SunnyShu0925 SunnyShu0925 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Wall-clock corrections can prematurely exhaust or extend the shared organization TPM retry window. The original monotonic-controller correction also left the provider queue comparing uptime-valued deadlines against epoch-valued floors, rejecting otherwise valid retries before transport.

Why This Change Was Made

SunnyShu's monotonic retry-window producer is retained, and the conversion is completed at the shared admission owner:

  • Private retry floors, pacing, expiry, deadline checks and timer subtraction use performance.now() consistently.
  • Existing onWait and rate-limit state timestamps are projected back to epoch time at their status boundaries. Absolute HTTP-date Retry-After parsing still uses wall time. No SDK fields, names or source paths change.
  • Current main's fallback-reservation owner is preserved. Its epoch-valued run origin is projected privately into monotonic time once, so mixed origins cannot silently disable the existing bounded-run fallback reserve.
  • Clock-driven recovery fixtures advance real elapsed monotonic time, including the genuine two-minute outage after slow useful output. Shared-window cases are consolidated into existing tables rather than adding test-line growth.

User Impact

An in-budget shared-TPM retry reaches the provider queue and actual transport despite a wall-clock correction. Genuine elapsed exhaustion still moves to configured fallback. Retry counts, provider floors, pacing, cancellation, model/account scope, authentication, permissions and existing fallback policy remain unchanged.

Evidence

Built Gateway proof and captured transport/status results compare current main b3aa84d0d79e3bfe7f2ef697fec3f5bf15b634fd with the candidate implementation, through real Gateway turns and a recording local HTTP/SSE provider. The fixture returns two primary HTTP 429 TPM responses with a two-second Retry-After floor, then succeeds. A process-local preload advances only the Gateway's Date.now() by ninety seconds during the first real retry backoff; the system/VM clock is untouched.

Scenario Main Candidate
Clock step, run 1 1 primary request, then fallback 3 primary requests; primary recovers
Clock step, run 2 1 primary request, then fallback 3 primary requests; primary recovers
No-step control 3 primary requests; primary recovers 3 primary requests; primary recovers
Genuine elapsed expiry 2 primary requests, then fallback 2 primary requests, then fallback

Candidate retries reach the third primary HTTP request 4.438 s and 4.411 s after the first request, within the existing fifteen-second budget and respecting the two-second floor. The expiry control holds the second failed provider response for sixteen real seconds; main and candidate then reach fallback after 18.418 s and 18.582 s. Built status reads remain epoch-shaped after the clock step, and the queue suite verifies actual onWait notifications with distinct epoch and uptime origins. Auxiliary recap requests are excluded from turn counts.

  • 194 focused controller, recovery, queue and fallback-consumer tests passed, plus 13 prompt-prefix tests. Final test-only refinements were rechecked in the two affected suites: 55 passing cases. Focused lint/formatting passed.
  • The admitted-existing-gate regression fails against the original incomplete queue with the shared wait-budget error before transport; the complete conversion passes without unhandled test rejections.
  • Passed-floor, org/profile merge, cap, cancellation, pacing, expiry, and fallback-reservation controls remain covered.
  • Full-PR delta: production +32/-15 (net +17), tests +59/-61 (net -2). No ceiling or baseline change. Broad type checks are CI-owned.

These are deterministic local-provider results, not live-provider quota or billing claims.

Merge Risk

Two-way door: reverting the code with a Gateway restart restores the former clock handling; no persisted-format, schema or configuration migration is involved. Blast radius is transient retry budgeting, shared-provider admission and its health/status consumers. A wrong conversion could delay or prematurely reject requests; public timestamp semantics and existing retry/fallback policies are preserved.

The embedded run failover controller measured the 15s shared-organization
TPM retry window with Date.now() (wall clock). When the wall clock jumps
inside the window, the elapsed budget is distorted and a still-valid retry
is rejected with retry_delay_unavailable, pushing the request to model
fallback. Seed and read the window on performance.now() so a pure
difference of two monotonic reads is immune to wall-clock drift.

The provider-rate-limit comparisons against the now-monotonic
retryDeadline switch to performance.now() to stay on the same clock. The
watchdog-facing onRetryWait deadline is left on the wall clock because its
consumer (session-controller.watchdog) compares on the wall clock.

Adds a regression test that seeds the window, jumps the wall clock +60s,
and asserts a second shared-TPM retry is still admitted.
@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.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels Oct 27, 2026
@SunnyShu0925
SunnyShu0925 force-pushed the fix/shared-tpm-retry-window-monotonic-244 branch from e648bf0 to f134dcb Compare October 27, 2026 23:43
@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. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed 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 28, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR measures retry budgets and shared provider admission with monotonic time while preserving epoch timestamps for status consumers.

Example: A Gateway receives two TPM-limit HTTP 429 responses with two-second retry floors while Date.now advances by 90 seconds.

  • Before: The primary is abandoned after one request and the turn uses fallback.
  • After: The primary receives three requests and recovers within the existing fifteen-second window.

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) Focused owner-level repair with shipped Gateway evidence, meaningful controls and no open findings; exact-head broad CI remains pending.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The supplied built Gateway evidence exercises the changed retry controller, admission queue and real HTTP/SSE transport in secretless Linux isolation: clock-step runs recover on primary, unchanged-clock controls succeed, genuine elapsed expiry reaches fallback, and status reads remain epoch-valued.
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 system-clock correction can stop valid provider recovery early or extend waiting beyond the intended elapsed budget.
Reason: This restores existing bounded recovery behavior without changing configuration, retry policy, provider selection contracts or persisted formats.

Merge readiness

✅ Ready for maintainer review

Keep this PR open: it fixes a current clock-domain bug, resolves both earlier findings, and introduces no actionable defect found in this review.

Priority: P2
Reviewed head: 3842242ceea60fdf54efe4c58a46d32124154be6

Before merge

None.

Findings

None.

Agent review details

How this fits together

The embedded retry controller supplies a deadline to the shared provider queue, which admits physical transport requests and exposes epoch-valued waiting and saturation status.

flowchart TD
  A[Gateway turn] --> B[Retry controller]
  B --> C[Shared provider queue]
  C --> D[HTTP transport]
  D --> E[Provider response]
  E --> B
  C --> F[Epoch status consumers]
Loading

Technical review

Best possible solution:

Keep elapsed-time arithmetic in the existing monotonic retry and admission owners, converting to epoch time only at established status boundaries.

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

The supplied built Gateway comparison reproduces premature fallback on the pinned main implementation using a process-local 90-second wall-clock step; current source confirms the same responsible arithmetic.

Is this the best way to solve the issue?

The patch repairs both deadline production and queue consumption at their existing owners, preserves external timestamp semantics, and adapts affected consumers without adding a parallel retry mechanism.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Transient retry elapsed budget keeps the original intent (4a1cc22: One retry controller must count failed-request time and backoff within a bounded outage window.)
  • Shared provider admission and status keeps the original intent (68a7e7b: Coordinate shared TPM floors, interactive priority and finite recovery pacing without creating another retry owner.)
  • Short shared-TPM fallback deadline keeps the original intent (5686263: Bound same-model recovery so configured fallback remains reachable during shared saturation.)
  • Bounded-run fallback reservation keeps the original intent (a479bc7: Default retries must leave bounded runs an opportunity to use eligible fallback while preserving explicit overrides.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 5686263 adds src/agents/embedded-agent-runner/run/failover-retry-controller.ts:435 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 568626311f8f; files: src/agents/embedded-agent-runner/run/failover-retry-controller.ts)
  • Ayaan Zaidi: Raw commit 4a1cc22 adds src/agents/embedded-agent-runner/run/failover-retry-controller.ts:239 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 4a1cc226964a; files: src/agents/embedded-agent-runner/run/failover-retry-controller.ts)

PR surface

Source +17, Tests -2. Total +15 across 6 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 2 32 15 +17
Tests 4 60 62 -2
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 6 92 77 +15

Review metrics

Metric Value Why it matters
Production versus test LOC Production +32/-15; tests +60/-62 The net production growth completes the queue conversion and preserves epoch status and fallback reservation contracts; regression coverage reuses existing fixtures without net test growth.

Labels

Label changes:

  • 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.
  • remove rating: 🧂 unranked krab: Current PR rating is rating: 🦞 diamond lobster, so this older rating label is no longer current.
  • remove merge-risk: 🚨 auth-provider: Current PR review selected no merge-risk labels.
  • remove status: 📣 needs proof: Current PR status label is status: 👀 ready for maintainer look.

Label justifications:

  • P2: Clock corrections can prematurely abandon a recoverable model or distort retry waits, with impact limited to affected running requests.
  • 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-28T00:00:14.623Z sha f134dcb :: needs real behavior proof before merge. :: [P1] [P1] Keep admission timestamps in compatible clock domains | [P2] [P2] Advance the monotonic clock in the recovery consumer test

Reviewed October 27, 2026, 10:16 PM ET / October 28, 2026, 02:16 UTC (Revision 2).

@obviyus

obviyus commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

Built Gateway shared-TPM clock-step proof

Baseline current main: b3aa84d0d79e3bfe7f2ef697fec3f5bf15b634fd. Candidate: 3842242ceea60fdf54efe4c58a46d32124154be6 (the final commit changes tests only; the tested production implementation is unchanged).

Both refs were built in secretless Linux/Node 24.22.1 isolation. Real Gateway session RPCs run the OpenClaw harness against a recording local HTTP/SSE provider. Primary requests receive real HTTP 429 responses identifying organization TPM exhaustion and a two-second Retry-After floor; the third primary request succeeds. A configured fallback returns a distinct completion marker. A preload advances only the Gateway process's Date.now by 90 seconds when its first real host retry backoff completes. The host/VM clock is never changed, and no retry/admission callback is mocked.

Case Main's actual turn requests / final model Candidate's actual turn requests / final model
Forward clock step, run 1 1 primary + 1 fallback / fallback 3 primary + 0 fallback / primary
Forward clock step, run 2 1 primary + 1 fallback / fallback 3 primary + 0 fallback / primary
No-step control 3 primary / primary 3 primary / primary
Genuine elapsed expiry 2 primary + 1 fallback / fallback 2 primary + 1 fallback / fallback

Main prematurely abandons the recoverable primary after about two seconds of real backoff. The candidate reaches the third physical primary request 4.438 s and 4.411 s after the first request, inside the existing fifteen-second window. Its measured retry gaps are 2.159/2.279 s and 2.155/2.256 s, respecting the provider's floor. No-step request spans are 5.187 s on main and 5.009 s on the candidate; no fallback is needed on either.

The expiry fixture delays the second failed provider response for sixteen real seconds. Main and candidate then reach fallback 18.418 s and 18.582 s after the first primary request, preserving genuine elapsed exhaustion rather than replenishing the window.

The built status reader also observes epoch-valued retryAt/expiresAt after the clock step, not process uptime. One candidate sample has wall time 1793152461324, uptime 17160.412 ms, retryAt 1793152463136.998 and expiresAt 1793152523136.998. The queue suite separately checks the actual onWait notification's epoch value with deliberately different epoch and uptime origins. Auxiliary recap calls are recorded separately and excluded from turn request counts.

Repair and checks

  • All private gate floors, pacing, expiry and timer arithmetic now use monotonic time. Absolute HTTP-date Retry-After parsing remains wall-clock based. Existing status fields and callbacks project back to epoch time; no SDK fields or source paths changed.
  • Current main's fallback-reservation change was merged. Its epoch-valued run origin is projected privately into monotonic time once, so the newly monotonic now value cannot silently disable the existing half-run fallback reserve. Its existing controls now include a wall-clock step.
  • Both slow-output recovery scenarios advance controlled performance time for the genuine two-minute outage; slow useful output still does not consume the transient outage window.
  • 194 focused tests across the controller, recovery, queue and fallback consumer suites passed, plus 13 prompt-prefix contract tests. The final two edited suites were rechecked: 55 passing tests. Focused lint/formatting passed; broad type checks remain CI-owned.
  • The positive existing-gate admission regression fails against the original incomplete contribution with the shared retry wait-budget error before transport. It passes with the complete conversion, without unhandled test rejections. Org/profile merging, already-passed floors, cancellation, cap, pacing and expiry controls remain covered.
  • Full-PR delta: production +32/-15 (net +17), tests +59/-61 (net -2), with no ceiling/baseline change.

All measured turns completed and returned their actual primary/fallback marker. This is deterministic local-provider proof, not a live-provider quota or billing claim.

@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: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. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Oct 28, 2026
@clawsweeper clawsweeper Bot added 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 dafda2f into openclaw:main Oct 28, 2026
140 of 142 checks passed
@obviyus

obviyus commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

Landed in dafda2f. Thanks @SunnyShu0925. TPM retry windows now ignore clock jumps.

@SunnyShu0925

Copy link
Copy Markdown
Contributor Author

@obviyus Thanks!

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

fix(agents): keep the shared TPM retry window on the monotonic clock

The shared tokens-per-minute retry window was measured with the wall clock, so a clock step during a rate-limit wait ended the window early and the turn fell back to another model after a single primary request. The provider queue now keeps its windows on monotonic time, with status still reported as wall-clock times through a private projection.

Evidence: built Gateway turns hitting a TPM rate limit from a local provider fixture, with a clock step inside the process during the retry window. On main the turn fell back after one primary request (twice); with this change it recovered on the primary after three real requests over 4.4s (twice). No-step runs and genuinely expired windows behave as expected on both refs. Details: openclaw/openclaw#186390 (comment).

Thanks @SunnyShu0925.

Co-authored-by: SunnyShu <0668000539@local>
Co-authored-by: Ayaan Zaidi <[email protected]>
github-actions Bot pushed a commit to tbohda2025/openclaw that referenced this pull request Oct 28, 2026
…(#186390)

fix(agents): keep the shared TPM retry window on the monotonic clock

The shared tokens-per-minute retry window was measured with the wall clock, so a clock step during a rate-limit wait ended the window early and the turn fell back to another model after a single primary request. The provider queue now keeps its windows on monotonic time, with status still reported as wall-clock times through a private projection.

Evidence: built Gateway turns hitting a TPM rate limit from a local provider fixture, with a clock step inside the process during the retry window. On main the turn fell back after one primary request (twice); with this change it recovered on the primary after three real requests over 4.4s (twice). No-step runs and genuinely expired windows behave as expected on both refs. Details: openclaw/openclaw#186390 (comment).

Thanks @SunnyShu0925.

Co-authored-by: SunnyShu <0668000539@local>
Co-authored-by: Ayaan Zaidi <[email protected]>
github-actions Bot pushed a commit to flashdropperflashdropper536/openclaw that referenced this pull request Oct 28, 2026
…(#186390)

fix(agents): keep the shared TPM retry window on the monotonic clock

The shared tokens-per-minute retry window was measured with the wall clock, so a clock step during a rate-limit wait ended the window early and the turn fell back to another model after a single primary request. The provider queue now keeps its windows on monotonic time, with status still reported as wall-clock times through a private projection.

Evidence: built Gateway turns hitting a TPM rate limit from a local provider fixture, with a clock step inside the process during the retry window. On main the turn fell back after one primary request (twice); with this change it recovered on the primary after three real requests over 4.4s (twice). No-step runs and genuinely expired windows behave as expected on both refs. Details: openclaw/openclaw#186390 (comment).

Thanks @SunnyShu0925.

Co-authored-by: SunnyShu <0668000539@local>
Co-authored-by: Ayaan Zaidi <[email protected]>
steipete added a commit that referenced this pull request Oct 28, 2026
The outage window has used performance.now since #186390, but this fixture only advanced Date.now. Advance the clock the retry owner consumes so the no-progress case actually exhausts its window while productive turns still recover.
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 28, 2026
…penclaw#186390)

fix(agents): keep the shared TPM retry window on the monotonic clock

The shared tokens-per-minute retry window was measured with the wall clock, so a clock step during a rate-limit wait ended the window early and the turn fell back to another model after a single primary request. The provider queue now keeps its windows on monotonic time, with status still reported as wall-clock times through a private projection.

Evidence: built Gateway turns hitting a TPM rate limit from a local provider fixture, with a clock step inside the process during the retry window. On main the turn fell back after one primary request (twice); with this change it recovered on the primary after three real requests over 4.4s (twice). No-step runs and genuinely expired windows behave as expected on both refs. Details: openclaw#186390 (comment).

Thanks @SunnyShu0925.

Co-authored-by: SunnyShu <0668000539@local>
Co-authored-by: Ayaan Zaidi <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 28, 2026
The outage window has used performance.now since openclaw#186390, but this fixture only advanced Date.now. Advance the clock the retry owner consumes so the no-progress case actually exhausts its window while productive turns still recover.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling 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