Move acceptance mutation construction into core

This commit is contained in:
Meta Vens 2026-07-11 19:15:27 +01:00
commit 51efe1c962
5 changed files with 285 additions and 216 deletions

View file

@ -1,47 +1,56 @@
use epiphany_core::EpiphanyReorientAcceptanceFinding;
use epiphany_core::EpiphanyReorientFindingInterpretation as CoreEpiphanyReorientFinding;
use epiphany_core::EpiphanyRoleAcceptanceFinding;
use epiphany_core::EpiphanyRoleFindingInterpretation as CoreEpiphanyRoleFinding;
use epiphany_core::EpiphanyRoleResultRoleId;
use epiphany_core::EpiphanyRoleStatePatchDocument;
use epiphany_core::EpiphanyStateUpdate;
use epiphany_core::EpiphanyStateUpdatedField;
use epiphany_core::MindGatewayReview;
use epiphany_core::build_reorient_acceptance_bundle;
use epiphany_core::build_role_acceptance_bundle;
use epiphany_core::imagination_role_state_patch_policy_errors;
use epiphany_core::mind_review_allows_state;
use epiphany_core::mind_review_reorient_acceptance;
use epiphany_core::mind_review_role_acceptance;
use epiphany_core::modeling_role_state_patch_policy_errors;
use epiphany_state_model::EpiphanyInvestigationCheckpoint;
pub struct RoleAcceptanceUpdate {
pub state_update: EpiphanyStateUpdate,
pub applied_patch: EpiphanyRoleStatePatchDocument,
pub changed_fields: Vec<EpiphanyStateUpdatedField>,
pub accepted_receipt_id: String,
pub accepted_observation_id: String,
pub accepted_evidence_id: String,
pub mind_review: MindGatewayReview,
}
pub type RoleAcceptanceUpdate = epiphany_core::EpiphanyRoleAcceptanceUpdate;
pub type ReorientAcceptanceUpdate = epiphany_core::EpiphanyReorientAcceptanceUpdate;
pub struct ReorientAcceptanceUpdate {
pub state_update: EpiphanyStateUpdate,
pub changed_fields: Vec<EpiphanyStateUpdatedField>,
pub accepted_receipt_id: String,
pub accepted_observation_id: String,
pub accepted_evidence_id: String,
pub mind_review: MindGatewayReview,
}
pub fn parse_core_role_finding_state_patch(
pub fn build_role_acceptance_update(
expected_revision: Option<u64>,
role_id: EpiphanyRoleResultRoleId,
binding_id: &str,
finding: &CoreEpiphanyRoleFinding,
) -> Result<EpiphanyRoleStatePatchDocument, String> {
finding
.state_patch
.clone()
.ok_or_else(|| "completed role finding did not include a reviewable statePatch".to_string())
accepted_evidence_id: String,
accepted_observation_id: String,
accepted_at: String,
) -> Result<RoleAcceptanceUpdate, String> {
epiphany_core::build_native_role_acceptance_update(
expected_revision,
role_id,
binding_id,
finding,
accepted_evidence_id,
accepted_observation_id,
accepted_at,
)
}
pub fn build_reorient_acceptance_update(
expected_revision: Option<u64>,
binding_id: &str,
finding: &CoreEpiphanyReorientFinding,
accepted_evidence_id: String,
accepted_observation_id: String,
accepted_at: String,
update_scratch: bool,
update_investigation_checkpoint: bool,
checkpoint: Option<EpiphanyInvestigationCheckpoint>,
) -> Result<ReorientAcceptanceUpdate, String> {
epiphany_core::build_native_reorient_acceptance_update(
expected_revision,
binding_id,
finding,
accepted_evidence_id,
accepted_observation_id,
accepted_at,
update_scratch,
update_investigation_checkpoint,
checkpoint,
)
}
pub fn state_update_from_core_patch(
@ -70,177 +79,6 @@ pub fn state_update_from_core_patch(
}
}
pub fn build_role_acceptance_update(
expected_revision: Option<u64>,
role_id: EpiphanyRoleResultRoleId,
binding_id: &str,
finding: &CoreEpiphanyRoleFinding,
accepted_evidence_id: String,
accepted_observation_id: String,
accepted_at: String,
) -> Result<RoleAcceptanceUpdate, String> {
let mut core_patch = match role_id {
EpiphanyRoleResultRoleId::Imagination => {
let patch = parse_core_role_finding_state_patch(finding)?;
let patch_errors = imagination_role_state_patch_policy_errors(&patch);
if !patch_errors.is_empty() {
return Err(format!(
"imagination role state patch is not acceptable: {}",
patch_errors.join("; ")
));
}
patch
}
EpiphanyRoleResultRoleId::Modeling => {
let patch = parse_core_role_finding_state_patch(finding)?;
let patch_errors = modeling_role_state_patch_policy_errors(&patch);
if !patch_errors.is_empty() {
return Err(format!(
"modeling role state patch is not acceptable: {}",
patch_errors.join("; ")
));
}
patch
}
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
}
EpiphanyRoleResultRoleId::Verification => EpiphanyRoleStatePatchDocument::default(),
EpiphanyRoleResultRoleId::Implementation | EpiphanyRoleResultRoleId::Reorientation => {
return Err(format!(
"role {:?} cannot be accepted through roleAccept",
role_id
));
}
};
let projected_fields = epiphany_update_patch_changed_fields(&core_patch)
.into_iter()
.map(|field| format!("{field:?}"))
.collect();
let mind_review = mind_review_role_acceptance(binding_id, role_id, finding, &core_patch);
mind_review_allows_state(&mind_review)?;
let acceptance_bundle = build_role_acceptance_bundle(
binding_id,
EpiphanyRoleAcceptanceFinding {
role_id,
verdict: finding.verdict.clone(),
summary: finding.summary.clone(),
next_safe_move: finding.next_safe_move.clone(),
files_inspected: finding.files_inspected.clone(),
runtime_result_id: finding.runtime_result_id.clone(),
runtime_job_id: finding.runtime_job_id.clone(),
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();
core_patch.evidence.push(acceptance_bundle.evidence);
core_patch.observations.push(acceptance_bundle.observation);
core_patch
.acceptance_receipts
.push(acceptance_bundle.receipt);
let applied_patch = core_patch.clone();
let changed_fields = epiphany_update_patch_changed_fields(&applied_patch);
let state_update = state_update_from_core_patch(expected_revision, core_patch);
Ok(RoleAcceptanceUpdate {
state_update,
applied_patch,
changed_fields,
accepted_receipt_id,
accepted_observation_id,
accepted_evidence_id,
mind_review,
})
}
pub fn build_reorient_acceptance_update(
expected_revision: Option<u64>,
binding_id: &str,
finding: &CoreEpiphanyReorientFinding,
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 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)?;
let acceptance_bundle = build_reorient_acceptance_bundle(
binding_id,
mind_finding,
accepted_evidence_id,
accepted_observation_id,
accepted_at,
update_scratch,
update_investigation_checkpoint
.then_some(checkpoint)
.flatten(),
)?;
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![
EpiphanyStateUpdatedField::AcceptanceReceipts,
EpiphanyStateUpdatedField::Observations,
EpiphanyStateUpdatedField::Evidence,
];
if scratch.is_some() {
changed_fields.push(EpiphanyStateUpdatedField::Scratch);
}
if investigation_checkpoint.is_some() {
changed_fields.push(EpiphanyStateUpdatedField::InvestigationCheckpoint);
}
Ok(ReorientAcceptanceUpdate {
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()
},
changed_fields,
accepted_receipt_id,
accepted_observation_id,
accepted_evidence_id,
mind_review,
})
}
pub fn epiphany_job_launch_changed_fields() -> Vec<EpiphanyStateUpdatedField> {
vec![
EpiphanyStateUpdatedField::JobBindings,
@ -271,56 +109,57 @@ pub fn thread_epiphany_patch_has_state_replacements(
pub fn epiphany_update_patch_changed_fields(
patch: &EpiphanyRoleStatePatchDocument,
) -> Vec<EpiphanyStateUpdatedField> {
let update = state_update_from_core_patch(None, patch.clone());
let mut fields = Vec::new();
if patch.objective.is_some() {
if update.objective.is_some() {
fields.push(EpiphanyStateUpdatedField::Objective);
}
if patch.active_subgoal_id.is_some() {
if update.active_subgoal_id.is_some() {
fields.push(EpiphanyStateUpdatedField::ActiveSubgoalId);
}
if patch.subgoals.is_some() {
if update.subgoals.is_some() {
fields.push(EpiphanyStateUpdatedField::Subgoals);
}
if patch.invariants.is_some() {
if update.invariants.is_some() {
fields.push(EpiphanyStateUpdatedField::Invariants);
}
if patch.graphs.is_some() {
if update.graphs.is_some() {
fields.push(EpiphanyStateUpdatedField::Graphs);
}
if patch.graph_frontier.is_some() {
if update.graph_frontier.is_some() {
fields.push(EpiphanyStateUpdatedField::GraphFrontier);
}
if patch.graph_checkpoint.is_some() {
if update.graph_checkpoint.is_some() {
fields.push(EpiphanyStateUpdatedField::GraphCheckpoint);
}
if patch.scratch.is_some() {
if update.scratch.is_some() {
fields.push(EpiphanyStateUpdatedField::Scratch);
}
if patch.investigation_checkpoint.is_some() {
if update.investigation_checkpoint.is_some() {
fields.push(EpiphanyStateUpdatedField::InvestigationCheckpoint);
}
if patch.job_bindings.is_some() {
if update.job_bindings.is_some() {
fields.push(EpiphanyStateUpdatedField::JobBindings);
}
if !patch.acceptance_receipts.is_empty() {
if !update.acceptance_receipts.is_empty() {
fields.push(EpiphanyStateUpdatedField::AcceptanceReceipts);
}
if !patch.runtime_links.is_empty() {
if !update.runtime_links.is_empty() {
fields.push(EpiphanyStateUpdatedField::RuntimeLinks);
}
if !patch.observations.is_empty() {
if !update.observations.is_empty() {
fields.push(EpiphanyStateUpdatedField::Observations);
}
if !patch.evidence.is_empty() {
if !update.evidence.is_empty() {
fields.push(EpiphanyStateUpdatedField::Evidence);
}
if patch.churn.is_some() {
if update.churn.is_some() {
fields.push(EpiphanyStateUpdatedField::Churn);
}
if patch.mode.is_some() {
if update.mode.is_some() {
fields.push(EpiphanyStateUpdatedField::Mode);
}
if patch.planning.is_some() {
if update.planning.is_some() {
fields.push(EpiphanyStateUpdatedField::Planning);
}
fields

View file

@ -2,22 +2,34 @@ use crate::EpiphanyCoordinatorRoleResultStatus;
use crate::EpiphanyCrrcResultStatus;
use crate::EpiphanyLaunchOrganContract;
use crate::EpiphanyReceiptEffectKind;
use crate::EpiphanyReorientAcceptanceFinding;
use crate::EpiphanyReorientFindingInterpretation;
use crate::EpiphanyRoleAcceptanceFinding;
use crate::EpiphanyRoleFindingInterpretation;
use crate::EpiphanyRoleResultRoleId;
use crate::EpiphanyRoleSelfPersistenceReview;
use crate::EpiphanyRoleSelfPersistenceStatus;
use crate::EpiphanyRoleStatePatchDocument;
use crate::EpiphanyRuntimeJobResult;
use crate::EpiphanyRuntimeJobSnapshot;
use crate::EpiphanyRuntimeJobStatus;
use crate::EpiphanyStateUpdate;
use crate::EpiphanyStateUpdatedField;
use crate::MIND_GATEWAY_REVIEW_TYPE;
use crate::MindGatewayReview;
use crate::apply_epiphany_state_update;
use crate::build_reorient_acceptance_bundle;
use crate::build_role_acceptance_bundle;
use crate::epiphany_state_update_validation_errors;
use crate::imagination_role_state_patch_policy_errors;
use crate::interpret_runtime_reorient_worker_result;
use crate::interpret_runtime_role_worker_result;
use crate::load_thread_state;
use crate::mind_review_allows_state;
use crate::mind_review_reorient_acceptance;
use crate::mind_review_role_acceptance;
use crate::modeling_role_state_patch_policy_errors;
use crate::research_role_state_patch_policy_errors;
use crate::runtime_job_snapshot;
use crate::runtime_reorient_worker_result;
use crate::runtime_role_worker_result;
@ -63,6 +75,25 @@ pub struct EpiphanyCoordinatorStateApplied {
pub state: EpiphanyThreadState,
}
pub struct EpiphanyRoleAcceptanceUpdate {
pub state_update: EpiphanyStateUpdate,
pub applied_patch: EpiphanyRoleStatePatchDocument,
pub changed_fields: Vec<EpiphanyStateUpdatedField>,
pub accepted_receipt_id: String,
pub accepted_observation_id: String,
pub accepted_evidence_id: String,
pub mind_review: MindGatewayReview,
}
pub struct EpiphanyReorientAcceptanceUpdate {
pub state_update: EpiphanyStateUpdate,
pub changed_fields: Vec<EpiphanyStateUpdatedField>,
pub accepted_receipt_id: String,
pub accepted_observation_id: String,
pub accepted_evidence_id: String,
pub mind_review: MindGatewayReview,
}
#[derive(Debug, Clone)]
pub struct EpiphanyCoordinatorRoleResultSnapshot {
pub status: EpiphanyCoordinatorRoleResultStatus,
@ -349,6 +380,194 @@ pub fn load_launch_organ_contract(
Ok(request.organ_launch_contract)
}
pub fn build_native_role_acceptance_update(
expected_revision: Option<u64>,
role_id: EpiphanyRoleResultRoleId,
binding_id: &str,
finding: &EpiphanyRoleFindingInterpretation,
accepted_evidence_id: String,
accepted_observation_id: String,
accepted_at: String,
) -> Result<EpiphanyRoleAcceptanceUpdate, String> {
let mut patch = finding.state_patch.clone().unwrap_or_default();
let errors = match role_id {
EpiphanyRoleResultRoleId::Imagination => imagination_role_state_patch_policy_errors(&patch),
EpiphanyRoleResultRoleId::Modeling => modeling_role_state_patch_policy_errors(&patch),
EpiphanyRoleResultRoleId::Research => research_role_state_patch_policy_errors(&patch),
EpiphanyRoleResultRoleId::Verification => {
patch = EpiphanyRoleStatePatchDocument::default();
Vec::new()
}
EpiphanyRoleResultRoleId::Implementation | EpiphanyRoleResultRoleId::Reorientation => {
return Err(format!(
"role {role_id:?} cannot be accepted through roleAccept"
));
}
};
if role_id != EpiphanyRoleResultRoleId::Verification && finding.state_patch.is_none() {
return Err("completed role finding did not include a reviewable statePatch".to_string());
}
if !errors.is_empty() {
return Err(format!(
"{} role state patch is not acceptable: {}",
role_label_lower(role_id),
errors.join("; ")
));
}
let projected_fields = changed_fields_from_patch(&patch)
.iter()
.map(|field| format!("{field:?}"))
.collect();
let mind_review = mind_review_role_acceptance(binding_id, role_id, finding, &patch);
mind_review_allows_state(&mind_review)?;
let bundle = build_role_acceptance_bundle(
binding_id,
EpiphanyRoleAcceptanceFinding {
role_id,
verdict: finding.verdict.clone(),
summary: finding.summary.clone(),
next_safe_move: finding.next_safe_move.clone(),
files_inspected: finding.files_inspected.clone(),
runtime_result_id: finding.runtime_result_id.clone(),
runtime_job_id: finding.runtime_job_id.clone(),
projected_fields,
},
accepted_evidence_id,
accepted_observation_id,
accepted_at,
)?;
let accepted_receipt_id = bundle.accepted_receipt_id.clone();
let accepted_observation_id = bundle.accepted_observation_id.clone();
let accepted_evidence_id = bundle.accepted_evidence_id.clone();
patch.evidence.push(bundle.evidence);
patch.observations.push(bundle.observation);
patch.acceptance_receipts.push(bundle.receipt);
let changed_fields = changed_fields_from_patch(&patch);
Ok(EpiphanyRoleAcceptanceUpdate {
state_update: state_update_from_patch(expected_revision, patch.clone()),
applied_patch: patch,
changed_fields,
accepted_receipt_id,
accepted_observation_id,
accepted_evidence_id,
mind_review,
})
}
pub fn build_native_reorient_acceptance_update(
expected_revision: Option<u64>,
binding_id: &str,
finding: &EpiphanyReorientFindingInterpretation,
accepted_evidence_id: String,
accepted_observation_id: String,
accepted_at: String,
update_scratch: bool,
update_investigation_checkpoint: bool,
checkpoint: Option<epiphany_state_model::EpiphanyInvestigationCheckpoint>,
) -> Result<EpiphanyReorientAcceptanceUpdate, String> {
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)?;
let bundle = build_reorient_acceptance_bundle(
binding_id,
mind_finding,
accepted_evidence_id,
accepted_observation_id,
accepted_at,
update_scratch,
update_investigation_checkpoint
.then_some(checkpoint)
.flatten(),
)?;
let accepted_receipt_id = bundle.accepted_receipt_id.clone();
let accepted_observation_id = bundle.accepted_observation_id.clone();
let accepted_evidence_id = bundle.accepted_evidence_id.clone();
let mut changed_fields = vec![
EpiphanyStateUpdatedField::AcceptanceReceipts,
EpiphanyStateUpdatedField::Observations,
EpiphanyStateUpdatedField::Evidence,
];
if bundle.scratch.is_some() {
changed_fields.push(EpiphanyStateUpdatedField::Scratch);
}
if bundle.investigation_checkpoint.is_some() {
changed_fields.push(EpiphanyStateUpdatedField::InvestigationCheckpoint);
}
Ok(EpiphanyReorientAcceptanceUpdate {
state_update: EpiphanyStateUpdate {
expected_revision,
scratch: bundle.scratch,
investigation_checkpoint: bundle.investigation_checkpoint,
acceptance_receipts: vec![bundle.receipt],
observations: vec![bundle.observation],
evidence: vec![bundle.evidence],
..Default::default()
},
changed_fields,
accepted_receipt_id,
accepted_observation_id,
accepted_evidence_id,
mind_review,
})
}
fn state_update_from_patch(
expected_revision: Option<u64>,
patch: EpiphanyRoleStatePatchDocument,
) -> 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,
}
}
fn changed_fields_from_patch(
patch: &EpiphanyRoleStatePatchDocument,
) -> Vec<EpiphanyStateUpdatedField> {
changed_fields(&state_update_from_patch(None, patch.clone()))
}
fn role_label_lower(role_id: EpiphanyRoleResultRoleId) -> &'static str {
match role_id {
EpiphanyRoleResultRoleId::Imagination => "imagination",
EpiphanyRoleResultRoleId::Modeling => "modeling",
EpiphanyRoleResultRoleId::Research => "research",
EpiphanyRoleResultRoleId::Verification => "verification",
EpiphanyRoleResultRoleId::Implementation => "implementation",
EpiphanyRoleResultRoleId::Reorientation => "reorientation",
}
}
fn latest_runtime_link<'a>(
state: &'a EpiphanyThreadState,
binding_id: &str,

View file

@ -124,7 +124,11 @@ pub use coordinator_service::EpiphanyCoordinatorReorientResultSnapshot;
pub use coordinator_service::EpiphanyCoordinatorRoleResultSnapshot;
pub use coordinator_service::EpiphanyCoordinatorService;
pub use coordinator_service::EpiphanyCoordinatorStateApplied;
pub use coordinator_service::EpiphanyReorientAcceptanceUpdate;
pub use coordinator_service::EpiphanyRoleAcceptanceUpdate;
pub use coordinator_service::apply_coordinator_state_update_to_state;
pub use coordinator_service::build_native_reorient_acceptance_update;
pub use coordinator_service::build_native_role_acceptance_update;
pub use coordinator_service::completed_reorient_finding;
pub use coordinator_service::completed_role_finding;
pub use coordinator_service::load_launch_organ_contract;

View file

@ -670,6 +670,13 @@ Mind-review requirement. `epiphany-codex-bridge::runtime_results` only maps the
native admission error into compatibility error classes. Full role/reorient
acceptance mutation and launch choreography remain in the bridge.
Role/reorientation acceptance mutation construction is now core-owned as well:
role-specific patch policy, Mind review, acceptance bundle, accepted
evidence/observation/receipt insertion, changed-field derivation, and the typed
state update are produced by `epiphany-core::coordinator_service`. The old
bridge builders were deleted rather than retained as fallbacks. Bridge receipt
persistence/proof enforcement and final state admission still remain to move.
Bridge identity correction from 2026-06-22: Epiphany owns swarm execution,
repo Body work, branch-local lanes, memory, evidence, and Persona cognition.
Bifrost owns GameCult identity, governed crossings into GitHub/Discord/Reddit

Binary file not shown.