JuliusBrussee/caveman · error

write stdout

Error message

write stdout: %w

What it means

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.

Solutions

  1. Check/repair the downstream consumer of stdout (it likely exited and closed the pipe).
  2. Verify disk space and permissions when stdout is redirected to a file.
  3. Retry the command without a short-lived consumer, or capture output to a file instead of a pipe.

Example fix

// before
caveman-engine compress < in.json | head -1
// after
caveman-engine compress < in.json > out.bin && head -c 200 out.bin
Defensive patterns

Strategy: try-catch

Validate before calling

// check the consumer is alive before writing, e.g. verify the pipe target process is running

Try / catch

if err := runCompress(...); err != nil {
    if errors.Is(err, syscall.EPIPE) { log.Println("stdout consumer exited early (broken pipe)") }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/73960a46a04b3338. Report an issue: GitHub.

Appendix: source

Thrown at engine/cmd/caveman-engine/main.go:133

}

// emitCompressResult preserves the engine's byte-safety contract at the CLI
// boundary. CCR failures are expected operational conditions: the engine has
// already returned an accounted pass-through result, so stdout must still get
// the original bytes. Any contradictory result fails rather than guessing.
func emitCompressResult(input []byte, res engine.Result, compressErr error, stdout, stderr io.Writer) error {
	code := ""
	if compressErr != nil {
		if !bytes.Equal(res.Output, input) || !res.PassedThrough() || res.TokensAfter != res.TokensBefore {
			return fmt.Errorf("unsafe recovery failure result: %w", compressErr)
		}
		code = "cave_ccr_unavailable"
		if errors.Is(compressErr, ccr.ErrBudgetExceeded) {
			code = "cave_ccr_budget_exceeded"
		}
	}
	if _, err := stdout.Write(res.Output); err != nil {
		return fmt.Errorf("write stdout: %w", err)
	}
	// Report stays on stderr so stdout remains clean payload bytes.
	report, err := json.Marshal(compressReport{Result: res, Error: code})
	if err != nil {
		return fmt.Errorf("encode report: %w", err)
	}
	if _, err := fmt.Fprintln(stderr, string(report)); err != nil {
		return fmt.Errorf("write report: %w", err)
	}
	return nil
}

func runDetect() {
	input, err := readBoundedInput(os.Stdin, maxStdinBytes)
	if err != nil {
		fatal("read stdin: %v", err)
	}
	// Detection needs no store.

View on GitHub (pinned to 3ee70a1026)