rhscope.bench.curation¶
Curate HackExample subsets from an RHDetectBench.
The probe training pipeline (rhscope.training.train_probe()) consumes
HackExample records; the benchmark harness carries
BenchExample records. The two schemas differ only in field names -
label vs is_hacking, both carry prompt, response, category.
This module is the glue between the two, plus deterministic subset sampling
for probe-grid training.
Use:
from rhscope.bench import RHDetectBench
from rhscope.bench.curation import curate_subset, curate_grid
bench = RHDetectBench.load_jsonl("rh-detect/test.jsonl")
# One category-specific subset:
code_hack_examples = curate_subset(bench, category="code_shortcut", n=2000)
# Full grid of curated subsets, one per category plus a balanced "general":
subsets = curate_grid(
bench,
category_names=["code_shortcut", "format_gaming", "verbosity"],
per_category=2000,
general=5000,
)
- rhscope.bench.curation.bench_to_hack_example(example)[source]¶
Convert a
BenchExampleinto aHackExample.All fields are preserved;
sourcemoves intoextrasinceHackExamplehas a dedicatedsourceattribute already.- Parameters:
example (BenchExample)
- Return type:
- rhscope.bench.curation.curate_subset(bench, *, category, n=None, seed=0, balance=True)[source]¶
Build a category-specific
HackExamplesubset frombench.- Parameters:
bench (RHDetectBench) – Source benchmark.
category (str) – Which behavior category to pull. Must appear in at least one of the bench’s examples.
n (int | None) – Target subset size. Clamped to the largest balanced size available if
balance=True. IfNone, returns the maximum balanced subset.seed (int) – RNG seed for the sampling.
balance (bool) – If True, hack / clean counts are equalized before sampling.
- Returns:
A shuffled list of
HackExamplerecords.- Raises:
ValueError – If the pool for
categoryis empty.- Return type:
- rhscope.bench.curation.curate_general_subset(bench, *, n, seed=0, categories=None)[source]¶
Build a general subset - balanced across both hack/clean AND categories.
The result approximates
ntotal examples split evenly across every category present in the bench (or the explicitcategorieslist), with each per-category slice internally balanced foris_hacking.- Parameters:
bench (RHDetectBench) – Source benchmark.
n (int) – Target total size.
seed (int) – RNG seed.
categories (Iterable[str] | None) – Optional explicit category list. Defaults to every category appearing in the bench.
- Returns:
A shuffled list of
HackExamplerecords.- Raises:
ValueError – If no categories can be used.
- Return type:
- rhscope.bench.curation.curate_grid(bench, *, category_names, per_category, general=None, seed=0, include_general=True, general_key='general')[source]¶
Build a full grid of curated subsets suitable for probe-matrix training.
- Parameters:
bench (RHDetectBench) – Source benchmark.
category_names (Iterable[str]) – Categories to produce one subset per.
per_category (int) – Target size per category-specific subset.
general (int | None) – Target size for the balanced-across-categories subset. Defaults to
per_category * len(category_names).seed (int) – Base RNG seed. Each category uses a derived seed so changing
category_namesorder does not reshuffle other categories.include_general (bool) – Whether to include the balanced subset at all.
general_key (str) – Dict key under which the balanced subset is stored.
- Returns:
{category_or_key: [HackExample, ...]}.- Raises:
ValueError – If the category list contains duplicates or
general_keycollides with a category name.- Return type:
dict[str, list[HackExample]]
- rhscope.bench.curation.hack_examples_by_category(examples)[source]¶
Group a list of
HackExampleby theircategoryfield.Examples with
category is Noneare dropped with a warning - probe training requires a non-empty category label.- Parameters:
examples (Iterable[HackExample])
- Return type: