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.

fix(tasks): record effective burstable request floors in the sandbox ledger (#79169)

Adam Bowker7e12735fd54f
diff --git a/products/tasks/backend/logic/services/modal_sandbox.py b/products/tasks/backend/logic/services/modal_sandbox.py
index 0d0cab00fa79..c048318d45d6 100644
--- a/products/tasks/backend/logic/services/modal_sandbox.py
+++ b/products/tasks/backend/logic/services/modal_sandbox.py
@@ -222,22 +222,19 @@ def _resource_create_kwargs(config: SandboxConfig) -> dict[str, object]:
``(request, limit)`` tuple form so the box is billed at ``max(request, actual)`` and can burst
up to the limit; otherwise emit the flat scalar, which makes request == limit (fixed size).
- The burstable request floor comes from ``cpu_request_cores`` / ``memory_request_mb`` (defaulting
- to the small floor in ``sandbox_config``). The request is clamped to the limit so it never
- exceeds it when the configured size is at or below the requested floor.
+ The burstable request floors come from the config's ``effective_*_request`` properties — the
+ same values the usage ledger records — so what Modal reserves and what pricing bills can't
+ diverge. VM memory can't burst, so it stays the flat scalar (request == limit).
"""
cpu_limit = float(config.cpu_cores)
memory_limit_mb = int(config.memory_gb * 1024)
if not config.burstable_resources:
return {"cpu": cpu_limit, "memory": memory_limit_mb}
- cpu_value = (min(float(config.cpu_request_cores), cpu_limit), cpu_limit)
+ cpu_value = (config.effective_cpu_request_cores, cpu_limit)
if config.is_vm:
return {"cpu": cpu_value, "memory": memory_limit_mb}
- return {
- "cpu": cpu_value,
- "memory": (min(int(config.memory_request_mb), memory_limit_mb), memory_limit_mb),
- }
+ return {"cpu": cpu_value, "memory": (config.effective_memory_request_mb, memory_limit_mb)}
LOCAL_MODAL_DOCKERFILES = {
diff --git a/products/tasks/backend/logic/services/sandbox.py b/products/tasks/backend/logic/services/sandbox.py
index c1f25cafa5cd..6fed27c20a45 100644
--- a/products/tasks/backend/logic/services/sandbox.py
+++ b/products/tasks/backend/logic/services/sandbox.py
@@ -114,7 +114,8 @@ class SandboxConfig(BaseModel):
# (the limit); Modal bills max(request, actual). When False, request == limit (fixed size).
burstable_resources: bool = False
# Request floor used when `burstable_resources` is True: the box reserves this much and bursts
- # up to `cpu_cores` / `memory_gb`. Clamped to the limit at create time so it never exceeds it.
+ # up to `cpu_cores` / `memory_gb`. Read through the `effective_*_request` properties, which
+ # apply the limit clamp and the VM memory pin.
cpu_request_cores: float = BURSTABLE_REQUEST_CPU_CORES
memory_request_mb: int = BURSTABLE_REQUEST_MEMORY_MB
vm_runtime: bool = False
@@ -148,6 +149,21 @@ class SandboxConfig(BaseModel):
def is_vm(self) -> bool:
return self.vm_runtime or self.template == SandboxTemplate.VM_BASE
+ @property
+ def effective_cpu_request_cores(self) -> float:
+ """CPU floor the provider actually reserves when burstable: the configured request,
+ clamped to the limit."""
+ return min(float(self.cpu_request_cores), float(self.cpu_cores))
+
+ @property
+ def effective_memory_request_mb(self) -> int:
+ """Memory floor the provider actually reserves when burstable. VM memory can't burst,
+ so a VM's request is pinned to its limit; gVisor requests are clamped to the limit."""
+ memory_limit_mb = int(self.memory_gb * 1024)
+ if self.is_vm:
+ return memory_limit_mb
+ return min(int(self.memory_request_mb), memory_limit_mb)
+
WORKING_DIR = DEFAULT_SANDBOX_WORKING_DIR
diff --git a/products/tasks/backend/logic/services/sandbox_usage.py b/products/tasks/backend/logic/services/sandbox_usage.py
index 11a942ff3c8d..eccb5e9f49aa 100644
--- a/products/tasks/backend/logic/services/sandbox_usage.py
+++ b/products/tasks/backend/logic/services/sandbox_usage.py
@@ -82,8 +82,8 @@ def open_sandbox_session(
"memory_gb": config.memory_gb,
"ttl_seconds": config.ttl_seconds,
"burstable": config.burstable_resources,
- "cpu_request_cores": config.cpu_request_cores if config.burstable_resources else None,
- "memory_request_mb": config.memory_request_mb if config.burstable_resources else None,
+ "cpu_request_cores": config.effective_cpu_request_cores if config.burstable_resources else None,
+ "memory_request_mb": config.effective_memory_request_mb if config.burstable_resources else None,
"created_at": created_at,
"ttl_expires_at": created_at + timedelta(seconds=config.ttl_seconds),
}
diff --git a/products/tasks/backend/logic/services/tests/test_sandbox_usage.py b/products/tasks/backend/logic/services/tests/test_sandbox_usage.py
index 340e471fd2b4..9a03db479aa5 100644
--- a/products/tasks/backend/logic/services/tests/test_sandbox_usage.py
+++ b/products/tasks/backend/logic/services/tests/test_sandbox_usage.py
@@ -5,6 +5,8 @@ from freezegun import freeze_time
from posthog.test.base import APIBaseTest
from unittest.mock import patch
+from parameterized import parameterized
+
from posthog.models.organization import Organization
from posthog.models.scoping import team_scope
from posthog.models.team.team import Team
@@ -108,19 +110,30 @@ class TestSandboxSessionWrites(SandboxUsageBase):
assert first.client_provenance == TaskClientProvenance.POSTHOG_DESKTOP
assert resumed.client_provenance == TaskClientProvenance.POSTHOG_DESKTOP
- def test_open_records_burstable_request_floors(self):
+ @parameterized.expand(
+ [
+ ("gvisor", {}, 0.5, 1024),
+ # VM memory can't burst — its effective request is the limit, and the ledger must
+ # record what the provider reserved, not the generic floor.
+ ("vm", {"vm_runtime": True}, 0.5, 16384),
+ ("clamped_to_limit", {"cpu_cores": 0.25, "memory_gb": 0.5}, 0.25, 512),
+ ]
+ )
+ def test_open_records_effective_burstable_request_floors(
+ self, name, config_overrides, expected_cpu_cores, expected_memory_mb
+ ):
run = self._run()
open_sandbox_session(
run_id=run.id,
- sandbox_id="sb-burst",
- config=_config(burstable_resources=True, cpu_request_cores=0.5, memory_request_mb=1024),
+ sandbox_id=f"sb-burst-{name}",
+ config=_config(burstable_resources=True, cpu_request_cores=0.5, memory_request_mb=1024, **config_overrides),
)
- session = SandboxSession.objects.unscoped().get(sandbox_id="sb-burst")
+ session = SandboxSession.objects.unscoped().get(sandbox_id=f"sb-burst-{name}")
assert session.burstable is True
- assert session.cpu_request_cores == 0.5
- assert session.memory_request_mb == 1024
+ assert session.cpu_request_cores == expected_cpu_cores
+ assert session.memory_request_mb == expected_memory_mb
def test_open_anchors_ttl_deadline_at_the_sandbox_creation_boundary(self):
# The provider's TTL clock starts at Sandbox.create(), minutes before repo
diff --git a/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py b/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py
index c7c5b5900a7f..18ce02c18a22 100644
--- a/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py
+++ b/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py
@@ -600,8 +600,8 @@ def create_sandbox_for_repository(input: CreateSandboxForRepositoryInput) -> Cre
emit_agent_log(
ctx.run_id,
"debug",
- f"Burstable resources enabled: requesting {config.cpu_request_cores} CPU / "
- f"{config.memory_request_mb} MiB, bursting up to {config.cpu_cores} CPU / "
+ f"Burstable resources enabled: requesting {config.effective_cpu_request_cores} CPU / "
+ f"{config.effective_memory_request_mb} MiB, bursting up to {config.cpu_cores} CPU / "
f"{int(config.memory_gb * 1024)} MiB",
)