{"record":{"id":"8756ff8e960b2fbb","repo":"alibaba/open-code-review","slug":"background-content-is-d-characters-exceeding-the","errorCode":null,"errorMessage":"background content is %d characters, exceeding the hard limit of %d (aborting)","messagePattern":"background content is (.+?) characters, exceeding the hard limit of (.+?) \\(aborting\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/opencodereview/background_file.go","lineNumber":102,"sourceCode":"\t}\n\n\tcleaned := sanitizeMarkdown(string(raw))\n\tif cleaned == \"\" {\n\t\treturn \"\", fmt.Errorf(\"background file %q is empty after sanitisation\", path)\n\t}\n\n\tif strings.Contains(cleaned, backgroundOpenTag) || strings.Contains(cleaned, backgroundCloseTag) {\n\t\treturn \"\", fmt.Errorf(\n\t\t\t\"background file %q must not contain the reserved delimiters %q or %q\",\n\t\t\tpath, backgroundOpenTag, backgroundCloseTag,\n\t\t)\n\t}\n\n\t// Enforce the limits on the cleaned content only: the wrapper delimiters add\n\t// overhead the user cannot control, so counting them would make the reported\n\t// character count misleading.\n\tif n := len([]rune(cleaned)); n > backgroundHardLimit {\n\t\treturn \"\", fmt.Errorf(\n\t\t\t\"background content is %d characters, exceeding the hard limit of %d (aborting)\",\n\t\t\tn, backgroundHardLimit,\n\t\t)\n\t} else if n > backgroundSoftLimit {\n\t\tfmt.Fprintf(os.Stderr,\n\t\t\t\"[ocr] --background-file content is %d characters, exceeding the recommended %d (continuing but review quality might be impacted)\\n\",\n\t\t\tn, backgroundSoftLimit,\n\t\t)\n\t}\n\n\treturn backgroundOpenTag + \"\\n\" + cleaned + \"\\n\" + backgroundCloseTag, nil\n}\n\nfunc sanitizeMarkdown(s string) string {\n\tvar b strings.Builder\n\tb.Grow(len(s))\n\n\tfor _, r := range s {","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/alibaba/open-code-review/blob/5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f/cmd/opencodereview/background_file.go#L84-L120","documentation":"loadBackgroundFile counts the runes of the sanitized background content and enforces a hard limit of backgroundHardLimit (8000) characters, aborting the review when exceeded. A softer 2000-character limit only warns. The limit keeps the injected background from consuming too much of the LLM's context and degrading review quality.","triggerScenarios":"`ocr review --background-file <path>` where the sanitized file contains more than 8000 characters (runes, not bytes).","commonSituations":"Dumping an entire architecture wiki page or coding standard document as background; concatenating multiple docs into one background file; a runaway export that pasted a whole codebase README plus design docs.","solutions":["Trim the file to under 8000 characters, keeping only the most decision-relevant guidance.","Summarize the long document into a condensed bullet list (under the 2000-char soft limit for best quality).","Split content: keep only project-specific rules in --background-file; rely on the commit message fallback or --background for short notes.","Check `wc -m <path>` against the limit before running the review."],"exampleFix":"// before: wc -m background.md -> 14500\n// after\nhead -c important-sections.txt background.md  # manually curate\nwc -m background.md  # -> 1800, under the soft limit","handlingStrategy":"validation","validationCode":"// shell: check rune/char count against ocr's hard limit before invoking\nchars=$(python3 -c \"import sys;print(len(open(sys.argv[1],encoding='utf-8').read()))\" \"$BG_FILE\")\n[ \"$chars\" -le 8000 ] || { echo \"background file too large: $chars chars\"; exit 1; }","typeGuard":null,"tryCatchPattern":"out, err := exec.Command(\"ocr\", \"review\", \"--background-file\", bg).CombinedOutput()\nif err != nil && strings.Contains(string(out), \"exceeding the hard limit\") {\n\treturn fmt.Errorf(\"trim %s below 8000 characters: %w\", bg, err)\n}","preventionTips":["Keep background files concise; target under the 2000-character soft limit for best review quality.","Run `wc -m background.md` before reviews; CI can enforce a max.","Summarize long docs into bullet lists rather than pasting full documents."],"tags":["cli","input-validation","size-limit"],"backgroundTag":"content-exceeds-hard-limit","analyzedSha":"5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f","analyzedAt":"2026-09-02T02:08:09.116Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}