2026-05-13 09:56:06 +01:00
use std ::path ::Path ;
use codex_app_server_protocol ::* ;
2026-05-20 07:51:30 +01:00
use epiphany_core ::EpiphanyContextParams ;
2026-05-14 08:57:31 +01:00
use epiphany_core ::EpiphanyDistillInput ;
2026-05-20 07:51:30 +01:00
use epiphany_core ::EpiphanyGraphQuery ;
2026-05-14 08:57:31 +01:00
use epiphany_core ::EpiphanyMapProposalInput ;
2026-05-20 07:54:00 +01:00
use epiphany_core ::EpiphanyRoleResultRoleId ;
2026-05-13 09:56:06 +01:00
use epiphany_core ::EpiphanySceneInput ;
2026-05-19 15:33:45 +01:00
use epiphany_core ::EpiphanyTokenUsageSnapshot ;
2026-05-20 09:54:23 +01:00
use epiphany_core ::EpiphanyViewLens ;
2026-05-13 09:56:06 +01:00
use epiphany_core ::derive_scene ;
2026-05-14 08:57:31 +01:00
use epiphany_core ::distill_observation ;
2026-05-20 09:54:23 +01:00
use epiphany_core ::epiphany_view_needs_jobs ;
use epiphany_core ::epiphany_view_needs_pressure ;
use epiphany_core ::epiphany_view_needs_reorientation_inputs ;
2026-05-14 08:57:31 +01:00
use epiphany_core ::propose_map_update ;
2026-05-20 09:45:40 +01:00
use epiphany_core ::reorient_finding_already_accepted ;
2026-05-19 15:24:57 +01:00
use epiphany_state_model ::EpiphanyRetrievalState ;
use epiphany_state_model ::EpiphanyThreadState ;
2026-05-13 09:56:06 +01:00
2026-05-13 10:13:41 +01:00
use crate ::context ::map_epiphany_context ;
use crate ::context ::map_epiphany_graph_query ;
2026-05-13 09:56:06 +01:00
use crate ::context ::map_epiphany_planning ;
use crate ::coordinator ::derive_epiphany_coordinator_status ;
use crate ::coordinator ::map_epiphany_coordinator_view ;
use crate ::coordinator ::map_epiphany_crrc_recommendation ;
use crate ::coordinator ::map_epiphany_roles ;
2026-05-19 21:58:29 +01:00
use crate ::coordinator ::map_protocol_crrc_recommendation ;
2026-05-19 22:06:09 +01:00
use crate ::coordinator ::map_protocol_role_board_lanes ;
2026-05-13 09:56:06 +01:00
use crate ::coordinator ::render_epiphany_roles_note ;
2026-05-19 16:35:58 +01:00
use crate ::cultnet ::EpiphanyFreshnessSurface ;
use crate ::cultnet ::EpiphanySurfaceSource ;
2026-05-13 09:56:06 +01:00
use crate ::jobs ::map_epiphany_jobs ;
use crate ::launch ::EPIPHANY_REORIENT_LAUNCH_BINDING_ID ;
2026-05-19 16:47:28 +01:00
use crate ::pressure ::derive_epiphany_pressure ;
2026-05-13 09:56:06 +01:00
use crate ::pressure ::map_epiphany_pressure ;
2026-05-20 13:54:38 +01:00
use crate ::protocol_edge ::protocol_job_from_surface ;
2026-05-13 09:56:06 +01:00
use crate ::reorient ::EpiphanyFreshnessWatcherSnapshot ;
2026-05-19 16:35:58 +01:00
use crate ::reorient ::derive_epiphany_freshness_view ;
2026-05-19 22:11:58 +01:00
use crate ::reorient ::derive_epiphany_reorient ;
use crate ::reorient ::map_protocol_reorient_decision ;
use crate ::reorient ::map_protocol_reorient_state_status ;
2026-05-20 01:32:32 +01:00
use crate ::results ::map_protocol_reorient_finding ;
2026-05-20 07:54:00 +01:00
use crate ::results ::map_protocol_role_result_role_id ;
2026-05-20 01:32:32 +01:00
use crate ::runtime_results ::load_core_epiphany_reorient_result_snapshot ;
2026-05-20 01:53:08 +01:00
use crate ::runtime_results ::load_core_epiphany_role_result_snapshot ;
2026-05-20 01:32:32 +01:00
use crate ::runtime_results ::map_protocol_reorient_result_status ;
2026-05-20 01:53:08 +01:00
use crate ::runtime_results ::map_protocol_role_result_status ;
2026-05-13 09:56:06 +01:00
use crate ::scene ::map_core_epiphany_scene_action ;
use crate ::scene ::map_epiphany_scene ;
2026-05-20 08:08:09 +01:00
fn map_protocol_epiphany_view_lens ( lens : EpiphanyViewLens ) -> ThreadEpiphanyViewLens {
match lens {
EpiphanyViewLens ::Scene = > ThreadEpiphanyViewLens ::Scene ,
EpiphanyViewLens ::Jobs = > ThreadEpiphanyViewLens ::Jobs ,
EpiphanyViewLens ::Roles = > ThreadEpiphanyViewLens ::Roles ,
EpiphanyViewLens ::Planning = > ThreadEpiphanyViewLens ::Planning ,
EpiphanyViewLens ::Pressure = > ThreadEpiphanyViewLens ::Pressure ,
EpiphanyViewLens ::Reorient = > ThreadEpiphanyViewLens ::Reorient ,
EpiphanyViewLens ::Crrc = > ThreadEpiphanyViewLens ::Crrc ,
EpiphanyViewLens ::Coordinator = > ThreadEpiphanyViewLens ::Coordinator ,
}
2026-05-13 09:56:06 +01:00
}
2026-05-13 10:13:41 +01:00
pub struct EpiphanyFreshnessResponseInput < ' a > {
pub thread_id : String ,
pub loaded : bool ,
pub state : Option < & ' a EpiphanyThreadState > ,
pub retrieval_override : Option < & ' a EpiphanyRetrievalState > ,
pub watcher_snapshot : Option < EpiphanyFreshnessWatcherSnapshot < ' a > > ,
}
2026-05-19 16:35:58 +01:00
pub fn derive_epiphany_freshness_surface (
2026-05-13 10:13:41 +01:00
input : EpiphanyFreshnessResponseInput < '_ > ,
2026-05-19 16:35:58 +01:00
) -> EpiphanyFreshnessSurface {
let freshness = derive_epiphany_freshness_view (
2026-05-13 10:13:41 +01:00
input . state ,
input . retrieval_override ,
input . watcher_snapshot ,
) ;
2026-05-19 16:35:58 +01:00
EpiphanyFreshnessSurface {
2026-05-13 10:13:41 +01:00
thread_id : input . thread_id ,
source : if input . loaded {
2026-05-19 16:35:58 +01:00
EpiphanySurfaceSource ::Live
2026-05-13 10:13:41 +01:00
} else {
2026-05-19 16:35:58 +01:00
EpiphanySurfaceSource ::Stored
2026-05-13 10:13:41 +01:00
} ,
2026-05-19 16:35:58 +01:00
state_revision : freshness . state_revision ,
retrieval : freshness . retrieval ,
graph : freshness . graph ,
watcher : freshness . watcher ,
2026-05-13 10:13:41 +01:00
}
}
pub fn map_epiphany_context_response (
thread_id : String ,
loaded : bool ,
state : Option < & EpiphanyThreadState > ,
2026-05-20 07:51:30 +01:00
params : & EpiphanyContextParams ,
2026-05-13 10:13:41 +01:00
) -> ThreadEpiphanyContextResponse {
2026-05-20 07:51:30 +01:00
let ( state_status , state_revision , context , missing ) = map_epiphany_context ( state , params ) ;
2026-05-13 10:13:41 +01:00
ThreadEpiphanyContextResponse {
thread_id ,
source : if loaded {
ThreadEpiphanyContextSource ::Live
} else {
ThreadEpiphanyContextSource ::Stored
} ,
state_status ,
state_revision ,
context ,
missing ,
}
}
pub fn map_epiphany_graph_query_response (
thread_id : String ,
loaded : bool ,
state : Option < & EpiphanyThreadState > ,
2026-05-20 07:51:30 +01:00
query : & EpiphanyGraphQuery ,
2026-05-13 10:13:41 +01:00
) -> ThreadEpiphanyGraphQueryResponse {
let ( state_status , state_revision , graph , frontier , checkpoint , matched , missing ) =
2026-05-20 07:51:30 +01:00
map_epiphany_graph_query ( state , query ) ;
2026-05-13 10:13:41 +01:00
ThreadEpiphanyGraphQueryResponse {
thread_id ,
source : if loaded {
ThreadEpiphanyContextSource ::Live
} else {
ThreadEpiphanyContextSource ::Stored
} ,
state_status ,
state_revision ,
graph ,
frontier ,
checkpoint ,
matched ,
missing ,
}
}
2026-05-13 09:56:06 +01:00
pub struct EpiphanyViewResponseInput < ' a > {
pub thread_id : String ,
2026-05-20 08:08:09 +01:00
pub lenses : Vec < EpiphanyViewLens > ,
2026-05-13 09:56:06 +01:00
pub loaded : bool ,
pub state : Option < & ' a EpiphanyThreadState > ,
pub retrieval_override : Option < & ' a EpiphanyRetrievalState > ,
pub watcher_snapshot : Option < EpiphanyFreshnessWatcherSnapshot < ' a > > ,
2026-05-19 15:33:45 +01:00
pub token_usage_info : Option < & ' a EpiphanyTokenUsageSnapshot > ,
2026-05-13 09:56:06 +01:00
pub runtime_store_path : Option < & ' a Path > ,
}
pub async fn map_epiphany_view_response (
input : EpiphanyViewResponseInput < '_ > ,
) -> ThreadEpiphanyViewResponse {
let EpiphanyViewResponseInput {
thread_id ,
lenses ,
loaded ,
state ,
retrieval_override ,
watcher_snapshot ,
token_usage_info ,
runtime_store_path ,
} = input ;
let needs_jobs = epiphany_view_needs_jobs ( & lenses ) ;
let needs_reorientation_inputs = epiphany_view_needs_reorientation_inputs ( & lenses ) ;
let needs_pressure = epiphany_view_needs_pressure ( & lenses ) ;
2026-05-19 16:47:28 +01:00
let core_pressure = needs_pressure . then ( | | derive_epiphany_pressure ( token_usage_info ) ) ;
2026-05-13 09:56:06 +01:00
let pressure = needs_pressure . then ( | | map_epiphany_pressure ( token_usage_info ) ) ;
2026-05-19 16:58:37 +01:00
let core_freshness = needs_reorientation_inputs
. then ( | | derive_epiphany_freshness_view ( state , retrieval_override , watcher_snapshot ) ) ;
2026-05-19 22:11:58 +01:00
let ( state_revision , core_reorient_state_status , core_reorient_decision ) =
2026-05-19 16:58:37 +01:00
if let ( Some ( freshness ) , Some ( core_pressure ) ) =
( core_freshness . as_ref ( ) , core_pressure . as_ref ( ) )
2026-05-13 09:56:06 +01:00
{
2026-05-19 22:11:58 +01:00
let ( state_status , decision ) = derive_epiphany_reorient (
2026-05-19 16:58:37 +01:00
state ,
core_pressure ,
& freshness . retrieval ,
& freshness . graph ,
& freshness . watcher ,
) ;
( freshness . state_revision , state_status , Some ( decision ) )
2026-05-13 09:56:06 +01:00
} else {
2026-05-19 22:11:58 +01:00
(
None ,
epiphany_core ::EpiphanyReorientStateStatus ::Missing ,
None ,
)
2026-05-13 09:56:06 +01:00
} ;
2026-05-19 22:11:58 +01:00
let reorient_state_status = map_protocol_reorient_state_status ( core_reorient_state_status ) ;
let reorient_decision = core_reorient_decision
. clone ( )
. map ( map_protocol_reorient_decision ) ;
2026-05-13 09:56:06 +01:00
let jobs = if needs_jobs {
map_epiphany_jobs ( state , retrieval_override )
} else {
Vec ::new ( )
} ;
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 = if runtime_store_path . is_some ( )
2026-05-20 08:08:09 +01:00
| | lenses . contains ( & EpiphanyViewLens ::Crrc )
| | lenses . contains ( & EpiphanyViewLens ::Coordinator )
2026-05-13 09:56:06 +01:00
{
2026-05-20 01:32:32 +01:00
load_core_epiphany_reorient_result_snapshot (
2026-05-13 09:56:06 +01:00
state ,
runtime_store_path ,
EPIPHANY_REORIENT_LAUNCH_BINDING_ID ,
)
. await
} else {
2026-05-20 01:32:32 +01:00
crate ::runtime_results ::EpiphanyReorientResultSnapshot {
status : epiphany_core ::EpiphanyCrrcResultStatus ::MissingState ,
finding : None ,
note : " Reorient result was not requested. " . to_string ( ) ,
}
2026-05-13 09:56:06 +01:00
} ;
2026-05-20 01:32:32 +01:00
let reorient_result_status = map_protocol_reorient_result_status ( reorient_result . status ) ;
let reorient_finding = reorient_result
. finding
. clone ( )
. map ( map_protocol_reorient_finding ) ;
let reorient_result_note = reorient_result . note . clone ( ) ;
2026-05-13 09:56:06 +01:00
let checkpoint_present = state
. and_then ( | state | state . investigation_checkpoint . as_ref ( ) )
. is_some ( ) ;
2026-05-20 01:32:32 +01:00
let reorient_finding_accepted = reorient_result . finding . as_ref ( ) . is_some_and ( | finding | {
2026-05-20 09:45:40 +01:00
state . is_some_and ( | state | reorient_finding_already_accepted ( state , finding ) )
2026-05-13 09:56:06 +01:00
} ) ;
2026-05-19 21:51:42 +01:00
let recommendation = if let ( Some ( core_pressure ) , Some ( decision ) ) =
2026-05-19 22:11:58 +01:00
( core_pressure . as_ref ( ) , core_reorient_decision . as_ref ( ) )
2026-05-19 21:51:42 +01:00
{
Some ( map_epiphany_crrc_recommendation (
loaded ,
2026-05-19 22:11:58 +01:00
core_reorient_state_status ,
2026-05-19 21:51:42 +01:00
core_pressure ,
decision ,
2026-05-20 01:32:32 +01:00
reorient_result . status ,
2026-05-19 21:51:42 +01:00
checkpoint_present ,
reorient_finding . is_some ( ) ,
reorient_finding_accepted ,
) )
} else {
None
} ;
2026-05-20 02:12:50 +01:00
let roles = if let ( Some ( core_pressure ) , Some ( decision ) , Some ( recommendation ) ) = (
core_pressure . as_ref ( ) ,
2026-05-19 22:11:58 +01:00
core_reorient_decision . as_ref ( ) ,
2026-05-13 09:56:06 +01:00
recommendation . as_ref ( ) ,
) {
Some ( map_epiphany_roles (
state ,
& jobs ,
decision ,
2026-05-20 02:12:50 +01:00
core_pressure ,
2026-05-13 09:56:06 +01:00
recommendation ,
2026-05-20 01:32:32 +01:00
reorient_result . status ,
2026-05-13 09:56:06 +01:00
reorient_job . as_ref ( ) ,
) )
} else {
None
} ;
2026-05-20 08:08:09 +01:00
let coordinator_response = if lenses . contains ( & EpiphanyViewLens ::Coordinator ) {
2026-05-19 21:51:42 +01:00
if let ( Some ( core_pressure ) , Some ( recommendation ) , Some ( roles ) ) = (
core_pressure . as_ref ( ) ,
recommendation . as_ref ( ) ,
roles . clone ( ) ,
) {
2026-05-13 09:56:06 +01:00
let status = derive_epiphany_coordinator_status (
state ,
runtime_store_path ,
2026-05-19 22:11:58 +01:00
core_reorient_state_status ,
2026-05-19 21:51:42 +01:00
core_pressure ,
2026-05-13 09:56:06 +01:00
recommendation ,
2026-05-19 22:06:09 +01:00
roles . roles . clone ( ) ,
2026-05-19 22:11:58 +01:00
core_reorient_decision . as_ref ( ) ,
2026-05-20 08:12:18 +01:00
reorient_result . status ,
2026-05-20 01:32:32 +01:00
reorient_result . finding . as_ref ( ) ,
2026-05-13 09:56:06 +01:00
checkpoint_present ,
)
. await ;
Some ( map_epiphany_coordinator_view (
thread_id . clone ( ) ,
loaded ,
2026-05-20 08:16:11 +01:00
core_reorient_state_status ,
2026-05-13 09:56:06 +01:00
state_revision ,
status ,
2026-05-20 08:16:11 +01:00
roles ,
2026-05-13 09:56:06 +01:00
) )
} else {
None
}
} else {
None
} ;
ThreadEpiphanyViewResponse {
thread_id : thread_id . clone ( ) ,
scene : lenses
2026-05-20 08:08:09 +01:00
. contains ( & EpiphanyViewLens ::Scene )
2026-05-13 09:56:06 +01:00
. then ( | | map_epiphany_scene ( state , loaded , EPIPHANY_REORIENT_LAUNCH_BINDING_ID ) ) ,
2026-05-20 08:08:09 +01:00
jobs : if lenses . contains ( & EpiphanyViewLens ::Jobs ) {
2026-05-20 02:19:06 +01:00
jobs . iter ( )
. cloned ( )
2026-05-20 13:54:38 +01:00
. map ( | job | protocol_job_from_surface ( job , None , None ) )
2026-05-20 02:19:06 +01:00
. collect ( )
2026-05-13 09:56:06 +01:00
} else {
Vec ::new ( )
} ,
2026-05-20 08:08:09 +01:00
roles : lenses . contains ( & EpiphanyViewLens ::Roles ) . then ( | | {
2026-05-19 22:06:09 +01:00
let role_board = roles . clone ( ) ;
let protocol_roles = role_board
. as_ref ( )
. map ( map_protocol_role_board_lanes )
. unwrap_or_default ( ) ;
2026-05-13 09:56:06 +01:00
ThreadEpiphanyViewRoles {
thread_id : thread_id . clone ( ) ,
source : if loaded {
ThreadEpiphanyRolesSource ::Live
} else {
ThreadEpiphanyRolesSource ::Stored
} ,
state_status : reorient_state_status ,
state_revision ,
note : render_epiphany_roles_note (
2026-05-19 22:06:09 +01:00
role_board
. as_ref ( )
. map ( | role_board | role_board . roles . as_slice ( ) )
. unwrap_or ( & [ ] ) ,
2026-05-13 09:56:06 +01:00
reorient_state_status ,
recommendation
. as_ref ( )
2026-05-19 22:06:09 +01:00
. map ( | recommendation | recommendation . action )
. unwrap_or ( epiphany_core ::EpiphanyCrrcAction ::Continue ) ,
2026-05-13 09:56:06 +01:00
) ,
2026-05-19 22:06:09 +01:00
roles : protocol_roles ,
2026-05-13 09:56:06 +01:00
}
} ) ,
2026-05-20 08:08:09 +01:00
planning : lenses . contains ( & EpiphanyViewLens ::Planning ) . then ( | | {
2026-05-13 09:56:06 +01:00
let ( state_status , state_revision , planning , summary ) = map_epiphany_planning ( state ) ;
ThreadEpiphanyViewPlanning {
thread_id : thread_id . clone ( ) ,
source : if loaded {
ThreadEpiphanyContextSource ::Live
} else {
ThreadEpiphanyContextSource ::Stored
} ,
state_status ,
state_revision ,
planning ,
summary ,
}
} ) ,
pressure : lenses
2026-05-20 08:08:09 +01:00
. contains ( & EpiphanyViewLens ::Pressure )
2026-05-13 09:56:06 +01:00
. then ( | | pressure . clone ( ) )
. flatten ( ) ,
reorient : lenses
2026-05-20 08:08:09 +01:00
. contains ( & EpiphanyViewLens ::Reorient )
2026-05-13 09:56:06 +01:00
. then ( | | {
reorient_decision
. clone ( )
. map ( | decision | ThreadEpiphanyViewReorient {
thread_id : thread_id . clone ( ) ,
source : if loaded {
ThreadEpiphanyReorientSource ::Live
} else {
ThreadEpiphanyReorientSource ::Stored
} ,
state_status : reorient_state_status ,
state_revision ,
decision ,
} )
} )
. flatten ( ) ,
crrc : lenses
2026-05-20 08:08:09 +01:00
. contains ( & EpiphanyViewLens ::Crrc )
2026-05-13 09:56:06 +01:00
. then ( | | {
let pressure = pressure . clone ( ) ? ;
let decision = reorient_decision . clone ( ) ? ;
let recommendation = recommendation . clone ( ) ? ;
2026-05-19 21:58:29 +01:00
let protocol_recommendation =
map_protocol_crrc_recommendation ( recommendation . clone ( ) ) ;
2026-05-13 09:56:06 +01:00
let available_actions = derive_scene ( EpiphanySceneInput {
state ,
loaded ,
reorient_binding_id : EPIPHANY_REORIENT_LAUNCH_BINDING_ID ,
} )
. available_actions
. into_iter ( )
. map ( map_core_epiphany_scene_action )
. collect ( ) ;
let note = format! (
" {} Result status: {:?}. {} " ,
recommendation . reason , reorient_result_status , reorient_result_note
) ;
Some ( ThreadEpiphanyViewCrrc {
thread_id : thread_id . clone ( ) ,
source : if loaded {
ThreadEpiphanyReorientSource ::Live
} else {
ThreadEpiphanyReorientSource ::Stored
} ,
state_status : reorient_state_status ,
state_revision ,
pressure ,
decision ,
2026-05-19 21:58:29 +01:00
recommendation : protocol_recommendation ,
2026-05-13 09:56:06 +01:00
reorient_binding_id : EPIPHANY_REORIENT_LAUNCH_BINDING_ID . to_string ( ) ,
reorient_result_status ,
2026-05-20 13:54:38 +01:00
reorient_job : reorient_job
. clone ( )
. map ( | job | protocol_job_from_surface ( job , None , None ) ) ,
2026-05-13 09:56:06 +01:00
reorient_finding : reorient_finding . clone ( ) ,
available_actions ,
note ,
} )
} )
. flatten ( ) ,
coordinator : coordinator_response ,
2026-05-20 08:08:09 +01:00
lenses : lenses
. into_iter ( )
. map ( map_protocol_epiphany_view_lens )
. collect ( ) ,
2026-05-13 09:56:06 +01:00
}
}
2026-05-13 10:05:42 +01:00
pub struct EpiphanyRoleResultResponseInput < ' a > {
pub thread_id : String ,
2026-05-20 07:54:00 +01:00
pub role_id : EpiphanyRoleResultRoleId ,
2026-05-20 07:59:07 +01:00
pub source : EpiphanySurfaceSource ,
2026-05-13 10:05:42 +01:00
pub binding_id : String ,
pub state : Option < & ' a EpiphanyThreadState > ,
pub runtime_store_path : Option < & ' a Path > ,
}
pub async fn map_epiphany_role_result_response (
input : EpiphanyRoleResultResponseInput < '_ > ,
) -> ThreadEpiphanyRoleResultResponse {
let EpiphanyRoleResultResponseInput {
thread_id ,
role_id ,
source ,
binding_id ,
state ,
runtime_store_path ,
} = input ;
2026-05-20 07:54:00 +01:00
let protocol_role_id = map_protocol_role_result_role_id ( role_id ) ;
2026-05-20 07:59:07 +01:00
let protocol_source = map_protocol_roles_source ( source ) ;
2026-05-13 10:05:42 +01:00
let Some ( state ) = state else {
return ThreadEpiphanyRoleResultResponse {
thread_id ,
2026-05-20 07:54:00 +01:00
role_id : protocol_role_id ,
2026-05-20 07:59:07 +01:00
source : protocol_source ,
2026-05-13 10:05:42 +01:00
state_status : ThreadEpiphanyReorientStateStatus ::Missing ,
state_revision : None ,
binding_id ,
status : ThreadEpiphanyRoleResultStatus ::MissingState ,
job : None ,
finding : None ,
note : " No authoritative Epiphany state exists for this thread. " . to_string ( ) ,
} ;
} ;
let job = map_epiphany_jobs ( Some ( state ) , None )
. into_iter ( )
2026-05-20 02:19:06 +01:00
. find ( | job | job . id = = binding_id )
2026-05-20 13:54:38 +01:00
. map ( | job | protocol_job_from_surface ( job , None , None ) ) ;
2026-05-20 07:54:00 +01:00
let result =
load_core_epiphany_role_result_snapshot ( state , runtime_store_path , role_id , & binding_id )
. await ;
2026-05-13 10:05:42 +01:00
ThreadEpiphanyRoleResultResponse {
thread_id ,
2026-05-20 07:54:00 +01:00
role_id : protocol_role_id ,
2026-05-20 07:59:07 +01:00
source : protocol_source ,
2026-05-13 10:05:42 +01:00
state_status : ThreadEpiphanyReorientStateStatus ::Ready ,
state_revision : Some ( state . revision ) ,
binding_id ,
2026-05-20 01:53:08 +01:00
status : map_protocol_role_result_status ( result . status ) ,
2026-05-13 10:05:42 +01:00
job ,
2026-05-20 01:53:08 +01:00
finding : result
. finding
2026-05-20 07:54:00 +01:00
. map ( | finding | crate ::results ::map_protocol_role_finding ( protocol_role_id , finding ) ) ,
2026-05-20 01:53:08 +01:00
note : result . note ,
2026-05-13 10:05:42 +01:00
}
}
pub struct EpiphanyReorientResultResponseInput < ' a > {
pub thread_id : String ,
2026-05-20 07:59:07 +01:00
pub source : EpiphanySurfaceSource ,
2026-05-13 10:05:42 +01:00
pub binding_id : String ,
pub state : Option < & ' a EpiphanyThreadState > ,
pub runtime_store_path : Option < & ' a Path > ,
}
pub async fn map_epiphany_reorient_result_response (
input : EpiphanyReorientResultResponseInput < '_ > ,
) -> ThreadEpiphanyReorientResultResponse {
let EpiphanyReorientResultResponseInput {
thread_id ,
source ,
binding_id ,
state ,
runtime_store_path ,
} = input ;
2026-05-20 07:59:07 +01:00
let protocol_source = map_protocol_reorient_result_source ( source ) ;
2026-05-13 10:05:42 +01:00
let Some ( state ) = state else {
return ThreadEpiphanyReorientResultResponse {
thread_id ,
2026-05-20 07:59:07 +01:00
source : protocol_source ,
2026-05-13 10:05:42 +01:00
state_status : ThreadEpiphanyReorientStateStatus ::Missing ,
state_revision : None ,
binding_id ,
status : ThreadEpiphanyReorientResultStatus ::MissingState ,
job : None ,
finding : None ,
note : " No authoritative Epiphany state exists for this thread. " . to_string ( ) ,
} ;
} ;
let job = map_epiphany_jobs ( Some ( state ) , None )
. into_iter ( )
2026-05-20 02:19:06 +01:00
. find ( | job | job . id = = binding_id )
2026-05-20 13:54:38 +01:00
. map ( | job | protocol_job_from_surface ( job , None , None ) ) ;
2026-05-20 01:53:08 +01:00
let result =
load_core_epiphany_reorient_result_snapshot ( Some ( state ) , runtime_store_path , & binding_id )
. await ;
2026-05-13 10:05:42 +01:00
ThreadEpiphanyReorientResultResponse {
thread_id ,
2026-05-20 07:59:07 +01:00
source : protocol_source ,
2026-05-13 10:05:42 +01:00
state_status : ThreadEpiphanyReorientStateStatus ::Ready ,
state_revision : Some ( state . revision ) ,
binding_id ,
2026-05-20 01:53:08 +01:00
status : map_protocol_reorient_result_status ( result . status ) ,
2026-05-13 10:05:42 +01:00
job ,
2026-05-20 01:53:08 +01:00
finding : result . finding . map ( map_protocol_reorient_finding ) ,
note : result . note ,
2026-05-13 10:05:42 +01:00
}
}
2026-05-13 10:26:35 +01:00
2026-05-20 07:59:07 +01:00
fn map_protocol_roles_source ( source : EpiphanySurfaceSource ) -> ThreadEpiphanyRolesSource {
match source {
EpiphanySurfaceSource ::Live = > ThreadEpiphanyRolesSource ::Live ,
EpiphanySurfaceSource ::Stored = > ThreadEpiphanyRolesSource ::Stored ,
}
}
fn map_protocol_reorient_result_source (
source : EpiphanySurfaceSource ,
) -> ThreadEpiphanyReorientSource {
match source {
EpiphanySurfaceSource ::Live = > ThreadEpiphanyReorientSource ::Live ,
EpiphanySurfaceSource ::Stored = > ThreadEpiphanyReorientSource ::Stored ,
}
}
2026-05-13 10:26:35 +01:00
pub fn map_epiphany_distill_response (
expected_revision : u64 ,
2026-05-20 07:56:46 +01:00
input : EpiphanyDistillInput ,
2026-05-13 10:26:35 +01:00
) -> std ::result ::Result < ThreadEpiphanyDistillResponse , String > {
2026-05-20 07:56:46 +01:00
let proposal = distill_observation ( input ) . map_err ( | err | err . to_string ( ) ) ? ;
Ok ( ThreadEpiphanyDistillResponse {
expected_revision ,
patch : ThreadEpiphanyUpdatePatch {
observations : vec ! [ proposal . observation ] ,
evidence : vec ! [ proposal . evidence ] ,
.. Default ::default ( )
} ,
} )
}
2026-05-13 10:26:35 +01:00
pub fn map_epiphany_propose_response (
state : EpiphanyThreadState ,
observation_ids : Vec < String > ,
) -> std ::result ::Result < ThreadEpiphanyProposeResponse , String > {
let expected_revision = state . revision ;
let proposal = propose_map_update ( EpiphanyMapProposalInput {
state ,
observation_ids ,
} )
. map_err ( | err | err . to_string ( ) ) ? ;
Ok ( ThreadEpiphanyProposeResponse {
expected_revision ,
patch : ThreadEpiphanyUpdatePatch {
observations : vec ! [ proposal . observation ] ,
evidence : vec ! [ proposal . evidence ] ,
graphs : Some ( proposal . graphs ) ,
graph_frontier : Some ( proposal . graph_frontier ) ,
churn : Some ( proposal . churn ) ,
.. Default ::default ( )
} ,
} )
}