Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,20 @@ all nine client pairings, default and shared prefixes, and concurrent constructi
They use unique test keys and never flush a database. These standalone tests do
not certify TLS, Redis Cluster, or high-availability behavior.

## Reading an existing history after upgrading

`findByConversationId` no longer silently skips unreadable records. Jedis, Lettuce,
and Redisson throw `IllegalStateException` with the zero-based list index of the
first record that cannot be decoded as a message (including JSON `null`). No
partial history is returned and no data is changed. If this happens, inspect a
backup and repair or restore the affected record before retrying; do not handle
the exception by saving an empty history.

No key or format migration is required. An absent conversation still returns an
empty list. The message decoder is unchanged: it still accepts legacy JSON strings
and the existing type aliases and retains its compatibility fallback for unknown
message types. This change does not introduce stricter message-schema validation.

## Snapshot replacement with `saveAll`

Jedis, Lettuce, and Redisson validate and serialize the entire replacement before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package io.github.agentic.ai.memory.redis;

import java.util.ArrayList;
import java.util.List;

import io.github.agentic.ai.memory.redis.serializer.MessageDeserializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import tools.jackson.core.JacksonException;
Expand Down Expand Up @@ -74,6 +77,18 @@ protected BaseRedisChatMemoryRepository(@Nullable String keyPrefix) {
}
}

List<Message> deserializeHistory(List<String> records) {
List<Message> history = new ArrayList<>(records.size());
for (String record : records) {
Message message = record == null ? null : deserializeMessage(record);
if (message == null) {
throw new IllegalStateException("Unreadable Redis chat memory record at index " + history.size());
}
history.add(message);
}
return history;
}

protected String serializeMessage(Message message) {
try {
return jsonMapper.writeValueAsString(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public List<Message> findByConversationId(String conversationId) {
if (CollectionUtils.isEmpty(messageStrings)) {
return Collections.emptyList();
}
return messageStrings.stream().map(this::deserializeMessage).filter(Objects::nonNull).collect(Collectors.toList());
return deserializeHistory(messageStrings);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public List<Message> findByConversationId(String conversationId) {
if (CollectionUtils.isEmpty(messageStrings)) {
return Collections.emptyList();
}
return messageStrings.stream().map(this::deserializeMessage).filter(Objects::nonNull).collect(Collectors.toList());
return deserializeHistory(messageStrings);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,7 @@ public List<String> findConversationIds() {
public List<Message> findByConversationId(String conversationId) {
Assert.hasText(conversationId, "conversationId cannot be null or empty");
RList<String> redisList = redissonClient.getList(getKeyPrefix() + conversationId);
return redisList.readAll()
.parallelStream()
.map(this::deserializeMessage)
.filter(Objects::nonNull)
.collect(Collectors.toList());
return deserializeHistory(redisList.readAll());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package io.github.agentic.ai.memory.redis;

import java.util.Map;
import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;

Expand All @@ -26,12 +26,16 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.ToolResponseMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.GenericContainer;
import redis.clients.jedis.Jedis;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

@ResourceLock("redis-deserialization-loggers")
Expand Down Expand Up @@ -83,21 +87,51 @@ private static BaseRedisChatMemoryRepository create(String client) {

@ParameterizedTest
@MethodSource("clientsAndLogLevels")
void readingMixedRecordsKeepsExistingBehaviorWithoutLoggingContents(String client, Level level) throws Exception {
void corruptMiddleRecordFailsWithoutExposingOrChangingHistory(String client, Level level) throws Exception {
String conversationId = "logging-test-" + UUID.randomUUID();
String key = "argi_chat_memory:" + conversationId;
String secret = RedisDeserializationLoggingTest.SECRET;
UserMessage message = UserMessage.builder().text(secret).metadata(Map.of("private", secret)).build();
var call = AssistantMessage.builder().content("")
.toolCalls(List.of(new AssistantMessage.ToolCall("id", "function", "tool", secret))).build();
var reply = ToolResponseMessage.builder()
.responses(List.of(new ToolResponseMessage.ToolResponse("id", "tool", secret))).build();
try (BaseRedisChatMemoryRepository repository = create(client); Jedis connection = new Jedis(host, port)) {
try {
connection.rpush(key, repository.serializeMessage(message), "{\"content\":" + secret + "}");
String[] records = { repository.serializeMessage(call), "{\"content\":" + secret + "}",
repository.serializeMessage(reply) };
connection.rpush(key, records);
try (var logs = new RedisDeserializationLoggingTest.CapturedLogs(level)) {
assertThat(repository.findByConversationId(conversationId)).containsExactly(message);
assertThatIllegalStateException().isThrownBy(() -> repository.findByConversationId(conversationId))
.withMessage("Unreadable Redis chat memory record at index 1").withNoCause();
logs.assertDiagnostic("Deserialization error");
logs.assertSafe();
}
// Reading still leaves both stored records untouched, including the damaged one.
assertThat(connection.llen(key)).isEqualTo(2);
assertThat(connection.lrange(key, 0, -1)).containsExactly(records);
connection.lrem(key, 1, records[1]);
assertThat(repository.findByConversationId(conversationId)).containsExactly(call, reply);
}
finally {
connection.del(key);
}
}
}

@ParameterizedTest
@ValueSource(strings = { "jedis", "lettuce", "redisson" })
void distinguishesEmptyAndLegacyHistoryFromAllUnreadableRecords(String client) throws Exception {
String id = "history-test-" + UUID.randomUUID();
String key = "argi_chat_memory:" + id;
try (BaseRedisChatMemoryRepository repository = create(client); Jedis connection = new Jedis(host, port)) {
try {
assertThat(repository.findByConversationId(id)).isEmpty();
connection.rpush(key, "\"legacy text\"", "{\"role\":\"user\",\"content\":\"second\"}");
assertThat(repository.findByConversationId(id))
.containsExactly(new UserMessage("legacy text"), new UserMessage("second"));
connection.del(key);
connection.rpush(key, "null", "{invalid");
assertThatIllegalStateException().isThrownBy(() -> repository.findByConversationId(id))
.withMessage("Unreadable Redis chat memory record at index 0").withNoCause();
assertThat(connection.lrange(key, 0, -1)).containsExactly("null", "{invalid");
}
finally {
connection.del(key);
Expand Down
Loading