Skip to content

fix(chat): prefer devcontainer sub-agents over host agent in AI chat#23682

Open
BillionClaw wants to merge 1 commit intocoder:mainfrom
BillionClaw:fix/prefer-subagent-in-chat
Open

fix(chat): prefer devcontainer sub-agents over host agent in AI chat#23682
BillionClaw wants to merge 1 commit intocoder:mainfrom
BillionClaw:fix/prefer-subagent-in-chat

Conversation

@BillionClaw
Copy link

Description

In workspaces with devcontainers, the AI chat always connected to agents[0] from GetWorkspaceAgentsInLatestBuildByWorkspaceID, which is always the host agent. This caused all AI chat commands to run on the host instead of inside the devcontainer.

This PR adds a selectPreferredAgent() helper that prefers agents with a parent_id (sub-agents) over the parent/host agent. Applied to 5 locations across two files.

Changes

  • coderd/x/chatd/chatd.go:
    • Added selectPreferredAgent() helper that iterates agents and returns the first one with a valid ParentID (sub-agent), falling back to agents[0]
    • loadWorkspaceAgentLocked: Use selectPreferredAgent() when creating the initial agent binding and persisting it
    • getWorkspaceConn: Use selectPreferredAgent() in the lazy dial validation fallback
  • coderd/exp_chats.go:
    • Added identical selectPreferredAgent() helper (separate package)
    • watchChatGit: Use selectPreferredAgent() for both the agent status check and the dial
    • watchChatDesktop: Use selectPreferredAgent() for both the agent status check and the dial

No Schema Changes Required

The parent_id field already exists on workspace_agents and is populated for devcontainer sub-agents.

Fixes

Fixes #23547

Testing

No new tests were added. The change is intentionally minimal and localized. The existing tests for GetWorkspaceAgentsInLatestBuildByWorkspaceID already cover the agent ordering.

In workspaces with devcontainers, the AI chat always connected to the
host agent (agents[0]) instead of the devcontainer sub-agent, causing
all AI chat commands to run on the host instead of inside the
devcontainer.

This change adds a selectPreferredAgent() helper that prefers agents
with a parent_id (sub-agents) over the parent/host agent. Applied to:
- loadWorkspaceAgentLocked (chatd.go): persisted agent binding + fallback
- getWorkspaceConn (chatd.go): lazy dial validation fallback
- watchChatGit (exp_chats.go): git operation agent
- watchChatDesktop (exp_chats.go): desktop/VNC agent

No schema changes required.
@github-actions github-actions bot added the community Pull Requests and issues created by the community. label Mar 26, 2026
@github-actions
Copy link


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


I have read the CLA Document and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community Pull Requests and issues created by the community.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Agents: AI chat always targets the host agent, ignoring devcontainer sub-agents

1 participant