File size: 10,140 Bytes
7043cc6 | 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 | """
drift_injector.py β Manages world state mutations for AdaptiveWorld.
DRIFT_CONFIGS holds one entry per scenario, defining:
- initial: DYNAMIC_CONFIG values at episode start
- mutated: DYNAMIC_CONFIG values after drift injection
- drift_type: the category of drift (for belief scoring)
The DriftInjector is instantiated per episode and called by
AdaptiveWorldEnvironment at drift_trigger_step.
"""
from typing import Dict, Any
DRIFT_CONFIGS: Dict[str, Dict] = {
# ββ Easy: E1 β Field rename (e-commerce order) βββββββββββββββββββββββββββββ
"easy_field_rename": {
"initial": {
"order_field": "qty",
"required_extra": None,
"order_status": "confirmed",
"noisy_errors": False,
},
"mutated": {
"order_field": "quantity",
"required_extra": "customer_id",
"order_status": "confirmed",
"noisy_errors": False,
},
"drift_type": "field_rename",
},
# ββ Easy: E2 β Endpoint version (hotel booking) ββββββββββββββββββββββββββββ
"easy_endpoint_version": {
"initial": {
"rooms_endpoint": "/mock_api/rooms/book",
"max_rooms_per_request": 0,
"bulk_booking_allowed": True,
},
"mutated": {
"rooms_endpoint": "/mock_api/v2/rooms/book",
"max_rooms_per_request": 0,
"bulk_booking_allowed": True,
},
"drift_type": "endpoint_version",
},
# ββ Easy: E3 β Auth scheme (flight search) βββββββββββββββββββββββββββββββββ
"easy_auth_format": {
"initial": {
"auth_scheme": "Bearer",
},
"mutated": {
"auth_scheme": "ApiKey",
},
"drift_type": "policy_change",
},
# ββ Medium: M1 β Double drift (insurance claim) ββββββββββββββββββββββββββββ
"medium_double_drift": {
"initial": {
"claims_endpoint": "/mock_api/claims",
"claims_id_field": "policy_id",
"claims_amount_field": "amount",
"claims_require_date": False,
},
"mutated": {
"claims_endpoint": "/mock_api/claims/v2",
"claims_id_field": "policy_number",
"claims_amount_field": "claim_amount",
"claims_require_date": True,
},
"drift_type": "field_rename",
},
# ββ Medium: M2 β Policy flip (e-commerce discount) βββββββββββββββββββββββββ
"medium_policy_flip": {
"initial": {
"discount_requires_membership": False,
},
"mutated": {
"discount_requires_membership": True,
},
"drift_type": "policy_change",
},
# ββ Medium: M3 β Rate limit rule change (hotel conference) βββββββββββββββββ
"medium_rate_limit": {
"initial": {
"rooms_endpoint": "/mock_api/rooms/book",
"max_rooms_per_request": 0,
"bulk_booking_allowed": True,
},
"mutated": {
"rooms_endpoint": "/mock_api/rooms/book",
"max_rooms_per_request": 1,
"bulk_booking_allowed": False,
},
"drift_type": "policy_change",
},
# ββ Hard: H1 β Silent semantic drift (order status) ββββββββββββββββββββββββ
"hard_status_meaning": {
"initial": {
"order_field": "qty",
"required_extra": None,
"order_status": "confirmed",
"noisy_errors": False,
},
"mutated": {
"order_field": "qty",
"required_extra": None,
"order_status": "approved", # API returns 200 OK but meaning changed
"noisy_errors": False,
},
"drift_type": "silent_semantic",
},
# ββ Hard: H2 β Response structure drift (product search) βββββββββββββββββββ
"hard_response_structure": {
"initial": {
"product_key": "products",
"product_id_field": "id",
"product_price_field": "price",
},
"mutated": {
"product_key": "items",
"product_id_field": "product_id",
"product_price_field": "cost",
},
"drift_type": "silent_semantic",
},
# ββ Hard: H3 β Cascading invalidation (category field) βββββββββββββββββββββ
"hard_cascading": {
"initial": {
"product_category": "electronics",
"discount_requires_membership": False,
},
"mutated": {
"product_category": "tech", # category change
"discount_requires_membership": True, # coupon eligibility invalidated
},
"drift_type": "silent_semantic",
},
# ββ Expert: EX1 β Transient error then real drift βββββββββββββββββββββββββ
# Note: transient 503 is handled via secondary_drift_step logic in environment
"expert_transient_vs_real": {
"initial": {
"order_field": "qty",
"required_extra": None,
"order_status": "confirmed",
"noisy_errors": False,
},
"mutated": {
"order_field": "quantity",
"required_extra": None,
"order_status": "confirmed",
"noisy_errors": False,
},
"drift_type": "field_rename",
"transient_error_step": 2, # step 2 returns 503 (not a real drift)
},
# ββ Expert: EX2 β Cross-service drift βββββββββββββββββββββββββββββββββββββ
"expert_cross_service": {
"initial": {
"auth_scheme": "Bearer",
"rooms_endpoint": "/mock_api/rooms/book",
},
"mutated": {
"auth_scheme": "ApiKey", # flight drift at step 3
"rooms_endpoint": "/mock_api/rooms/book",
},
"secondary_mutated": {
"auth_scheme": "ApiKey",
"rooms_endpoint": "/mock_api/v2/rooms/book", # hotel drift at step 6
},
"drift_type": "field_rename",
"secondary_drift_step": 6,
},
# ββ Expert: EX3 β Deprecation red herring βββββββββββββββββββββββββββββββββ
"expert_red_herring": {
"initial": {
"claims_endpoint": "/mock_api/claims",
"claims_id_field": "policy_id",
"claims_amount_field": "amount",
"claims_require_date": False,
},
"mutated": {
"claims_endpoint": "/mock_api/claims", # warning added but still same endpoint
"claims_id_field": "policy_id",
"claims_amount_field": "amount",
"claims_require_date": False,
"_deprecated_notice": "field X will be removed in v3", # red herring
},
"final_mutated": {
"claims_endpoint": "/mock_api/claims/v2", # real drift happens at step 6
"claims_id_field": "policy_number",
"claims_amount_field": "amount",
"claims_require_date": False,
},
"drift_type": "endpoint_version",
"secondary_drift_step": 6,
},
}
class DriftInjector:
"""
Manages world state for a single episode.
Instantiated per episode by AdaptiveWorldEnvironment.
"""
def __init__(self, scenario_id: str):
if scenario_id not in DRIFT_CONFIGS:
raise ValueError(f"Unknown scenario: {scenario_id}. "
f"Available: {list(DRIFT_CONFIGS.keys())}")
self.scenario_id = scenario_id
self.config = DRIFT_CONFIGS[scenario_id]
self.current_world: dict = dict(self.config["initial"])
self.drifted: bool = False
self.secondary_drifted: bool = False
def inject(self) -> Dict[str, Any]:
"""
Inject primary drift. Returns the mutation dict for the mock API.
Should be passed to /_admin/mutate.
"""
self.current_world = dict(self.config["mutated"])
self.drifted = True
return self.current_world
def inject_secondary(self) -> Dict[str, Any]:
"""
Inject secondary drift (expert scenarios with two drift events).
Returns mutation dict, or empty dict if no secondary drift defined.
"""
key = "secondary_mutated" if "secondary_mutated" in self.config else "final_mutated"
if key not in self.config:
return {}
self.current_world = dict(self.config[key])
self.secondary_drifted = True
return self.current_world
def get_initial(self) -> Dict[str, Any]:
return dict(self.config["initial"])
def get_world(self) -> Dict[str, Any]:
return self.current_world
def get_drift_type(self) -> str:
return self.config["drift_type"]
def get_world_truth(self) -> Dict[str, Any]:
"""
Ground truth for belief scoring.
Uses secondary/final mutated world for expert scenarios.
"""
for key in ("final_mutated", "secondary_mutated", "mutated"):
if key in self.config:
return dict(self.config[key])
return dict(self.config["mutated"])
def get_transient_error_step(self) -> int:
"""Returns the step at which a transient 503 should be simulated (EX1)."""
return self.config.get("transient_error_step", -1)
def get_secondary_drift_step(self) -> int:
"""Returns the step for secondary drift injection (EX2, EX3)."""
return self.config.get("secondary_drift_step", -1)
|