: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(tasks): VM sandbox image (#62145)
Alessandro Pogliaghi8a7844d9c911
diff --git a/.github/workflows/cd-sandbox-base-image.yml b/.github/workflows/cd-sandbox-base-image.ymlindex c9e6bd60377e..53e994b2b059 100644--- a/.github/workflows/cd-sandbox-base-image.yml+++ b/.github/workflows/cd-sandbox-base-image.yml@@ -234,6 +234,19 @@ jobs:build-args: |BASE_IMAGE=ghcr.io/posthog/posthog-sandbox-base:${{ github.sha }}+ - name: Build and push container image (vm)+ id: build-vm+ uses: depot/build-push-action@5f3b3c2e5a00f0093de47f657aeaefcedff27d18 # v1.17.0+ with:+ context: . # use local checkout (includes downloaded artifact)+ file: ./products/tasks/backend/sandbox/images/Dockerfile.sandbox-vm+ buildx-fallback: false # the fallback is so slow it's better to just fail+ push: true+ tags: ${{ github.ref == 'refs/heads/master' && format('ghcr.io/posthog/posthog-sandbox-vm:master,ghcr.io/posthog/posthog-sandbox-vm:{0}', github.sha) || format('ghcr.io/posthog/posthog-sandbox-vm:{0}', github.sha) }}+ platforms: linux/arm64,linux/amd64+ build-args: |+ BASE_IMAGE=ghcr.io/posthog/posthog-sandbox-base:${{ github.sha }}+# Single required status check for branch protection. Add future sandbox# image jobs to `needs` here rather than reconfiguring GitHub.sandbox_base_image_check:diff --git a/products/tasks/backend/sandbox/images/Dockerfile.sandbox-vm b/products/tasks/backend/sandbox/images/Dockerfile.sandbox-vmnew file mode 100644index 000000000000..9b6a249e697f--- /dev/null+++ b/products/tasks/backend/sandbox/images/Dockerfile.sandbox-vm@@ -0,0 +1,45 @@+# Dockerfile for the VM sandbox image (Docker-in-Docker capable).+#+# Layers a working Docker engine on top of the base sandbox image so a Modal VM+# sandbox (real Linux kernel) can run nested containers — e.g. the PostHog dev+# stack via `hogli`. This image only functions under the Modal VM runtime;+# dockerd cannot run under the default gVisor sandbox, so sandboxes created from+# it are always provisioned with vm_runtime.+ARG BASE_IMAGE=ghcr.io/posthog/posthog-sandbox-base:master+FROM ${BASE_IMAGE}++# Docker engine, compose v2 plugin and buildx from the Ubuntu archive (no extra+# apt repo). docker.io ships dockerd + containerd; docker-compose-v2 provides+# `docker compose`, which hogli / bin/start invoke.+RUN apt-get update && \+ apt-get install -y --no-install-recommends \+ docker.io \+ docker-compose-v2 \+ docker-buildx && \+ rm -rf /var/lib/apt/lists/*++RUN mkdir -p /etc/docker && \+ printf '%s\n' '{' ' "storage-driver": "overlay2"' '}' > /etc/docker/daemon.json++# There is no init system in the sandbox, so dockerd is launched on demand by+# this idempotent helper rather than by systemd. dockerd is detached with setsid+# and </dev/null so it survives its launching process. On the Modal VM runtime+# the first invocation's exec is still reaped while dockerd brings up its bridge,+# so treat this script's exit status as advisory and confirm with `docker info`+# (or just re-run it — it is idempotent and returns 0 once the daemon is up).+RUN printf '%s\n' \+ '#!/usr/bin/env bash' \+ 'set -uo pipefail' \+ 'if docker info >/dev/null 2>&1; then echo "dockerd already running"; exit 0; fi' \+ 'setsid dockerd </dev/null >/var/log/dockerd.log 2>&1 &' \+ 'for _ in $(seq 1 60); do' \+ ' if docker info >/dev/null 2>&1; then echo "dockerd ready"; exit 0; fi' \+ ' sleep 0.5' \+ 'done' \+ 'echo "dockerd failed to start; recent log:" >&2' \+ 'tail -n 20 /var/log/dockerd.log >&2 || true' \+ 'exit 1' \+ > /usr/local/bin/start-dockerd && \+ chmod +x /usr/local/bin/start-dockerd++CMD ["/bin/bash"]diff --git a/products/tasks/backend/services/modal_sandbox.py b/products/tasks/backend/services/modal_sandbox.pyindex 4e58a3f3339c..22e966ef034d 100644--- a/products/tasks/backend/services/modal_sandbox.py+++ b/products/tasks/backend/services/modal_sandbox.py@@ -76,6 +76,7 @@ DEFAULT_MODAL_APP_NAME = "posthog-sandbox-default"NOTEBOOK_MODAL_APP_NAME = "posthog-sandbox-notebook"SANDBOX_BASE_IMAGE = "ghcr.io/posthog/posthog-sandbox-base"SANDBOX_NOTEBOOK_IMAGE = "ghcr.io/posthog/posthog-sandbox-notebook"+SANDBOX_VM_IMAGE = "ghcr.io/posthog/posthog-sandbox-vm"SANDBOX_IMAGE = SANDBOX_BASE_IMAGEAGENT_SERVER_PORT = 8080 # Modal connect tokens require port 8080AGENT_SERVER_HEALTH_MAX_ATTEMPTS = 240@@ -95,12 +96,13 @@ def _get_modal_region() -> str:LOCAL_MODAL_DOCKERFILES = {SandboxTemplate.DEFAULT_BASE: Path("products/tasks/backend/sandbox/images/Dockerfile.sandbox-base"),SandboxTemplate.NOTEBOOK_BASE: Path("products/tasks/backend/sandbox/images/Dockerfile.sandbox-notebook"),+ SandboxTemplate.VM_BASE: Path("products/tasks/backend/sandbox/images/Dockerfile.sandbox-vm"),}LOCAL_MODAL_INSTALL_SKILLS_SCRIPT = Path("products/tasks/backend/sandbox/images/install-skills.sh")LOCAL_MODAL_GIT_GUARD_SCRIPT = Path("products/tasks/backend/sandbox/images/git-guard.sh")-_image_ref_cache: TTLCache = TTLCache(maxsize=2, ttl=300)+_image_ref_cache: TTLCache = TTLCache(maxsize=3, ttl=300)_image_ref_lock = threading.Lock()@@ -213,7 +215,7 @@ def _attach_local_package_mounts(image: modal.Image, template: SandboxTemplate)return image-_template_image_cache: TTLCache = TTLCache(maxsize=2, ttl=300)+_template_image_cache: TTLCache = TTLCache(maxsize=3, ttl=300)_template_image_lock = threading.Lock()@@ -222,6 +224,7 @@ def _get_template_image(template: SandboxTemplate) -> modal.Image:registry_image = {SandboxTemplate.DEFAULT_BASE: SANDBOX_BASE_IMAGE,SandboxTemplate.NOTEBOOK_BASE: SANDBOX_NOTEBOOK_IMAGE,+ SandboxTemplate.VM_BASE: SANDBOX_VM_IMAGE,}.get(template)if registry_image is None:raise ValueError(f"Unknown template: {template}")@@ -235,7 +238,7 @@ def _get_template_image(template: SandboxTemplate) -> modal.Image:return _attach_local_package_mounts(image, template)-@lru_cache(maxsize=2)+@lru_cache(maxsize=3)def _prepare_local_modal_build_context(template: SandboxTemplate) -> tuple[str, str]:dockerfile_relative_path = LOCAL_MODAL_DOCKERFILES.get(template)if dockerfile_relative_path is None:@@ -357,7 +360,7 @@ class ModalSandbox(SandboxBase):"verbose": True,}- if config.vm_runtime:+ if config.vm_runtime or config.template == SandboxTemplate.VM_BASE:create_kwargs["experimental_options"] = {"vm_runtime": True}if secrets:diff --git a/products/tasks/backend/services/sandbox.py b/products/tasks/backend/services/sandbox.pyindex 0b017c6ad368..6762c404c245 100644--- a/products/tasks/backend/services/sandbox.py+++ b/products/tasks/backend/services/sandbox.py@@ -49,6 +49,7 @@ class SandboxTemplate(str, Enum):DEFAULT_BASE = "default_base"NOTEBOOK_BASE = "notebook_base"PI_BASE = "pi_base"+ VM_BASE = "vm_base"class ExecutionResult(BaseModel):diff --git a/products/tasks/backend/services/test_modal_sandbox.py b/products/tasks/backend/services/test_modal_sandbox.pyindex 4c6e6f3c372f..c690b9cb9b59 100644--- a/products/tasks/backend/services/test_modal_sandbox.py+++ b/products/tasks/backend/services/test_modal_sandbox.py@@ -21,14 +21,16 @@ def patched_modal(mocker):class TestModalSandboxVmRuntime:@pytest.mark.parametrize(- "vm_runtime, expected_experimental_options",+ "template, vm_runtime, expected_experimental_options",[- (True, {"vm_runtime": True}),- (False, None),+ (SandboxTemplate.DEFAULT_BASE, True, {"vm_runtime": True}),+ (SandboxTemplate.DEFAULT_BASE, False, None),+ # VM_BASE forces vm_runtime even when the flag is explicitly False.+ (SandboxTemplate.VM_BASE, False, {"vm_runtime": True}),],)- def test_vm_runtime_experimental_option(self, patched_modal, vm_runtime, expected_experimental_options):- ModalSandbox.create(SandboxConfig(name="test", template=SandboxTemplate.DEFAULT_BASE, vm_runtime=vm_runtime))+ def test_vm_runtime_experimental_option(self, patched_modal, template, vm_runtime, expected_experimental_options):+ ModalSandbox.create(SandboxConfig(name="test", template=template, vm_runtime=vm_runtime))kwargs = patched_modal.call_args.kwargsif expected_experimental_options is None: