-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.py
More file actions
392 lines (333 loc) · 14.4 KB
/
env.py
File metadata and controls
392 lines (333 loc) · 14.4 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
"""
env.py — InboxOps OpenEnv Environment.
Implements the standard OpenEnv interface:
env.reset(scenario_id, task_difficulty) → Observation
env.step(action) → (Observation, Reward, done, info)
env.state() → EpisodeState
Three task difficulties gate what the agent must produce per email:
EASY → category only
MEDIUM → category + priority + routing + suggested_action
HARD → all of MEDIUM + draft_body + reply_tone
"""
from __future__ import annotations
import json
import os
import random
from copy import deepcopy
from pathlib import Path
from typing import Any, Optional
from models import (
Action,
ActionType,
CalendarEvent,
Email,
EmailCategory,
EpisodeState,
Observation,
Priority,
Reward,
SLAPolicy,
SuggestedAction,
TaskDifficulty,
)
from graders import (
GradeResult,
apply_penalties,
compute_episode_score,
grade_task1_action,
grade_task2_action,
grade_task3_action,
)
# ─────────────────────────────────────────────
# Data loading
# ─────────────────────────────────────────────
DATA_PATH = Path(__file__).parent / "data" / "inbox_scenarios.json"
def _load_data() -> dict:
with open(DATA_PATH) as f:
return json.load(f)
# Step budget per difficulty
STEP_BUDGETS = {
TaskDifficulty.EASY: 20,
TaskDifficulty.MEDIUM: 35,
TaskDifficulty.HARD: 50,
}
# ─────────────────────────────────────────────
# Environment
# ─────────────────────────────────────────────
class InboxOpsEnv:
"""
InboxOps — OpenEnv-compatible environment for operations inbox management.
Usage
-----
env = InboxOpsEnv()
obs = env.reset(scenario_id="scenario_001", task_difficulty=TaskDifficulty.HARD)
while not obs.done:
action = agent.act(obs)
obs, reward, done, info = env.step(action)
summary = env.episode_summary()
"""
def __init__(self, data_path: Optional[Path] = None):
self._raw = _load_data() if data_path is None else json.loads(Path(data_path).read_text())
self._scenarios: dict[str, dict] = {s["id"]: s for s in self._raw["scenarios"]}
self._ground_truth: dict[str, dict] = self._raw["ground_truth"]
self._state: Optional[EpisodeState] = None
self._emails: dict[str, Email] = {}
self._calendar: list[CalendarEvent] = []
self._sla_policies: dict[str, dict] = {}
self._reward_history: list[Reward] = []
# ── Public API ──────────────────────────────
def reset(
self,
scenario_id: Optional[str] = None,
task_difficulty: TaskDifficulty = TaskDifficulty.MEDIUM,
seed: Optional[int] = None,
) -> Observation:
"""
Reset the environment for a new episode.
Parameters
----------
scenario_id : str | None
Which scenario to load. If None, picks randomly.
task_difficulty : TaskDifficulty
Controls what the agent must produce (EASY / MEDIUM / HARD).
seed : int | None
Random seed for reproducibility.
Returns
-------
Observation
Initial observation containing the full inbox.
"""
if seed is not None:
random.seed(seed)
if scenario_id is None:
scenario_id = random.choice(list(self._scenarios.keys()))
scenario = self._scenarios[scenario_id]
self._load_scenario(scenario)
self._state = EpisodeState(
scenario_id=scenario_id,
task_difficulty=task_difficulty,
max_steps=STEP_BUDGETS[task_difficulty],
)
self._reward_history = []
return self._build_observation("Episode started. Process all pending emails.")
def step(self, action: Action) -> tuple[Observation, Reward, bool, dict]:
"""
Apply an action and advance the environment by one step.
Returns
-------
obs : Observation
reward : Reward
done : bool
info : dict (grading details, debug info)
"""
if self._state is None:
raise RuntimeError("Call reset() before step().")
if self._state.done:
raise RuntimeError("Episode is done. Call reset().")
self._state.step_number += 1
info: dict[str, Any] = {}
# ── Validate action ──────────────────────
validation_error = self._validate_action(action)
if validation_error:
reward = Reward(breakdown_notes=validation_error)
apply_penalties(reward, is_invalid=True)
self._reward_history.append(reward)
self._state.rewards_log.append(reward.to_dict())
obs = self._build_observation(f"INVALID ACTION: {validation_error}")
self._check_termination()
return obs, reward, self._state.done, {"error": validation_error}
# ── Handle control actions ───────────────
if action.action_type == ActionType.COMPLETE:
self._state.done = True
self._state.termination_reason = "agent_completed"
reward = Reward(breakdown_notes="Agent signalled completion.")
reward.compute_total()
obs = self._build_observation("Agent marked episode complete.")
return obs, reward, True, info
if action.action_type == ActionType.SKIP:
reward = Reward(breakdown_notes="Skipped.")
reward.compute_total()
obs = self._build_observation(f"Skipped email {action.email_id}.")
self._check_termination()
return obs, reward, self._state.done, info
# ── Grade the action ─────────────────────
grade_result = self._grade(action)
reward = grade_result.reward
# Check for duplicate
dup_count = self._count_duplicate(action)
if dup_count > 0:
apply_penalties(reward, is_duplicate=True, duplicate_action_count=dup_count)
# ── Update state ─────────────────────────
self._apply_action_to_state(action)
self._state.actions_log.append(action.to_dict())
self._state.rewards_log.append(reward.to_dict())
self._state.cumulative_score += reward.total
self._reward_history.append(reward)
# ── Build observation ────────────────────
result_msg = self._format_result(action, grade_result)
info["grade_details"] = grade_result.details
info["reward_breakdown"] = reward.to_dict()
self._check_termination()
obs = self._build_observation(result_msg)
return obs, reward, self._state.done, info
def state(self) -> EpisodeState:
if self._state is None:
raise RuntimeError("No active episode. Call reset().")
return deepcopy(self._state)
def episode_summary(self) -> dict:
if self._state is None:
return {}
processed = len(self._state.email_classifications)
return compute_episode_score(
rewards=self._reward_history,
total_emails=len(self._emails),
emails_processed=processed,
task_difficulty=self._state.task_difficulty,
)
def list_scenarios(self) -> list[str]:
return list(self._scenarios.keys())
def get_action_schema(self) -> dict:
"""Return a JSON-serialisable description of the action space."""
return {
"action_type": [a.value for a in ActionType],
"email_id": "string (required for email-targeting actions)",
"category": [c.value for c in EmailCategory],
"priority": [p.value for p in Priority],
"escalation_team": "string (e.g. engineering_oncall, legal_and_founder, bd_team, ...)",
"suggested_action": [s.value for s in SuggestedAction],
"draft_body": "string (full reply text, Task 3 only)",
"reply_tone": "string (e.g. professional_warm, urgent_empathetic, ...)",
"calendar_slot": "ISO-8601 datetime string (for scheduling actions)",
"notes": "string (agent reasoning, not graded)",
}
# ── Private helpers ─────────────────────────
def _load_scenario(self, scenario: dict) -> None:
self._emails = {}
for e in scenario["emails"]:
email = Email(
id=e["id"],
from_address=e["from"],
subject=e["subject"],
body=e["body"],
timestamp=e["timestamp"],
thread_id=e["thread_id"],
has_attachment=e.get("has_attachment", False),
labels=e.get("labels", []),
)
self._emails[email.id] = email
self._calendar = [
CalendarEvent(
id=c["id"],
title=c["title"],
start=c["start"],
end=c["end"],
attendees=c["attendees"],
)
for c in scenario.get("calendar_events", [])
]
self._sla_policies = scenario.get("sla_policies", {})
def _validate_action(self, action: Action) -> Optional[str]:
"""Return an error message if the action is invalid, else None."""
if action.action_type in (
ActionType.CLASSIFY,
ActionType.SET_PRIORITY,
ActionType.ROUTE,
ActionType.SET_ACTION,
ActionType.DRAFT_REPLY,
ActionType.ARCHIVE,
ActionType.ESCALATE,
ActionType.SKIP,
):
if not action.email_id:
return "email_id required for this action type."
if action.email_id not in self._emails:
return f"Unknown email_id: {action.email_id}"
if action.action_type == ActionType.CLASSIFY and action.category is None:
return "category required for CLASSIFY action."
if action.action_type == ActionType.SET_PRIORITY and action.priority is None:
return "priority required for SET_PRIORITY action."
if action.action_type == ActionType.DRAFT_REPLY and not action.draft_body:
return "draft_body required for DRAFT_REPLY action."
return None
def _grade(self, action: Action) -> GradeResult:
"""Route to the correct task grader."""
email_id = action.email_id
gt = self._ground_truth.get(email_id, {})
if not gt:
# No ground truth → neutral reward
r = Reward(breakdown_notes=f"No ground truth for {email_id}.")
r.compute_total()
return GradeResult(email_id=email_id or "", reward=r, details={})
difficulty = self._state.task_difficulty
step = self._state.step_number
if difficulty == TaskDifficulty.EASY:
return grade_task1_action(action, gt)
elif difficulty == TaskDifficulty.MEDIUM:
return grade_task2_action(action, gt, step)
else:
return grade_task3_action(action, gt, step)
def _apply_action_to_state(self, action: Action) -> None:
eid = action.email_id
if not eid:
return
if action.category:
self._state.email_classifications[eid] = action.category
if action.priority:
self._state.email_priorities[eid] = action.priority
if action.escalation_team:
self._state.email_routings[eid] = action.escalation_team
if action.suggested_action:
self._state.email_actions[eid] = action.suggested_action
if action.draft_body:
self._state.email_drafts[eid] = action.draft_body
if action.calendar_slot:
self._state.booked_slots.append(action.calendar_slot)
def _count_duplicate(self, action: Action) -> int:
"""Count how many times this exact (email_id, action_type) pair appeared before."""
eid = action.email_id
atype = action.action_type.value
return sum(
1
for prev in self._state.actions_log
if prev.get("email_id") == eid and prev.get("action_type") == atype
)
def _check_termination(self) -> None:
if self._state.done:
return
remaining = self._state.max_steps - self._state.step_number
if remaining <= 0:
self._state.done = True
self._state.termination_reason = "step_budget_exhausted"
return
# Auto-complete if all emails processed
processed = set(self._state.email_classifications.keys())
if processed >= set(self._emails.keys()):
self._state.done = True
self._state.termination_reason = "all_emails_processed"
def _build_observation(self, last_action_result: str) -> Observation:
processed = set(self._state.email_classifications.keys())
pending = [eid for eid in self._emails if eid not in processed]
n_steps = self._state.max_steps - self._state.step_number
running_score = (
self._state.cumulative_score / max(self._state.step_number, 1)
)
return Observation(
step_number=self._state.step_number,
task_difficulty=self._state.task_difficulty,
emails=list(self._emails.values()),
pending_email_ids=pending,
processed_email_ids=list(processed),
calendar_events=self._calendar,
active_sla_policies=self._sla_policies,
last_action_result=last_action_result,
step_budget_remaining=n_steps,
score_so_far=min(1.0, max(0.0, running_score)),
done=self._state.done,
)
def _format_result(self, action: Action, result: GradeResult) -> str:
lines = [
f"✓ Action: {action.action_type.value} on {action.email_id}",
f" Score this step: {result.reward.total:.3f}",
f" {result.reward.breakdown_notes}",
]
return "\n".join(lines)