The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(compression): drop the parent prompt cache when adopting a live child
Uttkarsh Tiwari21b86ab13e1b
diff --git a/agent/conversation_compression.py b/agent/conversation_compression.pyindex 9d44b8a1935..a7b6d08db9d 100644--- a/agent/conversation_compression.py+++ b/agent/conversation_compression.py@@ -1692,6 +1692,11 @@ def _adopt_live_compression_child(agent.session_id = child_session_id_rebind_session_context(child_session_id)agent._session_db_created = True+ # The agent now owns the child session, so any prompt the parent cached belongs to a+ # different session row: drop it before the seed below. The turn gates on this slot+ # (``turn_context``: restore/rebuild runs only while it is None), so a parent prompt left+ # in place would be sent for the child turn with no identity check at all.+ agent._cached_system_prompt = Nonechild_prompt = child.get("system_prompt")if child_prompt:# The tip's stored bytes are seeded straight into the cache slot the turn gates ondiff --git a/tests/agent/test_system_prompt_switch_invalidation.py b/tests/agent/test_system_prompt_switch_invalidation.pyindex 0162e13465d..48085dd65a2 100644--- a/tests/agent/test_system_prompt_switch_invalidation.py+++ b/tests/agent/test_system_prompt_switch_invalidation.py@@ -180,10 +180,17 @@ def test_compression_tip_adoption_applies_the_identity_check(db):only restores while the slot is None), so with the NULL gone it must apply the identitycheck itself: a tip whose route moved since its last persist stays unseeded (the nextrestore rebuilds), while a matching tip is adopted verbatim.++ The slot starts NON-NULL — it holds the parent's prompt, exactly as a live agent that+ cached a turn before the parent rotated. Adoption moves ``agent.session_id`` onto the+ child, so a rejected child prompt must not leave the parent's bytes behind: the turn gate+ (``turn_context``: restore/rebuild only while the slot is None) would then send the parent+ session's prompt for the child turn unvalidated."""from agent.conversation_compression import _adopt_live_compression_childstale = _stored_prompt("model-a", "prov-a")+ parent_prompt = _stored_prompt("parent-model", "parent-provider")db.create_session("parent", source="discord", model="model-a")db.end_session("parent", "compression")db.create_session("child", source="discord", model="model-a", parent_session_id="parent")@@ -193,7 +200,7 @@ def test_compression_tip_adoption_applies_the_identity_check(db):def _adopt(model: str, provider: str) -> MagicMock:agent = MagicMock()- agent._cached_system_prompt = None+ agent._cached_system_prompt = parent_promptagent.session_id = "parent"agent.model, agent.provider = model, provideragent.pass_session_id = False@@ -202,5 +209,7 @@ def test_compression_tip_adoption_applies_the_identity_check(db):assert _adopt_live_compression_child(agent, db, "parent") is not Nonereturn agent- assert _adopt("model-b", "prov-b")._cached_system_prompt is None+ rejected = _adopt("model-b", "prov-b")+ assert rejected.session_id == "child"+ assert rejected._cached_system_prompt is Noneassert _adopt("model-a", "prov-a")._cached_system_prompt == stale