Files
codex/codex-rs/prompts/src/multi_agent_instructions.rs
T
rhan-oai 663358f98d Keep agent concurrency guidance when native tools are disabled (#62864)
## Why

Disabling native multi-agent tools also dropped the default guidance about
how many agents can be active in a session. That limit still matters for
agents running on a host that handles turn admission.

## What changed

When `multi_agent_v2.disable_native_tools` is set, root agents and
subagents receive only the configured
`multi_agent_v2.max_concurrent_threads_per_session` in their default role
guidance. Explicit role overrides retain precedence, and empty role text
still suppresses the guidance.

## Testing

Cover both roles with bundled and catalog-provided role text. Update the
request snapshots for explicit and proactive delegation to show the
concurrency hint with native tools hidden.

GitOrigin-RevId: e940e7a7886b69888002307c021607b19bb87cd0
2026-11-20 23:44:47 +00:00

107 lines
4.6 KiB
Rust

//! Assembles multi-agent role instructions from selected text and runtime capabilities.
//! The segment owns rendering and attribution; consumers select and capture its inputs.
use crate::without_update_plan_instructions;
use codex_context_fragments::ContextualUserFragment;
use codex_protocol::models::ContentItem;
use codex_protocol::models::ContentItemKind;
use codex_protocol::models::input_text;
const DEFAULT_MULTI_AGENT_V2_MODEL_OVERRIDE_USAGE_HINT_TEXT: &str = "Full-history forks (`fork_turns` omitted or `\"all\"`) inherit the parent model and reasoning effort and do not accept overrides. Only set `model` or `reasoning_effort` when explicitly requested by the user, applicable `AGENTS.md` instructions, or skill instructions; when doing so, set `fork_turns` to `\"none\"`.";
const DEFAULT_MULTI_AGENT_V2_WAIT_AGENT_USAGE_HINT_TEXT: &str =
"When calling `wait_agent`, prefer longer waits (minutes) to avoid busy polling.";
const DEFAULT_MULTI_AGENT_V2_SHARED_USAGE_HINT_TEXT: &str = r#"Note that collaboration tools cannot be called from inside `functions.exec`. Call `spawn_agent`, `send_message`, `followup_task`, `wait_agent`, `interrupt_agent`, and `list_agents` only as direct tool calls using the recipient shown in their tool definitions, such as `to=functions.collaboration.spawn_agent`, since they are intentionally absent from the `functions.exec` `tools.*` namespace. Available tools in `functions.exec` are explicitly described with a `tools` namespace in the developer message.
All agents share the same directory. In detail:
- All agents have access to the same container and filesystem as you.
- All agents use the same current working directory.
- As a result, edits made by one agent are immediately visible to all other agents.
"#;
/// Multi-agent role text and the captured capabilities used to render its context segment.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MultiAgentRoleInstructions {
/// Complete role body.
Configured(String),
/// Selected catalog or bundled text, composed with captured runtime guidance.
Composed {
base: String,
omit_update_plan_instructions: bool,
max_concurrency: usize,
wait_agent_enabled: bool,
expose_model_overrides: bool,
},
}
impl MultiAgentRoleInstructions {
/// Content classification shared by role producers and inherited-history filtering.
pub const KIND: &str = "multi_agent.role_instructions";
pub fn concurrency_only(max_concurrency: usize) -> Self {
Self::Configured(concurrency_guidance(max_concurrency))
}
}
impl ContextualUserFragment for MultiAgentRoleInstructions {
fn content_kind(&self) -> ContentItemKind {
ContentItemKind(Self::KIND.to_string())
}
fn role(&self) -> &'static str {
"developer"
}
fn markers(&self) -> (&'static str, &'static str) {
match self {
Self::Configured(text) if Self::matches_text(text) => ("", ""),
Self::Configured(_) | Self::Composed { .. } => Self::type_markers(),
}
}
fn type_markers() -> (&'static str, &'static str) {
("<multi_agent_role>", "</multi_agent_role>")
}
fn body(&self) -> ContentItem {
input_text(match self {
Self::Configured(text) => text.clone(),
Self::Composed {
base,
omit_update_plan_instructions,
max_concurrency,
wait_agent_enabled,
expose_model_overrides,
} => {
let base = if *omit_update_plan_instructions {
without_update_plan_instructions(base)
} else {
base.clone()
};
let wait_agent_guidance = if *wait_agent_enabled {
format!("{DEFAULT_MULTI_AGENT_V2_WAIT_AGENT_USAGE_HINT_TEXT}\n\n")
} else {
String::new()
};
let shared = DEFAULT_MULTI_AGENT_V2_SHARED_USAGE_HINT_TEXT;
let concurrency = concurrency_guidance(*max_concurrency);
let mut text = format!("{base}\n{shared}\n{wait_agent_guidance}{concurrency}");
if *expose_model_overrides {
text.push_str("\n\n");
text.push_str(DEFAULT_MULTI_AGENT_V2_MODEL_OVERRIDE_USAGE_HINT_TEXT);
}
text
}
})
}
}
fn concurrency_guidance(max_concurrency: usize) -> String {
format!(
"There are {max_concurrency} available concurrency slots, meaning that up to {max_concurrency} agents can be active at once, including you."
)
}
#[cfg(test)]
#[path = "multi_agent_instructions_tests.rs"]
mod tests;