"""Top-K Population component — an uncapped keep-all leaderboard sorted by combined_score.

One file per component (see scaffold.py). Faithful port of SkyDiscover's ``TopKDatabase`` store,
which keeps every program ever generated (``# NOTE: no enforcement on population size at all``) and
ranks them by ``combined_score`` on every sample.
"""
from __future__ import annotations

from ...components.population import InMemoryPopulation


class TopKPopulation(InMemoryPopulation):
    """SkyDiscover ``TopKDatabase`` store: keep-all, fitness-sorted, no real population cap.

    :class:`~galapagos.components.population.InMemoryPopulation` already maintains a fitness-sorted
    leaderboard answering ``query({"top": k})``; Top-K simply runs it effectively uncapped (the
    reference enforces no size limit), so the parent (rank 1) and context (ranks 2..K+1) are always
    drawn from the full history.
    """

    def __init__(self, capacity: int | None = None):
        # None = uncapped (SkyDiscover enforces no population cap); drop_invalid mirrors the controller
        # dropping errored children (`if result.error: continue`) so they never pollute the leaderboard.
        super().__init__(capacity=capacity, drop_invalid=True)
