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(experiments): heal the latest run only when a metric is missing (#115684)

Rodrigo Ilorofb1f643c5af0
diff --git a/frontend/src/scenes/experiments/MetricsView/new/MetricRowGroup.tsx b/frontend/src/scenes/experiments/MetricsView/new/MetricRowGroup.tsx
index ba0906eee8a8..6955f8159224 100644
--- a/frontend/src/scenes/experiments/MetricsView/new/MetricRowGroup.tsx
+++ b/frontend/src/scenes/experiments/MetricsView/new/MetricRowGroup.tsx
@@ -917,7 +917,7 @@ export function MetricRowGroup({
metric={metric}
error={error}
query={debugQuery}
- onRetry={handleRetry}
+ onRetry={error?.retriable === true ? handleRetry : undefined}
retryDisabledReason={isRecalculating ? 'Recalculation in progress' : undefined}
/>
</td>
diff --git a/frontend/src/scenes/experiments/experimentMetricsLogic.test.ts b/frontend/src/scenes/experiments/experimentMetricsLogic.test.ts
index eec344bc2d17..e74306c94311 100644
--- a/frontend/src/scenes/experiments/experimentMetricsLogic.test.ts
+++ b/frontend/src/scenes/experiments/experimentMetricsLogic.test.ts
@@ -264,7 +264,7 @@ describe('experimentMetricsLogic', () => {
await expectLogic(logic).toDispatchActions(['setCurrentRecalculation', 'setPrimaryMetricsResultsErrors'])
- expect(logic.values.primaryMetricsResultsErrors[0]).toEqual({ detail: 'no events' })
+ expect(logic.values.primaryMetricsResultsErrors[0]).toEqual({ detail: 'no events', retriable: false })
})
it('counts failures toward progress and surfaces errors while a run is still in progress', async () => {
@@ -312,7 +312,7 @@ describe('experimentMetricsLogic', () => {
}
// Errors must surface in-row even mid-flight.
- expect(logic.values.primaryMetricsResultsErrors[0]).toEqual({ detail: 'boom-primary' })
+ expect(logic.values.primaryMetricsResultsErrors[0]).toEqual({ detail: 'boom-primary', retriable: false })
// Progress must count failures, not just completes; otherwise a fully-failing run shows 0/3 forever.
expect(logic.values.recalculationProgress).toEqual({ completed: 2, total: 3 })
jest.useRealTimers()
@@ -337,7 +337,7 @@ describe('experimentMetricsLogic', () => {
// The successful secondary metric loads its result.
expect(logic.values.secondaryMetricsResults[0]).toEqual(secondaryResult)
// The failed primary metric must keep its error for the in-row box, not be cleared by its null result.
- expect(logic.values.primaryMetricsResultsErrors[0]).toEqual({ detail: 'boom' })
+ expect(logic.values.primaryMetricsResultsErrors[0]).toEqual({ detail: 'boom', retriable: false })
})
it('shows no error toast on 404 (a fresh recalc is triggered instead)', async () => {
@@ -415,19 +415,13 @@ describe('experimentMetricsLogic', () => {
experiment: experimentWithExtraMetric,
},
{
- // A transient error that ran out of attempts: the backend marks it retriable, so a new run
- // can fix it. The window is reused, so only this metric recomputes.
- name: 'a failed run whose failure is retriable',
+ name: 'a failed run that has no entry for a metric',
latest: {
...partialFailureRecalculation,
- metric_errors: {
- [PRIMARY_METRIC_UUID]: {
- step: 'calculation',
- message: 'boom',
- error_type: 'timeout',
- retriable: true,
- },
- },
+ metric_errors: {},
+ results: partialFailureRecalculation.results.filter(
+ ({ metric_uuid }) => metric_uuid !== PRIMARY_METRIC_UUID
+ ),
},
experiment: EXPERIMENT,
},
@@ -452,34 +446,51 @@ describe('experimentMetricsLogic', () => {
expect(capturedBody).toEqual({ trigger: 'heal_latest_run' })
})
- it.each([
- {
- // The metric config, the data, or a resource limit must change first: only a user retry re-runs it.
- name: 'a non-retriable failure',
- metricError: { step: 'calculation', message: 'boom', error_type: 'validation_error', retriable: false },
- },
- {
- // A run recorded before the flag existed: never heal it, or every page load would start a run.
- name: 'a failure recorded without the retriable flag',
- metricError: { step: 'calculation', message: 'boom' },
+ const metricErrors = {
+ retriable: { step: 'calculation', message: 'boom', error_type: 'timeout', retriable: true },
+ 'non-retriable': {
+ step: 'calculation',
+ message: 'boom',
+ error_type: 'validation_error',
+ retriable: false,
},
- ])('does not heal $name', async ({ metricError }) => {
+ 'unflagged legacy': { step: 'calculation', message: 'boom' },
+ }
+ const experimentsByState = {
+ running: EXPERIMENT,
+ stopped: { ...EXPERIMENT, end_date: '2025-01-30T21:55:00Z' } as Experiment,
+ }
+
+ it.each(
+ (Object.keys(metricErrors) as (keyof typeof metricErrors)[]).flatMap((errorKind) =>
+ (Object.keys(experimentsByState) as (keyof typeof experimentsByState)[]).map((state) => ({
+ errorKind,
+ state,
+ }))
+ )
+ )('does not heal a $errorKind failure on a $state experiment', async ({ errorKind, state }) => {
const createMock = jest.fn(() => [201, completedRecalculation2])
useMocks({
get: {
'/api/projects/:team_id/experiments/:id/metrics_recalculation/latest/': () => [
200,
- { ...partialFailureRecalculation, metric_errors: { [PRIMARY_METRIC_UUID]: metricError } },
+ {
+ ...partialFailureRecalculation,
+ metric_errors: { [PRIMARY_METRIC_UUID]: metricErrors[errorKind] },
+ },
],
},
post: { '/api/projects/:team_id/experiments/:id/metrics_recalculation/': createMock },
})
- logic = experimentMetricsLogic({ experiment: EXPERIMENT })
+ logic = experimentMetricsLogic({ experiment: experimentsByState[state] })
logic.mount()
await expectLogic(logic).toDispatchActions(['loadLatestRecalculation']).toFinishAllListeners()
expect(createMock).not.toHaveBeenCalled()
- expect(logic.values.primaryMetricsResultsErrors[0]).toEqual({ detail: 'boom' })
+ expect(logic.values.primaryMetricsResultsErrors[0]).toEqual({
+ detail: 'boom',
+ retriable: errorKind === 'retriable',
+ })
})
it('applies terminal results and resumes polling the active run (reload while recalculating)', async () => {
@@ -1214,7 +1225,7 @@ describe('experimentMetricsLogic', () => {
// The successful secondary metric still loads its result.
expect(logic.values.secondaryMetricsResults[0]).toEqual(secondaryResult)
// The failed primary metric gets an error with the failure message for its in-row box.
- expect(logic.values.primaryMetricsResultsErrors[0]).toEqual({ detail: 'boom' })
+ expect(logic.values.primaryMetricsResultsErrors[0]).toEqual({ detail: 'boom', retriable: false })
})
it('gives up after MAX_POLL_RETRIES consecutive retrieve failures', async () => {
diff --git a/frontend/src/scenes/experiments/experimentMetricsLogic.ts b/frontend/src/scenes/experiments/experimentMetricsLogic.ts
index 7514332591de..f02f34247cc4 100644
--- a/frontend/src/scenes/experiments/experimentMetricsLogic.ts
+++ b/frontend/src/scenes/experiments/experimentMetricsLogic.ts
@@ -117,16 +117,13 @@ const metricErrorEntries = (recalculation: RecalculationPayload): Record<string,
(recalculation.metric_errors as Record<string, MetricErrorEntry> | null) ?? {}
/**
- * Metric uuids a run resolved: a result, or a failure a new run cannot fix. A retriable failure reads as a
- * gap and heals; an entry without the flag predates it and counts as covered, so an old run never heals in a loop.
+ * Metric uuids the run has an entry for: a result or an error. A failure, retriable or not, counts as
+ * covered, because only a manual retry or a manual recalculation runs a failed metric again.
*/
-const coveredMetricUuids = (recalculation: RecalculationPayload): string[] => {
- const errors = metricErrorEntries(recalculation)
- const retriable = new Set(Object.keys(errors).filter((uuid) => errors[uuid]?.retriable === true))
- return [...(recalculation.results ?? []).map(({ metric_uuid }) => metric_uuid), ...Object.keys(errors)].filter(
- (uuid) => !retriable.has(uuid)
- )
-}
+const coveredMetricUuids = (recalculation: RecalculationPayload): string[] => [
+ ...(recalculation.results ?? []).map(({ metric_uuid }) => metric_uuid),
+ ...Object.keys(metricErrorEntries(recalculation)),
+]
/**
* The trigger that fills a gap, by where the latest payload came from. A real run is healed in place:
@@ -148,13 +145,10 @@ const recalculationHasGap = (experiment: Experiment, recalculation: Recalculatio
const coveredUuids = new Set(coveredMetricUuids(recalculation))
- return (
- recalculation.completed_metrics + recalculation.failed_metrics < recalculation.total_metrics ||
- currentMetricUuids(experiment).some((uuid) => !coveredUuids.has(uuid))
- )
+ return currentMetricUuids(experiment).some((uuid) => !coveredUuids.has(uuid))
}
-type MetricErrorState = { detail: string } | null
+type MetricErrorState = { detail: string; retriable: boolean } | null
type ResolveByUuid<T> = (uuid: string) => T
/**
@@ -198,7 +192,7 @@ const resolveResultByUuid = (
}
/**
- * Resolver: a metric's failure as `{ detail }` (the MetricErrorState shape), or null. `metric_errors`
+ * Resolver: a metric's failure as `{ detail, retriable }` (the MetricErrorState shape), or null. `metric_errors`
* wins (it covers FAILED rows AND discovery-step failures absent from `results`), falling back to a
* failed row's error_message.
*/
@@ -211,7 +205,7 @@ const resolveErrorByUuid = (recalculation: RecalculationPayload): ResolveByUuid<
)
return (uuid) => {
const message = metricErrors[uuid]?.message ?? failedResultMessageByUuid.get(uuid)
- return message ? { detail: message } : null
+ return message ? { detail: message, retriable: metricErrors[uuid]?.retriable === true } : null
}
}