HEL-904: backend rework — recoverable register-failure mapping, worker-state corruption docstring, call-window voice pin closes capture/worker race

- register(): local metadata write failure -> LocalRecordFailed -> 503 Chinese
  message with the cg- id (shared registration NOT rolled back, refresh list to
  recover, never re-register); register_asr journaled BEFORE local persist so
  failed registrations still count in the resource ledger
- write_worker_voice(): docstring now states the whole-file rewrite on
  unreadable/corrupt state (other keys preserved only when parseable)
- capture->worker race closed with a session-immutable-snapshot equivalent:
  _capture_voice holds the captured voice (no await gap); apply_selection
  defers its worker-state write while any call is live; open_session releases
  the hold on failure/cancel; VoiceSessionService.on_session_settled releases
  on explicit end, end-into-debt and TTL expiry; response voice == the voice
  the worker actually reads for that call, mid-call changes affect the next
  call only; overlapping-call residual documented honestly
- tests: hel902 fault-injection case 1 rewritten for the new contract
  (recovery + restart + 409 gate kept), 4 new deterministic Event-gated race
  tests (worker read delayed past dispatch, concurrent apply, failure/cancel
  cleanup, next-call voice), zero sleep-based assertions
- docs: contract §2.2/§2.5/§3 updated; r5 followup package tightened (head
  milestones d167ba9/b09a907/pending-review, register ASR vs call ASR split,
  call-ASR counting means + honest pending blocker, single-timebase frame
  silence measurement, D1 female_soft reference audio pinned by sha256,
  recycling checklist, shared-TTS snapshot/restore, git-revert is not
  recycling, caps count failures/retries/outbound-no-response)
- evidence: pytest 380/380 (376 baseline + 4), verify chain 12/12, ruff clean,
  requirement checks 4/4 (origin/main and b09a907, with/without handoff)

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
施工员2号
2026-10-28 01:09:19 +08:00
co-authored by multica-agent
parent b09a907747
commit 07dda2bfa2
9 changed files with 687 additions and 53 deletions
+19
View File
@@ -222,6 +222,12 @@ class VoiceSessionService:
cleanup_max_attempts: int = CLEANUP_MAX_ATTEMPTS
cleanup_backoff_seconds: float = CLEANUP_BACKOFF_SECONDS
cleanup_grace_seconds: float = CLEANUP_GRACE_SECONDS
# HEL-904: 可选回调 ``Callable[[VoiceSession], None]``。一个会话离开
# 活跃表(显式挂断、TTL 到期、或转入清理欠账)即「结算」:该通话
# worker 的入口读窗口关闭。router 用它释放自定义音色的通话持有
# (capture 与 worker 实读之间的竞态收口)。回调异常不结算失败拖垮
# 会话流程(持有多留一会儿只会让应用晚一拍写透,安全方向)。
on_session_settled: object = None
def __post_init__(self) -> None:
self._sessions: dict[str, VoiceSession] = {}
@@ -319,6 +325,16 @@ class VoiceSessionService:
return self._tts_health.voices
# -- lifecycle ----------------------------------------------------------
def _settle(self, session: VoiceSession) -> None:
"""A session left the active registry (ended, expired, or moved into
the cleanup ledger): its worker's entry-read window is closed. The
optional HEL-904 hook releases the custom-voice hold for that call."""
hook = self.on_session_settled
if hook is None:
return
# 见字段注释:持有释放失败只会让应用写透晚一拍,不破坏会话流程
with contextlib.suppress(Exception):
hook(session)
def _prune(self) -> None:
"""Expired registrations move into the cleanup ledger — never silently
dropped: their room stays owned until its bounded delete lands."""
@@ -326,6 +342,7 @@ class VoiceSessionService:
expired = [k for k, v in self._sessions.items() if v.expires_at < now]
for session_id in expired:
session = self._sessions.pop(session_id)
self._settle(session) # TTL 到期:入口读窗口关闭(HEL-904)
if session_id not in self._debts:
self._debts[session_id] = _CleanupDebt(session=session, first_debt_at=now)
if expired:
@@ -502,6 +519,7 @@ class VoiceSessionService:
# From now on the room lives in the ledger alone: _sessions no
# longer bypasses the shared cap on a later end call.
self._sessions.pop(session_id, None)
self._settle(session) # 已挂断(删房在欠账里补):入口读窗口关闭
if session_id not in self._debts:
self._debts[session_id] = debt
debt.attempts += 1
@@ -513,6 +531,7 @@ class VoiceSessionService:
self._kick_cleanup()
raise
self._sessions.pop(session_id, None)
self._settle(session) # 删房落定:本通话结算(HEL-904 释放音色持有)
self._debts.pop(session_id, None)
self._persist_state()
return session