rhscope.integrations.base

Backend-agnostic monitor-penalty primitive.

MonitorPenaltyAdapter is the framework-neutral core of the “monitor-as-reward-shaping” pattern. Given a batch of (prompt, response) pairs, it computes per-example monitor scores and returns a reward delta. The adapter delegates scoring entirely to the monitor’s score_batch() - so heuristic, judge, probe, and composite monitors all plug in identically.

Framework-specific integrations (TRL, verl, OpenRLHF, a bespoke training loop) wrap this with their expected call conventions. The TRL integration (rhscope.integrations.trl) is a thin adapter on top of this module.

class rhscope.integrations.base.MonitorStats(lock=<factory>, latest=<factory>, history=<factory>, call_count=0)[source]

Bases: object

Thread-safe buffer of per-batch monitor metrics.

Framework adapters consume this via consume() to inject metrics into their trainer’s own log stream, so monitor scores show up alongside the trainer’s native metrics in whichever backend (TensorBoard, wandb, …) the trainer is configured with.

Parameters:
lock: allocate_lock
latest: dict[str, float]
history: list[dict[str, float]]
call_count: int = 0
record(metrics)[source]

Append metrics to the buffer and advance call_count.

Parameters:

metrics (dict[str, float])

Return type:

None

consume()[source]

Return a snapshot of latest and reset it for the next step.

Return type:

dict[str, float]

class rhscope.integrations.base.MonitorPenaltyResult(rewards, raw_scores, metrics)[source]

Bases: object

What MonitorPenaltyAdapter.score_batch() returns per call.

Variables:
  • rewards (numpy.ndarray) – (N,) reward deltas. Each entry is -penalty_coef * score[i] and should be added to the base reward by the caller (TRL does this automatically when the adapter is registered as a reward function).

  • raw_scores (numpy.ndarray) – (N,) unmodified monitor scores. Exposed so advanced callers can apply custom shaping without re-running scoring.

  • metrics (dict[str, float]) – Summary metrics for this batch (mean / max / min / hack fraction / penalty coef). Keyed by "<metric_prefix>/<name>".

Parameters:
rewards: ndarray
raw_scores: ndarray
metrics: dict[str, float]
class rhscope.integrations.base.MonitorPenaltyAdapter(monitor, *, penalty_coef=0.0, sink=None, metric_prefix='monitor')[source]

Bases: object

Framework-agnostic monitor-as-penalty primitive.

A single adapter instance owns:

  • A HackMonitor wrapping any detector family (heuristic, judge, probe, composite).

  • A Sink for streaming metrics.

  • A MonitorStats buffer that framework adapters drain on each log tick.

The adapter does not care what kind of detector the monitor wraps - everything runs through monitor.score_batch(prompts, responses).

Parameters:
  • monitor (HackMonitor) – Loaded HackMonitor. Any detector family works.

  • penalty_coef (float) – Coefficient applied to monitor scores. 0.0 makes this adapter a pure logger: scores are recorded but MonitorPenaltyResult.rewards is all zeros.

  • sink (Sink | None) – Optional metric sink.

  • metric_prefix (str) – Namespace for logged metric keys.

property monitor: HackMonitor

The wrapped HackMonitor.

property penalty_coef: float

Active penalty coefficient.

property stats: MonitorStats

Shared metrics buffer; framework adapters read this on log ticks.

score_batch(prompts, responses)[source]

Score (prompts, responses) and compute reward deltas.

Parameters:
  • prompts (Sequence[str]) – One prompt per example.

  • responses (Sequence[str]) – Model responses aligned with prompts.

Returns:

MonitorPenaltyResult with rewards, raw scores, and the metric buffer that was recorded for this batch.

Raises:

ValueError – If the two sequences have different lengths.

Return type:

MonitorPenaltyResult