Skip to content

Commit 5bbcdce

Browse files
authored
fix: skip empty llm summaries (#8195)
1 parent dceacd5 commit 5bbcdce

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

astrbot/core/agent/context/compressor.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,15 @@ async def __call__(self, messages: list[Message]) -> list[Message]:
218218
# generate summary
219219
try:
220220
response = await self.provider.text_chat(contexts=llm_payload)
221-
summary_content = response.completion_text
221+
summary_content = (response.completion_text or "").strip()
222222
except Exception as e:
223223
logger.error(f"Failed to generate summary: {e}")
224224
return messages
225225

226+
if not summary_content:
227+
logger.warning("LLM context compression returned an empty summary.")
228+
return messages
229+
226230
# build result
227231
result = []
228232
result.extend(system_messages)

tests/agent/test_context_manager.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,25 @@ def test_init_with_truncate_compressor(self):
9494

9595
assert isinstance(manager.compressor, TruncateByTurnsCompressor)
9696

97+
@pytest.mark.asyncio
98+
async def test_llm_compressor_keeps_history_when_summary_is_empty(self):
99+
from astrbot.core.agent.context.compressor import LLMSummaryCompressor
100+
101+
provider = MockProvider()
102+
provider.text_chat = AsyncMock(
103+
return_value=LLMResponse(role="assistant", completion_text=" ")
104+
)
105+
compressor = LLMSummaryCompressor(provider=provider, keep_recent=2) # type: ignore[arg-type]
106+
messages = self.create_messages(6)
107+
108+
with patch("astrbot.core.agent.context.compressor.logger") as mock_logger:
109+
result = await compressor(messages)
110+
111+
assert result == messages
112+
mock_logger.warning.assert_called_once_with(
113+
"LLM context compression returned an empty summary."
114+
)
115+
97116
# ==================== Empty and Edge Cases ====================
98117

99118
@pytest.mark.asyncio

0 commit comments

Comments
 (0)