File size: 6,764 Bytes
2b9a95b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"""
CausalGame v2.0 - FastAPI Application Entry Point.

This is a minimal entry point that:
1. Creates the FastAPI application
2. Loads experiment configuration
3. Initializes session manager
4. Includes all routers

Architecture:
- middleware/: DroneSheet, DroneState, visibility control
- modules/agent/: Agent action space and endpoints
- modules/environment/: SCM implementations
- modules/game/: Pure judgment functions
- admin/: Admin/frontend endpoints
"""

import os
import json
import logging
from pathlib import Path
from typing import Dict, Any, Optional
from contextlib import asynccontextmanager

from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware

from .routers import setup_routers
from .modules.agent.session import SessionManager
from .modules.agent.endpoints import set_session_manager
from .modules.environment.scm_registry import get_scm_for_experiment
from .security import ensure_admin_token_in_env

# Load .env before touching any os.environ lookups below.
try:
    from dotenv import load_dotenv  # type: ignore
    load_dotenv()
except ImportError:
    pass


# Logging setup
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


# ============================================================
# Configuration Loading
# ============================================================

def get_experiment_name() -> str:
    """Get experiment name from environment variable."""
    return os.environ.get("CAUSALGAME_EXPERIMENT", "antenna_trap")


def load_experiment_config(experiment_name: str) -> Dict[str, Any]:
    """
    Load experiment configuration from experiments/<name>/game.json.
    """
    # Try multiple possible locations
    possible_paths = [
        Path(__file__).parent.parent / "experiments" / experiment_name / "game.json",
        Path("experiments") / experiment_name / "game.json",
        Path("/app/experiments") / experiment_name / "game.json",
    ]

    for config_path in possible_paths:
        if config_path.exists():
            try:
                with open(config_path, 'r') as f:
                    config = json.load(f)
                    config['experiment'] = {'name': experiment_name}
                    logger.info(f"Loaded config from {config_path}")
                    return config
            except Exception as e:
                logger.warning(f"Failed to load {config_path}: {e}")

    # Return default config if no file found
    logger.warning(f"No config found for experiment '{experiment_name}', using defaults")
    return {
        'experiment': {'name': experiment_name},
        'resources': {
            'total_drone_budget': 200,
            'stage2_fleet_size': 1000,
            'victory_threshold': 0.55,
        },
        'agility_system': {
            'base_agility': 1.0,
            'linear_coefficient': 0.002,
            'exponential_decay_scale': 200.0,
            'min_agility': 0.1,
        }
    }


# ============================================================
# Application State
# ============================================================

class AppState:
    """Global application state."""

    def __init__(self):
        self.config: Dict[str, Any] = {}
        self.session_manager: Optional[SessionManager] = None
        self.experiment_name: str = ""

    def initialize(self, experiment_name: str) -> None:
        """Initialize application state."""
        self.experiment_name = experiment_name
        self.config = load_experiment_config(experiment_name)
        self.session_manager = SessionManager(self.config)

        # Set session manager for agent endpoints
        set_session_manager(self.session_manager)

        logger.info(f"Initialized app state for experiment: {experiment_name}")


# Global app state
app_state = AppState()


# ============================================================
# Lifespan Management
# ============================================================

@asynccontextmanager
async def lifespan(app: FastAPI):
    """
    Application lifespan management.

    Handles startup and shutdown events.
    """
    # Startup
    ensure_admin_token_in_env()
    experiment_name = get_experiment_name()
    app_state.initialize(experiment_name)
    logger.info(f"CausalGame v2.0 starting with experiment: {experiment_name}")

    yield

    # Shutdown
    logger.info("CausalGame v2.0 shutting down")


# ============================================================
# Application Factory
# ============================================================

def create_app() -> FastAPI:
    """
    Create and configure the FastAPI application.

    Returns:
        Configured FastAPI application
    """
    app = FastAPI(
        title="CausalGame v2.0",
        description="AI Agent Causal Reasoning Testbed",
        version="2.0.0",
        lifespan=lifespan,
    )

    # CORS middleware
    app.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )

    # Setup routers
    setup_routers(app)

    # Static files (for frontend)
    # NOTE: Only mount static files in production (Docker).
    # In development, use Vite dev server (npm run dev) for frontend.
    # Mounting to "/" would override API routes, so we only do this in Docker
    # where the app is the only server running.
    import os
    if os.environ.get("CAUSALGAME_SERVE_STATIC", "").lower() == "true":
        possible_static_paths = [
            Path(__file__).parent.parent / "dist",  # Local development
            Path("/app/dist"),  # Docker container
        ]
        for static_path in possible_static_paths:
            if static_path.exists():
                app.mount("/", StaticFiles(directory=str(static_path), html=True), name="static")
                logger.info(f"Mounted static files from {static_path}")
                break

    return app


# ============================================================
# Application Instance
# ============================================================

# Create the application
app = create_app()


# ============================================================
# Utility Functions
# ============================================================

def get_app_state() -> AppState:
    """Get global application state."""
    return app_state


def get_config() -> Dict[str, Any]:
    """Get current experiment configuration."""
    return app_state.config


def get_session_manager() -> SessionManager:
    """Get session manager."""
    if app_state.session_manager is None:
        raise RuntimeError("Application not initialized")
    return app_state.session_manager