: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): end workflow ai task runs with no slack thread (#90304)
Harley Alexander9849740be8cd
diff --git a/nodejs/src/cdp/templates/_destinations/posthog_tasks/posthog-create-task.template.ts b/nodejs/src/cdp/templates/_destinations/posthog_tasks/posthog-create-task.template.tsindex af0f210d712a..badcccaa3fc7 100644--- a/nodejs/src/cdp/templates/_destinations/posthog_tasks/posthog-create-task.template.ts+++ b/nodejs/src/cdp/templates/_destinations/posthog_tasks/posthog-create-task.template.ts@@ -143,6 +143,8 @@ return response.body{// Only meaningful on a Slack-triggered workflow; the builder hides it for other// triggers, and the hog code above no-ops when the trigger event isn't a Slack message.+ // Off, the backend sees no slack_context and ends the run when the agent finishes;+ // on, the run stays open for its idle window so the thread reply can relay.key: 'reply_in_slack_thread',type: 'boolean',label: 'Reply in the Slack thread',@@ -151,7 +153,7 @@ return response.bodydefault: true,templating: false,description:- 'The agent posts its updates as replies in the Slack thread that started this workflow. Replies in that thread are sent to the agent.',+ 'The agent posts its updates as replies in the Slack thread that started this workflow. Replies in that thread are sent to the agent. The run stays open for about 2 minutes after the agent finishes so replies can reach it.',},{// The engine treats a 4xx as a step failure before the code above runs, unless thediff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.pyindex 93581754861f..d55a4b2d70b1 100644--- a/products/tasks/backend/facade/api.py+++ b/products/tasks/backend/facade/api.py@@ -430,7 +430,9 @@ _TASK_RUN_PUBLIC_STATE_KEYS = frozenset(# first message; without them it silently falls back to `task.description`. Withheld from# human readers: a workflow task is team-readable, and its boot prompt embeds the triggering# event wholesale, which for a Slack trigger can be a private channel's message content.-_TASK_RUN_AGENT_STATE_KEYS = frozenset({"initial_prompt_override"})+# `end_run_when_done` gates the sandbox's `finish` tool for workflow runs; a key this+# filter drops never reaches the agent server, so the gate would silently do nothing.+_TASK_RUN_AGENT_STATE_KEYS = frozenset({"end_run_when_done", "initial_prompt_override"})def _public_task_run_state(state: dict | None, *, include_agent_keys: bool = False) -> dict:diff --git a/products/tasks/backend/logic/services/workflow_tasks.py b/products/tasks/backend/logic/services/workflow_tasks.pyindex eacfbe892029..c23f5eeb4203 100644--- a/products/tasks/backend/logic/services/workflow_tasks.py+++ b/products/tasks/backend/logic/services/workflow_tasks.py@@ -136,7 +136,10 @@ def create_workflow_task(the workflow or its team reached the daily created-task cap. A replayed`origin_key` bypasses the gate and every cap.- `event` is rendered into the agent's prompt as data. `slack_context` binds the run to+ `event` is rendered into the agent's prompt as data. The Slack thread binding decides+ the run's lifetime: a thread-bound run stays live until its inactivity timeout, so its+ reply posts and thread replies reach the agent, while a run with no binding ends+ itself the moment the agent is done. `slack_context` binds the run tothe Slack thread that triggered the workflow. The task is created either way: a contextis dropped, rather than failing the create, when it resolves to no Slack integration ofthis team, when the channel is externally shared without an approval, or when another@@ -262,6 +265,15 @@ def create_workflow_task(# user the run does not have. It must keep executing as the workflow owner.interaction_origin = "workflow" if thread_context is not None else None+ if slack_binding is None:+ # Only a thread-bound run must outlive its turn, because the reply relay+ # fires at end of turn. Every other workflow run gets the `finish` tool+ # (the agent server reads this key at boot), so its sandbox is reclaimed+ # promptly. The idle timeout is not a safe fallback for those runs: the+ # PR follow-up loop raises it well past the 2-minute window for+ # repository runs.+ extra_run_state["end_run_when_done"] = True+task = Task.create_and_run(team=team,title=(title or "").strip() or prompt[:255],diff --git a/products/tasks/backend/tests/test_facade.py b/products/tasks/backend/tests/test_facade.pyindex 8a6529e976fd..2c09634c3fb2 100644--- a/products/tasks/backend/tests/test_facade.py+++ b/products/tasks/backend/tests/test_facade.py@@ -268,7 +268,11 @@ class TestFacadeReadsAndMappers(TestCase):task=task,team=self.team,status=TaskRun.Status.QUEUED,- state={"initial_prompt_override": "framed prompt", "sandbox_jwt_kid": "secret"},+ state={+ "initial_prompt_override": "framed prompt",+ "end_run_when_done": True,+ "sandbox_jwt_kid": "secret",+ },)detail = facade.get_task_run_detail(run.id, task.id, self.team.id, include_agent_state=include_agent_state)@@ -276,6 +280,9 @@ class TestFacadeReadsAndMappers(TestCase):assert detail is not Noneexpected = "framed prompt" if include_agent_state else Noneassert detail.state.get("initial_prompt_override") == expected+ # The finish-tool gate reads this key at agent boot; a filter that drops it makes+ # every unbound workflow run idle out instead of ending itself.+ assert detail.state.get("end_run_when_done") == (True if include_agent_state else None)assert "sandbox_jwt_kid" not in detail.statedef test_get_task_run_maps_all_fields(self):diff --git a/products/tasks/backend/tests/test_workflow_tasks_api.py b/products/tasks/backend/tests/test_workflow_tasks_api.pyindex c929a57f6336..36e4118b245e 100644--- a/products/tasks/backend/tests/test_workflow_tasks_api.py+++ b/products/tasks/backend/tests/test_workflow_tasks_api.py@@ -149,6 +149,25 @@ class TestWorkflowTasksAPI(APIBaseTest):# `finish` holds a sandbox for the full background window.assert run.state["inactivity_timeout_seconds"] == 120+ def test_a_run_with_no_thread_binding_ends_itself(self) -> None:+ response = self._post()++ assert response.status_code == status.HTTP_201_CREATED, response.json()+ run = TaskRun.objects.get(id=response.json()["run_id"])+ # No binding means no reply to protect, and the idle timeout is not a safe+ # fallback: the PR follow-up loop raises it far past the 2-minute window.+ assert run.state["end_run_when_done"] is True++ def test_a_thread_bound_run_stays_open_so_its_reply_can_relay(self) -> None:+ integration = self._slack_integration()++ response = self._post({"slack_context": self._slack_context(integration)})++ assert response.status_code == status.HTTP_201_CREATED, response.json()+ run = TaskRun.objects.get(id=response.json()["run_id"])+ assert "end_run_when_done" not in run.state+ assert SlackThreadTaskMapping.objects.filter(task_run=run).exists()+def test_hands_the_agent_its_prompt_when_it_boots(self) -> None:response = self._post()