JuliusBrussee/caveman · error

resolve home directory

Error message

resolve home directory: %w

What it means

defaultCCRPath builds the default recovery store path under CAVEMAN_HOME, falling back to os.UserHomeDir(). If the home directory cannot be resolved (no HOME, no user database entry), it wraps the failure as 'resolve home directory'.

Solutions

  1. Set CAVEMAN_HOME explicitly to a writable directory
  2. Set HOME in the environment (e.g. `export HOME=/home/user` or in the unit file `Environment=HOME=/var/lib/app`)
  3. Pass the store path via CAVEMAN_CCR_DB so defaultCCRPath is never consulted
  4. Run as a user that has a valid passwd home entry

Example fix

// before
exec caveman-shrink shrink  # HOME unset in container
// after
ENV CAVEMAN_HOME=/data/caveman
RUN mkdir -p /data/caveman
Defensive patterns

Strategy: fallback

Validate before calling

if os.Getenv("CAVEMAN_HOME") == "" && os.Getenv("HOME") == "" {
  // set CAVEMAN_HOME to a writable dir before invoking
}

Try / catch

_, err := defaultCCRPath()
if err != nil && strings.HasPrefix(err.Error(), "resolve home directory") {
  os.Setenv("CAVEMAN_HOME", "/tmp/caveman") // or surface a config hint
}

Prevention

When it happens

Trigger: CAVEMAN_HOME unset AND os.UserHomeDir() returns an error — typically running with an empty HOME environment variable, as a system service user without a home, or in a stripped container/sandbox environment.

Common situations: Docker containers running as non-root with HOME unset; systemd units without HOME= set; CI runners executing as UID with no passwd entry; sudo/su environments that drop HOME.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at shrink/shrink.go:87

	if err != nil {
		return nil, nil, fmt.Errorf("open recovery store %q: %w", path, err)
	}
	return s, s.Close, nil
}

// defaultCCRPath resolves the durable shared recovery store path, mirroring the
// engine CLI, MCP server, and proxy: CAVEMAN_CCR_DB, else CAVEMAN_HOME/ccr.db,
// else ~/.caveman/ccr.db. It creates the parent directory so the first shrink on a
// machine succeeds.
func defaultCCRPath() (string, error) {
	if p := os.Getenv("CAVEMAN_CCR_DB"); p != "" {
		return p, nil
	}
	home := os.Getenv("CAVEMAN_HOME")
	if home == "" {
		h, err := os.UserHomeDir()
		if err != nil {
			return "", fmt.Errorf("resolve home directory: %w", err)
		}
		home = filepath.Join(h, ".caveman")
	}
	if err := os.MkdirAll(home, 0o700); err != nil {
		return "", fmt.Errorf("create %s: %w", home, err)
	}
	return filepath.Join(home, "ccr.db"), nil
}

// Result is the outcome of Shrink.
type Result struct {
	Output         []byte  `json:"-"`
	TokensBefore   int     `json:"tokens_before"`
	TokensAfter    int     `json:"tokens_after"`
	Ratio          float64 `json:"ratio"`
	Basis          string  `json:"basis"`
	ContentType    string  `json:"content_type"`
	RecoveryHandle string  `json:"recovery_handle,omitempty"`

View on GitHub (pinned to 3ee70a1026)