: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
fix(tasks): require message-driven resume in sandbox snapshots (#96888)
Georgiy Tarasovc7bb28bd0bce
diff --git a/docs/published/handbook/engineering/ai/sandboxed-agents.md b/docs/published/handbook/engineering/ai/sandboxed-agents.mdindex 7128d5ea5511..7212c38e78c6 100644--- a/docs/published/handbook/engineering/ai/sandboxed-agents.md+++ b/docs/published/handbook/engineering/ai/sandboxed-agents.md@@ -489,9 +489,14 @@ 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.-The agent advertises `prewarmedResumeMessageDriven` alongside `prewarmedResumeIdle`.-Publish the updated agent and rebuild sandbox images before changing backend snapshot-compatibility checks to require the new capability.+Full filesystem snapshots contain an agent binary. Prewarmed resumes require its+`prewarmedResumeMessageDriven` capability; the older `prewarmedResumeIdle` capability+alone 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.+Pi runs keep their snapshot, because the Pi server starts no turn of its own and loads+its session history from the API.+Publish the updated agent and rebuild sandbox images before deploying the stricter+backend capability gate, so the fallback supplies a compatible agent.## Local developmentdiff --git a/products/tasks/backend/logic/services/sandbox.py b/products/tasks/backend/logic/services/sandbox.pyindex da4ef6747af7..f466ee7b27a5 100644--- a/products/tasks/backend/logic/services/sandbox.py+++ b/products/tasks/backend/logic/services/sandbox.py@@ -498,9 +498,9 @@ class SandboxBase(ABC):)return result.exit_code == 0- def agent_server_supports_prewarmed_resume_idle(self) -> bool:+ def agent_server_supports_prewarmed_resume_message_driven(self) -> bool:result = self.execute(- "grep -q prewarmedResumeIdle /scripts/node_modules/.bin/agent-server",+ "grep -q prewarmedResumeMessageDriven /scripts/node_modules/.bin/agent-server",timeout_seconds=10,)return result.exit_code == 0diff --git a/products/tasks/backend/logic/services/tests/test_docker_sandbox.py b/products/tasks/backend/logic/services/tests/test_docker_sandbox.pyindex a761bbf50c18..9aa6558d102d 100644--- a/products/tasks/backend/logic/services/tests/test_docker_sandbox.py+++ b/products/tasks/backend/logic/services/tests/test_docker_sandbox.py@@ -1,4 +1,6 @@import shlex+import subprocess+from pathlib import Pathimport pytestfrom unittest.mock import patch@@ -22,6 +24,31 @@ def _log_result() -> ExecutionResult:return ExecutionResult(stdout="agent-server log", stderr="", exit_code=0)+ ("capabilities", "expected"),+ [+ ("", False),+ ("prewarmedResumeIdle", False),+ ("prewarmedResumeIdle prewarmedResumeMessageDriven", True),+ ],+)+def test_prewarmed_resume_requires_message_driven_agent(+ sandbox: DockerSandbox, tmp_path: Path, capabilities: str, expected: bool+) -> None:+ binary = tmp_path / "agent-server"+ binary.write_text(capabilities)++ def execute_probe(command: str, *, timeout_seconds: int) -> ExecutionResult:+ args = shlex.split(command)+ assert args[-1] == "/scripts/node_modules/.bin/agent-server"+ args[-1] = str(binary)+ result = subprocess.run(args, capture_output=True, text=True, timeout=timeout_seconds)+ return ExecutionResult(stdout=result.stdout, stderr=result.stderr, exit_code=result.returncode)++ with patch.object(sandbox, "execute", side_effect=execute_probe):+ assert sandbox.agent_server_supports_prewarmed_resume_message_driven() is expected++def test_wait_for_agent_server_ready_timeout_is_retryable_and_not_captured(sandbox: DockerSandbox):with (patch.object(sandbox, "_wait_for_health_check", return_value=False),diff --git a/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py b/products/tasks/backend/temporal/process_task/activities/provision_sandbox.pyindex e38b8b7fd7b8..7aa09059178a 100644--- a/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py+++ b/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py@@ -298,17 +298,28 @@ def _prewarmed_resume_needs_fresh_agent(*,used_snapshot: bool,) -> bool:- """Whether a full resume snapshot bundled an agent that cannot idle before the resumed prompt."""+ """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.+ """+ # `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+ # history from the API instead of the snapshot, so probing a Pi bundle for the string+ # rejects a healthy snapshot and re-clones the repository for no behavior change.+ if ctx.task_runtime == Task.Runtime.PI:+ return Falseif (not used_snapshot- or prepared.snapshot_external_id is None+ or (prepared.snapshot_external_id is None and prepared.snapshot_id is None)or prepared.snapshot_kind == SNAPSHOT_KIND_DIRECTORYor not (ctx.state or {}).get("prewarmed")or not (ctx.state or {}).get("resume_from_run_id")):return Falsetry:- return not sandbox.agent_server_supports_prewarmed_resume_idle()+ return not sandbox.agent_server_supports_prewarmed_resume_message_driven()except Exception:logger.warning("prewarmed_resume_agent_capability_probe_failed", extra={"run_id": ctx.run_id})return Truediff --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 ac75640971e3..bff2741bebbb 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@@ -157,33 +157,78 @@ def test_sandbox_image_kind(image_source: str, custom_image_name: str | None, ex@pytest.mark.parametrize(- "snapshot_kind, state, capability, expected",+ "task_runtime, snapshot_id, snapshot_external_id, snapshot_kind, state, capability, expected",[(+ Task.Runtime.ACP,+ None,+ "snapshot-1",SNAPSHOT_KIND_FILESYSTEM,{"prewarmed": True, "resume_from_run_id": "previous-run"},False,True,),(+ Task.Runtime.ACP,+ None,+ "snapshot-1",SNAPSHOT_KIND_FILESYSTEM,{"prewarmed": True, "resume_from_run_id": "previous-run"},True,False,),(+ Task.Runtime.ACP,+ None,+ "snapshot-1",SNAPSHOT_KIND_DIRECTORY,{"prewarmed": True, "resume_from_run_id": "previous-run"},False,False,),- (SNAPSHOT_KIND_FILESYSTEM, {"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"},+ False,+ False,+ ),+ (+ Task.Runtime.PI,+ None,+ "snapshot-1",+ SNAPSHOT_KIND_FILESYSTEM,+ {"prewarmed": True, "resume_from_run_id": "previous-run"},+ False,+ False,+ ),],)def test_old_full_snapshot_agent_is_rejected_only_for_prewarmed_resume(- mocker, snapshot_kind, state, capability, expected+ mocker, task_runtime, snapshot_id, snapshot_external_id, snapshot_kind, state, capability, expected):context = _context_for_desktop_bootstrap()+ context.task_runtime = task_runtimecontext.state = stateprepared = PrepareSandboxForRepositoryOutput(sandbox_name="task-sandbox-task-id",@@ -191,8 +236,8 @@ def test_old_full_snapshot_agent_is_rejected_only_for_prewarmed_resume(github_token="",branch=None,environment_variables={},- snapshot_id=None,- snapshot_external_id="snapshot-1",+ snapshot_id=snapshot_id,+ snapshot_external_id=snapshot_external_id,used_snapshot=True,should_create_snapshot=False,shallow_clone=True,@@ -201,7 +246,7 @@ def test_old_full_snapshot_agent_is_rejected_only_for_prewarmed_resume(snapshot_kind=snapshot_kind,)sandbox = mocker.Mock()- sandbox.agent_server_supports_prewarmed_resume_idle.return_value = capability+ sandbox.agent_server_supports_prewarmed_resume_message_driven.return_value = capabilityassert _prewarmed_resume_needs_fresh_agent(context, prepared, sandbox, used_snapshot=True) is expected