Coverage for src/romfarmer/planner/passes/generation.py: 26%
34 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"""generation pass — 1G1Gen cross-platform deduplication.
3Implements "1 Game 1 Generation": when a game exists on multiple platforms
4within the same console generation, keep only the version from the
5highest-priority platform.
7Input: a *merged* ``Catalog`` spanning all platforms in the generation
8 (``catalog.platform`` is ``None``).
10Algorithm:
111. Normalize all canonical names via ``_norm_key`` (lowercase, strip
12 punctuation) to build a cross-platform match key.
132. For each match key that appears on multiple platforms, keep the version
14 from the highest-priority platform (index 0 in
15 ``manifest.generation_platform_order``).
163. Lower-priority duplicates are removed with a human-readable reason that
17 names both the kept and removed platform.
19If ``manifest.generation_name`` is ``None`` or
20``manifest.generation_platform_order`` has fewer than 2 entries, this pass
21is a no-op.
22"""
24from __future__ import annotations
26import re
27from collections import defaultdict
28from typing import TYPE_CHECKING
30from romfarmer.ir.catalog import Catalog, GameUnit, PassResult, PassTrace, UnitId
32if TYPE_CHECKING:
33 from romfarmer.analysis.knowledge import KnowledgeBase
34 from romfarmer.ir.manifest import BuildManifest
35 from romfarmer.planner.costmodel import CostModel
37PASS_NAME = "generation"
39_NON_ALNUM = re.compile(r"[^a-z0-9 ]+")
42def _norm_key(name: str) -> str:
43 """Lowercase + strip non-alphanumeric (except space) for cross-platform matching."""
44 return _NON_ALNUM.sub("", name.lower()).strip()
47def run(
48 catalog: Catalog,
49 manifest: BuildManifest,
50 kb: KnowledgeBase,
51 cost_model: CostModel,
52) -> PassResult:
53 """Remove cross-platform duplicates based on platform priority."""
54 if manifest.generation_name is None or len(manifest.generation_platform_order) < 2:
55 return PassResult(
56 catalog=catalog,
57 trace=PassTrace(pass_name=PASS_NAME, removed=(), added=()),
58 )
60 priority_order = manifest.generation_platform_order # index 0 = highest
61 priority_map: dict[str, int] = {p: i for i, p in enumerate(priority_order)}
63 # Group units by normalised match key + platform
64 # match_key → {platform → list[GameUnit]}
65 by_key: dict[str, dict[str, list[GameUnit]]] = defaultdict(lambda: defaultdict(list))
66 for unit in catalog.units:
67 plat = str(unit.platform)
68 if plat not in priority_map:
69 # Platform not part of this generation — skip
70 continue
71 key = _norm_key(unit.canonical_name)
72 by_key[key][plat].append(unit)
74 removed: list[tuple[UnitId, str]] = []
76 for _match_key, platforms in by_key.items():
77 if len(platforms) <= 1:
78 continue # No cross-platform duplicate
80 # Find the highest-priority platform present
81 best_plat = min(platforms.keys(), key=lambda p: priority_map.get(p, 9999))
83 for plat, units in platforms.items():
84 if plat == best_plat:
85 continue
86 for unit in units:
87 removed.append(
88 (
89 unit.unit_id,
90 f"generation {manifest.generation_name}: "
91 f"'{unit.canonical_name}' on {plat!r} "
92 f"superseded by {best_plat!r}",
93 )
94 )
96 removed_ids = {uid for uid, _ in removed}
97 new_catalog = catalog.without(removed_ids)
98 return PassResult(
99 catalog=new_catalog,
100 trace=PassTrace(
101 pass_name=PASS_NAME,
102 removed=tuple(removed),
103 added=(),
104 ),
105 )