Skip to content

Conversation

@Muhammad-Bin-Ali
Copy link
Contributor

@Muhammad-Bin-Ali Muhammad-Bin-Ali commented Feb 2, 2026

Resolves #790

Summary

Fixes duplicate assistant messages when using addToolApprovalResponse with tools that have needsApproval: true.

Problem

When a user approves a tool via addToolApprovalResponse and then calls sendMessage(), two assistant messages are persisted with the same toolCallId:

  • Original message with server-generated ID (assistant_xxx) stuck in input-available state
  • New message with client-generated ID in approval-responded state

Solution

Wrap addToolApprovalResponse in useAgentChat to notify the server via a new CF_AGENT_TOOL_APPROVAL message type. The server updates the message in place using the existing ID, preventing duplicates when sendMessage() is called.

Changes

  • Add CF_AGENT_TOOL_APPROVAL message type in types.ts
  • Add _applyToolApproval server method to update message in place in index.ts
  • Wrap addToolApprovalResponse in useAgentChat to notify server first in react.tsx

Testing

To test this fix, use the reproduction example on branch reproduce/duplicate-message-needsApproval:

git checkout reproduce/duplicate-message-needsApproval
cd examples/approval-bug-repro
npm install
npm run dev
  1. Click "Send Test Message" to trigger a tool call
  2. Click "Approve" to call addToolApprovalResponse
  3. Check the "Server Persisted Messages" panel - should show only one message per toolCallId

Generated with OpenCode

@Muhammad-Bin-Ali Muhammad-Bin-Ali requested a review from a team February 2, 2026 19:01
@Muhammad-Bin-Ali Muhammad-Bin-Ali self-assigned this Feb 2, 2026
@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 2, 2026

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@829

commit: b359a7e

agents-git-bot bot pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Feb 2, 2026
…lResponse

This syncs documentation for cloudflare/agents PR #829, which fixes
duplicate assistant messages when using addToolApprovalResponse with
tools that have needsApproval.

Changes:
- Add addToolApprovalResponse to useAgentChat API reference
- Document tool approval handling with example
- Update human-in-the-loop guide to explain the fix
- Add changelog entry for the bug fix

Related to: cloudflare/agents#829
@claude
Copy link

claude bot commented Feb 2, 2026

Claude Code Review

Overall Assessment: Strong implementation with comprehensive test coverage. One race condition needs addressing.

Issues Found

Critical: Race Condition in _applyToolApproval (index.ts:2086-2095)

The synchronous SQL update followed by reload creates a race window. Between SQL execution and reload, another client could modify messages, causing lost updates or stale state.

Fix: Add await to the SQL query at line 2087.

Minor: Potential Stale Message in Broadcast (index.ts:2108-2114)

After reloading messages, you call _findMessageByToolCallId again. You could use the already-updated message from line 2079 instead.

Strengths

  • Comprehensive test coverage (6 test cases covering edge cases)
  • Handles streaming messages correctly by checking _streamingMessage first
  • Proper retry logic with backoff for race conditions
  • Good documentation and comments
  • Follows existing patterns for message updates

Architecture Alignment

  • Correctly uses MessageType enum pattern
  • Follows existing _apply method conventions
  • Properly sanitizes messages before persistence
  • Uses _broadcastChatMessage for multi-client sync

PR resolves #790 effectively.

@threepointone
Copy link
Contributor

add a changeset
add some tests
ask opencode if the claude code review feedback is valid, and if so, to address the valid ones

@changeset-bot
Copy link

changeset-bot bot commented Feb 3, 2026

🦋 Changeset detected

Latest commit: b359a7e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@cloudflare/ai-chat Patch
agents Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate assistant messages when using needsApproval tools

2 participants