File size: 16,395 Bytes
852b5ea
 
 
 
2a92b3a
 
 
 
852b5ea
2a92b3a
852b5ea
 
 
 
 
90fc756
 
2a92b3a
90fc756
 
2a92b3a
 
852b5ea
90fc756
 
 
852b5ea
2a92b3a
852b5ea
 
2a92b3a
 
852b5ea
 
2a92b3a
 
 
 
 
90fc756
 
 
 
2a92b3a
 
 
90fc756
 
 
2a92b3a
 
 
90fc756
 
852b5ea
2a92b3a
852b5ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11aa990
852b5ea
 
90fc756
852b5ea
 
2a92b3a
852b5ea
90fc756
 
 
 
 
2a92b3a
90fc756
 
 
 
2a92b3a
90fc756
 
 
2a92b3a
 
852b5ea
2a92b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
852b5ea
 
 
 
 
 
2a92b3a
852b5ea
90fc756
852b5ea
2a92b3a
 
 
 
 
852b5ea
2a92b3a
 
852b5ea
 
 
2a92b3a
852b5ea
 
 
2a92b3a
 
852b5ea
 
 
 
 
 
 
 
 
2a92b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c28868
2a92b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11aa990
 
 
 
 
 
 
 
2a92b3a
 
 
 
 
 
 
 
 
 
 
 
 
852b5ea
 
2a92b3a
 
 
 
 
 
 
 
 
 
852b5ea
 
 
 
 
2a92b3a
 
 
 
 
 
 
 
852b5ea
2a92b3a
852b5ea
2a92b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c28868
852b5ea
 
 
 
2a92b3a
852b5ea
 
 
 
 
 
2a92b3a
852b5ea
90fc756
 
2a92b3a
 
852b5ea
2a92b3a
b83c8ad
 
 
 
 
 
2a92b3a
2c28868
2a92b3a
 
 
90fc756
b83c8ad
 
 
 
 
90fc756
852b5ea
b83c8ad
852b5ea
90fc756
 
2a92b3a
852b5ea
2a92b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90fc756
2a92b3a
2c28868
2a92b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90fc756
 
 
 
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
"""
Inference Script β€” SQL Query Reviewer
======================================
MANDATORY environment variables:
    API_BASE_URL       The API endpoint for the LLM.
    MODEL_NAME         The model identifier to use for inference.
    HF_TOKEN           Your Hugging Face / API key.
    LOCAL_IMAGE_NAME   The name of the local Docker image for the environment.

STDOUT FORMAT (must match exactly):
    [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> score=<score> rewards=<r1,r2,...,rn>
"""

from __future__ import annotations

import asyncio
import json
import os
import sys
import traceback
from typing import Any, List, Optional

from openai import OpenAI

# ---------------------------------------------------------------------------
# Environment variables β€” read ALL names the validator might set
# ---------------------------------------------------------------------------

IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME") or os.getenv("IMAGE_NAME")
API_KEY = os.getenv("HF_TOKEN") or os.getenv("API_KEY") 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")
ENV_BASE_URL = os.getenv("ENV_BASE_URL", "http://localhost:8000")

BENCHMARK = "sql-query-reviewer"
MAX_STEPS = 10
SUCCESS_SCORE_THRESHOLD = 0.1

SYSTEM_PROMPT = """You are reviewing a SQL query for correctness, performance, and security.
Return exactly one JSON object with these keys:
- action_type: identify_issue, suggest_fix, approve, or request_more_context
- issue_category: syntax, performance, security, logic, or style (when relevant)
- issue_description: concise issue statement (when relevant)
- suggested_fix: corrected SQL or corrected fragment (when relevant)
- confidence: float between 0.0 and 1.0

Guidelines:
- Prefer identify_issue until you believe all important issues are covered.
- Use approve only when the query looks acceptable or all issues have been identified.
- Return ONLY valid JSON, no prose, no markdown fences.
"""

