Route prepared images through the attachment store (#46065)

## What changed

Upload prepared images from messages and tool outputs through the session's `AttachmentStore` before recording history. Use the returned inline bytes or file reference, and fall back to the prepared inline image if upload fails.

Keep history replay on `InlineAttachmentStore` so it does not upload recorded images, and preserve existing file references. Reuse image resizing directly for guardian review comparisons without uploading.

## Testing

Add coverage for upload failure preserving resized inline images. Extend rollout coverage to verify that only new images are uploaded, uploaded bytes have the expected dimensions, and file references and resize notices survive replay.

GitOrigin-RevId: e9966297c3579ef1e52376f0235fe3a7e3ec7ecc
This commit is contained in:
Krish Chainani authored and copyberry committed 2026-09-17 00:49:12 +00:00
1 parent 08663cc91b
commit 5e636ea760
10 files changed
+370 -134

No files matched your search

+91 -15
View File
@@ -2,6 +2,12 @@ use std::io::Cursor;
use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64_STANDARD;
use codex_attachment_store::AttachmentStoreError;
use codex_attachment_store::AttachmentStoreErrorKind;
use codex_attachment_store::InlineAttachmentStore;
use codex_attachment_store::ResolveFuture;
use codex_attachment_store::ResolveRequest;
use codex_attachment_store::UploadFuture;
use codex_protocol::models::ContentItemKind;
use codex_protocol::models::FunctionCallOutputBody;
use codex_protocol::models::FunctionCallOutputPayload;
@@ -17,6 +23,29 @@ use pretty_assertions::assert_eq;
use super::*;
struct FailingAttachmentStore;
impl AttachmentStore for FailingAttachmentStore {
fn upload(&self, _request: UploadRequest) -> UploadFuture<'_> {
Box::pin(async {
Err(AttachmentStoreError::new(
AttachmentStoreErrorKind::Backend,
"upload failed",
))
})
}
fn resolve<'a>(&'a self, request: ResolveRequest<'a>) -> ResolveFuture<'a> {
let file_id = request.file_id;
Box::pin(async move {
Err(AttachmentStoreError::new(
AttachmentStoreErrorKind::NotFound,
format!("attachment `{file_id}` was not found"),
))
})
}
}
fn png_data_url(width: u32, height: u32) -> (String, Vec<u8>) {
let image = ImageBuffer::from_pixel(width, height, Rgba([10u8, 20, 30, 255]));
let mut encoded = Cursor::new(Vec::new());
@@ -34,8 +63,8 @@ fn decoded_image(image_url: &str) -> (Vec<u8>, DynamicImage) {
(bytes, image)
}
#[test]
fn preparation_preserves_small_image_bytes_and_replaces_remote_urls() {
#[tokio::test(flavor = "multi_thread")]
async fn preparation_preserves_small_image_bytes_and_replaces_remote_urls() {
let (data_url, original_bytes) = png_data_url(/*width*/ 64, /*height*/ 32);
let mut items = vec![ResponseItem::Message {
id: None,
@@ -74,7 +103,9 @@ fn preparation_preserves_small_image_bytes_and_replaces_remote_urls() {
&mut items,
ImagePreparationMode::DetailBased,
ImageResizeNoticeMode::Disabled,
);
&InlineAttachmentStore,
)
.await;
let ResponseItem::Message { content, .. } = &items[0] else {
panic!("expected message");
@@ -112,8 +143,8 @@ fn preparation_preserves_small_image_bytes_and_replaces_remote_urls() {
);
}
#[test]
fn detail_policies_apply_the_expected_budgets() {
#[tokio::test(flavor = "multi_thread")]
async fn detail_policies_apply_the_expected_budgets() {
for (detail, effective_detail, input_dimensions, expected_dimensions) in [
(
Some(ImageDetail::High),
@@ -157,7 +188,9 @@ fn detail_policies_apply_the_expected_budgets() {
&mut items,
ImagePreparationMode::DetailBased,
ImageResizeNoticeMode::Disabled,
);
&InlineAttachmentStore,
)
.await;
let ResponseItem::Message { content, .. } = &items[0] else {
panic!("expected message");
@@ -187,8 +220,8 @@ fn detail_policies_apply_the_expected_budgets() {
}
}
#[test]
fn preparation_reports_tool_output_item_id() {
#[tokio::test(flavor = "multi_thread")]
async fn preparation_reports_tool_output_item_id() {
let call_id = "call-image";
let (image_url, _) = png_data_url(/*width*/ 64, /*height*/ 32);
let mut items = vec![ResponseItem::FunctionCallOutput {
@@ -208,7 +241,9 @@ fn preparation_reports_tool_output_item_id() {
&mut items,
ImagePreparationMode::DetailBased,
ImageResizeNoticeMode::Disabled,
);
&InlineAttachmentStore,
)
.await;
assert_eq!(
metadata,
@@ -224,9 +259,46 @@ fn preparation_reports_tool_output_item_id() {
);
}
#[tokio::test(flavor = "multi_thread")]
async fn upload_failure_keeps_resized_image_inline() {
let (image_url, _) = png_data_url(/*width*/ 2048, /*height*/ 2048);
let mut items = vec![ResponseItem::Message {
id: None,
role: "user".to_string(),
content: vec![ContentItem::InputImage {
image: ImageReference::Inline { image_url },
detail: Some(ImageDetail::High),
}],
phase: None,
internal_chat_message_metadata_passthrough: None,
}];
prepare_response_items(
&mut items,
ImagePreparationMode::DetailBased,
ImageResizeNoticeMode::Disabled,
&FailingAttachmentStore,
)
.await;
let ResponseItem::Message { content, .. } = &items[0] else {
panic!("expected message");
};
let [
ContentItem::InputImage {
image: ImageReference::Inline { image_url },
..
},
] = content.as_slice()
else {
panic!("expected inline image");
};
assert_eq!(decoded_image(image_url).1.dimensions(), (1600, 1600));
}
/// File-backed images retain their sequence positions even though preparation skips them.
#[test]
fn resize_notices_count_file_backed_images_and_skip_failed_images() {
#[tokio::test(flavor = "multi_thread")]
async fn resize_notices_count_file_backed_images_and_skip_failed_images() {
let (large_image_url, _) = png_data_url(/*width*/ 2048, /*height*/ 2048);
let (small_image_url, _) = png_data_url(/*width*/ 64, /*height*/ 32);
let mut items = vec![
@@ -305,7 +377,9 @@ fn resize_notices_count_file_backed_images_and_skip_failed_images() {
&mut items,
ImagePreparationMode::DetailBased,
ImageResizeNoticeMode::Enabled,
);
&InlineAttachmentStore,
)
.await;
let expected_user_notice = concat!(
"<image_resize_notice>\n",
"Image 4 of 4 in the preceding user message was resized from 2048x2048 to 1600x1600 pixels.\n",
@@ -443,8 +517,8 @@ fn resize_notices_count_file_backed_images_and_skip_failed_images() {
);
}
#[test]
fn preparation_replaces_only_failed_tool_images_and_preserves_metadata() {
#[tokio::test(flavor = "multi_thread")]
async fn preparation_replaces_only_failed_tool_images_and_preserves_metadata() {
let (valid_image_url, _) = png_data_url(/*width*/ 64, /*height*/ 32);
let expected_valid_image_url = valid_image_url.clone();
let mut items = vec![ResponseItem::CustomToolCallOutput {
@@ -490,7 +564,9 @@ fn preparation_replaces_only_failed_tool_images_and_preserves_metadata() {
&mut items,
ImagePreparationMode::DetailBased,
ImageResizeNoticeMode::Disabled,
);
&InlineAttachmentStore,
)
.await;
assert_eq!(
items,