Move coordinator status derivation into bridge

This commit is contained in:
Meta Vens 2026-05-13 09:41:16 +01:00
commit 431c56f525
7 changed files with 221 additions and 339 deletions

View file

@ -156,48 +156,8 @@ use codex_app_server_protocol::ThreadCompactStartParams;
use codex_app_server_protocol::ThreadCompactStartResponse;
use codex_app_server_protocol::ThreadDecrementElicitationParams;
use codex_app_server_protocol::ThreadDecrementElicitationResponse;
use codex_app_server_protocol::ThreadEpiphanyContext;
use codex_app_server_protocol::ThreadEpiphanyContextParams;
use codex_app_server_protocol::ThreadEpiphanyContextStateStatus;
use codex_app_server_protocol::ThreadEpiphanyCoordinatorAction;
use codex_app_server_protocol::ThreadEpiphanyCoordinatorSignals;
use codex_app_server_protocol::ThreadEpiphanyCrrcAction;
use codex_app_server_protocol::ThreadEpiphanyCrrcRecommendation;
use codex_app_server_protocol::ThreadEpiphanyGraphFreshness;
use codex_app_server_protocol::ThreadEpiphanyGraphFreshnessStatus;
use codex_app_server_protocol::ThreadEpiphanyGraphQuery;
use codex_app_server_protocol::ThreadEpiphanyGraphQueryDirection;
use codex_app_server_protocol::ThreadEpiphanyGraphQueryKind;
use codex_app_server_protocol::ThreadEpiphanyInvalidationInput;
use codex_app_server_protocol::ThreadEpiphanyInvalidationStatus;
use codex_app_server_protocol::ThreadEpiphanyJob;
use codex_app_server_protocol::ThreadEpiphanyJobKind;
use codex_app_server_protocol::ThreadEpiphanyJobStatus;
use codex_app_server_protocol::ThreadEpiphanyPressure;
use codex_app_server_protocol::ThreadEpiphanyPressureLevel;
use codex_app_server_protocol::ThreadEpiphanyReorientAction;
use codex_app_server_protocol::ThreadEpiphanyReorientCheckpointStatus;
use codex_app_server_protocol::ThreadEpiphanyReorientDecision;
use codex_app_server_protocol::ThreadEpiphanyReorientFinding;
use codex_app_server_protocol::ThreadEpiphanyReorientReason;
use codex_app_server_protocol::ThreadEpiphanyReorientResultStatus;
use codex_app_server_protocol::ThreadEpiphanyReorientStateStatus;
use codex_app_server_protocol::ThreadEpiphanyRetrievalFreshness;
use codex_app_server_protocol::ThreadEpiphanyRetrievalFreshnessStatus;
#[cfg(test)]
use codex_app_server_protocol::ThreadEpiphanyRetrieveResultKind;
use codex_app_server_protocol::ThreadEpiphanyRoleId;
use codex_app_server_protocol::ThreadEpiphanyRoleLane;
use codex_app_server_protocol::ThreadEpiphanyRoleResultStatus;
use codex_app_server_protocol::ThreadEpiphanyRoleSelfPersistenceStatus;
use codex_app_server_protocol::ThreadEpiphanyRoleStatus;
use codex_app_server_protocol::ThreadEpiphanySceneAction;
use codex_app_server_protocol::ThreadEpiphanySceneGraph;
use codex_app_server_protocol::ThreadEpiphanySceneSource;
use codex_app_server_protocol::ThreadEpiphanySceneStateStatus;
use codex_app_server_protocol::ThreadEpiphanySceneStatusCount;
use codex_app_server_protocol::ThreadEpiphanyStateUpdatedField;
use codex_app_server_protocol::ThreadEpiphanyUpdatePatch;
use codex_app_server_protocol::ThreadForkParams;
use codex_app_server_protocol::ThreadForkResponse;
use codex_app_server_protocol::ThreadIncrementElicitationParams;
@ -367,13 +327,6 @@ use codex_protocol::protocol::ConversationAudioParams;
use codex_protocol::protocol::ConversationStartParams;
use codex_protocol::protocol::ConversationStartTransport;
use codex_protocol::protocol::ConversationTextParams;
use codex_protocol::protocol::EpiphanyAcceptanceReceipt;
use codex_protocol::protocol::EpiphanyEvidenceRecord;
use codex_protocol::protocol::EpiphanyInvestigationCheckpoint;
use codex_protocol::protocol::EpiphanyInvestigationDisposition;
use codex_protocol::protocol::EpiphanyRetrievalState;
use codex_protocol::protocol::EpiphanyRetrievalStatus;
use codex_protocol::protocol::EpiphanyThreadState;
use codex_protocol::protocol::EventMsg;
use codex_protocol::protocol::GitInfo as CoreGitInfo;
use codex_protocol::protocol::InitialHistory;
@ -388,7 +341,6 @@ use codex_protocol::protocol::ReviewTarget as CoreReviewTarget;
use codex_protocol::protocol::RolloutItem;
use codex_protocol::protocol::SessionConfiguredEvent;
use codex_protocol::protocol::SessionMetaLine;
use codex_protocol::protocol::TokenUsageInfo as CoreTokenUsageInfo;
use codex_protocol::protocol::TurnEnvironmentSelection;
use codex_protocol::protocol::USER_MESSAGE_BEGIN;
use codex_protocol::protocol::W3cTraceContext;
@ -416,59 +368,8 @@ use codex_thread_store::ThreadStoreError;
use codex_thread_store::UpdateThreadMetadataParams as StoreUpdateThreadMetadataParams;
use codex_utils_absolute_path::AbsolutePathBuf;
use codex_utils_pty::DEFAULT_OUTPUT_BYTES_CAP;
use epiphany_codex_bridge::context::map_epiphany_context;
use epiphany_codex_bridge::context::map_epiphany_graph_query;
use epiphany_codex_bridge::context::map_epiphany_planning;
use epiphany_codex_bridge::coordinator::EpiphanyCoordinatorAutomationAction;
use epiphany_codex_bridge::coordinator::EpiphanyCoordinatorDecision;
use epiphany_codex_bridge::coordinator::epiphany_role_finding_accepted_evidence_id;
use epiphany_codex_bridge::coordinator::epiphany_role_finding_already_accepted;
use epiphany_codex_bridge::coordinator::epiphany_verification_finding_covers_current_modeling;
use epiphany_codex_bridge::coordinator::implementation_evidence_after_role_finding;
use epiphany_codex_bridge::coordinator::map_epiphany_coordinator;
use epiphany_codex_bridge::coordinator::map_epiphany_coordinator_automation_action;
use epiphany_codex_bridge::coordinator::map_epiphany_crrc_recommendation;
use epiphany_codex_bridge::coordinator::map_epiphany_roles;
use epiphany_codex_bridge::coordinator::role_finding_accepted_after;
use epiphany_codex_bridge::coordinator::select_epiphany_coordinator_automation_action;
use epiphany_codex_bridge::jobs::map_epiphany_jobs;
use epiphany_codex_bridge::launch::EPIPHANY_IMAGINATION_OWNER_ROLE;
use epiphany_codex_bridge::launch::EPIPHANY_IMAGINATION_ROLE_BINDING_ID;
use epiphany_codex_bridge::launch::EPIPHANY_MODELING_OWNER_ROLE;
use epiphany_codex_bridge::launch::EPIPHANY_MODELING_ROLE_BINDING_ID;
use epiphany_codex_bridge::launch::EPIPHANY_REORIENT_LAUNCH_BINDING_ID;
use epiphany_codex_bridge::launch::EPIPHANY_REORIENT_OWNER_ROLE;
use epiphany_codex_bridge::launch::EPIPHANY_VERIFICATION_OWNER_ROLE;
use epiphany_codex_bridge::launch::EPIPHANY_VERIFICATION_ROLE_BINDING_ID;
use epiphany_codex_bridge::launch::build_epiphany_reorient_launch_instruction;
use epiphany_codex_bridge::launch::build_epiphany_reorient_launch_request;
use epiphany_codex_bridge::launch::build_epiphany_role_launch_request;
use epiphany_codex_bridge::launch::epiphany_reorient_launch_output_schema;
use epiphany_codex_bridge::launch::epiphany_role_launch_output_schema;
use epiphany_codex_bridge::launch::epiphany_specialist_prompt_config;
use epiphany_codex_bridge::launch::render_epiphany_coordinator_note;
use epiphany_codex_bridge::mutation::epiphany_imagination_finding_has_reviewable_state_patch;
use epiphany_codex_bridge::mutation::epiphany_modeling_finding_has_reviewable_state_patch;
use epiphany_codex_bridge::mutation::epiphany_promote_changed_fields;
use epiphany_codex_bridge::mutation::epiphany_update_patch_changed_fields;
use epiphany_codex_bridge::mutation::reorient_finding_code_refs;
use epiphany_codex_bridge::mutation::reorient_finding_investigation_checkpoint;
use epiphany_codex_bridge::mutation::reorient_finding_scratch;
use epiphany_codex_bridge::mutation::role_finding_summary;
use epiphany_codex_bridge::pressure::map_epiphany_pressure;
use epiphany_codex_bridge::pressure::render_epiphany_pre_compaction_checkpoint_intervention;
use epiphany_codex_bridge::pressure::should_run_epiphany_pre_compaction_checkpoint_intervention;
use epiphany_codex_bridge::reorient::EpiphanyFreshnessWatcherSnapshot;
use epiphany_codex_bridge::reorient::map_epiphany_freshness;
use epiphany_codex_bridge::reorient::map_epiphany_reorient;
use epiphany_codex_bridge::results::map_epiphany_reorient_finding;
use epiphany_codex_bridge::results::map_epiphany_role_finding;
use epiphany_codex_bridge::retrieve::map_epiphany_retrieve_index_summary;
use epiphany_codex_bridge::retrieve::map_epiphany_retrieve_response;
use epiphany_codex_bridge::runtime_results::load_epiphany_role_result_from_runtime_spine_job;
use epiphany_codex_bridge::scene::map_epiphany_scene;
use epiphany_codex_bridge::state::live_thread_epiphany_state;
use epiphany_core::EpiphanyWorkerLaunchDocument;
use std::collections::HashMap;
use std::collections::HashSet;
use std::io::Error as IoError;

