Resolve saved Daybreak choices for each subagent turn (#60773)

## Why

A subagent can use a different model from its parent, with different supported access programs. Saved Daybreak choices need to resolve against the subagent's model and remain consistent throughout its turn, including compaction.

## What changed

- Resolve saved choices before native subagent turns start, preserving explicit turn overrides and applying saved toggles on later turns.
- Distinguish inherited parent programs from explicit child selections. Adapt inherited Daybreak Blue/Red selections to the child model while retaining the saved choice as the default for later turns and nested subagents.
- Validate the account associated with automatic selections and reject incompatible saved choices before sending model requests, while retaining rejected user input in conversation history.
- Require model changes to wait until the next turn when a saved Daybreak choice is present.

## Testing

Add coverage for both multi-agent spawn versions, model-specific selection, automatic compaction, WebSocket response reuse, account changes, unsupported choices, and rejected-input persistence across resume.

GitOrigin-RevId: 11971138d656600d9878187225e4d33217e68ad4
This commit is contained in:
sayan-oai
2026-11-11 10:10:37 +00:00
committed by copyberry
parent 340a18ed7c
commit c1934fc99e
12 changed files with 1043 additions and 82 deletions
+88 -32
View File
@@ -12,6 +12,7 @@ use crate::agent::role::apply_role_to_config;
use crate::agent::types::AgentMetadata;
use crate::agent::types::LiveAgent;
use crate::agent::types::SpawnAgentOptions;
use crate::agent::types::SpawnProgramSource;
use crate::agents_md_manager::SessionInstructions;
use crate::codex_thread::CodexThread;
use crate::codex_thread::ThreadConfigSnapshot;
@@ -746,51 +747,86 @@ impl LocalAgentControl {
disabled_plugin_ids,
..StartThreadOptions::new(config.clone())
};
let mut cyber_access_program = options.cyber_access_program;
let mut inherited_program_to_resolve = None;
if let Some(parent_id) = session_source
.as_ref()
.and_then(SessionSource::parent_thread_id)
&& let Ok(parent) = state.get_thread(parent_id).await
&& config.features.enabled(Feature::DaybreakProgramPropagation)
&& let Some(enrollment) = parent
.thread_extension_data()
.get::<helper_daybreak::Enrollment>()
&& parent
.session
.get_config()
.await
.features
.enabled(Feature::DaybreakProgramPropagation)
{
start_options
.thread_extension_init
.insert(helper_daybreak::Enrollment::NativeHelper);
let turn = match options.parent_turn_id.as_deref() {
Some(id) => parent.session.turn_context_for_sub_id(id).await,
None => None,
};
let spawn_owner = helper_daybreak::eligible_owner(&parent.session);
if let Some(turn_id) = options.parent_turn_id.as_deref()
&& let Some(turn) = parent.session.turn_context_for_sub_id(turn_id).await
&& let Some(intent) = helper_daybreak::spawning_intent(
&parent.session,
&turn,
&enrollment,
spawn_owner.as_ref(),
)
.await
if options.cyber_access_program_source == SpawnProgramSource::ParentTurn
&& let Some(resolution) = turn.as_ref().and_then(|turn| {
turn.extension_data
.get::<helper_daybreak::turn::SavedProgramResolution>()
})
{
if let Err(error) = &intent.choice {
// Only a usable, successful read can become a child's inherited default.
if options.cyber_access_program.is_none()
&& config.model_provider_id == codex_model_provider_info::OPENAI_PROVIDER_ID
resolution
.validate()
.map_err(|err| err.with_agent_context(AgentErrorContext::ChildStartup))?;
// Carry the saved choice. The child resolves it for its own model and account.
cyber_access_program = None;
}
if config.features.enabled(Feature::DaybreakProgramPropagation)
&& let Some(enrollment) = parent
.thread_extension_data()
.get::<helper_daybreak::Enrollment>()
&& parent
.session
.get_config()
.await
.features
.enabled(Feature::DaybreakProgramPropagation)
{
start_options
.thread_extension_init
.insert(helper_daybreak::Enrollment::NativeHelper);
if let Some(turn) = turn {
let propagate_program = spawn_owner.is_some()
&& turn.config.model_provider_id
== codex_model_provider_info::OPENAI_PROVIDER_ID
&& codex_model_provider::provider_uses_first_party_auth_path(
turn.provider.info(),
)
&& config.model_provider_id
== codex_model_provider_info::OPENAI_PROVIDER_ID
&& config
.model_provider
.supports_cyber_access_programs(&config.model_provider_id)
&& codex_model_provider::provider_uses_first_party_auth_path(
&config.model_provider,
)
);
if propagate_program
&& options.cyber_access_program_source == SpawnProgramSource::ParentTurn
{
return Err(CodexErr::InvalidRequest(error.clone())
.with_agent_context(AgentErrorContext::ChildStartup));
inherited_program_to_resolve = cyber_access_program;
}
if let Some(intent) = helper_daybreak::spawning_intent(
&parent.session,
&turn,
&enrollment,
spawn_owner.as_ref(),
)
.await
{
// A read failure must not become an immutable default on a live child.
if propagate_program
&& (options.cyber_access_program.is_none()
|| options.cyber_access_program_source
== SpawnProgramSource::ParentTurn)
&& let Err(error) = &intent.choice
{
return Err(CodexErr::InvalidRequest(error.clone())
.with_agent_context(AgentErrorContext::ChildStartup));
}
if intent.choice.is_ok() {
start_options.thread_extension_init.insert(intent);
}
}
} else {
start_options.thread_extension_init.insert(intent);
}
}
}
@@ -898,11 +934,31 @@ impl LocalAgentControl {
None
};
if let Some(inherited) = inherited_program_to_resolve
&& inherited != codex_protocol::turn_input::CyberAccessProgram::Standard
&& let Some(model) = new_thread
.thread
.thread_extension_data()
.get::<codex_protocol::openai_models::ModelInfo>()
&& model
.available_access_programs
.as_ref()
.is_some_and(|programs| !programs.cyber.contains(&inherited))
{
// Preserve the parent's turn override, adapting Blue/Red to the child model.
// The separately captured saved choice remains the default for later turns.
cyber_access_program = helper_daybreak::turn::select_program(
&model,
codex_protocol::openai_models::SavedDaybreakChoice::On,
)
.map_err(CodexErr::InvalidRequest)?
.or(Some(inherited));
}
let start_options = TurnStartOptions {
parent_turn_id: options.parent_turn_id,
turn_trigger: options.turn_trigger,
root_turn_id: options.root_turn_id,
cyber_access_program: options.cyber_access_program,
cyber_access_program,
..Default::default()
};
let input_admission_started_at = Instant::now();
@@ -54,7 +54,6 @@ pub(crate) async fn spawning_intent(
let choice = match saved_choice(parent).await {
Ok(Some(enabled)) => Ok(SavedDaybreakChoice::from(Some(enabled))),
Ok(None) => return None,
Err(_) if turn.cyber_access_program.is_some() => return None,
Err(error) => Err(error),
};
Some(SavedIntent {
+118 -3
View File
@@ -1,4 +1,5 @@
//! Capture the saved Daybreak choice for an admitted native helper turn.
//! Resolve a native helper's saved Daybreak choice once, before its turn starts.
//! The existing turn program then travels with normal and compaction requests.
use super::Enrollment;
use super::SavedIntent;
use super::eligible_owner;
@@ -7,7 +8,40 @@ use crate::session::session::Session;
use crate::session::turn_context::TurnContext;
use codex_features::Feature;
use codex_login::AuthOwner;
use codex_protocol::error::CodexErr;
use codex_protocol::error::Result as CodexResult;
use codex_protocol::openai_models::ModelInfo;
use codex_protocol::openai_models::SavedDaybreakChoice;
use codex_protocol::turn_input::CyberAccessProgram;
const ACCOUNT_CHANGED: &str =
"The account for this subagent's saved Daybreak choice has changed. Start a new subagent.";
/// An implicit program stays bound to the account whose choice selected it.
pub(crate) struct SavedProgramResolution(Result<AuthOwner, String>);
impl SavedProgramResolution {
pub(crate) fn validate(&self) -> CodexResult<()> {
match &self.0 {
Ok(owner) if owner.is_current() => Ok(()),
Ok(_) => Err(CodexErr::InvalidRequest(ACCOUNT_CHANGED.to_owned())),
Err(error) => Err(CodexErr::InvalidRequest(error.clone())),
}
}
}
/// A helper resolves automatic choices from saved intent on its own turn.
pub(crate) fn explicit_program_for_spawn(turn: &TurnContext) -> Option<CyberAccessProgram> {
if turn
.extension_data
.get::<SavedProgramResolution>()
.is_some()
{
None
} else {
turn.cyber_access_program
}
}
/// The chat account at admission, captured before preparing the new turn.
pub(crate) struct HelperTurnAdmission(pub(crate) AuthOwner);
@@ -18,16 +52,28 @@ impl HelperTurnAdmission {
if *enrollment != Enrollment::NativeHelper {
return None;
}
eligible_owner(session).map(Self)
eligible_owner(session)
.or_else(|| {
let inherited = session
.services
.thread_extension_data
.get::<SavedIntent>()?;
(!inherited.owner.is_current()).then(|| inherited.owner.clone())
})
.map(Self)
}
pub(crate) async fn capture_intent(&self, session: &Session, turn: &TurnContext) {
pub(crate) async fn capture_intent(&self, session: &Session, turn: &mut TurnContext) {
if !turn
.config
.features
.enabled(Feature::DaybreakProgramPropagation)
|| turn.config.model_provider_id != codex_model_provider_info::OPENAI_PROVIDER_ID
|| !codex_model_provider::provider_uses_first_party_auth_path(turn.provider.info())
|| !turn
.config
.model_provider
.supports_cyber_access_programs(&turn.config.model_provider_id)
{
return;
}
@@ -43,6 +89,75 @@ impl HelperTurnAdmission {
Err(_) if turn.cyber_access_program.is_some() => return,
Err(error) => (Err(error), self.0.clone()),
};
if turn.cyber_access_program.is_none() {
let resolution = if owner != self.0 || eligible_owner(session).as_ref() != Some(&self.0)
{
Err(ACCOUNT_CHANGED.to_owned())
} else {
choice
.clone()
.and_then(|choice| select_program(turn.model_info(), choice))
};
let bound_owner = resolution.map(|program| {
turn.cyber_access_program = program;
owner.clone()
});
turn.extension_data
.insert(SavedProgramResolution(bound_owner));
}
turn.extension_data.insert(SavedIntent { choice, owner });
}
}
pub(crate) fn select_program(
model: &ModelInfo,
choice: SavedDaybreakChoice,
) -> Result<Option<CyberAccessProgram>, String> {
let Some(programs) = model.available_access_programs.as_ref() else {
return Ok(None);
};
let selected = programs.select_saved(choice);
if selected.is_none() {
match choice {
SavedDaybreakChoice::On if !programs.cyber.is_empty() => {
return Err(format!(
"Model {} does not advertise Daybreak support. Turn Daybreak off or choose a compatible model.",
model.slug
));
}
SavedDaybreakChoice::Off => {
return Err(format!(
"Model {} does not advertise Standard. Choose a model that supports turning Daybreak off.",
model.slug
));
}
SavedDaybreakChoice::On | SavedDaybreakChoice::Unset => {}
}
}
Ok(selected)
}
#[cfg(test)]
mod tests {
use super::*;
use codex_protocol::openai_models::ModelAccessPrograms;
#[test]
fn missing_metadata_and_unset_choice_preserve_implicit_selection() {
let mut model = codex_models_manager::bundled_models_response()
.unwrap()
.models
.remove(0);
model.available_access_programs = None;
assert_eq!(select_program(&model, SavedDaybreakChoice::Off), Ok(None));
for cyber in [vec![], vec![CyberAccessProgram::DaybreakRed]] {
model.available_access_programs = Some(ModelAccessPrograms { cyber });
assert_eq!(select_program(&model, SavedDaybreakChoice::Unset), Ok(None));
assert!(
select_program(&model, SavedDaybreakChoice::Off)
.unwrap_err()
.contains("does not advertise Standard")
);
}
}
}
+10
View File
@@ -23,6 +23,14 @@ pub enum SpawnAgentForkMode {
FullHistory,
}
/// Whether a spawn program was chosen for the child or copied from its parent turn.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SpawnProgramSource {
#[default]
Explicit,
ParentTurn,
}
#[derive(Clone, Debug, Default)]
pub struct SpawnAgentOptions {
pub fork_parent_spawn_call_id: Option<String>,
@@ -39,6 +47,8 @@ pub struct SpawnAgentOptions {
pub disabled_plugin_ids: Option<Vec<String>>,
pub multi_agent_v2_usage_hints: Option<ResolvedMultiAgentV2UsageHints>,
pub cyber_access_program: Option<CyberAccessProgram>,
/// Parent selections apply to the first turn and may need adapting to the child's model.
pub cyber_access_program_source: SpawnProgramSource,
}
/// Identity and status observed from a loaded agent, without a handle to its runtime.