Skip to content

feat: Evaluate flags that depend on other flags in local evaluation - #314

Merged
emyller merged 11 commits into
mainfrom
feat/flag-dependency
Nov 2, 2026
Merged

emyller merged 11 commits into
mainfrom
feat/flag-dependency

Conversation

@emyller

@emyller emyller commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Local evaluation now serves flags whose segments depend on other flags, matching flagsmith-engine 11.1.1.

Closes SDK-10. Tested by engine-test-data v3.12.1, and by sdk-spec's local evaluation scenarios once #310 bumps the spec to v0.7.0.

Changes

  • Segment conditions can read other flags through $.flags.<feature>.enabled, .value and .variant, including chains of dependencies. A flag caught in a cycle serves its environment default, with the reason ERROR; code=CIRCULAR_DEPENDENCY.
  • Segments are evaluated without an identity. getEnvironmentFlags leaves out segments that need one (traits or identity), as the Flagsmith API does.
  • Flag results report the variant they were evaluated to.
  • Flags that no segment overrides now have the reason STATIC instead of DEFAULT.
  • npm run generate-engine-types works again, and the context types now include flags.
  • engine-test-data goes from v3.7.0 to v3.12.1.

🤖 Generated with Claude Code

Closes #292

emyller and others added 6 commits October 30, 2026 15:01
… to the result schema

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@emyller
emyller requested a review from a team as a code owner October 30, 2026 19:40
@emyller
emyller requested review from khvn26 and removed request for a team October 30, 2026 19:40
@linear-code

linear-code Bot commented Oct 30, 2026

Copy link
Copy Markdown

SDK-10

@coderabbitai

coderabbitai Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

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: 9a54453c-18cd-439e-a431-4b514558de96
Commits

Reviewing files that changed from the base of the PR and between 7d368be and 6e2ed81.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
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: ba4d904e-7570-4a56-834a-f7027b45a08e
Commits

Reviewing files that changed from the base of the PR and between 8b18195 and 7d368be.

Files selected for processing: 3
  • .github/workflows/codspeed.yml
  • Makefile
  • flagsmith-engine/evaluation/evaluationContext/mappers.ts

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

Walkthrough

The evaluation context now supports evaluated flag results. Local evaluation filters environment segments and resolves flag dependencies during segment checks. Feature results include variant information and distinguish static targeting from defaults for features with segment overrides. The segment evaluator API now checks one supplied segment. The type-generation script and engine test-data reference are updated. The SDK spec adds a client factory with local evaluation enabled. The benchmark workflow separates build and run targets and uses configured runner and mode values.

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

Merge Risk: Low · previous review 7d368

The changes are mergeable with bounded follow-up: confirm the benchmark mode is configured or restore its fallback, and strengthen the two targeted test assertions. No production flag-evaluation failure was established.

- [ ] 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

@codspeed

codspeed Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by ×3.1

1 warning

⚠️ 2 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 2 improved benchmarks
✅ 13 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ getEnvironmentFlags 86.7 ms 18.7 ms ×4.6
⚡ getEnvironmentFlags 10.9 ms 5.3 ms ×2.1

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing feat/flag-dependency (6e2ed81) with main (7b2bd9a)

Open in CodSpeed

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

Note

Follow-up findings are listed below so you can review them separately from inline comments.

Follow-up (1)
Minor (1)
Minor · Avoid copying the key array for each override. · index.ts:126-129

flagsmith-engine/index.ts:126-129
Minor · Performance & Scalability · Quick win

Avoid copying the key array for each override.

The index getter creates a new array for every override. For k segments that override one feature, this costs about k²/2 element copies. The index is rebuilt on each evaluation that reads $.flags. Append to the existing array instead.

🛠️ Suggested fix
-                    const segmentKeys = this.segmentKeysByFeatureNameCache.get(override.name) || [];
-                    this.segmentKeysByFeatureNameCache.set(override.name, [
-                        ...segmentKeys,
-                        segmentKey
-                    ]);
+                    const segmentKeys = this.segmentKeysByFeatureNameCache.get(override.name) || [];
+                    segmentKeys.push(segmentKey);
+                    this.segmentKeysByFeatureNameCache.set(override.name, segmentKeys);
Nitpick comments: 2
tests/engine/unit/segments/segment_evaluators.test.ts: 1

222-222: Trivial · Maintainability & Code Quality · Quick win

Add a test for identity-less segment matching on $.environment.key.

The new isContextInSegment tests all build their context from baseContext, which always has an identity. No test checks a segment whose only condition is $.environment.key against a context without an identity. The current code matches such a segment, so the gap affects regression coverage, not current behaviour. Add a case where identity is omitted and the segment rule compares $.environment.key, and assert true.

🧪 Suggested test
+    test('matches a segment on environment key without identity', () => {
+        const contextWithoutIdentity: EvaluationContext = {
+            environment: { key: 'env', name: 'test' },
+            segments: {},
+            features: {}
+        };
+        const segment = {
+            key: '1',
+            name: 'environment_segment',
+            rules: [
+                {
+                    type: ALL_RULE,
+                    conditions: [{ property: '$.environment.key', operator: 'EQUAL', value: 'env' }],
+                    rules: []
+                }
+            ],
+            overrides: []
+        };
+
+        expect(isContextInSegment(contextWithoutIdentity, segment)).toBe(true);
+    });
tests/engine/unit/engine.test.ts: 1

42-42: Trivial · Maintainability & Code Quality · Quick win

Add a test for a configured segment override that does not match.

