""" Simulation schemas — output contract for Phase 12's simulation agent. SimulationResult is the final output of the entire CIRO pipeline. It is consumed by: Phase 13 agent trace log Mobile outcome visualization screen (before/after + actions) Mobile Maps screen bottom sheet (dispatch_ticket) """ from __future__ import annotations from datetime import datetime, timezone from typing import Literal from pydantic import BaseModel, ConfigDict, Field CongestionLevel = Literal["severe", "heavy", "moderate", "light", "clear"] AreaStatus = Literal["crisis active", "contained", "resolved"] ActionStatus = Literal["completed", "in_progress", "pending"] class SystemState(BaseModel): """Snapshot of the crisis area before or after response execution.""" model_config = ConfigDict(extra="forbid") congestion_level: CongestionLevel affected_area_status: AreaStatus vehicles_rerouted: int = Field(ge=0) emergency_units_deployed: int = Field(ge=0) citizens_alerted: int = Field(ge=0) ngos_activated: int = Field(ge=0) class ExecutedAction(BaseModel): """A single response action from the plan, with its simulated outcome.""" model_config = ConfigDict(extra="forbid") action: str assigned_to: str status: ActionStatus simulated_effect: str duration_minutes: int = Field(ge=0) class DispatchedUnit(BaseModel): """One emergency service unit dispatched to the crisis.""" model_config = ConfigDict(extra="forbid") service: str units: int = Field(ge=1) eta_minutes: int = Field(ge=1) class DispatchTicket(BaseModel): """Structured emergency dispatch record shown on the Maps screen bottom sheet. Generated deterministically for medium/high-risk crises that require emergency services. None for low-risk crises (traffic jam, rainfall, road_blockage). """ model_config = ConfigDict(extra="forbid") ticket_id: str # e.g. "CIR-2025-0047" status: str # "DISPATCHED" crisis_id: str crisis_type: str location_label: str severity: int = Field(ge=1, le=5) units_dispatched: list[DispatchedUnit] issued_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) class SimulationResult(BaseModel): """Full output of the simulation agent — final stage of the CIRO pipeline. Attributes: before: System state at crisis onset (deterministic). after: System state after response execution (LLM). actions_executed: Each plan action with its simulated effect (LLM). dispatch_ticket: Emergency dispatch record; None for low-risk crises. outcome_summary: Plain-English paragraph describing what happened. time_to_resolution_estimate: e.g. "25–40 minutes". """ model_config = ConfigDict(extra="forbid") before: SystemState after: SystemState actions_executed: list[ExecutedAction] dispatch_ticket: DispatchTicket | None outcome_summary: str time_to_resolution_estimate: str # ── Agent output shape (subset of SimulationResult) ─────────────────────────── # # The simulation agent generates only the variable parts — `before` and # `dispatch_ticket` are constructed deterministically and injected by # `run_simulation()` before the final SimulationResult is assembled. class SimulationAgentOutput(BaseModel): """Structured output contract for the simulation agent itself. `before` and `dispatch_ticket` are excluded because they are built deterministically from SituationReport severity and crisis type — the agent should not invent ticket IDs or severity mappings. """ model_config = ConfigDict(extra="forbid") after: SystemState actions_executed: list[ExecutedAction] outcome_summary: str time_to_resolution_estimate: str reasoning: str confidence: float = Field(default=0.8, ge=0.0, le=1.0)