@@ -8,6 +8,7 @@ use agents_core::agent::PlannerHandle;
88use agents_core:: persistence:: Checkpointer ;
99use agents_core:: tools:: ToolBox ;
1010use std:: collections:: { HashMap , HashSet } ;
11+ use std:: num:: NonZeroUsize ;
1112use std:: sync:: Arc ;
1213
1314/// Parameters for create_deep_agent() that mirror the Python API exactly
@@ -54,7 +55,7 @@ pub struct DeepAgentConfig {
5455 pub event_dispatcher : Option < Arc < agents_core:: events:: EventDispatcher > > ,
5556 pub enable_pii_sanitization : bool ,
5657 pub token_tracking_config : Option < TokenTrackingConfig > ,
57- pub max_iterations : usize ,
58+ pub max_iterations : NonZeroUsize ,
5859}
5960
6061impl DeepAgentConfig {
@@ -73,7 +74,7 @@ impl DeepAgentConfig {
7374 event_dispatcher : None ,
7475 enable_pii_sanitization : true , // Enabled by default for security
7576 token_tracking_config : None ,
76- max_iterations : 10 ,
77+ max_iterations : NonZeroUsize :: new ( 10 ) . unwrap ( ) ,
7778 }
7879 }
7980
@@ -183,9 +184,19 @@ impl DeepAgentConfig {
183184 }
184185
185186 /// Set the maximum number of ReAct loop iterations before stopping.
187+ ///
188+ /// **Note**: `max_iterations` must be greater than 0. Passing 0 will result in a panic.
189+ ///
190+ /// # Panics
191+ ///
192+ /// Panics if `max_iterations` is 0.
193+ ///
194+ /// # Default
195+ ///
186196 /// Defaults to 10 if not specified.
187197 pub fn with_max_iterations ( mut self , max_iterations : usize ) -> Self {
188- self . max_iterations = max_iterations;
198+ self . max_iterations =
199+ NonZeroUsize :: new ( max_iterations) . expect ( "max_iterations must be greater than 0" ) ;
189200 self
190201 }
191202}
@@ -310,14 +321,14 @@ mod tests {
310321 fn test_config_default_max_iterations ( ) {
311322 let planner = create_mock_planner ( ) ;
312323 let config = DeepAgentConfig :: new ( "test instructions" , planner) ;
313- assert_eq ! ( config. max_iterations, 10 ) ;
324+ assert_eq ! ( config. max_iterations. get ( ) , 10 ) ;
314325 }
315326
316327 #[ test]
317328 fn test_config_custom_max_iterations ( ) {
318329 let planner = create_mock_planner ( ) ;
319330 let config = DeepAgentConfig :: new ( "test instructions" , planner) . with_max_iterations ( 25 ) ;
320- assert_eq ! ( config. max_iterations, 25 ) ;
331+ assert_eq ! ( config. max_iterations. get ( ) , 25 ) ;
321332 }
322333
323334 #[ test]
@@ -329,7 +340,7 @@ mod tests {
329340 . with_prompt_caching ( true )
330341 . with_pii_sanitization ( false ) ;
331342
332- assert_eq ! ( config. max_iterations, 30 ) ;
343+ assert_eq ! ( config. max_iterations. get ( ) , 30 ) ;
333344 assert_eq ! ( config. auto_general_purpose, false ) ;
334345 assert_eq ! ( config. enable_prompt_caching, true ) ;
335346 assert_eq ! ( config. enable_pii_sanitization, false ) ;
@@ -341,7 +352,14 @@ mod tests {
341352 let config = DeepAgentConfig :: new ( "test instructions" , planner) . with_max_iterations ( 42 ) ;
342353
343354 // Verify the value is actually stored
344- assert_eq ! ( config. max_iterations, 42 ) ;
355+ assert_eq ! ( config. max_iterations. get( ) , 42 ) ;
356+ }
357+
358+ #[ test]
359+ #[ should_panic( expected = "max_iterations must be greater than 0" ) ]
360+ fn test_config_zero_max_iterations_panics ( ) {
361+ let planner = create_mock_planner ( ) ;
362+ let _config = DeepAgentConfig :: new ( "test instructions" , planner) . with_max_iterations ( 0 ) ;
345363 }
346364
347365 #[ test]
@@ -351,19 +369,19 @@ mod tests {
351369 // Test that max_iterations works with various combinations
352370 let config =
353371 DeepAgentConfig :: new ( "test instructions" , planner. clone ( ) ) . with_max_iterations ( 5 ) ;
354- assert_eq ! ( config. max_iterations, 5 ) ;
372+ assert_eq ! ( config. max_iterations. get ( ) , 5 ) ;
355373
356374 let config2 = DeepAgentConfig :: new ( "test instructions" , planner. clone ( ) )
357375 . with_prompt_caching ( true )
358376 . with_max_iterations ( 15 ) ;
359- assert_eq ! ( config2. max_iterations, 15 ) ;
377+ assert_eq ! ( config2. max_iterations. get ( ) , 15 ) ;
360378 assert_eq ! ( config2. enable_prompt_caching, true ) ;
361379
362380 let config3 = DeepAgentConfig :: new ( "test instructions" , planner)
363381 . with_auto_general_purpose ( false )
364382 . with_max_iterations ( 100 )
365383 . with_pii_sanitization ( true ) ;
366- assert_eq ! ( config3. max_iterations, 100 ) ;
384+ assert_eq ! ( config3. max_iterations. get ( ) , 100 ) ;
367385 assert_eq ! ( config3. auto_general_purpose, false ) ;
368386 assert_eq ! ( config3. enable_pii_sanitization, true ) ;
369387 }
0 commit comments