From 01602fd301137869050a81d441368bac2de7da9b Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 18 Aug 2026 06:18:11 +0200 Subject: [PATCH] Refuse historical writable Mind epochs --- epiphany-core/src/mind_documents.rs | 2 +- epiphany-core/src/runtime_spine.rs | 90 ++++++++++++++++++- .../src/workspace_coverage_store_binding.rs | 16 +++- 3 files changed, 102 insertions(+), 6 deletions(-) diff --git a/epiphany-core/src/mind_documents.rs b/epiphany-core/src/mind_documents.rs index a55697a4..ee8d472d 100644 --- a/epiphany-core/src/mind_documents.rs +++ b/epiphany-core/src/mind_documents.rs @@ -13,7 +13,7 @@ use sha2::{Digest, Sha256}; use crate::{EpiphanyMindDocumentVersion, runtime_spine_cache}; -pub const MIND_SCHEMA_EPOCH: &str = "epiphany.mind.epoch.v1"; +pub const MIND_SCHEMA_EPOCH: &str = "epiphany.mind.epoch.v2"; pub const MIND_OBJECTIVE_KEY: &str = "objective"; pub const MIND_FOCUS_KEY: &str = "focus"; pub const MIND_MODE_KEY: &str = "mode"; diff --git a/epiphany-core/src/runtime_spine.rs b/epiphany-core/src/runtime_spine.rs index c1d0cca4..be65306f 100644 --- a/epiphany-core/src/runtime_spine.rs +++ b/epiphany-core/src/runtime_spine.rs @@ -173,7 +173,7 @@ pub const TOOL_INVOCATION_RECEIPT_TYPE: &str = "epiphany.tool_invocation_receipt pub const RUNTIME_IDENTITY_KEY: &str = "self"; pub const RUNTIME_SWARM_BINDING_KEY: &str = "runtime-swarm-binding"; pub const RUNTIME_SWARM_BINDING_SCHEMA_VERSION: &str = "epiphany.runtime.swarm_binding.v0"; -pub const RUNTIME_SPINE_SCHEMA_VERSION: &str = "epiphany.runtime_spine.v0"; +pub const RUNTIME_SPINE_SCHEMA_VERSION: &str = "epiphany.runtime_spine.v1"; pub const EPIPHANY_RUNTIME_ROOT_SESSION_ID: &str = "epiphany-main"; pub const RUNTIME_MODEL_EXECUTION_BINDING_SCHEMA_VERSION: &str = "epiphany.runtime.model_execution_binding.v0"; @@ -1017,6 +1017,8 @@ pub struct EpiphanyRuntimeJobSnapshot { pub fn runtime_spine_cache(store_path: impl AsRef) -> Result { let store_path = store_path.as_ref(); + let backing_store = runtime_spine_backing_store(store_path)?; + validate_runtime_store_epoch(&backing_store.pull_all()?)?; let mut cache = CultCache::new(); crate::mind_documents::register_mind_document_types(&mut cache)?; cache.register_entry_type::()?; @@ -1114,10 +1116,49 @@ pub fn runtime_spine_cache(store_path: impl AsRef) -> Result { cache.register_entry_type::()?; cache.register_entry_type::()?; cache.register_entry_type::()?; - cache.add_generic_backing_store(runtime_spine_backing_store(store_path)?); + cache.add_generic_backing_store(backing_store); Ok(cache) } +fn validate_runtime_store_epoch(envelopes: &[CultCacheEnvelope]) -> Result<()> { + if envelopes.is_empty() { + return Ok(()); + } + let mind_identities = envelopes + .iter() + .filter(|envelope| envelope.r#type == crate::EpiphanyMindIdentity::TYPE) + .collect::>(); + let runtime_identities = envelopes + .iter() + .filter(|envelope| envelope.r#type == EpiphanyRuntimeIdentity::TYPE) + .collect::>(); + if mind_identities.is_empty() && runtime_identities.is_empty() { + return Ok(()); + } + if mind_identities.len() != 1 || runtime_identities.len() != 1 { + return Err(anyhow!( + "claimed runtime Mind store does not have one current schema identity pair" + )); + } + let mind_envelope = mind_identities[0]; + let runtime_envelope = runtime_identities[0]; + let mind: crate::EpiphanyMindIdentity = rmp_serde::from_slice(&mind_envelope.payload) + .map_err(|error| anyhow!("runtime Mind schema identity is invalid: {error}"))?; + let runtime: EpiphanyRuntimeIdentity = rmp_serde::from_slice(&runtime_envelope.payload) + .map_err(|error| anyhow!("runtime schema identity is invalid: {error}"))?; + if mind_envelope.key != crate::MIND_SCHEMA_EPOCH + || mind.schema_epoch != crate::MIND_SCHEMA_EPOCH + || runtime_envelope.key != RUNTIME_IDENTITY_KEY + || runtime.schema_version != RUNTIME_SPINE_SCHEMA_VERSION + || mind.runtime_id != runtime.runtime_id + { + return Err(anyhow!( + "nonempty runtime Mind store belongs to an unsupported writable schema epoch" + )); + } + Ok(()) +} + pub fn initialize_runtime_spine( store_path: impl AsRef, options: RuntimeSpineInitOptions, @@ -13797,4 +13838,49 @@ pub(crate) mod tests { ); Ok(()) } + + #[test] + fn current_runtime_refuses_old_writable_epoch_without_mutation() -> Result<()> { + let temp = tempfile::tempdir()?; + let store = temp.path().join("historical.cc"); + let mut historical = CultCache::new(); + historical.register_entry_type::()?; + historical.register_entry_type::()?; + historical.add_generic_backing_store(runtime_spine_backing_store(&store)?); + historical.put( + "epiphany.mind.epoch.v1", + &crate::EpiphanyMindIdentity { + schema_epoch: "epiphany.mind.epoch.v1".into(), + runtime_id: "historical-runtime".into(), + }, + )?; + historical.put( + RUNTIME_IDENTITY_KEY, + &EpiphanyRuntimeIdentity { + schema_version: "epiphany.runtime_spine.v0".into(), + runtime_id: "historical-runtime".into(), + display_name: "Historical runtime".into(), + runtime_kind: "epiphany.native".into(), + created_at: "2026-08-17T00:00:00Z".into(), + updated_at: "2026-08-17T00:00:00Z".into(), + supported_document_types: Vec::new(), + metadata: BTreeMap::new(), + }, + )?; + let before = runtime_spine_backing_store(&store)?.pull_all()?; + assert!(runtime_spine_cache(&store).is_err()); + assert!( + initialize_runtime_spine( + &store, + RuntimeSpineInitOptions { + runtime_id: "historical-runtime".into(), + display_name: "Must not migrate".into(), + created_at: "2026-08-18T00:00:00Z".into(), + }, + ) + .is_err() + ); + assert_eq!(runtime_spine_backing_store(&store)?.pull_all()?, before); + Ok(()) + } } diff --git a/epiphany-core/src/workspace_coverage_store_binding.rs b/epiphany-core/src/workspace_coverage_store_binding.rs index 780b8ab6..5f7a96b3 100644 --- a/epiphany-core/src/workspace_coverage_store_binding.rs +++ b/epiphany-core/src/workspace_coverage_store_binding.rs @@ -488,9 +488,10 @@ fn workspace_coverage_binding_id( mod tests { use super::*; use crate::{ - BODY_SCHEMA_VERSION, EpiphanyRuntimeIdentity, EpiphanyRuntimeSwarmBinding, - RUNTIME_IDENTITY_KEY, RUNTIME_IDENTITY_TYPE, RUNTIME_SPINE_SCHEMA_VERSION, - RUNTIME_SWARM_BINDING_KEY, RUNTIME_SWARM_BINDING_SCHEMA_VERSION, + BODY_SCHEMA_VERSION, EpiphanyMindIdentity, EpiphanyRuntimeIdentity, + EpiphanyRuntimeSwarmBinding, MIND_SCHEMA_EPOCH, RUNTIME_IDENTITY_KEY, + RUNTIME_IDENTITY_TYPE, RUNTIME_SPINE_SCHEMA_VERSION, RUNTIME_SWARM_BINDING_KEY, + RUNTIME_SWARM_BINDING_SCHEMA_VERSION, }; use std::collections::BTreeMap; @@ -565,6 +566,15 @@ mod tests { runtime_backing.push_all( &[ envelope(RUNTIME_IDENTITY_TYPE, RUNTIME_IDENTITY_KEY, &identity, NOW)?, + envelope( + EpiphanyMindIdentity::TYPE, + MIND_SCHEMA_EPOCH, + &EpiphanyMindIdentity { + schema_epoch: MIND_SCHEMA_EPOCH.into(), + runtime_id: identity.runtime_id.clone(), + }, + NOW, + )?, envelope( EpiphanyRuntimeSwarmBinding::TYPE, RUNTIME_SWARM_BINDING_KEY,