Keep resident brakes on consequence surfaces

This commit is contained in:
Meta Vens 2026-07-19 00:45:18 +01:00
commit 86d702002a
6 changed files with 233 additions and 27 deletions

View file

@ -101,9 +101,10 @@ pub fn commit_request(
let identity = cache
.get::<crate::EpiphanyRuntimeIdentity>(crate::RUNTIME_IDENTITY_KEY)?
.ok_or_else(|| anyhow!("model direction consideration requires runtime identity"))?;
let thread = cache
.get::<crate::EpiphanyThreadStateEntry>(crate::THREAD_STATE_KEY)?
.ok_or_else(|| anyhow!("model direction consideration requires thread state"))?;
let Some(thread) = cache.get::<crate::EpiphanyThreadStateEntry>(crate::THREAD_STATE_KEY)?
else {
return Ok(None);
};
let model = crate::runtime_current_repo_model(runtime_store)?
.ok_or_else(|| anyhow!("model direction consideration requires admitted Modeling map"))?;
let model_hash = crate::memory_graph_model_hash(&model)?;
@ -251,6 +252,7 @@ pub fn render_prompt(request: &AdmittedModelDirectionConsiderationRequest) -> St
#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeMap;
fn request() -> AdmittedModelDirectionConsiderationRequest {
AdmittedModelDirectionConsiderationRequest {
@ -322,4 +324,34 @@ mod tests {
});
assert!(validate_result(&request, &proposed).is_ok());
}
#[test]
fn cold_runtime_without_thread_has_no_direction_request_yet() -> Result<()> {
let temp = tempfile::tempdir()?;
let store = temp.path().join("runtime.ccmp");
let mut cache = crate::runtime_spine_cache(&store)?;
cache.put(
crate::RUNTIME_IDENTITY_KEY,
&crate::EpiphanyRuntimeIdentity {
schema_version: crate::RUNTIME_SPINE_SCHEMA_VERSION.into(),
runtime_id: "runtime-cold".into(),
display_name: "Cold runtime".into(),
runtime_kind: "resident".into(),
created_at: "2026-07-18T00:00:00Z".into(),
updated_at: "2026-07-18T00:00:00Z".into(),
supported_document_types: Vec::new(),
metadata: BTreeMap::new(),
},
)?;
assert!(commit_request(&store, "2026-07-18T00:01:00Z")?.is_none());
let mut reloaded = crate::runtime_spine_cache(&store)?;
reloaded.pull_all_backing_stores()?;
assert!(
reloaded
.get_all::<AdmittedModelDirectionConsiderationRequest>()?
.is_empty()
);
Ok(())
}
}

View file

