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

@ -17,15 +17,22 @@ use codex_protocol::protocol::EpiphanyJobKind as CoreEpiphanyJobKind;
use codex_protocol::protocol::EpiphanyRetrievalState;
use codex_protocol::protocol::EpiphanyThreadState;
use codex_protocol::protocol::TokenUsageInfo as CoreTokenUsageInfo;
use epiphany_core::EpiphanyJobInterruptRequest;
use epiphany_core::EpiphanyJobLaunchRequest;
use epiphany_core::EpiphanyMemoryPatchReview;
use epiphany_core::EpiphanyMemoryPatchReviewStatus;
use epiphany_core::EpiphanyPromotionInput;
use epiphany_core::EpiphanyStateUpdate;
use epiphany_core::RuntimeSpineHeartbeatJobOptions;
use epiphany_core::RuntimeSpineHeartbeatLaunchPlanOptions;
use epiphany_core::apply_memory_patch_candidate;
use epiphany_core::clear_epiphany_job_binding_backend;
use epiphany_core::epiphany_state_update_validation_errors;
use epiphany_core::evaluate_promotion;
use epiphany_core::load_memory_graph_snapshot;
use epiphany_core::memory_graph_from_epiphany_graphs;
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 epiphany_core::runtime_role_worker_result;
use epiphany_core::write_memory_graph_snapshot;
use std::fs;
@ -54,6 +61,7 @@ use crate::runtime_results::load_completed_epiphany_role_finding;
use crate::state::client_visible_live_thread_epiphany_state;
use crate::state::load_thread_memory_graph_snapshot;
use crate::state::memory_graph_store_path;
use crate::state::runtime_spine_store_path;
use uuid::Uuid;
#[derive(Debug, Clone)]
@ -410,14 +418,60 @@ pub async fn launch_thread_epiphany_job(
missing_projection_reason: &str,
) -> Result<EpiphanyJobLaunchApplied, CodexErr> {
let changed_fields = epiphany_job_launch_changed_fields();
let launched = thread.epiphany_launch_job(launch_request).await?;
let epiphany_state =
client_visible_live_thread_epiphany_state(thread, launched.epiphany_state).await;
let current_state = thread.epiphany_state().await.unwrap_or_default();
validate_expected_revision(launch_request.expected_revision, 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: launch_request.binding_id.clone(),
kind: launch_request.kind,
scope: launch_request.scope.clone(),
owner_role: launch_request.owner_role.clone(),
authority_scope: launch_request.authority_scope.clone(),
linked_subgoal_ids: launch_request.linked_subgoal_ids.clone(),
linked_graph_node_ids: launch_request.linked_graph_node_ids.clone(),
instruction: launch_request.instruction.clone(),
launch_document: launch_request.launch_document.clone(),
output_contract_id: launch_request.output_contract_id.clone(),
max_runtime_seconds: launch_request.max_runtime_seconds,
runtime_job_id: backend_job_id.clone(),
},
)
.map_err(|err| CodexErr::InvalidRequest(err.to_string()))?;
let runtime_store = runtime_spine_store_path(thread).await;
open_epiphany_runtime_spine_job(
runtime_store.as_path(),
&current_state,
&launch_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 update = EpiphanyStateUpdate {
expected_revision: launch_request.expected_revision,
job_bindings: Some(next_job_bindings),
runtime_links: vec![launch_plan.runtime_link],
..Default::default()
};
let validation_errors = epiphany_state_update_validation_errors(&current_state, &update);
if !validation_errors.is_empty() {
return Err(CodexErr::InvalidRequest(format!(
"invalid Epiphany job launch patch: {}",
validation_errors.join("; ")
)));
}
let epiphany_state = thread.epiphany_update_state(update).await?;
let epiphany_state = client_visible_live_thread_epiphany_state(thread, epiphany_state).await;
let job = map_launched_epiphany_job(
&epiphany_state,
launched.binding_id.as_str(),
launched.launcher_job_id.as_str(),
launched.backend_job_id.as_str(),
launch_request.binding_id.as_str(),
launcher_job_id.as_str(),
backend_job_id.as_str(),
kind,
missing_projection_reason,
);
@ -429,27 +483,87 @@ pub async fn launch_thread_epiphany_job(
})
}
fn open_epiphany_runtime_spine_job(
store_path: &std::path::Path,
state: &EpiphanyThreadState,
request: &EpiphanyJobLaunchRequest,
backend_job_id: &str,
) -> Result<(), CodexErr> {
let now = Utc::now().to_rfc3339_opts(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 {backend_job_id:?} in {}: {err}",
store_path.display()
))
})?;
Ok(())
}
pub async fn interrupt_thread_epiphany_job(
thread: &CodexThread,
expected_revision: Option<u64>,
binding_id: &str,
reason: Option<String>,
_reason: Option<String>,
) -> Result<EpiphanyJobInterruptApplied, CodexErr> {
let changed_fields = vec![ThreadEpiphanyStateUpdatedField::JobBindings];
let interrupted = thread
.epiphany_interrupt_job(EpiphanyJobInterruptRequest {
if binding_id.trim().is_empty() {
return Err(CodexErr::InvalidRequest(
"epiphany job interrupt binding_id must be non-empty".to_string(),
));
}
let current_state = thread.epiphany_state().await.unwrap_or_default();
validate_expected_revision(expected_revision, current_state.revision)?;
let Some(binding_index) = current_state
.job_bindings
.iter()
.position(|binding| binding.id == binding_id)
else {
return Err(CodexErr::InvalidRequest(format!(
"epiphany job binding {binding_id:?} was not found"
)));
};
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 = thread
.epiphany_update_state(EpiphanyStateUpdate {
expected_revision,
binding_id: binding_id.to_string(),
reason,
job_bindings: Some(next_job_bindings),
..Default::default()
})
.await?;
let epiphany_state =
client_visible_live_thread_epiphany_state(thread, interrupted.epiphany_state).await;
let epiphany_state = client_visible_live_thread_epiphany_state(thread, epiphany_state).await;
let job = map_interrupted_epiphany_job(&epiphany_state, binding_id);
Ok(EpiphanyJobInterruptApplied {
cancel_requested: interrupted.cancel_requested,
interrupted_thread_ids: interrupted.interrupted_thread_ids,
cancel_requested,
interrupted_thread_ids,
revision: epiphany_state.revision,
changed_fields,
epiphany_state,
@ -495,10 +609,15 @@ pub async fn launch_thread_epiphany_reorient(
&decision,
load_thread_memory_graph_snapshot(thread).await?.as_ref(),
);
let changed_fields = epiphany_job_launch_changed_fields();
let launched = thread.epiphany_launch_job(launch_request).await?;
let epiphany_state =
client_visible_live_thread_epiphany_state(thread, launched.epiphany_state).await;
let launched = launch_thread_epiphany_job(
thread,
launch_request,
CoreEpiphanyJobKind::Specialist,
"missing launched reorient projection",
)
.await?;
let changed_fields = launched.changed_fields;
let epiphany_state = launched.epiphany_state;
let job = map_epiphany_jobs(Some(&epiphany_state), None)
.into_iter()
.find(|job| job.id == EPIPHANY_REORIENT_LAUNCH_BINDING_ID)

View file

@ -66,6 +66,16 @@ pub fn memory_graph_store_path(workspace_root: &Path) -> PathBuf {
workspace_root.join("state").join("memory-graph.msgpack")
}
pub async fn runtime_spine_store_path(thread: &CodexThread) -> PathBuf {
thread
.config_snapshot()
.await
.cwd
.join("state")
.join("runtime-spine.msgpack")
.to_path_buf()
}
pub async fn load_thread_memory_graph_snapshot(
thread: &CodexThread,
) -> Result<Option<EpiphanyMemoryGraphSnapshot>, CodexErr> {