File size: 12,063 Bytes
43bbf9a
 
 
 
 
 
 
0896bc8
43bbf9a
 
 
 
 
 
 
46fe048
43bbf9a
 
 
 
 
 
 
149cb7a
43bbf9a
 
46fe048
 
149cb7a
 
43bbf9a
 
 
 
 
 
 
aef77be
43bbf9a
38a1e2b
46fe048
43bbf9a
 
 
 
46fe048
 
 
 
5e62f87
 
 
 
0896bc8
 
 
 
 
 
 
 
aef77be
 
 
 
 
 
 
43bbf9a
0896bc8
43bbf9a
 
 
 
0896bc8
 
43bbf9a
 
 
fe0c379
43bbf9a
0896bc8
fe0c379
43bbf9a
 
 
 
 
46fe048
43bbf9a
 
 
 
 
 
46fe048
 
 
43bbf9a
46fe048
 
43bbf9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6b41a8
43bbf9a
 
 
 
149cb7a
46fe048
 
5e62f87
 
149cb7a
 
 
 
 
46fe048
 
 
 
5e62f87
 
 
 
 
 
46fe048
 
 
 
 
149cb7a
 
46fe048
5e62f87
46fe048
 
43bbf9a
149cb7a
 
 
 
 
 
43bbf9a
 
 
 
 
 
 
 
149cb7a
43bbf9a
149cb7a
 
 
46fe048
43bbf9a
46fe048
43bbf9a
 
 
 
 
 
149cb7a
 
 
 
43bbf9a
 
 
 
 
 
 
 
 
 
46fe048
d6b41a8
 
5e62f87
 
46fe048
43bbf9a
 
 
 
149cb7a
43bbf9a
 
 
 
 
46fe048
 
 
 
 
 
 
 
 
 
 
43bbf9a
 
149cb7a
46fe048
149cb7a
43bbf9a
 
 
 
 
 
51e788e
43bbf9a
 
 
149cb7a
43bbf9a
149cb7a
 
 
43bbf9a
5e62f87
46fe048
 
43bbf9a
 
46fe048
 
43bbf9a
 
 
46fe048
 
 
43bbf9a
 
 
32efb70
aef77be
 
 
 
43bbf9a
149cb7a
aef77be
 
32efb70
149cb7a
 
 
fe0c379
43bbf9a
 
 
0896bc8
43bbf9a
0896bc8
 
43bbf9a
 
0896bc8
aef77be
43bbf9a
 
 
 
 
 
 
 
 
 
aef77be
 
 
 
 
 
0896bc8
 
 
aef77be
 
 
 
