Skip to content

The agent that grows with you

fix(gateway): a provider pick drops the replaced route's context window

Tekniuma67e033a78dd
diff --git a/gateway/session_policy.py b/gateway/session_policy.py
index 071a9347cf1..0611208236d 100644
--- a/gateway/session_policy.py
+++ b/gateway/session_policy.py
@@ -388,7 +388,9 @@ def _apply_launch_overrides(params, config):
# own — the same route keys a committed /model switch rewrites (#138843). An explicit
# base_url is applied after the drop, so a pinned endpoint survives without carrying
# the replaced provider's other pins, and a same-provider re-pick keeps the profile's.
- for stale in ('base_url', 'api_mode', 'key_env', 'api_key_env', 'api_key', 'api', 'api_key_from'):
+ from gateway.session_route_identity import ROUTE_BOUND_KEYS
+ # The replaced provider's context window goes too: it was sized for the old model.
+ for stale in ROUTE_BOUND_KEYS:
model.pop(stale, None)
for key in ('provider', 'base_url'):
if key in params:
diff --git a/tests/gateway/test_session_policy.py b/tests/gateway/test_session_policy.py
index 0fa3df4a31e..6a70b1580ce 100644
--- a/tests/gateway/test_session_policy.py
+++ b/tests/gateway/test_session_policy.py
@@ -169,6 +169,17 @@ def test_provider_override_drops_the_previous_providers_route(tmp_path):
assert unrecorded.config()['model'] == {'default': 'm', 'provider': 'xiaomi'}
+def test_provider_change_drops_the_old_providers_context_window(tmp_path):
+ """The profile's context_length was sized for its own model: a pick to another provider must
+ not pin that window on the new model, while a same-route alias pick keeps it."""
+ from gateway.session_policy import build_policy
+ cfg = {'model': {'default': 'qwen', 'provider': 'custom', 'base_url': 'http://localhost:1234/v1',
+ 'context_length': 8192}}
+ moved = build_policy(dict(cwd=str(tmp_path), model='claude-opus-4.6', provider='anthropic'), cfg)
+ assert 'context_length' not in moved.config()['model']
+ alias = build_policy(dict(cwd=str(tmp_path), model='qwen', provider='vllm'), cfg)
+ assert alias.config()['model']['context_length'] == 8192
+
def test_provider_pick_over_auto_template_sends_its_key_to_its_own_endpoint(tmp_path, monkeypatch):
"""#139018: the install template (`provider: auto` + OpenRouter base_url) plus a Desktop
Google AI Studio pick resolved provider=gemini against openrouter.ai, so the Google key went