fix(session): preserve user turns in replay history
This commit is contained in:
@@ -48,6 +48,19 @@ def consolidator(store, mock_provider):
|
||||
)
|
||||
|
||||
|
||||
def _tool_round(call_id: str) -> list[dict]:
|
||||
return [
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": None,
|
||||
"tool_calls": [
|
||||
{"id": call_id, "type": "function", "function": {"name": "x", "arguments": "{}"}}
|
||||
],
|
||||
},
|
||||
{"role": "tool", "tool_call_id": call_id, "name": "x", "content": "ok"},
|
||||
]
|
||||
|
||||
|
||||
class TestConsolidatorSummarize:
|
||||
async def test_summarize_appends_to_history(self, consolidator, mock_provider, store):
|
||||
"""Consolidator should call LLM to summarize, then append to HISTORY.md."""
|
||||
@@ -219,21 +232,17 @@ class TestConsolidatorTokenBudget:
|
||||
assert session.metadata["_last_summary"]["text"] == "old conversation summary"
|
||||
consolidator.sessions.save.assert_called()
|
||||
|
||||
async def test_replay_window_overflow_matches_history_tool_boundary(
|
||||
async def test_replay_window_overflow_extends_to_long_recent_user_turn(
|
||||
self,
|
||||
consolidator,
|
||||
):
|
||||
"""Archive the exact prefix hidden by get_history's legal-start trimming."""
|
||||
"""Replay-window consolidation must not cut into the latest user turn."""
|
||||
session = Session(key="test:replay-tool-boundary")
|
||||
session.add_message("user", "run the tool")
|
||||
session.add_message(
|
||||
"assistant",
|
||||
"",
|
||||
tool_calls=[
|
||||
{"id": "call-1", "type": "function", "function": {"name": "x", "arguments": "{}"}}
|
||||
],
|
||||
)
|
||||
session.add_message("tool", "tool result", tool_call_id="call-1", name="x")
|
||||
session.add_message("user", "old")
|
||||
session.add_message("assistant", "old answer")
|
||||
session.add_message("user", "record this")
|
||||
for i in range(4):
|
||||
session.messages.extend(_tool_round(f"call-{i}"))
|
||||
session.add_message("assistant", "final answer")
|
||||
|
||||
consolidator.sessions._session_cache[session.key] = session
|
||||
@@ -242,13 +251,17 @@ class TestConsolidatorTokenBudget:
|
||||
|
||||
await consolidator.maybe_consolidate_by_tokens(
|
||||
session,
|
||||
replay_max_messages=2,
|
||||
replay_max_messages=4,
|
||||
)
|
||||
|
||||
archived_chunk = consolidator.archive.await_args.args[0]
|
||||
assert [m["role"] for m in archived_chunk] == ["user", "assistant", "tool"]
|
||||
assert session.last_consolidated == 3
|
||||
assert session.get_history(max_messages=2) == [{"role": "assistant", "content": "final answer"}]
|
||||
assert [m["content"] for m in archived_chunk] == ["old", "old answer"]
|
||||
assert session.last_consolidated == 2
|
||||
|
||||
history = session.get_history(max_messages=4, extend_to_user=True)
|
||||
assert len(history) > 4
|
||||
assert history[0]["content"] == "record this"
|
||||
assert history[-1]["content"] == "final answer"
|
||||
|
||||
async def test_large_chunk_archived_without_cap(self, consolidator):
|
||||
"""Without chunk cap, the full range from pick_consolidation_boundary is archived."""
|
||||
|
||||
@@ -111,6 +111,7 @@ class TestMaxMessagesIntegration:
|
||||
assert result is not None
|
||||
assert mock_hist.call_count == 1
|
||||
assert mock_hist.call_args.kwargs["max_messages"] == 25
|
||||
assert mock_hist.call_args.kwargs["extend_to_user"] is True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_zero_config_passes_builtin_limit_to_history_call(self, tmp_path: Path) -> None:
|
||||
@@ -129,6 +130,7 @@ class TestMaxMessagesIntegration:
|
||||
|
||||
assert result is not None
|
||||
assert mock_hist.call_args.kwargs["max_messages"] == DEFAULT_MAX_MESSAGES
|
||||
assert mock_hist.call_args.kwargs["extend_to_user"] is True
|
||||
|
||||
|
||||
class TestSchemaConfig:
|
||||
|
||||
@@ -641,6 +641,25 @@ def test_retain_recent_legal_suffix_can_extend_to_user_for_long_recent_turn():
|
||||
_assert_no_orphans(history)
|
||||
|
||||
|
||||
def test_get_history_can_extend_to_user_for_long_recent_turn():
|
||||
session = Session(key="test:history-extend-to-user")
|
||||
session.messages.append({"role": "user", "content": "old"})
|
||||
session.messages.append({"role": "assistant", "content": "old answer"})
|
||||
session.messages.append({"role": "user", "content": "record this"})
|
||||
for i in range(4):
|
||||
session.messages.extend(_tool_turn("recent", i))
|
||||
session.messages.append({"role": "assistant", "content": "done"})
|
||||
|
||||
hard_capped = session.get_history(max_messages=8)
|
||||
extended = session.get_history(max_messages=8, extend_to_user=True)
|
||||
|
||||
assert len(hard_capped) <= 8
|
||||
assert len(extended) > 8
|
||||
assert extended[0]["content"] == "record this"
|
||||
assert extended[-1]["content"] == "done"
|
||||
_assert_no_orphans(extended)
|
||||
|
||||
|
||||
# --- enforce_file_cap archive correctness (issue #4128) ---
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user