{"record":{"id":"0fdf7f4d874eb69a","repo":"xai-org/grok-build","slug":"memory-directory-does-not-exist-e","errorCode":null,"errorMessage":"memory directory {:?} does not exist: {e}","messagePattern":"memory directory (.+?) does not exist: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-grok-memory/src/storage.rs","lineNumber":270,"sourceCode":"    }\n\n    /// Read a memory file, optionally returning only a range of lines.\n    ///\n    /// - `from`: 0-based start line (default 0)\n    /// - `lines`: max number of lines to return (default: all)\n    ///\n    /// The path must resolve (via `canonicalize`) to a location inside the memory directory tree.\n    /// Both the path and the memory root must be canonicalizable; if either fails, the read is rejected.\n    pub fn read_file(\n        &self,\n        path: &Path,\n        from: Option<usize>,\n        lines: Option<usize>,\n    ) -> std::io::Result<String> {\n        // Security: canonicalize both sides; fail hard if either doesn't exist\n        let canonical = dunce::canonicalize(path)?;\n        let canonical_global = dunce::canonicalize(&self.global_dir).map_err(|e| {\n            std::io::Error::new(\n                std::io::ErrorKind::NotFound,\n                format!(\"memory directory {:?} does not exist: {e}\", self.global_dir),\n            )\n        })?;\n\n        // Fail-closed caveat for paths longer than MAX_PATH: see workspace clippy.toml\n        if !canonical.starts_with(&canonical_global) {\n            return Err(std::io::Error::new(\n                std::io::ErrorKind::PermissionDenied,\n                format!(\n                    \"path {:?} is outside the memory directory {:?}\",\n                    path, self.global_dir\n                ),\n            ));\n        }\n\n        // Read the canonicalized path, not the original, to prevent TOCTOU races.\n        let content = std::fs::read_to_string(&canonical)?;","sourceCodeStart":252,"sourceCodeEnd":288,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-grok-memory/src/storage.rs#L252-L288","documentation":"In MemoryStorage::read_file, the global memory directory is canonicalized defensively before any path containment check. If dunce::canonicalize fails on global_dir, this NotFound io::Error is returned, naming the missing directory and the underlying error. It prevents reading from a storage root that does not exist.","triggerScenarios":"Calling read_file when the global memory directory has been deleted, was never created, or GROK home points at a non-existent location.","commonSituations":"Fresh machines/CI where ~/.grok/memory was never initialized; users deleting the memory dir manually; misconfigured HOME or GROK_* env pointing elsewhere.","solutions":["Create the memory directory before reading (run the storage init/ensure routine)","Point the storage configuration at an existing directory","Verify HOME / grok-home env vars resolve to a real location","Check the wrapped io error for permission vs missing-directory cause"],"exampleFix":"// before\nlet content = storage.read_file(&path, None, None)?; // NotFound if global_dir missing\n// after\nstd::fs::create_dir_all(&memory_dir)?;\nlet content = storage.read_file(&path, None, None)?;","handlingStrategy":"validation","validationCode":"let global_dir = storage.global_dir();\nif !global_dir.is_dir() {\n    std::fs::create_dir_all(&global_dir)?;\n}","typeGuard":null,"tryCatchPattern":"match storage.read_file(&path, None, None) {\n    Err(e) if e.kind() == std::io::ErrorKind::NotFound => {\n        std::fs::create_dir_all(storage.global_dir())?;\n        storage.read_file(&path, None, None)?\n    }\n    other => other?,\n}","preventionTips":["Initialize/ensure the memory directory at storage construction","Verify HOME / grok-home env vars resolve in service contexts","Do not delete the memory root while storage instances are live","Canonicalize the root once and reuse it"],"tags":["io","filesystem","memory","not-found"],"backgroundTag":"directory-not-found","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}