2026-05-13 03:46:48 +01:00
use epiphany_core ::EpiphanyReorientAcceptanceFinding ;
2026-05-20 01:39:27 +01:00
use epiphany_core ::EpiphanyReorientFindingInterpretation as CoreEpiphanyReorientFinding ;
2026-05-13 03:46:48 +01:00
use epiphany_core ::EpiphanyRoleAcceptanceFinding ;
2026-05-20 01:39:27 +01:00
use epiphany_core ::EpiphanyRoleFindingInterpretation as CoreEpiphanyRoleFinding ;
2026-05-20 02:44:47 +01:00
use epiphany_core ::EpiphanyRoleResultRoleId ;
2026-05-13 03:37:32 +01:00
use epiphany_core ::EpiphanyRoleStatePatchDocument ;
2026-05-14 08:57:31 +01:00
use epiphany_core ::EpiphanyStateUpdate ;
2026-05-20 02:37:10 +01:00
use epiphany_core ::EpiphanyStateUpdatedField ;
2026-05-24 12:39:08 +01:00
use epiphany_core ::MindGatewayReview ;
2026-05-13 03:46:48 +01:00
use epiphany_core ::build_reorient_acceptance_bundle ;
use epiphany_core ::build_role_acceptance_bundle ;
2026-05-13 03:37:32 +01:00
use epiphany_core ::imagination_role_state_patch_policy_errors ;
2026-05-24 12:39:08 +01:00
use epiphany_core ::mind_review_allows_state ;
use epiphany_core ::mind_review_reorient_acceptance ;
use epiphany_core ::mind_review_role_acceptance ;
2026-05-13 03:37:32 +01:00
use epiphany_core ::modeling_role_state_patch_policy_errors ;
2026-05-19 15:24:57 +01:00
use epiphany_state_model ::EpiphanyInvestigationCheckpoint ;
2026-05-13 03:37:32 +01:00
2026-05-13 03:46:48 +01:00
pub struct RoleAcceptanceUpdate {
2026-05-20 02:08:53 +01:00
pub state_update : EpiphanyStateUpdate ,
2026-05-20 02:26:04 +01:00
pub applied_patch : EpiphanyRoleStatePatchDocument ,
2026-05-20 02:37:10 +01:00
pub changed_fields : Vec < EpiphanyStateUpdatedField > ,
2026-05-13 03:46:48 +01:00
pub accepted_receipt_id : String ,
pub accepted_observation_id : String ,
pub accepted_evidence_id : String ,
2026-05-24 12:39:08 +01:00
pub mind_review : MindGatewayReview ,
2026-05-13 03:46:48 +01:00
}
pub struct ReorientAcceptanceUpdate {
2026-05-13 03:54:50 +01:00
pub state_update : EpiphanyStateUpdate ,
2026-05-20 02:37:10 +01:00
pub changed_fields : Vec < EpiphanyStateUpdatedField > ,
2026-05-13 03:46:48 +01:00
pub accepted_receipt_id : String ,
pub accepted_observation_id : String ,
pub accepted_evidence_id : String ,
2026-05-24 12:39:08 +01:00
pub mind_review : MindGatewayReview ,
2026-05-13 03:46:48 +01:00
}
2026-05-20 01:39:27 +01:00
pub fn parse_core_role_finding_state_patch (
finding : & CoreEpiphanyRoleFinding ,
) -> Result < EpiphanyRoleStatePatchDocument , String > {
finding
. state_patch
. clone ( )
. ok_or_else ( | | " completed role finding did not include a reviewable statePatch " . to_string ( ) )
}
2026-05-20 02:08:53 +01:00
pub fn state_update_from_core_patch (
expected_revision : Option < u64 > ,
patch : EpiphanyRoleStatePatchDocument ,
2026-05-13 03:49:57 +01:00
) -> EpiphanyStateUpdate {
EpiphanyStateUpdate {
expected_revision ,
objective : patch . objective ,
active_subgoal_id : patch . active_subgoal_id ,
subgoals : patch . subgoals ,
invariants : patch . invariants ,
graphs : patch . graphs ,
graph_frontier : patch . graph_frontier ,
graph_checkpoint : patch . graph_checkpoint ,
scratch : patch . scratch ,
investigation_checkpoint : patch . investigation_checkpoint ,
job_bindings : patch . job_bindings ,
acceptance_receipts : patch . acceptance_receipts ,
runtime_links : patch . runtime_links ,
observations : patch . observations ,
evidence : patch . evidence ,
churn : patch . churn ,
mode : patch . mode ,
planning : patch . planning ,
}
}
2026-05-13 03:46:48 +01:00
pub fn build_role_acceptance_update (
2026-05-20 02:08:53 +01:00
expected_revision : Option < u64 > ,
2026-05-20 02:44:47 +01:00
role_id : EpiphanyRoleResultRoleId ,
2026-05-13 03:46:48 +01:00
binding_id : & str ,
2026-05-20 01:39:27 +01:00
finding : & CoreEpiphanyRoleFinding ,
2026-05-13 03:46:48 +01:00
accepted_evidence_id : String ,
accepted_observation_id : String ,
accepted_at : String ,
) -> Result < RoleAcceptanceUpdate , String > {
2026-05-20 01:39:27 +01:00
let mut core_patch = match role_id {
2026-05-20 02:44:47 +01:00
EpiphanyRoleResultRoleId ::Imagination = > {
2026-05-20 01:39:27 +01:00
let patch = parse_core_role_finding_state_patch ( finding ) ? ;
let patch_errors = imagination_role_state_patch_policy_errors ( & patch ) ;
2026-05-13 03:46:48 +01:00
if ! patch_errors . is_empty ( ) {
return Err ( format! (
" imagination role state patch is not acceptable: {} " ,
patch_errors . join ( " ; " )
) ) ;
}
patch
}
2026-05-20 02:44:47 +01:00
EpiphanyRoleResultRoleId ::Modeling = > {
2026-05-20 01:39:27 +01:00
let patch = parse_core_role_finding_state_patch ( finding ) ? ;
let patch_errors = modeling_role_state_patch_policy_errors ( & patch ) ;
2026-05-13 03:46:48 +01:00
if ! patch_errors . is_empty ( ) {
return Err ( format! (
" modeling role state patch is not acceptable: {} " ,
patch_errors . join ( " ; " )
) ) ;
}
patch
}
2026-05-30 01:02:16 +01:00
EpiphanyRoleResultRoleId ::Research = > {
let patch = parse_core_role_finding_state_patch ( finding ) ? ;
let patch_errors = epiphany_core ::research_role_state_patch_policy_errors ( & patch ) ;
if ! patch_errors . is_empty ( ) {
return Err ( format! (
" research role state patch is not acceptable: {} " ,
patch_errors . join ( " ; " )
) ) ;
}
patch
}
2026-05-20 02:44:47 +01:00
EpiphanyRoleResultRoleId ::Verification = > EpiphanyRoleStatePatchDocument ::default ( ) ,
EpiphanyRoleResultRoleId ::Implementation | EpiphanyRoleResultRoleId ::Reorientation = > {
2026-05-13 03:46:48 +01:00
return Err ( format! (
" role {:?} cannot be accepted through roleAccept " ,
role_id
) ) ;
}
} ;
2026-05-20 08:03:27 +01:00
let projected_fields = epiphany_update_patch_changed_fields ( & core_patch )
2026-05-13 03:46:48 +01:00
. into_iter ( )
. map ( | field | format! ( " {field:?} " ) )
. collect ( ) ;
2026-05-24 12:39:08 +01:00
let mind_review = mind_review_role_acceptance ( binding_id , role_id , finding , & core_patch ) ;
mind_review_allows_state ( & mind_review ) ? ;
2026-05-13 03:46:48 +01:00
let acceptance_bundle = build_role_acceptance_bundle (
binding_id ,
EpiphanyRoleAcceptanceFinding {
2026-05-20 02:44:47 +01:00
role_id ,
2026-05-13 03:46:48 +01:00
verdict : finding . verdict . clone ( ) ,
summary : finding . summary . clone ( ) ,
next_safe_move : finding . next_safe_move . clone ( ) ,
files_inspected : finding . files_inspected . clone ( ) ,
2026-05-20 01:39:27 +01:00
runtime_result_id : finding . runtime_result_id . clone ( ) ,
runtime_job_id : finding . runtime_job_id . clone ( ) ,
2026-05-13 03:46:48 +01:00
projected_fields ,
} ,
accepted_evidence_id ,
accepted_observation_id ,
accepted_at ,
) ? ;
let accepted_receipt_id = acceptance_bundle . accepted_receipt_id . clone ( ) ;
let accepted_observation_id = acceptance_bundle . accepted_observation_id . clone ( ) ;
let accepted_evidence_id = acceptance_bundle . accepted_evidence_id . clone ( ) ;
2026-05-20 01:39:27 +01:00
core_patch . evidence . push ( acceptance_bundle . evidence ) ;
core_patch . observations . push ( acceptance_bundle . observation ) ;
core_patch
. acceptance_receipts
. push ( acceptance_bundle . receipt ) ;
2026-05-20 02:26:04 +01:00
let applied_patch = core_patch . clone ( ) ;
2026-05-20 08:03:27 +01:00
let changed_fields = epiphany_update_patch_changed_fields ( & applied_patch ) ;
2026-05-20 02:08:53 +01:00
let state_update = state_update_from_core_patch ( expected_revision , core_patch ) ;
2026-05-13 03:46:48 +01:00
Ok ( RoleAcceptanceUpdate {
2026-05-20 02:08:53 +01:00
state_update ,
applied_patch ,
2026-05-13 03:46:48 +01:00
changed_fields ,
accepted_receipt_id ,
accepted_observation_id ,
accepted_evidence_id ,
2026-05-24 12:39:08 +01:00
mind_review ,
2026-05-13 03:46:48 +01:00
} )
}
pub fn build_reorient_acceptance_update (
2026-05-13 03:54:50 +01:00
expected_revision : Option < u64 > ,
2026-05-13 03:46:48 +01:00
binding_id : & str ,
2026-05-20 01:39:27 +01:00
finding : & CoreEpiphanyReorientFinding ,
2026-05-13 03:46:48 +01:00
accepted_evidence_id : String ,
accepted_observation_id : String ,
accepted_at : String ,
update_scratch : bool ,
update_investigation_checkpoint : bool ,
checkpoint : Option < EpiphanyInvestigationCheckpoint > ,
) -> Result < ReorientAcceptanceUpdate , String > {
2026-05-24 12:39:08 +01:00
let mind_finding = EpiphanyReorientAcceptanceFinding {
mode : finding . mode . clone ( ) ,
summary : finding . summary . clone ( ) ,
next_safe_move : finding . next_safe_move . clone ( ) ,
checkpoint_still_valid : finding . checkpoint_still_valid ,
files_inspected : finding . files_inspected . clone ( ) ,
runtime_result_id : finding . runtime_result_id . clone ( ) ,
runtime_job_id : finding . runtime_job_id . clone ( ) ,
} ;
let mind_review = mind_review_reorient_acceptance (
binding_id ,
& mind_finding ,
update_scratch ,
update_investigation_checkpoint ,
) ;
mind_review_allows_state ( & mind_review ) ? ;
2026-05-13 03:46:48 +01:00
let acceptance_bundle = build_reorient_acceptance_bundle (
binding_id ,
2026-05-24 12:39:08 +01:00
mind_finding ,
2026-05-13 03:46:48 +01:00
accepted_evidence_id ,
accepted_observation_id ,
accepted_at ,
update_scratch ,
2026-05-14 08:57:31 +01:00
update_investigation_checkpoint
. then_some ( checkpoint )
. flatten ( ) ,
2026-05-13 03:46:48 +01:00
) ? ;
let accepted_receipt_id = acceptance_bundle . accepted_receipt_id . clone ( ) ;
let accepted_observation_id = acceptance_bundle . accepted_observation_id . clone ( ) ;
let accepted_evidence_id = acceptance_bundle . accepted_evidence_id . clone ( ) ;
let scratch = acceptance_bundle . scratch ;
let investigation_checkpoint = acceptance_bundle . investigation_checkpoint ;
let mut changed_fields = vec! [
2026-05-20 02:37:10 +01:00
EpiphanyStateUpdatedField ::AcceptanceReceipts ,
EpiphanyStateUpdatedField ::Observations ,
EpiphanyStateUpdatedField ::Evidence ,
2026-05-13 03:46:48 +01:00
] ;
if scratch . is_some ( ) {
2026-05-20 02:37:10 +01:00
changed_fields . push ( EpiphanyStateUpdatedField ::Scratch ) ;
2026-05-13 03:46:48 +01:00
}
if investigation_checkpoint . is_some ( ) {
2026-05-20 02:37:10 +01:00
changed_fields . push ( EpiphanyStateUpdatedField ::InvestigationCheckpoint ) ;
2026-05-13 03:46:48 +01:00
}
Ok ( ReorientAcceptanceUpdate {
2026-05-13 03:54:50 +01:00
state_update : EpiphanyStateUpdate {
expected_revision ,
scratch ,
investigation_checkpoint ,
acceptance_receipts : vec ! [ acceptance_bundle . receipt ] ,
observations : vec ! [ acceptance_bundle . observation ] ,
evidence : vec ! [ acceptance_bundle . evidence ] ,
.. Default ::default ( )
} ,
2026-05-13 03:46:48 +01:00
changed_fields ,
accepted_receipt_id ,
accepted_observation_id ,
accepted_evidence_id ,
2026-05-24 12:39:08 +01:00
mind_review ,
2026-05-13 03:46:48 +01:00
} )
}
2026-05-20 02:37:10 +01:00
pub fn epiphany_job_launch_changed_fields ( ) -> Vec < EpiphanyStateUpdatedField > {
2026-05-13 03:37:32 +01:00
vec! [
2026-05-20 02:37:10 +01:00
EpiphanyStateUpdatedField ::JobBindings ,
EpiphanyStateUpdatedField ::RuntimeLinks ,
2026-05-13 03:37:32 +01:00
]
}
2026-05-20 02:28:52 +01:00
pub fn thread_epiphany_patch_has_state_replacements (
patch : & EpiphanyRoleStatePatchDocument ,
) -> bool {
2026-05-13 03:37:32 +01:00
patch . objective . is_some ( )
| | patch . active_subgoal_id . is_some ( )
| | patch . subgoals . is_some ( )
| | patch . invariants . is_some ( )
| | patch . graphs . is_some ( )
| | patch . graph_frontier . is_some ( )
| | patch . graph_checkpoint . is_some ( )
| | patch . scratch . is_some ( )
| | patch . investigation_checkpoint . is_some ( )
| | patch . job_bindings . is_some ( )
| | ! patch . acceptance_receipts . is_empty ( )
| | ! patch . runtime_links . is_empty ( )
| | patch . churn . is_some ( )
| | patch . mode . is_some ( )
| | patch . planning . is_some ( )
}
pub fn epiphany_update_patch_changed_fields (
2026-05-20 08:03:27 +01:00
patch : & EpiphanyRoleStatePatchDocument ,
2026-05-20 02:37:10 +01:00
) -> Vec < EpiphanyStateUpdatedField > {
2026-05-13 03:37:32 +01:00
let mut fields = Vec ::new ( ) ;
if patch . objective . is_some ( ) {
2026-05-20 02:37:10 +01:00
fields . push ( EpiphanyStateUpdatedField ::Objective ) ;
2026-05-13 03:37:32 +01:00
}
if patch . active_subgoal_id . is_some ( ) {
2026-05-20 02:37:10 +01:00
fields . push ( EpiphanyStateUpdatedField ::ActiveSubgoalId ) ;
2026-05-13 03:37:32 +01:00
}
if patch . subgoals . is_some ( ) {
2026-05-20 02:37:10 +01:00
fields . push ( EpiphanyStateUpdatedField ::Subgoals ) ;
2026-05-13 03:37:32 +01:00
}
if patch . invariants . is_some ( ) {
2026-05-20 02:37:10 +01:00
fields . push ( EpiphanyStateUpdatedField ::Invariants ) ;
2026-05-13 03:37:32 +01:00
}
if patch . graphs . is_some ( ) {
2026-05-20 02:37:10 +01:00
fields . push ( EpiphanyStateUpdatedField ::Graphs ) ;
2026-05-13 03:37:32 +01:00
}
if patch . graph_frontier . is_some ( ) {
2026-05-20 02:37:10 +01:00
fields . push ( EpiphanyStateUpdatedField ::GraphFrontier ) ;
2026-05-13 03:37:32 +01:00
}
if patch . graph_checkpoint . is_some ( ) {
2026-05-20 02:37:10 +01:00
fields . push ( EpiphanyStateUpdatedField ::GraphCheckpoint ) ;
2026-05-13 03:37:32 +01:00
}
if patch . scratch . is_some ( ) {
2026-05-20 02:37:10 +01:00
fields . push ( EpiphanyStateUpdatedField ::Scratch ) ;
2026-05-13 03:37:32 +01:00
}
if patch . investigation_checkpoint . is_some ( ) {
2026-05-20 02:37:10 +01:00
fields . push ( EpiphanyStateUpdatedField ::InvestigationCheckpoint ) ;
2026-05-13 03:37:32 +01:00
}
if patch . job_bindings . is_some ( ) {
2026-05-20 02:37:10 +01:00
fields . push ( EpiphanyStateUpdatedField ::JobBindings ) ;
2026-05-13 03:37:32 +01:00
}
if ! patch . acceptance_receipts . is_empty ( ) {
2026-05-20 02:37:10 +01:00
fields . push ( EpiphanyStateUpdatedField ::AcceptanceReceipts ) ;
2026-05-13 03:37:32 +01:00
}
if ! patch . runtime_links . is_empty ( ) {
2026-05-20 02:37:10 +01:00
fields . push ( EpiphanyStateUpdatedField ::RuntimeLinks ) ;
2026-05-13 03:37:32 +01:00
}
if ! patch . observations . is_empty ( ) {
2026-05-20 02:37:10 +01:00
fields . push ( EpiphanyStateUpdatedField ::Observations ) ;
2026-05-13 03:37:32 +01:00
}
if ! patch . evidence . is_empty ( ) {
2026-05-20 02:37:10 +01:00
fields . push ( EpiphanyStateUpdatedField ::Evidence ) ;
2026-05-13 03:37:32 +01:00
}
if patch . churn . is_some ( ) {
2026-05-20 02:37:10 +01:00
fields . push ( EpiphanyStateUpdatedField ::Churn ) ;
2026-05-13 03:37:32 +01:00
}
if patch . mode . is_some ( ) {
2026-05-20 02:37:10 +01:00
fields . push ( EpiphanyStateUpdatedField ::Mode ) ;
2026-05-13 03:37:32 +01:00
}
if patch . planning . is_some ( ) {
2026-05-20 02:37:10 +01:00
fields . push ( EpiphanyStateUpdatedField ::Planning ) ;
2026-05-13 03:37:32 +01:00
}
fields
}
pub fn epiphany_promote_changed_fields (
2026-05-20 02:28:52 +01:00
patch : & EpiphanyRoleStatePatchDocument ,
2026-05-20 02:37:10 +01:00
) -> Vec < EpiphanyStateUpdatedField > {
2026-05-20 08:03:27 +01:00
let mut fields = epiphany_update_patch_changed_fields ( patch ) ;
2026-05-20 02:37:10 +01:00
if ! fields . contains ( & EpiphanyStateUpdatedField ::Evidence ) {
fields . push ( EpiphanyStateUpdatedField ::Evidence ) ;
2026-05-13 03:37:32 +01:00
}
fields
}