mirror of
https://github.com/openai/codex.git
synced 2026-11-22 07:13:50 +00:00
fix: Auto-recover from corrupted sqlite databases (#26859)
Further investigation of the sqlite incidents showed that the problems are due to corruption from the older version of SQLite that we recently upgraded, and that the data is truly corrupted in the root database -- recovery of all data is not possible. Given that the data is reconstructable from the rollouts on disk, we should just auto-backup the database and let codex rebuild the rollout info from the disk rollouts. The new behavior is that appserver auto-backs-up and rebuilds (with logs reflecting that behavior). The CLI now pops a message letting you know this happened and the paths of the backed-up corrupt db and the new database. There is also context added so that the desktop app can read the rebuild info from it and inform the user with it.
This commit is contained in:
1 parent
a19d43a40a
commit
3691fe5b76
13 files changed
+693
-139
No files matched your search
+35
-2
@@ -352,9 +352,11 @@ async fn init_state_db_for_app_server_target(
|
||||
) -> std::io::Result<Option<StateDbHandle>> {
|
||||
match app_server_target {
|
||||
AppServerTarget::Embedded => state_db::try_init(config).await.map(Some).map_err(|err| {
|
||||
let database_path = codex_state::runtime_db_path_for_corruption_error(&err)
|
||||
.unwrap_or_else(|| codex_state::state_db_path(config.sqlite_home.as_path()));
|
||||
std::io::Error::other(LocalStateDbStartupError::new(
|
||||
codex_state::state_db_path(config.sqlite_home.as_path()),
|
||||
err.to_string(),
|
||||
database_path,
|
||||
format!("{err:#}"),
|
||||
))
|
||||
}),
|
||||
AppServerTarget::LocalDaemon { .. } | AppServerTarget::Remote { .. } => {
|
||||
@@ -2990,6 +2992,37 @@ mod tests {
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn embedded_state_db_corruption_preserves_failed_database_for_cli_recovery()
|
||||
-> color_eyre::Result<()> {
|
||||
let temp_dir = TempDir::new()?;
|
||||
let mut config = build_config(&temp_dir).await?;
|
||||
let sqlite_home = temp_dir.path().join("sqlite-home");
|
||||
std::fs::create_dir_all(&sqlite_home)?;
|
||||
let logs_db_path = codex_state::logs_db_path(&sqlite_home);
|
||||
std::fs::write(&logs_db_path, "not a sqlite database")?;
|
||||
config.sqlite_home = sqlite_home;
|
||||
|
||||
let err =
|
||||
match init_state_db_for_app_server_target(&config, &AppServerTarget::Embedded).await {
|
||||
Ok(_) => panic!("embedded startup should surface state db init failures"),
|
||||
Err(err) => err,
|
||||
};
|
||||
let startup_error = err
|
||||
.get_ref()
|
||||
.and_then(|err| err.downcast_ref::<LocalStateDbStartupError>())
|
||||
.expect("state db startup failure should retain its typed context");
|
||||
|
||||
assert_eq!(startup_error.database_path(), logs_db_path.as_path());
|
||||
assert!(
|
||||
codex_state::sqlite_error_detail_is_corruption(startup_error.detail()),
|
||||
"startup error should preserve the SQLite corruption cause, got: {}",
|
||||
startup_error.detail()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn windows_shows_trust_prompt_with_sandbox() -> std::io::Result<()> {
|
||||
|
||||
@@ -3,24 +3,28 @@ use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[error(
|
||||
"failed to initialize sqlite state db at {}: {detail}",
|
||||
state_db_path.display()
|
||||
"failed to initialize sqlite local db at {}: {detail}",
|
||||
database_path.display()
|
||||
)]
|
||||
pub struct LocalStateDbStartupError {
|
||||
state_db_path: PathBuf,
|
||||
database_path: PathBuf,
|
||||
detail: String,
|
||||
}
|
||||
|
||||
impl LocalStateDbStartupError {
|
||||
pub fn new(state_db_path: PathBuf, detail: String) -> Self {
|
||||
pub fn new(database_path: PathBuf, detail: String) -> Self {
|
||||
Self {
|
||||
state_db_path,
|
||||
database_path,
|
||||
detail,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn database_path(&self) -> &Path {
|
||||
self.database_path.as_path()
|
||||
}
|
||||
|
||||
pub fn state_db_path(&self) -> &Path {
|
||||
self.state_db_path.as_path()
|
||||
self.database_path()
|
||||
}
|
||||
|
||||
pub fn detail(&self) -> &str {
|
||||
|
||||
Reference in new issue
Block a user