Skip to content

codex-rs: Make agent instructions modifiable #1338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion codex-rs/core/src/client_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ pub struct Prompt {
/// the "fully qualified" tool name (i.e., prefixed with the server name),
/// which should be reported to the model in place of Tool::name.
pub extra_tools: HashMap<String, mcp_types::Tool>,

/// Allow agents to override the prompt instructions
pub agent_instructions: Option<String>,
}

impl Prompt {
pub(crate) fn get_full_instructions(&self, model: &str) -> Cow<str> {
let mut sections: Vec<&str> = vec![BASE_INSTRUCTIONS];
let mut sections: Vec<&str> = vec![self.agent_instructions.as_ref().map_or(BASE_INSTRUCTIONS, |z| z.as_str())];
if let Some(ref user) = self.user_instructions {
sections.push(user);
}
Expand Down
7 changes: 7 additions & 0 deletions codex-rs/core/src/codex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ impl Codex {
disable_response_storage: config.disable_response_storage,
notify: config.notify.clone(),
cwd: config.cwd.clone(),
agent_instructions: config.agent_instructions.clone(),
};

let config = Arc::new(config);
Expand Down Expand Up @@ -191,6 +192,9 @@ pub(crate) struct Session {
rollout: Mutex<Option<crate::rollout::RolloutRecorder>>,
state: Mutex<State>,
codex_linux_sandbox_exe: Option<PathBuf>,

/// This session's current agent instructions.
agent_instructions: Option<String>,
}

impl Session {
Expand Down Expand Up @@ -564,6 +568,7 @@ async fn submission_loop(
disable_response_storage,
notify,
cwd,
agent_instructions,
} => {
info!("Configuring session: model={model}; provider={provider:?}");
if !cwd.is_absolute() {
Expand Down Expand Up @@ -667,6 +672,7 @@ async fn submission_loop(
state: Mutex::new(state),
rollout: Mutex::new(rollout_recorder),
codex_linux_sandbox_exe: config.codex_linux_sandbox_exe.clone(),
agent_instructions,
}));

// Gather history metadata for SessionConfiguredEvent.
Expand Down Expand Up @@ -1010,6 +1016,7 @@ async fn run_turn(
user_instructions: sess.instructions.clone(),
store,
extra_tools,
agent_instructions: sess.agent_instructions.clone(),
};

let mut retries = 0;
Expand Down
13 changes: 13 additions & 0 deletions codex-rs/core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ pub struct Config {
/// If not "none", the value to use for `reasoning.summary` when making a
/// request using the Responses API.
pub model_reasoning_summary: ReasoningSummary,

/// Agent Instructions defines how the agent should function.
/// This defaults to the prompt.md contents located within codex-rs.
pub agent_instructions: Option<String>,
}

impl Config {
Expand Down Expand Up @@ -332,6 +336,10 @@ pub struct ConfigOverrides {
pub model_provider: Option<String>,
pub config_profile: Option<String>,
pub codex_linux_sandbox_exe: Option<PathBuf>,

/// Default agent instructions to use when configuring codex.
/// When None this will be set to the default agent instructions
pub agent_instructions: Option<String>,
}

impl Config {
Expand All @@ -353,6 +361,7 @@ impl Config {
model_provider,
config_profile: config_profile_key,
codex_linux_sandbox_exe,
agent_instructions
} = overrides;

let config_profile = match config_profile_key.or(cfg.profile) {
Expand Down Expand Up @@ -459,6 +468,7 @@ impl Config {
hide_agent_reasoning: cfg.hide_agent_reasoning.unwrap_or(false),
model_reasoning_effort: cfg.model_reasoning_effort.unwrap_or_default(),
model_reasoning_summary: cfg.model_reasoning_summary.unwrap_or_default(),
agent_instructions,
};
Ok(config)
}
Expand Down Expand Up @@ -803,6 +813,7 @@ disable_response_storage = true
hide_agent_reasoning: false,
model_reasoning_effort: ReasoningEffort::default(),
model_reasoning_summary: ReasoningSummary::default(),
agent_instructions: None,
},
o3_profile_config
);
Expand Down Expand Up @@ -845,6 +856,7 @@ disable_response_storage = true
hide_agent_reasoning: false,
model_reasoning_effort: ReasoningEffort::default(),
model_reasoning_summary: ReasoningSummary::default(),
agent_instructions: None,
};

assert_eq!(expected_gpt3_profile_config, gpt3_profile_config);
Expand Down Expand Up @@ -902,6 +914,7 @@ disable_response_storage = true
hide_agent_reasoning: false,
model_reasoning_effort: ReasoningEffort::default(),
model_reasoning_summary: ReasoningSummary::default(),
agent_instructions: None,
};

assert_eq!(expected_zdr_profile_config, zdr_profile_config);
Expand Down
3 changes: 3 additions & 0 deletions codex-rs/core/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ pub enum Op {
/// `ConfigureSession` operation so that the business-logic layer can
/// operate deterministically.
cwd: std::path::PathBuf,

/// defines the system instructions for the codex agent
agent_instructions: Option<String>,
},

/// Abort current task.
Expand Down
1 change: 1 addition & 0 deletions codex-rs/exec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub async fn run_main(cli: Cli, codex_linux_sandbox_exe: Option<PathBuf>) -> any
cwd: cwd.map(|p| p.canonicalize().unwrap_or(p)),
model_provider: None,
codex_linux_sandbox_exe,
agent_instructions: None,
};
// Parse `-c` overrides.
let cli_kv_overrides = match config_overrides.parse_overrides() {
Expand Down
1 change: 1 addition & 0 deletions codex-rs/mcp-server/src/codex_tool_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ impl CodexToolCallParam {
sandbox_policy,
model_provider: None,
codex_linux_sandbox_exe,
agent_instructions: None,
};

let cli_overrides = cli_overrides
Expand Down
1 change: 1 addition & 0 deletions codex-rs/tui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub fn run_main(cli: Cli, codex_linux_sandbox_exe: Option<PathBuf>) -> std::io::
model_provider: None,
config_profile: cli.config_profile.clone(),
codex_linux_sandbox_exe,
agent_instructions: None,
};
// Parse `-c` overrides from the CLI.
let cli_kv_overrides = match cli.config_overrides.parse_overrides() {
Expand Down