Support incremental replay of updated thread items (#35013)

## Why

Persisted item snapshots can be updated after their initial projection while retaining their creation order. Callers need a way to read those updates incrementally without replaying every item.

## What changed

- Track the rollout ordinal of each item's latest snapshot separately from its creation ordinal.
- Let thread-store callers filter items after an exclusive update watermark and sort pages by either creation or update ordinal.
- Keep existing app-server reads creation-ordered, reject incremental reads across forked lineages, and preserve compatibility with older writers.

## Testing

- Cover update-watermark filtering, pagination, sort directions, per-turn reads, cursor validation, forked lineages, migration compatibility, and refreshed item snapshots.

GitOrigin-RevId: d57de849abec27f788e9c384dbff073a0aca1053
This commit is contained in:
richardopenai authored and copyberry committed 2026-07-23 21:17:45 +00:00
1 parent 5b402270f9
commit b834702b27
13 files changed
+436 -37

No files matched your search

+69
View File
@@ -6,6 +6,7 @@ use sqlx::migrate::Migrator;
use std::borrow::Cow;
use super::STATE_MIGRATOR;
use super::THREAD_HISTORY_MIGRATOR;
use super::repair_legacy_recency_migration_version;
fn migrator_through(version: i64) -> Migrator {
@@ -100,6 +101,74 @@ INSERT INTO threads (
pool.close().await;
}
#[tokio::test]
async fn thread_item_update_ordinals_allow_older_writers() {
let sqlite_home = crate::runtime::test_support::unique_temp_dir();
tokio::fs::create_dir_all(&sqlite_home)
.await
.expect("sqlite home should be created");
let _cleanup = scopeguard::guard(sqlite_home.clone(), |sqlite_home| {
let _ = std::fs::remove_dir_all(sqlite_home);
});
let sqlite = crate::SqliteConfig::new_for_testing(sqlite_home.as_path().abs());
let pre_update_ordinal_migrator = Migrator {
migrations: Cow::Owned(
THREAD_HISTORY_MIGRATOR
.migrations
.iter()
.filter(|migration| migration.version < 4)
.cloned()
.collect(),
),
ignore_missing: THREAD_HISTORY_MIGRATOR.ignore_missing,
locking: THREAD_HISTORY_MIGRATOR.locking,
table_name: THREAD_HISTORY_MIGRATOR.table_name.clone(),
create_schemas: THREAD_HISTORY_MIGRATOR.create_schemas.clone(),
no_tx: THREAD_HISTORY_MIGRATOR.no_tx,
};
let pool = sqlite
.open_thread_history_db(
&pre_update_ordinal_migrator,
/*telemetry_override*/ None,
)
.await
.expect("pre-update-ordinal migrations should apply");
sqlx::query(
r#"
INSERT INTO thread_items (thread_id, turn_id, item_id, rollout_ordinal, created_at_ms, item_type, item_json) VALUES
('thread-1', 'turn-1', 'existing-item-1', 11, 1_100, 'userMessage', '{}'),
('thread-1', 'turn-1', 'existing-item-2', 12, 1_200, 'userMessage', '{}')
"#,
)
.execute(&pool)
.await
.expect("pre-migration items should be inserted");
THREAD_HISTORY_MIGRATOR
.run(&pool)
.await
.expect("update-ordinal migration should apply");
sqlx::query(
r#"
INSERT INTO thread_items (thread_id, turn_id, item_id, rollout_ordinal, created_at_ms, item_type, item_json) VALUES
('thread-1', 'turn-1', 'old-writer-item-1', 13, 1_300, 'userMessage', '{}'),
('thread-1', 'turn-1', 'old-writer-item-2', 14, 1_400, 'userMessage', '{}')
"#,
)
.execute(&pool)
.await
.expect("older writers should be able to append multiple items after migration");
let ordinals = sqlx::query_as::<_, (i64, i64)>(
"SELECT rollout_ordinal, updated_at_ordinal FROM thread_items WHERE thread_id = ? ORDER BY rollout_ordinal",
)
.bind("thread-1")
.fetch_all(&pool)
.await
.expect("old-writer items should load");
assert_eq!(ordinals, vec![(11, 11), (12, 12), (13, 0), (14, 0)]);
pool.close().await;
}
#[tokio::test]
async fn agent_job_tables_are_dropped_when_upgrading() {
let sqlite_home = crate::runtime::test_support::unique_temp_dir();