File size: 4,689 Bytes
9c4d9c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Client library for MissionCtrl environment.

Provides HTTP client functions for interacting with the MissionCtrl environment API.
Contains environment payload structures and example scenarios.
"""

import os
from typing import Any, Dict, Optional

import httpx
from dotenv import load_dotenv

# ---------------------------------------------------------------------------
# Load .env file automatically
# ---------------------------------------------------------------------------
load_dotenv()

# ---------------------------------------------------------------------------
# Environment configuration
# ---------------------------------------------------------------------------
ENV_BASE_URL: str = os.environ.get("ENV_BASE_URL", "http://localhost:7860")

# Known agent types in the environment
KNOWN_AGENTS = (
    "PlannerAgent",
    "ResearchAgent",
    "CoderAgent",
    "TesterAgent",
    "CommAgent",
)

# Available task tiers
TASKS = ["easy", "medium", "hard", "special"]

# Default max steps per episode
MAX_STEPS = 5


# ---------------------------------------------------------------------------
# HTTP Client
# ---------------------------------------------------------------------------
http = httpx.Client(timeout=60.0)


# ---------------------------------------------------------------------------
# Environment API Functions
# ---------------------------------------------------------------------------
def reset_env(task_id: str, seed: Optional[int] = None) -> Dict[str, Any]:
    """Reset the environment for a specific task.
    
    Args:
        task_id: The task tier to run (easy, medium, hard, special)
        seed: Optional random seed for reproducibility
        
    Returns:
        Dictionary containing the initial observation
    """
    payload = {"task_id": task_id}
    if seed is not None:
        payload["seed"] = seed
    
    resp = http.post(f"{ENV_BASE_URL}/reset", json=payload)
    resp.raise_for_status()
    return resp.json()


def step_env(action: str) -> Dict[str, Any]:
    """Execute one action in the environment.
    
    Args:
        action: The action string to execute (e.g., "APPROVE(task_1)")
        
    Returns:
        Dictionary containing the new observation, reward, done flag, and info
    """
    resp = http.post(f"{ENV_BASE_URL}/step", json={"action": action})
    resp.raise_for_status()
    return resp.json()


def get_state() -> Dict[str, Any]:
    """Get the current environment state (read-only).
    
    Returns:
        Dictionary containing the current observation
    """
    resp = http.get(f"{ENV_BASE_URL}/state")
    resp.raise_for_status()
    return resp.json()


def get_history() -> list:
    """Get the action history for the current episode.
    
    Returns:
        List of past actions and their results
    """
    resp = http.get(f"{ENV_BASE_URL}/history")
    resp.raise_for_status()
    return resp.json()


def record_result(tier: str, score: float, steps: int, history: list, 
                 score_breakdown: Optional[Dict] = None, 
                 hallucination_stats: Optional[Dict] = None) -> Dict[str, str]:
    """Push a completed episode result to the dashboard.
    
    Args:
        tier: Task tier (easy, medium, hard, special)
        score: Final score for the episode
        steps: Number of steps taken
        history: Action history for the episode
        score_breakdown: Optional detailed score breakdown
        hallucination_stats: Optional hallucination detection statistics
        
    Returns:
        Confirmation response
    """
    payload = {
        "tier": tier,
        "score": score,
        "steps": steps,
        "history": history,
        "score_breakdown": score_breakdown or {},
        "hallucination_stats": hallucination_stats or {},
    }
    resp = http.post(f"{ENV_BASE_URL}/record", json=payload)
    resp.raise_for_status()
    return resp.json()


# ---------------------------------------------------------------------------
# Example Usage
# ---------------------------------------------------------------------------
def example_basic_usage():
    """Example showing basic environment interaction."""
    # Reset environment for easy task
    result = reset_env("easy")
    obs = result["observation"]
    print(f"Started task with {len(obs['tasks'])} tasks")
    
    # Take a step
    step_result = step_env("NOOP")
    print(f"Reward: {step_result['reward']}, Done: {step_result['done']}")
    
    # Get current state
    state = get_state()
    print(f"Current step: {state['time_step']}")


if __name__ == "__main__":
    # For backward compatibility, delegate to inference script
    from inference import main
    main()