Files
coder/agent/agentchat/log_test.go
T
Mathias Fredriksson 1ba09d2ce2 feat: run chat tool calls once per tool call ID on the agent (#29946)
chatd resends unresolved tool calls on every retry and replica
takeover, and each resend of execute started a new process: one
command in CODAGT-757 started 36 times. An interrupt could neither
stop the process nor report what happened.

The agent now runs a request that carries Coder-Tool-Call-Id once
per chat and tool call ID and responds to repeats with the saved
response. StartProcess, EditFiles, and WriteFile send the header.
POST /api/v0/tool-calls/{id}/cancel refuses later requests for the
call, waits for a running one, stops its process, and returns the
saved response.

A process started with a tool call ID uses it as its process ID, and
its timeout_ms counts from the first start, so a retry does not
restart the timeout. Processes are keyed by chat ID: a chat sees only
its own processes, and a request without a chat ID sees only
processes started without one. Exited processes are kept for an hour
instead of 5 minutes so that retries can still read their output.

Requests without the header behave as before. Old agents ignore the
header and respond to a cancel with 404.

Refs CODAGT-757
2026-09-30 14:08:21 +03:00

107 lines
3.2 KiB
Go

package agentchat_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"cdr.dev/slog/v3"
"github.com/coder/coder/v2/agent/agentchat"
"github.com/coder/coder/v2/coderd/httpmw/loggermw"
"github.com/coder/coder/v2/coderd/tracing"
"github.com/coder/coder/v2/codersdk/workspacesdk"
"github.com/coder/coder/v2/testutil"
)
func TestMiddlewareAccessLog(t *testing.T) {
t.Parallel()
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) {
rw.WriteHeader(http.StatusNoContent)
})),
))
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)
entries := sink.Entries()
require.Len(t, entries, 1)
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) {
t.Parallel()
sink := testutil.NewFakeSink(t)
handler := tracing.StatusWriterMiddleware(loggermw.Logger(sink.Logger(), nil)(
agentchat.Middleware(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
rw.WriteHeader(http.StatusNoContent)
})),
))
rw := httptest.NewRecorder()
handler.ServeHTTP(rw, httptest.NewRequest(http.MethodGet, "/test", nil))
require.Equal(t, http.StatusNoContent, rw.Code)
entries := sink.Entries()
require.Len(t, entries, 1)
fields := fieldsByName(entries[0].Fields)
require.NotContains(t, fields, "chat_id")
require.NotContains(t, fields, "ancestor_chat_ids")
}
func TestMiddlewareContextFields(t *testing.T) {
t.Parallel()
chatID := uuid.New()
sink := testutil.NewFakeSink(t)
handler := tracing.StatusWriterMiddleware(loggermw.Logger(sink.Logger(), nil)(
agentchat.Middleware(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
sink.Logger().With(agentchat.Fields(r.Context())...).Info(r.Context(), "handler log")
rw.WriteHeader(http.StatusNoContent)
})),
))
req := httptest.NewRequest(http.MethodGet, "/test", nil)
req.Header.Set(workspacesdk.CoderChatIDHeader, chatID.String())
rw := httptest.NewRecorder()
handler.ServeHTTP(rw, req)
require.Equal(t, http.StatusNoContent, rw.Code)
entries := sink.Entries()
require.Len(t, entries, 2)
for _, entry := range entries {
if entry.Message != "handler log" {
continue
}
fields := fieldsByName(entry.Fields)
require.Equal(t, chatID.String(), fields["chat_id"])
return
}
t.Fatal("handler log entry not found")
}
func fieldsByName(fields []slog.Field) map[string]any {
byName := make(map[string]any, len(fields))
for _, field := range fields {
byName[field.Name] = field.Value
}
return byName
}