@ -4020,10 +4020,7 @@ fn assert_swarm_brake_allows_service_lifecycle_entry(
let scope_matches = matches!(brake.scope.as_str(), "swarm" | "all");
let surface_matches = brake.protected_surfaces.is_empty()
|| brake.protected_surfaces.iter().any(|surface| {
surface == "heartbeat.scheduler"
|| surface == "daemon.lifecycle_poke"
|| surface == "daemon.*"
|| surface == "*"
surface == "daemon.lifecycle_poke" || surface == "daemon.*" || surface == "*"
});
if scope_matches && surface_matches {
anyhow::bail!(
@ -4099,6 +4096,44 @@ mod provider_status_ownership_tests {
}
}
#[cfg(test)]
mod service_lifecycle_brake_authority_tests {
use super::*;
fn brake(protected_surfaces: &[&str]) -> EpiphanyCultMeshSwarmBrakeEntry {
EpiphanyCultMeshSwarmBrakeEntry {
schema_version: "epiphany.cultmesh.swarm_brake.v0".into(),
brake_id: "brake-test".into(),
status: "engaged".into(),
scope: "all".into(),
reason: "sleep".into(),
operator_agent_id: "operator".into(),
affected_clusters: vec!["local".into()],
protected_surfaces: protected_surfaces.iter().map(|v| (*v).into()).collect(),
created_at_utc: "2026-07-18T00:00:00Z".into(),
expires_at_utc: None,
private_state_exposed: false,
notes: Vec::new(),
runtime_id: "runtime".into(),
}
}
#[test]
fn cognitive_scheduler_brake_does_not_claim_service_physiology() {
let cognitive = brake(&[
"heartbeat.scheduler",
"coordinator.run",
"persona.public_speech",
"daemon.tool_invocation",
]);
assert!(assert_swarm_brake_allows_service_lifecycle_entry(Some(&cognitive)).is_ok());
for surface in ["daemon.lifecycle_poke", "daemon.*", "*"] {
let lifecycle = brake(&[surface]);
assert!(assert_swarm_brake_allows_service_lifecycle_entry(Some(&lifecycle)).is_err());
}
}
}
#[cfg(test)]
mod semantic_projector_authority_tests {
use super::*;

View file

@ -18,8 +18,9 @@ use super::participant_kind;
use anyhow::Result;
use anyhow::anyhow;
use cultcache_rs::SingleFileMessagePackBackingStore;
use cultcache_rs::{CultCache, CultCacheEnvelope, DatabaseEntry};
use cultcache_rs::{CacheBackingStore, CultCache, CultCacheEnvelope, DatabaseEntry};
use serde_json::Value;
use std::collections::HashSet;
use std::path::Path;
pub fn heartbeat_state_cache(store_path: impl AsRef<Path>) -> Result<CultCache> {
@ -27,8 +28,7 @@ pub fn heartbeat_state_cache(store_path: impl AsRef<Path>) -> Result<CultCache>
cache.register_entry_type::<EpiphanyHeartbeatStateEntry>()?;
cache.register_entry_type::<EpiphanyHeartbeatCognitionEntry>()?;
cache.register_entry_type::<EpiphanyHeartbeatStaleTurnRepairReceipt>()?;
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(store_path.as_ref()));
cache.pull_all_backing_stores()?;
import_owned_heartbeat_envelopes(&mut cache, store_path.as_ref(), false)?;
Ok(cache)
}
@ -37,11 +37,77 @@ fn legacy_heartbeat_state_cache(store_path: impl AsRef<Path>) -> Result<CultCach
cache.register_entry_type::<LegacyHeartbeatStateWithCognition>()?;
cache.register_entry_type::<EpiphanyHeartbeatCognitionEntry>()?;
cache.register_entry_type::<EpiphanyHeartbeatStaleTurnRepairReceipt>()?;
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(store_path.as_ref()));
cache.pull_all_backing_stores()?;
import_owned_heartbeat_envelopes(&mut cache, store_path.as_ref(), true)?;
Ok(cache)
}
fn import_owned_heartbeat_envelopes(
cache: &mut CultCache,
store_path: &Path,
legacy: bool,
) -> Result<()> {
let state_type = if legacy {
<LegacyHeartbeatStateWithCognition as DatabaseEntry>::TYPE
} else {
<EpiphanyHeartbeatStateEntry as DatabaseEntry>::TYPE
};
let mut identities = HashSet::new();
for envelope in SingleFileMessagePackBackingStore::new(store_path).pull_all()? {
let owned = (envelope.r#type == state_type && envelope.key == HEARTBEAT_STATE_KEY)
|| (envelope.r#type == <EpiphanyHeartbeatCognitionEntry as DatabaseEntry>::TYPE
&& envelope.key == HEARTBEAT_COGNITION_KEY)
|| envelope.r#type == <EpiphanyHeartbeatStaleTurnRepairReceipt as DatabaseEntry>::TYPE;
if !owned {
continue;
}
if !identities.insert((envelope.r#type.clone(), envelope.key.clone())) {
return Err(anyhow!(
"heartbeat store contains duplicate owner entry type {:?} key {:?}",
envelope.r#type,
envelope.key
));
}
if envelope.r#type == state_type {
if legacy {
// The legacy and current state documents intentionally share a
// polymorphic identity. A current payload is not legacy cargo.
let _ = cache.load_envelope::<LegacyHeartbeatStateWithCognition>(envelope);
} else {
cache.load_envelope::<EpiphanyHeartbeatStateEntry>(envelope)?;
}
} else if envelope.r#type == <EpiphanyHeartbeatCognitionEntry as DatabaseEntry>::TYPE {
cache.load_envelope::<EpiphanyHeartbeatCognitionEntry>(envelope)?;
} else {
cache.load_envelope::<EpiphanyHeartbeatStaleTurnRepairReceipt>(envelope)?;
}
}
Ok(())
}
fn commit_owned_entry<T: DatabaseEntry>(
store_path: &Path,
cache: &CultCache,
key: &str,
value: &T,
) -> Result<T> {
let expected = cache
.snapshot_envelopes()
.into_iter()
.find(|entry| entry.r#type == T::TYPE && entry.key == key);
let (replacement, written) = cache.prepare_entry(key, value)?;
let backing = SingleFileMessagePackBackingStore::new(store_path);
let committed = match expected {
Some(expected) => backing.compare_and_swap_entry(&expected, replacement)?,
None => backing.insert_entry_if_absent(replacement)?,
};
if !committed {
return Err(anyhow!(
"heartbeat owner entry lost exact atomic compare-and-swap"
));
}
Ok(written)
}
pub fn load_heartbeat_state_entry(
store_path: impl AsRef<Path>,
) -> Result<Option<EpiphanyHeartbeatStateEntry>> {
@ -54,8 +120,9 @@ pub fn write_heartbeat_state_entry(
state: &EpiphanyHeartbeatStateEntry,
) -> Result<EpiphanyHeartbeatStateEntry> {
validate_heartbeat_state(state)?;
let mut cache = heartbeat_state_cache(store_path)?;
cache.put(HEARTBEAT_STATE_KEY, state)
let store_path = store_path.as_ref();
let cache = heartbeat_state_cache(store_path)?;
commit_owned_entry(store_path, &cache, HEARTBEAT_STATE_KEY, state)
}
pub fn load_heartbeat_state_transaction(
@ -119,8 +186,9 @@ pub fn write_heartbeat_cognition_entry(
cognition: &EpiphanyHeartbeatCognitionEntry,
) -> Result<EpiphanyHeartbeatCognitionEntry> {
validate_heartbeat_cognition(cognition)?;
let mut cache = heartbeat_state_cache(store_path)?;
cache.put(HEARTBEAT_COGNITION_KEY, cognition)
let store_path = store_path.as_ref();
let cache = heartbeat_state_cache(store_path)?;
commit_owned_entry(store_path, &cache, HEARTBEAT_COGNITION_KEY, cognition)
}
pub fn write_heartbeat_stale_turn_repair_receipt(
@ -128,9 +196,27 @@ pub fn write_heartbeat_stale_turn_repair_receipt(
receipt: &EpiphanyHeartbeatStaleTurnRepairReceipt,
) -> Result<EpiphanyHeartbeatStaleTurnRepairReceipt> {
validate_heartbeat_stale_turn_repair_receipt(receipt)?;
let mut cache = heartbeat_state_cache(store_path)?;
let written = cache.put(receipt.receipt_id.clone(), receipt)?;
cache.put(HEARTBEAT_STALE_TURN_REPAIR_LATEST_KEY, &written)?;
let store_path = store_path.as_ref();
let cache = heartbeat_state_cache(store_path)?;
let written = receipt.clone();
let (receipt_entry, _) = cache.prepare_entry(&receipt.receipt_id, receipt)?;
let (latest_entry, _) = cache.prepare_entry(HEARTBEAT_STALE_TURN_REPAIR_LATEST_KEY, receipt)?;
let expected = cache
.snapshot_envelopes()
.into_iter()
.filter(|entry| {
entry.r#type == <EpiphanyHeartbeatStaleTurnRepairReceipt as DatabaseEntry>::TYPE
&& (entry.key == receipt.receipt_id
|| entry.key == HEARTBEAT_STALE_TURN_REPAIR_LATEST_KEY)
})
.collect::<Vec<_>>();
if !SingleFileMessagePackBackingStore::new(store_path)
.compare_and_swap_batch(&expected, vec![receipt_entry, latest_entry])?
{
return Err(anyhow!(
"heartbeat stale-turn receipt lost exact atomic compare-and-swap"
));
}
Ok(written)
}
@ -333,6 +419,13 @@ mod tests {
use crate::heartbeat_state::default_heartbeat_state;
use pretty_assertions::assert_eq;
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
#[cultcache(type = "test.foreign.readiness", schema = "ForeignReadiness")]
struct ForeignReadiness {
#[cultcache(key = 0)]
status: String,
}
#[test]
fn round_trips_state_and_cognition_documents() -> Result<()> {
let temp = tempfile::tempdir()?;
@ -373,4 +466,31 @@ mod tests {
assert_eq!(loaded_cognition.latest_run_id.as_deref(), Some("run-1"));
Ok(())
}
#[test]
fn heartbeat_owner_reads_and_writes_preserve_foreign_shared_store_rows() -> Result<()> {
let temp = tempfile::tempdir()?;
let store_path = temp.path().join("heartbeats.msgpack");
let mut foreign_cache = CultCache::new();
foreign_cache.register_entry_type::<ForeignReadiness>()?;
let (foreign, _) = foreign_cache.prepare_entry(
"provider-readiness",
&ForeignReadiness {
status: "ready".into(),
},
)?;
let mut backing = SingleFileMessagePackBackingStore::new(&store_path);
backing.push(&foreign)?;
assert!(load_heartbeat_state_entry(&store_path)?.is_none());
write_heartbeat_state_entry(&store_path, &default_heartbeat_state(1.0))?;
let rows = backing.pull_all()?;
assert!(rows.contains(&foreign));
assert!(rows.iter().any(|row| {
row.r#type == <EpiphanyHeartbeatStateEntry as DatabaseEntry>::TYPE
&& row.key == HEARTBEAT_STATE_KEY
}));
Ok(())
}
}