diff --git a/scripts/voice-latency.py b/scripts/voice-latency.py new file mode 100644 index 0000000..110ce85 --- /dev/null +++ b/scripts/voice-latency.py @@ -0,0 +1,238 @@ +#!/usr/bin/env python3 +"""Break down voice-gateway latency from the run trace. + +Reads `data/runs.jsonl` (written by the running gateway) and, for each spoken +interaction, reports the time between the **last word spoken** and the **first +reply audio** split by section: + + last word ── endpointing ── STT ── adapter ── [scheduler: guards + act + assess] + ── reply routing (humanize) ── TTS synth ── playback start + +The voice transport emits `voice_speech_end` at the last speech frame and +`voice_playback_start` right before the first reply samples reach the device; +the scheduler emits `submit`/`actionability`/`category_scope`/`intent_guard`/ +`assessed`/`ran`; the service emits `humanize`/`gateway_reply_queued` and the +adapter `gateway_reply_dequeued`/`voice_speak_start`/`voice_tts_synth`. + +Usage: + python scripts/voice-latency.py [data/runs.jsonl] [--json] [--decisions PATH] +""" + +from __future__ import annotations + +import argparse +import json +import statistics +import sys +from pathlib import Path + + +def load_rows(path: str) -> list[dict]: + rows = [] + if not Path(path).is_file(): + return rows + with open(path) as handle: + for line in handle: + line = line.strip() + if line: + rows.append(json.loads(line)) + return rows + + +def _first(events: list[dict], kind: str) -> dict | None: + return next((e for e in events if e.get("kind") == kind), None) + + +def _ts(event: dict | None) -> float | None: + return None if event is None else event.get("ts") + + +def build_interactions(rows: list[dict]) -> list[dict]: + """Split the trace into spoken interactions, each from speech-end to reply-end.""" + interactions: list[dict] = [] + current: dict | None = None + for event in rows: + kind = event.get("kind") + if kind == "voice_speech_end" or ( + kind == "voice_utterance" and current is None + ): + # A new utterance begins; close any prior interaction at its reply + # end. `voice_utterance` also starts one on legacy traces that + # predate the `voice_speech_end` probe (last-word boundary unknown). + if current is not None: + interactions.append(current) + current = {"events": [event]} + elif current is not None: + current["events"].append(event) + if kind in ("voice_spoke", "voice_speak_failed", "voice_stt_empty", + "voice_stt_failed", "voice_short_utterance"): + interactions.append(current) + current = None + if current is not None: + interactions.append(current) + return interactions + + +def _decisions_timing(decisions_path: str) -> dict[str, list[dict]]: + """Map run_id -> list of {phase, timing} from the decision log.""" + by_run: dict[str, list[dict]] = {} + if not Path(decisions_path).is_file(): + return by_run + with open(decisions_path) as handle: + for line in handle: + line = line.strip() + if not line: + continue + row = json.loads(line) + extra = row.get("extra") or {} + run_id = extra.get("run_id") + timing = extra.get("timing") + if run_id and timing: + by_run.setdefault(run_id, []).append( + {"phase": extra.get("phase"), **timing} + ) + return by_run + + +def analyze(interaction: dict, decisions: dict[str, list[dict]]) -> dict | None: + events = interaction["events"] + speech_end = _first(events, "voice_speech_end") + utterance = _first(events, "voice_utterance") + transcribed = _first(events, "voice_transcribed") + submit = _first(events, "submit") + if utterance is None or submit is None: + return None # empty/short utterance: nothing was dispatched + # On legacy traces without `voice_speech_end`, measure from the utterance. + origin = speech_end or utterance + + actionability = _first(events, "actionability") + category_scope = _first(events, "category_scope") + intent_guard = _first(events, "intent_guard") + assessed = _first(events, "assessed") + ran = _first(events, "ran") + reply_queued = _first(events, "gateway_reply_queued") + reply_dequeued = _first(events, "gateway_reply_dequeued") + speak_start = _first(events, "voice_speak_start") + tts = _first(events, "voice_tts_synth") + playback = _first(events, "voice_playback_start") + spoke = _first(events, "voice_spoke") + humanize = _first(events, "humanize") + + t0 = _ts(origin) + run_id = submit.get("run_id") + + def delta(a: dict | None, b: dict | None) -> float | None: + ta, tb = _ts(a), _ts(b) + return None if ta is None or tb is None else round(tb - ta, 3) + + out = { + "run_id": run_id, + "text": submit.get("text"), + "skill": (ran or {}).get("skill"), + "summary": (ran or {}).get("summary"), + "last_word_ts": t0, + "endpointing_s": delta(speech_end, utterance), + "stt_s": delta(utterance, transcribed), + "adapter_s": delta(transcribed, submit), + "scheduler_s": delta(submit, ran), + "guard_actionability_s": delta(submit, actionability), + "guard_category_s": delta(actionability, category_scope), + "guard_intent_s": delta(category_scope, intent_guard), + "act_assess_s": delta(intent_guard, assessed), + "assess_ran_s": delta(assessed, ran), + "reply_s": delta(ran, playback), + "humanize_s": (humanize or {}).get("elapsed_s"), + "tts_synth_s": (tts or {}).get("synth_s"), + "tts_audio_s": (tts or {}).get("audio_s"), + "queue_s": delta(reply_queued, reply_dequeued), + "speak_to_playback_s": delta(speak_start, playback), + "playback_s": delta(playback, spoke), + "total_to_first_audio_s": delta(origin, playback), + "total_to_reply_end_s": delta(origin, spoke), + "decisions": decisions.get(run_id, []), + } + # Anything after `ran` but before playback that is not humanize/TTS is queue. + if out["reply_s"] is not None: + accounted = (out["humanize_s"] or 0.0) + (out["tts_synth_s"] or 0.0) + out["reply_unaccounted_s"] = round(out["reply_s"] - accounted, 3) + return out + + +def fmt(value) -> str: + return "—" if value is None else f"{value:.2f}" + + +def render(results: list[dict]) -> None: + for r in results: + print(f"\nrun {r['run_id']} {r['text']!r} skill={r['skill']}") + print(f" last word → endpointing done : {fmt(r['endpointing_s'])} s") + print(f" STT : {fmt(r['stt_s'])} s") + print(f" adapter → scheduler submit : {fmt(r['adapter_s'])} s") + print(f" SCHEDULER total : {fmt(r['scheduler_s'])} s") + print(f" actionability guard : {fmt(r['guard_actionability_s'])} s") + print(f" category softmax + scope : {fmt(r['guard_category_s'])} s") + print(f" leaf softmax + intent : {fmt(r['guard_intent_s'])} s") + print(f" act + assess:outcome : {fmt(r['act_assess_s'])} s") + print(f" assess → ran : {fmt(r['assess_ran_s'])} s") + print(f" reply (ran → first audio) : {fmt(r['reply_s'])} s") + print(f" humanize : {fmt(r['humanize_s'])} s") + print(f" TTS synth : {fmt(r['tts_synth_s'])} s") + print(f" other (queue/open) : {fmt(r.get('reply_unaccounted_s'))} s") + print(f" playback (audio duration) : {fmt(r['playback_s'])} s") + print(f" ── LAST WORD → FIRST AUDIO : {fmt(r['total_to_first_audio_s'])} s") + if r["decisions"]: + for d in r["decisions"]: + print( + f" [{d.get('phase')}] tokens={d.get('input_tokens')} " + f"forward={d.get('forward_seconds')} total={d.get('total_seconds')}" + ) + + def agg(key: str) -> str: + vals = [r[key] for r in results if r.get(key) is not None] + if not vals: + return "—" + return f"median {statistics.median(vals):.2f}s mean {statistics.mean(vals):.2f}s (n={len(vals)})" + + print("\n===== aggregate =====") + for key, label in ( + ("endpointing_s", "endpointing"), + ("stt_s", "STT"), + ("scheduler_s", "SCHEDULER"), + ("guard_actionability_s", " actionability"), + ("guard_category_s", " category softmax+scope"), + ("guard_intent_s", " leaf softmax+intent"), + ("act_assess_s", " act+assess"), + ("reply_s", "reply (humanize+TTS)"), + ("humanize_s", " humanize"), + ("tts_synth_s", " TTS synth"), + ("total_to_first_audio_s", "TOTAL last word → first audio"), + ): + print(f"{label:28s}: {agg(key)}") + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("trace", nargs="?", default="data/runs.jsonl") + parser.add_argument("--decisions", default="data/decisions.jsonl") + parser.add_argument("--json", action="store_true") + args = parser.parse_args(argv) + + rows = load_rows(args.trace) + decisions = _decisions_timing(args.decisions) + results = [] + for interaction in build_interactions(rows): + analyzed = analyze(interaction, decisions) + if analyzed is not None: + results.append(analyzed) + if args.json: + print(json.dumps(results, indent=2)) + return 0 + if not results: + print("no spoken interactions found in", args.trace) + return 1 + render(results) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/semif_agent/gateway/service.py b/semif_agent/gateway/service.py index 4d58c1d..9dc1f50 100644 --- a/semif_agent/gateway/service.py +++ b/semif_agent/gateway/service.py @@ -24,6 +24,7 @@ from __future__ import annotations import queue import re import threading +import time from .base import InboundMessage, OutboundMessage from .humanize import Humanizer @@ -426,6 +427,12 @@ class GatewayService: # ---- outbound ---- + def _trace(self, kind: str, **fields) -> None: + """Best-effort trace (a test double's scheduler may have no trace log).""" + trace = getattr(self.scheduler, "trace", None) + if trace is not None: + trace.append(kind, "?", **fields) + def _reply( self, platform: str, chat_id: str, text: str, kind: str = "reply" ) -> None: @@ -442,7 +449,13 @@ class GatewayService: # result before optionally cleaning it up. text = _result_detail(text) if humanize: + started = time.time() text = self.humanizer.humanize(text) + self._trace( + "humanize", + elapsed_s=round(time.time() - started, 3), + chars=len(text), + ) prefix = str(self._platform_cfg.get(platform, {}).get("reply_prefix", "") or "") if prefix: text = f"{prefix}{text}" @@ -451,4 +464,5 @@ class GatewayService: target_queue = self.outbound.get(platform) if target_queue is None: return + self._trace("gateway_reply_queued", reply_kind=kind, chars=len(text)) target_queue.put(OutboundMessage(chat_id=str(chat_id), text=text)) diff --git a/semif_agent/gateway/voice.py b/semif_agent/gateway/voice.py index a6701a3..ad6d437 100644 --- a/semif_agent/gateway/voice.py +++ b/semif_agent/gateway/voice.py @@ -65,6 +65,7 @@ class VoiceAdapter(GatewayAdapter): if message is None: self.daemon.stop() return + self.daemon._event("gateway_reply_dequeued", chars=len(message.text)) self.daemon.speak(message.text) threading.Thread(target=_pump, name="voice-outbound-pump", daemon=True).start() diff --git a/semif_agent/log.py b/semif_agent/log.py index 2072bac..811e80d 100644 --- a/semif_agent/log.py +++ b/semif_agent/log.py @@ -42,6 +42,14 @@ class DecisionLog: } if extra: row["extra"] = extra + result_extra = getattr(result, "extra", None) or {} + timing = { + key: result_extra[key] + for key in ("input_tokens", "forward_seconds", "total_seconds") + if key in result_extra + } + if timing: + row.setdefault("extra", {})["timing"] = timing self.path.parent.mkdir(parents=True, exist_ok=True) with self.path.open("a") as handle: handle.write(json.dumps(row) + "\n") diff --git a/semif_agent/trace.py b/semif_agent/trace.py index b23da71..e66a7d8 100644 --- a/semif_agent/trace.py +++ b/semif_agent/trace.py @@ -19,8 +19,10 @@ class TraceLog: def __init__(self, path: str = "data/runs.jsonl"): self.path = Path(path) - def append(self, kind: str, run_id: str, **fields) -> None: - row = {"kind": kind, "run_id": run_id, "ts": time.time(), **fields} + 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") diff --git a/semif_agent/voice_transport.py b/semif_agent/voice_transport.py index 34969c0..65ae642 100644 --- a/semif_agent/voice_transport.py +++ b/semif_agent/voice_transport.py @@ -719,6 +719,9 @@ class VoiceDaemon: speech_ms = self.frame_ms silence_ms = 0 started = time.time() + #: Wall-clock of the most recent frame the VAD called speech — the + #: "last word spoken" boundary the latency breakdown measures from. + last_speech_ts = started while not self._stop.is_set(): if self._speaking.is_set(): break # a reply started; stop capturing so we don't transcribe it @@ -731,6 +734,7 @@ class VoiceDaemon: if engines.vad.is_speech(pcm, self.sample_rate): speech_ms += self.frame_ms silence_ms = 0 + last_speech_ts = time.time() else: silence_ms += self.frame_ms if silence_ms >= self.vad_silence_ms: @@ -743,6 +747,7 @@ class VoiceDaemon: silence_ms=silence_ms, ) return b"" + self._event("voice_speech_end", ts=last_speech_ts, speech_ms=speech_ms) self._event("voice_utterance", frames=len(frames), speech_ms=speech_ms) return b"".join(frames) @@ -776,11 +781,23 @@ class VoiceDaemon: text = (clipped or text[: self.max_speak_chars]) + "…" engines = self._ensure_engines() self._speaking.set() + self._event("voice_speak_start", chars=len(text)) try: + synth_started = time.time() pcm, sample_rate = engines.tts.synthesize(text) + self._event( + "voice_tts_synth", + synth_s=round(time.time() - synth_started, 3), + chars=len(text), + audio_s=round(len(pcm) / BYTES_PER_SAMPLE / sample_rate, 3) + if pcm and sample_rate + else 0.0, + ) if pcm: with self._audio_lock: engines.audio.open_output(sample_rate) + # The instant the first reply audio is handed to the device. + self._event("voice_playback_start", ts=time.time(), chars=len(text)) engines.audio.write(pcm, sample_rate) self._event("voice_spoke", chars=len(text)) except Exception as exc: @@ -800,6 +817,6 @@ class VoiceDaemon: # ---- tracing ---- - def _event(self, kind: str, **fields) -> None: + def _event(self, kind: str, ts: float | None = None, **fields) -> None: if self.trace is not None: - self.trace.append(kind, "?", **fields) + self.trace.append(kind, "?", ts=ts, **fields)