View file

@ -1,9 +1,7 @@
use std::sync::Arc;
use codex_app_server_protocol::ServerNotification;
use codex_app_server_protocol::ThreadEpiphanyCoordinatorSignals;
use codex_app_server_protocol::ThreadEpiphanyReorientStateStatus;
use codex_app_server_protocol::ThreadEpiphanyRoleId;
use codex_app_server_protocol::ThreadEpiphanyStateUpdatedNotification;
use codex_app_server_protocol::ThreadEpiphanyStateUpdatedSource;
use codex_core::CodexThread;
@ -13,32 +11,21 @@ use codex_protocol::protocol::Op;
use codex_protocol::protocol::TokenUsageInfo as CoreTokenUsageInfo;
use codex_protocol::user_input::UserInput as CoreInputItem;
use epiphany_codex_bridge::coordinator::EpiphanyCoordinatorAutomationAction;
use epiphany_codex_bridge::coordinator::derive_epiphany_coordinator_status;
use epiphany_codex_bridge::coordinator::epiphany_reorient_finding_already_accepted;
use epiphany_codex_bridge::coordinator::epiphany_role_finding_already_accepted;
use epiphany_codex_bridge::coordinator::epiphany_role_finding_cites_implementation_evidence;
use epiphany_codex_bridge::coordinator::epiphany_verification_finding_allows_implementation;
use epiphany_codex_bridge::coordinator::epiphany_verification_finding_covers_current_modeling;
use epiphany_codex_bridge::coordinator::epiphany_verification_finding_needs_evidence;
use epiphany_codex_bridge::coordinator::implementation_evidence_after_role_finding;
use epiphany_codex_bridge::coordinator::map_epiphany_coordinator;
use epiphany_codex_bridge::coordinator::map_epiphany_crrc_recommendation;
use epiphany_codex_bridge::coordinator::map_epiphany_roles;
use epiphany_codex_bridge::coordinator::role_finding_accepted_after;
use epiphany_codex_bridge::coordinator::select_epiphany_coordinator_automation_action;
use epiphany_codex_bridge::jobs::map_epiphany_jobs;
use epiphany_codex_bridge::launch::EPIPHANY_MODELING_ROLE_BINDING_ID;
use epiphany_codex_bridge::launch::EPIPHANY_REORIENT_LAUNCH_BINDING_ID;
use epiphany_codex_bridge::launch::EPIPHANY_VERIFICATION_ROLE_BINDING_ID;
use epiphany_codex_bridge::launch::build_epiphany_reorient_launch_request;
use epiphany_codex_bridge::mutation::epiphany_job_launch_changed_fields;
use epiphany_codex_bridge::mutation::epiphany_modeling_finding_has_reviewable_state_patch;
use epiphany_codex_bridge::pressure::map_epiphany_pressure;
use epiphany_codex_bridge::pressure::render_epiphany_pre_compaction_checkpoint_intervention;
use epiphany_codex_bridge::pressure::should_run_epiphany_pre_compaction_checkpoint_intervention;
use epiphany_codex_bridge::reorient::map_epiphany_freshness;
use epiphany_codex_bridge::reorient::map_epiphany_reorient;
use epiphany_codex_bridge::runtime_results::load_epiphany_reorient_result_snapshot;
use epiphany_codex_bridge::runtime_results::load_epiphany_role_result_snapshot;
use epiphany_codex_bridge::state::client_visible_live_thread_epiphany_state;
use tokio::sync::Mutex;
use tracing::warn;
@ -116,82 +103,24 @@ pub(crate) async fn maybe_run_epiphany_coordinator_automation_for_turn_boundary(
reorient_result_status,
reorient_job.as_ref(),
);
let (modeling_result_status, modeling_finding, _) = load_epiphany_role_result_snapshot(
&state,
let coordinator = derive_epiphany_coordinator_status(
Some(&state),
Some(runtime_store_path.as_path()),
ThreadEpiphanyRoleId::Modeling,
EPIPHANY_MODELING_ROLE_BINDING_ID,
)
.await;
let modeling_result_accepted = modeling_finding
.as_ref()
.is_some_and(|finding| epiphany_role_finding_already_accepted(&state, finding));
let modeling_result_reviewable = modeling_finding
.as_ref()
.is_some_and(epiphany_modeling_finding_has_reviewable_state_patch);
let (verification_result_status, verification_finding, _) = load_epiphany_role_result_snapshot(
&state,
Some(runtime_store_path.as_path()),
ThreadEpiphanyRoleId::Verification,
EPIPHANY_VERIFICATION_ROLE_BINDING_ID,
)
.await;
let verification_result_accepted = verification_finding
.as_ref()
.is_some_and(|finding| epiphany_role_finding_already_accepted(&state, finding));
let verification_result_covers_current_modeling =
epiphany_verification_finding_covers_current_modeling(
&state,
modeling_result_accepted,
modeling_finding.as_ref(),
verification_finding.as_ref(),
);
let modeling_result_accepted_after_verification = role_finding_accepted_after(
&state,
modeling_finding.as_ref(),
verification_finding.as_ref(),
);
let implementation_evidence_after_verification =
implementation_evidence_after_role_finding(&state, verification_finding.as_ref());
let verification_result_cites_implementation_evidence =
epiphany_role_finding_cites_implementation_evidence(&state, verification_finding.as_ref());
let verification_result_allows_implementation = verification_result_accepted
&& verification_finding
.as_ref()
.is_some_and(epiphany_verification_finding_allows_implementation);
let verification_result_needs_evidence = verification_result_accepted
&& verification_finding
.as_ref()
.is_some_and(epiphany_verification_finding_needs_evidence);
let source_signals = ThreadEpiphanyCoordinatorSignals {
pressure_level: pressure.level,
should_prepare_compaction: pressure.should_prepare_compaction,
reorient_action: reorient_decision.action,
crrc_action: crrc_recommendation.action,
modeling_result_status,
verification_result_status,
reorient_result_status,
};
let coordinator = map_epiphany_coordinator(
state_status,
state.investigation_checkpoint.as_ref().is_some(),
&pressure,
&crrc_recommendation,
&roles,
&source_signals,
modeling_result_accepted,
modeling_result_reviewable,
modeling_result_accepted_after_verification,
implementation_evidence_after_verification,
verification_result_cites_implementation_evidence,
verification_result_covers_current_modeling,
verification_result_accepted,
verification_result_allows_implementation,
verification_result_needs_evidence,
reorient_finding_accepted,
);
roles,
Some(&reorient_decision),
reorient_result_status,
reorient_finding.as_ref(),
state.investigation_checkpoint.as_ref().is_some(),
)
.await;
match select_epiphany_coordinator_automation_action(&coordinator, force_checkpoint_compaction) {
match select_epiphany_coordinator_automation_action(
&coordinator.decision,
force_checkpoint_compaction,
) {
EpiphanyCoordinatorAutomationAction::None => {}
EpiphanyCoordinatorAutomationAction::CompactRehydrateReorient => {
if let Err(err) = thread.submit(Op::Compact).await {

View file

@ -9,25 +9,15 @@ use codex_protocol::ThreadId;
use epiphany_codex_bridge::context::map_epiphany_context;
use epiphany_codex_bridge::context::map_epiphany_graph_query;
use epiphany_codex_bridge::context::map_epiphany_planning;
use epiphany_codex_bridge::coordinator::derive_epiphany_coordinator_status;
use epiphany_codex_bridge::coordinator::epiphany_reorient_finding_already_accepted;
use epiphany_codex_bridge::coordinator::epiphany_role_finding_already_accepted;
use epiphany_codex_bridge::coordinator::epiphany_role_finding_cites_implementation_evidence;
use epiphany_codex_bridge::coordinator::epiphany_verification_finding_allows_implementation;
use epiphany_codex_bridge::coordinator::epiphany_verification_finding_covers_current_modeling;
use epiphany_codex_bridge::coordinator::epiphany_verification_finding_needs_evidence;
use epiphany_codex_bridge::coordinator::implementation_evidence_after_role_finding;
use epiphany_codex_bridge::coordinator::map_epiphany_coordinator;
use epiphany_codex_bridge::coordinator::map_epiphany_coordinator_view;
use epiphany_codex_bridge::coordinator::map_epiphany_crrc_recommendation;
use epiphany_codex_bridge::coordinator::map_epiphany_roles;
use epiphany_codex_bridge::coordinator::render_epiphany_roles_note;
use epiphany_codex_bridge::coordinator::role_finding_accepted_after;
use epiphany_codex_bridge::jobs::map_epiphany_jobs;
use epiphany_codex_bridge::launch::EPIPHANY_MODELING_ROLE_BINDING_ID;
use epiphany_codex_bridge::launch::EPIPHANY_REORIENT_LAUNCH_BINDING_ID;
use epiphany_codex_bridge::launch::EPIPHANY_VERIFICATION_ROLE_BINDING_ID;
use epiphany_codex_bridge::launch::epiphany_role_binding_id;
use epiphany_codex_bridge::launch::render_epiphany_coordinator_note;
use epiphany_codex_bridge::mutation::epiphany_modeling_finding_has_reviewable_state_patch;
use epiphany_codex_bridge::pressure::map_epiphany_pressure;
use epiphany_codex_bridge::reorient::map_epiphany_freshness;
use epiphany_codex_bridge::reorient::map_epiphany_reorient;
@ -256,149 +246,26 @@ impl CodexMessageProcessor {
if let (Some(pressure), Some(recommendation), Some(roles)) =
(pressure.as_ref(), recommendation.as_ref(), roles.clone())
{
let (modeling_result_status, modeling_finding, _) =
if let Some(state) = thread.epiphany_state.as_ref() {
load_epiphany_role_result_snapshot(
state,
runtime_store_path.as_deref(),
ThreadEpiphanyRoleId::Modeling,
EPIPHANY_MODELING_ROLE_BINDING_ID,
)
.await
} else {
(
ThreadEpiphanyRoleResultStatus::MissingState,
None,
"No authoritative Epiphany state exists for this thread.".to_string(),
)
};
let modeling_result_accepted = modeling_finding.as_ref().is_some_and(|finding| {
thread
.epiphany_state
.as_ref()
.is_some_and(|state| epiphany_role_finding_already_accepted(state, finding))
});
let modeling_result_reviewable = modeling_finding
.as_ref()
.is_some_and(epiphany_modeling_finding_has_reviewable_state_patch);
let (verification_result_status, verification_finding, _) =
if let Some(state) = thread.epiphany_state.as_ref() {
load_epiphany_role_result_snapshot(
state,
runtime_store_path.as_deref(),
ThreadEpiphanyRoleId::Verification,
EPIPHANY_VERIFICATION_ROLE_BINDING_ID,
)
.await
} else {
(
ThreadEpiphanyRoleResultStatus::MissingState,
None,
"No authoritative Epiphany state exists for this thread.".to_string(),
)
};
let verification_result_accepted =
verification_finding.as_ref().is_some_and(|finding| {
thread.epiphany_state.as_ref().is_some_and(|state| {
epiphany_role_finding_already_accepted(state, finding)
})
});
let verification_result_covers_current_modeling =
thread.epiphany_state.as_ref().is_none_or(|state| {
epiphany_verification_finding_covers_current_modeling(
state,
modeling_result_accepted,
modeling_finding.as_ref(),
verification_finding.as_ref(),
)
});
let modeling_result_accepted_after_verification =
thread.epiphany_state.as_ref().is_some_and(|state| {
role_finding_accepted_after(
state,
modeling_finding.as_ref(),
verification_finding.as_ref(),
)
});
let implementation_evidence_after_verification =
thread.epiphany_state.as_ref().is_some_and(|state| {
implementation_evidence_after_role_finding(
state,
verification_finding.as_ref(),
)
});
let verification_result_cites_implementation_evidence =
thread.epiphany_state.as_ref().is_some_and(|state| {
epiphany_role_finding_cites_implementation_evidence(
state,
verification_finding.as_ref(),
)
});
let verification_result_allows_implementation = verification_result_accepted
&& verification_finding
.as_ref()
.is_some_and(epiphany_verification_finding_allows_implementation);
let verification_result_needs_evidence = verification_result_accepted
&& verification_finding
.as_ref()
.is_some_and(epiphany_verification_finding_needs_evidence);
let source_signals = ThreadEpiphanyCoordinatorSignals {
pressure_level: pressure.level,
should_prepare_compaction: pressure.should_prepare_compaction,
reorient_action: reorient_decision
.as_ref()
.map(|decision| decision.action)
.unwrap_or(ThreadEpiphanyReorientAction::Resume),
crrc_action: recommendation.action,
modeling_result_status,
verification_result_status,
reorient_result_status,
};
let coordinator = map_epiphany_coordinator(
let status = derive_epiphany_coordinator_status(
thread.epiphany_state.as_ref(),
runtime_store_path.as_deref(),
reorient_state_status,
checkpoint_present,
pressure,
recommendation,
&roles,
&source_signals,
modeling_result_accepted,
modeling_result_reviewable,
modeling_result_accepted_after_verification,
implementation_evidence_after_verification,
verification_result_cites_implementation_evidence,
verification_result_covers_current_modeling,
verification_result_accepted,
verification_result_allows_implementation,
verification_result_needs_evidence,
reorient_finding_accepted,
);
let note = render_epiphany_coordinator_note(
recommendation.action,
pressure.level,
modeling_result_status,
verification_result_status,
reorient_result_status,
coordinator.action,
);
Some(ThreadEpiphanyViewCoordinator {
thread_id: thread_id.clone(),
source: if loaded {
ThreadEpiphanyRolesSource::Live
} else {
ThreadEpiphanyRolesSource::Stored
},
state_status: reorient_state_status,
state_revision,
action: coordinator.action,
target_role: coordinator.target_role,
recommended_scene_action: coordinator.recommended_scene_action,
requires_review: coordinator.requires_review,
can_auto_run: coordinator.can_auto_run,
reason: coordinator.reason,
source_signals,
roles,
note,
})
reorient_decision.as_ref(),
reorient_result_status,
reorient_finding.as_ref(),
checkpoint_present,
)
.await;
Some(map_epiphany_coordinator_view(
thread_id.clone(),
loaded,
reorient_state_status,
state_revision,
status,
))
} else {
None
}