File size: 9,827 Bytes
8308777 | 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | ---
title: Fraud Detection Env
emoji: π
colorFrom: red
colorTo: orange
sdk: docker
app_port: 8000
---
# Fraud Detection RL Environment
An OpenEnv-compatible **multi-agent** RL environment for training and evaluating
fraud detection policies.
Two agents interact every episode:
- **Defender** (LLM / PPO) β learns to detect and block fraudulent activity.
- **Fraudster** (LLM / PPO) β acts as an adaptive adversary trying to launder money.
The world is **partially observable** β agents see different, noisy views derived
from the same hidden ground truth.
---
## Quick start
```bash
# Install dependencies
pip install -e ".[dev]"
# or with uv:
uv sync
# Start the environment server
uv run server
# or: python -m scam_detection.server.app
# In a second terminal, run the LLM agent (inference)
export HF_TOKEN=<your_token>
export MODEL_NAME=Qwen/Qwen2.5-72B-Instruct
export API_BASE_URL=https://router.huggingface.co/v1
python inference.py
# Or run offline PPO training (no server needed)
python train.py --episodes 200 --task mule_cashout
# Evaluate trained vs baseline
python evaluate.py --episodes 20
```
With Docker:
```bash
docker build -t fraud-detection-env:latest .
docker run -p 8000:8000 fraud-detection-env:latest
python inference.py # connects via ENV_URL
```
---
## Fraud families (task variants)
| Task name | Description |
|----------------------|-------------|
| `refund_abuse` | Mule users repeatedly buy and refund to extract value |
| `mule_cashout` | Stolen funds layered through a chain of mule accounts |
| `merchant_collusion` | Colluding merchant processes fake high-value transactions |
| `account_takeover` | Compromised legitimate accounts make rapid large transfers |
| `random` | Random family chosen each episode |
---
## Episode structure
```
reset(task_name=...)
ββ FraudObservation:
task_name, step_budget, fraud_family_hint
defender_obs (partial, noisy)
fraudster_obs (partial, operational)
available_defender_actions + targets
available_fraudster_actions + targets
step(FraudAction(defender_action, fraudster_action, targets)) Γ N
ββ FraudObservation:
defender_obs / fraudster_obs (updated)
defender_reward / fraudster_reward
available_* actions
info.grade (at terminal step)
done, reason
```
Maximum steps per episode: **20** (configurable via `DEFAULT_MAX_STEPS`).
---
## Action space
### Defender actions
| Action | Description |
|--------|-------------|
| `monitor` | Place account under surveillance |
| `challenge` | Send step-up authentication challenge |
| `freeze` | Freeze account (high precision required) |
| `hold` | Hold a pending transaction |
| `block_merchant` | Block a suspicious merchant |
| `investigate_neighborhood` | Flag all connected accounts |
| `do_nothing` | No action |
### Fraudster actions
| Action | Description |
|--------|-------------|
| `split_payment` | Make small split payments to avoid thresholds |
| `rotate_mule` | Switch to a different mule account |
| `switch_merchant` | Use a different merchant |
| `rotate_device` | Change device to reduce device-reuse signal |
| `delay` | Lie low for 2 steps to lower detection pressure |
| `refund_abuse` | Exploit refund workflows for value extraction |
| `cashout_attempt` | Attempt to cash out via a fraud route |
| `do_nothing` | No action |
---
## Reward structure
### Defender per-step rewards
| Condition | Reward |
|-----------|--------|
| Fraud account frozen (TP) | +1.0 |
| Early detection bonus | up to +0.3 |
| Fraudulent transaction held | +0.8 |
| Colluding merchant blocked | +0.9 |
| Legit account frozen (FP) | -0.5 + -0.3 + -0.15 |
| Fraudster cashes out (missed) | -0.5 |
| Investigation cost | -0.05 |
### Fraudster per-step rewards
| Condition | Reward |
|-----------|--------|
| Successful cashout | +1.0 |
| Undetected activity | +0.05 |
| Evasion (rotate when pressure high) | +0.2 |
| Account frozen by defender | -0.5 |
| Merchant blocked | -0.4 |
| Failed cashout | -0.3 |
| Route deactivated | -0.2 |
---
## Observation contract
Agents never receive: ground-truth fraud labels, opponent's internal state, or raw reward equations.
```python
# Reset observation
obs.task_name # str β active fraud family
obs.step_budget # {"total": 20, "used": 0, "remaining": 20}
obs.defender_obs # dict β partial, noisy defender view
obs.fraudster_obs # dict β partial, operational fraudster view
obs.available_defender_actions # List[str]
obs.available_fraudster_actions # List[str]
obs.defender_action_targets # Dict[str, List[str]]
obs.fraudster_action_targets # Dict[str, List[str]]
# Step observation (adds)
obs.defender_reward # float
obs.fraudster_reward # float
obs.info["grade"] # dict β episode grade at terminal step
obs.done # bool
obs.reason # "max_steps" | "all_routes_blocked" | "fraud_threshold_exceeded"
```
### Defender observation fields
```python
defender_obs = {
"step": int,
"fraud_family_hint": str, # noisy hint (80% accurate)
"accounts": [{
"id", "transaction_velocity", "refund_ratio",
"device_reuse_count", "risk_score", # noisy
"is_frozen", "is_challenged", "is_monitored",
"balance_range",
}],
"merchants": [{
"id", "transaction_count", "refund_rate",
"anomaly_score", # noisy
"is_blocked", "is_monitored",
}],
"alerts": [{"type", "account_id"/"merchant_id", "step", ...}],
"aggregate": {
"total_frozen", "total_monitored", "total_flagged", "blocked_merchants",
"recent_transaction_count",
},
}
```
### Fraudster observation fields
```python
fraudster_obs = {
"step": int,
"alert_level": float, # 0=safe, 1=detected
"delayed_steps_remaining": int,
"active_routes": [{
"id", "family", "detection_pressure",
"mule_status": [{"id", "is_frozen", "balance", "device_id"}],
"merchant_blocked", "total_laundered", "cashout_ready", "steps_active",
}],
"available_mule_ids": List[str],
"any_cashout_ready": bool,
"total_laundered_so_far": float,
}
```
---
## Episode grading metrics (terminal only)
| Metric | Description |
|--------|-------------|
| `total_fraud_prevented` | Amount of fraud stopped |
| `total_fraud_escaped` | Amount laundered |
| `false_positive_count` | Legit accounts/merchants wrongly actioned |
| `false_positive_rate` | FP / (FP + TP) |
| `detection_delay` | Average step of first true-positive action |
| `customer_friction_score` | Proportion of legit users disrupted |
| `merchant_disruption_score` | Proportion of legit merchants blocked |
| `defender_score` | Composite 0β1 |
| `fraudster_score` | Composite 0β1 |
Grading is saved to `episode_grades.csv`. Full rollout history (per-step
observations, actions, rewards) is saved to `rollout_history.csv` for
fine-tuning.
---
## Project structure
```
scam_detection/
βββ server/
β βββ app.py # FastAPI server
β βββ fraud_environment.py # FraudEnvironment (OpenEnv interface)
βββ models.py # FraudAction, FraudObservation
βββ constants.py # All constants
βββ hidden_world_state.py # Dataclasses for hidden world
βββ scenario_generator.py # ScenarioGenerator (world gen per family)
βββ observation_generator.py # ObservationGenerator (partial views)
βββ action_processor.py # ActionMasker + ActionProcessor
βββ transition_engine.py # TransitionEngine (world evolution)
βββ reward_engine.py # RewardEngine (per-step rewards)
βββ termination_engine.py # TerminationEngine (episode end)
βββ grading_engine.py # GradingEngine (episode metrics + CSV)
βββ policy_networks.py # DefenderPolicy + FraudsterPolicy (PPO)
βββ ppo_trainer.py # PPOTrainer (online training loop)
βββ baseline_detector.py # BaselineRuleDetector (non-RL baseline)
βββ client.py # FraudEnvClient (typed async client)
βββ inference.py # LLM agent runner (both agents)
βββ train.py # PPO training script
βββ evaluate.py # Evaluation + baseline comparison
βββ tests/
β βββ test_smoke.py # Smoke tests
βββ pyproject.toml
βββ Dockerfile
βββ README.md
```
---
## OpenEnv lifecycle
```python
from scam_detection.client import FraudEnvClient
from scam_detection.models import FraudAction, DefenderActionType, FraudsterActionType
async with FraudEnvClient(base_url="http://localhost:8000") as env:
# 1. Reset
result = await env.reset(task_name="mule_cashout", seed=42)
obs = result.observation
# 2. Steps
for _ in range(20):
action = FraudAction(
defender_action=DefenderActionType.MONITOR,
defender_target=obs.defender_action_targets.get("monitor", [None])[0],
fraudster_action=FraudsterActionType.CASHOUT_ATTEMPT,
fraudster_target=(obs.fraudster_action_targets.get("cashout_attempt") or [None])[0],
)
result = await env.step(action)
obs = result.observation
if obs.done:
print("Episode done:", obs.reason)
print("Grade:", obs.info.get("grade"))
break
```
---
## Environment variables
| Variable | Description |
|----------|-------------|
| `API_BASE_URL` | OpenAI-compatible LLM endpoint |
| `MODEL_NAME` | Model identifier |
| `HF_TOKEN` / `API_KEY` | Authentication key |
| `ENV_URL` | Environment server URL (default: http://localhost:8000) |
| `PORT` | Server port (default: 8000) |
|