Files
CyberGril/docs/rework/hel879/voice-seam-interfaces.md
T
zonggongandmultica-agent 3228b1e0d7 HEL-879 remaining closure: auto-expiry cleanup ownership, exact SDK speech-turn attribution, and the versioned v2 subtitle contract for HEL-898
A. Room lifecycle (总管 review items A-1/A-2): the single cleanup owner now watches
   ACTIVE session deadlines as well as debts - a session nobody touches expires into
   the ledger and has its room really deleted with zero further requests (old code:
   _cleanup_loop only scanned debts, expired rooms were never deleted until some
   request ran _prune). Explicit end and the background sweep share ONE persisted
   attempt/age cap and abandoned state - _sessions no longer bypasses it (the
   manager measured 2 background attempts + 3 more interactive deletes = 5); an
   abandoned debt raises CleanupAbandoned -> 503 with room/attempts/reason instead
   of pretending done-404, success clears both _sessions and _debts, restart
   continues the same persisted cap, close/cancel leaves no dangling coroutine, and
   only this instance's own rooms are ever touched. New wake event makes a fresh
   debt retry immediately instead of waiting out a deadline sleep.

B. Queued speech turns bind EXACTLY (总管 interleaving note_handle(H1)->H2->
   note_stream(S1)->S2 gave H1=None, H2='turn-one'): CyberGrillLLM.chat now reads
   the SDK's own agents_speech_handle ContextVar (livekit-agents 1.8.8 sets it in
   _create_speech_task around every speech pipeline; source-verified on .12 and
   asserted at runtime) so each ChatApiLLMStream carries speech_handle/speech_id of
   the very handle whose generation created it. TurnLedger binds only on that exact
   attribution - a newer speech NEVER retires an older pending handle, say/proactive
   generations bind nobody, missing attribution fails closed (no ack, no subtitle
   turn), interrupt/failure discards only its own binding.

C. Versioned subtitle contract v2 for the visual author's wiring card (HEL-898):
   every cg.subtitle payload gains {v:2, seq} (session-monotonic), turn identity
   (speech_id, turn_id, same-source message_id for proactive rows) and explicit
   lifecycle events - turn_start/assistant_interim/assistant_final/turn_end/
   turn_cancel(reason) plus proactive_start/proactive_end/proactive_cancel that
   ride the say SpeechHandle instead of the receipt branch. Accumulators are
   isolated per speech_id (a late delta after cancel is dropped producer-side);
   seq + speech_id give the frontend two independent stale-rejection rules. Full
   executable contract: docs/voice-subtitle-contract-v2.md with recorded samples in
   docs/rework/hel879/subtitle-samples/ (scripts/gen_subtitle_samples_hel879.py).
   web/src untouched.

Verification: the .12 real-SDK check now LOADS THE ACTUAL WORKER MODULE and drives
the callbacks IT registered on a real AgentSession (no hand-copied functions) -
both-queued delayed generations bind exactly, a say sandwiched between user turns
steals nothing, unattributed generations fail closed, interrupted turns cancel
without final/receipt (11/11 ALL PASS, source-level proof of the ContextVar wiring
included; evidence docs/rework/hel879/sdk-check-hel879-remaining.log). The old r2
lifespan test flake is root-caused (clock freeze and block were two separate portal
calls; a 10ms life tick could slip between them and claim the frozen day unblocked)
and fixed atomically without raising timeouts or dropping assertions - 12/12 solo,
full suite 350/350 three times in a row on .venv311; ruff back to the single
pre-existing error; voice chain 8/8; release offline R1-R7 with zero failures;
requirement checks 4/4 (origin/main and c121c42, each with/without handoff).
Zero real LLM/embed/ASR/TTS calls, zero downloads/installs, nothing deployed or
merged, real caps stay 0.

Co-authored-by: multica-agent <github@multica.ai>
2026-10-27 05:10:52 +08:00

