Repository navigation
fix(oauth): fail refresh for revoked Command Code keys - #217
Conversation
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
left a comment
There was a problem hiding this comment.
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.
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
On Oh My Pi, a Command Code
/logincredential 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 with401 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 a401and signs a credential out only when that refresh fails with an error it reads as a dead login (invalid_token, or401without a network or 5xx marker; seeisOAuthExpiryinpi-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
refreshTokennow checks the key against/alpha/whoami, the same call that/loginuses to validate a key. A401fails the refresh withCommand Code rejected the stored API key (401 invalid_token); run /login again.whoamifails the refresh with the existingvalidateApiKeyerror. Those messages contain no401orinvalid_token, so OMP treats them as temporary and keeps the credential.validateApiKeythrows a newInvalidApiKeyErrorfor a401. The/loginmessage is unchanged (Invalid Command Code API key).Unreleased) are updated.Testing
tests/test-oauth.ts: a valid key sendsAuthorization: Bearer <key>and comes back with a new expiry, a401fails with401andinvalid_tokenin the message, and a503fails without either. All three failed before the change.npm testpasses locally, including the mock pi and OMP host suites./logincredentials had revoked keys (bothwhoamiand/provider/v1/chat/completionsreturned 401), and one stored API key was valid. Before the change,omp -p --model commandcode/deepseek/deepseek-v4.1-flashfailed with the 401 above on every run. With this branch loaded via-e, OMP printed "A Command Code account was signed out automatically" twice, recordedoauth refresh failed: … 401 invalid_tokenas the disable cause on both rows, and answered. Later runs answered without any warning.Note
VALIDATE_API_KEY_TIMEOUT_MSis 15 s, while OMP gives one refresh 10 s (OAUTH_REFRESH_OPERATION_TIMEOUT_MSinauth/refresh.ts). Ifwhoamiis 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.