File size: 10,474 Bytes
2243dbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Helpers for the browser-driven inference console."""

from __future__ import annotations

import asyncio
import io
import time
from contextlib import redirect_stderr, redirect_stdout
from typing import Any, Dict, List, Optional

from pydantic import BaseModel, Field

try:
    from .. import inference as inference_runner
    from .atc_environment import ATCOptimizationEnvironment
except ImportError:
    import inference as inference_runner

    from server.atc_environment import ATCOptimizationEnvironment


UI_TASKS: List[Dict[str, Any]] = [
    {
        "task_id": "delhi_monsoon_recovery_easy",
        "title": "Delhi Monsoon Recovery",
        "difficulty": "Easy",
        "summary": "10 flights | 2 runways | weather disruption",
        "random_baseline": 0.21,
    },
    {
        "task_id": "mumbai_bank_balance_medium",
        "title": "Mumbai Hub Bank Balance",
        "difficulty": "Medium",
        "summary": "13 flights | 2 runways | airline equity",
        "random_baseline": 0.18,
    },
    {
        "task_id": "bengaluru_irrops_hard",
        "title": "Bengaluru IRROPS Recovery",
        "difficulty": "Hard",
        "summary": "17 flights | 2 runways | emergency priority",
        "random_baseline": 0.12,
    },
    {
        "task_id": "hyderabad_cargo_crunch_medium_hard",
        "title": "Hyderabad Cargo Crunch",
        "difficulty": "Hard",
        "summary": "7 flights | 1 runway | capacity constraint",
        "random_baseline": 0.15,
    },
]

MODEL_OPTIONS = [
    "heuristic-baseline",
    "Qwen/Qwen2.5-7B-Instruct",
    "Qwen/Qwen2.5-72B-Instruct",
    "microsoft/Phi-3.5-mini-instruct",
    "mistralai/Mistral-Nemo-Instruct-2407",
    "meta-llama/Llama-3.1-8B-Instruct",
]

AVERAGE_RANDOM_BASELINE = round(
    sum(float(task["random_baseline"]) for task in UI_TASKS) / len(UI_TASKS), 4
)
INFERENCE_LOCK = asyncio.Lock()


class InferenceRunRequest(BaseModel):
    """Payload accepted by the console inference endpoint."""

    hf_token: str = Field(default="")
    model_name: str = Field(default="heuristic-baseline")
    api_base_url: str = Field(default_factory=lambda: inference_runner.API_BASE_URL)


def _parse_log_fields(log_line: str) -> Dict[str, str]:
    """Parse key=value tokens from the structured inference logs."""

    fields: Dict[str, str] = {}
    for token in log_line.split()[1:]:
        if "=" in token:
            key, value = token.split("=", 1)
            fields[key] = value
    return fields


def _task_outcome(score: float) -> str:
    if score >= 0.9:
        return "Clean landing"
    if score >= 0.75:
        return "Controlled approach"
    if score >= 0.55:
        return "Holding pattern"
    if score >= 0.35:
        return "Turbulence warning"
    return "Loss of control"


def _scene_for_run(avg_score: float, min_score: float, had_stderr: bool) -> Dict[str, str]:
    if had_stderr or min_score < 0.35 or avg_score < 0.45:
        return {
            "scene": "crash",
            "scene_title": "Loss of control",
            "scene_caption": (
                "The run broke down badly enough to trigger the crash case. "
                "Inspect the console log and task strips before trusting the output."
            ),
        }
    if min_score < 0.55 or avg_score < 0.62:
        return {
            "scene": "warning",
            "scene_title": "Turbulence warning",
            "scene_caption": (
                "The model stayed airborne but made enough mistakes that the aircraft "
                "is flying unstable and needs immediate correction."
            ),
        }
    if avg_score < 0.8:
        return {
            "scene": "holding",
            "scene_title": "Holding pattern",
            "scene_caption": (
                "The plan is workable but hesitant. Expect extra vectoring, longer holds, "
                "and lower confidence on the harder tasks."
            ),
        }
    if avg_score < 0.92:
        return {
            "scene": "approach",
            "scene_title": "Controlled approach",
            "scene_caption": (
                "The model is lining up well. This is a stable approach with a few rough "
                "edges, not yet the best possible landing."
            ),
        }
    return {
        "scene": "landing",
        "scene_title": "Smooth landing",
        "scene_caption": (
            "The run stayed composed from start to finish. The aircraft touches down cleanly "
            "with strong task scores and low operational drama."
        ),
    }


def _run_single_task(task: Dict[str, Any], client: Optional[Any]) -> Dict[str, Any]:
    """Execute one task using the same planner logic as inference.py."""

    task_id = str(task["task_id"])
    env = ATCOptimizationEnvironment()
    rewards: List[float] = []
    steps_used = 0
    score = float(inference_runner.SCORE_EPSILON)
    success = False
    runtime_model = (
        inference_runner.MODEL_NAME if client is not None else "heuristic-baseline"
    )

    stdout_buffer = io.StringIO()
    stderr_buffer = io.StringIO()
    with redirect_stdout(stdout_buffer), redirect_stderr(stderr_buffer):
        inference_runner.log_start(
            task=task_id,
            env=inference_runner.BENCHMARK,
            model=runtime_model,
        )
        try:
            observation = env.reset(task_id=task_id)
            max_steps = inference_runner._step_budget(observation.steps_remaining)

            for step_index in range(max_steps):
                if observation.done:
                    break
                step_num = step_index + 1
                action = inference_runner.get_model_action(
                    client=client,
                    observation=observation,
                    task_id=task_id,
                    step=step_num,
                )
                observation = env.step(action)
                reward = float(observation.reward or 0.0)
                rewards.append(reward)
                steps_used = step_num
                inference_runner.log_step(
                    step=step_num,
                    action=(
                        f"submit_plan(count={len(action.proposal)},"
                        f"commit={inference_runner._bool_token(action.commit)})"
                    ),
                    reward=reward,
                    done=observation.done,
                    error=None,
                )
                if observation.done:
                    break

            score = max(
                inference_runner.SCORE_EPSILON,
                min(
                    1.0 - inference_runner.SCORE_EPSILON,
                    observation.current_metrics.overall_score,
                ),
            )
            success = score >= inference_runner.SUCCESS_SCORE_THRESHOLD
        except Exception as exc:  # pragma: no cover - defensive guard
            inference_runner._safe_stderr(f"Task execution failed for {task_id}: {exc}")
        finally:
            inference_runner.log_end(
                task=task_id,
                success=success,
                steps=steps_used,
                score=score,
                rewards=rewards,
            )

    stdout_lines = [
        line.strip() for line in stdout_buffer.getvalue().splitlines() if line.strip()
    ]
    stderr_lines = [
        line.strip() for line in stderr_buffer.getvalue().splitlines() if line.strip()
    ]
    end_fields = _parse_log_fields(stdout_lines[-1]) if stdout_lines else {}

    return {
        "task_id": task_id,
        "title": task["title"],
        "difficulty": task["difficulty"],
        "summary": task["summary"],
        "random_baseline": float(task["random_baseline"]),
        "score": round(float(score), 4),
        "improvement": round(float(score) - float(task["random_baseline"]), 4),
        "steps_used": int(end_fields.get("steps", steps_used or 0)),
        "success": bool(success),
        "outcome": _task_outcome(float(score)),
        "logs": stdout_lines,
        "stderr": stderr_lines,
    }


def run_requested_inference(payload: InferenceRunRequest) -> Dict[str, Any]:
    """Run the selected model end to end for the browser UI."""

    model_name = payload.model_name.strip() or "heuristic-baseline"
    hf_token = payload.hf_token.strip()
    api_base_url = payload.api_base_url.strip().rstrip("/") or inference_runner.API_BASE_URL

    if model_name != "heuristic-baseline" and not hf_token:
        raise ValueError("HF token is required for hosted model inference.")

    previous_model = inference_runner.MODEL_NAME
    previous_token = inference_runner.HF_TOKEN
    previous_api_base = inference_runner.API_BASE_URL
    client: Optional[Any] = None

    started_at = time.perf_counter()
    try:
        inference_runner.MODEL_NAME = model_name
        inference_runner.HF_TOKEN = hf_token
        inference_runner.API_BASE_URL = api_base_url

        if model_name != "heuristic-baseline" and hf_token:
            client = inference_runner.OpenAI(
                base_url=api_base_url,
                api_key=hf_token,
                timeout=180.0,
            )

        task_results = [_run_single_task(task, client) for task in UI_TASKS]
    finally:
        if client is not None and hasattr(client, "close"):
            try:
                client.close()
            except Exception:
                pass
        inference_runner.MODEL_NAME = previous_model
        inference_runner.HF_TOKEN = previous_token
        inference_runner.API_BASE_URL = previous_api_base

    total_runtime_seconds = round(time.perf_counter() - started_at, 2)
    average_agent_score = round(
        sum(float(task["score"]) for task in task_results) / len(task_results), 4
    )
    min_score = min(float(task["score"]) for task in task_results)
    had_stderr = any(task["stderr"] for task in task_results)
    scene = _scene_for_run(average_agent_score, min_score, had_stderr)

    return {
        "model": model_name,
        "api_base_url": api_base_url,
        "total_runtime_seconds": total_runtime_seconds,
        "average_agent_score": average_agent_score,
        "average_random_baseline": AVERAGE_RANDOM_BASELINE,
        "average_improvement": round(
            average_agent_score - AVERAGE_RANDOM_BASELINE, 4
        ),
        "task_count": len(task_results),
        "tasks": task_results,
        "generated_at": time.strftime("%Y-%m-%d %H:%M:%S UTC", time.gmtime()),
        **scene,
    }