File size: 21,158 Bytes
14b6f9b
 
 
 
 
 
 
 
 
 
 
 
 
 
961dad9
14b6f9b
 
 
 
 
50e4c08
 
 
6ed8536
 
 
 
 
3341e6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ed8536
 
 
 
 
 
 
 
 
 
 
 
 
961dad9
14b6f9b
6ed8536
4b48a6b
 
6ed8536
 
3341e6c
 
 
 
 
 
 
 
 
 
6ed8536
 
 
3341e6c
 
 
 
 
 
 
4b48a6b
557bac4
3341e6c
6ed8536
557bac4
4b48a6b
6ed8536
14b6f9b
961dad9
 
14b6f9b
 
 
 
 
 
 
 
 
 
 
 
961dad9
 
 
 
 
 
 
 
 
 
 
 
 
14b6f9b
 
 
 
 
 
 
 
 
 
 
 
 
961dad9
 
 
14b6f9b
 
 
961dad9
 
 
 
 
 
 
 
 
 
14b6f9b
 
 
 
961dad9
 
14b6f9b
 
 
 
 
 
 
 
 
 
 
961dad9
 
 
 
 
 
 
 
 
 
14b6f9b
 
 
 
961dad9
 
 
 
 
 
 
 
14b6f9b
961dad9
 
14b6f9b
 
961dad9
 
 
14b6f9b
 
6ed8536
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14b6f9b
961dad9
 
fcc7d82
961dad9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fcc7d82
961dad9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ed8536
961dad9
 
 
 
 
 
6ed8536
 
961dad9
 
 
 
 
 
 
d68f67f
961dad9
 
 
 
 
6ed8536
 
 
 
961dad9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d24e4fa
961dad9
 
 
 
 
 
14b6f9b
 
6ed8536
961dad9
 
14b6f9b
6ed8536
 
14b6f9b
961dad9
 
 
 
 
 
 
14b6f9b
961dad9
 
 
 
 
 
 
 
 
14b6f9b
961dad9
6ed8536
961dad9
 
 
 
 
14b6f9b
961dad9
 
14b6f9b
961dad9
 
 
14b6f9b
961dad9
 
 
 
14b6f9b
 
961dad9
9d46037
961dad9
557bac4
961dad9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d46037
961dad9
9d46037
961dad9
9d46037
961dad9
9d46037
961dad9
 
 
 
 
 
 
 
 
 
cb28453
50e4c08
cb28453
50e4c08
cb28453
d68f67f
50e4c08
557bac4
961dad9
 
14b6f9b
 
 
 
cb28453
 
14b6f9b
 
 
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#!/usr/bin/env python3

import os
import json
import logging
from typing import Dict, Any, Optional, List

try:
    from openai import OpenAI
except ImportError:
    print("Error: openai package not found. Install with: pip install openai")
    exit(1)

from env.environment import CodeGuardianEnv
from env.models import Action, AgentResponse, AgentState

# Setup logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

# Module-level environment variables
LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME")

DEFAULT_API_BASE_URL = "https://router.huggingface.co/v1"
DEFAULT_MODEL_NAME = "meta-llama/Meta-Llama-3-8B-Instruct"
HF_FALLBACK_MODEL = "mistralai/Mistral-7B-Instruct-v0.2"


def _hf_auth_tokens() -> Optional[str]:
    return os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACEHUB_API_TOKEN")


def _openai_auth_tokens() -> Optional[str]:
    return os.getenv("OPENAI_API_KEY") or os.getenv("API_KEY")


def _looks_like_huggingface_endpoint(url: str) -> bool:
    u = (url or "").lower()
    return any(x in u for x in ("huggingface", "hf.co", "router.huggingface"))


def _looks_like_openai_endpoint(url: str) -> bool:
    return "openai.com" in (url or "").lower()


def _looks_like_hf_hub_model(model: str) -> bool:
    """Hugging Face router / Inference typically uses org/model ids."""
    return "/" in (model or "").strip()


def _looks_like_openai_model(model: str) -> bool:
    m = (model or "").strip().lower()
    if not m or "/" in m:
        return False
    prefixes = (
        "gpt-",
        "o1",
        "o3",
        "o4",
        "ft:",
        "davinci",
        "text-embedding",
        "chatgpt-",
    )
    return any(m.startswith(p) for p in prefixes)


