From 624972ec549c5468a1a8271070ea6fe80f9036ab Mon Sep 17 00:00:00 2001
From: Manohar Paturi <186662190+ManoharPaturi@users.noreply.github.com>
Date: Tue, 13 Oct 2026 19:57:27 +0530
Subject: [PATCH 1/3] Python: fix(lab-tau2): check the over-max warning before
the half-of-max warning
The half-of-max branch ran first and also matched every over-max case,
so the red truncation warning was dead code. Swap the branch order.
Fixes #9338
Signed-off-by: Manohar Paturi <186662190+ManoharPaturi@users.noreply.github.com>
---
.../_sliding_window.py | 10 +++---
.../lab/tau2/tests/test_sliding_window.py | 34 +++++++++++++++++++
2 files changed, 39 insertions(+), 5 deletions(-)
diff --git a/python/packages/lab/tau2/agent_framework_lab_tau2/_sliding_window.py b/python/packages/lab/tau2/agent_framework_lab_tau2/_sliding_window.py
index 3a6157f3ef0..1218f047971 100644
--- a/python/packages/lab/tau2/agent_framework_lab_tau2/_sliding_window.py
+++ b/python/packages/lab/tau2/agent_framework_lab_tau2/_sliding_window.py
@@ -100,16 +100,16 @@ def _get_token_count(self, messages: list[Message]) -> int:
# Simple text message
total_tokens += self._estimate_any_object_token_count(msg.text)
- if total_tokens > self.max_tokens / 2:
+ if total_tokens > self.max_tokens:
+ logger.opt(colors=True).warning(
+ f"Total tokens {total_tokens} is over max tokens {self.max_tokens}. Will truncate messages."
+ )
+ elif total_tokens > self.max_tokens / 2:
logger.opt(colors=True).warning(
f"Total tokens {total_tokens} is "
f"{total_tokens / self.max_tokens * 100:.0f}% "
f"of max tokens {self.max_tokens}"
)
- elif total_tokens > self.max_tokens:
- logger.opt(colors=True).warning(
- f"Total tokens {total_tokens} is over max tokens {self.max_tokens}. Will truncate messages."
- )
return total_tokens
diff --git a/python/packages/lab/tau2/tests/test_sliding_window.py b/python/packages/lab/tau2/tests/test_sliding_window.py
index 8da46e86958..ba3eb06a3ed 100644
--- a/python/packages/lab/tau2/tests/test_sliding_window.py
+++ b/python/packages/lab/tau2/tests/test_sliding_window.py
@@ -211,3 +211,37 @@ async def test_real_world_scenario():
token_count = provider._get_token_count(truncated)
assert token_count <= provider.max_tokens * 1.1
+
+
+def test_get_token_count_warns_over_max():
+ """The over-max warning must win over the half-of-max warning (#9338)."""
+ import io
+
+ from loguru import logger
+
+ provider = SlidingWindowHistoryProvider(max_tokens=10)
+ messages = [
+ Message(
+ role="user",
+ contents=[
+ Content.from_text(
+ text="hello world, this message is intentionally long so it clearly exceeds the tiny ten token budget"
+ )
+ ],
+ )
+ ]
+
+ total = provider._get_token_count(messages)
+ assert total > provider.max_tokens
+
+ buffer = io.StringIO()
+ logger.remove()
+ logger.add(buffer, format="{message}")
+ try:
+ provider._get_token_count(messages)
+ finally:
+ logger.remove()
+
+ logged = buffer.getvalue()
+ assert "over max tokens" in logged
+ assert "Will truncate messages" in logged
From 2faf47a620f26589d20b72f48f90c44df50a3033 Mon Sep 17 00:00:00 2001
From: Manohar Paturi <186662190+ManoharPaturi@users.noreply.github.com>
Date: Wed, 14 Oct 2026 17:44:02 +0530
Subject: [PATCH 2/3] Python: wrap the long literal in the over-max warning
test
Signed-off-by: Manohar Paturi <186662190+ManoharPaturi@users.noreply.github.com>
---
python/packages/lab/tau2/tests/test_sliding_window.py | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/python/packages/lab/tau2/tests/test_sliding_window.py b/python/packages/lab/tau2/tests/test_sliding_window.py
index ba3eb06a3ed..dc509e98444 100644
--- a/python/packages/lab/tau2/tests/test_sliding_window.py
+++ b/python/packages/lab/tau2/tests/test_sliding_window.py
@@ -225,7 +225,10 @@ def test_get_token_count_warns_over_max():
role="user",
contents=[
Content.from_text(
- text="hello world, this message is intentionally long so it clearly exceeds the tiny ten token budget"
+ text=(
+ "hello world, this message is intentionally long "
+ "so it clearly exceeds the tiny ten token budget"
+ )
)
],
)
From 1bacfe2148a21c058fa59a7a57c195a25629d3ba Mon Sep 17 00:00:00 2001
From: Manohar Paturi <186662190+ManoharPaturi@users.noreply.github.com>
Date: Thu, 15 Oct 2026 16:53:54 +0530
Subject: [PATCH 3/3] Python: remove only the test sink from the loguru logger
Signed-off-by: Manohar Paturi <186662190+ManoharPaturi@users.noreply.github.com>
---
python/packages/lab/tau2/tests/test_sliding_window.py | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/python/packages/lab/tau2/tests/test_sliding_window.py b/python/packages/lab/tau2/tests/test_sliding_window.py
index dc509e98444..1a45485146f 100644
--- a/python/packages/lab/tau2/tests/test_sliding_window.py
+++ b/python/packages/lab/tau2/tests/test_sliding_window.py
@@ -238,12 +238,11 @@ def test_get_token_count_warns_over_max():
assert total > provider.max_tokens
buffer = io.StringIO()
- logger.remove()
- logger.add(buffer, format="{message}")
+ handler_id = logger.add(buffer, format="{message}")
try:
provider._get_token_count(messages)
finally:
- logger.remove()
+ logger.remove(handler_id)
logged = buffer.getvalue()
assert "over max tokens" in logged