@@ -379,7 +379,8 @@ export function DemoCallProvider({ children, initialAgentId }: { children: React
379379 . from ( 'call_history' )
380380 . select ( '*' )
381381 . eq ( 'user_id' , user . id )
382- . order ( 'date' , { ascending : false } ) ;
382+ . order ( 'date' , { ascending : false } )
383+ . limit ( 50 ) ;
383384
384385 if ( ! historyError && historyData && historyData . length > 0 ) {
385386 // Map Supabase snake_case to app camelCase
@@ -582,9 +583,9 @@ export function DemoCallProvider({ children, initialAgentId }: { children: React
582583 // Initial cleanup then fetch
583584 cleanupStaleSessions ( ) . then ( ( ) => fetchActiveSessions ( ) ) ;
584585
585- // Periodically cleanup stale sessions every minute
586+ // Periodically cleanup stale sessions and refresh global count every minute
586587 const cleanupInterval = setInterval ( ( ) => {
587- cleanupStaleSessions ( ) ;
588+ cleanupStaleSessions ( ) . then ( ( ) => fetchActiveSessions ( ) ) ;
588589 } , 60 * 1000 ) ;
589590
590591 // Subscribe to changes (debounced to avoid UI thrashing under high load)
@@ -597,10 +598,10 @@ export function DemoCallProvider({ children, initialAgentId }: { children: React
597598 } ;
598599
599600 const subscription = supabase
600- . channel ( 'active_sessions_changes ' )
601+ . channel ( 'active_sessions_global_count ' )
601602 . on (
602603 'postgres_changes' ,
603- { event : '*' , schema : 'public' , table : 'active_sessions' } ,
604+ { event : '*' , schema : 'public' , table : 'active_sessions' , filter : `user_id=eq. ${ user ?. id ?? '' } ` } ,
604605 ( payload ) => {
605606 console . log ( '📡 Active sessions change:' , payload . eventType ) ;
606607 debouncedFetch ( ) ;
@@ -817,7 +818,9 @@ export function DemoCallProvider({ children, initialAgentId }: { children: React
817818 }
818819 } ;
819820 updateToken ( ) ;
820- const interval = setInterval ( updateToken , 60000 ) ; // Refresh every 60s
821+ // Jitter prevents 1M sessions all refreshing at the same wall-clock second
822+ const jitter = Math . random ( ) * 30_000 ; // 0–30s random offset
823+ const interval = setInterval ( updateToken , 60_000 + jitter ) ;
821824 return ( ) => clearInterval ( interval ) ;
822825 } , [ ] ) ;
823826
0 commit comments