Skip to content

CLI: classify OAuth and fetch-guard failures instead of exiting 1 (#2842) - #2940

Merged
cliffhall merged 1 commit into
v2/mainfrom
v2/fix/2842-cli-auth-exit-codes
Nov 11, 2026
Merged

cliffhall merged 1 commit into
v2/mainfrom
v2/fix/2842-cli-auth-exit-codes

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #2842

What changed

classifyError (core/cli/error-handler.ts) sent every OAuth failure and every destination-guard refusal to its catch-all, which exits 1, the code EXIT_CODES reserves for usage errors, with envelope code "error". A script could not tell "the authorization server said no" from "the command line is wrong". This PR adds typed errors where the failures start and classifies them:

Failure code Exit
Authorization server redirects back with ?error= (e.g. access_denied from a declined consent), or the token exchange after the callback fails (SDK OAuthError, e.g. invalid_grant) oauth_error, with oauthError / oauthErrorDescription 3
No OAuth callback before the loopback deadline oauth_timeout 3
Step-up [y/N] answered N (cliOAuth.ts) step_up_declined 3
Re-authorization finished but the required scopes are still missing (cliOAuth.ts) insufficient_scope 3
OAuth destination guard refused a URL (FetchTargetError, either thrown directly or wrapped by undici as fetch failed) fetch_refused 12 (new)
Anything else error 1 (unchanged)
  • core/auth/node/runner-interactive-oauth.ts adds OAuthCallbackError and OAuthCallbackTimeoutError. They replace the plain Errors the flow threw before, and their messages are unchanged, so the TUI renders exactly what it did.
  • core/cli/cliOAuth.ts: the two plain-Error sites (declined step-up and insufficient scope) now throw CliExitCodeError(AUTH_REQUIRED, …). Their messages are also unchanged.
  • mcpdo: the daemon relays oauthError / oauthErrorDescription across the socket the same way it relays the CLI and mcpdo JSON error envelopes drop the structured Retry-After data from a 429 #2772 rate-limit fields. The client sanitizes both fields because they are server text. mcpdo's front-end OAuth already goes through handleError, so it gets the new codes directly.
  • The envelope redacts URLs in the new fields (CLI/TUI error output may not apply the same URL-redaction as the web client's OAuth timeout path #2423).
  • Docs: the CLI README exit-code table gains an oauth_error envelope example, the docs/cli-smoke-testing.md table is updated, and skills/mcpdo/SKILL.md now names the new exit-3 codes.

Why these exit codes

  • 3 for the OAuth failures. Authorization is what failed, and running it again is what can fix it, which is what 3 already means. The more specific code tells a script which auth failure happened without making it read message. For example, an explicit N (step_up_declined) is now distinct from a prompt that timed out (auth_required).
  • A new 12 (FETCH_REFUSED) for the guard. It is not 1, because the command line is fine. It is not 4 (UNREACHABLE) either, even though undici-wrapped refusals used to land there. Exit 4 tells a caller the network failed and a retry or a proxy may help. A refusal is a deterministic policy decision, so every retry gets the same answer. It gets its own code for the same reason 11 (RATE_LIMITED) has one: the right response is different from every neighbouring code's.
  • Field names. The issue asked for error / error_description as separate envelope fields. They are separate fields here, named oauthError / oauthErrorDescription. The envelope already sits under an error key and uses camelCase (retryAfterMs), so a literal error field would read .error.error.

Tests

  • clients/cli/__tests__/error-handler.test.ts: each new classification, field omission when the server sent nothing, redaction, and relayed fields on a pre-classified error.
  • clients/cli/__tests__/cliOAuth.test.ts: step_up_declined and insufficient_scope round-trip through classifyError.
  • clients/mcpdo/__tests__/oauth-error-envelope.test.ts: the daemon relays the fields and the client rebuilds them.
  • clients/web/src/test/core/auth/runner-interactive-oauth.test.ts: the flow rejects with the typed errors.

npm run local:gate passes.

🤖 Generated with Claude Code

OAuth protocol errors (a declined consent redirect, the SDK's token-endpoint
OAuthError) now exit 3 with code oauth_error and the server's error /
error_description as oauthError / oauthErrorDescription envelope fields. The
OAuth callback timeout exits 3 as oauth_timeout. A declined step-up and
insufficient scope after re-auth exit 3 as step_up_declined /
insufficient_scope. A destination-guard refusal (FetchTargetError, including
undici's fetch failed wrapping one) exits with the new code 12 as
fetch_refused. mcpdo relays the new OAuth fields across the daemon socket.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: cliffhall <[email protected]>
@cliffhall cliffhall added the v2 Issues and PRs for v2 label Nov 11, 2026
@cliffhall
cliffhall requested a balanced review from Copilot November 11, 2026 17:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the OAuth and destination-guard error-handling changes.

No open issues 🎉

What changed in this PR

Addresses #2842 by giving the shared CLI error handler distinct outcomes for OAuth failures and destination-guard refusals, so scripts can distinguish them from usage errors.

Changes:

  • Classifies OAuth failures as exit 3 with specific envelope codes, and fetch-guard refusals as exit 12.
  • Preserves structured OAuth error fields through mcpdo’s daemon relay, with sanitization and URL redaction.
  • Adds classification and relay tests, and updates CLI documentation.
File Description
skills/​mcpdo/​SKILL.md Documents exit-3 authentication recovery outcomes.
docs/​cli-smoke-testing.md Updates the exit-code reference.
core/​cli/​exit-codes.ts Defines exit 12 and OAuth envelope fields.
core/​cli/​error-handler.ts Classifies new failures and redacts OAuth fields.
core/​cli/​cliOAuth.ts Assigns explicit step-up failure codes.
core/​auth/​node/​runner-interactive-oauth.ts Introduces typed callback and timeout errors.
core/​auth/​node/​index.ts Exports the new OAuth error classes.
clients/​web/​src/​test/​core/​auth/​runner-interactive-oauth.test.ts Tests typed interactive OAuth failures.
clients/​mcpdo/​src/​daemon/​server.ts Relays structured OAuth error fields.
clients/​mcpdo/​src/​daemon/​protocol.ts Extends the daemon error contract.
clients/​mcpdo/​src/​daemon/​client.ts Rebuilds and sanitizes relayed OAuth fields.
clients/​mcpdo/​__tests__/​oauth-error-envelope.test.ts Tests daemon relay and client sanitization.
clients/​cli/​README.md Documents new outcomes and an OAuth envelope example.
clients/​cli/​__tests__/​error-handler.test.ts Covers classification, field omission, redaction, and relay handling.
clients/​cli/​__tests__/​cliOAuth.test.ts Tests declined step-up and insufficient-scope classifications.

Review effort: Balanced

@cliffhall

Copy link
Copy Markdown
Member Author

Copilot review loop closed: round 1 was clean (no inline comments, no suppressed findings, 'no open issues'). Stopping per pr-flow 7c on the first clean round.

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

Labels

v2 Issues and PRs for v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI: OAuth denials and other non-usage failures exit 1 with code "error", the same as a usage error

2 participants