diff --git a/crates/goose/src/session/session_manager.rs b/crates/goose/src/session/session_manager.rs index 88fc375f3..0749d6cbb 100644 --- a/crates/goose/src/session/session_manager.rs +++ b/crates/goose/src/session/session_manager.rs @@ -844,8 +844,7 @@ impl SessionManager { } pub struct SessionStorage { - pool: Pool, - initialized: tokio::sync::OnceCell<()>, + pool: tokio::sync::OnceCell>, session_dir: PathBuf, action_required: Arc, } @@ -1081,7 +1080,7 @@ async fn insert_usage_ledger_row( } impl SessionStorage { - fn create_pool(path: &Path) -> Pool { + fn create_pool(path: &Path, pool_options: SqlitePoolOptions) -> Pool { if let Some(parent) = path.parent() { fs::create_dir_all(parent).expect("Failed to create session database directory"); #[cfg(unix)] @@ -1099,42 +1098,43 @@ impl SessionStorage { .busy_timeout(std::time::Duration::from_secs(30)) .journal_mode(sqlx::sqlite::SqliteJournalMode::Wal); - SqlitePoolOptions::new().connect_lazy_with(options) + pool_options.connect_lazy_with(options) } pub fn new(data_dir: PathBuf) -> Self { let session_dir = data_dir.join(SESSIONS_FOLDER); - let db_path = session_dir.join(DB_NAME); Self { - pool: Self::create_pool(&db_path), - initialized: tokio::sync::OnceCell::new(), + pool: tokio::sync::OnceCell::new(), session_dir, action_required: Arc::new(crate::action_required_manager::ActionRequiredManager::new()), } } pub(crate) async fn pool(&self) -> Result<&Pool> { - self.initialized + self.pool .get_or_try_init(|| async { + let db_path = self.session_dir.join(DB_NAME); + // Bootstrap connections must never enter the application pool with + // schema metadata cached before migrations finished. + let bootstrap = Self::create_pool(&db_path, SqlitePoolOptions::new().max_connections(1)); let schema_exists = sqlx::query_scalar::<_, bool>( r#"SELECT EXISTS (SELECT name FROM sqlite_master WHERE type='table' AND name='schema_version')"#, ) - .fetch_one(&self.pool) - .await - .unwrap_or(false); + .fetch_one(&bootstrap) + .await?; if schema_exists { - Self::run_migrations(&self.pool).await?; + Self::run_migrations(&bootstrap).await?; } else { - Self::create_schema(&self.pool).await?; - if let Err(e) = Self::import_legacy(&self.pool, &self.session_dir).await { + Self::create_schema(&bootstrap).await?; + if let Err(e) = Self::import_legacy(&bootstrap, &self.session_dir).await { warn!("Failed to import some legacy sessions: {}", e); } } - Ok::<(), anyhow::Error>(()) + bootstrap.close().await; + Ok::<_, anyhow::Error>(Self::create_pool(&db_path, SqlitePoolOptions::new())) }) - .await?; - Ok(&self.pool) + .await } async fn create_schema(pool: &Pool) -> Result<()> { @@ -1858,6 +1858,8 @@ impl SessionStorage { let mut tx = pool.begin_with("BEGIN IMMEDIATE").await?; let today = chrono::Utc::now().format("%Y%m%d").to_string(); + // An explicit projection avoids SQLx's stale column metadata when SQLite + // refreshes another pooled connection's pre-migration schema during execution. let session = sqlx::query_as( r#" INSERT INTO sessions (id, name, user_set_name, session_type, working_dir, extension_data, goose_mode) @@ -1874,7 +1876,16 @@ impl SessionStorage { '{}', ? ) - RETURNING * + RETURNING id, working_dir, name, description, user_set_name, session_type, + created_at, updated_at, extension_data, + total_tokens, input_tokens, output_tokens, + cache_read_tokens, cache_write_tokens, + accumulated_total_tokens, accumulated_input_tokens, accumulated_output_tokens, + accumulated_cache_read_tokens, accumulated_cache_write_tokens, + accumulated_cost, schedule_id, recipe_json, user_recipe_values_json, + provider_name, model_config_json, goose_mode, + archived_at, project_id, parent_session_id, + system_prompt_override, system_prompt_extras_json, container_id "#, ) .bind(id) @@ -3132,7 +3143,7 @@ mod tests { fs::metadata(&session_dir).unwrap().permissions().mode() & 0o777, 0o700 ); - storage.pool.close().await; + storage.pool().await.unwrap().close().await; drop(storage); fs::set_permissions(&database_path, fs::Permissions::from_mode(0o644)).unwrap(); @@ -4168,7 +4179,7 @@ mod tests { .await .unwrap(); - let pool = session_manager.storage().pool.clone(); + let pool = session_manager.storage().pool().await.unwrap().clone(); let results = run_lock_upgrade_race(pool.clone(), session.id.clone(), "BEGIN", true).await; assert!( @@ -5280,7 +5291,7 @@ mod tests { .await .unwrap(); - let pool = &sm.storage().pool; + let pool = sm.storage().pool().await.unwrap(); sqlx::query("UPDATE sessions SET goose_mode = 'garbage' WHERE id = ?") .bind(&session.id) .execute(pool) @@ -5360,6 +5371,126 @@ mod tests { assert_eq!(acp_session.session_type, SessionType::Acp); } + #[tokio::test] + async fn test_application_pool_opens_connections_after_migration() { + use sqlx::Row; + + let temp_dir = TempDir::new().unwrap(); + let db_path = temp_dir.path().join(SESSIONS_FOLDER).join(DB_NAME); + let bootstrap = + SessionStorage::create_pool(&db_path, SqlitePoolOptions::new().max_connections(1)); + SessionStorage::create_schema(&bootstrap).await.unwrap(); + + for column in [ + "system_prompt_override", + "system_prompt_extras_json", + "container_id", + ] { + sqlx::query(AssertSqlSafe(format!( + "ALTER TABLE sessions DROP COLUMN {column}" + ))) + .execute(&bootstrap) + .await + .unwrap(); + } + sqlx::query("UPDATE schema_version SET version = 16") + .execute(&bootstrap) + .await + .unwrap(); + sqlx::query("INSERT INTO sessions (id, working_dir) VALUES ('before_migration', '/tmp')") + .execute(&bootstrap) + .await + .unwrap(); + + let storage = SessionStorage::new(temp_dir.path().to_path_buf()); + let migration_lock = bootstrap.begin_with("BEGIN IMMEDIATE").await.unwrap(); + let first = storage.pool(); + tokio::pin!(first); + assert!( + tokio::time::timeout(std::time::Duration::from_millis(100), &mut first) + .await + .is_err() + ); + assert!(storage.pool.get().is_none()); + migration_lock.commit().await.unwrap(); + bootstrap.close().await; + + let (first, second) = tokio::join!(first, storage.pool()); + let pool = first.unwrap(); + assert!(std::ptr::eq(pool, second.unwrap())); + + let mut connections = Vec::new(); + for _ in 0..pool.options().get_max_connections() { + let mut connection = pool.acquire().await.unwrap(); + // A wildcard deliberately exercises the SQLx stale-metadata failure + // independently of the explicit projection used by create_session. + let row = sqlx::query("SELECT * FROM sessions WHERE id = 'before_migration'") + .fetch_one(&mut *connection) + .await + .unwrap(); + for column in [ + "system_prompt_override", + "system_prompt_extras_json", + "container_id", + ] { + assert!(row.try_get::, _>(column).unwrap().is_none()); + } + connections.push(connection); + } + drop(connections); + + let session = storage + .create_session( + None, + temp_dir.path().to_path_buf(), + "After migration".to_string(), + SessionType::User, + GooseMode::default(), + ) + .await + .unwrap(); + assert_eq!(session.name, "After migration"); + assert!(session.system_prompt_override.is_none()); + assert!(session.system_prompt_extras.is_empty()); + assert!(session.container.is_none()); + pool.close().await; + } + + #[tokio::test] + async fn test_failed_migration_does_not_publish_application_pool() { + let temp_dir = TempDir::new().unwrap(); + let db_path = temp_dir.path().join(SESSIONS_FOLDER).join(DB_NAME); + let bootstrap = + SessionStorage::create_pool(&db_path, SqlitePoolOptions::new().max_connections(1)); + sqlx::query("CREATE TABLE schema_version (version INTEGER PRIMARY KEY)") + .execute(&bootstrap) + .await + .unwrap(); + sqlx::query("INSERT INTO schema_version VALUES (16)") + .execute(&bootstrap) + .await + .unwrap(); + + let storage = SessionStorage::new(temp_dir.path().to_path_buf()); + assert!(storage.pool().await.is_err()); + assert!(storage.pool.get().is_none()); + let version: i32 = sqlx::query_scalar("SELECT MAX(version) FROM schema_version") + .fetch_one(&bootstrap) + .await + .unwrap(); + assert_eq!(version, 16); + + SessionStorage::create_schema(&bootstrap).await.unwrap(); + bootstrap.close().await; + let pool = storage.pool().await.unwrap(); + let version: i32 = sqlx::query_scalar("SELECT MAX(version) FROM schema_version") + .fetch_one(pool) + .await + .unwrap(); + assert_eq!(version, CURRENT_SCHEMA_VERSION); + pool.close().await; + } + #[tokio::test] async fn test_cache_token_columns_migration_and_round_trip() { let temp_dir = TempDir::new().unwrap();