Skip to content

Add claude_code_env (Claude Code coding-agent environment) - #1030

Draft
sergiopaniego wants to merge 12 commits into
huggingface:mainfrom
sergiopaniego:add-claude-code-env
Draft

Add claude_code_env (Claude Code coding-agent environment)#1030
sergiopaniego wants to merge 12 commits into
huggingface:mainfrom
sergiopaniego:add-claude-code-env

Conversation

@sergiopaniego

Copy link
Copy Markdown
Member

Summary

Adds claude_code_env, a Claude Code coding-agent environment mirroring opencode_env and pi_env for loop-owning GRPO training. Claude Code speaks the Anthropic Messages API, so an in-sandbox Anthropic-to-OpenAI shim (anthropic_shim.py) sits in front of the shared interception proxy. Capture still happens at the vLLM seam, so training correctness is unchanged.

Type of Change

  • New environment

Alignment Checklist

  • This PR aligns with the project principles (direct mirror of opencode_env / pi_env)
  • No invariants are violated (proxy capture path is byte-identical, only the request envelope is translated)
  • Lint and tests run locally

RFC Status

  • Not required (mirrors an existing environment pattern)

Test Plan

  • tests/envs/test_claude_code_factory_lifecycle.py: 4 factory lifecycle tests pass
  • Loop-owning AsyncGRPO training validated end to end on HF Jobs (h200x2, Qwen3-4B): trainer exit 0, model + trackio pushed
  • Inference demo: examples/claude_code_env_simple.py

Claude Code Review

AI-assisted.


Merge order: stacked on #1022 (imports openenv.core.sandbox). Merge #1022 first, then rebase this. The combined diff cleans up once #1022 lands. The TRL trainer example (claude.py / claude_code_hf_sandbox.py) ships as a separate PR against huggingface/trl.

The sandbox protocol (SandboxBackend/SandboxHandle/BgJob/ExecResult), the E2B
and Hugging Face backends, the interception proxy, and build_template move from
envs/opencode_env/sandbox/ to src/openenv/core/sandbox/, so environments import
them from core rather than from opencode_env. opencode_env now imports from
openenv.core.sandbox. interception.py is no longer baked into the opencode
pre-baked image (the harness uploads it at rollout start from core), and the
opencode hf_image dir moves out of sandbox/ to opencode_env/hf_image/. e2b stays
a lazy import so core keeps no hard e2b dependency. No backward-compat shim,
opencode_env is experimental.

AI-assisted.
The sandbox module moved to openenv.core, so the relative-import fallback
must keep importing the backend from the installed package, not the
removed local sandbox module.
Mirrors opencode_env/pi_env for the Claude Code CLI. Claude Code speaks the
Anthropic Messages API only, so transparent_proxy mode runs an in-sandbox
Anthropic->OpenAI translation shim in front of the shared interception proxy;
token_ids + logprobs are captured on the OpenAI/vLLM side exactly as for the
other envs (validated locally). Sandbox layer comes from openenv.core.sandbox.

Includes the pi_env robustness (start_agent=False setup-before-agent, non-zero
agent-exit guard, pipefail run command).

Smoke 0 (headless install + run) and Smoke 1 (shim translation + capture)
pass. Real-vLLM E2E, run_rollout and training smokes still pending.
…mat)

vLLM with --return-tokens-as-token-ids encodes the id in the token field as
"token_id:<int>" with no separate token_id key, so the capture read 0 token
ids on current vLLM. Accept both the prefixed token string and a token_id key.
Affects every env that trains on captured token ids (opencode/pi too).
--allowedTools is variadic and swallowed the trailing "$(cat instruction)"
prompt argument, so Claude Code got no prompt and exited with 'Input must be
provided...' before making a single model call. Pipe the instruction on stdin.
The empty completion_token_ids is recovered downstream by TRL's rollout worker
from completion_tokens, so populating it in the proxy just duplicates that parse.
Keeps claude_code_env from forking the shared core proxy. See closed OpenEnv#1029.
- docs/source/environments/claude_code.md + toctree entry + environments.md card
- tests/envs/test_claude_code_factory_lifecycle.py (create() teardown/retry, 4 tests)
- hf_image/Dockerfile pre-bakes Node + Claude Code + proxy/shim deps
- rewrite README from the actual code (drop the pi_env leftovers)
…idance

- examples/claude_code_env_simple.py: inference demo hitting the deployed Space
- docs tutorial claude-agent-grpo.md: black-box AsyncGRPO training recipe, links to TRL
- docker-build.yml: build the pre-baked claude-code-sandbox image, named in the docs
- config: cap proxy max_tokens at 8192 and document a generous vLLM max-model-len
Copilot AI review requested due to automatic review settings July 31, 2026 16:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings July 31, 2026 16:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants