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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
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
"""W2-C β€” agent-hardening tests (pyUltroid/agent + plugins/brain.py contract).

Covers:

* W2-C.1 β€” provider-abstraction unit tests with mocked SDKs/HTTP for
  OpenAI, Anthropic, Gemini (google-genai), Groq, Ollama and the
  OpenAI-compatible endpoint. No network, no real SDK imports: each
  provider imports its SDK lazily inside the method, so tests inject fakes
  via ``sys.modules`` (openai/anthropic/groq/google.genai) or monkeypatch
  ``aiohttp.ClientSession`` (Ollama).
* W2-C.2 β€” Ollama is the default local provider path: FREE tier is tried
  first for every non-tool request, whatever the intent's target tier;
  tool-call requests deliberately skip the local tier.
* W2-C.3 β€” MiniAI's response cache lives in udB ({"exp", "v"} records with
  a timestamp TTL), uses identifier-safe keys, and disables cleanly when
  udB is absent. No dedicated redis client exists on MiniAI anymore.
* W2-C.4 β€” ToolExecutor capability enforcement: per-tool required caps,
  arg-level UPDATE_APPLY escalation, shell cap forwarding and the
  shell_execute timeout clamp.
* W2-C.5 β€” AgentLifecycle registration into the universal plugin lifecycle
  (idempotent, no-op without the runtime), plus start/stop hook behaviour.

Style follows the existing suite: namespace-isolated ``pyUltroid`` stubs
(see ``tests/test_core_architecture.py`` / ``tests/test_updater_engine.py``)
and plain ``asyncio.run`` β€” no pytest-asyncio dependency.
"""

from __future__ import annotations

import asyncio
import importlib
import json
import logging
import sys
import time
import types
from pathlib import Path
from types import SimpleNamespace

import pytest

ROOT = Path(__file__).resolve().parents[1]


# ── Namespace isolation ─────────────────────────────────────────────────────


class FakeUdB:
    """Dict-backed stand-in for pyUltroid's _BaseDatabase key API."""

    def __init__(self, initial=None):
        self.store = dict(initial or {})

    def get_key(self, key):
        return self.store.get(key)

    def set_key(self, key, value, cache_only=False):
        self.store[key] = value
        return True

    def del_key(self, key):
        self.store.pop(key, None)
        return True

    def keys(self):
        return list(self.store)


def _install_stub_namespace():
    """Stub the pyUltroid root + startup.logger + configs so agent modules
    import without the production bootstrap. The real pyUltroid.shell package
    (import-light) loads through the stub root's __path__."""
    root = types.ModuleType("pyUltroid")
    root.__path__ = [str(ROOT / "pyUltroid")]
    root.LOGS = logging.getLogger("agent-tests")
    root.asst = None
    root.ultroid_bot = None
    root.udB = None
    sys.modules["pyUltroid"] = root

    startup = types.ModuleType("pyUltroid.startup")
    startup.__path__ = []
    logger = types.ModuleType("pyUltroid.startup.logger")
    logger.LOGS = root.LOGS
    sys.modules["pyUltroid.startup"] = startup
    sys.modules["pyUltroid.startup.logger"] = logger

    configs = types.ModuleType("pyUltroid.configs")
    configs.Var = SimpleNamespace()  # no AI keys β€” providers start unconfigured
    sys.modules["pyUltroid.configs"] = configs
    return root


@pytest.fixture
def agent_modules(module_isolation):
    """Import pyUltroid.agent fresh inside an isolated, stubbed namespace."""
    with module_isolation("pyUltroid"):
        root = _install_stub_namespace()
        importlib.import_module("pyUltroid.agent")
        yield SimpleNamespace(
            root=root,
            pkg=sys.modules["pyUltroid.agent"],
                    providers=sys.modules["pyUltroid.agent._providers"],
            mini_mod=sys.modules["pyUltroid.agent._mini"],
            tools_mod=sys.modules["pyUltroid.agent._tools"],
            agent_mod=sys.modules["pyUltroid.agent._agent"],
            lifecycle=sys.modules["pyUltroid.agent._lifecycle"],
        )


# ── SDK fakes ───────────────────────────────────────────────────────────────


def _fake_openai_module(captured, content="hello from openai", tool_calls=None):
    """Fake `openai` module: AsyncOpenAI(**kw).chat.completions.create(**kw)."""
    mod = types.ModuleType("openai")

    class FakeCompletions:
        async def create(self, **kwargs):
            captured["create_kwargs"] = kwargs
            msg = SimpleNamespace(content=content, tool_calls=tool_calls)
            return SimpleNamespace(
                choices=[SimpleNamespace(message=msg)],
                usage=SimpleNamespace(prompt_tokens=11, completion_tokens=7),
            )

    class FakeAsyncOpenAI:
        def __init__(self, **kwargs):
            captured["client_kwargs"] = kwargs
            self.chat = SimpleNamespace(completions=FakeCompletions())

    mod.AsyncOpenAI = FakeAsyncOpenAI
    return mod


def _fake_anthropic_module(captured, text="hello from claude", tool_use=None):
    mod = types.ModuleType("anthropic")

    class FakeMessages:
        async def create(self, **kwargs):
            captured["create_kwargs"] = kwargs
            blocks = [SimpleNamespace(type="text", text=text)]
            if tool_use:
                blocks.append(SimpleNamespace(**tool_use))  # no .text attr
            return SimpleNamespace(
                content=blocks,
                usage=SimpleNamespace(input_tokens=9, output_tokens=4),
            )

    class FakeAsyncAnthropic:
        def __init__(self, **kwargs):
            captured["client_kwargs"] = kwargs
            self.messages = FakeMessages()

    mod.AsyncAnthropic = FakeAsyncAnthropic
    return mod


def _fake_google_genai(captured, text="hello from gemini", stream_chunks=("a", "b")):
    """Fake `google` + `google.genai` modules for the google-genai SDK."""
    genai = types.ModuleType("google.genai")

    class _Stream:
        def __aiter__(self):
            async def gen():
                for chunk in stream_chunks:
                    yield SimpleNamespace(text=chunk)

            return gen()

    class FakeModels:
        async def generate_content(self, model=None, contents=None, config=None):
            captured["model"] = model
            captured["contents"] = contents
            captured["config"] = config
            return SimpleNamespace(
                text=text,
                usage_metadata=SimpleNamespace(
                    prompt_token_count=6,
                    candidates_token_count=2,
                ),
            )

        async def generate_content_stream(self, model=None, contents=None, config=None):
            captured["stream_model"] = model
            return _Stream()

    class FakeClient:
        def __init__(self, api_key=None):
            captured["api_key"] = api_key
            self.aio = SimpleNamespace(models=FakeModels())

    genai.Client = FakeClient

    google = types.ModuleType("google")
    google.genai = genai
    google.__path__ = []  # mark as package for `from google import genai`
    return {"google": google, "google.genai": genai}


def _fake_groq_module(captured, content="hello from groq"):
    mod = types.ModuleType("groq")

    class FakeCompletions:
        async def create(self, **kwargs):
            captured["create_kwargs"] = kwargs
            msg = SimpleNamespace(content=content)
            return SimpleNamespace(
                choices=[SimpleNamespace(message=msg)],
                usage=SimpleNamespace(prompt_tokens=5, completion_tokens=3),
            )

    class FakeAsyncGroq:
        def __init__(self, **kwargs):
            captured["client_kwargs"] = kwargs
            self.chat = SimpleNamespace(completions=FakeCompletions())

    mod.AsyncGroq = FakeAsyncGroq
    return mod


