{"record":{"id":"af6350a38d26eb9f","repo":"JuliusBrussee/caveman","slug":"unsafe-recovery-failure-result-w","errorCode":null,"errorMessage":"unsafe recovery failure result: %w","messagePattern":"unsafe recovery failure result: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"engine/cmd/caveman-engine/main.go","lineNumber":125,"sourceCode":"\tif err := emitCompressResult(input, res, err, os.Stdout, os.Stderr); err != nil {\n\t\tfatal(\"compress: %v\", err)\n\t}\n}\n\ntype compressReport struct {\n\tengine.Result\n\tError string `json:\"error,omitempty\"`\n}\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","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/27d5a3981a347890211bb1bf2439e5c821a63bc9/engine/cmd/caveman-engine/main.go#L107-L143","documentation":"Emitted by the caveman-engine CLI when compression (CCR) failed AND the engine's result contradicts the byte-safety contract: on a CCR failure the engine must return an accounted pass-through (output == input, PassedThrough() true, token counts unchanged). A contradictory result means the fallback path itself is broken, so the CLI refuses to emit possibly-wrong bytes and fails instead of guessing.","triggerScenarios":"Running `caveman-engine compress` (or equivalent subcommand) where the engine returns an error from Compress but the accompanying Result is not byte-identical to the input or reports changed token counts — i.e. an engine-internal invariant break or a version mismatch between the CLI and the engine package.","commonSituations":"A bug in the engine's error-recovery path; an engine package built/patched inconsistently with the CLI; corrupted intermediate state after a crash mid-compress.","solutions":["Note the wrapped compressErr — it identifies which CCR failure started the fallback.","Reinstall/rebuild the engine binary so the CLI and engine packages come from one consistent build.","If reproducible with a small input, file a bug: this error indicates an internal invariant violation, not user misconfiguration.","As a workaround, run with the CCR store disabled or a fresh store path so the failing CCR path is bypassed."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// In shell: this error means exit non-zero; do NOT reuse partial stdout\n// out, err := cmd.Output(); if err != nil { discard out entirely }","preventionTips":["Build the CLI and engine from the same commit in one go build.","Treat this specific message as a report-a-bug signal, not a retryable failure.","Pin released engine versions in CI rather than mixing local patches with upstream builds."],"tags":["cli","invariant","safety","ccr"],"backgroundTag":null,"analyzedSha":"27d5a3981a347890211bb1bf2439e5c821a63bc9","analyzedAt":"2026-08-15T09:26:11.751Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}