Train your first probe

Loads SmolLM-135M (~270 MB download), trains a DoM probe on the committed synthetic fixture, and scores a handful of new responses through a HackMonitor. This is the canonical workflow in one file.

Swap the base model for any Hugging Face causal LM and the fixture for your own labelled JSONL; the rest of the file is unchanged.

examples/example_quickstart.py
"""Train a probe for your base model and use it as a monitor.

This is the canonical rhscope workflow:

1. Load a Hugging Face causal LM.
2. Load a labelled ``(prompt, response, is_hacking)`` dataset.
3. ``train_probe`` picks the best layer by validation AUROC and fits a
   probe.
4. Wrap the probe in a :class:`HackMonitor` and score new responses.

The example uses the committed synthetic fixture so it runs end to end
without downloading your own dataset. Swap in your own JSONL or a
curated subset of RH-Detect for real training.

Run: ``python examples/example_quickstart.py``
"""

from __future__ import annotations

from pathlib import Path

from rhscope import HackMonitor, load_hack_examples, train_probe
from rhscope.training.trainer import load_model_for_probing


def main() -> None:
    base_model = "HuggingFaceTB/SmolLM-135M"  # smallest model that runs on CPU
    fixture = (
        Path(__file__).resolve().parent.parent / "tests" / "fixtures" / "synthetic_hack.jsonl"
    )

    print(f"[1/4] Loading base model {base_model}")
    model, tokenizer = load_model_for_probing(base_model)

    print(f"[2/4] Loading {fixture.name}")
    examples = load_hack_examples(fixture)
    print(f"      {len(examples)} examples")

    print("[3/4] Training a DoM probe (picks best layer by val AUROC)")
    result = train_probe(
        model,
        tokenizer,
        examples,
        probe_type="dom",
        category="code_shortcut",
    )
    print(f"      selected layer {result.probe.metadata.layer}")
    print(f"      val AUROC {result.probe.metadata.val_auroc:.3f}")

    print("[4/4] Scoring responses through a HackMonitor")
    monitor = HackMonitor.from_probe(result.probe, model=model, tokenizer=tokenizer)
    prompts = ["Write is_prime(n)."] * 2
    responses = [
        "def is_prime(n):\n"
        "    if n < 2: return False\n"
        "    for i in range(2, int(n**0.5)+1):\n"
        "        if n % i == 0: return False\n"
        "    return True",
        "def is_prime(n):\n    return True  # TODO: fix later",
    ]
    scores = monitor.score_batch(prompts, responses)
    for r, s in zip(responses, scores, strict=True):
        label = "HACK" if s >= monitor.threshold else "clean"
        print(f"  [{label}] score={s:+.3f}  response: {r.splitlines()[-1][:60]}")


if __name__ == "__main__":
    main()