class _FakeAioContent:
    """Async iterator over response body lines (Ollama streaming)."""

    def __init__(self, lines):
        self._lines = lines

    def __aiter__(self):
        async def gen():
            for line in self._lines:
                yield line

        return gen()


class _FakeAioResp:
    def __init__(self, data=None, status=200, lines=()):
        self._data = data or {}
        self.status = status
        self.content = _FakeAioContent(lines)

    async def __aenter__(self):
        return self

    async def __aexit__(self, *exc):
        return False

    async def json(self):
        return self._data


class FakeAioSession:
    """Stand-in for aiohttp.ClientSession. Class attrs drive behaviour."""

    tags_status = 200
    chat_data: dict = {}
    chat_lines: tuple = ()
    calls: list = []

    def __init__(self, *args, **kwargs):
        pass

    async def __aenter__(self):
        return self

    async def __aexit__(self, *exc):
        return False

    def get(self, url, timeout=None):
        self.calls.append(("GET", url))
        return _FakeAioResp(status=self.tags_status)

    def post(self, url, json=None, timeout=None):
        self.calls.append(("POST", url, json))
        return _FakeAioResp(data=dict(self.chat_data), lines=self.chat_lines)


@pytest.fixture
def fake_aiohttp(monkeypatch):
    import aiohttp

    FakeAioSession.calls = []
    FakeAioSession.tags_status = 200
    FakeAioSession.chat_data = {}
    FakeAioSession.chat_lines = ()
    monkeypatch.setattr(aiohttp, "ClientSession", FakeAioSession)
    return FakeAioSession


# ── W2-C.1 β€” provider: registry + import safety ─────────────────────────────


def test_registry_lists_all_supported_providers(agent_modules):
    p = agent_modules.providers
    registry = p.ProviderRegistry()
    status = registry.provider_health()
    assert {
        "ollama",
        "openai_compat",
        "groq",
        "gemini",
        "apimix",
        "openrouter",
        "openai",
        "anthropic",
    } <= set(status)
    names = [provider.name for provider in registry._all]
    assert names.index("apimix") < names.index("openrouter")
    # Import-safety: with no keys configured only Ollama reports configured
    # (it self-checks reachability later), and nothing raised on import.
    assert status["ollama"] is True
    assert all(v is False for k, v in status.items() if k != "ollama")
    # W2-C.2: the cheapest provider is the local one.
    assert p.ProviderRegistry().provider_cheapest().name == "ollama"


def test_unconfigured_providers_return_error_not_raise(agent_modules):
    p = agent_modules.providers
    msgs = [p.AIMessage(role="user", content="hi")]
    for name, missing in [
        ("openai", "OPENAI_API_KEY"),
        ("anthropic", "ANTHROPIC_API_KEY"),
        ("gemini", "GEMINI_API_KEY"),
        ("groq", "GROQ_API_KEY"),
        ("openai_compat", "COMPAT_BASE_URL"),
    ]:
        provider = p.ProviderRegistry().provider_by_name(name)
        resp = asyncio.run(provider.complete(msgs))
        assert not resp.ok
        assert missing in resp.error


# ── W2-C.1 β€” provider: Ollama ────────────────────────────────────────────────


def test_ollama_complete_ok(agent_modules, fake_aiohttp):
    p = agent_modules.providers
    ollama = p.ProviderRegistry().provider_by_name("ollama")
    fake_aiohttp.chat_data = {
        "message": {"content": "local says hi"},
        "prompt_eval_count": 3,
        "eval_count": 5,
    }
    resp = asyncio.run(
        ollama.complete(
            [p.AIMessage(role="user", content="hello")],
            system="be brief",
        )
    )
    assert resp.ok
    assert resp.content == "local says hi"
    assert resp.provider == "ollama"
    assert resp.model == "llama3"  # built-in default
    assert resp.tokens_in == 3 and resp.tokens_out == 5
    post = [c for c in fake_aiohttp.calls if c[0] == "POST"][0]
    assert post[1].endswith("/api/chat")
    assert post[2]["system"] == "be brief"
    assert post[2]["stream"] is False


def test_ollama_not_running(agent_modules, fake_aiohttp):
    fake_aiohttp.tags_status = 500
    p = agent_modules.providers
    ollama = p.ProviderRegistry().provider_by_name("ollama")
    resp = asyncio.run(ollama.complete([p.AIMessage(role="user", content="hi")]))
    assert not resp.ok
    assert resp.error == "Ollama not running"


def test_ollama_stream_ok(agent_modules, fake_aiohttp):
    fake_aiohttp.chat_lines = (
        json.dumps({"message": {"content": "he"}}).encode(),
        json.dumps({"message": {"content": "y"}, "done": True}).encode(),
    )
    p = agent_modules.providers
    ollama = p.ProviderRegistry().provider_by_name("ollama")

    async def collect():
        return [
            c async for c in ollama.stream([p.AIMessage(role="user", content="hi")])
        ]

    assert asyncio.run(collect()) == ["he", "y"]


def test_ollama_default_model_resolves_udb_then_var(agent_modules):
    """W2-C.2: `.ollama use` writes the unified AGENT key β€” the provider reads
    it back. The legacy OLLAMA_MODEL name is migration-only input."""
    p = agent_modules.providers
    fresh = p.OllamaProvider()
    assert fresh.default_model == "llama3"  # built-in default

    agent_modules.root.udB = FakeUdB({"AGENT_LLAMA_OLLAMA_MODEL": "mistral"})
    fresh2 = p.OllamaProvider()
    assert fresh2.default_model == "mistral"  # the database wins


def test_ollama_default_model_observes_udb_changes(agent_modules):
    """Regression: the getter must NOT memoize the udB read β€” a later udB
    change is observed on the SAME provider instance."""
    p = agent_modules.providers
    agent_modules.root.udB = FakeUdB({"AGENT_LLAMA_OLLAMA_MODEL": "mistral"})
    provider = p.OllamaProvider()
    assert provider.default_model == "mistral"  # first read

    agent_modules.root.udB.set_key("AGENT_LLAMA_OLLAMA_MODEL", "qwen2")
    assert provider.default_model == "qwen2"  # observed live

    # Setter = in-process fallback, consulted only when the database has no key.
    agent_modules.root.udB.del_key("AGENT_LLAMA_OLLAMA_MODEL")
    provider.default_model = "phi3"  # brain.py setter path
    assert provider.default_model == "phi3"

    # A re-appearing database key is the source of truth again.
    agent_modules.root.udB.set_key("AGENT_LLAMA_OLLAMA_MODEL", "llama3.1")
    assert provider.default_model == "llama3.1"


# ── W2-C.1 β€” provider: OpenAI ────────────────────────────────────────────────


