refactor(telemetry): cut dead schema; tests assert what's actually written

Self-review after the #51714 feedback found the reviewer's dead-table finding
was not isolated — the schema advertised far more than the code populates, and
our own tests hid it by hand-feeding fields production never sends. Make the
surface honest by subtraction.

Schema (10 tel_* tables -> 5):
  - Delete tel_gateway_events, tel_cron_events, tel_skill_events,
    tel_memory_events, tel_feedback_events — declared, never written, never read.
  - Drop columns nothing populates: tel_runs.{profile_id,estimated_cost_usd,
    cost_status}; tel_model_calls.{ttft_ms,estimated_cost_usd,cost_status,
    cost_source,end_reason,retry_count}; tel_tool_calls.{backend,retry_count,
    approval}; tel_spans.attrs_json. Cost duplicated the existing sessions
    billing columns and was always NULL here.
  - events.py / emitter _TABLE_COLUMNS / OTLP _span_attrs / rollup / preview
    display all trimmed to match.

Correctness:
  - end_reason no longer hardcodes "completed". Production finalize callers pass
    `reason` (shutdown/session_expired/session_reset); _coarse_end_reason now
    reads it and maps accordingly.
  - Fix a latent bug the trim exposed: the model_call hook passed end_reason= to
    ModelCallEvent, which the @_safe wrapper was silently swallowing — so
    tel_model_calls dropped every row in real runs. Now writes correctly.

Tests:
  - Stop hand-feeding estimated_cost_usd / turn_exit_reason that no production
    call site sends. Finalize is now driven with the real `reason` kwarg, and
    assertions cover only fields that are actually populated. This is what let
    the model_call drop hide — the suite graded on a fictional contract.

Net: a smaller system that does what it says. Verified end-to-end over the real
dispatch path (runs + connected span tree + model/tool rows populate; dead
tables gone). 160 telemetry/state/insights tests green.
This commit is contained in:
emozilla 2026-06-27 01:21:41 -04:00 • committed by Victor Kyriazakos
parent 7c80b79bb0
commit edac3a7bae
12 changed files with 32 additions and 52 deletions

View file

@ -182,7 +182,6 @@ def _on_post_api_request(**kw: Any) -> None:
cache_write_tokens=int(usage.get("cache_write_tokens") or 0),
reasoning_tokens=int(usage.get("reasoning_tokens") or 0),
latency_ms=latency_ms,
end_reason="completed",
)
with _runs_lock:
run["model_call_count"] += 1
@ -324,30 +323,33 @@ def _on_session_finalize(**kw: Any) -> None:
model_call_count=run.get("model_call_count", 0),
tool_call_count=run.get("tool_call_count", 0),
error_count=run.get("error_count", 0),
estimated_cost_usd=_as_float(kw.get("estimated_cost_usd")),
cost_status=kw.get("cost_status"),
))
spans.clear_run()
def _coarse_end_reason(kw: Dict[str, Any]) -> str:
"""Map a finalize payload to a coarse end reason.
Production finalize callers pass ``reason`` (e.g. "shutdown", "session_expired",
"session_reset"); the older ``turn_exit_reason``/``interrupted``/``failed`` keys are
honored when present. Defaults to "ended".
"""
if kw.get("interrupted"):
return "interrupted"
if kw.get("failed"):
return "failed"
reason = str(kw.get("turn_exit_reason") or "").lower()
reason = str(kw.get("turn_exit_reason") or kw.get("reason") or "").lower()
if "max_iteration" in reason:
return "max_iterations"
if "timeout" in reason:
return "timeout"
return "completed"
def _as_float(v: Any) -> Optional[float]:
try:
return float(v) if v is not None else None
except (TypeError, ValueError):
return None
if "expired" in reason:
return "expired"
if "reset" in reason:
return "reset"
if "shutdown" in reason or "complete" in reason:
return "completed"
return reason or "ended"
# ── subagent lineage (reserved) ─────────────────────────────────────────────