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 @@ -16,6 +16,7 @@
package io.agentscope.core.session;

import io.agentscope.core.state.State;
import io.agentscope.core.util.JsonUtils;
import java.util.List;

/**
Expand Down Expand Up @@ -65,7 +66,7 @@ private ListHashUtil() {
*
* <ul>
* <li>List size
* <li>Hash codes of sampled elements
* <li>Content hashes of sampled elements
* </ul>
*
* <p>This method is designed to be lightweight and fast, using sampling for large lists to
Expand All @@ -88,13 +89,22 @@ public static String computeHash(List<? extends State> values) {

for (int idx : sampleIndices) {
State item = values.get(idx);
int itemHash = item != null ? item.hashCode() : 0;
int itemHash = computeItemHash(item);
sb.append(idx).append(":").append(itemHash).append(",");
}

return Integer.toHexString(sb.toString().hashCode());
}

private static int computeItemHash(State item) {
if (item == null) {
return 0;
}

String serialized = JsonUtils.getJsonCodec().toJson(item);
return serialized.hashCode();
}

/**
* Get the indices to sample from a list of given size.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ void testComputeHashSameListSameHash() {
assertEquals(hash1, hash2);
}

@Test
void testComputeHashEquivalentMsgListsSameHash() {
List<Msg> list1 = createStableMsgList(5);
List<Msg> list2 = createStableMsgList(5);

String hash1 = ListHashUtil.computeHash(list1);
String hash2 = ListHashUtil.computeHash(list2);

assertEquals(hash1, hash2);
}

@Test
void testComputeHashListModifiedDifferentHash() {
List<Msg> list = createMsgList(5);
Expand Down Expand Up @@ -205,4 +216,19 @@ private List<Msg> createMsgList(int size) {
}
return list;
}

private List<Msg> createStableMsgList(int size) {
List<Msg> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
list.add(
Msg.builder()
.id("msg-" + i)
.name("user")
.role(MsgRole.USER)
.content(TextBlock.builder().text("message " + i).build())
.timestamp("2026-01-01 00:00:0" + i + ".000")
.build());
}
return list;
}
}
Loading