{"record":{"id":"fac8d52fc3e2a56e","repo":"JuliusBrussee/caveman","slug":"write-report-w","errorCode":null,"errorMessage":"write report: %w","messagePattern":"write report: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/cmd/caveman-engine/main.go","lineNumber":141,"sourceCode":"\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) {\n\tif len(args) < 1 {\n\t\tfatal(\"usage: caveman-engine retrieve <handle> [query]\")\n\t}","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/engine/cmd/caveman-engine/main.go#L123-L159","documentation":"The final step of emitCompressResult prints the JSON report (with trailing newline) to stderr via fmt.Fprintln. If that write fails, it is wrapped as \"write report: %w\". Payload bytes may already have been written to stdout at this point, so this indicates stderr itself is unusable.","triggerScenarios":"fmt.Fprintln(stderr, string(report)) fails inside emitCompressResult — stderr descriptor is closed, the pipe reader exited, or the fd is otherwise unwritable during runCompress.","commonSituations":"Test harnesses or orchestrators that close stderr; `2>&-` shell redirection; container runtimes with detached stderr; piping stderr into a process that exited immediately.","solutions":["Ensure stderr is open and connected when launching caveman-engine.","Fix the supervisor/harness that closes or discloses stderr prematurely.","Check the consumer reading stderr (e.g. a log pipe) is still alive."],"exampleFix":"// before\ncmd := exec.Command(\"caveman-engine\", \"compress\")\ncmd.Stderr = nil // closed\n// after\ncmd.Stderr = os.Stderr","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if _, err := fmt.Fprintln(stderr, string(report)); err != nil {\n    log.Printf(\"report write failed (payload already emitted): %v\", err)\n}","preventionTips":["Never launch the engine with stderr closed","Keep stderr connected to a live reader in orchestrators","In shells, avoid `2>&-` when calling caveman-engine"],"tags":["io","stderr","report"],"backgroundTag":"broken-pipe","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"}