File size: 14,939 Bytes
24c2665
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import traceback
from typing import List, Tuple
import ast
import time
import requests
import docker
from docker.errors import DockerException
import socket

import numpy as np
from pebble import ProcessPool
from sandbox_fusion import run_code, RunCodeRequest, set_endpoint, RunStatus

from absolute_zero_reasoner.utils.code_utils.templates import (
    RUN_CODE_TEMPLATE_REPR,
    EVAL_INPUT_PREDICTION_TEMPLATE_REPR,
    EVAL_OUTPUT_PREDICTION_TEMPLATE_REPR,
    VALIDATE_CODE_TEMPLATE_REPR,
    CHECK_DETERMINISM_TEMPLATE_REPR,
    EVAL_K_INPUT_PREDICTION_TEMPLATE,
    EVAL_K_OUTPUT_PREDICTION_TEMPLATE,
)
from absolute_zero_reasoner.utils.code_utils.checks import contains_banned_imports
from absolute_zero_reasoner.utils.code_utils.parsers import parse_error


# Docker images
IMAGES = {
    'global': 'volcengine/sandbox-fusion:server-20250609',
    'china': 'vemlp-cn-beijing.cr.volces.com/preset-images/code-sandbox:server-20250609'
}
class DockerAPIRunner:
    def __init__(self, use_china_mirror=True, silent=False):
        self.image = IMAGES['china'] if use_china_mirror else IMAGES['global']
        self.container = None
        self.silent = silent
        self.client = docker.from_env()
        self.port = self._find_free_port()
    
    def _find_free_port(self):
        """Find an available port dynamically"""
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.bind(('', 0))
            s.listen(1)
            port = s.getsockname()[1]
        return port
    
    def start(self):
        """Start the Docker container using Docker API"""
        try:
            # Pull image if not exists
            if not self.silent:
                print(f"Pulling image: {self.image}")
            self.client.images.pull(self.image)
            
            # Run container
            self.container = self.client.containers.run(
                self.image,
                ports={'8080/tcp': self.port},
                detach=True,
                remove=True  # Auto-remove when stopped
            )
            
            if not self.silent:
                print(f"Container started: {self.container.short_id}")
            return True
            
        except DockerException as e:
            if not self.silent:
                print(f"Error starting container: {e}")
            return False
    
    def stop(self):
        """Stop the Docker container"""
        if self.container:
            try:
                self.container.stop()
                if not self.silent:
                    print("Container stopped")
                return True
            except DockerException as e:
                if not self.silent:
                    print(f"Error stopping container: {e}")
                return False
        return False
    
    def _wait_for_container_ready(self, max_wait_time: int = 60, check_interval: float = 1.0):
        """Wait for the Docker container to be ready"""
        if not self.container:
            raise Exception("Container not started")
        
        start_time = time.time()
        while time.time() - start_time < max_wait_time:
            # Reload container status
            self.container.reload()
            
            if not self.silent:
                print(f"Container status: {self.container.status}")
            
            if self.container.status == 'running':
                # Container is running, now check if service is ready
                # First try a simple port connection test
                try:
                    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                    sock.settimeout(2)
                    result = sock.connect_ex(('localhost', self.port))
                    sock.close()
                    
                    if result == 0:  # Port is open
                        # Try to make a simple request to test the service
                        try:
                            response = requests.get(f'http://localhost:{self.port}/', timeout=2)
                            if not self.silent:
                                print(f"Service responded with status: {response.status_code}")
                            return True  # Service is responding
                        except requests.exceptions.RequestException:
                            # Try alternative endpoints or just accept that port is open
                            if not self.silent:
                                print(f"Port {self.port} is open, assuming service is ready")
                            return True
                except:
                    pass
            elif self.container.status in ['exited', 'dead']:
                # Get container logs for debugging
                logs = self.container.logs().decode('utf-8')
                raise Exception(f"Container failed to start. Status: {self.container.status}. Logs: {logs[:500]}")
            
            time.sleep(check_interval)
        
        # Get final container logs for debugging
        logs = self.container.logs().decode('utf-8') if self.container else "No container"
        raise Exception(f"Container not ready after {max_wait_time} seconds. Final status: {self.container.status if self.container else 'None'}. Logs: {logs[:500]}")


