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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
"""

Layer 4: Code Execution Sandbox

Part of SOVEREIGN PYTHON LLM ENGINE



Isolated subprocess execution with timeout enforcement.

Critical for ReAct and MCTS agents.

"""

import subprocess
import tempfile
import sys
import asyncio
from pathlib import Path
from dataclasses import dataclass
from datetime import datetime

from ..models.entities import CodeExecutionResult


# ==========================================
# Execution Result
# ==========================================

@dataclass
class SandboxResult:
    """Extended execution result with metadata"""
    success: bool
    output: str
    exit_code: int
    elapsed_ms: int
    stdout: str
    stderr: str
    timestamp: datetime


# ==========================================
# Code Sandbox
# ==========================================

class CodeSandbox:
    """

    Isolated subprocess code execution.



    Security features:

    - Subprocess isolation

    - Timeout enforcement

    - Output truncation

    - No network access (optional)

    - No filesystem write access (optional)

    """

    def __init__(

        self,

        timeout: float = 10.0,

        max_output_length: int = 4000,

        allowed_builtins: set[str] | None = None

    ):
        """

        Initialize code sandbox.



        Args:

            timeout: Execution timeout in seconds

            max_output_length: Max output length (truncate if exceeded)

            allowed_builtins: Set of allowed builtins (None = all allowed)

        """
        self.timeout = timeout
        self.max_output_length = max_output_length
        self.allowed_builtins = allowed_builtins

    async def execute_python(self, code: str) -> CodeExecutionResult:
        """

        Execute Python code in isolated subprocess.



        Args:

            code: Python code string



        Returns:

            CodeExecutionResult with success, output, exit code

        """
        return await asyncio.to_thread(self._sync_execute_python, code)

    def _sync_execute_python(self, code: str) -> CodeExecutionResult:
        """

        Synchronous Python execution (runs in thread pool).

        """
        import time

        start_time = time.time()

        # Create temporary file
        with tempfile.NamedTemporaryFile(
            mode='w',
            suffix='.py',
            delete=False,
            encoding='utf-8'
        ) as tmp_file:
            tmp_file.write(code)
            tmp_file_path = tmp_file.name

        try:
            # Execute in subprocess
            proc = subprocess.run(
                [sys.executable, tmp_file_path],
                capture_output=True,
                text=True,
                timeout=self.timeout
            )

            stdout = proc.stdout.strip()
            stderr = proc.stderr.strip()

            # Combine stdout/stderr
            combined = f"{stdout}\n{stderr}".strip() if stderr else stdout

            # Truncate if too long
            if len(combined) > self.max_output_length:
                combined = combined[:self.max_output_length] + "\n...[Output Truncated]"

            elapsed_ms = int((time.time() - start_time) * 1000)

            return CodeExecutionResult(
                success=(proc.returncode == 0),
                output=combined if combined else "<No output>",
                exit_code=proc.returncode,
                elapsed_ms=elapsed_ms,
                stdout=stdout,
                stderr=stderr
            )

        except subprocess.TimeoutExpired:
            elapsed_ms = int((time.time() - start_time) * 1000)

            return CodeExecutionResult(
                success=False,
                output=f"Execution timeout after {self.timeout}s",
                exit_code=-1,
                elapsed_ms=elapsed_ms,
                stdout=None,
                stderr="TimeoutExpired"
            )

        except Exception as e:
            elapsed_ms = int((time.time() - start_time) * 1000)

            return CodeExecutionResult(
                success=False,
                output=f"System error: {str(e)}",
                exit_code=-1,
                elapsed_ms=elapsed_ms,
                stdout=None,
                stderr=str(e)
            )

        finally:
            # Clean up temp file
            try:
                Path(tmp_file_path).unlink()
            except Exception:
                pass

    async def execute_bash(self, script: str) -> CodeExecutionResult:
        """

        Execute bash script in subprocess.



        Args:

            script: Bash script string



        Returns:

            CodeExecutionResult

        """
        return await asyncio.to_thread(self._sync_execute_bash, script)

    def _sync_execute_bash(self, script: str) -> CodeExecutionResult:
        """Synchronous bash execution"""
        import time

        start_time = time.time()

        # Create temporary file
        with tempfile.NamedTemporaryFile(
            mode='w',
            suffix='.sh',
            delete=False,
            encoding='utf-8'
        ) as tmp_file:
            tmp_file.write(script)
            tmp_file_path = tmp_file.name

        try:
            # Make executable
            Path(tmp_file_path).chmod(0o755)

            # Execute in subprocess
            proc = subprocess.run(
                ['bash', tmp_file_path],
                capture_output=True,
                text=True,
                timeout=self.timeout
            )

            stdout = proc.stdout.strip()
            stderr = proc.stderr.strip()
            combined = f"{stdout}\n{stderr}".strip() if stderr else stdout

            if len(combined) > self.max_output_length:
                combined = combined[:self.max_output_length] + "\n...[Truncated]"

            elapsed_ms = int((time.time() - start_time) * 1000)

            return CodeExecutionResult(
                success=(proc.returncode == 0),
                output=combined if combined else "<No output>",
                exit_code=proc.returncode,
                elapsed_ms=elapsed_ms,
                stdout=stdout,
                stderr=stderr
            )

        except subprocess.TimeoutExpired:
            elapsed_ms = int((time.time() - start_time) * 1000)

            return CodeExecutionResult(
                success=False,
                output=f"Timeout after {self.timeout}s",
                exit_code=-1,
                elapsed_ms=elapsed_ms,
                stdout=None,
                stderr="TimeoutExpired"
            )

        finally:
            try:
                Path(tmp_file_path).unlink()
            except Exception:
                pass


