{"record":{"id":"bb50a90e5d492109","repo":"JuliusBrussee/caveman","slug":"openai-compress-invalid-splice-range","errorCode":null,"errorMessage":"openai compress: invalid splice range","messagePattern":"openai compress: invalid splice range","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"proxy/providers/openai/content_compress.go","lineNumber":651,"sourceCode":"\tvar buf bytes.Buffer\n\tenc := json.NewEncoder(&buf)\n\tenc.SetEscapeHTML(false)\n\tif err := enc.Encode(s); err != nil {\n\t\treturn nil, err\n\t}\n\treturn bytes.TrimSuffix(buf.Bytes(), []byte(\"\\n\")), nil\n}\n\nfunc spliceStringReplacements(body []byte, candidates []spliceCandidate, reps [][]byte) ([]byte, error) {\n\tif len(reps) != len(candidates) {\n\t\treturn nil, fmt.Errorf(\"openai compress: %d replacements for %d segments\", len(reps), len(candidates))\n\t}\n\tvar out []byte\n\tlast := 0\n\tchanged := false\n\tfor i, c := range candidates {\n\t\tif c.start < last || c.end > len(body) || c.start >= c.end {\n\t\t\treturn nil, fmt.Errorf(\"openai compress: invalid splice range\")\n\t\t}\n\t\trep := reps[i]\n\t\tif rep == nil || bytes.Equal(rep, c.original) {\n\t\t\tcontinue\n\t\t}\n\t\tquoted, err := quoteJSONStringNoHTML(string(rep))\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\tif !changed {\n\t\t\tout = make([]byte, 0, len(body)-len(c.original)+len(rep))\n\t\t}\n\t\tout = append(out, body[last:c.start]...)\n\t\tout = append(out, quoted...)\n\t\tlast = c.end\n\t\tchanged = true\n\t}\n\tif !changed {","sourceCodeStart":633,"sourceCodeEnd":669,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/proxy/providers/openai/content_compress.go#L633-L669","documentation":"During OpenAI response-body compression, spliceStringReplacements rewrites byte ranges of the JSON body with replacement bytes. Before splicing, it validates each candidate range is ordered, within the body bounds, and non-empty; if any range violates these invariants it aborts rather than emitting a corrupted body. This is an internal consistency check, so hitting it means the compression pass computed bad offsets.","triggerScenarios":"The compression pipeline produces candidate ranges (c.start/c.end) that overlap a previous replacement (c.start < last), extend past len(body) (c.end > len(body)), or are empty/inverted (c.start >= c.end), when applying string-replacement edits to the response body.","commonSituations":"A new compression transform computes offsets against an escaped or re-encoded form of the body instead of the raw bytes; concurrent edits to the same region; a regex/JSON-path rewrite spanning replacement boundaries introduced in a newer proxy version.","solutions":["Report the full request/response capture to the proxy maintainers with proxy version — this is an internal invariant failure, not user input error.","Disable the response-body compression option (compress off) to bypass the faulty splice pass.","Check whether the failing content type includes escaped Unicode or very large bodies and try a smaller/simpler payload to narrow the trigger.","Pin to the previous proxy version where compression did not corrupt ranges."],"exampleFix":"// before: transform computes offsets on unescaped text\nstart := strings.Index(unescaped, needle)\n// after: compute offsets on the original raw body bytes\nstart := bytes.Index(body, []byte(needle))","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"resp, err := doCompressedRequest(req)\nif err != nil {\n    if strings.Contains(err.Error(), \"invalid splice range\") {\n        // internal invariant failure: retry once with compression disabled\n        return doPlainRequest(req)\n    }\n    return err\n}","preventionTips":["Report reproducible captures to maintainers; this is not user-fixable.","Keep the proxy updated so compression offset bugs are patched.","Avoid unusual response bodies (heavy escaped Unicode) when testing new compression transforms."],"tags":["go","proxy","compression","internal-error"],"backgroundTag":"internal-invariant-violation","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}