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.
This commit is contained in:
kshitijk4poor 2026-06-22 15:26:29 +05:30
parent 87b60ae49a
commit ebd38e1280

View file

@ -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."""