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(workflows): re-check parked waits hourly instead of every 10 minutes (#100273)

Daniel Marchuk1c1af2cfbcde
diff --git a/nodejs/src/cdp/services/hogflows/actions/conditional_branch.test.ts b/nodejs/src/cdp/services/hogflows/actions/conditional_branch.test.ts
index ccdbaeec6fe0..053705f84cb7 100644
--- a/nodejs/src/cdp/services/hogflows/actions/conditional_branch.test.ts
+++ b/nodejs/src/cdp/services/hogflows/actions/conditional_branch.test.ts
@@ -98,7 +98,7 @@ describe('action.conditional_branch', () => {
action.config.delay_duration = '2h'
const result = await checkConditions(invocation, action)
expect(result).toEqual({
- // Should schedule for 10 minutes from now
+ // A conditional_branch has no matcher coverage, so it keeps the ten-minute cap.
scheduledAt: DateTime.utc().plus({ minutes: 10 }),
})
})
@@ -381,7 +381,7 @@ describe('action.conditional_branch', () => {
it('keeps the cached person on a re-check of a wait that already parked', async () => {
const refreshPerson = jest.fn().mockResolvedValue(undefined)
waitInvocation.refreshPerson = refreshPerson
- // Set once the wait parks; its re-checks run 10 minutes apart, by when the cache expired.
+ // Set once the wait parks; its re-checks run an hour apart, by when the cache expired.
waitInvocation.state.currentAction!.pollReparked = true
await handler.execute({
@@ -439,11 +439,10 @@ describe('action.conditional_branch', () => {
expect(result.nextAction).toBeUndefined()
})
- it('re-parks a wait_until_condition on the 10-minute cap (polling retained as backstop)', async () => {
- // Polling is kept for now: a wait_until_condition re-parks on the 10-minute cap and
- // re-checks its condition, even though the subscription matcher also wakes it early on a
- // matching signal. A 30-minute wait therefore schedules ~10 minutes out, not ~30.
- waitAction.config.max_wait_duration = '30m'
+ it('re-parks a wait_until_condition on the hourly cap (backstop retained)', async () => {
+ // The re-check is kept as a reconciliation backstop, so a wait longer than an hour parks
+ // for an hour at a time rather than for its full duration.
+ waitAction.config.max_wait_duration = '4h'
const result = await handler.execute({
invocation: waitInvocation,
@@ -451,9 +450,42 @@ describe('action.conditional_branch', () => {
result: createInvocationResult(waitInvocation),
})
+ expect(result.scheduledAt).toEqual(DateTime.utc().plus({ hours: 1 }))
+ })
+
+ it('keeps the ten-minute cap for a delayed conditional_branch, which the matcher never wakes', async () => {
+ // Every parked-job lookup in the subscription matcher is scoped to wait_until_condition, so
+ // a delayed branch has no wake at all and the re-check is the only thing that advances it.
+ // Sending it through the handler is what exercises the call site that picks the cap.
+ const branchAction = {
+ ...waitAction,
+ type: 'conditional_branch',
+ config: { conditions: [], delay_duration: '4h' },
+ } as unknown as typeof waitAction
+
+ const result = await handler.execute({
+ invocation: waitInvocation,
+ action: branchAction,
+ result: createInvocationResult(waitInvocation),
+ })
+
expect(result.scheduledAt).toEqual(DateTime.utc().plus({ minutes: 10 }))
})
+ it('parks a wait shorter than the backstop for its own duration', async () => {
+ // The cap only shortens a park; a 30-minute wait must still resolve at 30 minutes rather
+ // than being stretched to the hourly re-check.
+ waitAction.config.max_wait_duration = '30m'
+
+ const result = await handler.execute({
+ invocation: waitInvocation,
+ action: waitAction,
+ result: createInvocationResult(waitInvocation),
+ })
+
+ expect(result.scheduledAt).toEqual(DateTime.utc().plus({ minutes: 30 }))
+ })
+
it('marks the wait as re-parked when its condition does not match', async () => {
// The default condition does not match, so the wait re-parks and records that it has
// polled at least once — without counting a poll-only advance.
diff --git a/nodejs/src/cdp/services/hogflows/actions/conditional_branch.ts b/nodejs/src/cdp/services/hogflows/actions/conditional_branch.ts
index e5bff8e4b387..a3707574e0e2 100644
--- a/nodejs/src/cdp/services/hogflows/actions/conditional_branch.ts
+++ b/nodejs/src/cdp/services/hogflows/actions/conditional_branch.ts
@@ -10,12 +10,17 @@ import { findContinueAction, findNextAction, isEvaluableCondition } from '../hog
import { ActionHandler, ActionHandlerOptions, ActionHandlerResult } from './action.interface'
import { calculatedScheduledAt } from './delay'
-const DEFAULT_WAIT_DURATION_SECONDS = 10 * 60
-
-// Increments only when the 10-minute polling re-check advances a wait_until_condition that the
-// subscription matcher did NOT wake (and not an evaluate-on-entry match). This is the decisive
-// signal for removing the poll: while it sits at ~0 across teams for a sustained window, the
-// person/event/internal streams cover every wake and polling is provably redundant.
+// A parked conditional_branch has no matcher coverage: every parked-job lookup in the subscription
+// matcher is scoped to wait_until_condition, so this re-check is the only thing that advances a
+// delayed branch.
+const BRANCH_RECHECK_SECONDS = 10 * 60
+// A wait is woken by the matcher on a matching signal, so its re-check only has to reconcile a wake
+// lost between the condition being evaluated and the job being persisted.
+const WAIT_RECHECK_SECONDS = 60 * 60
+
+// Increments only when the periodic re-check advances a wait_until_condition that the subscription
+// matcher did NOT wake (and not an evaluate-on-entry match). It measures how often the backstop is
+// the only thing that moved a run, which is the rate of wakes the streams lost.
// Labelled by team and flow so a non-zero reading names the workflow still leaning on the poll; a
// series only exists for flows that actually poll-advance, so cardinality tracks incidence.
export const counterHogflowWaitPollOnlyAdvance = new Counter({
@@ -75,7 +80,7 @@ export class ConditionalBranchHandler implements ActionHandler {
// The person the worker read at dequeue can predate a write this wait is waiting for, and a
// wait that parks on that read is stuck: the write already happened, so no person message
// follows to wake it. Re-read before the first evaluation of each wait — including a wait
- // reached later in the same dequeue. Re-checks of a wait that already parked run 10 minutes
+ // reached later in the same dequeue. Re-checks of a wait that already parked run an hour
// apart, by when the cache has expired, so they keep the cheaper read.
if (action.type === 'wait_until_condition' && !invocation.state?.currentAction?.pollReparked) {
const refreshed = await invocation.refreshPerson?.()
@@ -110,7 +115,8 @@ export class ConditionalBranchHandler implements ActionHandler {
const conditionResult = await checkConditions(
invocation,
conditionalAction,
- this.createMemberCohortIdsLoader(invocation)
+ this.createMemberCohortIdsLoader(invocation),
+ action.type === 'wait_until_condition' ? WAIT_RECHECK_SECONDS : BRANCH_RECHECK_SECONDS
)
const isWait = action.type === 'wait_until_condition'
@@ -184,7 +190,10 @@ function conditionReferencesCohorts(condition: { filters?: unknown }): boolean {
export async function checkConditions(
invocation: CyclotronJobInvocationHogFlow,
action: Extract<HogFlowAction, { type: 'conditional_branch' }>,
- loadMemberCohortIds?: () => Promise<number[]>
+ loadMemberCohortIds?: () => Promise<number[]>,
+ // A wait is normalised into a conditional_branch before it gets here, so the caller decides which
+ // cap applies; the type on `action` can no longer tell the two apart.
+ recheckSeconds: number = BRANCH_RECHECK_SECONDS
): Promise<{
scheduledAt?: DateTime
nextAction?: HogFlowAction
@@ -219,13 +228,13 @@ export async function checkConditions(
}
if (action.config.delay_duration) {
- // Re-park on the 10-minute cap so the condition is re-checked by polling. The subscription
- // matcher also wakes the job early on a matching signal, but polling is kept as the backstop
- // for now; removing it is a follow-up once the matcher streams are proven in production.
+ // Re-park on the cap for this step type. A wake arriving between this evaluation and the job
+ // being persisted finds no available row and is never replayed, so neither step type can rely
+ // on the matcher alone.
const scheduledAt = calculatedScheduledAt(
action.config.delay_duration,
invocation.state.currentAction?.startedAtTimestamp,
- DEFAULT_WAIT_DURATION_SECONDS
+ recheckSeconds
)
if (scheduledAt) {
diff --git a/products/workflows/backend/services/timing_reschedule.py b/products/workflows/backend/services/timing_reschedule.py
index 2c0f4414e9fa..c633d533062e 100644
--- a/products/workflows/backend/services/timing_reschedule.py
+++ b/products/workflows/backend/services/timing_reschedule.py
@@ -6,10 +6,9 @@ import structlog
logger = structlog.get_logger(__name__)
# Steps whose parked runs a timing edit can strand: delays park up to 30 days out and time
-# windows up to a week. wait_until_condition currently re-parks on a 10-minute polling cap
-# (so a sweep is a cheap no-op re-park), but the poll is slated for removal in favor of
-# matcher wakes - after which its max-wait deadline parks for the full duration like a
-# delay and shortening it strands runs without a sweep.
+# windows up to a week. wait_until_condition re-parks on an hourly reconciliation cap, so a
+# sweep is a cheap no-op re-park for waits shorter than that; past an hour the sweep is what
+# stops a shortened max-wait stranding runs.
TIMING_ACTION_TYPES = {"delay", "wait_until_time_window", "wait_until_condition"}
# Only jobs parked on this many steps or fewer get swept; a diff touching more than this is
diff --git a/products/workflows/backend/services/wait_clock_conditions.py b/products/workflows/backend/services/wait_clock_conditions.py
index 4dc59d641fa6..9dd39f973ba0 100644
--- a/products/workflows/backend/services/wait_clock_conditions.py
+++ b/products/workflows/backend/services/wait_clock_conditions.py
@@ -3,11 +3,12 @@ Detects `wait_until_condition` conditions that only the polling re-check could e
A wait is woken by the subscription matcher when a message arrives on one of its streams: an event,
a person change, an internal event, a distinct_id repoint. A condition that compares against the
-clock produces no message at all, so nothing wakes it. The 10-minute polling re-check is what has
-been covering that gap, and it re-parks every wait in the fleet to do it.
+clock produces no message at all, so nothing wakes it. The periodic re-check is what has been
+covering that gap, and it re-parks every wait in the fleet to do it.
-Rejecting these at save time is what lets the poll go: express the same intent with a delay step and
-a condition that guards it, and the wait is woken by something that actually happens.
+Rejecting these at save time keeps that re-check a backstop rather than the mechanism: express the
+same intent with a delay step and a condition that guards it, and the wait is woken by something
+that actually happens.
"""
from typing import Optional