Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
dbdd6fc
feat: run agent tool call requests once and cancel them
mafredri Sep 28, 2026
ecf014b
fix: keep tool call cancels truthful and chat-scoped
mafredri Sep 28, 2026
9f976ec
fix: keep process_output waits independent of the start timeout
mafredri Sep 28, 2026
27fd06a
test(agent): test tool call behavior through tables
mafredri Sep 28, 2026
7707a52
fix: rename the execute wait option to timeout_from_execute
mafredri Sep 28, 2026
d9a8099
test(agent/agenttoolcall): keep only what the real agent cannot reach
mafredri Sep 28, 2026
5658045
test(agent): catch timeout, header, and cross-chat regressions
mafredri Sep 28, 2026
de8bfda
fix(agent/agentproc): key processes by chat ID and process ID
mafredri Sep 29, 2026
af99752
refactor(agent/agenttoolcall): rename entryMaxAge to forgetAfter
mafredri Sep 29, 2026
667b7e0
fix(agent/agentproc): keep exited processes for an hour
mafredri Sep 29, 2026
681d009
docs(agent): correct process retention and tool call ID comments
mafredri Sep 29, 2026
93913e1
test(agent): drop redundant running check
mafredri Sep 29, 2026
97af2a0
docs: say responds instead of answers in comments
mafredri Sep 29, 2026
2a7852f
fix(agent): keep tool call records while they are requested
mafredri Sep 29, 2026
e9b05b3
refactor(agent): rename the tool call kill hook to cancel
mafredri Sep 30, 2026
bbbea78
docs: rewrite tool call comments
mafredri Sep 30, 2026
f4007c2
refactor: rename TimeoutFromExecute to TimeoutFromStartProcess
mafredri Sep 30, 2026
26bff74
docs: put short struct field comments on the field line
mafredri Sep 30, 2026
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
3 changes: 3 additions & 0 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/coder/coder/v2/agent/agentscripts"
"github.com/coder/coder/v2/agent/agentsocket"
"github.com/coder/coder/v2/agent/agentssh"
"github.com/coder/coder/v2/agent/agenttoolcall"
"github.com/coder/coder/v2/agent/boundarylogproxy"
"github.com/coder/coder/v2/agent/proto"
"github.com/coder/coder/v2/agent/proto/resourcesmonitor"
Expand Down Expand Up @@ -344,6 +345,7 @@ type agent struct {
filesAPI *agentfiles.API
gitAPI *agentgit.API
processAPI *agentproc.API
toolCalls *agenttoolcall.Table
desktopAPI *agentdesktop.API
mcpManager *agentmcp.Manager
mcpAPI *agentmcp.API
Expand Down Expand Up @@ -474,6 +476,7 @@ func (a *agent) init() {
return ""
}
a.processAPI = agentproc.NewAPI(a.logger.Named("processes"), a.execer, a.filesystem, pathStore, a.envInfo, a.updateCommandEnv, workingDirFn)
a.toolCalls = agenttoolcall.New(a.clock, a.processAPI.CancelToolCall)
gitOpts := append([]agentgit.Option{agentgit.WithClock(a.clock)}, a.gitAPIOptions...)
a.gitAPI = agentgit.NewAPI(a.logger.Named("git"), pathStore, gitOpts...)
desktop := agentdesktop.NewPortableDesktop(
Expand Down
207 changes: 207 additions & 0 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4572,3 +4572,210 @@ func assertConnectionReport(t testing.TB, agentClient *agenttest.Client, report
}
assert.Equal(t, report.clientSessionID, reports[0].GetConnection().GetClientSessionId(), "connect reason should be %s", report.clientSessionID)
}

func TestAgent_ToolCall(t *testing.T) {
t.Parallel()
//nolint:dogsled
conn, _, _, agentFS, _ := setupAgent(t, agentsdk.Manifest{}, 0)
conn.SetExtraHeaders(http.Header{workspacesdk.CoderChatIDHeader: {uuid.NewString()}})

// Every call uses the tool call context, as chatd does, so a method
// that wrongly sent the tool call ID would get a saved response.
tests := []struct {
name string
fs afero.Fs
act func(ctx context.Context, path string) (any, error)
saved func(workspacesdk.CancelToolCallResponse) (any, error)
}{
{
name: "StartProcess",
fs: afero.NewOsFs(),
act: func(ctx context.Context, path string) (any, error) {
resp, err := conn.StartProcess(ctx, workspacesdk.StartProcessRequest{
Command: "printf run > " + filepath.Base(path),
WorkDir: filepath.Dir(path),
})
if err != nil {
return nil, err
}
out, err := conn.ProcessOutput(ctx, resp.ID, &workspacesdk.ProcessOutputOptions{Wait: true})
if err != nil {
return nil, err
}
if out.ExitCode == nil {
return nil, xerrors.Errorf("process output reports no exit: %+v", out)
}
return resp, nil
},
saved: func(r workspacesdk.CancelToolCallResponse) (any, error) { return r.StartProcessResult() },
},
{
name: "WriteFile",
fs: agentFS,
act: func(ctx context.Context, path string) (any, error) {
return nil, conn.WriteFile(ctx, path, strings.NewReader("run"))
},
saved: func(r workspacesdk.CancelToolCallResponse) (any, error) { return nil, r.WriteFileResult() },
},
{
name: "EditFiles",
fs: agentFS,
act: func(ctx context.Context, path string) (any, error) {
return conn.EditFiles(ctx, workspacesdk.FileEditRequest{
Files: []workspacesdk.FileEdits{{Path: path, Edits: []workspacesdk.FileEdit{{OldText: "reset", NewText: "run"}}}},
IncludeDiff: true,
})
},
saved: func(r workspacesdk.CancelToolCallResponse) (any, error) { return r.EditFilesResult() },
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitLong)
path := filepath.Join(t.TempDir(), "tool-call.txt")
reset := func() { require.NoError(t, afero.WriteFile(tc.fs, path, []byte("reset"), 0o600)) }
requireContent := func(want string) {
t.Helper()
content, err := afero.ReadFile(tc.fs, path)
require.NoError(t, err)
require.Equal(t, want, string(content))
}
id := uuid.New()
toolCtx := workspacesdk.WithToolCallID(ctx, id)

reset()
first, err := tc.act(toolCtx, path)
require.NoError(t, err)
requireContent("run")
reset()
repeat, err := tc.act(toolCtx, path)
require.NoError(t, err)
require.Equal(t, first, repeat)
requireContent("reset")

canceled, err := conn.CancelToolCall(toolCtx, id)
require.NoError(t, err)
require.True(t, canceled.Received)
saved, err := tc.saved(canceled)
require.NoError(t, err)
require.Equal(t, first, saved)

_, err = tc.act(toolCtx, path)
var sdkErr *codersdk.Error
require.ErrorAs(t, err, &sdkErr)
require.Equal(t, http.StatusConflict, sdkErr.StatusCode())
})
}
}

