Omkar1806 commited on
Commit
6952b37
Β·
verified Β·
1 Parent(s): 8a137ab

Update env.py

Browse files
Files changed (1) hide show
  1. env.py +408 -58
env.py CHANGED
@@ -1,110 +1,460 @@
 
 
 
 
 
 
 
 
 
1
  import numpy as np
2
  import gymnasium as gym
3
  from gymnasium import spaces
4
 
5
  # ---------------------------------------------------------------------------
6
- # Vocabulary & Encoding
7
  # ---------------------------------------------------------------------------
8
  KEYWORD_VOCAB = [
9
- "invoice", "payment", "overdue", "refund",
10
- "hacked", "breach", "unauthorized", "password",
11
- "crash", "error", "bug", "slow",
12
- "lawsuit", "legal", "attorney", "sue",
13
- "spam", "offer", "win", "free",
14
- "urgent", "critical", "angry", "threat",
15
  ]
16
 
17
  SENTIMENT_MAP = {"positive": 0, "neutral": 1, "negative": 2}
18
  CONTEXT_MAP = {"spam": 0, "billing": 1, "tech": 2, "security": 3, "legal": 4}
19
  OBS_DIM = len(KEYWORD_VOCAB) + len(SENTIMENT_MAP) + len(CONTEXT_MAP)
20
 
21
- # Labels for UI
22
  URGENCY_LABELS = ["General", "Billing", "Security Breach"]
23
  ROUTING_LABELS = ["AI Auto-Reply", "Tech Support", "Legal"]
24
  RESOLUTION_LABELS = ["Archive", "Draft Reply", "Escalate to Human"]
25
 
26
  # ---------------------------------------------------------------------------
27
- # Dataset
28
  # ---------------------------------------------------------------------------
29
  EMAIL_DATASET = [
30
- {"description": "Spam promo", "keywords": ["spam", "offer"], "sentiment": "positive", "context": "spam", "difficulty": "easy", "correct_actions": (0, 0, 0)},
31
- {"description": "Routine support", "keywords": ["slow", "error"], "sentiment": "neutral", "context": "tech", "difficulty": "easy", "correct_actions": (0, 1, 1)},
32
- {"description": "Billing inquiry", "keywords": ["invoice", "payment"], "sentiment": "neutral", "context": "billing", "difficulty": "easy", "correct_actions": (1, 0, 1)},
33
- {"description": "Overdue invoice", "keywords": ["invoice", "overdue"], "sentiment": "negative", "context": "billing", "difficulty": "medium", "correct_actions": (1, 0, 1)},
34
- {"description": "Refund dispute", "keywords": ["refund", "angry"], "sentiment": "negative", "context": "billing", "difficulty": "medium", "correct_actions": (1, 2, 2)},
35
- {"description": "Legal threat", "keywords": ["lawsuit", "attorney"], "sentiment": "negative", "context": "legal", "difficulty": "hard", "correct_actions": (2, 2, 2)},
36
- {"description": "Account hacked", "keywords": ["hacked", "password"], "sentiment": "negative", "context": "security", "difficulty": "hard", "correct_actions": (2, 1, 2)},
37
- {"description": "Data breach", "keywords": ["breach", "unauthorized"], "sentiment": "negative", "context": "security", "difficulty": "hard", "correct_actions": (2, 1, 2)},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  ]
39
 
40
  # ---------------------------------------------------------------------------
41
- # Environment Class
 
 
 
 
 
 
 
 
42
  # ---------------------------------------------------------------------------
43
  class EmailTriageEnv(gym.Env):
44
- def __init__(self, batch: list | None = None, shuffle: bool = True, task: str = "easy"):
 
 
 
 
45
  super().__init__()
46
 
47
- # Meta Grader logic: filter by task if no batch provided
48
- if batch is None:
49
- self.email_batch = [e for e in EMAIL_DATASET if e["difficulty"] == task]
50
- if not self.email_batch: # Fallback agar task match na ho
51
- self.email_batch = EMAIL_DATASET[:3]
52
- else:
53
  self.email_batch = batch
54
-
 
 
 
 
 
55
  self.shuffle = shuffle
 
 
56
  self.action_space = spaces.MultiDiscrete([3, 3, 3])
