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.

chore(tasks): stamp loop_id on all task run analytics events (#73629)

Georges-Antoine Assi1faf502cf653
diff --git a/products/tasks/backend/models.py b/products/tasks/backend/models.py
index c7b1f1e07a63..6bef26ea4d09 100644
--- a/products/tasks/backend/models.py
+++ b/products/tasks/backend/models.py
@@ -419,6 +419,10 @@ class Task(FileSystemSyncMixin, DeletedMetaFields, models.Model):
"environment": task_run.environment,
"is_resume": is_resume,
"has_pending_message": has_pending,
+ # Loop attribution: this event uses Task.capture_event (not TaskRun's),
+ # so carry it from the run state the same way TaskRun.capture_event does.
+ "loop_id": state.get("loop_id"),
+ "loop_trigger_id": state.get("loop_trigger_id"),
},
)
return task_run
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 e7e7f3210de2..6f4482f842dc 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
@@ -138,6 +138,12 @@ class TaskProcessingContext:
value = (self.state or {}).get("loop_id")
return value if isinstance(value, str) else None
+ @property
+ def loop_trigger_id(self) -> str | None:
+ """Set alongside loop_id when a loop trigger fired this run."""
+ value = (self.state or {}).get("loop_trigger_id")
+ return value if isinstance(value, str) else None
+
@property
def runtime_adapter(self) -> str | None:
value = (self.state or {}).get("runtime_adapter")
diff --git a/products/tasks/backend/temporal/process_task/workflow.py b/products/tasks/backend/temporal/process_task/workflow.py
index 99ac5ffbafc2..843879d046f7 100644
--- a/products/tasks/backend/temporal/process_task/workflow.py
+++ b/products/tasks/backend/temporal/process_task/workflow.py
@@ -1012,6 +1012,8 @@ class ProcessTaskWorkflow(PostHogWorkflow):
"task_id": self.context.task_id,
"repository": self.context.repository,
"team_id": self.context.team_id,
+ "loop_id": self.context.loop_id,
+ "loop_trigger_id": self.context.loop_trigger_id,
},
)
@@ -1064,6 +1066,8 @@ class ProcessTaskWorkflow(PostHogWorkflow):
"agent_launch_ms": sandbox_output.launch_ms,
"agent_ready_wait_ms": agent_server_output.ready_wait_ms,
"agent_session_init_ms": agent_server_output.session_init_ms,
+ "loop_id": self.context.loop_id,
+ "loop_trigger_id": self.context.loop_trigger_id,
},
)
return sandbox_id, agent_server_output.sandbox_url, agent_server_output.connect_token
diff --git a/products/tasks/backend/tests/test_models.py b/products/tasks/backend/tests/test_models.py
index 42e2ce8a3a2b..75fb54b58335 100644
--- a/products/tasks/backend/tests/test_models.py
+++ b/products/tasks/backend/tests/test_models.py
@@ -847,6 +847,21 @@ class TestTaskRun(TestCase):
self.assertEqual(len(props["error_message"]), 500)
self.assertTrue(props["error_message"].endswith("Error: the root cause sits at the tail"))
+ @parameterized.expand(
+ [
+ ("loop_run", {"loop_id": "loop-abc", "loop_trigger_id": "trig-xyz"}, "loop-abc", "trig-xyz"),
+ ("non_loop_run", {}, None, None),
+ ]
+ )
+ def test_task_run_created_carries_loop_attribution(self, _name, extra_state, expected_loop_id, expected_trigger_id):
+ with patch("products.tasks.backend.models.posthoganalytics.capture") as mock_capture:
+ self.task.create_run(extra_state=extra_state or None)
+ created = [c for c in mock_capture.call_args_list if c.kwargs.get("event") == "task_run_created"]
+ self.assertEqual(len(created), 1)
+ props = created[0].kwargs["properties"]
+ self.assertEqual(props["loop_id"], expected_loop_id)
+ self.assertEqual(props["loop_trigger_id"], expected_trigger_id)
+
def test_output_jsonfield(self):
run = TaskRun.objects.create(
task=self.task,