fix(session): archive actual idle compact drops

This commit is contained in:
chengyongru 2026-06-01 13:44:17 +08:00 committed by Xubin Ren
parent baffd6ef92
commit 0e37024114
4 changed files with 44 additions and 8 deletions

View File

@ -807,10 +807,9 @@ class Consolidator:
metadata={},
last_consolidated=0,
)
probe.retain_recent_legal_suffix(max_suffix)
dropped, already_consolidated = probe.retain_recent_legal_suffix(max_suffix)
kept = probe.messages
cut = len(tail) - len(kept)
archive_msgs = tail[:cut]
archive_msgs = dropped[already_consolidated:]
if not archive_msgs and not kept:
session.updated_at = datetime.now()

View File

@ -76,10 +76,9 @@ def _make_fake_compact(
metadata={},
last_consolidated=0,
)
probe.retain_recent_legal_suffix(max_suffix)
dropped, already_consolidated = probe.retain_recent_legal_suffix(max_suffix)
kept = probe.messages
cut = len(tail) - len(kept)
archive_msgs = tail[:cut]
archive_msgs = dropped[already_consolidated:]
if not archive_msgs and not kept:
session.updated_at = datetime.now()

View File

@ -440,6 +440,44 @@ class TestCompactIdleSession:
assert "u0" not in user_content
assert "u25" in user_content or "a25" in user_content
@pytest.mark.asyncio
async def test_non_contiguous_suffix_archives_actual_dropped_messages(
self,
real_consolidator,
mock_provider,
):
"""Assistant-only tails retain a non-contiguous slice, so archive the
actual dropped messages rather than a computed prefix."""
mock_provider.chat_with_retry.return_value = MagicMock(
content="Tail summary.", finish_reason="stop"
)
sessions = real_consolidator.sessions
session = sessions.get_or_create("cli:noncontiguous")
for i in range(15):
session.add_message("user", f"user-{i:02d}")
for i in range(10):
session.add_message("assistant", f"assistant-{i:02d}")
sessions.save(session)
result = await real_consolidator.compact_idle_session("cli:noncontiguous", max_suffix=6)
assert result == "Tail summary."
reloaded = sessions.get_or_create("cli:noncontiguous")
assert [m["content"] for m in reloaded.messages] == [
"user-14",
"assistant-00",
"assistant-01",
"assistant-02",
"assistant-03",
"assistant-04",
]
archived_call = mock_provider.chat_with_retry.call_args
user_content = archived_call.kwargs["messages"][1]["content"]
assert "user-14" not in user_content
assert "assistant-00" not in user_content
assert "assistant-09" in user_content
@pytest.mark.asyncio
async def test_acquires_consolidation_lock(self, real_consolidator, mock_provider):
"""Verify lock is held during execution."""

View File

@ -691,6 +691,6 @@ def test_retain_recent_legal_suffix_last_consolidated_correct_in_else_branch():
# Retained messages start from latest user (u9) + max_messages forward
# so retained = [u9, a0..a9][:4] → but these are from original indices 9..12
# Of those, indices 9,10,11 are < 12 (before_lc), so new_lc = 3
assert session.last_consolidated <= 12
assert session.last_consolidated == 3
# already_cons should count dropped messages with original index < 12
assert already_cons >= 0
assert already_cons == 9