57
- self.observation_space = spaces.Box(low=0.0, high=1.0, shape=(OBS_DIM,), dtype=np.float32)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
- self._max_episode_reward = len(self.email_batch) * 1.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
- def _encode(self, email):
62
- kw_flags = np.array([1.0 if kw in email["keywords"] else 0.0 for kw in KEYWORD_VOCAB], dtype=np.float32)
63
- sent_vec = np.zeros(len(SENTIMENT_MAP), dtype=np.float32)
64
- sent_vec[SENTIMENT_MAP[email["sentiment"]]] = 1.0
65
- ctx_vec = np.zeros(len(CONTEXT_MAP), dtype=np.float32)
66
- ctx_vec[CONTEXT_MAP[email["context"]]] = 1.0
67
- return np.concatenate([kw_flags, sent_vec, ctx_vec])
 
 
 
 
 
68
 
69
- def reset(self, *, seed=None, options=None):
70
  super().reset(seed=seed)
71
  self._queue = list(self.email_batch)
72
  if self.shuffle:
73
  self.np_random.shuffle(self._queue)
 
74
  self._step_idx = 0
75
  self._current_email = self._queue[self._step_idx]
76
- return self._encode(self._current_email), {"difficulty": self._current_email["difficulty"]}
 
 
 
 
 
77
 
78
- def step(self, action):
 
 
 
79
  scored_email = self._current_email
80
- correct = scored_email["correct_actions"]
81
-
82
- # Reward Logic
83
- raw_reward = 0.0
84
- if tuple(action) == correct:
85
- raw_reward = 1.0
86
- elif action[0] == correct[0]:
87
- raw_reward = 0.2 # Partial
88
-
89
- # Security Penalty
90
- if correct[0] == 2 and action[0] != 2:
91
- raw_reward = -2.0
92
 
 
93
  self._step_idx += 1
94
  terminated = self._step_idx >= len(self._queue)
95
-
96
  if not terminated:
97
  self._current_email = self._queue[self._step_idx]
98
  obs = self._encode(self._current_email)
99
  else:
100
  obs = self._encode(scored_email)
101
 
102
- # Normalize reward
103
- norm_reward = raw_reward / self._max_episode_reward
104
-
105
  info = {
106
- "raw_reward": raw_reward,
107
- "correct_actions": correct,
108
- "difficulty": scored_email["difficulty"]
 
109
  }
110
- return obs, norm_reward, terminated, False, info
 
 
 
 
 
 
 
 
 
 
 
1
+ """EmailTriageEnv β€” Intelligent Email Gatekeeper RL Environment
2
+ ============================================================
3
+ Observation : flat Box vector encoding keywords, sentiment, and context.
4
+ Action : MultiDiscrete([3, 3, 3])
5
+ [0] Urgency β€” 0=General, 1=Billing, 2=Security Breach
6
+ [1] Routing β€” 0=AI Auto-Reply, 1=Tech Support, 2=Legal
7
+ [2] Resolutionβ€” 0=Archive, 1=Draft Reply, 2=Escalate to Human
8
+ Reward : +1.0 fully correct | -2.0 wrong priority on crisis email
9
+ """
10
  import numpy as np
11
  import gymnasium as gym
12
  from gymnasium import spaces
13
 
14
  # ---------------------------------------------------------------------------
15
+ # Vocabulary & encoding helpers
16
  # ---------------------------------------------------------------------------
17
  KEYWORD_VOCAB = [
18
+ "invoice", "payment", "overdue", "refund", # billing
19
+ "hacked", "breach", "unauthorized", "password", # security
20
+ "crash", "error", "bug", "slow", # tech
21
+ "lawsuit", "legal", "attorney", "sue", # legal
22
+ "spam", "offer", "win", "free", # spam
23
+ "urgent", "critical", "angry", "threat", # sentiment signals
24
  ]
25
 
26
  SENTIMENT_MAP = {"positive": 0, "neutral": 1, "negative": 2}
27
  CONTEXT_MAP = {"spam": 0, "billing": 1, "tech": 2, "security": 3, "legal": 4}
28
  OBS_DIM = len(KEYWORD_VOCAB) + len(SENTIMENT_MAP) + len(CONTEXT_MAP)
29
 
30
+ # UI Labels (Required by app.py)
31
  URGENCY_LABELS = ["General", "Billing", "Security Breach"]
32
  ROUTING_LABELS = ["AI Auto-Reply", "Tech Support", "Legal"]
33
  RESOLUTION_LABELS = ["Archive", "Draft Reply", "Escalate to Human"]
34
 
35
  # ---------------------------------------------------------------------------
36
+ # Mock email dataset
37
  # ---------------------------------------------------------------------------
