mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
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
This commit is contained in:
+28
File diff suppressed because one or more lines are too long
+74
File diff suppressed because one or more lines are too long
@@ -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::<String>();
|
||||
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::<usize>()
|
||||
+ 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,
|
||||
|
||||
@@ -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<String, String>)],
|
||||
byte_budget: usize,
|
||||
omission_reserve_bytes: usize,
|
||||
) -> Vec<Option<String>> {
|
||||
let entries = groups
|
||||
.iter()
|
||||
.flat_map(|(_, namespaces)| namespaces.iter())
|
||||
.map(|(namespace, description)| (namespace.as_str(), description.as_str()))
|
||||
.collect::<Vec<_>>();
|
||||
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<Option<usize>> {
|
||||
let total_name_bytes = entries
|
||||
.iter()
|
||||
.map(|(namespace, _)| "- ".len() + namespace.len() + "\n".len())
|
||||
.sum::<usize>();
|
||||
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::<usize>();
|
||||
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;
|
||||
@@ -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::<Vec<_>>(),
|
||||
"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::<usize>() <= 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::<Vec<_>>();
|
||||
assert_eq!(
|
||||
calculate_namespace_row_truncation(
|
||||
&entries,
|
||||
description_byte_budget + 4 * entries.len(),
|
||||
/*omission_reserve_bytes*/ 64
|
||||
),
|
||||
expected.into_iter().map(Some).collect::<Vec<_>>(),
|
||||
"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}"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
"<tools>\nDeferred tool namespaces:\n- app: control the Codex App\n- gmail: access your Google Gmail Account & labels\n- hotline\n</tools>"
|
||||
"<tools>\nDeferred tool namespaces:\n- app: control the Codex App\n- gmail: access your Google Gmail Account & labels\n- hotline\n</tools>"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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!(
|
||||
"<tools>\nDeferred tool namespaces:\n- exact: {exact_description}\n- over: {capped_description}\n</tools>"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[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::<BTreeMap<_, _>>();
|
||||
let normalized_entries = namespaces
|
||||
.keys()
|
||||
.map(|name| {
|
||||
format!(
|
||||
"- {name}: {}界&<>\n",
|
||||
"界&<>'"".repeat(41)
|
||||
)
|
||||
})
|
||||
.map(|name| format!("- {name}: {}界...\n", "界&<>'\"".repeat(41)))
|
||||
.collect::<String>();
|
||||
// Before the byte cap, after first-line/250-character description normalization.
|
||||
let before = format!("<tools>\nDeferred tool namespaces:\n{normalized_entries}</tools>");
|
||||
@@ -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::<String>();
|
||||
|
||||
assert_eq!(
|
||||
(rendered.len(), rendered),
|
||||
(
|
||||
MAX_RENDERED_FRAGMENT_BYTES,
|
||||
format!("<tools>\nDeferred tool namespaces:\n{entries}</tools>")
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[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::<Vec<_>>();
|
||||
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::<String>();
|
||||
|
||||
assert_eq!(
|
||||
(rendered.len(), rendered),
|
||||
(
|
||||
4042,
|
||||
format!("<tools>\nDeferred tool namespaces:\n{entries}</tools>")
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[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!("<tools>\nDeferred tool namespaces:\n- {namespace}: &...\n- z: ...\n</tools>")
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[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!(
|
||||
"<tools>\nDeferred tool namespaces:\n- {namespace}: abcd...\n- b: X\n- c\n</tools>"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[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!(
|
||||
"<tools>\nAdded deferred tool namespaces:\n- {namespace}: AAA...\nRemoved deferred tool namespaces:\n- z: ZZ...\n</tools>"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[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!(
|
||||
"<tools>\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</tools>"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[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!(
|
||||
"<tools>\nRemoved deferred tool namespaces:\n- {namespace}: lon...\n- z: sh...\nNo deferred tool namespaces remain.\n</tools>"
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct RecordingMetrics {
|
||||
samples: Mutex<BTreeMap<(String, String, String), i64>>,
|
||||
|
||||
Reference in New Issue
Block a user