def _resolve_api_key(api_base_url: str, model_name: str) -> str:
    """
    Pick API key for an OpenAI-compatible client: Hugging Face token and/or OpenAI key,
    ordered from URL + model hints, with 'or' fallbacks so either stack can work.
    """
    hf_key = _hf_auth_tokens()
    oai_key = _openai_auth_tokens()

    url_hf = _looks_like_huggingface_endpoint(api_base_url)
    url_oai = _looks_like_openai_endpoint(api_base_url)
    model_hf = _looks_like_hf_hub_model(model_name)
    model_oai = _looks_like_openai_model(model_name)

    prefer_hf = url_hf or model_hf
    prefer_oai = url_oai or model_oai

    key: Optional[str] = None
    if prefer_hf and not prefer_oai:
        key = hf_key or oai_key
    elif prefer_oai and not prefer_hf:
        key = oai_key or hf_key
    elif prefer_hf and prefer_oai:
        if url_hf and not url_oai:
            key = hf_key or oai_key
        elif url_oai and not url_hf:
            key = oai_key or hf_key
        else:
            key = hf_key or oai_key
    else:
        key = hf_key or oai_key

    if not key:
        raise ValueError(
            "Set authentication for your provider: "
            "(HF_TOKEN or HUGGINGFACEHUB_API_TOKEN) for Hugging Face, "
            "or (OPENAI_API_KEY or API_KEY) for OpenAI"
        )
    return key


def _fallback_llm_json_string(reason: str = "LLM unavailable") -> str:
    return json.dumps(
        {
            "issues_detected": [],
            "risk_level": "low",
            "fix": "",
            "confidence": 0.1,
            "reasoning": reason,
            "reward_breakdown": {"detect": 0.0, "risk": 0.0, "fix": 0.0},
        }
    )


def setup_openai_client() -> tuple[OpenAI, str]:
    """Initialize OpenAI client from environment variables."""

    print("STEP: Initializing OpenAI client")

    api_base_url = os.getenv("API_BASE_URL", DEFAULT_API_BASE_URL)
    model_name = os.getenv("MODEL_NAME", DEFAULT_MODEL_NAME)
    api_key = _resolve_api_key(api_base_url, model_name)

    hf_key = _hf_auth_tokens()
    oai_key = _openai_auth_tokens()
    if api_key == hf_key and hf_key:
        auth_label = "Hugging Face token (HF_TOKEN / HUGGINGFACEHUB_API_TOKEN)"
    elif api_key == oai_key and oai_key:
        auth_label = "OpenAI key (OPENAI_API_KEY / API_KEY)"
    else:
        auth_label = "resolved API key"

    print(f"DEBUG: API_BASE_URL={api_base_url}")
    print(f"DEBUG: MODEL_NAME={model_name}")
    print(f"DEBUG: auth={auth_label}")
    logger.info(
        "OpenAI-compatible client: API_BASE_URL=%s MODEL_NAME=%s auth=%s",
        api_base_url,
        model_name,
        auth_label,
    )

    client = OpenAI(
        api_key=api_key,
        base_url=api_base_url,
    )

    return client, model_name

def create_adaptive_prompt(observation: Dict[str, Any], step_type: str, agent_state: AgentState) -> str:
    """Create adaptive prompt based on agent learning state."""
    step_names = {
        'detect': 'Issue Detection',
        'risk': 'Risk Assessment', 
        'fix': 'Fix Suggestion'
    }
    
    step_instructions = {
        'detect': 'list all issues you find in the code',
        'risk': 'output the risk level (low/medium/high)',
        'fix': 'provide corrected code that fixes the identified issues'
    }
    
    # Adaptive prompt based on learning
    learning_context = ""
    if agent_state.past_failures:
        learning_context = f"\nLEARNING FROM PAST MISTAKES:\n{chr(10).join(f'- Avoid: {failure}' for failure in agent_state.past_failures[-3:])}"
    
    if agent_state.prompt_strategy == "detailed":
        instruction_detail = " Be thorough and provide detailed reasoning."
    elif agent_state.prompt_strategy == "focused":
        instruction_detail = " Focus on most critical issues first."
    else:
        instruction_detail = ""
    
    prompt = f"""You are a professional code reviewer learning from feedback.

Code:
{observation.code}

Intent:
{observation.intent}

Constraints:
{chr(10).join(f"- {constraint}" for constraint in observation.constraints)}

Step: {step_names[step_type]}

Instructions:
* {step_instructions[step_type]}{instruction_detail}
* Provide confidence score (0.0-1.0)
* Include your reasoning{learning_context}

Respond ONLY in JSON format:
{{
  "issues_detected": [...],
  "risk_level": "...",
  "fix": "...",
  "confidence": 0.0,
  "reasoning": "...",
  "reward_breakdown": {{
    "detect": 0.0,
    "risk": 0.0,
    "fix": 0.0
  }}
}}"""
    
    return prompt

