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
+22 -1
View File
@@ -107,6 +107,14 @@ Content-Type: audio/wav (或 application/octet-stream)
"音色登记服务这会儿不在,稍后再试。";登记成功但列表未出现 `422`
"登记结果没有出现在音色列表里,请重试。"(不留下半条可选项)。
**共享登记成功、本地元数据写不进**(HEL-904 补):`503` 中文文案如实说明
编号与恢复路径——声音**已在共享注册表登记成功(不回滚)**,本地记录写不
进时列表里暂时只显示编号;**刷新列表即可按编号找回并直接试听/应用,不
需要重新登记**(重复登记只会另立新编号、留下无元数据孤儿条目)。响应
断连重试同样会另立新 `cg-` 编号并在共享注册表留下以编号显示的孤儿条目
(可正常使用,回收经 8090 面板自有删除入口,CyberGril 不代管)——实际
行为如实声明,不自动删除。
**建议录音格式**(前端 MediaRecorder 录完后转):WAV(PCM),16k 或 24k 采样,
单声道,16 位,5–20 秒,安静环境念 1–2 句完整的话。API 端校验是
"合法 WAV + ≥2 秒";时长上限由 15MB 大小线自然约束。
@@ -147,6 +155,16 @@ POST /api/voice/selection {"voice": "cg-9f2c1a77"}
- 生效时机:worker 在会话入口读一次音色 → **通话中改选最迟下一通话生效**,
绝不半句换人;`POST /api/voice/session` 的响应新增 `"voice"` 字段 =
本次通话捕获的有效音色(选择已失效则诚实回落 `"default"`)。
- **通话窗口持有**(HEL-904 收口 capture 与 worker 实读之间的竞态):
worker 是另一进程、读时点对 API 不可知(dispatch 返回 ≠ 已读),因此
开房 capture 把捕获值登记为通话持有,整个通话窗口(capture→挂断/到期)
都按「worker 尚未读」对待:期间 `POST /api/voice/selection` 只更新选择、
**暂缓写 worker 状态文件**,文件定在本通话捕获值上——会话响应的
`voice` 与 worker 本通话实际读到并使用的声音一致;挂断/到期/开房失败
或取消即结算持有,之后的应用立即写透。重叠通话属单用户产品的退化
场景:较新通话的 capture 会重写文件,较早通话若其 worker 迟迟未读,
可能读到较新捕获值(每个 worker 仍整通话单一音色,无半句换人)——
如实记录,不宣称完全消除。
## 3. 状态与资源(可核算、可恢复)
@@ -162,7 +180,10 @@ CyberGril 资料空间(实例数据库旁,与语音清理账同模式,重
每次记 `register_asr` 流水;试听每次记 `preview_tts`。LLM 调用零新增。
- 备份清单:`GET /api/voice/voices` 的 custom 项即完整清单(参考音频路径 +
参考文本 + 映射);恢复 = 按清单重新登记(面板无导出接口,属 R6 备份卡范围)。
- worker 文件 `voice_state_cybergril.json`:原子写且**保留文件里其他键**;
- worker 文件 `voice_state_cybergril.json`:原子写且在文件**可解析(JSON
对象)时保留其他键**;文件读不出(损坏/非 JSON/非对象)时会**整体重写**
为仅含 `voice` 的新状态,旧键此时无法保留(HEL-902 审核 §4 核出的实况,
不得宣称任何情况下都保留其他键;正常可读文件其他键原样保留)。
用户从未经 API 选择时,通话开启不代写该文件(部署/D1 域不替用户定)。
## 4. 部署说明(本卡未执行;总工按 HEL-877 runbook §4 顺序)