@@ -187,10 +187,26 @@ async def restore_state(self, session_id: str) -> Optional[str]:
187187
188188 state_data = state_bytes .decode ("utf-8" )
189189
190- # Restore to Redis for fast access
191- await self .state_service .save_state (
192- session_id , state_data , ttl_seconds = settings .state_ttl_seconds
193- )
190+ # Only restore to Redis if under the size threshold
191+ import base64 as _b64
192+
193+ raw_size = len (_b64 .b64decode (state_data ))
194+ max_redis_bytes = settings .state_max_redis_size_mb * 1024 * 1024
195+
196+ if raw_size <= max_redis_bytes :
197+ await self .state_service .save_state (
198+ session_id , state_data , ttl_seconds = settings .state_ttl_seconds
199+ )
200+ else :
201+ # Too large for Redis — save only pointer
202+ await self .state_service .save_state_pointer (
203+ session_id , state_data , ttl_seconds = settings .state_ttl_seconds
204+ )
205+ logger .info (
206+ "State too large for Redis, kept in MinIO only" ,
207+ session_id = session_id [:12 ],
208+ state_size_mb = round (raw_size / 1024 / 1024 , 1 ),
209+ )
194210
195211 logger .info (
196212 "Restored state from MinIO" ,
@@ -504,10 +520,16 @@ async def restore_state_by_hash(self, state_hash: str) -> Optional[str]:
504520
505521 state_data = state_bytes .decode ("utf-8" )
506522
507- # Restore to Redis for fast access
508- await self .state_service .save_state_by_hash (
509- state_hash , state_data , ttl_seconds = settings .state_ttl_seconds
510- )
523+ # Only restore to Redis if under the size threshold
524+ import base64 as _b64
525+
526+ raw_size = len (_b64 .b64decode (state_data ))
527+ max_redis_bytes = settings .state_max_redis_size_mb * 1024 * 1024
528+
529+ if raw_size <= max_redis_bytes :
530+ await self .state_service .save_state_by_hash (
531+ state_hash , state_data , ttl_seconds = settings .state_ttl_seconds
532+ )
511533
512534 logger .debug (
513535 "Restored state by hash from MinIO" ,
0 commit comments