{"record":{"id":"73960a46a04b3338","repo":"JuliusBrussee/caveman","slug":"write-stdout-w","errorCode":null,"errorMessage":"write stdout: %w","messagePattern":"write stdout: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/cmd/caveman-engine/main.go","lineNumber":133,"sourceCode":"}\n\n// emitCompressResult preserves the engine's byte-safety contract at the CLI\n// boundary. CCR failures are expected operational conditions: the engine has\n// already returned an accounted pass-through result, so stdout must still get\n// 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.","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/engine/cmd/caveman-engine/main.go#L115-L151","documentation":"emitCompressResult writes the (possibly pass-through) payload bytes to stdout; if that write fails it wraps the OS error as \"write stdout: %w\". Stdout must carry the clean payload bytes for the calling pipeline, so a write failure is fatal and the report is never emitted.","triggerScenarios":"stdout.Write(res.Output) returns an error inside emitCompressResult, called from runCompress when the caveman-engine compress subcommand's stdout is closed, a broken pipe, or a full/unwritable descriptor.","commonSituations":"Piping into a consumer that exited early (e.g. `caveman-engine compress | head -1`), redirecting stdout to a file on a full disk, or running with stdout closed by the harness.","solutions":["Check/repair the downstream consumer of stdout (it likely exited and closed the pipe).","Verify disk space and permissions when stdout is redirected to a file.","Retry the command without a short-lived consumer, or capture output to a file instead of a pipe."],"exampleFix":"// before\ncaveman-engine compress < in.json | head -1\n// after\ncaveman-engine compress < in.json > out.bin && head -c 200 out.bin","handlingStrategy":"try-catch","validationCode":"// check the consumer is alive before writing, e.g. verify the pipe target process is running","typeGuard":null,"tryCatchPattern":"if err := runCompress(...); err != nil {\n    if errors.Is(err, syscall.EPIPE) { log.Println(\"stdout consumer exited early (broken pipe)\") }\n}","preventionTips":["Avoid piping engine output into commands that exit early (head, less)","Redirect stdout to a file when chaining fragile consumers","Monitor downstream process liveness in pipelines"],"tags":["io","stdout","broken-pipe"],"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"}