Spaces:
Sleeping

File size: 10,890 Bytes
36c8ed0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
baseline.py
-----------
Baseline LLM inference script for the ISS Safety Operations Environment.
Uses OpenAI GPT-4o to play all 3 episodes and report final scores.

Usage:
    export OPENAI_API_KEY=your_key_here
    python baseline.py

Or run a specific episode:
    python baseline.py --episode audit_001
"""

import os
import json
import argparse
import re

from dotenv import load_dotenv
load_dotenv()  # loads OPENAI_API_KEY from .env automatically

from openai import OpenAI
from env.environment import ISSEnvironment
from env.objects import Action, Observation

# ---------------------------------------------------------------------------
# Client setup
# ---------------------------------------------------------------------------

client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))

if not os.environ.get("OPENAI_API_KEY"):
    raise EnvironmentError(
        "OPENAI_API_KEY not found.\n"
        "Add it to your .env file:\n"
        "  OPENAI_API_KEY=sk-...\n"
    )

# ---------------------------------------------------------------------------
# System prompt
# ---------------------------------------------------------------------------

SYSTEM_PROMPT = """You are a Mission Control Safety Officer aboard the ISS.
You will receive safety situation reports each turn and must respond with
a single action to resolve the situation.

IMPORTANT β€” You must ALWAYS respond with valid JSON and nothing else:
{
  "action_type": "<one of the allowed actions>",
  "target_object_id": "<object id or null>",
  "target_module": "<module name or null>",
  "reasoning": "<your reasoning, minimum 20 words>"
}

ALLOWED ACTIONS BY EPISODE TYPE:
  audit:         inspect_object, flag_non_compliant, clear_for_mission, submit_report, escalate
  emergency:     deploy_resource, evacuate_module, trigger_switch, contact_ground, submit_report, escalate
  investigation: pull_sensor_log, cross_reference, identify_root_cause, submit_report, escalate

MODULES: Lab, Node1, Node2, Airlock, CrewQuarters, ServiceModule

KEY RULES:
  - In audit: inspect objects, flag non-compliant ones, then submit_report with target_object_id set to your decision ("block_eva" or "clear_eva")
  - In emergency: ALWAYS evacuate crew BEFORE deploying any resource. Never deploy oxygen near fire.
  - In investigation: pull sensor logs before drawing conclusions. The obvious answer may be a red herring.
  - submit_report, identify_root_cause, and escalate are TERMINAL β€” episode ends immediately.
  - You have limited turns. Be decisive.

