diff --git a/.env.example b/.env.example index aa5436b..28c55dc 100644 --- a/.env.example +++ b/.env.example @@ -34,3 +34,13 @@ MT5_LOGIN= MT5_PASSWORD= MT5_SERVER= MT5_PATH=C:\Program Files\MetaTrader 5 EXNESS\terminal64.exe + +# --- Headline Arena Calibration Venue (Optional external forecast benchmarking) --- +# Leave blank for offline dry-run simulation, or reuse ~/.headlinearena/credentials.json +HA_BASE_URL=https://headlinearena.com +HA_AGENT_ID= +HA_CLIENT_SECRET= +HA_CREDENTIALS_PATH=~/.headlinearena/credentials.json +HA_MODEL_PROVIDER= +HA_MODEL_NAME= +HA_MODEL_VERSION= diff --git a/README.md b/README.md index 3c4cc8f..b1a3345 100644 --- a/README.md +++ b/README.md @@ -217,11 +217,14 @@ python -m argus_ai.cli single --ticker NVDA --regime BULLISH --vix 16.5 python -m argus_ai.cli adversarial-demo ``` -### 4. MCP Server, WebSocket Stream & Visual Dashboard +### 4. MCP Server, Headline Arena Calibration Desk & Visual Dashboard ```bash # Launch Model Context Protocol (MCP) Server over stdio for Claude Desktop / Cursor python -m argus_ai.cli mcp +# Run Headline Arena calibrated macro-futures dry-run (add --live-discovery --submit for live scoring) +python -m argus_ai.cli headline-arena + # Inspect Adaptive Bayesian Agent Weights python -m argus_ai.cli weights @@ -255,6 +258,7 @@ All **61 tests** run deterministically offline in `< 1.0s` and verify: - 🏛️ **[ADR 001: Adversarial Devil's Advocate Veto](docs/decisions/001-adversarial-devils-advocate-veto.md)** - ⚠️ **[Known Limitations & Risk Disclosures](docs/limitations.md)** - 🔌 **[Model Context Protocol (MCP) Integration Guide](docs/MCP_INTEGRATION_GUIDE.md)** +- 🎯 **[Headline Arena Calibration Integration](docs/HEADLINE_ARENA_INTEGRATION.md)** - 📜 **[Trade Passport Specification](docs/TRADE_PASSPORT_SPEC.md)** - 🤝 **[Team Contribution Guide](docs/CONTRIBUTING.md)** diff --git a/argus_ai/cli.py b/argus_ai/cli.py index b77b6bd..aa8c6e9 100644 --- a/argus_ai/cli.py +++ b/argus_ai/cli.py @@ -20,6 +20,7 @@ from argus_ai.core.kill_switch import EmergencyKillSwitch from argus_ai.core.streamer import MarketStreamListener from argus_ai.http_server import serve +from argus_ai.integrations.headline_arena import HeadlineArenaDesk, HeadlineArenaRunResult from argus_ai.mcp_server import ArgusMCPServer from argus_ai.scanner import OpportunityScanner @@ -178,6 +179,35 @@ def display_replay_rich(verification): )) +def display_headline_arena_rich(results: list[HeadlineArenaRunResult]) -> None: + tbl = Table( + title="Headline Arena Calibration Desk (Ternary Probability Forecasts & Refusal Scoring)", + header_style="bold cyan", + ) + tbl.add_column("Asset", style="bold white") + tbl.add_column("Challenge ID", style="dim") + tbl.add_column("Committee", justify="center") + tbl.add_column("DA Risk", justify="right") + tbl.add_column("Forecast", justify="center") + tbl.add_column("P(Bear / Neut / Bull)", justify="center") + tbl.add_column("Mode", justify="center") + + for res in results: + fc = res.forecast + probs = fc.probabilities + mode_str = "[bold green]SUBMITTED[/bold green]" if res.submitted else "[yellow]DRY-RUN[/yellow]" + tbl.add_row( + fc.asset, + fc.challenge_id, + fc.consensus_status, + f"{fc.disqualification_risk_pct:.0f}%", + f"{fc.direction.upper()} ({fc.confidence * 100:.0f}%)", + f"{probs['bearish']:.2f} / {probs['neutral']:.2f} / {probs['bullish']:.2f}", + mode_str, + ) + console.print(tbl) + + def main(argv: list[str] | None = None) -> None: print_banner() parser = argparse.ArgumentParser(description="ARGUS AI Autonomous Trading Desk CLI") @@ -194,6 +224,7 @@ def main(argv: list[str] | None = None) -> None: "backtest", "replay", "serve", + "headline-arena", ], nargs="?", default="scan", @@ -208,6 +239,11 @@ def main(argv: list[str] | None = None) -> None: parser.add_argument("--reset-kill", action="store_true", help="Reset manual kill switch") parser.add_argument("--host", type=str, default="127.0.0.1", help="Bind host for 'serve' HTTP workbench") parser.add_argument("--port", type=int, default=8000, help="Bind port for 'serve' HTTP workbench") + parser.add_argument("--asset", type=str, default="", help="Optional asset filter (e.g. GC, CL, ES, ZN) for 'headline-arena'") + parser.add_argument("--live-discovery", action="store_true", help="Discover live open challenges from Headline Arena") + parser.add_argument("--submit", action="store_true", help="Submit forecasts to Headline Arena (default is safe dry-run)") + parser.add_argument("--scorecard", action="store_true", help="Fetch and print the agent's Headline Arena scorecard") + parser.add_argument("--agent-id", type=str, default="", help="Optional Headline Arena agent_id for --scorecard") args = parser.parse_args(argv) @@ -224,6 +260,27 @@ def main(argv: list[str] | None = None) -> None: serve(host=args.host, port=args.port) return + if args.command == "headline-arena": + desk = HeadlineArenaDesk() + if args.scorecard: + scorecard = desk.client.get_scorecard(agent_id=args.agent_id or None) + console.print( + Panel( + "\n".join(f"[cyan]{k}:[/cyan] {v}" for k, v in scorecard.items()), + title="Headline Arena Agent Scorecard", + border_style="cyan", + ) + ) + return + + results = desk.run_cycle( + asset=args.asset or None, + live_discovery=args.live_discovery, + submit=args.submit, + ) + display_headline_arena_rich(results) + return + alpaca = AlpacaClient() kill_switch = EmergencyKillSwitch(alpaca) scanner = OpportunityScanner(alpaca, kill_switch) diff --git a/argus_ai/committee.py b/argus_ai/committee.py index daa022a..7c6e41d 100644 --- a/argus_ai/committee.py +++ b/argus_ai/committee.py @@ -147,9 +147,19 @@ def evaluate_opportunity( ) # 7. Assemble Immutable Trade Passport + ticker_upper = ctx.ticker.upper() + macro_futures_roots = {"GC", "CL", "ES", "NQ", "ZN", "ZB", "SI", "HG", "NG", "DX"} + asset_class: Literal["US_EQUITY", "CRYPTO", "FOREX", "MACRO_FUTURES"] + if "BTC" in ticker_upper or "ETH" in ticker_upper: + asset_class = "CRYPTO" + elif ticker_upper in macro_futures_roots: + asset_class = "MACRO_FUTURES" + else: + asset_class = "US_EQUITY" + passport = TradePassport( ticker=ctx.ticker, - asset_class="CRYPTO" if ("BTC" in ctx.ticker or "ETH" in ctx.ticker) else "US_EQUITY", + asset_class=asset_class, direction=proposed_direction, current_price=ctx.current_price, target_entry=risk_params["entry_price"], diff --git a/argus_ai/config/settings.py b/argus_ai/config/settings.py index b34a2ef..1d7c58d 100644 --- a/argus_ai/config/settings.py +++ b/argus_ai/config/settings.py @@ -34,11 +34,26 @@ class CommitteeSettings: enable_mcp: bool = True +@dataclass +class HeadlineArenaSettings: + base_url: str = field(default_factory=lambda: os.getenv("HA_BASE_URL", "https://headlinearena.com")) + agent_id: str = field(default_factory=lambda: os.getenv("HA_AGENT_ID", "")) + client_secret: str = field(default_factory=lambda: os.getenv("HA_CLIENT_SECRET", "")) + credentials_path: str = field( + default_factory=lambda: os.getenv("HA_CREDENTIALS_PATH", "~/.headlinearena/credentials.json") + ) + timeout_seconds: float = field(default_factory=lambda: float(os.getenv("HA_TIMEOUT_SECONDS", "20.0"))) + model_provider: str = field(default_factory=lambda: os.getenv("HA_MODEL_PROVIDER", "")) + model_name: str = field(default_factory=lambda: os.getenv("HA_MODEL_NAME", "")) + model_version: str = field(default_factory=lambda: os.getenv("HA_MODEL_VERSION", "")) + + @dataclass class ArgusConfig: alpaca: AlpacaSettings = field(default_factory=AlpacaSettings) risk: RiskSettings = field(default_factory=RiskSettings) committee: CommitteeSettings = field(default_factory=CommitteeSettings) + headline_arena: HeadlineArenaSettings = field(default_factory=HeadlineArenaSettings) watchlist: list[str] = field( default_factory=lambda: [ "NVDA", diff --git a/argus_ai/core/models.py b/argus_ai/core/models.py index 16437d4..0c90b08 100644 --- a/argus_ai/core/models.py +++ b/argus_ai/core/models.py @@ -60,7 +60,7 @@ class TradePassport(BaseModel): ) timestamp: str = Field(default_factory=lambda: datetime.datetime.now(datetime.timezone.utc).isoformat()) ticker: str - asset_class: Literal["US_EQUITY", "CRYPTO", "FOREX"] = "US_EQUITY" + asset_class: Literal["US_EQUITY", "CRYPTO", "FOREX", "MACRO_FUTURES"] = "US_EQUITY" direction: Literal["LONG", "SHORT"] # Pricing & Bracket Strategy diff --git a/argus_ai/integrations/__init__.py b/argus_ai/integrations/__init__.py new file mode 100644 index 0000000..590568d --- /dev/null +++ b/argus_ai/integrations/__init__.py @@ -0,0 +1,27 @@ +"""External venue and calibration integrations for ARGUS AI.""" + +from argus_ai.integrations.headline_arena import ( + CalibrationSummary, + HeadlineArenaChallenge, + HeadlineArenaClient, + HeadlineArenaDesk, + HeadlineArenaError, + HeadlineArenaRunResult, + TernaryForecast, + compute_calibration_summary, + generate_offline_macro_challenges, + passport_to_ternary_forecast, +) + +__all__ = [ + "CalibrationSummary", + "HeadlineArenaChallenge", + "HeadlineArenaClient", + "HeadlineArenaDesk", + "HeadlineArenaError", + "HeadlineArenaRunResult", + "TernaryForecast", + "compute_calibration_summary", + "generate_offline_macro_challenges", + "passport_to_ternary_forecast", +] diff --git a/argus_ai/integrations/headline_arena.py b/argus_ai/integrations/headline_arena.py new file mode 100644 index 0000000..955f64b --- /dev/null +++ b/argus_ai/integrations/headline_arena.py @@ -0,0 +1,985 @@ +""" +Headline Arena Calibration Integration for ARGUS AI. + +Connects the ARGUS 7-agent Investment Committee (including Devil's Advocate +vetoes and refusals) to Headline Arena's Brier-scored daily financial forecast +challenges on macro futures (GC, CL, ES, ZN, etc.), supporting: + +1. Calibrated TradePassport -> canonical ternary probability vector mapping + (``{"bearish", "neutral", "bullish"}``) so approvals, downsizes, rejections, + and Devil's Advocate vetoes all register cleanly on public calibration curves. +2. Cryptographic traceability via optional private ``forecast_context`` + carrying the SHA-256 ``TradePassport`` digest as an external evidence ref. +3. Zero-extra-dependency HTTPS REST client compatible with + ``~/.headlinearena/credentials.json`` from ``headlinearena-agent-plugin``. +4. Offline deterministic dry-run mode and local Brier / Headline Arena scoring. +""" + +from __future__ import annotations + +import json +import logging +import math +import time +import urllib.error +import urllib.parse +import urllib.request +import uuid +from collections.abc import Sequence +from pathlib import Path +from typing import Any, Literal + +import numpy as np +import pandas as pd +from pydantic import BaseModel, Field, model_validator + +from argus_ai.agents.audit_agent import AuditAgent +from argus_ai.backtest import compute_passport_digest +from argus_ai.committee import CommitteeOrchestrator +from argus_ai.config.settings import CONFIG, HeadlineArenaSettings +from argus_ai.core.models import MarketContext, TradePassport + +logger = logging.getLogger("argus.headline_arena") + +TernaryOutcome = Literal["bearish", "neutral", "bullish"] +TOKEN_REFRESH_MARGIN_SECONDS = 60 +MODEL_PROVIDER_MAX = 64 +MODEL_NAME_MAX = 128 +MODEL_VERSION_MAX = 64 + + +class HeadlineArenaError(RuntimeError): + """Raised when Headline Arena configuration, validation, or HTTP requests fail.""" + + +class HeadlineArenaChallenge(BaseModel): + """Normalized representation of an open Headline Arena financial challenge.""" + + challenge_id: str + asset: str + question: str = "" + status: str = "open" + deadline: str = "" + open_price: float = Field(gt=0.0) + dead_zone_pct: float | None = Field(default=None, ge=0.0) + resolution_criteria: str = "" + scope_key: str | None = None + contract_symbol: str | None = None + contract_month: str | None = None + ohlc_bars: list[dict[str, Any]] = Field(default_factory=list) + recent_news: list[str] = Field(default_factory=list) + vix_level: float = 18.0 + + +class TernaryForecast(BaseModel): + """Canonical Headline Arena ternary probability submission derived from a TradePassport.""" + + challenge_id: str + asset: str + probabilities: dict[TernaryOutcome, float] + direction: TernaryOutcome + confidence: float = Field(ge=0.0, le=1.0) + reasoning: str + summary: str = Field(max_length=500) + passport_id: str + passport_digest: str + consensus_status: Literal["APPROVED", "DOWNSIZED", "REJECTED", "VETOED"] + disqualification_risk_pct: float = Field(ge=0.0, le=100.0) + forecast_context: dict[str, Any] + + @model_validator(mode="after") + def _validate_probability_simplex(self) -> TernaryForecast: + required_keys = {"bearish", "neutral", "bullish"} + if set(self.probabilities.keys()) != required_keys: + raise ValueError("probabilities must have exact keys: bearish, neutral, bullish") + for key, value in self.probabilities.items(): + if not math.isfinite(value) or value < 0.0 or value > 1.0: + raise ValueError(f"probability for {key} must be finite and in [0, 1]") + total = sum(self.probabilities.values()) + if abs(total - 1.0) > 1e-6: + raise ValueError(f"probabilities must sum to 1.0 within 1e-6, got {total}") + return self + + +class HeadlineArenaRunResult(BaseModel): + """End-to-end result of evaluating one challenge through the ARGUS committee.""" + + challenge: HeadlineArenaChallenge + passport: TradePassport + forecast: TernaryForecast + submitted: bool = False + dry_run: bool = True + response: dict[str, Any] = Field(default_factory=dict) + + +class CalibrationSummary(BaseModel): + """Local calibration and refusal-quality metrics over resolved ternary forecasts.""" + + count: int + brier_score: float + headline_arena_avg_score: float + directional_accuracy: float + refusal_count: int + refusal_calibration_rate: float + + +def normalize_origin(raw: str) -> str: + """Normalize a Headline Arena origin/base URL and enforce HTTPS outside localhost.""" + cleaned = raw.strip().rstrip("/").removesuffix("/api/v1") + parsed = urllib.parse.urlparse(cleaned) + if parsed.scheme != "https" and parsed.hostname not in ("localhost", "127.0.0.1"): + raise HeadlineArenaError( + f"Insecure Headline Arena base URL '{raw}': HTTPS is required except for localhost." + ) + if not parsed.netloc: + raise HeadlineArenaError(f"Invalid Headline Arena base URL '{raw}'.") + return cleaned + + +def build_model_attribution_fields( + provider: str | None = None, + name: str | None = None, + version: str | None = None, +) -> dict[str, str]: + """Validate optional per-prediction model attribution fields matching Headline Arena's gate.""" + p = provider.strip() if isinstance(provider, str) and provider.strip() else None + n = name.strip() if isinstance(name, str) and name.strip() else None + v = version.strip() if isinstance(version, str) and version.strip() else None + + if (p is None) != (n is None): + raise HeadlineArenaError("model_provider and model_name must be provided together or both omitted.") + if v is not None and p is None: + raise HeadlineArenaError("model_version requires both model_provider and model_name.") + + for label, value, limit in ( + ("model_provider", p, MODEL_PROVIDER_MAX), + ("model_name", n, MODEL_NAME_MAX), + ("model_version", v, MODEL_VERSION_MAX), + ): + if value is not None and len(value) > limit: + raise HeadlineArenaError(f"{label} must be at most {limit} characters.") + + if p is None or n is None: + return {} + fields: dict[str, str] = {"model_provider": p, "model_name": n} + if v is not None: + fields["model_version"] = v + return fields + + +def _normalize_ternary(bearish: float, neutral: float, bullish: float) -> dict[TernaryOutcome, float]: + """Clamp, normalize, and round a 3-way distribution to sum strictly to 1.0.""" + raw = { + "bearish": max(0.01, float(bearish)), + "neutral": max(0.01, float(neutral)), + "bullish": max(0.01, float(bullish)), + } + total = sum(raw.values()) + rounded: dict[TernaryOutcome, float] = { + "bearish": round(raw["bearish"] / total, 4), + "neutral": round(raw["neutral"] / total, 4), + "bullish": round(raw["bullish"] / total, 4), + } + drift = round(1.0 - sum(rounded.values()), 4) + if drift != 0.0: + anchor: TernaryOutcome = max(rounded, key=lambda k: rounded[k]) + rounded[anchor] = round(rounded[anchor] + drift, 4) + return rounded + + +def passport_to_ternary_forecast( + passport: TradePassport, + challenge: HeadlineArenaChallenge | None = None, +) -> TernaryForecast: + """ + Map an ARGUS ``TradePassport`` into a calibrated Headline Arena ``TernaryForecast``. + + Key design property: ARGUS refusals (``VETOED`` / ``REJECTED``) are expressed + as calibrated probability mass on ``neutral`` (with asymmetric trap tail risk + on vetoes) rather than dropped silently, so over-refusal and under-refusal + both surface directly on Headline Arena's Brier calibration curve. + """ + status = passport.consensus.status + score_strength = min(abs(passport.consensus.composite_score) / 100.0, 1.0) + confidence = min(max(passport.consensus.overall_confidence, 0.0), 1.0) + da_risk = min(max(passport.devils_advocate.disqualification_risk_pct / 100.0, 0.0), 1.0) + is_bullish = passport.direction == "LONG" + + if status == "VETOED": + # Devil's Advocate veto flags an active bull/bear trap: shift primary mass + # to neutral and assign slightly more residual probability against the trapped side. + p_neutral = 0.56 + 0.22 * da_risk + residual = 1.0 - p_neutral + p_proposed = residual * 0.35 + p_opposite = residual * 0.65 + elif status == "REJECTED": + # Insufficient directional edge: center on neutral with gentle directional lean. + p_neutral = 0.54 + 0.16 * (1.0 - score_strength) + residual = 1.0 - p_neutral + lean = 0.50 + 0.20 * score_strength + p_proposed = residual * lean + p_opposite = residual * (1.0 - lean) + elif status == "DOWNSIZED": + # Cautious approval: moderate directional probability tempered by DA risk. + p_proposed = 0.42 + 0.14 * (0.5 * confidence + 0.5 * score_strength) * (1.0 - 0.35 * da_risk) + p_neutral = 0.30 + 0.14 * da_risk + p_opposite = max(0.05, 1.0 - p_proposed - p_neutral) + else: + # Full committee approval: strong calibrated directional probability, never over-certain. + conviction = 0.55 * confidence + 0.45 * score_strength + p_proposed = 0.50 + 0.34 * conviction * (1.0 - 0.30 * da_risk) + p_neutral = max(0.08, (1.0 - p_proposed) * (0.58 + 0.18 * da_risk)) + p_opposite = max(0.04, 1.0 - p_proposed - p_neutral) + + # Honor challenge's frozen dead_zone_pct (wider neutral band shifts small bounded mass to neutral). + if challenge is not None and challenge.dead_zone_pct is not None and challenge.dead_zone_pct > 0.20: + band_shift = min(0.08, (challenge.dead_zone_pct - 0.20) * 0.12) + p_neutral += band_shift + p_proposed = max(0.05, p_proposed - band_shift * 0.65) + p_opposite = max(0.03, p_opposite - band_shift * 0.35) + + p_bull = p_proposed if is_bullish else p_opposite + p_bear = p_opposite if is_bullish else p_proposed + probs = _normalize_ternary(bearish=p_bear, neutral=p_neutral, bullish=p_bull) + + direction: TernaryOutcome = max(probs, key=lambda k: probs[k]) + calibrated_confidence = probs[direction] + + digest = compute_passport_digest(passport) + vote_summary = ", ".join( + f"{v.agent_name}={v.recommendation}({v.score:+.0f}, {v.confidence * 100:.0f}%)" + for v in passport.agent_votes + ) + traps = "; ".join(passport.devils_advocate.identified_traps[:2]) or "none" + criteria_note = ( + f" Resolution criteria reviewed: {challenge.resolution_criteria}" + if challenge and challenge.resolution_criteria + else "" + ) + dead_zone_note = ( + f" Frozen dead-zone band: +/-{challenge.dead_zone_pct:.2f}%." + if challenge and challenge.dead_zone_pct is not None + else "" + ) + + reasoning = ( + f"ARGUS 7-agent committee status={status} (proposed={passport.direction}, " + f"composite={passport.consensus.composite_score:+.1f}, " + f"confidence={passport.consensus.overall_confidence * 100:.0f}%). " + f"Votes: [{vote_summary}]. " + f"Devil's Advocate disqualification risk={passport.devils_advocate.disqualification_risk_pct:.1f}% " + f"(traps: {traps}). " + f"Calibrated ternary distribution: bearish={probs['bearish']:.4f}, " + f"neutral={probs['neutral']:.4f}, bullish={probs['bullish']:.4f}." + f"{dead_zone_note}{criteria_note}" + ) + + raw_summary = ( + f"ARGUS {status} ({passport.ticker}): {direction.upper()} {calibrated_confidence * 100:.0f}% " + f"| score {passport.consensus.composite_score:+.1f}, DA risk " + f"{passport.devils_advocate.disqualification_risk_pct:.0f}% | passport {passport.passport_id}" + ) + summary = raw_summary[:500] + + observed_at = passport.timestamp + if observed_at.endswith("+00:00"): + observed_at = observed_at[:-6] + "Z" + elif "Z" not in observed_at and "+" not in observed_at: + observed_at = observed_at + "Z" + + forecast_context: dict[str, Any] = { + "schema_version": "1", + "decision": "initial", + "observed_at": observed_at, + "run_ref": passport.passport_id[:128], + "evidence_refs": [ + { + "kind": "external", + "namespace": "argus-trade-passport", + "id": passport.passport_id[:128], + "content_sha256": digest, + } + ], + } + + challenge_id = challenge.challenge_id if challenge else f"offline-{passport.ticker.lower()}" + asset = challenge.asset if challenge else passport.ticker + + return TernaryForecast( + challenge_id=challenge_id, + asset=asset, + probabilities=probs, + direction=direction, + confidence=calibrated_confidence, + reasoning=reasoning, + summary=summary, + passport_id=passport.passport_id, + passport_digest=digest, + consensus_status=status, + disqualification_risk_pct=passport.devils_advocate.disqualification_risk_pct, + forecast_context=forecast_context, + ) + + +def compute_calibration_summary( + resolved_pairs: Sequence[tuple[TernaryForecast, TernaryOutcome]], +) -> CalibrationSummary: + """ + Compute multi-class Brier score, Headline Arena leaderboard score, and + refusal calibration metrics across resolved ``(TernaryForecast, outcome)`` pairs. + """ + if not resolved_pairs: + return CalibrationSummary( + count=0, + brier_score=0.0, + headline_arena_avg_score=0.0, + directional_accuracy=0.0, + refusal_count=0, + refusal_calibration_rate=0.0, + ) + + brier_total = 0.0 + ha_score_total = 0.0 + correct_count = 0 + refusal_count = 0 + refusal_well_calibrated = 0 + + for forecast, actual in resolved_pairs: + probs = forecast.probabilities + brier = sum((probs[k] - (1.0 if k == actual else 0.0)) ** 2 for k in ("bearish", "neutral", "bullish")) + brier_total += brier + + if forecast.direction == actual: + correct_count += 1 + + if forecast.direction == "neutral": + ha_score_total += 60.0 if actual == "neutral" else 40.0 + else: + if forecast.direction == actual: + ha_score_total += 50.0 + forecast.confidence * 50.0 + else: + ha_score_total += 50.0 - forecast.confidence * 50.0 + + if forecast.consensus_status in ("VETOED", "REJECTED"): + refusal_count += 1 + # A refusal is well-calibrated when the outcome resolved neutral OR the + # directional argmax did not fail badly (neutral probability beat uniform). + if actual == "neutral" or probs[actual] >= 0.20: + refusal_well_calibrated += 1 + + n = len(resolved_pairs) + return CalibrationSummary( + count=n, + brier_score=round(brier_total / n, 4), + headline_arena_avg_score=round(ha_score_total / n, 2), + directional_accuracy=round(correct_count / n, 4), + refusal_count=refusal_count, + refusal_calibration_rate=round(refusal_well_calibrated / refusal_count, 4) if refusal_count else 0.0, + ) + + +def challenge_to_market_inputs(challenge: HeadlineArenaChallenge) -> tuple[MarketContext, pd.DataFrame]: + """ + Convert a Headline Arena challenge (and its OHLC bars / headlines) into + ARGUS's ``(MarketContext, bars_df)`` inputs. + """ + bars_df = _build_ohlc_dataframe(challenge) + closes = bars_df["close"] + highs = bars_df["high"] + lows = bars_df["low"] + current_price = float(closes.iloc[-1]) + + delta = closes.diff() + gain = delta.clip(lower=0.0).rolling(14, min_periods=1).mean() + loss = (-delta.clip(upper=0.0)).rolling(14, min_periods=1).mean() + last_gain = float(gain.iloc[-1]) + last_loss = float(loss.iloc[-1]) + if last_loss == 0.0: + rsi_14 = 100.0 if last_gain > 0.0 else 50.0 + else: + rs = last_gain / last_loss + rsi_14 = 100.0 - (100.0 / (1.0 + rs)) + + tr = pd.concat( + [ + highs - lows, + (highs - closes.shift(1)).abs(), + (lows - closes.shift(1)).abs(), + ], + axis=1, + ).max(axis=1) + atr_14 = float(tr.rolling(14, min_periods=1).mean().iloc[-1]) + atr_14 = max(atr_14, current_price * 0.002) + + sma_50 = float(closes.rolling(min(50, len(closes)), min_periods=1).mean().iloc[-1]) + pct_vs_sma = ((current_price - sma_50) / sma_50) * 100.0 if sma_50 > 0 else 0.0 + vol_pct = (atr_14 / current_price) * 100.0 if current_price > 0 else 1.0 + + if challenge.vix_level >= 28.0 or vol_pct >= 2.2: + regime = "CHOPPY" + elif pct_vs_sma >= 0.45: + regime = "BULLISH" + elif pct_vs_sma <= -0.45: + regime = "BEARISH" + else: + regime = "NEUTRAL" + + news = list(challenge.recent_news) + if not news: + news = [ + f"{challenge.asset} trading near {current_price:.2f} ahead of challenge settlement window", + ] + if challenge.question: + news.append(challenge.question) + + ctx = MarketContext( + ticker=challenge.asset, + current_price=round(current_price, 4), + high_24h=round(float(highs.tail(24).max()), 4), + low_24h=round(float(lows.tail(24).min()), 4), + volume_24h=round(float(bars_df["volume"].tail(24).sum()), 2), + rsi_14=round(rsi_14, 2), + atr_14=round(atr_14, 4), + sma_50=round(sma_50, 4), + market_regime=regime, + vix_level=challenge.vix_level, + recent_news=news, + ) + return ctx, bars_df + + +def _build_ohlc_dataframe(challenge: HeadlineArenaChallenge) -> pd.DataFrame: + """Parse challenge OHLC bars into a UTC-indexed DataFrame or synthesize deterministic bars.""" + if len(challenge.ohlc_bars) >= 20: + rows: list[dict[str, Any]] = [] + timestamps: list[pd.Timestamp] = [] + for idx, bar in enumerate(challenge.ohlc_bars): + ts_raw = bar.get("timestamp") or bar.get("time") or bar.get("t") + ts = ( + pd.Timestamp(ts_raw, tz="UTC") + if ts_raw and pd.Timestamp(ts_raw).tzinfo is None + else ( + pd.Timestamp(ts_raw).tz_convert("UTC") + if ts_raw + else pd.Timestamp("2026-01-01T00:00:00Z") + pd.Timedelta(minutes=5 * idx) + ) + ) + c = float(bar.get("close", bar.get("c", challenge.open_price))) + o = float(bar.get("open", bar.get("o", c))) + h = float(bar.get("high", bar.get("h", max(o, c)))) + lo = float(bar.get("low", bar.get("l", min(o, c)))) + vol = float(bar.get("volume", bar.get("v", 10000.0)) or 10000.0) + timestamps.append(ts) + rows.append({"open": o, "high": h, "low": lo, "close": c, "volume": vol}) + df = pd.DataFrame(rows, index=pd.DatetimeIndex(timestamps)).sort_index() + return df + + # Deterministic fallback series anchored to challenge.open_price + seed = sum(ord(ch) for ch in f"{challenge.challenge_id}:{challenge.asset}") % (2**31) + rng = np.random.default_rng(seed) + n_bars = 50 + dates = pd.date_range("2026-09-01T00:00:00Z", periods=n_bars, freq="h") + returns = rng.normal(loc=0.0005, scale=0.003, size=n_bars) + prices = challenge.open_price * np.exp(np.cumsum(returns)) + prices = prices * (challenge.open_price / prices[-1]) + spread = challenge.open_price * 0.0025 + return pd.DataFrame( + { + "open": prices - spread * 0.25, + "high": prices + spread, + "low": prices - spread, + "close": prices, + "volume": np.full(n_bars, 25000.0), + }, + index=dates, + ) + + +def generate_offline_macro_challenges() -> list[HeadlineArenaChallenge]: + """ + Create four deterministic macro-futures challenges (GC, CL, ES, ZN) representing + distinct market regimes (constructive uptrend, overbought bull-trap veto, + choppy low-edge range, and bearish breakdown) for offline dry-run verification. + """ + base_ts = pd.date_range("2026-09-04T00:00:00Z", periods=50, freq="h") + + def _series_to_bars(closes: list[float], half_range: float, vol_base: float) -> list[dict[str, Any]]: + bars: list[dict[str, Any]] = [] + for idx, close_px in enumerate(closes): + open_px = closes[idx - 1] if idx > 0 else close_px + bars.append( + { + "timestamp": base_ts[idx].isoformat(), + "open": round(open_px, 4), + "high": round(max(open_px, close_px) + half_range, 4), + "low": round(min(open_px, close_px) - half_range, 4), + "close": round(close_px, 4), + "volume": vol_base * (1.4 if idx >= 45 else 1.0), + } + ) + return bars + + # 1. Gold futures (GC): Constructive bullish trend with healthy pullbacks (RSI ~58-64) -> APPROVED LONG + gc_closes: list[float] = [] + px = 2480.0 + for i in range(50): + step = 3.2 if (i % 3 != 2) else -3.4 + px += step + gc_closes.append(round(px, 2)) + + # 2. Crude oil futures (CL): Parabolic overbought spike + high VIX + euphoric headlines -> VETOED by Devil's Advocate + cl_closes = [72.0 + 0.08 * i for i in range(36)] + [74.9 + 0.65 * j for j in range(14)] + + # 3. S&P 500 E-mini (ES): Flat choppy oscillation -> REJECTED / neutral calibration + es_closes = [5520.0 + (8.0 if i % 2 == 0 else -8.0) for i in range(50)] + + # 4. 10Y Treasury Note (ZN): Clean bearish drift with relief bounces (RSI ~40-46) -> APPROVED SHORT + zn_closes: list[float] = [] + zpx = 116.5 + for i in range(50): + step = -0.14 if (i % 3 != 2) else 0.13 + zpx += step + zn_closes.append(round(zpx, 3)) + + return [ + HeadlineArenaChallenge( + challenge_id="offline-gc-20260905", + asset="GC", + question="Will COMEX Gold futures (GC) close bullish, neutral, or bearish over the next 24h?", + status="open", + deadline="2026-09-06T12:00:00Z", + open_price=round(gc_closes[-1], 2), + dead_zone_pct=0.15, + resolution_criteria="Resolves bullish above +0.15%, bearish below -0.15%, else neutral.", + scope_key="GC", + contract_symbol="GCZ26", + contract_month="2026-12", + ohlc_bars=_series_to_bars(gc_closes, half_range=4.5, vol_base=32000.0), + recent_news=[ + "Gold futures see strong central-bank inflow and bullish breakout momentum", + "Real yields ease following growth expansion and sovereign reserve upgrades", + ], + vix_level=16.2, + ), + HeadlineArenaChallenge( + challenge_id="offline-cl-20260905", + asset="CL", + question="Will WTI Crude Oil futures (CL) close bullish, neutral, or bearish over the next 24h?", + status="open", + deadline="2026-09-06T12:00:00Z", + open_price=round(cl_closes[-1], 2), + dead_zone_pct=0.30, + resolution_criteria="Resolves bullish above +0.30%, bearish below -0.30%, else neutral.", + scope_key="CL", + contract_symbol="CLV26", + contract_month="2026-10", + ohlc_bars=_series_to_bars(cl_closes, half_range=0.85, vol_base=45000.0), + recent_news=[ + "Crude futures surge on bullish breakout rally with strong momentum fund inflow", + "Analysts issue target upgrade after refinery throughput beat", + "Energy complex sees expansion in speculative long positioning", + ], + vix_level=29.5, + ), + HeadlineArenaChallenge( + challenge_id="offline-es-20260905", + asset="ES", + question="Will S&P 500 E-mini futures (ES) close bullish, neutral, or bearish over the next 24h?", + status="open", + deadline="2026-09-06T12:00:00Z", + open_price=round(es_closes[-1], 2), + dead_zone_pct=0.10, + resolution_criteria="Resolves bullish above +0.10%, bearish below -0.10%, else neutral.", + scope_key="ES", + contract_symbol="ESU26", + contract_month="2026-09", + ohlc_bars=_series_to_bars(es_closes, half_range=6.0, vol_base=50000.0), + recent_news=[ + "Equity index futures trade sideways ahead of payrolls release", + ], + vix_level=21.0, + ), + HeadlineArenaChallenge( + challenge_id="offline-zn-20260905", + asset="ZN", + question="Will 10-Year Treasury Note futures (ZN) close bullish, neutral, or bearish over the next 24h?", + status="open", + deadline="2026-09-06T12:00:00Z", + open_price=round(zn_closes[-1], 3), + dead_zone_pct=0.12, + resolution_criteria="Resolves bullish above +0.12%, bearish below -0.12%, else neutral.", + scope_key="ZN", + contract_symbol="ZNZ26", + contract_month="2026-12", + ohlc_bars=_series_to_bars(zn_closes, half_range=0.15, vol_base=40000.0), + recent_news=[ + "Treasury futures turn bearish on sustained duration fund outflow", + "Auction demand miss triggers fixed-income rating downgrade", + ], + vix_level=18.5, + ), + ] + + +class HeadlineArenaClient: + """ + HTTPS REST client for Headline Arena (``https://headlinearena.com/api/v1``). + + Interoperates seamlessly with credentials saved by ``headlinearena-agent-plugin`` + (``~/.headlinearena/credentials.json``) or environment variables ``HA_AGENT_ID`` / + ``HA_CLIENT_SECRET``. + """ + + def __init__(self, settings: HeadlineArenaSettings | None = None) -> None: + self.settings = settings or CONFIG.headline_arena + self.origin = normalize_origin(self.settings.base_url) + self._agent_id: str | None = None + self._client_secret: str | None = None + self._access_token: str | None = None + self._token_expires_at: float = 0.0 + self._load_credentials() + + def _load_credentials(self) -> None: + if self.settings.agent_id and self.settings.client_secret: + self._agent_id = self.settings.agent_id.strip() + self._client_secret = self.settings.client_secret.strip() + return + + cred_path = Path(self.settings.credentials_path).expanduser() + if not cred_path.exists(): + return + + try: + data = json.loads(cred_path.read_text(encoding="utf-8")) + except (OSError, ValueError) as exc: + logger.warning("[HEADLINE ARENA] Could not read credentials file %s: %s", cred_path, exc) + return + + if not isinstance(data, dict): + return + + entry = data.get(self.origin) or data.get(f"{self.origin}/api/v1") + if not isinstance(entry, dict): + return + + # Support both single-agent and multi-agent credential store formats from ha.py + if "agent_id" in entry and "client_secret" in entry: + self._agent_id = str(entry["agent_id"]) + self._client_secret = str(entry["client_secret"]) + return + + agents = entry.get("agents") + default_id = entry.get("default") + if isinstance(agents, dict) and agents: + target = ( + agents.get(self.settings.agent_id) + if self.settings.agent_id + else (agents.get(default_id) if default_id else next(iter(agents.values()))) + ) + if isinstance(target, dict) and target.get("agent_id") and target.get("client_secret"): + self._agent_id = str(target["agent_id"]) + self._client_secret = str(target["client_secret"]) + + @property + def has_credentials(self) -> bool: + return bool(self._agent_id and self._client_secret) + + @property + def agent_id(self) -> str | None: + return self._agent_id + + def _api_url(self, path: str) -> str: + return f"{self.origin}/api/v1{path}" + + def _http_json( + self, + method: str, + path: str, + *, + body: dict[str, Any] | None = None, + authenticated: bool = False, + extra_headers: dict[str, str] | None = None, + ) -> dict[str, Any]: + headers: dict[str, str] = { + "Accept": "application/json", + "Content-Type": "application/json", + "User-Agent": "argus-ai/2.2.0", + "X-Request-Id": str(uuid.uuid4()), + } + if authenticated: + token = self.get_access_token() + headers["Authorization"] = f"Bearer {token}" + if self._agent_id: + headers["X-Agent-Id"] = self._agent_id + if extra_headers: + headers.update(extra_headers) + + payload_bytes = json.dumps(body).encode("utf-8") if body is not None else None + req = urllib.request.Request( + self._api_url(path), + data=payload_bytes, + headers=headers, + method=method.upper(), + ) + try: + with urllib.request.urlopen(req, timeout=self.settings.timeout_seconds) as resp: + raw = resp.read().decode("utf-8") or "{}" + parsed: Any = json.loads(raw) + return parsed if isinstance(parsed, dict) else {"items": parsed} + except urllib.error.HTTPError as exc: + err_body = exc.read().decode("utf-8", errors="replace") + raise HeadlineArenaError(f"Headline Arena HTTP {exc.code} on {path}: {err_body}") from exc + except urllib.error.URLError as exc: + raise HeadlineArenaError(f"Cannot reach Headline Arena at {self.origin}{path}: {exc.reason}") from exc + + def get_access_token(self, force_refresh: bool = False) -> str: + """Obtain or reuse a short-lived Bearer token via client_credentials.""" + if not self.has_credentials: + raise HeadlineArenaError( + "Headline Arena credentials not configured. Set HA_AGENT_ID and HA_CLIENT_SECRET " + "or register with headlinearena-agent-plugin (`ha.py register`)." + ) + now = time.time() + if not force_refresh and self._access_token and (self._token_expires_at - TOKEN_REFRESH_MARGIN_SECONDS) > now: + return self._access_token + + resp = self._http_json( + "POST", + "/agent/auth/token", + body={ + "grant_type": "client_credentials", + "agent_id": self._agent_id, + "client_secret": self._client_secret, + }, + authenticated=False, + ) + token = resp.get("access_token") + if not isinstance(token, str) or not token: + raise HeadlineArenaError(f"Headline Arena token response missing access_token: {resp}") + expires_in = int(resp.get("expires_in", 900)) + self._access_token = token + self._token_expires_at = now + expires_in + return token + + def list_open_challenges( + self, + asset: str | None = None, + authenticated: bool = False, + ) -> list[HeadlineArenaChallenge]: + """Discover open financial challenges (public or authenticated active scopes).""" + if authenticated and self.has_credentials: + resp = self._http_json("GET", "/eval/challenges/active", authenticated=True) + raw_items = resp.get("challenges", []) + else: + resp = self._http_json("GET", "/eval/challenges?status=open", authenticated=False) + raw_items = resp.get("items", resp.get("challenges", [])) + + challenges: list[HeadlineArenaChallenge] = [] + asset_filter = asset.strip().upper() if asset else None + + for entry in raw_items: + if not isinstance(entry, dict): + continue + raw_challenge = entry.get("challenge") + item: dict[str, Any] = raw_challenge if isinstance(raw_challenge, dict) else entry + raw_ctx = entry.get("context") + ctx_bundle: dict[str, Any] = raw_ctx if isinstance(raw_ctx, dict) else {} + if str(item.get("status", "open")).lower() != "open": + continue + item_asset = str(item.get("asset") or item.get("scope_key") or "").upper() + if not item_asset: + continue + if asset_filter and item_asset != asset_filter: + continue + + open_price_raw = item.get("open_price") or ctx_bundle.get("current_price") or ctx_bundle.get("price") + try: + open_price = float(open_price_raw) if open_price_raw is not None else 0.0 + except (TypeError, ValueError): + open_price = 0.0 + if open_price <= 0.0: + continue + + raw_ohlc = ctx_bundle.get("ohlc") + ohlc_obj: dict[str, Any] = raw_ohlc if isinstance(raw_ohlc, dict) else {} + raw_bars = ohlc_obj.get("bars") + bars: list[dict[str, Any]] = ( + [b for b in raw_bars if isinstance(b, dict)] if isinstance(raw_bars, list) else [] + ) + + dead_zone_raw = item.get("dead_zone_pct") + dead_zone_pct = float(dead_zone_raw) if dead_zone_raw is not None else None + raw_resolution = item.get("resolution") + resolution: dict[str, Any] = raw_resolution if isinstance(raw_resolution, dict) else {} + criteria = str(item.get("resolution_criteria") or resolution.get("criteria") or "") + + challenges.append( + HeadlineArenaChallenge( + challenge_id=str(item.get("id") or item.get("challenge_id")), + asset=item_asset, + question=str(item.get("question") or ""), + status=str(item.get("status") or "open"), + deadline=str(item.get("deadline") or ""), + open_price=open_price, + dead_zone_pct=dead_zone_pct, + resolution_criteria=criteria, + scope_key=str(item.get("scope_key") or item_asset), + contract_symbol=item.get("contract_symbol"), + contract_month=item.get("contract_month"), + ohlc_bars=bars, + ) + ) + return challenges + + def fetch_market_context(self, asset: str, hours: int = 24, bar_limit: int = 60) -> dict[str, Any]: + """Fetch live quote, OHLC bars, and linked headlines for a single asset.""" + encoded = urllib.parse.quote(asset.strip().upper(), safe="") + bounded_hours = max(1, min(int(hours), 168)) + bounded_bars = max(1, min(int(bar_limit), 2016)) + return self._http_json( + "GET", + f"/eval/context/{encoded}?hours={bounded_hours}&bar_limit={bounded_bars}", + authenticated=False, + ) + + def build_prediction_payload( + self, + forecast: TernaryForecast, + *, + idempotency_key: str | None = None, + model_provider: str | None = None, + model_name: str | None = None, + model_version: str | None = None, + is_revision: bool = False, + ) -> dict[str, Any]: + """Construct the exact JSON payload expected by ``POST /eval/challenges/{id}/predict``.""" + payload: dict[str, Any] = { + "probabilities": dict(forecast.probabilities), + "direction": forecast.direction, + "confidence": forecast.confidence, + "reasoning": forecast.reasoning, + "summary": forecast.summary, + "is_revision": is_revision, + "forecast_context": forecast.forecast_context, + } + if idempotency_key: + payload["idempotency_key"] = idempotency_key + + provider = model_provider if model_provider is not None else self.settings.model_provider + name = model_name if model_name is not None else self.settings.model_name + version = model_version if model_version is not None else self.settings.model_version + payload.update(build_model_attribution_fields(provider, name, version)) + return payload + + def submit_forecast( + self, + forecast: TernaryForecast, + *, + idempotency_key: str | None = None, + model_provider: str | None = None, + model_name: str | None = None, + model_version: str | None = None, + is_revision: bool = False, + dry_run: bool = False, + ) -> dict[str, Any]: + """Submit a calibrated ternary forecast (stake-free financial track).""" + idem_key = idempotency_key or f"argus-{forecast.passport_id}" + payload = self.build_prediction_payload( + forecast, + idempotency_key=idem_key, + model_provider=model_provider, + model_name=model_name, + model_version=model_version, + is_revision=is_revision, + ) + if dry_run: + return { + "status": "dry_run", + "endpoint": self._api_url(f"/eval/challenges/{forecast.challenge_id}/predict"), + "idempotency_key": idem_key, + "payload": payload, + } + + encoded_id = urllib.parse.quote(forecast.challenge_id, safe="") + return self._http_json( + "POST", + f"/eval/challenges/{encoded_id}/predict", + body=payload, + authenticated=True, + extra_headers={"Idempotency-Key": idem_key}, + ) + + def get_scorecard(self, agent_id: str | None = None) -> dict[str, Any]: + """Fetch an agent's public Headline Arena scorecard and calibration stats.""" + target = (agent_id or self._agent_id or "").strip() + if not target: + raise HeadlineArenaError("agent_id is required to fetch a Headline Arena scorecard.") + encoded = urllib.parse.quote(target, safe="") + return self._http_json("GET", f"/eval/agents/{encoded}/scorecard", authenticated=False) + + def get_challenge_results(self, challenge_id: str) -> dict[str, Any]: + """Fetch public settlement results for a resolved challenge.""" + encoded = urllib.parse.quote(challenge_id.strip(), safe="") + return self._http_json("GET", f"/eval/challenges/{encoded}/results", authenticated=False) + + +class HeadlineArenaDesk: + """ + Orchestrates ARGUS committee deliberation on Headline Arena macro-futures + challenges and produces calibrated ternary forecasts with Trade Passport receipts. + """ + + def __init__( + self, + client: HeadlineArenaClient | None = None, + committee: CommitteeOrchestrator | None = None, + audit: AuditAgent | None = None, + use_llm: bool = False, + ) -> None: + self.client = client or HeadlineArenaClient() + self.committee = committee or CommitteeOrchestrator(use_llm=use_llm) + self.audit = audit or AuditAgent() + + def evaluate_challenge( + self, + challenge: HeadlineArenaChallenge, + *, + submit: bool = False, + account_equity: float = 100000.0, + ) -> HeadlineArenaRunResult: + """Run the ARGUS committee on one challenge and optionally submit to Headline Arena.""" + ctx, bars_df = challenge_to_market_inputs(challenge) + passport = self.committee.evaluate_opportunity(ctx=ctx, bars_df=bars_df, account_equity=account_equity) + self.audit.record_passport(passport) + forecast = passport_to_ternary_forecast(passport, challenge=challenge) + + response = self.client.submit_forecast(forecast, dry_run=not submit) + return HeadlineArenaRunResult( + challenge=challenge, + passport=passport, + forecast=forecast, + submitted=submit, + dry_run=not submit, + response=response, + ) + + def run_cycle( + self, + *, + asset: str | None = None, + live_discovery: bool = False, + submit: bool = False, + ) -> list[HeadlineArenaRunResult]: + """Discover challenges (offline deterministic or live Headline Arena) and evaluate each.""" + if live_discovery: + challenges = self.client.list_open_challenges( + asset=asset, + authenticated=self.client.has_credentials, + ) + else: + challenges = generate_offline_macro_challenges() + if asset: + target = asset.strip().upper() + challenges = [c for c in challenges if c.asset.upper() == target] + + return [self.evaluate_challenge(ch, submit=submit) for ch in challenges] diff --git a/docs/HEADLINE_ARENA_INTEGRATION.md b/docs/HEADLINE_ARENA_INTEGRATION.md new file mode 100644 index 0000000..dbedd37 --- /dev/null +++ b/docs/HEADLINE_ARENA_INTEGRATION.md @@ -0,0 +1,52 @@ +# Headline Arena Calibration Integration + +ARGUS AI integrates with [Headline Arena](https://headlinearena.com) so the 7-agent Investment Committee — including **Devil's Advocate vetoes and low-edge refusals** — can be evaluated externally on public, Brier-scored macro futures calibration curves (`GC`, `CL`, `ES`, `ZN`, etc.). + +## How Refusals Map to Calibration Curves + +Headline Arena's daily financial challenges are stake-free and Brier-scored over the canonical ternary simplex `{"bearish", "neutral", "bullish"}`. `argus_ai.integrations.headline_arena.passport_to_ternary_forecast` translates every immutable `TradePassport` into a normalized probability vector: + +| Committee Decision | Probability Allocation Behavior | +| :--- | :--- | +| **`APPROVED`** | Concentrates calibrated mass (`0.50–0.84`) on the proposed direction scaled by composite score and confidence, tempered by Devil's Advocate risk. | +| **`DOWNSIZED`** | Moderates directional probability (`0.42–0.56`) and expands `neutral` mass (`0.30–0.44`) to reflect Devil's Advocate caution. | +| **`REJECTED`** | Shifts primary mass to `neutral` (`0.54–0.70`) when directional edge is insufficient. | +| **`VETOED`** | Shifts primary mass to `neutral` (`0.56–0.78`, scaled by `disqualification_risk_pct`) and assigns heavier residual tail weight against the trapped direction. | + +When a challenge specifies a frozen `dead_zone_pct` wider than `0.20%`, a bounded adjustment shifts additional mass into `neutral` rather than hardcoding per-asset thresholds. + +Every forecast also attaches a bounded private `forecast_context` referencing the immutable `TradePassport` ID (`run_ref`) and its SHA-256 digest (`content_sha256`). + +## Setup + +### Option A: Reuse `headlinearena-agent-plugin` credentials +Register an agent once with [`headlinearena-agent-plugin`](https://github.com/headlinearena/headlinearena-agent-plugin): +```bash +python3 scripts/ha.py register --name argus-ai-desk --bio "ARGUS 7-agent adversarial committee" \ + --model-provider Google --model-name gemini-2.5-flash +``` +`HeadlineArenaClient` automatically reads `~/.headlinearena/credentials.json` (both single-agent and multi-agent store formats). + +### Option B: Environment variables +Set the optional variables in `.env`: +```ini +HA_BASE_URL=https://headlinearena.com +HA_AGENT_ID=agt_... +HA_CLIENT_SECRET=... +``` + +## CLI Usage + +```bash +# 1. Offline deterministic dry-run across synthetic macro futures (GC, CL, ES, ZN) +python -m argus_ai.cli headline-arena + +# 2. Discover live open Headline Arena challenges without submitting (dry-run) +python -m argus_ai.cli headline-arena --live-discovery --asset GC + +# 3. Discover and submit live calibrated forecasts (stake-free daily financial track) +python -m argus_ai.cli headline-arena --live-discovery --submit + +# 4. Inspect your public Headline Arena scorecard +python -m argus_ai.cli headline-arena --scorecard +``` diff --git a/tests/test_headline_arena.py b/tests/test_headline_arena.py new file mode 100644 index 0000000..75d5ff3 --- /dev/null +++ b/tests/test_headline_arena.py @@ -0,0 +1,407 @@ +"""Tests for the ARGUS AI Headline Arena calibration integration.""" + +from __future__ import annotations + +import io +import json +import urllib.error +from pathlib import Path +from typing import Any, Literal + +import pytest + +from argus_ai.backtest import compute_passport_digest +from argus_ai.cli import main as cli_main +from argus_ai.config.settings import HeadlineArenaSettings +from argus_ai.core.models import ( + AgentVote, + ConsensusDecision, + DevilsAdvocateReview, + TradePassport, +) +from argus_ai.integrations.headline_arena import ( + HeadlineArenaChallenge, + HeadlineArenaClient, + HeadlineArenaDesk, + HeadlineArenaError, + TernaryForecast, + build_model_attribution_fields, + challenge_to_market_inputs, + compute_calibration_summary, + generate_offline_macro_challenges, + normalize_origin, + passport_to_ternary_forecast, +) + + +def _make_passport( + *, + ticker: str = "GC", + direction: Literal["LONG", "SHORT"] = "LONG", + status: Literal["APPROVED", "DOWNSIZED", "REJECTED", "VETOED"] = "APPROVED", + composite_score: float = 65.0, + overall_confidence: float = 0.80, + da_risk: float = 18.0, + veto: bool = False, +) -> TradePassport: + return TradePassport( + ticker=ticker, + asset_class="MACRO_FUTURES", + direction=direction, + current_price=2550.0, + target_entry=2550.0, + stop_loss=2520.0 if direction == "LONG" else 2580.0, + take_profit=2610.0 if direction == "LONG" else 2490.0, + risk_reward_ratio=2.0, + shares_or_contracts=0.0 if status in ("VETOED", "REJECTED") else 10.0, + position_size_usd=0.0 if status in ("VETOED", "REJECTED") else 25500.0, + account_risk_usd=0.0 if status in ("VETOED", "REJECTED") else 1000.0, + account_risk_pct=0.0 if status in ("VETOED", "REJECTED") else 1.0, + consensus=ConsensusDecision( + status=status, + overall_confidence=overall_confidence, + composite_score=composite_score, + downsize_factor=0.0 if status in ("VETOED", "REJECTED") else (0.5 if status == "DOWNSIZED" else 1.0), + primary_catalyst="EMA alignment", + main_opposition="Exhaustion risk" if veto else "None", + executive_summary=f"Status {status}", + unanimous=status == "APPROVED", + ), + agent_votes=[ + AgentVote( + agent_name="Quant Agent", + recommendation="BUY" if direction == "LONG" else "SELL", + confidence=overall_confidence, + score=composite_score, + key_arguments=["Momentum trend"], + ) + ], + devils_advocate=DevilsAdvocateReview( + adversarial_thesis="Overbought spike" if veto else "Low friction", + identified_traps=["Overbought RSI + VIX spike"] if veto else [], + disqualification_risk_pct=da_risk, + veto_recommended=veto, + recommended_contingency="Stand aside" if veto else "Standard ATR stop", + severity_level="CRITICAL" if veto else "LOW", + ), + ) + + +class _FakeHTTPResponse: + def __init__(self, payload: dict[str, Any] | list[Any], status: int = 200) -> None: + self._raw = json.dumps(payload).encode("utf-8") + self.status = status + + def read(self) -> bytes: + return self._raw + + def __enter__(self) -> Any: + return self + + def __exit__(self, *args: object) -> None: + return None + + +def test_normalize_origin_and_model_attribution_validation() -> None: + assert normalize_origin("https://headlinearena.com/api/v1/") == "https://headlinearena.com" + assert normalize_origin("http://127.0.0.1:8080/api/v1") == "http://127.0.0.1:8080" + assert normalize_origin("http://localhost:8000") == "http://localhost:8000" + + with pytest.raises(HeadlineArenaError, match="HTTPS is required"): + normalize_origin("http://headlinearena.com") + with pytest.raises(HeadlineArenaError, match="Invalid Headline Arena base URL"): + normalize_origin("https://") + + assert build_model_attribution_fields(None, None, None) == {} + assert build_model_attribution_fields("Google", "gemini-2.5-flash", "2026-09") == { + "model_provider": "Google", + "model_name": "gemini-2.5-flash", + "model_version": "2026-09", + } + with pytest.raises(HeadlineArenaError, match="together"): + build_model_attribution_fields("Google", None, None) + with pytest.raises(HeadlineArenaError, match="requires both"): + build_model_attribution_fields(None, None, "v1") + with pytest.raises(HeadlineArenaError, match="at most 64"): + build_model_attribution_fields("x" * 65, "model", None) + + +def test_passport_to_ternary_forecast_calibration_across_verdicts() -> None: + approved = _make_passport(status="APPROVED", direction="LONG", composite_score=72.0, overall_confidence=0.85, da_risk=15.0) + downsized = _make_passport(status="DOWNSIZED", direction="LONG", composite_score=55.0, overall_confidence=0.70, da_risk=55.0) + rejected = _make_passport(status="REJECTED", direction="LONG", composite_score=12.0, overall_confidence=0.45, da_risk=25.0) + vetoed = _make_passport(status="VETOED", direction="LONG", composite_score=62.0, overall_confidence=0.75, da_risk=85.0, veto=True) + + narrow_ch = HeadlineArenaChallenge( + challenge_id="ch-gc-1", + asset="GC", + open_price=2550.0, + dead_zone_pct=0.15, + resolution_criteria="Resolves bullish > +0.15%, bearish < -0.15%, else neutral.", + ) + wide_ch = HeadlineArenaChallenge( + challenge_id="ch-cl-1", + asset="CL", + open_price=75.0, + dead_zone_pct=0.60, + ) + + fc_app = passport_to_ternary_forecast(approved, narrow_ch) + fc_down = passport_to_ternary_forecast(downsized, narrow_ch) + fc_rej = passport_to_ternary_forecast(rejected, narrow_ch) + fc_veto = passport_to_ternary_forecast(vetoed, narrow_ch) + fc_wide = passport_to_ternary_forecast(approved, wide_ch) + + for fc in (fc_app, fc_down, fc_rej, fc_veto, fc_wide): + assert abs(sum(fc.probabilities.values()) - 1.0) <= 1e-6 + assert fc.confidence == fc.probabilities[fc.direction] + + assert fc_app.direction == "bullish" + assert fc_app.probabilities["bullish"] > fc_down.probabilities["bullish"] + assert fc_down.probabilities["neutral"] > fc_app.probabilities["neutral"] + + # Refusals map primary probability mass to neutral + assert fc_rej.direction == "neutral" + assert fc_veto.direction == "neutral" + # Vetoed LONG allocates more tail risk to bearish (trap reversal) than bullish + assert fc_veto.probabilities["bearish"] > fc_veto.probabilities["bullish"] + + # Wider dead-zone shifts additional mass to neutral + assert fc_wide.probabilities["neutral"] > fc_app.probabilities["neutral"] + + # Cryptographic TradePassport traceability in forecast_context + expected_digest = compute_passport_digest(approved) + assert fc_app.passport_digest == expected_digest + assert fc_app.forecast_context["schema_version"] == "1" + assert fc_app.forecast_context["decision"] == "initial" + assert fc_app.forecast_context["run_ref"] == approved.passport_id + assert fc_app.forecast_context["evidence_refs"][0]["content_sha256"] == expected_digest + + +def test_ternary_forecast_simplex_validation_rejects_bad_vectors() -> None: + base = passport_to_ternary_forecast(_make_passport()) + with pytest.raises(ValueError, match="sum to 1.0"): + TernaryForecast( + **{ + **base.model_dump(), + "probabilities": {"bearish": 0.5, "neutral": 0.5, "bullish": 0.5}, + } + ) + + +def test_compute_calibration_summary_brier_and_refusals() -> None: + empty = compute_calibration_summary([]) + assert empty.count == 0 + assert empty.brier_score == 0.0 + + fc_bull = passport_to_ternary_forecast(_make_passport(status="APPROVED", direction="LONG", composite_score=75.0)) + fc_veto = passport_to_ternary_forecast(_make_passport(status="VETOED", direction="LONG", da_risk=85.0, veto=True)) + + summary = compute_calibration_summary( + [ + (fc_bull, "bullish"), + (fc_veto, "neutral"), + ] + ) + assert summary.count == 2 + assert summary.directional_accuracy == 1.0 + assert summary.refusal_count == 1 + assert summary.refusal_calibration_rate == 1.0 + assert 0.0 < summary.brier_score < 0.35 + assert summary.headline_arena_avg_score > 65.0 + + +def test_client_credentials_file_formats_auth_and_rest_calls(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + # 1. Single-agent credentials file format + single_creds = tmp_path / "single.json" + single_creds.write_text( + json.dumps({"https://headlinearena.com": {"agent_id": "agt_single", "client_secret": "sec_1"}}), + encoding="utf-8", + ) + c1 = HeadlineArenaClient( + HeadlineArenaSettings( + base_url="https://headlinearena.com", + agent_id="", + client_secret="", + credentials_path=str(single_creds), + ) + ) + assert c1.has_credentials is True + assert c1.agent_id == "agt_single" + + # 2. Multi-agent credentials file format + multi_creds = tmp_path / "multi.json" + multi_creds.write_text( + json.dumps( + { + "https://headlinearena.com": { + "default": "agt_two", + "agents": { + "agt_one": {"agent_id": "agt_one", "client_secret": "sec_one"}, + "agt_two": {"agent_id": "agt_two", "client_secret": "sec_two"}, + }, + } + } + ), + encoding="utf-8", + ) + c2 = HeadlineArenaClient( + HeadlineArenaSettings( + base_url="https://headlinearena.com", + agent_id="", + client_secret="", + credentials_path=str(multi_creds), + ) + ) + assert c2.agent_id == "agt_two" + + # 3. Mocked HTTP transport across token, challenges, market context, submit, scorecard, results + captured: list[tuple[str, str, dict[str, str], dict[str, Any] | None]] = [] + + def fake_urlopen(req: Any, timeout: float = 20.0) -> _FakeHTTPResponse: + url = req.full_url + body = json.loads(req.data.decode("utf-8")) if req.data else None + captured.append((req.get_method(), url, dict(req.header_items()), body)) + + if url.endswith("/agent/auth/token"): + return _FakeHTTPResponse({"access_token": "tok_abc", "expires_in": 3600}) + if "/eval/challenges/active" in url: + return _FakeHTTPResponse( + { + "challenges": [ + { + "challenge": { + "id": "gc-live-1", + "asset": "GC", + "status": "open", + "open_price": 2610.5, + "dead_zone_pct": 0.15, + "resolution_criteria": "Close vs open +/-0.15%", + }, + "context": {"ohlc": {"bars": []}}, + }, + { + "challenge": { + "id": "cl-closed", + "asset": "CL", + "status": "resolved", + "open_price": 75.0, + } + }, + ] + } + ) + if "/eval/context/GC" in url: + return _FakeHTTPResponse({"asset": "GC", "current_price": 2615.0, "ohlc": {"bars": []}}) + if url.endswith("/predict"): + return _FakeHTTPResponse({"prediction_id": "pred_99", "revision_number": 1, "status": "accepted"}) + if "/scorecard" in url: + return _FakeHTTPResponse({"agent_id": "agt_two", "avg_score": 78.5, "forecasting_skill": 0.84}) + if "/results" in url: + return _FakeHTTPResponse({"challenge_id": "gc-live-1", "outcome": "bullish"}) + raise AssertionError(f"Unexpected URL: {url}") + + monkeypatch.setattr("urllib.request.urlopen", fake_urlopen) + + challenges = c2.list_open_challenges(asset="GC", authenticated=True) + assert len(challenges) == 1 + assert challenges[0].challenge_id == "gc-live-1" + + ctx_resp = c2.fetch_market_context("GC", hours=24, bar_limit=48) + assert ctx_resp["asset"] == "GC" + + fc = passport_to_ternary_forecast(_make_passport(), challenges[0]) + dry = c2.submit_forecast(fc, dry_run=True) + assert dry["status"] == "dry_run" + assert dry["payload"]["probabilities"] == fc.probabilities + + live = c2.submit_forecast(fc, model_provider="Google", model_name="gemini-2.5-flash", dry_run=False) + assert live["prediction_id"] == "pred_99" + # Verify token was cached (only 1 token request despite active challenges + submit) + token_calls = [c for c in captured if c[1].endswith("/agent/auth/token")] + assert len(token_calls) == 1 + + submit_call = next(c for c in captured if c[1].endswith("/predict")) + assert submit_call[2]["Authorization"] == "Bearer tok_abc" + assert submit_call[2]["X-agent-id"] == "agt_two" + assert "Idempotency-key" in submit_call[2] + assert submit_call[3] is not None + assert submit_call[3]["model_provider"] == "Google" + + sc = c2.get_scorecard() + assert sc["avg_score"] == 78.5 + res = c2.get_challenge_results("gc-live-1") + assert res["outcome"] == "bullish" + + +def test_client_error_handling_for_missing_creds_and_http_errors( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + client = HeadlineArenaClient( + HeadlineArenaSettings( + base_url="https://headlinearena.com", + agent_id="", + client_secret="", + credentials_path=str(tmp_path / "missing.json"), + ) + ) + assert client.has_credentials is False + with pytest.raises(HeadlineArenaError, match="credentials not configured"): + client.get_access_token() + with pytest.raises(HeadlineArenaError, match="agent_id is required"): + client.get_scorecard() + + def raise_http(*args: Any, **kwargs: Any) -> Any: + raise urllib.error.HTTPError( + url="https://headlinearena.com/api/v1/eval/challenges", + code=403, + msg="Forbidden", + hdrs=None, # type: ignore[arg-type] + fp=io.BytesIO(b'{"detail":"missing scope"}'), + ) + + monkeypatch.setattr("urllib.request.urlopen", raise_http) + with pytest.raises(HeadlineArenaError, match="HTTP 403"): + client.list_open_challenges() + + +def test_offline_macro_desk_cycle_and_cli_integration(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + challenges = generate_offline_macro_challenges() + assert [c.asset for c in challenges] == ["GC", "CL", "ES", "ZN"] + + # Verify fallback synthetic bars path when challenge has no OHLC bars + bare = HeadlineArenaChallenge(challenge_id="bare-1", asset="GC", open_price=2500.0) + ctx_bare, df_bare = challenge_to_market_inputs(bare) + assert len(df_bare) == 50 + assert ctx_bare.ticker == "GC" + + desk = HeadlineArenaDesk( + client=HeadlineArenaClient( + HeadlineArenaSettings( + base_url="https://headlinearena.com", + agent_id="", + client_secret="", + credentials_path=str(tmp_path / "none.json"), + ) + ), + use_llm=False, + ) + results = desk.run_cycle(live_discovery=False, submit=False) + assert len(results) == 4 + statuses = {r.challenge.asset: r.passport.consensus.status for r in results} + assert all(r.passport.asset_class == "MACRO_FUTURES" for r in results) + # CL overbought spike + high VIX must trigger Devil's Advocate VETO -> neutral calibration + assert statuses["CL"] == "VETOED" + cl_res = next(r for r in results if r.challenge.asset == "CL") + assert cl_res.forecast.direction == "neutral" + + # CLI dry-run + asset filter + cli_main(["headline-arena", "--asset", "GC"]) + + # CLI scorecard path (mocked) + monkeypatch.setattr( + HeadlineArenaClient, + "get_scorecard", + lambda self, agent_id=None: {"agent_id": agent_id or "agt_demo", "avg_score": 81.2}, + ) + cli_main(["headline-arena", "--scorecard", "--agent-id", "agt_demo"])