mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-06-15 23:34:00 +00:00
fix(session): archive actual idle compact drops
This commit is contained in:
parent
baffd6ef92
commit
0e37024114
@ -807,10 +807,9 @@ class Consolidator:
|
|||||||
metadata={},
|
metadata={},
|
||||||
last_consolidated=0,
|
last_consolidated=0,
|
||||||
)
|
)
|
||||||
probe.retain_recent_legal_suffix(max_suffix)
|
dropped, already_consolidated = probe.retain_recent_legal_suffix(max_suffix)
|
||||||
kept = probe.messages
|
kept = probe.messages
|
||||||
cut = len(tail) - len(kept)
|
archive_msgs = dropped[already_consolidated:]
|
||||||
archive_msgs = tail[:cut]
|
|
||||||
|
|
||||||
if not archive_msgs and not kept:
|
if not archive_msgs and not kept:
|
||||||
session.updated_at = datetime.now()
|
session.updated_at = datetime.now()
|
||||||
|
|||||||
@ -76,10 +76,9 @@ def _make_fake_compact(
|
|||||||
metadata={},
|
metadata={},
|
||||||
last_consolidated=0,
|
last_consolidated=0,
|
||||||
)
|
)
|
||||||
probe.retain_recent_legal_suffix(max_suffix)
|
dropped, already_consolidated = probe.retain_recent_legal_suffix(max_suffix)
|
||||||
kept = probe.messages
|
kept = probe.messages
|
||||||
cut = len(tail) - len(kept)
|
archive_msgs = dropped[already_consolidated:]
|
||||||
archive_msgs = tail[:cut]
|
|
||||||
|
|
||||||
if not archive_msgs and not kept:
|
if not archive_msgs and not kept:
|
||||||
session.updated_at = datetime.now()
|
session.updated_at = datetime.now()
|
||||||
|
|||||||
@ -440,6 +440,44 @@ class TestCompactIdleSession:
|
|||||||
assert "u0" not in user_content
|
assert "u0" not in user_content
|
||||||
assert "u25" in user_content or "a25" 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
|
@pytest.mark.asyncio
|
||||||
async def test_acquires_consolidation_lock(self, real_consolidator, mock_provider):
|
async def test_acquires_consolidation_lock(self, real_consolidator, mock_provider):
|
||||||
"""Verify lock is held during execution."""
|
"""Verify lock is held during execution."""
|
||||||
|
|||||||
@ -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
|
# Retained messages start from latest user (u9) + max_messages forward
|
||||||
# so retained = [u9, a0..a9][:4] → but these are from original indices 9..12
|
# 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
|
# 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
|
# already_cons should count dropped messages with original index < 12
|
||||||
assert already_cons >= 0
|
assert already_cons == 9
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user