mirror of
https://github.com/openai/codex.git
synced 2026-11-16 22:00:36 +00:00
Avoid redundant store reads for loaded thread metadata (#61837)
## Why Metadata-only `thread/read` requests for loaded threads reread the store to attach a title after already loading persisted metadata. ## What changed Reuse persisted metadata and overlay live settings, returning metadata-only responses without another store read. Keep history loading conditional on `includeTurns` and centralize stored-title trimming and legacy preview fallback handling. ## Testing Extend the in-memory store test to verify that a loaded thread without a rollout path returns its trimmed stored title with exactly one metadata read, while requests with turns still return stored history. GitOrigin-RevId: 74f20914afb44901d7586eb8e944afdae2640c31
This commit is contained in:
@@ -3147,56 +3147,15 @@ impl ThreadRequestProcessor {
|
||||
))
|
||||
})?;
|
||||
let loaded_thread = self.thread_manager.get_thread(thread_id).await.ok();
|
||||
let mut thread = if include_turns {
|
||||
if let Some(loaded_thread) = loaded_thread.as_ref() {
|
||||
// Loaded thread with turns: use persisted metadata when it exists,
|
||||
// but reconstruct turns from the live ThreadStore history.
|
||||
let persisted_thread = self
|
||||
.load_persisted_thread_for_read(thread_id, /*include_turns*/ false)
|
||||
.await?;
|
||||
self.load_live_thread_view(
|
||||
thread_id,
|
||||
include_turns,
|
||||
loaded_thread,
|
||||
persisted_thread,
|
||||
)
|
||||
let mut thread = if let Some(loaded_thread) = loaded_thread.as_ref() {
|
||||
self.load_live_thread_view(thread_id, include_turns, loaded_thread)
|
||||
.await?
|
||||
} else if let Some(thread) = self
|
||||
.load_persisted_thread_for_read(thread_id, include_turns)
|
||||
.await?
|
||||
{
|
||||
// Unloaded thread with turns: load metadata and history together
|
||||
// from the ThreadStore.
|
||||
thread
|
||||
} else {
|
||||
return Err(ThreadReadViewError::InvalidRequest(format!(
|
||||
"thread not loaded: {thread_id}"
|
||||
)));
|
||||
}
|
||||
} else if let Some(thread) = self
|
||||
.load_persisted_thread_for_read(thread_id, include_turns)
|
||||
.await?
|
||||
{
|
||||
if let Some(loaded_thread) = loaded_thread.as_ref() {
|
||||
self.load_live_thread_view(thread_id, include_turns, loaded_thread, Some(thread))
|
||||
.await?
|
||||
} else {
|
||||
thread
|
||||
}
|
||||
} else if let Some(loaded_thread) = loaded_thread.as_ref() {
|
||||
// Loaded metadata-only read before persistence is materialized: build
|
||||
// the response from the live thread snapshot.
|
||||
self.load_live_thread_view(
|
||||
thread_id,
|
||||
include_turns,
|
||||
loaded_thread,
|
||||
/*persisted_thread*/ None,
|
||||
)
|
||||
.await?
|
||||
} else {
|
||||
return Err(ThreadReadViewError::InvalidRequest(format!(
|
||||
"thread not loaded: {thread_id}"
|
||||
)));
|
||||
self.load_persisted_thread_for_read(thread_id, include_turns)
|
||||
.await?
|
||||
.ok_or_else(|| {
|
||||
ThreadReadViewError::InvalidRequest(format!("thread not loaded: {thread_id}"))
|
||||
})?
|
||||
};
|
||||
|
||||
thread.daybreak_enabled = pending_preference.resolve(thread.daybreak_enabled);
|
||||
@@ -3285,71 +3244,70 @@ impl ThreadRequestProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds a `thread/read` view from a loaded thread plus optional persisted metadata.
|
||||
/// Overlays live settings on persisted metadata, loading history only when turns are requested.
|
||||
async fn load_live_thread_view(
|
||||
&self,
|
||||
thread_id: ThreadId,
|
||||
include_turns: bool,
|
||||
loaded_thread: &CodexThread,
|
||||
persisted_thread: Option<Thread>,
|
||||
) -> Result<Thread, ThreadReadViewError> {
|
||||
let persisted = self
|
||||
.load_persisted_thread_for_read(thread_id, /*include_turns*/ false)
|
||||
.await?;
|
||||
let config_snapshot = loaded_thread.config_snapshot().await;
|
||||
if include_turns && config_snapshot.ephemeral {
|
||||
return Err(ThreadReadViewError::InvalidRequest(
|
||||
"ephemeral threads do not support includeTurns".to_string(),
|
||||
));
|
||||
}
|
||||
let fallback_thread =
|
||||
build_thread_from_loaded_snapshot(thread_id, &config_snapshot, loaded_thread);
|
||||
let mut thread = if let Some(mut thread) = persisted_thread {
|
||||
|
||||
let mut thread = if let Some(mut thread) = persisted {
|
||||
if thread.path.is_none() {
|
||||
thread.path = fallback_thread.path.clone();
|
||||
thread.path = loaded_thread.rollout_path();
|
||||
}
|
||||
thread.session_id = loaded_thread.startup_metadata().session_id.to_string();
|
||||
thread.ephemeral = config_snapshot.ephemeral;
|
||||
thread.can_accept_direct_input = Some(can_accept_direct_input(
|
||||
loaded_thread.multi_agent_version(),
|
||||
&config_snapshot.session_source,
|
||||
));
|
||||
apply_live_thread_settings(&mut thread, &config_snapshot);
|
||||
if !include_turns {
|
||||
if let Some(title) = display_thread_title(
|
||||
thread.history_mode,
|
||||
&thread.preview,
|
||||
thread.name.as_deref(),
|
||||
) {
|
||||
thread.name = Some(title.to_owned());
|
||||
}
|
||||
return Ok(thread);
|
||||
}
|
||||
thread.session_id.clone_from(&fallback_thread.session_id);
|
||||
thread.ephemeral = fallback_thread.ephemeral;
|
||||
thread.can_accept_direct_input = fallback_thread.can_accept_direct_input;
|
||||
thread
|
||||
} else {
|
||||
fallback_thread
|
||||
build_thread_from_loaded_snapshot(thread_id, &config_snapshot, loaded_thread)
|
||||
};
|
||||
apply_live_thread_settings(&mut thread, &config_snapshot);
|
||||
self.apply_thread_read_store_fields(thread_id, &mut thread, include_turns, loaded_thread)
|
||||
.await?;
|
||||
Ok(thread)
|
||||
}
|
||||
|
||||
async fn apply_thread_read_store_fields(
|
||||
&self,
|
||||
thread_id: ThreadId,
|
||||
thread: &mut Thread,
|
||||
include_turns: bool,
|
||||
loaded_thread: &CodexThread,
|
||||
) -> Result<(), ThreadReadViewError> {
|
||||
self.attach_thread_name(thread_id, thread).await;
|
||||
|
||||
self.attach_thread_name(thread_id, &mut thread).await;
|
||||
if include_turns {
|
||||
if matches!(
|
||||
thread.history_mode,
|
||||
codex_app_server_protocol::ThreadHistoryMode::Paginated
|
||||
) {
|
||||
self.thread_store
|
||||
.persist_thread(thread_id, PersistContext::Standard)
|
||||
.await
|
||||
.map_err(|err| thread_read_history_load_error(thread_id, err))?;
|
||||
thread.turns = self
|
||||
.paginated_thread_full_turns(thread_id)
|
||||
.await
|
||||
.map_err(ThreadReadViewError::JsonRpc)?;
|
||||
return Ok(());
|
||||
}
|
||||
let history = loaded_thread
|
||||
.load_history(/*include_archived*/ true)
|
||||
.await
|
||||
.map_err(|err| thread_read_history_load_error(thread_id, err))?;
|
||||
thread.turns = build_legacy_api_turns_from_rollout_items(&history.items);
|
||||
thread.turns = match thread.history_mode {
|
||||
ApiThreadHistoryMode::Paginated => {
|
||||
self.thread_store
|
||||
.persist_thread(thread_id, PersistContext::Standard)
|
||||
.await
|
||||
.map_err(|err| thread_read_history_load_error(thread_id, err))?;
|
||||
self.paginated_thread_full_turns(thread_id)
|
||||
.await
|
||||
.map_err(ThreadReadViewError::JsonRpc)?
|
||||
}
|
||||
ApiThreadHistoryMode::Legacy => {
|
||||
let history = loaded_thread
|
||||
.load_history(/*include_archived*/ true)
|
||||
.await
|
||||
.map_err(|err| thread_read_history_load_error(thread_id, err))?;
|
||||
build_legacy_api_turns_from_rollout_items(&history.items)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(thread)
|
||||
}
|
||||
|
||||
async fn thread_turns_list_response_inner(
|
||||
@@ -5283,14 +5241,13 @@ impl ThreadRequestProcessor {
|
||||
include_history: false,
|
||||
})
|
||||
.await
|
||||
&& let Some(title) = stored_thread.name.as_deref().map(str::trim)
|
||||
&& !title.is_empty()
|
||||
&& let Some(title) = display_thread_title(
|
||||
stored_thread.history_mode.into(),
|
||||
&thread.preview,
|
||||
stored_thread.name.as_deref(),
|
||||
)
|
||||
{
|
||||
if stored_thread.history_mode == ThreadHistoryMode::Paginated {
|
||||
thread.name = Some(title.to_string());
|
||||
} else {
|
||||
set_thread_name_from_title(thread, title.to_string());
|
||||
}
|
||||
thread.name = Some(title.to_owned());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6732,6 +6689,17 @@ fn thread_store_mutation_error(operation: &str, err: ThreadStoreError) -> JSONRP
|
||||
}
|
||||
}
|
||||
|
||||
/// Legacy threads can use their preview as a fallback title; don't promote it to an explicit name.
|
||||
fn display_thread_title<'a>(
|
||||
mode: ApiThreadHistoryMode,
|
||||
preview: &str,
|
||||
stored: Option<&'a str>,
|
||||
) -> Option<&'a str> {
|
||||
stored.map(str::trim).filter(|title| {
|
||||
!title.is_empty() && (mode == ApiThreadHistoryMode::Paginated || *title != preview.trim())
|
||||
})
|
||||
}
|
||||
|
||||
fn set_thread_name_from_title(thread: &mut Thread, title: String) {
|
||||
if title.trim().is_empty() || thread.preview.trim() == title.trim() {
|
||||
return;
|
||||
|
||||
@@ -1005,7 +1005,8 @@ async fn thread_turns_list_reads_store_history_without_rollout_path() -> Result<
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn thread_read_loaded_include_turns_reads_store_history_without_rollout_path() -> Result<()> {
|
||||
async fn thread_read_loaded_reads_stored_title_and_requested_history_without_rollout_path()
|
||||
-> Result<()> {
|
||||
let codex_home = TempDir::new()?;
|
||||
let store_id = Uuid::new_v4().to_string();
|
||||
MockResponsesConfig::new("http://127.0.0.1:1")
|
||||
@@ -1079,15 +1080,31 @@ async fn thread_read_loaded_include_turns_reads_store_history_without_rollout_pa
|
||||
thread_id,
|
||||
patch: ThreadMetadataPatch {
|
||||
daybreak_enabled: Some(false),
|
||||
name: Some(Some(" Stored title ".to_string())),
|
||||
..Default::default()
|
||||
},
|
||||
include_archived: false,
|
||||
})
|
||||
.await?;
|
||||
|
||||
let result = client
|
||||
let reads_before = store.calls().await.read_thread;
|
||||
let metadata = client
|
||||
.request(ClientRequest::ThreadRead {
|
||||
request_id: RequestId::Integer(2),
|
||||
params: ThreadReadParams {
|
||||
thread_id: thread.id.clone(),
|
||||
include_turns: false,
|
||||
},
|
||||
})
|
||||
.await?
|
||||
.expect("metadata-only thread/read should succeed");
|
||||
let metadata: ThreadReadResponse = serde_json::from_value(metadata)?;
|
||||
assert_eq!(metadata.thread.name.as_deref(), Some("Stored title"));
|
||||
assert_eq!(store.calls().await.read_thread - reads_before, 1);
|
||||
|
||||
let result = client
|
||||
.request(ClientRequest::ThreadRead {
|
||||
request_id: RequestId::Integer(3),
|
||||
params: ThreadReadParams {
|
||||
thread_id: thread.id,
|
||||
include_turns: true,
|
||||
|
||||
Reference in New Issue
Block a user