From dc6d74c37c17adbdbaf5c40e2eef58085067abde Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 19 May 2026 14:56:46 +0100 Subject: [PATCH] Abstract mutation host out of bridge --- epiphany-codex-bridge/Cargo.toml | 1 - epiphany-codex-bridge/src/mutation_service.rs | 81 ++++++----- vendor/codex/codex-rs/Cargo.lock | 1 - .../epiphany_automation.rs | 6 +- .../epiphany_mutation_routes.rs | 130 +++++++++--------- .../epiphany_thread_host.rs | 40 ++++++ 6 files changed, 148 insertions(+), 111 deletions(-) diff --git a/epiphany-codex-bridge/Cargo.toml b/epiphany-codex-bridge/Cargo.toml index 9031eff3..924f4cb0 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-core = { path = "../vendor/codex/codex-rs/core" } 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" } diff --git a/epiphany-codex-bridge/src/mutation_service.rs b/epiphany-codex-bridge/src/mutation_service.rs index fc61c871..4abd6fc1 100644 --- a/epiphany-codex-bridge/src/mutation_service.rs +++ b/epiphany-codex-bridge/src/mutation_service.rs @@ -1,6 +1,7 @@ use chrono::SecondsFormat; use chrono::Utc; use std::path::Path; +use std::path::PathBuf; use codex_app_server_protocol::ThreadEpiphanyJob; use codex_app_server_protocol::ThreadEpiphanyJobKind; @@ -12,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_core::CodexThread; use codex_protocol::error::CodexErr; use codex_protocol::protocol::EpiphanyEvidenceRecord; use codex_protocol::protocol::EpiphanyJobKind as CoreEpiphanyJobKind; @@ -56,10 +56,23 @@ use crate::reorient::map_epiphany_freshness; use crate::reorient::map_epiphany_reorient; use crate::runtime_results::load_completed_epiphany_reorient_finding; use crate::runtime_results::load_completed_epiphany_role_finding; -use crate::state::client_visible_epiphany_state_for_paths; -use crate::state::thread_state_mirror_id_from_rollout_path; use uuid::Uuid; +#[allow(async_fn_in_trait)] +pub trait EpiphanyMutationHost { + async fn epiphany_state(&self) -> Option; + async fn epiphany_reference_turn_id(&self) -> Option; + async fn epiphany_persist_state( + &self, + next_state: EpiphanyThreadState, + ) -> Result; + async fn epiphany_runtime_spine_store_path(&self) -> PathBuf; + async fn client_visible_epiphany_state( + &self, + fallback: EpiphanyThreadState, + ) -> EpiphanyThreadState; +} + #[derive(Debug, Clone)] pub struct EpiphanyThreadUpdateApplied { pub revision: u64, @@ -127,7 +140,7 @@ pub struct EpiphanyReorientLaunchApplied { } pub async fn apply_epiphany_state_update_to_thread( - thread: &CodexThread, + thread: &impl EpiphanyMutationHost, update: EpiphanyStateUpdate, ) -> Result { let current_state = thread.epiphany_state().await.unwrap_or_default(); @@ -171,7 +184,7 @@ pub fn apply_epiphany_state_update_to_state( } pub async fn launch_epiphany_job_on_thread( - thread: &CodexThread, + thread: &impl EpiphanyMutationHost, request: EpiphanyJobLaunchRequest, ) -> Result { let current_state = thread.epiphany_state().await.unwrap_or_default(); @@ -244,7 +257,7 @@ pub async fn launch_epiphany_job_on_thread( } pub async fn interrupt_epiphany_job_on_thread( - thread: &CodexThread, + thread: &impl EpiphanyMutationHost, request: EpiphanyJobInterruptRequest, ) -> Result { if request.binding_id.trim().is_empty() { @@ -293,14 +306,14 @@ pub async fn interrupt_epiphany_job_on_thread( } pub async fn apply_thread_epiphany_update( - thread: &CodexThread, + thread: &impl EpiphanyMutationHost, expected_revision: Option, patch: ThreadEpiphanyUpdatePatch, ) -> Result { 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?; - let epiphany_state = client_visible_live_thread_epiphany_state(thread, epiphany_state).await; + let epiphany_state = thread.client_visible_epiphany_state(epiphany_state).await; Ok(EpiphanyThreadUpdateApplied { revision: epiphany_state.revision, changed_fields, @@ -309,7 +322,7 @@ pub async fn apply_thread_epiphany_update( } pub async fn apply_thread_epiphany_promote( - thread: &CodexThread, + thread: &impl EpiphanyMutationHost, expected_revision: Option, patch: ThreadEpiphanyUpdatePatch, verifier_evidence: EpiphanyEvidenceRecord, @@ -339,7 +352,7 @@ pub async fn apply_thread_epiphany_promote( patch.evidence.push(verifier_evidence); let update = state_update_from_thread_patch(expected_revision, patch); let epiphany_state = apply_epiphany_state_update_to_thread(thread, update).await?; - let epiphany_state = client_visible_live_thread_epiphany_state(thread, epiphany_state).await; + let epiphany_state = thread.client_visible_epiphany_state(epiphany_state).await; Ok(EpiphanyThreadPromoteApplied::Accepted( EpiphanyThreadUpdateApplied { revision: epiphany_state.revision, @@ -350,7 +363,7 @@ pub async fn apply_thread_epiphany_promote( } pub async fn apply_thread_epiphany_role_accept( - thread: &CodexThread, + thread: &impl EpiphanyMutationHost, role_id: ThreadEpiphanyRoleId, expected_revision: Option, binding_id: &str, @@ -391,7 +404,7 @@ pub async fn apply_thread_epiphany_role_accept( state_update_from_thread_patch(expected_revision, acceptance_update.patch), ) .await?; - let epiphany_state = client_visible_live_thread_epiphany_state(thread, epiphany_state).await; + let epiphany_state = thread.client_visible_epiphany_state(epiphany_state).await; Ok(EpiphanyRoleAcceptApplied { revision: epiphany_state.revision, @@ -406,7 +419,7 @@ pub async fn apply_thread_epiphany_role_accept( } pub async fn apply_thread_epiphany_reorient_accept( - thread: &CodexThread, + thread: &impl EpiphanyMutationHost, expected_revision: Option, binding_id: &str, update_scratch: bool, @@ -451,7 +464,7 @@ pub async fn apply_thread_epiphany_reorient_accept( let changed_fields = acceptance_update.changed_fields.clone(); let epiphany_state = apply_epiphany_state_update_to_thread(thread, acceptance_update.state_update).await?; - let epiphany_state = client_visible_live_thread_epiphany_state(thread, epiphany_state).await; + let epiphany_state = thread.client_visible_epiphany_state(epiphany_state).await; Ok(EpiphanyReorientAcceptApplied { revision: epiphany_state.revision, @@ -465,7 +478,7 @@ pub async fn apply_thread_epiphany_reorient_accept( } pub async fn launch_thread_epiphany_role( - thread: &CodexThread, + thread: &impl EpiphanyMutationHost, thread_id: &str, role_id: ThreadEpiphanyRoleId, expected_revision: Option, @@ -495,15 +508,16 @@ pub async fn launch_thread_epiphany_role( } pub async fn launch_thread_epiphany_job( - thread: &CodexThread, + thread: &impl EpiphanyMutationHost, launch_request: EpiphanyJobLaunchRequest, kind: CoreEpiphanyJobKind, missing_projection_reason: &str, ) -> Result { let changed_fields = epiphany_job_launch_changed_fields(); let launched = launch_epiphany_job_on_thread(thread, launch_request).await?; - let epiphany_state = - client_visible_live_thread_epiphany_state(thread, launched.epiphany_state).await; + let epiphany_state = thread + .client_visible_epiphany_state(launched.epiphany_state) + .await; let job = map_launched_epiphany_job( &epiphany_state, launched.binding_id.as_str(), @@ -521,7 +535,7 @@ pub async fn launch_thread_epiphany_job( } pub async fn interrupt_thread_epiphany_job( - thread: &CodexThread, + thread: &impl EpiphanyMutationHost, expected_revision: Option, binding_id: &str, reason: Option, @@ -536,8 +550,9 @@ pub async fn interrupt_thread_epiphany_job( }, ) .await?; - let epiphany_state = - client_visible_live_thread_epiphany_state(thread, interrupted.epiphany_state).await; + let epiphany_state = thread + .client_visible_epiphany_state(interrupted.epiphany_state) + .await; let job = map_interrupted_epiphany_job(&epiphany_state, binding_id); Ok(EpiphanyJobInterruptApplied { @@ -551,7 +566,7 @@ pub async fn interrupt_thread_epiphany_job( } pub async fn launch_thread_epiphany_reorient( - thread: &CodexThread, + thread: &impl EpiphanyMutationHost, thread_id: &str, expected_revision: Option, max_runtime_seconds: Option, @@ -589,8 +604,9 @@ pub async fn launch_thread_epiphany_reorient( ); let changed_fields = epiphany_job_launch_changed_fields(); let launched = launch_epiphany_job_on_thread(thread, launch_request).await?; - let epiphany_state = - client_visible_live_thread_epiphany_state(thread, launched.epiphany_state).await; + let epiphany_state = thread + .client_visible_epiphany_state(launched.epiphany_state) + .await; let job = map_epiphany_jobs(Some(&epiphany_state), None) .into_iter() .find(|job| job.id == EPIPHANY_REORIENT_LAUNCH_BINDING_ID) @@ -667,20 +683,3 @@ fn validate_expected_revision( } Ok(()) } - -async fn client_visible_live_thread_epiphany_state( - thread: &CodexThread, - fallback: EpiphanyThreadState, -) -> EpiphanyThreadState { - let config = thread.config_snapshot().await; - let codex_home = thread.codex_home().await; - let rollout_path = thread.rollout_path(); - let mirror_thread_id = thread_state_mirror_id_from_rollout_path(rollout_path.as_deref()); - client_visible_epiphany_state_for_paths( - fallback, - config.cwd.as_path(), - codex_home, - Some(mirror_thread_id.as_str()), - ) - .await -} diff --git a/vendor/codex/codex-rs/Cargo.lock b/vendor/codex/codex-rs/Cargo.lock index 377fb1af..33b528d2 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-core", "codex-protocol", "codex-utils-absolute-path", "epiphany-core", diff --git a/vendor/codex/codex-rs/app-server/src/codex_message_processor/epiphany_automation.rs b/vendor/codex/codex-rs/app-server/src/codex_message_processor/epiphany_automation.rs index 0cd1765b..f41e608c 100644 --- a/vendor/codex/codex-rs/app-server/src/codex_message_processor/epiphany_automation.rs +++ b/vendor/codex/codex-rs/app-server/src/codex_message_processor/epiphany_automation.rs @@ -23,6 +23,7 @@ use epiphany_codex_bridge::retrieve::epiphany_retrieval_state_for_paths; use tokio::sync::Mutex; use tracing::warn; +use super::epiphany_thread_host::EpiphanyCodexThreadHost; use super::epiphany_thread_host::client_visible_live_thread_epiphany_state; use crate::outgoing_message::ThreadScopedOutgoingMessageSender; use crate::thread_state::ThreadState; @@ -76,9 +77,8 @@ pub(crate) async fn maybe_run_epiphany_coordinator_automation_for_turn_boundary( let Some(launch_request) = verdict.launch_request else { return; }; - let launched = match launch_epiphany_job_on_thread(thread.as_ref(), launch_request) - .await - { + let host = EpiphanyCodexThreadHost::new(thread.as_ref()); + let launched = match launch_epiphany_job_on_thread(&host, launch_request).await { Ok(launched) => launched, Err(err) => { warn!( 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 75b3f9a3..78dba2a3 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 @@ -25,6 +25,7 @@ use std::sync::Arc; use super::CodexMessageProcessor; use super::ConnectionRequestId; use super::ThreadReadViewError; +use super::epiphany_thread_host::EpiphanyCodexThreadHost; impl CodexMessageProcessor { async fn load_epiphany_thread( @@ -96,8 +97,9 @@ impl CodexMessageProcessor { Some(thread) => thread, None => return, }; + let host = EpiphanyCodexThreadHost::new(loaded_thread.as_ref()); let applied = match launch_thread_epiphany_role( - loaded_thread.as_ref(), + &host, &thread_id, role_id, expected_revision, @@ -170,28 +172,25 @@ impl CodexMessageProcessor { Some(thread) => thread, None => return, }; - let applied = match apply_thread_epiphany_role_accept( - loaded_thread.as_ref(), - role_id, - expected_revision, - &binding_id, - ) - .await - { - Ok(applied) => applied, - Err(CodexErr::InvalidRequest(message)) => { - self.send_invalid_request_error(request_id, message).await; - return; - } - Err(err) => { - self.send_internal_error( - request_id, - format!("failed to apply Epiphany role finding: {err}"), - ) - .await; - return; - } - }; + let host = EpiphanyCodexThreadHost::new(loaded_thread.as_ref()); + let applied = + match apply_thread_epiphany_role_accept(&host, role_id, expected_revision, &binding_id) + .await + { + Ok(applied) => applied, + Err(CodexErr::InvalidRequest(message)) => { + self.send_invalid_request_error(request_id, message).await; + return; + } + Err(err) => { + self.send_internal_error( + request_id, + format!("failed to apply Epiphany role finding: {err}"), + ) + .await; + return; + } + }; let changed_fields = applied.changed_fields; let epiphany_state = applied.epiphany_state; @@ -259,8 +258,9 @@ impl CodexMessageProcessor { .snapshot(&thread_id) .await; let token_usage_info = loaded_thread.token_usage_info().await; + let host = EpiphanyCodexThreadHost::new(loaded_thread.as_ref()); let applied = match launch_thread_epiphany_reorient( - loaded_thread.as_ref(), + &host, &thread_id, expected_revision, max_runtime_seconds, @@ -334,8 +334,9 @@ impl CodexMessageProcessor { Some(thread) => thread, None => return, }; + let host = EpiphanyCodexThreadHost::new(loaded_thread.as_ref()); let applied = match apply_thread_epiphany_reorient_accept( - loaded_thread.as_ref(), + &host, expected_revision, binding_id.as_str(), update_scratch, @@ -439,28 +440,25 @@ impl CodexMessageProcessor { None => return, }; - let applied = match apply_thread_epiphany_promote( - thread.as_ref(), - expected_revision, - patch, - verifier_evidence, - ) - .await - { - Ok(applied) => applied, - Err(CodexErr::InvalidRequest(message)) => { - self.send_invalid_request_error(request_id, message).await; - return; - } - Err(err) => { - self.send_internal_error( - request_id, - format!("failed to promote Epiphany state update: {err}"), - ) - .await; - return; - } - }; + let host = EpiphanyCodexThreadHost::new(thread.as_ref()); + let applied = + match apply_thread_epiphany_promote(&host, expected_revision, patch, verifier_evidence) + .await + { + Ok(applied) => applied, + Err(CodexErr::InvalidRequest(message)) => { + self.send_invalid_request_error(request_id, message).await; + return; + } + Err(err) => { + self.send_internal_error( + request_id, + format!("failed to promote Epiphany state update: {err}"), + ) + .await; + return; + } + }; let applied = match applied { EpiphanyThreadPromoteApplied::Accepted(applied) => applied, @@ -516,22 +514,22 @@ impl CodexMessageProcessor { None => return, }; - let applied = - match apply_thread_epiphany_update(thread.as_ref(), expected_revision, patch).await { - Ok(applied) => applied, - Err(CodexErr::InvalidRequest(message)) => { - self.send_invalid_request_error(request_id, message).await; - return; - } - Err(err) => { - self.send_internal_error( - request_id, - format!("failed to update Epiphany state for {thread_uuid}: {err}"), - ) - .await; - return; - } - }; + 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)) => { + self.send_invalid_request_error(request_id, message).await; + return; + } + Err(err) => { + self.send_internal_error( + request_id, + format!("failed to update Epiphany state for {thread_uuid}: {err}"), + ) + .await; + return; + } + }; let changed_fields = applied.changed_fields; let epiphany_state = applied.epiphany_state; let response = ThreadEpiphanyUpdateResponse { @@ -576,8 +574,9 @@ impl CodexMessageProcessor { None => return, }; + let host = EpiphanyCodexThreadHost::new(thread.as_ref()); let applied = match launch_thread_epiphany_job( - thread.as_ref(), + &host, build_epiphany_job_launch_request( expected_revision, binding_id.clone(), @@ -651,8 +650,9 @@ impl CodexMessageProcessor { None => return, }; + let host = EpiphanyCodexThreadHost::new(thread.as_ref()); let applied = match interrupt_thread_epiphany_job( - thread.as_ref(), + &host, expected_revision, &binding_id, reason, 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 d1e780ba..d69f7773 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,11 +3,51 @@ 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 epiphany_codex_bridge::mutation_service::EpiphanyMutationHost; use epiphany_codex_bridge::state::client_visible_epiphany_state_for_paths; use epiphany_codex_bridge::state::thread_state_mirror_id_from_rollout_path; +pub(super) struct EpiphanyCodexThreadHost<'a> { + thread: &'a CodexThread, +} + +impl<'a> EpiphanyCodexThreadHost<'a> { + pub(super) fn new(thread: &'a CodexThread) -> Self { + Self { thread } + } +} + +impl EpiphanyMutationHost for EpiphanyCodexThreadHost<'_> { + async fn epiphany_state(&self) -> Option { + self.thread.epiphany_state().await + } + + async fn epiphany_reference_turn_id(&self) -> Option { + self.thread.epiphany_reference_turn_id().await + } + + async fn epiphany_persist_state( + &self, + next_state: EpiphanyThreadState, + ) -> Result { + self.thread.epiphany_persist_state(next_state).await + } + + async fn epiphany_runtime_spine_store_path(&self) -> std::path::PathBuf { + self.thread.epiphany_runtime_spine_store_path().await + } + + async fn client_visible_epiphany_state( + &self, + fallback: EpiphanyThreadState, + ) -> EpiphanyThreadState { + client_visible_live_thread_epiphany_state(self.thread, fallback).await + } +} + pub(super) async fn load_epiphany_state_from_rollout_path( rollout_path: &Path, ) -> std::result::Result, String> {