Files
Ethan aedd998db6 fix(coderd/x/chatd): return agent and subagent lookup database errors (#30661)
Relates to CODAGT-1330

When chatd prepares a turn for a chat with a workspace, it looks up the
chat's agent so the agent binding is up to date before anything uses it.
That lookup dropped every error, so a failed database read looked the
same as a chat with no agent, and the turn carried on with whatever
binding the chat already had. If reading the chat's bound agent failed,
we'd also log it and resolve a new agent from scratch. Separately, a
failed read of the computer use provider setting hid the `computer_use`
subagent type from `spawn_agent` for that turn.

To fix, we'll just return the database error, so the prepare retry runs.
Not having a usable agent is still a normal result, e.g. the chat has no
workspace, the workspace was deleted, or more than one agent uses the
chat suffix, so a broken template doesn't fail every turn. A computer
use provider we don't recognise still just makes `computer_use`
unavailable.

This follows on from #30492.
2026-10-19 14:02:46 +11:00

93 lines
2.8 KiB
Go

package agentselect
import (
"cmp"
"slices"
"strings"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/coderd/database"
)
// Suffix marks chat-designated agents during the current PoC. This naming
// convention is an implementation detail, not a stable contract.
const Suffix = "-coderd-chat"
var (
// ErrNoEligibleAgents means the candidates contain no root agent.
ErrNoEligibleAgents = xerrors.New("no eligible workspace agents found")
// ErrMultipleChatAgents means more than one root agent uses Suffix.
ErrMultipleChatAgents = xerrors.New("only one agent should use this suffix")
)
// IsChatAgent reports whether name uses the chat-agent suffix convention.
func IsChatAgent(name string) bool {
return strings.HasSuffix(strings.ToLower(name), Suffix)
}
// FindChatAgent picks the best workspace agent for a chat session from the
// provided candidates. It applies these rules in order:
// 1. Filter to root agents only (ParentID is null).
// 2. Sort stably and deterministically by DisplayOrder ASC, then Name ASC
// (case-insensitive), then Name ASC, then ID ASC.
// 3. If exactly one root agent name ends with Suffix (case-insensitive),
// return it.
// 4. If zero root agents match the suffix, return the first root agent after
// sorting (deterministic fallback).
// 5. If more than one root agent matches the suffix, return an error with an
// actionable message.
// 6. If no root agents exist at all, return an error.
func FindChatAgent(
agents []database.WorkspaceAgent,
) (database.WorkspaceAgent, error) {
rootAgents := make([]database.WorkspaceAgent, 0, len(agents))
matchingAgents := make([]database.WorkspaceAgent, 0, 1)
for _, agent := range agents {
if agent.ParentID.Valid {
continue
}
rootAgents = append(rootAgents, agent)
if IsChatAgent(agent.Name) {
matchingAgents = append(matchingAgents, agent)
}
}
if len(rootAgents) == 0 {
return database.WorkspaceAgent{}, ErrNoEligibleAgents
}
compareAgents := func(a, b database.WorkspaceAgent) int {
if order := cmp.Compare(a.DisplayOrder, b.DisplayOrder); order != 0 {
return order
}
if order := cmp.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name)); order != 0 {
return order
}
if order := cmp.Compare(a.Name, b.Name); order != 0 {
return order
}
return cmp.Compare(a.ID.String(), b.ID.String())
}
slices.SortStableFunc(rootAgents, compareAgents)
slices.SortStableFunc(matchingAgents, compareAgents)
switch len(matchingAgents) {
case 0:
return rootAgents[0], nil
case 1:
return matchingAgents[0], nil
default:
names := make([]string, 0, len(matchingAgents))
for _, agent := range matchingAgents {
names = append(names, agent.Name)
}
return database.WorkspaceAgent{}, xerrors.Errorf(
"multiple agents match the chat suffix %q: %s: %w",
Suffix,
strings.Join(names, ", "),
ErrMultipleChatAgents,
)
}
}