Make observed thread metadata writes atomic and retryable (#59016)

## Why

A failed memory-mode update could leave other metadata changes applied. Failed observed metadata writes for paginated threads also need to remain pending so persistence can retry them.

## What changed

- Apply observed metadata, memory mode, and optional recency advancement in one SQLite upsert, preserving monotonic recency.
- Require successful SQLite writes for paginated thread metadata when a state runtime is configured, retaining failed writes for retry.
- Use SQLite busy-retry helpers for read-state writes and immediate transactions.

## Testing

Add regression tests that reject SQLite writes to verify pending metadata survives a failed persist and a rejected memory-mode update leaves existing metadata unchanged. Both tests verify a subsequent retry succeeds.

GitOrigin-RevId: 652bf6ebee0197976852aaf9f096b907697fd590
This commit is contained in:
Dmitriy Kovalenko
2026-11-03 00:16:13 +00:00
committed by copyberry
parent 9e77fb4ad5
commit c60cda34a3
4 changed files with 182 additions and 29 deletions
+42 -3
View File
@@ -932,11 +932,41 @@ WHERE id = ?
Ok(result.rows_affected() > 0)
}
/// Apply observed metadata, memory preference, and recency in one commit.
pub async fn upsert_thread_with_observed_metadata(
&self,
metadata: &crate::ThreadMetadata,
memory_mode: Option<&str>,
recency_at: Option<DateTime<Utc>>,
) -> anyhow::Result<()> {
self.upsert_thread_metadata(metadata, memory_mode, memory_mode, recency_at)
.await
}
async fn upsert_thread_with_creation_memory_mode(
&self,
metadata: &crate::ThreadMetadata,
creation_memory_mode: Option<&str>,
) -> anyhow::Result<()> {
self.upsert_thread_metadata(
metadata,
creation_memory_mode,
/*memory_mode*/ None,
/*recency_at*/ None,
)
.await
}
async fn upsert_thread_metadata(
&self,
metadata: &crate::ThreadMetadata,
creation_memory_mode: Option<&str>,
memory_mode: Option<&str>,
recency_at: Option<DateTime<Utc>>,
) -> anyhow::Result<()> {
let recency_at = recency_at
.map(|at| self.allocate_thread_recency_at(at))
.transpose()?;
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);
@@ -994,10 +1024,12 @@ 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,
recency_at = CASE WHEN ? IS NULL THEN threads.recency_at
ELSE MAX(?, MAX(?, threads.recency_at_ms + 1) / 1000) END,
created_at_ms = excluded.created_at_ms,
updated_at_ms = excluded.updated_at_ms,
recency_at_ms = threads.recency_at_ms,
recency_at_ms = CASE WHEN ? IS NULL THEN threads.recency_at_ms
ELSE MAX(?, threads.recency_at_ms + 1) END,
source = excluded.source,
originator = COALESCE(threads.originator, excluded.originator),
creator_user_id = COALESCE(threads.creator_user_id, excluded.creator_user_id),
@@ -1026,7 +1058,8 @@ ON CONFLICT(id) DO UPDATE SET
archived_at = excluded.archived_at,
git_sha = COALESCE(threads.git_sha, excluded.git_sha),
git_branch = COALESCE(threads.git_branch, excluded.git_branch),
git_origin_url = COALESCE(threads.git_origin_url, excluded.git_origin_url)
git_origin_url = COALESCE(threads.git_origin_url, excluded.git_origin_url),
memory_mode = COALESCE(?, threads.memory_mode)
"#,
)
.bind(metadata.id.to_string())
@@ -1080,6 +1113,12 @@ ON CONFLICT(id) DO UPDATE SET
.bind(metadata.project_id.as_deref())
.bind(metadata.daybreak_enabled)
.bind(metadata.attached_thread_parent_id.map(|id| id.to_string()))
.bind(recency_at.map(datetime_to_epoch_seconds))
.bind(recency_at.map(datetime_to_epoch_seconds))
.bind(recency_at.map(datetime_to_epoch_millis))
.bind(recency_at.map(datetime_to_epoch_millis))
.bind(recency_at.map(datetime_to_epoch_millis))
.bind(memory_mode)
.execute_with_busy_retry(self.pool.as_ref())
.await?;
self.insert_thread_spawn_edge_from_source_if_absent(metadata.id, metadata.source.as_str())