File size: 8,922 Bytes
a74cbe6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import re
from enum import Enum
from typing import Any, Dict, List, Optional

from openenv.core.env_server.types import Action, Observation, State
from pydantic import Field, model_validator



class ActionType(str, Enum):
    EXECUTE = "execute"
    DEFER = "defer"
    DELEGATE = "delegate"
    OPTIMIZE = "optimize"


class TaskStatus(str, Enum):
    PENDING = "pending"
    IN_PROGRESS = "in_progress"
    COMPLETED = "completed"
    DEFERRED = "deferred"
    FAILED = "failed"


class DifficultyLevel(str, Enum):
    EASY = "easy"
    MEDIUM = "medium"
    HARD = "hard"


def _coerce_action_type_from_text(text: str) -> ActionType:
    """
    Convert loose text from the web UI into the closest valid action type.

    The default web interface validates form fields against the Action model
    before our environment can run `message_to_action()`, so we accept
    friendly free-form input here and normalize it.
    """
    normalized = (text or "").strip().lower()
    if not normalized:
        return ActionType.EXECUTE

    keyword_map = (
        (ActionType.OPTIMIZE, ("optimize", "optimise", "tune", "analyze", "analyse")),
        (ActionType.DELEGATE, ("delegate", "assign", "handoff", "hand off", "offload")),
        (ActionType.DEFER, ("defer", "later", "wait", "skip", "postpone")),
        (ActionType.EXECUTE, ("execute", "run", "do", "complete", "process", "start")),
    )
    for action_type, keywords in keyword_map:
        if any(keyword in normalized for keyword in keywords):
            return action_type

    return ActionType.EXECUTE


def _extract_task_id_from_text(text: str) -> Optional[int]:
    if not text:
        return None

    explicit_match = re.search(r"(?:task|id|#)\s*(\d+)", text, flags=re.IGNORECASE)
    if explicit_match:
        return int(explicit_match.group(1))

    loose_match = re.search(r"\b(\d+)\b", text)
    if loose_match:
        return int(loose_match.group(1))

    return None


class TaskInfo(object):
    """Lightweight task descriptor (not a BaseModel to avoid nesting issues)."""

    def __init__(
        self,
        task_id: int,
        name: str,
        priority: float,
        deadline: int,
        uncertainty: float,
        value: float,
        required_energy: float,
        required_budget: float,
        category: str,
        status: str = "pending",
    ):
        self.task_id = task_id
        self.name = name
        self.priority = priority
        self.deadline = deadline
        self.uncertainty = uncertainty
        self.value = value
        self.required_energy = required_energy
        self.required_budget = required_budget
        self.category = category
        self.status = status

    def to_dict(self) -> Dict[str, Any]:
        return {
            "task_id": self.task_id,
            "name": self.name,
            "priority": round(self.priority, 3),
            "deadline": self.deadline,
            "uncertainty": round(self.uncertainty, 3),
            "value": round(self.value, 3),
            "required_energy": round(self.required_energy, 3),
            "required_budget": round(self.required_budget, 3),
            "category": self.category,
            "status": self.status,
        }


