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(cdp): unshadow rerun paginator attempts alias so max_attempts filter works (#72286)

Daniel Marchukb86a04de02b5
diff --git a/nodejs/src/cdp/rerun/rerun-paginator.service.test.ts b/nodejs/src/cdp/rerun/rerun-paginator.service.test.ts
index cf14e2631791..21fc6b2c33af 100644
--- a/nodejs/src/cdp/rerun/rerun-paginator.service.test.ts
+++ b/nodejs/src/cdp/rerun/rerun-paginator.service.test.ts
@@ -81,6 +81,12 @@ describe('RerunPaginatorService integration', () => {
invocation_id: string
status: 'running' | 'succeeded' | 'failed'
error?: Error
+ // Stable error_kind stamped on the row (e.g. 'janitor_poison_pill').
+ // When omitted it is derived from the error message, as before.
+ errorKind?: string
+ // Prior rerun count → the row's `attempts` column, for exercising the
+ // max_attempts filter. Defaults to 0.
+ rerunAttempts?: number
scheduledAt?: Date
}>
): Promise<void> => {
@@ -100,6 +106,7 @@ describe('RerunPaginatorService integration', () => {
globals: globals as any,
timings: [],
attempts: 0,
+ rerunAttempts: r.rerunAttempts,
},
teamId: team.id,
functionId: hogFunction.id,
@@ -108,7 +115,7 @@ describe('RerunPaginatorService integration', () => {
queuePriority: 0,
queueScheduledAt: r.scheduledAt ? ({ toJSDate: () => r.scheduledAt } as any) : undefined,
}
- seedingService.queueLifecycleRow(invocation, r.status, { error: r.error })
+ seedingService.queueLifecycleRow(invocation, r.status, { error: r.error, errorKind: r.errorKind })
}
await seedingService.flush()
@@ -488,6 +495,64 @@ describe('RerunPaginatorService integration', () => {
})
})
+ describe('max_attempts filter (poison-pill autodrain path)', () => {
+ // Regression guard for the aggregate-inside-aggregate ClickHouse error: with
+ // max_attempts set, fetchPage adds `argMax(attempts, version) < {max_attempts}`
+ // to HAVING. If the SELECT alias is named `attempts` it shadows the raw column,
+ // so `attempts` inside the HAVING argMax resolves to the alias (itself an
+ // aggregate) → ClickHouse rejects every page. No other test sets max_attempts
+ // (the manual-rerun UI leaves it unset), so this whole clause was dormant until
+ // the autodrain became the first caller to set it unconditionally.
+ it('runs a valid query and honours the cap when max_attempts is set', async () => {
+ await seedRows([
+ {
+ invocation_id: 'pp-under-cap',
+ status: 'failed',
+ error: new Error('poison pill'),
+ errorKind: 'janitor_poison_pill',
+ rerunAttempts: 1,
+ },
+ {
+ invocation_id: 'pp-over-cap',
+ status: 'failed',
+ error: new Error('poison pill'),
+ errorKind: 'janitor_poison_pill',
+ rerunAttempts: 5,
+ },
+ ])
+
+ // The exact filter the autodrain enqueues.
+ const state = buildState({
+ request: {
+ filter: {
+ window_start: '2026-01-01T00:00:00Z',
+ window_end: '2027-01-01T00:00:00Z',
+ status: ['failed'],
+ error_kind: ['janitor_poison_pill'],
+ max_attempts: 3,
+ },
+ },
+ })
+
+ const { state: next } = await paginator.processPage(team.id, state, {
+ jobId: 'test-rerun-job',
+ createdAt: DateTime.now(),
+ })
+
+ // The query executed — without the alias fix this is the CH
+ // aggregate-inside-aggregate error and every page fails here.
+ expect(next.progress.last_error).toBeUndefined()
+
+ // Under the cap is re-enqueued; over the cap is filtered by the HAVING clause.
+ const enqueued = hogQueue.queueInvocations.mock.calls[0]?.[0] as
+ | CyclotronJobInvocationHogFunction[]
+ | undefined
+ expect(enqueued?.map((i) => i.id)).toEqual(['pp-under-cap'])
+ expect(next.progress.queued).toBe(1)
+ expect(next.progress.done).toBe(true)
+ })
+ })
+
describe('rehydration', () => {
it('skips rows whose hog function has been deleted between original run and rerun', async () => {
const orphanedFn = await _insertHogFunction(hub.postgres, team.id, {
diff --git a/nodejs/src/cdp/rerun/rerun-paginator.service.ts b/nodejs/src/cdp/rerun/rerun-paginator.service.ts
index 42e3117cc7f0..b1c1013cfd4c 100644
--- a/nodejs/src/cdp/rerun/rerun-paginator.service.ts
+++ b/nodejs/src/cdp/rerun/rerun-paginator.service.ts
@@ -67,7 +67,9 @@ const toClickhouseDateTime = (value: string): string => {
interface InvocationRow {
invocation_id: string
parent_run_id: string
- attempts: number
+ // Prior rerun count for this invocation (argMax over version). Named to avoid
+ // shadowing the raw `attempts` column in the fetch query — see fetchPage.
+ latest_attempts: number
last_scheduled_at: string
first_scheduled_at: string
invocation_globals: string
@@ -467,7 +469,12 @@ export class RerunPaginatorService {
SELECT
invocation_id,
argMax(parent_run_id, version) AS parent_run_id,
- argMax(attempts, version) AS attempts,
+ -- NOT aliased 'attempts': that would shadow the raw column, and the
+ -- max_attempts HAVING below (argMax(attempts, version) < …) would then
+ -- resolve 'attempts' to this alias — an aggregate inside an aggregate,
+ -- which ClickHouse rejects. Any caller setting max_attempts (the poison-
+ -- pill autodrain always does) would fail every page. Keep the names distinct.
+ argMax(attempts, version) AS latest_attempts,
argMax(invocation_globals, version) AS invocation_globals,
argMax(first_scheduled_at, version) AS first_scheduled_at,
max(scheduled_at) AS last_scheduled_at
@@ -518,7 +525,7 @@ export class RerunPaginatorService {
// across concurrent callers, so a sequential loop would defeat that.
const rehydrated = await Promise.all(
rows.map(async (row): Promise<CyclotronJobInvocation | null> => {
- if (maxAttempts !== undefined && row.attempts >= maxAttempts) {
+ if (maxAttempts !== undefined && row.latest_attempts >= maxAttempts) {
counterRerunInvocationsSkipped.labels(state.function_kind, 'over_max_attempts').inc()
return null
}
@@ -614,7 +621,7 @@ export class RerunPaginatorService {
// rerun count) drives `is_retry` and `attempts` on the
// lifecycle rows.
attempts: 0,
- rerunAttempts: (row.attempts || 0) + 1,
+ rerunAttempts: (row.latest_attempts || 0) + 1,
// Carry the original first-scheduled time forward — the
// producer writes this verbatim on every retry's lifecycle
// rows so ReplacingMergeTree doesn't collapse it away.
@@ -676,7 +683,7 @@ export class RerunPaginatorService {
// Sticky rerun counter — mirror the hog function path so the
// lifecycle row producer can derive `attempts` / `is_retry`
// for flows too, and the `max_attempts` guard actually trips.
- rerunAttempts: (row.attempts || 0) + 1,
+ rerunAttempts: (row.latest_attempts || 0) + 1,
firstScheduledAt: row.first_scheduled_at,
}
return invocation