class SandboxfusionExecutor:
    def __init__(
        self,
        timeout_length: int = 10,
        ast_check: bool = False,
        max_workers: int = 1,
        use_china_mirror: bool = True,
    ) -> None:
        self.runner = DockerAPIRunner(use_china_mirror=use_china_mirror)
        running = self.runner.start()
        if not running:
            raise Exception("Failed to start Sandboxfusion Docker container")
        
        # Wait for the container to be ready
        self._wait_for_container_ready()
        set_endpoint(f'http://localhost:{self.runner.port}')
        
        self.timeout_length = timeout_length
        self.ast_check = ast_check
        self.max_workers = max_workers

    def _wait_for_container_ready(self, max_wait_time: int = 60, check_interval: float = 1.0):
        """Wait for the Docker container to be ready"""
        self.runner._wait_for_container_ready(max_wait_time, check_interval)

    def __del__(self):
        try:
            self.cleanup()
            self.runner.stop()
        except Exception as e:
            print(f"Error terminating pool: {e}")
            pass

    def cleanup(self):
        self.runner.stop()

    def process_generation_to_code(self, gens: str):
        return [g.strip().split('\n') for g in gens]
    
    def run_code(self, code: str, inputs: str, imports: List[str] = []) -> Tuple[str, str]:
        if isinstance(imports, np.ndarray):
            imports = imports.tolist()
        if imports:
            code = '\n'.join(imports) + '\n' + code
        code_snippet = RUN_CODE_TEMPLATE_REPR.format(code=code, inputs=inputs)
        # print(code_snippet)
        if self.ast_check:
            try:
                ast.parse(code_snippet)
            except:
                return '', 'error'
        return self.apply(code_snippet)

    def validate_code(self, code: str, inputs: str, imports: List[str] = []) -> bool:
        if isinstance(imports, np.ndarray):
            imports = imports.tolist()
        if imports:
            code = '\n'.join(imports) + '\n' + code
        code_snippet = VALIDATE_CODE_TEMPLATE_REPR.format(code=code, inputs=inputs)
        if self.ast_check:
            try:
                ast.parse(code_snippet)
            except:
                return False
        _, status = self.apply(code_snippet)
        return not 'error' in status.lower()

    def eval_input_prediction(self, code: str, gold_output: str, agent_input: str, imports: List[str] = []) -> float:
        if isinstance(imports, np.ndarray):
            imports = imports.tolist()
        if imports:
            code = '\n'.join(imports) + '\n' + code
        code_snippet = EVAL_INPUT_PREDICTION_TEMPLATE_REPR.format(code=code, gold_output=gold_output, agent_input=agent_input)
        if self.ast_check:
            try:
                ast.parse(code_snippet)
            except:
                return 0.0
        max_retries = 3
        for retry in range(max_retries):
            try:
                correct, status = self.apply(code_snippet)
                return 0.0 if 'error' in status.lower() or not eval(correct) else 1.0
            except Exception as e:
                if retry == max_retries - 1:
                    error_details = traceback.format_exc()
                    print(f"Error in eval_input_prediction: {e}\n{error_details}")
                    return
                time.sleep(0.1 * (retry + 1))  # Exponential backoff

    def eval_output_prediction(self, code: str, gold_output: str, agent_output: str, imports: List[str] = []) -> float:
        try: # fast check if we dont need to run the code
            if eval(gold_output) == eval(agent_output):
                return 1.0
        except:
            pass
        if isinstance(imports, np.ndarray):
            imports = imports.tolist()
        if imports:
            code = '\n'.join(imports) + '\n' + code
        code_snippet = EVAL_OUTPUT_PREDICTION_TEMPLATE_REPR.format(code=code, gold_output=gold_output, agent_output=agent_output)
        if self.ast_check:
            try:
                ast.parse(code_snippet)
            except:
                return 0.0
        max_retries = 3
        for retry in range(max_retries):
            try:
                correct, status = self.apply(code_snippet)
                return 0.0 if 'error' in status.lower() or not eval(correct) else 1.0
            except Exception as e:
                if retry == max_retries - 1:
                    error_details = traceback.format_exc()
                    print(f"Error in eval_output_prediction: {e}\n{error_details}")
                    return
                time.sleep(0.1 * (retry + 1))  # Exponential backoff

    def eval_k_input_prediction(self, code: str, gold_output: str, k_agent_inputs: List[str], imports: List[str] = []) -> List[float]:
        if isinstance(imports, np.ndarray):
            imports = imports.tolist()
        if imports:
            code = '\n'.join(imports) + '\n' + code
        invalid_lists = []
        valid_k_agent_inputs = []
        for k_agent_input in k_agent_inputs:
            try:
                ast.parse(f'f({k_agent_input})')
                valid_k_agent_inputs.append(k_agent_input)
            except:
                invalid_lists.append(0.0)
        acc_list, status = self.apply(EVAL_K_INPUT_PREDICTION_TEMPLATE(code=code, gold_output=gold_output, k_agent_inputs=valid_k_agent_inputs, repr_output=True))
        assert 'error' not in status.lower()
        output_acc = eval(acc_list) + invalid_lists
        assert len(output_acc) == len(k_agent_inputs)
        return output_acc

    def eval_k_output_prediction(self, code: str, gold_output: str, k_agent_outputs: List[str], imports: List[str] = []) -> List[float]:
        if isinstance(imports, np.ndarray):
            imports = imports.tolist()
        if imports:
            code = '\n'.join(imports) + '\n' + code
        invalid_lists = []
        valid_k_agent_outputs = []
        for k_agent_output in k_agent_outputs:
            try:
                if k_agent_output != '':
                    ast.parse(f'f({k_agent_output})')
                    valid_k_agent_outputs.append(k_agent_output)
                else:
                    invalid_lists.append(0.0)
            except:
                invalid_lists.append(0.0)
        acc_list, status = self.apply(EVAL_K_OUTPUT_PREDICTION_TEMPLATE(code=code, gold_output=gold_output, k_agent_outputs=valid_k_agent_outputs, repr_output=True))
        assert 'error' not in status.lower()
        output_acc = eval(acc_list) + invalid_lists
        assert len(output_acc) == len(k_agent_outputs)
        return output_acc

    def check_all(
        self,
        code: str,
        inputs: str,
        banned_keywords: List[str] = [],
        check_determinism: bool = True,
        imports: List[str] = [],
        check_error: bool = False,
        banned_keywords_for_errors_and_exceptions: List[str] = [],
    ) -> Tuple[bool, str]:
        if isinstance(imports, np.ndarray):
            imports = imports.tolist()
        if imports:
            code = '\n'.join(imports) + '\n' + code
        if contains_banned_imports(code=code, banned_keywords=banned_keywords, banned_keywords_for_errors_and_exceptions=banned_keywords_for_errors_and_exceptions if check_error else []):
            return False, None
        if check_error:
            code_snippet = RUN_CODE_TEMPLATE_REPR.format(code=code, inputs=inputs)
            try:
                ast.parse(code_snippet)
            except:
                return False, 'error'
            output, status = self.apply(code_snippet)
            if check_determinism: # run the code again, see if outputs are same
                output_2, status_2 = self.apply(code_snippet)
                if status_2.lower() != status.lower() and output != output_2:
                    return False, 'error'
            # True if the code is valid code but might have error, output no error if the code returns something
            return True, 'NoError' if status.lower() == 'done' else parse_error(status)
        else:
            if check_determinism:
                code_snippet = CHECK_DETERMINISM_TEMPLATE_REPR.format(code=code, inputs=inputs)
            else:
                code_snippet = RUN_CODE_TEMPLATE_REPR.format(code=code, inputs=inputs)
            if self.ast_check:
                try:
                    ast.parse(code_snippet)
                except:
                    return False, 'error'
            output, status = self.apply(code_snippet)
            return not 'error' in status.lower(), output

    def apply(self, code) -> Tuple[str, str]:
        try:
            response = run_code(
                RunCodeRequest(
                    code=code,
                    language='python',
                    compile_timeout=self.timeout_length,
                    run_timeout=self.timeout_length,
                )
            )
            if response.status == RunStatus.Success:
                # taking [1:-1] to exclude prefix space and suffix newline
                return response.run_result.stdout.split('<FINAL_REPR_SYMBOL>')[-1][1:-1], 'done'
            else:
                return '', 'error'

        except Exception as e:
            error_msg = f"Execution error: {str(e)}"
            return error_msg, 'error'


def _test():
    batch_code = [
"""
def f(a):
    return a
print('<FINAL_REPR_SYMBOL>', repr(f(12eee)))
"""
    ]

    executor = SandboxfusionExecutor()
    predictions = executor.apply(batch_code[0])
    print(predictions)


if __name__ == '__main__':
    _test()