Persist search service browser state

This commit is contained in:
miaolin
2026-05-28 23:42:50 -07:00
parent 522f549922
commit df769ef24b
19 changed files with 820 additions and 15 deletions
+2
View File
@@ -368,6 +368,8 @@ fn stored_thread_from_state(
.unwrap_or_else(SandboxPolicy::new_read_only_policy),
token_usage: metadata.and_then(|metadata| metadata.token_usage.clone()),
first_user_message: metadata.and_then(|metadata| metadata.first_user_message.clone()),
search_service_browser_state: metadata
.and_then(|metadata| metadata.search_service_browser_state.clone()),
history,
})
}
@@ -143,6 +143,7 @@ pub(super) fn stored_thread_from_rollout_item(
sandbox_policy: SandboxPolicy::new_read_only_policy(),
token_usage: None,
first_user_message: item.first_user_message,
search_service_browser_state: None,
history: None,
})
}
@@ -57,6 +57,7 @@ pub(super) async fn read_thread(
rollout_thread.name = thread.name;
}
rollout_thread.git_info = thread.git_info;
rollout_thread.search_service_browser_state = thread.search_service_browser_state;
thread = rollout_thread;
}
attach_history_if_requested(&mut thread, params.include_history).await?;
@@ -123,6 +124,7 @@ pub(super) async fn read_thread_by_rollout_path(
metadata.git_branch.or(fallback_branch),
metadata.git_origin_url.or(fallback_origin_url),
);
thread.search_service_browser_state = metadata.search_service_browser_state;
}
attach_history_if_requested(&mut thread, include_history).await?;
Ok(thread)
@@ -321,6 +323,7 @@ async fn stored_thread_from_sqlite_metadata(
),
token_usage: None,
first_user_message: metadata.first_user_message,
search_service_browser_state: metadata.search_service_browser_state,
history: None,
}
}
@@ -380,6 +383,7 @@ fn stored_thread_from_meta_line(
sandbox_policy: SandboxPolicy::new_read_only_policy(),
token_usage: None,
first_user_message: None,
search_service_browser_state: None,
history: None,
}
}
@@ -284,6 +284,9 @@ async fn apply_metadata_update(
if let Some(first_user_message) = patch.first_user_message {
metadata.first_user_message = Some(first_user_message);
}
if let Some(search_service_browser_state) = patch.search_service_browser_state {
metadata.search_service_browser_state = Some(search_service_browser_state);
}
if let Some(git_info) = patch.git_info {
let existing_git_info = git_info_from_parts(
metadata.git_sha.clone(),
@@ -323,6 +326,11 @@ async fn apply_metadata_update(
(true, Err(err)) => {
warn!("state db update_thread_metadata failed for {thread_id}: {err}");
}
(false, Ok(())) if require_sqlite_write => {
return Err(ThreadStoreError::Internal {
message: format!("sqlite state db unavailable for thread {thread_id}"),
});
}
(false, Ok(())) => {}
(false, Err(err)) if require_sqlite_write || !sqlite_write_error_is_best_effort(&err) => {
return Err(err);
@@ -359,7 +367,8 @@ fn sqlite_write_failure_should_block(patch: &ThreadMetadataPatch) -> bool {
// failure isolation so a corrupted optional state DB does not make JSONL transcript durability
// look broken. Explicit git-only updates still require SQLite because partial git patches need
// the existing SQLite value to preserve unspecified fields.
patch.git_info.is_some() && !has_observed_metadata_facts(patch)
patch.search_service_browser_state.is_some()
|| (patch.git_info.is_some() && !has_observed_metadata_facts(patch))
}
fn sqlite_write_error_is_best_effort(err: &ThreadStoreError) -> bool {
@@ -385,6 +394,7 @@ fn has_observed_metadata_facts(patch: &ThreadMetadataPatch) -> bool {
|| patch.sandbox_policy.is_some()
|| patch.token_usage.is_some()
|| patch.first_user_message.is_some()
|| patch.search_service_browser_state.is_some()
}
fn enum_to_string<T: serde::Serialize>(value: &T) -> String {
@@ -761,6 +771,54 @@ mod tests {
assert_eq!(memory_mode.as_deref(), Some("disabled"));
}
#[tokio::test]
async fn update_thread_metadata_persists_search_service_browser_state() {
let home = TempDir::new().expect("temp dir");
let config = test_config(home.path());
let uuid = Uuid::from_u128(313);
let thread_id = ThreadId::from_string(&uuid.to_string()).expect("valid thread id");
write_session_file(home.path(), "2025-01-03T18-45-00", uuid).expect("session file");
let runtime = codex_state::StateRuntime::init(
config.sqlite_home.clone(),
config.default_model_provider_id.clone(),
)
.await
.expect("state db should initialize");
let store = LocalThreadStore::new(config, Some(runtime.clone()));
let browser_state = json!({
"sonic_thread_id": "sonic-thread-123",
"external_urls": {
"turn0search0": "https://example.com/"
}
});
let thread = store
.update_thread_metadata(UpdateThreadMetadataParams {
thread_id,
patch: ThreadMetadataPatch {
search_service_browser_state: Some(browser_state.clone()),
..Default::default()
},
include_archived: false,
})
.await
.expect("persist search service browser state");
assert_eq!(
thread.search_service_browser_state.as_ref(),
Some(&browser_state)
);
let persisted = runtime
.get_thread(thread_id)
.await
.expect("read persisted metadata")
.expect("thread metadata");
assert_eq!(
persisted.search_service_browser_state.as_ref(),
Some(&browser_state)
);
}
#[tokio::test]
async fn update_thread_metadata_uses_live_rollout_path_for_external_resume() {
let home = TempDir::new().expect("temp dir");
@@ -1199,6 +1257,14 @@ mod tests {
}));
}
#[test]
fn sqlite_failures_block_for_search_service_browser_state_updates() {
assert!(sqlite_write_failure_should_block(&ThreadMetadataPatch {
search_service_browser_state: Some(json!({"sonic_thread_id": "sonic-thread-123"})),
..Default::default()
}));
}
#[tokio::test]
async fn metadata_patch_applies_title_over_existing_name() {
let home = TempDir::new().expect("temp dir");
+8
View File
@@ -402,6 +402,8 @@ pub struct StoredThread {
pub token_usage: Option<TokenUsage>,
/// First user message observed for this thread, if any.
pub first_user_message: Option<String>,
/// Search Service browser state needed by follow-up browse/open calls.
pub search_service_browser_state: Option<serde_json::Value>,
/// Persisted history, populated only when requested.
pub history: Option<StoredThreadHistory>,
}
@@ -525,6 +527,8 @@ pub struct ThreadMetadataPatch {
pub token_usage: Option<TokenUsage>,
/// First user message observed for this thread.
pub first_user_message: Option<String>,
/// Search Service browser state needed by follow-up browse/open calls.
pub search_service_browser_state: Option<serde_json::Value>,
/// Git metadata patch.
pub git_info: Option<GitInfoPatch>,
/// Thread memory behavior.
@@ -598,6 +602,9 @@ impl ThreadMetadataPatch {
if next.first_user_message.is_some() {
self.first_user_message = next.first_user_message;
}
if next.search_service_browser_state.is_some() {
self.search_service_browser_state = next.search_service_browser_state;
}
if let Some(git_info) = next.git_info {
self.git_info
.get_or_insert_with(GitInfoPatch::default)
@@ -629,6 +636,7 @@ impl ThreadMetadataPatch {
&& self.sandbox_policy.is_none()
&& self.token_usage.is_none()
&& self.first_user_message.is_none()
&& self.search_service_browser_state.is_none()
&& self.git_info.is_none()
&& self.memory_mode.is_none()
}