def parse_llm_response(response_text: str) -> AgentResponse:
    """Parse LLM response into structured AgentResponse with fallback."""
    try:
        # Clean up response text
        response_text = response_text.strip()
        if response_text.startswith('```json'):
            response_text = response_text[7:]
        if response_text.endswith('```'):
            response_text = response_text[:-3]
        response_text = response_text.strip()
        
        parsed = json.loads(response_text)
        
        # Ensure all required fields exist with defaults
        return AgentResponse(
            issues_detected=parsed.get('issues_detected', []),
            risk_level=parsed.get('risk_level', 'low'),
            fix=parsed.get('fix', ''),
            confidence=max(0.0, min(1.0, parsed.get('confidence', 0.5))),
            reasoning=parsed.get('reasoning', ''),
            reward_breakdown=parsed.get('reward_breakdown', {})
        )
    except (json.JSONDecodeError, AttributeError, ValueError) as e:
        logger.warning(f"Failed to parse LLM response: {e}")
        logger.warning(f"Raw response: {response_text[:200]}...")
        
        # Return fallback response
        return AgentResponse(
            issues_detected=[],
            risk_level='low',
            fix='',
            confidence=0.1,
            reasoning='Failed to parse response',
            reward_breakdown={}
        )

def create_action_from_response(step_type: str, response: AgentResponse) -> Action:
    """Create Action model from structured AgentResponse."""
    return Action(
        action_type=step_type,
        detected_issues=response.issues_detected if step_type == 'detect' else None,
        risk_level=response.risk_level if step_type == 'risk' else None,
        suggested_fix=response.fix if step_type == 'fix' else None
    )

def _is_model_missing_error(exc: BaseException) -> bool:
    msg = str(exc).lower()
    if "model_not_found" in msg or "model not found" in msg:
        return True
    body = getattr(exc, "body", None)
    if isinstance(body, str) and "model_not_found" in body.lower():
        return True
    if isinstance(body, dict):
        err = body.get("error")
        if isinstance(err, dict) and err.get("code") == "model_not_found":
            return True
    return False


def run_llm_inference(client: OpenAI, model_name: str, prompt: str) -> str:
    """Run LLM inference; never raises — returns JSON text or a safe fallback."""
    models_to_try: List[str] = [model_name]
    if HF_FALLBACK_MODEL and HF_FALLBACK_MODEL not in models_to_try:
        models_to_try.append(HF_FALLBACK_MODEL)

    last_error: Optional[BaseException] = None
    for m in models_to_try:
        try:
            response = client.chat.completions.create(
                model=m,
                messages=[
                    {
                        "role": "system",
                        "content": "You are a professional code reviewer. Always respond in valid JSON format with confidence and reasoning.",
                    },
                    {"role": "user", "content": prompt},
                ],
                temperature=0,
                max_tokens=500,
            )
            content = response.choices[0].message.content
            if content is None or not str(content).strip():
                logger.error("LLM returned empty content for model=%s", m)
                last_error = ValueError("empty LLM content")
                continue
            if m != model_name:
                logger.info("LLM succeeded using fallback model=%s (primary=%s failed)", m, model_name)
            return str(content)
        except Exception as e:
            last_error = e
            logger.error(
                "LLM inference failed for model=%s: %s",
                m,
                e,
                exc_info=not _is_model_missing_error(e),
            )
            if _is_model_missing_error(e):
                logger.error("model_not_found for model=%s; will try next model if available", m)
            continue

    logger.error("All LLM models exhausted; last_error=%s", last_error)
    return _fallback_llm_json_string("LLM call failed — using fallback response")

def run_baseline(env: CodeGuardianEnv) -> float:
    """Run a simple non-learning baseline agent for comparison."""
    print("STEP: Running baseline agent")
    
    obs = env.reset()
    total_reward = 0.0
    step_count = 0
    
    while not env.done and step_count < 50:
        current_step = env.state()['current_step']
        step_types = ['detect', 'risk', 'fix']
        step_type = step_types[current_step]
        
        # Baseline always outputs minimal responses
        if step_type == 'detect':
            action = Action(action_type='detect', detected_issues=[])
        elif step_type == 'risk':
            action = Action(action_type='risk', risk_level='low')
        else:
            action = Action(action_type='fix', suggested_fix='')
        
        next_obs, reward, done = env.step(action)
        total_reward += reward.value
        step_count += 1
        obs = next_obs
        
        logger.info(f"[BASELINE STEP] {step_count}: {step_type} -> reward={reward.value:+.3f}")
    
    print(f"STEP: Baseline complete - total_reward={total_reward:.3f} steps={step_count}")
    return total_reward

