2026-05-13 03:11:21 +01:00
use std ::path ::Path ;
2026-05-13 01:46:38 +01:00
2026-05-20 01:23:01 +01:00
use epiphany_core ::EpiphanyCoordinatorRoleResultStatus as CoreEpiphanyCoordinatorRoleResultStatus ;
use epiphany_core ::EpiphanyCrrcResultStatus as CoreEpiphanyCrrcResultStatus ;
2026-05-30 01:35:41 +01:00
use epiphany_core ::EpiphanyLaunchOrganContract ;
2026-05-30 01:28:32 +01:00
use epiphany_core ::EpiphanyReceiptEffectKind ;
2026-05-20 01:23:01 +01:00
use epiphany_core ::EpiphanyReorientFindingInterpretation ;
use epiphany_core ::EpiphanyRoleFindingInterpretation ;
2026-05-20 02:44:47 +01:00
use epiphany_core ::EpiphanyRoleResultRoleId ;
2026-05-28 16:55:19 +01:00
use epiphany_core ::EpiphanyRuntimeJobResult ;
2026-05-13 03:11:21 +01:00
use epiphany_core ::EpiphanyRuntimeJobSnapshot ;
use epiphany_core ::EpiphanyRuntimeJobStatus ;
2026-05-27 16:17:13 +01:00
use epiphany_core ::MIND_GATEWAY_REVIEW_TYPE ;
2026-05-13 08:42:13 +01:00
use epiphany_core ::interpret_runtime_reorient_worker_result ;
use epiphany_core ::interpret_runtime_role_worker_result ;
2026-05-13 03:11:21 +01:00
use epiphany_core ::runtime_job_snapshot ;
2026-05-13 08:42:13 +01:00
use epiphany_core ::runtime_reorient_worker_result ;
use epiphany_core ::runtime_role_worker_result ;
2026-05-27 16:17:13 +01:00
use epiphany_core ::runtime_worker_launch_request ;
2026-05-19 15:24:57 +01:00
use epiphany_state_model ::EpiphanyRuntimeLink ;
use epiphany_state_model ::EpiphanyThreadState ;
2026-05-13 03:11:21 +01:00
2026-05-19 15:38:31 +01:00
use crate ::error ::EpiphanyBridgeError ;
use crate ::error ::Result as BridgeResult ;
2026-05-20 01:23:01 +01:00
use crate ::results ::render_core_reorient_result_note ;
use crate ::results ::render_core_role_result_note ;
2026-05-13 03:11:21 +01:00
2026-05-20 01:23:01 +01:00
#[ derive(Debug, Clone) ]
pub struct EpiphanyRoleResultSnapshot {
pub status : CoreEpiphanyCoordinatorRoleResultStatus ,
pub finding : Option < EpiphanyRoleFindingInterpretation > ,
pub note : String ,
}
#[ derive(Debug, Clone) ]
pub struct EpiphanyReorientResultSnapshot {
pub status : CoreEpiphanyCrrcResultStatus ,
pub finding : Option < EpiphanyReorientFindingInterpretation > ,
pub note : String ,
}
pub fn load_core_epiphany_role_result_from_runtime_spine_job (
2026-05-13 01:46:38 +01:00
job_id : & str ,
runtime_store_path : Option < & Path > ,
2026-05-20 02:44:47 +01:00
role_id : EpiphanyRoleResultRoleId ,
2026-05-20 01:23:01 +01:00
) -> EpiphanyRoleResultSnapshot {
2026-05-13 01:46:38 +01:00
let Some ( runtime_store_path ) = runtime_store_path else {
2026-05-20 01:23:01 +01:00
return EpiphanyRoleResultSnapshot {
status : CoreEpiphanyCoordinatorRoleResultStatus ::Pending ,
finding : None ,
note : " Heartbeat activation owns this role specialist; no loaded runtime-spine store is available yet. "
2026-05-13 01:46:38 +01:00
. to_string ( ) ,
2026-05-20 01:23:01 +01:00
} ;
2026-05-13 01:46:38 +01:00
} ;
let snapshot = match runtime_job_snapshot ( runtime_store_path , job_id ) {
Ok ( Some ( snapshot ) ) = > snapshot ,
Ok ( None ) = > {
2026-05-20 01:23:01 +01:00
return EpiphanyRoleResultSnapshot {
status : CoreEpiphanyCoordinatorRoleResultStatus ::Pending ,
finding : None ,
note : format ! (
2026-05-13 01:46:38 +01:00
" Heartbeat runtime job {:?} has not reported typed state yet. " ,
job_id
) ,
2026-05-20 01:23:01 +01:00
} ;
2026-05-13 01:46:38 +01:00
}
Err ( err ) = > {
2026-05-20 01:23:01 +01:00
return EpiphanyRoleResultSnapshot {
status : CoreEpiphanyCoordinatorRoleResultStatus ::BackendUnavailable ,
finding : None ,
note : format ! (
2026-05-13 01:46:38 +01:00
" Failed to read heartbeat runtime-spine job {:?}: {err} " ,
job_id
) ,
2026-05-20 01:23:01 +01:00
} ;
2026-05-13 01:46:38 +01:00
}
} ;
let status = map_runtime_role_result_status ( & snapshot ) ;
2026-05-28 16:55:19 +01:00
let finding = match status {
CoreEpiphanyCoordinatorRoleResultStatus ::Completed = > {
match runtime_role_worker_result ( runtime_store_path , job_id ) {
Ok ( Some ( result ) ) = > Some ( interpret_runtime_role_worker_result ( role_id , & result ) ) ,
Ok ( None ) = > {
return EpiphanyRoleResultSnapshot {
status : CoreEpiphanyCoordinatorRoleResultStatus ::BackendUnavailable ,
finding : None ,
note : format ! (
" Heartbeat runtime job {:?} completed without an EpiphanyRuntimeRoleWorkerResult typed document; generic lifecycle receipts are not reviewable findings. " ,
job_id
) ,
} ;
}
Err ( err ) = > {
return EpiphanyRoleResultSnapshot {
status : CoreEpiphanyCoordinatorRoleResultStatus ::BackendUnavailable ,
finding : None ,
note : format ! (
" Failed to read typed role worker result for heartbeat runtime job {:?}: {err} " ,
job_id
) ,
} ;
}
2026-05-14 06:15:33 +01:00
}
2026-05-13 08:42:13 +01:00
}
2026-05-28 16:55:19 +01:00
CoreEpiphanyCoordinatorRoleResultStatus ::Failed
| CoreEpiphanyCoordinatorRoleResultStatus ::Cancelled = > snapshot
. result
. as_ref ( )
. map ( interpret_runtime_role_lifecycle_failure ) ,
_ = > None ,
2026-05-13 08:42:13 +01:00
} ;
2026-05-28 16:55:19 +01:00
let item_error = finding
. as_ref ( )
. and_then ( | finding | finding . job_error . as_deref ( ) ) ;
let note = render_core_role_result_note ( role_id , status , finding . as_ref ( ) , item_error ) ;
2026-05-20 01:23:01 +01:00
EpiphanyRoleResultSnapshot {
status ,
finding ,
note ,
}
2026-05-13 01:46:38 +01:00
}
2026-05-20 01:23:01 +01:00
pub fn load_core_epiphany_reorient_result_from_runtime_spine_job (
job_id : & str ,
runtime_store_path : Option < & Path > ,
) -> EpiphanyReorientResultSnapshot {
2026-05-13 01:46:38 +01:00
let Some ( runtime_store_path ) = runtime_store_path else {
2026-05-20 01:23:01 +01:00
return EpiphanyReorientResultSnapshot {
status : CoreEpiphanyCrrcResultStatus ::Pending ,
finding : None ,
note : " Heartbeat activation owns this reorientation worker; no loaded runtime-spine store is available yet. "
2026-05-13 01:46:38 +01:00
. to_string ( ) ,
2026-05-20 01:23:01 +01:00
} ;
2026-05-13 01:46:38 +01:00
} ;
let snapshot = match runtime_job_snapshot ( runtime_store_path , job_id ) {
Ok ( Some ( snapshot ) ) = > snapshot ,
Ok ( None ) = > {
2026-05-20 01:23:01 +01:00
return EpiphanyReorientResultSnapshot {
status : CoreEpiphanyCrrcResultStatus ::Pending ,
finding : None ,
note : format ! (
2026-05-13 01:46:38 +01:00
" Heartbeat runtime job {:?} has not reported typed state yet. " ,
job_id
) ,
2026-05-20 01:23:01 +01:00
} ;
2026-05-13 01:46:38 +01:00
}
Err ( err ) = > {
2026-05-20 01:23:01 +01:00
return EpiphanyReorientResultSnapshot {
status : CoreEpiphanyCrrcResultStatus ::BackendUnavailable ,
finding : None ,
note : format ! (
2026-05-13 01:46:38 +01:00
" Failed to read heartbeat runtime-spine job {:?}: {err} " ,
job_id
) ,
2026-05-20 01:23:01 +01:00
} ;
2026-05-13 01:46:38 +01:00
}
} ;
let status = map_runtime_reorient_result_status ( & snapshot ) ;
2026-05-28 16:55:19 +01:00
let finding = match status {
CoreEpiphanyCrrcResultStatus ::Completed = > {
match runtime_reorient_worker_result ( runtime_store_path , job_id ) {
Ok ( Some ( result ) ) = > Some ( interpret_runtime_reorient_worker_result ( & result ) ) ,
Ok ( None ) = > {
return EpiphanyReorientResultSnapshot {
status : CoreEpiphanyCrrcResultStatus ::BackendUnavailable ,
finding : None ,
note : format ! (
" Heartbeat runtime job {:?} completed without an EpiphanyRuntimeReorientWorkerResult typed document; generic lifecycle receipts are not reviewable findings. " ,
job_id
) ,
} ;
}
Err ( err ) = > {
return EpiphanyReorientResultSnapshot {
status : CoreEpiphanyCrrcResultStatus ::BackendUnavailable ,
finding : None ,
note : format ! (
" Failed to read typed reorientation worker result for heartbeat runtime job {:?}: {err} " ,
job_id
) ,
} ;
}
2026-05-14 06:15:33 +01:00
}
2026-05-13 08:42:13 +01:00
}
2026-05-28 16:55:19 +01:00
CoreEpiphanyCrrcResultStatus ::Failed | CoreEpiphanyCrrcResultStatus ::Cancelled = > snapshot
. result
. as_ref ( )
. map ( interpret_runtime_reorient_lifecycle_failure ) ,
_ = > None ,
2026-05-13 08:42:13 +01:00
} ;
2026-05-28 16:55:19 +01:00
let item_error = finding
. as_ref ( )
. and_then ( | finding | finding . job_error . as_deref ( ) ) ;
let note = render_core_reorient_result_note ( status , finding . as_ref ( ) , item_error ) ;
2026-05-20 01:23:01 +01:00
EpiphanyReorientResultSnapshot {
status ,
finding ,
note ,
}
}
2026-05-28 16:55:19 +01:00
fn interpret_runtime_role_lifecycle_failure (
result : & EpiphanyRuntimeJobResult ,
) -> EpiphanyRoleFindingInterpretation {
EpiphanyRoleFindingInterpretation {
verdict : Some ( result . verdict . clone ( ) ) ,
summary : Some ( result . summary . clone ( ) ) ,
next_safe_move : empty_string_as_none ( & result . next_safe_move ) ,
checkpoint_summary : None ,
scratch_summary : None ,
files_inspected : Vec ::new ( ) ,
frontier_node_ids : Vec ::new ( ) ,
evidence_ids : result . evidence_refs . clone ( ) ,
artifact_refs : result . artifact_refs . clone ( ) ,
runtime_result_id : Some ( result . result_id . clone ( ) ) ,
runtime_job_id : Some ( result . job_id . clone ( ) ) ,
open_questions : Vec ::new ( ) ,
evidence_gaps : Vec ::new ( ) ,
risks : Vec ::new ( ) ,
state_patch : None ,
self_patch : None ,
self_persistence : None ,
job_error : Some ( result . summary . clone ( ) ) ,
item_error : None ,
}
}
fn interpret_runtime_reorient_lifecycle_failure (
result : & EpiphanyRuntimeJobResult ,
) -> EpiphanyReorientFindingInterpretation {
EpiphanyReorientFindingInterpretation {
mode : None ,
summary : Some ( result . summary . clone ( ) ) ,
next_safe_move : empty_string_as_none ( & result . next_safe_move ) ,
checkpoint_still_valid : None ,
files_inspected : Vec ::new ( ) ,
frontier_node_ids : Vec ::new ( ) ,
evidence_ids : result . evidence_refs . clone ( ) ,
artifact_refs : result . artifact_refs . clone ( ) ,
runtime_result_id : Some ( result . result_id . clone ( ) ) ,
runtime_job_id : Some ( result . job_id . clone ( ) ) ,
job_error : Some ( result . summary . clone ( ) ) ,
item_error : None ,
}
}
fn empty_string_as_none ( value : & str ) -> Option < String > {
( ! value . trim ( ) . is_empty ( ) ) . then ( | | value . to_string ( ) )
}
2026-05-13 01:46:38 +01:00
fn map_runtime_role_result_status (
snapshot : & EpiphanyRuntimeJobSnapshot ,
2026-05-20 01:23:01 +01:00
) -> CoreEpiphanyCoordinatorRoleResultStatus {
2026-05-13 01:46:38 +01:00
match snapshot . job . status {
2026-05-20 01:23:01 +01:00
EpiphanyRuntimeJobStatus ::Queued = > CoreEpiphanyCoordinatorRoleResultStatus ::Pending ,
2026-05-13 01:46:38 +01:00
EpiphanyRuntimeJobStatus ::Running | EpiphanyRuntimeJobStatus ::WaitingForReview = > {
2026-05-20 01:23:01 +01:00
CoreEpiphanyCoordinatorRoleResultStatus ::Running
2026-05-13 01:46:38 +01:00
}
EpiphanyRuntimeJobStatus ::Completed = > {
if snapshot . result . is_some ( ) {
2026-05-20 01:23:01 +01:00
CoreEpiphanyCoordinatorRoleResultStatus ::Completed
2026-05-13 01:46:38 +01:00
} else {
2026-05-20 01:23:01 +01:00
CoreEpiphanyCoordinatorRoleResultStatus ::Pending
2026-05-13 01:46:38 +01:00
}
}
2026-05-20 01:23:01 +01:00
EpiphanyRuntimeJobStatus ::Failed = > CoreEpiphanyCoordinatorRoleResultStatus ::Failed ,
EpiphanyRuntimeJobStatus ::Cancelled = > CoreEpiphanyCoordinatorRoleResultStatus ::Cancelled ,
2026-05-13 01:46:38 +01:00
}
}
fn map_runtime_reorient_result_status (
snapshot : & EpiphanyRuntimeJobSnapshot ,
2026-05-20 01:23:01 +01:00
) -> CoreEpiphanyCrrcResultStatus {
2026-05-13 01:46:38 +01:00
match snapshot . job . status {
2026-05-20 01:23:01 +01:00
EpiphanyRuntimeJobStatus ::Queued = > CoreEpiphanyCrrcResultStatus ::Pending ,
2026-05-13 01:46:38 +01:00
EpiphanyRuntimeJobStatus ::Running | EpiphanyRuntimeJobStatus ::WaitingForReview = > {
2026-05-20 01:23:01 +01:00
CoreEpiphanyCrrcResultStatus ::Running
2026-05-13 01:46:38 +01:00
}
EpiphanyRuntimeJobStatus ::Completed = > {
if snapshot . result . is_some ( ) {
2026-05-20 01:23:01 +01:00
CoreEpiphanyCrrcResultStatus ::Completed
2026-05-13 01:46:38 +01:00
} else {
2026-05-20 01:23:01 +01:00
CoreEpiphanyCrrcResultStatus ::Pending
2026-05-13 01:46:38 +01:00
}
}
2026-05-20 01:23:01 +01:00
EpiphanyRuntimeJobStatus ::Failed = > CoreEpiphanyCrrcResultStatus ::Failed ,
EpiphanyRuntimeJobStatus ::Cancelled = > CoreEpiphanyCrrcResultStatus ::Cancelled ,
}
}
2026-05-20 01:32:32 +01:00
pub async fn load_core_epiphany_role_result_snapshot (
state : & EpiphanyThreadState ,
runtime_store_path : Option < & Path > ,
2026-05-20 02:44:47 +01:00
role_id : EpiphanyRoleResultRoleId ,
2026-05-20 01:32:32 +01:00
binding_id : & str ,
) -> EpiphanyRoleResultSnapshot {
2026-05-13 01:46:38 +01:00
if let Some ( link ) = latest_epiphany_runtime_link_for_binding ( state , binding_id ) {
2026-05-20 01:32:32 +01:00
return load_core_epiphany_role_result_from_runtime_spine_job (
2026-05-13 01:46:38 +01:00
link . runtime_job_id . as_str ( ) ,
runtime_store_path ,
role_id ,
) ;
}
if ! state
. job_bindings
. iter ( )
. any ( | binding | binding . id = = binding_id )
{
2026-05-20 01:32:32 +01:00
return EpiphanyRoleResultSnapshot {
status : CoreEpiphanyCoordinatorRoleResultStatus ::MissingBinding ,
finding : None ,
note : " No matching Epiphany role specialist binding exists. " . to_string ( ) ,
} ;
}
EpiphanyRoleResultSnapshot {
status : CoreEpiphanyCoordinatorRoleResultStatus ::BackendUnavailable ,
finding : None ,
note : " Role binding has no runtime-spine job id; launch a runtime-linked role worker for typed results. " . to_string ( ) ,
2026-05-13 01:46:38 +01:00
}
}
2026-05-20 01:39:27 +01:00
pub fn load_completed_core_epiphany_role_finding (
runtime_store_path : Option < & Path > ,
state : & EpiphanyThreadState ,
2026-05-20 02:44:47 +01:00
role_id : EpiphanyRoleResultRoleId ,
2026-05-20 01:39:27 +01:00
binding_id : & str ,
) -> BridgeResult < EpiphanyRoleFindingInterpretation > {
2026-05-13 03:37:32 +01:00
if let Some ( link ) = latest_epiphany_runtime_link_for_binding ( state , binding_id ) {
2026-05-20 01:39:27 +01:00
let snapshot = load_core_epiphany_role_result_from_runtime_spine_job (
2026-05-13 03:37:32 +01:00
link . runtime_job_id . as_str ( ) ,
2026-05-19 13:51:25 +01:00
runtime_store_path ,
2026-05-13 03:37:32 +01:00
role_id ,
) ;
2026-05-20 01:39:27 +01:00
if snapshot . status ! = CoreEpiphanyCoordinatorRoleResultStatus ::Completed {
2026-05-19 15:38:31 +01:00
return Err ( EpiphanyBridgeError ::InvalidRequest ( format! (
2026-05-13 03:37:32 +01:00
" cannot accept role result while worker status is {:?} " ,
2026-05-20 01:39:27 +01:00
snapshot . status
2026-05-13 03:37:32 +01:00
) ) ) ;
}
2026-05-27 20:58:31 +01:00
let runtime_store_path = runtime_store_path . ok_or_else ( | | {
EpiphanyBridgeError ::InvalidRequest (
" cannot accept completed role worker without a loaded runtime-spine store "
. to_string ( ) ,
)
} ) ? ;
2026-05-27 16:17:13 +01:00
require_launch_organ_contract ( runtime_store_path , link . runtime_job_id . as_str ( ) , " role " ) ? ;
2026-05-20 01:39:27 +01:00
return snapshot . finding . ok_or_else ( | | {
2026-05-19 15:38:31 +01:00
EpiphanyBridgeError ::InvalidRequest (
2026-05-13 03:37:32 +01:00
" cannot accept completed role worker because no typed runtime-spine result was recorded "
. to_string ( ) ,
)
} ) ;
}
if ! state
. job_bindings
. iter ( )
. any ( | binding | binding . id = = binding_id )
{
2026-05-19 15:38:31 +01:00
return Err ( EpiphanyBridgeError ::InvalidRequest ( format! (
2026-05-13 03:37:32 +01:00
" epiphany role binding {:?} was not found " ,
binding_id
) ) ) ;
}
2026-05-19 15:38:31 +01:00
Err ( EpiphanyBridgeError ::InvalidRequest (
2026-05-13 03:37:32 +01:00
" role findings without runtime-spine results are unsupported; accept only typed runtime-spine results "
. to_string ( ) ,
) )
}
2026-05-20 01:32:32 +01:00
pub async fn load_core_epiphany_reorient_result_snapshot (
state : Option < & EpiphanyThreadState > ,
runtime_store_path : Option < & Path > ,
binding_id : & str ,
) -> EpiphanyReorientResultSnapshot {
2026-05-13 01:46:38 +01:00
let Some ( state ) = state else {
2026-05-20 01:32:32 +01:00
return EpiphanyReorientResultSnapshot {
status : CoreEpiphanyCrrcResultStatus ::MissingState ,
finding : None ,
note : " No authoritative Epiphany state exists for this thread. " . to_string ( ) ,
} ;
2026-05-13 01:46:38 +01:00
} ;
if let Some ( link ) = latest_epiphany_runtime_link_for_binding ( state , binding_id ) {
2026-05-20 01:32:32 +01:00
return load_core_epiphany_reorient_result_from_runtime_spine_job (
2026-05-13 01:46:38 +01:00
link . runtime_job_id . as_str ( ) ,
runtime_store_path ,
) ;
}
if ! state
. job_bindings
. iter ( )
. any ( | binding | binding . id = = binding_id )
{
2026-05-20 01:32:32 +01:00
return EpiphanyReorientResultSnapshot {
status : CoreEpiphanyCrrcResultStatus ::MissingBinding ,
finding : None ,
note : " No matching Epiphany reorientation worker binding exists. " . to_string ( ) ,
} ;
}
EpiphanyReorientResultSnapshot {
status : CoreEpiphanyCrrcResultStatus ::BackendUnavailable ,
finding : None ,
note : " Reorientation binding has no runtime-spine job id; launch a runtime-linked reorient worker for typed results. " . to_string ( ) ,
2026-05-13 01:46:38 +01:00
}
}
2026-05-20 01:39:27 +01:00
pub fn load_completed_core_epiphany_reorient_finding (
runtime_store_path : Option < & Path > ,
state : & EpiphanyThreadState ,
binding_id : & str ,
) -> BridgeResult < EpiphanyReorientFindingInterpretation > {
2026-05-13 03:37:32 +01:00
if let Some ( link ) = latest_epiphany_runtime_link_for_binding ( state , binding_id ) {
2026-05-20 01:39:27 +01:00
let snapshot = load_core_epiphany_reorient_result_from_runtime_spine_job (
2026-05-13 03:37:32 +01:00
link . runtime_job_id . as_str ( ) ,
2026-05-19 13:51:25 +01:00
runtime_store_path ,
2026-05-13 03:37:32 +01:00
) ;
2026-05-20 01:39:27 +01:00
if snapshot . status ! = CoreEpiphanyCrrcResultStatus ::Completed {
2026-05-19 15:38:31 +01:00
return Err ( EpiphanyBridgeError ::InvalidRequest ( format! (
2026-05-13 03:37:32 +01:00
" cannot accept reorientation result while worker status is {:?} " ,
2026-05-20 01:39:27 +01:00
snapshot . status
2026-05-13 03:37:32 +01:00
) ) ) ;
}
2026-05-27 20:58:31 +01:00
let runtime_store_path = runtime_store_path . ok_or_else ( | | {
EpiphanyBridgeError ::InvalidRequest (
" cannot accept completed reorientation worker without a loaded runtime-spine store "
. to_string ( ) ,
)
} ) ? ;
2026-05-27 16:17:13 +01:00
require_launch_organ_contract (
runtime_store_path ,
link . runtime_job_id . as_str ( ) ,
" reorient " ,
) ? ;
2026-05-20 01:39:27 +01:00
return snapshot . finding . ok_or_else ( | | {
2026-05-19 15:38:31 +01:00
EpiphanyBridgeError ::InvalidRequest (
2026-05-13 03:37:32 +01:00
" cannot accept completed reorientation worker because no typed runtime-spine result was recorded "
. to_string ( ) ,
)
} ) ;
}
if ! state
. job_bindings
. iter ( )
. any ( | binding | binding . id = = binding_id )
{
2026-05-19 15:38:31 +01:00
return Err ( EpiphanyBridgeError ::InvalidRequest ( format! (
2026-05-13 03:37:32 +01:00
" epiphany reorientation binding {:?} was not found " ,
binding_id
) ) ) ;
}
2026-05-19 15:38:31 +01:00
Err ( EpiphanyBridgeError ::InvalidRequest (
2026-05-13 03:37:32 +01:00
" reorientation findings without runtime-spine results are unsupported; accept only typed runtime-spine results "
. to_string ( ) ,
) )
}
2026-05-27 16:17:13 +01:00
fn require_launch_organ_contract (
runtime_store_path : & Path ,
job_id : & str ,
expected_document_kind : & str ,
) -> BridgeResult < ( ) > {
2026-05-30 01:35:41 +01:00
load_launch_organ_contract_for_runtime_job ( runtime_store_path , job_id , expected_document_kind )
. map ( | _ | ( ) )
}
pub fn load_launch_organ_contract_for_runtime_job (
runtime_store_path : & Path ,
job_id : & str ,
expected_document_kind : & str ,
) -> BridgeResult < EpiphanyLaunchOrganContract > {
2026-05-27 16:17:13 +01:00
let request = runtime_worker_launch_request ( runtime_store_path , job_id ) . map_err ( | err | {
EpiphanyBridgeError ::Fatal ( format! (
" failed to read worker launch request for runtime job {:?}: {err} " ,
job_id
) )
} ) ? ;
let request = request . ok_or_else ( | | {
EpiphanyBridgeError ::InvalidRequest ( format! (
" cannot accept runtime job {:?} without its typed worker launch request " ,
job_id
) )
} ) ? ;
if request . document_kind ! = expected_document_kind {
return Err ( EpiphanyBridgeError ::InvalidRequest ( format! (
" cannot accept runtime job {:?}: launch document kind {:?} does not match expected {:?} " ,
job_id , request . document_kind , expected_document_kind
) ) ) ;
}
if request . organ_launch_contract . dependencies . is_empty ( )
2026-06-02 14:12:10 +01:00
| | request
. organ_launch_contract
. receipt_proof_profiles
. is_empty ( )
2026-05-27 16:17:13 +01:00
{
return Err ( EpiphanyBridgeError ::InvalidRequest ( format! (
2026-05-30 01:28:32 +01:00
" cannot accept runtime job {:?}: worker launch request has no organ dependency/proof-profile contract " ,
2026-05-27 16:17:13 +01:00
job_id
) ) ) ;
}
if ! request
. organ_launch_contract
2026-05-30 01:28:32 +01:00
. receipt_proof_profiles
2026-05-27 16:17:13 +01:00
. iter ( )
2026-05-30 01:28:32 +01:00
. any ( | profile | {
profile . effect_kind = = EpiphanyReceiptEffectKind ::StateAdmission
& & profile
. required_before_promotion_document_types
. iter ( )
. any ( | document_type | document_type = = MIND_GATEWAY_REVIEW_TYPE )
} )
2026-05-27 16:17:13 +01:00
{
return Err ( EpiphanyBridgeError ::InvalidRequest ( format! (
2026-05-30 01:28:32 +01:00
" cannot accept runtime job {:?}: worker launch contract has no state-admission proof profile requiring Mind review " ,
2026-05-27 16:17:13 +01:00
job_id
) ) ) ;
}
2026-05-30 01:35:41 +01:00
Ok ( request . organ_launch_contract )
2026-05-27 16:17:13 +01:00
}
2026-05-13 03:11:21 +01:00
pub fn latest_epiphany_runtime_link_for_binding < ' a > (
2026-05-13 01:46:38 +01:00
state : & ' a EpiphanyThreadState ,
binding_id : & str ,
) -> Option < & ' a EpiphanyRuntimeLink > {
state
. runtime_links
. iter ( )
. find ( | link | link . binding_id = = binding_id & & ! link . runtime_job_id . trim ( ) . is_empty ( ) )
}
2026-05-14 06:15:33 +01:00
#[ cfg(test) ]
mod tests {
use std ::fs ;
use std ::path ::Path ;
use std ::path ::PathBuf ;
2026-05-20 01:53:08 +01:00
use epiphany_core ::EpiphanyCoordinatorRoleResultStatus as CoreEpiphanyCoordinatorRoleResultStatus ;
use epiphany_core ::EpiphanyCrrcResultStatus as CoreEpiphanyCrrcResultStatus ;
2026-05-20 02:44:47 +01:00
use epiphany_core ::EpiphanyRoleResultRoleId ;
2026-05-14 06:15:33 +01:00
use epiphany_core ::RuntimeSpineInitOptions ;
use epiphany_core ::RuntimeSpineJobOptions ;
use epiphany_core ::RuntimeSpineJobResultOptions ;
use epiphany_core ::RuntimeSpineSessionOptions ;
use epiphany_core ::complete_runtime_job ;
use epiphany_core ::create_runtime_job ;
use epiphany_core ::create_runtime_session ;
use epiphany_core ::initialize_runtime_spine ;
use super ::* ;
const NOW : & str = " 2026-05-13T00:00:00Z " ;
#[ test ]
fn role_result_requires_typed_worker_document ( ) {
let store = temp_store_path ( ) ;
2026-05-28 16:55:19 +01:00
seed_lifecycle_result ( & store , " role-job " , " modeling " , " completed " ) ;
2026-05-14 06:15:33 +01:00
2026-05-20 01:53:08 +01:00
let result = load_core_epiphany_role_result_from_runtime_spine_job (
2026-05-14 06:15:33 +01:00
" role-job " ,
Some ( store . as_path ( ) ) ,
2026-05-20 02:44:47 +01:00
EpiphanyRoleResultRoleId ::Modeling ,
2026-05-14 06:15:33 +01:00
) ;
2026-05-20 01:53:08 +01:00
assert_eq! (
result . status ,
CoreEpiphanyCoordinatorRoleResultStatus ::BackendUnavailable
) ;
assert! ( result . finding . is_none ( ) ) ;
assert! (
result
. note
. contains ( " EpiphanyRuntimeRoleWorkerResult typed document " )
) ;
2026-05-14 06:15:33 +01:00
let _ = fs ::remove_file ( store ) ;
}
#[ test ]
fn reorient_result_requires_typed_worker_document ( ) {
let store = temp_store_path ( ) ;
2026-05-28 16:55:19 +01:00
seed_lifecycle_result ( & store , " reorient-job " , " reorientation " , " completed " ) ;
2026-05-14 06:15:33 +01:00
2026-05-20 01:53:08 +01:00
let result = load_core_epiphany_reorient_result_from_runtime_spine_job (
2026-05-14 06:15:33 +01:00
" reorient-job " ,
Some ( store . as_path ( ) ) ,
) ;
assert_eq! (
2026-05-20 01:53:08 +01:00
result . status ,
CoreEpiphanyCrrcResultStatus ::BackendUnavailable
) ;
assert! ( result . finding . is_none ( ) ) ;
assert! (
result
. note
. contains ( " EpiphanyRuntimeReorientWorkerResult typed document " )
2026-05-14 06:15:33 +01:00
) ;
let _ = fs ::remove_file ( store ) ;
}
2026-05-28 16:55:19 +01:00
#[ test ]
fn role_result_projects_failed_lifecycle_receipt_as_terminal ( ) {
let store = temp_store_path ( ) ;
seed_lifecycle_result ( & store , " role-job " , " modeling " , " failed " ) ;
let result = load_core_epiphany_role_result_from_runtime_spine_job (
" role-job " ,
Some ( store . as_path ( ) ) ,
EpiphanyRoleResultRoleId ::Modeling ,
) ;
assert_eq! (
result . status ,
CoreEpiphanyCoordinatorRoleResultStatus ::Failed
) ;
let finding = result . finding . expect ( " failed receipt should be visible " ) ;
assert_eq! ( finding . verdict . as_deref ( ) , Some ( " failed " ) ) ;
assert_eq! ( finding . runtime_job_id . as_deref ( ) , Some ( " role-job " ) ) ;
assert! (
finding
. job_error
. as_deref ( )
. is_some_and ( | error | error . contains ( " generic lifecycle result " ) )
) ;
assert! ( result . note . contains ( " failed " ) ) ;
let _ = fs ::remove_file ( store ) ;
}
#[ test ]
fn reorient_result_projects_failed_lifecycle_receipt_as_terminal ( ) {
let store = temp_store_path ( ) ;
seed_lifecycle_result ( & store , " reorient-job " , " reorientation " , " failed " ) ;
let result = load_core_epiphany_reorient_result_from_runtime_spine_job (
" reorient-job " ,
Some ( store . as_path ( ) ) ,
) ;
assert_eq! ( result . status , CoreEpiphanyCrrcResultStatus ::Failed ) ;
let finding = result . finding . expect ( " failed receipt should be visible " ) ;
assert_eq! ( finding . runtime_job_id . as_deref ( ) , Some ( " reorient-job " ) ) ;
assert! (
finding
. job_error
. as_deref ( )
. is_some_and ( | error | error . contains ( " generic lifecycle result " ) )
) ;
assert! ( result . note . contains ( " failed " ) ) ;
let _ = fs ::remove_file ( store ) ;
}
2026-05-14 06:15:33 +01:00
fn temp_store_path ( ) -> PathBuf {
std ::env ::temp_dir ( ) . join ( format! (
" epiphany-runtime-results-{}.msgpack " ,
uuid ::Uuid ::new_v4 ( )
) )
}
2026-05-28 16:55:19 +01:00
fn seed_lifecycle_result ( store : & Path , job_id : & str , role : & str , verdict : & str ) {
2026-05-14 06:15:33 +01:00
initialize_runtime_spine (
store ,
RuntimeSpineInitOptions {
runtime_id : " test-runtime " . to_string ( ) ,
display_name : " Test Runtime " . to_string ( ) ,
created_at : NOW . to_string ( ) ,
} ,
)
. expect ( " initialize runtime " ) ;
create_runtime_session (
store ,
RuntimeSpineSessionOptions {
session_id : " session-1 " . to_string ( ) ,
objective : " test typed result boundary " . to_string ( ) ,
created_at : NOW . to_string ( ) ,
coordinator_note : " test " . to_string ( ) ,
} ,
)
. expect ( " create session " ) ;
create_runtime_job (
store ,
RuntimeSpineJobOptions {
job_id : job_id . to_string ( ) ,
session_id : " session-1 " . to_string ( ) ,
role : role . to_string ( ) ,
created_at : NOW . to_string ( ) ,
summary : " generic lifecycle job " . to_string ( ) ,
artifact_refs : Vec ::new ( ) ,
} ,
)
. expect ( " create job " ) ;
complete_runtime_job (
store ,
RuntimeSpineJobResultOptions {
result_id : format ! ( " result-{job_id} " ) ,
job_id : job_id . to_string ( ) ,
completed_at : NOW . to_string ( ) ,
2026-05-28 16:55:19 +01:00
verdict : verdict . to_string ( ) ,
2026-05-14 06:15:33 +01:00
summary : " generic lifecycle result " . to_string ( ) ,
next_safe_move : " do not review generic lifecycle receipts " . to_string ( ) ,
evidence_refs : Vec ::new ( ) ,
artifact_refs : Vec ::new ( ) ,
} ,
)
. expect ( " complete job " ) ;
}
}