Files
romm/backend/handler/recommendation/feed.py
Georges-Antoine Assi 68709e79a7 fix(siblings): group versions by connected component everywhere (#5430)
* fix(siblings): group versions by connected component everywhere

The game page's siblings matched on any shared provider id, while the
gallery's group_by_meta_id window partitioned on the first non-null id in
a fixed provider order. A version matched by IGDB and ScreenScraper and
one matched by ScreenScraper alone were siblings on the game page but two
gallery cards. The recommendation surfaces didn't group at all: the
cold-start row ranks by rating, which every version shares, so one game
filled it.

rom_sibling_groups now stores each ROM's connected component over
rom_identity_keys, kept in step by add_rom/update_rom/delete_rom and
rebuilt after a scan. The sibling_roms view, the gallery window and the
recommendations all read it: the feed and similar games collapse each
game to one entry, exclude the other versions of a played game, and
show the version the grouped gallery shows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(siblings): answer the Versions filter from rom_sibling_groups

Only a ROM with another version has a group row, so "has versions" is a
primary-key probe rather than an EXISTS over the sibling_roms self-join.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(siblings): break representative ties by id; restore Versions filter

Versions often share a filename stem and region, and without a final key
the version that represents a group could change between queries.

The "Versions" filter goes back to EXISTS over sibling_roms: measured on
a 100k-ROM library it takes the same time as the direct group-row probe
(107 ms each), since the view merges into the query.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(migrations): renumber rom_sibling_groups to 0158 after master's 0157

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(recommendations): address Greptile review on sibling grouping

- Swap each recommendation to its visible representative before the
  visibility cut, so a game whose top-ranked version is hidden shows
  through a visible one instead of disappearing.
- Read candidates in batches until enough distinct games remain, so a
  game with more versions than one overfetch batch no longer crowds out
  the rest of the feed; the cold-start query pages with an id tiebreak.
- Shorten the 0158 migration and RomSiblingGroup docstrings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(recommendations): use the shared auth helpers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: code-review

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: simplify

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: prose

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: prose (code-review fixes)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(recommendations): hide only the hidden version, as the gallery does

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(siblings): regroup only on identity changes and scope the post-scan repair

add_rom skips the regroup when the merge changed no identity column. The
post-scan repair regroups the scanned ROMs for a ROM-scoped scan and
rebuilds only the scanned platforms otherwise, since no group spans two
platforms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 12:07:57 -05:00

426 lines
15 KiB
Python

"""Builds a user's personalised recommendation feed.
Computed on demand from the precomputed similarity edges plus live activity,
rather than precomputed per user: a feed built nightly would ignore the game
someone played an hour ago, which is exactly the signal that matters most.
"""
from __future__ import annotations
import json
import math
from collections.abc import Callable, Iterable, Iterator, Sequence
from dataclasses import dataclass, field
from datetime import datetime, timezone
from itertools import batched
from typing import Any, Final
from handler.database import db_recommendation_handler, db_rom_handler
from handler.database.recommendations_handler import UserAffinityRow
from handler.recommendation.diversity import (
OVERFETCH_FACTOR,
cap_by_series,
collapse_sibling_groups,
)
from handler.recommendation.scoring import Facet
from handler.redis_handler import sync_cache
from logger.logger import log
from models.rom import Rom, RomUserStatus
from utils.datetime import to_utc
# The user rating scale is 1-10; 5.5 is the indifference point, so anything
# below it pushes similar games *away* rather than merely not pulling them in.
NEUTRAL_RATING: Final = 5.5
MAX_RATING: Final = 10.0
# Playtime saturates: the difference between 1h and 10h says a lot, the
# difference between 100h and 200h says almost nothing.
PLAYTIME_SATURATION_HOURS: Final = 20.0
# Taste drifts. A game played last week should steer the feed more than one
# finished two years ago, without the old one falling off entirely.
RECENCY_HALFLIFE_DAYS: Final = 60.0
MIN_RECENCY_FACTOR: Final = 0.15
STATUS_AFFINITY: Final[dict[str, float]] = {
RomUserStatus.COMPLETED_100.value: 1.0,
RomUserStatus.FINISHED.value: 0.8,
RomUserStatus.RETIRED.value: 0.4,
RomUserStatus.INCOMPLETE.value: 0.3,
}
# Games the user has already played still belong in the feed (a sequel to
# something you finished is a fine suggestion), but an unplayed game you own
# is the outcome this feature exists to produce.
PLAYED_NOVELTY_FACTOR: Final = 0.3
# Disliked games steer, but less forcefully than loved ones.
NEGATIVE_SEED_DAMPING: Final = 0.5
# Ceiling applied during diversification, so one platform cannot own the row.
# The per-series ceiling is shared with the similar-games surface.
MAX_PER_PLATFORM: Final = 4
# Seeds are read in descending affinity; beyond this the contribution is noise.
MAX_SEEDS: Final = 60
FEED_CACHE_TTL_SECONDS: Final = 900
FEED_CACHE_PREFIX: Final = "recommendations:feed"
# Bumped when the similarity graph is rebuilt, which invalidates every user.
_GRAPH_VERSION_KEY: Final = f"{FEED_CACHE_PREFIX}:version"
@dataclass
class RecommendedRom:
rom: Rom
score: float
reasons: list[dict[str, str]] = field(default_factory=list)
seed_rom_id: int | None = None
seed_rom_name: str | None = None
@dataclass
class _Candidate:
rom_id: int
score: float = 0.0
best_seed_id: int | None = None
best_seed_contribution: float = 0.0
reasons: list[dict[str, str]] = field(default_factory=list)
def seed_affinity(row: UserAffinityRow, *, now: datetime | None = None) -> float:
"""How strongly one played game should steer the feed, in roughly [-1, 1].
Negative for games the user rated below the midpoint, which is what lets
the feed learn "not this kind of thing".
"""
signals: list[float] = []
if row.playtime_ms > 0:
hours = row.playtime_ms / 3_600_000
signals.append(
min(1.0, math.log1p(hours) / math.log1p(PLAYTIME_SATURATION_HOURS))
)
if row.rating:
# Maps 10 -> +1.0 and 1 -> -1.0, crossing zero at the scale midpoint.
signals.append(
max(
-1.0,
min(1.0, (row.rating - NEUTRAL_RATING) / (MAX_RATING - NEUTRAL_RATING)),
)
)
if row.status and row.status in STATUS_AFFINITY:
signals.append(STATUS_AFFINITY[row.status])
if row.now_playing:
signals.append(1.0)
if not signals:
# Played at some point, but nothing else is known about it.
return (
0.2 * _recency_factor(row.last_played, now=now) if row.last_played else 0.0
)
affinity = sum(signals) / len(signals)
if affinity < 0:
affinity *= NEGATIVE_SEED_DAMPING
return affinity * _recency_factor(row.last_played, now=now)
def _recency_factor(
last_played: datetime | None, *, now: datetime | None = None
) -> float:
"""Exponential decay on time since last played, floored so old loves persist."""
if last_played is None:
return MIN_RECENCY_FACTOR
reference = now or datetime.now(timezone.utc)
days = max(0.0, (reference - to_utc(last_played)).total_seconds() / 86_400)
decayed = math.pow(0.5, days / RECENCY_HALFLIFE_DAYS)
return max(MIN_RECENCY_FACTOR, decayed)
class FeedBuilder:
"""Ranks candidates for one user from the precomputed similarity graph."""
def __init__(self, user_id: int) -> None:
self.user_id = user_id
def build(self, limit: int = 20) -> list[RecommendedRom]:
affinity_rows = db_recommendation_handler.get_user_affinity(self.user_id)
seeds, excluded = self._partition(affinity_rows)
# Playing the USA release rules out the Japanese one too, but hiding a
# version hides only that version, as in the grouped gallery.
hidden = {row.rom_id for row in affinity_rows if row.hidden}
if excluded - hidden:
excluded = hidden | db_rom_handler.get_sibling_group_members(
list(excluded - hidden)
)
if not seeds:
return self._cold_start(limit, excluded)
candidates = self._accumulate(seeds, excluded)
if not candidates:
return self._cold_start(limit, excluded)
played_rom_ids = {
row.rom_id for row in affinity_rows if row.last_played is not None
}
return self._rank(candidates, played_rom_ids, seeds, limit)
# --- Stages ------------------------------------------------------------------
def _partition(
self, rows: Sequence[UserAffinityRow]
) -> tuple[dict[int, float], set[int]]:
"""Split the user's library interactions into seeds and hard exclusions."""
seeds: dict[int, float] = {}
excluded: set[int] = set()
for row in rows:
if row.hidden or row.status == RomUserStatus.NEVER_PLAYING.value:
excluded.add(row.rom_id)
# Never-played-on-purpose is a filtering decision, not a taste
# signal, so it contributes nothing to the seed set.
if row.status == RomUserStatus.NEVER_PLAYING.value:
continue
affinity = seed_affinity(row)
if abs(affinity) > 0.01:
seeds[row.rom_id] = affinity
# The seeds themselves are never their own recommendations.
excluded.update(seeds)
if len(seeds) > MAX_SEEDS:
strongest = sorted(seeds.items(), key=lambda kv: -abs(kv[1]))[:MAX_SEEDS]
seeds = dict(strongest)
return seeds, excluded
def _accumulate(
self, seeds: dict[int, float], excluded: set[int]
) -> dict[int, _Candidate]:
"""Fan out from every seed, summing weighted edge scores per candidate."""
edges = db_recommendation_handler.get_neighbours_for_roms(list(seeds))
candidates: dict[int, _Candidate] = {}
for seed_id, related_id, edge_score, reasons in edges:
if related_id in excluded:
continue
contribution = edge_score * seeds[seed_id]
candidate = candidates.get(related_id)
if candidate is None:
candidate = _Candidate(rom_id=related_id)
candidates[related_id] = candidate
candidate.score += contribution
# Attribute the recommendation to whichever seed pulled hardest, so
# "Because you played X" names the game that actually caused it.
if contribution > candidate.best_seed_contribution:
candidate.best_seed_contribution = contribution
candidate.best_seed_id = seed_id
candidate.reasons = list(reasons)
return {
rom_id: candidate
for rom_id, candidate in candidates.items()
if candidate.score > 0
}
def _rank(
self,
candidates: dict[int, _Candidate],
played_rom_ids: set[int],
seeds: dict[int, float],
limit: int,
) -> list[RecommendedRom]:
for candidate in candidates.values():
if candidate.rom_id in played_rom_ids:
candidate.score *= PLAYED_NOVELTY_FACTOR
depth = limit * OVERFETCH_FACTOR
# Seeds reach different versions of one game, and each would take a slot.
ordered = first_distinct_games(
batched(
sorted(candidates.values(), key=lambda c: (-c.score, c.rom_id)),
depth,
strict=False,
),
lambda candidate: candidate.rom_id,
want=depth,
)
roms = hydrate_roms([candidate.rom_id for candidate in ordered])
seed_names = db_recommendation_handler.get_rom_names(
[
candidate.best_seed_id
for candidate in ordered
if candidate.best_seed_id is not None
]
)
chosen = cap_by_series(
ordered,
lambda candidate: roms.get(candidate.rom_id),
limit=limit,
max_per_platform=MAX_PER_PLATFORM,
)
return [
RecommendedRom(
rom=roms[candidate.rom_id],
score=round(candidate.score, 6),
reasons=candidate.reasons,
seed_rom_id=candidate.best_seed_id,
seed_rom_name=seed_names.get(candidate.best_seed_id or -1),
)
for candidate in chosen
]
def _cold_start(self, limit: int, excluded: set[int]) -> list[RecommendedRom]:
"""No usable activity yet, so fall back to the library's best-reviewed."""
log.debug(f"No recommendation seeds for user {self.user_id}, using fallback")
batch_size = limit * OVERFETCH_FACTOR
exclude_rom_ids = list(excluded)
def pages() -> Iterator[list[int]]:
offset = 0
while page := db_recommendation_handler.get_fallback_rom_ids(
batch_size, exclude_rom_ids=exclude_rom_ids, offset=offset
):
yield page
offset += batch_size
# Every version of a game carries its rating, so they rank side by side.
rom_ids = first_distinct_games(pages(), lambda rom_id: rom_id, want=limit)
roms = hydrate_roms(rom_ids)
return [
RecommendedRom(
rom=roms[rom_id],
score=0.0,
reasons=[{"facet": Facet.TOP_RATED, "value": ""}],
)
for rom_id in rom_ids
if rom_id in roms
]
def first_distinct_games[T](
batches: Iterable[Sequence[T]], rom_id_of: Callable[[T], int], *, want: int
) -> list[T]:
"""The first `want` on-disk items that are each a different game, in order."""
# Lazy, so a game with a dozen versions costs another batch, not a short list.
seen: set[int] = set()
kept: list[T] = []
for batch in batches:
group_keys = db_rom_handler.get_sibling_group_keys(
[rom_id_of(item) for item in batch], present_only=True
)
# A version off disk has no key, leaving the game's slot to one on disk.
on_disk = [item for item in batch if rom_id_of(item) in group_keys]
kept.extend(collapse_sibling_groups(on_disk, rom_id_of, group_keys, seen))
if len(kept) >= want:
return kept[:want]
return kept
def hydrate_roms(
rom_ids: Sequence[int], *, include_missing: bool = False
) -> dict[int, Rom]:
"""Load ROMs through the shared `SimpleRomSchema` load path.
Missing-from-disk ROMs are dropped here rather than in SQL, so the edge
queries never have to join the wide `roms` table.
Args:
rom_ids: The ROMs to load.
include_missing: Keep ROMs missing from disk, for `_visible_representatives`
to swap for a version on disk.
"""
return {
rom.id: rom
for rom in db_rom_handler.get_roms_simple_by_ids(list(rom_ids))
if include_missing or not rom.missing_from_fs
}
def get_cached_feed(user_id: int, limit: int) -> list[RecommendedRom] | None:
"""Read a cached feed, re-hydrating the ROMs so visibility stays live."""
raw = sync_cache.get(_cache_key(user_id, limit))
if not raw:
return None
try:
entries: list[dict[str, Any]] = json.loads(raw)
except ValueError, TypeError:
return None
roms = hydrate_roms([entry["rom_id"] for entry in entries], include_missing=True)
return [
RecommendedRom(
rom=roms[entry["rom_id"]],
score=entry["score"],
reasons=entry.get("reasons") or [],
seed_rom_id=entry.get("seed_rom_id"),
seed_rom_name=entry.get("seed_rom_name"),
)
for entry in entries
if entry["rom_id"] in roms
]
def set_cached_feed(user_id: int, limit: int, feed: Sequence[RecommendedRom]) -> None:
"""Cache only the ranking, never the ROM rows themselves."""
payload = json.dumps(
[
{
"rom_id": item.rom.id,
"score": item.score,
"reasons": item.reasons,
"seed_rom_id": item.seed_rom_id,
"seed_rom_name": item.seed_rom_name,
}
for item in feed
]
)
sync_cache.set(_cache_key(user_id, limit), payload, ex=FEED_CACHE_TTL_SECONDS)
def invalidate_cached_feed(user_id: int) -> None:
sync_cache.incr(_user_version_key(user_id))
def invalidate_all_cached_feeds() -> None:
"""Drop every user's ranking, for when the graph underneath it changes."""
sync_cache.incr(_GRAPH_VERSION_KEY)
def _version(key: str) -> str:
raw = sync_cache.get(key)
if isinstance(raw, bytes):
return raw.decode()
return str(raw) if raw is not None else "0"
def _user_version_key(user_id: int) -> str:
return f"{FEED_CACHE_PREFIX}:version:{user_id}"
def _cache_key(user_id: int, limit: int) -> str:
# Both versions ride in the key so invalidation is a single INCR rather
# than a SCAN over a keyspace shared with the job queues and sessions.
# Superseded entries are never read again and fall out on their own TTL.
return (
f"{FEED_CACHE_PREFIX}:g{_version(_GRAPH_VERSION_KEY)}"
f":{user_id}:u{_version(_user_version_key(user_id))}:{limit}"
)