|
10 | 10 | from code_sandboxes import ExecutionResult, Logs, OutputMessage |
11 | 11 | from code_sandboxes.models import Result |
12 | 12 |
|
| 13 | +from agent_codemode.composition import executor as executor_module |
13 | 14 | from agent_codemode.composition.executor import CodeModeExecutor |
14 | 15 | from agent_codemode.discovery.registry import ToolRegistry |
15 | 16 |
|
16 | 17 |
|
17 | | -class _StreamingSandbox: |
| 18 | +class _StreamingClient: |
| 19 | + variant = "jupyter" |
| 20 | + |
18 | 21 | def __init__(self) -> None: |
19 | 22 | self.run_code_calls = 0 |
20 | 23 | self.streaming_called = False |
21 | 24 |
|
22 | | - def run_code(self, code: str, **kwargs) -> ExecutionResult: |
| 25 | + def execute_code(self, code: str, **kwargs) -> ExecutionResult: |
23 | 26 | _ = (code, kwargs.get("timeout"), kwargs.get("language"), kwargs.get("envs")) |
24 | 27 | self.run_code_calls += 1 |
25 | 28 | return ExecutionResult(logs=Logs()) |
26 | 29 |
|
27 | | - def run_code_streaming(self, code: str, **kwargs): |
| 30 | + def execute_code_streaming(self, code: str, **kwargs): |
28 | 31 | _ = (code, kwargs.get("timeout"), kwargs.get("language"), kwargs.get("envs")) |
29 | 32 | self.streaming_called = True |
30 | 33 | yield OutputMessage(line="status: RUNNING", timestamp=0.0, error=False) |
31 | 34 | yield OutputMessage(line="hello", timestamp=0.0, error=False) |
32 | 35 | yield Result(data={"text/plain": "42"}, is_main_result=True, extra={}) |
33 | 36 |
|
34 | 37 |
|
35 | | -class _NonStreamingSandbox: |
| 38 | +class _FailingStreamingClient: |
| 39 | + variant = "jupyter" |
| 40 | + |
36 | 41 | def __init__(self) -> None: |
37 | 42 | self.run_code_calls = 0 |
38 | 43 |
|
39 | | - def run_code(self, code: str, **kwargs) -> ExecutionResult: |
| 44 | + def execute_code(self, code: str, **kwargs) -> ExecutionResult: |
40 | 45 | _ = (code, kwargs.get("timeout"), kwargs.get("language"), kwargs.get("envs")) |
41 | 46 | self.run_code_calls += 1 |
42 | | - if self.run_code_calls >= 3: |
43 | | - return ExecutionResult( |
44 | | - logs=Logs(stdout=[OutputMessage(line="fallback", timestamp=0.0, error=False)]), |
45 | | - ) |
46 | 47 | return ExecutionResult(logs=Logs()) |
47 | 48 |
|
| 49 | + def execute_code_streaming(self, code: str, **kwargs): |
| 50 | + _ = (code, kwargs) |
| 51 | + raise RuntimeError("sandbox unavailable") |
| 52 | + yield |
| 53 | + |
48 | 54 |
|
49 | 55 | @pytest.mark.asyncio |
50 | | -async def test_execute_uses_streaming_when_supported(): |
| 56 | +async def test_execute_uses_streaming_when_supported(monkeypatch): |
| 57 | + monkeypatch.setattr(executor_module, "_get_identity_env", lambda: {}) |
51 | 58 | executor = CodeModeExecutor(registry=ToolRegistry()) |
52 | | - sandbox = _StreamingSandbox() |
53 | | - executor._sandbox = sandbox |
| 59 | + client = _StreamingClient() |
| 60 | + executor._sandbox_client = client |
54 | 61 | executor._setup_done = True |
55 | 62 |
|
56 | 63 | result = await executor.execute("print('hi')") |
57 | 64 |
|
58 | | - assert sandbox.streaming_called is True |
| 65 | + assert client.streaming_called is True |
59 | 66 | assert "status: RUNNING" in result.logs.stdout_text |
60 | 67 | assert "hello" in result.logs.stdout_text |
61 | 68 | assert result.results and result.results[0].data["text/plain"] == "42" |
62 | 69 |
|
63 | 70 |
|
64 | 71 | @pytest.mark.asyncio |
65 | | -async def test_execute_falls_back_to_run_code_without_streaming(): |
| 72 | +async def test_execute_reports_streaming_infrastructure_failure(monkeypatch): |
| 73 | + monkeypatch.setattr(executor_module, "_get_identity_env", lambda: {}) |
66 | 74 | executor = CodeModeExecutor(registry=ToolRegistry()) |
67 | | - sandbox = _NonStreamingSandbox() |
68 | | - executor._sandbox = sandbox |
| 75 | + client = _FailingStreamingClient() |
| 76 | + executor._sandbox_client = client |
69 | 77 | executor._setup_done = True |
70 | 78 |
|
71 | 79 | result = await executor.execute("print('hi')") |
72 | 80 |
|
73 | | - assert sandbox.run_code_calls >= 3 |
74 | | - assert result.logs.stdout_text == "fallback" |
| 81 | + assert client.run_code_calls >= 2 |
| 82 | + assert result.execution_ok is False |
| 83 | + assert result.execution_error == "sandbox unavailable" |
0 commit comments