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
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
# training.py – Memory‑safe: Phi‑3‑mini + Expert Demos + Fast PPO (2 iterations)
import os
os.environ["TRITON_DISABLE"] = "1"
os.environ["TOKENIZERS_PARALLELISM"] = "false"        # Issue #12: prevent OOM from parallel tokenization

import torch._dynamo
torch._dynamo.config.disable = True
import json
import torch
import torch.nn.functional as F
from torch.optim import AdamW
from dataclasses import dataclass
from typing import List, Dict, Tuple, Optional
import numpy as np
import re
import random
import matplotlib.pyplot as plt

from unsloth import FastLanguageModel
from transformers import TrainingArguments
from trl import SFTTrainer
from datasets import Dataset

from environment import CodeReviewEnv
from redteam import BUG_DB
from models import (
    RunTests, RunLinter, Inspect,
    ProposeFix, WriteComment, AskQuestion,
    Done, Skip, QueryDocs, map_to_env as model_map_to_env
)

# ======================================================================
@dataclass
class AgentAction:
    action_type: str
    content: Optional[str] = None

def parse_action(output: str) -> AgentAction:
    try:
        data = json.loads(output)
        return AgentAction(
            action_type=data.get("action_type", "").lower(),
            content=data.get("content")
        )
    except:
        pass
    json_match = re.search(r'```(?:json)?\s*(\{.*?\})\s*```', output, re.DOTALL)
    if json_match:
        try:
            data = json.loads(json_match.group(1))
            return AgentAction(
                action_type=data.get("action_type", "").lower(),
                content=data.get("content")
            )
        except:
            pass
    action_pattern = r'"action_type"\s*:\s*"(\w+)"'
    match = re.search(action_pattern, output)
    if match:
        return AgentAction(action_type=match.group(1).lower())
    output_lower = output.lower()
    if "test" in output_lower:
        return AgentAction("run_tests")
    if "lint" in output_lower:
        return AgentAction("run_linter")
    if "inspect" in output_lower:
        return AgentAction("inspect")
    if "doc" in output_lower or "documentation" in output_lower:
        return AgentAction("query_docs", "bug fix guidance")
    return AgentAction("invalid", output)

def map_to_env(action: AgentAction):
    return model_map_to_env(action.action_type, action.content)

# ======================================================================
def load_model():
    model, tokenizer = FastLanguageModel.from_pretrained(
        model_name="unsloth/Phi-3-mini-4k-instruct-bnb-4bit",
        max_seq_length=480,               # smaller window for memory
        load_in_4bit=True,
    )
    model = FastLanguageModel.get_peft_model(
        model,
        r=16,
        target_modules=[
            "q_proj", "k_proj", "v_proj", "o_proj",
            "gate_proj", "up_proj", "down_proj"
        ],
        lora_alpha=32,
        lora_dropout=0.0,
    )
    return model, tokenizer

def test_model_sanity(model, tokenizer) -> bool:
    print("\n" + "="*60)
    print("SANITY CHECK: Testing base model generation")
    print("="*60)
    test_prompt = "Hello, how are you?"
    messages = [{"role": "user", "content": test_prompt}]
    formatted = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
    inputs = tokenizer(formatted, return_tensors="pt", max_length=256, truncation=True).to("cuda")
    with torch.no_grad():
        outputs = model.generate(
            **inputs,
            max_new_tokens=30,
            do_sample=True,
            temperature=0.7,
            min_new_tokens=1,
            eos_token_id=tokenizer.eos_token_id,
            pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id,
        )
    generated_ids = outputs[0][inputs['input_ids'].shape[1]:]
    response = tokenizer.decode(generated_ids, skip_special_tokens=True).strip()
    print(f"Prompt: {test_prompt}")
    print(f"Response: {repr(response)}")
    if len(response) == 0:
        print("❌ Model produces empty output – cannot train.")
        return False
    print("βœ“ Model sanity check PASSED\n")
    return True

