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): tag modal sandboxes with task run identifiers (#64234)

Joshua Snyder10c42dd2cc07
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 3d33392ebf21..a55527d7a7e2 100644
--- a/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py
+++ b/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py
@@ -229,6 +229,27 @@ def _emit_image_source_log(ctx: TaskProcessingContext, prepared: PrepareSandboxF
emit_agent_log(ctx.run_id, "debug", f"Creating environment from {prepared.image_source_label}")
+def _build_sandbox_tags(
+ ctx: TaskProcessingContext,
+ prepared: PrepareSandboxForRepositoryOutput,
+ use_vm_sandbox: bool,
+) -> dict[str, str]:
+ """Tags forwarded to the Modal sandbox so it can be traced back when debugging.
+
+ Modal tag values must be strings; None values are dropped so we don't emit empty tags.
+ """
+ tags: dict[str, str | int | None] = {
+ "task_id": ctx.task_id,
+ "task_run_id": ctx.run_id,
+ "origin_product": ctx.origin_product,
+ "team_id": ctx.team_id,
+ "workflow_id": TaskRun.get_workflow_id(ctx.task_id, ctx.run_id),
+ "image_source": prepared.image_source,
+ "sandbox_runtime": "vm" if use_vm_sandbox else "gvisor",
+ }
+ return {key: str(value) for key, value in tags.items() if value is not None}
+
+
@activity.defn
@asyncify
def prepare_sandbox_for_repository(input: PrepareSandboxForRepositoryInput) -> PrepareSandboxForRepositoryOutput:
@@ -375,7 +396,7 @@ def create_sandbox_for_repository(input: CreateSandboxForRepositoryInput) -> Cre
environment_variables=prepared.environment_variables,
snapshot_id=prepared.snapshot_id,
snapshot_external_id=prepared.snapshot_external_id,
- metadata={"task_id": ctx.task_id},
+ metadata=_build_sandbox_tags(ctx, prepared, use_vm_sandbox),
vm_runtime=use_vm_sandbox,
**ctx.sandbox_resource_overrides(),
)
diff --git a/products/tasks/backend/temporal/process_task/tests/test_provision_sandbox.py b/products/tasks/backend/temporal/process_task/tests/test_provision_sandbox.py
new file mode 100644
index 000000000000..7ed88eb070e3
--- /dev/null
+++ b/products/tasks/backend/temporal/process_task/tests/test_provision_sandbox.py
@@ -0,0 +1,80 @@
+from typing import Any
+
+import pytest
+
+from products.tasks.backend.temporal.process_task.activities.get_task_processing_context import TaskProcessingContext
+from products.tasks.backend.temporal.process_task.activities.provision_sandbox import (
+ PrepareSandboxForRepositoryOutput,
+ _build_sandbox_tags,
+)
+
+
+def _context(**overrides) -> TaskProcessingContext:
+ defaults: dict[str, Any] = {
+ "task_id": "task-123",
+ "run_id": "run-456",
+ "team_id": 42,
+ "team_uuid": "team-uuid",
+ "organization_id": "org-uuid",
+ "github_integration_id": None,
+ "repository": "posthog/posthog",
+ "distinct_id": "distinct-id",
+ "origin_product": "error_tracking",
+ "state": {},
+ }
+ defaults.update(overrides)
+ return TaskProcessingContext(**defaults)
+
+
+def _prepared(**overrides) -> PrepareSandboxForRepositoryOutput:
+ defaults: dict[str, Any] = {
+ "sandbox_name": "sandbox-name",
+ "repository": "posthog/posthog",
+ "github_token": "token",
+ "branch": "feature-branch",
+ "environment_variables": {},
+ "snapshot_id": None,
+ "snapshot_external_id": None,
+ "used_snapshot": False,
+ "should_create_snapshot": True,
+ "shallow_clone": True,
+ "image_source": "base_image",
+ "image_source_label": "base image",
+ }
+ defaults.update(overrides)
+ return PrepareSandboxForRepositoryOutput(**defaults)
+
+
+def test_build_sandbox_tags_includes_all_identifiers():
+ tags = _build_sandbox_tags(_context(), _prepared(), use_vm_sandbox=False)
+
+ assert tags == {
+ "task_id": "task-123",
+ "task_run_id": "run-456",
+ "origin_product": "error_tracking",
+ "team_id": "42",
+ "workflow_id": "task-processing-task-123-run-456",
+ "image_source": "base_image",
+ "sandbox_runtime": "gvisor",
+ }
+
+
[email protected]("use_vm_sandbox, expected", [(True, "vm"), (False, "gvisor")])
+def test_build_sandbox_tags_marks_runtime(use_vm_sandbox, expected):
+ tags = _build_sandbox_tags(_context(), _prepared(), use_vm_sandbox=use_vm_sandbox)
+
+ assert tags["sandbox_runtime"] == expected
+
+
[email protected]("image_source", ["base_image", "resume_snapshot", "repository_snapshot"])
+def test_build_sandbox_tags_reflects_image_source(image_source):
+ tags = _build_sandbox_tags(_context(), _prepared(image_source=image_source), use_vm_sandbox=False)
+
+ assert tags["image_source"] == image_source
+
+
+def test_build_sandbox_tags_drops_none_values():
+ tags = _build_sandbox_tags(_context(origin_product=None), _prepared(), use_vm_sandbox=False)
+
+ assert "origin_product" not in tags
+ assert all(isinstance(value, str) for value in tags.values())