From bbe8a758a1daf4e512e5817acff120dbe9dc510b Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 18 May 2026 07:00:35 +0100 Subject: [PATCH] Move Epiphany job policy into bridge --- epiphany-codex-bridge/src/mutation_service.rs | 159 +++++++++++++-- epiphany-codex-bridge/src/state.rs | 10 + notes/fresh-workspace-handoff.md | 1 + state/ledgers.msgpack | Bin 145124 -> 145405 bytes state/map.yaml | 9 +- .../epiphany_automation.rs | 24 ++- .../codex/codex-rs/core/src/codex_thread.rs | 192 ------------------ 7 files changed, 170 insertions(+), 225 deletions(-) diff --git a/epiphany-codex-bridge/src/mutation_service.rs b/epiphany-codex-bridge/src/mutation_service.rs index 81a3b393..53af4f7a 100644 --- a/epiphany-codex-bridge/src/mutation_service.rs +++ b/epiphany-codex-bridge/src/mutation_service.rs @@ -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 { 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( + ¤t_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(), + ¤t_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(¤t_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, binding_id: &str, - reason: Option, + _reason: Option, ) -> Result { 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) diff --git a/epiphany-codex-bridge/src/state.rs b/epiphany-codex-bridge/src/state.rs index 7531de79..22be773d 100644 --- a/epiphany-codex-bridge/src/state.rs +++ b/epiphany-codex-bridge/src/state.rs @@ -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, CodexErr> { diff --git a/notes/fresh-workspace-handoff.md b/notes/fresh-workspace-handoff.md index e3dda54c..b13ffaa1 100644 --- a/notes/fresh-workspace-handoff.md +++ b/notes/fresh-workspace-handoff.md @@ -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. diff --git a/state/ledgers.msgpack b/state/ledgers.msgpack index 0d8c37bf2b83b6524090b03089b78748f7f1e6db..51d46c15ff739dfbc3bfa7b7cdd4e44b3345515b 100644 GIT binary patch delta 214 zcmaFzmgDbxjtR<4=7%?`PGe;3Y~I7zzK4-<`yNInAyd(oMJ2^MjSP&;bPY^(4J|?p z%&ZIyraQDS>M~xNzUMrnic&#-PG)kYLVjt9LVlV;S!!NNeo<AO4`1sJWT%eyd&B^4Cqmu04;7Aurwq$=bk78T`}D-@R`mZU0xl 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; diff --git a/vendor/codex/codex-rs/core/src/codex_thread.rs b/vendor/codex/codex-rs/core/src/codex_thread.rs index 61a3c025..e63a3806 100644 --- a/vendor/codex/codex-rs/core/src/codex_thread.rs +++ b/vendor/codex/codex-rs/core/src/codex_thread.rs @@ -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 { - 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( - ¤t_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(), - ¤t_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( - ¤t_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 { - 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 { match message { ResponseItem::Message { role, content, .. } => Ok(ResponseInputItem::Message {