# ======================================================================
def _expert_fix_from_context(obs) -> str:
    """

    Build a conservative fix template named `fix` (required by tests).

    Uses bug hints + code snippet patterns to create realistic fixes.

    """
    bug = (getattr(obs, "bug_description", "") or "").lower()
    code = getattr(obs, "code_snippet", "") or ""

    if "division" in bug or "average" in code.lower():
        return (
            "def fix(data):\n"
            "    if not data:\n"
            "        return 0\n"
            "    return sum(data) / len(data)"
        )

    if "operator" in bug or "sign" in bug:
        return (
            "def fix(a, b):\n"
            "    return a + b"
        )

    if "off_by_one" in bug or "loop" in bug:
        return (
            "def fix(items):\n"
            "    return len(items)"
        )

    if "null" in bug or "key" in bug or "dict" in code.lower():
        return (
            "def fix(payload):\n"
            "    users = payload.get('users', {})\n"
            "    user_id = payload.get('id')\n"
            "    return users.get(user_id)"
        )

    # Concurrency-heavy tasks (harder/hardest).
    if "race" in bug or "missing_lock" in bug or "thread_safe" in bug or "global_nonatomic" in bug:
        return (
            "import threading\n"
            "_lock = threading.Lock()\n"
            "\n"
            "def fix(counter):\n"
            "    with _lock:\n"
            "        if counter is None:\n"
            "            return 0\n"
            "        return counter + 1"
        )

    if "deadlock" in bug or "double_lock" in bug or "lock order" in bug or "nested_lock" in bug:
        return (
            "import threading\n"
            "_lock_a = threading.Lock()\n"
            "_lock_b = threading.Lock()\n"
            "\n"
            "def fix(work):\n"
            "    first, second = (_lock_a, _lock_b)\n"
            "    if id(first) > id(second):\n"
            "        first, second = second, first\n"
            "    with first:\n"
            "        with second:\n"
            "            return work() if callable(work) else work"
        )

    if "fork_join" in bug or "join" in bug:
        return (
            "import threading\n"
            "\n"
            "def fix(worker):\n"
            "    t = threading.Thread(target=worker)\n"
            "    t.start()\n"
            "    t.join()\n"
            "    return True"
        )

    # Generic safe fallback keeps the RL pipeline alive for unknown bugs.
    return (
        "def fix(data):\n"
        "    if data is None:\n"
        "        return None\n"
        "    return data"
    )


def _expert_supervised_policy(obs) -> str:
    """

    Real workflow policy:

    inspect -> tests/linter -> docs -> fix -> negotiate -> done.

    """
    author_msg = (getattr(obs, "author_response", "") or "").lower()
    tool_output = (getattr(obs, "last_tool_output", "") or "").lower()

    if not getattr(obs, "tests_run", False):
        if "inspect" not in tool_output:
            return '{"action_type": "inspect"}'
        return '{"action_type": "run_tests"}'

    if not getattr(obs, "linter_run", False):
        return '{"action_type": "run_linter"}'

    if not getattr(obs, "docs_queried", False):
        return '{"action_type": "query_docs", "content": "python bug fixing best practices for edge cases and null safety"}'

    # Use docs again on hard tasks when evidence is still weak.
    if getattr(obs, "current_test_score", 0.0) < 0.6 and getattr(obs, "step", 0) >= 3:
        bug_hint = (getattr(obs, "bug_description", "") or "concurrency bug").replace('"', "'")
        return json.dumps(
            {
                "action_type": "query_docs",
                "content": f"python {bug_hint} lock ordering race condition mitigation patterns",
            }
        )

    # If test quality is poor, propose a concrete fix.
    if getattr(obs, "current_test_score", 0.0) < 0.95:
        fix_code = _expert_fix_from_context(obs)
        return json.dumps({"action_type": "fix", "content": fix_code})

    # If author is still unconvinced, provide causal explanation.
    if author_msg and ("not convinced" in author_msg or "explain" in author_msg or "brief" in author_msg):
        return (
            '{"action_type": "comment", "content": "This fix works because it handles the failing edge case directly, '
            'keeps behavior deterministic, and aligns with the observed test and lint feedback. '
            'The change is intentionally small to reduce regression risk."}'
        )

    # If negotiation is strong enough and quality is good, terminate.
    conf = float(getattr(obs, "author_confidence", 0.0))
    threshold = float(getattr(obs, "author_threshold", 0.5))
    score = float(getattr(obs, "current_test_score", 0.0))
    if conf >= threshold and score >= 0.8:
        return '{"action_type": "done"}'

    # Nudge conversation forward when tests are okay but acceptance is pending.
    return (
        '{"action_type": "question", "content": "Would you like a quick walkthrough of a failing scenario, the root cause, and how the fix prevents regressions?"}'
    )

