Skip to content

fix(core): fix list hash stability for equivalent messages#1364

Open
Weilong-Qin wants to merge 7 commits into
agentscope-ai:mainfrom
Weilong-Qin:fix/unstable-computehash
Open

fix(core): fix list hash stability for equivalent messages#1364
Weilong-Qin wants to merge 7 commits into
agentscope-ai:mainfrom
Weilong-Qin:fix/unstable-computehash

Conversation

@Weilong-Qin
Copy link
Copy Markdown

@Weilong-Qin Weilong-Qin commented May 9, 2026

AgentScope-Java Version

1.0.13-SNAPSHOT

Description

Fixes #1357
ListHashUtil.computeHash was using each list item’s hashCode() 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.

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

@Weilong-Qin Weilong-Qin requested review from a team and Copilot May 9, 2026 09:26
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 9, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.computeHash to derive each sampled element hash from JsonUtils.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.

Comment on lines 90 to 93
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(",");
@luowanghaoyun
Copy link
Copy Markdown

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?

@Weilong-Qin
Copy link
Copy Markdown
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]:Unstable computeHash causes unnecessary MysqlSession rewrites

4 participants