mirror of
https://github.com/openai/codex.git
synced 2026-11-24 23:20:47 +00:00
Allow model overrides for incremental Code Mode tool messages (#60188)
## What changed Add `exec_tools_update_hint` and `exec_tools_removed_definitions_header` to `model_messages.tools.incremental_tools` and use them when rendering Code Mode catalog updates and removals. Overrides are literal strings limited to 512 UTF-8 bytes per field. Missing, null, or oversized values use the existing bundled wording. Empty strings omit the guidance while retaining changed definitions and removal keys. Wording changes alone do not trigger catalog updates. ## Testing Extend unit coverage for defaults, null fields, independent fallback, and UTF-8 byte limits. Add scenario assertions and a snapshot covering custom and empty wording, model switches, catalog changes, and restoration after compaction. GitOrigin-RevId: da9e00fdb5197a2b5fa39ce533ba87c76ff3e82c
This commit is contained in:
@@ -39,9 +39,6 @@ const ACTION_ADDED: &str = "added";
|
||||
const ACTION_REMOVED: &str = "removed";
|
||||
const ACTION_SCHEMA_CHANGED: &str = "schema_changed";
|
||||
|
||||
const EXEC_TOOLS_UPDATE_HINT: &str = "Use these definitions and namespace instructions with `functions.exec`. Each replaces its earlier version in full. Unlisted tools keep their latest definitions and `tools` methods.";
|
||||
const EXEC_TOOLS_REMOVED_DEFINITIONS_HEADER: &str = "Listed `Tool:` entries are unavailable until redefined or rediscovered. `Namespace instructions:` removes guidance only. Unlisted entries are unchanged.";
|
||||
|
||||
/// The exact serialized Responses Lite declarations visible in one captured step.
|
||||
pub(crate) struct TopLevelToolsState {
|
||||
messages: ResolvedIncrementalToolMessages,
|
||||
@@ -391,7 +388,8 @@ impl TopLevelToolsState {
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
if !changed.is_empty() {
|
||||
let update = render_exec_update(&changed);
|
||||
let update =
|
||||
render_exec_update(&changed, &self.messages.exec_tools_update_hint);
|
||||
if let Some(metrics) = self.metrics.as_deref() {
|
||||
let added = changed
|
||||
.iter()
|
||||
@@ -458,7 +456,11 @@ impl TopLevelToolsState {
|
||||
);
|
||||
}
|
||||
updates.push(
|
||||
WorldStateUpdate::fragment(RemovedExecDefinitions(removed)).standalone(),
|
||||
WorldStateUpdate::fragment(RemovedExecDefinitions {
|
||||
header: self.messages.exec_tools_removed_definitions_header.clone(),
|
||||
names: removed,
|
||||
})
|
||||
.standalone(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -550,19 +552,28 @@ impl ContextualUserFragment for RemovedTools {
|
||||
}
|
||||
|
||||
/// Render a nonempty batch of complete replacement definitions with the exec update hint.
|
||||
fn render_exec_update(definitions: &[&ExecDefinition]) -> IncrementalToolsUpdate {
|
||||
fn render_exec_update(definitions: &[&ExecDefinition], hint: &str) -> IncrementalToolsUpdate {
|
||||
let description = definitions
|
||||
.iter()
|
||||
.map(|entry| entry.description.as_str())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n\n");
|
||||
IncrementalToolsUpdate(format!(
|
||||
"{EXEC_TOOLS_UPDATE_HINT}\n\n## Namespace: functions\n{description}"
|
||||
))
|
||||
let mut text = hint.to_owned();
|
||||
if !text.is_empty() {
|
||||
text.push_str("\n\n");
|
||||
}
|
||||
text.push_str("## Namespace: functions\n");
|
||||
text.push_str(&description);
|
||||
IncrementalToolsUpdate(text)
|
||||
}
|
||||
|
||||
/// Removal notice containing the stable keys of withdrawn tools or namespace instructions.
|
||||
struct RemovedExecDefinitions(Vec<String>);
|
||||
struct RemovedExecDefinitions {
|
||||
/// Guidance preceding the removed keys; an empty header emits only the keys.
|
||||
header: String,
|
||||
/// Stable entry keys that are no longer present in the current catalog.
|
||||
names: Vec<String>,
|
||||
}
|
||||
|
||||
impl ContextualUserFragment for RemovedExecDefinitions {
|
||||
fn role(&self) -> &'static str {
|
||||
@@ -582,10 +593,13 @@ impl ContextualUserFragment for RemovedExecDefinitions {
|
||||
}
|
||||
|
||||
fn body(&self) -> ContentItem {
|
||||
input_text(format!(
|
||||
"{EXEC_TOOLS_REMOVED_DEFINITIONS_HEADER}\n- {}",
|
||||
self.0.join("\n- ")
|
||||
))
|
||||
let mut text = self.header.clone();
|
||||
if !text.is_empty() {
|
||||
text.push('\n');
|
||||
}
|
||||
text.push_str("- ");
|
||||
text.push_str(&self.names.join("\n- "));
|
||||
input_text(text)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,9 +5,11 @@ use crate::context::world_state::test_support::render_section_cases;
|
||||
use crate::context_manager::updates::merge_world_state_updates;
|
||||
use crate::session::extension_metrics::from_session_telemetry;
|
||||
use codex_code_mode::ExecDefinition;
|
||||
use codex_models_manager::model_info::model_info_from_slug;
|
||||
use codex_otel::MetricsClient;
|
||||
use codex_otel::MetricsConfig;
|
||||
use codex_otel::SessionTelemetry;
|
||||
use codex_prompts::ResolvedModelMessages;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::protocol::SessionSource;
|
||||
use opentelemetry_sdk::metrics::InMemoryMetricExporter;
|
||||
@@ -840,15 +842,25 @@ fn hidden_exec_is_not_reintroduced_and_returning_exec_restores_full_catalog() {
|
||||
role: "developer".to_string(),
|
||||
tools: Arc::from(vec![original]),
|
||||
},
|
||||
ContextualUserFragment::into(RemovedExecDefinitions(
|
||||
vec!["Tool: tools.a".to_string(),]
|
||||
)),
|
||||
ContextualUserFragment::into(RemovedExecDefinitions {
|
||||
header: ResolvedIncrementalToolMessages::default()
|
||||
.exec_tools_removed_definitions_header,
|
||||
names: vec!["Tool: tools.a".to_string()],
|
||||
}),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn changed_and_added_entries_share_one_update_and_removed_entries_are_withdrawn() {
|
||||
#[test_case::test_case(json!({}), None, None; "bundled")]
|
||||
#[test_case::test_case(json!({"exec_tools_update_hint": null, "exec_tools_removed_definitions_header": null}), None, None; "null_fields")]
|
||||
#[test_case::test_case(json!({"exec_tools_update_hint": "é".repeat(257), "exec_tools_removed_definitions_header": "Catalog removals:"}), None, Some("Catalog removals:".to_string()); "oversized_update_preserves_removal")]
|
||||
#[test_case::test_case(json!({"exec_tools_update_hint": "Catalog update.", "exec_tools_removed_definitions_header": "é".repeat(257)}), Some("Catalog update.".to_string()), None; "oversized_removal_preserves_update")]
|
||||
#[test_case::test_case(json!({"exec_tools_update_hint": "é".repeat(256), "exec_tools_removed_definitions_header": "🐈".repeat(128)}), Some("é".repeat(256)), Some("🐈".repeat(128)); "utf8_byte_limit")]
|
||||
fn changed_and_added_entries_share_one_update_and_removed_entries_are_withdrawn(
|
||||
overrides: Value,
|
||||
expected_update_hint: Option<String>,
|
||||
expected_removal_header: Option<String>,
|
||||
) {
|
||||
let definition = exec_declaration("Shared MCP Types:", "grammar");
|
||||
let original = TopLevelToolsState::new(
|
||||
/*messages*/ Default::default(),
|
||||
@@ -871,8 +883,11 @@ fn changed_and_added_entries_share_one_update_and_removed_entries_are_withdrawn(
|
||||
.0
|
||||
.unwrap();
|
||||
|
||||
let mut model = model_info_from_slug("test-model");
|
||||
model.model_messages =
|
||||
Some(serde_json::from_value(json!({"tools": {"incremental_tools": overrides}})).unwrap());
|
||||
let current = TopLevelToolsState::new(
|
||||
/*messages*/ Default::default(),
|
||||
ResolvedModelMessages::from_model(&model).incremental_tools(),
|
||||
vec![definition],
|
||||
/*metrics*/ None,
|
||||
)
|
||||
@@ -890,15 +905,20 @@ fn changed_and_added_entries_share_one_update_and_removed_entries_are_withdrawn(
|
||||
},
|
||||
])));
|
||||
let (snapshot, updates) = current.render_diff(PreviousSectionState::Known(&baseline));
|
||||
let defaults = ResolvedIncrementalToolMessages::default();
|
||||
let expected_update_hint = expected_update_hint.unwrap_or(defaults.exec_tools_update_hint);
|
||||
let expected_removal_header =
|
||||
expected_removal_header.unwrap_or(defaults.exec_tools_removed_definitions_header);
|
||||
assert_eq!(
|
||||
merge_world_state_updates(updates),
|
||||
vec![
|
||||
ContextualUserFragment::into(IncrementalToolsUpdate(
|
||||
"Use these definitions and namespace instructions with `functions.exec`. Each replaces its earlier version in full. Unlisted tools keep their latest definitions and `tools` methods.\n\n## Namespace: functions\n### a\nChanged a.\ndeclare const tools: { a(value: number): Promise<number> };\n\n### c\ndeclare const tools: { c(): Promise<boolean> };".to_string(),
|
||||
)),
|
||||
ContextualUserFragment::into(RemovedExecDefinitions(vec![
|
||||
"Tool: tools.b".to_string(),
|
||||
])),
|
||||
ContextualUserFragment::into(IncrementalToolsUpdate(format!(
|
||||
"{expected_update_hint}\n\n## Namespace: functions\n### a\nChanged a.\ndeclare const tools: {{ a(value: number): Promise<number> }};\n\n### c\ndeclare const tools: {{ c(): Promise<boolean> }};",
|
||||
))),
|
||||
ContextualUserFragment::into(RemovedExecDefinitions {
|
||||
header: expected_removal_header,
|
||||
names: vec!["Tool: tools.b".to_string()],
|
||||
}),
|
||||
]
|
||||
);
|
||||
assert!(
|
||||
|
||||
Reference in New Issue
Block a user