Skip to content

Commit f1787c4

Browse files
committed
feat(runtime): add auto-stop for max iterations in non-interactive mode
When max iterations are reached in non-interactive mode (e.g., MCP server), the runtime now automatically stops execution instead of blocking indefinitely waiting for user input. This prevents the system from hanging when `ToolsApproved` is true and provides a clear assistant message explaining why execution was stopped.
1 parent 84d3fc2 commit f1787c4

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

pkg/runtime/loop.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,24 @@ func (r *LocalRuntime) RunStream(ctx context.Context, sess *session.Session) <-c
175175
r.executeNotificationHooks(ctx, a, sess.ID, "warning", maxIterMsg)
176176
r.executeOnUserInputHooks(ctx, sess.ID, "max iterations reached")
177177

178+
// In non-interactive mode (e.g. MCP server), auto-stop instead of
179+
// blocking forever waiting for user input.
180+
if sess.ToolsApproved {
181+
slog.Debug("Auto-stopping after max iterations (non-interactive)", "agent", a.Name())
182+
183+
assistantMessage := chat.Message{
184+
Role: chat.MessageRoleAssistant,
185+
Content: fmt.Sprintf(
186+
"Execution stopped after reaching the configured max_iterations limit (%d).",
187+
runtimeMaxIterations,
188+
),
189+
CreatedAt: time.Now().Format(time.RFC3339),
190+
}
191+
192+
addAgentMessage(sess, a, &assistantMessage, events)
193+
return
194+
}
195+
178196
// Wait for user decision (resume / reject)
179197
select {
180198
case req := <-r.resumeChan:

0 commit comments

Comments
 (0)