diff --git a/src/cli/pfs.py b/src/cli/pfs.py index 28217fb..9c7489d 100644 --- a/src/cli/pfs.py +++ b/src/cli/pfs.py @@ -67,6 +67,7 @@ from pfs.commenters import family_comment_items, identify, regs_for from pfs.cpt_load import ingest as cpt_ingest from pfs.curated import apply_curated, load_curated from pfs.dossier import ( + TEMPLATE_ONLY_EVENTS, build_dossier, collect_facts, governance_excerpts, @@ -2434,10 +2435,12 @@ def dossier_cmd( if dry_run: facts = collect_facts(con, store, fam) events = dossier_timeline(facts) + drafted = [e for e in events if e.kind not in TEMPLATE_ONLY_EVENTS] typer.echo( f"{fam.key}: {len(facts)} facts, {len(events)} timeline events; " - f"model calls a run would make: {len(events) + 1} " - "(each retried once at most)" + f"model calls a run would make: {len(drafted)} event lines " + "(each retried once at most; the summary and the valuation and " + "comments lines are template copy)" ) continue d = build_dossier( diff --git a/src/pfs/citecheck.py b/src/pfs/citecheck.py index 03f2467..5bb00bb 100644 --- a/src/pfs/citecheck.py +++ b/src/pfs/citecheck.py @@ -12,14 +12,21 @@ from __future__ import annotations import re from dataclasses import dataclass -from typing import TYPE_CHECKING, Any, Iterable, Mapping +from typing import TYPE_CHECKING, Any, Iterable, Mapping, Sequence if TYPE_CHECKING: # pragma: no cover from pfs.dossier import Fact PROBLEM_KINDS = ("uncited", "unknown_fact", "number_mismatch") #: what ``grounding_problems`` adds for model prose -GROUNDING_KINDS = ("ungrounded", "code_mismatch", "merged", "unattributed") +GROUNDING_KINDS = ( + "ungrounded", + "code_mismatch", + "negation", + "number_role", + "merged", + "unattributed", +) @dataclass(frozen=True) @@ -82,7 +89,6 @@ def split_sentences(text: str) -> list[str]: out: list[str] = [] start = 0 for m in _END.finditer(text): - chunk = text[start : m.start()].strip() # the end match sits after the punctuation; a lone closing char is # part of the sentence chunk = text[start : m.end()].strip() @@ -98,7 +104,9 @@ def split_sentences(text: str) -> list[str]: if lead and merged: merged[-1] = f"{merged[-1]} {lead.group(1).strip()}" s = s[lead.end() :].strip() - if s: + if s and merged and not re.search(r"\w", s): + merged[-1] = f"{merged[-1]}{s}" # a stray full stop after the tags + elif s: merged.append(s) return merged @@ -207,15 +215,112 @@ _PROSE_GENERIC = frozenset( "following together respectively through value values both also rvu rvus".split() ) _CODE5 = re.compile(r"\b\d{5}\b") +#: R95: words that turn or scale a statement; one may appear only when a +#: cited fact says it too +_GUARDED = ("not", "no", "never", "all", "most", "many", "few", "majority", "none") +_GUARDED_RE = re.compile(r"\b(" + "|".join(_GUARDED) + r")\b", re.I) +_CONTRACTION = re.compile(r"n't\b", re.I) +_TOKENS = re.compile(r"[A-Za-z]+|\d[\d,.]*\d|\d") +_CODE_TOKEN = re.compile(r"^(?:\d{5}|[A-Z]\d{4})$") +_ROLES = ( + ("ruc", re.compile(r"^(?:recommend\w*|RUC)$", re.I)), + ("proposed", re.compile(r"^propos\w*$", re.I)), + ("final", re.compile(r"^finaliz\w*$", re.I)), + ("requested", re.compile(r"^request\w*$", re.I)), + ("support", re.compile(r"^support\w*$", re.I)), + ("oppose", re.compile(r"^oppos\w*$", re.I)), + ("modify", re.compile(r"^modif\w*$", re.I)), + ("unclear", re.compile(r"^unclear$", re.I)), +) + + +def _role(word: str) -> str | None: + return next((r for r, pat in _ROLES if pat.match(word)), None) + + +def number_roles(text: str) -> dict[str, set[tuple[str | None, str | None]]]: + """``value -> {(role, code)}`` for the numbers *text* writes (codes and + years aside): the role word right after a number (within three words: + "14 supported") or else the nearest before it (within six: "finalized + 2.63"), and the code nearest to it (within six words).""" + toks = [m.group(0) for m in _TOKENS.finditer(_BRACKETS.sub(" ", text or ""))] + toks = [t.rstrip(".,") for t in toks] + out: dict[str, set[tuple[str | None, str | None]]] = {} + for i, t in enumerate(toks): + if not t[:1].isdigit() or _CODE_TOKEN.match(t): + continue + if re.fullmatch(r"(?:19|20)\d\d", t): + continue + role = next((r for w in toks[i + 1 : i + 4] if (r := _role(w))), None) + if role is None: + role = next( + (r for w in reversed(toks[max(0, i - 6) : i]) if (r := _role(w))), None + ) + near = [ + (abs(j - i), j > i, w) + for j, w in enumerate(toks) + if _CODE_TOKEN.match(w) and abs(j - i) <= 6 + ] + code = min(near)[2] if near else None + out.setdefault(_norm_number(t), set()).add((role, code)) + return out + + +def _guard_problems(sentence: str, body: str, cited: Sequence[Fact]) -> list[Problem]: + texts = " ".join(f.text for f in cited) + texts = _CONTRACTION.sub(" not", texts) + for m in _GUARDED_RE.finditer(body): + word = m.group(1).lower() + if not re.search(rf"\b{word}\b", texts, re.I): + return [ + Problem( + sentence, + "negation", + f"'{m.group(0)}' is not said by the cited facts", + ) + ] + facts_roles: dict[str, set[tuple[str | None, str | None]]] = {} + for f in cited: + for value, pairs in number_roles(f.text).items(): + facts_roles.setdefault(value, set()).update(pairs) + for value, pairs in number_roles(body).items(): + known = facts_roles.get(value) + if not known: + continue + roles = {r for r, _c in known if r} + codes = {c for _r, c in known if c} + for role, code in pairs: + if role and roles and role not in roles: + return [ + Problem( + sentence, + "number_role", + f"{value} is {role} here but {'/'.join(sorted(roles))} in the facts", + ) + ] + if code and codes and code not in codes: + return [ + Problem( + sentence, + "number_role", + f"{value} sits beside {code} here but " + f"{'/'.join(sorted(codes))} in the facts", + ) + ] + return [] def grounding_problems(prose: str, facts: Mapping[str, Fact]) -> list[Problem]: """What model prose must also respect beyond :func:`check_prose` (the spec's anti-goals: no invented history, CMS's summary never merged with - our counts): every content word of a sentence occurs (stemmed) in the text - of the facts it cites, generic connective words aside (``ungrounded``); - every code it names, ranges expanded, is one those facts name - (``code_mismatch``); a sentence citing CMS's summary says "CMS" and one + our counts; R95): every content word of a sentence occurs (stemmed) in + the text of the facts it cites, generic connective words aside + (``ungrounded``); every code it names, ranges expanded, is one those + facts name (``code_mismatch``); a negation or quantifier (not, no, never, + all, most, many, few, majority, none; contractions read as "not") is one + a cited fact says too (``negation``); a number keeps the role word and + the code its fact gives it ("CMS finalized 2.63", "14 supported"; + ``number_role``); a sentence citing CMS's summary says "CMS" and one citing our classification says "classified" (``unattributed``), and no sentence cites both (``merged``). Sentences without a known tag are left to ``check_prose``.""" @@ -228,7 +333,7 @@ def grounding_problems(prose: str, facts: Mapping[str, Fact]) -> list[Problem]: cited = [facts[t] for t in tags_of(sentence) if t in facts] if not cited: continue - body = _BRACKETS.sub(" ", sentence) + body = _CONTRACTION.sub(" not", _BRACKETS.sub(" ", sentence)) known = set().union(*(_content(f.text) for f in cited)) | generic missing = sorted({w for w in _content(body) if w not in known}) if missing: @@ -254,6 +359,10 @@ def grounding_problems(prose: str, facts: Mapping[str, Fact]) -> list[Problem]: ) ) continue + guarded = _guard_problems(sentence, body, cited) + if guarded: + out += guarded + continue kinds = {f.kind for f in cited} if {"cms_summary", "stance_split"} <= kinds: out.append( diff --git a/src/pfs/dossier.py b/src/pfs/dossier.py index 0f6eff7..faa0922 100644 --- a/src/pfs/dossier.py +++ b/src/pfs/dossier.py @@ -63,6 +63,8 @@ from pfs.codetables import ( read_stances, ) from pfs.descriptors import rule_year_of +from pfs.families import find_codes +from pfs.lineage import _expand_ranges from pfs.runlog import RunLog, input_hash __all__ = [ @@ -105,7 +107,7 @@ _KIND_RANK = {k: i for i, k in enumerate(FACT_KINDS)} UNREVIEWED = "model paraphrase, unreviewed" UNREVIEWED_EXTRACT = "model extract, unreviewed" DOSSIER_VERSION = 1 -PROSE_PROMPT_VERSION = "prose-v2" +PROSE_PROMPT_VERSION = "prose-v3" #: only the specialties billing at least this share get money, impact and #: seat facts (the expected-stance rule's own floor) MIN_SHARE = 0.01 @@ -452,44 +454,90 @@ def round_text( _QUANTIFIER = { - "majority": "a majority of", - "many": "many", - "several": "several", - "some": "some", - "few": "a few", - "one": "one", - "all": "all", - "unspecified": "", + "majority": "a majority of commenters", + "many": "many commenters", + "several": "several commenters", + "some": "some commenters", + "few": "a few commenters", + "one": "one commenter", + "all": "all commenters", + "unspecified": "no quantifier", +} +_ACTION_PHRASE = { + "finalize_as_proposed": "finalized as proposed", + "finalize_modified": "finalized with changes", + "not_finalized": "not finalized", + "acknowledged": "acknowledged the comments", + "unclear": "not determinable from its wording", } _STANCE_PHRASE = { - "support": "supported the proposal", + "support": "support", "modify": "asked for changes", - "oppose": "opposed the proposal", - "mixed": "were divided", - "unclear": "commented", + "oppose": "oppose", + "mixed": "mixed", + "unclear": "unclear", } +QUOTE_CHARS = 300 +_PRINTED_MARK = re.compile(r"\(\s*printed page \d+\s*\)", re.I) +_CPT_PAREN = re.compile(r"\b(\d{5})\s*\(\s*(?!codes?\b|a range\b)[A-Z]") -def cms_summary_text(quantifier: str, stance: str, year: int, kind: str) -> str: - """CMS's own characterization of the commenters, as CMS's: "CMS reported - that a majority of commenters supported the proposal (CY2023 final - rule)." One commenter reads in the singular ("gave a mixed view").""" - phrase = _STANCE_PHRASE.get(stance, "commented") - if quantifier == "one": - who = "one commenter" - if stance == "mixed": - phrase = "gave a mixed view" - else: - who = " ".join(f"{_QUANTIFIER.get(quantifier, '')} commenters".split()) - return f"CMS reported that {who} {phrase} (CY{year} {kind} rule)." +def strip_descriptors(text: str) -> str: + """*text* with a CPT descriptor CMS printed in parentheses after a code + replaced by ``[CPT descriptor omitted]`` (AMA text never shown).""" + out, i = [], 0 + for m in _CPT_PAREN.finditer(text): + if m.start() < i: + continue + start = text.index("(", m.start()) + depth, j = 0, start + while j < len(text): + depth += {"(": 1, ")": -1}.get(text[j], 0) + if depth == 0: + break + j += 1 + out.append(text[i:start] + "[CPT descriptor omitted]") + i = j + 1 + out.append(text[i:]) + return "".join(out) + + +def comment_first_sentence(comment: str) -> str: + """The first sentence of a Comment paragraph, verbatim (FR text, public + domain) without its "Comment:" lead, printed-page marks or CPT + descriptors; at most 300 characters, cut at a word.""" + text = _PRINTED_MARK.sub(" ", comment or "") + text = re.sub(r"^\s*Comments?:\s*", "", text) + text = strip_descriptors(" ".join(text.split())) + first = split_sentences(text)[0] if text else "" + if len(first) > QUOTE_CHARS: + first = first[: QUOTE_CHARS - 1].rsplit(" ", 1)[0] + "…" + return first + + +def cms_summary_text( + quote: str, year: int, kind: str, quantifier: str, action: str, stance: str +) -> str: + """CMS's own summary of the comments, in CMS's words (R94): the first + sentence of the Comment paragraph, CMS's quantifier and the response + action read by rule; the model's stance reading only as "model reading, + unreviewed".""" + return ( + f'In the CY{year} {kind} rule, CMS summarized: "{quote}" CMS\'s quantifier: ' + f"{_QUANTIFIER.get(quantifier, quantifier)}. CMS's response: " + f"{_ACTION_PHRASE.get(action, action)}. Stance of CMS's summary (model " + f"reading, unreviewed): {_STANCE_PHRASE.get(stance, stance)}." + ) def stance_split_text( n: int, year: int, event_text: str, s: int, m: int, o: int, u: int ) -> str: + who = "commenter" if n == 1 else "commenters" + verb = "was" if u == 1 else "were" return ( - f"Of {n} CY{year} commenters we classified on {event_text}, {s} supported, " - f"{m} asked for modifications, {o} opposed and {u} were unclear." + f"Of {n} CY{year} {who} we classified on {event_text}, {s} supported, " + f"{m} asked for modifications, {o} opposed and {u} {verb} unclear." ) @@ -570,15 +618,18 @@ def stance_accuracy_line(ev: Mapping[str, Any] | None) -> str: ) -def _in_sample_line(ev: Mapping[str, Any]) -> str: +def _in_sample_line(ev: Mapping[str, Any], docket: str = "") -> str: + """The secondary tuning-set record (`in_sample_record`), as its own + sentence; with *docket*, only when the record was measured on that + docket (shown beside that docket's counts).""" rec = ev.get("in_sample_record") - if not rec: + if not rec or (docket and rec.get("docket") != docket): return "" return ( - " Tuning-set record: " - + stance_accuracy_line({**rec, "in_sample": True}).split( - ", measured on the tuning set " - )[1] + f" Tuning-set record on {rec.get('docket', 'the tuning set')} " + f"({rec['n_items']} hand-coded comments, {rec['n_positions']} positions): " + f"it finds {round(rec['recall'] * 100)}% of hand-coded positions, and the " + f"direction is right for {round(rec['direction'] * 100)}% of those it finds." ) @@ -597,12 +648,17 @@ def money_change(delta: float | None, *, lower_bound: bool) -> str: return f"{what} of {floor}${amount:,}" -def roster_year(captured: set[int], year: int) -> tuple[int | None, str]: - """R91: the RUC roster that valued rule year *year* is the year before - (the RUC that valued CY2023 met in 2022); the year itself when only that - is captured. ``(roster year, what the text says about it)``.""" +def roster_year( + captured: set[int], year: int, body: str = "RUC" +) -> tuple[int | None, str]: + """R91: the roster that acted for rule year *year* is the year before + (the RUC that valued CY2023 met in 2022; the CPT Editorial Panel that set + a code set met the year before too); the year itself when only that is + captured. ``(roster year, what the text says about it)``.""" if year - 1 in captured: - return year - 1, f" (the RUC that valued CY{year} met in {year - 1})" + if body == "RUC": + return year - 1, f" (the RUC that valued CY{year} met in {year - 1})" + return year - 1, f" (the Panel of {year - 1}, for CY{year})" if year in captured: return year, ( f" (the roster of CY{year} itself; the {year - 1} roster is not captured)" @@ -711,10 +767,14 @@ _STRUCTURED = ( ) -def _profile_text(code: str, edition: int, profile: Mapping[str, str]) -> str: +def _profile_body(code: str, profile: Mapping[str, str]) -> str: + """What one edition says about how *code* is selected: structured values + only (never book text), enum values in words.""" extras = [] if "mdm_level" in profile: - extras.append(f"with {profile['mdm_level']} medical decision making") + extras.append( + f"{profile['mdm_level'].replace('_', ' ')} medical decision making" + ) if "time_threshold_min" in profile: extras.append(f"a time threshold of {profile['time_threshold_min']} minutes") if "typical_time_min" in profile: @@ -722,14 +782,21 @@ def _profile_text(code: str, edition: int, profile: Mapping[str, str]) -> str: if profile.get("addon") == "true": extras.append("add-on status") basis = profile.get("selection_basis") - head = f"In the CPT {edition} codebook, {code}" if basis: - head += " " + _BASIS.get(basis, f"is selected on {basis.replace('_', ' ')}") - return head + (", " + _join_and(extras) if extras else "") + "." - return f"{head} carries {_join_and(extras)}." + head = f"{code} " + _BASIS.get( + basis, f"is selected on {basis.replace('_', ' ')}" + ) + return head + (", with " + _join_and(extras) if extras else "") + return f"{code} has {_join_and(extras)}" def _guideline_facts(con: Any, bib: _Bib, family: Any) -> list[Fact]: + """The CPT books' structured facts per code (R96): the earliest edition + on file ("earliest CPT edition on file (2019)", never a formation date), + then every change, dated to the code set that made it (the CPT Changes + year between two editions on file, cited to the family's coding + paragraph and CPT Changes; the later book is evidence, not the date); + deletions; and the flagged paraphrases.""" rows: list[GuidelineFactRow] = _rows(read_guideline_facts, con, family.key) members = family.codes out: list[Fact] = [] @@ -740,6 +807,7 @@ def _guideline_facts(con: Any, bib: _Bib, family: Any) -> list[Fact]: profiles: dict[str, dict[int, tuple[dict[str, str], GuidelineFactRow]]] = {} deleted: dict[tuple, list[str]] = {} + changes: dict[int, GuidelineFactRow] = {} for r in rows: if r.fact in _STRUCTURED: prof, _first = profiles.setdefault(r.code, {}).setdefault( @@ -752,6 +820,7 @@ def _guideline_facts(con: Any, bib: _Bib, family: Any) -> list[Fact]: deleted.setdefault( (r.edition_year, r.value, r.cite_item, r.cite_locator), [] ).append(r.code) + changes.setdefault(r.edition_year, r) else: # scope_point, panel_action: paraphrases, shown flagged out.append( _draft( @@ -764,22 +833,59 @@ def _guideline_facts(con: Any, bib: _Bib, family: Any) -> list[Fact]: note=UNREVIEWED, ) ) + earliest = min((e for by in profiles.values() for e in by), default=None) + coding = {m.code: (m.item_key, m.p_id) for m in family.members if m.p_id} for code, by_edition in profiles.items(): before: dict[str, str] | None = None + prior = 0 for edition in sorted(by_edition): prof, first = by_edition[edition] - if prof != before: + body = _profile_body(code, prof) + if before is None: + head = ( + f"In the earliest CPT edition on file ({edition})" + if edition == earliest + else f"In the CPT {edition} codebook" + ) out.append( _draft( - "formation" if before is None else "event", + "event", edition, (code,), - _profile_text(code, edition, prof), + f"{head}, {body}.", cite_of(first), - {"event_kind": "cpt_edition"}, + {"event_kind": "cpt_edition", "edition": edition}, ) ) - before = prof + elif prof != before: + year = next( + (y for y in sorted(changes) if prior < y <= edition), edition + ) + cite, also = cite_of(first), [] + if year != edition: + ch = changes[year] + book = book_cite( + ch.cite_item, + _book_edition(bib, ch.cite_item, f"CPT Changes {year}"), + ch.cite_locator, + ) + item, p_id = coding.get(code, ("", 0)) + if item and bib.rule(item)[1] == year: + cite, also = fr_para_cite(bib.store, item, p_id), [book, cite] + else: + cite, also = book, [cite] + out.append( + _draft( + "event", + year, + (code,), + f"From the CY{year} code set, {body} (CPT {edition} codebook).", + cite, + {"event_kind": "cpt_edition", "edition": edition}, + also, + ) + ) + before, prior = prof, edition for (year, value, item, loc), codes in deleted.items(): last = f" (last listed in the {value} edition)" if value.isdigit() else "" out.append( @@ -871,6 +977,13 @@ def _event_facts(con: Any, bib: _Bib, family: Any) -> list[Fact]: since = {ln.target: ln.since or 0 for ln in family.links} known = _known_codes(con, family) kept: dict[tuple, Any] = {} + # R96: the paragraph the curated membership cites (the coding paragraph, + # PETM225D ¶1672) before a recap elsewhere; then the lowest paragraph + coding = {(m.code, m.item_key, m.p_id) for m in family.members if m.p_id} + + def rank(e: Any) -> tuple[bool, int, str]: + return ((e.code, e.item_key, e.p_id) not in coding, e.p_id, e.item_key) + for code in [*family.codes, *since]: for e in _rows(read_events, con, code): if e.kind not in _EVENT_TEXT: @@ -888,10 +1001,7 @@ def _event_facts(con: Any, bib: _Bib, family: Any) -> list[Fact]: continue key = (code, e.year, e.kind, to, frm) prior = kept.get(key) - if prior is None or (e.p_id, e.item_key) < ( - prior[0].p_id, - prior[0].item_key, - ): + if prior is None or rank(e) < rank(prior[0]): kept[key] = (e, to, frm) # point-in-time kinds once per code, at the earliest year first: dict[tuple[str, str], int] = {} @@ -964,6 +1074,86 @@ def _link_facts(bib: _Bib, family: Any) -> list[Fact]: return out +_REPLACE = re.compile(r"\breplac\w*", re.I) +_FINALIZES = re.compile(r"\bfinaliz", re.I) + + +def _link_ends(bib: _Bib, family: Any) -> list[Fact]: + """R96: a curated link that a later final rule replaces. The end is the + earliest final PFS rule after the link's ``since`` with a plain sentence + that finalizes replacing the link's code ("We are finalizing the proposal + to replace HCPCS code G2211 with a modifier"); the replacing codes are the + family's links that begin that year and that the paragraph names (code + or proposed-rule placeholder). Cited to the paragraph naming them, the + others as ``also``.""" + from pfs.origin import _names, _plain + from pfs.rounds import _sentences, rule_items + + finals = { + it["item_key"]: rule_year_of(it["title"], it["date_published"]) + for it in rule_items(bib.store, []) + if it["kind"] == "final" + } + con = bib.store._con() # noqa: SLF001 + out: list[Fact] = [] + for ln in family.links: + if not ln.since: + continue + hits: dict[int, list[tuple[str, int, list[str]]]] = {} + for item_key, p_id, text in con.execute( + "SELECT item_key, p_id, text FROM fr_anchors " + "WHERE text LIKE ? AND text LIKE '%replac%' ORDER BY item_key, p_id", + (f"%{ln.target}%",), + ).fetchall(): + year = finals.get(item_key) + if not year or year <= ln.since: + continue + if not any( + _names(sent, ln.target) + and _REPLACE.search(sent) + and _FINALIZES.search(sent) + and _plain(sent, year) + for sent in _sentences(text) + ): + continue + successors = sorted( + other.target + for other in family.links + if other is not ln + and other.since == year + and any(_names(text, c) for c in (other.target, *other.aliases)) + ) + hits.setdefault(year, []).append((item_key, p_id, successors)) + if not hits: + continue + year = min(hits) + found = sorted(hits[year], key=lambda h: (not h[2], h[0], h[1])) + succ = sorted({c for h in found for c in h[2]}) + kinds = {o.kind for o in family.links if o.target in succ} + word = "modifiers" if kinds == {"modifier"} else "codes" + text = ( + f"From CY{year}, {ln.target} is replaced by {word} {_join_and(succ)}." + if succ + else f"From CY{year}, {ln.target} is replaced." + ) + out.append( + _draft( + "link", + year, + (ln.target, *succ), + text, + fr_para_cite(bib.store, found[0][0], found[0][1]), + { + "link_end": ln.target, + "end_year": year, + "replaced_by": "/".join(succ), + }, + [fr_para_cite(bib.store, k, p) for k, p, _s in found[1:]], + ) + ) + return out + + # ── facts: valuation rounds and PE ────────────────────────────────── _PERCENTILE = {"25th": "25th percentile", "median": "median"} @@ -1101,7 +1291,7 @@ def _round_facts(bib: _Bib, rounds: Sequence[RoundRow]) -> list[Fact]: "round", year, (code,), - f"For CY{year}, the RUC survey {what} time for {code} was " + f"For CY{year}, the RUC-recommended {what} time for {code} was " f"{v:g} minutes.", cite, {fld: v}, @@ -1191,20 +1381,72 @@ def _round_views(bib: _Bib, rounds: Sequence[RoundRow]) -> list[RoundView]: # ── facts: CMS's summaries and our stance counts (kept apart) ─────── +def _family_names(text: str, family: Any) -> tuple[list[str], bool]: + """``(family codes named, a pair phrase named)`` in *text*: member codes + (ranges expanded), link codes and their proposed-rule placeholders.""" + wanted = {*family.codes, *(ln.target for ln in family.links)} + wanted |= {a for ln in family.links for a in ln.aliases} + expanded = _expand_ranges(text or "") + named = sorted(c for c in wanted if re.search(rf"\b{re.escape(c)}\b", expanded)) + phrase = any(p in (text or "").lower() for p in family.pair_phrases) + return named, phrase + + +def _opening(paragraph: str) -> str: + """The first sentence of a Comment or Response paragraph, without its + lead word and printed-page marks.""" + text = re.sub( + r"^\s*(?:Comment|Response)s?:\s*", "", _PRINTED_MARK.sub(" ", paragraph or "") + ) + first = split_sentences(text) + return first[0] if first else "" + + def _cms_facts(con: Any, bib: _Bib, family: Any) -> list[Fact]: + """CMS's summaries about this family (R94, applied to what each comment + is about): the Comment's first sentence names a family code (ranges + expanded), a link code or its proposed-rule placeholder, or a pair + phrase; or the Comment names no code at all and the first sentence of + CMS's Response names the family. A summary that reaches the family only + in a later sentence or a list of other codes (a nursing-facility, + e-prescribing, TCM or CCM comment; 99339 listed beside 99091) is + dropped. The fact renders CMS's own words (``cms_summary_text``).""" out = [] + targets = {ln.target: ln for ln in family.links} + alias_of = {a: ln.target for ln in family.links for a in ln.aliases} for r in _rows(read_cms_summaries, con, family.key): + comment = bib.paragraph(r.item_key, r.comment_p_id) or "" + response = bib.paragraph(r.item_key, r.response_p_id) or "" + c_codes, c_phrase = _family_names(_opening(comment), family) + r_codes, r_phrase = _family_names(_opening(response), family) + no_codes = not find_codes(_expand_ranges(_PRINTED_MARK.sub(" ", comment))) + if not (c_codes or c_phrase) and not (no_codes and (r_codes or r_phrase)): + continue + first = comment_first_sentence(comment) + named = sorted(set(c_codes) | set(r_codes)) + links = sorted( + {alias_of.get(c, c) for c in c_codes if c in targets or c in alias_of} + ) out.append( _draft( "cms_summary", r.rule_year, - tuple(c for c in r.codes.split(",") if c), - cms_summary_text(r.quantifier, r.stance, r.rule_year, r.rule_kind), + tuple(c for c in named if c in family.codes or c in targets), + cms_summary_text( + first, + r.rule_year, + r.rule_kind, + r.quantifier, + r.response_action, + r.stance, + ), fr_para_cite(bib.store, r.item_key, r.comment_p_id), { "quantifier": r.quantifier, - "stance": r.stance, "response_action": r.response_action, + "model_reading_unreviewed": r.stance, + "comment_first_sentence": first, + "link_codes": " ".join(links), }, [fr_para_cite(bib.store, r.item_key, r.response_p_id)], ) @@ -1247,7 +1489,7 @@ def _stance_facts(con: Any, family: Any) -> list[Fact]: years = [_event_year(k) for k in events if _event_year(k)] year = min(years) if years else r.year ev = _eval(r) - acc = stance_accuracy_line(ev) + acc = stance_accuracy_line(ev) + (_in_sample_line(ev, r.period) if ev else "") cite = dataset_cite( "", f"Our classification of docket {r.period} comments", @@ -1315,9 +1557,16 @@ def _stance_facts(con: Any, family: Any) -> list[Fact]: # ── payment, impacts, money, seats ────────────────────────────────── -def _payment_rows(con: Any, family: Any) -> list[Any]: +def _payment_rows( + con: Any, family: Any, ends: Mapping[str, int] | None = None +) -> list[Any]: + """Every final and proposed payment row of the family's codes and of its + links from their ``since``; a link's rows stop at its end (*ends*: + target -> the year it is replaced), so a proposed payment for a code the + same NPRM proposed to replace is not shown (R96).""" from pfs.valuation import valuation + ends = ends or {} since = {ln.target: ln.since or 0 for ln in family.links} try: rows, _unpriced = valuation( @@ -1327,7 +1576,12 @@ def _payment_rows(con: Any, family: Any) -> list[Any]: if is_missing_table_error(exc): return [] raise - return [r for r in rows if r.code not in since or r.year >= since[r.code]] + return [ + r + for r in rows + if (r.code not in since or r.year >= since[r.code]) + and r.year < ends.get(r.code, 10**6) + ] def _payment_row(r: Any) -> PaymentRow: @@ -1579,13 +1833,13 @@ def _seat_facts( "seat", year, (), - f"On the {ry} RUC roster, the {s.organization} is a " + f"On the {ry} RUC roster{note}, the {s.organization} is a " f"{s.seat_type} seat.", _roster_cite(s, ry), {"roster_year": ry}, ) ) - cy, _cnote = roster_year({s.year for s in cpt}, year) + cy, cnote = roster_year({s.year for s in cpt}, year, "CPT_PANEL") if cy is not None: for s in (s for s in cpt if s.year == cy): plural = "s" if s.n_seats != 1 else "" @@ -1594,7 +1848,7 @@ def _seat_facts( "seat", year, (), - f"The {cy} CPT Editorial Panel roster lists {s.n_seats} seat" + f"The {cy} CPT Editorial Panel roster{cnote} lists {s.n_seats} seat" f"{plural} for {s.organization}.", _roster_cite(s, cy), {"roster_year": cy, "n_seats": s.n_seats}, @@ -1610,6 +1864,7 @@ def _seat_facts( class _Assembly: facts: list[Fact] years: list[int] + ends: list[Fact] rounds: list[RoundRow] payment: list[Any] psps: list[Any] @@ -1626,7 +1881,9 @@ def _assemble( bib = _Bib(store) rounds = _rows(read_rounds, con, family.key) + ends = _link_ends(bib, family) drafts: list[Fact] = [ + *ends, *_origin_facts(con, bib, family), *_guideline_facts(con, bib, family), *_event_facts(con, bib, family), @@ -1647,7 +1904,8 @@ def _assemble( and f.values.get("event_kind") != "cpt_edition" } ) - payment = _payment_rows(con, family) + end_years = {f.values["link_end"]: f.year for f in ends} + payment = _payment_rows(con, family, end_years) drafts += _payment_facts(payment) codes = [*family.codes, *(ln.target for ln in family.links)] psps = _rows(read_psps, con, codes) @@ -1692,8 +1950,18 @@ def _assemble( seen.add(key) unique.append(f) facts = [replace(f, id=f"F{i}") for i, f in enumerate(unique, 1)] + by_text = {(f.kind, f.year, f.text): f for f in facts} return _Assembly( - facts, years, rounds, payment, psps, seats, money, impacts, billing + facts, + years, + [by_text[(f.kind, f.year, f.text)] for f in ends], + rounds, + payment, + psps, + seats, + money, + impacts, + billing, ) @@ -1902,7 +2170,24 @@ _TIMELINE_ORDER = ( "comments", ) _TL_RANK = {k: i for i, k in enumerate(_TIMELINE_ORDER)} -_NOT_RULE_EVENTS = frozenset({"formation", "cpt_edition"}) +#: R95: lines that are always template copy (rounds, CMS's summaries, our +#: counts); the model writes only the other event lines +TEMPLATE_ONLY_EVENTS = frozenset({"valuation", "comments"}) +#: R94: what the first sentence of CMS's Comment paragraph is about decides +#: the event a summary sits beside; anything else stands as the year's +#: "comments" event, never filed under another +_CMS_TOPICS = ( + ( + re.compile( + r"\bRVUs?\b|work values?|\bvaluations?\b|\bRUC\b|practice expense|" + r"\bPEs?\b|direct PE", + re.I, + ), + ("valuation",), + ), + (re.compile(r"telehealth", re.I), ("telehealth_list",)), + (re.compile(r"\bcoding\b|descriptor|\bdelet", re.I), ("deleted", "cpt_panel")), +) def _tl_kind(f: Fact) -> str | None: @@ -1928,8 +2213,15 @@ def _tags(ids: Sequence[str]) -> str: def _with_tags(text: str, ids: Sequence[str]) -> str: - first = split_sentences(text)[0] if text else "" - return f"{first.rstrip('.').rstrip()} {_tags(ids)}." + """*text* with the tags closing every one of its sentences (after a + closing quotation mark, so a quote stays as written).""" + out = [] + for sent in split_sentences(text): + if re.search(r"[.!?][\"\u201d]$", sent): + out.append(f"{sent} {_tags(ids)}") + else: + out.append(f"{sent.rstrip('.').rstrip()} {_tags(ids)}.") + return " ".join(out) def template_event_line(e: TimelineEvent, facts: Mapping[str, Fact]) -> str: @@ -1957,12 +2249,30 @@ def template_event_line(e: TimelineEvent, facts: Mapping[str, Fact]) -> str: return _with_tags(lead.text, [lead.id]) +def _cms_target(f: Fact, groups: Mapping[tuple[int, str], Any]) -> str: + """The event of its year a CMS summary sits beside (R94): the link whose + code or placeholder the Comment names, else the topic of the Comment's + first sentence; ``comments`` when that event is not in the timeline.""" + year = f.year + # what the sentence says, not a descriptor it quotes in parentheses + quote = re.sub(r"\([^()]*\)", " ", str(f.values.get("comment_first_sentence", ""))) + for code in f.values.get("link_codes", "").split(): + if (year, f"link:{code}") in groups: + return f"link:{code}" + for pattern, kinds in _CMS_TOPICS: + if pattern.search(quote): + for k in kinds: + if (year, k) in groups: + return k + return "comments" + + def timeline(facts: Sequence[Fact]) -> list[TimelineEvent]: """Facts grouped by (year, kind): origins and formation with a year, CPT - edition changes, valuation rounds, lineage events and links; our stance - counts and CMS's summaries attach to the event of their year they speak - to (a year with only comments gets a ``comments`` event). Each event's - ``what_changed`` is its template line.""" + edition changes, valuation rounds, lineage events and links. Our stance + counts attach to the event of their year they speak to; CMS's summaries + to the event their Comment is about (``_cms_target``), else to the year's + ``comments`` event. Each event's ``what_changed`` is its template line.""" groups: dict[tuple[int, str], list[Fact]] = {} for f in facts: k = _tl_kind(f) @@ -1975,15 +2285,7 @@ def timeline(facts: Sequence[Fact]) -> list[TimelineEvent]: for k in prefer: if (year, k) in groups: return (year, k) - # a Panel meeting, a codebook edition or an origin is no rule event: - # comments on a rule never attach to one unless asked for by name - same = sorted( - (k for y, k in groups if y == year and k not in _NOT_RULE_EVENTS), - key=_tl_rank, - ) - if same: - return (year, same[0]) - groups[(year, "comments")] = [] + groups.setdefault((year, "comments"), []) return (year, "comments") for f in facts: @@ -1999,19 +2301,7 @@ def timeline(facts: Sequence[Fact]) -> list[TimelineEvent]: stance.setdefault(target(f.year, prefer), []).append(f.id) for f in facts: if f.kind == "cms_summary" and f.year: - prefer = [ - k - for (y, k), fs in sorted( - groups.items(), key=lambda kv: _tl_rank(kv[0][1]) - ) - if y == f.year - and k not in _NOT_RULE_EVENTS - and set(f.codes) & {c for g in fs for c in g.codes} - ] + ["valuation"] - prefer += sorted( - k for y, k in groups if y == f.year and k.startswith("link:") - ) - cms.setdefault(target(f.year, prefer), []).append(f.id) + cms.setdefault(target(f.year, [_cms_target(f, groups)]), []).append(f.id) by_id = {f.id: f for f in facts} out = [] for year, kind in sorted(groups, key=lambda yk: (yk[0], _tl_rank(yk[1]), yk[1])): @@ -2041,59 +2331,33 @@ _RULES = ( "write that carries meaning must come from a fact you cite. End every " "sentence with the tags of the facts it uses, written like [F12] or " "[F3][F7]. Copy every number, code and year exactly as a cited fact " - "writes it; never write a number or code the cited facts do not carry, " - "nor a range of codes that spans one they do not name. Keep proposed " - "values apart from final " - "values (a proposed value is never what CMS finalized). Keep CMS's own " - 'characterization of commenters ("CMS reported ...") apart from our ' - 'classified counts ("we classified ..."). Neutral, factual tone: no ' - "judgment words, no speculation, no advice. Return JSON " - '{"text": "..."}.' -) -SUMMARY_SYSTEM = ( - "You write the summary paragraph of a dossier on a Medicare code family, " - "at most 5 sentences, from numbered facts. " + _RULES + "writes it, beside the same code and role as in that fact; never write a " + "number or code the cited facts do not carry, nor a range of codes that " + "spans one they do not name. Never add a negation or a quantifier (not, " + "no, never, all, most, many, few, majority, none) a cited fact does not " + "use. Keep proposed values apart from final values (a proposed value is " + "never what CMS finalized). Neutral, factual tone: no judgment words, no " + 'speculation, no advice. Return JSON {"text": "..."}.' ) EVENT_SYSTEM = ( "You write ONE sentence saying what changed in one event of a Medicare " "code family's history, from numbered facts. " + _RULES ) -_PROSE_KINDS = frozenset( - {"origin", "formation", "event", "round", "cms_summary", "stance_split", "link"} -) -_SUMMARY_FACTS = 80 - - -def _prose_facts(facts: Sequence[Fact]) -> list[Fact]: - """What the model may read: reviewed facts of the narrative kinds (no - flagged paraphrase, no payment cell, no quote).""" - return [f for f in facts if not f.note and f.kind in _PROSE_KINDS] +#: the fact kinds an event line may be written from (our counts, CMS's +#: summaries, rounds and payment cells are template copy only, R95) +_PROSE_KINDS = frozenset({"origin", "formation", "event", "link"}) def _fact_lines(facts: Sequence[Fact]) -> str: return "\n".join(f"[{f.id}] {f.text}" for f in facts) -def _summary_input(facts: Sequence[Fact]) -> list[Fact]: - rank = { - "link": 0, - "formation": 1, - "round": 2, - "stance_split": 3, - "cms_summary": 4, - "origin": 5, - "event": 6, - } - pool = sorted( - _prose_facts(facts), key=lambda f: (rank.get(f.kind, 9), -(f.year or 0)) - ) - picked = {f.id for f in pool[:_SUMMARY_FACTS]} - return [f for f in facts if f.id in picked] - - def _event_input(e: TimelineEvent, by_id: Mapping[str, Fact]) -> list[Fact]: - ids = [*e.fact_ids, *e.stance_fact_ids, *e.cms_summary_fact_ids] - return [by_id[i] for i in ids if not by_id[i].note] + return [ + by_id[i] + for i in e.fact_ids + if not by_id[i].note and by_id[i].kind in _PROSE_KINDS + ] def _problems(text: str, allowed: Mapping[str, Fact], max_sentences: int) -> list[str]: @@ -2127,23 +2391,47 @@ def _ask_checked( def template_summary(facts: Sequence[Fact], events: Sequence[TimelineEvent]) -> str: - """At most five template lines: the newest and the earliest finalized - valuation, the newest deletion and formation, and the links, in time - order.""" + """The summary, template copy (R95), at most five sentences, each stated + once, spanning the family's history from its earliest finalized valuation + to its newest link: the earliest finalized valuation, the newest + deletion, the newest finalized valuation, the links in one sentence with + any replacement of a link (a link end) after a semicolon.""" by_id = {f.id: f for f in facts} picked: list[TimelineEvent] = [] vals = [e for e in events if e.kind == "valuation"] - if vals: - picked += [vals[-1]] + ([vals[0]] if len(vals) > 1 else []) - for kind in ("deleted", "formation"): - same = [e for e in events if e.kind == kind] - if same: - picked.append(same[-1]) - picked += [e for e in events if e.kind.startswith("link:")] - if not picked: - picked = list(events[:5]) - chosen = sorted(dict.fromkeys(picked[:5]), key=lambda e: (e.year, _tl_rank(e.kind))) - return " ".join(template_event_line(e, by_id) for e in chosen) + finals = [ + e for e in vals if any("CMS finalized" in by_id[i].text for i in e.fact_ids) + ] or vals + deleted = [e for e in events if e.kind == "deleted"] + if finals: + picked.append(finals[0]) + if deleted: + picked.append(deleted[-1]) + if len(finals) > 1: + picked.append(finals[-1]) + lines = [ + template_event_line(e, by_id) + for e in sorted(picked, key=lambda e: (e.year, _tl_rank(e.kind))) + ] + starts = [ + f for f in facts if f.kind == "link" and "link_kind" in f.values and not f.note + ] + ends = [f for f in facts if f.kind == "link" and "link_end" in f.values] + if starts: + items = [ + f"{f.codes[0]} (from CY{f.year})" + for f in sorted(starts, key=lambda f: (f.year or 0, f.codes)) + ] + sentence = ( + f"{_join_and(items)} can be reported with this family " + f"{_tags([f.id for f in starts])}" + ) + for f in ends: + sentence += f"; {f.text.rstrip('.')[:1].lower()}{f.text.rstrip('.')[1:]} {_tags([f.id])}" + lines.append(sentence + ".") + if not lines: + lines = [template_event_line(e, by_id) for e in events[:5]] + return " ".join(lines[:5]) def write_prose( @@ -2156,78 +2444,54 @@ def write_prose( limit: int = 0, fallbacks: list[str] | None = None, ) -> tuple[str, dict[int, str], str, bool]: - """The summary (at most five sentences) and one line per timeline event. - The model (*ask*, a ``JsonExtractor``) drafts each from the facts with - ``[F#]`` tags; ``check_prose`` and ``grounding_problems`` check every - sentence (citations and numbers; words, codes and the CMS-vs-ours + """The summary and one line per timeline event. The summary and the + lines of valuation and comments events are template copy (R95). For any + other event the model (*ask*, a ``JsonExtractor``) drafts one sentence + from the event's own facts with ``[F#]`` tags; ``check_prose`` and + ``grounding_problems`` check it (citations and numbers; words, codes, + negations and quantifiers, the role beside each number, the CMS-vs-ours attribution), one retry lists the problems, and a draft still failing - (or no model) is replaced by the template line. Flagged paraphrases, - payment cells and quotes are never given to the model. With a ledger *log* each unit is asked once (resumable, - *limit* caps new units); without *ask* the ledger recorded for - *replay_model* is replayed. Returns ``(summary, lines by event index, - extractor, check_passed)``; nothing unchecked is ever returned. A - *fallbacks* list receives the units that fell back to template copy - ("summary", "CY2023 valuation").""" + (or no model) is replaced by the template line. Flagged paraphrases are + never given to the model. With a ledger *log* each unit is asked once + (resumable, *limit* caps new units); without *ask* the ledger recorded + for *replay_model* is replayed. Returns ``(summary, lines by event + index, extractor, check_passed)``; nothing unchecked is ever returned. A + *fallbacks* list receives the model units that fell back to template + copy ("CY2023 deleted").""" by_id = {f.id: f for f in facts} model = getattr(ask, "model", None) if ask is not None else replay_model - units: list[tuple[str, str, str, dict[str, Fact], int, str]] = [] - s_in = _summary_input(facts) - units.append( - ( - "summary", - SUMMARY_SYSTEM, - f"Write the summary from these facts.\nFacts:\n{_fact_lines(s_in)}", - {f.id: f for f in s_in}, - 5, - template_summary(facts, events), - ) - ) - for i, e in enumerate(events): - e_in = _event_input(e, by_id) - units.append( - ( - f"event:{e.year}:{e.kind}", - EVENT_SYSTEM, - f"Event: CY{e.year}, {e.kind}.\nFacts:\n{_fact_lines(e_in)}", - {f.id: f for f in e_in}, - 2, - template_event_line(e, by_id), - ) - ) - texts: list[str] = [] - used_model = used_template = False + texts: list[str] = [template_summary(facts, events)] + used_model = False calls = 0 - for unit, system, user, allowed, max_s, fallback in units: + for e in events: + fallback = template_event_line(e, by_id) + e_in = _event_input(e, by_id) + if e.kind in TEMPLATE_ONLY_EVENTS or not e_in: + texts.append(fallback) + continue + unit = f"event:{e.year}:{e.kind}" + user = f"Event: CY{e.year}, {e.kind}.\nFacts:\n{_fact_lines(e_in)}" + allowed = {f.id: f for f in e_in} out: dict | None = None - if model is not None and allowed: - h = input_hash(system, user) + if model is not None: + h = input_hash(EVENT_SYSTEM, user) entry = log.get(unit, h, model, PROSE_PROMPT_VERSION) if log else None if entry is not None: out = entry.output elif ask is not None and not (limit and calls >= limit): calls += 1 - out = _ask_checked(ask, system, user, allowed, max_s) + out = _ask_checked(ask, EVENT_SYSTEM, user, allowed, 2) if log is not None: log.put(unit, h, model, PROSE_PROMPT_VERSION, out) # a recorded answer is re-checked: nothing unchecked is returned - if out is not None and not _problems(out["text"], allowed, max_s): + if out is not None and not _problems(out["text"], allowed, 2): texts.append(out["text"]) used_model = True else: texts.append(fallback) - used_template = True if fallbacks is not None: - fallbacks.append( - "summary" - if unit == "summary" - else "CY" + unit[6:].replace(":", " ", 1) - ) - if used_model and used_template: - extractor = f"model:{model}+template" - elif used_model: - extractor = f"model:{model}" - else: - extractor = "template" + fallbacks.append(f"CY{e.year} {e.kind}") + extractor = f"model:{model}+template" if used_model else "template" ok = all( not check_prose(t, by_id) and not grounding_problems(t, by_id) for t in texts ) @@ -2356,17 +2620,38 @@ def stakeholder_lines(d: Dossier) -> list[str]: # ── the dossier ───────────────────────────────────────────────────── -def _status(family: Any, payment: Sequence[Any]) -> str: +def _status(family: Any, ends: Sequence[Fact] = ()) -> str: + """Member codes active and deleted, and each link's span: from its + ``since``, or "CY2026 only; replaced by modifiers B1/B2 from CY2027" + when a later rule replaces it.""" + end = {f.values["link_end"]: f for f in ends} active = [m.code for m in family.members if m.until is None] deleted = [m for m in family.members if m.until is not None] text = f"{len(active)} active codes, {len(deleted)} deleted" if deleted: last = max(m.until for m in deleted) text += f" (last payable CY{last})" - if family.links: - text += "; linked: " + ", ".join( - f"{ln.target} from CY{ln.since}" for ln in family.links if ln.since + spans = [] + for ln in family.links: + if not ln.since: + continue + e = end.get(ln.target) + if e is None: + spans.append(f"{ln.target} from CY{ln.since}") + continue + last = e.year - 1 + span = ( + f"CY{ln.since} only" if last == ln.since else f"CY{ln.since}\u2013CY{last}" ) + by = e.values.get("replaced_by", "") + what = ( + f"replaced by {'modifiers' if 'modifiers' in e.text else 'codes'} {by}" + if by + else "replaced" + ) + spans.append(f"{ln.target} {span}; {what} from CY{e.year}") + if spans: + text += "; linked: " + ", ".join(spans) return text @@ -2432,7 +2717,7 @@ def build_dossier( family=family.key, name=family.name, codes=tuple(family.codes), - status=_status(family, a.payment), + status=_status(family, a.ends), years_active=_years_active(facts, a.payment), summary=summary, timeline=final_events, diff --git a/tests/cli/test_pfs_cli.py b/tests/cli/test_pfs_cli.py index 7f9dc16..d7d0ade 100644 --- a/tests/cli/test_pfs_cli.py +++ b/tests/cli/test_pfs_cli.py @@ -3708,6 +3708,7 @@ class TestDossier: ) assert res.exit_code == 0, res.output assert "model calls a run would make:" in res.output + assert "event lines" in res.output assert env.published == [] diff --git a/tests/pfs/fixtures/home_visits_fr.jsonl b/tests/pfs/fixtures/home_visits_fr.jsonl index 88418ed..8aac064 100644 --- a/tests/pfs/fixtures/home_visits_fr.jsonl +++ b/tests/pfs/fixtures/home_visits_fr.jsonl @@ -31,3 +31,5 @@ {"item_key": "78F558TF", "p_id": 2820, "page": 73431, "ordinal": 12, "text": "99304 through 99340 for initial, subsequent, discharge, and other nursing facility E/M services; new and ( printed page 73432) established patient domiciliary, rest home (eg, boarding home), or custodial care E/M services; and domiciliary, rest home (eg, assisted living facility), or home care plan oversight services; and", "title": "Medicare Program; CY 2011 PFS Final Rule", "date_published": "2010-11-29", "document_number": "2010-27969", "html_url": "https://www.federalregister.gov/documents/2010/11/29/2010-27969/medicare-program-payment-policies-under-the-physician-fee-schedule-and-other-revisions-to-part-b-for", "start_page": 73170, "end_page": 73860, "fr_volume": 75} {"item_key": "78F558TF", "p_id": 2821, "page": 73432, "ordinal": 1, "text": "99341 through 99350 for new and established patient home E/M visits. These codes are displayed in Table 66. All of these codes remain active in CY 2011 and there are no other codes used to describe these services.", "title": "Medicare Program; CY 2011 PFS Final Rule", "date_published": "2010-11-29", "document_number": "2010-27969", "html_url": "https://www.federalregister.gov/documents/2010/11/29/2010-27969/medicare-program-payment-policies-under-the-physician-fee-schedule-and-other-revisions-to-part-b-for", "start_page": 73170, "end_page": 73860, "fr_volume": 75} {"item_key": "GD84NJ2Y", "p_id": 8, "page": 55246, "ordinal": 8, "text": "Mail written comments (1 original and 2 copies) to the following address: Centers for Medicare & Medicaid Services, Department of Health and Human Services, Attention: CMS-1169-FC, P.O. Box 8013, Baltimore, MD 21244-8013.", "title": "Medicare Program; CY 2002 PFS Final Rule", "date_published": "2001-11-01", "document_number": "01-27275", "html_url": "https://www.federalregister.gov/documents/2001/11/01/01-27275/medicare-program-revisions-to-payment-policies-and-five-year-review-of-and-adjustments-to-the", "start_page": 55246, "end_page": 55503, "fr_volume": 66} +{"item_key": "JCWARTJD", "p_id": 3526, "page": 71320, "ordinal": 35, "text": "Additionally, in section II.D. of the final rule we are finalizing a policy to replace office/outpatient evaluation and management (O/O E/M) visit complexity add-on code, HCPCS code G2211 with modifiers that would allow for differential payment for ACO participants.", "title": "Medicare and Medicaid Programs; CY 2027 Payment Policies Under the Physician Fee Schedule and Other Changes to Part B Payment and Coverage Policies; Medicare Shared Savings Program Requirements; and Medicare Prescription Drug Inflation Rebate Program", "date_published": "2026-11-09", "document_number": "2026-22743", "html_url": "https://www.federalregister.gov/documents/2026/11/09/2026-22743/medicare-and-medicaid-programs-cy-2027-payment-policies-under-the-physician-fee-schedule-and-other", "start_page": 70992, "end_page": 72020, "fr_volume": 91} +{"item_key": "JCWARTJD", "p_id": 6666, "page": 71655, "ordinal": 1, "text": "We are finalizing the proposal to replace HCPCS code G2211 with a modifier (MOD1) that is set to 16 percent of the total RVUs for the E/M service on the claim. This policy is expected to have minimal impact given that the volume previously reported using HCPCS code G2211 would now be reported with the modifier, and that was accounted for using the weighted average of the percent increase for utilization of HCPCS code G2211 and adjusted for budget neutrality. The proposal to pay for services furnished in an ACO using a modifier (MOD2) that pays 32 percent of the total RVUs for the E/M service on the claim is expected to increase payment for practitioners that furnish a higher percentage of their services in ACOs. We note that MOD1 and MOD2 were placeholder codes. We are finalizing that MOD1 is now modifier B1, and MOD2 is now modifier B2. Our utilization estimates for MOD1 and MOD2 are reflected in the file titled “CY 2026 PFS Proposed Rule 2025 Utilization Data Crosswalked to 2027” available under “Downloads” for the CY 2027 PFS proposed rule on the CMS website at https://www.cms.gov/​medicare/​payment/​fee-schedules/​physician/​federal-regulation-notices.", "title": "Medicare and Medicaid Programs; CY 2027 Payment Policies Under the Physician Fee Schedule and Other Changes to Part B Payment and Coverage Policies; Medicare Shared Savings Program Requirements; and Medicare Prescription Drug Inflation Rebate Program", "date_published": "2026-11-09", "document_number": "2026-22743", "html_url": "https://www.federalregister.gov/documents/2026/11/09/2026-22743/medicare-and-medicaid-programs-cy-2027-payment-policies-under-the-physician-fee-schedule-and-other", "start_page": 70992, "end_page": 72020, "fr_volume": 91} diff --git a/tests/pfs/test_citecheck.py b/tests/pfs/test_citecheck.py index 7af0366..4e27e85 100644 --- a/tests/pfs/test_citecheck.py +++ b/tests/pfs/test_citecheck.py @@ -212,3 +212,122 @@ def test_cms_summary_stays_attributed_and_apart(): def test_a_range_the_fact_writes_may_be_restated(): facts = {"F1": _kind("The Panel deleted codes 99324-99328.", "formation")} assert grounding_problems("The Panel deleted codes 99324-99328 [F1].", facts) == [] + + +# ── R95: negation/quantifier and number-role guards (reviewer's probes) ── + +P = { + "F64": _kind( + 'In the CY2013 final rule, CMS summarized: "A few commenters were divided."', + "cms_summary", + "F64", + ), + "F325": _kind( + "For CY2023, the RUC recommended a work RVU of 1.00 for 99341 and CMS " + "finalized 1.00 (accept).", + fid="F325", + codes=("99341",), + ), + "F20": _kind( + "For CY2008, the RUC recommended a work RVU of 2.27 for 99326 and CMS " + "finalized 2.63 (refine).", + fid="F20", + codes=("99326",), + ), + "F369": _kind( + "Of 52 CY2023 commenters on this family in docket CMS-2022-0113, we " + "classified 28 with a firm position: 14 supported, 8 asked for " + "modifications and 6 opposed; 1 more was unclear.", + "stance_split", + "F369", + ), + "F2": _kind( + "For CY2023, the RUC recommended a work RVU of 1.65 for 99342 and CMS " + "finalized 1.65 (accept).", + fid="F2", + codes=("99342",), + ), +} + + +def test_probe_added_quantifier_is_rejected(): + [p] = grounding_problems("CMS reported that all commenters were divided [F64].", P) + assert p.kind == "negation" and "'all'" in p.detail + + +def test_probe_added_negation_is_rejected(): + [p] = grounding_problems( + "CMS did not finalize a work RVU of 1.00 for 99341 [F325].", P + ) + assert p.kind == "negation" + [q] = grounding_problems("CMS didn't finalize 1.00 for 99341 [F325].", P) + assert q.kind == "negation" + + +def test_probe_swapped_ruc_and_final_values_are_rejected(): + [p] = grounding_problems( + "For CY2008, the RUC recommended 2.63 for 99326 and CMS finalized 2.27 [F20].", + P, + ) + assert p.kind == "number_role" and "2.63" in p.detail + + +def test_probe_swapped_stance_counts_are_rejected(): + [p] = grounding_problems( + "Of 52 CY2023 commenters we classified, 14 opposed and 6 supported [F369].", P + ) + assert p.kind == "number_role" + + +def test_number_beside_another_code_is_rejected(): + [p] = grounding_problems("The RUC recommended 1.65 for 99341 [F325][F2].", P) + assert p.kind == "number_role" and "99341" in p.detail + + +def test_faithful_restatements_pass_the_guards(): + assert ( + grounding_problems( + "For CY2008, the RUC recommended 2.27 for 99326 and CMS finalized 2.63 [F20].", + P, + ) + == [] + ) + assert ( + grounding_problems( + "We classified 28 of 52 CY2023 commenters: 14 supported and 6 opposed [F369].", + P, + ) + == [] + ) + assert ( + grounding_problems("A few commenters were divided, CMS summarized [F64].", P) + == [] + ) + facts = {"F1": _kind("CMS did not finalize the change.", fid="F1")} + assert grounding_problems("CMS didn't finalize the change [F1].", facts) == [] + + +def test_number_roles(): + from pfs.citecheck import number_roles + + roles = number_roles(P["F20"].text) + assert roles["2.27"] == {("ruc", "99326")} and roles["2.63"] == {("final", "99326")} + assert number_roles("In 2023, the 99341 code [F1].") == {} + + +def test_a_number_only_in_values_has_no_role_to_check(): + facts = { + "F1": _kind( + "CMS finalized the value for 99341 (accept).", fid="F1", codes=("99341",) + ) + } + facts["F1"] = Fact( + "F1", + "round", + 2023, + ("99341",), + facts["F1"].text, + {"final": 1.0}, + Cite("fr_para", "x", "u"), + ) + assert grounding_problems("CMS finalized 1.00 for 99341 [F1].", facts) == [] diff --git a/tests/pfs/test_dossier.py b/tests/pfs/test_dossier.py index a71b675..f903443 100644 --- a/tests/pfs/test_dossier.py +++ b/tests/pfs/test_dossier.py @@ -93,65 +93,57 @@ def test_round_template_copy(): assert round_text(2008, "99326") == "" -@pytest.mark.parametrize( - "q,stance,want", - [ - ( - "majority", - "support", - "CMS reported that a majority of commenters supported the proposal (CY2023 final rule).", - ), - ( - "many", - "modify", - "CMS reported that many commenters asked for changes (CY2023 final rule).", - ), - ( - "several", - "oppose", - "CMS reported that several commenters opposed the proposal (CY2023 final rule).", - ), - ( - "some", - "mixed", - "CMS reported that some commenters were divided (CY2023 final rule).", - ), - ( - "few", - "unclear", - "CMS reported that a few commenters commented (CY2023 final rule).", - ), - ( - "all", - "support", - "CMS reported that all commenters supported the proposal (CY2023 final rule).", - ), - ( - "unspecified", - "support", - "CMS reported that commenters supported the proposal (CY2023 final rule).", - ), - ( - "one", - "support", - "CMS reported that one commenter supported the proposal (CY2023 final rule).", - ), - ( - "one", - "mixed", - "CMS reported that one commenter gave a mixed view (CY2023 final rule).", - ), - ], -) -def test_cms_summary_template(q, stance, want): - assert cms_summary_text(q, stance, 2023, "final") == want +def test_cms_summary_template(): + text = cms_summary_text( + "The majority of commenters supported our proposal.", + 2023, + "final", + "majority", + "acknowledged", + "support", + ) + assert text == ( + 'In the CY2023 final rule, CMS summarized: "The majority of commenters ' + "supported our proposal.\" CMS's quantifier: a majority of commenters. " + "CMS's response: acknowledged the comments. Stance of CMS's summary " + "(model reading, unreviewed): support." + ) + assert "no quantifier" in cms_summary_text( + "x", 2005, "final", "unspecified", "unclear", "mixed" + ) + assert "not determinable from its wording" in cms_summary_text( + "x", 2005, "final", "one", "unclear", "unclear" + ) + + +def test_comment_first_sentence_is_cms_words_without_cpt_text(): + para = ( + "Comment: One commenter requested clarification on CPT codes 99347 and " + "99348 ( Home visit for the evaluation and management of an established " + "patient (two of three)) and ( printed page 84506) more. Second sentence." + ) + assert dos.comment_first_sentence(para) == ( + "One commenter requested clarification on CPT codes 99347 and 99348 " + "[CPT descriptor omitted] and more." + ) + long = "Comment: " + "word " * 100 + "end." + first = dos.comment_first_sentence(long) + assert len(first) <= 300 and first.endswith("\u2026") + assert dos.comment_first_sentence("") == "" + assert ( + dos.strip_descriptors("codes (99341-99350) stay") == "codes (99341-99350) stay" + ) def test_stance_split_template(): assert ( stance_split_text(12, 2023, "the work RVU valuation", 6, 2, 3, 1) == "Of 12 CY2023 commenters we classified on the work RVU valuation, 6 " - "supported, 2 asked for modifications, 3 opposed and 1 were unclear." + "supported, 2 asked for modifications, 3 opposed and 1 was unclear." + ) + assert stance_split_text(1, 2023, "x", 1, 0, 0, 0) == ( + "Of 1 CY2023 commenter we classified on x, 1 supported, 0 asked for " + "modifications, 0 opposed and 0 were unclear." ) @@ -586,18 +578,31 @@ def test_guideline_facts(home_fixture): con, store, family, _ask = home_fixture facts = collect_facts(con, store, family) texts = [f.text for f in facts] - assert ( - "In the CPT 2022 codebook, 99341 is selected on history, examination and " - "medical decision making, with straightforward medical decision making and a " - "typical time of 20 minutes." in texts + first = next( + f + for f in facts + if f.text.startswith("In the earliest CPT edition on file (2022), 99341") ) + assert first.text == ( + "In the earliest CPT edition on file (2022), 99341 is selected on history, " + "examination and medical decision making, with straightforward medical " + "decision making and a typical time of 20 minutes." + ) + assert first.kind == "event" # never formation (R96) change = next( - f for f in facts if f.text.startswith("In the CPT 2024 codebook, 99341") + f for f in facts if f.text.startswith("From the CY2023 code set, 99341") ) - assert change.kind == "event" + assert change.year == 2023 and change.kind == "event" assert "medical decision making or total time" in change.text - assert "a time threshold of 15 minutes" in change.text - assert change.cite.label == "CPT 2024 p. 29" + assert "a time threshold of 15 minutes (CPT 2024 codebook)" in change.text + # dated to the code set that made it: the coding paragraph, CPT Changes, + # and the 2024 book as evidence + assert (change.cite.item_key, change.cite.p_id) == ("PETM225D", 1672) + assert [c.label for c in change.also] == [ + "CPT Changes 2023 p. 48", + "CPT 2024 p. 29", + ] + assert not [t for t in texts if t.startswith("In the CPT 2024 codebook")] deleted = next(f for f in facts if f.text.startswith("CPT deleted")) assert deleted.text == ( "CPT deleted 99324 and 99343 in its 2023 code set (last listed in the " @@ -618,12 +623,19 @@ def test_cms_summary_and_stance_split_are_separate_facts(home_fixture): facts = collect_facts(con, store, family) cms = [f for f in facts if f.kind == "cms_summary"] ours = [f for f in facts if f.kind == "stance_split"] - assert ( - "CMS reported that a majority of commenters supported the proposal (CY2023 final rule)." - in [f.text for f in cms] + majority = next( + f for f in cms if (f.cite.item_key, f.cite.p_id) == ("PETM225D", 1679) ) - majority = next(f for f in cms if "majority" in f.text) - assert (majority.cite.item_key, majority.cite.p_id) == ("PETM225D", 1679) + # CMS's own words, CMS's quantifier and the response action read by rule; + # the model's stance only as an unreviewed reading (R94) + assert majority.text == ( + 'In the CY2023 final rule, CMS summarized: "The majority of commenters ' + "supported our proposal to accept the RUC-recommended RVUs for all of the " + "Home or Residence Services codes.\" CMS's quantifier: a majority of " + "commenters. CMS's response: acknowledged the comments. Stance of CMS's " + "summary (model reading, unreviewed): support." + ) + assert majority.values["model_reading_unreviewed"] == "support" val = next(f for f in ours if "work RVU valuation" in f.text) assert val.text.startswith( "Of 3 CY2023 commenters we classified on the work RVU valuation, 1 supported, " @@ -633,7 +645,7 @@ def test_cms_summary_and_stance_split_are_separate_facts(home_fixture): assert not {f.id for f in cms} & {f.id for f in ours} assert all(f.cite.kind == "fr_para" for f in cms) assert all(f.cite.kind == "dataset" for f in ours) - assert all("CMS reported" not in f.text for f in ours) + assert all("CMS summarized" not in f.text for f in ours) assert all("we classified" not in f.text for f in cms) @@ -707,14 +719,12 @@ def test_seat_facts_use_the_prior_year_roster(home_fixture): "PRACTICE held 1 permanent and 0 rotating seats." in seats ) assert ( - "On the 2022 RUC roster, the Primary Care Rotating Seat is a rotating seat." - in seats + "On the 2022 RUC roster (the RUC that valued CY2023 met in 2022), the " + "Primary Care Rotating Seat is a rotating seat." in seats ) - assert any( - t.startswith( - "The 2026 CPT Editorial Panel roster lists 12 seats for national medical specialty societies" - ) - for t in seats + assert ( + "The 2026 CPT Editorial Panel roster (the Panel of 2026, for CY2027) lists " + "12 seats for national medical specialty societies." in seats ) @@ -727,6 +737,10 @@ def test_roster_year_falls_back_to_the_rule_year(): 2022, " (the RUC that valued CY2023 met in 2022)", ) + assert dos.roster_year({2026}, 2027, "CPT_PANEL") == ( + 2026, + " (the Panel of 2026, for CY2027)", + ) assert dos.roster_year(set(), 2023) == (None, "") @@ -899,20 +913,30 @@ def _echo(system, user): return {"text": f"{m.group(2).rstrip('.')} [{m.group(1)}]."} +def _eligible(events): + return [e for e in events if e.kind not in dos.TEMPLATE_ONLY_EVENTS] + + def test_model_prose_accepted(home_fixture): con, store, family, _ask = home_fixture facts = collect_facts(con, store, family) events = timeline(facts) ask = FakeAsk(default=_echo) summary, lines, extractor, ok = write_prose(facts, events, ask) - assert ok is True and extractor == "model:fake-14b" + assert ok is True and extractor == "model:fake-14b+template" assert len(lines) == len(events) ids = {f.id: f for f in facts} - assert check_prose(summary, ids) == [] + # the summary and the valuation and comments lines are template copy (R95) + assert summary == template_summary(facts, events) + for i, e in enumerate(events): + if e.kind in dos.TEMPLATE_ONLY_EVENTS: + assert lines[i] == template_event_line(e, ids) assert all(check_prose(line, ids) == [] for line in lines.values()) - assert len(ask.calls) == len(events) + 1 - # unreviewed paraphrases never reach the model - assert "Fixture paraphrase" not in "".join(u for _s, u in ask.calls) + assert len(ask.calls) == len(_eligible(events)) + prompts = "".join(u for _s, u in ask.calls) + # unreviewed paraphrases, our counts and CMS's summaries never reach it + assert "Fixture paraphrase" not in prompts + assert "we classified" not in prompts and "CMS summarized" not in prompts def test_prose_rejected_twice_falls_back_to_templates(home_fixture): @@ -925,7 +949,7 @@ def test_prose_rejected_twice_falls_back_to_templates(home_fixture): assert summary == template_summary(facts, events) ids = {f.id: f for f in facts} assert lines == {i: template_event_line(e, ids) for i, e in enumerate(events)} - assert len(ask.calls) == 2 * (len(events) + 1) # one retry each + assert len(ask.calls) == 2 * len(_eligible(events)) # one retry each retry = ask.calls[1][1] assert "uncited" in retry @@ -933,7 +957,7 @@ def test_prose_rejected_twice_falls_back_to_templates(home_fixture): def test_retry_with_problems_listed_can_succeed(home_fixture): con, store, family, _ask = home_fixture facts = collect_facts(con, store, family) - events = timeline(facts)[:1] + events = _eligible(timeline(facts))[:1] answers = iter([{"text": "No tag here."}, None]) def first_bad(system, user): @@ -942,22 +966,23 @@ def test_retry_with_problems_listed_can_succeed(home_fixture): ask = FakeAsk(default=first_bad) summary, lines, extractor, ok = write_prose(facts, events, ask) - assert extractor.startswith("model:fake-14b") - assert ok + assert extractor == "model:fake-14b+template" + assert ok and len(ask.calls) == 2 def test_mixed_prose_extractor(home_fixture): con, store, family, _ask = home_fixture facts = collect_facts(con, store, family) - events = timeline(facts)[:2] + events = _eligible(timeline(facts))[:2] + seen = [] - def only_summary(system, user): - return _echo(system, user) if "summary" in system.lower() else {"text": "x"} + def only_first(system, user): + seen.append(user) + return _echo(system, user) if len(seen) == 1 else {"text": "x"} - summary, lines, extractor, ok = write_prose( - facts, events, FakeAsk(default=only_summary) - ) - assert extractor == "model:fake-14b+template" + fb: list[str] = [] + write_prose(facts, events, FakeAsk(default=only_first), fallbacks=fb) + assert fb == [f"CY{events[1].year} {events[1].kind}"] def test_no_model_means_templates(home_fixture): @@ -971,14 +996,14 @@ def test_no_model_means_templates(home_fixture): def test_prose_ledger_resumes_and_replays(home_fixture, tmp_path): con, store, family, _ask = home_fixture facts = collect_facts(con, store, family) - events = timeline(facts)[:3] + events = _eligible(timeline(facts))[:3] log = RunLog(tmp_path / "prose.jsonl") ask = FakeAsk(default=_echo) first = write_prose(facts, events, ask, log=log, limit=2) assert len(ask.calls) == 2 - assert first[2] == "model:fake-14b+template" + assert first[1][2] == template_event_line(events[2], {f.id: f for f in facts}) second = write_prose(facts, events, ask, log=RunLog(tmp_path / "prose.jsonl")) - assert len(ask.calls) == 4 # the two not yet logged + assert len(ask.calls) == 3 # the one not yet logged replay = write_prose( facts, events, @@ -987,17 +1012,16 @@ def test_prose_ledger_resumes_and_replays(home_fixture, tmp_path): replay_model="fake-14b", ) assert replay == second - assert len(ask.calls) == 4 + assert len(ask.calls) == 3 def test_unchecked_model_text_is_never_returned(home_fixture): con, store, family, _ask = home_fixture facts = collect_facts(con, store, family) - events = timeline(facts)[:1] + events = _eligible(timeline(facts))[:1] # numbers no cited fact carries - ask = FakeAsk( - default=lambda s, u: {"text": f"99341 was paid $999.99 [{facts[0].id}]."} - ) + fid = events[0].fact_ids[0] + ask = FakeAsk(default=lambda s, u: {"text": f"99341 was paid $999.99 [{fid}]."}) summary, lines, extractor, ok = write_prose(facts, events, ask) assert "999.99" not in summary and "999.99" not in lines[0] assert extractor == "template" @@ -1235,9 +1259,11 @@ def test_round_edges(home_fixture): texts = [f.text for f in facts] assert "For CY2020, CMS proposed a work RVU of 1.60 for 99342." in texts assert "For CY2020, the RUC recommended a work RVU of 1.65 for 99342." in texts - assert "For CY2020, the RUC survey total time for 99342 was 97 minutes." in texts assert ( - "For CY2020, the RUC survey intra-service time for 99342 was 40 minutes." + "For CY2020, the RUC-recommended total time for 99342 was 97 minutes." in texts + ) + assert ( + "For CY2020, the RUC-recommended intra-service time for 99342 was 40 minutes." in texts ) assert "For CY2020, the comparator codes named for 99342 were 99215." in texts @@ -1324,10 +1350,13 @@ def test_guideline_edges(home_fixture): facts = collect_facts(con, store, family) texts = [f.text for f in facts] assert ( - "In the CPT 2021 codebook, 99341 carries with low medical decision making and add-on status." + "In the earliest CPT edition on file (2021), 99341 has low medical decision making and add-on status." + in texts + ) + assert ( + "In the earliest CPT edition on file (2021), 99350 is selected on other basis." in texts ) - assert "In the CPT 2021 codebook, 99350 is selected on other basis." in texts # identical paraphrases of one year collapse to one fact assert texts.count("A single-code point.") == 1 off = next(f for f in facts if f.text == "Off-family.") @@ -1784,32 +1813,60 @@ def _f(fid, kind, year, text, codes=(), **values): return Fact(fid, kind, year, tuple(codes), text, values, Cite("fr_para", "l", "u")) -def test_timeline_comment_only_years_and_fallbacks(): +def _cms(fid, year, quote, codes=(), links=""): + return Fact( + fid, + "cms_summary", + year, + tuple(codes), + f'In the CY{year} final rule, CMS summarized: "{quote}"', + {"comment_first_sentence": quote, "link_codes": links}, + Cite("fr_para", "l", "u"), + ) + + +def test_timeline_routes_cms_summaries_by_what_the_comment_is_about(): facts = [ - _f( - "F1", - "cms_summary", - 2005, - "CMS reported that one commenter commented (CY2005 final rule).", - ("99341",), - ), + _cms("F1", 2005, "One commenter asked about house calls.", ("99341",)), _f( "F2", - "event", + "round", 2010, - "99341 was created for CY2010.", + "For CY2010, CMS finalized a work RVU of 1.00 for 99341.", ("99341",), - event_kind="created", ), - _f( - "F3", - "cms_summary", - 2010, - "CMS reported that commenters commented (CY2010 final rule).", - ("99350",), - ), - _f( + _cms("F3", 2010, "Commenters said the RVUs were too low.", ("99350",)), + _cms( "F4", + 2010, + "Commenters asked about place of service 12 for 99341.", + ("99341",), + ), + _f( + "F5", + "link", + 2023, + "From CY2023, G0318 can be reported with this family (prolonged-add-on).", + ("G0318",), + link_kind="prolonged-add-on", + ), + _cms("F6", 2023, "A few commenters asked about GXXX3.", (), "G0318"), + _f( + "F7", + "event", + 2023, + "99341 is named in a telehealth-list paragraph of the CY2023 rule.", + ("99341",), + event_kind="telehealth_list", + ), + _cms( + "F8", + 2023, + "Commenters supported adding the codes to the telehealth list.", + ("99341",), + ), + _f( + "F9", "stance_split", 2011, "Of 2 CY2011 commenters we classified on the coding changes, 1 supported, 1 asked for modifications, 0 opposed and 0 were unclear. Preliminary.", @@ -1819,14 +1876,18 @@ def test_timeline_comment_only_years_and_fallbacks(): events = timeline(facts) by = {(e.year, e.kind): e for e in events} assert by[(2005, "comments")].cms_summary_fact_ids == ("F1",) - assert ( - by[(2005, "comments")].what_changed - == "CMS reported that one commenter commented (CY2005 final rule) [F1]." + assert by[(2005, "comments")].what_changed.startswith( + 'In the CY2005 final rule, CMS summarized: "One commenter' + ) + assert by[(2010, "valuation")].cms_summary_fact_ids == ("F3",) + # not about the valuation: its own comments event, never filed under it + assert by[(2010, "comments")].cms_summary_fact_ids == ("F4",) + assert by[(2023, "link:G0318")].cms_summary_fact_ids == ("F6",) + assert by[(2023, "telehealth_list")].cms_summary_fact_ids == ("F8",) + line = by[(2011, "comments")].what_changed + assert line.startswith("Of 2 CY2011 commenters") and line.endswith( + "Preliminary [F9]." ) - assert by[(2010, "created")].cms_summary_fact_ids == ("F3",) - assert by[(2011, "comments")].what_changed.startswith( - "Of 2 CY2011 commenters" - ) and by[(2011, "comments")].what_changed.endswith("[F4].") ids = {f.id: f for f in facts} assert check_prose(template_summary(facts, events), ids) == [] @@ -1834,30 +1895,34 @@ def test_timeline_comment_only_years_and_fallbacks(): def test_prose_too_long_is_a_problem(home_fixture): con, store, family, _ask = home_fixture facts = collect_facts(con, store, family) - events = timeline(facts)[:1] - six = " ".join(f"{facts[0].text.rstrip('.')} [{facts[0].id}]." for _ in range(6)) - ask = FakeAsk(default={"text": six}) + events = _eligible(timeline(facts))[:1] + lead = next(f for f in facts if f.id == events[0].fact_ids[0]) + three = " ".join(f"{lead.text.rstrip('.')} [{lead.id}]." for _ in range(3)) + ask = FakeAsk(default={"text": three}) summary, lines, extractor, ok = write_prose(facts, events, ask) assert extractor == "template" - assert "too long: 6 sentences, at most 5" in ask.calls[1][1] + assert "too long: 3 sentences, at most 2" in ask.calls[1][1] def test_in_sample_line(): assert dos._in_sample_line({"recall": 0.5}) == "" - line = dos._in_sample_line( - { - "in_sample_record": { - "recall": 0.407, - "direction": 0.818, - "n_items": 20, - "n_positions": 27, - "docket": "CMS-2022-0113", - } + ev = { + "in_sample_record": { + "recall": 0.407, + "direction": 0.818, + "n_items": 20, + "n_positions": 27, + "docket": "CMS-2022-0113", } + } + line = dos._in_sample_line(ev) + assert line == ( + " Tuning-set record on CMS-2022-0113 (20 hand-coded comments, 27 " + "positions): it finds 41% of hand-coded positions, and the direction is " + "right for 82% of those it finds." ) - assert ( - line.startswith(" Tuning-set record: (20 hand-coded comments") and "41%" in line - ) + assert dos._in_sample_line(ev, "CMS-2022-0113") == line + assert dos._in_sample_line(ev, "CMS-2025-0304") == "" def test_expand_range(): @@ -1872,11 +1937,243 @@ def test_expand_range(): def test_fallbacks_are_reported_per_unit(home_fixture): con, store, family, _ask = home_fixture facts = collect_facts(con, store, family) - events = timeline(facts)[:2] + events = _eligible(timeline(facts))[:2] fb: list[str] = [] write_prose(facts, events, FakeAsk(default=_echo), fallbacks=fb) assert fb == [] write_prose(facts, events, None, fallbacks=fb) - assert fb == ["summary", *(f"CY{e.year} {e.kind}" for e in events)] + assert fb == [f"CY{e.year} {e.kind}" for e in events] d = build_dossier(con, store, family) - assert d.fallbacks[0] == "summary" and len(d.fallbacks) == len(d.timeline) + 1 + assert len(d.fallbacks) == len(_eligible(d.timeline)) + assert "summary" not in d.fallbacks # template by rule, not a fallback + + +# ── fix round 1: R94 family filter, R96 link end, deletion cites ───── + +from pfs.codetables import CmsSummaryRow, write_cms_summaries # noqa: E402 + + +def test_cms_summaries_must_be_about_the_family(home_fixture): + con, store, family, _ask = home_fixture + paras = { + 99101: "Comment: Commenters disagreed that CPT codes 99091 and 99457 can be billed together.", + 99102: "Response: The codebook lists 99339 and 99340 among others.", + 99103: "Comment: Commenters asked us to revisit travel costs.", + 99104: "Response: The home visit codes 99347 and 99348 reflect travel. More.", + 99105: "Comment: Commenters thanked us.", + 99106: "Response: We appreciate the support.", + 99107: "Comment: Commenters supported the e-prescribing measure. They noted domiciliary care visit codes.", + 99108: "Response: We appreciate the support. The codes include 99324.", + 99109: "Comment: Commenters asked about home or residence services.", + } + for p_id, text in paras.items(): + _add_paragraph(store, p_id, text) + rows = [ + CmsSummaryRow( + family.key, + "PETM225D", + 2023, + "final", + c, + r, + "", + "unspecified", + "support", + "unclear", + "regex", + ) + for c, r in ( + (99101, 99102), + (99103, 99104), + (99105, 99106), + (99107, 99108), + (99109, 99999), + ) + ] + write_cms_summaries(con, family.key, rows) + cms = [f for f in collect_facts(con, store, family) if f.kind == "cms_summary"] + # about other codes (the family only listed in the response): dropped; + # nothing about the family: dropped; the family only in a later sentence: + # dropped; no code in the Comment and the Response opening with the + # family: kept; the Comment's first sentence naming the family: kept + assert sorted((f.cite.p_id, f.codes) for f in cms) == [ + (99103, ("99347", "99348")), + (99109, ()), + ] + assert any( + 'CMS summarized: "Commenters asked us to revisit travel costs."' in f.text + for f in cms + ) + + +def test_link_end_status_payment_and_summary(home_fixture): + con, store, family, _ask = home_fixture + d = build_dossier(con, store, family) + end = next(f for f in d.facts.values() if f.values.get("link_end") == "G2211") + assert end.text == "From CY2027, G2211 is replaced by modifiers B1 and B2." + assert (end.cite.item_key, end.cite.p_id) == ("JCWARTJD", 6666) + assert [(c.item_key, c.p_id) for c in end.also] == [("JCWARTJD", 3526)] + assert "G2211 CY2026 only; replaced by modifiers B1/B2 from CY2027" in d.status + assert any(e.kind == "link:G2211" and e.year == 2027 for e in d.timeline) + # the summary spans the earliest finalized valuation to the newest link, + # each statement once + sents = dos.split_sentences(d.summary) + assert len(sents) <= 5 + assert sents[0].startswith("For CY2008, CMS finalized work RVUs") + assert "G2211 (from CY2026)" in sents[-1] and "B2 (from CY2027)" in sents[-1] + assert "from CY2027, G2211 is replaced by modifiers B1 and B2" in sents[-1] + assert sum("deleted" in s for s in sents) == 1 + ids = d.facts + assert ( + check_prose(d.summary, ids) == [] + and dos.grounding_problems(d.summary, ids) == [] + ) + + +def test_payment_stops_at_a_link_end(home_fixture): + con, _store, family, _ask = home_fixture + rows = dos._payment_rows(con, family, {"99341": 2027}) + assert rows and max(r.year for r in rows if r.code == "99341") == 2026 + + +def test_link_end_edges(home_fixture): + con, store, family, _ask = home_fixture + from pfs.curated import CuratedLink + + _add_paragraph( + store, 99201, "We are finalizing our proposal to replace code G9001." + ) + _add_paragraph(store, 99202, "We proposed to replace code G9002 with G9003.") + _add_paragraph( + store, + 99203, + "We are finalizing a policy to replace code G9004 with code G9005.", + ) + fam = dataclasses.replace( + family, + links=( + CuratedLink("add-on", "G9000", None, None, "", 0, 0, 0), + CuratedLink("add-on", "G9001", 2020, None, "", 0, 0, 2020), + CuratedLink("add-on", "G9002", 2020, None, "", 0, 0, 2020), + CuratedLink("add-on", "G9004", 2023, None, "", 0, 0, 2023), + CuratedLink("add-on", "G9006", 2019, None, "", 0, 0, 2019), + CuratedLink("add-on", "G9005", 2023, None, "", 0, 0, 2023), + ), + ) + ends = dos._link_ends(dos._Bib(store), fam) + # G9001 has no successor named; G9002's sentence is a proposal; G9004's + # rule year is not after its since + assert [f.text for f in ends] == ["From CY2023, G9001 is replaced."] + assert dos._status(fam, ends).endswith( + "G9001 CY2020–CY2022; replaced from CY2023, G9002 from CY2020, G9004 from CY2023, G9006 from CY2019, G9005 from CY2023" + ) + fam2 = dataclasses.replace( + family, + links=( + CuratedLink("add-on", "G9004", 2022, None, "", 0, 0, 2022), + CuratedLink("add-on", "G9005", 2023, None, "", 0, 0, 2023), + ), + ) + [e] = dos._link_ends(dos._Bib(store), fam2) + assert e.text == "From CY2023, G9004 is replaced by codes G9005." + assert dos._status(fam2, [e]).endswith( + "G9004 CY2022 only; replaced by codes G9005 from CY2023, G9005 from CY2023" + ) + + +def test_deletions_cite_the_coding_paragraph(home_fixture): + con, store, family, _ask = home_fixture + facts = collect_facts(con, store, family) + for code in ("99324", "99328", "99343"): + f = next(f for f in facts if f.text == f"{code} was deleted for CY2023.") + assert (f.cite.item_key, f.cite.p_id) == ("PETM225D", 1672) + # 99339 has no curated paragraph: the paragraph that states it + f = next(f for f in facts if f.text == "99339 was deleted for CY2023.") + assert (f.cite.item_key, f.cite.p_id) == ("PETM225D", 390) + + +def test_edition_change_without_a_coding_paragraph(home_fixture): + con, store, family, _ask = home_fixture + write_guideline_facts( + con, + family.key, + [ + GuidelineFactRow( + family.key, "99339", 2022, "mdm_level", "low", "K22", "p. 5", "cpt-book" + ), + GuidelineFactRow( + family.key, + "99339", + 2024, + "mdm_level", + "high", + "K24", + "p. 6", + "cpt-book", + ), + GuidelineFactRow( + family.key, + "99343", + 2023, + "deleted_in_edition", + "2022", + "CH23", + "p. 48", + "cpt-book", + ), + ], + ) + facts = collect_facts(con, store, family) + f = next(f for f in facts if f.text.startswith("From the CY2023 code set, 99339")) + assert f.cite.label == "CPT Changes 2023 p. 48" + assert [c.label for c in f.also] == ["CPT 2024 p. 6"] + + +def test_nested_descriptor_is_stripped_once(): + assert dos.strip_descriptors( + "99341 ( Home visit, see 99342 ( Nested words)) end" + ) == ("99341 [CPT descriptor omitted] end") + + +def test_empty_model_answer_is_a_problem(home_fixture): + con, store, family, _ask = home_fixture + facts = collect_facts(con, store, family) + events = _eligible(timeline(facts))[:1] + ask = FakeAsk(default={"text": " "}) + write_prose(facts, events, ask) + assert "empty: no text" in ask.calls[1][1] + + +def test_template_summary_without_valuations_or_links(): + facts = [_cms("F1", 2005, "One commenter asked about house calls.", ("99341",))] + events = timeline(facts) + assert template_summary(facts, events).startswith( + 'In the CY2005 final rule, CMS summarized: "One commenter' + ) + + +def test_a_quote_keeps_its_closing_mark_and_every_sentence_is_tagged(): + line = dos._with_tags( + 'CMS summarized: "Codes were not valued." CMS said more.', ["F1"] + ) + assert line == 'CMS summarized: "Codes were not valued." [F1] CMS said more [F1].' + assert dos.split_sentences(line) == [ + 'CMS summarized: "Codes were not valued." [F1]', + "CMS said more [F1].", + ] + facts = { + "F1": _f( + "F1", + "cms_summary", + 2005, + 'CMS summarized: "Codes were not valued." CMS said more.', + ) + } + assert check_prose(line, facts) == [] and dos.grounding_problems(line, facts) == [] + + +def test_a_stray_full_stop_stays_with_its_sentence(): + assert dos.split_sentences('He said "x." [F1]. Next [F2].') == [ + 'He said "x." [F1].', + "Next [F2].", + ]