From 7e4bb7a43b85a93bb940c28a9bb7b43633c88619 Mon Sep 17 00:00:00 2001 From: ciregenz Date: Sat, 22 Aug 2026 21:55:05 -0700 Subject: [PATCH] [eric] agents: OSW_COMPACT_CEILING_TOKENS makes the mid-turn breaker drillable without a 180K turn --- backend/apps/agents/manager/context_budget.py | 24 ++++++++- .../tests/test_compact_ceiling_override.py | 52 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 backend/tests/test_compact_ceiling_override.py diff --git a/backend/apps/agents/manager/context_budget.py b/backend/apps/agents/manager/context_budget.py index 9310f1a9..824be19a 100644 --- a/backend/apps/agents/manager/context_budget.py +++ b/backend/apps/agents/manager/context_budget.py @@ -7,6 +7,7 @@ Compaction here only MARKS (sets compacted_through_msg_id); it never mutates session.messages, the originals stay for the UI drawer and only the history sent to the SDK is trimmed downstream (see backend/CLAUDE.md: "compaction must actually trim, not just mark").""" +import os from typing import Dict, Optional from typeguard import typechecked @@ -17,13 +18,34 @@ from backend.apps.agents.manager.session.history_compaction import get_branch_me from backend.apps.agents.manager.streaming.state import TurnState +@typechecked +def compact_ceiling_tokens(session: AgentSession) -> int: + """The absolute ceiling, with a drill override. + + The mid-turn breaker is otherwise only reachable by paying for a genuine 180K-token turn, + which in practice meant it was never drilled at all: three attempts to fire it cost real + money and still failed. `OSW_COMPACT_CEILING_TOKENS` lowers the bar so the same code path + can be exercised in seconds. Unset everywhere except a drill, and a junk value is ignored + rather than silently trusted.""" + raw = os.environ.get("OSW_COMPACT_CEILING_TOKENS", "").strip() + if raw: + try: + override = int(raw) + except ValueError: + return session.compact_abs_ceiling_tokens + if override > 0: + return override + return session.compact_abs_ceiling_tokens + + @typechecked def compact_trigger_tokens(session: AgentSession) -> int: """The token count where compaction fires: the TIGHTER of the pct threshold and the absolute ceiling (on a 200K window the pct wins at 130K; on a 1M window the ceiling wins at 180K, not 650K).""" window = max(1, session.context_window) - abs_pct = min(1.0, session.compact_abs_ceiling_tokens / window) + ceiling = compact_ceiling_tokens(session) + abs_pct = min(1.0, ceiling / window) return int(window * min(session.compact_threshold_pct, abs_pct)) diff --git a/backend/tests/test_compact_ceiling_override.py b/backend/tests/test_compact_ceiling_override.py new file mode 100644 index 00000000..ae690bdf --- /dev/null +++ b/backend/tests/test_compact_ceiling_override.py @@ -0,0 +1,52 @@ +"""The mid-turn context breaker has to be drillable, or it only ever gets reasoned about. + +Three attempts to fire it live (2026-08-22) failed: the empty-finish path is not inducible by +prompt (the model always writes text), and the breaker itself needs a genuine 180K-token turn, +which costs real money and died to a lane 401 twice. `OSW_COMPACT_CEILING_TOKENS` lowers the bar +so the SAME code path runs in seconds. It must be inert unless deliberately set, and it must not +trust a junk value. +""" + +import os + +import pytest + +from backend.apps.agents.core.models import AgentSession +from backend.apps.agents.manager.context_budget import compact_ceiling_tokens, compact_trigger_tokens + + +def p_session(window: int = 1_000_000) -> AgentSession: + s = AgentSession(name="t", model="opus-5") + s.context_window = window + return s + + +@pytest.fixture(autouse=True) +def p_clean_env(monkeypatch): + monkeypatch.delenv("OSW_COMPACT_CEILING_TOKENS", raising=False) + + +def test_unset_is_exactly_todays_behaviour(): + s = p_session() + assert compact_ceiling_tokens(s) == s.compact_abs_ceiling_tokens == 180_000 + assert compact_trigger_tokens(s) == 180_000 + + +def test_the_override_lowers_the_trigger(monkeypatch): + monkeypatch.setenv("OSW_COMPACT_CEILING_TOKENS", "20000") + assert compact_trigger_tokens(p_session()) == 20_000 + + +def test_a_junk_value_is_ignored_not_trusted(monkeypatch): + for junk in ("banana", "", " ", "1e5", "0", "-5"): + monkeypatch.setenv("OSW_COMPACT_CEILING_TOKENS", junk) + assert compact_ceiling_tokens(p_session()) == 180_000, f"junk {junk!r} must not change the ceiling" + + +def test_the_pct_threshold_still_wins_when_it_is_tighter(monkeypatch): + # On a 200K window the pct (0.65 -> 130K) is tighter than a 180K ceiling; an override must not + # be able to raise the trigger above what the percentage already allows. + s = p_session(window=200_000) + assert compact_trigger_tokens(s) == 130_000 + monkeypatch.setenv("OSW_COMPACT_CEILING_TOKENS", "900000") + assert compact_trigger_tokens(s) == 130_000