38
  EMAIL_DATASET = [
39
+ # ── Easy: Spam vs Real ──────────────────────────────────────────────────
40
+ {
41
+ "description": "Spam promo",
42
+ "keywords": ["spam", "offer", "win", "free"],
43
+ "sentiment": "positive",
44
+ "context": "spam",
45
+ "difficulty": "easy",
46
+ "correct_actions": (0, 0, 0),
47
+ },
48
+ {
49
+ "description": "Spam lottery",
50
+ "keywords": ["free", "win", "offer"],
51
+ "sentiment": "positive",
52
+ "context": "spam",
53
+ "difficulty": "easy",
54
+ "correct_actions": (0, 0, 0),
55
+ },
56
+ {
57
+ "description": "Routine support request",
58
+ "keywords": ["slow", "error"],
59
+ "sentiment": "neutral",
60
+ "context": "tech",
61
+ "difficulty": "easy",
62
+ "correct_actions": (0, 1, 1),
63
+ },
64
+ {
65
+ "description": "General billing inquiry",
66
+ "keywords": ["invoice", "payment"],
67
+ "sentiment": "neutral",
68
+ "context": "billing",
69
+ "difficulty": "easy",
70
+ "correct_actions": (1, 0, 1),
71
+ },
72
+
73
+ # ── Medium: Billing / Tech context ──────────────────────────────────────
74
+ {
75
+ "description": "Overdue invoice complaint",
76
+ "keywords": ["invoice", "overdue", "payment", "angry"],
77
+ "sentiment": "negative",
78
+ "context": "billing",
79
+ "difficulty": "medium",
80
+ "correct_actions": (1, 0, 1),
81
+ },
82
+ {
83
+ "description": "Refund dispute",
84
+ "keywords": ["refund", "payment", "angry"],
85
+ "sentiment": "negative",
86
+ "context": "billing",
87
+ "difficulty": "medium",
88
+ "correct_actions": (1, 2, 2),
89
+ },
90
+ {
91
+ "description": "App crash report",
92
+ "keywords": ["crash", "bug", "error"],
93
+ "sentiment": "negative",
94
+ "context": "tech",
95
+ "difficulty": "medium",
96
+ "correct_actions": (0, 1, 1),
97
+ },
98
+ {
99
+ "description": "Persistent login bug",
100
+ "keywords": ["bug", "password", "error"],
101
+ "sentiment": "negative",
102
+ "context": "tech",
103
+ "difficulty": "medium",
104
+ "correct_actions": (0, 1, 1),
105
+ },
106
+
107
+ # ── Medium: Legal Threats ────────────────────────────
108
+ {
109
+ "id": "TC-M-01",
110
+ "description": "Polite refund ultimatum with implied legal action",
111
+ "keywords": ["refund", "legal", "angry", "threat"],
112
+ "sentiment": "negative",
113
+ "context": "legal",
114
+ "difficulty": "medium",
115
+ "correct_actions": (2, 2, 2),
116
+ },
117
+ {
118
+ "id": "TC-M-02",
119
+ "description": "Overdue invoice with attorney CC warning",
120
+ "keywords": ["invoice", "overdue", "attorney", "legal", "payment", "threat"],
121
+ "sentiment": "negative",
122
+ "context": "legal",
123
+ "difficulty": "medium",
124
+ "correct_actions": (2, 2, 2),
125
+ },
126
+ {
127
+ "id": "TC-M-03",
128
+ "description": "Disappointed customer implying regulatory complaint",
129
+ "keywords": ["angry", "threat", "legal"],
130
+ "sentiment": "negative",
131
+ "context": "legal",
132
+ "difficulty": "medium",
133
+ "correct_actions": (2, 2, 2),
134
+ },
135
+ {
136
+ "id": "TC-M-04",
137
+ "description": "SLA breach complaint with legal remedy language",
138
+ "keywords": ["breach", "legal", "threat", "angry"],
139
+ "sentiment": "negative",
140
+ "context": "legal",
141
+ "difficulty": "medium",
142
+ "correct_actions": (2, 2, 2),
143
+ },
144
+ {
145
+ "id": "TC-M-05",
146
+ "description": "Billing dispute with small claims court mention",
147
+ "keywords": ["refund", "payment", "angry", "lawsuit", "urgent"],
148
+ "sentiment": "negative",
149
+ "context": "legal",
150
+ "difficulty": "medium",
151
+ "correct_actions": (2, 2, 2),
152
+ },
153
+ {
154
+ "id": "TC-M-06",
155
+ "description": "GDPR deletion request with legal consequence warning",
156
+ "keywords": ["legal", "breach", "threat", "angry"],
157
+ "sentiment": "negative",
158
+ "context": "legal",
159
+ "difficulty": "medium",
160
+ "correct_actions": (2, 2, 2),
161
+ },
162
+ {
163
+ "id": "TC-M-07",
164
+ "description": "Subscription cancellation with chargeback and CFPB threat",
165
+ "keywords": ["payment", "refund", "angry", "threat", "legal"],
166
+ "sentiment": "negative",
167
+ "context": "legal",
168
+ "difficulty": "medium",
169
+ "correct_actions": (2, 2, 2),
170
+ },
171
+ {
172
+ "id": "TC-M-08",
173
+ "description": "Vendor threatening IP infringement claim",
174
+ "keywords": ["unauthorized", "legal", "attorney", "threat"],
175
+ "sentiment": "negative",
176
+ "context": "legal",
177
+ "difficulty": "medium",
178
+ "correct_actions": (2, 2, 2),
179
+ },
180
+
181
+ # ── Hard: Subtle Phishing Attempts ──────────────────────────────────────
182
+ {
183
+ "id": "TC-H-01",
184
+ "description": "IT password reset disguised as routine security audit",
185
+ "keywords": ["password", "unauthorized", "critical", "urgent", "threat"],
186
+ "sentiment": "negative",
187
+ "context": "security",
188
+ "difficulty": "hard",
189
+ "correct_actions": (2, 1, 2),
190
+ },
191
+ {
192
+ "id": "TC-H-02",
193
+ "description": "Fake invoice payment portal redirect β€” credential harvest",
194
+ "keywords": ["invoice", "payment", "password", "unauthorized", "urgent"],
195
+ "sentiment": "neutral",
196
+ "context": "security",
197
+ "difficulty": "hard",
198
+ "correct_actions": (2, 1, 2),
199
+ },
200
+ {
201
+ "id": "TC-H-03",
202
+ "description": "HR benefits enrollment with credential capture",
203
+ "keywords": ["password", "urgent", "critical"],
204
+ "sentiment": "neutral",
205
+ "context": "security",
206
+ "difficulty": "hard",
207
+ "correct_actions": (2, 1, 2),
208
+ },
209
+ {
210
+ "id": "TC-H-04",
211
+ "description": "Fake account suspension notice with login link",
212
+ "keywords": ["unauthorized", "password", "breach", "urgent", "threat"],
213
+ "sentiment": "negative",
214
+ "context": "security",
215
+ "difficulty": "hard",
216
+ "correct_actions": (2, 1, 2),
217
+ },
218
+ {
219
+ "id": "TC-H-05",
220
+ "description": "Vendor onboarding BEC β€” admin credentials via reply",
221
+ "keywords": ["password", "unauthorized", "urgent"],
222
+ "sentiment": "neutral",
223
+ "context": "security",
224
+ "difficulty": "hard",
225
+ "correct_actions": (2, 1, 2),
226
+ },
227
+ {
228
+ "id": "TC-H-06",
229
+ "description": "Fake new sign-in alert β€” was this you? phish",
230
+ "keywords": ["unauthorized", "password", "hacked", "breach", "urgent"],
231
+ "sentiment": "negative",
232
+ "context": "security",
233
+ "difficulty": "hard",
234
+ "correct_actions": (2, 1, 2),
235
+ },
236
+ {
237
+ "id": "TC-H-07",
238
+ "description": "Payroll migration phish β€” salary interruption fear",
239
+ "keywords": ["payment", "password", "urgent", "threat"],
240
+ "sentiment": "negative",
241
+ "context": "security",
242
+ "difficulty": "hard",
243
+ "correct_actions": (2, 1, 2),
244
+ },
245
+ {
246
+ "id": "TC-H-08",
247
+ "description": "Software license renewal β€” admin credential request",
248
+ "keywords": ["password", "critical", "urgent", "error"],
249
+ "sentiment": "neutral",
250
+ "context": "security",
251
+ "difficulty": "hard",
252
+ "correct_actions": (2, 1, 2),
253
+ },
254
+
255
+ # ── Hard: Phishing + Legal Threat Overlay ───────────────────────────────
256
+ {
257
+ "id": "TC-H-09",
258
+ "description": "Fake GDPR breach notice β€” credential harvest via legal fear",
259
+ "keywords": ["breach", "hacked", "password", "legal", "threat", "urgent", "unauthorized"],
260
+ "sentiment": "negative",
261
+ "context": "security",
262
+ "difficulty": "hard",
263
+ "correct_actions": (2, 1, 2),
264
+ },
265
+ {
266
+ "id": "TC-H-10",
267
+ "description": "Ransomware disguised as software compliance audit",
268
+ "keywords": ["hacked", "breach", "unauthorized", "lawsuit", "legal", "threat", "critical", "urgent"],
269
+ "sentiment": "negative",
270
+ "context": "security",
271
+ "difficulty": "hard",
272
+ "correct_actions": (2, 2, 2),
273
+ },
274
+ {
275
+ "id": "TC-H-11",
276
+ "description": "Extortion β€” threatening to publish stolen data",
277
+ "keywords": ["hacked", "breach", "unauthorized", "attorney", "threat", "critical", "urgent"],
278
+ "sentiment": "negative",
279
+ "context": "security",
280
+ "difficulty": "hard",
281
+ "correct_actions": (2, 2, 2),
282
+ },
283
+ {
284
+ "id": "TC-H-12",
285
+ "description": "Fake law firm letter claiming evidence of data misuse",
286
+ "keywords": ["unauthorized", "breach", "attorney", "lawsuit", "legal", "threat"],
287
+ "sentiment": "negative",
288
+ "context": "legal",
289
+ "difficulty": "hard",
290
+ "correct_actions": (2, 2, 2),
291
+ },
292
+
293
+ # ── Hard: Crisis / Security threats ─────────────────────────────────────
294
+ {
295
+ "description": "Account hacked β€” urgent",
296
+ "keywords": ["hacked", "unauthorized", "password", "urgent", "angry"],
297
+ "sentiment": "negative",
298
+ "context": "security",
299
+ "difficulty": "hard",
300
+ "correct_actions": (2, 1, 2),
301
+ },
302
+ {
303
+ "description": "Data breach notification",
304
+ "keywords": ["breach", "unauthorized", "critical", "threat"],
305
+ "sentiment": "negative",
306
+ "context": "security",
307
+ "difficulty": "hard",
308
+ "correct_actions": (2, 1, 2),
309
+ },
310
+ {
311
+ "description": "Legal threat β€” lawsuit",
312
+ "keywords": ["lawsuit", "legal", "attorney", "threat", "angry"],
313
+ "sentiment": "negative",
314
+ "context": "legal",
315
+ "difficulty": "hard",
316
+ "correct_actions": (2, 2, 2),
317
+ },
318
+ {
319
+ "description": "Ransomware / extortion threat",
320
+ "keywords": ["hacked", "threat", "critical", "urgent", "breach"],
321
+ "sentiment": "negative",
322
+ "context": "security",
323
+ "difficulty": "hard",
324
+ "correct_actions": (2, 2, 2),
325
+ },
326
  ]
