Looking at your code, I'm impressed with how cleanly you've implemented the sandboxing integration! You're absolutely right that this could be easily modularized. Here are the key insights I see from your implementation:
What Makes This Modular
-
Clean Separation: Your sandboxing logic is contained in just a few key areas:
- Import handling (lines 17-50)
_execute_jailed()functionsetup_jail_config()function- Simple conditional in
exec_proc_agent()
-
Configuration-Driven: The
jail_configparameter flows through your execution chain without the core logic needing to know the internals -
Graceful Degradation: When RestrictedPython isn't available, it falls back cleanly
Modularization Strategy
You could extract this into a sandbox_module.py:## Integration Points
With this modular approach, your core system would only need these minimal changes:
-
Import the module:
from sandbox_module import create_sandbox_config, execute_sandboxed_function, SandboxError -
Replace your jail setup:
def setup_jail_config(args): return create_sandbox_config( jail_dir=getattr(args, 'jail_dir', None), jail_user=getattr(args, 'jail_user', 'agent_worker') ) -
Replace your jailed execution:
# In exec_proc_agent, replace _execute_jailed call with: try: result, status = execute_sandboxed_function( function_name, function_def, updated_step_params, jail_config ) except SandboxError as e: result, status = b'', {"status": {"value": 1, "reason": str(e)}}
Benefits of This Approach
- Plugin Architecture: Easy to add new sandbox backends (Docker, chroot, etc.)
- Zero Dependencies: Core system doesn't need RestrictedPython installed
- Testable: Each backend can be tested independently
- Configurable: Each backend can have its own configuration schema
- Fallback Strategy: Could add a "no-op" backend for development
The beauty of your original implementation is that it already has the right abstraction boundaries - the jail_config parameter flows cleanly through without coupling the core logic to the sandboxing implementation. This modular version just formalizes that separation!
No comments:
Post a Comment