def test_openai_complete_ok_with_tool_calls(agent_modules, monkeypatch):
    p = agent_modules.providers
    agent_modules.root.udB = FakeUdB(
        {
            "AGENT_CLOUD_OPENAI_KEY": "sk-test",
            "AGENT_CLOUD_OPENAI_MODEL": "gpt-4o-mini",
        }
    )
    captured = {}
    tc = SimpleNamespace(
        id="call1",
        function=SimpleNamespace(name="shell_execute", arguments='{"command": "ls"}'),
    )
    monkeypatch.setitem(
        sys.modules, "openai", _fake_openai_module(captured, tool_calls=[tc])
    )

    resp = asyncio.run(
        p.ProviderRegistry().provider_by_name("openai").complete(
            [p.AIMessage(role="user", content="list files")],
            tools=[{"type": "function", "function": {"name": "shell_execute"}}],
            system="sys",
        )
    )
    assert resp.ok and resp.provider == "openai"
    assert resp.tokens_in == 11 and resp.tokens_out == 7
    assert resp.tool_calls == [
        {"id": "call1", "name": "shell_execute", "arguments": '{"command": "ls"}'}
    ]
    assert captured["client_kwargs"]["api_key"] == "sk-test"
    assert captured["create_kwargs"]["messages"][0] == {
        "role": "system",
        "content": "sys",
    }
    assert captured["create_kwargs"]["tool_choice"] == "auto"


# ── W2-C.1 β€” provider: Anthropic ─────────────────────────────────────────────


def test_anthropic_complete_ok_with_tool_use(agent_modules, monkeypatch):
    p = agent_modules.providers
    provider = p.ProviderRegistry().provider_by_name("anthropic")
    provider.api_key = "sk-ant-test"  # Var snapshot was empty; set directly
    captured = {}
    monkeypatch.setitem(
        sys.modules,
        "anthropic",
        _fake_anthropic_module(
            captured,
            tool_use={
                "type": "tool_use",
                "id": "t1",
                "name": "web_search",
                "input": {"query": "x"},
            },
        ),
    )

    msgs = [
        p.AIMessage(role="system", content="from-history"),
        p.AIMessage(role="user", content="search x"),
    ]
    resp = asyncio.run(provider.complete(msgs))
    assert resp.ok and resp.content == "hello from claude"
    assert resp.tokens_in == 9 and resp.tokens_out == 4
    # tool_use blocks normalise to the OpenAI-shaped {"id", "name", "arguments"}
    assert resp.tool_calls == [
        {"id": "t1", "name": "web_search", "arguments": {"query": "x"}}
    ]
    sent = captured["create_kwargs"]
    # system extracted from history; system-role messages not in messages list
    assert sent["system"] == "from-history"
    assert all(m["role"] != "system" for m in sent["messages"])
    assert captured["client_kwargs"]["api_key"] == "sk-ant-test"


# ── W2-C.1 β€” provider: Gemini (google-genai) ────────────────────────────────


def test_gemini_complete_ok_google_genai(agent_modules, monkeypatch):
    p = agent_modules.providers
    provider = p.ProviderRegistry().provider_by_name("gemini")
    provider.api_key = "AIza-test"
    captured = {}
    for name, module in _fake_google_genai(captured).items():
        monkeypatch.setitem(sys.modules, name, module)

    msgs = [
        p.AIMessage(role="system", content="be terse"),
        p.AIMessage(role="user", content="hi"),
        p.AIMessage(role="assistant", content="hello"),
        p.AIMessage(role="user", content="how are you"),
    ]
    resp = asyncio.run(provider.complete(msgs, max_tokens=128, temperature=0.2))
    assert resp.ok and resp.content == "hello from gemini"
    assert resp.tokens_in == 6 and resp.tokens_out == 2
    assert captured["api_key"] == "AIza-test"
    # google-genai call shape: contents carry user/model roles, system goes
    # into config.system_instruction.
    roles = [c["role"] for c in captured["contents"]]
    assert roles == ["user", "model", "user"]
    assert captured["config"]["system_instruction"] == "be terse"
    assert captured["config"]["max_output_tokens"] == 128


def test_gemini_stream_ok(agent_modules, monkeypatch):
    p = agent_modules.providers
    provider = p.ProviderRegistry().provider_by_name("gemini")
    provider.api_key = "AIza-test"
    captured = {}
    for name, module in _fake_google_genai(
        captured, stream_chunks=("ge", "mini")
    ).items():
        monkeypatch.setitem(sys.modules, name, module)

    async def collect():
        return [
            c async for c in provider.stream([p.AIMessage(role="user", content="hi")])
        ]

    assert asyncio.run(collect()) == ["ge", "mini"]


def test_gemini_does_not_import_eol_package(agent_modules):
    """The EOL google.generativeai SDK must not appear anywhere in the module."""
    import inspect

    src = inspect.getsource(agent_modules.providers)
    assert "google.generativeai" not in src
    assert "generativeai" not in src


# ── W2-C.1 β€” provider: Groq ──────────────────────────────────────────────────


def test_groq_complete_ok(agent_modules, monkeypatch):
    p = agent_modules.providers
    provider = p.ProviderRegistry().provider_by_name("groq")
    provider.api_key = "gsk-test"
    captured = {}
    monkeypatch.setitem(sys.modules, "groq", _fake_groq_module(captured))

    resp = asyncio.run(
        provider.complete(
            [p.AIMessage(role="user", content="hi")],
            system="sys",
        )
    )
    assert resp.ok and resp.content == "hello from groq"
    assert resp.tokens_in == 5 and resp.tokens_out == 3
    assert captured["client_kwargs"]["api_key"] == "gsk-test"
    assert captured["create_kwargs"]["messages"][0] == {
        "role": "system",
        "content": "sys",
    }


# ── W2-C.1 β€” provider: OpenAI-compatible ────────────────────────────────────


def test_openai_compat_complete_ok(agent_modules, monkeypatch):
    p = agent_modules.providers
    provider = p.ProviderRegistry().provider_by_name("openai_compat")
    provider.base_url = "http://localhost:1234/v1"
    captured = {}
    monkeypatch.setitem(sys.modules, "openai", _fake_openai_module(captured))

    resp = asyncio.run(provider.complete([p.AIMessage(role="user", content="hi")]))
    assert resp.ok and resp.content == "hello from openai"
    assert captured["client_kwargs"]["base_url"] == "http://localhost:1234/v1"
    assert captured["client_kwargs"]["api_key"] == "none"  # built-in default


# ── W2-C.2 β€” local-first routing ─────────────────────────────────────────────


def test_build_order_local_first_for_non_tool_requests(agent_modules):
    """Regression: previously tiers below min_tier were excluded, so a
    CODE_HELP/UNKNOWN request never tried Ollama and failed when no cloud
    keys were set."""
    # Configure one cloud provider so the escalation tier is populated.
    agent_modules.root.udB = FakeUdB({"AGENT_CLOUD_OPENAI_KEY": "sk-test"})
    mini = agent_modules.mini_mod.MiniAI()
    CostTier = agent_modules.providers.CostTier
    order = mini._build_order(CostTier.MEDIUM, allow_local=True)
    names = [p.name for p in order]
    assert names[0] == "ollama"  # FREE tier first
    assert names.count("ollama") == 1  # deduped
    assert "openai" in names  # escalation still present
    assert names.index("ollama") < names.index("openai")


def test_build_order_skips_local_for_tool_requests(agent_modules):
    mini = agent_modules.mini_mod.MiniAI()
    CostTier = agent_modules.providers.CostTier
    order = mini._build_order(CostTier.MEDIUM, allow_local=False)
    assert all(p.cost_tier != CostTier.FREE for p in order)


