@@ -35,6 +35,35 @@ func (e RuntimeError) Unwrap() error {
3535// auto-continue when max iterations is reached, to prevent infinite loops.
3636const maxAutoExtensions = 5
3737
38+ // maxIterAction describes what the caller should do after a MaxIterationsReachedEvent.
39+ type maxIterAction int
40+
41+ const (
42+ maxIterContinue maxIterAction = iota // auto-approved, keep running
43+ maxIterStop // safety cap reached, caller should stop
44+ maxIterPrompt // not in yolo mode, caller should prompt the user
45+ )
46+
47+ // handleMaxIterationsAutoApprove decides whether to auto-extend iterations in
48+ // --yolo mode. Returns maxIterContinue (approved), maxIterStop (cap reached),
49+ // or maxIterPrompt (not in auto-approve mode, caller should ask the user).
50+ func handleMaxIterationsAutoApprove (autoApprove bool , autoExtensions * int , maxIter int ) maxIterAction {
51+ if ! autoApprove {
52+ return maxIterPrompt
53+ }
54+ * autoExtensions ++
55+ if * autoExtensions <= maxAutoExtensions {
56+ slog .Info ("Auto-extending iterations in yolo mode" ,
57+ "extension" , * autoExtensions ,
58+ "max_extensions" , maxAutoExtensions ,
59+ "current_max" , maxIter )
60+ return maxIterContinue
61+ }
62+ slog .Warn ("Max auto-extensions reached in yolo mode, stopping" ,
63+ "total_extensions" , * autoExtensions )
64+ return maxIterStop
65+ }
66+
3867// Config holds configuration for running an agent in CLI mode
3968type Config struct {
4069 AppName string
@@ -81,21 +110,10 @@ func Run(ctx context.Context, out *Printer, cfg Config, rt runtime.Runtime, sess
81110 rt .Resume (ctx , runtime .ResumeReject ("" ))
82111 }
83112 case * runtime.MaxIterationsReachedEvent :
84- if cfg .AutoApprove {
85- autoExtensions ++
86- if autoExtensions <= maxAutoExtensions {
87- slog .Info ("Auto-extending iterations in yolo mode (json)" ,
88- "extension" , autoExtensions ,
89- "max_extensions" , maxAutoExtensions ,
90- "current_max" , e .MaxIterations )
91- rt .Resume (ctx , runtime .ResumeApprove ())
92- } else {
93- slog .Warn ("Max auto-extensions reached in yolo mode (json), stopping" ,
94- "total_extensions" , autoExtensions )
95- rt .Resume (ctx , runtime .ResumeReject ("" ))
96- return nil
97- }
98- } else {
113+ switch handleMaxIterationsAutoApprove (cfg .AutoApprove , & autoExtensions , e .MaxIterations ) {
114+ case maxIterContinue :
115+ rt .Resume (ctx , runtime .ResumeApprove ())
116+ default : // maxIterStop or maxIterPrompt (no interactive prompt in JSON mode)
99117 rt .Resume (ctx , runtime .ResumeReject ("" ))
100118 return nil
101119 }
@@ -178,21 +196,13 @@ func Run(ctx context.Context, out *Printer, cfg Config, rt runtime.Runtime, sess
178196 out .PrintError (lastErr )
179197 }
180198 case * runtime.MaxIterationsReachedEvent :
181- if cfg .AutoApprove {
182- autoExtensions ++
183- if autoExtensions <= maxAutoExtensions {
184- slog .Info ("Auto-extending iterations in yolo mode" ,
185- "extension" , autoExtensions ,
186- "max_extensions" , maxAutoExtensions ,
187- "current_max" , e .MaxIterations )
188- rt .Resume (ctx , runtime .ResumeApprove ())
189- } else {
190- slog .Warn ("Max auto-extensions reached in yolo mode, stopping" ,
191- "total_extensions" , autoExtensions )
192- rt .Resume (ctx , runtime .ResumeReject ("" ))
193- return nil
194- }
195- } else {
199+ switch handleMaxIterationsAutoApprove (cfg .AutoApprove , & autoExtensions , e .MaxIterations ) {
200+ case maxIterContinue :
201+ rt .Resume (ctx , runtime .ResumeApprove ())
202+ case maxIterStop :
203+ rt .Resume (ctx , runtime .ResumeReject ("" ))
204+ return nil
205+ case maxIterPrompt :
196206 result := out .PromptMaxIterationsContinue (ctx , e .MaxIterations )
197207 switch result {
198208 case ConfirmationApprove :
0 commit comments