File size: 2,160 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
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
import random
from pathlib import Path
from commitguard_env.environment import CommitGuardEnvironment
from commitguard_env.models import CommitGuardAction

def run_random_episodes(n=100):
    env = CommitGuardEnvironment(data_path=Path("data/devign_filtered.jsonl"))
    
    rewards = []
    episode_lengths = []
    
    for i in range(n):
        obs = env.reset()
        done = False
        total_reward = 0
        steps = 0
        
        while not done:
            # Randomly choose an action
            action_type = random.choice(["request_context", "analyze", "verdict"])
            
            if action_type == "request_context":
                action = CommitGuardAction(action_type="request_context", file_path="random_file.c")
            elif action_type == "analyze":
                action = CommitGuardAction(action_type="analyze", reasoning="Thinking...")
            else:
                action = CommitGuardAction(
                    action_type="verdict", 
                    is_vulnerable=random.choice([True, False]),
                    vuln_type="CWE-119",
                    exploit_sketch="Random exploit attempt"
                )
            
            obs, reward, done = env.step(action)
            total_reward += reward
            steps += 1
            
            if steps > 10: # Safety break
                break
        
        rewards.append(total_reward)
        episode_lengths.append(steps)
    
    print(f"Finished {n} episodes.")
    print(f"Average reward: {sum(rewards)/n:.4f}")
    print(f"Max reward: {max(rewards):.4f}")
    print(f"Min reward: {min(rewards):.4f}")
    print(f"Average episode length: {sum(episode_lengths)/n:.2f}")
    print(f"Max episode length: {max(episode_lengths)}")
    
    # Check distribution
    unique_rewards = set(rewards)
    print(f"Unique rewards: {len(unique_rewards)}")
    if len(unique_rewards) > 1:
        print("Reward distribution looks healthy (not all zeros).")
    else:
        print("Warning: Only one reward value found.")

if __name__ == "__main__":
    run_random_episodes(100)