2026-05-13 01:33:32 +01:00
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 ::ThreadEpiphanyJob ;
use codex_app_server_protocol ::ThreadEpiphanyReorientAction ;
use codex_app_server_protocol ::ThreadEpiphanyReorientResultStatus ;
use codex_app_server_protocol ::ThreadEpiphanyReorientStateStatus ;
use codex_app_server_protocol ::ThreadEpiphanyRoleId ;
use codex_app_server_protocol ::ThreadEpiphanyRoleLane ;
use codex_app_server_protocol ::ThreadEpiphanyRoleResultStatus ;
use codex_app_server_protocol ::ThreadEpiphanyRoleStatus ;
2026-05-13 09:41:16 +01:00
use codex_app_server_protocol ::ThreadEpiphanyRolesSource ;
2026-05-13 01:33:32 +01:00
use codex_app_server_protocol ::ThreadEpiphanySceneAction ;
2026-05-13 09:41:16 +01:00
use codex_app_server_protocol ::ThreadEpiphanyViewCoordinator ;
2026-05-13 01:33:32 +01:00
use epiphany_core ::EpiphanyCoordinatorAction as CoreEpiphanyCoordinatorAction ;
use epiphany_core ::EpiphanyCoordinatorAutomationAction as CoreEpiphanyCoordinatorAutomationAction ;
use epiphany_core ::EpiphanyCoordinatorCrrcRecommendation ;
use epiphany_core ::EpiphanyCoordinatorDecision as CoreEpiphanyCoordinatorDecision ;
use epiphany_core ::EpiphanyCoordinatorInput ;
use epiphany_core ::EpiphanyCoordinatorRoleId as CoreEpiphanyCoordinatorRoleId ;
use epiphany_core ::EpiphanyCoordinatorRoleLane as CoreEpiphanyCoordinatorRoleLane ;
use epiphany_core ::EpiphanyCoordinatorRoleResultStatus as CoreEpiphanyCoordinatorRoleResultStatus ;
use epiphany_core ::EpiphanyCoordinatorRoleStatus as CoreEpiphanyCoordinatorRoleStatus ;
use epiphany_core ::EpiphanyCoordinatorSceneAction as CoreEpiphanyCoordinatorSceneAction ;
use epiphany_core ::EpiphanyCoordinatorSignals ;
use epiphany_core ::EpiphanyCrrcAction as CoreEpiphanyCrrcAction ;
use epiphany_core ::EpiphanyCrrcInput ;
use epiphany_core ::EpiphanyCrrcRecommendation as CoreEpiphanyCrrcRecommendation ;
use epiphany_core ::EpiphanyCrrcReorientAction as CoreEpiphanyCrrcReorientAction ;
use epiphany_core ::EpiphanyCrrcResultStatus as CoreEpiphanyCrrcResultStatus ;
use epiphany_core ::EpiphanyCrrcSceneAction as CoreEpiphanyCrrcSceneAction ;
use epiphany_core ::EpiphanyCrrcStateStatus as CoreEpiphanyCrrcStateStatus ;
2026-05-17 21:01:26 +01:00
use epiphany_core ::EpiphanyJobLaunchRequest ;
2026-05-20 02:19:06 +01:00
use epiphany_core ::EpiphanyJobStatus as CoreEpiphanyJobStatus ;
use epiphany_core ::EpiphanyJobView ;
2026-05-19 21:51:42 +01:00
use epiphany_core ::EpiphanyReorientAction as CoreEpiphanyReorientAction ;
2026-05-19 22:11:58 +01:00
use epiphany_core ::EpiphanyReorientDecision as CoreEpiphanyReorientDecision ;
2026-05-20 01:32:32 +01:00
use epiphany_core ::EpiphanyReorientFindingInterpretation as CoreEpiphanyReorientFinding ;
2026-05-19 22:11:58 +01:00
use epiphany_core ::EpiphanyReorientStateStatus as CoreEpiphanyReorientStateStatus ;
2026-05-13 01:33:32 +01:00
use epiphany_core ::EpiphanyRoleBoardCheckpointSummary ;
use epiphany_core ::EpiphanyRoleBoardInput ;
use epiphany_core ::EpiphanyRoleBoardJob ;
use epiphany_core ::EpiphanyRoleBoardJobStatus ;
use epiphany_core ::EpiphanyRoleBoardLane ;
use epiphany_core ::EpiphanyRoleBoardPlanningSummary ;
2026-05-20 01:32:32 +01:00
use epiphany_core ::EpiphanyRoleFindingInterpretation as CoreEpiphanyRoleFinding ;
2026-05-19 15:33:45 +01:00
use epiphany_core ::EpiphanyTokenUsageSnapshot ;
2026-05-13 01:33:32 +01:00
use epiphany_core ::coordinator_automation_action ;
use epiphany_core ::derive_role_board ;
2026-05-20 01:32:32 +01:00
use epiphany_core ::modeling_role_state_patch_policy_errors ;
2026-05-13 01:33:32 +01:00
use epiphany_core ::recommend_coordinator_action ;
use epiphany_core ::recommend_crrc_action ;
use epiphany_core ::render_role_board_note ;
use epiphany_core ::select_coordinator_automation_action ;
2026-05-19 15:24:57 +01:00
use epiphany_state_model ::EpiphanyAcceptanceReceipt ;
2026-05-20 02:19:06 +01:00
use epiphany_state_model ::EpiphanyJobKind as CoreEpiphanyJobKind ;
2026-05-19 15:24:57 +01:00
use epiphany_state_model ::EpiphanyRetrievalState ;
use epiphany_state_model ::EpiphanyThreadState ;
2026-05-13 01:33:32 +01:00
2026-05-20 02:19:06 +01:00
use crate ::jobs ::map_core_epiphany_job_view ;
2026-05-13 03:07:24 +01:00
use crate ::launch ::EPIPHANY_IMAGINATION_OWNER_ROLE ;
use crate ::launch ::EPIPHANY_IMAGINATION_ROLE_BINDING_ID ;
use crate ::launch ::EPIPHANY_MODELING_ROLE_BINDING_ID ;
2026-05-14 08:49:03 +01:00
use crate ::launch ::EPIPHANY_REORIENT_LAUNCH_BINDING_ID ;
2026-05-13 03:07:24 +01:00
use crate ::launch ::EPIPHANY_REORIENT_OWNER_ROLE ;
use crate ::launch ::EPIPHANY_VERIFICATION_ROLE_BINDING_ID ;
2026-05-14 08:49:03 +01:00
use crate ::launch ::build_epiphany_reorient_launch_request ;
2026-05-13 09:41:16 +01:00
use crate ::launch ::render_epiphany_coordinator_note ;
2026-05-19 16:47:28 +01:00
use crate ::pressure ::derive_epiphany_pressure ;
2026-05-14 08:49:03 +01:00
use crate ::reorient ::EpiphanyFreshnessWatcherSnapshot ;
2026-05-19 16:58:37 +01:00
use crate ::reorient ::derive_epiphany_freshness_view ;
2026-05-19 17:05:26 +01:00
use crate ::reorient ::derive_epiphany_reorient ;
2026-05-20 01:32:32 +01:00
use crate ::runtime_results ::load_core_epiphany_reorient_result_snapshot ;
use crate ::runtime_results ::load_core_epiphany_role_result_snapshot ;
2026-05-13 01:33:32 +01:00
use std ::collections ::HashSet ;
2026-05-13 09:41:16 +01:00
use std ::path ::Path ;
2026-05-13 01:33:32 +01:00
2026-05-13 03:07:24 +01:00
pub fn map_epiphany_crrc_recommendation (
2026-05-13 01:33:32 +01:00
loaded : bool ,
2026-05-19 22:11:58 +01:00
state_status : CoreEpiphanyReorientStateStatus ,
2026-05-19 21:51:42 +01:00
pressure : & epiphany_core ::EpiphanyPressure ,
2026-05-19 22:11:58 +01:00
decision : & CoreEpiphanyReorientDecision ,
result_status : CoreEpiphanyCrrcResultStatus ,
2026-05-13 01:33:32 +01:00
checkpoint_present : bool ,
finding_present : bool ,
finding_accepted : bool ,
2026-05-19 21:58:29 +01:00
) -> CoreEpiphanyCrrcRecommendation {
recommend_crrc_action ( EpiphanyCrrcInput {
2026-05-13 01:33:32 +01:00
loaded ,
2026-05-19 22:11:58 +01:00
state_status : map_core_crrc_state_status_from_reorient ( state_status ) ,
2026-05-13 01:33:32 +01:00
should_prepare_compaction : pressure . should_prepare_compaction ,
reorient_action : map_core_crrc_reorient_action ( decision . action ) ,
2026-05-19 22:11:58 +01:00
result_status ,
2026-05-13 01:33:32 +01:00
checkpoint_present ,
finding_present ,
finding_accepted ,
2026-05-19 21:58:29 +01:00
} )
2026-05-13 01:33:32 +01:00
}
2026-05-19 22:11:58 +01:00
fn map_core_crrc_state_status_from_reorient (
status : CoreEpiphanyReorientStateStatus ,
2026-05-13 01:33:32 +01:00
) -> CoreEpiphanyCrrcStateStatus {
match status {
2026-05-19 22:11:58 +01:00
CoreEpiphanyReorientStateStatus ::Missing = > CoreEpiphanyCrrcStateStatus ::Missing ,
CoreEpiphanyReorientStateStatus ::Ready = > CoreEpiphanyCrrcStateStatus ::Ready ,
2026-05-13 01:33:32 +01:00
}
}
fn map_core_crrc_reorient_action (
2026-05-19 22:11:58 +01:00
action : CoreEpiphanyReorientAction ,
2026-05-13 01:33:32 +01:00
) -> CoreEpiphanyCrrcReorientAction {
match action {
2026-05-19 22:11:58 +01:00
CoreEpiphanyReorientAction ::Resume = > CoreEpiphanyCrrcReorientAction ::Resume ,
CoreEpiphanyReorientAction ::Regather = > CoreEpiphanyCrrcReorientAction ::Regather ,
2026-05-19 21:51:42 +01:00
}
}
2026-05-19 22:11:58 +01:00
pub fn map_core_crrc_result_status (
2026-05-13 01:33:32 +01:00
status : ThreadEpiphanyReorientResultStatus ,
) -> CoreEpiphanyCrrcResultStatus {
match status {
ThreadEpiphanyReorientResultStatus ::MissingState = > {
CoreEpiphanyCrrcResultStatus ::MissingState
}
ThreadEpiphanyReorientResultStatus ::MissingBinding = > {
CoreEpiphanyCrrcResultStatus ::MissingBinding
}
ThreadEpiphanyReorientResultStatus ::BackendUnavailable = > {
CoreEpiphanyCrrcResultStatus ::BackendUnavailable
}
ThreadEpiphanyReorientResultStatus ::BackendMissing = > {
CoreEpiphanyCrrcResultStatus ::BackendMissing
}
ThreadEpiphanyReorientResultStatus ::Pending = > CoreEpiphanyCrrcResultStatus ::Pending ,
ThreadEpiphanyReorientResultStatus ::Running = > CoreEpiphanyCrrcResultStatus ::Running ,
ThreadEpiphanyReorientResultStatus ::Completed = > CoreEpiphanyCrrcResultStatus ::Completed ,
ThreadEpiphanyReorientResultStatus ::Failed = > CoreEpiphanyCrrcResultStatus ::Failed ,
ThreadEpiphanyReorientResultStatus ::Cancelled = > CoreEpiphanyCrrcResultStatus ::Cancelled ,
}
}
2026-05-19 21:51:42 +01:00
fn map_protocol_coordinator_source_signals (
signals : EpiphanyCoordinatorSourceSignals ,
) -> ThreadEpiphanyCoordinatorSignals {
ThreadEpiphanyCoordinatorSignals {
pressure_level : map_protocol_pressure_level ( signals . pressure_level ) ,
should_prepare_compaction : signals . should_prepare_compaction ,
reorient_action : map_protocol_reorient_action ( signals . reorient_action ) ,
2026-05-19 21:58:29 +01:00
crrc_action : map_protocol_crrc_action ( signals . crrc_action ) ,
2026-05-19 21:51:42 +01:00
modeling_result_status : map_protocol_coordinator_role_result_status (
signals . modeling_result_status ,
) ,
verification_result_status : map_protocol_coordinator_role_result_status (
signals . verification_result_status ,
) ,
reorient_result_status : map_protocol_crrc_result_status ( signals . reorient_result_status ) ,
}
}
fn map_protocol_pressure_level (
level : epiphany_core ::EpiphanyPressureLevel ,
) -> codex_app_server_protocol ::ThreadEpiphanyPressureLevel {
match level {
epiphany_core ::EpiphanyPressureLevel ::Unknown = > {
codex_app_server_protocol ::ThreadEpiphanyPressureLevel ::Unknown
}
epiphany_core ::EpiphanyPressureLevel ::Low = > {
codex_app_server_protocol ::ThreadEpiphanyPressureLevel ::Low
}
epiphany_core ::EpiphanyPressureLevel ::Elevated = > {
codex_app_server_protocol ::ThreadEpiphanyPressureLevel ::Elevated
}
epiphany_core ::EpiphanyPressureLevel ::High = > {
codex_app_server_protocol ::ThreadEpiphanyPressureLevel ::High
}
epiphany_core ::EpiphanyPressureLevel ::Critical = > {
codex_app_server_protocol ::ThreadEpiphanyPressureLevel ::Critical
}
}
}
fn map_protocol_reorient_action (
action : CoreEpiphanyReorientAction ,
) -> ThreadEpiphanyReorientAction {
match action {
CoreEpiphanyReorientAction ::Resume = > ThreadEpiphanyReorientAction ::Resume ,
CoreEpiphanyReorientAction ::Regather = > ThreadEpiphanyReorientAction ::Regather ,
}
}
fn map_protocol_crrc_result_status (
status : CoreEpiphanyCrrcResultStatus ,
) -> ThreadEpiphanyReorientResultStatus {
match status {
CoreEpiphanyCrrcResultStatus ::MissingState = > {
ThreadEpiphanyReorientResultStatus ::MissingState
}
CoreEpiphanyCrrcResultStatus ::MissingBinding = > {
ThreadEpiphanyReorientResultStatus ::MissingBinding
}
CoreEpiphanyCrrcResultStatus ::BackendUnavailable = > {
ThreadEpiphanyReorientResultStatus ::BackendUnavailable
}
CoreEpiphanyCrrcResultStatus ::BackendMissing = > {
ThreadEpiphanyReorientResultStatus ::BackendMissing
}
CoreEpiphanyCrrcResultStatus ::Pending = > ThreadEpiphanyReorientResultStatus ::Pending ,
CoreEpiphanyCrrcResultStatus ::Running = > ThreadEpiphanyReorientResultStatus ::Running ,
CoreEpiphanyCrrcResultStatus ::Completed = > ThreadEpiphanyReorientResultStatus ::Completed ,
CoreEpiphanyCrrcResultStatus ::Failed = > ThreadEpiphanyReorientResultStatus ::Failed ,
CoreEpiphanyCrrcResultStatus ::Cancelled = > ThreadEpiphanyReorientResultStatus ::Cancelled ,
}
}
2026-05-19 21:58:29 +01:00
pub fn map_protocol_crrc_action ( action : CoreEpiphanyCrrcAction ) -> ThreadEpiphanyCrrcAction {
2026-05-13 01:33:32 +01:00
match action {
CoreEpiphanyCrrcAction ::Continue = > ThreadEpiphanyCrrcAction ::Continue ,
CoreEpiphanyCrrcAction ::PrepareCheckpoint = > ThreadEpiphanyCrrcAction ::PrepareCheckpoint ,
CoreEpiphanyCrrcAction ::LaunchReorientWorker = > {
ThreadEpiphanyCrrcAction ::LaunchReorientWorker
}
CoreEpiphanyCrrcAction ::WaitForReorientWorker = > {
ThreadEpiphanyCrrcAction ::WaitForReorientWorker
}
CoreEpiphanyCrrcAction ::ReviewReorientResult = > {
ThreadEpiphanyCrrcAction ::ReviewReorientResult
}
CoreEpiphanyCrrcAction ::AcceptReorientResult = > {
ThreadEpiphanyCrrcAction ::AcceptReorientResult
}
CoreEpiphanyCrrcAction ::RegatherManually = > ThreadEpiphanyCrrcAction ::RegatherManually ,
}
}
fn map_core_crrc_scene_action ( action : CoreEpiphanyCrrcSceneAction ) -> ThreadEpiphanySceneAction {
match action {
CoreEpiphanyCrrcSceneAction ::Update = > ThreadEpiphanySceneAction ::Update ,
CoreEpiphanyCrrcSceneAction ::Reorient = > ThreadEpiphanySceneAction ::Reorient ,
CoreEpiphanyCrrcSceneAction ::ReorientLaunch = > ThreadEpiphanySceneAction ::ReorientLaunch ,
CoreEpiphanyCrrcSceneAction ::ReorientResult = > ThreadEpiphanySceneAction ::ReorientResult ,
CoreEpiphanyCrrcSceneAction ::ReorientAccept = > ThreadEpiphanySceneAction ::ReorientAccept ,
}
}
2026-05-19 21:58:29 +01:00
pub fn map_protocol_crrc_recommendation (
2026-05-13 01:33:32 +01:00
recommendation : CoreEpiphanyCrrcRecommendation ,
) -> ThreadEpiphanyCrrcRecommendation {
ThreadEpiphanyCrrcRecommendation {
2026-05-19 21:58:29 +01:00
action : map_protocol_crrc_action ( recommendation . action ) ,
2026-05-13 01:33:32 +01:00
recommended_scene_action : recommendation
. recommended_scene_action
. map ( map_core_crrc_scene_action ) ,
reason : recommendation . reason ,
}
}
2026-05-19 21:51:42 +01:00
pub type EpiphanyCoordinatorDecision = CoreEpiphanyCoordinatorDecision ;
pub struct EpiphanyCoordinatorSourceSignals {
pub pressure_level : epiphany_core ::EpiphanyPressureLevel ,
pub should_prepare_compaction : bool ,
pub reorient_action : CoreEpiphanyReorientAction ,
pub crrc_action : CoreEpiphanyCrrcAction ,
pub modeling_result_status : CoreEpiphanyCoordinatorRoleResultStatus ,
pub verification_result_status : CoreEpiphanyCoordinatorRoleResultStatus ,
pub reorient_result_status : CoreEpiphanyCrrcResultStatus ,
2026-05-13 01:33:32 +01:00
}
2026-05-13 09:41:16 +01:00
pub struct EpiphanyCoordinatorStatus {
pub decision : EpiphanyCoordinatorDecision ,
2026-05-19 21:51:42 +01:00
pub source_signals : EpiphanyCoordinatorSourceSignals ,
2026-05-19 22:06:09 +01:00
pub roles : Vec < EpiphanyRoleBoardLane > ,
2026-05-13 09:41:16 +01:00
pub note : String ,
pub modeling_result_accepted : bool ,
pub verification_result_accepted : bool ,
pub reorient_finding_accepted : bool ,
}
pub async fn derive_epiphany_coordinator_status (
state : Option < & EpiphanyThreadState > ,
runtime_store_path : Option < & Path > ,
2026-05-19 22:11:58 +01:00
state_status : CoreEpiphanyReorientStateStatus ,
2026-05-19 21:51:42 +01:00
pressure : & epiphany_core ::EpiphanyPressure ,
2026-05-19 21:58:29 +01:00
recommendation : & CoreEpiphanyCrrcRecommendation ,
2026-05-19 22:06:09 +01:00
roles : Vec < EpiphanyRoleBoardLane > ,
2026-05-19 22:11:58 +01:00
reorient_decision : Option < & CoreEpiphanyReorientDecision > ,
2026-05-13 09:41:16 +01:00
reorient_result_status : ThreadEpiphanyReorientResultStatus ,
2026-05-20 01:32:32 +01:00
reorient_finding : Option < & CoreEpiphanyReorientFinding > ,
2026-05-13 09:41:16 +01:00
checkpoint_present : bool ,
) -> EpiphanyCoordinatorStatus {
2026-05-20 01:32:32 +01:00
let ( modeling_result_status , modeling_finding ) = if let Some ( state ) = state {
let snapshot = load_core_epiphany_role_result_snapshot (
2026-05-13 09:41:16 +01:00
state ,
runtime_store_path ,
ThreadEpiphanyRoleId ::Modeling ,
EPIPHANY_MODELING_ROLE_BINDING_ID ,
)
2026-05-20 01:32:32 +01:00
. await ;
( snapshot . status , snapshot . finding )
2026-05-13 09:41:16 +01:00
} else {
2026-05-20 01:32:32 +01:00
( CoreEpiphanyCoordinatorRoleResultStatus ::MissingState , None )
2026-05-13 09:41:16 +01:00
} ;
let modeling_result_accepted = modeling_finding . as_ref ( ) . is_some_and ( | finding | {
2026-05-20 01:32:32 +01:00
state . is_some_and ( | state | {
epiphany_role_finding_already_accepted (
state ,
CoreEpiphanyCoordinatorRoleId ::Modeling ,
finding ,
)
} )
2026-05-13 09:41:16 +01:00
} ) ;
let modeling_result_reviewable = modeling_finding
. as_ref ( )
. is_some_and ( epiphany_modeling_finding_has_reviewable_state_patch ) ;
2026-05-20 01:32:32 +01:00
let ( verification_result_status , verification_finding ) = if let Some ( state ) = state {
let snapshot = load_core_epiphany_role_result_snapshot (
2026-05-13 09:41:16 +01:00
state ,
runtime_store_path ,
ThreadEpiphanyRoleId ::Verification ,
EPIPHANY_VERIFICATION_ROLE_BINDING_ID ,
)
2026-05-20 01:32:32 +01:00
. await ;
( snapshot . status , snapshot . finding )
2026-05-13 09:41:16 +01:00
} else {
2026-05-20 01:32:32 +01:00
( CoreEpiphanyCoordinatorRoleResultStatus ::MissingState , None )
2026-05-13 09:41:16 +01:00
} ;
let verification_result_accepted = verification_finding . as_ref ( ) . is_some_and ( | finding | {
2026-05-20 01:32:32 +01:00
state . is_some_and ( | state | {
epiphany_role_finding_already_accepted (
state ,
CoreEpiphanyCoordinatorRoleId ::Verification ,
finding ,
)
} )
2026-05-13 09:41:16 +01:00
} ) ;
let verification_result_covers_current_modeling = state . 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 = state . is_some_and ( | state | {
role_finding_accepted_after (
state ,
2026-05-20 01:32:32 +01:00
CoreEpiphanyCoordinatorRoleId ::Modeling ,
2026-05-13 09:41:16 +01:00
modeling_finding . as_ref ( ) ,
2026-05-20 01:32:32 +01:00
CoreEpiphanyCoordinatorRoleId ::Verification ,
2026-05-13 09:41:16 +01:00
verification_finding . as_ref ( ) ,
)
} ) ;
let implementation_evidence_after_verification = state . is_some_and ( | state | {
2026-05-20 01:32:32 +01:00
implementation_evidence_after_role_finding (
state ,
CoreEpiphanyCoordinatorRoleId ::Verification ,
verification_finding . as_ref ( ) ,
)
2026-05-13 09:41:16 +01:00
} ) ;
let verification_result_cites_implementation_evidence = state . 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 reorient_finding_accepted = reorient_finding . is_some_and ( | finding | {
state . is_some_and ( | state | epiphany_reorient_finding_already_accepted ( state , finding ) )
} ) ;
2026-05-19 21:51:42 +01:00
let source_signals = EpiphanyCoordinatorSourceSignals {
2026-05-13 09:41:16 +01:00
pressure_level : pressure . level ,
should_prepare_compaction : pressure . should_prepare_compaction ,
reorient_action : reorient_decision
. map ( | decision | decision . action )
2026-05-19 21:51:42 +01:00
. unwrap_or ( CoreEpiphanyReorientAction ::Resume ) ,
2026-05-19 21:58:29 +01:00
crrc_action : recommendation . action ,
2026-05-20 01:32:32 +01:00
modeling_result_status ,
verification_result_status ,
2026-05-19 21:51:42 +01:00
reorient_result_status : map_core_crrc_result_status ( reorient_result_status ) ,
2026-05-13 09:41:16 +01:00
} ;
let decision = map_epiphany_coordinator (
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 (
2026-05-19 21:58:29 +01:00
map_protocol_crrc_action ( recommendation . action ) ,
2026-05-19 21:51:42 +01:00
map_protocol_pressure_level ( pressure . level ) ,
2026-05-20 01:32:32 +01:00
map_protocol_coordinator_role_result_status ( modeling_result_status ) ,
map_protocol_coordinator_role_result_status ( verification_result_status ) ,
2026-05-13 09:41:16 +01:00
reorient_result_status ,
2026-05-19 21:51:42 +01:00
map_protocol_coordinator_action ( decision . action ) ,
2026-05-13 09:41:16 +01:00
) ;
EpiphanyCoordinatorStatus {
decision ,
source_signals ,
roles ,
note ,
modeling_result_accepted ,
verification_result_accepted ,
reorient_finding_accepted ,
}
}
pub fn map_epiphany_coordinator_view (
thread_id : String ,
loaded : bool ,
state_status : ThreadEpiphanyReorientStateStatus ,
state_revision : Option < u64 > ,
status : EpiphanyCoordinatorStatus ,
2026-05-19 22:06:09 +01:00
roles : Vec < ThreadEpiphanyRoleLane > ,
2026-05-13 09:41:16 +01:00
) -> ThreadEpiphanyViewCoordinator {
ThreadEpiphanyViewCoordinator {
thread_id ,
source : if loaded {
ThreadEpiphanyRolesSource ::Live
} else {
ThreadEpiphanyRolesSource ::Stored
} ,
state_status ,
state_revision ,
2026-05-19 21:51:42 +01:00
action : map_protocol_coordinator_action ( status . decision . action ) ,
target_role : status
. decision
. target_role
. map ( map_protocol_coordinator_role_id ) ,
recommended_scene_action : status
. decision
. recommended_scene_action
. map ( map_protocol_coordinator_scene_action ) ,
2026-05-13 09:41:16 +01:00
requires_review : status . decision . requires_review ,
can_auto_run : status . decision . can_auto_run ,
reason : status . decision . reason ,
2026-05-19 21:51:42 +01:00
source_signals : map_protocol_coordinator_source_signals ( status . source_signals ) ,
2026-05-19 22:06:09 +01:00
roles ,
2026-05-13 09:41:16 +01:00
note : status . note ,
}
}
2026-05-13 01:33:32 +01:00
#[ derive(Debug, Clone, Copy, PartialEq, Eq) ]
2026-05-13 03:07:24 +01:00
pub enum EpiphanyCoordinatorAutomationAction {
2026-05-13 01:33:32 +01:00
None ,
CompactRehydrateReorient ,
LaunchReorientWorker ,
}
2026-05-13 03:07:24 +01:00
pub fn map_epiphany_coordinator_automation_action (
2026-05-13 01:33:32 +01:00
decision : & EpiphanyCoordinatorDecision ,
) -> EpiphanyCoordinatorAutomationAction {
2026-05-19 21:51:42 +01:00
map_protocol_coordinator_automation_action ( coordinator_automation_action ( decision ) )
2026-05-13 01:33:32 +01:00
}
2026-05-13 03:07:24 +01:00
pub fn select_epiphany_coordinator_automation_action (
2026-05-13 01:33:32 +01:00
decision : & EpiphanyCoordinatorDecision ,
force_checkpoint_compaction : bool ,
) -> EpiphanyCoordinatorAutomationAction {
map_protocol_coordinator_automation_action ( select_coordinator_automation_action (
2026-05-19 21:51:42 +01:00
decision ,
2026-05-13 01:33:32 +01:00
force_checkpoint_compaction ,
) )
}
2026-05-14 08:49:03 +01:00
pub struct EpiphanyCoordinatorAutomationInput < ' a > {
pub thread_id : & ' a str ,
pub state : & ' a EpiphanyThreadState ,
pub retrieval_override : & ' a EpiphanyRetrievalState ,
pub watcher_snapshot : EpiphanyFreshnessWatcherSnapshot < ' a > ,
2026-05-19 15:33:45 +01:00
pub token_usage_info : Option < & ' a EpiphanyTokenUsageSnapshot > ,
2026-05-14 08:49:03 +01:00
pub runtime_store_path : & ' a Path ,
pub force_checkpoint_compaction : bool ,
}
pub struct EpiphanyCoordinatorAutomationVerdict {
pub action : EpiphanyCoordinatorAutomationAction ,
pub launch_request : Option < EpiphanyJobLaunchRequest > ,
}
pub async fn select_epiphany_coordinator_automation (
input : EpiphanyCoordinatorAutomationInput < '_ > ,
) -> EpiphanyCoordinatorAutomationVerdict {
2026-05-19 16:58:37 +01:00
let core_freshness = derive_epiphany_freshness_view (
2026-05-14 08:49:03 +01:00
Some ( input . state ) ,
Some ( input . retrieval_override ) ,
Some ( input . watcher_snapshot ) ,
) ;
2026-05-19 16:47:28 +01:00
let core_pressure = derive_epiphany_pressure ( input . token_usage_info ) ;
2026-05-19 22:11:58 +01:00
let ( state_status , reorient_decision ) = derive_epiphany_reorient (
2026-05-19 17:05:26 +01:00
Some ( input . state ) ,
& core_pressure ,
& core_freshness . retrieval ,
& core_freshness . graph ,
& core_freshness . watcher ,
) ;
2026-05-19 22:11:58 +01:00
if state_status ! = CoreEpiphanyReorientStateStatus ::Ready {
2026-05-14 08:49:03 +01:00
return EpiphanyCoordinatorAutomationVerdict {
action : EpiphanyCoordinatorAutomationAction ::None ,
launch_request : None ,
} ;
}
let jobs = crate ::jobs ::map_epiphany_jobs ( Some ( input . state ) , Some ( input . retrieval_override ) ) ;
let reorient_job = jobs
. iter ( )
. find ( | job | job . id = = EPIPHANY_REORIENT_LAUNCH_BINDING_ID )
. cloned ( ) ;
2026-05-20 01:32:32 +01:00
let reorient_result = load_core_epiphany_reorient_result_snapshot (
2026-05-14 08:49:03 +01:00
Some ( input . state ) ,
Some ( input . runtime_store_path ) ,
EPIPHANY_REORIENT_LAUNCH_BINDING_ID ,
)
. await ;
2026-05-20 01:32:32 +01:00
let reorient_finding_accepted = reorient_result
. finding
2026-05-14 08:49:03 +01:00
. as_ref ( )
. is_some_and ( | finding | epiphany_reorient_finding_already_accepted ( input . state , finding ) ) ;
let crrc_recommendation = map_epiphany_crrc_recommendation (
true ,
state_status ,
2026-05-19 21:51:42 +01:00
& core_pressure ,
2026-05-14 08:49:03 +01:00
& reorient_decision ,
2026-05-20 01:32:32 +01:00
reorient_result . status ,
2026-05-14 08:49:03 +01:00
input . state . investigation_checkpoint . as_ref ( ) . is_some ( ) ,
2026-05-20 01:32:32 +01:00
reorient_result . finding . is_some ( ) ,
2026-05-14 08:49:03 +01:00
reorient_finding_accepted ,
) ;
let roles = map_epiphany_roles (
Some ( input . state ) ,
& jobs ,
& reorient_decision ,
2026-05-20 02:12:50 +01:00
& core_pressure ,
2026-05-14 08:49:03 +01:00
& crrc_recommendation ,
2026-05-20 01:32:32 +01:00
reorient_result . status ,
2026-05-14 08:49:03 +01:00
reorient_job . as_ref ( ) ,
) ;
let coordinator = derive_epiphany_coordinator_status (
Some ( input . state ) ,
Some ( input . runtime_store_path ) ,
state_status ,
2026-05-19 21:51:42 +01:00
& core_pressure ,
2026-05-14 08:49:03 +01:00
& crrc_recommendation ,
2026-05-19 22:06:09 +01:00
roles . roles ,
2026-05-14 08:49:03 +01:00
Some ( & reorient_decision ) ,
2026-05-20 01:32:32 +01:00
map_protocol_crrc_result_status ( reorient_result . status ) ,
reorient_result . finding . as_ref ( ) ,
2026-05-14 08:49:03 +01:00
input . state . investigation_checkpoint . as_ref ( ) . is_some ( ) ,
)
. await ;
let action = select_epiphany_coordinator_automation_action (
& coordinator . decision ,
input . force_checkpoint_compaction ,
) ;
let launch_request = match action {
EpiphanyCoordinatorAutomationAction ::LaunchReorientWorker = > input
. state
. investigation_checkpoint
. as_ref ( )
. map ( | checkpoint | {
build_epiphany_reorient_launch_request (
input . thread_id ,
Some ( input . state . revision ) ,
None ,
input . state ,
checkpoint ,
2026-05-19 22:11:58 +01:00
& reorient_decision ,
2026-05-14 08:49:03 +01:00
)
} ) ,
EpiphanyCoordinatorAutomationAction ::None
| EpiphanyCoordinatorAutomationAction ::CompactRehydrateReorient = > None ,
} ;
let action = if matches! (
action ,
EpiphanyCoordinatorAutomationAction ::LaunchReorientWorker
) & & launch_request . is_none ( )
{
EpiphanyCoordinatorAutomationAction ::None
} else {
action
} ;
EpiphanyCoordinatorAutomationVerdict {
action ,
launch_request ,
}
}
2026-05-13 03:07:24 +01:00
pub fn map_epiphany_coordinator (
2026-05-19 22:11:58 +01:00
state_status : CoreEpiphanyReorientStateStatus ,
2026-05-13 01:33:32 +01:00
checkpoint_present : bool ,
2026-05-19 21:51:42 +01:00
pressure : & epiphany_core ::EpiphanyPressure ,
2026-05-19 21:58:29 +01:00
recommendation : & CoreEpiphanyCrrcRecommendation ,
2026-05-19 22:06:09 +01:00
roles : & [ EpiphanyRoleBoardLane ] ,
2026-05-19 21:51:42 +01:00
signals : & EpiphanyCoordinatorSourceSignals ,
2026-05-13 01:33:32 +01:00
modeling_result_accepted : bool ,
modeling_result_reviewable : bool ,
modeling_result_accepted_after_verification : bool ,
implementation_evidence_after_verification : bool ,
verification_result_cites_implementation_evidence : bool ,
verification_result_covers_current_modeling : bool ,
verification_result_accepted : bool ,
verification_result_allows_implementation : bool ,
verification_result_needs_evidence : bool ,
reorient_finding_accepted : bool ,
) -> EpiphanyCoordinatorDecision {
2026-05-19 21:51:42 +01:00
recommend_coordinator_action ( EpiphanyCoordinatorInput {
2026-05-19 22:11:58 +01:00
state_status : map_core_crrc_state_status_from_reorient ( state_status ) ,
2026-05-13 01:33:32 +01:00
checkpoint_present ,
should_prepare_compaction : pressure . should_prepare_compaction ,
2026-05-19 21:58:29 +01:00
recommendation : EpiphanyCoordinatorCrrcRecommendation {
action : recommendation . action ,
recommended_scene_action : recommendation
. recommended_scene_action
. map ( epiphany_core ::crrc_scene_action_to_coordinator_scene_action ) ,
} ,
2026-05-19 22:06:09 +01:00
roles : roles
. iter ( )
. map ( map_core_coordinator_role_lane_from_role_board )
. collect ( ) ,
2026-05-13 01:33:32 +01:00
signals : EpiphanyCoordinatorSignals {
2026-05-19 21:51:42 +01:00
modeling_result_status : signals . modeling_result_status ,
verification_result_status : signals . verification_result_status ,
2026-05-13 01:33:32 +01:00
} ,
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 ,
2026-05-19 21:51:42 +01:00
} )
2026-05-13 01:33:32 +01:00
}
2026-05-19 22:06:09 +01:00
fn map_core_coordinator_role_lane_from_role_board (
role : & EpiphanyRoleBoardLane ,
2026-05-13 01:33:32 +01:00
) -> CoreEpiphanyCoordinatorRoleLane {
CoreEpiphanyCoordinatorRoleLane {
2026-05-19 22:06:09 +01:00
id : role . id ,
status : role . status ,
2026-05-13 01:33:32 +01:00
}
}
fn map_protocol_coordinator_role_id (
role_id : CoreEpiphanyCoordinatorRoleId ,
) -> ThreadEpiphanyRoleId {
match role_id {
CoreEpiphanyCoordinatorRoleId ::Implementation = > ThreadEpiphanyRoleId ::Implementation ,
CoreEpiphanyCoordinatorRoleId ::Imagination = > ThreadEpiphanyRoleId ::Imagination ,
CoreEpiphanyCoordinatorRoleId ::Modeling = > ThreadEpiphanyRoleId ::Modeling ,
CoreEpiphanyCoordinatorRoleId ::Verification = > ThreadEpiphanyRoleId ::Verification ,
CoreEpiphanyCoordinatorRoleId ::Reorientation = > ThreadEpiphanyRoleId ::Reorientation ,
}
}
2026-05-19 21:51:42 +01:00
fn map_protocol_coordinator_role_result_status (
status : CoreEpiphanyCoordinatorRoleResultStatus ,
) -> ThreadEpiphanyRoleResultStatus {
match status {
CoreEpiphanyCoordinatorRoleResultStatus ::MissingState = > {
ThreadEpiphanyRoleResultStatus ::MissingState
}
CoreEpiphanyCoordinatorRoleResultStatus ::MissingBinding = > {
ThreadEpiphanyRoleResultStatus ::MissingBinding
}
CoreEpiphanyCoordinatorRoleResultStatus ::BackendUnavailable = > {
ThreadEpiphanyRoleResultStatus ::BackendUnavailable
}
CoreEpiphanyCoordinatorRoleResultStatus ::BackendMissing = > {
ThreadEpiphanyRoleResultStatus ::BackendMissing
}
CoreEpiphanyCoordinatorRoleResultStatus ::Pending = > ThreadEpiphanyRoleResultStatus ::Pending ,
CoreEpiphanyCoordinatorRoleResultStatus ::Running = > ThreadEpiphanyRoleResultStatus ::Running ,
CoreEpiphanyCoordinatorRoleResultStatus ::Completed = > {
ThreadEpiphanyRoleResultStatus ::Completed
}
CoreEpiphanyCoordinatorRoleResultStatus ::Failed = > ThreadEpiphanyRoleResultStatus ::Failed ,
CoreEpiphanyCoordinatorRoleResultStatus ::Cancelled = > {
ThreadEpiphanyRoleResultStatus ::Cancelled
}
}
}
2026-05-13 01:33:32 +01:00
fn map_protocol_coordinator_scene_action (
action : CoreEpiphanyCoordinatorSceneAction ,
) -> ThreadEpiphanySceneAction {
match action {
CoreEpiphanyCoordinatorSceneAction ::Update = > ThreadEpiphanySceneAction ::Update ,
CoreEpiphanyCoordinatorSceneAction ::Reorient = > ThreadEpiphanySceneAction ::Reorient ,
CoreEpiphanyCoordinatorSceneAction ::ReorientLaunch = > {
ThreadEpiphanySceneAction ::ReorientLaunch
}
CoreEpiphanyCoordinatorSceneAction ::ReorientResult = > {
ThreadEpiphanySceneAction ::ReorientResult
}
CoreEpiphanyCoordinatorSceneAction ::ReorientAccept = > {
ThreadEpiphanySceneAction ::ReorientAccept
}
CoreEpiphanyCoordinatorSceneAction ::RoleLaunch = > ThreadEpiphanySceneAction ::RoleLaunch ,
CoreEpiphanyCoordinatorSceneAction ::RoleResult = > ThreadEpiphanySceneAction ::RoleResult ,
}
}
fn map_protocol_coordinator_action (
action : CoreEpiphanyCoordinatorAction ,
) -> ThreadEpiphanyCoordinatorAction {
match action {
CoreEpiphanyCoordinatorAction ::PrepareCheckpoint = > {
ThreadEpiphanyCoordinatorAction ::PrepareCheckpoint
}
CoreEpiphanyCoordinatorAction ::CompactRehydrateReorient = > {
ThreadEpiphanyCoordinatorAction ::CompactRehydrateReorient
}
CoreEpiphanyCoordinatorAction ::LaunchReorientWorker = > {
ThreadEpiphanyCoordinatorAction ::LaunchReorientWorker
}
CoreEpiphanyCoordinatorAction ::WaitForReorientWorker = > {
ThreadEpiphanyCoordinatorAction ::WaitForReorientWorker
}
CoreEpiphanyCoordinatorAction ::ReviewReorientResult = > {
ThreadEpiphanyCoordinatorAction ::ReviewReorientResult
}
CoreEpiphanyCoordinatorAction ::RegatherManually = > {
ThreadEpiphanyCoordinatorAction ::RegatherManually
}
CoreEpiphanyCoordinatorAction ::LaunchModeling = > {
ThreadEpiphanyCoordinatorAction ::LaunchModeling
}
CoreEpiphanyCoordinatorAction ::ReviewModelingResult = > {
ThreadEpiphanyCoordinatorAction ::ReviewModelingResult
}
CoreEpiphanyCoordinatorAction ::LaunchVerification = > {
ThreadEpiphanyCoordinatorAction ::LaunchVerification
}
CoreEpiphanyCoordinatorAction ::ReviewVerificationResult = > {
ThreadEpiphanyCoordinatorAction ::ReviewVerificationResult
}
CoreEpiphanyCoordinatorAction ::ContinueImplementation = > {
ThreadEpiphanyCoordinatorAction ::ContinueImplementation
}
}
}
fn map_protocol_coordinator_automation_action (
action : CoreEpiphanyCoordinatorAutomationAction ,
) -> EpiphanyCoordinatorAutomationAction {
match action {
CoreEpiphanyCoordinatorAutomationAction ::None = > EpiphanyCoordinatorAutomationAction ::None ,
CoreEpiphanyCoordinatorAutomationAction ::CompactRehydrateReorient = > {
EpiphanyCoordinatorAutomationAction ::CompactRehydrateReorient
}
CoreEpiphanyCoordinatorAutomationAction ::LaunchReorientWorker = > {
EpiphanyCoordinatorAutomationAction ::LaunchReorientWorker
}
}
}
2026-05-13 03:07:24 +01:00
pub fn epiphany_reorient_finding_already_accepted (
2026-05-13 01:33:32 +01:00
state : & EpiphanyThreadState ,
2026-05-20 01:32:32 +01:00
finding : & CoreEpiphanyReorientFinding ,
2026-05-13 01:33:32 +01:00
) -> bool {
if let Some ( result_id ) = reorient_finding_runtime_result_id ( finding )
& & state . acceptance_receipts . iter ( ) . any ( | receipt | {
receipt . result_id = = result_id
& & receipt . status = = " accepted "
& & receipt . surface = = " reorientAccept "
} )
{
return true ;
}
false
}
2026-05-20 01:32:32 +01:00
fn epiphany_modeling_finding_has_reviewable_state_patch ( finding : & CoreEpiphanyRoleFinding ) -> bool {
finding
. state_patch
. as_ref ( )
. is_some_and ( | patch | modeling_role_state_patch_policy_errors ( patch ) . is_empty ( ) )
}
fn epiphany_role_finding_already_accepted (
2026-05-13 01:33:32 +01:00
state : & EpiphanyThreadState ,
2026-05-20 01:32:32 +01:00
role_id : CoreEpiphanyCoordinatorRoleId ,
finding : & CoreEpiphanyRoleFinding ,
2026-05-13 01:33:32 +01:00
) -> bool {
2026-05-20 01:32:32 +01:00
epiphany_role_finding_accepted_index ( state , role_id , finding ) . is_some ( )
2026-05-13 01:33:32 +01:00
}
2026-05-20 01:32:32 +01:00
fn epiphany_role_finding_accepted_evidence_id (
2026-05-13 01:33:32 +01:00
state : & EpiphanyThreadState ,
2026-05-20 01:32:32 +01:00
role_id : CoreEpiphanyCoordinatorRoleId ,
finding : & CoreEpiphanyRoleFinding ,
2026-05-13 01:33:32 +01:00
) -> Option < String > {
2026-05-20 01:32:32 +01:00
epiphany_role_finding_acceptance_receipt ( state , role_id , finding )
2026-05-13 01:33:32 +01:00
. and_then ( | receipt | receipt . accepted_evidence_id . clone ( ) )
}
2026-05-20 01:32:32 +01:00
fn epiphany_verification_finding_covers_current_modeling (
2026-05-13 01:33:32 +01:00
state : & EpiphanyThreadState ,
modeling_result_accepted : bool ,
2026-05-20 01:32:32 +01:00
modeling_finding : Option < & CoreEpiphanyRoleFinding > ,
verification_finding : Option < & CoreEpiphanyRoleFinding > ,
2026-05-13 01:33:32 +01:00
) -> bool {
if ! modeling_result_accepted {
return true ;
}
let Some ( modeling_finding ) = modeling_finding else {
return true ;
} ;
let Some ( verification_finding ) = verification_finding else {
return false ;
} ;
let mut modeling_evidence_ids : HashSet < String > =
modeling_finding . evidence_ids . iter ( ) . cloned ( ) . collect ( ) ;
2026-05-20 01:32:32 +01:00
if let Some ( accepted_id ) = epiphany_role_finding_accepted_evidence_id (
state ,
CoreEpiphanyCoordinatorRoleId ::Modeling ,
modeling_finding ,
) {
2026-05-13 01:33:32 +01:00
modeling_evidence_ids . insert ( accepted_id ) ;
}
if modeling_evidence_ids . is_empty ( ) {
return true ;
}
verification_finding
. evidence_ids
. iter ( )
. any ( | id | modeling_evidence_ids . contains ( id ) )
}
2026-05-20 01:32:32 +01:00
fn role_finding_accepted_after (
2026-05-13 01:33:32 +01:00
state : & EpiphanyThreadState ,
2026-05-20 01:32:32 +01:00
later_role_id : CoreEpiphanyCoordinatorRoleId ,
later : Option < & CoreEpiphanyRoleFinding > ,
earlier_role_id : CoreEpiphanyCoordinatorRoleId ,
earlier : Option < & CoreEpiphanyRoleFinding > ,
2026-05-13 01:33:32 +01:00
) -> bool {
let Some ( later ) = later else {
return false ;
} ;
2026-05-20 01:32:32 +01:00
let Some ( later_index ) = epiphany_role_finding_accepted_order_index ( state , later_role_id , later )
else {
2026-05-13 01:33:32 +01:00
return false ;
} ;
let Some ( earlier ) = earlier else {
return true ;
} ;
2026-05-20 01:32:32 +01:00
let Some ( earlier_index ) =
epiphany_role_finding_accepted_order_index ( state , earlier_role_id , earlier )
else {
2026-05-13 01:33:32 +01:00
return true ;
} ;
later_index < earlier_index
}
2026-05-20 01:32:32 +01:00
fn implementation_evidence_after_role_finding (
2026-05-13 01:33:32 +01:00
state : & EpiphanyThreadState ,
2026-05-20 01:32:32 +01:00
role_id : CoreEpiphanyCoordinatorRoleId ,
earlier : Option < & CoreEpiphanyRoleFinding > ,
2026-05-13 01:33:32 +01:00
) -> bool {
let earlier_index =
2026-05-20 01:32:32 +01:00
earlier . and_then ( | finding | epiphany_role_finding_accepted_index ( state , role_id , finding ) ) ;
2026-05-13 01:33:32 +01:00
state
. recent_evidence
. iter ( )
. enumerate ( )
. find ( | ( index , evidence ) | {
evidence . kind = = " implementation-audit "
& & earlier_index . is_none_or ( | earlier_index | * index < earlier_index )
} )
. is_some_and ( | ( _ , evidence ) | evidence . status = = " ok " )
}
2026-05-20 01:32:32 +01:00
fn epiphany_role_finding_cites_implementation_evidence (
2026-05-13 01:33:32 +01:00
state : & EpiphanyThreadState ,
2026-05-20 01:32:32 +01:00
finding : Option < & CoreEpiphanyRoleFinding > ,
2026-05-13 01:33:32 +01:00
) -> bool {
let Some ( finding ) = finding else {
return false ;
} ;
finding . evidence_ids . iter ( ) . any ( | id | {
state . recent_evidence . iter ( ) . any ( | evidence | {
evidence . id = = * id & & evidence . kind = = " implementation-audit " & & evidence . status = = " ok "
} )
} )
}
fn epiphany_role_finding_accepted_index (
state : & EpiphanyThreadState ,
2026-05-20 01:32:32 +01:00
role_id : CoreEpiphanyCoordinatorRoleId ,
finding : & CoreEpiphanyRoleFinding ,
2026-05-13 01:33:32 +01:00
) -> Option < usize > {
2026-05-20 01:32:32 +01:00
if let Some ( receipt ) = epiphany_role_finding_acceptance_receipt ( state , role_id , finding ) {
2026-05-13 01:33:32 +01:00
return receipt
. accepted_evidence_id
. as_ref ( )
. and_then ( | id | {
state
. recent_evidence
. iter ( )
. position ( | evidence | evidence . id = = * id )
} )
. or ( Some ( 0 ) ) ;
}
None
}
fn epiphany_role_finding_accepted_order_index (
state : & EpiphanyThreadState ,
2026-05-20 01:32:32 +01:00
role_id : CoreEpiphanyCoordinatorRoleId ,
finding : & CoreEpiphanyRoleFinding ,
2026-05-13 01:33:32 +01:00
) -> Option < usize > {
2026-05-20 01:32:32 +01:00
epiphany_role_finding_acceptance_receipt_index ( state , role_id , finding )
2026-05-13 01:33:32 +01:00
}
fn epiphany_role_finding_acceptance_receipt < ' a > (
state : & ' a EpiphanyThreadState ,
2026-05-20 01:32:32 +01:00
role_id : CoreEpiphanyCoordinatorRoleId ,
finding : & CoreEpiphanyRoleFinding ,
2026-05-13 01:33:32 +01:00
) -> Option < & ' a EpiphanyAcceptanceReceipt > {
let result_id = role_finding_runtime_result_id ( finding ) ? ;
state . acceptance_receipts . iter ( ) . find ( | receipt | {
receipt . result_id = = result_id
& & receipt . status = = " accepted "
& & receipt . surface = = " roleAccept "
2026-05-20 01:32:32 +01:00
& & receipt . role_id = = core_epiphany_role_label ( role_id )
2026-05-13 01:33:32 +01:00
} )
}
fn epiphany_role_finding_acceptance_receipt_index (
state : & EpiphanyThreadState ,
2026-05-20 01:32:32 +01:00
role_id : CoreEpiphanyCoordinatorRoleId ,
finding : & CoreEpiphanyRoleFinding ,
2026-05-13 01:33:32 +01:00
) -> Option < usize > {
let result_id = role_finding_runtime_result_id ( finding ) ? ;
state . acceptance_receipts . iter ( ) . position ( | receipt | {
receipt . result_id = = result_id
& & receipt . status = = " accepted "
& & receipt . surface = = " roleAccept "
2026-05-20 01:32:32 +01:00
& & receipt . role_id = = core_epiphany_role_label ( role_id )
2026-05-13 01:33:32 +01:00
} )
}
2026-05-20 01:32:32 +01:00
fn core_epiphany_role_label ( role_id : CoreEpiphanyCoordinatorRoleId ) -> & 'static str {
match role_id {
CoreEpiphanyCoordinatorRoleId ::Implementation = > " implementation " ,
CoreEpiphanyCoordinatorRoleId ::Imagination = > " imagination " ,
CoreEpiphanyCoordinatorRoleId ::Modeling = > " modeling " ,
CoreEpiphanyCoordinatorRoleId ::Verification = > " verification " ,
CoreEpiphanyCoordinatorRoleId ::Reorientation = > " reorientation " ,
}
}
fn epiphany_verification_finding_allows_implementation ( finding : & CoreEpiphanyRoleFinding ) -> bool {
finding
. verdict
. as_deref ( )
. is_some_and ( | verdict | verdict . eq_ignore_ascii_case ( " pass " ) )
2026-05-13 01:33:32 +01:00
}
2026-05-20 01:32:32 +01:00
fn epiphany_verification_finding_needs_evidence ( finding : & CoreEpiphanyRoleFinding ) -> bool {
finding
. verdict
. as_deref ( )
. is_some_and ( | verdict | verdict . eq_ignore_ascii_case ( " needs-evidence " ) )
2026-05-13 01:33:32 +01:00
}
2026-05-20 01:32:32 +01:00
fn role_finding_runtime_result_id ( finding : & CoreEpiphanyRoleFinding ) -> Option < String > {
2026-05-13 01:33:32 +01:00
finding . runtime_result_id . clone ( )
}
2026-05-20 01:32:32 +01:00
fn reorient_finding_runtime_result_id ( finding : & CoreEpiphanyReorientFinding ) -> Option < String > {
2026-05-13 01:33:32 +01:00
finding . runtime_result_id . clone ( )
}
2026-05-19 22:06:09 +01:00
#[ derive(Debug, Clone) ]
pub struct EpiphanyRoleBoardStatus {
pub roles : Vec < EpiphanyRoleBoardLane > ,
2026-05-20 02:19:06 +01:00
source_jobs : Vec < EpiphanyJobView > ,
2026-05-19 22:06:09 +01:00
}
2026-05-13 03:07:24 +01:00
pub fn map_epiphany_roles (
2026-05-13 01:33:32 +01:00
state : Option < & EpiphanyThreadState > ,
2026-05-20 02:19:06 +01:00
jobs : & [ EpiphanyJobView ] ,
2026-05-19 22:11:58 +01:00
decision : & CoreEpiphanyReorientDecision ,
2026-05-20 02:12:50 +01:00
pressure : & epiphany_core ::EpiphanyPressure ,
2026-05-19 21:58:29 +01:00
recommendation : & CoreEpiphanyCrrcRecommendation ,
2026-05-19 22:11:58 +01:00
result_status : CoreEpiphanyCrrcResultStatus ,
2026-05-20 02:19:06 +01:00
reorient_job : Option < & EpiphanyJobView > ,
2026-05-19 22:06:09 +01:00
) -> EpiphanyRoleBoardStatus {
2026-05-13 01:33:32 +01:00
let planning = state . map ( | state | & state . planning ) ;
let checkpoint = state . and_then ( | state | state . investigation_checkpoint . as_ref ( ) ) ;
let mut source_jobs = jobs . to_vec ( ) ;
if let Some ( job ) = reorient_job
& & ! source_jobs
. iter ( )
. any ( | source_job | source_job . id = = job . id & & source_job . owner_role = = job . owner_role )
{
source_jobs . push ( job . clone ( ) ) ;
}
2026-05-19 22:06:09 +01:00
let roles = derive_role_board ( EpiphanyRoleBoardInput {
2026-05-13 01:33:32 +01:00
state_present : state . is_some ( ) ,
planning : EpiphanyRoleBoardPlanningSummary {
capture_count : planning
. map ( | planning | planning . captures . len ( ) )
. unwrap_or ( 0 ) ,
backlog_item_count : planning
. map ( | planning | planning . backlog_items . len ( ) )
. unwrap_or ( 0 ) ,
roadmap_stream_count : planning
. map ( | planning | planning . roadmap_streams . len ( ) )
. unwrap_or ( 0 ) ,
objective_draft_count : planning
. map ( | planning | planning . objective_drafts . len ( ) )
. unwrap_or ( 0 ) ,
} ,
checkpoint : checkpoint . map ( | checkpoint | EpiphanyRoleBoardCheckpointSummary {
disposition : Some ( format! ( " {:?} " , checkpoint . disposition ) ) ,
next_action : checkpoint . next_action . clone ( ) ,
} ) ,
reorient_next_action : decision . next_action . clone ( ) ,
jobs : source_jobs . iter ( ) . map ( map_core_role_board_job ) . collect ( ) ,
2026-05-19 21:58:29 +01:00
crrc_action : recommendation . action ,
2026-05-13 01:33:32 +01:00
crrc_recommended_scene_action : recommendation
. recommended_scene_action
2026-05-19 21:58:29 +01:00
. map ( epiphany_core ::crrc_scene_action_to_coordinator_scene_action ) ,
2026-05-13 01:33:32 +01:00
crrc_reason : recommendation . reason . clone ( ) ,
reorient_decision_action : format ! ( " {:?} " , decision . action ) ,
pressure_level : format ! ( " {:?} " , pressure . level ) ,
2026-05-19 22:11:58 +01:00
reorient_result_status : result_status ,
2026-05-13 01:33:32 +01:00
reorient_job : reorient_job . map ( map_core_role_board_job ) ,
imagination_binding_id : EPIPHANY_IMAGINATION_ROLE_BINDING_ID . to_string ( ) ,
modeling_binding_id : EPIPHANY_MODELING_ROLE_BINDING_ID . to_string ( ) ,
verification_binding_id : EPIPHANY_VERIFICATION_ROLE_BINDING_ID . to_string ( ) ,
reorient_owner_role : EPIPHANY_REORIENT_OWNER_ROLE . to_string ( ) ,
imagination_owner_role : EPIPHANY_IMAGINATION_OWNER_ROLE . to_string ( ) ,
2026-05-19 22:06:09 +01:00
} ) ;
EpiphanyRoleBoardStatus { roles , source_jobs }
2026-05-13 01:33:32 +01:00
}
2026-05-20 02:19:06 +01:00
fn map_core_role_board_job ( job : & EpiphanyJobView ) -> EpiphanyRoleBoardJob {
2026-05-13 01:33:32 +01:00
EpiphanyRoleBoardJob {
id : job . id . clone ( ) ,
owner_role : job . owner_role . clone ( ) ,
status : map_core_role_board_job_status ( job . status ) ,
progress_note : job . progress_note . clone ( ) ,
blocking_reason : job . blocking_reason . clone ( ) ,
}
}
2026-05-20 02:19:06 +01:00
fn map_core_role_board_job_status ( status : CoreEpiphanyJobStatus ) -> EpiphanyRoleBoardJobStatus {
2026-05-13 01:33:32 +01:00
match status {
2026-05-20 02:19:06 +01:00
CoreEpiphanyJobStatus ::Idle = > EpiphanyRoleBoardJobStatus ::Idle ,
CoreEpiphanyJobStatus ::Needed = > EpiphanyRoleBoardJobStatus ::Needed ,
CoreEpiphanyJobStatus ::Pending = > EpiphanyRoleBoardJobStatus ::Pending ,
CoreEpiphanyJobStatus ::Running = > EpiphanyRoleBoardJobStatus ::Running ,
CoreEpiphanyJobStatus ::Completed = > EpiphanyRoleBoardJobStatus ::Completed ,
CoreEpiphanyJobStatus ::Failed = > EpiphanyRoleBoardJobStatus ::Failed ,
CoreEpiphanyJobStatus ::Cancelled = > EpiphanyRoleBoardJobStatus ::Cancelled ,
CoreEpiphanyJobStatus ::Blocked = > EpiphanyRoleBoardJobStatus ::Blocked ,
CoreEpiphanyJobStatus ::Unavailable = > EpiphanyRoleBoardJobStatus ::Unavailable ,
2026-05-13 01:33:32 +01:00
}
}
2026-05-19 22:06:09 +01:00
pub fn map_protocol_role_board_lanes (
role_board : & EpiphanyRoleBoardStatus ,
) -> Vec < ThreadEpiphanyRoleLane > {
role_board
. roles
. iter ( )
. cloned ( )
. map ( | lane | map_protocol_role_board_lane ( lane , & role_board . source_jobs ) )
. collect ( )
}
2026-05-13 01:33:32 +01:00
fn map_protocol_role_board_lane (
lane : EpiphanyRoleBoardLane ,
2026-05-20 02:19:06 +01:00
source_jobs : & [ EpiphanyJobView ] ,
2026-05-13 01:33:32 +01:00
) -> ThreadEpiphanyRoleLane {
ThreadEpiphanyRoleLane {
id : map_protocol_coordinator_role_id ( lane . id ) ,
title : lane . title ,
owner_role : lane . owner_role ,
status : map_protocol_coordinator_role_status ( lane . status ) ,
note : lane . note ,
jobs : lane
. jobs
. iter ( )
. map ( | job | map_protocol_role_board_job ( job , source_jobs ) )
. collect ( ) ,
authority_scopes : lane . authority_scopes ,
recommended_action : lane
. recommended_action
. map ( map_protocol_coordinator_scene_action ) ,
}
}
fn map_protocol_role_board_job (
job : & EpiphanyRoleBoardJob ,
2026-05-20 02:19:06 +01:00
source_jobs : & [ EpiphanyJobView ] ,
2026-05-13 01:33:32 +01:00
) -> ThreadEpiphanyJob {
source_jobs
. iter ( )
. find ( | source_job | source_job . id = = job . id & & source_job . owner_role = = job . owner_role )
. cloned ( )
2026-05-20 02:19:06 +01:00
. map ( map_core_epiphany_job_view )
. unwrap_or_else ( | | {
map_core_epiphany_job_view ( EpiphanyJobView {
id : job . id . clone ( ) ,
kind : CoreEpiphanyJobKind ::Specialist ,
scope : job . id . clone ( ) ,
owner_role : job . owner_role . clone ( ) ,
authority_scope : None ,
runtime_job_id : None ,
status : map_core_epiphany_job_status_from_role_board ( job . status ) ,
items_processed : None ,
items_total : None ,
progress_note : job . progress_note . clone ( ) ,
last_checkpoint_at_unix_seconds : None ,
blocking_reason : job . blocking_reason . clone ( ) ,
active_thread_ids : Vec ::new ( ) ,
linked_subgoal_ids : Vec ::new ( ) ,
linked_graph_node_ids : Vec ::new ( ) ,
} )
2026-05-13 01:33:32 +01:00
} )
}
2026-05-20 02:19:06 +01:00
fn map_core_epiphany_job_status_from_role_board (
2026-05-13 01:33:32 +01:00
status : EpiphanyRoleBoardJobStatus ,
2026-05-20 02:19:06 +01:00
) -> CoreEpiphanyJobStatus {
2026-05-13 01:33:32 +01:00
match status {
2026-05-20 02:19:06 +01:00
EpiphanyRoleBoardJobStatus ::Idle = > CoreEpiphanyJobStatus ::Idle ,
EpiphanyRoleBoardJobStatus ::Needed = > CoreEpiphanyJobStatus ::Needed ,
EpiphanyRoleBoardJobStatus ::Pending = > CoreEpiphanyJobStatus ::Pending ,
EpiphanyRoleBoardJobStatus ::Running = > CoreEpiphanyJobStatus ::Running ,
EpiphanyRoleBoardJobStatus ::Completed = > CoreEpiphanyJobStatus ::Completed ,
EpiphanyRoleBoardJobStatus ::Failed = > CoreEpiphanyJobStatus ::Failed ,
EpiphanyRoleBoardJobStatus ::Cancelled = > CoreEpiphanyJobStatus ::Cancelled ,
EpiphanyRoleBoardJobStatus ::Blocked = > CoreEpiphanyJobStatus ::Blocked ,
EpiphanyRoleBoardJobStatus ::Unavailable = > CoreEpiphanyJobStatus ::Unavailable ,
2026-05-13 01:33:32 +01:00
}
}
fn map_protocol_coordinator_role_status (
status : CoreEpiphanyCoordinatorRoleStatus ,
) -> ThreadEpiphanyRoleStatus {
match status {
CoreEpiphanyCoordinatorRoleStatus ::Ready = > ThreadEpiphanyRoleStatus ::Ready ,
CoreEpiphanyCoordinatorRoleStatus ::Needed = > ThreadEpiphanyRoleStatus ::Needed ,
CoreEpiphanyCoordinatorRoleStatus ::Running = > ThreadEpiphanyRoleStatus ::Running ,
CoreEpiphanyCoordinatorRoleStatus ::Waiting = > ThreadEpiphanyRoleStatus ::Waiting ,
CoreEpiphanyCoordinatorRoleStatus ::Review = > ThreadEpiphanyRoleStatus ::Review ,
CoreEpiphanyCoordinatorRoleStatus ::Blocked = > ThreadEpiphanyRoleStatus ::Blocked ,
CoreEpiphanyCoordinatorRoleStatus ::Unavailable = > ThreadEpiphanyRoleStatus ::Unavailable ,
CoreEpiphanyCoordinatorRoleStatus ::Completed = > ThreadEpiphanyRoleStatus ::Completed ,
}
}
2026-05-13 03:07:24 +01:00
pub fn render_epiphany_roles_note (
2026-05-19 22:06:09 +01:00
roles : & [ EpiphanyRoleBoardLane ] ,
2026-05-13 01:33:32 +01:00
state_status : ThreadEpiphanyReorientStateStatus ,
2026-05-19 22:06:09 +01:00
recommendation : CoreEpiphanyCrrcAction ,
2026-05-13 01:33:32 +01:00
) -> String {
render_role_board_note (
2026-05-19 22:06:09 +01:00
roles ,
2026-05-13 01:33:32 +01:00
format! ( " {:?} " , state_status ) . as_str ( ) ,
2026-05-19 22:06:09 +01:00
recommendation ,
2026-05-13 01:33:32 +01:00
)
}