"""JARVIS is the only sink for escalations; nothing sent there may dead-letter.""" import pytest from datetime import datetime from core.agent import MasterAgent from core.types import AgentMessage, AgentTask, MessageType, Priority def make_jarvis(): j = MasterAgent.__new__(MasterAgent) j.agent_id = "JARVIS" j._escalations = [] j._decision_log = [] return j @pytest.mark.asyncio async def test_exception_detected_message_lands_in_inbox(): j = make_jarvis() await j.handle_message(AgentMessage( message_id="m1", timestamp=datetime.now(), sender="DISPATCH_AGENT", recipient="JARVIS", message_type=MessageType.EXCEPTION_DETECTED, payload={"exception_type": "coverage_gap", "severity": "medium", "zone_id": "z"}, )) inbox = j.pending_escalations() assert len(inbox) == 1 assert inbox[0]["from"] == "DISPATCH_AGENT" assert inbox[0]["exception_type"] == "coverage_gap" @pytest.mark.asyncio async def test_human_review_task_is_recorded_not_dropped(): j = make_jarvis() task = AgentTask(task_id="t1", agent_type="master", task_type="human_review", priority=Priority.HIGH, data={"source": "EXCEPTION_AGENT", "exception_type": "miler_stalled", "severity": "high"}) result = await j.handle_task(task) assert result["status"] == "recorded" assert j.pending_escalations()[0]["from"] == "EXCEPTION_AGENT" @pytest.mark.asyncio async def test_inbox_is_newest_first_and_capped(): j = make_jarvis() for i in range(600): j._record_escalation("X", {"exception_type": "t", "severity": "low", "n": i}) assert len(j._escalations) == 500 assert j.pending_escalations(limit=3)[0]["payload"]["n"] == 599