Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(tasks): stamp boot rollout flags into run state at dispatch (#104330)

Alessandro Pogliaghie10683a261f8
diff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.py
index 82647dbe82ec..7b58f1f3a7bb 100644
--- a/products/tasks/backend/facade/api.py
+++ b/products/tasks/backend/facade/api.py
@@ -2474,6 +2474,9 @@ _PROTECTED_RUN_STATE_KEYS = frozenset(
# sandbox and re-enabling the run-log mirror with the rollout off).
AGENT_OTEL_TELEMETRY_STATE_KEY,
"sandbox_event_ingest_enabled",
+ "agent_proxy_keep_stream_open",
+ "overlap_clone_boot_enabled",
+ "use_modal_network_allowlist",
"stream_presence_gated",
"stream_thin_tail",
PR_LOOP_ENABLED_STATE_KEY,
diff --git a/products/tasks/backend/temporal/client.py b/products/tasks/backend/temporal/client.py
index 82a94a92b2ec..75fe5afa9221 100644
--- a/products/tasks/backend/temporal/client.py
+++ b/products/tasks/backend/temporal/client.py
@@ -17,10 +17,17 @@ from posthog.models.team.team import Team
from posthog.temporal.common.client import async_connect, sync_connect
from posthog.temporal.oauth import PosthogMcpScopes
-from products.tasks.backend.constants import AGENT_OTEL_TELEMETRY_STATE_KEY, SANDBOX_EVENT_INGEST_FEATURE_FLAG
+from products.tasks.backend.constants import (
+ AGENT_OTEL_TELEMETRY_STATE_KEY,
+ AGENT_PROXY_KEEP_STREAM_OPEN_FEATURE_FLAG,
+ MODAL_NETWORK_ALLOWLIST_FEATURE_FLAG,
+ OVERLAP_CLONE_BOOT_FEATURE_FLAG,
+ SANDBOX_EVENT_INGEST_FEATURE_FLAG,
+)
from products.tasks.backend.error_telemetry import truncate_error_message
from products.tasks.backend.feature_flags import is_agent_otel_telemetry_enabled, is_native_steering_signals_enabled
from products.tasks.backend.logic.services.dev_stack_image import DEV_STACK_IMAGE_NAME
+from products.tasks.backend.logic.services.run_actor import get_actor_distinct_id, get_task_run_credential_user
from products.tasks.backend.logic.services.workflow_step_resume import resume_workflow_step_for_run
from products.tasks.backend.metrics import AGENT_OTEL_TELEMETRY_STAMPED_TOTAL, observe_task_run_workflow_start
from products.tasks.backend.models import Task, TaskRun
@@ -42,6 +49,12 @@ logger = logging.getLogger(__name__)
_PRE_START_STATUSES: tuple[str, ...] = (TaskRun.Status.NOT_STARTED, TaskRun.Status.QUEUED)
+_BOOT_ROLLOUT_FLAGS: tuple[tuple[str, str], ...] = (
+ ("agent_proxy_keep_stream_open", AGENT_PROXY_KEEP_STREAM_OPEN_FEATURE_FLAG),
+ ("overlap_clone_boot_enabled", OVERLAP_CLONE_BOOT_FEATURE_FLAG),
+ ("use_modal_network_allowlist", MODAL_NETWORK_ALLOWLIST_FEATURE_FLAG),
+)
+
def _normalize_slack_context(slack_thread_context: Optional[Any]) -> Optional[dict[str, Any]]:
"""Convert slack_thread_context to dict if needed."""
@@ -119,6 +132,33 @@ def _get_task_run_for_metrics(run_id: str) -> TaskRun | None:
return None
+def _evaluate_boot_rollout_flag(
+ flag_key: str,
+ *,
+ distinct_id: str,
+ organization_id: str,
+ run_id: str,
+ task_id: str,
+) -> bool:
+ try:
+ return bool(
+ posthoganalytics.feature_enabled(
+ flag_key,
+ distinct_id=distinct_id,
+ groups={"organization": organization_id},
+ group_properties={"organization": {"id": organization_id}},
+ only_evaluate_locally=False,
+ send_feature_flag_events=False,
+ )
+ )
+ except Exception as e:
+ logger.warning(
+ "boot_rollout_capture_flag_failed",
+ extra={"run_id": run_id, "task_id": task_id, "flag_key": flag_key, "error": str(e)},
+ )
+ return False
+
+
def _capture_run_feature_flags(run_id: str) -> None:
"""Evaluate per-run rollout flags once at dispatch and stamp them into run state.
@@ -133,7 +173,12 @@ def _capture_run_feature_flags(run_id: str) -> None:
state = task_run.state or {}
need_event_ingest = not isinstance(state.get("sandbox_event_ingest_enabled"), bool)
need_otel_telemetry = not isinstance(state.get(AGENT_OTEL_TELEMETRY_STATE_KEY), bool)
- if not need_event_ingest and not need_otel_telemetry:
+ pending_boot_flags = [
+ (state_key, flag_key)
+ for state_key, flag_key in _BOOT_ROLLOUT_FLAGS
+ if not isinstance(state.get(state_key), bool)
+ ]
+ if not need_event_ingest and not need_otel_telemetry and not pending_boot_flags:
return
task = task_run.task
@@ -141,6 +186,8 @@ def _capture_run_feature_flags(run_id: str) -> None:
distinct_id = (
task.created_by.distinct_id if task.created_by and task.created_by.distinct_id else "process_task_workflow"
)
+ actor_user = get_task_run_credential_user(task, state)
+ boot_distinct_id = get_actor_distinct_id(actor_user) if actor_user else distinct_id
event_ingest_enabled = False
if need_event_ingest:
@@ -163,12 +210,25 @@ def _capture_run_feature_flags(run_id: str) -> None:
otel_telemetry_enabled = need_otel_telemetry and is_agent_otel_telemetry_enabled(
distinct_id=distinct_id, organization_id=organization_id
)
+ boot_flag_values: dict[str, bool] = {
+ state_key: _evaluate_boot_rollout_flag(
+ flag_key,
+ distinct_id=boot_distinct_id,
+ organization_id=organization_id,
+ run_id=run_id,
+ task_id=str(task.id),
+ )
+ for state_key, flag_key in pending_boot_flags
+ }
def _stamp_flags(latest_state: dict[str, Any]) -> None:
if need_event_ingest and not isinstance(latest_state.get("sandbox_event_ingest_enabled"), bool):
latest_state["sandbox_event_ingest_enabled"] = event_ingest_enabled
if need_otel_telemetry and not isinstance(latest_state.get(AGENT_OTEL_TELEMETRY_STATE_KEY), bool):
latest_state[AGENT_OTEL_TELEMETRY_STATE_KEY] = otel_telemetry_enabled
+ for state_key, value in boot_flag_values.items():
+ if not isinstance(latest_state.get(state_key), bool):
+ latest_state[state_key] = value
captured_state = TaskRun.mutate_state_atomic(task_run.id, _stamp_flags)
if need_otel_telemetry:
@@ -182,6 +242,7 @@ def _capture_run_feature_flags(run_id: str) -> None:
"task_id": str(task.id),
"sandbox_event_ingest_enabled": captured_state.get("sandbox_event_ingest_enabled"),
"agent_otel_telemetry_enabled": captured_state.get(AGENT_OTEL_TELEMETRY_STATE_KEY),
+ **{state_key: captured_state.get(state_key) for state_key, _ in _BOOT_ROLLOUT_FLAGS},
},
)
diff --git a/products/tasks/backend/tests/test_api.py b/products/tasks/backend/tests/test_api.py
index 97142707d8d9..f61bdd5a4444 100644
--- a/products/tasks/backend/tests/test_api.py
+++ b/products/tasks/backend/tests/test_api.py
@@ -6443,6 +6443,9 @@ class TestTaskRunAPI(BaseTaskAPITest):
"use_modal_vm_sandbox": False,
"agent_otel_telemetry_enabled": False,
"sandbox_event_ingest_enabled": False,
+ "agent_proxy_keep_stream_open": False,
+ "overlap_clone_boot_enabled": False,
+ "use_modal_network_allowlist": True,
"stream_presence_gated": True,
"stream_thin_tail": True,
"snapshot_external_id": "im-real",
@@ -6510,6 +6513,9 @@ class TestTaskRunAPI(BaseTaskAPITest):
"use_modal_vm_sandbox": True,
"agent_otel_telemetry_enabled": True,
"sandbox_event_ingest_enabled": True,
+ "agent_proxy_keep_stream_open": True,
+ "overlap_clone_boot_enabled": True,
+ "use_modal_network_allowlist": False,
"stream_presence_gated": False,
"stream_thin_tail": False,
"snapshot_external_id": "im-attacker",
@@ -6581,6 +6587,9 @@ class TestTaskRunAPI(BaseTaskAPITest):
assert run.state["use_modal_vm_sandbox"] is False
assert run.state["agent_otel_telemetry_enabled"] is False
assert run.state["sandbox_event_ingest_enabled"] is False
+ assert run.state["agent_proxy_keep_stream_open"] is False
+ assert run.state["overlap_clone_boot_enabled"] is False
+ assert run.state["use_modal_network_allowlist"] is True
assert run.state["stream_presence_gated"] is True
assert run.state["stream_thin_tail"] is True
assert run.state["snapshot_external_id"] == "im-real"
@@ -6631,6 +6640,9 @@ class TestTaskRunAPI(BaseTaskAPITest):
"claude_subscription_user_id",
"github_credential_source",
"agent_otel_telemetry_enabled",
+ "agent_proxy_keep_stream_open",
+ "overlap_clone_boot_enabled",
+ "use_modal_network_allowlist",
"stream_presence_gated",
"stream_thin_tail",
"sandbox_id",
@@ -6676,6 +6688,9 @@ class TestTaskRunAPI(BaseTaskAPITest):
assert run.state["claude_subscription_user_id"] == self.user.id
assert run.state["github_credential_source"] == "caller_token" # protected key survives removal
assert run.state["agent_otel_telemetry_enabled"] is False # protected key survives removal
+ assert run.state["agent_proxy_keep_stream_open"] is False
+ assert run.state["overlap_clone_boot_enabled"] is False
+ assert run.state["use_modal_network_allowlist"] is True
assert run.state["stream_presence_gated"] is True # protected key survives removal
assert run.state["stream_thin_tail"] is True # protected key survives removal
assert run.state["sandbox_id"] == "sb-real" # protected key survives removal
diff --git a/products/tasks/backend/tests/test_temporal_client.py b/products/tasks/backend/tests/test_temporal_client.py
index 54f44bf6729d..99f4cb8daab6 100644
--- a/products/tasks/backend/tests/test_temporal_client.py
+++ b/products/tasks/backend/tests/test_temporal_client.py
@@ -230,19 +230,56 @@ class TestExecuteTaskProcessingWorkflow(TestCase):
run.refresh_from_db()
self.assertEqual(run.state["sandbox_event_ingest_enabled"], True)
self.assertEqual(run.state["agent_otel_telemetry_enabled"], True)
- # Patching the shared posthoganalytics module attribute covers both evaluation
- # sites (event ingest in client.py, telemetry in feature_flags.py).
- self.assertEqual(flag.call_count, 2)
- for flag_key in ("tasks-cloud-runs-sandbox-event-ingest", "tasks-agent-run-otel-telemetry"):
+ self.assertEqual(run.state["agent_proxy_keep_stream_open"], True)
+ self.assertEqual(run.state["overlap_clone_boot_enabled"], True)
+ self.assertEqual(run.state["use_modal_network_allowlist"], True)
+ # Patching the shared posthoganalytics module attribute covers every evaluation
+ # site (client.py for the rollout stamps, feature_flags.py for telemetry).
+ self.assertEqual(flag.call_count, 5)
+ actor_distinct_id = f"user_{self.user.id}"
+ for flag_key, expected_distinct_id in (
+ ("tasks-cloud-runs-sandbox-event-ingest", "process_task_workflow"),
+ ("tasks-agent-run-otel-telemetry", "process_task_workflow"),
+ ("tasks-agent-proxy-keep-stream-open", actor_distinct_id),
+ ("tasks-overlap-clone-boot", actor_distinct_id),
+ ("tasks-modal-network-allowlist", actor_distinct_id),
+ ):
flag.assert_any_call(
flag_key,
- distinct_id="process_task_workflow",
+ distinct_id=expected_distinct_id,
groups={"organization": str(self.organization.id)},
group_properties={"organization": {"id": str(self.organization.id)}},
only_evaluate_locally=False,
send_feature_flag_events=False,
)
+ def test_boot_flag_capture_keeps_existing_stamp_and_survives_one_flag_error(self) -> None:
+ run = self._create_run()
+ TaskRun.update_state_atomic(str(run.id), updates={"overlap_clone_boot_enabled": False})
+ client = Mock()
+ client.start_workflow = AsyncMock()
+
+ def _feature_enabled(flag_key: str, **kwargs: object) -> bool:
+ if flag_key == "tasks-agent-proxy-keep-stream-open":
+ raise RuntimeError("flag service unavailable")
+ return True
+
+ with (
+ patch("products.tasks.backend.temporal.client.sync_connect", return_value=client),
+ patch(
+ "products.tasks.backend.temporal.client.posthoganalytics.feature_enabled",
+ side_effect=_feature_enabled,
+ ),
+ ):
+ self._execute_workflow("sync", run, self.user.id)
+
+ run.refresh_from_db()
+ self.assertEqual(run.state["overlap_clone_boot_enabled"], False)
+ self.assertEqual(run.state["agent_proxy_keep_stream_open"], False)
+ self.assertEqual(run.state["use_modal_network_allowlist"], True)
+ self.assertEqual(run.state["sandbox_event_ingest_enabled"], True)
+ self.assertEqual(run.state["agent_otel_telemetry_enabled"], True)
+
def test_captures_sandbox_event_ingest_flag_before_resuming_workflow(self) -> None:
run = self._create_run()
client = Mock()