Skip to content

pkg/llm: implement OIDC token source and thv llm token command - #5033

Merged
yrobla merged 2 commits into
mainfrom
yrobla/thv-llm-token
Apr 24, 2026
Merged

yrobla merged 2 commits into
mainfrom
yrobla/thv-llm-token

Conversation

@yrobla

@yrobla yrobla commented Apr 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds a three-tier TokenSource (in-memory → cached refresh token → browser OIDC+PKCE flow) for the LLM gateway. Access tokens are held in memory only; refresh tokens are persisted via ScopeLLM secrets. A 30 s preemptive refresh window avoids gateway rejections on expiry.

thv llm token is now fully implemented: non-interactive, prints a fresh JWT to stdout with all other output on stderr, suitable for use as apiKeyHelper or auth.command in Claude Code / Cursor.

Fixes #5028

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

  • Unit tests (task test)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

API Compatibility

  • This PR does not break the v1beta1 API, OR the api-break-allowed label is applied and the migration guidance is described above.

Changes

File Change

Does this introduce a user-facing change?

Implementation plan

Approved implementation plan

Special notes for reviewers

Large PR Justification

This is an isolated PR that covers a single functionality. Cannot be split.

@github-actions github-actions Bot added the size/M Medium PR: 300-599 lines changed label Apr 23, 2026
@yrobla
yrobla requested a review from Copilot April 23, 2026 13:27
@codecov

codecov Bot commented Apr 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 61.98830% with 65 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.77%. Comparing base (ee6a892) to head (cb2ae1f).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
pkg/llm/tokensource.go 61.98% 60 Missing and 5 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5033      +/-   ##
==========================================
+ Coverage   69.75%   69.77%   +0.01%     
==========================================
  Files         557      558       +1     
  Lines       56254    56425     +171     
==========================================
+ Hits        39239    39368     +129     
- Misses      13996    14032      +36     
- Partials     3019     3025       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Pull request overview

Implements OIDC token lifecycle management for the LLM gateway and wires it into the thv llm token CLI helper so external tools can fetch a fresh access token non-interactively.

Changes:

  • Added pkg/llm.TokenSource implementing a three-tier strategy (in-memory → secrets-backed refresh token → interactive browser flow).
  • Added unit tests for key derivation, scope handling, and non-interactive error behavior.
  • Implemented thv llm token to print a fresh JWT to stdout while keeping diagnostics on stderr.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
pkg/llm/tokensource.go New LLM gateway token source with tiered token acquisition + persistence hooks.
pkg/llm/tokensource_test.go New unit tests covering core helper functions and non-interactive behavior.
cmd/thv/app/llm.go Implements the thv llm token command execution path using the new token source.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/llm/tokensource.go Resolved
Comment thread pkg/llm/tokensource.go Resolved
Comment thread pkg/llm/tokensource.go Resolved
Comment thread pkg/llm/tokensource.go Resolved
Comment thread pkg/llm/tokensource.go Resolved
@github-actions github-actions Bot added size/L Large PR: 600-999 lines changed and removed size/M Medium PR: 300-599 lines changed labels Apr 23, 2026
@yrobla
yrobla requested a review from Copilot April 23, 2026 13:47

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.

Pull request overview

Implements the LLM gateway OIDC token lifecycle needed for thv llm by adding a three-tier token source (in-memory → cached refresh token → interactive browser flow) and wiring up thv llm token to emit a fresh access token for external tools.

Changes:

  • Added pkg/llm.TokenSource with deterministic refresh-token secret keys, refresh-token persistence, and a 30s preemptive refresh window.
  • Implemented thv llm token as a non-interactive command that prints only the JWT to stdout.
  • Added unit tests covering key derivation, scope handling, cache-restore behavior, and preemptive refresh behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
