mirror of
https://github.com/openai/codex.git
synced 2026-12-02 00:31:03 +00:00
Simplify Guardian eager compaction orchestration
This commit is contained in:
@@ -3314,8 +3314,15 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preserves_compaction_only_turn() {
|
||||
fn preserves_guardian_compaction_inside_a_turn() {
|
||||
let items = vec![
|
||||
RolloutItem::SessionMeta(SessionMetaLine {
|
||||
meta: SessionMeta {
|
||||
source: SessionSource::SubAgent(SubAgentSource::Other("guardian".to_string())),
|
||||
..Default::default()
|
||||
},
|
||||
git: None,
|
||||
}),
|
||||
RolloutItem::EventMsg(EventMsg::TurnStarted(TurnStartedEvent {
|
||||
turn_id: "turn-compact".into(),
|
||||
trace_id: None,
|
||||
|
||||
@@ -30,6 +30,7 @@ use codex_protocol::protocol::TokenUsage;
|
||||
use serde_json::Value;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::sync::Semaphore;
|
||||
use tokio::sync::SemaphorePermit;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tracing::warn;
|
||||
|
||||
@@ -59,8 +60,8 @@ use super::prompt::guardian_policy_prompt_with_config;
|
||||
|
||||
mod eager_compaction;
|
||||
|
||||
use eager_compaction::GuardianEagerCompaction;
|
||||
use eager_compaction::GuardianEagerCompactionOutcome;
|
||||
use eager_compaction::GuardianMaintenanceLatch;
|
||||
use eager_compaction::GuardianMaintenanceOutcome;
|
||||
|
||||
const GUARDIAN_INTERRUPT_DRAIN_TIMEOUT: Duration = Duration::from_secs(5);
|
||||
#[derive(Debug)]
|
||||
@@ -110,7 +111,8 @@ struct GuardianReviewSession {
|
||||
cancel_token: CancellationToken,
|
||||
reuse_key: GuardianReviewSessionReuseKey,
|
||||
review_lock: Semaphore,
|
||||
eager_compaction: GuardianEagerCompaction,
|
||||
maintenance_latch: GuardianMaintenanceLatch,
|
||||
background_runtime: tokio::runtime::Handle,
|
||||
state: Mutex<GuardianReviewState>,
|
||||
}
|
||||
|
||||
@@ -147,6 +149,13 @@ struct GuardianReviewForkSnapshot {
|
||||
last_reviewed_transcript_cursor: Option<GuardianTranscriptCursor>,
|
||||
}
|
||||
|
||||
enum GuardianTrunkReviewClaim<'a> {
|
||||
Acquired(SemaphorePermit<'a>),
|
||||
UseEphemeral {
|
||||
fork_snapshot: Option<GuardianReviewForkSnapshot>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
struct GuardianReviewSessionReuseKey {
|
||||
// Only include settings that affect spawned-session behavior so reuse
|
||||
@@ -221,13 +230,13 @@ pub(crate) fn prompt_cache_key_override_for_review_session(
|
||||
impl GuardianReviewSession {
|
||||
async fn shutdown(&self) {
|
||||
self.cancel_token.cancel();
|
||||
self.wait_for_eager_compaction().await;
|
||||
self.acquire_maintenance_latch().await;
|
||||
let _ = self.codex.shutdown_and_wait().await;
|
||||
}
|
||||
|
||||
fn shutdown_in_background(self: &Arc<Self>) {
|
||||
let review_session = Arc::clone(self);
|
||||
drop(tokio::spawn(async move {
|
||||
drop(self.background_runtime.spawn(async move {
|
||||
review_session.shutdown().await;
|
||||
}));
|
||||
}
|
||||
@@ -280,7 +289,8 @@ impl Drop for EphemeralReviewCleanup {
|
||||
return;
|
||||
};
|
||||
let state = Arc::clone(&self.state);
|
||||
drop(tokio::spawn(async move {
|
||||
let background_runtime = review_session.background_runtime.clone();
|
||||
drop(background_runtime.spawn(async move {
|
||||
let review_session = {
|
||||
let mut state = state.lock().await;
|
||||
state
|
||||
@@ -325,6 +335,58 @@ impl GuardianReviewSessionManager {
|
||||
}
|
||||
}
|
||||
|
||||
#[expect(
|
||||
clippy::await_holding_invalid_type,
|
||||
reason = "trunk identity and review ownership must be decided atomically"
|
||||
)]
|
||||
async fn claim_trunk_for_review<'a>(
|
||||
&self,
|
||||
trunk: &'a Arc<GuardianReviewSession>,
|
||||
params: &GuardianReviewSessionParams,
|
||||
) -> Result<GuardianTrunkReviewClaim<'a>, GuardianReviewSessionOutcome> {
|
||||
let maintenance_guard = run_before_review_deadline(
|
||||
params.deadline,
|
||||
params.external_cancel.as_ref(),
|
||||
trunk.acquire_maintenance_latch(),
|
||||
)
|
||||
.await?;
|
||||
if *maintenance_guard == GuardianMaintenanceOutcome::DiscardSession {
|
||||
let fork_snapshot = trunk.fork_snapshot().await;
|
||||
let review_session = self.remove_trunk_if_current(trunk).await;
|
||||
drop(maintenance_guard);
|
||||
if let Some(review_session) = review_session {
|
||||
review_session.shutdown_in_background();
|
||||
}
|
||||
return Ok(GuardianTrunkReviewClaim::UseEphemeral { fork_snapshot });
|
||||
}
|
||||
|
||||
// Hold the maintenance latch until review ownership is decided.
|
||||
let state = self.state.lock().await;
|
||||
let trunk_pointer_is_current = state
|
||||
.trunk
|
||||
.as_ref()
|
||||
.is_some_and(|current| Arc::ptr_eq(current, trunk));
|
||||
let trunk_guard = if trunk_pointer_is_current {
|
||||
trunk.review_lock.try_acquire().ok()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let Some(trunk_guard) = trunk_guard else {
|
||||
drop(state);
|
||||
drop(maintenance_guard);
|
||||
let fork_snapshot = if trunk_pointer_is_current {
|
||||
trunk.fork_snapshot().await
|
||||
} else {
|
||||
None
|
||||
};
|
||||
return Ok(GuardianTrunkReviewClaim::UseEphemeral { fork_snapshot });
|
||||
};
|
||||
drop(state);
|
||||
drop(maintenance_guard);
|
||||
|
||||
Ok(GuardianTrunkReviewClaim::Acquired(trunk_guard))
|
||||
}
|
||||
|
||||
#[expect(
|
||||
clippy::await_holding_invalid_type,
|
||||
reason = "review session selection and trunk spawning must stay serialized"
|
||||
@@ -416,62 +478,21 @@ impl GuardianReviewSessionManager {
|
||||
.await;
|
||||
}
|
||||
|
||||
let eager_compaction_guard = match run_before_review_deadline(
|
||||
deadline,
|
||||
params.external_cancel.as_ref(),
|
||||
trunk.wait_for_eager_compaction(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(guard) => guard,
|
||||
let trunk_guard = match self.claim_trunk_for_review(&trunk, ¶ms).await {
|
||||
Ok(GuardianTrunkReviewClaim::Acquired(guard)) => guard,
|
||||
Ok(GuardianTrunkReviewClaim::UseEphemeral { fork_snapshot }) => {
|
||||
return Box::pin(self.run_ephemeral_review(
|
||||
params,
|
||||
next_reuse_key,
|
||||
deadline,
|
||||
fork_snapshot,
|
||||
))
|
||||
.await;
|
||||
}
|
||||
Err(outcome) => {
|
||||
return (outcome, GuardianReviewAnalyticsResult::without_session());
|
||||
}
|
||||
};
|
||||
if *eager_compaction_guard == GuardianEagerCompactionOutcome::DiscardSession {
|
||||
let fork_snapshot = trunk.fork_snapshot().await;
|
||||
let review_session = self.remove_trunk_if_current(&trunk).await;
|
||||
drop(eager_compaction_guard);
|
||||
if let Some(review_session) = review_session {
|
||||
review_session.shutdown_in_background();
|
||||
}
|
||||
return Box::pin(self.run_ephemeral_review(
|
||||
params,
|
||||
next_reuse_key,
|
||||
deadline,
|
||||
fork_snapshot,
|
||||
))
|
||||
.await;
|
||||
}
|
||||
// Hold the maintenance latch until review ownership is decided.
|
||||
let state = self.state.lock().await;
|
||||
let trunk_pointer_is_current = state
|
||||
.trunk
|
||||
.as_ref()
|
||||
.is_some_and(|current| Arc::ptr_eq(current, &trunk));
|
||||
let trunk_guard = if trunk_pointer_is_current {
|
||||
trunk.review_lock.try_acquire().ok()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let Some(trunk_guard) = trunk_guard else {
|
||||
drop(state);
|
||||
drop(eager_compaction_guard);
|
||||
let fork_snapshot = if trunk_pointer_is_current {
|
||||
trunk.fork_snapshot().await
|
||||
} else {
|
||||
None
|
||||
};
|
||||
return Box::pin(self.run_ephemeral_review(
|
||||
params,
|
||||
next_reuse_key,
|
||||
deadline,
|
||||
fork_snapshot,
|
||||
))
|
||||
.await;
|
||||
};
|
||||
drop(state);
|
||||
drop(eager_compaction_guard);
|
||||
|
||||
let guardian_session_kind = if spawned_trunk {
|
||||
GuardianReviewSessionKind::TrunkNew
|
||||
@@ -513,12 +534,14 @@ impl GuardianReviewSessionManager {
|
||||
codex.session.get_config().await.as_ref(),
|
||||
codex.session.user_instructions().await,
|
||||
);
|
||||
let background_runtime = codex.session.services.runtime_handle.clone();
|
||||
self.state.lock().await.trunk = Some(Arc::new(GuardianReviewSession {
|
||||
reuse_key,
|
||||
codex,
|
||||
cancel_token: CancellationToken::new(),
|
||||
review_lock: Semaphore::new(/*permits*/ 1),
|
||||
eager_compaction: GuardianEagerCompaction::default(),
|
||||
maintenance_latch: GuardianMaintenanceLatch::default(),
|
||||
background_runtime,
|
||||
state: Mutex::new(GuardianReviewState {
|
||||
prior_review_count: 0,
|
||||
last_reviewed_transcript_cursor: None,
|
||||
@@ -533,6 +556,7 @@ impl GuardianReviewSessionManager {
|
||||
codex.session.get_config().await.as_ref(),
|
||||
codex.session.user_instructions().await,
|
||||
);
|
||||
let background_runtime = codex.session.services.runtime_handle.clone();
|
||||
self.state
|
||||
.lock()
|
||||
.await
|
||||
@@ -542,7 +566,8 @@ impl GuardianReviewSessionManager {
|
||||
codex,
|
||||
cancel_token: CancellationToken::new(),
|
||||
review_lock: Semaphore::new(/*permits*/ 1),
|
||||
eager_compaction: GuardianEagerCompaction::default(),
|
||||
maintenance_latch: GuardianMaintenanceLatch::default(),
|
||||
background_runtime,
|
||||
state: Mutex::new(GuardianReviewState {
|
||||
prior_review_count: 0,
|
||||
last_reviewed_transcript_cursor: None,
|
||||
@@ -697,7 +722,8 @@ async fn spawn_guardian_review_session(
|
||||
cancel_token,
|
||||
reuse_key,
|
||||
review_lock: Semaphore::new(/*permits*/ 1),
|
||||
eager_compaction: GuardianEagerCompaction::default(),
|
||||
maintenance_latch: GuardianMaintenanceLatch::default(),
|
||||
background_runtime: params.parent_session.services.runtime_handle.clone(),
|
||||
state: Mutex::new(GuardianReviewState {
|
||||
prior_review_count,
|
||||
last_reviewed_transcript_cursor: initial_transcript_cursor,
|
||||
@@ -1164,6 +1190,7 @@ mod tests {
|
||||
session.get_config().await.as_ref(),
|
||||
session.user_instructions().await,
|
||||
);
|
||||
let background_runtime = session.services.runtime_handle.clone();
|
||||
|
||||
(
|
||||
GuardianReviewSession {
|
||||
@@ -1177,7 +1204,8 @@ mod tests {
|
||||
cancel_token: CancellationToken::new(),
|
||||
reuse_key,
|
||||
review_lock: Semaphore::new(/*permits*/ 1),
|
||||
eager_compaction: GuardianEagerCompaction::default(),
|
||||
maintenance_latch: GuardianMaintenanceLatch::default(),
|
||||
background_runtime,
|
||||
state: Mutex::new(GuardianReviewState {
|
||||
prior_review_count: 0,
|
||||
last_reviewed_transcript_cursor: None,
|
||||
@@ -1189,6 +1217,33 @@ mod tests {
|
||||
)
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn guardian_background_runtime_outlives_temporary_review_runtime() {
|
||||
let (review_session, _tx_event, _rx_sub) = test_review_session().await;
|
||||
let background_runtime = review_session.background_runtime.clone();
|
||||
let (completed_tx, completed_rx) = tokio::sync::oneshot::channel();
|
||||
|
||||
std::thread::spawn(move || {
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.expect("build temporary runtime")
|
||||
.block_on(async move {
|
||||
drop(background_runtime.spawn(async move {
|
||||
tokio::task::yield_now().await;
|
||||
let _ = completed_tx.send(());
|
||||
}));
|
||||
});
|
||||
})
|
||||
.join()
|
||||
.expect("temporary runtime thread should exit cleanly");
|
||||
|
||||
tokio::time::timeout(Duration::from_secs(1), completed_rx)
|
||||
.await
|
||||
.expect("background task should outlive the temporary runtime")
|
||||
.expect("background task should report completion");
|
||||
}
|
||||
|
||||
fn turn_complete_event(
|
||||
turn_id: &str,
|
||||
last_agent_message: Option<&str>,
|
||||
|
||||
@@ -7,19 +7,20 @@ use tracing::warn;
|
||||
use crate::guardian::GUARDIAN_REVIEW_TIMEOUT;
|
||||
use crate::session::turn;
|
||||
|
||||
use super::GuardianReviewForkSnapshot;
|
||||
use super::GuardianReviewSession;
|
||||
use super::InitialHistory;
|
||||
use super::RolloutItem;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub(super) enum GuardianEagerCompactionOutcome {
|
||||
pub(super) enum GuardianMaintenanceOutcome {
|
||||
#[default]
|
||||
Reusable,
|
||||
DiscardSession,
|
||||
}
|
||||
|
||||
/// The held guard serializes review ownership with eager maintenance.
|
||||
pub(super) type GuardianEagerCompaction = Arc<Mutex<GuardianEagerCompactionOutcome>>;
|
||||
pub(super) type GuardianMaintenanceLatch = Arc<Mutex<GuardianMaintenanceOutcome>>;
|
||||
|
||||
impl GuardianReviewSession {
|
||||
pub(super) async fn schedule_eager_compaction(self: &Arc<Self>) {
|
||||
@@ -27,35 +28,33 @@ impl GuardianReviewSession {
|
||||
if !turn::auto_compact_needed(self.codex.session.as_ref(), turn_context.as_ref()).await {
|
||||
return;
|
||||
}
|
||||
let mut outcome_guard = Arc::clone(&self.eager_compaction).lock_owned().await;
|
||||
*outcome_guard = GuardianEagerCompactionOutcome::DiscardSession;
|
||||
let mut disposition_guard = Arc::clone(&self.maintenance_latch).lock_owned().await;
|
||||
*disposition_guard = GuardianMaintenanceOutcome::DiscardSession;
|
||||
|
||||
let review_session = Arc::clone(self);
|
||||
drop(tokio::spawn(async move {
|
||||
if review_session.run_eager_compaction(turn_context).await {
|
||||
*outcome_guard = GuardianEagerCompactionOutcome::Reusable;
|
||||
}
|
||||
drop(self.background_runtime.spawn(async move {
|
||||
*disposition_guard = review_session.run_eager_compaction(turn_context).await;
|
||||
}));
|
||||
}
|
||||
|
||||
pub(super) async fn wait_for_eager_compaction(
|
||||
pub(super) async fn acquire_maintenance_latch(
|
||||
&self,
|
||||
) -> OwnedMutexGuard<GuardianEagerCompactionOutcome> {
|
||||
Arc::clone(&self.eager_compaction).lock_owned().await
|
||||
) -> OwnedMutexGuard<GuardianMaintenanceOutcome> {
|
||||
Arc::clone(&self.maintenance_latch).lock_owned().await
|
||||
}
|
||||
|
||||
async fn run_eager_compaction(
|
||||
self: &Arc<Self>,
|
||||
turn_context: Arc<crate::session::turn_context::TurnContext>,
|
||||
) -> bool {
|
||||
) -> GuardianMaintenanceOutcome {
|
||||
let Ok(_review_guard) = self.review_lock.acquire().await else {
|
||||
return false;
|
||||
return GuardianMaintenanceOutcome::DiscardSession;
|
||||
};
|
||||
let Some(fork_snapshot) = self.fork_snapshot().await else {
|
||||
return false;
|
||||
return GuardianMaintenanceOutcome::DiscardSession;
|
||||
};
|
||||
let InitialHistory::Forked(items) = &fork_snapshot.initial_history else {
|
||||
return false;
|
||||
return GuardianMaintenanceOutcome::DiscardSession;
|
||||
};
|
||||
let rollout_item_count = items.len();
|
||||
let mut client_session = self.codex.session.services.model_client.new_session();
|
||||
@@ -85,35 +84,43 @@ impl GuardianReviewSession {
|
||||
.refresh_last_committed_fork_snapshot()
|
||||
.await
|
||||
.is_some_and(|snapshot| {
|
||||
let InitialHistory::Forked(items) = snapshot.initial_history else {
|
||||
return false;
|
||||
};
|
||||
items.get(rollout_item_count..).is_some_and(|items| {
|
||||
items.iter().any(|item| {
|
||||
matches!(
|
||||
item,
|
||||
RolloutItem::Compacted(compacted)
|
||||
if compacted.replacement_history.is_some()
|
||||
)
|
||||
})
|
||||
})
|
||||
snapshot_has_durable_compaction(&snapshot, rollout_item_count)
|
||||
});
|
||||
if !durable {
|
||||
if durable {
|
||||
GuardianMaintenanceOutcome::Reusable
|
||||
} else {
|
||||
warn!(
|
||||
guardian_thread_id = %self.codex.session.thread_id,
|
||||
"eager guardian compaction was not durably published"
|
||||
);
|
||||
GuardianMaintenanceOutcome::DiscardSession
|
||||
}
|
||||
durable
|
||||
}
|
||||
Some(Err(err)) => {
|
||||
warn!(
|
||||
guardian_thread_id = %self.codex.session.thread_id,
|
||||
"eager guardian compaction failed: {err}"
|
||||
);
|
||||
false
|
||||
GuardianMaintenanceOutcome::DiscardSession
|
||||
}
|
||||
None => false,
|
||||
None => GuardianMaintenanceOutcome::DiscardSession,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn snapshot_has_durable_compaction(
|
||||
snapshot: &GuardianReviewForkSnapshot,
|
||||
prior_rollout_item_count: usize,
|
||||
) -> bool {
|
||||
let InitialHistory::Forked(items) = &snapshot.initial_history else {
|
||||
return false;
|
||||
};
|
||||
items.get(prior_rollout_item_count..).is_some_and(|items| {
|
||||
items.iter().any(|item| {
|
||||
matches!(
|
||||
item,
|
||||
RolloutItem::Compacted(compacted) if compacted.replacement_history.is_some()
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ async fn guardian_compacts_between_reviews_before_the_next_request() -> Result<(
|
||||
|
||||
const FIRST_REVIEW_TOTAL_TOKENS: i64 = 500_000;
|
||||
const AUTO_COMPACT_TOKEN_LIMIT: i64 = 200_000;
|
||||
const COMPACT_RESPONSE_DELAY: Duration = Duration::from_secs(/*secs*/ 5);
|
||||
const COMPACT_RESPONSE_DELAY: Duration = Duration::from_secs(/*secs*/ 1);
|
||||
|
||||
let server = start_mock_server().await;
|
||||
let approval_policy = AskForApproval::OnRequest;
|
||||
@@ -63,8 +63,8 @@ async fn guardian_compacts_between_reviews_before_the_next_request() -> Result<(
|
||||
config.model_auto_compact_token_limit_scope =
|
||||
AutoCompactTokenLimitScope::BodyAfterPrefix;
|
||||
config.model_context_window = Some(400_000);
|
||||
config.model_provider.request_max_retries = Some(u64::MAX);
|
||||
config.model_provider.stream_max_retries = Some(u64::default());
|
||||
config.model_provider.request_max_retries = Some(0);
|
||||
config.model_provider.stream_max_retries = Some(0);
|
||||
config.model_provider.supports_websockets = false;
|
||||
})
|
||||
.build(&server)
|
||||
|
||||
Reference in New Issue
Block a user