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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
import pytest
from unittest.mock import AsyncMock

from langchain_core.runnables import Runnable, RunnableLambda
from langchain_core.messages import AIMessage
from langchain_core.outputs import ChatGeneration, ChatResult

from lilith_agent.models import (
    _RetryWrapper,
    _NoThinkWrapper,
    _BoundRetryWrapper,
    _BoundNoThinkWrapper,
    BatchAbortRateLimitError,
    QuestionRateLimitStreakError,
    RateLimitCooldownError,
    _reset_rate_limit_state_for_tests,
    batch_rate_limit_pause_seconds,
    clear_batch_rate_limit_window,
    is_retryable_rate_limit,
    rate_limit_question_scope,
)


class _FakeChatModel:
    """Minimal stand-in for a BaseChatModel exposing bind_tools."""

    _llm_type = "fake"

    def __init__(self):
        self.bound_with = None

    def bind_tools(self, tools, **kwargs):
        self.bound_with = tools
        return RunnableLambda(lambda msgs: AIMessage(content="ok"))


@pytest.fixture(autouse=True)
def _disable_retry_sleeps(monkeypatch):
    async def _no_async_sleep(_seconds: float) -> None:
        return None

    monkeypatch.setattr("lilith_agent.models._tenacity_sleep", lambda _seconds: None, raising=False)
    monkeypatch.setattr("lilith_agent.models._async_tenacity_sleep", _no_async_sleep, raising=False)


class _FailingGenerateModel:
    _llm_type = "failing"

    def __init__(self, exc: BaseException):
        self.exc = exc
        self.calls = 0

    def _generate(self, *args, **kwargs):
        self.calls += 1
        raise self.exc

    async def _agenerate(self, *args, **kwargs):
        self.calls += 1
        raise self.exc

    def bind_tools(self, tools, **kwargs):
        def _raise(_msgs):
            raise self.exc

        return RunnableLambda(_raise)


class _SuccessfulGenerateModel:
    _llm_type = "success"

    def __init__(self):
        self.calls = 0

    def _generate(self, *args, **kwargs):
        self.calls += 1
        return ChatResult(generations=[ChatGeneration(message=AIMessage(content="ok"))])

    async def _agenerate(self, *args, **kwargs):
        self.calls += 1
        return ChatResult(generations=[ChatGeneration(message=AIMessage(content="ok"))])

    def bind_tools(self, tools, **kwargs):
        return RunnableLambda(lambda msgs: AIMessage(content="ok"))


def test_retry_wrapper_bind_tools_returns_runnable():
    inner = _FakeChatModel()
    wrapper = _RetryWrapper.model_construct(inner=inner)

    bound = wrapper.bind_tools([])

    assert isinstance(bound, _BoundRetryWrapper)
    assert isinstance(bound, Runnable), (
        "bind_tools() must return a Runnable so create_react_agent accepts it"
    )


def test_retry_wrapper_bound_invoke_passes_through():
    inner = _FakeChatModel()
    wrapper = _RetryWrapper.model_construct(inner=inner)

    bound = wrapper.bind_tools([])
    result = bound.invoke([("user", "hi")])

    assert isinstance(result, AIMessage)
    assert result.content == "ok"


def test_no_think_wrapper_bind_tools_returns_runnable():
    inner = _FakeChatModel()
    wrapper = _NoThinkWrapper.model_construct(inner=inner, model_name="qwen-test")

    bound = wrapper.bind_tools([])

    assert isinstance(bound, _BoundNoThinkWrapper)
    assert isinstance(bound, Runnable)


def _make_genai_client_error(code: int):
    pytest.importorskip("google.genai.errors")
    from google.genai.errors import ClientError

    return ClientError(
        code,
        {
            "error": {
                "code": code,
                "status": "RESOURCE_EXHAUSTED" if code == 429 else "INVALID_ARGUMENT",
                "message": "test error",
            }
        },
    )


def test_genai_client_error_429_is_retryable():
    exc = _make_genai_client_error(429)

    assert is_retryable_rate_limit(exc) is True


def test_genai_client_error_400_is_not_retryable():
    exc = _make_genai_client_error(400)

    assert is_retryable_rate_limit(exc) is False


def test_retry_wrapper_records_first_gemini_cooldown(monkeypatch):
    _reset_rate_limit_state_for_tests()
    exc = _make_genai_client_error(429)
    sleeps = []
    monkeypatch.setattr("lilith_agent.models.time.sleep", sleeps.append)

    wrapper = _RetryWrapper.model_construct(
        inner=_FailingGenerateModel(exc), provider="google", model_name="gemini-3.1-pro"
    )

    with pytest.raises(RateLimitCooldownError) as raised:
        wrapper._generate([])

    assert raised.value.provider == "google"
    assert raised.value.model == "gemini-3.1-pro"
    assert raised.value.cooldown_seconds == 60
    assert sleeps == []


