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): bucket the title gateway flag per user (#127943)

Charles Vien526022dbd774
diff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.py
index 6f870c1b2ec2..05448383480e 100644
--- a/products/tasks/backend/facade/api.py
+++ b/products/tasks/backend/facade/api.py
@@ -8005,6 +8005,8 @@ def create_task(
if user_id is not None:
validated_data["created_by"] = User.objects.get(id=user_id)
+ created_by = validated_data.get("created_by")
+ title_distinct_id = created_by.distinct_id if created_by else None
if validated_data.get("repository") and not validated_data.get("github_integration"):
default_integration = IntegrationManager.github.oauth.find(team.id)
@@ -8124,7 +8126,7 @@ def create_task(
warm_task.title_manually_set = True
update_fields += ["title", "title_manually_set"]
elif description and not (warm_task.title or "").strip():
- warm_task.title = generate_task_title(naming_source or description)
+ warm_task.title = generate_task_title(naming_source or description, distinct_id=title_distinct_id)
warm_task.title_manually_set = False
update_fields += ["title", "title_manually_set"]
if description and not (warm_task.description or "").strip():
@@ -8258,7 +8260,9 @@ def create_task(
title = (validated_data.get("title") or "").strip()
if not title and (naming_source or validated_data.get("description")):
- validated_data["title"] = generate_task_title(naming_source or validated_data["description"])
+ validated_data["title"] = generate_task_title(
+ naming_source or validated_data["description"], distinct_id=title_distinct_id
+ )
validated_data.setdefault("title_manually_set", False)
elif title:
validated_data.setdefault("title_manually_set", True)
diff --git a/products/tasks/backend/logic/services/test_title_generator.py b/products/tasks/backend/logic/services/test_title_generator.py
index f02b06531050..3c2b3b81e4e9 100644
--- a/products/tasks/backend/logic/services/test_title_generator.py
+++ b/products/tasks/backend/logic/services/test_title_generator.py
@@ -131,6 +131,28 @@ class TestTitleGeneratorAIGateway:
assert mock_build.called is expect_gateway
assert mock_client_cls.called is not expect_gateway
+ @pytest.mark.parametrize(
+ ("distinct_id", "expected_flag_distinct_id"),
+ [
+ ("user-distinct-id", "user-distinct-id"),
+ ("", "random-id"),
+ (None, "random-id"),
+ ],
+ )
+ @patch("products.tasks.backend.logic.services.title_generator.uuid4", return_value="random-id")
+ @patch("products.tasks.backend.logic.services.title_generator.feature_enabled_or_false", return_value=False)
+ @patch("posthog.llm.gateway_client.resolve_ai_gateway_config", return_value=MagicMock())
+ @patch("products.ai_observability.backend.llm.client.Client")
+ def test_flag_buckets_by_caller_distinct_id(
+ self, mock_client_cls, _mock_resolve, mock_flag, _mock_uuid4, distinct_id, expected_flag_distinct_id
+ ):
+ mock_client_cls.return_value.stream.return_value = [StreamChunk(type="text", data={"text": "Direct title"})]
+
+ generate_task_title("Users are experiencing login issues", distinct_id=distinct_id)
+
+ assert mock_flag.call_args.args == ("tasks-title-ai-gateway", expected_flag_distinct_id)
+ mock_client_cls.assert_called_once_with(distinct_id="task-title-generator")
+
@patch("posthog.llm.gateway_client.build_ai_gateway_anthropic_client")
@patch("products.tasks.backend.logic.services.title_generator.feature_enabled_or_false", return_value=True)
@patch("posthog.llm.gateway_client.resolve_ai_gateway_config", return_value=MagicMock())
diff --git a/products/tasks/backend/logic/services/title_generator.py b/products/tasks/backend/logic/services/title_generator.py
index 4b4900e674e7..f23b71904a24 100644
--- a/products/tasks/backend/logic/services/title_generator.py
+++ b/products/tasks/backend/logic/services/title_generator.py
@@ -1,5 +1,6 @@
import logging
from typing import Any
+from uuid import uuid4
from posthog.ph_client import feature_enabled_or_false
@@ -15,7 +16,7 @@ TITLE_GATEWAY_TIMEOUT_SECONDS = 30.0
TITLE_GATEWAY_MAX_RETRIES = 1
-def generate_task_title(description: str) -> str:
+def generate_task_title(description: str, *, distinct_id: str | None = None) -> str:
"""
Generate a concise task title from a description using Claude Haiku.
@@ -70,7 +71,7 @@ Examples:
Output the title now:"""
messages: list[dict[str, Any]] = [{"role": "user", "content": user_prompt}]
- if _title_via_ai_gateway():
+ if _title_via_ai_gateway(distinct_id):
response_text = _complete_via_ai_gateway(system_prompt, user_prompt)
else:
client = Client(distinct_id=TITLE_DISTINCT_ID)
@@ -106,14 +107,14 @@ Output the title now:"""
return _fallback_title(description)
-def _title_via_ai_gateway() -> bool:
+def _title_via_ai_gateway(distinct_id: str | None) -> bool:
from posthog.llm.gateway_client import resolve_ai_gateway_config # noqa: PLC0415
if resolve_ai_gateway_config() is None:
return False
return feature_enabled_or_false(
TASK_TITLE_AI_GATEWAY_FEATURE_FLAG,
- TITLE_DISTINCT_ID,
+ distinct_id or str(uuid4()),
only_evaluate_locally=False,
send_feature_flag_events=False,
)
diff --git a/products/tasks/backend/tests/test_facade.py b/products/tasks/backend/tests/test_facade.py
index 38058dacffbd..05cc5af09dd1 100644
--- a/products/tasks/backend/tests/test_facade.py
+++ b/products/tasks/backend/tests/test_facade.py
@@ -1894,7 +1894,7 @@ class TestFacadeReadsAndMappers(TestCase):
# When a client pastes text (stored as an attachment), it sends the pasted content as
# naming_source so the title reads well, while description stays the bare prompt the
# agent — and the reload transcript dedup — expect.
- mock_title.side_effect = lambda text: f"title:{text}"
+ mock_title.side_effect = lambda text, **_: f"title:{text}"
dto = facade.create_task(
team_id=self.team.id,
user_id=self.user.id,
@@ -1908,11 +1908,11 @@ class TestFacadeReadsAndMappers(TestCase):
self.assertEqual(task.description, "Attached files: pasted-text.txt")
self.assertEqual(task.title, "title:Deploy blocks on a stale lockfile")
self.assertFalse(task.title_manually_set)
- mock_title.assert_called_once_with("Deploy blocks on a stale lockfile")
+ mock_title.assert_called_once_with("Deploy blocks on a stale lockfile", distinct_id=self.user.distinct_id)
@patch("products.tasks.backend.logic.services.title_generator.generate_task_title")
def test_create_task_falls_back_to_description_without_naming_source(self, mock_title):
- mock_title.side_effect = lambda text: f"title:{text}"
+ mock_title.side_effect = lambda text, **_: f"title:{text}"
dto = facade.create_task(
team_id=self.team.id,
user_id=self.user.id,
@@ -1923,7 +1923,7 @@ class TestFacadeReadsAndMappers(TestCase):
)
task = Task.objects.get(id=dto.id)
self.assertEqual(task.title, "title:Fix the login redirect")
- mock_title.assert_called_once_with("Fix the login redirect")
+ mock_title.assert_called_once_with("Fix the login redirect", distinct_id=self.user.distinct_id)
@patch("products.tasks.backend.temporal.client.execute_task_processing_workflow")
def test_create_and_run_persists_dispatch_params_for_reconcile(self, _mock_workflow):