File size: 10,873 Bytes
e2eb9d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3be06e2
e2eb9d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import json
import os
import re
import sys
import time
from typing import Any, Dict, List, Optional

import requests
from openai import OpenAI


# Load .env file if present (works without it too)
try:
    from dotenv import load_dotenv
    load_dotenv()
except ImportError:
    pass
# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------

IMAGE_NAME   = os.getenv("IMAGE_NAME")
API_KEY      = os.getenv("HF_TOKEN") or os.getenv("OPENAI_API_KEY") 
API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
MODEL_NAME   = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
BASE_URL     = os.getenv("ENV_BASE_URL", "http://localhost:8000")

BENCHMARK   = "agentops-gym"
MAX_STEPS   = 10
TEMPERATURE = 0.3
MAX_TOKENS  = 600

ALL_TASKS = ["task_1", "task_2", "task_3", "task_4"]

# ---------------------------------------------------------------------------
# System prompt
# ---------------------------------------------------------------------------

SYSTEM_PROMPT = """\
You are an expert software engineer agent. You solve coding tasks by calling tools.

Available tools:
  FileRead   β€” Read a file.         Parameters: {"filename": "path/to/file.py"}
  FileWrite  β€” Write/overwrite.     Parameters: {"filename": "...", "content": "..."}
  Grep       β€” Search all files.    Parameters: {"pattern": "regex_or_string"}
  Bash       β€” Simulated shell.     Parameters: {"command": "lint main.py"}
  WebSearch  β€” Search docs.         Parameters: {"query": "python lru_cache"}
  TodoWrite  β€” Record a plan.       Parameters: {"plan": "1. Do X\\n2. Do Y"}

RULES:
1. Respond ONLY with a single JSON object β€” no markdown, no extra text.
2. Format exactly: {"tool": "ToolName", "parameters": {...}, "reasoning": "why"}
3. Be efficient β€” minimize total tool calls.
4. For hard tasks: call TodoWrite FIRST to plan, then act.
5. Never repeat the exact same tool + parameters twice in a row.

Example:
{"tool": "Grep", "parameters": {"pattern": "def fetch"}, "reasoning": "Find the function"}
"""

# ---------------------------------------------------------------------------
# Mandatory stdout log helpers
# ---------------------------------------------------------------------------

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:
    err_val = error if error else "null"
    action_short = str(action).replace("\n", " ")[:200]
    print(
        f"[STEP] step={step} action={action_short} "
        f"reward={reward:.2f} done={str(done).lower()} error={err_val}",
        flush=True,
    )


def log_end(success: bool, steps: int, rewards: List[float]) -> None:
    rewards_str = ",".join(f"{r:.2f}" for r in rewards)
    print(
        f"[END] success={str(success).lower()} steps={steps} rewards={rewards_str}",
        flush=True,
    )

# ---------------------------------------------------------------------------
# HTTP helpers
# ---------------------------------------------------------------------------

def http_reset(task_id: str) -> Dict:
    """POST /reset and return the observation dict."""
    resp = requests.post(
        f"{BASE_URL}/reset",
        json={"task_id": task_id},
        timeout=30,
    )
    resp.raise_for_status()
    return resp.json()


def http_step(tool: str, parameters: Dict, reasoning: str = "") -> Dict:
    """POST /step with the correct body shape and return the response dict."""
    body = {
        "action": {
            "tool": tool,
            "parameters": parameters,
            "reasoning": reasoning,
        }
    }
    resp = requests.post(
        f"{BASE_URL}/step",
        json=body,
        timeout=30,
    )
    resp.raise_for_status()
    return resp.json()


def http_grader() -> Dict:
    resp = requests.get(f"{BASE_URL}/grader", timeout=10)
    if resp.status_code == 200:
        return resp.json()
    return {}

# ---------------------------------------------------------------------------
# Prompt builder
# ---------------------------------------------------------------------------

def build_prompt(obs: Dict) -> str:
    parts = [f"TASK: {obs.get('task_description', '')}"]
    parts.append(f"\nVisible files: {obs.get('visible_files', [])}")
    last = obs.get("last_tool_result")
    if last:
        # Truncate long outputs
        parts.append(f"\nLast tool result:\n{str(last)[:1500]}")
    history = obs.get("action_history", [])
    if history:
        parts.append(f"\nHistory (last 3): {history[-3:]}")
    if obs.get("message"):
        parts.append(f"\nEnv message: {obs['message']}")
    meta = obs.get("metadata", {})
    steps_rem = meta.get("steps_remaining", "?")
    parts.append(f"\nStep {obs.get('step_count', 0)}, steps remaining: {steps_rem}")
    parts.append("\nRespond with a single JSON tool call:")
    return "\n".join(parts)

# ---------------------------------------------------------------------------
# JSON extraction
# ---------------------------------------------------------------------------

