Skip to content

fix(coderd): skip validation for unexpired chat git tokens - #31921

Merged
mafredri merged 9 commits into
mainfrom
mafredri/chat-git-token-skip-validation
Nov 12, 2026
Merged

mafredri merged 9 commits into
mainfrom
mafredri/chat-git-token-skip-validation

Conversation

@mafredri

@mafredri mafredri commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

On main, resolveChatGitAccessToken calls Config.RefreshToken for every stored access token it reads. When OAuthToken().Valid() is true, RefreshToken still sends a provider validation request if a validation URL is configured. The resolver returns the stored access token whether validation succeeds or fails, so these requests count against the provider's API rate limit without changing token selection.

Skip RefreshToken when OAuthToken().Valid() is true. The check treats a token within 10 seconds of expiry as expired. A token with no expiry counts as valid if its access token is non-empty. Other RefreshToken callers still perform provider validation.

Verified with: TestResolveChatGitAccessToken, TestGetChatDiffStatus, TestGetChatDiffContents, TestRefreshToken/SaveBeforeValidate and make pre-commit.

Part of CODAGT-1543.

Decision log
  • Keep the check in the chat resolver instead of adding a shared skip-validation option: other RefreshToken callers depend on provider validation. Direct TokenSource token refresh would bypass existing coordination and persistence.
  • Resolver tests use stored access tokens and a local provider to check token selection, token refresh requests and persistence. Tokens passing OAuthToken().Valid() send no provider validation requests. Provider validation after token refresh is covered by coderd/externalauth tests, including TestRefreshToken/SaveBeforeValidate.
  • Follow-up (CRF-4): for an expired token, failed token refresh still returns the stored access token. If token refresh succeeds but provider validation fails, RefreshToken has already stored the new access token, yet the resolver returns the expired pre-refresh access token. This PR preserves both behaviors. The matching-origin path does not log RefreshToken errors, while the fallback path logs them at debug level; leave that difference for this follow-up.

🤖 Created with the help of Coder Agents.

@mafredri
mafredri temporarily deployed to main November 9, 2026 12:05 — with GitHub Actions Inactive
@mafredri
mafredri temporarily deployed to main November 9, 2026 12:05 — with GitHub Actions Inactive
@linear-code

linear-code Bot commented Nov 9, 2026

Copy link
Copy Markdown

CODAGT-1543

@mafredri
mafredri force-pushed the mafredri/chat-git-token-skip-validation branch from 06f5bab to b28f38f Compare November 9, 2026 13:20
@mafredri
mafredri temporarily deployed to main November 9, 2026 13:20 — with GitHub Actions Inactive
@mafredri
mafredri temporarily deployed to main November 9, 2026 13:20 — with GitHub Actions Inactive
@mafredri

This comment has been minimized.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T20:28:25.921479Z fb8d7a1 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

This comment has been minimized.

@mafredri
mafredri marked this pull request as ready for review November 9, 2026 14:54
@mafredri
mafredri temporarily deployed to main November 9, 2026 14:54 — with GitHub Actions Inactive

@coder-agents-review coder-agents-review 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 PR makes resolveChatGitAccessToken skip Config.RefreshToken for stored tokens that pass OAuthToken().Valid(), which removes one provider validation request per call. Findings: 7 P3, 1 P4, 2 Nit.

Out of scope (needs a ticket or explicit acceptance by a human):

  • coderd/exp_chats.go:5014: when origin matches config A and the chat owner has no usable link for A, the fallback loop returns the owner's token for another config B, and getChatDiffContents (exp_chats.go:4767) and resolveChatDiffReference (exp_chats.go:4828) send that token to A's provider API, for example a github.com token sent to a GitHub Enterprise host.
  • coderd/exp_chats.go:5049: when refreshing an expired token fails, the resolver returns the expired token, so the caller's next provider API request fails with a provider error instead of the caller receiving gitsync.ErrNoTokenAvailable.

Notes:

  • coderd/exp_chats.go:5045: hasConfig is always true, because configs and providerIDs are built from api.ExternalAuthConfigs in the same loop.
  • coderd/exp_chats_internal_test.go:49: with exp_chats.go restored to the base SHA, the Unexpired and NoExpiry subtests fail with one provider validation request; at head all 8 subtests pass.
  • coderd/exp_chats.go:5000: the resolver now repeats the token.Valid() check that RefreshToken uses to decide whether to refresh (externalauth.go:285). If RefreshToken changes that trigger, this resolver keeps skipping tokens that RefreshToken would refresh.

🤖 This review was automatically generated with Coder Agents.

