{"record":{"id":"07ff769e0fb86160","repo":"JuliusBrussee/caveman","slug":"resolve-home-directory-w","errorCode":null,"errorMessage":"resolve home directory: %w","messagePattern":"resolve home directory: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shrink/shrink.go","lineNumber":87,"sourceCode":"\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"open recovery store %q: %w\", path, err)\n\t}\n\treturn s, s.Close, nil\n}\n\n// defaultCCRPath resolves the durable shared recovery store path, mirroring the\n// engine CLI, MCP server, and proxy: CAVEMAN_CCR_DB, else CAVEMAN_HOME/ccr.db,\n// else ~/.caveman/ccr.db. It creates the parent directory so the first shrink on a\n// machine succeeds.\nfunc defaultCCRPath() (string, error) {\n\tif p := os.Getenv(\"CAVEMAN_CCR_DB\"); p != \"\" {\n\t\treturn p, nil\n\t}\n\thome := os.Getenv(\"CAVEMAN_HOME\")\n\tif home == \"\" {\n\t\th, err := os.UserHomeDir()\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"resolve home directory: %w\", err)\n\t\t}\n\t\thome = filepath.Join(h, \".caveman\")\n\t}\n\tif err := os.MkdirAll(home, 0o700); err != nil {\n\t\treturn \"\", fmt.Errorf(\"create %s: %w\", home, err)\n\t}\n\treturn filepath.Join(home, \"ccr.db\"), nil\n}\n\n// Result is the outcome of Shrink.\ntype Result struct {\n\tOutput         []byte  `json:\"-\"`\n\tTokensBefore   int     `json:\"tokens_before\"`\n\tTokensAfter    int     `json:\"tokens_after\"`\n\tRatio          float64 `json:\"ratio\"`\n\tBasis          string  `json:\"basis\"`\n\tContentType    string  `json:\"content_type\"`\n\tRecoveryHandle string  `json:\"recovery_handle,omitempty\"`","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shrink/shrink.go#L69-L105","documentation":"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'.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Set CAVEMAN_HOME explicitly to a writable directory","Set HOME in the environment (e.g. `export HOME=/home/user` or in the unit file `Environment=HOME=/var/lib/app`)","Pass the store path via CAVEMAN_CCR_DB so defaultCCRPath is never consulted","Run as a user that has a valid passwd home entry"],"exampleFix":"// before\nexec caveman-shrink shrink  # HOME unset in container\n// after\nENV CAVEMAN_HOME=/data/caveman\nRUN mkdir -p /data/caveman","handlingStrategy":"fallback","validationCode":"if os.Getenv(\"CAVEMAN_HOME\") == \"\" && os.Getenv(\"HOME\") == \"\" {\n  // set CAVEMAN_HOME to a writable dir before invoking\n}","typeGuard":null,"tryCatchPattern":"_, err := defaultCCRPath()\nif err != nil && strings.HasPrefix(err.Error(), \"resolve home directory\") {\n  os.Setenv(\"CAVEMAN_HOME\", \"/tmp/caveman\") // or surface a config hint\n}","preventionTips":["Always set CAVEMAN_HOME in containers, CI, and systemd units","Avoid running under users without a passwd home entry","Prefer CAVEMAN_CCR_DB for explicit path control"],"tags":["environment","home-directory","config"],"backgroundTag":"missing-env-var","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"}