Spaces:
Sleeping
Fix bugs, add tests, and improve code quality
Browse files- Fix START_TASK/SWITCH_TASK semantic distinction (was identical code)
- Fix progress reward lost on task completion step (worked_on_task_id)
- Fix grader weights summing to 0.95 (now 1.0)
- Fix grader efficiency giving idle agents perfect score
- Fix grader deadline scoring (now importance-weighted)
- Fix reward clamp [-1,1] truncating completion signal (now [-2,2])
- Add auto-clear current_task_id on task completion
- Add early termination when all tasks complete
- Add custom task mode (task="custom")
- Add 27 tests covering reset, step, grader, and edge cases
- Add Dockerfile to project root for validation script
- Add BSD copyright headers to all files
- Remove dead code, unused imports, and unused dependency
- Update README with accurate baseline scores and documentation
- Dockerfile +47 -0
- README.md +21 -16
- __init__.py +6 -0
- client.py +6 -0
- inference.py +6 -0
- models.py +6 -0
- pyproject.toml +0 -1
- server/__init__.py +6 -0
- server/app.py +7 -1
- server/rhythm_environment.py +78 -34
- tests/__init__.py +5 -0
- tests/test_rhythm_env.py +306 -0
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ARG BASE_IMAGE=ghcr.io/meta-pytorch/openenv-base:latest
|
| 2 |
+
FROM ${BASE_IMAGE} AS builder
|
| 3 |
+
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
COPY . /app/env
|
| 7 |
+
|
| 8 |
+
WORKDIR /app/env
|
| 9 |
+
|
| 10 |
+
RUN if ! command -v uv >/dev/null 2>&1; then \
|
| 11 |
+
curl -LsSf https://astral.sh/uv/install.sh | sh && \
|
| 12 |
+
mv /root/.local/bin/uv /usr/local/bin/uv && \
|
| 13 |
+
mv /root/.local/bin/uvx /usr/local/bin/uvx; \
|
| 14 |
+
fi
|
| 15 |
+
|
| 16 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 17 |
+
git \
|
| 18 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 19 |
+
|
| 20 |
+
RUN --mount=type=cache,target=/root/.cache/uv \
|
| 21 |
+
if [ -f uv.lock ]; then \
|
| 22 |
+
uv sync --frozen --no-install-project --no-editable; \
|
| 23 |
+
else \
|
| 24 |
+
uv sync --no-install-project --no-editable; \
|
| 25 |
+
fi
|
| 26 |
+
|
| 27 |
+
RUN --mount=type=cache,target=/root/.cache/uv \
|
| 28 |
+
if [ -f uv.lock ]; then \
|
| 29 |
+
uv sync --frozen --no-editable; \
|
| 30 |
+
else \
|
| 31 |
+
uv sync --no-editable; \
|
| 32 |
+
fi
|
| 33 |
+
|
| 34 |
+
FROM ${BASE_IMAGE}
|
| 35 |
+
|
| 36 |
+
WORKDIR /app
|
| 37 |
+
|
| 38 |
+
COPY --from=builder /app/env/.venv /app/.venv
|
| 39 |
+
COPY --from=builder /app/env /app/env
|
| 40 |
+
|
| 41 |
+
ENV PATH="/app/.venv/bin:$PATH"
|
| 42 |
+
ENV PYTHONPATH="/app/env:$PYTHONPATH"
|
| 43 |
+
|
| 44 |
+
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
| 45 |
+
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" || exit 1
|
| 46 |
+
|
| 47 |
+
CMD ["sh", "-c", "cd /app/env && uvicorn server.app:app --host 0.0.0.0 --port 8000"]
|
|
@@ -47,11 +47,13 @@ asyncio.run(main())
|
|
| 47 |
|
| 48 |
| Action | Parameters | Description |
|
| 49 |
|--------|-----------|-------------|
|
| 50 |
-
| `START_TASK` | `task_id: int` | Begin working on a
|
| 51 |
| `CONTINUE_TASK` | — | Continue working on current task |
|
| 52 |
-
| `SWITCH_TASK` | `task_id: int` | Switch to a different task (energy + reward penalty) |
|
| 53 |
| `TAKE_BREAK` | — | Rest to recover energy and reduce stress |
|
| 54 |
|
|
|
|
|
|
|
| 55 |
## Observation Space
|
| 56 |
|
| 57 |
| Field | Type | Description |
|
|
@@ -149,6 +151,8 @@ Custom tasks accept 1-10 tasks with configurable effort (0.05-1.0), deadlines (s
|
|
| 149 |
- **Meetings block work**: During meeting steps, the agent's action is ignored
|
| 150 |
- **Tasks have deadlines**: Missing them increases stress and incurs reward penalties
|
| 151 |
- **Energy depletes with work, recovers with breaks**: The agent must pace itself
|
|
|
|
|
|
|
| 152 |
|
| 153 |
## Environment Dynamics
|
| 154 |
|
|
@@ -168,17 +172,17 @@ Custom tasks accept 1-10 tasks with configurable effort (0.05-1.0), deadlines (s
|
|
| 168 |
| Taking a break | −0.08 |
|
| 169 |
| Completing a task | −0.10 |
|
| 170 |
|
| 171 |
-
**Task Progress**: `progress_delta = 0.15 × current_energy` per step. Lower energy = slower work.
|
| 172 |
|
| 173 |
## Reward Design
|
| 174 |
|
| 175 |
-
Multi-component reward per step (clamped to [-
|
| 176 |
|
| 177 |
| Component | Formula | Signal |
|
| 178 |
|-----------|---------|--------|
|
| 179 |
| Progress | `+delta × importance × 2.0` | Encourages productive work on important tasks |
|
| 180 |
| Completion bonus | `+importance × 1.5` | Rewards finishing tasks |
|
| 181 |
-
| Stress penalty | `−stress × 0.
|
| 182 |
| Deadline miss | `−0.3 per miss` | Penalizes missing deadlines |
|
| 183 |
| Switch penalty | `−0.1` | Discourages excessive context-switching |
|
| 184 |
| Idle penalty | `−0.05` | Penalizes wasted time |
|
|
@@ -190,30 +194,31 @@ Multi-component reward per step (clamped to [-1, 1]):
|
|
| 190 |
End-of-episode score in [0.0, 1.0]:
|
| 191 |
|
| 192 |
```
|
| 193 |
-
score = 0.
|
| 194 |
```
|
| 195 |
|
| 196 |
| Component | Calculation |
|
| 197 |
|-----------|-------------|
|
| 198 |
| Completion | Importance-weighted fraction of tasks completed |
|
| 199 |
-
| Deadline |
|
| 200 |
-
| Efficiency |
|
| 201 |
| Energy mgmt | Average energy maintained over the episode |
|
| 202 |
| Stress mgmt | 1 − average stress over the episode |
|
|
|
|
| 203 |
|
| 204 |
## Baseline Scores
|
| 205 |
|
| 206 |
Measured with the included `inference.py` heuristic (no LLM):
|
| 207 |
|
| 208 |
-
| Scenario | Baseline Heuristic | Random Agent (avg of 10) |
|
| 209 |
-
|----------|-------------------|--------------------------|
|
| 210 |
-
| Easy | **0.
|
| 211 |
-
| Medium | **0.
|
| 212 |
-
| Hard | **0.
|
| 213 |
|
| 214 |
-
-
|
| 215 |
-
-
|
| 216 |
-
- Strong LLM agents should score 0.65+ by learning energy management and deadline
|
| 217 |
|
| 218 |
## Setup Instructions
|
| 219 |
|
|
|
|
| 47 |
|
| 48 |
| Action | Parameters | Description |
|
| 49 |
|--------|-----------|-------------|
|
| 50 |
+
| `START_TASK` | `task_id: int` | Begin working on a task (only when idle — no current task) |
|
| 51 |
| `CONTINUE_TASK` | — | Continue working on current task |
|
| 52 |
+
| `SWITCH_TASK` | `task_id: int` | Switch to a different task (requires active task; energy + reward penalty) |
|
| 53 |
| `TAKE_BREAK` | — | Rest to recover energy and reduce stress |
|
| 54 |
|
| 55 |
+
**Note**: `START_TASK` and `SWITCH_TASK` are semantically distinct. `START_TASK` is only valid when the agent has no active task (e.g., after a break or at episode start). `SWITCH_TASK` is only valid when already working on a different task. Using the wrong one results in an idle penalty.
|
| 56 |
+
|
| 57 |
## Observation Space
|
| 58 |
|
| 59 |
| Field | Type | Description |
|
|
|
|
| 151 |
- **Meetings block work**: During meeting steps, the agent's action is ignored
|
| 152 |
- **Tasks have deadlines**: Missing them increases stress and incurs reward penalties
|
| 153 |
- **Energy depletes with work, recovers with breaks**: The agent must pace itself
|
| 154 |
+
- **Task completion auto-clears**: When a task is finished, `current_task_id` resets to `null` — the agent can immediately `START_TASK` a new one without needing a break or switch
|
| 155 |
+
- **Early termination**: The episode ends early if all tasks are completed, rewarding efficient agents
|
| 156 |
|
| 157 |
## Environment Dynamics
|
| 158 |
|
|
|
|
| 172 |
| Taking a break | −0.08 |
|
| 173 |
| Completing a task | −0.10 |
|
| 174 |
|
| 175 |
+
**Task Progress**: `progress_delta = 0.15 × current_energy × (1 - stress × 0.3)` per step. Lower energy = slower work. High stress also impairs productivity — at stress=1.0, output drops to 70% of normal.
|
| 176 |
|
| 177 |
## Reward Design
|
| 178 |
|
| 179 |
+
Multi-component reward per step (clamped to [-2, 2]):
|
| 180 |
|
| 181 |
| Component | Formula | Signal |
|
| 182 |
|-----------|---------|--------|
|
| 183 |
| Progress | `+delta × importance × 2.0` | Encourages productive work on important tasks |
|
| 184 |
| Completion bonus | `+importance × 1.5` | Rewards finishing tasks |
|
| 185 |
+
| Stress penalty | `−stress × 0.15` | Penalizes sustained high stress |
|
| 186 |
| Deadline miss | `−0.3 per miss` | Penalizes missing deadlines |
|
| 187 |
| Switch penalty | `−0.1` | Discourages excessive context-switching |
|
| 188 |
| Idle penalty | `−0.05` | Penalizes wasted time |
|
|
|
|
| 194 |
End-of-episode score in [0.0, 1.0]:
|
| 195 |
|
| 196 |
```
|
| 197 |
+
score = 0.40 × completion + 0.20 × deadline + 0.15 × efficiency + 0.10 × energy_mgmt + 0.15 × stress_mgmt − switch_penalty
|
| 198 |
```
|
| 199 |
|
| 200 |
| Component | Calculation |
|
| 201 |
|-----------|-------------|
|
| 202 |
| Completion | Importance-weighted fraction of tasks completed |
|
| 203 |
+
| Deadline | Importance-weighted fraction of deadlines met (missing important deadlines hurts more) |
|
| 204 |
+
| Efficiency | Realistic optimal steps (using avg energy) / actual working steps |
|
| 205 |
| Energy mgmt | Average energy maintained over the episode |
|
| 206 |
| Stress mgmt | 1 − average stress over the episode |
|
| 207 |
+
| Switch penalty | `min(0.15, switch_count × 0.02)` — penalizes excessive context-switching |
|
| 208 |
|
| 209 |
## Baseline Scores
|
| 210 |
|
| 211 |
Measured with the included `inference.py` heuristic (no LLM):
|
| 212 |
|
| 213 |
+
| Scenario | Baseline Heuristic | Random Agent (avg of 10) | Idle (all breaks) |
|
| 214 |
+
|----------|-------------------|--------------------------|-------------------|
|
| 215 |
+
| Easy | **0.670** | 0.310 | 0.241 |
|
| 216 |
+
| Medium | **0.612** | 0.533 | 0.238 |
|
| 217 |
+
| Hard | 0.145 | **0.422** | 0.232 |
|
| 218 |
|
| 219 |
+
- Idle/degenerate strategies score ~0.23-0.24 (zero completion, zero efficiency)
|
| 220 |
+
- The heuristic dominates on easy and medium but fails on hard — it is too conservative with energy management for the low-energy start (0.40), completing zero tasks. This demonstrates that hard truly requires intelligent triage, not simple rules.
|
| 221 |
+
- Strong LLM agents should score 0.65+ by learning energy management, stress-aware pacing, and deadline triage
|
| 222 |
|
| 223 |
## Setup Instructions
|
| 224 |
|
|
@@ -1,3 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
RhythmEnv — Daily Planning RL Environment for OpenEnv.
|
| 3 |
|
|
|
|
| 1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# LICENSE file in the root directory of this source tree.
|
| 6 |
+
|
| 7 |
"""
|
| 8 |
RhythmEnv — Daily Planning RL Environment for OpenEnv.
|
| 9 |
|
|
@@ -1,3 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
RhythmEnv Client.
|
| 3 |
|
|
|
|
| 1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# LICENSE file in the root directory of this source tree.
|
| 6 |
+
|
| 7 |
"""
|
| 8 |
RhythmEnv Client.
|
| 9 |
|
|
@@ -1,3 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
RhythmEnv Inference Script
|
| 3 |
===================================
|
|
|
|
| 1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# LICENSE file in the root directory of this source tree.
|
| 6 |
+
|
| 7 |
"""
|
| 8 |
RhythmEnv Inference Script
|
| 9 |
===================================
|
|
@@ -1,3 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
Data models for RhythmEnv Environment.
|
| 3 |
|
|
|
|
| 1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# LICENSE file in the root directory of this source tree.
|
| 6 |
+
|
| 7 |
"""
|
| 8 |
Data models for RhythmEnv Environment.
|
| 9 |
|
|
@@ -18,7 +18,6 @@ dependencies = [
|
|
| 18 |
"fastapi>=0.115.0",
|
| 19 |
"pydantic>=2.0.0",
|
| 20 |
"uvicorn>=0.24.0",
|
| 21 |
-
"requests>=2.31.0",
|
| 22 |
]
|
| 23 |
|
| 24 |
[project.optional-dependencies]
|
|
|
|
| 18 |
"fastapi>=0.115.0",
|
| 19 |
"pydantic>=2.0.0",
|
| 20 |
"uvicorn>=0.24.0",
|
|
|
|
| 21 |
]
|
| 22 |
|
| 23 |
[project.optional-dependencies]
|
|
@@ -1,3 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""RhythmEnv environment server components."""
|
| 2 |
|
| 3 |
from .rhythm_environment import RhythmEnvironment
|
|
|
|
| 1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# LICENSE file in the root directory of this source tree.
|
| 6 |
+
|
| 7 |
"""RhythmEnv environment server components."""
|
| 8 |
|
| 9 |
from .rhythm_environment import RhythmEnvironment
|
|
@@ -1,3 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
FastAPI application for the RhythmEnv Environment.
|
| 3 |
|
|
@@ -24,7 +30,7 @@ Usage:
|
|
| 24 |
|
| 25 |
try:
|
| 26 |
from openenv.core.env_server.http_server import create_app
|
| 27 |
-
except
|
| 28 |
raise ImportError(
|
| 29 |
"openenv is required for the web interface. Install dependencies with '\n uv sync\n'"
|
| 30 |
) from e
|
|
|
|
| 1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# LICENSE file in the root directory of this source tree.
|
| 6 |
+
|
| 7 |
"""
|
| 8 |
FastAPI application for the RhythmEnv Environment.
|
| 9 |
|
|
|
|
| 30 |
|
| 31 |
try:
|
| 32 |
from openenv.core.env_server.http_server import create_app
|
| 33 |
+
except ImportError as e: # pragma: no cover
|
| 34 |
raise ImportError(
|
| 35 |
"openenv is required for the web interface. Install dependencies with '\n uv sync\n'"
|
| 36 |
) from e
|
|
@@ -1,3 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
RhythmEnv Environment Implementation.
|
| 3 |
|
|
@@ -22,9 +28,7 @@ try:
|
|
| 22 |
RhythmState,
|
| 23 |
TaskInfo,
|
| 24 |
)
|
| 25 |
-
except ImportError
|
| 26 |
-
if "relative import" not in str(e) and "no known parent package" not in str(e):
|
| 27 |
-
raise
|
| 28 |
from models import (
|
| 29 |
ActionType,
|
| 30 |
RhythmAction,
|
|
@@ -196,7 +200,8 @@ BREAK_SPAM_PENALTY = 0.05
|
|
| 196 |
SWITCH_PENALTY = 0.1
|
| 197 |
IDLE_PENALTY = 0.05
|
| 198 |
DEADLINE_MISS_PENALTY = 0.3
|
| 199 |
-
STRESS_PENALTY_RATE = 0.
|
|
|
|
| 200 |
PROGRESS_REWARD_SCALE = 2.0
|
| 201 |
COMPLETION_BONUS_SCALE = 1.5
|
| 202 |
DEEP_WORK_BONUS = 0.05
|
|
@@ -308,6 +313,7 @@ class RhythmEnvironment(Environment):
|
|
| 308 |
switched = False
|
| 309 |
is_idle = False
|
| 310 |
is_meeting = self._timestep in self._meetings
|
|
|
|
| 311 |
|
| 312 |
# --- Meeting override ---
|
| 313 |
if is_meeting:
|
|
@@ -329,18 +335,16 @@ class RhythmEnvironment(Environment):
|
|
| 329 |
self._consecutive_breaks = 0
|
| 330 |
|
| 331 |
if action.action_type == ActionType.START_TASK:
|
| 332 |
-
|
| 333 |
-
switched = True
|
| 334 |
self._current_task_id = action.task_id
|
| 335 |
|
| 336 |
elif action.action_type == ActionType.SWITCH_TASK:
|
| 337 |
-
|
| 338 |
-
|
| 339 |
self._current_task_id = action.task_id
|
| 340 |
|
| 341 |
elif action.action_type == ActionType.CONTINUE_TASK:
|
| 342 |
-
|
| 343 |
-
is_idle = True
|
| 344 |
|
| 345 |
# Apply switch energy penalty
|
| 346 |
if switched:
|
|
@@ -353,20 +357,23 @@ class RhythmEnvironment(Environment):
|
|
| 353 |
and not is_idle
|
| 354 |
and self._current_task_id not in self._completed_tasks
|
| 355 |
):
|
|
|
|
| 356 |
task = self._tasks[self._current_task_id]
|
| 357 |
-
|
|
|
|
|
|
|
| 358 |
task["progress"] = min(task["effort"], task["progress"] + progress_delta)
|
| 359 |
|
| 360 |
# Check completion
|
| 361 |
if task["progress"] >= task["effort"] and self._current_task_id not in self._completed_tasks:
|
| 362 |
self._completed_tasks.add(self._current_task_id)
|
| 363 |
completed_this_step.append(self._current_task_id)
|
|
|
|
|
|
|
|
|
|
| 364 |
|
| 365 |
self._energy = max(0.0, self._energy - ENERGY_WORK_DRAIN)
|
| 366 |
self._steps_working += 1
|
| 367 |
-
elif self._current_task_id is not None and self._current_task_id in self._completed_tasks:
|
| 368 |
-
# Working on already-completed task = idle
|
| 369 |
-
is_idle = True
|
| 370 |
|
| 371 |
# --- Check deadlines ---
|
| 372 |
new_missed: List[int] = []
|
|
@@ -400,9 +407,10 @@ class RhythmEnvironment(Environment):
|
|
| 400 |
# --- Compute reward ---
|
| 401 |
reward = 0.0
|
| 402 |
|
| 403 |
-
# Progress reward
|
| 404 |
-
|
| 405 |
-
|
|
|
|
| 406 |
r = progress_delta * task["importance"] * PROGRESS_REWARD_SCALE
|
| 407 |
reward += r
|
| 408 |
reward_breakdown["progress_reward"] = round(r, 4)
|
|
@@ -454,11 +462,12 @@ class RhythmEnvironment(Environment):
|
|
| 454 |
reward += mode_bonus
|
| 455 |
reward_breakdown["mode_bonus"] = round(mode_bonus, 4)
|
| 456 |
|
| 457 |
-
# Clamp reward
|
| 458 |
-
reward = max(-
|
| 459 |
|
| 460 |
# --- Done? ---
|
| 461 |
-
|
|
|
|
| 462 |
|
| 463 |
# --- Final grading ---
|
| 464 |
if done:
|
|
@@ -489,14 +498,29 @@ class RhythmEnvironment(Environment):
|
|
| 489 |
|
| 490 |
def _validate_action(self, action: RhythmAction) -> bool:
|
| 491 |
"""Return True if the action is valid given current state."""
|
| 492 |
-
if action.action_type
|
| 493 |
if action.task_id is None:
|
| 494 |
return False
|
| 495 |
if action.task_id < 0 or action.task_id >= len(self._tasks):
|
| 496 |
return False
|
| 497 |
if action.task_id in self._completed_tasks:
|
| 498 |
return False
|
| 499 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 500 |
if self._current_task_id is None:
|
| 501 |
return False
|
| 502 |
if self._current_task_id in self._completed_tasks:
|
|
@@ -529,7 +553,15 @@ class RhythmEnvironment(Environment):
|
|
| 529 |
meetings = kwargs.get("meetings", [])
|
| 530 |
if not isinstance(meetings, list):
|
| 531 |
meetings = []
|
| 532 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 533 |
|
| 534 |
initial_energy = max(0.1, min(1.0, float(kwargs.get("initial_energy", 0.8))))
|
| 535 |
|
|
@@ -570,34 +602,46 @@ class RhythmEnvironment(Environment):
|
|
| 570 |
completed_importance / total_importance if total_importance > 0 else 0.0
|
| 571 |
)
|
| 572 |
|
| 573 |
-
# 2. Deadline score
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
|
|
|
|
|
|
|
|
|
| 577 |
|
| 578 |
-
# 3. Efficiency score
|
|
|
|
| 579 |
total_effort = sum(
|
| 580 |
t["effort"]
|
| 581 |
for t in self._tasks
|
| 582 |
if t["id"] in self._completed_tasks
|
| 583 |
)
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 587 |
|
| 588 |
# 4. Energy management (average energy)
|
| 589 |
-
steps_elapsed = max(self._timestep, 1)
|
| 590 |
energy_management = self._total_energy / steps_elapsed
|
| 591 |
|
| 592 |
# 5. Stress management (1 - average stress)
|
| 593 |
stress_management = 1.0 - (self._total_stress / steps_elapsed)
|
| 594 |
|
|
|
|
|
|
|
|
|
|
| 595 |
score = (
|
| 596 |
-
0.
|
| 597 |
+ 0.20 * deadline_score
|
| 598 |
+ 0.15 * efficiency_score
|
| 599 |
+ 0.10 * energy_management
|
| 600 |
-
+ 0.
|
|
|
|
| 601 |
)
|
| 602 |
return max(0.0, min(1.0, score))
|
| 603 |
|
|
|
|
| 1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# LICENSE file in the root directory of this source tree.
|
| 6 |
+
|
| 7 |
"""
|
| 8 |
RhythmEnv Environment Implementation.
|
| 9 |
|
|
|
|
| 28 |
RhythmState,
|
| 29 |
TaskInfo,
|
| 30 |
)
|
| 31 |
+
except (ImportError, ModuleNotFoundError):
|
|
|
|
|
|
|
| 32 |
from models import (
|
| 33 |
ActionType,
|
| 34 |
RhythmAction,
|
|
|
|
| 200 |
SWITCH_PENALTY = 0.1
|
| 201 |
IDLE_PENALTY = 0.05
|
| 202 |
DEADLINE_MISS_PENALTY = 0.3
|
| 203 |
+
STRESS_PENALTY_RATE = 0.15
|
| 204 |
+
STRESS_PROGRESS_FACTOR = 0.3
|
| 205 |
PROGRESS_REWARD_SCALE = 2.0
|
| 206 |
COMPLETION_BONUS_SCALE = 1.5
|
| 207 |
DEEP_WORK_BONUS = 0.05
|
|
|
|
| 313 |
switched = False
|
| 314 |
is_idle = False
|
| 315 |
is_meeting = self._timestep in self._meetings
|
| 316 |
+
worked_on_task_id: Optional[int] = None # tracks which task earned progress this step
|
| 317 |
|
| 318 |
# --- Meeting override ---
|
| 319 |
if is_meeting:
|
|
|
|
| 335 |
self._consecutive_breaks = 0
|
| 336 |
|
| 337 |
if action.action_type == ActionType.START_TASK:
|
| 338 |
+
# Validation ensures current_task_id is None here
|
|
|
|
| 339 |
self._current_task_id = action.task_id
|
| 340 |
|
| 341 |
elif action.action_type == ActionType.SWITCH_TASK:
|
| 342 |
+
# Validation ensures current_task_id exists and differs from target
|
| 343 |
+
switched = True
|
| 344 |
self._current_task_id = action.task_id
|
| 345 |
|
| 346 |
elif action.action_type == ActionType.CONTINUE_TASK:
|
| 347 |
+
pass # Validation already ensured current_task_id is valid
|
|
|
|
| 348 |
|
| 349 |
# Apply switch energy penalty
|
| 350 |
if switched:
|
|
|
|
| 357 |
and not is_idle
|
| 358 |
and self._current_task_id not in self._completed_tasks
|
| 359 |
):
|
| 360 |
+
worked_on_task_id = self._current_task_id
|
| 361 |
task = self._tasks[self._current_task_id]
|
| 362 |
+
# Stress impairs productivity: at stress=1.0, progress is 70% of normal
|
| 363 |
+
stress_factor = 1.0 - (self._stress * STRESS_PROGRESS_FACTOR)
|
| 364 |
+
progress_delta = PROGRESS_RATE * self._energy * stress_factor
|
| 365 |
task["progress"] = min(task["effort"], task["progress"] + progress_delta)
|
| 366 |
|
| 367 |
# Check completion
|
| 368 |
if task["progress"] >= task["effort"] and self._current_task_id not in self._completed_tasks:
|
| 369 |
self._completed_tasks.add(self._current_task_id)
|
| 370 |
completed_this_step.append(self._current_task_id)
|
| 371 |
+
# Auto-clear: agent becomes idle after finishing a task,
|
| 372 |
+
# so they can START_TASK a new one without needing to break/switch.
|
| 373 |
+
self._current_task_id = None
|
| 374 |
|
| 375 |
self._energy = max(0.0, self._energy - ENERGY_WORK_DRAIN)
|
| 376 |
self._steps_working += 1
|
|
|
|
|
|
|
|
|
|
| 377 |
|
| 378 |
# --- Check deadlines ---
|
| 379 |
new_missed: List[int] = []
|
|
|
|
| 407 |
# --- Compute reward ---
|
| 408 |
reward = 0.0
|
| 409 |
|
| 410 |
+
# Progress reward (use worked_on_task_id since current_task_id may be
|
| 411 |
+
# cleared on completion)
|
| 412 |
+
if progress_delta > 0 and worked_on_task_id is not None:
|
| 413 |
+
task = self._tasks[worked_on_task_id]
|
| 414 |
r = progress_delta * task["importance"] * PROGRESS_REWARD_SCALE
|
| 415 |
reward += r
|
| 416 |
reward_breakdown["progress_reward"] = round(r, 4)
|
|
|
|
| 462 |
reward += mode_bonus
|
| 463 |
reward_breakdown["mode_bonus"] = round(mode_bonus, 4)
|
| 464 |
|
| 465 |
+
# Clamp reward (wide enough to preserve completion signal)
|
| 466 |
+
reward = max(-2.0, min(2.0, round(reward, 4)))
|
| 467 |
|
| 468 |
# --- Done? ---
|
| 469 |
+
all_tasks_completed = len(self._completed_tasks) == len(self._tasks)
|
| 470 |
+
done = self._timestep >= MAX_STEPS or all_tasks_completed
|
| 471 |
|
| 472 |
# --- Final grading ---
|
| 473 |
if done:
|
|
|
|
| 498 |
|
| 499 |
def _validate_action(self, action: RhythmAction) -> bool:
|
| 500 |
"""Return True if the action is valid given current state."""
|
| 501 |
+
if action.action_type == ActionType.START_TASK:
|
| 502 |
if action.task_id is None:
|
| 503 |
return False
|
| 504 |
if action.task_id < 0 or action.task_id >= len(self._tasks):
|
| 505 |
return False
|
| 506 |
if action.task_id in self._completed_tasks:
|
| 507 |
return False
|
| 508 |
+
# START_TASK: only valid when not currently working on anything
|
| 509 |
+
if self._current_task_id is not None:
|
| 510 |
+
return False
|
| 511 |
+
elif action.action_type == ActionType.SWITCH_TASK:
|
| 512 |
+
if action.task_id is None:
|
| 513 |
+
return False
|
| 514 |
+
if action.task_id < 0 or action.task_id >= len(self._tasks):
|
| 515 |
+
return False
|
| 516 |
+
if action.task_id in self._completed_tasks:
|
| 517 |
+
return False
|
| 518 |
+
# SWITCH_TASK: only valid when already working on a different task
|
| 519 |
+
if self._current_task_id is None:
|
| 520 |
+
return False
|
| 521 |
+
if self._current_task_id == action.task_id:
|
| 522 |
+
return False
|
| 523 |
+
elif action.action_type == ActionType.CONTINUE_TASK:
|
| 524 |
if self._current_task_id is None:
|
| 525 |
return False
|
| 526 |
if self._current_task_id in self._completed_tasks:
|
|
|
|
| 553 |
meetings = kwargs.get("meetings", [])
|
| 554 |
if not isinstance(meetings, list):
|
| 555 |
meetings = []
|
| 556 |
+
valid_meetings = []
|
| 557 |
+
for m in meetings:
|
| 558 |
+
try:
|
| 559 |
+
mi = int(m)
|
| 560 |
+
if 0 <= mi < MAX_STEPS:
|
| 561 |
+
valid_meetings.append(mi)
|
| 562 |
+
except (ValueError, TypeError):
|
| 563 |
+
continue
|
| 564 |
+
meetings = valid_meetings
|
| 565 |
|
| 566 |
initial_energy = max(0.1, min(1.0, float(kwargs.get("initial_energy", 0.8))))
|
| 567 |
|
|
|
|
| 602 |
completed_importance / total_importance if total_importance > 0 else 0.0
|
| 603 |
)
|
| 604 |
|
| 605 |
+
# 2. Deadline score (importance-weighted: missing important deadlines hurts more)
|
| 606 |
+
met_importance = sum(
|
| 607 |
+
t["importance"]
|
| 608 |
+
for t in self._tasks
|
| 609 |
+
if t["id"] not in self._missed_deadlines
|
| 610 |
+
)
|
| 611 |
+
deadline_score = met_importance / total_importance if total_importance > 0 else 0.0
|
| 612 |
|
| 613 |
+
# 3. Efficiency score (using realistic optimal based on average energy)
|
| 614 |
+
steps_elapsed = max(self._timestep, 1)
|
| 615 |
total_effort = sum(
|
| 616 |
t["effort"]
|
| 617 |
for t in self._tasks
|
| 618 |
if t["id"] in self._completed_tasks
|
| 619 |
)
|
| 620 |
+
if total_effort > 0 and self._steps_working > 0:
|
| 621 |
+
avg_energy = self._total_energy / steps_elapsed
|
| 622 |
+
effective_rate = PROGRESS_RATE * max(avg_energy, 0.3)
|
| 623 |
+
optimal_steps = total_effort / effective_rate
|
| 624 |
+
efficiency_score = min(1.0, optimal_steps / self._steps_working)
|
| 625 |
+
else:
|
| 626 |
+
# No tasks completed = zero efficiency
|
| 627 |
+
efficiency_score = 0.0
|
| 628 |
|
| 629 |
# 4. Energy management (average energy)
|
|
|
|
| 630 |
energy_management = self._total_energy / steps_elapsed
|
| 631 |
|
| 632 |
# 5. Stress management (1 - average stress)
|
| 633 |
stress_management = 1.0 - (self._total_stress / steps_elapsed)
|
| 634 |
|
| 635 |
+
# 6. Switch penalty in grader (penalize excessive context-switching)
|
| 636 |
+
switch_penalty = min(0.15, self._switch_count * 0.02)
|
| 637 |
+
|
| 638 |
score = (
|
| 639 |
+
0.40 * completion_score
|
| 640 |
+ 0.20 * deadline_score
|
| 641 |
+ 0.15 * efficiency_score
|
| 642 |
+ 0.10 * energy_management
|
| 643 |
+
+ 0.15 * stress_management
|
| 644 |
+
- switch_penalty
|
| 645 |
)
|
| 646 |
return max(0.0, min(1.0, score))
|
| 647 |
|
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# LICENSE file in the root directory of this source tree.
|
|
@@ -0,0 +1,306 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# LICENSE file in the root directory of this source tree.
|
| 6 |
+
|
| 7 |
+
"""Tests for RhythmEnv environment."""
|
| 8 |
+
|
| 9 |
+
import pytest
|
| 10 |
+
|
| 11 |
+
from server.rhythm_environment import (
|
| 12 |
+
MAX_STEPS,
|
| 13 |
+
RhythmEnvironment,
|
| 14 |
+
)
|
| 15 |
+
from models import ActionType, RhythmAction
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@pytest.fixture
|
| 19 |
+
def env():
|
| 20 |
+
return RhythmEnvironment()
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# ---------------------------------------------------------------------------
|
| 24 |
+
# reset() tests
|
| 25 |
+
# ---------------------------------------------------------------------------
|
| 26 |
+
|
| 27 |
+
class TestReset:
|
| 28 |
+
def test_reset_returns_observation(self, env):
|
| 29 |
+
obs = env.reset(task="easy")
|
| 30 |
+
assert obs.timestep == 0
|
| 31 |
+
assert obs.done is False
|
| 32 |
+
assert obs.reward == 0.0
|
| 33 |
+
|
| 34 |
+
def test_reset_easy_has_3_tasks(self, env):
|
| 35 |
+
obs = env.reset(task="easy")
|
| 36 |
+
assert len(obs.tasks) == 3
|
| 37 |
+
|
| 38 |
+
def test_reset_medium_has_4_tasks(self, env):
|
| 39 |
+
obs = env.reset(task="medium")
|
| 40 |
+
assert len(obs.tasks) == 4
|
| 41 |
+
|
| 42 |
+
def test_reset_hard_has_5_tasks(self, env):
|
| 43 |
+
obs = env.reset(task="hard")
|
| 44 |
+
assert len(obs.tasks) == 5
|
| 45 |
+
|
| 46 |
+
def test_reset_unknown_task_defaults_to_easy(self, env):
|
| 47 |
+
obs = env.reset(task="nonexistent")
|
| 48 |
+
assert len(obs.tasks) == 3
|
| 49 |
+
|
| 50 |
+
def test_reset_empty_defaults_to_easy(self, env):
|
| 51 |
+
obs = env.reset()
|
| 52 |
+
assert len(obs.tasks) == 3
|
| 53 |
+
|
| 54 |
+
def test_reset_clears_state(self, env):
|
| 55 |
+
obs = env.reset(task="easy")
|
| 56 |
+
env.step(RhythmAction(action_type=ActionType.START_TASK, task_id=0))
|
| 57 |
+
obs = env.reset(task="easy")
|
| 58 |
+
assert obs.timestep == 0
|
| 59 |
+
assert obs.energy == 0.75
|
| 60 |
+
assert obs.current_task_id is None
|
| 61 |
+
|
| 62 |
+
def test_reset_custom_tasks(self, env):
|
| 63 |
+
obs = env.reset(
|
| 64 |
+
task="custom",
|
| 65 |
+
tasks=[
|
| 66 |
+
{"name": "T1", "effort": 0.3, "deadline": 10, "importance": 0.8},
|
| 67 |
+
{"name": "T2", "effort": 0.2, "deadline": 15, "importance": 0.5},
|
| 68 |
+
],
|
| 69 |
+
meetings=[5],
|
| 70 |
+
initial_energy=0.6,
|
| 71 |
+
)
|
| 72 |
+
assert len(obs.tasks) == 2
|
| 73 |
+
assert obs.tasks[0].name == "T1"
|
| 74 |
+
assert obs.energy == 0.6
|
| 75 |
+
assert obs.meetings == [5]
|
| 76 |
+
|
| 77 |
+
def test_reset_custom_rejects_empty_tasks(self, env):
|
| 78 |
+
with pytest.raises(ValueError, match="tasks"):
|
| 79 |
+
env.reset(task="custom", tasks=[])
|
| 80 |
+
|
| 81 |
+
def test_reset_custom_clamps_bounds(self, env):
|
| 82 |
+
obs = env.reset(
|
| 83 |
+
task="custom",
|
| 84 |
+
tasks=[{"name": "X", "effort": 99, "deadline": 999, "importance": -5}],
|
| 85 |
+
initial_energy=50,
|
| 86 |
+
)
|
| 87 |
+
t = obs.tasks[0]
|
| 88 |
+
assert t.effort <= 1.0
|
| 89 |
+
assert t.deadline <= MAX_STEPS
|
| 90 |
+
assert t.importance >= 0.1
|
| 91 |
+
assert obs.energy <= 1.0
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# ---------------------------------------------------------------------------
|
| 95 |
+
# step() tests
|
| 96 |
+
# ---------------------------------------------------------------------------
|
| 97 |
+
|
| 98 |
+
class TestStep:
|
| 99 |
+
def test_step_advances_timestep(self, env):
|
| 100 |
+
env.reset(task="easy")
|
| 101 |
+
obs = env.step(RhythmAction(action_type=ActionType.START_TASK, task_id=0))
|
| 102 |
+
assert obs.timestep == 1
|
| 103 |
+
|
| 104 |
+
def test_working_drains_energy(self, env):
|
| 105 |
+
env.reset(task="easy")
|
| 106 |
+
initial_energy = 0.75
|
| 107 |
+
obs = env.step(RhythmAction(action_type=ActionType.START_TASK, task_id=0))
|
| 108 |
+
assert obs.energy < initial_energy
|
| 109 |
+
|
| 110 |
+
def test_break_recovers_energy(self, env):
|
| 111 |
+
env.reset(task="hard") # starts at 0.4 energy
|
| 112 |
+
obs = env.step(RhythmAction(action_type=ActionType.TAKE_BREAK))
|
| 113 |
+
assert obs.energy > 0.4
|
| 114 |
+
|
| 115 |
+
def test_progress_accumulates(self, env):
|
| 116 |
+
env.reset(task="easy")
|
| 117 |
+
obs = env.step(RhythmAction(action_type=ActionType.START_TASK, task_id=0))
|
| 118 |
+
assert obs.tasks[0].progress > 0.0
|
| 119 |
+
|
| 120 |
+
def test_continue_task_works(self, env):
|
| 121 |
+
env.reset(task="easy")
|
| 122 |
+
env.step(RhythmAction(action_type=ActionType.START_TASK, task_id=0))
|
| 123 |
+
obs = env.step(RhythmAction(action_type=ActionType.CONTINUE_TASK))
|
| 124 |
+
assert obs.tasks[0].progress > 0.0
|
| 125 |
+
|
| 126 |
+
def test_start_task_invalid_when_already_working(self, env):
|
| 127 |
+
"""START_TASK should fail (idle) when already working on a task."""
|
| 128 |
+
env.reset(task="easy")
|
| 129 |
+
env.step(RhythmAction(action_type=ActionType.START_TASK, task_id=0))
|
| 130 |
+
# Trying START_TASK again while already on task 0 should be invalid
|
| 131 |
+
obs = env.step(RhythmAction(action_type=ActionType.START_TASK, task_id=1))
|
| 132 |
+
# Should incur idle penalty since START is invalid when current_task_id is set
|
| 133 |
+
assert "idle_penalty" in obs.reward_breakdown
|
| 134 |
+
|
| 135 |
+
def test_switch_task_requires_current_task(self, env):
|
| 136 |
+
"""SWITCH_TASK should fail when no task is active."""
|
| 137 |
+
env.reset(task="easy")
|
| 138 |
+
obs = env.step(RhythmAction(action_type=ActionType.SWITCH_TASK, task_id=0))
|
| 139 |
+
assert "idle_penalty" in obs.reward_breakdown
|
| 140 |
+
|
| 141 |
+
def test_meeting_blocks_action(self, env):
|
| 142 |
+
"""At meeting timesteps, the action should be ignored."""
|
| 143 |
+
env.reset(task="easy") # meetings at steps 3 and 11
|
| 144 |
+
# Advance to step 3 (meeting)
|
| 145 |
+
for _ in range(3):
|
| 146 |
+
env.step(RhythmAction(action_type=ActionType.TAKE_BREAK))
|
| 147 |
+
# Step at meeting time — action should be ignored
|
| 148 |
+
obs = env.step(RhythmAction(action_type=ActionType.START_TASK, task_id=0))
|
| 149 |
+
# After meeting, current_task_id should still be None
|
| 150 |
+
assert obs.current_task_id is None
|
| 151 |
+
|
| 152 |
+
def test_episode_ends_at_max_steps(self, env):
|
| 153 |
+
env.reset(task="easy")
|
| 154 |
+
for _ in range(MAX_STEPS):
|
| 155 |
+
obs = env.step(RhythmAction(action_type=ActionType.TAKE_BREAK))
|
| 156 |
+
assert obs.done is True
|
| 157 |
+
|
| 158 |
+
def test_early_termination_when_all_tasks_complete(self, env):
|
| 159 |
+
"""Episode ends early if all tasks are completed."""
|
| 160 |
+
env.reset(
|
| 161 |
+
task="custom",
|
| 162 |
+
tasks=[{"name": "Tiny", "effort": 0.05, "deadline": 19, "importance": 0.5}],
|
| 163 |
+
initial_energy=1.0,
|
| 164 |
+
)
|
| 165 |
+
obs = env.step(RhythmAction(action_type=ActionType.START_TASK, task_id=0))
|
| 166 |
+
assert obs.done is True
|
| 167 |
+
assert obs.timestep < MAX_STEPS
|
| 168 |
+
assert "final_score" in obs.reward_breakdown
|
| 169 |
+
|
| 170 |
+
def test_stress_affects_progress(self, env):
|
| 171 |
+
"""High stress should reduce progress rate."""
|
| 172 |
+
# Run 1: zero-stress scenario (custom, high energy, generous deadline)
|
| 173 |
+
env.reset(
|
| 174 |
+
task="custom",
|
| 175 |
+
tasks=[{"name": "A", "effort": 1.0, "deadline": 19, "importance": 0.5}],
|
| 176 |
+
initial_energy=1.0,
|
| 177 |
+
)
|
| 178 |
+
obs1 = env.step(RhythmAction(action_type=ActionType.START_TASK, task_id=0))
|
| 179 |
+
progress_low_stress = obs1.tasks[0].progress
|
| 180 |
+
|
| 181 |
+
# Run 2: build up stress by missing multiple deadlines, then measure
|
| 182 |
+
# progress. Use 3 tasks with deadline=1 so that after 2 steps,
|
| 183 |
+
# timestep=2 > 1 triggers 3 deadline misses → stress += 0.45.
|
| 184 |
+
# Work on task 3 throughout so energy drains equally.
|
| 185 |
+
env.reset(
|
| 186 |
+
task="custom",
|
| 187 |
+
tasks=[
|
| 188 |
+
{"name": "M1", "effort": 1.0, "deadline": 1, "importance": 0.9},
|
| 189 |
+
{"name": "M2", "effort": 1.0, "deadline": 1, "importance": 0.9},
|
| 190 |
+
{"name": "M3", "effort": 1.0, "deadline": 1, "importance": 0.9},
|
| 191 |
+
{"name": "B", "effort": 1.0, "deadline": 19, "importance": 0.5},
|
| 192 |
+
],
|
| 193 |
+
initial_energy=1.0,
|
| 194 |
+
)
|
| 195 |
+
# Step 1 (timestep 0→1): work on task B, deadlines approaching → some stress
|
| 196 |
+
env.step(RhythmAction(action_type=ActionType.START_TASK, task_id=3))
|
| 197 |
+
# Step 2 (timestep 1→2): continue working, 3 deadlines missed → stress +0.45
|
| 198 |
+
env.step(RhythmAction(action_type=ActionType.CONTINUE_TASK))
|
| 199 |
+
# Step 3: continue working on B under high stress
|
| 200 |
+
obs2 = env.step(RhythmAction(action_type=ActionType.CONTINUE_TASK))
|
| 201 |
+
# Total progress on B across steps 1-3
|
| 202 |
+
progress_high_stress_total = obs2.tasks[3].progress
|
| 203 |
+
# Progress in step 3 alone = total - progress from steps 1+2
|
| 204 |
+
# But simpler: just compare step-1 progress (no stress) vs later progress.
|
| 205 |
+
# Since energy decreases each step and stress accumulates, step 3's
|
| 206 |
+
# marginal progress is less than step 1's.
|
| 207 |
+
# We can verify stress is significant:
|
| 208 |
+
assert obs2.stress >= 0.3
|
| 209 |
+
# And the average per-step progress under stress is lower than the
|
| 210 |
+
# first step's progress with zero stress and full energy:
|
| 211 |
+
avg_progress_per_step = progress_high_stress_total / 3
|
| 212 |
+
assert progress_low_stress > avg_progress_per_step
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
# ---------------------------------------------------------------------------
|
| 216 |
+
# Grader tests
|
| 217 |
+
# ---------------------------------------------------------------------------
|
| 218 |
+
|
| 219 |
+
class TestGrader:
|
| 220 |
+
def test_final_score_in_range(self, env):
|
| 221 |
+
env.reset(task="easy")
|
| 222 |
+
for _ in range(MAX_STEPS):
|
| 223 |
+
obs = env.step(RhythmAction(action_type=ActionType.TAKE_BREAK))
|
| 224 |
+
score = obs.reward_breakdown.get("final_score", -1)
|
| 225 |
+
assert 0.0 <= score <= 1.0
|
| 226 |
+
|
| 227 |
+
def test_doing_nothing_scores_low(self, env):
|
| 228 |
+
env.reset(task="hard")
|
| 229 |
+
for _ in range(MAX_STEPS):
|
| 230 |
+
obs = env.step(RhythmAction(action_type=ActionType.TAKE_BREAK))
|
| 231 |
+
score = obs.reward_breakdown["final_score"]
|
| 232 |
+
assert score < 0.5
|
| 233 |
+
|
| 234 |
+
def test_heuristic_beats_random(self, env):
|
| 235 |
+
"""The simple heuristic should beat a do-nothing strategy."""
|
| 236 |
+
import random
|
| 237 |
+
random.seed(42)
|
| 238 |
+
|
| 239 |
+
# Heuristic run
|
| 240 |
+
obs = env.reset(task="easy")
|
| 241 |
+
for _ in range(MAX_STEPS):
|
| 242 |
+
if obs.done:
|
| 243 |
+
break
|
| 244 |
+
uncompleted = [t for t in obs.tasks if t.progress < t.effort]
|
| 245 |
+
if obs.energy < 0.3 or not uncompleted:
|
| 246 |
+
action = RhythmAction(action_type=ActionType.TAKE_BREAK)
|
| 247 |
+
elif obs.current_task_id is not None:
|
| 248 |
+
action = RhythmAction(action_type=ActionType.CONTINUE_TASK)
|
| 249 |
+
else:
|
| 250 |
+
action = RhythmAction(action_type=ActionType.START_TASK, task_id=uncompleted[0].id)
|
| 251 |
+
obs = env.step(action)
|
| 252 |
+
heuristic_score = obs.reward_breakdown["final_score"]
|
| 253 |
+
|
| 254 |
+
# Do-nothing run
|
| 255 |
+
obs = env.reset(task="easy")
|
| 256 |
+
for _ in range(MAX_STEPS):
|
| 257 |
+
obs = env.step(RhythmAction(action_type=ActionType.TAKE_BREAK))
|
| 258 |
+
idle_score = obs.reward_breakdown["final_score"]
|
| 259 |
+
|
| 260 |
+
assert heuristic_score > idle_score
|
| 261 |
+
|
| 262 |
+
def test_grader_deterministic(self, env):
|
| 263 |
+
"""Same actions produce same score."""
|
| 264 |
+
def run():
|
| 265 |
+
obs = env.reset(task="medium")
|
| 266 |
+
for _ in range(MAX_STEPS):
|
| 267 |
+
if obs.done:
|
| 268 |
+
break
|
| 269 |
+
obs = env.step(RhythmAction(action_type=ActionType.TAKE_BREAK))
|
| 270 |
+
return obs.reward_breakdown["final_score"]
|
| 271 |
+
|
| 272 |
+
assert run() == run()
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
# ---------------------------------------------------------------------------
|
| 276 |
+
# Edge cases
|
| 277 |
+
# ---------------------------------------------------------------------------
|
| 278 |
+
|
| 279 |
+
class TestEdgeCases:
|
| 280 |
+
def test_complete_task_then_continue_is_idle(self, env):
|
| 281 |
+
"""Continuing after completing a task should be treated as idle.
|
| 282 |
+
With auto-clear, current_task_id resets to None on completion,
|
| 283 |
+
so CONTINUE_TASK becomes invalid (no active task)."""
|
| 284 |
+
env.reset(
|
| 285 |
+
task="custom",
|
| 286 |
+
tasks=[
|
| 287 |
+
{"name": "Quick", "effort": 0.05, "deadline": 19, "importance": 0.5},
|
| 288 |
+
{"name": "Other", "effort": 1.0, "deadline": 19, "importance": 0.5},
|
| 289 |
+
],
|
| 290 |
+
initial_energy=1.0,
|
| 291 |
+
)
|
| 292 |
+
# One step should complete task 0 (0.15 * 1.0 > 0.05)
|
| 293 |
+
obs = env.step(RhythmAction(action_type=ActionType.START_TASK, task_id=0))
|
| 294 |
+
assert obs.tasks[0].progress >= obs.tasks[0].effort
|
| 295 |
+
# current_task_id auto-cleared after completion
|
| 296 |
+
assert obs.current_task_id is None
|
| 297 |
+
# CONTINUE with no current task → idle
|
| 298 |
+
obs = env.step(RhythmAction(action_type=ActionType.CONTINUE_TASK))
|
| 299 |
+
assert "idle_penalty" in obs.reward_breakdown
|
| 300 |
+
|
| 301 |
+
def test_reward_breakdown_has_all_components(self, env):
|
| 302 |
+
env.reset(task="easy")
|
| 303 |
+
obs = env.step(RhythmAction(action_type=ActionType.START_TASK, task_id=0))
|
| 304 |
+
# Should have at least progress_reward and stress_penalty
|
| 305 |
+
assert "progress_reward" in obs.reward_breakdown
|
| 306 |
+
assert "stress_penalty" in obs.reward_breakdown
|