def test_forced_unconfigured_provider_never_silently_falls_back(agent_modules):
    p = agent_modules.providers
    mini = agent_modules.mini_mod.MiniAI()
    resp = asyncio.run(
        mini.mini_respond(
            [p.AIMessage(role="user", content="hello")],
            force_provider="openai",
            use_cache=False,
        )
    )
    assert not resp.ok
    assert resp.provider == "openai"
    assert "not configured" in resp.error


def test_forced_provider_failure_is_returned(agent_modules, monkeypatch):
    p = agent_modules.providers
    mini = agent_modules.mini_mod.MiniAI()
    async def fail(*args, **kwargs):
        return p.AIResponse("", "openai", "m", error="upstream down")

    # Patched on the class: the registry is no longer a module singleton, so a
    # fresh MiniAI builds its own provider instances.
    monkeypatch.setattr(p.OpenAIProvider, "is_configured", lambda self: True)
    monkeypatch.setattr(p.OpenAIProvider, "complete", fail)
    resp = asyncio.run(
        mini.mini_respond(
            [p.AIMessage(role="user", content="hello")],
            force_provider="openai",
            use_cache=False,
        )
    )
    assert not resp.ok
    assert resp.provider == "openai"
    assert resp.error == "upstream down"


def test_respond_routes_to_ollama_by_default(agent_modules, fake_aiohttp):
    """End-to-end W2-C.2: no API keys, Ollama up β†’ .ai-style request succeeds
    via the local provider even for a non-trivial intent."""
    fake_aiohttp.chat_data = {"message": {"content": "local answer"}}
    mini = agent_modules.mini_mod.MiniAI()
    msgs = [
        agent_modules.providers.AIMessage(
            role="user", content="write a python function"
        )
    ]
    resp = asyncio.run(mini.mini_respond(msgs, use_cache=False))
    assert resp.ok
    assert resp.provider == "ollama"
    assert resp.content == "local answer"


def test_agent_respond_end_to_end_via_local(agent_modules, fake_aiohttp):
    fake_aiohttp.chat_data = {"message": {"content": "agent local answer"}}
    agent = agent_modules.agent_mod.UltroidAgent()
    resp = asyncio.run(
        agent.respond(
            "explain this code",
            chat_id=4242,
            use_tools=False,
            use_cache=False,
        )
    )
    assert resp.ok
    assert resp.provider == "ollama"
    # History recorded for the chat
    hist = agent.history(4242).get()
    assert [m.role for m in hist] == ["user", "assistant"]


# ── W2-C.3 β€” udB response cache ──────────────────────────────────────────────


def test_cache_roundtrip_through_udb(agent_modules, monkeypatch):
    p = agent_modules.providers
    mini_mod = agent_modules.mini_mod
    mini = mini_mod.MiniAI()
    agent_modules.root.udB = FakeUdB()

    calls = {"n": 0}

    async def fake_route(**kwargs):
        calls["n"] += 1
        return p.AIResponse("cached answer", "fake", "fake-1")

    monkeypatch.setattr(mini, "_route", fake_route)
    msgs = [p.AIMessage(role="user", content="what is the time")]

    first = asyncio.run(mini.mini_respond(msgs))
    assert first.provider == "fake" and calls["n"] == 1

    # Record shape: {"exp": ts, "v": content} under an identifier-safe key.
    keys = [
        k for k in agent_modules.root.udB.keys() if k.startswith(mini.CACHE_KEY_PFX)
    ]
    assert len(keys) == 1 and ":" not in keys[0]
    record = agent_modules.root.udB.get_key(keys[0])
    assert record["v"] == "cached answer"
    assert record["exp"] > time.time()

    second = asyncio.run(mini.mini_respond(list(msgs)))
    assert second.provider == "cache" and second.model == "udb"
    assert second.content == "cached answer"
    assert calls["n"] == 1  # served from cache, no second route


def test_cache_expired_entry_is_dropped(agent_modules):
    mini = agent_modules.mini_mod.MiniAI()
    p = agent_modules.providers
    agent_modules.root.udB = FakeUdB()
    key = agent_modules.mini_mod._cache_key([p.AIMessage(role="user", content="stale")])
    agent_modules.root.udB.set_key(key, {"exp": time.time() - 5, "v": "old"})

    assert mini._cache_get(key) is None
    assert agent_modules.root.udB.get_key(key) is None  # lazily deleted


def test_cache_set_skips_long_values(agent_modules):
    mini = agent_modules.mini_mod.MiniAI()
    agent_modules.root.udB = FakeUdB()
    mini._cache_set("BRAIN_CACHE_x", "y" * (mini.CACHE_MAX_LEN + 1))
    assert agent_modules.root.udB.get_key("BRAIN_CACHE_x") is None


def test_cache_disabled_cleanly_without_udb(agent_modules):
    mini = agent_modules.mini_mod.MiniAI()
    assert agent_modules.root.udB is None
    assert mini._cache_get("BRAIN_CACHE_none") is None
    mini._cache_set("BRAIN_CACHE_none", "v")  # no-op, no raise
    assert asyncio.run(mini.mini_cache_size()) == 0
    assert asyncio.run(mini.mini_flush_cache()) == 0


def test_flush_cache_counts_only_brain_keys(agent_modules):
    mini = agent_modules.mini_mod.MiniAI()
    agent_modules.root.udB = FakeUdB(
        {
            "BRAIN_CACHE_a": {"exp": time.time() + 60, "v": "1"},
            "BRAIN_CACHE_b": {"exp": time.time() + 60, "v": "2"},
            "SUDO_USERS": [1],
        }
    )
    assert asyncio.run(mini.mini_cache_size()) == 2
    assert asyncio.run(mini.mini_flush_cache()) == 2
    assert agent_modules.root.udB.get_key("SUDO_USERS") == [1]  # untouched


def test_builtin_greeting_short_circuits_before_cache(agent_modules, monkeypatch):
    p = agent_modules.providers
    mini = agent_modules.mini_mod.MiniAI()
    agent_modules.root.udB = FakeUdB()

    async def boom(**kwargs):
        raise AssertionError("routing must not run for builtin intents")

    monkeypatch.setattr(mini, "_route", boom)
    resp = asyncio.run(mini.mini_respond([p.AIMessage(role="user", content="hello")]))
    assert resp.provider == "builtin"


# ── W2-C.4 β€” capability policy ───────────────────────────────────────────────


def test_clamp_shell_timeout(agent_modules):
    clamp = agent_modules.tools_mod.clamp_shell_timeout
    assert clamp(9999) == 120.0
    assert clamp(120) == 120.0
    assert clamp(-5) == 1.0
    assert clamp("junk") == 60.0
    assert clamp(None) == 60.0


def test_unknown_tool_still_errors(agent_modules):
    ex = agent_modules.tools_mod.make_executor()
    result = asyncio.run(ex.execute("nope_tool", {}))
    assert "Unknown tool" in result["error"]


def test_denied_tool_never_imports_target_system(agent_modules):
    """install_package without Cap.RES_INSTALL must be refused BEFORE the
    extender (HRE) is imported."""
    tools = agent_modules.tools_mod
    ex = tools.make_executor(caps=set())  # no caps at all
    sys.modules.pop("pyUltroid.extender", None)
    result = asyncio.run(ex.execute("install_package", {"package": "x"}))
    assert result["denied"] is True
    assert "res.install" in result["missing_caps"]
    assert "pyUltroid.extender" not in sys.modules


