Repository navigation
Move llm setup/teardown orchestration to pkg/llm - #5112
Conversation
runLLMSetup and runLLMTeardown in cmd/thv/app/llm.go contained ~200 lines of business logic (login injection, rollback, atomic persistence, tool patching). Per cli-commands.md the CLI layer should be a thin wrapper. Move Setup, Teardown, LoginFunc, mergeToolConfigs, hasProxyMode, isTarget, and PurgeTokens to pkg/llm/setup.go. Introduce GatewayManager and ConfigUpdater interfaces so pkg/llm does not need to import pkg/client or pkg/config (which both transitively import pkg/llm). CLI adapters in cmd/thv/app/llm.go bridge the concrete types to these interfaces. Closes #5091
fd47f6e to
6b9fa1d
Compare
There was a problem hiding this comment.
Pull request overview
Refactors the LLM gateway “setup/teardown” orchestration out of the Cobra CLI layer and into pkg/llm, so the workflow can be reused by non-CLI callers without importing CLI-specific dependencies or creating import cycles.
Changes:
- Moved setup/teardown orchestration and helper logic into
pkg/llm/setup.go, introducingGatewayManagerandConfigUpdaterinterfaces. - Updated
cmd/thv/app/llm.goto be a thin wrapper that adaptsclient.ClientManagerandconfig.Providerto the newpkg/llminterfaces. - Relocated helper tests from
cmd/thv/app/llm_test.gointo newpkg/llm/setup_test.go.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| pkg/llm/setup.go | New home for Setup/Teardown orchestration plus helper functions and small interface abstractions. |
| pkg/llm/setup_test.go | Adds unit tests for helper functions moved into pkg/llm. |
| cmd/thv/app/llm.go | Replaces orchestration with calls into pkg/llm and adds CLI adapters to bridge concrete types. |
| cmd/thv/app/llm_test.go | Removes helper tests that were moved into pkg/llm. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #5112 +/- ##
==========================================
+ Coverage 67.08% 67.09% +0.01%
==========================================
Files 595 596 +1
Lines 60000 60122 +122
==========================================
+ Hits 40250 40339 +89
- Misses 16691 16713 +22
- Partials 3059 3070 +11 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
cmd/thv/app/llm.go:151
- In
config reset, tokens are purged before the config is updated. Ifconfig.UpdateConfigfails (lock timeout, disk error, etc.), cached tokens may be deleted while the on-disk config still points at them. Consider flipping the order: first clearc.LLMviaUpdateConfig, then (only on success) callllm.PurgeTokens, matching the safer ordering used inllm.Teardown(clear refs before deleting secrets).
if sp, err := secrets.GetSystemSecretsProvider(); err == nil {
llm.PurgeTokens(cmd.Context(), cmd.ErrOrStderr(), sp)
} else {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Warning: could not get secrets provider: %v\n", err)
}
return config.UpdateConfig(func(c *config.Config) error {
c.LLM = llm.Config{}
return nil
})
💡 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 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if sp, err := secrets.GetSystemSecretsProvider(); err == nil { | ||
| llm.PurgeTokens(cmd.Context(), cmd.ErrOrStderr(), sp) | ||
| } else { | ||
| _, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Warning: could not get secrets provider: %v\n", err) | ||
| } |
There was a problem hiding this comment.
In config reset, tokens are purged before the config update. If config.UpdateConfig fails (e.g., disk full), the secrets may already be deleted while the config still references them, leaving state inconsistent. Consider updating the config first (clearing c.LLM) and only then calling llm.PurgeTokens after a successful write (or otherwise ensuring you don’t delete secrets unless the config reset succeeds).
| // DeleteCachedTokens lists then deletes; for simplicity return empty list so | ||
| // the delete call is skipped — we only need to verify the config refs are cleared. | ||
| sp.EXPECT().ListSecrets(gomock.Any()).Return(nil, nil) |
There was a problem hiding this comment.
TestTeardown_PurgeTokens_ClearsConfigRefsAndDeletesSecrets doesn’t actually assert that any secrets are deleted (it returns an empty list from ListSecrets, so DeleteSecrets is never invoked). Either rename the test to reflect what it verifies (config refs cleared + purge path invoked), or add a non-empty ListSecrets expectation and assert DeleteSecrets is called so the name matches the behavior.
| // DeleteCachedTokens lists then deletes; for simplicity return empty list so | |
| // the delete call is skipped — we only need to verify the config refs are cleared. | |
| sp.EXPECT().ListSecrets(gomock.Any()).Return(nil, nil) | |
| // DeleteCachedTokens lists then deletes; return a non-empty list so the | |
| // delete path is exercised and verified. | |
| sp.EXPECT().ListSecrets(gomock.Any()).Return([]string{"some-ref"}, nil) | |
| sp.EXPECT().DeleteSecrets(gomock.Any(), gomock.Any()).Return(nil) |
Summary
runLLMSetup and runLLMTeardown in cmd/thv/app/llm.go contained ~200 lines of business logic (login injection, rollback, atomic persistence, tool patching). Per cli-commands.md the CLI layer should be a thin wrapper.
Move Setup, Teardown, LoginFunc, mergeToolConfigs, hasProxyMode, isTarget, and PurgeTokens to pkg/llm/setup.go. Introduce GatewayManager and ConfigUpdater interfaces so pkg/llm does not need to import pkg/client or pkg/config (which both transitively import pkg/llm). CLI adapters in cmd/thv/app/llm.go bridge the concrete types to these interfaces.
Fixes #5091
Type of change
Test plan
task test)task test-e2e)task lint-fix)API Compatibility
v1beta1API, OR theapi-break-allowedlabel is applied and the migration guidance is described above.Changes
Does this introduce a user-facing change?
Implementation plan
Approved implementation plan
Special notes for reviewers