"""Empty / thinking-only final-response recovery ladder for the conversation turn loop.

Runs when the model returned no visible text after ``<think>`` blocks. Ladder order is
load-bearing: partial-stream recovery → machinery-turn quiet exit → reuse prior
turn content (housekeeping tools only) → one post-tool-call nudge → thinking-only
completion reminder (×2) → empty-response retries (budgeted, deterministic-empty
short-circuit) → fallback provider → terminal ``(empty)`` sentinel. Nothing here imports
``agent.conversation_loop`` at module level.
"""

from __future__ import annotations

import logging
import re
from dataclasses import dataclass
from typing import Any, Dict, List, Optional

from agent import empty_response_guard as _empty_guard
from agent.message_metadata import append_message
from agent.message_sanitization import _sanitize_surrogates
from agent.turn_context_compaction import _refund_api_call
from agent.turn_recovery import interruptible_backoff_sleep

logger = logging.getLogger("agent.conversation_loop")

_INLINE_THINK_RE = re.compile(r'<think>|<thinking>|<reasoning>', re.IGNORECASE)

# The persisted user-row display kinds the gateway types as machinery: the self-injected
# internal_notification turn plus the producer-notice cards (background-process completions,
# async delegation results). Mirrors gateway.response_filters.MACHINERY_DISPLAY_KINDS;
# agent/ cannot import gateway/.
_MACHINERY_DISPLAY_KINDS = frozenset({"internal_notification", "process_complete", "async_delegation_complete"})

# Loop exit stamped when a machinery turn ends with the model adding nothing.
NOTIFICATION_NO_REPLY_EXIT = "notification_no_reply"

# Assistant row persisted for that exit: the same marker a deliberate no-reply on a human
# turn already leaves in the transcript (gateway.response_filters.LIVE_GATEWAY_SILENT_MARKERS).
MACHINERY_NO_REPLY_ROW = "[SILENT]"


def _latest_user_display_kind(messages: list[dict[str, Any]]) -> str | None:
    for msg in reversed(messages):
        if isinstance(msg, dict) and msg.get("role") == "user":
            return msg.get("display_kind")
    return None


def _notification_turn_without_tool_round(messages: list[dict[str, Any]]) -> bool:
    """True when the latest user row is a machinery turn no tool has followed.

    Scans back to the turn's user row: a ``tool`` row first means the model already
    acted on the notice (mid-task, keeps the full empty-response ladder), and any
    other user row (a steer, a recovery nudge) is the message the model is answering.
    """
    for msg in reversed(messages):
        if not isinstance(msg, dict):
            continue
        if msg.get("role") == "tool":
            return False
        if msg.get("role") == "user":
            return msg.get("display_kind") in _MACHINERY_DISPLAY_KINDS
    return False


@dataclass
class EmptyResponseVerdict:
    """Outcome of ``recover_empty_response``.

    ``action``: ``"break"`` (turn is done — ``final_response`` is set), ``"continue"``
    (re-enter the OUTER turn loop: a request-only hint was queued, a retry wait
    elapsed, or a fallback was activated and preflight must re-run), ``"return"``
    (interrupted during a retry wait — return ``result``) or ``"fallthrough"``
    (unreachable: every path exits; kept for the contract)."""

    action: str
    result: Optional[dict[str, Any]]
    final_response: Any
    turn_exit_reason: Any
    active_system_prompt: Any
    preflight_compression_blocked: bool
    api_call_count: int


def add_empty_response_retry_hint(agent: Any, api_messages: list[dict[str, Any]]) -> str:
    """Decorate only the outgoing tail; retain the hint across preflight rebuilds.

    Canonical user/tool content and the earlier cache prefix stay unchanged. Response
    intake clears the pending hint, so a recovered tool round cannot replay it.
    """
    hint = getattr(agent, "_empty_response_retry_hint", None)
    if not hint or not api_messages or api_messages[-1].get("role") not in {"user", "tool"}:
        return ""
    tail = api_messages[-1]
    content = tail.get("content")
    suffix = "\n\n" + hint
    if isinstance(content, list):
        content = [*content, {"type": "text", "text": suffix}]
    else:
        content = (content or "") + suffix
    api_messages[-1] = {**tail, "content": content}
    return suffix