# ======================================================================
def supervised_warmup(model, tokenizer, env, n_episodes=16, epochs=1, max_steps=8):
    print("\n" + "="*60)
    print("SUPERVISED WARM-UP: Real environment demonstrations")
    print("="*60)

    examples = []
    tasks = ["easy", "medium", "hard", "harder", "hardest"]
    for ep in range(n_episodes):
        task = random.choice(tasks)
        env.set_task(task)
        obs = env.reset()
        history = []
        done = False

        steps = 0
        while not done and steps < max_steps:
            prompt = build_prompt(obs, history)
            action_text = _expert_supervised_policy(obs)
            action = parse_action(action_text)
            env_action = map_to_env(action)
            next_obs, _, done, _ = env.step(env_action)

            messages = [
                {"role": "user", "content": prompt},
                {"role": "assistant", "content": action_text},
            ]
            full_text = tokenizer.apply_chat_template(messages, tokenize=False)
            examples.append({"text": full_text})

            history.append(f"Agent: {action_text}")
            history.append(f"Env: {next_obs.last_tool_output}")
            history = history[-8:]
            obs = next_obs
            steps += 1

        print(f"Supervised episode {ep+1}: task={task}, steps={steps}, done={done}")

    if not examples:
        print("No supervised examples generated; skipping warm-up.")
        return

    dataset = Dataset.from_list(examples)
    trainer = SFTTrainer(
        model=model,
        tokenizer=tokenizer,
        train_dataset=dataset,
        dataset_text_field="text",
        max_seq_length=480,
        args=TrainingArguments(
            output_dir="warmup_output",
            num_train_epochs=epochs,
            per_device_train_batch_size=2,
            gradient_accumulation_steps=2,
            learning_rate=2e-5,
            logging_steps=50,
            save_strategy="no",
            bf16=True,
        ),
    )
    print(f"Training on {len(examples)} real env examples for {epochs} epochs...")
    trainer.train()
    print("βœ“ Supervised warm-up (real env) complete\n")
    torch.cuda.empty_cache()

# ======================================================================
def generate_action_with_logprob(prompt, model, tokenizer, temperature=0.0, max_retries=2):
    messages = [{"role": "user", "content": prompt}]
    formatted = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
    inputs = tokenizer(formatted, return_tensors="pt", max_length=480, truncation=True).to("cuda")
    
    for attempt in range(max_retries):
        with torch.no_grad():
            outputs = model.generate(
                **inputs,
                max_new_tokens=64,
                do_sample=(temperature > 0),
                temperature=max(temperature, 0.01) if temperature > 0 else 1.0,
                min_new_tokens=1,
                return_dict_in_generate=True,
                output_scores=True,
            )
        generated_ids = outputs.sequences[0][inputs['input_ids'].shape[1]:]
        action_text = tokenizer.decode(generated_ids, skip_special_tokens=True).strip()
        
        logprobs = []
        for idx, token_id in enumerate(generated_ids):
            if idx < len(outputs.scores):
                token_logits = outputs.scores[idx][0]
                token_logprob = F.log_softmax(token_logits, dim=-1)[token_id].item()
                logprobs.append(token_logprob)
        total_logprob = sum(logprobs) if logprobs else -100.0
        
        if not action_text:
            fallback_actions = [
                '{"action_type": "run_tests"}',
                '{"action_type": "run_linter"}',
                '{"action_type": "inspect"}',
                '{"action_type": "skip"}',
            ]
            action_text = random.choice(fallback_actions)
            total_logprob = -50.0
            print(f"[WARN] Empty generation β†’ using fallback: {action_text}")
            return action_text, total_logprob
        
        try:
            json.loads(action_text)
            return action_text, total_logprob
        except:
            if attempt == max_retries - 1:
                return '{"action_type":"skip"}', -100.0
            continue
    return '{"action_type":"skip"}', -100.0

# ======================================================================
def build_prompt(obs, history_lines: List[str]) -> str:
    author_msg = getattr(obs, "author_response", "") or ""
    tool_output = getattr(obs, "last_tool_output", "") or ""
    author_personality = getattr(obs, "author_personality", "defensive")
    
    prompt = f"""You are an AI code review agent. Your goal is to convince a simulated human developer to accept your proposed fix and name your proposed fix function fix.



The developer has a **{author_personality}** personality and will only accept if you provide solid evidence:

- Tests pass (high pass ratio)

- Lint is clean (zero errors)

- Documentation or references are provided

- Your reasoning is clear, uses words like "because" or "therefore", and is detailed (over 30 words if needed)



Workflow:

1. Use `inspect` to understand the code.

2. Use `run_tests` and `run_linter` to gather evidence.

3. Use `query_docs` when you need references or language-specific guidance.

4. Propose a fix (`fix`) and explain why it works (`comment` or `question`).

5. If the developer pushes back, read their response carefully and address their specific concern.

6. Once convinced, use `done` to finish.



Code:

{obs.code_snippet}



Author says:

{author_msg if author_msg else "(no response yet – start with inspection)"}



Last tool output:

{tool_output if tool_output else "(none)"}



Available actions:

run_tests, run_linter, inspect, query_docs, fix, comment, question, done



Respond ONLY in JSON:

{{"action_type": "...", "content": "..."}}"""
    
    if history_lines:
        history = "\n".join(history_lines[-6:])
        prompt += f"\n\nPrevious steps:\n{history}"
    return prompt

