Skip to content

feat(Flag Dependency): Index flag references - #8525

Merged
emyller merged 23 commits into
mainfrom
feat/segment-dependency-flag-reference
Sep 18, 2026
Merged

emyller merged 23 commits into
mainfrom
feat/segment-dependency-flag-reference

Conversation

@emyller

@emyller emyller commented Sep 14, 2026

Copy link
Copy Markdown
Contributor
  • 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

Closes #8412

This implements the support foundation for flag dependency in the Management API:

  • Flag references (prerequisite) are now indexed upon creating and updating segments.
  • Segments are now validated against flag circular dependency when updating rules or overrides (in both Feature Versioning v1 and v2 models).

How did you test this code?

A whole lotta tests included.

@emyller emyller self-assigned this Sep 14, 2026
@vercel

vercel Bot commented Sep 14, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated
docs Ready Ready Preview Sep 18, 2026 6:18pm UTC
2 Skipped Deployments
Project Deployment Actions Updated
flagsmith-frontend-preview Ignored Ignored Preview Sep 18, 2026 6:18pm UTC
flagsmith-frontend-staging Ignored Ignored Preview Sep 18, 2026 6:18pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Sep 14, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

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
📝 Walkthrough

Walkthrough

The change adds a Django dependencies application and persistent segment-to-feature references. It maps supported $.flags conditions and validates environment-scoped dependency paths for cycles. Segment writes, overrides, feature-segment creation, and deletion maintain references within transactions. Tests and observability documentation cover the new behaviour.

Priority: ➖ Normal

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

Severity of issue fixed: Medium

Merge Risk: 🔵 Low · up to ed3de

Segment flag dependency indexing and cycle validation now run on every segment update, including metadata-only edits, which adds extra database work to those writes. Behaviour remains correct, so this can be merged with a follow-up to skip the dependency work when segment rules are unchanged.

✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Commit to this branch
  • Create a new PR

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

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added api Issue related to the REST API feature New feature or request labels Sep 14, 2026
@emyller
emyller force-pushed the feat/segment-dependency-flag-reference branch from 36dde6b to b28b065 Compare September 15, 2026 12:12
@github-actions github-actions Bot added feature New feature or request and removed feature New feature or request labels Sep 15, 2026
@emyller
emyller force-pushed the feat/segment-dependency-flag-reference branch from b28b065 to a819d0c Compare September 15, 2026 14:00
@github-actions github-actions Bot added feature New feature or request and removed feature New feature or request labels Sep 15, 2026
@github-actions github-actions Bot added feature New feature or request docs Documentation updates and removed feature New feature or request labels Sep 15, 2026
@emyller
emyller force-pushed the feat/segment-dependency-flag-reference branch from 66804ae to 3608b5e Compare September 16, 2026 00:20
@github-actions github-actions Bot added feature New feature or request and removed feature New feature or request docs Documentation updates labels Sep 16, 2026
@github-actions github-actions Bot added the docs Documentation updates label Sep 16, 2026
@github-actions github-actions Bot added the feature New feature or request label Sep 17, 2026

@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: 2


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: ec7252e8-2519-4efa-9575-e09e6a275fe3

📥 Commits

Reviewing files that changed from the base of the PR and between 640bffd and 8e2728a.

📒 Files selected for processing (15)
  • api/app/settings/common.py
  • api/features/dependencies/exceptions.py
  • api/features/dependencies/mappers.py
  • api/features/dependencies/services.py
  • api/features/dependencies/types.py
  • api/features/feature_segments/views.py
  • api/features/future/services.py
  • api/tests/integration/conftest.py
  • api/tests/integration/features/conftest.py
  • api/tests/integration/features/dependencies/test_segment_management.py
  • api/tests/integration/features/future/test_flag_endpoint.py
  • api/tests/types.py
  • api/tests/unit/features/dependencies/__init__.py
  • api/tests/unit/features/dependencies/test_mappers.py
  • docs/docs/deployment-self-hosting/observability/_events-catalogue.md

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

Comment thread api/features/dependencies/mappers.py
Comment thread api/features/feature_segments/views.py
@github-actions github-actions Bot added docs Documentation updates feature New feature or request and removed feature New feature or request docs Documentation updates labels Sep 17, 2026

@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.

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (2)

🟠 Major · Serialise dependency validation per environment. · services.py:106-170

api/features/dependencies/services.py:106-170
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Serialise dependency validation per environment. FeatureSegmentViewSet.perform_create writes the new FeatureSegment and then calls validate_segment_flag_dependencies inside transaction.atomic(). The validator reads get_overrides_in_effect() from segments.services without select_for_update.

In an environment with use_v2_feature_versioning=False, two requests can add these edges concurrently:

  • feature A overrides a segment that references feature B;
  • feature B overrides a segment that references feature A.

Each transaction can validate before the other transaction commits. Both validations then pass, and both FeatureSegment rows commit, leaving the cycle A → B → A. SegmentFlagReference has only foreign keys, and FeatureSegment uniqueness covers only identical rows. No database constraint enforces an acyclic graph.