def _retry_empty(
    agent: Any, response: Any, finish_reason: str, empty_candidate: bool, *, messages: Any,
    conversation_history: Any, api_call_count: int, observed_generation: bool = False,
) -> tuple:
    """Budgeted empty-response retry. Each empty attempt re-bills the full input, so the
    signature is recorded and deterministic empties stop burning paid retries (fails
    open: missing usage or any output keeps the budget). Returns
    ``(action_or_None, interrupt_result, deterministic_empty)``."""
    from agent.retry_utils import jittered_backoff

    if empty_candidate:
        _empty_guard.record_empty_attempt(
            agent, finish_reason=finish_reason, response=response, observed_generation=observed_generation,
        )
    budget = (
        _empty_guard.empty_retry_budget(agent, response)
        if empty_candidate else _empty_guard.DEFAULT_EMPTY_RETRY_BUDGET
    )
    deterministic = empty_candidate and _empty_guard.deterministic_empty(agent)
    if not (empty_candidate and agent._empty_content_retries < budget and not deterministic):
        return None, None, deterministic
    agent._empty_content_retries += 1
    n = agent._empty_content_retries
    wait_time = jittered_backoff(n, base_delay=5.0, max_delay=60.0)
    logger.warning(
        "Empty response (%s) — retry %d/%d in %.1fs (model=%s)",
        "reasoning but no visible content" if observed_generation else "no content or reasoning",
        n, budget, wait_time, agent.model,
    )
    _budget_note = (
        " — high-cost request, reduced retry budget"
        if budget < _empty_guard.DEFAULT_EMPTY_RETRY_BUDGET else ""
    )
    agent._buffer_diagnostic_status(
        f"⚠️ Empty response from model — retrying ({n}/{budget}) in {wait_time:.0f}s{_budget_note}"
    )
    _interrupted = interruptible_backoff_sleep(
        agent, wait_time, None,
        messages=messages,
        conversation_history=conversation_history,
        api_call_count=api_call_count,
        abort_message="Interrupt detected during empty-response retry wait, aborting.",
        interrupt_text=(
            f"Operation interrupted: retrying empty response from model (retry {n}/{budget})."
        ),
        activity_label=f"empty response retry backoff ({n}/{budget})",
    )
    if _interrupted is not None:
        return "return", _interrupted, deterministic
    return "continue", None, deterministic


def _terminal_empty(agent: Any, assistant_message: Any, finish_reason: str, messages: Any) -> str:
    """Retries and fallback exhausted: persist the ``(empty)`` sentinel row and return the
    delivery text. Reasoning is never copied into the delivery text here — the persisted row
    keeps the sentinel so later "continue" turns don't replay it and loop on empties."""
    _streak_cost = _empty_guard.streak_cost_usd(agent)
    if _streak_cost is not None:
        agent._buffer_diagnostic_status(
            f"ℹ️ Estimated cost of these empty attempts: ~${_streak_cost:.2f} (input tokens are billed "
            f"per attempt even when no answer is produced)"
        )
    agent._flush_status_buffer()
    reasoning_text = agent._extract_reasoning(assistant_message)
    agent._drop_trailing_empty_response_scaffolding(messages)
    assistant_msg = agent._build_assistant_message(assistant_message, finish_reason)
    assistant_msg["content"] = "(empty)"
    assistant_msg["_empty_terminal_sentinel"] = True
    append_message(messages, assistant_msg)

    if not reasoning_text:
        logger.warning(
            "Empty response (no content or reasoning) after %d retries. No fallback available. "
            "model=%s provider=%s",
            agent._empty_content_retries, agent.model,
            agent.provider,
        )
        # The gateway's final reply explains this turn's empty result, so a chat drops this copy.
        from gateway.warning_notifications import TerminalFailureText
        agent._emit_diagnostic_status(TerminalFailureText(
            "❌ Model returned no content after all retries"
            + (" and fallback attempts." if agent._fallback_chain else
               ". No fallback providers configured.")
        ))
        return "(empty)"

    logger.warning(
        "Reasoning-only response after %d retries; keeping it out of the terminal result "
        "(model=%s provider=%s)",
        agent._empty_content_retries, agent.model, agent.provider,
    )
    from gateway.warning_notifications import TerminalFailureText
    agent._emit_diagnostic_status(TerminalFailureText(
        "⚠️ Model produced reasoning but no visible response after all retries. Returning empty."
    ))
    return "(empty)"


def housekeeping_reply_line(agent: Any) -> Optional[str]:
    """The line written beside a housekeeping-only tool round (think blocks stripped), the turn's
    reply when the follow-up adds nothing; None when there is none (substantive tools mean it was
    narration)."""
    fallback = getattr(agent, '_last_content_with_tools', None)
    if not (fallback and getattr(agent, '_last_content_tools_all_housekeeping', False)):
        return None
    return _sanitize_surrogates(agent._strip_think_blocks(fallback).strip())


