fix(compress): improve context compression, improve kv-cache rate of context compression, handle compression model modalities#8530
Conversation
There was a problem hiding this comment.
Hey - I've found 2 issues, and left some high level feedback:
- Consider keeping a default value for the
tagparameter in_simple_print_message_role(or adding an overload) to avoid breaking any existing call sites that may still be calling it with no arguments. - In
_message_to_dict, you now special-caselistformsg.content; if other iterable container types (e.g., tuples) are possible here, it may be safer to normalize or handle them as well to avoid inconsistent serialization behavior.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider keeping a default value for the `tag` parameter in `_simple_print_message_role` (or adding an overload) to avoid breaking any existing call sites that may still be calling it with no arguments.
- In `_message_to_dict`, you now special-case `list` for `msg.content`; if other iterable container types (e.g., tuples) are possible here, it may be safer to normalize or handle them as well to avoid inconsistent serialization behavior.
## Individual Comments
### Comment 1
<location path="tests/unit/test_astr_main_agent.py" line_range="1000-1009" />
<code_context>
+ conv_mgr = mock_context.conversation_manager
+ _setup_conversation_for_build(conv_mgr)
+
+ with (
+ patch("astrbot.core.astr_main_agent.AgentRunner") as mock_runner_cls,
+ patch("astrbot.core.astr_main_agent.AstrAgentContext"),
+ ):
+ mock_runner = MagicMock()
+ mock_runner.reset = AsyncMock()
+ mock_runner_cls.return_value = mock_runner
+
+ result = await module.build_main_agent(
+ event=mock_event,
+ plugin_context=mock_context,
+ config=module.MainAgentBuildConfig(
+ tool_call_timeout=60,
+ max_context_length=7,
+ ),
+ )
+
+ assert result is not None
+ assert mock_runner.reset.await_args.kwargs["enforce_max_turns"] == 7
+
@pytest.mark.asyncio
</code_context>
<issue_to_address>
**suggestion (testing):** Strengthen the assertion around AgentRunner.reset by checking call count and guarding await_args usage
The test currently inspects `mock_runner.reset.await_args.kwargs["enforce_max_turns"]` directly. To avoid opaque failures if `reset` stops being awaited, also assert that `reset` was awaited exactly once (e.g. `mock_runner.reset.assert_awaited_once()`), and then either use `await_args.kwargs.get("enforce_max_turns")` or assert the full `kwargs` dict. This makes the expectation on the await pattern explicit and the failure mode clearer.
</issue_to_address>
### Comment 2
<location path="tests/agent/test_context_manager.py" line_range="129-135" />
<code_context>
)
+ @pytest.mark.asyncio
+ async def test_llm_compressor_handles_textpart_content(self):
+ from astrbot.core.agent.context.compressor import LLMSummaryCompressor
+
+ provider = MockProvider()
+ compressor = LLMSummaryCompressor(provider=provider, keep_recent=1) # type: ignore[arg-type]
+ messages = [
+ Message(role="user", content=[TextPart(text="Hello")]),
+ Message(role="assistant", content=[TextPart(text="Hi there")]),
+ Message(role="user", content=[TextPart(text="Summarize our work")]),
+ Message(role="assistant", content=[TextPart(text="Sure")]),
+ ]
+
+ result = await compressor(messages)
+
+ assert len(result) == 4
+ assert result[0].role == "user"
+ assert isinstance(result[0].content, str)
+ assert "previous history conversation summary" in result[0].content
+ assert result[-1].content == [TextPart(text="Sure")]
+
</code_context>
<issue_to_address>
**suggestion (testing):** Relax the assertion on the exact summary text to avoid brittle coupling to prompt wording
The current check for `"previous history conversation summary" in result[0].content` makes the test fragile to harmless copy changes in the prompt. Instead, consider asserting on more stable properties (e.g., that `result[0].content` is a non-empty string, that the mock provider was called, or that the content includes the relevant user texts) rather than this exact phrase.
```suggestion
result = await compressor(messages)
assert len(result) == 4
assert result[0].role == "user"
assert isinstance(result[0].content, str)
# Ensure we got a non-empty summary-like string without coupling to exact prompt wording
assert result[0].content.strip()
# The summary should reflect earlier conversation content
assert "Hello" in result[0].content
assert result[-1].content == [TextPart(text="Sure")]
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Code Review
This pull request updates message serialization in _message_to_dict to support lists of ContentPart objects, refactors message role logging to summarize long message histories, and ensures enforce_max_turns is passed during agent building, with accompanying unit tests. The reviewer suggests also serializing ToolCall objects in msg.tool_calls using model_dump() to prevent potential TypeError exceptions during LLM summary compression.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
…s for llm compress, fix AftCompact debug log Three context-compaction regression fixes after AstrBotDevs#8226: 1. Restore max_context_length -> enforce_max_turns propagation so normal turn-based truncation works again. 2. Serialize ContentPart and ToolCall objects into plain dicts in _message_to_dict so llm_compress no longer fails with JSON serialization errors. 3. Print _provider_messages (compacted) instead of run_context.messages (unchanged) in AftCompact debug log; truncate long role lists to first4,...,last4 to avoid log spam. Assertions in tests are also hardened to avoid coupling to exact prompt wording.
d5167f3 to
d931b9f
Compare
…dundant provider messages
Fixes #8484
Fixes #8498
This PR fixes three context-compaction regressions introduced after the context compaction refactor in #8226.
Modifications / 改动点
Screenshots or Test Results / 运行截图或测试结果
Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。