File size: 5,887 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
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
from __future__ import annotations

import json
import random
import uuid
from dataclasses import replace
from pathlib import Path

from .models import CommitGuardAction, CommitGuardObservation, CommitGuardState, ContextSnippet, DevignSample
from .reward import compute_reward


class CommitGuardEnvironment:
    def __init__(self, *, data_path: Path) -> None:
        self._data_path = data_path
        self._samples: list[DevignSample] = []
        self._state: CommitGuardState | None = None
        self._rng = random.Random(0)
        self._cwe_keywords: dict[str, list[str]] = {}

    def load(self) -> None:
        if self._samples:
            return
        # Load CWE keywords from data directory (matching instructions)
        try:
            kw_path = self._data_path.parent / "cwe_keywords.json"
            if not kw_path.exists():
                # Fallback to current directory or data subfolder if needed
                kw_path = self._data_path.parent / "data" / "cwe_keywords.json"
            
            self._cwe_keywords = json.loads(kw_path.read_text(encoding="utf-8"))
        except Exception:
            self._cwe_keywords = {}

        raw = self._data_path.read_text(encoding="utf-8").strip().splitlines()
        for line in raw:
            obj = json.loads(line)
            # Support both original and mvd schemas
            sample_id = str(obj.get("commit_id") or obj.get("sample_id", "unknown"))
            
            # Synthesize diff if missing (mvd branch data schema)
            diff = obj.get("diff")
            if not diff and "code_before" in obj and "code_after" in obj:
                diff = f"--- code_before\n+++ code_after\n{obj['code_before']}\n{obj['code_after']}"
            
            self._samples.append(
                DevignSample(
                    sample_id=sample_id,
                    diff=str(diff or ""),
                    available_files=list(obj.get("available_files") or []),
                    is_vulnerable=obj.get("is_vulnerable"),
                    cwe=obj.get("cwe") or obj.get("cwe_type"),
                    target_file=obj.get("target_file"),
                    files=obj.get("files"),
                )
            )
        if not self._samples:
            raise RuntimeError("no_samples_loaded")

    def reset(self, sample_id: str | None = None) -> CommitGuardObservation:
        self.load()
        if sample_id:
            sample = next((s for s in self._samples if s.sample_id == sample_id), None)
            if not sample:
                raise ValueError(f"sample_id {sample_id} not found")
        else:
            sample = self._rng.choice(self._samples)
        
        episode_id = str(uuid.uuid4())
        self._state = CommitGuardState(
            episode_id=episode_id,
            current_sample_id=sample.sample_id,
            step_count=0,
            context_requests=0,
            history=[],
        )
        return CommitGuardObservation(
            episode_id=episode_id,
            diff=sample.diff,
            available_files=sample.available_files,
            step_idx=0,
            budget_remaining=5,
        )

    def step(self, action: CommitGuardAction) -> tuple[CommitGuardObservation, float, bool]:
        if self._state is None:
            _ = self.reset()

        assert self._state is not None
        next_step = self._state.step_count + 1

        sample = next(s for s in self._samples if s.sample_id == self._state.current_sample_id)

        context_snippets: list[ContextSnippet] = []
        context_requests = self._state.context_requests
        if action.action_type == "request_context":
            context_requests += 1
            if action.file_path and sample.files and action.file_path in sample.files:
                content = sample.files[action.file_path]
                lines = content.splitlines()
                start = 1
                end = min(len(lines), 80)
                context_snippets = [
                    ContextSnippet(
                        file_path=action.file_path,
                        start_line=start,
                        end_line=end,
                        content="\n".join(lines[start - 1 : end]),
                    )
                ]

        reward = compute_reward(
            action=action,
            is_vulnerable=sample.is_vulnerable,
            cwe=sample.cwe,
            target_file=sample.target_file,
            cwe_keywords=self._cwe_keywords,
            context_requests=context_requests,
        )

        done = bool(action.action_type == "verdict" or next_step >= 5)

        self._state = replace(
            self._state,
            step_count=next_step,
            context_requests=context_requests,
            history=[
                *self._state.history,
                {
                    "step": next_step,
                    "action_type": action.action_type,
                    "parse_error": action.parse_error,
                },
            ],
        )

        obs = CommitGuardObservation(
            episode_id=self._state.episode_id,
            diff=sample.diff,
            available_files=sample.available_files,
            context_snippets=context_snippets,
            step_idx=next_step,
            budget_remaining=max(0, 5 - next_step),
            error=action.parse_error or (None if context_snippets else ("context_unavailable" if action.action_type == "request_context" else None)),
        )
        return obs, reward, done

    def state(self) -> CommitGuardState:
        if self._state is None:
            return CommitGuardState(episode_id="", current_sample_id="", step_count=0, context_requests=0, history=[])
        return self._state