fix: target the backend-selected chat agent for desktop, git, and terminal#26959
Draft
DanielleMaywood wants to merge 4 commits into
Draft
Conversation
…t, and task terminal The chat desktop and git watch handlers picked agents[0] from GetWorkspaceAgentsInLatestBuildByWorkspaceID, which has no ORDER BY and includes dev container sub-agents, so Portable Desktop could nondeterministically target a sub-agent. The task page terminal had the same problem via allApps.at(0)?.agent on the frontend. Move chatd's agentselect package out of internal/ and use FindChatAgent in watchChatDesktop and watchChatGit. On the frontend, add getFirstRootAgent and use it for the task terminal target and the terminal page's no-agent-name fallback, skipping agents with parent_id set.
Replace getFirstRootAgent with findChatAgent, a strict TypeScript mirror of agentselect.FindChatAgent, so the task terminal targets the same agent as the chat desktop and git streams: root agents only, sorted by display order, case-insensitive name, name, then ID, with the -coderd-chat suffix preference. Error cases map to undefined. Expose display_order on codersdk.WorkspaceAgent so the frontend can apply the primary sort key, and cross-reference the two implementations in doc comments so they stay in sync.
Docs preview📖 View docs preview for |
…ring selection Replace the frontend TypeScript mirror of agentselect.FindChatAgent with backend-provided data. getChat and listChats now enrich the response with a best-effort agent_id (resolved via FindChatAgent) when a chat has a bound workspace but chatd has not yet persisted the binding, and AgentChatPage uses chat.agent_id instead of falling back to the first workspace agent. Revert the display_order exposure on codersdk.WorkspaceAgent and the TaskPage changes; both existed only to support the frontend mirror. getWorkspaceAgent in chatHelpers now resolves strictly by ID with no first-agent fallback.
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.
On templates with a dev container, Coder Agents' Portable Desktop and web terminal could nondeterministically target the dev container sub-agent instead of the workspace's root agent, making them unusable.
Problem
GetWorkspaceAgentsInLatestBuildByWorkspaceIDhas noORDER BYand includes dev container sub-agents (rows withparent_idset), so its row order is unspecified. The bug surfaced in two places:watchChatDesktopandwatchChatGitincoderd/exp_chats.gousedagents[0]from that query for both the agent conversion and theAgentConndial, so the desktop and git streams could dial a sub-agent, which has no desktop.AgentChatPageresolved its agent viagetWorkspaceAgent(workspace, undefined), whose fallback returned the first agent across all resources, so the embedded web terminal could target a sub-agent.Fix
agentselect.FindChatAgent, the same deterministic selector chatd uses (root agents only; display order, name, ID sort;-coderd-chatsuffix preference). The package moved fromcoderd/x/chatd/internal/tocoderd/x/chatd/agentselect/socoderdcan import it.AgentChatPageuses the backend-selected agent from the chat'sagent_id, andgetWorkspaceAgentresolves strictly by ID with no first-agent fallback; whenagent_idis missing, no agent is shown rather than an arbitrary one.agent_idonce a turn dials the workspace,getChatandlistChatsenrich responses with a best-effortagent_id(resolved viaFindChatAgent, response-only, never persisted) when a chat has a bound workspace but no binding yet.Testing
chatHelpersvitest cases for strict-by-ID resolution.Root cause analysis and decision log
Root cause
GetWorkspaceAgentsInLatestBuildByWorkspaceID(coderd/database/queries/workspaceagents.sql) has noORDER BYand noparent_id IS NULLfilter, so non-deleted dev container sub-agents are included and row order is unspecified.watchChatDesktopandwatchChatGit(coderd/exp_chats.go) usedagents[0]for both thedb2sdk.WorkspaceAgentconversion and theAgentConndial. With a dev container present,agents[0]could be the sub-agent.AgentChatPagecalledgetWorkspaceAgent(workspace, undefined), whose fallback returned the first agent across all resources.Decisions
agentselect.FindChatAgentselector already used bycoderd/x/chatd, rather than writing a new one. Moved it out ofinternal/viagit mv; the package API is unchanged.FindChatAgentin TypeScript (including exposingdisplay_orderoncodersdk.WorkspaceAgent); that was rejected in favor of the backend providing the selected agent, since the chat API already persistsagent_id. The mirror and thedisplay_orderexposure were fully reverted; the net diff contains neither.getChat/listChatsis best-effort and response-only: on any error the field stays null and the UI shows no agent rather than an arbitrary one. Lookups are deduplicated per workspace within a request.getWorkspaceAgentinchatHelperslost its first-agent fallback; its only callers are inAgentChatPage, all of which now pass the chat'sagent_id.Verification
go build ./...,go test ./coderd/x/chatd/..., targetedTestWatchChatGit/TestWatchChatDesktop/TestEnrichChatAgentIDsruns,golangci-linton changed packagestsc -b, biome, vitest forchatHelpers.test.ts, Storybook tests forAgentChatPage(19 passed)make pre-commitpassed on every commitThis PR was generated by Coder Agents on behalf of @DanielleMaywood.