{"record":{"id":"696215670f544e20","repo":"alibaba/open-code-review","slug":"invalid-scan-template-w","errorCode":null,"errorMessage":"invalid scan template: %w","messagePattern":"invalid scan template: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/opencodereview/scan_cmd.go","lineNumber":136,"sourceCode":"\t\t\tretErr = errors.Join(retErr, fmt.Errorf(\"close output file: %w\", cerr))\n\t\t}\n\t}()\n\n\tcc, err := loadCommonContext(opts.repoDir, opts.rulePath, \"\", opts.maxTools, opts.maxGitProcs, false)\n\tif err != nil {\n\t\treturn err\n\t}\n\tapplyCLIExcludes(cc, splitPaths(opts.excludes))\n\n\t// scan owns its own template (scan_template.json) independent from the\n\t// diff-review template loaded by loadCommonContext above. Apply --max-tools\n\t// as an \"only raise\" override to the scan template's per-file budget.\n\tscanTpl, err := template.LoadScanDefault()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"load scan template: %w\", err)\n\t}\n\tif err := scanTpl.Validate(); err != nil {\n\t\treturn fmt.Errorf(\"invalid scan template: %w\", err)\n\t}\n\tif opts.maxTools > scanTpl.MaxToolRequestTimes {\n\t\tscanTpl.MaxToolRequestTimes = opts.maxTools\n\t}\n\tif opts.batch != \"\" {\n\t\t// CLI override of BATCH_STRATEGY; validated downstream by parseBatchStrategy\n\t\t// (unknown values silently fall back to \"none\").\n\t\tscanTpl.BatchStrategy = opts.batch\n\t}\n\t// Token budget: --max-tokens-budget overrides the template value when set.\n\tbudget := scanTpl.MaxTokensBudget\n\tif opts.maxTokensBudget > 0 {\n\t\tbudget = int64(opts.maxTokensBudget)\n\t}\n\n\tscanPaths := splitPaths(opts.paths)\n\n\tif opts.preview {","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/alibaba/open-code-review/blob/5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f/cmd/opencodereview/scan_cmd.go#L118-L154","documentation":"After loading the embedded scan template, `ocr scan` calls ScanTemplate.Validate(). If the template's own invariants are violated (e.g. non-positive MaxTokens or MaxToolRequestTimes, invalid budget/prompt fields), the CLI wraps the error as \"invalid scan template: %w\" and aborts before scanning. Like [130], the template is embedded, so this usually indicates a bad release artifact rather than user configuration.","triggerScenarios":"Running `ocr scan` (with or without --max-tools/--batch overrides applied later) when the parsed ScanTemplate fails Validate() — e.g. MaxTokens <= 0 or MaxToolRequestTimes <= 0 baked into the embedded scan_template.json.","commonSituations":"Using a forked or patched binary where scan_template.json was hand-edited with a zero/negative budget; a regression release where template validation constants drifted.","solutions":["Rebuild/reinstall ocr from an official source so the embedded scan_template.json passes Validate().","Read the wrapped message — the inner text names the violated field (e.g. \"max_tokens must be positive\") and fix it in scan_template.json if you own the checkout.","Check whether a --max-tools or environment override in your wrapper script is interacting with a patched template; test with a stock binary."],"exampleFix":"// before (hand-edited template in a fork)\n\"max_tool_request_times\": 0\n// after\n\"max_tool_request_times\": 40","handlingStrategy":"validation","validationCode":"tpl, err := template.LoadScanDefault()\nif err == nil {\n    err = tpl.Validate() // surfaces \"max_tokens must be positive\" etc.\n}\nif err != nil {\n    return err // run nothing until template is valid\n}","typeGuard":"func templateValid(t *template.ScanTemplate) bool {\n    return t != nil && t.MaxTokens > 0 && t.MaxToolRequestTimes > 0\n}","tryCatchPattern":"if err := scanTpl.Validate(); err != nil {\n    log.Fatalf(\"invalid scan template: %v\", err) // inner text names the bad field\n}","preventionTips":["Run template validation unit tests before building release binaries.","Keep budget fields (max_tokens, max_tool_request_times) strictly positive in any fork.","Smoke-test `ocr scan --preview` with a stock build after template changes."],"tags":["go","cli","template","validation"],"backgroundTag":"template-validation-failed","analyzedSha":"5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f","analyzedAt":"2026-09-02T02:08:09.116Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}