// TestAgent_ToolCallOtherChat checks that two chats using the same tool
// call ID get separate processes and cancels.
func TestAgent_ToolCallOtherChat(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitLong)
agentID := uuid.New()
//nolint:dogsled
connA, _, _, _, _ := setupAgent(t, agentsdk.Manifest{AgentID: agentID}, 0)
connA.SetExtraHeaders(http.Header{workspacesdk.CoderChatIDHeader: {uuid.NewString()}})
// connB shares connA's tailnet connection, which setupAgent closes.
connB := workspacesdk.NewAgentConn(connA.TailnetConn(), workspacesdk.AgentConnOptions{AgentID: agentID})
connB.SetExtraHeaders(http.Header{workspacesdk.CoderChatIDHeader: {uuid.NewString()}})

id := uuid.New()
toolCtx := workspacesdk.WithToolCallID(ctx, id)
_, err := connA.StartProcess(toolCtx, workspacesdk.StartProcessRequest{Command: "sleep 300"})
require.NoError(t, err)
t.Cleanup(func() {
_, err := connA.CancelToolCall(workspacesdk.WithToolCallID(context.Background(), id), id)
assert.NoError(t, err)
})

_, err = connB.StartProcess(toolCtx, workspacesdk.StartProcessRequest{Command: "printf b"})
require.NoError(t, err)
outB, err := connB.ProcessOutput(toolCtx, id.String(), &workspacesdk.ProcessOutputOptions{Wait: true})
require.NoError(t, err)
require.Equal(t, "printf b", outB.Command)
require.Equal(t, "b", outB.Output)

