Skip to content
Closed
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
52902a4
Add harness session runtime for training and evaluation
burtenshaw Mar 28, 2026
774f445
Handle MCP tool errors and lazy-load BrowserGym exports
burtenshaw Mar 30, 2026
3dc09ce
Refine experimental harness APIs and reward forwarding
burtenshaw Mar 30, 2026
fc0188e
Merge branch 'main' into feature/harness-interface
burtenshaw Mar 30, 2026
f67070d
docs: mention environment_factory in harness tutorial
burtenshaw Apr 13, 2026
896c8c0
test: cover harness review regressions
burtenshaw Apr 13, 2026
8637512
chore: merge main
burtenshaw Apr 13, 2026
5414298
feat(harness): rollout collection + openenv collect CLI (#560)
sergiopaniego Apr 18, 2026
032b3d3
fix: address browsergym harness review comments
burtenshaw Apr 29, 2026
03c9261
fix: format collect files
burtenshaw Apr 29, 2026
1448944
envs/opencode_env: OpenCode coding-agent harness primitive (#603)
adithya-s-k May 6, 2026
88240b7
Merge branch 'main' into feature/harness-interface
burtenshaw May 6, 2026
dc451de
fix: address ci docs and formatting
burtenshaw May 6, 2026
d4032a1
feat(tutorials): add SFT warm-up tutorial with reasoning_gym collect …
sergiopaniego May 6, 2026
004168d
Merge branch 'main' into feature/harness-interface
burtenshaw May 7, 2026
b943eb1
Merge branch 'main' into feature/harness-interface
burtenshaw May 7, 2026
da5a107
fix: address harness review feedback
burtenshaw May 7, 2026
99cffbb
fix: format harness lint
burtenshaw May 11, 2026
69a6fd7
fix: address harness review comments
burtenshaw May 11, 2026
627948c
Merge branch 'main' into feature/harness-interface
burtenshaw May 11, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ jobs:
- name: unity-env
dockerfile: envs/unity_env/server/Dockerfile
context: envs/unity_env
- name: opencode-env
dockerfile: envs/opencode_env/server/Dockerfile
context: envs/opencode_env
- name: openapp-env
dockerfile: envs/openapp_env/server/Dockerfile
context: envs/openapp_env
Expand Down
15 changes: 15 additions & 0 deletions docs/source/environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,20 @@ AgentWorldModel-1K — 1,000 synthetic MCP tool-use environments with 10,000 tas
```
````

````{grid-item-card} Opencode
:class-card: sd-border-1

`opencode_env` runs the OpenCode coding agent inside an isolated E2B sandbox against any OpenAI-compatible LLM endpoint, optionally capturing per-token logpr...

+++
```{button-link} environments/opencode.html
:color: primary
:outline:

{octicon}`file;1em` Docs
```
````

`````

```{tip}
Expand Down Expand Up @@ -619,4 +633,5 @@ environments/tbench2
environments/unity
environments/wildfire
environments/agent_world_model
environments/opencode
```
2 changes: 2 additions & 0 deletions docs/source/environments/opencode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
```{include} ../../../envs/opencode_env/README.md
```
129 changes: 129 additions & 0 deletions docs/source/tutorials/browsergym-harness.md

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of the tutorials now have a Open in Colab badge with its corresponding notebook. Could be added here or as a follow-up PR.

Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# BrowserGym Harness Rollouts

This tutorial shows how to drive BrowserGym through the OpenEnv harness runtime
when a trainer needs to keep token sampling, logprobs, and reward assignment
inside the training loop.

```{note}
Use this pattern for tool-driven BrowserGym rollouts. For a standard
`reset()` / `step()` GRPO flow, keep using the Wordle GRPO tutorial.
```

## What You'll Build

- A BrowserGym session factory that creates one environment client per rollout.
- A harness rollout function that TRL can call during training.
- A model-step wrapper that converts generated BrowserGym action text into
structured tool calls.

## Install Dependencies

Install OpenEnv, TRL, and the BrowserGym environment package:

```bash
pip install -U "trl[vllm]" peft trackio kernels
pip install -U git+https://github.com/meta-pytorch/OpenEnv.git
pip install -U "openenv-browsergym @ git+https://huggingface.co/spaces/openenv/browsergym_env"
```

## Build The Session Factory

`BrowserGymSessionFactory` adapts a BrowserGym client into the harness
`ResourceSession` interface. If your training setup already has an
`environment_factory`, pass that factory as `client_factory` so every rollout
gets a fresh environment instance.

```python
from browsergym_env import BrowserGymEnv
from browsergym_env.harness import BrowserGymSessionFactory

space_url = "https://openenv-browsergym-env.hf.space"


def environment_factory():
return BrowserGymEnv(base_url=space_url)


session_factory = BrowserGymSessionFactory(
client_factory=environment_factory,
default_task="click-test",
)
```

The session exposes BrowserGym actions such as `click`, `fill`, `send_keys`,
`scroll`, and `noop` as MCP-style tools while still executing the corresponding
BrowserGym action strings under the hood.

## Wrap TRL Generation

The harness calls a `model_step` function for each turn. The model step should
use the trainer-owned generation path, then return a `ModelStepResult` with the
completion text, token ids, logprobs, and exactly one BrowserGym tool call.

```python
from browsergym_env.harness import build_browsergym_action_tool_call
from openenv.core.harness import ModelStepResult
from openenv.core.llm_client import LLMResponse
from trl.experimental.openenv import generate_rollout_completions


def build_trl_browsergym_model_step(trainer, tokenizer):
def model_step(messages, tools, sampling):
del tools, sampling
prompt_text = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=False,
)
rollout_output = generate_rollout_completions(trainer, [prompt_text])[0]
completion_text = rollout_output.get("text") or tokenizer.decode(
rollout_output["completion_ids"],
skip_special_tokens=True,
)
tool_call = build_browsergym_action_tool_call(completion_text)
return ModelStepResult(
response=LLMResponse(content=completion_text, tool_calls=[tool_call]),
prompt_ids=list(rollout_output["prompt_ids"]),
completion_ids=list(rollout_output["completion_ids"]),
logprobs=list(rollout_output["logprobs"]),
)

return model_step
```

In practice, you should add a small parser around the completion text so common
outputs like `Action: click('13')` are normalized before calling
`build_browsergym_action_tool_call`.

## Create The Rollout Function

Pass the session factory, white-box harness adapter, and model-step builder to
`build_harness_rollout_func`:

```python
from openenv.core.harness import (
HarnessRunLimits,
MCPHarnessAdapter,
build_harness_rollout_func,
)

rollout_func = build_harness_rollout_func(
session_factory=session_factory,
harness_adapter=MCPHarnessAdapter(),
model_step_builder=lambda trainer, session: build_trl_browsergym_model_step(
trainer,
tokenizer,
),
limits=HarnessRunLimits(max_turns=10),
)
```

The returned function accepts TRL prompts and a trainer, runs one harness-backed
BrowserGym episode per prompt, and returns `prompt_ids`, `completion_ids`,
`logprobs`, `env_reward`, and `verify_metrics`.

## Full Example

See [`examples/browsergym_harness.py`](https://github.com/meta-pytorch/OpenEnv/blob/main/examples/browsergym_harness.py)
for a complete TRL-oriented helper that includes action normalization and a
ready-to-use `build_browsergym_rollout_func`.
1 change: 1 addition & 0 deletions docs/source/tutorials/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ end-to-end-walkthrough
mcp-environment
rubrics
wordle-grpo
browsergym-harness
rl-training-2048
evaluation-inspect
```
Loading
Loading