File size: 13,018 Bytes
46b3240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

"""

Basic integration test for ToolforgeEnvironment.



Tests the full episode lifecycle:

1. Create environment with task provider factory

2. Reset with eval mode and easy difficulty

3. Execute step actions through all tasks in the queue

4. Verify state progression and episode termination

"""

from datetime import datetime
import logging
from pathlib import Path

from server.toolforge_env_environment import ToolforgeEnvironment
from server.inputs.factory import create_input_provider
from server.inputs.simulated.task_selector import TaskSelector
from models import ToolforgeAction, ToolCall

logger = logging.getLogger(__name__)


def configure_logging() -> Path:
    """Configure console + timestamped file logging under test/ directory."""

    log_dir = Path(__file__).parent / "test"
    log_dir.mkdir(parents=True, exist_ok=True)

    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    log_file_path = log_dir / f"test_environment_{timestamp}.log"

    root_logger = logging.getLogger()
    root_logger.setLevel(logging.INFO)
    root_logger.handlers.clear()

    formatter = logging.Formatter(
        "%(asctime)s | %(levelname)-8s | %(name)s | %(message)s",
        "%Y-%m-%d %H:%M:%S",
    )

    file_handler = logging.FileHandler(log_file_path, encoding="utf-8")
    file_handler.setLevel(logging.INFO)
    file_handler.setFormatter(formatter)

    stream_handler = logging.StreamHandler()
    stream_handler.setLevel(logging.INFO)
    stream_handler.setFormatter(formatter)

    root_logger.addHandler(file_handler)
    root_logger.addHandler(stream_handler)

    logger.info("Logging initialized. Log file: %s", log_file_path)
    return log_file_path


def test_environment_reset_and_step():
    """

    Test basic reset β†’ step β†’ step scenario.

    

    Verifies:

    - Environment initializes correctly

    - Reset provides valid observation and task

    - Step processes actions without crashing

    - Step returns correct done/reward signals

    - State advances through tasks

    """
    logger.info("=" * 80)
    logger.info("TEST: Environment Reset and Step")
    logger.info("=" * 80)

    # ───────────────────────────────────────────────────────────────────────
    # 1. Initialize environment with task provider factory
    # ───────────────────────────────────────────────────────────────────────
    task_selector = TaskSelector(mode="eval")
    logger.info(f"Task selector created successfully: {task_selector}")
    env = ToolforgeEnvironment(
        task_selector=task_selector,
        input_provider_factory=create_input_provider
    )
    logger.info(f"βœ“ Environment created successfully: {env}")

    # ───────────────────────────────────────────────────────────────────────
    # 2. Reset environment in eval mode with easy difficulty
    # ───────────────────────────────────────────────────────────────────────
    reset_kwargs = {
        "episode_id": "test-episode-001",
        "mode": "eval",
        "difficulty": "easy",
        "seed": 42,
    }
    obs = env.reset(**reset_kwargs)
    
    logger.info(f"βœ“ Reset successful")
    logger.info(f"  Episode ID: {env.state.episode_id}")
    logger.info(f"  Initial Task: {obs.current_task.id}")
    logger.info(f"  Task Prompt: {obs.current_task.prompt}")
    logger.info(f"  Task Difficulty: {obs.current_task.difficulty}")
    logger.info(f"  Required Slots: {obs.current_task.required_slots}")
    logger.info(f"  Baseline Call Count: {obs.current_task.baseline_call_count}")
    logger.info(f"  Available Tools: {obs.available_tools}")
    logger.info(f"  Done: {obs.done}")
    logger.info(f"  Reward: {obs.reward}")
    
    assert obs is not None, "Reset should return observation"
    assert obs.current_task is not None, "Reset should provide current task"
    assert not obs.done, "Episode should not be done immediately after reset"
    assert len(obs.available_tools) > 0, "Some tools should be available"

    # ───────────────────────────────────────────────────────────────────────
    # 3. Build sample actions and execute steps
    # ───────────────────────────────────────────────────────────────────────
    step_count = 0
    max_steps = 20  # Limit iterations to prevent infinite loops
    
    logger.info("\n" + "=" * 80)
    logger.info("STEPPING THROUGH EPISODE")
    logger.info("=" * 80)
    
    while not obs.done and step_count < max_steps:
        step_count += 1
        current_task_id = obs.current_task.id
        logger.info(f"\n--- Step {step_count} ---")
        logger.info(f"Current Task ID: {current_task_id}")
        
        # Get tool names from available tools
        available_tool_names = [tool.name for tool in obs.available_tools]
        logger.info(f"Available Tools: {available_tool_names}")
        
        # Create a sample action: deploy service with healthcheck and notify
        # This mirrors a typical DevOps workflow pattern
        sample_plan = [
            ToolCall(
                tool_name="deploy",
                params={
                    "service_name": "test-service",
                    "version": "v1.0.0"
                }
            ),
            ToolCall(
                tool_name="healthcheck",
                params={
                    "service_name": "test-service"
                }
            ),
        ]
        
        action = ToolforgeAction(
            action_type="propose_plan",
            plan=sample_plan,
            macro_proposal=None,
            reasoning="Deploy service and verify health for ease-difficulty task completion"
        )
        
        logger.info(f"Action Plan: {[f'{call.tool_name}' for call in action.plan]}")
        
        # Execute the action
        obs = env.step(action)
        
        logger.info(f"βœ“ Step executed")
        logger.info(f"  Reward: {obs.reward:.3f}")
        logger.info(f"  Done: {obs.done}")
        logger.info(f"  Step Count in State: {env.state.step_count}")
        logger.info(f"  Tokens Used: {env.state.tokens_used}")
        logger.info(f"  Tasks Completed: {len(env.state.completed_tasks)}")
        logger.info(f"  Remaining Tasks in Queue: {len(env.state.task_queue)}")
        
        if not obs.done and obs.current_task:
            logger.info(f"  Next Task ID: {obs.current_task.id}")
        
        # Metadata from step
        if hasattr(obs, 'metadata') and obs.metadata:
            logger.info(f"  Progression: {obs.metadata.get('progression', 'N/A')}")
            logger.info(f"  Plan Accepted: {obs.metadata.get('plan_accepted', 'N/A')}")
            logger.info(f"  Macro Attempted: {obs.metadata.get('macro_attempted', 'N/A')}")

    # ───────────────────────────────────────────────────────────────────────
    # 4. Verify episode completion
    # ───────────────────────────────────────────────────────────────────────
    logger.info("\n" + "=" * 80)
    logger.info("EPISODE SUMMARY")
    logger.info("=" * 80)
    logger.info(f"Total Steps Executed: {step_count}")
    logger.info(f"Final State Step Count: {env.state.step_count}")
    logger.info(f"Tasks Completed: {len(env.state.completed_tasks)}")
    logger.info(f"Accepted Macros: {len(env.state.accepted_macros)}")
    logger.info(f"Rejected Macros: {env.state.rejected_macro_count}")
    logger.info(f"Total Tokens Used: {env.state.tokens_used}")
    logger.info(f"Episode Done: {env.state.done}")
    logger.info(f"Final Observation Done: {obs.done}")
    
    # List completed tasks
    if env.state.completed_tasks:
        logger.info(f"\nCompleted Tasks:")
        for task in env.state.completed_tasks:
            logger.info(f"  - {task.id} ({task.difficulty})")
    
    # Assertions to verify correctness
    assert obs.done or step_count >= max_steps, "Episode should be done or max steps reached"
    assert env.state.step_count > 0, "Step count should be incremented"
    assert len(env.state.completed_tasks) > 0, "At least one task should be completed"
    assert env.state.tokens_used >= 0, "Token usage should be non-negative"
    
    logger.info("\nβœ“ All assertions passed!")
    logger.info("=" * 80)


