"""Cache-stability monitoring: collapse detection and run accounting.""" import json from pydantic_ai.usage import RequestUsage from bot.core.cache_stability import ( MIN_PREFIX_TOKENS, CacheMonitor, ) def usage(*, uncached: int = 0, read: int = 0, write: int = 0) -> RequestUsage: return RequestUsage( input_tokens=uncached + read + write, cache_read_tokens=read, cache_write_tokens=write, ) def monitor() -> CacheMonitor: m = CacheMonitor.__new__(CacheMonitor) # skip _load(): no disk in tests m.runs = __import__("collections").deque(maxlen=60) m._current = None m._marks = {} m._latched = set() m._last_seen = {} return m def observe(m: CacheMonitor, **kw) -> None: m.observe(usage(**kw), model="claude-opus-5", provider="anthropic") def test_healthy_run_records_no_collapse(): m = monitor() m.begin_run("batch processing") observe(m, uncached=800, write=12_000) observe(m, uncached=200, read=12_000) observe(m, uncached=150, read=12_200) m.end_run() (run,) = m.runs assert run.requests == 3 assert run.collapses == 0 assert run.cache_read == 24_200 # the write is a real cost at a premium, so it stays in the denominator: # 24200 / (24200 read + 12000 written + 1150 uncached) assert round(run.hit_rate, 2) == 0.65 def test_collapse_detected_when_read_back_drops(): m = monitor() m.begin_run("cycle") observe(m, uncached=500, write=20_000) observe(m, uncached=500, read=20_000) observe(m, uncached=20_000, read=0) # prefix moved — nothing read back m.end_run() (run,) = m.runs assert run.collapses == 1 assert run.samples[-1].collapsed def test_collapse_warns_once_then_relatches_after_recovery(): m = monitor() m.begin_run("cycle") observe(m, write=20_000) observe(m, read=20_000) observe(m, read=0) # collapse observe(m, read=0) # sustained — latched, not re-reported observe(m, read=20_000) # healthy again, unlatches observe(m, read=0) # a fresh collapse is reported m.end_run() (run,) = m.runs assert [s.collapsed for s in run.samples] == [ False, False, True, False, False, True, ] def test_small_prefix_never_judged_collapsed(): """Below Anthropic's minimum cacheable size the read count is noise.""" m = monitor() m.begin_run("bio rewrite") observe(m, write=MIN_PREFIX_TOKENS - 100) observe(m, read=0) m.end_run() (run,) = m.runs assert run.collapses == 0 def test_marks_are_per_model_so_a_switch_does_not_warn(): m = monitor() m.begin_run("cycle") m.observe(usage(write=20_000), model="claude-opus-5", provider="anthropic") m.observe(usage(read=0), model="claude-haiku-4-5", provider="anthropic") m.end_run() (run,) = m.runs assert run.collapses == 0 def test_warm_start_reflects_first_request_read_back(): """The 1h tool+instruction TTL bridging two runs is the thing it proves.""" cold = monitor() cold.begin_run("cycle") observe(cold, uncached=14_000, write=14_000) cold.end_run() assert cold.runs[0].warm_start is False warm = monitor() warm.begin_run("cycle") observe(warm, uncached=300, read=14_000) warm.end_run() assert warm.runs[0].warm_start is True def test_marks_reset_between_runs(): """A new run must not be judged against the previous run's prefix.""" m = monitor() m.begin_run("first") observe(m, write=20_000) observe(m, read=20_000) m.end_run() m.begin_run("second") observe(m, uncached=20_000, read=0) # cold start, not a collapse m.end_run() assert m.runs[1].collapses == 0 def test_empty_run_is_not_recorded(): m = monitor() m.begin_run("failed before any model request") m.end_run() assert not m.runs def test_summary_aggregates_the_window(): m = monitor() m.begin_run("a") observe(m, uncached=1_000, write=10_000) m.end_run() m.begin_run("b") observe(m, uncached=500, read=10_000) m.end_run() summary = m.summary() assert summary["window_runs"] == 2 assert summary["cache_read"] == 10_000 assert summary["cache_write"] == 10_000 assert summary["uncached"] == 1_500 assert summary["warm_starts"] == 1 # newest run first, so the cockpit reads top-down assert [r["label"] for r in summary["runs"]] == ["b", "a"] def test_observation_failure_does_not_break_the_run(monkeypatch): """The monitor is observational — it must never take a run down.""" from bot.core.cache_stability import CacheObservingModel m = monitor() model = CacheObservingModel.__new__(CacheObservingModel) model.monitor = m def boom(*args, **kwargs): raise RuntimeError("telemetry exploded") monkeypatch.setattr(m, "observe", boom) monkeypatch.setattr( type(model), "wrapped", property(lambda self: _FakeModel()), raising=False ) model._observe(usage(read=1)) # does not raise class _FakeModel: model_name = "claude-opus-5" system = "anthropic" def test_saving_is_measured_against_a_no_cache_bill(): """The panel's headline: what caching removed from the input bill. Priced at the 1h write premium (2x) because the provider reports one write total without splitting 5m from 1h — the conservative read. """ m = monitor() m.begin_run("batch processing") observe(m, uncached=10_000, read=80_000, write=10_000) m.end_run() (run,) = m.runs # no cache: 100k tokens at 1x. billed: 80k*0.1 + 10k*2 + 10k*1 = 38k saved = run.saved assert saved is not None assert round(saved, 3) == 0.62 def test_a_write_only_run_costs_more_than_no_cache_at_all(): """Storing a prefix nothing reads back is a loss — the panel must be able to say so rather than always reporting a win.""" m = monitor() m.begin_run("cold cycle") observe(m, uncached=1_000, write=40_000) m.end_run() (run,) = m.runs saved = run.saved assert saved is not None assert saved < 0 def test_summary_reports_the_live_strategy_not_a_copy(): """The cockpit renders TTLs from this, so it can never describe a policy phi isn't running.""" from bot.core.cache_stability import CACHE_TTLS m = monitor() m.begin_run("a") observe(m, uncached=100, read=5_000) m.end_run() summary = m.summary() assert summary["strategy"] == CACHE_TTLS assert summary["prices"]["read"] == 0.1 def test_trace_link_is_captured_from_the_model_request_not_begin_run(): """Regression: begin_run happens before agent.run(), where no span is active — a trace id read there is always invalid, and every row shipped without a link. The span only exists once a model request is in flight. """ from opentelemetry.sdk.trace import TracerProvider tracer = TracerProvider().get_tracer("test") m = monitor() m.begin_run("cycle") # deliberately OUTSIDE any span, as in production with tracer.start_as_current_span("chat claude-opus-5"): observe(m, uncached=100, read=5_000) m.end_run() entry = m.summary()["runs"][0] assert entry["trace_id"] and len(entry["trace_id"]) == 32 assert entry["trace_id"] in (entry["trace_url"] or "") def test_trace_id_is_taken_from_the_first_request_only(): """One run is one trace; a later request must not overwrite it.""" from opentelemetry.sdk.trace import TracerProvider tracer = TracerProvider().get_tracer("test") m = monitor() m.begin_run("cycle") with tracer.start_as_current_span("chat 1"): observe(m, uncached=100, write=5_000) assert m._current is not None first = m._current.trace_id with tracer.start_as_current_span("chat 2"): observe(m, uncached=100, read=5_000) m.end_run() assert first and m.runs[0].trace_id == first def test_no_trace_link_outside_a_span(): m = monitor() m.begin_run("cycle") observe(m, uncached=100, read=5_000) m.end_run() assert m.summary()["runs"][0]["trace_url"] is None def test_provider_total_is_not_added_to_its_cached_subsets(tmp_path, monkeypatch): """Recorded voice-run usage, through the actual Anthropic usage adapter.""" from bot.core import cache_stability monkeypatch.setattr(cache_stability, "CACHE_FILE", tmp_path / "cache.json") m = monitor() m.begin_run("voice sample") for raw, read, write in [(2, 0, 53_081), (354, 65_803, 0)]: observed = RequestUsage.extract( { "model": "claude-sonnet-5", "usage": { "input_tokens": raw, "cache_read_input_tokens": read, "cache_creation_input_tokens": write, "output_tokens": 10, }, }, provider="anthropic", provider_url="https://api.anthropic.com", provider_fallback="anthropic", ) m.observe(observed, model="claude-sonnet-5", provider="anthropic") m.end_run() assert [s.billed_prefix for s in m.runs[0].samples] == [53_083, 66_157] assert m.summary()["uncached"] == 356 assert m.summary()["uncached_cost_tokens"] == 119_240 assert m.summary()["hit_rate"] == round(65_803 / 119_240, 4) # Existing snapshots already store provider totals. Reload recomputes # aggregates from those samples, ignoring previously inflated summaries. saved = json.loads(cache_stability.CACHE_FILE.read_text()) saved["runs"][0]["uncached"] = 119_240 cache_stability.CACHE_FILE.write_text(json.dumps(saved)) restored = CacheMonitor() assert restored.summary() == m.summary() assert restored.request_sizes() == m.request_sizes() def test_other_provider_keeps_usage_without_anthropic_cost_or_collapse( tmp_path, monkeypatch ): from bot.core import cache_stability monkeypatch.setattr(cache_stability, "CACHE_FILE", tmp_path / "cache.json") m = monitor() m.begin_run("Terra trial") m.observe(usage(uncached=100, read=2000), model="gpt-5.6-terra", provider="openai") m.observe(usage(uncached=2100), model="gpt-5.6-terra", provider="openai") m.end_run() summary = m.summary() assert summary["cache_read"] == 2000 assert summary["uncached"] == 2200 assert summary["saved"] is None assert summary["prices"] is None assert summary["strategy"] is None assert summary["billed_tokens"] is None assert summary["collapses"] == 0 loaded = CacheMonitor() assert loaded.runs[0].samples[0].provider == "openai" assert loaded.runs[0].saved is None stored = json.loads(cache_stability.CACHE_FILE.read_text()) for sample in stored["runs"][0]["samples"]: sample.pop("provider") cache_stability.CACHE_FILE.write_text(json.dumps(stored)) legacy = CacheMonitor() assert legacy.runs[0].samples[0].provider == "unknown" assert legacy.summary()["cache_read"] == 2000 assert legacy.summary()["saved"] is None def test_mixed_anthropic_models_do_not_combine_different_base_prices( tmp_path, monkeypatch ): from bot.core import cache_stability monkeypatch.setattr(cache_stability, "CACHE_FILE", tmp_path / "cache.json") m = monitor() for model in ["claude-sonnet-5", "claude-opus-5"]: m.begin_run(model) m.observe(usage(uncached=100, read=2000), model=model, provider="anthropic") m.end_run() assert all(r.saved is not None for r in m.runs) assert m.summary()["saved"] is None assert m.summary()["cache_read"] == 4000