AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend/copilot): cap the compaction window pin at the routed model's real window (#14374)
Reinier van der Leer300a8dc119f3
diff --git a/autogpt_platform/backend/backend/copilot/config.py b/autogpt_platform/backend/backend/copilot/config.pyindex d5c03b2cbf..5135a376d2 100644--- a/autogpt_platform/backend/backend/copilot/config.py+++ b/autogpt_platform/backend/backend/copilot/config.py@@ -441,10 +441,9 @@ class ChatConfig(BaseSettings):le=1_000_000,validation_alias=AliasChoices("CHAT_CLAUDE_AGENT_CONTEXT_WINDOW"),description="Context window the SDK subprocess is held to, in tokens "- "(sets ``CLAUDE_CODE_AUTO_COMPACT_WINDOW``; see ``sdk/env.py``). Only "- "raise it on a route that really serves 1M — past the provider's real "- "window the compaction trigger never fires (at 1M on Moonshot it lands "- "at 967K against Kimi's 262,144).",+ "(sets ``CLAUDE_CODE_AUTO_COMPACT_WINDOW``; see ``sdk/env.py``). "+ "Moonshot routes use the lower of this and the SKU's catalog window; "+ "Anthropic routes take it as given.",)claude_agent_autocompact_pct_override: int = Field(default=50,diff --git a/autogpt_platform/backend/backend/copilot/moonshot.py b/autogpt_platform/backend/backend/copilot/moonshot.pyindex 3cb138f8a1..8e9d322911 100644--- a/autogpt_platform/backend/backend/copilot/moonshot.py+++ b/autogpt_platform/backend/backend/copilot/moonshot.py@@ -76,6 +76,11 @@ def _overrides_from_catalog() -> dict[str, tuple[float, float]]:# Import-time snapshot is safe: the catalog is immutable within a process# (the file only changes at deploy), same lifecycle as every projection._RATE_OVERRIDES_USD_PER_MTOK: dict[str, tuple[float, float]] = _overrides_from_catalog()+_CONTEXT_WINDOWS: dict[str, int] = {+ m.slug: m.context_window+ for m in get_catalog().models+ if m.slug.startswith(_MOONSHOT_PREFIX)+}def is_moonshot_model(model: str | None) -> bool:@@ -164,3 +169,13 @@ def moonshot_supports_cache_control(model: str | None) -> bool:ballpark as Anthropic's ~60-95% on continuations."""return is_moonshot_model(model)+++def moonshot_context_window(model: str | None) -> int | None:+ """Catalog context window for a Moonshot *model*, or None if unknown.++ The CLI's model table has no Moonshot entry, so callers that must not+ exceed the provider's real window have nowhere else to read it from.+ Also None for non-Moonshot slugs, which the CLI knows itself.+ """+ return _CONTEXT_WINDOWS.get(model) if model else Nonediff --git a/autogpt_platform/backend/backend/copilot/moonshot_test.py b/autogpt_platform/backend/backend/copilot/moonshot_test.pyindex 0ece3417eb..762b39fcb2 100644--- a/autogpt_platform/backend/backend/copilot/moonshot_test.py+++ b/autogpt_platform/backend/backend/copilot/moonshot_test.py@@ -6,6 +6,7 @@ import pytestfrom backend.copilot.moonshot import (is_moonshot_model,+ moonshot_context_window,moonshot_supports_cache_control,override_cost_usd,rate_card_usd,@@ -177,3 +178,31 @@ class TestSupportsCacheControl:)def test_non_moonshot_does_not_support_cache_control(self, model) -> None:assert moonshot_supports_cache_control(model) is False+++class TestMoonshotContextWindow:+ """The catalog is the only place the real Kimi window is written down."""++ @pytest.mark.parametrize(+ "model, expected",+ [+ ("moonshotai/kimi-k2.5", 262_144),+ ("moonshotai/kimi-k2.6", 262_144),+ ("moonshotai/kimi-k2-thinking", 262_144),+ ("moonshotai/kimi-k3", 1_048_576),+ ],+ )+ def test_reads_the_catalog_window(self, model, expected) -> None:+ assert moonshot_context_window(model) == expected++ @pytest.mark.parametrize(+ "model",+ [+ "moonshotai/kimi-k9-unreleased",+ "anthropic/claude-sonnet-5",+ "",+ None,+ ],+ )+ def test_none_for_unlisted_and_non_moonshot(self, model) -> None:+ assert moonshot_context_window(model) is Nonediff --git a/autogpt_platform/backend/backend/copilot/sdk/env.py b/autogpt_platform/backend/backend/copilot/sdk/env.pyindex 968a519873..62e0a706ed 100644--- a/autogpt_platform/backend/backend/copilot/sdk/env.py+++ b/autogpt_platform/backend/backend/copilot/sdk/env.py@@ -13,7 +13,7 @@ import refrom urllib.parse import urlparsefrom backend.copilot.config import ChatConfig-from backend.copilot.moonshot import is_moonshot_model+from backend.copilot.moonshot import is_moonshot_model, moonshot_context_windowfrom backend.copilot.sdk.subscription import validate_subscription# ChatConfig is stateless (reads env vars) — a separate instance is fine.@@ -178,13 +178,14 @@ def build_sdk_env(# the model table, the 1M capability flags and the server-side experiment# branches that newer CLIs consult — without it the compaction trigger is# an emergent property of whichever bundled CLI we happen to ship.- env["CLAUDE_CODE_AUTO_COMPACT_WINDOW"] = str(config.claude_agent_context_window)+ window = _pinned_context_window(model)+ env["CLAUDE_CODE_AUTO_COMPACT_WINDOW"] = str(window)# The window above is clamped by the model's own window, which this# kill-switch holds at 200K; keeping it set past that point would swallow# the raise silently. 1M is GA (no beta header) on Sonnet 4.6+/5, so the# experimental-betas flag above does not cover it.- if config.claude_agent_context_window <= _CLI_DEFAULT_CONTEXT_WINDOW:+ if window <= _CLI_DEFAULT_CONTEXT_WINDOW:env["CLAUDE_CODE_DISABLE_1M_CONTEXT"] = "1"# Trigger threshold, as a percentage of the window pinned above (CLI@@ -216,6 +217,21 @@ def build_sdk_env(_CLI_DEFAULT_CONTEXT_WINDOW = 200_000+def _pinned_context_window(model: str | None) -> int:+ """Configured window pin, capped at a Moonshot route's real window.++ A pin above what the provider serves puts the compaction trigger past the+ point the provider rejects the turn, so compaction never fires. Anthropic+ is deliberately not capped: the catalog holds every Anthropic entry at 200K+ pending the Claude-5 tokenizer soak, which would cancel a legitimate raise.+ An unlisted Kimi SKU falls back to the window the CLI assumes anyway.+ """+ window = config.claude_agent_context_window+ if not is_moonshot_model(model):+ return window+ return min(window, moonshot_context_window(model) or _CLI_DEFAULT_CONTEXT_WINDOW)++# Sonnet 5's tokenizer counts ~30% more tokens than 4.x for the same text,# so the same 50%-of-200K trigger would compact at ~77% of the *text* budget# 4.x sessions get. Scaling the trigger by the inflation factor keeps thediff --git a/autogpt_platform/backend/backend/copilot/sdk/env_test.py b/autogpt_platform/backend/backend/copilot/sdk/env_test.pyindex af6d51c898..89edaa42ca 100644--- a/autogpt_platform/backend/backend/copilot/sdk/env_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/env_test.py@@ -633,7 +633,8 @@ class TestContextWindowPin:"""The window the CLI compacts against is ours, not the CLI's guess.At or below the default, ``CLAUDE_CODE_DISABLE_1M_CONTEXT`` holds the model- window itself at 200K too, making the default a real cap.+ window itself at 200K too, making the default a real cap. On Moonshot+ routes the pin is capped at the catalog's real window for the SKU."""@pytest.mark.parametrize(@@ -658,6 +659,43 @@ class TestContextWindowPin:assert result.get("CLAUDE_CODE_AUTO_COMPACT_WINDOW") == expectedassert ("CLAUDE_CODE_DISABLE_1M_CONTEXT" in result) is kill_switch+ @pytest.mark.parametrize(+ "model, window, expected, kill_switch",+ [+ # Kimi K2.x really serves 262,144: a 1M pin would put the trigger+ # at ~967K, past the point the provider rejects the request.+ ("moonshotai/kimi-k2.5", 1_000_000, "262144", False),+ ("moonshotai/kimi-k2-thinking", 500_000, "262144", False),+ # K3 really does serve 1M, so the raise survives there.+ ("moonshotai/kimi-k3", 1_000_000, "1000000", False),+ # A SKU the catalog does not carry falls back to the window the+ # CLI assumes anyway, rather than to an unbounded raise.+ ("moonshotai/kimi-k3.0", 1_000_000, "200000", True),+ # At or below the real window the configured pin is untouched.+ ("moonshotai/kimi-k2.5", 262_144, "262144", False),+ ("moonshotai/kimi-k2.5", 200_000, "200000", True),+ ],+ )+ def test_pin_capped_at_moonshot_real_window(+ self, model, window, expected, kill_switch+ ):+ """The CLI's model table has no Moonshot entry, so the pin is the only+ thing holding the trigger inside Kimi's real window."""+ cfg = _make_config(+ use_openrouter=True,+ api_key="sk-or-test",+ base_url="https://openrouter.ai/api/v1",+ thinking_standard_model=model,+ claude_agent_context_window=window,+ )+ with patch("backend.copilot.sdk.env.config", cfg):+ from backend.copilot.sdk.env import build_sdk_env++ result = build_sdk_env(model=model)++ assert result.get("CLAUDE_CODE_AUTO_COMPACT_WINDOW") == expected+ assert ("CLAUDE_CODE_DISABLE_1M_CONTEXT" in result) is kill_switch+def test_context_window_rejects_out_of_range(self):"""Pydantic bounds (ge=100_000, le=1_000_000) are the only guard betweena typo'd env var and the CLI's own clamps."""