Repository navigation
feat: send tool call IDs and cancel unresolved calls on interrupt - #29954
Merged
Merged
Conversation
This was referenced Sep 26, 2026
mafredri
force-pushed
the
mafredri/codagt-757/02-agent-tool-call-records
branch
2 times, most recently
from
September 28, 2026 11:17
0e8c849 to
d420b04
Compare
mafredri
force-pushed
the
mafredri/codagt-757/02-agent-tool-call-records
branch
from
September 28, 2026 13:02
d420b04 to
e71fffb
Compare
mafredri
force-pushed
the
mafredri/codagt-757/02-agent-tool-call-records
branch
from
September 28, 2026 13:41
e71fffb to
64557a1
Compare
mafredri
force-pushed
the
mafredri/codagt-757/02-agent-tool-call-records
branch
from
September 28, 2026 14:04
64557a1 to
85ab12e
Compare
mafredri
force-pushed
the
mafredri/codagt-757/02-agent-tool-call-records
branch
from
September 28, 2026 14:39
85ab12e to
56dde11
Compare
mafredri
force-pushed
the
mafredri/codagt-757/02-agent-tool-call-records
branch
2 times, most recently
from
September 28, 2026 17:27
f212a37 to
f3602fd
Compare
Member
Author
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f3602fd83d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
A task retry or ownership change runs unresolved tool calls again, and an interrupt records a generic result even when the command or edit already ran. Each local tool call now carries a tool call ID computed from committed rows, so the agent can return the first run's result, and the interrupt task asks the agent to cancel unresolved execute, edit_files, and write_file calls and records what it did. Agents without the cancel route keep the generic result. Foreground execute sends its timeout to the agent, which keeps the deadline of the first start, so a retry returns when the agent reports timed_out instead of waiting a full timeout again. chatd's own timeout stays as the backstop for agents that ignore timeout_ms.
An interrupted execute reported "not run" whenever the agent had no record of the call, but the agent forgets calls after an hour while their processes keep running. chatd now reads the process output in that case and reports "not run" only when the agent has no process with the tool call ID. Calls with an empty or repeated provider tool call ID in one message would share a tool call ID, and the second would get the first one's saved response. Those calls now get no tool call ID and keep today's behavior. The interrupt fell back to the generic result without saying why. Each failure to load, dial, or cancel now logs a warning.
The agent now ends a blocking output wait at the start timeout only when the request asks for it, so process_output waits keep their own timeout. The execute tool's foreground wait asks, so a retried execute still waits only for the rest of its timeout. The test's fake agent now honors that contract instead of answering timed_out unconditionally.
The SDK renamed the option that asks the agent to end a blocking output wait at the execute timeout to TimeoutFromExecute. The execute tool's foreground wait uses the new name so the branch builds against the renamed SDK.
The interrupt only commits the agent's real results when the dispatcher and the interrupt compute the same tool call ID. The unit tests faked the agent and checked each side separately, so they could not catch the two sides disagreeing. The new test runs write_file and a long execute on a real agent, interrupts, and checks that the committed results are the agent's write result and a killed command. It replaces the dispatcher unit test and the WriteApplied and ExecuteCanceled rows, which asserted the same behavior with fakes.
The interrupt dialed the agent whenever any call was unresolved, so a chat whose open calls were only other tools could wait up to the cancel timeout on an unreachable workspace. It now keeps only execute, edit_files, and write_file calls that have a tool call ID and returns before dialing when none remain. CancelToolCall no longer checks the tool name, since it only sees those three tools. The interrupt test gains a row for another tool, which must get no cancel and the generic result. It no longer pins the chat header, which the end-to-end test checks, and the row named for a missing cancel route is now CancelError, since it passes a plain error.
Results committed by an interrupt skipped the size cap that chatloop
applies to normal tool results, so a large edit diff or command output
from the agent went into the prompt whole. The interrupt now caps them
with chatloop's truncation at the default budget, since it does not
know the model's context window.
A truncated JSON result is no longer valid JSON, and committing it
fails the whole interrupt. The result is now built with
chatprompt.PartFromContent, which wraps such text as {"output": ...}
as the normal path does.
The agent answers a cancel only after the call's running handler finishes. Sent one at a time under one shared timeout, a slow first cancel left later cancels starting after the deadline, so those calls got the generic interrupted result and their processes kept running. Raise the timeout from 10s to 30s so running tool calls get a chance to finish and the interrupt records their real result.
The interrupt task read the chat's messages for the cancels without the chat lock or the task fence. A stalled task could then read tool calls a newer turn started and cancel them on the agent before its own commit failed the fence. Read them in the task's locked read instead. A failed message read now fails that read and the task retries, as the transaction cannot commit after a failed query.
The agent answers a cancel once it sends SIGKILL, and collects the process's last output when the process exits. A snapshot read right after the cancel could miss that output. Wait for the exit instead; the interrupt's cancel timeout bounds the wait.
Each cancel runs in its own goroutine, outside the recover of executeTaskSafely, so a panic in one crashed coderd. Recover it as that call's error: the call keeps the generic interrupted result and the interrupt commits, as chatloop does for parallel tool calls.
An interrupted execute result read "Ran X for 0ms" in the chat UI: the canceled result looked like a normal run, and the zero duration of a result that was never timed was shown. The canceled result now carries canceled: true from the agent's process output, so the UI can mark the command as killed. The error text stays for the model. A call the agent never received is now a plain-text error, like the edits' "not applied", since nothing ran. wall_duration_ms is omitted when zero, so untimed results show no duration.
An execute result with canceled: true is a command the user's interrupt killed with SIGKILL, so it gets the same killed indicator as a command killed by a process_signal call.
CancelToolCall picked its branch from negated name checks, so any tool other than execute got the edit/write "not applied" result, and the interrupt task's filter repeated the tool names as string literals. Each cancelable tool now has its own case, any other tool is an error, and CanCancelToolCall keeps the filter's list next to the switch.
…cancels The at least once paragraph did not mention that the workspace agent now runs execute, edit_files, and write_file calls once, and the interrupt goroutine steps did not include cancelling those calls on the workspace agent. The TODO that asked for this is removed.
Keep only comments that state a contract, invariant, or reason the code does not show, and write them as Go doc comments.
edit_files and write_file said "not applied" and execute said "not run" for the same outcome, and a killed execute said "canceled by the user". Each outcome now has one message for all tools. The test field agentless becomes notCancelable, since read_file does use the agent.
mafredri
force-pushed
the
mafredri/codagt-757/02-agent-tool-call-records
branch
from
September 30, 2026 11:08
adc0f83 to
18790dc
Compare
This was referenced Sep 30, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
chatd now sends a tool call ID, derived from committed rows, with
executestarts,edit_files, andwrite_file, so a retry or replica takeover gets the first run's result instead of running the tool again. A retried foregroundexecutewaits only for the rest of its timeout.An interrupt cancels the unresolved calls of those three tools on the agent and commits what happened:
tool call was canceled before it ran; no changes were made, the killed command's output withtool call was canceled while running, or the real result. If the cancel fails, including an old agent's 404, the result is today's generic text.Part 2 of 2 of the CODAGT-757 stack.
Depends on #29946.
Fixes https://linear.app/codercom/issue/CODAGT-757
Design: run chat tool calls once, and cancel them truthfully
CODAGT-757: run chat tool calls once, and cancel them truthfully
Status: revision 4, being implemented as two draft PRs. Earlier revisions are kept outside the repo (
/home/coder/wt/PLAN-revision-2.md,PLAN-revision-3.md) and as local tagscodagt-757-rev2/*andcodagt-757-rev3/*.Goal: the fewest lines that stop duplicate side effects and give truthful interrupt results. Rare cases that cost more than they are worth keep today's behavior (section 4).
1. Terms
executeit is also the process ID.Coder-Tool-Call-IdContent-Type, and body the agent returned for a tool call ID the first time.2. User stories
execute, the running or exited process) instead of running again. Holds while the agent keeps running.execute. Foreground and background processes of unresolved calls are killed. The result is "tool call was canceled while running" with the output so far, or the real result if the command had finished.waitUntilfrom the first start, so a retried foregroundexecutewaits only for the rest of its timeout.3. Design
Agent (
agent/agenttoolcall,agentproc)Coder-Tool-Call-Idis read byagentchat.Middlewareintoagentchat.Context.ToolCallID, next to the chat ID (and logged astool_call_id). Handlers and the middleware below read it from the context.A table keyed by chat ID and tool call ID. A record is running (with a channel that closes when the run finishes), done (with the saved response), or canceled.
One middleware, registered once for the whole agent API right after
agentchat.Middleware: a request whose context carries a tool call ID runs once per chat and ID. Without a tool call ID: unchanged. With it:A handler panic saves a 500 saying the outcome is unknown, so repeats and cancels do not wait forever.
POST /api/v0/tool-calls/{id}/cancel, mounted fromTable.Routes(): no record stores a canceled record, kills a process with that ID (it outlives the record), and responds withreceived: false. A record is marked canceled, the cancel waits for the run to finish, kills its process if the ID has one, and responds withreceived: trueand the saved response.Processes are keyed by chat ID and process ID. A process started with a tool call ID uses it as its process ID; a start for a key that already has a process returns that process. Output, signal, list, and kill see only the requester's chat's processes. The start request's
timeout_mssets the process'swaitUntilat the first start; a repeat does not move it, and the process keeps running after it. A start with bothbackgroundand a non-zerotimeout_msis rejected with 400, since only regular output waits read background processes. A blocking output wait withtimeout_from_start_process=true(the execute tool's own wait) returns at exit, atwaitUntil, or at the existing 5-minute cap; other waits, such asprocess_output's, ignorewaitUntil. The output response reportstimed_out(running pastwaitUntil) andcanceled(killed by a cancel).Adding a record deletes records that no request or cancel has used for an hour (
forgetAfter), to bound memory. chatd resends every unresolved call on each retry, and a long batch is retried about every 15 minutes, so a batch that chatd is still retrying keeps its records.Exited processes stay listed for an hour after they exit (5 minutes on
main). Redesigning this retention is CODAGT-1220.SDK (
codersdk/workspacesdk)CoderToolCallIDHeader,WithToolCallID(ctx, id),ToolCallIDFromContext(ctx). OnlyStartProcess,EditFiles, andWriteFilesend the header, because only they act.CancelToolCall(ctx, id) (CancelToolCallResponse, error);CancelToolCallResponse{Received bool; Status int; ContentType string; Body []byte}with one decoder per saved response, each calling the decode code of the method it mirrors:StartProcessResult(),EditFilesResult(),WriteFileResult().StartProcessRequest.TimeoutMs;ProcessOutputOptions.TimeoutFromStartProcess;ProcessOutputResponse.TimedOut,Canceled.chatd (
coderd/x/chatd)The dispatcher gives each tool call's context its tool call ID (
chattool.ToolCallIDs), attached withWithToolCallID. Only the three acting SDK methods send it. A call whose provider tool call ID is empty or repeats in the message gets none and behaves as today, since calls sharing an ID would get each other's saved responses.executeforeground sendstimeout_ms, waits withtimeout_from_start_process=true, and waits until the output response reports exit ortimed_out. chatd's own timeout, counted from each attempt, stays as a backstop, which gives an old agent (it ignorestimeout_ms) today's behavior.The interrupt task reads the unresolved
execute,edit_files, andwrite_filecalls that have a tool call ID in its locked read (the same read that checks the chat is still in the state the task started for). After releasing the lock, it dials the agent once and sends one cancel per call, all at once, under one 30-second timeout. A cancel waits for a running call to finish, so sending them at once keeps one slow call from using up the others' time. A panic in one cancel gives that call the generic text. Results:executeedit_files/write_filereceived: falsecanceledgives tool call was canceled while running with the output so far, marked"canceled": true(the UI shows the killed marker); exited gives the real resultResults go into the existing commit path (
committedPendingLocalToolCancellationMessages,FinishInterruption) as a map by provider tool call ID.4. Not handled
Retries keep today's behavior (the tool runs again):
shprocess only, asprocess_signaldoes today.An
executethat finished early in a batch that stays unresolved (for example next to a long sibling) keeps its record while chatd retries, but its process is removed an hour after it exits, the next time any chat lists processes. Later retries then get an output error ("get process output: ...") instead of its result. It does not run again, but its output is lost (CODAGT-1220).After a retry, an
executeresult'swall_duration_mscounts only the last attempt (a 20-minute timeout showed "for 5m" in UAT): chatd measures it per attempt, and the agent's responses do not carry the process's start time. The timeout itself counts from the first start (CODAGT-1243).Interrupts give a false "canceled before it ran" when the agent has no record of a call that did reach it. Today these cases give the generic text. Accepted to keep the change small; a complete fix needs a record that survives the agent process or a comparison of the call's age with the agent's uptime, both rejected (section 6):
executewhose process exited more than an hour earlier and was dropped from the process list after the record was deleted.A workspace that cannot be reached during the interrupt records no cancel, so a request that arrives later still runs. The committed result is today's generic text.
5. PRs
mafredri/codagt-757/01-sdk-tool-call: agent and SDK.mafredri/codagt-757/02-agent-tool-call-records: chatd.#29958, #29965, #29976, and #29978 are closed as superseded. Branches are rewritten from
mainand force-pushed (operator approval, 2026-09-28).6. Decisions (2026-09-28, operator)
waitUntil(accepted earlier).received: falseforexecutefalls back to the process list, and the remaining false results are documented in section 4 rather than fixed. Attach and kill respect chat boundaries.write_fileis atomic, so a cut-off upload saves a true error); retention counted from run end (recorded runs are short; the process list coversexecute); restricting IDs to three tools and per-route middleware (the SDK sends the header only from the three acting methods); a mixed clock (already one clock); reporting time left on retry (the timeout counts from the first start).waitUntil(timeout_from_execute=true);process_outputwaits keep their own timeout. A background start with a timeout is rejected (operator, after the first review round).entryMaxAgeis renamedforgetAfter.execute, an edit's record could be deleted while chatd still retried the batch, and the edit ran again. Every request or cancel that finds a record now resets its age. This replaces the earlier rejection of retention counted from run end.executecards read "Ran ... for 0ms"): the canceled result passes the agent'scanceledflag through as"canceled": true, and the UI shows it with the marker it already uses for commands killed byprocess_signal; "not run" is a plain-text error like the edits' "not applied" ("Failed to run"); a zerowall_duration_msis left out. Rejected: marking the canceled JSON resultis_error, which says "Failed to run" for a command that ran, makes interrupts the onlyexecutefailures flagged as errors, and would drop the output from the model on replay (only theerrorfield of anis_errorJSON part reaches it). The collapsed card not marking any failed command (exit code, timeout) is a separate UI question. The per-attempt duration after a retry is documented in section 4 (operator: a chatd and agent API issue; a WebSocket between them in the future would remove the blindness, and also the start-then-wait ofexecute).tool call was canceled before it ran; no changes were madeandtool call was canceled while running.CancelToolCallhas a case per tool name and rejects other tools before sending. The agent's cancel hook is namedcancel, notkill, because it also serves edits. ARCHITECTURE.md states the at-most-once exception and the interrupt's cancel step instead of a TODO. Comments keep only what the code does not show.TimeoutFromExecute(timeout_from_execute) is renamedTimeoutFromStartProcess(timeout_from_start_process), because it is part of the agent process API and should not be named after a chatd tool.