def take_housekeeping_reply_line(agent: Any) -> Optional[str]:
    """Consume ``housekeeping_reply_line`` as the turn's final response."""
    final_response = housekeeping_reply_line(agent)
    if final_response is None:
        return None
    agent._last_content_with_tools = None
    agent._last_content_tools_all_housekeeping = False
    # Do NOT modify the assistant message content (injected text poisoned history).
    # The final IS the response streamed before the housekeeping tool: name that identity
    # so a client settles the text it already shows instead of painting it a second time.
    # A held turn never showed it: its rewrite goes out as a new final.
    if getattr(agent, "_hold_stream", False) is not True:
        agent._response_was_previewed = True
        agent._reused_response_text = final_response
    return final_response


def _partial_stream_reply(agent: Any) -> Optional[str]:
    """The visible text streamed before the connection died, as the turn's reply; None if none."""
    partial = getattr(agent, "_current_streamed_assistant_text", "") or ""
    if not agent._has_content_after_think_block(partial):
        return None
    recovered = agent._strip_think_blocks(partial).strip()
    logger.info("Partial stream content delivered (%d chars) — using as final response", len(recovered))
    agent._emit_diagnostic_status("↻ Stream interrupted — using delivered content " "as final response")
    # A streamed fragment isn't a confirmed preview: gateway fallback delivery
    # sends the text plus the abnormal-turn explanation.
    agent._response_was_previewed = False
    return recovered


def _machinery_quiet_exit(agent: Any, assistant_message: Any, finish_reason: str, messages: Any) -> bool:
    """End a machinery turn's empty reply quietly; True when it did.

    A machinery turn (a self-injected internal_notification or a background-process /
    delegation completion the gateway staged as its own user row) answered empty means
    "nothing to add": no nudge, no paid retries, no provider switch, no sentinel row. Only
    when no tool ran this turn (a model that acted on the notice is mid-task) and the reply
    did not announce a tool call: ``finish_reason="tool_calls"`` with no payload is a dropped
    action, so it keeps the ladder below, whose re-prompt recovers it.
    """
    if finish_reason == "tool_calls" or not _notification_turn_without_tool_round(messages):
        return False
    logger.info(
        "Empty response on a machinery turn (%s): ending the turn without a reply",
        _latest_user_display_kind(messages),
    )
    # Close the turn in the transcript. The finalizer appends an assistant row only for a
    # non-empty final, and a user row left unanswered makes the next pre-call repair merge
    # the following human prompt into the notice (text and display_kind both lost). The
    # row carries the marker a deliberate no-reply already persists on human turns, so the
    # gateway keeps treating it as silence and the wire keeps alternating.
    assistant_msg = agent._build_assistant_message(assistant_message, finish_reason)
    assistant_msg["content"] = MACHINERY_NO_REPLY_ROW
    append_message(messages, assistant_msg)
    return True


