sandbox
sandbox
¶
High-level sandbox facade composing shield, gate, runtime, and SSH.
Convenience composition layer — delegates to the existing module-level
functions. Callers can also use those functions directly; this class
simply groups them behind a shared :class:SandboxConfig.
READY_MARKER = '>> init complete'
module-attribute
¶
Default log line emitted by init-ssh-and-repo.sh when the container is ready.
LifecycleHooks(pre_start=None, post_start=None, post_ready=None, post_stop=None)
dataclass
¶
Optional callbacks fired at container lifecycle transitions.
All slots are None by default. Sandbox.run() fires pre_start
before podman run and post_start after a successful launch.
post_ready and post_stop are available for callers to invoke at
the appropriate time (e.g. after log streaming or container exit).
pre_start = None
class-attribute
instance-attribute
¶
Fired before podman run.
post_start = None
class-attribute
instance-attribute
¶
Fired after a successful podman run.
post_ready = None
class-attribute
instance-attribute
¶
Fired when the container reports ready (caller responsibility).
post_stop = None
class-attribute
instance-attribute
¶
Fired after the container exits (caller responsibility).
RunSpec(container_name, image, env, volumes, command, task_dir, gpu_enabled=False, extra_args=(), unrestricted=True)
dataclass
¶
Everything needed for a single podman run invocation.
container_name
instance-attribute
¶
Unique container name.
image
instance-attribute
¶
Image tag to run (e.g. terok-l1-cli:ubuntu-24.04).
env
instance-attribute
¶
Environment variables injected into the container.
volumes
instance-attribute
¶
Volume mount strings (host:container[:opts]).
command
instance-attribute
¶
Command to execute inside the container.
task_dir
instance-attribute
¶
Host-side task directory (for shield state, logs, etc.).
gpu_enabled = False
class-attribute
instance-attribute
¶
Whether to pass GPU device args to podman.
extra_args = ()
class-attribute
instance-attribute
¶
Additional podman run arguments (e.g. port publishing).
unrestricted = True
class-attribute
instance-attribute
¶
When False, adds --security-opt no-new-privileges.
Sandbox(config=None)
¶
Stateless facade composing sandbox primitives.
All methods delegate to the module-level functions in this package,
passing the stored :class:SandboxConfig. The existing function-level
API remains the canonical interface — this class is a convenience for
callers that manage a config instance.
Source code in src/terok_sandbox/sandbox.py
config
property
¶
Return the sandbox configuration.
ensure_gate()
¶
Verify the gate server is running; raise SystemExit if not.
create_token(project_id, task_id)
¶
Create a task-scoped gate access token.
gate_url(repo_path, token)
¶
Build an HTTP URL for gate access to repo_path.
Source code in src/terok_sandbox/sandbox.py
gate_status()
¶
pre_start_args(container, task_dir)
¶
Return extra podman args for shield integration.
shield_down(container, task_dir)
¶
Remove shield rules for a container (allow all egress).
run(spec, *, hooks=None)
¶
Launch a detached container from spec.
Assembles and executes the podman run command, handling user
namespace mapping, shield or bypass networking, GPU device args,
environment and volume injection, CDI error detection, and lifecycle
hook callbacks.
Fires hooks.pre_start before podman run and hooks.post_start
after a successful launch. Raises :class:~.runtime.GpuConfigError
when the launch fails due to NVIDIA CDI misconfiguration.
Source code in src/terok_sandbox/sandbox.py
stream_logs(container, *, timeout=None, ready_check=None)
¶
Stream container logs until ready_check matches or timeout.
Source code in src/terok_sandbox/sandbox.py
wait_for_exit(container, timeout=None)
¶
Block until container exits; return exit code.