pkg/llm/tokensource.go New three-tier OIDC TokenSource with refresh-token persistence and preemptive refresh behavior.
pkg/llm/tokensource_test.go Unit tests validating TokenSource helpers, cache restore paths, and refresh-window logic.
cmd/thv/app/llm.go Implements thv llm token to fetch a fresh token non-interactively and print it to stdout.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/llm/tokensource.go Resolved
Comment thread pkg/llm/tokensource.go Resolved
Comment thread pkg/llm/tokensource.go Resolved
Comment thread pkg/llm/tokensource_test.go Resolved
@yrobla
yrobla force-pushed the yrobla/thv-llm-token branch from 3091f94 to 5edaa27 Compare April 23, 2026 14:05
@github-actions github-actions Bot added size/L Large PR: 600-999 lines changed and removed size/L Large PR: 600-999 lines changed labels Apr 23, 2026
@yrobla
yrobla requested a review from Copilot April 23, 2026 14:10

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.

Pull request overview

Implements an OIDC-based token lifecycle for the LLM gateway along with a non-interactive thv llm token CLI helper that prints a usable access token to stdout for integration with external AI tools.

Changes:

  • Added pkg/llm.TokenSource implementing a 3-tier strategy (in-memory → refresh token via secrets provider → interactive browser OIDC+PKCE flow) with a 30s preemptive refresh window.
  • Added unit tests covering secret-key derivation, cache-miss vs backend-error behavior, persisting refresh tokens, and preemptive refresh behavior.
  • Implemented thv llm token to print a fresh JWT to stdout (diagnostics to stderr) and persist refresh-token references back into config.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
pkg/llm/tokensource.go New LLM gateway OIDC token source with cache restore + persistence + preemptive refresh behavior.
pkg/llm/tokensource_test.go Unit test coverage for key token-source behaviors and edge cases.
cmd/thv/app/llm.go Implements thv llm token command to emit a valid access token and persist token references.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

jerm-dro
jerm-dro previously approved these changes Apr 23, 2026
Comment thread pkg/llm/tokensource.go Resolved
Base automatically changed from yrobla/thv-llm-foundation to main April 24, 2026 07:33
@yrobla
yrobla dismissed jerm-dro’s stale review April 24, 2026 07:33

The base branch was changed.

@yrobla
yrobla requested a review from blkt as a code owner April 24, 2026 07:33
Adds a three-tier TokenSource (in-memory → cached refresh token →
browser OIDC+PKCE flow) for the LLM gateway. Access tokens are held
in memory only; refresh tokens are persisted via ScopeLLM secrets.
A 30 s preemptive refresh window avoids gateway rejections on expiry.

thv llm token is now fully implemented: non-interactive, prints a
fresh JWT to stdout with all other output on stderr, suitable for use
as apiKeyHelper or auth.command in Claude Code / Cursor.

Part of #5028

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@yrobla
yrobla force-pushed the yrobla/thv-llm-token branch from 5edaa27 to 3c7f67a Compare April 24, 2026 08:17
@github-actions github-actions Bot removed the size/L Large PR: 600-999 lines changed label Apr 24, 2026
@github-actions github-actions Bot added the size/L Large PR: 600-999 lines changed label Apr 24, 2026
@yrobla
yrobla requested review from Copilot and jerm-dro April 24, 2026 08:23

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.

Pull request overview

Implements OIDC token lifecycle support for the LLM gateway, including a three-tier token acquisition strategy and a non-interactive thv llm token helper intended for integration with external AI tools.

Changes:

  • Added pkg/llm.TokenSource implementing in-memory → cached refresh token → (interactive) browser OIDC+PKCE fallback with a 30s preemptive refresh window.
  • Added unit tests covering key behaviors of secret-key derivation, scope normalization, cache restore behavior, and preemptive expiry shifting.
  • Implemented thv llm token command to print a fresh access token to stdout and persist refresh-token references back into config.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
