Skip to content

fix(api): block multivariate option changes during live experiments - #8917

Open
TejasLamba2006 wants to merge 8 commits into
Flagsmith:mainfrom
TejasLamba2006:fix/restrict-mv-options-in-live-experiment
Open

TejasLamba2006 wants to merge 8 commits into
Flagsmith:mainfrom
TejasLamba2006:fix/restrict-mv-options-in-live-experiment

Conversation

@TejasLamba2006

Copy link
Copy Markdown

Thanks for submitting a PR! Please check the boxes below:

  • I have read the Contributing Guide.
  • I have added information to docs/ if required so people know about the feature.
  • I have filled in the "Changes" section below.
  • I have filled in the "How did you test this code" section below.

Changes

Contributes to #8766

The API now rejects multivariate option changes on a feature that is in a running experiment:

  • MultivariateFeatureOptionViewSet create, update and destroy return 400 while the feature has a running experiment in any environment. Multivariate options belong to the feature, so one experiment locks them everywhere.
  • Feature create/update: the nested multivariate_options are rejected only when the submitted options differ from the stored ones. The frontend resubmits the whole feature on save, so other fields (description, tags, etc.) still save on a frozen feature.
  • New helper has_live_experiment in experimentation/feature_state_metadata.py.

Frontend: the create-feature modal already disables the add, edit and remove variant controls when the feature is frozen (useFeatureExperimentFreeze). That check only looks at experiments running in the current environment. Blocking variant edits from the UI for a feature running in another environment needs a project-level experiments query, which is not part of this PR. The API rejects those edits regardless.

How did you test this code?

Backend unit tests:

cd api
pytest tests/unit/features/multivariate/test_unit_multivariate_live_experiment.py
pytest tests/unit/features/ -k "multivariate or mv or feature_serializer or create_feature or update_feature"

The new test module covers create, update and delete of MV options during a live experiment (400), the same create still working without one, and feature PATCH with changed options (400) versus unchanged options plus a metadata change (200). The existing multivariate and feature suites pass.

@TejasLamba2006
TejasLamba2006 requested a review from a team as a code owner November 9, 2026 16:45
@TejasLamba2006
TejasLamba2006 requested review from emyller and removed request for a team November 9, 2026 16:45
@vercel

vercel Bot commented Nov 9, 2026

Copy link
Copy Markdown

@TejasLamba2006 is attempting to deploy a commit to the Flagsmith Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Explain Change →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

The change adds checks that prevent multivariate variation changes while an experiment is running or paused. The checks cover direct option operations, feature updates, and environment imports. Transactions and feature-row locks are added to relevant write paths, and starting an experiment locks its feature row. Tests cover rejected and permitted operations, imports, and cross-environment cases. Documentation updates the experiment prerequisite and six event source line references.

Estimated code review effort: 4 (Complex) | ~35 minutes

Merge Risk: Low · previous review 8682e

The change blocks multivariate option edits during running or paused experiments, and its main paths are covered by tests. Some narrow race windows and a pre-existing cross-project authorization gap remain. These are bounded concerns and are worth owner awareness rather than blocking the merge.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 1fcd5ea6-ea04-4540-b8d3-ff5767eb5c94
Commits

Reviewing files that changed from the base of the PR and between a91b390 and 8682e20.

Files selected for processing: 3

  • api/experimentation/feature_state_metadata.py
  • api/features/import_export/tasks.py
  • api/tests/unit/features/import_export/test_unit_features_import_export_tasks.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 4

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 8a697a32-dc99-42e6-a773-d976a4f84f00
Commits

Reviewing files that changed from the base of the PR and between 13539af and ffd2b0b.

Files selected for processing: 5

  • api/experimentation/feature_state_metadata.py
  • api/features/constants.py
  • api/features/multivariate/views.py
  • api/features/serializers.py
  • api/tests/unit/features/multivariate/test_unit_multivariate_live_experiment.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.

Comment thread api/features/multivariate/views.py Resolved
Comment thread api/features/multivariate/views.py Resolved
Comment thread api/features/serializers.py Resolved
Comment thread api/features/serializers.py Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 4

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 8a0c27d7-d8b4-4329-8549-44983957b743
Commits

Reviewing files that changed from the base of the PR and between ffd2b0b and 5b4a677.

Files selected for processing: 6

  • api/experimentation/feature_state_metadata.py
  • api/features/import_export/services.py
  • api/features/multivariate/views.py
  • api/features/serializers.py
  • api/tests/unit/features/import_export/test_unit_features_import_export_tasks.py
  • api/tests/unit/features/multivariate/test_unit_multivariate_live_experiment.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 1 remain after this review.

Comment thread api/features/import_export/services.py Resolved
Comment thread api/features/import_export/services.py Resolved
Comment thread api/features/multivariate/views.py Resolved
Comment thread api/features/serializers.py Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 5

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: bff86148-a732-4af1-85f1-7eebf38db057
Commits

