Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0d4bbd6
fix(core): fix list hash stability for equivalent messages
Weilong-Qin May 9, 2026
774b4cd
Move ListHashUtil hash test to core
Weilong-Qin May 9, 2026
a0f5e43
Merge branch 'agentscope-ai:main' into fix/unstable-computehash
Weilong-Qin May 11, 2026
0392f38
refactor(core): add value-based equals and hashCode methods to the Ms…
Weilong-Qin May 12, 2026
004b226
Merge remote-tracking branch 'origin/fix/unstable-computehash' into f…
Weilong-Qin May 12, 2026
cd4e0fe
test(core): add test
Weilong-Qin May 13, 2026
a94bbb0
test(core): add Msg test
Weilong-Qin May 13, 2026
32c03f1
fix(core): stabilize ToolUseBlock hashCode for Gemini thoughtSignature
Weilong-Qin May 26, 2026
bec4729
Merge branch 'main' into fix/unstable-computehash
Weilong-Qin Jun 4, 2026
765e942
Merge branch 'main' into fix/unstable-computehash
Weilong-Qin Jun 4, 2026
628b3a0
fix: variable name updated
Weilong-Qin Jun 4, 2026
d1ebe31
Merge branch 'main' into fix/unstable-computehash
Weilong-Qin Jun 5, 2026
b766ddf
Merge branch 'main' into fix/unstable-computehash
Weilong-Qin Jun 10, 2026
731c3a0
Merge branch 'main' into fix/unstable-computehash
Weilong-Qin Jun 12, 2026
166c697
Merge branch 'main' into fix/unstable-computehash
Weilong-Qin Jun 16, 2026
95c6798
Merge branch 'main' into fix/unstable-computehash
Weilong-Qin Jun 25, 2026
0ea3d44
fix(test): move OpenAIReasoningDetail hash test to extensions module
Weilong-Qin Jun 25, 2026
93cd966
style: apply spotless formatting
Weilong-Qin Jun 25, 2026
a4d90f0
fix(test): add missing ChatUsage import in OpenAIChatModelTest
Weilong-Qin Jun 25, 2026
a26d1e3
Merge branch 'main' into fix/unstable-computehash
Weilong-Qin Jun 26, 2026
913f598
Merge branch 'main' into fix/unstable-computehash
Weilong-Qin Jun 26, 2026
a050a84
Merge remote-tracking branch 'origin/main' into fix/unstable-computehash
Weilong-Qin Jun 26, 2026
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 @@ -122,13 +122,22 @@ public List<Content> convertMessages(List<Msg> msgs) {
// Build Part with FunctionCall and optional thought signature
Part.Builder partBuilder = Part.builder().functionCall(functionCall);

// Check for thought signature in metadata
// Check for thought signature in metadata (always stored as Base64 String,
// see ToolUseBlock#normalizeMetadata)
Map<String, Object> metadata = tub.getMetadata();
if (metadata != null
&& metadata.containsKey(ToolUseBlock.METADATA_THOUGHT_SIGNATURE)) {
Object signature = metadata.get(ToolUseBlock.METADATA_THOUGHT_SIGNATURE);
if (signature instanceof byte[]) {
partBuilder.thoughtSignature((byte[]) signature);
if (signature instanceof String encodedSignature
&& !encodedSignature.isEmpty()) {
try {
partBuilder.thoughtSignature(
Base64.getDecoder().decode(encodedSignature));
} catch (IllegalArgumentException e) {
log.warn(
"Invalid Base64 thought signature in ToolUseBlock metadata,"
+ " skipping");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -199,7 +200,9 @@ protected void parseToolCall(
Map<String, Object> metadata = null;
if (thoughtSignature != null) {
metadata = new HashMap<>();
metadata.put(ToolUseBlock.METADATA_THOUGHT_SIGNATURE, thoughtSignature);
metadata.put(
ToolUseBlock.METADATA_THOUGHT_SIGNATURE,
Base64.getEncoder().encodeToString(thoughtSignature));
}

blocks.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Objects;

/**
* OpenAI Reasoning Detail DTO (OpenRouter specific for Gemini).
Expand Down Expand Up @@ -90,4 +91,26 @@ public Integer getIndex() {
public void setIndex(Integer index) {
this.index = index;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof OpenAIReasoningDetail)) {
return false;
}
OpenAIReasoningDetail that = (OpenAIReasoningDetail) o;
return Objects.equals(this.id, that.id)
&& Objects.equals(this.type, that.type)
&& Objects.equals(this.data, that.data)
&& Objects.equals(this.text, that.text)
&& Objects.equals(this.format, that.format)
&& Objects.equals(this.index, that.index);
}

@Override
public int hashCode() {
return Objects.hash(this.id, this.type, this.data, this.text, this.format, this.index);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ public Source getSource() {
return source;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof AudioBlock)) {
return false;
}
AudioBlock that = (AudioBlock) o;
return Objects.equals(this.source, that.source);
}

@Override
public int hashCode() {
return Objects.hash(this.source);
}

/**
* Creates a new builder for constructing AudioBlock instances.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ public String getData() {
return data;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Base64Source)) {
return false;
}
Base64Source that = (Base64Source) o;
return Objects.equals(this.mediaType, that.mediaType)
&& Objects.equals(this.data, that.data);
}

@Override
public int hashCode() {
return Objects.hash(this.mediaType, this.data);
}

/**
* Creates a new builder for constructing Base64Source instances.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ public Integer getMaxPixels() {
return maxPixels;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ImageBlock)) {
return false;
}
ImageBlock that = (ImageBlock) o;
return Objects.equals(this.source, that.source)
&& Objects.equals(this.minPixels, that.minPixels)
&& Objects.equals(this.maxPixels, that.maxPixels);
}

@Override
public int hashCode() {
return Objects.hash(this.source, this.minPixels, this.maxPixels);
}

/**
* Creates a new builder for constructing ImageBlock instances.
*
Expand Down
23 changes: 23 additions & 0 deletions agentscope-core/src/main/java/io/agentscope/core/message/Msg.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,29 @@ private Msg(
this.timestamp = timestamp;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (!(o instanceof Msg)) {
return false;
} else {
Msg that = (Msg) o;
return Objects.equals(this.id, that.id)
&& Objects.equals(this.name, that.name)
&& this.role == that.role
&& Objects.equals(this.content, that.content)
&& Objects.equals(this.metadata, that.metadata)
&& Objects.equals(this.timestamp, that.timestamp);
}
}

@Override
public int hashCode() {
return Objects.hash(
this.id, this.name, this.role, this.content, this.metadata, this.timestamp);
}

/**
* Creates a new message builder with a randomly generated ID.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Objects;

/**
* Represents plain text content in a message.
Expand Down Expand Up @@ -56,6 +57,23 @@ public String toString() {
return text;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (!(o instanceof TextBlock)) {
return false;
} else {
TextBlock that = (TextBlock) o;
return Objects.equals(this.text, that.text);
}
}

@Override
public int hashCode() {
return Objects.hash(this.text);
}

/**
* Creates a new builder for constructing TextBlock instances.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* Represents reasoning or thinking content in a message.
Expand Down Expand Up @@ -81,6 +82,24 @@ public Map<String, Object> getMetadata() {
return metadata;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ThinkingBlock)) {
return false;
}
ThinkingBlock that = (ThinkingBlock) o;
return Objects.equals(this.thinking, that.thinking)
&& Objects.equals(this.metadata, that.metadata);
}

@Override
public int hashCode() {
return Objects.hash(this.thinking, this.metadata);
}

/**
* Creates a new builder for constructing ThinkingBlock instances.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.beans.Transient;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* Represents the result of a tool execution.
Expand Down Expand Up @@ -289,6 +290,26 @@ public ToolResultBlock withIdAndName(String id, String name) {
return new ToolResultBlock(id, name, this.output, this.metadata);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ToolResultBlock)) {
return false;
}
ToolResultBlock that = (ToolResultBlock) o;
return Objects.equals(this.id, that.id)
&& Objects.equals(this.name, that.name)
&& Objects.equals(this.output, that.output)
&& Objects.equals(this.metadata, that.metadata);
}

@Override
public int hashCode() {
return Objects.hash(this.id, this.name, this.output, this.metadata);
}

/**
* Creates a new builder for constructing ToolResultBlock instances.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* Represents a tool use request within a message.
Expand All @@ -33,7 +35,7 @@
*/
public final class ToolUseBlock extends ContentBlock {

/** Metadata key for Gemini thought signature (byte[] value). */
/** Metadata key for provider thought signatures (String value). */
public static final String METADATA_THOUGHT_SIGNATURE = "thoughtSignature";

private final String id;
Expand Down Expand Up @@ -93,7 +95,25 @@ public ToolUseBlock(
this.metadata =
metadata == null
? Collections.emptyMap()
: Collections.unmodifiableMap(new HashMap<>(metadata));
: Collections.unmodifiableMap(normalizeMetadata(metadata));
}

/**
* Normalizes metadata values to ensure stable {@link #equals(Object)} and {@link #hashCode()}
* across serialization round-trips.
*
* <p>Specifically, a {@code byte[]} stored under {@link #METADATA_THOUGHT_SIGNATURE} is
* converted to a Base64-encoded {@code String}, because {@code byte[]} relies on identity
* equality and would otherwise cause two semantically equal blocks to hash differently after
* deserialization.
*/
private static Map<String, Object> normalizeMetadata(Map<String, Object> metadata) {
Map<String, Object> copy = new HashMap<>(metadata);
Object signature = copy.get(METADATA_THOUGHT_SIGNATURE);
if (signature instanceof byte[] bytes) {
copy.put(METADATA_THOUGHT_SIGNATURE, Base64.getEncoder().encodeToString(bytes));
}
return copy;
}

/**
Expand Down Expand Up @@ -135,7 +155,7 @@ public String getContent() {
/**
* Gets the provider-specific metadata.
*
* <p>For Gemini, this may contain the thought signature under the key
* <p>For Gemini, this may contain a Base64-encoded thought signature under the key
* {@link #METADATA_THOUGHT_SIGNATURE}.
*
* @return The metadata map, or an empty map if not set
Expand All @@ -144,6 +164,27 @@ public Map<String, Object> getMetadata() {
return metadata;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ToolUseBlock)) {
return false;
}
ToolUseBlock that = (ToolUseBlock) o;
return Objects.equals(this.id, that.id)
&& Objects.equals(this.name, that.name)
&& Objects.equals(this.input, that.input)
&& Objects.equals(this.content, that.content)
&& Objects.equals(this.metadata, that.metadata);
}

@Override
public int hashCode() {
return Objects.hash(this.id, this.name, this.input, this.content, this.metadata);
}

/**
* Creates a new builder for constructing a ToolUseBlock.
*
Expand Down Expand Up @@ -211,7 +252,7 @@ public Builder content(String content) {
* Sets the provider-specific metadata.
*
* <p>For Gemini, use {@link ToolUseBlock#METADATA_THOUGHT_SIGNATURE} as the key
* to store thought signatures.
* to store Base64-encoded thought signatures.
*
* @param metadata The metadata map
* @return This builder for chaining
Expand Down
Loading
Loading