From 0f9a731adeb4ccc75a7a417945ec01ee98e997d7 Mon Sep 17 00:00:00 2001 From: Alex Daley Date: Sat, 26 Sep 2026 23:34:08 +0000 Subject: [PATCH] Preserve deferred tool namespace names before descriptions (#48574) ## Why Long descriptions could exhaust the 4 KiB tool summary budget and hide later namespace names, limiting their visibility for tool discovery. ## What changed - Reserve space for all namespace names before sharing the remaining budget across descriptions, including added and removed groups. - Truncate descriptions at UTF-8 boundaries with `...`, including at the existing 250-character cap. Omit whole namespaces only when names alone exceed the budget, reserving omission notices only in that case. - Render namespace names and descriptions without XML escaping. ## Testing Add unit and snapshot coverage for description allocation, Unicode boundaries, namespace omissions, and empty-state notices. Add a scenario verifying that a crowded catalog retains every namespace name, allows discovery of a late namespace with its full description, and keeps the summary unchanged on follow-up. GitOrigin-RevId: 9743cda5aa14190db22c788c9e91e1785a32682b --- ...sts__snapshots_description_truncation.snap | 28 ++ ...__tests__snapshots_namespace_omission.snap | 74 +++++ .../core/src/context/world_state/tools.rs | 61 ++-- .../src/context/world_state/tools_budget.rs | 117 ++++++++ .../context/world_state/tools_budget_tests.rs | 152 ++++++++++ .../src/context/world_state/tools_tests.rs | 284 +++++++++++++++++- codex-rs/core/tests/suite/scenarios.rs | 3 + .../suite/scenarios_tools_namespace_budget.rs | 157 ++++++++++ ...ace_budget__names_before_descriptions.snap | 45 +++ 9 files changed, 884 insertions(+), 37 deletions(-) create mode 100644 codex-rs/core/src/context/world_state/snapshots/codex_core__context__world_state__tools__tests__snapshots_description_truncation.snap create mode 100644 codex-rs/core/src/context/world_state/snapshots/codex_core__context__world_state__tools__tests__snapshots_namespace_omission.snap create mode 100644 codex-rs/core/src/context/world_state/tools_budget.rs create mode 100644 codex-rs/core/src/context/world_state/tools_budget_tests.rs create mode 100644 codex-rs/core/tests/suite/scenarios_tools_namespace_budget.rs create mode 100644 codex-rs/core/tests/suite/snapshots/all__suite__scenarios__tools_namespace_budget__names_before_descriptions.snap diff --git a/codex-rs/core/src/context/world_state/snapshots/codex_core__context__world_state__tools__tests__snapshots_description_truncation.snap b/codex-rs/core/src/context/world_state/snapshots/codex_core__context__world_state__tools__tests__snapshots_description_truncation.snap new file mode 100644 index 0000000000..8412689aec --- /dev/null +++ b/codex-rs/core/src/context/world_state/snapshots/codex_core__context__world_state__tools__tests__snapshots_description_truncation.snap @@ -0,0 +1,28 @@ +--- +source: core/src/context/world_state/tools_tests.rs +expression: "render_section_cases(&[(PreviousSectionState::Absent,\nPreviousSectionState::Known(&tools),)])" +--- +Absent -> {"project_00_documents":"Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up.","project_01_documents":"Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up.","project_02_documents":"Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up.","project_03_documents":"Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up.","project_04_documents":"Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up.","project_05_documents":"Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up.","project_06_documents":"Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up.","project_07_documents":"Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up.","project_08_documents":"Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up.","project_09_documents":"Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up.","project_10_documents":"Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up.","project_11_documents":"Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up.","project_12_documents":"Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up.","project_13_documents":"Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up.","project_14_documents":"Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up.","project_15_documents":"Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up.","project_16_documents":"Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up.","project_17_documents":"Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up.","project_18_documents":"Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up.","project_19_documents":"Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up."} (role - developer) + +Deferred tool namespaces: +- project_00_documents: Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their cont... +- project_01_documents: Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their cont... +- project_02_documents: Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their cont... +- project_03_documents: Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their cont... +- project_04_documents: Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their cont... +- project_05_documents: Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their cont... +- project_06_documents: Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their cont... +- project_07_documents: Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their cont... +- project_08_documents: Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their cont... +- project_09_documents: Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their cont... +- project_10_documents: Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their cont... +- project_11_documents: Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their cont... +- project_12_documents: Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their cont... +- project_13_documents: Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their cont... +- project_14_documents: Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their con... +- project_15_documents: Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their con... +- project_16_documents: Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their con... +- project_17_documents: Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their con... +- project_18_documents: Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their con... +- project_19_documents: Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their con... + diff --git a/codex-rs/core/src/context/world_state/snapshots/codex_core__context__world_state__tools__tests__snapshots_namespace_omission.snap b/codex-rs/core/src/context/world_state/snapshots/codex_core__context__world_state__tools__tests__snapshots_namespace_omission.snap new file mode 100644 index 0000000000..4644f334a6 --- /dev/null +++ b/codex-rs/core/src/context/world_state/snapshots/codex_core__context__world_state__tools__tests__snapshots_namespace_omission.snap @@ -0,0 +1,74 @@ +--- +source: core/src/context/world_state/tools_tests.rs +expression: "render_section_cases(&[(PreviousSectionState::Absent,\nPreviousSectionState::Known(&tools),)])" +--- +Absent -> {"mcp__codex_apps__workspace_00_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_01_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_02_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_03_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_04_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_05_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_06_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_07_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_08_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_09_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_10_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_11_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_12_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_13_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_14_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_15_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_16_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_17_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_18_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_19_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_20_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_21_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_22_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_23_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_24_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_25_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_26_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_27_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_28_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_29_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_30_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_31_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_32_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_33_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_34_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_35_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_36_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_37_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_38_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_39_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_40_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_41_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_42_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_43_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_44_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_45_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_46_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_47_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_48_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_49_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_50_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_51_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_52_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_53_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_54_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_55_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_56_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_57_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_58_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_59_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_60_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_61_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_62_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_63_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_64_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_65_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_66_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_67_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_68_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_69_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_70_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_71_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_72_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_73_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_74_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_75_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_76_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_77_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_78_project_documents_and_search":"Search project documents.","mcp__codex_apps__workspace_79_project_documents_and_search":"Search project documents."} (role - developer) + +Deferred tool namespaces: +- mcp__codex_apps__workspace_00_project_documents_and_search +- mcp__codex_apps__workspace_01_project_documents_and_search +- mcp__codex_apps__workspace_02_project_documents_and_search +- mcp__codex_apps__workspace_03_project_documents_and_search +- mcp__codex_apps__workspace_04_project_documents_and_search +- mcp__codex_apps__workspace_05_project_documents_and_search +- mcp__codex_apps__workspace_06_project_documents_and_search +- mcp__codex_apps__workspace_07_project_documents_and_search +- mcp__codex_apps__workspace_08_project_documents_and_search +- mcp__codex_apps__workspace_09_project_documents_and_search +- mcp__codex_apps__workspace_10_project_documents_and_search +- mcp__codex_apps__workspace_11_project_documents_and_search +- mcp__codex_apps__workspace_12_project_documents_and_search +- mcp__codex_apps__workspace_13_project_documents_and_search +- mcp__codex_apps__workspace_14_project_documents_and_search +- mcp__codex_apps__workspace_15_project_documents_and_search +- mcp__codex_apps__workspace_16_project_documents_and_search +- mcp__codex_apps__workspace_17_project_documents_and_search +- mcp__codex_apps__workspace_18_project_documents_and_search +- mcp__codex_apps__workspace_19_project_documents_and_search +- mcp__codex_apps__workspace_20_project_documents_and_search +- mcp__codex_apps__workspace_21_project_documents_and_search +- mcp__codex_apps__workspace_22_project_documents_and_search +- mcp__codex_apps__workspace_23_project_documents_and_search +- mcp__codex_apps__workspace_24_project_documents_and_search +- mcp__codex_apps__workspace_25_project_documents_and_search +- mcp__codex_apps__workspace_26_project_documents_and_search +- mcp__codex_apps__workspace_27_project_documents_and_search +- mcp__codex_apps__workspace_28_project_documents_and_search +- mcp__codex_apps__workspace_29_project_documents_and_search +- mcp__codex_apps__workspace_30_project_documents_and_search +- mcp__codex_apps__workspace_31_project_documents_and_search +- mcp__codex_apps__workspace_32_project_documents_and_search +- mcp__codex_apps__workspace_33_project_documents_and_search +- mcp__codex_apps__workspace_34_project_documents_and_search +- mcp__codex_apps__workspace_35_project_documents_and_search +- mcp__codex_apps__workspace_36_project_documents_and_search +- mcp__codex_apps__workspace_37_project_documents_and_search +- mcp__codex_apps__workspace_38_project_documents_and_search +- mcp__codex_apps__workspace_39_project_documents_and_search +- mcp__codex_apps__workspace_40_project_documents_and_search +- mcp__codex_apps__workspace_41_project_documents_and_search +- mcp__codex_apps__workspace_42_project_documents_and_search +- mcp__codex_apps__workspace_43_project_documents_and_search +- mcp__codex_apps__workspace_44_project_documents_and_search +- mcp__codex_apps__workspace_45_project_documents_and_search +- mcp__codex_apps__workspace_46_project_documents_and_search +- mcp__codex_apps__workspace_47_project_documents_and_search +- mcp__codex_apps__workspace_48_project_documents_and_search +- mcp__codex_apps__workspace_49_project_documents_and_search +- mcp__codex_apps__workspace_50_project_documents_and_search +- mcp__codex_apps__workspace_51_project_documents_and_search +- mcp__codex_apps__workspace_52_project_documents_and_search +- mcp__codex_apps__workspace_53_project_documents_and_search +- mcp__codex_apps__workspace_54_project_documents_and_search +- mcp__codex_apps__workspace_55_project_documents_and_search +- mcp__codex_apps__workspace_56_project_documents_and_search +- mcp__codex_apps__workspace_57_project_documents_and_search +- mcp__codex_apps__workspace_58_project_documents_and_search +- mcp__codex_apps__workspace_59_project_documents_and_search +- mcp__codex_apps__workspace_60_project_documents_and_search +- mcp__codex_apps__workspace_61_project_documents_and_search +- mcp__codex_apps__workspace_62_project_documents_and_search +- mcp__codex_apps__workspace_63_project_documents_and_search +- mcp__codex_apps__workspace_64_project_documents_and_search +... 15 additional namespaces omitted. + diff --git a/codex-rs/core/src/context/world_state/tools.rs b/codex-rs/core/src/context/world_state/tools.rs index 2751d3fe1d..7d206b9a03 100644 --- a/codex-rs/core/src/context/world_state/tools.rs +++ b/codex-rs/core/src/context/world_state/tools.rs @@ -1,8 +1,12 @@ +//! Bounded deferred namespace summaries that reserve names before descriptions. +//! Rendering allocations do not change the persisted namespace snapshot. + +use self::budget::DESCRIPTION_TRUNCATION_SUFFIX; +use self::budget::truncate_namespace_rows; use super::PreviousSectionState; use super::WorldStateContextFragment; use super::WorldStateSection; use crate::context::ContextualUserFragment; -use crate::context::environment_context::push_xml_escaped_text; use codex_extension_api::ExtensionMetrics; use codex_extension_api::RenderedWorldStateFragment; use codex_otel::CONTEXT_FRAGMENT_BYTES_BUCKETS; @@ -14,6 +18,9 @@ use codex_protocol::protocol::TOOLS_OPEN_TAG; use std::collections::BTreeMap; use std::sync::Arc; +#[path = "tools_budget.rs"] +mod budget; + const MAX_RENDERED_FRAGMENT_BYTES: usize = 4 * 1024; const MAX_NAMESPACE_DESCRIPTION_CHARS: usize = 250; const OMITTED_LINE_RESERVE_BYTES: usize = 64; @@ -44,14 +51,27 @@ impl ToolsState { deferred_namespaces: deferred_namespaces .into_iter() .map(|(namespace, description)| { - let description = description + let mut characters = description .lines() .next() .unwrap_or_default() .trim() - .chars() + .chars(); + let mut description = characters + .by_ref() .take(MAX_NAMESPACE_DESCRIPTION_CHARS) - .collect(); + .collect::(); + if characters.next().is_some() { + let prefix_bytes = description + .char_indices() + .nth( + MAX_NAMESPACE_DESCRIPTION_CHARS + - DESCRIPTION_TRUNCATION_SUFFIX.len(), + ) + .map_or(description.len(), |(index, _)| index); + description.truncate(prefix_bytes); + description.push_str(DESCRIPTION_TRUNCATION_SUFFIX); + } (namespace, description) }) .collect(), @@ -141,10 +161,17 @@ fn render_namespace_groups( + groups .iter() .filter(|(_, namespaces)| !namespaces.is_empty()) - .map(|(label, _)| label.len() + ":\n".len() + OMITTED_LINE_RESERVE_BYTES) + .map(|(label, _)| label.len() + ":\n".len()) .sum::() + empty_state.map_or(0, str::len); - let mut remaining_entry_bytes = body_budget.saturating_sub(fixed_bytes); + let entry_budget = body_budget.saturating_sub(fixed_bytes); + let omission_reserve_bytes = groups + .iter() + .filter(|(_, namespaces)| !namespaces.is_empty()) + .count() + * OMITTED_LINE_RESERVE_BYTES; + let mut entries = + truncate_namespace_rows(groups, entry_budget, omission_reserve_bytes).into_iter(); let mut rendered = "\n".to_string(); let mut before = FragmentSize { namespaces: 0, @@ -161,11 +188,12 @@ fn render_namespace_groups( before.namespaces += namespaces.len(); before.bytes += label.len() + ":\n".len(); let mut omitted = 0usize; - for (namespace, description) in *namespaces { - let entry = rendered_namespace(namespace, description); - before.bytes += entry.len(); - if entry.len() <= remaining_entry_bytes { - remaining_entry_bytes -= entry.len(); + for ((namespace, description), entry) in namespaces.iter().zip(entries.by_ref()) { + before.bytes += "- ".len() + namespace.len() + "\n".len(); + if !description.is_empty() { + before.bytes += ": ".len() + description.len(); + } + if let Some(entry) = entry { rendered.push_str(&entry); } else { omitted += 1; @@ -191,17 +219,6 @@ fn render_namespace_groups( } } -fn rendered_namespace(namespace: &str, description: &str) -> String { - let mut rendered = "- ".to_string(); - push_xml_escaped_text(&mut rendered, namespace); - if !description.is_empty() { - rendered.push_str(": "); - push_xml_escaped_text(&mut rendered, description); - } - rendered.push('\n'); - rendered -} - // Measures the rendered namespace block before/after the byte cap, after description normalization. fn record_fragment_metrics( metrics: &dyn ExtensionMetrics, diff --git a/codex-rs/core/src/context/world_state/tools_budget.rs b/codex-rs/core/src/context/world_state/tools_budget.rs new file mode 100644 index 0000000000..f645406db9 --- /dev/null +++ b/codex-rs/core/src/context/world_state/tools_budget.rs @@ -0,0 +1,117 @@ +//! Truncates borrowed namespace text within a shared byte budget. +//! Character iterators preserve UTF-8 boundaries and stable input order without +//! per-character tables. Callers account for headings and format omission notices. + +use std::collections::BTreeMap; +use std::collections::VecDeque; + +pub(super) const DESCRIPTION_TRUNCATION_SUFFIX: &str = "..."; + +/// Returns complete rows in group order, with `None` for omitted names. +/// The budget excludes headings; omission space is charged only when names overflow. +pub(super) fn truncate_namespace_rows( + groups: &[(&str, &BTreeMap)], + byte_budget: usize, + omission_reserve_bytes: usize, +) -> Vec> { + let entries = groups + .iter() + .flat_map(|(_, namespaces)| namespaces.iter()) + .map(|(namespace, description)| (namespace.as_str(), description.as_str())) + .collect::>(); + let retained_description_bytes = + calculate_namespace_row_truncation(&entries, byte_budget, omission_reserve_bytes); + entries + .into_iter() + .zip(retained_description_bytes) + .map(|((namespace, description), retained_description_bytes)| { + retained_description_bytes.map(|retained_description_bytes| { + if retained_description_bytes == 0 { + format!("- {namespace}\n") + } else if retained_description_bytes == description.len() { + format!("- {namespace}: {description}\n") + } else if let Some(prefix_bytes) = + retained_description_bytes.checked_sub(DESCRIPTION_TRUNCATION_SUFFIX.len()) + { + let prefix = &description[..description.floor_char_boundary(prefix_bytes)]; + format!("- {namespace}: {prefix}{DESCRIPTION_TRUNCATION_SUFFIX}\n") + } else { + format!("- {namespace}\n") + } + }) + }) + .collect() +} + +/// Reserves all names before sharing description space in character-round order. +/// `None` omits a row; `Some(0)` keeps its name; `Some(n)` keeps n description bytes. +fn calculate_namespace_row_truncation( + entries: &[(&str, &str)], + byte_budget: usize, + omission_reserve_bytes: usize, +) -> Vec> { + let total_name_bytes = entries + .iter() + .map(|(namespace, _)| "- ".len() + namespace.len() + "\n".len()) + .sum::(); + if total_name_bytes > byte_budget { + let mut remaining_bytes = byte_budget.saturating_sub(omission_reserve_bytes); + return entries + .iter() + .map(|(namespace, _)| { + let name_only_bytes = "- ".len() + namespace.len() + "\n".len(); + if name_only_bytes <= remaining_bytes { + remaining_bytes -= name_only_bytes; + Some(0) + } else { + None + } + }) + .collect(); + } + + let mut remaining_bytes = byte_budget - total_name_bytes; + let total_description_bytes = entries + .iter() + .filter(|(_, description)| !description.is_empty()) + .map(|(_, description)| ": ".len() + description.len()) + .sum::(); + if total_description_bytes <= remaining_bytes { + return entries + .iter() + .map(|(_, description)| Some(description.len())) + .collect(); + } + + let mut retained_description_bytes = vec![Some(0); entries.len()]; + if remaining_bytes == 0 { + return retained_description_bytes; + } + let mut active_rows = VecDeque::with_capacity(entries.len()); + active_rows.extend( + entries + .iter() + .enumerate() + .filter(|(_, (_, description))| !description.is_empty()) + .map(|(row_index, (_, description))| (row_index, description.char_indices())), + ); + // Each visit considers one character. Exhausted rows and prefixes that cannot + // fit leave the queue permanently, because the remaining budget only shrinks. + while let Some((row_index, mut characters)) = active_rows.pop_front() { + let Some((byte_index, ch)) = characters.next() else { + continue; + }; + let character_bytes = ch.len_utf8(); + let extra_bytes = character_bytes + if byte_index == 0 { ": ".len() } else { 0 }; + if extra_bytes <= remaining_bytes { + remaining_bytes -= extra_bytes; + retained_description_bytes[row_index] = Some(byte_index + character_bytes); + active_rows.push_back((row_index, characters)); + } + } + retained_description_bytes +} + +#[cfg(test)] +#[path = "tools_budget_tests.rs"] +mod tests; diff --git a/codex-rs/core/src/context/world_state/tools_budget_tests.rs b/codex-rs/core/src/context/world_state/tools_budget_tests.rs new file mode 100644 index 0000000000..f9c6f1a80e --- /dev/null +++ b/codex-rs/core/src/context/world_state/tools_budget_tests.rs @@ -0,0 +1,152 @@ +//! Coverage for name-first row retention and whole-character description budgets. + +use super::calculate_namespace_row_truncation; +use super::truncate_namespace_rows; +use pretty_assertions::assert_eq; +use std::collections::BTreeMap; + +#[test] +fn truncates_raw_rows_at_character_boundaries_in_group_order() { + let first = BTreeMap::from([ + ("πŸ¦€".to_string(), "&πŸ¦€x".to_string()), + ("&".to_string(), "πŸ¦€&y".to_string()), + ]); + let empty = BTreeMap::new(); + let last = BTreeMap::from([("b".to_string(), "x<&".to_string())]); + let groups = [("first", &first), ("empty", &empty), ("last", &last)]; + let cases = [ + ( + 36, + 64, + [ + Some("- &: πŸ¦€&y\n"), + Some("- πŸ¦€: &πŸ¦€x\n"), + Some("- b: x<&\n"), + ], + ), + ( + 32, + 64, + [Some("- &: ...\n"), Some("- πŸ¦€: &...\n"), Some("- b\n")], + ), + (15, 64, [Some("- &\n"), Some("- πŸ¦€\n"), Some("- b\n")]), + (14, 6, [Some("- &\n"), None, Some("- b\n")]), + ]; + + for (byte_budget, omission_reserve_bytes, expected) in cases { + assert_eq!( + truncate_namespace_rows(&groups, byte_budget, omission_reserve_bytes), + expected + .into_iter() + .map(|row| row.map(str::to_owned)) + .collect::>(), + "byte budget: {byte_budget}, omission reserve bytes: {omission_reserve_bytes}" + ); + } +} + +#[test] +fn marks_truncated_descriptions_within_the_allocated_bytes() { + let cases = [ + ("abcdef", 4, "- n\n"), + ("abcdef", 7, "- n\n"), + ("abcdef", 8, "- n\n"), + ("abcdef", 9, "- n: ...\n"), + ("abcdef", 10, "- n: a...\n"), + ("abcdef", 12, "- n: abcdef\n"), + ("ab", 8, "- n: ab\n"), + ("πŸ¦€abcdef", 10, "- n: ...\n"), + ("πŸ¦€abcdef", 13, "- n: πŸ¦€...\n"), + ("Γ©abcdef", 11, "- n: Γ©...\n"), + ]; + + for (description, byte_budget, expected) in cases { + let namespaces = BTreeMap::from([("n".to_string(), description.to_string())]); + let rows = truncate_namespace_rows( + &[("namespaces", &namespaces)], + byte_budget, + /*omission_reserve_bytes*/ 64, + ); + assert_eq!( + rows, + vec![Some(expected.to_string())], + "description: {description:?}, byte budget: {byte_budget}" + ); + assert!(rows.iter().flatten().map(String::len).sum::() <= byte_budget); + } +} + +#[test] +fn allocates_complete_description_prefixes_in_stable_round_robin_order() { + // Budgets include separators; retained byte offsets refer only to descriptions. + let cases = [ + (vec![], 9, vec![]), + (vec!["", ""], 9, vec![0, 0]), + (vec!["", "ab", ""], 4, vec![0, 2, 0]), + (vec!["a", "πŸ¦€"], 0, vec![0, 0]), + (vec!["a", "πŸ¦€"], 2, vec![0, 0]), + (vec!["Γ©β‚¬πŸ¦€", "xy"], 20, vec![9, 2]), + (vec!["Γ©β‚¬πŸ¦€", "xy"], 12, vec![5, 2]), + (vec!["ab", "cd"], 7, vec![2, 1]), + (vec!["a", "bcdef"], 9, vec![1, 4]), + (vec!["aπŸ¦€x", "bcdef"], 9, vec![1, 4]), + (vec!["πŸ¦€x", "abc"], 5, vec![0, 3]), + (vec!["πŸ¦€πŸ¦€x", "β‚¬πŸ¦€y"], 18, vec![9, 3]), + (vec!["πŸ¦€πŸ¦€x", "β‚¬πŸ¦€y"], 19, vec![8, 7]), + ]; + + for (descriptions, description_byte_budget, expected) in cases { + let entries = descriptions + .iter() + .map(|description| ("n", *description)) + .collect::>(); + assert_eq!( + calculate_namespace_row_truncation( + &entries, + description_byte_budget + 4 * entries.len(), + /*omission_reserve_bytes*/ 64 + ), + expected.into_iter().map(Some).collect::>(), + "descriptions: {descriptions:?}, description byte budget: {description_byte_budget}" + ); + } +} + +#[test] +fn retains_names_before_descriptions_and_reserves_omissions_only_on_overflow() { + let cases = [ + // Every name is retained before any description consumes the budget. + ( + vec![("a", "abcd"), ("b", "x")], + 11, + 64, + vec![Some(1), Some(0)], + ), + // Names alone exactly fit, so the omission reserve is unnecessary. + (vec![("a", ""), ("b", "")], 8, 64, vec![Some(0), Some(0)]), + // Charging the reserve skips the second name; spare bytes stay unused. + ( + vec![("aaaa", "x"), ("bbbb", "x"), ("c", "x")], + 15, + 3, + vec![Some(0), None, Some(0)], + ), + // An oversized name cannot prevent later complete names from fitting. + ( + vec![("oversized_name_aaa", "x"), ("a", "x"), ("b", "x")], + 14, + 2, + vec![None, Some(0), Some(0)], + ), + (vec![("ab", "x"), ("c", "")], 0, 2, vec![None, None]), + (vec![("a", "x"), ("b", "x")], 6, 64, vec![None, None]), + ]; + + for (entries, byte_budget, omission_reserve_bytes, expected) in cases { + assert_eq!( + calculate_namespace_row_truncation(&entries, byte_budget, omission_reserve_bytes), + expected, + "entries: {entries:?}, byte budget: {byte_budget}, omission reserve bytes: {omission_reserve_bytes}" + ); + } +} diff --git a/codex-rs/core/src/context/world_state/tools_tests.rs b/codex-rs/core/src/context/world_state/tools_tests.rs index 0e3ff92374..2321daa0dc 100644 --- a/codex-rs/core/src/context/world_state/tools_tests.rs +++ b/codex-rs/core/src/context/world_state/tools_tests.rs @@ -3,6 +3,7 @@ use super::MAX_RENDERED_FRAGMENT_BYTES; use super::ToolsState; use crate::context::world_state::PreviousSectionState; use crate::context::world_state::WorldStateSection; +use crate::context::world_state::test_support::render_section_cases; use codex_extension_api::ExtensionMetrics; use codex_otel::THREAD_TOOLS_FRAGMENT_BYTES_METRIC; use codex_otel::THREAD_TOOLS_NAMESPACES_TOTAL_METRIC; @@ -11,6 +12,43 @@ use std::collections::BTreeMap; use std::sync::Arc; use std::sync::Mutex; +#[test] +fn snapshots_description_truncation() { + let description = "Search project documents by title, owner, date, or status. Read matching documents with their summaries, source links, and recent activity. Create documents, update their contents, and organize results into collections for follow-up."; + let tools = ToolsState::new( + (0..20).map(|index| { + ( + format!("project_{index:02}_documents"), + description.to_string(), + ) + }), + Arc::new(RecordingMetrics::default()), + ); + + insta::assert_snapshot!(render_section_cases(&[( + PreviousSectionState::Absent, + PreviousSectionState::Known(&tools), + )])); +} + +#[test] +fn snapshots_namespace_omission() { + let tools = ToolsState::new( + (0..80).map(|index| { + ( + format!("mcp__codex_apps__workspace_{index:02}_project_documents_and_search"), + "Search project documents.".to_string(), + ) + }), + Arc::new(RecordingMetrics::default()), + ); + + insta::assert_snapshot!(render_section_cases(&[( + PreviousSectionState::Absent, + PreviousSectionState::Known(&tools), + )])); +} + #[test] fn renders_first_line_of_namespace_descriptions() { let tools = ToolsState::new( @@ -35,7 +73,7 @@ fn renders_first_line_of_namespace_descriptions() { assert_eq!( rendered, - "\nDeferred tool namespaces:\n- app: control the Codex App\n- gmail: access your Google Gmail Account & labels\n- hotline\n" + "\nDeferred tool namespaces:\n- app: control the Codex App\n- gmail: access your Google Gmail Account & labels\n- hotline\n" ); } @@ -87,23 +125,39 @@ fn renders_added_removed_and_updated_namespace_descriptions() { #[test] fn caps_namespace_descriptions_by_character_count() { - let description = "πŸ¦€".repeat(MAX_NAMESPACE_DESCRIPTION_CHARS + 1); + let exact_description = "πŸ¦€".repeat(MAX_NAMESPACE_DESCRIPTION_CHARS); + let capped_description = format!("{}...", "πŸ¦€".repeat(MAX_NAMESPACE_DESCRIPTION_CHARS - 3)); let tools = ToolsState::new( - [("app".to_string(), description)], + [ + ("exact".to_string(), exact_description.clone()), + ( + "over".to_string(), + "πŸ¦€".repeat(MAX_NAMESPACE_DESCRIPTION_CHARS + 1), + ), + ], Arc::new(RecordingMetrics::default()), ); assert_eq!( tools.snapshot(), - BTreeMap::from([( - "app".to_string(), - "πŸ¦€".repeat(MAX_NAMESPACE_DESCRIPTION_CHARS) - )]) + BTreeMap::from([ + ("exact".to_string(), exact_description.clone()), + ("over".to_string(), capped_description.clone()), + ]) + ); + assert_eq!( + tools + .render_diff(PreviousSectionState::Absent) + .expect("tools state should render") + .render(), + format!( + "\nDeferred tool namespaces:\n- exact: {exact_description}\n- over: {capped_description}\n" + ) ); } #[test] -fn caps_rendered_tools_fragment_after_xml_escaping() { +fn records_fragment_metrics_after_description_normalization_and_truncation() { let metrics = Arc::new(RecordingMetrics::default()); let namespaces = (0..100) .map(|index| { @@ -115,12 +169,7 @@ fn caps_rendered_tools_fragment_after_xml_escaping() { .collect::>(); let normalized_entries = namespaces .keys() - .map(|name| { - format!( - "- {name}: {}η•Œ&<>\n", - "η•Œ&<>'"".repeat(41) - ) - }) + .map(|name| format!("- {name}: {}η•Œ...\n", "η•Œ&<>'\"".repeat(41))) .collect::(); // Before the byte cap, after first-line/250-character description normalization. let before = format!("\nDeferred tool namespaces:\n{normalized_entries}"); @@ -131,17 +180,222 @@ fn caps_rendered_tools_fragment_after_xml_escaping() { .render(); assert!(rendered.len() <= MAX_RENDERED_FRAGMENT_BYTES); - assert!(rendered.contains(" additional namespaces omitted.\n")); let included = rendered .lines() .filter(|line| line.starts_with("- ")) .count(); + assert_eq!(included, 100); assert_eq!( metrics.take(), expected_metrics("snapshot", (100, &before), (included, &rendered)) ); } +#[test] +fn retains_all_names_before_sharing_description_bytes() { + let tools = ToolsState::new( + (0..100).map(|index| { + ( + format!("namespace_{index}"), + "&".repeat(MAX_NAMESPACE_DESCRIPTION_CHARS), + ) + }), + Arc::new(RecordingMetrics::default()), + ); + + let rendered = tools + .render_diff(PreviousSectionState::Absent) + .expect("tools state should render") + .render(); + + let entries = tools + .snapshot() + .keys() + .enumerate() + .map(|(index, namespace)| { + // Name-only rows leave 2,564 bytes: 200 separator bytes, 300 ellipsis + // bytes, and 20 ampersands per namespace, plus one for the first 64. + let description = "&".repeat(if index < 64 { 21 } else { 20 }); + format!("- {namespace}: {description}...\n") + }) + .collect::(); + + assert_eq!( + (rendered.len(), rendered), + ( + MAX_RENDERED_FRAGMENT_BYTES, + format!("\nDeferred tool namespaces:\n{entries}") + ) + ); +} + +#[test] +fn retains_names_that_fit_without_reserving_an_omission_notice() { + let namespaces = (0..80) + .map(|index| format!("service_{index:02}_{}", "x".repeat(/*n*/ 36))) + .collect::>(); + let tools = ToolsState::new( + namespaces + .iter() + .map(|namespace| (namespace.clone(), String::new())), + Arc::new(RecordingMetrics::default()), + ); + let rendered = tools + .render_diff(PreviousSectionState::Absent) + .expect("tools state should render") + .render(); + let entries = namespaces + .iter() + .map(|namespace| format!("- {namespace}\n")) + .collect::(); + + assert_eq!( + (rendered.len(), rendered), + ( + 4042, + format!("\nDeferred tool namespaces:\n{entries}") + ) + ); +} + +#[test] +fn preserves_raw_names_and_complete_unicode_at_the_budget_boundary() { + // Name-only rows leave 14 bytes. Each description gets five bytes, leaving + // two for its UTF-8 prefix after reserving the ellipsis. The second prefix + // cannot fit a crab, so it renders only the ellipsis. + let namespace = format!("a{}", "<".repeat(/*n*/ 4032)); + let tools = ToolsState::new( + [ + (namespace.clone(), "&πŸ¦€x".to_string()), + ("z".to_string(), "πŸ¦€&y".to_string()), + ], + Arc::new(RecordingMetrics::default()), + ); + + let rendered = tools + .render_diff(PreviousSectionState::Absent) + .expect("tools state should render") + .render(); + + assert_eq!( + (rendered.len(), rendered), + ( + MAX_RENDERED_FRAGMENT_BYTES - 3, + format!("\nDeferred tool namespaces:\n- {namespace}: &...\n- z: ...\n") + ) + ); +} + +#[test] +fn redistributes_description_space_after_short_and_empty_descriptions() { + // The names leave 12 bytes: three for `: X`, nine for `: abcd...`. + let namespace = "a".repeat(/*n*/ 4031); + let tools = ToolsState::new( + [ + (namespace.clone(), "abcdefgh".to_string()), + ("b".to_string(), "X".to_string()), + ("c".to_string(), String::new()), + ], + Arc::new(RecordingMetrics::default()), + ); + + let rendered = tools + .render_diff(PreviousSectionState::Absent) + .expect("tools state should render") + .render(); + + assert_eq!( + rendered, + format!( + "\nDeferred tool namespaces:\n- {namespace}: abcd...\n- b: X\n- c\n" + ) + ); +} + +#[test] +fn reserves_names_and_shares_descriptions_across_added_and_removed_groups() { + // Both groups' names leave 15 bytes, shared between descriptions and ellipses. + let namespace = "a".repeat(/*n*/ 3992); + let tools = ToolsState::new( + [(namespace.clone(), "AAAAAAAA".to_string())], + Arc::new(RecordingMetrics::default()), + ); + let previous = BTreeMap::from([("z".to_string(), "ZZZZZZZZ".to_string())]); + + let rendered = tools + .render_diff(PreviousSectionState::Known(&previous)) + .expect("tools state delta should render") + .render(); + + assert_eq!( + rendered, + format!( + "\nAdded deferred tool namespaces:\n- {namespace}: AAA...\nRemoved deferred tool namespaces:\n- z: ZZ...\n" + ) + ); +} + +#[test] +fn omits_whole_names_per_group_only_after_dropping_every_description() { + let retained_namespace = "b".repeat(/*n*/ 3870); + let tools = ToolsState::new( + [ + ("a".repeat(/*n*/ 4000), "oversized namespace".to_string()), + (retained_namespace.clone(), "large namespace".to_string()), + ( + "c".repeat(/*n*/ 100), + "another omitted namespace".to_string(), + ), + ("d".to_string(), "short namespace".to_string()), + ], + Arc::new(RecordingMetrics::default()), + ); + let previous = BTreeMap::from([ + ( + "y".repeat(/*n*/ 100), + "omitted removed namespace".to_string(), + ), + ("z".to_string(), "short removed namespace".to_string()), + ]); + + let rendered = tools + .render_diff(PreviousSectionState::Known(&previous)) + .expect("tools state delta should render") + .render(); + + assert_eq!( + rendered, + format!( + "\nAdded deferred tool namespaces:\n- {retained_namespace}\n- d\n... 2 additional namespaces omitted.\nRemoved deferred tool namespaces:\n- z\n... 1 additional namespaces omitted.\n" + ) + ); +} + +#[test] +fn reserves_the_empty_state_notice_when_all_namespaces_are_removed() { + // The names and empty-state notice leave 15 bytes for descriptions and ellipses. + let namespace = "a".repeat(/*n*/ 3988); + let previous = BTreeMap::from([ + (namespace.clone(), "long namespace".to_string()), + ("z".to_string(), "short namespace".to_string()), + ]); + + let rendered = ToolsState::new([], Arc::new(RecordingMetrics::default())) + .render_diff(PreviousSectionState::Known(&previous)) + .expect("final tools state delta should render") + .render(); + + assert_eq!( + (rendered.len(), rendered), + ( + MAX_RENDERED_FRAGMENT_BYTES, + format!( + "\nRemoved deferred tool namespaces:\n- {namespace}: lon...\n- z: sh...\nNo deferred tool namespaces remain.\n" + ) + ) + ); +} + #[derive(Default)] struct RecordingMetrics { samples: Mutex>, diff --git a/codex-rs/core/tests/suite/scenarios.rs b/codex-rs/core/tests/suite/scenarios.rs index 7bc392ced1..a6f3493639 100644 --- a/codex-rs/core/tests/suite/scenarios.rs +++ b/codex-rs/core/tests/suite/scenarios.rs @@ -113,6 +113,9 @@ mod shared_instructions; #[path = "scenarios_mxc.rs"] mod mxc; +#[path = "scenarios_tools_namespace_budget.rs"] +mod tools_namespace_budget; + fn skills_extensions() -> Arc> { let mut extensions = ExtensionRegistryBuilder::::new(); install(&mut extensions, |config: &Config| SkillsExtensionConfig { diff --git a/codex-rs/core/tests/suite/scenarios_tools_namespace_budget.rs b/codex-rs/core/tests/suite/scenarios_tools_namespace_budget.rs new file mode 100644 index 0000000000..5ba8d5ba5b --- /dev/null +++ b/codex-rs/core/tests/suite/scenarios_tools_namespace_budget.rs @@ -0,0 +1,157 @@ +//! Deferred namespace summaries keep names discoverable when descriptions exceed the budget. + +use anyhow::Result; +use codex_core::StartThreadOptions; +use codex_features::Feature; +use codex_protocol::dynamic_tools::DynamicToolFunctionSpec; +use codex_protocol::dynamic_tools::DynamicToolNamespaceSpec; +use codex_protocol::dynamic_tools::DynamicToolNamespaceTool; +use codex_protocol::dynamic_tools::DynamicToolSpec; +use core_test_support::apps_test_server::configure_search_capable_model; +use core_test_support::context_snapshot; +use core_test_support::context_snapshot::ContextSnapshotOptions; +use core_test_support::responses; +use core_test_support::skip_if_no_network; +use core_test_support::test_codex::test_codex; +use pretty_assertions::assert_eq; +use serde_json::json; + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn descriptions_share_space_without_hiding_late_namespaces() -> Result<()> { + skip_if_no_network!(Ok(())); + + let server = responses::start_mock_server().await; + let description = "Find records & retrieve
from this service. Search by project, owner, date, or status, then read the matching record to answer questions with its title, summary, source link, and relevant activity from the connected workspace."; + let namespaces = (0..19) + .map(|index| format!("service_{index:02}")) + .chain(std::iter::once("zulu_travel".to_string())) + .collect::>(); + let input_schema = json!({"type": "object", "properties": {}, "additionalProperties": false}); + let dynamic_tools = namespaces + .iter() + .map(|name| { + DynamicToolSpec::Namespace(DynamicToolNamespaceSpec { + name: name.clone(), + description: description.to_string(), + tools: vec![DynamicToolNamespaceTool::Function( + DynamicToolFunctionSpec { + name: "search_records".to_string(), + description: "Search records in this service.".to_string(), + input_schema: input_schema.clone(), + defer_loading: true, + }, + )], + }) + }) + .collect(); + let mut test = test_codex() + .with_config(|config| { + super::configure_scenario_catalog(config); + configure_search_capable_model(config); + config.agents_enabled = false; + config + .features + .enable(Feature::DeferredToolWorldState) + .expect("enable deferred namespace summaries"); + }) + .build_with_auto_env(&server) + .await?; + let thread = test + .thread_manager + .start_thread(StartThreadOptions { + dynamic_tools, + environments: Some(vec![test.executor_environment().selection().clone()]), + ..StartThreadOptions::new(test.config.clone()) + }) + .await?; + test.codex = thread.thread; + test.session_configured = thread.session_configured; + + let mock = responses::mount_sse_sequence( + &server, + vec![ + responses::sse(vec![ + responses::ev_tool_search_call( + "find-travel", + &json!({"query": "zulu_travel", "limit": 1}), + ), + responses::ev_completed("search"), + ]), + responses::sse(vec![ + responses::ev_assistant_message( + "found", + "Zulu Travel provides a record search tool.", + ), + responses::ev_completed("found"), + ]), + responses::sse(vec![ + responses::ev_assistant_message("follow-up", "The same services are available."), + responses::ev_completed("follow-up"), + ]), + ], + ) + .await; + test.submit_turn("Find the record search tool for Zulu Travel.") + .await?; + test.submit_turn("Are the same services still available?") + .await?; + + let requests = mock.requests(); + assert_eq!(requests.len(), 3); + let tools_sections = requests + .iter() + .map(|request| { + request + .message_input_texts("developer") + .into_iter() + .filter(|text| text.starts_with("")) + .collect::>() + }) + .collect::>(); + let [fragment] = tools_sections[0].as_slice() else { + panic!("expected one initial tools fragment"); + }; + assert!(fragment.len() <= 4096); + let advertised = fragment + .lines() + .filter_map(|line| line.strip_prefix("- ")) + .map(|line| line.split_once(": ").expect("nonempty description")) + .collect::>(); + assert_eq!( + advertised.iter().map(|(name, _)| *name).collect::>(), + namespaces.iter().map(String::as_str).collect::>(), + ); + assert!(advertised.iter().all(|(_, shortened)| { + shortened.strip_suffix("...").is_some_and(|prefix| { + !prefix.is_empty() + && prefix.len() < description.len() + && description.starts_with(prefix) + }) + })); + assert_eq!(tools_sections, vec![vec![fragment.clone()]; 3]); + assert_eq!( + requests[1].tool_search_output("find-travel")["tools"], + json!([{ + "type": "namespace", + "name": "zulu_travel", + "description": description, + "tools": [{ + "type": "function", + "name": "search_records", + "description": "Search records in this service.", + "strict": false, + "defer_loading": true, + "parameters": input_schema, + }], + }]), + ); + insta::assert_snapshot!( + "names_before_descriptions", + context_snapshot::format_request_history_snapshot( + "A crowded tool catalog retains every namespace, discovers a late namespace, and stays unchanged on follow-up.", + &requests, + &ContextSnapshotOptions::default().rewrite_known_segments(), + ) + ); + Ok(()) +} diff --git a/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__tools_namespace_budget__names_before_descriptions.snap b/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__tools_namespace_budget__names_before_descriptions.snap new file mode 100644 index 0000000000..4dbe7e0712 --- /dev/null +++ b/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__tools_namespace_budget__names_before_descriptions.snap @@ -0,0 +1,45 @@ +--- +source: core/tests/suite/scenarios_tools_namespace_budget.rs +expression: "context_snapshot::format_request_history_snapshot(\"A crowded tool catalog retains every namespace, discovers a late namespace, and stays unchanged on follow-up.\",\n&requests, &ContextSnapshotOptions::default().rewrite_known_segments(),)" +--- +Scenario: A crowded tool catalog retains every namespace, discovers a late namespace, and stays unchanged on follow-up. + +## Window 1 +-- request 1 (turn) -- +00:message/developer[2]: + [01] + [02] + Deferred tool namespaces: + - service_00: Find records & retrieve
from this service. Search by project, owner, date, or st... questions with its title, summary, source link, a... [hash=0ab3fdffc78b8a7e] + - service_01: Find records & retrieve
from this service. Search by project, owner, date, or st... questions with its title, summary, source link, a... [hash=8c11455aa17451d3] + - service_02: Find records & retrieve
from this service. Search by project, owner, date, or st... questions with its title, summary, source link, a... [hash=ba7bd9b7d9c593ac] + - service_03: Find records & retrieve
from this service. Search by project, owner, date, or st... questions with its title, summary, source link, a... [hash=1fd3c09fb04ffc39] + - service_04: Find records & retrieve
from this service. Search by project, owner, date, or st... questions with its title, summary, source link, a... [hash=00fdda43bcbcaf1a] + - service_05: Find records & retrieve
from this service. Search by project, owner, date, or st... questions with its title, summary, source link, a... [hash=e0cec0a267c572ef] + - service_06: Find records & retrieve
from this service. Search by project, owner, date, or st... questions with its title, summary, source link, a... [hash=1ce1fc0c63651118] + - service_07: Find records & retrieve
from this service. Search by project, owner, date, or st... questions with its title, summary, source link, a... [hash=3855dbcad5307b95] + - service_08: Find records & retrieve
from this service. Search by project, owner, date, or st... questions with its title, summary, source link, a... [hash=dd43e13ba10dae06] + - service_09: Find records & retrieve
from this service. Search by project, owner, date, or st... questions with its title, summary, source link, a... [hash=770d71c9fa136b7b] + - service_10: Find records & retrieve
from this service. Search by project, owner, date, or st... questions with its title, summary, source link, a... [hash=4f09ad3c23876b69] + - service_11: Find records & retrieve
from this service. Search by project, owner, date, or st... questions with its title, summary, source link, a... [hash=201bfaf907ccdd5c] + - service_12: Find records & retrieve
from this service. Search by project, owner, date, or st... questions with its title, summary, source link, a... [hash=fd05df37ef3bb583] + - service_13: Find records & retrieve
from this service. Search by project, owner, date, or st...r questions with its title, summary, source link, ... [hash=9013ae8c9f38b0ab] + - service_14: Find records & retrieve
from this service. Search by project, owner, date, or st...r questions with its title, summary, source link, ... [hash=58780ba86e1698fe] + - service_15: Find records & retrieve
from this service. Search by project, owner, date, or st...r questions with its title, summary, source link, ... [hash=54ea65e72647fd19] + - service_16: Find records & retrieve
from this service. Search by project, owner, date, or st...r questions with its title, summary, source link, ... [hash=5252ec987dd85214] + - service_17: Find records & retrieve
from this service. Search by project, owner, date, or st...r questions with its title, summary, source link, ... [hash=d6e058a14552297f] + - service_18: Find records & retrieve
from this service. Search by project, owner, date, or st...r questions with its title, summary, source link, ... [hash=ef78c2b1fa9afde2] + - zulu_travel: Find records & retrieve
from this service. Search by project, owner, date, or s...r questions with its title, summary, source link, ... [hash=8f37122c26e42e38] + +01:message/user: + +02:message/user: + Find the record search tool for Zulu Travel. +-- request 2 (turn) -- +03:tool_search_call +04:tool_search_output +-- request 3 (turn) -- +05:message/assistant: + Zulu Travel provides a record search tool. +06:message/user: + Are the same services still available?