Metadata-Version: 2.5
Name: rhscope
Version: 0.1.0
Summary: Activation probes for reward hacking in LLM RL post-training.
Project-URL: Homepage, https://github.com/082T/rhscope
Project-URL: Documentation, https://082t.github.io/rhscope/
Project-URL: Issues, https://github.com/082T/rhscope/issues
Project-URL: Changelog, https://github.com/082T/rhscope/blob/main/CHANGELOG.md
Author: rhscope contributors
License: Apache-2.0
License-File: LICENSE
Keywords: alignment,grpo,interpretability,llm,post-training,probes,reward-hacking,rlhf
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Requires-Dist: datasets>=2.14
Requires-Dist: huggingface-hub>=0.24
Requires-Dist: numpy>=1.24
Requires-Dist: pydantic>=2.0
Requires-Dist: pyyaml>=6.0
Requires-Dist: safetensors>=0.4
Requires-Dist: torch>=2.3
Requires-Dist: transformers>=4.45
Provides-Extra: bench
Requires-Dist: scikit-learn>=1.3; extra == 'bench'
Provides-Extra: dev
Requires-Dist: ipykernel>=6.29; extra == 'dev'
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: pre-commit>=3.6; extra == 'dev'
Requires-Dist: pytest-cov>=4.1; extra == 'dev'
Requires-Dist: pytest>=7.4; extra == 'dev'
Requires-Dist: ruff>=0.5; extra == 'dev'
Provides-Extra: docs
Requires-Dist: furo>=2024.1; extra == 'docs'
Requires-Dist: myst-parser>=2.0; extra == 'docs'
Requires-Dist: sphinx-copybutton>=0.5; extra == 'docs'
Requires-Dist: sphinx>=7.0; extra == 'docs'
Provides-Extra: tensorboard
Requires-Dist: tensorboard>=2.14; extra == 'tensorboard'
Provides-Extra: trl
Requires-Dist: accelerate>=0.30; extra == 'trl'
Requires-Dist: peft>=0.10; extra == 'trl'
Requires-Dist: trl>=0.11; extra == 'trl'
Provides-Extra: wandb
Requires-Dist: wandb>=0.16; extra == 'wandb'
Description-Content-Type: text/markdown

# rhscope

[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](LICENSE)

**Activation probes for reward hacking in LLM RL post-training.**

rhscope trains linear probes on a base model's hidden states to detect
reward hacking during RL fine-tuning, ships a sandbox-verified
benchmark to evaluate those probes, and includes a probe-subspace
drift detector that distinguishes genuine reform from monitor evasion.

```bash
pip install rhscope
```

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

# 1. Train a probe for your base model (~5 min on a modest GPU).
model, tokenizer = load_model_for_probing("Qwen/Qwen3-1.5B")
result = train_probe(
    model, tokenizer,
    load_hack_examples("hack_examples.jsonl"),
    probe_type="dom",
    category="code_shortcut",
)
result.probe.save("my-probe")

# 2. Use it as a reward-hacking monitor during RL.
monitor = HackMonitor.from_probe(result.probe, model=model, tokenizer=tokenizer)
scores  = monitor.score_batch(prompts, completions)   # higher = more hack-like
```

## Research - evasion vs reform

A policy trained against a probe-based reward penalty can either
genuinely stop hacking (reform) or continue hacking after rotating its
activation signature out of the probe's subspace (evade/obfuscate).
Scalar hack-rate curves cannot tell the two apart.
[`rhscope.research.EvasionDetector`](src/rhscope/research/evasion.py)
anchors a baseline probe direction, fits a fresh direction at each
eval step on held-out data, and classifies the trajectory as
`reforming` / `obfuscating` / `stable` / `undecided`.

## Install

```bash
pip install rhscope                          # core: probes + bench
pip install "rhscope[trl]"                   # + GRPO/PPO reward-penalty integration
pip install "rhscope[dev,bench,trl]"         # development
```

Python 3.10+. Probe training and scoring run on whatever GPU `torch`
can see; VRAM required scales with the base model's hidden state. All
probe types train on CPU too, which is useful for small-model or
heuristic-labelled smoke tests.

## Plug a probe into GRPO training

```python
from trl import GRPOTrainer
from rhscope import HackMonitor
from rhscope.integrations.trl import make_monitor_reward_fn

