Remove fixed message-board views and bulk subscriptions (#62137)

## Why

Guardian v3 reviewers can retrieve evidence from the live board, including
posts that arrive after a review begins. Fixed board views and bulk
subscription updates have no remaining callers outside tests.
https://github.com/openai/codex/commit/c2595ad3438311ce3070b1a7eef05cd0da8ad631

## What changed

Remove `BoardReadView`, its snapshot-specific tool bindings, and the
captured catalog and post cutoff from local board queries. Remove
`set_subscriptions` and keep individual subscription changes through
`set_subscription`.

Update the Guardian housekeeping test to check channel descriptions
through `list_channels`, and store captured Guardian evidence in an
owned vector.

GitOrigin-RevId: ede9e48abf4d009a2b4a82a437851657e41cf009
This commit is contained in:
felixxia-oai
2026-11-18 00:03:53 +00:00
committed by copyberry
parent 9e3681977b
commit 44de9d99ef
15 changed files with 80 additions and 963 deletions
+2 -5
View File
@@ -20,7 +20,6 @@ use codex_thread_store::ThreadStoreResult;
use codex_utils_string::to_json_string_bounded;
use serde::Deserialize;
use serde::Serialize;
use std::sync::Arc;
/// Position in the containing snapshot's root history, independent of channel routing.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
@@ -50,13 +49,11 @@ pub struct OriginalEvidence {
}
/// An immutable capture. Later history appends cannot enter its reads or routing.
/// Clones share the captured records rather than copying message payloads.
#[derive(Clone)]
pub struct EvidenceSnapshot {
thread_id: ThreadId,
start: HistoryPosition,
end: HistoryPosition,
records: Arc<[OriginalEvidence]>,
records: Vec<OriginalEvidence>,
// Report these positions only when the intake cursor accepts this capture.
pub(super) skipped_incomplete_deliveries: Vec<u64>,
}
@@ -179,7 +176,7 @@ impl EvidenceCursor {
thread_id: self.thread_id,
start: self.position,
end,
records: records.into(),
records,
skipped_incomplete_deliveries,
})
}
+13 -3
View File
@@ -463,9 +463,19 @@ async fn routes_captured_originals_and_updates_descriptions(
json!({"catalog":catalog,"sources":[source]})
);
}
let view = board.read_view(owner).await?;
let release = view
.channels()
let catalog = board
.list_channels(
owner,
ChannelQuery {
query: Some("release".into()),
direction: SortDirection::OldestFirst,
page: PageRequest::default(),
},
)
.await?;
let release = catalog
.channels
.results
.iter()
.find(|channel| channel.channel_name == "release");
assert_eq!(
-17
View File
@@ -1,7 +1,6 @@
//! Operations on one board. Requests describe intent independently of tool schemas.
//! Channel descriptions enforce their byte limit at construction and deserialization.
use crate::BoardReadView;
use crate::ChannelPage;
use crate::ChannelSummary;
use crate::Page;
@@ -19,7 +18,6 @@ use codex_protocol::error::Result;
use futures::future::BoxFuture;
use serde::Deserialize;
use serde::Serialize;
use std::collections::BTreeMap;
use std::num::NonZeroU32;
use std::sync::Arc;
use uuid::Uuid;
@@ -55,11 +53,6 @@ pub trait AgentMessageBoard: Send + Sync {
))
}
/// Capture a live board's channel summaries and post boundary together for read-only access.
fn read_view(&self, _caller: ThreadId) -> BoxFuture<'_, Result<Arc<dyn BoardReadView>>> {
unsupported("capturing a board read view")
}
fn create_channel(
&self,
caller: ThreadId,
@@ -158,16 +151,6 @@ pub trait AgentMessageBoard: Send + Sync {
) -> BoxFuture<'_, Result<Page<String>>> {
unsupported("listing channel subscriptions")
}
/// Set the caller's membership in the supplied channels as one atomic change.
/// Other channels and thread subscriptions retain their current membership.
fn set_subscriptions<'a>(
&'a self,
_caller: ThreadId,
_channels: &'a BTreeMap<&str, SubscriptionChange>,
) -> BoxFuture<'a, Result<()>> {
unsupported("setting channel subscriptions in bulk")
}
}
fn unsupported<T>(operation: &'static str) -> BoxFuture<'static, Result<T>> {
-3
View File
@@ -8,7 +8,6 @@ mod host;
mod in_memory;
mod local;
mod permissions;
mod read_view;
mod tools;
mod types;
@@ -43,10 +42,8 @@ pub use local::setup::configure_local_board;
pub use permissions::BoardPermissions;
pub use permissions::ChannelPermissions;
pub use permissions::PermissionDenied;
pub use read_view::BoardReadView;
pub use tools::MAX_TOOL_ARGUMENT_KIB;
pub use tools::MAX_TOOL_RESPONSE_BYTES;
pub use tools::message_board_read_tools;
pub use tools::message_board_tools;
pub use tools::selection::MessageBoardTool;
pub use types::ChannelPage;
+5 -31
View File
@@ -6,7 +6,6 @@
//! a board after its root has been permanently deleted.
//! Opening storage adds optional channel metadata without invalidating older readers.
//! Explicit connections can restrict writes; default handles retain legacy access.
//! Fixed read views freeze the channel catalog and restrict reads to a captured post sequence.
//! Posting subscribes and notifies; host appends share persistence without these side effects.
use self::connection::Access;
@@ -62,38 +61,13 @@ const MAX_POST_BYTES: usize = 64 * 1024;
const MAX_CHANNEL_BYTES: usize = 128;
const MAX_READ_CHARS: usize = 20_000;
/// Capturing a view makes the handle read-only, regardless of its original access.
#[derive(Clone)]
enum BoardMode {
Live { access: Option<Access> },
Fixed { view: Arc<catalog::ReadView> },
}
impl BoardMode {
const FIXED_ACCESS: Access = Access::ReadOnly;
fn access(&self) -> Option<Access> {
match self {
Self::Live { access } => *access,
Self::Fixed { .. } => Some(Self::FIXED_ACCESS),
}
}
fn view(&self) -> Option<&catalog::ReadView> {
match self {
Self::Live { .. } => None,
Self::Fixed { view } => Some(view),
}
}
}
#[derive(Clone)]
pub struct LocalAgentMessageBoard {
identity: SessionId,
storage_namespace: Arc<str>,
pool: Arc<SqlitePool>,
host: Arc<dyn MessageBoardHost>,
mode: BoardMode,
access: Option<Access>,
nested_json_pointer: Option<&'static str>,
}
@@ -118,7 +92,7 @@ impl LocalAgentMessageBoard {
pool: pool::open(sqlite, options).await?,
storage_namespace: Arc::from(options.storage_namespace.as_str()),
host,
mode: BoardMode::Live { access: None },
access: None,
nested_json_pointer: None,
})
}
@@ -128,7 +102,7 @@ impl LocalAgentMessageBoard {
caller: ThreadId,
request: CreateChannelRequest,
) -> Result<ChannelSummary> {
if let Some(access) = self.mode.access() {
if let Some(access) = self.access {
access.require_write(self.identity, "board.create_channel")?;
}
validate_channel(&request.channel_name)?;
@@ -203,7 +177,7 @@ impl LocalAgentMessageBoard {
request: PostRequest,
behavior: PostBehavior,
) -> Result<(PostMetadata, Vec<ThreadId>)> {
if let Some(access) = self.mode.access() {
if let Some(access) = self.access {
access.require_write(
self.identity,
match &request.destination {
@@ -392,7 +366,7 @@ impl LocalAgentMessageBoard {
let target_path = request.target_agent.unwrap_or(caller_path);
let target_agent = self.host.resolve_agent(target_path.clone()).await?;
if target_agent != caller
&& let Some(access) = self.mode.access()
&& let Some(access) = self.access
{
access.require_write(self.identity, "board.manage_subscriptions")?;
}
+2 -223
View File
@@ -1,236 +1,19 @@
//! Channel summaries and fixed read views. Capture only catalog metadata and a
//! monotonically increasing post boundary; payload reads continue to use SQLite.
//! Read channel metadata and permissions for a local board.
use super::BoardMode;
use super::LocalAgentMessageBoard;
use super::connection::Access;
use super::invalid;
use super::paging::Window;
use super::storage_error;
use crate::AgentMessageBoard;
use crate::BoardPermissions;
use crate::BoardReadView;
use crate::ChannelPage;
use crate::ChannelPermissions;
use crate::ChannelQuery;
use crate::ChannelSummary;
use crate::Page;
use crate::PostContent;
use crate::PostPreview;
use crate::PostQuery;
use crate::ReadPostRequest;
use crate::ReadThreadRequest;
use crate::SortDirection;
use crate::ThreadPage;
use crate::ThreadQuery;
use crate::ThreadSummary;
use caseless::default_case_fold_str;
use chrono::DateTime;
use chrono::Utc;
use codex_protocol::AgentPath;
use codex_protocol::SessionId;
use codex_protocol::ThreadId;
use codex_protocol::error::Result;
use futures::future::BoxFuture;
use sqlx::Row;
use sqlx::SqliteConnection;
use sqlx::sqlite::SqliteRow;
use std::sync::Arc;
use uuid::Uuid;
/// The captured catalog and the read-only SQLite handle that enforces its post boundary.
struct LocalBoardReadView {
board: LocalAgentMessageBoard,
view: Arc<ReadView>,
}
impl BoardReadView for LocalBoardReadView {
fn identity(&self) -> SessionId {
self.board.identity
}
fn with_json_search(&self, pointer: &'static str) -> Arc<dyn BoardReadView> {
let mut board = self.board.clone();
board.nested_json_pointer = Some(pointer);
Arc::new(Self {
board,
view: Arc::clone(&self.view),
})
}
fn storage_namespace(&self) -> &str {
&self.board.storage_namespace
}
fn channels(&self) -> &[ChannelSummary] {
&self.view.channels
}
fn list_channels(
&self,
caller: ThreadId,
query: ChannelQuery,
) -> BoxFuture<'_, Result<ChannelPage>> {
self.board.list_channels(caller, query)
}
fn list_threads(
&self,
caller: ThreadId,
query: ThreadQuery,
) -> BoxFuture<'_, Result<Page<ThreadSummary>>> {
self.board.list_threads(caller, query)
}
fn search_posts(
&self,
caller: ThreadId,
query: PostQuery,
) -> BoxFuture<'_, Result<Page<PostPreview>>> {
self.board.search_posts(caller, query)
}
fn read_thread(
&self,
caller: ThreadId,
request: ReadThreadRequest,
) -> BoxFuture<'_, Result<ThreadPage>> {
self.board.read_thread(caller, request)
}
fn read_post(
&self,
caller: ThreadId,
request: ReadPostRequest,
) -> BoxFuture<'_, Result<PostContent>> {
AgentMessageBoard::read_post(&self.board, caller, request)
}
fn appended_request_ids<'a>(
&'a self,
caller: ThreadId,
channel: &'a str,
after: i64,
) -> BoxFuture<'a, Result<Vec<(i64, String)>>> {
Box::pin(async move {
self.board.host.agent_path(caller).await?;
if after < 0 || after > self.view.post_cutoff {
return Err(invalid("post cursor is outside the captured board view"));
}
// The persisted retry key prefixes the caller-supplied request ID with "{caller}:".
sqlx::query_as(
"SELECT seq, substr(request_id, instr(request_id, ':') + 1) FROM posts
WHERE board=? AND channel=? AND seq>? AND seq<=? ORDER BY seq LIMIT ?",
)
.bind(self.board.identity.to_string())
.bind(channel)
.bind(after)
.bind(self.view.post_cutoff)
.bind(i64::from(crate::PageRequest::default().limit.get()))
.fetch_all(self.board.pool.as_ref())
.await
.map_err(storage_error)
})
}
fn read_post_texts<'a>(
&'a self,
caller: ThreadId,
positions: &'a [i64],
) -> BoxFuture<'a, Result<Vec<String>>> {
Box::pin(async move {
self.board.host.agent_path(caller).await?;
if positions.len() > crate::PageRequest::default().limit.get() as usize {
return Err(invalid("requested posts exceed one board page"));
}
// Drive primary-key lookups from the requested page, not from all posts on the board.
let texts = sqlx::query_scalar::<_, String>(
"SELECT json_extract(p.payload, '$.text') FROM json_each(?) AS requested
CROSS JOIN posts AS p
WHERE p.seq=requested.value AND p.board=? AND p.seq<=? ORDER BY requested.key",
)
.bind(serde_json::to_string(positions).map_err(storage_error)?)
.bind(self.board.identity.to_string())
.bind(self.view.post_cutoff)
.fetch_all(self.board.pool.as_ref())
.await
.map_err(storage_error)?;
if texts.len() != positions.len() {
return Err(invalid("post not found in this board view"));
}
Ok(texts)
})
}
}
pub(super) struct ReadView {
pub(super) post_cutoff: i64,
pub(super) channels: Vec<ChannelSummary>,
}
impl ReadView {
pub(super) fn list_channels(&self, query: ChannelQuery) -> Result<ChannelPage> {
let window = Window::new(&query.page)?;
let needle = default_case_fold_str(&query.query.unwrap_or_default());
let ordered: Box<dyn Iterator<Item = &ChannelSummary>> = match query.direction {
SortDirection::NewestFirst => Box::new(self.channels.iter()),
SortDirection::OldestFirst => Box::new(self.channels.iter().rev()),
};
let channels = ordered
.filter(|channel| default_case_fold_str(&channel.channel_name).contains(&needle))
.skip(window.offset() as usize)
.take(window.limit + 1)
.cloned()
.collect();
Ok(ChannelPage {
channels: window.finish(channels)?,
board_permissions: Some(BoardPermissions {
create_channel: false,
edit_metadata: false,
manage_permissions: false,
}),
})
}
}
pub(super) async fn capture(board: &LocalAgentMessageBoard) -> Result<Arc<dyn BoardReadView>> {
let mut tx = board.pool.begin().await.map_err(storage_error)?;
// seq is the database-wide AUTOINCREMENT key. Every future board append is above it.
let post_cutoff = sqlx::query_scalar::<_, i64>("SELECT COALESCE(MAX(seq),0) FROM posts")
.fetch_one(&mut *tx)
.await
.map_err(storage_error)?;
let rows = sqlx::query(
"SELECT c.name,c.created_at,c.author,c.description,last.id AS last_message_id,
(SELECT COUNT(*) FROM posts p WHERE p.board=c.board AND p.channel=c.name) AS message_count
FROM channels c LEFT JOIN posts last ON last.seq=(
SELECT p.seq FROM posts p WHERE p.board=c.board AND p.channel=c.name
ORDER BY p.timestamp DESC,p.seq DESC LIMIT 1
)
WHERE c.board=? ORDER BY COALESCE(last.timestamp,c.timestamp) DESC,c.name DESC",
)
.bind(board.identity.to_string())
.fetch_all(&mut *tx)
.await
.map_err(storage_error)?;
tx.commit().await.map_err(storage_error)?;
let channels = rows
.into_iter()
.map(|row| decode_channel_summary(Some(BoardMode::FIXED_ACCESS), row))
.collect::<Result<Vec<_>>>()?;
let view = Arc::new(ReadView {
post_cutoff,
channels,
});
let board = LocalAgentMessageBoard {
mode: BoardMode::Fixed {
view: Arc::clone(&view),
},
..board.clone()
};
Ok(Arc::new(LocalBoardReadView { board, view }))
}
pub(super) async fn channel_summary(
board: &LocalAgentMessageBoard,
conn: &mut SqliteConnection,
@@ -248,12 +31,8 @@ pub(super) async fn channel_summary(
.await
.map_err(storage_error)?
.ok_or_else(|| invalid("channel not found in this board"))?;
decode_channel_summary(board.mode.access(), row)
}
fn decode_channel_summary(access: Option<Access>, row: SqliteRow) -> Result<ChannelSummary> {
Ok(ChannelSummary {
permissions: access.map(|access| {
permissions: board.access.map(|access| {
let write = matches!(access, Access::ReadWrite);
ChannelPermissions {
read: true,
@@ -1,7 +1,6 @@
//! Host-supplied connections to one local board. Access is fixed per runtime,
//! independent of the board's storage or the agent's tree identity.
use super::BoardMode;
use super::LocalAgentMessageBoard;
use super::options::LocalBoardOptions;
use crate::MessageBoardHost;
@@ -104,9 +103,7 @@ impl LocalBoardConnection {
"board database changed; create a new board connection".into(),
));
}
board.mode = BoardMode::Live {
access: Some(self.access),
};
board.access = Some(self.access);
Ok(board)
}
}
@@ -1,4 +1,4 @@
//! Literal search over decoded JSON fields in a fixed read view.
//! Literal search over decoded JSON fields in board posts.
use super::StoredPost;
use super::paging::Window;
+1 -2
View File
@@ -1,5 +1,4 @@
//! Bounded offset pagination. Concurrent posts may shift live results between pages;
//! fixed read views exclude later appends from every page.
//! Bounded offset pagination. Concurrent posts may shift results between pages.
//! Cursors belong to the original query; restart without one to refresh the listing.
use super::StoredPost;
+35 -121
View File
@@ -16,7 +16,6 @@ use super::validate_channel;
use crate::AgentMessageBoard;
use crate::AppendRequest;
use crate::BoardPermissions;
use crate::BoardReadView;
use crate::ChannelPage;
use crate::ChannelQuery;
use crate::ChannelSummary;
@@ -30,7 +29,6 @@ use crate::PostQuery;
use crate::PostRequest;
use crate::ReadPostRequest;
use crate::ReadThreadRequest;
use crate::SubscriptionChange;
use crate::SubscriptionRequest;
use crate::SubscriptionState;
use crate::ThreadPage;
@@ -45,7 +43,6 @@ use codex_protocol::error::Result;
use futures::future::BoxFuture;
use sqlx::QueryBuilder;
use sqlx::SqliteConnection;
use std::collections::BTreeMap;
use std::sync::Arc;
use uuid::Uuid;
@@ -55,18 +52,13 @@ impl LocalAgentMessageBoard {
conn: &mut SqliteConnection,
id: Uuid,
) -> Result<StoredPost> {
let query = if let Some(view) = self.mode.view() {
sqlx::query_scalar("SELECT payload FROM posts WHERE seq<=? AND board=? AND id=?")
.bind(view.post_cutoff)
} else {
let payload: Option<String> =
sqlx::query_scalar("SELECT payload FROM posts WHERE board=? AND id=?")
};
let payload: Option<String> = query
.bind(self.identity.to_string())
.bind(id.to_string())
.fetch_optional(conn)
.await
.map_err(storage_error)?;
.bind(self.identity.to_string())
.bind(id.to_string())
.fetch_optional(conn)
.await
.map_err(storage_error)?;
serde_json::from_str(&payload.ok_or_else(|| invalid("post not found in this board"))?)
.map_err(storage_error)
}
@@ -103,7 +95,7 @@ impl LocalAgentMessageBoard {
caller: ThreadId,
request: UpsertChannelRequest,
) -> Result<()> {
if let Some(access) = self.mode.access() {
if let Some(access) = self.access {
access.require_write(self.identity, "channel.edit_metadata")?;
}
let UpsertChannelRequest {
@@ -178,13 +170,6 @@ impl AgentMessageBoard for LocalAgentMessageBoard {
}))
}
fn read_view(&self, caller: ThreadId) -> BoxFuture<'_, Result<Arc<dyn BoardReadView>>> {
Box::pin(async move {
self.host.agent_path(caller).await?;
catalog::capture(self).await
})
}
fn upsert_channel(
&self,
caller: ThreadId,
@@ -249,14 +234,6 @@ impl AgentMessageBoard for LocalAgentMessageBoard {
))
}
fn set_subscriptions<'a>(
&'a self,
caller: ThreadId,
channels: &'a BTreeMap<&str, SubscriptionChange>,
) -> BoxFuture<'a, Result<()>> {
Box::pin(self.apply_subscriptions(caller, channels))
}
fn read_post_texts_by_id<'a>(
&'a self,
caller: ThreadId,
@@ -269,12 +246,11 @@ impl AgentMessageBoard for LocalAgentMessageBoard {
}
let texts = sqlx::query_scalar::<_, String>(
"SELECT json_extract(p.payload, '$.text') FROM json_each(?) AS requested
CROSS JOIN posts AS p WHERE p.board=? AND p.id=requested.value AND p.seq<=?
CROSS JOIN posts AS p WHERE p.board=? AND p.id=requested.value
ORDER BY requested.key",
)
.bind(serde_json::to_string(ids).map_err(storage_error)?)
.bind(self.identity.to_string())
.bind(self.mode.view().map_or(i64::MAX, |view| view.post_cutoff))
.fetch_all(self.pool.as_ref())
.await
.map_err(storage_error)?;
@@ -300,9 +276,6 @@ impl AgentMessageBoard for LocalAgentMessageBoard {
) -> BoxFuture<'_, Result<ChannelPage>> {
Box::pin(async move {
self.host.agent_path(caller).await?;
if let Some(view) = self.mode.view() {
return view.list_channels(query);
}
let mut tx = self.pool.begin().await.map_err(storage_error)?;
let window = Window::new(&query.page)?;
let order = direction(query.direction);
@@ -333,7 +306,7 @@ impl AgentMessageBoard for LocalAgentMessageBoard {
}
Ok(ChannelPage {
channels: window.finish(channels)?,
board_permissions: self.mode.access().map(|access| BoardPermissions {
board_permissions: self.access.map(|access| BoardPermissions {
create_channel: matches!(access, Access::ReadWrite),
edit_metadata: false,
manage_permissions: false,
@@ -358,50 +331,25 @@ impl AgentMessageBoard for LocalAgentMessageBoard {
.fetch_one(&mut *tx)
.await
.map_err(storage_error)?;
if !exists
|| self.mode.view().is_some_and(|view| {
!view
.channels
.iter()
.any(|channel| channel.channel_name == query.channel_name)
})
{
if !exists {
return Err(invalid("channel not found in this board"));
}
let window = Window::new(&query.page)?;
let order = direction(query.direction);
// Select the page before looking up reply summaries, especially when
// activity sorting examines more roots than the page will return.
let mut sql = QueryBuilder::new("WITH ");
let (roots, by_time, for_count) = if let Some(view) = self.mode.view() {
// A sequence cutoff can make SQLite scan and sort a whole channel or
// thread. Keep the ordered indexes for roots and last activity, and
// allow the reply count to use the sequence range.
sql.push("visible_roots AS NOT MATERIALIZED (SELECT * FROM posts INDEXED BY posts_roots_created WHERE id=root AND seq<=")
.push_bind(view.post_cutoff)
.push("), visible_by_time AS NOT MATERIALIZED (SELECT * FROM posts INDEXED BY posts_board_root_timestamp WHERE seq<=")
.push_bind(view.post_cutoff)
.push("), visible_posts AS NOT MATERIALIZED (SELECT * FROM posts WHERE seq<=")
.push_bind(view.post_cutoff)
.push("), ");
("visible_roots", "visible_by_time", "visible_posts")
} else {
("posts", "posts", "posts")
};
sql.push("page AS MATERIALIZED (SELECT p.board,p.id,p.payload,p.seq,");
let mut sql = QueryBuilder::new(
"WITH page AS MATERIALIZED (SELECT p.board,p.id,p.payload,p.seq,",
);
match query.sort {
ThreadSort::Created => {
sql.push("p.timestamp");
}
ThreadSort::Activity => {
sql.push("(SELECT MAX(r.timestamp) FROM ")
.push(by_time)
.push(" r WHERE r.board=p.board AND r.root=p.id)");
sql.push("(SELECT MAX(r.timestamp) FROM posts r WHERE r.board=p.board AND r.root=p.id)");
}
}
sql.push(" AS sort_timestamp FROM ")
.push(roots)
.push(" p WHERE p.board=")
sql.push(" AS sort_timestamp FROM posts p WHERE p.board=")
.push_bind(self.identity.to_string())
.push(" AND p.channel=")
.push_bind(query.channel_name)
@@ -415,16 +363,8 @@ impl AgentMessageBoard for LocalAgentMessageBoard {
.push_bind(window.offset())
.push(
") SELECT p.payload,
(SELECT COUNT(*) FROM ",
)
.push(for_count)
.push(
" r WHERE r.board=p.board AND r.root=p.id AND r.id<>r.root),
(SELECT r.payload FROM ",
)
.push(by_time)
.push(
" r WHERE r.board=p.board AND r.root=p.id AND r.id<>r.root
(SELECT COUNT(*) FROM posts r WHERE r.board=p.board AND r.root=p.id AND r.id<>r.root),
(SELECT r.payload FROM posts r WHERE r.board=p.board AND r.root=p.id AND r.id<>r.root
ORDER BY r.timestamp DESC,r.seq DESC LIMIT 1)
FROM page p ORDER BY p.sort_timestamp ",
)
@@ -471,38 +411,22 @@ impl AgentMessageBoard for LocalAgentMessageBoard {
let mut tx = self.pool.begin().await.map_err(storage_error)?;
let window = Window::new(&query.page)?;
let after = if let Some(id) = query.after_message_id {
let lookup = if let Some(view) = self.mode.view() {
sqlx::query_as::<_, (i64, i64)>(
"SELECT timestamp,seq FROM posts WHERE seq<=? AND board=? AND id=?",
)
.bind(view.post_cutoff)
} else {
sqlx::query_as("SELECT timestamp,seq FROM posts WHERE board=? AND id=?")
};
Some(
lookup
.bind(self.identity.to_string())
.bind(id.to_string())
.fetch_optional(&mut *tx)
.await
.map_err(storage_error)?
.ok_or_else(|| invalid("post not found in this board"))?,
sqlx::query_as::<_, (i64, i64)>(
"SELECT timestamp,seq FROM posts WHERE board=? AND id=?",
)
.bind(self.identity.to_string())
.bind(id.to_string())
.fetch_optional(&mut *tx)
.await
.map_err(storage_error)?
.ok_or_else(|| invalid("post not found in this board"))?,
)
} else {
None
};
// For a frozen channel page, scan in result order. Otherwise SQLite can
// choose the sequence index for the cutoff and sort the entire channel.
let select = if self.mode.view().is_some() && query.channel_name.is_some() {
"SELECT payload FROM posts INDEXED BY posts_board_channel_timestamp WHERE board="
} else {
"SELECT payload FROM posts WHERE board="
};
let mut sql = QueryBuilder::new(select);
let mut sql = QueryBuilder::new("SELECT payload FROM posts WHERE board=");
sql.push_bind(self.identity.to_string());
if let Some(view) = self.mode.view() {
sql.push(" AND seq<=").push_bind(view.post_cutoff);
}
if let Some(channel) = query.channel_name {
sql.push(" AND channel=").push_bind(channel);
}
@@ -558,28 +482,18 @@ impl AgentMessageBoard for LocalAgentMessageBoard {
return Err(invalid("thread_id must identify a top-level post"));
}
let window = Window::new(&request.page)?;
let replies = if let Some(view) = self.mode.view() {
sqlx::query_scalar(
"SELECT payload FROM posts INDEXED BY posts_board_root_timestamp
WHERE seq<=? AND board=? AND root=? AND id<>root
ORDER BY timestamp DESC,seq DESC LIMIT ? OFFSET ?",
)
.bind(view.post_cutoff)
} else {
let posts = decode_posts(
sqlx::query_scalar(
"SELECT payload FROM posts WHERE board=? AND root=? AND id<>root
ORDER BY timestamp DESC,seq DESC LIMIT ? OFFSET ?",
)
};
let posts = decode_posts(
replies
.bind(self.identity.to_string())
.bind(request.thread_id.to_string())
.bind((window.limit + 1) as i64)
.bind(window.offset())
.fetch_all(&mut *tx)
.await
.map_err(storage_error)?,
.bind(self.identity.to_string())
.bind(request.thread_id.to_string())
.bind((window.limit + 1) as i64)
.bind(window.offset())
.fetch_all(&mut *tx)
.await
.map_err(storage_error)?,
)?;
let chars = (request.max_chars_per_post.get() as usize)
.min(MAX_READ_CHARS / (posts.len().min(window.limit) + 1));
@@ -1,61 +1,16 @@
//! Atomic replacement and discovery of a caller's own channel subscriptions.
//! Thread subscriptions and other agents are unaffected; no notifications are emitted.
//! Update explicit membership and its opt-out together for one subscription target.
use super::LocalAgentMessageBoard;
use super::invalid;
use super::storage_error;
use super::target_key;
use super::validate_channel;
use crate::SubscriptionChange;
use crate::SubscriptionTarget;
use codex_protocol::ThreadId;
use codex_protocol::error::Result;
use sqlx::Sqlite;
use sqlx::Transaction;
use std::collections::BTreeMap;
impl LocalAgentMessageBoard {
/// Atomically replaces the caller's membership in the supplied channels.
/// Channels outside this set keep their subscriptions and opt-outs.
/// A read-only live connection permits this self-only operation.
pub(super) async fn apply_subscriptions(
&self,
caller: ThreadId,
channels: &BTreeMap<&str, SubscriptionChange>,
) -> Result<()> {
self.host.agent_path(caller).await?;
for channel in channels.keys() {
validate_channel(channel)?;
}
let names =
serde_json::to_string(&channels.keys().collect::<Vec<_>>()).map_err(storage_error)?;
let mut tx = self.begin_write().await?;
let membership = sqlx::query_as::<_, (String, bool)>(
"SELECT c.name, EXISTS(
SELECT 1 FROM subscriptions AS s WHERE s.board=c.board
AND s.target=json_object('Channel',c.name) AND s.agent=?
) FROM channels AS c WHERE c.board=? AND c.name IN (SELECT value FROM json_each(?))",
)
.bind(caller.to_string())
.bind(self.identity.to_string())
.bind(names)
.fetch_all(&mut *tx)
.await
.map_err(storage_error)?;
if membership.len() != channels.len() {
return Err(invalid("channel not found in this board"));
}
for (name, subscribed) in membership {
let change = channels[name.as_str()];
if subscribed == (change == SubscriptionChange::Subscribe) {
continue;
}
self.change_subscription(&mut tx, &SubscriptionTarget::Channel(name), caller, change)
.await?;
}
tx.commit().await.map_err(storage_error)
}
/// Update explicit membership and its matching implicit-subscription opt-out atomically.
pub(super) async fn change_subscription(
&self,
-77
View File
@@ -1,77 +0,0 @@
//! Read-only board snapshots with a stable channel catalog and post cutoff.
use crate::ChannelPage;
use crate::ChannelQuery;
use crate::ChannelSummary;
use crate::Page;
use crate::PostContent;
use crate::PostPreview;
use crate::PostQuery;
use crate::ReadPostRequest;
use crate::ReadThreadRequest;
use crate::ThreadPage;
use crate::ThreadQuery;
use crate::ThreadSummary;
use codex_protocol::SessionId;
use codex_protocol::ThreadId;
use codex_protocol::error::Result;
use futures::future::BoxFuture;
use std::sync::Arc;
/// A read-only capture of one board. All handles share its namespace, catalog and post cutoff.
/// Later appends and description edits are invisible. Deletion can make captured posts unavailable.
pub trait BoardReadView: Send + Sync {
fn identity(&self) -> SessionId;
fn storage_namespace(&self) -> &str;
fn channels(&self) -> &[ChannelSummary];
/// Keep the capture and search literal values in JSON and in the string-encoded JSON at this pointer.
fn with_json_search(&self, pointer: &'static str) -> Arc<dyn BoardReadView>;
fn list_channels(
&self,
caller: ThreadId,
query: ChannelQuery,
) -> BoxFuture<'_, Result<ChannelPage>>;
fn list_threads(
&self,
caller: ThreadId,
query: ThreadQuery,
) -> BoxFuture<'_, Result<Page<ThreadSummary>>>;
fn search_posts(
&self,
caller: ThreadId,
query: PostQuery,
) -> BoxFuture<'_, Result<Page<PostPreview>>>;
fn read_thread(
&self,
caller: ThreadId,
request: ReadThreadRequest,
) -> BoxFuture<'_, Result<ThreadPage>>;
fn read_post(
&self,
caller: ThreadId,
request: ReadPostRequest,
) -> BoxFuture<'_, Result<PostContent>>;
/// Return one page after the given insertion position, in ascending order.
/// Zero precedes the first post. Positions stay valid across captures of the same board.
/// A page shorter than the default board page size exhausts the captured channel.
fn appended_request_ids<'a>(
&'a self,
caller: ThreadId,
channel: &'a str,
after: i64,
) -> BoxFuture<'a, Result<Vec<(i64, String)>>>;
/// Return whole post bodies for one page in the requested order, within this capture.
fn read_post_texts<'a>(
&'a self,
caller: ThreadId,
positions: &'a [i64],
) -> BoxFuture<'a, Result<Vec<String>>>;
}
+19 -87
View File
@@ -7,7 +7,6 @@ mod spec;
use self::selection::MessageBoardTool;
use crate::AgentMessageBoard;
use crate::AppendRequest;
use crate::BoardReadView;
use crate::ChannelQuery;
use crate::CreateChannelRequest;
use crate::PageRequest;
@@ -58,46 +57,6 @@ pub fn message_board_tools(
namespace: Option<&str>,
namespace_description: &str,
tool_messages: Option<&MultiAgentToolMessages>,
) -> Vec<Arc<dyn for<'call> ToolExecutor<ToolCall<'call>>>> {
bind_tools(
tools,
ToolTarget::Live(board),
caller,
caller_path,
namespace,
namespace_description,
tool_messages,
)
}
/// Creates the channel, thread, search and post read tools for one captured view.
pub fn message_board_read_tools(
view: Arc<dyn BoardReadView>,
caller: ThreadId,
caller_path: AgentPath,
namespace: Option<&str>,
namespace_description: &str,
tool_messages: Option<&MultiAgentToolMessages>,
) -> Vec<Arc<dyn for<'call> ToolExecutor<ToolCall<'call>>>> {
bind_tools(
&MessageBoardTool::READ_ONLY,
ToolTarget::Fixed(view),
caller,
caller_path,
namespace,
namespace_description,
tool_messages,
)
}
fn bind_tools(
tools: &[MessageBoardTool],
target: ToolTarget,
caller: ThreadId,
caller_path: AgentPath,
namespace: Option<&str>,
namespace_description: &str,
tool_messages: Option<&MultiAgentToolMessages>,
) -> Vec<Arc<dyn for<'call> ToolExecutor<ToolCall<'call>>>> {
tools
.iter()
@@ -114,7 +73,7 @@ fn bind_tools(
caller,
caller_path: caller_path.clone(),
tool,
target: target.clone(),
board: Arc::clone(&board),
namespace: namespace.map(str::to_owned),
namespace_description: namespace_description.to_owned(),
description: tool_message.and_then(|tool| tool.description.clone()),
@@ -123,17 +82,11 @@ fn bind_tools(
.collect()
}
#[derive(Clone)]
enum ToolTarget {
Live(Arc<dyn AgentMessageBoard>),
Fixed(Arc<dyn BoardReadView>),
}
struct BoardTool {
caller: ThreadId,
caller_path: AgentPath,
tool: MessageBoardTool,
target: ToolTarget,
board: Arc<dyn AgentMessageBoard>,
namespace: Option<String>,
namespace_description: String,
description: Option<String>,
@@ -211,13 +164,14 @@ impl BoardTool {
SortDirection::OldestFirst
}
};
match (self.tool, &self.target) {
(MessageBoardTool::UpsertChannel, ToolTarget::Live(board)) => {
let board = &self.board;
match self.tool {
MessageBoardTool::UpsertChannel => {
let request = serde_json::from_str(raw).map_err(model_error)?;
self.check_mutation_budget(budget, /*target_path_bytes*/ 0)?;
encode(board.upsert_channel(caller, request).await)
}
(MessageBoardTool::CreateChannel, ToolTarget::Live(board)) => {
MessageBoardTool::CreateChannel => {
let arguments::CreateChannel {
channel_name,
description,
@@ -248,7 +202,7 @@ impl BoardTool {
.await,
)
}
(MessageBoardTool::GetChannels, target) => {
MessageBoardTool::GetChannels => {
let arguments::GetChannels {
query,
recent_first,
@@ -262,14 +216,11 @@ impl BoardTool {
direction: direction(recent_first),
page: page(limit, cursor.clone(), scale),
};
match target {
ToolTarget::Live(board) => board.list_channels(caller, request),
ToolTarget::Fixed(view) => view.list_channels(caller, request),
}
board.list_channels(caller, request)
})
.await
}
(MessageBoardTool::SubscribedChannels, ToolTarget::Live(board)) => {
MessageBoardTool::SubscribedChannels => {
let arguments::SubscribedChannels { limit, cursor } =
serde_json::from_str(raw).map_err(model_error)?;
let limit = page_limit(limit);
@@ -278,7 +229,7 @@ impl BoardTool {
})
.await
}
(MessageBoardTool::ListThreads, target) => {
MessageBoardTool::ListThreads => {
let arguments::ListThreads {
channel_name,
sort,
@@ -297,14 +248,11 @@ impl BoardTool {
max_chars_per_post: nonzero(max_chars_per_post / scale),
page: page(limit, cursor.clone(), scale),
};
match target {
ToolTarget::Live(board) => board.list_threads(caller, request),
ToolTarget::Fixed(view) => view.list_threads(caller, request),
}
board.list_threads(caller, request)
})
.await
}
(MessageBoardTool::SearchPosts, target) => {
MessageBoardTool::SearchPosts => {
let arguments::SearchPosts {
channel_name,
query,
@@ -329,14 +277,11 @@ impl BoardTool {
max_chars_per_post: nonzero(max_chars_per_post / scale),
page: page(limit, cursor.clone(), scale),
};
match target {
ToolTarget::Live(board) => board.search_posts(caller, request),
ToolTarget::Fixed(view) => view.search_posts(caller, request),
}
board.search_posts(caller, request)
})
.await
}
(MessageBoardTool::ReadThread, target) => {
MessageBoardTool::ReadThread => {
let arguments::ReadThread {
thread_id,
limit,
@@ -351,14 +296,11 @@ impl BoardTool {
max_chars_per_post: nonzero(max_chars_per_post / scale),
page: page(limit, cursor.clone(), scale),
};
match target {
ToolTarget::Live(board) => board.read_thread(caller, request),
ToolTarget::Fixed(view) => view.read_thread(caller, request),
}
board.read_thread(caller, request)
})
.await
}
(MessageBoardTool::ReadPost, target) => {
MessageBoardTool::ReadPost => {
let arguments::ReadPost {
message_id,
offset_chars,
@@ -371,17 +313,11 @@ impl BoardTool {
offset_chars: offset_chars.unwrap_or_default(),
limit_chars: nonzero(limit_chars / scale),
};
match target {
ToolTarget::Live(board) => board.read_post(caller, request),
ToolTarget::Fixed(view) => view.read_post(caller, request),
}
board.read_post(caller, request)
})
.await
}
(
MessageBoardTool::Subscribe | MessageBoardTool::Unsubscribe,
ToolTarget::Live(board),
) => {
MessageBoardTool::Subscribe | MessageBoardTool::Unsubscribe => {
let arguments::Subscription {
channel_name,
thread_id,
@@ -417,7 +353,7 @@ impl BoardTool {
.await,
)
}
(MessageBoardTool::Post | MessageBoardTool::Append, ToolTarget::Live(board)) => {
MessageBoardTool::Post | MessageBoardTool::Append => {
let arguments::Post {
text,
channel_name,
@@ -486,10 +422,6 @@ impl BoardTool {
.await
})
}
(_, ToolTarget::Fixed(_)) => Err(FunctionCallError::Fatal(
"A board mutation was bound to a read-only view".to_string(),
)
.into()),
}
}
@@ -1940,89 +1940,6 @@ async fn silent_appends_preserve_evidence_and_explicit_subscriptions()
Ok(())
}
#[tokio::test]
async fn bulk_subscriptions_are_atomic_and_scoped_to_the_caller_and_supplied_channels() -> Result<()>
{
use SubscriptionChange::Subscribe;
use SubscriptionChange::Unsubscribe;
use std::collections::BTreeMap;
let dir = tempfile::tempdir()?;
let sqlite = SqliteConfig::new_for_testing(dir.path().to_path_buf().try_into().unwrap());
let owner = ThreadId::new();
let reviewer = ThreadId::new();
let host = Arc::new(Host {
clock: AtomicI64::default(),
fail_clock: AtomicBool::default(),
agent_path_calls: AtomicUsize::default(),
members: [
(owner, AgentPath::root()),
(reviewer, AgentPath::root().join("reviewer").unwrap()),
]
.into(),
active: AtomicBool::new(true),
fail_notifications: AtomicBool::default(),
notifications: Mutex::default(),
});
let board = LocalBoard::new(sqlite, owner.into());
let writer = board.read_write().open(host.clone()).await?;
let reader = board.read_only().open(host.clone()).await?;
for name in ["build", "release"] {
writer
.create_channel(
owner,
CreateChannelRequest {
channel_name: name.into(),
description: None,
subscription: Subscribe,
},
)
.await?;
}
reader
.set_subscriptions(
reviewer,
&BTreeMap::from([("build", Subscribe), ("release", Subscribe)]),
)
.await?;
let both = vec!["build".to_owned(), "release".to_owned()];
assert!(
reader
.set_subscriptions(
reviewer,
&BTreeMap::from([("build", Unsubscribe), ("missing", Subscribe)])
)
.await
.is_err()
);
assert_eq!(
reader
.subscribed_channels(reviewer, PageRequest::default())
.await?
.results,
both
);
reader
.set_subscriptions(reviewer, &BTreeMap::from([("build", Unsubscribe)]))
.await?;
assert_eq!(
reader
.subscribed_channels(reviewer, PageRequest::default())
.await?
.results,
vec!["release".to_owned()]
);
assert_eq!(
reader
.subscribed_channels(owner, PageRequest::default())
.await?
.results,
both
);
assert!(host.notifications.lock().unwrap().is_empty());
Ok(())
}
#[tokio::test]
async fn local_board_setup_survives_reopen() -> Result<()> {
use serde_json::json;
@@ -2270,6 +2187,3 @@ async fn storage_namespaces_preserve_isolation_reopen_and_deletion() -> Result<(
#[path = "local_board/mcp_tests.rs"]
mod mcp;
#[path = "local_board/read_view_tests.rs"]
mod read_view;
@@ -1,257 +0,0 @@
//! Fixed reads share SQLite storage while preserving the captured catalog and post boundary.
#![cfg(test)]
use super::*;
use pretty_assertions::assert_eq;
#[tokio::test]
async fn read_views_freeze_catalog_and_pages_without_changing_live_handles()
-> std::result::Result<(), Box<dyn std::error::Error>> {
let dir = tempfile::tempdir()?;
let sqlite = SqliteConfig::new_for_testing(dir.path().to_path_buf().try_into()?);
let root = ThreadId::new();
let host = Arc::new(Host {
clock: AtomicI64::default(),
fail_clock: AtomicBool::new(false),
agent_path_calls: AtomicUsize::default(),
members: [(root, AgentPath::root())].into(),
active: AtomicBool::new(true),
fail_notifications: AtomicBool::new(false),
notifications: Mutex::default(),
});
let writer = LocalAgentMessageBoard::open(
&sqlite,
&LocalBoardOptions::default(),
root.into(),
host.clone(),
)
.await?;
let reader = LocalBoard::new(sqlite.clone(), root.into())
.read_only()
.open(host.clone())
.await?;
for channel in ["alpha", "beta", "empty"] {
writer
.upsert_channel(
root,
UpsertChannelRequest {
channel_name: channel.into(),
description: "Original purpose".to_owned().try_into()?,
},
)
.await?;
}
let original = AppendRequest {
request_id: "original".into(),
destination: PostDestination::Channel("alpha".into()),
text: "proof original".into(),
};
let first = writer.append(root, original).await?;
for (request_id, destination) in [
("second", PostDestination::Channel("alpha".into())),
("reply-one", PostDestination::Thread(first.thread_id)),
("reply-two", PostDestination::Thread(first.thread_id)),
("beta", PostDestination::Channel("beta".into())),
] {
writer
.append(
root,
AppendRequest {
request_id: request_id.into(),
destination,
text: format!("proof {request_id}"),
},
)
.await?;
}
let catalog_query = ChannelQuery {
query: None,
direction: SortDirection::NewestFirst,
page: PageRequest::default(),
};
let before_catalog = reader.list_channels(root, catalog_query.clone()).await?;
let fixed = writer.read_view(root).await?;
assert_eq!(fixed.channels(), before_catalog.channels.results);
assert_eq!(
fixed.list_channels(root, catalog_query.clone()).await?,
before_catalog
);
let page = PageRequest {
cursor: None,
limit: NonZeroU32::MIN,
};
let search = PostQuery {
channel_name: Some("alpha".into()),
query: Some("PROOF".into()),
after_message_id: None,
author: Some(AgentPath::root()),
page: page.clone(),
max_chars_per_post: NonZeroU32::MAX,
};
let before_search = reader.search_posts(root, search.clone()).await?;
let next_search = PostQuery {
page: PageRequest {
cursor: Some(before_search.next_cursor.clone().unwrap()),
..page.clone()
},
..search.clone()
};
let before_next_search = reader.search_posts(root, next_search.clone()).await?;
let threads = ThreadQuery {
channel_name: "alpha".into(),
sort: ThreadSort::Activity,
direction: SortDirection::OldestFirst,
page: PageRequest::default(),
max_chars_per_post: NonZeroU32::MAX,
};
let before_threads = reader.list_threads(root, threads.clone()).await?;
let read_thread = ReadThreadRequest {
thread_id: first.thread_id,
page: page.clone(),
max_chars_per_post: NonZeroU32::MAX,
};
let before_replies = reader.read_thread(root, read_thread.clone()).await?;
let read = ReadPostRequest {
message_id: first.message_id,
offset_chars: 1,
limit_chars: NonZeroU32::MAX,
};
let content = reader.read_post(root, read.clone()).await?;
// Both newer and backdated appends must stay outside the fixed sequence boundary.
let mut later = Vec::new();
for clock in [100, -100] {
host.clock.store(clock, Ordering::SeqCst);
for destination in [
PostDestination::Channel("alpha".into()),
PostDestination::Thread(first.thread_id),
] {
later.push(
writer
.append(
root,
AppendRequest {
request_id: format!("later-{clock}-{destination:?}"),
destination,
text: "proof later".into(),
},
)
.await?,
);
}
}
for channel in ["alpha", "new"] {
writer
.upsert_channel(
root,
UpsertChannelRequest {
channel_name: channel.into(),
description: "Updated purpose".to_owned().try_into()?,
},
)
.await?;
}
assert_eq!(
fixed.search_posts(root, search.clone()).await?,
before_search
);
assert_eq!(
fixed.search_posts(root, next_search).await?,
before_next_search
);
assert_eq!(fixed.list_threads(root, threads).await?, before_threads);
assert_eq!(
fixed.read_thread(root, read_thread.clone()).await?,
before_replies
);
assert_eq!(fixed.read_post(root, read.clone()).await?, content);
assert_eq!(
fixed.list_channels(root, catalog_query.clone()).await?,
before_catalog
);
for post in later {
assert!(
fixed
.read_post(
root,
ReadPostRequest {
message_id: post.message_id,
..read.clone()
}
)
.await
.is_err()
);
assert!(
fixed
.search_posts(
root,
PostQuery {
after_message_id: Some(post.message_id),
..search.clone()
}
)
.await
.is_err()
);
if post.message_id == post.thread_id {
assert!(
fixed
.read_thread(
root,
ReadThreadRequest {
thread_id: post.thread_id,
..read_thread.clone()
}
)
.await
.is_err()
);
}
}
assert!(
fixed
.list_threads(
root,
ThreadQuery {
channel_name: "new".into(),
sort: ThreadSort::Created,
direction: SortDirection::NewestFirst,
page: page.clone(),
max_chars_per_post: NonZeroU32::MAX,
}
)
.await
.is_err()
);
assert_ne!(
reader.search_posts(root, search.clone()).await?,
fixed.search_posts(root, search).await?
);
assert_ne!(
reader.list_channels(root, catalog_query.clone()).await?,
before_catalog
);
assert_eq!(
writer.read_view(root).await?.channels(),
reader
.list_channels(root, catalog_query.clone())
.await?
.channels
.results
);
// A read-only live connection still permits its caller's subscription changes.
let subscription = SubscriptionRequest {
target: SubscriptionTarget::Channel("alpha".into()),
target_agent: None,
change: SubscriptionChange::Subscribe,
};
reader.set_subscription(root, subscription).await?;
assert_eq!(
reader.subscribed_channels(root, page).await?.results,
vec!["alpha".to_owned()]
);
Ok(())
}