# ---------------------------------------------------------------------------
# Structured stdout logging β€” matches hackathon spec EXACTLY
# ---------------------------------------------------------------------------


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


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


# ---------------------------------------------------------------------------
# LLM interaction β€” fully wrapped in try/except
# ---------------------------------------------------------------------------


def extract_json(content: str) -> dict[str, Any]:
    stripped = content.strip()
    if stripped.startswith("```"):
        lines = [l for l in stripped.splitlines() if not l.startswith("```")]
        stripped = "\n".join(lines).strip()
    start = stripped.find("{")
    end = stripped.rfind("}")
    if start == -1 or end == -1 or end <= start:
        raise ValueError(f"No JSON object found in: {content[:200]!r}")
    return json.loads(stripped[start : end + 1])


def choose_action(llm_client: Any, model_name: str, observation: Any) -> dict[str, Any]:
    """Ask LLM for an action. Returns a raw dict. NEVER raises."""
    try:
        obs_data = {
            "query": getattr(observation, "query", ""),
            "schema_info": getattr(observation, "schema_info", {}),
            "context": getattr(observation, "context", ""),
            "issues_found_so_far": [],
            "remaining_actions": getattr(observation, "remaining_actions", 0),
            "difficulty": getattr(observation, "difficulty", "unknown"),
            "feedback": getattr(observation, "feedback", ""),
        }

        # Safely serialize issues
        for i in getattr(observation, "issues_found_so_far", []):
            try:
                obs_data["issues_found_so_far"].append(
                    i.model_dump() if hasattr(i, "model_dump") else str(i)
                )
            except Exception:
                pass

        response = llm_client.chat.completions.create(
            model=model_name,
            temperature=0,
            max_tokens=300,
            messages=[
                {"role": "system", "content": SYSTEM_PROMPT},
                {"role": "user", "content": json.dumps(obs_data, indent=2)},
            ],
        )
        content = response.choices[0].message.content or ""
        parsed = extract_json(content)
        if "action_type" not in parsed:
            parsed["action_type"] = "approve"
        return parsed

    except Exception as exc:
        print(f"[DEBUG] choose_action error: {exc}", flush=True)
        return {"action_type": "approve", "confidence": 0.1}


# ---------------------------------------------------------------------------
# Episode runner (async) β€” for from_docker_image connection
# ---------------------------------------------------------------------------


async def run_episode_async(
    env: Any, llm_client: Any, model_name: str, task_id: str
) -> None:
    rewards: List[float] = []
    steps_taken = 0
    score = 0.0
    success = False

    log_start(task=task_id, env=BENCHMARK, model=model_name)

    try:
        from sql_query_reviewer.models import SQLReviewAction

        result = await env.reset()

        for step_num in range(1, MAX_STEPS + 1):
            if result.done:
                break

            action_dict = choose_action(llm_client, model_name, result.observation)

            try:
                action = SQLReviewAction.model_validate(action_dict)
            except Exception:
                action = SQLReviewAction(action_type="approve", confidence=0.1)

            action_str = action.action_type
            if action.issue_description:
                action_str = f"{action.action_type}({action.issue_category})"

            try:
                result = await env.step(action)
            except Exception as step_err:
                print(f"[DEBUG] env.step error: {step_err}", flush=True)
                log_step(step=step_num, action=action_str, reward=0.0, done=True, error=str(step_err))
                rewards.append(0.0)
                steps_taken = step_num
                break

            reward = result.reward if result.reward is not None else 0.0
            rewards.append(reward)
            steps_taken = step_num
            error_msg = None
            if hasattr(result, "info") and result.info:
                error_msg = result.info.get("error")

            log_step(step=step_num, action=action_str, reward=reward, done=result.done, error=error_msg)

            if result.done:
                break

        # Final score
        try:
            state = await env.state()
            if hasattr(state, "final_score") and state.final_score is not None:
                score = state.final_score
            else:
                score = sum(rewards) / max(len(rewards), 1)
        except Exception:
            score = sum(rewards) / max(len(rewards), 1) if rewards else 0.0

        score = max(0.01, min(0.99, score))
        success = score >= SUCCESS_SCORE_THRESHOLD

    except Exception as exc:
        print(f"[DEBUG] Episode error: {exc}", flush=True)
        traceback.print_exc(file=sys.stdout)

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


