File size: 11,510 Bytes
c532e4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
305
306
307
308
309
"""Episode runner — orchestrates agent + environment loop for API and WebSocket use."""

from __future__ import annotations

import asyncio
import json
import logging
import os
import time
import uuid
from pathlib import Path
from typing import Any, AsyncGenerator, Callable, Dict, List, Optional

from .api_models import StepResult, RunEpisodeResponse

logger = logging.getLogger(__name__)


def _get_model_backend():
    """Build a model backend from environment configuration."""
    api_key = os.environ.get("NVIDIA_API_KEY") or os.environ.get("OPENAI_API_KEY", "")
    base_url = os.environ.get("API_BASE_URL", "https://integrate.api.nvidia.com/v1")
    model_name = os.environ.get("MODEL_NAME", "nvidia/llama-3.1-nemotron-nano-8b-v1")
    temperature = float(os.environ.get("TEMPERATURE", "0.1"))
    max_tokens = int(os.environ.get("MAX_TOKENS", "900"))
    timeout = float(os.environ.get("REQUEST_TIMEOUT_S", "45"))

    if not api_key:
        logger.warning("No API key configured; episode runner will use mock backend")
        return None

    try:
        from openai import OpenAI
    except ImportError:
        logger.warning("openai package not installed; using mock backend")
        return None

    client = OpenAI(api_key=api_key, base_url=base_url, timeout=timeout)

    class NvidiaBackend:
        def generate(self, prompt: str, *, system_prompt: str) -> str:
            try:
                resp = client.chat.completions.create(
                    model=model_name,
                    messages=[
                        {"role": "system", "content": system_prompt},
                        {"role": "user", "content": prompt},
                    ],
                    temperature=temperature,
                    max_tokens=max_tokens,
                )
                return resp.choices[0].message.content or ""
            except Exception as exc:
                logger.error("Model backend call failed: %s", exc)
                return ""

    return NvidiaBackend()


