-
Notifications
You must be signed in to change notification settings - Fork 417
docs(tutorials): add black-box coding agent training tutorial (OpenCode + AsyncGRPO) #1028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sergiopaniego
wants to merge
2
commits into
main
Choose a base branch
from
feature/docs-opencode-harness-recipe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # Coding Agent Training with TRL (OpenCode) | ||
|
|
||
| This tutorial covers the black-box training path: training the actual | ||
| [`opencode`](https://opencode.ai) coding agent, with its own planner, tools, | ||
| context management, and stop condition, using TRL's experimental | ||
| `AsyncGRPOTrainer`. The agent owns its loop, and OpenEnv captures what it did. | ||
|
|
||
| > [!NOTE] | ||
| > Three GRPO patterns, three tutorials. For a standard `reset()` / `step()` | ||
| > flow where TRL drives the episode, see the | ||
| > [Wordle GRPO tutorial](wordle-grpo.md). For harness rollouts where the | ||
| > trainer still generates each turn (white-box), see the | ||
| > [BrowserGym harness tutorial](browsergym-harness.md). Use this page when you | ||
| > want to train a production agent as-is, without reimplementing its loop. | ||
|
|
||
| ## How It Works | ||
|
|
||
| The full recipe lives in TRL. The moving pieces: | ||
|
|
||
| 1. Each rollout runs the agent inside an OpenEnv session created by | ||
| `OpenCodeSessionFactory` from | ||
| [`opencode_env`](https://github.com/huggingface/OpenEnv/tree/main/envs/opencode_env), | ||
| in `transparent_proxy` mode. A small proxy inside the sandbox forwards the | ||
| agent's `/v1/chat/completions` calls to your vLLM server and records each | ||
| turn's token ids and logprobs to a trace. | ||
| 2. When the agent stops, TRL's `HarnessRolloutWorker` reads the trace, rebuilds | ||
| the per-turn training rows from the recorded ids, and scores the final | ||
| workspace with the session's `verify()` method (a held-out verifier the | ||
| agent never sees). | ||
| 3. `AsyncGRPOTrainer` trains on those rows, propagating the rollout reward to | ||
| every trained token through the group-relative advantage. NCCL weight sync | ||
| keeps the vLLM server on the current policy, so the agent always samples | ||
| from the model being trained. | ||
|
|
||
| Each rollout gets its own isolated session: one sandbox, one proxy port, one | ||
| agent process. Three small functions adapt the recipe to your task: | ||
| `rollout_reward_fn` (outcome to scalar reward), `train_turn_fn` (which turns | ||
| receive gradient), and `agent_turn_fn` (which trace entries are real agent | ||
| turns rather than auxiliary calls like title generation). All three are | ||
| documented in | ||
| [TRL's harness training guide](https://huggingface.co/docs/trl/openenv#training-on-harnesses-training-a-real-coding-agent-opencode). | ||
|
|
||
| ## Full Recipe | ||
|
|
||
| The reference script trains on competitive-coding problems from | ||
| `agentica-org/DeepCoder-Preview-Dataset`: the agent writes `solution.py`, and | ||
| the verifier runs it against held-out tests, returning the fraction passed. It | ||
| is self-contained, runs the agent in a local subprocess sandbox (no container | ||
| setup needed), needs two GPUs (one serving the policy with vLLM, one | ||
| training), and has been validated end to end on Qwen3 (see | ||
| [huggingface/trl#6420](https://github.com/huggingface/trl/pull/6420)). | ||
| Installation, the exact vLLM serving flags, and the run commands live next to | ||
| the recipe in TRL: | ||
|
|
||
| - [Training on harnesses](https://huggingface.co/docs/trl/openenv#training-on-harnesses-training-a-real-coding-agent-opencode) | ||
| in TRL's OpenEnv docs: rollout semantics, the reward path, turn selection, | ||
| and the trace contract. | ||
| - [`examples/scripts/openenv/opencode.py`](https://github.com/huggingface/trl/blob/main/examples/scripts/openenv/opencode.py) | ||
| in TRL: the complete, runnable script. | ||
| - [`envs/opencode_env`](https://github.com/huggingface/OpenEnv/tree/main/envs/opencode_env): | ||
| the OpenEnv side, including the session factory, sandbox backends, and the | ||
| transparent interception proxy. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ALIGNMENT FLAG (Tier 2, non-blocking) — RFC 005: Agentic Harness Integration (In Review)
This "agent owns its loop" black-box model diverges from RFC 005's proposed abstraction, where harness training goes through a
HarnessEnvironmentwith per-turnstep()+HarnessAdapter+Rubric. RFC 005 explicitly rejected the "onestep()= whole episode" model that this path effectively uses.The divergence already exists in the shipped
opencode_env+ TRL code — this tutorial just makes it canonical in the docs. Worth reconciling: probably update RFC 005 to reflect the shippedtransparent_proxy/HarnessRolloutWorker/ session-verify()design (or document both paths). cc @Darktex (RFC 005 author).