# ==========================================
# Restricted Sandbox (Extra Safety)
# ==========================================

class RestrictedPythonSandbox:
    """

    More restricted Python sandbox using RestrictedPython.



    Note: Requires `restrictedpython` package.

    This is a placeholder for the architecture.

    """

    def __init__(self, timeout: float = 5.0):
        self.timeout = timeout

    async def execute(self, code: str) -> CodeExecutionResult:
        """

        Execute code with RestrictedPython.



        This provides compile-time restrictions on dangerous operations:

        - No file I/O

        - No subprocess

        - No import of dangerous modules

        - No access to __builtins__

        """
        # TODO: Implement with RestrictedPython
        # For now, delegate to standard sandbox
        sandbox = CodeSandbox(timeout=self.timeout)
        return await sandbox.execute_python(code)


# ==========================================
# Sandbox Pool
# ==========================================

class SandboxPool:
    """

    Pool of sandboxes for concurrent execution.

    """

    def __init__(

        self,

        pool_size: int = 5,

        timeout: float = 10.0,

        max_output_length: int = 4000

    ):
        """

        Initialize sandbox pool.



        Args:

            pool_size: Number of sandboxes in pool

            timeout: Execution timeout

            max_output_length: Max output length

        """
        self.pool_size = pool_size
        self.timeout = timeout
        self.max_output_length = max_output_length

        self.sandboxes = [
            CodeSandbox(timeout=timeout, max_output_length=max_output_length)
            for _ in range(pool_size)
        ]

        self.semaphore = asyncio.Semaphore(pool_size)

    async def execute(self, code: str) -> CodeExecutionResult:
        """

        Execute code using pool.



        Args:

            code: Python code



        Returns:

            CodeExecutionResult

        """
        async with self.semaphore:
            # Get any available sandbox (they're all identical)
            sandbox = self.sandboxes[0]
            return await sandbox.execute_python(code)

    async def execute_batch(self, code_list: list[str]) -> list[CodeExecutionResult]:
        """

        Execute batch of code snippets concurrently.



        Args:

            code_list: List of code strings



        Returns:

            List of execution results

        """
        tasks = [self.execute(code) for code in code_list]
        return await asyncio.gather(*tasks)


# ==========================================
# Sandbox with Verification
# ==========================================

