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(cli): report anonymous beacon status accurately in the banner, log, and /stats (#3533)
Bradley Allan Davisfa9edb3a2989
diff --git a/headroom/cli/proxy.py b/headroom/cli/proxy.pyindex e3bb2979f..58a25b239 100644--- a/headroom/cli/proxy.py+++ b/headroom/cli/proxy.py@@ -1547,18 +1547,36 @@ Memory (Multi-Provider):" Stateless: YES (no filesystem writes — memory, logs, TOIN disabled)\n")- from headroom.telemetry.beacon import is_telemetry_enabled+ # Build telemetry section for the startup banner.+ #+ # HEADROOM_TELEMETRY (local aggregate stats, off by default) and+ # HEADROOM_BEACON (the anonymous upload beacon, ON by default —+ # see telemetry/beacon.py) are two independent switches. This banner+ # used to check only is_telemetry_enabled() and print "DISABLED" for+ # any operator who had merely turned local stats off, even though the+ # beacon — the switch that actually ships data off the machine — was+ # still on and unmentioned. Delegate to format_telemetry_notice(), the+ # one place that already gets the beacon-vs-local distinction right,+ # instead of re-deriving (and re-drifting from) the same wording here.+ from headroom.telemetry.beacon import (+ format_telemetry_notice,+ is_beacon_enabled,+ is_telemetry_enabled,+ )- # Build telemetry section for the startup banner. Telemetry is opt-in- # (off by default); the disabled line surfaces how to opt in.- if is_telemetry_enabled():- telemetry_line = (- " Telemetry: ENABLED (anonymous aggregate stats — you opted in)\n"- " Disable: HEADROOM_TELEMETRY=off or headroom proxy --no-telemetry"- )+ _notice = format_telemetry_notice(prefix=" ")+ if _notice:+ telemetry_line = _notice+ elif is_beacon_enabled() or is_telemetry_enabled():+ # format_telemetry_notice() returns "" when HEADROOM_TELEMETRY_WARN=off+ # suppresses the notice text itself — still say ON/OFF plainly rather+ # than silently showing nothing in the one place an operator is most+ # likely to be checking.+ telemetry_line = " Telemetry: ON (notice suppressed via HEADROOM_TELEMETRY_WARN=off)"else:telemetry_line = (- " Telemetry: DISABLED (opt in: HEADROOM_TELEMETRY=on or headroom proxy --telemetry)"+ " Telemetry: OFF (local stats: HEADROOM_TELEMETRY=on to enable | "+ "beacon: HEADROOM_BEACON=on to enable)")# Discover proxy extensions (third-party packages registered via thediff --git a/headroom/dashboard/templates/dashboard.html b/headroom/dashboard/templates/dashboard.htmlindex f796393b6..3eb691532 100644--- a/headroom/dashboard/templates/dashboard.html+++ b/headroom/dashboard/templates/dashboard.html@@ -143,7 +143,7 @@</div><template x-if="stats.anon_telemetry_shipping"><div class="inline-flex items-center gap-1.5 rounded-full border border-amber-500/40 bg-amber-500/10 px-2.5 py-1"- title="Anonymous aggregate telemetry is enabled. Disable with HEADROOM_TELEMETRY=off or --no-telemetry.">+ title="The anonymous telemetry beacon is on: compression stats only, never prompts, code, or file paths. Turn it off with HEADROOM_BEACON=off or DO_NOT_TRACK=1."><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentColor" aria-hidden="true"class="w-3 h-3 text-amber-400 shrink-0"><path fill-rule="evenodd"diff --git a/headroom/proxy/server.py b/headroom/proxy/server.pyindex b14d3048b..0801b5429 100644--- a/headroom/proxy/server.py+++ b/headroom/proxy/server.py@@ -189,7 +189,7 @@ from headroom.subscription.tracker import (get_subscription_tracker,)from headroom.telemetry import get_telemetry_collector-from headroom.telemetry.beacon import is_telemetry_enabled+from headroom.telemetry.beacon import is_beacon_enabled, is_telemetry_enabledfrom headroom.telemetry.toin import get_toinfrom headroom.transforms import (CacheAligner,@@ -2203,9 +2203,13 @@ class HeadroomProxy()# Log local telemetry status so operators can see it in the log stream.- # Nothing is sent externally — telemetry is collected locally only (the- # anonymous telemetry beacon was removed); operational metrics export- # only to your own OTEL collector via HEADROOM_OTEL_METRICS_*.+ # This is HEADROOM_TELEMETRY only (local aggregate stats; nothing+ # sent externally). It is NOT a statement about the anonymous upload+ # beacon (HEADROOM_BEACON), which is a separate, ON-by-default switch+ # (telemetry/beacon.py) still present in this codebase — the previous+ # wording here ("the anonymous telemetry beacon was removed") was+ # false and predates this fix. See is_beacon_enabled() below for the+ # accurate beacon-shipping status.if is_telemetry_enabled():logger.info("Local telemetry: ENABLED (aggregate stats, local only — nothing sent "@@ -2216,6 +2220,13 @@ class HeadroomProxy("Local telemetry: DISABLED (off by default — opt in: ""HEADROOM_TELEMETRY=on or --telemetry)")+ if is_beacon_enabled():+ logger.info(+ "Anonymous upload beacon: ENABLED (default — session summaries are sent to "+ "Headroom Labs). Opt out: HEADROOM_BEACON=off or DO_NOT_TRACK=1"+ )+ else:+ logger.info("Anonymous upload beacon: DISABLED")self.pipeline_extensions.emit(PipelineStage.POST_START,@@ -4804,9 +4815,14 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:# Per-language AST compression pauses. Empty on a healthy install;# non-empty is the explanation for a savings drop in one language."code_syntax_breaker": _code_syntax_breaker_status(),- # Always False: the anonymous telemetry beacon was removed, so no- # telemetry is ever shipped externally (local collection only).- "anon_telemetry_shipping": False,+ # Reflects the actual live state of the anonymous upload beacon+ # (HEADROOM_BEACON, on by default — see telemetry/beacon.py).+ # This field previously hardcoded False on the incorrect premise+ # that the beacon had been removed from the codebase; it had not,+ # so an operator polling /stats to confirm nothing ships+ # externally was given a false assurance regardless of their+ # actual HEADROOM_BEACON setting.+ "anon_telemetry_shipping": is_beacon_enabled(),"telemetry": {"enabled": telemetry_stats.get("enabled", False),"total_compressions": telemetry_stats.get("total_compressions", 0),diff --git a/tests/test_telemetry_warning.py b/tests/test_telemetry_warning.pyindex c8a70131a..96f70dc99 100644--- a/tests/test_telemetry_warning.py+++ b/tests/test_telemetry_warning.py@@ -132,7 +132,19 @@ class TestFormatTelemetryNotice:class TestProxyCLITelemetryBanner:- """Proxy CLI startup banner must include telemetry status."""+ """Proxy CLI startup banner must include telemetry status.++ Every scenario below sets HEADROOM_BEACON=off explicitly, isolating the+ local-telemetry-only wording (mirrors the convention already used by+ TestFormatTelemetryNotice, e.g. test_returns_notice_when_telemetry_on).+ Before the beacon-disclosure fix the banner never looked at the beacon+ at all, so these tests would have passed unchanged whether the beacon+ was on or off -- which is exactly the bug: an operator relying on this+ banner had no way to tell the two apart. The two tests at the bottom of+ this class (*_beacon_default_on_is_surfaced,+ *_beacon_off_local_off_says_fully_off) cover the beacon-on-by-default+ case the old banner never surfaced.+ """@pytest.fixturedef runner(self):@@ -141,6 +153,7 @@ class TestProxyCLITelemetryBanner:def test_banner_shows_telemetry_enabled(self, runner, monkeypatch):# Telemetry is opt-in: it only shows ENABLED once explicitly turned on.monkeypatch.setenv("HEADROOM_TELEMETRY", "on")+ monkeypatch.setenv("HEADROOM_BEACON", "off")from headroom.cli.main import main@@ -151,9 +164,12 @@ class TestProxyCLITelemetryBanner:assert "ENABLED" in result.outputdef test_banner_disabled_by_default(self, runner, monkeypatch):- # The whole point of opt-in: unset env => telemetry off, banner says so- # and surfaces how to opt in.+ # The whole point of opt-in: unset env => local telemetry off, banner+ # says so and surfaces how to opt in. (Beacon pinned off here so this+ # isolates the local-only wording; see the beacon-specific tests+ # below for the on-by-default beacon case.)monkeypatch.delenv("HEADROOM_TELEMETRY", raising=False)+ monkeypatch.setenv("HEADROOM_BEACON", "off")from headroom.cli.main import main@@ -161,11 +177,12 @@ class TestProxyCLITelemetryBanner:result = runner.invoke(main, ["proxy"])assert "Telemetry:" in result.output- assert "DISABLED" in result.output- assert "HEADROOM_TELEMETRY=on" in result.output or "--telemetry" in result.output+ assert "OFF" in result.output+ assert "HEADROOM_TELEMETRY=on" in result.outputdef test_telemetry_flag_opts_in(self, runner, monkeypatch):monkeypatch.delenv("HEADROOM_TELEMETRY", raising=False)+ monkeypatch.setenv("HEADROOM_BEACON", "off")from headroom.cli.main import main@@ -177,6 +194,7 @@ class TestProxyCLITelemetryBanner:def test_banner_shows_telemetry_disabled(self, runner, monkeypatch):monkeypatch.setenv("HEADROOM_TELEMETRY", "off")+ monkeypatch.setenv("HEADROOM_BEACON", "off")from headroom.cli.main import main@@ -184,10 +202,11 @@ class TestProxyCLITelemetryBanner:result = runner.invoke(main, ["proxy"])assert "Telemetry:" in result.output- assert "DISABLED" in result.output+ assert "OFF" in result.outputdef test_no_telemetry_flag_disables(self, runner, monkeypatch):monkeypatch.delenv("HEADROOM_TELEMETRY", raising=False)+ monkeypatch.setenv("HEADROOM_BEACON", "off")from headroom.cli.main import main@@ -195,10 +214,11 @@ class TestProxyCLITelemetryBanner:result = runner.invoke(main, ["proxy", "--no-telemetry"])assert "Telemetry:" in result.output- assert "DISABLED" in result.output+ assert "OFF" in result.outputdef test_banner_shows_opt_out_instructions_when_enabled(self, runner, monkeypatch):monkeypatch.setenv("HEADROOM_TELEMETRY", "on")+ monkeypatch.setenv("HEADROOM_BEACON", "off")from headroom.cli.main import main@@ -207,6 +227,49 @@ class TestProxyCLITelemetryBanner:assert "HEADROOM_TELEMETRY=off" in result.output or "--no-telemetry" in result.output+ def test_banner_beacon_default_on_is_surfaced(self, runner, monkeypatch):+ """The actual bug: HEADROOM_TELEMETRY=off / --no-telemetry alone used+ to make the banner print a bare "Telemetry: DISABLED" even though the+ anonymous upload beacon (a separate, on-by-default switch) was still+ active and never mentioned. After the fix, an operator who sets only+ --no-telemetry still sees the beacon disclosed."""+ monkeypatch.setenv("HEADROOM_TELEMETRY", "off")+ monkeypatch.delenv("HEADROOM_BEACON", raising=False)+ monkeypatch.delenv("DO_NOT_TRACK", raising=False)++ from headroom.cli.main import main++ with patch("headroom.proxy.server.run_server", side_effect=SystemExit(0)):+ result = runner.invoke(main, ["proxy", "--no-telemetry"])++ assert "Telemetry:" in result.output+ assert "compression stats" in result.output+ assert "HEADROOM_BEACON=off" in result.output+ # The old banner's bare "DISABLED" claim must not appear on the+ # Telemetry line specifically (the banner has an unrelated+ # "Memory: DISABLED" line that must not make this assertion a+ # false pass) -- that combination is exactly the false assurance+ # this fix removes.+ telemetry_line = next(+ line for line in result.output.splitlines() if line.strip().startswith("Telemetry:")+ )+ assert "DISABLED" not in telemetry_line++ def test_banner_beacon_off_and_local_off_says_fully_off(self, runner, monkeypatch):+ """Only when BOTH switches are off does the banner claim nothing is+ happening -- the one case where a bare OFF claim is actually true."""+ monkeypatch.setenv("HEADROOM_TELEMETRY", "off")+ monkeypatch.setenv("HEADROOM_BEACON", "off")++ from headroom.cli.main import main++ with patch("headroom.proxy.server.run_server", side_effect=SystemExit(0)):+ result = runner.invoke(main, ["proxy"])++ assert "Telemetry:" in result.output+ assert "OFF" in result.output+ assert "compression stats" not in result.output+# ---------------------------------------------------------------------------# wrap CLI telemetry notice@@ -268,14 +331,30 @@ class TestWrapCLITelemetryNotice:@pytest.mark.asyncioclass TestStatsEndpointTelemetryFlag:- """The /stats endpoint must expose anon_telemetry_shipping."""+ """The /stats endpoint's anon_telemetry_shipping field must reflect the+ live HEADROOM_BEACON state, not a hardcoded constant.++ Previously this field was hardcoded to False on the premise that "the+ anonymous telemetry beacon was removed" -- it was not (telemetry/beacon.py+ and telemetry/session.py fully implement and wire it via+ record_outcome() -> SessionAggregator -> a POST to+ headroom-beacon.headroom-beacon.workers.dev, gated on is_beacon_enabled(),+ which is ON BY DEFAULT). An operator polling /stats to confirm nothing+ ships externally got a false assurance regardless of their actual+ HEADROOM_BEACON setting. These two tests previously asserted `False`+ unconditionally, encoding the same incorrect premise as the field they+ were testing.+ """pytest.importorskip("fastapi")- async def test_stats_anon_telemetry_shipping_always_false(self, monkeypatch):- # The anonymous telemetry beacon was removed, so nothing is ever shipped- # externally — even with telemetry explicitly enabled.+ async def test_stats_anon_telemetry_shipping_reflects_beacon_on(self, monkeypatch):+ # HEADROOM_BEACON is on by default (BEACON_DEFAULT_ON = True) and+ # HEADROOM_TELEMETRY does not touch it -- setting local telemetry on+ # must not by itself make this field misreport False.monkeypatch.setenv("HEADROOM_TELEMETRY", "on")+ monkeypatch.delenv("HEADROOM_BEACON", raising=False)+ monkeypatch.delenv("DO_NOT_TRACK", raising=False)from headroom.proxy.server import ProxyConfig, create_appapp = create_app(@@ -294,10 +373,11 @@ class TestStatsEndpointTelemetryFlag:assert resp.status_code == 200data = resp.json()assert "anon_telemetry_shipping" in data- assert data["anon_telemetry_shipping"] is False+ assert data["anon_telemetry_shipping"] is True- async def test_stats_includes_anon_telemetry_shipping_false(self, monkeypatch):+ async def test_stats_anon_telemetry_shipping_false_when_beacon_off(self, monkeypatch):monkeypatch.setenv("HEADROOM_TELEMETRY", "off")+ monkeypatch.setenv("HEADROOM_BEACON", "off")from headroom.proxy.server import ProxyConfig, create_appapp = create_app(@@ -318,6 +398,29 @@ class TestStatsEndpointTelemetryFlag:assert "anon_telemetry_shipping" in dataassert data["anon_telemetry_shipping"] is False+ async def test_stats_anon_telemetry_shipping_false_when_do_not_track(self, monkeypatch):+ # DO_NOT_TRACK silences the beacon regardless of HEADROOM_BEACON.+ monkeypatch.setenv("DO_NOT_TRACK", "1")+ monkeypatch.delenv("HEADROOM_BEACON", raising=False)+ from headroom.proxy.server import ProxyConfig, create_app++ app = create_app(+ ProxyConfig(+ cache_enabled=False,+ rate_limit_enabled=False,+ cost_tracking_enabled=False,+ )+ )++ from httpx import ASGITransport, AsyncClient++ async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:+ resp = await client.get("/stats")++ assert resp.status_code == 200+ data = resp.json()+ assert data["anon_telemetry_shipping"] is False+# ---------------------------------------------------------------------------# telemetry __init__ exports