Files
CyberGril/web/tests/review878_late_cleanup.test.ts
T
xiaomoandmultica-agent 958c6962fb HEL-908 R1/R2 rework (HEL-909 review): whole-turn reasoning gate + cg.interrupt data channel
R1 — reasoning separation truly closed. The streaming hold leaked wholesale
on real token arrival (review A1: 'private reasoning '*20 beyond the 128-char
bound, no sentence ender; review A2: a Chinese sentence ender released the
hold) AND the adapter consumed the stray closer, so the app-level turn split
could never recover — the previous 'persistence and receipt always clean'
claim was false and is retracted. Per the engineering ruling the adapter now
has two explicit endpoint modes (GOV-006, no heuristics, no new engine):
buffer_turn (TurnReasoningGate) emits NOTHING during generation, splits the
whole turn at stream end and only then delivers the clean visible text; it
is set explicitly for all tiers in config/models.yaml (first visible text
waits for the full generation; cancelled/timed-out turns emit nothing and
end exactly as before — recorded and test-pinned, low latency NOT claimed).
stream (default, backward compatible) keeps explicit-tag/field semantics and
passes stray closers through unmodified so the turn-level split keeps its
signal. Both review strings and the recorded id=12 stream replay clean at
every chunk size through the real adapter and the real app into chat.delta,
the archive and the receipt; normal zh/en preserved byte-for-byte; unclosed
explicit thinking still fails honestly.

R2 — an authoritative interrupt when the LLM already finalized and TTS is
still playing (review evidence C: zero interrupts, old audio played out).
The page's existing hush() entry (ChatPanel untouched) now also publishes
{"v":1} on the room's own reliable data channel topic cg.interrupt
(fire-and-forget: a dropped publish never blocks or breaks the text send);
the worker subscribes data_received, validates topic/sender/payload and
calls session.interrupt(force=True) — the SDK's own programmatic interrupt,
force confirmed against the installed 1.8.8 source (non-force raises
RuntimeError on allow_interruptions=False speech). Failures log loudly; the
listener is unloaded via add_shutdown_callback; the server-cancel path in
llm_chat_api also interrupts with force and logs failures.

Verification (offline, zero real requests): real-SDK harness 8/8 including a
real SpeechHandle with a genuinely pending playout wait released by the
cg.interrupt message after a clean chat.final (evidence-C shape) plus the
full validation matrix; .venv311 full suite 480 passed / 8 skipped (2
test_r3 failures pre-exist at 5625de6); web npm ci from the existing lock,
typecheck 0 errors, lint 0 errors + 1 pre-existing warning, vitest 148/148,
build ok; ruff clean; check_requirements vs origin/main and b8ec832 with the
corrected handoff.

Co-authored-by: multica-agent <github@multica.ai>
2026-10-28 22:21:16 +08:00

45 lines
1.9 KiB
TypeScript

import { expect, test } from 'vitest';
import { VoiceController, type RoomEvents, type VoiceView } from '../src/voice/session';
test('old room disconnect cleanup cannot overwrite a newly opened call', async () => {
const pendingDeletes: (() => void)[] = [];
const releaseOld = () => pendingDeletes.splice(0).forEach(resolve => resolve());
let grant = 0;
const events: RoomEvents[] = [];
let view: VoiceView | undefined;
const controller = new VoiceController({
secure: () => true,
onView: v => { view = v; },
onRecord: () => {}, onSpoke: () => {},
agentWaitMs: 60000,
fetch: (async (url, init) => {
if (String(url).endsWith('/status')) return Response.json({ configured: true, tts_ok: true });
if (init?.method === 'POST') return Response.json({ session_id: `s${++grant}`, token: 'token', url: 'wss://fake.invalid' });
if (String(url).endsWith('/s1')) await new Promise<void>(resolve => { pendingDeletes.push(resolve); });
return Response.json({ ended: true });
}) as typeof fetch,
transport: async () => ({
capture: async () => ({ stop() {}, onEnded() {} }),
join: async (_url, _token, callbacks) => {
events.push(callbacks);
return { publish: async () => {}, close: async () => {}, hush() {}, interrupt() {}, resumeAudio: async () => {} };
},
}),
});
try {
await controller.start();
events[0].agentJoined();
events[0].dropped(); // DELETE s1 is deliberately held in flight.
await controller.start();
events[1].agentJoined();
expect(view?.phase).toBe('listening');
releaseOld();
await new Promise(resolve => setTimeout(resolve, 0));
expect(controller.active).toBe(true);
expect(view?.phase).toBe('listening');
} finally {
releaseOld();
await controller.end();
}
});