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.

perf(tasks): fetch the run's feature flags in one request (#122637)

Alessandro Pogliaghid8a057b4bf35
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.py
index c651133caf71..efbd79705ee3 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
@@ -8,6 +8,7 @@ from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
import posthoganalytics
+from posthoganalytics.types import FlagsAndPayloads
from temporalio import activity
from posthog.dataclasses import frozen
@@ -27,13 +28,13 @@ from products.tasks.backend.constants import (
DEV_STACK_PREVIEW_FEATURE_FLAG,
HOGLAND_HOTPLUG_GOLDEN_FEATURE_FLAG,
HOGLAND_SANDBOX_FEATURE_FLAG,
+ MODAL_VM_SANDBOX_FEATURE_FLAG,
OVERLAP_CLONE_BOOT_FEATURE_FLAG,
PI_ACP_FEATURE_FLAG,
PR_LOOP_ENABLED_STATE_KEY,
RTK_DISABLED_FEATURE_FLAG,
STORE_SKILLS_STATE_KEY,
SUBSCRIPTION_PLAN_NAMES,
- get_vm_sandbox_flag_payload,
is_same_run_resume_state,
vm_sandbox_allowed_origin_products,
vm_sandbox_default_base_origin_products,
@@ -387,10 +388,58 @@ class TaskProcessingContext:
}
+PR_LOOP_FEATURE_FLAG = "tasks-pr-loop"
+
+RUN_FEATURE_FLAG_KEYS: list[str] = [
+ PR_LOOP_FEATURE_FLAG,
+ BENJAMIN_FEATURE_FLAG,
+ MODAL_VM_SANDBOX_FEATURE_FLAG,
+ OVERLAP_CLONE_BOOT_FEATURE_FLAG,
+ DESKTOP_WORKSPACE_WARM_FEATURE_FLAG,
+ AGENT_SERVER_SHADOW_FEATURE_FLAG,
+ RTK_DISABLED_FEATURE_FLAG,
+ HOGLAND_SANDBOX_FEATURE_FLAG,
+ HOGLAND_HOTPLUG_GOLDEN_FEATURE_FLAG,
+ DEV_STACK_PREVIEW_FEATURE_FLAG,
+ AGENT_PEER_MESSAGING_FEATURE_FLAG,
+ PI_ACP_FEATURE_FLAG,
+]
+
+
+@StepTimer("flags")
+def _fetch_run_flags(*, distinct_id: str, organization_id: str, run_id: str) -> FlagsAndPayloads:
+ flags: dict[str, Any] = {}
+ payloads: dict[str, Any] = {}
+ try:
+ for only_evaluate_locally in (True, False):
+ missing_keys = [key for key in RUN_FEATURE_FLAG_KEYS if key not in flags]
+ if not missing_keys:
+ break
+ response = posthoganalytics.get_all_flags_and_payloads(
+ distinct_id,
+ groups={"organization": organization_id},
+ group_properties={"organization": {"id": organization_id}},
+ only_evaluate_locally=only_evaluate_locally,
+ flag_keys_to_evaluate=missing_keys,
+ )
+ flags.update(response.get("featureFlags") or {})
+ payloads.update(response.get("featureFlagPayloads") or {})
+ except Exception as e:
+ log_with_activity_context("run_flags_fetch_failed", run_id=run_id, error=str(e))
+ return {"featureFlags": flags, "featureFlagPayloads": payloads}
+
+
+def _flag_enabled(flags: FlagsAndPayloads, key: str) -> bool:
+ return bool((flags.get("featureFlags") or {}).get(key))
+
+
+def _flag_payload(flags: FlagsAndPayloads, key: str) -> object:
+ return (flags.get("featureFlagPayloads") or {}).get(key)
+
+
def _is_peer_messaging_enabled(
*,
- distinct_id: str,
- organization_id: str,
+ flags: FlagsAndPayloads,
run_id: str,
) -> bool:
"""Whether the agent peer-messaging tools should surface in the sandbox.
@@ -398,74 +447,23 @@ def _is_peer_messaging_enabled(
Fail-closed exposure gate only — the peers list/message endpoints enforce the
flag and runtime again server-side, so a stale env var in a resumed sandbox
can never authorize anything."""
- try:
- enabled = bool(
- posthoganalytics.feature_enabled(
- AGENT_PEER_MESSAGING_FEATURE_FLAG,
- 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:
- log_with_activity_context("peer_messaging_flag_check_failed", run_id=run_id, error=str(e))
- return False
+ enabled = _flag_enabled(flags, AGENT_PEER_MESSAGING_FEATURE_FLAG)
if enabled:
log_with_activity_context("peer_messaging_flag_checked", run_id=run_id, peer_messaging_enabled=True)
return enabled
-def _is_pi_acp_enabled(
- *,
- distinct_id: str,
- organization_id: str,
- run_id: str,
-) -> bool:
- try:
- return bool(
- posthoganalytics.feature_enabled(
- PI_ACP_FEATURE_FLAG,
- 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:
- log_with_activity_context("pi_acp_flag_check_failed", run_id=run_id, error=str(e))
- return False
+def _is_pi_acp_enabled(*, flags: FlagsAndPayloads) -> bool:
+ return _flag_enabled(flags, PI_ACP_FEATURE_FLAG)
-def _is_agent_shadow_enabled(
- *,
- distinct_id: str,
- organization_id: str,
- run_id: str,
-) -> bool:
- try:
- return bool(
- posthoganalytics.feature_enabled(
- AGENT_SERVER_SHADOW_FEATURE_FLAG,
- 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:
- log_with_activity_context("agent_shadow_flag_check_failed", run_id=run_id, error=str(e))
- return False
+def _is_agent_shadow_enabled(*, flags: FlagsAndPayloads) -> bool:
+ return _flag_enabled(flags, AGENT_SERVER_SHADOW_FEATURE_FLAG)
-@StepTimer("flag_rtk")
def _is_rtk_enabled(
*,
- distinct_id: str,
- organization_id: str,
+ flags: FlagsAndPayloads,
run_id: str,
state: dict | None = None,
) -> bool:
@@ -474,20 +472,7 @@ def _is_rtk_enabled(
otherwise the per-run state override (the user's toggle in PostHog Desktop settings)
applies. Fails open (enabled, override honored) on flag-service errors so the
default posture is preserved."""
- try:
- disabled = bool(
- posthoganalytics.feature_enabled(
- RTK_DISABLED_FEATURE_FLAG,
- 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:
- log_with_activity_context("rtk_disabled_flag_check_failed", run_id=run_id, error=str(e))
- disabled = False
+ disabled = _flag_enabled(flags, RTK_DISABLED_FEATURE_FLAG)
if disabled:
log_with_activity_context("rtk_disabled_flag_checked", run_id=run_id, rtk_enabled=False)
@@ -569,11 +554,9 @@ def _ensure_codex_account_connected(owner_id: int | None, run_id: str) -> None:
)
-@StepTimer("flag_benjamin")
def _is_benjamin_enabled(
*,
- distinct_id: str,
- organization_id: str,
+ flags: FlagsAndPayloads,
run_id: str,
origin_product: str | None = None,
state: dict | None = None,
@@ -587,19 +570,7 @@ def _is_benjamin_enabled(
if isinstance(state_override, bool):
return state_override
- try:
- payload = posthoganalytics.get_feature_flag_payload(
- BENJAMIN_FEATURE_FLAG,
- distinct_id=distinct_id,
- groups={"organization": organization_id},
- group_properties={"organization": {"id": organization_id}},
- only_evaluate_locally=False,
- send_feature_flag_events=False,
- )
- fraction = _benjamin_rollout_fraction(payload, origin_product)
- except Exception as e:
- log_with_activity_context("benjamin_flag_check_failed", run_id=run_id, error=str(e))
- return False
+ fraction = _benjamin_rollout_fraction(_flag_payload(flags, BENJAMIN_FEATURE_FLAG), origin_product)
if fraction <= 0:
return False
@@ -676,11 +647,9 @@ def _require_template_compatible_with_custom_image(state: dict, custom_image_nam
)
-@StepTimer("flag_sandbox_runtime")
def _resolve_modal_vm_sandbox(
*,
- distinct_id: str,
- organization_id: str,
+ flags: FlagsAndPayloads,
run_id: str,
origin_product: str | None,
custom_image_available: bool = False,
@@ -715,11 +684,7 @@ def _resolve_modal_vm_sandbox(
origin_rollout_percentages: dict[str, float] = {}
default_custom_image: str | None = None
if state_override is None:
- try:
- payload = get_vm_sandbox_flag_payload(distinct_id=distinct_id, organization_id=organization_id)
- except Exception as e:
- log_with_activity_context("modal_vm_sandbox_flag_check_failed", run_id=run_id, error=str(e))
- return VmSandboxDecision(use_vm_sandbox=False)
+ payload = _flag_payload(flags, MODAL_VM_SANDBOX_FEATURE_FLAG)
allowed_origins = vm_sandbox_allowed_origin_products(payload)
default_base_origins = vm_sandbox_default_base_origin_products(payload)
origin_rollout_percentages = vm_sandbox_origin_rollout_percentages(payload)
@@ -788,11 +753,9 @@ def _is_burstable_sandbox_resources_enabled(
return True
-@StepTimer("flag_clone_overlap")
def _is_overlap_clone_boot_enabled(
*,
- distinct_id: str,
- organization_id: str,
+ flags: FlagsAndPayloads,
run_id: str,
state: dict | None = None,
) -> bool:
@@ -805,20 +768,7 @@ def _is_overlap_clone_boot_enabled(
)
return state_override
- try:
- enabled = bool(
- posthoganalytics.feature_enabled(
- OVERLAP_CLONE_BOOT_FEATURE_FLAG,
- 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:
- log_with_activity_context("overlap_clone_boot_flag_check_failed", run_id=run_id, error=str(e))
- return False
+ enabled = _flag_enabled(flags, OVERLAP_CLONE_BOOT_FEATURE_FLAG)
log_with_activity_context(
"overlap_clone_boot_flag_checked",
@@ -828,27 +778,12 @@ def _is_overlap_clone_boot_enabled(
return enabled
-@StepTimer("flag_desktop_workspace")
def _is_desktop_workspace_warm_enabled(
*,
- distinct_id: str,
- organization_id: str,
+ flags: FlagsAndPayloads,
run_id: str,
) -> bool:
- try:
- enabled = bool(
- posthoganalytics.feature_enabled(
- DESKTOP_WORKSPACE_WARM_FEATURE_FLAG,
- 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:
- log_with_activity_context("desktop_workspace_warm_flag_check_failed", run_id=run_id, error=str(e))
- return False
+ enabled = _flag_enabled(flags, DESKTOP_WORKSPACE_WARM_FEATURE_FLAG)
log_with_activity_context(
"desktop_workspace_warm_flag_checked",
@@ -858,11 +793,9 @@ def _is_desktop_workspace_warm_enabled(
return enabled
-@StepTimer("flag_dev_stack_preview")
def _is_dev_stack_preview_enabled(
*,
- distinct_id: str,
- organization_id: str,
+ flags: FlagsAndPayloads,
run_id: str,
origin_product: str | None,
use_modal_vm_sandbox: bool,
@@ -877,20 +810,7 @@ def _is_dev_stack_preview_enabled(
):
return False
- try:
- enabled = bool(
- posthoganalytics.feature_enabled(
- DEV_STACK_PREVIEW_FEATURE_FLAG,
- 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:
- log_with_activity_context("dev_stack_preview_flag_check_failed", run_id=run_id, error=str(e))
- return False
+ enabled = _flag_enabled(flags, DEV_STACK_PREVIEW_FEATURE_FLAG)
log_with_activity_context(
"dev_stack_preview_flag_checked",
@@ -900,11 +820,9 @@ def _is_dev_stack_preview_enabled(
return enabled
-@StepTimer("flag_sandbox_backend")
def _resolve_sandbox_backend(
*,
- distinct_id: str,
- organization_id: str,
+ flags: FlagsAndPayloads,
run_id: str,
state: dict | None,
task_runtime: str,
@@ -956,20 +874,7 @@ def _resolve_sandbox_backend(
log_with_activity_context("sandbox_backend_state_override", run_id=run_id, sandbox_backend="hogland")
return "hogland"
- try:
- enabled = bool(
- posthoganalytics.feature_enabled(
- HOGLAND_SANDBOX_FEATURE_FLAG,
- 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:
- log_with_activity_context("hogland_sandbox_flag_check_failed", run_id=run_id, error=str(e))
- return "modal"
+ enabled = _flag_enabled(flags, HOGLAND_SANDBOX_FEATURE_FLAG)
log_with_activity_context(
"hogland_sandbox_flag_checked",
@@ -1138,17 +1043,10 @@ def get_task_processing_context(input: GetTaskProcessingContextInput) -> TaskPro
# opt in unconditionally — independent of the org-level `tasks-pr-loop` rollout
# that gates other origins. This mirrors the babysitting the Slack coding bot
# gets for its PRs.
- with StepTimer("flag_pr_loop"):
- pr_loop_enabled = (
- task.origin_product == Task.OriginProduct.SIGNAL_REPORT
- or posthoganalytics.feature_enabled(
- "tasks-pr-loop",
- distinct_id=distinct_id,
- groups={"organization": organization_id},
- group_properties={"organization": {"id": organization_id}},
- )
- or False
- ) # Ensure we get a boolean value even if the flag is missing
+ flags = _fetch_run_flags(distinct_id=distinct_id, organization_id=organization_id, run_id=run_id)
+ pr_loop_enabled = task.origin_product == Task.OriginProduct.SIGNAL_REPORT or _flag_enabled(
+ flags, PR_LOOP_FEATURE_FLAG
+ )
log_with_activity_context(f"pr_loop_enabled: {pr_loop_enabled} for this task run", run_id=run_id)
state_updates: dict[str, Any] = {PR_LOOP_ENABLED_STATE_KEY: pr_loop_enabled}
include_live_context = state.get("include_live_context") is not False
@@ -1246,8 +1144,7 @@ def get_task_processing_context(input: GetTaskProcessingContextInput) -> TaskPro
_require_template_compatible_with_custom_image(state, environment_custom_image_name, run_id=run_id)
vm_sandbox_decision = _resolve_modal_vm_sandbox(
- distinct_id=distinct_id,
- organization_id=organization_id,
+ flags=flags,
run_id=run_id,
origin_product=task.origin_product,
custom_image_available=environment_custom_image_name is not None,
@@ -1283,8 +1180,7 @@ def get_task_processing_context(input: GetTaskProcessingContextInput) -> TaskPro
f"burstable_sandbox_resources_enabled: {burstable_sandbox_resources_enabled} for this task run", run_id=run_id
)
overlap_clone_boot_enabled = _is_overlap_clone_boot_enabled(
- distinct_id=distinct_id,
- organization_id=organization_id,
+ flags=flags,
run_id=run_id,
state=state,
)
@@ -1292,27 +1188,20 @@ def get_task_processing_context(input: GetTaskProcessingContextInput) -> TaskPro
f"overlap_clone_boot_enabled: {overlap_clone_boot_enabled} for this task run", run_id=run_id
)
desktop_workspace_warm_enabled = _is_desktop_workspace_warm_enabled(
- distinct_id=distinct_id,
- organization_id=organization_id,
+ flags=flags,
run_id=run_id,
)
log_with_activity_context(
f"desktop_workspace_warm_enabled: {desktop_workspace_warm_enabled} for this task run", run_id=run_id
)
- agent_shadow_enabled = input.launches_agent and _is_agent_shadow_enabled(
- distinct_id=distinct_id,
- organization_id=organization_id,
- run_id=run_id,
- )
+ agent_shadow_enabled = input.launches_agent and _is_agent_shadow_enabled(flags=flags)
rtk_enabled = _is_rtk_enabled(
- distinct_id=distinct_id,
- organization_id=organization_id,
+ flags=flags,
run_id=run_id,
state=state,
)
benjamin_enabled = _is_benjamin_enabled(
- distinct_id=distinct_id,
- organization_id=organization_id,
+ flags=flags,
run_id=run_id,
origin_product=task.origin_product,
state=state,
@@ -1344,8 +1233,7 @@ def get_task_processing_context(input: GetTaskProcessingContextInput) -> TaskPro
sandbox_backend_state = {**state, "sandbox_backend": resumed_backend}
sandbox_backend = _resolve_sandbox_backend(
- distinct_id=distinct_id,
- organization_id=organization_id,
+ flags=flags,
run_id=run_id,
state=sandbox_backend_state,
task_runtime=task.runtime,
@@ -1368,29 +1256,15 @@ def get_task_processing_context(input: GetTaskProcessingContextInput) -> TaskPro
log_with_activity_context(f"sandbox_backend: {sandbox_backend} for this task run", run_id=run_id)
# Pluggable-memory golden: only meaningful on hogland, and org-scoped like the
# backend flag itself. Fails closed to the fixed-size golden on any error.
- use_hogland_hotplug_golden = False
- if sandbox_backend == "hogland":
- try:
- use_hogland_hotplug_golden = bool(
- posthoganalytics.feature_enabled(
- HOGLAND_HOTPLUG_GOLDEN_FEATURE_FLAG,
- 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:
- log_with_activity_context("hogland_hotplug_golden_flag_check_failed", run_id=run_id, error=str(e))
- use_hogland_hotplug_golden = False
+ use_hogland_hotplug_golden = sandbox_backend == "hogland" and _flag_enabled(
+ flags, HOGLAND_HOTPLUG_GOLDEN_FEATURE_FLAG
+ )
log_with_activity_context(
f"use_hogland_hotplug_golden: {use_hogland_hotplug_golden} for this task run", run_id=run_id
)
dev_stack_preview_enabled = _is_dev_stack_preview_enabled(
- distinct_id=distinct_id,
- organization_id=organization_id,
+ flags=flags,
run_id=run_id,
origin_product=task.origin_product,
use_modal_vm_sandbox=use_modal_vm_sandbox,
@@ -1476,14 +1350,8 @@ def get_task_processing_context(input: GetTaskProcessingContextInput) -> TaskPro
# so ACP runs never even evaluate it.
peer_messaging_enabled=task.runtime == Task.Runtime.PI
and _is_peer_messaging_enabled(
- distinct_id=distinct_id,
- organization_id=organization_id,
- run_id=run_id,
- ),
- pi_acp_conversation_enabled=task.runtime == Task.Runtime.PI
- and _is_pi_acp_enabled(
- distinct_id=distinct_id,
- organization_id=organization_id,
+ flags=flags,
run_id=run_id,
),
+ pi_acp_conversation_enabled=task.runtime == Task.Runtime.PI and _is_pi_acp_enabled(flags=flags),
)
diff --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.py
index cb23ed0b86be..ffcc56d98894 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
@@ -1,12 +1,13 @@
from typing import Any
import pytest
-from unittest.mock import patch
+from unittest.mock import call, patch
from django.core.exceptions import ValidationError
from django.test import override_settings
from asgiref.sync import async_to_sync
+from posthoganalytics.types import FlagsAndPayloads
from temporalio.testing import ActivityEnvironment
from posthog.models import OrganizationMembership, User
@@ -21,6 +22,7 @@ from products.tasks.backend.constants import (
CODEX_OWN_SUBSCRIPTION_CLOUD_FEATURE_FLAG,
DESKTOP_WORKSPACE_WARM_FEATURE_FLAG,
DEV_STACK_IMAGE_NAME,
+ DEV_STACK_PREVIEW_FEATURE_FLAG,
HOGLAND_SANDBOX_FEATURE_FLAG,
MODAL_VM_SANDBOX_FEATURE_FLAG,
RTK_DISABLED_FEATURE_FLAG,
@@ -36,10 +38,12 @@ from products.tasks.backend.logic.services.agent_instructions import AgentInstru
from products.tasks.backend.models import TASK_OWNERSHIP_VERSION_STATE_KEY, SandboxEnvironment, Task, TaskRun
from products.tasks.backend.temporal.process_task.activities import provision_sandbox
from products.tasks.backend.temporal.process_task.activities.get_task_processing_context import (
+ RUN_FEATURE_FLAG_KEYS,
GetTaskProcessingContextInput,
TaskProcessingContext,
VmSandboxDecision,
_ensure_subscription_allowed,
+ _fetch_run_flags,
_is_agent_shadow_enabled,
_is_benjamin_enabled,
_is_burstable_sandbox_resources_enabled,
@@ -59,14 +63,21 @@ FEATURE_ENABLED_TARGET = (
"get_task_processing_context.posthoganalytics.feature_enabled"
)
-
-VM_FLAG_PAYLOAD_TARGET = "products.tasks.backend.constants.posthoganalytics.get_feature_flag_payload"
-BENJAMIN_PAYLOAD_TARGET = (
+ALL_FLAGS_TARGET = (
"products.tasks.backend.temporal.process_task.activities."
- "get_task_processing_context.posthoganalytics.get_feature_flag_payload"
+ "get_task_processing_context.posthoganalytics.get_all_flags_and_payloads"
)
+def _flags(flags: dict[str, Any] | None = None, payloads: dict[str, Any] | None = None) -> FlagsAndPayloads:
+ return {"featureFlags": flags or {}, "featureFlagPayloads": payloads or {}}
+
+
+def _failed_flags() -> FlagsAndPayloads:
+ with patch(ALL_FLAGS_TARGET, side_effect=RuntimeError("flag service failed")):
+ return _fetch_run_flags(distinct_id="distinct-id", organization_id="organization-id", run_id="run-id")
+
+
async def _run_context(
activity_environment: ActivityEnvironment, input_data: GetTaskProcessingContextInput
) -> TaskProcessingContext:
@@ -114,79 +125,78 @@ def test_snapshot_resume_requires_a_resume_marker_and_snapshot(state: dict[str,
@pytest.mark.parametrize("flag_value,expected", [(True, True), (False, False), (None, False)])
def test_desktop_workspace_warm_flag_uses_organization_rollout(flag_value, expected):
- with patch(
- "products.tasks.backend.temporal.process_task.activities.get_task_processing_context.posthoganalytics.feature_enabled",
- return_value=flag_value,
- ) as feature_enabled_mock:
- assert (
- _is_desktop_workspace_warm_enabled(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id="run-id",
- )
- is expected
+ assert (
+ _is_desktop_workspace_warm_enabled(
+ flags=_flags({DESKTOP_WORKSPACE_WARM_FEATURE_FLAG: flag_value}), run_id="run-id"
)
-
- feature_enabled_mock.assert_called_once_with(
- DESKTOP_WORKSPACE_WARM_FEATURE_FLAG,
- distinct_id="distinct-id",
- groups={"organization": "organization-id"},
- group_properties={"organization": {"id": "organization-id"}},
- only_evaluate_locally=False,
- send_feature_flag_events=False,
+ is expected
)
-def test_desktop_workspace_warm_flag_fails_closed():
- with patch(
- "products.tasks.backend.temporal.process_task.activities.get_task_processing_context.posthoganalytics.feature_enabled",
- side_effect=RuntimeError("flag service failed"),
- ):
- assert (
- _is_desktop_workspace_warm_enabled(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id="run-id",
- )
- is False
- )
-
-
-def test_agent_shadow_flag_uses_server_side_organization_targeting():
- with patch(FEATURE_ENABLED_TARGET, return_value=True) as feature_enabled_mock:
- assert (
- _is_agent_shadow_enabled(distinct_id="distinct-id", organization_id="organization-id", run_id="run-id")
- is True
- )
-
- feature_enabled_mock.assert_called_once_with(
- AGENT_SERVER_SHADOW_FEATURE_FLAG,
- distinct_id="distinct-id",
- groups={"organization": "organization-id"},
- group_properties={"organization": {"id": "organization-id"}},
- only_evaluate_locally=False,
- send_feature_flag_events=False,
- )
+def test_run_flags_keep_local_results_and_ask_remotely_only_for_the_rest():
+ def all_flags(distinct_id: str, *, only_evaluate_locally: bool, **kwargs: Any) -> FlagsAndPayloads:
+ if only_evaluate_locally:
+ return _flags({RTK_DISABLED_FEATURE_FLAG: True})
+ return _flags({AGENT_SERVER_SHADOW_FEATURE_FLAG: True})
+
+ with patch(ALL_FLAGS_TARGET, side_effect=all_flags) as all_flags_mock:
+ flags = _fetch_run_flags(distinct_id="distinct-id", organization_id="organization-id", run_id="run-id")
+
+ assert _is_agent_shadow_enabled(flags=flags) is True
+ assert _is_rtk_enabled(flags=flags, run_id="run-id") is False
+ targeting = {
+ "groups": {"organization": "organization-id"},
+ "group_properties": {"organization": {"id": "organization-id"}},
+ }
+ assert all_flags_mock.call_args_list == [
+ call("distinct-id", **targeting, only_evaluate_locally=True, flag_keys_to_evaluate=RUN_FEATURE_FLAG_KEYS),
+ call(
+ "distinct-id",
+ **targeting,
+ only_evaluate_locally=False,
+ flag_keys_to_evaluate=[key for key in RUN_FEATURE_FLAG_KEYS if key != RTK_DISABLED_FEATURE_FLAG],
+ ),
+ ]
-def test_agent_shadow_flag_fails_closed():
- with patch(FEATURE_ENABLED_TARGET, side_effect=RuntimeError("flag service failed")):
- assert (
- _is_agent_shadow_enabled(distinct_id="distinct-id", organization_id="organization-id", run_id="run-id")
- is False
- )
+ "read_flag,expected",
+ [
+ (lambda flags: _is_agent_shadow_enabled(flags=flags), False),
+ (lambda flags: _is_desktop_workspace_warm_enabled(flags=flags, run_id="run-id"), False),
+ (lambda flags: _is_rtk_enabled(flags=flags, run_id="run-id"), True),
+ (
+ lambda flags: _is_benjamin_enabled(
+ flags=flags, run_id="run-id", origin_product=Task.OriginProduct.USER_CREATED
+ ),
+ False,
+ ),
+ (
+ lambda flags: (
+ _resolve_modal_vm_sandbox(
+ flags=flags, run_id="run-id", origin_product="user_created", custom_image_available=True
+ ).use_vm_sandbox
+ ),
+ False,
+ ),
+ (lambda flags: _preview_gate(flags=flags), False),
+ ],
+ ids=["agent_shadow", "desktop_workspace_warm", "rtk", "benjamin", "modal_vm_sandbox", "dev_stack_preview"],
+)
+def test_flags_fall_back_to_defaults_when_the_flags_request_fails(read_flag, expected):
+ assert read_flag(_failed_flags()) is expected
def _preview_gate(
*,
+ flags: FlagsAndPayloads,
origin_product: str = Task.OriginProduct.USER_CREATED,
use_modal_vm_sandbox: bool = True,
custom_image_name: str | None = DEV_STACK_IMAGE_NAME,
repository: str | None = "PostHog/PostHog",
) -> bool:
return _is_dev_stack_preview_enabled(
- distinct_id="distinct-id",
- organization_id="organization-id",
+ flags=flags,
run_id="run-id",
origin_product=origin_product,
use_modal_vm_sandbox=use_modal_vm_sandbox,
@@ -210,19 +220,7 @@ def _preview_gate(
],
)
def test_dev_stack_preview_needs_the_flag_and_the_right_shape_of_run(flag_value, overrides, expected):
- with patch(
- "products.tasks.backend.temporal.process_task.activities.get_task_processing_context.posthoganalytics.feature_enabled",
- return_value=flag_value,
- ):
- assert _preview_gate(**overrides) is expected
-
-
-def test_dev_stack_preview_flag_fails_closed():
- with patch(
- "products.tasks.backend.temporal.process_task.activities.get_task_processing_context.posthoganalytics.feature_enabled",
- side_effect=RuntimeError("flag service failed"),
- ):
- assert _preview_gate() is False
+ assert _preview_gate(flags=_flags({DEV_STACK_PREVIEW_FEATURE_FLAG: flag_value}), **overrides) is expected
class TestGetTaskProcessingContextActivity:
@@ -299,15 +297,14 @@ class TestGetTaskProcessingContextActivity:
"products.tasks.backend.temporal.process_task.activities.get_task_processing_context.context_layer_facade.is_context_layer_enabled",
return_value=True,
),
+ patch(ALL_FLAGS_TARGET, return_value=_flags({AGENT_SERVER_SHADOW_FEATURE_FLAG: True})),
):
flag.side_effect = lambda key, distinct_id=None, **kwargs: (
- (key == CLAUDE_OWN_SUBSCRIPTION_CLOUD_FEATURE_FLAG and distinct_id == owner.distinct_id)
- or key == AGENT_SERVER_SHADOW_FEATURE_FLAG
+ key == CLAUDE_OWN_SUBSCRIPTION_CLOUD_FEATURE_FLAG and distinct_id == owner.distinct_id
)
result = async_to_sync(activity_environment.run)(get_task_processing_context, input_data)
assert result.agent_shadow_enabled is launches_agent
- assert (AGENT_SERVER_SHADOW_FEATURE_FLAG in [call.args[0] for call in flag.call_args_list]) is launches_agent
assert isinstance(result, TaskProcessingContext)
assert result.task_id == str(test_task.id)
@@ -354,10 +351,7 @@ class TestGetTaskProcessingContextActivity:
override_settings(
HOGLAND_API_URL="https://hogland.example", HOGLAND_API_TOKEN="hog-tok", CLOUD_DEPLOYMENT="US"
),
- patch(
- FEATURE_ENABLED_TARGET,
- side_effect=lambda key, **kwargs: key == HOGLAND_SANDBOX_FEATURE_FLAG and flag_enabled,
- ),
+ patch(ALL_FLAGS_TARGET, return_value=_flags({HOGLAND_SANDBOX_FEATURE_FLAG: flag_enabled})),
):
result = async_to_sync(_run_context)(
activity_environment,
@@ -699,21 +693,12 @@ class TestGetTaskProcessingContextActivity:
task_run = test_task.create_run()
input_data = GetTaskProcessingContextInput(run_id=str(task_run.id))
- def feature_enabled(flag_key, **kwargs):
- if flag_key == "tasks-pr-loop":
- return flag_value
- return False
-
- with patch(
- "products.tasks.backend.temporal.process_task.activities.get_task_processing_context.posthoganalytics.feature_enabled",
- side_effect=feature_enabled,
- ) as feature_enabled_mock:
+ with patch(ALL_FLAGS_TARGET, return_value=_flags({"tasks-pr-loop": flag_value})) as all_flags_mock:
result = async_to_sync(activity_environment.run)(get_task_processing_context, input_data)
assert result.pr_loop_enabled is expected
- args, kwargs = feature_enabled_mock.call_args_list[0]
- assert args[0] == "tasks-pr-loop"
- assert kwargs["distinct_id"] == get_actor_distinct_id(test_task.created_by)
+ args, kwargs = all_flags_mock.call_args
+ assert args[0] == get_actor_distinct_id(test_task.created_by)
org_id = str(test_task.team.organization_id)
assert kwargs["groups"] == {"organization": org_id}
assert kwargs["group_properties"] == {"organization": {"id": org_id}}
@@ -806,19 +791,10 @@ class TestGetTaskProcessingContextActivity:
task_run = test_task.create_run()
input_data = GetTaskProcessingContextInput(run_id=str(task_run.id))
- def feature_enabled(flag_key, **kwargs):
- return False # `tasks-pr-loop` disabled for the org
-
- with patch(
- "products.tasks.backend.temporal.process_task.activities.get_task_processing_context.posthoganalytics.feature_enabled",
- side_effect=feature_enabled,
- ) as feature_enabled_mock:
+ with patch(ALL_FLAGS_TARGET, return_value=_flags({"tasks-pr-loop": False})):
result = async_to_sync(activity_environment.run)(get_task_processing_context, input_data)
assert result.pr_loop_enabled is True
- # The signal_report origin short-circuits the gate, so the flag is never consulted.
- called_flags = [call.args[0] for call in feature_enabled_mock.call_args_list]
- assert "tasks-pr-loop" not in called_flags
@pytest.mark.django_db(transaction=True)
@pytest.mark.parametrize("pr_loop_flag", [False, True])
@@ -826,10 +802,7 @@ class TestGetTaskProcessingContextActivity:
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",
- side_effect=lambda flag_key, **kwargs: pr_loop_flag if flag_key == "tasks-pr-loop" else False,
- ):
+ with patch(ALL_FLAGS_TARGET, return_value=_flags({"tasks-pr-loop": pr_loop_flag})):
result = async_to_sync(activity_environment.run)(get_task_processing_context, input_data)
assert result.pr_babysit_enabled is pr_loop_flag
@@ -858,88 +831,34 @@ class TestGetTaskProcessingContextActivity:
],
)
def test_rtk_enabled_defaults_on_with_kill_switch(self, kill_switch_value, expected):
- with patch(
- "products.tasks.backend.temporal.process_task.activities.get_task_processing_context.posthoganalytics.feature_enabled",
- return_value=kill_switch_value,
- ) as feature_enabled_mock:
- assert (
- _is_rtk_enabled(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id="run-id",
- )
- is expected
- )
-
- feature_enabled_mock.assert_called_once_with(
- RTK_DISABLED_FEATURE_FLAG,
- distinct_id="distinct-id",
- groups={"organization": "organization-id"},
- group_properties={"organization": {"id": "organization-id"}},
- only_evaluate_locally=False,
- send_feature_flag_events=False,
+ assert (
+ _is_rtk_enabled(flags=_flags({RTK_DISABLED_FEATURE_FLAG: kill_switch_value}), run_id="run-id") is expected
)
- def test_rtk_enabled_fails_open_on_flag_error(self):
- with patch(
- "products.tasks.backend.temporal.process_task.activities.get_task_processing_context.posthoganalytics.feature_enabled",
- side_effect=RuntimeError("flag service failed"),
- ):
- assert (
- _is_rtk_enabled(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id="run-id",
- )
- is True
- )
-
@pytest.mark.parametrize("state_override", [True, False])
def test_rtk_enabled_state_override_applies_when_kill_switch_inactive(self, state_override):
- with patch(
- "products.tasks.backend.temporal.process_task.activities.get_task_processing_context.posthoganalytics.feature_enabled",
- return_value=False,
- ):
- assert (
- _is_rtk_enabled(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id="run-id",
- state={"rtk_enabled": state_override},
- )
- is state_override
+ assert (
+ _is_rtk_enabled(
+ flags=_flags({RTK_DISABLED_FEATURE_FLAG: False}),
+ run_id="run-id",
+ state={"rtk_enabled": state_override},
)
+ is state_override
+ )
@pytest.mark.parametrize("state_override", [True, False])
def test_rtk_kill_switch_beats_any_state_override(self, state_override):
- with patch(
- "products.tasks.backend.temporal.process_task.activities.get_task_processing_context.posthoganalytics.feature_enabled",
- return_value=True,
- ):
- assert (
- _is_rtk_enabled(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id="run-id",
- state={"rtk_enabled": state_override},
- )
- is False
+ assert (
+ _is_rtk_enabled(
+ flags=_flags({RTK_DISABLED_FEATURE_FLAG: True}),
+ run_id="run-id",
+ state={"rtk_enabled": state_override},
)
+ is False
+ )
def test_rtk_flag_error_still_honors_state_override(self):
- with patch(
- "products.tasks.backend.temporal.process_task.activities.get_task_processing_context.posthoganalytics.feature_enabled",
- side_effect=RuntimeError("flag service failed"),
- ):
- assert (
- _is_rtk_enabled(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id="run-id",
- state={"rtk_enabled": False},
- )
- is False
- )
+ assert _is_rtk_enabled(flags=_failed_flags(), run_id="run-id", state={"rtk_enabled": False}) is False
SUBSCRIPTION_CASES = [
("claude", CLAUDE_OWN_SUBSCRIPTION_CLOUD_FEATURE_FLAG, "Claude plan", "Claude"),
@@ -1000,55 +919,26 @@ class TestGetTaskProcessingContextActivity:
@pytest.mark.parametrize("launched_value", [True, False])
def test_benjamin_launch_persisted_value_pins_later_resolutions(self, launched_value):
- with patch(BENJAMIN_PAYLOAD_TARGET) as payload_mock:
- assert (
- _is_benjamin_enabled(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id="run-id",
- origin_product=Task.OriginProduct.USER_CREATED,
- state={"benjamin_effective": launched_value, "benjamin_enabled": not launched_value},
- )
- is launched_value
+ assert (
+ _is_benjamin_enabled(
+ flags=_flags(payloads={BENJAMIN_FEATURE_FLAG: {"default": 0 if launched_value else 1}}),
+ run_id="run-id",
+ origin_product=Task.OriginProduct.USER_CREATED,
+ state={"benjamin_effective": launched_value, "benjamin_enabled": not launched_value},
)
-
- payload_mock.assert_not_called()
+ is launched_value
+ )
@pytest.mark.parametrize("state_override", [True, False])
def test_benjamin_state_override_wins_without_consulting_the_flag(self, state_override):
- with patch(BENJAMIN_PAYLOAD_TARGET) as payload_mock:
- assert (
- _is_benjamin_enabled(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id="run-id",
- origin_product=Task.OriginProduct.USER_CREATED,
- state={"benjamin_enabled": state_override},
- )
- is state_override
- )
-
- payload_mock.assert_not_called()
-
- def test_benjamin_disabled_when_flag_serves_no_payload(self):
- with patch(BENJAMIN_PAYLOAD_TARGET, return_value=None) as payload_mock:
- assert (
- _is_benjamin_enabled(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id="run-id",
- origin_product=Task.OriginProduct.USER_CREATED,
- )
- is False
+ assert (
+ _is_benjamin_enabled(
+ flags=_flags(payloads={BENJAMIN_FEATURE_FLAG: {"default": 0 if state_override else 1}}),
+ run_id="run-id",
+ origin_product=Task.OriginProduct.USER_CREATED,
+ state={"benjamin_enabled": state_override},
)
-
- payload_mock.assert_called_once_with(
- BENJAMIN_FEATURE_FLAG,
- distinct_id="distinct-id",
- groups={"organization": "organization-id"},
- group_properties={"organization": {"id": "organization-id"}},
- only_evaluate_locally=False,
- send_feature_flag_events=False,
+ is state_override
)
@pytest.mark.parametrize(
@@ -1068,16 +958,14 @@ class TestGetTaskProcessingContextActivity:
],
)
def test_benjamin_applies_the_payload_rollout_fraction(self, payload, origin_product, expected):
- with patch(BENJAMIN_PAYLOAD_TARGET, return_value=payload):
- assert (
- _is_benjamin_enabled(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id="run-id",
- origin_product=origin_product,
- )
- is expected
+ assert (
+ _is_benjamin_enabled(
+ flags=_flags(payloads={BENJAMIN_FEATURE_FLAG: payload}),
+ run_id="run-id",
+ origin_product=origin_product,
)
+ is expected
+ )
@pytest.mark.parametrize(
"run_id, fraction, expected",
@@ -1089,76 +977,15 @@ class TestGetTaskProcessingContextActivity:
],
)
def test_benjamin_buckets_runs_deterministically_by_run_id(self, run_id, fraction, expected):
- with patch(BENJAMIN_PAYLOAD_TARGET, return_value={"default": fraction}):
- assert (
- _is_benjamin_enabled(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id=run_id,
- origin_product=Task.OriginProduct.USER_CREATED,
- )
- is expected
- )
-
- def test_benjamin_fails_closed_on_flag_error(self):
- with patch(BENJAMIN_PAYLOAD_TARGET, side_effect=RuntimeError("flag service failed")):
- assert (
- _is_benjamin_enabled(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id="run-id",
- origin_product=Task.OriginProduct.USER_CREATED,
- )
- is False
- )
-
- @pytest.mark.parametrize(
- "payload, expected",
- [
- ('{"origin_products": ["user_created"]}', True),
- (None, False),
- ],
- )
- def test_modal_vm_sandbox_flag_uses_organization_rollout(self, payload, expected):
- with patch(
- VM_FLAG_PAYLOAD_TARGET,
- return_value=payload,
- ) as payload_mock:
- assert (
- _resolve_modal_vm_sandbox(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id="run-id",
- origin_product="user_created",
- custom_image_available=True,
- ).use_vm_sandbox
- is expected
+ assert (
+ _is_benjamin_enabled(
+ flags=_flags(payloads={BENJAMIN_FEATURE_FLAG: {"default": fraction}}),
+ run_id=run_id,
+ origin_product=Task.OriginProduct.USER_CREATED,
)
-
- payload_mock.assert_called_once_with(
- MODAL_VM_SANDBOX_FEATURE_FLAG,
- distinct_id="distinct-id",
- groups={"organization": "organization-id"},
- group_properties={"organization": {"id": "organization-id"}},
- only_evaluate_locally=False,
- send_feature_flag_events=False,
+ is expected
)
- def test_modal_vm_sandbox_flag_fails_closed(self):
- with patch(
- VM_FLAG_PAYLOAD_TARGET,
- side_effect=RuntimeError("flag service failed"),
- ):
- assert (
- _resolve_modal_vm_sandbox(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id="run-id",
- origin_product="user_created",
- ).use_vm_sandbox
- is False
- )
-
@pytest.mark.parametrize(
"origin_product, custom_image_available, expected",
[
@@ -1168,64 +995,47 @@ class TestGetTaskProcessingContextActivity:
],
)
def test_modal_vm_sandbox_state_override_skips_flag_check(self, origin_product, custom_image_available, expected):
- with patch(
- VM_FLAG_PAYLOAD_TARGET,
- return_value=None,
- ) as payload_mock:
- assert (
- _resolve_modal_vm_sandbox(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id="run-id",
- origin_product=origin_product,
- custom_image_available=custom_image_available,
- state={"use_modal_vm_sandbox": True},
- ).use_vm_sandbox
- is expected
- )
-
- payload_mock.assert_not_called()
+ assert (
+ _resolve_modal_vm_sandbox(
+ flags=_flags(),
+ run_id="run-id",
+ origin_product=origin_product,
+ custom_image_available=custom_image_available,
+ state={"use_modal_vm_sandbox": True},
+ ).use_vm_sandbox
+ is expected
+ )
def test_modal_vm_sandbox_custom_template_forces_gvisor_over_default_base(self):
# A rollout that names the run's origin cannot move a custom-template run onto the VM
# image, which carries none of the template's tooling; the flag is not consulted.
- with patch(
- VM_FLAG_PAYLOAD_TARGET,
- return_value='{"default_base_origin_products": ["autoresearch"]}',
- ) as payload_mock:
- assert (
- _resolve_modal_vm_sandbox(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id="run-id",
- origin_product="autoresearch",
- state={"sandbox_template": "autoresearch_base"},
- ).use_vm_sandbox
- is False
- )
-
- payload_mock.assert_not_called()
+ assert (
+ _resolve_modal_vm_sandbox(
+ flags=_flags(
+ payloads={MODAL_VM_SANDBOX_FEATURE_FLAG: '{"default_base_origin_products": ["autoresearch"]}'}
+ ),
+ run_id="run-id",
+ origin_product="autoresearch",
+ state={"sandbox_template": "autoresearch_base"},
+ ).use_vm_sandbox
+ is False
+ )
def test_modal_vm_sandbox_false_state_override_forces_gvisor_over_default_base(self):
# A trusted server-set use_modal_vm_sandbox=False forces gVisor even when the org's payload
# would place this origin on the VM base; the bool override also skips the flag fetch.
- with patch(
- VM_FLAG_PAYLOAD_TARGET,
- return_value='{"default_base_origin_products": ["user_created"]}',
- ) as payload_mock:
- assert (
- _resolve_modal_vm_sandbox(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id="run-id",
- origin_product="user_created",
- custom_image_available=True,
- state={"use_modal_vm_sandbox": False},
- ).use_vm_sandbox
- is False
- )
-
- payload_mock.assert_not_called()
+ assert (
+ _resolve_modal_vm_sandbox(
+ flags=_flags(
+ payloads={MODAL_VM_SANDBOX_FEATURE_FLAG: '{"default_base_origin_products": ["user_created"]}'}
+ ),
+ run_id="run-id",
+ origin_product="user_created",
+ custom_image_available=True,
+ state={"use_modal_vm_sandbox": False},
+ ).use_vm_sandbox
+ is False
+ )
@pytest.mark.parametrize(
"origin_product, payload, custom_image_available, expected",
@@ -1262,20 +1072,15 @@ class TestGetTaskProcessingContextActivity:
],
)
def test_modal_vm_sandbox_origin_product_gating(self, origin_product, payload, custom_image_available, expected):
- with patch(
- VM_FLAG_PAYLOAD_TARGET,
- return_value=payload,
- ):
- assert (
- _resolve_modal_vm_sandbox(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id="run-id",
- origin_product=origin_product,
- custom_image_available=custom_image_available,
- ).use_vm_sandbox
- is expected
- )
+ assert (
+ _resolve_modal_vm_sandbox(
+ flags=_flags(payloads={MODAL_VM_SANDBOX_FEATURE_FLAG: payload}),
+ run_id="run-id",
+ origin_product=origin_product,
+ custom_image_available=custom_image_available,
+ ).use_vm_sandbox
+ is expected
+ )
@pytest.mark.parametrize(
"payload, expected",
@@ -1370,37 +1175,34 @@ class TestGetTaskProcessingContextActivity:
],
)
def test_modal_vm_sandbox_default_custom_image_resolution(self, origin_product, expected):
- with patch(
- VM_FLAG_PAYLOAD_TARGET,
- return_value='{"default_base_origin_products": ["user_created"], "default_custom_image": "posthog-dev-stack"}',
- ):
- assert (
- _resolve_modal_vm_sandbox(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id="run-id",
- origin_product=origin_product,
- )
- == expected
+ assert (
+ _resolve_modal_vm_sandbox(
+ flags=_flags(
+ payloads={
+ MODAL_VM_SANDBOX_FEATURE_FLAG: '{"default_base_origin_products": ["user_created"], "default_custom_image": "posthog-dev-stack"}'
+ }
+ ),
+ run_id="run-id",
+ origin_product=origin_product,
)
+ == expected
+ )
def test_modal_vm_sandbox_state_override_never_gets_default_custom_image(self):
# Image-builder runs (trusted state override) must keep layering on the plain VM
# base: the flag is never consulted, so the org default image cannot apply.
- with patch(
- VM_FLAG_PAYLOAD_TARGET,
- return_value='{"default_base_origin_products": ["image_builder"], "default_custom_image": "posthog-dev-stack"}',
- ) as payload_mock:
- decision = _resolve_modal_vm_sandbox(
- distinct_id="distinct-id",
- organization_id="organization-id",
- run_id="run-id",
- origin_product="image_builder",
- state={"use_modal_vm_sandbox": True},
- )
+ decision = _resolve_modal_vm_sandbox(
+ flags=_flags(
+ payloads={
+ MODAL_VM_SANDBOX_FEATURE_FLAG: '{"default_base_origin_products": ["image_builder"], "default_custom_image": "posthog-dev-stack"}'
+ }
+ ),
+ run_id="run-id",
+ origin_product="image_builder",
+ state={"use_modal_vm_sandbox": True},
+ )
assert decision == VmSandboxDecision(use_vm_sandbox=True, default_custom_image=None)
- payload_mock.assert_not_called()
@pytest.mark.parametrize(
"state,expected",
@@ -1436,8 +1238,12 @@ class TestGetTaskProcessingContextActivity:
input_data = GetTaskProcessingContextInput(run_id=str(task_run.id))
with patch(
- VM_FLAG_PAYLOAD_TARGET,
- return_value='{"default_base_origin_products": ["user_created"], "default_custom_image": "posthog-dev-stack"}',
+ ALL_FLAGS_TARGET,
+ return_value=_flags(
+ payloads={
+ MODAL_VM_SANDBOX_FEATURE_FLAG: '{"default_base_origin_products": ["user_created"], "default_custom_image": "posthog-dev-stack"}'
+ }
+ ),
):
result = async_to_sync(activity_environment.run)(get_task_processing_context, input_data)
@@ -1457,14 +1263,13 @@ class TestGetTaskProcessingContextActivity:
task_run = test_task.create_run()
input_data = GetTaskProcessingContextInput(run_id=str(task_run.id))
- with (
- patch(
- VM_FLAG_PAYLOAD_TARGET,
- return_value='{"default_base_origin_products": ["user_created"], "default_custom_image": "posthog-dev-stack"}',
- ),
- patch(
- "products.tasks.backend.temporal.process_task.activities.get_task_processing_context.posthoganalytics.feature_enabled",
- return_value=True,
+ with patch(
+ ALL_FLAGS_TARGET,
+ return_value=_flags(
+ {HOGLAND_SANDBOX_FEATURE_FLAG: True},
+ payloads={
+ MODAL_VM_SANDBOX_FEATURE_FLAG: '{"default_base_origin_products": ["user_created"], "default_custom_image": "posthog-dev-stack"}'
+ },
),
):
result = async_to_sync(activity_environment.run)(get_task_processing_context, input_data)
@@ -1531,8 +1336,7 @@ _HOGLAND_SETTINGS = {"HOGLAND_API_URL": "https://hogland.example", "HOGLAND_API_
class TestResolveSandboxBackend:
def _resolve(self, **overrides):
kwargs: dict[str, Any] = {
- "distinct_id": "user-1",
- "organization_id": "org-1",
+ "flags": _flags(),
"run_id": "run-1",
"state": None,
"task_runtime": "acp",
@@ -1542,11 +1346,7 @@ class TestResolveSandboxBackend:
return _resolve_sandbox_backend(**kwargs)
def _resolve_with_flag(self, flag_enabled, **overrides):
- with patch(
- "products.tasks.backend.temporal.process_task.activities.get_task_processing_context.posthoganalytics.feature_enabled",
- return_value=flag_enabled,
- ):
- return self._resolve(**overrides)
+ return self._resolve(flags=_flags({HOGLAND_SANDBOX_FEATURE_FLAG: flag_enabled}), **overrides)
@override_settings(**_HOGLAND_SETTINGS)
def test_flag_routes_a_plain_run_to_hogland(self):
@@ -1585,19 +1385,11 @@ class TestResolveSandboxBackend:
@override_settings(HOGLAND_API_URL=None, HOGLAND_API_TOKEN=None, HOGLAND_API_TOKEN_FILE=None)
def test_unconfigured_hogland_stays_on_modal_without_evaluating_the_flag(self):
- with patch(
- "products.tasks.backend.temporal.process_task.activities.get_task_processing_context.posthoganalytics.feature_enabled",
- ) as feature_enabled_mock:
- assert self._resolve() == "modal"
- feature_enabled_mock.assert_not_called()
+ assert self._resolve_with_flag(True) == "modal"
@override_settings(**_HOGLAND_SETTINGS)
def test_flag_evaluation_failure_fails_closed_to_modal(self):
- with patch(
- "products.tasks.backend.temporal.process_task.activities.get_task_processing_context.posthoganalytics.feature_enabled",
- side_effect=RuntimeError("flag service down"),
- ):
- assert self._resolve() == "modal"
+ assert self._resolve(flags=_failed_flags()) == "modal"
@pytest.mark.parametrize(
"override,flag_enabled,expected",