Purify live semantic projector state

This commit is contained in:
Meta Vens 2026-07-15 10:55:30 +01:00
commit 2c9b904b6c
12 changed files with 657 additions and 21 deletions

View file

@ -2532,6 +2532,8 @@ fn agent_memory_cache(store_path: &Path) -> Result<CultCache> {
cache.register_entry_type::<crate::MemorySemanticProjectionClaim>()?;
cache.register_entry_type::<crate::MemorySemanticProjectionAttempt>()?;
cache.register_entry_type::<crate::MemorySemanticIndexReceipt>()?;
cache.register_entry_type::<crate::MemorySemanticProjectorExecutorGrant>()?;
cache.register_entry_type::<crate::MemorySemanticProjectorRecoveryAuthorization>()?;
cache.register_entry_type::<EpiphanyAgentMemoryEntry>()?;
cache.register_entry_type::<EpiphanyAgentStateSoaEntry>()?;
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(store_path));

View file

@ -31,8 +31,15 @@ use epiphany_core::load_epiphany_cultmesh_managed_service_policy;
use epiphany_core::load_epiphany_cultmesh_managed_service_policy_with_digest;
use epiphany_core::load_epiphany_cultmesh_status;
use epiphany_core::load_epiphany_cultmesh_swarm_brake;
use epiphany_core::load_latest_epiphany_cultmesh_daemon_heartbeat;
use epiphany_core::load_latest_epiphany_cultmesh_daemon_service_lifecycle_receipt_for_service;
use epiphany_core::migrate_memory_semantic_projection_attempts_v0;
use epiphany_core::observe_native_process as observe_process;
use epiphany_core::query_epiphany_local_verse_context;
use epiphany_core::retire_epiphany_cultmesh_operator_status_documents;
use epiphany_core::retire_memory_semantic_projection_claims_v0;
use epiphany_core::retire_orphaned_memory_semantic_projection_attempts_v0;
use epiphany_core::retire_unowned_memory_semantic_index_receipts;
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;
@ -66,6 +73,9 @@ fn main() -> Result<()> {
"tick" | "schedule" | "reconcile-all" => tick(args),
"serve" | "loop" | "daemon" => serve(args),
"managed-service-serve" | "service-desired-state-serve" => managed_service_serve(args),
"migrate-retired-operator-status" => migrate_retired_operator_status(args),
"migrate-semantic-attempts-v0" => migrate_semantic_attempts_v0(args),
"semantic-projector-service-status" => semantic_projector_service_status(args),
"service-plan" => service_plan(args),
"service-launch" | "launch-service" | "start-service" => service_launch(args),
"managed-service-policy" | "service-desired-state" => managed_service_policy(args),
@ -147,6 +157,96 @@ fn main() -> Result<()> {
}
}
fn migrate_retired_operator_status(args: Args) -> Result<()> {
let removed_keys = retire_epiphany_cultmesh_operator_status_documents(&args.store)?;
println!(
"{}",
serde_json::to_string_pretty(&json!({
"schemaVersion": "epiphany.cultmesh.retired_document_migration.v0",
"status": "completed",
"retiredDocumentType": "epiphany.cultmesh.operator_status",
"removedKeys": removed_keys,
"privateStateExposed": false,
}))?
);
Ok(())
}
fn migrate_semantic_attempts_v0(args: Args) -> Result<()> {
let agent_store = args
.agent_store
.as_ref()
.context("migrate-semantic-attempts-v0 requires --agent-store")?;
let runtime_store = args
.runtime_store
.as_ref()
.context("migrate-semantic-attempts-v0 requires --runtime-store")?;
if agent_store == runtime_store {
anyhow::bail!("semantic Mind and Modeling stores must remain distinct");
}
let retired_mind_claim_ids = retire_memory_semantic_projection_claims_v0(agent_store)?;
let retired_modeling_claim_ids = retire_memory_semantic_projection_claims_v0(runtime_store)?;
let retired_mind_attempt_ids =
retire_orphaned_memory_semantic_projection_attempts_v0(agent_store)?;
let retired_modeling_attempt_ids =
retire_orphaned_memory_semantic_projection_attempts_v0(runtime_store)?;
let retired_mind_receipt_ids = retire_unowned_memory_semantic_index_receipts(agent_store)?;
let retired_modeling_receipt_ids =
retire_unowned_memory_semantic_index_receipts(runtime_store)?;
let mind_attempt_ids = migrate_memory_semantic_projection_attempts_v0(agent_store)?;
let modeling_attempt_ids = migrate_memory_semantic_projection_attempts_v0(runtime_store)?;
println!(
"{}",
serde_json::to_string_pretty(&json!({
"schemaVersion": "epiphany.memory_semantic_projection_attempt_migration.v0",
"status": "completed",
"retiredMindClaimIds": retired_mind_claim_ids,
"retiredModelingClaimIds": retired_modeling_claim_ids,
"retiredMindAttemptIds": retired_mind_attempt_ids,
"retiredModelingAttemptIds": retired_modeling_attempt_ids,
"retiredMindReceiptIds": retired_mind_receipt_ids,
"retiredModelingReceiptIds": retired_modeling_receipt_ids,
"mindAttemptIds": mind_attempt_ids,
"modelingAttemptIds": modeling_attempt_ids,
"privateStateExposed": false,
}))?
);
Ok(())
}
fn semantic_projector_service_status(args: Args) -> Result<()> {
let receipt = load_latest_epiphany_cultmesh_daemon_service_lifecycle_receipt_for_service(
&args.store,
args.runtime_id.clone(),
SEMANTIC_PROJECTOR_SERVICE_ID,
)?
.context("semantic projector launch receipt is absent")?;
let heartbeat = load_latest_epiphany_cultmesh_daemon_heartbeat(
&args.store,
args.runtime_id,
SEMANTIC_PROJECTOR_EXECUTOR_ID,
)?
.context("semantic projector heartbeat is absent")?;
let correlation_matches = heartbeat.startup_lifecycle_receipt_id == receipt.receipt_id;
println!(
"{}",
serde_json::to_string_pretty(&json!({
"schemaVersion": "epiphany.memory_semantic_projector_service_status.v0",
"status": if correlation_matches && heartbeat.status == "ready" { "ready" } else { "degraded" },
"processId": receipt.process_id,
"launchReceiptId": receipt.receipt_id,
"executableSha256": receipt.executable_sha256,
"heartbeatId": heartbeat.heartbeat_id,
"providerIncarnation": heartbeat.provider_incarnation,
"heartbeatStatus": heartbeat.status,
"startupCorrelationMatches": correlation_matches,
"privateStateExposed": false,
"authoritative": false,
}))?
);
Ok(())
}
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)),
@ -389,13 +489,21 @@ fn service_launch(args: Args) -> Result<()> {
if args.wait_child {
anyhow::bail!("reserved semantic projector launch is an infinite managed child");
}
Some((policy.policy_id, digest, Uuid::new_v4().to_string()))
let executable_sha256 = local_file_sha256(&command_path.display().to_string())
.map(|digest| format!("sha256-{digest}"))
.context("reserved semantic projector executable cannot be fingerprinted")?;
Some((
policy.policy_id,
digest,
Uuid::new_v4().to_string(),
executable_sha256,
))
} else {
None
};
let mut command = Command::new(&command_path);
command.args(&service_args);
if let Some((_, _, receipt_id)) = &reserved_launch {
if let Some((_, _, receipt_id, _)) = &reserved_launch {
command.env("EPIPHANY_STARTUP_LIFECYCLE_RECEIPT_ID", receipt_id);
}
if let Some(stdout_path) = &args.stdout_artifact {
@ -454,12 +562,13 @@ fn service_launch(args: Args) -> Result<()> {
.as_ref()
.map(|path| path.display().to_string()),
);
if let Some((policy_id, policy_digest, receipt_id)) = reserved_launch {
if let Some((policy_id, policy_digest, receipt_id, executable_sha256)) = reserved_launch {
receipt.receipt_id = receipt_id.clone();
receipt.managed_policy_id = policy_id;
receipt.managed_policy_digest = policy_digest;
receipt.provider_daemon_id = SEMANTIC_PROJECTOR_EXECUTOR_ID.to_string();
receipt.startup_correlation_id = receipt_id;
receipt.executable_sha256 = executable_sha256;
}
let written = match write_epiphany_cultmesh_daemon_service_lifecycle_receipt(
&args.store,
@ -4131,6 +4240,9 @@ impl Args {
| "daemon"
| "managed-service-serve"
| "service-desired-state-serve"
| "migrate-retired-operator-status"
| "migrate-semantic-attempts-v0"
| "semantic-projector-service-status"
| "service-plan"
| "install-service"
| "service-launch"

View file

@ -1,15 +1,17 @@
use anyhow::{Context, Result, anyhow};
use epiphany_core::{
EPIPHANY_CULTMESH_DAEMON_HEARTBEAT_EVENT_SCHEMA_VERSION,
EpiphanyCultMeshDaemonHeartbeatEventEntry, MemorySemanticIndexConfig,
MemorySemanticProjectionInput, MemorySemanticProjectorPulseStatus,
EpiphanyCultMeshDaemonHeartbeatEventEntry, EpiphanyCultMeshDaemonServiceLifecycleReceiptEntry,
MemorySemanticIndexConfig, MemorySemanticProjectionInput, MemorySemanticProjectorPulseStatus,
SemanticProjectorServiceBody, authenticate_epiphany_cultmesh_semantic_projector_launch,
load_epiphany_cultmesh_daemon_service_lifecycle_receipt,
publish_epiphany_cultmesh_semantic_projection_health,
write_epiphany_cultmesh_daemon_heartbeat_event,
};
use serde_json::json;
use sha2::{Digest, Sha256};
use std::env;
use std::fs;
use std::path::PathBuf;
use std::thread;
use std::time::Duration;
@ -26,7 +28,27 @@ fn main() -> Result<()> {
"managed projector requires its startup launch receipt id"
));
}
authenticate_managed_launch(&args)?;
let receipt = authenticate_managed_launch(&args)?;
if receipt.process_id != Some(std::process::id()) {
return Err(anyhow!(
"managed projector process disagrees with its launch receipt"
));
}
let executable = env::current_exe().context("failed to resolve projector executable")?;
let executable_sha256 = format!(
"sha256-{:x}",
Sha256::digest(fs::read(&executable).with_context(|| {
format!(
"failed to fingerprint projector executable {}",
executable.display()
)
})?)
);
if receipt.executable_sha256 != executable_sha256 {
return Err(anyhow!(
"managed projector executable disagrees with its launch receipt"
));
}
}
let mut semantic_config = MemorySemanticIndexConfig::from_env();
semantic_config.qdrant_url = args.qdrant_url.clone();
@ -48,21 +70,21 @@ fn main() -> Result<()> {
.clone()
.or_else(|| outcome.inspections.last().map(|row| row.scope_id.clone()));
let mut health_faults = 0_u32;
let mut health_publication_faults = Vec::new();
for input in &inputs {
let store = source_store(&args, input)?;
if publish_epiphany_cultmesh_semantic_projection_health(
if let Err(error) = publish_epiphany_cultmesh_semantic_projection_health(
&args.local_verse_store,
args.runtime_id.clone(),
store,
input,
&provider_incarnation,
)
.is_err()
{
health_faults += 1;
) {
health_publication_faults
.push(format!("{}: {error:#}", input.obligation().partition));
}
}
let health_faults = health_publication_faults.len() as u32;
let degraded = source_faults > 0
|| health_faults > 0
|| matches!(
@ -98,6 +120,7 @@ fn main() -> Result<()> {
"inspectedSourceCount": outcome.inspections.len(),
"sourceFaultCount": source_faults,
"healthPublicationFaultCount": health_faults,
"healthPublicationFaults": health_publication_faults,
"selectedScopeId": outcome.selected_scope_id,
"privateStateExposed": false,
"authoritative": false,
@ -112,7 +135,9 @@ fn main() -> Result<()> {
Ok(())
}
fn authenticate_managed_launch(args: &Args) -> Result<()> {
fn authenticate_managed_launch(
args: &Args,
) -> Result<EpiphanyCultMeshDaemonServiceLifecycleReceiptEntry> {
let deadline = Instant::now() + Duration::from_secs(5);
loop {
if load_epiphany_cultmesh_daemon_service_lifecycle_receipt(
@ -122,12 +147,11 @@ fn authenticate_managed_launch(args: &Args) -> Result<()> {
)?
.is_some()
{
authenticate_epiphany_cultmesh_semantic_projector_launch(
return authenticate_epiphany_cultmesh_semantic_projector_launch(
&args.local_verse_store,
args.runtime_id.clone(),
&args.startup_lifecycle_receipt_id,
)?;
return Ok(());
);
}
if Instant::now() >= deadline {
return Err(anyhow!(

View file

@ -2675,6 +2675,26 @@ pub fn open_epiphany_cultmesh_node(
)
}
/// Removes the extinct ownerless operator-status projection from an existing
/// local Verse so the live schema catalog can open it again.
///
/// This is deliberately explicit migration, never implicit node bootstrap.
pub fn retire_epiphany_cultmesh_operator_status_documents(
store_path: impl AsRef<Path>,
) -> Result<Vec<String>> {
const RETIRED_TYPE: &str = "epiphany.cultmesh.operator_status";
let mut backing = SingleFileMessagePackBackingStore::new(store_path.as_ref());
let retired = backing
.pull_all()?
.into_iter()
.filter(|entry| entry.r#type == RETIRED_TYPE)
.collect::<Vec<_>>();
for entry in &retired {
backing.delete(entry)?;
}
Ok(retired.into_iter().map(|entry| entry.key).collect())
}
fn semantic_projection_health_scope_key(swarm_id: &str, partition: &str) -> String {
use sha2::{Digest, Sha256};
format!(
@ -4251,6 +4271,15 @@ pub fn load_latest_epiphany_cultmesh_daemon_service_lifecycle_receipt(
node.get(EPIPHANY_CULTMESH_DAEMON_SERVICE_LIFECYCLE_RECEIPT_LATEST_KEY)
}
pub fn load_latest_epiphany_cultmesh_daemon_service_lifecycle_receipt_for_service(
store_path: impl AsRef<Path>,
runtime_id: impl Into<String>,
service_id: &str,
) -> Result<Option<EpiphanyCultMeshDaemonServiceLifecycleReceiptEntry>> {
let node = open_epiphany_cultmesh_node(store_path, runtime_id)?;
node.get(&epiphany_cultmesh_daemon_service_lifecycle_receipt_latest_key(service_id))
}
pub fn load_epiphany_cultmesh_daemon_service_lifecycle_receipts(
store_path: impl AsRef<Path>,
runtime_id: impl Into<String>,
@ -4871,6 +4900,7 @@ fn validate_semantic_projector_launch_receipt(
|| receipt.process_id.is_none()
|| receipt.exit_code.is_some()
|| receipt.completed_at_utc.is_none()
|| !receipt.executable_sha256.starts_with("sha256-")
|| receipt.managed_policy_id.trim().is_empty()
|| !receipt.managed_policy_digest.starts_with("sha256-")
|| receipt.provider_daemon_id != "epiphany-memory-semantic-projector"
@ -8035,8 +8065,40 @@ pub fn write_epiphany_cultmesh_bifrost_contracts(
mod tests {
use super::*;
use cultcache_rs::CacheBackingStore;
use cultcache_rs::CultCacheEnvelope;
use pretty_assertions::assert_eq;
#[test]
fn explicit_migration_removes_only_retired_operator_status_documents() -> Result<()> {
let temp = tempfile::tempdir()?;
let store = temp.path().join("verse.ccmp");
let mut backing = SingleFileMessagePackBackingStore::new(&store);
backing.push(&CultCacheEnvelope {
key: "epiphany-local/operator-status/latest".into(),
r#type: "epiphany.cultmesh.operator_status".into(),
payload: vec![0x80],
stored_at: "2026-07-15T10:00:00Z".into(),
schema_id: Some("epiphany.cultmesh.operator_status".into()),
})?;
backing.push(&CultCacheEnvelope {
key: "epiphany-local/status".into(),
r#type: EPIPHANY_CULTMESH_STATUS_TYPE.into(),
payload: vec![0x80],
stored_at: "2026-07-15T10:00:01Z".into(),
schema_id: Some(EPIPHANY_CULTMESH_STATUS_TYPE.into()),
})?;
assert_eq!(
retire_epiphany_cultmesh_operator_status_documents(&store)?,
vec!["epiphany-local/operator-status/latest"]
);
let remaining = backing.pull_all()?;
assert_eq!(remaining.len(), 1);
assert_eq!(remaining[0].r#type, EPIPHANY_CULTMESH_STATUS_TYPE);
assert!(retire_epiphany_cultmesh_operator_status_documents(&store)?.is_empty());
Ok(())
}
#[test]
fn reserved_semantic_projector_policy_requires_specialized_exact_writer() -> Result<()> {
let temp = tempfile::tempdir()?;
@ -8128,7 +8190,7 @@ mod tests {
operator_artifact_ref: "service://semantic-projector/launch".into(),
private_state_exposed: false,
notes: vec![],
executable_sha256: String::new(),
executable_sha256: "sha256-test-projector".into(),
preflight_witness_id: String::new(),
required_document_types: vec![],
schema_preflight_passed: false,
@ -8716,7 +8778,7 @@ mod tests {
operator_artifact_ref: "artifact://service-lifecycle/first".to_string(),
private_state_exposed: false,
notes: Vec::new(),
executable_sha256: String::new(),
executable_sha256: "sha256-test-projector".into(),
preflight_witness_id: String::new(),
required_document_types: Vec::new(),
schema_preflight_passed: false,
@ -11546,7 +11608,7 @@ mod tests {
operator_artifact_ref: "service://semantic-projector/launch".into(),
private_state_exposed: false,
notes: vec![],
executable_sha256: String::new(),
executable_sha256: "sha256-test-projector".into(),
preflight_witness_id: String::new(),
required_document_types: vec![],
schema_preflight_passed: false,

View file

@ -472,6 +472,7 @@ 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;
pub use cultmesh_integration::load_latest_epiphany_cultmesh_daemon_service_lifecycle_receipt;
pub use cultmesh_integration::load_latest_epiphany_cultmesh_daemon_service_lifecycle_receipt_for_service;
pub use cultmesh_integration::load_latest_epiphany_cultmesh_daemon_tool_invocation_intent;
pub use cultmesh_integration::load_latest_epiphany_cultmesh_daemon_tool_invocation_receipt;
pub use cultmesh_integration::load_latest_epiphany_cultmesh_eve_connection_intent;
@ -495,6 +496,7 @@ pub use cultmesh_integration::open_epiphany_cultmesh_node;
pub use cultmesh_integration::publish_epiphany_cultmesh_provider_state;
pub use cultmesh_integration::publish_epiphany_cultmesh_semantic_projection_health;
pub use cultmesh_integration::query_epiphany_local_verse_context;
pub use cultmesh_integration::retire_epiphany_cultmesh_operator_status_documents;
pub use cultmesh_integration::seed_epiphany_local_verse_context;
pub use cultmesh_integration::write_epiphany_cultmesh_agent_state_soa_summary;
#[cfg(test)]
@ -711,11 +713,15 @@ pub use memory_graph::memory_graph_from_heartbeat_cognition;
pub use memory_graph::memory_graph_model_hash;
pub use memory_graph::memory_graph_node_id;
pub use memory_graph::memory_semantic_projection_query_eligible;
pub use memory_graph::migrate_memory_semantic_projection_attempts_v0;
pub use memory_graph::observe_memory_semantic_projection;
pub use memory_graph::plan_memory_graph_context_cut;
pub use memory_graph::plan_memory_graph_context_cut_with_ranked_ids;
pub use memory_graph::refresh_or_validate_repo_memory_graph;
pub use memory_graph::resolve_semantic_candidate;
pub use memory_graph::retire_memory_semantic_projection_claims_v0;
pub use memory_graph::retire_orphaned_memory_semantic_projection_attempts_v0;
pub use memory_graph::retire_unowned_memory_semantic_index_receipts;
pub use memory_graph::semantic_memory_context;
pub use memory_graph::semantic_point_id;
pub use memory_graph::validate_memory_graph_entry;

View file

@ -11,6 +11,7 @@ use cultcache_rs::{
SingleFileMessagePackBackingStore,
};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::path::Path;
@ -21,6 +22,231 @@ pub const MEMORY_SEMANTIC_PROJECTOR_EXECUTOR_GRANT_SCHEMA_VERSION: &str =
pub const MEMORY_SEMANTIC_PROJECTOR_RECOVERY_AUTHORIZATION_SCHEMA_VERSION: &str =
"gamecult.epiphany.memory_semantic_projector_recovery_authorization.v2";
#[derive(Deserialize, Serialize)]
struct LegacyMemorySemanticProjectionAttemptV0 {
schema_version: String,
attempt_id: String,
obligation_id: String,
started_at: String,
completed_at: Option<String>,
status: String,
error: Option<String>,
}
#[derive(Deserialize, Serialize)]
struct LegacyMemorySemanticProjectionClaimV0 {
schema_version: String,
scope_id: String,
claim_id: String,
obligation_id: String,
attempt_id: String,
executor_id: String,
epoch: u64,
status: String,
claimed_at: String,
completed_at: Option<String>,
}
/// Retires pre-Idunn claim/attempt pairs from active authority. Their old
/// receipts remain historical and query-ineligible; current obligations can
/// then receive fresh fenced work.
pub fn retire_memory_semantic_projection_claims_v0(
store_path: impl AsRef<Path>,
) -> Result<Vec<String>> {
const ATTEMPT_TYPE: &str = "gamecult.epiphany.memory_semantic_projection_attempt";
const CLAIM_TYPE: &str = "gamecult.epiphany.memory_semantic_projection_claim";
let backing = SingleFileMessagePackBackingStore::new(store_path.as_ref());
let opening = backing.pull_all()?;
let mut retired = Vec::new();
for claim_envelope in opening.iter().filter(|entry| entry.r#type == CLAIM_TYPE) {
if rmp_serde::from_slice::<MemorySemanticProjectionClaim>(&claim_envelope.payload).is_ok() {
continue;
}
let claim = rmp_serde::from_slice::<LegacyMemorySemanticProjectionClaimV0>(
&claim_envelope.payload,
)?;
if claim.schema_version != "gamecult.epiphany.memory_semantic_projection_claim.v0" {
return Err(anyhow!(
"unsupported legacy semantic claim schema for {:?}",
claim.claim_id
));
}
let attempt_envelope = opening
.iter()
.find(|entry| entry.r#type == ATTEMPT_TYPE && entry.key == claim.attempt_id)
.ok_or_else(|| {
anyhow!(
"legacy semantic claim {:?} has no exact attempt",
claim.claim_id
)
})?;
let attempt = rmp_serde::from_slice::<LegacyMemorySemanticProjectionAttemptV0>(
&attempt_envelope.payload,
)?;
if attempt.attempt_id != claim.attempt_id || attempt.obligation_id != claim.obligation_id {
return Err(anyhow!("legacy semantic claim/attempt pair disagrees"));
}
if !backing
.delete_batch_if_unchanged(&[claim_envelope.clone(), attempt_envelope.clone()])?
{
return Err(anyhow!(
"legacy semantic authority changed during explicit retirement"
));
}
retired.push(claim.claim_id);
}
Ok(retired)
}
/// Retires historical v0 attempts whose scope claim was already superseded or
/// overwritten before fenced claim history existed.
pub fn retire_orphaned_memory_semantic_projection_attempts_v0(
store_path: impl AsRef<Path>,
) -> Result<Vec<String>> {
const ATTEMPT_TYPE: &str = "gamecult.epiphany.memory_semantic_projection_attempt";
const CLAIM_TYPE: &str = "gamecult.epiphany.memory_semantic_projection_claim";
let backing = SingleFileMessagePackBackingStore::new(store_path.as_ref());
let opening = backing.pull_all()?;
let current_claim_attempt_ids = opening
.iter()
.filter(|entry| entry.r#type == CLAIM_TYPE)
.filter_map(|entry| {
rmp_serde::from_slice::<MemorySemanticProjectionClaim>(&entry.payload)
.ok()
.map(|claim| claim.attempt_id)
})
.collect::<std::collections::BTreeSet<_>>();
let mut retired = Vec::new();
for attempt_envelope in opening.iter().filter(|entry| entry.r#type == ATTEMPT_TYPE) {
if rmp_serde::from_slice::<MemorySemanticProjectionAttempt>(&attempt_envelope.payload)
.is_ok()
{
continue;
}
let attempt = rmp_serde::from_slice::<LegacyMemorySemanticProjectionAttemptV0>(
&attempt_envelope.payload,
)?;
if attempt.schema_version != "gamecult.epiphany.memory_semantic_projection_attempt.v0" {
return Err(anyhow!(
"unsupported orphaned semantic attempt schema for {:?}",
attempt.attempt_id
));
}
if current_claim_attempt_ids.contains(&attempt.attempt_id) {
continue;
}
if !backing.delete_batch_if_unchanged(std::slice::from_ref(attempt_envelope))? {
return Err(anyhow!(
"orphaned semantic attempt changed during explicit retirement"
));
}
retired.push(attempt.attempt_id);
}
Ok(retired)
}
/// Retires index receipts whose physical claim namespace is absent from the
/// canonical store. Their Qdrant points are rebuildable cache residue.
pub fn retire_unowned_memory_semantic_index_receipts(
store_path: impl AsRef<Path>,
) -> Result<Vec<String>> {
const RECEIPT_TYPE: &str = "gamecult.epiphany.memory_semantic_index_receipt";
const CLAIM_TYPE: &str = "gamecult.epiphany.memory_semantic_projection_claim";
let backing = SingleFileMessagePackBackingStore::new(store_path.as_ref());
let opening = backing.pull_all()?;
let claim_ids = opening
.iter()
.filter(|entry| entry.r#type == CLAIM_TYPE)
.filter_map(|entry| {
rmp_serde::from_slice::<MemorySemanticProjectionClaim>(&entry.payload)
.ok()
.map(|claim| claim.claim_id)
})
.collect::<std::collections::BTreeSet<_>>();
let mut retired = Vec::new();
for envelope in opening.iter().filter(|entry| entry.r#type == RECEIPT_TYPE) {
let receipt = rmp_serde::from_slice::<MemorySemanticIndexReceipt>(&envelope.payload)?;
if !receipt.claim_id.trim().is_empty()
&& receipt.claim_epoch > 0
&& claim_ids.contains(&receipt.claim_id)
{
continue;
}
if !backing.delete_batch_if_unchanged(std::slice::from_ref(envelope))? {
return Err(anyhow!(
"unowned semantic index receipt changed during explicit retirement"
));
}
retired.push(receipt.receipt_id);
}
Ok(retired)
}
/// Upgrades v0 attempt rows by binding them to the exact persisted claim that
/// already owns their attempt identity. No claim means no migration.
pub fn migrate_memory_semantic_projection_attempts_v0(
store_path: impl AsRef<Path>,
) -> Result<Vec<String>> {
const ATTEMPT_TYPE: &str = "gamecult.epiphany.memory_semantic_projection_attempt";
const CLAIM_TYPE: &str = "gamecult.epiphany.memory_semantic_projection_claim";
let backing = SingleFileMessagePackBackingStore::new(store_path.as_ref());
let opening = backing.pull_all()?;
let claims = opening
.iter()
.filter(|entry| entry.r#type == CLAIM_TYPE)
.map(|entry| rmp_serde::from_slice::<MemorySemanticProjectionClaim>(&entry.payload))
.collect::<std::result::Result<Vec<_>, _>>()?;
let mut migrated = Vec::new();
for envelope in opening.iter().filter(|entry| entry.r#type == ATTEMPT_TYPE) {
if rmp_serde::from_slice::<MemorySemanticProjectionAttempt>(&envelope.payload).is_ok() {
continue;
}
let legacy =
rmp_serde::from_slice::<LegacyMemorySemanticProjectionAttemptV0>(&envelope.payload)?;
if legacy.schema_version != "gamecult.epiphany.memory_semantic_projection_attempt.v0" {
return Err(anyhow!(
"unsupported legacy semantic attempt schema for {:?}",
legacy.attempt_id
));
}
let claim = claims
.iter()
.find(|claim| {
claim.attempt_id == legacy.attempt_id && claim.obligation_id == legacy.obligation_id
})
.ok_or_else(|| {
anyhow!(
"legacy semantic attempt {:?} has no exact owning claim",
legacy.attempt_id
)
})?;
let upgraded = MemorySemanticProjectionAttempt {
schema_version: MEMORY_SEMANTIC_PROJECTION_ATTEMPT_SCHEMA_VERSION.to_string(),
attempt_id: legacy.attempt_id,
obligation_id: legacy.obligation_id,
started_at: legacy.started_at,
completed_at: legacy.completed_at,
status: legacy.status,
error: legacy.error,
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(),
};
validate_memory_semantic_projection_attempt(&upgraded)?;
let mut replacement: CultCacheEnvelope = envelope.clone();
replacement.payload = rmp_serde::to_vec_named(&upgraded)?;
if !backing.compare_and_swap_entry(envelope, replacement)? {
return Err(anyhow!(
"semantic attempt changed during explicit migration"
));
}
migrated.push(upgraded.attempt_id);
}
Ok(migrated)
}
#[derive(Clone, Debug, PartialEq, DatabaseEntry)]
#[cultcache(
type = "gamecult.epiphany.memory_semantic_projector_executor_grant",
@ -73,6 +299,7 @@ mod authority_tests {
use crate::{
MEMORY_SEMANTIC_PROJECTION_OBLIGATION_SCHEMA_VERSION, SEMANTIC_PROJECTION_SCHEMA_VERSION,
};
use anyhow::Context;
use tempfile::tempdir;
fn obligation() -> MemorySemanticProjectionObligation {
@ -126,6 +353,164 @@ mod authority_tests {
})
}
#[test]
fn legacy_attempt_migration_binds_exact_owning_claim() -> Result<()> {
let temp = tempdir()?;
let store = temp.path().join("legacy-attempt.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",
"executor-a-incarnation",
"execute",
"idunn-incarnation-a",
"2026-07-15T04:01:00Z",
)?;
let backing = SingleFileMessagePackBackingStore::new(&store);
let opening = backing.pull_all()?;
let attempt_envelope = opening
.iter()
.find(|entry| {
entry.r#type == MemorySemanticProjectionAttempt::TYPE
&& entry.key == acquisition.claim.attempt_id
})
.context("attempt missing")?;
let attempt: MemorySemanticProjectionAttempt =
rmp_serde::from_slice(&attempt_envelope.payload)?;
let legacy = LegacyMemorySemanticProjectionAttemptV0 {
schema_version: "gamecult.epiphany.memory_semantic_projection_attempt.v0".into(),
attempt_id: attempt.attempt_id.clone(),
obligation_id: attempt.obligation_id.clone(),
started_at: attempt.started_at,
completed_at: attempt.completed_at,
status: attempt.status,
error: attempt.error,
};
let mut legacy_envelope = attempt_envelope.clone();
legacy_envelope.payload = rmp_serde::to_vec_named(&legacy)?;
assert!(backing.compare_and_swap_entry(attempt_envelope, legacy_envelope)?);
assert_eq!(
migrate_memory_semantic_projection_attempts_v0(&store)?,
vec![attempt.attempt_id.clone()]
);
let (_, _, _, upgraded) = load_running_claim(&store, &acquisition.claim.claim_id)?;
assert_eq!(upgraded.claim_id, acquisition.claim.claim_id);
assert_eq!(upgraded.claim_epoch, acquisition.claim.epoch);
assert_eq!(upgraded.executor_id, acquisition.claim.executor_id);
assert_eq!(
upgraded.executor_incarnation,
acquisition.claim.executor_incarnation
);
assert_eq!(upgraded.authority_id, acquisition.claim.authority_id);
assert!(migrate_memory_semantic_projection_attempts_v0(&store)?.is_empty());
Ok(())
}
#[test]
fn legacy_unfenced_claim_and_attempt_retire_atomically() -> Result<()> {
let temp = tempdir()?;
let store = temp.path().join("legacy-claim.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",
"executor-a-incarnation",
"execute",
"idunn-incarnation-a",
"2026-07-15T04:01:00Z",
)?;
let backing = SingleFileMessagePackBackingStore::new(&store);
let opening = backing.pull_all()?;
let claim_envelope = exact_envelope(
&opening,
MemorySemanticProjectionClaim::TYPE,
&acquisition.claim.scope_id,
)?;
let attempt_envelope = exact_envelope(
&opening,
MemorySemanticProjectionAttempt::TYPE,
&acquisition.claim.attempt_id,
)?;
let legacy_claim = LegacyMemorySemanticProjectionClaimV0 {
schema_version: "gamecult.epiphany.memory_semantic_projection_claim.v0".into(),
scope_id: acquisition.claim.scope_id.clone(),
claim_id: acquisition.claim.claim_id.clone(),
obligation_id: acquisition.claim.obligation_id.clone(),
attempt_id: acquisition.claim.attempt_id.clone(),
executor_id: acquisition.claim.executor_id.clone(),
epoch: acquisition.claim.epoch,
status: acquisition.claim.status.clone(),
claimed_at: acquisition.claim.claimed_at.clone(),
completed_at: acquisition.claim.completed_at.clone(),
};
let attempt: MemorySemanticProjectionAttempt =
rmp_serde::from_slice(&attempt_envelope.payload)?;
let legacy_attempt = LegacyMemorySemanticProjectionAttemptV0 {
schema_version: "gamecult.epiphany.memory_semantic_projection_attempt.v0".into(),
attempt_id: attempt.attempt_id,
obligation_id: attempt.obligation_id,
started_at: attempt.started_at,
completed_at: attempt.completed_at,
status: attempt.status,
error: attempt.error,
};
let mut legacy_claim_envelope = claim_envelope.clone();
legacy_claim_envelope.payload = rmp_serde::to_vec_named(&legacy_claim)?;
let mut legacy_attempt_envelope = attempt_envelope.clone();
legacy_attempt_envelope.payload = rmp_serde::to_vec_named(&legacy_attempt)?;
assert!(backing.compare_and_swap_batch(
&[claim_envelope, attempt_envelope],
vec![legacy_claim_envelope, legacy_attempt_envelope],
)?);
assert_eq!(
retire_memory_semantic_projection_claims_v0(&store)?,
vec![acquisition.claim.claim_id]
);
let observation = observe_memory_semantic_projection(&store, &projection_input)?;
assert_eq!(observation.status, "pending");
assert!(retire_memory_semantic_projection_claims_v0(&store)?.is_empty());
Ok(())
}
#[test]
fn orphaned_legacy_attempt_retires_without_fabricated_claim() -> Result<()> {
let temp = tempdir()?;
let store = temp.path().join("orphaned-attempt.msgpack");
let legacy = LegacyMemorySemanticProjectionAttemptV0 {
schema_version: "gamecult.epiphany.memory_semantic_projection_attempt.v0".into(),
attempt_id: "orphaned-attempt-1".into(),
obligation_id: "obligation-modeling-7".into(),
started_at: "2026-07-15T04:01:00Z".into(),
completed_at: Some("2026-07-15T04:02:00Z".into()),
status: "succeeded".into(),
error: None,
};
let mut backing = SingleFileMessagePackBackingStore::new(&store);
backing.push(&CultCacheEnvelope {
key: legacy.attempt_id.clone(),
r#type: MemorySemanticProjectionAttempt::TYPE.into(),
payload: rmp_serde::to_vec_named(&legacy)?,
stored_at: "2026-07-15T04:02:00Z".into(),
schema_id: Some(MemorySemanticProjectionAttempt::TYPE.into()),
})?;
assert_eq!(
retire_orphaned_memory_semantic_projection_attempts_v0(&store)?,
vec![legacy.attempt_id]
);
assert!(backing.pull_all()?.is_empty());
Ok(())
}
#[test]
fn concurrent_idunn_acquisition_has_one_winner_and_no_issued_litter() -> Result<()> {
let temp = tempdir()?;

View file

@ -846,6 +846,8 @@ pub fn runtime_spine_cache(store_path: impl AsRef<Path>) -> Result<CultCache> {
cache.register_entry_type::<crate::MemorySemanticProjectionClaim>()?;
cache.register_entry_type::<crate::MemorySemanticProjectionAttempt>()?;
cache.register_entry_type::<crate::MemorySemanticIndexReceipt>()?;
cache.register_entry_type::<crate::MemorySemanticProjectorExecutorGrant>()?;
cache.register_entry_type::<crate::MemorySemanticProjectorRecoveryAuthorization>()?;
cache.register_entry_type::<EpiphanyRuntimeSession>()?;
cache.register_entry_type::<EpiphanyRuntimeJob>()?;
cache.register_entry_type::<EpiphanyRuntimeWorkerLaunchRequest>()?;