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
41 lines
1.0 KiB
Go
41 lines
1.0 KiB
Go
package agentchat
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/coder/coder/v2/codersdk/workspacesdk"
|
|
)
|
|
|
|
// extractContext reads chat identity headers from the request.
|
|
// Returns zero values if headers are absent (non-chat request).
|
|
func extractContext(r *http.Request) (chatCtx Context, ok bool) {
|
|
raw := r.Header.Get(workspacesdk.CoderChatIDHeader)
|
|
if raw == "" {
|
|
return Context{}, false
|
|
}
|
|
chatID, err := uuid.Parse(raw)
|
|
if err != nil {
|
|
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
|
|
if err := json.Unmarshal([]byte(rawAncestors), &ids); err == nil {
|
|
for _, s := range ids {
|
|
if id, err := uuid.Parse(s); err == nil {
|
|
ancestorIDs = append(ancestorIDs, id)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return Context{ID: chatID, AncestorIDs: ancestorIDs, ToolCallID: toolCallID}, true
|
|
}
|