Add legacy rollout migration to paginated history (#37175)

## What changed

- Add `LocalThreadStore::migrate_rollouts` with dry-run and apply modes,
  optional thread selection, throughput limiting, and per-rollout outcomes.
- Canonicalize legacy JSONL records into paginated history while preserving
  model-visible conversation context, including compressed rollouts and copied
  fork history.
- Publish replacements atomically and journal migrations so interrupted SQLite
  projections can be recovered. Coordinate migration with rollout compression
  and skip rollouts with active writers.
- Make paginated history a one-way SQLite promotion so stale metadata cannot
  downgrade a migrated thread.

## Testing

- Cover legacy record normalization, dry runs, idempotency, malformed input,
  compressed rollouts, active writers, interrupted migration recovery, and a
  cold app-server resume after migration.

GitOrigin-RevId: b9991b659f28ebb52da39ce62e8b4e0bae2ba2bb
This commit is contained in:
Owen Lin
2026-08-05 22:57:27 +00:00
committed by copyberry
parent e3465b48ad
commit 6bb6e9045f
18 changed files with 3115 additions and 5 deletions
+38 -4
View File
@@ -55,6 +55,14 @@ WHERE threads.id = ?
.transpose()
}
/// Permanently promote a thread to paginated history without changing metadata or recency.
pub async fn mark_thread_paginated(&self, thread_id: ThreadId) -> anyhow::Result<bool> {
let result = sqlx::query("UPDATE threads SET history_mode = 'paginated' WHERE id = ?")
.bind(thread_id.to_string())
.execute(self.pool.as_ref())
.await?;
Ok(result.rows_affected() > 0)
}
pub async fn get_thread_memory_mode(&self, id: ThreadId) -> anyhow::Result<Option<String>> {
let row = sqlx::query("SELECT memory_mode FROM threads WHERE id = ?")
.bind(id.to_string())
@@ -873,7 +881,11 @@ ON CONFLICT(id) DO UPDATE SET
updated_at_ms = excluded.updated_at_ms,
recency_at_ms = threads.recency_at_ms,
source = excluded.source,
history_mode = excluded.history_mode,
-- Paginated history is a one-way promotion; stale legacy metadata must not downgrade it.
history_mode = CASE
WHEN threads.history_mode = 'paginated' THEN threads.history_mode
ELSE excluded.history_mode
END,
thread_source = excluded.thread_source,
agent_nickname = excluded.agent_nickname,
agent_role = excluded.agent_role,
@@ -1512,7 +1524,7 @@ mod tests {
}
#[tokio::test]
async fn thread_metadata_round_trips_history_mode() {
async fn thread_metadata_history_mode_does_not_downgrade() {
let codex_home = unique_temp_dir();
let runtime = StateRuntime::init(
crate::SqliteConfig::new_for_testing(codex_home.as_path().abs()),
@@ -1522,20 +1534,42 @@ mod tests {
.expect("state db should initialize");
let thread_id =
ThreadId::from_string("00000000-0000-0000-0000-000000000124").expect("valid thread id");
let mut metadata = test_thread_metadata(&codex_home, thread_id, codex_home.clone());
metadata.history_mode = ThreadHistoryMode::Paginated;
let metadata = test_thread_metadata(&codex_home, thread_id, codex_home.clone());
runtime
.upsert_thread(&metadata)
.await
.expect("upsert should succeed");
assert!(
runtime
.mark_thread_paginated(thread_id)
.await
.expect("mark paginated history")
);
let metadata = runtime
.get_thread(thread_id)
.await
.expect("thread should load")
.expect("thread should exist");
assert_eq!(metadata.history_mode, ThreadHistoryMode::Paginated);
let mut stale_metadata = metadata;
stale_metadata.history_mode = ThreadHistoryMode::Legacy;
runtime
.upsert_thread(&stale_metadata)
.await
.expect("upsert stale legacy metadata");
assert_eq!(
runtime
.get_thread(thread_id)
.await
.expect("read migrated thread")
.expect("thread should exist")
.history_mode,
ThreadHistoryMode::Paginated
);
}
#[tokio::test]