alibaba/open-code-review · error

invalid max_tokens in app config: must be a positive integer

Error message

invalid max_tokens in app config: must be a positive integer

What it means

resolveMaxTokens rejects a negative max_tokens saved in the app config file. The CLI override was absent (0), the saved cfg.MaxTokens was used, and it was negative — config files allow 0 (meaning 'unset') but not negative values.

Source

Thrown at cmd/opencodereview/shared.go:60

	// true when requireGit was set; may be false when scan accepts non-git
	// directories.
	IsGitRepo bool
}

// resolveMaxTokens applies the per-run CLI override, then the saved setting,
// and finally the embedded task-template default.
func resolveMaxTokens(templateDefault int, cfg *Config, cliOverride int) (int, error) {
	if cliOverride < 0 {
		return 0, fmt.Errorf("--max-tokens must be a non-negative integer")
	}
	if cliOverride > 0 {
		return cliOverride, nil
	}
	if cfg == nil || cfg.MaxTokens == 0 {
		return templateDefault, nil
	}
	if cfg.MaxTokens < 0 {
		return 0, fmt.Errorf("invalid max_tokens in app config: must be a positive integer")
	}
	return cfg.MaxTokens, nil
}

// resolveEffort applies the standard precedence for the review effort preset:
// CLI flag > saved app config > EffortDefault.
func resolveEffort(cfg *Config, cliOverride string) (template.Effort, error) {
	if cliOverride != "" {
		return template.ParseEffort(cliOverride)
	}
	if cfg != nil && cfg.Effort != "" {
		return template.ParseEffort(cfg.Effort)
	}
	return template.EffortDefault, nil
}

// loadCommonContext validates the working directory, loads the embedded
// template, raises MaxToolRequestTimes when maxTools exceeds the default,

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Open the app config and set max_tokens to a positive integer, or remove/zero it to use the template default
  2. Re-set it via the CLI/settings command (`--max-tokens` with a positive value persisted to config)
  3. If a tool wrote -1, fix the writer or reset config to defaults

Example fix

// before (config)
"max_tokens": -1
// after (config)
"max_tokens": 20000   // or delete the key to use the template default
Defensive patterns

Strategy: validation

Validate before calling

// sanitize config before passing to resolveMaxTokens
if cfg != nil && cfg.MaxTokens < 0 {
    return errors.New("max_tokens in app config must be >= 0")
}

Type guard

func validConfigMaxTokens(c *Config) bool { return c == nil || c.MaxTokens >= 0 }

Try / catch

limit, err := resolveMaxTokens(def, cfg, 0)
if err != nil {
    return fmt.Errorf("fix max_tokens in your app config: %w", err)
}

Prevention

When it happens

Trigger: App config (~/.config/... ocr config) contains max_tokens: -1 or another negative number; config edited by hand or written by a buggy script/tool.

Common situations: Manual config editing mistakes; a migration or old version wrote an invalid sentinel like -1 for 'auto'.

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/3cc151f68b0a5a4b. Report an issue: GitHub.