mirror of
https://github.com/openai/codex.git
synced 2026-11-23 23:10:47 +00:00
## Why Namespace-wide guidance can repeat on every entry in flat Code Mode listings. The existing prefix limit covers only reachable `functions` tools, leaving indirect namespace guidance outside the budget. ## What changed - Add `builtin_tool_namespaces` to put guidance on namespace headers in Code Mode prompts and tool search responses, including headers shared with client tools. The selector applies to namespaces with a registered built-in tool and is omitted from flat listings. Overlapping selectors must agree after trimming. - Validate all four prefix maps together against the selected model's `description_prefix_budget_bytes`, which defaults to 512. Count each trimmed value once in UTF-8 bytes, including unused selectors. Exclude keys, paragraph separators, and rendered copies. ## Testing Cover shared headers, flat listings, and conflicting selectors. Exercise the default budget boundary with multibyte text and verify that a model can raise the limit. GitOrigin-RevId: 1570c23ea1dbad6041cc8e48d0dc2b77f92596c7
301 lines
11 KiB
Rust
301 lines
11 KiB
Rust
//! Checks prefix normalization and rendering without exercising tool execution.
|
|
|
|
use super::*;
|
|
use crate::ResponsesApiNamespaceTool;
|
|
use codex_code_mode::CodeModeToolKind;
|
|
use codex_protocol::openai_models::IndirectDescriptionPrefixes;
|
|
use codex_protocol::openai_models::ToolMessages;
|
|
use pretty_assertions::assert_eq;
|
|
use serde_json::json;
|
|
|
|
#[test]
|
|
fn plain_tools_join_an_existing_functions_group_only_with_a_prefix() {
|
|
let original_tools = [
|
|
ToolName::plain("plain"),
|
|
ToolName::namespaced("functions", "namespaced"),
|
|
]
|
|
.map(|tool_name| ToolDefinition {
|
|
name: tool_name.name.clone(),
|
|
tool_name,
|
|
description: "Tool guidance.".to_string(),
|
|
kind: CodeModeToolKind::Function,
|
|
input_schema: None,
|
|
input_schema_max_bytes: None,
|
|
output_schema: None,
|
|
});
|
|
let original_namespaces = BTreeMap::from([(
|
|
"functions".to_string(),
|
|
ToolNamespaceDescription {
|
|
name: "functions".to_string(),
|
|
description: "Original namespace guidance.".to_string(),
|
|
},
|
|
)]);
|
|
let render = |tools: &[ToolDefinition],
|
|
namespaces: &BTreeMap<String, ToolNamespaceDescription>| {
|
|
codex_code_mode::build_exec_tool_description_and_definitions(
|
|
tools,
|
|
&[],
|
|
namespaces,
|
|
codex_code_mode::DEFAULT_EXEC_YIELD_TIME_MS,
|
|
/*code_mode_only*/ true,
|
|
codex_code_mode::ImageDetailVisibility::Visible,
|
|
codex_code_mode::DeferredToolDiscovery::Catalog,
|
|
/*incremental_exec_enabled*/ false,
|
|
/*messages*/ None,
|
|
)
|
|
.0
|
|
};
|
|
let original = render(&original_tools, &original_namespaces);
|
|
for messages in [
|
|
json!({}),
|
|
json!({"indirect_description_prefixes": null}),
|
|
json!({"indirect_description_prefixes": {}}),
|
|
json!({"indirect_description_prefixes": {"namespaces": null, "mcp_servers": null}}),
|
|
json!({"indirect_description_prefixes": {"namespaces": {}, "mcp_servers": {}}}),
|
|
json!({"indirect_description_prefixes": {"namespaces": {"functions": " \n"}}}),
|
|
json!({"indirect_description_prefixes": {
|
|
"namespaces": {"unavailable": "Unused guidance."},
|
|
}}),
|
|
] {
|
|
let configured: ToolMessages = serde_json::from_value(messages).unwrap();
|
|
let mut tools = original_tools.clone();
|
|
let mut namespaces = original_namespaces.clone();
|
|
IndirectNamespacePrefixes::new(configured.indirect_description_prefixes.as_ref(), [], [])
|
|
.unwrap()
|
|
.apply_exec_prompt(&mut tools, &mut namespaces);
|
|
assert_eq!(render(&tools, &namespaces), original);
|
|
}
|
|
let configured = IndirectDescriptionPrefixes {
|
|
namespaces: Some(BTreeMap::from([(
|
|
"functions".to_string(),
|
|
"Catalog guidance.".to_string(),
|
|
)])),
|
|
..Default::default()
|
|
};
|
|
let mut tools = original_tools.clone();
|
|
let mut namespaces = original_namespaces;
|
|
IndirectNamespacePrefixes::new(Some(&configured), [], [])
|
|
.unwrap()
|
|
.apply_exec_prompt(&mut tools, &mut namespaces);
|
|
let rendered = render(&tools, &namespaces);
|
|
assert_eq!(rendered.matches("## functions").count(), 1);
|
|
assert!(rendered.contains("## functions\nCatalog guidance.\n\nOriginal namespace guidance."));
|
|
assert!(rendered.find("## functions").unwrap() < rendered.find("### `plain`").unwrap());
|
|
|
|
// A client function and a built-in can share the default namespace.
|
|
let builtin_only = IndirectDescriptionPrefixes {
|
|
builtin_tool_namespaces: configured.namespaces.clone(),
|
|
..Default::default()
|
|
};
|
|
let inventory = [&original_tools[0].tool_name];
|
|
let prefixes = IndirectNamespacePrefixes::new(Some(&builtin_only), [], inventory).unwrap();
|
|
let mut tools = original_tools.clone();
|
|
let mut namespaces = BTreeMap::new();
|
|
prefixes.apply_exec_prompt(&mut tools, &mut namespaces);
|
|
assert_eq!(namespaces["functions"].description, "Catalog guidance.");
|
|
for (tool, original) in tools.iter().zip(&original_tools) {
|
|
assert_eq!(tool.description, original.description);
|
|
}
|
|
let mut flat = original_tools.clone();
|
|
prefixes.apply_code_mode(&mut flat);
|
|
assert_eq!(flat, original_tools);
|
|
|
|
// Search coalesces both owners under one header. Both member descriptions are preserved.
|
|
let mut search = [LoadableToolSpec::Namespace(crate::ResponsesApiNamespace {
|
|
name: "functions".into(),
|
|
description: "Shared namespace.".into(),
|
|
tools: original_tools
|
|
.iter()
|
|
.map(|tool| {
|
|
ResponsesApiNamespaceTool::Function(crate::ResponsesApiTool {
|
|
name: tool.tool_name.name.clone(),
|
|
description: tool.description.clone(),
|
|
strict: false,
|
|
defer_loading: Some(true),
|
|
parameters: crate::JsonSchema::object(
|
|
BTreeMap::new(),
|
|
/*required*/ None,
|
|
Some(false.into()),
|
|
),
|
|
output_schema: None,
|
|
})
|
|
})
|
|
.collect(),
|
|
})];
|
|
let mut expected = search.clone();
|
|
let LoadableToolSpec::Namespace(namespace) = &mut expected[0] else {
|
|
unreachable!()
|
|
};
|
|
namespace.description = "Catalog guidance.\n\nShared namespace.".into();
|
|
prefixes.apply_search(&mut search);
|
|
assert_eq!(search, expected);
|
|
|
|
let mut shared = builtin_only.clone();
|
|
let server_namespaces = [("calendar", "functions")];
|
|
shared.mcp_servers = Some(BTreeMap::from([(
|
|
"calendar".into(),
|
|
" Catalog guidance. ".into(),
|
|
)]));
|
|
let prefixes =
|
|
IndirectNamespacePrefixes::new(Some(&shared), server_namespaces, inventory).unwrap();
|
|
let mut namespaces = BTreeMap::new();
|
|
let mut tools = original_tools.clone();
|
|
prefixes.apply_exec_prompt(&mut tools, &mut namespaces);
|
|
assert_eq!(namespaces["functions"].description, "Catalog guidance.");
|
|
shared.mcp_servers = Some(BTreeMap::from([(
|
|
"calendar".into(),
|
|
"Different guidance.".into(),
|
|
)]));
|
|
assert_eq!(
|
|
IndirectNamespacePrefixes::new(Some(&shared), server_namespaces, inventory)
|
|
.err()
|
|
.expect("conflicting contributors")
|
|
.to_string(),
|
|
"Conflicting indirect description prefixes for namespace `functions`: `mcp_servers.calendar` and `builtin_tool_namespaces.functions`",
|
|
);
|
|
|
|
let overlapping = IndirectDescriptionPrefixes {
|
|
namespaces: Some(BTreeMap::from([("functions".into(), " ".into())])),
|
|
..builtin_only
|
|
};
|
|
assert_eq!(
|
|
IndirectNamespacePrefixes::new(Some(&overlapping), [], inventory)
|
|
.err()
|
|
.expect("explicit empty prefix conflicts")
|
|
.to_string(),
|
|
"Conflicting indirect description prefixes for namespace `functions`: `namespaces.functions` and `builtin_tool_namespaces.functions`"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn flat_tool_descriptions_use_trimmed_namespace_prefixes() {
|
|
let configured = IndirectDescriptionPrefixes {
|
|
namespaces: Some(BTreeMap::from([(
|
|
"functions".to_string(),
|
|
" \nFunction guidance.\t ".to_string(),
|
|
)])),
|
|
..Default::default()
|
|
};
|
|
let original = ToolDefinition {
|
|
name: "inspect".to_string(),
|
|
tool_name: ToolName::plain("inspect"),
|
|
description: "Inspect.".to_string(),
|
|
kind: CodeModeToolKind::Function,
|
|
input_schema: None,
|
|
input_schema_max_bytes: None,
|
|
output_schema: None,
|
|
};
|
|
let mut tools = [original.clone()];
|
|
IndirectNamespacePrefixes::new(Some(&configured), [], [])
|
|
.unwrap()
|
|
.apply_code_mode(&mut tools);
|
|
assert_eq!(
|
|
tools,
|
|
[ToolDefinition {
|
|
description: "Function guidance.\n\nInspect.".to_string(),
|
|
..original
|
|
}]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn matching_prefixes_cover_each_namespace_once() {
|
|
let servers = BTreeMap::from([
|
|
("codex_apps".to_string(), " App guidance. ".to_string()),
|
|
("apps_alias".to_string(), "App guidance.\n".to_string()),
|
|
]);
|
|
let exact = BTreeMap::from([(
|
|
"mcp__codex_apps__gmail".to_string(),
|
|
"App guidance.".to_string(),
|
|
)]);
|
|
let configured = IndirectDescriptionPrefixes {
|
|
namespaces: Some(exact),
|
|
mcp_servers: Some(servers),
|
|
..Default::default()
|
|
};
|
|
let original = ["gmail", "calendar", "drive", "slack"].map(|app| ToolDefinition {
|
|
name: format!("mcp__codex_apps__{app}__search"),
|
|
tool_name: ToolName::namespaced(format!("mcp__codex_apps__{app}"), "search"),
|
|
description: "Search.".to_string(),
|
|
kind: CodeModeToolKind::Function,
|
|
input_schema: None,
|
|
input_schema_max_bytes: None,
|
|
output_schema: None,
|
|
});
|
|
let mut tools = original.clone();
|
|
let namespaces = original
|
|
.iter()
|
|
.map(|tool| ("codex_apps", tool.tool_name.namespace.as_deref().unwrap()))
|
|
.chain([
|
|
("codex_apps", "mcp__codex_apps__gmail"),
|
|
("apps_alias", "mcp__codex_apps__gmail"),
|
|
]);
|
|
IndirectNamespacePrefixes::new(Some(&configured), namespaces, [])
|
|
.unwrap()
|
|
.apply_code_mode(&mut tools);
|
|
let expected = original
|
|
.into_iter()
|
|
.map(|tool| ToolDefinition {
|
|
description: "App guidance.\n\nSearch.".to_string(),
|
|
..tool
|
|
})
|
|
.collect::<Vec<_>>();
|
|
assert_eq!(tools.as_slice(), expected);
|
|
}
|
|
|
|
#[test]
|
|
fn conflicting_namespace_and_server_prefixes_report_keys_without_values() {
|
|
for (namespace_prefix, server_prefix) in [
|
|
("Namespace guidance.", "Server guidance."),
|
|
("", "Server guidance."),
|
|
("Namespace guidance.", " \n"),
|
|
] {
|
|
let configured = IndirectDescriptionPrefixes {
|
|
namespaces: Some(BTreeMap::from([(
|
|
"mcp__reports_one".to_string(),
|
|
namespace_prefix.to_string(),
|
|
)])),
|
|
mcp_servers: Some(BTreeMap::from([(
|
|
"reports.one".to_string(),
|
|
server_prefix.to_string(),
|
|
)])),
|
|
..Default::default()
|
|
};
|
|
let error = IndirectNamespacePrefixes::new(
|
|
Some(&configured),
|
|
[("reports.one", "mcp__reports_one")],
|
|
[],
|
|
)
|
|
.err()
|
|
.expect("conflicting prefixes")
|
|
.to_string();
|
|
assert_eq!(
|
|
error,
|
|
"Conflicting indirect description prefixes for namespace `mcp__reports_one`: `namespaces.mcp__reports_one` and `mcp_servers.reports.one`",
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn conflicting_mcp_servers_report_the_shared_namespace() {
|
|
let configured = IndirectDescriptionPrefixes {
|
|
mcp_servers: Some(BTreeMap::from([
|
|
("reports.one".to_string(), "First guidance.".to_string()),
|
|
("reports.two".to_string(), "Second guidance.".to_string()),
|
|
])),
|
|
..Default::default()
|
|
};
|
|
let error = IndirectNamespacePrefixes::new(
|
|
Some(&configured),
|
|
[("reports.one", "shared"), ("reports.two", "shared")],
|
|
[],
|
|
)
|
|
.err()
|
|
.expect("conflicting prefixes")
|
|
.to_string();
|
|
assert_eq!(
|
|
error,
|
|
"Conflicting indirect description prefixes for namespace `shared`: `mcp_servers.reports.one` and `mcp_servers.reports.two`",
|
|
);
|
|
}
|