mirror of
https://github.com/openai/codex.git
synced 2026-11-20 22:40:37 +00:00
## Why A fixed evidence view hides posts routed after the review cutoff. Live retrieval lets reviewers find those posts as they arrive while still allowing intake to confirm that evidence through a requested history position has been routed. ## What changed Guardian retrieval tools search the live message board, including channels the reviewer does not subscribe to. Subscription tools update only the acting reviewer's subscriptions. The housekeeper uses the subscribers present at each post's insertion to queue one committed post per original in each recipient's inbox, even when multiple subscribed channels match. It acknowledges catch-up requests after routing and queuing evidence through the requested history position. ## Testing Cover catch-up ordering, delivery based on subscription state at insertion, live search across unsubscribed channels, and caller-scoped subscription changes. GitOrigin-RevId: 28b447d936f19ea68bd3c3adb297d473d65173c0
150 lines
5.5 KiB
Rust
150 lines
5.5 KiB
Rust
//! Reviewer tools that change only the caller’s live channel subscriptions.
|
||
|
||
use codex_message_board::AgentMessageBoard;
|
||
use codex_message_board::MAX_TOOL_ARGUMENT_KIB;
|
||
use codex_message_board::MAX_TOOL_RESPONSE_BYTES;
|
||
use codex_message_board::MessageBoardTool;
|
||
use codex_message_board::SubscriptionChange;
|
||
use codex_message_board::SubscriptionRequest;
|
||
use codex_message_board::SubscriptionState;
|
||
use codex_message_board::SubscriptionTarget;
|
||
use codex_protocol::AgentPath;
|
||
use codex_protocol::ThreadId;
|
||
use codex_tools::FunctionCallError;
|
||
use codex_tools::JsonToolOutput;
|
||
use codex_tools::ResponsesApiNamespace;
|
||
use codex_tools::ResponsesApiNamespaceTool;
|
||
use codex_tools::ToolCall;
|
||
use codex_tools::ToolExecutor;
|
||
use codex_tools::ToolExecutorFuture;
|
||
use codex_tools::ToolName;
|
||
use codex_tools::ToolSpec;
|
||
use serde::Deserialize;
|
||
use std::sync::Arc;
|
||
use uuid::Uuid;
|
||
|
||
#[derive(Deserialize)]
|
||
#[serde(deny_unknown_fields)]
|
||
struct Arguments {
|
||
channel_name: String,
|
||
}
|
||
|
||
struct SubscriptionTool {
|
||
board: Arc<dyn AgentMessageBoard>,
|
||
caller: ThreadId,
|
||
path: AgentPath,
|
||
change: SubscriptionChange,
|
||
name: ToolName,
|
||
spec: ToolSpec,
|
||
}
|
||
|
||
pub(crate) fn tools(
|
||
board: &Arc<dyn AgentMessageBoard>,
|
||
caller: ThreadId,
|
||
path: &AgentPath,
|
||
namespace: Option<&str>,
|
||
namespace_description: &str,
|
||
) -> Vec<Arc<dyn for<'call> ToolExecutor<ToolCall<'call>>>> {
|
||
[
|
||
(MessageBoardTool::Subscribe, SubscriptionChange::Subscribe),
|
||
(MessageBoardTool::Unsubscribe, SubscriptionChange::Unsubscribe),
|
||
]
|
||
.into_iter()
|
||
.map(|(tool, change)| {
|
||
let ToolSpec::Function(mut function) = tool.spec() else {
|
||
unreachable!("shared subscription specs are functions")
|
||
};
|
||
function
|
||
.parameters
|
||
.properties
|
||
.get_or_insert_default()
|
||
.retain(|name, _| name == "channel_name");
|
||
function.parameters.required = Some(vec!["channel_name".into()].into_boxed_slice());
|
||
function.description = match change {
|
||
SubscriptionChange::Subscribe => "Subscribe yourself to new evidence in this channel. Search or read its earlier posts separately.",
|
||
SubscriptionChange::Unsubscribe => "Stop receiving new evidence from this channel. Evidence already admitted to the reviewer conversation is retained.",
|
||
}
|
||
.into();
|
||
let name = ToolName::new(namespace.map(str::to_owned), function.name.clone());
|
||
let spec = match namespace {
|
||
Some(namespace) => ToolSpec::Namespace(ResponsesApiNamespace {
|
||
name: namespace.into(),
|
||
description: namespace_description.into(),
|
||
tools: vec![ResponsesApiNamespaceTool::Function(function)],
|
||
}),
|
||
None => ToolSpec::Function(function),
|
||
};
|
||
Arc::new(SubscriptionTool {
|
||
board: Arc::clone(board),
|
||
caller,
|
||
path: path.clone(),
|
||
change,
|
||
name,
|
||
spec,
|
||
}) as Arc<dyn for<'call> ToolExecutor<ToolCall<'call>>>
|
||
})
|
||
.collect()
|
||
}
|
||
|
||
impl<'call> ToolExecutor<ToolCall<'call>> for SubscriptionTool {
|
||
fn tool_name(&self) -> ToolName {
|
||
self.name.clone()
|
||
}
|
||
|
||
fn spec(&self) -> ToolSpec {
|
||
self.spec.clone()
|
||
}
|
||
|
||
fn handle<'a>(&'a self, call: ToolCall<'call>) -> ToolExecutorFuture<'a>
|
||
where
|
||
'call: 'a,
|
||
{
|
||
Box::pin(async move {
|
||
let raw = call.function_arguments()?;
|
||
if raw.len() > MAX_TOOL_ARGUMENT_KIB * 1024 {
|
||
return Err(FunctionCallError::RespondToModel(format!(
|
||
"subscription arguments exceed {MAX_TOOL_ARGUMENT_KIB} KiB"
|
||
)));
|
||
}
|
||
let arguments: Arguments = serde_json::from_str(raw)
|
||
.map_err(|error| FunctionCallError::RespondToModel(error.to_string()))?;
|
||
// For live subscriptions the last post ID is known only after the write.
|
||
// Budget for a full UUID to avoid accepting a mutation we cannot acknowledge.
|
||
let largest_response = SubscriptionState {
|
||
channel_name: arguments.channel_name.clone(),
|
||
thread_id: None,
|
||
target_agent: self.path.clone(),
|
||
enabled: self.change == SubscriptionChange::Subscribe,
|
||
last_message_id: Some(Uuid::nil()),
|
||
};
|
||
if serde_json::to_vec(&largest_response)
|
||
.map_err(|error| FunctionCallError::Fatal(error.to_string()))?
|
||
.len()
|
||
> call.response_byte_budget(MAX_TOOL_RESPONSE_BYTES)
|
||
{
|
||
return Err(FunctionCallError::RespondToModel(
|
||
"subscription response exceeds the tool-output budget".into(),
|
||
));
|
||
}
|
||
let state = self
|
||
.board
|
||
.set_subscription(
|
||
self.caller,
|
||
SubscriptionRequest {
|
||
target: SubscriptionTarget::Channel(arguments.channel_name),
|
||
target_agent: None,
|
||
change: self.change,
|
||
},
|
||
)
|
||
.await
|
||
.map_err(|error| FunctionCallError::RespondToModel(error.to_string()))?;
|
||
let response = serde_json::to_value(state)
|
||
.map_err(|error| FunctionCallError::Fatal(error.to_string()))?;
|
||
Ok(
|
||
Box::new(JsonToolOutput::new(response).with_external_context())
|
||
as Box<dyn codex_tools::ToolOutput>,
|
||
)
|
||
})
|
||
}
|
||
}
|