mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Centralize model-message resolution and rendering in codex-prompts (#46026)
## What changed - Add `ResolvedModelMessages` to resolve catalog text and bundled defaults while preserving explicit empty overrides and their source. - Move base-instruction rendering, Guardian prompt composition, multi-agent role rendering, and `update_plan` guidance filtering into `codex-prompts`; migrate consumers to the shared APIs. - Separate permission-profile resolution from prompt composition, and annotate Guardian policy and classifier instructions with content kinds. ## Testing Add and update coverage for missing versus empty templates, literal overrides, multi-agent role composition, Guardian policy substitution and truncation, and preservation of permission path spellings and order. GitOrigin-RevId: 52335bb7acec0f432d5c57acb2accd5f0276056e
This commit is contained in:
1 parent
0d083092b4
commit
a8c36ca6d2
94 files changed
+2345
-2000
No files matched your search
@@ -0,0 +1,22 @@
|
||||
//! Resolves collaboration catalog text and bundled presets while retaining their source.
|
||||
//! Runtime consumers use catalog overrides first, then the selected mode's settings.
|
||||
|
||||
use super::ResolvedMessage;
|
||||
use codex_collaboration_mode_templates::DEFAULT;
|
||||
use codex_collaboration_mode_templates::PLAN;
|
||||
use codex_protocol::openai_models::CollaborationModeMessages;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct ResolvedCollaborationModeMessages<'a> {
|
||||
pub default: ResolvedMessage<'a>,
|
||||
pub plan: ResolvedMessage<'a>,
|
||||
}
|
||||
|
||||
impl<'a> ResolvedCollaborationModeMessages<'a> {
|
||||
pub(crate) fn new(messages: Option<&'a CollaborationModeMessages>) -> Self {
|
||||
Self {
|
||||
default: ResolvedMessage::new(messages.and_then(|m| m.default.as_deref()), DEFAULT),
|
||||
plan: ResolvedMessage::new(messages.and_then(|m| m.plan.as_deref()), PLAN),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
//! Resolves Guardian policy, feedback, and classifier text from catalog values or bundled defaults.
|
||||
//! Configuration overlays and prompt composition remain with consumers.
|
||||
|
||||
use codex_protocol::openai_models::AutoReviewMessages;
|
||||
use codex_protocol::openai_models::ModelMessages;
|
||||
|
||||
const POLICY: &str = include_str!("../../templates/guardian/policy.md");
|
||||
const POLICY_TEMPLATE: &str = include_str!("../../templates/guardian/policy_template.md");
|
||||
const NODE_REPL_POLICY: &str = include_str!("../../templates/guardian/node_repl_policy.md");
|
||||
const REJECTION_INSTRUCTIONS: &str = concat!(
|
||||
"The agent must not attempt to achieve the same outcome via workaround, ",
|
||||
"indirect execution, or policy circumvention. ",
|
||||
"Proceed only with a materially safer alternative, ",
|
||||
"or if the user explicitly approves the action after being informed of the risk. ",
|
||||
"Otherwise, stop and request user input.",
|
||||
);
|
||||
const TIMEOUT_INSTRUCTIONS: &str = concat!(
|
||||
"The automatic permission approval review did not finish before its deadline. ",
|
||||
"Do not assume the action is unsafe based on the timeout alone. ",
|
||||
"You may retry once, or ask the user for guidance or explicit approval.",
|
||||
);
|
||||
const CLASSIFIER_INSTRUCTIONS: &str =
|
||||
include_str!("../../templates/guardian/classifier_instructions.md");
|
||||
|
||||
/// Auto-review policy and feedback text before runtime configuration overlays and rendering.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct ResolvedAutoReviewMessages<'a> {
|
||||
pub policy: &'a str,
|
||||
pub policy_template: &'a str,
|
||||
pub node_repl_policy: &'a str,
|
||||
pub rejection_instructions: &'a str,
|
||||
pub timeout_instructions: &'a str,
|
||||
}
|
||||
|
||||
impl<'a> ResolvedAutoReviewMessages<'a> {
|
||||
pub(crate) fn new(messages: Option<&'a AutoReviewMessages>) -> Self {
|
||||
Self {
|
||||
policy: messages
|
||||
.and_then(|messages| messages.policy.as_deref())
|
||||
.unwrap_or(POLICY),
|
||||
policy_template: messages
|
||||
.and_then(|messages| messages.policy_template.as_deref())
|
||||
.unwrap_or(POLICY_TEMPLATE),
|
||||
node_repl_policy: messages
|
||||
.and_then(|messages| messages.node_repl_policy.as_deref())
|
||||
.unwrap_or(NODE_REPL_POLICY),
|
||||
rejection_instructions: messages
|
||||
.and_then(|messages| messages.rejection_instructions.as_deref())
|
||||
.unwrap_or(REJECTION_INSTRUCTIONS),
|
||||
timeout_instructions: messages
|
||||
.and_then(|messages| messages.timeout_instructions.as_deref())
|
||||
.unwrap_or(TIMEOUT_INSTRUCTIONS),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn classifier_instructions(messages: Option<&ModelMessages>) -> &str {
|
||||
messages
|
||||
.and_then(|messages| messages.guardian_v2.as_ref())
|
||||
.and_then(|messages| messages.classifier_instructions.as_deref())
|
||||
.unwrap_or(CLASSIFIER_INSTRUCTIONS)
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
//! Resolves multi-agent role bases and mode text while preserving catalog provenance.
|
||||
//! Consumers own role assembly, suppression, and effort-dependent mode selection.
|
||||
|
||||
use super::ResolvedMessage;
|
||||
use codex_protocol::openai_models::MultiAgentMessages;
|
||||
|
||||
const DEFAULT_MULTI_AGENT_V2_ROOT_AGENT_USAGE_HINT_TEXT: &str = r#"You are `/root`, the primary agent in a team of agents collaborating to fulfill the user's goals.
|
||||
|
||||
At the start of your turn, you are the active agent.
|
||||
You can spawn sub-agents to handle subtasks, and those sub-agents can spawn their own sub-agents.
|
||||
All agents in the team, including the agents that you can assign tasks to, are equally intelligent and capable, and have access to the same set of tools.
|
||||
|
||||
You can use `spawn_agent` to create a new agent, `followup_task` to give an existing agent a new task and trigger a turn, and `send_message` to pass a message to a running agent without triggering a turn.
|
||||
Child agents can also spawn their own sub-agents.
|
||||
You can decide how much context you want to propagate to your sub-agents with the `fork_turns` parameter.
|
||||
|
||||
You will receive messages in the analysis channel in the form:
|
||||
```
|
||||
Message Type: MESSAGE | FINAL_ANSWER
|
||||
Task name: <recipient>
|
||||
Sender: <author>
|
||||
Payload:
|
||||
<payload text>
|
||||
```
|
||||
They may be addressed as to=/root
|
||||
"#;
|
||||
const DEFAULT_MULTI_AGENT_V2_SUBAGENT_USAGE_HINT_TEXT: &str = r#"You are an agent in a team of agents collaborating to complete a task.
|
||||
|
||||
You can spawn sub-agents to handle subtasks, and those sub-agents can spawn their own sub-agents. All agents in the team, including the agents that you can assign tasks to, are equally intelligent and capable, and have access to the same set of tools.
|
||||
|
||||
You can use `spawn_agent` to create a new agent, `followup_task` to give an existing agent a new task and trigger a turn, and `send_message` to pass a message to a running agent.
|
||||
Child agents can also spawn their own sub-agents.
|
||||
|
||||
When you provide a response in the final channel, that content is immediately delivered back to your parent agent.
|
||||
|
||||
You will receive messages in the analysis channel in the form:
|
||||
```
|
||||
Message Type: NEW_TASK | MESSAGE | FINAL_ANSWER
|
||||
Task name: <recipient>
|
||||
Sender: <author>
|
||||
Payload:
|
||||
<payload text>
|
||||
```
|
||||
You may also see them addressed as to=/root/..., which indicates your identity is /root/...
|
||||
"#;
|
||||
const EXPLICIT_REQUEST_ONLY_MULTI_AGENT_MODE_TEXT: &str = "Any earlier instruction enabling proactive multi-agent delegation no longer applies. Do not spawn sub-agents unless the user or applicable AGENTS.md/skill instructions explicitly ask for sub-agents, delegation, or parallel agent work.";
|
||||
const PROACTIVE_MULTI_AGENT_MODE_TEXT: &str = "Proactive multi-agent delegation is active. Any earlier developer instruction requiring an explicit user request before spawning sub-agents no longer applies. This mode remains active until a later multi-agent mode developer message changes it. User requests override this hint.\n\nIf at any point you can parallelize work by delegating tasks to another agent (no matter if you are root or subagent), you should do so using collaboration tools if it could save time or improve quality.";
|
||||
|
||||
/// Model-only role bases and mode alternatives for runtime selection.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct ResolvedMultiAgentMessages<'a> {
|
||||
pub root: ResolvedMessage<'a>,
|
||||
pub subagent: ResolvedMessage<'a>,
|
||||
pub explicit: ResolvedMessage<'a>,
|
||||
pub proactive: ResolvedMessage<'a>,
|
||||
/// A supplied hint overrides both effort-specific modes, including when empty.
|
||||
pub hint: Option<&'a str>,
|
||||
}
|
||||
|
||||
impl<'a> ResolvedMultiAgentMessages<'a> {
|
||||
pub(crate) fn new(messages: Option<&'a MultiAgentMessages>) -> Self {
|
||||
let role = messages.and_then(|messages| messages.role.as_ref());
|
||||
let mode = messages.and_then(|messages| messages.mode.as_ref());
|
||||
Self {
|
||||
root: ResolvedMessage::new(
|
||||
role.and_then(|role| role.root.as_deref()),
|
||||
DEFAULT_MULTI_AGENT_V2_ROOT_AGENT_USAGE_HINT_TEXT,
|
||||
),
|
||||
subagent: ResolvedMessage::new(
|
||||
role.and_then(|role| role.subagent.as_deref()),
|
||||
DEFAULT_MULTI_AGENT_V2_SUBAGENT_USAGE_HINT_TEXT,
|
||||
),
|
||||
explicit: ResolvedMessage::new(
|
||||
mode.and_then(|mode| mode.explicit.as_deref()),
|
||||
EXPLICIT_REQUEST_ONLY_MULTI_AGENT_MODE_TEXT,
|
||||
),
|
||||
proactive: ResolvedMessage::new(
|
||||
mode.and_then(|mode| mode.proactive.as_deref()),
|
||||
PROACTIVE_MULTI_AGENT_MODE_TEXT,
|
||||
),
|
||||
hint: mode.and_then(|mode| mode.hint_text.as_deref()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
//! Resolves permission and approval text from sparse catalog messages and bundled defaults.
|
||||
//! Preserves explicit empty strings and source identity; composition and rendering live in
|
||||
//! `permissions_instructions` and consume caller-resolved runtime facts.
|
||||
|
||||
use super::ResolvedMessage;
|
||||
use codex_protocol::openai_models::ApprovalMessages;
|
||||
use codex_protocol::openai_models::PermissionMessages;
|
||||
|
||||
const APPROVAL_POLICY_NEVER: &str =
|
||||
include_str!("../../templates/permissions/approval_policy/never.md");
|
||||
const APPROVAL_POLICY_UNLESS_TRUSTED: &str =
|
||||
include_str!("../../templates/permissions/approval_policy/unless_trusted.md");
|
||||
const APPROVAL_POLICY_ON_REQUEST: &str =
|
||||
include_str!("../../templates/permissions/approval_policy/on_request.md");
|
||||
|
||||
pub(crate) const DANGER_FULL_ACCESS_TEMPLATE: &str =
|
||||
include_str!("../../templates/permissions/sandbox_mode/danger_full_access.md");
|
||||
pub(crate) const WORKSPACE_WRITE_TEMPLATE: &str =
|
||||
include_str!("../../templates/permissions/sandbox_mode/workspace_write.md");
|
||||
pub(crate) const READ_ONLY_TEMPLATE: &str =
|
||||
include_str!("../../templates/permissions/sandbox_mode/read_only.md");
|
||||
|
||||
/// Resolved approval-policy text; policy and tool selection remain with the consumer.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub(crate) struct ResolvedApprovalMessages<'a> {
|
||||
pub(crate) never: ResolvedMessage<'a>,
|
||||
pub(crate) on_request: ResolvedMessage<'a>,
|
||||
pub(crate) on_request_auto_review: ResolvedMessage<'a>,
|
||||
pub(crate) unless_trusted: ResolvedMessage<'a>,
|
||||
}
|
||||
|
||||
impl<'a> ResolvedApprovalMessages<'a> {
|
||||
pub(crate) fn new(messages: Option<&'a ApprovalMessages>) -> Self {
|
||||
Self {
|
||||
never: ResolvedMessage::new(
|
||||
messages.and_then(|m| m.never.as_deref()),
|
||||
APPROVAL_POLICY_NEVER,
|
||||
),
|
||||
on_request: ResolvedMessage::new(
|
||||
messages.and_then(|m| m.on_request.as_deref()),
|
||||
APPROVAL_POLICY_ON_REQUEST,
|
||||
),
|
||||
on_request_auto_review: ResolvedMessage::new(
|
||||
messages.and_then(|m| m.on_request_auto_review.as_deref()),
|
||||
APPROVAL_POLICY_ON_REQUEST,
|
||||
),
|
||||
unless_trusted: ResolvedMessage::new(
|
||||
messages.and_then(|m| m.unless_trusted.as_deref()),
|
||||
APPROVAL_POLICY_UNLESS_TRUSTED,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Permission text retains its source so renderers can distinguish catalog text from templates.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub(crate) struct ResolvedPermissionMessages<'a> {
|
||||
pub(crate) danger_full_access: ResolvedMessage<'a>,
|
||||
pub(crate) workspace_write: ResolvedMessage<'a>,
|
||||
pub(crate) read_only: ResolvedMessage<'a>,
|
||||
}
|
||||
|
||||
impl<'a> ResolvedPermissionMessages<'a> {
|
||||
pub(crate) fn new(messages: Option<&'a PermissionMessages>) -> Self {
|
||||
Self {
|
||||
danger_full_access: ResolvedMessage::new(
|
||||
messages.and_then(|m| m.danger_full_access.as_deref()),
|
||||
DANGER_FULL_ACCESS_TEMPLATE,
|
||||
),
|
||||
workspace_write: ResolvedMessage::new(
|
||||
messages.and_then(|m| m.workspace_write.as_deref()),
|
||||
WORKSPACE_WRITE_TEMPLATE,
|
||||
),
|
||||
read_only: ResolvedMessage::new(
|
||||
messages.and_then(|m| m.read_only.as_deref()),
|
||||
READ_ONLY_TEMPLATE,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user