mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Add configurable developer instructions for v2 subagents (#35708)
## What changed - Add `features.multi_agent_v2.subagent_developer_instructions` to override inherited developer instructions for subagents that do not define role-specific instructions. - Preserve role-specific instructions as the higher-precedence setting, and carry the effective instructions through full and bounded forks, compacted histories, and cold resume without duplication. - Treat an unset override as inheritance and a blank override as clearing inherited instructions. ## Testing - Cover configuration parsing and materialization, instruction precedence, fork modes, compaction, role application, and resumed subagents. GitOrigin-RevId: 32321e61abc59b14a1be5f31e6bd6570bb1cd89d
This commit is contained in:
1 parent
3418498f01
commit
49025589b0
14 files changed
+1035
-70
No files matched your search
@@ -1,6 +1,6 @@
|
||||
use super::residency::is_v2_resident_session_source;
|
||||
use super::*;
|
||||
use crate::agent::role::apply_role_to_config;
|
||||
use crate::agent::role::apply_role_to_config_for_multi_agent_v2;
|
||||
use crate::config::PermissionProfileSnapshot;
|
||||
use codex_extension_api::ExtensionDataInit;
|
||||
|
||||
@@ -301,7 +301,7 @@ impl AgentControl {
|
||||
),
|
||||
};
|
||||
|
||||
apply_role_to_config(&mut config, Some(&role_name))
|
||||
apply_role_to_config_for_multi_agent_v2(&mut config, Some(&role_name))
|
||||
.await
|
||||
.map_err(CodexErr::InvalidRequest)?;
|
||||
config
|
||||
@@ -595,6 +595,32 @@ impl AgentControl {
|
||||
|
||||
let parent_thread_id = *parent_thread_id;
|
||||
let parent_thread = state.get_thread(parent_thread_id).await?;
|
||||
let (subagent_developer_instructions, parent_developer_instructions) = match (
|
||||
multi_agent_version,
|
||||
config
|
||||
.multi_agent_v2
|
||||
.subagent_developer_instructions
|
||||
.as_ref(),
|
||||
) {
|
||||
(MultiAgentVersion::V2, Some(_)) => {
|
||||
let parent_developer_instructions = match parent_thread
|
||||
.session
|
||||
.new_default_turn()
|
||||
.await
|
||||
.developer_instructions
|
||||
.clone()
|
||||
{
|
||||
Some(instructions) if !instructions.is_empty() => Some(instructions),
|
||||
Some(_) | None => None,
|
||||
};
|
||||
(
|
||||
Some(config.developer_instructions.clone().unwrap_or_default()),
|
||||
parent_developer_instructions,
|
||||
)
|
||||
}
|
||||
(MultiAgentVersion::Disabled | MultiAgentVersion::V1, _)
|
||||
| (MultiAgentVersion::V2, None) => (None, None),
|
||||
};
|
||||
let parent_history_mode = parent_thread.config_snapshot().await.history_mode;
|
||||
// `record_conversation_items` only queues persistence writes asynchronously.
|
||||
// Flush before snapshotting store history for a fork.
|
||||
@@ -644,43 +670,122 @@ impl AgentControl {
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
let preserve_reference_context_item = matches!(fork_mode, SpawnAgentForkMode::FullHistory);
|
||||
forked_rollout_items.retain(|item| {
|
||||
keep_forked_rollout_item(item, preserve_reference_context_item)
|
||||
&& !matches!(
|
||||
item,
|
||||
RolloutItem::ResponseItem(response_item)
|
||||
if is_multi_agent_v2_usage_hint_message(
|
||||
response_item,
|
||||
&multi_agent_v2_usage_hint_texts_to_filter,
|
||||
)
|
||||
)
|
||||
});
|
||||
if destination_history_mode == Some(ThreadHistoryMode::Paginated) {
|
||||
forked_rollout_items.retain(|item| {
|
||||
!matches!(
|
||||
item,
|
||||
RolloutItem::EventMsg(
|
||||
EventMsg::ItemCompleted(_)
|
||||
| EventMsg::TokenCount(_)
|
||||
| EventMsg::ThreadGoalUpdated(_)
|
||||
| EventMsg::ThreadSettingsApplied(_),
|
||||
)
|
||||
)
|
||||
});
|
||||
}
|
||||
for item in &mut forked_rollout_items {
|
||||
if let RolloutItem::Compacted(compacted) = item
|
||||
&& let Some(replacement_history) = compacted.replacement_history.as_mut()
|
||||
{
|
||||
replacement_history.retain(|response_item| {
|
||||
!is_multi_agent_v2_usage_hint_message(
|
||||
response_item,
|
||||
&multi_agent_v2_usage_hint_texts_to_filter,
|
||||
)
|
||||
});
|
||||
let mut preserve_reference_context_item =
|
||||
matches!(fork_mode, SpawnAgentForkMode::FullHistory);
|
||||
if preserve_reference_context_item {
|
||||
for item in forked_rollout_items.iter().rev() {
|
||||
let RolloutItem::Compacted(compacted) = item else {
|
||||
continue;
|
||||
};
|
||||
// Legacy checkpoints force the child to rebuild context regardless of the
|
||||
// live parent's reference baseline; an older superseded checkpoint does not.
|
||||
if compacted.replacement_history.is_none() {
|
||||
preserve_reference_context_item = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
let mut replaced_parent_developer_instructions = false;
|
||||
// Scrub inherited hints and replace only the parent's developer-instruction fragment.
|
||||
// Compaction stores response items separately, so sanitize both top-level messages and
|
||||
// compacted replacement histories with the same policy.
|
||||
let retain_forked_item = |response_item: &mut ResponseItem, replaced: &mut bool| {
|
||||
if is_multi_agent_v2_usage_hint_message(
|
||||
response_item,
|
||||
&multi_agent_v2_usage_hint_texts_to_filter,
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if let Some(parent_developer_instructions) = parent_developer_instructions.as_ref()
|
||||
&& let Some(subagent_developer_instructions) =
|
||||
subagent_developer_instructions.as_ref()
|
||||
&& let ResponseItem::Message { role, content, .. } = response_item
|
||||
&& role == "developer"
|
||||
{
|
||||
content.retain_mut(|content_item| {
|
||||
let ContentItem::InputText { text } = content_item else {
|
||||
return true;
|
||||
};
|
||||
// TODO(anp) track better message fragment provenance in rollouts.
|
||||
if !text.contains(parent_developer_instructions) {
|
||||
return true;
|
||||
}
|
||||
|
||||
*replaced = true;
|
||||
let replacement = if preserve_reference_context_item {
|
||||
subagent_developer_instructions.as_str()
|
||||
} else {
|
||||
""
|
||||
};
|
||||
*text = text.replace(parent_developer_instructions, replacement);
|
||||
!text.is_empty()
|
||||
});
|
||||
return !content.is_empty();
|
||||
}
|
||||
|
||||
true
|
||||
};
|
||||
forked_rollout_items.retain_mut(|item| {
|
||||
if !keep_forked_rollout_item(item, preserve_reference_context_item)
|
||||
|| destination_history_mode == Some(ThreadHistoryMode::Paginated)
|
||||
&& matches!(
|
||||
&*item,
|
||||
RolloutItem::EventMsg(
|
||||
EventMsg::ItemCompleted(_)
|
||||
| EventMsg::TokenCount(_)
|
||||
| EventMsg::ThreadGoalUpdated(_)
|
||||
| EventMsg::ThreadSettingsApplied(_),
|
||||
)
|
||||
)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
match item {
|
||||
RolloutItem::ResponseItem(response_item) => {
|
||||
retain_forked_item(response_item, &mut replaced_parent_developer_instructions)
|
||||
}
|
||||
RolloutItem::Compacted(compacted) => {
|
||||
if let Some(replacement_history) = compacted.replacement_history.as_mut() {
|
||||
// Matches before this checkpoint cannot survive its replacement history.
|
||||
replaced_parent_developer_instructions = false;
|
||||
replacement_history.retain_mut(|response_item| {
|
||||
retain_forked_item(
|
||||
response_item,
|
||||
&mut replaced_parent_developer_instructions,
|
||||
)
|
||||
});
|
||||
}
|
||||
true
|
||||
}
|
||||
RolloutItem::EventMsg(_)
|
||||
| RolloutItem::SessionMeta(_)
|
||||
| RolloutItem::TurnContext(_)
|
||||
| RolloutItem::InterAgentCommunication(_)
|
||||
| RolloutItem::InterAgentCommunicationMetadata { .. }
|
||||
| RolloutItem::WorldState(_) => true,
|
||||
}
|
||||
});
|
||||
// Full forks reuse the parent's reference context instead of rebuilding it. If that
|
||||
// context omitted the parent's developer fragment, append the child's override so its
|
||||
// instructions still reach the model exactly once.
|
||||
if let Some(subagent_developer_instructions) = subagent_developer_instructions.as_ref()
|
||||
&& preserve_reference_context_item
|
||||
&& !replaced_parent_developer_instructions
|
||||
&& !subagent_developer_instructions.is_empty()
|
||||
&& parent_thread
|
||||
.session
|
||||
.reference_context_item()
|
||||
.await
|
||||
.is_some()
|
||||
&& let Some(developer_message) =
|
||||
crate::context_manager::updates::build_developer_update_item(vec![
|
||||
subagent_developer_instructions.clone(),
|
||||
])
|
||||
{
|
||||
forked_rollout_items.push(RolloutItem::ResponseItem(developer_message));
|
||||
}
|
||||
if preserve_reference_context_item
|
||||
&& multi_agent_version == MultiAgentVersion::V2
|
||||
&& let Some(subagent_usage_hint_text) =
|
||||
|
||||
Reference in new issue
Block a user