Skip to content

The agent that grows with you

fix(compression): announce compaction before the pre-compression credential refresh

kshitijk4poore91abaf863ec
diff --git a/agent/conversation_compression.py b/agent/conversation_compression.py
index 2b4abf1d53e..9edc9711b64 100644
--- a/agent/conversation_compression.py
+++ b/agent/conversation_compression.py
@@ -4116,8 +4116,6 @@ def compress_context(
if not force and _automatic_compression_gate_blocks(agent, bypass_cooldown):
return messages, _existing_system_prompt(agent, system_message)
- _refresh_main_credential(agent)
-
_pre_msg_count = len(messages)
# In-place keeps the SAME session_id (no rotation/child/renumber/re-sync). A
# missing attribute must default True, not rotation, which can wedge sessions.
@@ -4128,6 +4126,8 @@ def compress_context(
lifecycle = _announce_compression_start(
agent, message_count=_pre_msg_count, approx_tokens=approx_tokens, focus_topic=focus_topic, force=force
)
+ # After the announce: an expired token's refresh is a blocking network call. Before the probe and summary call.
+ _refresh_main_credential(agent)
# Lazy feasibility probe (~400ms cold) on first attempt, not __init__; it sets
# _compression_warning so status replay still surfaces the warning. Marked checked
# only after the probe completes (transient failures are swallowed inside). A hard
diff --git a/tests/agent/test_anthropic_third_party_oauth_guard.py b/tests/agent/test_anthropic_third_party_oauth_guard.py
index 3ad5a078c01..b93e67ed11e 100644
--- a/tests/agent/test_anthropic_third_party_oauth_guard.py
+++ b/tests/agent/test_anthropic_third_party_oauth_guard.py
@@ -134,7 +134,7 @@ class TestOAuthFlagOnRefresh:
from agent.conversation_compression import compress_context
old, new = "sk-ant-oat01-old", "sk-ant-oat01-new"
- seen = []
+ seen, announced = [], []
agent.api_mode, agent.provider = "anthropic_messages", "anthropic"
agent._anthropic_base_url = "https://api.anthropic.com"
agent._anthropic_client = MagicMock()
@@ -151,11 +151,13 @@ class TestOAuthFlagOnRefresh:
patch("agent.anthropic_credentials.resolve_anthropic_token", return_value=new),
patch.object(AIAgent, "_build_direct_anthropic_client", return_value=MagicMock()),
patch.object(cc, "compress", side_effect=fake_compress),
+ patch.object(agent, "_emit_status", side_effect=lambda _s: announced.append(agent._anthropic_api_key)),
):
compress_context(agent, [{"role": "user", "content": "q"}, {"role": "assistant", "content": "a"}],
"system", approx_tokens=100_000, force=True)
assert seen == [new]
+ assert announced[:1] == [old] # the Desktop announce lands before the (possibly blocking) refresh
assert agent._anthropic_api_key == agent.api_key == new
assert agent._primary_runtime == {"api_key": new, "anthropic_api_key": new, "compressor_api_key": new,
"is_anthropic_oauth": agent._is_anthropic_oauth}