Files
stack/tests/llm/test_policy_api.py
kert 9a266b0fd0
Some checks failed
CI / lint (push) Successful in 1m24s
CI / notebooks-smoke (push) Successful in 2m8s
Deploy / changes (push) Successful in 21s
CI / test (push) Failing after 5m27s
Infra CI / notebooks (push) Successful in 1m38s
Infra CI / zotero (push) Successful in 38s
Infra CI / docs (push) Successful in 48s
Infra CI / api (push) Successful in 3m4s
Infra CI / bcda (push) Successful in 4m6s
Infra CI / mc (push) Successful in 35s
Deploy / notebooks (push) Has been skipped
Deploy / zotero (push) Has been skipped
Deploy / docs (push) Has been skipped
Infra CI / llm (push) Successful in 2m9s
Deploy / api (push) Successful in 1m40s
Deploy / bcda (push) Successful in 2m6s
Deploy / mc (push) Has been skipped
Deploy / llm (push) Successful in 2m0s
Deploy / report (push) Successful in 22s
feat(llm): /api/policy — the dossier sliced and cached per replica, ETag/304 (lex S1)
2026-11-13 02:36:24 -05:00

301 lines
10 KiB
Python

"""The policy API (lex S1 Task 3): router, per-mtime cache, ETag/304, errors."""
from __future__ import annotations
import dataclasses
import os
import duckdb
import pytest
from fastapi.testclient import TestClient
from llm import evidence, policy
from llm.api import app
from llm.config import LlmConfig
from llm.policy_slices import SECTIONS
from pfs.codetables import ensure_tables
from pfs.dossier import read_dossier, write_dossier
from tests.llm.policy_fixture import fixture_dossier
CFG = LlmConfig(
ollama_hosts=("http://h1:11434",),
embed_model="e",
instruct_model="c",
embed_dim=768,
build_ann_index=False,
pg_host="x",
pg_port=5432,
pg_db="llm",
pg_user="llm",
duckdb_replica="/nonexistent/aco.ro.duckdb",
)
URL = "/api/policy/dossier/{}"
HV = {"family": "HOME-VISITS"}
def _write(path, *dossiers, sql=None):
# a republish replaces the file; drop the cached read-only handle first
if evidence._REPLICA is not None:
evidence._REPLICA[2].close()
evidence._REPLICA = None
con = duckdb.connect(str(path))
ensure_tables(con)
for d in dossiers:
write_dossier(con, d)
if sql:
con.execute(sql)
con.close()
@pytest.fixture
def replica(tmp_path, monkeypatch):
# hermetic: opening the replica must not rewrite the global registry
monkeypatch.setattr(evidence, "refresh_from", lambda con: 0)
path = tmp_path / "aco.ro.duckdb"
_write(path, fixture_dossier())
app.dependency_overrides[policy.config_dep] = lambda: dataclasses.replace(
CFG, duckdb_replica=str(path)
)
evidence._REPLICA = None
policy.clear_cache()
yield path
app.dependency_overrides.clear()
evidence._REPLICA = None
policy.clear_cache()
@pytest.fixture
def client(replica):
return TestClient(app)
def _count_reads(monkeypatch):
calls = []
def counting(con, family):
calls.append(family)
return read_dossier(con, family)
monkeypatch.setattr("pfs.dossier.read_dossier", counting)
return calls
def test_section_ok_etag_and_304(client):
r = client.get(URL.format("header"), params=HV)
assert r.status_code == 200
assert r.json()["family"] == "HOME-VISITS"
assert r.headers["cache-control"] == "no-cache"
assert r.headers["content-type"] == "application/json"
etag = r.headers["etag"]
assert etag.startswith('"') and len(etag) == 22
r2 = client.get(URL.format("header"), params=HV, headers={"If-None-Match": etag})
assert r2.status_code == 304 and r2.content == b""
assert r2.headers["etag"] == etag
r3 = client.get(
URL.format("header"), params=HV, headers={"If-None-Match": "W/" + etag}
)
assert r3.status_code == 304
r4 = client.get(URL.format("header"), params=HV, headers={"If-None-Match": '"x"'})
assert r4.status_code == 200
r5 = client.get(URL.format("header"), params=HV, headers={"If-None-Match": "*"})
assert r5.status_code == 304
@pytest.mark.parametrize("section", [s for s in SECTIONS if s != "sources"])
def test_every_built_section_serves(client, section):
r = client.get(URL.format(section), params=HV)
assert r.status_code == 200
assert r.json()["family"] == "HOME-VISITS"
def test_sources_section_not_built_yet(client):
r = client.get(URL.format("sources"), params=HV)
assert r.status_code == 404
assert r.json()["detail"]["section"] == "sources"
def test_header_tier_and_book(client):
h = client.get(URL.format("header"), params=HV).json()
assert h["tier"] == "reviewed"
assert h["phase"] is not None
def test_machine_tier_for_uncurated_family(replica, client, monkeypatch):
_write(replica, dataclasses.replace(fixture_dossier(), family="ZZZ-MACH"))
r = client.get(URL.format("header"), params={"family": "zzz-mach"})
assert r.status_code == 200
h = r.json()
assert h["tier"] == "machine" and h["phase"] is None and h["domains"] == []
def test_lowercase_family_normalized(client):
r = client.get(URL.format("header"), params={"family": "home-visits"})
assert r.status_code == 200
assert r.json()["family"] == "HOME-VISITS"
def test_bad_family_400(client):
for bad in ("a b", "../x", ""):
r = client.get(URL.format("header"), params={"family": bad})
assert r.status_code == 400, bad
assert r.json()["detail"]["message"] == "invalid family key"
assert r.json()["detail"]["family"] == bad
assert client.get(URL.format("header")).status_code == 422
def test_unknown_section_404(client):
r = client.get(URL.format("nope"), params=HV)
assert r.status_code == 404
assert r.json()["detail"]["sections"] == list(SECTIONS)
def test_no_dossier_404_with_status(client, monkeypatch):
r = client.get(URL.format("header"), params={"family": "CCM"})
assert r.status_code == 404
d = r.json()["detail"]
assert d["status"] == "planned" and d["family"] == "CCM"
assert d["message"] == "No dossier is built for CCM yet."
from pfs.curated import load_curated
real = load_curated()
monkeypatch.setattr(
"pfs.curated.load_curated", lambda *a, **k: {**real, "ZZZ-CUR": 1}
)
r = client.get(URL.format("header"), params={"family": "ZZZ-CUR"})
assert r.status_code == 404 and r.json()["detail"]["status"] == "curated"
r = client.get(URL.format("header"), params={"family": "ZZZ-NONE"})
assert r.status_code == 404 and r.json()["detail"]["status"] == "unknown"
def test_derived_status(replica, client, monkeypatch):
from pfs.families import FAMILIES
monkeypatch.setitem(FAMILIES, "ZZZ-REG", object())
r = client.get(URL.format("header"), params={"family": "ZZZ-REG"})
assert r.json()["detail"]["status"] == "derived"
def test_slash_family_key_is_valid(client):
r = client.get(URL.format("header"), params={"family": "A/D-TX-PROGRAM-HOUR"})
assert r.status_code == 404
assert r.json()["detail"]["family"] == "A/D-TX-PROGRAM-HOUR"
def test_replica_missing_503(tmp_path, client):
app.dependency_overrides[policy.config_dep] = lambda: dataclasses.replace(
CFG, duckdb_replica=str(tmp_path / "gone.duckdb")
)
r = client.get(URL.format("header"), params=HV)
assert r.status_code == 503
assert r.json()["detail"]["message"] == "The data replica is not available."
def test_replica_unopenable_503(tmp_path, client):
bad = tmp_path / "bad.duckdb"
bad.write_bytes(b"this is not a duckdb file" * 100)
app.dependency_overrides[policy.config_dep] = lambda: dataclasses.replace(
CFG, duckdb_replica=str(bad)
)
r = client.get(URL.format("header"), params=HV)
assert r.status_code == 503
def test_dossier_read_failure_503(client, monkeypatch):
def boom(con, family):
raise RuntimeError("io")
monkeypatch.setattr("pfs.dossier.read_dossier", boom)
r = client.get(URL.format("header"), params=HV)
assert r.status_code == 503
def test_table_missing_404(tmp_path, client):
path = tmp_path / "empty.duckdb"
con = duckdb.connect(str(path))
con.execute("CREATE TABLE t (a INTEGER)")
con.close()
app.dependency_overrides[policy.config_dep] = lambda: dataclasses.replace(
CFG, duckdb_replica=str(path)
)
r = client.get(URL.format("header"), params=HV)
assert r.status_code == 404
assert r.json()["detail"]["family"] == "HOME-VISITS"
assert r.json()["detail"]["status"] == "curated"
def test_cache_reloads_after_replica_republish(replica, client):
r1 = client.get(URL.format("header"), params=HV)
assert r1.json()["built_at"] != "2026-11-14T00:00:00"
t = os.stat(replica).st_mtime
_write(
replica, dataclasses.replace(fixture_dossier(), built_at="2026-11-14T00:00:00")
)
os.utime(replica, (t + 5, t + 5))
r2 = client.get(URL.format("header"), params=HV)
assert r2.json()["built_at"] == "2026-11-14T00:00:00"
assert r2.headers["etag"] != r1.headers["etag"]
def test_cache_reads_dossier_once_per_mtime(client, monkeypatch):
calls = _count_reads(monkeypatch)
for s in ("header", "timeline", "payment", "header"):
assert client.get(URL.format(s), params=HV).status_code == 200
assert calls == ["HOME-VISITS"]
def test_cache_bounded(replica, client, monkeypatch):
_write(replica, dataclasses.replace(fixture_dossier(), family="ZZZ-TWO"))
monkeypatch.setattr(policy, "CACHE_MAX", 1)
calls = _count_reads(monkeypatch)
client.get(URL.format("header"), params=HV)
client.get(URL.format("header"), params={"family": "ZZZ-TWO"})
assert list(policy._CACHE) == ["ZZZ-TWO"]
client.get(URL.format("header"), params=HV)
assert calls == ["HOME-VISITS", "ZZZ-TWO", "HOME-VISITS"]
def test_fact_endpoint(client):
r = client.get("/api/policy/fact", params={**HV, "id": "F4"})
assert r.status_code == 200
assert r.json()["view"] == "valuation"
etag = r.headers["etag"]
r2 = client.get(
"/api/policy/fact", params={**HV, "id": "F4"}, headers={"If-None-Match": etag}
)
assert r2.status_code == 304
r = client.get("/api/policy/fact", params={**HV, "id": "F999"})
assert r.status_code == 404 and r.json()["detail"]["message"] == "unknown fact"
assert client.get("/api/policy/fact", params={**HV, "id": "x"}).status_code == 422
r = client.get("/api/policy/fact", params={"family": "CCM", "id": "F1"})
assert r.status_code == 404 and r.json()["detail"]["status"] == "planned"
def test_load_slice_matches_http(replica, client):
cfg = dataclasses.replace(CFG, duckdb_replica=str(replica))
for s in ("header", "politics", "facts"):
assert (
policy.load_slice(cfg, "home-visits", s)
== client.get(URL.format(s), params=HV).json()
)
with pytest.raises(policy.PolicyError) as ei:
policy.load_slice(cfg, "a b", "header")
assert ei.value.status == 400
assert policy.load_fact(cfg, "HOME-VISITS", "F4")["view"] == "valuation"
def test_router_mounted_on_app():
# this FastAPI nests included routers under app.routes; openapi flattens
paths = set(app.openapi()["paths"])
assert "/api/policy/dossier/{section}" in paths
assert "/api/policy/fact" in paths
def test_default_dependencies(monkeypatch):
monkeypatch.setattr("llm.config.load", lambda: "cfg")
assert policy.config_dep() == "cfg"
from conf import connect
assert policy.bib_dep() is connect.bib