mirror of
https://github.com/openai/codex.git
synced 2026-11-25 23:30:49 +00:00
Restart Guardian v3 shadow intake when Auto Review resumes (#63988)
## Why Switching an enrolled thread to user approvals stops Guardian v3 shadow intake. Previously, switching the same thread back to Auto Review left intake stopped for subsequent turns. ## What changed Restart intake in the background with a fresh evidence board after checking that the thread still routes approvals to Auto Review. Start at the end of persisted history so the new board only receives new evidence. Existing child threads can attach to the new board on their next turn. ## Testing Add app-server coverage for routing later turns after Auto Review resumes without replaying earlier turns. Extend the host test to cover stale settings notifications, cancellation of a queued restart, and creation of a fresh board and reviewer. GitOrigin-RevId: 5201d7f7a7e3f2c045c69b9584b036467a5767d7
This commit is contained in:
@@ -121,6 +121,15 @@ impl Host {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum IntakeStart {
|
||||
Initial(
|
||||
codex_protocol::protocol::ThreadHistoryMode,
|
||||
HistoryInitialization,
|
||||
),
|
||||
Restart,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum BoardStorage {
|
||||
File(SqliteConfig),
|
||||
@@ -193,25 +202,32 @@ impl Host {
|
||||
root: ThreadId,
|
||||
tree: &TreeDelivery,
|
||||
thread: &ExtensionData,
|
||||
mode: codex_protocol::protocol::ThreadHistoryMode,
|
||||
initialization: HistoryInitialization,
|
||||
start: IntakeStart,
|
||||
) -> anyhow::Result<()> {
|
||||
let history = self
|
||||
.manager
|
||||
.upgrade()
|
||||
.context("Guardian host stopped")?
|
||||
.thread_store();
|
||||
if matches!(self.storage, BoardStorage::Memory) {
|
||||
anyhow::ensure!(
|
||||
matches!(
|
||||
initialization,
|
||||
HistoryInitialization::New | HistoryInitialization::Cleared
|
||||
),
|
||||
"in-memory Guardian evidence requires a fresh root"
|
||||
);
|
||||
}
|
||||
let cursor =
|
||||
EvidenceCursor::at_thread_ready(history.as_ref(), root, mode, initialization).await?;
|
||||
let cursor = match start {
|
||||
IntakeStart::Initial(mode, initialization) => {
|
||||
if matches!(self.storage, BoardStorage::Memory) {
|
||||
anyhow::ensure!(
|
||||
matches!(
|
||||
initialization,
|
||||
HistoryInitialization::New | HistoryInitialization::Cleared
|
||||
),
|
||||
"in-memory Guardian evidence requires a fresh root"
|
||||
);
|
||||
}
|
||||
EvidenceCursor::at_thread_ready(history.as_ref(), root, mode, initialization)
|
||||
.await?
|
||||
}
|
||||
IntakeStart::Restart => {
|
||||
history.flush_thread(root).await?;
|
||||
EvidenceCursor::from_now(history.as_ref(), root).await?
|
||||
}
|
||||
};
|
||||
let board_host = (self.board_host)(root);
|
||||
let board = match &self.storage {
|
||||
BoardStorage::File(sqlite) => {
|
||||
@@ -414,16 +430,8 @@ impl ThreadLifecycleContributor<Config> for Host {
|
||||
if actor != root {
|
||||
return;
|
||||
}
|
||||
if let Err(error) = self
|
||||
.activate(
|
||||
root,
|
||||
&tree,
|
||||
input.thread_store,
|
||||
input.history_mode,
|
||||
input.history_initialization,
|
||||
)
|
||||
.await
|
||||
{
|
||||
let start = IntakeStart::Initial(input.history_mode, input.history_initialization);
|
||||
if let Err(error) = self.activate(root, &tree, input.thread_store, start).await {
|
||||
tracing::warn!(%root, error = %error, "Guardian intake unavailable");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -603,6 +603,7 @@ async fn shadow_intake_enrolls_only_new_opted_in_roots() -> anyhow::Result<()> {
|
||||
assert_eq!(thread.get::<crate::host::RootWorker>().is_some(), expected);
|
||||
assert!(thread.get::<Arc<dyn AttachedReviewer>>().is_none());
|
||||
}
|
||||
let old_worker = thread.get::<super::RootWorker>().context("first board")?;
|
||||
let old_reviewer = thread.get::<ShadowReviewer>().context("first reviewer")?;
|
||||
let child = ExtensionData::new(ThreadId::new().to_string());
|
||||
let source = SessionSource::SubAgent(SubAgentSource::ThreadSpawn {
|
||||
@@ -638,6 +639,45 @@ async fn shadow_intake_enrolls_only_new_opted_in_roots() -> anyhow::Result<()> {
|
||||
assert!(thread.get::<ShadowReviewer>().is_none());
|
||||
assert!(thread.get::<super::RootWorker>().is_none());
|
||||
|
||||
test.thread_store
|
||||
.persist_thread(root, PersistContext::Standard)
|
||||
.await?;
|
||||
let enrollment = thread
|
||||
.get::<super::shadow::Enrollment>()
|
||||
.context("enrollment")?;
|
||||
let set_reviewer = |reviewer| {
|
||||
test.codex
|
||||
.update_thread_settings(codex_protocol::protocol::ThreadSettingsOverrides {
|
||||
approval_policy: Some(AskForApproval::OnRequest),
|
||||
approvals_reviewer: Some(reviewer),
|
||||
..Default::default()
|
||||
})
|
||||
};
|
||||
// A delayed Auto Review notification must not override the root's live settings.
|
||||
set_reviewer(ApprovalsReviewer::User).await?;
|
||||
settings.on_config_changed(&session, thread, &disabled, &config);
|
||||
assert_eq!(turns.on_turn_start(start()).now_or_never(), Some(()));
|
||||
enrollment.drain().await;
|
||||
assert!(thread.get::<ShadowReviewer>().is_none());
|
||||
assert!(thread.get::<super::RootWorker>().is_none());
|
||||
|
||||
// Keep the root eligible so only cancellation can stop the queued restart.
|
||||
set_reviewer(ApprovalsReviewer::AutoReview).await?;
|
||||
settings.on_config_changed(&session, thread, &disabled, &config);
|
||||
assert_eq!(turns.on_turn_start(start()).now_or_never(), Some(()));
|
||||
settings.on_config_changed(&session, thread, &config, &disabled);
|
||||
enrollment.drain().await;
|
||||
assert!(thread.get::<ShadowReviewer>().is_none());
|
||||
assert!(thread.get::<super::RootWorker>().is_none());
|
||||
|
||||
settings.on_config_changed(&session, thread, &disabled, &config);
|
||||
assert_eq!(turns.on_turn_start(start()).now_or_never(), Some(()));
|
||||
enrollment.drain().await;
|
||||
let new_worker = thread.get::<super::RootWorker>().context("fresh board")?;
|
||||
let new_reviewer = thread.get::<ShadowReviewer>().context("fresh reviewer")?;
|
||||
assert!(!Arc::ptr_eq(&old_worker.reader, &new_worker.reader));
|
||||
assert!(!Arc::ptr_eq(&old_reviewer.reviewer, &new_reviewer.reviewer));
|
||||
assert!(!new_reviewer.cancellation.is_cancelled());
|
||||
lifecycle
|
||||
.on_thread_stop(ThreadStopInput {
|
||||
thread_store: thread,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
//! Shadow intake starts off the live path and stops when the root leaves Auto Review.
|
||||
//! A root keeps its enrollment across settings changes, but each stretch of
|
||||
//! Auto Review owns a separate board, housekeeper and set of reviewers.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
@@ -10,12 +11,14 @@ use codex_core::config::Config;
|
||||
use codex_core::guardian_review::ShadowReviewer;
|
||||
use codex_extension_api::ConfigContributor;
|
||||
use codex_extension_api::ExtensionData;
|
||||
use codex_guardian_reviewer::routes_approval_policy_to_guardian;
|
||||
use codex_protocol::AgentPath;
|
||||
use codex_protocol::ThreadId;
|
||||
use tokio_util::task::TaskTracker;
|
||||
|
||||
use super::ActorReviewer;
|
||||
use super::Host;
|
||||
use super::IntakeStart;
|
||||
use super::RootWorker;
|
||||
use super::TreeDelivery;
|
||||
|
||||
@@ -25,21 +28,30 @@ pub(super) struct Actor {
|
||||
pub(super) path: AgentPath,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
enum State {
|
||||
// Covers startup and the published worker. Both share one cancellation token.
|
||||
Running(Arc<TreeDelivery>),
|
||||
Off,
|
||||
#[default]
|
||||
PendingStart,
|
||||
PendingStart(IntakeStart),
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct Enrollment {
|
||||
state: Mutex<State>,
|
||||
tasks: TaskTracker,
|
||||
}
|
||||
|
||||
impl Default for Enrollment {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
state: Mutex::new(State::PendingStart(IntakeStart::Initial(
|
||||
codex_protocol::protocol::ThreadHistoryMode::Paginated,
|
||||
codex_history::HistoryInitialization::New,
|
||||
))),
|
||||
tasks: TaskTracker::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Enrollment {
|
||||
pub(super) fn stop(&self, thread: &ExtensionData) {
|
||||
let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
@@ -85,6 +97,14 @@ impl ConfigContributor<Config> for Host {
|
||||
};
|
||||
if !self.enabled(config) {
|
||||
enrollment.stop(thread);
|
||||
} else {
|
||||
let mut state = enrollment
|
||||
.state
|
||||
.lock()
|
||||
.unwrap_or_else(PoisonError::into_inner);
|
||||
if matches!(*state, State::Off) {
|
||||
*state = State::PendingStart(IntakeStart::Restart);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -117,8 +137,11 @@ impl Host {
|
||||
};
|
||||
let root = identity.root;
|
||||
if identity.actor != root {
|
||||
// The root board may become available after a child starts.
|
||||
if thread.get::<ShadowReviewer>().is_some() {
|
||||
// A surviving child joins the new board on its next turn.
|
||||
if thread
|
||||
.get::<ShadowReviewer>()
|
||||
.is_some_and(|r| !r.cancellation.is_cancelled())
|
||||
{
|
||||
return;
|
||||
}
|
||||
self.attach_shadow(thread, &identity, &self.tree_delivery(root));
|
||||
@@ -131,7 +154,7 @@ impl Host {
|
||||
.state
|
||||
.lock()
|
||||
.unwrap_or_else(PoisonError::into_inner);
|
||||
let State::PendingStart = *state else {
|
||||
let State::PendingStart(start) = *state else {
|
||||
return;
|
||||
};
|
||||
let tree = self.tree_delivery(root);
|
||||
@@ -139,6 +162,8 @@ impl Host {
|
||||
let host = self.clone();
|
||||
let current = Arc::clone(&enrollment);
|
||||
enrollment.tasks.spawn(async move {
|
||||
// On restart the durable tail may already include the enabling turn.
|
||||
// Missing it is preferable to delaying the turn or replaying disabled history.
|
||||
let startup = async {
|
||||
let manager = host.manager.upgrade().context("Guardian host stopped")?;
|
||||
let root_thread = manager
|
||||
@@ -146,18 +171,37 @@ impl Host {
|
||||
std::ptr::eq(e, current.as_ref())
|
||||
})
|
||||
.await?;
|
||||
// Config notifications can arrive out of order. Confirm the root
|
||||
// still uses Auto Review before opening a board or reading history.
|
||||
if matches!(start, IntakeStart::Restart) {
|
||||
let settings = root_thread.thread_settings_snapshot().await;
|
||||
if !routes_approval_policy_to_guardian(
|
||||
settings.approval_policy,
|
||||
settings.approvals_reviewer,
|
||||
) {
|
||||
let mut state = current.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
if !tree.stop.is_cancelled() {
|
||||
*state = State::Off;
|
||||
}
|
||||
return anyhow::Ok(());
|
||||
}
|
||||
}
|
||||
let thread = root_thread.thread_extension_data();
|
||||
host.activate(root, &tree, thread, codex_protocol::protocol::ThreadHistoryMode::Paginated, codex_history::HistoryInitialization::New).await?;
|
||||
host.activate(root, &tree, thread, start).await?;
|
||||
host.attach_shadow(thread, &identity, &tree);
|
||||
if !tree.stop.is_cancelled() {
|
||||
tracing::debug!(event.name = "guardian_v3.shadow_intake_started", %root, "Guardian shadow intake started");
|
||||
let event_name = match start {
|
||||
IntakeStart::Initial(..) => "guardian_v3.shadow_intake_started",
|
||||
IntakeStart::Restart => "guardian_v3.shadow_intake_restarted",
|
||||
};
|
||||
tracing::debug!(event.name = event_name, %root, "Guardian shadow intake started");
|
||||
}
|
||||
anyhow::Ok(())
|
||||
};
|
||||
if let Some(Err(_)) = tree.stop.run_until_cancelled(startup).await {
|
||||
let mut state = current.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
if !tree.stop.is_cancelled() {
|
||||
*state = State::PendingStart;
|
||||
*state = State::PendingStart(start);
|
||||
drop(state);
|
||||
tracing::warn!(%root, "Guardian shadow intake unavailable until the next turn");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user