@@ -298,9 +298,22 @@ def __init__(
298298
299299 # Pre-approved packages (stdlib + user-specified)
300300 self .allowed_packages : set [str ] = {
301- "json" , "re" , "math" , "collections" , "itertools" ,
302- "functools" , "datetime" , "random" , "string" , "typing" ,
303- "os" , "sys" , "io" , "time" , "pathlib" , "copy" ,
301+ "json" ,
302+ "re" ,
303+ "math" ,
304+ "collections" ,
305+ "itertools" ,
306+ "functools" ,
307+ "datetime" ,
308+ "random" ,
309+ "string" ,
310+ "typing" ,
311+ "os" ,
312+ "sys" ,
313+ "io" ,
314+ "time" ,
315+ "pathlib" ,
316+ "copy" ,
304317 }
305318 if allowed_packages :
306319 self .allowed_packages .update (allowed_packages )
@@ -472,10 +485,12 @@ def _install_package(self, package: str):
472485 capture_output = True ,
473486 )
474487 self ._installed_packages .add (package )
475- self ._overhead_stats ["package_installs" ].append ({
476- "package" : package ,
477- "time_ms" : (time .perf_counter () - start ) * 1000 ,
478- })
488+ self ._overhead_stats ["package_installs" ].append (
489+ {
490+ "package" : package ,
491+ "time_ms" : (time .perf_counter () - start ) * 1000 ,
492+ }
493+ )
479494
480495 def _extract_missing_module (self , stderr : str ) -> str | None :
481496 """Extract module name from ImportError/ModuleNotFoundError."""
@@ -580,24 +595,38 @@ def _linux_sandbox_wrap(self, cmd: list[str]) -> list[str]:
580595
581596 bwrap_cmd = [
582597 "bwrap" ,
583- "--ro-bind" , "/usr" , "/usr" ,
584- "--ro-bind" , "/lib" , "/lib" ,
585- "--ro-bind" , "/bin" , "/bin" ,
586- "--ro-bind" , "/sbin" , "/sbin" ,
598+ "--ro-bind" ,
599+ "/usr" ,
600+ "/usr" ,
601+ "--ro-bind" ,
602+ "/lib" ,
603+ "/lib" ,
604+ "--ro-bind" ,
605+ "/bin" ,
606+ "/bin" ,
607+ "--ro-bind" ,
608+ "/sbin" ,
609+ "/sbin" ,
587610 ]
588611
589612 # Add /lib64 if it exists
590613 if os .path .exists ("/lib64" ):
591614 bwrap_cmd .extend (["--ro-bind" , "/lib64" , "/lib64" ])
592615
593- bwrap_cmd .extend ([
594- "--ro-bind" , self .venv_path , self .venv_path ,
595- "--bind" , self .temp_dir , self .temp_dir ,
596- "--unshare-net" ,
597- "--unshare-pid" ,
598- "--die-with-parent" ,
599- "--" ,
600- ])
616+ bwrap_cmd .extend (
617+ [
618+ "--ro-bind" ,
619+ self .venv_path ,
620+ self .venv_path ,
621+ "--bind" ,
622+ self .temp_dir ,
623+ self .temp_dir ,
624+ "--unshare-net" ,
625+ "--unshare-pid" ,
626+ "--die-with-parent" ,
627+ "--" ,
628+ ]
629+ )
601630
602631 return bwrap_cmd + cmd
603632
@@ -621,11 +650,13 @@ def execute_code(self, code: str) -> REPLResult:
621650 total_time = time .perf_counter () - total_start
622651
623652 # Track overhead
624- self ._overhead_stats ["executions" ].append ({
625- "total_ms" : total_time * 1000 ,
626- "code_ms" : (result .execution_time or 0 ) * 1000 ,
627- "overhead_ms" : (total_time - (result .execution_time or 0 )) * 1000 ,
628- })
653+ self ._overhead_stats ["executions" ].append (
654+ {
655+ "total_ms" : total_time * 1000 ,
656+ "code_ms" : (result .execution_time or 0 ) * 1000 ,
657+ "overhead_ms" : (total_time - (result .execution_time or 0 )) * 1000 ,
658+ }
659+ )
629660
630661 return result
631662
@@ -638,13 +669,15 @@ def _try_execute(self, code: str) -> REPLResult:
638669
639670 # Build environment - inherit essential vars for macOS compatibility
640671 env = os .environ .copy ()
641- env .update ({
642- "PATH" : os .path .join (self .venv_path , "bin" ) + ":/usr/bin:/bin" ,
643- "HOME" : self .temp_dir ,
644- "TMPDIR" : self .temp_dir ,
645- "PYTHONDONTWRITEBYTECODE" : "1" ,
646- "PYTHONNOUSERSITE" : "1" ,
647- })
672+ env .update (
673+ {
674+ "PATH" : os .path .join (self .venv_path , "bin" ) + ":/usr/bin:/bin" ,
675+ "HOME" : self .temp_dir ,
676+ "TMPDIR" : self .temp_dir ,
677+ "PYTHONDONTWRITEBYTECODE" : "1" ,
678+ "PYTHONNOUSERSITE" : "1" ,
679+ }
680+ )
648681
649682 try :
650683 result = subprocess .run (
@@ -720,9 +753,7 @@ def add_context(
720753 context_path = os .path .join (self .temp_dir , f"context_{ context_index } .txt" )
721754 with open (context_path , "w" ) as f :
722755 f .write (context_payload )
723- self .execute_code (
724- f"with open(r'{ context_path } ', 'r') as f:\n { var_name } = f.read()"
725- )
756+ self .execute_code (f"with open(r'{ context_path } ', 'r') as f:\n { var_name } = f.read()" )
726757 else :
727758 context_path = os .path .join (self .temp_dir , f"context_{ context_index } .json" )
728759 with open (context_path , "w" ) as f :
@@ -798,7 +829,9 @@ def get_overhead_summary(self) -> dict:
798829 "packages_installed" : [i ["package" ] for i in installs ],
799830 "overhead_percentage" : round (
800831 (total_overhead / (total_overhead + total_code_time )) * 100 , 1
801- ) if total_code_time > 0 else 0 ,
832+ )
833+ if total_code_time > 0
834+ else 0 ,
802835 }
803836
804837 def print_overhead_summary (self ):
0 commit comments