fix(voice): stop backtick TTS crash and actually count error turns

Claude replies with markdown/backticks by default; MeloTTS's Korean text
normaliser has no entry for '`' and dies with KeyError: '`', so any reply
mentioning a command/code block crashed the whole voice turn (500 on
/api/voice-turn). Fix at the shared synth() choke point with
normalize_for_speech(), which flattens code fences/inline code/links/markdown
and guarantees no backtick reaches the worker — covering both the dashboard
voice turn and the Discord speak() bridge. Also add a PERSONA line asking the
model to avoid markdown (belt-and-suspenders; the code strip is the real fix).

errors_total never moved for turn-level failures: it was only bumped by
log("error") events, and the dashboard voice path calls turn.finish(error=...)
without logging. Emit one error-level log event from Turn.finish() when a turn
ends in error, so both the server counter and the browser SSE mirror stay
consistent, guarded to count at most once. Drop the now-redundant pipeline
log("error") to avoid double counting and remove the dead _publish stub.

Verified: raw backtick -> worker KeyError '`' reproduced; after fix real
MeloTTS synth of a backtick+fenced reply succeeds; /api/voice-turn returns 200
with a wav body on a backtick reply and errors_total stays 0, and an induced
synth failure returns 500 with errors_total incrementing to exactly 1. Full
suite 18 passed.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-08-18 23:29:13 +09:00
parent ee6f6b7f55
commit 356d1128fa
6 changed files with 119 additions and 7 deletions

View File

@@ -88,6 +88,7 @@ class Turn:
self.error = ""
self.total_ms = 0.0
self._steps: list[Step] = []
self._error_logged = False # count this turn's failure at most once
# -- recording API (called from the pipeline) ------------------------- #
def heard(self, text: str) -> None:
@@ -108,8 +109,19 @@ class Turn:
self.error = error
elif any(s.ok is False for s in self._steps):
self.status = "error"
if not self.error:
failed = next((s for s in self._steps if s.ok is False), None)
self.error = (failed.error if failed else "") or "step failed"
else:
self.status = "ok"
# A turn that ended in error must be reflected in errors_total. That
# counter is driven by error-level log events on BOTH the server
# (Monitor.log) and the browser (dashboard SSE handler), so emit one
# log event here rather than bumping a counter the client won't mirror.
# Guarded so the repeated _touch()/finish() calls can't double-count.
if self.status == "error" and not self._error_logged:
self._error_logged = True
self._monitor.log("error", f"대화 #{self.id} 실패: {self.error}")
self._touch()
# -- internal --------------------------------------------------------- #
@@ -185,11 +197,8 @@ class Monitor:
return t
def _publish(self, t: Turn) -> None:
# Recompute error total lazily on error transitions.
if t.status == "error":
with self._lock:
# errors_total counts turns that ended in error at most once
pass
# errors_total is bumped once when the turn transitions to error, inside
# Turn.finish() (via a log event), so this only streams the turn state.
self._broadcast({"type": "turn", "turn": t.to_dict()})
# -- snapshot / subscribe (read side, HTTP threads) ------------------- #