Comment thread coderd/exp_chats.go
if !link.OAuthToken().Valid() {
//nolint:gocritic // System context carried through for token refresh.
refreshed, refreshErr := config.RefreshToken(dbauthz.AsSystemRestricted(ctx), api.Database, link)
if refreshErr == nil {

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.

P3 [CRF-4] When token refresh succeeds and provider validation then fails, the resolver returns the expired pre-refresh token although the refreshed token is already stored. (Mafuuu, Mafu-san, Leorio, Knov)

refreshAndValidateToken (coderd/externalauth/externalauth.go:485-509) writes the new token, then returns updatedAuthLink, err when validateWithRetry fails, and both loops assign link = refreshed only when refreshErr == nil (also exp_chats.go:5056). A scratch test with an expired stored token, a succeeding /token, and a /validate returning 401 gave returned=stored-access-token stored=refreshed-access-token. After this PR every RefreshToken call in this function is a refresh, and a rate-limited validation endpoint, the condition this PR targets, makes this case more likely. The decision log defers this without a ticket; in both loops, assign link = refreshed when refreshed.OAuthToken().Valid(), whatever refreshErr is.

🤖

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The observed returned-token behavior is real, but both loops already do this on main. The PR intentionally preserves that policy, and its decision log describes the new-token-stored/old-token-returned effect as a separate follow-up. Adopting the refreshed link despite a validation error would change production token selection beyond removing unnecessary provider validation, so that behavior remains unchanged here.

🤖 Posted using /amend-review skill via Coder Agents.

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.

Moved to out of scope: main has the same outcome, and this PR only re-indents the if refreshErr == nil lines. It still has no ticket, so a human needs to file one or accept the behavior. The doc comment describes this case incorrectly; that is on the doc comment thread.

🤖

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agreed that this is inherited behavior outside this PR's scope, as your latest reply confirms. The existing decision log explicitly records the returned-token effect as a separate follow-up. Keeping production token selection unchanged here and leaving this thread open.

🤖 Posted using /amend-review skill via Coder Agents.

Comment thread coderd/exp_chats.go Resolved
Comment thread coderd/exp_chats.go Resolved
Comment thread coderd/exp_chats.go Resolved
Comment thread coderd/exp_chats.go Resolved
Comment thread coderd/exp_chats.go Resolved
Comment thread coderd/exp_chats_internal_test.go Resolved
Comment thread coderd/exp_chats_internal_test.go Resolved
Comment thread coderd/exp_chats_internal_test.go Resolved
Comment thread coderd/exp_chats_internal_test.go Resolved
@mafredri
mafredri force-pushed the mafredri/chat-git-token-skip-validation branch from b28f38f to 6f90bb2 Compare November 9, 2026 19:54
@mafredri
mafredri temporarily deployed to main November 9, 2026 19:55 — with GitHub Actions Inactive
@mafredri

This comment has been minimized.

@chatgpt-codex-connector

This comment has been minimized.

coder-agents-review[bot]

This comment was marked as outdated.

Chat git token reads validate unexpired tokens even though the resolver
returns the stored token when validation fails. These provider requests
consume GitHub quota without changing the token used for git requests.

Refresh only tokens that oauth2 considers expired. Keep shared token
validation and refresh-failure behavior unchanged.
Chat token consumers make provider API requests, not git transport
requests. Name that access-check boundary in the resolver comment.
Keep chat token tests focused on provider traffic and returned tokens.
Shared refresh validation counts and lease calls are not chat contracts.

Use one PostgreSQL fixture with isolated users to check persisted tokens
without scripting the shared refresh implementation.
Provider selection and the local expiry check are separate decisions.
Keep their conditions explicit without changing token refresh or
failure handling.
Keep provider setup outside the resolver subtests so token selection,
token refresh requests and persistence can be read together. Name the
OAuth configuration and token response fields explicitly.
The chat token fixture accepts a context, so place it first to follow
the repository's Go API convention enforced by revive.
Pin the stored-token fallback for invalid-grant errors in both resolver
lookup paths. Keep token selection and provider validation policy
unchanged while documenting that contract at the resolver boundary.
RefreshToken can persist a new access token before provider validation
returns an error. Name the returned value by when it was read instead
of implying that it always matches the current database row.
@mafredri
mafredri force-pushed the mafredri/chat-git-token-skip-validation branch from 6f90bb2 to fb8d7a1 Compare November 9, 2026 20:25
@mafredri
mafredri temporarily deployed to main November 9, 2026 20:25 — with GitHub Actions Inactive
@mafredri

This comment has been minimized.

@mafredri

This comment has been minimized.

@chatgpt-codex-connector

This comment has been minimized.

@coder-agents-review coder-agents-review 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.

Round 3 reviews fb8d7a1, which rewrites the resolver doc comment; no new findings. The doc comment now matches both loops for every RefreshToken error path, and no in-scope findings remain open.

Out of scope (needs a ticket or explicit acceptance by a human):

  • coderd/exp_chats.go:5003: when refresh succeeds and validation fails, both loops return the expired pre-refresh token while the row holds the refreshed one, so one chat git request fails per occurrence. The decision log calls it a follow-up but links no ticket.
  • coderd/exp_chats.go:5014: when the owner has no usable link for the origin's provider, the fallback returns another provider's token, which getChatDiffContents and resolveChatDiffReference send to the origin's provider API.
  • coderd/exp_chats.go:5049: when refreshing an expired token fails, the resolver returns the expired token, so callers get a provider error instead of gitsync.ErrNoTokenAvailable.
  • coderd/exp_chats.go:5046: the origin and fallback loops duplicate the read, refresh, and return sequence and differ in how they log refreshErr, so any token-selection change must be made in both.

🤖 This review was automatically generated with Coder Agents.

Distinguish provider validation from the local expiry check and describe
its ten-second and no-expiry rules explicitly. Keep token selection and
error handling unchanged.
@mafredri
mafredri temporarily deployed to main November 9, 2026 22:37 — with GitHub Actions Inactive
@mafredri
mafredri merged commit a48a8c0 into main Nov 12, 2026
29 checks passed
@mafredri
mafredri deleted the mafredri/chat-git-token-skip-validation branch November 12, 2026 12:04
@mafredri
mafredri temporarily deployed to main November 12, 2026 12:04 — with GitHub Actions Inactive
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 12, 2026

This branch was previously deployed

1 inactive deployment
main — 685a1d95 Deployed Nov 12, 2026 by mafredri via cla #254886
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants