Skip to content

fix(core): refresh provider OAuth tokens only near expiry - #63645

Open
hcrosse wants to merge 1 commit into
anomalyco:devfrom
hcrosse:fix/provider-oauth-refresh-margin
Open

hcrosse wants to merge 1 commit into
anomalyco:devfrom
hcrosse:fix/provider-oauth-refresh-margin

Conversation

@hcrosse

@hcrosse hcrosse commented Nov 13, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #50759

Type of change

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

What does this PR do?

Provider logins refresh an OAuth token when it expires within Credential.expiring's default margin of 5 minutes. A provider that issues 5-minute access tokens therefore refreshes on every resolve, which also rotates the refresh token each time.

This passes a 30-second margin from Integration.connection.resolve. MCP OAuth keeps its own REFRESH_AHEAD.

How did you verify your code works?

  • Added the reproduction from the issue: a 300-second token resolved three times now makes no refresh. Added a test that a token inside 30 seconds refreshes once. Both fail without the change.
  • bun run test on the integration, integration-refresh, credential and credential-refresh tests passes (34 tests), and so do bun typecheck, oxlint and prettier.
  • I've run the same margin change in my own build against a provider with 5-minute tokens.

Screenshots / recordings

Not a UI change.

Checklist

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

Provider logins used the default 5-minute expiry margin, so a 5-minute token refreshed on every resolve. Use a 30-second margin for provider logins. MCP OAuth keeps its own margin.

Fixes anomalyco#50759
Copilot AI balanced review requested due to automatic review settings November 13, 2026 01:22

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.

🟢 Approval recommended

No active review issues remain for the provider OAuth refresh-margin change.

No open issues 🎉

What changed in this PR

This PR addresses the short-lived-token refresh problem in #50759 by using a 30-second refresh margin for provider OAuth credentials. MCP OAuth behavior remains unchanged.

Changes:

  • Apply the same 30-second margin to both the initial expiry check and the refresh-time stale check.
  • Add regression tests verifying that fresh 300-second tokens are reused and tokens nearing expiry refresh only once.
File Description
packages/​core/​test/​integration-refresh.test.ts Adds coverage for short-lived token reuse and the 30-second refresh threshold.
packages/​core/​src/​integration.ts Applies a shared 30-second expiry check to provider credential resolution and refresh.

Review effort: Balanced

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

This looks good to me. On the base, I ran the reproduction from #50759: a freshly issued 300-second token was refreshed on every resolve (refresh-0, refresh-1, refresh-2). With this change it isn't refreshed at all. Both new tests fail on the base and pass here. The integration and credential tests (34) and tsc -b in packages/core pass.

The same 30-second margin is used for the first check and for the re-check inside Credential.refresh, so the two stay consistent. It matches the margin MCP OAuth already uses (REFRESH_AHEAD).

Public API: unchanged. This is internal to Integration.connection.resolve, and Credential.expiring keeps its signature and default.

No blocking concerns.

This branch has not been deployed

No deployments
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.

V2 provider OAuth refresh races across locations and processes

2 participants