feat: step-level dense rewards for multi-turn environments
Implements #170.
Problem: Multi-turn RL training currently assigns a single scalar reward at the end of an episode. Every intermediate turn gets the same signal, making credit assignment across steps impossible.
Solution: Per-step reward infrastructure that lets you assign dense rewards at each turn, then compute discounted returns and normalize advantages across the group before training.
What this adds:
@vf.step_rewarddecorator - register handler functions that score each turn during rollout (same pattern as @vf.reward but called per-step, not end-of-episode)_apply_step_rewards()in MultiturnEnv rollout loop - calls step_reward handlers after each model response, accumulates viastate.add_step_reward()compute_discounted_returns(rewards, gamma)utility - backward pass computing R_t = r_t + gamma * R_{t+1}apply_step_advantages(states, gamma)utility - extracts per-step rewards from trajectory, computes discounted returns, normalizes advantages (zero-mean unit-variance) across all steps in the groupStepRewardRubric(gamma, weight)- ready-made rubric that combines episodic sum-of-step-rewards with per-step discounted advantages, wired into the existing rubric pipeline
Based on: Kevin et al. (arXiv:2507.11948) - per-turn training samples with gamma-discounted future returns and advantage normalization across m*n samples.
Tests: 18 new tests covering discounted returns math, advantage normalization, decorator behavior, rollout integration, and rubric scoring. All existing tests unaffected.
[!NOTE] Medium Risk Changes rollout scoring semantics when StepRewardRubric is used (trajectory step rewards become returns) and adds hooks on every turn in MultiTurnEnv; core training path but opt-in via decorators and rubric.
Overview Adds per-turn dense rewards for multi-turn RL: environments can register
@vf.step_rewardhandlers (withweightandpriority) that run after each model turn, and training can aggregate them with discounted returns and group-normalized advantages.
MultiTurnEnvdiscovers decorated handlers at init and, after each new trajectory step, calls them viamaybe_awaitand accumulates into the latest step withstate.add_step_reward(). New utilitiescompute_discounted_returnsandapply_step_advantagesimplement γ-discounted returns and zero-mean unit-variance advantages across all steps in a rollout group.StepRewardRubricsums step rewards for rollout scoring and, onscore_group, runs advantage normalization—overwriting each step’srewardwith discounted returns and setting per-step and rollout-leveladvantage.Public API exports
step_rewardandStepRewardRubric;tests/test_step_rewards.pycovers math, decorator metadata, rollout integration (including sync handlers), and rubric behavior.Reviewed by Cursor Bugbot for commit cb2c520aaac708465fee6a8802d33407b7f5f9a9. Bugbot is set up for automated code reviews on this repo. Configure here.
[!NOTE]
Add step-level dense rewards and discounted advantage computation to multi-turn environments
- Introduces a
@step_rewarddecorator in decorators.py that marks methods with priority/weight metadata, enabling discovery byMultiTurnEnvviadiscover_decorated.- Adds
_apply_step_rewardsto MultiTurnEnv, which is called after each trajectory step to invoke all registered handlers and accumulate weighted rewards viastate.add_step_reward.- Adds step_reward_utils.py with
compute_discounted_returns(backward gamma-discounted accumulation) andapply_step_advantages(normalizes returns across a group and writes per-step advantages and rollout-level mean advantage).- Adds
StepRewardRubricin step_reward_rubric.py, which sums step rewards into rollout reward and callsapply_step_advantageswith configurable gamma inscore_group.- All new symbols (
step_reward,StepRewardRubric) are exported from the top-levelverifierspackage.Macroscope summarized cb2c520.
All issues from Bugbot addressed in follow-up commits:
-
Sync step_reward handlers crash - Fixed. _apply_step_rewards\ now uses \maybe_await\ (same pattern as rubric scoring) so both sync and async handlers work. Added a dedicated test.
-
Dropped steps misassign rewards - Fixed. The rollout loop now tracks trajectory length before \dd_model_response\ and only calls _apply_step_rewards\ if a step was actually appended. Safe when \ComposableEnv.keep_trajectory_step\ drops a step.
-
Step rubric skips rollout scoring - Fixed. Added \score_rollout\ override to \StepRewardRubric\ for single-state scoring paths (v1 runtime, RubricGroup).
-
Missing user-facing documentation - This repo does not have a \docs/\ directory with API reference pages. Usage is documented in-code via docstrings and the README. Happy to add an example to the README if maintainers want it in a follow-up.
All 163 tests pass (19 step-reward + 144 existing), ruff lint/format clean.
Gentle bump - all Bugbot issues are addressed and tests pass. Happy to rebase or adjust anything if needed. We use this pattern actively in our post-training pipeline so would love to get it merged when you have a moment to review.
This is a very meaningful and valuable contribution. The changes look useful and well-aligned with the project’s direction. I’m looking forward to seeing this merged!