diff --git a/codex-rs/state/migrations/0040_cleanup_guardian_thread_metadata.sql b/codex-rs/state/migrations/0040_cleanup_guardian_thread_metadata.sql new file mode 100644 index 0000000000..5cfee1f216 --- /dev/null +++ b/codex-rs/state/migrations/0040_cleanup_guardian_thread_metadata.sql @@ -0,0 +1,13 @@ +-- Guardian prompts are synthetic review context, not user-authored messages. +-- Keep the SQLite projection small; rollout JSONL remains canonical. +-- Legacy automatic titles were empty or copied first_user_message; preserve a +-- different non-empty title because it may have been set explicitly. +UPDATE threads +SET title = CASE + WHEN trim(title) = '' OR trim(title) = trim(first_user_message) + THEN 'Guardian review' + ELSE title + END, + preview = 'Approval review', + first_user_message = '' +WHERE source = '{"subagent":{"other":"guardian"}}'; diff --git a/codex-rs/state/src/migrations_tests.rs b/codex-rs/state/src/migrations_tests.rs index d11cc13ea1..9b48daf713 100644 --- a/codex-rs/state/src/migrations_tests.rs +++ b/codex-rs/state/src/migrations_tests.rs @@ -26,6 +26,136 @@ fn migrator_through(version: i64) -> Migrator { } } +#[tokio::test] +async fn guardian_metadata_cleanup_compacts_prompt_projection() { + let pool = SqlitePoolOptions::new() + .max_connections(1) + .connect("sqlite::memory:") + .await + .expect("in-memory database should open"); + migrator_through(/*version*/ 39) + .run(&pool) + .await + .expect("pre-cleanup migrations should apply"); + + sqlx::query( + r#" +INSERT INTO threads ( + id, + rollout_path, + created_at, + updated_at, + created_at_ms, + updated_at_ms, + source, + model_provider, + cwd, + title, + preview, + sandbox_policy, + approval_mode, + first_user_message +) VALUES + ( + '00000000-0000-0000-0000-000000000001', + '/tmp/guardian.jsonl', + 1700000000, + 1700000100, + 1700000000123, + 1700000100456, + '{"subagent":{"other":"guardian"}}', + 'openai', + '/tmp', + 'large guardian prompt', + 'large guardian prompt', + 'read-only', + 'on-request', + 'large guardian prompt' + ), + ( + '00000000-0000-0000-0000-000000000002', + '/tmp/worker.jsonl', + 1700000000, + 1700000100, + 1700000000123, + 1700000100456, + '{"subagent":{"other":"worker"}}', + 'openai', + '/tmp', + 'worker title', + 'worker preview', + 'read-only', + 'on-request', + 'worker first message' + ), + ( + '00000000-0000-0000-0000-000000000003', + '/tmp/named-guardian.jsonl', + 1700000000, + 1700000100, + 1700000000123, + 1700000100456, + '{"subagent":{"other":"guardian"}}', + 'openai', + '/tmp', + 'Named Guardian review', + 'large guardian prompt', + 'read-only', + 'on-request', + 'large guardian prompt' + ) + "#, + ) + .execute(&pool) + .await + .expect("legacy metadata rows should insert"); + + STATE_MIGRATOR + .run(&pool) + .await + .expect("guardian metadata cleanup should apply"); + + let rows = + sqlx::query("SELECT id, title, preview, first_user_message FROM threads ORDER BY id") + .fetch_all(&pool) + .await + .expect("cleaned metadata rows should load"); + let actual = rows + .iter() + .map(|row| { + ( + row.get::("id"), + row.get::("title"), + row.get::("preview"), + row.get::("first_user_message"), + ) + }) + .collect::>(); + assert_eq!( + actual, + vec![ + ( + "00000000-0000-0000-0000-000000000001".to_string(), + "Guardian review".to_string(), + "Approval review".to_string(), + String::new(), + ), + ( + "00000000-0000-0000-0000-000000000002".to_string(), + "worker title".to_string(), + "worker preview".to_string(), + "worker first message".to_string(), + ), + ( + "00000000-0000-0000-0000-000000000003".to_string(), + "Named Guardian review".to_string(), + "Approval review".to_string(), + String::new(), + ), + ] + ); +} + #[tokio::test] async fn recency_migration_backfills_and_seeds_old_binary_inserts() { let pool = SqlitePoolOptions::new()