_, err = connB.CancelToolCall(toolCtx, id)
require.NoError(t, err)
// CancelToolCall marks the process canceled before returning, so a
// wrong cancel shows here without waiting.
outA, err := connA.ProcessOutput(toolCtx, id.String(), nil)
require.NoError(t, err)
require.Equal(t, "sleep 300", outA.Command)
require.False(t, outA.Canceled)
}

func TestAgent_ProcessOutputTimeoutFromStartProcess(t *testing.T) {
t.Parallel()
//nolint:dogsled
conn, _, _, _, _ := setupAgent(t, agentsdk.Manifest{}, 0)
conn.SetExtraHeaders(http.Header{workspacesdk.CoderChatIDHeader: {uuid.NewString()}})

// A 1 ms timeout has passed before any wait uses it and one hour cannot
// pass during the test, so no case depends on timing.
tests := []struct {
name string
timeoutMs int64
opts workspacesdk.ProcessOutputOptions
cancel bool
want workspacesdk.ProcessOutputResponse
}{
{
name: "TimeoutFromStartProcess",
timeoutMs: 1,
opts: workspacesdk.ProcessOutputOptions{Wait: true, TimeoutFromStartProcess: true},
want: workspacesdk.ProcessOutputResponse{Running: true, TimedOut: true},
},
{
name: "PlainWait",
timeoutMs: 1,
opts: workspacesdk.ProcessOutputOptions{Wait: true},
cancel: true,
want: workspacesdk.ProcessOutputResponse{Canceled: true},
},
{
name: "BeforeTimeout",
timeoutMs: time.Hour.Milliseconds(),
want: workspacesdk.ProcessOutputResponse{Running: true},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitLong)
id := uuid.New()
toolCtx := workspacesdk.WithToolCallID(ctx, id)
_, err := conn.StartProcess(toolCtx, workspacesdk.StartProcessRequest{Command: "sleep 300", TimeoutMs: tc.timeoutMs})
require.NoError(t, err)
// Cancel the tool call so the agent does not wait for its process
// on close.
t.Cleanup(func() {
_, err := conn.CancelToolCall(workspacesdk.WithToolCallID(context.Background(), id), id)
assert.NoError(t, err)
})

type result struct {
resp workspacesdk.ProcessOutputResponse
err error
}
waited := make(chan result, 1)
go func() {
resp, err := conn.ProcessOutput(toolCtx, id.String(), &tc.opts)
waited <- result{resp, err}
}()
if tc.cancel {
_, err := conn.CancelToolCall(toolCtx, id)
require.NoError(t, err)
}

got := testutil.RequireReceive(ctx, t, waited)
require.NoError(t, got.err)
require.Equal(t, tc.want.Running, got.resp.Running)
require.Equal(t, tc.want.TimedOut, got.resp.TimedOut)
require.Equal(t, tc.want.Canceled, got.resp.Canceled)
})
}
}
13 changes: 9 additions & 4 deletions agent/agentchat/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ import (

// extractContext reads chat identity headers from the request.
// Returns zero values if headers are absent (non-chat request).
func extractContext(r *http.Request) (chatID uuid.UUID, ancestorIDs []uuid.UUID, ok bool) {
func extractContext(r *http.Request) (chatCtx Context, ok bool) {
raw := r.Header.Get(workspacesdk.CoderChatIDHeader)
if raw == "" {
return uuid.Nil, nil, false
return Context{}, false
}
chatID, err := uuid.Parse(raw)
if err != nil {
return uuid.Nil, nil, false
return Context{}, false
}
toolCallID, err := uuid.Parse(r.Header.Get(workspacesdk.CoderToolCallIDHeader))
if err != nil {
toolCallID = uuid.Nil
}
ancestorIDs := []uuid.UUID{}
rawAncestors := r.Header.Get(workspacesdk.CoderAncestorChatIDsHeader)
if rawAncestors != "" {
var ids []string
Expand All @@ -31,5 +36,5 @@ func extractContext(r *http.Request) (chatID uuid.UUID, ancestorIDs []uuid.UUID,
}
}
}
return chatID, ancestorIDs, true
return Context{ID: chatID, AncestorIDs: ancestorIDs, ToolCallID: toolCallID}, true
}
23 changes: 13 additions & 10 deletions agent/agentchat/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type chatContextKey struct{}
type Context struct {
ID uuid.UUID
AncestorIDs []uuid.UUID
ToolCallID uuid.UUID // from Coder-Tool-Call-Id; uuid.Nil if absent or malformed
}

// FromContext returns the chat identity stored on the context.
Expand Down Expand Up @@ -46,7 +47,7 @@ func Fields(ctx context.Context) []slog.Field {
if !ok {
return nil
}
return chatFields(chatCtx.ID, chatCtx.AncestorIDs)
return chatFields(chatCtx)
}

// Middleware tags agent logs for requests that originate from
Expand All @@ -55,30 +56,32 @@ func Fields(ctx context.Context) []slog.Field {
// Install after loggermw.Logger so access-log enrichment can run.
func Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
chatID, ancestorIDs, ok := extractContext(r)
chatCtx, ok := extractContext(r)
if !ok {
next.ServeHTTP(rw, r)
return
}

fields := chatFields(chatID, ancestorIDs)
fields := chatFields(chatCtx)
if requestLogger := loggermw.RequestLoggerFromContext(r.Context()); requestLogger != nil {
requestLogger.WithFields(fields...)
}

ctx := WithContext(r.Context(), chatID, ancestorIDs)
next.ServeHTTP(rw, r.WithContext(ctx))
next.ServeHTTP(rw, r.WithContext(context.WithValue(r.Context(), chatContextKey{}, chatCtx)))
})
}

