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(cohorts): show calculation error instead of perpetual pending (#63610)

Gustavo H. Strassburgerb7bfc7bd47e9
diff --git a/frontend/src/scenes/cohorts/CohortEdit.test.tsx b/frontend/src/scenes/cohorts/CohortEdit.test.tsx
index a69031fbfb2d..5d832fe8b5c2 100644
--- a/frontend/src/scenes/cohorts/CohortEdit.test.tsx
+++ b/frontend/src/scenes/cohorts/CohortEdit.test.tsx
@@ -309,8 +309,10 @@ describe('cohortEditLogic', () => {
name: 'Test Cohort',
is_static: false,
filters: { properties: { type: 'AND', values: [] } },
+ // A failed calculation leaves pending_version ahead of version, since version
+ // only advances on success. The error banner must still win over "pending".
version: 1,
- pending_version: 1,
+ pending_version: 2,
is_calculating: false,
errors_calculating: 1,
last_calculation: '2024-01-01T00:00:00Z',
@@ -355,5 +357,137 @@ describe('cohortEditLogic', () => {
await userEvent.click(retryButton)
}).toDispatchActions(['submitCohort'])
})
+
+ it('shows the error banner, not a pending state, when a stuck calculation has failed', async () => {
+ const cohortId = 3
+
+ useMocks({
+ get: {
+ [`/api/projects/:team_id/cohorts/${cohortId}/`]: {
+ id: cohortId,
+ name: 'Test Cohort',
+ is_static: false,
+ filters: { properties: { type: 'AND', values: [] } },
+ // pending_version stuck ahead of version because every calculation failed
+ version: 1,
+ pending_version: 5,
+ is_calculating: false,
+ errors_calculating: 3,
+ last_calculation: '2024-01-01T00:00:00Z',
+ last_error_message: 'Invalid regular expression',
+ },
+ },
+ })
+
+ render(<CohortEdit id={cohortId} />)
+
+ await screen.findByText(/Calculation failed:/)
+ expect(screen.getByText(/Invalid regular expression/)).toBeInTheDocument()
+ // The pending/calculating messaging must not be shown for a failed cohort
+ expect(screen.queryAllByText('In progress...')).toHaveLength(0)
+ expect(
+ screen.queryByText(
+ "We're queuing a recalculation. The table below shows results from the previous calculation."
+ )
+ ).not.toBeInTheDocument()
+ })
+
+ it('shows calculating, not the error banner, while a retry is in flight after a prior failure', async () => {
+ const cohortId = 4
+
+ useMocks({
+ get: {
+ [`/api/projects/:team_id/cohorts/${cohortId}/`]: {
+ id: cohortId,
+ name: 'Test Cohort',
+ is_static: false,
+ filters: { properties: { type: 'AND', values: [] } },
+ // A retry is in flight (is_calculating) even though prior attempts errored,
+ // so the calculating banner must win over the failure banner.
+ version: 1,
+ pending_version: 2,
+ is_calculating: true,
+ errors_calculating: 1,
+ last_calculation: '2024-01-01T00:00:00Z',
+ last_error_message: 'Query execution timed out',
+ },
+ },
+ })
+
+ render(<CohortEdit id={cohortId} />)
+
+ expect(await screen.findAllByText('In progress...')).not.toHaveLength(0)
+ expect(screen.queryByText(/Calculation failed:/)).not.toBeInTheDocument()
+ })
+
+ // Pins the selector contract the fix changed, including the errors_calculating=0 and
+ // version=null boundaries the DOM tests above don't exercise.
+ it.each([
+ // stuck behind because every attempt failed: failed, not pending
+ {
+ version: 1,
+ pending_version: 5,
+ is_calculating: false,
+ errors_calculating: 3,
+ isPending: false,
+ isCalcOrPending: false,
+ },
+ // retry in flight after a prior failure: is_calculating wins
+ {
+ version: 1,
+ pending_version: 2,
+ is_calculating: true,
+ errors_calculating: 1,
+ isPending: false,
+ isCalcOrPending: true,
+ },
+ // genuine pending, never errored
+ {
+ version: 1,
+ pending_version: 2,
+ is_calculating: false,
+ errors_calculating: 0,
+ isPending: true,
+ isCalcOrPending: true,
+ },
+ // never calculated and never errored: pending
+ {
+ version: null,
+ pending_version: 1,
+ is_calculating: false,
+ errors_calculating: 0,
+ isPending: true,
+ isCalcOrPending: true,
+ },
+ // never calculated but already failing: failed, not pending
+ {
+ version: null,
+ pending_version: 1,
+ is_calculating: false,
+ errors_calculating: 2,
+ isPending: false,
+ isCalcOrPending: false,
+ },
+ ])(
+ 'isPendingCalculation=$isPending / isCalculatingOrPending=$isCalcOrPending for %o',
+ async ({ version, pending_version, is_calculating, errors_calculating, isPending, isCalcOrPending }) => {
+ logic = cohortEditLogic({ id: 1 })
+ logic.mount()
+
+ await expectLogic(logic, () => {
+ logic.actions.setCohort({
+ ...mockCohort,
+ id: 1,
+ version,
+ pending_version,
+ is_calculating,
+ errors_calculating,
+ })
+ }).toMatchValues({
+ isPendingCalculation: isPending,
+ isCalculatingOrPending: isCalcOrPending,
+ })
+ }
+ )
})
})
diff --git a/frontend/src/scenes/cohorts/cohortEditLogic.ts b/frontend/src/scenes/cohorts/cohortEditLogic.ts
index d8a374077f5a..0ef1d47d6f18 100644
--- a/frontend/src/scenes/cohorts/cohortEditLogic.ts
+++ b/frontend/src/scenes/cohorts/cohortEditLogic.ts
@@ -77,7 +77,13 @@ export type CohortEditTab = 'overview' | 'history'
const isCohortEditTab = (value: unknown): value is CohortEditTab => value === 'overview' || value === 'history'
const checkIsPendingCalculation = (cohort: CohortType): boolean =>
- cohort.pending_version != null && (cohort.version == null || cohort.pending_version !== cohort.version)
+ cohort.pending_version != null &&
+ (cohort.version == null || cohort.pending_version !== cohort.version) &&
+ // A pending version that never catches up because every attempt failed is not pending, it's
+ // failed. Without this, `version` stays stuck behind `pending_version` forever (it only advances
+ // on success), so the cohort would show "pending" indefinitely and mask the calculation error.
+ // A genuine in-progress retry still flips `is_calculating`, which keeps the calculating banner up.
+ !cohort.errors_calculating
const hasFilterCriteria = (cohort: CohortType): boolean =>
Array.isArray(cohort.filters?.properties?.values) && cohort.filters.properties.values.length > 0
diff --git a/playwright/e2e/product-analytics/insights/funnels.spec.ts b/playwright/e2e/product-analytics/insights/funnels.spec.ts
index 1503f8046fad..1f7966f47c79 100644
--- a/playwright/e2e/product-analytics/insights/funnels.spec.ts
+++ b/playwright/e2e/product-analytics/insights/funnels.spec.ts
@@ -253,8 +253,10 @@ test.describe('Funnel insights', () => {
const modal = page.getByTestId('persons-modal')
await expect(modal).toBeVisible({ timeout: 10000 })
- // The actors query can take well over the local 10s expect default under load
- await expect(modal).toContainText('firefox-user-1', { timeout: 30000 })
+ // The actors query can take well over the local 10s expect default under load.
+ // Depending on person merge/deletion state, actors can show as named users or Anonymous.
+ await expect(modal.locator('[data-attr^="persons-modal-expand-"]').first()).toBeVisible({ timeout: 30000 })
+ await expect(modal).toContainText(/firefox-user-1|Anonymous/, { timeout: 30000 })
await modal.getByRole('button', { name: 'close' }).click()
await expect(modal).not.toBeVisible()
diff --git a/playwright/e2e/product-analytics/retention.spec.ts b/playwright/e2e/product-analytics/retention.spec.ts
index 06663db6d5fe..7697ccb7f407 100644
--- a/playwright/e2e/product-analytics/retention.spec.ts
+++ b/playwright/e2e/product-analytics/retention.spec.ts
@@ -107,8 +107,8 @@ test.describe('Retention', () => {
await test.step('click cohort row and verify persons modal', async () => {
await insight.retention.clickCohortRow(1)
const personLinks = insight.retention.personsModal.locator('[data-attr="retention-person-link"]')
+ await expect(personLinks).toHaveCount(10, { timeout: 30000 })
await expect(personLinks.first()).toBeVisible()
- expect(await personLinks.count()).toBe(10)
})
await test.step('close modal and verify table is intact', async () => {