# ======================================================================
@dataclass
class Trajectory:
    states: List[str]
    actions: List[str]
    rewards: List[float]
    logprobs: List[float]
    dones: List[bool]
    def __len__(self): return len(self.states)

def collect_trajectory(env, model, tokenizer, max_steps=6, temperature=0.0):
    obs = env.reset()
    history_lines = []
    states, actions, rewards, logprobs, dones = [], [], [], [], []
    for step in range(max_steps):
        prompt = build_prompt(obs, history_lines)
        states.append(prompt)
        action_text, logprob = generate_action_with_logprob(prompt, model, tokenizer, temperature)
        actions.append(action_text)
        logprobs.append(logprob)
        action = parse_action(action_text)
        env_action = map_to_env(action)
        next_obs, reward, done, _ = env.step(env_action)
        rewards.append(reward.value)
        dones.append(done)
        history_lines.append(f"Agent: {action_text}")
        history_lines.append(f"Env: {next_obs.last_tool_output}")
        obs = next_obs
        if done: break
    return Trajectory(states, actions, rewards, logprobs, dones)

def collect_trajectories(env, model, tokenizer, n_trajectories, max_steps=6,

                         task_levels=None, task_weights=None):
    if task_levels is None:
        task_levels = list(BUG_DB.keys())
    if task_weights is not None and len(task_weights) != len(task_levels):
        raise ValueError("task_weights must match task_levels length")
    if task_weights is not None and sum(task_weights) <= 0:
        raise ValueError("task_weights must have a positive total")
    trajectories = []
    for i in range(n_trajectories):
        sampled_task = random.choices(task_levels, weights=task_weights, k=1)[0]
        env.set_task(sampled_task)
        traj = collect_trajectory(env, model, tokenizer, max_steps)
        total_reward = sum(traj.rewards)
        print(f"Trajectory {i+1}/{n_trajectories}: task={sampled_task}, steps={len(traj)}, reward={total_reward:.3f}")
        trajectories.append(traj)
    return trajectories

def compute_returns_and_advantages(rewards, dones, gamma=0.99, standardize=True):
    """

    Compute discounted returns and REINFORCE-style baseline advantages.

    Advantages are centered and optionally standardised.

    """
    n = len(rewards)
    returns = [0.0]*n
    running = 0.0
    for t in reversed(range(n)):
        if dones[t]: running = 0.0
        running = rewards[t] + gamma * running
        returns[t] = running
    if standardize:
        advantages = np.array(returns) - np.mean(returns)
        adv_std = np.std(advantages) + 1e-8
        advantages = (advantages / adv_std).tolist()
    else:
        advantages = returns.copy()
    return advantages, returns

