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): complete delivered report runs after watch timeout (#106060)

Georgiy Tarasove19c3f23a0cb
diff --git a/products/signals/backend/supersession.py b/products/signals/backend/supersession.py
index 10635c8751f4..fc0d038188ff 100644
--- a/products/signals/backend/supersession.py
+++ b/products/signals/backend/supersession.py
@@ -180,7 +180,7 @@ def automated_targets(team_id: int, report_id: str) -> list[ImplementationTarget
receipt is None
or run.task_id in active_tasks
or latest_runs[run.task_id] != run.id
- or run.status != "completed"
+ or run.status not in {"completed", "failed"}
or run.environment != "cloud"
or run.mode != "background"
):
@@ -192,9 +192,23 @@ def automated_targets(team_id: int, report_id: str) -> list[ImplementationTarget
or run.state.get("self_driving_head_branch") != content.automation_branch
):
continue
+ verified_urls = (
+ {
+ canonical_pr_url(raw_url)
+ for raw_url in (run.state.get("verified_pr_urls") or [])
+ if isinstance(raw_url, str)
+ }
+ if run.status == "failed" and run.state.get("timed_out_wall_clock") is True
+ else set()
+ )
for raw_url in tasks_facade.read_pr_urls(run.output):
url = canonical_pr_url(raw_url)
- if not url or url not in eligible_urls or url in targets:
+ if (
+ not url
+ or url not in eligible_urls
+ or url in targets
+ or (run.status == "failed" and url not in verified_urls)
+ ):
continue
targets[url] = ImplementationTarget(
task_id=run.task_id,
diff --git a/products/signals/backend/test/test_supersede_handover.py b/products/signals/backend/test/test_supersede_handover.py
index 1eaa84acb3b0..1327216cb65a 100644
--- a/products/signals/backend/test/test_supersede_handover.py
+++ b/products/signals/backend/test/test_supersede_handover.py
@@ -313,6 +313,38 @@ class TestSupersedeHandover(BaseTest):
)
assert not automated_targets(self.team.id, str(self.report.id))
+ def test_wall_clock_timeout_after_verified_pr_allows_replacement(self) -> None:
+ self.implementation_run.status = "failed"
+ self.implementation_run.state = {
+ **self.implementation_run.state,
+ "timed_out_wall_clock": True,
+ "verified_pr_urls": [OLD_PR],
+ }
+ self.implementation_run.save(update_fields=["status", "state"])
+
+ assert {target.pr_url for target in automated_targets(self.team.id, str(self.report.id))} == {OLD_PR}
+ assert _resolve_supersede(self.report, self.decision()).allowed
+
+ @parameterized.expand(
+ [
+ ("other_failure", "failed", False, [OLD_PR]),
+ ("unverified", "failed", True, []),
+ ("cancelled", "cancelled", True, [OLD_PR]),
+ ]
+ )
+ def test_failed_run_without_verified_wall_clock_timeout_is_ineligible(
+ self, _name: str, status: str, timed_out: bool, verified_urls: list[str]
+ ) -> None:
+ self.implementation_run.status = status
+ self.implementation_run.state = {
+ **self.implementation_run.state,
+ "timed_out_wall_clock": timed_out,
+ "verified_pr_urls": verified_urls,
+ }
+ self.implementation_run.save(update_fields=["status", "state"])
+
+ assert not automated_targets(self.team.id, str(self.report.id))
+
def test_fork_and_changed_head_do_not_authorize_replacement(self) -> None:
decision = self.decision()
self.prs[1]["head_sha"] = "human-edit"
diff --git a/products/tasks/backend/temporal/process_task/activities/tests/test_update_task_run_status.py b/products/tasks/backend/temporal/process_task/activities/tests/test_update_task_run_status.py
index 7c0c6e278299..6a72a40c16ba 100644
--- a/products/tasks/backend/temporal/process_task/activities/tests/test_update_task_run_status.py
+++ b/products/tasks/backend/temporal/process_task/activities/tests/test_update_task_run_status.py
@@ -188,19 +188,23 @@ class TestUpdateTaskRunStatusActivity:
@pytest.mark.django_db(transaction=True)
@pytest.mark.parametrize(
- "marker",
- [TIMED_OUT_WALL_CLOCK_STATE_KEY, SANDBOX_GONE_STATE_KEY],
+ "marker,status",
+ [
+ (TIMED_OUT_WALL_CLOCK_STATE_KEY, TaskRun.Status.FAILED),
+ (SANDBOX_GONE_STATE_KEY, TaskRun.Status.FAILED),
+ (TIMED_OUT_WALL_CLOCK_STATE_KEY, TaskRun.Status.COMPLETED),
+ ],
)
- def test_timeout_marker_is_recorded_in_state(self, activity_environment, test_task_run, marker):
+ def test_timeout_marker_is_recorded_in_state(self, activity_environment, test_task_run, marker, status):
input_data = UpdateTaskRunStatusInput(
run_id=str(test_task_run.id),
- status=TaskRun.Status.FAILED,
+ status=status,
timeout_marker=marker,
)
async_to_sync(activity_environment.run)(update_task_run_status, input_data)
test_task_run.refresh_from_db()
- assert test_task_run.status == TaskRun.Status.FAILED
+ assert test_task_run.status == status
assert test_task_run.error_message is None
assert test_task_run.state.get(marker) is True
@@ -323,6 +327,13 @@ class TestUpdateTaskRunStatusActivity:
"task_run_failed",
TIMED_OUT_WALL_CLOCK_STATE_KEY,
),
+ (
+ TaskRun.Status.COMPLETED,
+ False,
+ TIMED_OUT_WALL_CLOCK_STATE_KEY,
+ "task_run_completed",
+ TIMED_OUT_WALL_CLOCK_STATE_KEY,
+ ),
],
)
@patch("products.tasks.backend.models.posthoganalytics.capture")
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 e28cfc82a998..341a53b06ce5 100644
--- a/products/tasks/backend/temporal/process_task/tests/test_workflow.py
+++ b/products/tasks/backend/temporal/process_task/tests/test_workflow.py
@@ -2247,7 +2247,7 @@ class TestProcessTaskWorkflowUnit:
cleanup_sandbox_mock.assert_awaited_once_with("sandbox-123", complete_stream=True)
@pytest.mark.parametrize(
- "event, origin_product, pr_progress_emitted, ci_repetitions, end_of_turn_received, expected_status, expected_kwargs",
+ "event, origin_product, pr_progress_emitted, ci_repetitions, end_of_turn_received, turn_succeeded, expected_status, expected_kwargs",
[
(
process_task_workflow_module.TaskEvent.TIMEOUT_REACHED,
@@ -2255,6 +2255,7 @@ class TestProcessTaskWorkflowUnit:
False,
1,
None,
+ False,
"completed",
{"timed_out_inactivity": True},
),
@@ -2264,6 +2265,7 @@ class TestProcessTaskWorkflowUnit:
False,
1,
None,
+ False,
"failed",
{"timed_out_inactivity": True},
),
@@ -2275,6 +2277,7 @@ class TestProcessTaskWorkflowUnit:
True,
1,
None,
+ False,
"completed",
{"timed_out_inactivity": True},
),
@@ -2286,6 +2289,7 @@ class TestProcessTaskWorkflowUnit:
False,
0,
None,
+ False,
"completed",
{"timed_out_inactivity": True},
),
@@ -2297,6 +2301,7 @@ class TestProcessTaskWorkflowUnit:
False,
1,
False,
+ False,
"failed",
{"error_message": AGENT_LOST_ERROR_MESSAGE, "timed_out_inactivity": True},
),
@@ -2308,6 +2313,7 @@ class TestProcessTaskWorkflowUnit:
False,
1,
False,
+ False,
"completed",
{"timed_out_inactivity": True},
),
@@ -2317,6 +2323,7 @@ class TestProcessTaskWorkflowUnit:
False,
1,
None,
+ False,
"failed",
{"timeout_marker": TIMED_OUT_WALL_CLOCK_STATE_KEY},
),
@@ -2326,6 +2333,37 @@ class TestProcessTaskWorkflowUnit:
False,
1,
None,
+ False,
+ "failed",
+ {"timeout_marker": TIMED_OUT_WALL_CLOCK_STATE_KEY},
+ ),
+ (
+ process_task_workflow_module.TaskEvent.MAX_DURATION_REACHED,
+ "signal_report",
+ True,
+ 1,
+ True,
+ True,
+ "completed",
+ {"timeout_marker": TIMED_OUT_WALL_CLOCK_STATE_KEY},
+ ),
+ (
+ process_task_workflow_module.TaskEvent.MAX_DURATION_REACHED,
+ "signal_report",
+ False,
+ 1,
+ True,
+ True,
+ "failed",
+ {"timeout_marker": TIMED_OUT_WALL_CLOCK_STATE_KEY},
+ ),
+ (
+ process_task_workflow_module.TaskEvent.MAX_DURATION_REACHED,
+ "signal_report",
+ True,
+ 1,
+ True,
+ False,
"failed",
{"timeout_marker": TIMED_OUT_WALL_CLOCK_STATE_KEY},
),
@@ -2339,13 +2377,12 @@ class TestProcessTaskWorkflowUnit:
pr_progress_emitted,
ci_repetitions,
end_of_turn_received,
+ turn_succeeded,
expected_status,
expected_kwargs,
):
- # The wall-clock cap is a failure for every origin; the inactivity timeout only fails for
- # 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.
+ # A report implementation that delivered a PR and finished its turn can complete when its
+ # watcher reaches the wall-clock cap. Other wall-clock exits still fail.
workflow = ProcessTaskWorkflow()
workflow._pr_progress_emitted = pr_progress_emitted
workflow._ci_repetitions = ci_repetitions
@@ -2388,6 +2425,8 @@ class TestProcessTaskWorkflowUnit:
async def wait_for_event():
workflow._end_of_turn_received = end_of_turn_received
+ workflow._agent_active = False if end_of_turn_received is True else None
+ workflow._last_turn_succeeded = turn_succeeded
return event
monkeypatch.setattr(workflow, "_wait_for_event", wait_for_event)
diff --git a/products/tasks/backend/temporal/process_task/workflow.py b/products/tasks/backend/temporal/process_task/workflow.py
index dce456dee5b2..d8c7606879ce 100644
--- a/products/tasks/backend/temporal/process_task/workflow.py
+++ b/products/tasks/backend/temporal/process_task/workflow.py
@@ -413,6 +413,9 @@ _PATCH_ID_COMPLETE_STREAM_AFTER_CLEANUP_FAILURE = "tasks-complete-stream-after-c
# Same two-step deprecate-then-delete cleanup lifecycle as the patches above.
_PATCH_ID_RUN_LIFECYCLE_BOUNDS = "tasks-run-lifecycle-bounds"
+# Keep histories that already recorded a failed wall-clock exit on their original branch.
+_PATCH_ID_DELIVERED_PR_TIMEOUT_STATUS = "tasks-delivered-pr-timeout-status"
+
_PATCH_ID_SNAPSHOT_BEFORE_CI_FOLLOW_UP = "tasks-snapshot-before-ci-follow-up"
AGENT_LOST_ERROR_MESSAGE = "The agent stopped before finishing its turn"
@@ -1592,9 +1595,12 @@ class ProcessTaskWorkflow(PostHogWorkflow):
)
elif timeout_event == TaskEvent.MAX_DURATION_REACHED:
# Only reachable under the lifecycle-bounds patch (the timer is gated on it).
- # 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)
+ status = (
+ "completed"
+ if workflow.patched(_PATCH_ID_DELIVERED_PR_TIMEOUT_STATUS) and self._delivered_pr_watch_complete()
+ else "failed"
+ )
+ await self._update_task_run_status(status, timeout_marker=TIMED_OUT_WALL_CLOCK_STATE_KEY)
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
@@ -2902,6 +2908,18 @@ class ProcessTaskWorkflow(PostHogWorkflow):
return False
return not self.context.create_pr or self._ci_repetitions > 0
+ def _delivered_pr_watch_complete(self) -> bool:
+ return (
+ self.context.origin_product == _ORIGIN_PRODUCT_SIGNAL_REPORT
+ and self._pr_progress_emitted
+ and self._last_turn_succeeded
+ and self._end_of_turn_received is True
+ and self._agent_active is False
+ and self._pending_followup is None
+ and not self._pending_followups
+ and self._pending_babysit is None
+ )
+
def _agent_lost_mid_turn(self) -> bool:
"""True only when an open turn is backed by evidence the agent was still working.