Skip to content

fix: Auto-expire auth profile cooldowns when timestamp expires - #3607

Closed
bergcb322-clawd wants to merge 1 commit into
openclaw:mainfrom
bergcb322-clawd:fix/auth-cooldown-auto-expire
Closed

bergcb322-clawd wants to merge 1 commit into
openclaw:mainfrom
bergcb322-clawd:fix/auth-cooldown-auto-expire

Conversation

@bergcb322-clawd

@bergcb322-clawd bergcb322-clawd commented Jan 28, 2026 •

Copy link
Copy Markdown

Fixes #3604

Problem

Auth profiles were getting stuck in cooldown even after the cooldownUntil timestamp had expired, blocking the agent from making API calls indefinitely.

Root Cause

The cooldown check only happened during profile selection (isProfileInCooldown), but expired cooldowns were never cleared from the store. Profiles remained marked as unavailable even when Date.now() >= cooldownUntil.

Solution

Added auto-expiry logic in resolveAuthProfileOrder (both explicit and round-robin paths):

  • When now >= unusableUntil, clear both cooldownUntil and disabledUntil
  • Ensures profiles become available immediately after cooldown expires
  • Minimal change, no new dependencies

Testing

Manually tested by:

  1. Setting expired cooldown timestamp in auth-profiles.json
  2. Making API call
  3. Verifying profile becomes available without manual intervention

Evidence

Before fix:

  • Cooldown set to: 1769633531559 (Jan 28 20:52:11 UTC)
  • Current time: 1769634806000 (Jan 28 21:13:26 UTC)
  • Diff: -1274 seconds (20+ minutes expired)
  • Profile still marked unavailable

After fix:

  • Profile auto-expires and becomes available

Greptile Overview

Greptile Summary

This PR updates auth profile ordering (src/agents/auth-profiles/order.ts) to auto-expire cooldown/disable timestamps when they’re already in the past, preventing profiles from remaining stuck as “in cooldown” indefinitely. The logic is applied in both the explicit-order path and the round-robin ordering path so that resolveAuthProfileOrder returns available profiles immediately after expiry.

Confidence Score: 3/5

  • Mostly safe to merge, with one behavior edge case to double-check around disabled-vs-cooldown expiry.
  • The change is localized and addresses a real stuck-state bug, but it mutates stored usage stats during ordering and clears both cooldownUntil and disabledUntil together, which could unintentionally re-enable a profile if those fields are meant to expire independently.
  • src/agents/auth-profiles/order.ts

(2/5) Greptile learns from your feedback when you react with thumbs up/down!

Fixes openclaw#3604

Auth profiles were getting stuck in cooldown even after the cooldownUntil
timestamp had expired. This happened because the cooldown check only happened
during profile selection, but expired cooldowns were never cleared from the
store.

Changes:
- Added auto-expiry logic in profile ordering (both explicit and round-robin paths)
- When now >= cooldownUntil, clear both cooldownUntil and disabledUntil
- Ensures profiles become available immediately after cooldown expires

Tested by manually setting expired cooldown and verifying it clears on next
profile resolution attempt.
@openclaw-barnacle openclaw-barnacle Bot added the agents Agent runtime and tooling label Jan 28, 2026

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

Comment on lines +104 to +107
// Auto-expire cooldowns that have passed
if (stats && cooldownUntil > 0 && now >= cooldownUntil) {
stats.cooldownUntil = undefined;
stats.disabledUntil = undefined;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Clearing disabledUntil when only cooldownUntil expired can re-enable profiles prematurely.

In the explicit-order path, cooldownUntil is computed as max(cooldownUntil, disabledUntil) (via resolveProfileUnusableUntil). If disabledUntil is still in the future, now >= cooldownUntil is false, so this block doesn’t run. But if disabledUntil is past and cooldownUntil is also past, both get cleared even though disabledUntil might represent a billing disable reason that you may want to preserve until it truly expires (or preserve disabledReason/counters). Consider expiring each field independently (clear cooldownUntil only if now >= cooldownUntil, and clear disabledUntil only if now >= disabledUntil) to avoid unintended state changes.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/auth-profiles/order.ts
Line: 104:107

Comment:
[P1] Clearing `disabledUntil` when only `cooldownUntil` expired can re-enable profiles prematurely.

In the explicit-order path, `cooldownUntil` is computed as `max(cooldownUntil, disabledUntil)` (via `resolveProfileUnusableUntil`). If `disabledUntil` is still in the future, `now >= cooldownUntil` is false, so this block doesn’t run. But if `disabledUntil` is past and `cooldownUntil` is also past, both get cleared even though `disabledUntil` might represent a billing disable reason that you may want to preserve until it truly expires (or preserve `disabledReason`/counters). Consider expiring each field independently (clear `cooldownUntil` only if `now >= cooldownUntil`, and clear `disabledUntil` only if `now >= disabledUntil`) to avoid unintended state changes.

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines 155 to 166
for (const profileId of order) {
const stats = store.usageStats?.[profileId];
if (stats) {
const unusableUntil = resolveProfileUnusableUntil(stats);
// Auto-expire cooldowns that have passed
if (unusableUntil && unusableUntil > 0 && now >= unusableUntil) {
stats.cooldownUntil = undefined;
stats.disabledUntil = undefined;
}
}

if (isProfileInCooldown(store, profileId)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Duplicated cooldown-expiry logic diverges from isProfileInCooldown behavior.

resolveProfileUnusableUntil/expiry logic is now implemented in two places here and separately in src/agents/auth-profiles/usage.ts. This increases the chance of future drift (e.g., if disabledReason/other fields start mattering). It may be simpler/safer to centralize “expire stats if unusableUntil passed” in a shared helper in usage.ts and call it from both ordering paths.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/auth-profiles/order.ts
Line: 155:166

Comment:
[P3] Duplicated cooldown-expiry logic diverges from `isProfileInCooldown` behavior.

`resolveProfileUnusableUntil`/expiry logic is now implemented in two places here and separately in `src/agents/auth-profiles/usage.ts`. This increases the chance of future drift (e.g., if `disabledReason`/other fields start mattering). It may be simpler/safer to centralize “expire stats if unusableUntil passed” in a shared helper in `usage.ts` and call it from both ordering paths.

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

@thewilloftheshadow

Copy link
Copy Markdown
Member

Closing in favor of #17853 as it has more tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auth profile cooldown doesn't auto-expire

2 participants