fix(core): fix list hash stability for equivalent messages#1364
fix(core): fix list hash stability for equivalent messages#1364Weilong-Qin wants to merge 7 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes unstable list hashing used by session persistence by switching ListHashUtil.computeHash from element hashCode() to hashing a JSON-serialized representation of sampled elements, aiming to prevent unnecessary full rewrites when lists are logically equivalent.
Changes:
- Update
ListHashUtil.computeHashto derive each sampled element hash fromJsonUtils.getJsonCodec().toJson(item). - Add a unit test asserting that two separately constructed but equivalent
List<Msg>instances produce the same hash.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| agentscope-core/src/main/java/io/agentscope/core/session/ListHashUtil.java | Switch per-item hashing from hashCode() to JSON-serialization-based hashing for better stability across equivalent objects. |
| agentscope-extensions/agentscope-extensions-session-mysql/src/test/java/io/agentscope/core/session/mysql/MysqlSessionTest.java | Add regression test to validate stable hashing for equivalent message lists. |
| for (int idx : sampleIndices) { | ||
| State item = values.get(idx); | ||
| int itemHash = item != null ? item.hashCode() : 0; | ||
| int itemHash = item != null ? JsonUtils.getJsonCodec().toJson(item).hashCode() : 0; | ||
| sb.append(idx).append(":").append(itemHash).append(","); |
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
|
Per-item toJson in computeHash can be expensive (allocations + serialization) on frequent saves. Would it make sense to switch to a cheaper stable fingerprint (e.g. value-based hashCode on persisted State types, or a small dedicated fingerprint API) instead of JSON here? |
OK, I will try to override the hashCode and equals methods based on attribute values for the relevant implementation classes. |
AgentScope-Java Version
1.0.13-SNAPSHOT
Description
Fixes #1357
ListHashUtil.computeHashwas using each list item’shashCode()directly.As a result, two separately constructed but semantically identical message lists could produce different hash values. It leads to unnecessary full rewrites in session persistence paths.
To fix it, hash each item based on its JSON serialization.
Checklist
Please check the following items before code is ready to be reviewed.
mvn spotless:applymvn test)