Add trace boundaries and timings so the last-word -> first-audio latency can be split by section: - voice_transport: emit `voice_speech_end` (the last VAD-speech frame, i.e. the true "last word"), `voice_speak_start`, `voice_tts_synth` (Piper duration + audio length), and `voice_playback_start` (the instant the first reply samples reach the device). - trace: `TraceLog.append` accepts an explicit `ts` so an event records when it happened, not when it was written. - gateway/voice: trace `gateway_reply_dequeued` at the outbound pump. - gateway/service: trace `humanize` (duration) and `gateway_reply_queued`; a best-effort `_trace` helper tolerates test doubles with no trace log. - log: persist the engine's per-decision `input_tokens`/`forward_seconds`/ `total_seconds` (already computed by the backend, previously discarded) into the decision row's `extra.timing`. scripts/voice-latency.py reads data/runs.jsonl and prints a per-interaction breakdown (endpointing, STT, each SemIf guard, act/assess, humanize, TTS, playback) plus aggregates.
46 lines
1.7 KiB
Python
46 lines
1.7 KiB
Python
"""Run lifecycle tracing: the events around each request's trip through the
|
|
scheduler.
|
|
|
|
Decision rows live in `decisions.jsonl` (SemIf-compatible). Lifecycle events
|
|
(submit, dropped, queued, dequeued, assessed, requeued, ...) live here in
|
|
`runs.jsonl`, keyed by `run_id` (the request id) so the dashboard can rebuild
|
|
each run's flow without touching the decision-log contract. Both files are
|
|
append-only; replays read them in order.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import time
|
|
from pathlib import Path
|
|
|
|
|
|
class TraceLog:
|
|
def __init__(self, path: str = "data/runs.jsonl"):
|
|
self.path = Path(path)
|
|
|
|
def append(self, kind: str, run_id: str, ts: float | None = None, **fields) -> None:
|
|
# `ts` lets a caller record the moment an event *happened* (e.g. the last
|
|
# speech frame of an utterance) rather than when the row was written.
|
|
row = {"kind": kind, "run_id": run_id, "ts": time.time() if ts is None else ts, **fields}
|
|
self.path.parent.mkdir(parents=True, exist_ok=True)
|
|
with self.path.open("a") as handle:
|
|
handle.write(json.dumps(row) + "\n")
|
|
|
|
def read(self) -> list[dict]:
|
|
if not self.path.is_file():
|
|
return []
|
|
rows = []
|
|
with self.path.open("r") as handle:
|
|
for line in handle:
|
|
line = line.strip()
|
|
if line:
|
|
rows.append(json.loads(line))
|
|
return rows
|
|
|
|
def runs(self) -> dict[str, list[dict]]:
|
|
"""Group events by run_id, preserving insertion order of first sighting."""
|
|
grouped: dict[str, list[dict]] = {}
|
|
for row in self.read():
|
|
grouped.setdefault(row.get("run_id", ""), []).append(row)
|
|
return grouped |