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(replay-vision): reconcile scanner schedules before the reapers (#88749)

Kim Svatos Dugan1ed92abaf100
diff --git a/products/replay_vision/backend/temporal/reconciler.py b/products/replay_vision/backend/temporal/reconciler.py
index 9361bf19672a..78db05fa4b0e 100644
--- a/products/replay_vision/backend/temporal/reconciler.py
+++ b/products/replay_vision/backend/temporal/reconciler.py
@@ -61,7 +61,24 @@ class ReconcileScannerSchedulesWorkflow(PostHogWorkflow):
@workflow.run
async def run(self, inputs: ReconcileScannerSchedulesInputs) -> ReconcileScannerSchedulesResult:
- # Best-effort and first: a schedule-sync failure below must not starve the reapers, and vice versa.
+ if workflow.patched("sync-schedules-before-reapers-2026-08"):
+ # Schedule sync goes first because it is the only phase a user feels: an enabled scanner
+ # that never gets a sweep schedule silently never scans. The reapers only settle rows that
+ # are already wrong, so a skipped pass costs one tick. The reapers' combined start-to-close
+ # budget is larger than the workflow execution timeout, so running them first lets one slow
+ # reaper starve the sync on every tick.
+ result, systemic_failure = await self._sync_schedules()
+ await self._run_reapers()
+ else:
+ await self._run_reapers()
+ result, systemic_failure = await self._sync_schedules()
+ if systemic_failure is not None:
+ raise systemic_failure
+ return result
+
+ async def _run_reapers(self) -> None:
+ # Each reaper is best-effort and isolated: one failure must not stop the others, and none of
+ # them may stop schedule sync.
try:
await self._run_reaper(
reap_orphaned_observations_activity,
@@ -95,6 +112,9 @@ class ReconcileScannerSchedulesWorkflow(PostHogWorkflow):
except Exception:
workflow.logger.exception("replay_vision.reap_backfill_schedules_failed")
+ async def _sync_schedules(self) -> tuple[ReconcileScannerSchedulesResult, ApplicationError | None]:
+ """Converge per-scanner schedules with the table. Returns the result plus a systemic failure to
+ raise once the rest of the tick is done."""
# A scanner toggled between the two listings recovers on the next tick.
enabled_entries, existing_entries = await asyncio.gather(
workflow.execute_activity(
@@ -157,8 +177,8 @@ class ReconcileScannerSchedulesWorkflow(PostHogWorkflow):
attempted = len(to_upsert) + len(to_delete)
succeeded = len(result.upserted) + len(result.deleted)
if attempted > 0 and succeeded == 0:
- raise ApplicationError(f"reconciler: all {attempted} fan-out activities failed")
- return result
+ return result, ApplicationError(f"reconciler: all {attempted} fan-out activities failed")
+ return result, None
async def _run_reaper(
self,
diff --git a/products/replay_vision/backend/tests/test_reconciler.py b/products/replay_vision/backend/tests/test_reconciler.py
index 3f56ce4b37bf..da669ecc4bba 100644
--- a/products/replay_vision/backend/tests/test_reconciler.py
+++ b/products/replay_vision/backend/tests/test_reconciler.py
@@ -37,6 +37,7 @@ from products.replay_vision.backend.temporal.activities import (
delete_scanner_schedule_activity,
list_enabled_scanners_activity,
list_scanner_schedules_activity,
+ reap_backfill_schedules_activity,
reap_childless_inline_scanners_activity,
reap_orphaned_observations_activity,
reap_stuck_vision_action_runs_activity,
@@ -235,8 +236,12 @@ class _ReconcileMocks:
self.reap_stuck_run_calls = 0
self.upserted: list[uuid.UUID] = []
self.deleted: list[uuid.UUID] = []
+ self.calls: list[Any] = []
async def execute_activity(self, activity_fn: Any, activity_input: Any = None, **_: Any) -> Any:
+ self.calls.append(activity_fn)
+ if activity_fn in (reap_childless_inline_scanners_activity, reap_backfill_schedules_activity):
+ return 0
if activity_fn is reap_orphaned_observations_activity:
self.reap_calls += 1
if self.reap_error:
@@ -406,9 +411,27 @@ async def test_reconcile_workflow_pre_patch_skips_run_reaper() -> None:
assert mocks.reap_stuck_run_calls == 0
[email protected]([("patched_syncs_first", True), ("pre_patch_reaps_first", False)])
+async def test_reconcile_workflow_orders_sync_before_reapers(_name: str, patched: bool) -> None:
+ # The reapers' combined start-to-close budget is larger than the workflow execution timeout, so
+ # running them ahead of the sync lets one slow reaper starve schedule sync on every tick. Replays
+ # of pre-patch executions must keep the old order or they fail the determinism check.
+ sid = uuid.uuid4()
+ fp = compute_schedule_fingerprint({"sample_rate": 0.5})
+ mocks = _ReconcileMocks(enabled=_enabled((sid, 1, fp)), existing=_existing())
+ result = await _run_reconcile(mocks, patched=patched)
+ synced_first = mocks.calls.index(list_enabled_scanners_activity) < mocks.calls.index(
+ reap_orphaned_observations_activity
+ )
+ assert synced_first is patched
+ # Either way the sync itself still happens.
+ assert result.upserted == [sid]
+
+
@pytest.mark.asyncio
async def test_reconcile_workflow_survives_reap_failure() -> None:
- # The reaper is best-effort: its failure must not block schedule sync (and vice versa — it runs first).
+ # The reaper is best-effort: its failure must not block schedule sync, and vice versa.
sid = uuid.uuid4()
fp = compute_schedule_fingerprint({"sample_rate": 0.5})
mocks = _ReconcileMocks(