Skip to content

Commit

Permalink
invert experiment flag
Browse files Browse the repository at this point in the history
  • Loading branch information
xrsv committed Mar 3, 2025
1 parent dcdead7 commit 2ad9f77
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
21 changes: 11 additions & 10 deletions src/routers/alpha-router/alpha-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3200,21 +3200,22 @@ export class AlphaRouter
);
}

// Percentage of time we allow using cached routes. We start with 99% to gradually roll out the change.
public static readonly CACHED_ROUTES_EXPERIMENT_FLAG_ENABLED_DEFAULT_PERCENTAGE = 0.99;
// Percentage of time we want to skip cached routes for the experiment.
// Starting with 1% to gradually roll out the change.
public static readonly CACHED_ROUTES_SKIP_EXPERIMENT_FLAG_PERCENTAGE = 0.01;

// We want to skip cached routes access whenever "intent === INTENT.CACHING".
// To verify this functionality though, we want to start by using a percentage of the time.
public static isAllowedToEnterCachedRoutes(intent?: INTENT): boolean {
// By default, we allow the access to cached routes (original functionality - 99% of the time)
const shouldAllow =
Math.random() <
AlphaRouter.CACHED_ROUTES_EXPERIMENT_FLAG_ENABLED_DEFAULT_PERCENTAGE;
if (shouldAllow) {
return true;
// Check if we should run the experiment
const shouldRunExperiment =
Math.random() < AlphaRouter.CACHED_ROUTES_SKIP_EXPERIMENT_FLAG_PERCENTAGE;
if (shouldRunExperiment) {
// For the experiment group, we want to skip the cached routes access if the intent is caching
return intent !== INTENT.CACHING;
}

// For the remaining percent of the time, we want to skip the cached routes access if the intent is caching.
return intent !== INTENT.CACHING;
// For the control group, we always allow access to cached routes.
return true;
}
}
8 changes: 4 additions & 4 deletions test/unit/routers/alpha-router/alpha-router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3101,14 +3101,14 @@ describe('alpha router', () => {
});

test('returns correct values based on random percentage and intent', () => {
// Test when random is below threshold (should always return true)
randomStub.returns(AlphaRouter.CACHED_ROUTES_EXPERIMENT_FLAG_ENABLED_DEFAULT_PERCENTAGE - 0.01);
// Test when random is above experiment threshold (control group - should always return true)
randomStub.returns(AlphaRouter.CACHED_ROUTES_SKIP_EXPERIMENT_FLAG_PERCENTAGE + 0.01);
expect(AlphaRouter.isAllowedToEnterCachedRoutes(INTENT.CACHING)).toBe(true);
expect(AlphaRouter.isAllowedToEnterCachedRoutes(INTENT.QUOTE)).toBe(true);
expect(AlphaRouter.isAllowedToEnterCachedRoutes(undefined)).toBe(true);

// Test when random is above threshold
randomStub.returns(AlphaRouter.CACHED_ROUTES_EXPERIMENT_FLAG_ENABLED_DEFAULT_PERCENTAGE + 0.001);
// Test when random is below experiment threshold (experiment group)
randomStub.returns(AlphaRouter.CACHED_ROUTES_SKIP_EXPERIMENT_FLAG_PERCENTAGE - 0.001);
// Should return false only for CACHING intent
expect(AlphaRouter.isAllowedToEnterCachedRoutes(INTENT.CACHING)).toBe(false);
// Should return true for other intents
Expand Down

0 comments on commit 2ad9f77

Please sign in to comment.