diff --git a/epiphany-core/src/bin/epiphany-cluster-daemon.rs b/epiphany-core/src/bin/epiphany-cluster-daemon.rs index caf84036..9ed10b36 100644 --- a/epiphany-core/src/bin/epiphany-cluster-daemon.rs +++ b/epiphany-core/src/bin/epiphany-cluster-daemon.rs @@ -1,17 +1,20 @@ use anyhow::Context; use anyhow::Result; use chrono::Utc; +use epiphany_core::EPIPHANY_CULTMESH_DAEMON_HEARTBEAT_EVENT_SCHEMA_VERSION; use epiphany_core::EPIPHANY_CULTMESH_DAEMON_STATUS_SCHEMA_VERSION; +use epiphany_core::EpiphanyCultMeshDaemonHeartbeatEventEntry; use epiphany_core::EpiphanyCultMeshDaemonStatusEntry; use epiphany_core::load_epiphany_cultmesh_cluster_topology; -use epiphany_core::load_epiphany_cultmesh_daemon_liveness; use epiphany_core::publish_epiphany_cultmesh_provider_state; +use epiphany_core::write_epiphany_cultmesh_daemon_heartbeat_event; use epiphany_core::write_epiphany_cultmesh_daemon_status; use serde_json::json; use std::env; use std::path::PathBuf; use std::thread; use std::time::Duration; +use uuid::Uuid; fn main() -> Result<()> { let args = Args::parse()?; @@ -39,7 +42,10 @@ fn serve(args: Args) -> Result<()> { print_output(&args, "serveComplete", written) } -fn write_heartbeat(args: &Args, iteration: u64) -> Result { +fn write_heartbeat( + args: &Args, + iteration: u64, +) -> Result { let topology = load_epiphany_cultmesh_cluster_topology(&args.store, args.runtime_id.clone())?; let cluster = topology .into_iter() @@ -55,52 +61,76 @@ fn write_heartbeat(args: &Args, iteration: u64) -> Result EpiphanyCultMeshDaemonHeartbeatEventEntry { + EpiphanyCultMeshDaemonHeartbeatEventEntry { + schema_version: EPIPHANY_CULTMESH_DAEMON_HEARTBEAT_EVENT_SCHEMA_VERSION.to_string(), + heartbeat_id: Uuid::new_v4().to_string(), + daemon_id: daemon_id.to_string(), + cluster_id: cluster_id.to_string(), + provider_incarnation: args.provider_incarnation.clone(), + sequence: iteration.saturating_add(1), + status: "ready".to_string(), + heartbeat_at, + private_state_exposed: false, + startup_lifecycle_receipt_id: args.startup_lifecycle_receipt_id.clone(), + } } fn print_output( args: &Args, status: &str, - written: Vec, + written: Vec, ) -> Result<()> { let latest = written .last() @@ -115,7 +145,10 @@ fn print_output( "daemonId": latest.daemon_id, "clusterId": latest.cluster_id, "daemonStatus": latest.status, - "lastHeartbeatUtc": latest.last_heartbeat_utc, + "heartbeatId": latest.heartbeat_id, + "providerIncarnation": latest.provider_incarnation, + "sequence": latest.sequence, + "lastHeartbeatUtc": latest.heartbeat_at, "heartbeatCount": written.len(), "intervalSeconds": args.interval_seconds, "maxIterations": args.max_iterations, @@ -132,6 +165,8 @@ struct Args { daemon_id: String, interval_seconds: u64, max_iterations: u64, + provider_incarnation: String, + startup_lifecycle_receipt_id: String, } impl Args { @@ -143,6 +178,9 @@ impl Args { let mut daemon_id = None; let mut interval_seconds = 60_u64; let mut max_iterations = 1_u64; + let mut provider_incarnation = None; + let mut startup_lifecycle_receipt_id = + env::var("EPIPHANY_STARTUP_LIFECYCLE_RECEIPT_ID").unwrap_or_default(); while let Some(arg) = values.next() { match arg.as_str() { @@ -165,6 +203,18 @@ impl Args { .context("missing --max-iterations value")? .parse()?; } + "--provider-incarnation" => { + provider_incarnation = Some( + values + .next() + .context("missing --provider-incarnation value")?, + ) + } + "--startup-lifecycle-receipt-id" => { + startup_lifecycle_receipt_id = values + .next() + .context("missing --startup-lifecycle-receipt-id value")? + } other => anyhow::bail!("unknown argument {other:?}"), } } @@ -177,6 +227,47 @@ impl Args { daemon_id, interval_seconds, max_iterations, + provider_incarnation: provider_incarnation + .unwrap_or_else(|| Uuid::new_v4().to_string()), + startup_lifecycle_receipt_id, }) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn process_heartbeat_events_keep_one_incarnation_and_increment_sequence() { + let args = Args { + command: "serve".to_string(), + store: PathBuf::from("unused.ccmp"), + runtime_id: "runtime-test".to_string(), + daemon_id: "daemon-test".to_string(), + interval_seconds: 1, + max_iterations: 2, + provider_incarnation: "incarnation-test".to_string(), + startup_lifecycle_receipt_id: "receipt-test".to_string(), + }; + let first = new_heartbeat_event( + &args, + "daemon-test", + "cluster-test", + 0, + "2026-07-15T12:00:00Z".to_string(), + ); + let second = new_heartbeat_event( + &args, + "daemon-test", + "cluster-test", + 1, + "2026-07-15T12:00:01Z".to_string(), + ); + assert_eq!(first.provider_incarnation, second.provider_incarnation); + assert_eq!(first.sequence, 1); + assert_eq!(second.sequence, 2); + assert_ne!(first.heartbeat_id, second.heartbeat_id); + assert_eq!(first.startup_lifecycle_receipt_id, "receipt-test"); + } +} diff --git a/epiphany-core/src/bin/epiphany-daemon-supervisor.rs b/epiphany-core/src/bin/epiphany-daemon-supervisor.rs index 04c7c020..3a7f5cd5 100644 --- a/epiphany-core/src/bin/epiphany-daemon-supervisor.rs +++ b/epiphany-core/src/bin/epiphany-daemon-supervisor.rs @@ -16,10 +16,14 @@ use epiphany_core::EpiphanyCultMeshSwarmBrakeEntry; use epiphany_core::EpiphanyLocalVerseContext; use epiphany_core::EpiphanyProcessObservation as ProcessObservation; use epiphany_core::EpiphanyServiceExecutionAuditReport; +use epiphany_core::MemorySemanticProjectionInput; +use epiphany_core::agent_memory_semantic_projection_input; use epiphany_core::epiphany_cluster_service_execution_audit_report; use epiphany_core::epiphany_cultmesh_daemon_poke_intent_from_status; use epiphany_core::epiphany_cultmesh_daemon_poke_receipt_for_intent; use epiphany_core::epiphany_service_execution_audit_report; +use epiphany_core::idunn_acquire_memory_semantic_projection_for_local_supervisor; +use epiphany_core::idunn_recover_memory_semantic_projection_from_cultmesh; use epiphany_core::load_epiphany_cultmesh_cluster_topology; use epiphany_core::load_epiphany_cultmesh_daemon_restart_policy; use epiphany_core::load_epiphany_cultmesh_daemon_service_lifecycle_receipts; @@ -29,6 +33,7 @@ use epiphany_core::load_epiphany_cultmesh_status; use epiphany_core::load_epiphany_cultmesh_swarm_brake; use epiphany_core::observe_native_process as observe_process; use epiphany_core::query_epiphany_local_verse_context; +use epiphany_core::runtime_modeling_semantic_projection_input; use epiphany_core::seed_epiphany_local_verse_context; use epiphany_core::write_epiphany_cultmesh_daemon_poke_intent; use epiphany_core::write_epiphany_cultmesh_daemon_poke_receipt; @@ -130,12 +135,82 @@ fn main() -> Result<()> { "windows-service-start" | "service-start" => windows_service_control(args, "start"), "windows-service-stop" | "service-stop" => windows_service_control(args, "stop"), "policy" | "write-policy" => write_policy(args), + "semantic-acquire" => semantic_acquire(args), + "semantic-recover" => semantic_recover(args), other => anyhow::bail!( - "unknown command {other:?}; use reconcile, tick, serve, service-plan, service-launch, service-runbook, repo-work-service-audit, cluster-service-runbook, cluster-service-install-plan, cluster-service-audit, cluster-service-start, cluster-service-stop, cluster-service-execution-readiness, cluster-service-execution-runbook, cluster-service-execution-audit, windows-service-install, windows-service-execution-readiness, windows-service-execution-runbook, windows-service-execution-audit, service-execution-audit-smoke, windows-service-reconcile, windows-service-status, windows-service-start, windows-service-stop, or policy" + "unknown command {other:?}; use reconcile, tick, serve, service-plan, service-launch, service-runbook, repo-work-service-audit, cluster-service-runbook, cluster-service-install-plan, cluster-service-audit, cluster-service-start, cluster-service-stop, cluster-service-execution-readiness, cluster-service-execution-runbook, cluster-service-execution-audit, windows-service-install, windows-service-execution-readiness, windows-service-execution-runbook, windows-service-execution-audit, service-execution-audit-smoke, windows-service-reconcile, windows-service-status, windows-service-start, windows-service-stop, policy, semantic-acquire, or semantic-recover" ), } } +fn semantic_projection_input(args: &Args) -> Result<(MemorySemanticProjectionInput, &Path)> { + match (&args.agent_store, &args.runtime_store) { + (Some(path), None) => Ok((agent_memory_semantic_projection_input(path)?, path)), + (None, Some(path)) => Ok((runtime_modeling_semantic_projection_input(path)?, path)), + _ => anyhow::bail!( + "semantic projector commands require exactly one of --agent-store or --runtime-store" + ), + } +} + +fn required<'a>(value: &'a Option, flag: &str) -> Result<&'a str> { + value.as_deref().with_context(|| format!("missing {flag}")) +} + +fn semantic_acquire(args: Args) -> Result<()> { + let (input, store) = semantic_projection_input(&args)?; + let purpose = required(&args.semantic_purpose, "--purpose")?; + if !matches!(purpose, "execute" | "repair") { + anyhow::bail!("--purpose must be execute or repair"); + } + let acquisition = idunn_acquire_memory_semantic_projection_for_local_supervisor( + store, + &input, + required(&args.executor_id, "--executor-id")?, + purpose, + &Utc::now().to_rfc3339(), + )?; + println!( + "{}", + serde_json::to_string_pretty(&json!({ + "grantId": acquisition.grant.grant_id, + "claimId": acquisition.claim.claim_id, + "epoch": acquisition.claim.epoch, + "executorId": acquisition.claim.executor_id, + "purpose": purpose, + "privateStateExposed": false + }))? + ); + Ok(()) +} + +fn semantic_recover(args: Args) -> Result<()> { + let (input, store) = semantic_projection_input(&args)?; + let (authorization, claim) = idunn_recover_memory_semantic_projection_from_cultmesh( + &args.store, + args.runtime_id.clone(), + store, + &input, + required(&args.expected_claim_id, "--expected-claim-id")?, + required(&args.executor_id, "--executor-id")?, + required(&args.receipt_id, "--receipt-id")?, + required(&args.provider_heartbeat_id, "--provider-heartbeat-id")?, + &Utc::now().to_rfc3339(), + )?; + println!( + "{}", + serde_json::to_string_pretty(&json!({ + "authorizationId": authorization.authorization_id, + "claimId": claim.claim_id, + "epoch": claim.epoch, + "executorId": claim.executor_id, + "executorIncarnation": claim.executor_incarnation, + "privateStateExposed": false + }))? + ); + Ok(()) +} + fn require_supervisor_bootstrap(args: &Args) -> Result<()> { load_epiphany_cultmesh_status(&args.store, args.runtime_id.clone())? .with_context(|| { @@ -2667,7 +2742,7 @@ fn reconcile_daemon_status( )?; let attempted_at = Utc::now(); - let restart_output = run_restart_command(policy)?; + let restart_output = run_restart_command(policy, &receipt_id)?; let completed_at = Utc::now(); let resulting_status = restart_lifecycle_observation(&restart_output).to_string(); let receipt_status = if restart_output.success { @@ -3344,9 +3419,16 @@ fn service_lifecycle_receipt( } } -fn run_restart_command(policy: &EpiphanyCultMeshDaemonRestartPolicyEntry) -> Result { +fn run_restart_command( + policy: &EpiphanyCultMeshDaemonRestartPolicyEntry, + lifecycle_receipt_id: &str, +) -> Result { let mut command = Command::new(&policy.restart_command); command.args(&policy.restart_args); + command.env( + "EPIPHANY_STARTUP_LIFECYCLE_RECEIPT_ID", + lifecycle_receipt_id, + ); if let Some(cwd) = &policy.cwd { command.current_dir(cwd); } @@ -3499,6 +3581,38 @@ mod provider_status_ownership_tests { } } +#[cfg(test)] +mod semantic_projector_authority_tests { + #[test] + fn supervisor_owns_idunn_incarnations_and_exact_evidence_selection() { + let source = include_str!("epiphany-daemon-supervisor.rs"); + let issuer_flag = ["--issuer", "-incarnation"].concat(); + let executor_flag = ["--executor", "-incarnation"].concat(); + assert!(!source.contains(&issuer_flag)); + assert!(!source.contains(&executor_flag)); + let evidence_id_flag = ["--lifecycle", "-evidence-id"].concat(); + let evidence_hash_flag = ["--lifecycle", "-evidence-hash"].concat(); + assert!(!source.contains(&evidence_id_flag)); + assert!(!source.contains(&evidence_hash_flag)); + assert!(source.contains("--receipt-id")); + assert!(source.contains("--provider-heartbeat-id")); + assert!(source.contains("idunn_acquire_memory_semantic_projection_for_local_supervisor")); + assert!(source.contains("idunn_recover_memory_semantic_projection_from_cultmesh")); + } + + #[test] + fn recovery_command_only_rotates_claim_authority() { + let source = include_str!("epiphany-daemon-supervisor.rs"); + let start = source.find("fn semantic_recover(args: Args)").unwrap(); + let tail = &source[start..]; + let end = tail.find("\nfn ").unwrap_or(tail.len()); + let body = &tail[..end]; + assert!(body.contains("idunn_recover_memory_semantic_projection_from_cultmesh")); + assert!(!body.contains("execute_memory_semantic_projection")); + assert!(!body.contains("MemorySemanticIndexConfig")); + } +} + #[derive(Clone)] struct Args { command: String, @@ -3538,6 +3652,12 @@ struct Args { artifact_ref: Option, stdout_artifact: Option, stderr_artifact: Option, + agent_store: Option, + runtime_store: Option, + executor_id: Option, + semantic_purpose: Option, + expected_claim_id: Option, + provider_heartbeat_id: Option, } impl Args { @@ -3581,6 +3701,12 @@ impl Args { let mut artifact_ref = None; let mut stdout_artifact = None; let mut stderr_artifact = None; + let mut agent_store = None; + let mut runtime_store = None; + let mut executor_id = None; + let mut semantic_purpose = None; + let mut expected_claim_id = None; + let mut provider_heartbeat_id = None; while let Some(arg) = values.next() { match arg.as_str() { @@ -3723,6 +3849,33 @@ impl Args { values.next().context("missing --stderr-artifact value")?, )); } + "--agent-store" => { + agent_store = Some(PathBuf::from( + values.next().context("missing --agent-store value")?, + )) + } + "--runtime-store" => { + runtime_store = Some(PathBuf::from( + values.next().context("missing --runtime-store value")?, + )) + } + "--executor-id" => { + executor_id = Some(values.next().context("missing --executor-id value")?) + } + "--purpose" => { + semantic_purpose = Some(values.next().context("missing --purpose value")?) + } + "--expected-claim-id" => { + expected_claim_id = + Some(values.next().context("missing --expected-claim-id value")?) + } + "--provider-heartbeat-id" => { + provider_heartbeat_id = Some( + values + .next() + .context("missing --provider-heartbeat-id value")?, + ) + } other => anyhow::bail!("unknown argument {other:?}"), } } @@ -3804,6 +3957,8 @@ impl Args { | "service-start" | "windows-service-stop" | "service-stop" + | "semantic-acquire" + | "semantic-recover" ) => { "*".to_string() @@ -3858,6 +4013,12 @@ impl Args { artifact_ref, stdout_artifact, stderr_artifact, + agent_store, + runtime_store, + executor_id, + semantic_purpose, + expected_claim_id, + provider_heartbeat_id, }) } } diff --git a/epiphany-core/src/bin/epiphany-memory-semantic.rs b/epiphany-core/src/bin/epiphany-memory-semantic.rs index 9ea980d3..5854c856 100644 --- a/epiphany-core/src/bin/epiphany-memory-semantic.rs +++ b/epiphany-core/src/bin/epiphany-memory-semantic.rs @@ -18,12 +18,12 @@ fn main() -> Result<()> { match command.as_str() { "index" => { let (input, source_store) = options.load_projection_input()?; - let executor_id = options - .executor_id + let claim_id = options + .claim_id .as_deref() - .ok_or_else(|| usage_error("index requires --executor-id "))?; + .ok_or_else(|| usage_error("index requires --claim-id "))?; let receipt = - execute_memory_semantic_projection(source_store, &input, executor_id, &config)?; + execute_memory_semantic_projection(source_store, &input, claim_id, &config)?; print_receipt(&receipt, source_store)?; } "health" => { @@ -82,7 +82,7 @@ struct Options { graph_store: Option, runtime_store: Option, agent_store: Option, - executor_id: Option, + claim_id: Option, local_verse_store: Option, runtime_id: String, provider_incarnation: Option, @@ -100,7 +100,7 @@ impl Options { graph_store: None, runtime_store: None, agent_store: None, - executor_id: None, + claim_id: None, local_verse_store: None, runtime_id: "epiphany-memory-semantic".to_string(), provider_incarnation: None, @@ -121,7 +121,7 @@ impl Options { "--graph-store" => options.graph_store = Some(PathBuf::from(value()?)), "--runtime-store" => options.runtime_store = Some(PathBuf::from(value()?)), "--agent-store" => options.agent_store = Some(PathBuf::from(value()?)), - "--executor-id" => options.executor_id = Some(value()?), + "--claim-id" => options.claim_id = Some(value()?), "--local-verse-store" => options.local_verse_store = Some(PathBuf::from(value()?)), "--runtime-id" => options.runtime_id = value()?, "--provider-incarnation" => options.provider_incarnation = Some(value()?), @@ -235,7 +235,7 @@ fn parse_profile(value: &str) -> Result { fn usage_error(message: &str) -> anyhow::Error { anyhow!( - "{message}\nusage: epiphany-memory-semantic (--runtime-store |--agent-store |--graph-store ) [--swarm-id ] --partition [--executor-id ] [--local-verse-store --runtime-id --provider-incarnation ] [--text ] [--query-id ] [--budget ] [--profile ]" + "{message}\nusage: epiphany-memory-semantic (--runtime-store |--agent-store |--graph-store ) [--swarm-id ] --partition [--claim-id ] [--local-verse-store --runtime-id --provider-incarnation ] [--text ] [--query-id ] [--budget ] [--profile ]" ) } diff --git a/epiphany-core/src/cultmesh_integration.rs b/epiphany-core/src/cultmesh_integration.rs index d57c7791..9a36cd0b 100644 --- a/epiphany-core/src/cultmesh_integration.rs +++ b/epiphany-core/src/cultmesh_integration.rs @@ -22,7 +22,10 @@ use cultmesh_rs::CultMeshNodeOptions; use cultmesh_rs::cultmesh_documents; use serde::Serialize; use serde_json::Value; +use sha2::Digest; +use sha2::Sha256; use std::path::Path; +use uuid::Uuid; pub const EPIPHANY_CULTMESH_STATUS_TYPE: &str = "epiphany.cultmesh.status"; pub const EPIPHANY_CULTMESH_STATUS_SCHEMA_VERSION: &str = "epiphany.cultmesh.status.v0"; @@ -124,6 +127,10 @@ pub const EPIPHANY_CULTMESH_EVE_SURFACE_STATE_SCHEMA_VERSION: &str = pub const EPIPHANY_CULTMESH_DAEMON_STATUS_TYPE: &str = "epiphany.cultmesh.daemon_status"; pub const EPIPHANY_CULTMESH_DAEMON_STATUS_SCHEMA_VERSION: &str = "epiphany.cultmesh.daemon_status.v0"; +pub const EPIPHANY_CULTMESH_DAEMON_HEARTBEAT_EVENT_TYPE: &str = + "epiphany.cultmesh.daemon_heartbeat_event"; +pub const EPIPHANY_CULTMESH_DAEMON_HEARTBEAT_EVENT_SCHEMA_VERSION: &str = + "epiphany.cultmesh.daemon_heartbeat_event.v1"; pub const EPIPHANY_CULTMESH_DAEMON_POKE_INTENT_TYPE: &str = "epiphany.cultmesh.daemon_poke_intent"; pub const EPIPHANY_CULTMESH_DAEMON_POKE_INTENT_SCHEMA_VERSION: &str = "epiphany.cultmesh.daemon_poke_intent.v1"; @@ -1218,6 +1225,34 @@ pub struct EpiphanyCultMeshDaemonStatusEntry { pub notes: Vec, } +#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] +#[cultcache( + type = "epiphany.cultmesh.daemon_heartbeat_event", + schema = "EpiphanyCultMeshDaemonHeartbeatEventEntry" +)] +pub struct EpiphanyCultMeshDaemonHeartbeatEventEntry { + #[cultcache(key = 0)] + pub schema_version: String, + #[cultcache(key = 1)] + pub heartbeat_id: String, + #[cultcache(key = 2)] + pub daemon_id: String, + #[cultcache(key = 3)] + pub cluster_id: String, + #[cultcache(key = 4)] + pub provider_incarnation: String, + #[cultcache(key = 5)] + pub sequence: u64, + #[cultcache(key = 6)] + pub status: String, + #[cultcache(key = 7)] + pub heartbeat_at: String, + #[cultcache(key = 8)] + pub private_state_exposed: bool, + #[cultcache(key = 9, default)] + pub startup_lifecycle_receipt_id: String, +} + #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] #[cultcache( type = "epiphany.cultmesh.daemon_poke_intent", @@ -2584,6 +2619,7 @@ cultmesh_documents!(EpiphanyCultMeshDocuments { EpiphanyCultMeshEveConnectionReceiptEntry => EPIPHANY_CULTMESH_EVE_CONNECTION_RECEIPT_SCHEMA_VERSION, EpiphanyCultMeshEveSurfaceStateEntry => EPIPHANY_CULTMESH_EVE_SURFACE_STATE_SCHEMA_VERSION, EpiphanyCultMeshDaemonStatusEntry => EPIPHANY_CULTMESH_DAEMON_STATUS_SCHEMA_VERSION, + EpiphanyCultMeshDaemonHeartbeatEventEntry => EPIPHANY_CULTMESH_DAEMON_HEARTBEAT_EVENT_SCHEMA_VERSION, EpiphanyCultMeshDaemonPokeIntentEntry => EPIPHANY_CULTMESH_DAEMON_POKE_INTENT_SCHEMA_VERSION, EpiphanyCultMeshDaemonPokeReceiptEntry => EPIPHANY_CULTMESH_DAEMON_POKE_RECEIPT_SCHEMA_VERSION, EpiphanyCultMeshDaemonRestartPolicyEntry => EPIPHANY_CULTMESH_DAEMON_RESTART_POLICY_SCHEMA_VERSION, @@ -3950,6 +3986,18 @@ pub fn load_latest_epiphany_cultmesh_daemon_poke_intent( node.get(EPIPHANY_CULTMESH_DAEMON_POKE_INTENT_LATEST_KEY) } +pub fn load_epiphany_cultmesh_daemon_poke_intent( + store_path: impl AsRef, + runtime_id: impl Into, + intent_id: &str, +) -> Result> { + if intent_id.trim().is_empty() { + return Err(anyhow!("daemon poke intent identity is required")); + } + let node = open_epiphany_cultmesh_node(store_path, runtime_id)?; + node.get(&epiphany_cultmesh_daemon_poke_intent_key(intent_id)) +} + pub fn load_latest_epiphany_cultmesh_daemon_poke_receipt( store_path: impl AsRef, runtime_id: impl Into, @@ -3958,6 +4006,18 @@ pub fn load_latest_epiphany_cultmesh_daemon_poke_receipt( node.get(EPIPHANY_CULTMESH_DAEMON_POKE_RECEIPT_LATEST_KEY) } +pub fn load_epiphany_cultmesh_daemon_poke_receipt( + store_path: impl AsRef, + runtime_id: impl Into, + receipt_id: &str, +) -> Result> { + if receipt_id.trim().is_empty() { + return Err(anyhow!("daemon poke receipt identity is required")); + } + let node = open_epiphany_cultmesh_node(store_path, runtime_id)?; + node.get(&epiphany_cultmesh_daemon_poke_receipt_key(receipt_id)) +} + pub fn write_epiphany_cultmesh_daemon_restart_policy( store_path: impl AsRef, runtime_id: impl Into, @@ -7000,6 +7060,288 @@ pub fn write_epiphany_cultmesh_daemon_status( Ok(written) } +fn epiphany_cultmesh_daemon_heartbeat_event_key(heartbeat_id: &str) -> String { + format!("epiphany-local/daemon-heartbeat/event/{heartbeat_id}") +} + +fn epiphany_cultmesh_daemon_heartbeat_latest_key(daemon_id: &str) -> String { + format!("epiphany-local/daemon-heartbeat/{daemon_id}/latest") +} + +pub fn write_epiphany_cultmesh_daemon_heartbeat_event( + store_path: impl AsRef, + runtime_id: impl Into, + event: EpiphanyCultMeshDaemonHeartbeatEventEntry, +) -> Result { + validate_daemon_heartbeat_event(&event)?; + let store_path = store_path.as_ref(); + let runtime_id = runtime_id.into(); + let event_key = epiphany_cultmesh_daemon_heartbeat_event_key(&event.heartbeat_id); + let latest_key = epiphany_cultmesh_daemon_heartbeat_latest_key(&event.daemon_id); + let backing = SingleFileMessagePackBackingStore::new(store_path); + + for _ in 0..8 { + let node = open_epiphany_cultmesh_node(store_path, runtime_id.clone())?; + if let Some(existing) = node.get::(&event_key)? { + return if existing == event { + Ok(existing) + } else { + Err(anyhow!( + "immutable daemon heartbeat identity collision for {:?}", + event.heartbeat_id + )) + }; + } + let latest = node.get::(&latest_key)?; + let advances_latest = match latest.as_ref() { + Some(current) => daemon_heartbeat_advances(current, &event)?, + None => true, + }; + let event_envelope = node.cache().prepare_entry(&event_key, &event)?.0; + let mut replacements = vec![event_envelope]; + let mut expected = Vec::new(); + if advances_latest { + if let Some(envelope) = node + .cache() + .get_envelope::(&latest_key)? + { + expected.push(envelope); + } + replacements.push(node.cache().prepare_entry(&latest_key, &event)?.0); + } + if backing.compare_and_swap_batch(&expected, replacements)? { + return Ok(event); + } + } + Err(anyhow!( + "daemon heartbeat latest advanced during publication" + )) +} + +pub fn load_epiphany_cultmesh_daemon_heartbeat_event( + store_path: impl AsRef, + runtime_id: impl Into, + heartbeat_id: &str, +) -> Result> { + validate_heartbeat_identifier("heartbeat", heartbeat_id)?; + open_epiphany_cultmesh_node(store_path, runtime_id)? + .get(&epiphany_cultmesh_daemon_heartbeat_event_key(heartbeat_id)) +} + +pub fn load_latest_epiphany_cultmesh_daemon_heartbeat( + store_path: impl AsRef, + runtime_id: impl Into, + daemon_id: &str, +) -> Result> { + validate_heartbeat_identifier("daemon", daemon_id)?; + open_epiphany_cultmesh_node(store_path, runtime_id)? + .get(&epiphany_cultmesh_daemon_heartbeat_latest_key(daemon_id)) +} + +pub fn idunn_acquire_memory_semantic_projection_for_local_supervisor( + canonical_store: impl AsRef, + input: &crate::MemorySemanticProjectionInput, + executor_id: &str, + purpose: &str, + acquired_at: &str, +) -> Result { + crate::memory_graph::semantic_projector::idunn_acquire_memory_semantic_projection( + canonical_store, + input, + executor_id, + &format!("executor-{}", Uuid::new_v4()), + purpose, + &format!("idunn-{}", Uuid::new_v4()), + acquired_at, + ) +} + +#[allow(clippy::too_many_arguments)] +pub fn idunn_recover_memory_semantic_projection_from_cultmesh( + verse_store: impl AsRef, + runtime_id: impl Into, + canonical_store: impl AsRef, + input: &crate::MemorySemanticProjectionInput, + expected_claim_id: &str, + replacement_executor_id: &str, + lifecycle_receipt_id: &str, + provider_heartbeat_id: &str, + recovered_at: &str, +) -> Result<( + crate::MemorySemanticProjectorRecoveryAuthorization, + crate::MemorySemanticProjectionClaim, +)> { + let verse_store = verse_store.as_ref(); + let runtime_id = runtime_id.into(); + let canonical_store = canonical_store.as_ref(); + crate::observe_memory_semantic_projection(canonical_store, input)?; + + let node = open_epiphany_cultmesh_node(verse_store, runtime_id)?; + let receipt_key = epiphany_cultmesh_daemon_poke_receipt_key(lifecycle_receipt_id); + let receipt = node + .get::(&receipt_key)? + .ok_or_else(|| anyhow!("Idunn recovery lifecycle receipt is absent"))?; + validate_daemon_poke_receipt(&receipt)?; + if receipt.receipt_id != lifecycle_receipt_id + || receipt.target_daemon_id != "epiphany-memory-semantic-projector" + || receipt.status != "restart-succeeded" + || receipt.resulting_status != "awaiting-provider-heartbeat" + { + return Err(anyhow!( + "Idunn recovery requires the exact successful awaiting-heartbeat receipt" + )); + } + + let intent_key = epiphany_cultmesh_daemon_poke_intent_key(&receipt.intent_id); + let intent = node + .get::(&intent_key)? + .ok_or_else(|| anyhow!("Idunn recovery lifecycle intent is absent"))?; + validate_daemon_poke_intent(&intent)?; + if intent.intent_id != receipt.intent_id + || intent.target_daemon_id != receipt.target_daemon_id + || intent.target_cluster_id != receipt.target_cluster_id + { + return Err(anyhow!("Idunn recovery lifecycle chain disagrees")); + } + + let heartbeat_key = epiphany_cultmesh_daemon_heartbeat_event_key(provider_heartbeat_id); + let heartbeat = node + .get::(&heartbeat_key)? + .ok_or_else(|| anyhow!("Idunn recovery provider heartbeat is absent"))?; + validate_daemon_heartbeat_event(&heartbeat)?; + if heartbeat.heartbeat_id != provider_heartbeat_id + || heartbeat.daemon_id != receipt.target_daemon_id + || heartbeat.cluster_id != receipt.target_cluster_id + || heartbeat.status != "ready" + || heartbeat.startup_lifecycle_receipt_id != receipt.receipt_id + { + return Err(anyhow!("Idunn recovery provider heartbeat disagrees")); + } + let receipt_completed = DateTime::parse_from_rfc3339(&receipt.completed_at_utc)?; + let heartbeat_at = DateTime::parse_from_rfc3339(&heartbeat.heartbeat_at)?; + if heartbeat_at <= receipt_completed { + return Err(anyhow!( + "Idunn recovery heartbeat must follow lifecycle completion" + )); + } + + let intent_digest = + cultmesh_envelope_digest::(&node, &intent_key)?; + let receipt_digest = + cultmesh_envelope_digest::(&node, &receipt_key)?; + let heartbeat_digest = cultmesh_envelope_digest::( + &node, + &heartbeat_key, + )?; + let evidence = + crate::memory_graph::semantic_projector::idunn_semantic_recovery_evidence_from_cultmesh( + canonical_store, + input, + expected_claim_id, + &format!("idunn-{}", Uuid::new_v4()), + &intent.intent_id, + &intent_digest, + &receipt.receipt_id, + &receipt_digest, + &receipt.completed_at_utc, + &heartbeat.heartbeat_id, + &heartbeat_digest, + &heartbeat.provider_incarnation, + &heartbeat.heartbeat_at, + &heartbeat.startup_lifecycle_receipt_id, + )?; + crate::memory_graph::semantic_projector::idunn_recover_memory_semantic_projection( + canonical_store, + input, + expected_claim_id, + replacement_executor_id, + &heartbeat.provider_incarnation, + &evidence, + recovered_at, + ) +} + +fn cultmesh_envelope_digest(node: &CultMeshNode, key: &str) -> Result { + let envelope = node + .cache() + .get_envelope::(key)? + .ok_or_else(|| anyhow!("authenticated CultMesh evidence envelope disappeared"))?; + let mut digest = Sha256::new(); + digest.update(envelope.r#type.as_bytes()); + digest.update([0]); + digest.update(envelope.key.as_bytes()); + digest.update([0]); + digest.update(&envelope.payload); + Ok(format!("sha256-{:x}", digest.finalize())) +} + +fn daemon_heartbeat_advances( + current: &EpiphanyCultMeshDaemonHeartbeatEventEntry, + candidate: &EpiphanyCultMeshDaemonHeartbeatEventEntry, +) -> Result { + validate_daemon_heartbeat_event(current)?; + let current_time = DateTime::parse_from_rfc3339(¤t.heartbeat_at)?; + let candidate_time = DateTime::parse_from_rfc3339(&candidate.heartbeat_at)?; + if current.provider_incarnation == candidate.provider_incarnation { + if candidate.sequence > current.sequence && candidate_time < current_time { + return Err(anyhow!( + "daemon heartbeat time regressed within provider incarnation" + )); + } + if candidate.sequence <= current.sequence { + return Ok(false); + } + } + Ok( + (candidate_time, candidate.sequence, &candidate.heartbeat_id) + > (current_time, current.sequence, ¤t.heartbeat_id), + ) +} + +fn validate_daemon_heartbeat_event( + event: &EpiphanyCultMeshDaemonHeartbeatEventEntry, +) -> Result<()> { + if event.schema_version != EPIPHANY_CULTMESH_DAEMON_HEARTBEAT_EVENT_SCHEMA_VERSION { + return Err(anyhow!("unsupported daemon heartbeat schema")); + } + validate_heartbeat_identifier("heartbeat", &event.heartbeat_id)?; + validate_heartbeat_identifier("daemon", &event.daemon_id)?; + validate_heartbeat_identifier("cluster", &event.cluster_id)?; + validate_heartbeat_identifier("provider incarnation", &event.provider_incarnation)?; + if !event.startup_lifecycle_receipt_id.is_empty() { + validate_heartbeat_identifier( + "startup lifecycle receipt", + &event.startup_lifecycle_receipt_id, + )?; + } + if event.sequence == 0 { + return Err(anyhow!("daemon heartbeat sequence must be positive")); + } + if !matches!(event.status.as_str(), "ready" | "degraded" | "stopping") { + return Err(anyhow!("invalid daemon heartbeat status")); + } + DateTime::parse_from_rfc3339(&event.heartbeat_at) + .context("daemon heartbeat requires RFC3339 heartbeat_at")?; + if event.private_state_exposed { + return Err(anyhow!("daemon heartbeat must not expose private state")); + } + Ok(()) +} + +fn validate_heartbeat_identifier(label: &str, value: &str) -> Result<()> { + if value.is_empty() + || value.len() > 128 + || !value + .bytes() + .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.')) + { + return Err(anyhow!( + "daemon heartbeat requires a bounded opaque {label} id" + )); + } + Ok(()) +} + fn validate_daemon_status(status: &EpiphanyCultMeshDaemonStatusEntry) -> Result<()> { if status.private_state_exposed { return Err(anyhow!("daemon statuses must not expose private state")); @@ -7752,12 +8094,14 @@ mod tests { )?; assert_eq!(pending.status, "pending"); - let claim = crate::memory_graph::semantic_projector::claim_memory_semantic_projection( + let claim = idunn_acquire_memory_semantic_projection_for_local_supervisor( &canonical, - input.obligation(), + &input, "executor-a", + "execute", "2026-07-15T12:01:00Z", - )?; + )? + .claim; let raw_receipt = crate::MemorySemanticIndexReceipt { schema_version: crate::MEMORY_SEMANTIC_INDEX_RECEIPT_SCHEMA_VERSION.to_string(), receipt_id: "receipt-four-state".to_string(), @@ -7800,18 +8144,14 @@ mod tests { assert_eq!(ready.status, "ready"); assert!(ready.query_eligible_display_only); - let terminal_claim = crate::MemorySemanticProjectionClaim { - status: "succeeded".to_string(), - completed_at: Some("2026-07-15T12:02:01Z".to_string()), - ..claim.clone() - }; - let repair = crate::memory_graph::semantic_projector::reopen_succeeded_projection_claim( + let repair = idunn_acquire_memory_semantic_projection_for_local_supervisor( &canonical, - &terminal_claim, - input.obligation(), + &input, "executor-b", + "repair", "2026-07-15T12:03:00Z", - )?; + )? + .claim; crate::memory_graph::semantic_projector::fail_memory_semantic_projection_claim( &canonical, &repair.claim_id, @@ -10659,6 +10999,201 @@ mod tests { Ok(()) } + fn heartbeat_event( + heartbeat_id: &str, + incarnation: &str, + sequence: u64, + heartbeat_at: &str, + ) -> EpiphanyCultMeshDaemonHeartbeatEventEntry { + EpiphanyCultMeshDaemonHeartbeatEventEntry { + schema_version: EPIPHANY_CULTMESH_DAEMON_HEARTBEAT_EVENT_SCHEMA_VERSION.to_string(), + heartbeat_id: heartbeat_id.to_string(), + daemon_id: "daemon-test".to_string(), + cluster_id: "cluster-test".to_string(), + provider_incarnation: incarnation.to_string(), + sequence, + status: "ready".to_string(), + heartbeat_at: heartbeat_at.to_string(), + private_state_exposed: false, + startup_lifecycle_receipt_id: String::new(), + } + } + + #[test] + fn daemon_heartbeat_events_are_immutable_and_advance_latest_monotonically() -> Result<()> { + let temp = tempfile::tempdir()?; + let store = temp.path().join("daemon-heartbeats.ccmp"); + let first = heartbeat_event("heartbeat-1", "incarnation-a", 1, "2026-07-15T12:00:00Z"); + write_epiphany_cultmesh_daemon_heartbeat_event(&store, "runtime-test", first.clone())?; + assert_eq!( + load_epiphany_cultmesh_daemon_heartbeat_event(&store, "runtime-test", "heartbeat-1")?, + Some(first.clone()) + ); + + let delayed = heartbeat_event( + "heartbeat-delayed", + "incarnation-a", + 1, + "2026-07-15T11:59:59Z", + ); + write_epiphany_cultmesh_daemon_heartbeat_event(&store, "runtime-test", delayed)?; + assert_eq!( + load_latest_epiphany_cultmesh_daemon_heartbeat(&store, "runtime-test", "daemon-test")?, + Some(first.clone()) + ); + + let restarted = heartbeat_event("heartbeat-2", "incarnation-b", 1, "2026-07-15T12:00:01Z"); + write_epiphany_cultmesh_daemon_heartbeat_event(&store, "runtime-test", restarted.clone())?; + assert_eq!( + load_latest_epiphany_cultmesh_daemon_heartbeat(&store, "runtime-test", "daemon-test")?, + Some(restarted) + ); + + let mut collision = first.clone(); + collision.status = "degraded".to_string(); + assert!( + write_epiphany_cultmesh_daemon_heartbeat_event(&store, "runtime-test", collision) + .expect_err("heartbeat identity is immutable") + .to_string() + .contains("identity collision") + ); + let mut private = heartbeat_event( + "heartbeat-private", + "incarnation-b", + 2, + "2026-07-15T12:00:02Z", + ); + private.private_state_exposed = true; + assert!( + write_epiphany_cultmesh_daemon_heartbeat_event(&store, "runtime-test", private) + .expect_err("private heartbeat must be refused") + .to_string() + .contains("must not expose private state") + ); + Ok(()) + } + + #[test] + fn semantic_recovery_requires_exact_correlated_restart_heartbeat_and_is_single_use() + -> Result<()> { + let temp = tempfile::tempdir()?; + let canonical = temp.path().join("canonical.msgpack"); + let verse = temp.path().join("verse.ccmp"); + let input = semantic_health_input(&canonical, "swarm-recovery", "mind", 1)?; + let claim = idunn_acquire_memory_semantic_projection_for_local_supervisor( + &canonical, + &input, + "executor-old", + "execute", + "2026-07-15T12:00:00Z", + )? + .claim; + let status = EpiphanyCultMeshDaemonStatusEntry { + schema_version: EPIPHANY_CULTMESH_DAEMON_STATUS_SCHEMA_VERSION.to_string(), + daemon_id: "epiphany-memory-semantic-projector".to_string(), + cluster_id: "local".to_string(), + body_domain: "semantic-projection".to_string(), + daemon_surface_id: "daemon.semantic-projector".to_string(), + eve_surface_id: "eve.semantic-projector".to_string(), + status: "stale".to_string(), + last_heartbeat_utc: "2026-07-15T12:00:01Z".to_string(), + supported_actions: vec!["pokeDaemon".to_string()], + operator_action: "restart".to_string(), + private_state_exposed: false, + notes: Vec::new(), + }; + let mut intent = epiphany_cultmesh_daemon_poke_intent_from_status( + "semantic-restart-intent-1", + "idunn", + &status, + "fence exact abandoned semantic projector claim", + ); + intent.requested_at_utc = "2026-07-15T12:01:00Z".to_string(); + write_epiphany_cultmesh_daemon_poke_intent(&verse, "runtime-test", intent.clone())?; + let mut receipt = epiphany_cultmesh_daemon_poke_receipt_for_intent( + "semantic-restart-receipt-1", + &intent, + "restart-succeeded", + "awaiting-provider-heartbeat", + "process://semantic-projector/exit/0", + ); + receipt.attempted_at_utc = "2026-07-15T12:01:00Z".to_string(); + receipt.completed_at_utc = "2026-07-15T12:02:00Z".to_string(); + write_epiphany_cultmesh_daemon_poke_receipt(&verse, "runtime-test", receipt.clone())?; + + let unrelated = EpiphanyCultMeshDaemonHeartbeatEventEntry { + schema_version: EPIPHANY_CULTMESH_DAEMON_HEARTBEAT_EVENT_SCHEMA_VERSION.to_string(), + heartbeat_id: "semantic-heartbeat-unrelated".to_string(), + daemon_id: status.daemon_id.clone(), + cluster_id: status.cluster_id.clone(), + provider_incarnation: "provider-new".to_string(), + sequence: 1, + status: "ready".to_string(), + heartbeat_at: "2026-07-15T12:03:00Z".to_string(), + private_state_exposed: false, + startup_lifecycle_receipt_id: String::new(), + }; + write_epiphany_cultmesh_daemon_heartbeat_event(&verse, "runtime-test", unrelated)?; + assert!( + idunn_recover_memory_semantic_projection_from_cultmesh( + &verse, + "runtime-test", + &canonical, + &input, + &claim.claim_id, + "executor-new", + &receipt.receipt_id, + "semantic-heartbeat-unrelated", + "2026-07-15T12:04:00Z", + ) + .is_err() + ); + + let correlated = EpiphanyCultMeshDaemonHeartbeatEventEntry { + heartbeat_id: "semantic-heartbeat-correlated".to_string(), + sequence: 2, + startup_lifecycle_receipt_id: receipt.receipt_id.clone(), + ..heartbeat_event( + "semantic-heartbeat-template", + "provider-new", + 2, + "2026-07-15T12:03:00Z", + ) + }; + let mut correlated = correlated; + correlated.daemon_id = status.daemon_id; + correlated.cluster_id = status.cluster_id; + write_epiphany_cultmesh_daemon_heartbeat_event(&verse, "runtime-test", correlated.clone())?; + let (_, recovered) = idunn_recover_memory_semantic_projection_from_cultmesh( + &verse, + "runtime-test", + &canonical, + &input, + &claim.claim_id, + "executor-new", + &receipt.receipt_id, + &correlated.heartbeat_id, + "2026-07-15T12:04:00Z", + )?; + assert_eq!(recovered.epoch, claim.epoch + 1); + assert_eq!(recovered.executor_incarnation, "provider-new"); + assert!( + idunn_recover_memory_semantic_projection_from_cultmesh( + &verse, + "runtime-test", + &canonical, + &input, + &recovered.claim_id, + "executor-third", + &receipt.receipt_id, + &correlated.heartbeat_id, + "2026-07-15T12:05:00Z", + ) + .is_err() + ); + Ok(()) + } + #[test] fn bifrost_mirrors_name_arrival_order_without_rewriting_storage_keys() { let source = include_str!("cultmesh_integration.rs"); diff --git a/epiphany-core/src/lib.rs b/epiphany-core/src/lib.rs index 954731ae..39376135 100644 --- a/epiphany-core/src/lib.rs +++ b/epiphany-core/src/lib.rs @@ -224,6 +224,8 @@ pub use cultmesh_integration::EPIPHANY_CULTMESH_CONTINUITY_CONTRACT_TYPE; pub use cultmesh_integration::EPIPHANY_CULTMESH_COORDINATOR_RUN_RECEIPT_LATEST_KEY; pub use cultmesh_integration::EPIPHANY_CULTMESH_COORDINATOR_RUN_RECEIPT_SCHEMA_VERSION; pub use cultmesh_integration::EPIPHANY_CULTMESH_COORDINATOR_RUN_RECEIPT_TYPE; +pub use cultmesh_integration::EPIPHANY_CULTMESH_DAEMON_HEARTBEAT_EVENT_SCHEMA_VERSION; +pub use cultmesh_integration::EPIPHANY_CULTMESH_DAEMON_HEARTBEAT_EVENT_TYPE; pub use cultmesh_integration::EPIPHANY_CULTMESH_DAEMON_POKE_INTENT_LATEST_KEY; pub use cultmesh_integration::EPIPHANY_CULTMESH_DAEMON_POKE_INTENT_SCHEMA_VERSION; pub use cultmesh_integration::EPIPHANY_CULTMESH_DAEMON_POKE_INTENT_TYPE; @@ -337,6 +339,7 @@ pub use cultmesh_integration::EpiphanyCultMeshBifrostPublicProofPublicationRecei pub use cultmesh_integration::EpiphanyCultMeshClusterTopologyEntry; pub use cultmesh_integration::EpiphanyCultMeshContinuityContractEntry; pub use cultmesh_integration::EpiphanyCultMeshCoordinatorRunReceiptEntry; +pub use cultmesh_integration::EpiphanyCultMeshDaemonHeartbeatEventEntry; pub use cultmesh_integration::EpiphanyCultMeshDaemonPokeIntentEntry; pub use cultmesh_integration::EpiphanyCultMeshDaemonPokeReceiptEntry; pub use cultmesh_integration::EpiphanyCultMeshDaemonRestartPolicyEntry; @@ -426,6 +429,8 @@ pub use cultmesh_integration::epiphany_cultmesh_substrate_gate_contracts; pub use cultmesh_integration::epiphany_cultmesh_verse_policies; pub use cultmesh_integration::epiphany_local_verse_work_loop_summary; pub use cultmesh_integration::epiphany_service_execution_audit_report; +pub use cultmesh_integration::idunn_acquire_memory_semantic_projection_for_local_supervisor; +pub use cultmesh_integration::idunn_recover_memory_semantic_projection_from_cultmesh; pub use cultmesh_integration::load_arrival_latest_epiphany_cultmesh_bifrost_artifact_acceptance_receipt; pub use cultmesh_integration::load_arrival_latest_epiphany_cultmesh_bifrost_body_change_publication_intent; pub use cultmesh_integration::load_arrival_latest_epiphany_cultmesh_bifrost_body_change_publication_receipt; @@ -434,7 +439,10 @@ pub use cultmesh_integration::load_arrival_latest_epiphany_cultmesh_bifrost_gith pub use cultmesh_integration::load_arrival_latest_epiphany_cultmesh_bifrost_metrics_receipt; pub use cultmesh_integration::load_arrival_latest_epiphany_cultmesh_bifrost_public_proof_publication_receipt; pub use cultmesh_integration::load_epiphany_cultmesh_cluster_topology; +pub use cultmesh_integration::load_epiphany_cultmesh_daemon_heartbeat_event; pub use cultmesh_integration::load_epiphany_cultmesh_daemon_liveness; +pub use cultmesh_integration::load_epiphany_cultmesh_daemon_poke_intent; +pub use cultmesh_integration::load_epiphany_cultmesh_daemon_poke_receipt; pub use cultmesh_integration::load_epiphany_cultmesh_daemon_restart_policy; pub use cultmesh_integration::load_epiphany_cultmesh_daemon_restart_policy_directory; pub use cultmesh_integration::load_epiphany_cultmesh_daemon_service_lifecycle_receipts; @@ -456,6 +464,7 @@ pub use cultmesh_integration::load_epiphany_cultmesh_status; pub use cultmesh_integration::load_epiphany_cultmesh_swarm_brake; pub use cultmesh_integration::load_latest_epiphany_cultmesh_agent_state_soa_summary; pub use cultmesh_integration::load_latest_epiphany_cultmesh_coordinator_run_receipt; +pub use cultmesh_integration::load_latest_epiphany_cultmesh_daemon_heartbeat; pub use cultmesh_integration::load_latest_epiphany_cultmesh_daemon_poke_intent; pub use cultmesh_integration::load_latest_epiphany_cultmesh_daemon_poke_receipt; pub use cultmesh_integration::load_latest_epiphany_cultmesh_daemon_scheduler_receipt; @@ -500,6 +509,7 @@ pub use cultmesh_integration::write_epiphany_cultmesh_bifrost_metrics_receipt; pub use cultmesh_integration::write_epiphany_cultmesh_bifrost_public_proof_publication_receipt; pub use cultmesh_integration::write_epiphany_cultmesh_continuity_contracts; pub use cultmesh_integration::write_epiphany_cultmesh_coordinator_run_receipt; +pub use cultmesh_integration::write_epiphany_cultmesh_daemon_heartbeat_event; pub use cultmesh_integration::write_epiphany_cultmesh_daemon_poke_intent; pub use cultmesh_integration::write_epiphany_cultmesh_daemon_poke_receipt; pub use cultmesh_integration::write_epiphany_cultmesh_daemon_restart_policy; @@ -639,6 +649,8 @@ pub use memory_graph::MEMORY_SEMANTIC_INDEX_RECEIPT_SCHEMA_VERSION; pub use memory_graph::MEMORY_SEMANTIC_PROJECTION_ATTEMPT_SCHEMA_VERSION; pub use memory_graph::MEMORY_SEMANTIC_PROJECTION_CLAIM_SCHEMA_VERSION; pub use memory_graph::MEMORY_SEMANTIC_PROJECTION_OBLIGATION_SCHEMA_VERSION; +pub use memory_graph::MEMORY_SEMANTIC_PROJECTOR_EXECUTOR_GRANT_SCHEMA_VERSION; +pub use memory_graph::MEMORY_SEMANTIC_PROJECTOR_RECOVERY_AUTHORIZATION_SCHEMA_VERSION; pub use memory_graph::MemorySemanticIndexConfig; pub use memory_graph::MemorySemanticIndexReceipt; pub use memory_graph::MemorySemanticProjectionAttempt; @@ -650,6 +662,9 @@ pub use memory_graph::MemorySemanticProjectionObligation; pub use memory_graph::MemorySemanticProjectionObservation; pub use memory_graph::MemorySemanticProjectionReadiness; pub use memory_graph::MemorySemanticProjectionSourceHead; +pub use memory_graph::MemorySemanticProjectorAcquisition; +pub use memory_graph::MemorySemanticProjectorExecutorGrant; +pub use memory_graph::MemorySemanticProjectorRecoveryAuthorization; pub use memory_graph::RepoFrontierAdoptedPlan; pub use memory_graph::RepoFrontierItem; pub use memory_graph::RepoFrontierStatus; diff --git a/epiphany-core/src/memory_graph/semantic_index.rs b/epiphany-core/src/memory_graph/semantic_index.rs index 5afc581b..dcafb1d2 100644 --- a/epiphany-core/src/memory_graph/semantic_index.rs +++ b/epiphany-core/src/memory_graph/semantic_index.rs @@ -21,7 +21,7 @@ pub const MEMORY_SEMANTIC_INDEX_RECEIPT_SCHEMA_VERSION: &str = pub const MEMORY_SEMANTIC_PROJECTION_OBLIGATION_SCHEMA_VERSION: &str = "gamecult.epiphany.memory_semantic_projection_obligation.v0"; pub const MEMORY_SEMANTIC_PROJECTION_ATTEMPT_SCHEMA_VERSION: &str = - "gamecult.epiphany.memory_semantic_projection_attempt.v0"; + "gamecult.epiphany.memory_semantic_projection_attempt.v1"; const MODELING_COLLECTION_DEFAULT: &str = "epiphany_modeling_v1"; const MIND_COLLECTION_DEFAULT: &str = "epiphany_mind_v1"; const QUERY_LIMIT_MAX: usize = 64; @@ -159,6 +159,16 @@ pub struct MemorySemanticProjectionAttempt { pub status: String, #[cultcache(key = 6)] pub error: Option, + #[cultcache(key = 7)] + pub claim_id: String, + #[cultcache(key = 8)] + pub claim_epoch: u64, + #[cultcache(key = 9)] + pub executor_id: String, + #[cultcache(key = 10)] + pub executor_incarnation: String, + #[cultcache(key = 11)] + pub authority_id: String, } #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -356,6 +366,11 @@ pub fn validate_memory_semantic_projection_attempt( } if attempt.attempt_id.trim().is_empty() || attempt.obligation_id.trim().is_empty() + || attempt.claim_id.trim().is_empty() + || attempt.claim_epoch == 0 + || attempt.executor_id.trim().is_empty() + || attempt.executor_incarnation.trim().is_empty() + || attempt.authority_id.trim().is_empty() || attempt.started_at.trim().is_empty() || chrono::DateTime::parse_from_rfc3339(&attempt.started_at).is_err() || attempt @@ -367,6 +382,14 @@ pub fn validate_memory_semantic_projection_attempt( "semantic projection attempt is missing identity or time" )); } + if attempt.completed_at.as_deref().is_some_and(|completed_at| { + chrono::DateTime::parse_from_rfc3339(completed_at).ok() + < chrono::DateTime::parse_from_rfc3339(&attempt.started_at).ok() + }) { + return Err(anyhow!( + "semantic projection attempt completes before it starts" + )); + } match attempt.status.as_str() { "running" if attempt.completed_at.is_none() && attempt.error.is_none() => Ok(()), "failed" if attempt.completed_at.is_some() && attempt.error.is_some() => Ok(()), @@ -1347,6 +1370,11 @@ mod tests { completed_at: Some("2026-07-15T10:00:31Z".to_string()), status: "failed".to_string(), error: Some("qdrant unavailable".to_string()), + claim_id: "claim-7".to_string(), + claim_epoch: 1, + executor_id: "executor-a".to_string(), + executor_incarnation: "executor-a-incarnation".to_string(), + authority_id: "authority-a".to_string(), } } diff --git a/epiphany-core/src/memory_graph/semantic_projector.rs b/epiphany-core/src/memory_graph/semantic_projector.rs index bc11be5e..8bf6dbe3 100644 --- a/epiphany-core/src/memory_graph/semantic_projector.rs +++ b/epiphany-core/src/memory_graph/semantic_projector.rs @@ -15,7 +15,406 @@ use sha2::{Digest, Sha256}; use std::path::Path; pub const MEMORY_SEMANTIC_PROJECTION_CLAIM_SCHEMA_VERSION: &str = - "gamecult.epiphany.memory_semantic_projection_claim.v0"; + "gamecult.epiphany.memory_semantic_projection_claim.v1"; +pub const MEMORY_SEMANTIC_PROJECTOR_EXECUTOR_GRANT_SCHEMA_VERSION: &str = + "gamecult.epiphany.memory_semantic_projector_executor_grant.v1"; +pub const MEMORY_SEMANTIC_PROJECTOR_RECOVERY_AUTHORIZATION_SCHEMA_VERSION: &str = + "gamecult.epiphany.memory_semantic_projector_recovery_authorization.v1"; + +#[derive(Clone, Debug, PartialEq, DatabaseEntry)] +#[cultcache( + type = "gamecult.epiphany.memory_semantic_projector_executor_grant", + schema = "MemorySemanticProjectorExecutorGrant" +)] +pub struct MemorySemanticProjectorExecutorGrant { + #[cultcache(key = 0)] + pub schema_version: String, + #[cultcache(key = 1)] + pub grant_id: String, + #[cultcache(key = 2)] + pub scope_id: String, + #[cultcache(key = 3)] + pub swarm_id: String, + #[cultcache(key = 4)] + pub partition: String, + #[cultcache(key = 5)] + pub obligation_id: String, + #[cultcache(key = 6)] + pub executor_id: String, + #[cultcache(key = 7)] + pub executor_incarnation: String, + #[cultcache(key = 8)] + pub purpose: String, + #[cultcache(key = 9)] + pub issuer_id: String, + #[cultcache(key = 10)] + pub issuer_incarnation: String, + #[cultcache(key = 11)] + pub status: String, + #[cultcache(key = 12)] + pub issued_at: String, + #[cultcache(key = 13)] + pub consumed_at: Option, + #[cultcache(key = 14)] + pub resulting_claim_id: Option, + #[cultcache(key = 15)] + pub resulting_claim_epoch: Option, + #[cultcache(key = 16)] + pub predecessor_claim_id: Option, + #[cultcache(key = 17)] + pub predecessor_claim_epoch: Option, + #[cultcache(key = 18)] + pub predecessor_claim_status: String, +} + +#[cfg(test)] +mod authority_tests { + use super::*; + use crate::{ + MEMORY_SEMANTIC_PROJECTION_OBLIGATION_SCHEMA_VERSION, SEMANTIC_PROJECTION_SCHEMA_VERSION, + }; + use tempfile::tempdir; + + fn obligation() -> MemorySemanticProjectionObligation { + MemorySemanticProjectionObligation { + schema_version: MEMORY_SEMANTIC_PROJECTION_OBLIGATION_SCHEMA_VERSION.to_string(), + obligation_id: "obligation-modeling-7".into(), + swarm_id: "swarm-a".into(), + partition: "modeling".into(), + canonical_source_id: "runtime/repo-model".into(), + source_commit_id: "admission-7".into(), + graph_id: "repo-model".into(), + source_generation: 7, + source_model_hash: "model-hash-7".into(), + canonical_content_set_hash: "content-set-7".into(), + projection_schema_version: SEMANTIC_PROJECTION_SCHEMA_VERSION.into(), + created_at: "2026-07-15T04:00:00Z".into(), + } + } + + fn input( + store: &Path, + obligation: &MemorySemanticProjectionObligation, + ) -> Result { + let envelopes = SingleFileMessagePackBackingStore::new(store).pull_all()?; + let authority = exact_envelope( + &envelopes, + MemorySemanticProjectionObligation::TYPE, + &obligation.obligation_id, + )?; + Ok(MemorySemanticProjectionInput { + snapshot: super::super::EpiphanyMemoryGraphSnapshot { + schema_version: Some("v0".to_string()), + graph_id: obligation.graph_id.clone(), + model_revision: obligation.source_generation, + ..Default::default() + }, + obligation: obligation.clone(), + authority: MemorySemanticProjectionAuthoritySnapshot { + head: MemorySemanticProjectionSourceHead { + swarm_id: obligation.swarm_id.clone(), + partition: obligation.partition.clone(), + canonical_source_id: obligation.canonical_source_id.clone(), + source_commit_id: obligation.source_commit_id.clone(), + graph_id: obligation.graph_id.clone(), + source_generation: obligation.source_generation, + source_model_hash: obligation.source_model_hash.clone(), + canonical_content_set_hash: obligation.canonical_content_set_hash.clone(), + }, + envelopes: vec![authority], + }, + }) + } + + #[test] + fn concurrent_idunn_acquisition_has_one_winner_and_no_issued_litter() -> Result<()> { + let temp = tempdir()?; + let store = temp.path().join("acquisition.msgpack"); + let obligation = obligation(); + let mut cache = semantic_projector_cache(&store)?; + cache.put(&obligation.obligation_id, &obligation)?; + let projection_input = input(&store, &obligation)?; + let barrier = std::sync::Arc::new(std::sync::Barrier::new(3)); + let mut workers = Vec::new(); + for executor in ["executor-a", "executor-b"] { + let store = store.clone(); + let projection_input = projection_input.clone(); + let barrier = barrier.clone(); + workers.push(std::thread::spawn(move || { + barrier.wait(); + idunn_acquire_memory_semantic_projection( + store, + &projection_input, + executor, + &format!("{executor}-incarnation"), + "execute", + "idunn-incarnation-a", + "2026-07-15T04:01:00Z", + ) + })); + } + barrier.wait(); + let results = workers + .into_iter() + .map(|worker| worker.join().expect("worker")) + .collect::>(); + assert_eq!(results.iter().filter(|result| result.is_ok()).count(), 1); + let envelopes = SingleFileMessagePackBackingStore::new(&store).pull_all()?; + let grants = decode_all::(&envelopes)?; + assert_eq!(grants.len(), 1); + assert!(grants.iter().all(|grant| grant.status == "consumed")); + let claim = decode_all::(&envelopes)? + .pop() + .expect("claim"); + authenticate_claim_authority_from_envelopes(&envelopes, &claim)?; + Ok(()) + } + + #[test] + fn acquisition_is_epoch_bound_and_timestamp_ordered() -> Result<()> { + let temp = tempdir()?; + let store = temp.path().join("ordering.msgpack"); + let obligation = obligation(); + let mut cache = semantic_projector_cache(&store)?; + cache.put(&obligation.obligation_id, &obligation)?; + let projection_input = input(&store, &obligation)?; + assert!( + idunn_acquire_memory_semantic_projection( + &store, + &projection_input, + "executor-a", + "executor-a-incarnation", + "execute", + "idunn-incarnation-a", + "2026-07-15T03:59:59Z" + ) + .is_err() + ); + let acquisition = idunn_acquire_memory_semantic_projection( + &store, + &projection_input, + "executor-a", + "executor-a-incarnation", + "execute", + "idunn-incarnation-a", + "2026-07-15T04:01:00Z", + )?; + assert_eq!(acquisition.grant.predecessor_claim_status, "absent"); + assert_eq!(acquisition.grant.resulting_claim_epoch, Some(1)); + assert_eq!(acquisition.claim.authority_id, acquisition.grant.grant_id); + fail_memory_semantic_projection_claim( + &store, + &acquisition.claim.claim_id, + "2026-07-15T04:02:00Z", + "test failure", + )?; + let successor = idunn_acquire_memory_semantic_projection( + &store, + &projection_input, + "executor-b", + "executor-b-incarnation", + "execute", + "idunn-incarnation-a", + "2026-07-15T04:03:00Z", + )?; + assert_eq!( + successor.grant.predecessor_claim_id.as_deref(), + Some(acquisition.claim.claim_id.as_str()) + ); + assert_eq!(successor.grant.predecessor_claim_epoch, Some(1)); + assert_eq!(successor.grant.predecessor_claim_status, "failed"); + assert_eq!(successor.claim.epoch, 2); + Ok(()) + } + + #[test] + fn acquisition_refuses_advanced_non_obligation_authority() -> Result<()> { + let temp = tempdir()?; + let store = temp.path().join("stale-acquisition.msgpack"); + let obligation = obligation(); + let mut cache = semantic_projector_cache(&store)?; + cache.put(&obligation.obligation_id, &obligation)?; + let marker_key = "canonical-head-marker"; + cache.put(marker_key, &obligation)?; + let mut projection_input = input(&store, &obligation)?; + let opening = SingleFileMessagePackBackingStore::new(&store).pull_all()?; + projection_input.authority.envelopes.push(exact_envelope( + &opening, + MemorySemanticProjectionObligation::TYPE, + marker_key, + )?); + let mut advanced = obligation.clone(); + advanced.source_generation += 1; + advanced.source_model_hash = "advanced-model-hash".to_string(); + advanced.canonical_content_set_hash = "advanced-content-hash".to_string(); + cache.put(marker_key, &advanced)?; + assert!( + idunn_acquire_memory_semantic_projection( + &store, + &projection_input, + "executor-stale", + "executor-stale-incarnation", + "execute", + "idunn-incarnation-a", + "2026-07-15T04:01:00Z", + ) + .is_err() + ); + Ok(()) + } + + #[test] + fn sealed_recovery_binds_lifecycle_receipt_and_provider_heartbeat() -> Result<()> { + let temp = tempdir()?; + let store = temp.path().join("recovery.msgpack"); + let obligation = obligation(); + let mut cache = semantic_projector_cache(&store)?; + cache.put(&obligation.obligation_id, &obligation)?; + let projection_input = input(&store, &obligation)?; + let acquisition = idunn_acquire_memory_semantic_projection( + &store, + &projection_input, + "executor-a", + "provider-old", + "execute", + "idunn-incarnation-a", + "2026-07-15T04:01:00Z", + )?; + let evidence = idunn_semantic_recovery_evidence_from_cultmesh( + &store, + &projection_input, + &acquisition.claim.claim_id, + "idunn-incarnation-a", + "poke-intent-1", + "sha256-intent", + "poke-receipt-1", + "sha256-receipt", + "2026-07-15T04:02:00Z", + "heartbeat-1", + "sha256-heartbeat", + "provider-new", + "2026-07-15T04:03:00Z", + "poke-receipt-1", + )?; + let (authorization, recovered) = idunn_recover_memory_semantic_projection( + &store, + &projection_input, + &acquisition.claim.claim_id, + "executor-b", + "provider-new", + &evidence, + "2026-07-15T04:04:00Z", + )?; + assert_eq!(authorization.status, "consumed"); + assert_eq!(authorization.lifecycle_intent_id, "poke-intent-1"); + assert_eq!(authorization.lifecycle_receipt_id, "poke-receipt-1"); + assert_eq!(authorization.provider_heartbeat_id, "heartbeat-1"); + assert_eq!( + authorization.provider_incarnation, + recovered.executor_incarnation + ); + assert_eq!(recovered.authority_id, authorization.authorization_id); + assert!( + fail_memory_semantic_projection_claim( + &store, + &acquisition.claim.claim_id, + "2026-07-15T04:05:00Z", + "late old executor" + ) + .is_err() + ); + assert!( + idunn_semantic_recovery_evidence_from_cultmesh( + &store, + &projection_input, + &recovered.claim_id, + "idunn-incarnation-a", + "poke-intent-2", + "sha256-intent-2", + "poke-receipt-2", + "sha256-receipt-2", + "2026-07-15T04:03:00Z", + "heartbeat-2", + "sha256-heartbeat-2", + "provider-new", + "2026-07-15T04:03:00Z", + "poke-receipt-2", + ) + .is_err() + ); + Ok(()) + } +} + +#[derive(Clone, Debug, PartialEq, DatabaseEntry)] +#[cultcache( + type = "gamecult.epiphany.memory_semantic_projector_recovery_authorization", + schema = "MemorySemanticProjectorRecoveryAuthorization" +)] +pub struct MemorySemanticProjectorRecoveryAuthorization { + #[cultcache(key = 0)] + pub schema_version: String, + #[cultcache(key = 1)] + pub authorization_id: String, + #[cultcache(key = 2)] + pub scope_id: String, + #[cultcache(key = 3)] + pub swarm_id: String, + #[cultcache(key = 4)] + pub partition: String, + #[cultcache(key = 5)] + pub obligation_id: String, + #[cultcache(key = 6)] + pub claim_id: String, + #[cultcache(key = 7)] + pub claim_epoch: u64, + #[cultcache(key = 8)] + pub attempt_id: String, + #[cultcache(key = 9)] + pub abandoned_executor_id: String, + #[cultcache(key = 10)] + pub replacement_executor_id: String, + #[cultcache(key = 11)] + pub replacement_executor_incarnation: String, + #[cultcache(key = 12)] + pub issuer_id: String, + #[cultcache(key = 13)] + pub issuer_incarnation: String, + #[cultcache(key = 14)] + pub lifecycle_intent_id: String, + #[cultcache(key = 15)] + pub lifecycle_intent_digest: String, + #[cultcache(key = 16)] + pub status: String, + #[cultcache(key = 17)] + pub issued_at: String, + #[cultcache(key = 18)] + pub consumed_at: Option, + #[cultcache(key = 19)] + pub resulting_claim_id: Option, + #[cultcache(key = 20)] + pub resulting_claim_epoch: Option, + #[cultcache(key = 21)] + pub abandoned_executor_incarnation: String, + #[cultcache(key = 22)] + pub lifecycle_receipt_id: String, + #[cultcache(key = 23)] + pub lifecycle_receipt_digest: String, + #[cultcache(key = 24)] + pub lifecycle_receipt_completed_at: String, + #[cultcache(key = 25)] + pub provider_heartbeat_id: String, + #[cultcache(key = 26)] + pub provider_heartbeat_digest: String, + #[cultcache(key = 27)] + pub provider_incarnation: String, + #[cultcache(key = 28)] + pub provider_heartbeat_at: String, + #[cultcache(key = 29)] + pub canonical_store_id: String, + #[cultcache(key = 30)] + pub lifecycle_correlation_id: String, +} #[derive(Clone, Debug, PartialEq, DatabaseEntry)] #[cultcache( @@ -43,6 +442,117 @@ pub struct MemorySemanticProjectionClaim { pub claimed_at: String, #[cultcache(key = 9)] pub completed_at: Option, + #[cultcache(key = 10)] + pub executor_incarnation: String, + #[cultcache(key = 11)] + pub authority_kind: String, + #[cultcache(key = 12)] + pub authority_id: String, +} + +#[allow(dead_code)] // Sealed until Idunn can supply authenticated lifecycle evidence. +#[derive(Clone, Debug)] +pub struct IdunnSemanticRecoveryEvidence { + issuer_incarnation: String, + lifecycle_intent_id: String, + lifecycle_intent_digest: String, + lifecycle_receipt_id: String, + lifecycle_receipt_digest: String, + lifecycle_receipt_completed_at: String, + provider_heartbeat_id: String, + provider_heartbeat_digest: String, + provider_incarnation: String, + provider_heartbeat_at: String, + canonical_store_id: String, + scope_id: String, + swarm_id: String, + partition: String, + obligation_id: String, + claim_id: String, + claim_epoch: u64, + abandoned_executor_incarnation: String, + lifecycle_correlation_id: String, +} + +#[allow(clippy::too_many_arguments)] +pub(crate) fn idunn_semantic_recovery_evidence_from_cultmesh( + store_path: impl AsRef, + input: &MemorySemanticProjectionInput, + expected_claim_id: &str, + issuer_incarnation: &str, + lifecycle_intent_id: &str, + lifecycle_intent_digest: &str, + lifecycle_receipt_id: &str, + lifecycle_receipt_digest: &str, + lifecycle_receipt_completed_at: &str, + provider_heartbeat_id: &str, + provider_heartbeat_digest: &str, + provider_incarnation: &str, + provider_heartbeat_at: &str, + lifecycle_correlation_id: &str, +) -> Result { + validate_memory_semantic_projection_obligation(&input.obligation)?; + let store_path = store_path.as_ref(); + let envelopes = SingleFileMessagePackBackingStore::new(store_path).pull_all()?; + let persisted_obligation = decode_one::( + &envelopes, + &input.obligation.obligation_id, + )? + .ok_or_else(|| anyhow!("recovery evidence canonical obligation disappeared"))?; + if persisted_obligation != input.obligation { + return Err(anyhow!("recovery evidence canonical obligation advanced")); + } + for expected in &input.authority.envelopes { + let persisted = envelopes + .iter() + .find(|row| row.r#type == expected.r#type && row.key == expected.key) + .ok_or_else(|| anyhow!("recovery evidence input authority disappeared"))?; + if persisted != expected { + return Err(anyhow!("recovery evidence input authority advanced")); + } + } + let scope_id = projection_scope_id(&input.obligation.swarm_id, &input.obligation.partition)?; + let claim = decode_one::(&envelopes, &scope_id)? + .ok_or_else(|| anyhow!("recovery evidence requires current scope claim"))?; + validate_memory_semantic_projection_claim(&claim)?; + if claim.claim_id != expected_claim_id + || claim.obligation_id != input.obligation.obligation_id + || claim.status != "running" + { + return Err(anyhow!( + "recovery evidence claim does not match canonical input" + )); + } + authenticate_claim_authority_from_envelopes(&envelopes, &claim)?; + let evidence = IdunnSemanticRecoveryEvidence { + issuer_incarnation: issuer_incarnation.to_string(), + lifecycle_intent_id: lifecycle_intent_id.to_string(), + lifecycle_intent_digest: lifecycle_intent_digest.to_string(), + lifecycle_receipt_id: lifecycle_receipt_id.to_string(), + lifecycle_receipt_digest: lifecycle_receipt_digest.to_string(), + lifecycle_receipt_completed_at: lifecycle_receipt_completed_at.to_string(), + provider_heartbeat_id: provider_heartbeat_id.to_string(), + provider_heartbeat_digest: provider_heartbeat_digest.to_string(), + provider_incarnation: provider_incarnation.to_string(), + provider_heartbeat_at: provider_heartbeat_at.to_string(), + canonical_store_id: memory_semantic_projector_store_id(store_path)?, + scope_id, + swarm_id: input.obligation.swarm_id.clone(), + partition: input.obligation.partition.clone(), + obligation_id: input.obligation.obligation_id.clone(), + claim_id: claim.claim_id, + claim_epoch: claim.epoch, + abandoned_executor_incarnation: claim.executor_incarnation, + lifecycle_correlation_id: lifecycle_correlation_id.to_string(), + }; + validate_idunn_semantic_recovery_evidence(&evidence)?; + Ok(evidence) +} + +#[derive(Clone, Debug, PartialEq)] +pub struct MemorySemanticProjectorAcquisition { + pub grant: MemorySemanticProjectorExecutorGrant, + pub claim: MemorySemanticProjectionClaim, } #[derive(Clone, Debug)] @@ -206,6 +716,22 @@ pub(crate) fn projection_scope_id(swarm_id: &str, partition: &str) -> Result Result { + let canonical = store_path.canonicalize().map_err(|error| { + anyhow!("semantic projector canonical store identity is unavailable: {error}") + })?; + let rendered = canonical.to_string_lossy(); + let identity = if cfg!(windows) { + rendered.to_lowercase() + } else { + rendered.into_owned() + }; + Ok(format!( + "memory-semantic-store-{:x}", + Sha256::digest(identity.as_bytes()) + )) +} + pub fn load_memory_semantic_projection_success( store_path: impl AsRef, obligation: &MemorySemanticProjectionObligation, @@ -231,6 +757,12 @@ pub fn load_memory_semantic_projection_success( let claim = decode_one::(&envelopes, &scope_id)? .ok_or_else(|| anyhow!("ready semantic projection lost its scope claim"))?; validate_memory_semantic_projection_claim(&claim)?; + if claim.scope_id != scope_id { + return Err(anyhow!( + "semantic projection claim key disagrees with payload" + )); + } + authenticate_claim_authority(store_path.as_ref(), &claim)?; if claim.status != "succeeded" || claim.obligation_id != obligation.obligation_id || receipt.claim_id != claim.claim_id @@ -243,7 +775,7 @@ pub fn load_memory_semantic_projection_success( let attempt = decode_one::(&envelopes, &claim.attempt_id)? .ok_or_else(|| anyhow!("ready semantic projection lost its claim attempt"))?; validate_memory_semantic_projection_attempt(&attempt)?; - if attempt.status != "succeeded" || attempt.obligation_id != claim.obligation_id { + if attempt.status != "succeeded" || !attempt_authenticates_claim(&attempt, &claim) { return Err(anyhow!( "ready semantic projection claim is not authenticated by its succeeded attempt" )); @@ -269,10 +801,182 @@ pub fn load_memory_semantic_projection_readiness( ) } -pub fn execute_memory_semantic_projection( +#[allow(clippy::too_many_arguments)] +pub(crate) fn idunn_acquire_memory_semantic_projection( store_path: impl AsRef, input: &MemorySemanticProjectionInput, executor_id: &str, + executor_incarnation: &str, + purpose: &str, + idunn_incarnation: &str, + acquired_at: &str, +) -> Result { + let obligation = &input.obligation; + validate_memory_semantic_projection_obligation(obligation)?; + validate_identity_and_time(executor_id, acquired_at)?; + validate_opaque_identity(executor_incarnation, "executor incarnation")?; + validate_opaque_identity(idunn_incarnation, "issuer incarnation")?; + if !matches!(purpose, "execute" | "repair") { + return Err(anyhow!("semantic projector grant purpose is invalid")); + } + let store_path = store_path.as_ref(); + observe_memory_semantic_projection(store_path, input)?; + let cache = semantic_projector_cache(store_path)?; + let opening = SingleFileMessagePackBackingStore::new(store_path).pull_all()?; + let persisted = + decode_one::(&opening, &obligation.obligation_id)? + .ok_or_else(|| anyhow!("semantic projector grant requires persisted obligation"))?; + if persisted != *obligation { + return Err(anyhow!("semantic projector grant obligation advanced")); + } + ensure_not_before( + acquired_at, + &obligation.created_at, + "acquisition before obligation", + )?; + let scope_id = projection_scope_id(&obligation.swarm_id, &obligation.partition)?; + let current = decode_one::(&opening, &scope_id)?; + if let Some(claim) = ¤t { + validate_memory_semantic_projection_claim(claim)?; + if claim.scope_id != scope_id { + return Err(anyhow!( + "semantic projector scope key disagrees with claim payload" + )); + } + let attempt = decode_one::(&opening, &claim.attempt_id)? + .ok_or_else(|| anyhow!("semantic projector scope attempt disappeared"))?; + validate_memory_semantic_projection_attempt(&attempt)?; + if attempt.obligation_id != claim.obligation_id || attempt.status != claim.status { + return Err(anyhow!( + "semantic projector scope claim and attempt disagree" + )); + } + if claim.status == "running" { + return Err(anyhow!("semantic projector scope is already running")); + } + ensure_not_before( + acquired_at, + claim.completed_at.as_deref().unwrap_or(&claim.claimed_at), + "acquisition before predecessor", + )?; + } + match purpose { + "execute" + if current.as_ref().is_some_and(|claim| { + claim.status == "succeeded" && claim.obligation_id == obligation.obligation_id + }) => + { + return Err(anyhow!("execute grant cannot authorize succeeded repair")); + } + "repair" + if !current.as_ref().is_some_and(|claim| { + claim.status == "succeeded" && claim.obligation_id == obligation.obligation_id + }) => + { + return Err(anyhow!("repair grant requires exact succeeded claim")); + } + _ => {} + } + let epoch = current.as_ref().map_or(1, |claim| claim.epoch + 1); + let grant_id = format!("memory-semantic-executor-grant-{}", uuid::Uuid::new_v4()); + let claim = running_claim( + &scope_id, + obligation, + executor_id, + executor_incarnation, + "executor_grant", + &grant_id, + epoch, + acquired_at, + ); + let attempt = running_attempt(&claim); + let grant = MemorySemanticProjectorExecutorGrant { + schema_version: MEMORY_SEMANTIC_PROJECTOR_EXECUTOR_GRANT_SCHEMA_VERSION.to_string(), + grant_id: grant_id.clone(), + scope_id: scope_id.clone(), + swarm_id: obligation.swarm_id.clone(), + partition: obligation.partition.clone(), + obligation_id: obligation.obligation_id.clone(), + executor_id: executor_id.to_string(), + executor_incarnation: executor_incarnation.to_string(), + purpose: purpose.to_string(), + issuer_id: "idunn".to_string(), + issuer_incarnation: idunn_incarnation.to_string(), + status: "consumed".to_string(), + issued_at: acquired_at.to_string(), + consumed_at: Some(acquired_at.to_string()), + resulting_claim_id: Some(claim.claim_id.clone()), + resulting_claim_epoch: Some(claim.epoch), + predecessor_claim_id: current.as_ref().map(|row| row.claim_id.clone()), + predecessor_claim_epoch: current.as_ref().map(|row| row.epoch), + predecessor_claim_status: current + .as_ref() + .map_or("absent", |row| row.status.as_str()) + .to_string(), + }; + validate_memory_semantic_projector_executor_grant(&grant)?; + if opening.iter().any(|envelope| { + envelope.r#type == MemorySemanticProjectorExecutorGrant::TYPE + && envelope.key == grant.grant_id + }) { + return Err(anyhow!( + "semantic projector executor grant identity collision" + )); + } + let obligation_envelope = exact_envelope( + &opening, + MemorySemanticProjectionObligation::TYPE, + &obligation.obligation_id, + )?; + let mut expected = vec![obligation_envelope.clone()]; + let mut replacements = vec![obligation_envelope]; + for authority in &input.authority.envelopes { + if authority.r#type == MemorySemanticProjectionObligation::TYPE + && authority.key == obligation.obligation_id + { + continue; + } + let persisted = opening + .iter() + .find(|row| row.r#type == authority.r#type && row.key == authority.key) + .ok_or_else(|| anyhow!("semantic projector acquisition authority disappeared"))?; + if persisted != authority { + return Err(anyhow!("semantic projector acquisition authority advanced")); + } + expected.push(persisted.clone()); + replacements.push(persisted.clone()); + } + if current.is_some() { + let claim = exact_envelope( + &opening, + MemorySemanticProjectionClaim::TYPE, + &grant.scope_id, + )?; + expected.push(claim.clone()); + let current = current.as_ref().expect("checked current claim"); + let attempt = exact_envelope( + &opening, + MemorySemanticProjectionAttempt::TYPE, + ¤t.attempt_id, + )?; + expected.push(attempt.clone()); + replacements.push(attempt); + } + replacements.push(cache.prepare_entry(&scope_id, &claim)?.0); + replacements.push(cache.prepare_entry(&attempt.attempt_id, &attempt)?.0); + replacements.push(cache.prepare_entry(&grant.grant_id, &grant)?.0); + if !SingleFileMessagePackBackingStore::new(store_path) + .compare_and_swap_batch(&expected, replacements)? + { + return Err(anyhow!("semantic projector acquisition lost exact CAS")); + } + Ok(MemorySemanticProjectorAcquisition { grant, claim }) +} + +pub fn execute_memory_semantic_projection( + store_path: impl AsRef, + input: &MemorySemanticProjectionInput, + claim_id: &str, config: &super::MemorySemanticIndexConfig, ) -> Result { let partition = match input.obligation.partition.as_str() { @@ -286,26 +990,13 @@ pub fn execute_memory_semantic_projection( }; let store_path = store_path.as_ref(); let started_at = now_rfc3339(); - let claim = - claim_memory_semantic_projection(store_path, &input.obligation, executor_id, &started_at)?; - let claim = if claim.status == "succeeded" { - if let Some(receipt) = load_memory_semantic_projection_success( - store_path, - &input.obligation, - &input.authority.head, - )? { - return Ok(receipt); - } - reopen_succeeded_projection_claim( - store_path, - &claim, - &input.obligation, - executor_id, - &now_rfc3339(), - )? - } else { - claim - }; + let (_, _, claim, _) = load_running_claim(store_path, claim_id)?; + if claim.obligation_id != input.obligation.obligation_id { + return Err(anyhow!( + "semantic projection claim does not bind input obligation" + )); + } + authenticate_claim_authority(store_path, &claim)?; let raw_receipt = match super::semantic_index::index_memory_semantic_partition( &input.snapshot, &input.obligation.swarm_id, @@ -349,134 +1040,49 @@ pub fn execute_memory_semantic_projection( } } -pub(crate) fn reopen_succeeded_projection_claim( - store_path: &Path, - current: &MemorySemanticProjectionClaim, - obligation: &MemorySemanticProjectionObligation, - executor_id: &str, - reopened_at: &str, -) -> Result { - validate_memory_semantic_projection_claim(current)?; - validate_identity_and_time(executor_id, reopened_at)?; - if current.status != "succeeded" || current.obligation_id != obligation.obligation_id { - return Err(anyhow!( - "semantic projection repair requires exact succeeded claim" - )); - } - let cache = semantic_projector_cache(store_path)?; - let opening = SingleFileMessagePackBackingStore::new(store_path).pull_all()?; - let stored_claim = decode_one::(&opening, ¤t.scope_id)? - .ok_or_else(|| anyhow!("semantic projection repair claim disappeared"))?; - if stored_claim != *current { - return Err(anyhow!("semantic projection repair claim advanced")); - } - let old_attempt = decode_one::(&opening, ¤t.attempt_id)? - .ok_or_else(|| anyhow!("semantic projection repair lost succeeded attempt"))?; - if old_attempt.status != "succeeded" { - return Err(anyhow!( - "semantic projection repair attempt is not succeeded" - )); - } - let next = running_claim( - ¤t.scope_id, - obligation, - executor_id, - current.epoch + 1, - reopened_at, - ); - let next_attempt = running_attempt(&next); - terminal_or_recovery_cas( - store_path, - &opening, - current, - &old_attempt, - vec![ - cache.prepare_entry(¤t.scope_id, &next)?.0, - cache - .prepare_entry(&old_attempt.attempt_id, &old_attempt)? - .0, - cache - .prepare_entry(&next_attempt.attempt_id, &next_attempt)? - .0, - ], - )?; - Ok(next) -} - -pub(crate) fn claim_memory_semantic_projection( - store_path: impl AsRef, - obligation: &MemorySemanticProjectionObligation, - executor_id: &str, - claimed_at: &str, -) -> Result { - validate_memory_semantic_projection_obligation(obligation)?; - validate_identity_and_time(executor_id, claimed_at)?; - let store_path = store_path.as_ref(); - let cache = semantic_projector_cache(store_path)?; - let opening = SingleFileMessagePackBackingStore::new(store_path).pull_all()?; - let stored = - decode_one::(&opening, &obligation.obligation_id)? - .ok_or_else(|| anyhow!("semantic projection claim requires persisted obligation"))?; - if stored != *obligation { - return Err(anyhow!("semantic projection obligation identity collision")); - } - let scope_id = projection_scope_id(&obligation.swarm_id, &obligation.partition)?; - let current = decode_one::(&opening, &scope_id)?; - if let Some(current) = ¤t { - validate_memory_semantic_projection_claim(current)?; - if current.status == "running" { - return Err(anyhow!( - "semantic projection scope is already claimed by executor {:?}; executor identity does not confer shared mutation authority", - current.executor_id - )); - } - if current.status == "succeeded" && current.obligation_id == obligation.obligation_id { - return Ok(current.clone()); - } - } - let epoch = current.as_ref().map_or(1, |claim| claim.epoch + 1); - let claim = running_claim(&scope_id, obligation, executor_id, epoch, claimed_at); - let attempt = running_attempt(&claim); - let obligation_envelope = exact_envelope( - &opening, - "gamecult.epiphany.memory_semantic_projection_obligation", - &obligation.obligation_id, - )?; - let mut expected = vec![obligation_envelope.clone()]; - let mut replacements = vec![obligation_envelope]; - if current.is_some() { - let current_envelope = exact_envelope( - &opening, - "gamecult.epiphany.memory_semantic_projection_claim", - &scope_id, - )?; - expected.push(current_envelope); - } - replacements.push(cache.prepare_entry(&scope_id, &claim)?.0); - replacements.push(cache.prepare_entry(&attempt.attempt_id, &attempt)?.0); - if !SingleFileMessagePackBackingStore::new(store_path) - .compare_and_swap_batch(&expected, replacements)? - { - return Err(anyhow!("semantic projection claim lost exact CAS")); - } - Ok(claim) -} - -#[allow(dead_code)] // Deliberately withheld from callers until Idunn supplies typed recovery authority. -pub(crate) fn recover_memory_semantic_projection_claim( +#[allow(clippy::too_many_arguments)] +#[allow(dead_code, clippy::too_many_arguments)] // Withheld behind sealed recovery evidence. +pub(crate) fn idunn_recover_memory_semantic_projection( store_path: impl AsRef, + input: &MemorySemanticProjectionInput, expected_claim_id: &str, - executor_id: &str, + replacement_executor_id: &str, + replacement_executor_incarnation: &str, + evidence: &IdunnSemanticRecoveryEvidence, recovered_at: &str, - reason: &str, -) -> Result { - validate_identity_and_time(executor_id, recovered_at)?; - if expected_claim_id.trim().is_empty() || reason.trim().is_empty() { +) -> Result<( + MemorySemanticProjectorRecoveryAuthorization, + MemorySemanticProjectionClaim, +)> { + validate_identity_and_time(replacement_executor_id, recovered_at)?; + validate_opaque_identity( + replacement_executor_incarnation, + "replacement executor incarnation", + )?; + validate_opaque_identity(&evidence.issuer_incarnation, "issuer incarnation")?; + validate_idunn_semantic_recovery_evidence(evidence)?; + if replacement_executor_incarnation != evidence.provider_incarnation { return Err(anyhow!( - "semantic projection recovery requires claim and reason" + "recovery executor incarnation is not the observed provider incarnation" )); } + ensure_strictly_before( + &evidence.lifecycle_receipt_completed_at, + &evidence.provider_heartbeat_at, + "provider heartbeat must follow lifecycle receipt", + )?; + ensure_not_before( + recovered_at, + &evidence.provider_heartbeat_at, + "recovery precedes provider heartbeat", + )?; let store_path = store_path.as_ref(); + validate_memory_semantic_projection_obligation(&input.obligation)?; + if evidence.canonical_store_id != memory_semantic_projector_store_id(store_path)? { + return Err(anyhow!( + "recovery evidence belongs to another canonical store" + )); + } let cache = semantic_projector_cache(store_path)?; let opening = SingleFileMessagePackBackingStore::new(store_path).pull_all()?; let current = decode_all::(&opening)? @@ -486,24 +1092,73 @@ pub(crate) fn recover_memory_semantic_projection_claim( validate_memory_semantic_projection_claim(¤t)?; if current.status != "running" { return Err(anyhow!( - "only a running semantic projection claim can recover" + "semantic projection recovery requires exact running claim" )); } + authenticate_claim_authority_from_envelopes(&opening, ¤t)?; let obligation = decode_one::(&opening, ¤t.obligation_id)? .ok_or_else(|| anyhow!("semantic projection recovery lost obligation"))?; + if obligation != input.obligation + || current.obligation_id != input.obligation.obligation_id + || current.scope_id + != projection_scope_id(&input.obligation.swarm_id, &input.obligation.partition)? + || evidence.scope_id != current.scope_id + || evidence.swarm_id != obligation.swarm_id + || evidence.partition != obligation.partition + || evidence.obligation_id != obligation.obligation_id + || evidence.claim_id != current.claim_id + || evidence.claim_epoch != current.epoch + || evidence.abandoned_executor_incarnation != current.executor_incarnation + { + return Err(anyhow!( + "recovery evidence does not bind exact canonical claim input" + )); + } + for expected in &input.authority.envelopes { + let persisted = opening + .iter() + .find(|row| row.r#type == expected.r#type && row.key == expected.key) + .ok_or_else(|| anyhow!("recovery canonical input authority disappeared"))?; + if persisted != expected { + return Err(anyhow!("recovery canonical input authority advanced")); + } + } let old_attempt = decode_one::(&opening, ¤t.attempt_id)? .ok_or_else(|| anyhow!("semantic projection recovery lost running attempt"))?; validate_memory_semantic_projection_attempt(&old_attempt)?; - if old_attempt.status != "running" { + if old_attempt.status != "running" || !attempt_authenticates_claim(&old_attempt, ¤t) { return Err(anyhow!( - "semantic projection recovery attempt is not running" + "semantic projection recovery running attempt disagrees" )); } + ensure_not_before(recovered_at, ¤t.claimed_at, "recovery before claim")?; + let authorization_id = format!( + "memory-semantic-recovery-authorization-{:x}", + Sha256::digest( + format!( + "{}|{}|{}|{}", + evidence.canonical_store_id, + evidence.lifecycle_intent_digest, + evidence.lifecycle_receipt_digest, + evidence.provider_heartbeat_digest + ) + .as_bytes() + ) + ); + if opening.iter().any(|row| { + row.r#type == MemorySemanticProjectorRecoveryAuthorization::TYPE + && row.key == authorization_id + }) { + return Err(anyhow!("semantic recovery evidence was already consumed")); + } let next = running_claim( ¤t.scope_id, &obligation, - executor_id, + replacement_executor_id, + replacement_executor_incarnation, + "recovery_authorization", + &authorization_id, current.epoch + 1, recovered_at, ); @@ -511,25 +1166,79 @@ pub(crate) fn recover_memory_semantic_projection_claim( let failed_attempt = MemorySemanticProjectionAttempt { completed_at: Some(recovered_at.to_string()), status: "failed".to_string(), - error: Some(format!("superseded by fenced recovery: {reason}")), + error: Some("superseded by authenticated fenced recovery".to_string()), ..old_attempt.clone() }; + let authorization = MemorySemanticProjectorRecoveryAuthorization { + schema_version: MEMORY_SEMANTIC_PROJECTOR_RECOVERY_AUTHORIZATION_SCHEMA_VERSION.to_string(), + authorization_id: authorization_id.clone(), + scope_id: current.scope_id.clone(), + swarm_id: obligation.swarm_id.clone(), + partition: obligation.partition.clone(), + obligation_id: obligation.obligation_id.clone(), + claim_id: current.claim_id.clone(), + claim_epoch: current.epoch, + attempt_id: current.attempt_id.clone(), + abandoned_executor_id: current.executor_id.clone(), + abandoned_executor_incarnation: current.executor_incarnation.clone(), + replacement_executor_id: replacement_executor_id.to_string(), + replacement_executor_incarnation: replacement_executor_incarnation.to_string(), + issuer_id: "idunn".to_string(), + issuer_incarnation: evidence.issuer_incarnation.clone(), + lifecycle_intent_id: evidence.lifecycle_intent_id.clone(), + lifecycle_intent_digest: evidence.lifecycle_intent_digest.clone(), + lifecycle_receipt_id: evidence.lifecycle_receipt_id.clone(), + lifecycle_receipt_digest: evidence.lifecycle_receipt_digest.clone(), + lifecycle_receipt_completed_at: evidence.lifecycle_receipt_completed_at.clone(), + provider_heartbeat_id: evidence.provider_heartbeat_id.clone(), + provider_heartbeat_digest: evidence.provider_heartbeat_digest.clone(), + provider_incarnation: evidence.provider_incarnation.clone(), + provider_heartbeat_at: evidence.provider_heartbeat_at.clone(), + canonical_store_id: evidence.canonical_store_id.clone(), + lifecycle_correlation_id: evidence.lifecycle_correlation_id.clone(), + status: "consumed".to_string(), + issued_at: recovered_at.to_string(), + consumed_at: Some(recovered_at.to_string()), + resulting_claim_id: Some(next.claim_id.clone()), + resulting_claim_epoch: Some(next.epoch), + }; + validate_memory_semantic_projector_recovery_authorization(&authorization)?; + let mut replacements = vec![ + cache.prepare_entry(¤t.scope_id, &next)?.0, + cache + .prepare_entry(&failed_attempt.attempt_id, &failed_attempt)? + .0, + cache + .prepare_entry(&next_attempt.attempt_id, &next_attempt)? + .0, + cache + .prepare_entry(&authorization.authorization_id, &authorization)? + .0, + ]; + let authority_expected = input + .authority + .envelopes + .iter() + .filter(|row| { + !matches!( + row.r#type.as_str(), + "gamecult.epiphany.memory_semantic_projection_obligation" + | "gamecult.epiphany.memory_semantic_projection_claim" + | "gamecult.epiphany.memory_semantic_projection_attempt" + ) + }) + .cloned() + .collect::>(); + replacements.extend(authority_expected.iter().cloned()); terminal_or_recovery_cas( store_path, &opening, ¤t, &old_attempt, - vec![ - cache.prepare_entry(¤t.scope_id, &next)?.0, - cache - .prepare_entry(&failed_attempt.attempt_id, &failed_attempt)? - .0, - cache - .prepare_entry(&next_attempt.attempt_id, &next_attempt)? - .0, - ], + replacements, + &authority_expected, )?; - Ok(next) + Ok((authorization, next)) } pub(crate) fn fail_memory_semantic_projection_claim( @@ -545,6 +1254,7 @@ pub(crate) fn fail_memory_semantic_projection_claim( .map_err(|_| anyhow!("semantic projection completion time must be RFC3339"))?; let store_path = store_path.as_ref(); let (cache, opening, claim, attempt) = load_running_claim(store_path, claim_id)?; + ensure_not_before(completed_at, &claim.claimed_at, "failure before claim")?; let terminal_claim = MemorySemanticProjectionClaim { status: "failed".to_string(), completed_at: Some(completed_at.to_string()), @@ -567,6 +1277,7 @@ pub(crate) fn fail_memory_semantic_projection_claim( .prepare_entry(&attempt.attempt_id, &terminal_attempt)? .0, ], + &[], )?; Ok(terminal_claim) } @@ -587,9 +1298,20 @@ pub(crate) fn succeed_memory_semantic_projection_claim( } let store_path = store_path.as_ref(); let (cache, opening, claim, attempt) = load_running_claim(store_path, claim_id)?; + ensure_not_before(completed_at, &claim.claimed_at, "success before claim")?; let obligation = decode_one::(&opening, &claim.obligation_id)? .ok_or_else(|| anyhow!("semantic projection success lost obligation"))?; + ensure_not_before( + &receipt.indexed_at, + &claim.claimed_at, + "index receipt before claim", + )?; + ensure_not_before( + completed_at, + &receipt.indexed_at, + "success before index receipt", + )?; if receipt.obligation_id != claim.obligation_id || receipt.claim_id != claim.claim_id || receipt.claim_epoch != claim.epoch @@ -680,6 +1402,12 @@ pub(crate) fn validate_memory_semantic_projection_claim( || claim.obligation_id.trim().is_empty() || claim.attempt_id.trim().is_empty() || claim.executor_id.trim().is_empty() + || !is_opaque_identity(&claim.executor_incarnation) + || !matches!( + claim.authority_kind.as_str(), + "executor_grant" | "recovery_authorization" + ) + || !is_opaque_identity(&claim.authority_id) || claim.epoch == 0 || chrono::DateTime::parse_from_rfc3339(&claim.claimed_at).is_err() { @@ -701,12 +1429,132 @@ pub(crate) fn validate_memory_semantic_projection_claim( } } +pub fn validate_memory_semantic_projector_executor_grant( + grant: &MemorySemanticProjectorExecutorGrant, +) -> Result<()> { + let consumed = grant.status == "consumed" + && grant.consumed_at.as_deref().is_some_and(valid_rfc3339) + && grant + .resulting_claim_id + .as_deref() + .is_some_and(is_opaque_identity) + && grant.resulting_claim_epoch.is_some_and(|epoch| epoch > 0); + if grant.schema_version != MEMORY_SEMANTIC_PROJECTOR_EXECUTOR_GRANT_SCHEMA_VERSION + || !grant + .grant_id + .starts_with("memory-semantic-executor-grant-") + || !is_opaque_identity(&grant.grant_id) + || !is_opaque_identity(&grant.scope_id) + || !is_opaque_identity(&grant.swarm_id) + || !matches!(grant.partition.as_str(), "mind" | "modeling") + || !is_opaque_identity(&grant.obligation_id) + || !is_opaque_identity(&grant.executor_id) + || !is_opaque_identity(&grant.executor_incarnation) + || !matches!(grant.purpose.as_str(), "execute" | "repair") + || grant.issuer_id != "idunn" + || !is_opaque_identity(&grant.issuer_incarnation) + || !valid_rfc3339(&grant.issued_at) + || grant.consumed_at.as_deref() != Some(grant.issued_at.as_str()) + || !consumed + || !matches!( + grant.predecessor_claim_status.as_str(), + "absent" | "failed" | "succeeded" + ) + || (grant.predecessor_claim_status == "absent" + && (grant.predecessor_claim_id.is_some() || grant.predecessor_claim_epoch.is_some())) + || (grant.predecessor_claim_status != "absent" + && !(grant + .predecessor_claim_id + .as_deref() + .is_some_and(is_opaque_identity) + && grant.predecessor_claim_epoch.is_some_and(|epoch| epoch > 0))) + || (grant.predecessor_claim_status == "absent" && grant.resulting_claim_epoch != Some(1)) + || grant + .predecessor_claim_epoch + .is_some_and(|epoch| grant.resulting_claim_epoch != Some(epoch + 1)) + || (grant.purpose == "repair" && grant.predecessor_claim_status != "succeeded") + || (grant.purpose == "execute" && grant.predecessor_claim_status == "succeeded") + { + return Err(anyhow!("semantic projector executor grant is invalid")); + } + Ok(()) +} + +pub fn validate_memory_semantic_projector_recovery_authorization( + authorization: &MemorySemanticProjectorRecoveryAuthorization, +) -> Result<()> { + let consumed = authorization.status == "consumed" + && authorization + .consumed_at + .as_deref() + .is_some_and(valid_rfc3339) + && authorization + .resulting_claim_id + .as_deref() + .is_some_and(is_opaque_identity) + && authorization + .resulting_claim_epoch + .is_some_and(|epoch| epoch > authorization.claim_epoch); + if authorization.schema_version + != MEMORY_SEMANTIC_PROJECTOR_RECOVERY_AUTHORIZATION_SCHEMA_VERSION + || !authorization + .authorization_id + .starts_with("memory-semantic-recovery-authorization-") + || !is_opaque_identity(&authorization.authorization_id) + || !is_opaque_identity(&authorization.scope_id) + || !is_opaque_identity(&authorization.swarm_id) + || !matches!(authorization.partition.as_str(), "mind" | "modeling") + || !is_opaque_identity(&authorization.obligation_id) + || !is_opaque_identity(&authorization.claim_id) + || authorization.claim_epoch == 0 + || !is_opaque_identity(&authorization.attempt_id) + || !is_opaque_identity(&authorization.abandoned_executor_id) + || !is_opaque_identity(&authorization.abandoned_executor_incarnation) + || !is_opaque_identity(&authorization.replacement_executor_id) + || !is_opaque_identity(&authorization.replacement_executor_incarnation) + || authorization.issuer_id != "idunn" + || !is_opaque_identity(&authorization.issuer_incarnation) + || !is_opaque_identity(&authorization.lifecycle_intent_id) + || !is_opaque_identity(&authorization.lifecycle_intent_digest) + || !is_opaque_identity(&authorization.lifecycle_receipt_id) + || !is_opaque_identity(&authorization.lifecycle_receipt_digest) + || !valid_rfc3339(&authorization.lifecycle_receipt_completed_at) + || !is_opaque_identity(&authorization.provider_heartbeat_id) + || !is_opaque_identity(&authorization.provider_heartbeat_digest) + || !is_opaque_identity(&authorization.provider_incarnation) + || !valid_rfc3339(&authorization.provider_heartbeat_at) + || !is_opaque_identity(&authorization.canonical_store_id) + || !is_opaque_identity(&authorization.lifecycle_correlation_id) + || authorization.lifecycle_correlation_id != authorization.lifecycle_receipt_id + || authorization.replacement_executor_incarnation != authorization.provider_incarnation + || authorization.provider_incarnation == authorization.abandoned_executor_incarnation + || !strictly_before( + &authorization.lifecycle_receipt_completed_at, + &authorization.provider_heartbeat_at, + ) + || !not_before( + authorization.consumed_at.as_deref().unwrap_or(""), + &authorization.provider_heartbeat_at, + ) + || !valid_rfc3339(&authorization.issued_at) + || authorization.consumed_at.as_deref() != Some(authorization.issued_at.as_str()) + || !consumed + { + return Err(anyhow!( + "semantic projector recovery authorization is invalid" + )); + } + Ok(()) +} + pub(crate) fn semantic_projector_cache(store_path: &Path) -> Result { let mut cache = CultCache::new(); cache.register_entry_type::()?; cache.register_entry_type::()?; cache.register_entry_type::()?; cache.register_entry_type::()?; + cache.register_entry_type::()?; + cache.register_entry_type::()?; cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(store_path)); Ok(cache) } @@ -715,12 +1563,21 @@ fn running_claim( scope_id: &str, obligation: &MemorySemanticProjectionObligation, executor_id: &str, + executor_incarnation: &str, + authority_kind: &str, + authority_id: &str, epoch: u64, claimed_at: &str, ) -> MemorySemanticProjectionClaim { let fingerprint = format!( - "{}|{}|{}|{}", - scope_id, obligation.obligation_id, executor_id, epoch + "{}|{}|{}|{}|{}|{}|{}", + scope_id, + obligation.obligation_id, + executor_id, + executor_incarnation, + authority_kind, + authority_id, + epoch ); let claim_id = format!( "memory-semantic-claim-{:x}", @@ -733,6 +1590,9 @@ fn running_claim( obligation_id: obligation.obligation_id.clone(), attempt_id: format!("memory-semantic-attempt-{claim_id}"), executor_id: executor_id.to_string(), + executor_incarnation: executor_incarnation.to_string(), + authority_kind: authority_kind.to_string(), + authority_id: authority_id.to_string(), epoch, status: "running".to_string(), claimed_at: claimed_at.to_string(), @@ -749,6 +1609,11 @@ fn running_attempt(claim: &MemorySemanticProjectionClaim) -> MemorySemanticProje completed_at: None, status: "running".to_string(), error: None, + claim_id: claim.claim_id.clone(), + claim_epoch: claim.epoch, + executor_id: claim.executor_id.clone(), + executor_incarnation: claim.executor_incarnation.clone(), + authority_id: claim.authority_id.clone(), } } @@ -774,18 +1639,127 @@ fn load_running_claim( let attempt = decode_one::(&opening, &claim.attempt_id)? .ok_or_else(|| anyhow!("semantic projection running attempt is missing"))?; validate_memory_semantic_projection_attempt(&attempt)?; - if attempt.status != "running" || attempt.obligation_id != claim.obligation_id { + if attempt.status != "running" || !attempt_authenticates_claim(&attempt, &claim) { return Err(anyhow!("semantic projection attempt does not own claim")); } Ok((cache, opening, claim, attempt)) } +fn authenticate_claim_authority( + store_path: &Path, + claim: &MemorySemanticProjectionClaim, +) -> Result<()> { + let envelopes = SingleFileMessagePackBackingStore::new(store_path).pull_all()?; + authenticate_claim_authority_from_envelopes(&envelopes, claim)?; + if claim.authority_kind == "recovery_authorization" { + let auth = decode_one::( + &envelopes, + &claim.authority_id, + )? + .ok_or_else(|| anyhow!("semantic recovery authority disappeared"))?; + if auth.canonical_store_id != memory_semantic_projector_store_id(store_path)? { + return Err(anyhow!( + "semantic recovery authority belongs to another store" + )); + } + } + Ok(()) +} + +fn authenticate_claim_authority_from_envelopes( + envelopes: &[CultCacheEnvelope], + claim: &MemorySemanticProjectionClaim, +) -> Result<()> { + match claim.authority_kind.as_str() { + "executor_grant" => { + let grant = + decode_one::(envelopes, &claim.authority_id)? + .ok_or_else(|| { + anyhow!("semantic projection claim lost consumed executor authority") + })?; + validate_memory_semantic_projector_executor_grant(&grant)?; + if grant.grant_id != claim.authority_id + || grant.scope_id != claim.scope_id + || grant.obligation_id != claim.obligation_id + || grant.executor_id != claim.executor_id + || grant.executor_incarnation != claim.executor_incarnation + || grant.resulting_claim_id.as_deref() != Some(claim.claim_id.as_str()) + || grant.resulting_claim_epoch != Some(claim.epoch) + { + return Err(anyhow!( + "semantic projection claim executor authority disagrees" + )); + } + } + "recovery_authorization" => { + let auth = decode_one::( + envelopes, + &claim.authority_id, + )? + .ok_or_else(|| anyhow!("semantic projection claim lost consumed recovery authority"))?; + validate_memory_semantic_projector_recovery_authorization(&auth)?; + let abandoned_attempt = + decode_one::(envelopes, &auth.attempt_id)? + .ok_or_else(|| anyhow!("semantic recovery authority lost abandoned attempt"))?; + validate_memory_semantic_projection_attempt(&abandoned_attempt)?; + if auth.authorization_id != claim.authority_id + || auth.scope_id != claim.scope_id + || auth.obligation_id != claim.obligation_id + || auth.replacement_executor_id != claim.executor_id + || auth.replacement_executor_incarnation != claim.executor_incarnation + || auth.resulting_claim_id.as_deref() != Some(claim.claim_id.as_str()) + || auth.resulting_claim_epoch != Some(claim.epoch) + || abandoned_attempt.attempt_id != auth.attempt_id + || abandoned_attempt.obligation_id != auth.obligation_id + || abandoned_attempt.claim_id != auth.claim_id + || abandoned_attempt.claim_epoch != auth.claim_epoch + || abandoned_attempt.executor_id != auth.abandoned_executor_id + || abandoned_attempt.executor_incarnation != auth.abandoned_executor_incarnation + || abandoned_attempt.status != "failed" + || abandoned_attempt.completed_at.as_deref() != auth.consumed_at.as_deref() + { + return Err(anyhow!( + "semantic projection claim recovery authority disagrees" + )); + } + } + _ => { + return Err(anyhow!( + "semantic projection claim authority kind is invalid" + )); + } + } + Ok(()) +} + +fn attempt_authenticates_claim( + attempt: &MemorySemanticProjectionAttempt, + claim: &MemorySemanticProjectionClaim, +) -> bool { + attempt.attempt_id == claim.attempt_id + && attempt.obligation_id == claim.obligation_id + && attempt.claim_id == claim.claim_id + && attempt.claim_epoch == claim.epoch + && attempt.executor_id == claim.executor_id + && attempt.executor_incarnation == claim.executor_incarnation + && attempt.authority_id == claim.authority_id + && attempt.started_at == claim.claimed_at + && match (&attempt.completed_at, &claim.completed_at) { + (None, None) => true, + (Some(attempt_completed), Some(claim_completed)) => { + attempt_completed == claim_completed + } + _ => false, + } +} + fn terminal_or_recovery_cas( store_path: &Path, opening: &[CultCacheEnvelope], claim: &MemorySemanticProjectionClaim, attempt: &MemorySemanticProjectionAttempt, mut replacements: Vec, + extra_expected: &[CultCacheEnvelope], ) -> Result<()> { let obligation = exact_envelope( opening, @@ -803,10 +1777,11 @@ fn terminal_or_recovery_cas( &attempt.attempt_id, )?; replacements.push(obligation.clone()); - if !SingleFileMessagePackBackingStore::new(store_path).compare_and_swap_batch( - &[obligation, claim_envelope, attempt_envelope], - replacements, - )? { + let mut expected = vec![obligation, claim_envelope, attempt_envelope]; + expected.extend_from_slice(extra_expected); + if !SingleFileMessagePackBackingStore::new(store_path) + .compare_and_swap_batch(&expected, replacements)? + { return Err(anyhow!( "semantic projection terminal transition lost exact CAS" )); @@ -863,377 +1838,116 @@ fn validate_identity_and_time(identity: &str, time: &str) -> Result<()> { Ok(()) } +fn validate_opaque_identity(value: &str, label: &str) -> Result<()> { + if !is_opaque_identity(value) { + return Err(anyhow!("semantic projection {label} is invalid")); + } + Ok(()) +} + +fn is_opaque_identity(value: &str) -> bool { + !value.is_empty() + && value.len() <= 256 + && value + .bytes() + .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.' | b':')) +} + +fn valid_rfc3339(value: &str) -> bool { + chrono::DateTime::parse_from_rfc3339(value).is_ok() +} + +fn validate_idunn_semantic_recovery_evidence( + evidence: &IdunnSemanticRecoveryEvidence, +) -> Result<()> { + for (value, label) in [ + (&evidence.issuer_incarnation, "issuer incarnation"), + (&evidence.lifecycle_intent_id, "lifecycle intent identity"), + (&evidence.lifecycle_intent_digest, "lifecycle intent digest"), + (&evidence.lifecycle_receipt_id, "lifecycle receipt identity"), + ( + &evidence.lifecycle_receipt_digest, + "lifecycle receipt digest", + ), + ( + &evidence.provider_heartbeat_id, + "provider heartbeat identity", + ), + ( + &evidence.provider_heartbeat_digest, + "provider heartbeat digest", + ), + (&evidence.provider_incarnation, "provider incarnation"), + (&evidence.canonical_store_id, "canonical store identity"), + (&evidence.scope_id, "scope identity"), + (&evidence.swarm_id, "swarm identity"), + (&evidence.obligation_id, "obligation identity"), + (&evidence.claim_id, "claim identity"), + ( + &evidence.abandoned_executor_incarnation, + "abandoned executor incarnation", + ), + ( + &evidence.lifecycle_correlation_id, + "lifecycle correlation identity", + ), + ] { + validate_opaque_identity(value, label)?; + } + if !valid_rfc3339(&evidence.lifecycle_receipt_completed_at) + || !valid_rfc3339(&evidence.provider_heartbeat_at) + || !matches!(evidence.partition.as_str(), "mind" | "modeling") + || evidence.claim_epoch == 0 + || evidence.provider_incarnation == evidence.abandoned_executor_incarnation + || evidence.lifecycle_correlation_id != evidence.lifecycle_receipt_id + || !strictly_before( + &evidence.lifecycle_receipt_completed_at, + &evidence.provider_heartbeat_at, + ) + { + return Err(anyhow!("semantic recovery evidence chronology is invalid")); + } + Ok(()) +} + +fn strictly_before(earlier: &str, later: &str) -> bool { + match ( + chrono::DateTime::parse_from_rfc3339(earlier), + chrono::DateTime::parse_from_rfc3339(later), + ) { + (Ok(earlier), Ok(later)) => earlier < later, + _ => false, + } +} + +fn not_before(later: &str, earlier: &str) -> bool { + match ( + chrono::DateTime::parse_from_rfc3339(later), + chrono::DateTime::parse_from_rfc3339(earlier), + ) { + (Ok(later), Ok(earlier)) => later >= earlier, + _ => false, + } +} + +fn ensure_strictly_before(earlier: &str, later: &str, message: &str) -> Result<()> { + if !strictly_before(earlier, later) { + return Err(anyhow!(message.to_string())); + } + Ok(()) +} + +fn ensure_not_before(later: &str, earlier: &str, message: &str) -> Result<()> { + let later = chrono::DateTime::parse_from_rfc3339(later) + .map_err(|_| anyhow!("semantic projection later time must be RFC3339"))?; + let earlier = chrono::DateTime::parse_from_rfc3339(earlier) + .map_err(|_| anyhow!("semantic projection earlier time must be RFC3339"))?; + if later < earlier { + return Err(anyhow!(message.to_string())); + } + Ok(()) +} + fn now_rfc3339() -> String { chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true) } - -#[cfg(test)] -mod tests { - use super::*; - use crate::{ - MEMORY_SEMANTIC_INDEX_RECEIPT_SCHEMA_VERSION, - MEMORY_SEMANTIC_PROJECTION_OBLIGATION_SCHEMA_VERSION, SEMANTIC_PROJECTION_SCHEMA_VERSION, - }; - use tempfile::tempdir; - - fn obligation() -> MemorySemanticProjectionObligation { - MemorySemanticProjectionObligation { - schema_version: MEMORY_SEMANTIC_PROJECTION_OBLIGATION_SCHEMA_VERSION.to_string(), - obligation_id: "obligation-modeling-7".to_string(), - swarm_id: "swarm-a".to_string(), - partition: "modeling".to_string(), - canonical_source_id: "runtime/repo-model".to_string(), - source_commit_id: "admission-7".to_string(), - graph_id: "repo-model".to_string(), - source_generation: 7, - source_model_hash: "model-hash-7".to_string(), - canonical_content_set_hash: "content-set-7".to_string(), - projection_schema_version: SEMANTIC_PROJECTION_SCHEMA_VERSION.to_string(), - created_at: "2026-07-15T04:00:00Z".to_string(), - } - } - - fn source_head() -> MemorySemanticProjectionSourceHead { - let obligation = obligation(); - MemorySemanticProjectionSourceHead { - swarm_id: obligation.swarm_id, - partition: obligation.partition, - canonical_source_id: obligation.canonical_source_id, - source_commit_id: obligation.source_commit_id, - graph_id: obligation.graph_id, - source_generation: obligation.source_generation, - source_model_hash: obligation.source_model_hash, - canonical_content_set_hash: obligation.canonical_content_set_hash, - } - } - - fn receipt() -> MemorySemanticIndexReceipt { - let obligation = obligation(); - MemorySemanticIndexReceipt { - schema_version: MEMORY_SEMANTIC_INDEX_RECEIPT_SCHEMA_VERSION.to_string(), - receipt_id: "receipt-modeling-7".to_string(), - swarm_id: obligation.swarm_id, - partition: obligation.partition, - collection_name: "modeling".to_string(), - graph_id: obligation.graph_id, - model_revision: obligation.source_generation, - model_hash: obligation.source_model_hash, - embedding_provider_id: "embedder".to_string(), - embedding_model: "model".to_string(), - vector_dimensions: 3, - indexed_document_count: 1, - deleted_document_count: 0, - canonical_content_set_hash: obligation.canonical_content_set_hash, - indexed_at: "2026-07-15T04:03:00Z".to_string(), - status: "ready".to_string(), - obligation_id: String::new(), - canonical_source_id: String::new(), - source_commit_id: String::new(), - source_generation: obligation.source_generation, - projection_schema_version: SEMANTIC_PROJECTION_SCHEMA_VERSION.to_string(), - claim_id: String::new(), - claim_epoch: 0, - } - } - - fn receipt_for_claim(claim: &MemorySemanticProjectionClaim) -> MemorySemanticIndexReceipt { - MemorySemanticIndexReceipt { - obligation_id: claim.obligation_id.clone(), - claim_id: claim.claim_id.clone(), - claim_epoch: claim.epoch, - ..receipt() - } - } - - #[test] - fn scope_claim_recovery_fences_old_executor_and_terminalizes_exact_success() -> Result<()> { - let temp = tempdir()?; - let store = temp.path().join("projector.msgpack"); - let obligation = obligation(); - let mut cache = semantic_projector_cache(&store)?; - cache.put(&obligation.obligation_id, &obligation)?; - - let first = claim_memory_semantic_projection( - &store, - &obligation, - "executor-a", - "2026-07-15T04:01:00Z", - )?; - assert!( - claim_memory_semantic_projection( - &store, - &obligation, - "executor-a", - "2026-07-15T04:01:00Z" - ) - .is_err(), - "executor labels are not reusable mutation capabilities" - ); - assert!( - claim_memory_semantic_projection( - &store, - &obligation, - "executor-b", - "2026-07-15T04:01:01Z" - ) - .is_err() - ); - let recovered = recover_memory_semantic_projection_claim( - &store, - &first.claim_id, - "executor-b", - "2026-07-15T04:02:00Z", - "daemon restarted under operator recovery", - )?; - assert_eq!(recovered.epoch, first.epoch + 1); - assert!( - fail_memory_semantic_projection_claim( - &store, - &first.claim_id, - "2026-07-15T04:02:30Z", - "late old executor" - ) - .is_err() - ); - - let mut cache = semantic_projector_cache(&store)?; - cache.pull_all_backing_stores()?; - let authority_envelope = cache - .snapshot_envelopes() - .into_iter() - .find(|envelope| { - envelope.r#type == "gamecult.epiphany.memory_semantic_projection_obligation" - && envelope.key == obligation.obligation_id - }) - .expect("obligation envelope"); - let bound = succeed_memory_semantic_projection_claim( - &store, - &recovered.claim_id, - &MemorySemanticProjectionAuthoritySnapshot { - head: source_head(), - envelopes: vec![authority_envelope], - }, - receipt_for_claim(&recovered), - "2026-07-15T04:03:00Z", - )?; - assert_eq!(bound.obligation_id, obligation.obligation_id); - - let mut cache = semantic_projector_cache(&store)?; - cache.pull_all_backing_stores()?; - let terminal = cache - .get::(&recovered.scope_id)? - .expect("terminal claim"); - assert_eq!(terminal.status, "succeeded"); - let old_attempt = cache - .get::(&first.attempt_id)? - .expect("fenced old attempt"); - assert_eq!(old_attempt.status, "failed"); - let new_attempt = cache - .get::(&recovered.attempt_id)? - .expect("successful recovered attempt"); - assert_eq!(new_attempt.status, "succeeded"); - - let repair_attempt = MemorySemanticProjectionAttempt { - schema_version: MEMORY_SEMANTIC_PROJECTION_ATTEMPT_SCHEMA_VERSION.to_string(), - attempt_id: "repair-after-success".to_string(), - obligation_id: obligation.obligation_id.clone(), - started_at: "2026-07-15T04:04:00Z".to_string(), - completed_at: Some("2026-07-15T04:04:01Z".to_string()), - status: "failed".to_string(), - error: Some("observed cache corruption".to_string()), - }; - cache.put(&repair_attempt.attempt_id, &repair_attempt)?; - assert!( - load_memory_semantic_projection_success(&store, &obligation, &source_head())?.is_none() - ); - Ok(()) - } - - #[test] - fn terminal_success_refuses_a_preexisting_receipt_identity_collision() -> Result<()> { - let temp = tempdir()?; - let store = temp.path().join("receipt-collision.msgpack"); - let obligation = obligation(); - let mut cache = semantic_projector_cache(&store)?; - cache.put(&obligation.obligation_id, &obligation)?; - let claim = claim_memory_semantic_projection( - &store, - &obligation, - "executor-a", - "2026-07-15T04:01:00Z", - )?; - let mut collision = receipt_for_claim(&claim); - collision.status = "failed".to_string(); - cache.put(&collision.receipt_id, &collision)?; - let authority_envelope = SingleFileMessagePackBackingStore::new(&store) - .pull_all()? - .into_iter() - .find(|envelope| { - envelope.r#type == "gamecult.epiphany.memory_semantic_projection_obligation" - && envelope.key == obligation.obligation_id - }) - .expect("obligation envelope"); - assert!( - succeed_memory_semantic_projection_claim( - &store, - &claim.claim_id, - &MemorySemanticProjectionAuthoritySnapshot { - head: source_head(), - envelopes: vec![authority_envelope], - }, - receipt_for_claim(&claim), - "2026-07-15T04:03:00Z", - ) - .is_err() - ); - Ok(()) - } - - #[test] - fn succeeded_claim_with_missing_receipt_reopens_under_a_new_fence() -> Result<()> { - let temp = tempdir()?; - let store = temp.path().join("missing-receipt.msgpack"); - let obligation = obligation(); - let mut cache = semantic_projector_cache(&store)?; - cache.put(&obligation.obligation_id, &obligation)?; - let claim = claim_memory_semantic_projection( - &store, - &obligation, - "executor-a", - "2026-07-15T04:01:00Z", - )?; - let authority_envelope = SingleFileMessagePackBackingStore::new(&store) - .pull_all()? - .into_iter() - .find(|envelope| { - envelope.r#type == "gamecult.epiphany.memory_semantic_projection_obligation" - && envelope.key == obligation.obligation_id - }) - .expect("obligation envelope"); - let bound = succeed_memory_semantic_projection_claim( - &store, - &claim.claim_id, - &MemorySemanticProjectionAuthoritySnapshot { - head: source_head(), - envelopes: vec![authority_envelope], - }, - receipt_for_claim(&claim), - "2026-07-15T04:03:00Z", - )?; - let mut cache = semantic_projector_cache(&store)?; - cache.pull_all_backing_stores()?; - cache.delete::(&bound.receipt_id)?; - let terminal = cache - .get::(&claim.scope_id)? - .expect("succeeded claim"); - let reopened = reopen_succeeded_projection_claim( - &store, - &terminal, - &obligation, - "executor-b", - "2026-07-15T04:04:00Z", - )?; - assert_eq!(reopened.status, "running"); - assert_eq!(reopened.epoch, terminal.epoch + 1); - assert_ne!(reopened.claim_id, terminal.claim_id); - Ok(()) - } - - #[test] - fn readiness_refuses_receipt_namespace_substitution_and_invented_epoch() -> Result<()> { - let temp = tempdir()?; - let store = temp.path().join("receipt-namespace-substitution.msgpack"); - let obligation = obligation(); - let mut cache = semantic_projector_cache(&store)?; - cache.put(&obligation.obligation_id, &obligation)?; - let claim = claim_memory_semantic_projection( - &store, - &obligation, - "executor-a", - "2026-07-15T04:01:00Z", - )?; - let authority_envelope = SingleFileMessagePackBackingStore::new(&store) - .pull_all()? - .into_iter() - .find(|envelope| { - envelope.r#type == "gamecult.epiphany.memory_semantic_projection_obligation" - && envelope.key == obligation.obligation_id - }) - .expect("obligation envelope"); - let bound = succeed_memory_semantic_projection_claim( - &store, - &claim.claim_id, - &MemorySemanticProjectionAuthoritySnapshot { - head: source_head(), - envelopes: vec![authority_envelope], - }, - receipt_for_claim(&claim), - "2026-07-15T04:03:00Z", - )?; - - let mut cache = semantic_projector_cache(&store)?; - cache.pull_all_backing_stores()?; - cache.delete::(&bound.receipt_id)?; - let mut substituted = bound.clone(); - substituted.claim_id = "invented-claim".to_string(); - cache.put(&substituted.receipt_id, &substituted)?; - assert!( - load_memory_semantic_projection_success(&store, &obligation, &source_head()).is_err() - ); - - cache.delete::(&substituted.receipt_id)?; - let mut invented_epoch = bound; - invented_epoch.claim_epoch = claim.epoch + 99; - cache.put(&invented_epoch.receipt_id, &invented_epoch)?; - assert!( - load_memory_semantic_projection_success(&store, &obligation, &source_head()).is_err() - ); - Ok(()) - } - - #[test] - fn readiness_refuses_succeeded_claim_with_substituted_attempt_obligation() -> Result<()> { - let temp = tempdir()?; - let store = temp.path().join("attempt-substitution.msgpack"); - let obligation = obligation(); - let mut cache = semantic_projector_cache(&store)?; - cache.put(&obligation.obligation_id, &obligation)?; - let claim = claim_memory_semantic_projection( - &store, - &obligation, - "executor-a", - "2026-07-15T04:01:00Z", - )?; - let authority_envelope = SingleFileMessagePackBackingStore::new(&store) - .pull_all()? - .into_iter() - .find(|envelope| { - envelope.r#type == "gamecult.epiphany.memory_semantic_projection_obligation" - && envelope.key == obligation.obligation_id - }) - .expect("obligation envelope"); - succeed_memory_semantic_projection_claim( - &store, - &claim.claim_id, - &MemorySemanticProjectionAuthoritySnapshot { - head: source_head(), - envelopes: vec![authority_envelope], - }, - receipt_for_claim(&claim), - "2026-07-15T04:03:00Z", - )?; - - let mut cache = semantic_projector_cache(&store)?; - cache.pull_all_backing_stores()?; - let mut attempt = cache - .get::(&claim.attempt_id)? - .expect("terminal attempt"); - cache.delete::(&attempt.attempt_id)?; - attempt.obligation_id = "invented-obligation".to_string(); - cache.put(&attempt.attempt_id, &attempt)?; - assert!( - load_memory_semantic_projection_success(&store, &obligation, &source_head()).is_err() - ); - Ok(()) - } -} diff --git a/notes/epiphany-current-algorithmic-map.md b/notes/epiphany-current-algorithmic-map.md index 327201bf..59e46374 100644 --- a/notes/epiphany-current-algorithmic-map.md +++ b/notes/epiphany-current-algorithmic-map.md @@ -590,5 +590,59 @@ retaining pulse `completed`, iteration `1`, and lifecycle receipt No command args or routine payloads are projected. Next merge these rows into the main provider-owned Eve composition rather than keeping a specialist query only. +## Semantic Projector Executor Authority + +Canonical Mind and Modeling admission own projection demand. Their transaction +writes the exact semantic-projection obligation beside the canonical source +head; neither Idunn nor the projector may invent that demand. Idunn owns the +decision to assign an executor or fence an abandoned executor. The projector +owns only the claim-bound Qdrant mutation, post-write observation, attempt +terminalization, and exact success receipt. + +The execution chain is now: + +`canonical obligation -> Idunn acquisition CAS -> consumed executor grant + running claim/attempt -> epoch-isolated Qdrant synchronization -> canonical authority reauthentication -> terminal claim/attempt + exact receipt CAS`. + +Acquisition binds scope, obligation, executor identity and incarnation, +purpose, Idunn incarnation, predecessor claim, and resulting claim epoch in one +consumed typed grant. `execute` cannot reopen an exact succeeded claim; +`repair` requires that exact predecessor. The execution CLI accepts a claim id, +not an executor label, and authenticates the consumed authority behind the +claim before touching Qdrant. Acquisition reauthenticates the complete sealed +canonical input and carries its authority envelopes through the same CAS, so a +persisted historical obligation cannot reopen an advanced source head. + +Running-claim recovery reloads the exact Idunn poke intent/receipt and a +provider-authored immutable heartbeat event from CultMesh. It requires an +`awaiting-provider-heartbeat` lifecycle result followed by a newer ready +heartbeat for the replacement provider incarnation, hashes those source +envelopes, requires the heartbeat to name the exact restart receipt as its +startup cause, and consumes a typed recovery authorization in the same CAS that +fails the abandoned attempt and advances the claim epoch. The old executor can +thereafter write only its physically isolated abandoned Qdrant namespace and +cannot terminalize the active CultCache claim. + +Projection health, elapsed time, process status, command exit, Qdrant counts, +CultMesh mirrors, Eve, and swarm overview remain derived sight. They cannot +create an obligation, grant, claim, attempt, success receipt, recovery, or +query admission. Initial execution, retry, repair, and recovered execution all +use the same claim-authenticating execution primitive; only acquisition and +recovery differ in how Idunn establishes the claim authority. + +Verification covers concurrent acquisition with one winner and no issued +litter, predecessor/epoch ordering, purpose separation, claim-authority +authentication, evidence field substitution, lifecycle/heartbeat causal +ordering, recovery single use, and the inability of the recovery command to +execute projection work itself. + +Remaining limits are physical deployment, not permission for another adapter. +There is no open-obligation daemon pulse or packaged single projector owner +yet. The low-level mint and recovery primitives are crate-private. Their public +local-supervisor ports generate incarnations internally, but remain trusted +in-process ports rather than an OS-authenticated Idunn service boundary. +Deployment must keep those ports inside one configured Idunn body until a +durable authenticated service identity owns them. Canonical store and Qdrant +placement must be chosen as one topology; dual projectors are forbidden. + This map must change when ownership changes. Historical scars belong in git, -evidence, or an explicitly archived note—not in the machine's proprioception. +evidence, or an explicitly archived note—not in the machine's Modeling state. diff --git a/notes/fresh-workspace-handoff.md b/notes/fresh-workspace-handoff.md index 354af05f..a9717a4f 100644 --- a/notes/fresh-workspace-handoff.md +++ b/notes/fresh-workspace-handoff.md @@ -1202,6 +1202,50 @@ mirror independently, and `epiphany-verse-query semantic-health` reads a named, compact report without touching canonical state. A stale sealed input is refused rather than published as if it described the current canonical head. -Next: give Idunn typed executor grants, single-use fenced recovery authority, -open-obligation discovery, and one packaged projector owner. CultMesh, Idunn, -Eve, and Gjallar remain unable to mint semantic readiness. +## Idunn semantic executor authority cut (2026-07-15) + +Executor labels no longer open semantic mutation authority. Idunn acquisition +now atomically writes a consumed typed executor grant, a scope claim, and its +running attempt against the exact persisted obligation and predecessor claim. +The grant binds executor incarnation, purpose, Idunn incarnation, predecessor +status/id/epoch, and resulting claim id/epoch. `execute` cannot repair a +succeeded claim; `repair` requires that exact succeeded predecessor. The +projector CLI now requires the acquired claim id and authenticates its consumed +grant before Qdrant work or terminalization. Acquisition reauthenticates the +whole sealed canonical input and carries its authority envelopes through the +same CAS, so an old persisted obligation cannot mint against an advanced head. + +Fenced recovery now consumes typed evidence rather than free-form reason text. +The CultMesh bridge authenticates the exact immutable Idunn poke intent and +successful `awaiting-provider-heartbeat` receipt, plus a provider-authored ready +heartbeat event that causally follows receipt completion and names the +replacement provider incarnation. The heartbeat must also name that exact +restart receipt as its startup cause. Their envelope digests enter a consumed +recovery authorization. One CAS fails the abandoned attempt, advances the +epoch, creates the replacement claim/attempt, and records that authorization. +Recovery rotates authority only; it cannot execute projection or mint success. +Provider heartbeat events are immutable and their latest pointer advances +monotonically per daemon/incarnation. + +Authority map: canonical Mind/Modeling admission owns obligations; Idunn owns +executor assignment and explicit recovery decision; the projector owns claim- +bound mutation and terminal evidence; the query gate alone authenticates +readiness. Health, liveness, timers, command exits, Qdrant state, CultMesh, +Eve, and swarm overview remain derived sight and forbidden writers. Initial +execution, retry, repair, and recovered execution share the same claim- +authenticating projector primitive. + +Remaining limits: open-obligation discovery is not yet attached to the Idunn +daemon, no single projector owner is packaged, canonical-store/Qdrant placement +between local and Yggdrasil remains undecided, and local Qdrant remains +overexposed. The low-level mint and recovery primitives are crate-private. +Their public local-supervisor ports generate incarnations internally, but are +still a trusted in-process boundary rather than authenticated OS IPC. Keep them +inside one configured Idunn body until durable service identity owns them. Do +not expose those ports to untrusted Verse peers. Do not deploy two projectors. + +Next: add a bounded Idunn pulse that discovers authenticated open Mind and +Modeling obligations without creating demand, runs the same acquire/execute +primitive, republishes sight independently, and reports provider readiness only +after its own heartbeat. Then select and package exactly one deployment +topology. diff --git a/state/ledgers.msgpack b/state/ledgers.msgpack index 70188aea..6acfdfd4 100644 Binary files a/state/ledgers.msgpack and b/state/ledgers.msgpack differ diff --git a/state/map.yaml b/state/map.yaml index 0e78deb4..f8e3f80a 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -714,4 +714,4 @@ current_status: - Service lifecycle receipt history is now event-only: `load_epiphany_cultmesh_daemon_service_lifecycle_receipts` filters out the `epiphany-local/daemon-service-lifecycle-receipt/latest` mirror, while `load_latest_epiphany_cultmesh_daemon_service_lifecycle_receipt` remains the latest-state reader. Focused unit test `service_lifecycle_receipt_history_excludes_latest_mirror` proves two per-receipt writes return exactly two history records and latest points at the second. Fresh smoke `verse-query-lifecycle-history-filter-smoke` and live overview/triage `local-20260618-145632-497-efac6f0d` / `local-20260618-145632-321-867947af` prove compact swarm sights still expose service lifecycle attention plus the present elevated runbook artifact without daemon pokes or service mutation. Do not use a broad unscoped cargo test filter for this check on Windows; it fans out across bin harnesses and hit a broken pipe during this pass. - `epiphany-verse-query seed-compact` is the compact seed receipt for wrappers and read-only rites. The older `seed` command still prints the full local Verse context for explicit debugging, but compact wrapper modes now avoid full `seed`/final `query` readback after the combined overview proof exposed Windows debug stack overflow from full-context serialization pressure. last_completed_repo_swarm_cut: Huginn live-fire has crossed the first real repo Body from Persona pressure to branch-local Hands commit and Soul/Mind closure, and now has repeatable closure and redacted-public-proof mouths. On `E:\Projects\Huginn` branch `epiphany/huginn/first-awakening`, `tools/epiphany_local_run.ps1 -Mode repo-persona-intake` accepted item `huginn-inspection-readback-livefire`, Weksa/Persona intake remained sealed, and `epiphany-work derive-plan` lowered it to model-authored safe family `repo.status_section` for `README.md` only. `tools/epiphany_local_run.ps1 -Mode repo-swarm-run` advanced four bounded pulses: run-from-plan, adopt-from-plan, execute-from-plan, and close-from-execute. Huginn commit `e9b07c38fceabbdc89ea35c2156326c80c0393a2` updated only `README.md`; closure receipt `.epiphany\work\work-close-huginn-inspection-readback-livefire.json` records status `closed`, Soul verdict `passed`, Mind state commit `repo-work-close-huginn-inspection-readback-livefire-mind-commit`, safe family `repo.status_section`, path scope `README.md`, Bifrost publication gate, and `privateStateExposed=false`. Verification also ran Huginn's TypeScript compiler through concrete Node path: `C:\Program Files\nodejs\node.exe E:\Projects\Huginn\node_modules\typescript\bin\tsc -p tsconfig.json` passed. Native verifier `epiphany-repo-livefire-closure-smoke --workspace E:\Projects\Huginn --item huginn-inspection-readback-livefire --expected-path README.md --expected-family repo.status_section --expected-gate awaiting-publication --expected-blocker bifrost-publication-missing` checks closure receipt, Mind map entry, overview gate/blocker, authority seals, and `git show` changed paths, returning `status=ok`, `soulVerdict=passed`, `publicationGate=Bifrost`, and `privateStateExposed=false`. Wrapper `tools/epiphany_local_run.ps1 -Mode repo-livefire-closure -Workspace E:\Projects\Huginn -RepoWorkItem huginn-inspection-readback-livefire -SkipBuild` exposes the same read-only proof through the local operator mouth; artifact `local-20260621-190156-803-2f85ba15` printed commit, path, gate, blocker, Soul verdict, Mind commit receipt, and `privateStateExposed=False` without push, merge, publication, or target mutation. New wrapper `tools/epiphany_local_run.ps1 -Mode repo-work-public-proof -Workspace E:\Projects\Huginn -RepoWorkItem huginn-inspection-readback-livefire -SkipBuild` exports the redacted proof bundle and repo-local Verse index without publishing; artifact `local-20260621-190645-345-4555b1dc` wrote `.epiphany\public\proof-bundles\repo-work-public-proof-huginn-inspection-readback-livefire.json`, proof id `repo-work-public-proof-huginn-inspection-readback-livefire`, SHA-256 `857545b199f3e37eb06c2e757e5cfc201958e46667bb1f8403fd333a22620f63`, nine artifact rows, zero publication rows, and `privateStateExposed=False`. swarm overview readback `local-20260621-190701-140-b9519ec6` sees the `REPO-WORK-PUBLIC-PROOF` row and action row from Huginn's repo-local Verse with the same SHA-256 while Bifrost publication remains missing. Launcher compact modes are now named compact operator-context modes instead of read-only modes, and `bifrost-public-proof` summaries print item, target public Verse, room, proof SHA-256, review receipts, and credit receipts so publication receipts are auditable without claiming upstream sync. - next_action: Complete semantic projection physiology on the landed atomic admission, physically epoch-isolated projector, exact query gate, provider-status ownership cut, and non-authoritative CultMesh health sight. Mind and Modeling emit canonical obligations; live `gamecult.epiphany.main` projections are isolated in Qdrant by obligation, claim, and epoch; readiness authenticates the complete succeeded claim/attempt/receipt chain. CultMesh health publication reauthenticates the sealed canonical input and success chain, publishes bounded pending/failed/ready sight with provider incarnation through chronological CAS, refuses stale sealed inputs, and carries no readiness capability, private error, path, command, or payload prose. Idunn records stale observation, restart attempts, policy, and backoff without writing provider heartbeat/status; command success remains `awaiting-provider-heartbeat` until a causally newer provider heartbeat proves recovery. Next add typed Idunn executor grants and single-use recovery authorization, and add open-obligation discovery without letting a timer create demand. Package exactly one projector owner after resolving local-versus-Yggdrasil canonical-store placement; Yggdrasil currently has GPU Ollama/Odin/Idunn but no Qdrant or Epiphany target, while local Qdrant is healthy but overexposed on all interfaces. Timers, collection metadata, point counts, runtime ids, paths, CLI labels, Idunn liveness, CultMesh mirrors, Eve/Gjallar, and payload prose own no readiness. + next_action: Complete semantic projection physiology on the landed atomic admission, physically epoch-isolated projector, exact query gate, provider-status ownership cut, non-authoritative CultMesh health sight, and typed Idunn executor/recovery authority. Mind and Modeling alone emit canonical obligations. Idunn acquisition reauthenticates the sealed current source and CAS-writes its authority envelopes with a consumed grant plus claim/attempt bound to exact scope, obligation, executor incarnation, purpose, predecessor, and epoch; historical obligations cannot reopen advanced heads, and execute cannot become repair. Execution accepts the authenticated claim id rather than treating an executor label as capability. Recovery authenticates the exact Idunn poke intent/awaiting-heartbeat receipt and a newer provider-authored ready heartbeat that names that receipt as its startup cause, then one CAS consumes their store/input/claim-bound authorization, fails the abandoned attempt, and advances the claim epoch; recovery cannot execute or mint readiness. The projector alone owns claim-bound Qdrant mutation and terminal evidence, while the opaque query gate authenticates the complete succeeded grant-or-recovery/claim/attempt/receipt chain. Next add bounded open-obligation discovery for both Mind and Modeling without letting a timer create demand, then package exactly one projector owner after resolving local-versus-Yggdrasil canonical-store and Qdrant placement. Low-level mint/recovery are crate-private; public local-supervisor ports generate incarnations internally but remain a trusted in-process boundary, not authenticated OS IPC. Keep them inside one configured Idunn body until durable service identity owns them. Yggdrasil has GPU Ollama/Odin/Idunn but no Qdrant or Epiphany target; local Qdrant is healthy but overexposed on all interfaces. Timers, collection metadata, point counts, paths, CLI labels, Idunn liveness, CultMesh mirrors, Eve/swarm overview, and payload prose own no obligation, recovery, completion, or readiness.