{"record":{"id":"685b57a9df198e21","repo":"JuliusBrussee/caveman","slug":"encode-report-w","errorCode":null,"errorMessage":"encode report: %w","messagePattern":"encode report: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/cmd/caveman-engine/main.go","lineNumber":138,"sourceCode":"// the original bytes. Any contradictory result fails rather than guessing.\nfunc emitCompressResult(input []byte, res engine.Result, compressErr error, stdout, stderr io.Writer) error {\n\tcode := \"\"\n\tif compressErr != nil {\n\t\tif !bytes.Equal(res.Output, input) || !res.PassedThrough() || res.TokensAfter != res.TokensBefore {\n\t\t\treturn fmt.Errorf(\"unsafe recovery failure result: %w\", compressErr)\n\t\t}\n\t\tcode = \"cave_ccr_unavailable\"\n\t\tif errors.Is(compressErr, ccr.ErrBudgetExceeded) {\n\t\t\tcode = \"cave_ccr_budget_exceeded\"\n\t\t}\n\t}\n\tif _, err := stdout.Write(res.Output); err != nil {\n\t\treturn fmt.Errorf(\"write stdout: %w\", err)\n\t}\n\t// Report stays on stderr so stdout remains clean payload bytes.\n\treport, err := json.Marshal(compressReport{Result: res, Error: code})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"encode report: %w\", err)\n\t}\n\tif _, err := fmt.Fprintln(stderr, string(report)); err != nil {\n\t\treturn fmt.Errorf(\"write report: %w\", err)\n\t}\n\treturn nil\n}\n\nfunc runDetect() {\n\tinput, err := readBoundedInput(os.Stdin, maxStdinBytes)\n\tif err != nil {\n\t\tfatal(\"read stdin: %v\", err)\n\t}\n\t// Detection needs no store.\n\teng := engine.New(nil, nil)\n\tfmt.Println(eng.Detect(input))\n}\n\nfunc runRetrieve(args []string) {","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/27d5a3981a347890211bb1bf2439e5c821a63bc9/engine/cmd/caveman-engine/main.go#L120-L156","documentation":"json.Marshal of the compressReport (engine Result plus error code) failed. The report struct contains engine counters and an error-code string, so marshal failures are essentially impossible for well-formed engine Results — this error signals memory exhaustion or a corrupted Result (e.g. NaN/Inf in a numeric field).","triggerScenarios":"emitCompressResult building the JSON report when a numeric field in res holds a value JSON cannot encode (NaN, +Inf) — normally prevented by engine validation — or an out-of-memory condition during marshal.","commonSituations":"A patched/forked engine Result introducing an unencodable field; running under a hard memory limit (cgroup) so marshal allocation fails.","solutions":["If you forked the engine, ensure numeric Result fields never hold NaN/Inf (validate before returning).","Check the process memory limit and raise it if the OOM killer or allocator is involved.","Reproduce with the stock binary to rule out local modifications.","If it persists on stock builds, report it — stdout already received the payload, only the stderr report was lost."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never put NaN/Inf into engine Result numeric fields if you fork the engine.","Run under memory limits with headroom above the payload size.","Note stdout already carried the payload; only the stderr report failed."],"tags":["cli","json","report","marshal"],"backgroundTag":null,"analyzedSha":"27d5a3981a347890211bb1bf2439e5c821a63bc9","analyzedAt":"2026-08-15T09:26:11.751Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}