Reviewing files that changed from the base of the PR and between 5b4a677 and d564c8d.

Files selected for processing: 7

  • api/experimentation/services.py
  • api/features/import_export/services.py
  • api/features/import_export/tasks.py
  • api/features/multivariate/views.py
  • api/features/serializers.py
  • api/tests/unit/features/import_export/test_unit_features_import_export_tasks.py
  • api/tests/unit/features/multivariate/test_unit_multivariate_live_experiment.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.

Comment thread api/experimentation/services.py Resolved
Comment thread api/features/import_export/services.py Resolved
Comment thread api/features/serializers.py Resolved

@Zaimwa9 Zaimwa9 left a comment

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.

First of all, thanks for picking this one, that is a very complex one given the model of the MV options and their weight allocation (mix between project and environment level). It also implies some design decisions that are still under discussions

There are some blockers right now, the main changes we need to move it forward for now would be:

  • Unchanged updates to a variant must not be rejected, otherwise saving the flag in another environment breaks
  • The lock should cover paused experiments as well as running ones
  • The import guard is too wide, and the new transaction changes the behaviour of every import
  • The error should come back in one shape and name the environment running the experiment

I described some tests that should help implementing the first point.

This behavior could also be rewarded with a one-line mention/warning in the doc 👍

Thanks again!

Comment thread api/features/multivariate/views.py Resolved
Comment thread api/features/constants.py Resolved
Comment thread api/features/import_export/tasks.py Resolved
Comment thread api/features/import_export/services.py Resolved
Comment thread api/features/serializers.py Resolved
@Zaimwa9
Zaimwa9 removed the request for review from emyller November 11, 2026 11:33
@TejasLamba2006
TejasLamba2006 requested review from a team as code owners November 12, 2026 06:36
@TejasLamba2006
TejasLamba2006 requested review from Holmus and removed request for a team November 12, 2026 06:36
@github-actions github-actions Bot added api Issue related to the REST API docs Documentation updates labels Nov 12, 2026
@TejasLamba2006

Copy link
Copy Markdown
Author

@Zaimwa9 thanks for the detailed review, the test table made the first point easy to pin down. Pushed a91b390:

  • perform_update only checks for an experiment when the type, value, key or feature of the variation actually changes. default_percentage_allocation is left out, and create and delete stay blocked. The tests from your table are in, including the replay of the dashboard save in environment B while A runs the experiment.
  • Paused experiments lock the variations too. The live_experiment fixture runs every test for both running and paused.
  • _import_features_for_environment is back to how it was. A first pass over the import rejects it before anything is written, and only when it would add a variation or when the target is the experiment's environment. Importing existing variations into another environment goes through.
  • The error is {"detail": "..."} everywhere and names the environment.
  • The check in validate_multivariate_options is gone, only update checks now.
  • Added a sentence about the lock to the Create an Experiment page.

Could you take another look when you get a chance?

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 4

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: af3e240a-f26f-4022-a0c8-b9a0a4bae9db
Commits

Reviewing files that changed from the base of the PR and between d564c8d and a91b390.

Files selected for processing: 9

  • api/experimentation/feature_state_metadata.py
  • api/features/constants.py
  • api/features/import_export/tasks.py
  • api/features/multivariate/views.py
  • api/features/serializers.py
  • api/tests/unit/features/import_export/test_unit_features_import_export_tasks.py
  • api/tests/unit/features/multivariate/test_unit_multivariate_live_experiment.py
  • docs/docs/deployment-self-hosting/observability/_events-catalogue.md
  • docs/docs/experimentation/create-an-experiment.md

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Comment thread api/features/import_export/tasks.py Resolved
Comment thread api/features/import_export/tasks.py Resolved
Comment thread api/features/multivariate/views.py Resolved
Comment thread api/features/multivariate/views.py Resolved

@Zaimwa9 Zaimwa9 left a comment

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 fast changes. However we had to re-think the behavior around destructive imports in an environment with features running an experiment, which would be great to adapt.

👍

Comment thread api/features/multivariate/views.py Resolved
Comment thread api/features/import_export/tasks.py Resolved
Comment thread api/features/multivariate/views.py Resolved
@TejasLamba2006

Copy link
Copy Markdown
Author

@Zaimwa9 updated in 8682e20: get_live_experiment now takes an optional environment_id, and destructive imports skip features that have a running or paused experiment in the target environment (including when another environment also has an experiment) or would add a variation while an experiment is active anywhere, letting the rest of the batch import.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 10 files

Tip: cubic used a learning from your PR history. Let your coding agent read cubic learnings directly with the cubic MCP.

View guided diff | Re-trigger cubic

Comment thread api/features/multivariate/views.py Resolved
Comment thread api/features/multivariate/views.py Resolved
Comment thread api/features/import_export/tasks.py Resolved
Comment thread api/features/import_export/tasks.py Resolved
Comment thread api/features/serializers.py Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 2 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread api/features/serializers.py Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 3 files (changes from recent commits).