88 lines
5.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# HEL-879 · 语音接缝接口与验收(交原视觉作者接线;总工不改已认可 web/src 视觉)
> 背景:总管独立复核指出「CG-011 语音主动开口 no-op、她的增量字幕缺失」是
> **实现缺口**,不能归为「等音色/预算就能自动完成」。本文件给出后台已落地的
> 最小接缝、前端需要补的**精确接口**与**验收标准**。代码基点:分支
> `hel-879-assembly`(见交付评论精确 head);后台实现
> `src/cybergril/voice/seam.py` + `worker.py` 接线。
>
> **2026-10-27 收口更新**:字幕消息已升级为**版本化契约 v2**(信封
> `v/seq`、确切轮次 `speech_id`、显式起止/取消事件、迟到/乱序保护、主动
> 播报同源 `message_id`)。完整可执行契约与录制样例见
> `docs/voice-subtitle-contract-v2.md` 与
> `docs/rework/hel879/subtitle-samples/*.jsonl`——**接线卡(HEL-898)以
> 该契约为准**,本文 A 节的 v1 语义作为兼容背景保留。
## A. 她的增量回复字幕(assistant_interim)
### 现状与缺口
- 用户侧 ASR 字幕已齐:`interim`(ASR 草稿)与 `user_final`(纠错终稿)在
`web/src/voice/session.ts` 已渲染(现有 `interim` 分支当「听到用户在说」处理,
**不能复用来渲染她的回复**——那是另一个语义)。
- 缺的是**她**的回复在 TTS 首包前/播放中的增量文字。后台现已补齐:worker 把
/api/chat SSE 的每个 delta 累积后发布到同一 `cg.subtitle` 通道。
### 后台已交付(无需前端改动即生效的数据)
- 数据通道:topic `cg.subtitle`(不变)。
- 新 kind:`assistant_interim`,`role: "assistant"`,`text` 为**截至当前的完整
回复文本**(替换语义,同用户 interim;发布时机=SSE 分片到达,先于/伴随 TTS)。
- 既有 kind 不变:`assistant_final`(已落库的完整回复,播放完成且未打断后发)、
`error`(φ 原句,未落库)、`interim`/`user_final`(用户侧)。
- fail-closed 语义不变:打断/失败/无回执的轮**只有 interim,不会有 final**——
前端必须把无 final 收尾的 interim 按「这轮没有成为她的正式回复」处理。
### 前端需补(原视觉作者;具体视觉由其定)
1. `web/src/voice/session.ts` 字幕分支:新增 `assistant_interim` 处理(当前
`if (s.kind === 'interim')` 只覆盖用户草稿)。
2. 渲染位置/样式:与 `assistant_final` 同一气泡位(建议替换式过渡),与用户
`heard` 视觉可区分;**不进入正式聊天记录**(只有 final 落库)。
3. 收尾规则:`assistant_final` 到达 → interim 消失、final 生效;`error` 到达 →
interim 消失、按现有 error 呈现;被打断(无任何收尾)→ 下一次 interim 或
新轮开始时清掉;挂断 → 清掉。
### 验收(离线可测,无需真实通话)
- [ ] 用假 LiveKit/假音频脚本回放 `cg.subtitle` 序列
`interim(user) → user_final → assistant_interim("她在") →
assistant_interim("她在打字中") → assistant_final("她在打字中。")`,
页面按序呈现且 interim 被 final 原子替换。
- [ ] 打断负例:`assistant_interim` 后直接新的 `interim(user)`(无 final/error),
她的 interim 残影不越过新用户轮。
- [ ] φ 负例:`assistant_interim` → `error`,error 文案呈现且不落正式记录。
- [ ] 全程零新增权限申请、零非回环出站(沿用 HEL-896 浏览器检查口径)。
## B. CG-011 语音主动开口(最小接缝,默认关闭)
### 后台已交付
- `worker.py _maybe_open_proactive`(真实代码路径,`CYBERGRIL_PROACTIVE_VOICE=1`
才启用;**默认关**,等接线卡与呈现批准):
- 数据来源:与文字收件箱同一 `GET /api/messages`(`ChatApiClient.recent_messages`),
只选**服务端已落库**(`proactive=1`、`role=companion`、`status=sent`、非空、
本房间未念过)的行,最新优先,每会话 ≤1 条(`PROACTIVE_PER_SESSION`)。
- 播报:`session.say(text, allow_interruptions=True)`——可被用户打断;
无新模型调用、无第二人格、**无第二条回执通道**(文字回执规则独立且已存在)。
- 失败 fail-closed:接缝任何异常只记日志跳过,不影响会话开启。
- 选择逻辑独立在 `seam.py pick_proactive/pending_proactive`(离线可测,
`tests/test_voice_rework_hel879.py` 三例覆盖)。
### 前端/接线卡需定(原视觉作者 + 总管,经用户批准后启用)
1. 入口呈现:她主动开口时页面如何呈现(字幕走 `assistant_final` 同位?
是否复用留言卡「回她」入口语义?未读标记如何联动——**worker 不代标已读**,
阅读态仍归页面)。
2. 节奏:会话开启后何时念(现在=开启即查一次、每会话最多一条);是否需要在
会话中轮询新 proactive(后续卡,不默记完成)。
3. 打断呈现:用户开口打断她的主动播报时,字幕按打断负例处理(无 final 收尾)。
### 验收(启用后)
- [ ] 预置一条落库 proactive 行 → 开语音会话 → 她原文念出该行(与文字收件箱
同一行、一字不差),字幕按定稿呈现。
- [ ] 播报中用户开口 → 立即被打断、无残音;该行不被标记已读(除非用户在页面
读它)、无回执伪造。
- [ ] 无 proactive 行 → 会话正常开启,静默。
- [ ] API 不可达 → 接缝跳过,语音会话不受影响。
## 边界
- 本文件只交接口与验收;**总工未改任何已认可 web/src 视觉**(本卡 web/src 仅
勘正 session.ts 一处无证据注释,无逻辑/样式改动,diff 见交付评论)。
- R5 真机验收(≥3 轮、1 秒打断、停麦无残音、可信 HTTPS/WSS、D1 音色、有限
预算)仍待主卡集中批准,接缝通过≠R5 完成。