Deduplicate model catalog strings in memory (#63735)

## Why

Identical instructions and tool descriptions can appear in multiple
model catalog records. Each decode or clone previously allocated its own
copy of that text.

## What changed

Use `CodexCachedStr` for catalog instructions, messages, and tool
overrides. It stores short strings inline and interns longer strings so
independent decodes and clones can share storage. Resolved incremental
tool messages also reuse cached text from overrides and bundled defaults.
JSON and generated TypeScript still represent these values as strings.

## Testing

Add tests for shared storage across catalog fields, independent decodes,
clones, and incremental tool defaults. Cover plain JSON string round
trips and borrowed access to short inline UTF-8 strings.

GitOrigin-RevId: 6f53b4e19f01f6ed52a8fd364279b924cf794be2
This commit is contained in:
Adam Perry @ OpenAI
2026-11-24 20:21:47 +00:00
committed by copyberry
parent 7a24022794
commit 9a3c131eb4
91 changed files with 799 additions and 508 deletions
@@ -55,7 +55,7 @@ async fn api_key_discovery_flag_controls_fetches_and_cached_catalogs() {
expected_model
.model_messages
.get_or_insert_default()
.instructions_template = overrides.base_instructions.clone();
.instructions_template = overrides.base_instructions.as_deref().map(Into::into);
assert_eq!(
manager
.get_model_info(&bundled_model.slug, &overrides)
+9 -7
View File
@@ -8,6 +8,7 @@ use codex_protocol::openai_models::TruncationMode;
use codex_protocol::openai_models::TruncationPolicyConfig;
use codex_protocol::openai_models::WebSearchToolType;
use codex_protocol::openai_models::default_input_modalities;
use codex_utils_cached_str::CodexCachedStr;
use crate::config::ModelsManagerConfig;
use codex_utils_output_truncation::approx_bytes_for_tokens;
@@ -45,20 +46,20 @@ pub fn with_config_overrides(mut model: ModelInfo, config: &ModelsManagerConfig)
if let Some(base_instructions) = &config.base_instructions {
let model_messages = model.model_messages.get_or_insert_default();
model_messages.instructions_template = Some(base_instructions.clone());
model_messages.instructions_template = Some(base_instructions.as_str().into());
} else if config.personality == Some(Personality::None)
&& let Some(instructions_template) = model
.model_messages
.as_mut()
.and_then(|messages| messages.instructions_template.as_mut())
{
*instructions_template = strip_personality_section(std::mem::take(instructions_template));
strip_personality_section(instructions_template);
}
model
}
fn strip_personality_section(mut instructions: String) -> String {
fn strip_personality_section(instructions: &mut CodexCachedStr) {
let mut section_start = None;
let mut section_end = None;
let mut offset = 0;
@@ -81,10 +82,11 @@ fn strip_personality_section(mut instructions: String) -> String {
if let Some(section_start) = section_start {
let section_end = section_end.unwrap_or(instructions.len());
instructions.replace_range(section_start..section_end, "");
let mut text = String::with_capacity(instructions.len() - (section_end - section_start));
text.push_str(&instructions[..section_start]);
text.push_str(&instructions[section_end..]);
*instructions = text.into();
}
instructions
}
fn is_h1_heading(line: &str) -> bool {
@@ -150,7 +152,7 @@ pub fn model_info_from_slug(slug: &str) -> ModelInfo {
fn local_model_messages() -> ModelMessages {
ModelMessages {
instructions_template: Some(BASE_INSTRUCTIONS.to_string()),
instructions_template: Some(BASE_INSTRUCTIONS.into()),
..Default::default()
}
}
+41 -41
View File
@@ -33,86 +33,86 @@ fn base_instruction_override_is_literal_and_preserves_catalog_messages() {
let async_message_description = "Catalog async message description.";
let mut model = model_info_from_slug("unknown-model");
let approvals = ApprovalMessages {
on_request: Some("user approvals".to_string()),
on_request_auto_review: Some("auto approvals".to_string()),
never: Some("never approvals".to_string()),
unless_trusted: Some("unless-trusted approvals".to_string()),
on_request: Some("user approvals".into()),
on_request_auto_review: Some("auto approvals".into()),
never: Some("never approvals".into()),
unless_trusted: Some("unless-trusted approvals".into()),
granular: Some(GranularApprovalMessages {
intro: Some("Catalog: {{ policy }}".to_string()),
prompted_categories: Some("Allowed: {{ categories }}".to_string()),
rejected_categories: Some("Denied: {{ categories }}".to_string()),
intro: Some("Catalog: {{ policy }}".into()),
prompted_categories: Some("Allowed: {{ categories }}".into()),
rejected_categories: Some("Denied: {{ categories }}".into()),
}),
..Default::default()
};
let collaboration_modes = CollaborationModeMessages {
initial: Some("initial instructions".to_string()),
default: Some("default instructions".to_string()),
plan: Some("plan instructions".to_string()),
initial: Some("initial instructions".into()),
default: Some("default instructions".into()),
plan: Some("plan instructions".into()),
};
let auto_review = AutoReviewMessages {
policy: Some("review policy".to_string()),
policy_template: Some("review policy template".to_string()),
policy: Some("review policy".into()),
policy_template: Some("review policy template".into()),
node_repl_policy: None,
rejection_instructions: Some("rejection instructions".to_string()),
timeout_instructions: Some(String::new()),
rejection_instructions: Some("rejection instructions".into()),
timeout_instructions: Some("".into()),
};
let permissions = PermissionMessages {
danger_full_access: Some("danger".to_string()),
workspace_write: Some(String::new()),
danger_full_access: Some("danger".into()),
workspace_write: Some("".into()),
read_only: None,
approved_prefixes: None,
..PermissionMessages::default()
};
let multi_agent = MultiAgentMessages {
role: Some(MultiAgentRoleMessages {
root: Some("root base".to_string()),
subagent: Some("subagent base".to_string()),
root: Some("root base".into()),
subagent: Some("subagent base".into()),
}),
mode: Some(MultiAgentModeMessages {
explicit: Some("explicit mode".to_string()),
proactive: Some("proactive mode".to_string()),
hint_text: Some("mode hint".to_string()),
explicit: Some("explicit mode".into()),
proactive: Some("proactive mode".into()),
hint_text: Some("mode hint".into()),
}),
};
let token_budget = ModelTokenBudgetConfig {
enabled: false,
use_history_notes_extension: false,
reminder_threshold_tokens: 128,
reminder_message_template: "budget reminder".to_string(),
guidance_message: "budget guidance".to_string(),
auto_compact_fallback_prompt: "compact prompt".to_string(),
reminder_message_template: "budget reminder".into(),
guidance_message: "budget guidance".into(),
auto_compact_fallback_prompt: "compact prompt".into(),
auto_compact_fallback_buffer_tokens: 64,
};
let guardian_v2 = GuardianV2ModelConfig {
classifier_instructions: Some("Guardian experiment".to_string()),
classifier_instructions: Some("Guardian experiment".into()),
..Default::default()
};
let confirmation_policies = ConfirmationPolicies {
browser_use: Some("# Browser policy\n\n{{literal_markdown}}\n".to_string()),
computer_use: Some(" # Native policy\r\n\n${native_markdown}\n".to_string()),
browser_use: Some("# Browser policy\n\n{{literal_markdown}}\n".into()),
computer_use: Some(" # Native policy\r\n\n${native_markdown}\n".into()),
};
let mut messages = ModelMessages {
apps: Some(AppsMessages {
usage_instructions: Some("Catalog Apps instructions.".to_string()),
usage_instructions: Some("Catalog Apps instructions.".into()),
}),
plugins: None,
content_filter_guidance: None,
persistent_instructions: Some(persistent_instructions.to_string()),
persistent_instructions: Some(persistent_instructions.into()),
tools: Some(ToolMessages {
send_user_message_async: Some(ToolMessage {
description: Some(async_message_description.to_string()),
description: Some(async_message_description.into()),
..Default::default()
}),
multi_agent: Some(MultiAgentToolMessages {
spawn_agent: Some(ToolMessage {
description: Some("Catalog spawn description.".to_string()),
description: Some("Catalog spawn description.".into()),
..Default::default()
}),
..Default::default()
}),
..Default::default()
}),
instructions_template: Some("template".to_string()),
instructions_template: Some("template".into()),
approvals: Some(approvals),
collaboration_modes: Some(collaboration_modes),
auto_review: Some(auto_review),
@@ -131,7 +131,7 @@ fn base_instruction_override_is_literal_and_preserves_catalog_messages() {
let updated = with_config_overrides(model, &config);
messages.instructions_template = Some(override_instructions.to_string());
messages.instructions_template = Some(override_instructions.into());
assert_eq!(updated.model_messages, Some(messages));
assert_eq!(render_model_instructions(&updated), override_instructions);
}
@@ -159,16 +159,16 @@ fn personality_none_strips_catalog_instruction_sources_through_the_next_h1() {
),
] {
let mut messages = ModelMessages {
instructions_template: Some(instructions.to_string()),
persistent_instructions: Some(String::new()),
instructions_template: Some(instructions.into()),
persistent_instructions: Some("".into()),
tools: Some(ToolMessages {
send_user_message_async: Some(ToolMessage {
description: Some(String::new()),
description: Some("".into()),
..Default::default()
}),
multi_agent: Some(MultiAgentToolMessages {
spawn_agent: Some(ToolMessage {
description: Some(String::new()),
description: Some("".into()),
..Default::default()
}),
..Default::default()
@@ -176,7 +176,7 @@ fn personality_none_strips_catalog_instruction_sources_through_the_next_h1() {
..Default::default()
}),
approvals: Some(ApprovalMessages {
on_request: Some("user approvals".to_string()),
on_request: Some("user approvals".into()),
on_request_auto_review: None,
never: None,
unless_trusted: None,
@@ -190,7 +190,7 @@ fn personality_none_strips_catalog_instruction_sources_through_the_next_h1() {
let updated = with_config_overrides(model, &config);
messages.instructions_template = Some(expected.to_string());
messages.instructions_template = Some(expected.into());
assert_eq!(updated.model_messages, Some(messages));
}
}
@@ -210,7 +210,7 @@ fn baked_personality_section_is_preserved_without_explicit_none() {
.model_messages
.as_mut()
.expect("fallback model messages")
.instructions_template = Some(instructions.to_string());
.instructions_template = Some(instructions.into());
assert_eq!(
render_model_instructions(&with_config_overrides(model, &config)),
@@ -226,7 +226,7 @@ fn explicit_empty_base_instructions_stay_empty_with_personality_none() {
.model_messages
.as_mut()
.expect("fallback model messages")
.instructions_template = Some("Intro\n# Personality\nRemove me".to_string());
.instructions_template = Some("Intro\n# Personality\nRemove me".into());
let config = ModelsManagerConfig {
base_instructions: Some(String::new()),
personality: Some(Personality::None),