# ---------------------------------------------------------------------------
# Episode runner (sync) β€” for direct HTTP connection
# ---------------------------------------------------------------------------


def run_episode(env: Any, llm_client: Any, model_name: str, task_id: str) -> None:
    """Public episode runner expected by tests.

    Uses the synchronous env interface: reset(task_id=...), step(...), state().
    """
    return run_episode_sync(env, llm_client, model_name, task_id)


def run_episode_sync(
    env: Any, llm_client: Any, model_name: str, task_id: str
) -> None:
    rewards: List[float] = []
    steps_taken = 0
    score = 0.0
    success = False

    log_start(task=task_id, env=BENCHMARK, model=model_name)

    try:
        from sql_query_reviewer.models import SQLReviewAction

        result = env.reset(task_id=task_id)

        for step_num in range(1, MAX_STEPS + 1):
            if result.done:
                break

            action_dict = choose_action(llm_client, model_name, result.observation)

            try:
                action = SQLReviewAction.model_validate(action_dict)
            except Exception:
                action = SQLReviewAction(action_type="approve", confidence=0.1)

            action_str = action.action_type
            if action.issue_description:
                action_str = f"{action.action_type}({action.issue_category})"

            try:
                result = env.step(action)
            except Exception as step_err:
                print(f"[DEBUG] env.step error: {step_err}", flush=True)
                log_step(step=step_num, action=action_str, reward=0.0, done=True, error=str(step_err))
                rewards.append(0.0)
                steps_taken = step_num
                break

            reward = result.reward if result.reward is not None else 0.0
            rewards.append(reward)
            steps_taken = step_num
            error_msg = None
            if hasattr(result, "info") and result.info:
                error_msg = result.info.get("error")

            log_step(step=step_num, action=action_str, reward=reward, done=result.done, error=error_msg)

            if result.done:
                break

        try:
            state = env.state()
            if hasattr(state, "final_score") and state.final_score is not None:
                score = state.final_score
            else:
                score = sum(rewards) / max(len(rewards), 1)
        except Exception:
            score = sum(rewards) / max(len(rewards), 1) if rewards else 0.0

        score = max(0.01, min(0.99, score))
        success = score >= SUCCESS_SCORE_THRESHOLD

    except Exception as exc:
        print(f"[DEBUG] Episode error: {exc}", flush=True)
        traceback.print_exc(file=sys.stdout)

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


# ---------------------------------------------------------------------------
# Main β€” tries Docker image first (validator), then HTTP (local dev)
# ---------------------------------------------------------------------------


