Datasets:
File size: 28,852 Bytes
37d21bb dcf6ff7 37d21bb 64900a8 37d21bb dcf6ff7 37d21bb dcf6ff7 37d21bb dcf6ff7 37d21bb dcf6ff7 37d21bb dcf6ff7 37d21bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | """One isolated agent session: a CLI and model rebuilding one benchmark item (a room, an object or a demonstration's
scene) in Docker, over one or more budgets, under the item's contract (generation.contracts).
Budgets are cumulative. `900 1800` tells the agent 15 minutes, keeps what it made at the stop as the 15-minute result,
then resumes the same conversation with the rest of the 30 minutes. Each budget is its own run folder,
data/runs/<track>/<engine>/<item>/<UTC time>/. Only the public input, the contract's tools, the output, scratch and
temporary credentials enter the container; its only network exit is the egress proxy.
simready generate --track scenes --engine mujoco --id 41159420 --cli codex --model gpt-6.1-sol --budget 900 1800
simready generate --track objects --engine mujoco --id input_2d_001 --cli claude --claude-config-dir ~/.claude-huan --model claude-opus-5-5
simready generate --track demos --engine isaac --id ID --cli opencode --model opencode-go/kimi-k3.1-pro --gpu GPU-...
"""
from datetime import datetime, timedelta, timezone
import fcntl
import json
import os
from pathlib import Path
import re
import shlex
import shutil
import subprocess
import tempfile
import time
import imageio_ffmpeg
from ..common.io import DATA, REPO_ROOT
from . import contracts
from .contracts import EGRESS_HOSTS, IMAGES
from .cost import combine_costs, events, keep_codex_usage, keep_opencode_usage, run_cost
from .stage import contract_for, frozen_protocol, item_input, stage
from .opencode_auth import OpenCodeLogin
from .quota import opencode_limit
MUJOCO_IMAGE, ISAAC_IMAGE = IMAGES["mujoco"], IMAGES["isaac"]
RUNS, RUNTIME = DATA / "runs", DATA / "runtime"
ISAAC_USER = "1234:1234"
# Every tool but fetching web pages (downloads are banned; web search is allowed and runs on the model provider's side).
# The egress proxy enforces the ban for everything else the agent runs.
CLAUDE_AGENT = ["/usr/local/bin/claude", "-p", "--verbose", "--include-partial-messages", "--output-format", "stream-json",
"--safe-mode", "--setting-sources", "", "--strict-mcp-config", "--mcp-config", '{"mcpServers":{}}',
"--disable-slash-commands", "--dangerously-skip-permissions", "--disallowedTools", "WebFetch"]
CODEX_FLAGS = ["--json", "--ignore-user-config", "--ignore-rules", "--skip-git-repo-check", "--dangerously-bypass-approvals-and-sandbox",
"-c", 'web_search="live"', "--disable", "browser_use", "--disable", "browser_use_external", "--disable", "in_app_browser"]
# OpenCode (installed in both images) with every tool but webfetch, which its permission config denies (the tool then is
# not offered at all); its websearch stays. --auto approves everything not denied, --pure loads no external plugins. The
# session, its login and its config live in the state folder (XDG_* below); nothing from the host's OpenCode, Claude or
# project config is read, nothing is shared, nothing updates and the model list is the built-in one.
OPENCODE = "/opt/opencode/bin/opencode"
OPENCODE_FLAGS = ["run", "--pure", "--auto", "--format", "json"]
OPENCODE_CONFIG = {"$schema": "https://opencode.ai/config.json", "permission": {"webfetch": "deny"}, "share": "disabled", "autoupdate": False}
OPENCODE_ENV = {"XDG_DATA_HOME": "/auth", "XDG_CONFIG_HOME": "/auth/config", "XDG_CACHE_HOME": "/auth/cache", "XDG_STATE_HOME": "/auth/state",
"OPENCODE_CONFIG_CONTENT": json.dumps(OPENCODE_CONFIG, separators=(",", ":")),
**{f"OPENCODE_DISABLE_{name}": "1" for name in ("AUTOUPDATE", "MODELS_FETCH", "DEFAULT_PLUGINS", "LSP_DOWNLOAD", "PROJECT_CONFIG",
"CLAUDE_CODE", "EXTERNAL_SKILLS")}}
OPENCODE_DATA = "opencode" # XDG_DATA_HOME/opencode: auth.json and opencode.db (the sessions), inside the state folder
EGRESS_NETWORK, EGRESS_PROXY = "simready-egress", "simready-egress-proxy"
EGRESS_URL = f"http://{EGRESS_PROXY}:3128"
EGRESS_ENV = {"HTTPS_PROXY": EGRESS_URL, "HTTP_PROXY": EGRESS_URL, "https_proxy": EGRESS_URL, "http_proxy": EGRESS_URL,
"NO_PROXY": "localhost,127.0.0.1", "no_proxy": "localhost,127.0.0.1", "DISABLE_TELEMETRY": "1", "DISABLE_ERROR_REPORTING": "1"}
LOGIN_SYNC_SECONDS = 20
STOP_SECONDS = 300 # wait for a killed container to exit before giving up on its docker client
STALL_SECONDS = 300 # no model output this long after the start: the CLI is retrying a refusal (a spent usage limit) silently
MODEL_OUTPUT = {"claude": {"assistant"}, "codex": {"item.started", "item.completed", "turn.completed"}, "opencode": {"step_finish", "text", "tool_use"}}
def ensure_egress(network=EGRESS_NETWORK, proxy=EGRESS_PROXY, hosts=EGRESS_HOSTS):
"""The internal network and its proxy, created once and shared by every session (a lock keeps parallel sessions apart)."""
def docker(*arguments):
return subprocess.run(["docker", *arguments], capture_output=True, text=True)
with (Path(tempfile.gettempdir()) / "simready-egress.lock").open("a") as lock:
fcntl.flock(lock, fcntl.LOCK_EX)
if docker("network", "inspect", network).returncode:
docker("network", "create", "--internal", network)
state = docker("inspect", "-f", "{{.State.Running}} {{index .Config.Labels \"simready.egress\"}}", proxy).stdout.split()
if state != ["true", ",".join(hosts)]:
docker("rm", "-f", proxy)
started = docker("run", "-d", "--name", proxy, "--restart", "unless-stopped", "--label", "simready.egress=" + ",".join(hosts),
"--read-only", "--cap-drop", "ALL", "--user", "65534:65534", "--network", "bridge",
"--mount", f"type=bind,src={contracts.RUNTIME / 'egress_proxy.py'},dst=/proxy.py,readonly",
MUJOCO_IMAGE, "python", "/proxy.py", "--allow", ",".join(hosts))
if started.returncode:
raise RuntimeError(f"egress proxy did not start: {started.stderr.strip()}")
docker("network", "connect", network, proxy)
return EGRESS_URL
def save_record(path, record):
temporary = path.with_suffix(".tmp")
temporary.write_text(json.dumps(record, indent=2) + "\n")
temporary.replace(path)
# ---------------------------------------------------------------- credentials: copied in, rotations saved back
def persist_rotated(original, fresh, initial, name, valid):
"""Keep a CLI's token rotation without overwriting a login that changed meanwhile."""
data = (fresh / name).read_bytes()
if data == initial:
return "unchanged"
if not valid(json.loads(data)):
return "invalid_refresh_not_persisted"
with (original / ".benchmark-auth.lock").open("a") as lock:
fcntl.flock(lock, fcntl.LOCK_EX)
backups = original / "credential-backups"
backups.mkdir(mode=0o700, exist_ok=True)
descriptor = os.open(backups / (datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%S%fZ") + ".json"), os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
with os.fdopen(descriptor, "wb") as stream:
stream.write(data)
canonical = original / name
if canonical.read_bytes() != initial:
return "backup_saved_concurrent_login_preserved"
with tempfile.NamedTemporaryFile("wb", dir=original, delete=False) as stream:
stream.write(data)
Path(stream.name).replace(canonical)
return "refreshed_credentials_persisted"
def persist_claude(original, fresh, initial):
def valid(content):
credential = content.get("claudeAiOauth", {})
return all(credential.get(k) for k in ["accessToken", "refreshToken", "expiresAt"])
return persist_rotated(original, fresh, initial, ".credentials.json", valid)
def persist_codex(original, fresh, initial):
return persist_rotated(original, fresh, initial, "auth.json",
lambda content: content.get("tokens", {}).get("access_token") or content.get("OPENAI_API_KEY"))
def persist_opencode(original, fresh, initial):
"""OpenCode's auth.json: one entry per provider, an API key ("key") or an OAuth pair ("access", "refresh")."""
return persist_rotated(original, fresh, initial, "auth.json",
lambda content: bool(content) and all(isinstance(v, dict) and (v.get("key") or v.get("access") and v.get("refresh"))
for v in content.values()))
# ---------------------------------------------------------------- what the CLI logs
def session_identity(cli, log):
"""The CLI's own session id, which a later budget resumes."""
for event in events(log, 30):
if cli == "opencode" and event.get("sessionID"):
return event["sessionID"]
if cli == "codex" and event.get("type") == "thread.started":
return event.get("thread_id")
if cli == "claude" and event.get("subtype") == "init":
return event.get("session_id")
return None
def model_answered(cli, log):
"""Has the model produced anything yet? A CLI's start-up events (Claude's init, Codex's thread) do not count."""
return any(event.get("type") in MODEL_OUTPUT[cli] for event in events(log))
def throttling(log):
"""Throttling shrinks an agent's real working time, so it is reported with the result."""
retries = limited = 0
for event in events(log):
retries += event.get("subtype") == "api_retry"
if event.get("type") == "rate_limit_event":
limited += (event.get("rate_limit_info") or {}).get("status") not in {"allowed", "allowed_warning"}
elif event.get("type") == "error":
error = event.get("error") or {}
if isinstance(error, dict):
data = error.get("data") or {}
limited += isinstance(data, dict) and data.get("statusCode") == 429
return {"api_retries": retries, "rate_limit_events": limited}
def new_run(root, track, engine, item):
"""A run folder named by the time, taking the next second if a parallel session has this one."""
now = datetime.now(timezone.utc)
while True:
rid = now.strftime("%Y%m%dT%H%M%SZ")
run = Path(root) / track / engine / item / rid
try:
run.mkdir(parents=True)
return rid, run
except FileExistsError:
now += timedelta(seconds=1)
def shown(path):
return path.relative_to(REPO_ROOT) if path.is_relative_to(REPO_ROOT) else path
def change_owner(directory, owner):
subprocess.run(["docker", "run", "--rm", "--user", "0:0", "--entrypoint", "bash", "--mount", f"type=bind,src={directory},dst=/d",
ISAAC_IMAGE, "-lc", "chown -R " + owner + " /d"], check=True, stdout=subprocess.DEVNULL)
def login_folder(cli, claude_config_dir=None):
if cli == "opencode":
return Path.home() / ".local/share/opencode"
if cli == "codex":
return Path.home() / ".codex"
return Path(claude_config_dir).expanduser() if claude_config_dir else Path.home() / ".claude"
def isaac_preflight(cli):
"""Isaac Sim's shader cache volume per CLI family, warmed once by a GPU render (docs/generation.md)."""
return RUNTIME / "isaac" / f"smoke_{cli}" / "smoke.json"
class Session:
def __init__(self, track, engine, item, cli, model, effort=None, gpu=None, claude_config_dir=None, campaign="", runs_root=None):
if cli not in contracts.CLIS:
raise SystemExit(f"Unknown CLI {cli}; one of {', '.join(contracts.CLIS)}.")
# OpenCode runs a model at its default reasoning unless a variant is named.
self.effort = effort or ("default" if cli == "opencode" else "medium")
self.track, self.engine, self.item, self.cli, self.model, self.gpu = track, engine, item, cli, model, gpu
self.campaign, self.runs_root = campaign, Path(runs_root).resolve() if runs_root else RUNS
self.isaac = engine == "isaac"
self.login = login_folder(cli, claude_config_dir)
self.claude_config_dir = self.login if cli == "claude" else None
self.auth_name = "auth.json" if cli in ("codex", "opencode") else ".credentials.json"
self.session_id = None
item_input(track, item)
self.contract = contract_for(track, engine, item)
if contracts.missing_files(self.contract):
raise SystemExit(f"{self.contract} stages {', '.join(contracts.missing_files(self.contract))}, not present yet.")
if cli == "opencode":
self.opencode_login = OpenCodeLogin(self.login, model.split("/", 1)[0])
try:
self.read_login()
except (OSError, RuntimeError) as error:
raise SystemExit(f"OpenCode login is unavailable: {error}") from None
elif not (self.login / self.auth_name).is_file():
raise SystemExit(f"The {cli} login is unavailable ({self.login / self.auth_name}).")
if self.isaac and not gpu:
raise SystemExit("Isaac generation needs a GPU (--gpu UUID).")
if self.isaac and not (isaac_preflight(cli).is_file() and json.loads(isaac_preflight(cli).read_text()).get("success")):
raise SystemExit(f"Complete the Isaac GPU/render preflight first ({shown(isaac_preflight(cli))}; docs/generation.md).")
@property
def held(self):
"""Where the login sits in the state folder (and under /auth in the container)."""
return f"{OPENCODE_DATA}/{self.auth_name}" if self.cli == "opencode" else self.auth_name
def persist(self, original, state, initial):
"""Save a refreshed login from the state folder back to the CLI's own login."""
if self.cli == "opencode":
login = getattr(self, "opencode_login", None)
if login is not None and login.v2:
return login.persist(state / self.held, initial)
return persist_opencode(original, state / OPENCODE_DATA, initial)
return (persist_codex if self.cli == "codex" else persist_claude)(original, state, initial)
def read_login(self):
login = getattr(self, "opencode_login", None)
return login.read() if login is not None else (self.login / self.auth_name).read_bytes()
def agent(self):
if self.cli == "opencode":
variant = ["--variant", self.effort] if self.effort != "default" else []
return [OPENCODE, *OPENCODE_FLAGS, "-m", self.model, *variant, *(["-s", self.session_id] if self.session_id else [])]
if self.cli == "codex":
flags = ["-m", self.model, "-c", "model_reasoning_effort=" + json.dumps(self.effort)] + CODEX_FLAGS
start = ["exec", "resume", *flags, self.session_id, "-"] if self.session_id else ["exec", *flags, "-C", "/work", "-"]
return ["/opt/codex/bin/codex", "--no-daemon", *start]
return [*CLAUDE_AGENT, "--model", self.model, "--effort", self.effort, *(["--resume", self.session_id] if self.session_id else [])]
def docker(self, name, public, output, state, scratch):
command = ["docker", "run", "--rm", "-i", "--name", name, "--label", f"simready.campaign={self.campaign}", "--cap-drop", "ALL",
"--security-opt", "no-new-privileges", "--workdir", "/work",
"--network", EGRESS_NETWORK, *[part for key, value in EGRESS_ENV.items() for part in ("-e", f"{key}={value}")],
"--mount", f"type=bind,src={public},dst=/work/input,readonly", "--mount", f"type=bind,src={output},dst=/work/output",
"--mount", f"type=bind,src={state},dst=/auth", "--mount", f"type=bind,src={scratch},dst=/tmp"]
if (public.parent / "tools").is_dir():
command += ["--mount", f"type=bind,src={public.parent / 'tools'},dst=/work/tools,readonly"]
if self.isaac:
command += ["--gpus", "device=" + self.gpu, "--user", ISAAC_USER, "--pids-limit", "2048", "--memory", "48g", "--cpus", "12", "--shm-size", "2g",
"--entrypoint", "bash", "-e", "ACCEPT_EULA=Y", "-e", "NVIDIA_DRIVER_CAPABILITIES=all",
"--mount", f"type=bind,src={imageio_ffmpeg.get_ffmpeg_exe()},dst=/usr/local/bin/ffmpeg,readonly",
"--mount", f"type=volume,src=simready-isaac-{self.cli}-cache,dst=/isaac-sim/.cache"]
else:
command += ["--user", f"{os.getuid()}:{os.getgid()}", "--pids-limit", "512", "--memory", "12g", "--cpus", "4", "--read-only",
"-e", "HOME=/tmp/agent-home"]
if self.cli == "opencode": # the binary is the image's own (pinned); its config, cache and sessions live in the state folder
command += [part for key, value in OPENCODE_ENV.items() for part in ("-e", f"{key}={value}")]
elif self.cli == "codex":
command += ["-e", "CODEX_HOME=/auth", "-e", "PATH=/opt/codex/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin"]
else:
binary = (Path.home() / ".local/bin/claude").resolve()
command += ["-e", "CLAUDE_CONFIG_DIR=/auth", "-e", "DISABLE_AUTOUPDATER=1", "--mount", f"type=bind,src={binary},dst=/usr/local/bin/claude,readonly"]
shell = "" if self.isaac else "mkdir -p /tmp/agent-home && "
return command + [ISAAC_IMAGE if self.isaac else MUJOCO_IMAGE, *(["-lc"] if self.isaac else ["bash", "-c"]), shell + "exec " + shlex.join(self.agent())]
def sync_login(self, name, known):
"""Refresh tokens rotate: a login refreshed in this container reaches the saved login at once, and one refreshed by
another session sharing the login reaches this container, or sessions sharing a login invalidate each other.
Returns what both now hold."""
try:
held = subprocess.run(["docker", "exec", name, "cat", "/auth/" + self.held], capture_output=True, timeout=15, check=True).stdout
saved = self.read_login()
if held and held != known:
with tempfile.TemporaryDirectory() as directory:
fresh = Path(directory)
(fresh / self.held).parent.mkdir(parents=True, exist_ok=True)
(fresh / self.held).write_bytes(held)
(fresh / self.held).chmod(0o600)
self.persist(self.login, fresh, known)
return self.read_login()
if saved != known:
subprocess.run(["docker", "exec", "-i", name, "sh", "-c", f"cat > /auth/{self.held}.new && mv /auth/{self.held}.new /auth/{self.held}"],
input=saved, capture_output=True, timeout=15, check=True)
return saved
except (subprocess.SubprocessError, OSError, ValueError, RuntimeError):
pass # the end of the stage saves it anyway
return known
def stage(self, run, record, prompt, seconds, state, scratch):
"""One timed container. Stopping at the budget is normal; the agent's session survives in `state`."""
public, output, log = run / "input", run / "output", run / "generation.jsonl"
initial = self.read_login()
(state / self.held).parent.mkdir(mode=0o700, exist_ok=True)
(state / self.held).write_bytes(initial)
(state / self.held).chmod(0o600)
if self.cli == "claude": # the account and onboarding fields only, so the CLI starts without its first-run questions
config = self.login / ".claude.json"
config = config if config.is_file() else Path.home() / ".claude.json"
content = json.loads(config.read_text())
(state / ".claude.json").write_text(json.dumps({k: content[k] for k in ("oauthAccount", "hasCompletedOnboarding", "installMethod") if k in content}))
if self.isaac:
for directory in (state, output, scratch):
change_owner(directory, ISAAC_USER)
name = re.sub(r"[^a-zA-Z0-9_.-]", "-", f"simready-gen-{self.track}-{self.engine}-{self.item}-{run.name.lower()}")
started = time.monotonic()
record.update(status="running", started_at=datetime.now(timezone.utc).isoformat())
save_record(run / "run.json", record)
guard = getattr(self, "quota_guard", None)
if guard:
reason = guard.reason(self.cli)
if reason:
log.touch()
record.update(status="quota_paused", quota_pause_reason=reason,
api_retries=0, rate_limit_events=0, exit_code=None,
credential_refresh_persistence="unchanged", cli_session_id=self.session_id,
finished_at=datetime.now(timezone.utc).isoformat())
return 0.
try:
with log.open("w") as stream:
ensure_egress()
process = subprocess.Popen(self.docker(name, public, output, state, scratch), stdin=subprocess.PIPE, stdout=stream,
stderr=subprocess.STDOUT, text=True, start_new_session=True)
process.stdin.write(prompt)
process.stdin.close()
try:
while True:
try:
process.wait(timeout=min(LOGIN_SYNC_SECONDS, max(seconds - (time.monotonic() - started), .1)))
break
except subprocess.TimeoutExpired:
reason = guard.reason(self.cli) if guard else None
if guard and self.cli == "opencode" and guard.config.get("opencode_pause_on_limit"):
reason = reason or opencode_limit(log)
if reason:
guard.pause(self.cli, reason)
if reason:
record["quota_pause_reason"] = reason
raise
if time.monotonic() - started >= seconds:
raise
if not record.get("model_answered"):
record["model_answered"] = model_answered(self.cli, log)
if not record["model_answered"] and time.monotonic() - started >= STALL_SECONDS:
record["stalled"] = True
raise
initial = self.sync_login(name, initial)
record.update(status="completed" if process.returncode == 0 else "cli_failed", exit_code=process.returncode)
except subprocess.TimeoutExpired:
subprocess.run(["docker", "kill", name], capture_output=True)
try: # an Isaac container on a loaded host can take minutes to stop; the result stands either way
process.wait(timeout=STOP_SECONDS)
except subprocess.TimeoutExpired:
process.kill()
process.wait()
record.update(status="quota_paused" if record.get("quota_pause_reason") else
"cli_failed" if record.get("stalled") else "budget_expired", exit_code=process.returncode)
finally:
subprocess.run(["docker", "rm", "-f", name], capture_output=True)
if self.isaac:
for directory in (state, output, scratch):
change_owner(directory, f"{os.getuid()}:{os.getgid()}")
record["credential_refresh_persistence"] = self.persist(self.login, state, initial)
record["finished_at"] = datetime.now(timezone.utc).isoformat()
self.session_id = self.session_id or session_identity(self.cli, log)
record["cli_session_id"] = self.session_id
record.update(throttling(log))
if guard and self.cli == "opencode" and guard.config.get("opencode_pause_on_limit"):
reason = opencode_limit(log)
if reason:
guard.pause(self.cli, reason)
record.update(status="quota_paused", quota_pause_reason=reason)
if record.get("stalled"): # no answer at all: a refused login (rate or usage limit), not the agent's work
record["rate_limit_events"] = max(1, record["rate_limit_events"])
return time.monotonic() - started
def run(self, budgets, on_result=None):
"""Run every budget; `on_result(run, record)` is called as each result is ready to evaluate."""
rows, used, previous, earlier_cost = [], 0., None, None
protocol = frozen_protocol(self.track, self.engine)
if not protocol.is_file():
print(f"warning: {shown(protocol)} is not frozen yet; the run records protocol sha256 null", flush=True)
(RUNTIME / "session_scratch").mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryDirectory(prefix="simready-session-") as directory, tempfile.TemporaryDirectory(dir=RUNTIME / "session_scratch") as scratch:
state = Path(directory)
state.chmod(0o700)
Path(scratch).chmod(0o777)
try:
for budget in budgets:
if budget - used <= 0:
break
rid, run = new_run(self.runs_root, self.track, self.engine, self.item)
if previous is None:
(run / "output").mkdir()
else:
shutil.copytree(previous / "output", run / "output", symlinks=True)
(run / "output").chmod(0o777)
_, first = stage(self.track, self.engine, self.item, run, budgets[0], self.cli)
prompt = first if previous is None else contracts.resume_text(budgets[len(rows) - 1], budget - used)
(run / "submitted_prompt.txt").write_text(prompt)
contract = contracts.record(self.contract)
record = {"track": self.track, "engine": self.engine, "item": self.item, "run_id": rid, "campaign": self.campaign,
"cli": self.cli, "model": self.model, "model_reasoning_effort": self.effort,
"budget_seconds": budget, "told_budget_seconds": budgets[0], "stage": len(rows) + 1,
"continued_from": previous.name if previous else None, "contract": contract,
"engine_version": contract["engine_version"], "image": contract["image"],
"image_id": subprocess.check_output(["docker", "image", "inspect", contract["image"], "--format", "{{.Id}}"], text=True).strip(),
"required_refinement_rounds": (contract["refinement"] or {}).get("minimum_completed_rounds"), "gpu_uuid": self.gpu,
"input_snapshot": "input", "tools": "tools" if (run / "tools").is_dir() else None,
"evaluator_protocol": "evaluator_protocol.json" if (run / "evaluator_protocol.json").is_file() else None,
"output": "output", "log": "generation.jsonl", "status": "preparing",
"agent_cli": contracts.cli_record(self.cli, self.model, self.claude_config_dir)}
save_record(run / "run.json", record)
print(f"Run: {shown(run)}", flush=True)
record["stage_seconds"] = round(self.stage(run, record, prompt, budget - used, state, Path(scratch)), 3)
used += record["stage_seconds"]
record["elapsed_seconds"] = round(used, 3)
if self.cli == "codex":
keep_codex_usage(state, run) # the session's counts are cumulative across resumes
if self.cli == "opencode":
keep_opencode_usage(state / OPENCODE_DATA, run) # cumulative too: every session in its database
cost = run_cost(run, record)
record["cost"] = combine_costs(earlier_cost, cost) if self.cli == "claude" else cost
earlier_cost = record["cost"]
record["scene_present"] = any((run / "output" / name).is_file() for name in ("scene.xml", "scene.usda", "scene.usd"))
save_record(run / "run.json", record)
rows.append(run)
previous = run
if on_result:
on_result(run, record)
if record["status"] in {"cli_failed", "quota_paused"} or not self.session_id:
break
finally:
if self.isaac:
change_owner(scratch, f"{os.getuid()}:{os.getgid()}")
return rows
|