327
 
328
  # ---------------------------------------------------------------------------
329
+ # Reward weights β€” adjust freely
330
+ # ---------------------------------------------------------------------------
331
+ REWARD_CORRECT_FULL = 1.0
332
+ REWARD_PARTIAL_ONE_WRONG = 0.2
333
+ REWARD_PARTIAL_BOTH_WRONG= 0.1
334
+ PENALTY_MISSED_SECURITY = -2.0
335
+
336
+ # ---------------------------------------------------------------------------
337
+ # Environment
338
  # ---------------------------------------------------------------------------
339
  class EmailTriageEnv(gym.Env):
340
+ """Single-email-per-step triage environment."""
341
+ metadata = {"render_modes": ["human"]}
342
+
343
+ # --- BUG FIX: Added `task` argument and filtering logic ---
344
+ def __init__(self, task: str = "all", batch: list | None = None, shuffle: bool = True):
345
  super().__init__()
346
 
347
+ if batch is not None:
 
 
 
 
 
348
  self.email_batch = batch
349
+ elif task != "all":
350
+ # Filter dataset based on the task difficulty ("easy", "medium", "hard")
351
+ self.email_batch = [e for e in EMAIL_DATASET if e.get("difficulty") == task]
352
+ else:
353
+ self.email_batch = EMAIL_DATASET
354
+
355
  self.shuffle = shuffle
