2026-05-13 03:37:32 +01:00
use codex_app_server_protocol ::ThreadEpiphanyRoleId ;
2026-05-14 08:57:31 +01:00
use codex_app_server_protocol ::ThreadEpiphanyStateUpdatedField ;
2026-05-13 03:57:22 +01:00
use codex_app_server_protocol ::ThreadEpiphanyStateUpdatedNotification ;
use codex_app_server_protocol ::ThreadEpiphanyStateUpdatedSource ;
2026-05-13 03:37:32 +01:00
use codex_app_server_protocol ::ThreadEpiphanyUpdatePatch ;
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-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-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 ;
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
use crate ::results ::map_core_role_result_role_id ;
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 ,
}
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-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-13 03:37:32 +01:00
pub fn core_state_patch_from_protocol (
patch : & ThreadEpiphanyUpdatePatch ,
) -> EpiphanyRoleStatePatchDocument {
EpiphanyRoleStatePatchDocument {
objective : patch . objective . clone ( ) ,
active_subgoal_id : patch . active_subgoal_id . clone ( ) ,
subgoals : patch . subgoals . clone ( ) ,
invariants : patch . invariants . clone ( ) ,
graphs : patch . graphs . clone ( ) ,
graph_frontier : patch . graph_frontier . clone ( ) ,
graph_checkpoint : patch . graph_checkpoint . clone ( ) ,
scratch : patch . scratch . clone ( ) ,
investigation_checkpoint : patch . investigation_checkpoint . clone ( ) ,
job_bindings : patch . job_bindings . clone ( ) ,
acceptance_receipts : patch . acceptance_receipts . clone ( ) ,
runtime_links : patch . runtime_links . clone ( ) ,
observations : patch . observations . clone ( ) ,
evidence : patch . evidence . clone ( ) ,
churn : patch . churn . clone ( ) ,
mode : patch . mode . clone ( ) ,
planning : patch . planning . clone ( ) ,
}
}
2026-05-20 01:39:27 +01:00
pub fn protocol_patch_from_core (
patch : EpiphanyRoleStatePatchDocument ,
) -> ThreadEpiphanyUpdatePatch {
ThreadEpiphanyUpdatePatch {
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:49:57 +01:00
pub fn state_update_from_thread_patch (
expected_revision : Option < u64 > ,
patch : ThreadEpiphanyUpdatePatch ,
2026-05-20 02:08:53 +01:00
) -> EpiphanyStateUpdate {
state_update_from_core_patch ( expected_revision , core_state_patch_from_protocol ( & patch ) )
}
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:57:22 +01:00
pub fn epiphany_state_updated_notification (
thread_id : String ,
source : ThreadEpiphanyStateUpdatedSource ,
revision : u64 ,
2026-05-20 02:37:10 +01:00
changed_fields : Vec < EpiphanyStateUpdatedField > ,
2026-05-19 15:24:57 +01:00
epiphany_state : epiphany_state_model ::EpiphanyThreadState ,
2026-05-13 03:57:22 +01:00
) -> ThreadEpiphanyStateUpdatedNotification {
ThreadEpiphanyStateUpdatedNotification {
thread_id ,
source ,
revision ,
2026-05-20 02:37:10 +01:00
changed_fields : map_protocol_state_updated_fields ( changed_fields ) ,
2026-05-13 03:57:22 +01:00
epiphany_state ,
}
}
2026-05-20 02:37:10 +01:00
pub fn map_protocol_state_updated_fields (
fields : Vec < EpiphanyStateUpdatedField > ,
) -> Vec < ThreadEpiphanyStateUpdatedField > {
fields
. into_iter ( )
. map ( map_protocol_state_updated_field )
. collect ( )
}
pub fn map_protocol_state_updated_field (
field : EpiphanyStateUpdatedField ,
) -> ThreadEpiphanyStateUpdatedField {
match field {
EpiphanyStateUpdatedField ::Objective = > ThreadEpiphanyStateUpdatedField ::Objective ,
EpiphanyStateUpdatedField ::ActiveSubgoalId = > {
ThreadEpiphanyStateUpdatedField ::ActiveSubgoalId
}
EpiphanyStateUpdatedField ::Subgoals = > ThreadEpiphanyStateUpdatedField ::Subgoals ,
EpiphanyStateUpdatedField ::Invariants = > ThreadEpiphanyStateUpdatedField ::Invariants ,
EpiphanyStateUpdatedField ::Graphs = > ThreadEpiphanyStateUpdatedField ::Graphs ,
EpiphanyStateUpdatedField ::GraphFrontier = > ThreadEpiphanyStateUpdatedField ::GraphFrontier ,
EpiphanyStateUpdatedField ::GraphCheckpoint = > {
ThreadEpiphanyStateUpdatedField ::GraphCheckpoint
}
EpiphanyStateUpdatedField ::Scratch = > ThreadEpiphanyStateUpdatedField ::Scratch ,
EpiphanyStateUpdatedField ::InvestigationCheckpoint = > {
ThreadEpiphanyStateUpdatedField ::InvestigationCheckpoint
}
EpiphanyStateUpdatedField ::JobBindings = > ThreadEpiphanyStateUpdatedField ::JobBindings ,
EpiphanyStateUpdatedField ::AcceptanceReceipts = > {
ThreadEpiphanyStateUpdatedField ::AcceptanceReceipts
}
EpiphanyStateUpdatedField ::RuntimeLinks = > ThreadEpiphanyStateUpdatedField ::RuntimeLinks ,
EpiphanyStateUpdatedField ::Observations = > ThreadEpiphanyStateUpdatedField ::Observations ,
EpiphanyStateUpdatedField ::Evidence = > ThreadEpiphanyStateUpdatedField ::Evidence ,
EpiphanyStateUpdatedField ::Churn = > ThreadEpiphanyStateUpdatedField ::Churn ,
EpiphanyStateUpdatedField ::Mode = > ThreadEpiphanyStateUpdatedField ::Mode ,
EpiphanyStateUpdatedField ::Planning = > ThreadEpiphanyStateUpdatedField ::Planning ,
}
}
2026-05-13 03:37:32 +01:00
pub fn imagination_role_accept_patch_errors ( patch : & ThreadEpiphanyUpdatePatch ) -> Vec < String > {
imagination_role_state_patch_policy_errors ( & core_state_patch_from_protocol ( patch ) )
}
pub fn modeling_role_accept_patch_errors ( patch : & ThreadEpiphanyUpdatePatch ) -> Vec < String > {
modeling_role_state_patch_policy_errors ( & core_state_patch_from_protocol ( patch ) )
}
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-13 03:46:48 +01:00
role_id : ThreadEpiphanyRoleId ,
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-13 03:46:48 +01:00
ThreadEpiphanyRoleId ::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
}
ThreadEpiphanyRoleId ::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-20 01:39:27 +01:00
ThreadEpiphanyRoleId ::Verification = > EpiphanyRoleStatePatchDocument ::default ( ) ,
2026-05-13 03:46:48 +01:00
ThreadEpiphanyRoleId ::Implementation | ThreadEpiphanyRoleId ::Reorientation = > {
return Err ( format! (
" role {:?} cannot be accepted through roleAccept " ,
role_id
) ) ;
}
} ;
2026-05-20 01:39:27 +01:00
let patch_for_projection = protocol_patch_from_core ( core_patch . clone ( ) ) ;
let projected_fields = epiphany_update_patch_changed_fields ( & patch_for_projection )
2026-05-13 03:46:48 +01:00
. into_iter ( )
. map ( | field | format! ( " {field:?} " ) )
. collect ( ) ;
let acceptance_bundle = build_role_acceptance_bundle (
binding_id ,
EpiphanyRoleAcceptanceFinding {
role_id : map_core_role_result_role_id ( role_id ) ,
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 ( ) ;
let changed_fields =
epiphany_update_patch_changed_fields ( & protocol_patch_from_core ( applied_patch . clone ( ) ) ) ;
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 ,
} )
}
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 > {
let acceptance_bundle = build_reorient_acceptance_bundle (
binding_id ,
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 ( ) ,
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
} ,
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-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 (
patch : & ThreadEpiphanyUpdatePatch ,
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 02:28:52 +01:00
let mut fields = epiphany_update_patch_changed_fields ( & protocol_patch_from_core ( patch . clone ( ) ) ) ;
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
}