From 7093a8b978ee53ad213b8e74579ccc64925f1c63 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 8 Aug 2026 16:08:27 +0100 Subject: [PATCH] Add receipted supervisor acceptance correction --- epiphany-core/src/bin/epiphany-mind-repair.rs | 23 ++- epiphany-core/src/coordinator_state.rs | 188 +++++++++++++++++- epiphany-core/src/lib.rs | 3 + 3 files changed, 210 insertions(+), 4 deletions(-) diff --git a/epiphany-core/src/bin/epiphany-mind-repair.rs b/epiphany-core/src/bin/epiphany-mind-repair.rs index 5d1dfdcd..2bedc167 100644 --- a/epiphany-core/src/bin/epiphany-mind-repair.rs +++ b/epiphany-core/src/bin/epiphany-mind-repair.rs @@ -1,7 +1,7 @@ use anyhow::{Result, anyhow}; use epiphany_core::{ RepoFrontierExecutionAmendment, RepoFrontierRoute, amend_repo_frontier_execution, - runtime_spine_cache, + apply_supervisor_modeling_acceptance_correction, runtime_spine_cache, }; use sha2::{Digest, Sha256}; use std::{collections::BTreeMap, env, path::PathBuf}; @@ -10,7 +10,10 @@ fn main() -> Result<()> { let mut values = BTreeMap::new(); let mut args = env::args().skip(1); let command = args.next().ok_or_else(|| anyhow!(usage()))?; - if command != "amend-frontier-execution" && command != "inspect-frontier-execution" { + if command != "amend-frontier-execution" + && command != "inspect-frontier-execution" + && command != "supersede-modeling-acceptance" + { return Err(anyhow!("unknown command {command}\n{}", usage())); } while let Some(flag) = args.next() { @@ -28,6 +31,20 @@ fn main() -> Result<()> { .ok_or_else(|| anyhow!("missing {name}")) }; let store = PathBuf::from(take("--store")?); + if command == "supersede-modeling-acceptance" { + let receipt = apply_supervisor_modeling_acceptance_correction( + &store, + &take("--thread-id")?, + take("--expected-revision")?.parse()?, + &take("--receipt-id")?, + &take("--result-id")?, + &take("--source-actor-id")?, + &take("--reason")?, + &take("--corrected-at")?, + )?; + println!("{}", serde_json::to_string_pretty(&receipt)?); + return Ok(()); + } let route_id = take("--route-id")?; if command == "inspect-frontier-execution" { let mut cache = runtime_spine_cache(&store)?; @@ -101,5 +118,5 @@ fn main() -> Result<()> { } fn usage() -> &'static str { - "usage: epiphany-mind-repair inspect-frontier-execution --store PATH --route-id ID\n epiphany-mind-repair amend-frontier-execution --store PATH --route-id ID --source-actor-id ID --command-id ID --admission-id ID --packet-sha256 SHA256 --previous-action TEXT --previous-command TEXT --action TEXT --replacement-command TEXT --rationale TEXT --amended-at RFC3339" + "usage: epiphany-mind-repair inspect-frontier-execution --store PATH --route-id ID\n epiphany-mind-repair amend-frontier-execution --store PATH --route-id ID --source-actor-id ID --command-id ID --admission-id ID --packet-sha256 SHA256 --previous-action TEXT --previous-command TEXT --action TEXT --replacement-command TEXT --rationale TEXT --amended-at RFC3339\n epiphany-mind-repair supersede-modeling-acceptance --store PATH --thread-id ID --expected-revision N --receipt-id ID --result-id ID --source-actor-id ID --reason missing-typed-future-frontier --corrected-at RFC3339" } diff --git a/epiphany-core/src/coordinator_state.rs b/epiphany-core/src/coordinator_state.rs index b9e24ae2..87bc80f7 100644 --- a/epiphany-core/src/coordinator_state.rs +++ b/epiphany-core/src/coordinator_state.rs @@ -7,7 +7,9 @@ use crate::epiphany_state_update_validation_errors; use crate::read_accepted_coordinator_state; use anyhow::Result; use anyhow::anyhow; -use epiphany_state_model::EpiphanyThreadState; +use epiphany_state_model::{EpiphanyEvidenceRecord, EpiphanyThreadState}; +use serde::Serialize; +use sha2::{Digest, Sha256}; use std::path::Path; #[derive(Debug, Clone, PartialEq)] @@ -17,6 +19,124 @@ pub struct EpiphanyCoordinatorStateApplied { pub state: EpiphanyThreadState, } +pub const SUPERVISOR_MODELING_ACCEPTANCE_CORRECTION_REASON: &str = + "missing-typed-future-frontier"; + +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct EpiphanySupervisorAcceptanceCorrectionReceipt { + pub correction_id: String, + pub thread_id: String, + pub source_actor_id: String, + pub prior_receipt_id: String, + pub prior_receipt_sha256: String, + pub result_id: String, + pub job_id: String, + pub binding_id: String, + pub role_id: String, + pub reason: String, + pub corrected_at: String, + pub prior_revision: u64, + pub corrected_revision: u64, +} + +pub fn apply_supervisor_modeling_acceptance_correction( + store: &Path, + thread_id: &str, + expected_revision: u64, + prior_receipt_id: &str, + result_id: &str, + source_actor_id: &str, + reason: &str, + corrected_at: &str, +) -> Result { + for (name, value) in [ + ("thread_id", thread_id), + ("prior_receipt_id", prior_receipt_id), + ("result_id", result_id), + ("source_actor_id", source_actor_id), + ] { + if value.trim().is_empty() { + return Err(anyhow!("supervisor acceptance correction requires {name}")); + } + } + if reason != SUPERVISOR_MODELING_ACCEPTANCE_CORRECTION_REASON { + return Err(anyhow!( + "unsupported supervisor Modeling acceptance correction reason" + )); + } + chrono::DateTime::parse_from_rfc3339(corrected_at) + .map_err(|_| anyhow!("supervisor acceptance correction timestamp must be RFC3339"))?; + + let current = read_coordinator_state(store)? + .ok_or_else(|| anyhow!("supervisor acceptance correction requires coordinator state"))?; + if current.revision != expected_revision { + return Err(anyhow!( + "supervisor acceptance correction revision mismatch: expected {expected_revision}, found {}", + current.revision + )); + } + let receipt_index = current + .acceptance_receipts + .iter() + .position(|receipt| receipt.id == prior_receipt_id) + .ok_or_else(|| anyhow!("supervisor acceptance correction receipt is absent"))?; + let prior = current.acceptance_receipts[receipt_index].clone(); + if prior.result_id != result_id + || prior.role_id != "modeling" + || prior.surface != "roleAccept" + || prior.status != "accepted" + { + return Err(anyhow!( + "supervisor acceptance correction requires the exact accepted Modeling receipt" + )); + } + let prior_receipt_sha256 = format!( + "{:x}", + Sha256::digest(rmp_serde::to_vec_named(&prior)?) + ); + let semantic = format!( + "{thread_id}\0{source_actor_id}\0{prior_receipt_id}\0{prior_receipt_sha256}\0{result_id}\0{reason}\0{corrected_at}\0{expected_revision}" + ); + let correction_id = format!( + "supervisor-acceptance-correction-{:x}", + Sha256::digest(semantic.as_bytes()) + ); + let mut next = current.clone(); + next.acceptance_receipts.remove(receipt_index); + next.recent_evidence.insert( + 0, + EpiphanyEvidenceRecord { + id: correction_id.clone(), + kind: "supervisor-runtime-repair".into(), + status: "ok".into(), + summary: format!( + "Superseded routing effect of accepted Modeling receipt {prior_receipt_id} ({prior_receipt_sha256}) for result {result_id}; reason={reason}. Immutable result and admitted RepoModel remain evidence." + ), + code_refs: Vec::new(), + }, + ); + next.revision = next.revision.saturating_add(1); + next.last_updated_turn_id = Some(correction_id.clone()); + let mut transaction = open_coordinator_state_transaction(store, ¤t)?; + commit_coordinator_state_transaction(&mut transaction, thread_id, &next, Vec::new(), Vec::new())?; + Ok(EpiphanySupervisorAcceptanceCorrectionReceipt { + correction_id, + thread_id: thread_id.to_string(), + source_actor_id: source_actor_id.to_string(), + prior_receipt_id: prior_receipt_id.to_string(), + prior_receipt_sha256, + result_id: result_id.to_string(), + job_id: prior.job_id, + binding_id: prior.binding_id, + role_id: prior.role_id, + reason: reason.to_string(), + corrected_at: corrected_at.to_string(), + prior_revision: current.revision, + corrected_revision: next.revision, + }) +} + pub fn read_coordinator_state(store: &Path) -> Result> { read_accepted_coordinator_state(store) } @@ -197,4 +317,70 @@ mod tests { ); Ok(()) } + + #[test] + fn supervisor_correction_removes_only_legacy_modeling_routing_authority() -> Result<()> { + let temp = tempfile::tempdir()?; + let store = temp.path().join("supervisor-correction.cc"); + let accepted = epiphany_state_model::EpiphanyAcceptanceReceipt { + id: "accept-modeling-legacy".into(), + result_id: "result-modeling-legacy".into(), + job_id: "job-modeling-legacy".into(), + binding_id: "epiphany-role-modeling".into(), + surface: "roleAccept".into(), + role_id: "modeling".into(), + status: "accepted".into(), + accepted_at: "2026-08-08T15:00:00Z".into(), + summary: Some("Legacy result omitted its now-required frontier.".into()), + ..Default::default() + }; + let seeded = apply_coordinator_state_update( + &store, + "thread-repair", + EpiphanyStateUpdate { + expected_revision: Some(0), + objective: Some("Preserve the admitted Body map.".into()), + acceptance_receipts: vec![accepted.clone()], + ..Default::default() + }, + None, + )?; + let before_hostile = std::fs::read(&store)?; + assert!( + apply_supervisor_modeling_acceptance_correction( + &store, + "thread-repair", + seeded.revision, + &accepted.id, + &accepted.result_id, + "operator-enginseer", + "invent-a-frontier", + "2026-08-08T15:01:00Z", + ) + .is_err() + ); + assert_eq!(std::fs::read(&store)?, before_hostile); + + let correction = apply_supervisor_modeling_acceptance_correction( + &store, + "thread-repair", + seeded.revision, + &accepted.id, + &accepted.result_id, + "operator-enginseer", + SUPERVISOR_MODELING_ACCEPTANCE_CORRECTION_REASON, + "2026-08-08T15:01:00Z", + )?; + let repaired = read_coordinator_state(&store)?.expect("repaired state"); + assert_eq!(correction.corrected_revision, seeded.revision + 1); + assert!(repaired.acceptance_receipts.is_empty()); + assert_eq!(repaired.objective.as_deref(), seeded.state.objective.as_deref()); + assert_eq!(repaired.recent_evidence[0].id, correction.correction_id); + assert!( + repaired.recent_evidence[0] + .summary + .contains(&correction.prior_receipt_sha256) + ); + Ok(()) + } } diff --git a/epiphany-core/src/lib.rs b/epiphany-core/src/lib.rs index 01a787f5..9474be84 100644 --- a/epiphany-core/src/lib.rs +++ b/epiphany-core/src/lib.rs @@ -240,9 +240,12 @@ pub use coordinator_results::read_runtime_reorient_result; pub use coordinator_results::read_runtime_role_result; pub use coordinator_service::EpiphanyCoordinatorService; pub use coordinator_state::EpiphanyCoordinatorStateApplied; +pub use coordinator_state::EpiphanySupervisorAcceptanceCorrectionReceipt; +pub use coordinator_state::SUPERVISOR_MODELING_ACCEPTANCE_CORRECTION_REASON; pub use coordinator_state::apply_coordinator_state_update; pub use coordinator_state::apply_coordinator_state_update_from_state; pub use coordinator_state::apply_coordinator_state_update_to_state; +pub use coordinator_state::apply_supervisor_modeling_acceptance_correction; pub use coordinator_state::read_coordinator_state; pub use cultcache_rs::CacheBackingStore; pub use cultcache_rs::CultCache;