def ppo_update(trajectories, model, tokenizer, optimizer, n_epochs=1, clip_epsilon=0.2,

               entropy_coef=0.01, gamma=0.99):
    model.train()
    all_states, all_actions, all_old_logprobs, all_advantages = [], [], [], []
    for traj in trajectories:
        advantages, _ = compute_returns_and_advantages(traj.rewards, traj.dones, gamma=gamma, standardize=True)
        all_states.extend(traj.states)
        all_actions.extend(traj.actions)
        all_old_logprobs.extend(traj.logprobs)
        all_advantages.extend(advantages)
    n_samples = len(all_states)
    total_loss, total_policy_loss, total_entropy, n_updates = 0.0, 0.0, 0.0, 0
    for epoch in range(n_epochs):
        indices = np.random.permutation(n_samples)
        for i in indices:
            state = all_states[i]
            action = all_actions[i]
            old_logprob = all_old_logprobs[i]
            advantage = all_advantages[i]
            messages = [{"role": "user", "content": state}]
            formatted = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
            full_text = formatted + action
            inputs = tokenizer(full_text, return_tensors="pt", max_length=480, truncation=True).to("cuda")
            outputs = model(**inputs)
            logits = outputs.logits
            action_ids = tokenizer.encode(action, add_special_tokens=False)
            prefix_ids = tokenizer.encode(formatted, add_special_tokens=False)
            action_start = len(prefix_ids)
            logprobs = []
            entropy = 0.0
            for idx, token_id in enumerate(action_ids):
                position = action_start + idx - 1
                if 0 <= position < logits.shape[1]:
                    token_logits = logits[0, position]
                    log_probs = F.log_softmax(token_logits, dim=-1)
                    token_logprob = log_probs[token_id]
                    logprobs.append(token_logprob)
                    probs = F.softmax(token_logits, dim=-1)
                    entropy += -(probs * log_probs).sum()
            if not logprobs: continue
            new_logprob = sum(logprobs)
            avg_entropy = entropy / len(logprobs) if logprobs else 0.0
            ratio = torch.exp(new_logprob - old_logprob)
            surr1 = ratio * advantage
            surr2 = torch.clamp(ratio, 1 - clip_epsilon, 1 + clip_epsilon) * advantage
            policy_loss = -torch.min(surr1, surr2)
            loss = policy_loss - entropy_coef * avg_entropy
            optimizer.zero_grad()
            loss.backward()
            torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
            optimizer.step()
            total_loss += loss.item()
            total_policy_loss += policy_loss.item()
            total_entropy += avg_entropy.item()
            n_updates += 1
    torch.cuda.empty_cache()
    return {"loss": total_loss / n_updates if n_updates else 0.0,
            "policy_loss": total_policy_loss / n_updates if n_updates else 0.0,
            "entropy": total_entropy / n_updates if n_updates else 0.0}

def evaluate_policy(env, model, tokenizer, n_episodes=3, max_steps=6,

                    task_levels=None, verbose=False):
    """Evaluate the current policy across task levels. Returns metrics + optional traces."""
    model.eval()
    if task_levels is None:
        task_levels = list(BUG_DB.keys())
    total_rewards = []
    traces = []  # human-readable behavior logs
    for ep in range(n_episodes):
        task = task_levels[ep % len(task_levels)]
        env.set_task(task)
        traj = collect_trajectory(env, model, tokenizer, max_steps, temperature=0.0)
        ep_reward = sum(traj.rewards)
        total_rewards.append(ep_reward)
        if verbose:
            actions_taken = []
            for a in traj.actions:
                try:
                    actions_taken.append(json.loads(a).get("action_type", "?"))
                except Exception:
                    actions_taken.append("?")
            traces.append({
                "task": task,
                "reward": round(ep_reward, 4),
                "steps": len(traj),
                "actions": actions_taken,
            })
    return {
        "avg_reward": float(np.mean(total_rewards)),
        "std_reward": float(np.std(total_rewards)),
        "min_reward": float(np.min(total_rewards)),
        "max_reward": float(np.max(total_rewards)),
        "traces": traces,
    }

