Add network failure fallback and cache recovery
epic-organization-feature-flags-runtime-task-002 — Extend the FeatureFlagInitializer with a robust fallback mechanism: when the remote fetch fails (network error, timeout, or non-200 response), the initializer must transparently serve the last-known-good state from the persistent local cache, logging a warning without throwing so the bootstrap sequence is never blocked.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 1 - 540 tasks
Can start after Tier 0 completes
Implementation Notes
Use a try/catch wrapping the repository fetch, catching `Exception` broadly but logging the runtime type for diagnostics. Avoid catching `Error` (stack overflow, assertion errors) — only catch recoverable `Exception` subclasses. For the persistent cache key, use `'feature_flags_${orgId}'` to ensure strict org isolation. Consider a short connection timeout (e.g., 5 seconds via Supabase client config) so the fallback triggers quickly on mobile networks rather than blocking for 30+ seconds.
The `InitializationSource` value should be stored as a field on the initializer and exposed as a getter so FeatureFlagProvider can surface it via a diagnostic stream for QA builds.
Testing Requirements
Unit tests using mocktail to simulate: (1) SocketException on fetch with pre-populated cache — assert flags loaded from cache, no throw; (2) TimeoutException on fetch with pre-populated cache — same assertions; (3) SocketException with empty cache — assert safe defaults applied, no throw; (4) Successful fetch after previous fallback — assert cache is updated with fresh data. Verify InitializationSource value in each scenario.
The feature-flag-initializer must complete before any screen that checks feature flags renders. If the navigation shell is pushed concurrently with initialization (e.g., via a parallel Riverpod provider chain), some screens may query flags before they are loaded and incorrectly receive all-disabled defaults.
Mitigation & Contingency
Mitigation: Gate the main navigation shell render behind the feature-flag-initializer's Future by using a splash/loading screen that awaits the initialization provider. Use Riverpod's ref.watch on an initialization state enum (loading/ready/error) to block rendering.
Contingency: If race conditions are observed in testing, introduce an explicit initialization barrier using a ChangeNotifier or a dedicated `featureFlagsReadyProvider` that the router guard checks before allowing navigation.
If feature-flag-provider is watched by many widgets simultaneously and a flag map refresh triggers all of them to rebuild at once (e.g., after an organization switch), the app could experience a significant UI jank or dropped frames.
Mitigation & Contingency
Mitigation: Use select() on the provider to have each widget watch only the specific flag key it needs rather than the entire map. Ensure the provider uses equality checks so rebuilds only propagate when the specific flag value changes.
Contingency: If rebuild storms are measured via Flutter DevTools, refactor to a family provider keyed by flag key, so each widget subscribes only to its own flag's changes.