def test_update_check_apply_requires_update_apply(agent_modules):
    tools = agent_modules.tools_mod
    Cap = tools.Cap
    ex = tools.make_executor()  # DEFAULT_AGENT_CAPS β€” no UPDATE_APPLY
    denied = asyncio.run(ex.execute("update_check", {"apply": True}))
    assert denied["denied"] is True
    assert Cap.UPDATE_APPLY in denied["missing_caps"]
    # …but the read-only check stays within the default grant.
    assert ex._missing_caps("update_check", {"apply": False}) == []


def test_install_package_denied_by_default_and_dispatchable_with_grant(
    agent_modules, monkeypatch
):
    """Regression: RES_INSTALL left the default grant β€” on-demand pip installs
    are supply-chain-sensitive and require an explicit grant."""
    tools = agent_modules.tools_mod
    Cap = tools.Cap
    assert Cap.RES_INSTALL not in tools.DEFAULT_AGENT_CAPS

    ex = tools.make_executor()  # default grant
    sys.modules.pop("pyUltroid.extender", None)
    denied = asyncio.run(ex.execute("install_package", {"package": "pillow"}))
    assert denied["denied"] is True
    assert Cap.RES_INSTALL in denied["missing_caps"]
    assert "pyUltroid.extender" not in sys.modules  # refused before any import

    extender_mod = types.ModuleType("pyUltroid.extender")

    class FakeHRE:
        async def install_dep(self, package, plugin=None):
            return SimpleNamespace(
                installed=[package],
                skipped=[],
                failed=[],
                success=True,
            )

    extender_mod.ex = SimpleNamespace(resources=FakeHRE())
    monkeypatch.setitem(sys.modules, "pyUltroid.extender", extender_mod)

    granted = tools.make_executor(
        caps=set(tools.DEFAULT_AGENT_CAPS) | {Cap.RES_INSTALL}
    )
    result = asyncio.run(granted.execute("install_package", {"package": "pillow"}))
    assert result["success"] is True
    assert result["installed"] == ["pillow"]


def test_update_check_apply_allowed_with_explicit_grant(agent_modules, monkeypatch):
    """Granting UPDATE_APPLY lets dispatch proceed (proven via a stub updater)."""
    tools = agent_modules.tools_mod
    Cap = tools.Cap

    updater_mod = types.ModuleType("pyUltroid.updater")

    class FakeEngine:
        def __init__(self):
            self.applied = []

        async def check(self):
            return SimpleNamespace(
                has_changes=True,
                commit_count=1,
                needs_restart=False,
                impact_summary=lambda: "plugins changed",
                changelog_text=lambda limit: "one commit",
            )

        async def apply(self, diff):
            self.applied.append(diff)
            return SimpleNamespace(summary=lambda: "update complete")

    engine = FakeEngine()
    updater_mod.up = engine
    monkeypatch.setitem(sys.modules, "pyUltroid.updater", updater_mod)

    ex = tools.make_executor(caps=set(tools.DEFAULT_AGENT_CAPS) | {Cap.UPDATE_APPLY})
    result = asyncio.run(ex.execute("update_check", {"apply": True}))
    assert result["has_changes"] is True
    assert result["applied"] == "update complete"
    assert len(engine.applied) == 1


def test_get_bot_status_uses_unified_updater_facade(agent_modules, monkeypatch):
    tools = agent_modules.tools_mod
    updater_mod = types.ModuleType("pyUltroid.updater")
    updater_mod.up = SimpleNamespace(
        git=SimpleNamespace(
            current_sha=lambda: "abc1234",
            current_branch=lambda: "main",
        )
    )
    tracer_mod = types.ModuleType("pyUltroid.tracer")
    tracer_mod.tr = SimpleNamespace(
        index=SimpleNamespace(stats=lambda: {"plugins": 12, "handlers": 34})
    )
    monkeypatch.setitem(sys.modules, "pyUltroid.updater", updater_mod)
    monkeypatch.setitem(sys.modules, "pyUltroid.tracer", tracer_mod)

    result = asyncio.run(tools.make_executor()._tool_get_bot_status())

    assert result == {
        "version": "abc1234",
        "branch": "main",
        "plugins": 12,
        "handlers": 34,
        "uptime_info": "available via .uptime command",
    }


def test_shell_execute_denied_without_shell_exec(agent_modules):
    tools = agent_modules.tools_mod
    Cap = tools.Cap
    ex = tools.make_executor(caps={Cap.PLUGIN_READ})
    result = asyncio.run(ex.execute("shell_execute", {"command": "ls"}))
    assert result["denied"] is True
    assert Cap.SHELL_EXEC in result["missing_caps"]


def test_shell_execute_forwards_granted_caps_and_clamps(agent_modules, monkeypatch):
    """The shell router must receive exactly the executor's shell-layer caps
    (not the hardcoded broad set from before W2-C.4) and a clamped timeout."""
    tools = agent_modules.tools_mod
    shell_pkg = importlib.import_module("pyUltroid.shell")
    context_mod = importlib.import_module("pyUltroid.shell._context")

    captured = {}

    async def fake_execute(ctx, command, timeout=60.0):
        captured["ctx"] = ctx
        captured["command"] = command
        captured["timeout"] = timeout
        return SimpleNamespace(stdout="ok", stderr="", exit_code=0, cwd="/tmp", ok=True)

    monkeypatch.setattr(shell_pkg.sh, "execute", fake_execute)

    Cap = tools.Cap
    ex = tools.make_executor()  # default grant
    result = asyncio.run(
        ex.execute(
            "shell_execute",
            {"command": "ls -la", "timeout": 99999},
        )
    )
    assert result["ok"] is True
    ctx = captured["ctx"]
    assert ctx.caller == "ai"
    assert ctx.trust == context_mod.TrustLevel.AI_AGENT
    assert ctx.plugin_name == "agent.agent"
    # Only shell-layer caps forwarded, and only granted ones.
    assert ctx.caps <= tools._SHELL_LAYER_CAPS
    assert ctx.caps == frozenset(
        c for c in tools.DEFAULT_AGENT_CAPS if c in tools._SHELL_LAYER_CAPS
    )
    # Privileged caps can never leak in even if granted to the executor:
    assert captured["timeout"] == 120.0  # clamped from 99999


def test_shell_execute_strips_privileged_caps_even_if_granted(
    agent_modules, monkeypatch
):
    tools = agent_modules.tools_mod
    shell_pkg = importlib.import_module("pyUltroid.shell")
    captured = {}

    async def fake_execute(ctx, command, timeout=60.0):
        captured["ctx"] = ctx
        return SimpleNamespace(stdout="", stderr="", exit_code=0, cwd="/", ok=True)

    monkeypatch.setattr(shell_pkg.sh, "execute", fake_execute)
    Cap = tools.Cap
    ex = tools.make_executor(caps=set(tools.DEFAULT_AGENT_CAPS) | {Cap.PROC_KILL})
    asyncio.run(ex.execute("shell_execute", {"command": "ps"}))
    # PROC_KILL IS a shell-layer cap and was explicitly granted β†’ forwarded.
    assert Cap.PROC_KILL in captured["ctx"].caps
    # Non-shell caps (UPDATE_READ etc.) never reach the shell context.
    assert Cap.UPDATE_READ not in captured["ctx"].caps


