Skip to content

fix(core): ask to reconnect ChatGPT when its refresh token is rejected - #65289

Merged
hearnadam merged 1 commit into
anomalyco:devfrom
hearnadam:chatgpt-reconnect-error
Nov 22, 2026
Merged

hearnadam merged 1 commit into
anomalyco:devfrom
hearnadam:chatgpt-reconnect-error

Conversation

@hearnadam

@hearnadam hearnadam commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #60133

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When a ChatGPT sign-in's refresh token has been revoked or has expired, OpenAI's token endpoint answers 400 with {"error":"invalid_grant"}. request() dropped the body, so every prompt failed with "Request failed: 400" and nothing told the user to reconnect.

refresh() now turns that response into "This ChatGPT sign-in has expired or was revoked. Connect again." Only invalid_grant on the refresh grant is matched. Other failures, and the authorization-code exchange, keep their current errors.

This does not add a "needs sign in" status for the connection. The refresh runs in core rather than in the plugin, so where that status would be set needs a decision and is left out of this PR.

How did you verify your code works?

  • Added a test in provider-chatgpt.test.ts that runs refresh() against a local server. invalid_grant gives the new message; a 400 with a different error and a 500 still give "Request failed: ". The test fails without the fix.
  • bun run test test/plugin/provider-chatgpt.test.ts and bun typecheck in packages/core, and bun run check at the root.
  • Confirmed the response shape by posting an invalid refresh token to the real token endpoint, which returned 400 {"error":"invalid_grant"}. I have not tested against a real sign-in whose token was revoked.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #55211

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

The change looks sound to me. When OpenAI rejects a refresh with invalid_grant, the new message replaces the bare 400. It reaches users unchanged through Integration.AuthorizationError. I couldn't run it end to end, because that needs a real ChatGPT sign-in whose token has been revoked. Instead I ran refresh() against a local server. Without the fix, the invalid_grant case fails with "Request failed: 400". With the fix, the new message appears, and other 400s and 500s keep their current error. The package's tests and tsc -b pass.

The export with a default URL follows the pattern revoke already uses. The invalid_grant check matches the existing handling in opencode.ts. The test is small and uses a local server, not the real network.

Public API: unchanged. refresh is a newly exported function inside core only.

No blocking concerns.

Related: #55211

Copilot AI 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.

🟡 Changes recommended

The reconnect-message branch incorrectly handles non-400 responses containing invalid_grant, masking server errors as expired sign-ins instead of preserving the existing HTTP error.

1 open issue
What changed in this PR

Improves ChatGPT OAuth refresh errors in core so users are told to reconnect when their refresh token is rejected, addressing the unclear prompt failures reported in #60133. Authorization-code exchange behavior and connection status remain unchanged.

Changes:

  • Recognizes invalid_grant responses during refresh and supplies an actionable reconnect message.
  • Adds local-server tests for rejected refresh tokens and other HTTP failures.
File Description
packages/​core/​test/​plugin/​provider-chatgpt.test.ts Tests reconnect messaging and fallback HTTP errors.
packages/​core/​src/​plugin/​provider/​chatgpt.ts Decodes refresh rejection responses and provides the reconnect message.

Review effort: Balanced

if (!response.ok) throw new Error(`Request failed: ${response.status}`)
return response.json() as Promise<A>
if (response.ok) return response.json() as Promise<A>
if (rejected && Option.isSome(decodeInvalidGrant(await response.text()))) throw new Error(rejected)
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.

ChatGPT OAuth connection fails with Request failed: 400 until reconnected

3 participants