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
@@ -597,9 +597,9 @@ async fn submitted_sparse_updates_preserve_captured_steps_and_ordering() {
enabled: false,
use_history_notes_extension: false,
reminder_threshold_tokens: 2_000,
reminder_message_template: "{n_remaining} tokens remain.".to_string(),
guidance_message: format!("Guidance for {}.", model.slug),
auto_compact_fallback_prompt: "Save state before rollover.".to_string(),
reminder_message_template: "{n_remaining} tokens remain.".into(),
guidance_message: format!("Guidance for {}.", model.slug).into(),
auto_compact_fallback_prompt: "Save state before rollover.".into(),
auto_compact_fallback_buffer_tokens: 4_000,
});
}
@@ -624,7 +624,7 @@ async fn submitted_sparse_updates_preserve_captured_steps_and_ordering() {
.model_messages
.as_mut()
.expect("model messages")
.instructions_template = Some("Destination-model instructions.".to_string());
.instructions_template = Some("Destination-model instructions.".into());
let expected_destination = destination.clone();
let ActivationFixture {
session,
@@ -1414,8 +1414,8 @@ async fn parent_fallback_policy_uses_both_config_lifetimes(
let mut live_config = admitted_config.clone();
live_config.guardian_policy_config = live_policy.map(str::to_string);
let (mut admitted, mut destination) = safety_models();
parent_review_messages(&mut admitted).policy = Some("catalog policy A".to_string());
parent_review_messages(&mut destination).policy = Some("catalog policy B".to_string());
parent_review_messages(&mut admitted).policy = Some("catalog policy A".into());
parent_review_messages(&mut destination).policy = Some("catalog policy B".into());
assert_eq!(
check_legacy_model_safety(
&admitted,
@@ -1442,24 +1442,24 @@ async fn parent_fallback_preserves_explicit_empty_and_bundled_defaults() {
let check = |destination: &CatalogModel| {
check_legacy_model_safety(&admitted, &admitted, destination, &config, &config)
};
parent_review_messages(&mut destination).policy = Some(defaults.policy.to_string());
parent_review_messages(&mut destination).policy = Some(defaults.policy.into());
parent_review_messages(&mut destination).policy_template =
Some(defaults.policy_template.to_string());
Some(defaults.policy_template.into());
assert_eq!(check(&destination), Ok(()));
parent_review_messages(&mut destination).policy_template = Some(String::new());
parent_review_messages(&mut destination).policy_template = Some("".into());
assert_eq!(
check(&destination),
Err("the destination changes the Guardian parent-fallback policy template".to_string())
);
parent_review_messages(&mut destination).policy_template = None;
parent_review_messages(&mut destination).policy = Some(String::new());
parent_review_messages(&mut destination).policy = Some("".into());
assert_eq!(
check(&destination),
Err("the destination changes the Guardian parent-fallback policy".to_string())
);
parent_review_messages(&mut destination).policy = None;
assert_eq!(check(&destination), Ok(()));
parent_review_messages(&mut destination).node_repl_policy = Some(String::new());
parent_review_messages(&mut destination).node_repl_policy = Some("".into());
assert_eq!(
check(&destination),
Err("the destination changes the Guardian parent-fallback node REPL policy".to_string())
@@ -1473,10 +1473,10 @@ async fn unchanged_explicit_reviewer_does_not_use_parent_policy() {
for model in [&mut admitted, &mut destination] {
model.auto_review_model_override = Some("shared-reviewer".to_string());
}
parent_review_messages(&mut admitted).policy = Some("catalog policy A".to_string());
parent_review_messages(&mut destination).policy = Some("catalog policy B".to_string());
parent_review_messages(&mut destination).policy_template = Some(String::new());
parent_review_messages(&mut destination).node_repl_policy = Some(String::new());
parent_review_messages(&mut admitted).policy = Some("catalog policy A".into());
parent_review_messages(&mut destination).policy = Some("catalog policy B".into());
parent_review_messages(&mut destination).policy_template = Some("".into());
parent_review_messages(&mut destination).node_repl_policy = Some("".into());
assert_eq!(
check_legacy_model_safety(
&admitted,
@@ -1490,7 +1490,7 @@ async fn unchanged_explicit_reviewer_does_not_use_parent_policy() {
}
#[test_case(GuardianV2ModelConfig {
classifier_instructions: Some("Review the complete action.".to_string()),
classifier_instructions: Some("Review the complete action.".into()),
..Default::default()
}; "classifier instructions")]
#[test_case(GuardianV2ModelConfig {
@@ -1503,7 +1503,7 @@ async fn unchanged_explicit_reviewer_does_not_use_parent_policy() {
}; "classifier effort")]
#[test_case(GuardianV2ModelConfig {
transcript: Some(GuardianV2TranscriptModelConfig {
sources: Some(vec!["reasoning".to_string()]),
sources: Some(vec!["reasoning".into()]),
..Default::default()
}),
..Default::default()
@@ -416,14 +416,14 @@ async fn model_resolution_preserves_startup_overrides_and_instruction_provenance
) {
let configured_instructions = "explicit instructions";
let auto_review = AutoReviewMessages {
policy: Some("catalog review policy".to_string()),
policy_template: Some("catalog review template".to_string()),
policy: Some("catalog review policy".into()),
policy_template: Some("catalog review template".into()),
node_repl_policy: None,
rejection_instructions: None,
timeout_instructions: None,
};
let guardian_v2 = GuardianV2ModelConfig {
classifier_instructions: Some("catalog classifier".to_string()),
classifier_instructions: Some("catalog classifier".into()),
review_threshold_basis_points: Some(7_500),
..Default::default()
};
@@ -437,7 +437,7 @@ async fn model_resolution_preserves_startup_overrides_and_instruction_provenance
.as_mut()
.expect("test model should have instruction metadata");
messages.instructions_template =
Some("Catalog B.\n# Personality\nfixed\n# Rules\nKeep the rules.".to_string());
Some("Catalog B.\n# Personality\nfixed\n# Rules\nKeep the rules.".into());
messages.auto_review = Some(auto_review);
messages.guardian_v2 = Some(guardian_v2);
let catalog = ModelsResponse {
+2 -2
View File
@@ -11353,7 +11353,7 @@ async fn build_initial_context_uses_retained_step_after_model_change() {
model_info.context_window = None;
model_info.max_context_window = None;
let messages = model_info.model_messages.as_mut().unwrap();
messages.instructions_template = Some("A instructions".to_string());
messages.instructions_template = Some("A instructions".into());
});
session.state.lock().await.base_instructions_provenance =
Some(codex_protocol::models::BaseInstructionsProvenance::Model {
@@ -11401,7 +11401,7 @@ async fn build_initial_context_uses_retained_step_after_model_change() {
.model_messages
.as_mut()
.unwrap()
.instructions_template = Some("B instructions".to_string());
.instructions_template = Some("B instructions".into());
turn_context
.next_step_settings
.store(Arc::new(ResolvedStepSettings::new(
+3 -3
View File
@@ -103,9 +103,9 @@ pub(super) fn resolve_token_budget(
use_history_notes_extension: configured_token_budget
.is_some_and(|token_budget| token_budget.use_history_notes_extension),
reminder_threshold_tokens: Some(model_defaults.reminder_threshold_tokens),
reminder_message_template: model_defaults.reminder_message_template.clone(),
guidance_message: Some(model_defaults.guidance_message.clone()),
auto_compact_fallback_prompt: Some(model_defaults.auto_compact_fallback_prompt.clone()),
reminder_message_template: model_defaults.reminder_message_template.to_string(),
guidance_message: Some(model_defaults.guidance_message.to_string()),
auto_compact_fallback_prompt: Some(model_defaults.auto_compact_fallback_prompt.to_string()),
auto_compact_fallback_buffer_tokens: Some(
model_defaults.auto_compact_fallback_buffer_tokens,
),