-
-
Notifications
You must be signed in to change notification settings - Fork 258
feat: Make a/b tests independent #7511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
4fa286e
452a0ea
30a9037
1c63786
6ec468a
c4c8cd2
601b2a6
526f5fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ import type { | |
| FeatureFlagScopeValue, | ||
| } from './remote-feature-flag-controller-types'; | ||
| import { | ||
| createDeterministicSeed, | ||
| generateDeterministicRandomNumber, | ||
| isFeatureFlagWithScopeValue, | ||
| } from './utils/user-segmentation-utils'; | ||
|
|
@@ -278,7 +279,6 @@ export class RemoteFeatureFlagController extends BaseController< | |
| ): Promise<FeatureFlags> { | ||
| const processedRemoteFeatureFlags: FeatureFlags = {}; | ||
| const metaMetricsId = this.#getMetaMetricsId(); | ||
| const thresholdValue = generateDeterministicRandomNumber(metaMetricsId); | ||
|
|
||
| for (const [ | ||
| remoteFeatureFlagName, | ||
|
|
@@ -291,7 +291,13 @@ export class RemoteFeatureFlagController extends BaseController< | |
| continue; | ||
| } | ||
|
|
||
| if (Array.isArray(processedValue) && thresholdValue) { | ||
| if (Array.isArray(processedValue)) { | ||
| const deterministicSeed = createDeterministicSeed( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a slow cryptographic operation and it runs once per flag, but we don't need to run this for all flags. It's unnecessary unless the flag has a threshold. Perhaps we can calculate this later, after we've determined that there is a threshold on this specific flag? That way it can be skipped for non-threshold flags, which is the majority of them.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's also not ideal to run this every time flags get updated 🤔. |
||
| metaMetricsId, | ||
| remoteFeatureFlagName, | ||
| ); | ||
| const thresholdValue = | ||
| generateDeterministicRandomNumber(deterministicSeed); | ||
| const selectedGroup = processedValue.find( | ||
| (featureFlag): featureFlag is FeatureFlagScopeValue => { | ||
| if (!isFeatureFlagWithScopeValue(featureFlag)) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| import type { Json } from '@metamask/utils'; | ||
| import { sha256 } from '@noble/hashes/sha256'; | ||
| import { bytesToHex } from '@noble/hashes/utils'; | ||
| import { validate as uuidValidate, version as uuidVersion } from 'uuid'; | ||
|
|
||
| import type { FeatureFlagScopeValue } from '../remote-feature-flag-controller-types'; | ||
|
|
@@ -19,6 +21,33 @@ const MIN_UUID_V4_BIGINT = uuidStringToBigInt(MIN_UUID_V4); | |
| const MAX_UUID_V4_BIGINT = uuidStringToBigInt(MAX_UUID_V4); | ||
| const UUID_V4_VALUE_RANGE_BIGINT = MAX_UUID_V4_BIGINT - MIN_UUID_V4_BIGINT; | ||
|
|
||
| /** | ||
| * Creates a deterministic hex ID by hashing the input seed. | ||
| * This ensures consistent group assignment for A/B testing across different feature flags. | ||
| * Normalizes inputs to lowercase to ensure case-insensitive consistency. | ||
| * | ||
| * @param metaMetricsId - The user's MetaMetrics ID (must be non-empty) | ||
| * @param featureFlagName - The feature flag name to create unique seed per flag | ||
| * @returns A hex string with '0x' prefix suitable for generateDeterministicRandomNumber | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| * @throws Error if metaMetricsId is empty | ||
| */ | ||
| export function createDeterministicSeed( | ||
| metaMetricsId: string, | ||
| featureFlagName: string, | ||
| ): string { | ||
| if (!metaMetricsId) { | ||
| throw new Error('MetaMetrics ID cannot be empty'); | ||
| } | ||
|
|
||
| // Normalize to lowercase to ensure case-insensitive consistency | ||
| const normalizedId = metaMetricsId.toLowerCase(); | ||
| const normalizedFlagName = featureFlagName.toLowerCase(); | ||
| const seed = normalizedId + normalizedFlagName; | ||
| const hashBuffer = sha256(seed); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If async is OK here we should consider using
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just upstreamed this which may be useful here
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @FrederikBolding can you expand on that ? I've used sha256 from noble/hashes to keep consistency with what I've noticed in other packages.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't seem to be published/exported yet in the current version of metamask/utils
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was published earlier today!
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SHA-256 using native code will perform better across mobile and extension! We automatically fall back to noble if unavailable |
||
| const hash = bytesToHex(hashBuffer); | ||
| return `0x${hash}`; | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Generates a deterministic random number between 0 and 1 based on a metaMetricsId. | ||
| * This is useful for A/B testing and feature flag rollouts where we want | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.