# ======================================================================
# MANUAL WARM-UP (no SFTTrainer β†’ no multiprocessing OOM)
# ======================================================================
def json_warmup(model, tokenizer, json_path="training_data.json",

                n_episodes=20, epochs=2, lr=2e-5):
    """

    Supervised warm-up from pre-generated expert demonstrations.

    Uses raw cross-entropy on action tokens with manual gradient steps.

    NO SFTTrainer, NO multiprocessing – runs safely on any GPU.

    """
    print("\n" + "="*60)
    print("SUPERVISED WARM-UP: training_data.json (manual cross-entropy)")
    print("="*60)

    with open(json_path, encoding="utf-8") as f:
        data = json.load(f)

    # Each episode = 7 steps. Select n_episodes worth.
    steps_per_episode = 7
    max_examples = n_episodes * steps_per_episode
    if max_examples < len(data):
        data = data[:max_examples]

    print(f"  {len(data)} examples ({len(data)//steps_per_episode} episodes), "
          f"{epochs} epoch(s), lr={lr}")

    model.train()
    warmup_opt = AdamW(model.parameters(), lr=lr)
    warmup_losses = []   # per-epoch avg loss

    for epoch in range(epochs):
        random.shuffle(data)
        epoch_loss = 0.0
        n_valid = 0

        for i, example in enumerate(data):
            prompt = example["prompt"]
            action = example["action"]

            # ---- tokenize full sequence (prompt + action) ----
            messages = [
                {"role": "user", "content": prompt},
                {"role": "assistant", "content": action},
            ]
            full_text = tokenizer.apply_chat_template(messages, tokenize=False)
            inputs = tokenizer(full_text, return_tensors="pt",
                               max_length=480, truncation=True).to("cuda")

            # ---- find where the action tokens start ----
            prompt_only = tokenizer.apply_chat_template(
                [{"role": "user", "content": prompt}],
                tokenize=False, add_generation_prompt=True
            )
            prompt_ids = tokenizer.encode(prompt_only, add_special_tokens=False)
            prompt_len = len(prompt_ids)

            total_len = inputs.input_ids.shape[1]
            if prompt_len >= total_len:
                continue  # prompt was truncated away, skip

            # ---- cross-entropy on action tokens only ----
            outputs = model(**inputs)
            logits = outputs.logits

            # next-token prediction: logits[t] predicts token[t+1]
            shift_logits = logits[0, prompt_len - 1 : total_len - 1]
            shift_labels = inputs.input_ids[0, prompt_len : total_len]

            min_len = min(shift_logits.shape[0], shift_labels.shape[0])
            if min_len == 0:
                continue

            loss = F.cross_entropy(shift_logits[:min_len], shift_labels[:min_len])

            warmup_opt.zero_grad()
            loss.backward()
            torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
            warmup_opt.step()

            epoch_loss += loss.item()
            n_valid += 1

            if (i + 1) % 25 == 0:
                avg = epoch_loss / n_valid
                print(f"    epoch {epoch+1}  step {i+1:3d}/{len(data)}  "
                      f"running_loss={avg:.4f}")

        avg_loss = epoch_loss / max(n_valid, 1)
        warmup_losses.append(avg_loss)
        print(f"  Epoch {epoch+1} done: avg_loss={avg_loss:.4f}  "
              f"({n_valid} valid examples)")

    torch.cuda.empty_cache()
    print(f"βœ“ Warm-up complete.  Loss: "
          f"{' β†’ '.join(f'{l:.4f}' for l in warmup_losses)}\n")
    return warmup_losses


