Tool Confirmation Bug Fix

Note: For complete tool confirmation documentation, see Tool Confirmation System

Bug Description

When using the refactored EmissaryRefactored component, tool confirmation was failing with the error:

No user message found to resend with confirmed tools

The bug manifested as:

  1. User clicks “Confirm Tools” button
  2. Tools are confirmed (logged to console)
  3. Error message appears: “No user message found to resend with confirmed tools”
  4. Tool confirmation state is cleared
  5. No API call is made
  6. User sees no response despite clicking confirm

Root Cause

The ToolConfirmationContext was not properly accessing messages from the MessageStreamingContext.

Specific Issue: In ToolConfirmationContext.tsx line 140:

const messages = messageContext?.messages || [];

The messageContext prop was undefined because:

  1. ToolConfirmationContext imported useMessageStreaming but didn’t use it
  2. It relied on a messageContext prop that wasn’t being passed correctly in the refactored context hierarchy
  3. This resulted in an empty messages array being passed to getLastUserMessage()
  4. getLastUserMessage([]) returned null
  5. The error was triggered and tool confirmation failed

Solution

Modified ToolConfirmationContext.tsx to properly use the MessageStreamingContext:

Changes Made:

  1. Added context usage (line 63):
    const messageStreamingContext = useMessageStreaming();
    
  2. Fixed message access (line 140): ```typescript // Before: const messages = messageContext?.messages || [];

// After: const messages = messageStreamingContext?.messages || messageContext?.messages || [];


3. **Updated dependencies** (line 206):
```typescript
}, [state.confirmedTools, messageStreamingContext, messageContext, clearConfirmation, setProcessingConfirmation]);

How the Fix Works

  1. ToolConfirmationContext now directly accesses the MessageStreamingContext via useMessageStreaming()
  2. Messages are retrieved from the active context: messageStreamingContext?.messages
  3. Falls back to the prop-based approach for backwards compatibility: messageContext?.messages
  4. getLastUserMessage() receives the actual messages array instead of empty array
  5. Last user message is found successfully
  6. Tool confirmation proceeds with the API call
  7. User sees the expected response

Testing

The fix was verified to:

  • ✅ Properly access messages from MessageStreamingContext
  • ✅ Allow getLastUserMessage() to find user messages
  • ✅ Enable successful tool confirmation flow
  • ✅ Maintain backwards compatibility with prop-based message passing
  • ✅ Pass TypeScript compilation
  • ✅ Pass build process

Impact

This fix resolves the tool confirmation failure in the refactored EmissaryRefactored component, allowing users to:

  • Successfully confirm tool configurations
  • See API responses after tool confirmation
  • Use the refactored app without encountering this blocking error

The fix maintains backward compatibility and doesn’t break existing functionality.