Add local diagnostic ZIP exports to the app server (#55940)

## What changed

Add a save RPC that accepts the existing report options and an absolute `savePath` on the server host. Save the report, selected attachments, and a manifest without uploading, and return `savedPath`, `threadId`, and `promptHash`. Local exports remain available when uploads are disabled by configuration.

Publish archives atomically without replacing existing files. Bound uncompressed contents to 200 MiB and attachments to 128, preserve duplicate attachment names safely, and fail without publishing a partial ZIP when a selected file cannot be read. Honor `includeLogs` and omit automatic rollouts for untouched threads without materializing them.

Update protocol schemas and generated TypeScript and Python types.

## Testing

Add coverage for local-only saves, disabled uploads, path validation, overwrite protection, log consent, archive budgets, duplicate names, missing files, and rollout selection before materialization and after writer shutdown.

GitOrigin-RevId: 222c0fe7ab923904de9c8a9f12ad27f7a0104875
This commit is contained in:
viktor-oai
2026-10-21 16:43:30 +00:00
committed by copyberry
parent 430a16799d
commit a3b685331f
34 changed files with 1481 additions and 120 deletions
+1
View File
@@ -132,6 +132,7 @@ libc = { workspace = true }
codex-windows-sandbox = { workspace = true }
[dev-dependencies]
zip = { workspace = true }
codex-uds = { workspace = true }
app_test_support = { workspace = true }
axum = { workspace = true, default-features = false, features = [
@@ -1924,6 +1924,9 @@ impl MessageProcessor {
ClientRequest::FeedbackUpload { params, .. } => {
self.feedback_processor.feedback_upload(params).await
}
ClientRequest::FeedbackSave { params, .. } => {
self.feedback_processor.feedback_save(params).await
}
};
match result {
@@ -1,6 +1,8 @@
use super::feedback_thread_index::FeedbackThreadIndex;
use super::*;
use crate::error_code::OVERLOADED_ERROR_CODE;
use codex_app_server_protocol::FeedbackSaveParams;
use codex_app_server_protocol::FeedbackSaveResponse;
use codex_connectors::ConnectorDirectoryCacheContext;
use codex_connectors::ConnectorDirectoryCacheKey;
use codex_connectors::connector_runtime_cache_path;
@@ -11,8 +13,11 @@ use codex_feedback::FeedbackSnapshot;
use codex_feedback::WINDOWS_SANDBOX_LOG_ATTACHMENT_FILENAME;
use codex_feedback::guardian_review_failures;
use codex_rollout::RolloutRecorder;
use codex_utils_absolute_path::AbsolutePathBuf;
use codex_utils_path_uri::LegacyAppPathString;
use sha2::Digest;
use sha2::Sha256;
use std::path::Path;
use tokio::sync::Semaphore;
#[derive(Clone)]
@@ -50,20 +55,46 @@ impl FeedbackRequestProcessor {
&self,
params: FeedbackUploadParams,
) -> Result<Option<ClientResponsePayload>, JSONRPCErrorError> {
self.upload_feedback_response(params)
.await
.map(|response| Some(response.into()))
}
async fn upload_feedback_response(
&self,
params: FeedbackUploadParams,
) -> Result<FeedbackUploadResponse, JSONRPCErrorError> {
if !self.config.feedback_enabled {
return Err(invalid_request(
"sending feedback is disabled by configuration",
));
}
self.feedback_response(params, /*save_path*/ None)
.await
.map(|response| Some(response.into()))
}
pub(crate) async fn feedback_save(
&self,
params: FeedbackSaveParams,
) -> Result<Option<ClientResponsePayload>, JSONRPCErrorError> {
if !Path::new(params.save_path.as_str()).is_absolute() {
return Err(invalid_request(
"feedback savePath must be an absolute host path",
));
}
let save_path = AbsolutePathBuf::try_from(params.save_path)
.map_err(|error| invalid_request(format!("invalid feedback savePath: {error}")))?;
let saved_path = LegacyAppPathString::from_abs_path(&save_path);
let response = self
.feedback_response(params.feedback, Some(save_path))
.await?;
Ok(Some(
FeedbackSaveResponse {
saved_path,
thread_id: response.thread_id,
prompt_hash: response.prompt_hash,
}
.into(),
))
}
async fn feedback_response(
&self,
params: FeedbackUploadParams,
save_path: Option<AbsolutePathBuf>,
) -> Result<FeedbackUploadResponse, JSONRPCErrorError> {
let permit = self
.uploads
.clone()
@@ -98,8 +129,8 @@ impl FeedbackRequestProcessor {
let auth = self.auth_manager.auth_cached();
let turn_metadata = if let Some(conversation_id) = conversation_id
&& let Some(rollout_path) = self
.resolve_rollout_path(conversation_id, self.state_db.as_ref())
.await
.resolve_rollout_path(conversation_id, self.state_db.as_ref(), save_path.as_ref())
.await?
{
feedback_turn_metadata_from_rollout(
&rollout_path,
@@ -183,8 +214,12 @@ impl FeedbackRequestProcessor {
.flat_map(|index| &mut index.threads)
{
if let Some(rollout_path) = self
.resolve_rollout_path(thread.thread_id, state_db_ctx.as_ref())
.await
.resolve_rollout_path(
thread.thread_id,
state_db_ctx.as_ref(),
save_path.as_ref(),
)
.await?
&& seen_attachment_paths.insert(rollout_path.clone())
{
thread.rollout_filename = codex_rollout::plain_rollout_path(&rollout_path)
@@ -208,7 +243,10 @@ impl FeedbackRequestProcessor {
});
}
}
if let Some(index) = feedback_index {
if let Some(mut index) = feedback_index {
if save_path.is_some() {
index.rollout_archive_filename = None;
}
match index.attachment() {
Ok(attachment) => extra_attachments.insert(0, attachment),
Err(err) => warn!("failed to serialize feedback thread index: {err}"),
@@ -271,6 +309,11 @@ impl FeedbackRequestProcessor {
let runtime_handle = tokio::runtime::Handle::current();
let codex_home = self.config.codex_home.clone();
let operation = if save_path.is_some() {
"save"
} else {
"upload"
};
let upload_result = tokio::task::spawn_blocking(move || {
// Cancelling the RPC waiter must not release a still-running upload's slot.
let _permit = permit;
@@ -279,19 +322,22 @@ impl FeedbackRequestProcessor {
.extend(codex_feedback::daemon_log_attachments(codex_home.as_path()));
}
let tags = (!upload_tags.is_empty()).then_some(&upload_tags);
runtime_handle.block_on(snapshot.upload_feedback(
FeedbackUploadOptions {
classification: &classification,
reason: reason.as_deref(),
tags,
include_logs,
extra_attachments,
extra_attachment_paths: &attachment_paths,
session_source: Some(session_source),
logs_override: sqlite_feedback_logs,
},
&http_client_factory,
))
let options = FeedbackUploadOptions {
classification: &classification,
reason: reason.as_deref(),
tags,
include_logs,
extra_attachments,
extra_attachment_paths: &attachment_paths,
session_source: Some(session_source),
logs_override: sqlite_feedback_logs,
};
match save_path {
Some(path) => snapshot.save_feedback(options, path.as_path()),
None => {
runtime_handle.block_on(snapshot.upload_feedback(options, &http_client_factory))
}
}
})
.await;
@@ -299,13 +345,13 @@ impl FeedbackRequestProcessor {
Ok(result) => result,
Err(join_err) => {
return Err(internal_error(format!(
"failed to upload feedback: {join_err}"
"failed to {operation} feedback: {join_err}"
)));
}
};
upload_result
.map_err(|err| internal_error(format!("failed to upload feedback: {err:#}")))?;
.map_err(|err| internal_error(format!("failed to {operation} feedback: {err:#}")))?;
Ok(FeedbackUploadResponse {
thread_id,
prompt_hash,
@@ -316,21 +362,31 @@ impl FeedbackRequestProcessor {
&self,
conversation_id: ThreadId,
state_db_ctx: Option<&StateDbHandle>,
) -> Option<PathBuf> {
if let Ok(conversation) = self.thread_manager.get_thread(conversation_id).await
&& let Some(rollout_path) = conversation.rollout_path()
{
return Some(rollout_path);
save_path: Option<&AbsolutePathBuf>,
) -> Result<Option<PathBuf>, JSONRPCErrorError> {
if let Ok(conversation) = self.thread_manager.get_thread(conversation_id).await {
if save_path.is_some() {
// Explicit extraLogFiles are collected separately and remain strict, even
// when they name this same not-yet-materialized automatic rollout.
return conversation.feedback_rollout_path().await.map_err(|error| {
internal_error(format!("failed to select feedback rollout: {error}"))
});
}
if let Some(rollout_path) = conversation.rollout_path() {
return Ok(Some(rollout_path));
}
}
let state_db_ctx = state_db_ctx?;
state_db_ctx
let Some(state_db_ctx) = state_db_ctx else {
return Ok(None);
};
Ok(state_db_ctx
.find_rollout_path_by_id(conversation_id, /*archived_only*/ None)
.await
.unwrap_or_else(|err| {
warn!("failed to resolve rollout path for thread_id={conversation_id}: {err}");
None
})
}))
}
}
@@ -14,7 +14,7 @@ const MAX_LISTED_OMISSIONS: usize = 64;
#[derive(Serialize)]
pub(super) struct FeedbackThreadIndex {
rollout_archive_filename: &'static str,
pub(super) rollout_archive_filename: Option<&'static str>,
pub threads: Vec<FeedbackThread>,
retained_failure_thread_ids: Vec<ThreadId>,
omitted_thread_ids: Vec<ThreadId>,
@@ -58,7 +58,7 @@ impl FeedbackThreadIndex {
.take(MAX_LISTED_OMISSIONS)
.collect::<Vec<_>>();
Self {
rollout_archive_filename: "rollouts.tar.gz",
rollout_archive_filename: Some("rollouts.tar.gz"),
threads: thread_ids
.into_iter()
.map(|thread_id| FeedbackThread {
@@ -352,3 +352,262 @@ async fn feedback_upload_recovers_persisted_evidence_and_reports_sqlite_failures
pool.close().await;
Ok(())
}
/// Disabling uploads still permits local ZIPs, with invalid paths and collisions leaving existing files intact.
#[tokio::test]
async fn feedback_save_is_local_and_never_overwrites() -> Result<()> {
let proxy = TcpListener::bind("127.0.0.1:0").await?;
let proxy_uri = format!("http://{}", proxy.local_addr()?);
let mut app_server = TestAppServer::builder()
.with_args(&["-c", "feedback.enabled=false"])
.with_env_overrides(&[
("HTTPS_PROXY", Some(proxy_uri.as_str())),
("https_proxy", Some(proxy_uri.as_str())),
("NO_PROXY", Some("")),
("no_proxy", Some("")),
])
.build_initialized()
.await?;
let request = app_server
.send_raw_request("feedback/upload", Some(json!({ "classification": "bug" })))
.await?;
let error = app_server
.read_stream_until_error_message(RequestId::Integer(request))
.await?;
assert_eq!(
error.error.message,
"sending feedback is disabled by configuration"
);
let directory = tempfile::tempdir()?;
let saved = directory.path().join("local.zip");
std::fs::write(&saved, b"existing export")?;
let foreign = if cfg!(windows) {
"/tmp/foreign.zip"
} else {
r"C:\Users\test\foreign.zip"
};
for (path, message) in [
(json!("relative.zip"), "absolute host path"),
(json!("~/feedback.zip"), "absolute host path"),
(json!(foreign), "absolute host path"),
(json!(&saved), "already exists"),
(
json!(directory.path().join("missing/feedback.zip")),
"failed to save feedback",
),
] {
let request = app_server
.send_raw_request(
"feedback/save",
Some(json!({
"classification": "bug", "savePath": path,
})),
)
.await?;
let error = app_server
.read_stream_until_error_message(RequestId::Integer(request))
.await?;
assert!(error.error.message.contains(message), "{error:?}");
}
assert_eq!(std::fs::read(&saved)?, b"existing export");
assert_eq!(std::fs::read_dir(directory.path())?.count(), 1);
std::fs::remove_file(&saved)?;
let request = app_server
.send_raw_request(
"feedback/save",
Some(json!({
"classification": "bug", "savePath": saved,
})),
)
.await?;
let response = app_server
.read_stream_until_response_message(RequestId::Integer(request))
.await?;
assert_eq!(response.result["savedPath"], json!(saved));
let mut archive = zip::ZipArchive::new(std::fs::File::open(&saved)?)?;
let report: serde_json::Value = serde_json::from_reader(archive.by_name("report.json")?)?;
assert_eq!(report["tags"]["classification"], "bug");
assert!(
timeout(Duration::from_millis(100), proxy.accept())
.await
.is_err()
);
Ok(())
}
#[tokio::test]
async fn feedback_save_preserves_selected_rollout_or_fails_without_upload() -> Result<()> {
use std::io::Read;
let proxy = TcpListener::bind("127.0.0.1:0").await?;
let proxy_uri = format!("http://{}", proxy.local_addr()?);
let mut app_server = TestAppServer::builder()
.with_env_overrides(&[
("HTTPS_PROXY", Some(proxy_uri.as_str())),
("https_proxy", Some(proxy_uri.as_str())),
("NO_PROXY", Some("")),
("no_proxy", Some("")),
])
.build_initialized()
.await?;
let directory = tempfile::tempdir()?;
let filename = format!(
"rollout-2026-10-19T12-00-00-{}.jsonl",
codex_protocol::ThreadId::new()
);
let rollout = directory.path().join(&filename);
let bytes = b"{\"synthetic\":true}\n";
std::fs::write(&rollout, bytes)?;
let saved = directory.path().join("selected.zip");
let request = app_server.send_raw_request("feedback/save", Some(json!({
"classification": "bug", "includeLogs": true, "extraLogFiles": [rollout], "savePath": saved,
}))).await?;
let response = app_server
.read_stream_until_response_message(RequestId::Integer(request))
.await?;
assert_eq!(response.result["savedPath"], json!(saved));
let mut zip = zip::ZipArchive::new(std::fs::File::open(&saved)?)?;
let entry = zip
.file_names()
.collect::<zip::result::ZipResult<Vec<_>>>()?
.into_iter()
.find(|name| name.ends_with(&format!("/{filename}")))
.expect("selected rollout")
.to_string();
let mut actual = Vec::new();
zip.by_name(&entry)?.read_to_end(&mut actual)?;
assert_eq!(actual, bytes);
drop(zip);
std::fs::remove_file(&rollout)?;
let missing = directory.path().join("missing.zip");
let request = app_server.send_raw_request("feedback/save", Some(json!({
"classification": "bug", "includeLogs": true, "extraLogFiles": [rollout], "savePath": missing,
}))).await?;
let error = app_server
.read_stream_until_error_message(RequestId::Integer(request))
.await?;
assert_eq!(error.error.code, -32603);
assert!(error.error.message.starts_with("failed to save feedback:"));
assert!(error.error.message.contains("selected attachment"));
assert!(!missing.exists());
assert_eq!(std::fs::read_dir(directory.path())?.count(), 1);
assert!(
timeout(Duration::from_millis(100), proxy.accept())
.await
.is_err()
);
Ok(())
}
/// Automatic untouched rollouts are omitted; explicit missing and deleted persisted files fail.
#[tokio::test]
async fn feedback_save_handles_rollout_materialization() -> Result<()> {
use codex_app_server_protocol::ThreadStartParams;
use std::io::Read;
let server = app_test_support::create_mock_responses_server_repeating_assistant("Done").await;
let home = tempfile::tempdir()?;
app_test_support::MockResponsesConfig::new(&server.uri()).write(home.path())?;
let proxy = TcpListener::bind("127.0.0.1:0").await?;
let proxy_uri = format!("http://{}", proxy.local_addr()?);
let mut app = TestAppServer::builder()
.with_codex_home(home.path())
.with_args(&["-c", "feedback.enabled=false"])
.with_env_overrides(&[
("HTTPS_PROXY", Some(proxy_uri.as_str())),
("https_proxy", Some(proxy_uri.as_str())),
("NO_PROXY", Some("127.0.0.1,localhost")),
("no_proxy", Some("127.0.0.1,localhost")),
])
.build_initialized()
.await?;
let request = app
.send_thread_start_request_with_auto_env(ThreadStartParams::default())
.await?;
let response = app
.read_stream_until_response_message(RequestId::Integer(request))
.await?;
let thread = response.result["thread"]["id"].as_str().unwrap();
let rollout = std::path::PathBuf::from(response.result["thread"]["path"].as_str().unwrap());
assert!(!rollout.exists());
let output = tempfile::tempdir()?;
for explicit in [false, true] {
let destination = output.path().join(format!("fresh-{explicit}.zip"));
let request = app.send_raw_request("feedback/save", Some(json!({"classification":"bug", "includeLogs":true, "threadId":thread, "extraLogFiles": if explicit { vec![&rollout] } else { vec![] }, "savePath":destination}))).await?;
if explicit {
let error = app
.read_stream_until_error_message(RequestId::Integer(request))
.await?;
assert!(error.error.message.contains("selected attachment"));
assert!(!destination.exists());
} else {
app.read_stream_until_response_message(RequestId::Integer(request))
.await?;
let mut zip = zip::ZipArchive::new(std::fs::File::open(destination)?)?;
let names = zip
.file_names()
.collect::<zip::result::ZipResult<Vec<_>>>()?
.into_iter()
.map(std::borrow::Cow::into_owned)
.collect::<Vec<_>>();
assert!(
!names
.iter()
.any(|name| name.ends_with(".jsonl") || name.ends_with("rollouts.tar.gz"))
);
let index_name = names
.iter()
.find(|name| name.ends_with("feedback-thread-index.json"))
.expect("thread index");
let index: serde_json::Value = serde_json::from_reader(zip.by_name(index_name)?)?;
assert_eq!(
index["threads"][0]["rollout_filename"],
serde_json::Value::Null
);
assert_eq!(index["rollout_archive_filename"], serde_json::Value::Null);
}
}
assert!(!rollout.exists(), "saving must not materialize the rollout");
let request = app
.send_raw_request(
"turn/start",
Some(json!({"threadId":thread,"input":[{"type":"text","text":"hello"}]})),
)
.await?;
app.read_stream_until_response_message(RequestId::Integer(request))
.await?;
app.read_stream_until_notification_message("turn/completed")
.await?;
assert!(rollout.exists());
let expected = std::fs::read(&rollout)?;
let saved = output.path().join("materialized.zip");
let request = app.send_raw_request("feedback/save", Some(json!({"classification":"bug", "includeLogs":true, "threadId":thread, "savePath":saved}))).await?;
app.read_stream_until_response_message(RequestId::Integer(request))
.await?;
let mut zip = zip::ZipArchive::new(std::fs::File::open(saved)?)?;
let suffix = format!("/{}", rollout.file_name().unwrap().to_string_lossy());
let entry = zip
.file_names()
.collect::<zip::result::ZipResult<Vec<_>>>()?
.into_iter()
.find(|name| name.ends_with(&suffix))
.unwrap()
.into_owned();
let mut actual = Vec::new();
zip.by_name(&entry)?.read_to_end(&mut actual)?;
assert_eq!(actual, expected);
std::fs::remove_file(&rollout)?;
let failed = output.path().join("deleted.zip");
let request = app.send_raw_request("feedback/save", Some(json!({"classification":"bug", "includeLogs":true, "threadId":thread, "savePath":failed}))).await?;
let error = app
.read_stream_until_error_message(RequestId::Integer(request))
.await?;
assert!(error.error.message.contains("selected attachment"));
assert!(!failed.exists());
assert!(
timeout(Duration::from_millis(100), proxy.accept())
.await
.is_err()
);
Ok(())
}