fix: resolve three bugs in core agent and tool management (#1164 #1165 #1166)#1167
Merged
Merged
Conversation
- StaticLongTermMemoryHook: inject retrieved memory at the beginning of the message list instead of appending it at the end, so the LLM receives context before the user query (fixes agentscope-ai#1164) - ToolGroupManager: replace non-thread-safe ArrayList with CopyOnWriteArrayList for activeGroups, and update setActiveGroups() and copyTo() to mutate the list in-place rather than reassigning the field, preventing race conditions in concurrent agent scenarios (fixes agentscope-ai#1165) - MessageUtils.extractRecentToolCalls: replace msg.getName().equals() with Objects.equals() to avoid NullPointerException when an assistant message has no name set (fixes agentscope-ai#1166)
The test was asserting memory message at index 1 (end), which matched the old buggy behavior. Update to assert index 0 (beginning) to align with the fix in agentscope-ai#1164.
4fcddee to
5317598
Compare
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
Author
|
三个问题:空指针,并发安全,记忆反序 |
Contributor
Author
|
给孩子过了吧 呜呜呜 |
Collaborator
|
lichuang34 这个账号没有签署 CLA,要不用Fruank4这个账号提交下后两个commit |
Contributor
Author
|
CLA验证了 |
LearningGp
requested changes
Apr 24, 2026
…ring - ToolGroupManager: replace clear()+addAll() with direct reference swap via volatile field to avoid transient empty list visible to readers - StaticLongTermMemoryHook: change long-term memory msg role from SYSTEM to USER and inject at end of message list instead of beginning
6ba4830 to
9d0d8fe
Compare
…n behavior Memory message is now USER role and appended at end of message list.
liangxingguang
pushed a commit
to liangxingguang/agentscope-java
that referenced
this pull request
May 21, 2026
…-ai#1164 agentscope-ai#1165 agentscope-ai#1166) (agentscope-ai#1167) ## Summary This PR fixes three bugs identified in the core library: - **agentscope-ai#1164** \`StaticLongTermMemoryHook\` injects retrieved memory at the beginning of the message list with a \`SYSTEM\` role, which breaks message sequence constraints and degrades RAG quality - **agentscope-ai#1165** \`ToolGroupManager.activeGroups\` uses a non-thread-safe \`ArrayList\`, causing race conditions in concurrent agent scenarios - **agentscope-ai#1166** \`MessageUtils.extractRecentToolCalls\` calls \`.equals()\` on \`msg.getName()\` without a null check, throwing \`NullPointerException\` when an assistant message has no name ## Changes ### Fix agentscope-ai#1164 — \`StaticLongTermMemoryHook.java\` Change memory message role from \`SYSTEM\` to \`USER\` and append it at the end of the message list so the sequence remains valid and the memory context is closest to the model's generation point: \`\`\`java // Before Msg memoryMsg = Msg.builder().role(MsgRole.SYSTEM)...build(); List<Msg> enhancedMessages = new ArrayList<>(); enhancedMessages.addAll(inputMessages); enhancedMessages.add(memoryMsg); // appended at end but wrong role // After Msg memoryMsg = Msg.builder().role(MsgRole.USER)...build(); List<Msg> enhancedMessages = new ArrayList<>(inputMessages); enhancedMessages.add(memoryMsg); // USER role, appended at end \`\`\` ### Fix agentscope-ai#1165 — \`ToolGroupManager.java\` Replace the non-thread-safe \`ArrayList\` with \`CopyOnWriteArrayList\` (declared \`volatile\`) and update \`setActiveGroups()\` / \`copyTo()\` to replace the reference atomically, avoiding the transient empty-list window that \`clear()\` + \`addAll()\` would create: \`\`\`java // Before private List<String> activeGroups = new ArrayList<>(); // ... this.activeGroups = new ArrayList<>(activeGroups); target.activeGroups = new ArrayList<>(this.activeGroups); // After private volatile List<String> activeGroups = new CopyOnWriteArrayList<>(); // ... this.activeGroups = new CopyOnWriteArrayList<>(activeGroups); target.activeGroups = new CopyOnWriteArrayList<>(this.activeGroups); \`\`\` ### Fix agentscope-ai#1166 — \`MessageUtils.java\` Replace \`msg.getName().equals(agentName)\` with \`Objects.equals()\` to safely handle \`null\` names: \`\`\`java // Before if (msg.getRole() == MsgRole.ASSISTANT && msg.getName().equals(agentName)) { // After if (msg.getRole() == MsgRole.ASSISTANT && Objects.equals(msg.getName(), agentName)) { \`\`\` ## Test plan - [x] Existing unit tests pass (\`mvn test -pl agentscope-core\`) - [x] \`StaticLongTermMemoryHookTest\` updated to assert \`USER\` role and end-of-list position - [ ] Verify no \`ConcurrentModificationException\` when multiple agents modify tool groups concurrently - [ ] Verify no \`NullPointerException\` when assistant messages without names are present in memory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes three bugs identified in the core library:
Changes
Fix #1164 — `StaticLongTermMemoryHook.java`
Change memory message role from `SYSTEM` to `USER` and append it at the end of the message list so the sequence remains valid and the memory context is closest to the model's generation point:
```java
// Before
Msg memoryMsg = Msg.builder().role(MsgRole.SYSTEM)...build();
List enhancedMessages = new ArrayList<>();
enhancedMessages.addAll(inputMessages);
enhancedMessages.add(memoryMsg); // appended at end but wrong role
// After
Msg memoryMsg = Msg.builder().role(MsgRole.USER)...build();
List enhancedMessages = new ArrayList<>(inputMessages);
enhancedMessages.add(memoryMsg); // USER role, appended at end
```
Fix #1165 — `ToolGroupManager.java`
Replace the non-thread-safe `ArrayList` with `CopyOnWriteArrayList` (declared `volatile`) and update `setActiveGroups()` / `copyTo()` to replace the reference atomically, avoiding the transient empty-list window that `clear()` + `addAll()` would create:
```java
// Before
private List activeGroups = new ArrayList<>();
// ...
this.activeGroups = new ArrayList<>(activeGroups);
target.activeGroups = new ArrayList<>(this.activeGroups);
// After
private volatile List activeGroups = new CopyOnWriteArrayList<>();
// ...
this.activeGroups = new CopyOnWriteArrayList<>(activeGroups);
target.activeGroups = new CopyOnWriteArrayList<>(this.activeGroups);
```
Fix #1166 — `MessageUtils.java`
Replace `msg.getName().equals(agentName)` with `Objects.equals()` to safely handle `null` names:
```java
// Before
if (msg.getRole() == MsgRole.ASSISTANT && msg.getName().equals(agentName)) {
// After
if (msg.getRole() == MsgRole.ASSISTANT && Objects.equals(msg.getName(), agentName)) {
```
Test plan