Move coordinator judgment into core

This commit is contained in:
Meta Vens 2026-05-20 08:39:56 +01:00
commit 3d4ed26b0f
9 changed files with 461 additions and 285 deletions

View file

@ -15,15 +15,14 @@ use codex_app_server_protocol::ThreadEpiphanySceneAction;
use codex_app_server_protocol::ThreadEpiphanyViewCoordinator;
use epiphany_core::EpiphanyCoordinatorAction as CoreEpiphanyCoordinatorAction;
use epiphany_core::EpiphanyCoordinatorAutomationAction as CoreEpiphanyCoordinatorAutomationAction;
use epiphany_core::EpiphanyCoordinatorCrrcRecommendation;
use epiphany_core::EpiphanyCoordinatorDecision as CoreEpiphanyCoordinatorDecision;
use epiphany_core::EpiphanyCoordinatorInput;
use epiphany_core::EpiphanyCoordinatorRoleId as CoreEpiphanyCoordinatorRoleId;
use epiphany_core::EpiphanyCoordinatorRoleLane as CoreEpiphanyCoordinatorRoleLane;
use epiphany_core::EpiphanyCoordinatorRoleResultStatus as CoreEpiphanyCoordinatorRoleResultStatus;
use epiphany_core::EpiphanyCoordinatorRoleStatus as CoreEpiphanyCoordinatorRoleStatus;
use epiphany_core::EpiphanyCoordinatorSceneAction as CoreEpiphanyCoordinatorSceneAction;
use epiphany_core::EpiphanyCoordinatorSignals;
use epiphany_core::EpiphanyCoordinatorSourceSignals;
use epiphany_core::EpiphanyCoordinatorStatus as CoreEpiphanyCoordinatorStatus;
use epiphany_core::EpiphanyCoordinatorStatusInput;
use epiphany_core::EpiphanyCrrcAction as CoreEpiphanyCrrcAction;
use epiphany_core::EpiphanyCrrcInput;
use epiphany_core::EpiphanyCrrcRecommendation as CoreEpiphanyCrrcRecommendation;
@ -47,10 +46,9 @@ use epiphany_core::EpiphanyRoleBoardPlanningSummary;
use epiphany_core::EpiphanyRoleFindingInterpretation as CoreEpiphanyRoleFinding;
use epiphany_core::EpiphanyRoleResultRoleId;
use epiphany_core::EpiphanyTokenUsageSnapshot;
use epiphany_core::coordinator_automation_action;
use epiphany_core::derive_coordinator_status;
use epiphany_core::derive_role_board;
use epiphany_core::modeling_role_state_patch_policy_errors;
use epiphany_core::recommend_coordinator_action;
use epiphany_core::recommend_crrc_action;
use epiphany_core::render_role_board_note;
use epiphany_core::select_coordinator_automation_action;
@ -236,24 +234,9 @@ pub fn map_protocol_crrc_recommendation(
pub type EpiphanyCoordinatorDecision = CoreEpiphanyCoordinatorDecision;
pub struct EpiphanyCoordinatorSourceSignals {
pub pressure_level: epiphany_core::EpiphanyPressureLevel,
pub should_prepare_compaction: bool,
pub reorient_action: CoreEpiphanyReorientAction,
pub crrc_action: CoreEpiphanyCrrcAction,
pub modeling_result_status: CoreEpiphanyCoordinatorRoleResultStatus,
pub verification_result_status: CoreEpiphanyCoordinatorRoleResultStatus,
pub reorient_result_status: CoreEpiphanyCrrcResultStatus,
}
pub struct EpiphanyCoordinatorStatus {
pub decision: EpiphanyCoordinatorDecision,
pub source_signals: EpiphanyCoordinatorSourceSignals,
pub roles: Vec<EpiphanyRoleBoardLane>,
pub core: CoreEpiphanyCoordinatorStatus,
pub note: String,
pub modeling_result_accepted: bool,
pub verification_result_accepted: bool,
pub reorient_finding_accepted: bool,
}
pub async fn derive_epiphany_coordinator_status(
@ -351,24 +334,18 @@ pub async fn derive_epiphany_coordinator_status(
let reorient_finding_accepted = reorient_finding.is_some_and(|finding| {
state.is_some_and(|state| epiphany_reorient_finding_already_accepted(state, finding))
});
let source_signals = EpiphanyCoordinatorSourceSignals {
pressure_level: pressure.level,
should_prepare_compaction: pressure.should_prepare_compaction,
let core = derive_coordinator_status(EpiphanyCoordinatorStatusInput {
state_status: map_core_crrc_state_status_from_reorient(state_status),
checkpoint_present,
pressure: pressure.clone(),
recommendation: recommendation.clone(),
roles,
reorient_action: reorient_decision
.map(|decision| decision.action)
.unwrap_or(CoreEpiphanyReorientAction::Resume),
crrc_action: recommendation.action,
modeling_result_status,
verification_result_status,
reorient_result_status,
};
let decision = map_epiphany_coordinator(
state_status,
checkpoint_present,
pressure,
recommendation,
&roles,
&source_signals,
modeling_result_accepted,
modeling_result_reviewable,
modeling_result_accepted_after_verification,
@ -379,25 +356,17 @@ pub async fn derive_epiphany_coordinator_status(
verification_result_allows_implementation,
verification_result_needs_evidence,
reorient_finding_accepted,
);
});
let note = render_epiphany_coordinator_note(
recommendation.action,
pressure.level,
modeling_result_status,
verification_result_status,
source_signals.reorient_result_status,
decision.action,
core.source_signals.reorient_result_status,
core.decision.action,
);
EpiphanyCoordinatorStatus {
decision,
source_signals,
roles,
note,
modeling_result_accepted,
verification_result_accepted,
reorient_finding_accepted,
}
EpiphanyCoordinatorStatus { core, note }
}
pub fn map_epiphany_coordinator_view(
@ -417,46 +386,27 @@ pub fn map_epiphany_coordinator_view(
},
state_status: map_protocol_reorient_state_status(state_status),
state_revision,
action: map_protocol_coordinator_action(status.decision.action),
action: map_protocol_coordinator_action(status.core.decision.action),
target_role: status
.core
.decision
.target_role
.map(map_protocol_coordinator_role_id),
recommended_scene_action: status
.core
.decision
.recommended_scene_action
.map(map_protocol_coordinator_scene_action),
requires_review: status.decision.requires_review,
can_auto_run: status.decision.can_auto_run,
reason: status.decision.reason,
source_signals: map_protocol_coordinator_source_signals(status.source_signals),
requires_review: status.core.decision.requires_review,
can_auto_run: status.core.decision.can_auto_run,
reason: status.core.decision.reason,
source_signals: map_protocol_coordinator_source_signals(status.core.source_signals),
roles: map_protocol_role_board_lanes(&roles),
note: status.note,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EpiphanyCoordinatorAutomationAction {
None,
CompactRehydrateReorient,
LaunchReorientWorker,
}
pub fn map_epiphany_coordinator_automation_action(
decision: &EpiphanyCoordinatorDecision,
) -> EpiphanyCoordinatorAutomationAction {
map_protocol_coordinator_automation_action(coordinator_automation_action(decision))
}
pub fn select_epiphany_coordinator_automation_action(
decision: &EpiphanyCoordinatorDecision,
force_checkpoint_compaction: bool,
) -> EpiphanyCoordinatorAutomationAction {
map_protocol_coordinator_automation_action(select_coordinator_automation_action(
decision,
force_checkpoint_compaction,
))
}
pub type EpiphanyCoordinatorAutomationAction = CoreEpiphanyCoordinatorAutomationAction;
pub struct EpiphanyCoordinatorAutomationInput<'a> {
pub thread_id: &'a str,
@ -544,8 +494,8 @@ pub async fn select_epiphany_coordinator_automation(
)
.await;
let action = select_epiphany_coordinator_automation_action(
&coordinator.decision,
let action = select_coordinator_automation_action(
&coordinator.core.decision,
input.force_checkpoint_compaction,
);
let launch_request = match action {
@ -582,64 +532,6 @@ pub async fn select_epiphany_coordinator_automation(
}
}
pub fn map_epiphany_coordinator(
state_status: CoreEpiphanyReorientStateStatus,
checkpoint_present: bool,
pressure: &epiphany_core::EpiphanyPressure,
recommendation: &CoreEpiphanyCrrcRecommendation,
roles: &[EpiphanyRoleBoardLane],
signals: &EpiphanyCoordinatorSourceSignals,
modeling_result_accepted: bool,
modeling_result_reviewable: bool,
modeling_result_accepted_after_verification: bool,
implementation_evidence_after_verification: bool,
verification_result_cites_implementation_evidence: bool,
verification_result_covers_current_modeling: bool,
verification_result_accepted: bool,
verification_result_allows_implementation: bool,
verification_result_needs_evidence: bool,
reorient_finding_accepted: bool,
) -> EpiphanyCoordinatorDecision {
recommend_coordinator_action(EpiphanyCoordinatorInput {
state_status: map_core_crrc_state_status_from_reorient(state_status),
checkpoint_present,
should_prepare_compaction: pressure.should_prepare_compaction,
recommendation: EpiphanyCoordinatorCrrcRecommendation {
action: recommendation.action,
recommended_scene_action: recommendation
.recommended_scene_action
.map(epiphany_core::crrc_scene_action_to_coordinator_scene_action),
},
roles: roles
.iter()
.map(map_core_coordinator_role_lane_from_role_board)
.collect(),
signals: EpiphanyCoordinatorSignals {
modeling_result_status: signals.modeling_result_status,
verification_result_status: signals.verification_result_status,
},
modeling_result_accepted,
modeling_result_reviewable,
modeling_result_accepted_after_verification,
implementation_evidence_after_verification,
verification_result_cites_implementation_evidence,
verification_result_covers_current_modeling,
verification_result_accepted,
verification_result_allows_implementation,
verification_result_needs_evidence,
reorient_finding_accepted,
})
}
fn map_core_coordinator_role_lane_from_role_board(
role: &EpiphanyRoleBoardLane,
) -> CoreEpiphanyCoordinatorRoleLane {
CoreEpiphanyCoordinatorRoleLane {
id: role.id,
status: role.status,
}
}
fn map_protocol_coordinator_role_id(
role_id: CoreEpiphanyCoordinatorRoleId,
) -> ThreadEpiphanyRoleId {
@ -740,20 +632,6 @@ fn map_protocol_coordinator_action(
}
}
fn map_protocol_coordinator_automation_action(
action: CoreEpiphanyCoordinatorAutomationAction,
) -> EpiphanyCoordinatorAutomationAction {
match action {
CoreEpiphanyCoordinatorAutomationAction::None => EpiphanyCoordinatorAutomationAction::None,
CoreEpiphanyCoordinatorAutomationAction::CompactRehydrateReorient => {
EpiphanyCoordinatorAutomationAction::CompactRehydrateReorient
}
CoreEpiphanyCoordinatorAutomationAction::LaunchReorientWorker => {
EpiphanyCoordinatorAutomationAction::LaunchReorientWorker
}
}
}
pub fn epiphany_reorient_finding_already_accepted(
state: &EpiphanyThreadState,
finding: &CoreEpiphanyReorientFinding,

View file

@ -1,4 +1,3 @@
use std::path::Path;
use std::sync::OnceLock;
use codex_app_server_protocol::ThreadEpiphanyReorientWorkerLaunchDocument;
@ -12,15 +11,13 @@ use epiphany_core::EpiphanyJobLaunchRequest;
use epiphany_core::EpiphanyPressureLevel as CoreEpiphanyPressureLevel;
use epiphany_core::EpiphanyReorientAction as CoreEpiphanyReorientAction;
use epiphany_core::EpiphanyReorientDecision as CoreEpiphanyReorientDecision;
use epiphany_core::EpiphanyReorientFreshnessStatus as CoreEpiphanyReorientFreshnessStatus;
use epiphany_core::EpiphanyReorientPressureLevel as CoreEpiphanyReorientPressureLevel;
use epiphany_core::EpiphanyReorientReason as CoreEpiphanyReorientReason;
use epiphany_core::EpiphanyReorientLaunchRequestInput;
use epiphany_core::EpiphanyReorientWorkerLaunchDocument;
use epiphany_core::EpiphanyRoleResultRoleId;
use epiphany_core::EpiphanyRoleWorkerLaunchDocument;
use epiphany_core::EpiphanyWorkerLaunchDocument;
use epiphany_core::build_reorient_job_launch_request;
use epiphany_state_model::EpiphanyInvestigationCheckpoint;
use epiphany_state_model::EpiphanyInvestigationDisposition;
use epiphany_state_model::EpiphanyJobKind as CoreEpiphanyJobKind;
use epiphany_state_model::EpiphanyThreadState;
@ -487,88 +484,18 @@ pub fn build_epiphany_reorient_launch_request(
checkpoint: &EpiphanyInvestigationCheckpoint,
decision: &CoreEpiphanyReorientDecision,
) -> EpiphanyJobLaunchRequest {
let linked_subgoal_ids = epiphany_active_subgoal_ids(Some(state));
let linked_graph_node_ids = unique_strings(
epiphany_active_graph_node_ids(Some(state))
.into_iter()
.chain(decision.active_frontier_node_ids.iter().cloned()),
);
let checkpoint_next_action = checkpoint
.next_action
.clone()
.unwrap_or_else(|| decision.next_action.clone());
let authority_scope = match decision.action {
CoreEpiphanyReorientAction::Resume => "epiphany.reorient.resume",
CoreEpiphanyReorientAction::Regather => "epiphany.reorient.regather",
};
let scope = match decision.action {
CoreEpiphanyReorientAction::Resume => "reorient-guided checkpoint resume",
CoreEpiphanyReorientAction::Regather => "reorient-guided checkpoint regather",
};
let instruction = build_epiphany_reorient_launch_instruction(decision.action);
let launch_document =
EpiphanyWorkerLaunchDocument::Reorient(EpiphanyReorientWorkerLaunchDocument {
thread_id: thread_id.to_string(),
mode: reorient_action_label(decision.action).to_string(),
checkpoint_id: checkpoint.checkpoint_id.clone(),
checkpoint_kind: checkpoint.kind.clone(),
checkpoint_disposition: investigation_disposition_label(checkpoint.disposition)
.to_string(),
checkpoint_focus: Some(checkpoint.focus.clone()),
checkpoint_summary: checkpoint.summary.clone(),
checkpoint_next_action,
checkpoint_open_questions: checkpoint.open_questions.clone(),
checkpoint_evidence_ids: checkpoint.evidence_ids.clone(),
checkpoint_code_refs: checkpoint.code_refs.clone(),
decision_reasons: decision
.reasons
.iter()
.map(|reason| reorient_reason_label(*reason).to_string())
.collect(),
decision_note: decision.note.clone(),
pressure_level: reorient_pressure_level_label(decision.pressure_level).to_string(),
retrieval_status: reorient_freshness_status_label(decision.retrieval_status)
.to_string(),
graph_status: reorient_freshness_status_label(decision.graph_status).to_string(),
watcher_status: reorient_freshness_status_label(decision.watcher_status).to_string(),
checkpoint_dirty_paths: decision
.checkpoint_dirty_paths
.iter()
.map(path_to_display_string)
.collect(),
checkpoint_changed_paths: decision
.checkpoint_changed_paths
.iter()
.map(path_to_display_string)
.collect(),
scratch: state.scratch.clone(),
graphs: Some(state.graphs.clone()),
recent_evidence: state.recent_evidence.iter().take(8).cloned().collect(),
recent_observations: state.observations.iter().take(8).cloned().collect(),
active_frontier_node_ids: decision.active_frontier_node_ids.clone(),
linked_subgoal_ids: linked_subgoal_ids.clone(),
linked_graph_node_ids: linked_graph_node_ids.clone(),
});
let output_contract_id = launch_document.output_contract_id().to_string();
EpiphanyJobLaunchRequest {
build_reorient_job_launch_request(EpiphanyReorientLaunchRequestInput {
thread_id,
expected_revision,
binding_id: EPIPHANY_REORIENT_LAUNCH_BINDING_ID.to_string(),
kind: CoreEpiphanyJobKind::Specialist,
scope: scope.to_string(),
owner_role: EPIPHANY_REORIENT_OWNER_ROLE.to_string(),
authority_scope: authority_scope.to_string(),
linked_subgoal_ids: epiphany_active_subgoal_ids(Some(state)),
linked_graph_node_ids: unique_strings(
epiphany_active_graph_node_ids(Some(state))
.into_iter()
.chain(decision.active_frontier_node_ids.iter().cloned()),
),
instruction,
launch_document,
output_contract_id,
max_runtime_seconds,
}
binding_id: EPIPHANY_REORIENT_LAUNCH_BINDING_ID,
owner_role: EPIPHANY_REORIENT_OWNER_ROLE,
instruction,
state,
checkpoint,
decision,
})
}
pub fn build_epiphany_reorient_launch_instruction(action: CoreEpiphanyReorientAction) -> String {
@ -741,56 +668,3 @@ fn epiphany_active_graph_node_ids(state: Option<&EpiphanyThreadState>) -> Vec<St
.map(|frontier| frontier.active_node_ids.clone())
.unwrap_or_default()
}
fn path_to_display_string(path: impl AsRef<Path>) -> String {
path.as_ref().to_string_lossy().to_string()
}
fn reorient_action_label(action: CoreEpiphanyReorientAction) -> &'static str {
match action {
CoreEpiphanyReorientAction::Resume => "resume",
CoreEpiphanyReorientAction::Regather => "regather",
}
}
fn investigation_disposition_label(disposition: EpiphanyInvestigationDisposition) -> &'static str {
match disposition {
EpiphanyInvestigationDisposition::ResumeReady => "resume_ready",
EpiphanyInvestigationDisposition::RegatherRequired => "regather_required",
}
}
fn reorient_reason_label(reason: CoreEpiphanyReorientReason) -> &'static str {
match reason {
CoreEpiphanyReorientReason::MissingState => "missingState",
CoreEpiphanyReorientReason::MissingCheckpoint => "missingCheckpoint",
CoreEpiphanyReorientReason::CheckpointRequestedRegather => "checkpointRequestedRegather",
CoreEpiphanyReorientReason::CheckpointPathsDirty => "checkpointPathsDirty",
CoreEpiphanyReorientReason::CheckpointPathsChanged => "checkpointPathsChanged",
CoreEpiphanyReorientReason::FrontierChanged => "frontierChanged",
CoreEpiphanyReorientReason::UnanchoredCheckpointWhileStateStale => {
"unanchoredCheckpointWhileStateStale"
}
CoreEpiphanyReorientReason::CheckpointReady => "checkpointReady",
}
}
fn reorient_pressure_level_label(level: CoreEpiphanyReorientPressureLevel) -> &'static str {
match level {
CoreEpiphanyReorientPressureLevel::Unknown => "unknown",
CoreEpiphanyReorientPressureLevel::Low => "low",
CoreEpiphanyReorientPressureLevel::Medium => "medium",
CoreEpiphanyReorientPressureLevel::High => "high",
CoreEpiphanyReorientPressureLevel::Critical => "critical",
}
}
fn reorient_freshness_status_label(status: CoreEpiphanyReorientFreshnessStatus) -> &'static str {
match status {
CoreEpiphanyReorientFreshnessStatus::Unknown => "unknown",
CoreEpiphanyReorientFreshnessStatus::Clean => "clean",
CoreEpiphanyReorientFreshnessStatus::Dirty => "dirty",
CoreEpiphanyReorientFreshnessStatus::Stale => "stale",
CoreEpiphanyReorientFreshnessStatus::Changed => "changed",
}
}