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(visual-review): keep the supersession chain intact when a run is deleted (#97270)

Julian Bezc5fa2dab32ec
diff --git a/products/visual_review/backend/logic/retention.py b/products/visual_review/backend/logic/retention.py
index 723aa261d85c..f11c8575417a 100644
--- a/products/visual_review/backend/logic/retention.py
+++ b/products/visual_review/backend/logic/retention.py
@@ -168,6 +168,16 @@ class RetentionSweep:
.values_list("id", flat=True)[:limit]
)
+ def _splice_out_of_chain(self, run_id: UUID) -> None:
+ """Give the runs that name this run its own successor instead."""
+ successor_id = self._runs().filter(id=run_id).values_list("superseded_by_id", flat=True).first()
+ if successor_id is None:
+ # The run is the group's latest. Only the quiet-branch pass deletes
+ # one of those, and it takes a group where no run is superseded, so
+ # there is nothing to re-point.
+ return
+ self._runs().filter(superseded_by_id=run_id).update(superseded_by_id=successor_id)
+
def _delete_runs(self, run_ids: list[UUID]) -> int:
deleted = 0
# One run per DELETE, in the order given (oldest first). Django applies
@@ -175,10 +185,18 @@ class RetentionSweep:
# anything, so a batch that holds two links of one supersession chain
# would set the older link to NULL while the group's latest run still
# exists and break the unique_latest_run_per_group index.
+ #
+ # Oldest first is not enough on its own, because the retention class is
+ # a property of the row, not of its age: a run with no PR number counts
+ # as protected history and gets 180 days while the runs after it on the
+ # same branch get 30. Such a run survives the sweep that deletes the run
+ # it names, so it has to be re-pointed before the DELETE runs.
for run_id in run_ids:
if self._out_of_time():
break
- _total, per_model = self._runs().filter(id=run_id).delete()
+ with transaction.atomic(using=WRITER_DB):
+ self._splice_out_of_chain(run_id)
+ _total, per_model = self._runs().filter(id=run_id).delete()
deleted += per_model.get(Run._meta.label, 0)
return deleted
diff --git a/products/visual_review/backend/tests/logic/test_retention.py b/products/visual_review/backend/tests/logic/test_retention.py
index 18d79a453d1c..3c5b0db9f56a 100644
--- a/products/visual_review/backend/tests/logic/test_retention.py
+++ b/products/visual_review/backend/tests/logic/test_retention.py
@@ -158,6 +158,20 @@ class TestRetentionSweep:
== 1
)
+ def test_protected_run_is_repointed_when_the_run_it_names_expires(self, repo, now):
+ latest = self._run(repo, now, age_days=5)
+ expired = self._run(repo, now, age_days=40, superseded_by=latest)
+ # No PR number counts as protected history, so this one keeps its 180
+ # days while the run it names goes at 30.
+ protected = self._run(repo, now, age_days=40, pr_number=None, superseded_by=expired)
+
+ result = retention.sweep_repo(repo, now=now)
+
+ assert result.runs_deleted == 1
+ assert not Run.objects.filter(id=expired.id).exists()
+ protected.refresh_from_db()
+ assert protected.superseded_by_id == latest.id
+
@pytest.mark.parametrize(
("model", "extra_fields"),
[