Skip to content

fix(oauth): fail refresh for revoked Command Code keys - #217

Merged
pierreraby merged 3 commits into
patlux:mainfrom
mrshu:fix/refresh-rejects-revoked-keys
Nov 15, 2026
Merged

pierreraby merged 3 commits into
patlux:mainfrom
mrshu:fix/refresh-rejects-revoked-keys

Conversation

@mrshu

@mrshu mrshu commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

On Oh My Pi, a Command Code /login credential whose key was revoked or rotated kept being used for every request, even when a valid key was stored next to it. Every request then failed with 401 Invalid 'Authorization' header or token.

The cause is refreshToken. It returned the stored key with a new ten-year expiry and never contacted Command Code. OMP 18.20 forces a refresh after a 401 and signs a credential out only when that refresh fails with an error it reads as a dead login (invalid_token, or 401 without a network or 5xx marker; see isOAuthExpiry in pi-ai/src/error/flags.ts). Because refresh always succeeded, the dead credential was never signed out, and OMP's preference for OAuth credentials over stored API keys kept selecting it.

Change

  • refreshToken now checks the key against /alpha/whoami, the same call that /login uses to validate a key. A 401 fails the refresh with Command Code rejected the stored API key (401 invalid_token); run /login again.
  • A network error, timeout or 5xx from whoami fails the refresh with the existing validateApiKey error. Those messages contain no 401 or invalid_token, so OMP treats them as temporary and keeps the credential.
  • validateApiKey throws a new InvalidApiKeyError for a 401. The /login message is unchanged (Invalid Command Code API key).
  • pi refreshes only when a credential expires, so with the ten-year expiry it rarely reaches this code.
  • README (OMP 401 paragraph) and CHANGELOG (Unreleased) are updated.

Testing

  • Three new cases in tests/test-oauth.ts: a valid key sends Authorization: Bearer <key> and comes back with a new expiry, a 401 fails with 401 and invalid_token in the message, and a 503 fails without either. All three failed before the change.
  • npm test passes locally, including the mock pi and OMP host suites.
  • Live, on my own account with OMP 18.20.0: two stored /login credentials had revoked keys (both whoami and /provider/v1/chat/completions returned 401), and one stored API key was valid. Before the change, omp -p --model commandcode/deepseek/deepseek-v4.1-flash failed with the 401 above on every run. With this branch loaded via -e, OMP printed "A Command Code account was signed out automatically" twice, recorded oauth refresh failed: … 401 invalid_token as the disable cause on both rows, and answered. Later runs answered without any warning.

Note

VALIDATE_API_KEY_TIMEOUT_MS is 15 s, while OMP gives one refresh 10 s (OAUTH_REFRESH_OPERATION_TIMEOUT_MS in auth/refresh.ts). If whoami is slow, OMP gives up first and treats that as a temporary failure, so the credential is kept and retried later. I left the timeout as it is; a shorter one for the refresh path would be a separate change.

refreshToken returned the stored key with a new ten-year expiry and never
contacted Command Code. On Oh My Pi, a revoked /login credential therefore
kept being selected over a valid one, and every request failed with
401 Invalid 'Authorization' header or token.

Refresh now checks the key against /alpha/whoami. A 401 fails the refresh
with "401 invalid_token", which OMP's forced refresh after a 401 reads as a
dead login: it signs the credential out and uses the next stored one.
Network errors and 5xx responses keep their own errors, which OMP treats
as temporary.

@pierreraby pierreraby left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the fix and for documenting the live OMP reproduction. The revoked-key handling looks sound, but there is one host-compatibility blocker on this head.

refreshToken is registered directly at index.ts:201. Pi 1.5.2 expects refreshToken(credentials, signal: AbortSignal), whereas the new second parameter is ValidateApiKeyOptions. The required entrypoint gate fails with TS2322:

npm run typecheck:entrypoint -- /path/to/installed/@earendil-works/pi-coding-agent

