"""The LM transport ceiling tests (fix: EXTRACT hangs on slow local models). Law: a provider's transport ceiling (read timeout + retries) is CONFIG, not code — a 27b model on a busy home GPU can legitimately take >600s for a long structured output, and the silent-ollama proxy in front of it cuts at its own READ_TIMEOUT. dspy 3.4.1 routes openai/... through lm15 whose default read timeout is 600s — the wall every attempt of your 2407.3s EXTRACT call died on. Pinned here: 1. config.yaml may set per-provider `read_timeout_s` and `num_retries`; configure_lm forwards them (timeout= -> lm15 Timeouts(read=), num_retries -> the dspy retry loop, LMTransportError IS retryable). 2. Defaults stay honest: nothing configured -> dspy defaults (read 600s, retries 3), unchanged behavior for cloud. 3. The first run.log line reports the ceiling (no more silent 600s walls — you SEE the read timeout that will kill a slow call). 4. The run dies at startup on an impossible ceiling (retries<0, timeout<=0) rather than mid-run. Run: .venv/bin/python tests/lm_transport.py """ from __future__ import annotations import os import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) import floop.llm as llm from floop.llm import configure_lm CFG = Path(__file__).resolve().parent.parent / "config.yaml" def test_config_carries_ceiling(): """config.yaml supports read_timeout_s + num_retries per provider.""" import yaml cfg = yaml.safe_load(CFG.read_text())["lm"] local = cfg["local"] assert local.get("read_timeout_s") == 1800, ( "local provider should declare read_timeout_s=1800 (30min — the " "silent-ollama cut is 600s and the 27b EXTRACT runs longer)") assert local.get("num_retries") == 1, ( "local provider should declare num_retries=1 (a hung call retries " "once more, not 3 — each attempt costs up to read_timeout_s)") cloud = cfg["ollama-cloud"] assert cloud.get("read_timeout_s") == 300, ( "cloud provider should declare read_timeout_s=300 (glm-5.3-flash " "answers EXTRACT in ~3s; 300s bounds a wedged call)") print("ok: config.yaml declares per-provider read_timeout_s + num_retries") def test_configure_lm_forwards_ceiling(): """configure_lm builds the LM with timeout=/num_retries from config.""" import dspy os.environ["FLOOP_LM"] = "local" info = configure_lm() assert info["provider"] == "local" lm = llm._ACTIVE assert lm.kwargs.get("timeout") == 1800, lm.kwargs.get("timeout") assert getattr(lm, "num_retries", None) == 1, getattr(lm, "num_retries", None) # the cloud provider, in the same config, carries its own ceiling os.environ["FLOOP_LM"] = "ollama-cloud" info2 = configure_lm() assert info2["provider"] == "ollama-cloud" lm2 = llm._ACTIVE assert lm2.kwargs.get("timeout") == 300 assert getattr(lm2, "num_retries", None) == 2 del os.environ["FLOOP_LM"] print("ok: configure_lm forwards timeout= + num_retries per provider") def test_defaults_unchanged_when_unset(): """No ceiling declared -> dspy defaults (600s read, retries 3).""" import yaml src = CFG.read_text() assert "read_timeout_s" in src, "config.yaml must carry the new keys" # simulate: build an LM from a config WITHOUT the keys — the loader # must not invent values (None -> dspy default) cfg = yaml.safe_load(src)["lm"] stripped = {k: ({kk: vv for kk, vv in v.items() if kk not in ("read_timeout_s", "num_retries")} if isinstance(v, dict) else v) for k, v in cfg.items()} orig_load = llm._load_yaml llm._load_yaml = lambda: {"lm": stripped} # noqa: E731 — monkeypatch, test-only try: os.environ["FLOOP_LM"] = "local" configure_lm() lm = llm._ACTIVE assert "timeout" not in lm.kwargs, ( f"unset ceiling must not reach the LM (got {lm.kwargs.get('timeout')})") assert getattr(lm, "num_retries", None) is None or lm.num_retries == 3, ( "dspy default num_retries=3 must survive untouched") finally: llm._load_yaml = orig_load del os.environ["FLOOP_LM"] print("ok: no ceiling declared -> dspy defaults, nothing invented") def test_run_log_first_line_reports_ceiling(): """The provider dict carries the ceiling for the run.log provenance.""" os.environ["FLOOP_LM"] = "local" info = configure_lm() assert info.get("read_timeout_s") == 1800, info assert info.get("num_retries") == 1, info del os.environ["FLOOP_LM"] print("ok: configure_lm returns read_timeout_s/num_retries for the log line") if __name__ == "__main__": test_config_carries_ceiling() test_configure_lm_forwards_ceiling() test_defaults_unchanged_when_unset() test_run_log_first_line_reports_ceiling() print("\nlm_transport: 4/4 ok")