Support image references by file ID in inputs and tool outputs (#45794)

## What changed

- Accept `fileId` alongside the existing `url` form for app-server image inputs, and forward file references to the Responses API as `file_id`. Update generated schemas and client types.
- Preserve file references, image detail hints, and mixed inline/file image ordering through user-message events, thread history, and rollout migration. Retain file images when truncating tool output.
- Pass file references through image preparation without resolving them, while keeping resize-notice numbering correct. Omit them from unsupported TUI display and Guardian image context.
- Reject image-edit requests whose recent-image window includes a file reference, preventing selection of an older inline image instead.

## Testing

Add coverage for serialization, request and rollout preservation, mixed-image history ordering, incomplete ordering metadata, tool-output truncation, and rejection of unsupported image-edit selections.

GitOrigin-RevId: 6ca20a8577155cc934b720803c3b7b3bffdf972a
This commit is contained in:
Krish Chainani authored and copyberry committed 2026-09-15 21:19:08 +00:00
1 parent c51cb968e4
commit 7b8b17b97a
95 files changed
+2434 -324

No files matched your search

@@ -25,6 +25,7 @@ use axum::routing::post;
use codex_app_server_protocol::ApprovalsReviewer;
use codex_app_server_protocol::AskForApproval;
use codex_app_server_protocol::ClientRequest;
use codex_app_server_protocol::ImageReference;
use codex_app_server_protocol::ItemCompletedNotification;
use codex_app_server_protocol::ItemGuardianApprovalReviewStartedNotification;
use codex_app_server_protocol::McpServerElicitationRequest;
@@ -1031,7 +1032,9 @@ async fn guardian_v2_routes_scoped_tool_approvals(
}
if mixed_evidence {
turn_input.push(UserInput::Image {
url: EVIDENCE_IMAGE.to_owned(),
image: ImageReference::Inline {
url: EVIDENCE_IMAGE.to_owned(),
},
detail: None,
});
}
@@ -9,6 +9,7 @@ use app_test_support::TestAppServer;
use app_test_support::write_chatgpt_auth;
use codex_app_server_protocol::ImageGenerationFailure;
use codex_app_server_protocol::ImageGenerationItem;
use codex_app_server_protocol::ImageReference as V2ImageReference;
use codex_app_server_protocol::ItemCompletedNotification;
use codex_app_server_protocol::ThreadItem;
use codex_app_server_protocol::ThreadReadParams;
@@ -657,7 +658,9 @@ async fn transparent_image_edit_preserves_metadata_and_recent_pathless_image() -
text_elements: Vec::new(),
},
V2UserInput::Image {
url: image_url.to_string(),
image: V2ImageReference::Inline {
url: image_url.to_string(),
},
detail: None,
},
],
@@ -12,6 +12,7 @@ use codex_app_server::in_process::InProcessStartArgs;
use codex_app_server_protocol::ClientInfo;
use codex_app_server_protocol::ClientRequest;
use codex_app_server_protocol::DeprecationNoticeNotification;
use codex_app_server_protocol::ImageReference;
use codex_app_server_protocol::InitializeCapabilities;
use codex_app_server_protocol::InitializeParams;
use codex_app_server_protocol::JSONRPCError;
@@ -234,6 +235,80 @@ async fn thread_read_can_include_turns() -> Result<()> {
Ok(())
}
/// Preserves a file-backed image across a completed turn, including a cold history read.
#[tokio::test]
async fn thread_read_preserves_file_id_from_completed_turn() -> Result<()> {
let server = create_mock_responses_server_repeating_assistant("Done").await;
let codex_home = TempDir::new()?;
MockResponsesConfig::new(&server.uri()).write(codex_home.path())?;
let mut mcp = TestAppServer::builder()
.with_codex_home(codex_home.path())
.build_initialized()
.await?;
let start_id = mcp
.send_thread_start_request_with_auto_env(ThreadStartParams {
model: Some("mock-model".to_string()),
..Default::default()
})
.await?;
let ThreadStartResponse { thread, .. } =
timeout(DEFAULT_READ_TIMEOUT, mcp.read_response(start_id)).await??;
let image = UserInput::Image {
image: ImageReference::File {
file_id: "file_123".to_string(),
},
detail: None,
};
timeout(
DEFAULT_READ_TIMEOUT,
mcp.start_turn_and_wait_for_completion(TurnStartParams {
thread_id: thread.id.clone(),
input: vec![image.clone()],
..Default::default()
}),
)
.await??;
let read_id = mcp
.send_thread_read_request(ThreadReadParams {
thread_id: thread.id.clone(),
include_turns: true,
})
.await?;
let ThreadReadResponse {
thread: loaded_thread,
} = timeout(DEFAULT_READ_TIMEOUT, mcp.read_response(read_id)).await??;
let ThreadItem::UserMessage { content, .. } = &loaded_thread.turns[0].items[0] else {
panic!("expected completed turn to start with a user message");
};
assert_eq!(content, &vec![image.clone()]);
timeout(DEFAULT_READ_TIMEOUT, mcp.shutdown_gracefully()).await??;
drop(mcp);
let mut mcp = TestAppServer::builder()
.with_codex_home(codex_home.path())
.build_initialized()
.await?;
let read_id = mcp
.send_thread_read_request(ThreadReadParams {
thread_id: thread.id,
include_turns: true,
})
.await?;
let ThreadReadResponse {
thread: restarted_thread,
} = timeout(DEFAULT_READ_TIMEOUT, mcp.read_response(read_id)).await??;
let ThreadItem::UserMessage { content, .. } = &restarted_thread.turns[0].items[0] else {
panic!("expected persisted turn to start with a user message");
};
assert_eq!(content, &vec![image]);
Ok(())
}
#[tokio::test]
async fn paginated_stored_thread_routes_projected_turns() -> Result<()> {
let server = create_mock_responses_server_repeating_assistant("Done").await;
@@ -34,6 +34,7 @@ use codex_app_server_protocol::CyberAccessProgram;
use codex_app_server_protocol::FileChangeApprovalDecision;
use codex_app_server_protocol::FileChangePatchUpdatedNotification;
use codex_app_server_protocol::FileChangeRequestApprovalResponse;
use codex_app_server_protocol::ImageReference as V2ImageReference;
use codex_app_server_protocol::ItemCompletedNotification;
use codex_app_server_protocol::ItemStartedNotification;
use codex_app_server_protocol::JSONRPCError;
@@ -267,7 +268,15 @@ async fn turn_start_omits_notification_media_without_changing_model_input() -> R
text_elements: Vec::new(),
},
V2UserInput::Image {
url: TINY_PNG_DATA_URL.to_string(),
image: V2ImageReference::Inline {
url: TINY_PNG_DATA_URL.to_string(),
},
detail: None,
},
V2UserInput::Image {
image: V2ImageReference::File {
file_id: "file_123".to_string(),
},
detail: None,
},
],
@@ -315,6 +324,8 @@ async fn turn_start_omits_notification_media_without_changing_model_input() -> R
content[0]["type"].as_str(),
Some("text" | "input_text")
));
assert!(content[0].get("fileId").is_none());
assert!(content[0].get("file_id").is_none());
user_message_notifications.push(notification.method);
}
}
@@ -332,8 +343,17 @@ async fn turn_start_omits_notification_media_without_changing_model_input() -> R
);
let model_input_images = received_response_input_images(&server).await?;
assert_eq!(model_input_images.len(), 1);
assert_eq!(model_input_images[0]["image_url"], TINY_PNG_DATA_URL);
assert_eq!(model_input_images.len(), 2);
assert!(
model_input_images
.iter()
.any(|image| image["image_url"] == TINY_PNG_DATA_URL)
);
assert!(
model_input_images
.iter()
.any(|image| image["file_id"] == "file_123")
);
Ok(())
}
@@ -1393,7 +1413,9 @@ async fn turn_start_tracks_thread_originator_in_analytics() -> Result<()> {
thread_id: thread.id.clone(),
client_user_message_id: None,
input: vec![V2UserInput::Image {
url: TINY_PNG_DATA_URL.to_string(),
image: V2ImageReference::Inline {
url: TINY_PNG_DATA_URL.to_string(),
},
detail: None,
}],
turn_trigger: Some("user".to_string()),