Skip to content

Automate browser based workflows with AI

Bound feature-flag flip staleness to snapshot sync (SKY-12962) (#7704)

pedrohsdb79371567980b
diff --git a/skyvern/forge/sdk/experimentation/providers.py b/skyvern/forge/sdk/experimentation/providers.py
index 2b2e273f7..25a4ad179 100644
--- a/skyvern/forge/sdk/experimentation/providers.py
+++ b/skyvern/forge/sdk/experimentation/providers.py
@@ -178,6 +178,15 @@ class BaseExperimentationProvider(ABC):
async def _prepare_feature_flag_resolution(self, feature_name: str, *, cached: bool) -> None:
return None
+ def invalidate_resolution_caches(self) -> int:
+ """Drop all cached resolutions; returns how many entries were dropped."""
+ dropped = len(self.result_map) + len(self.tri_state_map) + len(self.variant_map) + len(self.payload_map)
+ self.result_map.clear()
+ self.tri_state_map.clear()
+ self.variant_map.clear()
+ self.payload_map.clear()
+ return dropped
+
async def is_feature_enabled(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> bool:
await self._prepare_feature_flag_resolution(feature_name, cached=False)
feature_flag_value = await self._is_feature_enabled(feature_name, distinct_id, properties)
@@ -195,6 +204,9 @@ class BaseExperimentationProvider(ABC):
await self._prepare_feature_flag_resolution(feature_name, cached=True)
return await self._is_feature_enabled(feature_name, distinct_id, properties)
+ # The prepare hook is the only suspension point in the cached resolvers:
+ # nothing may await between cache consult and store, or a concurrent data
+ # refresh could interleave and hits would lose generation consistency.
async def is_feature_enabled_cached(
self, feature_name: str, distinct_id: str, properties: dict | None = None
) -> bool:
@@ -202,12 +214,15 @@ class BaseExperimentationProvider(ABC):
if should_bypass_feature_flag_cache(feature_name):
await self._prepare_feature_flag_resolution(feature_name, cached=False)
feature_flag_value = await self._is_feature_enabled(feature_name, distinct_id, properties)
- elif cache_key in self.result_map:
- feature_flag_value = self.result_map[cache_key]
else:
+ # Freshness first: a provider that reloads flag data here may
+ # invalidate the maps, so a hit below is generation-consistent.
await self._prepare_feature_flag_resolution(feature_name, cached=True)
- feature_flag_value = await self._is_feature_enabled(feature_name, distinct_id, properties)
- self.result_map[cache_key] = feature_flag_value
+ if cache_key in self.result_map:
+ feature_flag_value = self.result_map[cache_key]
+ else:
+ feature_flag_value = await self._is_feature_enabled(feature_name, distinct_id, properties)
+ self.result_map[cache_key] = feature_flag_value
record_feature_flag_resolution(
feature_name=feature_name,
resolution_kind="enabled",
@@ -232,12 +247,13 @@ class BaseExperimentationProvider(ABC):
if should_bypass_feature_flag_cache(feature_name):
await self._prepare_feature_flag_resolution(feature_name, cached=False)
resolved = await self._resolve_feature_flag(feature_name, distinct_id, properties)
- elif cache_key in self.tri_state_map:
- resolved = self.tri_state_map[cache_key]
else:
await self._prepare_feature_flag_resolution(feature_name, cached=True)
- resolved = await self._resolve_feature_flag(feature_name, distinct_id, properties)
- self.tri_state_map[cache_key] = resolved
+ if cache_key in self.tri_state_map:
+ resolved = self.tri_state_map[cache_key]
+ else:
+ resolved = await self._resolve_feature_flag(feature_name, distinct_id, properties)
+ self.tri_state_map[cache_key] = resolved
record_feature_flag_resolution(
feature_name=feature_name,
resolution_kind="enabled",
@@ -275,10 +291,10 @@ class BaseExperimentationProvider(ABC):
async def get_value_cached(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
cache_key = _make_cache_key(feature_name, distinct_id, properties)
+ await self._prepare_feature_flag_resolution(feature_name, cached=True)
if cache_key in self.variant_map:
variant = self.variant_map[cache_key]
else:
- await self._prepare_feature_flag_resolution(feature_name, cached=True)
variant = await self._get_value(feature_name, distinct_id, properties)
self.variant_map[cache_key] = variant
record_feature_flag_resolution(
@@ -290,10 +306,10 @@ class BaseExperimentationProvider(ABC):
async def get_payload_cached(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> Any:
cache_key = _make_cache_key(feature_name, distinct_id, properties)
+ await self._prepare_feature_flag_resolution(feature_name, cached=True)
if cache_key in self.payload_map:
payload = self.payload_map[cache_key]
else:
- await self._prepare_feature_flag_resolution(feature_name, cached=True)
payload = await self._get_payload(feature_name, distinct_id, properties)
self.payload_map[cache_key] = payload
record_feature_flag_resolution(
diff --git a/tests/unit/test_feature_flag_data_hash_cache.py b/tests/unit/test_feature_flag_data_hash_cache.py
index 35898c19d..0c87cf745 100644
--- a/tests/unit/test_feature_flag_data_hash_cache.py
+++ b/tests/unit/test_feature_flag_data_hash_cache.py
@@ -88,10 +88,13 @@ async def test_feature_enabled_cached_bypasses_known_kill_switch_flags() -> None
assert "RATE_LIMITING_ENABLED" in FEATURE_FLAG_CACHE_BYPASS_NAMES
assert provider.enabled_calls == 3
+ # Freshness runs before every cached consult (TTL-gated inside the
+ # provider), so hits also record a cached-mode prepare call.
assert provider.prepare_calls == [
("RATE_LIMITING_ENABLED", False),
("RATE_LIMITING_ENABLED", False),
("NOT_A_KILL_SWITCH", True),
+ ("NOT_A_KILL_SWITCH", True),
]