@@ -113,25 +113,11 @@ type recentCall struct {
113113 }
114114
115115 func ProcessTurn(ctx context.Context, opts ProcessTurnOptions) error {
116- // Enforce max depth to prevent infinite recursion
117- if opts.Depth > MaxDepth {
118- return fmt.Errorf("max sub-agent depth exceeded: %d > %d", opts.Depth, MaxDepth)
119- }
120-
121- if opts.MaxToolRounds == 0 {
122- opts.MaxToolRounds = 60
123- }
124-
125- // Convert MCP tools to provider format once
126- providerTools := make([]provider.Tool, len(opts.Tools))
127- for i, t := range opts.Tools {
128- providerTools[i] = provider.Tool{
129- Name: t.Name,
130- Description: t.Description,
131- Parameters: t.InputSchema,
132- }
116+ if err := normalizeTurnOptions(&opts); err != nil {
117+ return err
133118 }
134119
120+ providerTools := toProviderTools(opts.Tools)
135121 var recent []recentCall
136122 for round := 0; round < opts.MaxToolRounds; round++ {
137123 // Inject a <system-reminder> into the last tool result to keep
@@ -160,30 +146,63 @@ func ProcessTurn(ctx context.Context, opts ProcessTurnOptions) error {
160146 // Execute each tool call and update history
161147 toolResults := executeToolCalls(ctx, opts.Proxy, resp.ToolCalls, opts.OnMessage)
162148 opts.History = append(opts.History, toolResults...)
163-
164- for _, tc := range resp.ToolCalls {
165- recent = append(recent, recentCall{Name: tc.Name, Args: string(tc.Arguments)})
166- }
167- if len(recent) >= 3 {
168- last3 := recent[len(recent)-3:]
169- if last3[0] == last3[1] && last3[1] == last3[2] {
170- if len(toolResults) > 0 {
171- last := &toolResults[len(toolResults)-1]
172- last.Content += "\n\n<system-reminder>WARNING: You are repeating the same tool call with the same arguments. This is wasteful. Stop and either try a different approach, summarize what you know, or ask the user for help.</system-reminder>"
173- opts.History[len(opts.History)-1] = *last
174- }
175- }
176- }
149+ appendRecentCalls(&opts, resp.ToolCalls, toolResults, &recent)
177150
178151 // Continue loop to let LLM process tool results
179152 }
180153
181154 // Tool call limit reached — do one final call with no tools so the LLM
182155 // must reply with text summarizing progress.
156+ return finalizeToolLimit(ctx, &opts)
157+}
158+
159+func normalizeTurnOptions(opts *ProcessTurnOptions) error {
160+ // Enforce max depth to prevent infinite recursion
161+ if opts.Depth > MaxDepth {
162+ return fmt.Errorf("max sub-agent depth exceeded: %d > %d", opts.Depth, MaxDepth)
163+ }
164+ if opts.MaxToolRounds == 0 {
165+ opts.MaxToolRounds = 60
166+ }
167+ return nil
168+}
169+
170+func toProviderTools(tools []mcp.Tool) []provider.Tool {
171+ if len(tools) == 0 {
172+ return nil
173+ }
174+ providerTools := make([]provider.Tool, len(tools))
175+ for i, t := range tools {
176+ providerTools[i] = provider.Tool{
177+ Name: t.Name,
178+ Description: t.Description,
179+ Parameters: t.InputSchema,
180+ }
181+ }
182+ return providerTools
183+}
184+
185+func appendRecentCalls(opts *ProcessTurnOptions, toolCalls []provider.ToolCall, toolResults []provider.Message, recent *[]recentCall) {
186+ for _, tc := range toolCalls {
187+ *recent = append(*recent, recentCall{Name: tc.Name, Args: string(tc.Arguments)})
188+ }
189+ if len(*recent) < 3 {
190+ return
191+ }
192+ last3 := (*recent)[len(*recent)-3:]
193+ if last3[0] == last3[1] && last3[1] == last3[2] {
194+ if len(toolResults) > 0 && len(opts.History) > 0 {
195+ last := &toolResults[len(toolResults)-1]
196+ last.Content += "\n\n<system-reminder>WARNING: You are repeating the same tool call with the same arguments. This is wasteful. Stop and either try a different approach, summarize what you know, or ask the user for help.</system-reminder>"
197+ opts.History[len(opts.History)-1] = *last
198+ }
199+ }
200+}
201+
202+func finalizeToolLimit(ctx context.Context, opts *ProcessTurnOptions) error {
183203 if err := ctx.Err(); err != nil {
184204 return err
185205 }
186-
187206 limitMsg := provider.Message{
188207 Role: "user",
189208 Content: "You have exhausted your tool call limit for this turn. Respond in text only. Summarize what you accomplished and what remains.",
@@ -194,12 +213,12 @@ func ProcessTurn(ctx context.Context, opts ProcessTurnOptions) error {
194213 }
195214 opts.History = append(opts.History, limitMsg)
196215
197- resp, err := streamAndCollect(ctx, &opts, nil)
216+ resp, err := streamAndCollect(ctx, opts, nil)
198217 if err != nil {
199218 return fmt.Errorf("final text-only LLM stream failed: %w", err)
200219 }
201220
202- emitAssistant(&opts, resp)
221+ emitAssistant(opts, resp)
203222 return nil
204223 }
205224