Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(tasks): default standard cloud runs to the VM base image (#72592)

Alessandro Pogliaghi3434fd277d52
diff --git a/docs/published/handbook/engineering/ai/sandboxed-agents.md b/docs/published/handbook/engineering/ai/sandboxed-agents.md
index d0cd3318a078..3b31d17d966e 100644
--- a/docs/published/handbook/engineering/ai/sandboxed-agents.md
+++ b/docs/published/handbook/engineering/ai/sandboxed-agents.md
@@ -261,6 +261,31 @@ Agents should stage changes with `git add`, then use the `git_signed_commit` too
| Image | `ghcr.io/posthog/posthog-sandbox-base` | Local Dockerfile build |
| Auth | Modal connect token | No token needed |
+### Runtime selection (gVisor vs Modal VM)
+
+Production sandboxes run on one of two Modal runtimes,
+chosen per run in `get_task_processing_context` (`_is_modal_vm_sandbox_enabled`)
+and forked in `provision_sandbox`:
+
+- **gVisor** (`SandboxTemplate.DEFAULT_BASE`) — the historical default: a gVisor kernel-sandboxed container.
+- **Modal VM** (`SandboxTemplate.VM_BASE`) — a kernel microVM that also bakes in Docker-in-Docker,
+ so the agent can run nested containers.
+ Custom base images layer on this base, and it is what image-builder runs execute on.
+
+Selection is driven by the `tasks-modal-vm-sandbox` flag's JSON payload,
+which carries two origin allowlists:
+
+- `origin_products` — origins allowed on the VM runtime when a custom image is resolved for the run
+ (custom images cannot run under gVisor).
+- `default_base_origin_products` — origins that default to the bare VM base image **even without a custom image**.
+ This is the knob for making the VM runtime the default for standard cloud runs;
+ we widen it origin-by-origin (and, later, the flag's release condition) as the rollout expands.
+
+Runs with a restricted-egress `SandboxEnvironment` (a custom domain allowlist) always stay on gVisor —
+Modal's outbound domain allowlist is a gVisor-only feature.
+The `use_modal_vm_sandbox` run-state key force-selects the VM runtime for trusted server-created runs
+(image builders) and is never accepted from client input.
+
### Network access
Network access is configured per-team via `SandboxEnvironment`:
diff --git a/products/tasks/backend/constants.py b/products/tasks/backend/constants.py
index 5a6cf748d1a0..8dc2f7548933 100644
--- a/products/tasks/backend/constants.py
+++ b/products/tasks/backend/constants.py
@@ -22,9 +22,28 @@ def vm_sandbox_allowed_origin_products(payload: object) -> set[str]:
return set()
-def vm_sandbox_allowed_origins(*, distinct_id: str, organization_id: str) -> set[str]:
- """Allowed origin products from the VM-sandbox flag; empty when off (payload only resolves on match)."""
- payload = posthoganalytics.get_feature_flag_payload(
+def vm_sandbox_default_base_origin_products(payload: object) -> set[str]:
+ """Origins allowed to run on the bare VM *base* image even without a custom image.
+
+ This is the knob that makes the VM runtime the default for standard cloud runs: an
+ origin listed here provisions on `SandboxTemplate.VM_BASE` instead of the gVisor
+ default base, without requiring the user to pick a custom image. Read only from the
+ explicit dict key — unlike `origin_products`, a bare-list payload keeps its legacy
+ `origin_products` meaning and never opts an origin into the default base."""
+ if isinstance(payload, str):
+ try:
+ payload = json.loads(payload)
+ except (ValueError, TypeError):
+ payload = None
+ value = payload.get("default_base_origin_products") if isinstance(payload, dict) else None
+ if isinstance(value, list) and all(isinstance(item, str) for item in value):
+ return {item for item in value if isinstance(item, str)}
+ return set()
+
+
+def get_vm_sandbox_flag_payload(*, distinct_id: str, organization_id: str) -> object:
+ """Raw payload of the Modal VM-sandbox flag; resolves to None when the flag is off."""
+ return posthoganalytics.get_feature_flag_payload(
MODAL_VM_SANDBOX_FEATURE_FLAG,
distinct_id=distinct_id,
groups={"organization": organization_id},
@@ -32,7 +51,13 @@ def vm_sandbox_allowed_origins(*, distinct_id: str, organization_id: str) -> set
only_evaluate_locally=False,
send_feature_flag_events=False,
)
- return vm_sandbox_allowed_origin_products(payload)
+
+
+def vm_sandbox_allowed_origins(*, distinct_id: str, organization_id: str) -> set[str]:
+ """Allowed origin products from the VM-sandbox flag; empty when off (payload only resolves on match)."""
+ return vm_sandbox_allowed_origin_products(
+ get_vm_sandbox_flag_payload(distinct_id=distinct_id, organization_id=organization_id)
+ )
MAX_CUSTOM_IMAGES_PER_TEAM = 20
diff --git a/products/tasks/backend/temporal/process_task/activities/get_task_processing_context.py b/products/tasks/backend/temporal/process_task/activities/get_task_processing_context.py
index aae2600ce4ff..b38f4dd2111e 100644
--- a/products/tasks/backend/temporal/process_task/activities/get_task_processing_context.py
+++ b/products/tasks/backend/temporal/process_task/activities/get_task_processing_context.py
@@ -18,7 +18,9 @@ from products.tasks.backend.constants import (
OVERLAP_CLONE_BOOT_FEATURE_FLAG,
RTK_DISABLED_FEATURE_FLAG,
SANDBOX_EVENT_INGEST_FEATURE_FLAG,
- vm_sandbox_allowed_origins,
+ get_vm_sandbox_flag_payload,
+ vm_sandbox_allowed_origin_products,
+ vm_sandbox_default_base_origin_products,
)
from products.tasks.backend.exceptions import TaskInvalidStateError, TaskRunNotReadyError
from products.tasks.backend.logic.services.sandbox_config import (
@@ -378,7 +380,38 @@ def _is_modal_vm_sandbox_enabled(
)
return False
- if origin_product != Task.OriginProduct.IMAGE_BUILDER and not custom_image_available:
+ # A trusted, server-set per-run override (image builders) forces the runtime
+ # decision without consulting the flag; any non-bool value is ignored.
+ raw_state_override = (state or {}).get("use_modal_vm_sandbox")
+ state_override: bool | None = raw_state_override if isinstance(raw_state_override, bool) else None
+
+ # Resolve the flag payload once and derive both allowlists from it, skipping the
+ # fetch entirely when a bool state override is present (so image-builder runs never
+ # depend on the flag service):
+ # - allowed_origins: origins permitted on the VM runtime (custom images are VM-only).
+ # - default_base_origins: origins that may run on the bare VM *base* image even
+ # without a custom image — makes the VM runtime the default for standard runs.
+ allowed_origins: set[str] = set()
+ default_base_origins: set[str] = set()
+ if state_override is None:
+ try:
+ payload = get_vm_sandbox_flag_payload(distinct_id=distinct_id, organization_id=organization_id)
+ except Exception as e:
+ log_with_activity_context("modal_vm_sandbox_flag_check_failed", run_id=run_id, error=str(e))
+ return False
+ allowed_origins = vm_sandbox_allowed_origin_products(payload)
+ default_base_origins = vm_sandbox_default_base_origin_products(payload)
+
+ origin_allows_default_base = origin_product in default_base_origins
+
+ # Custom images are VM-only, so VM historically required one. image_builder always
+ # runs on VM (it builds images on that base); origins in the default-base allowlist
+ # may run on the bare VM base image with no custom image at all.
+ if (
+ origin_product != Task.OriginProduct.IMAGE_BUILDER
+ and not custom_image_available
+ and not origin_allows_default_base
+ ):
log_with_activity_context(
"modal_vm_sandbox_skipped_without_custom_image",
run_id=run_id,
@@ -386,8 +419,7 @@ def _is_modal_vm_sandbox_enabled(
)
return False
- state_override = (state or {}).get("use_modal_vm_sandbox")
- if isinstance(state_override, bool):
+ if state_override is not None:
log_with_activity_context(
"modal_vm_sandbox_state_override",
run_id=run_id,
@@ -395,20 +427,14 @@ def _is_modal_vm_sandbox_enabled(
)
return state_override
- try:
- allowed_origins = vm_sandbox_allowed_origins(distinct_id=distinct_id, organization_id=organization_id)
- except Exception as e:
- log_with_activity_context("modal_vm_sandbox_flag_check_failed", run_id=run_id, error=str(e))
- return False
-
- origin_allowed = origin_product in allowed_origins
- result = origin_allowed
+ result = origin_product in allowed_origins or origin_allows_default_base
log_with_activity_context(
"modal_vm_sandbox_flag_checked",
run_id=run_id,
- flag_enabled=bool(allowed_origins),
+ flag_enabled=bool(allowed_origins or default_base_origins),
origin_product=origin_product,
allowed_origin_products=sorted(allowed_origins),
+ default_base_origin_products=sorted(default_base_origins),
custom_image_available=custom_image_available,
use_modal_vm_sandbox=result,
)
diff --git a/products/tasks/backend/temporal/process_task/activities/tests/test_get_task_processing_context.py b/products/tasks/backend/temporal/process_task/activities/tests/test_get_task_processing_context.py
index 4ad44e04e10b..6424664bb0ee 100644
--- a/products/tasks/backend/temporal/process_task/activities/tests/test_get_task_processing_context.py
+++ b/products/tasks/backend/temporal/process_task/activities/tests/test_get_task_processing_context.py
@@ -17,6 +17,7 @@ from products.tasks.backend.constants import (
RTK_DISABLED_FEATURE_FLAG,
SANDBOX_EVENT_INGEST_FEATURE_FLAG,
vm_sandbox_allowed_origin_products,
+ vm_sandbox_default_base_origin_products,
)
from products.tasks.backend.exceptions import TaskInvalidStateError, TaskRunNotReadyError
from products.tasks.backend.models import SandboxEnvironment, Task
@@ -767,6 +768,49 @@ class TestGetTaskProcessingContextActivity:
payload_mock.assert_not_called()
+ def test_modal_vm_sandbox_restricted_egress_overrides_default_base(self):
+ # Restricted egress must win over the default-base allowlist: VM can't enforce Modal's
+ # outbound domain allowlist, so a default-base origin with a custom domain list stays on
+ # gVisor and the flag is never consulted (the egress gate returns before the fetch).
+ with patch(
+ VM_FLAG_PAYLOAD_TARGET,
+ return_value='{"default_base_origin_products": ["user_created"]}',
+ ) as payload_mock:
+ assert (
+ _is_modal_vm_sandbox_enabled(
+ distinct_id="distinct-id",
+ organization_id="organization-id",
+ run_id="run-id",
+ origin_product="user_created",
+ allowed_domains=["github.com"],
+ )
+ is False
+ )
+
+ payload_mock.assert_not_called()
+
+ def test_modal_vm_sandbox_false_state_override_forces_gvisor_over_default_base(self):
+ # A trusted server-set use_modal_vm_sandbox=False forces gVisor even when the org's payload
+ # would place this origin on the VM base; the bool override also skips the flag fetch.
+ with patch(
+ VM_FLAG_PAYLOAD_TARGET,
+ return_value='{"default_base_origin_products": ["user_created"]}',
+ ) as payload_mock:
+ assert (
+ _is_modal_vm_sandbox_enabled(
+ distinct_id="distinct-id",
+ organization_id="organization-id",
+ run_id="run-id",
+ origin_product="user_created",
+ allowed_domains=None,
+ custom_image_available=True,
+ state={"use_modal_vm_sandbox": False},
+ )
+ is False
+ )
+
+ payload_mock.assert_not_called()
+
@pytest.mark.parametrize(
"origin_product, payload, custom_image_available, expected",
[
@@ -779,6 +823,25 @@ class TestGetTaskProcessingContextActivity:
("user_created", {"origin_products": ["signals_scout"]}, True, False),
("user_created", '{"origin_products": ["user_created"]}', False, False),
("user_created", '{"origin_products": ["user_created"]}', True, True),
+ # default_base_origin_products: listed origins run on the bare VM base image
+ # with no custom image at all — the "VM as default" rollout knob.
+ (
+ "user_created",
+ {"origin_products": ["user_created"], "default_base_origin_products": ["user_created"]},
+ False,
+ True,
+ ),
+ # default-base alone (no origin_products) is enough for a no-custom-image run.
+ ("user_created", {"default_base_origin_products": ["user_created"]}, False, True),
+ # the waiver is scoped per origin — an unlisted origin still gets gVisor.
+ ("signals_scout", {"default_base_origin_products": ["user_created"]}, False, False),
+ # origin_products membership alone does NOT waive the custom-image requirement.
+ (
+ "user_created",
+ {"origin_products": ["user_created"], "default_base_origin_products": ["signals_scout"]},
+ False,
+ False,
+ ),
],
)
def test_modal_vm_sandbox_origin_product_gating(self, origin_product, payload, custom_image_available, expected):
@@ -813,6 +876,23 @@ class TestGetTaskProcessingContextActivity:
def test_vm_sandbox_allowed_origin_products_parsing(self, payload, expected):
assert vm_sandbox_allowed_origin_products(payload) == expected
+ @pytest.mark.parametrize(
+ "payload, expected",
+ [
+ (None, set()),
+ ({"default_base_origin_products": ["user_created"]}, {"user_created"}),
+ ('{"default_base_origin_products": ["a", "b"]}', {"a", "b"}),
+ # Distinct from vm_sandbox_allowed_origin_products: read only from the explicit
+ # dict key, and a bare list is never an opt-in (it keeps origin_products meaning).
+ ({"origin_products": ["user_created"]}, set()),
+ (["user_created"], set()),
+ ("not-json", set()),
+ ({"default_base_origin_products": [1, 2]}, set()),
+ ],
+ )
+ def test_vm_sandbox_default_base_origin_products_parsing(self, payload, expected):
+ assert vm_sandbox_default_base_origin_products(payload) == expected
+
@pytest.mark.parametrize(
"state,expected",
[