Skip to content

Conversation

@assagman
Copy link
Contributor

@assagman assagman commented Jan 15, 2026

Summary

Make ACP and headless mode respect default_agent config and properly handle agent modes and visibilities. Previously, subagents and hidden agents could be incorrectly selected as the default agent.

Fixes #8680

Changes

  • Filter out subagents (mode === "subagent") from defaultAgent candidates
  • Filter out hidden agents (hidden === true) from defaultAgent candidates
  • 4 different errors introduced:
    • default agent "${cfg.default_agent}" not found
    • default agent "${cfg.default_agent}" is a subagent
    • default agent "${cfg.default_agent}" is hidden
    • no primary visible agent found
  • Add 7 test cases for defaultAgent selection logic
  • Persist default mode in session manager during ACP session init

Breaking Changes

None

Testing

  • Added unit tests covering agent selection cases

Copilot AI review requested due to automatic review settings January 15, 2026 14:15
@github-actions
Copy link
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

Based on my search, I found the following potentially related PRs:

Related PRs:

  1. fix(tui): guard default agent selection #7796 - fix(tui): guard default agent selection

    • Directly related to default agent selection logic, though focused on TUI
  2. Add default variant config per agent #7140 - Add default variant config per agent

    • Related to default configuration per agent, though focused on variants rather than mode/visibility filtering
  3. feat(agent): add subagents config for per-agent task tool filtering #7271 - feat(agent): add subagents config for per-agent task tool filtering

    • Handles subagent configuration which is mentioned in this PR's filtering logic
  4. fix(tui): handle undefined agent during login/bootstrap transition #7940 - fix(tui): handle undefined agent during login/bootstrap transition

    • Related to agent selection edge cases during transitions
  5. fix(acp): persist session state to disk and add isReplay metadata #6563 - fix(acp): persist session state to disk and add isReplay metadata

    • Related to ACP mode mentioned in this PR, though focused on session persistence

These are not direct duplicates of PR #8678, but they address related aspects of agent selection, configuration, and mode handling. The most closely related is #7796 which also deals with guarding default agent selection.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request fixes default agent selection in ACP and headless modes to properly respect the default_agent configuration while filtering out unsuitable agents (subagents and hidden agents).

Changes:

  • Modified Agent.defaultAgent() to filter out subagents and hidden agents from default selection
  • Added logic to persist the default agent mode in ACP session initialization
  • Added 7 comprehensive unit tests covering various default agent selection scenarios

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
packages/opencode/src/agent/agent.ts Updated defaultAgent() function with proper filtering logic for subagents and hidden agents with appropriate fallback behavior
packages/opencode/src/acp/agent.ts Added setMode() call to persist default agent mode during ACP session initialization
packages/opencode/test/agent/agent.test.ts Added 7 test cases covering default agent selection including config respect, subagent filtering, hidden agent filtering, non-existent agents, and disabled build scenarios

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

@assagman assagman force-pushed the fix/default-agent-selection branch from 8d1a6ef to 9dc3746 Compare January 15, 2026 16:50
Previously used 3-step fallback (primary -> visible -> build).
Now uses single check: find primary+visible OR hardcoded 'build'.
Same behavior, cleaner implementation.

Signed-off-by: assagman <[email protected]>
Comment on lines 257 to 260
export async function defaultAgent() {
return state().then((x) => Object.keys(x)[0])
const agents = await list()
const primaryVisible = agents.find((a) => a.mode !== "subagent" && a.hidden !== true)
return primaryVisible?.name || "build"
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should prolly throw an error if no primary agent is available... Thoughts?

Copy link
Contributor Author

@assagman assagman Jan 15, 2026

Choose a reason for hiding this comment

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

Yes, I'm currently thinking about it. Maybe you're right.

TUI throws unhandled exception here: https://github.com/anomalyco/opencode/blob/dev/packages/opencode/src/cli/cmd/tui/context/local.tsx#L41-L41

(when no primary visible agent found)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @rekram1-node , I've updated the logic as throwing errors for invalid cases:

  1. cfg.default_agent is set:
    a. cfg.default_agent is not found in state
    b. cfg.default_agent is a subagent
    c. cfg.default_agent is hidden
  2. cfg.default_agent is not set and all of them either subagent or hidden -> no primary visible agent found

@assagman assagman changed the title fix(agent): default agent selection in acp and headless modes fix(agent): default agent selection in acp and headless mode Jan 15, 2026
Copy link
Contributor

Copilot AI left a comment

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 3 out of 3 changed files in this pull request and generated 4 comments.


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

Users now see specific error messages explaining why their configured
default_agent cannot be used (not found, is subagent, or is hidden)
instead of silently falling back to a different agent.

Signed-off-by: assagman <[email protected]>
Copy link
Contributor

Copilot AI left a comment

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 3 out of 3 changed files in this pull request and generated no new comments.


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

@rekram1-node
Copy link
Collaborator

/review

@github-actions
Copy link
Contributor

lgtm

@rekram1-node rekram1-node merged commit 81983d4 into anomalyco:dev Jan 15, 2026
8 of 9 checks passed
@assagman assagman deleted the fix/default-agent-selection branch January 15, 2026 23:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ACP and headless mode does not respect default agent definition

2 participants