Skip to content

Move llm setup/teardown orchestration to pkg/llm - #5112

Merged
yrobla merged 2 commits into
mainfrom
refactor/llm-pkg-setup-teardown
Apr 29, 2026
Merged

yrobla merged 2 commits into
mainfrom
refactor/llm-pkg-setup-teardown

Conversation

@yrobla

@yrobla yrobla commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

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

  • 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

@github-actions github-actions Bot added the size/L Large PR: 600-999 lines changed label Apr 29, 2026
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
@yrobla
yrobla force-pushed the refactor/llm-pkg-setup-teardown branch from fd47f6e to 6b9fa1d Compare April 29, 2026 09:07
@yrobla
yrobla requested a review from Copilot April 29, 2026 09:08
@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 29, 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

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, introducing GatewayManager and ConfigUpdater interfaces.
  • Updated cmd/thv/app/llm.go to be a thin wrapper that adapts client.ClientManager and config.Provider to the new pkg/llm interfaces.
  • Relocated helper tests from cmd/thv/app/llm_test.go into new pkg/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.

Comment thread pkg/llm/setup.go Resolved
Comment thread pkg/llm/setup.go Resolved
Comment thread pkg/llm/setup.go Resolved
Comment thread pkg/llm/setup.go Resolved
@codecov

codecov Bot commented Apr 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 78.68852% with 26 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.09%. Comparing base (9c66743) to head (502bd4a).
⚠️ Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
pkg/llm/setup.go 78.68% 16 Missing and 10 partials ⚠️
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.
📢 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.

@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 29, 2026
@yrobla
yrobla requested a review from Copilot April 29, 2026 09:45

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 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. If config.UpdateConfig fails (lock timeout, disk error, etc.), cached tokens may be deleted while the on-disk config still points at them. Consider flipping the order: first clear c.LLM via UpdateConfig, then (only on success) call llm.PurgeTokens, matching the safer ordering used in llm.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.

@yrobla
yrobla merged commit b27d310 into main Apr 29, 2026
51 checks passed
@yrobla
yrobla deleted the refactor/llm-pkg-setup-teardown branch April 29, 2026 10:00

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 2 comments.


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

Comment thread cmd/thv/app/llm.go
Comment on lines +143 to +147
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)
}

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

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

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

Copilot uses AI. Check for mistakes.
Comment thread pkg/llm/setup_test.go
Comment on lines +127 to +129
// 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)

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

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

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.

Suggested change
// 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)

Copilot uses AI. Check for mistakes.
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.

refactor: move llm setup/teardown orchestration to pkg/llm

5 participants