{"record":{"id":"d5570c2d1bde0b9a","repo":"JuliusBrussee/caveman","slug":"secure-memories-db-w","errorCode":null,"errorMessage":"secure memories db: %w","messagePattern":"secure memories db: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mem/store.go","lineNumber":109,"sourceCode":"}\n\n// Open opens (creating if needed) a cavemem store.\nfunc Open(opts Options) (*Store, error) {\n\tmemPath, ccrPath := \":memory:\", \":memory:\"\n\tif !opts.InMemory {\n\t\tdir := opts.Dir\n\t\tif dir == \"\" {\n\t\t\tdir = defaultDir()\n\t\t}\n\t\tif err := os.MkdirAll(dir, 0o700); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"create %s: %w\", dir, err)\n\t\t}\n\t\tmemPath = filepath.Join(dir, \"mem.db\")\n\t\tccrPath = filepath.Join(dir, \"ccr.db\")\n\t}\n\tcanonicalMemPath, err := ccr.PrepareSQLitePathCanonical(memPath)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"secure memories db: %w\", err)\n\t}\n\t// Same embedded-SQLite-behind-a-multi-process-CLI discipline as the recovery\n\t// store: a single writer connection plus busy_timeout(5000)+WAL. Several\n\t// cavemem processes (and the MCP server) share one mem.db, and the JS client\n\t// fires Promise.all(facts.map(remember)); without this, concurrent writes\n\t// returned SQLITE_BUSY and were silently dropped. See ccr.SQLiteDSN.\n\tdb, err := sql.Open(\"sqlite\", ccr.SQLiteDSN(canonicalMemPath))\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"open memories db: %w\", err)\n\t}\n\tdb.SetMaxOpenConns(1)\n\tdb.SetMaxIdleConns(1)\n\t// The cold-start migration is multi-statement DDL; under a fan-out of fresh\n\t// processes it can outlast a single busy_timeout, so retry it on SQLITE_BUSY\n\t// (runtime writes rely on busy_timeout alone). Both steps are idempotent.\n\tif err := ccr.RetryOnBusy(func() error { _, e := db.Exec(memSchema); return e }); err != nil {\n\t\t_ = db.Close()\n\t\treturn nil, fmt.Errorf(\"migrate memories db: %w\", err)","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/mem/store.go#L91-L127","documentation":"Before opening the SQLite memories database, Open canonicalizes/prepares the path via ccr.PrepareSQLitePathCanonical (which applies security/POSIX path handling). Failure here is reported as 'secure memories db'. It means the mem.db path could not be safely prepared, typically due to path resolution problems.","triggerScenarios":"PrepareSQLitePathCanonical fails on the computed memPath — e.g. invalid or non-canonicalizable path, symlink or path resolution errors, or platform-specific path issues.","commonSituations":"opts.Dir containing symlinks or irregular path components on strict platforms; running on an unsupported/locked-down filesystem; relative paths resolving oddly under different working directories.","solutions":["Check the wrapped error to see which path failed canonicalization","Use a simple absolute path without symlinks for the store directory","Verify the path exists with expected permissions and is a regular location","Update/verify the ccr path-preparation helper supports your platform"],"exampleFix":"// before\nmem.Open(mem.Options{Dir: \"~/data/../link/cavemem\"})\n// after\nmem.Open(mem.Options{Dir: \"/var/lib/cavemem\"})","handlingStrategy":"validation","validationCode":"path := filepath.Join(dir, \"mem.db\")\nresolved, err := filepath.EvalSymlinks(filepath.Dir(path))\nif err != nil || resolved == \"\" { /* fix dir before Open */ }","typeGuard":null,"tryCatchPattern":"store, err := mem.Open(opts)\nif err != nil && strings.Contains(err.Error(), \"secure memories db\") {\n  // path preparation failed: simplify/fix the dir path\n  return err\n}","preventionTips":["Avoid symlinks and odd path components in the store directory","Use plain absolute paths for mem.db locations","Keep path-preparation helpers (ccr) up to date for your platform"],"tags":["sqlite","path","security","store"],"backgroundTag":"file-open-failed","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"}