"""
Transparent, rule-based classifier for extracting an agent's SELF-REPORTED
success claim from the final natural-language message in a tau-bench
trajectory. This is deliberately simple and fully disclosed as a limitation
(not an LLM-judge classifier) so that all downstream results are traceable
to an auditable rule set rather than a second opaque model.
"""
import re

SUCCESS_PATTERNS = [
    r"\bsuccessfully\b", r"\bhas been (completed|processed|updated|cancelled|exchanged|refunded|issued)\b",
    r"\bhave been (completed|processed|updated|cancelled|exchanged|refunded|issued)\b",
    r"\ball set\b", r"\bconfirmed\b", r"\bi'?ve processed\b", r"\bi have processed\b",
    r"\bcompleted the\b", r"\bprocessed your\b", r"\brefund will be (issued|processed)\b",
    r"\border has been\b", r"\byour request has been\b",
]
FAILURE_PATTERNS = [
    r"\bunable to\b", r"\bcannot\b", r"\bcan't\b", r"\bi'?m sorry\b", r"\bi apologize\b",
    r"\bnot able to\b", r"\bunfortunately\b", r"\btransfer you to\b", r"\bhuman agent\b",
    r"\bnot possible\b", r"\bcan not\b", r"\bunable\b",
]


def get_final_user_facing_message(traj):
    """Return the last assistant message that has non-empty content (i.e.,
    a natural-language reply to the user, not a tool-call-only message)."""
    for m in reversed(traj):
        if m.get("role") == "assistant" and m.get("content"):
            return m["content"]
    return ""


def self_reported_success_score(final_message):
    """Return a self-reported-success score in {0.0, 0.5, 1.0} based on
    transparent keyword/regex matching. 0.5 = ambiguous/no clear signal
    (neither pattern set matched, or both matched)."""
    text = final_message.lower()
    has_success = any(re.search(p, text) for p in SUCCESS_PATTERNS)
    has_failure = any(re.search(p, text) for p in FAILURE_PATTERNS)
    if has_success and not has_failure:
        return 1.0
    if has_failure and not has_success:
        return 0.0
    return 0.5  # ambiguous: both or neither matched
