mirror of
https://github.com/openai/codex.git
synced 2026-11-23 23:10:47 +00:00
Add explicit local board connections with fixed runtime access (#53400)
## Why Local message boards were scoped to an agent tree. Independently created Codex runtimes need a way to share a board while receiving different access permissions. ## What changed - Add `LocalBoard` and `LocalBoardConnection` so hosts can supply read-only or read-write access through `thread_extension_init`, including for isolated, ephemeral runtimes. - Allow read-only connections to read and manage their own subscriptions; reject posting, replying, channel creation, and changes to another agent's subscriptions. Expose effective permissions in channel discovery. - Use unique runtime paths and require matching board IDs and canonical database paths when resolving agents or delivering notifications. - Document explicit extension installation and connection setup for new runtimes, cold resumes, and forks. ## Testing Add a scenario and context snapshot covering an independent writer and isolated Guardian sharing a board through a symlink alias, read-only permission denials, subscription and explicit notifications, and rejection of notifications to an unconnected runtime before a post is written. GitOrigin-RevId: f92410c3a5c6a46920c3e0e71849c85dc83d9c31
This commit is contained in:
@@ -25,3 +25,30 @@ otherwise it uses UTC now.
|
||||
|
||||
This loads channels and descriptions only. ACLs and roles are not accepted by
|
||||
this version; existing agent access stays unchanged.
|
||||
|
||||
## Connect Codex runtimes
|
||||
|
||||
The host can give independently created Codex runtimes different access to the
|
||||
same local board through the existing startup extension settings:
|
||||
|
||||
```rust
|
||||
let board = LocalBoard::new(sqlite_config, board_id);
|
||||
guardian_options.thread_extension_init.insert(board.read_only());
|
||||
writer_options.thread_extension_init.insert(board.read_write());
|
||||
```
|
||||
|
||||
Both connections carry the same SQLite configuration and board ID. Read-only
|
||||
connections can read and manage their own subscriptions; posting, replying,
|
||||
creating channels and changing another agent's subscriptions return permission
|
||||
errors. Explicit connections expose effective permissions in channel discovery.
|
||||
Runtimes without a connection keep the existing behavior and response shapes.
|
||||
|
||||
Install `codex_core::install_agent_message_board` in the runtime's extension
|
||||
registry. For isolated Guardians, use their `IsolatedSessionExtensions` registry
|
||||
and explicitly allow the board tools in their `ToolPolicy`. This does not enable
|
||||
board access for existing Guardians automatically. Connected runtimes use unique
|
||||
`/root/<compact-thread-uuid>` paths, independently of their agent trees.
|
||||
|
||||
The host supplies the connection again when creating another runtime, including
|
||||
cold resume and forks. Connections are not database ACLs or inherited through
|
||||
`spawn_agent`. Local filesystem writers remain trusted.
|
||||
|
||||
@@ -24,7 +24,7 @@ use uuid::Uuid;
|
||||
// Local and remote implementations must remain usable through the same handle.
|
||||
const _: Option<&dyn AgentMessageBoard> = None;
|
||||
|
||||
/// Stores discussions and subscriptions for one agent tree.
|
||||
/// Stores discussions and subscriptions for one board.
|
||||
///
|
||||
/// Every operation validates caller membership. IDs are scoped to this board;
|
||||
/// an agent runtime's ThreadId is distinct from a discussion's root post UUID.
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
//! Installs caller-bound tools using persistent host identities.
|
||||
//! Installs caller-bound tools using host identities and optional local connections.
|
||||
//!
|
||||
//! Backend selection is a host concern. Startup failures leave tools unavailable
|
||||
//! and emit a warning; a disabled factory does not open storage.
|
||||
//! Tool namespaces follow the host's configuration at runtime startup.
|
||||
|
||||
use crate::AgentMessageBoard;
|
||||
use crate::LocalBoardConnection;
|
||||
use crate::tools::message_board_tools_with_descriptions;
|
||||
use codex_extension_api::ExtensionData;
|
||||
use codex_extension_api::ExtensionEventSink;
|
||||
@@ -60,7 +61,11 @@ impl<C: Sync> ThreadLifecycleContributor<C> for BoardExtension<C> {
|
||||
CodexErr::InvalidRequest("invalid board caller identity".into())
|
||||
})?;
|
||||
if let Some(board) = (self.open)(&input, tree, caller).await? {
|
||||
if board.identity() != tree {
|
||||
let connection = input.thread_store.get::<LocalBoardConnection>();
|
||||
let expected = connection
|
||||
.as_ref()
|
||||
.map_or(tree, |connection| connection.identity());
|
||||
if board.identity() != expected {
|
||||
return Err(CodexErr::InvalidRequest(
|
||||
"message-board factory returned another tree".into(),
|
||||
));
|
||||
@@ -69,10 +74,13 @@ impl<C: Sync> ThreadLifecycleContributor<C> for BoardExtension<C> {
|
||||
board,
|
||||
caller,
|
||||
namespace: (self.tool_namespace)(input.config),
|
||||
path: input
|
||||
.session_source
|
||||
.get_agent_path()
|
||||
.unwrap_or_else(AgentPath::root),
|
||||
path: match connection {
|
||||
Some(connection) => connection.caller_path(caller)?,
|
||||
None => input
|
||||
.session_source
|
||||
.get_agent_path()
|
||||
.unwrap_or_else(AgentPath::root),
|
||||
},
|
||||
});
|
||||
}
|
||||
Ok::<(), CodexErr>(())
|
||||
|
||||
@@ -14,7 +14,7 @@ pub enum NotificationDelivery {
|
||||
SkippedInactive,
|
||||
}
|
||||
|
||||
/// Tree-scoped membership, clock and notification access supplied by the host.
|
||||
/// Board-scoped membership, clock and notification access supplied by the host.
|
||||
///
|
||||
/// The host remains authoritative for membership even when runtimes are unloaded.
|
||||
/// Clock reads use the posting agent's configured source, including simulated
|
||||
|
||||
@@ -34,6 +34,8 @@ pub use host::NotificationDelivery;
|
||||
pub use in_memory::InMemoryAgentMessageBoard;
|
||||
pub use in_memory::InMemoryMessageBoards;
|
||||
pub use local::LocalAgentMessageBoard;
|
||||
pub use local::connection::LocalBoard;
|
||||
pub use local::connection::LocalBoardConnection;
|
||||
pub use local::setup::LocalBoardSetup;
|
||||
pub use local::setup::configure_local_board;
|
||||
pub use permissions::BoardPermissions;
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
//! SQLite-backed boards. The tree ID scopes every read and write.
|
||||
//! SQLite-backed boards. The board ID scopes every read and write.
|
||||
//!
|
||||
//! Immediate transactions serialize mutations across independently opened handles.
|
||||
//! Live handles in this process share one connection pool per database path.
|
||||
//! Accepted posts survive runtime unload and process restart, but cannot recreate
|
||||
//! 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.
|
||||
|
||||
use self::connection::Access;
|
||||
use crate::ChannelDescription;
|
||||
use crate::ChannelPermissions;
|
||||
use crate::ChannelSummary;
|
||||
use crate::CreateChannelRequest;
|
||||
use crate::MessageBoardHost;
|
||||
@@ -38,6 +41,7 @@ use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub(crate) mod connection;
|
||||
mod lifecycle;
|
||||
mod paging;
|
||||
mod pool;
|
||||
@@ -57,6 +61,7 @@ pub struct LocalAgentMessageBoard {
|
||||
identity: SessionId,
|
||||
pool: Arc<SqlitePool>,
|
||||
host: Arc<dyn MessageBoardHost>,
|
||||
access: Option<Access>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
@@ -77,6 +82,7 @@ impl LocalAgentMessageBoard {
|
||||
identity,
|
||||
pool: pool::open(sqlite).await?,
|
||||
host,
|
||||
access: None,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -85,6 +91,9 @@ impl LocalAgentMessageBoard {
|
||||
caller: ThreadId,
|
||||
request: CreateChannelRequest,
|
||||
) -> Result<ChannelSummary> {
|
||||
if let Some(access) = self.access {
|
||||
access.require_write(self.identity, "board.create_channel")?;
|
||||
}
|
||||
validate_channel(&request.channel_name)?;
|
||||
let author = self.host.agent_path(caller).await?;
|
||||
let now = self.host.current_time(caller).await?;
|
||||
@@ -116,6 +125,15 @@ impl LocalAgentMessageBoard {
|
||||
/// into a recipient's later turn. Once committed, notification failures are
|
||||
/// logged without failing the post; the persisted content remains readable.
|
||||
pub async fn post(&self, caller: ThreadId, request: PostRequest) -> Result<PostMetadata> {
|
||||
if let Some(access) = self.access {
|
||||
access.require_write(
|
||||
self.identity,
|
||||
match &request.destination {
|
||||
PostDestination::Thread(_) => "channel.reply",
|
||||
PostDestination::Channel(_) | PostDestination::NewChannel(_) => "channel.post",
|
||||
},
|
||||
)?;
|
||||
}
|
||||
let board = self.clone();
|
||||
tokio::spawn(async move { board.post_inner(caller, request).await })
|
||||
.await
|
||||
@@ -269,6 +287,11 @@ 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?;
|
||||
if target_agent != caller
|
||||
&& let Some(access) = self.access
|
||||
{
|
||||
access.require_write(self.identity, "board.manage_subscriptions")?;
|
||||
}
|
||||
let mut tx = self.begin_write().await?;
|
||||
let (channel, root, last) = match &request.target {
|
||||
SubscriptionTarget::Channel(name) => {
|
||||
@@ -413,7 +436,15 @@ impl LocalAgentMessageBoard {
|
||||
.map_err(storage_error)?
|
||||
.ok_or_else(|| invalid("channel not found in this board"))?;
|
||||
Ok(ChannelSummary {
|
||||
permissions: None,
|
||||
permissions: self.access.map(|access| {
|
||||
let write = matches!(access, Access::ReadWrite);
|
||||
ChannelPermissions {
|
||||
read: true,
|
||||
post: write,
|
||||
reply: write,
|
||||
edit_metadata: false,
|
||||
}
|
||||
}),
|
||||
channel_name: name.to_string(),
|
||||
description: row.get("description"),
|
||||
created_at: DateTime::parse_from_rfc3339(row.get("created_at"))
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
//! 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::LocalAgentMessageBoard;
|
||||
use crate::MessageBoardHost;
|
||||
use crate::PermissionDenied;
|
||||
use codex_protocol::AgentPath;
|
||||
use codex_protocol::SessionId;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::error::CodexErr;
|
||||
use codex_protocol::error::Result;
|
||||
use codex_state::SqliteConfig;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
/// Shared local storage from which a host grants individual runtime connections.
|
||||
#[derive(Clone)]
|
||||
pub struct LocalBoard {
|
||||
sqlite: SqliteConfig,
|
||||
identity: SessionId,
|
||||
}
|
||||
|
||||
impl LocalBoard {
|
||||
pub fn new(sqlite: SqliteConfig, identity: SessionId) -> Self {
|
||||
Self { sqlite, identity }
|
||||
}
|
||||
|
||||
pub fn read_only(&self) -> LocalBoardConnection {
|
||||
LocalBoardConnection {
|
||||
board: self.clone(),
|
||||
access: Access::ReadOnly,
|
||||
database: OnceLock::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_write(&self) -> LocalBoardConnection {
|
||||
LocalBoardConnection {
|
||||
board: self.clone(),
|
||||
access: Access::ReadWrite,
|
||||
database: OnceLock::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Insert into `StartThreadOptions::thread_extension_init` before creating Codex.
|
||||
/// Hosts must supply it again for a new runtime, including cold resume or a fork.
|
||||
/// The board extension must be installed, including in an isolated session's registry.
|
||||
#[derive(Clone)]
|
||||
pub struct LocalBoardConnection {
|
||||
board: LocalBoard,
|
||||
access: Access,
|
||||
// Capture the opened database, independently of caller unload or symlink changes.
|
||||
database: OnceLock<PathBuf>,
|
||||
}
|
||||
|
||||
impl LocalBoardConnection {
|
||||
pub fn identity(&self) -> SessionId {
|
||||
self.board.identity
|
||||
}
|
||||
|
||||
/// Compare the canonical database paths captured when the connections were opened.
|
||||
pub fn same_board(&self, other: &Self) -> bool {
|
||||
self.identity() == other.identity()
|
||||
&& self
|
||||
.database
|
||||
.get()
|
||||
.zip(other.database.get())
|
||||
.is_some_and(|(database, other)| database == other)
|
||||
}
|
||||
|
||||
/// Independent runtimes have no common agent tree. Use their unique IDs as paths.
|
||||
pub fn caller_path(&self, caller: ThreadId) -> Result<AgentPath> {
|
||||
AgentPath::root()
|
||||
.join(&caller.to_string().replace('-', ""))
|
||||
.map_err(CodexErr::InvalidRequest)
|
||||
}
|
||||
|
||||
pub async fn open(&self, host: Arc<dyn MessageBoardHost>) -> Result<LocalAgentMessageBoard> {
|
||||
let mut board =
|
||||
LocalAgentMessageBoard::open(&self.board.sqlite, self.identity(), host).await?;
|
||||
// Pool initialization canonicalizes the directory before opening SQLite.
|
||||
let options = board.pool.connect_options();
|
||||
let database = options.get_filename();
|
||||
if self.database.get_or_init(|| database.to_path_buf()) != database {
|
||||
return Err(CodexErr::InvalidRequest(
|
||||
"board database changed; create a new board connection".into(),
|
||||
));
|
||||
}
|
||||
board.access = Some(self.access);
|
||||
Ok(board)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(super) enum Access {
|
||||
ReadOnly,
|
||||
ReadWrite,
|
||||
}
|
||||
|
||||
impl Access {
|
||||
pub(super) fn require_write(self, board: SessionId, action: &str) -> Result<()> {
|
||||
match self {
|
||||
Self::ReadWrite => Ok(()),
|
||||
Self::ReadOnly => Err(PermissionDenied {
|
||||
message: "This board connection is read-only.".into(),
|
||||
action: Some(action.into()),
|
||||
resource: Some(format!("board:{board}")),
|
||||
}
|
||||
.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
use super::LocalAgentMessageBoard;
|
||||
use super::MAX_READ_CHARS;
|
||||
use super::StoredPost;
|
||||
use super::connection::Access;
|
||||
use super::invalid;
|
||||
use super::paging::Window;
|
||||
use super::paging::decode_posts;
|
||||
@@ -10,6 +11,7 @@ use super::paging::direction;
|
||||
use super::paging::preview;
|
||||
use super::storage_error;
|
||||
use crate::AgentMessageBoard;
|
||||
use crate::BoardPermissions;
|
||||
use crate::ChannelPage;
|
||||
use crate::ChannelQuery;
|
||||
use crate::ChannelSummary;
|
||||
@@ -109,7 +111,11 @@ impl AgentMessageBoard for LocalAgentMessageBoard {
|
||||
}
|
||||
Ok(ChannelPage {
|
||||
channels: window.finish(channels)?,
|
||||
board_permissions: None,
|
||||
board_permissions: self.access.map(|access| BoardPermissions {
|
||||
create_channel: matches!(access, Access::ReadWrite),
|
||||
edit_metadata: false,
|
||||
manage_permissions: false,
|
||||
}),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! Service-computed permissions and denials. Codex exposes these without evaluating ACLs.
|
||||
//! Backend-computed permissions and denials exposed by the board tools.
|
||||
|
||||
use codex_protocol::error::CodexErr;
|
||||
use codex_tools::FunctionCallError;
|
||||
@@ -25,7 +25,7 @@ pub struct ChannelPermissions {
|
||||
pub edit_metadata: bool,
|
||||
}
|
||||
|
||||
/// A service denial retained through the board's existing error interface.
|
||||
/// A backend denial retained through the board's existing error interface.
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[serde(tag = "code", rename = "permission_denied")]
|
||||
pub struct PermissionDenied {
|
||||
|
||||
Reference in New Issue
Block a user