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
2 changes: 2 additions & 0 deletions coderd/x/chatd/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,8 @@ Since the runner doesn't wait for goroutines to finish when it cancels them, and

Tool calls have at least once semantics: if the goroutine executes a tool call, and the replica crashes before the result is persisted, another replica will execute the tool call again later. Future work may include adding a mechanism to ensure at most once semantics.

TODO (CODAGT-757): for a workspace agent with API version 2.13 or later, checked once per turn, `execute` sends tool call headers with its start request, so the agent starts at most one process per tool call and a task retry or ownership change gets the same process and result instead of starting another. The agent owns the execute deadline, and a request the agent does not answer is sent again for up to one minute before the result says the outcome is unknown. Rewrite this paragraph.

Parallel tool call results must be inserted in bulk after all parallel tool calls finish in a single `CommitStep` transition so that the generation goroutine only increments `history_version` once, since a change to the `history_version` interrupts the gorotuine. This is consistent with the existing chatd implementation.

The generation goroutine supports:
Expand Down
15 changes: 12 additions & 3 deletions coderd/x/chatd/chatd.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ const (
)

var (
errChatHasNoWorkspaceAgent = xerrors.New("workspace has no running agent: the workspace is likely stopped. Use the start_workspace tool to start it")
errChatWorkspaceDeleted = xerrors.New("the chat's workspace was deleted (for example by dormancy cleanup) and cannot execute tools. Use the create_workspace tool to create a new one")
errChatHasNoWorkspaceAgent = chattool.ErrWorkspaceHasNoAgent
errChatWorkspaceDeleted = chattool.ErrWorkspaceDeleted
errChatAgentDisconnected = xerrors.New(
"workspace agent has been disconnected for at least 90 seconds " +
"and cannot execute tools. To recover, call stop_workspace " +
Expand Down Expand Up @@ -540,6 +540,15 @@ func (c *turnWorkspaceContext) getWorkspaceAgent(ctx context.Context) (database.
return agent, err
}

// isCapableAgent reports whether the chat's workspace agent is a capable
// agent (chattool.IsCapableAgent), judged from the agent row that
// getWorkspaceConn uses. It is false when no agent row loads; the tools
// then get the same error from getWorkspaceConn.
func (c *turnWorkspaceContext) isCapableAgent(ctx context.Context) bool {
agent, err := c.getWorkspaceAgent(ctx)
return err == nil && chattool.IsCapableAgent(agent)
}

func (c *turnWorkspaceContext) ensureWorkspaceAgent(
ctx context.Context,
) (database.Chat, database.WorkspaceAgent, error) {
Expand Down Expand Up @@ -580,7 +589,7 @@ func (c *turnWorkspaceContext) loadWorkspaceAgentLocked(
}

if !chatSnapshot.WorkspaceID.Valid {
return chatSnapshot, database.WorkspaceAgent{}, xerrors.New("this tool requires a workspace and this chat does not have one. Use the create_workspace tool to create one")
return chatSnapshot, database.WorkspaceAgent{}, chattool.ErrChatHasNoWorkspace
}

// A soft-deleted workspace keeps its agent rows, so the bound agent
Expand Down
22 changes: 21 additions & 1 deletion coderd/x/chatd/chatloop/chatloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ type ExecuteLocalToolsOptions struct {
ModelProvider string
ModelName string

// ToolCallIdentity identifies the assistant message containing the
// tool calls; each call's context carries it with the call's own
// ToolCallID and ToolName. Tools see no identity when its ChatID is
// unset.
ToolCallIdentity chattool.ToolCallIdentity

// ContextLimit is the model's context window in tokens. It is used
// to derive a per-result byte budget so a single oversized tool
// result cannot overflow the prompt. Zero means unknown, in which
Expand Down Expand Up @@ -554,6 +560,7 @@ func ExecuteLocalTools(ctx context.Context, opts ExecuteLocalToolsOptions) (Pers
opts.ToolNameAliases,
batchStart,
opts.BillingRecorder,
opts.ToolCallIdentity,
)
for _, execution := range toolExecutions {
tr := execution.content
Expand Down Expand Up @@ -1037,6 +1044,18 @@ type toolExecutionResult struct {
interval BilledInterval
}

// toolCallContext returns the context for running tc in a batch
// identified by batch, carrying tc's chattool.ToolCallIdentity when the
// batch has one.
func toolCallContext(ctx context.Context, batch chattool.ToolCallIdentity, tc fantasy.ToolCallContent) context.Context {
if batch.ChatID == uuid.Nil {
return ctx
}
batch.ToolCallID = tc.ToolCallID
batch.ToolName = tc.ToolName
return chattool.WithToolCallIdentity(ctx, batch)
}

// executeTools runs non-serial calls concurrently, then SerialToolCalls in
// call order. Results are returned in original order after all tools finish.
// recorder, if set, receives live start and completion timestamps.
Expand All @@ -1057,6 +1076,7 @@ func executeTools(
toolNameAliases map[string]string,
batchStart time.Time,
recorder ToolBillingRecorder,
batch chattool.ToolCallIdentity,
) []toolExecutionResult {
if len(toolCalls) == 0 {
return nil
Expand Down Expand Up @@ -1128,7 +1148,7 @@ func executeTools(
}
}()
executions[i].content = executeSingleTool(
ctx,
toolCallContext(ctx, batch, tc),
toolMap,
tc,
metrics,
Expand Down
6 changes: 6 additions & 0 deletions coderd/x/chatd/chatloop/chatloop_run_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/coder/coder/v2/coderd/x/chatd/chaterror"
"github.com/coder/coder/v2/coderd/x/chatd/chatsanitize"
"github.com/coder/coder/v2/coderd/x/chatd/chattest"
"github.com/coder/coder/v2/coderd/x/chatd/chattool"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/testutil"
"github.com/coder/quartz"
Expand Down Expand Up @@ -1009,6 +1010,7 @@ func TestExecuteToolsNotifiesStepToolCallObservers(t *testing.T) {
map[string]string{"observer_alias": "observer_tool"},
time.Time{},
nil,
chattool.ToolCallIdentity{},
)

require.Equal(t, []string{"observer_tool", "other_tool", "denied_tool"}, observedNames,
Expand Down Expand Up @@ -1081,6 +1083,7 @@ func TestExecuteToolsNotifiesStepToolResultObservers(t *testing.T) {
map[string]string{"observer_alias": "observer_tool"},
time.Time{},
nil,
chattool.ToolCallIdentity{},
)

require.Equal(t, 1, notifications, "each called observer is notified once per step")
Expand Down Expand Up @@ -1159,6 +1162,7 @@ func TestExecuteToolsReconcilesResultsBeforeSerialCalls(t *testing.T) {
nil,
time.Time{},
nil,
chattool.ToolCallIdentity{},
)

require.True(t, notified)
Expand Down Expand Up @@ -1227,6 +1231,7 @@ func TestExecuteToolsSerialToolCallOrder(t *testing.T) {
nil,
time.Time{},
nil,
chattool.ToolCallIdentity{},
)

require.Equal(t, []string{"a:start", "a:end", "b:start", "b:end", "c:start", "c:end"}, events,
Expand Down Expand Up @@ -1316,6 +1321,7 @@ func TestExecuteToolsReturnsExecutionIntervals(t *testing.T) {
nil,
batchStart,
liveToolBillingRecorder{started: started, completed: completed},
chattool.ToolCallIdentity{},
)
}()

Expand Down
86 changes: 86 additions & 0 deletions coderd/x/chatd/chatloop/toolcall_identity_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package chatloop

import (
"context"
"sync"
"testing"

"charm.land/fantasy"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/coderd/x/chatd/chattool"
"github.com/coder/coder/v2/testutil"
"github.com/coder/quartz"
)

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

type seen struct {
identity chattool.ToolCallIdentity
ok bool
}
// runBatch runs two parallel calls and one serial call and returns
// the identity each read from its context, by tool call ID.
runBatch := func(t *testing.T, batch chattool.ToolCallIdentity) map[string]seen {
ctx := testutil.Context(t, testutil.WaitShort)
var (
mu sync.Mutex
got = map[string]seen{}
)
record := func(ctx context.Context, call fantasy.ToolCall) (fantasy.ToolResponse, error) {
identity, ok := chattool.ToolCallIdentityFromContext(ctx)
mu.Lock()
got[call.ID] = seen{identity: identity, ok: ok}
mu.Unlock()
return fantasy.NewTextResponse("ok"), nil
}
parallel := fantasy.NewAgentTool("parallel_probe", "records its identity",
func(ctx context.Context, _ struct{}, call fantasy.ToolCall) (fantasy.ToolResponse, error) {
return record(ctx, call)
})
serial := serialMarkerTool{fantasy.NewAgentTool("serial_probe", "records its identity",
func(ctx context.Context, _ struct{}, call fantasy.ToolCall) (fantasy.ToolResponse, error) {
return record(ctx, call)
})}
_, err := ExecuteLocalTools(ctx, ExecuteLocalToolsOptions{
Tools: []fantasy.AgentTool{parallel, serial},
ActiveTools: []string{"parallel_probe", "serial_probe"},
ToolCalls: []fantasy.ToolCallContent{
{ToolCallID: "call-1", ToolName: "parallel_probe", Input: "{}"},
{ToolCallID: "call-2", ToolName: "parallel_probe", Input: "{}"},
{ToolCallID: "call-3", ToolName: "serial_probe", Input: "{}"},
},
ToolCallIdentity: batch,
Clock: quartz.NewReal(),
})
require.NoError(t, err)
return got
}

t.Run("EachCallSeesItsOwnIdentity", func(t *testing.T) {
t.Parallel()

chatID := uuid.New()
got := runBatch(t, chattool.ToolCallIdentity{ChatID: chatID, MessageID: 42})
identity := func(toolCallID, toolName string) seen {
return seen{ok: true, identity: chattool.ToolCallIdentity{
ChatID: chatID, MessageID: 42, ToolCallID: toolCallID, ToolName: toolName,
}}
}
assert.Equal(t, map[string]seen{
"call-1": identity("call-1", "parallel_probe"),
"call-2": identity("call-2", "parallel_probe"),
"call-3": identity("call-3", "serial_probe"),
}, got)
})

t.Run("NoChatIDNoIdentity", func(t *testing.T) {
t.Parallel()

got := runBatch(t, chattool.ToolCallIdentity{})
assert.Equal(t, map[string]seen{"call-1": {}, "call-2": {}, "call-3": {}}, got)
})
}
Loading
Loading