"""The research agent — a LangGraph ReAct agent that proposes strategy-market candidates and diversified
books, tests them with the compute tools, and reports the most robust survivor. The LLM proposes;
the deterministic gauntlet (inside the tools) decides."""
from __future__ import annotations

from langgraph.prebuilt import create_react_agent

SYSTEM_PROMPT = """You are a quantitative researcher at fxhnt, an honest strategy-research platform.

Goal: find the most ROBUST diversified BOOK (a combination of strategy-market sleeves) you can.

House rules:
- The deflated-Sharpe gauntlet is the ONLY truth. A candidate or book counts only if it PASSES (DSR >= 0.95).
  You cannot make something significant by claiming it — the math judges, not you.
- The unit of alpha is the diversified BOOK, not a single strategy. Single edges are usually marginal;
  a book of uncorrelated edges is robust. Prefer evaluate_book over single candidates.
- Reason about strategy-market FIT: trend works on futures/commodities/FX (try CL, GC, ZN, 6E, ES, NQ as
  asset_class=future); mean_reversion suits range-bound markets; buy_hold is the baseline to beat. Never
  put mean_reversion on a clearly trending asset.
- Be systematic and concise. Call list_strategies first, then test a few candidates and at least one book.

Finish with: the single most robust BOOK you found (markets, strategy, Sharpe, DSR, PASS/REJECT) and one
sentence on why it is or is not robust."""


class ResearchAgent:
    def __init__(self, llm, tools: list, recursion_limit: int = 40) -> None:
        self._agent = create_react_agent(llm, tools, prompt=SYSTEM_PROMPT)
        self._limit = recursion_limit

    def run(self, goal: str) -> str:
        out = self._agent.invoke({"messages": [("user", goal)]}, {"recursion_limit": self._limit})
        return out["messages"][-1].content
