Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(proxy): reset the cc-switch upstream when Claude Official is selected (#3166)

gglucassfed7281120af
diff --git a/headroom/proxy/cc_switch_reconciler.py b/headroom/proxy/cc_switch_reconciler.py
index a39cad586..959e3644b 100644
--- a/headroom/proxy/cc_switch_reconciler.py
+++ b/headroom/proxy/cc_switch_reconciler.py
@@ -162,7 +162,20 @@ class CCSwitchReconciler:
self._atomic_write(data)
logger.info("cc-switch reconciler: official -> route via Headroom")
return True
- return False # leave official direct (default, safe for OAuth)
+ # Leaving official direct (default, safe for OAuth) still has to
+ # drop a previously captured third-party upstream. The upstream is
+ # process-wide (``HeadroomProxy.ANTHROPIC_API_URL`` is a class
+ # attr), so a stale DeepSeek/Kimi endpoint would keep receiving
+ # this proxy's Anthropic traffic from every client still routed
+ # through it -- with Anthropic credentials attached.
+ if self.current_upstream not in (None, self.default_upstream):
+ logger.info(
+ "cc-switch reconciler: official -> upstream reset to %s",
+ self.default_upstream,
+ )
+ self.current_upstream = self.default_upstream
+ self._set_upstream(self.default_upstream)
+ return False
# Already pointing at us: nothing to do (loop guard).
if url.rstrip("/") == self.proxy_url:
diff --git a/tests/test_proxy/test_cc_switch_reconciler.py b/tests/test_proxy/test_cc_switch_reconciler.py
index a27aff03b..a635c2c45 100644
--- a/tests/test_proxy/test_cc_switch_reconciler.py
+++ b/tests/test_proxy/test_cc_switch_reconciler.py
@@ -115,6 +115,33 @@ def test_official_left_direct_by_default(tmp_path, monkeypatch):
assert json.loads(sf.read_text())["env"] == {}
+def test_official_resets_a_captured_third_party_upstream(tmp_path, monkeypatch):
+ monkeypatch.delenv("HEADROOM_CC_SWITCH_ROUTE_OFFICIAL", raising=False)
+ r, sf, captured = _make(tmp_path)
+ _write(sf, {"env": {"ANTHROPIC_BASE_URL": "https://api.kimi.com/anthropic"}})
+ assert r.tick() is True
+ assert captured[-1] == "https://api.kimi.com/anthropic"
+
+ # Switching back to Claude Official leaves settings.json direct, but the
+ # process-wide upstream must not stay on Kimi -- otherwise Anthropic OAuth
+ # traffic from any client still routed through this proxy is sent there.
+ _write(sf, {"env": {}})
+ assert r.tick() is False
+ assert json.loads(sf.read_text())["env"] == {}
+ assert captured[-1] == DEFAULT
+ assert r.current_upstream == DEFAULT
+
+
+def test_official_does_not_reset_upstream_when_never_captured(tmp_path, monkeypatch):
+ monkeypatch.delenv("HEADROOM_CC_SWITCH_ROUTE_OFFICIAL", raising=False)
+ r, sf, captured = _make(tmp_path)
+ _write(sf, {"env": {}})
+ assert r.tick() is False
+ # Nothing was captured, so nothing to reset: an operator-configured
+ # upstream must not be clobbered by a passing official tick.
+ assert captured == []
+
+
def test_official_routed_when_opted_in(tmp_path, monkeypatch):
monkeypatch.setenv("HEADROOM_CC_SWITCH_ROUTE_OFFICIAL", "1")
r, sf, captured = _make(tmp_path)