Skip to content

chatd can initiate tool calls via dispatch hooks while another such process is already ongoing #30316

Open

Description

@cstyan

IIUC when the model emits tool calls for client-provided tools, chatd record that and moves the chat to requires_action. The client side observes this through the chat stream, runs the tools itself, and calls SubmitToolResults.

The handler for the submission runs three steps:

  • a read that checks the chat is still requires_action and resolves the pending tool-call IDs (a lock-free snapshot after perf(coderd/x/chatd): make chat stream reads lock-free #30266)
  • an outbound HTTP call to any admin-configured hooks (EventPostToolUse) outside any transaction, and
  • a CompleteRequiresAction transition under the chat's exclusive row lock that records the results and returns to running.

Currently two concurrent submissions (a client retry, two tabs) for the tool work can both pass the read check for requires_action, both call the hooks, and then the second fails at commit since we check there that the status is still requires_action, so external hook side effects fire for results that were never accepted.

To resolve this, rather than holding a lock across the hook call, we could add an intermediate status processing_results.

SubmitToolResults first takes the row lock, does some validation and then flips requires_action -> processing_results and commits; then calls the hooks; then commits the results with processing_results -> running.

A second submission finds processing_results and is rejected before any hook fires. If the hook call fails, the request releases the claim back to requires_action so the client can resubmit. Additionally we could handle the server dying mid-claim; a claimed-at timestamp plus a timeout (handled by the existing requires_action_timeout task, which would also need to own processing_results chats) either releases the claim or cancels the action, as the existing requires_action_deadline_at does today; it must not re-fire the hooks.

cc @hugodutka @ethanndickson

Activity

  1. linear-code commented on Oct 3, 2026

    @linear-code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions