Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

42.4 KB
Raw Blame
# Copyright 2025 The OpenSandbox Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
BatchSandbox-based workload provider implementation.
"""

import logging
import json
import shlex
from datetime import datetime, timezone
from typing import Callable, Dict, List, Any, Optional

from opensandbox_server.config import (
    AppConfig,
    INGRESS_MODE_GATEWAY,
)
from opensandbox_server.extensions.keys import BOOTSTRAP_EXECD_ISOLATION_KEY
from opensandbox_server.services.constants import OPENSANDBOX_EGRESS_MITMPROXY_TRANSPARENT
from opensandbox_server.services.helpers import format_ingress_endpoint
from opensandbox_server.api.schema import Endpoint, ImageSpec, PlatformSpec, Volume
from opensandbox_server.services.k8s.image_pull_secret_helper import (
    build_image_pull_secret,
    build_image_pull_secret_name,
)
from opensandbox_server.services.k8s.batchsandbox_template import BatchSandboxTemplateManager
from opensandbox_server.services.k8s.client import K8sClient
from opensandbox_server.services.k8s.egress_helper import apply_egress_to_spec
from opensandbox_server.services.validators import ensure_egress_runtime_compatible
from opensandbox_server.services.k8s.provider_common import (
    DEFAULT_ENTRYPOINT,
    _build_execd_init_container,
    _build_main_container,
    _container_to_dict,
    _extract_platform_unschedulable_message_from_pod,
    _workload_platform_constraint_scope,
)
from opensandbox_server.services.k8s.status_helpers import (
    POOL_CAPACITY_EXHAUSTED_REASON,
)
from opensandbox_server.services.k8s.windows_profile import (
    apply_windows_profile_arch_selector,
    apply_windows_profile_overrides,
    is_windows_profile,
    validate_windows_profile_resource_limits,
)
from opensandbox_server.services.k8s.volume_helper import apply_volumes_to_pod_spec
from opensandbox_server.services.k8s.workload_provider import (
    EgressWorkloadSettings,
    WorkloadProvider,
)
from opensandbox_server.services.runtime_resolver import SecureRuntimeResolver

logger = logging.getLogger(__name__)


_PUBLIC_STATE_BY_PHASE = {
    "Pending": "Pending",
    "Succeed": "Running",
    "Running": "Running",
    "Pausing": "Pausing",
    "Paused": "Paused",
    "Resuming": "Resuming",
    "Failed": "Failed",
}


def _merge_security_context(
    template_sc: Dict[str, Any], runtime_sc: Dict[str, Any]
) -> Dict[str, Any]:
    """Merge the template's container securityContext into the runtime one.

    Nested dicts (capabilities, seccompProfile, ...) merge recursively so a
    template member on one key (e.g. capabilities.add) survives even when the
    runtime populates another key of the same field (e.g. capabilities.drop from
    network-policy wiring). On actual conflicting leaves, the runtime value wins.
    """
    merged = dict(template_sc)
    for key, runtime_value in runtime_sc.items():
        template_value = merged.get(key)
        if isinstance(runtime_value, dict) and isinstance(template_value, dict):
            merged[key] = _merge_security_context(template_value, runtime_value)
        else:
            merged[key] = runtime_value
    return merged


class BatchSandboxProvider(WorkloadProvider):
    def __init__(
        self,
        k8s_client: K8sClient,
        app_config: Optional[AppConfig] = None,
    ):
        self.k8s_client = k8s_client
        self.ingress_config = app_config.ingress if app_config else None

        k8s_config = app_config.kubernetes if app_config else None
        template_file_path = k8s_config.batchsandbox_template_file if k8s_config else None
        if template_file_path:
            logger.info(f"Using BatchSandbox template file: {template_file_path}")
        self.execd_init_resources = k8s_config.execd_init_resources if k8s_config else None
        self.image_pull_policy = k8s_config.image_pull_policy if k8s_config else "IfNotPresent"
        self.execd_run_as_init = bool(app_config and app_config.runtime.execd_run_as_init)

        self.resolver = SecureRuntimeResolver(app_config) if app_config else None
        self.runtime_class = (
            self.resolver.get_k8s_runtime_class() if self.resolver else None
        )

        self.group = "sandbox.opensandbox.io"
        self.version = "v1alpha1"
        self.plural = "batchsandboxes"

        self.template_manager = BatchSandboxTemplateManager(template_file_path)
        self._warn_on_restartable_template_policy()

    def supports_image_auth(self) -> bool:
        return True

    def create_workload(
        self,
        sandbox_id: str,
        namespace: str,
        image_spec: ImageSpec,
        entrypoint: List[str],
        env: Dict[str, str],
        resource_limits: Dict[str, str],
        labels: Dict[str, str],
        expires_at: Optional[datetime],
        execd_image: str,
        extensions: Optional[Dict[str, str]] = None,
        egress_settings: Optional[EgressWorkloadSettings] = None,
        volumes: Optional[List[Volume]] = None,
        platform: Optional[PlatformSpec] = None,
        annotations: Optional[Dict[str, str]] = None,
        resource_requests: Optional[Dict[str, str]] = None,
    ) -> Dict[str, Any]:
        """Create a BatchSandbox in template mode or pool mode."""
        extensions = extensions or {}
        windows_profile = is_windows_profile(platform)

        if self.runtime_class:
            logger.info(f"Using Kubernetes RuntimeClass '{self.runtime_class}' for sandbox {sandbox_id}")

        if extensions.get("poolRef"):
            if platform is not None:
                raise ValueError(
                    "platform is not supported together with extensions.poolRef yet. "
                    "Pool-level platform modeling is not available in this iteration."
                )
            if volumes:
                raise ValueError(
                    "Pool mode does not support volumes. "
                    "Remove 'volumes' from request or use template mode."
                )
            if egress_settings is not None:
                raise ValueError(
                    "Pool mode does not support networkPolicy. "
                    "Remove 'networkPolicy' from request or use template mode."
                )
            return self._create_workload_from_pool(
                batchsandbox_name=sandbox_id,
                namespace=namespace,
                labels=labels,
                pool_ref=extensions["poolRef"],
                expires_at=expires_at,
                entrypoint=entrypoint,
                env=env,
                annotations=annotations,
            )

        extra_volumes, extra_mounts, extra_security_context = self._extract_template_pod_extras()

        if windows_profile:
            validate_windows_profile_resource_limits(resource_limits)

        has_egress = egress_settings is not None
        disable_ipv6_for_egress = (
            egress_settings.disable_ipv6 if egress_settings is not None else False
        )
        init_container = _build_execd_init_container(
            execd_image,
            self.execd_init_resources,
            disable_ipv6_for_egress=disable_ipv6_for_egress,
        )
        
        main_env = dict(env)
        main_env["OPENSANDBOX_ID"] = sandbox_id
        if self.execd_run_as_init:
            main_env["EXECD_INIT"] = "1"
        if egress_settings is not None and egress_settings.credential_proxy_enabled:
            main_env[OPENSANDBOX_EGRESS_MITMPROXY_TRANSPARENT] = "true"

        main_container = _build_main_container(
            image_spec=image_spec,
            entrypoint=entrypoint,
            env=main_env,
            resource_limits=resource_limits,
            has_network_policy=has_egress,
            isolation_enabled=(extensions or {}).get(BOOTSTRAP_EXECD_ISOLATION_KEY) == "enable",
            image_pull_policy=self.image_pull_policy,
            resource_requests=resource_requests or None,
        )
        
        containers = [_container_to_dict(main_container)]
        pod_volumes = [
            {
                "name": "opensandbox-bin",
                "emptyDir": {}
            }
        ]
        if (extensions or {}).get(BOOTSTRAP_EXECD_ISOLATION_KEY) == "enable":
            pod_volumes.append({
                "name": "isolation-upper",
                "emptyDir": {}
            })
        pod_spec = {
            "automountServiceAccountToken": False,
            "initContainers": [_container_to_dict(init_container)],
            "containers": containers,
            "volumes": pod_volumes,
            # A kubelet in-place container restart recreates the container from
            # the image: the writable layer and in-memory state are wiped while
            # the sandbox keeps reporting healthy. Restarting is never a
            # meaningful recovery for a sandbox, so default to Never unless the
            # template explicitly opts into a restartable policy.
            "restartPolicy": self._template_restart_policy() or "Never",
        }
        if windows_profile:
            apply_windows_profile_overrides(
                pod_spec=pod_spec,
                entrypoint=entrypoint,
                env=env,
                resource_limits=resource_limits,
                disable_ipv6_for_egress=disable_ipv6_for_egress,
                resource_requests=resource_requests or None,
            )
            template = self.template_manager.get_base_template()
            template_spec = (
                template.get("spec", {})
                .get("template", {})
                .get("spec", {})
            )
            apply_windows_profile_arch_selector(
                pod_spec=pod_spec,
                template_spec=template_spec if isinstance(template_spec, dict) else {},
                platform=platform,
            )
        else:
            self._apply_platform_node_selector(pod_spec, platform)

        containers = pod_spec.get("containers", [])
        if self.runtime_class:
            pod_spec["runtimeClassName"] = self.runtime_class

        if image_spec.auth:
            secret_name = build_image_pull_secret_name(sandbox_id)
            pod_spec["imagePullSecrets"] = [{"name": secret_name}]

        apply_egress_to_spec(
            containers=containers,
            egress_settings=egress_settings,
            sandbox_id=sandbox_id,
            pod_volumes=pod_spec["volumes"],
        )

        if volumes:
            apply_volumes_to_pod_spec(pod_spec, volumes)

        spec: Dict[str, Any] = {
            "replicas": 1,
            "template": {
                "metadata": {
                    "labels": labels,
                    "annotations": annotations or {},
                },
                "spec": pod_spec,
            },
        }

        runtime_manifest = {
            "apiVersion": f"{self.group}/{self.version}",
            "kind": "BatchSandbox",
            "metadata": {
                "name": sandbox_id,
                "namespace": namespace,
                "labels": labels,
            },
            "spec": spec,
        }
        if annotations:
            runtime_manifest["metadata"]["annotations"] = annotations

        batchsandbox = self.template_manager.merge_with_runtime_values(runtime_manifest)
        if expires_at is None:
            batchsandbox["spec"].pop("expireTime", None)
        else:
            batchsandbox["spec"]["expireTime"] = expires_at.isoformat()
        self._merge_pod_spec_extras(
            batchsandbox, extra_volumes, extra_mounts, extra_security_context
        )
        merged_pod_spec = batchsandbox.get("spec", {}).get("template", {}).get("spec", {})
        ensure_egress_runtime_compatible(
            egress_settings.network_policy if egress_settings is not None else None,
            effective_runtime_class=merged_pod_spec.get("runtimeClassName"),
        )
        if platform is not None and not windows_profile:
            WorkloadProvider.ensure_platform_compatible_with_affinity(merged_pod_spec, platform)

        created = self.k8s_client.create_custom_object(
            group=self.group,
            version=self.version,
            namespace=namespace,
            plural=self.plural,
            body=batchsandbox,
        )

        if image_spec.auth:
            secret = build_image_pull_secret(
                sandbox_id=sandbox_id,
                image_uri=image_spec.uri,
                auth=image_spec.auth,
                owner_uid=created["metadata"]["uid"],
                owner_api_version=f"{self.group}/{self.version}",
                owner_kind="BatchSandbox",
            )
            try:
                self.k8s_client.create_secret(namespace=namespace, body=secret)
                logger.info(f"Created imagePullSecret for sandbox {sandbox_id}")
            except Exception:
                logger.warning(f"Failed to create imagePullSecret for sandbox {sandbox_id}, rolling back BatchSandbox")
                try:
                    self.k8s_client.delete_custom_object(
                        group=self.group,
                        version=self.version,
                        namespace=namespace,
                        plural=self.plural,
                        name=sandbox_id,
                        grace_period_seconds=0,
                        propagation_policy=self._deletion_propagation_policy(batchsandbox),
                    )
                except Exception as del_exc:
                    logger.warning(f"Failed to rollback BatchSandbox {sandbox_id}: {del_exc}")
                raise

        return {
            "name": created["metadata"]["name"],
            "uid": created["metadata"]["uid"],
            "apiVersion": f"{self.group}/{self.version}",
            "kind": "BatchSandbox",
        }

    def _apply_platform_node_selector(
        self,
        pod_spec: Dict[str, Any],
        platform: Optional[PlatformSpec],
    ) -> None:
        if platform is None:
            return

        template = self.template_manager.get_base_template()
        template_spec = (
            template.get("spec", {})
            .get("template", {})
            .get("spec", {})
        )
        WorkloadProvider.apply_platform_node_selector(
            pod_spec=pod_spec,
            template_spec=template_spec if isinstance(template_spec, dict) else {},
            platform=platform,
        )

    def _create_workload_from_pool(
        self,
        batchsandbox_name: str,
        namespace: str,
        labels: Dict[str, str],
        pool_ref: str,
        expires_at: Optional[datetime],
        entrypoint: List[str],
        env: Dict[str, str],
        annotations: Optional[Dict[str, str]] = None,
    ) -> Dict[str, Any]:
        """Create an interactive BatchSandbox task in an existing on-demand Pool."""
        entrypoint = entrypoint or DEFAULT_ENTRYPOINT
        spec: Dict[str, Any] = {
            "replicas": 1,
            "poolRef": pool_ref,
            "taskTemplate": self._build_task_template(
                entrypoint,
                env,
                batchsandbox_name,
            ),
        }
        if expires_at is not None:
            spec["expireTime"] = expires_at.isoformat()
        runtime_manifest = {
            "apiVersion": f"{self.group}/{self.version}",
            "kind": "BatchSandbox",
            "metadata": {
                "name": batchsandbox_name,
                "namespace": namespace,
                "labels": labels,
            },
            "spec": spec,
        }
        if annotations:
            runtime_manifest["metadata"]["annotations"] = annotations

        created = self.k8s_client.create_custom_object(
            group=self.group,
            version=self.version,
            namespace=namespace,
            plural=self.plural,
            body=runtime_manifest,
        )
        
        return {
            "name": created["metadata"]["name"],
            "uid": created["metadata"]["uid"],
            "apiVersion": f"{self.group}/{self.version}",
            "kind": "BatchSandbox",
        }

    def _template_restart_policy(self) -> Optional[str]:
        """Explicit restartPolicy from the user's BatchSandbox template, if any.

        Returned as the runtime value so it survives the runtime-wins deep
        merge; None means the template is silent and the Never default applies.
        """
        template = self.template_manager.get_base_template()
        template_spec = template.get("spec", {}).get("template", {}).get("spec", {})
        if not isinstance(template_spec, dict):
            return None
        policy = template_spec.get("restartPolicy")
        if isinstance(policy, str) and policy:
            return policy
        return None

    def _warn_on_restartable_template_policy(self) -> None:
        """Surface the in-place-restart footgun once at startup, at template load.

        A restartable policy is honored as an explicit opt-out of the Never
        default, but a kubelet container restart recreates the container from
        its image: the writable layer and in-memory state are wiped while the
        sandbox keeps reporting healthy.
        """
        policy = self._template_restart_policy()
        if policy and policy != "Never":
            logger.warning(
                "BatchSandbox template sets restartPolicy=%s: in-place container "
                "restarts will silently reset sandbox state (writable layer, "
                "in-memory data) while the sandbox keeps reporting healthy.",
                policy,
            )

    def _extract_template_pod_extras(
        self,
    ) -> tuple[list[Dict[str, Any]], list[Dict[str, Any]], Optional[Dict[str, Any]]]:
        """Extract extra template volumes, mounts, and container securityContext for runtime merge."""
        template = self.template_manager.get_base_template()
        spec = template.get("spec", {}) if isinstance(template, dict) else {}
        template_spec = spec.get("template", {}).get("spec", {})
        extra_volumes = template_spec.get("volumes", []) or []

        extra_mounts: list[Dict[str, Any]] = []
        extra_security_context: Optional[Dict[str, Any]] = None
        containers = template_spec.get("containers", []) or []
        if containers:
            target = None
            for container in containers:
                if container.get("name") == "sandbox":
                    target = container
                    break
            if target is None:
                target = containers[0]
            extra_mounts = target.get("volumeMounts", []) or []
            security_context = target.get("securityContext")
            if isinstance(security_context, dict):
                extra_security_context = security_context

        if not isinstance(extra_volumes, list):
            extra_volumes = []
        if not isinstance(extra_mounts, list):
            extra_mounts = []
        return extra_volumes, extra_mounts, extra_security_context

    def _merge_pod_spec_extras(
        self,
        batchsandbox: Dict[str, Any],
        extra_volumes: list[Dict[str, Any]],
        extra_mounts: list[Dict[str, Any]],
        extra_security_context: Optional[Dict[str, Any]] = None,
    ) -> None:
        """Merge template-provided volumes, mounts, and securityContext into runtime pod spec."""
        try:
            spec = batchsandbox["spec"]["template"]["spec"]
        except KeyError:
            return

        volumes = spec.get("volumes", []) or []
        if isinstance(volumes, list) and extra_volumes:
            existing = {v.get("name") for v in volumes if isinstance(v, dict)}
            for vol in extra_volumes:
                if not isinstance(vol, dict):
                    continue
                name = vol.get("name")
                if not name or name in existing:
                    continue
                volumes.append(vol)
                existing.add(name)
            spec["volumes"] = volumes

        containers = spec.get("containers", []) or []
        if not containers or not isinstance(containers, list):
            return
        main_container = containers[0]
        if extra_security_context and isinstance(main_container, dict):
            # The template's container securityContext is a base default: merge it
            # into the runtime container's own securityContext (runtime leaves win,
            # nested dicts merge so template members like capabilities.add survive),
            # and fill the whole context when the runtime sets none.
            runtime_security_context = main_container.get("securityContext")
            if isinstance(runtime_security_context, dict):
                main_container["securityContext"] = _merge_security_context(
                    extra_security_context, runtime_security_context
                )
            else:
                main_container["securityContext"] = extra_security_context
        mounts = main_container.get("volumeMounts", []) or []
        if isinstance(mounts, list) and extra_mounts:
            existing = {m.get("name") for m in mounts if isinstance(m, dict)}
            for mnt in extra_mounts:
                if not isinstance(mnt, dict):
                    continue
                name = mnt.get("name")
                if not name or name in existing:
                    continue
                mounts.append(mnt)
                existing.add(name)
            main_container["volumeMounts"] = mounts

    def _build_task_template(
        self,
        entrypoint: List[str],
        env: Dict[str, str],
        sandbox_id: str,
    ) -> Dict[str, Any]:
        """Build pool taskTemplate with shell-escaped bootstrap command.

        The task shell always execs bootstrap.sh so task-executor keeps the
        task alive until bootstrap exits during sandbox cleanup. With
        execd_run_as_init enabled, bootstrap execs `execd --init` (the
        EXECD_INIT env is injected below), so execd becomes the root of the
        task process tree. Without it, bootstrap supervises execd and the
        user process directly.
        """
        escaped_entrypoint = ' '.join(shlex.quote(arg) for arg in entrypoint)
        # Keep bootstrap as task-executor's direct child in both process
        # topologies, otherwise its shell writes a successful exit marker
        # before the sandbox has been cleaned up.
        user_process_cmd = f"exec /opt/opensandbox/bootstrap.sh {escaped_entrypoint}"

        wrapped_command = ["/bin/sh", "-c", user_process_cmd]

        if self.execd_run_as_init:
            env = {**env, "EXECD_INIT": "1"}
        env_list = [{"name": k, "value": v} for k, v in env.items()] if env else []
        env_list.append({"name": "OPENSANDBOX_ID", "value": sandbox_id})

        return {
            "spec": {
                "process": {
                    "command": wrapped_command,
                    "env": env_list,
                }
            }
        }


    def subscribe_workload(
        self, sandbox_id: str, namespace: str, callback: Callable[[str, Dict[str, Any]], None]
    ) -> Optional[Callable[[], None]]:
        return self.k8s_client.subscribe_custom_objects(
            group=self.group,
            version=self.version,
            namespace=namespace,
            plural=self.plural,
            names=[sandbox_id, self.legacy_resource_name(sandbox_id)],
            callback=callback,
        )

    def get_workload(self, sandbox_id: str, namespace: str) -> Optional[Dict[str, Any]]:
        workload = self.k8s_client.get_custom_object(
            group=self.group,
            version=self.version,
            namespace=namespace,
            plural=self.plural,
            name=sandbox_id,
        )
        if workload:
            return workload

        legacy_name = self.legacy_resource_name(sandbox_id)
        if legacy_name != sandbox_id:
            return self.k8s_client.get_custom_object(
                group=self.group,
                version=self.version,
                namespace=namespace,
                plural=self.plural,
                name=legacy_name,
            )

        return None
    
    @staticmethod
    def _deletion_propagation_policy(batchsandbox: Dict[str, Any]) -> str:
        # Keep executor and main containers alive until the task cleanup finalizer
        # stops tasks and hooks, then deletes owned pods and waits for their removal.
        return "Background" if batchsandbox.get("spec", {}).get("taskTemplate") is not None else "Foreground"

    def delete_workload(self, sandbox_id: str, namespace: str) -> None:
        batchsandbox = self.get_workload(sandbox_id, namespace)
        if not batchsandbox:
            raise Exception(f"BatchSandbox for sandbox {sandbox_id} not found")

        self.k8s_client.delete_custom_object(
            group=self.group,
            version=self.version,
            namespace=namespace,
            plural=self.plural,
            name=batchsandbox["metadata"]["name"],
            grace_period_seconds=0,
            propagation_policy=self._deletion_propagation_policy(batchsandbox),
        )

    def list_workloads(self, namespace: str, label_selector: str) -> List[Dict[str, Any]]:
        return self.k8s_client.list_custom_objects(
            group=self.group,
            version=self.version,
            namespace=namespace,
            plural=self.plural,
            label_selector=label_selector,
        )

    def list_workloads_all_namespaces(self, label_selector: str) -> List[Dict[str, Any]]:
        """List BatchSandboxes across all namespaces matching the label selector."""
        return self.k8s_client.list_custom_objects_all_namespaces(
            group=self.group,
            version=self.version,
            plural=self.plural,
            label_selector=label_selector,
        )

    def patch_workload(self, sandbox_id: str, namespace: str, spec_patch: Dict[str, Any]) -> Dict[str, Any]:
        """Patch BatchSandbox spec (e.g., spec.pause for pause/resume)."""
        batchsandbox = self.get_workload(sandbox_id, namespace)
        if not batchsandbox:
            return None
        return self.k8s_client.patch_custom_object(
            group=self.group,
            version=self.version,
            namespace=namespace,
            plural=self.plural,
            name=batchsandbox["metadata"]["name"],
            body=spec_patch,
        )

    @staticmethod
    def _has_true_condition(conditions: List[Dict[str, Any]], condition_type: str) -> bool:
        for cond in conditions:
            if cond.get("type") == condition_type and cond.get("status") == "True":
                return True
        return False

    @staticmethod
    def _first_true_condition_message(conditions: List[Dict[str, Any]], condition_types: List[str]) -> Optional[str]:
        for condition_type in condition_types:
            for cond in conditions:
                if cond.get("type") == condition_type and cond.get("status") == "True":
                    message = cond.get("message")
                    if isinstance(message, str) and message.strip():
                        return message
        return None

    def _patch_pause_with_retry_bridge(self, sandbox_id: str, namespace: str, target: Optional[bool]) -> None:
        self.patch_workload(sandbox_id, namespace, {"spec": {"pause": None}})
        try:
            self.patch_workload(sandbox_id, namespace, {"spec": {"pause": target}})
            return
        except Exception as exc:
            current = self.get_workload(sandbox_id, namespace)
            current_pause = None if not current else current.get("spec", {}).get("pause")
            if current is not None and current_pause == target:
                logger.warning(
                    f"BatchSandbox {sandbox_id} retry bridge target patch raised "
                    f"{type(exc).__name__} but read-back confirmed spec.pause={target}"
                )
                return

            logger.warning(
                f"BatchSandbox {sandbox_id} retry bridge target patch raised "
                f"{type(exc).__name__} and current spec.pause={current_pause}; "
                "retrying target patch once"
            )
            retried = self.patch_workload(sandbox_id, namespace, {"spec": {"pause": target}})
            if retried is None:
                raise exc

    def update_expiration(self, sandbox_id: str, namespace: str, expires_at: datetime) -> None:
        batchsandbox = self.get_workload(sandbox_id, namespace)
        if not batchsandbox:
            raise Exception(f"BatchSandbox for sandbox {sandbox_id} not found")

        body = {
            "spec": {
                "expireTime": expires_at.isoformat()
            }
        }
        
        self.k8s_client.patch_custom_object(
            group=self.group,
            version=self.version,
            namespace=namespace,
            plural=self.plural,
            name=batchsandbox["metadata"]["name"],
            body=body,
        )

    def pause_sandbox(self, sandbox_id: str, namespace: str) -> None:
        """Pause a BatchSandbox by patching spec.pause=true.

        Validates that the current status.phase allows pause:
        - Succeed: allowed (fresh pause)
        - Succeed + PauseFailed=True: allowed (retry after failure, server internally patches nil->true)
        - Pausing/Resuming: not allowed (operation in progress)
        - Paused: not allowed (already paused)
        - Failed: not allowed (sandbox unavailable)
        - Failed + PauseFailed=True: not allowed (sandbox unavailable)
        """
        batchsandbox = self.get_workload(sandbox_id, namespace)
        if not batchsandbox:
            raise ValueError(f"Sandbox '{sandbox_id}' not found")

        status = batchsandbox.get("status", {})
        phase = status.get("phase", "")
        conditions = status.get("conditions", [])

        pause_failed = self._has_true_condition(conditions, "PauseFailed")

        if phase == "Succeed":
            pass
        elif phase == "Pausing":
            raise ValueError(f"Cannot pause: operation in progress (phase={phase})")
        elif phase == "Resuming":
            raise ValueError(f"Cannot pause: operation in progress (phase={phase})")
        elif phase == "Paused":
            raise ValueError("Sandbox is already paused")
        elif phase == "Failed":
            if pause_failed:
                raise ValueError("Cannot pause: sandbox is not available (pause caused pod loss)")
            else:
                raise ValueError("Cannot pause: sandbox is not available")
        elif phase == "Pending":
            raise ValueError(f"Cannot pause: sandbox is being created (phase={phase})")
        else:
            raise ValueError(f"Cannot pause sandbox in phase {phase}")

        if pause_failed:
            self._patch_pause_with_retry_bridge(sandbox_id, namespace, True)
            logger.info(f"Patched BatchSandbox {sandbox_id} retry bridge spec.pause=nil->true")
        else:
            self.patch_workload(sandbox_id, namespace, {"spec": {"pause": True}})
            logger.info(f"Patched BatchSandbox {sandbox_id} spec.pause=true")

    def resume_sandbox(self, sandbox_id: str, namespace: str) -> None:
        """Resume a BatchSandbox by patching spec.pause=false.

        Validates that the current status.phase allows resume:
        - Paused: allowed (fresh resume)
        - Paused + ResumeFailed=True: allowed (retry after failure, server internally patches nil->false)
        - Resuming/Pausing: not allowed (operation in progress)
        - Succeed: not allowed (not paused)
        - Failed: not allowed (sandbox unavailable)
        """
        batchsandbox = self.get_workload(sandbox_id, namespace)
        if not batchsandbox:
            raise ValueError(f"Sandbox '{sandbox_id}' not found")

        status = batchsandbox.get("status", {})
        phase = status.get("phase", "")
        conditions = status.get("conditions", [])

        resume_failed = self._has_true_condition(conditions, "ResumeFailed")

        # Allow resume when Paused (or Paused with ResumeFailed for retry)
        if phase == "Paused":
            # Always allowed, even if ResumeFailed=True (retry scenario)
            pass
        elif phase == "Resuming":
            raise ValueError(f"Cannot resume: operation in progress (phase={phase})")
        elif phase == "Pausing":
            raise ValueError(f"Cannot resume: operation in progress (phase={phase})")
        elif phase == "Succeed":
            public_state = _PUBLIC_STATE_BY_PHASE[phase]
            raise ValueError(
                f"Cannot resume sandbox in state {public_state}, expected Paused"
            )
        elif phase == "Failed":
            if resume_failed:
                raise ValueError("Cannot resume: sandbox is not available (resume caused pod start failure)")
            else:
                raise ValueError("Cannot resume: sandbox is not available")
        elif phase == "Pending":
            raise ValueError(f"Cannot resume: sandbox is being created (phase={phase})")
        else:
            raise ValueError(f"Cannot resume sandbox in phase {phase}, expected Paused")

        if resume_failed:
            self._patch_pause_with_retry_bridge(sandbox_id, namespace, False)
            logger.info(f"Patched BatchSandbox {sandbox_id} retry bridge spec.pause=nil->false")
        else:
            self.patch_workload(sandbox_id, namespace, {"spec": {"pause": False}})
            logger.info(f"Patched BatchSandbox {sandbox_id} spec.pause=false")

    def get_expiration(self, workload: Dict[str, Any]) -> Optional[datetime]:
        """Parse expiration timestamp from `spec.expireTime`."""
        spec = workload.get("spec", {})
        expire_time_str = spec.get("expireTime")
        
        if not expire_time_str:
            return None
        
        try:
            return datetime.fromisoformat(expire_time_str.replace('Z', '+00:00'))
        except (ValueError, TypeError) as e:
            logger.warning(f"Invalid expireTime format: {expire_time_str}, error: {e}")
            return None

    def _expired_status(self, workload: Dict[str, Any]) -> Optional[Dict[str, Any]]:
        """Running is pod readiness, not TTL. A past expireTime must leave Running.

        last_transition_at is the expireTime itself so wall-clock billing stops
        at the configured timeout instead of creation time or "now".
        """
        expire_time_str = (workload.get("spec") or {}).get("expireTime")
        expires_at = self.get_expiration(workload)
        if not expire_time_str or expires_at is None:
            return None
        if expires_at.tzinfo is None:
            expires_at = expires_at.replace(tzinfo=timezone.utc)
        if expires_at > datetime.now(timezone.utc):
            return None
        return {
            "state": "Terminated",
            "reason": "SANDBOX_EXPIRED",
            "message": "Sandbox expired",
            "last_transition_at": expire_time_str,
        }

    def _parse_pod_ip(self, workload: Dict[str, Any]) -> Optional[str]:
        """Parse first pod IP from endpoints annotation."""
        annotations = workload.get("metadata", {}).get("annotations", {})
        endpoints_str = annotations.get("sandbox.opensandbox.io/endpoints")
        if not endpoints_str:
            return None
        try:
            endpoints = json.loads(endpoints_str)
            if endpoints and len(endpoints) > 0:
                return endpoints[0]
        except (json.JSONDecodeError, IndexError, TypeError):
            pass
        return None

    def _platform_unschedulable_message_from_selector(self, workload: Dict[str, Any]) -> Optional[str]:
        workload_has_platform_constraints, workload_has_non_platform_constraints = _workload_platform_constraint_scope(
            workload,
            "template",
            self.analyze_platform_constraints_in_pod_spec,
        )
        if not workload_has_platform_constraints:
            return None
        status = workload.get("status", {})
        selector = status.get("selector")
        namespace = workload.get("metadata", {}).get("namespace")
        if not selector or not namespace:
            return None
        try:
            pods = self.k8s_client.list_pods(
                namespace=namespace,
                label_selector=selector,
            )
        except Exception:
            return None

        for pod in pods:
            message = _extract_platform_unschedulable_message_from_pod(
                pod,
                workload_has_platform_constraints,
                workload_has_non_platform_constraints,
                self.is_platform_unschedulable,
            )
            if message:
                return message
        return None

    def get_status(self, workload: Dict[str, Any]) -> Dict[str, Any]:
        """Derive sandbox state from BatchSandbox status and pod readiness."""
        metadata = workload.get("metadata", {})
        deletion_timestamp = metadata.get("deletionTimestamp")
        if deletion_timestamp:
            return {
                "state": "Stopping",
                "reason": "DELETING",
                "message": "Sandbox is being deleted",
                "last_transition_at": deletion_timestamp,
            }

        status = workload.get("status", {})
        creation_timestamp = metadata.get("creationTimestamp")

        # Phase is authoritative when set (Pausing/Paused/Resuming/Failed)
        phase = status.get("phase", "")
        failed_message = self._first_true_condition_message(
            status.get("conditions", []),
            ["PodFailed", "ResumeFailed", "PauseFailed"],
        )
        phase_map = {
            "Pending": ("CREATING", "Sandbox is being created"),
            "Succeed": ("RUNNING", "Sandbox is running"),
            "Running": ("RUNNING", "Sandbox is running"),
            "Pausing": ("PAUSING", "Pausing sandbox"),
            "Paused": ("PAUSED", "Sandbox is paused"),
            "Resuming": ("RESUMING", "Resuming sandbox"),
            "Failed": ("FAILED", failed_message or "Operation failed"),
        }
        # A failed task must not be reported as a healthy sandbox. Neither signal below can
        # see it: `ready` counts Ready PODS, and a Pool pod is Ready before any sandbox is
        # dispatched to it (that is what pre-warming means), while the Succeed phase is
        # derived from `Ready > 0` alone. Without this check a sandbox whose entrypoint never
        # ran is indistinguishable from a healthy one over the API.
        #
        # Lifecycle phases stay authoritative: Pausing/Paused/Resuming are driven by an
        # explicit user operation, and Failed already reports itself with a better message.
        task_failed = int(status.get("taskFailed") or 0)
        if task_failed > 0 and phase not in ("Pausing", "Paused", "Resuming", "Failed"):
            return {
                "state": "Failed",
                "reason": "TASK_FAILED",
                "message": f"{task_failed} sandbox task(s) failed",
                "last_transition_at": creation_timestamp,
            }

        if phase in ("Succeed", "Running"):
            expired = self._expired_status(workload)
            if expired is not None:
                return expired

        if phase in phase_map and phase != "Pending":
            reason, message = phase_map[phase]
            return {
                "state": _PUBLIC_STATE_BY_PHASE[phase],
                "reason": reason,
                "message": message,
                "last_transition_at": creation_timestamp,
            }

        conditions = status.get("conditions", [])
        if self._has_true_condition(conditions, "PoolAllocationPending"):
            pool_capacity_message = self._first_true_condition_message(
                conditions,
                ["PoolAllocationPending"],
            ) or "Pool capacity is currently unavailable"
            return {
                "state": "Pending",
                "reason": POOL_CAPACITY_EXHAUSTED_REASON,
                "message": pool_capacity_message,
                "last_transition_at": creation_timestamp,
            }

        if phase == "Pending":
            reason, message = phase_map[phase]
            return {
                "state": _PUBLIC_STATE_BY_PHASE[phase],
                "reason": reason,
                "message": message,
                "last_transition_at": creation_timestamp,
            }

        # Fallback: derive from pod state
        replicas = status.get("replicas", 0)
        ready = status.get("ready", 0)
        allocated = status.get("allocated", 0)
        pod_ip = self._parse_pod_ip(workload)

        if pod_ip:
            expired = self._expired_status(workload)
            if expired is not None:
                return expired

        if ready == 1 and pod_ip:
            state = "Running"
            reason = "POD_READY_WITH_IP"
            message = f"Pod is ready with IP ({ready}/{replicas} ready)"
        elif pod_ip:
            state = "Allocated"
            reason = "IP_ASSIGNED"
            message = f"Pod has IP assigned but not ready ({allocated}/{replicas} allocated, {ready} ready)"
        else:
            unschedulable_message = self._platform_unschedulable_message_from_selector(workload)
            if unschedulable_message:
                state = "Failed"
                reason = "POD_PLATFORM_UNSCHEDULABLE"
                message = unschedulable_message
            else:
                state = "Pending"
                reason = "POD_SCHEDULED" if allocated > 0 else "BATCHSANDBOX_PENDING"
                message = (
                    f"Pod is scheduled but waiting for IP ({allocated}/{replicas} allocated, {ready} ready)"
                    if allocated > 0
                    else "BatchSandbox is pending allocation"
                )

        return {
            "state": state,
            "reason": reason,
            "message": message,
            "last_transition_at": creation_timestamp,
        }
    
    def get_internal_endpoint(
        self, workload: Dict[str, Any], port: int, sandbox_id: str
    ) -> Optional[Endpoint]:
        """Resolve the internal endpoint from the BatchSandbox annotation."""
        pod_ip = self._parse_pod_ip(workload)
        if not pod_ip:
            return None
        return Endpoint(endpoint=f"{pod_ip}:{port}")

    def get_endpoint_info(self, workload: Dict[str, Any], port: int, sandbox_id: str) -> Optional[Endpoint]:
        """Resolve endpoint using gateway ingress or parsed pod IP."""
        if self.ingress_config and self.ingress_config.mode == INGRESS_MODE_GATEWAY:
            return format_ingress_endpoint(self.ingress_config, sandbox_id, port)

        return self.get_internal_endpoint(workload, port, sandbox_id)