Revert thread recencyAt for sidebar ordering (#28655)

## Why

Revert #27910 to remove the newly introduced thread `recencyAt`
persistence and API behavior from `main`.

## What changed

This reverts commit `fac3158c2a783095768076489815f361fa9b0db4`,
including the state migration, thread-store propagation, app-server API
surface, generated schemas, and related tests.

## Validation

Not run before opening; relying on CI for the initial fast signal.
This commit is contained in:
pakrym-oai authored and GitHub committed 2026-06-16 21:39:30 -07:00
1 parent 0a3ad4c4ba
commit cb15c64760
67 files changed
+95 -1150

No files matched your search

-2
View File
@@ -175,7 +175,6 @@ SELECT
threads.rollout_path,
threads.created_at_ms AS created_at,
threads.updated_at_ms AS updated_at,
threads.recency_at_ms AS recency_at,
threads.source,
threads.thread_source,
threads.agent_path,
@@ -546,7 +545,6 @@ SELECT
threads.rollout_path,
threads.created_at_ms AS created_at,
threads.updated_at_ms AS updated_at,
threads.recency_at_ms AS recency_at,
threads.source,
threads.thread_source,
threads.agent_nickname,
@@ -47,7 +47,6 @@ pub(super) fn test_thread_metadata(
rollout_path: codex_home.join(format!("rollout-{thread_id}.jsonl")),
created_at: now,
updated_at: now,
recency_at: now,
source: "cli".to_string(),
thread_source: None,
agent_nickname: None,
+32 -305
View File
@@ -1,7 +1,6 @@
use super::*;
use crate::SortDirection;
use codex_protocol::protocol::SessionSource;
use std::sync::atomic::AtomicI64;
use std::sync::atomic::Ordering;
impl StateRuntime {
@@ -13,7 +12,6 @@ SELECT
threads.rollout_path,
threads.created_at_ms AS created_at,
threads.updated_at_ms AS updated_at,
threads.recency_at_ms AS recency_at,
threads.source,
threads.thread_source,
threads.agent_nickname,
@@ -500,7 +498,6 @@ ON CONFLICT(child_thread_id) DO NOTHING
metadata: &crate::ThreadMetadata,
) -> anyhow::Result<bool> {
let updated_at = self.allocate_thread_updated_at(metadata.updated_at)?;
let recency_at = self.allocate_thread_recency_at(metadata.recency_at)?;
let preview = metadata_preview(metadata);
let result = sqlx::query(
r#"
@@ -509,10 +506,8 @@ INSERT INTO threads (
rollout_path,
created_at,
updated_at,
recency_at,
created_at_ms,
updated_at_ms,
recency_at_ms,
source,
thread_source,
agent_nickname,
@@ -535,7 +530,7 @@ INSERT INTO threads (
git_branch,
git_origin_url,
memory_mode
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO NOTHING
"#,
)
@@ -543,10 +538,8 @@ ON CONFLICT(id) DO NOTHING
.bind(metadata.rollout_path.display().to_string())
.bind(datetime_to_epoch_seconds(metadata.created_at))
.bind(datetime_to_epoch_seconds(updated_at))
.bind(datetime_to_epoch_seconds(recency_at))
.bind(datetime_to_epoch_millis(metadata.created_at))
.bind(datetime_to_epoch_millis(updated_at))
.bind(datetime_to_epoch_millis(recency_at))
.bind(metadata.source.as_str())
.bind(
metadata
@@ -628,32 +621,6 @@ ON CONFLICT(id) DO NOTHING
Ok(result.rows_affected() > 0)
}
pub async fn touch_thread_recency_at(
&self,
thread_id: ThreadId,
recency_at: DateTime<Utc>,
) -> anyhow::Result<bool> {
let recency_at = self.allocate_thread_recency_at(recency_at)?;
let recency_at_seconds = datetime_to_epoch_seconds(recency_at);
let recency_at_millis = datetime_to_epoch_millis(recency_at);
let result = sqlx::query(
r#"
UPDATE threads
SET
recency_at = MAX(?, MAX(?, recency_at_ms + 1) / 1000),
recency_at_ms = MAX(?, recency_at_ms + 1)
WHERE id = ?
"#,
)
.bind(recency_at_seconds)
.bind(recency_at_millis)
.bind(recency_at_millis)
.bind(thread_id.to_string())
.execute(self.pool.as_ref())
.await?;
Ok(result.rows_affected() > 0)
}
/// Allocate a persisted `updated_at` value for thread-list cursor ordering.
///
/// We keep a process-local high-water mark so hot rollout writes can get unique,
@@ -664,56 +631,42 @@ WHERE id = ?
&self,
updated_at: DateTime<Utc>,
) -> anyhow::Result<DateTime<Utc>> {
allocate_thread_timestamp(self.thread_updated_at_millis.as_ref(), updated_at)
}
let candidate = datetime_to_epoch_millis(updated_at);
let allocated = loop {
let current = self.thread_updated_at_millis.load(Ordering::Relaxed);
fn allocate_thread_recency_at(
&self,
recency_at: DateTime<Utc>,
) -> anyhow::Result<DateTime<Utc>> {
allocate_thread_timestamp(self.thread_recency_at_millis.as_ref(), recency_at)
}
}
// New wall-clock time: advance the process-local high-water mark and use it as-is.
if candidate > current {
if self
.thread_updated_at_millis
.compare_exchange(current, candidate, Ordering::Relaxed, Ordering::Relaxed)
.is_ok()
{
break candidate;
}
continue;
}
fn allocate_thread_timestamp(
high_water_mark: &AtomicI64,
timestamp: DateTime<Utc>,
) -> anyhow::Result<DateTime<Utc>> {
let candidate = datetime_to_epoch_millis(timestamp);
let allocated = loop {
let current = high_water_mark.load(Ordering::Relaxed);
// New wall-clock time: advance the process-local high-water mark and use it as-is.
if candidate > current {
if high_water_mark
.compare_exchange(current, candidate, Ordering::Relaxed, Ordering::Relaxed)
.is_ok()
{
// Older timestamps come from backfill/repair paths that preserve rollout mtimes.
// Do not drag historical rows forward just because this process has seen newer writes.
if candidate.saturating_add(1000) <= current {
break candidate;
}
continue;
}
// Older timestamps come from backfill/repair paths that preserve rollout mtimes.
// Do not drag historical rows forward just because this process has seen newer writes.
if candidate.saturating_add(1000) <= current {
break candidate;
}
// Same hot one-second bucket as the current high-water mark. Allocate the next
// millisecond so updated_at remains unique and cursor-orderable inside the process.
let bumped = current.saturating_add(1);
if self
.thread_updated_at_millis
.compare_exchange(current, bumped, Ordering::Relaxed, Ordering::Relaxed)
.is_ok()
{
break bumped;
}
};
epoch_millis_to_datetime(allocated)
}
// Same hot one-second bucket as the current high-water mark. Allocate the next
// millisecond so the timestamp remains unique and cursor-orderable inside the process.
let bumped = current.saturating_add(1);
if high_water_mark
.compare_exchange(current, bumped, Ordering::Relaxed, Ordering::Relaxed)
.is_ok()
{
break bumped;
}
};
epoch_millis_to_datetime(allocated)
}
impl StateRuntime {
pub async fn update_thread_git_info(
&self,
thread_id: ThreadId,
@@ -749,7 +702,6 @@ WHERE id = ?
creation_memory_mode: Option<&str>,
) -> anyhow::Result<()> {
let updated_at = self.allocate_thread_updated_at(metadata.updated_at)?;
let insert_recency_at = self.allocate_thread_recency_at(metadata.recency_at)?;
let preview = metadata_preview(metadata);
// Backfill/reconcile callers merge existing git info before upserting, but that
// read/modify/write is not atomic. Preserve non-null SQLite git fields here so
@@ -761,10 +713,8 @@ INSERT INTO threads (
rollout_path,
created_at,
updated_at,
recency_at,
created_at_ms,
updated_at_ms,
recency_at_ms,
source,
thread_source,
agent_nickname,
@@ -787,15 +737,13 @@ INSERT INTO threads (
git_branch,
git_origin_url,
memory_mode
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO UPDATE SET
rollout_path = excluded.rollout_path,
created_at = excluded.created_at,
updated_at = excluded.updated_at,
recency_at = threads.recency_at,
created_at_ms = excluded.created_at_ms,
updated_at_ms = excluded.updated_at_ms,
recency_at_ms = threads.recency_at_ms,
source = excluded.source,
thread_source = excluded.thread_source,
agent_nickname = excluded.agent_nickname,
@@ -823,10 +771,8 @@ ON CONFLICT(id) DO UPDATE SET
.bind(metadata.rollout_path.display().to_string())
.bind(datetime_to_epoch_seconds(metadata.created_at))
.bind(datetime_to_epoch_seconds(updated_at))
.bind(datetime_to_epoch_seconds(insert_recency_at))
.bind(datetime_to_epoch_millis(metadata.created_at))
.bind(datetime_to_epoch_millis(updated_at))
.bind(datetime_to_epoch_millis(insert_recency_at))
.bind(metadata.source.as_str())
.bind(
metadata
@@ -1133,7 +1079,6 @@ SELECT
threads.rollout_path,
threads.created_at_ms AS created_at,
threads.updated_at_ms AS updated_at,
threads.recency_at_ms AS recency_at,
threads.source,
threads.thread_source,
threads.agent_nickname,
@@ -1252,7 +1197,6 @@ pub(super) fn push_thread_filters<'a>(
let column = match sort_key {
SortKey::CreatedAt => "threads.created_at_ms",
SortKey::UpdatedAt => "threads.updated_at_ms",
SortKey::RecencyAt => "threads.recency_at_ms",
};
let operator = match sort_direction {
SortDirection::Asc => ">",
@@ -1264,19 +1208,6 @@ pub(super) fn push_thread_filters<'a>(
builder.push(operator);
builder.push(" ");
builder.push_bind(anchor_ts);
if sort_key == SortKey::RecencyAt
&& let Some(anchor_id) = anchor.id
{
builder.push(" OR (");
builder.push(column);
builder.push(" = ");
builder.push_bind(anchor_ts);
builder.push(" AND threads.id ");
builder.push(operator);
builder.push(" ");
builder.push_bind(anchor_id.to_string());
builder.push(")");
}
builder.push(")");
}
}
@@ -1301,7 +1232,6 @@ pub(super) fn push_thread_order_and_limit(
let order_column = match sort_key {
SortKey::CreatedAt => "threads.created_at_ms",
SortKey::UpdatedAt => "threads.updated_at_ms",
SortKey::RecencyAt => "threads.recency_at_ms",
};
let order_direction = match sort_direction {
SortDirection::Asc => "ASC",
@@ -1317,10 +1247,6 @@ pub(super) fn push_thread_order_and_limit(
builder.push(order_column);
builder.push(" ");
builder.push(order_direction);
if sort_key == SortKey::RecencyAt {
builder.push(", threads.id ");
builder.push(order_direction);
}
builder.push(" LIMIT ");
builder.push_bind(limit as i64);
}
@@ -1595,7 +1521,6 @@ mod tests {
let anchor = Anchor {
ts: older_updated_at,
id: None,
};
let model_providers = ["test-provider".to_string()];
let page = runtime
@@ -1622,7 +1547,6 @@ mod tests {
Some(Anchor {
ts: DateTime::<Utc>::from_timestamp_millis(1_700_000_200_000)
.expect("valid timestamp"),
id: None,
})
);
@@ -1707,7 +1631,6 @@ mod tests {
Some(Anchor {
ts: DateTime::<Utc>::from_timestamp_millis(1_700_000_300_000)
.expect("valid timestamp"),
id: None,
})
);
@@ -1770,7 +1693,6 @@ mod tests {
];
let anchor = Anchor {
ts: DateTime::<Utc>::from_timestamp(1_700_000_000, 0).expect("valid timestamp"),
id: None,
};
for (sort_key, visible_index, cwd_index) in [
(
@@ -1783,11 +1705,6 @@ mod tests {
"idx_threads_visible_updated_at_ms",
"idx_threads_archived_cwd_updated_at_ms",
),
(
SortKey::RecencyAt,
"idx_threads_visible_recency_at_ms",
"idx_threads_archived_cwd_recency_at_ms",
),
] {
for (cwd_filters, anchor, expected_index, expect_temp_sort) in [
(None, None, visible_index, false),
@@ -2362,186 +2279,6 @@ mod tests {
assert_eq!(persisted.preview.as_deref(), Some("first-user-message"));
}
#[tokio::test]
async fn touch_thread_recency_at_is_monotonic_and_survives_stale_upsert() {
let codex_home = unique_temp_dir();
let runtime = StateRuntime::init(codex_home.clone(), "test-provider".to_string())
.await
.expect("state db should initialize");
let thread_id =
ThreadId::from_string("00000000-0000-0000-0000-000000000792").expect("valid thread id");
let mut metadata = test_thread_metadata(&codex_home, thread_id, codex_home.clone());
let original_recency_at = metadata.recency_at;
runtime
.upsert_thread(&metadata)
.await
.expect("initial upsert should succeed");
let touched_at =
DateTime::<Utc>::from_timestamp_millis(1_700_001_111_123).expect("timestamp");
assert!(
runtime
.touch_thread_recency_at(thread_id, touched_at)
.await
.expect("touch should succeed")
);
metadata.updated_at =
DateTime::<Utc>::from_timestamp_millis(1_700_001_222_456).expect("timestamp");
metadata.title = "updated metadata".to_string();
assert_eq!(metadata.recency_at, original_recency_at);
runtime
.upsert_thread(&metadata)
.await
.expect("stale metadata upsert should succeed");
let persisted = runtime
.get_thread(thread_id)
.await
.expect("thread should load")
.expect("thread should exist");
assert_eq!(persisted.recency_at, touched_at);
assert_eq!(persisted.updated_at, metadata.updated_at);
assert_eq!(persisted.title, "updated metadata");
assert!(
runtime
.touch_thread_recency_at(thread_id, original_recency_at)
.await
.expect("older touch should succeed")
);
let persisted = runtime
.get_thread(thread_id)
.await
.expect("thread should load")
.expect("thread should exist");
assert_eq!(
datetime_to_epoch_millis(persisted.recency_at),
datetime_to_epoch_millis(touched_at) + 1
);
}
#[tokio::test]
async fn list_threads_orders_and_pages_by_recency_at() {
let codex_home = unique_temp_dir();
let runtime = StateRuntime::init(codex_home.clone(), "test-provider".to_string())
.await
.expect("state db should initialize");
let first_id =
ThreadId::from_string("00000000-0000-0000-0000-000000000793").expect("valid thread id");
let second_id =
ThreadId::from_string("00000000-0000-0000-0000-000000000794").expect("valid thread id");
let third_id =
ThreadId::from_string("00000000-0000-0000-0000-000000000795").expect("valid thread id");
let recency_at =
DateTime::<Utc>::from_timestamp_millis(1_700_002_000_456).expect("timestamp");
for thread_id in [first_id, second_id, third_id] {
let mut metadata = test_thread_metadata(&codex_home, thread_id, codex_home.clone());
metadata.recency_at = recency_at;
runtime
.upsert_thread(&metadata)
.await
.expect("thread insert should succeed");
}
sqlx::query("UPDATE threads SET recency_at = ?, recency_at_ms = ?")
.bind(datetime_to_epoch_seconds(recency_at))
.bind(datetime_to_epoch_millis(recency_at))
.execute(runtime.pool.as_ref())
.await
.expect("recency timestamps should match");
let first_page = runtime
.list_threads(
/*page_size*/ 1,
ThreadFilterOptions {
archived_only: false,
allowed_sources: &[],
model_providers: None,
cwd_filters: None,
anchor: None,
sort_key: SortKey::RecencyAt,
sort_direction: SortDirection::Desc,
search_term: None,
},
)
.await
.expect("list should succeed");
assert_eq!(
first_page
.items
.iter()
.map(|item| item.id)
.collect::<Vec<_>>(),
vec![third_id]
);
assert_eq!(
first_page.next_anchor,
Some(Anchor {
ts: recency_at,
id: Some(third_id),
})
);
let second_page = runtime
.list_threads(
/*page_size*/ 1,
ThreadFilterOptions {
archived_only: false,
allowed_sources: &[],
model_providers: None,
cwd_filters: None,
anchor: first_page.next_anchor.as_ref(),
sort_key: SortKey::RecencyAt,
sort_direction: SortDirection::Desc,
search_term: None,
},
)
.await
.expect("second list should succeed");
assert_eq!(
second_page
.items
.iter()
.map(|item| item.id)
.collect::<Vec<_>>(),
vec![second_id]
);
assert_eq!(
second_page.next_anchor,
Some(Anchor {
ts: recency_at,
id: Some(second_id),
})
);
let third_page = runtime
.list_threads(
/*page_size*/ 1,
ThreadFilterOptions {
archived_only: false,
allowed_sources: &[],
model_providers: None,
cwd_filters: None,
anchor: second_page.next_anchor.as_ref(),
sort_key: SortKey::RecencyAt,
sort_direction: SortDirection::Desc,
search_term: None,
},
)
.await
.expect("third list should succeed");
assert_eq!(
third_page
.items
.iter()
.map(|item| item.id)
.collect::<Vec<_>>(),
vec![first_id]
);
assert_eq!(third_page.next_anchor, None);
}
#[tokio::test]
async fn thread_updated_at_uses_unique_epoch_millis_and_reads_legacy_seconds() {
let codex_home = unique_temp_dir();
@@ -2558,10 +2295,8 @@ mod tests {
DateTime::<Utc>::from_timestamp_millis(1_700_001_111_123).expect("timestamp millis");
let mut first = test_thread_metadata(&codex_home, first_id, codex_home.clone());
first.updated_at = updated_at;
first.recency_at = updated_at;
let mut second = test_thread_metadata(&codex_home, second_id, codex_home.clone());
second.updated_at = updated_at;
second.recency_at = updated_at;
runtime
.upsert_thread(&first)
@@ -2590,14 +2325,6 @@ mod tests {
datetime_to_epoch_millis(second.updated_at),
1_700_001_111_124
);
assert_eq!(
datetime_to_epoch_millis(first.recency_at),
1_700_001_111_123
);
assert_eq!(
datetime_to_epoch_millis(second.recency_at),
1_700_001_111_124
);
let second_row: (i64, i64, Option<i64>, Option<i64>) = sqlx::query_as(
"SELECT created_at, updated_at, created_at_ms, updated_at_ms FROM threads WHERE id = ?",
)