: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
feat(tasks): read timeouts from the task profile (#125252)
Charles Viende411b17d2fc
diff --git a/products/tasks/backend/temporal/constants.py b/products/tasks/backend/temporal/constants.pyindex fe6d2d1393d1..45d5ee09efb1 100644--- a/products/tasks/backend/temporal/constants.py+++ b/products/tasks/backend/temporal/constants.py@@ -49,7 +49,11 @@ POSTHOG_AI_ORIGIN_PRODUCT = "posthog_ai"def resolve_inactivity_timeout(- *, is_user_origin: bool = False, origin_product: str | None = None, state: dict | None = None+ *,+ is_user_origin: bool = False,+ origin_product: str | None = None,+ state: dict | None = None,+ profile_idle_minutes: int | None = None,) -> timedelta:"""Effective inactivity timeout for a task run, in priority order.@@ -59,10 +63,11 @@ def resolve_inactivity_timeout(2. The `TASKS_INACTIVITY_TIMEOUT_SECONDS` env var (global fallback, e.g.`=30` to force a fast shutdown for local resume-flow testing).3. The test default (short, so orphaned runs don't pin a worker).- 4. The origin-aware production default (longer for user-driven runs, shorter for+ 4. The task profile's `idle_minutes`, when the task has a profile.+ 5. The origin-aware production default (longer for user-driven runs, shorter forPostHog AI's chat-shaped ones).- `origin_product` narrows step 4 only. It stays a plain string so callers can pass+ `origin_product` narrows step 5 only. It stays a plain string so callers can pass`task.origin_product` straight through without this module importing Django models."""per_task = (state or {}).get("inactivity_timeout_seconds")@@ -72,6 +77,8 @@ def resolve_inactivity_timeout(return timedelta(seconds=settings.TASKS_INACTIVITY_TIMEOUT_SECONDS)if settings.TEST:return timedelta(seconds=INACTIVITY_TIMEOUT_TEST_SECONDS)+ if profile_idle_minutes is not None:+ return timedelta(minutes=profile_idle_minutes)if origin_product == POSTHOG_AI_ORIGIN_PRODUCT:return timedelta(seconds=POSTHOG_AI_IDLE_TIMEOUT_SECONDS)return timedelta(seconds=INACTIVITY_TIMEOUT_USER_SECONDS if is_user_origin else INACTIVITY_TIMEOUT_DEFAULT_SECONDS)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 2c44fe2fce5a..2532126f68d4 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@@ -48,6 +48,7 @@ from products.tasks.backend.exceptions import (TaskRunNotReadyError,)from products.tasks.backend.facade.api import ensure_task_run_session+from products.tasks.backend.facade.profiles import TaskProfile, TaskProfileError, get_task_profilefrom products.tasks.backend.feature_flags import own_subscription_cloud_enabledfrom products.tasks.backend.logic.model_access import ModelAccess, resolve_model_accessfrom products.tasks.backend.logic.services.agent_instructions import agent_instructions_state_update@@ -111,6 +112,9 @@ class TaskProcessingContext:repository: str | Nonedistinct_id: strorigin_product: str | None = None+ profile_key: str | None = None+ profile_idle_minutes: int | None = None+ profile_max_wake_minutes: int | None = Nonestartup_requested_at: str | None = Nonetask_runtime: str = Task.Runtime.ACPenvironment: str | None = None@@ -303,7 +307,10 @@ class TaskProcessingContext:def inactivity_timeout(self) -> timedelta:"""Idle time before the workflow times the run out; longer for user-driven runs."""return resolve_inactivity_timeout(- is_user_origin=self._is_user_origin(), origin_product=self.origin_product, state=self.state+ is_user_origin=self._is_user_origin(),+ origin_product=self.origin_product,+ state=self.state,+ profile_idle_minutes=self.profile_idle_minutes,)def _is_user_origin(self) -> bool:@@ -321,13 +328,22 @@ class TaskProcessingContext:sessions can legitimately run for hours, so they are uncapped — except interactivesignals-origin runs, whose inference is unbilled and which therefore get their own(longer) ceiling, resolved activity-side into the field below. Autonomous runs get- the cap as a safety net unless the setting disables it deployment-wide.+ the cap as a safety net unless the setting disables it deployment-wide. A task with+ a profile takes both the exemption and the cap from the profile's `max_wake_minutes`."""- if self.mode == "interactive" or self._is_user_origin():+ if self.mode == "interactive" or self._exempt_from_max_run_duration():if self.interactive_max_run_duration_seconds:return timedelta(seconds=self.interactive_max_run_duration_seconds)return None- return resolve_max_run_duration()+ cap = resolve_max_run_duration()+ if cap is None or self.profile_max_wake_minutes is None:+ return cap+ return timedelta(minutes=self.profile_max_wake_minutes)++ def _exempt_from_max_run_duration(self) -> bool:+ if self.profile_key:+ return self.profile_max_wake_minutes is None+ return self._is_user_origin()def sandbox_resource_overrides(self) -> dict[str, float | int]:"""Per-task SandboxConfig overrides (compute + TTL), clamped to server-owned bounds.@@ -872,6 +888,16 @@ def _resolve_sandbox_backend(return "hogland" if enabled else "modal"+def _stored_task_profile(task: Task, run_id: str) -> TaskProfile | None:+ if not task.profile:+ return None+ try:+ return get_task_profile(task.profile)+ except TaskProfileError:+ log_with_activity_context("task_profile_not_registered", run_id=run_id, profile=task.profile)+ return None++def _compile_effective_network_policy(allowed_domains: list[str]) -> EffectiveNetworkPolicy:debug_domains = _get_debug_only_domains() if settings.DEBUG else []debug_ports = _get_debug_only_ports() if settings.DEBUG else []@@ -1275,6 +1301,8 @@ def get_task_processing_context(input: GetTaskProcessingContextInput) -> TaskProif user_github_integration is not None:user_github_integration_id = str(user_github_integration.integration.id)+ profile = _stored_task_profile(task, run_id)+return TaskProcessingContext(task_id=str(task.id),run_id=run_id,@@ -1287,6 +1315,9 @@ def get_task_processing_context(input: GetTaskProcessingContextInput) -> TaskProrepository=run_repository,distinct_id=distinct_id,origin_product=task.origin_product,+ profile_key=profile.key if profile else None,+ profile_idle_minutes=profile.idle_minutes if profile else None,+ profile_max_wake_minutes=profile.max_wake_minutes if profile else None,task_runtime=task.runtime,environment=task_run.environment,task_created_by_id=task.created_by_id,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.pyindex 9034104086f1..2a92031aa320 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@@ -34,6 +34,7 @@ from products.tasks.backend.constants import (vm_sandbox_origin_rollout_percentages,)from products.tasks.backend.exceptions import ProcessTaskFatalError, TaskInvalidStateError, TaskRunNotReadyError+from products.tasks.backend.facade.profiles import get_task_profilefrom products.tasks.backend.logic.services.agent_instructions import AgentInstructionsStorefrom products.tasks.backend.models import TASK_OWNERSHIP_VERSION_STATE_KEY, SandboxEnvironment, Task, TaskRunfrom products.tasks.backend.temporal.process_task.activities import provision_sandbox@@ -445,6 +446,33 @@ class TestGetTaskProcessingContextActivity:finally:self._cleanup_task(task)+ @pytest.mark.django_db(transaction=True)+ @pytest.mark.parametrize(+ "stored_profile,expected_profile",+ [+ (None, None),+ ("business_knowledge", "business_knowledge"),+ ("test.unregistered", None),+ ],+ )+ def test_get_task_processing_context_carries_the_stored_profile_timeouts(+ self, activity_environment, test_task, stored_profile, expected_profile+ ):+ test_task.profile = stored_profile+ test_task.save(update_fields=["profile"])+ task_run = test_task.create_run()++ result = async_to_sync(activity_environment.run)(+ get_task_processing_context, GetTaskProcessingContextInput(run_id=str(task_run.id))+ )++ profile = get_task_profile(expected_profile) if expected_profile else None+ assert (result.profile_key, result.profile_idle_minutes, result.profile_max_wake_minutes) == (+ expected_profile,+ profile.idle_minutes if profile else None,+ profile.max_wake_minutes if profile else None,+ )+@pytest.mark.django_db(transaction=True)def test_get_task_processing_context_with_create_pr_false(self, activity_environment, test_task):task_run = test_task.create_run()diff --git a/products/tasks/backend/temporal/process_task/tests/test_inactivity_timeout.py b/products/tasks/backend/temporal/process_task/tests/test_inactivity_timeout.pyindex aed90dc813b4..7fe32e2d563c 100644--- a/products/tasks/backend/temporal/process_task/tests/test_inactivity_timeout.py+++ b/products/tasks/backend/temporal/process_task/tests/test_inactivity_timeout.py@@ -20,21 +20,38 @@ from products.tasks.backend.temporal.process_task.activities.get_task_processingclass TestResolveInactivityTimeout(SimpleTestCase):@parameterized.expand([- ("user_origin", True, None, INACTIVITY_TIMEOUT_USER_SECONDS),- ("non_user_origin", False, "signals_scout", INACTIVITY_TIMEOUT_DEFAULT_SECONDS),- ("posthog_ai", False, POSTHOG_AI_ORIGIN_PRODUCT, POSTHOG_AI_IDLE_TIMEOUT_SECONDS),+ ("user_origin", True, None, None, INACTIVITY_TIMEOUT_USER_SECONDS),+ ("non_user_origin", False, "signals_scout", None, INACTIVITY_TIMEOUT_DEFAULT_SECONDS),+ ("posthog_ai", False, POSTHOG_AI_ORIGIN_PRODUCT, None, POSTHOG_AI_IDLE_TIMEOUT_SECONDS),+ ("profile_on_user_origin", True, None, 45, 45 * 60),+ ("profile_on_posthog_ai", False, POSTHOG_AI_ORIGIN_PRODUCT, 45, 45 * 60),])@override_settings(TEST=False, TASKS_INACTIVITY_TIMEOUT_SECONDS=0)- def test_production_default_depends_on_origin(self, _name, is_user_origin, origin_product, expected_seconds):- result = resolve_inactivity_timeout(is_user_origin=is_user_origin, origin_product=origin_product)+ def test_production_default_depends_on_profile_then_origin(+ self, _name, is_user_origin, origin_product, profile_idle_minutes, expected_seconds+ ):+ result = resolve_inactivity_timeout(+ is_user_origin=is_user_origin, origin_product=origin_product, profile_idle_minutes=profile_idle_minutes+ )self.assertEqual(result, timedelta(seconds=expected_seconds))+ @override_settings(TEST=False, TASKS_INACTIVITY_TIMEOUT_SECONDS=0)+ def test_per_task_override_wins_over_profile(self):+ result = resolve_inactivity_timeout(state={"inactivity_timeout_seconds": 600}, profile_idle_minutes=45)+ self.assertEqual(result, timedelta(seconds=600))+def test_posthog_ai_origin_constant_matches_model_enum(self):self.assertEqual(POSTHOG_AI_ORIGIN_PRODUCT, Task.OriginProduct.POSTHOG_AI.value)+ @parameterized.expand(+ [+ ("origin", {}, POSTHOG_AI_IDLE_TIMEOUT_SECONDS),+ ("profile", {"profile_key": "test.profile", "profile_idle_minutes": 45}, 45 * 60),+ ]+ )@override_settings(TEST=False, TASKS_INACTIVITY_TIMEOUT_SECONDS=0)- def test_context_forwards_its_origin_to_the_resolver(self):+ def test_context_forwards_its_origin_and_profile_to_the_resolver(self, _name, profile_fields, expected_seconds):# Guards the wiring, not the resolver: dropping `origin_product=` at this call site# would leave every resolver case green while PostHog AI silently fell back to 30 minutes.context = TaskProcessingContext(@@ -47,8 +64,9 @@ class TestResolveInactivityTimeout(SimpleTestCase):repository="explore-science/paper-wizard-frontend",distinct_id="distinct",origin_product=POSTHOG_AI_ORIGIN_PRODUCT,+ **profile_fields,)- self.assertEqual(context.inactivity_timeout(), timedelta(seconds=POSTHOG_AI_IDLE_TIMEOUT_SECONDS))+ self.assertEqual(context.inactivity_timeout(), timedelta(seconds=expected_seconds))@override_settings(TEST=True, TASKS_INACTIVITY_TIMEOUT_SECONDS=0)def test_test_default_is_short_regardless_of_origin(self):diff --git a/products/tasks/backend/temporal/process_task/tests/test_max_run_duration.py b/products/tasks/backend/temporal/process_task/tests/test_max_run_duration.pyindex 34ccf8ae956b..101ee2d69da7 100644--- a/products/tasks/backend/temporal/process_task/tests/test_max_run_duration.py+++ b/products/tasks/backend/temporal/process_task/tests/test_max_run_duration.py@@ -12,7 +12,11 @@ DEFAULT_CAP_SECONDS = 3 * 60 * 60def _context(- state: dict | None, *, origin_product: str | None = Task.OriginProduct.WORKFLOW.value+ state: dict | None,+ *,+ origin_product: str | None = Task.OriginProduct.WORKFLOW.value,+ profile_key: str | None = None,+ profile_max_wake_minutes: int | None = None,) -> TaskProcessingContext:return TaskProcessingContext(task_id="task-id",@@ -25,6 +29,8 @@ def _context(distinct_id="distinct",state=state,origin_product=origin_product,+ profile_key=profile_key,+ profile_max_wake_minutes=profile_max_wake_minutes,)@@ -51,6 +57,50 @@ class TestMaxRunDuration:def test_user_created_background_runs_are_uncapped(self, _name: str, origin_product: str | None):assert _context({"mode": "background"}, origin_product=origin_product).max_run_duration() is None+ @parameterized.expand(+ [+ ("profile_cap", "background", Task.OriginProduct.WORKFLOW.value, 120, DEFAULT_CAP_SECONDS, 120),+ (+ "profile_cap_on_user_origin",+ "background",+ Task.OriginProduct.USER_CREATED.value,+ 120,+ DEFAULT_CAP_SECONDS,+ 120,+ ),+ ("profile_without_cap", "background", Task.OriginProduct.WORKFLOW.value, None, DEFAULT_CAP_SECONDS, None),+ (+ "interactive_profile_task",+ "interactive",+ Task.OriginProduct.WORKFLOW.value,+ 120,+ DEFAULT_CAP_SECONDS,+ None,+ ),+ ("setting_disables_profile_cap", "background", Task.OriginProduct.WORKFLOW.value, 120, 0, None),+ ]+ )+ def test_profile_sets_the_cap(+ self,+ _name: str,+ mode: str,+ origin_product: str,+ max_wake_minutes: int | None,+ setting_seconds: int,+ expected_minutes: int | None,+ ):+ context = _context(+ {"mode": mode},+ origin_product=origin_product,+ profile_key="test.profile",+ profile_max_wake_minutes=max_wake_minutes,+ )++ with override_settings(TASKS_MAX_RUN_DURATION_SECONDS=setting_seconds):+ result = context.max_run_duration()++ assert result == (None if expected_minutes is None else timedelta(minutes=expected_minutes))+@override_settings(TASKS_MAX_RUN_DURATION_SECONDS=90)def test_setting_override_applies_to_capped_runs(self):assert resolve_max_run_duration() == timedelta(seconds=90)