def update_agent_state(agent_state: AgentState, episode_reward: float, feedback: str) -> AgentState:
    """Update agent state based on episode performance for policy improvement."""
    agent_state.episode += 1
    agent_state.total_reward = episode_reward
    agent_state.improvement_history.append(episode_reward)
    
    # Update best score
    if episode_reward > agent_state.best_score:
        agent_state.best_score = episode_reward
        logger.info(f"New best score achieved: {episode_reward:.3f}")
    
    # STRONGER POLICY UPDATE LOGIC
    # Treat poor performance as failure
    if episode_reward < agent_state.best_score * 0.8:
        agent_state.past_failures.append(f"Episode {agent_state.episode}: reward={episode_reward:.3f}")
        # Keep only recent failures
        agent_state.past_failures = agent_state.past_failures[-5:]
        logger.info(f"Failure recorded - Episode reward: {episode_reward:.3f} vs Best: {agent_state.best_score:.3f}")
    
    # IMPROVED STRATEGY SWITCHING
    if episode_reward < agent_state.best_score * 0.7:  # Poor performance threshold
        old_strategy = agent_state.prompt_strategy
        
        if agent_state.prompt_strategy == "default":
            agent_state.prompt_strategy = "detailed"
            logger.info("Switching strategy: default → detailed")
        elif agent_state.prompt_strategy == "detailed":
            agent_state.prompt_strategy = "focused"
            logger.info("Switching strategy: detailed → focused")
        else:
            # Reset to default if focused still fails
            agent_state.prompt_strategy = "default"
            logger.info("Switching strategy: focused → default (reset)")
        
        if old_strategy != agent_state.prompt_strategy:
            logger.info(f"Strategy change triggered by low performance: {episode_reward:.3f} < {agent_state.best_score * 0.7:.3f}")
    
    return agent_state

def run_episode(env: CodeGuardianEnv, client: OpenAI, model_name: str, agent_state: AgentState) -> tuple[float, List[Dict]]:
    """Run a single episode and return total reward with episode history."""
    obs = env.reset()
    episode_reward = 0.0
    episode_history = []
    step_count = 0
    
    print(f"STEP: Episode {agent_state.episode + 1} started - model={model_name} task_id={obs.task_id} difficulty={obs.difficulty}")
    logger.info(f"[EPISODE START] Episode {agent_state.episode + 1} - model={model_name} task_id={obs.task_id} difficulty={obs.difficulty}")
    
    while not env.done:
        current_step = env.state()['current_step']
        step_types = ['detect', 'risk', 'fix']
        step_type = step_types[current_step]
        
        step_count += 1
        print(f"STEP: {step_type} - task_id={obs.task_id}")
        logger.info(f"[STEP] step_number={step_count} step_type={step_type} task_id={obs.task_id}")
        
        # Create adaptive prompt
        prompt = create_adaptive_prompt(obs, step_type, agent_state)
        
        # Run LLM inference (never raises)
        response_text = run_llm_inference(client, model_name, prompt)
        logger.info("LLM response (truncated): %s...", response_text[:100])
        agent_response = parse_llm_response(response_text)
        
        # Create action from response
        action = create_action_from_response(step_type, agent_response)
        
        # Execute step
        next_obs, reward, done = env.step(action)
        
        # CONFIDENCE-BASED REWARD ADJUSTMENT
        confidence_penalty = (1 - agent_response.confidence) * 0.1
        adjusted_reward = reward.value - confidence_penalty
        episode_reward += adjusted_reward
        
        logger.info(f"confidence_penalty={confidence_penalty:.3f} adjusted_reward={adjusted_reward:+.3f} confidence={agent_response.confidence:.2f}")
        
        # Log step results
        step_info = {
            "step": step_count,
            "type": step_type,
            "action": action.dict(),
            "response": agent_response.dict(),
            "reward": reward.dict(),
            "adjusted_reward": adjusted_reward,
            "confidence_penalty": confidence_penalty,
            "total_reward": episode_reward
        }
        episode_history.append(step_info)
        
        logger.info(f"action_type={action.action_type} reward={reward.value:+.3f} adjusted_reward={adjusted_reward:+.3f} feedback={reward.feedback} total_reward={episode_reward:+.3f} current_step={env.state()['current_step']} current_task={env.state()['current_task']}")
        
        # Update observation for next step
        obs = next_obs
        
        # Safety check
        if step_count > 50:  # Prevent infinite loops
            logger.warning("Maximum steps reached, stopping episode")
            break
    
    # Final episode log
    print(f"STEP: Episode {agent_state.episode + 1} complete - final_score={episode_reward:.3f} total_steps={step_count} strategy={agent_state.prompt_strategy}")
    logger.info(f"[EPISODE END] Episode {agent_state.episode + 1} - final_score={episode_reward:.3f} total_steps={step_count} strategy={agent_state.prompt_strategy}")
    
    return episode_reward, episode_history

