Add persisted manual ordering for thread sections (#36007)

## What changed

- Add `thread/section/move` to atomically move a thread into, within, or out of a section. Threads can be inserted before an existing member or appended, and moves within a section preserve `sectionEnteredAt`.
- Add `section_position` sorting to `thread/list`, with ascending order as its default, and expose `sectionEnteredAt` in thread responses.
- Persist section positions and entry times in SQLite, including migration of existing section members into recency order. Section membership is no longer updated through `thread/metadata/update`.

## Testing

- Cover section moves, reordering, pagination, persistence across restarts and rollout reconciliation, concurrent updates, and rank renumbering.

GitOrigin-RevId: aec6d7ddedca5277029b5caf5c074975397e956c
This commit is contained in:
joeytrasatti-openai authored and copyberry committed 2026-07-29 18:37:22 +00:00
1 parent c41a38dd10
commit ad6fc66b6d
75 files changed
+2879 -543

No files matched your search

@@ -145,6 +145,8 @@ pub(super) fn stored_thread_from_rollout_item(
recency_at,
archived_at,
section: item.section,
section_position: None,
section_entered_at: None,
cwd: item.cwd.unwrap_or_default(),
cli_version: item.cli_version.unwrap_or_default(),
source,
@@ -192,6 +194,20 @@ pub(super) fn sqlite_thread_name(metadata: &ThreadMetadata) -> Option<String> {
.map(str::to_string)
}
pub(super) async fn resolve_thread_section_metadata(
state_db: &codex_state::StateRuntime,
thread_ids: &[ThreadId],
) -> HashMap<ThreadId, (Option<i64>, Option<DateTime<Utc>>)> {
if thread_ids.is_empty() {
return HashMap::new();
}
state_db
.get_thread_section_ordering(thread_ids)
.await
.unwrap_or_default()
}
pub(super) async fn resolve_thread_names(
store: &LocalThreadStore,
thread_history_modes: &HashMap<ThreadId, ThreadHistoryMode>,
+244 -1
View File
@@ -1,13 +1,18 @@
use std::collections::HashMap;
use chrono::DateTime;
use chrono::Utc;
use codex_rollout::RolloutConfig;
use codex_rollout::RolloutRecorder;
use codex_rollout::parse_cursor;
use codex_state::ThreadFilterOptions;
use super::LocalThreadStore;
use super::helpers::resolve_thread_names;
use super::helpers::resolve_thread_section_metadata;
use super::helpers::set_thread_name;
use super::helpers::stored_thread_from_rollout_item;
use super::read_thread::stored_thread_from_state_metadata;
use crate::ListThreadsParams;
use crate::SortDirection;
use crate::ThreadPage;
@@ -20,6 +25,9 @@ pub(super) async fn list_threads(
store: &LocalThreadStore,
params: ListThreadsParams,
) -> ThreadStoreResult<ThreadPage> {
if params.sort_key == ThreadSortKey::SectionPosition {
return list_section_threads(store, params).await;
}
let cursor = params
.cursor
.as_deref()
@@ -33,6 +41,7 @@ pub(super) async fn list_threads(
ThreadSortKey::CreatedAt => codex_rollout::ThreadSortKey::CreatedAt,
ThreadSortKey::UpdatedAt => codex_rollout::ThreadSortKey::UpdatedAt,
ThreadSortKey::RecencyAt => codex_rollout::ThreadSortKey::RecencyAt,
ThreadSortKey::SectionPosition => unreachable!("section order uses the state database"),
};
let sort_direction = match params.sort_direction {
SortDirection::Asc => codex_rollout::SortDirection::Asc,
@@ -47,7 +56,7 @@ pub(super) async fn list_threads(
generate_memories: false,
};
let page = list_rollout_threads(
state_db,
state_db.clone(),
&rollout_config,
store.config.default_model_provider_id.as_str(),
&params,
@@ -84,10 +93,149 @@ pub(super) async fn list_threads(
set_thread_name(thread, name);
}
}
if let Some(state_db) = state_db {
let sectioned_thread_ids = items
.iter()
.filter(|thread| thread.section.is_some())
.map(|thread| thread.thread_id)
.collect::<Vec<_>>();
let section_metadata =
resolve_thread_section_metadata(state_db.as_ref(), &sectioned_thread_ids).await;
for thread in items.iter_mut().filter(|thread| thread.section.is_some()) {
if let Some((section_position, section_entered_at)) =
section_metadata.get(&thread.thread_id)
{
thread.section_position = *section_position;
thread.section_entered_at = *section_entered_at;
}
}
}
Ok(ThreadPage { items, next_cursor })
}
async fn list_section_threads(
store: &LocalThreadStore,
params: ListThreadsParams,
) -> ThreadStoreResult<ThreadPage> {
let section = params
.section
.as_ref()
.and_then(Option::as_deref)
.ok_or_else(|| ThreadStoreError::InvalidRequest {
message: "section-position sorting requires a section filter".to_owned(),
})?;
let state_db = store
.state_db()
.await
.ok_or_else(|| ThreadStoreError::Internal {
message: "state DB unavailable for section-ordered thread listing".to_owned(),
})?;
let anchor = params
.cursor
.as_deref()
.map(|cursor| -> ThreadStoreResult<codex_state::Anchor> {
let (position, thread_id) =
cursor
.split_once('|')
.ok_or_else(|| ThreadStoreError::InvalidRequest {
message: format!("invalid cursor: {cursor}"),
})?;
let timestamp = position
.parse::<i64>()
.ok()
.and_then(DateTime::<Utc>::from_timestamp_millis)
.ok_or_else(|| ThreadStoreError::InvalidRequest {
message: format!("invalid cursor: {cursor}"),
})?;
let thread_id = codex_protocol::ThreadId::from_string(thread_id).map_err(|_| {
ThreadStoreError::InvalidRequest {
message: format!("invalid cursor: {cursor}"),
}
})?;
Ok(codex_state::Anchor {
ts: timestamp,
id: Some(thread_id),
})
})
.transpose()?;
let allowed_sources = params
.allowed_sources
.iter()
.map(|source| match serde_json::to_value(source) {
Ok(serde_json::Value::String(source)) => source,
Ok(source) => source.to_string(),
Err(_) => String::new(),
})
.collect::<Vec<_>>();
let normalized_cwd_filters = params.cwd_filters.as_ref().map(|filters| {
filters
.iter()
.map(|cwd| codex_rollout::state_db::normalize_cwd_for_state_db(cwd))
.collect::<Vec<_>>()
});
let filters = ThreadFilterOptions {
archived_only: params.archived,
allowed_sources: allowed_sources.as_slice(),
model_providers: params.model_providers.as_deref(),
cwd_filters: normalized_cwd_filters.as_deref(),
section: Some(Some(section)),
anchor: anchor.as_ref(),
sort_key: codex_state::SortKey::SectionPosition,
sort_direction: match params.sort_direction {
SortDirection::Asc => codex_state::SortDirection::Asc,
SortDirection::Desc => codex_state::SortDirection::Desc,
},
search_term: params.search_term.as_deref(),
};
let page = match params.relation_filter {
Some(ThreadRelationFilter::DirectChildrenOf(thread_id)) => {
state_db
.list_threads_by_relation(
params.page_size,
codex_state::ThreadRelationFilter::DirectChildrenOf(thread_id),
filters,
)
.await
}
Some(ThreadRelationFilter::DescendantsOf(thread_id)) => {
state_db
.list_threads_by_relation(
params.page_size,
codex_state::ThreadRelationFilter::DescendantsOf(thread_id),
filters,
)
.await
}
None => state_db.list_threads(params.page_size, filters).await,
}
.map_err(|err| ThreadStoreError::Internal {
message: format!("failed to list section-ordered threads: {err}"),
})?;
let codex_state::ThreadsPage {
items: metadata_items,
parent_thread_ids,
next_anchor,
..
} = page;
let items = metadata_items
.into_iter()
.map(|metadata| {
let parent_thread_id = parent_thread_ids.get(&metadata.id).copied();
stored_thread_from_state_metadata(store, metadata, parent_thread_id)
})
.collect();
let next_cursor = next_anchor.and_then(|anchor| {
anchor.id.map(|thread_id| {
let position = anchor.ts.timestamp_millis();
format!("{position}|{thread_id}")
})
});
Ok(ThreadPage { items, next_cursor })
}
pub(super) async fn list_rollout_threads(
state_db: Option<codex_rollout::StateDbHandle>,
config: &RolloutConfig,
@@ -202,6 +350,7 @@ mod tests {
use codex_protocol::ThreadId;
use codex_protocol::protocol::SessionSource;
use codex_protocol::protocol::ThreadHistoryMode;
use codex_state::PINNED_THREAD_SECTION_ID;
use codex_utils_absolute_path::test_support::PathExt;
use pretty_assertions::assert_eq;
use std::fs;
@@ -209,6 +358,7 @@ mod tests {
use uuid::Uuid;
use super::*;
use crate::MoveThreadToSectionParams;
use crate::ThreadStore;
use crate::local::LocalThreadStore;
use crate::local::test_support::test_config;
@@ -505,6 +655,99 @@ mod tests {
assert_eq!(page.items[0].source, SessionSource::Cli);
}
#[tokio::test]
async fn section_listing_uses_sqlite_metadata_without_reading_rollouts() {
let home = TempDir::new().expect("temp dir");
let config = test_config(home.path());
let state = codex_state::StateRuntime::init(
config.sqlite.clone(),
config.default_model_provider_id.clone(),
)
.await
.expect("initialize state");
let store = LocalThreadStore::new(config, Some(state.clone()));
let mut thread_ids = Vec::new();
let mut first_rollout_path = None;
for index in 0..3 {
let uuid = Uuid::from_u128(975 + index);
let thread_id = ThreadId::from_string(&uuid.to_string()).expect("valid thread id");
let timestamp = format!("2025-01-03T16-{index:02}-00");
let rollout_path =
write_session_file(home.path(), &timestamp, uuid).expect("write rollout");
codex_rollout::state_db::reconcile_rollout(
Some(state.as_ref()),
rollout_path.as_path(),
"test-provider",
/*builder*/ None,
&[],
/*archived_only*/ None,
/*new_thread_memory_mode*/ None,
)
.await;
store
.move_thread_to_section(MoveThreadToSectionParams {
thread_id,
section: Some(PINNED_THREAD_SECTION_ID.to_owned()),
before_thread_id: None,
})
.await
.expect("append section member");
thread_ids.push(thread_id);
if index == 0 {
first_rollout_path = Some(rollout_path);
}
}
fs::remove_file(first_rollout_path.expect("first rollout path"))
.expect("remove rollout without invalidating SQLite metadata");
let params = ListThreadsParams {
page_size: 2,
cursor: None,
sort_key: ThreadSortKey::SectionPosition,
sort_direction: SortDirection::Asc,
allowed_sources: Vec::new(),
model_providers: None,
cwd_filters: None,
section: Some(Some(PINNED_THREAD_SECTION_ID.to_owned())),
archived: false,
search_term: None,
relation_filter: None,
use_state_db_only: true,
};
let page = store
.list_threads(params.clone())
.await
.expect("section listing should use SQLite metadata");
assert_eq!(
page.items
.iter()
.map(|thread| thread.thread_id)
.collect::<Vec<_>>(),
vec![thread_ids[0], thread_ids[1]]
);
assert_eq!(page.next_cursor, Some(format!("2000000|{}", thread_ids[1])));
let next_page = store
.list_threads(ListThreadsParams {
cursor: page.next_cursor,
..params
})
.await
.expect("section cursor should continue listing from SQLite metadata");
assert_eq!(
next_page
.items
.iter()
.map(|thread| thread.thread_id)
.collect::<Vec<_>>(),
vec![thread_ids[2]]
);
assert_eq!(next_page.next_cursor, None);
}
#[tokio::test]
async fn list_threads_rejects_invalid_cursor() {
let home = TempDir::new().expect("temp dir");
+9
View File
@@ -5,6 +5,7 @@ mod helpers;
mod list_threads;
mod live_writer;
mod model_context;
mod move_thread_to_section;
mod paginated_fork;
mod read_thread;
// This lands before the reader PRs that consume the shared lineage resolver.
@@ -47,6 +48,7 @@ use crate::ListItemsParams;
use crate::ListThreadsParams;
use crate::ListTurnsParams;
use crate::LoadThreadHistoryParams;
use crate::MoveThreadToSectionParams;
use crate::PrepareForkParams;
use crate::PreparedFork;
use crate::ReadThreadByRolloutPathParams;
@@ -489,6 +491,13 @@ impl ThreadStore for LocalThreadStore {
Box::pin(async move { update_thread_metadata::update_thread_metadata(self, params).await })
}
fn move_thread_to_section(
&self,
params: MoveThreadToSectionParams,
) -> ThreadStoreFuture<'_, ()> {
Box::pin(async move { move_thread_to_section::move_thread_to_section(self, params).await })
}
fn archive_thread(&self, params: ArchiveThreadParams) -> ThreadStoreFuture<'_, ()> {
Box::pin(async move {
archive_thread::archive_threads(
@@ -0,0 +1,58 @@
use super::LocalThreadStore;
use crate::MoveThreadToSectionParams;
use crate::ThreadStoreError;
use crate::ThreadStoreResult;
pub(super) async fn move_thread_to_section(
store: &LocalThreadStore,
params: MoveThreadToSectionParams,
) -> ThreadStoreResult<()> {
if params
.section
.as_deref()
.is_some_and(|section| section.trim().is_empty())
{
return Err(ThreadStoreError::InvalidRequest {
message: "section must not be empty".to_owned(),
});
}
if params.section.is_none() && params.before_thread_id.is_some() {
return Err(ThreadStoreError::InvalidRequest {
message: "before thread cannot be specified without a section".to_owned(),
});
}
let Some(state_db) = store.state_db().await else {
return Err(ThreadStoreError::Unsupported {
operation: "thread/section/move",
});
};
let updated = state_db
.move_thread_to_section(
params.thread_id,
params.section.as_deref(),
params.before_thread_id,
)
.await
.map_err(|err| {
let message = err.to_string();
if message.starts_with("before thread ")
|| message.starts_with("thread ")
|| message.starts_with("section ")
{
ThreadStoreError::InvalidRequest { message }
} else {
ThreadStoreError::Internal {
message: format!("failed to move thread {}: {message}", params.thread_id),
}
}
})?;
if !updated {
return Err(ThreadStoreError::ThreadNotFound {
thread_id: params.thread_id,
});
}
Ok(())
}
+53 -7
View File
@@ -63,6 +63,8 @@ pub(super) async fn read_thread(
{
rollout_thread.recency_at = thread.recency_at;
rollout_thread.section = thread.section;
rollout_thread.section_position = thread.section_position;
rollout_thread.section_entered_at = thread.section_entered_at;
if thread.name.is_some() {
rollout_thread.name = thread.name;
}
@@ -133,6 +135,8 @@ pub(super) async fn read_thread_by_rollout_path(
} else {
thread.recency_at = metadata.recency_at;
thread.section = metadata.section;
thread.section_position = metadata.section_position;
thread.section_entered_at = metadata.section_entered_at;
let (fallback_sha, fallback_branch, fallback_origin_url) = match thread.git_info.take()
{
Some(info) => (
@@ -326,12 +330,22 @@ async fn read_sqlite_metadata(
runtime.get_thread(thread_id).await.ok().flatten()
}
async fn stored_thread_from_sqlite_metadata(
pub(super) async fn stored_thread_from_sqlite_metadata(
store: &LocalThreadStore,
metadata: ThreadMetadata,
) -> ThreadStoreResult<StoredThread> {
let session_meta = match read_required_session_meta_line(metadata.rollout_path.as_path()).await
{
Ok(meta_line) if meta_line.meta.id != metadata.id => {
return Err(ThreadStoreError::Internal {
message: format!(
"session metadata {} belongs to thread {}, expected {}",
metadata.rollout_path.display(),
meta_line.meta.id,
metadata.id
),
});
}
Ok(meta_line) => Some(meta_line.meta),
Err(_)
if codex_rollout::existing_rollout_path(metadata.rollout_path.as_path())
@@ -349,7 +363,6 @@ async fn stored_thread_from_sqlite_metadata(
});
}
};
let rollout_path = codex_rollout::plain_rollout_path(metadata.rollout_path.as_path());
let forked_from_id = session_meta.as_ref().and_then(|meta| meta.forked_from_id);
let parent_thread_id = session_meta.as_ref().and_then(|meta| meta.parent_thread_id);
let history_mode = session_meta
@@ -357,6 +370,23 @@ async fn stored_thread_from_sqlite_metadata(
.map(|meta| meta.history_mode)
.unwrap_or(metadata.history_mode);
let name = thread_name_from_metadata(store, &metadata, history_mode).await;
let mut thread = stored_thread_from_state_metadata(store, metadata, parent_thread_id);
thread.forked_from_id = forked_from_id;
thread.history_mode = history_mode;
thread.name = name;
Ok(thread)
}
pub(super) fn stored_thread_from_state_metadata(
store: &LocalThreadStore,
metadata: ThreadMetadata,
parent_thread_id: Option<codex_protocol::ThreadId>,
) -> StoredThread {
let name = match metadata.history_mode {
ThreadHistoryMode::Paginated => sqlite_thread_name(&metadata),
ThreadHistoryMode::Legacy => distinct_thread_metadata_title(&metadata),
};
let rollout_path = codex_rollout::plain_rollout_path(metadata.rollout_path.as_path());
let preview = metadata
.preview
.clone()
@@ -364,11 +394,11 @@ async fn stored_thread_from_sqlite_metadata(
.unwrap_or_default();
let permission_profile =
permission_profile_from_metadata_value(&metadata.sandbox_policy, metadata.cwd.as_path());
Ok(StoredThread {
StoredThread {
thread_id: metadata.id,
extra_config: None,
rollout_path: Some(rollout_path),
forked_from_id,
forked_from_id: None,
parent_thread_id,
preview,
name,
@@ -384,10 +414,12 @@ async fn stored_thread_from_sqlite_metadata(
recency_at: metadata.recency_at,
archived_at: metadata.archived_at,
section: metadata.section,
section_position: metadata.section_position,
section_entered_at: metadata.section_entered_at,
cwd: metadata.cwd,
cli_version: metadata.cli_version,
source: parse_session_source(&metadata.source),
history_mode,
history_mode: metadata.history_mode,
thread_source: metadata.thread_source,
agent_nickname: metadata.agent_nickname,
agent_role: metadata.agent_role,
@@ -402,7 +434,7 @@ async fn stored_thread_from_sqlite_metadata(
token_usage: None,
first_user_message: metadata.first_user_message,
history: None,
})
}
}
async fn thread_name_from_metadata(
@@ -480,6 +512,8 @@ fn stored_thread_from_meta_line(
recency_at: updated_at,
archived_at: archived.then_some(updated_at),
section: None,
section_position: None,
section_entered_at: None,
cwd: meta_line.meta.cwd,
cli_version: meta_line.meta.cli_version,
source: meta_line.meta.source,
@@ -635,6 +669,12 @@ mod tests {
builder.recency_at = Some(recency_at);
let mut metadata = builder.build(config.default_model_provider_id.as_str());
metadata.title = "Stale SQLite name".to_string();
metadata.section = Some(codex_state::ThreadSection {
id: codex_state::PINNED_THREAD_SECTION_ID.to_string(),
name: codex_state::PINNED_THREAD_SECTION_NAME.to_string(),
});
metadata.section_position = Some(2_000_000);
metadata.section_entered_at = Some(recency_at);
runtime
.upsert_thread(&metadata)
.await
@@ -655,6 +695,12 @@ mod tests {
let git_info = thread.git_info.expect("git info should be present");
assert_eq!(thread.name.as_deref(), Some("Latest index name"));
assert_eq!(thread.recency_at, recency_at);
assert_eq!(
thread.section.as_ref().map(|section| section.id.as_str()),
Some(codex_state::PINNED_THREAD_SECTION_ID)
);
assert_eq!(thread.section_position, Some(2_000_000));
assert_eq!(thread.section_entered_at, Some(recency_at));
assert_eq!(git_info.branch.as_deref(), Some("sqlite-branch"));
assert_eq!(
git_info.commit_hash.as_ref().map(|sha| sha.0.as_str()),
@@ -827,7 +873,7 @@ mod tests {
text: "Rollout user message".to_string(),
text_elements: Vec::new(),
}])),
started_at_ms: None,
started_at_ms: Some(0),
completed_at_ms: 0,
})),
)
@@ -8,6 +8,7 @@ use codex_rollout::search_rollout_matches;
use super::LocalThreadStore;
use super::helpers::resolve_thread_names;
use super::helpers::resolve_thread_section_metadata;
use super::helpers::set_thread_name;
use super::helpers::stored_thread_from_rollout_item;
use super::list_threads::list_rollout_threads;
@@ -52,6 +53,11 @@ pub(super) async fn search_threads(
ThreadSortKey::CreatedAt => codex_rollout::ThreadSortKey::CreatedAt,
ThreadSortKey::UpdatedAt => codex_rollout::ThreadSortKey::UpdatedAt,
ThreadSortKey::RecencyAt => codex_rollout::ThreadSortKey::RecencyAt,
ThreadSortKey::SectionPosition => {
return Err(ThreadStoreError::InvalidRequest {
message: "section-position sorting requires a section filter".to_owned(),
});
}
};
let sort_direction = match params.sort_direction {
SortDirection::Asc => codex_rollout::SortDirection::Asc,
@@ -166,6 +172,23 @@ pub(super) async fn search_threads(
})
})
.collect::<Vec<_>>();
if let Some(state_db) = state_db {
let sectioned_thread_ids = items
.iter()
.filter(|item| item.thread.section.is_some())
.map(|item| item.thread.thread_id)
.collect::<Vec<_>>();
let mut section_metadata =
resolve_thread_section_metadata(state_db.as_ref(), &sectioned_thread_ids).await;
for item in &mut items {
if let Some((section_position, section_entered_at)) =
section_metadata.remove(&item.thread.thread_id)
{
item.thread.section_position = section_position;
item.thread.section_entered_at = section_entered_at;
}
}
}
set_thread_search_result_names(store, &mut items).await;
Ok(ThreadSearchPage { items, next_cursor })
@@ -188,10 +211,12 @@ fn cursor_from_thread_search_item(
.as_deref()
.or(item.item.updated_at.as_deref())
.or(item.item.created_at.as_deref())?,
ThreadSortKey::SectionPosition => return None,
};
match sort_key {
ThreadSortKey::RecencyAt => parse_cursor(&format!("{timestamp}|{}", item.item.thread_id?)),
ThreadSortKey::CreatedAt | ThreadSortKey::UpdatedAt => parse_cursor(timestamp),
ThreadSortKey::SectionPosition => None,
}
}
@@ -264,7 +264,7 @@ async fn apply_metadata_update(
let sqlite_write_result: ThreadStoreResult<()> = if let Some(state_db) = state_db.as_ref() {
let patch = patch.clone();
async {
let mut existing =
let existing =
state_db
.get_thread(thread_id)
.await
@@ -277,30 +277,6 @@ async fn apply_metadata_update(
rollout_path_archived = resolved.archived;
rollout_path = Some(resolved.path);
}
if existing.is_none()
&& patch.section.is_some()
&& let Some(path) = rollout_path.as_deref()
&& let Some(existing_rollout_path) =
codex_rollout::existing_rollout_path(path).await
{
codex_rollout::state_db::reconcile_rollout(
Some(state_db.as_ref()),
existing_rollout_path.as_path(),
store.config.default_model_provider_id.as_str(),
/*builder*/ None,
&[],
/*archived_only*/ Some(rollout_path_archived),
/*new_thread_memory_mode*/ None,
)
.await;
existing = state_db.get_thread(thread_id).await.map_err(|err| {
ThreadStoreError::Internal {
message: format!(
"failed to read reconciled thread metadata for {thread_id}: {err}"
),
}
})?;
}
let mut metadata = match existing.clone() {
Some(metadata) => metadata,
None => {
@@ -389,24 +365,6 @@ 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(section) = patch.section.clone() {
metadata.section = match section {
Some(section_id) => Some(
state_db
.get_thread_section(&section_id)
.await
.map_err(|err| ThreadStoreError::Internal {
message: format!(
"failed to read section {section_id} for thread {thread_id}: {err}"
),
})?
.ok_or_else(|| ThreadStoreError::InvalidRequest {
message: format!("thread section not found: {section_id}"),
})?,
),
None => None,
};
}
if let Some(git_info) = patch.git_info {
let existing_git_info = git_info_from_parts(
metadata.git_sha.clone(),
@@ -424,21 +382,6 @@ async fn apply_metadata_update(
.map_err(|err| ThreadStoreError::Internal {
message: format!("failed to update thread metadata for {thread_id}: {err}"),
})?;
if let Some(section) = patch.section {
let updated = state_db
.update_thread_section(thread_id, section.as_deref())
.await
.map_err(|err| ThreadStoreError::Internal {
message: format!("failed to update section for thread {thread_id}: {err}"),
})?;
if !updated {
return Err(ThreadStoreError::Internal {
message: format!(
"thread metadata unavailable before section update: {thread_id}"
),
});
}
}
if let Some(name) = patch.name.as_ref() {
let history_mode = history_mode.ok_or_else(|| ThreadStoreError::Internal {
message: format!(
@@ -603,9 +546,8 @@ fn sqlite_write_failure_should_block(patch: &ThreadMetadataPatch) -> bool {
// transcript-derived metadata, thread names, and memory-mode indexing were log-only. Keep that
// 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. User-selected section state is
// SQLite-only, so losing its write must also fail the explicit metadata update.
patch.section.is_some() || (patch.git_info.is_some() && !has_observed_metadata_facts(patch))
// the existing SQLite value to preserve unspecified fields.
patch.git_info.is_some() && !has_observed_metadata_facts(patch)
}
fn sqlite_write_error_is_best_effort(err: &ThreadStoreError) -> bool {
@@ -865,6 +807,7 @@ mod tests {
use super::*;
use crate::GitInfoPatch;
use crate::ListThreadsParams;
use crate::MoveThreadToSectionParams;
use crate::ResumeThreadParams;
use crate::SortDirection;
use crate::ThreadMetadataPatch;
@@ -905,7 +848,7 @@ mod tests {
}
#[tokio::test]
async fn section_only_metadata_updates_persist_in_sqlite_without_changing_the_rollout() {
async fn section_moves_persist_in_sqlite_without_changing_the_rollout() {
let home = TempDir::new().expect("temp dir");
let config = test_config(home.path());
let uuid = Uuid::from_u128(320);
@@ -921,18 +864,34 @@ mod tests {
.expect("state db should initialize");
let store = LocalThreadStore::new(config, Some(runtime.clone()));
let pinned = store
.update_thread_metadata(UpdateThreadMetadataParams {
codex_rollout::state_db::reconcile_rollout(
Some(runtime.as_ref()),
rollout_path.as_path(),
"test-provider",
/*builder*/ None,
&[],
/*archived_only*/ None,
/*new_thread_memory_mode*/ None,
)
.await;
store
.move_thread_to_section(MoveThreadToSectionParams {
thread_id,
patch: ThreadMetadataPatch {
section: Some(Some(codex_state::PINNED_THREAD_SECTION_ID.to_string())),
..Default::default()
},
include_archived: false,
section: Some(codex_state::PINNED_THREAD_SECTION_ID.to_string()),
before_thread_id: None,
})
.await
.expect("pin thread");
let pinned = store
.read_thread(ReadThreadParams {
thread_id,
include_archived: false,
include_history: false,
})
.await
.expect("read pinned thread");
assert_eq!(
pinned.section,
Some(codex_state::ThreadSection {
@@ -999,18 +958,24 @@ mod tests {
original_rollout
);
let unpinned = store
.update_thread_metadata(UpdateThreadMetadataParams {
store
.move_thread_to_section(MoveThreadToSectionParams {
thread_id,
patch: ThreadMetadataPatch {
section: Some(None),
..Default::default()
},
include_archived: false,
section: None,
before_thread_id: None,
})
.await
.expect("clear thread section");
let unpinned = store
.read_thread(ReadThreadParams {
thread_id,
include_archived: false,
include_history: false,
})
.await
.expect("read unpinned thread");
assert_eq!(unpinned.section, None);
assert_eq!(
runtime
@@ -1829,14 +1794,6 @@ mod tests {
}));
}
#[test]
fn sqlite_failures_block_for_explicit_section_updates() {
assert!(sqlite_write_failure_should_block(&ThreadMetadataPatch {
section: Some(None),
..Default::default()
}));
}
#[tokio::test]
async fn metadata_patch_applies_title_over_existing_name() {
let home = TempDir::new().expect("temp dir");