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): stop active follow-ups on cancellation (#72497)

Alessandro Pogliaghi678a7663cf37
diff --git a/products/tasks/backend/temporal/process_task/tests/test_workflow.py b/products/tasks/backend/temporal/process_task/tests/test_workflow.py
index 4f55b04688ea..77715f4bdf05 100644
--- a/products/tasks/backend/temporal/process_task/tests/test_workflow.py
+++ b/products/tasks/backend/temporal/process_task/tests/test_workflow.py
@@ -451,6 +451,38 @@ class TestProcessTaskFollowupDispatch:
assert deliveries == ["keep working"]
assert workflow._pending_followups == []
+ async def test_cancellation_stops_active_followup_and_discards_queue(self, monkeypatch):
+ workflow = ProcessTaskWorkflow()
+ workflow._context = _build_context(github_integration_id=123)
+ followup_cancelled = asyncio.Event()
+
+ async def fake_send_followup(
+ *, message, artifact_ids, actor_user_id=None, message_id=None, context=None, steer=False
+ ):
+ try:
+ await asyncio.Event().wait()
+ except asyncio.CancelledError:
+ followup_cancelled.set()
+ raise
+
+ monkeypatch.setattr(workflow, "_send_followup_to_sandbox", fake_send_followup)
+ monkeypatch.setattr(process_task_workflow_module.workflow, "now", Mock(return_value=Mock()))
+ monkeypatch.setattr(process_task_workflow_module.workflow, "deprecate_patch", Mock())
+ monkeypatch.setattr(process_task_workflow_module.workflow, "logger", Mock())
+
+ await workflow.send_followup_message("keep working")
+ assert await workflow._dispatch_next_followup() is True
+ await asyncio.sleep(0)
+ await workflow.send_followup_message("queued work")
+
+ await workflow.complete_task("cancelled", "Stopped by user")
+ await workflow._finish_active_followup()
+
+ assert followup_cancelled.is_set()
+ assert workflow._active_followup_task is None
+ assert workflow._pending_followup is None
+ assert workflow._pending_followups == []
+
async def test_completion_waits_for_declined_steer_fallback(self, monkeypatch):
workflow = ProcessTaskWorkflow()
workflow._context = _build_context(github_integration_id=123)
diff --git a/products/tasks/backend/temporal/process_task/workflow.py b/products/tasks/backend/temporal/process_task/workflow.py
index 471b82fcef09..0c2ad2b91ba7 100644
--- a/products/tasks/backend/temporal/process_task/workflow.py
+++ b/products/tasks/backend/temporal/process_task/workflow.py
@@ -420,6 +420,15 @@ class ProcessTaskWorkflow(PostHogWorkflow):
async def _finish_active_followup(self) -> None:
self._shutting_down = True
+ if self._completion_status == "cancelled":
+ self._pending_followup = None
+ self._pending_followups.clear()
+ if self._active_followup_task is not None:
+ task = self._active_followup_task
+ self._active_followup_task = None
+ await self._cancel_relay(task)
+ return
+
while True:
if self._active_followup_task is not None:
task = self._active_followup_task