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> {

View file

@ -60,6 +60,7 @@ remembers doctrine; the branch remembers the blade.
- Thread config helpers now live in `codex_message_processor/thread_config.rs`: instruction-source loading, thread config override construction, config-load error shaping, and dynamic-tool validation. The pure dynamic-tool validation contract has module-local unit tests.
- Turn control routes now live in `codex_message_processor/turn_routes.rs`: turn start, thread item injection, app-server client info setting, turn steer, turn interrupt, mode normalization, and input-limit validation with module-local unit tests.
- Epiphany read-route memory graph loading now belongs to `epiphany-codex-bridge::state::load_thread_memory_graph_snapshot`. Vendored app-server read routes may decide whether a loaded thread exists, but they no longer own the memory graph store path or load mechanics.
- Epiphany job launch/interrupt policy now belongs to `epiphany-codex-bridge::mutation_service`: heartbeat launch planning, runtime-spine job opening, binding/runtime-link mutation, and interrupt binding cleanup are out of vendored `CodexThread`. `CodexThread` still owns the narrow Codex-session read/update seam for Epiphany state until that persistence seam can be replaced cleanly.
- Heartbeat durable document/schema types now live in `epiphany-core/src/heartbeat_state/heartbeat_documents.rs`, role/default construction lives in `epiphany-core/src/heartbeat_state/heartbeat_roles.rs`, persistence/validation lives in `epiphany-core/src/heartbeat_state/heartbeat_store.rs`, and status/artifact JSON projection lives in `epiphany-core/src/heartbeat_state/heartbeat_projection.rs`. The root `heartbeat_state.rs` keeps scheduling and Void cognition orchestration for the next cuts. Focused heartbeat tests and `epiphany-core` check pass after the splits.
- Heartbeat role/default and store seams now have module-local unit tests: fixed lane catalog, Ghostlight scene role projection, and typed state/cognition CultCache round-trip. Keep future heartbeat cuts aligned to those seam tests instead of growing root integration-only coverage.
- Heartbeat adaptive pacing now lives in `epiphany-core/src/heartbeat_state/heartbeat_pacing.rs`, including running-turn count and effective cooldown multipliers. It has a module-local unit test for pending-turn counting; root scheduling calls the pacing seam instead of owning pressure/concurrency math directly.

Binary file not shown.

View file

@ -23,9 +23,12 @@ objective:
JSON may survive only at schema/wire/forensic/quarantine boundaries; and
vendored Codex must be reduced to the smallest OpenAI subscription
auth/model-transport adapter needed to preserve the user's Codex
compatibility. Read-route memory graph loading now lives in
`epiphany-codex-bridge::state`; vendored app-server route code calls that
bridge instead of owning the store path/load mechanics itself.
compatibility. Read-route memory graph loading and runtime-spine path
derivation now live in `epiphany-codex-bridge::state`; vendored
app-server route code calls that bridge instead of owning the store
path/load mechanics itself. Job launch/interrupt policy now lives in
`epiphany-codex-bridge::mutation_service`; vendored `CodexThread` remains
the narrow Codex-session persistence seam for Epiphany state read/update.
constraints:
- No frontier-scale training tithe.

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 {