Implement the following plan: Fix: Defer external agent discovery from CLI startup to…
- User
Implement the following plan:
Fix: Defer external agent discovery from CLI startup to hooks execution
Context
DiscoverAndRegisterinhooks_cmd.go:31-33runs duringnewHooksCmd(), which is called duringNewRootCmd()command tree construction (root.go:82). This means external agent discovery (PATH scanning + process spawning forinfocalls) happens on every CLI invocation, includingentire version,entire status, etc. This adds unnecessary overhead.Approach: Cobra RunE fallback for external agents
Key insight: When Cobra can't match a subcommand, it falls back to the parent command's
RunEwith the unmatched args. We use this to lazily discover external agents only when they're actually invoked via hooks.Changes
1.
cmd/entire/cli/hooks_cmd.go— Remove eager discovery, add RunE fallback- Remove
DiscoverAndRegistercall fromnewHooksCmd() - Keep built-in agent subcommand registration (no process spawning, agents already imported via
_imports) - Add
RunEto the hooks command that handles unrecognized subcommands (external agents):
go - Remove
Assistant
Let me start by reading the relevant files.
Assistant
Now let me check how
executeAgentHookshould work — I need to understand the full flow for the RunE fallback. Let me also check what imports are needed.Assistant
Now I have a clear picture. Let me implement the changes:
hooks_cmd.go— Remove eagerDiscoverAndRegister, addRunEfallbackhook_registry.go— ExtractexecuteAgentHookfunction, reuse in both paths
Assistant
Now extract
executeAgentHookinhook_registry.go:Assistant
Now I need to check if
newAgentHookVerbCmdWithLoggingwas using thePersistentPreRunE/PersistentPostRunEfrom the parentnewAgentHooksCmdfor logging. SinceexecuteAgentHooknow callsinitHookLoggingitself, I need to make sure the registered subcommand path doesn't double-initialize logging. Let me look at how this works.Assistant
The registered subcommand path uses
PersistentPreRunE/PersistentPostRunEfor logging, while the RunE fallback path (for external agents) needs its own logging initialization since it bypasses the subcommand tree.The issue is that for registered (built-in) agents,
initHookLoggingwill be called twice — once inPersistentPreRunEand once inexecuteAgentHook. I need to remove the duplicate. The cleanest approach: for built-in agents going through the subcommand path,PersistentPreRunEhandles logging. For the RunE fallback,executeAgentHookhandles it. So I should NOT callinitHookLogginginexecuteAgentHook, but instead handle it only in the RunE fallback.Let me revise:
Assistant
Now update the two call sites:
Assistant
Now update the RunE fallback in
hooks_cmd.go:Assistant
Now let me verify both files look correct:
Assistant
Good. Now let me check if
setup.goneeds changes for external agent discovery duringentire enable. Let me look at whatdetectOrSelectAgentdoes with agent listing:Assistant
The
entire enableflow usesagent.List()andagent.DetectAll()to find agents. External agents won't appear in these lists unlessDiscoverAndRegisteris called first. This is a reasonable gap — duringentire enable, if external agents exist, they should be discoverable. Let me check if the plan says to add this.The plan says: "If agent selection during setup should include external agents, add
DiscoverAndRegisterto the enable flow. Check if this is already handled or needs to be added."Let me check
detectOrSelectAgentmore carefully:Assistant
I think adding external agent discovery to the
entire enableflow makes sense — it ensures users can select external agents during setup. Let me add it at the start of theRunEfor enable, right after the git repo check:Assistant
Now add the discovery call after the git repo check in the enable command:
Assistant
Now let me also check if the
import "time"is still needed inhooks_cmd.go(it is — used forcontext.WithTimeout). Let me check if setup.go needstimeimport... no, it usesctxfrom the command directly, which is fine.Let me run fmt and lint:
Assistant
The lint error is pre-existing (not from my changes — it's in
external.go:462). Let me verify:Assistant
Good — the lint error is in
external.gowhich I didn't modify. It's pre-existing. Let me run tests:Assistant
All tests pass. Here's a summary of the changes:
Changes made
cmd/entire/cli/hooks_cmd.go- Removed eager
external.DiscoverAndRegister()call fromnewHooksCmd()— this was running on every CLI invocation - Added
RunEfallback withArgs: cobra.ArbitraryArgs— when Cobra can't match a subcommand (e.g.,entire hooks my-ext-agent stop), it falls back here withargs["my-ext-agent", "stop"] - The RunE lazily calls
DiscoverAndRegisteronly when an unrecognized agent subcommand is invoked
cmd/entire/cli/hook_registry.go- Extracted
executeAgentHook()function containing the core hook logic (git repo check, enabled check, logging, event parsing, lifecycle dispatch) newAgentHookVerbCmdWithLoggingnow delegates toexecuteAgentHook(cmd, agentName, hookName, false)—falsebecause the parent'sPersistentPreRunEhandles logging- The RunE fallback calls
executeAgentHook(cmd, agentName, hookName, true)— to initialize logging since it bypasses the subcommand tree
- Removed eager
- User
which other outstanding issues we have?
Assistant
Let me check the branch status and any TODO/issue markers.
Assistant
No TODOs. Let me check the pre-existing lint issue and look for other rough edges.
Assistant
Here's what's outstanding on the
nodo/external-pluginsbranch:1. Lint error —
external.go:462(wrapcheck)bytes.Buffer.Writeerror isn't wrapped. Easy fix:goreturn w.buf.Write(p) // needs fmt.Errorf wrappingThough
bytes.Buffer.Writenever actually returns an error, the linter flags it.2. Uncommitted changes (just done)
The lazy discovery refactor we just did — 3 files modified but not yet committed.
No other code issues found
- No TODOs/FIXMEs added on this branch
- All tests pass (
test:ci) - No other lint errors beyond the one above
Want me to fix the lint error in
external.go:462and/or commit the lazy discovery changes?- User
fix liniting and run the tests
Assistant
Verify
fmtis already imported:Assistant
Good. Now run fmt, lint, and tests:
Assistant
Lint is clean. Running tests:
Assistant
All clean — 0 lint issues, all tests pass.
- User
don't remove the
nolint:ireturncomments Assistant
No
nolint:ireturncomments were removed by any changes on this branch. The diff shows none were touched. Is there a specific file you're seeing this in?