| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172 |
- """Tests for configuration loading."""
- import importlib.util
- import tempfile
- import tomllib
- from pathlib import Path
- from stt_daemon.config import load_config
- REPO_ROOT = Path(__file__).resolve().parents[1]
- LAYA_REV = "970dc8c5f63d7b886a68409493f37d569424f933"
- def test_load_config_defaults():
- """Test that load_config returns defaults when no file exists."""
- with tempfile.TemporaryDirectory() as tmpdir:
- # Change to temp dir and load (no config.toml exists)
- import os
- old_cwd = os.getcwd()
- try:
- os.chdir(tmpdir)
- cfg = load_config()
- assert cfg.audio.backend == "parec"
- assert cfg.audio.device == "VirtualMicSink.monitor"
- assert cfg.audio.sample_rate == 16000
- assert cfg.vad.aggressiveness == 2
- assert cfg.whisper.model == "small"
- assert cfg.whisper.language is None
- assert cfg.output.stdout is True
- assert cfg.output.jsonl is False
- finally:
- os.chdir(old_cwd)
- def test_load_config_from_file():
- """Test loading config from a TOML file."""
- config_toml = """
- [audio]
- backend = "sounddevice"
- device = "hw:0"
- sample_rate = 44100
- [vad]
- aggressiveness = 3
- pre_roll_ms = 500
- [whisper]
- model = "base"
- device = "cuda"
- language = "es"
- [output]
- stdout = false
- jsonl = true
- session_dir = "logs"
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.audio.backend == "sounddevice"
- assert cfg.audio.device == "hw:0"
- assert cfg.audio.sample_rate == 44100
- assert cfg.vad.aggressiveness == 3
- assert cfg.vad.pre_roll_ms == 500
- assert cfg.whisper.model == "base"
- assert cfg.whisper.device == "cuda"
- assert cfg.whisper.language == "es"
- assert cfg.output.stdout is False
- assert cfg.output.jsonl is True
- assert cfg.output.session_dir == "logs"
- def test_language_normalization_auto():
- """Test that language 'auto' is normalized to None."""
- config_toml = """
- [whisper]
- language = "auto"
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.whisper.language is None
- def test_language_normalization_empty_string():
- """Test that language '' is normalized to None."""
- config_toml = """
- [whisper]
- language = ""
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.whisper.language is None
- def test_language_normalization_none():
- """Test that language 'none' is normalized to None."""
- config_toml = """
- [whisper]
- language = "none"
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.whisper.language is None
- def test_language_explicit_en():
- """Test that explicit language codes are preserved."""
- config_toml = """
- [whisper]
- language = "en"
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.whisper.language == "en"
- def test_partial_config():
- """Test that partial configs merge with defaults."""
- config_toml = """
- [audio]
- device = "custom_device"
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- # Overridden
- assert cfg.audio.device == "custom_device"
- # Defaults
- assert cfg.audio.backend == "parec"
- assert cfg.audio.sample_rate == 16000
- assert cfg.vad.aggressiveness == 2
- def test_chunking_and_confidence_fields():
- """New chunking/gate fields load from TOML."""
- config_toml = """
- [vad]
- chunk_pause_ms = 500
- soft_max_s = 8.0
- rms_floor = 0.01
- trailing_trim_ms = 150
- [whisper]
- no_speech_prob_max = 0.7
- avg_logprob_min = -0.8
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.vad.chunk_pause_ms == 500
- assert cfg.vad.soft_max_s == 8.0
- assert cfg.vad.rms_floor == 0.01
- assert cfg.vad.trailing_trim_ms == 150
- assert cfg.whisper.no_speech_prob_max == 0.7
- assert cfg.whisper.avg_logprob_min == -0.8
- def test_chunking_defaults():
- """New fields have sane defaults."""
- with tempfile.TemporaryDirectory() as tmpdir:
- import os
- old_cwd = os.getcwd()
- try:
- os.chdir(tmpdir)
- cfg = load_config()
- finally:
- os.chdir(old_cwd)
- assert cfg.vad.chunk_pause_ms == 350
- assert cfg.vad.soft_max_s == 10.0
- assert cfg.vad.rms_floor == 0.008
- assert cfg.vad.trailing_trim_ms == 200
- assert cfg.whisper.no_speech_prob_max == 0.6
- assert cfg.whisper.avg_logprob_min == -1.0
- def test_output_defaults_disable_audio_saving():
- with tempfile.TemporaryDirectory() as tmpdir:
- import os
- old_cwd = os.getcwd()
- try:
- os.chdir(tmpdir)
- cfg = load_config()
- finally:
- os.chdir(old_cwd)
- assert cfg.output.save_audio is False
- assert cfg.output.session_dir == "transcripts"
- assert cfg.output.session_gap_min == 5.0
- assert not hasattr(cfg.output, "jsonl_dir")
- assert not hasattr(cfg.output, "audio_dir")
- def test_output_save_audio_fields():
- config_toml = """
- [output]
- save_audio = true
- session_dir = "clips"
- session_gap_min = 12.0
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.output.save_audio is True
- assert cfg.output.session_dir == "clips"
- assert cfg.output.session_gap_min == 12.0
- def test_legacy_output_and_brief_keys_ignored():
- """Retired keys in a legacy TOML load without error and keep the new defaults."""
- config_toml = """
- [output]
- jsonl_dir = "logs"
- audio_dir = "clips"
- [brief]
- write = false
- dir = "briefing"
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.output.session_dir == "transcripts"
- assert cfg.output.session_gap_min == 5.0
- assert cfg.brief.write is False
- assert not hasattr(cfg.output, "jsonl_dir")
- assert not hasattr(cfg.output, "audio_dir")
- assert not hasattr(cfg.brief, "dir")
- assert not hasattr(cfg.brief, "filename")
- def test_daemon_defaults():
- with tempfile.TemporaryDirectory() as tmpdir:
- import os
- old_cwd = os.getcwd()
- try:
- os.chdir(tmpdir)
- cfg = load_config()
- finally:
- os.chdir(old_cwd)
- assert cfg.daemon.queue_size == 50
- assert cfg.daemon.backlog_warn_at == 5
- def test_daemon_config_fields():
- config_toml = """
- [daemon]
- queue_size = 20
- backlog_warn_at = 3
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.daemon.queue_size == 20
- assert cfg.daemon.backlog_warn_at == 3
- def test_unknown_sections_warn_and_are_ignored(caplog):
- """A typo'd section like [assitant] must warn, not silently use defaults."""
- config_toml = """
- [assitant]
- require_viewer = false
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- with caplog.at_level("WARNING", logger="stt_daemon.config"):
- cfg = load_config(str(config_path))
- assert "assitant" in caplog.text
- assert cfg.assistant.require_viewer is True
- def test_cpu_threads_default_is_auto():
- """cpu_threads defaults to 0 (CTranslate2's automatic choice)."""
- with tempfile.TemporaryDirectory() as tmpdir:
- import os
- old_cwd = os.getcwd()
- try:
- os.chdir(tmpdir)
- cfg = load_config()
- finally:
- os.chdir(old_cwd)
- assert cfg.whisper.cpu_threads == 0
- def test_cpu_threads_config_field():
- config_toml = """
- [whisper]
- cpu_threads = 16
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.whisper.cpu_threads == 16
- def test_language_strategy_defaults_to_static():
- """language_strategy defaults to 'static'."""
- with tempfile.TemporaryDirectory() as tmpdir:
- import os
- old_cwd = os.getcwd()
- try:
- os.chdir(tmpdir)
- cfg = load_config()
- finally:
- os.chdir(old_cwd)
- assert cfg.whisper.language_strategy == "static"
- def test_language_strategy_sticky_is_parsed():
- """language_strategy 'sticky' is parsed correctly."""
- config_toml = """
- [whisper]
- language_strategy = "sticky"
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.whisper.language_strategy == "sticky"
- def test_unknown_language_strategy_falls_back_to_static(caplog):
- """An unknown language_strategy value falls back to 'static' with a warning."""
- config_toml = """
- [whisper]
- language_strategy = "bogus"
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- with caplog.at_level("WARNING", logger="stt_daemon.config"):
- cfg = load_config(str(config_path))
- assert cfg.whisper.language_strategy == "static"
- assert "Unknown language_strategy" in caplog.text
- assert "bogus" in caplog.text
- def test_sticky_redetect_logprob_defaults_to_minus_0_8():
- """sticky_redetect_logprob defaults to -0.8."""
- with tempfile.TemporaryDirectory() as tmpdir:
- import os
- old_cwd = os.getcwd()
- try:
- os.chdir(tmpdir)
- cfg = load_config()
- finally:
- os.chdir(old_cwd)
- assert cfg.whisper.sticky_redetect_logprob == -0.8
- def test_sticky_redetect_logprob_is_parsed():
- """sticky_redetect_logprob is parsed from TOML."""
- config_toml = """
- [whisper]
- sticky_redetect_logprob = -0.5
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.whisper.sticky_redetect_logprob == -0.5
- def test_llm_context_and_compaction_defaults():
- with tempfile.TemporaryDirectory() as tmpdir:
- import os
- old_cwd = os.getcwd()
- try:
- os.chdir(tmpdir)
- cfg = load_config()
- finally:
- os.chdir(old_cwd)
- assert cfg.llm.max_context_tokens == 131072
- assert cfg.llm.compaction_safety_margin == 256
- assert cfg.llm.compaction_enabled is True
- assert cfg.llm.compaction_trigger_ratio == 0.8
- assert cfg.llm.compaction_keep_turns == 6
- assert cfg.llm.compaction_max_tokens == 512
- assert cfg.llm.compaction_timeout_s == 30.0
- assert cfg.llm.compaction_input_tokens == 8192
- assert cfg.llm.inference_queue_size == 8
- assert cfg.llm.prompt_budget == 131072 - 1024 - 256
- assert not hasattr(cfg.llm, "max_prompt_tokens")
- def test_llm_context_and_compaction_fields_parsed():
- config_toml = """
- [llm]
- max_context_tokens = 8192
- max_tokens = 2048
- compaction_safety_margin = 128
- compaction_enabled = false
- compaction_trigger_ratio = 0.5
- compaction_keep_turns = 2
- compaction_max_tokens = 256
- compaction_timeout_s = 12.5
- compaction_input_tokens = 4096
- inference_queue_size = 3
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.llm.max_context_tokens == 8192
- assert cfg.llm.compaction_safety_margin == 128
- assert cfg.llm.compaction_enabled is False
- assert cfg.llm.compaction_trigger_ratio == 0.5
- assert cfg.llm.compaction_keep_turns == 2
- assert cfg.llm.compaction_max_tokens == 256
- assert cfg.llm.compaction_timeout_s == 12.5
- assert cfg.llm.compaction_input_tokens == 4096
- assert cfg.llm.inference_queue_size == 3
- assert cfg.llm.prompt_budget == 8192 - 2048 - 128
- def test_llm_auth_and_thinking_fields_parsed():
- config_toml = """
- [llm]
- model = "gpt-4o-mini"
- api_key_env = "OPENAI_API_KEY"
- session_id = "stt-chat"
- session_header = "x-opencode-session"
- user_agent = "stt-daemon/1.0"
- reasoning_effort = "high"
- extra_body = { enable_thinking = true, top_k = 20 }
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.llm.model == "gpt-4o-mini"
- assert cfg.llm.api_key_env == "OPENAI_API_KEY"
- assert cfg.llm.session_id == "stt-chat"
- assert cfg.llm.session_header == "x-opencode-session"
- assert cfg.llm.user_agent == "stt-daemon/1.0"
- assert cfg.llm.reasoning_effort == "high"
- assert cfg.llm.extra_body == {"enable_thinking": True, "top_k": 20}
- def test_llm_auth_and_thinking_defaults():
- with tempfile.TemporaryDirectory() as tmpdir:
- import os
- old_cwd = os.getcwd()
- try:
- os.chdir(tmpdir)
- cfg = load_config()
- finally:
- os.chdir(old_cwd)
- assert cfg.llm.model == ""
- assert cfg.llm.api_key_env == ""
- assert cfg.llm.session_id == ""
- assert cfg.llm.session_header == ""
- assert cfg.llm.user_agent == ""
- assert cfg.llm.reasoning_effort == ""
- assert cfg.llm.extra_body == {}
- def test_max_prompt_tokens_is_deprecated_and_ignored(caplog):
- config_toml = """
- [llm]
- max_prompt_tokens = 50
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- with caplog.at_level("WARNING", logger="stt_daemon.config"):
- cfg = load_config(str(config_path))
- assert "max_prompt_tokens" in caplog.text
- assert "deprecated" in caplog.text
- assert cfg.llm.prompt_budget == 131072 - 1024 - 256
- def test_backend_defaults_to_faster_whisper():
- """backend defaults to 'faster-whisper'."""
- with tempfile.TemporaryDirectory() as tmpdir:
- import os
- old_cwd = os.getcwd()
- try:
- os.chdir(tmpdir)
- cfg = load_config()
- finally:
- os.chdir(old_cwd)
- assert cfg.whisper.backend == "faster-whisper"
- def test_backend_pywhispercpp_is_parsed():
- """backend 'pywhispercpp' is parsed from TOML."""
- config_toml = """
- [whisper]
- backend = "pywhispercpp"
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.whisper.backend == "pywhispercpp"
- def test_backend_is_normalized():
- """Backend names are normalized: stripped, lowercased, underscores to dashes."""
- config_toml = """
- [whisper]
- backend = " Faster_Whisper "
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.whisper.backend == "faster-whisper"
- def test_unknown_backend_falls_back_to_faster_whisper(caplog):
- """An unknown backend value falls back to 'faster-whisper' with a warning."""
- config_toml = """
- [whisper]
- backend = "bogus"
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- with caplog.at_level("WARNING", logger="stt_daemon.config"):
- cfg = load_config(str(config_path))
- assert cfg.whisper.backend == "faster-whisper"
- assert "Unknown whisper backend" in caplog.text
- assert "bogus" in caplog.text
- def test_triage_defaults():
- """The [triage] section defaults match the plan's surviving-key list."""
- with tempfile.TemporaryDirectory() as tmpdir:
- import os
- old_cwd = os.getcwd()
- try:
- os.chdir(tmpdir)
- cfg = load_config()
- finally:
- os.chdir(old_cwd)
- assert cfg.triage.enabled is False
- assert cfg.triage.questions_file == "questions.json"
- assert cfg.triage.window == 500
- assert cfg.triage.state_char_budget == 3000
- assert cfg.triage.periodic_s == 30.0
- assert cfg.triage.predict_max_tokens == 1024
- assert cfg.triage.confidence_field == "answer_confidence"
- assert cfg.triage.escalate_threshold == 0.6
- assert cfg.triage.choice_min_confidence == 0.85
- assert cfg.triage.extract_enabled is True
- assert cfg.triage.extract_max_tokens == 3072
- assert cfg.triage.extract_timeout_s == 30.0
- assert cfg.triage.brief_slot_id is None
- assert cfg.triage.brief_gate_enabled is True
- assert cfg.triage.brief_gate_threshold == 0.5
- assert cfg.triage.brief_max_chars == 4000
- assert cfg.triage.brief_pending_char_budget == 3000
- assert cfg.triage.ledger_max_facts == 200
- assert cfg.triage.fact_max_chars == 240
- assert cfg.triage.device == ""
- assert cfg.triage.preload == ["multilingual"]
- assert cfg.triage.default_checkpoint == "multilingual"
- def test_triage_config_fields_parsed():
- """Every surviving [triage] key is applied from TOML."""
- config_toml = """
- [triage]
- enabled = true
- questions_file = "custom_questions.json"
- window = 4
- state_char_budget = 800
- predict_max_tokens = 768
- confidence_field = "confidence"
- escalate_threshold = 0.7
- choice_min_confidence = 0.9
- extract_enabled = false
- extract_max_tokens = 256
- extract_timeout_s = 12.5
- device = "cuda"
- preload = ["multilingual", "english"]
- default_checkpoint = "english"
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.triage.enabled is True
- assert cfg.triage.questions_file == "custom_questions.json"
- assert cfg.triage.window == 4
- assert cfg.triage.state_char_budget == 800
- assert cfg.triage.predict_max_tokens == 768
- assert cfg.triage.confidence_field == "confidence"
- assert cfg.triage.escalate_threshold == 0.7
- assert cfg.triage.choice_min_confidence == 0.9
- assert cfg.triage.extract_enabled is False
- assert cfg.triage.extract_max_tokens == 256
- assert cfg.triage.extract_timeout_s == 12.5
- assert cfg.triage.device == "cuda"
- assert cfg.triage.preload == ["multilingual", "english"]
- assert cfg.triage.default_checkpoint == "english"
- def test_llm_slot_and_brief_prompt_defaults():
- """Slots default to None; the brief prompts match the plan verbatim."""
- with tempfile.TemporaryDirectory() as tmpdir:
- import os
- old_cwd = os.getcwd()
- try:
- os.chdir(tmpdir)
- cfg = load_config()
- finally:
- os.chdir(old_cwd)
- assert cfg.llm.chat_slot_id is None
- assert cfg.llm.command_slot_id is None
- assert cfg.llm.triage_system_prompt == (
- "You maintain a living brief of a business conversation. The caller sends "
- "CURRENT LEDGER plus CURRENT BRIEF plus NEW UTTERANCES. Reply with exactly "
- "two sections in this order: NEW FACTS: first, then BRIEF:. Facts go first "
- "so a truncated reply still preserves extraction (truncation safety). In "
- "NEW FACTS:, emit one fact per line as '- <fact>', or the single line "
- "'none' when the new utterances add no new facts. Never repeat facts "
- "already listed in CURRENT LEDGER. In BRIEF:, return the UPDATED brief as "
- "plain text only, with no markdown fences and no JSON. Open with a Summary "
- "section: a rolling overview of the conversation so far, one to a few "
- "sentences for a short session and growing to a short paragraph or two as "
- "the session extends. Preserve prior content unless the new utterances "
- "contradict it; merge duplicates; prune stale items; keep owners, "
- "responsibilities, and deadlines explicit. Use short sections: Summary, "
- "Opportunities, Blockers/Risks, Responsibilities, Decisions, Action Items, "
- "Deadlines, Open Questions. Never silently delete Action Items, Open "
- "Questions, or Decisions lines. When an action completes or a question "
- "is answered, do NOT drop the line — move it to Decisions as "
- "'Done: <original> — resolved: <what happened>' (or keep it in place "
- "with a 'Done (...)' suffix) AND list the resolution as a NEW FACTS "
- "bullet. Prune only exact duplicates, never resolved history. "
- "Never emit a 'Prior Details:' section. Output "
- "the two sections directly; never emit tool calls, function-call syntax, "
- "or special wrapper tokens."
- )
- assert cfg.llm.brief_compaction_system_prompt == (
- "You compress a triage briefing conversation. Preserve every opportunity, blocker, "
- "owner, responsibility, decision, deadline, and open question; drop pleasantries. "
- "Output only the summary."
- )
- def test_absent_slot_ids_stay_none():
- """Present sections with the slot keys absent leave every slot id None."""
- config_toml = """
- [llm]
- temperature = 0.5
- [triage]
- window = 12
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.llm.chat_slot_id is None
- assert cfg.llm.command_slot_id is None
- assert cfg.triage.brief_slot_id is None
- def test_slot_id_zero_is_preserved():
- """A slot id of 0 is valid and must load as 0, not None."""
- config_toml = """
- [llm]
- chat_slot_id = 0
- command_slot_id = 0
- [triage]
- brief_slot_id = 1
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.llm.chat_slot_id == 0
- assert cfg.llm.command_slot_id == 0
- assert cfg.triage.brief_slot_id == 1
- def test_non_numeric_slot_degrades_to_none():
- """A non-numeric slot value degrades to None without raising."""
- config_toml = """
- [llm]
- chat_slot_id = "abc"
- [triage]
- brief_slot_id = "nope"
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.llm.chat_slot_id is None
- assert cfg.triage.brief_slot_id is None
- def test_triage_brief_keys_are_parsed():
- """Every new [triage] brief key is applied from TOML."""
- config_toml = """
- [triage]
- brief_slot_id = 7
- brief_gate_enabled = false
- brief_gate_threshold = 0.8
- brief_max_chars = 1234
- brief_pending_char_budget = 777
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.triage.brief_slot_id == 7
- assert cfg.triage.brief_gate_enabled is False
- assert cfg.triage.brief_gate_threshold == 0.8
- assert cfg.triage.brief_max_chars == 1234
- assert cfg.triage.brief_pending_char_budget == 777
- def test_triage_periodic_key_is_parsed():
- """The [triage] periodic cadence is applied from TOML."""
- config_toml = """
- [triage]
- periodic_s = 12.5
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.triage.periodic_s == 12.5
- def test_triage_ledger_keys_are_parsed():
- """The [triage] ledger cap keys are applied from TOML."""
- config_toml = """
- [triage]
- ledger_max_facts = 42
- fact_max_chars = 111
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.triage.ledger_max_facts == 42
- assert cfg.triage.fact_max_chars == 111
- def test_brief_defaults():
- """The [brief] section defaults to no on-disk writing."""
- with tempfile.TemporaryDirectory() as tmpdir:
- import os
- old_cwd = os.getcwd()
- try:
- os.chdir(tmpdir)
- cfg = load_config()
- finally:
- os.chdir(old_cwd)
- assert cfg.brief.write is False
- assert not hasattr(cfg.brief, "dir")
- assert not hasattr(cfg.brief, "filename")
- def test_brief_config_fields_are_parsed():
- """The [brief] write key is applied from TOML."""
- config_toml = """
- [brief]
- write = true
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.brief.write is True
- assert not hasattr(cfg.brief, "dir")
- assert not hasattr(cfg.brief, "filename")
- def test_brief_write_false_is_parsed(caplog):
- """An explicit [brief] write=false loads as False and is not an unknown section."""
- config_toml = """
- [brief]
- write = false
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- with caplog.at_level("WARNING", logger="stt_daemon.config"):
- cfg = load_config(str(config_path))
- assert cfg.brief.write is False
- assert "brief" not in caplog.text
- def test_triage_dump_io_defaults_to_false():
- """[triage].dump_io is opt-in: False unless explicitly enabled."""
- with tempfile.TemporaryDirectory() as tmpdir:
- import os
- old_cwd = os.getcwd()
- try:
- os.chdir(tmpdir)
- cfg = load_config()
- finally:
- os.chdir(old_cwd)
- assert cfg.triage.dump_io is False
- def test_triage_dump_io_is_parsed():
- """[triage].dump_io = true is applied from TOML."""
- config_toml = """
- [triage]
- dump_io = true
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.triage.dump_io is True
- def test_unknown_triag_section_warns_and_keeps_defaults(caplog):
- """A typo'd [triag] section must warn and never raise; triage keeps defaults."""
- config_toml = """
- [triag]
- enabled = true
- window = 3
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- with caplog.at_level("WARNING", logger="stt_daemon.config"):
- cfg = load_config(str(config_path))
- assert "triag" in caplog.text
- assert cfg.triage.enabled is False
- assert cfg.triage.window == 500
- def test_triage_config_has_no_struck_keys():
- """The v5 keys are deliberately gone from TriageConfig (superseded/removed)."""
- from stt_daemon.config import TriageConfig
- struck = {"socket", "model", "predict_timeout_s", "checkpoint", "subfolder", "max_len"}
- for name in struck:
- assert not hasattr(TriageConfig, name), f"TriageConfig must not expose {name!r}"
- def test_llm_triage_system_prompt_is_parsed():
- """[llm].triage_system_prompt overrides the JSON-only extractor default."""
- config_toml = """
- [llm]
- triage_system_prompt = "ONLY JSON, nothing else."
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.llm.triage_system_prompt == "ONLY JSON, nothing else."
- def test_no_stt_laya_script():
- """Triage is in-process only: no stt-laya console script may ever be added."""
- with (REPO_ROOT / "pyproject.toml").open("rb") as fh:
- data = tomllib.load(fh)
- scripts = data["project"]["scripts"]
- assert set(scripts) == {"stt-daemon", "stt-assistant", "stt-ctl", "stt-tui"}
- assert not any("laya" in name for name in scripts)
- def test_layasvc_module_not_importable():
- """The retired external laya service package must not exist (in-process lane)."""
- assert importlib.util.find_spec("stt_daemon.layasvc") is None
- def test_triage_extra_pins_laya_revision():
- """The triage extra pins laya to the exact verified upstream revision."""
- with (REPO_ROOT / "pyproject.toml").open("rb") as fh:
- data = tomllib.load(fh)
- assert data["project"]["optional-dependencies"]["triage"] == ["laya"]
- source = data["tool"]["uv"]["sources"]["laya"]
- assert source["git"] == "https://github.com/NandhaKishorM/laya.git"
- assert source["rev"] == LAYA_REV
- def test_questions_json_gitignored():
- """The user's private questions.json is never committed (like config.toml)."""
- lines = (REPO_ROOT / ".gitignore").read_text().splitlines()
- assert "questions.json" in lines
- def test_speaker_defaults():
- """The [speaker] section defaults match the plan's 14-key list."""
- with tempfile.TemporaryDirectory() as tmpdir:
- import os
- old_cwd = os.getcwd()
- try:
- os.chdir(tmpdir)
- cfg = load_config()
- finally:
- os.chdir(old_cwd)
- assert cfg.speaker.enabled is False
- assert cfg.speaker.model_path is None
- assert cfg.speaker.provider == "cpu"
- assert cfg.speaker.threshold_id == 0.60
- assert cfg.speaker.threshold_new == 0.45
- assert cfg.speaker.min_enroll_s == 2.0
- assert cfg.speaker.rms_floor == 0.0
- assert cfg.speaker.margin == 0.04
- assert cfg.speaker.mint_cooldown_s == 60.0
- assert cfg.speaker.confirm_s == 12.0
- assert cfg.speaker.max_prototypes == 8
- assert cfg.speaker.provisional_ttl_h == 24.0
- assert cfg.speaker.store_dir == "speakers"
- assert cfg.speaker.join_timeout_s == 2.0
- def test_speaker_config_fields_parsed():
- """Every [speaker] key is applied from TOML."""
- config_toml = """
- [speaker]
- enabled = true
- model_path = "models/nemo_en_titanet_small.onnx"
- provider = "cuda"
- threshold_id = 0.7
- threshold_new = 0.5
- min_enroll_s = 3.0
- rms_floor = 0.01
- margin = 0.05
- mint_cooldown_s = 30.0
- confirm_s = 8.0
- max_prototypes = 4
- provisional_ttl_h = 12.0
- store_dir = "voiceprints"
- join_timeout_s = 1.5
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.speaker.enabled is True
- assert cfg.speaker.model_path == "models/nemo_en_titanet_small.onnx"
- assert cfg.speaker.provider == "cuda"
- assert cfg.speaker.threshold_id == 0.7
- assert cfg.speaker.threshold_new == 0.5
- assert cfg.speaker.min_enroll_s == 3.0
- assert cfg.speaker.rms_floor == 0.01
- assert cfg.speaker.margin == 0.05
- assert cfg.speaker.mint_cooldown_s == 30.0
- assert cfg.speaker.confirm_s == 8.0
- assert cfg.speaker.max_prototypes == 4
- assert cfg.speaker.provisional_ttl_h == 12.0
- assert cfg.speaker.store_dir == "voiceprints"
- assert cfg.speaker.join_timeout_s == 1.5
- def test_unknown_speaker_key_is_ignored():
- """An unknown key inside [speaker] is silently ignored (per-field .get)."""
- config_toml = """
- [speaker]
- enabled = true
- bogus_key = 42
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.speaker.enabled is True
- assert not hasattr(cfg.speaker, "bogus_key")
- assert cfg.speaker.provider == "cpu"
- def test_unknown_speaker_provider_falls_back_to_cpu(caplog):
- """An unknown speaker provider falls back to 'cpu' with a warning."""
- config_toml = """
- [speaker]
- provider = "bogus"
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- with caplog.at_level("WARNING", logger="stt_daemon.config"):
- cfg = load_config(str(config_path))
- assert cfg.speaker.provider == "cpu"
- assert "Unknown speaker provider" in caplog.text
- assert "bogus" in caplog.text
- def test_debug_defaults():
- """The [debug] section defaults to disabled with signal-log defaults."""
- with tempfile.TemporaryDirectory() as tmpdir:
- import os
- old_cwd = os.getcwd()
- try:
- os.chdir(tmpdir)
- cfg = load_config()
- finally:
- os.chdir(old_cwd)
- assert cfg.debug.enabled is False
- assert cfg.debug.dir == "troubleshoot/debug"
- assert cfg.debug.heartbeat is True
- assert cfg.debug.max_bytes_per_day == 50000000
- def test_debug_config_fields_are_parsed():
- """Every [debug] key is applied from TOML."""
- config_toml = """
- [debug]
- enabled = true
- dir = "dbg"
- heartbeat = false
- max_bytes_per_day = 1000
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.debug.enabled is True
- assert cfg.debug.dir == "dbg"
- assert cfg.debug.heartbeat is False
- assert cfg.debug.max_bytes_per_day == 1000
- def test_debug_section_not_warned(caplog):
- """An explicit [debug] section loads without an unknown-section warning."""
- config_toml = """
- [debug]
- enabled = false
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- with caplog.at_level("WARNING", logger="stt_daemon.config"):
- cfg = load_config(str(config_path))
- assert cfg.debug.enabled is False
- assert "debug" not in caplog.text
- def test_unknown_debug_key_is_ignored():
- """An unknown key inside [debug] is silently ignored (per-field .get)."""
- config_toml = """
- [debug]
- enabled = true
- bogus_key = 42
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.debug.enabled is True
- assert not hasattr(cfg.debug, "bogus_key")
- assert cfg.debug.dir == "troubleshoot/debug"
- def test_unknown_top_level_still_warns(caplog):
- """An unknown top-level section still warns after [debug] exists."""
- config_toml = """
- [debugg]
- enabled = true
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- with caplog.at_level("WARNING", logger="stt_daemon.config"):
- cfg = load_config(str(config_path))
- assert "debugg" in caplog.text
- assert cfg.debug.enabled is False
|