def train_agent(num_episodes: int = 10) -> AgentState:
    """Train agent over multiple episodes with policy improvement."""
    try:
        # Setup
        client, model_name = setup_openai_client()
        agent_state = AgentState()
        all_episodes = []
        
        print(f"STEP: RL training started - episodes={num_episodes} model={model_name}")
        logger.info(f"[TRAINING START] Starting RL training for {num_episodes} episodes with model: {model_name}")
        
        # RUN BASELINE FIRST
        logger.info("\n{'='*60}")
        logger.info("BASELINE EVALUATION")
        logger.info(f"{'='*60}")
        baseline_env = CodeGuardianEnv()
        baseline_score = run_baseline(baseline_env)
        logger.info(f"Baseline score: {baseline_score:.3f}")
        
        # TRAINING LOOP
        logger.info(f"\n{'='*60}")
        logger.info("REINFORCEMENT LEARNING")
        logger.info(f"{'='*60}")
        
        for episode in range(num_episodes):
            logger.info(f"\n{'='*50}")
            logger.info(f"EPISODE {episode + 1}/{num_episodes}")
            logger.info(f"{'='*50}")
            
            env = CodeGuardianEnv()
            episode_reward, episode_history = run_episode(env, client, model_name, agent_state)
            all_episodes.append({
                "episode": episode + 1,
                "reward": episode_reward,
                "history": episode_history
            })
            
            # Update agent state for policy improvement
            agent_state = update_agent_state(agent_state, episode_reward, f"Episode {episode + 1} completed")
            
            logger.info(f"Episode {episode + 1} completed with reward: {episode_reward:.3f}")
            logger.info(f"Best score so far: {agent_state.best_score:.3f}")
            logger.info(f"Current strategy: {agent_state.prompt_strategy}")
            
            # EARLY STOPPING
            if episode > 3 and agent_state.best_score > 2.5:
                logger.info("Early stopping triggered: high performance achieved")
                logger.info(f"Stopping at episode {episode + 1} with best score {agent_state.best_score:.3f}")
                break
        
        # PERFORMANCE COMPARISON
        print("STEP: Training summary analysis")
        logger.info(f"\n{'='*60}")
        logger.info(f"[TRAINING SUMMARY]")
        logger.info(f"{'='*60}")
        
        # Calculate improvement over baseline
        improvement = ((agent_state.best_score - baseline_score) / (abs(baseline_score) + 1e-6)) * 100
        
        # Log key metrics
        logger.info(f"RL Agent Best Score: {agent_state.best_score:.3f}")
        logger.info(f"Baseline Score: {baseline_score:.3f}")
        logger.info(f"Performance Improvement: {improvement:.2f}%")
        
        # Additional metrics
        if all_episodes:
            avg_reward = sum(ep["reward"] for ep in all_episodes) / len(all_episodes)
            logger.info(f"Average Episode Reward: {avg_reward:.3f}")
            logger.info(f"Episodes Completed: {len(all_episodes)}")
            logger.info(f"Final Strategy: {agent_state.prompt_strategy}")
            
            # Performance classification
            if improvement > 50:
                print("STEP: EXCELLENT - significant improvement over baseline")
            elif improvement > 20:
                print("STEP: GOOD - measurable improvement achieved")
            elif improvement > 0:
                print("STEP: POSITIVE - some improvement over baseline")
            else:
                print("STEP: NO IMPROVEMENT - RL agent not better than baseline")
        
        return agent_state
        
    except Exception as e:
        logger.error(f"Training failed: {e}")
        raise

def main():
    """Main inference loop with RL training."""
    try:
        print("START")
        
        LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME")
        if LOCAL_IMAGE_NAME:
            print(f"STEP: inference using Docker image {LOCAL_IMAGE_NAME}")
        else:
            print("STEP: Starting inference")
        
        agent_state = train_agent(num_episodes=10)
        return 0
        
    except Exception as e:
        logger.error(f"Inference failed: {e}")
        return 1
    finally:
        print("END")

if __name__ == "__main__":
    exit(main())