func chatFields(chatID uuid.UUID, ancestorIDs []uuid.UUID) []slog.Field {
fields := []slog.Field{slog.F("chat_id", chatID.String())}
if len(ancestorIDs) == 0 {
func chatFields(chatCtx Context) []slog.Field {
fields := []slog.Field{slog.F("chat_id", chatCtx.ID.String())}
if chatCtx.ToolCallID != uuid.Nil {
fields = append(fields, slog.F("tool_call_id", chatCtx.ToolCallID.String()))
}
if len(chatCtx.AncestorIDs) == 0 {
return fields
}

ancestors := make([]string, 0, len(ancestorIDs))
for _, id := range ancestorIDs {
ancestors := make([]string, 0, len(chatCtx.AncestorIDs))
for _, id := range chatCtx.AncestorIDs {
ancestors = append(ancestors, id.String())
}
return append(fields, slog.F("ancestor_chat_ids", ancestors))
Expand Down
3 changes: 3 additions & 0 deletions agent/agentchat/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestMiddlewareAccessLog(t *testing.T) {

chatID := uuid.New()
ancestorID := uuid.New()
toolCallID := uuid.New()
sink := testutil.NewFakeSink(t)
handler := tracing.StatusWriterMiddleware(loggermw.Logger(sink.Logger(), nil)(
agentchat.Middleware(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
Expand All @@ -31,6 +32,7 @@ func TestMiddlewareAccessLog(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/test", nil)
req.Header.Set(workspacesdk.CoderChatIDHeader, chatID.String())
req.Header.Set(workspacesdk.CoderAncestorChatIDsHeader, mustMarshalJSON(t, []string{ancestorID.String()}))
req.Header.Set(workspacesdk.CoderToolCallIDHeader, toolCallID.String())
rw := httptest.NewRecorder()
handler.ServeHTTP(rw, req)
require.Equal(t, http.StatusNoContent, rw.Code)
Expand All @@ -40,6 +42,7 @@ func TestMiddlewareAccessLog(t *testing.T) {
fields := fieldsByName(entries[0].Fields)
require.Equal(t, chatID.String(), fields["chat_id"])
require.Equal(t, []string{ancestorID.String()}, fields["ancestor_chat_ids"])
require.Equal(t, toolCallID.String(), fields["tool_call_id"])
}

func TestMiddlewareWithoutChatHeader(t *testing.T) {
Expand Down
Loading
Loading