monitor   = HackMonitor.from_pretrained("my-probe")           # or .from_probe(probe)
reward_fn = make_monitor_reward_fn(monitor, penalty_coef=0.1).fn

trainer = GRPOTrainer(
    model=your_model,
    reward_funcs=[your_env.reward_func(), reward_fn],
    ...
)
trainer.train()
```

TRL sums reward-function outputs, so `reward_fn` returning negative
values penalises hacking without subclassing the trainer or wiring
custom callbacks. Watch `monitor/hack_fraction` in TensorBoard
alongside your loss curve to catch monitor evasion before it corrupts
the run.

The same pattern works with verl via
[`rhscope.integrations.verl.MonitorRewardModel`](src/rhscope/integrations/verl.py).

## Probes we ship

| Probe | Where | When to use |
|---|---|---|
| `DoMProbe` | `rhscope.DoMProbe` | Default. Difference-of-means direction with optional ZCA whitening. Trains in seconds, interpretable as a single activation direction. |
| `LogisticProbe` | `rhscope.LogisticProbe` | L2 logistic regression via torch-native L-BFGS. Stronger separator than DoM on noisier data. |
| `MLPProbe` | `rhscope.MLPProbe` | Shallow two-layer MLP with early stopping. Use when the hacking concept is non-linear in activation space. |
| `OnlineProbe` | `rhscope.OnlineProbe` | DoM probe with `partial_fit` for streaming RL rollouts; snapshots to a frozen `DoMProbe` on demand. |

Choose the probe in `train_probe(..., probe_type="dom" / "logistic" / "mlp")`.

## Filter preference data for DPO / IPO / KTO

```python
from rhscope.integrations.dpo import score_dpo_pairs

scores = score_dpo_pairs(monitor, model, tokenizer, prompts, chosen, rejected)
keep   = scores.filter_mask(drop_preference_flips=True)
clean  = [p for p, k in zip(preferences, keep) if k]
```

`preference_flips_to_hacker` flags pairs where the chosen completion
scores *more* hack-like than the rejected one, i.e. your preference
data is actively rewarding hacking.

## Compose with your own detectors

rhscope ships probes. If you want to ensemble a probe with an external
LLM-as-judge, a custom rule, or another team's probe, wrap your scoring
function with `CallableDetector` and feed it to `CompositeDetector`:

```python
from rhscope.bench import CallableDetector, CompositeDetector

def my_judge(prompts, responses):
    # call your judge here (OpenAI, Anthropic, Ollama, whatever)
    return scores

fused = CompositeDetector(
    [probe_detector, CallableDetector(my_judge, name="judge")],
    reducer="max",
)
monitor = HackMonitor(fused)
```

We do not ship built-in judge or heuristic detectors - that scope
belongs to other libraries. rhscope is the probe side.

## Documentation

Full docs site at **[082t.github.io/rhscope](https://082t.github.io/rhscope/)**. Local sources:

- [Introduction](docs/introduction.md) - what rhscope is, when to use it, mental model
- [Quickstart](docs/quickstart.md) - install to first score in 10 minutes
- [Examples](docs/examples/index.md) - five self-contained runnable scripts
- [API overview](docs/api_overview.md) - complete public surface
- [Benchmark](docs/benchmark.md) + [Probe catalog](docs/probes/catalog.md) - methodology and shipped probes
- [Related work](docs/related_work.md) - how rhscope positions against prior art
- [Limitations](docs/limitations.md) - what rhscope does NOT do in v0.1

## Citation

```bibtex
@software{rhscope,
    title = {rhscope: activation probes for reward hacking in LLM RL post-training},
    year  = {2026},
}
```

## License

Apache 2.0. See [LICENSE](LICENSE).
