Lightweight coding agent that runs in your terminal
Rust
Consolidate local message board storage helpers (#52629)
jif7f2f4fd46e0f
diff --git a/codex-rs/ext/agent-message-board/src/local.rs b/codex-rs/ext/agent-message-board/src/local.rsindex 7e817861a2..335d6279e1 100644--- a/codex-rs/ext/agent-message-board/src/local.rs+++ b/codex-rs/ext/agent-message-board/src/local.rs@@ -34,13 +34,8 @@ use serde::Serialize;use sqlx::Row;use sqlx::SqliteConnection;use sqlx::SqlitePool;-use std::collections::HashMap;use std::collections::HashSet;-use std::path::PathBuf;use std::sync::Arc;-use std::sync::LazyLock;-use std::sync::Weak;-use tokio::sync::Mutex;use uuid::Uuid;mod lifecycle;@@ -56,40 +51,6 @@ mod pools_tests;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";--// Weak entries let the last board handle release its pool. Initialization and-// recovery share one lock so concurrent starts cannot open duplicate or stale pools.-static POOLS: LazyLock<Mutex<HashMap<PathBuf, Weak<SqlitePool>>>> = LazyLock::new(Mutex::default);--const SCHEMA: &str = "-CREATE TABLE IF NOT EXISTS board_templates (board TEXT PRIMARY KEY NOT NULL, template TEXT NOT NULL);-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, description TEXT,- PRIMARY KEY(board,name)-);-CREATE TABLE IF NOT EXISTS posts (- seq INTEGER PRIMARY KEY AUTOINCREMENT,- board TEXT NOT NULL, id TEXT NOT NULL, channel TEXT NOT NULL, root TEXT NOT NULL,- author TEXT NOT NULL, timestamp INTEGER NOT NULL, body_search TEXT NOT NULL,- payload TEXT NOT NULL, request_id TEXT NOT NULL, request TEXT NOT NULL,- UNIQUE(board,id), UNIQUE(board,request_id)-);-CREATE INDEX IF NOT EXISTS posts_board_channel ON posts(board,channel,seq);-CREATE INDEX IF NOT EXISTS posts_board_channel_timestamp ON posts(board,channel,timestamp,seq);-CREATE INDEX IF NOT EXISTS posts_roots_created ON posts(board,channel,timestamp,seq) WHERE id=root;-CREATE INDEX IF NOT EXISTS posts_board_root ON posts(board,root,seq);-CREATE INDEX IF NOT EXISTS posts_board_root_timestamp ON posts(board,root,timestamp,seq);-CREATE INDEX IF NOT EXISTS posts_board_timestamp ON posts(board,timestamp,seq);-CREATE TABLE IF NOT EXISTS subscriptions (- board TEXT NOT NULL, target TEXT NOT NULL, agent TEXT NOT NULL,- PRIMARY KEY(board,target,agent)-);-CREATE TABLE IF NOT EXISTS subscription_opt_outs (- board TEXT NOT NULL, target TEXT NOT NULL, agent TEXT NOT NULL,- PRIMARY KEY(board,target,agent)-);";#[derive(Clone)]pub struct LocalAgentMessageBoard {@@ -128,10 +89,11 @@ impl LocalAgentMessageBoard {let author = self.host.agent_path(caller).await?;let now = self.host.current_time(caller).await?;let mut tx = self.begin_write().await?;- self.insert_channel(+ insert_channel(&mut tx,+ self.identity,&request.channel_name,- request.description.as_ref().map(ChannelDescription::as_str),+ request.description.as_ref(),&author,now,)@@ -216,8 +178,15 @@ impl LocalAgentMessageBoard {}PostDestination::NewChannel(channel) => {validate_channel(channel)?;- self.insert_channel(&mut tx, channel, /*description*/ None, &author, now)- .await?;+ insert_channel(+ &mut tx,+ self.identity,+ channel,+ /*description*/ None,+ &author,+ now,+ )+ .await?;self.subscribe(&mut tx,&SubscriptionTarget::Channel(channel.clone()),@@ -377,23 +346,6 @@ impl LocalAgentMessageBoard {})}- async fn insert_channel(- &self,- conn: &mut SqliteConnection,- name: &str,- description: Option<&str>,- author: &AgentPath,- now: DateTime<Utc>,- ) -> Result<()> {- let inserted = sqlx::query("INSERT OR IGNORE INTO channels(board,name,name_search,created_at,timestamp,author,description) VALUES(?,?,?,?,?,?,?)")- .bind(self.identity.to_string()).bind(name).bind(default_case_fold_str(name)).bind(now.to_rfc3339()).bind(now.timestamp_micros()).bind(author.to_string()).bind(description)- .execute(conn).await.map_err(storage_error)?.rows_affected();- if inserted == 0 {- return Err(invalid("channel already exists"));- }- Ok(())- }-async fn subscribe(&self,conn: &mut SqliteConnection,@@ -478,6 +430,25 @@ impl LocalAgentMessageBoard {}}+// Runtime writes and template setup validate names and board lifecycle before+// inserting within their own transaction.+async fn insert_channel(+ conn: &mut SqliteConnection,+ board: SessionId,+ name: &str,+ description: Option<&ChannelDescription>,+ author: &AgentPath,+ now: DateTime<Utc>,+) -> Result<()> {+ let inserted = sqlx::query("INSERT OR IGNORE INTO channels(board,name,name_search,created_at,timestamp,author,description) VALUES(?,?,?,?,?,?,?)")+ .bind(board.to_string()).bind(name).bind(default_case_fold_str(name)).bind(now.to_rfc3339()).bind(now.timestamp_micros()).bind(author.to_string()).bind(description.map(ChannelDescription::as_str))+ .execute(conn).await.map_err(storage_error)?.rows_affected();+ if inserted == 0 {+ return Err(invalid("channel already exists"));+ }+ Ok(())+}+fn validate_channel(name: &str) -> Result<()> {if name.is_empty()|| name.len() > MAX_CHANNEL_BYTESdiff --git a/codex-rs/ext/agent-message-board/src/local/lifecycle.rs b/codex-rs/ext/agent-message-board/src/local/lifecycle.rsindex f60fd65d5c..8b3daa3196 100644--- a/codex-rs/ext/agent-message-board/src/local/lifecycle.rs+++ b/codex-rs/ext/agent-message-board/src/local/lifecycle.rs@@ -4,11 +4,11 @@//! Recovery closes the cached pool before backing up and recreating the database,//! and excludes new handles until the replacement's deletion tombstones are saved.-use super::DATABASE_FILE;use super::LocalAgentMessageBoard;-use super::POOLS;-use super::SCHEMA;use super::invalid;+use super::pool::DATABASE_FILE;+use super::pool::POOLS;+use super::pool::SCHEMA;use super::storage_error;use codex_protocol::SessionId;use codex_protocol::error::Result;diff --git a/codex-rs/ext/agent-message-board/src/local/pool.rs b/codex-rs/ext/agent-message-board/src/local/pool.rsindex ba4d978a2d..b27417ca74 100644--- a/codex-rs/ext/agent-message-board/src/local/pool.rs+++ b/codex-rs/ext/agent-message-board/src/local/pool.rs@@ -1,14 +1,52 @@-//! Shared SQLite initialization used by runtime handles and local experiment setup.+//! Owns the local database schema and pool registry shared by runtime handles and setup.+//! Initialization and deletion recovery share the registry lock to exclude stale pools.-use super::DATABASE_FILE;-use super::POOLS;-use super::SCHEMA;use super::storage_error;use codex_protocol::error::Result;use codex_state::SqliteConfig;use sqlx::SqlitePool;+use std::collections::HashMap;+use std::path::PathBuf;use std::sync::Arc;+use std::sync::LazyLock;use std::sync::Weak;+use tokio::sync::Mutex;++pub(super) const DATABASE_FILE: &str = "agent_message_board_1.sqlite";++// Weak entries let the last board handle release its pool. Initialization and+// recovery share one lock so concurrent starts cannot open duplicate or stale pools.+pub(super) static POOLS: LazyLock<Mutex<HashMap<PathBuf, Weak<SqlitePool>>>> =+ LazyLock::new(Mutex::default);++pub(super) const SCHEMA: &str = "+CREATE TABLE IF NOT EXISTS board_templates (board TEXT PRIMARY KEY NOT NULL, template TEXT NOT NULL);+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, description TEXT,+ PRIMARY KEY(board,name)+);+CREATE TABLE IF NOT EXISTS posts (+ seq INTEGER PRIMARY KEY AUTOINCREMENT,+ board TEXT NOT NULL, id TEXT NOT NULL, channel TEXT NOT NULL, root TEXT NOT NULL,+ author TEXT NOT NULL, timestamp INTEGER NOT NULL, body_search TEXT NOT NULL,+ payload TEXT NOT NULL, request_id TEXT NOT NULL, request TEXT NOT NULL,+ UNIQUE(board,id), UNIQUE(board,request_id)+);+CREATE INDEX IF NOT EXISTS posts_board_channel ON posts(board,channel,seq);+CREATE INDEX IF NOT EXISTS posts_board_channel_timestamp ON posts(board,channel,timestamp,seq);+CREATE INDEX IF NOT EXISTS posts_roots_created ON posts(board,channel,timestamp,seq) WHERE id=root;+CREATE INDEX IF NOT EXISTS posts_board_root ON posts(board,root,seq);+CREATE INDEX IF NOT EXISTS posts_board_root_timestamp ON posts(board,root,timestamp,seq);+CREATE INDEX IF NOT EXISTS posts_board_timestamp ON posts(board,timestamp,seq);+CREATE TABLE IF NOT EXISTS subscriptions (+ board TEXT NOT NULL, target TEXT NOT NULL, agent TEXT NOT NULL,+ PRIMARY KEY(board,target,agent)+);+CREATE TABLE IF NOT EXISTS subscription_opt_outs (+ board TEXT NOT NULL, target TEXT NOT NULL, agent TEXT NOT NULL,+ PRIMARY KEY(board,target,agent)+);";#[expect(clippy::await_holding_invalid_type,diff --git a/codex-rs/ext/agent-message-board/src/local/setup.rs b/codex-rs/ext/agent-message-board/src/local/setup.rsindex eca2410978..b767a4f38d 100644--- a/codex-rs/ext/agent-message-board/src/local/setup.rs+++ b/codex-rs/ext/agent-message-board/src/local/setup.rs@@ -1,11 +1,13 @@//! Loads initial channels into an empty local board and pins their source template.+use super::insert_channel;use super::invalid;use super::storage_error;use super::validate_channel;use crate::ChannelDescription;use chrono::DateTime;use chrono::Utc;+use codex_protocol::AgentPath;use codex_protocol::SessionId;use codex_protocol::error::Result;use codex_state::SqliteConfig;@@ -59,10 +61,10 @@ pub async fn configure_local_board(.begin_with("BEGIN IMMEDIATE").await.map_err(storage_error)?;- let board = board.to_string();+ let board_key = board.to_string();let deleted: bool =sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM deleted_boards WHERE board=?)")- .bind(&board)+ .bind(&board_key).fetch_one(&mut *tx).await.map_err(storage_error)?;@@ -71,7 +73,7 @@ pub async fn configure_local_board(}let pinned: Option<String> =sqlx::query_scalar("SELECT template FROM board_templates WHERE board=?")- .bind(&board)+ .bind(&board_key).fetch_optional(&mut *tx).await.map_err(storage_error)?;@@ -81,7 +83,7 @@ pub async fn configure_local_board(}} else {let used: bool = sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM channels WHERE board=?)")- .bind(&board)+ .bind(&board_key).fetch_one(&mut *tx).await.map_err(storage_error)?;@@ -91,18 +93,22 @@ pub async fn configure_local_board());}sqlx::query("INSERT INTO board_templates(board,template) VALUES(?,?)")- .bind(&board)+ .bind(&board_key).bind(encoded).execute(&mut *tx).await.map_err(storage_error)?;let now = setup.timestamp.unwrap_or_else(Utc::now);for (name, channel) in &template.channels {- sqlx::query("INSERT INTO channels(board,name,name_search,created_at,timestamp,author,description) VALUES(?,?,?,?,?,'/root',?)")- .bind(&board).bind(name).bind(caseless::default_case_fold_str(name))- .bind(now.to_rfc3339()).bind(now.timestamp_micros())- .bind(channel.description.as_ref().map(ChannelDescription::as_str))- .execute(&mut *tx).await.map_err(storage_error)?;+ insert_channel(+ &mut tx,+ board,+ name,+ channel.description.as_ref(),+ &AgentPath::root(),+ now,+ )+ .await?;}}tx.commit().await.map_err(storage_error)