Lightweight coding agent that runs in your terminal
Rust
Avoid full metadata rewrites for thread timestamp updates (#48983)
jifc0d26949be41
diff --git a/codex-rs/thread-store/src/local/mod.rs b/codex-rs/thread-store/src/local/mod.rsindex 24f1e54990..05f2d9061d 100644--- a/codex-rs/thread-store/src/local/mod.rs+++ b/codex-rs/thread-store/src/local/mod.rs@@ -38,6 +38,9 @@ mod pending_thread_metadata_tests;mod read_thread_tests;#[cfg(test)]mod test_support;+#[cfg(test)]+#[path = "timestamp_metadata_tests.rs"]+mod timestamp_metadata_tests;use codex_protocol::ThreadId;use codex_protocol::protocol::ThreadHistoryMode;diff --git a/codex-rs/thread-store/src/local/timestamp_metadata_tests.rs b/codex-rs/thread-store/src/local/timestamp_metadata_tests.rsnew file mode 100644index 0000000000..0e48bbce7b--- /dev/null+++ b/codex-rs/thread-store/src/local/timestamp_metadata_tests.rs@@ -0,0 +1,126 @@+//! Verifies narrow SQLite timestamp updates preserve metadata and repair fallbacks.++use super::test_support::test_config;+use super::test_support::write_session_file_with_history_mode;+use super::*;+use chrono::Utc;+use pretty_assertions::assert_eq;++#[tokio::test]+async fn timestamp_updates_repair_missing_rows_then_touch_only_timestamp_columns() {+ for history_mode in [ThreadHistoryMode::Legacy, ThreadHistoryMode::Paginated] {+ let home = tempfile::TempDir::new().expect("temp dir");+ let config = test_config(home.path());+ let runtime = codex_state::StateRuntime::init(+ config.sqlite.clone(),+ config.default_model_provider_id.clone(),+ )+ .await+ .expect("initialize state db");+ let pool = config+ .sqlite+ .open_read_write_pool(&config.sqlite.state_db_path())+ .await+ .expect("open SQL observer");+ let store = LocalThreadStore::new(config, Some(runtime.clone()));+ let id = uuid::Uuid::new_v4();+ let thread_id = ThreadId::from_string(&id.to_string()).expect("thread id");+ let path = write_session_file_with_history_mode(+ home.path(),+ "2025-01-03T12-00-00",+ id,+ history_mode,+ )+ .expect("write canonical history");+ let params = UpdateThreadMetadataParams {+ thread_id,+ patch: ThreadMetadataPatch {+ updated_at: Some(Utc::now()),+ ..Default::default()+ },+ include_archived: false,+ };+ // Missing rows must still be reconstructed from their canonical history.+ store+ .update_thread_metadata(params.clone())+ .await+ .expect("reconstruct missing metadata");+ let mut expected = runtime+ .get_thread(thread_id)+ .await+ .expect("read repaired metadata")+ .expect("repaired row");+ assert_eq!(+ (&expected.rollout_path, expected.history_mode),+ (&path, history_mode)+ );+ sqlx::raw_sql(+ r#"+ CREATE TABLE timestamp_writes(kind TEXT);+ CREATE TRIGGER count_timestamp AFTER UPDATE OF updated_at_ms ON threads+ BEGIN INSERT INTO timestamp_writes VALUES ('timestamp'); END;+ CREATE TRIGGER count_full_row AFTER UPDATE OF title ON threads+ BEGIN INSERT INTO timestamp_writes VALUES ('full'); END;+ "#,+ )+ .execute(&pool)+ .await+ .expect("install write counters");++ store+ .update_thread_metadata(params.clone())+ .await+ .expect("touch existing metadata");+ let actual = runtime+ .get_thread(thread_id)+ .await+ .expect("read touched metadata")+ .expect("touched row");+ assert!(actual.updated_at > expected.updated_at);+ expected.updated_at = actual.updated_at;+ assert_eq!(actual, expected);+ let writes: Vec<String> =+ sqlx::query_scalar("SELECT kind FROM timestamp_writes ORDER BY rowid")+ .fetch_all(&pool)+ .await+ .expect("read write counters");+ assert_eq!(writes, vec!["timestamp"]);++ store+ .stage_pending_thread_metadata(+ thread_id,+ ThreadMetadataPatch {+ model: Some("staged-model".to_string()),+ ..Default::default()+ },+ )+ .await+ .expect("stage metadata");+ store+ .update_thread_metadata(params)+ .await+ .expect("apply staged metadata");+ let actual = runtime+ .get_thread(thread_id)+ .await+ .expect("read staged metadata")+ .expect("updated row");+ expected.model = Some("staged-model".to_string());+ expected.updated_at = actual.updated_at;+ assert_eq!(actual, expected);+ assert!(+ store+ .pending_thread_metadata+ .lock(thread_id)+ .await+ .is_none()+ );+ let mut writes: Vec<String> = sqlx::query_scalar("SELECT kind FROM timestamp_writes")+ .fetch_all(&pool)+ .await+ .expect("read write counters");+ writes.sort();+ assert_eq!(writes, vec!["full", "timestamp", "timestamp"]);+ pool.close().await;+ }+}diff --git a/codex-rs/thread-store/src/local/update_thread_metadata.rs b/codex-rs/thread-store/src/local/update_thread_metadata.rsindex 8cfb38bf2f..76bc877d5e 100644--- a/codex-rs/thread-store/src/local/update_thread_metadata.rs+++ b/codex-rs/thread-store/src/local/update_thread_metadata.rs@@ -360,6 +360,22 @@ async fn apply_metadata_update(.map_err(|err| ThreadStoreError::Internal {message: format!("failed to read thread metadata for {thread_id}: {err}"),})?;+ // Existing timestamp-only observations must not rewrite metadata or its indexes.+ // Missing rows and changed rollout paths still need the normal repair path.+ if let Some(updated_at) = patch.updated_at+ && patch.is_empty_except_updated_at()+ && existing.as_ref().is_some_and(|metadata| {+ rollout_path.as_ref().is_none_or(|path| path == &metadata.rollout_path)+ })+ && state_db+ .touch_thread_updated_at(thread_id, updated_at)+ .await+ .map_err(|err| ThreadStoreError::Internal {+ message: format!("failed to update thread timestamp for {thread_id}: {err}"),+ })?+ {+ return Ok(());+ }let project_id = if existing.is_none()&& let Some(Some(project_id)) = patch.project_id.as_ref()&& state_dbdiff --git a/codex-rs/thread-store/src/types.rs b/codex-rs/thread-store/src/types.rsindex 692cb1029c..5030f8864d 100644--- a/codex-rs/thread-store/src/types.rs+++ b/codex-rs/thread-store/src/types.rs@@ -914,6 +914,10 @@ impl ThreadMetadataPatch {}pub fn is_empty(&self) -> bool {+ self.updated_at.is_none() && self.is_empty_except_updated_at()+ }++ pub(crate) fn is_empty_except_updated_at(&self) -> bool {self.name.is_none()&& self.rollout_path.is_none()&& self.preview.is_none()@@ -922,7 +926,6 @@ impl ThreadMetadataPatch {&& self.model.is_none()&& self.reasoning_effort.is_none()&& self.created_at.is_none()- && self.updated_at.is_none()&& self.advance_recency_at.is_none()&& self.source.is_none()&& self.originator.is_none()