Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions api/experimentation/feature_state_metadata.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
from typing import TYPE_CHECKING, Any, Callable

from flagsmith_schemas.api import FeatureStateMetadata
from rest_framework.exceptions import ValidationError

from experimentation.models import Experiment, ExperimentStatus
from features.constants import LIVE_EXPERIMENT_MV_OPTIONS_MESSAGE

if TYPE_CHECKING: # pragma: no cover
from environments.models import Environment
from features.models import FeatureState


def get_live_experiment(
feature_id: int, environment_id: int | None = None
) -> Experiment | None:
# Multivariate options belong to the feature, not an environment, so an
# experiment in any environment locks them. A paused experiment counts:
# changing the variants before it resumes would skew its results.
experiments = Experiment.objects.filter(
feature_id=feature_id,
status__in=(ExperimentStatus.RUNNING, ExperimentStatus.PAUSED),
)
if environment_id is not None:
experiments = experiments.filter(environment_id=environment_id)
return experiments.select_related("environment").first() # type: ignore[no-any-return]


def live_experiment_error(experiment: Experiment) -> ValidationError:
return ValidationError(
{
"detail": LIVE_EXPERIMENT_MV_OPTIONS_MESSAGE.format(
environment=experiment.environment.name
)
}
)


def check_no_live_experiment(feature_id: int) -> None:
if experiment := get_live_experiment(feature_id):
raise live_experiment_error(experiment)


