mirror of
https://github.com/openai/codex.git
synced 2026-11-25 23:30:49 +00:00
Delete persisted message boards with their root threads (#47029)
## Why Permanent thread deletion needs to remove persisted agent message board data even when the message board feature has been disabled. ## What changed - Add a cleanup hook for single and batch thread deletion, invoked after reference and writer checks but before rollout removal so cleanup failures remain retryable. - Remove the deleted roots' channels, posts, and subscriptions regardless of the current feature setting. - Record board tombstones and check them within write transactions to prevent delayed writes from recreating deleted data. ## Testing Add an app-server regression test that creates a board, archives its root thread, restarts with the feature disabled, and verifies that permanent deletion removes the persisted channels, posts, and subscriptions. GitOrigin-RevId: 4ea71559a8df0814dffda23a056a9bbaf6e0a0cb
This commit is contained in:
@@ -4,9 +4,12 @@ use app_test_support::TestAppServer;
|
||||
use app_test_support::create_fake_paginated_rollout;
|
||||
use app_test_support::create_fake_rollout;
|
||||
use app_test_support::create_mock_responses_server_repeating_assistant;
|
||||
use app_test_support::create_mock_responses_server_sequence;
|
||||
use codex_app_server_protocol::ClientRequest;
|
||||
use codex_app_server_protocol::JSONRPCError;
|
||||
use codex_app_server_protocol::RequestId;
|
||||
use codex_app_server_protocol::ThreadArchiveParams;
|
||||
use codex_app_server_protocol::ThreadArchiveResponse;
|
||||
use codex_app_server_protocol::ThreadDeleteParams;
|
||||
use codex_app_server_protocol::ThreadDeleteResponse;
|
||||
use codex_app_server_protocol::ThreadDeletedNotification;
|
||||
@@ -16,14 +19,20 @@ use codex_app_server_protocol::ThreadResumeParams;
|
||||
use codex_app_server_protocol::ThreadResumeResponse;
|
||||
use codex_app_server_protocol::ThreadStartParams;
|
||||
use codex_app_server_protocol::ThreadStartResponse;
|
||||
use codex_app_server_protocol::TurnStartParams;
|
||||
use codex_app_server_protocol::TurnStatus;
|
||||
use codex_app_server_protocol::UserInput;
|
||||
use codex_core::find_thread_path_by_id_str;
|
||||
use codex_features::Feature;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::protocol::HistoryPosition;
|
||||
use codex_state::DirectionalThreadSpawnEdgeStatus;
|
||||
use codex_state::SqliteConfig;
|
||||
use codex_state::StateRuntime;
|
||||
use codex_utils_absolute_path::test_support::PathExt;
|
||||
use core_test_support::responses;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::json;
|
||||
use std::path::Path;
|
||||
use tempfile::TempDir;
|
||||
use tokio::time::timeout;
|
||||
@@ -334,3 +343,92 @@ async fn thread_delete_handles_live_threads_before_rollout_exists() -> Result<()
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn thread_delete_removes_persisted_board_even_with_feature_disabled() -> Result<()> {
|
||||
let server = create_mock_responses_server_sequence(vec![
|
||||
responses::sse(vec![
|
||||
responses::ev_function_call_with_namespace(
|
||||
"post",
|
||||
"collaboration",
|
||||
"post",
|
||||
&json!({"new_channel_name":"design", "text":"Persist this decision"}).to_string(),
|
||||
),
|
||||
responses::ev_completed("post"),
|
||||
]),
|
||||
responses::sse(vec![
|
||||
responses::ev_assistant_message("done", "Done"),
|
||||
responses::ev_completed("done"),
|
||||
]),
|
||||
])
|
||||
.await;
|
||||
let home = TempDir::new()?;
|
||||
MockResponsesConfig::new(&server.uri())
|
||||
.enable_feature(Feature::AgentMessageBoard)
|
||||
.enable_feature(Feature::MultiAgentV2)
|
||||
.write(home.path())?;
|
||||
let mut app = TestAppServer::builder()
|
||||
.with_codex_home(home.path())
|
||||
.build_initialized()
|
||||
.await?;
|
||||
let thread = app.start_thread(ThreadStartParams::default()).await?.thread;
|
||||
let completed = app
|
||||
.start_turn_and_wait_for_completion(TurnStartParams {
|
||||
thread_id: thread.id.clone(),
|
||||
input: vec![UserInput::Text {
|
||||
text: "Post the decision".into(),
|
||||
text_elements: Vec::new(),
|
||||
}],
|
||||
..Default::default()
|
||||
})
|
||||
.await?;
|
||||
assert_eq!(completed.turn.status, TurnStatus::Completed);
|
||||
|
||||
let sqlite = SqliteConfig::new_for_testing(home.path().abs());
|
||||
let pool = sqlite
|
||||
.open_read_only_pool(
|
||||
&sqlite.home().join("agent_message_board_1.sqlite"),
|
||||
/*busy_timeout*/ None,
|
||||
)
|
||||
.await?;
|
||||
assert_eq!(board_counts(&pool).await?, (1, 1, 1));
|
||||
let _: ThreadArchiveResponse = app
|
||||
.request(|request_id| ClientRequest::ThreadArchive {
|
||||
request_id,
|
||||
params: ThreadArchiveParams {
|
||||
thread_id: thread.id.clone(),
|
||||
},
|
||||
})
|
||||
.await?;
|
||||
app.shutdown_gracefully().await?;
|
||||
|
||||
MockResponsesConfig::new(&server.uri())
|
||||
.disable_feature(Feature::AgentMessageBoard)
|
||||
.write(home.path())?;
|
||||
let mut app = TestAppServer::builder()
|
||||
.with_codex_home(home.path())
|
||||
.build_initialized()
|
||||
.await?;
|
||||
assert_eq!(board_counts(&pool).await?, (1, 1, 1));
|
||||
let _: ThreadDeleteResponse = app
|
||||
.request(|request_id| ClientRequest::ThreadDelete {
|
||||
request_id,
|
||||
params: ThreadDeleteParams {
|
||||
thread_id: thread.id,
|
||||
},
|
||||
})
|
||||
.await?;
|
||||
assert_eq!(board_counts(&pool).await?, (0, 0, 0));
|
||||
app.shutdown_gracefully().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn board_counts(pool: &sqlx::SqlitePool) -> Result<(i64, i64, i64)> {
|
||||
Ok(sqlx::query_as(
|
||||
"SELECT (SELECT COUNT(*) FROM channels),
|
||||
(SELECT COUNT(*) FROM posts),
|
||||
(SELECT COUNT(*) FROM subscriptions)",
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await?)
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ use crate::tasks::interrupted_turn_history_marker;
|
||||
use crate::thread_startup_metadata::ThreadStartupMetadata;
|
||||
use codex_agent_graph_store::AgentGraphStore;
|
||||
use codex_agent_graph_store::LocalAgentGraphStore;
|
||||
use codex_agent_message_board_extension::LocalAgentMessageBoard;
|
||||
use codex_analytics::AnalyticsEventsClient;
|
||||
use codex_app_server_protocol::ThreadHistoryBuilder;
|
||||
use codex_app_server_protocol::TurnStatus;
|
||||
@@ -449,10 +450,23 @@ pub fn thread_store_from_config(
|
||||
.features
|
||||
.enabled(Feature::BackgroundPaginatedRolloutMigration);
|
||||
let has_state_db = state_db.is_some();
|
||||
let store = Arc::new(LocalThreadStore::new(
|
||||
LocalThreadStoreConfig::from_config(config),
|
||||
state_db,
|
||||
));
|
||||
let sqlite = config.sqlite_config().clone();
|
||||
let store = Arc::new(
|
||||
LocalThreadStore::new(LocalThreadStoreConfig::from_config(config), state_db)
|
||||
.with_thread_data_cleanup(move |thread_ids| {
|
||||
let sqlite = sqlite.clone();
|
||||
Box::pin(async move {
|
||||
let boards = thread_ids.into_iter().map(Into::into).collect::<Vec<_>>();
|
||||
LocalAgentMessageBoard::delete_boards(&sqlite, &boards)
|
||||
.await
|
||||
.map_err(|err| ThreadStoreError::Internal {
|
||||
message: format!(
|
||||
"failed to delete agent message boards: {err}"
|
||||
),
|
||||
})
|
||||
})
|
||||
}),
|
||||
);
|
||||
if has_state_db && background_migration_enabled {
|
||||
let startup_store = Arc::clone(&store);
|
||||
let codex_home = config.codex_home.to_path_buf();
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! SQLite-backed boards. The tree ID scopes every read and write.
|
||||
//!
|
||||
//! Immediate transactions serialize mutations across independently opened handles.
|
||||
//! Accepted posts survive runtime unload and process restart.
|
||||
//! Accepted posts survive runtime unload and process restart, but cannot recreate
|
||||
//! a board after its root has been permanently deleted.
|
||||
|
||||
use crate::ChannelSummary;
|
||||
use crate::CreateChannelRequest;
|
||||
@@ -34,14 +35,17 @@ use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
|
||||
mod lifecycle;
|
||||
mod paging;
|
||||
mod queries;
|
||||
|
||||
const MAX_POST_BYTES: usize = 64 * 1024;
|
||||
const MAX_CHANNEL_BYTES: usize = 128;
|
||||
const MAX_READ_CHARS: usize = 20_000;
|
||||
const DATABASE_FILE: &str = "agent_message_board_1.sqlite";
|
||||
|
||||
const SCHEMA: &str = "
|
||||
CREATE TABLE IF NOT EXISTS deleted_boards (board TEXT PRIMARY KEY NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS channels (
|
||||
board TEXT NOT NULL, name TEXT NOT NULL, name_search TEXT NOT NULL, created_at TEXT NOT NULL, timestamp INTEGER NOT NULL, author TEXT NOT NULL,
|
||||
PRIMARY KEY(board,name)
|
||||
@@ -86,7 +90,7 @@ impl LocalAgentMessageBoard {
|
||||
) -> Result<Self> {
|
||||
tokio::fs::create_dir_all(sqlite.home()).await?;
|
||||
let pool = sqlite
|
||||
.open_read_write_pool(&sqlite.home().join("agent_message_board_1.sqlite"))
|
||||
.open_read_write_pool(&sqlite.home().join(DATABASE_FILE))
|
||||
.await
|
||||
.map_err(storage_error)?;
|
||||
sqlx::raw_sql(SCHEMA)
|
||||
@@ -108,11 +112,7 @@ impl LocalAgentMessageBoard {
|
||||
validate_channel(&request.channel_name)?;
|
||||
let author = self.host.agent_path(caller).await?;
|
||||
let now = self.host.current_time(caller).await?;
|
||||
let mut tx = self
|
||||
.pool
|
||||
.begin_with("BEGIN IMMEDIATE")
|
||||
.await
|
||||
.map_err(storage_error)?;
|
||||
let mut tx = self.begin_write().await?;
|
||||
self.insert_channel(&mut tx, &request.channel_name, &author, now)
|
||||
.await?;
|
||||
if request.subscription == SubscriptionChange::Subscribe {
|
||||
@@ -166,11 +166,7 @@ impl LocalAgentMessageBoard {
|
||||
recipients.insert(self.host.resolve_agent(path.clone()).await?);
|
||||
}
|
||||
let now = self.host.current_time(caller).await?;
|
||||
let mut tx = self
|
||||
.pool
|
||||
.begin_with("BEGIN IMMEDIATE")
|
||||
.await
|
||||
.map_err(storage_error)?;
|
||||
let mut tx = self.begin_write().await?;
|
||||
if let Some(post) = self
|
||||
.existing_post(&mut tx, &request_id, &request_json)
|
||||
.await?
|
||||
@@ -270,11 +266,7 @@ impl LocalAgentMessageBoard {
|
||||
let caller_path = self.host.agent_path(caller).await?;
|
||||
let target_path = request.target_agent.unwrap_or(caller_path);
|
||||
let target_agent = self.host.resolve_agent(target_path.clone()).await?;
|
||||
let mut tx = self
|
||||
.pool
|
||||
.begin_with("BEGIN IMMEDIATE")
|
||||
.await
|
||||
.map_err(storage_error)?;
|
||||
let mut tx = self.begin_write().await?;
|
||||
let (channel, root, last) = match &request.target {
|
||||
SubscriptionTarget::Channel(name) => {
|
||||
let summary = self.channel_summary(&mut tx, name).await?;
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
//! Permanent board deletion, serialized with writes across all open handles.
|
||||
//!
|
||||
//! Keep only a root ID tombstone so delayed writes cannot resurrect deleted data.
|
||||
|
||||
use super::DATABASE_FILE;
|
||||
use super::LocalAgentMessageBoard;
|
||||
use super::SCHEMA;
|
||||
use super::invalid;
|
||||
use super::storage_error;
|
||||
use codex_protocol::SessionId;
|
||||
use codex_protocol::error::Result;
|
||||
use codex_state::SqliteConfig;
|
||||
use sqlx::Sqlite;
|
||||
use sqlx::Transaction;
|
||||
|
||||
impl LocalAgentMessageBoard {
|
||||
/// Permanently removes boards owned by these roots, including their posts and subscriptions.
|
||||
/// A child's ID does not match its parent's board. Unload and archive must not call this.
|
||||
/// Safe to retry and independent of whether the feature is currently enabled.
|
||||
pub async fn delete_boards(sqlite: &SqliteConfig, roots: &[SessionId]) -> Result<()> {
|
||||
let path = sqlite.home().join(DATABASE_FILE);
|
||||
if roots.is_empty() || !tokio::fs::try_exists(&path).await? {
|
||||
return Ok(());
|
||||
}
|
||||
let pool = sqlite
|
||||
.open_read_write_pool(&path)
|
||||
.await
|
||||
.map_err(storage_error)?;
|
||||
// Also handles databases created before permanent deletion was supported.
|
||||
sqlx::raw_sql(SCHEMA)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.map_err(storage_error)?;
|
||||
let mut tx = pool
|
||||
.begin_with("BEGIN IMMEDIATE")
|
||||
.await
|
||||
.map_err(storage_error)?;
|
||||
for root in roots {
|
||||
sqlx::query("INSERT OR IGNORE INTO deleted_boards (board) VALUES (?)")
|
||||
.bind(root.to_string())
|
||||
.execute(&mut *tx)
|
||||
.await
|
||||
.map_err(storage_error)?;
|
||||
for statement in [
|
||||
"DELETE FROM subscriptions WHERE board=?",
|
||||
"DELETE FROM posts WHERE board=?",
|
||||
"DELETE FROM channels WHERE board=?",
|
||||
] {
|
||||
sqlx::query(statement)
|
||||
.bind(root.to_string())
|
||||
.execute(&mut *tx)
|
||||
.await
|
||||
.map_err(storage_error)?;
|
||||
}
|
||||
}
|
||||
tx.commit().await.map_err(storage_error)?;
|
||||
pool.close().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) async fn begin_write(&self) -> Result<Transaction<'_, Sqlite>> {
|
||||
let mut tx = self
|
||||
.pool
|
||||
.begin_with("BEGIN IMMEDIATE")
|
||||
.await
|
||||
.map_err(storage_error)?;
|
||||
let deleted: bool =
|
||||
sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM deleted_boards WHERE board=?)")
|
||||
.bind(self.identity.to_string())
|
||||
.fetch_one(&mut *tx)
|
||||
.await
|
||||
.map_err(storage_error)?;
|
||||
if deleted {
|
||||
return Err(invalid(
|
||||
"the message board's root has been permanently deleted",
|
||||
));
|
||||
}
|
||||
Ok(tx)
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,8 @@
|
||||
//! Existing rollout files are deleted before this operation reports success. A rollout file that
|
||||
//! vanishes after discovery counts as already deleted. Main state DB rows are deleted after every
|
||||
//! associated rollout is removed, under the same lifecycle lock, so queued artifact mutations cannot
|
||||
//! use deleted thread metadata.
|
||||
//! use deleted thread metadata. Host-owned data is cleaned after reference and writer checks,
|
||||
//! before removing rollouts, so cleanup failures remain retryable even without a state DB.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
@@ -73,6 +74,9 @@ pub(super) async fn delete_thread(
|
||||
let thread_rollouts = ThreadRollouts::from_index(&reference_index, thread_id);
|
||||
ensure_no_external_references(&reference_index, std::slice::from_ref(&thread_rollouts))?;
|
||||
let mut writer_guards = store.acquire_writer_locks(&[thread_id]).await?;
|
||||
if let Some(cleanup) = &store.thread_data_cleanup {
|
||||
cleanup(vec![thread_id]).await?;
|
||||
}
|
||||
let found_rollout =
|
||||
match delete_thread_after_reference_check(store, thread_rollouts, &mut writer_guards).await
|
||||
{
|
||||
@@ -117,6 +121,9 @@ pub(super) async fn delete_threads(
|
||||
ensure_no_external_references(&reference_index, thread_rollouts.as_slice())?;
|
||||
|
||||
let mut writer_guards = store.acquire_writer_locks(&lock_thread_ids).await?;
|
||||
if let Some(cleanup) = &store.thread_data_cleanup {
|
||||
cleanup(thread_ids.clone()).await?;
|
||||
}
|
||||
for thread_rollouts in thread_rollouts {
|
||||
match delete_thread_after_reference_check(store, thread_rollouts, &mut writer_guards).await
|
||||
{
|
||||
|
||||
@@ -142,8 +142,11 @@ pub struct LocalThreadStore {
|
||||
writer_lock_coordinator: Arc<WriterLockCoordinator>,
|
||||
state_db: Option<StateDbHandle>,
|
||||
thread_history_db: Arc<OnceCell<sqlx::SqlitePool>>,
|
||||
thread_data_cleanup: Option<Arc<ThreadDataCleanup>>,
|
||||
}
|
||||
|
||||
type ThreadDataCleanup = dyn Fn(Vec<ThreadId>) -> ThreadStoreFuture<'static, ()> + Send + Sync;
|
||||
|
||||
type WriterLockGuard = Arc<codex_rollout::WriterLockGuard>;
|
||||
|
||||
struct LiveRecorderEntry {
|
||||
@@ -257,9 +260,21 @@ impl LocalThreadStore {
|
||||
writer_lock_coordinator,
|
||||
state_db,
|
||||
thread_history_db: Arc::new(OnceCell::new()),
|
||||
thread_data_cleanup: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds cleanup for host-owned durable data when threads are permanently deleted.
|
||||
/// Runs after reference and writer checks, under the lifecycle locks, before deleting rollouts.
|
||||
/// Cleanup must be idempotent: later deletion steps can fail and the caller may retry.
|
||||
pub fn with_thread_data_cleanup(
|
||||
mut self,
|
||||
cleanup: impl Fn(Vec<ThreadId>) -> ThreadStoreFuture<'static, ()> + Send + Sync + 'static,
|
||||
) -> Self {
|
||||
self.thread_data_cleanup = Some(Arc::new(cleanup));
|
||||
self
|
||||
}
|
||||
|
||||
/// Return the state DB handle used by local rollout writers.
|
||||
pub async fn state_db(&self) -> Option<StateDbHandle> {
|
||||
self.state_db.clone()
|
||||
|
||||
Reference in New Issue
Block a user