Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Stop redundant message-board reads when limits reach one (#47064)

jif51d221a9b77a
diff --git a/codex-rs/ext/agent-message-board/src/tools.rs b/codex-rs/ext/agent-message-board/src/tools.rs
index dd7acd66d3..39815c83a9 100644
--- a/codex-rs/ext/agent-message-board/src/tools.rs
+++ b/codex-rs/ext/agent-message-board/src/tools.rs
@@ -122,13 +122,13 @@ impl BoardTool {
) -> Result<Value, FunctionCallError> {
let board = &self.board;
let caller = self.caller;
- let page = |limit: Option<NonZeroU32>, cursor, scale| PageRequest {
- limit: nonzero(limit.map_or(20, NonZeroU32::get).min(50) / scale),
+ let page_limit = |limit: Option<NonZeroU32>| limit.map_or(20, NonZeroU32::get).min(50);
+ let preview_limit =
+ |limit: Option<NonZeroU32>| limit.map_or(1000, NonZeroU32::get).min(20_000);
+ let page = |limit, cursor, scale| PageRequest {
+ limit: nonzero(limit / scale),
cursor,
};
- let preview = |requested: Option<NonZeroU32>, scale| {
- nonzero(requested.map_or(1000, NonZeroU32::get).min(20_000) / scale)
- };
let direction = |recent_first: Option<bool>| {
if recent_first.unwrap_or(true) {
SortDirection::NewestFirst
@@ -166,7 +166,8 @@ impl BoardTool {
limit,
cursor,
} = serde_json::from_str(raw).map_err(model_error)?;
- bounded_read(budget, |scale| {
+ let limit = page_limit(limit);
+ bounded_read(budget, limit, |scale| {
board.list_channels(
caller,
ChannelQuery {
@@ -187,14 +188,16 @@ impl BoardTool {
cursor,
max_chars_per_post,
} = serde_json::from_str(raw).map_err(model_error)?;
- bounded_read(budget, |scale| {
+ let limit = page_limit(limit);
+ let max_chars_per_post = preview_limit(max_chars_per_post);
+ bounded_read(budget, limit.max(max_chars_per_post), |scale| {
board.list_threads(
caller,
ThreadQuery {
channel_name: channel_name.clone(),
sort: sort.unwrap_or(ThreadSort::Created),
direction: direction(recent_first),
- max_chars_per_post: preview(max_chars_per_post, scale),
+ max_chars_per_post: nonzero(max_chars_per_post / scale),
page: page(limit, cursor.clone(), scale),
},
)
@@ -215,7 +218,9 @@ impl BoardTool {
.map(|path| self.caller_path.resolve(&path))
.transpose()
.map_err(model_error)?;
- bounded_read(budget, |scale| {
+ let limit = page_limit(limit);
+ let max_chars_per_post = preview_limit(max_chars_per_post);
+ bounded_read(budget, limit.max(max_chars_per_post), |scale| {
board.search_posts(
caller,
PostQuery {
@@ -223,7 +228,7 @@ impl BoardTool {
query: query.clone(),
after_message_id,
author: author.clone(),
- max_chars_per_post: preview(max_chars_per_post, scale),
+ max_chars_per_post: nonzero(max_chars_per_post / scale),
page: page(limit, cursor.clone(), scale),
},
)
@@ -237,12 +242,14 @@ impl BoardTool {
cursor,
max_chars_per_post,
} = serde_json::from_str(raw).map_err(model_error)?;
- bounded_read(budget, |scale| {
+ let limit = page_limit(limit);
+ let max_chars_per_post = preview_limit(max_chars_per_post);
+ bounded_read(budget, limit.max(max_chars_per_post), |scale| {
board.read_thread(
caller,
ReadThreadRequest {
thread_id,
- max_chars_per_post: preview(max_chars_per_post, scale),
+ max_chars_per_post: nonzero(max_chars_per_post / scale),
page: page(limit, cursor.clone(), scale),
},
)
@@ -255,15 +262,14 @@ impl BoardTool {
offset_chars,
limit_chars,
} = serde_json::from_str(raw).map_err(model_error)?;
- bounded_read(budget, |scale| {
+ let limit_chars = limit_chars.map_or(20_000, NonZeroU32::get).min(20_000);
+ bounded_read(budget, limit_chars, |scale| {
board.read_post(
caller,
ReadPostRequest {
message_id,
offset_chars: offset_chars.unwrap_or_default(),
- limit_chars: nonzero(
- limit_chars.map_or(20_000, NonZeroU32::get).min(20_000) / scale,
- ),
+ limit_chars: nonzero(limit_chars / scale),
},
)
})
@@ -371,16 +377,22 @@ impl BoardTool {
/// Try the requested read first. Halve limits only when its serialized output is too large.
/// Reissuing the read lets each backend generate a cursor for exactly the returned page.
+/// Stop once every limit has reached one; another read would repeat the same request.
async fn bounded_read<'a, T: Serialize>(
budget: usize,
+ largest_limit: u32,
fetch: impl Fn(u32) -> BoxFuture<'a, codex_protocol::error::Result<T>>,
) -> Result<Value, FunctionCallError> {
- // Read limits are capped at 20,000, so the final attempt asks for one character/item.
- for shift in 0..=15 {
- let result = encode(fetch(1 << shift).await)?;
+ let mut scale = 1;
+ loop {
+ let result = encode(fetch(scale).await)?;
if result.to_string().len() <= budget {
return Ok(result);
}
+ if largest_limit / scale <= 1 {
+ break;
+ }
+ scale *= 2;
}
Err(model_error(
"The output budget is too small for this result's metadata.",
diff --git a/codex-rs/ext/agent-message-board/tests/local_board.rs b/codex-rs/ext/agent-message-board/tests/local_board.rs
index a4aba522d7..9dfbbbf29d 100644
--- a/codex-rs/ext/agent-message-board/tests/local_board.rs
+++ b/codex-rs/ext/agent-message-board/tests/local_board.rs
@@ -6,6 +6,7 @@ use std::sync::Arc;
use std::sync::Mutex;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::AtomicI64;
+use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use chrono::DateTime;
@@ -22,6 +23,7 @@ use pretty_assertions::assert_eq;
struct Host {
clock: AtomicI64,
+ agent_path_calls: AtomicUsize,
members: HashMap<ThreadId, AgentPath>,
active: AtomicBool,
fail_notifications: AtomicBool,
@@ -31,6 +33,7 @@ struct Host {
impl MessageBoardHost for Host {
fn agent_path(&self, caller: ThreadId) -> BoxFuture<'_, Result<AgentPath>> {
Box::pin(async move {
+ self.agent_path_calls.fetch_add(1, Ordering::SeqCst);
self.members
.get(&caller)
.cloned()
@@ -88,6 +91,7 @@ async fn shared_handles_resume_posts_and_preserve_subscription_rules() {
let child_path = AgentPath::root().join("worker").unwrap();
let host = Arc::new(Host {
clock: AtomicI64::default(),
+ agent_path_calls: AtomicUsize::default(),
members: [(root, AgentPath::root()), (child, child_path.clone())].into(),
fail_notifications: AtomicBool::new(false),
active: AtomicBool::new(true),
@@ -240,6 +244,7 @@ async fn failed_requests_do_not_create_channels_or_notify_inactive_agents() {
let root = ThreadId::new();
let host = Arc::new(Host {
clock: AtomicI64::default(),
+ agent_path_calls: AtomicUsize::default(),
members: [(root, AgentPath::root())].into(),
fail_notifications: AtomicBool::new(false),
active: AtomicBool::new(false),
@@ -305,6 +310,7 @@ async fn queries_enforce_page_and_preview_caps() {
let host = Arc::new(Host {
fail_notifications: AtomicBool::new(false),
clock: AtomicI64::default(),
+ agent_path_calls: AtomicUsize::default(),
members: [(root, AgentPath::root())].into(),
active: AtomicBool::new(false),
notifications: Mutex::default(),
@@ -395,6 +401,7 @@ async fn queries_page_discussions_and_search_unicode() {
let host = Arc::new(Host {
fail_notifications: AtomicBool::new(false),
clock: AtomicI64::default(),
+ agent_path_calls: AtomicUsize::default(),
members: [(root, AgentPath::root())].into(),
active: AtomicBool::new(true),
notifications: Mutex::default(),
@@ -655,6 +662,7 @@ async fn tools_cover_channel_discussions_subscriptions_and_escaped_previews() {
fail_notifications: AtomicBool::new(false),
members: [(root, AgentPath::root()), (child, child_path.clone())].into(),
clock: AtomicI64::default(),
+ agent_path_calls: AtomicUsize::default(),
active: AtomicBool::new(true),
notifications: Mutex::default(),
});
@@ -852,6 +860,7 @@ async fn tools_validate_arguments_deduplicate_calls_and_bound_unicode_results()
fail_notifications: AtomicBool::new(false),
members: [(root, AgentPath::root()), (child, child_path)].into(),
clock: AtomicI64::default(),
+ agent_path_calls: AtomicUsize::default(),
active: AtomicBool::new(true),
notifications: Mutex::default(),
});
@@ -950,6 +959,43 @@ async fn tools_validate_arguments_deduplicate_calls_and_bound_unicode_results()
next_cursor: None,
}
);
+ // An impossible metadata budget stops when both the page and preview reach one.
+ // Unequal limits ensure we keep shrinking while either dimension can still change.
+ for (name, args, expected_reads) in [
+ ("get_channels", json!({"limit":1}), 1),
+ (
+ "list_threads",
+ json!({"channel_name":"work","limit":1,"max_chars_per_post":8}),
+ 4,
+ ),
+ ("search_posts", json!({"limit":8,"max_chars_per_post":1}), 4),
+ (
+ "read_thread",
+ json!({"thread_id":metadata.thread_id,"limit":3,"max_chars_per_post":1}),
+ 2,
+ ),
+ (
+ "read_post",
+ json!({"message_id":metadata.message_id,"limit_chars":3}),
+ 2,
+ ),
+ ] {
+ let mut limited = call(name, args);
+ limited.truncation_policy = TruncationPolicy::Bytes(1);
+ host.agent_path_calls.store(0, Ordering::SeqCst);
+ let error = tool(name).handle(limited).await.err().unwrap();
+ assert_eq!(
+ error,
+ codex_tools::FunctionCallError::RespondToModel(
+ "The output budget is too small for this result's metadata.".into()
+ ),
+ );
+ assert_eq!(
+ host.agent_path_calls.load(Ordering::SeqCst),
+ expected_reads,
+ "{name}",
+ );
+ }
let read_call = call("read_post", json!({"message_id":metadata.message_id}));
let result = tool("read_post").handle(read_call.clone()).await.unwrap();
assert!(result.contains_external_context());