Skip to content

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

fix(k8s): inject the guest lifecycle env only when hooks are present

高然e5263af726b2
diff --git a/server/opensandbox_server/services/k8s/create_helpers.py b/server/opensandbox_server/services/k8s/create_helpers.py
index 8d07a511e..2e268a94c 100644
--- a/server/opensandbox_server/services/k8s/create_helpers.py
+++ b/server/opensandbox_server/services/k8s/create_helpers.py
@@ -95,8 +95,10 @@ def _build_create_workload_context(
resource_requests = request.resource_requests.root
sandbox_env, egress_env = split_egress_env(request.env)
- if request.lifecycle is not None:
- # idlePolicy/renewalPolicy are control-plane state; never reach execd.
+ if request.lifecycle is not None and request.lifecycle.has_hooks:
+ # Transport hooks only. idlePolicy/renewalPolicy are control-plane
+ # state; an empty payload would still overwrite hooks persisted in
+ # the image/snapshot, so policy-only lifecycles inject nothing.
sandbox_env[OPENSANDBOX_LIFECYCLE] = request.lifecycle.model_dump_json(
by_alias=True,
exclude_none=True,
diff --git a/server/tests/k8s/test_lifecycle_hooks.py b/server/tests/k8s/test_lifecycle_hooks.py
index 0a0de4a75..bcb9b6971 100644
--- a/server/tests/k8s/test_lifecycle_hooks.py
+++ b/server/tests/k8s/test_lifecycle_hooks.py
@@ -113,3 +113,25 @@ def test_create_context_never_transports_renewal_policy_to_execd(
)
assert "renewalPolicy" not in json.loads(context.sandbox_env[OPENSANDBOX_LIFECYCLE])
+
+
+def test_create_context_skips_lifecycle_env_for_policy_only_lifecycle(
+ k8s_app_config,
+ create_sandbox_request,
+):
+ """A renewal-only lifecycle must not inject OPENSANDBOX_LIFECYCLE: an
+ empty payload would overwrite hooks persisted in the image/snapshot."""
+ create_sandbox_request.lifecycle = SandboxLifecycle(
+ renewalPolicy={"extendSeconds": 3600},
+ )
+
+ context = _build_create_workload_context(
+ k8s_app_config,
+ create_sandbox_request,
+ "sandbox-1",
+ datetime.now(timezone.utc),
+ lambda: "egress-token",
+ lambda: "secure-token",
+ )
+
+ assert OPENSANDBOX_LIFECYCLE not in context.sandbox_env