Spaces:
Running
Running
File size: 17,868 Bytes
03a907a | 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 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | """
inference.py TraceRL OpenEnv Baseline Agent
==============================================
MetaEnv Round 1 competition-compliant inference script.
MANDATORY ENV VARS (injected by grading server):
API_BASE_URL The LLM API endpoint (OpenAI-compatible)
MODEL_NAME The model identifier for inference
HF_TOKEN Hugging Face / API key
STDOUT FORMAT (machine-parsed by automated validator):
[START] task=<task_name> env=<benchmark> model=<model_name>
[STEP] step=<n> action=<action_str> reward=<0.00> done=<true|false> error=<msg|null>
[END] success=<true|false> steps=<n> rewards=<r1,r2,...,rn>
RULES:
- reward / rewards always formatted to exactly 2 decimal places (f"{reward:.2f}")
- done / success always lowercase boolean strings: true / false
- error is raw last_action_error string, or null if none
- One [START], one [STEP] per env.step(), one [END] always even on exception
"""
import os
import sys
import textwrap
import difflib
import re
import time
from typing import List, Optional
from openai import OpenAI
import unidiff
from client import CodeFixerEnv
from models import CodeFixerAction
from dotenv import load_dotenv
load_dotenv()
import logging
logger = logging.getLogger(__name__)
def _enable_hermetic_runtime() -> None:
"""
Keep this process isolated from parent/global Python environment leakage.
"""
if os.getenv("HERMETIC_RUN", "1").strip().lower() not in {"1", "true", "yes"}:
return
os.environ["PYTHONPATH"] = ""
os.environ["PYTHONNOUSERSITE"] = "1"
os.environ["PYTHONDONTWRITEBYTECODE"] = "1"
os.environ.pop("PYTHONHOME", None)
_enable_hermetic_runtime()
API_BASE_URL = os.getenv("API_BASE_URL", "https://integrate.api.nvidia.com/v1")
API_KEY = os.getenv("HF_TOKEN") or os.getenv("API_KEY")
MODEL_NAME = os.getenv("MODEL_NAME", "qwen/qwen2.5-coder-32b-instruct")
MAX_STEPS = int(os.getenv("MAX_STEPS", "10"))
TEMPERATURE = float(os.getenv("TEMPERATURE", "0.7"))
MAX_TOKENS = int(os.getenv("MAX_TOKENS", "512"))
SUCCESS_SCORE_THRESHOLD = float(os.getenv("SUCCESS_THRESHOLD", "0.5"))
MAX_RETRIES = int(os.getenv("MAX_RETRIES", "3"))
_DIFFICULTIES = ["easy", "medium", "hard"]
TASK_NAME = (os.getenv("TRACERL_TASK") or "").strip().lower()
BENCHMARK = os.getenv("TRACERL_BENCHMARK", "rl-code-fix-env")
ENV_URL = os.getenv("ENV_URL", "http://localhost:8000")
if "TASK_SOURCE" not in os.environ:
os.environ["TASK_SOURCE"] = "swebench"
if TASK_NAME and TASK_NAME not in _DIFFICULTIES:
print(
f"[DEBUG] Invalid TRACERL_TASK='{TASK_NAME}'. Falling back to all tasks.",
flush=True, file=sys.stderr,
)
TASK_NAME = ""
llm = OpenAI(api_key=API_KEY, base_url=API_BASE_URL)
SYSTEM_PROMPT = textwrap.dedent("""
You are an autonomous bug-fixing agent. You will be given a Python function or module that contains one or more bugs.
Your task is to produce corrected Python code that fixes all bugs.
Output format — you MUST follow this exactly:
- Return ONLY the corrected code, nothing else.
- Do NOT include markdown fences (```), prose, or explanations.
- If the code has no bugs, output exactly: NO_CHANGE
Code rules:
- Preserve the original function signature exactly.
- Do not add new imports unless strictly required by the fix.
- Do not add print statements, debug code, or comments that were not in the original.
- Apply the minimal fix needed. Do not refactor, rename, or reformat unrelated lines.
Think step-by-step internally, but your final output must be ONLY corrected code or NO_CHANGE.
""").strip()
def _build_user_prompt(observation: dict, history: List[str], step: int) -> str:
"""Build the per-step prompt from the current environment observation."""
code = observation.get("code", "(no code provided)")
test_score = observation.get("test_score", 0.0)
total_tests = observation.get("total_tests", 1)
error_logs = observation.get("logs", "")
test_status = "PASSING" if test_score >= 1.0 else "FAILING"
parts = [
f"Step: {step}",
f"Tests Status: {test_status} (score {test_score:.2f}/{total_tests})",
"",
"=== Current Code ===",
code,
"",
]
if error_logs:
parts += ["=== Error Logs ===", error_logs, ""]
# FIX: include actual previous patch attempts so LLM learns from history
if history:
parts += ["=== Previous Attempts (last 3) ==="]
parts += history[-3:]
parts += [""]
parts += [
"Your task: provide the corrected full code for the snippet above.",
"Return ONLY corrected code (or NO_CHANGE), no explanations or markdown.",
]
return "\n".join(parts)
def log_start(task: str, env: str, model: str) -> None:
print(f"[START] task={task} env={env} model={model}", flush=True)
def log_step(
step: int,
action: str,
reward: float,
done: bool,
error: Optional[str],
) -> None:
done_str = "true" if done else "false"
error_str = error if error else "null"
action_str = action.replace("\n", "\\n").replace("\r", "")
print(
f"[STEP] step={step} action={action_str} "
f"reward={reward:.2f} done={done_str} error={error_str}",
flush=True,
)
def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
success_str = "true" if success else "false"
rewards_str = ",".join(f"{r:.2f}" for r in rewards)
print(
f"[END] success={success_str} steps={steps} rewards={rewards_str}",
flush=True,
)
_env_client: Optional[CodeFixerEnv] = None
def _get_client() -> CodeFixerEnv:
global _env_client
if _env_client is None:
_env_client = CodeFixerEnv(base_url=ENV_URL)
return _env_client
def env_reset() -> dict:
result = _get_client().reset()
obs = result.observation
return {
"observation": {
"code": obs.code,
"logs": obs.logs,
"test_score": obs.test_score,
"total_tests": obs.total_tests,
"steps": obs.steps,
},
"done": getattr(obs, "done", False),
}
def env_step(action_type: str, payload: str = "") -> dict:
try:
result = _get_client().step(CodeFixerAction(type=action_type, payload=payload))
obs = result.observation
return {
"observation": {
"code": obs.code,
"logs": obs.logs,
"test_score": obs.test_score,
"total_tests": obs.total_tests,
"steps": obs.steps,
},
"reward": result.reward,
"done": result.done,
"error": None,
}
except Exception as exc:
return {
"observation": {
"code": "",
"logs": "",
"test_score": 0.0,
"total_tests": 1,
"steps": 0,
},
"reward": 0.0,
"done": False,
"error": str(exc),
}
def _strip_markdown_fences(text: str) -> str:
cleaned = (text or "").strip()
# Remove opening fence with optional language
if cleaned.startswith("```"):
cleaned = re.sub(r"^```[a-zA-Z0-9_-]*\s*\n", "", cleaned)
cleaned = re.sub(r"\n```$", "", cleaned.strip())
return cleaned.strip()
def _looks_like_unified_diff(text: str) -> bool:
return ("--- " in text and "+++ " in text and "@@ " in text)
def _is_valid_unified_diff(text: str) -> bool:
try:
unidiff.PatchSet(text)
return True
except Exception:
return False
def _build_unified_diff(original: str, revised: str) -> str:
lines = difflib.unified_diff(
original.splitlines(),
revised.splitlines(),
fromfile="a/code.py",
tofile="b/code.py",
lineterm="",
)
return "\n".join(lines).strip()
def _looks_like_python_code(text: str) -> bool:
"""Check if text looks like Python code (not a diff, not prose)."""
text_lower = text.lower().strip()
# Not a diff
if _looks_like_unified_diff(text):
return False
# Not just "no change" or similar
if text_lower in ("no_change", "no change", "no changes", "no changes made",
"the code is correct", "already correct", "no bugs found",
"no bug", "no bugs", "already fixed"):
return False
# Check for Python-like patterns (def, class, import, etc.)
python_indicators = ['def ', 'class ', 'import ', 'from ', 'return ',
'if __name__', 'async def', 'self.', 'print(']
return any(indicator in text for indicator in python_indicators)
def _looks_like_output_value(text: str) -> bool:
"""Check if text is just an output value like [[3,1],[4,2]]"""
text = text.strip()
# Looks like a literal value/expression, not code
if text.startswith('[') and text.endswith(']') and '\n' not in text:
# Could be a list/dict literal
if ',' in text or ':' in text:
return True
# Single word or simple expression
if '\n' not in text and len(text.split()) <= 3:
# Check if it's not a function definition
if 'def ' not in text and 'class ' not in text:
return True
return False
def _normalize_action(raw_action: str, original_code: str) -> str:
"""Normalize LLM output to unified diff format."""
cleaned = _strip_markdown_fences(raw_action)
# Handle empty or NO_CHANGE cases
if not cleaned:
return ""
cleaned_lower = cleaned.lower().strip()
if cleaned_lower == "no_change":
return ""
# Check for common "no change" phrases
no_change_phrases = [
"no change", "no changes", "no changes made",
"the code is correct", "already correct", "no bugs found",
"no bug", "no bugs", "already fixed", "no modifications needed",
"the provided code is already correct", "code appears to be correct"
]
if any(phrase in cleaned_lower for phrase in no_change_phrases):
return ""
# Check if output looks like a raw value (not code) - reject it
if _looks_like_output_value(cleaned):
print(f"[DEBUG] Rejected output that looks like a value: {cleaned[:50]}...",
flush=True, file=sys.stderr)
return ""
# Check if it looks like a unified diff already
if _looks_like_unified_diff(cleaned):
return cleaned if _is_valid_unified_diff(cleaned) else ""
# Check if it looks like Python code - convert to diff
if _looks_like_python_code(cleaned):
generated = _build_unified_diff(original_code, cleaned)
return generated if _is_valid_unified_diff(generated) else ""
# If nothing matches, return empty (invalid output)
print(f"[DEBUG] Could not parse LLM output as code or diff: {cleaned[:100]}...",
flush=True, file=sys.stderr)
return ""
def get_action(observation: dict, history: List[str], step: int) -> str:
"""
Call the LLM to produce the next action.
Returns a unified diff patch string or empty string on failure.
"""
user_prompt = _build_user_prompt(observation, history, step)
for attempt in range(MAX_RETRIES):
try:
completion = llm.chat.completions.create(
model=MODEL_NAME,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_prompt},
],
temperature=TEMPERATURE,
max_tokens=MAX_TOKENS,
stream=False,
)
raw_action = (completion.choices[0].message.content or "").strip()
return _normalize_action(raw_action, observation.get("code", ""))
except Exception as exc:
print(
f"[DEBUG] LLM call failed at step {step} (attempt {attempt+1}/{MAX_RETRIES}): {exc}",
flush=True, file=sys.stderr,
)
if "429" in str(exc) and attempt < MAX_RETRIES - 1:
# FIX: exponential backoff with longer base delay for 429s
sleep_time = 5 * (2 ** attempt)
print(
f"[DEBUG] Rate limited. Sleeping {sleep_time}s before retry.",
flush=True, file=sys.stderr,
)
time.sleep(sleep_time)
continue
if attempt == MAX_RETRIES - 1:
return ""
return ""
def main() -> None:
forced_task = os.getenv("TRACERL_TASK")
task_plan = [TASK_NAME] if forced_task else _DIFFICULTIES[:]
try:
for planned_task in task_plan:
rewards: List[float] = []
history: List[str] = []
steps_taken: int = 0
success: bool = False
score: float = 0.0
final_test_score: float = 0.0
episode_start = time.perf_counter()
log_start(task=planned_task, env=BENCHMARK, model=MODEL_NAME)
print(f"[DEBUG] planned_task={planned_task}", flush=True, file=sys.stderr)
try:
reset_resp = env_reset()
observation = reset_resp.get("observation", {})
done = reset_resp.get("done", False)
for step in range(1, MAX_STEPS + 1):
step_start = time.perf_counter()
if done:
break
# Get patch from LLM
action = get_action(observation, history, step)
# Step 1: apply the patch
patch_resp = env_step("apply_patch", action)
patch_obs = patch_resp.get("observation", {})
patch_error = patch_resp.get("error") or None
if not patch_error and patch_obs.get("code"):
observation = patch_obs
# Step 2: always run tests after patching to get real reward signal
test_resp = env_step("run_tests", "")
next_obs = test_resp.get("observation", {})
reward = float(test_resp.get("reward", 0.0))
done = bool(test_resp.get("done", False))
test_error = test_resp.get("error") or None
error = patch_error or test_error
if not test_error and next_obs.get("code"):
observation = next_obs
test_score = float(next_obs.get("test_score", 0.0))
final_test_score = test_score
total_tests = int(next_obs.get("total_tests", 1))
test_status = "PASS" if test_score >= 1.0 else "FAIL"
raw_logs = (next_obs.get("logs") or "").strip()
log_preview = raw_logs.replace("\n", " | ")
if len(log_preview) > 300:
log_preview = log_preview[:300] + "...(truncated)"
print(
f"[DEBUG] tests status={test_status} score={test_score:.2f}/{total_tests}",
flush=True, file=sys.stderr,
)
if log_preview:
print(f"[DEBUG] test_logs={log_preview}", flush=True, file=sys.stderr)
rewards.append(reward)
steps_taken = step
log_step(step=step, action=action, reward=reward, done=done, error=error)
print(
f"[DEBUG] step={step} elapsed_s={time.perf_counter() - step_start:.3f}",
flush=True, file=sys.stderr,
)
# FIX: store actual patch in history so LLM sees what it already tried
history.append(
f"Step {step}: patch={'(none)' if not action else action[:120]} | "
f"test={test_status} reward={reward:.2f}"
)
if done:
success = final_test_score >= SUCCESS_SCORE_THRESHOLD
break
else:
success = False
score = min(max(final_test_score, 0.0), 1.0)
if not done:
success = score >= SUCCESS_SCORE_THRESHOLD
except Exception as exc:
print(f"[DEBUG] Episode crashed: {exc}", flush=True, file=sys.stderr)
success = False
score = min(max(final_test_score, 0.0), 1.0)
finally:
log_end(success=success, steps=steps_taken, score=score, rewards=rewards)
print(
f"[DEBUG] total_elapsed_s={time.perf_counter() - episode_start:.3f}",
flush=True, file=sys.stderr,
)
finally:
if _env_client is not None:
try:
_env_client.close()
except Exception:
pass
if __name__ == "__main__":
main()
|