← Back to Pull requests

Keep only the latest handler timing instead of one per output #948

Open
unknown opened this pull request with 301 lines diff across 3 files
View on GitHub
unknown unknown commented on Dec 01, 2026 11:26 UTC
Author

Summary

BaseHandler keeps the duration of every output it has ever emitted:

self._times: list[float] = []
...
self._times.append(perf_counter() - start_time)   # once per emitted output, in _run_loop
...
@property
def last_time(self) -> float:
    return self._times[-1]

The only reader is last_time, which takes the last entry. The list is never trimmed, so every handler thread grows it by one float for each output for the life of the process. OpenAICompatibleSTTHandler appends to it too.

The TTS stage is where this adds up. It emits one output per 512-sample block at 16 kHz, about 31 per second of speech, and the LLM stage emits one per streamed chunk. Each entry costs about 32 bytes (a float object plus a list slot), so a server that speaks for an hour keeps about 3.6 MB in the TTS handler's list alone. That memory is never released, because the handler threads, and with them the pooled Realtime pipeline units, live as long as the server.

Change

-        self._times: list[float] = []
+        # only the latest duration is read (last_time); a list grew by one entry per output forever
+        self._times: deque[float] = deque(maxlen=1)

A deque(maxlen=1) keeps the same interface, .append() and [-1], so last_time, the timing log and OpenAICompatibleSTTHandler's own append are unchanged. The tests that build handlers with object.__new__ and set handler._times = [...] keep working. No other code reads _times: git grep -n "_times\b" src finds only these sites.

Tests

New tests/test_base_handler_timings.py::test_handler_keeps_only_the_latest_timing_not_one_per_output runs a minimal BaseHandler subclass through run() with inputs that emit 5,000 outputs and then 3. It checks that every output is forwarded in order, that _times holds at most one entry, and that last_time is still readable.

On unmodified main (6644d4f):

>       assert len(handler._times) <= 1
E       assert 5003 <= 1

With the change it passes.

Local gate (darwin/arm64, Python 3.12.16, uv sync --group dev, the steps in ci.yml):

  • ruff check src/ tests/, ruff format --check src/ tests/ and mypy src/ are clean.

  • pytest tests/ -q: 4191 passed, 11 skipped, 0 failed (main at 6644d4f: 4190 passed, 11 skipped; the difference is the new test).
  • 🤖 Generated with Claude Code

    Diff: 301 lines (+225 / -76) across 3 files Reviewer: andimarafioti Review duration: 3864m
    All Vouch review confidence checks passed
    Residual risk 0.24 = Change Risk (0.45) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 301 lines diff across 3 files
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Shallow Review Depth shallow_review_depth
    0.344
    Diff Size diff_size
    0.342
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #948"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.1% (MEDIUM) · Higher = larger blast radius
    45.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 24% · Safe to merge
    24%