"""Regression tests for cached gateway transcript selection."""

from gateway.run import _build_gateway_agent_history, _select_cached_agent_history


def test_select_cached_history_keeps_expected_replay_cleanup():
    """A durable read-only tool tail removed by stock replay cleanup stays removed."""
    live = [
        {"role": "user", "content": "check status", "_db_persisted": True},
        {
            "role": "assistant",
            "content": "",
            "tool_calls": [
                {
                    "id": "call-1",
                    "type": "function",
                    "function": {"name": "web_search", "arguments": "{}"},
                }
            ],
            "_db_persisted": True,
        },
        {
            "role": "tool",
            "tool_call_id": "call-1",
            "content": "[Command interrupted]",
            "_db_persisted": True,
        },
    ]

    persisted, observed = _build_gateway_agent_history(live)

    assert observed is None
    assert len(persisted) < len(live)
    assert persisted == [{"role": "user", "content": "check status", "_db_persisted": True}]
    assert _select_cached_agent_history(persisted, live) is persisted


def test_select_cached_history_preserves_live_when_a_real_row_is_unpersisted():
    persisted = [
        {"role": "user", "content": "hello"},
        {"role": "assistant", "content": "hi"},
    ]
    live = [
        {"role": "user", "content": "hello", "_db_persisted": True},
        {"role": "assistant", "content": "hi", "_db_persisted": True},
        {"role": "user", "content": "not written"},
    ]

    selected = _select_cached_agent_history(persisted, live)

    assert selected is not persisted
    assert selected == live


def test_select_cached_history_ignores_unmarked_ephemeral_scaffolding():
    """Internal retry nudges are deliberately unpersisted, not evidence of DB lag."""
    persisted = [{"role": "user", "content": "ship the fix"}]
    live = [
        {"role": "user", "content": "ship the fix", "_db_persisted": True},
        {
            "role": "user",
            "content": "Verify your work before stopping.",
            "_pre_verify_synthetic": True,
        },
    ]

    assert _select_cached_agent_history(persisted, live) is persisted


def test_persistent_transcript_lag_escalates_to_error(caplog):
    """The 'Persisted transcript lagged live cached history' line repeated at WARNING for 11 days in
    #114266. A per-session streak of lagging turns escalates it to ERROR; a turn whose disk copy
    caught up resets the streak."""
    import logging
    from types import SimpleNamespace

    from gateway.run_turn_runner import _TRANSCRIPT_LAG_ESCALATION_TURNS, TurnRunner

    persisted = [{"role": "user", "content": "hello"}]
    live = [{"role": "user", "content": "hello", "_db_persisted": True},
            {"role": "assistant", "content": "not written"}]
    runner = SimpleNamespace()
    ctx = SimpleNamespace(history=persisted, channel_prompt=None, user_config=None,
                          session_id="sid-1", session_key="agent:main:telegram:dm:1")
    turn = TurnRunner(runner, ctx)
    agent = SimpleNamespace(session_id="sid-1", _session_messages=live)

    with caplog.at_level(logging.WARNING, logger="gateway.run"):
        for _ in range(_TRANSCRIPT_LAG_ESCALATION_TURNS):
            history, _observed, _media = turn._load_turn_history(agent, reused_cached_agent=True)
    assert len(history) == len(live)  # live context still preserved
    levels = [r.levelno for r in caplog.records if "lagged live cached history" in r.getMessage()]
    assert levels == [logging.WARNING] * (_TRANSCRIPT_LAG_ESCALATION_TURNS - 1) + [logging.ERROR]

    # Disk caught up: the streak resets, so the next lag starts again at WARNING.
    agent._session_messages = [dict(persisted[0], _db_persisted=True)]
    turn._load_turn_history(agent, reused_cached_agent=True)
    assert ctx.session_key not in runner._transcript_lag_streaks
