Functions to detect the current operating system and architecture.
is_apple_silicon
def is_apple_silicon()->bool:
Check if running on Apple Silicon Mac (for MPS detection).
is_linux
def is_linux()->bool:
Check if running on Linux.
is_macos
def is_macos()->bool:
Check if running on macOS.
is_windows
def is_windows()->bool:
Check if running on Windows.
# Test detection functionsprint(f"is_windows(): {is_windows()}")print(f"is_macos(): {is_macos()}")print(f"is_linux(): {is_linux()}")print(f"is_apple_silicon(): {is_apple_silicon()}")# Exactly one of these should be Trueassertsum([is_windows(), is_macos(), is_linux()]) ==1
Get current platform string for manifest filtering.
Returns strings like ‘linux-x64’, ‘darwin-arm64’, ‘win-x64’.
# Test platform stringcurrent = get_current_platform()print(f"Current platform: {current}")# Should be one of the expected formatsvalid_prefixes = ["linux-", "darwin-", "win-"]assertany(current.startswith(p) for p in valid_prefixes)
Current platform: linux-x64
Path Utilities
Functions for cross-platform path handling, particularly for conda environments.
get_python_in_env
def get_python_in_env( env_path:Path, # Path to conda environment root)->Path: # Path to Python executable
Get the Python executable path for a conda environment.
On Windows: env_path/python.exe On Unix: env_path/bin/python
def terminate_process( process:Popen, # Process to terminate (must be a session/group leader for subtree kill) timeout:float=2.0, # Seconds to wait before force kill)->None:
Terminate a subprocess + its entire process subtree (grandchildren, etc).
Session A 2026-05-27: enhanced from worker-only termination to FULL subtree termination. Workers are spawned with get_popen_isolation_kwargs() which sets start_new_session=True on Unix → the worker is its own session leader and ALL of its descendants inherit the same process-group ID (unless they setsid themselves, which is rare). os.killpg(worker_pid, SIGTERM/SIGKILL) sends the signal to every process in that group atomically — closes the orphan-grandchild bug surfaced by Voxtral-vLLM (vLLM api_server spawned its own EngineCore subprocess; pre-fix, the worker terminated cleanly but vLLM + EngineCore kept running as orphans, eating GPU memory until manual kill).
Strategy on Unix: 1. SIGTERM the worker’s process group via os.killpg (atomic). 2. Wait up to timeout for the worker to exit. 3. If anything still alive, SIGKILL the process group. 4. psutil-based safety sweep for any process that setsid-ed away from the original group (rare but possible — e.g., a poorly-isolated subprocess).
Strategy on Windows: 1. process.terminate() + wait + kill (legacy path). True process-group signaling on Windows requires Job Objects which the substrate doesn’t currently wire — Windows users are advised to avoid plugins that spawn subprocesses until that’s added. (TODO: track as substrate gap.)
terminate_self
def terminate_self()->None:
Terminate the current process (for worker suicide pact).
On Unix: Sends SIGTERM to self for graceful shutdown On Windows: Calls os._exit() since Windows lacks SIGTERM
Shell Command Execution
Cross-platform shell command execution without hardcoded shell paths.
run_shell_command
def run_shell_command( cmd:str, # Shell command to execute check:bool=True, # Whether to raise on non-zero exit capture_output:bool=False, # Whether to capture stdout/stderr kwargs:VAR_KEYWORD)->CompletedProcess: # Additional kwargs passed to subprocess.run
Run a shell command cross-platform.
Unlike using shell=True with executable=‘/bin/bash’, this function uses the platform’s default shell: - Linux/macOS: /bin/sh (or $SHELL) - Windows: cmd.exe
conda_env_exists
def conda_env_exists( env_name:str, # Name of the conda environment conda_cmd:str='conda', # Conda command (conda, mamba, micromamba))->bool:
Check if a conda environment exists (cross-platform).
Uses ‘conda env list –json’ instead of piping to grep, which doesn’t work on Windows.
# Test shell command (simple echo)if is_windows(): result = run_shell_command("echo hello", capture_output=True)else: result = run_shell_command("echo hello", capture_output=True)print(f"Return code: {result.returncode}")print(f"Output: {result.stdout}")assert result.returncode ==0
# Test conda_env_exists (will return False for non-existent env)exists = conda_env_exists("this-env-should-not-exist-12345")print(f"Non-existent env exists: {exists}")assert exists ==False# Test with base env "miniforge3" (should exist if miniforge3 is installed)base_exists = conda_env_exists("miniforge3")print(f"Base env exists: {base_exists}")
Non-existent env exists: False
Base env exists: True
Conda/Micromamba Management
Functions for managing conda and micromamba runtimes, including downloading micromamba binaries and building commands with proper prefix handling for project-local mode.
get_micromamba_download_url
def get_micromamba_download_url( platform_str:Optional=None, # Platform string (e.g., 'linux-x64'), uses current if None)->str: # Download URL for micromamba binary
Get the micromamba download URL for the specified or current platform.
download_micromamba
def download_micromamba( dest_path:Path, # Destination path for the micromamba binary platform_str:Optional=None, # Platform string, uses current if None show_progress:bool=True, # Whether to print progress messages)->bool: # True if download succeeded
Download and extract micromamba binary to the specified path.
# Test micromamba URL functionscurrent_platform = get_current_platform()url = get_micromamba_download_url()print(f"Platform: {current_platform}")print(f"Download URL: {url}")# Verify all platforms have URLsfor plat in ["linux-x64", "linux-arm64", "darwin-x64", "darwin-arm64", "win-x64"]:assert plat in MICROMAMBA_URLS, f"Missing URL for {plat}"print(f" {plat}: {MICROMAMBA_URLS[plat]}")