Skip to content

fix(compress): improve context compression, improve kv-cache rate of context compression, handle compression model modalities#8530

Merged
Soulter merged 7 commits into
AstrBotDevs:masterfrom
Foolllll-J:fix/context-compaction-regressions
Jun 3, 2026
Merged

fix(compress): improve context compression, improve kv-cache rate of context compression, handle compression model modalities#8530
Soulter merged 7 commits into
AstrBotDevs:masterfrom
Foolllll-J:fix/context-compaction-regressions

Conversation

@Foolllll-J
Copy link
Copy Markdown
Contributor

@Foolllll-J Foolllll-J commented Jun 2, 2026

Fixes #8484
Fixes #8498

This PR fixes three context-compaction regressions introduced after the context compaction refactor in #8226.

  1. Restore max_context_length propagation from build_main_agent() to ToolLoopAgentRunner as enforce_max_turns, so the normal turn-based truncation path works again.
  2. Serialize ContentPart and ToolCall instances into plain dicts before round-based LLM summary compression, so llm_compress no longer fails with TypeError: Object of type TextPart/ToolCall is not JSON serializable.
  3. Fix [AftCompact] debug log to print the actual compacted messages (_provider_messages) instead of the unchanged run_context.messages, and truncate long role lists to first4,...,last4 to avoid log spam.

Modifications / 改动点

  • Updated astrbot/core/astr_main_agent.py
    • Restore enforce_max_turns=config.max_context_length when resetting the main agent runner.
  • Updated astrbot/core/agent/context/compressor.py
    • Convert ContentPart items in Message.content to serializable dicts before round splitting / summary rendering.
    • Convert ToolCall items in Message.tool_calls to serializable dicts via model_dump() to prevent JSON serialization errors.
  • Updated astrbot/core/agent/runners/tool_loop_agent_runner.py
    • Make _simple_print_message_role accept a messages parameter so [AftCompact] can print the actual compacted result.
    • Pass self._provider_messages to [AftCompact] instead of self.run_context.messages.
    • Truncate long role lists to first4,last4 for readability.
  • Updated tests
    • Added a regression test in tests/unit/test_astr_main_agent.py to verify max_context_length is passed through to the runner, with assert_awaited_once() added for clearer failure messages.
    • Added a regression test in tests/agent/test_context_manager.py to verify LLMSummaryCompressor can handle TextPart content without raising serialization errors; assertions hardened to avoid coupling to exact prompt wording.
  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

[2026-06-03 02:54:25.981] [Core] [DBUG] [runners.tool_loop_agent_runner:620]: [BefCompact] messages -> [50] system,user,assistant,user,...,assistant,user,assistant,user
[2026-06-03 02:54:25.981] [Core] [DBUG] [runners.tool_loop_agent_runner:620]: [AftCompact] messages -> [6] system,user,assistant,user,assistant,user

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.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. labels Jun 2, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 2 issues, and left some high level feedback:

  • 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.
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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread tests/unit/test_astr_main_agent.py
Comment thread tests/agent/test_context_manager.py
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread astrbot/core/agent/context/compressor.py Outdated
…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.
@Foolllll-J Foolllll-J force-pushed the fix/context-compaction-regressions branch from d5167f3 to d931b9f Compare June 2, 2026 19:18
@Foolllll-J Foolllll-J changed the title Fix/context compaction regressions fix(context): fix three context-compaction regressions after #8226 Jun 2, 2026
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Jun 3, 2026
@Soulter Soulter changed the title fix(context): fix three context-compaction regressions after #8226 fix(compress): improve context compression, improve kv-cache rate of context compression Jun 3, 2026
@Soulter Soulter requested a review from RC-CHN June 3, 2026 06:05
@Soulter Soulter changed the title fix(compress): improve context compression, improve kv-cache rate of context compression fix(compress): improve context compression, improve kv-cache rate of context compression, handle compression model modalities Jun 3, 2026
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jun 3, 2026
@Soulter Soulter merged commit 1daa0e3 into AstrBotDevs:master Jun 3, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] V4.25.2版本 普通配置项上下文截断失效 [Bug] LLM压缩上下文进行压缩时报错。

3 participants