[codex] Restore thread recency with compatible migration history (#28671)

## Summary

- Revert #28655, restoring the thread `recencyAt` behavior introduced by
#27910.
- Move `threads_recency_at` to migration 0039 so it no longer collides
with `external_agent_config_imports` at version 0038.
- Repair databases that already applied the recency migration as version
38 by moving the matching migration-history row to version 39 before
SQLx validation. The current version-38 migration can then apply
normally.

## Validation

- `just test -p codex-state
migrations::tests::repairs_recency_migration_that_was_applied_as_version_38`
- `just test -p codex-state -p codex-rollout -p codex-thread-store -p
codex-app-server-protocol -p codex-tui`: 3,439 passed; six TUI tests
could not open the machine's existing read-only incident database at
`~/.codex/sqlite/state_5.sqlite`.
- `just fix -p codex-state`
- `just fmt`
- Verified that state migration versions are unique.
This commit is contained in:
Jeremy Rose authored and GitHub committed 2026-06-17 18:52:18 +00:00
1 parent d9e0551564
commit 7dc7096ae1
67 files changed
+1274 -100

No files matched your search

@@ -204,6 +204,7 @@ async fn apply_metadata_update(
.map_err(|err| ThreadStoreError::Internal {
message: format!("failed to read thread metadata for {thread_id}: {err}"),
})?;
let advance_recency_at = patch.advance_recency_at;
if existing.is_none() && rollout_path.is_none() {
let resolved = resolve_rollout_path(store, thread_id, include_archived).await?;
rollout_path_archived = resolved.archived;
@@ -260,6 +261,11 @@ async fn apply_metadata_update(
if let Some(updated_at) = patch.updated_at {
metadata.updated_at = updated_at;
}
if existing.is_none()
&& let Some(recency_at) = advance_recency_at
{
metadata.recency_at = recency_at;
}
if let Some(source) = patch.source {
metadata.source = enum_to_string(&source);
}
@@ -310,6 +316,18 @@ async fn apply_metadata_update(
.map_err(|err| ThreadStoreError::Internal {
message: format!("failed to update thread metadata for {thread_id}: {err}"),
})?;
if existing.is_some()
&& let Some(recency_at) = advance_recency_at
{
state_db
.touch_thread_recency_at(thread_id, recency_at)
.await
.map_err(|err| ThreadStoreError::Internal {
message: format!(
"failed to advance thread recency_at for {thread_id}: {err}"
),
})?;
}
if let Some(memory_mode) = patch.memory_mode {
state_db
.set_thread_memory_mode(thread_id, memory_mode_as_str(memory_mode))