Skip to content

The agent that grows with you

fix(compression): harden native compaction rejection matcher + config coercion (#82777)

Tekniume029e300caa1
diff --git a/agent/agent_init.py b/agent/agent_init.py
index 79bf7e9fd84..1f77240b001 100644
--- a/agent/agent_init.py
+++ b/agent/agent_init.py
@@ -2105,7 +2105,11 @@ def init_agent(
# Native OpenAI Responses server-side compaction (opt-in). Only ever
# engages for gpt-5.6-family models on api.openai.com or the ChatGPT
# Codex backend — the per-request gate lives in agent/native_compaction.py.
- codex_responses_native_compaction = bool(
+ # Shared truthy coercion: "false"/"off"/"no" strings stay disabled
+ # (bool("false") is True — #82777).
+ from utils import is_truthy_value as _is_truthy
+
+ codex_responses_native_compaction = _is_truthy(
_compression_cfg.get("codex_responses_native", False)
)
_native_threshold_raw = _compression_cfg.get(
diff --git a/agent/conversation_loop.py b/agent/conversation_loop.py
index bc7dad72c36..38fc16523fb 100644
--- a/agent/conversation_loop.py
+++ b/agent/conversation_loop.py
@@ -4517,7 +4517,9 @@ def run_conversation(
and bool(getattr(agent, "codex_responses_native_compaction", False))
):
from agent.native_compaction import is_native_compaction_rejection
- if is_native_compaction_rejection(api_error):
+ if is_native_compaction_rejection(
+ api_error, getattr(api_error, "status_code", None)
+ ):
_retry.native_compaction_reject_retry_attempted = True
agent.codex_responses_native_compaction = False
agent._vprint(
diff --git a/agent/native_compaction.py b/agent/native_compaction.py
index caceda8aeda..dd4cd1d08c4 100644
--- a/agent/native_compaction.py
+++ b/agent/native_compaction.py
@@ -266,16 +266,41 @@ def prune_pre_checkpoint_items(
return checkpoint_run + list(reversed(retained_reversed)) + post
-def is_native_compaction_rejection(error: Any) -> bool:
- """True when a provider error names the context_management field.
+def is_native_compaction_rejection(error: Any, status_code: Any = None) -> bool:
+ """True when a provider error is a STRUCTURED rejection of the
+ context_management field.
Used by the conversation loop's one-shot recovery: strip the field,
disable native compaction for the rest of the session, retry. Matching
- is deliberately narrow — generic 4xx/5xx/timeouts must NOT permanently
- downgrade native compaction, they take the normal retry path.
+ is deliberately narrow — a transient 5xx/timeout whose body merely
+ ECHOES the request (and therefore contains the field name) must NOT
+ permanently downgrade native compaction for the session (#82777).
+
+ Two conditions, both required when a status is known:
+
+ * ``status_code`` is 400 (or unknown/None — some transports surface
+ only a message string; field-name matching alone is then the best
+ available signal, preserving pre-#82777 behavior for them), and
+ * the error text names ``context_management`` / ``compact_threshold``
+ alongside rejection language ("unknown", "unsupported", "invalid",
+ "unexpected", "not permitted"...). A bare field-name echo without
+ rejection language does not match.
"""
text = str(error or "").lower()
- return "context_management" in text or "compact_threshold" in text
+ if "context_management" not in text and "compact_threshold" not in text:
+ return False
+ if status_code is not None:
+ try:
+ if int(status_code) != 400:
+ return False
+ except (TypeError, ValueError):
+ pass
+ rejection_markers = (
+ "unknown", "unsupported", "invalid", "unexpected", "not permitted",
+ "not allowed", "unrecognized", "extra field", "no such", "bad request",
+ "not supported",
+ )
+ return any(marker in text for marker in rejection_markers)
def merge_interim_reasoning_items(
diff --git a/tests/run_agent/test_native_compaction.py b/tests/run_agent/test_native_compaction.py
index 24aaee5aaac..e62595e3b03 100644
--- a/tests/run_agent/test_native_compaction.py
+++ b/tests/run_agent/test_native_compaction.py
@@ -183,6 +183,50 @@ class TestRejectionMatcher:
):
assert not is_native_compaction_rejection(err)
+ def test_field_echo_without_rejection_language_does_not_match(self):
+ # A transient failure body that merely echoes the request (and so
+ # contains the field name) must not downgrade the session (#82777).
+ assert not is_native_compaction_rejection(
+ "upstream timeout while processing request with "
+ "context_management=[{...}]"
+ )
+ assert not is_native_compaction_rejection(
+ "connection reset; last request included compact_threshold=200000"
+ )
+
+ def test_non_400_status_does_not_match(self):
+ msg = "Unknown parameter: 'context_management'"
+ assert not is_native_compaction_rejection(msg, 500)
+ assert not is_native_compaction_rejection(msg, 503)
+ assert not is_native_compaction_rejection(msg, 429)
+
+ def test_400_status_with_rejection_language_matches(self):
+ msg = "Unknown parameter: 'context_management'"
+ assert is_native_compaction_rejection(msg, 400)
+
+ def test_unknown_status_preserves_message_only_matching(self):
+ # Transports that surface only a string keep working.
+ assert is_native_compaction_rejection(
+ "Error code: 400 - Unknown parameter: 'context_management'", None
+ )
+ assert is_native_compaction_rejection(
+ "unsupported field compact_threshold", "not-a-number"
+ )
+
+
+class TestConfigCoercion:
+ def test_false_like_strings_stay_disabled(self, monkeypatch):
+ from utils import is_truthy_value
+
+ for raw in ("false", "off", "no", "0", "", "FALSE", " Off "):
+ assert not is_truthy_value(raw, False), raw
+
+ def test_true_like_strings_enable(self):
+ from utils import is_truthy_value
+
+ for raw in ("true", "1", "yes", "on", "TRUE"):
+ assert is_truthy_value(raw, False), raw
+
class TestWirePlumbing:
"""context_management flows through build_kwargs and both preflights."""