def test_send_text_file_uses_in_memory_payload_and_current_chat(
    agent_modules, monkeypatch
):
    tools = agent_modules.tools_mod
    captured = {}
    bridge = types.ModuleType("pyUltroid.agent._tgbridge")

    async def send_file_via_master(file_obj, entity_ref, *, caption=None):
        captured.update(
            file=file_obj,
            chat=entity_ref,
            caption=caption,
        )
        return {"ok": True, "message_id": 123}

    bridge.send_file_via_master = send_file_via_master
    monkeypatch.setitem(sys.modules, "pyUltroid.agent._tgbridge", bridge)
    executor = tools.make_executor(
        chat_id=42, caps={tools.Cap.TG_SEND_FILE}
    )
    result = asyncio.run(
        executor.execute(
            "send_text_file",
            {
                "filename": "poem.txt",
                "content": "A short poem.\n",
                "caption": "Your poem",
            },
        )
    )

    assert result["ok"] is True
    assert result["filename"] == "poem.txt"
    assert result["message_id"] == 123
    assert captured["chat"] == 42
    assert captured["file"].name == "poem.txt"
    assert captured["file"].getvalue() == b"A short poem.\n"
    assert captured["caption"] == "Your poem"

    schema = next(
        item["function"]
        for item in tools.TOOL_SCHEMAS
        if item["function"]["name"] == "send_text_file"
    )
    assert set(schema["parameters"]["properties"]) == {
        "filename",
        "content",
        "caption",
    }


def test_send_text_file_is_permission_gated_and_rejects_paths(
    agent_modules, monkeypatch
):
    tools = agent_modules.tools_mod
    denied = asyncio.run(
        tools.make_executor(chat_id=42, caps=set()).execute(
            "send_text_file", {"filename": "poem.txt", "content": "text"}
        )
    )
    assert denied["denied"] is True
    assert tools.Cap.TG_SEND_FILE in denied["missing_caps"]

    bridge = types.ModuleType("pyUltroid.agent._tgbridge")
    bridge.send_file_via_master = lambda *args, **kwargs: pytest.fail(
        "invalid file request must not reach the Telegram bridge"
    )
    monkeypatch.setitem(sys.modules, "pyUltroid.agent._tgbridge", bridge)
    executor = tools.make_executor(
        chat_id=42, caps={tools.Cap.TG_SEND_FILE}
    )
    invalid = asyncio.run(
        executor.execute(
            "send_text_file",
            {"filename": "../.env", "content": "not a file read"},
        )
    )
    oversized = asyncio.run(
        executor.execute(
            "send_text_file",
            {"filename": "poem.txt", "content": "x" * (256 * 1024 + 1)},
        )
    )
    no_chat = asyncio.run(
        tools.make_executor(chat_id=0, caps={tools.Cap.TG_SEND_FILE}).execute(
            "send_text_file", {"filename": "poem.txt", "content": "text"}
        )
    )
    assert "without a path" in invalid["error"]
    assert "256 KiB" in oversized["error"]
    assert "no current chat" in no_chat["error"]


# ── W2-C.5 β€” lifecycle ───────────────────────────────────────────────────────


def test_lifecycle_no_universal_runtime_is_noop(agent_modules):
    lc = agent_modules.lifecycle
    assert agent_modules.root.asst is None and agent_modules.root.ultroid_bot is None
    assert lc.register_lifecycle() is False
    assert lc.lifecycle_registered() is False


def test_lifecycle_registers_idempotently_and_hooks_run(agent_modules):
    lc = agent_modules.lifecycle

    class FakeLifecycle:
        def __init__(self):
            self.plugins = []

        def register(self, plugin):
            self.plugins.append(plugin)

    fake = FakeLifecycle()
    agent_modules.root.ultroid_bot = SimpleNamespace(universal_plugins=fake)

    assert lc.register_lifecycle() is True
    assert lc.register_lifecycle() is True  # idempotent
    assert len(fake.plugins) == 1  # registered exactly once
    assert lc.lifecycle_registered() is True
    assert fake.plugins[0].id == "ultroid.agent"

    plugin = fake.plugins[0]
    asyncio.run(plugin.lifecycle_start(fake))
    assert plugin.started is True
    asyncio.run(plugin.lifecycle_start(fake))  # start is idempotent
    asyncio.run(plugin.lifecycle_stop(fake))
    assert plugin.started is False
    asyncio.run(plugin.lifecycle_stop(fake))  # stop is idempotent


def test_lifecycle_start_survives_ollama_down(agent_modules, fake_aiohttp):
    lc = agent_modules.lifecycle
    fake_aiohttp.tags_status = 500
    plugin = lc.AgentLifecycle()
    asyncio.run(plugin.lifecycle_start(None))  # must not raise
    assert plugin.started is True
    asyncio.run(plugin.lifecycle_stop(None))


def test_lifecycle_stop_wakes_waiters_with_shutdown_response(
    agent_modules, monkeypatch
):
    """Regression: stop() must stamp a well-formed shutdown AIResponse for
    every in-flight MiniAI cache key BEFORE waking waiters, and must not
    clear results early (a woken waiter would otherwise fall through and
    re-route a fresh request mid-shutdown)."""
    p = agent_modules.providers
    mini_mod = agent_modules.mini_mod
    mini = mini_mod.MiniAI()
    lc = agent_modules.lifecycle

    msgs = [p.AIMessage(role="user", content="unique in-flight question")]
    key = mini_mod._cache_key(msgs, "")

    async def scenario():
        route_started = asyncio.Event()
        route_release = asyncio.Event()

        async def fake_route(**kwargs):
            route_started.set()
            await route_release.wait()
            return p.AIResponse("real answer", "fake", "fake-1")

        monkeypatch.setattr(mini, "_route", fake_route)

        primary = asyncio.ensure_future(mini.mini_respond(list(msgs), use_cache=False))
        await route_started.wait()

        # Second identical request parks on the in-flight event.
        waiter = asyncio.ensure_future(mini.mini_respond(list(msgs), use_cache=False))
        for _ in range(10):
            await asyncio.sleep(0)
        assert key in mini._in_flight

        # The union, exactly as production: the lifecycle and the MiniAI are
        # one object, so stop() can reach the in-flight map it must wake.
        plugin = agent_modules.pkg.Agent()
        plugin._in_flight = mini._in_flight
        plugin._in_flight_results = mini._in_flight_results
        plugin.started = True
        await plugin.lifecycle_stop(None)

        # The waiter wakes with the shutdown response, not a re-route.
        waiter_resp = await asyncio.wait_for(waiter, 2)
        # Results NOT cleared too early: the stamped response is still there.
        assert mini._in_flight_results.get(key) is waiter_resp
        # No dead events left for future identical requests to hang on.
        assert key not in mini._in_flight

        route_release.set()
        primary_resp = await asyncio.wait_for(primary, 2)
        return waiter_resp, primary_resp

    waiter_resp, primary_resp = asyncio.run(scenario())
    assert waiter_resp.provider == "lifecycle"
    assert waiter_resp.model == "shutdown"
    assert waiter_resp.error and "stopped" in waiter_resp.error
    assert not waiter_resp.ok  # well-formed error response
    assert primary_resp.content == "real answer"  # in-flight primary unaffected


