: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
refactor(tasks): always use directory resume snapshots (#124065)
Alessandro Pogliaghi77b80cfed49d
diff --git a/docs/published/handbook/engineering/ai/sandboxed-agents.md b/docs/published/handbook/engineering/ai/sandboxed-agents.mdindex 807e0e57b3eb..60a521253e22 100644--- a/docs/published/handbook/engineering/ai/sandboxed-agents.md+++ b/docs/published/handbook/engineering/ai/sandboxed-agents.md@@ -600,8 +600,8 @@ The `use_modal_vm_sandbox` run-state key force-selects the VM runtime for trusteEvery Modal sandbox is created with a [readiness probe](https://modal.com/docs/guide/sandboxes#readiness-probes) that runs `true` inside the sandbox until it exits 0.Provisioning waits on that probe before it runs anything else in the sandbox, because a sandbox can come up dead with every RPC succeeding.-That happens most often after a filesystem snapshot restore: a resume snapshot, or the prebaked dev-stack image, which is itself a snapshot.-A sandbox whose probe has not passed within `READINESS_PROBE_TIMEOUT_SECONDS` (`products/tasks/backend/logic/services/modal_sandbox.py`) is terminated and recreated from the next image candidate in the downgrade chain: resume snapshot, then custom or dev-stack image, then the plain base.+That happens most often after a filesystem snapshot restore: a repository snapshot, a Streamlit app image, or the prebaked dev-stack image, which is itself a snapshot.+A sandbox whose probe has not passed within `READINESS_PROBE_TIMEOUT_SECONDS` (`products/tasks/backend/logic/services/modal_sandbox.py`) is terminated and recreated from the next image candidate in the downgrade chain: custom or dev-stack image, then the plain base.Termination is retried, and provisioning fails when it still does not complete: the run stores only the id of the sandbox that `create()` returned, so a sandbox left running here is invisible to every later cleanup path.A directory resume snapshot is mounted into the sandbox after the probe has passed, and Modal stops the probe at its first success.Provisioning therefore runs one more `true` after that mount, and recreates the sandbox without the mount when it fails.@@ -715,7 +715,7 @@ same-run restores stay idle until a message arrives. Internal recovery instructiuse hidden content blocks; adapters and transcript rendering omit those blocks fromuser message echoes. Existing unmarked transcript entries are unchanged.-Full filesystem snapshots contain an agent binary. Prewarmed resumes require its+Repository snapshots contain an agent binary. Prewarmed resumes require its`prewarmedResumeMessageDriven` capability; the older `prewarmedResumeIdle` capabilityalone is insufficient. An incompatible snapshot uses the existing fresh-agent fallback.The check applies to ACP runs, because the capability belongs to the ACP agent server.diff --git a/products/tasks/backend/constants.py b/products/tasks/backend/constants.pyindex e90cf9f1056c..de491955e07b 100644--- a/products/tasks/backend/constants.py+++ b/products/tasks/backend/constants.py@@ -229,9 +229,7 @@ OWN_SUBSCRIPTION_CLOUD_FEATURE_FLAGS: dict[str, str] = {SUBSCRIPTION_PLAN_NAMES: dict[str, str] = {"claude": "Claude plan", "codex": "ChatGPT plan"}CODEX_SUBSCRIPTION_EGRESS_DOMAINS: tuple[str, ...] = ("chatgpt.com",)-SnapshotKind = Literal["filesystem", "directory"]-SNAPSHOT_KIND_FILESYSTEM: SnapshotKind = "filesystem"-SNAPSHOT_KIND_DIRECTORY: SnapshotKind = "directory"+RESUME_SNAPSHOT_KIND = "directory"DEFAULT_SANDBOX_WORKING_DIR = "/tmp/workspace"# Directory a sandbox clones repositories under, as `<root>/<organization>/<repository>`. Anything# that tells an agent where its checkout is must read it from here (see `sandbox_repo_path`), or adiff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.pyindex 4052877d0014..f29b519858aa 100644--- a/products/tasks/backend/facade/api.py+++ b/products/tasks/backend/facade/api.py@@ -2930,8 +2930,6 @@ def delete_sandbox_custom_image(image_id: str | UUID, team_id: int, user_id: int# - sandbox_cpu_cores / sandbox_memory_gb / sandbox_ttl_seconds / inactivity_timeout_seconds set# the run's compute and lifetime at creation; a caller could otherwise PATCH a queued run to# provision an oversized or long-lived sandbox beyond what they're entitled to.-# - use_modal_directory_resume_snapshots is the server-side directory snapshot rollout decision;-# a caller could otherwise force directory snapshot creation while the feature flag is off.# - use_modal_vm_sandbox is reserved for trusted server-created runs such as image builders;# a caller could otherwise force the VM runtime while the feature flag or custom-image gate is off.# - snapshot_external_id / snapshot_kind / snapshot_mount_path control which Modal image is@@ -3006,7 +3004,6 @@ _PROTECTED_RUN_STATE_KEYS = frozenset("wizard_config","reconciled_pr_merge_url","wizard_head_branch",- "use_modal_directory_resume_snapshots","use_modal_vm_sandbox",# The image a run boots from; a PATCHed value would pick an image the task never asked for."sandbox_template",diff --git a/products/tasks/backend/logic/services/docker_sandbox.py b/products/tasks/backend/logic/services/docker_sandbox.pyindex 079ece8646c6..ecb2c3d8216a 100644--- a/products/tasks/backend/logic/services/docker_sandbox.py+++ b/products/tasks/backend/logic/services/docker_sandbox.py@@ -452,8 +452,8 @@ class DockerSandbox(AgentServerLaunchMixin):def _get_image(config: SandboxConfig) -> str:"""Get the image to use, checking for snapshots first."""config.snapshot_restored = False- if config.snapshot_external_id:- snapshot_image = f"posthog-sandbox-snapshot:{config.snapshot_external_id}"+ if config.resume_snapshot_external_id:+ snapshot_image = f"posthog-sandbox-snapshot:{config.resume_snapshot_external_id}"result = DockerSandbox._run(["docker", "images", "-q", snapshot_image])if result.stdout.strip():config.snapshot_restored = Truediff --git a/products/tasks/backend/logic/services/hogland_sandbox.py b/products/tasks/backend/logic/services/hogland_sandbox.pyindex c6f7b264a469..a2dc674ae9c5 100644--- a/products/tasks/backend/logic/services/hogland_sandbox.py+++ b/products/tasks/backend/logic/services/hogland_sandbox.py@@ -250,12 +250,16 @@ class HoglandSandbox(AgentServerLaunchMixin):"Hogland run provisioning from the pluggable-memory golden",extra={"config_name": config.name, "snapshot_alias": snapshot_alias},)- if config.snapshot_id or config.snapshot_external_id:+ if config.snapshot_id or config.snapshot_external_id or config.resume_snapshot_external_id:# Backend resolution forces resume snapshots off for hogland runs; if an id# slips through anyway, cold-boot loudly rather than restoring the wrong thing.logger.warning("Hogland backend ignores resume snapshot; cold-booting from the golden image",- extra={"snapshot_id": config.snapshot_id, "snapshot_external_id": config.snapshot_external_id},+ extra={+ "snapshot_id": config.snapshot_id,+ "snapshot_external_id": config.snapshot_external_id,+ "resume_snapshot_external_id": config.resume_snapshot_external_id,+ },)config.snapshot_restored = Falseconfig.image_fallback = Nonediff --git a/products/tasks/backend/logic/services/modal_sandbox.py b/products/tasks/backend/logic/services/modal_sandbox.pyindex 4f7d37693855..4bed35d7304d 100644--- a/products/tasks/backend/logic/services/modal_sandbox.py+++ b/products/tasks/backend/logic/services/modal_sandbox.py@@ -42,13 +42,7 @@ from posthog.exceptions_capture import capture_exceptionfrom posthog.settings import CLOUD_DEPLOYMENTfrom posthog.utils import get_instance_region-from products.tasks.backend.constants import (- ALLOWED_DIRECTORY_RESUME_SNAPSHOT_MOUNT_PATHS,- DEV_STACK_IMAGE_NAME,- SNAPSHOT_KIND_DIRECTORY,- SNAPSHOT_KIND_FILESYSTEM,- SnapshotKind,-)+from products.tasks.backend.constants import ALLOWED_DIRECTORY_RESUME_SNAPSHOT_MOUNT_PATHS, DEV_STACK_IMAGE_NAMEfrom products.tasks.backend.exceptions import (SandboxCleanupError,SandboxControlPlaneError,@@ -336,12 +330,6 @@ def _get_modal_region() -> str:return MODAL_REGION_BY_DEPLOYMENT.get(CLOUD_DEPLOYMENT, DEFAULT_MODAL_REGION)-def _normalize_snapshot_kind(value: object) -> SnapshotKind:- if value == SNAPSHOT_KIND_DIRECTORY:- return SNAPSHOT_KIND_DIRECTORY- return SNAPSHOT_KIND_FILESYSTEM--def _resource_create_kwargs(config: SandboxConfig) -> dict[str, object]:"""Build the `cpu`/`memory` kwargs for ``modal.Sandbox.create``.@@ -1078,43 +1066,42 @@ class ModalSandbox(AgentServerLaunchMixin):config.snapshot_restored = Falseconfig.image_fallback = Nonesnapshot_external_id: str | None = None- snapshot_kind = _normalize_snapshot_kind(config.snapshot_kind)# No default-fill: a directory snapshot must arrive with an explicit allowed mount# path, or the mount below is refused. Defaulting here would silently re-target a# snapshot that upstream validation invalidated (mount path stripped).snapshot_mount_path: str | None = config.snapshot_mount_pathsnapshot_image: modal.Image | None = None- if config.snapshot_external_id:+ if config.resume_snapshot_external_id:+ snapshot_external_id = config.resume_snapshot_external_id+ try:+ snapshot_image = modal.Image.from_id(config.resume_snapshot_external_id)+ except Exception as e:+ logger.warning(f"Failed to load resume snapshot image {config.resume_snapshot_external_id}: {e}")+ capture_exception(e)+ elif config.snapshot_external_id:snapshot_external_id = config.snapshot_external_idtry:snapshot_image = modal.Image.from_id(config.snapshot_external_id)except Exception as e:- logger.warning(f"Failed to load resume snapshot image {config.snapshot_external_id}: {e}")+ logger.warning(f"Failed to load snapshot image {config.snapshot_external_id}: {e}")capture_exception(e)elif config.snapshot_id:snapshot = SandboxSnapshot.objects.get(id=config.snapshot_id)if snapshot.status == SandboxSnapshot.Status.COMPLETE:snapshot_external_id = snapshot.external_id- snapshot_kind = _normalize_snapshot_kind(snapshot.metadata.get("snapshot_kind"))- metadata_mount_path = snapshot.metadata.get("snapshot_mount_path")- if isinstance(metadata_mount_path, str) and metadata_mount_path:- snapshot_mount_path = metadata_mount_pathtry:snapshot_image = modal.Image.from_id(snapshot.external_id)except Exception as e:logger.warning(f"Failed to load snapshot image {snapshot.external_id}: {e}")capture_exception(e)- # Keep the config's kind authoritative for downstream trust decisions — the- # dev-stack bootstrap gate distinguishes directory from filesystem restores.- config.snapshot_kind = snapshot_kind# Creation candidates, best first. Each later entry is a downgrade attempted only# when creating from the previous image fails (e.g. its overlay image build errors),# so a broken overlay costs the local packages, never the snapshot or custom image.candidates: list[ImageCandidate] = []requested_dev_stack = config.is_dev_stack_image- if snapshot_image is not None and snapshot_kind == SNAPSHOT_KIND_FILESYSTEM:+ if snapshot_image is not None and config.resume_snapshot_external_id is None:overlaid_snapshot = _attach_local_package_mounts(snapshot_image, config.template, install_dependencies=False)@@ -1153,7 +1140,7 @@ class ModalSandbox(AgentServerLaunchMixin):)candidates.append(ImageCandidate(base_image, "base image"))if config.require_snapshot:- if snapshot_image is None or snapshot_kind != SNAPSHOT_KIND_FILESYSTEM:+ if snapshot_image is None or config.resume_snapshot_external_id is not None:raise RuntimeError("The required filesystem snapshot could not be loaded")candidates = [ImageCandidate(@@ -1198,7 +1185,7 @@ class ModalSandbox(AgentServerLaunchMixin):config.image_fallback = f"{custom_image_failure} -> base image"directory_mount_applied = False- if snapshot_kind == SNAPSHOT_KIND_DIRECTORY and snapshot_image is not None:+ if config.resume_snapshot_external_id and snapshot_image is not None:# The mount REPLACES the target directory in the running sandbox — over a live# system path (the legacy "/tmp" default) that kills Modal's in-sandbox helpers,# and a snapshot's content only fits the path it was captured from. Last-line@@ -1242,7 +1229,6 @@ class ModalSandbox(AgentServerLaunchMixin):"sandbox_id": sb.object_id,"winner_label": winner.label,"snapshot_external_id": snapshot_external_id,- "snapshot_kind": snapshot_kind,"snapshot_mount_path": snapshot_mount_path,},)diff --git a/products/tasks/backend/logic/services/sandbox.py b/products/tasks/backend/logic/services/sandbox.pyindex 8dfc16a68890..9b9aa680819d 100644--- a/products/tasks/backend/logic/services/sandbox.py+++ b/products/tasks/backend/logic/services/sandbox.py@@ -37,9 +37,6 @@ from products.tasks.backend.constants import (DEFAULT_SANDBOX_WORKING_DIR,DEV_STACK_IMAGE_NAME,SANDBOX_REPOSITORIES_ROOT,- SNAPSHOT_KIND_DIRECTORY,- SNAPSHOT_KIND_FILESYSTEM,- SnapshotKind,)from products.tasks.backend.logic.services.local_skills import BUNDLED_SKILLS_PATHS, ENV_DISABLE_BUNDLED_SKILLSfrom products.tasks.backend.logic.services.sandbox_config import (@@ -217,7 +214,7 @@ class SandboxConfig(BaseModel):environment_variables: dict[str, str] | None = Nonesnapshot_id: str | None = Nonesnapshot_external_id: str | None = None- snapshot_kind: SnapshotKind = SNAPSHOT_KIND_FILESYSTEM+ resume_snapshot_external_id: str | None = Nonesnapshot_mount_path: str | None = Nonesnapshot_source: str = "none"snapshot_restored: bool = False@@ -533,16 +530,15 @@ class SandboxBase(ABC):# its author code execution with another member's secrets. Two checks:# - the reserved name (user images always publish as posthog-sandbox-custom-*),# with the absolute path below avoiding PATH shadowing;- # - not a filesystem-snapshot restore: such a resume boots a mutable snapshot+ # - not a filesystem-snapshot restore: such a restore boots a mutable snapshot# of a prior run — one that processed untrusted repo content and could have# replaced the helper file itself. Directory restores only mount the workspace# dir (ALLOWED_DIRECTORY_RESUME_SNAPSHOT_MOUNT_PATHS), leaving the helper the# vetted image's own binary, so the warmup stays on for them — but the# workspace is still attacker-controlled, so the launcher below scrubs the- # environment (fixed PATH, no credentials) rather than trusting it. Anything- # not explicitly a directory restore is treated as filesystem — fail closed.+ # environment (fixed PATH, no credentials) rather than trusting it.restored_untrusted_filesystem = (- self.config.snapshot_restored and self.config.snapshot_kind != SNAPSHOT_KIND_DIRECTORY+ self.config.snapshot_restored and self.config.resume_snapshot_external_id is None)if (not self.config.is_vmdiff --git a/products/tasks/backend/logic/services/test_modal_sandbox.py b/products/tasks/backend/logic/services/test_modal_sandbox.pyindex 998c47e25443..11432b821601 100644--- a/products/tasks/backend/logic/services/test_modal_sandbox.py+++ b/products/tasks/backend/logic/services/test_modal_sandbox.py@@ -9,11 +9,7 @@ from django.conf import settingsfrom modal.exception import TimeoutError as ModalTimeoutError-from products.tasks.backend.constants import (- DEFAULT_SANDBOX_WORKING_DIR,- SNAPSHOT_KIND_DIRECTORY,- SNAPSHOT_KIND_FILESYSTEM,-)+from products.tasks.backend.constants import DEFAULT_SANDBOX_WORKING_DIRfrom products.tasks.backend.exceptions import SandboxExecutionErrorfrom products.tasks.backend.logic.services.modal_sandbox import (DEFAULT_MODAL_APP_NAME,@@ -238,8 +234,7 @@ class TestModalSandboxDirectorySnapshotMount:SandboxConfig(name="test",template=SandboxTemplate.DEFAULT_BASE,- snapshot_external_id="im-dir",- snapshot_kind=SNAPSHOT_KIND_DIRECTORY,+ resume_snapshot_external_id="im-dir",snapshot_mount_path=mount_path,))@@ -251,8 +246,17 @@ class TestModalSandboxDirectorySnapshotMount:fake_sandbox.mount_image.assert_not_called()assert sandbox.config.snapshot_restored is False- @pytest.mark.parametrize("snapshot_kind", [SNAPSHOT_KIND_DIRECTORY, SNAPSHOT_KIND_FILESYSTEM])- def test_wedged_restore_falls_back_to_base_image(self, patched_modal, mocker, snapshot_kind):+ @pytest.mark.parametrize(+ "restore",+ [+ {+ "resume_snapshot_external_id": "im-snap",+ "snapshot_mount_path": DEFAULT_SANDBOX_WORKING_DIR,+ },+ {"snapshot_external_id": "im-snap"},+ ],+ )+ def test_wedged_restore_falls_back_to_base_image(self, patched_modal, mocker, restore):snapshot_image = MagicMock()mocker.patch("modal.Image.from_id", return_value=snapshot_image)wedged = MagicMock()@@ -266,13 +270,11 @@ class TestModalSandboxDirectorySnapshotMount:SandboxConfig(name="test",template=SandboxTemplate.DEFAULT_BASE,- snapshot_external_id="im-snap",- snapshot_kind=snapshot_kind,- snapshot_mount_path=DEFAULT_SANDBOX_WORKING_DIR if snapshot_kind == SNAPSHOT_KIND_DIRECTORY else None,+ **restore,))- if snapshot_kind == SNAPSHOT_KIND_DIRECTORY:+ if "resume_snapshot_external_id" in restore:wedged.mount_image.assert_called_once_with(DEFAULT_SANDBOX_WORKING_DIR, snapshot_image)else:wedged.mount_image.assert_not_called()diff --git a/products/tasks/backend/logic/services/tests/test_modal_sandbox.py b/products/tasks/backend/logic/services/tests/test_modal_sandbox.pyindex ca3dc98c830b..b3066022d925 100644--- a/products/tasks/backend/logic/services/tests/test_modal_sandbox.py+++ b/products/tasks/backend/logic/services/tests/test_modal_sandbox.py@@ -33,7 +33,7 @@ from requests.exceptions import (from posthog.utils import get_instance_region-from products.tasks.backend.constants import DEFAULT_SANDBOX_WORKING_DIR, SNAPSHOT_KIND_DIRECTORY+from products.tasks.backend.constants import DEFAULT_SANDBOX_WORKING_DIRfrom products.tasks.backend.dev_stack_image_models import DevStackImageTargetfrom products.tasks.backend.exceptions import (ProcessTaskFatalError,@@ -524,23 +524,19 @@ class TestModalSandboxAgentServer:write.assert_not_called()@pytest.mark.parametrize(- "enabled,snapshot_kind,compatible",+ "enabled,restored,compatible",[- (False, "directory", True),- (False, "filesystem", True),- (True, "filesystem", True),- (True, "filesystem", False),- (True, "directory", False),- (True, None, True),- (True, None, False),- (False, None, True),+ (False, False, True),+ (True, False, True),+ (True, False, False),+ (True, True, True),+ (True, True, False),],)def test_initializes_only_compatible_task_filesystems(- self, mock_sandbox: Any, enabled: bool, snapshot_kind: str | None, compatible: bool+ self, mock_sandbox: Any, enabled: bool, restored: bool, compatible: bool) -> None:- mock_sandbox.config.snapshot_restored = snapshot_kind is not None- mock_sandbox.config.snapshot_kind = snapshot_kind+ mock_sandbox.config.snapshot_restored = restoredwith (override_settings(TASKS_SANDBOX_PERSISTENT_FILESYSTEM_ENABLED=enabled),patch.object(@@ -553,11 +549,11 @@ class TestModalSandboxAgentServer:patch.object(mock_sandbox, "_write_required_file") as write,):mock_sandbox._prepare_task_filesystem()- initialize = compatible and (snapshot_kind is not None or enabled)+ initialize = compatible and (restored or enabled)assert write.call_count == int(initialize)- if snapshot_kind is not None or enabled:+ if restored or enabled:assert ("initialize gvisor" in execute.call_args.args[0]) == initialize- assert ("test -f" if snapshot_kind else "test -d") in execute.call_args_list[0].args[0]+ assert ("test -f" if restored else "test -d") in execute.call_args_list[0].args[0]else:execute.assert_not_called()@@ -2459,8 +2455,7 @@ class TestModalSandboxCreateImageFallback:name="t",template=SandboxTemplate.VM_BASE,custom_image_name="posthog-dev-stack",- snapshot_external_id="im-snap-1",- snapshot_kind=SNAPSHOT_KIND_DIRECTORY,+ resume_snapshot_external_id="im-snap-1",snapshot_mount_path=DEFAULT_SANDBOX_WORKING_DIR,)custom_image = MagicMock(name="custom_image")@@ -2551,8 +2546,7 @@ class TestModalSandboxCreateImageFallback:name="t",template=SandboxTemplate.VM_BASE,custom_image_name="posthog-dev-stack",- snapshot_external_id="im-snap-1",- snapshot_kind=SNAPSHOT_KIND_DIRECTORY,+ resume_snapshot_external_id="im-snap-1",snapshot_mount_path=DEFAULT_SANDBOX_WORKING_DIR,)custom_image = MagicMock(name="custom_image")@@ -2578,7 +2572,7 @@ class TestLaunchDevStackBootstrap:vm: bool = True,custom_image_name: str | None = "posthog-dev-stack",snapshot_restored: bool = False,- snapshot_kind: Any = None,+ resume_snapshot_external_id: str | None = None,) -> Any:mock_modal_sandbox = MagicMock()mock_modal_sandbox.object_id = "sb-boot"@@ -2587,7 +2581,7 @@ class TestLaunchDevStackBootstrap:vm_runtime=vm,custom_image_name=custom_image_name,snapshot_restored=snapshot_restored,- **({"snapshot_kind": snapshot_kind} if snapshot_kind is not None else {}),+ resume_snapshot_external_id=resume_snapshot_external_id,)with patch.object(ModalSandbox, "_get_app_for_config", return_value=MagicMock()):return ModalSandbox(sandbox=mock_modal_sandbox, config=config)@@ -2659,7 +2653,7 @@ class TestLaunchDevStackBootstrap:# Directory resumes only mount the workspace dir, so /usr/local/bin's helper is# still the vetted image's own binary — the warmup must not be lost on the# primary resume path.- sandbox = self._sandbox(snapshot_restored=True, snapshot_kind=SNAPSHOT_KIND_DIRECTORY)+ sandbox = self._sandbox(snapshot_restored=True, resume_snapshot_external_id="im-dir")result = ExecutionResult(stdout="", stderr="", exit_code=0, error=None)with (@@ -2674,7 +2668,7 @@ class TestLaunchDevStackBootstrap:# launcher must strip the environment (no injected credentials) and pin PATH to# system dirs via an absolute /usr/bin/env, so the helper can never resolve or# run the workspace binary with the run's POSTHOG_PERSONAL_API_KEY / GITHUB_TOKEN.- sandbox = self._sandbox(snapshot_restored=True, snapshot_kind=SNAPSHOT_KIND_DIRECTORY)+ sandbox = self._sandbox(snapshot_restored=True, resume_snapshot_external_id="im-dir")result = ExecutionResult(stdout="", stderr="", exit_code=0, error=None)with (diff --git a/products/tasks/backend/logic/services/tests/test_warm.py b/products/tasks/backend/logic/services/tests/test_warm.pyindex fae1e35080d0..569352141916 100644--- a/products/tasks/backend/logic/services/tests/test_warm.py+++ b/products/tasks/backend/logic/services/tests/test_warm.py@@ -6,6 +6,7 @@ from rest_framework.exceptions import PermissionDenied, Throttledfrom posthog.exceptions import QuotaLimitExceeded+from products.tasks.backend.constants import DEFAULT_SANDBOX_WORKING_DIRfrom products.tasks.backend.logic.services.warm import SandboxWarmer, WarmSourceChangedfrom products.tasks.backend.models import Task, TaskRun@@ -78,7 +79,13 @@ class TestSandboxWarmerWarm(APIBaseTest):def test_rewarms_with_resume_after_terminal_run(self):task = self._task()- terminal = task.create_run(mode="interactive", extra_state={"snapshot_external_id": "snap-1"})+ terminal = task.create_run(+ mode="interactive",+ extra_state={+ "snapshot_external_id": "snap-1",+ "snapshot_mount_path": DEFAULT_SANDBOX_WORKING_DIR,+ },+ )terminal.status = TaskRun.Status.COMPLETEDterminal.save(update_fields=["status"])diff --git a/products/tasks/backend/temporal/__init__.py b/products/tasks/backend/temporal/__init__.pyindex 6b370edfb937..0e039a5e92dc 100644--- a/products/tasks/backend/temporal/__init__.py+++ b/products/tasks/backend/temporal/__init__.py@@ -28,7 +28,6 @@ from .process_task.activities import (execute_task_in_sandbox,forward_pending_user_message,get_sandbox_exit_reason,- get_sandbox_for_repository,get_task_processing_context,inject_fresh_tokens_on_resume,invalidate_resume_snapshot,@@ -91,7 +90,6 @@ ACTIVITIES = [clone_repository_in_sandbox,checkout_branch_in_sandbox,materialize_context_layer_in_sandbox,- get_sandbox_for_repository,execute_task_in_sandbox,run_wizard,forward_pending_user_message,diff --git a/products/tasks/backend/temporal/execute_sandbox/tests/test_execute_sandbox_workflow.py b/products/tasks/backend/temporal/execute_sandbox/tests/test_execute_sandbox_workflow.pyindex 6d753c4d7376..7f74417bbc1b 100644--- a/products/tasks/backend/temporal/execute_sandbox/tests/test_execute_sandbox_workflow.py+++ b/products/tasks/backend/temporal/execute_sandbox/tests/test_execute_sandbox_workflow.py@@ -30,10 +30,8 @@ from products.tasks.backend.temporal.execute_sandbox.workflow import (SandboxEvent,)from products.tasks.backend.temporal.process_task.activities.cleanup_sandbox import CleanupSandboxInput, cleanup_sandbox-from products.tasks.backend.temporal.process_task.activities.get_sandbox_for_repository import (- GetSandboxForRepositoryOutput,-)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 GetSandboxForRepositoryOutputfrom products.tasks.backend.temporal.process_task.activities.send_followup_to_sandbox import STEER_DECLINED_OUTCOMEfrom products.tasks.backend.temporal.process_task.activities.start_agent_server import StartAgentServerOutputfrom products.tasks.backend.temporal.process_task.credential_refresh import (diff --git a/products/tasks/backend/temporal/execute_sandbox/workflow.py b/products/tasks/backend/temporal/execute_sandbox/workflow.pyindex 0112cda5c6e1..21133b674998 100644--- a/products/tasks/backend/temporal/execute_sandbox/workflow.py+++ b/products/tasks/backend/temporal/execute_sandbox/workflow.py@@ -55,9 +55,6 @@ from products.tasks.backend.temporal.process_task.activities.emit_progress_activemit_progress_activity,)from products.tasks.backend.temporal.process_task.activities.forward_pending_message import forward_pending_user_message-from products.tasks.backend.temporal.process_task.activities.get_sandbox_for_repository import (- GetSandboxForRepositoryOutput,-)from products.tasks.backend.temporal.process_task.activities.get_task_processing_context import (GetTaskProcessingContextInput,TaskProcessingContext,@@ -67,6 +64,7 @@ from products.tasks.backend.temporal.process_task.activities.provision_sandbox iCheckoutBranchInSandboxInput,CloneRepositoryInSandboxInput,CreateSandboxForRepositoryInput,+ GetSandboxForRepositoryOutput,InjectFreshTokensOnResumeInput,PrepareSandboxForRepositoryInput,checkout_branch_in_sandbox,diff --git a/products/tasks/backend/temporal/metrics.py b/products/tasks/backend/temporal/metrics.pyindex 56d25d8cd822..7374f3bfb9da 100644--- a/products/tasks/backend/temporal/metrics.py+++ b/products/tasks/backend/temporal/metrics.py@@ -13,6 +13,7 @@ from opentelemetry.trace import Spanfrom temporalio import activity, workflowfrom temporalio.common import MetricMeter+from products.tasks.backend.constants import RESUME_SNAPSHOT_KINDfrom products.tasks.backend.logic.services.model_catalogue import runtime_adapter_forAttributes = dict[str, str | int | float | bool]@@ -235,18 +236,21 @@ def increment_resume_mode(mode: str, *, origin_product: str | None) -> None:pass-def increment_snapshot_usage(- used_snapshot: bool,- *,- snapshot_source: str = "unknown",- snapshot_kind: str = "unknown",-) -> None:+def _snapshot_kind_label(snapshot_source: str) -> str:+ if snapshot_source == "resume":+ return RESUME_SNAPSHOT_KIND+ if snapshot_source == "repository":+ return "filesystem"+ return "none"+++def increment_snapshot_usage(used_snapshot: bool, *, snapshot_source: str = "unknown") -> None:try:meter = _metric_meter({"used_snapshot": _bool_label(used_snapshot),"snapshot_source": snapshot_source,- "snapshot_kind": snapshot_kind,+ "snapshot_kind": _snapshot_kind_label(snapshot_source),})meter.create_counter(@@ -257,12 +261,12 @@ def increment_snapshot_usage(pass-def increment_snapshot_restore(snapshot_source: str, snapshot_kind: str, outcome: str) -> None:+def increment_snapshot_restore(snapshot_source: str, outcome: str) -> None:try:meter = _metric_meter({"snapshot_source": snapshot_source,- "snapshot_kind": snapshot_kind,+ "snapshot_kind": _snapshot_kind_label(snapshot_source),"outcome": outcome,})@@ -274,9 +278,9 @@ def increment_snapshot_restore(snapshot_source: str, snapshot_kind: str, outcomepass-def increment_snapshot_create(snapshot_kind: str, outcome: str) -> None:+def increment_snapshot_create(outcome: str) -> None:try:- meter = _metric_meter({"snapshot_kind": snapshot_kind, "outcome": outcome})+ meter = _metric_meter({"snapshot_kind": RESUME_SNAPSHOT_KIND, "outcome": outcome})meter.create_counter("tasks_process_snapshot_create","Resume snapshot creation outcomes for process-task runs",@@ -285,10 +289,10 @@ def increment_snapshot_create(snapshot_kind: str, outcome: str) -> None:pass-def record_snapshot_create_latency_ms(snapshot_kind: str, outcome: str, latency_ms: int) -> None:+def record_snapshot_create_latency_ms(outcome: str, latency_ms: int) -> None:try:delta = dt.timedelta(milliseconds=latency_ms)- _metric_meter({"snapshot_kind": snapshot_kind, "outcome": outcome}).create_histogram_timedelta(+ _metric_meter({"snapshot_kind": RESUME_SNAPSHOT_KIND, "outcome": outcome}).create_histogram_timedelta("tasks_process_snapshot_create_latency","Resume snapshot creation latency for process-task runs",unit="ms",@@ -582,7 +586,7 @@ def record_agent_server_session_init_ms(attributes["runtime"] = runtime_metric_meter(attributes).create_histogram_timedelta("tasks_process_sandbox_step_latency",- "Latency for get_sandbox_for_repository sub-steps",+ "Latency for sandbox provisioning sub-steps",unit="ms",).record(dt.timedelta(milliseconds=session_init_ms))except Exception:@@ -613,7 +617,7 @@ def record_agent_server_step_ms(attributes["runtime"] = runtime_metric_meter(attributes).create_histogram_timedelta("tasks_process_sandbox_step_latency",- "Latency for get_sandbox_for_repository sub-steps",+ "Latency for sandbox provisioning sub-steps",unit="ms",).record(dt.timedelta(milliseconds=duration_ms))except Exception:@@ -819,7 +823,7 @@ class StepTimer(ContextDecorator):try:_metric_meter(attributes).create_histogram_timedelta("tasks_process_sandbox_step_latency",- "Latency for get_sandbox_for_repository sub-steps",+ "Latency for sandbox provisioning sub-steps",unit="ms",).record(delta)except Exception:diff --git a/products/tasks/backend/temporal/process_task/README.md b/products/tasks/backend/temporal/process_task/README.mdindex bdcf864de4ac..bf9c3f6bba66 100644--- a/products/tasks/backend/temporal/process_task/README.md+++ b/products/tasks/backend/temporal/process_task/README.md@@ -54,7 +54,7 @@ For combined task creation, run-start responses, and MCP tools, see [Create task### PostHog API-`backend/presentation/views/api.py` (thin viewsets) over `backend/facade/api.py` (behavior) — every user-triggered cloud launch path, including prewarming, checks server-side PostHog Desktop access before provisioning or activating a run. Composer prewarming carries the complete ordered repository selection, so single- and multi-repository submissions can reuse only a matching sandbox. A terminal task can also prewarm a successor through `POST .../tasks/{id}/warm/`; the source run must remain the task's latest terminal run, and the normal `run` request activates that successor with `resume_from_run_id`. Runtime, model, branch, permission mode, and sandbox configuration must match; reasoning effort may change and is applied before the warmed agent's first turn. Full-filesystem resume snapshots bundle their agent binary, so prewarming probes for the deferred-resume capability and falls back to a fresh sandbox when an old snapshot lacks it. `TaskViewSet.run` creates a `TaskRun` (status=QUEUED) and starts the Temporal workflow. `TaskRunViewSet.partial_update` handles status transitions and signals the Temporal workflow on terminal statuses via `signal_workflow_completion`. `TaskRunViewSet.cancel` (`POST .../runs/{id}/cancel/`) is the user-facing kill switch: `cancel_task_run` interrupts the in-flight agent turn, signals `complete_task("cancelled")` so the workflow snapshots the session and tears down the sandbox, and falls back to finalizing the run directly when no workflow is running.+`backend/presentation/views/api.py` (thin viewsets) over `backend/facade/api.py` (behavior) — every user-triggered cloud launch path, including prewarming, checks server-side PostHog Desktop access before provisioning or activating a run. Composer prewarming carries the complete ordered repository selection, so single- and multi-repository submissions can reuse only a matching sandbox. A terminal task can also prewarm a successor through `POST .../tasks/{id}/warm/`; the source run must remain the task's latest terminal run, and the normal `run` request activates that successor with `resume_from_run_id`. Runtime, model, branch, permission mode, and sandbox configuration must match; reasoning effort may change and is applied before the warmed agent's first turn. Repository snapshots bundle their agent binary, so prewarming probes for the deferred-resume capability and falls back to a fresh sandbox when an old snapshot lacks it. `TaskViewSet.run` creates a `TaskRun` (status=QUEUED) and starts the Temporal workflow. `TaskRunViewSet.partial_update` handles status transitions and signals the Temporal workflow on terminal statuses via `signal_workflow_completion`. `TaskRunViewSet.cancel` (`POST .../runs/{id}/cancel/`) is the user-facing kill switch: `cancel_task_run` interrupts the in-flight agent turn, signals `complete_task("cancelled")` so the workflow snapshots the session and tears down the sandbox, and falls back to finalizing the run directly when no workflow is running.### Temporal workflow@@ -232,17 +232,17 @@ Desktop and backend use the same flag; a stale client cannot bypass the backend## Sandbox providers-| | DockerSandbox | ModalSandbox |-| ----------------- | -------------------------------------------------------------- | --------------------------------------------------------------------- |-| Use case | Local development | Production |-| Internal port | 47821 | 8080 |-| Host port | Dynamically assigned | N/A (cloud-routed) |-| Isolation | Standard Docker container | gVisor kernel-level sandboxing |-| Auth | No token needed | Modal connect token |-| Image source | Local Dockerfile build | `ghcr.io/posthog/posthog-sandbox-base` |-| Snapshots | Docker commit/tag | Modal `snapshot_filesystem()` |-| URL rewriting | Auto (`localhost` → `host.docker.internal`, `:8010` → `:8000`) | None (uses `SANDBOX_API_URL` or ngrok) |-| `SANDBOX_API_URL` | Not needed (auto-transform handles it) | Only for local dev with Modal (ngrok URL); production uses `SITE_URL` |+| | DockerSandbox | ModalSandbox |+| ----------------- | -------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |+| Use case | Local development | Production |+| Internal port | 47821 | 8080 |+| Host port | Dynamically assigned | N/A (cloud-routed) |+| Isolation | Standard Docker container | gVisor kernel-level sandboxing |+| Auth | No token needed | Modal connect token |+| Image source | Local Dockerfile build | `ghcr.io/posthog/posthog-sandbox-base` |+| Snapshots | Docker commit/tag | Modal `snapshot_directory()` for resume; `snapshot_filesystem()` for repository and published images |+| URL rewriting | Auto (`localhost` → `host.docker.internal`, `:8010` → `:8000`) | None (uses `SANDBOX_API_URL` or ngrok) |+| `SANDBOX_API_URL` | Not needed (auto-transform handles it) | Only for local dev with Modal (ngrok URL); production uses `SITE_URL` |### Local development with Dockerdiff --git a/products/tasks/backend/temporal/process_task/activities/__init__.py b/products/tasks/backend/temporal/process_task/activities/__init__.pyindex 98e0e4407a19..f908264419a5 100644--- a/products/tasks/backend/temporal/process_task/activities/__init__.py+++ b/products/tasks/backend/temporal/process_task/activities/__init__.py@@ -5,11 +5,6 @@ from .enforce_self_driving_quota import EnforceSelfDrivingRunQuotaInput, enforcefrom .execute_task_in_sandbox import ExecuteTaskInput, ExecuteTaskOutput, execute_task_in_sandboxfrom .forward_pending_message import forward_pending_user_messagefrom .get_sandbox_exit_reason import GetSandboxExitReasonInput, get_sandbox_exit_reason-from .get_sandbox_for_repository import (- GetSandboxForRepositoryInput,- GetSandboxForRepositoryOutput,- get_sandbox_for_repository,-)from .get_task_processing_context import TaskProcessingContext, get_task_processing_contextfrom .materialize_context_layer import (MaterializeContextLayerInput,@@ -24,6 +19,7 @@ from .provision_sandbox import (CloneRepositoryInSandboxOutput,CreateSandboxForRepositoryInput,CreateSandboxForRepositoryOutput,+ GetSandboxForRepositoryOutput,InjectFreshTokensOnResumeInput,InvalidateResumeSnapshotInput,PrepareSandboxForRepositoryInput,@@ -89,7 +85,6 @@ __all__ = ["EmitProgressInput","ExecuteTaskInput","ExecuteTaskOutput",- "GetSandboxForRepositoryInput","GetSandboxForRepositoryOutput","CheckoutBranchInSandboxInput","CheckoutBranchInSandboxOutput",@@ -141,7 +136,6 @@ __all__ = ["send_permission_denial_guidance","send_permission_response_to_sandbox","send_followup_to_sandbox",- "get_sandbox_for_repository","get_task_processing_context","inject_fresh_tokens_on_resume","restore_sandbox_connection_state",diff --git a/products/tasks/backend/temporal/process_task/activities/create_resume_snapshot.py b/products/tasks/backend/temporal/process_task/activities/create_resume_snapshot.pyindex fe6280f56994..56133b4f9f6e 100644--- a/products/tasks/backend/temporal/process_task/activities/create_resume_snapshot.py+++ b/products/tasks/backend/temporal/process_task/activities/create_resume_snapshot.py@@ -6,11 +6,7 @@ from temporalio import activityfrom posthog.temporal.common.utils import asyncify-from products.tasks.backend.constants import (- DEFAULT_DIRECTORY_RESUME_SNAPSHOT_MOUNT_PATH,- SNAPSHOT_KIND_DIRECTORY,- SnapshotKind,-)+from products.tasks.backend.constants import DEFAULT_DIRECTORY_RESUME_SNAPSHOT_MOUNT_PATH, RESUME_SNAPSHOT_KINDfrom products.tasks.backend.exceptions import (SandboxNotRunningError,SnapshotFileLimitExceededError,@@ -28,17 +24,14 @@ logger = structlog.get_logger(__name__)class CreateResumeSnapshotInput:sandbox_id: strrun_id: str- # Retained for Temporal payload compatibility while existing workflows drain.- use_directory_snapshot: bool = Truesnapshot_mount_path: str = DEFAULT_DIRECTORY_RESUME_SNAPSHOT_MOUNT_PATHreason: str = "teardown"allow_pruning: bool = True-@dataclass+@dataclass(frozen=True)class CreateResumeSnapshotOutput:external_id: str | None- snapshot_kind: SnapshotKind | None = Nonesnapshot_mount_path: str | None = Noneerror: str | None = Noneduration_ms: int | None = None@@ -53,7 +46,6 @@ def create_resume_snapshot(input: CreateResumeSnapshotInput) -> CreateResumeSnapAPI can look it up when resuming."""SandboxClass = get_sandbox_class()- snapshot_kind: SnapshotKind = SNAPSHOT_KIND_DIRECTORYsnapshot_mount_path = input.snapshot_mount_path or DEFAULT_DIRECTORY_RESUME_SNAPSHOT_MOUNT_PATHlogger.info(@@ -61,7 +53,6 @@ def create_resume_snapshot(input: CreateResumeSnapshotInput) -> CreateResumeSnapsandbox_id=input.sandbox_id,run_id=input.run_id,reason=input.reason,- snapshot_kind=snapshot_kind,)started_at = time.perf_counter()@@ -74,17 +65,15 @@ def create_resume_snapshot(input: CreateResumeSnapshotInput) -> CreateResumeSnapsandbox_id=input.sandbox_id,run_id=input.run_id,reason=input.reason,- snapshot_kind=snapshot_kind,duration_ms=duration_ms,error=str(e),)outcome = "sandbox_not_found"- increment_snapshot_create(snapshot_kind, outcome)- record_snapshot_create_latency_ms(snapshot_kind, outcome, duration_ms)+ increment_snapshot_create(outcome)+ record_snapshot_create_latency_ms(outcome, duration_ms)emit_agent_log(input.run_id, "debug", f"Resume snapshot failed ({input.reason}): sandbox not found")return CreateResumeSnapshotOutput(external_id=None,- snapshot_kind=snapshot_kind,error=f"Sandbox not found: {e}",duration_ms=duration_ms,)@@ -97,15 +86,13 @@ def create_resume_snapshot(input: CreateResumeSnapshotInput) -> CreateResumeSnapsandbox_id=input.sandbox_id,run_id=input.run_id,reason=input.reason,- snapshot_kind=snapshot_kind,duration_ms=duration_ms,)- increment_snapshot_create(snapshot_kind, outcome)- record_snapshot_create_latency_ms(snapshot_kind, outcome, duration_ms)+ increment_snapshot_create(outcome)+ record_snapshot_create_latency_ms(outcome, duration_ms)emit_agent_log(input.run_id, "debug", f"Resume snapshot failed ({input.reason}): sandbox not running")return CreateResumeSnapshotOutput(external_id=None,- snapshot_kind=snapshot_kind,error="Sandbox not running",duration_ms=duration_ms,)@@ -121,16 +108,14 @@ def create_resume_snapshot(input: CreateResumeSnapshotInput) -> CreateResumeSnapsandbox_id=input.sandbox_id,run_id=input.run_id,reason=input.reason,- snapshot_kind=snapshot_kind,duration_ms=duration_ms,error=str(e),)- increment_snapshot_create(snapshot_kind, outcome)- record_snapshot_create_latency_ms(snapshot_kind, outcome, duration_ms)+ increment_snapshot_create(outcome)+ record_snapshot_create_latency_ms(outcome, duration_ms)emit_agent_log(input.run_id, "debug", f"Resume snapshot failed ({input.reason}): file limit exceeded")return CreateResumeSnapshotOutput(external_id=None,- snapshot_kind=snapshot_kind,error=str(e),duration_ms=duration_ms,)@@ -142,12 +127,11 @@ def create_resume_snapshot(input: CreateResumeSnapshotInput) -> CreateResumeSnapsandbox_id=input.sandbox_id,run_id=input.run_id,reason=input.reason,- snapshot_kind=snapshot_kind,duration_ms=duration_ms,error=str(e),)- increment_snapshot_create(snapshot_kind, outcome)- record_snapshot_create_latency_ms(snapshot_kind, outcome, duration_ms)+ increment_snapshot_create(outcome)+ record_snapshot_create_latency_ms(outcome, duration_ms)raiseexcept SandboxNotRunningError as e:outcome = "sandbox_not_running"@@ -157,16 +141,14 @@ def create_resume_snapshot(input: CreateResumeSnapshotInput) -> CreateResumeSnapsandbox_id=input.sandbox_id,run_id=input.run_id,reason=input.reason,- snapshot_kind=snapshot_kind,duration_ms=duration_ms,error=str(e),)- increment_snapshot_create(snapshot_kind, outcome)- record_snapshot_create_latency_ms(snapshot_kind, outcome, duration_ms)+ increment_snapshot_create(outcome)+ record_snapshot_create_latency_ms(outcome, duration_ms)emit_agent_log(input.run_id, "debug", f"Resume snapshot failed ({input.reason}): sandbox not running")return CreateResumeSnapshotOutput(external_id=None,- snapshot_kind=snapshot_kind,error=str(e),duration_ms=duration_ms,)@@ -178,16 +160,14 @@ def create_resume_snapshot(input: CreateResumeSnapshotInput) -> CreateResumeSnapsandbox_id=input.sandbox_id,run_id=input.run_id,reason=input.reason,- snapshot_kind=snapshot_kind,duration_ms=duration_ms,error=str(e),)- increment_snapshot_create(snapshot_kind, outcome)- record_snapshot_create_latency_ms(snapshot_kind, outcome, duration_ms)+ increment_snapshot_create(outcome)+ record_snapshot_create_latency_ms(outcome, duration_ms)emit_agent_log(input.run_id, "debug", f"Resume snapshot failed ({input.reason}): {e}")return CreateResumeSnapshotOutput(external_id=None,- snapshot_kind=snapshot_kind,error=str(e),duration_ms=duration_ms,)@@ -195,7 +175,7 @@ def create_resume_snapshot(input: CreateResumeSnapshotInput) -> CreateResumeSnaptry:updates = {"snapshot_external_id": external_id,- "snapshot_kind": snapshot_kind,+ "snapshot_kind": RESUME_SNAPSHOT_KIND,"snapshot_mount_path": snapshot_mount_path,}TaskRun.update_state_atomic(input.run_id, updates=updates)@@ -207,31 +187,28 @@ def create_resume_snapshot(input: CreateResumeSnapshotInput) -> CreateResumeSnapsandbox_id=input.sandbox_id,run_id=input.run_id,reason=input.reason,- snapshot_kind=snapshot_kind,snapshot_external_id=external_id,duration_ms=duration_ms,error=str(e),)- increment_snapshot_create(snapshot_kind, outcome)- record_snapshot_create_latency_ms(snapshot_kind, outcome, duration_ms)+ increment_snapshot_create(outcome)+ record_snapshot_create_latency_ms(outcome, duration_ms)emit_agent_log(input.run_id, "debug", f"Resume snapshot failed ({input.reason}): state could not be saved")return CreateResumeSnapshotOutput(external_id=None,- snapshot_kind=snapshot_kind,error=f"Snapshot state could not be saved: {e}",duration_ms=duration_ms,)duration_ms = int((time.perf_counter() - started_at) * 1000)- increment_snapshot_create(snapshot_kind, outcome)- record_snapshot_create_latency_ms(snapshot_kind, outcome, duration_ms)+ increment_snapshot_create(outcome)+ record_snapshot_create_latency_ms(outcome, duration_ms)logger.info("create_resume_snapshot_created",sandbox_id=input.sandbox_id,run_id=input.run_id,reason=input.reason,- snapshot_kind=snapshot_kind,snapshot_external_id=external_id,snapshot_mount_path=snapshot_mount_path,duration_ms=duration_ms,@@ -239,7 +216,6 @@ def create_resume_snapshot(input: CreateResumeSnapshotInput) -> CreateResumeSnapemit_agent_log(input.run_id, "debug", f"Resume snapshot created ({input.reason}): {external_id}")return CreateResumeSnapshotOutput(external_id=external_id,- snapshot_kind=snapshot_kind,snapshot_mount_path=snapshot_mount_path,duration_ms=duration_ms,)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.pydeleted file mode 100644index 2c1d4398da7b..000000000000--- a/products/tasks/backend/temporal/process_task/activities/get_sandbox_for_repository.py+++ /dev/null@@ -1,456 +0,0 @@-import shlex-import logging-from dataclasses import dataclass--from django.conf import settings-from django.utils import timezone--from temporalio import activity--from posthog.dataclasses import frozen-from posthog.models.user_integration import ReauthorizationRequired-from posthog.temporal.common.utils import asyncify--from products.tasks.backend.constants import SNAPSHOT_KIND_FILESYSTEM, filter_user_sandbox_env_vars-from products.tasks.backend.exceptions import (- CredentialUnavailableError,- GitHubAuthenticationError,- OAuthTokenError,- TaskNotFoundError,-)-from products.tasks.backend.logic.services.connection_token import get_sandbox_jwt_public_key-from products.tasks.backend.logic.services.sandbox import (- Sandbox,- SandboxConfig,- SandboxTemplate,- needs_full_history,- parse_sandbox_repo_mount_map,- workload_for_origin_product,-)-from products.tasks.backend.models import SandboxSnapshot, Task, TaskRun-from products.tasks.backend.temporal.metrics import (- StepTimer,- increment_snapshot_restore,- increment_snapshot_usage,- modal_sandbox_backend_label,- sandbox_runtime_label,-)-from products.tasks.backend.temporal.oauth import create_oauth_access_token_for_run-from products.tasks.backend.temporal.observability import emit_agent_log, log_activity_execution-from products.tasks.backend.temporal.process_task.sandbox_connection import persist_sandbox_connection-from products.tasks.backend.temporal.process_task.utils import (- get_git_identity_env_vars,- get_sandbox_api_url,- get_sandbox_github_token,- get_sandbox_name_for_task,- get_sandbox_snapshot_metadata,- get_task_run_credential_user,- mcp_exec_skills_env_vars,- parse_run_state,- run_gateway_env_vars,-)--from .get_task_processing_context import TaskProcessingContext--logger = logging.getLogger(__name__)---def _get_image_source_label(- *,- has_repo: bool,- 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}"-- if snapshot is not None:- 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"-- if provider and provider.upper() in ("MODAL_DOCKER", "MODAL_EVALS"):- return "local Modal Dockerfile build"-- if settings.DEBUG and not has_repo:- return "local debug sandbox image"-- return "published sandbox base image"---def _emit_provisioning_diagnostics(ctx: TaskProcessingContext, sandbox: object) -> None:- diagnostics = getattr(sandbox, "provision_diagnostics", None)- if diagnostics is None:- return-- summary_lines = getattr(diagnostics, "summary_lines", None) or []- if summary_lines:- emit_agent_log(- ctx.run_id,- "debug",- "Sandbox image build summary:\n" + "\n".join(f"- {line}" for line in summary_lines),- )-- raw_excerpt = getattr(diagnostics, "raw_excerpt", None)- if raw_excerpt:- emit_agent_log(ctx.run_id, "debug", f"Sandbox image build logs:\n{raw_excerpt}")---def _build_environment_variables(- ctx: TaskProcessingContext, task: Task, github_token: str, access_token: str-) -> dict[str, str]:- environment_variables = {- "POSTHOG_PERSONAL_API_KEY": access_token,- "POSTHOG_API_URL": get_sandbox_api_url(),- "POSTHOG_PROJECT_ID": str(ctx.team_id),- "JWT_PUBLIC_KEY": get_sandbox_jwt_public_key(),- }-- if ctx.sandbox_environment_id:- sandbox_environment = ctx.get_sandbox_environment()- if sandbox_environment and sandbox_environment.environment_variables:- safe_vars, skipped_keys = filter_user_sandbox_env_vars(sandbox_environment.environment_variables)- environment_variables.update(safe_vars)-- emit_agent_log(- ctx.run_id,- "debug",- f"Applied {len(safe_vars)} sandbox environment variable(s) from '{sandbox_environment.name}'",- )- if skipped_keys:- emit_agent_log(- ctx.run_id,- "debug",- f"Skipped reserved/blocked sandbox environment variable keys from '{sandbox_environment.name}': {', '.join(sorted(skipped_keys))}",- )-- if github_token:- environment_variables["GITHUB_TOKEN"] = github_token- environment_variables["GH_TOKEN"] = github_token-- if settings.SANDBOX_LLM_GATEWAY_URL:- environment_variables["LLM_GATEWAY_URL"] = settings.SANDBOX_LLM_GATEWAY_URL-- environment_variables.update(run_gateway_env_vars(ctx, task))- environment_variables.update(mcp_exec_skills_env_vars(ctx))- return environment_variables---@dataclass-class GetSandboxForRepositoryInput:- context: TaskProcessingContext---@frozen-class GetSandboxForRepositoryOutput:- sandbox_id: str- sandbox_url: str- connect_token: str | None- used_snapshot: bool- should_create_snapshot: bool- jwt_kid: str | None = None- agent_server_launched: bool = False- boot_path: str = "classic"- image_source: str | None = None- # Per-phase boot durations, threaded through to the sandbox_started analytics event.- create_ms: int | None = None- clone_ms: int | None = None- checkout_ms: int | None = None- launch_ms: int | None = None- agent_prepare_ms: int | None = None- agent_invoke_ms: int | None = None- dev_stack_preview_sized: bool = False- agent_shadow_launched: bool = False---@asyncify-def get_sandbox_for_repository(input: GetSandboxForRepositoryInput) -> GetSandboxForRepositoryOutput:- ctx = input.context-- with log_activity_execution(- "get_sandbox_for_repository",- **ctx.to_log_context(),- ):- has_repo = ctx.repository is not None- repository: str | None = ctx.repository- github_integration_id: int | None = ctx.github_integration_id-- snapshot = None- used_snapshot = False- snapshot_source = "none"- snapshot_kind = SNAPSHOT_KIND_FILESYSTEM- snapshot_mount_path: str | None = 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",- origin_product=ctx.origin_product,- runtime=sandbox_runtime_label(ctx.use_modal_vm_sandbox),- ) as snapshot_lookup_timer:- snapshot = SandboxSnapshot.get_latest_snapshot_with_repos(github_integration_id, [repository])- used_snapshot = snapshot is not None- snapshot_lookup_timer.set_used_snapshot(used_snapshot)- if snapshot is not None:- snapshot_metadata = get_sandbox_snapshot_metadata(snapshot)- if not snapshot_metadata.is_usable:- snapshot = None- used_snapshot = False- else:- snapshot_source = "repository"- snapshot_kind = snapshot_metadata.kind- snapshot_mount_path = snapshot_metadata.mount_path- elif not has_repo:- emit_agent_log(ctx.run_id, "debug", "Creating environment without repository")-- try:- task = Task.objects.select_related("created_by", "github_integration", "github_user_integration").get(- id=ctx.task_id- )- except Task.DoesNotExist as e:- raise TaskNotFoundError(f"Task {ctx.task_id} not found", {"task_id": ctx.task_id}, cause=e)-- # Signal report research and pinned Signals scouts need full history for git log and git blame.- # All other sandboxes use shallow clone (--depth 1) for faster boot.- shallow = not needs_full_history(task.origin_product)-- actor_user = get_task_run_credential_user(task, ctx.state)- github_token = ""- should_inject_github_token = ctx.has_github_credentials and (- has_repo or ctx.github_user_integration_id is not None or ctx.github_integration_id is not None- )- if should_inject_github_token:- try:- github_token = (- get_sandbox_github_token(- github_integration_id,- run_id=ctx.run_id,- state=ctx.state,- task=task,- actor_user=actor_user,- github_user_integration_id=ctx.github_user_integration_id,- repository=repository,- )- or ""- )- except ReauthorizationRequired as e:- raise CredentialUnavailableError(- "GitHub user integration for this run requires reauthorization",- {"github_integration_id": github_integration_id, "task_id": ctx.task_id},- cause=e,- )- except Exception as e:- raise GitHubAuthenticationError(- f"Failed to get GitHub token for integration {github_integration_id}",- {"github_integration_id": github_integration_id, "task_id": ctx.task_id, "error": str(e)},- cause=e,- )-- try:- access_token = create_oauth_access_token_for_run(task, ctx.state, run_id=ctx.run_id)- except Exception as e:- raise OAuthTokenError(- f"Failed to create OAuth access token for task {ctx.task_id}",- {"task_id": ctx.task_id, "error": str(e)},- cause=e,- )-- environment_variables = _build_environment_variables(ctx, task, github_token, access_token)-- environment_variables.update(get_git_identity_env_vars(task, ctx.state))-- run_state = parse_run_state(ctx.state)-- # Set resume run ID independently of snapshot so conversation history- # can be rebuilt from logs even when the filesystem snapshot has expired.- if run_state.resume_from_run_id:- environment_variables["POSTHOG_RESUME_RUN_ID"] = run_state.resume_from_run_id- elif run_state.same_run_resume:- environment_variables["POSTHOG_RESUME_RUN_ID"] = str(ctx.run_id)-- # Check for resume snapshot (takes priority over integration-level snapshots)- resume_snapshot_ext_id = run_state.snapshot_external_id- if resume_snapshot_ext_id:- if not run_state.resume_snapshot_is_usable():- emit_agent_log(- ctx.run_id,- "debug",- "Previous session snapshot is unusable; resuming with a fresh sandbox",- )- resume_snapshot_ext_id = None- else:- used_snapshot = True- snapshot_source = "resume"- snapshot_kind = run_state.resume_snapshot_kind()- 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:- emit_agent_log(ctx.run_id, "debug", f"Resuming environment from snapshot for {repository}")- elif has_repo and used_snapshot:- emit_agent_log(ctx.run_id, "debug", f"Found existing environment for {repository}")- elif has_repo:- emit_agent_log(ctx.run_id, "debug", f"Creating environment from {image_source_label} for {repository}")- else:- emit_agent_log(ctx.run_id, "debug", f"Creating environment from {image_source_label}")-- config = SandboxConfig(- name=get_sandbox_name_for_task(ctx.task_id),- template=SandboxTemplate.VM_BASE if use_vm_sandbox else SandboxTemplate.DEFAULT_BASE,- workload=workload_for_origin_product(ctx.origin_product),- 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,- snapshot_mount_path=snapshot_mount_path,- snapshot_source=snapshot_source,- snapshot_id=str(snapshot.id) if snapshot and not resume_snapshot_ext_id else None,- metadata={"task_id": ctx.task_id},- **ctx.sandbox_resource_overrides(),- )-- emit_agent_log(- ctx.run_id,- "debug",- f"Provisioning sandbox from {image_source_label} (image build may take a few minutes on first run)",- )- runtime = sandbox_runtime_label(use_vm_sandbox)- with StepTimer(- "sandbox_creation",- used_snapshot=used_snapshot,- origin_product=ctx.origin_product,- runtime=runtime,- sandbox_backend=modal_sandbox_backend_label(),- ) as sandbox_creation_timer:- sandbox = Sandbox.create(config)- # The provider's TTL clock starts here — the usage ledger anchors its- # kill deadline on this boundary, not on when the row is opened below.- sandbox_created_at = timezone.now()- used_snapshot = bool((resume_snapshot_ext_id or snapshot) and sandbox.config.snapshot_restored)- sandbox_creation_timer.set_used_snapshot(used_snapshot)- if not sandbox.start_cpu_billing_sampler():- activity.logger.warning("Failed to start sandbox CPU billing sampler", extra={"sandbox_id": sandbox.id})- if sandbox.config.image_fallback:- emit_agent_log(ctx.run_id, "warn", f"Sandbox image downgraded: {sandbox.config.image_fallback}")- if sandbox.launch_dev_stack_bootstrap():- emit_agent_log(- ctx.run_id, "debug", "Warming the prebaked dev stack in the background (compose host aliases + dockerd)"- )- snapshot_outcome = "used" if used_snapshot else "fresh" if snapshot_source == "none" else "fallback"- metrics_snapshot_kind = snapshot_kind if snapshot_source != "none" else "none"- increment_snapshot_usage(used_snapshot, snapshot_source=snapshot_source, snapshot_kind=metrics_snapshot_kind)- increment_snapshot_restore(snapshot_source, metrics_snapshot_kind, snapshot_outcome)- _emit_provisioning_diagnostics(ctx, sandbox)- emit_agent_log(ctx.run_id, "debug", f"Sandbox provisioned: {sandbox.id}")-- if has_repo and not used_snapshot:- assert repository is not None- local_bind = parse_sandbox_repo_mount_map().get(repository.lower())- # Bind mounts are only applied for Docker sandboxes; Modal ignores SANDBOX_REPO_MOUNT_MAP.- if local_bind is not None and getattr(settings, "SANDBOX_PROVIDER", None) == "docker":- emit_agent_log(- ctx.run_id,- "debug",- f"Using local checkout for {repository} at {local_bind} (SANDBOX_REPO_MOUNT_MAP); skipping clone from GitHub",- )- else:- emit_agent_log(ctx.run_id, "debug", f"Cloning {repository} into sandbox")- with StepTimer(- "repository_clone",- used_snapshot=used_snapshot,- origin_product=ctx.origin_product,- runtime=runtime,- ):- clone_result = sandbox.clone_repository(repository, github_token=github_token, shallow=shallow)- if clone_result.exit_code != 0:- sandbox.destroy()- raise RuntimeError(f"Failed to clone repository {repository}: {clone_result.stderr}")-- if has_repo and ctx.branch:- assert repository is not None- emit_agent_log(ctx.run_id, "debug", f"Checking out branch {ctx.branch}")- org, repo = repository.lower().split("/")- repo_path = f"/tmp/workspace/repos/{org}/{repo}"-- # For snapshot-based sandboxes, update the remote URL with the fresh token- # since the snapshotted .git/config may contain an expired token.- if used_snapshot and github_token:- update_remote = (- f"cd {shlex.quote(repo_path)} && "- f"git remote set-url origin https://x-access-token:{shlex.quote(github_token)}@github.com/{shlex.quote(repository)}.git"- )- update_result = sandbox.execute(update_remote, timeout_seconds=30)- if update_result.exit_code != 0:- logger.warning(- "Failed to update remote URL for snapshot",- extra={"branch": ctx.branch, "stderr": update_result.stderr},- )-- depth_flag = f" --depth {shlex.quote('1')}" if shallow else ""- fetch_and_checkout = (- f"cd {shlex.quote(repo_path)} && "- f"git fetch{depth_flag} origin -- {shlex.quote(ctx.branch)} && "- f"git checkout -B {shlex.quote(ctx.branch)} FETCH_HEAD"- )- try:- result = sandbox.execute(fetch_and_checkout, timeout_seconds=5 * 60)- except Exception:- sandbox.destroy()- raise- if result.exit_code != 0:- sandbox.destroy()- logger.warning("Branch checkout failed", extra={"branch": ctx.branch, "stderr": result.stderr})- raise RuntimeError(f"Failed to checkout branch {ctx.branch}")-- credentials = sandbox.get_connect_credentials()-- try:- with StepTimer(- "persist_sandbox_connection",- origin_product=ctx.origin_product,- runtime=runtime,- used_snapshot=used_snapshot,- boot_path="classic",- ):- persist_sandbox_connection(- run_id=ctx.run_id,- sandbox=sandbox,- credentials=credentials,- sandbox_created_at=sandbox_created_at,- task_runtime=ctx.task_runtime,- )- except Exception:- try:- sandbox.destroy()- finally:- TaskRun.clear_sandbox_connection_state_atomic(ctx.run_id, sandbox.id)- raise-- activity.logger.info(f"Created sandbox {sandbox.id} (used_snapshot={used_snapshot})")-- return GetSandboxForRepositoryOutput(- sandbox_id=sandbox.id,- sandbox_url=credentials.url,- connect_token=credentials.token,- used_snapshot=used_snapshot,- should_create_snapshot=not used_snapshot,- )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.pyindex e1f973d6da5e..8aa7ac60e3be 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@@ -131,10 +131,8 @@ class TaskProcessingContext:json_schema: dict | None = Noneci_prompt: str | None = None# Captured at workflow start so snapshot creation is deterministic across- # activity retries. This means "create any Modal resume snapshot"; filesystem- # snapshots are guarded by the legacy setting, directory snapshots by feature flag.+ # activity retries.use_modal_resume_snapshots: bool = True- use_modal_directory_resume_snapshots: bool = True # Temporal payload compatibility# Captured at workflow start so the sandbox event transport branch is# deterministic for the full run.sandbox_event_ingest_enabled: bool = False@@ -1304,7 +1302,6 @@ def get_task_processing_context(input: GetTaskProcessingContextInput) -> TaskProci_prompt=task.ci_prompt,# Hogland has no resume-snapshot support yet; every run cold-boots there.use_modal_resume_snapshots=sandbox_backend != "hogland",- use_modal_directory_resume_snapshots=sandbox_backend != "hogland",sandbox_event_ingest_enabled=sandbox_event_ingest_enabled,sandbox_rotation_enabled=True,agent_otel_telemetry_enabled=agent_otel_telemetry_enabled,diff --git a/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py b/products/tasks/backend/temporal/process_task/activities/provision_sandbox.pyindex 1f7d221e64a5..cbfc92c98eb0 100644--- a/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py+++ b/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py@@ -23,12 +23,7 @@ from posthog.models.user_integration import ReauthorizationRequiredfrom posthog.temporal.common.utils import asyncifyfrom products.context_layer.backend.facade import api as context_layer_facade-from products.tasks.backend.constants import (- DEV_STACK_IMAGE_NAME,- SNAPSHOT_KIND_DIRECTORY,- SNAPSHOT_KIND_FILESYSTEM,- filter_user_sandbox_env_vars,-)+from products.tasks.backend.constants import DEV_STACK_IMAGE_NAME, filter_user_sandbox_env_varsfrom products.tasks.backend.exceptions import (ComputeBillingLimitError,CredentialUnavailableError,@@ -108,7 +103,6 @@ from products.tasks.backend.temporal.process_task.utils import (get_sandbox_github_token,get_sandbox_name_for_task,get_sandbox_otel_env_vars,- get_sandbox_snapshot_metadata,get_task_run_credential_user,installation_confirms_repository_denied,mcp_exec_skills_env_vars,@@ -137,7 +131,7 @@ class PrepareSandboxForRepositoryInput:context: TaskProcessingContext-@dataclass+@dataclass(frozen=True)class PrepareSandboxForRepositoryOutput:sandbox_name: strrepository: str | None@@ -151,7 +145,6 @@ class PrepareSandboxForRepositoryOutput:shallow_clone: boolimage_source: strimage_source_label: str- snapshot_kind: str = SNAPSHOT_KIND_FILESYSTEMsnapshot_mount_path: str | None = Nonesnapshot_source: str = "none"sandbox_creation_timeout_seconds: int = 300@@ -176,6 +169,27 @@ class CreateSandboxForRepositoryOutput:dev_stack_preview_sized: bool = False+@frozen+class GetSandboxForRepositoryOutput:+ sandbox_id: str+ sandbox_url: str+ connect_token: str | None+ used_snapshot: bool+ should_create_snapshot: bool+ jwt_kid: str | None = None+ agent_server_launched: bool = False+ boot_path: str = "classic"+ image_source: str | None = None+ create_ms: int | None = None+ clone_ms: int | None = None+ checkout_ms: int | None = None+ launch_ms: int | None = None+ agent_prepare_ms: int | None = None+ agent_invoke_ms: int | None = None+ dev_stack_preview_sized: bool = False+ agent_shadow_launched: bool = False++@dataclassclass CloneRepositoryInSandboxOutput:clone_ms: int | None = None@@ -354,9 +368,9 @@ def _prewarmed_resume_needs_fresh_agent() -> bool:"""Whether a restored full snapshot bundled an agent that cannot idle before the resumed prompt.- A repository snapshot (``snapshot_id``) restores the same filesystem as a resume snapshot- (``snapshot_external_id``), so it supplies the snapshot's own agent binary too and needs the- same probe. Only a directory restore keeps the vetted image's agent.+ A repository snapshot (``snapshot_id``) restores a full filesystem, so it supplies the+ snapshot's own agent binary too and needs the same probe. A directory resume keeps the+ vetted image's agent."""# `prewarmedResumeMessageDriven` is an ACP capability, advertised and consumed only by the# ACP agent server. The Pi server dispatches no startup turn and downloads its session@@ -366,8 +380,7 @@ def _prewarmed_resume_needs_fresh_agent(return Falseif (not used_snapshot- or (prepared.snapshot_external_id is None and prepared.snapshot_id is None)- or prepared.snapshot_kind == SNAPSHOT_KIND_DIRECTORY+ or prepared.snapshot_external_id is not Noneor not (ctx.state or {}).get("prewarmed")or not (ctx.state or {}).get("resume_from_run_id")):@@ -713,7 +726,6 @@ def prepare_sandbox_for_repository(input: PrepareSandboxForRepositoryInput) -> Psnapshot = Noneused_snapshot = Falsesnapshot_source = "none"- snapshot_kind = SNAPSHOT_KIND_FILESYSTEMsnapshot_mount_path: str | None = None# Repo-setup snapshots come from default-base sandboxes; restoring one would silently# drop a custom base image or a non-default template. Resume snapshots were taken from@@ -729,14 +741,7 @@ def prepare_sandbox_for_repository(input: PrepareSandboxForRepositoryInput) -> Pused_snapshot = snapshot is not Nonesnapshot_lookup_timer.set_used_snapshot(used_snapshot)if snapshot is not None:- snapshot_metadata = get_sandbox_snapshot_metadata(snapshot)- if not snapshot_metadata.is_usable:- snapshot = None- used_snapshot = False- else:- snapshot_source = "repository"- snapshot_kind = snapshot_metadata.kind- snapshot_mount_path = snapshot_metadata.mount_path+ snapshot_source = "repository"elif not has_repo:log_with_activity_context("Creating environment without repository", run_id=ctx.run_id)@@ -762,9 +767,6 @@ def prepare_sandbox_for_repository(input: PrepareSandboxForRepositoryInput) -> Penvironment_variables = _build_environment_variables(ctx, task, github_token, access_token)- # VM and gVisor both resume from snapshots. A run's stored snapshot kind- # determines the restore mechanism; the rollout flag only chooses the- # kind of new snapshot created after this run.resume_snapshot_external_id = run_state.snapshot_external_idif resume_snapshot_external_id:if not run_state.resume_snapshot_is_usable():@@ -777,7 +779,6 @@ def prepare_sandbox_for_repository(input: PrepareSandboxForRepositoryInput) -> Pelse:used_snapshot = Truesnapshot_source = "resume"- snapshot_kind = run_state.resume_snapshot_kind()snapshot_mount_path = run_state.resume_snapshot_mount_path()is_resume = bool(run_state.same_run_resume or run_state.resume_from_run_id)@@ -795,9 +796,7 @@ def prepare_sandbox_for_repository(input: PrepareSandboxForRepositoryInput) -> P"run_id": ctx.run_id,"resume_mode": resume_mode,"state_snapshot_external_id": run_state.snapshot_external_id,- "state_snapshot_kind": run_state.snapshot_kind,"effective_snapshot_external_id": resume_snapshot_external_id,- "effective_snapshot_kind": snapshot_kind,"effective_snapshot_mount_path": snapshot_mount_path,"same_run_resume": run_state.same_run_resume,"same_run_resume_idle": run_state.same_run_resume_idle,@@ -839,7 +838,6 @@ def prepare_sandbox_for_repository(input: PrepareSandboxForRepositoryInput) -> Pshallow_clone=shallow_clone,image_source=image_source,image_source_label=image_source_label,- snapshot_kind=snapshot_kind,snapshot_mount_path=snapshot_mount_path,snapshot_source=snapshot_source,sandbox_creation_timeout_seconds=sandbox_class.creation_timeout_seconds,@@ -850,7 +848,7 @@ def prepare_sandbox_for_repository(input: PrepareSandboxForRepositoryInput) -> Pdef _pin_governs_sandbox_agent(ctx: TaskProcessingContext, config: SandboxConfig, *, used_snapshot: bool) -> bool:if ctx.sandbox_backend == "hogland" or config.custom_image_name:return False- return not used_snapshot or config.snapshot_kind == SNAPSHOT_KIND_DIRECTORY+ return not used_snapshot or config.resume_snapshot_external_id is not None@StepTimer("sandbox_agent_version")@@ -933,8 +931,7 @@ def _create_sandbox_for_repository(input: CreateSandboxForRepositoryInput) -> Crcustom_image_name=ctx.custom_image_name if use_vm_sandbox else None,environment_variables=prepared.environment_variables,snapshot_id=prepared.snapshot_id,- snapshot_external_id=prepared.snapshot_external_id,- snapshot_kind=prepared.snapshot_kind,+ resume_snapshot_external_id=prepared.snapshot_external_id,snapshot_mount_path=prepared.snapshot_mount_path,snapshot_source=prepared.snapshot_source,metadata=_build_sandbox_tags(ctx, prepared, use_vm_sandbox),@@ -996,8 +993,7 @@ def _create_sandbox_for_repository(input: CreateSandboxForRepositoryInput) -> Cr)sandbox.destroy()config.snapshot_id = None- config.snapshot_external_id = None- config.snapshot_kind = SNAPSHOT_KIND_FILESYSTEM+ config.resume_snapshot_external_id = Noneconfig.snapshot_mount_path = Noneconfig.snapshot_source = "none"config.snapshot_restored = False@@ -1043,13 +1039,8 @@ def _create_sandbox_for_repository(input: CreateSandboxForRepositoryInput) -> Crsnapshot_outcome = ("used" if actual_used_snapshot else "fresh" if prepared.snapshot_source == "none" else "fallback")- metrics_snapshot_kind = prepared.snapshot_kind if prepared.snapshot_source != "none" else "none"- increment_snapshot_usage(- actual_used_snapshot,- snapshot_source=prepared.snapshot_source,- snapshot_kind=metrics_snapshot_kind,- )- increment_snapshot_restore(prepared.snapshot_source, metrics_snapshot_kind, snapshot_outcome)+ increment_snapshot_usage(actual_used_snapshot, snapshot_source=prepared.snapshot_source)+ increment_snapshot_restore(prepared.snapshot_source, snapshot_outcome)record_sandbox_created(runtime,diff --git a/products/tasks/backend/temporal/process_task/activities/start_agent_server.py b/products/tasks/backend/temporal/process_task/activities/start_agent_server.pyindex 980d6b2cc92a..1abe6384165b 100644--- a/products/tasks/backend/temporal/process_task/activities/start_agent_server.py+++ b/products/tasks/backend/temporal/process_task/activities/start_agent_server.py@@ -42,7 +42,6 @@ from products.tasks.backend.logic.services.launch_preparation_metrics import laufrom products.tasks.backend.logic.services.modal_sandbox import ModalSandboxfrom products.tasks.backend.logic.services.sandbox import (REPO_READY_FILE,- SNAPSHOT_KIND_DIRECTORY,SUBSCRIPTION_CLI_FLAGS,SandboxBase,get_sandbox_class_for_sandbox_id,@@ -227,7 +226,7 @@ def _has_agent_shadow_launch_marker(ctx: TaskProcessingContext, sandbox: Sandboxdef _launch_agent_shadow(ctx: TaskProcessingContext, sandbox: SandboxBase) -> bool:- if sandbox.config.snapshot_restored and sandbox.config.snapshot_kind != SNAPSHOT_KIND_DIRECTORY:+ if sandbox.config.snapshot_restored and sandbox.config.resume_snapshot_external_id is None:return Falseif current_activity_attempt() > 1 and _has_agent_shadow_launch_marker(ctx, sandbox):return True@@ -785,7 +784,7 @@ def start_agent_server(input: StartAgentServerInput) -> StartAgentServerOutput:"""Start the agent-server HTTP server in the sandbox.Sandbox credentials (sandbox_url, connect_token) are passed directly via input- from get_sandbox_for_repository output.+ from the sandbox provisioning output."""ctx = input.contextdiff --git a/products/tasks/backend/temporal/process_task/activities/tests/test_create_resume_snapshot.py b/products/tasks/backend/temporal/process_task/activities/tests/test_create_resume_snapshot.pyindex 4170dd0a33b9..956803104df6 100644--- a/products/tasks/backend/temporal/process_task/activities/tests/test_create_resume_snapshot.py+++ b/products/tasks/backend/temporal/process_task/activities/tests/test_create_resume_snapshot.py@@ -4,7 +4,7 @@ import pytestfrom asgiref.sync import async_to_sync-from products.tasks.backend.constants import DEFAULT_DIRECTORY_RESUME_SNAPSHOT_MOUNT_PATH, SNAPSHOT_KIND_DIRECTORY+from products.tasks.backend.constants import DEFAULT_DIRECTORY_RESUME_SNAPSHOT_MOUNT_PATH, RESUME_SNAPSHOT_KINDfrom products.tasks.backend.exceptions import SnapshotFileLimitExceededError, SnapshotTimeoutErrorfrom products.tasks.backend.models import Task, TaskRunfrom products.tasks.backend.temporal.process_task.activities.create_resume_snapshot import (@@ -40,7 +40,7 @@ def test_create_directory_resume_snapshot_preserves_pending_delivery(activity_en)output = activity_environment.run(unwrap(create_resume_snapshot),- CreateResumeSnapshotInput(sandbox_id="sandbox-1", run_id=str(run.id), use_directory_snapshot=True),+ CreateResumeSnapshotInput(sandbox_id="sandbox-1", run_id=str(run.id)),)sandbox.create_directory_snapshot.assert_called_once_with(DEFAULT_DIRECTORY_RESUME_SNAPSHOT_MOUNT_PATH)@@ -49,11 +49,10 @@ def test_create_directory_resume_snapshot_preserves_pending_delivery(activity_enassert run.state == {**pending_state,"snapshot_external_id": "im-dir",- "snapshot_kind": SNAPSHOT_KIND_DIRECTORY,+ "snapshot_kind": RESUME_SNAPSHOT_KIND,"snapshot_mount_path": DEFAULT_DIRECTORY_RESUME_SNAPSHOT_MOUNT_PATH,}assert output.external_id == "im-dir"- assert output.snapshot_kind == SNAPSHOT_KIND_DIRECTORYassert output.snapshot_mount_path == DEFAULT_DIRECTORY_RESUME_SNAPSHOT_MOUNT_PATH@@ -79,7 +78,7 @@ def test_transient_snapshot_error_propagates_so_temporal_retries(activity_envirowith pytest.raises(SnapshotTimeoutError):async_to_sync(activity_environment.run)(create_resume_snapshot,- CreateResumeSnapshotInput(sandbox_id="sandbox-1", run_id="run-1", use_directory_snapshot=True),+ CreateResumeSnapshotInput(sandbox_id="sandbox-1", run_id="run-1"),)update_state.assert_not_called()@@ -114,9 +113,7 @@ def test_directory_file_limit_preserves_state(activity_environment, mocker, tmp_output = async_to_sync(activity_environment.run)(create_resume_snapshot,- CreateResumeSnapshotInput(- sandbox_id="sandbox-1", run_id="run-1", use_directory_snapshot=True, allow_pruning=allow_pruning- ),+ CreateResumeSnapshotInput(sandbox_id="sandbox-1", run_id="run-1", allow_pruning=allow_pruning),)assert output.external_id is Nonediff --git a/products/tasks/backend/temporal/process_task/activities/tests/test_get_sandbox_for_repository.py b/products/tasks/backend/temporal/process_task/activities/tests/test_get_sandbox_for_repository.pydeleted file mode 100644index d774e2411c0e..000000000000--- a/products/tasks/backend/temporal/process_task/activities/tests/test_get_sandbox_for_repository.py+++ /dev/null@@ -1,149 +0,0 @@-import os-import uuid--import pytest--from asgiref.sync import async_to_sync--from products.tasks.backend.logic.services.sandbox import Sandbox-from products.tasks.backend.models import SandboxSnapshot-from products.tasks.backend.temporal.process_task.activities.get_sandbox_for_repository import (- GetSandboxForRepositoryInput,- get_sandbox_for_repository,-)-from products.tasks.backend.temporal.process_task.activities.get_task_processing_context import TaskProcessingContext--- not os.environ.get("MODAL_TOKEN_ID") or not os.environ.get("MODAL_TOKEN_SECRET"),- reason="MODAL_TOKEN_ID and MODAL_TOKEN_SECRET environment variables not set",-)[email protected]("sandbox_ai_gateway")-class TestGetSandboxForRepositoryActivity:- def _create_context(self, github_integration, test_task, test_task_run) -> TaskProcessingContext:- return TaskProcessingContext(- task_id=str(test_task.id),- run_id=str(test_task_run.id),- team_id=test_task.team_id,- team_uuid=str(test_task.team.uuid),- organization_id=str(test_task.team.organization_id),- github_integration_id=github_integration.id,- repository=test_task.repository,- distinct_id=test_task.created_by.distinct_id or "test-user-id",- task_created_by_id=test_task.created_by_id,- )-- @pytest.mark.django_db(transaction=True)- def test_get_sandbox_with_unavailable_snapshot_falls_back(- self, activity_environment, github_integration, test_task, test_task_run- ):- snapshot = SandboxSnapshot.objects.create(- integration=github_integration,- external_id=f"fake_snapshot_{uuid.uuid4().hex[:8]}",- repos=[test_task.repository],- status=SandboxSnapshot.Status.COMPLETE,- )-- sandbox_id = None- try:- context = self._create_context(github_integration, test_task, test_task_run)- input_data = GetSandboxForRepositoryInput(context=context)-- result = async_to_sync(activity_environment.run)(get_sandbox_for_repository, input_data)-- assert result.sandbox_id is not None- assert result.used_snapshot is False- assert result.should_create_snapshot is True-- sandbox_id = result.sandbox_id-- finally:- snapshot.delete()- if sandbox_id:- try:- sandbox = Sandbox.get_by_id(sandbox_id)- sandbox.destroy()- except Exception:- pass-- @pytest.mark.django_db(transaction=True)- def test_get_sandbox_without_snapshot_returns_should_create_snapshot(- self, activity_environment, github_integration, test_task, test_task_run- ):- sandbox_id = None- try:- context = self._create_context(github_integration, test_task, test_task_run)- input_data = GetSandboxForRepositoryInput(context=context)-- result = async_to_sync(activity_environment.run)(get_sandbox_for_repository, input_data)-- assert result.sandbox_id is not None- assert result.used_snapshot is False- assert result.should_create_snapshot is True-- sandbox_id = result.sandbox_id-- finally:- if sandbox_id:- try:- sandbox = Sandbox.get_by_id(sandbox_id)- sandbox.destroy()- except Exception:- pass-- @pytest.mark.django_db(transaction=True)- def test_get_sandbox_creates_sandbox_from_base_when_no_snapshot(- self, activity_environment, github_integration, test_task, test_task_run- ):- sandbox_id = None- try:- context = self._create_context(github_integration, test_task, test_task_run)- input_data = GetSandboxForRepositoryInput(context=context)-- result = async_to_sync(activity_environment.run)(get_sandbox_for_repository, input_data)-- sandbox_id = result.sandbox_id-- sandbox = Sandbox.get_by_id(sandbox_id)- assert sandbox is not None-- check_result = sandbox.execute("echo 'sandbox is running'")- assert check_result.exit_code == 0- assert "sandbox is running" in check_result.stdout-- finally:- if sandbox_id:- try:- sandbox = Sandbox.get_by_id(sandbox_id)- sandbox.destroy()- except Exception:- pass-- @pytest.mark.django_db(transaction=True)- def test_get_sandbox_includes_environment_variables(- self, activity_environment, github_integration, test_task, test_task_run- ):- sandbox_id = None- try:- context = self._create_context(github_integration, test_task, test_task_run)- input_data = GetSandboxForRepositoryInput(context=context)-- result = async_to_sync(activity_environment.run)(get_sandbox_for_repository, input_data)-- sandbox_id = result.sandbox_id- sandbox = Sandbox.get_by_id(sandbox_id)-- check_github = sandbox.execute("echo $GITHUB_TOKEN")- assert check_github.exit_code == 0-- check_posthog = sandbox.execute("echo $POSTHOG_PERSONAL_API_KEY")- assert check_posthog.exit_code == 0- assert check_posthog.stdout.strip() != ""-- finally:- if sandbox_id:- try:- sandbox = Sandbox.get_by_id(sandbox_id)- sandbox.destroy()- except Exception:- passdiff --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.pyindex ffcc56d98894..9034104086f1 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@@ -1217,19 +1217,6 @@ class TestGetTaskProcessingContextActivity:assert _is_burstable_sandbox_resources_enabled(run_id="run-id", state=state) is expected@pytest.mark.django_db(transaction=True)- def test_get_task_processing_context_enables_directory_resume_snapshots(self, activity_environment, test_task):- task_run = test_task.create_run()- input_data = GetTaskProcessingContextInput(run_id=str(task_run.id))-- with patch(- "products.tasks.backend.temporal.process_task.activities.get_task_processing_context.posthoganalytics.feature_enabled",- return_value=False,- ):- result = async_to_sync(activity_environment.run)(get_task_processing_context, input_data)-- assert result.use_modal_resume_snapshots is True- assert result.use_modal_directory_resume_snapshots is True-@pytest.mark.django_db(transaction=True)def test_get_task_processing_context_applies_org_default_custom_image(self, activity_environment, test_task):# Wiring guard for the elif chain in the activity body: a VM run with nodiff --git a/products/tasks/backend/temporal/process_task/activities/tests/test_provision_sandbox.py b/products/tasks/backend/temporal/process_task/activities/tests/test_provision_sandbox.pyindex f179397b0aa5..b4af8ab6ace8 100644--- a/products/tasks/backend/temporal/process_task/activities/tests/test_provision_sandbox.py+++ b/products/tasks/backend/temporal/process_task/activities/tests/test_provision_sandbox.py@@ -17,7 +17,6 @@ from asgiref.sync import async_to_syncfrom posthog.temporal.common.posthog_client import is_expected_activity_failure-from products.tasks.backend.constants import SNAPSHOT_KIND_DIRECTORY, SNAPSHOT_KIND_FILESYSTEMfrom products.tasks.backend.exceptions import (ComputeBillingLimitError,OrganizationExecutionError,@@ -249,22 +248,20 @@ def test_sandbox_image_kind(image_source: str, custom_image_name: str | None, ex@pytest.mark.parametrize(- "task_runtime, snapshot_id, snapshot_external_id, snapshot_kind, state, capability, expected",+ "task_runtime, snapshot_id, snapshot_external_id, state, capability, expected",[(Task.Runtime.ACP,+ "snapshot-row-1",None,- "snapshot-1",- SNAPSHOT_KIND_FILESYSTEM,{"prewarmed": True, "resume_from_run_id": "previous-run"},False,True,),(Task.Runtime.ACP,+ "snapshot-row-1",None,- "snapshot-1",- SNAPSHOT_KIND_FILESYSTEM,{"prewarmed": True, "resume_from_run_id": "previous-run"},True,False,@@ -273,43 +270,22 @@ def test_sandbox_image_kind(image_source: str, custom_image_name: str | None, exTask.Runtime.ACP,None,"snapshot-1",- SNAPSHOT_KIND_DIRECTORY,{"prewarmed": True, "resume_from_run_id": "previous-run"},False,False,),- (- Task.Runtime.ACP,- None,- "snapshot-1",- SNAPSHOT_KIND_FILESYSTEM,- {"resume_from_run_id": "previous-run"},- False,- False,- ),(Task.Runtime.ACP,"snapshot-row-1",None,- SNAPSHOT_KIND_FILESYSTEM,- {"prewarmed": True, "resume_from_run_id": "previous-run"},- False,- True,- ),- (- Task.Runtime.ACP,- "snapshot-row-1",- None,- SNAPSHOT_KIND_DIRECTORY,- {"prewarmed": True, "resume_from_run_id": "previous-run"},+ {"resume_from_run_id": "previous-run"},False,False,),(Task.Runtime.PI,+ "snapshot-row-1",None,- "snapshot-1",- SNAPSHOT_KIND_FILESYSTEM,{"prewarmed": True, "resume_from_run_id": "previous-run"},False,False,@@ -317,7 +293,7 @@ def test_sandbox_image_kind(image_source: str, custom_image_name: str | None, ex],)def test_old_full_snapshot_agent_is_rejected_only_for_prewarmed_resume(- mocker, task_runtime, snapshot_id, snapshot_external_id, snapshot_kind, state, capability, expected+ mocker, task_runtime, snapshot_id, snapshot_external_id, state, capability, expected):context = _context_for_desktop_bootstrap()context.task_runtime = task_runtime@@ -335,7 +311,6 @@ def test_old_full_snapshot_agent_is_rejected_only_for_prewarmed_resume(shallow_clone=True,image_source="resume_snapshot",image_source_label="resume snapshot snapshot-1",- snapshot_kind=snapshot_kind,)sandbox = mocker.Mock()sandbox.agent_server_supports_prewarmed_resume_message_driven.return_value = capability@@ -1144,7 +1119,7 @@ def test_create_sandbox_records_installed_agent_version([("hogland_golden", False),("custom_vm_image", False),- ("filesystem_snapshot", False),+ ("repository_snapshot", False),("directory_snapshot", True),],)@@ -1159,9 +1134,9 @@ def test_create_sandbox_compares_the_agent_with_the_pin_only_for_pinned_images(if restored:prepared = dataclasses.replace(prepared,- snapshot_external_id="snapshot-id",+ snapshot_id="snapshot-id" if source == "repository_snapshot" else None,+ snapshot_external_id="snapshot-id" if source == "directory_snapshot" else None,used_snapshot=True,- snapshot_kind=SNAPSHOT_KIND_DIRECTORY if source == "directory_snapshot" else SNAPSHOT_KIND_FILESYSTEM,)update_state, mismatch_extras = _create_and_record_agent_version(diff --git a/products/tasks/backend/temporal/process_task/activities/tests/test_start_agent_server.py b/products/tasks/backend/temporal/process_task/activities/tests/test_start_agent_server.pyindex 82a831c83b79..ab705984fffe 100644--- a/products/tasks/backend/temporal/process_task/activities/tests/test_start_agent_server.py+++ b/products/tasks/backend/temporal/process_task/activities/tests/test_start_agent_server.py@@ -947,12 +947,22 @@ def test_agent_shadow_skips_filesystem_snapshot(mocker) -> None:)sandbox = mocker.Mock(id="sandbox-id")sandbox.config.snapshot_restored = True- sandbox.config.snapshot_kind = "filesystem"+ sandbox.config.resume_snapshot_external_id = Noneassert _launch_agent_shadow(_context(agent_shadow_enabled=True), sandbox) is Falsesandbox.execute.assert_not_called()+def test_agent_shadow_allows_directory_snapshot(mocker) -> None:+ sandbox = mocker.Mock(id="sandbox-id")+ sandbox.config.snapshot_restored = True+ sandbox.config.resume_snapshot_external_id = "im-dir"+ sandbox.agent_server_health_url.return_value = "http://127.0.0.1:8080/health"+ sandbox.execute.return_value = ExecutionResult(stdout="", stderr="", exit_code=0)++ assert _launch_agent_shadow(_context(agent_shadow_enabled=True), sandbox) is True++def test_agent_shadow_result_is_allowlisted(mocker) -> None:sandbox = mocker.Mock(id="sandbox-id")sandbox.execute.return_value = ExecutionResult(diff --git a/products/tasks/backend/temporal/process_task/tests/test_ai_gateway_token.py b/products/tasks/backend/temporal/process_task/tests/test_ai_gateway_token.pyindex dc9175980864..7f8d32ea4cb7 100644--- a/products/tasks/backend/temporal/process_task/tests/test_ai_gateway_token.py+++ b/products/tasks/backend/temporal/process_task/tests/test_ai_gateway_token.py@@ -858,23 +858,6 @@ class TestProvisioningBoundaries:env.assert_called_once_with(ctx, task)assert out["AI_GATEWAY_TOKEN"] == "phe"- def test_repository_builder_uses_the_shared_derivation(self, mint_settings):- import importlib-- mod = importlib.import_module(- "products.tasks.backend.temporal.process_task.activities.get_sandbox_for_repository"- )-- ctx, task = self._ctx(), self._task()- with (- patch.object(mod, "run_gateway_env_vars", return_value={"AI_GATEWAY_TOKEN": "phe"}) as env,- patch.object(mod, "get_sandbox_jwt_public_key", return_value="jwt"),- patch.object(mod, "get_sandbox_api_url", return_value="url"),- ):- out = mod._build_environment_variables(ctx, task, "", "tok")- env.assert_called_once_with(ctx, task)- assert out["AI_GATEWAY_TOKEN"] == "phe"-def test_pinned_mint_is_stamped_on_the_run(self, mint_settings):env = {"AI_GATEWAY_TOKEN": "phe", "AI_GATEWAY_PRODUCT": "slack_app"}with (diff --git a/products/tasks/backend/temporal/process_task/tests/test_followup.py b/products/tasks/backend/temporal/process_task/tests/test_followup.pyindex 31c3d8188bac..5630ebe8cb24 100644--- a/products/tasks/backend/temporal/process_task/tests/test_followup.py+++ b/products/tasks/backend/temporal/process_task/tests/test_followup.py@@ -291,7 +291,6 @@ def _mock_get_context_configurable(_input) -> TaskProcessingContext:pr_loop_enabled=_ci_context_overrides.get("pr_loop_enabled", True),ci_prompt=_ci_context_overrides.get("ci_prompt"),use_modal_resume_snapshots=_ci_context_overrides.get("use_modal_resume_snapshots", False),- use_modal_directory_resume_snapshots=_ci_context_overrides.get("use_modal_directory_resume_snapshots", False),state=_ci_context_overrides.get("state"),)@@ -312,7 +311,6 @@ def _mock_create_resume_snapshot(input: CreateResumeSnapshotInput) -> CreateResureturn CreateResumeSnapshotOutput(external_id=None, error="snapshot failed", duration_ms=10)return CreateResumeSnapshotOutput(external_id=f"snapshot-{len(_snapshot_events)}",- snapshot_kind="directory",snapshot_mount_path="/tmp/workspace",duration_ms=10,)@@ -444,7 +442,6 @@ class TestCIFollowUpLoop:global _snapshot_failures_remaining_ci_context_overrides["state"] = {"mode": "interactive"}_ci_context_overrides["use_modal_resume_snapshots"] = True- _ci_context_overrides["use_modal_directory_resume_snapshots"] = True_pr_context_overrides["behavior"] = "unchanged"_snapshot_failures_remaining = 1@@ -471,7 +468,6 @@ class TestCIFollowUpLoop:async def test_refreshes_ci_snapshot_after_follow_up(self):_ci_context_overrides["state"] = {"mode": "interactive"}_ci_context_overrides["use_modal_resume_snapshots"] = True- _ci_context_overrides["use_modal_directory_resume_snapshots"] = True_pr_context_overrides["behavior"] = "unchanged"async with await WorkflowEnvironment.start_time_skipping() as env:diff --git a/products/tasks/backend/temporal/process_task/tests/test_utils.py b/products/tasks/backend/temporal/process_task/tests/test_utils.pyindex 21412f996490..1e8fcba4f0c2 100644--- a/products/tasks/backend/temporal/process_task/tests/test_utils.py+++ b/products/tasks/backend/temporal/process_task/tests/test_utils.py@@ -13,11 +13,7 @@ from posthog.models.user import Userfrom posthog.models.user_integration import UserGitHubIntegration, UserIntegrationfrom products.mcp_store.backend.facade.contracts import ActiveInstallation, SandboxInstallations-from products.tasks.backend.constants import (- DEFAULT_DIRECTORY_RESUME_SNAPSHOT_MOUNT_PATH,- DEFAULT_SANDBOX_WORKING_DIR,- SNAPSHOT_KIND_DIRECTORY,-)+from products.tasks.backend.constants import DEFAULT_SANDBOX_WORKING_DIR, RESUME_SNAPSHOT_KINDfrom products.tasks.backend.exceptions import CredentialUnavailableErrorfrom products.tasks.backend.models import Taskfrom products.tasks.backend.redis import get_tasks_cache@@ -132,33 +128,16 @@ class TestRunStateResumeCompatibility(SimpleTestCase):self.assertEqual(parsed.same_run_resume_idle, expected_idle)-class TestRunStateSnapshotPaths(TestCase):+class TestRunStateSnapshotPaths(SimpleTestCase):@parameterized.expand([- (- "new_directory_snapshot",- {"snapshot_kind": SNAPSHOT_KIND_DIRECTORY},- DEFAULT_DIRECTORY_RESUME_SNAPSHOT_MOUNT_PATH,- ),- (- "stored_directory_snapshot_path",- {"snapshot_kind": SNAPSHOT_KIND_DIRECTORY, "snapshot_mount_path": DEFAULT_SANDBOX_WORKING_DIR},- DEFAULT_SANDBOX_WORKING_DIR,- ),+ ("workspace_path", {"snapshot_mount_path": DEFAULT_SANDBOX_WORKING_DIR}, DEFAULT_SANDBOX_WORKING_DIR),# A disallowed stored path invalidates the snapshot (None) — it must NOT be remapped# to the default: the snapshot's content layout only fits the path it was captured# from. "/tmp" is the legacy default whose re-mount killed the sandbox.- (- "legacy_tmp_directory_snapshot",- {"snapshot_kind": SNAPSHOT_KIND_DIRECTORY, "snapshot_mount_path": "/tmp"},- None,- ),- (- "unsupported_directory_snapshot_path",- {"snapshot_kind": SNAPSHOT_KIND_DIRECTORY, "snapshot_mount_path": "/tmp/agent-env"},- None,- ),- ("filesystem_snapshot", {"snapshot_kind": "filesystem"}, None),+ ("legacy_tmp_path", {"snapshot_mount_path": "/tmp"}, None),+ ("unsupported_path", {"snapshot_mount_path": "/tmp/agent-env"}, None),+ ("no_path", {}, None),])def test_resume_snapshot_mount_path(self, _name: str, state: dict[str, str], expected_path: str | None) -> None:@@ -166,19 +145,9 @@ class TestRunStateSnapshotPaths(TestCase):@parameterized.expand([- (- "directory_workspace_path",- {"snapshot_kind": SNAPSHOT_KIND_DIRECTORY, "snapshot_mount_path": DEFAULT_SANDBOX_WORKING_DIR},- True,- ),- ("directory_no_path", {"snapshot_kind": SNAPSHOT_KIND_DIRECTORY}, True),- (- "directory_legacy_tmp_path",- {"snapshot_kind": SNAPSHOT_KIND_DIRECTORY, "snapshot_mount_path": "/tmp"},- False,- ),- ("filesystem", {"snapshot_kind": "filesystem"}, True),- ("no_kind", {}, True),+ ("workspace_path", {"snapshot_mount_path": DEFAULT_SANDBOX_WORKING_DIR}, True),+ ("legacy_tmp_path", {"snapshot_mount_path": "/tmp"}, False),+ ("no_path", {}, False),])def test_resume_snapshot_is_usable(self, _name: str, state: dict[str, str], expected: bool) -> None:@@ -190,30 +159,25 @@ class TestRunStateSnapshotPaths(TestCase):"directory_full_triple",{"snapshot_external_id": "im-dir",- "snapshot_kind": SNAPSHOT_KIND_DIRECTORY,+ "snapshot_kind": RESUME_SNAPSHOT_KIND,"snapshot_mount_path": DEFAULT_SANDBOX_WORKING_DIR,},{"snapshot_external_id": "im-dir",- "snapshot_kind": SNAPSHOT_KIND_DIRECTORY,+ "snapshot_kind": RESUME_SNAPSHOT_KIND,"snapshot_mount_path": DEFAULT_SANDBOX_WORKING_DIR,},),(- "filesystem_no_mount_path",- {"snapshot_external_id": "im-fs", "snapshot_kind": "filesystem"},- {"snapshot_external_id": "im-fs", "snapshot_kind": "filesystem"},- ),- (- "legacy_no_kind",+ "legacy_no_mount_path",{"snapshot_external_id": "im-old"},- {"snapshot_external_id": "im-old", "snapshot_kind": "filesystem"},+ {},),("unusable_directory",{"snapshot_external_id": "im-dir",- "snapshot_kind": SNAPSHOT_KIND_DIRECTORY,+ "snapshot_kind": RESUME_SNAPSHOT_KIND,"snapshot_mount_path": "/tmp",},{},diff --git a/products/tasks/backend/temporal/process_task/tests/test_workflow.py b/products/tasks/backend/temporal/process_task/tests/test_workflow.pyindex 675d1147f9e4..3cbc32ce064f 100644--- a/products/tasks/backend/temporal/process_task/tests/test_workflow.py+++ b/products/tasks/backend/temporal/process_task/tests/test_workflow.py@@ -574,9 +574,7 @@ class TestSandboxRotation:return cast("asyncio.Task[None]", name)def _snapshot(self, external_id: str | None = "snap-1") -> CreateResumeSnapshotOutput:- return CreateResumeSnapshotOutput(- external_id=external_id, snapshot_kind="directory", snapshot_mount_path="/mnt/work"- )+ return CreateResumeSnapshotOutput(external_id=external_id, snapshot_mount_path="/mnt/work")def _replacement(self, used_snapshot: bool = True) -> GetSandboxForRepositoryOutput:return GetSandboxForRepositoryOutput(diff --git a/products/tasks/backend/temporal/process_task/utils.py b/products/tasks/backend/temporal/process_task/utils.pyindex ed70fb0b3af8..885d0758840e 100644--- a/products/tasks/backend/temporal/process_task/utils.py+++ b/products/tasks/backend/temporal/process_task/utils.py@@ -34,13 +34,10 @@ from products.tasks.backend import model_catalogfrom products.tasks.backend.constants import (ALLOWED_DIRECTORY_RESUME_SNAPSHOT_MOUNT_PATHS,CODEX_INITIAL_PERMISSION_MODE_CHOICES,- DEFAULT_DIRECTORY_RESUME_SNAPSHOT_MOUNT_PATH,EVAL_INTERACTION_ORIGIN,INITIAL_PERMISSION_MODE_CHOICES,- SNAPSHOT_KIND_DIRECTORY,- SNAPSHOT_KIND_FILESYSTEM,+ RESUME_SNAPSHOT_KIND,InitialPermissionMode,- SnapshotKind,filter_user_sandbox_env_vars,is_same_run_resume_idle_state,is_same_run_resume_state,@@ -97,7 +94,7 @@ if TYPE_CHECKING:from posthog.models.user import Userfrom products.slack_app.backend.models import SlackThreadTaskMapping- from products.tasks.backend.models import SandboxSnapshot, Task, TaskRun+ from products.tasks.backend.models import Task, TaskRunfrom products.tasks.backend.temporal.process_task.activities.get_task_processing_context import (TaskProcessingContext,)@@ -342,25 +339,23 @@ def normalize_directory_resume_snapshot_mount_path(snapshot_mount_path: object)A snapshot's content layout matches the path it was captured from, so a stored path outsidethe allowlist (notably the legacy "/tmp" default, whose mount replaced the live system temp- dir and killed the sandbox) cannot be remapped to a safe path — the snapshot is unusable and- the resume must fall back to a fresh sandbox.+ dir and killed the sandbox) cannot be remapped to a safe path. A snapshot with no stored path+ predates the path stamp and cannot be restored either, so the resume falls back to a fresh+ sandbox."""- if not snapshot_mount_path:- return DEFAULT_DIRECTORY_RESUME_SNAPSHOT_MOUNT_PATHif isinstance(snapshot_mount_path, str) and snapshot_mount_path in ALLOWED_DIRECTORY_RESUME_SNAPSHOT_MOUNT_PATHS:return snapshot_mount_path- logger.warning(- "Directory resume snapshot has an unsupported mount path; invalidating the snapshot",- extra={"snapshot_mount_path": snapshot_mount_path},- )+ if snapshot_mount_path:+ logger.warning(+ "Directory resume snapshot has an unsupported mount path; invalidating the snapshot",+ extra={"snapshot_mount_path": snapshot_mount_path},+ )return None-def is_resume_snapshot_usable(kind: SnapshotKind, mount_path: str | None) -> bool:- """Whether a stored snapshot may be restored into a new sandbox; False falls back to a fresh one.- A directory snapshot whose stored mount path was invalidated (legacy "/tmp" captures) can't be restored."""- return not (kind == SNAPSHOT_KIND_DIRECTORY and mount_path is None)+def is_resume_snapshot_usable(mount_path: str | None) -> bool:+ return mount_path is not Noneclass RunState(BaseModel, extra="allow"):@@ -383,7 +378,6 @@ class RunState(BaseModel, extra="allow"):same_run_resume: bool = Falsesame_run_resume_idle: bool = Falsesnapshot_external_id: str | None = None- snapshot_kind: str | None = Nonesnapshot_mount_path: str | None = Nonesandbox_id: str | None = Nonesandbox_url: str | None = None@@ -402,19 +396,11 @@ class RunState(BaseModel, extra="allow"):def model_access(self) -> ModelAccess:return resolve_model_access(self.model_dump())- def resume_snapshot_kind(self) -> SnapshotKind:- if self.snapshot_kind == SNAPSHOT_KIND_DIRECTORY:- return SNAPSHOT_KIND_DIRECTORY- return SNAPSHOT_KIND_FILESYSTEM-def resume_snapshot_mount_path(self) -> str | None:- if self.resume_snapshot_kind() != SNAPSHOT_KIND_DIRECTORY:- return Nonereturn normalize_directory_resume_snapshot_mount_path(self.snapshot_mount_path)def resume_snapshot_is_usable(self) -> bool:- """See ``is_resume_snapshot_usable`` — callers must provision fresh when False."""- return is_resume_snapshot_usable(self.resume_snapshot_kind(), self.resume_snapshot_mount_path())+ return is_resume_snapshot_usable(self.resume_snapshot_mount_path())def resume_snapshot_carry_state(self) -> dict[str, Any]:"""State keys a successor run must copy (always the full set, never the external ID@@ -423,7 +409,7 @@ class RunState(BaseModel, extra="allow"):return {}carried: dict[str, Any] = {"snapshot_external_id": self.snapshot_external_id,- "snapshot_kind": self.resume_snapshot_kind(),+ "snapshot_kind": RESUME_SNAPSHOT_KIND,}mount_path = self.resume_snapshot_mount_path()if mount_path is not None:@@ -438,29 +424,6 @@ def parse_run_state(state: dict[str, Any] | None) -> RunState:return RunState.model_validate(normalized_state)-@dataclass(frozen=True)-class SnapshotMetadata:- kind: SnapshotKind- mount_path: str | None-- @property- def is_usable(self) -> bool:- """See ``is_resume_snapshot_usable`` — same invalidation rule."""- return is_resume_snapshot_usable(self.kind, self.mount_path)---def get_sandbox_snapshot_metadata(snapshot: SandboxSnapshot) -> SnapshotMetadata:- kind: SnapshotKind = (- SNAPSHOT_KIND_DIRECTORY- if snapshot.metadata.get("snapshot_kind") == SNAPSHOT_KIND_DIRECTORY- else SNAPSHOT_KIND_FILESYSTEM- )- mount_path = None- if kind == SNAPSHOT_KIND_DIRECTORY:- mount_path = normalize_directory_resume_snapshot_mount_path(snapshot.metadata.get("snapshot_mount_path"))- return SnapshotMetadata(kind=kind, mount_path=mount_path)--# TTL for the per-run GitHub user token cache. Kept for backward-compat with callers# (notably the PostHog Desktop CLI) that still pass ``github_user_token`` on the run request.# The server-side identity flow should be preferred going forward.diff --git a/products/tasks/backend/temporal/process_task/workflow.py b/products/tasks/backend/temporal/process_task/workflow.pyindex 4b655181abf6..21590c85891e 100644--- a/products/tasks/backend/temporal/process_task/workflow.py+++ b/products/tasks/backend/temporal/process_task/workflow.py@@ -18,7 +18,7 @@ from posthog.dataclasses import frozenfrom posthog.temporal.common.base import PostHogWorkflowfrom posthog.temporal.oauth import PosthogMcpScopes-from products.tasks.backend.constants import SNAPSHOT_KIND_FILESYSTEM, is_same_run_resume_state+from products.tasks.backend.constants import RESUME_SNAPSHOT_KIND, is_same_run_resume_statefrom products.tasks.backend.error_telemetry import truncate_error_messagefrom products.tasks.backend.logic.services.sandbox import is_public_sandbox_repofrom products.tasks.backend.temporal.babysit_pr.prompts import (@@ -70,7 +70,6 @@ from .activities.feature_flags import ()from .activities.forward_pending_message import forward_pending_user_messagefrom .activities.get_sandbox_exit_reason import GetSandboxExitReasonInput, get_sandbox_exit_reason-from .activities.get_sandbox_for_repository import GetSandboxForRepositoryOutputfrom .activities.get_task_processing_context import (GetTaskProcessingContextInput,TaskProcessingContext,@@ -85,6 +84,7 @@ from .activities.provision_sandbox import (CloneRepositoryInSandboxOutput,CreateSandboxForRepositoryInput,CreateSandboxForRepositoryOutput,+ GetSandboxForRepositoryOutput,InjectFreshTokensOnResumeInput,InvalidateResumeSnapshotInput,PrepareSandboxForRepositoryInput,@@ -2028,7 +2028,6 @@ class ProcessTaskWorkflow(PostHogWorkflow):snapshot_external_id=None,used_snapshot=False,should_create_snapshot=True,- snapshot_kind=SNAPSHOT_KIND_FILESYSTEM,snapshot_mount_path=None,snapshot_source="none",image_source=fresh_image_source,@@ -2993,7 +2992,7 @@ class ProcessTaskWorkflow(PostHogWorkflow):self.context.state = {**(self.context.state or {}),"snapshot_external_id": snapshot.external_id,- "snapshot_kind": snapshot.snapshot_kind,+ "snapshot_kind": RESUME_SNAPSHOT_KIND,"snapshot_mount_path": snapshot.snapshot_mount_path,"same_run_resume": True,"same_run_resume_idle": True,@@ -3315,7 +3314,6 @@ class ProcessTaskWorkflow(PostHogWorkflow):"sandbox_id": sandbox_id,"reason": reason,"snapshot_external_id": result.external_id,- "snapshot_kind": result.snapshot_kind,"duration_ms": result.duration_ms,},)@@ -3327,7 +3325,6 @@ class ProcessTaskWorkflow(PostHogWorkflow):"run_id": self.context.run_id,"sandbox_id": sandbox_id,"reason": reason,- "snapshot_kind": result.snapshot_kind,"duration_ms": result.duration_ms,"error": result.error,},diff --git a/products/tasks/backend/tests/test_api.py b/products/tasks/backend/tests/test_api.pyindex de584fc925bf..b5e8f9d08882 100644--- a/products/tasks/backend/tests/test_api.py+++ b/products/tasks/backend/tests/test_api.py@@ -56,7 +56,7 @@ from posthog.utils import absolute_urifrom products.posthog_ai.backend.models.assistant import Conversationfrom products.slack_app.backend.models import SlackThreadTaskMappingfrom products.tasks.backend.access import DesktopAccessResolutionError-from products.tasks.backend.constants import DEV_STACK_PREVIEW_PORT+from products.tasks.backend.constants import DEFAULT_SANDBOX_WORKING_DIR, DEV_STACK_PREVIEW_PORTfrom products.tasks.backend.exceptions import SandboxNotFoundErrorfrom products.tasks.backend.facade import (access as tasks_access,@@ -6066,6 +6066,7 @@ class TestTaskAPI(BaseTaskAPITest):"context_window": "1m","fast_mode": True,"snapshot_external_id": "snap-1",+ "snapshot_mount_path": DEFAULT_SANDBOX_WORKING_DIR,},)@@ -7685,7 +7686,6 @@ class TestTaskRunAPI(BaseTaskAPITest):"sandbox_memory_gb": 8,"sandbox_ttl_seconds": 1800,"inactivity_timeout_seconds": 600,- "use_modal_directory_resume_snapshots": True,"use_modal_vm_sandbox": False,"sandbox_template": "default_base","agent_otel_telemetry_enabled": False,@@ -7766,7 +7766,6 @@ class TestTaskRunAPI(BaseTaskAPITest):"sandbox_ttl_seconds": 86400,"inactivity_timeout_seconds": 86400,"wizard_config": {},- "use_modal_directory_resume_snapshots": False,"use_modal_vm_sandbox": True,# a VM template here would boot a Docker-capable box past the VM gate"sandbox_template": "vm_base",@@ -7850,7 +7849,6 @@ class TestTaskRunAPI(BaseTaskAPITest):assert run.state["sandbox_ttl_seconds"] == 1800assert run.state["inactivity_timeout_seconds"] == 600assert "wizard_config" not in run.state # caller cannot mark a run as a wizard run- assert run.state["use_modal_directory_resume_snapshots"] is Trueassert run.state["use_modal_vm_sandbox"] is Falseassert run.state["sandbox_template"] == "default_base"assert run.state["agent_otel_telemetry_enabled"] is False@@ -7928,7 +7926,6 @@ class TestTaskRunAPI(BaseTaskAPITest):"stream_presence_gated","stream_thin_tail","sandbox_id",- "use_modal_directory_resume_snapshots","use_modal_vm_sandbox","snapshot_external_id","snapshot_kind",@@ -7981,7 +7978,6 @@ class TestTaskRunAPI(BaseTaskAPITest):assert run.state["stream_presence_gated"] is True # protected key survives removalassert run.state["stream_thin_tail"] is True # protected key survives removalassert run.state["sandbox_id"] == "sb-real" # protected key survives removal- assert run.state["use_modal_directory_resume_snapshots"] is True # protected key survives removalassert run.state["use_modal_vm_sandbox"] is False # protected key survives removalassert run.state["snapshot_external_id"] == "im-real" # protected key survives removalassert run.state["snapshot_kind"] == "directory" # protected key survives removaldiff --git a/products/tasks/backend/tests/test_warm_facade.py b/products/tasks/backend/tests/test_warm_facade.pyindex 4dbfe06dd1ca..9e5a2466be6a 100644--- a/products/tasks/backend/tests/test_warm_facade.py+++ b/products/tasks/backend/tests/test_warm_facade.py@@ -17,6 +17,7 @@ from posthog.exceptions import QuotaLimitExceededfrom posthog.models import Integration, Userfrom posthog.models.user_integration import UserIntegration+from products.tasks.backend.constants import DEFAULT_SANDBOX_WORKING_DIRfrom products.tasks.backend.facade import (access as tasks_access,api as facade,@@ -1706,6 +1707,7 @@ class TestWarmTaskResumeSandbox(APIBaseTest):branch="release",extra_state={"snapshot_external_id": "snapshot-1",+ "snapshot_mount_path": DEFAULT_SANDBOX_WORKING_DIR,"pr_base_branch": base_branch,"stack_base_branch": "release","auto_publish": True,