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.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(proxy): recognise every local spelling of the proxy in the cc-switch loop guard (#4405)
HetCreepd9958bfdff04
diff --git a/headroom/proxy/cc_switch_reconciler.py b/headroom/proxy/cc_switch_reconciler.pyindex 959e3644b..c2b91b649 100644--- a/headroom/proxy/cc_switch_reconciler.py+++ b/headroom/proxy/cc_switch_reconciler.py@@ -50,6 +50,7 @@ import loggingimport osfrom collections.abc import Callablefrom pathlib import Path+from urllib.parse import urlsplitlogger = logging.getLogger(__name__)@@ -98,6 +99,23 @@ class CCSwitchReconciler:self._task: asyncio.Task | None = Noneself._last_mtime_ns: int | None = None+ def _is_self(self, url: str) -> bool:+ """True if ``url`` names this proxy (``localhost`` or its bind address, any path).++ Loopback families are distinct sockets: ``[::1]`` is not the proxy when it is+ bound to ``127.0.0.1``, so another local provider on that address is captured.+ """+ try:+ want = urlsplit(self.proxy_url)+ got = urlsplit(url)+ return (+ (got.hostname or "").lower() in {"localhost", (want.hostname or "").lower()}+ and got.scheme == want.scheme+ and got.port == want.port+ )+ except ValueError:+ return False+async def start(self) -> None:if self._task is not None:return@@ -178,7 +196,7 @@ class CCSwitchReconciler:return False# Already pointing at us: nothing to do (loop guard).- if url.rstrip("/") == self.proxy_url:+ if self._is_self(url):return False# Third-party / custom endpoint: capture it as upstream, point Claude at us.diff --git a/tests/test_proxy/test_cc_switch_reconciler.py b/tests/test_proxy/test_cc_switch_reconciler.pyindex a635c2c45..a4aab3cdd 100644--- a/tests/test_proxy/test_cc_switch_reconciler.py+++ b/tests/test_proxy/test_cc_switch_reconciler.py@@ -66,6 +66,66 @@ def test_no_rewrite_loop(tmp_path):assert r.tick() is False+ "url",+ [+ "http://127.0.0.1:8787",+ "http://127.0.0.1:8787/",+ "http://127.0.0.1:8787/v1",+ "http://localhost:8787",+ "http://LOCALHOST:8787/v1/",+ ],+)+def test_proxy_own_url_spellings_are_not_captured(tmp_path, url):+ r, sf, captured = _make(tmp_path)+ _write(sf, {"env": {"ANTHROPIC_BASE_URL": url}})+ assert r.tick() is False+ assert captured == []+ assert r.current_upstream is None+++ "url",+ [+ "http://127.0.0.1:8788",+ "http://localhost:9999/v1",+ "https://127.0.0.1:8787",+ "http://[::1]:8787",+ "https://api.deepseek.com/anthropic",+ ],+)+def test_other_ports_and_hosts_are_still_captured(tmp_path, url):+ r, sf, captured = _make(tmp_path)+ _write(sf, {"env": {"ANTHROPIC_BASE_URL": url}})+ assert r.tick() is True+ assert captured == [url]++[email protected]("url", ["http://127.0.0.1:99999", "http://[::1"])+def test_unparseable_base_url_is_not_treated_as_the_proxy(tmp_path, url):+ # urlsplit raises ValueError for these; such a URL is not this proxy.+ r, sf, captured = _make(tmp_path)+ _write(sf, {"env": {"ANTHROPIC_BASE_URL": url}})+ assert r.tick() is True+ assert captured == [url]+++def test_ipv6_proxy_address_is_self_only_for_ipv6_bind(tmp_path):+ sf = tmp_path / "settings.json"+ captured: list[str] = []+ r = CCSwitchReconciler(+ proxy_url="http://[::1]:8787",+ default_upstream=DEFAULT,+ set_upstream=captured.append,+ path=sf,+ )+ _write(sf, {"env": {"ANTHROPIC_BASE_URL": "http://[::1]:8787"}})+ assert r.tick() is False+ _write(sf, {"env": {"ANTHROPIC_BASE_URL": "http://127.0.0.1:8787"}})+ assert r.tick() is True+ assert captured == ["http://127.0.0.1:8787"]++def test_switching_provider_recaptures(tmp_path):r, sf, captured = _make(tmp_path)_write(