async def async_main() -> int:
    # Build LLM client (even without key, don't crash β€” emit logs and exit)
    if not API_KEY:
        print("[DEBUG] WARNING: No API key found (HF_TOKEN / API_KEY / OPENAI_API_KEY)", flush=True)
        _fallback_ids = [
            "easy_001", "easy_002", "easy_003", "easy_004", "easy_005", "easy_006", "easy_007",
            "medium_001", "medium_002", "medium_003", "medium_004", "medium_005", "medium_006", "medium_007",
            "hard_001", "hard_002", "hard_003", "hard_004", "hard_005", "hard_006",
        ]
        for tid in _fallback_ids:
            log_start(task=tid, env=BENCHMARK, model=MODEL_NAME)
            log_end(success=False, steps=0, score=0.01, rewards=[])
        return 1

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

    _default_ids = ",".join([
        "easy_001", "easy_002", "easy_003", "easy_004", "easy_005", "easy_006", "easy_007",
        "medium_001", "medium_002", "medium_003", "medium_004", "medium_005", "medium_006", "medium_007",
        "hard_001", "hard_002", "hard_003", "hard_004", "hard_005", "hard_006",
    ])
    task_ids = tuple(
        tid.strip()
        for tid in os.getenv("TASK_IDS", _default_ids).split(",")
        if tid.strip()
    )

    env = None

    try:
        # ------------------------------------------------------------------
        # Method 1: from_docker_image (what the hackathon validator uses)
        # ------------------------------------------------------------------
        if IMAGE_NAME:
            print(f"[DEBUG] Connecting via Docker image: {IMAGE_NAME}", flush=True)
            try:
                from sql_query_reviewer.client import SQLReviewEnv
                env = await SQLReviewEnv.from_docker_image(IMAGE_NAME)
                print("[DEBUG] Docker connection OK", flush=True)
                for task_id in task_ids:
                    await run_episode_async(env, llm_client, MODEL_NAME, task_id)
                return 0
            except AttributeError:
                # from_docker_image not implemented in our client β€” try openenv-core
                print("[DEBUG] from_docker_image not in custom client, trying openenv generic", flush=True)
                try:
                    from openenv.core.env_client import GenericEnvClient
                    env = await GenericEnvClient.from_docker_image(IMAGE_NAME)
                    print("[DEBUG] GenericEnvClient Docker connection OK", flush=True)
                    for task_id in task_ids:
                        await run_episode_async(env, llm_client, MODEL_NAME, task_id)
                    return 0
                except Exception as exc2:
                    print(f"[DEBUG] GenericEnvClient also failed: {exc2}", flush=True)
            except Exception as exc:
                print(f"[DEBUG] Docker connection failed: {exc}", flush=True)

        # ------------------------------------------------------------------
        # Method 2: Async HTTP (fallback for local/URL-based testing)
        # ------------------------------------------------------------------
        print(f"[DEBUG] Connecting via URL: {ENV_BASE_URL}", flush=True)
        try:
            from sql_query_reviewer.client import SQLReviewEnv
            env = SQLReviewEnv(base_url=ENV_BASE_URL)
            await env.__aenter__()
            print("[DEBUG] Async URL connection OK", flush=True)
            for task_id in task_ids:
                await run_episode_async(env, llm_client, MODEL_NAME, task_id)
            return 0
        except Exception as exc:
            print(f"[DEBUG] Async URL failed: {exc}", flush=True)

        # ------------------------------------------------------------------
        # Method 3: Sync HTTP (last resort)
        # ------------------------------------------------------------------
        try:
            from sql_query_reviewer.client import SyncSQLReviewEnv
            sync_env = SyncSQLReviewEnv(base_url=ENV_BASE_URL)
            sync_env.__enter__()
            print("[DEBUG] Sync HTTP connection OK", flush=True)
            for task_id in task_ids:
                run_episode_sync(sync_env, llm_client, MODEL_NAME, task_id)
            sync_env.close()
            return 0
        except Exception as exc:
            print(f"[DEBUG] Sync HTTP also failed: {exc}", flush=True)

        # All methods failed β€” still emit valid log lines
        print("[DEBUG] All connection methods exhausted", flush=True)
        for task_id in task_ids:
            log_start(task=task_id, env=BENCHMARK, model=MODEL_NAME)
            log_end(success=False, steps=0, score=0.01, rewards=[])
        return 1

    except Exception as exc:
        print(f"[DEBUG] Fatal: {exc}", flush=True)
        traceback.print_exc(file=sys.stdout)
        return 1

    finally:
        if env is not None:
            try:
                await env.close()
            except Exception as close_err:
                print(f"[DEBUG] env.close() error: {close_err}", flush=True)


def main() -> int:
    return asyncio.run(async_main())


if __name__ == "__main__":
    raise SystemExit(main())