Coverage for src/romfarmer/planner/passes/rating.py: 23%
35 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-09-05 02:19 -0700
« prev ^ index » next coverage.py v7.15.4, created at 2026-09-05 02:19 -0700
1"""rating pass — filter by scraped/external rating.
3Three sub-modes (all use ``GameUnit.rating``, populated by ``CatalogBuilder``
4from the ``KnowledgeBase``):
6- ``manifest.rating_min``: remove units whose rating < threshold (unit
7 interval, 0.0–1.0). Units with ``rating=None`` are *kept* unless
8 ``manifest.rating_unrated == "drop"``.
9- ``manifest.rating_top_n``: keep only the top-N rated units (ties broken
10 by ``canonical_name`` asc for determinism). Units with ``rating=None``
11 sort below rated units.
12- Both together: apply ``rating_min`` first, then ``rating_top_n``.
14If neither is set, the pass is a no-op.
15"""
17from __future__ import annotations
19from typing import TYPE_CHECKING
21from romfarmer.ir.catalog import Catalog, GameUnit, PassResult, PassTrace, UnitId
23if TYPE_CHECKING:
24 from romfarmer.analysis.knowledge import KnowledgeBase
25 from romfarmer.ir.manifest import BuildManifest
26 from romfarmer.planner.costmodel import CostModel
28PASS_NAME = "rating"
31def _rating_sort_key(unit: GameUnit) -> tuple[float, str]:
32 """(rating desc, name asc) — None ratings sort below 0.0."""
33 return (-(unit.rating if unit.rating is not None else -1.0), unit.canonical_name)
36def run(
37 catalog: Catalog,
38 manifest: BuildManifest,
39 kb: KnowledgeBase,
40 cost_model: CostModel,
41) -> PassResult:
42 """Apply rating-min and/or top-N filtering."""
43 if manifest.rating_min is None and manifest.rating_top_n is None:
44 return PassResult(
45 catalog=catalog,
46 trace=PassTrace(pass_name=PASS_NAME, removed=(), added=()),
47 )
49 removed: list[tuple[UnitId, str]] = []
50 survivors: list[GameUnit] = list(catalog.units)
52 # 1. Minimum threshold
53 if manifest.rating_min is not None:
54 threshold = manifest.rating_min
55 next_survivors: list[GameUnit] = []
56 drop_unrated = manifest.rating_unrated == "drop"
57 for unit in survivors:
58 if unit.rating is None:
59 if drop_unrated:
60 removed.append(
61 (unit.unit_id, f"rating: unrated (unrated=drop, min {threshold:.2f})")
62 )
63 else:
64 next_survivors.append(unit)
65 elif unit.rating < threshold:
66 removed.append(
67 (
68 unit.unit_id,
69 f"rating {unit.rating:.2f} < min {threshold:.2f}",
70 )
71 )
72 else:
73 next_survivors.append(unit)
74 survivors = next_survivors
76 # 2. Top-N cap
77 if manifest.rating_top_n is not None:
78 n = manifest.rating_top_n
79 sorted_survivors = sorted(survivors, key=_rating_sort_key)
80 kept = sorted_survivors[:n]
81 cut = sorted_survivors[n:]
82 for unit in cut:
83 removed.append(
84 (
85 unit.unit_id,
86 f"rating top-{n} cap (rating={unit.rating})",
87 )
88 )
89 survivors = kept
91 removed_ids = {uid for uid, _ in removed}
92 new_catalog = catalog.without(removed_ids)
93 return PassResult(
94 catalog=new_catalog,
95 trace=PassTrace(
96 pass_name=PASS_NAME,
97 removed=tuple(removed),
98 added=(),
99 ),
100 )