Repository navigation
thv llm: complete setup/teardown orchestration - #5068
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
b2a0c20 to
2e8fa0b
Compare
There was a problem hiding this comment.
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 setupflags that merge into persisted config before tool configuration runs. - Trigger interactive OIDC login during setup and start a background
thv llm proxy startwhen 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.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
aponcedeleonch
left a comment
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
aponcedeleonch
left a comment
There was a problem hiding this comment.
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.
Summary
Implements
thv llm setupandthv 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 setupaccepts inline--gateway-url/--issuer/--client-id(and the other config flags), merging them into the persisted config before running. This lets you combineconfig setandsetupinto 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 teardownreverts all (or a single named) tool's config patches.--purge-tokensdeletes cached OIDC tokens from the secrets provider and clears the cached token reference/expiry from config.purgeCachedTokensandhasProxyModeextracted as helpers.loginFuncis injected intorunLLMSetupso unit tests can skip the keyring.E2E tests in
cli_llm_setup_test.gocover: inline-flag config + tool patching, full teardown revert, single-tool targeted teardown, no-config error, and--purge-tokenscleanup.Fixes #5031
Type of change
Test plan
task test)task lint-fix)task test-e2e)API Compatibility
v1beta1API, OR theapi-break-allowedlabel is applied and the migration guidance is described above.Does this introduce a user-facing change?
Yes —
thv llm setupconfigures all detected AI tools and triggers OIDC login in a single command.thv llm teardownreverts those changes. Proxy-mode users runthv llm proxy startseparately 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 runthv llm proxy startthemselves.Follow-up
#5091 — move setup/teardown orchestration to
pkg/llm/so HTTP API and operator callers can reuse it without importing cobra.