- asr_usage.py (stdlib-only): append-only JSONL ledger, per-call/window audio seconds counted from actually-sent frames (sample-rate aware, resend counted), connect attempt/failure/retry semantics (normal per-utterance reconnects are not retries), fail-closed default (real cap=0 unless a window is armed), pre-send blocking so no byte crosses the cap, ledger replay for restart recovery and cross-window isolation, resumable summarize CLI - stt_funasr.py: guard hooks on the real send path (check_connect before open_utt, precheck before send, count after send, recv-side death recorded, per-utterance flush aggregation); usage=None keeps prior behavior - worker.py: entrypoint builds guard from env per job, room name as call id, on_exhausted -> ctx.shutdown ends the call, window summary in start log - tests: 17 deterministic offline tests incl. fake livekit/websockets SDK driving the real _run seam, sample-rate variants, silence/cancel/failure/ reconnect/repeated-end, cap rejection before crossing, ledger persistence across restart, room deletion keeps history, other windows unaffected Co-authored-by: multica-agent <github@multica.ai>
832 lines
33 KiB
Python
832 lines
33 KiB
Python
"""HEL-906 连续通话 ASR 实际用量计数与窗口闸的确定性离线测试.
|
||
|
||
两层验证(复审要求"接缝必须真的被专属 worker 调用,不能只有独立计数类
|
||
测试"):
|
||
|
||
1. 纯计数/闸/账本恢复(guard 单元):fail-closed 默认、按实际帧样本计时、
|
||
触线前拒绝、重试口径、落盘恢复、跨窗口隔离、崩溃少计有界。
|
||
2. 真实接缝:用假 livekit/websockets 模块(隔离假 provider、假帧)导入
|
||
真实的 voice/stt_funasr.py 与 voice/worker.py,驱动真实 ``_run`` 送数
|
||
路径——建连检查、发送前拒绝、发送后计数、接收侧死亡、流异常重试、
|
||
utterance 聚合落账全部走产品代码;worker entrypoint 布线(guard 注入、
|
||
房间名标识、触线 ctx.shutdown、未布防拒绝)逐项断言。
|
||
|
||
零真实 ASR/TTS/LiveKit 调用(fake 连接全部在进程内),零安装。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import asyncio
|
||
import enum
|
||
import json
|
||
import subprocess
|
||
import sys
|
||
import types
|
||
from pathlib import Path
|
||
from types import SimpleNamespace
|
||
|
||
import httpx
|
||
import pytest
|
||
|
||
from cybergril.voice.asr_usage import (
|
||
AsrUsageGuard,
|
||
AsrWindowClosed,
|
||
AsrWindowPolicy,
|
||
build_usage_from_env,
|
||
policy_from_env,
|
||
summarize_ledger,
|
||
)
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# fake livekit / websockets SDK(只满足本仓库代码用到的表面,进程内假 provider)
|
||
# ---------------------------------------------------------------------------
|
||
|
||
_FAKE_MODULES = (
|
||
"livekit",
|
||
"livekit.rtc",
|
||
"livekit.agents",
|
||
"livekit.agents.stt",
|
||
"livekit.agents.llm",
|
||
"livekit.agents.tts",
|
||
"livekit.agents.types",
|
||
"livekit.plugins",
|
||
"livekit.plugins.silero",
|
||
"websockets",
|
||
)
|
||
|
||
|
||
class FakeFrame:
|
||
def __init__(self, data: bytes, sample_rate: int, samples_per_channel: int) -> None:
|
||
self.data = data
|
||
self.sample_rate = sample_rate
|
||
self.samples_per_channel = samples_per_channel
|
||
|
||
|
||
class FakeResampler:
|
||
"""线性比例重采样替身:输出帧数与输入样本数严格成比例(16bit/单声道)."""
|
||
|
||
def __init__(self, in_rate: int, out_rate: int) -> None:
|
||
self.out_rate = out_rate
|
||
self.ratio = out_rate / in_rate
|
||
self._in_samples = 0
|
||
self._out_samples = 0
|
||
|
||
def push(self, frame: FakeFrame) -> list[FakeFrame]:
|
||
self._in_samples += frame.samples_per_channel
|
||
target = round(self._in_samples * self.ratio)
|
||
n = target - self._out_samples
|
||
if n <= 0:
|
||
return []
|
||
self._out_samples = target
|
||
return [FakeFrame(b"\x00" * (n * 2), self.out_rate, n)]
|
||
|
||
|
||
class FakeWS:
|
||
"""进程内 FunASR websocket 假 provider:记录每个出站字节."""
|
||
|
||
def __init__(self, server_msgs: list[str] | None = None, recv_fail: bool = False) -> None:
|
||
self.sent: list[bytes | str] = []
|
||
self.server_msgs = list(server_msgs or [])
|
||
self.recv_fail = recv_fail
|
||
self.closed = False
|
||
self.fail_send = False
|
||
|
||
async def send(self, payload: bytes | str) -> None:
|
||
if self.fail_send:
|
||
raise OSError("send refused")
|
||
self.sent.append(payload)
|
||
|
||
async def close(self) -> None:
|
||
self.closed = True
|
||
|
||
def __aiter__(self):
|
||
return self
|
||
|
||
async def __anext__(self):
|
||
while True:
|
||
if self.closed:
|
||
raise StopAsyncIteration
|
||
if self.recv_fail:
|
||
raise ConnectionError("recv died")
|
||
if self.server_msgs:
|
||
return self.server_msgs.pop(0)
|
||
await asyncio.sleep(0.005)
|
||
|
||
|
||
class FakeWebsockets:
|
||
"""websockets.connect 假 provider:可编程失败与假服务端响应."""
|
||
|
||
def __init__(self) -> None:
|
||
self.attempts = 0
|
||
self.fail_attempts: set[int] = set()
|
||
self.sockets: list[FakeWS] = []
|
||
self.ws_factory = None
|
||
|
||
def connect(self, url: str, max_size: int | None = None):
|
||
async def _connect() -> FakeWS:
|
||
self.attempts += 1
|
||
if self.attempts in self.fail_attempts:
|
||
raise OSError(f"connect refused (attempt {self.attempts})")
|
||
if self.ws_factory is not None:
|
||
ws = self.ws_factory()
|
||
else:
|
||
ws = FakeWS(server_msgs=[json.dumps(
|
||
{"mode": "2pass-offline", "text": "测试终稿"})])
|
||
self.sockets.append(ws)
|
||
return ws
|
||
|
||
return _connect()
|
||
|
||
def bytes_sent(self) -> list[bytes]:
|
||
return [p for ws in self.sockets for p in ws.sent if isinstance(p, bytes)]
|
||
|
||
|
||
class _InCh:
|
||
def __init__(self) -> None:
|
||
self.q: asyncio.Queue = asyncio.Queue()
|
||
|
||
def push(self, item) -> None:
|
||
self.q.put_nowait(item)
|
||
|
||
def end(self) -> None:
|
||
self.q.put_nowait(None)
|
||
|
||
def __aiter__(self):
|
||
return self
|
||
|
||
async def __anext__(self):
|
||
return await self.q.get()
|
||
|
||
|
||
class _OutCh:
|
||
def __init__(self) -> None:
|
||
self.q: asyncio.Queue = asyncio.Queue()
|
||
|
||
def send_nowait(self, item) -> None:
|
||
self.q.put_nowait(item)
|
||
|
||
def empty(self) -> bool:
|
||
return self.q.empty()
|
||
|
||
def get_nowait(self):
|
||
return self.q.get_nowait()
|
||
|
||
|
||
class SpeechEventType(enum.Enum):
|
||
START_OF_SPEECH = "start_of_speech"
|
||
INTERIM_TRANSCRIPT = "interim_transcript"
|
||
FINAL_TRANSCRIPT = "final_transcript"
|
||
END_OF_SPEECH = "end_of_speech"
|
||
|
||
|
||
class SpeechData:
|
||
def __init__(self, *, language: str, text: str) -> None:
|
||
self.language = language
|
||
self.text = text
|
||
|
||
|
||
class SpeechEvent:
|
||
def __init__(self, *, type, alternatives) -> None:
|
||
self.type = type
|
||
self.alternatives = alternatives
|
||
|
||
|
||
def _kwargs_ns(**kwargs):
|
||
return SimpleNamespace(**kwargs)
|
||
|
||
|
||
class FakeRecognizeStream:
|
||
class _FlushSentinel:
|
||
pass
|
||
|
||
def __init__(self, *, stt=None, conn_options=None) -> None:
|
||
self._stt = stt
|
||
self._conn_options = conn_options
|
||
self._input_ch = _InCh()
|
||
self._event_ch = _OutCh()
|
||
self._run_task: asyncio.Task | None = None
|
||
|
||
def _ensure_run(self) -> asyncio.Task:
|
||
if self._run_task is None:
|
||
self._run_task = asyncio.get_running_loop().create_task(self._run())
|
||
return self._run_task
|
||
|
||
def __aiter__(self):
|
||
self._ensure_run()
|
||
return self
|
||
|
||
async def __anext__(self):
|
||
self._ensure_run()
|
||
while True:
|
||
if not self._event_ch.empty():
|
||
return self._event_ch.get_nowait()
|
||
task = self._run_task
|
||
if task.done():
|
||
exc = task.exception()
|
||
if exc is not None:
|
||
raise exc
|
||
raise StopAsyncIteration
|
||
await asyncio.sleep(0.005)
|
||
|
||
|
||
class FakeAgentSession:
|
||
last: FakeAgentSession | None = None # 测试替身的类级记录
|
||
|
||
def __init__(self, **kwargs) -> None:
|
||
self.kwargs = kwargs
|
||
self.handlers: dict[str, list] = {}
|
||
self.started = None
|
||
FakeAgentSession.last = self
|
||
|
||
def on(self, ev: str):
|
||
def deco(fn):
|
||
self.handlers.setdefault(ev, []).append(fn)
|
||
return fn
|
||
|
||
return deco
|
||
|
||
async def start(self, *, room=None, agent=None) -> None:
|
||
self.started = (room, agent)
|
||
|
||
|
||
class FakeCtx:
|
||
"""JobContext 替身:记录 shutdown 调用."""
|
||
|
||
def __init__(self, room_name: str) -> None:
|
||
self.room = SimpleNamespace(
|
||
name=room_name,
|
||
local_participant=SimpleNamespace(
|
||
publish_data=self._publish_data),
|
||
)
|
||
self.proc = SimpleNamespace(userdata={"vad": object()})
|
||
self.shutdown_calls: list[str] = []
|
||
|
||
async def _publish_data(self, data, topic=None, reliable=False) -> None:
|
||
return None
|
||
|
||
async def connect(self) -> None:
|
||
return None
|
||
|
||
def shutdown(self, reason: str = "", message: str = "") -> None:
|
||
self.shutdown_calls.append(reason or message or "shutdown")
|
||
|
||
|
||
class _VAD:
|
||
@classmethod
|
||
def load(cls):
|
||
return cls()
|
||
|
||
|
||
def _install_fake_sdk(monkeypatch) -> SimpleNamespace:
|
||
rtc_mod = types.ModuleType("livekit.rtc")
|
||
rtc_mod.AudioFrame = FakeFrame
|
||
rtc_mod.AudioResampler = FakeResampler
|
||
|
||
stt_mod = types.ModuleType("livekit.agents.stt")
|
||
stt_mod.STT = type("STT", (), {"__init__": lambda self, *, capabilities=None: setattr(
|
||
self, "capabilities", capabilities)})
|
||
stt_mod.STTCapabilities = _kwargs_ns
|
||
stt_mod.SpeechData = SpeechData
|
||
stt_mod.SpeechEvent = SpeechEvent
|
||
stt_mod.SpeechEventType = SpeechEventType
|
||
stt_mod.RecognizeStream = FakeRecognizeStream
|
||
|
||
llm_mod = types.ModuleType("livekit.agents.llm")
|
||
for name in ("LLM", "ChatChunk", "ChatContext", "ChoiceDelta", "LLMStream", "Tool"):
|
||
setattr(llm_mod, name, type(name, (), {"__init__": lambda self, *a, **k: None}))
|
||
|
||
tts_mod = types.ModuleType("livekit.agents.tts")
|
||
tts_mod.TTS = type("TTS", (), {"__init__": lambda self, *, capabilities=None,
|
||
sample_rate=None, num_channels=None: None})
|
||
tts_mod.TTSCapabilities = _kwargs_ns
|
||
for name in ("AudioEmitter", "ChunkedStream"):
|
||
setattr(tts_mod, name, type(name, (), {}))
|
||
|
||
types_mod = types.ModuleType("livekit.agents.types")
|
||
types_mod.APIConnectOptions = type("APIConnectOptions", (), {})
|
||
types_mod.DEFAULT_API_CONNECT_OPTIONS = types_mod.APIConnectOptions()
|
||
types_mod.NOT_GIVEN = object()
|
||
types_mod.NotGivenOr = object
|
||
|
||
agents_mod = types.ModuleType("livekit.agents")
|
||
agents_mod.Agent = type("Agent", (), {"__init__": lambda self, *a, **k: None})
|
||
agents_mod.AgentSession = FakeAgentSession
|
||
for name in ("JobContext", "JobProcess"):
|
||
setattr(agents_mod, name, type(name, (), {}))
|
||
agents_mod.WorkerOptions = type("WorkerOptions", (), {"__init__": lambda self, **k: None})
|
||
agents_mod.cli = type("cli", (), {"run_app": staticmethod(lambda *a, **k: None)})
|
||
agents_mod.APIConnectionError = type("APIConnectionError", (Exception,), {})
|
||
agents_mod.APIStatusError = type("APIStatusError", (Exception,), {})
|
||
agents_mod.stt = stt_mod
|
||
agents_mod.llm = llm_mod
|
||
agents_mod.tts = tts_mod
|
||
agents_mod.types = types_mod
|
||
|
||
silero_mod = types.ModuleType("livekit.plugins.silero")
|
||
silero_mod.VAD = _VAD
|
||
plugins_mod = types.ModuleType("livekit.plugins")
|
||
plugins_mod.silero = silero_mod
|
||
|
||
livekit_mod = types.ModuleType("livekit")
|
||
livekit_mod.rtc = rtc_mod
|
||
livekit_mod.agents = agents_mod
|
||
livekit_mod.plugins = plugins_mod
|
||
|
||
websockets_mod = types.ModuleType("websockets")
|
||
fake_ws_module = FakeWebsockets()
|
||
websockets_mod.connect = fake_ws_module.connect
|
||
|
||
for name, mod in (
|
||
("livekit", livekit_mod),
|
||
("livekit.rtc", rtc_mod),
|
||
("livekit.agents", agents_mod),
|
||
("livekit.agents.stt", stt_mod),
|
||
("livekit.agents.llm", llm_mod),
|
||
("livekit.agents.tts", tts_mod),
|
||
("livekit.agents.types", types_mod),
|
||
("livekit.plugins", plugins_mod),
|
||
("livekit.plugins.silero", silero_mod),
|
||
("websockets", websockets_mod),
|
||
):
|
||
monkeypatch.setitem(sys.modules, name, mod)
|
||
for cached in ("cybergril.voice.stt_funasr", "cybergril.voice.worker",
|
||
"cybergril.voice.tts_cosyvoice", "cybergril.voice.llm_chat_api"):
|
||
monkeypatch.delitem(sys.modules, cached, raising=False)
|
||
return SimpleNamespace(websockets=fake_ws_module, rtc=rtc_mod, stt=stt_mod)
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# helpers
|
||
# ---------------------------------------------------------------------------
|
||
|
||
def make_policy(**overrides) -> AsrWindowPolicy:
|
||
values = dict(window_id="w-test", max_audio_seconds=1000.0,
|
||
max_connect_retries_per_call=6, flush_seconds=5.0,
|
||
resync_seconds=1e9)
|
||
values.update(overrides)
|
||
return AsrWindowPolicy(**values)
|
||
|
||
|
||
def make_guard(tmp_path: Path, *, on_exhausted=None, **overrides) -> AsrUsageGuard:
|
||
return AsrUsageGuard(make_policy(**overrides), tmp_path / "asr-usage.jsonl",
|
||
on_exhausted=on_exhausted)
|
||
|
||
|
||
def frame_16k(n_samples: int = 160) -> FakeFrame:
|
||
return FakeFrame(b"\x00" * (n_samples * 2), 16000, n_samples)
|
||
|
||
|
||
def frame_48k(n_samples: int = 480) -> FakeFrame:
|
||
return FakeFrame(b"\x00" * (n_samples * 2), 48000, n_samples)
|
||
|
||
|
||
def read_records(tmp_path: Path) -> list[dict]:
|
||
path = tmp_path / "asr-usage.jsonl"
|
||
if not path.exists():
|
||
return []
|
||
return [json.loads(line) for line in path.read_text(encoding="utf-8").splitlines()
|
||
if line.strip()]
|
||
|
||
|
||
async def drive(module, stt, script, *, timeout: float = 10.0):
|
||
"""按脚本驱动真实 RecognizeStream(帧/flush/None=结束),等待跑完."""
|
||
stream = stt.stream()
|
||
task = stream._ensure_run()
|
||
for item in script:
|
||
if item is None:
|
||
stream._input_ch.end()
|
||
elif isinstance(item, str) and item == "flush":
|
||
stream._input_ch.push(module.RecognizeStream._FlushSentinel())
|
||
else:
|
||
stream._input_ch.push(item)
|
||
await asyncio.sleep(0)
|
||
await asyncio.wait_for(task, timeout=timeout)
|
||
return stream
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# 1) guard 单元:计数口径、fail-closed、触线、恢复、隔离
|
||
# ---------------------------------------------------------------------------
|
||
|
||
def test_policy_env_armed_and_fail_closed_default():
|
||
assert policy_from_env({}).armed is False # 未布防 = 真实 cap=0
|
||
policy = policy_from_env({
|
||
"CYBERGRIL_ASR_WINDOW_ID": "hel902-w1",
|
||
"CYBERGRIL_ASR_WINDOW_MAX_AUDIO_SECONDS": "2700",
|
||
"CYBERGRIL_ASR_MAX_CONNECT_RETRIES_PER_CALL": "6",
|
||
})
|
||
assert policy.armed
|
||
assert policy.max_audio_seconds == 2700.0
|
||
assert policy.max_connect_retries_per_call == 6
|
||
# 配置损坏按 fail-closed(0)处理,不放宽
|
||
bad = policy_from_env({"CYBERGRIL_ASR_WINDOW_ID": "w",
|
||
"CYBERGRIL_ASR_WINDOW_MAX_AUDIO_SECONDS": "abc"})
|
||
assert bad.max_audio_seconds == 0.0
|
||
|
||
|
||
def test_unarmed_guard_blocks_everything_and_records(tmp_path):
|
||
guard = make_guard(tmp_path, window_id="")
|
||
with pytest.raises(AsrWindowClosed):
|
||
guard.begin_stream("room-a")
|
||
with pytest.raises(AsrWindowClosed):
|
||
guard.check_connect("room-a")
|
||
with pytest.raises(AsrWindowClosed):
|
||
guard.precheck_audio("room-a", 16000, 160)
|
||
blocks = [r for r in read_records(tmp_path) if r["kind"] == "block"]
|
||
assert {(b["what"], b["reason"]) for b in blocks} == {
|
||
("new_stream", "window-not-armed"),
|
||
("new_connection", "window-not-armed"),
|
||
("audio", "window-not-armed"),
|
||
}
|
||
|
||
|
||
def test_guard_audio_counted_by_actual_frame_samples(tmp_path):
|
||
guard = make_guard(tmp_path)
|
||
call = guard.begin_stream("room-a")
|
||
guard.check_connect(call)
|
||
guard.note_connect_result(call, ok=True)
|
||
# 16k 直接路径:160 样本 = 0.01s;重采样路径按输出帧 16k 计
|
||
for _ in range(10):
|
||
guard.precheck_audio(call, 16000, 160)
|
||
guard.note_audio_sent(call, 16000, 160, 320)
|
||
for _ in range(5):
|
||
guard.precheck_audio(call, 16000, 160)
|
||
guard.note_audio_sent(call, 16000, 160, 320) # 重发按重复计
|
||
guard.stream_end(call, "closed")
|
||
assert guard.calls[call].audio_seconds == pytest.approx(0.15)
|
||
audio = [r for r in read_records(tmp_path) if r["kind"] == "audio"]
|
||
assert sum(r["seconds"] for r in audio) == pytest.approx(0.15)
|
||
|
||
|
||
def test_guard_blocks_crossing_frame_before_send(tmp_path):
|
||
stops: list[str] = []
|
||
guard = make_guard(tmp_path, max_audio_seconds=2.0, on_exhausted=stops.append)
|
||
call = guard.begin_stream("room-a")
|
||
guard.check_connect(call)
|
||
guard.note_connect_result(call, ok=True)
|
||
guard.precheck_audio(call, 16000, 16000) # 1.0s
|
||
guard.note_audio_sent(call, 16000, 16000, 32000)
|
||
guard.flush_audio(call)
|
||
guard.precheck_audio(call, 16000, 16000) # 恰好等于上限:允许(≤上限)
|
||
guard.note_audio_sent(call, 16000, 16000, 32000)
|
||
guard.flush_audio(call)
|
||
with pytest.raises(AsrWindowClosed): # 第 3 秒会在发送前拒绝
|
||
guard.precheck_audio(call, 16000, 16000)
|
||
assert stops == ["window-audio-exhausted"] # 回调恰好一次
|
||
# 同窗新通也立即拒绝(新连接/新音频都不放行)
|
||
with pytest.raises(AsrWindowClosed):
|
||
guard.begin_stream("room-b")
|
||
blocks = [r for r in read_records(tmp_path) if r["kind"] == "block"]
|
||
assert {b["reason"] for b in blocks} == {"window-audio-exhausted"}
|
||
assert guard.window_audio_seconds == pytest.approx(2.0) # 一个字节没多
|
||
|
||
|
||
def test_guard_retry_semantics_only_after_failure(tmp_path):
|
||
guard = make_guard(tmp_path, max_connect_retries_per_call=2)
|
||
call = guard.begin_stream("room-a")
|
||
|
||
guard.check_connect(call) # 初次建连不是 retry
|
||
guard.note_connect_result(call, ok=True)
|
||
guard.check_connect(call) # 正常说完一轮后的下一次建连也不是 retry
|
||
guard.note_connect_result(call, ok=True)
|
||
assert guard.calls[call].connect_retries == 0
|
||
|
||
guard.check_connect(call) # 失败 → retry_pending
|
||
guard.note_connect_result(call, ok=False, error="OSError: refused")
|
||
guard.check_connect(call) # 失败后的再建连 = retry #1
|
||
guard.note_connect_result(call, ok=True)
|
||
guard.check_connect(call) # 再次失败 → retry_pending
|
||
guard.note_connect_result(call, ok=False, error="OSError: refused")
|
||
guard.check_connect(call) # retry #2(用尽上限)
|
||
guard.note_connect_result(call, ok=True)
|
||
cu = guard.calls[call]
|
||
assert (cu.connect_attempts, cu.connect_failures, cu.connect_retries) == (6, 2, 2)
|
||
|
||
guard.check_connect(call) # 上次成功 → 非 retry:不受 retry 上限约束
|
||
guard.note_connect_result(call, ok=True)
|
||
guard.check_connect(call) # 失败 → 置 retry_pending
|
||
guard.note_connect_result(call, ok=False, error="OSError: refused")
|
||
with pytest.raises(AsrWindowClosed): # retry #3 在建连前被拒
|
||
guard.check_connect(call)
|
||
cu = guard.calls[call]
|
||
assert (cu.connect_attempts, cu.connect_failures, cu.connect_retries) == (8, 3, 2)
|
||
|
||
# 新的一通有独立 retry 预算(每通口径)
|
||
call2 = guard.begin_stream("room-b")
|
||
guard.check_connect(call2)
|
||
guard.note_connect_result(call2, ok=True)
|
||
assert guard.calls["room-b"].connect_attempts == 1
|
||
|
||
|
||
def test_guard_ledger_recovery_same_window_no_reset(tmp_path):
|
||
guard1 = make_guard(tmp_path, max_audio_seconds=3.0)
|
||
call = guard1.begin_stream("room-a")
|
||
guard1.check_connect(call)
|
||
guard1.note_connect_result(call, ok=True)
|
||
guard1.check_connect(call)
|
||
guard1.note_connect_result(call, ok=False, error="x")
|
||
for _ in range(150):
|
||
guard1.precheck_audio(call, 16000, 160)
|
||
guard1.note_audio_sent(call, 16000, 160, 320)
|
||
guard1.flush_audio(call)
|
||
guard1.stream_end(call, "closed")
|
||
# 模拟进程重启:同窗口同账本重建 guard,预算与逐通历史都在
|
||
guard2 = make_guard(tmp_path, max_audio_seconds=3.0)
|
||
assert guard2.window_audio_seconds == pytest.approx(1.5)
|
||
cu = guard2.calls["room-a"]
|
||
assert (cu.connect_attempts, cu.connect_failures, cu.connect_retries) == (2, 1, 0)
|
||
assert cu.audio_seconds == pytest.approx(1.5)
|
||
assert cu.retry_pending is True # 最后一次连接事实是失败
|
||
# 耗尽同窗口剩余预算后拒绝;重开同窗口 id 不能重置
|
||
call = guard2.begin_stream("room-a")
|
||
guard2.check_connect(call)
|
||
guard2.note_connect_result(call, ok=True) # retry_pending=True → 这是 retry
|
||
with pytest.raises(AsrWindowClosed):
|
||
for _ in range(200):
|
||
guard2.precheck_audio(call, 16000, 160)
|
||
guard2.note_audio_sent(call, 16000, 160, 320)
|
||
guard2.flush_audio(call)
|
||
|
||
|
||
def test_guard_window_isolation_does_not_stop_others(tmp_path):
|
||
"""W1 耗尽不误停别的窗口/会话(一次性窗口各有独立预算)."""
|
||
g1 = make_guard(tmp_path, window_id="w1", max_audio_seconds=0.05)
|
||
with pytest.raises(AsrWindowClosed):
|
||
call = g1.begin_stream("room-a")
|
||
g1.check_connect(call)
|
||
g1.note_connect_result(call, ok=True)
|
||
for _ in range(10):
|
||
g1.precheck_audio(call, 16000, 160)
|
||
g1.note_audio_sent(call, 16000, 160, 320)
|
||
g2 = make_guard(tmp_path, window_id="w2", max_audio_seconds=100.0)
|
||
call = g2.begin_stream("room-a") # 同账本、不同窗口:完全不受 W1 影响
|
||
g2.check_connect(call)
|
||
g2.note_connect_result(call, ok=True)
|
||
for _ in range(10):
|
||
g2.precheck_audio(call, 16000, 160)
|
||
g2.note_audio_sent(call, 16000, 160, 320)
|
||
g2.stream_end(call, "closed")
|
||
assert g2.calls["room-a"].audio_seconds == pytest.approx(0.1)
|
||
|
||
|
||
def test_guard_crash_undercount_bounded_by_flush(tmp_path):
|
||
"""如实边界:崩溃前 ≤flush 阈值的已发音频未落账(有界少计)."""
|
||
guard = make_guard(tmp_path, flush_seconds=5.0)
|
||
call = guard.begin_stream("room-a")
|
||
guard.check_connect(call)
|
||
guard.note_connect_result(call, ok=True)
|
||
for _ in range(30):
|
||
guard.precheck_audio(call, 16000, 160)
|
||
guard.note_audio_sent(call, 16000, 160, 320) # 0.3s < flush 阈值
|
||
# 模拟进程直接消失(没有 stream_end/flush)
|
||
replayed = summarize_ledger(tmp_path / "asr-usage.jsonl", "w-test")
|
||
assert replayed["windows"]["w-test"]["audio_seconds"] == 0.0
|
||
# 正常路径(flush/stream_end)计数完整落账
|
||
guard2 = make_guard(tmp_path, flush_seconds=5.0)
|
||
call = guard2.begin_stream("room-b")
|
||
guard2.note_audio_sent(call, 16000, 16000, 32000)
|
||
guard2.stream_end(call, "closed")
|
||
assert summarize_ledger(tmp_path / "asr-usage.jsonl", "w-test")["windows"][
|
||
"w-test"]["calls"]["room-b"]["audio_seconds"] == pytest.approx(1.0)
|
||
|
||
|
||
def test_summarize_cli_reproducible(tmp_path, monkeypatch):
|
||
guard = make_guard(tmp_path)
|
||
call = guard.begin_stream("room-a")
|
||
guard.check_connect(call)
|
||
guard.note_connect_result(call, ok=True)
|
||
guard.note_audio_sent(call, 16000, 16000, 32000)
|
||
guard.stream_end(call, "closed")
|
||
repo_root = Path(__file__).resolve().parents[1]
|
||
proc = subprocess.run(
|
||
[sys.executable, "-m", "cybergril.voice.asr_usage",
|
||
str(tmp_path / "asr-usage.jsonl"), "--window", "w-test"],
|
||
capture_output=True, text=True, timeout=60,
|
||
env={"PYTHONPATH": str(repo_root / "src"), "PATH": "/usr/bin:/bin"},
|
||
check=True,
|
||
)
|
||
out = json.loads(proc.stdout)
|
||
call_summary = out["windows"]["w-test"]["calls"]["room-a"]
|
||
assert call_summary["audio_seconds"] == pytest.approx(1.0)
|
||
assert call_summary["connect_attempts"] == 1
|
||
assert out["parse_errors"] == 0
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# 2) 真实接缝:stt_funasr._run 在假 provider/假帧上走完整送数路径
|
||
# ---------------------------------------------------------------------------
|
||
|
||
@pytest.fixture
|
||
def fake_sdk(monkeypatch):
|
||
return _install_fake_sdk(monkeypatch)
|
||
|
||
|
||
async def test_seam_counts_fed_audio_both_sample_rates(fake_sdk, tmp_path):
|
||
import cybergril.voice.stt_funasr as funasr
|
||
|
||
guard = make_guard(tmp_path)
|
||
stt = funasr.FunASRSTT(usage=guard, call_label="cg-voice-abc")
|
||
await drive(funasr, stt, [frame_16k() for _ in range(30)] + ["flush"]
|
||
+ [frame_48k() for _ in range(30)] + [None])
|
||
cu = guard.calls["cg-voice-abc"]
|
||
# 16k 直接路径 0.3s + 48k→16k 重采样路径 0.3s(按实际输出帧计时)
|
||
assert cu.audio_seconds == pytest.approx(0.6)
|
||
assert cu.connect_attempts == 2 # 两轮 utterance 两次建连
|
||
assert (cu.connect_failures, cu.connect_retries) == (0, 0)
|
||
records = read_records(tmp_path)
|
||
audio_records = [r for r in records if r["kind"] == "audio"]
|
||
assert [round(r["seconds"], 6) for r in audio_records] == [0.3, 0.3] # utterance 边界聚合
|
||
assert all(r["call"] == "cg-voice-abc" for r in records)
|
||
ends = [r for r in records if r["kind"] == "call_end"]
|
||
assert [r["reason"] for r in ends] == ["closed"]
|
||
# 假 provider 侧:每帧恰好一次出站,cfg/is_speaking 各一条文本
|
||
audio_payloads = fake_sdk.websockets.bytes_sent()
|
||
assert len(audio_payloads) == 60
|
||
assert all(len(p) == 320 for p in audio_payloads)
|
||
|
||
|
||
async def test_seam_silence_and_cancel_counted(fake_sdk, tmp_path):
|
||
import cybergril.voice.stt_funasr as funasr
|
||
|
||
guard = make_guard(tmp_path)
|
||
stt = funasr.FunASRSTT(usage=guard, call_label="room-silent")
|
||
# 静音数据帧只要真实送出就计数;不 flush 直接结束(用户取消/挂断)
|
||
silence = FakeFrame(b"\x00" * 320, 16000, 160)
|
||
await drive(funasr, stt, [silence] * 20 + [None])
|
||
cu = guard.calls["room-silent"]
|
||
assert cu.audio_seconds == pytest.approx(0.2)
|
||
records = read_records(tmp_path)
|
||
assert [r["reason"] for r in records if r["kind"] == "call_end"] == ["closed"]
|
||
# 反复结束:第二通历史不覆盖第一通
|
||
stt2 = funasr.FunASRSTT(usage=guard, call_label="room-silent-2")
|
||
await drive(funasr, stt2, [frame_16k(1600)] + [None]) # 0.1s
|
||
assert guard.calls["room-silent-2"].audio_seconds == pytest.approx(0.1)
|
||
assert len([r for r in read_records(tmp_path)
|
||
if r["kind"] == "call_start"]) == 2
|
||
|
||
|
||
async def test_seam_connect_failure_marks_error_and_retry_denied(fake_sdk, tmp_path):
|
||
import cybergril.voice.stt_funasr as funasr
|
||
|
||
guard = make_guard(tmp_path, max_connect_retries_per_call=2)
|
||
stt = funasr.FunASRSTT(usage=guard, call_label="room-flaky")
|
||
fake_sdk.websockets.fail_attempts = {1, 3, 4}
|
||
|
||
with pytest.raises(OSError): # 初次建连失败 → 流异常结束
|
||
await drive(funasr, stt, [frame_16k(), None])
|
||
records = read_records(tmp_path)
|
||
assert records[-1]["kind"] == "call_end" and records[-1]["reason"] == "error"
|
||
|
||
stt2 = funasr.FunASRSTT(usage=guard, call_label="room-flaky")
|
||
await drive(funasr, stt2, [frame_16k(), "flush", None]) # retry#1 成功
|
||
assert guard.calls["room-flaky"].connect_retries == 1
|
||
|
||
stt3 = funasr.FunASRSTT(usage=guard, call_label="room-flaky")
|
||
with pytest.raises(OSError): # 上次成功后这次是普通建连,但连接失败
|
||
await drive(funasr, stt3, [frame_16k(), None])
|
||
|
||
stt4 = funasr.FunASRSTT(usage=guard, call_label="room-flaky")
|
||
with pytest.raises(OSError): # retry#2(用尽上限)且仍失败
|
||
await drive(funasr, stt4, [frame_16k(), None])
|
||
|
||
stt5 = funasr.FunASRSTT(usage=guard, call_label="room-flaky")
|
||
attempts_before = fake_sdk.websockets.attempts
|
||
with pytest.raises(AsrWindowClosed): # retry#3 建连前被拒:连假 provider 都不碰
|
||
await drive(funasr, stt5, [frame_16k(), None])
|
||
assert fake_sdk.websockets.attempts == attempts_before
|
||
blocks = [r for r in read_records(tmp_path) if r["kind"] == "block"]
|
||
assert blocks[-1]["reason"] == "connect-retries-exhausted"
|
||
cu = guard.calls["room-flaky"]
|
||
assert (cu.connect_attempts, cu.connect_failures, cu.connect_retries) == (4, 3, 2)
|
||
|
||
stt5 = funasr.FunASRSTT(usage=guard, call_label="room-flaky")
|
||
with pytest.raises(AsrWindowClosed): # 该通 retry 预算已用尽:再建连被拒
|
||
await drive(funasr, stt5, [frame_16k(), None])
|
||
# 新的一通有独立 retry 预算:两次正常 utterance 建连都放行
|
||
stt6 = funasr.FunASRSTT(usage=guard, call_label="room-flaky-2")
|
||
await drive(funasr, stt6, [frame_16k(), "flush", frame_16k(), "flush", None])
|
||
assert guard.calls["room-flaky-2"].connect_attempts == 2
|
||
assert guard.calls["room-flaky-2"].connect_retries == 0
|
||
|
||
|
||
async def test_seam_window_exhaustion_stops_before_crossing(fake_sdk, tmp_path):
|
||
import cybergril.voice.stt_funasr as funasr
|
||
|
||
stops: list[str] = []
|
||
guard = AsrUsageGuard(
|
||
make_policy(max_audio_seconds=0.05),
|
||
tmp_path / "asr-usage.jsonl", on_exhausted=stops.append)
|
||
stt = funasr.FunASRSTT(usage=guard, call_label="room-cap")
|
||
with pytest.raises(AsrWindowClosed):
|
||
await drive(funasr, stt, [frame_16k() for _ in range(6)] + [None])
|
||
# 0.05s = 5 帧放行,第 6 帧发送前拒绝:出站字节恰好 5 帧
|
||
assert len(fake_sdk.websockets.bytes_sent()) == 5
|
||
assert guard.calls["room-cap"].audio_seconds == pytest.approx(0.05)
|
||
assert stops == ["window-audio-exhausted"]
|
||
records = read_records(tmp_path)
|
||
assert records[-1]["kind"] == "call_end"
|
||
assert records[-1]["reason"] == "window_exhausted"
|
||
assert any(r["kind"] == "block" for r in records)
|
||
# 本窗口后续会话:新流在建流即被拒
|
||
stt2 = funasr.FunASRSTT(usage=guard, call_label="room-cap-2")
|
||
with pytest.raises(AsrWindowClosed):
|
||
await drive(funasr, stt2, [frame_16k(), None])
|
||
|
||
|
||
async def test_seam_recv_death_recorded_as_connection_died(fake_sdk, tmp_path):
|
||
import cybergril.voice.stt_funasr as funasr
|
||
|
||
guard = make_guard(tmp_path)
|
||
fake_sdk.websockets.ws_factory = lambda: FakeWS(recv_fail=True)
|
||
stt = funasr.FunASRSTT(usage=guard, call_label="room-dead")
|
||
await drive(funasr, stt, [frame_16k(), "flush", None], timeout=6)
|
||
records = read_records(tmp_path)
|
||
died = [r for r in records if r["kind"] == "connection_died"]
|
||
assert len(died) == 1 and died[0]["call"] == "room-dead"
|
||
assert guard.calls["room-dead"].connect_failures == 1
|
||
assert guard.calls["room-dead"].retry_pending is True
|
||
# 死亡后的下一次建连计为 retry(接收侧死亡未伴随建连失败也如实计数)
|
||
fake_sdk.websockets.ws_factory = None
|
||
stt2 = funasr.FunASRSTT(usage=guard, call_label="room-dead")
|
||
await drive(funasr, stt2, [frame_16k(), "flush", None])
|
||
assert guard.calls["room-dead"].connect_retries == 1
|
||
|
||
|
||
async def test_ledger_survives_room_deletion(fake_sdk, tmp_path):
|
||
"""删房成功会话账本移除该通;ASR 账本逐通历史原样保留."""
|
||
import cybergril.voice.stt_funasr as funasr
|
||
from cybergril.voice.session import VoiceConfig, VoiceSessionService
|
||
|
||
guard = make_guard(tmp_path)
|
||
stt = funasr.FunASRSTT(usage=guard, call_label="cg-voice-keep")
|
||
await drive(funasr, stt, [frame_16k(1600), "flush", None])
|
||
before = read_records(tmp_path)
|
||
|
||
service = VoiceSessionService(
|
||
config=VoiceConfig(livekit_url="ws://v.test", livekit_internal="http://v.test",
|
||
api_key="devkey", api_secret="s"),
|
||
http=httpx.AsyncClient(transport=httpx.MockTransport(_fake_twirp)))
|
||
session = await service.open_session(epoch=1)
|
||
ended = await service.end_session(session.session_id)
|
||
assert ended is not None
|
||
assert service.get(session.session_id) is None # 会话账本已移除
|
||
assert read_records(tmp_path) == before # ASR 账本一字未动
|
||
await service.aclose()
|
||
|
||
|
||
def _fake_twirp(request: httpx.Request) -> httpx.Response:
|
||
if request.url.path.endswith("CreateDispatch"):
|
||
return httpx.Response(200, json={"id": "AD_1"})
|
||
return httpx.Response(200, content=b"")
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# 3) worker 接线:entrypoint 真的构造并注入 guard,触线真的关 job
|
||
# ---------------------------------------------------------------------------
|
||
|
||
async def test_worker_entrypoint_wires_guard(fake_sdk, tmp_path, monkeypatch, capsys):
|
||
import cybergril.voice.worker as worker
|
||
|
||
ledger = tmp_path / "worker-asr.jsonl"
|
||
env = {"CYBERGRIL_ASR_WINDOW_ID": "hel902-w1",
|
||
"CYBERGRIL_ASR_WINDOW_MAX_AUDIO_SECONDS": "2700",
|
||
"CYBERGRIL_ASR_MAX_CONNECT_RETRIES_PER_CALL": "6",
|
||
"CYBERGRIL_ASR_USAGE_LEDGER": str(ledger)}
|
||
monkeypatch.setattr(worker, "build_usage_from_env",
|
||
lambda on_exhausted=None: build_usage_from_env(
|
||
env, on_exhausted=on_exhausted, ledger_path=ledger))
|
||
ctx = FakeCtx("cg-voice-room1")
|
||
await worker.entrypoint(ctx)
|
||
captured = FakeAgentSession.last
|
||
assert captured is not None and captured.started is not None
|
||
stt_obj = captured.kwargs["stt"]
|
||
assert stt_obj._call_label == "cg-voice-room1" # 房间名 = 每通标识
|
||
guard = stt_obj._usage
|
||
assert isinstance(guard, AsrUsageGuard)
|
||
assert guard.policy.window_id == "hel902-w1"
|
||
out = capsys.readouterr().out
|
||
assert "asr_window=" in out and "hel902-w1" in out
|
||
# 触线回调真的接到 ctx.shutdown:预检越限帧 → 结束本通 job
|
||
call = guard.begin_stream("cg-voice-room1")
|
||
guard.note_audio_sent(call, 16000, 16000, 32000)
|
||
guard.flush_audio(call)
|
||
with pytest.raises(AsrWindowClosed):
|
||
guard.precheck_audio(call, 16000, 16000 * 2700) # 会越 2700s 上限的帧
|
||
assert ctx.shutdown_calls, "on_exhausted 必须真的调用 ctx.shutdown 结束本通"
|
||
|
||
|
||
async def test_worker_entrypoint_default_is_fail_closed(fake_sdk, monkeypatch, capsys):
|
||
import cybergril.voice.worker as worker
|
||
|
||
monkeypatch.setattr(
|
||
worker, "build_usage_from_env",
|
||
lambda on_exhausted=None: build_usage_from_env({}, on_exhausted=on_exhausted,
|
||
ledger_path=None))
|
||
ctx = FakeCtx("cg-voice-room2")
|
||
await worker.entrypoint(ctx)
|
||
guard = FakeAgentSession.last.kwargs["stt"]._usage
|
||
assert guard.policy.armed is False # 未布防 = 真实 cap=0
|
||
with pytest.raises(AsrWindowClosed):
|
||
guard.begin_stream("cg-voice-room2")
|
||
assert "asr_window=" in capsys.readouterr().out
|