File size: 1,181 Bytes
e4f3d12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import random

from commitguard_env.environment import CommitGuardEnvironment
from commitguard_env.models import CommitGuardAction
from pathlib import Path


def test_env_100_random_episodes_no_crash() -> None:
    data_path = Path(__file__).resolve().parent.parent / "data" / "devign_filtered.jsonl"
    env = CommitGuardEnvironment(data_path=data_path)
    rng = random.Random(0)

    for _ in range(100):
        obs = env.reset()
        assert obs.budget_remaining == 5
        done = False
        steps = 0

        while not done:
            steps += 1
            # Mix in malformed actions to ensure robustness.
            if rng.random() < 0.2:
                action = CommitGuardAction(action_type="analyze", raw_action="<<<", parse_error="synthetic")
            else:
                action = CommitGuardAction(action_type=rng.choice(["analyze", "request_context", "verdict"]))  # type: ignore[arg-type]
            obs, reward, done = env.step(action)
            assert isinstance(reward, float)
            assert obs.step_idx == steps
            assert obs.budget_remaining == max(0, 5 - steps)

        assert steps <= 5