merge(upstream): sync with openai/codex main into upstream-merge\n\n- Prefer upstream in core: adopt new ToolsConfig API and OpenAI tools plumbing\n- Keep our TUI/tooling; no conflicts in prefer-ours buckets\n- Purge rules respected; no reintroduced assets\n\nBuild: ./build-fast.sh ok; verify.sh ok

This commit is contained in:
just-every-code
2025-09-10 22:27:14 +00:00
7 changed files with 375 additions and 742 deletions
-1
View File
@@ -115,7 +115,6 @@ async fn run_llm_request(
prompt.input = input;
prompt.store = true;
prompt.user_instructions = None;
prompt.environment_context = None;
prompt.status_items = vec![];
prompt.base_instructions_override = None;
prompt.text_format = Some(text_format);
+6
View File
@@ -797,6 +797,7 @@ async fn execute_model_with_permissions(
}
// Tool creation functions
#[allow(dead_code)]
pub fn create_run_agent_tool() -> OpenAiTool {
let mut properties = BTreeMap::new();
@@ -862,6 +863,7 @@ pub fn create_run_agent_tool() -> OpenAiTool {
})
}
#[allow(dead_code)]
pub fn create_check_agent_status_tool() -> OpenAiTool {
let mut properties = BTreeMap::new();
@@ -884,6 +886,7 @@ pub fn create_check_agent_status_tool() -> OpenAiTool {
})
}
#[allow(dead_code)]
pub fn create_get_agent_result_tool() -> OpenAiTool {
let mut properties = BTreeMap::new();
@@ -906,6 +909,7 @@ pub fn create_get_agent_result_tool() -> OpenAiTool {
})
}
#[allow(dead_code)]
pub fn create_cancel_agent_tool() -> OpenAiTool {
let mut properties = BTreeMap::new();
@@ -940,6 +944,7 @@ pub fn create_cancel_agent_tool() -> OpenAiTool {
})
}
#[allow(dead_code)]
pub fn create_wait_for_agent_tool() -> OpenAiTool {
let mut properties = BTreeMap::new();
@@ -992,6 +997,7 @@ pub fn create_wait_for_agent_tool() -> OpenAiTool {
})
}
#[allow(dead_code)]
pub fn create_list_agents_tool() -> OpenAiTool {
let mut properties = BTreeMap::new();
+1 -1
View File
@@ -46,7 +46,7 @@ pub struct Prompt {
/// A list of key-value pairs that will be added as a developer message
/// for the model to use
pub environment_context: Option<EnvironmentContext>,
pub(crate) environment_context: Option<EnvironmentContext>,
/// Tools available to the model, including additional tools sourced from
/// external MCP servers.
+11 -15
View File
@@ -378,6 +378,7 @@ use codex_protocol::models::ResponseInputItem;
use codex_protocol::models::ResponseItem;
use codex_protocol::models::ShellToolCallParams;
use crate::openai_tools::ToolsConfig;
use crate::openai_tools::ToolsConfigParams;
use crate::openai_tools::get_openai_tools;
use crate::parse_command::parse_command;
use crate::plan_tool::handle_update_plan;
@@ -1673,18 +1674,17 @@ async fn submission_loop(
}
}
let default_shell = shell::default_user_shell().await;
let mut tools_config = ToolsConfig::new(
&config.model_family,
let tools_config = ToolsConfig::new(&ToolsConfigParams {
model_family: &config.model_family,
approval_policy,
sandbox_policy.clone(),
config.include_plan_tool,
config.include_apply_patch_tool,
config.tools_web_search_request,
config.use_experimental_streamable_shell_tool,
config.include_view_image_tool,
);
tools_config.web_search_allowed_domains =
config.tools_web_search_allowed_domains.clone();
sandbox_policy: sandbox_policy.clone(),
include_plan_tool: config.include_plan_tool,
include_apply_patch_tool: config.include_apply_patch_tool,
include_web_search_request: config.tools_web_search_request,
use_streamable_shell_tool: config
.use_experimental_streamable_shell_tool,
include_view_image_tool: config.include_view_image_tool,
});
sess = Some(Arc::new(Session {
client,
@@ -2160,13 +2160,9 @@ async fn run_turn(
sub_id: String,
input: Vec<ResponseItem>,
) -> CodexResult<Vec<ProcessedResponseItem>> {
// Check if browser is enabled
let browser_enabled = codex_browser::global::get_browser_manager().await.is_some();
let tools = get_openai_tools(
&sess.tools_config,
Some(sess.mcp_connection_manager.list_all_tools()),
browser_enabled,
);
let mut retries = 0;
+104 -10
View File
@@ -8,12 +8,10 @@ use crate::shell::Shell;
use codex_protocol::config_types::SandboxMode;
use codex_protocol::models::ContentItem;
use codex_protocol::models::ResponseItem;
use codex_protocol::protocol::ENVIRONMENT_CONTEXT_CLOSE_TAG;
use codex_protocol::protocol::ENVIRONMENT_CONTEXT_OPEN_TAG;
use std::path::PathBuf;
/// wraps environment context message in a tag for the model to parse more easily.
pub(crate) const ENVIRONMENT_CONTEXT_START: &str = "<environment_context>";
pub(crate) const ENVIRONMENT_CONTEXT_END: &str = "</environment_context>";
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, DeriveDisplay)]
#[serde(rename_all = "kebab-case")]
#[strum(serialize_all = "kebab-case")]
@@ -23,11 +21,12 @@ pub enum NetworkAccess {
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename = "environment_context", rename_all = "snake_case")]
pub struct EnvironmentContext {
pub(crate) struct EnvironmentContext {
pub cwd: Option<PathBuf>,
pub approval_policy: Option<AskForApproval>,
pub sandbox_mode: Option<SandboxMode>,
pub network_access: Option<NetworkAccess>,
pub writable_roots: Option<Vec<PathBuf>>,
pub shell: Option<Shell>,
}
@@ -59,6 +58,16 @@ impl EnvironmentContext {
}
None => None,
},
writable_roots: match sandbox_policy {
Some(SandboxPolicy::WorkspaceWrite { writable_roots, .. }) => {
if writable_roots.is_empty() {
None
} else {
Some(writable_roots.clone())
}
}
_ => None,
},
shell,
}
}
@@ -74,12 +83,13 @@ impl EnvironmentContext {
/// <cwd>...</cwd>
/// <approval_policy>...</approval_policy>
/// <sandbox_mode>...</sandbox_mode>
/// <writable_roots>...</writable_roots>
/// <network_access>...</network_access>
/// <shell>...</shell>
/// </environment_context>
/// ```
pub fn serialize_to_xml(self) -> String {
let mut lines = vec![ENVIRONMENT_CONTEXT_START.to_string()];
let mut lines = vec![ENVIRONMENT_CONTEXT_OPEN_TAG.to_string()];
if let Some(cwd) = self.cwd {
lines.push(format!(" <cwd>{}</cwd>", cwd.to_string_lossy()));
}
@@ -96,12 +106,22 @@ impl EnvironmentContext {
" <network_access>{network_access}</network_access>"
));
}
if let Some(shell) = self.shell {
if let Some(shell_name) = shell.name() {
lines.push(format!(" <shell>{}</shell>", shell_name));
if let Some(writable_roots) = self.writable_roots {
lines.push(" <writable_roots>".to_string());
for writable_root in writable_roots {
lines.push(format!(
" <root>{}</root>",
writable_root.to_string_lossy()
));
}
lines.push(" </writable_roots>".to_string());
}
lines.push(ENVIRONMENT_CONTEXT_END.to_string());
if let Some(shell) = self.shell
&& let Some(shell_name) = shell.name()
{
lines.push(format!(" <shell>{shell_name}</shell>"));
}
lines.push(ENVIRONMENT_CONTEXT_CLOSE_TAG.to_string());
lines.join("\n")
}
}
@@ -117,3 +137,77 @@ impl From<EnvironmentContext> for ResponseItem {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
fn workspace_write_policy(writable_roots: Vec<&str>, network_access: bool) -> SandboxPolicy {
SandboxPolicy::WorkspaceWrite {
writable_roots: writable_roots.into_iter().map(PathBuf::from).collect(),
network_access,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
}
}
#[test]
fn serialize_workspace_write_environment_context() {
let context = EnvironmentContext::new(
Some(PathBuf::from("/repo")),
Some(AskForApproval::OnRequest),
Some(workspace_write_policy(vec!["/repo", "/tmp"], false)),
None,
);
let expected = r#"<environment_context>
<cwd>/repo</cwd>
<approval_policy>on-request</approval_policy>
<sandbox_mode>workspace-write</sandbox_mode>
<network_access>restricted</network_access>
<writable_roots>
<root>/repo</root>
<root>/tmp</root>
</writable_roots>
</environment_context>"#;
assert_eq!(context.serialize_to_xml(), expected);
}
#[test]
fn serialize_read_only_environment_context() {
let context = EnvironmentContext::new(
None,
Some(AskForApproval::Never),
Some(SandboxPolicy::ReadOnly),
None,
);
let expected = r#"<environment_context>
<approval_policy>never</approval_policy>
<sandbox_mode>read-only</sandbox_mode>
<network_access>restricted</network_access>
</environment_context>"#;
assert_eq!(context.serialize_to_xml(), expected);
}
#[test]
fn serialize_full_access_environment_context() {
let context = EnvironmentContext::new(
None,
Some(AskForApproval::OnFailure),
Some(SandboxPolicy::DangerFullAccess),
None,
);
let expected = r#"<environment_context>
<approval_policy>on-failure</approval_policy>
<sandbox_mode>danger-full-access</sandbox_mode>
<network_access>enabled</network_access>
</environment_context>"#;
assert_eq!(context.serialize_to_xml(), expected);
}
}
File diff suppressed because it is too large Load Diff
+8 -2
View File
@@ -431,11 +431,17 @@ async fn overrides_turn_context_but_keeps_cached_prefix_and_key_constant() {
// After overriding the turn context, the environment context should be emitted again
// reflecting the new approval policy and sandbox settings. Omit cwd because it did
// not change.
let expected_env_text_2 = r#"<environment_context>
let expected_env_text_2 = format!(
r#"<environment_context>
<approval_policy>never</approval_policy>
<sandbox_mode>workspace-write</sandbox_mode>
<network_access>enabled</network_access>
</environment_context>"#;
<writable_roots>
<root>{}</root>
</writable_roots>
</environment_context>"#,
writable.path().to_string_lossy()
);
let expected_env_msg_2 = serde_json::json!({
"type": "message",
"id": serde_json::Value::Null,