Skip to content

thv llm: complete setup/teardown orchestration - #5068

Merged
yrobla merged 5 commits into
mainfrom
issue-5031
Apr 28, 2026
Merged

yrobla merged 5 commits into
mainfrom
issue-5031

Conversation

@yrobla

@yrobla yrobla commented Apr 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Implements thv llm setup and thv llm teardown, the final piece of the LLM gateway authentication epic (#5016).

Why: The tool adapter scaffolding was already in place (detection, config patching, revert), but setup and teardown were stubs. Users had no end-to-end command to configure their AI tools to authenticate through the LLM gateway.

What changed:

  • thv llm setup accepts inline --gateway-url / --issuer / --client-id (and the other config flags), merging them into the persisted config before running. This lets you combine config set and setup into one invocation.

  • OIDC login runs before tool config files are patched. If login fails, no tool config files or persisted state are modified. For proxy-mode tools (Cursor, VS Code, Xcode), setup prints a hint to run thv llm proxy start — the proxy is a normal foreground command, not a background daemon.

  • thv llm teardown reverts all (or a single named) tool's config patches. --purge-tokens deletes cached OIDC tokens from the secrets provider and clears the cached token reference/expiry from config.

  • purgeCachedTokens and hasProxyMode extracted as helpers. loginFunc is injected into runLLMSetup so unit tests can skip the keyring.

  • E2E tests in cli_llm_setup_test.go cover: inline-flag config + tool patching, full teardown revert, single-tool targeted teardown, no-config error, and --purge-tokens cleanup.

Fixes #5031

Type of change

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

Test plan

  • Unit tests (task test)
  • Linting (task lint-fix)
  • E2E tests (task test-e2e)
  • 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.

Does this introduce a user-facing change?

Yes — thv llm setup configures all detected AI tools and triggers OIDC login in a single command. thv llm teardown reverts those changes. Proxy-mode users run thv llm proxy start separately as a foreground process.

Special notes for reviewers

The proxy is intentionally not started automatically by setup — no daemon, no PID tracking, no platform-specific detach logic. Users run thv llm proxy start themselves.

Follow-up

#5091 — move setup/teardown orchestration to pkg/llm/ so HTTP API and operator callers can reuse it without importing cobra.

@github-actions github-actions Bot added the size/M Medium PR: 300-599 lines changed label Apr 27, 2026
@codecov

codecov Bot commented Apr 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.97%. Comparing base (3af3847) to head (86be6ca).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5068      +/-   ##
==========================================
+ Coverage   66.92%   66.97%   +0.04%     
==========================================
  Files         595      595              
  Lines       60080    60080              
==========================================
+ Hits        40208    40238      +30     
+ Misses      16807    16773      -34     
- Partials     3065     3069       +4     

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

@yrobla
yrobla force-pushed the issue-5030 branch 5 times, most recently from b2a0c20 to 2e8fa0b Compare April 28, 2026 07:23
@github-actions github-actions Bot added size/M Medium PR: 300-599 lines changed and removed size/M Medium PR: 300-599 lines changed labels Apr 28, 2026
Base automatically changed from issue-5030 to main April 28, 2026 08:21
@github-actions github-actions Bot added size/M Medium PR: 300-599 lines changed and removed size/M Medium PR: 300-599 lines changed labels Apr 28, 2026
@yrobla
yrobla requested a review from Copilot April 28, 2026 08:30

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

This PR completes the end-to-end thv llm setup / thv llm teardown orchestration by allowing inline config flags during setup, performing an interactive OIDC login as part of setup, and tracking/stopping a background proxy process across CLI invocations.

Changes:

  • Add inline thv llm setup flags that merge into persisted config before tool configuration runs.
  • Trigger interactive OIDC login during setup and start a background thv llm proxy start when proxy-mode tools are configured; persist proxy PID for teardown.
  • Add E2E coverage for setup/teardown flows and update unit tests for the new setup function signature.

Reviewed changes

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

File Description
test/e2e/cli_llm_setup_test.go Adds E2E coverage for setup/teardown, inline flags, targeted teardown, and --purge-tokens.
pkg/llm/config.go Extends ProxyConfig with a persisted PID field.
cmd/thv/app/llm_test.go Updates unit tests to call the new runLLMSetup signature (context + injected login func).
cmd/thv/app/llm.go Implements inline-flag merge for setup, OIDC login during setup, background proxy spawn + PID persistence, and teardown proxy stopping + PID clearing.

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

Comment thread cmd/thv/app/llm.go Resolved
Comment thread cmd/thv/app/llm.go Resolved
Comment thread cmd/thv/app/llm.go Resolved
Comment thread cmd/thv/app/llm.go Resolved
Comment thread cmd/thv/app/llm.go Resolved
Comment thread cmd/thv/app/llm.go Resolved
Add the missing pieces that turn the adapter scaffolding into a working
end-to-end flow:

- thv llm setup now accepts inline --gateway-url / --issuer / --client-id
  (and the other config flags) so "config set" and "setup" can be combined
  in a single command. Flags are merged into the persisted config before
  the rest of setup runs.

- setup triggers an interactive OIDC browser login after configuring all
  detected tools, so the refresh token is stored before any tool or proxy
  tries to use it.

- setup starts the localhost proxy as a background process when any
  proxy-mode tool (Cursor, VS Code, Xcode) was configured, and saves the
  PID to config so teardown can stop it.

- teardown sends SIGINT to the stored proxy PID (if any) before clearing
  the PID from config. The PID is always zeroed in the final UpdateConfig
  call regardless of whether the signal succeeded.

- pkg/llm.ProxyConfig gains a PID field (yaml/json: "pid") to hold the
  background proxy process ID across CLI invocations.

E2E tests in cli_llm_setup_test.go cover: inline-flag config + tool
patching, full teardown revert, single-tool targeted teardown, no-config
error, and --purge-tokens token cleanup.

Fixes #5031
@github-actions github-actions Bot added size/M Medium PR: 300-599 lines changed and removed size/M Medium PR: 300-599 lines changed labels Apr 28, 2026
@yrobla
yrobla requested a review from Copilot April 28, 2026 09:25
@github-actions github-actions Bot added size/M Medium PR: 300-599 lines changed and removed size/M Medium PR: 300-599 lines changed labels Apr 28, 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

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


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

Comment thread cmd/thv/app/llm.go Resolved
Comment thread test/e2e/cli_llm_setup_test.go Resolved
Comment thread cmd/thv/app/llm.go Resolved
@github-actions github-actions Bot added size/M Medium PR: 300-599 lines changed and removed size/M Medium PR: 300-599 lines changed labels Apr 28, 2026
@yrobla
yrobla requested a review from Copilot April 28, 2026 10:50

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

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

Comments suppressed due to low confidence (1)

cmd/thv/app/llm.go:330

  • OIDC login is performed before tool detection, which means users may be forced through an interactive login flow even when no supported AI tools are installed (the command later prints “No supported AI tools detected.”). Consider detecting tools first and only running login when at least one tool will be configured, while still keeping “login before patching” by deferring Apply until after login succeeds.
	// Run OIDC login first so that a failed login leaves no tool config files
	// or persisted state modified.
	_, _ = fmt.Fprintln(out, "Ensuring you are logged in to the LLM gateway…")
	if err := login(ctx, &llmCfg); err != nil {
		return fmt.Errorf("OIDC login failed: %s", llm.SanitizeTokenError(err))
	}
	_, _ = fmt.Fprintln(out, "Login successful.")

	detected := cm.DetectedLLMGatewayClients()
	if len(detected) == 0 {
		_, _ = fmt.Fprintln(out, "No supported AI tools detected.")
		return nil

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

Comment thread cmd/thv/app/llm.go Resolved
Comment thread cmd/thv/app/llm.go Resolved
Comment thread test/e2e/oidc_mock.go Resolved
@github-actions github-actions Bot added size/M Medium PR: 300-599 lines changed and removed size/M Medium PR: 300-599 lines changed labels Apr 28, 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.

Looks solid overall. Nice job on the e2e suite, the fake-browser script and the cancellable auth wait are well thought out. A few inline comments below.

One higher-level thing I wanted to flag: runLLMSetup and runLLMTeardown are now around 100 lines each of orchestration, with login injection, rollback, and atomic persistence. Per .claude/rules/cli-commands.md the CLI layer should be a thin wrapper with business logic in pkg/. Not a blocker for this PR, but worth thinking about before more callers (HTTP API, operator) need the same flow.

A couple of testing gaps I noticed too: I think it's worth adding a unit test that injects a loginFunc returning an error and asserts no tool config files were touched and no config was persisted. The PR description calls this out as a key behavior change but I don't see it covered. Same for the explicit-zero case on --proxy-port 0 once you decide what to do with the flag-loop comment below.

Comment thread cmd/thv/app/llm.go Resolved
Comment thread cmd/thv/app/llm.go Resolved
Comment thread cmd/thv/app/llm.go Resolved
Comment thread cmd/thv/app/llm.go Resolved
@github-actions github-actions Bot added size/M Medium PR: 300-599 lines changed and removed size/M Medium PR: 300-599 lines changed labels Apr 28, 2026
@github-actions github-actions Bot added size/M Medium PR: 300-599 lines changed and removed size/M Medium PR: 300-599 lines changed labels Apr 28, 2026
@yrobla
yrobla requested a review from Copilot April 28, 2026 14:53
@yrobla
yrobla requested a review from aponcedeleonch April 28, 2026 14:55
@github-actions github-actions Bot added size/M Medium PR: 300-599 lines changed and removed size/M Medium PR: 300-599 lines changed labels Apr 28, 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

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


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

Comment thread cmd/thv/app/llm.go Resolved
Comment thread test/e2e/cli_llm_setup_test.go Resolved
Comment thread test/e2e/cli_llm_setup_test.go Resolved
Comment thread cmd/thv/app/llm.go Resolved
@github-actions github-actions Bot removed the size/M Medium PR: 300-599 lines changed label Apr 28, 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.

All four fixes addressed cleanly. The intentional decision to keep the two SetFields calls with the updated comment about preserving concurrent writes is the right call. The login-failure test is exactly the gap I had in mind. Nice extra touch reordering detection before login so an empty detection skips the browser flow entirely.

LGTM.

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

Labels

size/L Large PR: 600-999 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

thv llm: setup and teardown orchestration

4 participants