No test under tests/engine asserts TARGETING_REASONS.DEFAULT. This change replaces the only such assertion, at engine.test.ts:42, with STATIC. A feature with a segment override that the identity does not match returns DEFAULT. A regression to STATIC would pass every reason assertion. Add a test for that case that asserts DEFAULT.

Additional comments: 1
flagsmith-engine/index.ts-52-54: 1

52-54: Minor · Potential issue · Quick win

Unverified finding
We couldn't verify this from the code. It is shown for review, not as a verified issue.

For a cyclic flag, getEvaluationResult changes only the reason of the already evaluated result. A separate, valid segment override can therefore leave its value in place despite the stated environment-default-on-cycle behavior.

The returned A is enabled with reason ERROR; code=CIRCULAR_DEPENDENCY rather than disabled at its environment default.

This can occur when: Flag A has a self-dependent segment that triggers cycle detection and a second, independently matching segment that enables A, while A's environment default is disabled.


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: a30e27ed-1e75-477d-8a0e-978101a1cf8a
Commits

Reviewing files that changed from the base of the PR and between c386862 and 9a5c115.

Files selected for processing: 12
  • .gitmodules
  • flagsmith-engine/evaluation/evaluationContext/evaluationContext.types.ts
  • flagsmith-engine/evaluation/evaluationContext/mappers.ts
  • flagsmith-engine/features/constants.ts
  • flagsmith-engine/features/types.ts
  • flagsmith-engine/index.ts
  • flagsmith-engine/segments/evaluators.ts
  • package.json
  • sdk-spec/steps.mts
  • tests/engine/engine-tests/engine-test-data
  • tests/engine/unit/engine.test.ts
  • tests/engine/unit/segments/segment_evaluators.test.ts

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

Comment thread flagsmith-engine/index.ts Resolved
Comment thread flagsmith-engine/index.ts Resolved
Comment thread flagsmith-engine/index.ts Resolved
emyller and others added 2 commits October 30, 2026 18:00
…vironment document

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

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

Follow-up Comments

Nitpick Give the mvfeature option a distinct key in the fixture and assert that key.

107-107: Trivial · Maintainability & Code Quality · Quick win

Give the mv_feature option a distinct key in the fixture and assert that key.

The option in tests/sdk/data/environment.json sets value and key to bar. The variant.key assertion at tests/engine/unit/mappers.test.ts:107 therefore also passes if the mapper copies value into key. Setting a different key in the fixture makes the assertion detect that regression. The value assertion at line 108 stays bar.

Suggested fix
--- a/tests/sdk/data/environment.json
+++ b/tests/sdk/data/environment.json
@@
           "multivariate_feature_option": {
             "value": "bar",
             "id": 1,
-            "key": "bar"
+            "key": "bar_key"
           },
--- a/tests/engine/unit/mappers.test.ts
+++ b/tests/engine/unit/mappers.test.ts
@@
-        expect(variant.key).toBe('bar');
+        expect(variant.key).toBe('bar_key');

Open the source location

Nitpick Assert the evaluated variant key for a keyed multivariate winner.

107-107: Trivial · Functional Correctness · Quick win

Assert the evaluated variant key for a keyed multivariate winner.

The multivariate evaluation test at tests/engine/unit/engine.test.ts:343-366 defines its variants without key. It asserts only flags['Multivariate Feature'].value. No engine unit test asserts variant on an evaluated flag. A keyed winner therefore has no unit-level check on the returned variant key.

Add a key to the winning option in that test and assert that the evaluated flag's variant equals that key. This covers the path where the winning option's key is returned.

Open the source location


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: 2612f920-c7c1-4991-bdf0-15d10890db3b
Commits

Reviewing files that changed from the base of the PR and between 9a5c115 and 8b18195.

Files selected for processing: 6
  • flagsmith-engine/evaluation/evaluationContext/mappers.ts
  • flagsmith-engine/features/models.ts
  • flagsmith-engine/features/util.ts
  • flagsmith-engine/index.ts
  • tests/engine/unit/mappers.test.ts
  • tests/sdk/data/environment.json

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

emyller and others added 2 commits October 30, 2026 18:40
…nvironment flags

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Run CodSpeed on the runner and in the mode set by the CODSPEED_RUNS_ON and CODSPEED_MODE organisation variables.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

@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

Nitpick comments: 1
flagsmith-engine/evaluation/evaluationContext/mappers.ts: 1

93-93: Trivial · Maintainability & Code Quality · Quick win

Assert the omitted segment in the environment-evaluation test.

The test at tests/engine/unit/mappers.test.ts:282 checks only that context.segments is defined. An empty object, or a result that keeps the identity-dependent segment, also passes. Add an assertion that segment '1' is absent in environment-evaluation mode.

🧪 Suggested test change
         expect(context.features).toBeDefined();
-        expect(context.segments).toBeDefined();
+        expect(context.segments).not.toHaveProperty('1');
     });

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: ba4d904e-7570-4a56-834a-f7027b45a08e
Commits

Reviewing files that changed from the base of the PR and between 8b18195 and 7d368be.

Files selected for processing: 3
  • .github/workflows/codspeed.yml
  • Makefile
  • flagsmith-engine/evaluation/evaluationContext/mappers.ts

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

Comment thread .github/workflows/codspeed.yml Resolved

@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

@emyller
emyller merged commit 40ea291 into main Nov 2, 2026
7 checks passed
@emyller
emyller deleted the feat/flag-dependency branch November 2, 2026 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support $.flags context values in segment conditions

2 participants