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
@@ -155,9 +155,12 @@ impl GuardianV2Config {
) -> Result<Self, String> {
let mut configured = self.local_overrides.clone();
if let Some(model_defaults) = model_defaults {
configured.classifier_instructions = configured
.classifier_instructions
.or_else(|| model_defaults.classifier_instructions.clone());
configured.classifier_instructions = configured.classifier_instructions.or_else(|| {
model_defaults
.classifier_instructions
.as_deref()
.map(str::to_owned)
});
configured.review_threshold = configured.review_threshold.or_else(|| {
model_defaults
.review_threshold_basis_points
@@ -134,7 +134,7 @@ fn model_prompt_and_explicit_threshold_precedence_are_preserved() {
ResolvedModelMessages::bundled().guardian_classifier_instructions(),
] {
let defaults = GuardianV2ModelConfig {
classifier_instructions: Some(prompt.to_owned()),
classifier_instructions: Some(prompt.into()),
..Default::default()
};
let resolved = builtin.with_model_defaults(Some(&defaults)).unwrap();
@@ -162,7 +162,7 @@ fn model_prompt_and_explicit_threshold_precedence_are_preserved() {
}
let model_threshold = GuardianV2ModelConfig {
classifier_instructions: Some("Model-owned instructions.".to_owned()),
classifier_instructions: Some("Model-owned instructions.".into()),
review_threshold_basis_points: Some(6_000),
..Default::default()
};
@@ -197,7 +197,7 @@ fn model_defaults_preserve_transcript_mode_and_matching_instructions() {
})
.unwrap()
.with_model_defaults(Some(&GuardianV2ModelConfig {
classifier_instructions: Some("Model-owned prompt.".to_owned()),
classifier_instructions: Some("Model-owned prompt.".into()),
..Default::default()
}))
.unwrap();
@@ -220,7 +220,7 @@ fn classifier_caps_preserve_complete_provenance_and_output_contract() {
let prompt = "Return a JSON action_risk score. ".repeat(200);
let policy = "Require approval for unsafe actions.";
let model_defaults = GuardianV2ModelConfig {
classifier_instructions: Some(prompt.clone()),
classifier_instructions: Some(prompt.as_str().into()),
max_classifier_instruction_tokens: Some(100),
..Default::default()
};
@@ -256,7 +256,7 @@ fn classifier_caps_preserve_complete_provenance_and_output_contract() {
fn model_runtime_settings_preserve_local_overrides() {
let prompt = "legacy instructions ".repeat(3_000);
let defaults = GuardianV2ModelConfig {
classifier_instructions: Some(prompt.clone()),
classifier_instructions: Some(prompt.as_str().into()),
max_classifier_instruction_tokens: Some(256),
max_tool_call_lag: Some(1),
async_classifier_conversation_token_limit: Some(80_000),
@@ -1123,7 +1123,7 @@ async fn sample_configured_conversation_history_with_delivery(
.auto_review
.as_mut()
.expect("reviewer model should have Guardian policy")
.policy = Some(TEST_CATALOG_GUARDIAN_POLICY.to_owned());
.policy = Some(TEST_CATALOG_GUARDIAN_POLICY.into());
})
.with_model("gpt-5.5")
.with_config(move |config| {
@@ -2340,12 +2340,12 @@ async fn contributor_uses_model_defaults_and_preserves_local_overrides() -> Resu
let model_defaults = GuardianV2ModelConfig {
async_classifier_mode: Some(codex_protocol::openai_models::AsyncClassifierMode::Snapshot),
async_classifier_conversation_token_limit: None,
classifier_instructions: Some("Use the experimental model-owned prompt.".to_owned()),
classifier_instructions: Some("Use the experimental model-owned prompt.".into()),
review_threshold_basis_points: Some(6_000),
max_tool_call_lag: Some(2),
reasoning_effort: Some(ReasoningEffort::Minimal),
transcript: Some(GuardianV2TranscriptModelConfig {
sources: Some(vec!["reasoning".to_owned()]),
sources: Some(vec!["reasoning".into()]),
include_images: Some(true),
max_message_entry_tokens: Some(128),
max_message_transcript_tokens: Some(256),
@@ -175,10 +175,10 @@ fn sparse_catalog_messages_preserve_defaults_and_explicit_empty_descriptions() {
};
tool.description.clear();
let messages = codex_protocol::openai_models::NotesToolMessages {
namespace_description: Some(String::new()),
namespace_description: Some("".into()),
read_file: Some(codex_protocol::openai_models::ToolMessage {
description: Some(String::new()),
parameters: Some(String::new()),
description: Some("".into()),
parameters: Some("".into()),
}),
..Default::default()
};
@@ -142,7 +142,7 @@ async fn installed_extension_exposes_and_invokes_history_notes_tools() -> TestRe
let step_store = ExtensionData::new("step-1");
let mut messages = NotesToolMessages {
read_file: Some(ToolMessage {
description: Some("Catalog read instructions".to_string()),
description: Some("Catalog read instructions".into()),
..Default::default()
}),
..Default::default()
@@ -155,7 +155,7 @@ async fn installed_extension_exposes_and_invokes_history_notes_tools() -> TestRe
.find(|tool| tool.tool_name() == ToolName::namespaced("notes", "read_file"))
.expect("notes.read_file should be exposed");
let captured_spec = captured_tool.spec();
messages.read_file.as_mut().expect("read_file").description = Some(String::new());
messages.read_file.as_mut().expect("read_file").description = Some("".into());
step_store.insert(messages);
let next_tools =
registry.tool_contributors()[0].tools_for_step(&session_store, &thread_store, &step_store);