{"record":{"id":"cce2f69d59a5b548","repo":"chenhg5/cc-connect","slug":"invalid-content-length-w","errorCode":null,"errorMessage":"invalid Content-Length: %w","messagePattern":"invalid Content-Length: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"agent/copilot/jsonrpc.go","lineNumber":104,"sourceCode":"\n// readMessage reads one Content-Length framed message and returns the raw JSON body.\nfunc (lr *lspReader) readMessage() ([]byte, error) {\n\tcontentLength := -1\n\tfor {\n\t\tline, err := lr.reader.ReadString('\\n')\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"read header: %w\", err)\n\t\t}\n\t\tline = strings.TrimRight(line, \"\\r\\n\")\n\t\tif line == \"\" {\n\t\t\t// End of headers\n\t\t\tbreak\n\t\t}\n\t\tif strings.HasPrefix(line, \"Content-Length: \") {\n\t\t\tval := strings.TrimPrefix(line, \"Content-Length: \")\n\t\t\tn, err := strconv.Atoi(strings.TrimSpace(val))\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"invalid Content-Length: %w\", err)\n\t\t\t}\n\t\t\tcontentLength = n\n\t\t}\n\t\t// Ignore other headers (Content-Type, etc.)\n\t}\n\n\tif contentLength < 0 {\n\t\treturn nil, fmt.Errorf(\"missing Content-Length header\")\n\t}\n\tif contentLength > 10*1024*1024 {\n\t\treturn nil, fmt.Errorf(\"Content-Length too large: %d\", contentLength)\n\t}\n\n\tbody := make([]byte, contentLength)\n\tif _, err := io.ReadFull(lr.reader, body); err != nil {\n\t\treturn nil, fmt.Errorf(\"read body: %w\", err)\n\t}\n\treturn body, nil","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/agent/copilot/jsonrpc.go#L86-L122","documentation":"The header line 'Content-Length: <val>' was found but its value could not be parsed as an integer (strconv.Atoi failed). The copilot peer sent a malformed Content-Length header, violating the LSP framing protocol, so the message cannot be framed or read safely.","triggerScenarios":"readMessage parsing a header like 'Content-Length: abc' or with garbage after the number from the copilot process stdout — emitted when a non-conforming CLI writes junk to stdout.","commonSituations":"A copilot CLI version (or wrapper script) printing log lines interleaved with LSP frames on stdout; a mock/debug shim emitting malformed headers; locale/encoding issues injecting characters into the header.","solutions":["Check whether the copilot binary or a wrapper script pollutes stdout with non-LSP output and route logs to stderr","Upgrade/downgrade the copilot CLI to a conforming version","Test with runMockCopilot to reproduce the exact header bytes being sent","If a wrapper is used, strip non-LSP lines before they reach the framing reader"],"exampleFix":"// before (mock emits bad header)\nfmt.Fprintf(w, \"Content-Length: %s\\r\\n\\r\\n\", len(body)) // %s on int -> bad\n// after\nfmt.Fprintf(w, \"Content-Length: %d\\r\\n\\r\\n\", len(body))","handlingStrategy":"validation","validationCode":"// validate a header line before parsing\nfunc parseContentLength(line string) (int, error) {\n    val := strings.TrimSpace(strings.TrimPrefix(line, \"Content-Length: \"))\n    if val == \"\" { return 0, errors.New(\"empty Content-Length\") }\n    n, err := strconv.Atoi(val)\n    if err != nil || n < 0 { return 0, fmt.Errorf(\"bad Content-Length %q\", val) }\n    return n, nil\n}","typeGuard":null,"tryCatchPattern":"if _, err := reader.readMessage(); err != nil && strings.Contains(err.Error(), \"invalid Content-Length\") {\n    slog.Error(\"peer sent malformed LSP header — check for stdout pollution\", \"err\", err)\n    // fail fast; do not attempt to resync framing\n}","preventionTips":["Keep stdout strictly LSP-framed: logs/warnings go to stderr","Test CLI upgrades against runMockCopilot-style golden frames","Reject wrapper scripts that echo extra text on stdout"],"tags":["jsonrpc","lsp","protocol","parse"],"backgroundTag":"invalid-json-response","analyzedSha":"4000b2338aa6e850c99df54f8b0ed6ed7460b401","analyzedAt":"2026-09-06T11:45:09.575Z","contentChangedAt":"2026-09-06T11:45:09.575Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}