Lightweight coding agent that runs in your terminal
Rust
Prune diagnostic logs periodically by age and database size (#49425)
Dmitriy Kovalenko8ea2c0e0d4bc
diff --git a/codex-rs/state/src/runtime.rs b/codex-rs/state/src/runtime.rsindex 2adc20c5b4..3cf1622226 100644--- a/codex-rs/state/src/runtime.rs+++ b/codex-rs/state/src/runtime.rs@@ -43,6 +43,7 @@ mod backfill;mod external_agent_config_imports;mod goals;mod logs;+mod logs_maintenance;mod memories;mod memory_versions;mod projects;@@ -276,12 +277,7 @@ impl StateRuntime {runtime.close().await;return Err(err);}- if let Err(err) = runtime.run_logs_startup_maintenance().await {- warn!(- "failed to run startup maintenance for logs db at {}: {err}",- logs_path.display(),- );- }+ runtime.start_periodic_logs_maintenance(std::time::Duration::from_secs(30 * 60));Ok(runtime)}diff --git a/codex-rs/state/src/runtime/logs.rs b/codex-rs/state/src/runtime/logs.rsindex 0cdc91124c..9b331b491a 100644--- a/codex-rs/state/src/runtime/logs.rs+++ b/codex-rs/state/src/runtime/logs.rs@@ -1,7 +1,5 @@use super::*;-const LOG_RETENTION_DAYS: i64 = 10;-impl StateRuntime {pub async fn insert_log(&self, entry: &LogEntry) -> anyhow::Result<()> {self.insert_logs(std::slice::from_ref(entry)).await@@ -280,30 +278,6 @@ WHERE id IN (Ok(())}- pub(crate) async fn delete_logs_before(&self, cutoff_ts: i64) -> anyhow::Result<u64> {- let result = sqlx::query("DELETE FROM logs WHERE ts < ?")- .bind(cutoff_ts)- .execute(self.logs_pool.as_ref())- .await?;- Ok(result.rows_affected())- }-- pub(crate) async fn run_logs_startup_maintenance(&self) -> anyhow::Result<()> {- let Some(cutoff) =- Utc::now().checked_sub_signed(chrono::Duration::days(LOG_RETENTION_DAYS))- else {- return Ok(());- };- self.delete_logs_before(cutoff.timestamp()).await?;- // Startup cleanup should not wait behind or block foreground work.- // PASSIVE checkpoints copy whatever is immediately available and skip- // frames that would require waiting on active readers or writers.- sqlx::query("PRAGMA wal_checkpoint(PASSIVE)")- .execute(self.logs_pool.as_ref())- .await?;- Ok(())- }-/// Query logs with optional filters.pub async fn query_logs(&self, query: &LogQuery) -> anyhow::Result<Vec<LogRow>> {let mut builder = QueryBuilder::<Sqlite>::new(@@ -568,6 +542,7 @@ mod tests {#[tokio::test]async fn insert_logs_use_dedicated_log_database() {+ let now = chrono::Utc::now().timestamp();let codex_home = unique_temp_dir();let runtime = StateRuntime::init(crate::SqliteConfig::new_for_testing(codex_home.as_path().abs()),@@ -578,7 +553,7 @@ mod tests {runtime.insert_logs(&[LogEntry {- ts: 1,+ ts: now + 1,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -753,6 +728,7 @@ mod tests {#[tokio::test]async fn query_logs_with_search_matches_rendered_body_substring() {+ let now = chrono::Utc::now().timestamp();let codex_home = unique_temp_dir();let runtime = StateRuntime::init(crate::SqliteConfig::new_for_testing(codex_home.as_path().abs()),@@ -764,7 +740,7 @@ mod tests {runtime.insert_logs(&[LogEntry {- ts: 1_700_000_001,+ ts: now + 1,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -777,7 +753,7 @@ mod tests {module_path: None,},LogEntry {- ts: 1_700_000_002,+ ts: now + 2,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -809,6 +785,7 @@ mod tests {#[tokio::test]async fn query_logs_filters_level_set_without_rewriting_stored_level() {+ let now = chrono::Utc::now().timestamp();let codex_home = unique_temp_dir();let runtime = StateRuntime::init(crate::SqliteConfig::new_for_testing(codex_home.as_path().abs()),@@ -820,7 +797,7 @@ mod tests {runtime.insert_logs(&[LogEntry {- ts: 1,+ ts: now + 1,ts_nanos: 0,level: "TRACE".to_string(),target: "cli".to_string(),@@ -833,7 +810,7 @@ mod tests {module_path: None,},LogEntry {- ts: 2,+ ts: now + 2,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -846,7 +823,7 @@ mod tests {module_path: None,},LogEntry {- ts: 3,+ ts: now + 3,ts_nanos: 0,level: "warn".to_string(),target: "cli".to_string(),@@ -859,7 +836,7 @@ mod tests {module_path: None,},LogEntry {- ts: 4,+ ts: now + 4,ts_nanos: 0,level: "ERROR".to_string(),target: "cli".to_string(),@@ -897,6 +874,7 @@ mod tests {#[tokio::test]async fn insert_logs_prunes_old_rows_when_thread_exceeds_size_limit() {+ let now = chrono::Utc::now().timestamp();let codex_home = unique_temp_dir();let runtime = StateRuntime::init(crate::SqliteConfig::new_for_testing(codex_home.as_path().abs()),@@ -909,7 +887,7 @@ mod tests {runtime.insert_logs(&[LogEntry {- ts: 1,+ ts: now + 1,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -922,7 +900,7 @@ mod tests {module_path: Some("mod".to_string()),},LogEntry {- ts: 2,+ ts: now + 2,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -947,13 +925,14 @@ mod tests {.expect("query thread logs");assert_eq!(rows.len(), 1);- assert_eq!(rows[0].ts, 2);+ assert_eq!(rows[0].ts, now + 2);let _ = tokio::fs::remove_dir_all(codex_home).await;}#[tokio::test]async fn insert_logs_prunes_single_thread_row_when_it_exceeds_size_limit() {+ let now = chrono::Utc::now().timestamp();let codex_home = unique_temp_dir();let runtime = StateRuntime::init(crate::SqliteConfig::new_for_testing(codex_home.as_path().abs()),@@ -965,7 +944,7 @@ mod tests {let eleven_mebibytes = "d".repeat(11 * 1024 * 1024);runtime.insert_logs(&[LogEntry {- ts: 1,+ ts: now + 1,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -995,6 +974,7 @@ mod tests {#[tokio::test]async fn insert_logs_prunes_threadless_rows_per_process_uuid_only() {+ let now = chrono::Utc::now().timestamp();let codex_home = unique_temp_dir();let runtime = StateRuntime::init(crate::SqliteConfig::new_for_testing(codex_home.as_path().abs()),@@ -1007,7 +987,7 @@ mod tests {runtime.insert_logs(&[LogEntry {- ts: 1,+ ts: now + 1,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1020,7 +1000,7 @@ mod tests {module_path: Some("mod".to_string()),},LogEntry {- ts: 2,+ ts: now + 2,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1033,7 +1013,7 @@ mod tests {module_path: Some("mod".to_string()),},LogEntry {- ts: 3,+ ts: now + 3,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1060,13 +1040,14 @@ mod tests {let mut timestamps: Vec<i64> = rows.into_iter().map(|row| row.ts).collect();timestamps.sort_unstable();- assert_eq!(timestamps, vec![2, 3]);+ assert_eq!(timestamps, vec![now + 2, now + 3]);let _ = tokio::fs::remove_dir_all(codex_home).await;}#[tokio::test]async fn insert_logs_prunes_single_threadless_process_row_when_it_exceeds_size_limit() {+ let now = chrono::Utc::now().timestamp();let codex_home = unique_temp_dir();let runtime = StateRuntime::init(crate::SqliteConfig::new_for_testing(codex_home.as_path().abs()),@@ -1078,7 +1059,7 @@ mod tests {let eleven_mebibytes = "e".repeat(11 * 1024 * 1024);runtime.insert_logs(&[LogEntry {- ts: 1,+ ts: now + 1,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1108,6 +1089,7 @@ mod tests {#[tokio::test]async fn insert_logs_prunes_threadless_rows_with_null_process_uuid() {+ let now = chrono::Utc::now().timestamp();let codex_home = unique_temp_dir();let runtime = StateRuntime::init(crate::SqliteConfig::new_for_testing(codex_home.as_path().abs()),@@ -1120,7 +1102,7 @@ mod tests {runtime.insert_logs(&[LogEntry {- ts: 1,+ ts: now + 1,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1133,7 +1115,7 @@ mod tests {module_path: Some("mod".to_string()),},LogEntry {- ts: 2,+ ts: now + 2,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1146,7 +1128,7 @@ mod tests {module_path: Some("mod".to_string()),},LogEntry {- ts: 3,+ ts: now + 3,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1172,13 +1154,14 @@ mod tests {let mut timestamps: Vec<i64> = rows.into_iter().map(|row| row.ts).collect();timestamps.sort_unstable();- assert_eq!(timestamps, vec![2, 3]);+ assert_eq!(timestamps, vec![now + 2, now + 3]);let _ = tokio::fs::remove_dir_all(codex_home).await;}#[tokio::test]async fn insert_logs_prunes_single_threadless_null_process_row_when_it_exceeds_limit() {+ let now = chrono::Utc::now().timestamp();let codex_home = unique_temp_dir();let runtime = StateRuntime::init(crate::SqliteConfig::new_for_testing(codex_home.as_path().abs()),@@ -1190,7 +1173,7 @@ mod tests {let eleven_mebibytes = "f".repeat(11 * 1024 * 1024);runtime.insert_logs(&[LogEntry {- ts: 1,+ ts: now + 1,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1220,6 +1203,7 @@ mod tests {#[tokio::test]async fn insert_logs_prunes_old_rows_when_thread_exceeds_row_limit() {+ let now = chrono::Utc::now().timestamp();let codex_home = unique_temp_dir();let runtime = StateRuntime::init(crate::SqliteConfig::new_for_testing(codex_home.as_path().abs()),@@ -1230,7 +1214,7 @@ mod tests {let entries: Vec<LogEntry> = (1..=1_001).map(|ts| LogEntry {- ts,+ ts: now + ts,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1258,14 +1242,15 @@ mod tests {let timestamps: Vec<i64> = rows.into_iter().map(|row| row.ts).collect();assert_eq!(timestamps.len(), 1_000);- assert_eq!(timestamps.first().copied(), Some(2));- assert_eq!(timestamps.last().copied(), Some(1_001));+ assert_eq!(timestamps.first().copied(), Some(now + 2));+ assert_eq!(timestamps.last().copied(), Some(now + 1_001));let _ = tokio::fs::remove_dir_all(codex_home).await;}#[tokio::test]async fn insert_logs_prunes_old_threadless_rows_when_process_exceeds_row_limit() {+ let now = chrono::Utc::now().timestamp();let codex_home = unique_temp_dir();let runtime = StateRuntime::init(crate::SqliteConfig::new_for_testing(codex_home.as_path().abs()),@@ -1276,7 +1261,7 @@ mod tests {let entries: Vec<LogEntry> = (1..=1_001).map(|ts| LogEntry {- ts,+ ts: now + ts,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1308,14 +1293,15 @@ mod tests {.map(|row| row.ts).collect();assert_eq!(timestamps.len(), 1_000);- assert_eq!(timestamps.first().copied(), Some(2));- assert_eq!(timestamps.last().copied(), Some(1_001));+ assert_eq!(timestamps.first().copied(), Some(now + 2));+ assert_eq!(timestamps.last().copied(), Some(now + 1_001));let _ = tokio::fs::remove_dir_all(codex_home).await;}#[tokio::test]async fn insert_logs_prunes_old_threadless_null_process_rows_when_row_limit_exceeded() {+ let now = chrono::Utc::now().timestamp();let codex_home = unique_temp_dir();let runtime = StateRuntime::init(crate::SqliteConfig::new_for_testing(codex_home.as_path().abs()),@@ -1326,7 +1312,7 @@ mod tests {let entries: Vec<LogEntry> = (1..=1_001).map(|ts| LogEntry {- ts,+ ts: now + ts,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1358,14 +1344,15 @@ mod tests {.map(|row| row.ts).collect();assert_eq!(timestamps.len(), 1_000);- assert_eq!(timestamps.first().copied(), Some(2));- assert_eq!(timestamps.last().copied(), Some(1_001));+ assert_eq!(timestamps.first().copied(), Some(now + 2));+ assert_eq!(timestamps.last().copied(), Some(now + 1_001));let _ = tokio::fs::remove_dir_all(codex_home).await;}#[tokio::test]async fn query_feedback_logs_returns_newest_lines_within_limit_in_order() {+ let now = chrono::Utc::now().timestamp();let codex_home = unique_temp_dir();let runtime = StateRuntime::init(crate::SqliteConfig::new_for_testing(codex_home.as_path().abs()),@@ -1377,7 +1364,7 @@ mod tests {runtime.insert_logs(&[LogEntry {- ts: 1,+ ts: now + 1,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1390,7 +1377,7 @@ mod tests {module_path: None,},LogEntry {- ts: 2,+ ts: now + 2,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1403,7 +1390,7 @@ mod tests {module_path: None,},LogEntry {- ts: 3,+ ts: now + 3,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1427,9 +1414,14 @@ mod tests {assert_eq!(String::from_utf8(bytes).expect("valid utf-8"),[- format_feedback_log_line(/*ts*/ 1, /*ts_nanos*/ 0, "INFO", "alpha"),- format_feedback_log_line(/*ts*/ 2, /*ts_nanos*/ 0, "INFO", "bravo"),- format_feedback_log_line(/*ts*/ 3, /*ts_nanos*/ 0, "INFO", "charlie"),+ format_feedback_log_line(/*ts*/ now + 1, /*ts_nanos*/ 0, "INFO", "alpha"),+ format_feedback_log_line(/*ts*/ now + 2, /*ts_nanos*/ 0, "INFO", "bravo"),+ format_feedback_log_line(+ /*ts*/ now + 3,+ /*ts_nanos*/ 0,+ "INFO",+ "charlie"+ ),].concat());@@ -1439,6 +1431,7 @@ mod tests {#[tokio::test]async fn query_feedback_logs_excludes_oversized_newest_row() {+ let now = chrono::Utc::now().timestamp();let codex_home = unique_temp_dir();let runtime = StateRuntime::init(crate::SqliteConfig::new_for_testing(codex_home.as_path().abs()),@@ -1451,7 +1444,7 @@ mod tests {runtime.insert_logs(&[LogEntry {- ts: 1,+ ts: now + 1,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1464,7 +1457,7 @@ mod tests {module_path: None,},LogEntry {- ts: 2,+ ts: now + 2,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1492,6 +1485,7 @@ mod tests {#[tokio::test]async fn query_feedback_logs_includes_threadless_rows_from_same_process() {+ let now = chrono::Utc::now().timestamp();let codex_home = unique_temp_dir();let runtime = StateRuntime::init(crate::SqliteConfig::new_for_testing(codex_home.as_path().abs()),@@ -1503,7 +1497,7 @@ mod tests {runtime.insert_logs(&[LogEntry {- ts: 1,+ ts: now + 1,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1516,7 +1510,7 @@ mod tests {module_path: None,},LogEntry {- ts: 2,+ ts: now + 2,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1529,7 +1523,7 @@ mod tests {module_path: None,},LogEntry {- ts: 3,+ ts: now + 3,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1542,7 +1536,7 @@ mod tests {module_path: None,},LogEntry {- ts: 4,+ ts: now + 4,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1567,19 +1561,19 @@ mod tests {String::from_utf8(bytes).expect("valid utf-8"),[format_feedback_log_line(- /*ts*/ 1,+ /*ts*/ now + 1,/*ts_nanos*/ 0,"INFO","threadless-before"),format_feedback_log_line(- /*ts*/ 2,+ /*ts*/ now + 2,/*ts_nanos*/ 0,"INFO","thread-scoped"),format_feedback_log_line(- /*ts*/ 3,+ /*ts*/ now + 3,/*ts_nanos*/ 0,"INFO","threadless-after"@@ -1593,6 +1587,7 @@ mod tests {#[tokio::test]async fn query_feedback_logs_excludes_threadless_rows_from_prior_processes() {+ let now = chrono::Utc::now().timestamp();let codex_home = unique_temp_dir();let runtime = StateRuntime::init(crate::SqliteConfig::new_for_testing(codex_home.as_path().abs()),@@ -1604,7 +1599,7 @@ mod tests {runtime.insert_logs(&[LogEntry {- ts: 1,+ ts: now + 1,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1617,7 +1612,7 @@ mod tests {module_path: None,},LogEntry {- ts: 2,+ ts: now + 2,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1630,7 +1625,7 @@ mod tests {module_path: None,},LogEntry {- ts: 3,+ ts: now + 3,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1643,7 +1638,7 @@ mod tests {module_path: None,},LogEntry {- ts: 4,+ ts: now + 4,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1668,19 +1663,19 @@ mod tests {String::from_utf8(bytes).expect("valid utf-8"),[format_feedback_log_line(- /*ts*/ 2,+ /*ts*/ now + 2,/*ts_nanos*/ 0,"INFO","old-process-thread"),format_feedback_log_line(- /*ts*/ 3,+ /*ts*/ now + 3,/*ts_nanos*/ 0,"INFO","new-process-thread"),format_feedback_log_line(- /*ts*/ 4,+ /*ts*/ now + 4,/*ts_nanos*/ 0,"INFO","new-process-threadless"@@ -1694,6 +1689,7 @@ mod tests {#[tokio::test]async fn query_feedback_logs_keeps_newest_suffix_across_thread_and_threadless_logs() {+ let now = chrono::Utc::now().timestamp();let codex_home = unique_temp_dir();let runtime = StateRuntime::init(crate::SqliteConfig::new_for_testing(codex_home.as_path().abs()),@@ -1714,7 +1710,7 @@ mod tests {runtime.insert_logs(&[LogEntry {- ts: 1,+ ts: now + 1,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1727,7 +1723,7 @@ mod tests {module_path: None,},LogEntry {- ts: 2,+ ts: now + 2,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1740,7 +1736,7 @@ mod tests {module_path: None,},LogEntry {- ts: 3,+ ts: now + 3,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1772,6 +1768,7 @@ mod tests {#[tokio::test]async fn query_feedback_logs_for_threads_merges_requested_threads_and_threadless_rows() {+ let now = chrono::Utc::now().timestamp();let codex_home = unique_temp_dir();let runtime = StateRuntime::init(crate::SqliteConfig::new_for_testing(codex_home.as_path().abs()),@@ -1783,7 +1780,7 @@ mod tests {runtime.insert_logs(&[LogEntry {- ts: 1,+ ts: now + 1,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1796,7 +1793,7 @@ mod tests {module_path: None,},LogEntry {- ts: 2,+ ts: now + 2,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1809,7 +1806,7 @@ mod tests {module_path: None,},LogEntry {- ts: 3,+ ts: now + 3,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1822,7 +1819,7 @@ mod tests {module_path: None,},LogEntry {- ts: 4,+ ts: now + 4,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1835,7 +1832,7 @@ mod tests {module_path: None,},LogEntry {- ts: 5,+ ts: now + 5,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1848,7 +1845,7 @@ mod tests {module_path: None,},LogEntry {- ts: 6,+ ts: now + 6,ts_nanos: 0,level: "INFO".to_string(),target: "cli".to_string(),@@ -1872,16 +1869,26 @@ mod tests {assert_eq!(String::from_utf8(bytes).expect("valid utf-8"),[- format_feedback_log_line(/*ts*/ 1, /*ts_nanos*/ 0, "INFO", "thread-1"),- format_feedback_log_line(/*ts*/ 2, /*ts_nanos*/ 0, "INFO", "thread-2"),format_feedback_log_line(- /*ts*/ 3,+ /*ts*/ now + 1,+ /*ts_nanos*/ 0,+ "INFO",+ "thread-1"+ ),+ format_feedback_log_line(+ /*ts*/ now + 2,+ /*ts_nanos*/ 0,+ "INFO",+ "thread-2"+ ),+ format_feedback_log_line(+ /*ts*/ now + 3,/*ts_nanos*/ 0,"INFO","threadless-proc-1"),format_feedback_log_line(- /*ts*/ 4,+ /*ts*/ now + 4,/*ts_nanos*/ 0,"INFO","threadless-proc-2"diff --git a/codex-rs/state/src/runtime/logs_maintenance.rs b/codex-rs/state/src/runtime/logs_maintenance.rsnew file mode 100644index 0000000000..7d7879730c--- /dev/null+++ b/codex-rs/state/src/runtime/logs_maintenance.rs@@ -0,0 +1,84 @@+//! Prune diagnostic logs in the background immediately, then on a timer.+//! SQLite keeps freed pages for reuse.++use super::StateRuntime;+use chrono::Utc;+use sqlx::SqlitePool;+use std::sync::Arc;+use std::time::Duration;++const LOG_RETENTION_SECONDS: i64 = 10 * 24 * 60 * 60; // 10 days+const LOG_DATABASE_BUDGET_BYTES: i64 = 64 * 1024 * 1024; // 64 MiB++impl StateRuntime {+ pub(super) fn start_periodic_logs_maintenance(self: &Arc<Self>, period: Duration) {+ let runtime = Arc::downgrade(self);+ tokio::spawn(async move {+ let mut interval = tokio::time::interval_at(+ /*start*/ tokio::time::Instant::now(),+ /*period*/ period,+ );+ interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);+ let mut first_sweep = true;+ loop {+ // Hold no strong runtime reference while waiting.+ interval.tick().await;+ let Some(runtime) = runtime.upgrade() else {+ break;+ };+ if runtime.logs_pool.is_closed() {+ break;+ }+ if let Err(err) = prune_by_age_and_size(+ &runtime.logs_pool,+ Utc::now().timestamp(),+ LOG_DATABASE_BUDGET_BYTES,+ )+ .await+ {+ tracing::warn!("failed to prune diagnostic logs: {err}");+ }+ if first_sweep {+ first_sweep = false;+ // Preserve the startup checkpoint without waiting for readers or writers.+ if let Err(err) = sqlx::query("PRAGMA wal_checkpoint(PASSIVE)")+ .execute(runtime.logs_pool.as_ref())+ .await+ {+ tracing::warn!("failed to checkpoint diagnostic logs: {err}");+ }+ }+ }+ });+ }+}++async fn prune_by_age_and_size(+ pool: &SqlitePool,+ now: i64,+ budget_bytes: i64,+) -> anyhow::Result<()> {+ let mut retention_seconds = LOG_RETENTION_SECONDS;+ loop {+ sqlx::query("DELETE FROM logs WHERE ts < ?")+ .bind(now.saturating_sub(retention_seconds))+ .execute(pool)+ .await?;+ // Exclude free pages: SQLite reuses them without shrinking the file.+ let occupied_bytes: i64 = sqlx::query_scalar(+ "SELECT (page_count - freelist_count) * page_size \+ FROM pragma_page_count(), pragma_freelist_count(), pragma_page_size()",+ )+ .fetch_one(pool)+ .await?;+ if occupied_bytes <= budget_bytes || retention_seconds == 1 {+ return Ok(());+ }+ // Keep the newest second even if it exceeds the budget.+ retention_seconds = (retention_seconds / 2).max(1);+ }+}++#[cfg(test)]+#[path = "logs_maintenance_tests.rs"]+mod tests;diff --git a/codex-rs/state/src/runtime/logs_maintenance_tests.rs b/codex-rs/state/src/runtime/logs_maintenance_tests.rsnew file mode 100644index 0000000000..1ca3939b1a--- /dev/null+++ b/codex-rs/state/src/runtime/logs_maintenance_tests.rs@@ -0,0 +1,166 @@+//! Exercise age/size pruning and automatic cleanup of an idle runtime.++use super::LOG_RETENTION_SECONDS;+use super::StateRuntime;+use super::prune_by_age_and_size;+use crate::SqliteConfig;+use crate::migrations::LOGS_MIGRATOR;+use chrono::Utc;+use codex_utils_absolute_path::test_support::PathExt;+use pretty_assertions::assert_eq;+use sqlx::SqlitePool;+use std::path::PathBuf;+use std::time::Duration;++const NOW: i64 = 2_000_000_000;+const DAY: i64 = 24 * 60 * 60;++async fn database_with_logs(ages: &[i64]) -> (SqlitePool, PathBuf) {+ let directory = super::super::test_support::unique_temp_dir();+ tokio::fs::create_dir_all(&directory).await.unwrap();+ let pool = SqliteConfig::new_for_testing(directory.as_path().abs())+ .open_read_write_pool(&directory.join("logs.sqlite"))+ .await+ .unwrap();+ LOGS_MIGRATOR.run(&pool).await.unwrap();+ for age in ages {+ sqlx::query(+ "INSERT INTO logs (ts, ts_nanos, level, target, feedback_log_body) \+ VALUES (?, 0, 'INFO', 'retention-test', ?)",+ )+ .bind(NOW - age)+ .bind("x".repeat(128 * 1024))+ .execute(&pool)+ .await+ .unwrap();+ }+ (pool, directory)+}++async fn retained_ages(pool: &SqlitePool) -> Vec<i64> {+ sqlx::query_scalar("SELECT ? - ts FROM logs ORDER BY ts DESC")+ .bind(NOW)+ .fetch_all(pool)+ .await+ .unwrap()+}++#[tokio::test]+async fn keeps_ten_day_boundary_when_under_budget() {+ let (pool, directory) =+ database_with_logs(&[0, LOG_RETENTION_SECONDS, LOG_RETENTION_SECONDS + 1]).await;+ prune_by_age_and_size(&pool, NOW, /*budget_bytes*/ 1024 * 1024)+ .await+ .unwrap();+ assert_eq!(retained_ages(&pool).await, vec![0, LOG_RETENTION_SECONDS]);+ pool.close().await;+ tokio::fs::remove_dir_all(directory).await.unwrap();+}++#[tokio::test]+async fn halves_again_when_first_halving_deletes_nothing() {+ let (pool, directory) = database_with_logs(&[DAY, 2 * DAY, 4 * DAY]).await;+ // Three payloads exceed this budget; two fit, including table/index pages.+ prune_by_age_and_size(&pool, NOW, /*budget_bytes*/ 350 * 1024)+ .await+ .unwrap();+ assert_eq!(retained_ages(&pool).await, vec![DAY, 2 * DAY]);+ // A second cleanup must ignore free pages left in the file.+ let allocated: i64 = sqlx::query_scalar(+ "SELECT page_count * page_size FROM pragma_page_count(), pragma_page_size()",+ )+ .fetch_one(&pool)+ .await+ .unwrap();+ assert!(allocated > 350 * 1024);+ prune_by_age_and_size(&pool, NOW, /*budget_bytes*/ 350 * 1024)+ .await+ .unwrap();+ assert_eq!(retained_ages(&pool).await, vec![DAY, 2 * DAY]);+ pool.close().await;+ tokio::fs::remove_dir_all(directory).await.unwrap();+}++#[tokio::test]+async fn stops_at_one_second_when_recent_rows_exceed_budget() {+ let (pool, directory) = database_with_logs(&[-1, 0, 1, 2, DAY]).await;+ prune_by_age_and_size(&pool, NOW, /*budget_bytes*/ 1)+ .await+ .unwrap();+ assert_eq!(retained_ages(&pool).await, vec![-1, 0, 1]);+ pool.close().await;+ tokio::fs::remove_dir_all(directory).await.unwrap();+}++#[tokio::test]+async fn periodic_cleanup_prunes_without_new_log_writes_or_restart() {+ let directory = super::super::test_support::unique_temp_dir();+ let runtime = StateRuntime::init(+ SqliteConfig::new_for_testing(directory.as_path().abs()),+ "test-provider".to_string(),+ )+ .await+ .unwrap();+ let now = Utc::now().timestamp();+ for timestamp in [now - 11 * DAY, now] {+ sqlx::query("INSERT INTO logs (ts, ts_nanos, level, target) VALUES (?, 0, 'INFO', 'test')")+ .bind(timestamp)+ .execute(runtime.logs_pool.as_ref())+ .await+ .unwrap();+ }+ runtime.start_periodic_logs_maintenance(Duration::from_millis(10));+ tokio::time::timeout(Duration::from_secs(5), async {+ loop {+ let timestamps: Vec<i64> = sqlx::query_scalar("SELECT ts FROM logs ORDER BY ts")+ .fetch_all(runtime.logs_pool.as_ref())+ .await+ .unwrap();+ if timestamps == vec![now] {+ break;+ }+ tokio::time::sleep(Duration::from_millis(10)).await;+ }+ })+ .await+ .expect("periodic cleanup should remove expired logs while idle");+ runtime.close().await;+ tokio::fs::remove_dir_all(directory).await.unwrap();+}++#[tokio::test]+async fn startup_cleanup_runs_without_waiting_for_the_period() {+ let directory = super::super::test_support::unique_temp_dir();+ tokio::fs::create_dir_all(&directory).await.unwrap();+ let sqlite = SqliteConfig::new_for_testing(directory.as_path().abs());+ let pool = sqlite+ .open_read_write_pool(&sqlite.logs_db_path())+ .await+ .unwrap();+ LOGS_MIGRATOR.run(&pool).await.unwrap();+ sqlx::query("INSERT INTO logs (ts, ts_nanos, level, target) VALUES (0, 0, 'INFO', 'test')")+ .execute(&pool)+ .await+ .unwrap();+ pool.close().await;++ let runtime = StateRuntime::init(sqlite, "test-provider".to_string())+ .await+ .unwrap();+ tokio::time::timeout(Duration::from_secs(5), async {+ loop {+ let count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM logs")+ .fetch_one(runtime.logs_pool.as_ref())+ .await+ .unwrap();+ if count == 0 {+ break;+ }+ tokio::time::sleep(Duration::from_millis(10)).await;+ }+ })+ .await+ .expect("startup cleanup should run before the 30-minute interval");+ runtime.close().await;+ tokio::fs::remove_dir_all(directory).await.unwrap();+}