mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
Preserve base instruction provenance across sessions (#37446)
## Why Persisted base instructions need to retain whether they were explicitly customized or generated from a model template. Without that distinction, forks and config-lock replays can treat model-generated instructions as custom and keep the wrong template after a model or personality change. ## What changed - Record custom or model provenance with base instructions in rollouts and config locks. - Preserve custom instructions across model changes, while allowing model-generated instructions to follow the selected model and personality. - Keep legacy rollouts without provenance compatible and propagate provenance through forks and spawned agents. ## Testing - Cover provenance serialization and legacy rollout decoding. - Cover role personality changes, forked model changes, config-lock replay, and multi-agent configuration. GitOrigin-RevId: 801c26559835f558027ce5112700c0cd31fcb21b
This commit is contained in:
@@ -13,6 +13,7 @@ use codex_features::RolloutBudgetConfigToml;
|
||||
use codex_features::TokenBudgetConfigToml;
|
||||
use codex_features::ToolRegistryConfigToml;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::models::BaseInstructionsProvenance;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::config_lock::ConfigLockReplayOptions;
|
||||
@@ -25,6 +26,7 @@ use super::SessionConfiguration;
|
||||
|
||||
pub(crate) async fn validate_config_lock_if_configured(
|
||||
session_configuration: &SessionConfiguration,
|
||||
base_instructions_provenance: Option<&BaseInstructionsProvenance>,
|
||||
) -> anyhow::Result<()> {
|
||||
if session_configuration.session_source.is_non_root_agent() {
|
||||
return Ok(());
|
||||
@@ -36,7 +38,10 @@ pub(crate) async fn validate_config_lock_if_configured(
|
||||
else {
|
||||
return Ok(());
|
||||
};
|
||||
let actual = session_configuration.to_config_lockfile_toml()?;
|
||||
let mut actual = session_configuration.to_config_lockfile_toml()?;
|
||||
if actual.config.instructions.is_some() {
|
||||
actual.base_instructions_provenance = base_instructions_provenance.cloned();
|
||||
}
|
||||
let config = session_configuration.original_config_do_not_use.as_ref();
|
||||
let options = ConfigLockReplayOptions {
|
||||
allow_codex_version_mismatch: config.config_lock_allow_codex_version_mismatch,
|
||||
@@ -49,13 +54,17 @@ pub(crate) async fn validate_config_lock_if_configured(
|
||||
pub(crate) async fn export_config_lock_if_configured(
|
||||
session_configuration: &SessionConfiguration,
|
||||
conversation_id: ThreadId,
|
||||
base_instructions_provenance: Option<&BaseInstructionsProvenance>,
|
||||
) -> anyhow::Result<()> {
|
||||
let config = session_configuration.original_config_do_not_use.as_ref();
|
||||
let Some(export_dir) = config.config_lock_export_dir.as_ref() else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let lock = session_configuration.to_config_lockfile_toml()?;
|
||||
let mut lock = session_configuration.to_config_lockfile_toml()?;
|
||||
if lock.config.instructions.is_some() {
|
||||
lock.base_instructions_provenance = base_instructions_provenance.cloned();
|
||||
}
|
||||
let lock = toml::to_string_pretty(&lock).context("failed to serialize config lock")?;
|
||||
let path = export_dir.join(format!("{conversation_id}.config.lock.toml"));
|
||||
|
||||
|
||||
@@ -103,6 +103,7 @@ use codex_protocol::items::UserMessageItem;
|
||||
use codex_protocol::models::ActivePermissionProfile;
|
||||
use codex_protocol::models::AdditionalPermissionProfile;
|
||||
use codex_protocol::models::BaseInstructions;
|
||||
use codex_protocol::models::BaseInstructionsProvenance;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::models::SandboxEnforcement;
|
||||
use codex_protocol::openai_models::ModelInfo;
|
||||
@@ -1263,6 +1264,7 @@ impl Session {
|
||||
let state = self.state.lock().await;
|
||||
BaseInstructions {
|
||||
text: state.session_configuration.base_instructions.clone(),
|
||||
provenance: state.base_instructions_provenance.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -546,6 +546,27 @@ impl Session {
|
||||
session_configuration.collaboration_mode.model(),
|
||||
session_configuration.provider
|
||||
);
|
||||
let base_instructions_provenance = if config.base_instructions.is_some() {
|
||||
Some(
|
||||
config
|
||||
.base_instructions_provenance
|
||||
.clone()
|
||||
.unwrap_or(BaseInstructionsProvenance::Custom),
|
||||
)
|
||||
} else if let Some(inherited_base_instructions) = initial_history.get_base_instructions() {
|
||||
let BaseInstructions { text, provenance } = inherited_base_instructions;
|
||||
provenance.or_else(|| {
|
||||
(text == model_info.get_model_instructions(config.personality)).then(|| {
|
||||
BaseInstructionsProvenance::Model {
|
||||
model: model_info.slug.clone(),
|
||||
}
|
||||
})
|
||||
})
|
||||
} else {
|
||||
Some(BaseInstructionsProvenance::Model {
|
||||
model: model_info.slug.clone(),
|
||||
})
|
||||
};
|
||||
let forked_from_id = session_configuration
|
||||
.forked_from_thread_id
|
||||
.or_else(|| initial_history.forked_from_id());
|
||||
@@ -660,6 +681,7 @@ impl Session {
|
||||
originator: session_configuration.originator.clone(),
|
||||
base_instructions: BaseInstructions {
|
||||
text: session_configuration.base_instructions.clone(),
|
||||
provenance: base_instructions_provenance.clone(),
|
||||
},
|
||||
dynamic_tools: session_configuration.dynamic_tools.clone(),
|
||||
selected_capability_roots: selected_capability_roots.clone(),
|
||||
@@ -1012,16 +1034,22 @@ impl Session {
|
||||
);
|
||||
}
|
||||
session_configuration.thread_name = thread_name.clone();
|
||||
validate_config_lock_if_configured(&session_configuration).await?;
|
||||
export_config_lock_if_configured(&session_configuration, thread_id).await?;
|
||||
validate_config_lock_if_configured(
|
||||
&session_configuration,
|
||||
base_instructions_provenance.as_ref(),
|
||||
)
|
||||
.await?;
|
||||
export_config_lock_if_configured(
|
||||
&session_configuration,
|
||||
thread_id,
|
||||
base_instructions_provenance.as_ref(),
|
||||
)
|
||||
.await?;
|
||||
let mut state = SessionState::new_with_auto_compact_window_ids(
|
||||
session_configuration.clone(),
|
||||
initial_auto_compact_window_ids,
|
||||
);
|
||||
state.base_instructions_model = (config.base_instructions.is_none()
|
||||
&& session_configuration.base_instructions
|
||||
== model_info.get_model_instructions(config.personality))
|
||||
.then(|| model_info.slug.clone());
|
||||
state.base_instructions_provenance = base_instructions_provenance.clone();
|
||||
let managed_network_requirements_configured = config
|
||||
.config_layer_stack
|
||||
.requirements_toml()
|
||||
|
||||
@@ -2647,6 +2647,7 @@ async fn recompute_token_usage_uses_session_base_instructions() {
|
||||
let history = session.clone_history().await;
|
||||
let session_base_instructions = BaseInstructions {
|
||||
text: override_instructions,
|
||||
provenance: None,
|
||||
};
|
||||
let expected_tokens = history
|
||||
.estimate_token_count_with_base_instructions(&session_base_instructions)
|
||||
|
||||
@@ -26,6 +26,7 @@ use codex_extension_api::WorldStateContributionInput;
|
||||
use codex_features::Feature;
|
||||
use codex_protocol::error::CodexErr;
|
||||
use codex_protocol::error::Result as CodexResult;
|
||||
use codex_protocol::models::BaseInstructionsProvenance;
|
||||
|
||||
impl Session {
|
||||
#[tracing::instrument(name = "world_state.build", level = "info", skip_all)]
|
||||
@@ -50,8 +51,12 @@ impl Session {
|
||||
.map(|previous| previous.model)
|
||||
.or_else(|| {
|
||||
state
|
||||
.base_instructions_model
|
||||
.base_instructions_provenance
|
||||
.as_ref()
|
||||
.and_then(|provenance| match provenance {
|
||||
BaseInstructionsProvenance::Model { model } => Some(model),
|
||||
BaseInstructionsProvenance::Custom => None,
|
||||
})
|
||||
.filter(|_| base_instructions != model_instructions)
|
||||
.cloned()
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user