Skip to content

fix(grok): Prefer a live credential over a stale keyring entry - #37

Merged
euxaristia merged 2 commits into
mainfrom
fix/grok-stale-keyring-credential
Aug 24, 2026
Merged

fix(grok): Prefer a live credential over a stale keyring entry#37
euxaristia merged 2 commits into
mainfrom
fix/grok-stale-keyring-credential

Conversation

@euxaristia

Copy link
Copy Markdown
Owner

Summary

Grok reported HTTP 401 and rendered as degraded with no usage windows, on a
machine where the Grok CLI was signed in and working.

load_grok() read the OS keyring before ~/.grok/auth.json and returned the
first entry it found regardless of freshness, so an expired cairn-code
oauth:xai token shadowed the live one the CLI maintains. The refresh probe
could not rescue it: grok models refreshes the CLI's file, nothing rewrites
the keyring copy, so the probe fired on every call, refreshed a file that was
then ignored, and handed back the same dead token.

This is a regression. Before cef4016 ("Load Grok and Claude tokens from
cairn-code keyring entries"), load_grok() read only the file. That commit put
the keyring ahead of the file for both Claude and Grok; 6f07976 ("Handle
nullable Claude quota fields and prioritize canonical credentials file") fixed
it one commit later for Claude alone.

The second commit closes a smaller gap in the same path: a billing call that
never landed was indistinguishable from a window that had gone unspent.

Changes

  • src/credentials/mod.rs: load_grok() now collects every candidate with the
    canonical file first and picks the first still-live one via pick_live(),
    rather than the first that merely exists. A plain order swap would fix Grok
    while breaking the mirror case Antigravity documents, where the keyring holds
    the live token and the file is the stale artifact.
  • src/fetch.rs: used_percent becomes an Option, reported as
    usage unavailable when billing does not answer. An absent
    creditUsagePercent inside a successful response stays a real zero, since
    the payload is protobuf JSON and omits fields still holding their default.

Test plan

  • cargo fmt --check
  • RUSTFLAGS="-Dwarnings" cargo clippy --all-targets
  • cargo test (160 pass, up from 155)
  • Three of the new tests fail against the old code, confirmed by reverting each
    change in place:
    • a_stale_keyring_entry_never_shadows_a_live_credential fails with
      left: "expired-keyring-copy", right: "refreshed-cli-token"
    • grok_billing_that_never_answers_reports_unavailable_rather_than_zero fails
      with left: "0% used", right: "usage unavailable"
  • Manual: limits status -p grok --json returned
    "ErrorMessage": "Grok user API returned HTTP 401" with an empty Windows
    array before, and a healthy Weekly window with a reset countdown after.
  • The reported percentage was cross-checked against the Grok CLI's own
    billing: fetched credits config log records, which show the field present
    with real values for the previous billing period and absent in the new one.

Fixes #36

load_grok() read the OS keyring before ~/.grok/auth.json and returned the first
entry it found, so an expired cairn-code token shadowed the fresh one the Grok
CLI maintains and every request went out with a dead bearer. The refresh probe
could not recover from it either: it refreshes the CLI's file, nothing rewrites
the keyring copy, so the probe ran on every call and changed nothing.

Collect the candidates with the canonical file first and take the first one that
is still live, rather than the first one that exists. Order alone would fix Grok
while breaking the mirror case Antigravity documents, where the keyring holds the
live token and the file is the stale artifact.

cef4016 introduced the keyring-first ordering for both Claude and Grok, and
6f07976 corrected only Claude.

Refs #36

Claude-Session: https://claude.ai/code/session_01PvhTkPF3fvjPUh5ao9MeWL
used_percent was initialised to 0.0 and only overwritten on success, so a billing
request that never landed rendered as "0% used" and looked like a healthy, idle
week.

Carry it as an Option and report "usage unavailable" when the call does not
answer. Absence of creditUsagePercent inside a successful response stays a real
zero: the payload is protobuf JSON, which omits any field still holding its
default, so a period with no spend legitimately returns without the field.

Refs #36

Claude-Session: https://claude.ai/code/session_01PvhTkPF3fvjPUh5ao9MeWL
@coderabbitai

coderabbitai Bot commented Aug 22, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e9e93f13-7aed-4bc4-9049-2492a25fff3d

📥 Commits

Reviewing files that changed from the base of the PR and between 9670d91 and a43655c.

📒 Files selected for processing (2)
  • src/credentials/mod.rs
  • src/fetch.rs

📝 Walkthrough

Walkthrough

The change updates Grok credential selection to evaluate all supported stores and improves billing usage reporting. Fresh credentials now take precedence over stale entries. Failed billing requests now display unavailable usage instead of zero.

Changes

Grok handling

Layer / File(s) Summary
Credential candidate selection
src/credentials/mod.rs
load_grok aggregates canonical CLI and keyring credentials. It selects the first fresh token and retains the canonical token when all candidates are expired. Tests cover fresh, stale, and empty candidate sets.
Billing usage status
src/fetch.rs
Grok billing treats omitted creditUsagePercent as zero and failed billing requests as unavailable. Tests cover omitted, failed, and successful billing responses.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Poem

A rabbit checks each token in line,
Fresh keys hop forward, stale keys decline.
Zero means quiet, not data gone,
Unknown now speaks when calls move on.
Grok’s weekly gauge shines clear and bright.

🐇

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Comment @coderabbitai help to get the list of available commands.

@euxaristia
euxaristia marked this pull request as ready for review August 24, 2026 09:40
@euxaristia
euxaristia merged commit ceaed03 into main Aug 24, 2026
4 checks passed
@euxaristia
euxaristia deleted the fix/grok-stale-keyring-credential branch August 24, 2026 09:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Grok reports HTTP 401 when a stale keyring entry shadows the CLI credential

1 participant