Lock a common row for each affected environment in a stable order before the dependency graph query, and hold the lock through the surrounding transaction's commit. Alternatively, enforce the acyclic invariant at the database boundary.

🟠 Major · Defer dependency indexing from segment writes. · serializers.py:224-244

api/segments/serializers.py:224-244
🚀 Performance & Scalability | 🟠 Major | 🏗️ Heavy lift

Defer dependency indexing from segment writes.

SegmentViewSet uses SegmentSerializer for segment writes. The serializer calls index_segment_flag_references() inside transaction.atomic(). For indexed rules, this performs a feature lookup, reads and deletes existing SegmentFlagReference rows, bulk-inserts replacements, and emits dependency log events. Updates also run validate_segment_flag_dependencies(), which queries dependency edges synchronously.

This extends the segment-write transaction and adds database and logging work to the request path. Defer indexing, or redesign the write path to preserve validation and consistency without this synchronous work.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 5789cfba-8937-4d99-9400-b24e9d302845

📥 Commits

Reviewing files that changed from the base of the PR and between 8e2728a and b25eecd.

📒 Files selected for processing (2)
  • api/features/dependencies/mappers.py
  • api/tests/integration/features/dependencies/test_segment_management.py

Included review availability: Your plan provides up to 8 included reviews per hour; 6 remain after this review.

@khvn26 khvn26 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM bar a couple questions around the circular dependency validation. Good job 👍

Comment thread api/features/dependencies/services.py Outdated
Comment thread api/features/dependencies/services.py Outdated
Comment thread api/features/dependencies/services.py
@github-actions github-actions Bot added docs Documentation updates feature New feature or request and removed feature New feature or request docs Documentation updates labels Sep 18, 2026

@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.

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

🟠 Major · Reject cross-project segment overrides before dependency validation. · services.py:24-62

api/features/dependencies/services.py:24-62
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Reject cross-project segment overrides before dependency validation.

FeatureSegmentCreateSerializer accepts independent feature, segment, and environment foreign keys. FeatureSegmentPermissions checks only the environment permission, and get_overrides_in_effect() returns a global queryset. A request can therefore attach a project B segment to a project A feature and environment.

SegmentFlagReference does not enforce matching projects. _get_dependency_edges() then joins its prerequisite feature names without a project filter. If the project B segment references a feature named F, validation can treat that reference as a dependency of project A's feature F and raise a false CircularDependencyError.

Require matching project IDs when creating FeatureSegment and SegmentFlagReference rows. Also scope dependency traversal to the environment's project as a defensive check.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: cff53a2f-d919-439a-bd44-f3dbcd06aead

📥 Commits

Reviewing files that changed from the base of the PR and between b25eecd and 8d64135.

📒 Files selected for processing (3)
  • api/features/dependencies/services.py
  • api/tests/unit/features/dependencies/test_services.py
  • docs/docs/deployment-self-hosting/observability/_events-catalogue.md

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

@emyller
emyller force-pushed the feat/segment-dependency-flag-reference branch from 8d64135 to ed3decf Compare September 18, 2026 12:56
@github-actions github-actions Bot added feature New feature or request and removed feature New feature or request docs Documentation updates labels Sep 18, 2026

@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: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 95500cc5-6a71-4f1e-8196-49606291e624

📥 Commits

Reviewing files that changed from the base of the PR and between 8d64135 and ed3decf.

📒 Files selected for processing (25)
  • api/app/settings/common.py
  • api/features/dependencies/__init__.py
  • api/features/dependencies/apps.py
  • api/features/dependencies/exceptions.py
  • api/features/dependencies/mappers.py
  • api/features/dependencies/migrations/0001_initial.py
  • api/features/dependencies/migrations/__init__.py
  • api/features/dependencies/models.py
  • api/features/dependencies/services.py
  • api/features/dependencies/types.py
  • api/features/feature_segments/views.py
  • api/features/future/services.py
  • api/features/views.py
  • api/segments/serializers.py
  • api/segments/services.py
  • api/tests/integration/conftest.py
  • api/tests/integration/features/conftest.py
  • api/tests/integration/features/dependencies/test_segment_management.py
  • api/tests/integration/features/future/test_flag_endpoint.py
  • api/tests/types.py
  • api/tests/unit/features/dependencies/__init__.py
  • api/tests/unit/features/dependencies/test_mappers.py
  • api/tests/unit/features/dependencies/test_services.py
  • api/tests/unit/segments/test_unit_segments_services.py
  • docs/docs/deployment-self-hosting/observability/_events-catalogue.md
💤 Files with no reviewable changes (3)
  • api/features/dependencies/init.py
  • api/features/dependencies/migrations/init.py
  • api/tests/unit/features/dependencies/init.py

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

Comment thread api/segments/serializers.py
@github-actions github-actions Bot added the docs Documentation updates label Sep 18, 2026

@khvn26 khvn26 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

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 feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Index segment references to dependent flags

3 participants