test_config.py 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. """Tests for configuration loading."""
  2. import importlib.util
  3. import tempfile
  4. import tomllib
  5. from pathlib import Path
  6. from stt_daemon.config import load_config
  7. REPO_ROOT = Path(__file__).resolve().parents[1]
  8. LAYA_REV = "970dc8c5f63d7b886a68409493f37d569424f933"
  9. def test_load_config_defaults():
  10. """Test that load_config returns defaults when no file exists."""
  11. with tempfile.TemporaryDirectory() as tmpdir:
  12. # Change to temp dir and load (no config.toml exists)
  13. import os
  14. old_cwd = os.getcwd()
  15. try:
  16. os.chdir(tmpdir)
  17. cfg = load_config()
  18. assert cfg.audio.backend == "parec"
  19. assert cfg.audio.device == "VirtualMicSink.monitor"
  20. assert cfg.audio.sample_rate == 16000
  21. assert cfg.vad.aggressiveness == 2
  22. assert cfg.whisper.model == "small"
  23. assert cfg.whisper.language is None
  24. assert cfg.output.stdout is True
  25. assert cfg.output.jsonl is False
  26. finally:
  27. os.chdir(old_cwd)
  28. def test_load_config_from_file():
  29. """Test loading config from a TOML file."""
  30. config_toml = """
  31. [audio]
  32. backend = "sounddevice"
  33. device = "hw:0"
  34. sample_rate = 44100
  35. [vad]
  36. aggressiveness = 3
  37. pre_roll_ms = 500
  38. [whisper]
  39. model = "base"
  40. device = "cuda"
  41. language = "es"
  42. [output]
  43. stdout = false
  44. jsonl = true
  45. session_dir = "logs"
  46. """
  47. with tempfile.TemporaryDirectory() as tmpdir:
  48. config_path = Path(tmpdir) / "config.toml"
  49. config_path.write_text(config_toml)
  50. cfg = load_config(str(config_path))
  51. assert cfg.audio.backend == "sounddevice"
  52. assert cfg.audio.device == "hw:0"
  53. assert cfg.audio.sample_rate == 44100
  54. assert cfg.vad.aggressiveness == 3
  55. assert cfg.vad.pre_roll_ms == 500
  56. assert cfg.whisper.model == "base"
  57. assert cfg.whisper.device == "cuda"
  58. assert cfg.whisper.language == "es"
  59. assert cfg.output.stdout is False
  60. assert cfg.output.jsonl is True
  61. assert cfg.output.session_dir == "logs"
  62. def test_language_normalization_auto():
  63. """Test that language 'auto' is normalized to None."""
  64. config_toml = """
  65. [whisper]
  66. language = "auto"
  67. """
  68. with tempfile.TemporaryDirectory() as tmpdir:
  69. config_path = Path(tmpdir) / "config.toml"
  70. config_path.write_text(config_toml)
  71. cfg = load_config(str(config_path))
  72. assert cfg.whisper.language is None
  73. def test_language_normalization_empty_string():
  74. """Test that language '' is normalized to None."""
  75. config_toml = """
  76. [whisper]
  77. language = ""
  78. """
  79. with tempfile.TemporaryDirectory() as tmpdir:
  80. config_path = Path(tmpdir) / "config.toml"
  81. config_path.write_text(config_toml)
  82. cfg = load_config(str(config_path))
  83. assert cfg.whisper.language is None
  84. def test_language_normalization_none():
  85. """Test that language 'none' is normalized to None."""
  86. config_toml = """
  87. [whisper]
  88. language = "none"
  89. """
  90. with tempfile.TemporaryDirectory() as tmpdir:
  91. config_path = Path(tmpdir) / "config.toml"
  92. config_path.write_text(config_toml)
  93. cfg = load_config(str(config_path))
  94. assert cfg.whisper.language is None
  95. def test_language_explicit_en():
  96. """Test that explicit language codes are preserved."""
  97. config_toml = """
  98. [whisper]
  99. language = "en"
  100. """
  101. with tempfile.TemporaryDirectory() as tmpdir:
  102. config_path = Path(tmpdir) / "config.toml"
  103. config_path.write_text(config_toml)
  104. cfg = load_config(str(config_path))
  105. assert cfg.whisper.language == "en"
  106. def test_partial_config():
  107. """Test that partial configs merge with defaults."""
  108. config_toml = """
  109. [audio]
  110. device = "custom_device"
  111. """
  112. with tempfile.TemporaryDirectory() as tmpdir:
  113. config_path = Path(tmpdir) / "config.toml"
  114. config_path.write_text(config_toml)
  115. cfg = load_config(str(config_path))
  116. # Overridden
  117. assert cfg.audio.device == "custom_device"
  118. # Defaults
  119. assert cfg.audio.backend == "parec"
  120. assert cfg.audio.sample_rate == 16000
  121. assert cfg.vad.aggressiveness == 2
  122. def test_chunking_and_confidence_fields():
  123. """New chunking/gate fields load from TOML."""
  124. config_toml = """
  125. [vad]
  126. chunk_pause_ms = 500
  127. soft_max_s = 8.0
  128. rms_floor = 0.01
  129. trailing_trim_ms = 150
  130. [whisper]
  131. no_speech_prob_max = 0.7
  132. avg_logprob_min = -0.8
  133. """
  134. with tempfile.TemporaryDirectory() as tmpdir:
  135. config_path = Path(tmpdir) / "config.toml"
  136. config_path.write_text(config_toml)
  137. cfg = load_config(str(config_path))
  138. assert cfg.vad.chunk_pause_ms == 500
  139. assert cfg.vad.soft_max_s == 8.0
  140. assert cfg.vad.rms_floor == 0.01
  141. assert cfg.vad.trailing_trim_ms == 150
  142. assert cfg.whisper.no_speech_prob_max == 0.7
  143. assert cfg.whisper.avg_logprob_min == -0.8
  144. def test_chunking_defaults():
  145. """New fields have sane defaults."""
  146. with tempfile.TemporaryDirectory() as tmpdir:
  147. import os
  148. old_cwd = os.getcwd()
  149. try:
  150. os.chdir(tmpdir)
  151. cfg = load_config()
  152. finally:
  153. os.chdir(old_cwd)
  154. assert cfg.vad.chunk_pause_ms == 350
  155. assert cfg.vad.soft_max_s == 10.0
  156. assert cfg.vad.rms_floor == 0.008
  157. assert cfg.vad.trailing_trim_ms == 200
  158. assert cfg.whisper.no_speech_prob_max == 0.6
  159. assert cfg.whisper.avg_logprob_min == -1.0
  160. def test_output_defaults_disable_audio_saving():
  161. with tempfile.TemporaryDirectory() as tmpdir:
  162. import os
  163. old_cwd = os.getcwd()
  164. try:
  165. os.chdir(tmpdir)
  166. cfg = load_config()
  167. finally:
  168. os.chdir(old_cwd)
  169. assert cfg.output.save_audio is False
  170. assert cfg.output.session_dir == "transcripts"
  171. assert cfg.output.session_gap_min == 5.0
  172. assert not hasattr(cfg.output, "jsonl_dir")
  173. assert not hasattr(cfg.output, "audio_dir")
  174. def test_output_save_audio_fields():
  175. config_toml = """
  176. [output]
  177. save_audio = true
  178. session_dir = "clips"
  179. session_gap_min = 12.0
  180. """
  181. with tempfile.TemporaryDirectory() as tmpdir:
  182. config_path = Path(tmpdir) / "config.toml"
  183. config_path.write_text(config_toml)
  184. cfg = load_config(str(config_path))
  185. assert cfg.output.save_audio is True
  186. assert cfg.output.session_dir == "clips"
  187. assert cfg.output.session_gap_min == 12.0
  188. def test_legacy_output_and_brief_keys_ignored():
  189. """Retired keys in a legacy TOML load without error and keep the new defaults."""
  190. config_toml = """
  191. [output]
  192. jsonl_dir = "logs"
  193. audio_dir = "clips"
  194. [brief]
  195. write = false
  196. dir = "briefing"
  197. """
  198. with tempfile.TemporaryDirectory() as tmpdir:
  199. config_path = Path(tmpdir) / "config.toml"
  200. config_path.write_text(config_toml)
  201. cfg = load_config(str(config_path))
  202. assert cfg.output.session_dir == "transcripts"
  203. assert cfg.output.session_gap_min == 5.0
  204. assert cfg.brief.write is False
  205. assert not hasattr(cfg.output, "jsonl_dir")
  206. assert not hasattr(cfg.output, "audio_dir")
  207. assert not hasattr(cfg.brief, "dir")
  208. assert not hasattr(cfg.brief, "filename")
  209. def test_daemon_defaults():
  210. with tempfile.TemporaryDirectory() as tmpdir:
  211. import os
  212. old_cwd = os.getcwd()
  213. try:
  214. os.chdir(tmpdir)
  215. cfg = load_config()
  216. finally:
  217. os.chdir(old_cwd)
  218. assert cfg.daemon.queue_size == 50
  219. assert cfg.daemon.backlog_warn_at == 5
  220. def test_daemon_config_fields():
  221. config_toml = """
  222. [daemon]
  223. queue_size = 20
  224. backlog_warn_at = 3
  225. """
  226. with tempfile.TemporaryDirectory() as tmpdir:
  227. config_path = Path(tmpdir) / "config.toml"
  228. config_path.write_text(config_toml)
  229. cfg = load_config(str(config_path))
  230. assert cfg.daemon.queue_size == 20
  231. assert cfg.daemon.backlog_warn_at == 3
  232. def test_unknown_sections_warn_and_are_ignored(caplog):
  233. """A typo'd section like [assitant] must warn, not silently use defaults."""
  234. config_toml = """
  235. [assitant]
  236. require_viewer = false
  237. """
  238. with tempfile.TemporaryDirectory() as tmpdir:
  239. config_path = Path(tmpdir) / "config.toml"
  240. config_path.write_text(config_toml)
  241. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  242. cfg = load_config(str(config_path))
  243. assert "assitant" in caplog.text
  244. assert cfg.assistant.require_viewer is True
  245. def test_cpu_threads_default_is_auto():
  246. """cpu_threads defaults to 0 (CTranslate2's automatic choice)."""
  247. with tempfile.TemporaryDirectory() as tmpdir:
  248. import os
  249. old_cwd = os.getcwd()
  250. try:
  251. os.chdir(tmpdir)
  252. cfg = load_config()
  253. finally:
  254. os.chdir(old_cwd)
  255. assert cfg.whisper.cpu_threads == 0
  256. def test_cpu_threads_config_field():
  257. config_toml = """
  258. [whisper]
  259. cpu_threads = 16
  260. """
  261. with tempfile.TemporaryDirectory() as tmpdir:
  262. config_path = Path(tmpdir) / "config.toml"
  263. config_path.write_text(config_toml)
  264. cfg = load_config(str(config_path))
  265. assert cfg.whisper.cpu_threads == 16
  266. def test_language_strategy_defaults_to_static():
  267. """language_strategy defaults to 'static'."""
  268. with tempfile.TemporaryDirectory() as tmpdir:
  269. import os
  270. old_cwd = os.getcwd()
  271. try:
  272. os.chdir(tmpdir)
  273. cfg = load_config()
  274. finally:
  275. os.chdir(old_cwd)
  276. assert cfg.whisper.language_strategy == "static"
  277. def test_language_strategy_sticky_is_parsed():
  278. """language_strategy 'sticky' is parsed correctly."""
  279. config_toml = """
  280. [whisper]
  281. language_strategy = "sticky"
  282. """
  283. with tempfile.TemporaryDirectory() as tmpdir:
  284. config_path = Path(tmpdir) / "config.toml"
  285. config_path.write_text(config_toml)
  286. cfg = load_config(str(config_path))
  287. assert cfg.whisper.language_strategy == "sticky"
  288. def test_unknown_language_strategy_falls_back_to_static(caplog):
  289. """An unknown language_strategy value falls back to 'static' with a warning."""
  290. config_toml = """
  291. [whisper]
  292. language_strategy = "bogus"
  293. """
  294. with tempfile.TemporaryDirectory() as tmpdir:
  295. config_path = Path(tmpdir) / "config.toml"
  296. config_path.write_text(config_toml)
  297. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  298. cfg = load_config(str(config_path))
  299. assert cfg.whisper.language_strategy == "static"
  300. assert "Unknown language_strategy" in caplog.text
  301. assert "bogus" in caplog.text
  302. def test_sticky_redetect_logprob_defaults_to_minus_0_8():
  303. """sticky_redetect_logprob defaults to -0.8."""
  304. with tempfile.TemporaryDirectory() as tmpdir:
  305. import os
  306. old_cwd = os.getcwd()
  307. try:
  308. os.chdir(tmpdir)
  309. cfg = load_config()
  310. finally:
  311. os.chdir(old_cwd)
  312. assert cfg.whisper.sticky_redetect_logprob == -0.8
  313. def test_sticky_redetect_logprob_is_parsed():
  314. """sticky_redetect_logprob is parsed from TOML."""
  315. config_toml = """
  316. [whisper]
  317. sticky_redetect_logprob = -0.5
  318. """
  319. with tempfile.TemporaryDirectory() as tmpdir:
  320. config_path = Path(tmpdir) / "config.toml"
  321. config_path.write_text(config_toml)
  322. cfg = load_config(str(config_path))
  323. assert cfg.whisper.sticky_redetect_logprob == -0.5
  324. def test_llm_context_and_compaction_defaults():
  325. with tempfile.TemporaryDirectory() as tmpdir:
  326. import os
  327. old_cwd = os.getcwd()
  328. try:
  329. os.chdir(tmpdir)
  330. cfg = load_config()
  331. finally:
  332. os.chdir(old_cwd)
  333. assert cfg.llm.max_context_tokens == 131072
  334. assert cfg.llm.compaction_safety_margin == 256
  335. assert cfg.llm.compaction_enabled is True
  336. assert cfg.llm.compaction_trigger_ratio == 0.8
  337. assert cfg.llm.compaction_keep_turns == 6
  338. assert cfg.llm.compaction_max_tokens == 512
  339. assert cfg.llm.compaction_timeout_s == 30.0
  340. assert cfg.llm.compaction_input_tokens == 8192
  341. assert cfg.llm.inference_queue_size == 8
  342. assert cfg.llm.prompt_budget == 131072 - 1024 - 256
  343. assert not hasattr(cfg.llm, "max_prompt_tokens")
  344. def test_llm_context_and_compaction_fields_parsed():
  345. config_toml = """
  346. [llm]
  347. max_context_tokens = 8192
  348. max_tokens = 2048
  349. compaction_safety_margin = 128
  350. compaction_enabled = false
  351. compaction_trigger_ratio = 0.5
  352. compaction_keep_turns = 2
  353. compaction_max_tokens = 256
  354. compaction_timeout_s = 12.5
  355. compaction_input_tokens = 4096
  356. inference_queue_size = 3
  357. """
  358. with tempfile.TemporaryDirectory() as tmpdir:
  359. config_path = Path(tmpdir) / "config.toml"
  360. config_path.write_text(config_toml)
  361. cfg = load_config(str(config_path))
  362. assert cfg.llm.max_context_tokens == 8192
  363. assert cfg.llm.compaction_safety_margin == 128
  364. assert cfg.llm.compaction_enabled is False
  365. assert cfg.llm.compaction_trigger_ratio == 0.5
  366. assert cfg.llm.compaction_keep_turns == 2
  367. assert cfg.llm.compaction_max_tokens == 256
  368. assert cfg.llm.compaction_timeout_s == 12.5
  369. assert cfg.llm.compaction_input_tokens == 4096
  370. assert cfg.llm.inference_queue_size == 3
  371. assert cfg.llm.prompt_budget == 8192 - 2048 - 128
  372. def test_max_prompt_tokens_is_deprecated_and_ignored(caplog):
  373. config_toml = """
  374. [llm]
  375. max_prompt_tokens = 50
  376. """
  377. with tempfile.TemporaryDirectory() as tmpdir:
  378. config_path = Path(tmpdir) / "config.toml"
  379. config_path.write_text(config_toml)
  380. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  381. cfg = load_config(str(config_path))
  382. assert "max_prompt_tokens" in caplog.text
  383. assert "deprecated" in caplog.text
  384. assert cfg.llm.prompt_budget == 131072 - 1024 - 256
  385. def test_backend_defaults_to_faster_whisper():
  386. """backend defaults to 'faster-whisper'."""
  387. with tempfile.TemporaryDirectory() as tmpdir:
  388. import os
  389. old_cwd = os.getcwd()
  390. try:
  391. os.chdir(tmpdir)
  392. cfg = load_config()
  393. finally:
  394. os.chdir(old_cwd)
  395. assert cfg.whisper.backend == "faster-whisper"
  396. def test_backend_pywhispercpp_is_parsed():
  397. """backend 'pywhispercpp' is parsed from TOML."""
  398. config_toml = """
  399. [whisper]
  400. backend = "pywhispercpp"
  401. """
  402. with tempfile.TemporaryDirectory() as tmpdir:
  403. config_path = Path(tmpdir) / "config.toml"
  404. config_path.write_text(config_toml)
  405. cfg = load_config(str(config_path))
  406. assert cfg.whisper.backend == "pywhispercpp"
  407. def test_backend_is_normalized():
  408. """Backend names are normalized: stripped, lowercased, underscores to dashes."""
  409. config_toml = """
  410. [whisper]
  411. backend = " Faster_Whisper "
  412. """
  413. with tempfile.TemporaryDirectory() as tmpdir:
  414. config_path = Path(tmpdir) / "config.toml"
  415. config_path.write_text(config_toml)
  416. cfg = load_config(str(config_path))
  417. assert cfg.whisper.backend == "faster-whisper"
  418. def test_unknown_backend_falls_back_to_faster_whisper(caplog):
  419. """An unknown backend value falls back to 'faster-whisper' with a warning."""
  420. config_toml = """
  421. [whisper]
  422. backend = "bogus"
  423. """
  424. with tempfile.TemporaryDirectory() as tmpdir:
  425. config_path = Path(tmpdir) / "config.toml"
  426. config_path.write_text(config_toml)
  427. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  428. cfg = load_config(str(config_path))
  429. assert cfg.whisper.backend == "faster-whisper"
  430. assert "Unknown whisper backend" in caplog.text
  431. assert "bogus" in caplog.text
  432. def test_triage_defaults():
  433. """The [triage] section defaults match the plan's surviving-key list."""
  434. with tempfile.TemporaryDirectory() as tmpdir:
  435. import os
  436. old_cwd = os.getcwd()
  437. try:
  438. os.chdir(tmpdir)
  439. cfg = load_config()
  440. finally:
  441. os.chdir(old_cwd)
  442. assert cfg.triage.enabled is False
  443. assert cfg.triage.questions_file == "questions.json"
  444. assert cfg.triage.window == 500
  445. assert cfg.triage.state_char_budget == 3000
  446. assert cfg.triage.periodic_s == 30.0
  447. assert cfg.triage.predict_max_tokens == 1024
  448. assert cfg.triage.confidence_field == "answer_confidence"
  449. assert cfg.triage.escalate_threshold == 0.6
  450. assert cfg.triage.choice_min_confidence == 0.85
  451. assert cfg.triage.extract_enabled is True
  452. assert cfg.triage.extract_max_tokens == 3072
  453. assert cfg.triage.extract_timeout_s == 30.0
  454. assert cfg.triage.brief_slot_id is None
  455. assert cfg.triage.brief_gate_enabled is True
  456. assert cfg.triage.brief_gate_threshold == 0.5
  457. assert cfg.triage.brief_max_chars == 4000
  458. assert cfg.triage.brief_pending_char_budget == 3000
  459. assert cfg.triage.ledger_max_facts == 200
  460. assert cfg.triage.fact_max_chars == 240
  461. assert cfg.triage.device == ""
  462. assert cfg.triage.preload == ["multilingual"]
  463. assert cfg.triage.default_checkpoint == "multilingual"
  464. def test_triage_config_fields_parsed():
  465. """Every surviving [triage] key is applied from TOML."""
  466. config_toml = """
  467. [triage]
  468. enabled = true
  469. questions_file = "custom_questions.json"
  470. window = 4
  471. state_char_budget = 800
  472. predict_max_tokens = 768
  473. confidence_field = "confidence"
  474. escalate_threshold = 0.7
  475. choice_min_confidence = 0.9
  476. extract_enabled = false
  477. extract_max_tokens = 256
  478. extract_timeout_s = 12.5
  479. device = "cuda"
  480. preload = ["multilingual", "english"]
  481. default_checkpoint = "english"
  482. """
  483. with tempfile.TemporaryDirectory() as tmpdir:
  484. config_path = Path(tmpdir) / "config.toml"
  485. config_path.write_text(config_toml)
  486. cfg = load_config(str(config_path))
  487. assert cfg.triage.enabled is True
  488. assert cfg.triage.questions_file == "custom_questions.json"
  489. assert cfg.triage.window == 4
  490. assert cfg.triage.state_char_budget == 800
  491. assert cfg.triage.predict_max_tokens == 768
  492. assert cfg.triage.confidence_field == "confidence"
  493. assert cfg.triage.escalate_threshold == 0.7
  494. assert cfg.triage.choice_min_confidence == 0.9
  495. assert cfg.triage.extract_enabled is False
  496. assert cfg.triage.extract_max_tokens == 256
  497. assert cfg.triage.extract_timeout_s == 12.5
  498. assert cfg.triage.device == "cuda"
  499. assert cfg.triage.preload == ["multilingual", "english"]
  500. assert cfg.triage.default_checkpoint == "english"
  501. def test_llm_slot_and_brief_prompt_defaults():
  502. """Slots default to None; the brief prompts match the plan verbatim."""
  503. with tempfile.TemporaryDirectory() as tmpdir:
  504. import os
  505. old_cwd = os.getcwd()
  506. try:
  507. os.chdir(tmpdir)
  508. cfg = load_config()
  509. finally:
  510. os.chdir(old_cwd)
  511. assert cfg.llm.chat_slot_id is None
  512. assert cfg.llm.command_slot_id is None
  513. assert cfg.llm.triage_system_prompt == (
  514. "You maintain a living brief of a business conversation. Given the current brief "
  515. "(your previous reply, if any) and a new transcript segment, return the UPDATED "
  516. "brief as plain text only, with no markdown fences and no JSON. Open with a Summary "
  517. "section: a rolling overview of the conversation so far, one to a few sentences for "
  518. "a short session and growing to a short paragraph or two as the session extends. "
  519. "Preserve prior content unless the new segment contradicts it; merge duplicates; "
  520. "prune stale items; keep owners, responsibilities, and deadlines explicit. Use short "
  521. "sections: Summary, Opportunities, Blockers/Risks, Responsibilities, Decisions, "
  522. "Action Items, Deadlines, Open Questions. Output the brief text directly; never "
  523. "emit tool calls, function-call syntax, or special wrapper tokens."
  524. )
  525. assert cfg.llm.brief_compaction_system_prompt == (
  526. "You compress a triage briefing conversation. Preserve every opportunity, blocker, "
  527. "owner, responsibility, decision, deadline, and open question; drop pleasantries. "
  528. "Output only the summary."
  529. )
  530. def test_absent_slot_ids_stay_none():
  531. """Present sections with the slot keys absent leave every slot id None."""
  532. config_toml = """
  533. [llm]
  534. temperature = 0.5
  535. [triage]
  536. window = 12
  537. """
  538. with tempfile.TemporaryDirectory() as tmpdir:
  539. config_path = Path(tmpdir) / "config.toml"
  540. config_path.write_text(config_toml)
  541. cfg = load_config(str(config_path))
  542. assert cfg.llm.chat_slot_id is None
  543. assert cfg.llm.command_slot_id is None
  544. assert cfg.triage.brief_slot_id is None
  545. def test_slot_id_zero_is_preserved():
  546. """A slot id of 0 is valid and must load as 0, not None."""
  547. config_toml = """
  548. [llm]
  549. chat_slot_id = 0
  550. command_slot_id = 0
  551. [triage]
  552. brief_slot_id = 1
  553. """
  554. with tempfile.TemporaryDirectory() as tmpdir:
  555. config_path = Path(tmpdir) / "config.toml"
  556. config_path.write_text(config_toml)
  557. cfg = load_config(str(config_path))
  558. assert cfg.llm.chat_slot_id == 0
  559. assert cfg.llm.command_slot_id == 0
  560. assert cfg.triage.brief_slot_id == 1
  561. def test_non_numeric_slot_degrades_to_none():
  562. """A non-numeric slot value degrades to None without raising."""
  563. config_toml = """
  564. [llm]
  565. chat_slot_id = "abc"
  566. [triage]
  567. brief_slot_id = "nope"
  568. """
  569. with tempfile.TemporaryDirectory() as tmpdir:
  570. config_path = Path(tmpdir) / "config.toml"
  571. config_path.write_text(config_toml)
  572. cfg = load_config(str(config_path))
  573. assert cfg.llm.chat_slot_id is None
  574. assert cfg.triage.brief_slot_id is None
  575. def test_triage_brief_keys_are_parsed():
  576. """Every new [triage] brief key is applied from TOML."""
  577. config_toml = """
  578. [triage]
  579. brief_slot_id = 7
  580. brief_gate_enabled = false
  581. brief_gate_threshold = 0.8
  582. brief_max_chars = 1234
  583. brief_pending_char_budget = 777
  584. """
  585. with tempfile.TemporaryDirectory() as tmpdir:
  586. config_path = Path(tmpdir) / "config.toml"
  587. config_path.write_text(config_toml)
  588. cfg = load_config(str(config_path))
  589. assert cfg.triage.brief_slot_id == 7
  590. assert cfg.triage.brief_gate_enabled is False
  591. assert cfg.triage.brief_gate_threshold == 0.8
  592. assert cfg.triage.brief_max_chars == 1234
  593. assert cfg.triage.brief_pending_char_budget == 777
  594. def test_triage_periodic_key_is_parsed():
  595. """The [triage] periodic cadence is applied from TOML."""
  596. config_toml = """
  597. [triage]
  598. periodic_s = 12.5
  599. """
  600. with tempfile.TemporaryDirectory() as tmpdir:
  601. config_path = Path(tmpdir) / "config.toml"
  602. config_path.write_text(config_toml)
  603. cfg = load_config(str(config_path))
  604. assert cfg.triage.periodic_s == 12.5
  605. def test_triage_ledger_keys_are_parsed():
  606. """The [triage] ledger cap keys are applied from TOML."""
  607. config_toml = """
  608. [triage]
  609. ledger_max_facts = 42
  610. fact_max_chars = 111
  611. """
  612. with tempfile.TemporaryDirectory() as tmpdir:
  613. config_path = Path(tmpdir) / "config.toml"
  614. config_path.write_text(config_toml)
  615. cfg = load_config(str(config_path))
  616. assert cfg.triage.ledger_max_facts == 42
  617. assert cfg.triage.fact_max_chars == 111
  618. def test_brief_defaults():
  619. """The [brief] section defaults to no on-disk writing."""
  620. with tempfile.TemporaryDirectory() as tmpdir:
  621. import os
  622. old_cwd = os.getcwd()
  623. try:
  624. os.chdir(tmpdir)
  625. cfg = load_config()
  626. finally:
  627. os.chdir(old_cwd)
  628. assert cfg.brief.write is False
  629. assert not hasattr(cfg.brief, "dir")
  630. assert not hasattr(cfg.brief, "filename")
  631. def test_brief_config_fields_are_parsed():
  632. """The [brief] write key is applied from TOML."""
  633. config_toml = """
  634. [brief]
  635. write = true
  636. """
  637. with tempfile.TemporaryDirectory() as tmpdir:
  638. config_path = Path(tmpdir) / "config.toml"
  639. config_path.write_text(config_toml)
  640. cfg = load_config(str(config_path))
  641. assert cfg.brief.write is True
  642. assert not hasattr(cfg.brief, "dir")
  643. assert not hasattr(cfg.brief, "filename")
  644. def test_brief_write_false_is_parsed(caplog):
  645. """An explicit [brief] write=false loads as False and is not an unknown section."""
  646. config_toml = """
  647. [brief]
  648. write = false
  649. """
  650. with tempfile.TemporaryDirectory() as tmpdir:
  651. config_path = Path(tmpdir) / "config.toml"
  652. config_path.write_text(config_toml)
  653. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  654. cfg = load_config(str(config_path))
  655. assert cfg.brief.write is False
  656. assert "brief" not in caplog.text
  657. def test_triage_dump_io_defaults_to_false():
  658. """[triage].dump_io is opt-in: False unless explicitly enabled."""
  659. with tempfile.TemporaryDirectory() as tmpdir:
  660. import os
  661. old_cwd = os.getcwd()
  662. try:
  663. os.chdir(tmpdir)
  664. cfg = load_config()
  665. finally:
  666. os.chdir(old_cwd)
  667. assert cfg.triage.dump_io is False
  668. def test_triage_dump_io_is_parsed():
  669. """[triage].dump_io = true is applied from TOML."""
  670. config_toml = """
  671. [triage]
  672. dump_io = true
  673. """
  674. with tempfile.TemporaryDirectory() as tmpdir:
  675. config_path = Path(tmpdir) / "config.toml"
  676. config_path.write_text(config_toml)
  677. cfg = load_config(str(config_path))
  678. assert cfg.triage.dump_io is True
  679. def test_unknown_triag_section_warns_and_keeps_defaults(caplog):
  680. """A typo'd [triag] section must warn and never raise; triage keeps defaults."""
  681. config_toml = """
  682. [triag]
  683. enabled = true
  684. window = 3
  685. """
  686. with tempfile.TemporaryDirectory() as tmpdir:
  687. config_path = Path(tmpdir) / "config.toml"
  688. config_path.write_text(config_toml)
  689. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  690. cfg = load_config(str(config_path))
  691. assert "triag" in caplog.text
  692. assert cfg.triage.enabled is False
  693. assert cfg.triage.window == 500
  694. def test_triage_config_has_no_struck_keys():
  695. """The v5 keys are deliberately gone from TriageConfig (superseded/removed)."""
  696. from stt_daemon.config import TriageConfig
  697. struck = {"socket", "model", "predict_timeout_s", "checkpoint", "subfolder", "max_len"}
  698. for name in struck:
  699. assert not hasattr(TriageConfig, name), f"TriageConfig must not expose {name!r}"
  700. def test_llm_triage_system_prompt_is_parsed():
  701. """[llm].triage_system_prompt overrides the JSON-only extractor default."""
  702. config_toml = """
  703. [llm]
  704. triage_system_prompt = "ONLY JSON, nothing else."
  705. """
  706. with tempfile.TemporaryDirectory() as tmpdir:
  707. config_path = Path(tmpdir) / "config.toml"
  708. config_path.write_text(config_toml)
  709. cfg = load_config(str(config_path))
  710. assert cfg.llm.triage_system_prompt == "ONLY JSON, nothing else."
  711. def test_no_stt_laya_script():
  712. """Triage is in-process only: no stt-laya console script may ever be added."""
  713. with (REPO_ROOT / "pyproject.toml").open("rb") as fh:
  714. data = tomllib.load(fh)
  715. scripts = data["project"]["scripts"]
  716. assert set(scripts) == {"stt-daemon", "stt-assistant", "stt-ctl", "stt-tui"}
  717. assert not any("laya" in name for name in scripts)
  718. def test_layasvc_module_not_importable():
  719. """The retired external laya service package must not exist (in-process lane)."""
  720. assert importlib.util.find_spec("stt_daemon.layasvc") is None
  721. def test_triage_extra_pins_laya_revision():
  722. """The triage extra pins laya to the exact verified upstream revision."""
  723. with (REPO_ROOT / "pyproject.toml").open("rb") as fh:
  724. data = tomllib.load(fh)
  725. assert data["project"]["optional-dependencies"]["triage"] == ["laya"]
  726. source = data["tool"]["uv"]["sources"]["laya"]
  727. assert source["git"] == "https://github.com/NandhaKishorM/laya.git"
  728. assert source["rev"] == LAYA_REV
  729. def test_questions_json_gitignored():
  730. """The user's private questions.json is never committed (like config.toml)."""
  731. lines = (REPO_ROOT / ".gitignore").read_text().splitlines()
  732. assert "questions.json" in lines
  733. def test_speaker_defaults():
  734. """The [speaker] section defaults match the plan's 14-key list."""
  735. with tempfile.TemporaryDirectory() as tmpdir:
  736. import os
  737. old_cwd = os.getcwd()
  738. try:
  739. os.chdir(tmpdir)
  740. cfg = load_config()
  741. finally:
  742. os.chdir(old_cwd)
  743. assert cfg.speaker.enabled is False
  744. assert cfg.speaker.model_path is None
  745. assert cfg.speaker.provider == "cpu"
  746. assert cfg.speaker.threshold_id == 0.60
  747. assert cfg.speaker.threshold_new == 0.45
  748. assert cfg.speaker.min_enroll_s == 2.0
  749. assert cfg.speaker.rms_floor == 0.0
  750. assert cfg.speaker.margin == 0.04
  751. assert cfg.speaker.mint_cooldown_s == 60.0
  752. assert cfg.speaker.confirm_s == 12.0
  753. assert cfg.speaker.max_prototypes == 8
  754. assert cfg.speaker.provisional_ttl_h == 24.0
  755. assert cfg.speaker.store_dir == "speakers"
  756. assert cfg.speaker.join_timeout_s == 2.0
  757. def test_speaker_config_fields_parsed():
  758. """Every [speaker] key is applied from TOML."""
  759. config_toml = """
  760. [speaker]
  761. enabled = true
  762. model_path = "models/nemo_en_titanet_small.onnx"
  763. provider = "cuda"
  764. threshold_id = 0.7
  765. threshold_new = 0.5
  766. min_enroll_s = 3.0
  767. rms_floor = 0.01
  768. margin = 0.05
  769. mint_cooldown_s = 30.0
  770. confirm_s = 8.0
  771. max_prototypes = 4
  772. provisional_ttl_h = 12.0
  773. store_dir = "voiceprints"
  774. join_timeout_s = 1.5
  775. """
  776. with tempfile.TemporaryDirectory() as tmpdir:
  777. config_path = Path(tmpdir) / "config.toml"
  778. config_path.write_text(config_toml)
  779. cfg = load_config(str(config_path))
  780. assert cfg.speaker.enabled is True
  781. assert cfg.speaker.model_path == "models/nemo_en_titanet_small.onnx"
  782. assert cfg.speaker.provider == "cuda"
  783. assert cfg.speaker.threshold_id == 0.7
  784. assert cfg.speaker.threshold_new == 0.5
  785. assert cfg.speaker.min_enroll_s == 3.0
  786. assert cfg.speaker.rms_floor == 0.01
  787. assert cfg.speaker.margin == 0.05
  788. assert cfg.speaker.mint_cooldown_s == 30.0
  789. assert cfg.speaker.confirm_s == 8.0
  790. assert cfg.speaker.max_prototypes == 4
  791. assert cfg.speaker.provisional_ttl_h == 12.0
  792. assert cfg.speaker.store_dir == "voiceprints"
  793. assert cfg.speaker.join_timeout_s == 1.5
  794. def test_unknown_speaker_key_is_ignored():
  795. """An unknown key inside [speaker] is silently ignored (per-field .get)."""
  796. config_toml = """
  797. [speaker]
  798. enabled = true
  799. bogus_key = 42
  800. """
  801. with tempfile.TemporaryDirectory() as tmpdir:
  802. config_path = Path(tmpdir) / "config.toml"
  803. config_path.write_text(config_toml)
  804. cfg = load_config(str(config_path))
  805. assert cfg.speaker.enabled is True
  806. assert not hasattr(cfg.speaker, "bogus_key")
  807. assert cfg.speaker.provider == "cpu"
  808. def test_unknown_speaker_provider_falls_back_to_cpu(caplog):
  809. """An unknown speaker provider falls back to 'cpu' with a warning."""
  810. config_toml = """
  811. [speaker]
  812. provider = "bogus"
  813. """
  814. with tempfile.TemporaryDirectory() as tmpdir:
  815. config_path = Path(tmpdir) / "config.toml"
  816. config_path.write_text(config_toml)
  817. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  818. cfg = load_config(str(config_path))
  819. assert cfg.speaker.provider == "cpu"
  820. assert "Unknown speaker provider" in caplog.text
  821. assert "bogus" in caplog.text
  822. def test_debug_defaults():
  823. """The [debug] section defaults to disabled with signal-log defaults."""
  824. with tempfile.TemporaryDirectory() as tmpdir:
  825. import os
  826. old_cwd = os.getcwd()
  827. try:
  828. os.chdir(tmpdir)
  829. cfg = load_config()
  830. finally:
  831. os.chdir(old_cwd)
  832. assert cfg.debug.enabled is False
  833. assert cfg.debug.dir == "debug"
  834. assert cfg.debug.heartbeat is True
  835. assert cfg.debug.max_bytes_per_day == 50000000
  836. def test_debug_config_fields_are_parsed():
  837. """Every [debug] key is applied from TOML."""
  838. config_toml = """
  839. [debug]
  840. enabled = true
  841. dir = "dbg"
  842. heartbeat = false
  843. max_bytes_per_day = 1000
  844. """
  845. with tempfile.TemporaryDirectory() as tmpdir:
  846. config_path = Path(tmpdir) / "config.toml"
  847. config_path.write_text(config_toml)
  848. cfg = load_config(str(config_path))
  849. assert cfg.debug.enabled is True
  850. assert cfg.debug.dir == "dbg"
  851. assert cfg.debug.heartbeat is False
  852. assert cfg.debug.max_bytes_per_day == 1000
  853. def test_debug_section_not_warned(caplog):
  854. """An explicit [debug] section loads without an unknown-section warning."""
  855. config_toml = """
  856. [debug]
  857. enabled = false
  858. """
  859. with tempfile.TemporaryDirectory() as tmpdir:
  860. config_path = Path(tmpdir) / "config.toml"
  861. config_path.write_text(config_toml)
  862. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  863. cfg = load_config(str(config_path))
  864. assert cfg.debug.enabled is False
  865. assert "debug" not in caplog.text
  866. def test_unknown_debug_key_is_ignored():
  867. """An unknown key inside [debug] is silently ignored (per-field .get)."""
  868. config_toml = """
  869. [debug]
  870. enabled = true
  871. bogus_key = 42
  872. """
  873. with tempfile.TemporaryDirectory() as tmpdir:
  874. config_path = Path(tmpdir) / "config.toml"
  875. config_path.write_text(config_toml)
  876. cfg = load_config(str(config_path))
  877. assert cfg.debug.enabled is True
  878. assert not hasattr(cfg.debug, "bogus_key")
  879. assert cfg.debug.dir == "debug"
  880. def test_unknown_top_level_still_warns(caplog):
  881. """An unknown top-level section still warns after [debug] exists."""
  882. config_toml = """
  883. [debugg]
  884. enabled = true
  885. """
  886. with tempfile.TemporaryDirectory() as tmpdir:
  887. config_path = Path(tmpdir) / "config.toml"
  888. config_path.write_text(config_toml)
  889. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  890. cfg = load_config(str(config_path))
  891. assert "debugg" in caplog.text
  892. assert cfg.debug.enabled is False