File size: 8,179 Bytes
9c9da24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import asyncio
import os
import time
from typing import Any, Dict, List, Optional, Tuple

from openai import OpenAI

from src.agent import LLMAgent, SystemPrompts
from src.environment import TrainingEnv

API_BASE_URL = os.getenv("API_BASE_URL", "https://api.openai.com/v1").strip()
MODEL_NAME = os.getenv("MODEL_NAME", "gpt-5.3").strip()
HF_TOKEN = os.getenv("HF_TOKEN", "").strip()

# Validate
if not MODEL_NAME:
    raise ValueError("MODEL_NAME is not set in environment variables")

if not HF_TOKEN:
    raise ValueError("HF_TOKEN (API key) is not set in environment variables")
BENCHMARK = os.getenv("BENCHMARK_NAME", "linux-sre-env").strip()


def _safe_int(raw: str, default: int) -> int:
    try:
        return int(raw)
    except Exception:
        return default


def _safe_float(raw: str, default: float) -> float:
    try:
        return float(raw)
    except Exception:
        return default


MAX_STEPS_OVERRIDE = _safe_int(os.getenv("MAX_STEPS", "0"), 0)
TEMPERATURE = _safe_float(os.getenv("INFER_TEMPERATURE", "0.2"), 0.2)
MAX_TOKENS = _safe_int(os.getenv("INFER_MAX_TOKENS", "320"), 320)
SUCCESS_SCORE_THRESHOLD = _safe_float(
    os.getenv("SUCCESS_SCORE_THRESHOLD", "0.8"), 0.8)
GLOBAL_TIMEOUT = _safe_int(
    os.getenv("GLOBAL_TIMEOUT", "1140"), 1140)  # 19 min (1 min buffer)
PER_TASK_TIMEOUT = _safe_int(
    os.getenv("PER_TASK_TIMEOUT", "180"), 180)  # 3 min per task
MAX_STEPS_CAP = 15  # hard cap per task to stay within runtime budget


def validate_required_env() -> None:
    missing: List[str] = []
    if not API_BASE_URL:
        missing.append("API_BASE_URL")
    if not MODEL_NAME:
        missing.append("MODEL_NAME")
    if not HF_TOKEN:
        missing.append("HF_TOKEN")
    if missing:
        raise RuntimeError(
            "Missing required environment variables: " + ", ".join(missing))


def _bool_text(value: bool) -> str:
    return "true" if value else "false"


def _one_line(value: str) -> str:
    return str(value).replace("\r", " ").replace("\n", " ").strip()


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:
    error_value = _one_line(error) if error else "null"
    print(
        f"[STEP] step={step} action={_one_line(action)} reward={reward:.2f} done={_bool_text(done)} error={error_value}",
        flush=True,
    )


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


def build_client() -> OpenAI:
    validate_required_env()
    return OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)


def build_messages(initial: Dict[str, Any], history: List[str], step: int, last_reward: float) -> List[Any]:
    system_prompt = (
        SystemPrompts.get_sys(str(initial.get("task_name", "linux-sre")))
        + "\n\nReturn exactly one shell command for the next best move."
    )

    observation = SystemPrompts.format_observation(initial)
    recent = "\n".join(history[-4:]) if history else "None"
    user_prompt = (
        f"{observation}\n\n"
        f"Step: {step}\n"
        f"Last reward: {last_reward:.2f}\n"
        f"Recent attempts:\n{recent}\n\n"
        "Now provide the next command."
    )

    return [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": user_prompt},
    ]


def decide_action(
    client: OpenAI,
    initial: Dict[str, Any],
    history: List[str],
    step: int,
    last_reward: float,
) -> Tuple[str, Optional[str]]:
    try:
        messages: List[Any] = build_messages(
            initial, history, step, last_reward)
        response: Any = client.chat.completions.create(
            model=MODEL_NAME,
            messages=messages,
            temperature=TEMPERATURE,
            max_tokens=MAX_TOKENS,
            stream=False,
        )
        raw_text = str(response.choices[0].message.content or "").strip()
        command = LLMAgent.extract_command(raw_text) or raw_text
        command = _one_line(command)
        if not command:
            return "echo noop", "empty_model_output"
        return command, None
    except Exception as exc:
        return "echo fallback", _one_line(str(exc))


async def run_task(task_key: str) -> None:
    env = TrainingEnv(scenario=task_key)
    reset_out = env.reset()
    prompt_state: Dict[str, Any] = {
        "task_name": reset_out["info"]["task_name"],
        "task_instructions": reset_out["info"]["instructions"],
        "observation": reset_out["observation"],
        "max_steps": reset_out["info"]["max_steps"],
    }
    client = build_client()

    rewards: List[float] = []
    history: List[str] = []
    steps_taken = 0
    score = 0.0
    success = False
    last_reward = 0.0

    max_steps = MAX_STEPS_OVERRIDE if MAX_STEPS_OVERRIDE > 0 else min(
        int(reset_out["info"]["max_steps"]), MAX_STEPS_CAP)

    log_start(task=task_key, env=BENCHMARK, model=MODEL_NAME)
    task_start = time.monotonic()

    try:
        for step in range(1, max_steps + 1):
            if env.finished:
                break
            if time.monotonic() - task_start > PER_TASK_TIMEOUT:
                break

            command, model_error = decide_action(
                client, prompt_state, history, step, last_reward)
            result = env.step(command)

            reward = float(result.get("reward") or 0.0)
            done = bool(result.get("done"))
            info = result.get("info", {})
            score = float(info.get("task_score", score))
            if "observation" in result:
                prompt_state["observation"] = result["observation"]

            step_error = model_error
            if not step_error:
                raw_error = info.get("last_action_error") or info.get("error")
                if raw_error:
                    step_error = _one_line(str(raw_error))

            rewards.append(reward)
            steps_taken = step
            last_reward = reward

            log_step(step=step, action=command, reward=reward,
                     done=done, error=step_error)
            history.append(f"step={step} action={command} reward={reward:.2f}")

            if done:
                break

        # Clamp score strictly between 0 and 1 (validator rejects 0.0 and 1.0)
        score = min(max(score, 0.0), 1.0)
        if score <= 0.0:
            score = 0.01
        if score >= 1.0:
            score = 0.99
        success = score >= SUCCESS_SCORE_THRESHOLD

    except Exception as exc:
        steps_taken += 1
        log_step(
            step=steps_taken,
            action="internal_error",
            reward=0.0,
            done=True,
            error=_one_line(str(exc)),
        )

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


# Default set of tasks to run (must be >= 3 to satisfy the grader requirement)
DEFAULT_TASKS = ["log_analysis", "permission_repair", "process_recovery"]


async def main() -> None:
    parser = argparse.ArgumentParser(
        description="Custom OpenEnv inference runner.")
    parser.add_argument("--task", default=None,
                        help="Scenario key to run (omit to run default 3 tasks)")
    parser.add_argument("--all", action="store_true",
                        help="Run every available task")
    args = parser.parse_args()

    validate_required_env()

    if args.all:
        task_keys = TrainingEnv.avail_tasks()
    elif args.task:
        task_keys = [args.task]
    else:
        task_keys = DEFAULT_TASKS
    global_start = time.monotonic()
    for key in task_keys:
        if time.monotonic() - global_start > GLOBAL_TIMEOUT:
            print(
                f"[END] success=false steps=0 score=0.000 rewards= # skipped (global timeout)", flush=True)
            break
        await run_task(key)


if __name__ == "__main__":
    asyncio.run(main())