The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(compression): split snapshot-check-raises case into its own test
kshitijk4pooref491c34b325
diff --git a/tests/tui_gateway/test_manual_compress_history_fence.py b/tests/tui_gateway/test_manual_compress_history_fence.pyindex 93dc1010296..3a5bcaca826 100644--- a/tests/tui_gateway/test_manual_compress_history_fence.py+++ b/tests/tui_gateway/test_manual_compress_history_fence.py@@ -6,19 +6,11 @@ from unittest.mock import MagicMockimport pytest[email protected]("in_place", [False, True])[email protected]("compress_args,stale", [- ("", False), ("", True), ("", "validation_error"), ("here 2", True),-])-def test_manual_compress_rejects_history_rewritten_before_admission(- tmp_path, monkeypatch, caplog, stale, in_place, compress_args,-):+def _agent_with_history(tmp_path, monkeypatch, in_place=False):from hermes_state import SessionDBfrom run_agent import AIAgentfrom agent.context_compressor import SUMMARY_PREFIX- from tui_gateway import server- monkeypatch.setattr(server, "_get_usage", lambda agent: {})db = SessionDB(db_path=tmp_path / "state.db")parent = "manual-fence-parent"db.create_session(parent, source="cli")@@ -42,9 +34,21 @@ def test_manual_compress_rejects_history_rewritten_before_admission(agent.compression_in_place = in_placeagent._compression_feasibility_checked = Truemonkeypatch.setattr(agent, "_build_system_prompt", lambda *args, **kwargs: "fixture prompt")+ return db, agent, parent, before++[email protected]("in_place", [False, True])[email protected]("compress_args,stale", [("", False), ("", True), ("here 2", True)])+def test_manual_compress_rejects_history_rewritten_before_admission(+ tmp_path, monkeypatch, caplog, stale, in_place, compress_args,+):+ from tui_gateway import server++ monkeypatch.setattr(server, "_get_usage", lambda agent: {})+ db, agent, parent, before = _agent_with_history(tmp_path, monkeypatch, in_place)session = {"agent": agent, "history_lock": threading.Lock(), "history": list(before),"history_version": 1, "running": False, "session_key": parent, "cwd": str(tmp_path)}- if stale is True:+ if stale:# A real guarded edit wins after the host snapshot but before the durable lease.replacement = [{"role": "user", "content": "edited question"},{"role": "assistant", "content": "edited answer"}]@@ -59,25 +63,12 @@ def test_manual_compress_rejects_history_rewritten_before_admission(monkeypatch.setattr(db, "try_acquire_compression_lock", edit_then_acquire)try:- if stale == "validation_error":- from agent.conversation_compression_manual import compress_now, parse_compress_args-- def fail_validation():- assert db.get_compression_lock_holder(parent) is not None- raise RuntimeError("validation failed")-- with pytest.raises(RuntimeError, match="validation failed"):- compress_now(agent, before, parse_compress_args(compress_args), snapshot_is_current=fail_validation)- assert db.get_compression_lock_holder(parent) is None- assert agent.session_id == parent- assert db.get_messages_as_conversation(parent) == before- returnwith caplog.at_level(logging.INFO, logger="agent.conversation_compression"):removed, _ = server._compress_session_history(session, focus_topic=compress_args, before_messages=before, history_version=1,)assert db.get_compression_lock_holder(parent) is None- if stale is True:+ if stale:assert removed == 0assert agent.session_id == parent, "stale compression rotated the live agent"assert db.find_live_compression_child(parent) is None@@ -93,3 +84,23 @@ def test_manual_compress_rejects_history_rewritten_before_admission(finally:agent.close()db.close()+++def test_manual_compress_releases_lease_when_snapshot_check_raises(tmp_path, monkeypatch):+ from agent.conversation_compression_manual import compress_now, parse_compress_args++ db, agent, parent, before = _agent_with_history(tmp_path, monkeypatch)++ def fail_validation():+ assert db.get_compression_lock_holder(parent) is not None+ raise RuntimeError("validation failed")++ try:+ with pytest.raises(RuntimeError, match="validation failed"):+ compress_now(agent, before, parse_compress_args(""), snapshot_is_current=fail_validation)+ assert db.get_compression_lock_holder(parent) is None+ assert agent.session_id == parent+ assert db.get_messages_as_conversation(parent) == before+ finally:+ agent.close()+ db.close()