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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
import json
import hashlib
import os
import random
import re
from collections import defaultdict, deque
from dataclasses import dataclass, field
from typing import Optional

from dotenv import load_dotenv
from huggingface_hub import InferenceClient

load_dotenv()

USE_LOCAL_MODEL = os.getenv("USE_LOCAL_MODEL", "1").strip().lower() in {"1", "true", "yes"}
LOCAL_MODEL_ID = os.getenv("LOCAL_MODEL_ID", "Qwen/Qwen2.5-7B-Instruct")
LOCAL_MAX_NEW_TOKENS = int(os.getenv("LOCAL_MAX_NEW_TOKENS", "56"))
ENABLE_LLM_PICK = os.getenv("ENABLE_LLM_PICK", "0").strip().lower() in {"1", "true", "yes"}
USE_VALID_ACTIONS = os.getenv("USE_VALID_ACTIONS", "1").strip().lower() in {"1", "true", "yes"}
REMOTE_MODEL = os.getenv("REMOTE_MODEL", "Qwen/Qwen2.5-72B-Instruct")

DISALLOWED_ACTIONS = {"inventory", "i", "quit", "q", "restart", "restore", "save", "script", "unscript"}
MOVE_ACTIONS = {
    "north",
    "south",
    "east",
    "west",
    "northeast",
    "northwest",
    "southeast",
    "southwest",
    "up",
    "down",
    "enter",
    "exit",
}
NEGATIVE_PATTERNS = (
    "you can't",
    "you cannot",
    "that's not",
    "i don't",
    "nothing happens",
    "there is no",
    "not here",
    "not open",
    "not allowed",
    "not see",
)

_local_pipeline = None
_remote_client = None
LLM_READY = False

if ENABLE_LLM_PICK:
    if USE_LOCAL_MODEL:
        try:
            import torch
            from transformers import pipeline as hf_pipeline

            dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
            _local_pipeline = hf_pipeline("text-generation", model=LOCAL_MODEL_ID, device_map="auto", torch_dtype=dtype)
            LLM_READY = True
        except Exception:
            LLM_READY = False
    else:
        token = os.getenv("HF_TOKEN")
        if token:
            _remote_client = InferenceClient(token=token)
            LLM_READY = True


@dataclass
class RunResult:
    final_score: int
    max_score: int
    moves: int
    locations_visited: set[str]
    game_completed: bool
    error: Optional[str] = None
    history: list[tuple[str, str, str]] = field(default_factory=list)


@dataclass
class ActionStats:
    tries: int = 0
    success: int = 0
    fail: int = 0
    score_gain: int = 0


@dataclass
class LocationMemory:
    visits: int = 0
    valid_actions: list[str] = field(default_factory=list)
    actions: dict[str, ActionStats] = field(default_factory=dict)
    failed: set[str] = field(default_factory=set)
    promising: set[str] = field(default_factory=set)
    exits: dict[str, str] = field(default_factory=dict)
    move_fail_sigs: dict[str, set[str]] = field(default_factory=dict)


def _extract_generated_text(outputs) -> str:
    if not outputs:
        return ""
    first = outputs[0]
    text = first.get("generated_text", "") if isinstance(first, dict) else str(first)
    if isinstance(text, list) and text:
        tail = text[-1]
        return str(tail.get("content", "")) if isinstance(tail, dict) else str(tail)
    return str(text)


def call_llm(prompt: str, seed: int, max_tokens: int = 56) -> str:
    if not LLM_READY:
        return ""
    if USE_LOCAL_MODEL and _local_pipeline is not None:
        kwargs = {"max_new_tokens": min(max_tokens, LOCAL_MAX_NEW_TOKENS), "do_sample": False, "temperature": 0.0}
        try:
            return _extract_generated_text(_local_pipeline([{"role": "user", "content": prompt}], **kwargs))
        except Exception:
            plain = f"USER:\n{prompt}\n\nASSISTANT:\n"
            return _extract_generated_text(_local_pipeline(plain, **kwargs))
    if _remote_client is None:
        return ""
    try:
        resp = _remote_client.chat.completions.create(
            model=REMOTE_MODEL,
            messages=[{"role": "user", "content": prompt}],
            temperature=0.0,
            max_tokens=max_tokens,
            seed=seed,
        )
        return resp.choices[0].message.content or ""
    except Exception:
        return ""


