Awesome Reviewers

When implementing sorting/ranking or token-based relevance in retrieval pipelines, treat the algorithm as a system with strict contracts:

1) Keep ranking features consistent across stages

2) Guard token matching against over-grounding

3) Make heuristic gates parameter-correct and ordered

4) Lock the behavior with regression tests

Example: use a single source of truth for slot sort keys

def slot_key(post: dict, prepared_query, relevance_fn, display_rank_fn) -> tuple:
    # Ensure relevance used here is computed from the same fields
    # that display_rank_fn uses.
    text = f"{post.get('title') or ''} {post.get('selftext') or ''}"  # same as display path
    rel = relevance_fn(prepared_query, text)

    # Tie-break structure must match display ordering.
    engagement = post.get("engagement") or {}
    has_comments = 1 if (engagement.get("num_comments") or 0) > 0 else 0
    rounded_rel = round(rel, 1)

    return (
        has_comments,
        rounded_rel,
        engagement.get("score") or 0,
        # optionally keep stable incoming order if equal
    )

Apply the same principle to entity grounding: only relax matching rules when you can prove they don’t introduce substring/general-token false positives.