Skip to content

fix: auto auth profile override should not bias profile ordering - #31056

Closed
taw0002 wants to merge 1 commit into
openclaw:mainfrom
taw0002:fix/auth-profile-ordering-v2
Closed

taw0002 wants to merge 1 commit into
openclaw:mainfrom
taw0002:fix/auth-profile-ordering-v2

Conversation

@taw0002

@taw0002 taw0002 commented Mar 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Two related bugs in auth profile rotation that cause auto-selected fallback profiles (e.g., API key) to be used instead of higher-priority profiles (e.g., OAuth) even when those profiles are available.

Bug 1: preferredProfile passed for auto overrides (run.ts)

resolveAuthProfileOrder() received preferredProfile: preferredProfileId regardless of whether the source was user or auto. When set, resolveAuthProfileOrder moves the preferred profile to the front of the candidate list. This means an auto-selected anthropic:default (API key) gets front-of-queue treatment, bypassing OAuth profiles.

Fix: Only pass preferredProfile when the override source is user.

Bug 2: No re-evaluation when higher-priority profiles exit cooldown (session-override.ts)

resolveSessionAuthProfileOverride() kept the current auto-selected profile as long as it wasn't in cooldown itself. It never checked whether a higher-priority profile had exited cooldown and become available again.

Fix: When the current auto-selected profile is not the highest-priority in the order, scan for any higher-priority profile that's now available.

Tests

Added 2 new test cases to session-override.test.ts:

  • Re-selects higher-priority profile when it exits cooldown
  • Stays on current profile when higher-priority profiles are still in cooldown