class EpisodeRunner:
    """Runs a complete episode: reset env, loop agent steps, collect trajectory."""

    def __init__(self) -> None:
        self._active_episodes: Dict[str, Dict[str, Any]] = {}
        self._completed_episodes: Dict[str, RunEpisodeResponse] = {}

    @property
    def active_episode_ids(self) -> List[str]:
        return list(self._active_episodes.keys())

    def get_status(self, episode_id: str) -> Optional[Dict[str, Any]]:
        if episode_id in self._active_episodes:
            return self._active_episodes[episode_id]
        if episode_id in self._completed_episodes:
            resp = self._completed_episodes[episode_id]
            return {
                "episode_id": episode_id,
                "status": resp.status,
                "current_step": len(resp.steps),
                "max_steps": resp.steps[-1].step if resp.steps else 0,
                "pass_rate": resp.final_pass_rate,
                "total_reward": resp.total_reward,
                "done": True,
            }
        return None

    def get_result(self, episode_id: str) -> Optional[RunEpisodeResponse]:
        return self._completed_episodes.get(episode_id)

    def run_episode_sync(

        self,

        repo_path: str = "",

        test_identifier: str = "",

        max_steps: int = 8,

        num_runs: int = 10,

        on_step: Optional[Callable[[StepResult], None]] = None,

    ) -> RunEpisodeResponse:
        """Run a full episode synchronously. Returns completed response."""
        from .FlakeForge_environment import FlakeForgeEnvironment
        from .docker_runner import DockerTestRunner

        project_root = Path(__file__).parents[1]
        default_repo = os.environ.get(
            "FF_REPO_PATH",
            str(project_root / "test_repos" / "timing_race_minimal"),
        )
        repo_path = repo_path or default_repo
        test_identifier = test_identifier or os.environ.get(
            "FF_TEST_ID", "tests/test_flaky.py::test_fetch_should_complete"
        )

        episode_id = str(uuid.uuid4())[:8]
        self._active_episodes[episode_id] = {
            "episode_id": episode_id,
            "status": "initializing",
            "current_step": 0,
            "max_steps": max_steps,
            "pass_rate": 0.0,
            "total_reward": 0.0,
            "done": False,
        }

        try:
            runner = DockerTestRunner(repo_path)
            env = FlakeForgeEnvironment(
                repo_path=repo_path,
                test_identifier=test_identifier,
                max_steps=max_steps,
                num_runs=num_runs,
                runner=runner,
            )

            observation = env.reset(episode_id=episode_id)
            baseline_pass_rate = observation.baseline_pass_rate

            self._active_episodes[episode_id].update({
                "status": "running",
                "pass_rate": baseline_pass_rate,
            })

            backend = _get_model_backend()
            agent = None
            if backend is not None:
                try:
                    import sys
                    proj = str(Path(__file__).parents[1])
                    if proj not in sys.path:
                        sys.path.insert(0, proj)
                    from agent.unified_agent import UnifiedFlakeForgeAgent
                    agent = UnifiedFlakeForgeAgent(backend)
                except ImportError:
                    logger.warning("Could not import UnifiedFlakeForgeAgent")

            steps: List[StepResult] = []
            total_reward = 0.0
            causal_graph = observation.causal_graph

            for step_num in range(max_steps):
                if observation.done:
                    break

                pass_rate_before = observation.current_pass_rate

                if agent is not None:
                    action = agent.generate(observation)
                else:
                    from models import FlakeForgeAction
                    action = FlakeForgeAction(
                        raw_response="",
                        think_text='{"claims":[{"category":"unknown","entity":"","location":"","polarity":"present","reason":"no model backend configured"}],"confidence":0.1}',
                        patch_text="",
                        predicted_category="unknown",
                        predicted_confidence=0.1,
                    )

                observation = env.step(action)
                reward = observation.reward
                total_reward += reward

                step_result = StepResult(
                    step=step_num + 1,
                    action=action.action_type,
                    category=action.predicted_category,
                    confidence=action.predicted_confidence,
                    reward=round(reward, 4),
                    reward_breakdown=observation.reward_breakdown,
                    pass_rate_before=round(pass_rate_before, 4),
                    pass_rate_after=round(observation.current_pass_rate, 4),
                    patch_applied=bool(observation.patch_result.get("success")),
                    patch_files=observation.patch_result.get("files_modified", []),
                    think_summary=action.think_text[:200] if action.think_text else "",
                    done=observation.done,
                    done_reason=observation.done_reason,
                )
                steps.append(step_result)

                self._active_episodes[episode_id].update({
                    "current_step": step_num + 1,
                    "pass_rate": observation.current_pass_rate,
                    "total_reward": round(total_reward, 4),
                })

                if on_step:
                    on_step(step_result)

                if observation.done:
                    break

            result = RunEpisodeResponse(
                episode_id=episode_id,
                status="completed",
                steps=steps,
                total_reward=round(total_reward, 4),
                final_pass_rate=observation.current_pass_rate,
                baseline_pass_rate=baseline_pass_rate,
                done_reason=observation.done_reason or "max_steps_reached",
                causal_graph=causal_graph,
            )

        except Exception as exc:
            logger.error("Episode %s failed: %s", episode_id, exc, exc_info=True)
            result = RunEpisodeResponse(
                episode_id=episode_id,
                status="error",
                steps=[],
                total_reward=0.0,
                final_pass_rate=0.0,
                baseline_pass_rate=0.0,
                done_reason=f"error: {exc}",
            )

        self._completed_episodes[episode_id] = result
        self._active_episodes.pop(episode_id, None)
        return result

    async def run_episode_async(

        self,

        repo_path: str = "",

        test_identifier: str = "",

        max_steps: int = 8,

        num_runs: int = 10,

    ) -> RunEpisodeResponse:
        """Run episode in a thread pool to avoid blocking the event loop."""
        loop = asyncio.get_event_loop()
        return await loop.run_in_executor(
            None,
            lambda: self.run_episode_sync(
                repo_path=repo_path,
                test_identifier=test_identifier,
                max_steps=max_steps,
                num_runs=num_runs,
            ),
        )

    async def stream_episode(

        self,

        repo_path: str = "",

        test_identifier: str = "",

        max_steps: int = 8,

        num_runs: int = 10,

    ) -> AsyncGenerator[Dict[str, Any], None]:
        """Yield step-by-step results as they happen, for WebSocket streaming."""
        step_queue: asyncio.Queue[Optional[StepResult]] = asyncio.Queue()

        def on_step(result: StepResult) -> None:
            step_queue.put_nowait(result)

        loop = asyncio.get_event_loop()
        task = loop.run_in_executor(
            None,
            lambda: self.run_episode_sync(
                repo_path=repo_path,
                test_identifier=test_identifier,
                max_steps=max_steps,
                num_runs=num_runs,
                on_step=on_step,
            ),
        )

        completed = False
        while not completed:
            try:
                result = await asyncio.wait_for(step_queue.get(), timeout=120.0)
                if result is None:
                    break
                yield {"type": "step", "data": result.model_dump()}
                if result.done:
                    completed = True
            except asyncio.TimeoutError:
                yield {"type": "timeout", "data": {"message": "Step timed out"}}
                completed = True

        try:
            episode_result = await task
            yield {"type": "complete", "data": episode_result.model_dump()}
        except Exception as exc:
            yield {"type": "error", "data": {"message": str(exc)}}


# Singleton instance
_runner: Optional[EpisodeRunner] = None


def get_episode_runner() -> EpisodeRunner:
    global _runner
    if _runner is None:
        _runner = EpisodeRunner()
    return _runner