def test_lifecycle_shutdown_results_swept_after_delay(agent_modules):
    """The delayed sweep drops only entries still holding the shutdown
    sentinel β€” a real response that raced in for the same key is kept."""
    lc = agent_modules.lifecycle
    p = agent_modules.providers
    mini = agent_modules.mini_mod.MiniAI()

    sentinel = p.AIResponse("", "lifecycle", "shutdown", error="x")
    other = p.AIResponse("real", "prov", "model")
    mini._in_flight_results["K1"] = sentinel
    mini._in_flight_results["K2"] = sentinel
    mini._in_flight_results["K3"] = other

    asyncio.run(lc._sweep_stamped_results(mini, {"K1", "K3"}, sentinel, delay=0))

    assert "K1" not in mini._in_flight_results  # stamped sentinel swept
    assert mini._in_flight_results["K2"] is sentinel  # not stamped: untouched
    assert mini._in_flight_results["K3"] is other  # raced real resp kept


# ── Phase 1.2 β€” model-aware parameter dispatch ──────────────────────────────


def test_base_provider_default_capabilities(agent_modules):
    p = agent_modules.providers
    caps = p.BaseProvider().model_capabilities("anything")
    assert caps == {
        "max_tokens": "max_tokens",
        "temperature": True,
        "tools": True,
        "system": True,
        "stream": True,
    }


def test_openai_model_capabilities_o_series_vs_standard(agent_modules):
    p = agent_modules.providers
    provider = p.ProviderRegistry().provider_by_name("openai")
    std = provider.model_capabilities("gpt-4o")
    assert std["max_tokens"] == "max_tokens"
    assert std["temperature"] is True
    for m in ("o1", "o1-mini", "o3", "o3-mini", "o4-mini"):
        caps = provider.model_capabilities(m)
        assert caps["max_tokens"] == "max_completion_tokens", m
        assert caps["temperature"] is False, m


def test_gemini_model_capabilities_uses_max_output_tokens(agent_modules):
    p = agent_modules.providers
    provider = p.ProviderRegistry().provider_by_name("gemini")
    caps = provider.model_capabilities("gemini-2.5-flash")
    assert caps["max_tokens"] == "max_output_tokens"
    assert caps["temperature"] is True


def test_anthropic_model_capabilities(agent_modules):
    p = agent_modules.providers
    provider = p.ProviderRegistry().provider_by_name("anthropic")
    caps = provider.model_capabilities("claude-haiku-4-5-20251001")
    assert caps["max_tokens"] == "max_tokens"
    assert caps["temperature"] is True
    assert caps["tools"] is True


def test_groq_default_model_updated(agent_modules):
    p = agent_modules.providers
    provider = p.ProviderRegistry().provider_by_name("groq")
    assert provider.default_model == "llama-3.3-70b-versatile"


def test_gemini_default_model_updated(agent_modules):
    p = agent_modules.providers
    provider = p.ProviderRegistry().provider_by_name("gemini")
    # api_key unset but default_model falls back to the built-in
    assert provider.default_model == "gemini-2.5-flash"


def test_openai_o1_complete_uses_max_completion_tokens(agent_modules, monkeypatch):
    """o3-mini must send max_completion_tokens (not max_tokens) and omit
    temperature entirely β€” verified through the captured create kwargs."""
    p = agent_modules.providers
    agent_modules.root.udB = FakeUdB({"AGENT_CLOUD_OPENAI_KEY": "sk-test"})
    captured = {}
    monkeypatch.setitem(sys.modules, "openai", _fake_openai_module(captured))

    provider = p.ProviderRegistry().provider_by_name("openai")
    resp = asyncio.run(
        provider.complete(
            [p.AIMessage(role="user", content="think hard")],
            model="o3-mini",
            max_tokens=2000,
            temperature=0.5,
        )
    )
    assert resp.ok and resp.model == "o3-mini"
    kw = captured["create_kwargs"]
    assert kw["max_completion_tokens"] == 2000
    assert "max_tokens" not in kw
    assert "temperature" not in kw


def test_openai_standard_model_keeps_max_tokens(agent_modules, monkeypatch):
    """Regression: the non-o-series path is unchanged β€” max_tokens + temperature."""
    p = agent_modules.providers
    agent_modules.root.udB = FakeUdB({"AGENT_CLOUD_OPENAI_KEY": "sk-test"})
    captured = {}
    monkeypatch.setitem(sys.modules, "openai", _fake_openai_module(captured))

    provider = p.ProviderRegistry().provider_by_name("openai")
    asyncio.run(
        provider.complete(
            [p.AIMessage(role="user", content="hi")],
            model="gpt-4o",
            max_tokens=512,
            temperature=0.3,
        )
    )
    kw = captured["create_kwargs"]
    assert kw["max_tokens"] == 512
    assert kw["temperature"] == 0.3
    assert "max_completion_tokens" not in kw


def test_agent_respond_forwards_model_to_provider(agent_modules, monkeypatch):
    """respond(model=...) reaches the provider's complete(model=...)."""
    p = agent_modules.providers
    agent_modules.root.udB = FakeUdB({"AGENT_CLOUD_OPENAI_KEY": "sk-test"})
    captured = {}
    monkeypatch.setitem(sys.modules, "openai", _fake_openai_module(captured))

    agent = agent_modules.agent_mod.UltroidAgent()
    asyncio.run(
        agent.respond(
            "list the loaded plugins",
            chat_id=7,
            provider_name="openai",
            model="gpt-4o",
            use_tools=False,
            use_cache=False,
        )
    )
    assert captured["create_kwargs"]["model"] == "gpt-4o"


# ── Phase 1.2 β€” CloudflareProvider ──────────────────────────────────────────


class FakeCfSession:
    """aiohttp.ClientSession stand-in for Cloudflare REST calls; accepts the
    ``headers`` kwarg (unlike the Ollama FakeAioSession) and records requests."""

    response_data: dict = {}
    calls: list = []

    def __init__(self, *args, **kwargs):
        pass

    async def __aenter__(self):
        return self

    async def __aexit__(self, *exc):
        return False

    def post(self, url, json=None, headers=None, timeout=None):
        self.calls.append({"url": url, "json": json, "headers": headers})
        return _FakeAioResp(data=dict(self.response_data))


@pytest.fixture
def cf_aiohttp(monkeypatch):
    import aiohttp

    FakeCfSession.calls = []
    FakeCfSession.response_data = {}
    monkeypatch.setattr(aiohttp, "ClientSession", FakeCfSession)
    return FakeCfSession


def test_cloudflare_not_in_registry_when_unconfigured(agent_modules):
    """Unconfigured environments keep the six-provider shape (no CF creds)."""
    p = agent_modules.providers
    assert "cloudflare" not in p.ProviderRegistry().provider_health()
    assert p.ProviderRegistry().provider_by_name("cloudflare") is None


def test_cloudflare_unconfigured_returns_error(agent_modules):
    p = agent_modules.providers
    cf = p.CloudflareProvider()
    assert not cf.is_configured()
    resp = asyncio.run(cf.complete([p.AIMessage(role="user", content="hi")]))
    assert not resp.ok
    assert "CF_ACCOUNT/CF_TOKEN" in resp.error


