Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions coderd/x/chatd/subagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ type waitAgentArgs struct {
}

type messageAgentArgs struct {
ChatID string `json:"chat_id"`
Message string `json:"message"`
Interrupt bool `json:"interrupt,omitempty"`
ChatID string `json:"chat_id"`
Message string `json:"message"`
BusyBehavior string `json:"busy_behavior,omitempty" enum:"queue,steer,interrupt"`
Comment thread
ethanndickson marked this conversation as resolved.
Resolved
}

type interruptAgentArgs struct {
Expand Down Expand Up @@ -737,11 +737,14 @@ func (p *Server) subagentTools(
"message_agent",
"Send a follow-up message to a previously spawned child "+
"agent. If the agent is idle, it resumes work on the "+
"message. If it is busy, the message is queued behind its "+
"current work and any earlier queued messages; set interrupt "+
"to true for corrections, changed scope, or a handoff that "+
"returns the child's task to you, so its current work stops "+
"first. Interrupting does not clear earlier queued messages, "+
"message. If the agent is busy, busy_behavior controls when "+
"the message arrives: \"queue\" (default) after the agent's "+
"current turn, \"steer\" before the agent's next model call "+
"without stopping its work, \"interrupt\" after the agent's "+
"current step is stopped. Messages already queued for the "+
"agent arrive before it. Use \"interrupt\" for corrections, "+
"changed scope, or a handoff that returns the child's task "+
"to you. Interrupting does not clear earlier queued messages, "+
"and the tool result does not confirm the child has stopped. "+
"Use wait_agent to collect the child's response. A handoff is "+
"acknowledged only when wait_agent returns the child's response "+
Expand All @@ -756,6 +759,17 @@ func (p *Server) subagentTools(
if err != nil {
return fantasy.NewTextErrorResponse(err.Error()), nil
}
var busyBehavior database.ChatBusyBehavior
switch args.BusyBehavior {
case "", string(database.ChatBusyBehaviorQueue):
busyBehavior = database.ChatBusyBehaviorQueue
case string(database.ChatBusyBehaviorSteer):
busyBehavior = database.ChatBusyBehaviorSteer
case string(database.ChatBusyBehaviorInterrupt):
busyBehavior = database.ChatBusyBehaviorInterrupt
default:
return fantasy.NewTextErrorResponse(`busy_behavior must be "queue", "steer", or "interrupt"`), nil
}

parent := currentChat()
var targetChatInfo *database.Chat
Expand All @@ -767,10 +781,6 @@ func (p *Server) subagentTools(
slog.Error(lookupErr),
)
}
busyBehavior := database.ChatBusyBehaviorQueue
if args.Interrupt {
busyBehavior = database.ChatBusyBehaviorInterrupt
}
targetChat, err := p.sendSubagentMessage(
ctx,
parent.ID,
Expand All @@ -783,7 +793,7 @@ func (p *Server) subagentTools(
}

interrupted := false
if args.Interrupt && targetChatInfo != nil {
if busyBehavior == database.ChatBusyBehaviorInterrupt && targetChatInfo != nil {
interrupted = targetChatInfo.Status == database.ChatStatusRunning
}
return toolJSONResponse(withSubagentType(map[string]any{
Expand Down
45 changes: 45 additions & 0 deletions coderd/x/chatd/subagent_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2979,6 +2979,51 @@ func TestSubagentLifecycleToolErrorsIncludePersistedSubagentType(t *testing.T) {
}
}

func TestMessageAgentBusyBehavior(t *testing.T) {
t.Parallel()

db, ps := dbtestutil.NewDB(t)
server := newInternalTestServer(t, db, ps, chatprovider.ProviderAPIKeys{})
user, org, model := seedInternalChatDeps(t, db)

tests := []struct {
busyBehavior string
wantQueued []database.ChatBusyBehavior
wantStatus database.ChatStatus
}{
{"", []database.ChatBusyBehavior{database.ChatBusyBehaviorQueue}, database.ChatStatusRunning},
{"steer", []database.ChatBusyBehavior{database.ChatBusyBehaviorSteer}, database.ChatStatusRunning},
{"interrupt", []database.ChatBusyBehavior{database.ChatBusyBehaviorInterrupt}, database.ChatStatusInterrupting},
{"bogus", nil, database.ChatStatusRunning},
}
for _, tt := range tests {
t.Run("BusyBehavior="+tt.busyBehavior, func(t *testing.T) {
t.Parallel()
ctx := chatdTestContext(t)
parent, child := createParentChildChats(ctx, t, server, user, org, model)
setChatStatus(ctx, t, db, child.ID, database.ChatStatusRunning, "")

resp := runSubagentTool(ctx, t, server, parent, parent.LastModelConfigID, "message_agent", messageAgentArgs{
ChatID: child.ID.String(),
Message: "follow up",
BusyBehavior: tt.busyBehavior,
})
require.Equal(t, tt.wantQueued == nil, resp.IsError, resp.Content)

queued, err := db.GetChatQueuedMessages(ctx, child.ID)
require.NoError(t, err)
var gotQueued []database.ChatBusyBehavior
for _, row := range queued {
gotQueued = append(gotQueued, row.BusyBehavior)
}
require.Equal(t, tt.wantQueued, gotQueued)
storedChild, err := db.GetChatByID(ctx, child.ID)
require.NoError(t, err)
require.Equal(t, tt.wantStatus, storedChild.Status)
})
}
}

func TestSpawnAgent_ComputerUseUsesComputerUseModelNotParent(t *testing.T) {
t.Parallel()

Expand Down
Loading