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): label sandbox provisioning logs with the custom base image (#71448)

Alessandro Pogliaghi55b8055faed6
diff --git a/products/tasks/backend/temporal/process_task/activities/get_sandbox_for_repository.py b/products/tasks/backend/temporal/process_task/activities/get_sandbox_for_repository.py
index 55c943aff3e8..e5fc6f3117b5 100644
--- a/products/tasks/backend/temporal/process_task/activities/get_sandbox_for_repository.py
+++ b/products/tasks/backend/temporal/process_task/activities/get_sandbox_for_repository.py
@@ -46,6 +46,7 @@ def _get_image_source_label(
provider: str | None,
resume_snapshot_external_id: str | None,
snapshot: SandboxSnapshot | None,
+ custom_image_name: str | None = None,
) -> str:
if resume_snapshot_external_id:
return f"resume snapshot {resume_snapshot_external_id}"
@@ -54,6 +55,9 @@ def _get_image_source_label(
external_id = snapshot.external_id or str(snapshot.id)
return f"repository snapshot {external_id}"
+ if custom_image_name:
+ return f"custom base image {custom_image_name}"
+
if provider == "docker":
return "local Docker sandbox image"
@@ -124,7 +128,8 @@ def get_sandbox_for_repository(input: GetSandboxForRepositoryInput) -> GetSandbo
snapshot_source = "none"
snapshot_kind = SNAPSHOT_KIND_FILESYSTEM
snapshot_mount_path: str | None = None
- if has_repo and github_integration_id is not None:
+ # Repo-setup snapshots come from default-base sandboxes; restoring one would drop the custom image.
+ if has_repo and github_integration_id is not None and not ctx.custom_image_name:
assert repository is not None
with StepTimer("snapshot_lookup") as snapshot_lookup_timer:
snapshot = SandboxSnapshot.get_latest_snapshot_with_repos(github_integration_id, [repository])
@@ -249,11 +254,14 @@ def get_sandbox_for_repository(input: GetSandboxForRepositoryInput) -> GetSandbo
snapshot_mount_path = run_state.resume_snapshot_mount_path()
provider = getattr(settings, "SANDBOX_PROVIDER", None)
+ use_vm_sandbox = ctx.use_modal_vm_sandbox
+ custom_image_name = ctx.custom_image_name if use_vm_sandbox else None
image_source_label = _get_image_source_label(
has_repo=has_repo,
provider=provider,
resume_snapshot_external_id=resume_snapshot_ext_id,
snapshot=snapshot if not resume_snapshot_ext_id else None,
+ custom_image_name=custom_image_name,
)
if resume_snapshot_ext_id:
@@ -267,7 +275,9 @@ def get_sandbox_for_repository(input: GetSandboxForRepositoryInput) -> GetSandbo
config = SandboxConfig(
name=get_sandbox_name_for_task(ctx.task_id),
- template=SandboxTemplate.DEFAULT_BASE,
+ template=SandboxTemplate.VM_BASE if use_vm_sandbox else SandboxTemplate.DEFAULT_BASE,
+ custom_image_name=custom_image_name,
+ vm_runtime=use_vm_sandbox,
environment_variables=environment_variables,
snapshot_external_id=resume_snapshot_ext_id,
snapshot_kind=snapshot_kind,
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 a3f3b678c1a9..bd90152676fd 100644
--- a/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py
+++ b/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py
@@ -208,6 +208,17 @@ def _get_image_source_label(
return "base_image", "published sandbox base image"
+def get_fresh_image_source_for_context(ctx: TaskProcessingContext) -> tuple[str, str]:
+ """Image source and label for a sandbox provisioned fresh (no snapshot) from this context."""
+ return _get_image_source_label(
+ has_repo=ctx.repository is not None,
+ provider=getattr(settings, "SANDBOX_PROVIDER", None),
+ resume_snapshot_external_id=None,
+ snapshot=None,
+ custom_image_name=ctx.custom_image_name if ctx.use_modal_vm_sandbox else None,
+ )
+
+
def _build_environment_variables(
ctx: TaskProcessingContext, task: Task, github_token: str, access_token: str
) -> dict[str, str]:
diff --git a/products/tasks/backend/temporal/process_task/tests/test_workflow.py b/products/tasks/backend/temporal/process_task/tests/test_workflow.py
index 290e581d91a2..4a8eb82ddde5 100644
--- a/products/tasks/backend/temporal/process_task/tests/test_workflow.py
+++ b/products/tasks/backend/temporal/process_task/tests/test_workflow.py
@@ -73,6 +73,8 @@ def _build_context(
use_modal_resume_snapshots: bool = True,
sandbox_event_ingest_enabled: bool = False,
environment: str | None = None,
+ use_modal_vm_sandbox: bool = False,
+ custom_image_name: str | None = None,
) -> TaskProcessingContext:
return TaskProcessingContext(
task_id="task-id",
@@ -89,6 +91,8 @@ def _build_context(
_branch="feature-branch",
use_modal_resume_snapshots=use_modal_resume_snapshots,
sandbox_event_ingest_enabled=sandbox_event_ingest_enabled,
+ use_modal_vm_sandbox=use_modal_vm_sandbox,
+ custom_image_name=custom_image_name,
)
@@ -1134,13 +1138,22 @@ class TestProcessTaskWorkflowUnit:
assert inject_fresh_tokens_on_resume not in activity_calls
+ @pytest.mark.parametrize(
+ "custom_image_name, expected_image_source, expected_image_source_label",
+ [
+ (None, "base_image", "published sandbox base image"),
+ ("sandbox-custom-abc", "custom_image", "custom base image sandbox-custom-abc"),
+ ],
+ )
async def test_get_sandbox_for_repository_falls_back_to_fresh_sandbox_when_resume_injection_fails(
- self, monkeypatch
+ self, monkeypatch, custom_image_name, expected_image_source, expected_image_source_label
):
workflow = ProcessTaskWorkflow()
workflow._context = _build_context(
github_integration_id=123,
state={"snapshot_external_id": "im-abc123", "resume_from_run_id": "previous-run-id"},
+ use_modal_vm_sandbox=custom_image_name is not None,
+ custom_image_name=custom_image_name,
)
prepared = PrepareSandboxForRepositoryOutput(
@@ -1205,6 +1218,8 @@ class TestProcessTaskWorkflowUnit:
assert fresh_prepared.snapshot_external_id is None
assert fresh_prepared.used_snapshot is False
assert fresh_prepared.should_create_snapshot is True
+ assert fresh_prepared.image_source == expected_image_source
+ assert fresh_prepared.image_source_label == expected_image_source_label
assert clone_repository_in_sandbox in activity_calls
async def test_get_sandbox_for_repository_propagates_non_dead_sandbox_failures(self, monkeypatch):
diff --git a/products/tasks/backend/temporal/process_task/workflow.py b/products/tasks/backend/temporal/process_task/workflow.py
index 4afa19419e92..6855e6727e39 100644
--- a/products/tasks/backend/temporal/process_task/workflow.py
+++ b/products/tasks/backend/temporal/process_task/workflow.py
@@ -53,6 +53,7 @@ from .activities.provision_sandbox import (
checkout_branch_in_sandbox,
clone_repository_in_sandbox,
create_sandbox_for_repository,
+ get_fresh_image_source_for_context,
inject_fresh_tokens_on_resume,
invalidate_resume_snapshot,
prepare_sandbox_for_repository,
@@ -927,6 +928,7 @@ class ProcessTaskWorkflow(PostHogWorkflow):
)
await self._cleanup_sandbox(created.sandbox_id)
self._sandbox_id_for_cleanup = None
+ fresh_image_source, fresh_image_source_label = get_fresh_image_source_for_context(self.context)
prepared = replace(
prepared,
snapshot_id=None,
@@ -936,8 +938,8 @@ class ProcessTaskWorkflow(PostHogWorkflow):
snapshot_kind=SNAPSHOT_KIND_FILESYSTEM,
snapshot_mount_path=None,
snapshot_source="none",
- image_source="base_image",
- image_source_label="published sandbox base image",
+ image_source=fresh_image_source,
+ image_source_label=fresh_image_source_label,
)
await self._emit_progress(
"sandbox",