Add Epiphany CRRC safe-boundary automation

This commit is contained in:
Meta Vens 2026-04-28 19:03:25 +01:00
commit 86ba6de5a9
6 changed files with 248 additions and 19 deletions

View file

@ -1,8 +1,10 @@
use crate::codex_message_processor::ApiVersion;
use crate::codex_message_processor::maybe_run_epiphany_coordinator_automation_for_turn_boundary;
use crate::codex_message_processor::read_rollout_items_from_rollout;
use crate::codex_message_processor::read_summary_from_rollout;
use crate::codex_message_processor::summary_to_thread;
use crate::codex_message_processor::thread_epiphany_jobs_updated_notification_for_agent_job_progress;
use crate::epiphany_invalidation::EpiphanyInvalidationManager;
use crate::error_code::INTERNAL_ERROR_CODE;
use crate::error_code::INVALID_REQUEST_ERROR_CODE;
use crate::outgoing_message::ClientRequestResult;
@ -187,6 +189,7 @@ pub(crate) async fn apply_bespoke_event_handling(
outgoing: ThreadScopedOutgoingMessageSender,
thread_state: Arc<tokio::sync::Mutex<ThreadState>>,
thread_watch_manager: ThreadWatchManager,
epiphany_invalidation_manager: EpiphanyInvalidationManager,
api_version: ApiVersion,
fallback_model_provider: String,
codex_home: &Path,
@ -244,6 +247,15 @@ pub(crate) async fn apply_bespoke_event_handling(
&thread_state,
)
.await;
if !turn_failed {
maybe_run_epiphany_coordinator_automation_for_turn_boundary(
conversation_id,
conversation,
epiphany_invalidation_manager,
&outgoing,
)
.await;
}
}
EventMsg::SkillsUpdateAvailable => {
if let ApiVersion::V2 = api_version {
@ -3289,6 +3301,7 @@ mod tests {
outgoing: ThreadScopedOutgoingMessageSender,
thread_state: Arc<Mutex<ThreadState>>,
thread_watch_manager: ThreadWatchManager,
epiphany_invalidation_manager: EpiphanyInvalidationManager,
analytics_events_client: AnalyticsEventsClient,
codex_home: PathBuf,
}
@ -3308,6 +3321,7 @@ mod tests {
self.outgoing.clone(),
self.thread_state.clone(),
self.thread_watch_manager.clone(),
self.epiphany_invalidation_manager.clone(),
ApiVersion::V2,
"test-provider".to_string(),
&self.codex_home,
@ -3629,6 +3643,7 @@ mod tests {
outgoing: outgoing.clone(),
thread_state: thread_state.clone(),
thread_watch_manager: thread_watch_manager.clone(),
epiphany_invalidation_manager: EpiphanyInvalidationManager::new(),
analytics_events_client: AnalyticsEventsClient::new(
AuthManager::from_auth_for_testing(
CodexAuth::create_dummy_chatgpt_auth_for_testing(),
@ -4672,6 +4687,7 @@ mod tests {
let thread_state = new_thread_state();
let thread_watch_manager = ThreadWatchManager::new();
let epiphany_invalidation_manager = EpiphanyInvalidationManager::new();
let (tx, mut rx) = mpsc::channel(CHANNEL_CAPACITY);
let outgoing = Arc::new(OutgoingMessageSender::new(tx));
let outgoing = ThreadScopedOutgoingMessageSender::new(
@ -4706,6 +4722,7 @@ mod tests {
outgoing.clone(),
thread_state.clone(),
thread_watch_manager.clone(),
epiphany_invalidation_manager.clone(),
ApiVersion::V2,
"test-provider".to_string(),
codex_home.path(),
@ -4762,6 +4779,7 @@ mod tests {
outgoing,
thread_state,
thread_watch_manager,
epiphany_invalidation_manager,
ApiVersion::V2,
"test-provider".to_string(),
codex_home.path(),

View file

@ -10632,6 +10632,7 @@ impl CodexMessageProcessor {
thread_outgoing,
thread_state.clone(),
thread_watch_manager.clone(),
epiphany_invalidation_manager.clone(),
api_version,
fallback_model_provider.clone(),
codex_home.as_path(),
@ -13510,6 +13511,40 @@ struct EpiphanyCoordinatorDecision {
reason: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum EpiphanyCoordinatorAutomationAction {
None,
CompactRehydrateReorient,
LaunchReorientWorker,
}
fn map_epiphany_coordinator_automation_action(
decision: &EpiphanyCoordinatorDecision,
) -> EpiphanyCoordinatorAutomationAction {
if !decision.can_auto_run {
return EpiphanyCoordinatorAutomationAction::None;
}
match decision.action {
ThreadEpiphanyCoordinatorAction::CompactRehydrateReorient => {
EpiphanyCoordinatorAutomationAction::CompactRehydrateReorient
}
ThreadEpiphanyCoordinatorAction::LaunchReorientWorker => {
EpiphanyCoordinatorAutomationAction::LaunchReorientWorker
}
ThreadEpiphanyCoordinatorAction::PrepareCheckpoint
| ThreadEpiphanyCoordinatorAction::WaitForReorientWorker
| ThreadEpiphanyCoordinatorAction::ReviewReorientResult
| ThreadEpiphanyCoordinatorAction::RegatherManually
| ThreadEpiphanyCoordinatorAction::LaunchModeling
| ThreadEpiphanyCoordinatorAction::ReviewModelingResult
| ThreadEpiphanyCoordinatorAction::LaunchVerification
| ThreadEpiphanyCoordinatorAction::ReviewVerificationResult
| ThreadEpiphanyCoordinatorAction::ContinueImplementation => {
EpiphanyCoordinatorAutomationAction::None
}
}
}
fn map_epiphany_coordinator(
state_status: ThreadEpiphanyReorientStateStatus,
checkpoint_present: bool,
@ -15191,6 +15226,154 @@ pub(crate) async fn thread_epiphany_jobs_updated_notification_for_agent_job_prog
})
}
pub(crate) async fn maybe_run_epiphany_coordinator_automation_for_turn_boundary(
thread_id: ThreadId,
thread: Arc<CodexThread>,
epiphany_invalidation_manager: EpiphanyInvalidationManager,
outgoing: &ThreadScopedOutgoingMessageSender,
) {
let thread_id_text = thread_id.to_string();
let Some(state) = thread.epiphany_state().await else {
return;
};
let retrieval_override = thread.epiphany_retrieval_state().await;
let config_snapshot = thread.config_snapshot().await;
epiphany_invalidation_manager
.ensure_thread_watch(&thread_id_text, &config_snapshot.cwd)
.await;
let watcher_snapshot = epiphany_invalidation_manager
.snapshot(&thread_id_text)
.await;
let token_usage_info = thread.token_usage_info().await;
let state_db_ctx = thread.epiphany_state_runtime().await;
let (_state_revision, retrieval, graph, watcher) = map_epiphany_freshness(
Some(&state),
Some(&retrieval_override),
Some(&watcher_snapshot),
);
let pressure = map_epiphany_pressure(token_usage_info.as_ref());
let (state_status, reorient_decision) =
map_epiphany_reorient(Some(&state), &pressure, &retrieval, &graph, &watcher);
if state_status != ThreadEpiphanyReorientStateStatus::Ready {
return;
}
let job_resolution =
load_epiphany_job_launcher_snapshots(state_db_ctx.as_ref(), Some(&state)).await;
let jobs = map_epiphany_jobs(Some(&state), Some(&retrieval_override), &job_resolution);
let reorient_job = jobs
.iter()
.find(|job| job.id == EPIPHANY_REORIENT_LAUNCH_BINDING_ID)
.cloned();
let (reorient_result_status, reorient_finding, _) = load_epiphany_reorient_result_snapshot(
Some(&state),
state_db_ctx.as_ref(),
EPIPHANY_REORIENT_LAUNCH_BINDING_ID,
)
.await;
let crrc_recommendation = map_epiphany_crrc_recommendation(
true,
state_status,
&pressure,
&reorient_decision,
reorient_result_status,
state.investigation_checkpoint.as_ref().is_some(),
reorient_finding.is_some(),
reorient_finding
.as_ref()
.is_some_and(|finding| epiphany_reorient_finding_already_accepted(&state, finding)),
);
let roles = map_epiphany_roles(
Some(&state),
&jobs,
&reorient_decision,
&pressure,
&crrc_recommendation,
reorient_result_status,
reorient_job.as_ref(),
);
let (modeling_result_status, _, _) = load_epiphany_role_result_snapshot(
&state,
state_db_ctx.as_ref(),
ThreadEpiphanyRoleId::Modeling,
EPIPHANY_MODELING_ROLE_BINDING_ID,
)
.await;
let (verification_result_status, _, _) = load_epiphany_role_result_snapshot(
&state,
state_db_ctx.as_ref(),
ThreadEpiphanyRoleId::Verification,
EPIPHANY_VERIFICATION_ROLE_BINDING_ID,
)
.await;
let source_signals = ThreadEpiphanyCoordinatorSignals {
pressure_level: pressure.level,
should_prepare_compaction: pressure.should_prepare_compaction,
reorient_action: reorient_decision.action,
crrc_action: crrc_recommendation.action,
modeling_result_status,
verification_result_status,
reorient_result_status,
};
let coordinator = map_epiphany_coordinator(
state_status,
state.investigation_checkpoint.as_ref().is_some(),
&pressure,
&crrc_recommendation,
&roles,
&source_signals,
);
match map_epiphany_coordinator_automation_action(&coordinator) {
EpiphanyCoordinatorAutomationAction::None => {}
EpiphanyCoordinatorAutomationAction::CompactRehydrateReorient => {
if let Err(err) = thread.submit(Op::Compact).await {
warn!(
"failed to run Epiphany coordinator automatic compaction for {thread_id}: {err}"
);
}
}
EpiphanyCoordinatorAutomationAction::LaunchReorientWorker => {
let Some(checkpoint) = state.investigation_checkpoint.as_ref() else {
return;
};
let launch_request = build_epiphany_reorient_launch_request(
&thread_id_text,
Some(state.revision),
None,
&state,
checkpoint,
&reorient_decision,
);
let launched = match thread.epiphany_launch_job(launch_request).await {
Ok(launched) => launched,
Err(err) => {
warn!(
"failed to launch Epiphany coordinator reorientation worker for {thread_id}: {err}"
);
return;
}
};
let epiphany_state =
client_visible_live_thread_epiphany_state(thread.as_ref(), launched.epiphany_state)
.await;
outgoing
.send_server_notification(ServerNotification::ThreadEpiphanyStateUpdated(
ThreadEpiphanyStateUpdatedNotification {
thread_id: thread_id_text,
source: ThreadEpiphanyStateUpdatedSource::JobLaunch,
revision: epiphany_state.revision,
changed_fields: vec![ThreadEpiphanyStateUpdatedField::JobBindings],
epiphany_state,
},
))
.await;
}
}
}
async fn load_epiphany_job_launcher_snapshots(
state_db_ctx: Option<&StateDbHandle>,
state: Option<&EpiphanyThreadState>,
@ -18294,6 +18477,10 @@ mod tests {
ThreadEpiphanyCoordinatorAction::CompactRehydrateReorient
);
assert!(decision.can_auto_run);
assert_eq!(
map_epiphany_coordinator_automation_action(&decision),
EpiphanyCoordinatorAutomationAction::CompactRehydrateReorient
);
}
#[test]
@ -18330,6 +18517,10 @@ mod tests {
Some(ThreadEpiphanyRoleId::Reorientation)
);
assert!(decision.can_auto_run);
assert_eq!(
map_epiphany_coordinator_automation_action(&decision),
EpiphanyCoordinatorAutomationAction::LaunchReorientWorker
);
}
#[test]
@ -18362,6 +18553,10 @@ mod tests {
ThreadEpiphanyCoordinatorAction::ReviewReorientResult
);
assert!(decision.requires_review);
assert_eq!(
map_epiphany_coordinator_automation_action(&decision),
EpiphanyCoordinatorAutomationAction::None
);
}
#[test]
@ -18392,6 +18587,10 @@ mod tests {
launch_modeling.action,
ThreadEpiphanyCoordinatorAction::LaunchModeling
);
assert_eq!(
map_epiphany_coordinator_automation_action(&launch_modeling),
EpiphanyCoordinatorAutomationAction::None
);
let modeling_done = coordinator_signals(
ThreadEpiphanyCrrcAction::Continue,
@ -18411,6 +18610,10 @@ mod tests {
launch_verification.action,
ThreadEpiphanyCoordinatorAction::LaunchVerification
);
assert_eq!(
map_epiphany_coordinator_automation_action(&launch_verification),
EpiphanyCoordinatorAutomationAction::None
);
let verification_done = coordinator_signals(
ThreadEpiphanyCrrcAction::Continue,