def test_environment_multiple_tasks_easy():
    """

    Test environment through multiple easy-difficulty tasks.

    

    Verifies:

    - Task queue is properly managed

    - Episode progresses through multiple tasks

    - Each task is properly presented and tracked

    """
    logger.info("\n\n" + "=" * 80)
    logger.info("TEST: Multiple Tasks in Easy Mode")
    logger.info("=" * 80)

    task_selector = TaskSelector(mode="eval")
    env = ToolforgeEnvironment(
        task_selector=task_selector,
        input_provider_factory=create_input_provider
    )

    # Reset with easy difficulty
    obs = env.reset(
        episode_id="test-episode-multi",
        mode="eval",
        difficulty="easy",
        seed=42,
    )
    
    logger.info(f"βœ“ Reset successful for multi-task test")
    logger.info(f"  Initial Task Queue Size: {len(env.state.task_queue)}")
    
    task_ids_seen = set()
    step_count = 0
    max_steps = 50
    
    while not obs.done and step_count < max_steps:
        step_count += 1
        task_id = obs.current_task.id
        task_ids_seen.add(task_id)
        
        logger.info(f"\nStep {step_count}: Task '{task_id}'")
        logger.info(f"  Difficulty: {obs.current_task.difficulty}")
        logger.info(f"  Queue Size: {len(env.state.task_queue)}")
        
        # Create minimal action plan
        action = ToolforgeAction(
            action_type="propose_plan",
            plan=[
                ToolCall(
                    tool_name="deploy",
                    params={"service_name": "test", "version": "v1.0.0"}
                )
            ],
            macro_proposal=None,
            reasoning="Test action"
        )
        
        obs = env.step(action)
    
    logger.info(f"\nβœ“ Multi-task test completed")
    logger.info(f"  Total Steps: {step_count}")
    logger.info(f"  Unique Tasks Seen: {len(task_ids_seen)}")
    logger.info(f"  Tasks Completed: {len(env.state.completed_tasks)}")
    logger.info(f"  Episode Done: {obs.done}")
    
    assert len(task_ids_seen) > 0, "Should see at least one task"
    assert len(env.state.completed_tasks) > 0, "Should complete at least one task"
    
    logger.info("βœ“ Multi-task test passed!")
    logger.info("=" * 80)


if __name__ == "__main__":
    # ───────────────────────────────────────────────────────────────────────
    # Run all tests
    # ───────────────────────────────────────────────────────────────────────
    log_path = configure_logging()
    try:
        logger.info("Starting tests. Output log: %s", log_path)
        test_environment_reset_and_step()
        test_environment_multiple_tasks_easy()
        
        logger.info("\n\n" + "πŸŽ‰ " * 20)
        logger.info("ALL TESTS PASSED!")
        logger.info("πŸŽ‰ " * 20)
        
    except AssertionError as e:
        logger.error(f"\n❌ Test assertion failed: {e}")
        raise
    except Exception as e:
        logger.error(f"\n❌ Test failed with exception: {e}")
        raise