# ======================================================================
# MAIN TRAINING PIPELINE
# ======================================================================
def train_ppo():
    # --- Hyperparameters ---
    n_iterations = 8            # enough for a clear upward trend
    trajectories_per_iter = 4   # on-policy data per iteration
    n_epochs = 1
    max_steps = 6
    learning_rate = 3e-5
    clip_epsilon = 0.2
    entropy_coef = 0.01
    gamma = 0.99

    # --- Pre-load embedder before LLM (Issue #13) ---
    from rltool import ToolBox
    print("Pre-loading sentence-transformer embedder...")
    ToolBox._get_embedder()
    print("βœ“ Embedder ready")

    # --- Load model ---
    print("Loading model...")
    model, tokenizer = load_model()
    if not test_model_sanity(model, tokenizer):
        return
    env = CodeReviewEnv()
    task_levels = list(BUG_DB.keys())

    # ==================================================================
    # PHASE 0: BASELINE (untrained policy)
    # ==================================================================
    print("\n" + "="*60)
    print("PHASE 0 – BASELINE EVALUATION (untrained)")
    print("="*60)
    baseline = evaluate_policy(env, model, tokenizer, n_episodes=5,
                               max_steps=max_steps, task_levels=task_levels,
                               verbose=True)
    baseline_reward = baseline["avg_reward"]
    print(f"Baseline avg reward: {baseline_reward:.4f}  "
          f"(min={baseline['min_reward']:.4f}, max={baseline['max_reward']:.4f})")
    print("Baseline behavior:")
    for t in baseline["traces"]:
        print(f"  task={t['task']:8s}  reward={t['reward']:+.4f}  "
              f"steps={t['steps']}  actions={t['actions']}")

    # ==================================================================
    # PHASE 1: SUPERVISED WARM-UP (expert demos, manual CE)
    # ==================================================================
    warmup_losses = json_warmup(
        model, tokenizer,
        json_path="training_data.json",
        n_episodes=20,  # 140 examples (20 Γ— 7 steps)
        epochs=2,
        lr=2e-5,
    )

    # Post-warmup evaluation
    print("="*60)
    print("POST WARM-UP EVALUATION")
    print("="*60)
    post_warmup = evaluate_policy(env, model, tokenizer, n_episodes=5,
                                  max_steps=max_steps, task_levels=task_levels,
                                  verbose=True)
    warmup_reward = post_warmup["avg_reward"]
    print(f"Post-warmup avg reward: {warmup_reward:.4f}  "
          f"(Ξ” vs baseline: {warmup_reward - baseline_reward:+.4f})")
    print("Post-warmup behavior:")
    for t in post_warmup["traces"]:
        print(f"  task={t['task']:8s}  reward={t['reward']:+.4f}  "
              f"steps={t['steps']}  actions={t['actions']}")

    # ==================================================================
    # PHASE 2: TRUE RL – PPO (on-policy, real environment interaction)
    # ==================================================================
    optimizer = AdamW(model.parameters(), lr=learning_rate)
    print(f"\n{'='*60}")
    print(f"PHASE 2 – PPO TRAINING: {n_iterations} iterations Γ— "
          f"{trajectories_per_iter} trajectories (true RL)")
    print(f"{'='*60}\n")

    reward_history = []
    eval_history = []
    loss_history = []
    policy_loss_history = []
    entropy_history = []

    for iteration in range(n_iterations):
        print(f"\n--- PPO Iteration {iteration + 1}/{n_iterations} ---")

        # Collect on-policy trajectories from REAL environment
        trajectories = collect_trajectories(
            env, model, tokenizer, trajectories_per_iter, max_steps,
            task_levels=task_levels, task_weights=None
        )
        avg_reward = float(np.mean([sum(t.rewards) for t in trajectories]))
        reward_history.append(avg_reward)
        print(f"  Collect  avg reward: {avg_reward:+.4f}")

        # PPO policy gradient update
        metrics = ppo_update(
            trajectories, model, tokenizer, optimizer,
            n_epochs=n_epochs, clip_epsilon=clip_epsilon,
            entropy_coef=entropy_coef, gamma=gamma
        )
        loss_history.append(float(metrics["loss"]))
        policy_loss_history.append(float(metrics["policy_loss"]))
        entropy_history.append(float(metrics["entropy"]))
        print(f"  Update   loss={metrics['loss']:.4f}  "
              f"policy={metrics['policy_loss']:.4f}  "
              f"entropy={metrics['entropy']:.4f}")

        # Evaluate greedy policy after update
        eval_m = evaluate_policy(env, model, tokenizer, n_episodes=3,
                                 max_steps=max_steps, task_levels=task_levels,
                                 verbose=False)
        eval_history.append(eval_m["avg_reward"])
        delta = eval_m["avg_reward"] - baseline_reward
        print(f"  Eval     avg reward: {eval_m['avg_reward']:+.4f}  "
              f"(Ξ” baseline: {delta:+.4f})")

    # ==================================================================
    # PHASE 3: FINAL EVALUATION (proof of learning)
    # ==================================================================
    print("\n" + "="*60)
    print("PHASE 3 – FINAL EVALUATION (after all training)")
    print("="*60)
    final = evaluate_policy(env, model, tokenizer, n_episodes=5,
                            max_steps=max_steps, task_levels=task_levels,
                            verbose=True)
    print(f"Final avg reward: {final['avg_reward']:.4f}  "
          f"(min={final['min_reward']:.4f}, max={final['max_reward']:.4f})")
    print("Final behavior:")
    for t in final["traces"]:
        print(f"  task={t['task']:8s}  reward={t['reward']:+.4f}  "
              f"steps={t['steps']}  actions={t['actions']}")

    total_improvement = final["avg_reward"] - baseline_reward
    ppo_improvement = final["avg_reward"] - warmup_reward
    print(f"\n{'='*60}")
    print("TRAINING SUMMARY")
    print(f"  Baseline reward:    {baseline_reward:+.4f}")
    print(f"  Post-warmup reward: {warmup_reward:+.4f}  "
          f"(warmup Ξ”: {warmup_reward - baseline_reward:+.4f})")
    print(f"  Final reward:       {final['avg_reward']:+.4f}  "
          f"(PPO Ξ”: {ppo_improvement:+.4f})")
    print(f"  Total improvement:  {total_improvement:+.4f}")
    print(f"  Reward trend (PPO): {' β†’ '.join(f'{r:+.3f}' for r in reward_history)}")
    print(f"  Loss trend (PPO):   {' β†’ '.join(f'{l:.4f}' for l in loss_history)}")
    if total_improvement > 0:
        print(f"  βœ“ Agent IMPROVED by {total_improvement:+.4f}")
    else:
        print(f"  βœ— No overall improvement detected")
    print(f"{'='*60}")

    # ==================================================================
    # PLOTS
    # ==================================================================
    iters = list(range(1, n_iterations + 1))

    # --- 1. Warm-up loss curve ---
    if warmup_losses:
        fig, ax = plt.subplots(figsize=(7, 4))
        ax.plot(range(1, len(warmup_losses) + 1), warmup_losses,
                marker="o", linewidth=2, color="tab:purple")
        ax.set_title("Warm-up Loss (supervised, per epoch)",
                     fontsize=13, fontweight="bold")
        ax.set_xlabel("Epoch")
        ax.set_ylabel("Cross-Entropy Loss")
        ax.grid(alpha=0.3)
        fig.tight_layout()
        fig.savefig("warmup_loss.png", dpi=150)
        plt.close(fig)

    # --- 2. PPO reward curve ---
    fig, ax = plt.subplots(figsize=(9, 5))
    ax.plot(iters, reward_history, marker="o", linewidth=2,
            label="Collect reward", color="tab:blue")
    ax.plot(iters, eval_history, marker="s", linewidth=2, linestyle="--",
            label="Eval reward", color="tab:green")
    ax.axhline(y=baseline_reward, color="tab:gray", linestyle=":",
               linewidth=1.5, label=f"Baseline ({baseline_reward:+.3f})")
    ax.axhline(y=warmup_reward, color="tab:purple", linestyle=":",
               linewidth=1.5, label=f"Post-warmup ({warmup_reward:+.3f})")
    ax.set_title("PPO Reward per Iteration", fontsize=14, fontweight="bold")
    ax.set_xlabel("Iteration")
    ax.set_ylabel("Average Reward")
    ax.legend(loc="best", fontsize=8)
    ax.grid(alpha=0.3)
    fig.tight_layout()
    fig.savefig("reward_curve.png", dpi=150)
    plt.close(fig)

    # --- 3. PPO loss curve ---
    fig, ax = plt.subplots(figsize=(9, 5))
    ax.plot(iters, loss_history, marker="o", linewidth=2,
            label="Total loss", color="tab:red")
    ax.plot(iters, policy_loss_history, marker="^", linewidth=2, linestyle="--",
            label="Policy loss", color="tab:orange")
    ax.set_title("PPO Loss per Iteration", fontsize=14, fontweight="bold")
    ax.set_xlabel("Iteration")
    ax.set_ylabel("Loss")
    ax.legend(loc="best")
    ax.grid(alpha=0.3)
    fig.tight_layout()
    fig.savefig("loss_curve.png", dpi=150)
    plt.close(fig)

    # --- 4. Combined 3-panel summary ---
    fig, axes = plt.subplots(1, 3, figsize=(18, 5))

    # Panel A: warm-up loss
    if warmup_losses:
        axes[0].plot(range(1, len(warmup_losses) + 1), warmup_losses,
                     marker="o", linewidth=2, color="tab:purple")
        axes[0].set_title("A. Warm-up Loss ↓")
        axes[0].set_xlabel("Epoch")
        axes[0].set_ylabel("CE Loss")
        axes[0].grid(alpha=0.3)

    # Panel B: PPO reward
    axes[1].plot(iters, reward_history, marker="o", linewidth=2,
                 color="tab:blue", label="Collect")
    axes[1].plot(iters, eval_history, marker="s", linewidth=2,
                 linestyle="--", color="tab:green", label="Eval")
    axes[1].axhline(y=baseline_reward, color="tab:gray", linestyle=":",
                    linewidth=1.5, label="Baseline")
    axes[1].axhline(y=warmup_reward, color="tab:purple", linestyle=":",
                    linewidth=1.5, label="Post-warmup")
    axes[1].set_title("B. PPO Reward ↑")
    axes[1].set_xlabel("Iteration")
    axes[1].set_ylabel("Avg Reward")
    axes[1].legend(fontsize=7)
    axes[1].grid(alpha=0.3)

    # Panel C: PPO loss
    axes[2].plot(iters, loss_history, marker="o", linewidth=2,
                 color="tab:red", label="Total")
    axes[2].plot(iters, policy_loss_history, marker="^", linewidth=2,
                 linestyle="--", color="tab:orange", label="Policy")
    axes[2].set_title("C. PPO Loss ↓")
    axes[2].set_xlabel("Iteration")
    axes[2].set_ylabel("Loss")
    axes[2].legend(fontsize=7)
    axes[2].grid(alpha=0.3)

    fig.suptitle("Code Review Agent – Full Training Evidence",
                 fontsize=14, fontweight="bold")
    fig.tight_layout()
    fig.savefig("training_summary.png", dpi=150)
    plt.close(fig)

    print("Plots saved: warmup_loss.png, reward_curve.png, "
          "loss_curve.png, training_summary.png")
    print("="*60)

if __name__ == "__main__":
    train_ppo()