{"record":{"id":"c4a350161f95304d","repo":"JuliusBrussee/caveman","slug":"migrate-memories-db-w","errorCode":null,"errorMessage":"migrate memories db: %w","messagePattern":"migrate memories db: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mem/store.go","lineNumber":127,"sourceCode":"\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)\n\t}\n\tif err := ccr.RetryOnBusy(func() error { return migrateMemorySchema(db) }); err != nil {\n\t\t_ = db.Close()\n\t\treturn nil, fmt.Errorf(\"migrate memories db: %w\", err)\n\t}\n\tstore, err := ccr.Open(ccrPath)\n\tif err != nil {\n\t\t_ = db.Close()\n\t\treturn nil, fmt.Errorf(\"open ccr: %w\", err)\n\t}\n\treturn &Store{db: db, eng: engine.New(store, nil), ccr: store}, nil\n}\n\n// Close releases the databases.\nfunc (s *Store) Close() error {\n\terr := s.db.Close()\n\tif cerr := s.ccr.Close(); cerr != nil && err == nil {\n\t\terr = cerr","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/mem/store.go#L109-L145","documentation":"Applying the memories DB schema (or the legacy schema migration) failed after SQLITE_BUSY retries. Both steps are idempotent and retried via RetryOnBusy, so this error means the migration genuinely failed (lock contention beyond retries, disk I/O error, or SQL error). The db handle is closed before returning.","triggerScenarios":"db.Exec(memSchema) or migrateMemorySchema fails and keeps failing through RetryOnBusy — heavy multi-process contention outlasting retries, corrupted db file, or disk errors.","commonSituations":"Many cavemem processes starting simultaneously against a fresh mem.db; a corrupted or partially-written mem.db; read-only volume where WAL/journal files cannot be created.","solutions":["Re-run when contention subsides; stagger process starts on cold start","Check the wrapped error: SQL/disk errors may require repairing or removing mem.db (after backup)","Ensure the volume is writable (WAL/journal needs write access alongside the db file)","Reduce concurrent cold-start fan-out or increase busy timeout/retry budget"],"exampleFix":"// before: 50 processes starting at once on fresh mem.db\n// after: stagger or retry\nfor i in 1 2 3; do cavemem remember ... && break; sleep 2; done","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"store, err := mem.Open(opts)\nif err != nil && strings.Contains(err.Error(), \"migrate memories db\") {\n  // cold-start contention or db problem; back off and retry or inspect db\n  time.Sleep(2 * time.Second)\n  return mem.Open(opts)\n}","preventionTips":["Stagger cold starts of many cavemem processes","Ensure the db volume is writable (WAL/journal side files)","Back up and repair/replace mem.db if corruption is indicated","Rely on the built-in RetryOnBusy; add outer backoff for extreme fan-out"],"tags":["sqlite","migration","database","schema"],"backgroundTag":"database-write-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"}