diff --git a/epiphany-codex-bridge/Cargo.toml b/epiphany-codex-bridge/Cargo.toml index 08502d9e..5579fd90 100644 --- a/epiphany-codex-bridge/Cargo.toml +++ b/epiphany-codex-bridge/Cargo.toml @@ -13,7 +13,6 @@ path = "src/lib.rs" anyhow = "1" chrono = { version = "0.4", default-features = false, features = ["clock", "serde"] } codex-app-server-protocol = { path = "../vendor/codex/codex-rs/app-server-protocol" } -codex-protocol = { path = "../vendor/codex/codex-rs/protocol" } codex-utils-absolute-path = { path = "../vendor/codex/codex-rs/utils/absolute-path" } epiphany-core = { path = "../epiphany-core" } epiphany-state-model = { path = "../epiphany-state-model" } diff --git a/epiphany-codex-bridge/src/error.rs b/epiphany-codex-bridge/src/error.rs new file mode 100644 index 00000000..777cb03e --- /dev/null +++ b/epiphany-codex-bridge/src/error.rs @@ -0,0 +1,20 @@ +use std::error::Error; +use std::fmt; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum EpiphanyBridgeError { + InvalidRequest(String), + Fatal(String), +} + +pub type Result = std::result::Result; + +impl fmt::Display for EpiphanyBridgeError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::InvalidRequest(message) | Self::Fatal(message) => f.write_str(message), + } + } +} + +impl Error for EpiphanyBridgeError {} diff --git a/epiphany-codex-bridge/src/lib.rs b/epiphany-codex-bridge/src/lib.rs index 39947df5..09246335 100644 --- a/epiphany-codex-bridge/src/lib.rs +++ b/epiphany-codex-bridge/src/lib.rs @@ -1,6 +1,7 @@ pub mod checkpoint; pub mod context; pub mod coordinator; +pub mod error; pub mod invalidation; pub mod jobs; pub mod launch; diff --git a/epiphany-codex-bridge/src/mutation_service.rs b/epiphany-codex-bridge/src/mutation_service.rs index f5dafc23..dd85ab98 100644 --- a/epiphany-codex-bridge/src/mutation_service.rs +++ b/epiphany-codex-bridge/src/mutation_service.rs @@ -13,7 +13,6 @@ use codex_app_server_protocol::ThreadEpiphanyRoleFinding; use codex_app_server_protocol::ThreadEpiphanyRoleId; use codex_app_server_protocol::ThreadEpiphanyStateUpdatedField; use codex_app_server_protocol::ThreadEpiphanyUpdatePatch; -use codex_protocol::error::CodexErr; use epiphany_core::EpiphanyJobInterruptRequest; use epiphany_core::EpiphanyJobInterruptResult; use epiphany_core::EpiphanyJobLaunchRequest; @@ -35,6 +34,8 @@ use epiphany_state_model::EpiphanyJobKind as CoreEpiphanyJobKind; use epiphany_state_model::EpiphanyRetrievalState; use epiphany_state_model::EpiphanyThreadState; +use crate::error::EpiphanyBridgeError; +use crate::error::Result as BridgeResult; use crate::jobs::epiphany_blocked_state_job; use crate::jobs::map_epiphany_jobs; use crate::jobs::map_interrupted_epiphany_job; @@ -65,7 +66,7 @@ pub trait EpiphanyMutationHost { async fn epiphany_persist_state( &self, next_state: EpiphanyThreadState, - ) -> Result; + ) -> BridgeResult; async fn epiphany_runtime_spine_store_path(&self) -> PathBuf; async fn client_visible_epiphany_state( &self, @@ -142,7 +143,7 @@ pub struct EpiphanyReorientLaunchApplied { pub async fn apply_epiphany_state_update_to_thread( thread: &impl EpiphanyMutationHost, update: EpiphanyStateUpdate, -) -> Result { +) -> BridgeResult { let current_state = thread.epiphany_state().await.unwrap_or_default(); let reference_turn_id = thread.epiphany_reference_turn_id().await; let next_state = @@ -154,9 +155,9 @@ pub fn apply_epiphany_state_update_to_state( current_state: &EpiphanyThreadState, update: EpiphanyStateUpdate, reference_turn_id: Option, -) -> Result { +) -> BridgeResult { if update.is_empty() { - return Err(CodexErr::InvalidRequest( + return Err(EpiphanyBridgeError::InvalidRequest( "epiphany update patch must contain at least one mutation".to_string(), )); } @@ -165,7 +166,7 @@ pub fn apply_epiphany_state_update_to_state( if let Some(expected_revision) = update.expected_revision && next_state.revision != expected_revision { - return Err(CodexErr::InvalidRequest(format!( + return Err(EpiphanyBridgeError::InvalidRequest(format!( "epiphany state revision mismatch: expected {expected_revision}, found {}", next_state.revision ))); @@ -173,7 +174,7 @@ pub fn apply_epiphany_state_update_to_state( let validation_errors = epiphany_state_update_validation_errors(&next_state, &update); if !validation_errors.is_empty() { - return Err(CodexErr::InvalidRequest(format!( + return Err(EpiphanyBridgeError::InvalidRequest(format!( "invalid epiphany update patch: {}", validation_errors.join("; ") ))); @@ -186,7 +187,7 @@ pub fn apply_epiphany_state_update_to_state( pub async fn launch_epiphany_job_on_thread( thread: &impl EpiphanyMutationHost, request: EpiphanyJobLaunchRequest, -) -> Result { +) -> BridgeResult { let current_state = thread.epiphany_state().await.unwrap_or_default(); validate_expected_revision(request.expected_revision, current_state.revision)?; @@ -209,7 +210,7 @@ pub async fn launch_epiphany_job_on_thread( runtime_job_id: backend_job_id.clone(), }, ) - .map_err(|err| CodexErr::InvalidRequest(err.to_string()))?; + .map_err(|err| EpiphanyBridgeError::InvalidRequest(err.to_string()))?; let runtime_store = thread.epiphany_runtime_spine_store_path().await; open_epiphany_runtime_spine_job( runtime_store.as_path(), @@ -231,7 +232,7 @@ pub async fn launch_epiphany_job_on_thread( }, ); if !validation_errors.is_empty() { - return Err(CodexErr::InvalidRequest(format!( + return Err(EpiphanyBridgeError::InvalidRequest(format!( "invalid Epiphany job launch patch: {}", validation_errors.join("; ") ))); @@ -259,9 +260,9 @@ pub async fn launch_epiphany_job_on_thread( pub async fn interrupt_epiphany_job_on_thread( thread: &impl EpiphanyMutationHost, request: EpiphanyJobInterruptRequest, -) -> Result { +) -> BridgeResult { if request.binding_id.trim().is_empty() { - return Err(CodexErr::InvalidRequest( + return Err(EpiphanyBridgeError::InvalidRequest( "epiphany job interrupt binding_id must be non-empty".to_string(), )); } @@ -274,7 +275,7 @@ pub async fn interrupt_epiphany_job_on_thread( .iter() .position(|binding| binding.id == request.binding_id) else { - return Err(CodexErr::InvalidRequest(format!( + return Err(EpiphanyBridgeError::InvalidRequest(format!( "epiphany job binding {:?} was not found", request.binding_id ))); @@ -309,7 +310,7 @@ pub async fn apply_thread_epiphany_update( thread: &impl EpiphanyMutationHost, expected_revision: Option, patch: ThreadEpiphanyUpdatePatch, -) -> Result { +) -> BridgeResult { let changed_fields = epiphany_update_patch_changed_fields(&patch); let update = state_update_from_thread_patch(expected_revision, patch); let epiphany_state = apply_epiphany_state_update_to_thread(thread, update).await?; @@ -326,7 +327,7 @@ pub async fn apply_thread_epiphany_promote( expected_revision: Option, patch: ThreadEpiphanyUpdatePatch, verifier_evidence: EpiphanyEvidenceRecord, -) -> Result { +) -> BridgeResult { let decision = evaluate_promotion(EpiphanyPromotionInput { has_state_replacements: thread_epiphany_patch_has_state_replacements(&patch), active_subgoal_id: patch.active_subgoal_id.clone(), @@ -367,9 +368,9 @@ pub async fn apply_thread_epiphany_role_accept( role_id: ThreadEpiphanyRoleId, expected_revision: Option, binding_id: &str, -) -> Result { +) -> BridgeResult { let state = thread.epiphany_state().await.ok_or_else(|| { - CodexErr::InvalidRequest( + EpiphanyBridgeError::InvalidRequest( "cannot accept an Epiphany role finding without authoritative Epiphany state" .to_string(), ) @@ -392,7 +393,7 @@ pub async fn apply_thread_epiphany_role_accept( format!("obs-{accepted_prefix}-{}", Uuid::new_v4()), Utc::now().to_rfc3339_opts(SecondsFormat::Secs, true), ) - .map_err(CodexErr::InvalidRequest)?; + .map_err(EpiphanyBridgeError::InvalidRequest)?; let accepted_receipt_id = acceptance_update.accepted_receipt_id.clone(); let accepted_observation_id = acceptance_update.accepted_observation_id.clone(); @@ -424,16 +425,16 @@ pub async fn apply_thread_epiphany_reorient_accept( binding_id: &str, update_scratch: bool, update_investigation_checkpoint: bool, -) -> Result { +) -> BridgeResult { let state = thread.epiphany_state().await.ok_or_else(|| { - CodexErr::InvalidRequest( + EpiphanyBridgeError::InvalidRequest( "cannot accept a reorientation finding without authoritative Epiphany state" .to_string(), ) })?; validate_expected_revision(expected_revision, state.revision)?; if update_investigation_checkpoint && state.investigation_checkpoint.is_none() { - return Err(CodexErr::InvalidRequest( + return Err(EpiphanyBridgeError::InvalidRequest( "cannot update investigation checkpoint because this thread has no durable checkpoint" .to_string(), )); @@ -456,7 +457,7 @@ pub async fn apply_thread_epiphany_reorient_accept( update_investigation_checkpoint, state.investigation_checkpoint.clone(), ) - .map_err(CodexErr::InvalidRequest)?; + .map_err(EpiphanyBridgeError::InvalidRequest)?; let accepted_receipt_id = acceptance_update.accepted_receipt_id.clone(); let accepted_observation_id = acceptance_update.accepted_observation_id.clone(); @@ -483,9 +484,9 @@ pub async fn launch_thread_epiphany_role( role_id: ThreadEpiphanyRoleId, expected_revision: Option, max_runtime_seconds: Option, -) -> Result { +) -> BridgeResult { let state = thread.epiphany_state().await.ok_or_else(|| { - CodexErr::InvalidRequest( + EpiphanyBridgeError::InvalidRequest( "cannot launch an Epiphany role specialist without authoritative Epiphany state" .to_string(), ) @@ -497,7 +498,7 @@ pub async fn launch_thread_epiphany_role( max_runtime_seconds, &state, ) - .map_err(CodexErr::InvalidRequest)?; + .map_err(EpiphanyBridgeError::InvalidRequest)?; launch_thread_epiphany_job( thread, launch_request, @@ -512,7 +513,7 @@ pub async fn launch_thread_epiphany_job( launch_request: EpiphanyJobLaunchRequest, kind: CoreEpiphanyJobKind, missing_projection_reason: &str, -) -> Result { +) -> BridgeResult { let changed_fields = epiphany_job_launch_changed_fields(); let launched = launch_epiphany_job_on_thread(thread, launch_request).await?; let epiphany_state = thread @@ -539,7 +540,7 @@ pub async fn interrupt_thread_epiphany_job( expected_revision: Option, binding_id: &str, reason: Option, -) -> Result { +) -> BridgeResult { let changed_fields = vec![ThreadEpiphanyStateUpdatedField::JobBindings]; let interrupted = interrupt_epiphany_job_on_thread( thread, @@ -574,7 +575,7 @@ pub async fn launch_thread_epiphany_reorient( retrieval_override: Option<&EpiphanyRetrievalState>, watcher_snapshot: Option>, token_usage_info: Option<&EpiphanyTokenUsageSnapshot>, -) -> Result { +) -> BridgeResult { let (state_revision, retrieval, graph, watcher) = map_epiphany_freshness(state, retrieval_override, watcher_snapshot); let pressure = map_epiphany_pressure(token_usage_info); @@ -582,13 +583,13 @@ pub async fn launch_thread_epiphany_reorient( map_epiphany_reorient(state, &pressure, &retrieval, &graph, &watcher); let state = state.ok_or_else(|| { - CodexErr::InvalidRequest(format!( + EpiphanyBridgeError::InvalidRequest(format!( "cannot launch a reorientation worker without authoritative Epiphany state: {}", decision.note )) })?; let checkpoint = state.investigation_checkpoint.as_ref().ok_or_else(|| { - CodexErr::InvalidRequest(format!( + EpiphanyBridgeError::InvalidRequest(format!( "cannot launch a reorientation worker without a durable investigation checkpoint: {}", decision.note )) @@ -636,7 +637,7 @@ fn open_epiphany_runtime_spine_job( state: &EpiphanyThreadState, request: &EpiphanyJobLaunchRequest, backend_job_id: &str, -) -> Result<(), CodexErr> { +) -> BridgeResult<()> { let now = Utc::now().to_rfc3339_opts(SecondsFormat::Secs, true); open_runtime_spine_heartbeat_job( store_path, @@ -661,7 +662,7 @@ fn open_epiphany_runtime_spine_job( }, ) .map_err(|err| { - CodexErr::Fatal(format!( + EpiphanyBridgeError::Fatal(format!( "failed to open Epiphany runtime spine job {:?} in {}: {err}", backend_job_id, store_path.display() @@ -673,11 +674,11 @@ fn open_epiphany_runtime_spine_job( fn validate_expected_revision( expected_revision: Option, actual_revision: u64, -) -> Result<(), CodexErr> { +) -> BridgeResult<()> { if let Some(expected_revision) = expected_revision && actual_revision != expected_revision { - return Err(CodexErr::InvalidRequest(format!( + return Err(EpiphanyBridgeError::InvalidRequest(format!( "epiphany state revision mismatch: expected {expected_revision}, found {actual_revision}" ))); } diff --git a/epiphany-codex-bridge/src/runtime_results.rs b/epiphany-codex-bridge/src/runtime_results.rs index 06e0bfc9..47742dc3 100644 --- a/epiphany-codex-bridge/src/runtime_results.rs +++ b/epiphany-codex-bridge/src/runtime_results.rs @@ -5,8 +5,6 @@ use codex_app_server_protocol::ThreadEpiphanyReorientResultStatus; use codex_app_server_protocol::ThreadEpiphanyRoleFinding; use codex_app_server_protocol::ThreadEpiphanyRoleId; use codex_app_server_protocol::ThreadEpiphanyRoleResultStatus; -use codex_protocol::error::CodexErr; -use codex_protocol::error::Result as CodexResult; use epiphany_core::EpiphanyRuntimeJobSnapshot; use epiphany_core::EpiphanyRuntimeJobStatus; use epiphany_core::interpret_runtime_reorient_worker_result; @@ -17,6 +15,8 @@ use epiphany_core::runtime_role_worker_result; use epiphany_state_model::EpiphanyRuntimeLink; use epiphany_state_model::EpiphanyThreadState; +use crate::error::EpiphanyBridgeError; +use crate::error::Result as BridgeResult; use crate::results::map_core_role_result_role_id; use crate::results::map_protocol_reorient_finding; use crate::results::map_protocol_role_finding; @@ -273,7 +273,7 @@ pub fn load_completed_epiphany_role_finding( state: &EpiphanyThreadState, role_id: ThreadEpiphanyRoleId, binding_id: &str, -) -> CodexResult { +) -> BridgeResult { if let Some(link) = latest_epiphany_runtime_link_for_binding(state, binding_id) { let (status, finding, _note) = load_epiphany_role_result_from_runtime_spine_job( link.runtime_job_id.as_str(), @@ -281,13 +281,13 @@ pub fn load_completed_epiphany_role_finding( role_id, ); if status != ThreadEpiphanyRoleResultStatus::Completed { - return Err(CodexErr::InvalidRequest(format!( + return Err(EpiphanyBridgeError::InvalidRequest(format!( "cannot accept role result while worker status is {:?}", status ))); } return finding.ok_or_else(|| { - CodexErr::InvalidRequest( + EpiphanyBridgeError::InvalidRequest( "cannot accept completed role worker because no typed runtime-spine result was recorded" .to_string(), ) @@ -299,13 +299,13 @@ pub fn load_completed_epiphany_role_finding( .iter() .any(|binding| binding.id == binding_id) { - return Err(CodexErr::InvalidRequest(format!( + return Err(EpiphanyBridgeError::InvalidRequest(format!( "epiphany role binding {:?} was not found", binding_id ))); } - Err(CodexErr::InvalidRequest( + Err(EpiphanyBridgeError::InvalidRequest( "role findings without runtime-spine results are unsupported; accept only typed runtime-spine results" .to_string(), )) @@ -356,20 +356,20 @@ pub fn load_completed_epiphany_reorient_finding( runtime_store_path: Option<&Path>, state: &EpiphanyThreadState, binding_id: &str, -) -> CodexResult { +) -> BridgeResult { if let Some(link) = latest_epiphany_runtime_link_for_binding(state, binding_id) { let (status, finding, _note) = load_epiphany_reorient_result_from_runtime_spine_job( link.runtime_job_id.as_str(), runtime_store_path, ); if status != ThreadEpiphanyReorientResultStatus::Completed { - return Err(CodexErr::InvalidRequest(format!( + return Err(EpiphanyBridgeError::InvalidRequest(format!( "cannot accept reorientation result while worker status is {:?}", status ))); } return finding.ok_or_else(|| { - CodexErr::InvalidRequest( + EpiphanyBridgeError::InvalidRequest( "cannot accept completed reorientation worker because no typed runtime-spine result was recorded" .to_string(), ) @@ -381,13 +381,13 @@ pub fn load_completed_epiphany_reorient_finding( .iter() .any(|binding| binding.id == binding_id) { - return Err(CodexErr::InvalidRequest(format!( + return Err(EpiphanyBridgeError::InvalidRequest(format!( "epiphany reorientation binding {:?} was not found", binding_id ))); } - Err(CodexErr::InvalidRequest( + Err(EpiphanyBridgeError::InvalidRequest( "reorientation findings without runtime-spine results are unsupported; accept only typed runtime-spine results" .to_string(), )) diff --git a/vendor/codex/codex-rs/Cargo.lock b/vendor/codex/codex-rs/Cargo.lock index 1b78c8a9..d3bfae8c 100644 --- a/vendor/codex/codex-rs/Cargo.lock +++ b/vendor/codex/codex-rs/Cargo.lock @@ -5084,7 +5084,6 @@ dependencies = [ "anyhow", "chrono", "codex-app-server-protocol", - "codex-protocol", "codex-utils-absolute-path", "epiphany-core", "epiphany-state-model", diff --git a/vendor/codex/codex-rs/app-server/src/codex_message_processor/epiphany_mutation_routes.rs b/vendor/codex/codex-rs/app-server/src/codex_message_processor/epiphany_mutation_routes.rs index 6c44b6b9..9fc73a37 100644 --- a/vendor/codex/codex-rs/app-server/src/codex_message_processor/epiphany_mutation_routes.rs +++ b/vendor/codex/codex-rs/app-server/src/codex_message_processor/epiphany_mutation_routes.rs @@ -1,9 +1,9 @@ use codex_app_server_protocol::*; use codex_core::CodexThread; use codex_protocol::ThreadId; -use codex_protocol::error::CodexErr; use codex_protocol::protocol::EpiphanyRetrievalState; use codex_protocol::protocol::EpiphanyThreadState; +use epiphany_codex_bridge::error::EpiphanyBridgeError; use epiphany_codex_bridge::invalidation::epiphany_freshness_watcher_snapshot; use epiphany_codex_bridge::launch::EPIPHANY_REORIENT_LAUNCH_BINDING_ID; use epiphany_codex_bridge::launch::build_epiphany_job_launch_request; @@ -109,7 +109,7 @@ impl CodexMessageProcessor { .await { Ok(applied) => applied, - Err(CodexErr::InvalidRequest(message)) => { + Err(EpiphanyBridgeError::InvalidRequest(message)) => { self.send_invalid_request_error(request_id, message).await; return; } @@ -179,7 +179,7 @@ impl CodexMessageProcessor { .await { Ok(applied) => applied, - Err(CodexErr::InvalidRequest(message)) => { + Err(EpiphanyBridgeError::InvalidRequest(message)) => { self.send_invalid_request_error(request_id, message).await; return; } @@ -274,7 +274,7 @@ impl CodexMessageProcessor { .await { Ok(applied) => applied, - Err(CodexErr::InvalidRequest(message)) => { + Err(EpiphanyBridgeError::InvalidRequest(message)) => { self.send_invalid_request_error(request_id, message).await; return; } @@ -347,7 +347,7 @@ impl CodexMessageProcessor { .await { Ok(applied) => applied, - Err(CodexErr::InvalidRequest(message)) => { + Err(EpiphanyBridgeError::InvalidRequest(message)) => { self.send_invalid_request_error(request_id, message).await; return; } @@ -448,7 +448,7 @@ impl CodexMessageProcessor { .await { Ok(applied) => applied, - Err(CodexErr::InvalidRequest(message)) => { + Err(EpiphanyBridgeError::InvalidRequest(message)) => { self.send_invalid_request_error(request_id, message).await; return; } @@ -519,7 +519,7 @@ impl CodexMessageProcessor { let host = EpiphanyCodexThreadHost::new(thread.as_ref()); let applied = match apply_thread_epiphany_update(&host, expected_revision, patch).await { Ok(applied) => applied, - Err(CodexErr::InvalidRequest(message)) => { + Err(EpiphanyBridgeError::InvalidRequest(message)) => { self.send_invalid_request_error(request_id, message).await; return; } @@ -599,7 +599,7 @@ impl CodexMessageProcessor { .await { Ok(applied) => applied, - Err(CodexErr::InvalidRequest(message)) => { + Err(EpiphanyBridgeError::InvalidRequest(message)) => { self.send_invalid_request_error(request_id, message).await; return; } @@ -662,7 +662,7 @@ impl CodexMessageProcessor { .await { Ok(applied) => applied, - Err(CodexErr::InvalidRequest(message)) => { + Err(EpiphanyBridgeError::InvalidRequest(message)) => { self.send_invalid_request_error(request_id, message).await; return; } diff --git a/vendor/codex/codex-rs/app-server/src/codex_message_processor/epiphany_thread_host.rs b/vendor/codex/codex-rs/app-server/src/codex_message_processor/epiphany_thread_host.rs index 5385afe5..cf75611a 100644 --- a/vendor/codex/codex-rs/app-server/src/codex_message_processor/epiphany_thread_host.rs +++ b/vendor/codex/codex-rs/app-server/src/codex_message_processor/epiphany_thread_host.rs @@ -3,10 +3,11 @@ use std::path::Path; use codex_core::CodexThread; use codex_core::RolloutRecorder; use codex_core::latest_epiphany_state_from_rollout_items; -use codex_protocol::error::CodexErr; use codex_protocol::protocol::EpiphanyThreadState; use codex_protocol::protocol::InitialHistory; use codex_protocol::protocol::TokenUsageInfo; +use epiphany_codex_bridge::error::EpiphanyBridgeError; +use epiphany_codex_bridge::error::Result as BridgeResult; use epiphany_codex_bridge::mutation_service::EpiphanyMutationHost; use epiphany_codex_bridge::pressure::EpiphanyTokenUsageSnapshot; use epiphany_codex_bridge::state::client_visible_epiphany_state_for_paths; @@ -34,8 +35,11 @@ impl EpiphanyMutationHost for EpiphanyCodexThreadHost<'_> { async fn epiphany_persist_state( &self, next_state: EpiphanyThreadState, - ) -> Result { - self.thread.epiphany_persist_state(next_state).await + ) -> BridgeResult { + self.thread + .epiphany_persist_state(next_state) + .await + .map_err(|err| EpiphanyBridgeError::Fatal(err.to_string())) } async fn epiphany_runtime_spine_store_path(&self) -> std::path::PathBuf {