def test_retry_wrapper_escalates_gemini_cooldowns(monkeypatch):
    _reset_rate_limit_state_for_tests()
    exc = _make_genai_client_error(429)
    monkeypatch.setattr("lilith_agent.models.time.sleep", lambda _: None)
    wrapper = _RetryWrapper.model_construct(
        inner=_FailingGenerateModel(exc), provider="google", model_name="gemini-3.1-pro"
    )

    with pytest.raises(RateLimitCooldownError) as first:
        wrapper._generate([])
    with pytest.raises(RateLimitCooldownError) as second:
        wrapper._generate([])
    with pytest.raises(RateLimitCooldownError) as third:
        wrapper._generate([])

    assert first.value.cooldown_seconds == 60
    assert second.value.cooldown_seconds == 120
    assert third.value.cooldown_seconds == 300


def test_success_resets_lane_failure_counter(monkeypatch):
    _reset_rate_limit_state_for_tests()
    exc = _make_genai_client_error(429)
    monkeypatch.setattr("lilith_agent.models.time.sleep", lambda _: None)
    failing = _RetryWrapper.model_construct(
        inner=_FailingGenerateModel(exc), provider="google", model_name="gemini-3.1-pro"
    )
    success = _RetryWrapper.model_construct(
        inner=_SuccessfulGenerateModel(), provider="google", model_name="gemini-3.1-pro"
    )

    with pytest.raises(RateLimitCooldownError):
        failing._generate([])
    success._generate([])
    with pytest.raises(RateLimitCooldownError) as raised:
        failing._generate([])

    assert raised.value.cooldown_seconds == 60


def test_same_gemini_lane_shares_cooldown(monkeypatch):
    _reset_rate_limit_state_for_tests()
    exc = _make_genai_client_error(429)
    now = [1000.0]
    sleeps = []
    monkeypatch.setattr("lilith_agent.models.time.monotonic", lambda: now[0])
    monkeypatch.setattr("lilith_agent.models.time.sleep", sleeps.append)
    first = _RetryWrapper.model_construct(
        inner=_FailingGenerateModel(exc), provider="google", model_name="gemini-3.1-pro"
    )
    second = _RetryWrapper.model_construct(
        inner=_SuccessfulGenerateModel(), provider="google", model_name="gemini-3.1-pro"
    )

    with pytest.raises(RateLimitCooldownError):
        first._generate([])
    now[0] = 1005.0
    second._generate([])

    assert sleeps == [55.0]


def test_other_gemini_lane_does_not_sleep_for_pro_cooldown(monkeypatch):
    _reset_rate_limit_state_for_tests()
    exc = _make_genai_client_error(429)
    now = [1000.0]
    sleeps = []
    monkeypatch.setattr("lilith_agent.models.time.monotonic", lambda: now[0])
    monkeypatch.setattr("lilith_agent.models.time.sleep", sleeps.append)
    pro = _RetryWrapper.model_construct(
        inner=_FailingGenerateModel(exc), provider="google", model_name="gemini-3.1-pro"
    )
    flash = _RetryWrapper.model_construct(
        inner=_SuccessfulGenerateModel(), provider="google", model_name="gemini-3-flash-preview"
    )

    with pytest.raises(RateLimitCooldownError):
        pro._generate([])
    now[0] = 1005.0
    flash._generate([])

    assert sleeps == []


def test_unknown_google_model_does_not_get_gemini_cooldown(monkeypatch):
    _reset_rate_limit_state_for_tests()
    exc = _make_genai_client_error(429)
    monkeypatch.setattr("lilith_agent.models.time.sleep", lambda _: None)
    wrapper = _RetryWrapper.model_construct(
        inner=_FailingGenerateModel(exc), provider="google", model_name="gemini-unknown"
    )

    with pytest.raises(type(exc)):
        wrapper._generate([])


def _make_genai_quota_error(details: list[dict]):
    pytest.importorskip("google.genai.errors")
    from google.genai.errors import ClientError

    return ClientError(
        429,
        {
            "error": {
                "code": 429,
                "status": "RESOURCE_EXHAUSTED",
                "message": "quota exceeded",
                "details": details,
            }
        },
    )


def test_daily_quota_metadata_raises_batch_abort(monkeypatch):
    _reset_rate_limit_state_for_tests()
    exc = _make_genai_quota_error(
        [
            {
                "@type": "type.googleapis.com/google.rpc.QuotaFailure",
                "violations": [{"quotaId": "GenerateRequestsPerDayPerProjectPerModel"}],
            }
        ]
    )
    monkeypatch.setattr("lilith_agent.models.time.sleep", lambda _: None)
    wrapper = _RetryWrapper.model_construct(
        inner=_FailingGenerateModel(exc), provider="google", model_name="gemini-3.1-pro"
    )

    with pytest.raises(BatchAbortRateLimitError) as raised:
        wrapper._generate([])

    assert "daily" in raised.value.reason.lower()


