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): fail a lost agent only on workflow-origin runs (#100943)

Alessandro Pogliaghi8148990cc81a
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 275dbaae7180..58ab6d5002fb 100644
--- a/products/tasks/backend/temporal/process_task/tests/test_workflow.py
+++ b/products/tasks/backend/temporal/process_task/tests/test_workflow.py
@@ -1768,6 +1768,17 @@ class TestProcessTaskWorkflowUnit:
assert workflow._agent_lost_mid_turn() is expected
+ @pytest.mark.parametrize(
+ "origin_product, expected",
+ [("workflow", True), ("slack", False), ("user_created", False), (None, False)],
+ )
+ async def test_only_a_workflow_run_fails_on_a_lost_agent(self, origin_product, expected):
+ workflow = ProcessTaskWorkflow()
+ workflow._context = _build_context(github_integration_id=123, origin_product=origin_product)
+ workflow._end_of_turn_received = False
+
+ assert workflow._agent_lost_exit_is_failure() is expected
+
@pytest.mark.parametrize(
"outcome, expected",
[(None, False), (STEER_DECLINED_OUTCOME, True), (RuntimeError("Sandbox session is dead"), True)],
@@ -2062,8 +2073,13 @@ class TestProcessTaskWorkflowUnit:
[
(None, False, 1, None, "completed"),
(None, False, 1, True, "completed"),
- # The agent was mid-turn when the sandbox vanished, so its work is gone.
- (None, False, 1, False, "failed"),
+ # The agent was mid-turn when the sandbox vanished, so the workflow step waiting on
+ # this run has no work to continue from.
+ ("workflow", False, 1, False, "failed"),
+ # Every other origin keeps completing: the run is the snapshot its reader or its resume
+ # flow picks up, and a red run in the task list would report breakage that never was.
+ (None, False, 1, False, "completed"),
+ ("slack", False, 1, False, "completed"),
("user_created", False, 1, None, "completed"),
# Onboarding runs are one-shot, so a vanished sandbox is a failed setup rather than a
# resumable snapshot.
@@ -2192,16 +2208,28 @@ class TestProcessTaskWorkflowUnit:
"completed",
{"timed_out_inactivity": True},
),
- # The agent was still mid-turn when the timer fired, so it died rather than finished.
+ # The agent was still mid-turn when the timer fired, so the step waiting on this run
+ # has to hear that its work never happened.
(
process_task_workflow_module.TaskEvent.TIMEOUT_REACHED,
- None,
+ "workflow",
False,
1,
False,
"failed",
{"error_message": AGENT_LOST_ERROR_MESSAGE, "timed_out_inactivity": True},
),
+ # An attended run ends the same way every time it answers and then idles out, so the
+ # open turn is its normal exit rather than a lost agent.
+ (
+ process_task_workflow_module.TaskEvent.TIMEOUT_REACHED,
+ "slack",
+ False,
+ 1,
+ False,
+ "completed",
+ {"timed_out_inactivity": True},
+ ),
(
process_task_workflow_module.TaskEvent.MAX_DURATION_REACHED,
None,
@@ -2234,8 +2262,9 @@ class TestProcessTaskWorkflowUnit:
expected_kwargs,
):
# The wall-clock cap is a failure for every origin; the inactivity timeout only fails for
- # onboarding runs that delivered nothing, because other origins resume from the timed-out
- # run and a PR-bearing onboarding run already succeeded.
+ # onboarding runs that delivered nothing and workflow runs whose turn never closed, because
+ # other origins resume from the timed-out run and a PR-bearing onboarding run already
+ # succeeded.
workflow = ProcessTaskWorkflow()
workflow._pr_progress_emitted = pr_progress_emitted
workflow._ci_repetitions = ci_repetitions
diff --git a/products/tasks/backend/temporal/process_task/workflow.py b/products/tasks/backend/temporal/process_task/workflow.py
index bde714c86d95..d5c6e3f55cbf 100644
--- a/products/tasks/backend/temporal/process_task/workflow.py
+++ b/products/tasks/backend/temporal/process_task/workflow.py
@@ -438,6 +438,9 @@ _PATCH_ID_DEV_STACK_PREVIEW = "tasks-dev-stack-preview"
# Django model imports.
_ONBOARDING_ORIGIN_PRODUCT = "onboarding"
+# `Task.OriginProduct.WORKFLOW`, mirrored for the same reason.
+_WORKFLOW_ORIGIN_PRODUCT = "workflow"
+
def _deprecate_ci_follow_up_pr_context_patch() -> None:
workflow.deprecate_patch(_PATCH_ID_CI_FOLLOW_UP_PR_CONTEXT)
@@ -1519,7 +1522,7 @@ class ProcessTaskWorkflow(PostHogWorkflow):
# A run that outlived the hard cap is a failure, not a completion, and the
# state marker carries the reason so error_message stays empty.
await self._update_task_run_status("failed", timeout_marker=TIMED_OUT_WALL_CLOCK_STATE_KEY)
- elif timeout_event is not None and self._agent_lost_mid_turn():
+ elif timeout_event is not None and self._agent_lost_exit_is_failure():
await self._update_task_run_status(
"failed", error_message=AGENT_LOST_ERROR_MESSAGE, timed_out_inactivity=True
)
@@ -2790,10 +2793,23 @@ class ProcessTaskWorkflow(PostHogWorkflow):
"""
return self._end_of_turn_received is False and self._agent_active is not False
+ def _agent_lost_exit_is_failure(self) -> bool:
+ """Whether a lost agent should terminalize the run as FAILED.
+
+ Workflow-origin runs only. A workflow step waits on the run's terminal status, so a turn
+ that never finished has to read as a failure or the step continues on work that never
+ happened. Every other origin ends this way routinely: an attended run answers its user and
+ then idles out with the turn still open, and a background one leaves the stopped run as the
+ snapshot its resume flow picks up. Failing those reports breakage to the person reading the
+ task list when nothing broke.
+ """
+ return self.context.origin_product == _WORKFLOW_ORIGIN_PRODUCT and self._agent_lost_mid_turn()
+
def _mark_sandbox_gone(self) -> None:
- # A sandbox that vanished mid-turn took the agent's work with it. Mid-setup it is a failed
- # setup for onboarding; see _onboarding_exit_is_failure for the open-PR exemption.
- agent_lost = self._agent_lost_mid_turn()
+ # A sandbox that vanished mid-turn took the agent's work with it, which only a workflow step
+ # needs told; see _agent_lost_exit_is_failure. Mid-setup it is a failed setup for
+ # onboarding; see _onboarding_exit_is_failure for the open-PR exemption.
+ agent_lost = self._agent_lost_exit_is_failure()
self._completion_status = "failed" if agent_lost or self._onboarding_exit_is_failure() else "completed"
self._completion_error = SANDBOX_GONE_ERROR_MESSAGE
self._completion_timeout_marker = SANDBOX_GONE_STATE_KEY