Merge origin/main into rhan/surface-updates and resolve guardian tests conflict

This commit is contained in:
Roy Han
2026-03-16 17:42:39 -07:00
1516 changed files with 150231 additions and 6961 deletions

View File

@@ -18,6 +18,10 @@ const CONNECTOR_DESCRIPTION: &str = "Plan events and manage your calendar.";
const PROTOCOL_VERSION: &str = "2025-11-25";
const SERVER_NAME: &str = "codex-apps-test";
const SERVER_VERSION: &str = "1.0.0";
const SEARCHABLE_TOOL_COUNT: usize = 100;
pub const CALENDAR_CREATE_EVENT_RESOURCE_URI: &str =
"connector://calendar/tools/calendar_create_event";
const CALENDAR_LIST_EVENTS_RESOURCE_URI: &str = "connector://calendar/tools/calendar_list_events";
#[derive(Clone)]
pub struct AppsTestServer {
@@ -29,6 +33,21 @@ impl AppsTestServer {
Self::mount_with_connector_name(server, CONNECTOR_NAME).await
}
pub async fn mount_searchable(server: &MockServer) -> Result<Self> {
mount_oauth_metadata(server).await;
mount_connectors_directory(server).await;
mount_streamable_http_json_rpc(
server,
CONNECTOR_NAME.to_string(),
CONNECTOR_DESCRIPTION.to_string(),
/*searchable*/ true,
)
.await;
Ok(Self {
chatgpt_base_url: server.uri(),
})
}
pub async fn mount_with_connector_name(
server: &MockServer,
connector_name: &str,
@@ -39,6 +58,7 @@ impl AppsTestServer {
server,
connector_name.to_string(),
CONNECTOR_DESCRIPTION.to_string(),
/*searchable*/ false,
)
.await;
Ok(Self {
@@ -94,12 +114,14 @@ async fn mount_streamable_http_json_rpc(
server: &MockServer,
connector_name: String,
connector_description: String,
searchable: bool,
) {
Mock::given(method("POST"))
.and(path_regex("^/api/codex/apps/?$"))
.respond_with(CodexAppsJsonRpcResponder {
connector_name,
connector_description,
searchable,
})
.mount(server)
.await;
@@ -108,6 +130,7 @@ async fn mount_streamable_http_json_rpc(
struct CodexAppsJsonRpcResponder {
connector_name: String,
connector_description: String,
searchable: bool,
}
impl Respond for CodexAppsJsonRpcResponder {
@@ -154,7 +177,7 @@ impl Respond for CodexAppsJsonRpcResponder {
"notifications/initialized" => ResponseTemplate::new(202),
"tools/list" => {
let id = body.get("id").cloned().unwrap_or(Value::Null);
ResponseTemplate::new(200).set_body_json(json!({
let mut response = json!({
"jsonrpc": "2.0",
"id": id,
"result": {
@@ -175,7 +198,12 @@ impl Respond for CodexAppsJsonRpcResponder {
"_meta": {
"connector_id": CONNECTOR_ID,
"connector_name": self.connector_name.clone(),
"connector_description": self.connector_description.clone()
"connector_description": self.connector_description.clone(),
"_codex_apps": {
"resource_uri": CALENDAR_CREATE_EVENT_RESOURCE_URI,
"contains_mcp_source": true,
"connector_id": CONNECTOR_ID
}
}
},
{
@@ -192,13 +220,43 @@ impl Respond for CodexAppsJsonRpcResponder {
"_meta": {
"connector_id": CONNECTOR_ID,
"connector_name": self.connector_name.clone(),
"connector_description": self.connector_description.clone()
"connector_description": self.connector_description.clone(),
"_codex_apps": {
"resource_uri": CALENDAR_LIST_EVENTS_RESOURCE_URI,
"contains_mcp_source": true,
"connector_id": CONNECTOR_ID
}
}
}
],
"nextCursor": null
}
}))
});
if self.searchable
&& let Some(tools) = response
.pointer_mut("/result/tools")
.and_then(Value::as_array_mut)
{
for index in 2..SEARCHABLE_TOOL_COUNT {
tools.push(json!({
"name": format!("calendar_timezone_option_{index}"),
"description": format!("Read timezone option {index}."),
"inputSchema": {
"type": "object",
"properties": {
"timezone": { "type": "string" }
},
"additionalProperties": false
},
"_meta": {
"connector_id": CONNECTOR_ID,
"connector_name": self.connector_name.clone(),
"connector_description": self.connector_description.clone()
}
}));
}
}
ResponseTemplate::new(200).set_body_json(response)
}
"tools/call" => {
let id = body.get("id").cloned().unwrap_or(Value::Null);
@@ -214,6 +272,7 @@ impl Respond for CodexAppsJsonRpcResponder {
.pointer("/params/arguments/starts_at")
.and_then(Value::as_str)
.unwrap_or_default();
let codex_apps_meta = body.pointer("/params/_meta/_codex_apps").cloned();
ResponseTemplate::new(200).set_body_json(json!({
"jsonrpc": "2.0",
@@ -223,6 +282,9 @@ impl Respond for CodexAppsJsonRpcResponder {
"type": "text",
"text": format!("called {tool_name} for {title} at {starts_at}")
}],
"structuredContent": {
"_codex_apps": codex_apps_meta,
},
"isError": false
}
}))

View File

@@ -1,6 +1,11 @@
use regex_lite::Regex;
use serde_json::Value;
use std::sync::OnceLock;
use crate::responses::ResponsesRequest;
use codex_protocol::protocol::APPS_INSTRUCTIONS_OPEN_TAG;
use codex_protocol::protocol::PLUGINS_INSTRUCTIONS_OPEN_TAG;
use codex_protocol::protocol::SKILLS_INSTRUCTIONS_OPEN_TAG;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum ContextSnapshotRenderMode {
@@ -16,12 +21,16 @@ pub enum ContextSnapshotRenderMode {
#[derive(Debug, Clone)]
pub struct ContextSnapshotOptions {
render_mode: ContextSnapshotRenderMode,
strip_capability_instructions: bool,
strip_agents_md_user_context: bool,
}
impl Default for ContextSnapshotOptions {
fn default() -> Self {
Self {
render_mode: ContextSnapshotRenderMode::RedactedText,
strip_capability_instructions: false,
strip_agents_md_user_context: false,
}
}
}
@@ -31,6 +40,16 @@ impl ContextSnapshotOptions {
self.render_mode = render_mode;
self
}
pub fn strip_capability_instructions(mut self) -> Self {
self.strip_capability_instructions = true;
self
}
pub fn strip_agents_md_user_context(mut self) -> Self {
self.strip_agents_md_user_context = true;
self
}
}
pub fn format_request_input_snapshot(
@@ -68,17 +87,29 @@ pub fn format_response_items_snapshot(items: &[Value], options: &ContextSnapshot
.map(|content| {
content
.iter()
.map(|entry| {
.filter_map(|entry| {
if let Some(text) = entry.get("text").and_then(Value::as_str) {
return format_snapshot_text(text, options);
if options.strip_capability_instructions
&& role == "developer"
&& is_capability_instruction_text(text)
{
return None;
}
if options.strip_agents_md_user_context
&& role == "user"
&& text.starts_with("# AGENTS.md instructions for ")
{
return None;
}
return Some(format_snapshot_text(text, options));
}
let Some(content_type) =
entry.get("type").and_then(Value::as_str)
else {
return "<UNKNOWN_CONTENT_ITEM>".to_string();
return Some("<UNKNOWN_CONTENT_ITEM>".to_string());
};
let Some(content_object) = entry.as_object() else {
return format!("<{content_type}>");
return Some(format!("<{content_type}>"));
};
let mut extra_keys = content_object
.keys()
@@ -86,11 +117,11 @@ pub fn format_response_items_snapshot(items: &[Value], options: &ContextSnapshot
.cloned()
.collect::<Vec<String>>();
extra_keys.sort();
if extra_keys.is_empty() {
Some(if extra_keys.is_empty() {
format!("<{content_type}>")
} else {
format!("<{content_type}:{}>", extra_keys.join(","))
}
})
})
.collect::<Vec<String>>()
})
@@ -241,6 +272,15 @@ fn canonicalize_snapshot_text(text: &str) -> String {
if text.starts_with("<permissions instructions>") {
return "<PERMISSIONS_INSTRUCTIONS>".to_string();
}
if text.starts_with(APPS_INSTRUCTIONS_OPEN_TAG) {
return "<APPS_INSTRUCTIONS>".to_string();
}
if text.starts_with(SKILLS_INSTRUCTIONS_OPEN_TAG) {
return "<SKILLS_INSTRUCTIONS>".to_string();
}
if text.starts_with(PLUGINS_INSTRUCTIONS_OPEN_TAG) {
return "<PLUGINS_INSTRUCTIONS>".to_string();
}
if text.starts_with("# AGENTS.md instructions for ") {
return "<AGENTS_MD>".to_string();
}
@@ -282,7 +322,24 @@ fn canonicalize_snapshot_text(text: &str) -> String {
{
return format!("<COMPACTION_SUMMARY>\n{summary}");
}
text.to_string()
normalize_dynamic_snapshot_paths(text)
}
fn is_capability_instruction_text(text: &str) -> bool {
text.starts_with(APPS_INSTRUCTIONS_OPEN_TAG)
|| text.starts_with(SKILLS_INSTRUCTIONS_OPEN_TAG)
|| text.starts_with(PLUGINS_INSTRUCTIONS_OPEN_TAG)
}
fn normalize_dynamic_snapshot_paths(text: &str) -> String {
static SYSTEM_SKILL_PATH_RE: OnceLock<Regex> = OnceLock::new();
let system_skill_path_re = SYSTEM_SKILL_PATH_RE.get_or_init(|| {
Regex::new(r"/[^)\n]*/skills/\.system/([^/\n]+)/SKILL\.md")
.expect("system skill path regex should compile")
});
system_skill_path_re
.replace_all(text, "<SYSTEM_SKILLS_ROOT>/$1/SKILL.md")
.into_owned()
}
#[cfg(test)]
@@ -353,6 +410,87 @@ mod tests {
assert_eq!(rendered, "00:message/user:<AGENTS_MD>");
}
#[test]
fn redacted_text_mode_keeps_capability_instruction_placeholders() {
let items = vec![json!({
"type": "message",
"role": "developer",
"content": [
{
"type": "input_text",
"text": "<apps_instructions>\n## Apps\nbody\n</apps_instructions>"
},
{
"type": "input_text",
"text": "<skills_instructions>\n## Skills\nbody\n</skills_instructions>"
},
{
"type": "input_text",
"text": "<plugins_instructions>\n## Plugins\nbody\n</plugins_instructions>"
}
]
})];
let rendered = format_response_items_snapshot(
&items,
&ContextSnapshotOptions::default().render_mode(ContextSnapshotRenderMode::RedactedText),
);
assert_eq!(
rendered,
"00:message/developer[3]:\n [01] <APPS_INSTRUCTIONS>\n [02] <SKILLS_INSTRUCTIONS>\n [03] <PLUGINS_INSTRUCTIONS>"
);
}
#[test]
fn strip_capability_instructions_omits_capability_parts_from_developer_messages() {
let items = vec![json!({
"type": "message",
"role": "developer",
"content": [
{ "type": "input_text", "text": "<permissions instructions>\n...</permissions instructions>" },
{ "type": "input_text", "text": "<skills_instructions>\n## Skills\n...</skills_instructions>" },
{ "type": "input_text", "text": "<plugins_instructions>\n## Plugins\n...</plugins_instructions>" }
]
})];
let rendered = format_response_items_snapshot(
&items,
&ContextSnapshotOptions::default()
.render_mode(ContextSnapshotRenderMode::RedactedText)
.strip_capability_instructions(),
);
assert_eq!(rendered, "00:message/developer:<PERMISSIONS_INSTRUCTIONS>");
}
#[test]
fn strip_agents_md_user_context_omits_agents_fragment_from_user_messages() {
let items = vec![json!({
"type": "message",
"role": "user",
"content": [
{
"type": "input_text",
"text": "# AGENTS.md instructions for /tmp/example\n\n<INSTRUCTIONS>\n- test\n</INSTRUCTIONS>"
},
{
"type": "input_text",
"text": "<environment_context>\n <cwd>/tmp/example</cwd>\n</environment_context>"
}
]
})];
let rendered = format_response_items_snapshot(
&items,
&ContextSnapshotOptions::default()
.render_mode(ContextSnapshotRenderMode::RedactedText)
.strip_agents_md_user_context(),
);
assert_eq!(rendered, "00:message/user:<ENVIRONMENT_CONTEXT:cwd=<CWD>>");
}
#[test]
fn redacted_text_mode_normalizes_environment_context_with_subagents() {
let items = vec![json!({
@@ -442,4 +580,23 @@ mod tests {
"00:message/user[3]:\n [01] <image>\n [02] <input_image:image_url>\n [03] </image>"
);
}
#[test]
fn redacted_text_mode_normalizes_system_skill_temp_paths() {
let items = vec![json!({
"type": "message",
"role": "developer",
"content": [{
"type": "input_text",
"text": "## Skills\n- openai-docs: helper (file: /private/var/folders/yk/p4jp9nzs79s5q84csslkgqtm0000gn/T/.tmpAnGVww/skills/.system/openai-docs/SKILL.md)"
}]
})];
let rendered = format_response_items_snapshot(&items, &ContextSnapshotOptions::default());
assert_eq!(
rendered,
"00:message/developer:## Skills\\n- openai-docs: helper (file: <SYSTEM_SKILLS_ROOT>/openai-docs/SKILL.md)"
);
}
}

View File

@@ -25,8 +25,8 @@ pub mod zsh_fork;
#[ctor]
fn enable_deterministic_unified_exec_process_ids_for_tests() {
codex_core::test_support::set_thread_manager_test_mode(true);
codex_core::test_support::set_deterministic_process_ids(true);
codex_core::test_support::set_thread_manager_test_mode(/*enabled*/ true);
codex_core::test_support::set_deterministic_process_ids(/*enabled*/ true);
}
#[ctor]
@@ -79,7 +79,7 @@ pub fn test_path_buf_with_windows(unix_path: &str, windows_path: Option<&str>) -
}
pub fn test_path_buf(unix_path: &str) -> PathBuf {
test_path_buf_with_windows(unix_path, None)
test_path_buf_with_windows(unix_path, /*windows_path*/ None)
}
pub fn test_absolute_path_with_windows(
@@ -91,7 +91,7 @@ pub fn test_absolute_path_with_windows(
}
pub fn test_absolute_path(unix_path: &str) -> AbsolutePathBuf {
test_absolute_path_with_windows(unix_path, None)
test_absolute_path_with_windows(unix_path, /*windows_path*/ None)
}
pub fn test_tmp_path() -> AbsolutePathBuf {
@@ -264,7 +264,7 @@ pub fn sandbox_network_env_var() -> &'static str {
}
pub fn format_with_current_shell(command: &str) -> Vec<String> {
codex_core::shell::default_user_shell().derive_exec_args(command, true)
codex_core::shell::default_user_shell().derive_exec_args(command, /*use_login_shell*/ true)
}
pub fn format_with_current_shell_display(command: &str) -> String {
@@ -273,7 +273,8 @@ pub fn format_with_current_shell_display(command: &str) -> String {
}
pub fn format_with_current_shell_non_login(command: &str) -> Vec<String> {
codex_core::shell::default_user_shell().derive_exec_args(command, false)
codex_core::shell::default_user_shell()
.derive_exec_args(command, /*use_login_shell*/ false)
}
pub fn format_with_current_shell_display_non_login(command: &str) -> String {

View File

@@ -81,7 +81,7 @@ pub async fn start_streaming_sse_server(
tokio::spawn(async move {
let (request, body_prefix) = read_http_request(&mut stream).await;
let Some((method, path)) = parse_request_line(&request) else {
let _ = write_http_response(&mut stream, 400, "bad request", "text/plain").await;
let _ = write_http_response(&mut stream, /*status*/ 400, "bad request", "text/plain").await;
return;
};
@@ -90,7 +90,7 @@ pub async fn start_streaming_sse_server(
.await
.is_err()
{
let _ = write_http_response(&mut stream, 400, "bad request", "text/plain").await;
let _ = write_http_response(&mut stream, /*status*/ 400, "bad request", "text/plain").await;
return;
}
let body = serde_json::json!({
@@ -98,7 +98,7 @@ pub async fn start_streaming_sse_server(
"object": "list"
})
.to_string();
let _ = write_http_response(&mut stream, 200, &body, "application/json").await;
let _ = write_http_response(&mut stream, /*status*/ 200, &body, "application/json").await;
return;
}
@@ -108,13 +108,13 @@ pub async fn start_streaming_sse_server(
{
Ok(body) => body,
Err(_) => {
let _ = write_http_response(&mut stream, 400, "bad request", "text/plain").await;
let _ = write_http_response(&mut stream, /*status*/ 400, "bad request", "text/plain").await;
return;
}
};
requests.lock().await.push(body);
let Some((chunks, completion)) = take_next_stream(&state).await else {
let _ = write_http_response(&mut stream, 500, "no responses queued", "text/plain").await;
let _ = write_http_response(&mut stream, /*status*/ 500, "no responses queued", "text/plain").await;
return;
};
@@ -138,7 +138,7 @@ pub async fn start_streaming_sse_server(
return;
}
let _ = write_http_response(&mut stream, 404, "not found", "text/plain").await;
let _ = write_http_response(&mut stream, /*status*/ 404, "not found", "text/plain").await;
});
}
}

View File

@@ -105,7 +105,7 @@ impl TestCodexBuilder {
Some(home) => home,
None => Arc::new(TempDir::new()?),
};
Box::pin(self.build_with_home(server, home, None)).await
Box::pin(self.build_with_home(server, home, /*resume_from*/ None)).await
}
pub async fn build_with_streaming_server(
@@ -117,7 +117,12 @@ impl TestCodexBuilder {
Some(home) => home,
None => Arc::new(TempDir::new()?),
};
Box::pin(self.build_with_home_and_base_url(format!("{base_url}/v1"), home, None)).await
Box::pin(self.build_with_home_and_base_url(
format!("{base_url}/v1"),
home,
/*resume_from*/ None,
))
.await
}
pub async fn build_with_websocket_server(
@@ -138,7 +143,7 @@ impl TestCodexBuilder {
.enable(Feature::ResponsesWebsockets)
.expect("test config should allow feature update");
}));
Box::pin(self.build_with_home_and_base_url(base_url, home, None)).await
Box::pin(self.build_with_home_and_base_url(base_url, home, /*resume_from*/ None)).await
}
pub async fn resume(
@@ -202,7 +207,7 @@ impl TestCodexBuilder {
config.clone(),
path,
auth_manager,
None,
/*parent_trace*/ None,
))
.await?
}
@@ -226,7 +231,7 @@ impl TestCodexBuilder {
) -> anyhow::Result<(Config, Arc<TempDir>)> {
let model_provider = ModelProviderInfo {
base_url: Some(base_url),
..built_in_model_providers()["openai"].clone()
..built_in_model_providers(/*openai_base_url*/ None)["openai"].clone()
};
let cwd = Arc::new(TempDir::new()?);
let mut config = load_default_config_for_test(home).await;
@@ -362,8 +367,13 @@ impl TestCodex {
approval_policy: AskForApproval,
sandbox_policy: SandboxPolicy,
) -> Result<()> {
self.submit_turn_with_context(prompt, approval_policy, sandbox_policy, None)
.await
self.submit_turn_with_context(
prompt,
approval_policy,
sandbox_policy,
/*service_tier*/ None,
)
.await
}
async fn submit_turn_with_context(

View File

@@ -23,7 +23,8 @@ impl TestCodexExecBuilder {
pub fn cmd_with_server(&self, server: &MockServer) -> assert_cmd::Command {
let mut cmd = self.cmd();
let base = format!("{}/v1", server.uri());
cmd.env("OPENAI_BASE_URL", base);
cmd.arg("-c")
.arg(format!("openai_base_url={}", toml_string_literal(&base)));
cmd
}
@@ -35,6 +36,10 @@ impl TestCodexExecBuilder {
}
}
fn toml_string_literal(value: &str) -> String {
serde_json::to_string(value).expect("serialize TOML string literal")
}
pub fn test_codex_exec() -> TestCodexExecBuilder {
TestCodexExecBuilder {
home: TempDir::new().expect("create temp home"),

View File

@@ -102,7 +102,7 @@ fn find_test_zsh_path() -> Result<Option<PathBuf>> {
return Ok(None);
}
match crate::fetch_dotslash_file(&dotslash_zsh, None) {
match crate::fetch_dotslash_file(&dotslash_zsh, /*dotslash_cache*/ None) {
Ok(path) => Ok(Some(path)),
Err(error) => {
eprintln!("skipping zsh-fork test: failed to fetch zsh via dotslash: {error:#}");