AgentScope-Java is an open-source project. To involve a broader community, we recommend asking your questions in English.
Is your feature request related to a problem? Please describe.
We encountered an infinite loop issue triggered by the AutoContext compression mechanism in AgentScope-Java, which is caused by improper compression of essential skill document context. The detailed trigger path is as follows:
-
The Agent loads SKILL.md and detects an instruction requiring reference document loading.
-
The Agent repeatedly calls theload_skill_through_path tool, generating a large number of consecutive tool_use and tool_result messages.
-
AutoContextHook triggers compressIfNeeded() before the next inference.
-
Strategy 1 (Tool Call Compression): Detects more than 4 consecutive tool messages and replaces all skill documents with LLM summaries.
-
Strategy 2/3 (Large Payload Offloading): Replaces skill documents exceeding the 5KB largePayloadThreshold with a 200-character preview.
-
Strategy 5 (Current Round Large Message Summary): Performs the same offloading for large messages in the current round.
-
The model loses detailed methodology content from the compressed skill documents and decides to reload the documents, leading to an infinite loop.
The core conflict: The compression strategy treats the tool results of load_skill_through_path (skill documents) the same as those of queryDashboard (chart data). However, skill documents are essential persistent context that must be retained, while chart data can be discarded after analysis.
Describe the solution you'd like
Implement a pinnedToolNames mechanism to protect critical tool messages from being compressed, thereby resolving the infinite loop. The specific implementation is as follows:
-
Add a new configuration item pinnedToolNames to mark protected tool names.
-
Modify relevant files to skip pinned tool messages in specified compression strategies, ensuring skill documents are not compressed.
Detailed modifications to files:
-
AutoContextConfig.java (agentscope-java)
Path: agentscope-extensions/agentscope-extensions-autocontext-memory/src/main/java/.../AutoContextConfig.java
-
Add new field Set pinnedToolNames (default empty set)
-
Add getter getPinnedToolNames()
-
Add pinnedToolNames(Set) method in Builder
-
MsgUtils.java (agentscope-java)
Path: agentscope-extensions/agentscope-extensions-autocontext-memory/src/main/java/.../MsgUtils.java
-
Add isPinnedToolMessage(Msg msg, Set pinnedToolNames) method
-
Check both ToolUseBlock (ASSISTANT-side tool_call) and ToolResultBlock (TOOL-side result)
-
Match whether the tool name is in the pinnedToolNames set
-
AutoContextMemory.java (agentscope-java)
Path: agentscope-extensions/agentscope-extensions-autocontext-memory/src/main/java/.../AutoContextMemory.java
- Strategy 1 (extractPrevToolMsgsForCompress): Treat pinned tool messages as non-tool messages in the consecutive tool message detection loop to break the consecutive chain, so skill loading tool messages are not included in the compressible range.
- Strategy 2/3 (offloadingLargePayload): Skip pinned tool messages when traversing messages to prevent large skill documents from being offloaded to 200-character previews.
- Strategy 5 (summaryCurrentRoundLargeMessages): Skip pinned tool messages when traversing large messages in the current round to prevent skill documents in the current round from being replaced by LLM summaries.
- Strategies 4, 6 (Historical Round Summary/Current Round Compression): No modifications. After a complete dialogue round (user→tools→assistant final response), summarizing the entire round including skill documents is safe because the analysis is completed.
-
Add configuration in createAutoContextMemory(): .pinnedToolNames(Set.of("load_skill_through_path"))
Describe alternatives you've considered
-
Disable partial compression strategies: Directly turning off Strategies 1, 2/3, and 5 can stop the infinite loop but will lose the compression capability for non-critical business tool messages, increasing context overhead and reducing model efficiency.
-
Hardcode skill tool protection: Embedding theload_skill_through_path tool name directly into the compression logic lacks flexibility and cannot adapt to other custom critical tools in different business scenarios.
-
Adjust compression thresholds: Increasing the number of consecutive tool messages or the large payload threshold can only delay the occurrence of the loop, not eliminate the root cause of missing critical skill content.
The pinnedToolNames mechanism is the optimal solution as it protects essential context while retaining normal compression for non-critical tools, balancing system stability and performance.
AgentScope-Java is an open-source project. To involve a broader community, we recommend asking your questions in English.
Is your feature request related to a problem? Please describe.
We encountered an infinite loop issue triggered by the AutoContext compression mechanism in AgentScope-Java, which is caused by improper compression of essential skill document context. The detailed trigger path is as follows:
The Agent loads SKILL.md and detects an instruction requiring reference document loading.
The Agent repeatedly calls theload_skill_through_path tool, generating a large number of consecutive tool_use and tool_result messages.
AutoContextHook triggers compressIfNeeded() before the next inference.
Strategy 1 (Tool Call Compression): Detects more than 4 consecutive tool messages and replaces all skill documents with LLM summaries.
Strategy 2/3 (Large Payload Offloading): Replaces skill documents exceeding the 5KB largePayloadThreshold with a 200-character preview.
Strategy 5 (Current Round Large Message Summary): Performs the same offloading for large messages in the current round.
The model loses detailed methodology content from the compressed skill documents and decides to reload the documents, leading to an infinite loop.
The core conflict: The compression strategy treats the tool results of load_skill_through_path (skill documents) the same as those of queryDashboard (chart data). However, skill documents are essential persistent context that must be retained, while chart data can be discarded after analysis.
Describe the solution you'd like
Implement a pinnedToolNames mechanism to protect critical tool messages from being compressed, thereby resolving the infinite loop. The specific implementation is as follows:
Add a new configuration item pinnedToolNames to mark protected tool names.
Modify relevant files to skip pinned tool messages in specified compression strategies, ensuring skill documents are not compressed.
Detailed modifications to files:
AutoContextConfig.java (agentscope-java)
Path: agentscope-extensions/agentscope-extensions-autocontext-memory/src/main/java/.../AutoContextConfig.java
Add new field Set pinnedToolNames (default empty set)
Add getter getPinnedToolNames()
Add pinnedToolNames(Set) method in Builder
MsgUtils.java (agentscope-java)
Path: agentscope-extensions/agentscope-extensions-autocontext-memory/src/main/java/.../MsgUtils.java
Add isPinnedToolMessage(Msg msg, Set pinnedToolNames) method
Check both ToolUseBlock (ASSISTANT-side tool_call) and ToolResultBlock (TOOL-side result)
Match whether the tool name is in the pinnedToolNames set
AutoContextMemory.java (agentscope-java)
Path: agentscope-extensions/agentscope-extensions-autocontext-memory/src/main/java/.../AutoContextMemory.java
Add configuration in createAutoContextMemory(): .pinnedToolNames(Set.of("load_skill_through_path"))
Describe alternatives you've considered
Disable partial compression strategies: Directly turning off Strategies 1, 2/3, and 5 can stop the infinite loop but will lose the compression capability for non-critical business tool messages, increasing context overhead and reducing model efficiency.
Hardcode skill tool protection: Embedding theload_skill_through_path tool name directly into the compression logic lacks flexibility and cannot adapt to other custom critical tools in different business scenarios.
Adjust compression thresholds: Increasing the number of consecutive tool messages or the large payload threshold can only delay the occurrence of the loop, not eliminate the root cause of missing critical skill content.
The pinnedToolNames mechanism is the optimal solution as it protects essential context while retaining normal compression for non-critical tools, balancing system stability and performance.