agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Bug]:AutoContextMemory Strategy 5 sends invalid tool messages to the summarization model

Open liuwencan opened this issue 5 days ago • 2 comments

AgentScope-Java is an open-source project. To involve a broader community, we recommend asking your questions in English.

Describe the bug AutoContextMemory Strategy 5 may send an invalid tool message sequence to the summary model when summarizing a large message.

The problem occurs in:

io.agentscope.core.memory.autocontext.AutoContextMemory#callModelForSummary
When the message contains ToolUseBlock or ToolResultBlock, it may be sent to the summary model with its original ASSISTANT or TOOL role, even though the corresponding tool-call/result message is not included in the summary request.

DeepSeek rejects the request with HTTP 400, and the exception terminates the agent execution.

To Reproduce

  1. Configure AgentScope-Java with AutoContext and the official DeepSeek API.
  2. Enable tools that can return a large result, such as context_reload or load_skill_through_path.
  3. Run an agent until AutoContext Strategy 5 is triggered.
  4. Let the agent summarize a large tool-related message.
  5. Observe the request failure.

Example log:

Compression triggered - msgCount: 64/50
Strategy 5: Checking for current round large messages to summarize
Offloaded current round large message: index=63, size=12280 chars

The original conversation may contain a valid pair:

assistant: tool call, id=call_123
tool: result for call_123

However, callModelForSummary() creates a separate summary request containing only one of these messages, for example:

user: summarize the following message
tool: result for call_123
user: end of messages

or

user: summarize the following message
assistant: tool_calls=[call_123]
user: end of messages

Expected behavior The summary model should receive tool-related content as ordinary text, or receive a complete valid tool-call/result sequence.

The summary request should never contain:

  • an isolated TOOL message without a preceding assistant tool_calls;
  • an assistant tool_calls message without matching tool results. If summary generation fails, AutoContext should ideally skip this compression step or fall back to the original message instead of terminating the entire agent execution.

Error messages An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. (insufficient tool messages following tool_calls message)

Another occurrence:

Messages with role 'tool' must be a response to a preceding message with 'tool_calls'

Relevant stack trace::

at io.agentscope.core.memory.autocontext.AutoContextMemory.callModelForSummary(AutoContextMemory.java:...)
at io.agentscope.core.memory.autocontext.AutoContextMemory.generateLargeMessageSummary(AutoContextMemory.java:...)
at io.agentscope.core.memory.autocontext.AutoContextMemory.summaryCurrentRoundLargeMessages(AutoContextMemory.java:...)
at io.agentscope.core.memory.autocontext.AutoContextMemory.compressIfNeeded(AutoContextMemory.java:...)

Environment (please complete the following information):

AgentScope-Java Version: 1.0.12 Java Version: 21 OS: Windows Model: DeepSeek official API API endpoint: https://api.deepseek.com/v1/chat/completions AutoContext strategy: Strategy 5

Additional context The issue is intermittent because it depends on which large message is selected for compression. For example:

A large ordinary text message can be summarized successfully.
A large TOOL result may fail because it is sent as an isolated TOOL message.
An assistant message containing only ToolUseBlock may fail because it is sent without matching tool results.

As a local workaround, I changed callModelForSummary() so that messages containing ToolUseBlock or ToolResultBlock are converted to ordinary USER text messages in the summary request:

if (hasToolUse || hasToolResult) {
    String text =
            textForCompression == null || textForCompression.isBlank()
                    ? "[Tool message contains no plain text output]"
                    : textForCompression;

    messageForCompression =
            Msg.builder()
                    .role(MsgRole.USER)
                    .name("user")
                    .content(TextBlock.builder().text(text).build())
                    .build();
}

The original tool message structure is still preserved when constructing the compressed message returned to the conversation history.

After this change, the DeepSeek summary request succeeds and the agent continues execution.

The same scenario did not fail with Qwen, which may indicate that DeepSeek performs stricter validation of OpenAI-compatible tool message sequences. However, the summary request should still be valid for all OpenAI-compatible providers.

liuwencan avatar Nov 30 '26 05:11 liuwencan

This appears to be the same legacy summary-request boundary reported in #3518. That issue was closed after the discussion noted that this component is no longer in 2.x; closing it did not establish a fix for applications still using 1.x.

I checked the current 1.x branch at 9f47564c8b6a16eccddf198a5f3c0b06b5b2c7be. In AutoContextMemory.callModelForSummary, extracted text keeps message.getRole(), and an empty extraction leaves the original message in the separate summary request. That means changing the role only in the non-empty-text branch would leave a tool-only/empty-output case uncovered.

Would a narrowly scoped 1.x maintenance fix be accepted? If so, and if nobody is already handling it, I can take the summary-input normalization with offline tests for non-empty and empty tool results, tool-use-only messages, ordinary text controls, and preservation of the original conversation messages. The tests should inspect the messages passed to the summary model rather than rely on one provider accepting an invalid sequence.

I would keep the separate “continue after any summarization failure” behavior out of that first patch until its failure policy is agreed. In particular, interruption/cancellation should not be swallowed as a successful compression. This is a source comparison only; I have not run a 1.x reproduction or changed the implementation.

CryoThrust avatar Nov 30 '26 05:11 CryoThrust

2.x 版本已移除 AutoContextMemory。建议在本地改改,就算我们在 1.x 分支修复了,后续 1.x 也不会发新版了

jujn avatar Nov 30 '26 07:11 jujn