You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This commit was created on GitHub.com and signed with GitHub’s verified signature.
Fixed
Concurrent first-open of a brand-new database could crash with SQLITE_BUSY.PRAGMA journal_mode=WAL on a new file can return database is locked in a few milliseconds and does not honor busy_timeout, so the migrate lock (taken afterwards) never got a chance to run. Hooks, the MCP server and the CLI can all hit this when they start together. Opening a connection now sets busy_timeout first and retries the WAL switch in userspace. The busy predicate matches bun:sqlite's real error shape (code: "SQLITE_BUSY", errno: 5, message: "database is locked"). A regression test opens a fresh database with 8 processes, 8 times in one case.
A loser of that first-open race could still re-run migrations.migrateDb read hasMemories before taking <db>.migrate.lock and then branched on the stale flag after waiting, so the second process re-applied first-open ALTER TABLEs (duplicate column name: status / first_recalled_at) and crashed in reconcile with database is locked. The pre-lock read is now only a fast path when the DB is already current; under the lock the memories table and _schema_version are re-read and a loser that finds migrations applied does nothing. schema.sql itself is CREATE IF NOT EXISTS and is applied only under the lock (getDb() no longer execs it on a second unlocked connection). First-open still reconciles early migrations whose columns schema.sql already has; that reconcile now retries SQLITE_BUSY.
An exhausted SQLITE_BUSY on a migration apply could fall through into reconcile. Reconcile is only for duplicate-column / already-exists failures; a busy error that already used its retries is now rethrown.
getDb() must not close the cold-start handle when initDb swaps in the migrated connection. Callers such as recall() (and the OpenCode adapter) take that handle, await an embedding, then keep querying it. Closing the temporary handle at the swap throws Cannot use a closed database. The startup handle is left open — a harmless process-lifetime leak. A regression test covers recall() with a 300ms stub embedding on an already-migrated DB, without waitForInit().
Busy retries spun the CPU.applyConnectionPragmas, migration apply/reconcile, and withRetry now share retryOnBusy and wait with Bun.sleepSync.
The graph server and consolidate-db still used the raw WAL pragma string. They now call applyConnectionPragmas.
initHive used a deferred transaction. Two inits could both read a missing schema_version before either wrote; the write now uses BEGIN IMMEDIATE.
Hermes Python first-open had the same WAL race.connect / hive open now set busy_timeout first and retry journal_mode=WAL on database is locked. init_hive re-checks schema_version after open so a concurrent init cannot write a second owner. PRAGMA busy_timeout matches sqlite3.connect(timeout=10) (10 seconds) instead of shortening it to 5s. apply_connection_pragmas always attempts the WAL switch at least once, and only retries on database is locked / SQLITE_BUSY, not any message containing "busy".