Fixes #25510
Replaces #25534 (which had merge conflicts)

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S labels Mar 1, 2026

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 354c4701aa

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +134 to +138
if (next && source === "auto" && order.length > 1) {
const currentIndex = order.indexOf(next);
if (currentIndex > 0) {
const higherPriority = order.slice(0, currentIndex).find(
(pid) => !isProfileInCooldown(store, pid)

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.

P2 Badge Skip higher-priority override after explicit rotation

The new re-evaluation in resolveSessionAuthProfileOverride runs for every auto-sourced choice, including cases where next was intentionally advanced by pickNextAvailable (new-session rotation or compaction-driven rotation). In a simple healthy order like [p1, p2] with current p1, this block rewrites next from p2 back to p1, so rotation is effectively canceled and selection stays pinned to the first profile. This check should only run when we kept the current fallback (the “higher-priority exited cooldown” case), not after an intentional rotation step.

Useful? React with 👍 / 👎.

@greptile-apps

greptile-apps Bot commented Mar 1, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes two related bugs in auth profile rotation that prevented higher-priority profiles (OAuth) from being used when available, favoring lower-priority fallbacks (API keys).

Changes:

  • run.ts: Only pass preferredProfile to resolveAuthProfileOrder() when source is user, preventing auto-selected profiles from receiving unintended priority
  • session-override.ts: Added re-evaluation logic to switch from auto-selected profiles to higher-priority profiles when they exit cooldown
  • Added two test cases covering the re-evaluation scenarios (though they need to be moved inside the describe block)

Logic correctness: The fix correctly addresses both bugs. The run.ts change prevents auto-selected profiles from biasing the order, and the session-override.ts change ensures sessions upgrade to higher-priority profiles when available.

Confidence Score: 4/5

  • Safe to merge after fixing test structure issue
  • The logic changes are correct and well-tested, addressing the stated bugs effectively. The only issue is the test structure (tests outside describe block), which is a minor syntax problem that doesn't affect the actual functionality but should be corrected before merging.
  • Fix the test structure in src/agents/auth-profiles/session-override.test.ts before merging

Last reviewed commit: 354c470

@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.

3 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@taw0002
taw0002 force-pushed the fix/auth-profile-ordering-v2 branch from 354c470 to 90ffa3c Compare March 1, 2026 23:22

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 90ffa3c9ef

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/agents/pi-embedded-runner/run.ts Outdated
@@ -348,7 +348,7 @@ export async function runEmbeddedPiAgent(
cfg: params.config,
store: authStore,
provider,
preferredProfile: preferredProfileId,
preferredProfile: params.authProfileIdSource === "user" ? preferredProfileId : 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.

P2 Badge Preserve inferred user overrides when source is missing

Only passing preferredProfile when authProfileIdSource === "user" drops backward-compatible behavior for sessions that have authProfileOverride but no authProfileOverrideSource. resolveSessionAuthProfileOverride explicitly infers that case as user-selected and returns early (src/agents/auth-profiles/session-override.ts:110-119), so after this change runEmbeddedPiAgent can ignore that explicit profile and start from a different candidate whenever multiple profiles exist. This creates an upgrade regression for legacy session entries unless undefined source is treated as inferred-user (or otherwise preserved) instead of non-preferred.

Useful? React with 👍 / 👎.

@taw0002
taw0002 force-pushed the fix/auth-profile-ordering-v2 branch from f67c93a to cbc591b Compare March 2, 2026 01:12

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cbc591b197

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +137 to +141
if (next && source === "auto" && order.length > 1 && !intentionallyRotated) {
const currentIndex = order.indexOf(next);
if (currentIndex > 0) {
const higherPriority = order
.slice(0, currentIndex)

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.

P2 Badge Avoid reverting intentional rotation on subsequent turns

Fresh evidence beyond the earlier thread: this re-evaluation still executes on the next invocation, and resolveSessionAuthProfileOverride is called for each message (src/auto-reply/reply/get-reply-run.ts:446), so intentionallyRotated only protects the current call. If a new session intentionally rotates from p1 to p2, the next non-new turn (isNewSession=false) will immediately switch back to p1 whenever it is available, which collapses the intended auto round-robin persistence and reintroduces ordering bias.

Useful? React with 👍 / 👎.

@taw0002
taw0002 force-pushed the fix/auth-profile-ordering-v2 branch from cbc591b to 4ae7dea Compare March 2, 2026 01:36

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4ae7deae2a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +37 to +40
if (!cooldownUntilByProfileBeforeOrder.has(profileId)) {
continue;
}
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.

P2 Badge Restrict higher-priority reselection to actual cooldown expiry

Using cooldownUntilByProfileBeforeOrder.has(profileId) as the only gate means any historical cooldownUntil value (including long-expired entries) is treated as a reason to override the current auto profile. When a session intentionally rotates to a lower-priority healthy profile (for example on isNewSession), the next non-new turn can immediately snap back to the first profile solely because that higher profile once had cooldown metadata, so rotation persistence becomes dependent on stale history rather than a real just-ended cooldown event.

Useful? React with 👍 / 👎.

Two related bugs in auth profile rotation:

1. preferredProfile passed for auto overrides (run.ts): resolveAuthProfileOrder
   received preferredProfile regardless of whether the source was user or auto.
   Auto-selected profiles (e.g., API key fallback) got front-of-queue treatment,
   bypassing higher-priority profiles (e.g., OAuth). Fix: only pass
   preferredProfile when source is user or inferred-user (legacy sessions
   without explicit source).

2. No re-evaluation when higher-priority profiles exit cooldown
   (session-override.ts): once a session fell back to a lower-priority profile,
   it stayed there even after the higher-priority profile exited cooldown. Fix:
   check for available higher-priority profiles on each resolution, but only
   when not intentionally rotating (new session or post-compaction).

Fixes openclaw#25510
@taw0002
taw0002 force-pushed the fix/auth-profile-ordering-v2 branch from 4ae7dea to 6f6bd6b Compare March 2, 2026 03:32

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6f6bd6bfef

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +129 to +133
} else if (source === "auto") {
const best = pickFirstAvailable();
if (best && order.indexOf(best) < order.indexOf(current)) {
next = best;
}

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.

P2 Badge Preserve auto rotation across subsequent session turns

Avoid re-selecting pickFirstAvailable() unconditionally for source === "auto" here, because on the message after an intentional rotation (isNewSession or compaction), this branch immediately snaps next back to the first profile in order and undoes the rotation. In a healthy two-profile order [p1, p2], a new-session rotation to p2 is reverted to p1 on the very next non-new turn, so the session cannot stay on its rotated profile long enough to get the intended round-robin behavior.

Useful? React with 👍 / 👎.

@Takhoffman
Takhoffman requested a review from a team as a code owner March 24, 2026 20:16
@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added the stale Marked as stale due to inactivity label Apr 6, 2026
@openclaw-barnacle

Copy link
Copy Markdown

Closing due to inactivity.
If you believe this PR should be revived, post in #pr-thunderdome-dangerzone on Discord to talk to a maintainer.
That channel is the escape hatch for high-quality PRs that get auto-closed.

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

Labels

agents Agent runtime and tooling size: S stale Marked as stale due to inactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: auto auth profile override can bypass manual-first key rotation and pin API key fallback

1 participant