Abstract mutation host out of bridge

This commit is contained in:
Meta Vens 2026-05-19 14:56:46 +01:00
commit dc6d74c37c
6 changed files with 148 additions and 111 deletions

View file

@ -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!(

View file

@ -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,

View file

@ -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<EpiphanyThreadState> {
self.thread.epiphany_state().await
}
async fn epiphany_reference_turn_id(&self) -> Option<String> {
self.thread.epiphany_reference_turn_id().await
}
async fn epiphany_persist_state(
&self,
next_state: EpiphanyThreadState,
) -> Result<EpiphanyThreadState, CodexErr> {
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<Option<EpiphanyThreadState>, String> {