From ebd38e12807ded8514d20c6699d880598a903c9f Mon Sep 17 00:00:00 2001 From: kshitijk4poor <82637225+kshitijk4poor@users.noreply.github.com> Date: Mon, 22 Jun 2026 15:26:29 +0530 Subject: [PATCH] test(agent): regression for token-only compression progress (#39550, #23767) Adds test_413_retries_on_token_only_compression: same message count but materially fewer tokens after compaction must count as progress and retry, not abort. Fails on main without the salvaged fix, passes with it. --- tests/run_agent/test_413_compression.py | 42 +++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/run_agent/test_413_compression.py b/tests/run_agent/test_413_compression.py index 4801e48eda35..48ce2636c560 100644 --- a/tests/run_agent/test_413_compression.py +++ b/tests/run_agent/test_413_compression.py @@ -440,6 +440,48 @@ class TestHTTP413Compression: assert result.get("partial") is True assert "413" in result["error"] + def test_413_retries_on_token_only_compression(self, agent): + """Same message COUNT but fewer TOKENS must count as progress and retry. + + Regression for #39550/#23767: tool-result pruning / in-place + summarization can shrink request size without dropping the message + count. The old gate (len(messages) < original_len) treated that as + 'cannot compress further' and aborted; the fix re-estimates tokens and + retries when they drop materially. + """ + err_413 = _make_413_error() + ok_resp = _mock_response(content="OK after token-only compaction", finish_reason="stop") + agent.client.chat.completions.create.side_effect = [err_413, ok_resp] + + # 3 large messages in, 3 much smaller messages out (same count, far + # fewer tokens) — exactly the token-only-progress case. + prefill = [ + {"role": "user", "content": "x" * 4000}, + {"role": "assistant", "content": "y" * 4000}, + {"role": "user", "content": "z" * 4000}, + ] + + with ( + patch.object(agent, "_compress_context") as mock_compress, + patch.object(agent, "_persist_session"), + patch.object(agent, "_save_trajectory"), + patch.object(agent, "_cleanup_task_resources"), + ): + # Same message count (3) but ~10x smaller content → token drop. + mock_compress.return_value = ( + [ + {"role": "user", "content": "x" * 300}, + {"role": "assistant", "content": "y" * 300}, + {"role": "user", "content": "z" * 300}, + ], + "compressed prompt", + ) + result = agent.run_conversation("hello", conversation_history=prefill) + + mock_compress.assert_called_once() + assert result["completed"] is True + assert result["final_response"] == "OK after token-only compaction" + class TestPreflightCompression: """Preflight compression should compress history before the first API call."""