File size: 9,540 Bytes
7f84b94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
TransitionEngine — updates the hidden world state after each step.

Processing order (per step):
  1. Fraudster action is applied first.
  2. Defender action is applied second.
  3. World evolves: new transactions generated, risk scores updated,
     fraud routes age, delayed consequences take effect.

The engine also handles:
  - Delayed consequences (held transactions, challenged accounts)
  - Fraudster adaptation after heavy defender pressure
  - Risk score propagation (connected accounts inherit elevated risk)
"""
from __future__ import annotations

import random
import uuid
from typing import List

from scam_detection.constants import (
    MAX_TRANSACTIONS_PER_STEP,
    MIN_TRANSACTIONS_PER_STEP,
)
from scam_detection.hidden_world_state import (
    FraudRoute,
    HiddenWorldState,
    Transaction,
    UserNode,
)
from scam_detection.action_processor import ActionResult


class TransitionEngine:
    """
    Applies post-action world state updates for one step.

    Call ``advance(world, fraudster_result, defender_result)`` after both
    agents' actions have been processed by ``ActionProcessor``.
    """

    def advance(
        self,
        world: HiddenWorldState,
        fraudster_result: ActionResult,
        defender_result: ActionResult,
    ) -> None:
        """
        Advance the world state by one step.

        Steps:
          1. Decrement fraudster delay counter.
          2. Generate organic background transactions.
          3. Age fraud routes and escalate aggressiveness.
          4. Propagate risk scores.
          5. Generate defender alerts.
          6. Apply adaptation (fraudster reacts to high detection pressure).
          7. Check whether fraud routes are exhausted.
          8. Increment step counter.
        """
        rng = random.Random(world.seed ^ world.step ^ 0xABC)

        self._tick_delay(world)
        self._generate_transactions(world, rng)
        self._age_routes(world, rng)
        self._propagate_risk(world)
        self._generate_alerts(world, fraudster_result, defender_result)
        self._fraudster_adaptation(world, rng)
        self._deactivate_exhausted_routes(world)

        world.step += 1

    # =========================================================================
    # Sub-steps
    # =========================================================================

    def _tick_delay(self, world: HiddenWorldState) -> None:
        if world.fraudster_delayed_steps > 0:
            world.fraudster_delayed_steps -= 1

    def _generate_transactions(
        self, world: HiddenWorldState, rng: random.Random
    ) -> None:
        """Generate organic (background) transactions this step."""
        n = rng.randint(MIN_TRANSACTIONS_PER_STEP, MAX_TRANSACTIONS_PER_STEP)
        user_ids  = list(world.users.keys())
        merch_ids = list(world.merchants.keys())
        if not user_ids or not merch_ids:
            return

        for _ in range(n):
            user_id    = rng.choice(user_ids)
            merch_id   = rng.choice(merch_ids)
            user       = world.users[user_id]
            merchant   = world.merchants[merch_id]

            amount = rng.uniform(10.0, 200.0)
            if user.balance < amount:
                continue  # insufficient funds

            tx_id = f"tx_{world.step:03d}_{uuid.uuid4().hex[:6]}"
            is_fraud = user.is_fraudster_controlled and not user.is_frozen

            # Fraudster-controlled accounts make larger, more frequent transactions
            if is_fraud and world.fraudster_delayed_steps == 0:
                amount *= rng.uniform(2.0, 5.0)
                amount = min(amount, user.balance * 0.8)

            tx = Transaction(
                id=tx_id,
                from_user_id=user_id,
                to_merchant_id=merch_id,
                amount=round(amount, 2),
                timestamp=world.step,
                is_fraud=is_fraud,
                device_id=user.device_id,
                fraud_family=world.fraud_family if is_fraud else None,
            )
            user.balance -= amount
            user.transaction_count += 1
            merchant.transaction_count += 1
            merchant.total_volume += amount

            world.transactions.append(tx)

    def _age_routes(
        self, world: HiddenWorldState, rng: random.Random
    ) -> None:
        """Age active fraud routes and optionally escalate aggressiveness."""
        for route in world.fraud_routes:
            if not route.is_active:
                continue
            route.steps_active += 1

            # Gradually increase detection pressure from organic monitoring
            route.detection_pressure = min(
                1.0, route.detection_pressure + rng.uniform(0.01, 0.05)
            )

            # Escalate aggressiveness over time if alert level is low
            if world.fraudster_alert_level < 0.3 and route.steps_active % 5 == 0:
                world.fraud_aggressiveness = min(1.0, world.fraud_aggressiveness + 0.05)

    def _propagate_risk(self, world: HiddenWorldState) -> None:
        """
        Propagate elevated risk scores to accounts sharing a device or
        appearing in the same fraud route as known bad actors.
        """
        # Device-based propagation
        device_risk: dict = {}
        for uid, user in world.users.items():
            dev = user.device_id
            if user.risk_score > 0.5:
                device_risk[dev] = max(device_risk.get(dev, 0.0), user.risk_score * 0.5)

        for uid, user in world.users.items():
            inherited = device_risk.get(user.device_id, 0.0)
            if inherited > user.risk_score:
                user.risk_score = min(1.0, user.risk_score * 0.8 + inherited * 0.2)

        # Mule-network propagation
        for route in world.fraud_routes:
            if not route.is_active:
                continue
            for mid in route.mule_ids:
                if mid in world.users:
                    world.users[mid].risk_score = min(
                        1.0, world.users[mid].risk_score + 0.05
                    )

    def _generate_alerts(
        self,
        world: HiddenWorldState,
        fraudster_result: ActionResult,
        defender_result: ActionResult,
    ) -> None:
        """Generate alerts for the defender based on recent activity."""
        # Alert on high-velocity accounts
        step_txs = [tx for tx in world.transactions if tx.timestamp == world.step]
        velocity_map: dict = {}
        for tx in step_txs:
            velocity_map[tx.from_user_id] = velocity_map.get(tx.from_user_id, 0) + 1

        for uid, count in velocity_map.items():
            if count >= 3:
                world.defender_alerts.append({
                    "type": "high_velocity",
                    "account_id": uid,
                    "count": count,
                    "step": world.step,
                })

        # Alert on high device-reuse
        for uid, user in world.users.items():
            dev = world.devices.get(user.device_id)
            if dev and dev.reuse_count >= 4:
                world.defender_alerts.append({
                    "type": "device_reuse",
                    "account_id": uid,
                    "device_id": user.device_id,
                    "reuse_count": dev.reuse_count,
                    "step": world.step,
                })

        # Alert on high merchant refund rate
        for mid, merchant in world.merchants.items():
            if merchant.refund_rate > 0.4 and not merchant.is_blocked:
                world.defender_alerts.append({
                    "type": "high_refund_rate",
                    "merchant_id": mid,
                    "refund_rate": round(merchant.refund_rate, 3),
                    "step": world.step,
                })

        # Trim alerts to last 50
        if len(world.defender_alerts) > 50:
            world.defender_alerts = world.defender_alerts[-50:]

    def _fraudster_adaptation(
        self, world: HiddenWorldState, rng: random.Random
    ) -> None:
        """
        Model adaptive fraudster behaviour.

        If detection pressure is high on a route, the fraudster may
        spontaneously rotate mules or delay as a built-in adaptation
        (separate from the chosen action).
        """
        for route in world.fraud_routes:
            if not route.is_active:
                continue
            if route.detection_pressure > 0.7 and rng.random() < 0.3:
                # Adaptive: lower pressure by slightly rotating strategy
                route.detection_pressure *= 0.85
                world.fraudster_alert_level = min(
                    1.0, world.fraudster_alert_level + 0.05
                )

    def _deactivate_exhausted_routes(self, world: HiddenWorldState) -> None:
        """Deactivate fraud routes where all mules are frozen."""
        for route in world.fraud_routes:
            if not route.is_active:
                continue
            all_frozen = all(
                world.users.get(mid, UserNode(id="x", is_frozen=True)).is_frozen
                for mid in route.mule_ids
            )
            merchant_blocked = False
            if route.merchant_id:
                m = world.merchants.get(route.merchant_id)
                if m and m.is_blocked:
                    merchant_blocked = True
            if all_frozen or merchant_blocked:
                route.is_active = False

        world.available_routes = [r.id for r in world.fraud_routes if r.is_active]