def test_long_retry_delay_raises_batch_abort(monkeypatch):
    _reset_rate_limit_state_for_tests()
    exc = _make_genai_quota_error(
        [
            {
                "@type": "type.googleapis.com/google.rpc.RetryInfo",
                "retryDelay": "900s",
            }
        ]
    )
    monkeypatch.setattr("lilith_agent.models.time.sleep", lambda _: None)
    wrapper = _RetryWrapper.model_construct(
        inner=_FailingGenerateModel(exc), provider="google", model_name="gemini-3.1-pro"
    )

    with pytest.raises(BatchAbortRateLimitError) as raised:
        wrapper._generate([])

    assert "retry" in raised.value.reason.lower()


def test_question_rate_limit_scope_raises_after_50_events():
    _reset_rate_limit_state_for_tests()
    exc = _make_genai_client_error(429)

    with pytest.raises(QuestionRateLimitStreakError) as raised:
        with rate_limit_question_scope():
            for _ in range(50):
                try:
                    raise exc
                except BaseException as caught:
                    from lilith_agent.models import record_rate_limit_observation

                    record_rate_limit_observation(caught)

    assert raised.value.count == 50


def test_question_rate_limit_scope_resets_after_success():
    _reset_rate_limit_state_for_tests()
    exc = _make_genai_client_error(429)

    with rate_limit_question_scope():
        from lilith_agent.models import record_rate_limit_observation, record_rate_limit_success

        for _ in range(49):
            record_rate_limit_observation(exc)
        record_rate_limit_success()
        for _ in range(49):
            record_rate_limit_observation(exc)


def test_batch_window_triggers_pause_after_70_rate_limits_in_100_outcomes():
    _reset_rate_limit_state_for_tests()
    exc = _make_genai_client_error(429)
    from lilith_agent.models import record_rate_limit_observation, record_rate_limit_success

    for _ in range(70):
        record_rate_limit_observation(exc)
    for _ in range(30):
        record_rate_limit_success()

    assert batch_rate_limit_pause_seconds() == 300
    clear_batch_rate_limit_window()
    assert batch_rate_limit_pause_seconds() is None


def test_batch_window_does_not_trigger_below_threshold():
    _reset_rate_limit_state_for_tests()
    exc = _make_genai_client_error(429)
    from lilith_agent.models import record_rate_limit_observation, record_rate_limit_success

    for _ in range(69):
        record_rate_limit_observation(exc)
    for _ in range(31):
        record_rate_limit_success()

    assert batch_rate_limit_pause_seconds() is None


def test_bound_retry_wrapper_raises_cooldown_for_gemini_lane(monkeypatch):
    _reset_rate_limit_state_for_tests()
    exc = _make_genai_client_error(429)
    monkeypatch.setattr("lilith_agent.models.time.sleep", lambda _: None)
    wrapper = _RetryWrapper.model_construct(
        inner=_FailingGenerateModel(exc), provider="google", model_name="gemini-3.1-pro"
    )

    bound = wrapper.bind_tools([])

    with pytest.raises(RateLimitCooldownError) as raised:
        bound.invoke([("user", "hi")])

    assert raised.value.model == "gemini-3.1-pro"


@pytest.mark.asyncio
async def test_async_retry_wrapper_raises_cooldown_for_gemini_lane(monkeypatch):
    _reset_rate_limit_state_for_tests()
    exc = _make_genai_client_error(429)
    monkeypatch.setattr("lilith_agent.models.asyncio.sleep", AsyncMock())
    wrapper = _RetryWrapper.model_construct(
        inner=_FailingGenerateModel(exc), provider="google", model_name="gemini-3.1-pro"
    )

    with pytest.raises(RateLimitCooldownError) as raised:
        await wrapper._agenerate([])

    assert raised.value.model == "gemini-3.1-pro"


@pytest.mark.asyncio
async def test_async_bound_retry_wrapper_raises_cooldown_for_gemini_lane(monkeypatch):
    _reset_rate_limit_state_for_tests()
    exc = _make_genai_client_error(429)
    monkeypatch.setattr("lilith_agent.models.asyncio.sleep", AsyncMock())
    wrapper = _RetryWrapper.model_construct(
        inner=_FailingGenerateModel(exc), provider="google", model_name="gemini-3.1-pro"
    )

    bound = wrapper.bind_tools([])

    with pytest.raises(RateLimitCooldownError) as raised:
        await bound.ainvoke([("user", "hi")])

    assert raised.value.model == "gemini-3.1-pro"


