File size: 4,845 Bytes
6edd399 aca822c 6edd399 aca822c 6edd399 49f8d21 e24a11d d66a82a 49f8d21 d66a82a 49f8d21 6edd399 cc586ae 6edd399 cc586ae 49f8d21 cc586ae 49f8d21 cc586ae 6edd399 49f8d21 cc586ae 6edd399 cc586ae 6edd399 49f8d21 cc586ae 49f8d21 2d0622b cc586ae 49f8d21 6edd399 2d0622b 6edd399 49f8d21 6edd399 557040d 49f8d21 d66a82a 49f8d21 557040d 49f8d21 6edd399 49f8d21 6edd399 aca822c | 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 | """app.py - FastAPI server exposing the AI SOC Gym environment.
Keeps the HuggingFace Space alive on port 7860 and exposes
the OpenEnv interface via HTTP so agents can interact remotely.
"""
import sys
import os
import uvicorn
from fastapi import FastAPI, HTTPException, Request
from pydantic import BaseModel
from typing import Optional, Dict, Any
# Ensure we can import from the root directory
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from environment import AIGymEnv
from tasks import BruteForceSSHTask, LateralMovementTask, APTMultiStageTask
from models import Action
app = FastAPI(
title="AI SOC Gym",
description="RL environment where LLM agents defend against real cyberattacks.",
version="1.0.0",
)
# Global environment instance
_env: Optional[AIGymEnv] = None
TASK_MAP = {
"brute": BruteForceSSHTask,
"lateral": LateralMovementTask,
"apt": APTMultiStageTask,
}
# ---------- Request/Response schemas ----------
class ResetRequest(BaseModel):
task: str = "brute"
seed: Optional[int] = 42
class StepRequest(BaseModel):
action: Dict[str, Any]
# ---------- Helper: clamp all floats in reward to strictly (0, 1) ----------
def _clamp(v: float) -> float:
"""Clamp to the same strict reward bounds used by the local grader."""
return max(0.05, min(0.95, float(v)))
def _safe_reward(reward_dict: dict) -> dict:
"""Recursively clamp all float values in a reward dict to strictly (0.05, 0.95).
The OpenEnv validator calls /step via REST and reads the reward.score directly
from the JSON response. This is the definitive safety net at the API boundary.
"""
result = {}
for k, v in reward_dict.items():
if isinstance(v, float):
result[k] = _clamp(v)
elif isinstance(v, dict):
result[k] = _safe_reward(v)
else:
result[k] = v
return result
# ---------- Endpoints ----------
@app.get("/")
def root():
return {
"name": "AI SOC Gym",
"version": "1.0.0",
"tasks": list(TASK_MAP.keys()),
"status": "running",
}
@app.post("/reset")
async def reset(request: Request):
global _env
try:
body = await request.json()
except Exception:
body = {}
if not isinstance(body, dict):
body = {}
task_name = body.get("task", "brute")
seed = body.get("seed", 42)
task_cls = TASK_MAP.get(task_name)
if task_cls is None:
raise HTTPException(
status_code=400,
detail=f"Unknown task: {task_name}. Choose from {list(TASK_MAP.keys())}"
)
_env = AIGymEnv(seed=seed)
_env.load_task(task_cls())
obs = _env.reset()
return {
"logs": [log.model_dump() for log in obs.logs],
"metadata": obs.metadata.model_dump(),
}
@app.post("/step")
async def step(request: Request):
global _env
if _env is None:
raise HTTPException(status_code=400, detail="Call /reset first")
try:
body = await request.json()
except Exception:
body = {}
# Accept BOTH formats:
# 1. {"action": {"type": ..., "target_type": ..., "target": ...}} (our format)
# 2. {"type": ..., "target_type": ..., "target": ...} (OpenEnv validator format)
if isinstance(body, dict) and "action" in body:
action_data = body["action"]
elif isinstance(body, dict) and "type" in body:
action_data = body # action sent directly
else:
raise HTTPException(status_code=422, detail="Missing required 'action' in JSON body")
try:
action = Action(**action_data)
except Exception as e:
raise HTTPException(status_code=422, detail=f"Invalid action: {e}")
obs, reward, done, info = _env.step(action)
raw_reward = reward.model_dump()
# CRITICAL: Clamp all reward floats to strictly (0.05, 0.95) at the API
# boundary. The OpenEnv validator reads reward.score directly from this
# JSON response — any exact 0.0 or 1.0 will fail Task Validation.
reward_dict = _safe_reward(raw_reward)
import sys
sys.stderr.write(f"\n[DEBUG] RAW REWARD: {raw_reward}\n")
sys.stderr.write(f"[DEBUG] SAFE REWARD: {reward_dict}\n")
return {
"observation": {
"logs": [log.model_dump() for log in obs.logs],
"metadata": obs.metadata.model_dump(),
},
"reward": reward_dict,
"done": done,
"info": info.model_dump(),
}
@app.get("/state")
def state():
global _env
if _env is None:
raise HTTPException(status_code=400, detail="Call /reset first")
return _env.state()
@app.get("/health")
def health():
return {"status": "ok"}
def main():
uvicorn.run("server.app:app", host="0.0.0.0", port=7860)
if __name__ == "__main__":
main()
|