From 85b6dac130a0233798494757dd4f96950e68ef8f Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 18 May 2026 07:18:36 +0100 Subject: [PATCH] Move Epiphany state update application into bridge --- epiphany-codex-bridge/src/mutation_service.rs | 61 +++++++++++++----- notes/fresh-workspace-handoff.md | 2 +- state/ledgers.msgpack | Bin 145984 -> 146318 bytes state/map.yaml | 6 +- .../codex/codex-rs/core/src/codex_thread.rs | 54 ++++------------ 5 files changed, 62 insertions(+), 61 deletions(-) diff --git a/epiphany-codex-bridge/src/mutation_service.rs b/epiphany-codex-bridge/src/mutation_service.rs index 95b4cf13..d29a5b03 100644 --- a/epiphany-codex-bridge/src/mutation_service.rs +++ b/epiphany-codex-bridge/src/mutation_service.rs @@ -25,6 +25,7 @@ use epiphany_core::EpiphanyStateUpdate; use epiphany_core::RuntimeSpineHeartbeatJobOptions; use epiphany_core::RuntimeSpineHeartbeatLaunchPlanOptions; use epiphany_core::apply_memory_patch_candidate; +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::evaluate_promotion; @@ -131,6 +132,34 @@ pub struct EpiphanyReorientLaunchApplied { pub job: ThreadEpiphanyJob, } +async fn apply_thread_epiphany_state_update( + thread: &CodexThread, + update: EpiphanyStateUpdate, +) -> Result { + if update.is_empty() { + return Err(CodexErr::InvalidRequest( + "epiphany update patch must contain at least one mutation".to_string(), + )); + } + + let reference_turn_id = thread.epiphany_reference_turn_id().await; + let mut next_state = thread.epiphany_state().await.unwrap_or_default(); + validate_expected_revision(update.expected_revision, next_state.revision)?; + + let validation_errors = epiphany_state_update_validation_errors(&next_state, &update); + if !validation_errors.is_empty() { + return Err(CodexErr::InvalidRequest(format!( + "invalid epiphany update patch: {}", + validation_errors.join("; ") + ))); + } + + apply_epiphany_state_update(&mut next_state, update, reference_turn_id.clone()); + thread + .persist_epiphany_state(reference_turn_id, next_state) + .await +} + pub async fn apply_thread_epiphany_update( thread: &CodexThread, expected_revision: Option, @@ -138,7 +167,7 @@ pub async fn apply_thread_epiphany_update( ) -> Result { let changed_fields = epiphany_update_patch_changed_fields(&patch); let update = state_update_from_thread_patch(expected_revision, patch); - let epiphany_state = thread.epiphany_update_state(update).await?; + let epiphany_state = apply_thread_epiphany_state_update(thread, update).await?; let epiphany_state = client_visible_live_thread_epiphany_state(thread, epiphany_state).await; Ok(EpiphanyThreadUpdateApplied { revision: epiphany_state.revision, @@ -176,7 +205,7 @@ pub async fn apply_thread_epiphany_promote( let mut patch = patch; patch.evidence.push(verifier_evidence); let update = state_update_from_thread_patch(expected_revision, patch); - let epiphany_state = thread.epiphany_update_state(update).await?; + let epiphany_state = apply_thread_epiphany_state_update(thread, update).await?; let epiphany_state = client_visible_live_thread_epiphany_state(thread, epiphany_state).await; Ok(EpiphanyThreadPromoteApplied::Accepted( EpiphanyThreadUpdateApplied { @@ -227,12 +256,11 @@ pub async fn apply_thread_epiphany_role_accept( let accepted_evidence_id = acceptance_update.accepted_evidence_id.clone(); let changed_fields = acceptance_update.changed_fields.clone(); let applied_patch = acceptance_update.patch.clone(); - let epiphany_state = thread - .epiphany_update_state(state_update_from_thread_patch( - expected_revision, - acceptance_update.patch, - )) - .await?; + let epiphany_state = apply_thread_epiphany_state_update( + thread, + state_update_from_thread_patch(expected_revision, acceptance_update.patch), + ) + .await?; let epiphany_state = client_visible_live_thread_epiphany_state(thread, epiphany_state).await; Ok(EpiphanyRoleAcceptApplied { @@ -363,9 +391,8 @@ pub async fn apply_thread_epiphany_reorient_accept( let accepted_observation_id = acceptance_update.accepted_observation_id.clone(); let accepted_evidence_id = acceptance_update.accepted_evidence_id.clone(); let changed_fields = acceptance_update.changed_fields.clone(); - let epiphany_state = thread - .epiphany_update_state(acceptance_update.state_update) - .await?; + let epiphany_state = + apply_thread_epiphany_state_update(thread, acceptance_update.state_update).await?; let epiphany_state = client_visible_live_thread_epiphany_state(thread, epiphany_state).await; Ok(EpiphanyReorientAcceptApplied { @@ -465,7 +492,7 @@ pub async fn launch_thread_epiphany_job( validation_errors.join("; ") ))); } - let epiphany_state = thread.epiphany_update_state(update).await?; + let epiphany_state = apply_thread_epiphany_state_update(thread, update).await?; let epiphany_state = client_visible_live_thread_epiphany_state(thread, epiphany_state).await; let job = map_launched_epiphany_job( &epiphany_state, @@ -551,13 +578,15 @@ pub async fn interrupt_thread_epiphany_job( binding_index, "No active heartbeat turn is currently bound; launch explicitly to resume specialist work.", ); - let epiphany_state = thread - .epiphany_update_state(EpiphanyStateUpdate { + let epiphany_state = apply_thread_epiphany_state_update( + thread, + EpiphanyStateUpdate { expected_revision, job_bindings: Some(next_job_bindings), ..Default::default() - }) - .await?; + }, + ) + .await?; let epiphany_state = client_visible_live_thread_epiphany_state(thread, epiphany_state).await; let job = map_interrupted_epiphany_job(&epiphany_state, binding_id); diff --git a/notes/fresh-workspace-handoff.md b/notes/fresh-workspace-handoff.md index ec2e3668..a7549a53 100644 --- a/notes/fresh-workspace-handoff.md +++ b/notes/fresh-workspace-handoff.md @@ -60,7 +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 memory graph loading and runtime-spine path derivation now belong to `epiphany-codex-bridge::state`. Vendored app-server read routes may decide whether a loaded thread exists, but they no longer own memory graph or runtime-spine store path/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. +- Epiphany job launch/interrupt policy and state-update application now belong to `epiphany-codex-bridge::mutation_service`: heartbeat launch planning, runtime-spine job opening, binding/runtime-link mutation, interrupt binding cleanup, and typed patch apply are out of vendored `CodexThread`. `CodexThread` now exposes only the narrow Codex-session host seam for reading current Epiphany state/reference turn and persisting an already-mutated snapshot into rollout/session storage. - Epiphany retrieval status, indexing, and retrieval execution now belong to `epiphany-codex-bridge::retrieve`; vendored `CodexThread` only exposes cwd/codex-home host facts for that path. - 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. diff --git a/state/ledgers.msgpack b/state/ledgers.msgpack index 6b9befdbd69a636ba06149708b4c7f61fa173afa..8179361ba6d712ecca9663eb8ca165562647e413 100644 GIT binary patch delta 223 zcmX@`g`@8~#{^|2>yVAA(-;|hn)fia?_p%zzK4-1&qB0iQAzPmBLgEdT?12HLyHgt zGb=-*>5ATrdW>At4cr)wr#D(MSxn#M$tW zAR{raQlYpcu_RT$C^an=D4v|EP*Pfyr;wPJqEL`pRGe8{Qml}eQv5NZGb diff --git a/state/map.yaml b/state/map.yaml index caa150a4..0c4fc46a 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -26,9 +26,11 @@ objective: compatibility. 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 store path/load mechanics itself. Job - launch/interrupt policy now lives in + launch/interrupt policy and state-update application now live in `epiphany-codex-bridge::mutation_service`; vendored `CodexThread` remains - the narrow Codex-session persistence seam for Epiphany state read/update. + the narrow Codex-session persistence seam for reading the live Epiphany + state and writing an already-mutated snapshot into Codex rollout/session + storage. Retrieval status, indexing, and retrieval execution now live in `epiphany-codex-bridge::retrieve`; vendored Codex supplies cwd/codex-home host facts rather than owning Epiphany retrieval behavior. diff --git a/vendor/codex/codex-rs/core/src/codex_thread.rs b/vendor/codex/codex-rs/core/src/codex_thread.rs index f645d972..33afc1e7 100644 --- a/vendor/codex/codex-rs/core/src/codex_thread.rs +++ b/vendor/codex/codex-rs/core/src/codex_thread.rs @@ -34,9 +34,6 @@ 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::EpiphanyStateUpdate; -use epiphany_core::apply_epiphany_state_update; -use epiphany_core::epiphany_state_update_validation_errors; use rmcp::model::ReadResourceRequestParams; use std::collections::HashMap; use std::path::PathBuf; @@ -329,59 +326,32 @@ impl CodexThread { self.codex.session.epiphany_state().await } - pub async fn epiphany_update_state( - &self, - update: EpiphanyStateUpdate, - ) -> CodexResult { - if update.is_empty() { - return Err(CodexErr::InvalidRequest( - "epiphany update patch must contain at least one mutation".to_string(), - )); - } - - let reference_turn_id = self - .codex + pub async fn epiphany_reference_turn_id(&self) -> Option { + self.codex .session .reference_context_item() .await - .and_then(|item| item.turn_id); - let mut next_state = self - .codex - .session - .epiphany_state() - .await - .unwrap_or_default(); - if let Some(expected_revision) = update.expected_revision - && next_state.revision != expected_revision - { - return Err(CodexErr::InvalidRequest(format!( - "epiphany state revision mismatch: expected {expected_revision}, found {}", - next_state.revision - ))); - } + .and_then(|item| item.turn_id) + } - let validation_errors = epiphany_state_update_validation_errors(&next_state, &update); - if !validation_errors.is_empty() { - return Err(CodexErr::InvalidRequest(format!( - "invalid epiphany update patch: {}", - validation_errors.join("; ") - ))); - } - - apply_epiphany_state_update(&mut next_state, update, reference_turn_id.clone()); + pub async fn persist_epiphany_state( + &self, + reference_turn_id: Option, + state: EpiphanyThreadState, + ) -> CodexResult { self.codex .session - .set_epiphany_state(Some(next_state.clone())) + .set_epiphany_state(Some(state.clone())) .await; self.codex .session .persist_rollout_items(&[RolloutItem::EpiphanyState(EpiphanyStateItem { turn_id: reference_turn_id, - state: next_state.clone(), + state: state.clone(), })]) .await; self.codex.session.flush_rollout().await?; - Ok(next_state) + Ok(state) } pub async fn read_mcp_resource(