def get_feature_state_metadata_builder(
environment: "Environment",
) -> Callable[["FeatureState"], dict[str, Any] | None]:
Expand Down
5 changes: 4 additions & 1 deletion api/experimentation/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
from features.feature_states.models import API_VALUE_TYPES
from features.future.services import delete_segment_override, update_flag
from features.future.types import SegmentOverrideRequest
from features.models import FeatureState
from features.models import Feature, FeatureState
from features.value_types import BOOLEAN, STRING
from features.versioning.versioning_service import get_environment_flags_list
from integrations.flagsmith.client import get_openfeature_client
Expand Down Expand Up @@ -617,6 +617,9 @@ def transition_experiment_status(

experiment.status = target_status
if target_status == ExperimentStatus.RUNNING and not experiment.started_at:
Feature.objects.select_for_update().filter(
pk=experiment.feature_id
).exists()
Comment on lines +620 to +622

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Functional Correctness · Quick win

Code analysis · TLA+ model checking

Script executed:

rg -n 'VALID_STATUS_TRANSITIONS|started_at|ExperimentStatus.PAUSED|transition_experiment_status' api/experimentation; sed -n '605,632p' api/experimentation/services.py

Repository: Flagsmith/flagsmith

Length of output: 3106


tla executed:

---- MODULE ReviewModel ----
EXTENDS Naturals
(* Assumption to verify: resuming a paused experiment retains started_at,
   so the new feature lock is skipped. Writer phases represent its feature
   lock, live check, and save. A violation records a save while running.
TLC
INIT Init
NEXT Next
INVARIANT Safe
*)
VARIABLES live, owner, writer, resumed, violation
vars == <<live, owner, writer, resumed, violation>>
Init == /\ live = FALSE /\ owner = "none" /\ writer = "waiting"
        /\ resumed = FALSE /\ violation = FALSE
Check == /\ writer = "waiting" /\ owner = "none"
         /\ owner' = "writer"
         /\ writer' = (IF live THEN "rejected" ELSE "ready")
         /\ UNCHANGED <<live, resumed, violation>>
Resume == /\ ~resumed /\ live' = TRUE /\ resumed' = TRUE
          /\ UNCHANGED <<owner, writer, violation>>
Save == /\ writer = "ready" /\ writer' = "done"
        /\ owner' = "none" /\ violation' = live
        /\ UNCHANGED <<live, resumed>>
Reject == /\ writer = "rejected" /\ writer' = "done"
          /\ owner' = "none" /\ UNCHANGED <<live, resumed, violation>>
Idle == /\ writer = "done" /\ resumed /\ UNCHANGED vars
Next == Check \/ Resume \/ Save \/ Reject \/ Idle
Safe == ~violation
====

Length of output: 3972


Script executed:

git diff 13539afcb99c20b4351e26f215d01b49f2a9be03 d564c8d053d14408261767f8c48432435b3adc51 -- api/experimentation/services.py api/features/multivariate/views.py api/features/serializers.py
nl -ba api/features/multivariate/views.py | sed -n '1,180p'
nl -ba api/experimentation/models.py | sed -n '125,220p'
nl -ba api/experimentation/views.py | sed -n '445,515p;580,655p'
nl -ba api/experimentation/services.py | sed -n '570,645p'
rg -n -F -e 'has_live_experiment' -e 'live_experiment' -e 'transition_experiment_status(' -- api/experimentation api/features api/tests/unit/features/multivariate

Repository: Flagsmith/flagsmith

Length of output: 33851


Script executed:

nl -ba api/experimentation/feature_state_metadata.py | sed -n '1,130p'
rg -n -F -e 'def enable_experiment_rollout' -e 'def _enable_experiment_rollout' -e 'LIVE_EXPERIMENT' -- api/experimentation api/features
rg -n -F -e 'paused' -e 'PAUSED' -- api/tests/unit/features/multivariate/test_unit_multivariate_live_experiment.py api/tests/unit/experimentation

Repository: Flagsmith/flagsmith

Length of output: 4823


Script executed:

nl -ba api/experimentation/services.py | sed -n '1110,1235p'
nl -ba api/features/multivariate/models.py | sed -n '1,240p'
nl -ba api/features/multivariate/serializers.py | sed -n '1,220p'
rg -n -F -e 'multivariate_feature_option' -e 'multivariate_options' -- api/experimentation/services.py api/experimentation/serializers.py
nl -ba api/tests/unit/experimentation/test_experiment_views.py | sed -n '550,620p'
rg -n -F -e 'EXPERIMENTATION_ENABLED' -- api/app/settings/common.py api/experimentation/permissions.py

Repository: Flagsmith/flagsmith

Length of output: 24983


Script executed:

nl -ba api/experimentation/services.py | sed -n '400,465p;900,950p;990,1050p'
nl -ba api/experimentation/permissions.py | sed -n '1,80p'
nl -ba api/tests/unit/experimentation/test_experiment_views.py | sed -n '619,700p'
rg -n -F -e 'multivariate_feature_option.get' -e 'multivariate_feature_option.' -- api/features api/evaluation
rg -n -F -e 'EXPERIMENTATION_ENABLED' -- api/app/settings docker-compose.yml docker-compose.yaml helm charts 2>/dev/null || test "$?" -eq 1

Repository: Flagsmith/flagsmith

Length of output: 13459


Script executed:

rg -n -F -e 'def create_experiment_audit_log' -e 'class AuditLog(' -e 'def apply_experiment_rollout' -- api/experimentation api/audit
nl -ba api/experimentation/services.py | sed -n '515,575p;1030,1110p'
nl -ba api/features/versioning/schemas.py | sed -n '1,85p'
nl -ba docker-compose.yml | sed -n '20,45p'
rg -n -F -e 'get_experiment_rollout' -e '_expected_variant_shares' -- api/experimentation/services.py

Repository: Flagsmith/flagsmith

Length of output: 10425


Web query:

PostgreSQL foreign key update unchanged key skip referential integrity trigger RI_FKey_fk_upd_check_required select for key share

Result:

Sources consulted:
- postgres/src/backend/utils/adt/ri_triggers.c at master · postgres/postgres · GitHub — https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/ri_triggers.c
- PostgreSQL Source Code: src/backend/utils/adt/ri_triggers.c Source File — https://doxygen.postgresql.org/ri__triggers_8c_source.html
- PostgreSQL Source Code: src/backend/utils/adt/ri_triggers.c File Reference — https://doxygen.postgresql.org/ri__triggers_8c.html
- …and 27 more

Script executed:

nl -ba docker-compose.yml | sed -n '1,20p;44,70p'
rg -n -F -e 'postgres:' -e 'postgresql:' -- docker-compose.yml api/pyproject.toml api/Dockerfile

Repository: Flagsmith/flagsmith

Length of output: 2589


Script executed:

python3 - <<'PY'
import urllib.request
url='https://raw.githubusercontent.com/postgres/postgres/REL_15_5/src/backend/utils/adt/ri_triggers.c'
try:
    text=urllib.request.urlopen(url, timeout=15).read().decode()
except Exception as e:
    print('FETCH FAILED:',type(e).__name__,str(e))
else:
    lines=text.splitlines()
    for i,line in enumerate(lines):
        if line.startswith('RI_FKey_fk_upd_check_required('):
            print(url)
            for j in range(i-12, min(i+85,len(lines))):
                print(f'{j+1}: {lines[j]}')
            break
    else:
        print('Function not found')
PY

Repository: Flagsmith/flagsmith

Length of output: 3544


Lock the feature when resuming an experiment.

If an option update passes its check while an experiment is paused, a concurrent resume can commit before the update. The option change can then commit while the experiment is running. Acquire the feature lock for every transition to RUNNING.

experiment.started_at = timezone.now()
_remove_superseded_experiment_rollouts(
experiment, AuthorData.from_user(user)
Expand Down
4 changes: 4 additions & 0 deletions api/features/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
# Multivariate variant reported when an identity falls through to the control value
CONTROL_VARIANT_KEY = "control"
RESERVED_VARIANT_KEY_MESSAGE = f'"{CONTROL_VARIANT_KEY}" is a reserved variant key.'
LIVE_EXPERIMENT_MV_OPTIONS_MESSAGE = (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DRF will return this error in three different shapes: a bare list from the view, and both a list and a string under multivariate_options from the serializer. None say which environment is running the experiment.

Could we use one shape, {"detail": "<message>"} like the experiment endpoints, and name the environment in the message?

'Variations cannot be changed while an experiment is running or paused in the "{environment}" '
"environment. Complete the experiment to change them."
)

# Tag filtering strategy
UNION = "UNION"
Expand Down
29 changes: 28 additions & 1 deletion api/features/import_export/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
register_task_handler,
)

from environments.models import Environment
from experimentation.feature_state_metadata import get_live_experiment
from features.import_export.constants import (
FAILED,
PROCESSING,
Expand Down Expand Up @@ -153,7 +155,12 @@ def _import_features_for_environment(feature_import: FeatureImport) -> None:
project=project,
).first()

if existing_feature and feature_import.strategy == SKIP:
if existing_feature and (
feature_import.strategy == SKIP
or _should_skip_for_live_experiment(

@cubic-dev-ai cubic-dev-ai Bot Nov 14, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: This per-row check can skip a conflicting feature after earlier rows have already been overwritten, then mark the import SUCCESS, leaving a partial import that looks complete. Preflight the full payload and fail before applying any rows when it contains a locked variant mutation.

(Based on your team's feedback about preflighting imports with experiment conflicts.)

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At api/features/import_export/tasks.py, line 160:

<comment>This per-row check can skip a conflicting feature after earlier rows have already been overwritten, then mark the import `SUCCESS`, leaving a partial import that looks complete. Preflight the full payload and fail before applying any rows when it contains a locked variant mutation.

(Based on your team's feedback about preflighting imports with experiment conflicts.) </comment>

<file context>
@@ -153,7 +155,12 @@ def _import_features_for_environment(feature_import: FeatureImport) -> None:
-        if existing_feature and feature_import.strategy == SKIP:
+        if existing_feature and (
+            feature_import.strategy == SKIP
+            or _should_skip_for_live_experiment(
+                feature_data, existing_feature, environment
+            )
</file context>
Fix with cubic

feature_data, existing_feature, environment
)
):
continue

if existing_feature is None:
Expand All @@ -166,6 +173,26 @@ def _import_features_for_environment(feature_import: FeatureImport) -> None:
feature_import.save()


def _should_skip_for_live_experiment(
feature_data: FeatureExportData,
existing_feature: Feature,
environment: Environment,
) -> bool:
if get_live_experiment(existing_feature.id, environment.id):
return True
if not feature_data["multivariate"]:
return False
existing_variations = {
(option.type, option.value)
for option in existing_feature.multivariate_options.all()
}
adds_variation = any(
(mv_data["type"], mv_data["value"]) not in existing_variations
for mv_data in feature_data["multivariate"]
)
return bool(adds_variation and get_live_experiment(existing_feature.id))

@cubic-dev-ai cubic-dev-ai Bot Nov 14, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: The import checks for a live experiment without locking the feature, then can create a variation after a concurrent start commits. Hold the feature-row lock across the experiment check and the import write so the start transition and import serialize.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At api/features/import_export/tasks.py, line 193:

<comment>The import checks for a live experiment without locking the feature, then can create a variation after a concurrent start commits. Hold the feature-row lock across the experiment check and the import write so the start transition and import serialize.</comment>

<file context>
@@ -166,6 +173,26 @@ def _import_features_for_environment(feature_import: FeatureImport) -> None:
+        (mv_data["type"], mv_data["value"]) not in existing_variations
+        for mv_data in feature_data["multivariate"]
+    )
+    return bool(adds_variation and get_live_experiment(existing_feature.id))
+
+
</file context>
Fix with cubic



# Should only run on official flagsmith instance.
if (
settings.FLAGSMITH_ON_FLAGSMITH_FEATURE_EXPORT_ENVIRONMENT_ID
Expand Down
64 changes: 63 additions & 1 deletion api/features/multivariate/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,33 @@
CREATE_FEATURE,
VIEW_PROJECT,
)
from django.db import transaction
from django.utils.decorators import method_decorator
from drf_spectacular.utils import extend_schema
from rest_framework import viewsets
from rest_framework.decorators import api_view
from rest_framework.exceptions import ValidationError
from rest_framework.generics import get_object_or_404
from rest_framework.response import Response

from experimentation.feature_state_metadata import check_no_live_experiment
from features.models import Feature
from projects.permissions import NestedProjectPermissions

from .models import MultivariateFeatureOption
from .serializers import MultivariateFeatureOptionSerializer

# What makes a variation. `default_percentage_allocation` is left out: the
# dashboard always sends it as 0, and it only seeds new environments.
VARIATION_FIELDS = (
"type",
"string_value",
"integer_value",
"boolean_value",
"key",
"feature",
)


@method_decorator(
name="list",
Expand Down Expand Up @@ -69,9 +83,57 @@ def get_permissions(self): # type: ignore[no-untyped-def]
def create(self, request, *args, **kwargs): # type: ignore[no-untyped-def]
feature_pk = self.kwargs.get("feature_pk")
project_pk = self.kwargs.get("project_pk")
get_object_or_404(Feature.objects.filter(project__id=project_pk), pk=feature_pk)
url_feature = get_object_or_404(
Feature.objects.filter(project__id=project_pk), pk=feature_pk
)
if "feature" in request.data and str(request.data.get("feature")) != str(
url_feature.id
):
raise ValidationError({"feature": "Feature must match the URL."})
return super().create(request, *args, **kwargs)

def perform_create(self, serializer): # type: ignore[no-untyped-def]
url_feature = get_object_or_404(
Feature.objects.filter(project__id=self.kwargs["project_pk"]),
pk=self.kwargs["feature_pk"],
)
if serializer.validated_data["feature"] != url_feature:
raise ValidationError({"feature": "Feature must match the URL."})
with transaction.atomic():
Feature.objects.select_for_update().filter(pk=url_feature.pk).exists()
check_no_live_experiment(url_feature.id)
super().perform_create(serializer)

def perform_update(self, serializer): # type: ignore[no-untyped-def]
instance = serializer.instance
data = serializer.validated_data
# The dashboard PUTs every option on each save, changed or not, so only
# a change to the variation itself is checked against experiments.
if not any(
field in data and data[field] != getattr(instance, field)
for field in VARIATION_FIELDS
):
super().perform_update(serializer)
Comment on lines +112 to +116

@coderabbitai coderabbitai Bot Nov 12, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major · Data Integrity & Integration · Heavy lift

Lock and reload the option before taking the unchanged-update path.

Suppose an unchanged PUT loads value A. Another request changes the option to B, and an experiment then starts. The first PUT still compares A with its cached instance, takes this branch, and saves A without checking the now-running experiment. The dashboard’s unchanged-option saves can therefore change a live variation. Lock the feature and reload the option before deciding whether the submitted fields changed. DRF’s model update saves the supplied instance after setting its validated fields. (raw.githubusercontent.com)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong qualification of major, kind of rare scenario

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Zaimwa9 I agree. The race requires a narrow overlap, so Minor is a better qualification than Major.

The race could still overwrite a variation after an experiment starts. I’ll leave the decision on whether to address it to you. I’m not marking the finding as addressed.

You are interacting with an AI system.

@cubic-dev-ai cubic-dev-ai Bot Nov 14, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: This fast path saves without locking or reloading the option, so its “unchanged” comparison can become stale. A concurrent variant edit followed by experiment start lets this PUT overwrite the variant during the live run; recheck against locked current state before bypassing the experiment guard.

(Based on your team's feedback about locking experiment variants.)

View Feedback

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At api/features/multivariate/views.py, line 116:

<comment>This fast path saves without locking or reloading the option, so its “unchanged” comparison can become stale. A concurrent variant edit followed by experiment start lets this PUT overwrite the variant during the live run; recheck against locked current state before bypassing the experiment guard.

(Based on your team's feedback about locking experiment variants.) </comment>

<file context>
@@ -69,9 +83,57 @@ def get_permissions(self):  # type: ignore[no-untyped-def]
+            field in data and data[field] != getattr(instance, field)
+            for field in VARIATION_FIELDS
+        ):
+            super().perform_update(serializer)
+            return
+
</file context>
Fix with cubic

return

features = {instance.feature, data.get("feature", instance.feature)}

@coderabbitai coderabbitai Bot Nov 12, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major · Security & Privacy · Quick win

Authorise the replacement feature before moving an option.

If a requester can edit an option in project A, the requester can supply a feature ID from project B without permission to edit project B. The new branch checks that target for experiments, but neither it nor the source-object permission check authorises the target. The update can attach the option to project B. Require permission on the target project, or reject cross-project moves. The serializer’s relation uses the related model’s default manager. (raw.githubusercontent.com)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of scope, it's already existing and should be addressed as a follow up

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Zaimwa9 Understood. We can address the authorisation concern in a follow-up. Would you like me to open a GitHub issue for it?

I’m not marking the finding as addressed.

You are interacting with an AI system.

@cubic-dev-ai cubic-dev-ai Bot Nov 14, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: This feature set can use the option’s stale source association: after a concurrent reassignment from A to B, the lock set can omit B while the save mutates the option now attached to B. perform_destroy also locks stale instance.feature_id, allowing a live variant to be deleted; lock and reload the option before checking its current feature.

(Based on your team's feedback about locking experiment variants.)

View Feedback

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At api/features/multivariate/views.py, line 119:

<comment>This feature set can use the option’s stale source association: after a concurrent reassignment from A to B, the lock set can omit B while the save mutates the option now attached to B. `perform_destroy` also locks stale `instance.feature_id`, allowing a live variant to be deleted; lock and reload the option before checking its current feature.

(Based on your team's feedback about locking experiment variants.) </comment>

<file context>
@@ -69,9 +83,57 @@ def get_permissions(self):  # type: ignore[no-untyped-def]
+            super().perform_update(serializer)
+            return
+
+        features = {instance.feature, data.get("feature", instance.feature)}
+        with transaction.atomic():
+            list(
</file context>
Fix with cubic

with transaction.atomic():
list(
Feature.objects.select_for_update()
.filter(pk__in=[feature.pk for feature in features])
.order_by("pk")
.values_list("pk", flat=True)
)
for feature in features:
check_no_live_experiment(feature.id)
super().perform_update(serializer)
Comment on lines +107 to +129

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the work on this, the direction is right: variants are shared across the project, so adding, deleting or changing a variant's value or key should be blocked while an experiment is running.

There is one case we need to keep working, though. Users must still be able to change the flag's value and variant percentages in the other environments, where no experiment is running.
How it behaves today:

  1. Start an experiment on a multivariate flag in environment A.
  2. In environment B, toggle the flag or change a variant weight and save.
  3. The save fails with "Multivariate options cannot be changed while the feature is in a live experiment."

The cause is that the dashboard sends a PUT mv-options/{id}/ for every existing option on each save, even when nothing changed. perform_update rejects every PUT, so the environment-level save that follows never runs.

Could you make perform_update reject only real changes?

  • Compare type, string_value, integer_value, boolean_value, key and feature against the stored option, and only raise if one of them differs.
  • Leave default_percentage_allocation out of the comparison. The dashboard always sends it as 0, and it only seeds new environments, so it does not affect a running experiment.

Create and delete stay blocked as they are.

Here are some test ideas that could help implementing the behavior imo.

Test What it proves
test_update_mv_option__running_experiment_unchanged_payload__returns_200 A PUT with the stored values, sent the way the dashboard sends it (default_percentage_allocation: 0), is not treated as an edit.
test_update_mv_option__running_experiment_default_allocation_only__returns_200 Changing only default_percentage_allocation is allowed, and the option's value and key are untouched.
test_update_mv_option__running_experiment_changed_field__returns_400 Parametrised over string_value and key: a real change to a variant is still rejected and nothing is saved. This replaces the current test_update_mv_option__live_experiment__returns_400.
test_update_mv_option__experiment_not_running__returns_200 Parametrised over created, and completed: only a running experiment locks the variants.
test_save_feature_value__experiment_running_in_other_environment__returns_200 Replays the dashboard's save in environment B while an experiment runs in environment A: a PUT mv-options/{id}/ for each option with its variant value and key unchanged, then the feature state update with a new control value and new variant percentages for environment B. Both succeed, environment B stores the new control value and percentages, and environment A's are unchanged.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is another related change to do here.

We need to lock it on a PAUSED experiment too, because once the experimentation resumes, if the variants have changed, all the results and values identities will receive will be skewed.

We will ask the users to complete experiments if they need to change the variants so in the code that means:

  • has_live_experiment should match both RUNNING and PAUSED and for the tests PAUSED behaves the same as running


def perform_destroy(self, instance): # type: ignore[no-untyped-def]
with transaction.atomic():
Feature.objects.select_for_update().filter(pk=instance.feature_id).exists()
check_no_live_experiment(instance.feature_id)
super().perform_destroy(instance)

def get_queryset(self): # type: ignore[no-untyped-def]
if getattr(self, "swagger_fake_view", False):
return MultivariateFeatureOption.objects.none()
Expand Down
43 changes: 42 additions & 1 deletion api/features/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
FeatureStateValueSerializer,
)
from common.projects.permissions import VIEW_PROJECT
from django.db import models
from django.db import models, transaction
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import extend_schema_field
from drf_writable_nested import ( # type: ignore[attr-defined]
Expand All @@ -31,6 +31,8 @@
from evaluation.types import EvaluatedFeatureState
from experimentation.feature_state_metadata import (
get_feature_state_metadata_builder,
get_live_experiment,
live_experiment_error,
)
from integrations.vcs.services import dispatch_vcs_on_feature_state_changed
from metadata.serializers import MetadataSerializer, MetadataSerializerMixin
Expand Down Expand Up @@ -307,6 +309,19 @@ def validate_project_features_limit(self, project: Project) -> None:
}
)

def update(self, instance: Feature, validated_data: dict[str, Any]) -> Feature:
with transaction.atomic():
Feature.objects.select_for_update().filter(pk=instance.pk).exists()
if "multivariate_options" in validated_data and (
experiment := get_live_experiment(instance.id)
):
if self._changes_multivariate_options(
validated_data["multivariate_options"], instance
):
raise live_experiment_error(experiment)
validated_data.pop("multivariate_options")
return super().update(instance, validated_data) # type: ignore[no-any-return]

def validate_multivariate_options(self, multivariate_options): # type: ignore[no-untyped-def]
if multivariate_options:
user = self.context["request"].user
Expand All @@ -326,6 +341,32 @@ def validate_multivariate_options(self, multivariate_options): # type: ignore[n
raise serializers.ValidationError("Invalid percentage allocation")
return multivariate_options

def _changes_multivariate_options(
self,
multivariate_options: list[dict[str, Any]] | None,
instance: Feature,
) -> bool:
# The frontend resubmits the whole feature, so only a real change to
# variation value/type counts. `default_percentage_allocation` only

@cubic-dev-ai cubic-dev-ai Bot Nov 17, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: A feature PATCH that changes only default_percentage_allocation now passes this comparison, but update() then removes all multivariate_options before saving, so the successful response silently leaves the allocation unchanged. Persist the permitted allocation edits while skipping variant changes; otherwise new environments inherit the old allocation.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At api/features/serializers.py, line 350:

<comment>A feature PATCH that changes only `default_percentage_allocation` now passes this comparison, but `update()` then removes all `multivariate_options` before saving, so the successful response silently leaves the allocation unchanged. Persist the permitted allocation edits while skipping variant changes; otherwise new environments inherit the old allocation.</comment>

<file context>
@@ -347,13 +347,13 @@ def _changes_multivariate_options(
     ) -> bool:
         # The frontend resubmits the whole feature, so only a real change to
-        # writable option fields counts. Compare on content, not ids.
+        # variation value/type counts. `default_percentage_allocation` only
+        # seeds new options or environments and does not affect live variants.
         fields = (
</file context>
Fix with cubic

# seeds new options or environments and does not affect live variants.
fields = (
"type",
"integer_value",
"string_value",
"boolean_value",
)
current = NestedMultivariateFeatureOptionSerializer(
instance.multivariate_options.all(), many=True
).data

def signature(options) -> list[tuple[Any, ...]]: # type: ignore[no-untyped-def]
return sorted(
(tuple(option.get(field) for field in fields) for option in options),
key=repr,
)

return signature(current) != signature(multivariate_options or [])

def validate_owners(self, owners: list[FFAdminUser]) -> list[FFAdminUser]:
project: Project = self.context["project"]
invalid_users = [
Expand Down
Loading
Loading