def test_cloudflare_text_complete(agent_modules, cf_aiohttp):
    p = agent_modules.providers
    cf = p.CloudflareProvider()
    cf._account = "acct123"
    cf._token = "tok456"
    cf_aiohttp.response_data = {
        "success": True,
        "result": {
            "response": "cf says hi",
            "usage": {"prompt_tokens": 4, "completion_tokens": 6},
        },
    }
    resp = asyncio.run(
        cf.complete(
            [p.AIMessage(role="user", content="hi")],
            system="be brief",
        )
    )
    assert resp.ok
    assert resp.content == "cf says hi"
    assert resp.provider == "cloudflare"
    assert resp.tokens_in == 4 and resp.tokens_out == 6
    call = cf_aiohttp.calls[0]
    assert call["url"].endswith("/ai/run/@cf/meta/llama-3.1-8b-instruct")
    assert call["headers"]["Authorization"] == "Bearer tok456"
    assert call["headers"]["Content-Type"] == "application/json"
    assert call["json"]["messages"][0] == {"role": "system", "content": "be brief"}
    assert call["json"]["max_tokens"] == 1024
    assert call["json"]["temperature"] == 0.7


def test_cloudflare_image_complete(agent_modules, cf_aiohttp):
    p = agent_modules.providers
    cf = p.CloudflareProvider()
    cf._account = "acct123"
    cf._token = "tok456"
    cf_aiohttp.response_data = {
        "success": True,
        "result": {"image": "BASE64BYTES"},
    }
    resp = asyncio.run(
        cf.complete(
            [p.AIMessage(role="user", content="a cat in space")],
            model="@cf/black-forest-labs/flux-1-schnell",
        )
    )
    assert resp.ok
    assert resp.image == "BASE64BYTES"
    assert "image generated" in resp.content
    call = cf_aiohttp.calls[0]
    assert "flux-1-schnell" in call["url"]
    # image path sends only the prompt β€” no token cap / temperature
    assert call["json"] == {"prompt": "a cat in space"}


def test_cloudflare_model_capabilities_text_vs_image(agent_modules):
    p = agent_modules.providers
    cf = p.CloudflareProvider()
    text_caps = cf.model_capabilities("@cf/meta/llama-3.1-8b-instruct")
    assert text_caps["max_tokens"] == "max_tokens"
    assert text_caps["temperature"] is True
    assert text_caps["stream"] is True
    img_caps = cf.model_capabilities("@cf/stabilityai/stable-diffusion-xl-base-1.0")
    assert img_caps["max_tokens"] is None
    assert img_caps["temperature"] is False
    assert img_caps["stream"] is False


def test_cloudflare_api_error_surfaces(agent_modules, cf_aiohttp):
    p = agent_modules.providers
    cf = p.CloudflareProvider()
    cf._account = "acct"
    cf._token = "tok"
    cf_aiohttp.response_data = {
        "success": False,
        "errors": [{"message": "bad model"}],
    }
    resp = asyncio.run(cf.complete([p.AIMessage(role="user", content="hi")]))
    assert not resp.ok
    assert "bad model" in resp.error


def test_provider_register_is_idempotent_by_name(agent_modules):
    p = agent_modules.providers
    reg = p.ProviderRegistry()
    before = len(reg._all)
    cf = p.CloudflareProvider()
    reg.provider_register(cf)
    assert len(reg._all) == before + 1
    reg.provider_register(p.CloudflareProvider())  # same name -> deduped
    assert len(reg._all) == before + 1
    assert reg.provider_by_name("cloudflare") is cf
    reg.provider_register(None)  # None is a no-op
    assert len(reg._all) == before + 1


# ── Phase 1.2 β€” ToolPipeline + Codex compatibility ──────────────────────────


def test_codex_tool_kwargs(agent_modules):
    k = agent_modules.tools_mod.codex_tool_kwargs
    assert k(None) == {}
    assert k([]) == {}
    base = k([{"type": "function", "function": {"name": "x"}}])
    assert base["tool_choice"] == "auto"
    assert "parallel_tool_calls" not in base
    full = k(["t"], tool_choice="required", parallel_tool_calls=True)
    assert full["tool_choice"] == "required"
    assert full["parallel_tool_calls"] is True
    none_choice = k(["t"], tool_choice=None)
    assert "tool_choice" not in none_choice


def test_tool_pipeline_chains_with_feed(agent_modules, monkeypatch):
    tools = agent_modules.tools_mod
    ex = tools.make_executor()

    calls = []

    async def fake_execute(self, tool_name, arguments):
        calls.append((tool_name, dict(arguments)))
        if tool_name == "first":
            return {"results": "DATA"}
        return {"ok": True}

    monkeypatch.setattr(tools.ToolExecutor, "execute", fake_execute)

    pipe = tools.ToolPipeline(ex)
    pipe.add("first", {"q": "x"})
    pipe.add("second", {"action": "store", "key": "k"}, feed={"results": "value"})
    outcomes = asyncio.run(pipe.run())

    assert len(outcomes) == 2
    assert outcomes[0]["tool"] == "first"
    assert outcomes[1]["tool"] == "second"
    # first step: declared args only (no feed)
    assert calls[0] == ("first", {"q": "x"})
    # second step: declared args + fed-forward value
    assert calls[1] == ("second", {"action": "store", "key": "k", "value": "DATA"})
    assert outcomes[1]["result"] == {"ok": True}


def test_tool_pipeline_empty_run(agent_modules):
    tools = agent_modules.tools_mod
    pipe = tools.ToolPipeline(tools.make_executor())
    assert asyncio.run(pipe.run()) == []



def test_openrouter_retries_unavailable_model_without_rewriting_config(
    agent_modules, monkeypatch
):
    p = agent_modules.providers
    configured_model = "dots-studio/dots-3-note-preview"
    agent_modules.root.udB = FakeUdB(
        {
            "AGENT_CLOUD_OPENROUTER_KEY": "unit-test-key",
            "AGENT_CLOUD_OPENROUTER_MODEL": configured_model,
        }
    )
    calls = []

    class FakeCompletions:
        async def create(self, **kwargs):
            calls.append(kwargs)
            if len(calls) == 1:
                raise RuntimeError(
                    "Error code: 404 - {'error': {'message': "
                    "'No endpoints found for dots-studio/dots-3-note-preview.'}}"
                )
            message = SimpleNamespace(content="fallback answer", tool_calls=None)
            return SimpleNamespace(
                choices=[SimpleNamespace(message=message)],
                usage=SimpleNamespace(prompt_tokens=4, completion_tokens=3),
            )

    class FakeAsyncOpenAI:
        def __init__(self, **kwargs):
            self.chat = SimpleNamespace(completions=FakeCompletions())

    fake_openai = types.ModuleType("openai")
    fake_openai.AsyncOpenAI = FakeAsyncOpenAI
    monkeypatch.setitem(sys.modules, "openai", fake_openai)

    provider = p.ProviderRegistry().provider_by_name("openrouter")
    response = asyncio.run(
        provider.complete([p.AIMessage(role="user", content="hey")])
    )

    assert response.ok
    assert response.content == "fallback answer"
    assert response.model == provider.fallback_model
    assert [call["model"] for call in calls] == [
        configured_model,
        provider.fallback_model,
    ]
    assert agent_modules.root.udB.get_key("AGENT_CLOUD_OPENROUTER_MODEL") == configured_model
    assert provider._model_unavailable("No endpoints found for model")
    assert not provider._model_unavailable("401 invalid API key")