fix(llm): chat valuation never sends a CPT code's short descriptor (prompt and payload); HCPCS Level II text stays (I6)

This commit is contained in:
kert
2026-11-13 02:59:23 -05:00
parent e928e4c759
commit 749e033c61
3 changed files with 69 additions and 5 deletions

View File

@@ -15,6 +15,7 @@ from __future__ import annotations
import logging
import os
import re
import threading
from dataclasses import asdict, dataclass
from pathlib import Path
@@ -87,6 +88,19 @@ def cap_codes(det: Detection, max_codes: int) -> tuple[str, ...]:
return capped
#: a CPT code (Category I, II or III, or a PLA code): four digits and a
#: character. Its descriptor is AMA text; a HCPCS Level II code (a letter
#: and four digits) carries CMS's own public-domain descriptor.
_CPT_CODE = re.compile(r"\d{4}[0-9A-Z]")
def public_description(code: str, description: str) -> str:
"""*description* when it may be shown: blank for a CPT code (I6, the
AMA text never leaves in a prompt or a payload), kept for a HCPCS Level
II code."""
return "" if _CPT_CODE.fullmatch(code or "") else description
def _cap_valuation_rows(
rows: Sequence[ValuationRow], explicit: Sequence[str], max_rows: int
) -> list[ValuationRow]:
@@ -140,8 +154,10 @@ class ValuationEvidence:
f"; {r.status_note}" if r.status_note else ""
)
cf = _n(r.cf, 4, money=True) + (f" [{r.cf_note}]" if r.cf_note else "")
desc = public_description(r.code, r.description)
quoted = f' "{desc}"' if desc else ""
lines.append(
f'{r.label} {r.code} "{r.description}" — status {status}; '
f"{r.label} {r.code}{quoted} — status {status}; "
f"work {_n(r.work)}; PE {_n(r.pe_nf)} non-fac / {_n(r.pe_f)} fac; "
f"MP {_n(r.mp)}; total {_n(r.total_nf)} non-fac / {_n(r.total_f)} fac; "
f"CF {cf} → payment {_n(r.pay_nf, money=True)} non-fac / "
@@ -171,7 +187,10 @@ class ValuationEvidence:
"type": "valuation",
"codes": list(self.codes),
"families": list(self.families),
"rows": [asdict(r) for r in self.rows],
"rows": [
{**asdict(r), "description": public_description(r.code, r.description)}
for r in self.rows
],
"provenance": provenance,
"unpriced": list(self.unpriced),
}

View File

@@ -603,7 +603,7 @@
['pay_nf', 'Pay NF'], ['pay_f', 'Pay F'],
];
function fmt(key, v) {
if (v === null || v === undefined) return '—';
if (v === null || v === undefined || (key === 'description' && !v)) return '—';
if (key === 'cf') return '$' + Number(v).toFixed(4);
if (key === 'pay_nf' || key === 'pay_f') return '$' + Number(v).toFixed(2);
if (typeof v === 'number') return v.toFixed(2);
@@ -620,13 +620,16 @@
const t = document.createElement('table');
t.className = 'valuation';
if (caption) t.createCaption().textContent = caption;
// a CPT code's descriptor is AMA text and arrives blank (only HCPCS
// Level II text is shown): no Description column when none has one
const cols = VAL_COLS.filter(([k]) => k !== 'description' || rs.some((r) => r.description));
const thead = t.createTHead().insertRow();
for (const [, name] of VAL_COLS) { const th = document.createElement('th'); th.textContent = name; thead.appendChild(th); }
for (const [, name] of cols) { const th = document.createElement('th'); th.textContent = name; thead.appendChild(th); }
const tb = t.createTBody();
for (const r of rs) {
const tr = tb.insertRow();
if (r.proposed) tr.className = 'proposed'; // tr.proposed: italic
for (const [key] of VAL_COLS) {
for (const [key] of cols) {
const td = tr.insertCell();
td.textContent = fmt(key, r[key]);
// why a status is not paid / which of the year's CFs this is

View File

@@ -86,6 +86,36 @@ class TestEvidenceText:
"Not priced in the indexed fee schedule: Z9999"
)
def test_cpt_descriptors_never_reach_the_prompt_or_the_payload(self):
"""I6: a CPT code's short descriptor is AMA text; the code number is
enough. A HCPCS Level II descriptor is CMS's own text and stays."""
ev = ValuationEvidence(
("99341", "0591T", "G0318"),
(),
(
_row(code="99341", description="Short text of a CPT code"),
_row(code="0591T", description="Short text of a category III code"),
_row(code="G0318", description="Prolong home e/m visit"),
),
(),
)
block = ev.prompt_block()
assert "Short text" not in block
assert "[PFS CY2026 Addendum B] 99341 — status A" in block
assert "[PFS CY2026 Addendum B] 0591T — status A" in block
assert (
'[PFS CY2026 Addendum B] G0318 "Prolong home e/m visit" — status' in block
)
rows = {r["code"]: r["description"] for r in ev.payload()["rows"]}
assert rows == {"99341": "", "0591T": "", "G0318": "Prolong home e/m visit"}
def test_public_description(self):
assert evidence.public_description("99213", "x") == ""
assert evidence.public_description("0042U", "x") == ""
assert evidence.public_description("G2211", "Complex e/m visit add on") == (
"Complex e/m visit add on"
)
def test_prompt_block_labels_the_cf_and_an_unpaid_status(self):
ev = ValuationEvidence(
("G0559",),
@@ -1331,3 +1361,15 @@ class TestManualSources:
with patch("llm.evidence._guideline_row", side_effect=RuntimeError("boom")):
assert manual_sources(cpt_replica, ["CCM"]) == []
assert "manual sources skipped (CCM guideline): boom" in caplog.text
def test_chat_page_drops_an_all_blank_description_column():
"""I6: the chat table tolerates blank CPT descriptors (no empty
Description column; a blank cell reads as a dash)."""
from pathlib import Path
import llm
html = (Path(llm.__file__).parent / "web" / "chat.html").read_text()
assert "k !== 'description' || rs.some((r) => r.description)" in html
assert "(key === 'description' && !v)) return '—'" in html