Skip to content

InMemoryChatMemoryRepository retains the caller's mutable message list #6952

Closed

Description

@pengmoubuaixuexi

Bug description

InMemoryChatMemoryRepository.saveAll stores the supplied List<Message> directly. Mutating that list after saveAll silently changes the stored conversation without another repository write. In contrast, findByConversationId already returns a defensive copy.

Environment

Spring AI 2.0.2-SNAPSHOT, commit 7374b63fcb6fd0e1950e70c9e04192a6826f2f2a (current main when checked before submission). Windows 11, JDK 17.0.20.1. No model service or vector store is required.

Steps to reproduce

Save a mutable list with saveAll, clear the original list, and retrieve the conversation. The saved message has disappeared even though no repository write or deletion occurred after the save.

Minimal Complete Reproducible example

Place this JUnit test in spring-ai-model/src/test/java/org/springframework/ai/chat/memory/:

package org.springframework.ai.chat.memory;

import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import static org.assertj.core.api.Assertions.assertThat;

class ChatMemoryListIsolationTests {
    @Test
    void savedMessagesSurviveCallerListMutation() {
        var repository = new InMemoryChatMemoryRepository();
        Message message = new UserMessage("Hello");
        List<Message> messages = new ArrayList<>(List.of(message));
        repository.saveAll("conversation", messages);
        messages.clear();
        assertThat(repository.findByConversationId("conversation")).containsExactly(message);
    }
}
./mvnw -pl spring-ai-model -am -Dtest=ChatMemoryListIsolationTests -Dsurefire.failIfNoSpecifiedTests=false test

On Windows, use mvnw.cmd. The equivalent regression added to the existing repository test class was executed locally against the unmodified implementation: 1 test, 1 failure, 0 errors; actual history was [].

Expected behavior

Expected: the repository retains the saved message until a subsequent repository write or deletion. Actual: the stored history becomes empty. Applications that clear or reuse their message buffer can therefore lose or replace previously saved conversation history.

A defensive snapshot at the write boundary (List.copyOf(messages)) would isolate the stored list from subsequent structural mutations by the caller. The existing validation already rejects null lists and null elements. This does not attempt to deep-copy individual Message objects.

The proposed regression test is InMemoryChatMemoryRepositoryTests.saveMessagesCopiesCallerOwnedList. With the defensive copy, all 31 repository/message-window tests and the scoped reactor build pass. Source inspection, implementation, and test preparation used AI assistance; the patch was reviewed and approved by the contributor before submission.

Activity

  1. added a commit that references this issue on Sep 10, 2026
    c7f7c73
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions