fix(pfs): curated validation never raises at import; stack pfs families --check lists the problems (I7)

This commit is contained in:
kert
2026-11-13 02:37:37 -05:00
parent 9a266b0fd0
commit 2f69da97eb
5 changed files with 156 additions and 13 deletions

View File

@@ -65,7 +65,7 @@ from pfs.codetables import (
)
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.curated import apply_curated, curated_problems, load_curated
from pfs.dossier import (
build_dossier,
collect_facts,
@@ -583,8 +583,21 @@ def families(
verbose: bool = typer.Option(
False, "--verbose", help="Print every family's members, not just the summary."
),
check: bool = typer.Option(
False,
"--check",
help="Validate curated.yaml: list every problem and exit 1 if any.",
),
) -> None:
"""Derive families from pfs.code_element / pfs.code_event / pfs.rvu."""
if check:
problems = curated_problems()
for p in problems:
typer.echo(f"curated: {p}")
if problems:
raise typer.Exit(1)
typer.echo(f"curated: {len(load_curated())} families, no problems")
return
if write:
with _batch() as con:
ensure_tables(con)

View File

@@ -7,6 +7,7 @@ overlays the rows on the derived table.
from __future__ import annotations
import logging
from dataclasses import dataclass
from importlib import resources
from pathlib import Path
@@ -16,6 +17,8 @@ import yaml
from pfs.codetables import FamilyRow
log = logging.getLogger(__name__)
ROLES = ("base", "add-on", "predecessor", "successor", "member")
KINDS = ("add-on", "modifier", "prolonged-add-on")
@@ -181,26 +184,70 @@ def _parse(key: str, raw: Any) -> CuratedFamily:
)
def load_curated(path: Path | None = None) -> dict[str, CuratedFamily]:
def default_source() -> Any:
"""The packaged ``curated.yaml``."""
return resources.files("pfs").joinpath("curated.yaml")
def load_curated(
path: Path | None = None,
*,
strict: bool = True,
problems: list[str] | None = None,
) -> dict[str, CuratedFamily]:
"""The curated families of *path* (the packaged file by default). With
*strict* the first problem raises ``ValueError``; otherwise (the
import-time registry, I7) a family that fails validation, or that claims
a code another family already holds, is logged and skipped, an
unreadable file gives ``{}``, and each problem is appended to
*problems*."""
from pfs.families import normalize_key # lazy: families imports this module
src = path or resources.files("pfs").joinpath("curated.yaml")
data = yaml.safe_load(src.read_text(encoding="utf-8")) or {}
found = problems if problems is not None else []
def bad(message: str) -> None:
if strict:
raise ValueError(message)
found.append(message)
log.error("curated family skipped: %s", message)
src = path or default_source()
try:
data = yaml.safe_load(src.read_text(encoding="utf-8")) or {}
except (OSError, yaml.YAMLError) as exc:
if strict:
raise
bad(f"curated.yaml unreadable: {exc}")
return {}
if not isinstance(data, dict):
bad("curated.yaml must map family keys to families")
return {}
out: dict[str, CuratedFamily] = {}
owner: dict[str, str] = {}
for raw_key, raw in data.items():
key = normalize_key(str(raw_key))
fam = _parse(key, raw)
for code in fam.codes:
if code in owner and owner[code] != key:
raise ValueError(
f"{key} member {code}: already a member of {owner[code]}"
)
owner[code] = key
try:
fam = _parse(key, raw)
except ValueError as exc:
bad(str(exc))
continue
clash = next((c for c in fam.codes if owner.get(c, key) != key), None)
if clash is not None:
bad(f"{key} member {clash}: already a member of {owner[clash]}")
continue
owner.update(dict.fromkeys(fam.codes, key))
out[key] = fam
return out
def curated_problems(path: Path | None = None) -> list[str]:
"""Every validation problem of *path* (the packaged file by default):
what ``stack pfs families --check`` reports; ``[]`` when it is clean."""
problems: list[str] = []
load_curated(path, strict=False, problems=problems)
return problems
def curated_rows(families: Mapping[str, CuratedFamily]) -> list[FamilyRow]:
return [
FamilyRow(

View File

@@ -150,10 +150,13 @@ FAMILIES: dict[str, Family] = dict(HAND_FAMILIES)
@functools.cache
def _curated_registry() -> dict[str, CuratedFamily]:
"""The curated registry, built at import: never raises (I7). A family
that fails validation is logged and skipped; ``stack pfs families
--check`` lists the problems and exits non-zero."""
# Lazy: ``pfs.curated`` imports names from this module.
from pfs.curated import load_curated
return load_curated()
return load_curated(strict=False)
def _load_curated_families() -> dict[str, Family]:

View File

@@ -1292,6 +1292,27 @@ class TestFamiliesCurated:
assert len([r for r in fams if r.key == "HOME-VISITS"]) == 20
assert "curated 1" in res.output
def test_check_passes_on_the_packaged_file(self, con):
res = runner.invoke(app, ["pfs", "families", "--check"])
assert res.exit_code == 0, res.output
assert "curated: 1 families, no problems" in res.output
def test_check_fails_and_lists_every_problem(self, con, monkeypatch, tmp_path):
import pfs.curated as cur
ok = "{code: '99341', role: base, cite: {item_key: K, p_id: 1}}"
bad = tmp_path / "c.yaml"
bad.write_text(
f"A:\n name: A\n members:\n - {ok}\n"
f"B:\n name: B\n members:\n - {ok}\n"
"C:\n members: []\n"
)
monkeypatch.setattr(cur, "default_source", lambda: bad)
res = runner.invoke(app, ["pfs", "families", "--check"])
assert res.exit_code == 1
assert "curated: B member 99341: already a member of A" in res.output
assert "curated: C: missing name" in res.output
def test_asker_builds_json_extractor_on_instruct_model(monkeypatch):
from llm import config as llm_config

View File

@@ -5,7 +5,7 @@ from __future__ import annotations
import pytest
from pfs.codetables import FamilyRow
from pfs.curated import apply_curated, curated_rows, load_curated
from pfs.curated import apply_curated, curated_problems, curated_rows, load_curated
def _write(tmp_path, body):
@@ -239,3 +239,62 @@ def test_alias_parsing_and_errors(tmp_path):
]:
with pytest.raises(ValueError, match=needle):
load_curated(_write(tmp_path, bad))
# ── I7: validation never raises at import; --check reports ───────────
_GOOD = "{code: '99341', role: base, cite: {item_key: K, p_id: 1}}"
def test_lenient_load_skips_a_bad_family_and_keeps_the_rest(tmp_path, caplog):
body = (
f"A:\n name: A\n members:\n - {_GOOD}\n"
f"B:\n name: B\n members:\n - {_GOOD}\n"
"C:\n members: []\n"
)
problems: list[str] = []
out = load_curated(_write(tmp_path, body), strict=False, problems=problems)
assert list(out) == ["A"]
assert problems == [
"B member 99341: already a member of A",
"C: missing name",
]
assert "already a member of A" in caplog.text
def test_lenient_load_of_unreadable_yaml_is_empty(tmp_path):
problems: list[str] = []
assert (
load_curated(_write(tmp_path, "A: [\n"), strict=False, problems=problems) == {}
)
assert len(problems) == 1 and problems[0].startswith("curated.yaml unreadable")
assert curated_problems(_write(tmp_path, "- just\n- a list\n")) == [
"curated.yaml must map family keys to families"
]
def test_curated_problems_of_the_packaged_file_is_empty():
assert curated_problems() == []
def test_the_import_time_registry_never_raises(tmp_path, monkeypatch):
"""``pfs.families`` builds its curated registry at import: a second
family sharing a code is logged and skipped, never raised."""
import pfs.curated as cur
from pfs import families
bad = _write(
tmp_path,
f"A:\n name: A\n members:\n - {_GOOD}\n"
f"B:\n name: B\n members:\n - {_GOOD}\n",
)
monkeypatch.setattr(cur, "default_source", lambda: bad)
registry = families._curated_registry.__wrapped__()
assert list(registry) == ["A"]
def test_strict_load_of_unreadable_yaml_raises(tmp_path):
import yaml
with pytest.raises(yaml.YAMLError):
load_curated(_write(tmp_path, "A: [\n"))