class VerifiedSandbox:
    """

    Sandbox that verifies code before execution.

    """

    def __init__(self, sandbox: CodeSandbox):
        self.sandbox = sandbox

    async def execute_with_verification(

        self,

        code: str,

        test_assertion: str

    ) -> tuple[CodeExecutionResult, bool]:
        """

        Execute code and verify with test assertion.



        Args:

            code: Code to execute

            test_assertion: Test assertion (e.g., "assert solution(5) == 10")



        Returns:

            (execution_result, test_passed)

        """
        # Append test assertion to code
        full_code = f"{code}\n\n{test_assertion}\nprint('TESTS_PASSED')"

        # Execute
        result = await self.sandbox.execute_python(full_code)

        # Check if tests passed
        test_passed = result.success and "TESTS_PASSED" in result.output

        return result, test_passed


# ==========================================
# Sandbox Statistics
# ==========================================

class SandboxStatistics:
    """

    Track sandbox execution statistics.

    """

    def __init__(self):
        self.total_executions = 0
        self.successful_executions = 0
        self.failed_executions = 0
        self.timeouts = 0
        self.total_elapsed_ms = 0

    def record(self, result: CodeExecutionResult) -> None:
        """Record execution result"""
        self.total_executions += 1

        if result.success:
            self.successful_executions += 1
        else:
            self.failed_executions += 1

        if result.exit_code == -1 and "timeout" in result.output.lower():
            self.timeouts += 1

        self.total_elapsed_ms += result.elapsed_ms

    def get_stats(self) -> dict[str, float]:
        """Get statistics"""
        return {
            "total": self.total_executions,
            "successful": self.successful_executions,
            "failed": self.failed_executions,
            "timeouts": self.timeouts,
            "success_rate": (
                self.successful_executions / self.total_executions
                if self.total_executions > 0
                else 0.0
            ),
            "avg_elapsed_ms": (
                self.total_elapsed_ms / self.total_executions
                if self.total_executions > 0
                else 0.0
            )
        }


# ==========================================
# Monitored Sandbox
# ==========================================

class MonitoredSandbox:
    """

    Sandbox with execution monitoring and statistics.

    """

    def __init__(self, sandbox: CodeSandbox):
        self.sandbox = sandbox
        self.stats = SandboxStatistics()

    async def execute(self, code: str) -> CodeExecutionResult:
        """Execute code with monitoring"""
        result = await self.sandbox.execute_python(code)
        self.stats.record(result)
        return result

    def get_statistics(self) -> dict[str, float]:
        """Get execution statistics"""
        return self.stats.get_stats()


# ==========================================
# Sandbox Security Validator
# ==========================================

class SecurityValidator:
    """

    Validate code before execution for dangerous patterns.

    """

    DANGEROUS_PATTERNS = [
        "import os",
        "import subprocess",
        "import shutil",
        "eval(",
        "exec(",
        "compile(",
        "__import__",
        "open(",
        "file(",
        "input(",
        "raw_input(",
    ]

    @staticmethod
    def is_safe(code: str) -> tuple[bool, str | None]:
        """

        Check if code is safe to execute.



        Args:

            code: Python code



        Returns:

            (is_safe, reason) tuple

        """
        for pattern in SecurityValidator.DANGEROUS_PATTERNS:
            if pattern in code:
                return False, f"Dangerous pattern detected: {pattern}"

        return True, None


class SafeSandbox:
    """

    Sandbox that validates code before execution.

    """

    def __init__(self, sandbox: CodeSandbox):
        self.sandbox = sandbox
        self.validator = SecurityValidator()

    async def execute(self, code: str) -> CodeExecutionResult:
        """

        Execute code after validation.



        Args:

            code: Python code



        Returns:

            CodeExecutionResult (or error if validation fails)

        """
        # Validate first
        is_safe, reason = self.validator.is_safe(code)

        if not is_safe:
            return CodeExecutionResult(
                success=False,
                output=f"Code validation failed: {reason}",
                exit_code=-2,
                elapsed_ms=0,
                stdout=None,
                stderr=reason
            )

        # Execute if safe
        return await self.sandbox.execute_python(code)