def extract_tool_call(text: str) -> Optional[Dict]:
    """Extract a valid JSON tool call from model output."""
    text = text.strip()
    # Strip markdown fences
    if "```" in text:
        for block in text.split("```"):
            block = block.strip().lstrip("json").strip()
            if block.startswith("{"):
                text = block
                break
    # Direct parse
    try:
        obj = json.loads(text)
        if "tool" in obj:
            return obj
    except json.JSONDecodeError:
        pass
    # Extract first {...} block
    m = re.search(r'\{[^{}]+\}', text, re.DOTALL)
    if m:
        try:
            obj = json.loads(m.group())
            if "tool" in obj:
                return obj
        except json.JSONDecodeError:
            pass
    return None

# ---------------------------------------------------------------------------
# Episode runner
# ---------------------------------------------------------------------------

def run_episode(client: OpenAI, task_id: str) -> Dict:
    log_start(task=task_id, env=BENCHMARK, model=MODEL_NAME)

    rewards: List[float] = []
    steps_taken = 0
    score = 0.0
    success = False
    error_msg = None

    try:
        # Reset
        reset_resp = http_reset(task_id)
        obs = reset_resp.get("observation", {})

        for step in range(1, MAX_STEPS + 1):
            if reset_resp.get("done") or obs.get("done"):
                break

            # Ask the model
            prompt = build_prompt(obs)
            try:
                completion = client.chat.completions.create(
                    model=MODEL_NAME,
                    messages=[
                        {"role": "system", "content": SYSTEM_PROMPT},
                        {"role": "user",   "content": prompt},
                    ],
                    max_tokens=MAX_TOKENS,
                    temperature=TEMPERATURE,
                )
                raw = (completion.choices[0].message.content or "").strip()
            except Exception as e:
                error_msg = f"LLM error: {e}"
                log_step(step=step, action="(llm_error)", reward=0.0, done=True, error=str(e))
                break

            tool_call = extract_tool_call(raw)
            if tool_call is None:
                # Fallback: safe no-op grep
                tool_call = {
                    "tool": "Grep",
                    "parameters": {"pattern": "def "},
                    "reasoning": "fallback β€” could not parse model output",
                }

            tool      = tool_call.get("tool", "Grep")
            params    = tool_call.get("parameters", {})
            reasoning = tool_call.get("reasoning", "")
            action_str = f"{tool}({json.dumps(params)})"

            # Execute
            try:
                step_resp = http_step(tool, params, reasoning)
            except requests.HTTPError as e:
                error_msg = str(e)
                log_step(step=step, action=action_short, reward=0.0, done=True, error=error_msg)
                break

            obs     = step_resp.get("observation", {})
            reward  = float(step_resp.get("reward", 0.0) or 0.0)
            done    = bool(step_resp.get("done", False))
            rewards.append(reward)
            steps_taken = step

            log_step(step=step, action=action_str, reward=reward, done=done, error=None)

            if done:
                break

        # Fetch grader score
        grader = http_grader()
        score = float(grader.get("score", 0.0) or 0.0)
        success = score >= 0.5

    except Exception as exc:
        print(f"[DEBUG] Episode error for {task_id}: {exc}", flush=True)

    finally:
        log_end(success=success, steps=steps_taken, rewards=rewards)

    return {
        "task_id":  task_id,
        "score":    score,
        "steps":    steps_taken,
        "success":  success,
        "rewards":  rewards,
    }


def main() -> None:
    if not API_KEY:
        print("ERROR: HF_TOKEN (or API_KEY) must be set.", file=sys.stderr)
        print("  export HF_TOKEN=hf_xxx", file=sys.stderr)
        sys.exit(1)

    for attempt in range(10):
        try:
            r = requests.get(f"{BASE_URL}/health", timeout=5)
            if r.status_code == 200:
                break
        except Exception:
            pass
        print(f"[DEBUG] Waiting for server... attempt {attempt+1}/10", flush=True)
        time.sleep(2)
    else:
        print("ERROR: Server did not become ready.", file=sys.stderr)
        sys.exit(1)

    client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)

    print("=" * 60, flush=True)
    print(f"AgentOps Gym β€” Baseline Inference", flush=True)
    print(f"Model: {MODEL_NAME}  |  Server: {BASE_URL}", flush=True)
    print("=" * 60, flush=True)

    results = []
    for task_id in ALL_TASKS:
        print("─" * 40, flush=True)
        result = run_episode(client, task_id)
        results.append(result)

    print("=" * 60, flush=True)
    print("BASELINE SUMMARY", flush=True)
    print("=" * 60, flush=True)

    total   = sum(r["score"] for r in results)
    solved  = sum(1 for r in results if r["success"])
    avg     = total / len(results) if results else 0.0

    for r in results:
        status = "βœ… PASS" if r["success"] else "❌ FAIL"
        print(f"  {r['task_id']:>8}    score={r['score']:.3f}  steps={r['steps']:2d}  {status}", flush=True)

    print(f"\n  Average score: {avg:.3f}", flush=True)
    print(f"  Solved: {solved} / {len(results)}", flush=True)
    print("=" * 60, flush=True)


if __name__ == "__main__":
    main()