356
+
357
+ # Action space: [urgency(3), routing(3), resolution(3)]
358
  self.action_space = spaces.MultiDiscrete([3, 3, 3])
359
+
360
+ # Observation space: binary keyword flags + one-hot sentiment + one-hot context
361
+ self.observation_space = spaces.Box(
362
+ low=0.0, high=1.0, shape=(OBS_DIM,), dtype=np.float32
363
+ )
364
+
365
+ # Internal state (populated by reset)
366
+ self._queue: list[dict] = []
367
+ self._current_email: dict = {}
368
+ self._step_idx: int = 0
369
+
370
+ def _encode(self, email: dict) -> np.ndarray:
371
+ """Convert an email dict into a flat float32 observation vector."""
372
+ kw_flags = np.array(
373
+ [1.0 if kw in email["keywords"] else 0.0 for kw in KEYWORD_VOCAB],
374
+ dtype=np.float32,
375
+ )
376
+ sentiment_vec = np.zeros(len(SENTIMENT_MAP), dtype=np.float32)
377
+ sentiment_vec[SENTIMENT_MAP[email["sentiment"]]] = 1.0
378
 
379
+ context_vec = np.zeros(len(CONTEXT_MAP), dtype=np.float32)
380
+ context_vec[CONTEXT_MAP[email["context"]]] = 1.0
381
+
382
+ return np.concatenate([kw_flags, sentiment_vec, context_vec])
383
+
384
+ def _compute_reward(self, action: np.ndarray, email: dict) -> float:
385
+ """Strict reward rules based on priority."""
386
+ urgency = int(action[0])
387
+ routing = int(action[1])
388
+ resolution = int(action[2])
389
+ correct = email["correct_actions"]
390
+
391
+ # Priority 1: Security breach miss β€” sabse bada crime
392
+ if correct[0] == 2 and urgency != 2:
393
+ return PENALTY_MISSED_SECURITY
394
+
395
+ # Priority 2: Perfect match
396
+ if (urgency, routing, resolution) == correct:
397
+ return REWARD_CORRECT_FULL
398
 
