Files
stack/tests/llm/policy_fixture.py

404 lines
12 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""The fixture dossier the lex API and JS tests share (lex S1 Task 2).
Built from Dossier constructors, never from a frozen copy of live JSON, and
with invented strings only (no CPT descriptor text). ``dossier_fixture_slices``
is what ``tests/lex/fixtures/*.json`` are generated from.
"""
from __future__ import annotations
from pfs.cite import Cite
from pfs.dossier import (
UNREVIEWED,
UNREVIEWED_EXTRACT,
Dossier,
Excerpt,
Fact,
PaymentRow,
RoundView,
TimelineEvent,
stance_accuracy_line,
)
from pfs.utilization import SeriesRow
ACC = stance_accuracy_line(
{
"dockets": ["CMS-0000-0001"],
"in_sample": False,
"n_items": 40,
"n_positions": 55,
"recall": 0.8,
"direction": 0.9,
}
)
_BASE = "https://example.test/fr/"
def _para(item: str, p: int, year: int = 2023) -> Cite:
return Cite(
"fr_para",
f"CY{year} PFS final {item}{p}",
f"{_BASE}{item}#p{p}",
item,
p,
locator=f"{item}{p}",
)
def _f(
n: int,
kind: str,
year: int | None,
codes: tuple[str, ...],
text: str,
cite: Cite,
*,
values: dict | None = None,
also: tuple[Cite, ...] = (),
note: str = "",
) -> Fact:
return Fact(f"F{n}", kind, year, codes, text, values or {}, cite, also, note)
def _facts() -> dict[str, Fact]:
book = Cite(
"cpt_book", "CPT 2019 p. 20", "", "BOOK2019", page=20, locator="CPT 2019 p. 20"
)
statute = Cite(
"statute",
"Act section 9999",
"https://example.test/statute/9999",
locator="Act section 9999",
)
study = Cite(
"study", "Invented study", "https://example.test/study", "ART1", page=3
)
comment = Cite("comment", "Comment 1", "https://example.test/comment/1")
facts = [
_f(
1,
"origin",
2007,
("99341",),
"Invented origin sentence.",
_para("MZ24MX5S", 878, 2007),
also=(statute,),
),
_f(
2,
"formation",
2019,
("99341",),
"Invented formation paraphrase.",
book,
note=UNREVIEWED,
),
_f(
3,
"event",
2023,
("99343",),
"99343 was deleted for CY2023.",
_para("PETM225D", 1672),
values={"event_kind": "deleted"},
),
_f(
4,
"round",
2023,
("99341",),
"Invented valuation round text.",
_para("PETM225D", 1676),
values={"final_work_rvu": 1.5},
),
_f(
5,
"pe",
2022,
("99341",),
"Invented practice expense note.",
_para("MZ24MX5S", 877, 2022),
note=UNREVIEWED_EXTRACT,
),
_f(
6,
"cms_summary",
2023,
("99341",),
"Invented CMS summary.",
_para("PETM225D", 1679),
),
_f(7, "stance_split", 2023, ("99341",), f"Invented split. {ACC}", comment),
_f(8, "stance_split", 2018, ("99341",), "Invented older split.", comment),
_f(9, "link", 2023, ("G0318",), "G0318 is linked.", _para("PETM225D", 1694)),
_f(
10,
"payment",
2022,
("99343",),
"Invented payment text.",
Cite("rvu_file", "CY2022 RVU file", "https://example.test/rvu/2022"),
values={"vintage": "CY2022 final"},
),
_f(
11,
"payment",
2023,
("99341",),
"Invented payment text.",
Cite("rvu_file", "CY2023 RVU file", "https://example.test/rvu/2023"),
values={"vintage": "CY2023 final"},
),
_f(
12,
"payment",
2027,
("99341",),
"Invented proposed payment text.",
Cite("rvu_file", "CY2027 RVU file", "https://example.test/rvu/2027"),
values={"vintage": "CY2027 proposed"},
),
_f(
13,
"impact",
2023,
("99341",),
"Invented impact.",
Cite("dataset", "Impact table", "https://example.test/impact"),
),
_f(
14,
"money",
2023,
("99341",),
"Invented money.",
Cite("dataset", "Money table", "https://example.test/money"),
),
_f(
15,
"seat",
2022,
("99341",),
"Invented seat.",
Cite("dataset", "Seat roster", "https://example.test/seat"),
),
_f(16, "finding", None, (), "Invented finding.", study),
_f(
17,
"event",
2021,
("99343",),
"Invented unplaced event.",
_para("PETM225D", 1600, 2021),
),
_f(
18,
"payment",
2023,
("99999",),
"Invented orphan payment.",
Cite("rvu_file", "CY2023 RVU file", "https://example.test/rvu/2023"),
values={"vintage": "CY2023 final"},
),
]
return {f.id: f for f in facts}
def _round(code: str, year: int, item: str, **kw) -> RoundView:
base = dict(
family="HOME-VISITS", code=code, rule_year=year, item_key=item,
rule_kind="final", p_id=1, page=0, source="fr-para", table_title="",
ruc_work_rvu=None, ruc_percentile="", survey_intra_min=None,
survey_total_min=None, comparators="", surveying_societies="",
requested_work_rvu=None, proposed_work_rvu=None, final_work_rvu=None,
decision="accept", rationale_p_id=0, rationale_quote="", pe_note="",
extractor="fixture",
) # fmt: skip
base.update(kw)
return RoundView(**base)
def _pay(code: str, year: int, vintage: str, *, proposed: bool) -> PaymentRow:
return PaymentRow(
code=code, vintage=vintage, year=year, proposed=proposed,
status="A", work=1.0, pe_nf=1.0, pe_f=0.5, mp=0.1, total_nf=2.1,
total_f=1.6, cf=33.0, pay_nf=69.3, pay_f=52.8, label=vintage,
citation="CY RVU file", url="https://example.test/rvu",
) # fmt: skip
def fixture_dossier(*, stance_accuracy: str = ACC) -> Dossier:
units = [
{
"org_type": "specialty_society", "specialty": "CARDIOLOGY",
"names": ["American College of Cardiology"], "n_commenters": 1,
"stance": {"support": 1}, "expected": "support",
"expected_reason": "invented reason", "share": 0.05,
"share_text": "5.0%", "delta_total": -12106.0,
"delta_lower_bound": True,
"delta_text": "a cut of at least $12,106",
},
{
"org_type": "individual_clinician", "specialty": "CARDIOLOGY",
"names": ["jane q. DOE"], "n_commenters": 1,
"stance": {"modify": 1}, "expected": "", "expected_reason": "",
"share": 0.0, "share_text": "", "delta_total": None,
"delta_lower_bound": False, "delta_text": "",
},
{
"org_type": "health_system", "specialty": "NONE",
"names": ["ACME health LLC"], "n_commenters": 1, "stance": {},
"expected": "", "expected_reason": "", "share": 0.0,
"share_text": "", "delta_total": None, "delta_lower_bound": False,
"delta_text": "",
},
] # fmt: skip
panel = {
"docket": "CMS-0000-0001", "rule_year": 2023, "event_key": "HV:2023:x",
"event_text": "invented event", "accuracy": "", "roster_year": 2022,
"roster_note": "", "units": units,
"seats": {
"CARDIOLOGY": {
"ruc_permanent": 1, "ruc_rotating": 0, "cpt": 0,
"holders": ["American College of Cardiology"],
"item_keys": ["AAAA1111"],
}
},
} # fmt: skip
return Dossier(
family="HOME-VISITS",
name="Home or Residence Services",
codes=("99341", "99343"),
status="active",
years_active="2007–2027",
summary=(
"For CY2023, CMS finalized work RVUs for 99341 [F4]. "
"99343 was deleted for CY2023 [F3][F3]."
),
timeline=[
TimelineEvent(
2023,
"deleted",
("99343",),
"99343 was deleted for CY2023 [F3].",
("F3",),
(),
(),
),
TimelineEvent(
2023,
"valuation",
("99341",),
"Invented valuation line [F4].",
("F4",),
("F7",),
("F6",),
),
TimelineEvent(
2023,
"link:G0318",
("G0318",),
"Invented link line [F9].",
("F9",),
(),
(),
),
], # fmt: skip
rounds=[
_round(
"99341",
2023,
"PETM225D",
ruc_percentile="25th",
rationale_quote="We are finalizing the RUC-recommended values.",
),
_round("99343", 2008, "54GTKR5G", requested_work_rvu=2.85),
],
payment=[
_pay("99343", 2022, "CY2022 final", proposed=False),
_pay("99341", 2023, "CY2023 final", proposed=False),
_pay("99341", 2027, "CY2027 proposed", proposed=True),
_pay("G0318", 2023, "CY2023 final", proposed=False),
],
utilization=[
SeriesRow("99341", 2022, 10, 100, 500.0, 60.0, 48.0),
SeriesRow("99341", 2023, 12, 120, 600.0, 61.0, 49.0),
],
stakeholders=[panel],
governance=[
Excerpt(
"Invented governance excerpt.",
Cite("study", "Invented study", "https://example.test/study", "ART1"),
)
],
sources={},
gaps=["Invented gap one.", "Invented gap two."],
facts=_facts(),
built_at="2026-11-13T05:23:37",
check_passed=True,
extractor="model:qwen2.5:14b+template",
review=[
{"fact_id": "F2", "reason": UNREVIEWED},
{"fact_id": "F5", "reason": UNREVIEWED_EXTRACT},
{
"fact_id": "",
"reason": "held summary sentence",
"quote": "Invented held sentence.",
"cite": "PETM225D ¶1",
"response": "invented response",
},
],
fallbacks=["CY2021 formation"],
stance_accuracy=stance_accuracy,
)
def fixture_info() -> dict:
"""The bib facts behind the fixture's sources (invented titles)."""
from llm.policy_slices import SourceInfo
return {
"PETM225D": SourceInfo(
"Invented final rule", "https://example.test/fr/PETM225D", True, True
),
"ART1": SourceInfo(
"Invented article", "https://example.test/study", True, False
),
}
def dossier_fixture_slices(d: Dossier | None = None) -> dict[str, dict]:
"""Every per-section slice of the fixture, keyed by fixture file stem."""
from llm.policy_slices import (
contents_entry,
contents_slice,
facts_index,
header_slice,
payment_slice,
politics_slice,
sources_slice,
timeline_slice,
valuation_slice,
)
from pfs.book import load_book
d = d or fixture_dossier()
return {
"header": header_slice(d, tier="reviewed", book=load_book()["HOME-VISITS"]),
"timeline": timeline_slice(d),
"valuation": valuation_slice(d),
"payment": payment_slice(d),
"politics": politics_slice(d),
"facts": facts_index(d),
"sources": sources_slice(
d, fixture_info(), viewer_base="https://llm.example.test/ui/pdf/"
),
"contents": contents_slice(
[contents_entry(d, tier="reviewed", book=load_book()["HOME-VISITS"])],
load_book(),
{"HOME-VISITS"},
(8662, 7858),
),
}