Skip to content

Issue: 3930, Fix MessageChatMemoryAdvisor to handle message updates correctly #3940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

wilocu
Copy link
Contributor

@wilocu wilocu commented Jul 29, 2025

Summary

Fixes issue where MessageChatMemoryAdvisor incorrectly handles modified and resent messages by appending them as new messages
instead of replacing the original message and its response.

Fixes #3930

Problem

When a user modifies and resends an existing message, the current implementation:

  • Keeps the old user message in memory
  • Keeps the original AI response in memory
  • Appends the new modified message
  • Generates a new AI response

This leads to inconsistent conversation history with duplicate/conflicting information.

Solution

Enhanced MessageChatMemoryAdvisor and related components to:

  • Track messages using unique identifiers (messageId in metadata)
  • Detect when a message with the same ID already exists
  • Remove the old message and its corresponding assistant response
  • Add the new message, allowing a fresh AI response

Key Changes

  • ChatMemoryRepository: Added findByMessageId() and deleteMessageAndResponse() default methods
  • MessageWindowChatMemory: Added removeMessageAndResponse() method
  • MessageChatMemoryAdvisor: Enhanced with message update detection and handling logic
  • Tests: Added comprehensive test coverage for message update functionality

Backward Compatibility

✅ Fully backward compatible - existing functionality unchanged:

  • Messages without explicit messageId work exactly as before
  • Automatic ID generation based on content hash only affects true duplicates
  • All existing tests pass without modification

Test Coverage

  • Added testMessageUpdateFunctionality() - validates complete update workflow
  • Added testMessageIdGeneration() - ensures consistent ID generation
  • All existing tests continue to pass (8/8 in MessageChatMemoryAdvisorTests, 26/26 in ChatMemory tests)

Usage Example

// User sends initial message
UserMessage original = UserMessage.builder()
.text("What is the capital of France?")
.metadata(Map.of("messageId", "msg-001"))
.build();

// Later, user modifies and resends with same messageId
UserMessage updated = UserMessage.builder()
.text("What is the capital of Italy?")
.metadata(Map.of("messageId", "msg-001"))
.build();

// Result: Old message + response removed, new message added

Test Plan

  • All existing unit tests pass
  • New test cases cover message update scenarios
  • Integration tests with MessageWindowChatMemory
  • Memory management tests verify no regressions
  • Java formatting and checkstyle validation pass

Signed-off-by: Mattia Pasetto [email protected]

I am still pretty new to contributions, please double check and let me know of any errors.

Signed-off-by: Mattia Pasetto <[email protected]>
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.

MessageChatMemoryAdvisor does not handle user message modification correctly
1 participant