def recover_empty_response(
    agent: Any, assistant_message: Any, response: Any, finish_reason: str, *, final_response: Any,
    messages: list[dict[str, Any]], api_messages: Any, conversation_history: Any,
    active_system_prompt: Any, api_call_count: int, turn_exit_reason: Any,
    preflight_compression_blocked: bool,
) -> EmptyResponseVerdict:
    """Recover without storing failed reasoning or synthetic conversation turns."""
    from agent.conversation_loop import (
        _CODEX_INCOMPLETE_NUDGE, _EMPTY_TOOL_RESPONSE_NUDGE, _sync_failover_system_message,
    )

    _turn_exit_reason = turn_exit_reason
    _preflight_compression_blocked = preflight_compression_blocked

    def _verdict(action: str, result: Optional[dict[str, Any]] = None) -> EmptyResponseVerdict:
        return EmptyResponseVerdict(
            action=action, result=result, final_response=final_response,
            turn_exit_reason=_turn_exit_reason, active_system_prompt=active_system_prompt,
            preflight_compression_blocked=_preflight_compression_blocked,
            api_call_count=api_call_count,
        )

    # Partial stream recovery: content streamed before the connection died becomes the
    # final response instead of fallback or retries.
    if (_recovered := _partial_stream_reply(agent)) is not None:
        _turn_exit_reason = "partial_stream_recovery"
        final_response = _recovered
        return _verdict("break")

    if _machinery_quiet_exit(agent, assistant_message, finish_reason, messages):
        _turn_exit_reason = NOTIFICATION_NO_REPLY_EXIT
        final_response = ""
        return _verdict("break")

    # Prior turn had real content + ONLY housekeeping tools: model is done, reuse it.
    # With substantive tools it was mid-task narration and the empty reply is a choke;
    # let the post-tool nudge handle it.
    if (final := take_housekeeping_reply_line(agent)) is not None:
        _turn_exit_reason = "fallback_prior_turn_content"
        logger.info("Empty follow-up after tool calls — using prior turn content as final response")
        agent._emit_diagnostic_status("↻ Empty response after tool calls — using earlier content as final answer")
        agent._empty_content_retries = 0
        final_response = final
        return _verdict("break")

    # Post-tool-call empty (no prior content, or only mid-task narration): nudge once.
    _prior_was_tool = any(m.get("role") == "tool" for m in messages[-5:])
    # Ollama puts <think> in content, not reasoning_content, so _has_structured misses
    # it; detect here to route to the thinking-only reminder.
    _has_inline_thinking = bool(_INLINE_THINK_RE.search(final_response or ""))
    if (
        _prior_was_tool
        and not getattr(agent, "_post_tool_empty_retried", False)
        and not _has_inline_thinking
    ):
        agent._post_tool_empty_retried = True
        # Clear stale narration so it doesn't resurface on a later empty response.
        agent._last_content_with_tools = None
        agent._last_content_tools_all_housekeeping = False
        logger.info("Empty response after tool calls — nudging model " "to continue processing")
        agent._buffer_diagnostic_status("⚠️ Model returned empty after tool calls — " "nudging to continue")
        agent._empty_response_retry_hint = _EMPTY_TOOL_RESPONSE_NUDGE
        final_response = None
        return _verdict("continue")

    # A thinking-only assistant prefill is stripped by the send-time sanitizer, making
    # that retry byte-identical. Send a completion reminder on the input instead.
    _has_structured = bool(
        getattr(assistant_message, "reasoning", None)
        or getattr(assistant_message, "reasoning_content", None)
        or getattr(assistant_message, "reasoning_details", None)
        or _has_inline_thinking
    )
    if _has_structured and agent._thinking_prefill_retries < 2:
        agent._thinking_prefill_retries += 1
        logger.info(
            "Thinking-only response (no visible content) — requesting completion (%d/2)",
            agent._thinking_prefill_retries,
        )
        agent._buffer_diagnostic_status(
            f"↻ Thinking-only response — requesting completion ({agent._thinking_prefill_retries}/2)"
        )
        agent._empty_response_retry_hint = _CODEX_INCOMPLETE_NUDGE
        final_response = None
        return _verdict("continue")

    # Empty-response retries: truly empty replies AND reasoning-only replies after
    # completion-reminder exhaustion.
    _truly_empty = not agent._strip_think_blocks(final_response).strip()
    _empty_candidate = _truly_empty and (not _has_structured or agent._thinking_prefill_retries >= 2)
    action, interrupt_result, _deterministic_empty = _retry_empty(
        agent, response, finish_reason, _empty_candidate, messages=messages,
        conversation_history=conversation_history, api_call_count=api_call_count,
        observed_generation=_has_structured,
    )
    if action is not None:
        return _verdict(action, interrupt_result)

    if _truly_empty and _deterministic_empty:
        logger.warning(
            "Repeated empty response detected (model=%s provider=%s finish_reason=%s) — "
            "skipping remaining retries",
            agent.model, agent.provider, finish_reason,
        )
        agent._buffer_diagnostic_status(
            "⚠️ Model is repeatedly returning empty content — skipping further retries "
            "to avoid repeat charges"
        )

    # Exhausted retries — try the next provider in the chain before "(empty)".
    if _truly_empty and agent._fallback_chain:
        logger.warning(
            "Empty response after %d retries — attempting fallback (model=%s, provider=%s)",
            agent._empty_content_retries, agent.model, agent.provider,
        )
        agent._buffer_diagnostic_status("⚠️ Model returning empty responses — " "switching to fallback provider...")
        if agent._try_activate_fallback():
            active_system_prompt = _sync_failover_system_message(agent, api_messages, active_system_prompt)
            agent._empty_content_retries = 0
            agent._buffer_diagnostic_status(f"↻ Switched to fallback: {agent.model} " f"({agent.provider})")
            logger.info(
                "Fallback activated after empty responses: now using %s on %s",
                agent.model, agent.provider,
            )
            # OUTER loop: `continue` re-runs preflight against the fallback's window;
            # `break` would end the turn without calling the fallback.
            _preflight_compression_blocked = False
            # The fallback hop is a provider switch, not a model turn: refund the empty
            # call so a mid-turn fallback doesn't eat the iteration budget (#77305).
            api_call_count = _refund_api_call(agent, api_call_count)
            return _verdict("continue")

    _turn_exit_reason = "empty_response_exhausted"
    final_response = _terminal_empty(agent, assistant_message, finish_reason, messages)
    return _verdict("break")
