Verified Solution[microsoft/vscode] Chat trajectory loss timing issue
Sponsored Content
### ROOT CAUSE
The issue arises due to a race condition or timing mismatch in the chat trajectory handling. When a new message is sent before the previous tool call completes, the system fails to properly cancel the pending tool call and update the chat history. This results in missing assistant messages and incorrect tool result states, particularly when the tool call is cancelled.
### CODE FIX
```typescript
// Add cancellation logic to handle concurrent messages
export function handleNewChatMessage(message: string, pendingToolCall: ToolCall | null) {
if (pendingToolCall) {
// Cancel the pending tool call
pendingToolCall.cancel();
pendingToolCall = null;
}
// Proceed with sending the new message
sendChatMessage(message);
}
// Modify the tool call service to handle cancellations
class ToolCallService {
private activeCalls: Map = new Map();
startToolCall(callId: string, callback: () => void) {
const call = new ToolCall(callId, callback);
this.activeCalls.set(callId, call);
call.start();
}
cancelToolCall(callId: string) {
const call = this.activeCalls.get(callId);
if (call) {
call.cancel();
this.activeCalls.delete(callId);
}
}
}
// Update the chat history to reflect cancellations
function updateChatHistory(newMessage: ChatMessage, cancelledCall: ToolCall | null) {
if (cancelledCall) {
// Add a system message indicating cancellation
const cancellationMessage = {
role: 'system',
content: 'The user cancelled the tool call'
};
newMessage.messages.push(cancellationMessage);
}
// Save the updated chat history
}
```
This fix ensures that:
1. Pending tool calls are properly cancelled when a new message is sent.
2. The chat history reflects the cancellation with a system message.
3. The active calls are tracked and managed to avoid race conditions.
The changes should be integrated into the chat controller and tool call service to handle concurrent messages and cancellations gracefully.
Deploy on DigitalOcean ($200 Credit)
Related Fixes
[gitlab-org/gitlab] Improve display of work items on Milestone page
Memory leak in async runtime
[StackOverflow/python] Django version upgrade while using Django River 3.3.0