0896bc8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Baseline inference runner with strict structured logs."""

from __future__ import annotations

import asyncio
import json
import os
import signal
import subprocess
import sys
import time
from pathlib import Path
from typing import List, Optional, Tuple

import httpx
from openai import APIConnectionError, APIError, APITimeoutError, OpenAI, OpenAIError, RateLimitError

ROOT = Path(__file__).resolve().parent
if str(ROOT) not in sys.path:
    sys.path.insert(0, str(ROOT))

from client import ATCOptimizationEnv
from models import ATCOptimizationAction
from planner import build_heuristic_plan, build_refined_plan


API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1").rstrip("/")
MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
HF_TOKEN = os.getenv("HF_TOKEN", "").strip()

LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME", os.getenv("IMAGE_NAME", ""))
BENCHMARK = "atc_optimization_openenv"
TASK_IDS = [
    "delhi_monsoon_recovery_easy",
    "mumbai_bank_balance_medium",
    "bengaluru_irrops_hard",
]
SCORE_EPSILON = 0.01
SUCCESS_SCORE_THRESHOLD = 0.65
SCORE_EPSILON = 0.01
MAX_STEPS_CAP = int(os.getenv("MAX_STEPS_CAP", "4"))
MAX_TOKENS = 1400
TEMPERATURE = 0


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


def _step_budget(steps_remaining: int) -> int:
    return min(MAX_STEPS_CAP, max(1, int(steps_remaining)))


def _safe_print(msg: str) -> None:
    """Print to stdout, silently ignoring broken pipe errors."""
    try:
        print(msg, flush=True)
    except (BrokenPipeError, OSError):
        pass


def _safe_stderr(message: str) -> None:
    try:
        print(message, file=sys.stderr, flush=True)
    except (BrokenPipeError, OSError):
        pass


def log_start(task: str, env: str, model: str) -> None:
    _safe_print(f"[START] task={task} env={env} model={model}")


def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]) -> None:
    error_text = "null" if error is None else error.replace("\n", " ").strip()
    _safe_print(
        f"[STEP] step={step} action={action} reward={reward:.2f} done={_bool_token(done)} error={error_text}"
    )


def log_end(task: str, success: bool, steps: int, score: float, rewards: List[float]) -> None:
    rewards_text = ",".join(f"{item:.2f}" for item in rewards)
    _safe_print(
        f"[END] task={task} success={_bool_token(success)} steps={steps} score={score:.2f} rewards={rewards_text}"
    )


async def wait_for_server(base_url: str, timeout_s: float = 30.0) -> None:
    deadline = time.time() + timeout_s
    last_error: Optional[str] = None
    async with httpx.AsyncClient(timeout=3.0) as client:
        while time.time() < deadline:
            try:
                response = await client.get(f"{base_url}/health")
                if response.status_code == 200:
                    return
                last_error = f"HTTP {response.status_code}"
            except (httpx.RequestError, httpx.TimeoutException) as exc:
                last_error = f"{type(exc).__name__}: {exc}"
            await asyncio.sleep(0.5)
    suffix = f" (last_error={last_error})" if last_error else ""
    raise RuntimeError(f"Timed out waiting for server health at {base_url}{suffix}")


async def prepare_base_url() -> Tuple[str, Optional[subprocess.Popen]]:
    for env_name in ("ENV_BASE_URL", "SPACE_URL", "PING_URL"):
        if os.getenv(env_name):
            return os.environ[env_name].rstrip("/"), None

    port = int(os.getenv("LOCAL_PORT", "8000"))
    base_url = f"http://127.0.0.1:{port}"
    process = subprocess.Popen(
        [
            sys.executable,
            "-m",
            "uvicorn",
            "server.app:app",
            "--host",
            "127.0.0.1",
            "--port",
            str(port),
        ],
        cwd=str(ROOT),
        stdout=subprocess.DEVNULL,
        stderr=subprocess.DEVNULL,
    )
    try:
        await wait_for_server(base_url)
        return base_url, process
    except RuntimeError:
        process.terminate()
        raise


def build_seed_plan(observation, step: int):
    if not observation.flights:
        raise ValueError("observation contains no flights")
    if observation.steps_remaining < 0:
        raise ValueError("observation.steps_remaining cannot be negative")
    if step <= 1 or not observation.current_plan:
        return build_heuristic_plan(observation)
    return build_refined_plan(observation, seed_plan=list(observation.current_plan))


def _extract_json_object(response_text: str) -> dict:
    start = response_text.find("{")
    end = response_text.rfind("}")
    if start == -1 or end == -1:
        try:
            payload = json.loads(response_text)
        except json.JSONDecodeError as exc:
            raise ValueError(f"response is not JSON: {response_text[:120]}") from exc
    else:
        payload = json.loads(response_text[start : end + 1])
    if not isinstance(payload, dict):
        raise ValueError(f"response payload is not an object: {type(payload)}")
    return payload


def get_model_action(client: Optional[OpenAI], observation, task_id: str, step: int) -> ATCOptimizationAction:
    seed_plan = build_seed_plan(observation, step)
    seed_json = json.dumps([item.model_dump() for item in seed_plan], ensure_ascii=True)
    should_commit = step >= _step_budget(observation.steps_remaining)

    if client is None or not API_BASE_URL or not HF_TOKEN or MODEL_NAME == "heuristic-baseline":
        return ATCOptimizationAction(
            proposal=seed_plan,
            rationale=(
                "Deterministic multi-step baseline used because no model endpoint is configured. "
                f"Planning step {step} {'refines the prior plan' if step > 1 else 'establishes a safe initial schedule'}."
            ),
            commit=should_commit,
        )

    prompt = (
        "You are optimizing a disrupted ATC runway schedule.\n"
        "Return strict JSON only with keys rationale and proposal.\n"
        "proposal must be an array of objects with flight_id, runway, assigned_minute, hold_minutes.\n"
        "Do not omit any flight. Keep assignments conflict-free and priority aware.\n\n"
        f"Task: {task_id}\n"
        f"Planning step: {step}\n"
        f"Briefing:\n{observation.briefing}\n\n"
        f"Current metrics: {observation.current_metrics.model_dump_json()}\n"
        f"Diagnostics: {json.dumps(observation.diagnostics)}\n"
        f"Recommendations: {json.dumps(observation.recommendations)}\n"
        f"Seed candidate plan:\n{seed_json}\n"
    )

    try:
        completion = client.chat.completions.create(
            model=MODEL_NAME,
            messages=[
                {
                    "role": "system",
                    "content": (
                        "You are a conservative ATC planner. Output strict JSON only. "
                        "When this is not the final step, improve the plan but keep it open for one more evaluation."
                    ),
                },
                {"role": "user", "content": prompt},
            ],
            temperature=TEMPERATURE,
            max_tokens=MAX_TOKENS,
            stream=False,
        )
        text = (completion.choices[0].message.content or "").strip()
        if not text:
            raise ValueError("empty model response")
        payload = _extract_json_object(text)
        if "proposal" not in payload:
            raise ValueError("response missing 'proposal' field")
        if not isinstance(payload["proposal"], list):
            raise ValueError("response field 'proposal' must be a list")

        action = ATCOptimizationAction.model_validate(
            {
                "proposal": payload["proposal"],
                "rationale": payload.get("rationale", "Model-generated ATC plan."),
                "commit": should_commit,
            }
        )
        if len(action.proposal) < len(observation.flights):
            raise ValueError("model proposal omitted flights")
        return action
    except (
        APIConnectionError,
        APITimeoutError,
        APIError,
        RateLimitError,
        OpenAIError,
        json.JSONDecodeError,
        KeyError,
        TypeError,
        ValueError,
    ) as exc:
        print(f"Model request failed for {task_id}: {exc}", file=sys.stderr, flush=True)
        return ATCOptimizationAction(
            proposal=seed_plan,
            rationale="Fell back to deterministic planner after model failure.",
            commit=should_commit,
        )


async def run_task(client: Optional[OpenAI], base_url: str, task_id: str) -> float:
    rewards: List[float] = []
    steps_taken = 0
    score = SCORE_EPSILON
    success = False
    runtime_model = MODEL_NAME if client is not None else "heuristic-baseline"
    log_start(task=task_id, env=BENCHMARK, model=runtime_model)
    env: Optional[ATCOptimizationEnv] = None

    try:
        env = ATCOptimizationEnv(base_url=base_url)
        await env.__aenter__()
        result = await env.reset(task_id=task_id)
        max_steps = _step_budget(result.observation.steps_remaining)

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

        score = max(SCORE_EPSILON, min(1.0 - SCORE_EPSILON, result.observation.current_metrics.overall_score))
        score = max(
            SCORE_EPSILON,
            min(1.0 - SCORE_EPSILON, result.observation.current_metrics.overall_score),
        )
        success = score >= SUCCESS_SCORE_THRESHOLD
        return score
    except Exception as exc:
        _safe_stderr(f"Task execution failed for {task_id}: {exc}")
        return SCORE_EPSILON
    finally:
        if env is not None:
            await env.__aexit__(None, None, None)
        log_end(task=task_id, success=success, steps=steps_taken, score=score, rewards=rewards)


async def main() -> None:
    process: Optional[subprocess.Popen] = None
    try:
        client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN) if API_BASE_URL and HF_TOKEN else None
        base_url, process = await prepare_base_url()
        for task_id in TASK_IDS:
            await run_task(client, base_url, task_id)
    except Exception as exc:
        _safe_stderr(f"[FATAL] {type(exc).__name__}: {exc}")
    finally:
        if process is not None:
            process.terminate()
            try:
                process.wait(timeout=10)
            except subprocess.TimeoutExpired:
                process.kill()


if __name__ == "__main__":
    for stream in (sys.stdout, sys.stderr):
        if hasattr(stream, "reconfigure"):
            try:
                stream.reconfigure(encoding="utf-8", errors="backslashreplace")
            except Exception:
                pass
    # On Linux, reset SIGPIPE to default so broken-pipe produces a clean exit
    # instead of a Python exception.  On Windows this signal doesn't exist.
    if hasattr(signal, "SIGPIPE"):
        try:
            signal.signal(signal.SIGPIPE, signal.SIG_DFL)
        except Exception:
            pass
    try:
        asyncio.run(main())
    except (BrokenPipeError, OSError, KeyboardInterrupt):
        pass
    finally:
        # Silently close stdout/stderr to avoid secondary BrokenPipeError
        # during interpreter shutdown.
        try:
            sys.stdout.close()
        except (BrokenPipeError, OSError):
            pass
        try:
            sys.stderr.close()
        except (BrokenPipeError, OSError):
            pass