Skip to main content
Source: src/cwsandbox/_session.py:38
Manages sandbox lifecycle and provides function execution. Use a session when:
  • Creating multiple sandboxes with shared configuration
  • Executing Python functions in sandboxes
  • You want automatic cleanup of orphaned sandboxes
Metrics are automatically tracked when exec() completes on any sandbox associated with this session. Use log_metrics(step=N) to log metrics at specific training steps.

Properties

sandbox_count

Number of sandboxes currently tracked by this session.

Methods

log_metrics

Log accumulated sandbox metrics to wandb. Call this during training to correlate sandbox usage with training steps. Metrics are automatically tracked when exec() completes, so users only need to call log_metrics() for step correlation. Metrics are also logged automatically on session close. Parameters
  • step (int | None): Training step to associate with metrics. If provided, metrics are logged at this step number in wandb.
  • reset (bool): If True (default), reset accumulated metrics after a successful log. Metrics are preserved if log() fails (no active wandb run). Set to False to keep accumulating regardless.
Returns
  • bool: True if metrics were logged, False if no reporter configured
  • bool: or no active wandb run.
Examples

get_metrics

Get current accumulated metrics. Returns
  • dict[str, Any]: Dictionary with cwsandbox/* prefixed metric names and values.
  • dict[str, Any]: Empty dict if no reporter is configured.

close

Stop all managed sandboxes, return OperationRef immediately. Returns
  • OperationRef[None]: OperationRef[None]: Use .result() to block until all sandboxes stopped.
Raises
  • SandboxError: If one or more running sandboxes failed to stop.
Examples

sandbox

Create an unstarted sandbox with session defaults. Returns immediately without any network calls. The sandbox auto-starts on first operation (exec, read_file, write_file, wait), or can be started explicitly with start().result(). Parameters
  • command (str | None): Command to run in sandbox
  • args (list[str] | None): Arguments for the command
  • container_image (str | None): Container image to use
  • tags (list[str] | None): Tags for the sandbox (merged with session defaults)
  • profile_ids (list[str] | None): Optional list of profile IDs for infrastructure selection. See SandboxDefaults.profile_ids for semantics. Prefer profile_names when selecting by name.
  • profile_names (list[str] | None): Optional list of profile names for infrastructure selection (preferred over profile_ids). See SandboxDefaults.profile_names for semantics.
  • runner_ids (list[str] | None): Optional list of runner IDs
  • resources (ResourceOptions | dict[str, Any] | None): Resource configuration. Accepts ResourceOptions for separate requests/limits, or a flat dict for backward-compatible Guaranteed QoS.
  • mounted_files (list[dict[str, Any]] | None): Files to mount into the sandbox
  • s3_mount (dict[str, Any] | None): S3 bucket mount configuration
  • ports (list[dict[str, Any]] | None): Port mappings for the sandbox
  • network (NetworkOptions | dict[str, Any] | None): Network configuration (NetworkOptions dataclass)
  • max_timeout_seconds (int | None): Maximum timeout for sandbox operations
  • environment_variables (dict[str, str] | None): Environment variables to inject into the sandbox. Merges with and overrides matching keys from the session defaults. Use for non-sensitive config only.
  • annotations (dict[str, str] | None): Kubernetes pod annotations for the sandbox. Merges with and overrides matching keys from the session defaults. Use for non-sensitive metadata only.
  • secrets (Sequence[Secret | dict[str, Any]] | None): Secrets to inject as environment variables. Merged with session defaults (defaults first, then this list).
Returns
  • Sandbox: An unstarted Sandbox registered with the session.
Raises
  • SandboxError: If the session has been closed.
Examples

list

List sandboxes, optionally adopting them into this session. Automatically includes the session’s default tags in the filter. This makes it easy to find sandboxes created by this session or a previous run with the same defaults. By default, only active (non-terminal) sandboxes are returned. Set include_stopped=True to widen the search to include terminal sandboxes (completed, failed, terminated). A terminal status filter (e.g. status="completed") also widens the search automatically. Parameters
  • tags (list[str] | None): Additional tags to filter by (merged with session’s default tags)
  • status (str | None): Filter by status
  • profile_ids (list[str] | None): Optional list of profile IDs for infrastructure selection (defaults to session’s profile_ids if set). See SandboxDefaults.profile_ids for semantics. Prefer profile_names when selecting by name.
  • profile_names (list[str] | None): Optional list of profile names for infrastructure selection (preferred over profile_ids). Defaults to session’s profile_names if set. See SandboxDefaults.profile_names for semantics.
  • runner_ids (list[str] | None): Filter by runner IDs (defaults to session’s runner_ids if set)
  • include_stopped (bool): If True, include terminal sandboxes (completed, failed, terminated). Defaults to False.
  • adopt (bool): If True, register discovered sandboxes with this session so they are stopped when the session closes
Returns
  • OperationRef[list[Sandbox]]: OperationRef[list[Sandbox]]: Use .result() to block for results,
  • OperationRef[list[Sandbox]]: or await directly in async contexts.
Examples

from_id

Attach to an existing sandbox, optionally adopting it into this session. Parameters
  • sandbox_id (str): The ID of the existing sandbox
  • adopt (bool): If True (default), register the sandbox with this session
Returns
  • OperationRef[Sandbox]: OperationRef[Sandbox]: Use .result() to block for the Sandbox instance,
  • OperationRef[Sandbox]: or await directly in async contexts.
Examples

adopt

Adopt an existing Sandbox instance into this session for cleanup tracking. Use this when you have a Sandbox from Sandbox.list() or Sandbox.from_id() that you want to be automatically stopped when the session closes. Parameters
  • sandbox (Sandbox): A Sandbox instance to track
Raises
  • SandboxError: If the session is closed
  • ValueError: If the sandbox has no sandbox_id
Examples

function

Decorator to execute a Python function in a sandbox. Each function call creates an ephemeral sandbox, executes the function, and returns the result. The sandbox is automatically cleaned up. The decorated function must be synchronous. Async functions are not supported. Parameters
  • container_image (str | None): Override session’s default image for this function
  • serialization (Serialization): How to serialize arguments and return values. Defaults to JSON for safety. Use PICKLE for complex types, but only in trusted environments.
  • temp_dir (str | None): Override temp directory for payload/result files in sandbox. Defaults to session default. Created if missing.
  • profile_ids (list[str] | None): Optional list of profile IDs for infrastructure selection. See SandboxDefaults.profile_ids for semantics. Prefer profile_names when selecting by name.
  • profile_names (list[str] | None): Optional list of profile names for infrastructure selection (preferred over profile_ids). See SandboxDefaults.profile_names for semantics.
  • runner_ids (list[str] | None): Optional list of runner IDs
  • resources (ResourceOptions | dict[str, Any] | None): Resource configuration. Accepts ResourceOptions for separate requests/limits, or a flat dict for backward-compatible Guaranteed QoS.
  • mounted_files (Sequence[dict[str, Any]] | None): Files to mount into the sandbox
  • s3_mount (dict[str, Any] | None): S3 bucket mount configuration
  • ports (Sequence[dict[str, Any]] | None): Port mappings for the sandbox
  • network (NetworkOptions | dict[str, Any] | None): Network configuration (NetworkOptions dataclass)
  • max_timeout_seconds (int | None): Maximum timeout for sandbox operations
  • environment_variables (dict[str, str] | None): Environment variables to inject into the sandbox. Merges with and overrides matching keys from the session defaults. Use for non-sensitive config only.
  • annotations (dict[str, str] | None): Kubernetes pod annotations for the sandbox. Merges with and overrides matching keys from the session defaults. Use for non-sensitive metadata only.
Returns
  • Callable[[Callable[P, R]], RemoteFunction[P, R]]: A decorator that wraps a function as a RemoteFunction
Examples
Last modified on May 19, 2026