Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:416rehman DeepZero TopKSelector

From Leeroopedia
Knowledge Sources
Domains Ranking, Reduce
Last Updated 2026-10-27 00:00 GMT

Overview

Built-in ReduceProcessor that sorts all active samples by an upstream metric and keeps only the top K, acting as a global synchronization barrier for finding-based ranking.

Description

The TopKSelector class is a built-in ReduceProcessor that ranks samples by a metric from an upstream stage and keeps only the top K. It reads the metric via a metric_path string in the format stage_name.data_key (e.g., "semgrep_scanner.finding_count"), extracting it from each sample's history.

Key behaviors:

  • keep_top: 0 means keep all samples, still ordered by the metric. This lets a stage rank work without discarding any of it—a ranked corpus is useful for early termination or display even when nothing is dropped.
  • sort_order: "desc" (default) puts highest-metric samples first; "asc" reverses.
  • The metric is read from entry.history[stage_name].data[data_key] and coerced to float; missing or non-numeric values default to 0.0.
  • Samples not in the returned list are filtered out of the pipeline.

Usage

  • Ranking by finding count: metric_path: "semgrep_scanner.finding_count", keep_top: 10
  • Ranking without dropping: keep_top: 0 (keep all, sorted)
  • Ranking by priority score: metric_path: "discover.priority_score", sort_order: "desc"

Code Reference

Source Location

  • Repository: DeepZero
  • File: src/deepzero/stages/top_k.py

Signature

class TopKSelector(ReduceProcessor):
    description = "sorts active samples by an upstream metric and keeps only the top k"

    @dataclass
    class Config:
        metric_path: str = ""
        keep_top: int = 10
        sort_order: str = "desc"

    def validate(self, ctx: ProcessorContext) -> list[str]: ...

    def process(self, ctx: ProcessorContext, entries: list[ProcessorEntry]) -> list[str]: ...

Import

from deepzero.stages.top_k import TopKSelector

I/O Contract

Inputs

Name Type Required Description
metric_path str Yes (config) "stage_name.data_key" path to the ranking metric
keep_top int No (config) Number to keep (0 = keep all, just order; default 10)
sort_order str No (config) "desc" (default) or "asc"
entries list[ProcessorEntry] Yes (runtime) All active samples

Outputs

Name Type Description
returns list[str] Sample IDs to keep, ordered by metric (worst-first for desc)

Usage Examples

BYOVD: Rank by Semgrep Findings

stages:
  - name: rank_by_findings
    processor: top_k
    config:
      metric_path: "semgrep_scanner.finding_count"
      keep_top: 10
      sort_order: "desc"

Keep All, Just Order

# Rank by priority score but keep every sample
stages:
  - name: rank_by_priority
    processor: top_k
    config:
      metric_path: "discover.priority_score"
      keep_top: 0
      sort_order: "desc"

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment