mirror of
https://github.com/openai/codex.git
synced 2026-12-05 01:00:54 +00:00
Fix session lookup and resume after rollout compression (#54008)
## Why Compression replaces `.jsonl` rollout files with `.jsonl.zst` files, so checking only the original path can skip existing sessions. Compression can also replace a file between path lookup and canonicalization. ## What changed - Resolve compressed rollout alternatives when selecting the latest local session. - Retry rollout path resolution when files disappear during lookup or canonicalization. ## Testing Extend coverage to verify latest-session filtering after startup compression and resuming a compressed rollout through its original relative `.jsonl` path with preserved turns. GitOrigin-RevId: a4d5c399567ab4c7cd8aaa8b5f38821ee8b8d802
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
//! Exercises the compression trigger through the public app-server API.
|
||||
//! Exercises compression and subsequent resume through the public app-server API.
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::Result;
|
||||
use app_test_support::DEFAULT_CLIENT_NAME;
|
||||
use app_test_support::MockResponsesConfig;
|
||||
use app_test_support::TestAppServer;
|
||||
use app_test_support::create_fake_rollout;
|
||||
use app_test_support::create_mock_responses_server_repeating_assistant;
|
||||
use app_test_support::rollout_path;
|
||||
use codex_app_server_protocol::ClientInfo;
|
||||
use codex_app_server_protocol::ClientRequest;
|
||||
@@ -14,6 +16,10 @@ use codex_app_server_protocol::JSONRPCErrorError;
|
||||
use codex_app_server_protocol::JSONRPCMessage;
|
||||
use codex_app_server_protocol::RequestId;
|
||||
use codex_app_server_protocol::RolloutCompressResponse;
|
||||
use codex_app_server_protocol::ThreadReadParams;
|
||||
use codex_app_server_protocol::ThreadReadResponse;
|
||||
use codex_app_server_protocol::ThreadResumeParams;
|
||||
use codex_app_server_protocol::ThreadResumeResponse;
|
||||
use pretty_assertions::assert_eq;
|
||||
use tempfile::TempDir;
|
||||
use tokio::time::timeout;
|
||||
@@ -22,7 +28,9 @@ const READ_TIMEOUT: Duration = Duration::from_secs(/*secs*/ 20);
|
||||
|
||||
#[tokio::test]
|
||||
async fn rollout_compress_runs_after_startup_with_compression_disabled() -> 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 app_server = TestAppServer::builder()
|
||||
.with_codex_home(codex_home.path())
|
||||
.with_args(&["-c", "features.local_thread_store_compression=false"])
|
||||
@@ -35,12 +43,21 @@ async fn rollout_compress_runs_after_startup_with_compression_disabled() -> Resu
|
||||
filename_ts,
|
||||
"2025-01-05T12:00:00Z",
|
||||
"Saved user message",
|
||||
/*model_provider*/ None,
|
||||
Some("mock_provider"),
|
||||
/*git_info*/ None,
|
||||
)?;
|
||||
let path = rollout_path(codex_home.path(), filename_ts, &thread_id);
|
||||
let original = std::fs::read_to_string(&path)?;
|
||||
let compressed_path = path.with_extension("jsonl.zst");
|
||||
let before: ThreadReadResponse = app_server
|
||||
.request(|request_id| ClientRequest::ThreadRead {
|
||||
request_id,
|
||||
params: ThreadReadParams {
|
||||
thread_id: thread_id.clone(),
|
||||
include_turns: true,
|
||||
},
|
||||
})
|
||||
.await?;
|
||||
|
||||
let response: RolloutCompressResponse = app_server
|
||||
.request(|request_id| ClientRequest::RolloutCompress {
|
||||
@@ -61,7 +78,21 @@ async fn rollout_compress_runs_after_startup_with_compression_disabled() -> Resu
|
||||
while let Some(line) = reader.next_line().await? {
|
||||
lines.push(line);
|
||||
}
|
||||
drop(reader);
|
||||
assert_eq!(lines.join("\n") + "\n", original);
|
||||
let relative_path = path.strip_prefix(codex_home.path())?.to_path_buf();
|
||||
let resumed: ThreadResumeResponse = app_server
|
||||
.request(|request_id| ClientRequest::ThreadResume {
|
||||
request_id,
|
||||
params: ThreadResumeParams {
|
||||
thread_id: thread_id.clone(),
|
||||
path: Some(relative_path),
|
||||
..Default::default()
|
||||
},
|
||||
})
|
||||
.await?;
|
||||
assert_eq!(resumed.thread.id, thread_id);
|
||||
assert_eq!(resumed.thread.turns, before.thread.turns);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -227,17 +227,39 @@ pub(super) async fn resolve_requested_rollout_path(
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
let Some(path) = codex_rollout::existing_rollout_path(path.as_path()).await else {
|
||||
return Err(ThreadStoreError::InvalidRequest {
|
||||
message: format!(
|
||||
"failed to resolve rollout path `{}`: file does not exist",
|
||||
path.display()
|
||||
),
|
||||
});
|
||||
};
|
||||
std::fs::canonicalize(path.as_path()).map_err(|err| ThreadStoreError::InvalidRequest {
|
||||
message: format!("failed to resolve rollout path `{}`: {err}", path.display()),
|
||||
})
|
||||
// Compression can replace the physical file after lookup, before canonicalization.
|
||||
// Resolve again from the requested path so either representation remains usable.
|
||||
let mut retries_remaining = 3;
|
||||
loop {
|
||||
let resolved_path = match codex_rollout::existing_rollout_path(path.as_path()).await {
|
||||
Some(path) => path,
|
||||
None if retries_remaining > 0 => {
|
||||
retries_remaining -= 1;
|
||||
continue;
|
||||
}
|
||||
None => {
|
||||
return Err(ThreadStoreError::InvalidRequest {
|
||||
message: format!(
|
||||
"failed to resolve rollout path `{}`: file does not exist",
|
||||
path.display()
|
||||
),
|
||||
});
|
||||
}
|
||||
};
|
||||
match std::fs::canonicalize(&resolved_path) {
|
||||
Err(err) if err.kind() == std::io::ErrorKind::NotFound && retries_remaining > 0 => {
|
||||
retries_remaining -= 1;
|
||||
}
|
||||
result => {
|
||||
return result.map_err(|err| ThreadStoreError::InvalidRequest {
|
||||
message: format!(
|
||||
"failed to resolve rollout path `{}`: {err}",
|
||||
resolved_path.display()
|
||||
),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn attach_history_if_requested(
|
||||
|
||||
+29
-4
@@ -864,10 +864,14 @@ async fn lookup_latest_session_target_with_app_server(
|
||||
.data
|
||||
.into_iter()
|
||||
.find_map(session_target_from_app_server_thread);
|
||||
if target.as_ref().is_some_and(|target| {
|
||||
uses_remote_workspace || target.path.as_deref().is_some_and(std::path::Path::exists)
|
||||
}) {
|
||||
return Ok(target);
|
||||
if let Some(target) = target
|
||||
&& (uses_remote_workspace
|
||||
|| match target.path.as_deref() {
|
||||
Some(path) => codex_rollout::existing_rollout_path(path).await.is_some(),
|
||||
None => false,
|
||||
})
|
||||
{
|
||||
return Ok(Some(target));
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
@@ -3511,6 +3515,9 @@ requires_openai_auth = {requires_openai_auth}
|
||||
config
|
||||
.features
|
||||
.set_enabled(codex_features::Feature::Worktrees, /*enabled*/ false)?;
|
||||
config
|
||||
.features
|
||||
.enable(codex_features::Feature::LocalThreadStoreCompression)?;
|
||||
let model_provider = config.model_provider_id.as_str();
|
||||
let project_thread_id = write_session_rollout(
|
||||
temp_dir.path(),
|
||||
@@ -3536,6 +3543,14 @@ requires_openai_auth = {requires_openai_auth}
|
||||
model_provider,
|
||||
&other_cwd,
|
||||
)?;
|
||||
let rollout_paths = [
|
||||
("2025-01-02T10-00-00", project_thread_id),
|
||||
("2025-01-02T11-00-00", linked_thread_id),
|
||||
("2025-01-02T12-00-00", other_thread_id),
|
||||
]
|
||||
.map(|(timestamp, thread_id)| {
|
||||
app_test_support::rollout_path(temp_dir.path(), timestamp, &thread_id.to_string())
|
||||
});
|
||||
|
||||
let mut app_server = AppServerSession::new(
|
||||
codex_app_server_client::AppServerClient::InProcess(
|
||||
@@ -3543,6 +3558,16 @@ requires_openai_auth = {requires_openai_auth}
|
||||
),
|
||||
ThreadParamsMode::Embedded,
|
||||
);
|
||||
// Exercise the logical .jsonl paths returned by thread/list after startup compression.
|
||||
tokio::time::timeout(std::time::Duration::from_secs(/*secs*/ 30), async {
|
||||
while rollout_paths
|
||||
.iter()
|
||||
.any(|path| path.exists() || !path.with_extension("jsonl.zst").exists())
|
||||
{
|
||||
tokio::time::sleep(std::time::Duration::from_millis(/*millis*/ 10)).await;
|
||||
}
|
||||
})
|
||||
.await?;
|
||||
let filter_cwd = latest_session_cwd_filter(
|
||||
/*uses_remote_workspace*/ false, /*remote_cwd_override*/ None, &config,
|
||||
/*show_all*/ false,
|
||||
|
||||
Reference in New Issue
Block a user