Spaces:
Sleeping

File size: 13,172 Bytes
9c920f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb64148
 
 
 
9b42062
fb64148
 
47b55f2
 
 
fb64148
 
 
 
9c920f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
env/environment.py
ISSEnvironment class β€” the main OpenEnv interface.
"""
from __future__ import annotations

import json
from pathlib import Path

from env.grader import grade
from env.objects import (
    Action,
    Alert,
    EnvironmentState,
    LogEntry,
    Observation,
    Reward,
    SafetyObject,
)

_EPISODES_DIR = Path(__file__).parent / "episodes"


class ISSEnvironment:
    """OpenEnv-compliant environment for ISS Safety Operations."""

    def __init__(self) -> None:
        self._state: EnvironmentState | None = None
        self._max_turns: int = 6
        self._reasoning_accumulator: list[str] = []
        self._pending_danger_flags: list[str] = []

    # ------------------------------------------------------------------
    # Public API
    # ------------------------------------------------------------------

    def reset(self, episode_id: str) -> Observation:
        """Load episode JSON and return the initial Observation."""
        episode_path = _EPISODES_DIR / f"{episode_id}.json"
        with episode_path.open() as f:
            data = json.load(f)

        # Build SafetyObject list
        objects: list[SafetyObject] = [
            SafetyObject(**obj) for obj in data["objects"]
        ]

        # Build object registry keyed by object_id
        object_registry: dict[str, SafetyObject] = {
            obj.object_id: obj for obj in objects
        }

        # Build active alerts
        active_alerts: list[Alert] = [
            Alert(**a) for a in data.get("active_alerts", [])
        ]

        # Build visible evidence log (only visible entries for investigation)
        evidence_log_raw = data.get("evidence_log", [])
        evidence_log: list[LogEntry] = [
            LogEntry(**e) for e in evidence_log_raw if e.get("visible_to_agent", True)
        ]

        # Build hidden logs
        hidden_logs_raw = data.get("hidden_logs", [])
        hidden_logs: list[LogEntry] = [LogEntry(**e) for e in hidden_logs_raw]

        # Fire-spread schedule β€” keys are string timesteps
        fire_spread_schedule: dict | None = data.get("fire_spread_schedule")

        max_turns: int = data.get("max_turns", 6)

        obs = Observation(
            episode_id=episode_id,
            episode_type=data["episode_type"],
            mission_context=data["mission_context"],
            timestep=0,
            objects=objects,
            active_alerts=active_alerts,
            crew_locations=dict(data["crew_locations"]),
            evidence_log=evidence_log,
            actions_taken=[],
            turns_remaining=max_turns,
            # Emergency episodes start with comms unavailable;
            # audit and investigation have ground control reachable from the start.
            ground_control_available=(data["episode_type"] != "emergency"),
        )

        self._state = EnvironmentState(
            episode_id=episode_id,
            episode_type=data["episode_type"],
            current_observation=obs,
            object_registry=object_registry,
            fire_spread_schedule=fire_spread_schedule,
            hidden_logs=hidden_logs,
            ground_truth=data["ground_truth"],
            done=False,
            total_reward=0.0,
        )
        # Store max_turns for grader
        self._max_turns: int = max_turns
        self._reasoning_accumulator: list[str] = []
        # Pending danger flags raised inside _apply_action;
        # flushed into actions_taken AFTER the action log in step().
        self._pending_danger_flags: list[str] = []

        return obs

    def step(self, action: Action) -> tuple[Observation, Reward, bool, dict]:
        """Apply an action and return (obs, reward, done, info)."""
        if self._state is None:
            raise RuntimeError("Call reset() before step().")
        if self._state.done:
            raise RuntimeError("Episode is already done. Call reset() to start a new episode.")

        # Validate action is permitted for this episode type
        self._validate_action(action)

        # Apply action effects to state
        self._apply_action(action)

        obs = self._state.current_observation

        # Increment timestep and decrement turns_remaining
        obs.timestep += 1
        obs.turns_remaining -= 1

        # Log the formatted action string FIRST, then append any danger flags.
        # This guarantees order: ["deploy_resource:OT_lab_01", "DANGEROUS:oxygen_near_fire"]
        action_log = self.format_action_log(action)
        obs.actions_taken.append(action_log)
        for flag in self._pending_danger_flags:
            obs.actions_taken.append(flag)
        self._pending_danger_flags.clear()

        # Accumulate reasoning for grader
        self._reasoning_accumulator.append(action.reasoning)

        # Fire spread for emergency episodes
        if self._state.episode_type == "emergency":
            self._spread_fire(obs.timestep)

        # Check terminal condition
        done = self._check_done(action)
        self._state.done = done

        # Compute reward only at terminal step
        if done:
            combined_reasoning = " | ".join(self._reasoning_accumulator)
            reward = grade(
                episode_type=self._state.episode_type,
                actions_taken=obs.actions_taken,
                ground_truth=self._state.ground_truth,
                turns_remaining=obs.turns_remaining,
                max_turns=self._max_turns,
                reasoning=combined_reasoning,
            )
            self._state.total_reward = reward.score
        else:
            reward = Reward(
                score=0.0,
                outcome_score=0.0,
                efficiency_bonus=0.0,
                danger_penalty=0.0,
                escalation_penalty=0.0,
                reasoning_bonus=0.0,
                breakdown={},
            )

        info: dict = {
            "timestep": obs.timestep,
            "turns_remaining": obs.turns_remaining,
            "actions_taken": list(obs.actions_taken),
        }

        return obs, reward, done, info

    def state(self) -> dict:
        """Return the full internal state as a dict."""
        if self._state is None:
            raise RuntimeError("Call reset() before state().")
        return self._state.model_dump()

    # ------------------------------------------------------------------
    # Async wrappers (required by openenv-core's create_app)
    # ------------------------------------------------------------------

    async def reset_async(self, episode_id: str = "audit_001", **kwargs) -> Observation:
        return self.reset(episode_id)

    async def step_async(self, action: Action, **kwargs) -> Observation:
        obs, reward, done, info = self.step(action)
        return obs

    async def state_async(self) -> dict:
        return self.state()

    # ------------------------------------------------------------------
    # Helpers
    # ------------------------------------------------------------------

    @staticmethod
    def format_action_log(action: Action) -> str:
        """Always format as 'action_type:target'. Used everywhere."""
        target = action.target_object_id or action.target_module or "none"
        return f"{action.action_type}:{target}"

    def _validate_action(self, action: Action) -> None:
        """Raise ValueError if action is invalid for the current episode type."""
        episode_type = self._state.episode_type  # type: ignore[union-attr]

        audit_only = {"inspect_object", "flag_non_compliant", "clear_for_mission"}
        emergency_only = {"deploy_resource", "evacuate_module", "trigger_switch", "contact_ground"}
        investigation_only = {"pull_sensor_log", "cross_reference", "identify_root_cause"}
        universal = {"escalate", "submit_report"}

        at = action.action_type

        if at in universal:
            return  # always allowed

        if episode_type == "audit" and at not in audit_only:
            raise ValueError(
                f"Action '{at}' is not permitted in 'audit' episodes. "
                f"Allowed: {audit_only | universal}"
            )
        if episode_type == "emergency" and at not in emergency_only:
            raise ValueError(
                f"Action '{at}' is not permitted in 'emergency' episodes. "
                f"Allowed: {emergency_only | universal}"
            )
        if episode_type == "investigation" and at not in investigation_only:
            raise ValueError(
                f"Action '{at}' is not permitted in 'investigation' episodes. "
                f"Allowed: {investigation_only | universal}"
            )

    def _apply_action(self, action: Action) -> None:  # noqa: C901
        """Mutate observation / registry based on action_type."""
        obs = self._state.current_observation  # type: ignore[union-attr]
        registry = self._state.object_registry  # type: ignore[union-attr]
        at = action.action_type

        if at == "inspect_object":
            # No state change β€” logged only (logging happens in step)
            pass

        elif at == "flag_non_compliant":
            # Log only via action_log β€” nothing to mutate here
            pass

        elif at == "clear_for_mission":
            # Log only via action_log β€” nothing to mutate here
            pass

        elif at == "deploy_resource":
            obj_id = action.target_object_id
            if obj_id and obj_id in registry:
                obj = registry[obj_id]
                obj.status = "depleted"  # type: ignore[assignment]

                # Dangerous combination check
                if obj.object_type == "OxygenTank":
                    fire_in_module = any(
                        a.alert_type == "fire" and a.module == obj.module
                        for a in obs.active_alerts
                    )
                    if fire_in_module:
                        # Stage the flag; it will be flushed AFTER the action
                        # log is appended in step(), preserving correct order.
                        self._pending_danger_flags.append("DANGEROUS:oxygen_near_fire")

                # Keep observation objects in sync
                obs.objects = list(registry.values())

        elif at == "evacuate_module":
            target_module = action.target_module
            if target_module:
                for crew_id, location in obs.crew_locations.items():
                    if location == target_module:
                        obs.crew_locations[crew_id] = "Node1"

        elif at == "trigger_switch":
            obj_id = action.target_object_id
            if obj_id and obj_id in registry:
                panel = registry[obj_id]
                panel.status = "depleted"  # type: ignore[assignment]
                # Remove fire alerts in the same module
                module = panel.module
                obs.active_alerts = [
                    a for a in obs.active_alerts
                    if not (a.alert_type == "fire" and a.module == module)
                ]
                obs.objects = list(registry.values())

        elif at == "contact_ground":
            obs.ground_control_available = True

        elif at == "pull_sensor_log":
            obj_id = action.target_object_id
            hidden = self._state.hidden_logs or []  # type: ignore[union-attr]
            for entry in hidden:
                if entry.object_id == obj_id and not entry.visible_to_agent:
                    entry.visible_to_agent = True
                    obs.evidence_log.append(entry)

        elif at == "cross_reference":
            # Logged via format_action_log as "cross_reference:obj1+obj2"
            # No additional state change needed
            pass

        elif at == "identify_root_cause":
            # Terminal β€” logged only
            pass

        elif at == "submit_report":
            # Terminal β€” logged only
            pass

        elif at == "escalate":
            # Terminal β€” logged only
            pass

    def _spread_fire(self, timestep: int) -> None:
        """Check fire spread schedule and add new fire alerts if needed."""
        schedule = self._state.fire_spread_schedule  # type: ignore[union-attr]
        if not schedule:
            return
        module = schedule.get(str(timestep))
        if module:
            obs = self._state.current_observation  # type: ignore[union-attr]
            new_alert = Alert(
                alert_id=f"fire_{module.lower()}_{timestep:02d}",
                alert_type="fire",
                module=module,
                severity="high",
                triggered_at_timestep=timestep,
            )
            obs.active_alerts.append(new_alert)

    def _check_done(self, action: Action) -> bool:
        """Return True if this action terminates the episode."""
        terminal_actions = {"submit_report", "escalate", "identify_root_cause"}
        if action.action_type in terminal_actions:
            return True
        obs = self._state.current_observation  # type: ignore[union-attr]
        if obs.turns_remaining <= 0:
            return True
        return False