Requires human review: Auto-approval blocked because this review re-detected 1 unresolved issue already reported by Cubic.

Re-trigger cubic

Comment thread api/features/serializers.py Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 3 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread api/features/serializers.py Resolved

@Zaimwa9 Zaimwa9 left a comment

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!

So basically 8682e20 is exactly what we wanted and 7aa28e9 is fine too.

It would be preferrable to revert the two following commits (e81f2fd and 81166e8). They start creeping out of scope and it modifies the path every flag save goes through. Let's leave them out of this work.

Those comments don't need to be addressed, i'll answer to the cubic threads, the concurrency risk is too rare to justify extra locking on these paths.

So just need:

  • reverting the 2 commits
  • resolving conflicts
  • maybe add one doc mention that an overwrite import skips flags under an experiment

Multivariate options were only restricted in the frontend. The API
accepted create, update and delete on the MV options endpoint, and
changed nested multivariate_options on feature update, even when the
feature was in a running experiment.

Reject these writes with a 400 while any environment has a running
experiment for the feature. On feature update, only reject when the
submitted options differ from the stored ones, so other feature fields
can still be saved.

Closes Flagsmith#8766
- mv-options PUT only checks for an experiment when the variation itself
  changes (type, value, key or feature). default_percentage_allocation is
  ignored, so the dashboard can still save other environments.
- Paused experiments lock the variations as well as running ones.
- Imports keep their original per-feature behaviour. A first pass rejects
  the import before anything is written, and only when it would add a
  variation or overwrite the environment running the experiment.
- One error shape, {"detail": ...}, naming the experiment environment.
- Drop the duplicate check in validate_multivariate_options.
- Mention the lock in the experiment docs.
…e import

Skip individual features in _import_features_for_environment instead of
failing the whole import when a feature has a running or paused experiment
in the target environment or when the import would add a variation while an
experiment is running or paused in any environment.
@TejasLamba2006
TejasLamba2006 force-pushed the fix/restrict-mv-options-in-live-experiment branch from 81166e8 to ff02709 Compare November 24, 2026 16:46
@TejasLamba2006

Copy link
Copy Markdown
Author

@Zaimwa9 updated and rebased onto main (head ff02709):

  • Dropped e81f2fd and 81166e8 so the branch sits back on the 8682e20 + 7aa28e9 scope.
  • Resolved the conflicts in api/experimentation/services.py and docs/docs/deployment-self-hosting/observability/_events-catalogue.md (verified against flagsmith docgen events).
  • Added a note to docs/docs/administration-and-security/data-management/bulk-import-and-export.md explaining that Overwrite Destructive skips features with a running or paused experiment in the target environment, or when the import would add a variation while an experiment is active in any environment.

Ready for another look whenever you have time.

@vercel

vercel Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
flagsmith-frontend-staging Ignored Ignored Preview Nov 26, 2026 12:19pm UTC

Request Review

@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.61832% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 98.07%. Comparing base (02dd781) to head (ff02709).
⚠️ Report is 22 commits behind head on main.

Files with missing lines Patch % Lines
api/features/multivariate/views.py 96.87% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8917      +/-   ##
==========================================
- Coverage   98.93%   98.07%   -0.86%     
==========================================
  Files        1715     1716       +1     
  Lines       72779    73032     +253     
==========================================
- Hits        72002    71627     -375     
- Misses        777     1405     +628     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Zaimwa9 Zaimwa9 left a comment

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.

One last change to have the CI pass as I was already ready to approve 🙏

Comment thread api/features/multivariate/views.py Outdated
Comment on lines +100 to +101
if serializer.validated_data["feature"] != url_feature:
raise ValidationError({"feature": "Feature must match the URL."})

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.

I think that's what codecov is flagging, sorry I missed it but I believe this path is unreachable because a request without a feature would fail the serializer validation.
It should be safe to remove and please codecov

@TejasLamba2006

Copy link
Copy Markdown
Author

Thanks @Zaimwa9. The one uncovered line was a feature mismatch check in perform_create that can't be reached, since create() already rejects a mismatched feature in the body with a 400 (covered by test_create_mv_option__body_feature_mismatches_url__returns_400). I removed it in 7c2d410, so codecov/patch should pass now.

The Fix Documentation Artefacts failure looks unrelated to the code. The job dies at "Generate GitHub App token with push access" because the app client-id secret isn't available to workflows from forks. The docs changes in this PR were already checked against flagsmith docgen events, so I think a maintainer needs to re-run that job or push the artefacts.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

0 issues found across 1 file (changes from recent commits).

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Requires human review: Auto-approval skipped because cubic reviewed only this push, not the earlier force-push. Comment @cubic review to review the whole pull request.

Re-trigger cubic

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Issue related to the REST API docs Documentation updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants