alibaba/open-code-review · error

scan: max_tool_request_times must be positive

Error message

scan: max_tool_request_times must be positive

What it means

ScanTemplate.Validate rejects a template whose MaxToolRequestTimes field is zero or negative. The library requires a positive cap on how many tool requests a scan conversation may make, so a template missing this setting (Go zero value 0) fails validation before any LLM run starts. It is a fast-fail config guard, not a runtime condition.

Source

Thrown at internal/config/template/template.go:334

	if t.MaxToolRequestTimes <= 0 {
		return fmt.Errorf("max_tool_request_times must be positive")
	}
	if t.MaxReviewRounds < 0 {
		return fmt.Errorf("max_review_rounds must not be negative")
	}
	if len(t.MainTask.Messages) == 0 {
		return fmt.Errorf("main_task.messages must not be empty")
	}
	return nil
}

// Validate checks that a ScanTemplate has the minimum fields populated.
func (t *ScanTemplate) Validate() error {
	if t.MaxTokens <= 0 {
		return fmt.Errorf("scan: max_tokens must be positive")
	}
	if t.MaxToolRequestTimes <= 0 {
		return fmt.Errorf("scan: max_tool_request_times must be positive")
	}
	if len(t.MainTask.Messages) == 0 {
		return fmt.Errorf("scan: main_task.messages must not be empty")
	}
	return nil
}

// LlmConversation is a preset prompt with settings.
type LlmConversation struct {
	Messages []ChatMessage `json:"messages"`
}

// ChatMessage represents a single message in a conversation.
type ChatMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Set max_tool_request_times to a positive integer in the template config
  2. If the field was renamed/missing, fix the key so it deserializes into MaxToolRequestTimes
  3. Pick a sane default (e.g. 20-50) based on how many tool calls a scan typically needs

Example fix

// before
template := template.ScanTemplate{ MaxTokens: 4096 }
if err := template.Validate(); err != nil { ... } // fails
// after
template := template.ScanTemplate{ MaxTokens: 4096, MaxToolRequestTimes: 30 }
if err := template.Validate(); err != nil { ... }
Defensive patterns

Strategy: validation

Validate before calling

func validTemplate(t template.ScanTemplate) bool { return t.MaxToolRequestTimes > 0 }

Try / catch

if err := t.Validate(); err != nil { return fmt.Errorf("invalid scan template: %w", err) }

Prevention

When it happens

Trigger: Calling Validate() on a ScanTemplate literal that leaves MaxToolRequestTimes unset (defaults to 0) or explicitly sets it to a negative number.

Common situations: Hand-written YAML/JSON scan templates missing the max_tool_request_times key; templates copied from older versions before the field existed; typos in the field name causing the value not to be deserialized.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02). Data as JSON: /api/errors/c394f798efa0f608. Report an issue: GitHub.