hylo.codeagents.sandbox¶
Package: hylo-code-agents · Core contracts
sandbox ¶
A restricted namespace for model-written Python (RFC-0006: "programs are data until gated").
Every block is parsed and checked against an allow-list before it runs
- imports: only
mathandnumpy(and numpy.linalg / random / fft) - no names or attributes that start with an underscore (no dunders, no private toolkit state)
- no open / exec / eval / compile / globals / getattr / ... (they are not in the builtins either)
- no frame, generator or code-object internals, no numpy file I/O
- no class definitions, no async code
Builtins are a small allow-list; print writes to a buffer the agent returns to the model. The namespace persists
between blocks (one per episode or research session). Each block has a wall-clock limit that excludes the time the
robot spends executing (paused(), used around Executor.run): on expiry an exception is raised inside the running
thread (CPython async exception), so a busy loop stops; a call stuck inside C code stops when it returns.
This is a guard against mistakes and casual escapes by a model, not an OS-level jail: run untrusted models in a container as well. Memory use is not limited.
ALLOWED_IMPORTS
module-attribute
¶
DENIED_NAMES
module-attribute
¶
DENIED_NAMES = {'open', 'exec', 'eval', 'compile', 'globals', 'locals', 'vars', 'getattr', 'setattr', 'delattr', 'hasattr', 'input', 'breakpoint', 'help', 'exit', 'quit', 'memoryview', 'type', 'object', 'super', 'dir', 'classmethod', 'staticmethod', 'property', '__import__', '__builtins__', 'importlib', 'os', 'sys', 'subprocess'}
DENIED_ATTRS
module-attribute
¶
DENIED_ATTRS = {'gi_frame', 'gi_code', 'gi_yieldfrom', 'cr_frame', 'cr_code', 'cr_await', 'ag_frame', 'ag_code', 'f_globals', 'f_locals', 'f_builtins', 'f_back', 'f_code', 'tb_frame', 'tb_next', 'co_code', 'co_consts', 'func_globals', 'func_code', 'mro', 'tofile', 'dump', 'dumps', 'load', 'loads', 'save', 'savez', 'savez_compressed', 'savetxt', 'loadtxt', 'genfromtxt', 'fromfile', 'fromregex', 'memmap', 'ctypes', 'ctypeslib', 'lib', 'f2py', 'distutils', 'testing', 'DataSource', 'core', 'system', 'popen'}
SAFE_BUILTINS
module-attribute
¶
SAFE_BUILTINS = ('abs', 'all', 'any', 'bool', 'callable', 'chr', 'dict', 'divmod', 'enumerate', 'filter', 'float', 'format', 'frozenset', 'hash', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'list', 'map', 'max', 'min', 'next', 'ord', 'pow', 'range', 'repr', 'reversed', 'round', 'set', 'slice', 'sorted', 'str', 'sum', 'tuple', 'zip', 'ArithmeticError', 'AssertionError', 'Exception', 'IndexError', 'KeyError', 'LookupError', 'NotImplementedError', 'RuntimeError', 'StopIteration', 'TypeError', 'ValueError', 'ZeroDivisionError')
CodeRejected ¶
Bases: ValueError
The source failed validation; nothing ran.
ProgramTimeout ¶
Bases: BaseException
Raised inside a block that ran past its wall-clock limit (BaseException: except Exception cannot eat it).
ProgramCancelled ¶
Bases: BaseException
Raised inside a block whose episode or session is over.
CodeChecker ¶
Bases: NodeVisitor
The AST allow-list check behind validate(); subclasses widen or narrow it (scene programs).
BlockResult
dataclass
¶
BlockResult(output: str = '', error: str | None = None, value: Any = None, seconds: float = 0.0, timed_out: bool = False, cancelled: bool = False, rejected: bool = False)
Sandbox ¶
Sandbox(names: dict[str, Any] | None = None, *, block_timeout_s: float = 60.0, max_output: int = 6000)
A persistent restricted namespace. names are what programs see (robot, finish, skills, ...).
add ¶
Make names visible to programs (this namespace, and fresh ones made by define).
paused ¶
Stop the block's clock (and the watchdog) while the robot executes.
run ¶
Validate and run one block in the persistent namespace. A final bare expression is printed (REPL style).
program_source ¶
A self-contained program from the blocks run so far: the imports, function definitions and constant
assignments made at the top level of any block (the latest definition of each name), in the order last
defined. None if no block defined entry. Statements that act (calls, loops, other assignments) are left
out, so submitting it does not replay an experiment.
call ¶
call(fn: Callable[..., Any], *args: Any, timeout_s: float | None = None, **kwargs: Any) -> BlockResult
Call a function defined in this namespace under the time limit (code.program runs solve(robot)).
define ¶
Validate source, run its definitions (in a fresh namespace with this one's builtins, print and initial
names, unless fresh=False) and return the function name. Raises CodeRejected. Not time-limited by itself:
call it inside call or a block.
validate ¶
Parse and check a program; raises CodeRejected listing every problem found.