Validate direct Epiphany replacement updates

This commit is contained in:
Meta Vens 2026-04-25 12:07:25 +01:00
commit e0f1a46844
10 changed files with 231 additions and 32 deletions

View file

@ -1,5 +1,7 @@
use crate::agent::AgentStatus;
use crate::config::ConstraintResult;
use crate::epiphany_promotion::EpiphanyStateReplacementValidationInput;
use crate::epiphany_promotion::validate_state_replacement_patch;
use crate::epiphany_retrieval;
use crate::epiphany_retrieval::EpiphanyRetrieveQuery;
use crate::epiphany_retrieval::EpiphanyRetrieveResponse;
@ -629,9 +631,41 @@ fn epiphany_state_update_validation_errors(
}
}
errors.extend(epiphany_state_replacement_validation_errors(state, update));
errors
}
fn epiphany_state_replacement_validation_errors(
state: &EpiphanyThreadState,
update: &EpiphanyStateUpdate,
) -> Vec<String> {
let validates_subgoal_target = update.subgoals.is_some() || update.active_subgoal_id.is_some();
let validates_graph_target = update.graphs.is_some()
|| update.graph_frontier.is_some()
|| update.graph_checkpoint.is_some();
validate_state_replacement_patch(EpiphanyStateReplacementValidationInput {
active_subgoal_id: update.active_subgoal_id.as_deref(),
subgoals: if validates_subgoal_target {
update
.subgoals
.as_deref()
.or(Some(state.subgoals.as_slice()))
} else {
None
},
invariants: update.invariants.as_deref(),
graphs: if validates_graph_target {
update.graphs.as_ref().or(Some(&state.graphs))
} else {
None
},
graph_frontier: update.graph_frontier.as_ref(),
graph_checkpoint: update.graph_checkpoint.as_ref(),
churn: update.churn.as_ref(),
})
}
fn nonempty_id(id: &str) -> Option<&str> {
if id.is_empty() { None } else { Some(id) }
}
@ -695,6 +729,8 @@ fn prepend_recent<T>(items: &mut Vec<T>, mut new_items: Vec<T>) {
#[cfg(test)]
mod epiphany_update_tests {
use super::*;
use codex_protocol::protocol::EpiphanyGraph;
use codex_protocol::protocol::EpiphanyGraphNode;
fn evidence(id: &str) -> EpiphanyEvidenceRecord {
EpiphanyEvidenceRecord {
@ -859,6 +895,40 @@ mod epiphany_update_tests {
.any(|error| error.contains("patch.observations.id must not be empty"))
);
}
#[test]
fn validate_epiphany_state_update_rejects_structurally_invalid_replacements() {
let state = EpiphanyThreadState {
graphs: EpiphanyGraphs {
architecture: EpiphanyGraph {
nodes: vec![EpiphanyGraphNode {
id: "state".to_string(),
title: "State".to_string(),
purpose: "Carry the explicit map".to_string(),
..Default::default()
}],
edges: Vec::new(),
},
..Default::default()
},
..Default::default()
};
let update = EpiphanyStateUpdate {
graph_frontier: Some(EpiphanyGraphFrontier {
active_node_ids: vec!["missing".to_string()],
..Default::default()
}),
..Default::default()
};
let errors = epiphany_state_update_validation_errors(&state, &update);
assert!(
errors
.iter()
.any(|error| error.contains("graph frontier references missing node"))
);
}
}
fn pending_message_input_item(message: &ResponseItem) -> CodexResult<ResponseInputItem> {

View file

@ -1,3 +1,5 @@
pub use epiphany_core::EpiphanyPromotionDecision;
pub use epiphany_core::EpiphanyPromotionInput;
pub use epiphany_core::EpiphanyStateReplacementValidationInput;
pub use epiphany_core::evaluate_promotion;
pub use epiphany_core::validate_state_replacement_patch;