Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions agent/agent_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,34 @@ def _moa_reference_relay(event: str, **kwargs: Any) -> None:
compression_enabled = str(_compression_cfg.get("enabled", True)).lower() in {"true", "1", "yes"}
compression_target_ratio = float(_compression_cfg.get("target_ratio", 0.20))
compression_protect_last = int(_compression_cfg.get("protect_last_n", 20))
# Cap on compression retry rounds before a turn gives up with "max
# compression attempts reached" (compression.max_attempts). Hardcoding 3
# strands sessions that legitimately need more rounds — e.g. a restart
# history reload whose incompressible tool schemas keep the request
# estimate above the threshold even though the messages compress fine
# (the #62605 failure class). Default 3 preserves current behavior, so
# an unset key is behavior-neutral; validated >= 1, hard-capped at 10,
# and any non-int-like value falls back to 3. Booleans are rejected
# (bool subclasses int, so int(True) would silently become 1) and
# fractional floats are rejected rather than truncated — "4.7 attempts"
# is a config mistake, not a request for 4.
_raw_max_attempts = _compression_cfg.get("max_attempts", 3)
if isinstance(_raw_max_attempts, bool):
compression_max_attempts = 3
elif isinstance(_raw_max_attempts, int):
compression_max_attempts = _raw_max_attempts
elif isinstance(_raw_max_attempts, float):
compression_max_attempts = (
int(_raw_max_attempts) if _raw_max_attempts.is_integer() else 3
)
else:
try:
compression_max_attempts = int(str(_raw_max_attempts).strip())
except (TypeError, ValueError):
compression_max_attempts = 3
if compression_max_attempts < 1:
compression_max_attempts = 3
compression_max_attempts = min(compression_max_attempts, 10)
# protect_first_n is the number of non-system messages to protect at
# the head, in addition to the system prompt (which is always
# implicitly protected by the compressor). Floor at 0 — a value of
Expand Down Expand Up @@ -2224,6 +2252,7 @@ def _moa_reference_relay(event: str, **kwargs: Any) -> None:
agent.compression_enabled = compression_enabled
agent.compression_in_place = compression_in_place
agent.codex_app_server_auto_compaction = codex_app_server_auto_compaction
agent.max_compression_attempts = compression_max_attempts

