diff --git a/src/cli/pfs.py b/src/cli/pfs.py index 4d1b983..634df08 100644 --- a/src/cli/pfs.py +++ b/src/cli/pfs.py @@ -1294,6 +1294,6 @@ def commenters( f"{counts['pending']} comments not yet identified; rerun to resume" ) with _batch() as bcon: - n = write_commenters(bcon, rows, docket=docket) + n = write_commenters(bcon, rows, docket=docket, family=fam.key) typer.echo(f"wrote {n} commenters") _publish() diff --git a/src/pfs/codetables.py b/src/pfs/codetables.py index a4c0c06..37d2ee9 100644 --- a/src/pfs/codetables.py +++ b/src/pfs/codetables.py @@ -373,6 +373,8 @@ class CommenterRow: reg_category: str #: ``regs`` (no model answer) or ``model:@`` extractor: str + #: the curated family whose comment set this row belongs to (R57) + family: str = "" ORIGIN_KINDS = ( @@ -485,7 +487,7 @@ CREATE TABLE IF NOT EXISTS pfs.cms_comment_summary ( CREATE TABLE IF NOT EXISTS pfs.commenter ( item_key VARCHAR, docket VARCHAR, comment_id VARCHAR, org_name VARCHAR, org_type VARCHAR, specialty VARCHAR, form_group VARCHAR, form_size INTEGER, - basis VARCHAR, reg_category VARCHAR, extractor VARCHAR); + basis VARCHAR, reg_category VARCHAR, extractor VARCHAR, family VARCHAR); """ @@ -534,6 +536,10 @@ def ensure_tables(con: Any) -> None: ) # R32: the paragraph stating an origin's reference. con.execute("ALTER TABLE pfs.code_origin ADD COLUMN IF NOT EXISTS ref_p_id INTEGER") + # R57: pfs.commenter rows are per family. Every row written before the + # column existed came from the only family run, HOME-VISITS. + con.execute("ALTER TABLE pfs.commenter ADD COLUMN IF NOT EXISTS family VARCHAR") + con.execute("UPDATE pfs.commenter SET family = 'HOME-VISITS' WHERE family IS NULL") def _insert(con: Any, table: str, rows: Sequence[Any]) -> int: @@ -919,31 +925,42 @@ def read_cms_summaries(con: Any, family: str) -> list[CmsSummaryRow]: def write_commenters( - con: Any, rows: Sequence[CommenterRow], *, docket: str = "" + con: Any, rows: Sequence[CommenterRow], *, docket: str = "", family: str = "" ) -> int: - """Replace the rows' items. With *docket*, first drop every stored row of - that docket: a rerun that no longer selects an item (R54) must not leave - its old row behind.""" + """Replace the rows' items (per row family). With *docket* and *family*, + first drop every stored row of that family in that docket: a rerun that no + longer selects an item (R54) must not leave its old row behind, and + another family's rows for the same docket are never touched (R57).""" ensure_tables(con) if docket: - con.execute("DELETE FROM pfs.commenter WHERE docket = ?", [docket]) + con.execute( + "DELETE FROM pfs.commenter WHERE docket = ? AND family = ?", + [docket, family], + ) if not rows: return 0 con.executemany( - "DELETE FROM pfs.commenter WHERE item_key = ?", [(r.item_key,) for r in rows] + "DELETE FROM pfs.commenter WHERE item_key = ? AND family = ?", + [(r.item_key, r.family) for r in rows], ) return _insert(con, "pfs.commenter", rows) def read_commenters( - con: Any, item_keys: Sequence[str] | None = None + con: Any, item_keys: Sequence[str] | None = None, family: str | None = None ) -> list[CommenterRow]: - sql = "SELECT * FROM pfs.commenter" + where: list[str] = [] params: list[Any] = [] if item_keys is not None: if not item_keys: return [] - sql += f" WHERE item_key IN ({','.join('?' * len(item_keys))})" - params = list(item_keys) - rows = con.execute(sql + " ORDER BY docket, item_key", params).fetchall() + where.append(f"item_key IN ({','.join('?' * len(item_keys))})") + params += list(item_keys) + if family is not None: + where.append("family = ?") + params.append(family) + sql = "SELECT * FROM pfs.commenter" + if where: + sql += " WHERE " + " AND ".join(where) + rows = con.execute(sql + " ORDER BY docket, item_key, family", params).fetchall() return [CommenterRow(*r) for r in rows] diff --git a/src/pfs/commenters.py b/src/pfs/commenters.py index bdc3438..2e6f732 100644 --- a/src/pfs/commenters.py +++ b/src/pfs/commenters.py @@ -571,6 +571,7 @@ def identify( "regs_gov_fields", "", "blocked:mirror", + family.key, ) ) continue @@ -594,6 +595,7 @@ def identify( extractor=( f"model:{model}@{IDENTITY_PROMPT_VERSION}" if letter else "regs" ), + family=family.key, ) ) return rows, {**counts, "pending": pending, "blocked": len(blocked)} diff --git a/tests/pfs/test_commenters.py b/tests/pfs/test_commenters.py index 067fdbf..bf5aa98 100644 --- a/tests/pfs/test_commenters.py +++ b/tests/pfs/test_commenters.py @@ -795,3 +795,69 @@ def test_prompt_defines_vendor_and_sends_trade_associations_to_other(): and "never an association" in system ) assert "other (including trade associations, coalitions" in system + + +def test_family_rewrite_leaves_other_families_rows_alone(con): + def row(k, fam, org="AAFP"): + return CommenterRow( + k, "D", "c", org, "specialty_society", "", "", 0, "both", "", "regs", fam + ) + + write_commenters(con, [row("K1", "A"), row("K2", "A")], docket="D", family="A") + write_commenters(con, [row("K1", "B"), row("K3", "B")], docket="D", family="B") + assert [(r.item_key, r.family) for r in read_commenters(con)] == [ + ("K1", "A"), + ("K1", "B"), + ("K2", "A"), + ("K3", "B"), + ] + # rewriting A for D drops A's stale K2 but keeps all of B's + write_commenters(con, [row("K1", "A", org="AMA")], docket="D", family="A") + got = read_commenters(con) + assert [(r.item_key, r.family, r.org_name) for r in got] == [ + ("K1", "A", "AMA"), + ("K1", "B", "AAFP"), + ("K3", "B", "AAFP"), + ] + assert [r.item_key for r in read_commenters(con, family="B")] == ["K1", "K3"] + assert [r.item_key for r in read_commenters(con, ["K1"], family="A")] == ["K1"] + + +def test_legacy_commenter_table_is_migrated_to_home_visits(): + c = duckdb.connect(":memory:") + c.execute("CREATE SCHEMA pfs") + c.execute( + "CREATE TABLE pfs.commenter (item_key VARCHAR, docket VARCHAR, comment_id VARCHAR, " + "org_name VARCHAR, org_type VARCHAR, specialty VARCHAR, form_group VARCHAR, " + "form_size INTEGER, basis VARCHAR, reg_category VARCHAR, extractor VARCHAR)" + ) + c.execute( + "INSERT INTO pfs.commenter VALUES ('K1','D','c','','unknown','','',0,'','','regs')" + ) + ensure_tables(c) + assert [r.family for r in read_commenters(c)] == ["HOME-VISITS"] + + +def test_identify_sets_family(tmp_path, con, store): + rows, _ = identify( + engine_with([("K1", "a")])[0], + store, + con, + HOME, + docket="D", + mirror=FakeMirror(), + ask=FakeAsk(None), + log=RunLog(tmp_path / "l.jsonl"), + ) + assert rows[0].family == "HOME-VISITS" + rows, _ = identify( + engine_with([("K1", "a")])[0], + store, + con, + HOME, + docket="D", + mirror=FakeMirror(fail=True), + ask=FakeAsk(None), + log=RunLog(tmp_path / "m.jsonl"), + ) + assert rows[0].extractor == "blocked:mirror" and rows[0].family == "HOME-VISITS"