: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): let task creation take a source (#123800)
Charles Vien603fbbcea62d
diff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.pyindex 783db8314fbb..d2d05aba5e55 100644--- a/products/tasks/backend/facade/api.py+++ b/products/tasks/backend/facade/api.py@@ -104,7 +104,7 @@ from products.tasks.backend.constants import ()from products.tasks.backend.error_telemetry import truncate_error_messagefrom products.tasks.backend.facade.gateway import mint_private_gateway_token, revoke_private_gateway_token-from products.tasks.backend.facade.profiles import TaskProfile, TaskRunOptions, resolve_task_origin+from products.tasks.backend.facade.profiles import TaskProfile, TaskRunOptions, TaskSource, resolve_task_originfrom products.tasks.backend.feature_flags import (get_model_access_error,own_subscription_cloud_enabled as own_subscription_cloud_enabled,@@ -200,6 +200,7 @@ from products.tasks.backend.repository_config_analytics import (capture_space_context_changed,repositories_differ,)+from products.tasks.backend.task_sources import apply_task_sourcefrom products.tasks.backend.visibility import (PRODUCT_PRIVATE_ORIGIN_PRODUCTS,TEAM_READABLE_ORIGIN_PRODUCTS,@@ -2030,6 +2031,7 @@ def create_and_run_task(internal: bool | None = None,sandbox_environment_id: str | None = None,channel_id: str | UUID | None = None,+ source: TaskSource | None = None,**extra,) -> contracts.CreatedTaskDTO:"""Create a task and (optionally) kick off its processing workflow.@@ -2053,6 +2055,10 @@ def create_and_run_task(origin = resolve_task_origin(profile=profile, origin_product=origin_product, internal=internal)origin_product = cast("Task.OriginProduct", origin.origin_product)internal = origin.internal+ columns = apply_task_source(source, signal_report_id=signal_report_id, hog_flow_id=extra.get("hog_flow_id"))+ signal_report_id = columns.signal_report_id+ if columns.hog_flow_id is not None:+ extra["hog_flow_id"] = columns.hog_flow_id# create_pr=False sessions (research, repo selection, custom agents) can never open the# billable PR, so the quota gate must not block them.if origin_product == Task.OriginProduct.SIGNAL_REPORT and create_pr:@@ -2266,6 +2272,7 @@ def create_task_without_run(mcp_builtin_agent_key: MCPBuiltInAgentKey | None = None,channel: Channel | None = None,options: TaskRunOptions | None = None,+ source: TaskSource | None = None,) -> UUID:"""Create a Task row with no initial run, returning its id.@@ -2290,6 +2297,7 @@ def create_task_without_run(channel=channel,mcp_builtin_agent_key=mcp_builtin_agent_key,options=options,+ source=source,)return task.iddiff --git a/products/tasks/backend/facade/profiles.py b/products/tasks/backend/facade/profiles.pyindex 4453f85315f6..2d0eed1c67fd 100644--- a/products/tasks/backend/facade/profiles.py+++ b/products/tasks/backend/facade/profiles.py@@ -56,6 +56,17 @@ class GithubAccess(StrEnum):WRITE = "write"+class SourceKind(StrEnum):+ SIGNAL_REPORT = "signal_report"+ HOG_FLOW = "hog_flow"+++@frozen+class TaskSource:+ kind: SourceKind+ id: str++@frozenclass WarmPolicy:"""Lets a profile start a sandbox before the first message arrives, so the first reply is fast."""@@ -91,9 +102,9 @@ class TaskProfile:# The TaskRunOptions fields a public API caller may set. The server refuses any other field from# a public caller.public_options: frozenset[str]- # The kind of row that started the task, such as "signal_report". The server checks that the- # row exists in the caller's team. None means the task has no source row.- source_kind: str | None+ # The kind of row that starts every task on this profile, such as SIGNAL_REPORT. Task creation+ # refuses a source of any other kind. None means the task takes no source.+ source_kind: SourceKind | None# True shows the task in task lists, personal channel filing, auto-archive, the MCP task filter# and the Desktop staff toggle. Automated work such as loops and scouts is unlisted.@@ -253,6 +264,10 @@ class TaskRunOptionsError(ValueError):pass+class TaskSourceError(ValueError):+ pass++# Tasks stored without a profile and SQL filters only know a task's origin_product, so every profile# that shares an origin must agree on these fields._ORIGIN_SCOPED_FIELDS = (@@ -312,3 +327,11 @@ def resolve_task_origin(if internal is not None and internal == profile.listed:raise TaskProfileError(f"internal={internal} does not match task profile {profile.key!r}")return TaskOrigin(origin_product=profile.origin_product, internal=not profile.listed, profile_key=profile.key)+++def check_task_source(*, profile: TaskProfile | None, source: TaskSource | None) -> None:+ if profile is None:+ return+ kind = source.kind if source is not None else None+ if kind != profile.source_kind:+ raise TaskSourceError(f"Task profile {profile.key!r} takes source {profile.source_kind}, got {kind}")diff --git a/products/tasks/backend/models.py b/products/tasks/backend/models.pyindex e89c086b2e1b..849a5da98ca6 100644--- a/products/tasks/backend/models.py+++ b/products/tasks/backend/models.py@@ -52,7 +52,13 @@ from products.tasks.backend.dev_stack_image_models import (DevStackImageTarget as DevStackImageTarget,)from products.tasks.backend.error_telemetry import truncate_error_message-from products.tasks.backend.facade.profiles import TaskProfile, TaskRunOptions, resolve_task_origin+from products.tasks.backend.facade.profiles import (+ TaskProfile,+ TaskRunOptions,+ TaskSource,+ check_task_source,+ resolve_task_origin,+)from products.tasks.backend.feature_flags import (is_task_run_stream_presence_gated,is_task_run_stream_thin_tail,@@ -70,6 +76,7 @@ from products.tasks.backend.pr_urls import read_pr_urlsfrom products.tasks.backend.redis import dedicated_stream_configured, run_uses_dedicated_streamfrom products.tasks.backend.run_options import RunSettings, apply_run_options, run_options_recordfrom products.tasks.backend.storage import append_jsonl_object+from products.tasks.backend.task_sources import apply_task_source, source_from_columnslogger = structlog.get_logger(__name__)@@ -1563,6 +1570,7 @@ class Task(Taggable, DeletedMetaFields, models.Model):mcp_credential_owner_id: int | None = None,mcp_gateway_server_ids: list[str] | None = None,options: TaskRunOptions | None = None,+ source: TaskSource | None = None,) -> "Task":"""Create the Task row without an initial run or workflow.@@ -1571,6 +1579,12 @@ class Task(Taggable, DeletedMetaFields, models.Model):own state. The run `extra_state` assembled by `_build_task` is discarded here."""origin = resolve_task_origin(profile=profile, origin_product=origin_product, internal=internal)+ columns = apply_task_source(source, signal_report_id=signal_report_id, hog_flow_id=None)+ if profile is not None:+ check_task_source(+ profile=profile,+ source=source_from_columns(signal_report_id=columns.signal_report_id, hog_flow_id=columns.hog_flow_id),+ )runtime = Task.Runtime.ACP.valueif options is not None:settings = apply_run_options(@@ -1600,7 +1614,8 @@ class Task(Taggable, DeletedMetaFields, models.Model):slack_thread_context=slack_thread_context,slack_thread_url=slack_thread_url,branch=branch,- signal_report_id=signal_report_id,+ signal_report_id=columns.signal_report_id,+ hog_flow_id=columns.hog_flow_id,sandbox_environment_id=sandbox_environment_id,internal=origin.internal,output_schema=output_schema,@@ -1677,6 +1692,7 @@ class Task(Taggable, DeletedMetaFields, models.Model):mcp_tool_allowlist: list[str] | None = None,before_task_dispatch: Callable[[uuid.UUID], dict[str, JsonValue] | None] | None = None,options: TaskRunOptions | None = None,+ source: TaskSource | None = None,) -> "Task":startup_requested_at = django_timezone.now()from products.tasks.backend.logic.services.workflow_dispatch import (@@ -1697,6 +1713,13 @@ class Task(Taggable, DeletedMetaFields, models.Model):origin = resolve_task_origin(profile=profile, origin_product=origin_product, internal=internal)origin_product = cast("Task.OriginProduct", origin.origin_product)internal = origin.internal+ columns = apply_task_source(source, signal_report_id=signal_report_id, hog_flow_id=hog_flow_id)+ signal_report_id = columns.signal_report_id+ hog_flow_id = columns.hog_flow_id+ if profile is not None:+ check_task_source(+ profile=profile, source=source_from_columns(signal_report_id=signal_report_id, hog_flow_id=hog_flow_id)+ )if options is not None:settings = apply_run_options(options,diff --git a/products/tasks/backend/task_sources.py b/products/tasks/backend/task_sources.pynew file mode 100644index 000000000000..79b538cc5954--- /dev/null+++ b/products/tasks/backend/task_sources.py@@ -0,0 +1,33 @@+from uuid import UUID++from posthog.dataclasses import frozen++from products.tasks.backend.facade.profiles import SourceKind, TaskSource, TaskSourceError+++@frozen+class SourceColumns:+ signal_report_id: str | None+ hog_flow_id: UUID | None+++def apply_task_source(+ source: TaskSource | None, *, signal_report_id: str | None, hog_flow_id: UUID | None+) -> SourceColumns:+ if source is None:+ return SourceColumns(signal_report_id=signal_report_id, hog_flow_id=hog_flow_id)+ if signal_report_id is not None or hog_flow_id is not None:+ raise TaskSourceError("Pass the source as source or as signal_report_id and hog_flow_id, not both")+ if source.kind == SourceKind.SIGNAL_REPORT:+ return SourceColumns(signal_report_id=source.id, hog_flow_id=None)+ return SourceColumns(signal_report_id=None, hog_flow_id=UUID(source.id))+++def source_from_columns(*, signal_report_id: str | None, hog_flow_id: UUID | None) -> TaskSource | None:+ if signal_report_id and hog_flow_id:+ raise TaskSourceError("A task has one source. Pass signal_report_id or hog_flow_id, not both")+ if signal_report_id:+ return TaskSource(kind=SourceKind.SIGNAL_REPORT, id=str(signal_report_id))+ if hog_flow_id:+ return TaskSource(kind=SourceKind.HOG_FLOW, id=str(hog_flow_id))+ return Nonediff --git a/products/tasks/backend/tests/test_facade.py b/products/tasks/backend/tests/test_facade.pyindex e030f1218fca..b47ddd47b2be 100644--- a/products/tasks/backend/tests/test_facade.py+++ b/products/tasks/backend/tests/test_facade.py@@ -28,7 +28,14 @@ from products.tasks.backend.facade import (contracts,warm as warm_facade,)-from products.tasks.backend.facade.profiles import Repository, TaskProfileError, TaskRunOptions, get_task_profile+from products.tasks.backend.facade.profiles import (+ Repository,+ SourceKind,+ TaskProfileError,+ TaskRunOptions,+ TaskSource,+ get_task_profile,+)from products.tasks.backend.models import (TASK_OWNERSHIP_VERSION_STATE_KEY,Channel,@@ -42,6 +49,8 @@ from products.tasks.backend.models import (from products.tasks.backend.prompts import WIZARD_HEAD_BRANCH_PLACEHOLDER, build_wizard_pr_agent_promptfrom products.tasks.backend.run_options import run_options_record+REPORT_ID = "0f7a6c1e-3b8d-4f2a-9c5e-1d2b3a4c5e6f"+FACADE_MODULES = ["products.tasks.backend.facade.api","products.tasks.backend.facade.contracts",@@ -1814,6 +1823,23 @@ class TestFacadeReadsAndMappers(TestCase):task = Task.objects.get(id=created if isinstance(created, UUID) else created.task_id)self.assertEqual((task.repositories, task.options), (["posthog/posthog"], run_options_record(options)))+ @parameterized.expand([("create_and_run_task",), ("create_task_without_run",)])+ @patch("products.tasks.backend.temporal.client.execute_task_processing_workflow")+ def test_facade_stores_the_task_source(self, entry: str, _mock_workflow) -> None:+ flow_id = uuid4()++ created = getattr(facade, entry)(+ team=self.team,+ title="t",+ description="d",+ user_id=self.user.id,+ origin_product=facade.TaskOriginProduct.USER_CREATED,+ source=TaskSource(kind=SourceKind.HOG_FLOW, id=str(flow_id)),+ )++ task = Task.objects.get(id=created if isinstance(created, UUID) else created.task_id)+ self.assertEqual(task.hog_flow_id, flow_id)+@parameterized.expand([("create_and_run_task_neither", "create_and_run_task", {}),@@ -2335,7 +2361,16 @@ class TestSelfDrivingQuotaFacadeGates(TestCase):return_value=SelfDrivingQuotaGate(limited=True, enforced=True),)- def test_create_and_run_task_blocked_for_self_driving_origin_when_enforced(self):+ @parameterized.expand(+ [+ ("no_report", {}, None),+ ("report_id", {"signal_report_id": REPORT_ID}, REPORT_ID),+ ("source", {"source": TaskSource(kind=SourceKind.SIGNAL_REPORT, id=REPORT_ID)}, REPORT_ID),+ ]+ )+ def test_create_and_run_task_blocked_for_self_driving_origin_when_enforced(+ self, _name: str, report_kwargs: dict, expected_report_id: str | None+ ) -> None:# The implementation task is the step that leads to the billable PR; over-quota teams# must not get one through the facade regardless of caller.from posthog.exceptions import QuotaLimitExceeded@@ -2352,12 +2387,14 @@ class TestSelfDrivingQuotaFacadeGates(TestCase):origin_product=facade.TaskOriginProduct.SIGNAL_REPORT,user_id=self.user.id,repository="posthog/posthog",+ **report_kwargs,)self.assertFalse(Task.objects.filter(team=self.team).exists())# The facade gate keeps its own stage: its main caller is the auto-start pipeline, whose# over-quota hits must not pollute the manual-path (`manual_create`) telemetry bucket.self.assertEqual(capture_mock.call_args.kwargs["stage"], "task_create")self.assertTrue(capture_mock.call_args.kwargs["enforced"])+ self.assertEqual(capture_mock.call_args.kwargs["report_id"], expected_report_id)@patch("products.tasks.backend.temporal.client.execute_task_processing_workflow")def test_create_and_run_task_allows_non_pr_sessions_when_enforced(self, _mock_workflow):diff --git a/products/tasks/backend/tests/test_models.py b/products/tasks/backend/tests/test_models.pyindex 3dd05499cd51..e96bfbc70c35 100644--- a/products/tasks/backend/tests/test_models.py+++ b/products/tasks/backend/tests/test_models.py@@ -1,6 +1,7 @@import jsonimport uuidfrom contextlib import nullcontext+from dataclasses import replacefrom datetime import UTC, datetime, timedeltafrom functools import partialfrom typing import Any, ClassVar@@ -26,9 +27,12 @@ from products.tasks.backend.facade.profiles import (GithubAccess,PrAuthorship,Repository,+ SourceKind,TaskProfileError,TaskRunOptions,TaskRunOptionsError,+ TaskSource,+ TaskSourceError,get_task_profile,)from products.tasks.backend.models import (@@ -47,6 +51,9 @@ from products.tasks.backend.models import (from products.tasks.backend.run_options import run_options_recordBUSINESS_KNOWLEDGE = get_task_profile("business_knowledge")+SOURCE_ID = uuid.UUID("5ab01f4d-5b1e-4990-9802-4f8792a76759")+FLOW_SOURCE = TaskSource(kind=SourceKind.HOG_FLOW, id=str(SOURCE_ID))+REPORT_PROFILE = replace(BUSINESS_KNOWLEDGE, key="test.report", source_kind=SourceKind.SIGNAL_REPORT)# Far enough back that any bump is unambiguously forward, and stable so a test can assert the# clock did *not* move.@@ -369,6 +376,49 @@ class TestTask(TestCase):self.assertFalse(Task.objects.filter(team=self.team).exists())+ @parameterized.expand([(SourceKind.SIGNAL_REPORT,), (SourceKind.HOG_FLOW,)])+ @patch("products.tasks.backend.temporal.client.execute_task_processing_workflow")+ def test_task_creation_stores_its_source(self, kind: SourceKind, _mock_execute_workflow) -> None:+ from products.signals.backend.models import SignalReport++ user = User.objects.create(email=f"{kind}@test.com")+ source_id = SignalReport.objects.create(team=self.team).id if kind == SourceKind.SIGNAL_REPORT else SOURCE_ID++ task = Task.create_and_run(+ team=self.team,+ title="t",+ description="d",+ user_id=user.id,+ origin_product=Task.OriginProduct.USER_CREATED,+ source=TaskSource(kind=kind, id=str(source_id)),+ )++ task.refresh_from_db()+ expected = (source_id, None) if kind == SourceKind.SIGNAL_REPORT else (None, source_id)+ self.assertEqual((task.signal_report_id, task.hog_flow_id), expected)++ @parameterized.expand(+ [+ (+ "source_and_column",+ {"origin_product": Task.OriginProduct.USER_CREATED, "hog_flow_id": SOURCE_ID, "source": FLOW_SOURCE},+ ),+ ("profile_takes_no_source", {"profile": BUSINESS_KNOWLEDGE, "source": FLOW_SOURCE}),+ ("profile_takes_no_legacy_source", {"profile": BUSINESS_KNOWLEDGE, "hog_flow_id": SOURCE_ID}),+ (+ "profile_given_both_columns",+ {"profile": REPORT_PROFILE, "signal_report_id": str(SOURCE_ID), "hog_flow_id": SOURCE_ID},+ ),+ ]+ )+ def test_task_creation_refuses_a_source_it_cannot_take(self, name: str, kwargs: dict) -> None:+ user = User.objects.create(email=f"{name}@test.com")++ with self.assertRaises(TaskSourceError):+ Task.create_and_run(team=self.team, title="t", description="d", user_id=user.id, **kwargs)++ self.assertFalse(Task.objects.filter(team=self.team).exists())+@parameterized.expand([("committed", False), ("rolled_back", True)])def test_create_and_run_captures_only_committed_tasks(self, _name: str, rolled_back: bool) -> None:user = User.objects.create(email="[email protected]")diff --git a/products/tasks/backend/tests/test_profiles.py b/products/tasks/backend/tests/test_profiles.pyindex 41699ab82863..dee9ee8d9b8e 100644--- a/products/tasks/backend/tests/test_profiles.py+++ b/products/tasks/backend/tests/test_profiles.py@@ -1,3 +1,4 @@+from contextlib import nullcontextfrom dataclasses import replacefrom django.test import SimpleTestCase@@ -6,14 +7,21 @@ from parameterized import parameterizedfrom products.tasks.backend.facade.profiles import (Read,+ SourceKind,TaskProfile,TaskProfileError,+ TaskSource,+ TaskSourceError,+ check_task_source,get_task_profile,register_task_profile,resolve_task_origin,)BUSINESS_KNOWLEDGE = get_task_profile("business_knowledge")+REPORT_PROFILE = replace(BUSINESS_KNOWLEDGE, key="test.report", source_kind=SourceKind.SIGNAL_REPORT)+REPORT_SOURCE = TaskSource(kind=SourceKind.SIGNAL_REPORT, id="report-1")+FLOW_SOURCE = TaskSource(kind=SourceKind.HOG_FLOW, id="5ab01f4d-5b1e-4990-9802-4f8792a76759")class TestTaskProfileRegistry(SimpleTestCase):@@ -76,3 +84,20 @@ class TestTaskProfileRegistry(SimpleTestCase):) -> None:with self.assertRaises(TaskProfileError):resolve_task_origin(profile=profile, origin_product=origin, internal=internal)++ @parameterized.expand(+ [+ ("no_profile_with_source", None, FLOW_SOURCE, False),+ ("no_profile_without_source", None, None, False),+ ("profile_without_kind_or_source", BUSINESS_KNOWLEDGE, None, False),+ ("profile_with_matching_source", REPORT_PROFILE, REPORT_SOURCE, False),+ ("profile_without_kind_given_source", BUSINESS_KNOWLEDGE, REPORT_SOURCE, True),+ ("profile_with_kind_missing_source", REPORT_PROFILE, None, True),+ ("profile_with_kind_other_source", REPORT_PROFILE, FLOW_SOURCE, True),+ ]+ )+ def test_check_task_source_matches_the_profile(+ self, _name: str, profile: TaskProfile | None, source: TaskSource | None, refused: bool+ ) -> None:+ with self.assertRaises(TaskSourceError) if refused else nullcontext():+ check_task_source(profile=profile, source=source)