Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

✨ feat(feature-flags): support percentage rollout in allowlist flag values (#22143)

Inneicea2f82e0ce8
diff --git a/packages/app-config/src/featureFlags/schema.test.ts b/packages/app-config/src/featureFlags/schema.test.ts
index c773852ece9..4e5443cfb81 100644
--- a/packages/app-config/src/featureFlags/schema.test.ts
+++ b/packages/app-config/src/featureFlags/schema.test.ts
@@ -125,6 +125,70 @@ describe('evaluateFeatureFlag', () => {
expect(evaluateFeatureFlag([], 'user-123')).toBe(false);
expect(evaluateFeatureFlag([])).toBe(false);
});
+
+ describe('percentage rollout token', () => {
+ const userIds = Array.from({ length: 10_000 }, (_, i) => `user-${i}`);
+ const enabledUsers = (value: string[], flagKey: string) =>
+ userIds.filter((id) => evaluateFeatureFlag(value, id, flagKey));
+
+ it('buckets users by FNV-1a of "flagKey:userId" modulo 100', () => {
+ // FNV-1a("channel:user-1") = 4173342739 → bucket 39, derived outside the codebase
+ expect(evaluateFeatureFlag(['rollout:39'], 'user-1', 'channel')).toBe(false);
+ expect(evaluateFeatureFlag(['rollout:40'], 'user-1', 'channel')).toBe(true);
+ });
+
+ it('enables roughly the configured share of users', () => {
+ const share = enabledUsers(['rollout:10'], 'channel').length / userIds.length;
+
+ expect(share).toBeGreaterThan(0.08);
+ expect(share).toBeLessThan(0.12);
+ });
+
+ it('keeps every enabled user enabled when the percentage grows', () => {
+ const atTen = enabledUsers(['rollout:10'], 'channel');
+ const atTwenty = new Set(enabledUsers(['rollout:20'], 'channel'));
+
+ expect(atTen.every((id) => atTwenty.has(id))).toBe(true);
+ });
+
+ it('picks a different population for each flag key', () => {
+ const channelUsers = new Set(enabledUsers(['rollout:10'], 'channel'));
+ const overlap = enabledUsers(['rollout:10'], 'dashboard').filter((id) =>
+ channelUsers.has(id),
+ );
+
+ expect(overlap.length).toBeLessThan(channelUsers.size / 2);
+ });
+
+ it('enables nobody at 0% and everybody at 100%', () => {
+ expect(enabledUsers(['rollout:0'], 'channel')).toHaveLength(0);
+ expect(enabledUsers(['rollout:100'], 'channel')).toHaveLength(userIds.length);
+ });
+
+ it('still enables allowlisted users outside the rollout bucket', () => {
+ expect(evaluateFeatureFlag(['rollout:0', 'user-1'], 'user-1', 'channel')).toBe(true);
+ });
+
+ it('ignores malformed rollout tokens', () => {
+ for (const token of [
+ 'rollout:abc',
+ 'rollout:150',
+ 'rollout:-5',
+ 'rollout:10.5',
+ 'rollout:',
+ ]) {
+ expect(enabledUsers([token], 'channel')).toHaveLength(0);
+ }
+ });
+
+ it('does not treat the token as a user id', () => {
+ expect(evaluateFeatureFlag(['rollout:0'], 'rollout:0', 'channel')).toBe(false);
+ });
+
+ it('returns false for anonymous callers', () => {
+ expect(evaluateFeatureFlag(['rollout:100'], undefined, 'channel')).toBe(false);
+ });
+ });
});
describe('mapFeatureFlagsEnvToState', () => {
@@ -371,4 +435,13 @@ describe('mapFeatureFlagsEnvToState', () => {
expect(mappedState.enableKnowledgeBase).toBe(true);
expect(mappedState.enableRAGEval).toBe(true);
});
+
+ it('salts the rollout bucket with each flag key', () => {
+ // user-1 lands in bucket 39 for channel and 74 for dashboard
+ const config = { channel: ['rollout:40'], dashboard: ['rollout:40'] };
+ const mappedState = mapFeatureFlagsEnvToState(config, 'user-1');
+
+ expect(mappedState.enableChannels).toBe(true);
+ expect(mappedState.enableDashboard).toBe(false);
+ });
});
diff --git a/packages/app-config/src/featureFlags/schema.ts b/packages/app-config/src/featureFlags/schema.ts
index 2a889008b3a..7dc8c35e933 100644
--- a/packages/app-config/src/featureFlags/schema.ts
+++ b/packages/app-config/src/featureFlags/schema.ts
@@ -108,15 +108,45 @@ export type IFeatureFlags = z.infer<typeof FeatureFlagsSchema>;
* @param userId - The current user ID
* @returns boolean indicating if the feature is enabled for the user
*/
+// The percentage lives inside the allowlist array (e.g. `['rollout:10', 'user-a']`) instead of a
+// new value shape: every reader of the shared snapshot (this schema, develop-center, lobe-admin)
+// only accepts `boolean | string[]`, and an unknown shape makes the runtime drop the whole snapshot.
+const ROLLOUT_TOKEN_PATTERN = /^rollout:(\d{1,3})$/;
+
+const parseRolloutPercent = (values: string[]): number | undefined => {
+ for (const value of values) {
+ const match = ROLLOUT_TOKEN_PATTERN.exec(value);
+ if (!match) continue;
+
+ const percent = Number(match[1]);
+ if (percent <= 100) return percent;
+ }
+};
+
+const fnv1a = (input: string) => {
+ let hash = 0x81_1c_9d_c5;
+ for (const byte of new TextEncoder().encode(input)) {
+ hash ^= byte;
+ hash = Math.imul(hash, 0x01_00_01_93);
+ }
+ return hash >>> 0;
+};
+
export const evaluateFeatureFlag = (
flagValue: boolean | string[] | undefined,
userId?: string,
+ flagKey = '',
): boolean | undefined => {
if (typeof flagValue === 'boolean') return flagValue;
if (Array.isArray(flagValue)) {
- if (userId && flagValue.includes(userId)) return true;
- return false;
+ if (!userId) return false;
+ if (!ROLLOUT_TOKEN_PATTERN.test(userId) && flagValue.includes(userId)) return true;
+
+ const percent = parseRolloutPercent(flagValue);
+ if (percent === undefined) return false;
+
+ return fnv1a(`${flagKey}:${userId}`) % 100 < percent;
}
};
@@ -191,45 +221,53 @@ export const mapFeatureFlagsEnvToState = (
userId?: string,
): IFeatureFlagsState => {
return {
- isAgentEditable: evaluateFeatureFlag(config.edit_agent, userId),
-
- enableAgentShare: evaluateFeatureFlag(config.agent_share, userId),
- enableGatewayMuxMobile: evaluateFeatureFlag(config.agent_gateway_mux_mobile, userId),
- enableLlmRelay: evaluateFeatureFlag(config.agent_llm_relay, userId),
- enableDashboard: evaluateFeatureFlag(config.dashboard, userId),
- enableImLayout: evaluateFeatureFlag(config.im_layout, userId),
- enableChannels: evaluateFeatureFlag(config.channel, userId),
- enableTopicPreview: evaluateFeatureFlag(config.topic_preview, userId),
- showProvider: evaluateFeatureFlag(config.provider_settings, userId),
-
- showOpenAIApiKey: evaluateFeatureFlag(config.openai_api_key, userId),
- showOpenAIProxyUrl: evaluateFeatureFlag(config.openai_proxy_url, userId),
-
- showApiKeyManage: evaluateFeatureFlag(config.api_key_manage, userId),
-
- showAiImage: evaluateFeatureFlag(config.ai_image, userId),
- showChangelog: evaluateFeatureFlag(config.changelog, userId),
-
- enableCheckUpdates: evaluateFeatureFlag(config.check_updates, userId),
- showWelcomeSuggest: evaluateFeatureFlag(config.welcome_suggest, userId),
-
- enableKnowledgeBase: evaluateFeatureFlag(config.knowledge_base, userId),
- enableRAGEval: evaluateFeatureFlag(config.rag_eval, userId),
- enableAgentOnboarding: evaluateFeatureFlag(config.agent_onboarding, userId),
- enableDevDock: evaluateFeatureFlag(config.dev_dock, userId),
- enableAuthCaptcha: evaluateFeatureFlag(config.auth_captcha, userId),
- enableOnboardingV2: evaluateFeatureFlag(config.onboarding_v2, userId),
- enableStorageOverage: evaluateFeatureFlag(config.storage_overage, userId),
-
- showCloudPromotion: evaluateFeatureFlag(config.cloud_promotion, userId),
- enableWorkspace: evaluateFeatureFlag(config.workspace, userId),
-
- showMarket: evaluateFeatureFlag(config.market, userId),
- enableSTT: evaluateFeatureFlag(config.speech_to_text, userId),
- enableVoiceDictation: evaluateFeatureFlag(config.voice_dictation, userId),
-
- hideGitHub: evaluateFeatureFlag(config.commercial_hide_github, userId),
- hideDocs: evaluateFeatureFlag(config.commercial_hide_docs, userId),
+ isAgentEditable: evaluateFeatureFlag(config.edit_agent, userId, 'edit_agent'),
+
+ enableAgentShare: evaluateFeatureFlag(config.agent_share, userId, 'agent_share'),
+ enableGatewayMuxMobile: evaluateFeatureFlag(
+ config.agent_gateway_mux_mobile,
+ userId,
+ 'agent_gateway_mux_mobile',
+ ),
+ enableLlmRelay: evaluateFeatureFlag(config.agent_llm_relay, userId, 'agent_llm_relay'),
+ enableDashboard: evaluateFeatureFlag(config.dashboard, userId, 'dashboard'),
+ enableImLayout: evaluateFeatureFlag(config.im_layout, userId, 'im_layout'),
+ enableChannels: evaluateFeatureFlag(config.channel, userId, 'channel'),
+ enableTopicPreview: evaluateFeatureFlag(config.topic_preview, userId, 'topic_preview'),
+ showProvider: evaluateFeatureFlag(config.provider_settings, userId, 'provider_settings'),
+
+ showOpenAIApiKey: evaluateFeatureFlag(config.openai_api_key, userId, 'openai_api_key'),
+ showOpenAIProxyUrl: evaluateFeatureFlag(config.openai_proxy_url, userId, 'openai_proxy_url'),
+
+ showApiKeyManage: evaluateFeatureFlag(config.api_key_manage, userId, 'api_key_manage'),
+
+ showAiImage: evaluateFeatureFlag(config.ai_image, userId, 'ai_image'),
+ showChangelog: evaluateFeatureFlag(config.changelog, userId, 'changelog'),
+
+ enableCheckUpdates: evaluateFeatureFlag(config.check_updates, userId, 'check_updates'),
+ showWelcomeSuggest: evaluateFeatureFlag(config.welcome_suggest, userId, 'welcome_suggest'),
+
+ enableKnowledgeBase: evaluateFeatureFlag(config.knowledge_base, userId, 'knowledge_base'),
+ enableRAGEval: evaluateFeatureFlag(config.rag_eval, userId, 'rag_eval'),
+ enableAgentOnboarding: evaluateFeatureFlag(config.agent_onboarding, userId, 'agent_onboarding'),
+ enableDevDock: evaluateFeatureFlag(config.dev_dock, userId, 'dev_dock'),
+ enableAuthCaptcha: evaluateFeatureFlag(config.auth_captcha, userId, 'auth_captcha'),
+ enableOnboardingV2: evaluateFeatureFlag(config.onboarding_v2, userId, 'onboarding_v2'),
+ enableStorageOverage: evaluateFeatureFlag(config.storage_overage, userId, 'storage_overage'),
+
+ showCloudPromotion: evaluateFeatureFlag(config.cloud_promotion, userId, 'cloud_promotion'),
+ enableWorkspace: evaluateFeatureFlag(config.workspace, userId, 'workspace'),
+
+ showMarket: evaluateFeatureFlag(config.market, userId, 'market'),
+ enableSTT: evaluateFeatureFlag(config.speech_to_text, userId, 'speech_to_text'),
+ enableVoiceDictation: evaluateFeatureFlag(config.voice_dictation, userId, 'voice_dictation'),
+
+ hideGitHub: evaluateFeatureFlag(
+ config.commercial_hide_github,
+ userId,
+ 'commercial_hide_github',
+ ),
+ hideDocs: evaluateFeatureFlag(config.commercial_hide_docs, userId, 'commercial_hide_docs'),
};
};