feat: add slash commands for agent switching#2790
Open
dgageot wants to merge 6 commits into
Open
Conversation
trungutt
previously approved these changes
May 13, 2026
aheritier
requested changes
May 17, 2026
7df0e5a to
7e3d57d
Compare
Member
Author
|
/review |
dgageot
added a commit
to dgageot/cagent
that referenced
this pull request
May 18, 2026
This commit addresses all review feedback from PR docker#2790: **Critical fixes (blocking issues):** 1. **Fix SetCurrentAgent error handling in CLI and TUI** (runner.go, handlers.go) - If SetCurrentAgent fails, we now return an empty message instead of proceeding to send the message to the wrong agent - Added checked type assertion in TUI to prevent potential panics - Both CLI and TUI now properly handle agent switch failures 2. **Add unit tests for PrepareUserMessage and agent switching** (runner_test.go) - Added comprehensive tests for agent-switching commands - Tests cover success cases, failure cases, and edge cases - Tests verify that empty messages are returned on switch failures - Tests verify that agent-only commands (no instruction) work correctly **Non-blocking improvements:** 3. **Update agent-schema.json** (agent-schema.json) - Changed description from 'must be reachable from sub-agent graph' to 'must be defined in team configuration' to match runtime behavior - The runtime validates agent existence but doesn't enforce graph reachability 4. **Add explanatory comment for fallback logic** (commands.go) - Added comment explaining why description falls back to instruction - Clarifies the shorthand command definition behavior 5. **Improve code quality** - Fixed unchecked type assertion in TUI handlers - Used slog.WarnContext instead of slog.Warn for proper context propagation - All linters pass, all tests pass Addresses feedback from @aheritier and docker-agent bot.
Add support for commands that switch the active agent via a new 'agent:' field in the commands section of agent.yaml. Users can now use /plan to switch to a planner sub-agent, /review to switch to a reviewer, etc. Trailing arguments after the command are forwarded to the target agent as the first prompt.
Wires the new agent-switching slash command feature into the built-in coder agent: - Adds a planner sub-agent with read-only tools (filesystem, fetch, todo, user_prompt) - Adds /plan command on root agent to switch to plan mode - Adds symmetric /back command on planner agent to hand work back - Updates root instruction to mention the /plan command
Addresses review feedback from docker-agent bot:
1. In pkg/cli/runner.go (PrepareUserMessage):
- Moved ResolveCommand call BEFORE SetCurrentAgent
- This ensures the command is looked up in the original agent's command table
- Prevents raw slash-command strings from being sent to target agents when
the target agent doesn't have the command defined
2. In pkg/tui/handlers.go (handleAgentCommand):
- Added switchSucceeded flag to track whether agent switch was successful
- Only send resolved message if the agent switch succeeded
- Prevents messages from being sent to the wrong agent when switching fails
This commit addresses all review feedback from PR docker#2790: **Critical fixes (blocking issues):** 1. **Fix SetCurrentAgent error handling in CLI and TUI** (runner.go, handlers.go) - If SetCurrentAgent fails, we now return an empty message instead of proceeding to send the message to the wrong agent - Added checked type assertion in TUI to prevent potential panics - Both CLI and TUI now properly handle agent switch failures 2. **Add unit tests for PrepareUserMessage and agent switching** (runner_test.go) - Added comprehensive tests for agent-switching commands - Tests cover success cases, failure cases, and edge cases - Tests verify that empty messages are returned on switch failures - Tests verify that agent-only commands (no instruction) work correctly **Non-blocking improvements:** 3. **Update agent-schema.json** (agent-schema.json) - Changed description from 'must be reachable from sub-agent graph' to 'must be defined in team configuration' to match runtime behavior - The runtime validates agent existence but doesn't enforce graph reachability 4. **Add explanatory comment for fallback logic** (commands.go) - Added comment explaining why description falls back to instruction - Clarifies the shorthand command definition behavior 5. **Improve code quality** - Fixed unchecked type assertion in TUI handlers - Used slog.WarnContext instead of slog.Warn for proper context propagation - All linters pass, all tests pass Addresses feedback from @aheritier and docker-agent bot.
Address critical review feedback from docker-agent bot: **CLI (runner.go) fixes:** 1. Resolve command content BEFORE switching agents - Previously, SetCurrentAgent was called before ResolveCommand, causing the command lookup to run against the new agent's command table instead of the original agent's. This resulted in raw slash-command strings being sent to the target agent when the command wasn't re-declared. 2. Return explicit error on agent-switch failure - Previously returned an empty UserMessage that was unconditionally added to the session, silently corrupting conversation history. Now returns (nil, "", error) so callers can handle the failure properly. 3. Handle agent-only commands with no trailing args - When a command like /plan has no instruction and no trailing text, return (nil, "", nil) to signal successful agent switch with no message to send, preventing empty messages from being forwarded to the LLM. **TUI (handlers.go) fixes:** Already implemented in the current code: - Check if agent switch succeeded before sending resolved message - Use comma-ok form for type assertion to prevent panics **Test updates:** - Update PrepareUserMessage signature to return error - Fix test expectations to match new nil-message behavior - Add expectError and expectNilMessage flags to test cases All linters and tests passing.
a8dfd3a to
68b8555
Compare
Member
Author
|
/review |
|
❌ PR Review Failed — The review agent encountered an error and could not complete the review. View logs. |
aheritier
approved these changes
May 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds support for slash commands that switch the active agent. For example, declaring
/planin the agent config makes the planner sub-agent take over the conversation when the user types/plan.Usage
A new
agent:field can be set on a command inagent.yaml:When the user types
/plan, the active agent is switched toplanner. Anything typed after the command (e.g./plan add a logout button) is forwarded to the target agent as the first user message.agent:can be combined withinstruction:to switch and send a fixed prompt; on its own it acts as a pure handoff.A complete example lives in
examples/agent_switching_commands.yaml.Changes
pkg/config/types/commands.go— newAgentfield onCommand; YAML parser accepts theagentkey.pkg/runtime/commands.go— newLookupCommandhelper;ResolveCommandforwards trailing args verbatim for agent-only commands.pkg/app/app.go— exposesApp.LookupCommand.pkg/tui/handlers.go—handleAgentCommandswitches the active agent before sending the resolved message.pkg/cli/runner.go—PrepareUserMessagedoes the same for the non-TUI flow.agent-schema.json— documents the newagentproperty.examples/agent_switching_commands.yaml— full example with/plan,/review,/back.pkg/runtime/commands_test.go— unit tests for the new behavior.Validation
task lintclean.task testpasses for all changed packages.task buildsucceeds.