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.

feat(tasks): make experiment cleanup tasks team-visible (#72817)

Juraj Majerik2513b72c4684
diff --git a/products/tasks/backend/presentation/serializers.py b/products/tasks/backend/presentation/serializers.py
index 5c97215dec6d..2fc9dea50550 100644
--- a/products/tasks/backend/presentation/serializers.py
+++ b/products/tasks/backend/presentation/serializers.py
@@ -595,6 +595,11 @@ class TaskWriteSerializer(serializers.Serializer):
"""Reject internal-only origins that are set by server-side flows, never by API callers."""
if value == tasks_facade.TaskOriginProduct.IMAGE_BUILDER:
raise serializers.ValidationError("origin_product 'image_builder' is reserved for image-builder sessions")
+ if value == tasks_facade.TaskOriginProduct.EXPERIMENTS:
+ # Experiments tasks are team-readable, so letting API callers pick this origin
+ # would let them expose an arbitrary task to the whole team. The experiments
+ # flow creates its tasks server-side through the facade, never through here.
+ raise serializers.ValidationError("origin_product 'experiments' is reserved for the experiments flow")
return value
def validate_repository(self, value):
diff --git a/products/tasks/backend/tests/test_api.py b/products/tasks/backend/tests/test_api.py
index 63a59c9fc8cf..6e45ceb6a5ee 100644
--- a/products/tasks/backend/tests/test_api.py
+++ b/products/tasks/backend/tests/test_api.py
@@ -376,6 +376,44 @@ class TestTaskCreatorScoping(BaseTaskAPITest):
ids = {r["id"] for r in response.json()["results"]}
self.assertEqual(ids, {str(run.id)})
+ def test_retrieve_experiments_task_owned_by_another_user_is_visible(self):
+ # Flag-cleanup tasks are surfaced on the (team-visible) experiment, so any
+ # team member must be able to open them, not just whoever ended the experiment.
+ other_user = self.create_organization_user("experiment-ender")
+ task = Task.objects.create(
+ team=self.team,
+ created_by=other_user,
+ title="Clean up feature flag my-experiment-flag",
+ description="Opened on experiment end",
+ origin_product=Task.OriginProduct.EXPERIMENTS,
+ )
+
+ response = self.client.get(f"/api/projects/@current/tasks/{task.id}/")
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ self.assertEqual(response.json()["id"], str(task.id))
+
+ def test_update_experiments_task_owned_by_another_user_returns_404(self):
+ # Experiments tasks are team-readable but stay creator-driven: runs execute
+ # with the creator's credentials, so teammates must not be able to edit,
+ # run, or command them.
+ other_user = self.create_organization_user("experiment-ender")
+ task = Task.objects.create(
+ team=self.team,
+ created_by=other_user,
+ title="Clean up feature flag my-experiment-flag",
+ description="Opened on experiment end",
+ origin_product=Task.OriginProduct.EXPERIMENTS,
+ )
+
+ response = self.client.patch(
+ f"/api/projects/@current/tasks/{task.id}/",
+ {"title": "Hijacked"},
+ format="json",
+ )
+ self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
+ task.refresh_from_db()
+ self.assertEqual(task.title, "Clean up feature flag my-experiment-flag")
+
def test_retrieve_other_user_non_signal_internal_task_returns_404(self):
# Non-signal internal tasks created by another user remain private.
other_user = self.create_organization_user("victim")
@@ -1225,13 +1263,19 @@ class TestTaskAPI(BaseTaskAPITest):
task = Task.objects.get(id=data["id"])
self.assertEqual(task.origin_product, Task.OriginProduct.HOGDESK)
- def test_create_task_rejects_internal_image_builder_origin(self):
+ @parameterized.expand(
+ [
+ ("image_builder",),
+ ("experiments",),
+ ]
+ )
+ def test_create_task_rejects_internal_origin(self, origin: str):
response = self.client.post(
"/api/projects/@current/tasks/",
{
"title": "New Task",
"description": "New Description",
- "origin_product": "image_builder",
+ "origin_product": origin,
"repository": "posthog/posthog",
},
format="json",
diff --git a/products/tasks/backend/visibility.py b/products/tasks/backend/visibility.py
index 35cbbcd97895..ee14dce5cce9 100644
--- a/products/tasks/backend/visibility.py
+++ b/products/tasks/backend/visibility.py
@@ -26,6 +26,18 @@ TEAM_VISIBLE_ORIGIN_PRODUCTS = [
Task.OriginProduct.HOGDESK,
]
+# Origin products whose tasks are team-readable but stay creator-driven, like
+# public-channel tasks: teammates can open the task and read what the agent did or
+# asked, but runs execute with the creator's credentials (oauth.py mints tokens from
+# `task.created_by`), so edits, runs, and agent commands stay with the creator.
+# - EXPERIMENTS: flag-cleanup tasks opened when an experiment ends. The experiment
+# (and the task's status on it) is team-visible, so any team member should be able
+# to open the task, not just whoever clicked "End experiment".
+TEAM_READABLE_ORIGIN_PRODUCTS = [
+ *TEAM_VISIBLE_ORIGIN_PRODUCTS,
+ Task.OriginProduct.EXPERIMENTS,
+]
+
def task_control_q(user_id: int | None) -> Q:
"""Filter for tasks the given user may mutate or drive (edits, runs, agent commands).
@@ -49,12 +61,16 @@ def task_control_q(user_id: int | None) -> Q:
def task_visibility_q(user_id: int | None) -> Q:
"""Filter for tasks visible (readable) to the given user.
- Everything controllable per ``task_control_q``, plus tasks in a public
- channel: channel feeds are multiplayer, so every team member sees every
- task filed there. Personal-channel ("#me") tasks stay creator-only via
- the control rules.
+ Everything controllable per ``task_control_q``, plus team-readable origins
+ and tasks in a public channel: channel feeds are multiplayer, so every team
+ member sees every task filed there. Personal-channel ("#me") tasks stay
+ creator-only via the control rules.
"""
- return task_control_q(user_id) | Q(channel__channel_type=Channel.ChannelType.PUBLIC, channel__deleted=False)
+ return (
+ task_control_q(user_id)
+ | Q(origin_product__in=TEAM_READABLE_ORIGIN_PRODUCTS)
+ | Q(channel__channel_type=Channel.ChannelType.PUBLIC, channel__deleted=False)
+ )
def task_run_visibility_q(user_id: int | None) -> Q:
@@ -62,6 +78,6 @@ def task_run_visibility_q(user_id: int | None) -> Q:
return (
Q(task__created_by_id=user_id)
| Q(task__created_by__isnull=True)
- | Q(task__origin_product__in=TEAM_VISIBLE_ORIGIN_PRODUCTS)
+ | Q(task__origin_product__in=TEAM_READABLE_ORIGIN_PRODUCTS)
| Q(task__channel__channel_type=Channel.ChannelType.PUBLIC, task__channel__deleted=False)
)