Respond ONLY with the JSON object. No explanation outside the JSON."""


# ---------------------------------------------------------------------------
# Observation formatter
# ---------------------------------------------------------------------------

def format_observation(obs: Observation) -> str:
    """Convert an Observation into a human-readable prompt string for the LLM."""
    lines = []

    lines.append(f"=== ISS SAFETY OPERATIONS β€” TURN {obs.timestep} ===")
    lines.append(f"Episode Type : {obs.episode_type.upper()}")
    lines.append(f"Mission      : {obs.mission_context}")
    lines.append(f"Turns Left   : {obs.turns_remaining}")
    lines.append(f"Ground Ctrl  : {'AVAILABLE' if obs.ground_control_available else 'UNAVAILABLE'}")
    lines.append("")

    # Crew
    lines.append("CREW LOCATIONS:")
    for crew_id, loc in obs.crew_locations.items():
        lines.append(f"  {crew_id}: {loc}")
    lines.append("")

    # Active alerts
    if obs.active_alerts:
        lines.append("ACTIVE ALERTS:")
        for alert in obs.active_alerts:
            lines.append(f"  [{alert.severity.upper()}] {alert.alert_type} in {alert.module} (alert_id={alert.alert_id})")
        lines.append("")

    # Objects
    lines.append("SAFETY OBJECTS:")
    for obj in obs.objects:
        pressure = f", pressure={obj.pressure_level:.2f}" if obj.pressure_level is not None else ""
        expiry = f", expiry_days={obj.expiry_days_remaining}"
        tag = f", tag_valid={obj.inspection_tag_valid}"
        lines.append(
            f"  [{obj.module}] {obj.object_id} ({obj.object_type})"
            f" β€” status={obj.status}{pressure}{expiry}{tag}"
        )
    lines.append("")

    # Evidence log (investigation only)
    if obs.evidence_log:
        lines.append("EVIDENCE LOG:")
        for entry in obs.evidence_log:
            visibility = "" if entry.visible_to_agent else " [HIDDEN]"
            lines.append(f"  {entry.timestamp} | {entry.object_id} | {entry.event}{visibility}")
        lines.append("")

    # Actions already taken this episode
    if obs.actions_taken:
        lines.append("ACTIONS TAKEN SO FAR:")
        for a in obs.actions_taken:
            lines.append(f"  {a}")
        lines.append("")

    lines.append("What is your next action? Respond with valid JSON only.")
    return "\n".join(lines)


# ---------------------------------------------------------------------------
# Response parser
# ---------------------------------------------------------------------------

def parse_action(response_text: str) -> Action:
    """
    Parse the LLM's text response into an Action object.
    Attempts JSON extraction even if the model adds surrounding text.
    """
    # Try to extract JSON block if present
    json_match = re.search(r'\{.*\}', response_text, re.DOTALL)
    if not json_match:
        raise ValueError(f"No JSON found in LLM response:\n{response_text}")

    data = json.loads(json_match.group())

    return Action(
        action_type=data["action_type"],
        target_object_id=data.get("target_object_id"),
        target_module=data.get("target_module"),
        reasoning=data.get("reasoning", "No reasoning provided."),
    )


# ---------------------------------------------------------------------------
# Episode runner
# ---------------------------------------------------------------------------

def run_episode(env: ISSEnvironment, episode_id: str, verbose: bool = True) -> dict:
    """Run a single episode with the LLM agent. Returns result dict."""
    if verbose:
        print(f"\n{'='*60}")
        print(f"  EPISODE: {episode_id}")
        print(f"{'='*60}")

    obs = env.reset(episode_id)
    done = False
    turn = 0
    history = []   # conversation history for multi-turn context

    # Start conversation with system prompt
    messages = [{"role": "system", "content": SYSTEM_PROMPT}]

    while not done:
        turn += 1
        prompt = format_observation(obs)

        if verbose:
            print(f"\n--- Turn {turn} ---")

        # Add user message (current observation)
        messages.append({"role": "user", "content": prompt})

        # Call LLM
        response = client.chat.completions.create(
            model="gpt-4o",
            messages=messages,
            temperature=0.2,       # low temperature for deterministic safety decisions
            max_tokens=300,
        )

        response_text = response.choices[0].message.content.strip()

        # Add assistant response to history (multi-turn context)
        messages.append({"role": "assistant", "content": response_text})

        if verbose:
            print(f"LLM Response: {response_text}")

        # Parse into Action
        try:
            action = parse_action(response_text)
        except Exception as e:
            if verbose:
                print(f"[PARSE ERROR] {e} β€” escalating as fallback.")
            action = Action(
                action_type="escalate",
                target_object_id=None,
                target_module=None,
                reasoning="Parse error in LLM response, escalating to ground control for guidance.",
            )

        if verbose:
            target = action.target_object_id or action.target_module or "β€”"
            print(f"Action: {action.action_type} β†’ {target}")
            print(f"Reasoning: {action.reasoning[:100]}...")

        # Step the environment
        obs, reward, done, info = env.step(action)

        history.append({
            "turn": turn,
            "action": f"{action.action_type}:{action.target_object_id or action.target_module or 'none'}",
            "reasoning": action.reasoning,
        })

        if done:
            if verbose:
                print(f"\n{'='*60}")
                print(f"  EPISODE COMPLETE β€” Score: {reward.score:.3f}")
                print(f"  Outcome:    {reward.outcome_score:.3f}")
                print(f"  Efficiency: {reward.efficiency_bonus:.3f}")
                print(f"  Danger Pen: {reward.danger_penalty:.3f}")
                print(f"  Escalation: {reward.escalation_penalty:.3f}")
                print(f"  Reasoning:  {reward.reasoning_bonus:.3f}")
                print(f"  Breakdown:  {reward.breakdown}")
                print(f"{'='*60}")

    return {
        "episode_id": episode_id,
        "score": reward.score,
        "outcome_score": reward.outcome_score,
        "efficiency_bonus": reward.efficiency_bonus,
        "danger_penalty": reward.danger_penalty,
        "escalation_penalty": reward.escalation_penalty,
        "reasoning_bonus": reward.reasoning_bonus,
        "turns_used": turn,
        "actions": history,
        "breakdown": reward.breakdown,
    }


# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------

def main():
    parser = argparse.ArgumentParser(description="ISS Safety Ops β€” LLM Baseline Agent")
    parser.add_argument(
        "--episode",
        type=str,
        default=None,
        help="Run a specific episode (e.g. audit_001). Defaults to all 3.",
    )
    parser.add_argument(
        "--quiet",
        action="store_true",
        help="Suppress per-turn output, only show final scores.",
    )
    args = parser.parse_args()

    # API key is already loaded from .env at module import time

    env = ISSEnvironment()
    episodes = (
        [args.episode] if args.episode
        else ["audit_001", "emergency_001", "investigation_001"]
    )

    all_results = []
    for ep in episodes:
        result = run_episode(env, ep, verbose=not args.quiet)
        all_results.append(result)

    # Summary table
    print("\n" + "="*60)
    print("  FINAL SCORES")
    print("="*60)
    print(f"  {'Episode':<25} {'Score':>8}  {'Turns':>6}")
    print(f"  {'-'*25} {'-'*8}  {'-'*6}")
    for r in all_results:
        print(f"  {r['episode_id']:<25} {r['score']:>8.3f}  {r['turns_used']:>6}")

    avg = sum(r["score"] for r in all_results) / len(all_results)
    print(f"  {'AVERAGE':<25} {avg:>8.3f}")
    print("="*60)

    # Save results to JSON
    output_path = "baseline_results.json"
    with open(output_path, "w") as f:
        json.dump(all_results, f, indent=2)
    print(f"\nDetailed results saved to: {output_path}")


if __name__ == "__main__":
    main()