# Reject models whose context window is below the minimum required
# for reliable tool-calling workflows (64K tokens).
Expand Down
20 changes: 16 additions & 4 deletions agent/conversation_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,13 @@ def run_conversation(
truncated_tool_call_retries = 0
truncated_response_parts: List[str] = []
compression_attempts = 0
# One resolved per-turn compression attempt cap, shared by every site that
# consumes ``compression_attempts``: the pre-API pressure gate, the
# overflow/413 retry handlers, and the post-tool compaction gate.
# Config-driven via compression.max_attempts (parsed + validated in
# agent_init); default 3 preserves the prior hardcoded behavior for
# objects without the attribute (older pickles / minimal stubs).
max_compression_attempts = getattr(agent, "max_compression_attempts", 3)
_last_preflight_pressure: Optional[int] = None
_preflight_compression_blocked = _ctx.preflight_compression_blocked
_turn_exit_reason = "unknown" # Diagnostic: why the loop ended
Expand Down Expand Up @@ -1168,7 +1175,7 @@ def run_conversation(
if (
agent.compression_enabled
and len(messages) > 1
and compression_attempts < 3
and compression_attempts < max_compression_attempts
and not _preflight_compression_blocked
and not _defer_preflight(request_pressure_tokens)
and not _compression_cooldown
Expand All @@ -1177,12 +1184,13 @@ def run_conversation(
compression_attempts += 1
logger.info(
"Pre-API compression: ~%s request tokens >= %s threshold "
"(context=%s, attempt=%s/3)",
"(context=%s, attempt=%s/%s)",
f"{request_pressure_tokens:,}",
f"{int(getattr(_compressor, 'threshold_tokens', 0) or 0):,}",
f"{int(getattr(_compressor, 'context_length', 0) or 0):,}"
if getattr(_compressor, "context_length", 0) else "unknown",
compression_attempts,
max_compression_attempts,
)
agent._emit_status(
f"📦 Pre-API compression: ~{request_pressure_tokens:,} tokens "
Expand Down Expand Up @@ -1252,7 +1260,6 @@ def run_conversation(
retry_count = 0
max_retries = agent._api_max_retries
_retry = TurnRetryState()
max_compression_attempts = 3

finish_reason = "stop"
response = None # Guard against UnboundLocalError if all retries fail
Expand Down Expand Up @@ -5175,7 +5182,12 @@ def _perform_api_call(next_api_kwargs):
messages, tools=agent.tools or None
)

if agent.compression_enabled and _compressor.should_compress(_real_tokens):
if (
agent.compression_enabled
and compression_attempts < max_compression_attempts
and _compressor.should_compress(_real_tokens)
):
compression_attempts += 1
agent._safe_print(" ⟳ compacting context…")
messages, active_system_prompt = agent._compress_context(
messages, system_message,
Expand Down
8 changes: 7 additions & 1 deletion agent/turn_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,13 @@ def build_turn_context(
f">= {_compressor.threshold_tokens:,} threshold. "
"This may take a moment."
)
for _pass in range(3):
# Preflight passes honor the same configured per-turn cap
# (compression.max_attempts) as the loop's compression sites;
# default 3 preserves the prior hardcoded behavior.
_max_preflight_passes = max(
1, int(getattr(agent, "max_compression_attempts", 3) or 3)
)
for _pass in range(_max_preflight_passes):
_orig_len = len(messages)
_orig_tokens = _preflight_tokens
messages, active_system_prompt = agent._compress_context(
Expand Down
6 changes: 6 additions & 0 deletions cli-config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,12 @@ compression:
# compression of older turns.
protect_last_n: 20

# Compression retry rounds before a turn gives up with "max compression
# attempts reached" (default: 3, same as the previous hardcoded value).
# Raise (e.g. 6) for tool-schema-heavy sessions where 3 rounds cannot bring
# the request estimate under the threshold. Validated >= 1, hard cap 10.
max_attempts: 3

# Codex app-server (codex CLI runtime) thread-compaction mode. The codex
# agent owns the real thread context on this runtime, so Hermes' summarizer
# cannot shrink it — compaction goes through the app server instead.
Expand Down
1 change: 1 addition & 0 deletions contributors/emails/dominicbejar@gmail.com
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dombejar
5 changes: 5 additions & 0 deletions hermes_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,11 @@ def _ensure_hermes_home_managed(home: Path):
# set this above 0.75 to override the floor.
"target_ratio": 0.20, # fraction of threshold to preserve as recent tail
"protect_last_n": 20, # minimum recent messages to keep uncompressed
"max_attempts": 3, # compression retry rounds before a turn gives up
# with "max compression attempts reached". Raise
# (e.g. 6) for tool-schema-heavy sessions where 3
# rounds cannot clear the request estimate.
# Validated >= 1, hard-capped at 10.
"hygiene_hard_message_limit": 5000, # gateway session-hygiene force-compress threshold by message count
"protect_first_n": 3, # non-system head messages always preserved
# verbatim, in ADDITION to the system prompt
Expand Down
119 changes: 119 additions & 0 deletions tests/agent/test_compression_max_attempts_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
"""compression.max_attempts — config-driven compression retry cap.

The conversation loop's compression retry cap was hardcoded to 3, stranding
sessions that legitimately need more rounds — e.g. a restart history reload
whose incompressible tool schemas keep the request estimate above the
threshold while the messages themselves compress fine (the #62605 failure
class). The cap is now parsed from ``compression.max_attempts`` in
``agent_init`` and read by the loop via
``getattr(agent, "max_compression_attempts", 3)``.

These tests pin the parse/validate/attach seam: default preserved, custom
value honored, floor and ceiling enforced, garbage tolerated.
"""

from __future__ import annotations

import contextlib
import io
from pathlib import Path

from hermes_state import SessionDB
from run_agent import AIAgent


def _config(max_attempts=None) -> dict:
compression = {
"enabled": True,
"threshold": 0.50,
"target_ratio": 0.20,
"protect_first_n": 3,
"protect_last_n": 20,
}
if max_attempts is not None:
compression["max_attempts"] = max_attempts
return {
"compression": compression,
"prompt_caching": {"cache_ttl": "5m"},
"sessions": {},
"bedrock": {},
}


def _make_agent(monkeypatch, tmp_path: Path, *, max_attempts=None):
from hermes_cli import config as config_mod

monkeypatch.setattr(
config_mod, "load_config", lambda: _config(max_attempts=max_attempts)
)
db = SessionDB(db_path=tmp_path / "state.db")
with contextlib.redirect_stdout(io.StringIO()):
agent = AIAgent(
base_url="https://chatgpt.com/backend-api/codex",
api_key="test-key",
provider="openai-codex",
model="gpt-5.5",
enabled_toolsets=[],
disabled_toolsets=[],
quiet_mode=True,
skip_memory=True,
session_db=db,
session_id="max-attempts-test",
)
return agent


class TestCompressionMaxAttemptsConfig:
def test_default_is_three_when_unset(self, monkeypatch, tmp_path):
agent = _make_agent(monkeypatch, tmp_path)
assert agent.max_compression_attempts == 3

def test_custom_value_is_honored(self, monkeypatch, tmp_path):
agent = _make_agent(monkeypatch, tmp_path, max_attempts=6)
assert agent.max_compression_attempts == 6

def test_hard_capped_at_ten(self, monkeypatch, tmp_path):
agent = _make_agent(monkeypatch, tmp_path, max_attempts=25)
assert agent.max_compression_attempts == 10

def test_zero_and_negative_fall_back_to_default(self, monkeypatch, tmp_path):
agent = _make_agent(monkeypatch, tmp_path, max_attempts=0)
assert agent.max_compression_attempts == 3
agent = _make_agent(monkeypatch, tmp_path, max_attempts=-2)
assert agent.max_compression_attempts == 3

def test_non_integer_falls_back_to_default(self, monkeypatch, tmp_path):
agent = _make_agent(monkeypatch, tmp_path, max_attempts="lots")
assert agent.max_compression_attempts == 3

def test_boolean_is_rejected_not_coerced(self, monkeypatch, tmp_path):
# bool subclasses int: int(True) == 1 would silently near-disable
# compression retries. YAML `max_attempts: true` must fall back to 3.
agent = _make_agent(monkeypatch, tmp_path, max_attempts=True)
assert agent.max_compression_attempts == 3
agent = _make_agent(monkeypatch, tmp_path, max_attempts=False)
assert agent.max_compression_attempts == 3

def test_fractional_float_is_rejected_not_truncated(self, monkeypatch, tmp_path):
# "4.7 attempts" is a config mistake, not a request for 4.
agent = _make_agent(monkeypatch, tmp_path, max_attempts=4.7)
assert agent.max_compression_attempts == 3

def test_integral_float_and_numeric_string_are_accepted(
self, monkeypatch, tmp_path
):
agent = _make_agent(monkeypatch, tmp_path, max_attempts=5.0)
assert agent.max_compression_attempts == 5
agent = _make_agent(monkeypatch, tmp_path, max_attempts="6")
assert agent.max_compression_attempts == 6

def test_loop_pickup_degrades_to_default_when_attribute_missing(
self, monkeypatch, tmp_path
):
# The loop reads getattr(agent, "max_compression_attempts", 3): a
# configured agent exposes its value, and an object without the
# attribute (older pickle / minimal stub) degrades to the prior
# hardcoded behavior.
agent = _make_agent(monkeypatch, tmp_path, max_attempts=7)
assert getattr(agent, "max_compression_attempts", 3) == 7
assert getattr(object(), "max_compression_attempts", 3) == 3
Loading
Loading