class StudentAgent:
    def __init__(self):
        self.rng = random.Random(0)
        self.tool_names = set()

        self.score = 0
        self.max_score_seen = 350
        self.no_progress_streak = 0
        self.same_location_streak = 0

        self.last_obs_sig = ""
        self.last_loc = "Unknown"
        self.llm_calls = 0

        self.history = []
        self.action_trace = deque(maxlen=16)
        self.location_trace = deque(maxlen=20)
        self.action_queue = deque(maxlen=16)
        self.tabu_pairs = deque(maxlen=28)

        self.location_memory = defaultdict(LocationMemory)
        self.global_stats = defaultdict(ActionStats)
        self.global_failures = defaultdict(int)
        self.global_successes = defaultdict(int)
        self.term_counts = defaultdict(int)

        self.inventory = set()

    async def run(self, client, game: str, max_steps: int, seed: int, verbose: bool = False) -> RunResult:
        self.rng.seed(seed)
        tools = await client.list_tools()
        self.tool_names = {t.name for t in tools}
        if "play_action" not in self.tool_names:
            return RunResult(0, 0, 0, set(), False, error="play_action tool missing")

        visited, out_hist = set(), []

        text = self._extract_tool_text(await client.call_tool("play_action", {"action": "look"}))
        obs, meta = self._split_observation_and_meta(text)
        cur_loc = self._loc(obs, meta)
        self._record(action="look", from_loc=cur_loc, obs=obs, meta=meta)
        visited.add(self._visit_loc(obs, meta))

        if "inventory" in self.tool_names:
            self.inventory.update(await self._fetch_inventory(client))
        if USE_VALID_ACTIONS and "get_valid_actions" in self.tool_names:
            valid = await self._fetch_valid_actions(client)
            if self._valid_actions_usable(valid):
                self.location_memory[cur_loc].valid_actions = valid

        for step in range(1, max_steps + 1):
            if self._done(obs, meta):
                break

            cur_loc = self._loc(obs, meta)
            loc_mem = self.location_memory[cur_loc]

            entered_new = bool(meta.get("changed_location", False)) if isinstance(meta, dict) else False
            if loc_mem.visits <= 1:
                entered_new = True

            if "inventory" in self.tool_names and (step <= 3 or step % 14 == 0 or self.no_progress_streak >= 3):
                self.inventory.update(await self._fetch_inventory(client))

            if USE_VALID_ACTIONS and "get_valid_actions" in self.tool_names and (
                entered_new
                or not loc_mem.valid_actions
                or (self.no_progress_streak >= 4 and step % 4 == 0)
            ):
                valid = await self._fetch_valid_actions(client)
                if self._valid_actions_usable(valid):
                    loc_mem.valid_actions = valid

            action, thought = self._choose_action(cur_loc, obs, step, max_steps, seed + step)
            try:
                text = self._extract_tool_text(await client.call_tool("play_action", {"action": action}))
                obs, meta = self._split_observation_and_meta(text)
            except Exception as exc:
                obs, meta = f"Tool error: {exc}", {}

            self._record(action=action, from_loc=cur_loc, obs=obs, meta=meta)
            visited.add(self._visit_loc(obs, meta))
            out_hist.append((thought, action, obs[:220]))

            if verbose:
                print(f"[step {step}] score={self.score} loc={self._loc(obs,meta)} stall={self.no_progress_streak} action={action}")
            if self._done(obs, meta):
                break

        moves = int(meta.get("moves", 0)) if isinstance(meta, dict) else 0
        return RunResult(self.score, self.max_score_seen, moves, visited, self._done(obs, meta), history=out_hist)

    async def _fetch_valid_actions(self, client) -> list[str]:
        try:
            raw = self._extract_tool_text(await client.call_tool("get_valid_actions", {"timeout_s": 2.5}))
        except Exception:
            return []
        return self._parse_valid_actions(raw)

    async def _fetch_inventory(self, client) -> set[str]:
        try:
            raw = self._extract_tool_text(await client.call_tool("inventory", {}))
        except Exception:
            return set()
        return self._parse_inventory(raw)

    def _record(self, action: str, from_loc: str, obs: str, meta: dict):
        action = self._norm(action)
        to_loc = self._loc(obs, meta)
        self.location_memory[to_loc].visits += 1
        self.location_trace.append(to_loc)
        self.action_trace.append(action)

        prev_score = self.score
        score_now = int(meta.get("score", self._extract_score(obs))) if isinstance(meta, dict) else self._extract_score(obs)
        self.score = max(self.score, score_now)
        score_gain = max(0, self.score - prev_score)

        if isinstance(meta, dict):
            self.max_score_seen = max(self.max_score_seen, int(meta.get("max_score", self.max_score_seen)))

        obs_sig = self._sig(obs)
        prev_sig = self.last_obs_sig
        changed_loc = (to_loc != from_loc) or (bool(meta.get("changed_location", False)) if isinstance(meta, dict) else False)
        negative = self._negative(obs)

        progress = "none"
        if score_gain > 0:
            progress = "score"
        elif changed_loc:
            progress = "move"
        elif obs_sig != self.last_obs_sig and not negative and action not in {"look", "inventory"}:
            progress = "state"

        if progress == "none":
            self.no_progress_streak += 1
            self.same_location_streak += 1
        else:
            self.no_progress_streak = 0
            self.same_location_streak = 0 if changed_loc else max(0, self.same_location_streak - 1)

        from_mem = self.location_memory[from_loc]
        stats = from_mem.actions.setdefault(action, ActionStats())
        global_stats = self.global_stats[action]
        stats.tries += 1
        global_stats.tries += 1

        if progress == "none":
            stats.fail += 1
            global_stats.fail += 1
            if self._is_move(action):
                # Contextual move memory: avoid repeating same failed move in same local context.
                key_sig = prev_sig or obs_sig
                from_mem.move_fail_sigs.setdefault(action, set()).add(key_sig)
            else:
                from_mem.failed.add(action)  # strict for non-move actions
            self.global_failures[action] += 1
        else:
            stats.success += 1
            global_stats.success += 1
            self.global_successes[action] += 1
            if action in from_mem.failed:
                from_mem.failed.remove(action)
            if self._is_move(action) and action in from_mem.move_fail_sigs:
                from_mem.move_fail_sigs.pop(action, None)

        if score_gain > 0:
            stats.score_gain += score_gain
            global_stats.score_gain += score_gain
            from_mem.promising.add(action)

        if changed_loc and self._is_move(action):
            from_mem.exits[action] = to_loc

        for hint in self._extract_promising_actions(obs):
            from_mem.promising.add(hint)
        if progress in {"state", "score"}:
            for follow in self._extract_followup_actions(obs):
                if follow not in from_mem.failed:
                    self.action_queue.append(follow)
        self._update_terms(obs)

        self.history.append({"loc": to_loc, "action": action, "score": self.score, "progress": progress})
        self.tabu_pairs.append((from_loc, action))
        if len(self.history) > 120:
            self.history = self.history[-120:]

        self.last_obs_sig = obs_sig
        self.last_loc = to_loc

    def _choose_action(self, loc: str, obs: str, step: int, max_steps: int, seed: int) -> tuple[str, str]:
        loc_mem = self.location_memory[loc]
        valid = list(loc_mem.valid_actions)
        valid_set = set(valid)
        obs_sig = self._sig(obs)
        low_obs = obs.lower()
        has_audio_hint = any(w in low_obs for w in ("noise", "sound", "hear", "listen"))

        hints = self._extract_promising_actions(obs)
        moves = [a for a in valid if self._is_move(a)] or list(MOVE_ACTIONS)

        verbs = self._candidate_verbs(valid)
        objects = sorted(self._extract_objects(obs))[:8]
        inv_objs = sorted(self.inventory)[:4]
        terms = self._top_terms(8)
        allow_composed = self._composed_mode(valid)

        generated = []
        for obj in objects:
            generated.extend([f"examine {obj}", f"search {obj}"])
            generated.extend(f"{v} {obj}" for v in verbs[:4])
        for obj in inv_objs:
            generated.extend(f"{v} {obj}" for v in verbs[:2])
        composed = self._compose_actions(objects, inv_objs, terms) if allow_composed else []

        pool = hints + sorted(loc_mem.promising) + valid + generated + composed + moves + ["look", "wait", "listen"]

        candidates = []
        seen = set()
        for cand in pool:
            a = self._norm(cand)
            if not a or a in seen or len(a) > 48:
                continue
            if a in DISALLOWED_ACTIONS or self._unsafe_action(a) or self._incomplete_action(a):
                continue
            if a == "listen" and not has_audio_hint and not allow_composed:
                continue
            if self._is_move(a) and self._move_context_blocked(loc, a, obs_sig):
                continue
            seen.add(a)
            candidates.append(a)

        # Heavy context management: failed actions are banned in that location.
        candidates = [a for a in candidates if a not in loc_mem.failed]

        if not candidates:
            backup = [a for a in moves if a not in loc_mem.failed]
            if backup:
                candidates = backup
            else:
                return "look", "fallback"

        while self.action_queue:
            queued = self._norm(self.action_queue.popleft())
            if queued and queued in candidates and queued not in loc_mem.failed:
                return queued, "queued-followup"

        # Prioritize frontier exploration after a small amount of local probing.
        local_non_move_tries = sum(st.tries for a, st in loc_mem.actions.items() if not self._is_move(a))
        fresh_local = [
            a
            for a in candidates
            if a in valid_set
            and not self._is_move(a)
            and a not in {"look", "wait"}
            and self._likely_actionable(a)
            and loc_mem.actions.get(a, ActionStats()).tries == 0
        ]
        if fresh_local and loc_mem.visits <= 2 and local_non_move_tries < 2 and self.no_progress_streak <= 1:
            pick = max(
                fresh_local,
                key=lambda a: (
                    self._contextual_priority(a, obs),
                    self._score_action(loc, a, valid_set, set(hints)),
                ),
            )
            return pick, "local-probe"

        composed_probe = [
            a
            for a in candidates
            if self._is_composed_action(a)
            and loc_mem.actions.get(a, ActionStats()).tries == 0
            and self._likely_actionable(a)
        ]
        if allow_composed and composed_probe and self.no_progress_streak >= 2:
            pick = max(composed_probe[:12], key=lambda a: self._score_action(loc, a, valid_set, set(hints)))
            return pick, "composed-probe"

        frontier_move = self._frontier_move(loc, candidates, obs_sig)
        rich_non_move = [a for a in valid if " " in a and not self._is_move(a)]
        frontier_gate = (self.no_progress_streak >= 1 or local_non_move_tries >= 2 or loc_mem.visits >= 2)
        if len(rich_non_move) >= 2:
            frontier_gate = (self.no_progress_streak >= 3 or local_non_move_tries >= 4 or loc_mem.visits >= 4)
        if frontier_move and frontier_gate:
            return frontier_move, "frontier"

        if self.same_location_streak >= 3 or self.no_progress_streak >= 3 or loc_mem.visits >= 4:
            move_pick = self._least_tried_move(loc, candidates)
            if move_pick:
                return move_pick, "exploration-bias"

        ranked = sorted(
            candidates,
            key=lambda a: self._score_action(loc, a, valid_set, set(hints)),
            reverse=True,
        )
        if not ranked:
            return "look", "fallback"

        if self._should_use_llm(step, max_steps, ranked):
            picked = self._llm_pick(loc, obs, ranked[:8], seed)
            if picked in ranked:
                self.llm_calls += 1
                return picked, "llm-pick"

        return ranked[0], "heuristic"

    def _move_context_blocked(self, loc: str, action: str, obs_sig: str) -> bool:
        fails = self.location_memory[loc].move_fail_sigs.get(action, set())
        return bool(obs_sig and obs_sig in fails)

    def _frontier_move(self, loc: str, candidates: list[str], obs_sig: str) -> str:
        loc_mem = self.location_memory[loc]
        move_candidates = [a for a in candidates if self._is_move(a)]
        if not move_candidates:
            return ""

        # Direct frontier: untried exits from current location first.
        untried = [
            a
            for a in move_candidates
            if loc_mem.actions.get(a, ActionStats()).tries == 0 and not self._move_context_blocked(loc, a, obs_sig)
        ]
        if untried:
            untried.sort(key=lambda a: loc_mem.actions.get(a, ActionStats()).tries)
            return untried[0]

        # Indirect frontier: shortest known path to a location with untried exits.
        q = deque([(loc, "")])
        seen = {loc}
        while q:
            node, first_act = q.popleft()
            if node != loc and self._has_untried_exits(node):
                if first_act and first_act in move_candidates and not self._move_context_blocked(loc, first_act, obs_sig):
                    return first_act
            for act, dst in self.location_memory[node].exits.items():
                if not dst or dst in seen:
                    continue
                seen.add(dst)
                q.append((dst, first_act or act))
        return ""

    def _has_untried_exits(self, loc: str) -> bool:
        mem = self.location_memory[loc]
        moves = [a for a in mem.valid_actions if self._is_move(a)]
        if not moves:
            moves = list(MOVE_ACTIONS)
        return any(mem.actions.get(a, ActionStats()).tries == 0 for a in moves)

    def _score_action(self, loc: str, action: str, valid_set: set[str], hint_set: set[str]) -> float:
        loc_mem = self.location_memory[loc]
        st = loc_mem.actions.get(action, ActionStats())
        gt = self.global_stats.get(action, ActionStats())

        score = 60.0 * st.score_gain + 8.0 * st.success - 12.0 * st.fail - 3.0 * st.tries - 1.0 * gt.tries
        if st.tries == 0:
            score += 10.0
        if gt.tries == 0:
            score += 4.0
        if action in valid_set:
            score += 8.0
        if action in hint_set or action in loc_mem.promising:
            score += 10.0
        if self._is_move(action):
            score += 2.0
            if st.tries == 0:
                score += 6.0
            if self.no_progress_streak >= 1:
                score += 12.0
            if self.same_location_streak >= 2:
                score += 10.0
        else:
            if st.tries == 0 and loc_mem.visits <= 2:
                score += 12.0
            if self.same_location_streak >= 3:
                score -= 10.0
            if self._is_composed_action(action):
                score += 6.0 if self.no_progress_streak >= 2 else 1.0

        recent = list(self.action_trace)[-3:]
        if action in recent:
            score -= 10.0
        if len(recent) >= 2 and recent[-1] == recent[-2] == action:
            score -= 20.0
        if (loc, action) in self.tabu_pairs:
            score -= 14.0

        if self.global_failures[action] >= 3 and self.global_successes[action] == 0:
            score -= 12.0

        opposite = {"north": "south", "south": "north", "east": "west", "west": "east", "up": "down", "down": "up", "enter": "exit", "exit": "enter"}
        last = self.action_trace[-1] if self.action_trace else ""
        if opposite.get(last, "") == action and self.no_progress_streak <= 1:
            score -= 16.0

        return score

    def _least_tried_move(self, loc: str, candidates: list[str]) -> str:
        loc_mem = self.location_memory[loc]
        moves = [a for a in candidates if self._is_move(a) and a not in loc_mem.failed]
        if not moves:
            return ""
        obs_sig = self.last_obs_sig
        moves = [a for a in moves if not self._move_context_blocked(loc, a, obs_sig)] or moves
        moves.sort(key=lambda a: loc_mem.actions.get(a, ActionStats()).tries)
        return moves[0]

    @staticmethod
    def _verb_priority(action: str) -> int:
        head = action.split()[0] if action.split() else ""
        pri = {"open": 6, "unlock": 6, "take": 5, "get": 5, "read": 4, "use": 4, "enter": 3, "examine": 2, "search": 1}
        return pri.get(head, 0)

    def _contextual_priority(self, action: str, obs: str) -> int:
        base = self._verb_priority(action)
        head = action.split()[0] if action.split() else ""
        low = obs.lower()
        if any(w in low for w in ("noise", "sound", "hear")):
            if head in {"search", "examine", "listen"}:
                base += 4
            if head in {"open", "take"}:
                base -= 3
        if head in {"open", "unlock"} and any(k in action for k in ("door", "window", "mailbox", "gate", "chest", "box", "lid")):
            base += 5
        return base

    @staticmethod
    def _is_composed_action(action: str) -> bool:
        head = action.split()[0] if action.split() else ""
        if head in {"ask", "tell", "show", "give", "follow", "catch", "greet", "talk"}:
            return True
        return any(tok in action for tok in (" about ", " with ", " to ", " under ", " inside ", " behind ", " in ", " on "))

    @staticmethod
    def _composed_mode(valid_actions: list[str]) -> bool:
        if not valid_actions:
            return True
        non_move = [a for a in valid_actions if a not in MOVE_ACTIONS and a not in {"look", "wait", "listen"}]
        rich = [a for a in non_move if " " in a]
        return len(rich) <= 1

    @staticmethod
    def _likely_actionable(action: str) -> bool:
        parts = action.split()
        if len(parts) <= 1:
            return parts[0] in {"look", "wait", "listen"} if parts else False
        obj = parts[-1]
        generic = {
            "north",
            "south",
            "east",
            "west",
            "up",
            "down",
            "inside",
            "outside",
            "back",
            "front",
            "forest",
            "field",
            "path",
            "road",
            "area",
            "dark",
            "night",
        }
        return obj not in generic

    def _update_terms(self, obs: str):
        for t in self._extract_keywords(obs):
            self.term_counts[t] += 1

    def _top_terms(self, k: int) -> list[str]:
        ranked = sorted(self.term_counts.items(), key=lambda kv: kv[1], reverse=True)
        return [t for t, _ in ranked[:k]]

    @staticmethod
    def _extract_keywords(obs: str) -> list[str]:
        low = obs.lower()
        stop = {
            "the", "and", "for", "with", "from", "that", "this", "there", "here", "into", "over",
            "you", "your", "are", "was", "were", "have", "has", "had", "not", "but", "all", "any",
            "north", "south", "east", "west", "up", "down", "look", "time", "night", "dark",
            "forest", "field", "path", "road", "place", "some", "thing", "things",
        }
        out = []
        for tok in re.findall(r"[a-z]{3,16}", low):
            if tok in stop:
                continue
            out.append(tok)
        return out

    def _compose_actions(self, objects: list[str], inv_objs: list[str], terms: list[str]) -> list[str]:
        nouns, seen = [], set()
        for item in list(objects) + list(terms):
            n = self._normalize_noun(str(item))
            if not n or n in seen or len(n) > 24:
                continue
            seen.add(n)
            nouns.append(n)
        nouns = nouns[:8]
        actors = [n for n in nouns if len(n.split()) == 1][:5]
        inv_clean = [self._normalize_noun(x) for x in inv_objs]
        inv_clean = [x for x in inv_clean if x]

        out = []
        for n in nouns:
            out.extend([f"look for {n}", f"look under {n}", f"look behind {n}", f"look inside {n}"])
            out.extend([f"follow {n}", f"catch {n}"])
        for a in actors:
            out.extend([f"greet {a}", f"talk to {a}"])
            for t in nouns[:4]:
                if t != a:
                    out.extend([f"ask {a} about {t}", f"tell {a} about {t}"])
        for it in inv_clean[:3]:
            for a in actors[:3]:
                out.extend([f"show {it} to {a}", f"give {it} to {a}"])
            for t in nouns[:3]:
                out.extend([f"use {it} with {t}", f"put {it} in {t}", f"put {it} on {t}"])

        dedup, used = [], set()
        for a in out:
            a = self._norm(a)
            if not a or a in used or len(a) > 48:
                continue
            if a in DISALLOWED_ACTIONS or self._unsafe_action(a) or self._incomplete_action(a):
                continue
            used.add(a)
            dedup.append(a)
            if len(dedup) >= 48:
                break
        return dedup

    @staticmethod
    def _normalize_noun(text: str) -> str:
        low = re.sub(r"[^a-z ]+", " ", (text or "").lower())
        toks = [t for t in low.split() if t]
        drop = {
            "a", "an", "the", "of", "to", "in", "on", "at", "from", "with", "and",
            "all", "everything", "anything", "something", "thing", "things",
            "north", "south", "east", "west", "up", "down",
        }
        toks = [t for t in toks if t not in drop]
        if not toks:
            return ""
        out = " ".join(toks[:2])
        return out if len(out) >= 3 else ""

    def _should_use_llm(self, step: int, max_steps: int, ranked: list[str]) -> bool:
        if not ENABLE_LLM_PICK or not LLM_READY:
            return False
        if len(ranked) < 3:
            return False
        if self.llm_calls >= min(10, max_steps // 8 + 2):
            return False
        return self.no_progress_streak >= 3 or step <= 2 or step % 16 == 0

    def _llm_pick(self, loc: str, obs: str, candidates: list[str], seed: int) -> str:
        opts = "\n".join(f"- {c}" for c in candidates)
        prompt = (
            f"Location: {loc}\n"
            f"Score: {self.score}/{self.max_score_seen}\n"
            f"No-progress streak: {self.no_progress_streak}\n"
            f"Recent actions: {list(self.action_trace)[-6:]}\n\n"
            f"Observation:\n{obs[:1200]}\n\n"
            f"Choose one exact action from this list:\n{opts}\n"
            "Return JSON: {\"action\":\"...\"}."
        )
        text = call_llm(prompt, seed=seed, max_tokens=40)
        if not text:
            return ""

        try:
            payload = json.loads(text)
            pick = self._norm(str(payload.get("action", "")))
            return pick if pick in candidates else ""
        except Exception:
            pass

        low = text.lower()
        for cand in candidates:
            if cand in low:
                return cand
        return ""

    @staticmethod
    def _extract_tool_text(result) -> str:
        if hasattr(result, "content") and result.content:
            chunk = result.content[0]
            return chunk.text if hasattr(chunk, "text") else str(chunk)
        if isinstance(result, list) and result:
            item = result[0]
            return item.text if hasattr(item, "text") else str(item)
        return str(result)

    def _split_observation_and_meta(self, text: str) -> tuple[str, dict]:
        marker = "\n[META]"
        if marker not in text:
            return text.strip(), {}
        obs, meta_blob = text.rsplit(marker, 1)
        try:
            meta = json.loads(meta_blob.strip())
            if isinstance(meta, dict):
                return obs.strip(), meta
        except Exception:
            pass
        return text.strip(), {}

    @staticmethod
    def _norm(action: str) -> str:
        a = " ".join((action or "").strip().lower().split())
        alias = {
            "n": "north",
            "s": "south",
            "e": "east",
            "w": "west",
            "u": "up",
            "d": "down",
            "i": "inventory",
            "l": "look",
            "ne": "northeast",
            "nw": "northwest",
            "se": "southeast",
            "sw": "southwest",
        }
        if a in alias:
            return alias[a]
        if a.startswith("go "):
            return alias.get(a.split(" ", 1)[1].strip(), a.split(" ", 1)[1].strip())
        return a

    @staticmethod
    def _is_move(action: str) -> bool:
        return action in MOVE_ACTIONS

    @staticmethod
    def _unsafe_action(action: str) -> bool:
        parts = action.split()
        if not parts:
            return True
        if " all " in f" {action} ":
            return True
        if parts[0] in {"take", "drop", "put", "throw", "eat"} and len(parts) >= 2:
            if parts[-1] in {"thing", "things", "something", "anything", "everything", "all"}:
                return True
        return False

    @staticmethod
    def _incomplete_action(action: str) -> bool:
        parts = action.split()
        if len(parts) != 1:
            return False
        return parts[0] in {"examine", "search", "take", "drop", "open", "close", "read", "use", "insert", "put", "throw", "attack"}

    @staticmethod
    def _parse_inventory(raw: str) -> set[str]:
        text = str(raw or "").strip()
        low = text.lower()
        if not text or "empty" in low or "not initialized" in low:
            return set()
        payload = text.split(":", 1)[1] if ":" in text else text
        out = set()
        for part in payload.lower().split(","):
            item = " ".join(part.strip().split())
            if item and len(item) <= 30 and re.fullmatch(r"[a-z][a-z -]*[a-z]", item):
                out.add(item)
        return out

    @staticmethod
    def _parse_valid_actions(raw: str) -> list[str]:
        try:
            payload = json.loads(raw)
            actions = payload.get("valid_actions", []) if isinstance(payload, dict) else []
        except Exception:
            return []
        if not isinstance(actions, list):
            return []

        out, seen = [], set()
        for item in actions:
            a = " ".join(str(item).strip().lower().split())
            if not a or a in seen or len(a) > 48:
                continue
            if a in DISALLOWED_ACTIONS or StudentAgent._unsafe_action(a) or StudentAgent._incomplete_action(a):
                continue
            seen.add(a)
            out.append(a)
            if len(out) >= 36:
                break
        return out

    @staticmethod
    def _valid_actions_usable(actions: list[str]) -> bool:
        return bool(actions) and any(a in MOVE_ACTIONS for a in actions)

    @staticmethod
    def _extract_score(text: str) -> int:
        for pat in (r"\[score:\s*(\d+)", r"score[:\s]+(\d+)"):
            m = re.search(pat, text, flags=re.IGNORECASE)
            if m:
                return int(m.group(1))
        return 0

    @staticmethod
    def _sig(text: str) -> str:
        return re.sub(r"\s+", " ", (text or "").strip().lower())[:320]

    def _loc(self, obs: str, meta: dict) -> str:
        if isinstance(meta, dict):
            loc = str(meta.get("location", "")).strip()
            if loc:
                return self._canon_loc(loc)
        lines = [ln.strip() for ln in obs.splitlines() if ln.strip()]
        if not lines:
            return "Unknown"
        first = lines[0]
        if len(first) <= 70 and not first.endswith((".", "!", "?")):
            return self._canon_loc(first)
        sent = re.split(r"[.!?]", first, maxsplit=1)[0].strip()
        return self._canon_loc(sent[:64] if sent else "Unknown")

    def _visit_loc(self, obs: str, meta: dict) -> str:
        base = self._loc(obs, meta)
        sig = self._sig(obs)
        if not sig:
            return base
        h = hashlib.sha1(sig.encode("utf-8")).hexdigest()[:8]
        action = ""
        if isinstance(meta, dict):
            action = self._norm(str(meta.get("action", "")))
        head = action.split()[0] if action else ""
        streak = min(self.no_progress_streak, 9)
        linger = min(self.same_location_streak, 9)
        return f"{base}@{h}:{head}:{streak}:{linger}" if head else f"{base}@{h}:{streak}:{linger}"

    @staticmethod
    def _canon_loc(loc: str) -> str:
        text = " ".join((loc or "Unknown").strip().split())
        parts = text.split("#")
        if len(parts) >= 3:
            tail = parts[-1]
            if tail.isdigit():
                return f"{parts[0]}#{tail}"
        return text

    @staticmethod
    def _done(obs: str, meta: dict) -> bool:
        if isinstance(meta, dict) and bool(meta.get("done", False)):
            return True
        low = obs.lower()
        return any(x in low for x in ("game over", "you have died", "you are dead", "the end"))

    @staticmethod
    def _negative(text: str) -> bool:
        low = text.lower()
        return any(p in low for p in NEGATIVE_PATTERNS)

    @staticmethod
    def _extract_promising_actions(obs: str) -> list[str]:
        low = obs.lower()
        out = []
        for m in re.finditer(r'"([a-z][a-z ]{1,36})"', low):
            out.append(" ".join(m.group(1).split()))
        for pat in (r"(?:try|perhaps|maybe|you could|you can)\s+([a-z]+(?:\s+[a-z]+){0,3})",):
            for m in re.finditer(pat, low):
                out.append(" ".join(m.group(1).split()))

        dedup, seen = [], set()
        for a in out:
            a = StudentAgent._norm(a)
            if not a or a in seen or a in DISALLOWED_ACTIONS or len(a) > 48:
                continue
            seen.add(a)
            dedup.append(a)
        return dedup[:10]

    @staticmethod
    def _extract_followup_actions(obs: str) -> list[str]:
        low = obs.lower()
        out = []

        for d in MOVE_ACTIONS:
            if re.search(rf"\b{d}\b", low):
                out.append(d)

        patterns = (
            r"(?:allow|allows|allowed|can|could|may|might)\s+(?:you\s+)?([a-z]+(?:\s+[a-z]+){0,2})",
            r"(?:lets?|enable|enables)\s+(?:you\s+)?(?:to\s+)?([a-z]+(?:\s+[a-z]+){0,2})",
        )
        for pat in patterns:
            for m in re.finditer(pat, low):
                cand = " ".join(m.group(1).split())
                if cand == "entry":
                    cand = "enter"
                out.append(cand)

        dedup, seen = [], set()
        for a in out:
            a = StudentAgent._norm(a)
            if not a or a in seen or len(a) > 48:
                continue
            if a in DISALLOWED_ACTIONS or StudentAgent._unsafe_action(a) or StudentAgent._incomplete_action(a):
                continue
            seen.add(a)
            dedup.append(a)
        return dedup[:10]

    @staticmethod
    def _extract_objects(obs: str) -> set[str]:
        stop = {
            "you",
            "your",
            "there",
            "here",
            "north",
            "south",
            "east",
            "west",
            "up",
            "down",
            "room",
            "path",
            "road",
            "forest",
            "field",
            "farm",
            "night",
            "dark",
            "sky",
            "moon",
            "place",
            "some",
        }
        low = obs.lower()
        out = set()
        patterns = (
            r"(?:there is|there are|you see|you can see)\s+(?:a|an|the|some)?\s*([a-z][a-z\- ]{1,30})",
            r"\b(?:a|an|the)\s+([a-z][a-z\- ]{1,22})\s+(?:is|are|lies|sits|stands)\b",
            r"\bin\s+([a-z]{4,20})\b",
        )
        for pat in patterns:
            for m in re.finditer(pat, low):
                cand = " ".join(m.group(1).split())
                cand = re.split(r"\b(?:here|there|that|which|who|with|near|on|in|from|to)\b", cand, maxsplit=1)[0].strip()
                if cand and cand not in stop and len(cand) <= 30:
                    out.add(cand)
        return out

    def _candidate_verbs(self, valid_actions: list[str]) -> list[str]:
        out, seen = [], set()
        for action in valid_actions:
            head = action.split()[0] if action.split() else ""
            if not head or head in MOVE_ACTIONS or head in DISALLOWED_ACTIONS:
                continue
            if head in seen:
                continue
            seen.add(head)
            out.append(head)
            if len(out) >= 8:
                break

        ranked_global = sorted(
            self.global_stats.items(),
            key=lambda kv: (kv[1].score_gain, kv[1].success - kv[1].fail),
            reverse=True,
        )
        for action, _ in ranked_global:
            head = action.split()[0] if action.split() else ""
            if head and head not in MOVE_ACTIONS and head not in DISALLOWED_ACTIONS and head not in seen:
                seen.add(head)
                out.append(head)
            if len(out) >= 10:
                break

        for fallback in ("examine", "search", "open", "take", "read", "use", "listen"):
            if fallback not in seen:
                out.append(fallback)
        return out[:10]