mirror of
https://github.com/openai/codex.git
synced 2026-11-21 22:50:37 +00:00
Track model and personality in world state (#35530)
## What changed - Add model and personality sections to the persisted world-state snapshot. - Generate model-switch and personality instructions through world-state diffs, including when prior state must be inferred during replay. - Keep those instruction fragments in standalone developer messages instead of merging them with adjacent context updates. ## Testing - Cover initial, unchanged, and changed model and personality states. - Verify that model changes are recorded in rollout world-state items. GitOrigin-RevId: 497e29cf38cac08e992e546df900f6f8b01120f9
This commit is contained in:
@@ -1,64 +1,11 @@
|
||||
use crate::context::ContextualUserFragment;
|
||||
use crate::context::ModelSwitchInstructions;
|
||||
use crate::context::PersonalitySpecInstructions;
|
||||
use crate::session::PreviousTurnSettings;
|
||||
use crate::session::turn_context::TurnContext;
|
||||
use codex_protocol::config_types::Personality;
|
||||
use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::openai_models::ModelInfo;
|
||||
use codex_protocol::protocol::TurnContextItem;
|
||||
|
||||
fn build_personality_update_item(
|
||||
previous: Option<&TurnContextItem>,
|
||||
next: &TurnContext,
|
||||
personality_feature_enabled: bool,
|
||||
) -> Option<String> {
|
||||
if !personality_feature_enabled {
|
||||
return None;
|
||||
}
|
||||
let previous = previous?;
|
||||
if next.model_info.slug != previous.model {
|
||||
return None;
|
||||
}
|
||||
|
||||
if let Some(personality) = next.personality
|
||||
&& next.personality != previous.personality
|
||||
{
|
||||
let model_info = &next.model_info;
|
||||
let personality_message = personality_message_for(model_info, personality);
|
||||
personality_message.map(|message| PersonalitySpecInstructions::new(message).render())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn personality_message_for(
|
||||
model_info: &ModelInfo,
|
||||
personality: Personality,
|
||||
) -> Option<String> {
|
||||
model_info
|
||||
.model_messages
|
||||
.as_ref()
|
||||
.and_then(|spec| spec.get_personality_message(Some(personality)))
|
||||
.filter(|message| !message.is_empty())
|
||||
}
|
||||
|
||||
pub(crate) fn build_model_instructions_update_item(
|
||||
previous_turn_settings: Option<&PreviousTurnSettings>,
|
||||
next: &TurnContext,
|
||||
) -> Option<String> {
|
||||
let previous_turn_settings = previous_turn_settings?;
|
||||
if previous_turn_settings.model == next.model_info.slug {
|
||||
return None;
|
||||
}
|
||||
|
||||
let model_instructions = next.model_info.get_model_instructions(next.personality);
|
||||
if model_instructions.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(ModelSwitchInstructions::new(model_instructions).render())
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum MessageGroup {
|
||||
Standalone,
|
||||
Mergeable,
|
||||
}
|
||||
|
||||
pub(crate) fn build_developer_update_item(text_sections: Vec<String>) -> Option<ResponseItem> {
|
||||
@@ -72,20 +19,29 @@ pub(crate) fn build_contextual_user_message(text_sections: Vec<String>) -> Optio
|
||||
pub(crate) fn merge_contextual_fragments(
|
||||
fragments: Vec<Box<dyn ContextualUserFragment>>,
|
||||
) -> Vec<ResponseItem> {
|
||||
let mut messages: Vec<(&str, Vec<String>)> = Vec::with_capacity(fragments.len());
|
||||
let mut messages: Vec<(&str, MessageGroup, Vec<String>)> = Vec::with_capacity(fragments.len());
|
||||
for fragment in fragments {
|
||||
let role = fragment.role();
|
||||
let group = if fragment.requires_separate_message() {
|
||||
MessageGroup::Standalone
|
||||
} else {
|
||||
MessageGroup::Mergeable
|
||||
};
|
||||
let text = fragment.render();
|
||||
match messages.last_mut() {
|
||||
Some((previous_role, text_sections)) if *previous_role == role => {
|
||||
Some((previous_role, previous_group, text_sections))
|
||||
if *previous_role == role
|
||||
&& *previous_group == MessageGroup::Mergeable
|
||||
&& group == MessageGroup::Mergeable =>
|
||||
{
|
||||
text_sections.push(text);
|
||||
}
|
||||
_ => messages.push((role, vec![text])),
|
||||
_ => messages.push((role, group, vec![text])),
|
||||
}
|
||||
}
|
||||
messages
|
||||
.into_iter()
|
||||
.filter_map(|(role, text_sections)| build_text_message(role, text_sections))
|
||||
.filter_map(|(role, _, text_sections)| build_text_message(role, text_sections))
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -107,28 +63,3 @@ fn build_text_message(role: &str, text_sections: Vec<String>) -> Option<Response
|
||||
internal_chat_message_metadata_passthrough: None,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn build_settings_update_items(
|
||||
previous: Option<&TurnContextItem>,
|
||||
previous_turn_settings: Option<&PreviousTurnSettings>,
|
||||
next: &TurnContext,
|
||||
personality_feature_enabled: bool,
|
||||
) -> Vec<ResponseItem> {
|
||||
// TODO(ccunningham): build_settings_update_items still does not cover every
|
||||
// model-visible item emitted by build_initial_context. Persist the remaining
|
||||
// inputs or add explicit replay events so fork/resume can diff everything
|
||||
// deterministically.
|
||||
let developer_update_sections = [
|
||||
// Keep model-switch instructions first so model-specific guidance is read before
|
||||
// any other context diffs on this turn.
|
||||
build_model_instructions_update_item(previous_turn_settings, next),
|
||||
build_personality_update_item(previous, next, personality_feature_enabled),
|
||||
]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.collect();
|
||||
|
||||
build_developer_update_item(developer_update_sections)
|
||||
.into_iter()
|
||||
.collect()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user