test_config.py 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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_llm_auth_and_thinking_fields_parsed():
  373. config_toml = """
  374. [llm]
  375. model = "gpt-4o-mini"
  376. api_key_env = "OPENAI_API_KEY"
  377. session_id = "stt-chat"
  378. session_header = "x-opencode-session"
  379. user_agent = "stt-daemon/1.0"
  380. reasoning_effort = "high"
  381. extra_body = { enable_thinking = true, top_k = 20 }
  382. """
  383. with tempfile.TemporaryDirectory() as tmpdir:
  384. config_path = Path(tmpdir) / "config.toml"
  385. config_path.write_text(config_toml)
  386. cfg = load_config(str(config_path))
  387. assert cfg.llm.model == "gpt-4o-mini"
  388. assert cfg.llm.api_key_env == "OPENAI_API_KEY"
  389. assert cfg.llm.session_id == "stt-chat"
  390. assert cfg.llm.session_header == "x-opencode-session"
  391. assert cfg.llm.user_agent == "stt-daemon/1.0"
  392. assert cfg.llm.reasoning_effort == "high"
  393. assert cfg.llm.extra_body == {"enable_thinking": True, "top_k": 20}
  394. def test_llm_auth_and_thinking_defaults():
  395. with tempfile.TemporaryDirectory() as tmpdir:
  396. import os
  397. old_cwd = os.getcwd()
  398. try:
  399. os.chdir(tmpdir)
  400. cfg = load_config()
  401. finally:
  402. os.chdir(old_cwd)
  403. assert cfg.llm.model == ""
  404. assert cfg.llm.api_key_env == ""
  405. assert cfg.llm.session_id == ""
  406. assert cfg.llm.session_header == ""
  407. assert cfg.llm.user_agent == ""
  408. assert cfg.llm.reasoning_effort == ""
  409. assert cfg.llm.extra_body == {}
  410. def test_max_prompt_tokens_is_deprecated_and_ignored(caplog):
  411. config_toml = """
  412. [llm]
  413. max_prompt_tokens = 50
  414. """
  415. with tempfile.TemporaryDirectory() as tmpdir:
  416. config_path = Path(tmpdir) / "config.toml"
  417. config_path.write_text(config_toml)
  418. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  419. cfg = load_config(str(config_path))
  420. assert "max_prompt_tokens" in caplog.text
  421. assert "deprecated" in caplog.text
  422. assert cfg.llm.prompt_budget == 131072 - 1024 - 256
  423. def test_backend_defaults_to_faster_whisper():
  424. """backend defaults to 'faster-whisper'."""
  425. with tempfile.TemporaryDirectory() as tmpdir:
  426. import os
  427. old_cwd = os.getcwd()
  428. try:
  429. os.chdir(tmpdir)
  430. cfg = load_config()
  431. finally:
  432. os.chdir(old_cwd)
  433. assert cfg.whisper.backend == "faster-whisper"
  434. def test_backend_pywhispercpp_is_parsed():
  435. """backend 'pywhispercpp' is parsed from TOML."""
  436. config_toml = """
  437. [whisper]
  438. backend = "pywhispercpp"
  439. """
  440. with tempfile.TemporaryDirectory() as tmpdir:
  441. config_path = Path(tmpdir) / "config.toml"
  442. config_path.write_text(config_toml)
  443. cfg = load_config(str(config_path))
  444. assert cfg.whisper.backend == "pywhispercpp"
  445. def test_backend_is_normalized():
  446. """Backend names are normalized: stripped, lowercased, underscores to dashes."""
  447. config_toml = """
  448. [whisper]
  449. backend = " Faster_Whisper "
  450. """
  451. with tempfile.TemporaryDirectory() as tmpdir:
  452. config_path = Path(tmpdir) / "config.toml"
  453. config_path.write_text(config_toml)
  454. cfg = load_config(str(config_path))
  455. assert cfg.whisper.backend == "faster-whisper"
  456. def test_unknown_backend_falls_back_to_faster_whisper(caplog):
  457. """An unknown backend value falls back to 'faster-whisper' with a warning."""
  458. config_toml = """
  459. [whisper]
  460. backend = "bogus"
  461. """
  462. with tempfile.TemporaryDirectory() as tmpdir:
  463. config_path = Path(tmpdir) / "config.toml"
  464. config_path.write_text(config_toml)
  465. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  466. cfg = load_config(str(config_path))
  467. assert cfg.whisper.backend == "faster-whisper"
  468. assert "Unknown whisper backend" in caplog.text
  469. assert "bogus" in caplog.text
  470. def test_triage_defaults():
  471. """The [triage] section defaults match the plan's surviving-key list."""
  472. with tempfile.TemporaryDirectory() as tmpdir:
  473. import os
  474. old_cwd = os.getcwd()
  475. try:
  476. os.chdir(tmpdir)
  477. cfg = load_config()
  478. finally:
  479. os.chdir(old_cwd)
  480. assert cfg.triage.enabled is False
  481. assert cfg.triage.questions_file == "questions.json"
  482. assert cfg.triage.window == 500
  483. assert cfg.triage.state_char_budget == 3000
  484. assert cfg.triage.periodic_s == 30.0
  485. assert cfg.triage.predict_max_tokens == 1024
  486. assert cfg.triage.confidence_field == "answer_confidence"
  487. assert cfg.triage.escalate_threshold == 0.6
  488. assert cfg.triage.choice_min_confidence == 0.85
  489. assert cfg.triage.extract_enabled is True
  490. assert cfg.triage.extract_max_tokens == 3072
  491. assert cfg.triage.extract_timeout_s == 30.0
  492. assert cfg.triage.brief_slot_id is None
  493. assert cfg.triage.brief_gate_enabled is True
  494. assert cfg.triage.brief_gate_threshold == 0.5
  495. assert cfg.triage.brief_max_chars == 4000
  496. assert cfg.triage.brief_pending_char_budget == 3000
  497. assert cfg.triage.ledger_max_facts == 200
  498. assert cfg.triage.fact_max_chars == 240
  499. assert cfg.triage.device == ""
  500. assert cfg.triage.preload == ["multilingual"]
  501. assert cfg.triage.default_checkpoint == "multilingual"
  502. def test_triage_config_fields_parsed():
  503. """Every surviving [triage] key is applied from TOML."""
  504. config_toml = """
  505. [triage]
  506. enabled = true
  507. questions_file = "custom_questions.json"
  508. window = 4
  509. state_char_budget = 800
  510. predict_max_tokens = 768
  511. confidence_field = "confidence"
  512. escalate_threshold = 0.7
  513. choice_min_confidence = 0.9
  514. extract_enabled = false
  515. extract_max_tokens = 256
  516. extract_timeout_s = 12.5
  517. device = "cuda"
  518. preload = ["multilingual", "english"]
  519. default_checkpoint = "english"
  520. """
  521. with tempfile.TemporaryDirectory() as tmpdir:
  522. config_path = Path(tmpdir) / "config.toml"
  523. config_path.write_text(config_toml)
  524. cfg = load_config(str(config_path))
  525. assert cfg.triage.enabled is True
  526. assert cfg.triage.questions_file == "custom_questions.json"
  527. assert cfg.triage.window == 4
  528. assert cfg.triage.state_char_budget == 800
  529. assert cfg.triage.predict_max_tokens == 768
  530. assert cfg.triage.confidence_field == "confidence"
  531. assert cfg.triage.escalate_threshold == 0.7
  532. assert cfg.triage.choice_min_confidence == 0.9
  533. assert cfg.triage.extract_enabled is False
  534. assert cfg.triage.extract_max_tokens == 256
  535. assert cfg.triage.extract_timeout_s == 12.5
  536. assert cfg.triage.device == "cuda"
  537. assert cfg.triage.preload == ["multilingual", "english"]
  538. assert cfg.triage.default_checkpoint == "english"
  539. def test_llm_slot_and_brief_prompt_defaults():
  540. """Slots default to None; the brief prompts match the plan verbatim."""
  541. with tempfile.TemporaryDirectory() as tmpdir:
  542. import os
  543. old_cwd = os.getcwd()
  544. try:
  545. os.chdir(tmpdir)
  546. cfg = load_config()
  547. finally:
  548. os.chdir(old_cwd)
  549. assert cfg.llm.chat_slot_id is None
  550. assert cfg.llm.command_slot_id is None
  551. assert cfg.llm.triage_system_prompt == (
  552. "You maintain a living brief of a business conversation. The caller sends "
  553. "CURRENT LEDGER plus CURRENT BRIEF plus NEW UTTERANCES. Reply with exactly "
  554. "two sections in this order: NEW FACTS: first, then BRIEF:. Facts go first "
  555. "so a truncated reply still preserves extraction (truncation safety). In "
  556. "NEW FACTS:, emit one fact per line as '- <fact>', or the single line "
  557. "'none' when the new utterances add no new facts. Never repeat facts "
  558. "already listed in CURRENT LEDGER. In BRIEF:, return the UPDATED brief as "
  559. "plain text only, with no markdown fences and no JSON. Open with a Summary "
  560. "section: a rolling overview of the conversation so far, one to a few "
  561. "sentences for a short session and growing to a short paragraph or two as "
  562. "the session extends. Preserve prior content unless the new utterances "
  563. "contradict it; merge duplicates; prune stale items; keep owners, "
  564. "responsibilities, and deadlines explicit. Use short sections: Summary, "
  565. "Opportunities, Blockers/Risks, Responsibilities, Decisions, Action Items, "
  566. "Deadlines, Open Questions. Never silently delete Action Items, Open "
  567. "Questions, or Decisions lines. When an action completes or a question "
  568. "is answered, do NOT drop the line — move it to Decisions as "
  569. "'Done: <original> — resolved: <what happened>' (or keep it in place "
  570. "with a 'Done (...)' suffix) AND list the resolution as a NEW FACTS "
  571. "bullet. Prune only exact duplicates, never resolved history. Lead the "
  572. "Summary with currently-open opportunities first, then the rest. Keep the "
  573. "Opportunities section the most complete and current — do not let "
  574. "Blockers/Risks crowd it out; other sections stay unchanged. "
  575. "Never emit a 'Prior Details:' section. Output "
  576. "the two sections directly; never emit tool calls, function-call syntax, "
  577. "or special wrapper tokens."
  578. )
  579. assert cfg.llm.brief_compaction_system_prompt == (
  580. "You compress a triage briefing conversation. Preserve every opportunity, blocker, "
  581. "owner, responsibility, decision, deadline, and open question; drop pleasantries. "
  582. "Output only the summary."
  583. )
  584. def test_absent_slot_ids_stay_none():
  585. """Present sections with the slot keys absent leave every slot id None."""
  586. config_toml = """
  587. [llm]
  588. temperature = 0.5
  589. [triage]
  590. window = 12
  591. """
  592. with tempfile.TemporaryDirectory() as tmpdir:
  593. config_path = Path(tmpdir) / "config.toml"
  594. config_path.write_text(config_toml)
  595. cfg = load_config(str(config_path))
  596. assert cfg.llm.chat_slot_id is None
  597. assert cfg.llm.command_slot_id is None
  598. assert cfg.triage.brief_slot_id is None
  599. def test_slot_id_zero_is_preserved():
  600. """A slot id of 0 is valid and must load as 0, not None."""
  601. config_toml = """
  602. [llm]
  603. chat_slot_id = 0
  604. command_slot_id = 0
  605. [triage]
  606. brief_slot_id = 1
  607. """
  608. with tempfile.TemporaryDirectory() as tmpdir:
  609. config_path = Path(tmpdir) / "config.toml"
  610. config_path.write_text(config_toml)
  611. cfg = load_config(str(config_path))
  612. assert cfg.llm.chat_slot_id == 0
  613. assert cfg.llm.command_slot_id == 0
  614. assert cfg.triage.brief_slot_id == 1
  615. def test_non_numeric_slot_degrades_to_none():
  616. """A non-numeric slot value degrades to None without raising."""
  617. config_toml = """
  618. [llm]
  619. chat_slot_id = "abc"
  620. [triage]
  621. brief_slot_id = "nope"
  622. """
  623. with tempfile.TemporaryDirectory() as tmpdir:
  624. config_path = Path(tmpdir) / "config.toml"
  625. config_path.write_text(config_toml)
  626. cfg = load_config(str(config_path))
  627. assert cfg.llm.chat_slot_id is None
  628. assert cfg.triage.brief_slot_id is None
  629. def test_triage_brief_keys_are_parsed():
  630. """Every new [triage] brief key is applied from TOML."""
  631. config_toml = """
  632. [triage]
  633. brief_slot_id = 7
  634. brief_gate_enabled = false
  635. brief_gate_threshold = 0.8
  636. brief_max_chars = 1234
  637. brief_pending_char_budget = 777
  638. """
  639. with tempfile.TemporaryDirectory() as tmpdir:
  640. config_path = Path(tmpdir) / "config.toml"
  641. config_path.write_text(config_toml)
  642. cfg = load_config(str(config_path))
  643. assert cfg.triage.brief_slot_id == 7
  644. assert cfg.triage.brief_gate_enabled is False
  645. assert cfg.triage.brief_gate_threshold == 0.8
  646. assert cfg.triage.brief_max_chars == 1234
  647. assert cfg.triage.brief_pending_char_budget == 777
  648. def test_triage_periodic_key_is_parsed():
  649. """The [triage] periodic cadence is applied from TOML."""
  650. config_toml = """
  651. [triage]
  652. periodic_s = 12.5
  653. """
  654. with tempfile.TemporaryDirectory() as tmpdir:
  655. config_path = Path(tmpdir) / "config.toml"
  656. config_path.write_text(config_toml)
  657. cfg = load_config(str(config_path))
  658. assert cfg.triage.periodic_s == 12.5
  659. def test_triage_ledger_keys_are_parsed():
  660. """The [triage] ledger cap keys are applied from TOML."""
  661. config_toml = """
  662. [triage]
  663. ledger_max_facts = 42
  664. fact_max_chars = 111
  665. """
  666. with tempfile.TemporaryDirectory() as tmpdir:
  667. config_path = Path(tmpdir) / "config.toml"
  668. config_path.write_text(config_toml)
  669. cfg = load_config(str(config_path))
  670. assert cfg.triage.ledger_max_facts == 42
  671. assert cfg.triage.fact_max_chars == 111
  672. def test_brief_defaults():
  673. """The [brief] section defaults to no on-disk writing."""
  674. with tempfile.TemporaryDirectory() as tmpdir:
  675. import os
  676. old_cwd = os.getcwd()
  677. try:
  678. os.chdir(tmpdir)
  679. cfg = load_config()
  680. finally:
  681. os.chdir(old_cwd)
  682. assert cfg.brief.write is False
  683. assert not hasattr(cfg.brief, "dir")
  684. assert not hasattr(cfg.brief, "filename")
  685. def test_brief_config_fields_are_parsed():
  686. """The [brief] write key is applied from TOML."""
  687. config_toml = """
  688. [brief]
  689. write = true
  690. """
  691. with tempfile.TemporaryDirectory() as tmpdir:
  692. config_path = Path(tmpdir) / "config.toml"
  693. config_path.write_text(config_toml)
  694. cfg = load_config(str(config_path))
  695. assert cfg.brief.write is True
  696. assert not hasattr(cfg.brief, "dir")
  697. assert not hasattr(cfg.brief, "filename")
  698. def test_brief_write_false_is_parsed(caplog):
  699. """An explicit [brief] write=false loads as False and is not an unknown section."""
  700. config_toml = """
  701. [brief]
  702. write = false
  703. """
  704. with tempfile.TemporaryDirectory() as tmpdir:
  705. config_path = Path(tmpdir) / "config.toml"
  706. config_path.write_text(config_toml)
  707. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  708. cfg = load_config(str(config_path))
  709. assert cfg.brief.write is False
  710. assert "brief" not in caplog.text
  711. def test_triage_dump_io_defaults_to_false():
  712. """[triage].dump_io is opt-in: False unless explicitly enabled."""
  713. with tempfile.TemporaryDirectory() as tmpdir:
  714. import os
  715. old_cwd = os.getcwd()
  716. try:
  717. os.chdir(tmpdir)
  718. cfg = load_config()
  719. finally:
  720. os.chdir(old_cwd)
  721. assert cfg.triage.dump_io is False
  722. def test_triage_dump_io_is_parsed():
  723. """[triage].dump_io = true is applied from TOML."""
  724. config_toml = """
  725. [triage]
  726. dump_io = true
  727. """
  728. with tempfile.TemporaryDirectory() as tmpdir:
  729. config_path = Path(tmpdir) / "config.toml"
  730. config_path.write_text(config_toml)
  731. cfg = load_config(str(config_path))
  732. assert cfg.triage.dump_io is True
  733. def test_unknown_triag_section_warns_and_keeps_defaults(caplog):
  734. """A typo'd [triag] section must warn and never raise; triage keeps defaults."""
  735. config_toml = """
  736. [triag]
  737. enabled = true
  738. window = 3
  739. """
  740. with tempfile.TemporaryDirectory() as tmpdir:
  741. config_path = Path(tmpdir) / "config.toml"
  742. config_path.write_text(config_toml)
  743. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  744. cfg = load_config(str(config_path))
  745. assert "triag" in caplog.text
  746. assert cfg.triage.enabled is False
  747. assert cfg.triage.window == 500
  748. def test_triage_config_has_no_struck_keys():
  749. """The v5 keys are deliberately gone from TriageConfig (superseded/removed)."""
  750. from stt_daemon.config import TriageConfig
  751. struck = {"socket", "model", "predict_timeout_s", "checkpoint", "subfolder", "max_len"}
  752. for name in struck:
  753. assert not hasattr(TriageConfig, name), f"TriageConfig must not expose {name!r}"
  754. def test_llm_triage_system_prompt_is_parsed():
  755. """[llm].triage_system_prompt overrides the JSON-only extractor default."""
  756. config_toml = """
  757. [llm]
  758. triage_system_prompt = "ONLY JSON, nothing else."
  759. """
  760. with tempfile.TemporaryDirectory() as tmpdir:
  761. config_path = Path(tmpdir) / "config.toml"
  762. config_path.write_text(config_toml)
  763. cfg = load_config(str(config_path))
  764. assert cfg.llm.triage_system_prompt == "ONLY JSON, nothing else."
  765. def test_no_stt_laya_script():
  766. """Triage is in-process only: no stt-laya console script may ever be added."""
  767. with (REPO_ROOT / "pyproject.toml").open("rb") as fh:
  768. data = tomllib.load(fh)
  769. scripts = data["project"]["scripts"]
  770. assert set(scripts) == {"stt-daemon", "stt-assistant", "stt-ctl", "stt-tui"}
  771. assert not any("laya" in name for name in scripts)
  772. def test_layasvc_module_not_importable():
  773. """The retired external laya service package must not exist (in-process lane)."""
  774. assert importlib.util.find_spec("stt_daemon.layasvc") is None
  775. def test_triage_extra_pins_laya_revision():
  776. """The triage extra pins laya to the exact verified upstream revision."""
  777. with (REPO_ROOT / "pyproject.toml").open("rb") as fh:
  778. data = tomllib.load(fh)
  779. assert data["project"]["optional-dependencies"]["triage"] == ["laya"]
  780. source = data["tool"]["uv"]["sources"]["laya"]
  781. assert source["git"] == "https://github.com/NandhaKishorM/laya.git"
  782. assert source["rev"] == LAYA_REV
  783. def test_questions_json_gitignored():
  784. """The user's private questions.json is never committed (like config.toml)."""
  785. lines = (REPO_ROOT / ".gitignore").read_text().splitlines()
  786. assert "questions.json" in lines
  787. def test_speaker_defaults():
  788. """The [speaker] section defaults match the plan's 14-key list."""
  789. with tempfile.TemporaryDirectory() as tmpdir:
  790. import os
  791. old_cwd = os.getcwd()
  792. try:
  793. os.chdir(tmpdir)
  794. cfg = load_config()
  795. finally:
  796. os.chdir(old_cwd)
  797. assert cfg.speaker.enabled is False
  798. assert cfg.speaker.model_path is None
  799. assert cfg.speaker.provider == "cpu"
  800. assert cfg.speaker.threshold_id == 0.60
  801. assert cfg.speaker.threshold_new == 0.45
  802. assert cfg.speaker.min_enroll_s == 2.0
  803. assert cfg.speaker.rms_floor == 0.0
  804. assert cfg.speaker.margin == 0.04
  805. assert cfg.speaker.mint_cooldown_s == 60.0
  806. assert cfg.speaker.confirm_s == 12.0
  807. assert cfg.speaker.max_prototypes == 8
  808. assert cfg.speaker.provisional_ttl_h == 24.0
  809. assert cfg.speaker.store_dir == "speakers"
  810. assert cfg.speaker.join_timeout_s == 2.0
  811. def test_speaker_config_fields_parsed():
  812. """Every [speaker] key is applied from TOML."""
  813. config_toml = """
  814. [speaker]
  815. enabled = true
  816. model_path = "models/nemo_en_titanet_small.onnx"
  817. provider = "cuda"
  818. threshold_id = 0.7
  819. threshold_new = 0.5
  820. min_enroll_s = 3.0
  821. rms_floor = 0.01
  822. margin = 0.05
  823. mint_cooldown_s = 30.0
  824. confirm_s = 8.0
  825. max_prototypes = 4
  826. provisional_ttl_h = 12.0
  827. store_dir = "voiceprints"
  828. join_timeout_s = 1.5
  829. """
  830. with tempfile.TemporaryDirectory() as tmpdir:
  831. config_path = Path(tmpdir) / "config.toml"
  832. config_path.write_text(config_toml)
  833. cfg = load_config(str(config_path))
  834. assert cfg.speaker.enabled is True
  835. assert cfg.speaker.model_path == "models/nemo_en_titanet_small.onnx"
  836. assert cfg.speaker.provider == "cuda"
  837. assert cfg.speaker.threshold_id == 0.7
  838. assert cfg.speaker.threshold_new == 0.5
  839. assert cfg.speaker.min_enroll_s == 3.0
  840. assert cfg.speaker.rms_floor == 0.01
  841. assert cfg.speaker.margin == 0.05
  842. assert cfg.speaker.mint_cooldown_s == 30.0
  843. assert cfg.speaker.confirm_s == 8.0
  844. assert cfg.speaker.max_prototypes == 4
  845. assert cfg.speaker.provisional_ttl_h == 12.0
  846. assert cfg.speaker.store_dir == "voiceprints"
  847. assert cfg.speaker.join_timeout_s == 1.5
  848. def test_unknown_speaker_key_is_ignored():
  849. """An unknown key inside [speaker] is silently ignored (per-field .get)."""
  850. config_toml = """
  851. [speaker]
  852. enabled = true
  853. bogus_key = 42
  854. """
  855. with tempfile.TemporaryDirectory() as tmpdir:
  856. config_path = Path(tmpdir) / "config.toml"
  857. config_path.write_text(config_toml)
  858. cfg = load_config(str(config_path))
  859. assert cfg.speaker.enabled is True
  860. assert not hasattr(cfg.speaker, "bogus_key")
  861. assert cfg.speaker.provider == "cpu"
  862. def test_unknown_speaker_provider_falls_back_to_cpu(caplog):
  863. """An unknown speaker provider falls back to 'cpu' with a warning."""
  864. config_toml = """
  865. [speaker]
  866. provider = "bogus"
  867. """
  868. with tempfile.TemporaryDirectory() as tmpdir:
  869. config_path = Path(tmpdir) / "config.toml"
  870. config_path.write_text(config_toml)
  871. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  872. cfg = load_config(str(config_path))
  873. assert cfg.speaker.provider == "cpu"
  874. assert "Unknown speaker provider" in caplog.text
  875. assert "bogus" in caplog.text
  876. def test_debug_defaults():
  877. """The [debug] section defaults to disabled with signal-log defaults."""
  878. with tempfile.TemporaryDirectory() as tmpdir:
  879. import os
  880. old_cwd = os.getcwd()
  881. try:
  882. os.chdir(tmpdir)
  883. cfg = load_config()
  884. finally:
  885. os.chdir(old_cwd)
  886. assert cfg.debug.enabled is False
  887. assert cfg.debug.dir == "troubleshoot/debug"
  888. assert cfg.debug.heartbeat is True
  889. assert cfg.debug.max_bytes_per_day == 50000000
  890. def test_debug_config_fields_are_parsed():
  891. """Every [debug] key is applied from TOML."""
  892. config_toml = """
  893. [debug]
  894. enabled = true
  895. dir = "dbg"
  896. heartbeat = false
  897. max_bytes_per_day = 1000
  898. """
  899. with tempfile.TemporaryDirectory() as tmpdir:
  900. config_path = Path(tmpdir) / "config.toml"
  901. config_path.write_text(config_toml)
  902. cfg = load_config(str(config_path))
  903. assert cfg.debug.enabled is True
  904. assert cfg.debug.dir == "dbg"
  905. assert cfg.debug.heartbeat is False
  906. assert cfg.debug.max_bytes_per_day == 1000
  907. def test_debug_section_not_warned(caplog):
  908. """An explicit [debug] section loads without an unknown-section warning."""
  909. config_toml = """
  910. [debug]
  911. enabled = false
  912. """
  913. with tempfile.TemporaryDirectory() as tmpdir:
  914. config_path = Path(tmpdir) / "config.toml"
  915. config_path.write_text(config_toml)
  916. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  917. cfg = load_config(str(config_path))
  918. assert cfg.debug.enabled is False
  919. assert "debug" not in caplog.text
  920. def test_unknown_debug_key_is_ignored():
  921. """An unknown key inside [debug] is silently ignored (per-field .get)."""
  922. config_toml = """
  923. [debug]
  924. enabled = true
  925. bogus_key = 42
  926. """
  927. with tempfile.TemporaryDirectory() as tmpdir:
  928. config_path = Path(tmpdir) / "config.toml"
  929. config_path.write_text(config_toml)
  930. cfg = load_config(str(config_path))
  931. assert cfg.debug.enabled is True
  932. assert not hasattr(cfg.debug, "bogus_key")
  933. assert cfg.debug.dir == "troubleshoot/debug"
  934. def test_unknown_top_level_still_warns(caplog):
  935. """An unknown top-level section still warns after [debug] exists."""
  936. config_toml = """
  937. [debugg]
  938. enabled = true
  939. """
  940. with tempfile.TemporaryDirectory() as tmpdir:
  941. config_path = Path(tmpdir) / "config.toml"
  942. config_path.write_text(config_toml)
  943. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  944. cfg = load_config(str(config_path))
  945. assert "debugg" in caplog.text
  946. assert cfg.debug.enabled is False