From 091d1dbb8224fddbffa57d94c0da70f34ae9082c Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Fri, 20 Nov 2026 18:59:21 +0000 Subject: [PATCH 1/3] deps: update dependency flagsmith/sdk-spec to v0.10.0 --- .github/workflows/sdk-spec.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/sdk-spec.yml b/.github/workflows/sdk-spec.yml index bb978e9..2dd74d7 100644 --- a/.github/workflows/sdk-spec.yml +++ b/.github/workflows/sdk-spec.yml @@ -11,8 +11,8 @@ permissions: jobs: sdk-spec: # Keep the ref in `uses:` and `spec-ref` in sync. - uses: Flagsmith/sdk-spec/.github/workflows/run-spec.yml@v0.9.0 + uses: Flagsmith/sdk-spec/.github/workflows/run-spec.yml@v0.10.0 with: - spec-ref: v0.9.0 + spec-ref: v0.10.0 language: elixir tags: core events local-evaluation From 415fd7591e2cb513b3ddd13bea4607e209e5cf4e Mon Sep 17 00:00:00 2001 From: "flagsmithsmith[bot]" <346993984+flagsmithsmith[bot]@users.noreply.github.com> Date: Fri, 20 Nov 2026 20:38:27 +0000 Subject: [PATCH 2/3] feat: Include the evaluation reason in environment and identity flags `Flagsmith.Schemas.Flag` now has a `reason`: why the flag has its value, e.g. "STATIC", "DEFAULT", "TARGETING_MATCH; segment=premium_users" or "SPLIT; weight=60.0". Flags evaluated by the SDK, from the environment document, take the engine's reason; flags from the /identities/ API take the `reason` Flagsmith returns for each. Implements Flagsmith SDK spec v0.10.0. Co-Authored-By: Claude Opus 5.5 --- lib/flagsmith_engine.ex | 32 ++++++++--------- lib/schemas/environment_feature_state.ex | 2 ++ lib/schemas/features/feature_state.ex | 4 ++- lib/schemas/flag.ex | 5 +++ sdk-spec/steps.exs | 7 +++- test/flagsmith_client_test.exs | 24 ++++++++----- test/flagsmith_engine_test.exs | 46 ++++++++++++++++++++++-- test/support/generators.ex | 2 +- 8 files changed, 90 insertions(+), 32 deletions(-) diff --git a/lib/flagsmith_engine.ex b/lib/flagsmith_engine.ex index fea86ee..ebb6076 100644 --- a/lib/flagsmith_engine.ex +++ b/lib/flagsmith_engine.ex @@ -153,25 +153,21 @@ defmodule Flagsmith.Engine do defp to_feature_states(feature_states, flags) do Enum.map(feature_states, fn %{feature: %{name: name}} -> - case Map.fetch!(flags, name) do - %FlagResult{ - enabled: enabled, - value: value, + %FlagResult{ + enabled: enabled, + value: value, + variant: variant, + reason: reason, + metadata: %{feature_state: feature_state} + } = Map.fetch!(flags, name) + + %{ + feature_state + | enabled: enabled, + feature_state_value: value, variant: variant, - metadata: %{feature_state: feature_state} - } - when enabled === feature_state.enabled and value === feature_state.feature_state_value and - variant === feature_state.variant -> - feature_state - - %FlagResult{ - enabled: enabled, - value: value, - variant: variant, - metadata: %{feature_state: feature_state} - } -> - %{feature_state | enabled: enabled, feature_state_value: value, variant: variant} - end + reason: reason + } end) end diff --git a/lib/schemas/environment_feature_state.ex b/lib/schemas/environment_feature_state.ex index dfac852..683e35b 100644 --- a/lib/schemas/environment_feature_state.ex +++ b/lib/schemas/environment_feature_state.ex @@ -24,6 +24,8 @@ defmodule Flagsmith.Schemas.Environment.FeatureState do # The variant an identity is served, set when evaluating its flags. field(:variant, :string, virtual: true) + # Why the flag evaluated to its value, set when evaluating flags. + field(:reason, :string, virtual: true) end @doc false diff --git a/lib/schemas/features/feature_state.ex b/lib/schemas/features/feature_state.ex index ecabe9f..45aa5ce 100644 --- a/lib/schemas/features/feature_state.ex +++ b/lib/schemas/features/feature_state.ex @@ -19,6 +19,7 @@ defmodule Flagsmith.Schemas.Features.FeatureState do field(:id, :integer) field(:identity, :integer) field(:variant, :string) + field(:reason, :string) embeds_one(:feature, Flagsmith.Schemas.Features.Feature) embeds_one(:metadata, __MODULE__.Metadata) @@ -38,7 +39,8 @@ defmodule Flagsmith.Schemas.Features.FeatureState do :feature_segment, :environment, :enabled, - :variant + :variant, + :reason ], empty_values: [] ) diff --git a/lib/schemas/flag.ex b/lib/schemas/flag.ex index aba4160..7a48bc5 100644 --- a/lib/schemas/flag.ex +++ b/lib/schemas/flag.ex @@ -13,6 +13,7 @@ defmodule Flagsmith.Schemas.Flag do field(:feature_name, :string) field(:value, :string) field(:variant, :string) + field(:reason, :string) embeds_one(:experiment, Experiment) end @@ -22,6 +23,7 @@ defmodule Flagsmith.Schemas.Flag do enabled: enabled, feature_state_value: value, variant: variant, + reason: reason, metadata: metadata, feature: %Environment.Feature{ name: name, @@ -34,6 +36,7 @@ defmodule Flagsmith.Schemas.Flag do value: value, feature_id: id, variant: variant, + reason: reason, experiment: metadata && metadata.experiment } end @@ -42,6 +45,7 @@ defmodule Flagsmith.Schemas.Flag do enabled: enabled, feature_state_value: value, variant: variant, + reason: reason, metadata: metadata, feature: %Features.Feature{ name: name, @@ -54,6 +58,7 @@ defmodule Flagsmith.Schemas.Flag do value: value, feature_id: id, variant: variant, + reason: reason, experiment: metadata && metadata.experiment } end diff --git a/sdk-spec/steps.exs b/sdk-spec/steps.exs index fe44a15..e6e687f 100644 --- a/sdk-spec/steps.exs +++ b/sdk-spec/steps.exs @@ -7,7 +7,12 @@ defmodule SDKSpec.Steps do alias Flagsmith.Schemas defp as_spec_flag(flag), - do: %{feature: flag.feature_name, enabled: flag.enabled, value: flag.value} + do: %{ + feature: flag.feature_name, + enabled: flag.enabled, + value: flag.value, + reason: flag.reason + } defp as_spec_flags(flags), do: Enum.map(Client.all_flags(flags), &as_spec_flag/1) diff --git a/test/flagsmith_client_test.exs b/test/flagsmith_client_test.exs index fd084ad..e9108eb 100644 --- a/test/flagsmith_client_test.exs +++ b/test/flagsmith_client_test.exs @@ -53,25 +53,29 @@ defmodule Flagsmith.Client.Test do enabled: false, feature_id: 13535, feature_name: "body_size", - value: "18px" + value: "18px", + reason: "STATIC" }, "header_size" => %Schemas.Flag{ enabled: false, feature_id: 13534, feature_name: "header_size", - value: "24px" + value: "24px", + reason: "STATIC" }, "secret_button" => %Schemas.Flag{ enabled: true, feature_id: 17985, feature_name: "secret_button", - value: "{\"colour\": \"#ababab\"}" + value: "{\"colour\": \"#ababab\"}", + reason: "STATIC" }, "test_identity" => %Schemas.Flag{ enabled: true, feature_id: 18382, feature_name: "test_identity", - value: "very_yes" + value: "very_yes", + reason: "STATIC" } } }} = Flagsmith.Client.get_environment_flags(config) @@ -288,25 +292,29 @@ defmodule Flagsmith.Client.Test do enabled: false, feature_id: 13535, feature_name: "body_size", - value: "18px" + value: "18px", + reason: "STATIC" }, "header_size" => %Schemas.Flag{ enabled: false, feature_id: 13534, feature_name: "header_size", - value: "34px" + value: "34px", + reason: "STATIC" }, "secret_button" => %Schemas.Flag{ enabled: true, feature_id: 17985, feature_name: "secret_button", - value: "" + value: "", + reason: "TARGETING_MATCH; segment=test_segment" }, "test_identity" => %Schemas.Flag{ enabled: true, feature_id: 18382, feature_name: "test_identity", - value: "very_no" + value: "very_no", + reason: "IDENTITY_OVERRIDE" } } } diff --git a/test/flagsmith_engine_test.exs b/test/flagsmith_engine_test.exs index 5062f8a..30f0331 100644 --- a/test/flagsmith_engine_test.exs +++ b/test/flagsmith_engine_test.exs @@ -187,7 +187,9 @@ defmodule Flagsmith.EngineTest do # note for non elixir devs the ^ (pin) operator on the left side of a match (=) # forces the variable to be exactly the one that was pinned instead of doing # re-assignement, it would be equivalent to doing var == something - assert ^feature_states = Flagsmith.Engine.get_environment_feature_states(env) + # evaluated without an identity, each comes with the reason it has its value + static_feature_states = Enum.map(feature_states, &%{&1 | reason: "STATIC"}) + assert ^static_feature_states = Flagsmith.Engine.get_environment_feature_states(env) end test "get_environment_feature_states/1 when project hide_disabled is true", %{env: env} do @@ -207,14 +209,16 @@ defmodule Flagsmith.EngineTest do # replace the feature states with the new one that should have an additional enabled one new_env = %{env | feature_states: new_feature_states} + expected_first_feature_state = %{new_first_feature_state | reason: "STATIC"} # we should have 3 enabled, the 2 enabled originally + the last one we enabled - assert [^new_first_feature_state, _, _] = + assert [^expected_first_feature_state, _, _] = Flagsmith.Engine.get_environment_feature_states(new_env) end test "get_environment_feature_state/2", %{env: env} do - [%{feature: %{name: name}} = first_feature_state | _] = env.feature_states + [%{feature: %{name: name}} = feature_state | _] = env.feature_states + first_feature_state = %{feature_state | reason: "STATIC"} assert ^first_feature_state = Flagsmith.Engine.get_environment_feature_state(env, name) end @@ -249,6 +253,42 @@ defmodule Flagsmith.EngineTest do ] = Flagsmith.Engine.get_identity_feature_states(env, identity, []) end + test "get_identity_feature_states/3 gives the reason each flag has its value", %{ + env: env, + identity: identity + } do + # without the identity's overrides, which match as the `identity_overrides` segment + identity = %{identity | identity_features: []} + + reasons = fn env, traits -> + env + |> Flagsmith.Engine.get_identity_feature_states(identity, traits) + |> Map.new(fn %{feature: %{name: name}, reason: reason} -> {name, reason} end) + end + + # the identity's `show_popup` trait is false by default, matching `test_segment` + # which overrides `secret_button`, and it falls in the 60% split of `header_size` + assert %{ + "header_size" => "SPLIT; weight=60.0", + "body_size" => "STATIC", + "secret_button" => "TARGETING_MATCH; segment=test_segment", + "test_identity" => "STATIC" + } = reasons.(env, []) + + # with `show_popup` true and only `test_segment` overriding `secret_button`, + # no segment overriding it matches + segments = Enum.filter(env.project.segments, &(&1.name == "test_segment")) + env = %{env | project: %{env.project | segments: segments}} + + assert %{"secret_button" => "DEFAULT"} = + reasons.(env, [ + %Traits.Trait{ + trait_key: "show_popup", + trait_value: %Traits.Trait.Value{value: true, type: :boolean} + } + ]) + end + test "get_identity_segments/3", %{env: env, identity: identity} do # Verify that the hash function is called with the composite key # and return a hash that will evaluate as 99.9 (to ensure the identity diff --git a/test/support/generators.ex b/test/support/generators.ex index c2edd03..970fcca 100644 --- a/test/support/generators.ex +++ b/test/support/generators.ex @@ -164,7 +164,7 @@ defmodule Flagsmith.Engine.Test.Generators do end def json_identity() do - "{\"flags\":[{\"enabled\":false,\"environment\":11278,\"feature\":{\"created_date\":\"2021-10-24T13:40:02.805964Z\",\"default_enabled\":false,\"description\":\"Header Size\",\"id\":13534,\"initial_value\":\"24px\",\"name\":\"header_size\",\"type\":\"MULTIVARIATE\"},\"feature_segment\":null,\"feature_state_value\":\"34px\",\"id\":72267,\"identity\":null},{\"enabled\":false,\"environment\":11278,\"feature\":{\"created_date\":\"2021-10-24T13:41:35.650846Z\",\"default_enabled\":false,\"description\":null,\"id\":13535,\"initial_value\":\"18px\",\"name\":\"body_size\",\"type\":\"STANDARD\"},\"feature_segment\":null,\"feature_state_value\":\"18px\",\"id\":72269,\"identity\":null},{\"enabled\":true,\"environment\":11278,\"feature\":{\"created_date\":\"2022-02-07T19:54:48.630966Z\",\"default_enabled\":false,\"description\":null,\"id\":17985,\"initial_value\":null,\"name\":\"secret_button\",\"type\":\"STANDARD\"},\"feature_segment\":11493,\"feature_state_value\":\"\",\"id\":95631,\"identity\":null},{\"enabled\":true,\"environment\":11278,\"feature\":{\"created_date\":\"2022-02-13T16:14:18.546593Z\",\"default_enabled\":false,\"description\":null,\"id\":18382,\"initial_value\":\"very_yes\",\"name\":\"test_identity\",\"type\":\"STANDARD\"},\"feature_segment\":null,\"feature_state_value\":\"very_no\",\"id\":94429,\"identity\":24842744}],\"traits\":[]}" + "{\"flags\":[{\"enabled\":false,\"environment\":11278,\"feature\":{\"created_date\":\"2021-10-24T13:40:02.805964Z\",\"default_enabled\":false,\"description\":\"Header Size\",\"id\":13534,\"initial_value\":\"24px\",\"name\":\"header_size\",\"type\":\"MULTIVARIATE\"},\"feature_segment\":null,\"feature_state_value\":\"34px\",\"id\":72267,\"reason\":\"STATIC\",\"identity\":null},{\"enabled\":false,\"environment\":11278,\"feature\":{\"created_date\":\"2021-10-24T13:41:35.650846Z\",\"default_enabled\":false,\"description\":null,\"id\":13535,\"initial_value\":\"18px\",\"name\":\"body_size\",\"type\":\"STANDARD\"},\"feature_segment\":null,\"feature_state_value\":\"18px\",\"id\":72269,\"reason\":\"STATIC\",\"identity\":null},{\"enabled\":true,\"environment\":11278,\"feature\":{\"created_date\":\"2022-02-07T19:54:48.630966Z\",\"default_enabled\":false,\"description\":null,\"id\":17985,\"initial_value\":null,\"name\":\"secret_button\",\"type\":\"STANDARD\"},\"feature_segment\":11493,\"feature_state_value\":\"\",\"id\":95631,\"reason\":\"TARGETING_MATCH; segment=test_segment\",\"identity\":null},{\"enabled\":true,\"environment\":11278,\"feature\":{\"created_date\":\"2022-02-13T16:14:18.546593Z\",\"default_enabled\":false,\"description\":null,\"id\":18382,\"initial_value\":\"very_yes\",\"name\":\"test_identity\",\"type\":\"STANDARD\"},\"feature_segment\":null,\"feature_state_value\":\"very_no\",\"id\":94429,\"reason\":\"IDENTITY_OVERRIDE\",\"identity\":24842744}],\"traits\":[]}" end def map_identity(), do: Jason.decode!(json_identity()) From f57c1473bb2fc02e8dccfa1e0e3f55f7ae320480 Mon Sep 17 00:00:00 2001 From: "flagsmithsmith[bot]" <346993984+flagsmithsmith[bot]@users.noreply.github.com> Date: Sat, 21 Nov 2026 19:47:45 +0000 Subject: [PATCH 3/3] test: Use the reasons Flagsmith returns in the identities fixture The fixture's header_size value comes from its 60% split, and Flagsmith reports identity overrides as "TARGETING_MATCH; segment=identity_overrides". Co-Authored-By: Claude Opus 5.5 --- test/flagsmith_client_test.exs | 4 ++-- test/support/generators.ex | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/flagsmith_client_test.exs b/test/flagsmith_client_test.exs index e9108eb..0e6e3ee 100644 --- a/test/flagsmith_client_test.exs +++ b/test/flagsmith_client_test.exs @@ -300,7 +300,7 @@ defmodule Flagsmith.Client.Test do feature_id: 13534, feature_name: "header_size", value: "34px", - reason: "STATIC" + reason: "SPLIT; weight=60.0" }, "secret_button" => %Schemas.Flag{ enabled: true, @@ -314,7 +314,7 @@ defmodule Flagsmith.Client.Test do feature_id: 18382, feature_name: "test_identity", value: "very_no", - reason: "IDENTITY_OVERRIDE" + reason: "TARGETING_MATCH; segment=identity_overrides" } } } diff --git a/test/support/generators.ex b/test/support/generators.ex index 970fcca..37a659b 100644 --- a/test/support/generators.ex +++ b/test/support/generators.ex @@ -164,7 +164,7 @@ defmodule Flagsmith.Engine.Test.Generators do end def json_identity() do - "{\"flags\":[{\"enabled\":false,\"environment\":11278,\"feature\":{\"created_date\":\"2021-10-24T13:40:02.805964Z\",\"default_enabled\":false,\"description\":\"Header Size\",\"id\":13534,\"initial_value\":\"24px\",\"name\":\"header_size\",\"type\":\"MULTIVARIATE\"},\"feature_segment\":null,\"feature_state_value\":\"34px\",\"id\":72267,\"reason\":\"STATIC\",\"identity\":null},{\"enabled\":false,\"environment\":11278,\"feature\":{\"created_date\":\"2021-10-24T13:41:35.650846Z\",\"default_enabled\":false,\"description\":null,\"id\":13535,\"initial_value\":\"18px\",\"name\":\"body_size\",\"type\":\"STANDARD\"},\"feature_segment\":null,\"feature_state_value\":\"18px\",\"id\":72269,\"reason\":\"STATIC\",\"identity\":null},{\"enabled\":true,\"environment\":11278,\"feature\":{\"created_date\":\"2022-02-07T19:54:48.630966Z\",\"default_enabled\":false,\"description\":null,\"id\":17985,\"initial_value\":null,\"name\":\"secret_button\",\"type\":\"STANDARD\"},\"feature_segment\":11493,\"feature_state_value\":\"\",\"id\":95631,\"reason\":\"TARGETING_MATCH; segment=test_segment\",\"identity\":null},{\"enabled\":true,\"environment\":11278,\"feature\":{\"created_date\":\"2022-02-13T16:14:18.546593Z\",\"default_enabled\":false,\"description\":null,\"id\":18382,\"initial_value\":\"very_yes\",\"name\":\"test_identity\",\"type\":\"STANDARD\"},\"feature_segment\":null,\"feature_state_value\":\"very_no\",\"id\":94429,\"reason\":\"IDENTITY_OVERRIDE\",\"identity\":24842744}],\"traits\":[]}" + "{\"flags\":[{\"enabled\":false,\"environment\":11278,\"feature\":{\"created_date\":\"2021-10-24T13:40:02.805964Z\",\"default_enabled\":false,\"description\":\"Header Size\",\"id\":13534,\"initial_value\":\"24px\",\"name\":\"header_size\",\"type\":\"MULTIVARIATE\"},\"feature_segment\":null,\"feature_state_value\":\"34px\",\"id\":72267,\"reason\":\"SPLIT; weight=60.0\",\"identity\":null},{\"enabled\":false,\"environment\":11278,\"feature\":{\"created_date\":\"2021-10-24T13:41:35.650846Z\",\"default_enabled\":false,\"description\":null,\"id\":13535,\"initial_value\":\"18px\",\"name\":\"body_size\",\"type\":\"STANDARD\"},\"feature_segment\":null,\"feature_state_value\":\"18px\",\"id\":72269,\"reason\":\"STATIC\",\"identity\":null},{\"enabled\":true,\"environment\":11278,\"feature\":{\"created_date\":\"2022-02-07T19:54:48.630966Z\",\"default_enabled\":false,\"description\":null,\"id\":17985,\"initial_value\":null,\"name\":\"secret_button\",\"type\":\"STANDARD\"},\"feature_segment\":11493,\"feature_state_value\":\"\",\"id\":95631,\"reason\":\"TARGETING_MATCH; segment=test_segment\",\"identity\":null},{\"enabled\":true,\"environment\":11278,\"feature\":{\"created_date\":\"2022-02-13T16:14:18.546593Z\",\"default_enabled\":false,\"description\":null,\"id\":18382,\"initial_value\":\"very_yes\",\"name\":\"test_identity\",\"type\":\"STANDARD\"},\"feature_segment\":null,\"feature_state_value\":\"very_no\",\"id\":94429,\"reason\":\"TARGETING_MATCH; segment=identity_overrides\",\"identity\":24842744}],\"traits\":[]}" end def map_identity(), do: Jason.decode!(json_identity())