Move Epiphany job policy into bridge

This commit is contained in:
Meta Vens 2026-05-18 07:00:35 +01:00
commit bbe8a758a1
7 changed files with 170 additions and 225 deletions

View file

@ -6,6 +6,7 @@ use codex_app_server_protocol::ThreadEpiphanyStateUpdatedSource;
use codex_core::CodexThread;
use codex_core::SteerInputError;
use codex_protocol::ThreadId;
use codex_protocol::protocol::EpiphanyJobKind as CoreEpiphanyJobKind;
use codex_protocol::protocol::Op;
use codex_protocol::protocol::TokenUsageInfo as CoreTokenUsageInfo;
use codex_protocol::user_input::UserInput as CoreInputItem;
@ -14,11 +15,10 @@ use epiphany_codex_bridge::coordinator::EpiphanyCoordinatorAutomationInput;
use epiphany_codex_bridge::coordinator::select_epiphany_coordinator_automation;
use epiphany_codex_bridge::invalidation::EpiphanyInvalidationManager;
use epiphany_codex_bridge::invalidation::epiphany_freshness_watcher_snapshot;
use epiphany_codex_bridge::mutation::epiphany_job_launch_changed_fields;
use epiphany_codex_bridge::mutation_service::launch_thread_epiphany_job;
use epiphany_codex_bridge::pressure::map_epiphany_pressure;
use epiphany_codex_bridge::pressure::render_epiphany_pre_compaction_checkpoint_intervention;
use epiphany_codex_bridge::pressure::should_run_epiphany_pre_compaction_checkpoint_intervention;
use epiphany_codex_bridge::state::client_visible_live_thread_epiphany_state;
use tokio::sync::Mutex;
use tracing::warn;
@ -72,8 +72,15 @@ pub(crate) async fn maybe_run_epiphany_coordinator_automation_for_turn_boundary(
let Some(launch_request) = verdict.launch_request else {
return;
};
let launched = match thread.epiphany_launch_job(launch_request).await {
Ok(launched) => launched,
let applied = match launch_thread_epiphany_job(
thread.as_ref(),
launch_request,
CoreEpiphanyJobKind::Specialist,
"missing coordinator-launched reorient projection",
)
.await
{
Ok(applied) => applied,
Err(err) => {
warn!(
"failed to launch Epiphany coordinator reorientation worker for {thread_id}: {err}"
@ -81,17 +88,14 @@ pub(crate) async fn maybe_run_epiphany_coordinator_automation_for_turn_boundary(
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: epiphany_job_launch_changed_fields(),
epiphany_state,
revision: applied.revision,
changed_fields: applied.changed_fields,
epiphany_state: applied.epiphany_state,
},
))
.await;

View file

@ -35,21 +35,11 @@ use codex_protocol::protocol::TokenUsageInfo;
use codex_protocol::protocol::W3cTraceContext;
use codex_protocol::user_input::UserInput;
use codex_utils_absolute_path::AbsolutePathBuf;
use epiphany_core::EpiphanyJobInterruptRequest;
use epiphany_core::EpiphanyJobInterruptResult;
use epiphany_core::EpiphanyJobLaunchRequest;
use epiphany_core::EpiphanyJobLaunchResult;
use epiphany_core::EpiphanyRetrieveQuery;
use epiphany_core::EpiphanyRetrieveResponse;
use epiphany_core::EpiphanyStateUpdate;
use epiphany_core::RuntimeSpineHeartbeatJobOptions;
use epiphany_core::RuntimeSpineHeartbeatLaunchPlanOptions;
use epiphany_core::apply_epiphany_state_update;
use epiphany_core::clear_epiphany_job_binding_backend;
use epiphany_core::epiphany_state_update_validation_errors;
use epiphany_core::open_runtime_spine_heartbeat_job;
use epiphany_core::plan_runtime_spine_heartbeat_launch;
use epiphany_core::replace_or_append_epiphany_job_binding;
use rmcp::model::ReadResourceRequestParams;
use std::collections::HashMap;
use std::path::PathBuf;
@ -57,7 +47,6 @@ use tokio::sync::Mutex;
use tokio::sync::watch;
use codex_rollout::state_db::StateDbHandle;
use uuid::Uuid;
#[derive(Clone, Debug)]
pub struct ThreadConfigSnapshot {
@ -394,148 +383,6 @@ impl CodexThread {
Ok(next_state)
}
pub async fn epiphany_launch_job(
&self,
request: EpiphanyJobLaunchRequest,
) -> CodexResult<EpiphanyJobLaunchResult> {
let current_state = self
.codex
.session
.epiphany_state()
.await
.unwrap_or_default();
if let Some(expected_revision) = request.expected_revision
&& current_state.revision != expected_revision
{
return Err(CodexErr::InvalidRequest(format!(
"epiphany state revision mismatch: expected {expected_revision}, found {}",
current_state.revision
)));
}
let launcher_job_id = format!("epiphany-heartbeat-launch-{}", Uuid::new_v4());
let backend_job_id = Uuid::new_v4().to_string();
let launch_plan = plan_runtime_spine_heartbeat_launch(
&current_state,
RuntimeSpineHeartbeatLaunchPlanOptions {
binding_id: request.binding_id.clone(),
kind: request.kind,
scope: request.scope.clone(),
owner_role: request.owner_role.clone(),
authority_scope: request.authority_scope.clone(),
linked_subgoal_ids: request.linked_subgoal_ids.clone(),
linked_graph_node_ids: request.linked_graph_node_ids.clone(),
instruction: request.instruction.clone(),
launch_document: request.launch_document.clone(),
output_contract_id: request.output_contract_id.clone(),
max_runtime_seconds: request.max_runtime_seconds,
runtime_job_id: backend_job_id.clone(),
},
)
.map_err(|err| CodexErr::InvalidRequest(err.to_string()))?;
let runtime_store = self.epiphany_runtime_spine_store_path().await;
open_epiphany_runtime_spine_job(
runtime_store.as_path(),
&current_state,
&request,
backend_job_id.as_str(),
)?;
let next_job_bindings = replace_or_append_epiphany_job_binding(
current_state.job_bindings.clone(),
launch_plan.binding,
);
let validation_errors = epiphany_state_update_validation_errors(
&current_state,
&EpiphanyStateUpdate {
job_bindings: Some(next_job_bindings.clone()),
runtime_links: vec![launch_plan.runtime_link.clone()],
..Default::default()
},
);
if !validation_errors.is_empty() {
return Err(CodexErr::InvalidRequest(format!(
"invalid Epiphany job launch patch: {}",
validation_errors.join("; ")
)));
}
let epiphany_state = self
.epiphany_update_state(EpiphanyStateUpdate {
expected_revision: request.expected_revision,
job_bindings: Some(next_job_bindings),
runtime_links: vec![launch_plan.runtime_link],
..Default::default()
})
.await?;
Ok(EpiphanyJobLaunchResult {
epiphany_state,
binding_id: request.binding_id,
launcher_job_id,
backend_job_id,
})
}
pub async fn epiphany_interrupt_job(
&self,
request: EpiphanyJobInterruptRequest,
) -> CodexResult<EpiphanyJobInterruptResult> {
if request.binding_id.trim().is_empty() {
return Err(CodexErr::InvalidRequest(
"epiphany job interrupt binding_id must be non-empty".to_string(),
));
}
let current_state = self
.codex
.session
.epiphany_state()
.await
.unwrap_or_default();
if let Some(expected_revision) = request.expected_revision
&& current_state.revision != expected_revision
{
return Err(CodexErr::InvalidRequest(format!(
"epiphany state revision mismatch: expected {expected_revision}, found {}",
current_state.revision
)));
}
let Some(binding_index) = current_state
.job_bindings
.iter()
.position(|binding| binding.id == request.binding_id)
else {
return Err(CodexErr::InvalidRequest(format!(
"epiphany job binding {:?} was not found",
request.binding_id
)));
};
let interrupted_thread_ids = Vec::new();
let cancel_requested = false;
let next_job_bindings = clear_epiphany_job_binding_backend(
current_state.job_bindings.clone(),
binding_index,
"No active heartbeat turn is currently bound; launch explicitly to resume specialist work.",
);
let epiphany_state = self
.epiphany_update_state(EpiphanyStateUpdate {
expected_revision: request.expected_revision,
job_bindings: Some(next_job_bindings),
..Default::default()
})
.await?;
Ok(EpiphanyJobInterruptResult {
epiphany_state,
binding_id: request.binding_id,
cancel_requested,
interrupted_thread_ids,
})
}
pub async fn epiphany_retrieval_state(&self) -> EpiphanyRetrievalState {
let config = self.codex.thread_config_snapshot().await;
let workspace_root = config.cwd.to_path_buf();
@ -668,45 +515,6 @@ impl CodexThread {
}
}
fn open_epiphany_runtime_spine_job(
store_path: &std::path::Path,
state: &EpiphanyThreadState,
request: &EpiphanyJobLaunchRequest,
backend_job_id: &str,
) -> CodexResult<()> {
let now = chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true);
open_runtime_spine_heartbeat_job(
store_path,
RuntimeSpineHeartbeatJobOptions {
runtime_id: "epiphany-local".to_string(),
display_name: "Epiphany Local".to_string(),
session_id: "epiphany-main".to_string(),
objective: state
.objective
.clone()
.filter(|objective| !objective.trim().is_empty())
.unwrap_or_else(|| "Epiphany heartbeat activation".to_string()),
coordinator_note: "App-server launch opened this typed runtime session.".to_string(),
job_id: backend_job_id.to_string(),
role: request.owner_role.clone(),
binding_id: request.binding_id.clone(),
authority_scope: request.authority_scope.clone(),
instruction: request.instruction.clone(),
launch_document: request.launch_document.clone(),
output_contract_id: request.output_contract_id.clone(),
created_at: now,
},
)
.map_err(|err| {
CodexErr::Fatal(format!(
"failed to open Epiphany runtime spine job {:?} in {}: {err}",
backend_job_id,
store_path.display()
))
})?;
Ok(())
}
fn pending_message_input_item(message: &ResponseItem) -> CodexResult<ResponseInputItem> {
match message {
ResponseItem::Message { role, content, .. } => Ok(ResponseInputItem::Message {