File tree Expand file tree Collapse file tree
main/java/io/agentscope/core/session
test/java/io/agentscope/core/session Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1616package io .agentscope .core .session ;
1717
1818import io .agentscope .core .state .State ;
19+ import io .agentscope .core .util .JsonUtils ;
1920import java .util .List ;
2021
2122/**
@@ -65,7 +66,7 @@ private ListHashUtil() {
6566 *
6667 * <ul>
6768 * <li>List size
68- * <li>Hash codes of sampled elements
69+ * <li>Content hashes of sampled elements
6970 * </ul>
7071 *
7172 * <p>This method is designed to be lightweight and fast, using sampling for large lists to
@@ -88,13 +89,22 @@ public static String computeHash(List<? extends State> values) {
8889
8990 for (int idx : sampleIndices ) {
9091 State item = values .get (idx );
91- int itemHash = item != null ? item . hashCode () : 0 ;
92+ int itemHash = computeItemHash ( item ) ;
9293 sb .append (idx ).append (":" ).append (itemHash ).append ("," );
9394 }
9495
9596 return Integer .toHexString (sb .toString ().hashCode ());
9697 }
9798
99+ private static int computeItemHash (State item ) {
100+ if (item == null ) {
101+ return 0 ;
102+ }
103+
104+ String serialized = JsonUtils .getJsonCodec ().toJson (item );
105+ return serialized .hashCode ();
106+ }
107+
98108 /**
99109 * Get the indices to sample from a list of given size.
100110 *
Original file line number Diff line number Diff line change @@ -55,6 +55,17 @@ void testComputeHashSameListSameHash() {
5555 assertEquals (hash1 , hash2 );
5656 }
5757
58+ @ Test
59+ void testComputeHashEquivalentMsgListsSameHash () {
60+ List <Msg > list1 = createStableMsgList (5 );
61+ List <Msg > list2 = createStableMsgList (5 );
62+
63+ String hash1 = ListHashUtil .computeHash (list1 );
64+ String hash2 = ListHashUtil .computeHash (list2 );
65+
66+ assertEquals (hash1 , hash2 );
67+ }
68+
5869 @ Test
5970 void testComputeHashListModifiedDifferentHash () {
6071 List <Msg > list = createMsgList (5 );
@@ -205,4 +216,19 @@ private List<Msg> createMsgList(int size) {
205216 }
206217 return list ;
207218 }
219+
220+ private List <Msg > createStableMsgList (int size ) {
221+ List <Msg > list = new ArrayList <>();
222+ for (int i = 0 ; i < size ; i ++) {
223+ list .add (
224+ Msg .builder ()
225+ .id ("msg-" + i )
226+ .name ("user" )
227+ .role (MsgRole .USER )
228+ .content (TextBlock .builder ().text ("message " + i ).build ())
229+ .timestamp ("2026-01-01 00:00:0" + i + ".000" )
230+ .build ());
231+ }
232+ return list ;
233+ }
208234}
You can’t perform that action at this time.
0 commit comments