Files
coder/agent/agentchat/log.go
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

89 lines
2.5 KiB
Go

package agentchat
import (
"context"
"net/http"
"github.com/google/uuid"
"cdr.dev/slog/v3"
"github.com/coder/coder/v2/coderd/httpmw/loggermw"
)
type chatContextKey struct{}
// Context carries the chat identity associated with an agent request.
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.
func FromContext(ctx context.Context) (Context, bool) {
chatCtx, ok := ctx.Value(chatContextKey{}).(Context)
if !ok || chatCtx.ID == uuid.Nil {
return Context{}, false
}
return chatCtx, true
}
// WithContext stores chat identity on the context for downstream logs.
func WithContext(ctx context.Context, chatID uuid.UUID, ancestorIDs []uuid.UUID) context.Context {
if chatID == uuid.Nil {
return ctx
}
ancestors := make([]uuid.UUID, len(ancestorIDs))
copy(ancestors, ancestorIDs)
return context.WithValue(ctx, chatContextKey{}, Context{
ID: chatID,
AncestorIDs: ancestors,
})
}
// Fields returns structured log fields for the chat identity on ctx.
func Fields(ctx context.Context) []slog.Field {
chatCtx, ok := FromContext(ctx)
if !ok {
return nil
}
return chatFields(chatCtx)
}
// Middleware tags agent logs for requests that originate from
// chatd. Agent log lines emitted while serving a request with Coder-Chat-Id,
// or by background work started by such a request, should include chat_id.
// 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) {
chatCtx, ok := extractContext(r)
if !ok {
next.ServeHTTP(rw, r)
return
}
fields := chatFields(chatCtx)
if requestLogger := loggermw.RequestLoggerFromContext(r.Context()); requestLogger != nil {
requestLogger.WithFields(fields...)
}
next.ServeHTTP(rw, r.WithContext(context.WithValue(r.Context(), chatContextKey{}, chatCtx)))
})
}
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(chatCtx.AncestorIDs))
for _, id := range chatCtx.AncestorIDs {
ancestors = append(ancestors, id.String())
}
return append(fields, slog.F("ancestor_chat_ids", ancestors))
}