class AetherTaskFlowAction(Action):
    """
    Action for the AETHER-TaskFlow environment.

    The agent selects a task by ID and decides how to act on it.
    """

    @model_validator(mode="before")
    @classmethod
    def normalize_web_input(cls, data: Any) -> Any:
        """
        Make the default web form resilient to casual text input.

        Examples that should validate cleanly:
          {"action_type": "hi"}
          {"action_type": "execute task 3"}
          {"message": "delegate 2"}
        """
        if isinstance(data, str):
            data = {"action_type": data}

        if not isinstance(data, dict):
            return data

        payload = dict(data)
        raw_message = payload.get("message")
        raw_action_type = payload.get("action_type")

        # When the UI sends a free-form message, treat it as action text.
        if isinstance(raw_message, str) and not raw_action_type:
            raw_action_type = raw_message
        payload.pop("message", None)

        if isinstance(raw_action_type, str):
            parsed_task_id = _extract_task_id_from_text(raw_action_type)
            payload["action_type"] = _coerce_action_type_from_text(raw_action_type).value

            if payload.get("task_id") in (None, "") and parsed_task_id is not None:
                payload["task_id"] = parsed_task_id

            if payload.get("reasoning") in (None, "") and raw_action_type.strip():
                payload["reasoning"] = f"parsed from '{raw_action_type.strip()[:80]}'"

        if payload.get("task_id") in (None, ""):
            payload["task_id"] = 0

        return payload

    action_type: ActionType = Field(
        ...,
        description=(
            "How to act on the selected task. "
            "'execute': consume resources and complete the task; "
            "'defer': postpone to a later step (low penalty); "
            "'delegate': offload at reduced reward but no resource cost; "
            "'optimize': reduce task uncertainty before execution."
        ),
    )
    task_id: int = Field(
        ...,
        ge=0,
        description="ID of the task to act on (from the current task list).",
    )
    reasoning: Optional[str] = Field(
        default=None,
        max_length=500,
        description="Optional agent reasoning for this action (logged but not scored).",
    )


class AetherTaskFlowObservation(Observation):
    """
    Observation returned after each step in the AETHER-TaskFlow environment.

    Contains all information the agent needs to make the next decision.
    """

    # Task queue
    tasks: List[Dict[str, Any]] = Field(
        default_factory=list,
        description="Current list of pending/deferred tasks as dicts.",
    )

    # Resource pool
    time_remaining: int = Field(
        default=10,
        ge=0,
        description="Time steps remaining in this episode.",
    )
    energy_remaining: float = Field(
        default=10.0,
        ge=0.0,
        description="Energy units remaining.",
    )
    budget_remaining: float = Field(
        default=50.0,
        ge=0.0,
        description="Budget units remaining.",
    )

    # System health
    system_health: float = Field(
        default=1.0,
        ge=0.0,
        le=1.0,
        description="Overall system health [0,1]. Drops on overload or missed deadlines.",
    )

    # Episode progress
    step_number: int = Field(
        default=0,
        ge=0,
        description="Current step number in this episode.",
    )
    tasks_completed: int = Field(
        default=0,
        ge=0,
        description="Total tasks completed so far.",
    )
    tasks_failed: int = Field(
        default=0,
        ge=0,
        description="Total tasks that missed their deadline.",
    )
    cumulative_value: float = Field(
        default=0.0,
        description="Total value accumulated so far.",
    )

    # Last action feedback
    last_action_type: Optional[str] = Field(
        default=None,
        description="Action type taken in the previous step.",
    )
    last_action_task_id: Optional[int] = Field(
        default=None,
        description="Task ID acted on in the previous step.",
    )
    last_action_outcome: Optional[str] = Field(
        default=None,
        description="Human-readable outcome of the last action.",
    )

    # Episode info
    difficulty: str = Field(
        default="easy",
        description="Current task difficulty level.",
    )
    episode_id: Optional[str] = Field(
        default=None,
        description="Unique identifier for this episode.",
    )



class AetherTaskFlowState(State):
    """
    Internal state of the AETHER-TaskFlow environment.

    This is the ground truth state used by the grader.
    """

    difficulty: str = Field(default="easy")
    tasks: List[Dict[str, Any]] = Field(default_factory=list)
    completed_tasks: List[Dict[str, Any]] = Field(default_factory=list)
    failed_tasks: List[Dict[str, Any]] = Field(default_factory=list)
    deferred_tasks: List[Dict[str, Any]] = Field(default_factory=list)

    resources: Dict[str, float] = Field(
        default_factory=lambda: {"time": 10.0, "energy": 10.0, "budget": 50.0}
    )
    initial_resources: Dict[str, float] = Field(
        default_factory=lambda: {"time": 10.0, "energy": 10.0, "budget": 50.0}
    )

    system_health: float = Field(default=1.0)
    cumulative_value: float = Field(default=0.0)
    cumulative_reward: float = Field(default=0.0)

    tasks_completed: int = Field(default=0)
    tasks_failed: int = Field(default=0)

    episode_done: bool = Field(default=False)
    seed: Optional[int] = Field(default=None)