399
+ # Priority 3: Urgency sahi hai β€” partial credit
400
+ if urgency == correct[0]:
401
+ routing_ok = (routing == correct[1])
402
+ resolution_ok = (resolution == correct[2])
403
+
404
+ if routing_ok and not resolution_ok:
405
+ return REWARD_PARTIAL_ONE_WRONG
406
+ if resolution_ok and not routing_ok:
407
+ return REWARD_PARTIAL_ONE_WRONG
408
+ return REWARD_PARTIAL_BOTH_WRONG
409
+
410
+ return 0.0
411
 
412
+ def reset(self, *, seed: int | None = None, options: dict | None = None):
413
  super().reset(seed=seed)
414
  self._queue = list(self.email_batch)
415
  if self.shuffle:
416
  self.np_random.shuffle(self._queue)
417
+
418
  self._step_idx = 0
419
  self._current_email = self._queue[self._step_idx]
420
+ obs = self._encode(self._current_email)
421
+ info = {
422
+ "description": self._current_email.get("description", ""),
423
+ "difficulty": self._current_email.get("difficulty", "")
424
+ }
425
+ return obs, info
426
 
427
+ # --- BUG FIX: Returning raw float reward instead of normalised ---
428
+ def step(self, action: np.ndarray):
429
+ """Process one email triage decision."""
430
+ # Save current email and compute raw reward
431
  scored_email = self._current_email
432
+ reward = self._compute_reward(action, scored_email)
 
 
 
 
 
 
 
 
 
 
 
433
 
434
+ # Move to next email
435
  self._step_idx += 1
436
  terminated = self._step_idx >= len(self._queue)
437
+
438
  if not terminated:
439
  self._current_email = self._queue[self._step_idx]
440
  obs = self._encode(self._current_email)
441
  else:
442
  obs = self._encode(scored_email)
443
 
 
 
 
444
  info = {
445
+ "raw_reward": reward,
446
+ "correct_actions": scored_email["correct_actions"],
447
+ "difficulty": scored_email.get("difficulty", ""),
448
+ "description": scored_email.get("description", ""),
449
  }
450
+
451
+ # Return standard Gymnasium step signature with raw reward
452
+ return obs, float(reward), terminated, False, info
453
+
454
+ def render(self, mode: str = "human"):
455
+ """Print current email details to stdout."""
456
+ e = self._current_email
457
+ print(f"[Step {self._step_idx}] {e.get('description', '')} "
458
+ f"| difficulty={e.get('difficulty', '')} "
459
+ f"| sentiment={e.get('sentiment', '')} "
460
+ f"| context={e.get('context', '')}")