File size: 10,557 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 | """
Data models for the Fraud Detection RL Environment.
Design rationale
----------------
OpenEnv's ``create_app`` accepts exactly **one** Action class and **one**
Observation class. For the multi-agent setup (defender + fraudster), we
use a **unified superset** approach:
* ``FraudAction`` β one class containing fields for BOTH agents.
Each ``step()`` call uses only the fields relevant
to the agent whose turn it is. The other agent's
fields default to ``do_nothing``.
* ``FraudObservation`` β one class carrying partial observations for both
agents (``defender_obs`` and ``fraudster_obs``).
The ``current_agent`` field indicates whose turn
it is; only that agent's observation fields are
populated in a given response.
Turn-based interaction protocol
--------------------------------
1. ``reset()`` β returns **fraudster** observation (current_agent="fraudster").
2. Fraudster ``step()`` β sends fraudster_action; environment processes it
and returns **defender** observation (current_agent="defender").
3. Defender ``step()`` β sends defender_action; environment runs the full
world transition, computes rewards, checks termination,
and returns the **next fraudster** observation
(current_agent="fraudster") or a terminal observation.
4. Repeat from step 2 until ``done=True``.
Only the defender can trigger episode termination (termination is checked
exclusively after the defender's half-step).
"""
from __future__ import annotations
from enum import Enum
from typing import Any, Dict, List, Optional
from openenv.core.env_server.types import Action, Observation
from pydantic import Field
# ---------------------------------------------------------------------------
# Enumerations
# ---------------------------------------------------------------------------
class DefenderActionType(str, Enum):
"""Actions available to the defender agent."""
MONITOR = "monitor"
CHALLENGE = "challenge"
FREEZE = "freeze"
HOLD = "hold"
BLOCK_MERCHANT = "block_merchant"
INVESTIGATE_NEIGHBORHOOD = "investigate_neighborhood"
DO_NOTHING = "do_nothing"
class FraudsterActionType(str, Enum):
"""Actions available to the fraudster agent."""
SPLIT_PAYMENT = "split_payment"
ROTATE_MULE = "rotate_mule"
SWITCH_MERCHANT = "switch_merchant"
ROTATE_DEVICE = "rotate_device"
DELAY = "delay"
REFUND_ABUSE = "refund_abuse"
CASHOUT_ATTEMPT = "cashout_attempt"
DO_NOTHING = "do_nothing"
class FraudFamily(str, Enum):
"""Supported fraud scenario families."""
REFUND_ABUSE = "refund_abuse"
MULE_CASHOUT = "mule_cashout"
MERCHANT_COLLUSION = "merchant_collusion"
ACCOUNT_TAKEOVER = "account_takeover"
RANDOM = "random"
# ---------------------------------------------------------------------------
# Unified Action (both agents in one payload)
# ---------------------------------------------------------------------------
class FraudAction(Action):
"""
A single step action combining decisions from both agents.
The defender submits its ``defender_action`` and optional
``defender_target`` (account/transaction ID). The fraudster
submits ``fraudster_action`` and optional ``fraudster_target``.
Either field may be ``do_nothing`` when an agent passes its turn.
Example β combined action::
FraudAction(
defender_action="freeze",
defender_target="user_003",
fraudster_action="rotate_mule",
fraudster_target="mule_002",
)
"""
defender_action: DefenderActionType = Field(
default=DefenderActionType.DO_NOTHING,
description="Defender's action for this step.",
)
defender_target: Optional[str] = Field(
default=None,
description="Target ID (account/transaction/merchant) for defender.",
)
fraudster_action: FraudsterActionType = Field(
default=FraudsterActionType.DO_NOTHING,
description="Fraudster's action for this step.",
)
fraudster_target: Optional[str] = Field(
default=None,
description="Target ID (mule/merchant/device) for fraudster.",
)
def model_dump(self, *args, **kwargs):
kwargs.setdefault("exclude_none", True)
return super().model_dump(*args, **kwargs)
def model_dump_json(self, *args, **kwargs):
kwargs.setdefault("exclude_none", True)
return super().model_dump_json(*args, **kwargs)
# ---------------------------------------------------------------------------
# Unified Observation (partial views for both agents)
# ---------------------------------------------------------------------------
class FraudObservation(Observation):
"""
Observation returned by the environment at every step.
Contains partial views for both agents derived from the same hidden
world state. Neither agent receives ground-truth fraud labels directly.
ββ Shared fields βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
episode_id : current episode identifier
step : current step number
step_budget : remaining step budget
task_name : active fraud family
episode_done : True when episode has ended
reason : termination reason ("max_steps" | "all_fraud_done" | "all_routes_blocked")
ββ Agent observations (partial) ββββββββββββββββββββββββββββββββββββββββββ
defender_obs : partial noisy observation for the defender
fraudster_obs : partial operational observation for the fraudster
ββ Action masks (legal actions this step) βββββββββββββββββββββββββββββββββ
available_defender_actions : list of legal defender actions
available_fraudster_actions : list of legal fraudster actions
ββ Rewards (per-step) ββββββββββββββββββββββββββββββββββββββββββββββββββββ
defender_reward : step reward for the defender
fraudster_reward : step reward for the fraudster
ββ Info βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
info : reward_breakdown + cumulative + debug
"""
# ββ Shared βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
episode_id: Optional[str] = Field(default=None)
step: Optional[int] = Field(default=None)
step_budget: Optional[Dict[str, int]] = Field(
default=None, description="Keys: total, used, remaining."
)
task_name: Optional[str] = Field(
default=None, description="Active fraud family."
)
episode_done: Optional[bool] = Field(
default=None, description="True when episode has ended."
)
reason: Optional[str] = Field(
default=None,
description="Termination reason: max_steps | all_fraud_done | all_routes_blocked.",
)
# ββ Partial agent observations ββββββββββββββββββββββββββββββββββββββββββββ
defender_obs: Optional[Dict[str, Any]] = Field(
default=None,
description=(
"Defender's partial, noisy view: transaction velocities, "
"refund ratios, device reuse counts, merchant anomaly scores, "
"freeze/flag status, alerts."
),
)
fraudster_obs: Optional[Dict[str, Any]] = Field(
default=None,
description=(
"Fraudster's partial operational view: available mule routes, "
"detection pressure, cashout readiness, account balances."
),
)
# ββ Action masks βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
available_defender_actions: Optional[List[str]] = Field(
default=None,
description="Legal defender actions given current state.",
)
available_fraudster_actions: Optional[List[str]] = Field(
default=None,
description="Legal fraudster actions given current state.",
)
defender_action_targets: Optional[Dict[str, List[str]]] = Field(
default=None,
description="Maps each legal defender action β list of valid target IDs.",
)
fraudster_action_targets: Optional[Dict[str, List[str]]] = Field(
default=None,
description="Maps each legal fraudster action β list of valid target IDs.",
)
# ββ Turn indicator ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
current_agent: Optional[str] = Field(
default=None,
description=(
"Which agent should act next: 'fraudster' or 'defender'. "
"None when the episode is done."
),
)
# ββ Per-step rewards ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
defender_reward: Optional[float] = Field(default=None)
fraudster_reward: Optional[float] = Field(default=None)
# ββ Info / diagnostics ββββββββββββββββββββββββββββββββββββββββββββββββββββ
info: Optional[Dict[str, Any]] = Field(
default=None,
description="reward_breakdown + cumulative totals + debug.",
)
|