pkg/llm/tokensource.go New OIDC token source with caching, refresh-token persistence, and preemptive refresh handling for LLM gateway auth.
pkg/llm/tokensource_test.go New unit tests for token source helpers and cache restore/persist behavior.
cmd/thv/app/llm.go Implements thv llm token using the new token source (non-interactive; stdout token, stderr diagnostics).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/llm/tokensource.go Resolved
Comment thread pkg/llm/tokensource.go Resolved
Comment thread pkg/llm/tokensource_test.go Resolved
Comment thread pkg/llm/tokensource.go Resolved
@yrobla
yrobla force-pushed the yrobla/thv-llm-token branch from 3c7f67a to e38ffa2 Compare April 24, 2026 08:58
@github-actions github-actions Bot added size/XL Extra large PR: 1000+ lines changed and removed size/L Large PR: 600-999 lines changed labels Apr 24, 2026

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

Large PR Detected

This PR exceeds 1000 lines of changes and requires justification before it can be reviewed.

How to unblock this PR:

Add a section to your PR description with the following format:

## Large PR Justification

[Explain why this PR must be large, such as:]
- Generated code that cannot be split
- Large refactoring that must be atomic
- Multiple related changes that would break if separated
- Migration or data transformation

Alternative:

Consider splitting this PR into smaller, focused changes (< 1000 lines each) for easier review and reduced risk.

See our Contributing Guidelines for more details.


This review will be automatically dismissed once you add the justification section.

@github-actions github-actions Bot added size/XL Extra large PR: 1000+ lines changed and removed size/XL Extra large PR: 1000+ lines changed labels Apr 24, 2026
@github-actions
github-actions Bot dismissed their stale review April 24, 2026 09:11

Large PR justification has been provided. Thank you!

@github-actions

Copy link
Copy Markdown
Contributor

✅ Large PR justification has been provided. The size review has been dismissed and this PR can now proceed with normal review.

@yrobla
yrobla force-pushed the yrobla/thv-llm-token branch from e38ffa2 to 1ef77f8 Compare April 24, 2026 09:17
@yrobla
yrobla requested a review from Copilot April 24, 2026 09:17
@github-actions github-actions Bot added size/XL Extra large PR: 1000+ lines changed and removed size/XL Extra large PR: 1000+ lines changed labels Apr 24, 2026

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.

Pull request overview

Implements an OIDC-based token acquisition flow for the LLM gateway, plus a thv llm token CLI command intended to emit a fresh JWT for external tool integration.

Changes:

  • Added pkg/llm.TokenSource implementing a multi-tier token retrieval strategy with preemptive refresh behavior.
  • Implemented thv llm token to print an access token to stdout while keeping diagnostics on stderr.
  • Added unit tests covering key token-source behaviors (cache restore, refresh window logic, persistence callbacks).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
pkg/llm/tokensource.go New LLM TokenSource with OIDC refresh, persistence, and preemptive refresh behavior.
pkg/llm/tokensource_test.go New unit tests for the LLM TokenSource behavior.
cmd/thv/app/llm.go Implements thv llm token command using the new token source.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/llm/tokensource.go Resolved
Comment thread pkg/llm/tokensource_test.go Resolved
@yrobla
yrobla force-pushed the yrobla/thv-llm-token branch from 1ef77f8 to cb2ae1f Compare April 24, 2026 09:25
@github-actions github-actions Bot added size/XL Extra large PR: 1000+ lines changed and removed size/XL Extra large PR: 1000+ lines changed labels Apr 24, 2026

@aponcedeleonch aponcedeleonch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Three inline comments on the new token source: one architectural (the bulk of this file duplicates pkg/registry/auth/oauth_token_source.go), and two concrete correctness issues (withPreemptiveRefresh composition, and the _AT cache not being invalidated on refresh-token rotation). Happy to pair on the shared extraction if that's the direction.

Comment thread pkg/llm/tokensource.go Resolved
Comment thread pkg/llm/tokensource.go Resolved
Comment thread pkg/llm/tokensource.go Resolved
@yrobla
yrobla merged commit 854e0ab into main Apr 24, 2026
51 checks passed
@yrobla
yrobla deleted the yrobla/thv-llm-token branch April 24, 2026 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XL Extra large PR: 1000+ lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

thv llm: OIDC token lifecycle and thv llm token command

5 participants