From 58a3495e34e412ac035db1c32e4fe63be96930db Mon Sep 17 00:00:00 2001 From: kert Date: Fri, 13 Nov 2026 02:14:32 -0500 Subject: [PATCH] fix(pfs): IOM and MLN references link their manual chapter (lex S1 acceptance) --- src/pfs/cite.py | 14 +++++++--- src/pfs/dossier.py | 25 ++++++++++++++++-- src/pfs/guidance.py | 16 ++++++++++++ tests/pfs/test_cite.py | 9 +++++++ tests/pfs/test_dossier.py | 53 ++++++++++++++++++++++++++++++++++++++ tests/pfs/test_guidance.py | 31 ++++++++++++++++++++++ 6 files changed, 143 insertions(+), 5 deletions(-) diff --git a/src/pfs/cite.py b/src/pfs/cite.py index 71a6840..6f2a073 100644 --- a/src/pfs/cite.py +++ b/src/pfs/cite.py @@ -233,11 +233,19 @@ def _cfr_url(locator: str) -> str: def reference_cite( - kind: str, locator: str, *, item_key: str = "", p_id: int = 0, page: int = 0 + kind: str, + locator: str, + *, + item_key: str = "", + p_id: int = 0, + page: int = 0, + url: str = "", ) -> Cite: - """A CFR, IOM or MLN reference (``kind``), the eCFR link for a CFR cite; + """A CFR, IOM or MLN reference (``kind``), the eCFR link for a CFR cite + and the given *url* (the manual chapter) otherwise; *item_key*/*p_id* are the source that cites it.""" - url = _cfr_url(locator) if kind == "cfr" else "" + if kind == "cfr": + url = _cfr_url(locator) return Cite(kind, locator, url, item_key, p_id, page, locator) diff --git a/src/pfs/dossier.py b/src/pfs/dossier.py index 3a4e04a..402b01c 100644 --- a/src/pfs/dossier.py +++ b/src/pfs/dossier.py @@ -376,6 +376,15 @@ class _Bib: self.store = store self._rules: dict[str, tuple[str, int | None]] = {} self._paras: dict[tuple[str, int], str | None] = {} + self._ref_urls: dict[tuple[str, str], str] = {} + + def reference_url(self, kind: str, locator: str) -> str: + key = (kind, locator) + if key not in self._ref_urls: + from pfs.guidance import reference_url + + self._ref_urls[key] = reference_url(self.store, kind, locator) + return self._ref_urls[key] def rule(self, item_key: str) -> tuple[str, int | None]: if item_key not in self._rules: @@ -737,13 +746,25 @@ def _origin_facts(con: Any, bib: _Bib, family: Any) -> list[Fact]: edition, page = (m.group(1), m.group(2)) if m else (loc, rows[0].page_label) cite = book_cite(item_key, edition, page) elif shape.startswith("CMS guidance for"): - cite = reference_cite(cite_kind, ref, item_key=item_key, p_id=p_id) + cite = reference_cite( + cite_kind, + ref, + item_key=item_key, + p_id=p_id, + url=bib.reference_url(cite_kind, ref), + ) if p_id: also.append(fr_para_cite(bib.store, item_key, p_id)) else: cite = fr_para_cite(bib.store, item_key, p_id) also.append( - reference_cite(cite_kind, ref, item_key=item_key, p_id=ref_p_id) + reference_cite( + cite_kind, + ref, + item_key=item_key, + p_id=ref_p_id, + url=bib.reference_url(cite_kind, ref), + ) ) if kind == "statute" and (url := ssa_section_url(ref)): also.append(Cite("statute", ref, url, locator=ref)) diff --git a/src/pfs/guidance.py b/src/pfs/guidance.py index 8b66f22..8c7e067 100644 --- a/src/pfs/guidance.py +++ b/src/pfs/guidance.py @@ -196,6 +196,22 @@ def resolve_mln(store: Any, locator: str) -> str: return "" +_IOM_LOCATOR = re.compile(r"^(?P\d{3}-\d{2})\s+ch\.\s*(?P\d+[A-Z]?)") + + +def reference_url(store: Any, kind: str, locator: str) -> str: + """The URL of the manual chapter (``iom``) or MLN product (``mln``) the + reference *locator* names, or ``""`` when unresolved or another kind.""" + if kind == "iom": + m = _IOM_LOCATOR.match(locator or "") + key = resolve_iom(store, m["pub"], m["ch"]) if m else "" + elif kind == "mln": + key = resolve_mln(store, locator) + else: + key = "" + return store.get(key).url or "" if key else "" + + #: (item_key, section) -> located page; storage path -> normalized page #: texts. Both live for the process — one ``stack pfs guidance --write`` #: run locates the same chapter's sections for several families. diff --git a/tests/pfs/test_cite.py b/tests/pfs/test_cite.py index 703633f..526d0ab 100644 --- a/tests/pfs/test_cite.py +++ b/tests/pfs/test_cite.py @@ -13,6 +13,7 @@ from pfs.cite import ( fr_page_cite, fr_para_cite, guidance_cite, + reference_cite, rule_label, rvu_cite, ssa_section_url, @@ -186,3 +187,11 @@ def test_ssa_section_url(ref, url): def test_cite_round_trips_through_a_dict(): c = comment_cite("X-1") assert Cite(**c.__dict__) == c + + +def test_reference_cite_keeps_given_url(): + assert reference_cite("iom", "100-04 ch.26", url="u").url == "u" + assert reference_cite("iom", "100-04 ch.26").url == "" + assert reference_cite("cfr", "42 CFR 410.78", url="u").url.startswith( + "https://www.ecfr.gov/" + ) diff --git a/tests/pfs/test_dossier.py b/tests/pfs/test_dossier.py index 6d25a32..125229e 100644 --- a/tests/pfs/test_dossier.py +++ b/tests/pfs/test_dossier.py @@ -1450,6 +1450,59 @@ def test_origin_edges(home_fixture): ) +def test_origin_iom_cite_carries_manual_url(home_fixture): + from bib.item import Manual + + con, store, family, _ask = home_fixture + key = store.create( + Manual( + title="Medicare Claims Processing Manual \u2014 Chapter 12: Physicians", + url="https://www.cms.gov/x/clm104c12.pdf", + ) + ) + store.add_tag(key, "pub:100-04") + write_origin( + con, + family.key, + [ + OriginRow( + family.key, + "99341", + "coverage", + "100-04 ch.12", + 2019, + "CMS guidance for 99341 points to 100-04 ch.12.", + "iom", + "PETM225D", + 1684, + "", + "100-04 ch.12", + "guidance", + ), + OriginRow( + family.key, + "99341", + "coverage", + "100-04 ch.5", + 2019, + "CMS guidance for 99341 points to 100-04 ch.5.", + "iom", + "PETM225D", + 1684, + "", + "100-04 ch.5", + "guidance", + ), + ], + ) + facts = collect_facts(con, store, family) + hit = next(f for f in facts if f.text.endswith("points to 100-04 ch.12.")) + assert hit.cite.kind == "iom" + assert hit.cite.url == "https://www.cms.gov/x/clm104c12.pdf" + miss = next(f for f in facts if f.text.endswith("points to 100-04 ch.5.")) + assert miss.cite.url == "" + + def test_event_edges(home_fixture): con, store, family, _ask = home_fixture write_events( diff --git a/tests/pfs/test_guidance.py b/tests/pfs/test_guidance.py index 7e832ec..6dacfb0 100644 --- a/tests/pfs/test_guidance.py +++ b/tests/pfs/test_guidance.py @@ -33,6 +33,7 @@ from pfs.guidance import ( harvest_cpt, iom_section_page, mln_refs, + reference_url, resolve_cfr, resolve_iom, resolve_mln, @@ -271,6 +272,36 @@ class TestResolveIom: assert resolve_iom(s, "100-04", "5") == "" +class TestReferenceUrl: + def test_reference_url_iom_chapter(self, store): + s, _, manual_key = store + s.update(manual_key, url="https://www.cms.gov/x/clm104c12.pdf") + assert ( + reference_url(s, "iom", "100-04 ch.12 §30.6.14.1") + == "https://www.cms.gov/x/clm104c12.pdf" + ) + + def test_reference_url_mln(self, store): + s, _, _ = store + key = s.create( + Manual( + title="Eval and Mgmt (MLN909188, June 2025)", + url="https://www.cms.gov/x/mln909188.pdf", + ) + ) + assert key + assert ( + reference_url(s, "mln", "MLN 909188") + == "https://www.cms.gov/x/mln909188.pdf" + ) + + def test_reference_url_unresolved_is_empty(self, store): + s, _, _ = store + assert reference_url(s, "iom", "100-04 ch.5") == "" + assert reference_url(s, "iom", "chapter 12") == "" + assert reference_url(s, "statute", "x") == "" + + # ── harvest (FR paragraphs) ────────────────────────────────────────