Type '(credentials: OAuthCredentials, options?: ValidateApiKeyOptions) => Promise<OAuthCredentials>'
is not assignable to type '(credentials: OAuthCredentials, signal: AbortSignal) => Promise<OAuthCredentials>'.
Type 'AbortSignal' has no properties in common with type 'ValidateApiKeyOptions'.

I confirmed this gate passes on the base commit 2a5c55d and fails on the reviewed head 0babcd0. At runtime the host signal is also treated as the options object and ignored: a synthetic call with an already-aborted signal still completes successfully.

Please keep the host-compatible second signal argument, propagate cancellation to the whoami request, and move test-option injection to a helper or a separate parameter. A host-call-shape/abort regression test would protect this boundary. Please also run the separate entrypoint gate against the installed Pi declarations; npm test alone does not run it.

Otherwise, local validation is positive: the complete npm test suite passes with real Pi and OMP against loopback mocks, all 27 OAuth tests pass, and the three new tests fail against the base implementation. Formatting and the ordinary unit typecheck pass too. Additional synthetic network, timeout and non-401 status checks preserve non-definitive failures. No paid API calls were made.

An independent fresh-context review confirmed the same host-contract issue. I am currently handling maintenance solo, so I ran that second review before posting this request.

Comment thread src/oauth.ts Resolved
Pi calls refreshToken(credentials, signal: AbortSignal). The previous
commit made the second parameter a test-options object, which broke
the entrypoint typecheck (TS2322) and ignored host cancellation.

- refreshToken takes (credentials, signal?, options?); tests inject
  fetch through the third parameter.
- validateApiKey accepts a signal. Aborting it cancels the whoami
  request and rejects the refresh even if fetch ignores the signal.
  The abort error contains neither 401 nor invalid_token.
- Add a test that calls refresh with a host signal, both aborted
  mid-request and already aborted.
pierreraby
pierreraby previously approved these changes Nov 15, 2026

@pierreraby pierreraby left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-reviewed at 1b5ab66. The previous blocking host-contract issue is resolved: refreshToken now accepts the host AbortSignal as its second argument, propagates cancellation, and covers both in-flight and already-aborted signals.

Validation: entrypoint typecheck against real Pi 1.5.2 declarations passes (the previous TS2322 is gone); full npm test with PI_LOCAL_REQUIRED=1 and OMP_COMPAT_REQUIRED=1 passes, including real Pi/OMP processes against mocks and all three protocols on both hosts; OAuth tests 28/28; formatting and diff checks pass. Additional synthetic checks cover non-401 responses, network failures, timeout, cancellation and listener cleanup. A fresh independent review confirms the Pi/OMP OAuth contracts and definitive-versus-transient error classification.

Non-blocking follow-ups: a real-host revoked-credential failover test would strengthen coverage; the validation timeout is 15s while OMP waits 10s for refresh, so slow validation remains transient; the comment about unchanged network errors could be more precise because validateApiKey wraps them. No remaining blocking findings.

GitHub workflows are still awaiting external-contribution authorization (action_required); this approval does not assert that remote CI has passed.

@pierreraby pierreraby left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Revalidated exact head 6b4d082 after merging the catalog fix #218. Mechanical git checks confirm the parents are the previously reviewed OAuth head 1b5ab66 and main d3eda89; the sole manual conflict resolution preserves both changelog entries. OAuth source/tests and entrypoint are unchanged from the reviewed head; catalog source/tests match main.

Full required-host npm test, real Pi 1.5.2 entrypoint typecheck, formatting, latest CLI catalog and live public pricing checks all pass locally. Remote CI, catalog and memory benchmark pass for this exact head. Fresh independent review of the combined tree found no blocking issue. Previous non-blocking follow-ups remain; no paid API calls were made.

@pierreraby
pierreraby merged commit a84f239 into patlux:main Nov 15, 2026
15 checks passed
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.

2 participants