: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(experiments): prepare calculation-key compatibility (#127788)
Andersff898eef9546
diff --git a/posthog/temporal/experiments/README.md b/posthog/temporal/experiments/README.mdindex 59e12fc40357..d265bd3f37e8 100644--- a/posthog/temporal/experiments/README.md+++ b/posthog/temporal/experiments/README.md@@ -40,6 +40,10 @@ The write looks the row up on `(experiment, metric_uuid, query_to)` and stores tA row that already holds that window under another fingerprint, such as a recalculation result at the same window, is updated in place instead of failing on the unique constraint.A failed write is the exception: it leaves a completed row under another fingerprint in place, so a transient failure cannot replace a recalculation result.+The significance notification compares a new daily result with the metric's newest earlier completed point that is compatible with it: a point under either key version of the metric's config, or whose display key is the version 1 key.+So neither a key version change nor a settings change that version 1 does not cover sends a repeated notification.+Without any earlier point, every significant variant counts as newly significant, as on the first point of a new experiment.+These workflows used to publish a `timeseries_sync` recalculation row per experiment as well.The scheduled recalculation workflow owns that job now, so the publish pass is gated out behind`experiment-drop-timeseries-publish-2026-10`.diff --git a/posthog/temporal/experiments/utils.py b/posthog/temporal/experiments/utils.pyindex f20421fa1b7e..fb7e7c35da12 100644--- a/posthog/temporal/experiments/utils.py+++ b/posthog/temporal/experiments/utils.py@@ -112,6 +112,8 @@ def check_significance_transition(fingerprint: str,result_dict: dict,query_to_utc: datetime,+ *,+ display_key: str | None = None,) -> None:try:new_significant_keys = _get_significant_variant_keys(result_dict)@@ -124,6 +126,7 @@ def check_significance_transition(metric_uuid=metric_uuid,calculation_key=fingerprint,before=query_to_utc,+ display_key=display_key,)prev_significant_keys = _get_significant_variant_keys(previous_result) if previous_result else set()diff --git a/products/demo/backend/logic/products/hedgebox/matrix.py b/products/demo/backend/logic/products/hedgebox/matrix.pyindex 1150ae6b963c..7b67269c516e 100644--- a/products/demo/backend/logic/products/hedgebox/matrix.py+++ b/products/demo/backend/logic/products/hedgebox/matrix.py@@ -1785,7 +1785,7 @@ class HedgeboxMatrix(Matrix):)# Inline metrics need stored fingerprints (normally stamped by the experiment service on- # create/launch) — the timeseries endpoint looks results up by them.+ # create/launch), and the backfill below files the seeded days under the same key.calculation_keys = metric_calculation_keys(new_experiment.id, team_id=team.id).inlinefor metric_dict in metrics:metric_dict["fingerprint"] = calculation_keys[metric_dict["uuid"]]diff --git a/products/experiments/backend/experiment_service.py b/products/experiments/backend/experiment_service.pyindex 965749d6244c..15b22f4c8d56 100644--- a/products/experiments/backend/experiment_service.py+++ b/products/experiments/backend/experiment_service.py@@ -92,10 +92,11 @@ from products.experiments.backend.logic.timeseries_text import ExperimentTimeserfrom products.experiments.backend.metric_calculation.config import (ExperimentCalculationSettings,build_primary_calculation_configs,+ get_metric_calculation_config,saved_metric_calculation_keys,stamp_calculation_keys,)-from products.experiments.backend.metric_calculation.results import MetricResultStore+from products.experiments.backend.metric_calculation.results import DailyTimeseries, MetricResultStorefrom products.experiments.backend.metric_resolution import MetricRolefrom products.experiments.backend.metric_utils import filter_metric_group_ids_by_eventfrom products.experiments.backend.metric_validation import (@@ -4586,14 +4587,13 @@ class ExperimentService:# Timeseries# ------------------------------------------------------------------- def get_timeseries_results(- self,- experiment: Experiment,- *,- metric_uuid: str,- fingerprint: str,- ) -> dict:- """Retrieve timeseries results for an experiment-metric combination."""+ def get_timeseries_results(self, experiment: Experiment, *, metric_uuid: str) -> dict:+ """Retrieve timeseries results for an experiment-metric combination.++ The calculation key comes from the metric's current calculation config, never from the client, so the chart+ shows the rows of the current settings. `MetricResultStore.timeseries` says which row a day without such a row+ shows.+ """project_tz = ZoneInfo(experiment.team.timezone) if experiment.team.timezone else ZoneInfo("UTC")if not experiment.start_date:@@ -4612,8 +4612,12 @@ class ExperimentService:for experiment_date in experiment_dates:timeseries[experiment_date.isoformat()] = None- stored = MetricResultStore(experiment_id=experiment.id).timeseries(- metric_uuid, fingerprint, timezone=project_tz+ # A metric that no longer resolves on the experiment has no key, so it reads as a series with no rows.+ calculation_config = get_metric_calculation_config(experiment, metric_uuid)+ stored = (+ MetricResultStore(experiment_id=experiment.id).timeseries(calculation_config, timezone=project_tz)+ if calculation_config is not None+ else DailyTimeseries(by_day={}, earliest=None, latest=None))completed_count = 0@@ -4656,14 +4660,18 @@ class ExperimentService:else:overall_status = "partial"- active_recalculation = ExperimentTimeseriesRecalculation.objects.filter(- experiment=experiment,- fingerprint=fingerprint,- status__in=[- ExperimentTimeseriesRecalculation.Status.PENDING,- ExperimentTimeseriesRecalculation.Status.IN_PROGRESS,- ],- ).first()+ active_recalculation = (+ ExperimentTimeseriesRecalculation.objects.filter(+ experiment=experiment,+ fingerprint=calculation_config.calculation_key(),+ status__in=[+ ExperimentTimeseriesRecalculation.Status.PENDING,+ ExperimentTimeseriesRecalculation.Status.IN_PROGRESS,+ ],+ ).first()+ if calculation_config is not None+ else None+ )response = {"experiment_id": experiment.id,diff --git a/products/experiments/backend/facade/enums.py b/products/experiments/backend/facade/enums.pyindex 35083590d065..674b0e404b04 100644--- a/products/experiments/backend/facade/enums.py+++ b/products/experiments/backend/facade/enums.py@@ -3,6 +3,8 @@from decimal import Decimalfrom enum import StrEnum+from posthog.enums import LabeledStrEnum+MAX_RECALCULATION_TIMES = 2# frontend/src/scenes/settings/environment/ExperimentRecalculationTime.tsx repeats this value, so change both together.MIN_RECALCULATION_GAP_HOURS = 6@@ -34,3 +36,9 @@ class PrecomputeSkipReason(StrEnum):COHORT_NOT_CALCULATED = "cohort_not_calculated"DATA_WAREHOUSE = "data_warehouse"GROUP_AGGREGATION = "group_aggregation"+++class ExperimentResultConfigurationStatus(LabeledStrEnum):+ CURRENT = "current", "Current settings"+ PREVIOUS = "previous", "Earlier settings"+ UNKNOWN = "unknown", "Settings not recorded"diff --git a/products/experiments/backend/metric_calculation/config.py b/products/experiments/backend/metric_calculation/config.pyindex cf0e061124e0..fd23875b5924 100644--- a/products/experiments/backend/metric_calculation/config.py+++ b/products/experiments/backend/metric_calculation/config.py@@ -7,23 +7,37 @@ Building a config does not schedule or run a calculation.A config does not pin the events and warehouse rows, the definitions a metric references (actions,cohorts, warehouse tables, property types), the team's HogQL modifiers, or the clock that the maturity-filters read. The same config can therefore give a different result later.--Key version 1 hashes only part of a config: the metric, the start date, the stats method, the exposure-criteria as stored, maturity and the excluded variants. The other fields do not change it.+filters read. The same config can therefore give a different result later. Equality and the key also leave out+which event the `experiment-exposure-event` flag picks as the default exposure event, so a result stays+reusable for its window when that flag changes for the team.++The settings factories reset a value that the calculation ignores to its default: the CUPED lookback without+CUPED, and the sequential tuning parameter without sequential testing. So such a value does not split equal+configs.++Two key versions exist. Key version 2 hashes the metric and an explicit field list of each setting that equality+compares, with numbers normalized. So equal configs have equal keys, and so do settings that differ only by float+noise. Key version 1 (`legacy_key`) hashes only part of a config: the metric, the start date, the stats method,+the exposure criteria as stored, maturity and the excluded variants. `calculation_key` returns the key of the+settings' `key_version`, which `resolve_calculation_key_version` decides. Readers understand both versions, and+results also store the version 1 key as their display key. Stored results keep the key they were written with."""import jsonimport hashlib-from collections.abc import Collection+import dataclasses+from collections.abc import Collection, Mappingfrom copy import deepcopyfrom dataclasses import fieldfrom datetime import datetime+from enum import Enumfrom typing import TYPE_CHECKING, Anyfrom zoneinfo import ZoneInfoimport pydantic+from posthog.schema import ActionsNode, ExperimentEventExposureConfig, ExperimentWarehouseNativeExposureConfig+from posthog.dataclasses import frozenfrom products.experiments.backend.hogql_queries import get_baseline_variant_key@@ -32,6 +46,13 @@ from products.experiments.backend.hogql_queries.experiment_query_builder importExposureQueryParams,get_exposure_config_params_for_builder,)+from products.experiments.backend.hogql_queries.exposure_query_logic import (+ DEFAULT_EXPOSURE_EVENT,+ get_multiple_variant_handling_from_experiment,+ has_activation_config,+ normalize_to_exposure_criteria,+ resolve_filter_test_accounts,+)from products.experiments.backend.hogql_queries.utils import (BayesianSettings,FrequentistSettings,@@ -43,6 +64,7 @@ from products.experiments.backend.hogql_queries.utils import (from products.experiments.backend.metric_resolution import (MetricRole,apply_saved_metric_overrides,+ get_effective_experiment_metrics,get_effective_inline_metrics,get_metrics_for_calculation,saved_metric_link_role,@@ -51,6 +73,7 @@ from products.experiments.backend.metric_resolution import (from products.experiments.backend.metric_utils import get_default_metric_titlefrom products.experiments.backend.models.experiment import Experiment, metric_display_rankfrom products.experiments.backend.models.team_experiments_config import TeamExperimentsConfig+from products.experiments.stats.frequentist.method import DEFAULT_SEQUENTIAL_TUNING_PARAMETERif TYPE_CHECKING:from posthog.models.team.team import Team@@ -59,6 +82,25 @@ if TYPE_CHECKING:SPEC_VERSION = 1+# The version of the calculation key, hashed into the key itself. A change that makes the engine compute a+# different result for an existing config bumps it, so that results computed before the change stop counting+# for reuse.+CALCULATION_KEY_VERSION = 2+KEY_VERSIONS = (1, CALCULATION_KEY_VERSION)++# Significant digits that a float keeps in the calculation key. Stored settings carry float noise, for+# example `1 - 0.95` is stored as 0.050000000000000044, and the noise must not split equal settings into+# two keys.+_KEY_FLOAT_DIGITS = 12++# Stands for the default exposure event in the key. The `experiment-exposure-event` flag picks that event, and each+# process evaluates the flag locally (`resolve_default_exposure_event`). A key that hashed the picked event would+# change at each rollout step of the flag, and two processes that evaluate the flag differently would file and look+# up the results of one experiment under two keys. A stored result therefore stays reusable for its window when the+# flag changes. To make results computed with the old default event stop counting for reuse when the new event+# ships to every team, treat the switch as an engine change and bump CALCULATION_KEY_VERSION.+_DEFAULT_EXPOSURE_EVENT_IN_KEY = "$default_exposure_event"+# Fields of a stored metric definition that do not describe what the metric computes: its identity, its# display name, the stored key itself, and an experiment-level setting that some stored definitions carry._NON_ANALYTICAL_METRIC_FIELDS = frozenset({"uuid", "name", "kind", "fingerprint", "only_count_matured_users"})@@ -93,6 +135,30 @@ def _analytical_definition(definition: dict[str, Any]) -> dict[str, Any]:return metric+def _canonical(value: Any) -> Any:+ """`value` as JSON data that is equal for equal settings: floats rounded to _KEY_FLOAT_DIGITS significant+ digits, a float with an integral value as an int, enums as their values, and datetimes as UTC ISO strings, so+ the same instant gives the same key in every timezone.++ It refuses models and dataclasses. A dump of a whole model hashes every field the model has, so a schema change+ that adds a defaulted field would change every key. The key reads such inputs through explicit field lists.+ """+ if value is None or isinstance(value, bool | int | str):+ return value+ if isinstance(value, float):+ rounded = float(f"{value:.{_KEY_FLOAT_DIGITS}g}")+ return int(rounded) if rounded.is_integer() else rounded+ if isinstance(value, Enum):+ return _canonical(value.value)+ if isinstance(value, datetime):+ return value.astimezone(ZoneInfo("UTC")).isoformat()+ if isinstance(value, Mapping):+ return {str(key): _canonical(item) for key, item in value.items()}+ if isinstance(value, list | tuple):+ return [_canonical(item) for item in value]+ raise TypeError(f"Cannot hash a {type(value).__name__} into a calculation key")++def _variant_keys(feature_flag: "FeatureFlag", excluded_variants: tuple[str, ...]) -> tuple[str, ...]:keys = (variant.get("key") for variant in feature_flag.variants if isinstance(variant, dict))return tuple(key for key in keys if isinstance(key, str) and key not in excluded_variants)@@ -118,6 +184,78 @@ def _resolve_exposure(return None+def _exposure_entity(+ config: ExperimentEventExposureConfig | ActionsNode | ExperimentWarehouseNativeExposureConfig | None,+ stored_config: Any,+ *,+ default_event_resolves: bool,+) -> dict[str, Any]:+ """The event or action of an exposure or activation config, and its property filters as stored. The stored+ filters stay the same when a schema change adds a defaulted field to a filter model, so the key does too.+ A warehouse-native exposure has no event and no filters, so its source and its SQL identify it."""+ if isinstance(config, ExperimentWarehouseNativeExposureConfig):+ return {"connection_id": config.connection_id, "query": config.query}+ stored_properties = stored_config.get("properties") if isinstance(stored_config, dict) else None+ properties = deepcopy(list(stored_properties or []))+ if isinstance(config, ActionsNode):+ return {"action_id": config.id, "properties": properties}+ if config is None or (default_event_resolves and config.event == DEFAULT_EXPOSURE_EVENT):+ return {"event": _DEFAULT_EXPOSURE_EVENT_IN_KEY, "properties": properties}+ return {"event": config.event, "properties": properties}+++def normalize_exposure(exposure_criteria: dict[str, Any] | None) -> dict[str, Any]:+ """The stored exposure criteria with the product defaults applied, as the calculation key reads them.++ It follows `get_exposure_config_params_for_builder`, except that the default exposure event stays unresolved+ (`_DEFAULT_EXPOSURE_EVENT_IN_KEY`). Activation applies only with the default exposure, and an activation config+ names its event literally, as in the query builder.+ """+ try:+ criteria = normalize_to_exposure_criteria(exposure_criteria)+ except pydantic.ValidationError:+ # The query runner rejects such criteria. Their stored form keeps them apart from every valid configuration.+ return {"unparsed": deepcopy(exposure_criteria)}+ stored = exposure_criteria if isinstance(exposure_criteria, dict) else {}+ exposure_config = criteria.exposure_config if criteria is not None else None+ normalized: dict[str, Any] = {+ "exposure": _exposure_entity(exposure_config, stored.get("exposure_config"), default_event_resolves=True),+ "multiple_variant_handling": get_multiple_variant_handling_from_experiment(criteria).value,+ "filter_test_accounts": resolve_filter_test_accounts(criteria),+ }+ if criteria is not None and has_activation_config(criteria):+ normalized["activation"] = _exposure_entity(+ criteria.activation_config, stored.get("activation_config"), default_event_resolves=False+ )+ return normalized+++def _statistics_key_input(stats: BayesianSettings | FrequentistSettings) -> dict[str, Any]:+ if isinstance(stats, FrequentistSettings):+ return {+ "method": "frequentist",+ "alpha": _canonical(stats.alpha),+ "difference_type": stats.difference_type.value,+ "sequential_testing_enabled": stats.sequential_testing_enabled,+ "sequential_tuning_parameter": _canonical(stats.sequential_tuning_parameter),+ }+ return {+ "method": "bayesian",+ "ci_level": _canonical(stats.ci_level),+ "difference_type": stats.difference_type.value,+ }+++def _cuped_key_input(cuped: CupedQueryConfig) -> dict[str, Any]:+ return {"enabled": cuped.enabled, "lookback_days": cuped.lookback_days}+++def resolve_calculation_key_version(team: "Team") -> int:+ """The calculation key version that a new operation of this team uses. It is the only activation decision:+ queued work carries the version in its key, so a worker never calls this."""+ return 1++@frozenclass ExperimentCalculationSettings:"""What every metric calculation of an experiment reads outside the metric definition, with the@@ -132,19 +270,29 @@ class ExperimentCalculationSettings:variants: tuple[str, ...]baseline: strexcluded_variants: tuple[str, ...]- # None when the stored exposure criteria do not parse. The query runner rejects such an experiment.- exposure: ExposureQueryParams | None+ # What the query runner resolves from the stored exposure criteria. The `experiment-exposure-event` flag picks its+ # default exposure event, so equality and the key read `normalized_exposure` instead. None when the stored+ # criteria do not parse. The query runner rejects such an experiment.+ exposure: ExposureQueryParams | None = field(compare=False)+ # The stored exposure criteria as `normalize_exposure` returns them.+ normalized_exposure: dict[str, Any]# The team's test account filters when the exposure filters test accounts, else empty.test_account_filters: tuple[dict[str, Any], ...]stats: BayesianSettings | FrequentistSettings# The CUPED setting of the experiment. The query builders apply it only to metrics that support CUPED.cuped: CupedQueryConfigonly_count_matured_users: bool- # Retention metrics bucket their windows into days or hours in this time zone.+ # Date property filters, the CUPED lookback and retention windows read this time zone.timezone: str# The exposure criteria as stored. Key version 1 hashes this form, so settings that resolve to the# same `exposure` can still have different keys when their stored JSON differs.stored_exposure_criteria: dict[str, Any] | None = field(compare=False)+ # The version of `MetricCalculationConfig.calculation_key`. Workers take it from the key of their queued work, so+ # one operation never mixes versions.+ key_version: int = 1++ def with_key_version(self, key_version: int) -> "ExperimentCalculationSettings":+ return dataclasses.replace(self, key_version=key_version)@propertydef stats_method(self) -> StatsMethod:@@ -162,12 +310,14 @@ class ExperimentCalculationSettings:only_count_matured_users: bool | None,excluded_variants: list[str] | None,team_config: TeamExperimentsConfig | None = None,+ key_version: int | None = None,) -> "ExperimentCalculationSettings":"""Settings from explicit values, for a caller that holds a configuration that is not saved yet.Stored configuration never makes this raise, because the experiment read and write paths build- settings to stamp the metric keys. A configuration that the query runner rejects still gives settings.- Without `team_config`, this reads the team's experiment settings with one query.+ settings to stamp the metric keys. A configuration the query runner rejects still resolves.+ Without `team_config`, this reads the team's experiment settings with one query. Without `key_version`,+ `resolve_calculation_key_version` decides it."""config = team_config or team_experiments_configs([team.id])[team.id]stats_config = stats_config if isinstance(stats_config, dict) else None@@ -182,6 +332,10 @@ class ExperimentCalculationSettings:team_default_sequential_testing_enabled=config.default_sequential_testing_enabled,team_default_sequential_tuning_parameter=config.default_sequential_tuning_parameter,)+ # Only the sequential test reads the tuning parameter. Without sequential testing, the stored value+ # computes the same thing as the default, as a CUPED lookback does without CUPED.+ if not stats.sequential_testing_enabled:+ stats = dataclasses.replace(stats, sequential_tuning_parameter=DEFAULT_SEQUENTIAL_TUNING_PARAMETER)else:stats = resolve_bayesian_settings(stats_config)return cls(@@ -193,6 +347,7 @@ class ExperimentCalculationSettings:baseline=get_baseline_variant_key(stats_config, list(variants)),excluded_variants=excluded,exposure=exposure,+ normalized_exposure=normalize_exposure(exposure_criteria),test_account_filters=(tuple(deepcopy(test_account_filters))if exposure is not None and exposure.filter_test_accounts and isinstance(test_account_filters, list)@@ -207,11 +362,16 @@ class ExperimentCalculationSettings:only_count_matured_users=bool(only_count_matured_users),timezone=team.timezone,stored_exposure_criteria=deepcopy(exposure_criteria),+ key_version=key_version if key_version is not None else resolve_calculation_key_version(team),)@classmethoddef from_experiment(- cls, experiment: Experiment, *, team_config: TeamExperimentsConfig | None = None+ cls,+ experiment: Experiment,+ *,+ team_config: TeamExperimentsConfig | None = None,+ key_version: int | None = None,) -> "ExperimentCalculationSettings":"""Settings from the current fields of the experiment, which does not need to be saved. A caller thatbuilds the configs of several experiments of one team passes the team's config to read it once."""@@ -224,6 +384,7 @@ class ExperimentCalculationSettings:only_count_matured_users=experiment.only_count_matured_users,excluded_variants=experiment.excluded_variants,team_config=team_config,+ key_version=key_version,)def build_metric_config(@@ -262,13 +423,57 @@ class MetricCalculationConfig:settings: ExperimentCalculationSettingsdef calculation_key(self) -> str:- """Key version 1, a SHA-256 hex digest.-- Result rows in ExperimentMetricResult and the stored `fingerprint` of each metric hold this key.- A change to the hashed payload makes every stored result unreachable, so a new analytical input- needs a new key version, not an edit here.+ """The key that files and finds the results of this config, a SHA-256 hex digest, in the version of+ `settings.key_version`. Result rows in ExperimentMetricResult and the stored `fingerprint` of each inline+ metric hold it."""+ return self.key_for_version(self.settings.key_version)++ def key_for_version(self, key_version: int) -> str:+ if key_version == 1:+ return self.legacy_key()+ if key_version == CALCULATION_KEY_VERSION:+ return self._key_v2()+ raise ValueError(f"Unknown calculation key version {key_version}")++ def key_version_of(self, key: str) -> int | None:+ """The key version in which this config gives `key`, or None when no version does, for example because the+ configuration changed after the key was computed."""+ return next((version for version in reversed(KEY_VERSIONS) if self.key_for_version(version) == key), None)++ def with_key_version(self, key_version: int) -> "MetricCalculationConfig":+ return dataclasses.replace(self, settings=self.settings.with_key_version(key_version))++ def _key_v2(self) -> str:+ """Key version 2. It hashes the metric and each settings field that equality compares, except the team and+ the key version, together with CALCULATION_KEY_VERSION. Nested settings enter through explicit field lists.+ The time zone enters for every metric type, because date property filters, the CUPED lookback and retention+ windows all read it. A change to the hashed payload makes every stored result unreachable for reuse, so it+ goes with a bump of CALCULATION_KEY_VERSION."""settings = self.settings+ payload: dict[str, Any] = {+ "key_version": CALCULATION_KEY_VERSION,+ "metric": _canonical(self.metric),+ "start_date": _canonical(settings.start_date),+ "feature_flag_key": settings.feature_flag_key,+ "aggregation_group_type_index": settings.aggregation_group_type_index,+ "variants": list(settings.variants),+ "baseline": settings.baseline,+ "excluded_variants": list(settings.excluded_variants),+ "exposure": _canonical(settings.normalized_exposure),+ "test_account_filters": _canonical(settings.test_account_filters),+ "stats": _statistics_key_input(settings.stats),+ "cuped": _cuped_key_input(settings.cuped),+ "only_count_matured_users": settings.only_count_matured_users,+ "timezone": settings.timezone,+ }+ return _sha256(payload)++ def legacy_key(self) -> str:+ """Key version 1, byte-identical to the key that rows written before key version 2 carry. Those rows keep it.+ It hashes only inputs that the experiment owns, so it also serves as the display key that keeps chart history+ across a team setting change. A change to its payload makes every historical result unreachable."""+ settings = self.settingsstart_date = settings.start_datepayload: dict[str, Any] = {"metric": self.metric,@@ -282,8 +487,12 @@ class MetricCalculationConfig:payload["only_count_matured_users"] = Trueif settings.excluded_variants:payload["excluded_variants"] = list(settings.excluded_variants)- encoded = json.dumps(payload, sort_keys=True, separators=(",", ":"))- return hashlib.sha256(encoded.encode("utf-8")).hexdigest()+ return _sha256(payload)+++def _sha256(payload: dict[str, Any]) -> str:+ encoded = json.dumps(payload, sort_keys=True, separators=(",", ":"))+ return hashlib.sha256(encoded.encode("utf-8")).hexdigest()def build_calculation_configs(@@ -326,6 +535,30 @@ def get_metric_calculation_config(experiment: Experiment, metric_uuid: str) -> Mreturn settings.build_metric_config(metric_id=metric.uuid, role=metric.role, definition=metric.definition)+def find_calculation_config_by_key(+ experiment: Experiment, metric_uuid: str, calculation_key: str+) -> MetricCalculationConfig | None:+ """The calculation config of the experiment's metric with this uuid whose calculation key is `calculation_key`+ in some key version, under the current configuration of the experiment, at that key version.++ Unlike `get_metric_calculation_config`, this keys an inline and a saved metric that share a uuid each by its+ own definition, the same way `metric_calculation_keys` does. None when no metric with this uuid has this key,+ for example because the configuration changed after the caller computed the key.+ """+ candidates = [metric for metric in get_effective_experiment_metrics(experiment) if metric.uuid == metric_uuid]+ if not candidates:+ return None+ settings = ExperimentCalculationSettings.from_experiment(experiment)+ for metric in candidates:+ calculation_config = settings.build_metric_config(+ metric_id=metric.uuid, role=metric.role, definition=metric.definition+ )+ key_version = calculation_config.key_version_of(calculation_key)+ if key_version is not None:+ return calculation_config.with_key_version(key_version)+ return None++def calculation_metric_name(experiment: Experiment, calculation_config: MetricCalculationConfig) -> str:definition = calculation_config.definition# An inline metric wins over a saved metric with the same uuid, as in `get_metrics_for_calculation`.@@ -341,8 +574,8 @@ def calculation_metric_name(experiment: Experiment, calculation_config: MetricCadef stamp_calculation_keys(metrics: list[dict[str, Any]], role: MetricRole, settings: ExperimentCalculationSettings) -> list[dict[str, Any]]:- """Copies of stored inline metric definitions, each with its calculation key in `fingerprint`. The- chart reads the daily results of an inline metric by that stored key."""+ """Copies of stored inline metric definitions, each with its calculation key in `fingerprint`. Readers+ derive the key from the config, so the stored value only describes the metric to API clients."""stamped = []for metric in metrics:metric_copy = deepcopy(metric)diff --git a/products/experiments/backend/metric_calculation/results.py b/products/experiments/backend/metric_calculation/results.pyindex d5a8a7d4c8e9..38aaea83fb1d 100644--- a/products/experiments/backend/metric_calculation/results.py+++ b/products/experiments/backend/metric_calculation/results.py@@ -10,6 +10,18 @@ hide two storage conventions from the callers:The calculation key includes the start date, so no reader needs `query_from` to tell a relaunch apart.+A row carries the key of one key version (`MetricCalculationConfig.key_for_version`). Readers find the rows of the+current config under every key version, so rows that another key version wrote stay visible. The newest compatible+row wins, and rows under the current settings win only a tie. Calculation reuse requires the active key. Rows are never relabeled with+current settings: a version 1 key omits analytical inputs, so the settings of such a row are unknown.++A write can also store its config's version 1 key in `display_key`, salted like the fingerprint on a run row.+The chart, current outcome and significance baseline use it to preserve history across project setting changes.+Ended experiments also use it for run reads and cold-start display, so opening the page does not overwrite the+result after a project setting changes. Running experiments leave such run results uncovered and recompute.+Display readers know whether each result used current, previous, or unknown settings; display compatibility+never makes a result reusable for calculation.+Several rows can share `(experiment, metric_uuid, query_to)` once the unique constraint on that key goes. Everyquery that can meet such rows returns the one with the newest `completed_at`, then the highest id. It never looks arow up with `.get()` on that key. While the constraint holds, no two rows tie, so this order changes no result.@@ -24,12 +36,13 @@ Every query filters on the experiment first, so the `(experiment, metric_uuid, qimport hashlibfrom collections.abc import Iterable, Mapping+from dataclasses import fieldfrom datetime import date, datetime, timedeltafrom typing import Anyfrom zoneinfo import ZoneInfofrom django.db import transaction-from django.db.models import F, Q, QuerySet+from django.db.models import BooleanField, Case, F, Q, QuerySet, Value, Whenfrom django.db.models.fields.json import KT, KeyTransformfrom django.db.models.functions import Leastfrom django.utils import timezone as django_timezone@@ -40,9 +53,15 @@ from posthog.schema import ExperimentQueryResponsefrom posthog.dataclasses import frozen-from products.experiments.backend.facade.enums import ExposuresPath+from products.experiments.backend.facade.enums import ExperimentResultConfigurationStatus, ExposuresPathfrom products.experiments.backend.logic.result_stats import MetricResultStats, parse_stored_response-from products.experiments.backend.metric_calculation.config import MetricCalculationConfig, build_calculation_configs+from products.experiments.backend.metric_calculation.config import (+ CALCULATION_KEY_VERSION,+ KEY_VERSIONS,+ MetricCalculationConfig,+ build_calculation_configs,+ find_calculation_config_by_key,+)from products.experiments.backend.models.experiment import (Experiment,ExperimentMetricResult,@@ -77,18 +96,62 @@ def _recalc_fingerprint(config_fingerprint: str) -> str:return hashlib.sha256(f"{config_fingerprint}{_RECALCULATION_SALT}".encode()).hexdigest()-def _newest_write_first(rows: QuerySet[ExperimentMetricResult], *window_order: str) -> QuerySet[ExperimentMetricResult]:+def _newest_write_first(+ rows: QuerySet[ExperimentMetricResult], *window_order: str | Case+) -> QuerySet[ExperimentMetricResult]:"""Order `rows` by `window_order`, then put the newest write first among rows that tie on it."""return rows.order_by(*window_order, F("completed_at").desc(nulls_last=True), F("id").desc())+def _current_key_first(under_current_key: Q) -> Case:+ """An ascending order on this puts the rows that `under_current_key` matches first."""+ return Case(When(under_current_key, then=Value(False)), default=Value(True), output_field=BooleanField())+++def _keys_of(config: MetricCalculationConfig, *, salted: bool = False) -> list[str]:+ """The keys of `config` in every key version, the active key first."""+ versions = [config.settings.key_version, *(v for v in KEY_VERSIONS if v != config.settings.key_version)]+ keys = [config.key_for_version(version) for version in versions]+ return [_recalc_fingerprint(key) for key in keys] if salted else keys+++def _current_settings_keys(config: MetricCalculationConfig) -> list[str]:+ """The keys, bare and salted, of the rows that used the current settings of `config`. A version 2 key hashes every+ setting. A version 1 key omits settings, so its rows count only while version 1 is active."""+ versions = {CALCULATION_KEY_VERSION, config.settings.key_version}+ keys = [config.key_for_version(version) for version in versions]+ return [*keys, *(_recalc_fingerprint(key) for key in keys)]+++def _under_current_settings(config: MetricCalculationConfig) -> Q:+ """The rows of `config` that used its current settings. The readers of the newest result order by the window+ first and by this only to break a tie, so the newest compatible row wins. So after the active key version+ changes, neither a newer row under the other version nor a newer row under earlier settings hides behind an older+ row under current settings."""+ return Q(fingerprint__in=_current_settings_keys(config))+++def result_configuration_status(fingerprint: str | None, config: MetricCalculationConfig) -> str:+ """The provenance of a row of `config` that a reader found, relative to the active key version.++ A row that only its display key matched comes from other settings: writers store the display key with the+ version 2 key, because under version 1 the display key equals the fingerprint.+ """+ if fingerprint in _current_settings_keys(config):+ return ExperimentResultConfigurationStatus.CURRENT+ if fingerprint in _keys_of(config) + _keys_of(config, salted=True):+ return ExperimentResultConfigurationStatus.UNKNOWN+ return ExperimentResultConfigurationStatus.PREVIOUS++@frozenclass DailyTimeseries:- """The stored results of one metric under one calculation key, one row per day."""+ """The stored results of one metric, one row per day."""by_day: dict[date, ExperimentMetricResult]earliest: ExperimentMetricResult | Nonelatest: ExperimentMetricResult | None+ configuration_by_day: dict[date, str] = field(default_factory=dict)@frozen@@ -102,6 +165,7 @@ class ResultSummary:baseline_samples: str | Nonebaseline_sum: str | Nonevariant_results: tuple[dict[str, Any], ...]+ configuration_status: str = ExperimentResultConfigurationStatus.UNKNOWN@frozen@@ -110,36 +174,61 @@ class MetricResultStore:experiment_id: int- def for_run(self, run: ExperimentMetricsRecalculation, *, with_result: bool = True) -> list[ExperimentMetricResult]:+ def for_run(+ self,+ run: ExperimentMetricsRecalculation,+ *,+ configs: list[MetricCalculationConfig] | None = None,+ with_result: bool = True,+ ) -> list[ExperimentMetricResult]:"""The rows of a recalculation run, one per metric uuid, ordered by metric uuid. With `with_result=False` therows don't load `result`, which for a funnel holds the sessions of each step, and reading it queries again.A run has no link to its rows. The rows are found by the salted keys of the run's metrics under thecurrent configuration of the experiment, at the run's query_to. A metric that no longer resolves on the- experiment has no key, so its row is not found. A configuration change after the run changes the keys, so- the run's rows are not found until the configuration changes back.+ experiment has no key, so its row is not found. Ended experiments also retain completed rows with a+ compatible display key; running experiments expose a gap after the calculation key changes.++ A row under the salted key of another key version stands in for a metric that has no row under its active key+ at the window. So a run that another key version wrote still reads complete."""if run.query_to is None:return []calculation_configs = {calculation_config.metric_id: calculation_config- for calculation_config in build_calculation_configs(run.experiment)+ for calculation_config in (configs if configs is not None else build_calculation_configs(run.experiment))}- fingerprints = [- _recalc_fingerprint(calculation_configs[metric_uuid].calculation_key())- for metric_uuid in run.metric_uuids or []+ run_configs = [+ calculation_configs[metric_uuid]+ for metric_uuid in dict.fromkeys(run.metric_uuids or [])if metric_uuid in calculation_configs]- if not fingerprints:+ if not run_configs:return []+ filed_under_run_keys = Q()+ under_current_settings = Q()+ for calculation_config in run_configs:+ run_keys = _keys_of(calculation_config, salted=True)+ filed_under_run_keys |= Q(metric_uuid=calculation_config.metric_id, fingerprint__in=run_keys)+ under_current_settings |= Q(metric_uuid=calculation_config.metric_id) & _under_current_settings(+ calculation_config+ )+ if run.experiment.is_stopped:+ filed_under_run_keys |= Q(+ metric_uuid=calculation_config.metric_id,+ display_key=_recalc_fingerprint(calculation_config.legacy_key()),+ status=_COMPLETED,+ )# The recalc fingerprint is per config, not per run, so each window of a running experiment holds a row under# the same fingerprint. Without the query_to filter a later run would return every earlier window's row.rows = ExperimentMetricResult.objects.filter(- experiment_id=self.experiment_id, fingerprint__in=fingerprints, query_to=run.query_to+ filed_under_run_keys, experiment_id=self.experiment_id, query_to=run.query_to)if not with_result:rows = rows.defer("result")- return list(_newest_write_first(rows, "metric_uuid").distinct("metric_uuid"))+ return list(+ _newest_write_first(rows, "metric_uuid", _current_key_first(under_current_settings)).distinct("metric_uuid")+ )def with_zero_baseline(self, rows: Iterable[ExperimentMetricResult]) -> list[ExperimentMetricResult]:"""Those of these rows whose baseline reads zero on the metric or on its denominator, with `result`, in the order@@ -165,7 +254,7 @@ class MetricResultStore:)def has_completed(self, calculation_config: MetricCalculationConfig, *, window: datetime) -> bool:- """Whether a recalculation already stored a completed result for this calculation config at this window."""+ """Whether this exact calculation config already completed at this window."""return ExperimentMetricResult.objects.filter(experiment_id=self.experiment_id,metric_uuid=calculation_config.metric_id,@@ -175,40 +264,73 @@ class MetricResultStore:).exists()def latest_daily_point(- self, calculation_config: MetricCalculationConfig, *, since: datetime, until: datetime+ self,+ calculation_config: MetricCalculationConfig,+ *,+ since: datetime,+ until: datetime,+ preserve_history: bool = False,) -> ExperimentMetricResult | None:- """The completed daily point of this calculation config with the latest query_to inside [since, until]."""+ """The completed daily point of this calculation config with the latest query_to inside [since, until], under+ either key version. A point under current settings wins a tie on query_to."""+ keys = Q(fingerprint__in=_keys_of(calculation_config))+ if preserve_history:+ keys |= Q(display_key=calculation_config.legacy_key())rows = ExperimentMetricResult.objects.filter(+ keys,experiment_id=self.experiment_id,metric_uuid=calculation_config.metric_id,- fingerprint=calculation_config.calculation_key(),status=_COMPLETED,query_to__gte=since,query_to__lte=until,)- return _newest_write_first(rows, "-query_to").first()+ return _newest_write_first(+ rows, "-query_to", _current_key_first(_under_current_settings(calculation_config))+ ).first()- def timeseries(self, metric_uuid: str, calculation_key: str, *, timezone: ZoneInfo) -> DailyTimeseries:- """The rows of one metric under a calculation key, whatever their status, one per day of `timezone`.+ def timeseries(self, calculation_config: MetricCalculationConfig, *, timezone: ZoneInfo) -> DailyTimeseries:+ """The daily rows of one metric, whatever their status, one per day of `timezone`.Only daily rows carry the bare key, because a recalculation salts it. The latest query_to inside a day- stands for that day.+ stands for that day. A day without a row under the active key shows a row under another key version of the+ config, or whose display key is the version 1 key, if it has one. So the history that another key version+ wrote, and the history from before a team setting changed, still render."""+ current_keys = _current_settings_keys(calculation_config)rows = list(_newest_write_first(ExperimentMetricResult.objects.filter(- experiment_id=self.experiment_id, metric_uuid=metric_uuid, fingerprint=calculation_key+ Q(fingerprint__in=_keys_of(calculation_config)) | Q(display_key=calculation_config.legacy_key()),+ experiment_id=self.experiment_id,+ metric_uuid=calculation_config.metric_id,),+ _current_key_first(_under_current_settings(calculation_config)),"-query_to",))by_day: dict[date, ExperimentMetricResult] = {}+ filled_by_earlier_key: set[date] = set()+ shown: list[ExperimentMetricResult] = []for row in rows:# A daily row's query_to is the exclusive end of its day, so the microsecond before it falls inside# the day the row covers.day = (row.query_to - timedelta(microseconds=1)).astimezone(timezone).date()+ under_earlier_key = row.fingerprint not in current_keys+ # Rows under current settings come first, so another row only fills a day that none of them covers.+ if under_earlier_key and day in by_day and day not in filled_by_earlier_key:+ continueby_day.setdefault(day, row)- return DailyTimeseries(by_day=by_day, earliest=rows[-1] if rows else None, latest=rows[0] if rows else None)+ if under_earlier_key:+ filled_by_earlier_key.add(day)+ shown.append(row)+ return DailyTimeseries(+ by_day=by_day,+ earliest=min(shown, key=lambda row: row.query_to, default=None),+ latest=max(shown, key=lambda row: row.query_to, default=None),+ configuration_by_day={+ day: result_configuration_status(row.fingerprint, calculation_config) for day, row in by_day.items()+ },+ )def current_outcome(self, calculation_config: MetricCalculationConfig) -> ResultSummary | None:"""The current result of the metric that `calculation_config` describes. `current_outcomes` defines the rule."""@@ -233,13 +355,22 @@ class MetricResultStore:return rows.values_list(F("data_until"), flat=True).first()def previous_completed(- self, metric_uuid: str, calculation_key: str, *, before: datetime+ self, metric_uuid: str, fingerprints: Iterable[str], *, display_key: str | None, before: datetime) -> ExperimentMetricResult | None:- """The completed daily point under this key with the latest query_to before `before`."""+ """The completed daily point with the latest query_to before `before` among the points under `fingerprints`+ or whose display key is `display_key`.++ The newest compatible point wins whatever its key. So neither a change of the key version nor a settings change+ that the display key does not cover makes the next point look like a new transition, and after settings+ change and change back, the point under the other settings is the baseline, not an older one.+ """+ under_key = Q(fingerprint__in=list(fingerprints))+ if display_key is not None:+ under_key |= Q(display_key=display_key)rows = ExperimentMetricResult.objects.filter(+ under_key,experiment_id=self.experiment_id,metric_uuid=metric_uuid,- fingerprint=calculation_key,status=_COMPLETED,query_to__lt=before,)@@ -301,13 +432,23 @@ class MetricResultStore:)def record_daily_point(- self, metric_uuid: str, calculation_key: str, *, window: datetime, query_from: datetime, result: dict[str, Any]+ self,+ metric_uuid: str,+ calculation_key: str,+ *,+ display_key: str | None = None,+ window: datetime,+ query_from: datetime,+ result: dict[str, Any],) -> None:- """Store a completed daily point under the bare calculation key. The backfill stores its past days here too."""+ """Store a completed daily point under the bare calculation key. The backfill stores its past days here too.+ `display_key` is the version 1 key of the config that `calculation_key` belongs to. Without it the row has no+ display key, which only a reader under another key version or other settings misses."""self._upsert(metric_uuid,window,fingerprint=calculation_key,+ display_key=display_key,query_from=query_from,status=_COMPLETED,result=result,@@ -317,10 +458,17 @@ class MetricResultStore:)def record_daily_failure(- self, metric_uuid: str, calculation_key: str, *, window: datetime, query_from: datetime, error_message: str+ self,+ metric_uuid: str,+ calculation_key: str,+ *,+ display_key: str | None = None,+ window: datetime,+ query_from: datetime,+ error_message: str,) -> None:"""Store a failed daily point under the bare calculation key, unless a completed row under another- fingerprint holds the window.+ fingerprint holds the window. `display_key` is as for `record_daily_point`.That row is usually a recalculation of a stopped experiment, because a late daily run and the recalculationboth use `end_date`. The run finds its row only by the salted key, and a failure has no result to put in its@@ -331,6 +479,7 @@ class MetricResultStore:metric_uuid,window,fingerprint=calculation_key,+ display_key=display_key,query_from=query_from,status=_FAILED,result=None,@@ -348,12 +497,17 @@ class MetricResultStore:query_from: datetime,completed_at: datetime,) -> None:- """Copy daily points into a timeseries sync run at its window, under the salted keys a run read looks for."""+ """Copy daily points into a timeseries sync run at its window, under the salted keys a run read looks for.++ Each copy retains its point's own key and provenance, including unknown settings on version 1 points.+ """for calculation_config, point in points:+ assert point.fingerprint is not Noneself._upsert(calculation_config.metric_id,window,- fingerprint=_recalc_fingerprint(calculation_config.calculation_key()),+ fingerprint=_recalc_fingerprint(point.fingerprint),+ display_key=_recalc_fingerprint(calculation_config.legacy_key()),query_from=query_from,status=_COMPLETED,result=point.result,@@ -403,6 +557,7 @@ class MetricResultStore:calculation_config.metric_id,window,fingerprint=_recalc_fingerprint(calculation_config.calculation_key()),+ display_key=_recalc_fingerprint(calculation_config.legacy_key()),query_from=query_from,status=status,result=result,@@ -418,6 +573,7 @@ class MetricResultStore:window: datetime,*,fingerprint: str,+ display_key: str | None,query_from: datetime,status: str,result: dict[str, Any] | None,@@ -428,6 +584,7 @@ class MetricResultStore:) -> None:fields: dict[str, Any] = {"fingerprint": fingerprint,+ "display_key": display_key,"query_from": query_from,"status": status,"result": result,@@ -465,18 +622,29 @@ class MetricResultStore:def _current_rows(config_by_experiment: Mapping[int, MetricCalculationConfig]) -> QuerySet[ExperimentMetricResult]:"""For each experiment, the row that `current_outcomes` describes."""filed_under_config = Q()+ under_current_settings = Q()for experiment_id, calculation_config in config_by_experiment.items():- key = calculation_config.calculation_key()- filed_under_config |= Q(- experiment_id=experiment_id,- metric_uuid=calculation_config.metric_id,- fingerprint__in=[key, _recalc_fingerprint(key)],+ legacy_key = calculation_config.legacy_key()+ display_keys = [legacy_key, _recalc_fingerprint(legacy_key)]+ under_current_settings |= Q(experiment_id=experiment_id) & _under_current_settings(calculation_config)+ filed_under_config |= Q(experiment_id=experiment_id, metric_uuid=calculation_config.metric_id) & (+ Q(fingerprint__in=[*_keys_of(calculation_config), *_keys_of(calculation_config, salted=True)])+ | Q(display_key__in=display_keys))return (ExperimentMetricResult.objects.filter(filed_under_config, status=_COMPLETED)# Postgres LEAST skips a NULL, so a row without completed_at ranks by its query_to.- .alias(data_until=Least("query_to", "completed_at"))- .order_by("experiment_id", F("data_until").desc(), F("completed_at").desc(nulls_last=True), "-id")+ .alias(+ data_until=Least("query_to", "completed_at"),+ under_other_settings=_current_key_first(under_current_settings),+ )+ .order_by(+ "experiment_id",+ F("data_until").desc(),+ "under_other_settings",+ F("completed_at").desc(nulls_last=True),+ "-id",+ ).distinct("experiment_id"))@@ -488,9 +656,14 @@ class MetricResultStore:calculation key, by a recalculation run or by the daily workflows. A row's data ends at its query_to, or atits completed_at when that comes first. A backfill stamps the current day with the end of that day, but therow holds data only up to the moment the backfill ran, so a later run of the same day must win over it. A row- under another key was computed from another configuration, for example from the start date before a- relaunch, so it does not count. The end of the data counts, not the newest write, because a backfill writes+ whose key and display key both belong to another configuration, for example to the start date before a+ relaunch, does not count. The end of the data counts, not the newest write, because a backfill writespast windows after the newer ones.++ Rows under the other key version, and rows whose display key is the version 1 key, count too. The row whose+ data ends last wins, and a row under current settings wins a tie. Each experiment's rows rank by that+ experiment's own keys. A calculation key+ hashes no experiment id, so the earlier key of one experiment can be the current key of another."""if not config_by_experiment:return {}@@ -499,6 +672,7 @@ class MetricResultStore:"metric_uuid","completed_at","query_to",+ "fingerprint",baseline_samples=KT("result__baseline__number_of_samples"),baseline_sum=KT("result__baseline__sum"),variant_results=KeyTransform("variant_results", "result"),@@ -509,6 +683,9 @@ class MetricResultStore:summaries[row["experiment_id"]] = ResultSummary(completed_at=row["completed_at"],query_to=row["query_to"],+ configuration_status=result_configuration_status(+ row["fingerprint"], config_by_experiment[row["experiment_id"]]+ ),baseline_samples=row["baseline_samples"],baseline_sum=row["baseline_sum"],variant_results=tuple(variant for variant in variant_results if isinstance(variant, dict)),@@ -517,13 +694,37 @@ class MetricResultStore:def previous_completed_metric_result(- experiment_id: int, *, team_id: int, metric_uuid: str, calculation_key: str, before: datetime+ experiment_id: int,+ *,+ team_id: int,+ metric_uuid: str,+ calculation_key: str,+ before: datetime,+ display_key: str | None = None,) -> dict[str, Any] | None:- """The stored result of the completed daily point under this key with the latest query_to before `before`,- for callers outside the product. None when there is no such point or the experiment is not in the team."""- if not Experiment.objects.filter(id=experiment_id, team_id=team_id).exists():+ """The stored result of the completed daily point that a new point under this key compares with, for callers+ outside the product. None when there is no such point or the experiment is not in the team.++ `MetricResultStore.previous_completed` picks the point among the points under this key, under the version 1 key+ `display_key`, or whose display key it is. Without `display_key`, the keys come from the metric's calculation config+ under the current configuration of the experiment when that config gives `calculation_key` in some key version.+ """+ experiment = (+ Experiment.objects.select_related("team", "feature_flag").filter(id=experiment_id, team_id=team_id).first()+ )+ if experiment is None:return None- row = MetricResultStore(experiment_id=experiment_id).previous_completed(metric_uuid, calculation_key, before=before)+ fingerprints = {calculation_key}+ if display_key is None:+ calculation_config = find_calculation_config_by_key(experiment, metric_uuid, calculation_key)+ if calculation_config is not None:+ fingerprints.update(_keys_of(calculation_config))+ display_key = calculation_config.legacy_key()+ else:+ fingerprints.add(display_key)+ row = MetricResultStore(experiment_id=experiment_id).previous_completed(+ metric_uuid, fingerprints, display_key=display_key, before=before+ )return row.result if row is not None else None@@ -535,10 +736,11 @@ def record_daily_metric_result(window: datetime,query_from: datetime,result: dict[str, Any],+ display_key: str | None = None,) -> None:"""Store a completed daily point, for the scheduled workflows outside the product."""MetricResultStore(experiment_id=experiment_id).record_daily_point(- metric_uuid, calculation_key, window=window, query_from=query_from, result=result+ metric_uuid, calculation_key, display_key=display_key, window=window, query_from=query_from, result=result)@@ -550,8 +752,14 @@ def record_daily_metric_failure(window: datetime,query_from: datetime,error_message: str,+ display_key: str | None = None,) -> None:"""Store a failed daily point, for the scheduled workflows outside the product."""MetricResultStore(experiment_id=experiment_id).record_daily_failure(- metric_uuid, calculation_key, window=window, query_from=query_from, error_message=error_message+ metric_uuid,+ calculation_key,+ display_key=display_key,+ window=window,+ query_from=query_from,+ error_message=error_message,)diff --git a/products/experiments/backend/migrations/0049_experimentmetricresult_display_key.py b/products/experiments/backend/migrations/0049_experimentmetricresult_display_key.pynew file mode 100644index 000000000000..59bea516c730--- /dev/null+++ b/products/experiments/backend/migrations/0049_experimentmetricresult_display_key.py@@ -0,0 +1,17 @@+# Generated by Django 5.2.17 on 2026-10-01 08:43++from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ("experiments", "0048_experiment_running_start_date_index"),+ ]++ operations = [+ migrations.AddField(+ model_name="experimentmetricresult",+ name="display_key",+ field=models.CharField(blank=True, max_length=64, null=True),+ ),+ ]diff --git a/products/experiments/backend/migrations/max_migration.txt b/products/experiments/backend/migrations/max_migration.txtindex 8121f46e2d67..2bf3bcfa5223 100644--- a/products/experiments/backend/migrations/max_migration.txt+++ b/products/experiments/backend/migrations/max_migration.txt@@ -1 +1 @@-0048_experiment_running_start_date_index+0049_experimentmetricresult_display_keydiff --git a/products/experiments/backend/models/experiment.py b/products/experiments/backend/models/experiment.pyindex 5242c4631811..cbea7ee11e7e 100644--- a/products/experiments/backend/models/experiment.py+++ b/products/experiments/backend/models/experiment.py@@ -474,6 +474,10 @@ class ExperimentMetricResult(models.Model):experiment = models.ForeignKey("Experiment", on_delete=models.CASCADE)metric_uuid = models.CharField(max_length=255)fingerprint = models.CharField(max_length=64, null=True, blank=True) # SHA256 hash is 64 chars+ # The version 1 key of the calculation config the row was computed from, salted like `fingerprint` on a+ # recalculation row. It hashes only inputs the experiment owns, so the chart still shows the row after a team+ # setting moves `fingerprint` away from the current key. No reuse check reads it.+ display_key = models.CharField(max_length=64, null=True, blank=True)query_from = models.DateTimeField()query_to = models.DateTimeField()status = models.CharField(max_length=20, choices=Status, default=Status.PENDING)diff --git a/products/experiments/backend/presentation/views.py b/products/experiments/backend/presentation/views.pyindex 1f62bba90005..a67b4bdfaebf 100644--- a/products/experiments/backend/presentation/views.py+++ b/products/experiments/backend/presentation/views.py@@ -1574,10 +1574,11 @@ class EnterpriseExperimentsViewSet(type=str,location=OpenApiParameter.QUERY,description=(- "Fingerprint of the metric configuration. Available alongside metric_uuid on "- "each metric in the experiment's metrics array."+ "Ignored. The server derives the metric's calculation key from the experiment's current "+ "settings. Accepted so that existing clients keep working."),- required=True,+ required=False,+ deprecated=True,),],)@@ -1585,15 +1586,12 @@ class EnterpriseExperimentsViewSet(def timeseries_results(self, request: Request, *args: Any, **kwargs: Any) -> Response:experiment = self.get_object()metric_uuid = request.query_params.get("metric_uuid")- fingerprint = request.query_params.get("fingerprint")if not metric_uuid:raise ValidationError("metric_uuid query parameter is required")- if not fingerprint:- raise ValidationError("fingerprint query parameter is required")service = ExperimentService(team=self.team, user=request.user)- return Response(service.get_timeseries_results(experiment, metric_uuid=metric_uuid, fingerprint=fingerprint))+ return Response(service.get_timeseries_results(experiment, metric_uuid=metric_uuid))@action(methods=["POST"], detail=True, required_scopes=["experiment:write"])def recalculate_timeseries(self, request: Request, *args: Any, **kwargs: Any) -> Response:diff --git a/products/experiments/backend/recalculation.py b/products/experiments/backend/recalculation.pyindex ef18d1cf1e3d..2a29409a4049 100644--- a/products/experiments/backend/recalculation.py+++ b/products/experiments/backend/recalculation.py@@ -32,6 +32,7 @@ from posthog.ph_client import feature_enabled_or_falsefrom posthog.settings import CLICKHOUSE_CLUSTERfrom posthog.temporal.common.client import sync_connect+from products.experiments.backend.hogql_queries.base_query_utils import experiment_window_endfrom products.experiments.backend.metric_calculation.config import build_calculation_configsfrom products.experiments.backend.metric_calculation.results import MetricResultStorefrom products.experiments.backend.metric_resolution import get_metrics_for_calculation@@ -131,9 +132,9 @@ def _derive_counters(recalc: ExperimentMetricsRecalculation, results: list[dict]the same list is also surfaced to the client.Inherits the fingerprint-divergence hazard from `get_run_results`: `completed_metrics` can silently drop- if experiment fields that feed the fingerprint (start_date, exposure_criteria, stats method,- only_count_matured_users) change between the workflow's writes and this read. `failed_metrics` is partly- immune because discovery-step failures come from `metric_errors` (stored on the row), not from result rows.+ if settings that feed the calculation key change between the workflow's writes and this read.+ `failed_metrics` is partly immune because discovery-step failures come from `metric_errors` (stored on the+ row), not from result rows."""rows = results if results is not None else get_run_results(recalc)completed = sum(1 for r in rows if r["status"] == ExperimentMetricResult.Status.COMPLETED)@@ -622,12 +623,8 @@ def get_run_results(recalc: ExperimentMetricsRecalculation) -> list[dict]:Never returns rows from a previous run or from the timeseries workflow (which uses config fingerprints).- Divergence hazard: the store finds a run's rows by recomputing each metric's fingerprint from mutable- experiment fields (start_date, exposure_criteria, stats method, only_count_matured_users). If any of these- change after the run wrote its rows, this can return [] for what is on-disk a successful run, until the- experiment fields revert. Symptom: "results disappeared after editing exposure_criteria / start_date / stats- config." This is the explicit trade-off of "no FK on ExperimentMetricResult": the snapshot lives in the- fingerprint, not in a stored column.+ Ended experiments retain completed rows with a compatible display key after project settings change, without+ making those rows reusable."""rows = MetricResultStore(experiment_id=recalc.experiment_id).for_run(recalc)return [@@ -648,10 +645,12 @@ def build_timeseries_cold_start_payload(experiment: Experiment) -> dict | None:metrics-recalculation run exists yet. Timeseries rows live in ExperimentMetricResult under the metric'sCONFIG fingerprint (not a per-run recalc fingerprint), so they're found without any recalc row.- Only points younger than TIMESERIES_FALLBACK_MAX_AGE count. The frontend accepts a fallback that covers every- metric without starting a run, so an older point must read as a gap or the page would show weeks-old numbers- as completed. Returns None when no metric has a fresh completed point (caller then keeps the 404). query_to- and completed_at both pin to the freshest point's date. GET never triggers anything itself.+ Only points younger than TIMESERIES_FALLBACK_MAX_AGE count, except that an ended experiment keeps its latest+ compatible point from any time since launch, including a result under earlier settings, when that point reaches+ the end date. The frontend+ accepts a fallback that covers every metric without starting a run, so an older point must read as a gap or+ the page would show weeks-old numbers as completed. Returns None when no metric has a fresh completed point+ (caller then keeps the 404). query_to and completed_at both pin to the freshest point's date. GET never triggers anything itself."""with team_scope(experiment.team_id, canonical=True):calculation_configs = build_calculation_configs(experiment)@@ -662,7 +661,19 @@ def build_timeseries_cold_start_payload(experiment: Experiment) -> dict | None:for calculation_config in calculation_configs:# Bounded on both sides: the backfill writes end-of-day points, so today's point can carry a future# query_to that would surface here as a future completion time.- row = store.latest_daily_point(calculation_config, since=now - TIMESERIES_FALLBACK_MAX_AGE, until=now)+ row = None+ if experiment.is_stopped and experiment.start_date:+ row = store.latest_daily_point(+ calculation_config, since=experiment.start_date, until=now, preserve_history=True+ )+ # A point whose data ends before the end date misses the final hours, so a run must compute them.+ # A backfill stamps the end of its day as query_to, but its data ends at completed_at.+ if row is not None:+ data_until = min(row.query_to, row.completed_at or row.query_to)+ if data_until < experiment_window_end(experiment, now):+ row = None+ if row is None:+ row = store.latest_daily_point(calculation_config, since=now - TIMESERIES_FALLBACK_MAX_AGE, until=now)if row is None:continueresults.append(diff --git a/products/experiments/backend/temporal/ARCHITECTURE.md b/products/experiments/backend/temporal/ARCHITECTURE.mdindex 940cd4de9897..927c5881c5a0 100644--- a/products/experiments/backend/temporal/ARCHITECTURE.md+++ b/products/experiments/backend/temporal/ARCHITECTURE.md@@ -71,7 +71,19 @@ Every metric in a single recalc shares one `query_to` timestamp, stamped by the### Recalc fingerprint, not config fingerprint-Every result row is keyed by a `recalc_fp = sha256(config_fp + "recalculation")` (`_recalc_fingerprint` in `metric_calculation/results.py`). The config part is the calculation key that the daily timeseries workflows use too: `MetricCalculationConfig.calculation_key()` in `metric_calculation/config.py`. The calculation config holds everything the calculation reads, but key version 1 hashes only the effective metric definition, the start date, the stats method, the stored exposure criteria, maturity and the excluded variants. The salt is a fixed string, so the recalc fingerprint is deterministic per config, not per run.+Every result row is keyed by a `recalc_fp = sha256(config_fp + "recalculation")` (`_recalc_fingerprint` in `metric_calculation/results.py`). The config part is the calculation key that the daily timeseries workflows use too: `MetricCalculationConfig.calculation_key()` in `metric_calculation/config.py`. The salt is a fixed string, so the recalc fingerprint is deterministic per config, not per run.++Two key versions exist, and `calculation_key()` returns the one of `settings.key_version`. `resolve_calculation_key_version` decides the version when an operation starts; queued work carries it in its key, so a worker never re-decides it. Key version 1 (`legacy_key()`) hashes only the effective metric definition, the start date, the stats method, the stored exposure criteria, maturity and the excluded variants.++Key version 2 hashes the effective metric definition and every analytical setting of the calculation config: start date, flag key, entity, variants, the resolved baseline, excluded variants, the exposure criteria, test account filters, the resolved statistics method and settings, CUPED with team defaults applied, maturity, and the team timezone. Every metric type keys on the timezone, because date property filters, the CUPED lookback and retention windows all read it. Floats are rounded to 12 significant digits first, so stored float noise such as `1 - 0.95` does not split a key. Nested settings enter through explicit field lists, so a schema change that adds a defaulted field to a model changes no key. The key version is part of the hashed payload: a change that makes the engine compute a different result for an existing config bumps `CALCULATION_KEY_VERSION`, so older results stop counting for reuse.++Key version 2 reads the exposure criteria as stored, with the product defaults applied (`normalize_exposure`). It does not read the event that the `experiment-exposure-event` flag picks as the default exposure event, because each process evaluates that flag locally: two pods that disagree would file and look up one experiment's results under two keys, and each rollout step would re-key every affected experiment. A stored result therefore stays reusable for its window when the flag changes for a team, as with key version 1. Shipping the new event to every team is an engine change, which bumps `CALCULATION_KEY_VERSION` if older results must stop counting for reuse.++Rows keep the key they were written with. Readers find a config's rows under both key versions. The newest compatible row wins, and rows under the current settings win only a tie, while `has_completed` requires the active key. A version 1 row's exact analytical settings are unknown, so no conversion may assign it a version 2 key without recomputing it.++Writes can also store the version 1 key in `display_key`, salted for run rows. The chart, current outcome and significance baseline use it to preserve history across project setting changes. Ended experiments also use it for `for_run` and cold-start display, so a project edit does not create a gap that automatically overwrites their decided result. Running experiments still recompute.++The display key omits confidence, baseline and CUPED settings as well as project settings, so it must never authorize calculation reuse. Metric definition, start date, stats method, stored exposure criteria, maturity and excluded-variant edits change the display key too and hide incompatible history.This matters because the recalc workflow shares the `ExperimentMetricResult` table with the timeseries workflows. If we used the config fingerprint, every recalc would overwrite the cached daily timeseries row, wrecking the timeseries reads. The constant salt keeps the recalc family distinct from the timeseries family on the same table, so they never collide.@@ -99,11 +111,12 @@ The snapshot for such an experiment is the one row at `end_date` for the current`ExperimentMetricResult` has no foreign key pointing back to `ExperimentMetricsRecalculation`. The scoping key lives entirely in the fingerprint plus `query_to`. This avoided a migration on a shared table to add a nullable column, and makes the two workflow families uniform in how they write results.-The trade-off: reads have to recompute the fingerprint set to find a run's results (`MetricResultStore.for_run` walks each metric, recomputes its `config_fp`, applies the recalc salt, then `WHERE fingerprint IN (...) AND query_to = recalc.query_to`). If the experiment's `start_date` / `exposure_criteria` / stats config changes between the write and the read, the recomputed fingerprints don't match the on-disk ones, and results "disappear." Documented inline as the fingerprint-divergence hazard.+Run reads rebuild the current keys and match the run's window. A configuration change can leave a running experiment uncovered; an ended experiment keeps compatible completed results through the display key.Every read and write of `ExperimentMetricResult` goes through `MetricResultStore` (`metric_calculation/results.py`), so the salt and the sync's copy window stay in one module.The current result of a metric (`current_outcome`) is the completed row whose data ends last under the metric's current calculation key, salted or bare.A row's data ends at the earlier of its `query_to` and its `completed_at`, because the backfill stamps the current day with the end of that day but holds data only up to the moment it ran.+Without such a row, the row under the other key version, or whose display key is the version 1 key, whose data ends last stands in.The experiment-completed event reads it for the first primary metric in display order, inline or saved, and the setup context reads it for its outcomes.A backfilled past window does not win over a newer window, a backfilled current day does not win over a later run of that day, and a row under an earlier configuration, for example from before a relaunch, does not count.When several rows share `(experiment, metric_uuid, query_to)`, each query returns the row with the newest `completed_at`, then the highest id.diff --git a/products/experiments/backend/test/test_calculation_config.py b/products/experiments/backend/test/test_calculation_config.pyindex 7e90ae7d1f41..00001a2835d9 100644--- a/products/experiments/backend/test/test_calculation_config.py+++ b/products/experiments/backend/test/test_calculation_config.py@@ -1,23 +1,31 @@-from datetime import UTC, datetime+import dataclasses+from datetime import UTC, datetime, timedeltafrom typing import Anyfrom uuid import uuid4import pytestfrom posthog.test.base import BaseTest+from unittest.mock import patchfrom parameterized import parameterized+from posthog.schema import ExperimentEventExposureConfig, ExperimentExposureCriteria+from posthog.models.team.extensions import get_or_create_team_extensionfrom products.experiments.backend.hogql_queries.cuped_config import CupedQueryConfig+from products.experiments.backend.hogql_queries.exposure_query_logic import EXPERIMENT_EXPOSURE_EVENT_CUTOFFfrom products.experiments.backend.hogql_queries.utils import BayesianSettings, FrequentistSettingsfrom products.experiments.backend.metric_calculation.config import (ExperimentCalculationSettings,build_calculation_configs,+ normalize_exposure,)+from products.experiments.backend.metric_calculation.results import _recalc_fingerprint, result_configuration_statusfrom products.experiments.backend.metric_resolution import MetricRole, MetricSourcefrom products.experiments.backend.models.experiment import Experiment, ExperimentSavedMetric, ExperimentToSavedMetricfrom products.experiments.backend.models.team_experiments_config import TeamExperimentsConfig+from products.experiments.backend.test.utils import patch_calculation_key_versionfrom products.experiments.stats.shared.enums import DifferenceTypefrom products.feature_flags.backend.models.feature_flag import FeatureFlag@@ -54,8 +62,8 @@ DEFINITIONS: dict[str, dict[str, Any]] = {},}-# Result rows and stored `fingerprint` values of these configurations already hold these hashes, so the-# calculation key must keep producing them. Keyed by (metric kind, breakdown, only_count_matured_users).+# Result rows written before key version 2 hold these hashes, and the display readers find them by the legacy+# key, so it must keep producing them. Keyed by (metric kind, breakdown, only_count_matured_users).STORED_KEYS: dict[tuple[str, bool, bool], str] = {("mean", False, False): "8c4c170619bed04837934834898b06c80e1ffb081cfc83e50394e7a90809e7f1",("mean", False, True): "170e5a30b8b04a520777ec3cccac4e055f7038b64dd547263499ee75ea1b71fc",@@ -131,6 +139,10 @@ STORED_KEY_CASES = [class TestMetricCalculationConfig(BaseTest):+ def setUp(self) -> None:+ super().setUp()+ self.enterContext(patch_calculation_key_version(2))+def _flag(self) -> FeatureFlag:return FeatureFlag.objects.create(team=self.team,@@ -184,7 +196,7 @@ class TestMetricCalculationConfig(BaseTest):ExperimentToSavedMetric.objects.create(experiment=experiment, saved_metric=saved_metric, metadata=metadata)@parameterized.expand(STORED_KEY_CASES)- def test_key_equals_the_stored_fingerprint(+ def test_legacy_key_equals_the_stored_fingerprint(self,_name: str,kind: str,@@ -198,7 +210,7 @@ class TestMetricCalculationConfig(BaseTest):[calculation_config] = build_calculation_configs(experiment)- assert calculation_config.calculation_key() == stored_key+ assert calculation_config.legacy_key() == stored_key@parameterized.expand([("no_breakdowns", False), ("with_breakdowns", True)])def test_equivalent_inline_and_saved_metrics_have_equal_configs(self, _name: str, breakdown: bool) -> None:@@ -261,6 +273,25 @@ class TestMetricCalculationConfig(BaseTest):{"stats_config": {}},{"stats_config": {"baseline_variant_key": "control"}},),+ (+ "absent_and_explicit_default_exposure_event",+ {"exposure_criteria": {"filterTestAccounts": True}},+ {+ "exposure_criteria": {+ "filterTestAccounts": True,+ "exposure_config": {+ "kind": "ExperimentEventExposureConfig",+ "event": "$feature_flag_called",+ "properties": [],+ },+ }+ },+ ),+ (+ "tuning_parameter_without_sequential_testing",+ {"stats_config": {"method": "frequentist"}},+ {"stats_config": {"method": "frequentist", "frequentist": {"sequential_tuning_parameter": 10000}}},+ ),])def test_configurations_that_compute_the_same_thing_have_equal_configs(@@ -278,6 +309,74 @@ class TestMetricCalculationConfig(BaseTest):first_config, second_config = calculation_configsassert first_config == second_config+ assert first_config.calculation_key() == second_config.calculation_key()++ @parameterized.expand(+ [+ ("baseline", "experiment", {"stats_config": {"baseline_variant_key": "test"}}, True),+ ("credible_interval", "experiment", {"stats_config": {"bayesian": {"ci_level": 0.9}}}, True),+ ("explicit_cuped", "experiment", {"stats_config": {"cuped": {"enabled": True}}}, True),+ ("team_default_cuped", "team_config", {"default_cuped_enabled": True}, True),+ ("alpha", "experiment", {"stats_config": {"frequentist": {"alpha": 0.1}}}, True),+ ("team_default_sequential", "team_config", {"default_sequential_testing_enabled": True}, True),+ ("flag_aggregation", "flag", {"aggregation_group_type_index": 0}, True),+ ("team_test_account_filters", "team", {"test_account_filters": [{"key": "email", "type": "person"}]}, True),+ ("test_account_filtering_off", "experiment", {"exposure_criteria": {"filterTestAccounts": False}}, True),+ ("name_and_description", "experiment", {"name": "renamed", "description": "new words"}, False),+ ("display_order", "experiment", {"primary_metrics_ordered_uuids": ["inline-mean"]}, False),+ ("end_date", "experiment", {"end_date": datetime(2026, 2, 1, tzinfo=UTC)}, False),+ ("conclusion", "experiment", {"conclusion": "won", "conclusion_comment": "shipped"}, False),+ ]+ )+ def test_the_key_changes_with_every_analytical_input_and_nothing_else(+ self, _name: str, target: str, changes: dict[str, Any], key_changes: bool+ ) -> None:+ # Alpha and sequential testing only reach the resolved settings of a frequentist experiment.+ base_stats = {"method": "frequentist"} if _name in ("alpha", "team_default_sequential") else {}+ experiment = self._experiment(stats_config=base_stats)+ self._add_metric(experiment, "mean", "inline", role="primary")+ get_or_create_team_extension(self.team, TeamExperimentsConfig)+ [before] = build_calculation_configs(experiment)++ if target == "experiment":+ if "stats_config" in changes:+ changes = {"stats_config": {**base_stats, **changes["stats_config"]}}+ Experiment.objects.filter(pk=experiment.pk).update(**changes)+ elif target == "team_config":+ TeamExperimentsConfig.objects.filter(team=self.team).update(**changes)+ elif target == "flag":+ flag = experiment.feature_flag+ flag.filters = {**flag.filters, **changes}+ flag.save()+ else:+ for name, value in changes.items():+ setattr(self.team, name, value)+ self.team.save()+ [after] = build_calculation_configs(Experiment.objects.get(pk=experiment.pk))++ assert (after.calculation_key() != before.calculation_key()) is key_changes++ def test_the_key_does_not_depend_on_the_exposure_event_rollout_flag(self) -> None:+ experiment = self._experiment(start_date=EXPERIMENT_EXPOSURE_EVENT_CUTOFF + timedelta(days=1))+ self._add_metric(experiment, "mean", "inline")+ calculation_configs = {}+ for flag_enabled in (False, True):+ with patch(+ "products.experiments.backend.hogql_queries.exposure_query_logic.posthoganalytics.feature_enabled",+ return_value=flag_enabled,+ ):+ [calculation_configs[flag_enabled]] = build_calculation_configs(experiment)++ # The query runner still counts the event the flag picks.+ assert [+ calculation_config.settings.exposure and calculation_config.settings.exposure.exposure_config+ for calculation_config in calculation_configs.values()+ ] == [+ ExperimentEventExposureConfig(event="$feature_flag_called", properties=[]),+ ExperimentEventExposureConfig(event="$experiment_exposure", properties=[]),+ ]+ assert calculation_configs[False] == calculation_configs[True]+ assert calculation_configs[False].calculation_key() == calculation_configs[True].calculation_key()@parameterized.expand([@@ -304,12 +403,14 @@ _SETTINGS = ExperimentCalculationSettings(baseline="control",excluded_variants=(),exposure=None,+ normalized_exposure=normalize_exposure(None),test_account_filters=(),stats=BayesianSettings(ci_level=0.95, difference_type=DifferenceType.RELATIVE),cuped=CupedQueryConfig(),only_count_matured_users=False,timezone="UTC",stored_exposure_criteria=None,+ key_version=2,)_MEAN = {**DEFINITIONS["mean"], "uuid": "m1"}@@ -327,3 +428,323 @@ def test_only_real_breakdowns_change_the_key(variant: dict, expected_equal: boolbase_key = _SETTINGS.build_metric_config(metric_id="m1", role="primary", definition=_MEAN).calculation_key()variant_key = _SETTINGS.build_metric_config(metric_id="m1", role="primary", definition=variant).calculation_key()assert (variant_key == base_key) is expected_equal+++_FREQUENTIST = FrequentistSettings(+ alpha=0.05, difference_type=DifferenceType.RELATIVE, sequential_testing_enabled=False, sequential_tuning_parameter=0+)+++ "stats,metric_change,expected_equal",+ [+ # `1 - 0.95` is how a confidence level becomes a stored alpha.+ (dataclasses.replace(_FREQUENTIST, alpha=1 - 0.95), {}, True),+ (dataclasses.replace(_FREQUENTIST, alpha=0.1), {}, False),+ (_FREQUENTIST, {"upper_bound_percentile": 0.9500000000000001}, True),+ (_FREQUENTIST, {"upper_bound_percentile": 0.9}, False),+ (_FREQUENTIST, {"conversion_window": 14.0}, True),+ ],+)+def test_float_noise_does_not_split_keys(+ stats: FrequentistSettings, metric_change: dict[str, Any], expected_equal: bool+) -> None:+ base = dataclasses.replace(_SETTINGS, stats=_FREQUENTIST)+ base_key = base.build_metric_config(+ metric_id="m1", role="primary", definition={**_MEAN, "upper_bound_percentile": 0.95, "conversion_window": 14}+ ).calculation_key()+ variant_key = (+ dataclasses.replace(_SETTINGS, stats=stats)+ .build_metric_config(+ metric_id="m1",+ role="primary",+ definition={**_MEAN, "upper_bound_percentile": 0.95, "conversion_window": 14, **metric_change},+ )+ .calculation_key()+ )+ assert (variant_key == base_key) is expected_equal+++_SEQUENTIAL = dataclasses.replace(_FREQUENTIST, sequential_testing_enabled=True, sequential_tuning_parameter=5000)+_CUPED = CupedQueryConfig(enabled=True, lookback_days=14)+_RETENTION = {**DEFINITIONS["retention"], "uuid": "m1"}+_BROWSER_FILTER = {"key": "$browser", "type": "event", "value": ["Chrome"], "operator": "exact"}+_DEFAULT_EVENT_WITH_PROPERTIES: dict[str, Any] = {+ "filterTestAccounts": True,+ "multiple_variant_handling": "exclude",+ "exposure_config": {+ "kind": "ExperimentEventExposureConfig",+ "event": "$feature_flag_called",+ "properties": [_BROWSER_FILTER],+ },+}+_ACTION_ACTIVATION: dict[str, Any] = {+ "filterTestAccounts": False,+ "multiple_variant_handling": "first_seen",+ "activation_config": {+ "kind": "ActionsNode",+ "id": 7,+ "name": "Signed up",+ "properties": [{"key": "plan", "type": "person", "value": ["pro"], "operator": "exact"}],+ },+}+_CUSTOM_EVENT: dict[str, Any] = {+ "filterTestAccounts": True,+ "exposure_config": {"kind": "ExperimentEventExposureConfig", "event": "checkout_viewed", "properties": []},+}+_ACTION_EXPOSURE: dict[str, Any] = {"filterTestAccounts": True, "exposure_config": {"kind": "ActionsNode", "id": 7}}+++def _with_exposure(+ criteria: dict[str, Any], settings: ExperimentCalculationSettings = _SETTINGS+) -> ExperimentCalculationSettings:+ return dataclasses.replace(settings, normalized_exposure=normalize_exposure(criteria))+++# Each case changes one value that the calculation reads and expects a new key, or changes a value that the+# calculation ignores and expects the same key. Named after the field it changes.+_KEY_INPUT_CASES: list[tuple[str, ExperimentCalculationSettings, ExperimentCalculationSettings, dict, bool]] = [+ (+ "settings.start_date",+ _SETTINGS,+ dataclasses.replace(_SETTINGS, start_date=START + timedelta(days=1)),+ _MEAN,+ False,+ ),+ ("settings.feature_flag_key", _SETTINGS, dataclasses.replace(_SETTINGS, feature_flag_key="other"), _MEAN, False),+ (+ "settings.aggregation_group_type_index",+ _SETTINGS,+ dataclasses.replace(_SETTINGS, aggregation_group_type_index=0),+ _MEAN,+ False,+ ),+ ("settings.variants", _SETTINGS, dataclasses.replace(_SETTINGS, variants=("control", "test", "b")), _MEAN, False),+ ("settings.baseline", _SETTINGS, dataclasses.replace(_SETTINGS, baseline="test"), _MEAN, False),+ ("settings.excluded_variants", _SETTINGS, dataclasses.replace(_SETTINGS, excluded_variants=("b",)), _MEAN, False),+ (+ "settings.test_account_filters",+ _SETTINGS,+ dataclasses.replace(_SETTINGS, test_account_filters=({"key": "email", "type": "person"},)),+ _MEAN,+ False,+ ),+ ("settings.stats", _SETTINGS, dataclasses.replace(_SETTINGS, stats=_FREQUENTIST), _MEAN, False),+ ("settings.cuped", _SETTINGS, dataclasses.replace(_SETTINGS, cuped=_CUPED), _MEAN, False),+ (+ "settings.only_count_matured_users",+ _SETTINGS,+ dataclasses.replace(_SETTINGS, only_count_matured_users=True),+ _MEAN,+ False,+ ),+ ("settings.timezone", _SETTINGS, dataclasses.replace(_SETTINGS, timezone="Asia/Kolkata"), _RETENTION, False),+ *[+ (+ f"timezone_for_a_{kind}_metric",+ _SETTINGS,+ dataclasses.replace(_SETTINGS, timezone="Asia/Kolkata"),+ definition,+ False,+ )+ for kind, definition in (("mean", _MEAN), ("funnel", DEFINITIONS["funnel"]), ("ratio", DEFINITIONS["ratio"]))+ ],+ (+ "bayesian.ci_level",+ _SETTINGS,+ dataclasses.replace(_SETTINGS, stats=BayesianSettings(ci_level=0.9, difference_type=DifferenceType.RELATIVE)),+ _MEAN,+ False,+ ),+ (+ "bayesian.difference_type",+ _SETTINGS,+ dataclasses.replace(_SETTINGS, stats=BayesianSettings(ci_level=0.95, difference_type=DifferenceType.ABSOLUTE)),+ _MEAN,+ False,+ ),+ *[+ (+ f"frequentist.{name}",+ dataclasses.replace(_SETTINGS, stats=_SEQUENTIAL),+ dataclasses.replace(_SETTINGS, stats=changed),+ _MEAN,+ False,+ )+ for name, changed in (+ ("alpha", dataclasses.replace(_SEQUENTIAL, alpha=0.1)),+ ("difference_type", dataclasses.replace(_SEQUENTIAL, difference_type=DifferenceType.ABSOLUTE)),+ ("sequential_testing_enabled", dataclasses.replace(_SEQUENTIAL, sequential_testing_enabled=False)),+ ("sequential_tuning_parameter", dataclasses.replace(_SEQUENTIAL, sequential_tuning_parameter=10000)),+ )+ ],+ *[+ (+ f"cuped.{name}",+ dataclasses.replace(_SETTINGS, cuped=_CUPED),+ dataclasses.replace(_SETTINGS, cuped=changed),+ _MEAN,+ False,+ )+ for name, changed in (+ ("enabled", dataclasses.replace(_CUPED, enabled=False)),+ ("lookback_days", dataclasses.replace(_CUPED, lookback_days=7)),+ )+ ],+ *[+ (+ f"exposure_criteria.{name}",+ _with_exposure(_DEFAULT_EVENT_WITH_PROPERTIES),+ _with_exposure(changed),+ _MEAN,+ False,+ )+ for name, changed in (+ ("exposure_config", _CUSTOM_EVENT),+ (+ "activation_config",+ {**_DEFAULT_EVENT_WITH_PROPERTIES, "activation_config": _ACTION_ACTIVATION["activation_config"]},+ ),+ ("filterTestAccounts", {**_DEFAULT_EVENT_WITH_PROPERTIES, "filterTestAccounts": False}),+ (+ "multiple_variant_handling",+ {**_DEFAULT_EVENT_WITH_PROPERTIES, "multiple_variant_handling": "first_seen"},+ ),+ )+ ],+ (+ "exposure_config.event",+ _with_exposure(_CUSTOM_EVENT),+ _with_exposure(+ {**_CUSTOM_EVENT, "exposure_config": {**_CUSTOM_EVENT["exposure_config"], "event": "cart_viewed"}}+ ),+ _MEAN,+ False,+ ),+ (+ "exposure_config.properties",+ _with_exposure(_DEFAULT_EVENT_WITH_PROPERTIES),+ _with_exposure(+ {+ **_DEFAULT_EVENT_WITH_PROPERTIES,+ "exposure_config": {**_DEFAULT_EVENT_WITH_PROPERTIES["exposure_config"], "properties": []},+ }+ ),+ _MEAN,+ False,+ ),+ (+ "actions_node.id",+ _with_exposure(_ACTION_EXPOSURE),+ _with_exposure({**_ACTION_EXPOSURE, "exposure_config": {"kind": "ActionsNode", "id": 8}}),+ _MEAN,+ False,+ ),+ (+ "actions_node.properties",+ _with_exposure(_ACTION_EXPOSURE),+ _with_exposure(+ {**_ACTION_EXPOSURE, "exposure_config": {"kind": "ActionsNode", "id": 7, "properties": [_BROWSER_FILTER]}}+ ),+ _MEAN,+ False,+ ),+]++[email protected]("_name,before,after,definition,keys_equal", _KEY_INPUT_CASES)+def test_the_key_follows_what_the_calculation_reads(+ _name: str,+ before: ExperimentCalculationSettings,+ after: ExperimentCalculationSettings,+ definition: dict,+ keys_equal: bool,+) -> None:+ before_key = before.build_metric_config(metric_id="m1", role="primary", definition=definition).calculation_key()+ after_key = after.build_metric_config(metric_id="m1", role="primary", definition=definition).calculation_key()+ assert (after_key == before_key) is keys_equal+++def test_every_field_that_can_change_the_key_has_a_case() -> None:+ # The key reads explicit field lists, so a field added to one of these types reaches no key until someone adds+ # it. This fails until the new field has a case above, and the case fails until the key reads the field.+ compared_settings = {field.name for field in dataclasses.fields(ExperimentCalculationSettings) if field.compare}+ expected = (+ # The exposure_criteria cases cover normalized_exposure. The team is the tenant and the key version picks the+ # key, so neither is an input.+ {f"settings.{name}" for name in compared_settings - {"team_id", "normalized_exposure", "key_version"}}+ | {f"bayesian.{field.name}" for field in dataclasses.fields(BayesianSettings)}+ | {f"frequentist.{field.name}" for field in dataclasses.fields(FrequentistSettings)}+ | {f"cuped.{field.name}" for field in dataclasses.fields(CupedQueryConfig)}+ | {f"exposure_criteria.{name}" for name in ExperimentExposureCriteria.model_fields}+ # The kind tells an event from an action, and the actions_node cases cover that. The response and the+ # version are not inputs of the query.+ | {+ f"exposure_config.{name}"+ for name in set(ExperimentEventExposureConfig.model_fields) - {"kind", "response", "version"}+ }+ )+ changing_cases = {name for name, *_rest, keys_equal in _KEY_INPUT_CASES if not keys_equal}+ assert expected <= changing_cases+++ "settings,definition,key",+ [+ (_SETTINGS, _MEAN, "e456819bf18b5561c706a037769cb017fb4a7bd8e620c30a23ce1b9cc39cfe50"),+ (+ dataclasses.replace(+ _with_exposure(_DEFAULT_EVENT_WITH_PROPERTIES),+ stats=_FREQUENTIST,+ excluded_variants=("test-2",),+ test_account_filters=({"key": "email", "value": "@example.com", "type": "person"},),+ cuped=CupedQueryConfig(enabled=True, lookback_days=7),+ ),+ _MEAN,+ "70e81cb7ae1b2572557d20f5875391d61c119d5a2aa9b075cd3ef69c43ae750e",+ ),+ (+ dataclasses.replace(_with_exposure(_ACTION_ACTIVATION), stats=_SEQUENTIAL, timezone="Europe/Berlin"),+ _RETENTION,+ "aebb0ab85180a4c2f0b341840f2e4399fe3acc042a62f638b511d301d88f481f",+ ),+ ],+)+def test_key_version_2_is_stable(settings: ExperimentCalculationSettings, definition: dict, key: str) -> None:+ # Every stored result is filed under this hash. A change to the hashed payload, such as a new settings field,+ # makes all of them unreachable for reuse, so it has to come with a CALCULATION_KEY_VERSION bump and new pins.+ assert settings.build_metric_config(metric_id="m1", role="primary", definition=definition).calculation_key() == key++[email protected]("active_version", [1, 2])+def test_a_key_tells_its_version_whatever_version_is_active(active_version: int) -> None:+ calculation_config = _SETTINGS.build_metric_config(metric_id="m1", role="primary", definition=_MEAN)+ active = calculation_config.with_key_version(active_version)++ assert active.calculation_key() == calculation_config.key_for_version(active_version)+ assert [active.key_version_of(calculation_config.key_for_version(version)) for version in (1, 2)] == [1, 2]+ assert active.key_version_of("0" * 64) is None+++ "active_version,row_version,salted,expected",+ [+ (1, 1, False, "current"),+ (1, 2, True, "current"),+ # A version 1 key omits settings, so once version 2 is active such a row's settings are unknown.+ (2, 1, False, "unknown"),+ (2, 1, True, "unknown"),+ (2, 2, False, "current"),+ (1, None, False, "previous"),+ ],+)+def test_result_provenance_is_relative_to_the_active_key_version(+ active_version: int, row_version: int | None, salted: bool, expected: str+) -> None:+ calculation_config = _SETTINGS.build_metric_config(+ metric_id="m1", role="primary", definition=_MEAN+ ).with_key_version(active_version)+ fingerprint = calculation_config.key_for_version(row_version) if row_version is not None else "a" * 64+ fingerprint = _recalc_fingerprint(fingerprint) if salted else fingerprint++ assert result_configuration_status(fingerprint, calculation_config) == expecteddiff --git a/products/experiments/backend/test/test_experiment_service.py b/products/experiments/backend/test/test_experiment_service.pyindex 680c0ea1f366..6bd7df9798fe 100644--- a/products/experiments/backend/test/test_experiment_service.py+++ b/products/experiments/backend/test/test_experiment_service.py@@ -5508,28 +5508,41 @@ class TestExperimentService(APIBaseTest):experiment = service.create_experiment(name="No Start", feature_flag_key="ts-no-start")with self.assertRaises(ValidationError) as ctx:- service.get_timeseries_results(experiment, metric_uuid="m1", fingerprint="fp1")+ service.get_timeseries_results(experiment, metric_uuid="m1")assert "not been started" in str(ctx.exception)- def test_get_timeseries_results_pending_when_no_records(self):- self._create_flag(key="ts-pending")+ @parameterized.expand([("metric_on_the_experiment", True), ("metric_not_on_the_experiment", False)])+ def test_get_timeseries_results_pending_when_no_records(self, _name: str, metric_on_experiment: bool) -> None:+ self._create_flag(key=f"ts-pending-{_name}")service = self._service()experiment = service.create_experiment(name="Pending",- feature_flag_key="ts-pending",+ feature_flag_key=f"ts-pending-{_name}",start_date=timezone.now() - timedelta(days=2),end_date=timezone.now(),+ metrics=[self._DEFAULT_METRIC] if metric_on_experiment else None,+ allow_unknown_events=True,)- result = service.get_timeseries_results(experiment, metric_uuid="m1", fingerprint="fp1")+ result = service.get_timeseries_results(experiment, metric_uuid="m1")assert result["status"] == "pending"assert result["experiment_id"] == experiment.idassert result["metric_uuid"] == "m1"assert len(result["timeseries"]) == 3- def test_get_timeseries_results_completed(self):+ @parameterized.expand(+ [+ ("current_rows", ("current", "current"), "completed"),+ # Rows under the inactive key version of the config render where no current row covers a day.+ ("rows_of_the_other_key_version", ("other_version", "current"), "completed"),+ ("rows_of_earlier_settings", ("earlier_settings", "current"), "partial"),+ ]+ )+ def test_get_timeseries_results_completed(+ self, _name: str, day_keys: tuple[str, str], expected_status: str+ ) -> None:self._create_flag(key="ts-completed")service = self._service()now = timezone.now()@@ -5541,14 +5554,23 @@ class TestExperimentService(APIBaseTest):feature_flag_key="ts-completed",start_date=start_midnight,end_date=end_midnight,+ metrics=[self._DEFAULT_METRIC],+ allow_unknown_events=True,)+ calculation_config = get_metric_calculation_config(Experiment.objects.get(pk=experiment.pk), "m1")+ assert calculation_config is not None+ keys = {+ "current": calculation_config.calculation_key(),+ "other_version": calculation_config.key_for_version(2),+ "earlier_settings": "a" * 64,+ }# Create results whose query_to is midnight (exclusive end of each day)- for day_offset in range(2):+ for day_offset, key_family in enumerate(day_keys):ExperimentMetricResult.objects.create(experiment=experiment,metric_uuid="m1",- fingerprint="fp1",+ fingerprint=keys[key_family],query_from=start_midnight + timedelta(days=day_offset),query_to=start_midnight + timedelta(days=day_offset + 1),status="completed",@@ -5556,9 +5578,9 @@ class TestExperimentService(APIBaseTest):completed_at=now,)- result = service.get_timeseries_results(experiment, metric_uuid="m1", fingerprint="fp1")+ result = service.get_timeseries_results(experiment, metric_uuid="m1")- assert result["status"] == "completed"+ assert result["status"] == expected_statusassert result["computed_at"] is not Nonedef test_get_timeseries_results_strips_step_sessions_and_emits_formatted_results(self):@@ -5572,6 +5594,8 @@ class TestExperimentService(APIBaseTest):feature_flag_key="ts-strip",start_date=start_midnight,end_date=end_midnight,+ metrics=[self._DEFAULT_METRIC],+ allow_unknown_events=True,)session = {"event_uuid": "u", "person_id": "p", "session_id": "s", "timestamp": "t"}@@ -5601,7 +5625,7 @@ class TestExperimentService(APIBaseTest):ExperimentMetricResult.objects.create(experiment=experiment,metric_uuid="m1",- fingerprint="fp1",+ fingerprint=_calculation_key(experiment, "m1"),query_from=start_midnight + timedelta(days=day_offset),query_to=start_midnight + timedelta(days=day_offset + 1),status="completed",@@ -5609,7 +5633,7 @@ class TestExperimentService(APIBaseTest):completed_at=now,)- result = service.get_timeseries_results(experiment, metric_uuid="m1", fingerprint="fp1")+ result = service.get_timeseries_results(experiment, metric_uuid="m1")for day_payload in result["timeseries"].values():if day_payload is None:diff --git a/products/experiments/backend/test/test_metric_result_store.py b/products/experiments/backend/test/test_metric_result_store.pyindex 9999e0a2d048..96c20c0bc922 100644--- a/products/experiments/backend/test/test_metric_result_store.py+++ b/products/experiments/backend/test/test_metric_result_store.py@@ -5,9 +5,15 @@ from zoneinfo import ZoneInfofrom posthog.test.base import BaseTestfrom django.db import connection+from django.utils import timezone as django_timezonefrom parameterized import parameterized+from posthog.schema import ExperimentQuery++from posthog.models.team.extensions import get_or_create_team_extension++from products.experiments.backend.hogql_queries.experiment_query_runner import ExperimentQueryRunnerfrom products.experiments.backend.metric_calculation.config import (MetricCalculationConfig,get_metric_calculation_config,@@ -16,12 +22,17 @@ from products.experiments.backend.metric_calculation.results import (MetricResultStore,_recalc_fingerprint,previous_completed_metric_result,+ record_daily_metric_result,+ result_configuration_status,)+from products.experiments.backend.metric_resolution import build_metricfrom products.experiments.backend.models.experiment import (Experiment,ExperimentMetricResult,ExperimentMetricsRecalculation,)+from products.experiments.backend.models.team_experiments_config import TeamExperimentsConfig+from products.experiments.backend.test.utils import patch_calculation_key_versionfrom products.feature_flags.backend.models.feature_flag import FeatureFlag_START = datetime(2026, 1, 1, tzinfo=UTC)@@ -62,6 +73,10 @@ def _samples(row: ExperimentMetricResult | None) -> list[int]:class TestMetricResultStore(BaseTest):+ def setUp(self) -> None:+ super().setUp()+ self.enterContext(patch_calculation_key_version(2))+def _experiment(self) -> Experiment:flag = FeatureFlag.objects.create(team=self.team, created_by=self.user, key="store-flag", name="store")return Experiment.objects.create(@@ -81,12 +96,20 @@ class TestMetricResultStore(BaseTest):)def _row(- self, experiment: Experiment, fingerprint: str, *, query_to: datetime, completed_at: datetime, samples: int- ) -> None:- ExperimentMetricResult.objects.create(+ self,+ experiment: Experiment,+ fingerprint: str,+ *,+ query_to: datetime,+ completed_at: datetime,+ samples: int,+ display_key: str | None = None,+ ) -> ExperimentMetricResult:+ return ExperimentMetricResult.objects.create(experiment=experiment,metric_uuid="m1",fingerprint=fingerprint,+ display_key=display_key,query_from=_START,query_to=query_to,status=ExperimentMetricResult.Status.COMPLETED,@@ -95,12 +118,17 @@ class TestMetricResultStore(BaseTest):)def _read(self, reader: str, experiment: Experiment, calculation_config: MetricCalculationConfig) -> list[int]:+ """The samples of the row a reader returns at _WINDOW."""store = MetricResultStore(experiment_id=experiment.id)- key = calculation_config.calculation_key()match reader:case "for_run":run = ExperimentMetricsRecalculation.objects.create(- team=self.team, experiment=experiment, metric_uuids=["m1"], query_to=_WINDOW+ team=self.team,+ experiment=experiment,+ metric_uuids=["m1"],+ query_to=_WINDOW,+ status=ExperimentMetricsRecalculation.Status.COMPLETED,+ completed_at=django_timezone.now(),)return [sample for row in store.for_run(run) for sample in _samples(row)]case "latest_daily_point":@@ -108,15 +136,29 @@ class TestMetricResultStore(BaseTest):store.latest_daily_point(calculation_config, since=_WINDOW - timedelta(days=1), until=_WINDOW))case "timeseries":- return _samples(store.timeseries("m1", key, timezone=ZoneInfo("UTC")).by_day.get(_WINDOW_DAY))+ return _samples(store.timeseries(calculation_config, timezone=ZoneInfo("UTC")).by_day.get(_WINDOW_DAY))case "previous_completed":- return _samples(store.previous_completed("m1", key, before=_WINDOW + timedelta(days=1)))+ return _samples(+ store.previous_completed(+ "m1",+ [calculation_config.calculation_key(), calculation_config.legacy_key()],+ display_key=calculation_config.legacy_key(),+ before=_WINDOW + timedelta(days=1),+ )+ )case "current_outcome":summary = store.current_outcome(calculation_config)assert summary is not None and summary.baseline_samples is not Nonereturn [int(summary.baseline_samples)]raise AssertionError(f"unknown reader {reader}")+ def _allow_rows_sharing_a_window(self) -> None:+ # The unique constraint allows one row per (experiment, metric_uuid, query_to). Dropping it inside the+ # test transaction lets two rows share a window, and the rollback at the end of the test restores it.+ assert connection.in_atomic_block+ with connection.schema_editor() as editor:+ editor.alter_unique_together(ExperimentMetricResult, [("experiment", "metric_uuid", "query_to")], [])+@parameterized.expand([(f"{reader}_{tie}", reader, tie)@@ -139,11 +181,7 @@ class TestMetricResultStore(BaseTest):if reader == "for_run"else calculation_config.calculation_key())- # The unique constraint allows one row per (experiment, metric_uuid, query_to). Dropping it inside the- # test transaction lets two rows share a window, and the rollback at the end of the test restores it.- assert connection.in_atomic_block- with connection.schema_editor() as editor:- editor.alter_unique_together(ExperimentMetricResult, [("experiment", "metric_uuid", "query_to")], [])+ self._allow_rows_sharing_a_window()if tie == "newer_write_wins":# The newer write gets the lower id, so an order on id alone picks the wrong row.self._row(experiment, fingerprint, query_to=_WINDOW, completed_at=_WINDOW + timedelta(hours=2), samples=2)@@ -154,6 +192,57 @@ class TestMetricResultStore(BaseTest):assert self._read(reader, experiment, calculation_config) == [2]+ @parameterized.expand(+ [+ *(+ (f"{reader}_{case}", reader, case)+ for reader in ("for_run", "latest_daily_point", "timeseries", "current_outcome")+ for case in ("only_a_version_1_row", "a_version_2_row_covers_it")+ ),+ *(+ (f"{reader}_{case}", reader, case)+ for reader in ("latest_daily_point", "current_outcome")+ # After a return to version 1, newer version 2 rows of the same settings must not hide behind an+ # older version 1 row. Under version 2, a backfill of past days must not hide the newer end-date row.+ for case in ("rolled_back_with_a_newer_version_2_row", "a_newer_version_1_row_than_the_version_2_row")+ ),+ ]+ )+ def test_a_version_1_row_stands_in_only_where_no_version_2_row_covers_it(+ self, _name: str, reader: str, case: str+ ) -> None:+ experiment = self._experiment()+ calculation_config = get_metric_calculation_config(experiment, "m1")+ assert calculation_config is not None+ salt = _recalc_fingerprint if reader == "for_run" else (lambda key: key)+ version_1_window, version_2_window = {+ "rolled_back_with_a_newer_version_2_row": (_WINDOW - timedelta(days=1), _WINDOW),+ "a_newer_version_1_row_than_the_version_2_row": (_WINDOW, _WINDOW - timedelta(days=1)),+ }.get(case, (_WINDOW, _WINDOW))+ self._allow_rows_sharing_a_window()+ if case != "only_a_version_1_row":+ self._row(+ experiment,+ salt(calculation_config.key_for_version(2)),+ display_key=salt(calculation_config.legacy_key()),+ query_to=version_2_window,+ completed_at=version_2_window,+ samples=2,+ )+ # On a shared window the version 1 row is the newer write, so only the key family keeps it behind the+ # version 2 row.+ self._row(+ experiment,+ salt(calculation_config.legacy_key()),+ query_to=version_1_window,+ completed_at=version_1_window,+ samples=1,+ )+ active_version = 1 if case == "rolled_back_with_a_newer_version_2_row" else 2++ expected = [1] if case in ("only_a_version_1_row", "a_newer_version_1_row_than_the_version_2_row") else [2]+ assert self._read(reader, experiment, calculation_config.with_key_version(active_version)) == expected+def test_the_latest_window_inside_a_day_stands_for_that_day(self) -> None:experiment = self._experiment()calculation_config = get_metric_calculation_config(experiment, "m1")@@ -166,7 +255,9 @@ class TestMetricResultStore(BaseTest):experiment, key, query_to=_WINDOW - timedelta(hours=6), completed_at=_WINDOW + timedelta(hours=2), samples=1)- timeseries = MetricResultStore(experiment_id=experiment.id).timeseries("m1", key, timezone=ZoneInfo("UTC"))+ timeseries = MetricResultStore(experiment_id=experiment.id).timeseries(+ calculation_config, timezone=ZoneInfo("UTC")+ )assert list(timeseries.by_day) == [_WINDOW_DAY]assert _samples(timeseries.by_day[_WINDOW_DAY]) == [2]@@ -193,23 +284,231 @@ class TestMetricResultStore(BaseTest):assert self._read("current_outcome", experiment, calculation_config) == [2]- @parameterized.expand([("own_team", True), ("other_team", False)])- def test_previous_completed_metric_result_reads_only_the_callers_team(self, _name: str, own_team: bool) -> None:+ @parameterized.expand(+ [("without_a_current_row", False, 1), ("with_a_current_row_whose_data_ends_as_late", True, 2)]+ )+ def test_a_batch_outcome_ranks_each_experiment_by_its_own_keys(+ self, _name: str, tied_current_row: bool, expected_samples: int+ ) -> None:+ earlier_stats = {"method": "bayesian", "bayesian": {"ci_level": 0.9}}experiment = self._experiment()+ experiment.stats_config = earlier_stats+ experiment.save()+ earlier_config = get_metric_calculation_config(experiment, "m1")+ assert earlier_config is not None+ self._row(+ experiment,+ earlier_config.calculation_key(),+ display_key=earlier_config.legacy_key(),+ query_to=_WINDOW,+ completed_at=_WINDOW + timedelta(minutes=30),+ samples=1,+ )+ experiment.stats_config = {"method": "bayesian", "bayesian": {"ci_level": 0.99}}+ experiment.save()calculation_config = get_metric_calculation_config(experiment, "m1")assert calculation_config is not None- key = calculation_config.calculation_key()- self._row(experiment, key, query_to=_WINDOW, completed_at=_WINDOW, samples=7)+ if tied_current_row:+ # Its data ends at _WINDOW too, and the earlier row is the newer write, so only the experiment's own+ # current key breaks the tie.+ self._row(+ experiment,+ calculation_config.calculation_key(),+ query_to=_WINDOW + timedelta(hours=1),+ completed_at=_WINDOW,+ samples=2,+ )+ # Another experiment on the same flag and start date now has the first experiment's earlier settings.+ other = Experiment.objects.create(+ team=self.team,+ created_by=self.user,+ feature_flag=experiment.feature_flag,+ name="other",+ start_date=_START,+ metrics=experiment.metrics,+ stats_config=earlier_stats,+ )+ other_config = get_metric_calculation_config(other, "m1")+ assert other_config is not None and other_config.calculation_key() == earlier_config.calculation_key()++ outcome = MetricResultStore.current_outcomes({experiment.id: calculation_config, other.id: other_config})[+ experiment.id+ ]++ assert outcome.baseline_samples is not None+ assert int(outcome.baseline_samples) == expected_samples++ @parameterized.expand(+ [+ ("own_team", True, [("version_2", _WINDOW, 7)], 7),+ ("other_team", False, [("version_2", _WINDOW, 7)], None),+ ("only_a_point_from_before_the_conversion", True, [("version_1", _WINDOW, 7)], 7),+ ("only_a_point_under_earlier_settings", True, [("earlier_settings", _WINDOW, 7)], 7),+ (+ "the_newest_point_under_earlier_settings_wins",+ True,+ [("version_2", _WINDOW - timedelta(days=1), 5), ("earlier_settings", _WINDOW, 7)],+ 7,+ ),+ (+ "the_newest_point_under_the_other_key_version_wins",+ True,+ [("version_2", _WINDOW - timedelta(days=1), 5), ("version_1", _WINDOW, 7)],+ 7,+ ),+ ]+ )+ def test_previous_completed_metric_result(+ self, _name: str, own_team: bool, points: list[tuple[str, datetime, int]], expected_samples: int | None+ ) -> None:+ experiment = self._experiment()+ calculation_config = get_metric_calculation_config(experiment, "m1")+ assert calculation_config is not None+ for family, query_to, samples in points:+ fingerprint = {+ "version_2": calculation_config.calculation_key(),+ "version_1": calculation_config.legacy_key(),+ "earlier_settings": "key-of-the-earlier-team-settings",+ }[family]+ display_key = calculation_config.legacy_key() if family == "earlier_settings" else None+ self._row(+ experiment,+ fingerprint,+ display_key=display_key,+ query_to=query_to,+ completed_at=query_to,+ samples=samples,+ )result = previous_completed_metric_result(experiment.id,team_id=self.team.id if own_team else self.team.id + 1,metric_uuid="m1",- calculation_key=key,+ calculation_key=calculation_config.calculation_key(),before=_WINDOW + timedelta(days=1),)- assert result == (_stored_result(7) if own_team else None)+ assert result == (_stored_result(expected_samples) if expected_samples is not None else None)++ @parameterized.expand(+ [+ (f"{change}_{stopped}", change, history_shown, stopped)+ for change, history_shown in [("team", True), ("team_config", True), ("metric", False)]+ for stopped in [False, True]+ ]+ )+ def test_a_settings_change_keeps_the_history_that_the_experiment_still_describes(+ self, _name: str, change: str, history_shown: bool, stopped: bool+ ) -> None:+ experiment = self._experiment()+ if stopped:+ experiment.end_date = _WINDOW + timedelta(hours=1)+ experiment.save()+ calculation_config = get_metric_calculation_config(experiment, "m1")+ assert calculation_config is not None+ store = MetricResultStore(experiment_id=experiment.id)+ store.record_daily_point(+ "m1",+ calculation_config.calculation_key(),+ display_key=calculation_config.legacy_key(),+ window=_WINDOW,+ query_from=_START,+ result=_stored_result(3),+ )+ run_window = _WINDOW + timedelta(hours=1)+ run = ExperimentMetricsRecalculation.objects.create(+ team=self.team,+ experiment=experiment,+ metric_uuids=["m1"],+ query_to=run_window,+ status=ExperimentMetricsRecalculation.Status.IN_PROGRESS,+ )+ store.record_run_result(+ str(run.id),+ calculation_config,+ window=run_window,+ query_from=_START,+ result=_stored_result(4),+ query_id=None,+ )+ ExperimentMetricsRecalculation.objects.filter(id=run.id).update(+ status=ExperimentMetricsRecalculation.Status.COMPLETED, completed_at=django_timezone.now()+ )++ if change == "team":+ self.team.test_account_filters = [{"key": "email", "type": "person", "value": "@example.com"}]+ self.team.save()+ elif change == "team_config":+ config = get_or_create_team_extension(self.team, TeamExperimentsConfig)+ config.default_cuped_enabled = True+ config.save()+ else:+ experiment.metrics = [+ {+ "uuid": "m1",+ "kind": "ExperimentMetric",+ "metric_type": "mean",+ "source": {"kind": "EventsNode", "event": "signup"},+ }+ ]+ experiment.save()+ experiment = Experiment.objects.get(pk=experiment.pk)+ changed_config = get_metric_calculation_config(experiment, "m1")+ assert changed_config is not None and changed_config.calculation_key() != calculation_config.calculation_key()++ series = store.timeseries(changed_config, timezone=ZoneInfo("UTC"))+ outcome = store.current_outcome(changed_config)+ shown = (_samples(series.by_day.get(_WINDOW_DAY)), outcome and outcome.baseline_samples)+ assert shown == (([3], "4") if history_shown else ([], None))+ assert not store.has_completed(changed_config, window=run_window)+ stored_run = ExperimentMetricsRecalculation.objects.get(id=run.id)+ run_rows = store.for_run(stored_run)+ if stopped and history_shown:+ assert result_configuration_status(run_rows[0].fingerprint, changed_config) == "previous"+ assert [sample for row in run_rows for sample in _samples(row)] == ([4] if stopped and history_shown else [])+ assert store.latest_daily_point(changed_config, since=_WINDOW - timedelta(days=1), until=_WINDOW) is None+ if history_shown:+ assert series.configuration_by_day[_WINDOW_DAY] == "previous"+ assert outcome is not None and outcome.configuration_status == "previous"+ preserved = store.latest_daily_point(changed_config, since=_START, until=_WINDOW, preserve_history=stopped)+ assert _samples(preserved) == ([3] if stopped and history_shown else [])++ def test_legacy_result_is_displayable_but_not_reusable(self) -> None:+ experiment = self._experiment()+ config = get_metric_calculation_config(experiment, "m1")+ assert config is not None+ self._row(+ experiment, _recalc_fingerprint(config.legacy_key()), query_to=_WINDOW, completed_at=_WINDOW, samples=7+ )+ run = self._run(experiment, ExperimentMetricsRecalculation.Status.COMPLETED)+ [row] = MetricResultStore(experiment_id=experiment.id).for_run(run)+ assert (_samples(row), result_configuration_status(row.fingerprint, config)) == ([7], "unknown")+ assert not MetricResultStore(experiment_id=experiment.id).has_completed(config, window=_WINDOW)++ def test_daily_facades_use_the_already_resolved_display_key(self) -> None:+ experiment = self._experiment()+ config = get_metric_calculation_config(experiment, "m1")+ assert config is not None+ with self.assertNumQueries(6):+ record_daily_metric_result(+ experiment.id,+ metric_uuid="m1",+ calculation_key=config.calculation_key(),+ display_key=config.legacy_key(),+ window=_WINDOW,+ query_from=_START,+ result=_stored_result(3),+ )+ with self.assertNumQueries(2):+ result = previous_completed_metric_result(+ experiment.id,+ team_id=self.team.id,+ metric_uuid="m1",+ calculation_key=config.calculation_key(),+ display_key=config.legacy_key(),+ before=_WINDOW + timedelta(days=1),+ )+ assert result == _stored_result(3)def _run(self, experiment: Experiment, status: str) -> ExperimentMetricsRecalculation:return ExperimentMetricsRecalculation.objects.create(@@ -241,11 +540,35 @@ class TestMetricResultStore(BaseTest):query_id="q",)case "daily_point":- store.record_daily_point("m1", key, window=_WINDOW, query_from=_START, result=_stored_result(9))+ store.record_daily_point(+ "m1",+ key,+ display_key=calculation_config.legacy_key(),+ window=_WINDOW,+ query_from=_START,+ result=_stored_result(9),+ )case "daily_failure":- store.record_daily_failure("m1", key, window=_WINDOW, query_from=_START, error_message="boom")+ store.record_daily_failure(+ "m1",+ key,+ display_key=calculation_config.legacy_key(),+ window=_WINDOW,+ query_from=_START,+ error_message="boom",+ )+ case "daily_metric_result":+ record_daily_metric_result(+ experiment.id,+ metric_uuid="m1",+ calculation_key=key,+ display_key=calculation_config.legacy_key(),+ window=_WINDOW,+ query_from=_START,+ result=_stored_result(9),+ )case "sync_copy":- point = ExperimentMetricResult(result=_stored_result(9))+ point = ExperimentMetricResult(fingerprint=key, result=_stored_result(9))store.copy_into_sync_run(_WINDOW, [(calculation_config, point)], query_from=_START, completed_at=_WINDOW)@@ -257,6 +580,7 @@ class TestMetricResultStore(BaseTest):("run_result", True, ExperimentMetricResult.Status.COMPLETED),("run_failure", True, ExperimentMetricResult.Status.FAILED),("daily_point", False, ExperimentMetricResult.Status.COMPLETED),+ ("daily_metric_result", False, ExperimentMetricResult.Status.COMPLETED),("sync_copy", True, ExperimentMetricResult.Status.COMPLETED),])@@ -271,8 +595,11 @@ class TestMetricResultStore(BaseTest):self._write(writer, experiment, calculation_config)row = ExperimentMetricResult.objects.get(experiment=experiment, metric_uuid="m1", query_to=_WINDOW)- key = calculation_config.calculation_key()- assert row.fingerprint == (_recalc_fingerprint(key) if salted else key)+ salt = _recalc_fingerprint if salted else (lambda key: key)+ assert (row.fingerprint, row.display_key) == (+ salt(calculation_config.calculation_key()),+ salt(calculation_config.legacy_key()),+ )assert row.status == statusif status == ExperimentMetricResult.Status.COMPLETED:assert (row.result, row.error_message) == (_stored_result(9), None)@@ -341,3 +668,30 @@ class TestMetricResultStore(BaseTest):ExperimentMetricResult.Status.COMPLETED,None,)+++class TestCalculationKeyVersionDefault(BaseTest):+ def test_the_default_version_keeps_the_keys_and_the_query_cache_payload(self) -> None:+ flag = FeatureFlag.objects.create(team=self.team, created_by=self.user, key="default-version-flag")+ experiment = Experiment.objects.create(+ team=self.team,+ created_by=self.user,+ feature_flag=flag,+ name="default version",+ start_date=_START,+ metrics=[+ {+ "uuid": "m1",+ "kind": "ExperimentMetric",+ "metric_type": "mean",+ "source": {"kind": "EventsNode", "event": "$pageview"},+ }+ ],+ )+ calculation_config = get_metric_calculation_config(experiment, "m1")+ assert calculation_config is not None+ assert calculation_config.calculation_key() == calculation_config.legacy_key()+ assert experiment.metrics is not None+ query = ExperimentQuery(experiment_id=experiment.id, metric=build_metric(experiment.metrics[0]))+ # A new payload field would miss every cached experiment response when this release deploys.+ assert "calculation_key" not in ExperimentQueryRunner(query=query, team=self.team).get_cache_payload()diff --git a/products/experiments/backend/test/test_metrics_recalculation_api.py b/products/experiments/backend/test/test_metrics_recalculation_api.pyindex 930751feb4e3..4922f9ae54ca 100644--- a/products/experiments/backend/test/test_metrics_recalculation_api.py+++ b/products/experiments/backend/test/test_metrics_recalculation_api.py@@ -34,10 +34,10 @@ def _mean_metric(uuid: str) -> dict:}-def _calculation_key(experiment: Experiment, metric_uuid: str) -> str:+def _calculation_key(experiment: Experiment, metric_uuid: str, *, other_version: bool = False) -> str:calculation_config = get_metric_calculation_config(experiment, metric_uuid)assert calculation_config is not None- return calculation_config.calculation_key()+ return calculation_config.key_for_version(2) if other_version else calculation_config.calculation_key()class TestMetricsRecalculationAPI(APIBaseTest):@@ -327,38 +327,57 @@ class TestMetricsRecalculationAPI(APIBaseTest):assert body["status"] == "pending"assert body["active_run"] == {"id": str(active.id), "status": "pending"}- def test_get_latest_returns_most_recent_completed_with_results(self):+ @parameterized.expand(+ [+ ("current_keys", ("current", "current")),+ # A run under the other key version. On page load the frontend starts a run when a completed+ # run leaves a metric uncovered, so those rows must cover every metric of it.+ ("run_under_the_other_key_version", ("other_version", "other_version")),+ ("run_partly_recomputed_under_the_current_key", ("other_version", "current")),+ ]+ )+ def test_get_latest_returns_most_recent_completed_with_results(+ self, _name: str, key_families: tuple[str, str]+ ) -> None:exp = self._launched_experiment()+ exp.metrics = [_mean_metric("m1"), _mean_metric("m2")]+ exp.save()recalc = ExperimentMetricsRecalculation.objects.create(team=self.team,experiment=exp,- metric_uuids=["m1"],+ metric_uuids=["m1", "m2"],+ total_metrics=2,status="completed",query_to=datetime(2026, 6, 1, tzinfo=UTC),+ completed_at=timezone.now() - timedelta(minutes=5),)- # Narrow nullable fields populated above. _launched_experiment always sets metrics + start_date,+ # Narrow nullable fields populated above. _launched_experiment always sets start_date,# and we just set query_to on the recalc above.- assert exp.metrics and exp.start_date is not None+ assert exp.start_date is not Noneassert recalc.query_to is not None- recalc_fp = _recalc_fingerprint(_calculation_key(exp, "m1"))- ExperimentMetricResult.objects.create(- experiment=exp,- metric_uuid="m1",- fingerprint=recalc_fp,- query_from=exp.start_date,- query_to=recalc.query_to,- status="completed",- result={"ok": True},- )+ for metric_uuid, family in zip(("m1", "m2"), key_families):+ ExperimentMetricResult.objects.create(+ experiment=exp,+ metric_uuid=metric_uuid,+ fingerprint=_recalc_fingerprint(+ _calculation_key(exp, metric_uuid, other_version=family == "other_version")+ ),+ query_from=exp.start_date,+ query_to=recalc.query_to,+ status="completed",+ result={"ok": metric_uuid},+ )resp = self.client.get(self._latest_url(exp.id))assert resp.status_code == status.HTTP_200_OK, resp.contentbody = resp.json()assert body["id"] == str(recalc.id)assert body["status"] == "completed"- assert len(body["results"]) == 1- assert body["results"][0]["metric_uuid"] == "m1"- assert body["results"][0]["result"] == {"ok": True}+ assert body["completed_metrics"] + body["failed_metrics"] == body["total_metrics"] == 2+ assert [(result["metric_uuid"], result["result"]) for result in body["results"]] == [+ ("m1", {"ok": "m1"}),+ ("m2", {"ok": "m2"}),+ ]assert body.get("active_run") is None# ------------------------------------------------------------------@@ -397,23 +416,27 @@ class TestMetricsRecalculationAPI(APIBaseTest):# GET /metrics_recalculation/latest/ — timeseries cold-start fallback# ------------------------------------------------------------------- def _store_timeseries_point(self, exp: Experiment, metric_uuid: str, query_to: datetime) -> None:+ def _store_timeseries_point(+ self, exp: Experiment, metric_uuid: str, query_to: datetime, *, other_version: bool = False+ ) -> None:assert exp.start_date is not NoneExperimentMetricResult.objects.create(experiment=exp,metric_uuid=metric_uuid,- fingerprint=_calculation_key(exp, metric_uuid),+ fingerprint=_calculation_key(exp, metric_uuid, other_version=other_version),query_from=exp.start_date,query_to=query_to,status="completed",result={"ok": True},)+ @parameterized.expand([("current_point", False), ("point_under_the_other_key_version", True)])@time_machine.travel("2026-09-01T12:00:00Z", tick=False)- def test_get_latest_returns_timeseries_fallback_on_cold_start(self):+ def test_get_latest_returns_timeseries_fallback_on_cold_start(self, _name: str, other_version: bool) -> None:# No real recalc row, but a completed timeseries point exists → 200 with source=timeseries_fallback.+ # The frontend starts a cold run for any metric the fallback leaves out, so a version 1 point must cover it.exp = self._launched_experiment(flag_key="ts-fallback")- self._store_timeseries_point(exp, "m1", timezone.now() - timedelta(hours=1))+ self._store_timeseries_point(exp, "m1", timezone.now() - timedelta(hours=1), other_version=other_version)resp = self.client.get(self._latest_url(exp.id))assert resp.status_code == status.HTTP_200_OK, resp.content@@ -422,8 +445,8 @@ class TestMetricsRecalculationAPI(APIBaseTest):# The latest response type declares metric_retries; the fallback has no run, so it reports none.assert body["metric_retries"] == {}assert body["status"] == "completed"- assert len(body["results"]) == 1- assert body["results"][0]["result"] == {"ok": True}+ assert body["completed_metrics"] == body["total_metrics"] == 1+ assert [result["result"] for result in body["results"]] == [{"ok": True}]@time_machine.travel("2026-09-01T12:00:00Z", tick=False)def test_get_latest_prefers_timeseries_fallback_over_first_active_run(self):diff --git a/products/experiments/backend/test/test_presentation_api.py b/products/experiments/backend/test/test_presentation_api.pyindex 5975dc483e9f..951104ca9205 100644--- a/products/experiments/backend/test/test_presentation_api.py+++ b/products/experiments/backend/test/test_presentation_api.py@@ -5642,6 +5642,40 @@ class TestExperimentCRUD(_HoistFlagConfigClientMixin, APILicensedTest):metric_type = metric["metric_type"]self.assertEqual(metric["fingerprint"], expected_updated_fingerprints[metric_type])+ @parameterized.expand([("stale_client_fingerprint", "&fingerprint=" + "0" * 64), ("no_client_fingerprint", "")])+ def test_timeseries_results_reads_the_key_of_the_current_settings(self, _name: str, fingerprint_param: str) -> None:+ start = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=1)+ flag = FeatureFlag.objects.create(team=self.team, created_by=self.user, key="chart-key-flag")+ experiment = Experiment.objects.create(+ team=self.team,+ created_by=self.user,+ feature_flag=flag,+ name="Chart key",+ start_date=start,+ end_date=start + timedelta(hours=12),+ metrics=[{"kind": "ExperimentMetric", "metric_type": "mean", "uuid": "m1"}],+ )+ calculation_config = get_metric_calculation_config(experiment, "m1")+ assert calculation_config is not None+ ExperimentMetricResult.objects.create(+ experiment=experiment,+ metric_uuid="m1",+ fingerprint=calculation_config.calculation_key(),+ query_from=start,+ query_to=start + timedelta(days=1),+ status=ExperimentMetricResult.Status.COMPLETED,+ result={"variant_results": []},+ completed_at=start + timedelta(days=1),+ )++ response = self.client.get(+ f"/api/projects/{self.team.id}/experiments/{experiment.id}/timeseries_results/?metric_uuid=m1"+ f"{fingerprint_param}"+ )++ assert response.status_code == status.HTTP_200_OK, response.content+ assert response.json()["timeseries"] == {start.date().isoformat(): {"variant_results": []}}+def test_creating_draft_experiment_sets_status_draft(self):response = self.client.post(f"/api/projects/{self.team.id}/experiments/",diff --git a/products/experiments/backend/test/test_recalculation_service.py b/products/experiments/backend/test/test_recalculation_service.pyindex 956e1ff259b2..5591dbbe3900 100644--- a/products/experiments/backend/test/test_recalculation_service.py+++ b/products/experiments/backend/test/test_recalculation_service.py@@ -39,6 +39,7 @@ from products.experiments.backend.recalculation import (request_recalculation,request_recalculation_without_age_limit,)+from products.experiments.backend.test.utils import patch_calculation_key_versionfrom products.feature_flags.backend.models.feature_flag import FeatureFlag@@ -629,6 +630,10 @@ class TestRecalculationService(BaseTest):@pytest.mark.django_db(transaction=True)@time_machine.travel("2026-09-01T12:00:00Z", tick=False)class TestTimeseriesColdStartPayload(BaseTest):+ def setUp(self) -> None:+ super().setUp()+ self.enterContext(patch_calculation_key_version(2))+def _flag(self, key: str) -> FeatureFlag:return FeatureFlag.objects.create(team=self.team,@@ -653,7 +658,14 @@ class TestTimeseriesColdStartPayload(BaseTest):def _config_fp(self, exp: Experiment, metric_uuid: str) -> str:return _calculation_key(exp, metric_uuid)- def _timeseries_point(self, exp: Experiment, metric_uuid: str, query_to: datetime, result: dict | None) -> None:+ def _timeseries_point(+ self,+ exp: Experiment,+ metric_uuid: str,+ query_to: datetime,+ result: dict | None,+ completed_at: datetime | None = None,+ ) -> None:assert exp.start_date is not NoneExperimentMetricResult.objects.create(experiment=exp,@@ -663,6 +675,7 @@ class TestTimeseriesColdStartPayload(BaseTest):query_to=query_to,status="completed" if result is not None else "failed",result=result,+ completed_at=completed_at,)def test_returns_none_when_no_timeseries_data(self):@@ -702,26 +715,56 @@ class TestTimeseriesColdStartPayload(BaseTest):@parameterized.expand([- ("fresh_point", timedelta(hours=23), True),- ("stale_point", timedelta(hours=25), False),+ ("fresh_point", timedelta(hours=23), None, True),+ ("stale_point", timedelta(hours=25), None, False),# The backfill writes end-of-day points, so today's point can sit in the future.- ("future_point", timedelta(hours=-10), False),+ ("future_point", timedelta(hours=-10), None, False),+ # An ended experiment's point that reaches the end date is its final result, however old.+ ("ended_with_a_point_at_the_end_date", timedelta(days=3), timedelta(days=3), True),+ # A point before the end date misses the final hours, so a cold run must compute them.+ ("ended_with_a_stale_point_before_the_end_date", timedelta(days=4), timedelta(days=3), False),+ # A backfill stamps the end of the day it ran on, so its data stops at completed_at, before the end date.+ (+ "ended_with_a_backfill_point_computed_before_the_end_date",+ timedelta(days=3) - timedelta(hours=6),+ timedelta(days=3),+ False,+ timedelta(days=3) + timedelta(hours=8),+ ),])- def test_only_points_inside_the_max_age_feed_the_fallback(self, _name: str, age: timedelta, included: bool):+ def test_only_points_inside_the_max_age_feed_the_fallback(+ self,+ _name: str,+ age: timedelta,+ ended_ago: timedelta | None,+ included: bool,+ completed_ago: timedelta | None = None,+ ) -> None:exp = self._experiment(f"ts-age-{_name}", ["m1"])- self._timeseries_point(exp, "m1", timezone.now() - age, {"ok": True})+ if ended_ago is not None:+ exp.end_date = timezone.now() - ended_ago+ exp.save()+ completed_at = timezone.now() - completed_ago if completed_ago is not None else None+ self._timeseries_point(exp, "m1", timezone.now() - age, {"ok": True}, completed_at=completed_at)payload = build_timeseries_cold_start_payload(exp)assert (payload is not None) == included- def test_config_fingerprint_mismatch_yields_no_point(self):- exp = self._experiment("ts-drift", ["m1"])- # Store a point under a stale fingerprint, then change config so the recomputed fp won't match.+ @parameterized.expand(+ [+ # An exclusion changes both keys, so neither the current nor the version 1 key finds the point.+ ("excluded_variants", {"excluded_variants": ["test"]}),+ # The version 1 key ignores the credible interval, but the point carries the current key of the old+ # settings, so the version 1 fallback does not find it either.+ ("credible_interval", {"stats_config": {"bayesian": {"ci_level": 0.9}}}),+ ]+ )+ def test_a_settings_edit_after_the_point_yields_no_point(self, _name: str, changes: dict) -> None:+ exp = self._experiment(f"ts-drift-{_name}", ["m1"])self._timeseries_point(exp, "m1", timezone.now() - timedelta(hours=1), {"ok": True})- exp.exposure_criteria = {"filterTestAccounts": True}- exp.save()- assert build_timeseries_cold_start_payload(exp) is None+ Experiment.objects.filter(pk=exp.pk).update(**changes)+ assert build_timeseries_cold_start_payload(Experiment.objects.get(pk=exp.pk)) is None@pytest.mark.django_db(transaction=True)diff --git a/products/experiments/backend/test/test_setup_context.py b/products/experiments/backend/test/test_setup_context.pyindex 09306ba56f4a..6316c5e53a34 100644--- a/products/experiments/backend/test/test_setup_context.py+++ b/products/experiments/backend/test/test_setup_context.py@@ -80,10 +80,10 @@ def _retention_metric(uuid: str, start_event: str = "signup", completion_event:}-def _key(experiment: Experiment, metric_uuid: str) -> str:+def _key(experiment: Experiment, metric_uuid: str, *, other_version: bool = False) -> str:calculation_config = get_metric_calculation_config(experiment, metric_uuid)assert calculation_config is not None- return calculation_config.calculation_key()+ return calculation_config.key_for_version(2) if other_version else calculation_config.calculation_key()def _payload_of_each_section() -> dict[str, Any]:@@ -886,27 +886,43 @@ class TestPostgresSections(APIBaseTest):assert outcome.analyzed_exposures == 70assert outcome.result_data_through == now - timedelta(days=1)- def test_a_result_of_an_earlier_configuration_is_not_the_outcome(self) -> None:+ @parameterized.expand(+ [+ ("start_date_edited_after_the_result", "current", {"days": 12}, False),+ # A result under the other key version still describes the experiment.+ ("result_under_the_other_key_version", "other_version", None, True),+ ("result_under_the_current_key", "current", None, True),+ ]+ )+ def test_the_outcome_follows_the_calculation_key(+ self, _name: str, key_family: str, moved_start: dict[str, int] | None, has_outcome: bool+ ) -> None:now = timezone.now()experiment = self._experiment(- "edited-start", start_date=now - timedelta(days=10), metrics=[_mean_metric("inline-primary")]+ "keyed", start_date=now - timedelta(days=10), metrics=[_mean_metric("inline-primary")])ExperimentMetricResult.objects.create(experiment=experiment,metric_uuid="inline-primary",- fingerprint=_key(experiment, "inline-primary"),+ fingerprint=_key(experiment, "inline-primary", other_version=key_family == "other_version"),query_from=now - timedelta(days=10),query_to=now,status=ExperimentMetricResult.Status.COMPLETED,result=_stored_result(40, [40], False),completed_at=now,)- Experiment.objects.filter(pk=experiment.pk).update(start_date=now - timedelta(days=12))+ if moved_start is not None:+ Experiment.objects.filter(pk=experiment.pk).update(start_date=now - timedelta(**moved_start))previous = get_previous_experiments(Experiment.objects.filter(team_id=self.team.pk), limit=10)- assert previous.experiments[0].outcome is None- assert previous.summary.launched_without_results == 1+ outcome = previous.experiments[0].outcome+ if has_outcome:+ assert outcome is not None+ assert outcome.analyzed_exposures == 80+ else:+ assert outcome is None+ assert previous.summary.launched_without_results == 1def test_a_draft_does_not_crowd_out_a_launched_experiment(self) -> None:self._experiment("fresh-draft", days_ago=0)diff --git a/products/experiments/backend/test/test_timeseries_sync.py b/products/experiments/backend/test/test_timeseries_sync.pyindex db3992f370a1..2b3cdfabb1d4 100644--- a/products/experiments/backend/test/test_timeseries_sync.py+++ b/products/experiments/backend/test/test_timeseries_sync.py@@ -70,17 +70,19 @@ class TestSyncTimeseriesRecalculation(BaseTest):exp.save()return exp- def _config_fp(self, exp: Experiment, metric_uuid: str) -> str:+ def _config_fp(self, exp: Experiment, metric_uuid: str, *, other_version: bool = False) -> str:calculation_config = get_metric_calculation_config(exp, metric_uuid)assert calculation_config is not None- return calculation_config.calculation_key()+ return calculation_config.key_for_version(2) if other_version else calculation_config.calculation_key()- def _timeseries_point(self, exp: Experiment, metric_uuid: str, query_to: datetime, result: dict) -> None:+ def _timeseries_point(+ self, exp: Experiment, metric_uuid: str, query_to: datetime, result: dict, *, other_version: bool = False+ ) -> None:assert exp.start_date is not NoneExperimentMetricResult.objects.create(experiment=exp,metric_uuid=metric_uuid,- fingerprint=self._config_fp(exp, metric_uuid),+ fingerprint=self._config_fp(exp, metric_uuid, other_version=other_version),query_from=exp.start_date,query_to=query_to,status="completed",@@ -99,10 +101,11 @@ class TestSyncTimeseriesRecalculation(BaseTest):saved = ExperimentSavedMetric.objects.create(team=self.team, name=metric_uuid, query=_mean_metric(metric_uuid))ExperimentToSavedMetric.objects.create(experiment=exp, saved_metric=saved, metadata={"type": "primary"})- def test_creates_a_completed_run_from_the_points_of_this_daily_run(self):- exp = self._experiment("sync-full", [_mean_metric("m1"), _mean_metric("m2")])+ @parameterized.expand([("points_under_the_active_key", False), ("a_point_under_the_other_key_version", True)])+ def test_creates_a_completed_run_from_the_points_of_this_daily_run(self, _name: str, m2_other_version: bool):+ exp = self._experiment(f"sync-full-{_name}", [_mean_metric("m1"), _mean_metric("m2")])self._timeseries_point(exp, "m1", IN_RUN, {"m": 1})- self._timeseries_point(exp, "m2", LATER_IN_RUN, {"m": 2})+ self._timeseries_point(exp, "m2", LATER_IN_RUN, {"m": 2}, other_version=m2_other_version)recalculation_id = self._sync(exp)@@ -122,7 +125,7 @@ class TestSyncTimeseriesRecalculation(BaseTest):copies = ExperimentMetricResult.objects.filter(experiment=exp, query_to=recalc.query_to)assert {row.fingerprint for row in copies} == {_recalc_fingerprint(self._config_fp(exp, "m1")),- _recalc_fingerprint(self._config_fp(exp, "m2")),+ _recalc_fingerprint(self._config_fp(exp, "m2", other_version=m2_other_version)),}# The timeseries rows keep their config fingerprint.assert ExperimentMetricResult.objects.filter(experiment=exp, fingerprint=self._config_fp(exp, "m1")).exists()diff --git a/products/experiments/backend/test/utils.py b/products/experiments/backend/test/utils.pyindex aa42059ab748..8b5c47c9108c 100644--- a/products/experiments/backend/test/utils.py+++ b/products/experiments/backend/test/utils.py@@ -17,3 +17,11 @@ def patch_exposure_event_flag(*, enabled: bool) -> AbstractContextManager[Any]:return enabled and flag_key == EXPERIMENT_EXPOSURE_EVENT_FLAGreturn patch("posthoganalytics.feature_enabled", side_effect=fake_feature_enabled)+++def patch_calculation_key_version(key_version: int) -> AbstractContextManager[Any]:+ """Makes every new operation use this calculation key version, as the activation decision would."""+ return patch(+ "products.experiments.backend.metric_calculation.config.resolve_calculation_key_version",+ return_value=key_version,+ )diff --git a/products/experiments/frontend/generated/api.schemas.ts b/products/experiments/frontend/generated/api.schemas.tsindex ae2ca31c8774..922bffc9dc79 100644--- a/products/experiments/frontend/generated/api.schemas.ts+++ b/products/experiments/frontend/generated/api.schemas.ts@@ -5447,9 +5447,9 @@ export const ExperimentsRunningTimeTargetRetrieveMode = {export type ExperimentsTimeseriesResultsRetrieveParams = {/**- * Fingerprint of the metric configuration. Available alongside metric_uuid on each metric in the experiment's metrics array.+ * Ignored. The server derives the metric's calculation key from the experiment's current settings. Accepted so that existing clients keep working.*/- fingerprint: string+ fingerprint?: string/*** UUID of the metric to fetch timeseries for. Available on each metric in the experiment's metrics array.*/diff --git a/products/experiments/mcp/tools.yaml b/products/experiments/mcp/tools.yamlindex 7ae5aff24b23..261ac290badc 100644--- a/products/experiments/mcp/tools.yaml+++ b/products/experiments/mcp/tools.yaml@@ -1396,9 +1396,8 @@ tools:doing" almost always means, use experiment-metrics-recalculation-latest-retrieve instead.- Requires metric_uuid and fingerprint as query parameters (both available from the experiment's metrics array- — each metric has a uuid and the fingerprint is computed from its configuration), so you generally need to- fetch the experiment first.+ Requires metric_uuid as a query parameter, available on each metric in the experiment's metrics array, so+ you generally need to fetch the experiment first. The fingerprint parameter is ignored and can be left out.Returns a timeseries object keyed by date (YYYY-MM-DD) with per-variant statistical results for each day,diff --git a/services/mcp/schema/generated-tool-definitions.json b/services/mcp/schema/generated-tool-definitions.jsonindex 96200b0bce72..2d36452863c3 100644--- a/services/mcp/schema/generated-tool-definitions.json+++ b/services/mcp/schema/generated-tool-definitions.json@@ -6224,7 +6224,7 @@}},"experiment-timeseries-results": {- "description": "Only for the day-by-day history of a SINGLE metric — how its results moved over the course of the experiment. For an experiment's current results across all metrics, which is what \"how is this experiment doing\" almost always means, use experiment-metrics-recalculation-latest-retrieve instead.\n\nRequires metric_uuid and fingerprint as query parameters (both available from the experiment's metrics array — each metric has a uuid and the fingerprint is computed from its configuration), so you generally need to fetch the experiment first.\n\nReturns a timeseries object keyed by date (YYYY-MM-DD) with per-variant statistical results for each day, overall status (\"pending\", \"completed\", \"partial\", \"failed\"), computed_at timestamp, and recalculation_status if a recalculation is in progress.",+ "description": "Only for the day-by-day history of a SINGLE metric — how its results moved over the course of the experiment. For an experiment's current results across all metrics, which is what \"how is this experiment doing\" almost always means, use experiment-metrics-recalculation-latest-retrieve instead.\n\nRequires metric_uuid as a query parameter, available on each metric in the experiment's metrics array, so you generally need to fetch the experiment first. The fingerprint parameter is ignored and can be left out.\n\nReturns a timeseries object keyed by date (YYYY-MM-DD) with per-variant statistical results for each day, overall status (\"pending\", \"completed\", \"partial\", \"failed\"), computed_at timestamp, and recalculation_status if a recalculation is in progress.","category": "Experiments","feature": "experiments","summary": "Get experiment metric results over time",diff --git a/services/mcp/schema/tool-definitions-all.json b/services/mcp/schema/tool-definitions-all.jsonindex d5050c244280..f05fc40ef9e3 100644--- a/services/mcp/schema/tool-definitions-all.json+++ b/services/mcp/schema/tool-definitions-all.json@@ -6341,7 +6341,7 @@}},"experiment-timeseries-results": {- "description": "Only for the day-by-day history of a SINGLE metric — how its results moved over the course of the experiment. For an experiment's current results across all metrics, which is what \"how is this experiment doing\" almost always means, use experiment-metrics-recalculation-latest-retrieve instead.\n\nRequires metric_uuid and fingerprint as query parameters (both available from the experiment's metrics array — each metric has a uuid and the fingerprint is computed from its configuration), so you generally need to fetch the experiment first.\n\nReturns a timeseries object keyed by date (YYYY-MM-DD) with per-variant statistical results for each day, overall status (\"pending\", \"completed\", \"partial\", \"failed\"), computed_at timestamp, and recalculation_status if a recalculation is in progress.",+ "description": "Only for the day-by-day history of a SINGLE metric — how its results moved over the course of the experiment. For an experiment's current results across all metrics, which is what \"how is this experiment doing\" almost always means, use experiment-metrics-recalculation-latest-retrieve instead.\n\nRequires metric_uuid as a query parameter, available on each metric in the experiment's metrics array, so you generally need to fetch the experiment first. The fingerprint parameter is ignored and can be left out.\n\nReturns a timeseries object keyed by date (YYYY-MM-DD) with per-variant statistical results for each day, overall status (\"pending\", \"completed\", \"partial\", \"failed\"), computed_at timestamp, and recalculation_status if a recalculation is in progress.","category": "Experiments","feature": "experiments","summary": "Get experiment metric results over time",diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex 13aabb65b289..2d88e6e41e86 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -125789,9 +125789,9 @@ export namespace Schemas {export type ExperimentsTimeseriesResultsRetrieveParams = {/**- * Fingerprint of the metric configuration. Available alongside metric_uuid on each metric in the experiment's metrics array.+ * Ignored. The server derives the metric's calculation key from the experiment's current settings. Accepted so that existing clients keep working.*/- fingerprint: string;+ fingerprint?: string;/*** UUID of the metric to fetch timeseries for. Available on each metric in the experiment's metrics array.*/diff --git a/services/mcp/src/generated/experiments/api.ts b/services/mcp/src/generated/experiments/api.tsindex 131c5975e5d0..8a34db50f8e4 100644--- a/services/mcp/src/generated/experiments/api.ts+++ b/services/mcp/src/generated/experiments/api.ts@@ -19840,8 +19840,9 @@ export const ExperimentsTimeseriesResultsRetrieveParams = () => zod.object({export const ExperimentsTimeseriesResultsRetrieveQueryParams = () => zod.object({fingerprint: zod.string()+ .optional().describe(- "Fingerprint of the metric configuration. Available alongside metric_uuid on each metric in the experiment's metrics array."+ "Ignored. The server derives the metric's calculation key from the experiment's current settings. Accepted so that existing clients keep working."),metric_uuid: zod.string()diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/experiment-timeseries-results.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/experiment-timeseries-results.jsonindex fd0e4eeafe35..ca7520736baf 100644--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/experiment-timeseries-results.json+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/experiment-timeseries-results.json@@ -2,7 +2,7 @@"$schema": "https://json-schema.org/draft/2020-12/schema","properties": {"fingerprint": {- "description": "Fingerprint of the metric configuration. Available alongside metric_uuid on each metric in the experiment's metrics array.",+ "description": "Ignored. The server derives the metric's calculation key from the experiment's current settings. Accepted so that existing clients keep working.","type": "string"},"id": {@@ -14,6 +14,6 @@"type": "string"}},- "required": ["id", "fingerprint", "metric_uuid"],+ "required": ["id", "metric_uuid"],"type": "object"}