mirror of
https://github.com/openai/codex.git
synced 2026-11-24 23:20:47 +00:00
Wait for pending history writes before Guardian v3 reviews (#63675)
## Why A root-thread instruction can be visible in memory while its history append is still pending. Guardian v3 reads persisted history, so a review can miss a newer instruction that revokes permission for an action. ## What changed Track pending appends for review-relevant root history and wait for confirmation before capturing the evidence cutoff for an initial review or retry. Confirm the append when the thread store accepts the history batch, before updating thread metadata. If an append goes unconfirmed, fail review preparation and prompt the user to reload the root thread and resend any missing instructions. ## Testing Add regression coverage for initial and retry cutoffs waiting on a pending revocation, failed appends of forwarded instructions, and history remaining readable after an acknowledged append followed by a metadata failure. GitOrigin-RevId: b83ca178ca7b58dbc23b1f064b402df30f9689e1
This commit is contained in:
@@ -235,12 +235,24 @@ impl LiveThread {
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn append_items(&self, raw_items: &[RolloutItem]) -> ThreadStoreResult<()> {
|
||||
self.append_items_with_ack(raw_items, || {}).await
|
||||
}
|
||||
|
||||
/// Calls `on_append` synchronously once the store accepts a nonempty batch of
|
||||
/// persistable items. A later metadata error or cancellation does not undo that
|
||||
/// acknowledgment. Metadata errors still propagate to the caller.
|
||||
#[tracing::instrument(
|
||||
name = "append_items",
|
||||
level = "trace",
|
||||
skip_all,
|
||||
fields(item_count = raw_items.len())
|
||||
)]
|
||||
pub async fn append_items(&self, raw_items: &[RolloutItem]) -> ThreadStoreResult<()> {
|
||||
pub async fn append_items_with_ack(
|
||||
&self,
|
||||
raw_items: &[RolloutItem],
|
||||
on_append: impl FnOnce() + Send,
|
||||
) -> ThreadStoreResult<()> {
|
||||
let items = self
|
||||
.persist_appended_items(raw_items)
|
||||
.await?
|
||||
@@ -248,6 +260,7 @@ impl LiveThread {
|
||||
if items.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
on_append();
|
||||
if self.should_defer_rollout_metadata().await {
|
||||
let _ = self
|
||||
.metadata_sync
|
||||
|
||||
@@ -3,7 +3,11 @@ use std::time::Duration;
|
||||
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::models::BaseInstructions;
|
||||
use codex_protocol::models::ResponseInputItem;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::models::input_text;
|
||||
use codex_protocol::protocol::EventMsg;
|
||||
use codex_protocol::protocol::HistoryPosition;
|
||||
use codex_protocol::protocol::SessionSource;
|
||||
use codex_protocol::protocol::ThreadHistoryMode;
|
||||
use codex_protocol::protocol::ThreadMemoryMode;
|
||||
@@ -16,11 +20,55 @@ use tempfile::TempDir;
|
||||
use super::*;
|
||||
use crate::CreateThreadParams;
|
||||
use crate::LiveThread;
|
||||
use crate::OriginalHistoryRequest;
|
||||
use crate::PersistContext;
|
||||
use crate::ThreadPersistenceMetadata;
|
||||
use crate::UpdateThreadMetadataParams;
|
||||
use crate::local::test_support::test_config;
|
||||
|
||||
#[tokio::test]
|
||||
async fn acknowledged_history_is_readable_after_metadata_failure() {
|
||||
let (_home, store, runtime) = store_with_runtime().await;
|
||||
let thread_id = ThreadId::new();
|
||||
let mut params = create_thread_params(thread_id);
|
||||
params.history_mode = ThreadHistoryMode::Paginated;
|
||||
let live = LiveThread::create(store.clone(), params)
|
||||
.await
|
||||
.expect("create live thread");
|
||||
// Keep the canonical writer open while SQLite metadata updates fail.
|
||||
runtime.close().await;
|
||||
|
||||
let message = ResponseItem::from(ResponseInputItem::Message {
|
||||
role: "user".to_string(),
|
||||
content: vec![input_text("Stop the release.")],
|
||||
phase: None,
|
||||
});
|
||||
let items = [RolloutItem::ResponseItem(message.clone().into())];
|
||||
let mut acknowledged = false;
|
||||
let error = live
|
||||
.append_items_with_ack(&items, || acknowledged = true)
|
||||
.await
|
||||
.expect_err("surface the metadata failure");
|
||||
assert!(error.to_string().contains("failed to read thread metadata"));
|
||||
assert!(acknowledged);
|
||||
store.flush_thread(thread_id).await.expect("flush history");
|
||||
let start = HistoryPosition {
|
||||
thread_id,
|
||||
end_ordinal_exclusive: 0,
|
||||
end_byte_offset: 0,
|
||||
};
|
||||
let history = store
|
||||
.read_original_history(thread_id, OriginalHistoryRequest::After(start))
|
||||
.await
|
||||
.expect("read committed history");
|
||||
let RolloutItem::ResponseItem(saved) = &history.items.last().expect("saved instruction").item
|
||||
else {
|
||||
panic!("expected appended user message");
|
||||
};
|
||||
assert_eq!(saved.item, message);
|
||||
live.discard().await.expect("remove writer");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn paginated_metadata_write_failure_stays_pending_until_retry() {
|
||||
let (_home, store, runtime) = store_with_runtime().await;
|
||||
|
||||
Reference in New Issue
Block a user