Skip to main content
Source: src/cwsandbox/_session.py:49
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, including when template_id is set, so list()/adopt can find the sandbox). Environment variables and annotations do not merge on the template path (template-owned spec).
  • profile_ids: Removed in 1.x; passing a value raises TypeError.
  • profile_names: Removed in 1.x; passing a value raises TypeError.
  • runner_ids (list[str] | None): Optional CKS runner pin (incompatible with serverless and with placement_spillover='serverless_then_cks')
  • 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 at startup. Each dict should have mount_path (str) and file_content (bytes). Note: Mounted files are read-only at runtime. To modify a file, use sandbox.write_file() after the sandbox is running.
  • s3_mount: Removed in 1.x; passing a value raises TypeError.
  • ports: Removed in 1.x; use services=[Service(...)] instead.
  • network (NetworkOptions | dict[str, Any] | None): NetworkOptions (or dict) with deny flags and optional create-time hostname grants. Port exposure uses services=.
  • file_system_snapshot (FileSystemSnapshotOptions | dict[str, Any] | None): Convenience single-mount FSS options (FileSystemSnapshotOptions or dict). Prefer volumes= for multi-volume setups.
  • max_timeout_seconds: Removed in 1.x; use request_timeout_seconds.
  • request_timeout_seconds (float | None): Client-side HTTP timeout for sandbox RPCs. Defaults to the session’s SandboxDefaults.request_timeout_seconds.
  • data_plane_mode (DataPlaneMode | str | None): Override the session’s data-plane transport policy.
  • auth (AuthConfig | None): Authentication strategy, resolved headers, or provider. Overrides the session default for this sandbox.
  • 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).
  • containers (Sequence[Container | Mapping[str, Any]] | None): Multi-container spec. Mutually exclusive with single-container kwargs. Replaces session defaults.secrets, environment_variables, security_context, and working_dir; put those on each Container. Not used by @session.function().
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 show_terminated=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): Removed in 1.x; passing a value raises TypeError.
  • profile_names (list[str] | None): Removed in 1.x; passing a value raises TypeError.
  • runner_ids (list[str] | None): Filter by runner IDs (defaults to session’s runner_ids if set)
  • volume_ids (list[str] | tuple[str, ...] | None): Filter to sandboxes attached to these registered Volume IDs
  • show_terminated (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
  • temp_dir (str | None): Override temp directory for payload/result files in sandbox. Defaults to session default. Created if missing.
  • profile_ids: Removed in 1.x; passing a value raises TypeError.
  • profile_names: Removed in 1.x; passing a value raises TypeError.
  • runner_ids (list[str] | None): Optional CKS runner pin (incompatible with serverless and with placement_spillover='serverless_then_cks')
  • 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 at startup. Each dict should have mount_path (str) and file_content (bytes). Note: Mounted files are read-only at runtime. To modify a file, use sandbox.write_file() after the sandbox is running.
  • s3_mount: Removed in 1.x; passing a value raises TypeError.
  • ports: Removed in 1.x; use services=[Service(...)] instead.
  • network (NetworkOptions | dict[str, Any] | None): NetworkOptions (or dict) with deny flags and optional create-time hostname grants. Port exposure uses services=.
  • file_system_snapshot (FileSystemSnapshotOptions | dict[str, Any] | None): Convenience single-mount FSS options (FileSystemSnapshotOptions or dict). Prefer volumes= for multi-volume setups.
  • max_timeout_seconds: Removed in 1.x; use request_timeout_seconds.
  • request_timeout_seconds (float | None): Client-side HTTP timeout for sandbox RPCs. Defaults to the session’s SandboxDefaults.request_timeout_seconds.
  • data_plane_mode (DataPlaneMode | str | None): Override the session’s data-plane transport policy.
  • 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 September 17, 2026