{"record":{"id":"e934761994089af5","repo":"chenhg5/cc-connect","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":"agent/codex/context_usage.go","lineNumber":147,"sourceCode":"\tif err != nil {\n\t\treturn nil, path, err\n\t}\n\tif usage == nil {\n\t\treturn nil, path, fmt.Errorf(\"context usage not found in rollout\")\n\t}\n\treturn usage, path, nil\n}\n\nfunc resolveCodexHome(extraEnv []string) (string, error) {\n\tif value := getenvFromList(extraEnv, \"CODEX_HOME\"); value != \"\" {\n\t\treturn strings.TrimSpace(value), nil\n\t}\n\tif value := strings.TrimSpace(os.Getenv(\"CODEX_HOME\")); value != \"\" {\n\t\treturn value, nil\n\t}\n\thomeDir, err := os.UserHomeDir()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"resolve home directory: %w\", err)\n\t}\n\treturn filepath.Join(homeDir, \".codex\"), nil\n}\n\nfunc getenvFromList(env []string, key string) string {\n\tprefix := key + \"=\"\n\tfor i := len(env) - 1; i >= 0; i-- {\n\t\tentry := env[i]\n\t\tif strings.HasPrefix(entry, prefix) {\n\t\t\treturn strings.TrimSpace(strings.TrimPrefix(entry, prefix))\n\t\t}\n\t}\n\treturn \"\"\n}\n\nfunc findSessionFileInCodexHome(codexHome, sessionID string) string {\n\tif strings.TrimSpace(codexHome) == \"\" || strings.TrimSpace(sessionID) == \"\" {\n\t\treturn \"\"","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/agent/codex/context_usage.go#L129-L165","documentation":"resolveCodexHome determines the codex home directory: CODEX_HOME env (from the provided env list first, then os.Getenv), falling back to os.UserHomeDir()+\"/.codex\". When os.UserHomeDir fails (it cannot determine the current user's home), the error is wrapped with this message. Downstream callers (cached models, model catalog, context usage) all fail with it.","triggerScenarios":"resolveCodexHome called in an environment where os.UserHomeDir() returns an error — no HOME env var on Unix, or user profile resolution failing on Windows — and CODEX_HOME is unset.","commonSituations":"Running cc-connect as a systemd service with a minimal env (HOME unset); containers running as a non-login user; Windows services without a loaded user profile; CI runners with scrubbed environments.","solutions":["Set CODEX_HOME explicitly (e.g. CODEX_HOME=/home/user/.codex) in the service environment.","Set HOME (Unix) or ensure the user profile is loaded (Windows) for the process.","Run the daemon as a regular user whose home directory is resolvable."],"exampleFix":"// before\nExecStart=/usr/local/bin/cc-connect  # no HOME => UserHomeDir fails\n// after (systemd)\n[Service]\nEnvironment=CODEX_HOME=/home/alice/.codex\nExecStart=/usr/local/bin/cc-connect","handlingStrategy":"validation","validationCode":"if os.Getenv(\"CODEX_HOME\") == \"\" {\n    if _, err := os.UserHomeDir(); err != nil {\n        return fmt.Errorf(\"cannot resolve codex home: set CODEX_HOME explicitly: %w\", err)\n    }\n}","typeGuard":null,"tryCatchPattern":"codexHome, err := resolveCodexHome(extraEnv)\nif err != nil {\n    return fmt.Errorf(\"codex home unavailable; set CODEX_HOME: %w\", err)\n}","preventionTips":["Always set CODEX_HOME explicitly in daemon/systemd/container environments.","Never launch services with a scrubbed environment missing HOME.","Add a config check that warns when CODEX_HOME is unset at startup.","Run containers with a defined user and home directory."],"tags":["environment","home-directory","codex-home"],"backgroundTag":"missing-env-var","analyzedSha":"4000b2338aa6e850c99df54f8b0ed6ed7460b401","analyzedAt":"2026-09-06T11:45:09.575Z","contentChangedAt":"2026-09-06T11:45:09.575Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}