-
Notifications
You must be signed in to change notification settings - Fork 353
openenv/tbench2: Daytona sandbox mode — one cloud sandbox per episode, as its own agent function #1675
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
Merged
Merged
openenv/tbench2: Daytona sandbox mode — one cloud sandbox per episode, as its own agent function #1675
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
adf96c4
openenv/tbench2: per-task sandbox image recipe + Daytona materialization
nblintao fafb03a
openenv/tbench2: split the provider-agnostic recipe from the Daytona …
nblintao 669ebf5
openenv: drop the patched-checkout framing from the recipe docs
nblintao 827c465
openenv/tbench2: deterministic embed tar + provider-first module names
nblintao 6f2d0c2
openenv/tbench2: per-task Daytona cloud-sandbox execution backend
nblintao af451d9
openenv: preflight the daytona SDK import at launch
nblintao aeb35a7
openenv: call the now-public make_daytona from task_snapshots
nblintao d0b335d
openenv: reap sandboxes orphaned by cancellation mid-create
nblintao af1d596
openenv: preflight the task_snapshots recipe symbols too
nblintao d996682
openenv: adopt the in-repo tb2_task_sandbox recipe
nblintao 67204ec
openenv: scan_golden stages the oracle solution from the local checkout
nblintao c245287
openenv: point docs at the split recipe/sandbox modules
nblintao 451f0bf
openenv: point the tbench2_env install at upstream main
nblintao 16c1cfa
openenv: follow the recipe PR's module renames
nblintao 0835ebe
openenv/tbench2: keep the Daytona key out of ray's logged channels
nblintao c26e474
requirements: declare openai, an already-load-bearing dependency
nblintao 3525bbc
openenv/tbench2: split the Daytona sandbox mode into its own agent fu…
nblintao 03003cc
openenv/tbench2: review fixes — hoist two imports; evaluate errors dr…
nblintao 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| """Standalone Terminal-Bench-2 eval: an OpenAI-compatible *API* as the policy, | ||
| Daytona sandboxes as the env. No GPU, no miles training pipeline. | ||
|
|
||
| This reuses the exact agent-env loop miles runs during training | ||
| (``openenv_agent_function._multi_turn``: reset -> {policy emits a shell command | ||
| -> exec -> feed output back} -> canonical tests/test.sh -> binary reward) but | ||
| swaps miles' session-server policy for a plain API client. The machine running | ||
| this only orchestrates; the policy runs in the cloud (e.g. DeepSeek) and each | ||
| episode runs in its own Daytona sandbox (the task's OFFICIAL image + | ||
| env server layer — recipe in the sibling ``tb2_sandbox_recipe`` module, | ||
| materialized by ``tb2_sandbox_daytona``), created | ||
| before the episode and deleted after. | ||
|
|
||
| Why a separate script and not ``run-openenv-tbench2.py``: those launchers always | ||
| bring up Megatron+sglang via Ray (the policy must be miles' own engine so the | ||
| session server can record on-policy tokens for the backward pass). Pure eval | ||
| needs none of that. The ``run()`` entry also hardcodes ``api_key="EMPTY"`` | ||
| (self-hosted engines don't check it), so we call each module's ``run_episode`` | ||
| with our own authenticated client instead. | ||
|
|
||
| Env vars: | ||
| DEEPSEEK_API_KEY / POLICY_API_KEY policy API key (required) | ||
| POLICY_BASE_URL OpenAI-compatible root (default https://api.deepseek.com) | ||
| POLICY_MODEL default deepseek-v4-flash | ||
| OPENENV_TB2_TASKS_DIR Daytona sandbox mode (TB2 checkout path); with | ||
| DAYTONA_API_KEY or a key file at ~/.config/daytona/api_key. | ||
| Otherwise OPENENV_ENV_URL is used. | ||
| OPENENV_MAX_TURNS, OPENENV_MAX_ROLLOUT_TIME_SECONDS, ... as in the adapter. | ||
|
|
||
| Usage: | ||
| # DAYTONA_API_KEY may be omitted if ~/.config/daytona/api_key is provisioned | ||
| DEEPSEEK_API_KEY=sk-... DAYTONA_API_KEY=dtn_... \ | ||
| OPENENV_TB2_TASKS_DIR=/path/to/terminal-bench-2 \ | ||
| python eval_tbench2_via_api.py --tasks chess-best-move --concurrency 2 | ||
| # or from make_tbench2_data.py output: | ||
| python eval_tbench2_via_api.py --data /root/tbench2_train.jsonl | ||
| """ | ||
|
|
||
| import argparse | ||
| import asyncio | ||
| import json | ||
| import os | ||
| import sys | ||
|
|
||
| from openai import AsyncOpenAI | ||
|
nblintao marked this conversation as resolved.
|
||
|
|
||
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | ||
| import openenv_agent_function as oaf # noqa: E402 | ||
|
|
||
|
|
||
| def _load_rows(args: argparse.Namespace) -> list[dict]: | ||
| """--tasks builds rows with the adapter's own agent-contract prompt, so it | ||
| reproduces exactly what make_tbench2_data.py emits for training.""" | ||
| if args.tasks: | ||
| ids = [t.strip() for t in args.tasks.split(",") if t.strip()] | ||
| return [ | ||
| {"prompt": [{"role": "system", "content": oaf.TB2_AGENT_SYSTEM_PROMPT}], "metadata": {"task_id": t}} | ||
| for t in ids | ||
| ] | ||
| with open(args.data) as f: | ||
| return [json.loads(line) for line in f if line.strip()] | ||
|
|
||
|
|
||
| async def _eval_one( | ||
| run_episode, policy: AsyncOpenAI, model: str, row: dict, request_kwargs: dict | ||
| ) -> tuple[str, float | None, dict]: | ||
| task_id = row.get("metadata", {}).get("task_id", "?") | ||
| cap = float(os.getenv("OPENENV_MAX_ROLLOUT_TIME_SECONDS", "3600")) | ||
| try: | ||
| # Per-episode wall-clock cap: run_episode deliberately leaves timeout | ||
| # and failure semantics to the caller (run() maps a timeout to reward 0 | ||
| # for training; a sweep must count it as errored instead). Bounds a | ||
| # task that loops on slow generations so one straggler can't stall the | ||
| # whole sweep. | ||
| reward, metrics = await asyncio.wait_for( | ||
| run_episode( | ||
| policy, | ||
| model, | ||
| row.get("prompt", []), | ||
| request_kwargs, | ||
| row.get("metadata", {}), | ||
| ), | ||
| timeout=cap, | ||
| ) | ||
| return task_id, reward, metrics | ||
| except asyncio.TimeoutError: | ||
| return task_id, None, {"error": f"timeout>{cap:.0f}s"} | ||
| except Exception as e: # noqa: BLE001 - one task failing must not sink the sweep | ||
| return task_id, None, {"error": f"{type(e).__name__}: {e}"} | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| ap = argparse.ArgumentParser() | ||
| src = ap.add_mutually_exclusive_group(required=True) | ||
| src.add_argument("--data", help="jsonl from make_tbench2_data.py (one row per task)") | ||
| src.add_argument("--tasks", help="comma-separated task_ids (system prompt inlined)") | ||
| ap.add_argument("--concurrency", type=int, default=4) | ||
| ap.add_argument("--temperature", type=float, default=0.8) | ||
| ap.add_argument("--out", default="", help="optional path to write per-task jsonl results") | ||
| args = ap.parse_args() | ||
|
|
||
| api_key = os.getenv("DEEPSEEK_API_KEY") or os.getenv("POLICY_API_KEY") | ||
| if not api_key: | ||
| sys.exit("set DEEPSEEK_API_KEY (or POLICY_API_KEY)") | ||
| base_url = os.getenv("POLICY_BASE_URL", "https://api.deepseek.com") | ||
| model = os.getenv("POLICY_MODEL", "deepseek-v4-flash") | ||
| rows = _load_rows(args) | ||
| tasks_dir = os.getenv("OPENENV_TB2_TASKS_DIR", "").strip() | ||
| if tasks_dir: | ||
| import openenv_daytona_agent_function as odaf | ||
|
|
||
| run_episode = odaf.run_episode | ||
| env_desc = f"daytona sandboxes (tasks_dir={tasks_dir})" | ||
| else: | ||
| run_episode = oaf.run_episode | ||
| env_desc = os.getenv("OPENENV_ENV_URL", oaf._DEFAULT_ENV_URL) | ||
| print(f"policy={model} @ {base_url} | env={env_desc} | " f"{len(rows)} tasks | concurrency={args.concurrency}") | ||
|
|
||
| policy = AsyncOpenAI(base_url=base_url, api_key=api_key) | ||
| request_kwargs = {"temperature": args.temperature} | ||
| sem = asyncio.Semaphore(args.concurrency) | ||
|
|
||
| async def _run(row: dict) -> tuple[str, float | None, dict]: | ||
| async with sem: | ||
| tid, reward, metrics = await _eval_one(run_episode, policy, model, row, request_kwargs) | ||
| tag = "ERR" if reward is None else f"{reward:.0f}" | ||
| extra = metrics.get("error") or f"turns={metrics.get('turns')}" | ||
| print(f" [{tag}] {tid:40s} {extra}", flush=True) | ||
| return tid, reward, metrics | ||
|
|
||
| results = await asyncio.gather(*(_run(r) for r in rows)) | ||
|
|
||
| scored = [(t, r) for t, r, _ in results if r is not None] | ||
| solved = sum(1 for _, r in scored if r >= 1.0) | ||
| errs = sum(1 for _, r, _ in results if r is None) | ||
| print(f"\n=== pass {solved}/{len(scored)} scored ({errs} errored) ===") | ||
| if scored: | ||
| print("solved:", sorted(t for t, r in scored if r >= 1.0)) | ||
| if args.out: | ||
| with open(args.out, "w") as f: | ||
| for t, r, m in results: | ||
| f.write(json.dumps({"task_id": t, "reward": r, "metrics": m}) + "\n") | ||
| print(f"wrote {args.out}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) | ||
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
Oops, something went wrong.
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.
I'll have another round of polish after some more factorings in a separate PR