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.

fix(tasks): exempt user-created runs from duration cap (#81281)

Alessandro Pogliaghi28b8083654e7
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 f73f9e62f0ab..9513a4b40674 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
@@ -209,22 +209,23 @@ class TaskProcessingContext:
def inactivity_timeout(self) -> timedelta:
"""Idle time before the workflow times the run out; longer for user-driven runs."""
- is_user_origin = not self.origin_product or self.origin_product in (
+ return resolve_inactivity_timeout(is_user_origin=self._is_user_origin(), state=self.state)
+
+ def _is_user_origin(self) -> bool:
+ return not self.origin_product or self.origin_product in (
Task.OriginProduct.USER_CREATED.value,
Task.OriginProduct.IMAGE_BUILDER.value,
)
- return resolve_inactivity_timeout(is_user_origin=is_user_origin, state=self.state)
def max_run_duration(self) -> timedelta | None:
"""Hard wall-clock cap on total run time, or None when the run is exempt.
Unlike the inactivity timeout, this is not reset by heartbeats, so it stops a
- wedged-but-heartbeating agent that would otherwise run forever. Interactive
- sessions can legitimately stay open for hours under a human, so they are
- uncapped; autonomous runs get the cap as a safety net, unless the setting
- disables it deployment-wide.
+ wedged-but-heartbeating agent that would otherwise run forever. User-driven
+ sessions can legitimately run for hours, so they are uncapped. Autonomous runs
+ get the cap as a safety net unless the setting disables it deployment-wide.
"""
- if self.mode == "interactive":
+ if self.mode == "interactive" or self._is_user_origin():
return None
return resolve_max_run_duration()
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.py
index 11dd3b357321..c83a1a1c3bbe 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
@@ -2,13 +2,18 @@ from datetime import timedelta
from django.test import override_settings
+from parameterized import parameterized
+
+from products.tasks.backend.models import Task
from products.tasks.backend.temporal.constants import MAX_INACTIVITY_TIMEOUT_SECONDS, resolve_max_run_duration
from products.tasks.backend.temporal.process_task.activities.get_task_processing_context import TaskProcessingContext
DEFAULT_CAP_SECONDS = 3 * 60 * 60
-def _context(state: dict | None) -> TaskProcessingContext:
+def _context(
+ state: dict | None, *, origin_product: str | None = Task.OriginProduct.AUTOMATION.value
+) -> TaskProcessingContext:
return TaskProcessingContext(
task_id="task-id",
run_id="run-id",
@@ -19,6 +24,7 @@ def _context(state: dict | None) -> TaskProcessingContext:
repository="posthog/posthog-js",
distinct_id="distinct",
state=state,
+ origin_product=origin_product,
)
@@ -30,11 +36,21 @@ class TestMaxRunDuration:
assert _context({"mode": "interactive"}).max_run_duration() is None
@override_settings(TASKS_MAX_RUN_DURATION_SECONDS=DEFAULT_CAP_SECONDS)
- def test_background_runs_get_the_default_cap(self):
+ def test_autonomous_background_runs_get_the_default_cap(self):
assert _context({"mode": "background"}).max_run_duration() == timedelta(seconds=DEFAULT_CAP_SECONDS)
# Default mode (no explicit mode in state) is background.
assert _context(None).max_run_duration() == timedelta(seconds=DEFAULT_CAP_SECONDS)
+ @parameterized.expand(
+ [
+ ("explicit_user_created", Task.OriginProduct.USER_CREATED.value),
+ ("missing_origin", None),
+ ]
+ )
+ @override_settings(TASKS_MAX_RUN_DURATION_SECONDS=DEFAULT_CAP_SECONDS)
+ 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
+
@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)