def test_deepseek_config_defaults_and_env(monkeypatch):
    from lilith_agent.config import Config

    monkeypatch.delenv("GAIA_DEEPSEEK_API_KEY", raising=False)
    monkeypatch.delenv("GAIA_DEEPSEEK_BASE_URL", raising=False)
    monkeypatch.setenv("DEEPSEEK_API_KEY", "ds-env-key")

    cfg = Config.from_env()

    assert cfg.deepseek_api_key == "ds-env-key"
    assert cfg.deepseek_base_url == "https://api.deepseek.com"

    monkeypatch.setenv("GAIA_DEEPSEEK_API_KEY", "gaia-ds-key")
    monkeypatch.setenv("GAIA_DEEPSEEK_BASE_URL", "https://deepseek.internal")

    cfg = Config.from_env()

    assert cfg.deepseek_api_key == "gaia-ds-key"
    assert cfg.deepseek_base_url == "https://deepseek.internal"


def test_agent_model_tier_selects_configured_tier(monkeypatch):
    from lilith_agent.config import Config
    from lilith_agent.models import _resolve_agent_model_choice

    monkeypatch.setenv("GAIA_CHEAP_PROVIDER", "deepseek")
    monkeypatch.setenv("GAIA_CHEAP_MODEL", "deepseek-v4-flash")
    monkeypatch.setenv("GAIA_STRONG_PROVIDER", "deepseek")
    monkeypatch.setenv("GAIA_STRONG_MODEL", "deepseek-v4-pro")
    monkeypatch.setenv("GAIA_AGENT_MODEL_TIER", "strong")

    assert _resolve_agent_model_choice(Config.from_env()) == ("deepseek", "deepseek-v4-pro")


def test_agent_model_direct_override_wins_over_tier(monkeypatch):
    from lilith_agent.config import Config
    from lilith_agent.models import _resolve_agent_model_choice

    monkeypatch.setenv("GAIA_AGENT_MODEL_TIER", "cheap")
    monkeypatch.setenv("GAIA_AGENT_PROVIDER", "deepseek")
    monkeypatch.setenv("GAIA_AGENT_MODEL", "deepseek-v4-pro")

    assert _resolve_agent_model_choice(Config.from_env()) == ("deepseek", "deepseek-v4-pro")


def test_invalid_agent_model_tier_is_rejected(monkeypatch):
    from lilith_agent.config import Config

    monkeypatch.setenv("GAIA_AGENT_MODEL_TIER", "medium")

    with pytest.raises(ValueError, match="GAIA_AGENT_MODEL_TIER"):
        Config.from_env()


def test_build_deepseek_uses_openai_compatible_endpoint(monkeypatch):
    from dataclasses import replace

    from lilith_agent.config import Config
    from lilith_agent.models import _build

    class FakeChatOpenAI:
        def __init__(self, **kwargs):
            self.kwargs = kwargs

    monkeypatch.setattr("lilith_agent.models.ChatOpenAI", FakeChatOpenAI)
    monkeypatch.setattr(
        "lilith_agent.models._RetryWrapper",
        lambda inner, provider, model_name: inner,
    )

    cfg = replace(
        Config.from_env(),
        deepseek_api_key="ds-key",
        deepseek_base_url="https://deepseek.internal",
        max_tokens=123,
    )

    model = _build("deepseek", "deepseek-v4-flash", cfg)

    assert model.kwargs == {
        "model": "deepseek-v4-flash",
        "api_key": "ds-key",
        "base_url": "https://deepseek.internal",
        "max_tokens": 123,
    }


def _fake_deepseek_cfg(monkeypatch):
    from dataclasses import replace

    from lilith_agent.config import Config

    class FakeChatOpenAI:
        def __init__(self, **kwargs):
            self.kwargs = kwargs

    monkeypatch.setattr("lilith_agent.models.ChatOpenAI", FakeChatOpenAI)
    monkeypatch.setattr(
        "lilith_agent.models._RetryWrapper",
        lambda inner, provider, model_name: inner,
    )
    return replace(
        Config.from_env(),
        deepseek_api_key="ds-key",
        deepseek_base_url="https://deepseek.internal",
        max_tokens=123,
    )


def test_build_deepseek_thinking_disabled_sets_extra_body(monkeypatch):
    from lilith_agent.models import _build

    cfg = _fake_deepseek_cfg(monkeypatch)

    model = _build("deepseek", "deepseek-v4-flash", cfg, thinking=False)

    assert model.kwargs["extra_body"] == {"thinking": {"type": "disabled"}}


def test_build_deepseek_thinking_enabled_omits_extra_body(monkeypatch):
    from lilith_agent.models import _build

    cfg = _fake_deepseek_cfg(monkeypatch)

    model = _build("deepseek", "deepseek-v4-flash", cfg)

    assert "extra_body" not in model.kwargs