Bind Modeling memory to live source truth

This commit is contained in:
Meta Vens 2026-07-13 18:51:38 +01:00
commit fcbab316e6
19 changed files with 583 additions and 76 deletions

View file

@ -27,6 +27,7 @@ use epiphany_core::memory_graph_from_epiphany_graphs;
use epiphany_core::memory_graph_from_heartbeat_cognition;
use epiphany_core::memory_graph_node_id;
use epiphany_core::plan_memory_graph_context_cut;
use epiphany_core::refresh_or_validate_repo_memory_graph;
use epiphany_core::validate_memory_graph_snapshot;
use epiphany_core::write_memory_graph_snapshot;
use std::env;
@ -291,6 +292,41 @@ fn read_refresh_args(args: impl Iterator<Item = String>) -> Result<RefreshArgs>
}
fn refresh_memory_graph_store(args: RefreshArgs) -> Result<serde_json::Value> {
if args.sources.is_empty()
&& args.agent_store.is_none()
&& args.heartbeat_store.is_none()
&& let Some(thread_state_store) = &args.thread_state_store
{
let state = load_thread_state(thread_state_store)?.ok_or_else(|| {
anyhow!(
"thread state store {} is missing",
thread_state_store.display()
)
})?;
let repo_root = thread_state_store
.parent()
.and_then(std::path::Path::parent)
.unwrap_or_else(|| std::path::Path::new("."));
let source_identity = thread_state_store
.canonicalize()
.unwrap_or_else(|_| thread_state_store.clone())
.to_string_lossy()
.into_owned();
let (snapshot, refresh) = refresh_or_validate_repo_memory_graph(
&args.store,
&source_identity,
&state,
repo_root,
args.graph_id,
)?;
return Ok(serde_json::json!({
"ok": true, "store": args.store, "graphId": snapshot.graph_id,
"source": snapshot.source, "refresh": format!("{refresh:?}").to_lowercase(),
"domains": snapshot.domains.len(), "nodes": snapshot.nodes.len(),
"edges": snapshot.edges.len(), "summaries": snapshot.summaries.len(),
"freshness": snapshot.freshness,
}));
}
let mut snapshots = Vec::new();
let mut source_status = Vec::new();
@ -341,7 +377,22 @@ fn refresh_memory_graph_store(args: RefreshArgs) -> Result<serde_json::Value> {
thread_state_store.display()
)
})?;
let snapshot = memory_graph_from_epiphany_graphs("repo-profile", &state.graphs);
let repo_root = thread_state_store
.parent()
.and_then(std::path::Path::parent)
.unwrap_or_else(|| std::path::Path::new("."));
let source_identity = thread_state_store
.canonicalize()
.unwrap_or_else(|_| thread_state_store.clone())
.to_string_lossy()
.into_owned();
let snapshot = memory_graph_from_epiphany_graphs(
"repo-profile",
&state.graphs,
source_identity,
state.revision,
repo_root,
)?;
source_status.push(serde_json::json!({
"kind": "thread_state_store",
"source": thread_state_store,

View file

@ -5,21 +5,20 @@ use crate::EpiphanyMemoryContextPacket;
use crate::EpiphanyMemoryContextQuery;
use crate::EpiphanyMemoryProfile;
use crate::EpiphanyPromptContextInput;
use crate::RepoMemoryGraphRefresh;
use crate::RuntimeHandsReceiptChainSummary;
use crate::load_epiphany_cultmesh_cluster_topology;
use crate::load_epiphany_cultmesh_status;
use crate::load_latest_epiphany_cultmesh_work_loop_telemetry;
use crate::load_memory_graph_snapshot;
use crate::memory_graph_from_epiphany_graphs;
use crate::plan_memory_graph_context_cut;
use crate::query_epiphany_local_verse_context;
use crate::refresh_or_validate_repo_memory_graph;
use crate::render_epiphany_prompt_context;
use crate::runtime_hands_command_receipt;
use crate::runtime_hands_commit_receipt;
use crate::runtime_hands_patch_receipt;
use crate::runtime_latest_hands_receipt_chain_after;
use crate::write_epiphany_cultmesh_work_loop_telemetry;
use crate::write_memory_graph_snapshot;
use chrono::SecondsFormat;
use chrono::Utc;
use epiphany_state_model::EpiphanyAcceptanceReceipt;
@ -632,28 +631,27 @@ fn launch_memory_context(
focus: &str,
) -> Result<EpiphanyMemoryContextPacket, String> {
let memory_graph_store = memory_graph_store_path(runtime_store_path);
let (snapshot, refreshed_from_state) = match load_memory_graph_snapshot(&memory_graph_store)
.map_err(|error| {
format!(
"failed to load memory graph store {}: {error}",
memory_graph_store.display()
)
})? {
Some(snapshot) => (snapshot, false),
None => {
let snapshot = memory_graph_from_epiphany_graphs(
format!("bridge-launch-state-rev-{}", state.revision),
&state.graphs,
);
write_memory_graph_snapshot(&memory_graph_store, &snapshot).map_err(|error| {
format!(
"failed to write memory graph store {} from thread state: {error}",
memory_graph_store.display()
)
})?;
(snapshot, true)
}
};
let repo_root = runtime_store_path
.parent()
.and_then(Path::parent)
.unwrap_or_else(|| Path::new("."));
let source_identity = fs::canonicalize(runtime_store_path)
.unwrap_or_else(|_| runtime_store_path.to_path_buf())
.to_string_lossy()
.into_owned();
let (snapshot, refresh) = refresh_or_validate_repo_memory_graph(
&memory_graph_store,
&source_identity,
state,
repo_root,
format!("bridge-launch-state-rev-{}", state.revision),
)
.map_err(|error| {
format!(
"failed to refresh memory graph store {}: {error}",
memory_graph_store.display()
)
})?;
let mut packet = plan_memory_graph_context_cut(
&snapshot,
@ -665,7 +663,7 @@ fn launch_memory_context(
..Default::default()
},
);
if refreshed_from_state {
if refresh == RepoMemoryGraphRefresh::Refreshed {
packet.warnings.push(format!(
"Memory graph store was refreshed from current thread-state repo graph at {}.",
memory_graph_store.display()

View file

@ -617,6 +617,7 @@ pub use memory_graph::EpiphanyMemoryPatchCandidate;
pub use memory_graph::MEMORY_GRAPH_KEY;
pub use memory_graph::MEMORY_GRAPH_SCHEMA_VERSION;
pub use memory_graph::MEMORY_GRAPH_TYPE;
pub use memory_graph::RepoMemoryGraphRefresh;
pub use memory_graph::compose_memory_graph_snapshots;
pub use memory_graph::derive_memory_graph_freshness;
pub use memory_graph::lifecycle_allowed_for_profile;
@ -630,6 +631,7 @@ pub use memory_graph::memory_graph_from_epiphany_graphs;
pub use memory_graph::memory_graph_from_heartbeat_cognition;
pub use memory_graph::memory_graph_node_id;
pub use memory_graph::plan_memory_graph_context_cut;
pub use memory_graph::refresh_or_validate_repo_memory_graph;
pub use memory_graph::validate_memory_graph_entry;
pub use memory_graph::validate_memory_graph_snapshot;
pub use memory_graph::write_memory_graph_entry;

View file

@ -18,10 +18,12 @@ pub fn plan_memory_graph_context_cut(
query: &EpiphanyMemoryContextQuery,
) -> EpiphanyMemoryContextPacket {
let budget = query.budget.unwrap_or(12).clamp(1, 64) as usize;
let freshness = snapshot
let dirty_source_hashes = snapshot
.freshness
.clone()
.unwrap_or_else(|| derive_memory_graph_freshness(snapshot, &[]));
.as_ref()
.map(|freshness| freshness.dirty_source_hashes.as_slice())
.unwrap_or(&[]);
let freshness = derive_memory_graph_freshness(snapshot, dirty_source_hashes);
let stale_nodes: HashSet<&str> = freshness
.stale_node_ids
.iter()

View file

@ -16,6 +16,7 @@ pub use epiphany_state_model::EpiphanyMemoryEmbeddingManifest;
pub use epiphany_state_model::EpiphanyMemoryFreshness;
pub use epiphany_state_model::EpiphanyMemoryFreshnessStatus;
pub use epiphany_state_model::EpiphanyMemoryGraphSnapshot;
pub use epiphany_state_model::EpiphanyMemoryGraphSource;
pub use epiphany_state_model::EpiphanyMemoryLifecycle;
pub use epiphany_state_model::EpiphanyMemoryLifecycleReceipt;
pub use epiphany_state_model::EpiphanyMemoryNode;
@ -30,9 +31,11 @@ pub use freshness::derive_memory_graph_freshness;
pub use ids::memory_graph_domain_id;
pub use ids::memory_graph_edge_id;
pub use ids::memory_graph_node_id;
pub use profiles::RepoMemoryGraphRefresh;
pub use profiles::memory_graph_from_agent_memories;
pub use profiles::memory_graph_from_epiphany_graphs;
pub use profiles::memory_graph_from_heartbeat_cognition;
pub use profiles::refresh_or_validate_repo_memory_graph;
pub use store::EpiphanyMemoryGraphEntry;
pub use store::MEMORY_GRAPH_KEY;
pub use store::MEMORY_GRAPH_SCHEMA_VERSION;

View file

@ -92,7 +92,7 @@ pub fn memory_graph_from_agent_memories(
}
EpiphanyMemoryGraphSnapshot {
schema_version: Some("epiphany.memory_graph.v0".to_string()),
schema_version: Some("epiphany.memory_graph.v1".to_string()),
graph_id: graph_id.into(),
domains,
nodes,

View file

@ -45,7 +45,7 @@ pub fn memory_graph_from_heartbeat_cognition(
import_bridge_pressure(cognition, &mut domains, &mut nodes, &mut summaries);
EpiphanyMemoryGraphSnapshot {
schema_version: Some("epiphany.memory_graph.v0".to_string()),
schema_version: Some("epiphany.memory_graph.v1".to_string()),
graph_id: graph_id.into(),
domains,
nodes,

View file

@ -4,4 +4,6 @@ pub mod repo;
pub use agent::memory_graph_from_agent_memories;
pub use heartbeat::memory_graph_from_heartbeat_cognition;
pub use repo::RepoMemoryGraphRefresh;
pub use repo::memory_graph_from_epiphany_graphs;
pub use repo::refresh_or_validate_repo_memory_graph;

View file

@ -13,18 +13,128 @@ use crate::memory_graph::EpiphanyMemorySummary;
use crate::memory_graph::memory_graph_domain_id;
use crate::memory_graph::memory_graph_edge_id;
use crate::memory_graph::memory_graph_node_id;
use anyhow::{Context, Result};
use epiphany_state_model::EpiphanyCodeRef;
use epiphany_state_model::EpiphanyGraph;
use epiphany_state_model::EpiphanyGraphEdge;
use epiphany_state_model::EpiphanyGraphLink;
use epiphany_state_model::EpiphanyGraphNode;
use epiphany_state_model::EpiphanyGraphs;
use epiphany_state_model::EpiphanyThreadState;
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use uuid::Uuid;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RepoMemoryGraphRefresh {
Reused,
Refreshed,
}
pub fn refresh_or_validate_repo_memory_graph(
store_path: &Path,
source_identity: &str,
state: &EpiphanyThreadState,
repo_root: &Path,
graph_id: impl Into<String>,
) -> Result<(EpiphanyMemoryGraphSnapshot, RepoMemoryGraphRefresh)> {
let current_entry = crate::memory_graph::load_memory_graph_entry(store_path)?;
if let Some(snapshot) = current_entry
.filter(|entry| entry.schema_version == crate::memory_graph::MEMORY_GRAPH_SCHEMA_VERSION)
.and_then(|entry| entry.snapshot().ok())
.filter(|snapshot| {
snapshot.schema_version.as_deref()
== Some(crate::memory_graph::MEMORY_GRAPH_SCHEMA_VERSION)
})
{
let provenance_matches = snapshot.source.as_ref().is_some_and(|source| {
source.kind == "thread_state_repo_graph"
&& source.identity == source_identity
&& source.revision == state.revision
});
if provenance_matches && snapshot_anchor_bytes_match(&snapshot, repo_root) {
return Ok((snapshot, RepoMemoryGraphRefresh::Reused));
}
}
let snapshot = memory_graph_from_epiphany_graphs(
graph_id,
&state.graphs,
source_identity,
state.revision,
repo_root,
)?;
let temporary = store_path.with_extension(format!("refresh-{}.tmp", Uuid::new_v4()));
crate::memory_graph::write_memory_graph_snapshot(&temporary, &snapshot)?;
atomic_replace_file(&temporary, store_path).with_context(|| {
format!(
"atomically replace memory graph store {}",
store_path.display()
)
})?;
Ok((snapshot, RepoMemoryGraphRefresh::Refreshed))
}
#[cfg(windows)]
fn atomic_replace_file(source: &Path, destination: &Path) -> Result<()> {
use std::os::windows::ffi::OsStrExt;
use windows_sys::Win32::Storage::FileSystem::{
MOVEFILE_REPLACE_EXISTING, MOVEFILE_WRITE_THROUGH, MoveFileExW,
};
let source = source
.as_os_str()
.encode_wide()
.chain(Some(0))
.collect::<Vec<_>>();
let destination = destination
.as_os_str()
.encode_wide()
.chain(Some(0))
.collect::<Vec<_>>();
let moved = unsafe {
MoveFileExW(
source.as_ptr(),
destination.as_ptr(),
MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH,
)
};
if moved == 0 {
return Err(std::io::Error::last_os_error().into());
}
Ok(())
}
#[cfg(not(windows))]
fn atomic_replace_file(source: &Path, destination: &Path) -> Result<()> {
fs::rename(source, destination)?;
Ok(())
}
fn snapshot_anchor_bytes_match(snapshot: &EpiphanyMemoryGraphSnapshot, repo_root: &Path) -> bool {
snapshot
.nodes
.iter()
.flat_map(|node| &node.anchors)
.chain(snapshot.edges.iter().flat_map(|edge| &edge.anchors))
.all(|anchor| {
let (Some(code_ref), Some(expected)) = (&anchor.code_ref, &anchor.source_hash) else {
return false;
};
fs::read(repo_root.join(&code_ref.path))
.ok()
.is_some_and(|bytes| format!("sha256:{:x}", Sha256::digest(bytes)) == *expected)
})
}
pub fn memory_graph_from_epiphany_graphs(
graph_id: impl Into<String>,
graphs: &EpiphanyGraphs,
) -> EpiphanyMemoryGraphSnapshot {
source_identity: impl Into<String>,
source_revision: u64,
repo_root: &Path,
) -> Result<EpiphanyMemoryGraphSnapshot> {
let architecture_domain = memory_graph_domain_id(
EpiphanyMemoryProfile::RepoArchitecture,
"accepted_graph",
@ -54,12 +164,14 @@ pub fn memory_graph_from_epiphany_graphs(
EpiphanyMemoryProfile::RepoArchitecture,
&mut node_map,
&mut nodes,
repo_root,
);
import_graph_edges(
&graphs.architecture,
EpiphanyMemoryProfile::RepoArchitecture,
&node_map,
&mut edges,
repo_root,
);
summaries.push(repo_summary(
"summary-accepted-architecture-graph",
@ -84,12 +196,14 @@ pub fn memory_graph_from_epiphany_graphs(
EpiphanyMemoryProfile::RepoDataflow,
&mut node_map,
&mut nodes,
repo_root,
);
import_graph_edges(
&graphs.dataflow,
EpiphanyMemoryProfile::RepoDataflow,
&node_map,
&mut edges,
repo_root,
);
summaries.push(repo_summary(
"summary-accepted-dataflow-graph",
@ -102,22 +216,56 @@ pub fn memory_graph_from_epiphany_graphs(
));
}
import_graph_links(graphs.links.as_slice(), &node_map, &mut edges);
import_graph_links(graphs.links.as_slice(), &node_map, &mut edges, repo_root);
EpiphanyMemoryGraphSnapshot {
schema_version: Some("epiphany.memory_graph.v0".to_string()),
let stale_node_ids = nodes
.iter()
.filter(|node| node.lifecycle == EpiphanyMemoryLifecycle::Stale)
.map(|node| node.id.clone())
.collect::<Vec<_>>();
let stale_edge_ids = edges
.iter()
.filter(|edge| edge.lifecycle == EpiphanyMemoryLifecycle::Stale)
.map(|edge| edge.id.clone())
.collect::<Vec<_>>();
let stale = !stale_node_ids.is_empty() || !stale_edge_ids.is_empty();
if stale {
for summary in &mut summaries {
summary.freshness = EpiphanyMemoryFreshnessStatus::Stale;
}
}
Ok(EpiphanyMemoryGraphSnapshot {
schema_version: Some("epiphany.memory_graph.v1".to_string()),
graph_id: graph_id.into(),
source: Some(crate::memory_graph::EpiphanyMemoryGraphSource {
kind: "thread_state_repo_graph".to_string(),
identity: source_identity.into(),
revision: source_revision,
}),
domains,
nodes,
edges,
summaries,
freshness: Some(EpiphanyMemoryFreshness {
status: EpiphanyMemoryFreshnessStatus::Ready,
note: Some("Imported from accepted Epiphany graph state.".to_string()),
status: if stale {
EpiphanyMemoryFreshnessStatus::Stale
} else {
EpiphanyMemoryFreshnessStatus::Ready
},
stale_node_ids,
stale_edge_ids,
note: Some(
if stale {
"One or more accepted graph anchors could not be verified against source bytes."
} else {
"Imported from accepted Epiphany graph state."
}
.to_string(),
),
..Default::default()
}),
..Default::default()
}
})
}
fn repo_domain(id: String, profile: EpiphanyMemoryProfile, title: &str) -> EpiphanyMemoryDomain {
@ -136,13 +284,15 @@ fn import_graph_nodes(
profile: EpiphanyMemoryProfile,
node_map: &mut HashMap<String, String>,
nodes: &mut Vec<EpiphanyMemoryNode>,
repo_root: &Path,
) {
for node in &graph.nodes {
let memory_id = memory_graph_node_id(domain_id, "accepted_graph_node", &node.id, None);
node_map.insert(node.id.clone(), memory_id.clone());
nodes.push(memory_node_from_graph_node(
memory_id, domain_id, profile, node,
));
nodes.push(
memory_node_from_graph_node(memory_id.clone(), domain_id, profile, node, repo_root)
.unwrap_or_else(|_| stale_memory_node(memory_id, domain_id, profile, node)),
);
}
}
@ -151,14 +301,19 @@ fn memory_node_from_graph_node(
domain_id: &str,
profile: EpiphanyMemoryProfile,
node: &EpiphanyGraphNode,
) -> EpiphanyMemoryNode {
let anchors = anchors_from_code_refs(&id, &node.code_refs);
repo_root: &Path,
) -> Result<EpiphanyMemoryNode> {
let anchors = anchors_from_code_refs(&id, &node.code_refs, repo_root)?;
let source_hashes = if anchors.is_empty() {
vec!["anchor:missing".to_string()]
} else {
anchors.iter().map(|anchor| anchor.id.clone()).collect()
anchors
.iter()
.filter_map(|anchor| anchor.source_hash.clone())
.collect()
};
EpiphanyMemoryNode {
let grounded = !anchors.is_empty();
Ok(EpiphanyMemoryNode {
id,
domain_id: domain_id.to_string(),
profile,
@ -173,9 +328,37 @@ fn memory_node_from_graph_node(
),
anchors,
source_hashes,
lifecycle: graph_status_lifecycle(node.status.as_deref()),
lifecycle: if grounded {
graph_status_lifecycle(node.status.as_deref())
} else {
EpiphanyMemoryLifecycle::Stale
},
salience: 70,
confidence: 80,
confidence: if grounded { 80 } else { 0 },
..Default::default()
})
}
fn stale_memory_node(
id: String,
domain_id: &str,
profile: EpiphanyMemoryProfile,
node: &EpiphanyGraphNode,
) -> EpiphanyMemoryNode {
EpiphanyMemoryNode {
id,
domain_id: domain_id.to_string(),
profile,
kind: memory_node_kind(node),
title: node.title.clone(),
claim: graph_node_claim(node),
question: "Source anchor is unavailable; refresh after restoring it.".to_string(),
tension: "Accepted anatomy cannot be verified against source bytes.".to_string(),
action_implication: "Do not use this claim as fresh compressed anatomy.".to_string(),
source_hashes: vec!["anchor:missing".to_string()],
lifecycle: EpiphanyMemoryLifecycle::Stale,
salience: 70,
confidence: 0,
..Default::default()
}
}
@ -185,6 +368,7 @@ fn import_graph_edges(
profile: EpiphanyMemoryProfile,
node_map: &HashMap<String, String>,
edges: &mut Vec<EpiphanyMemoryEdge>,
repo_root: &Path,
) {
for edge in &graph.edges {
let source_id = node_map
@ -196,7 +380,7 @@ fn import_graph_edges(
.cloned()
.unwrap_or_else(|| edge.target_id.clone());
edges.push(memory_edge_from_graph_edge(
source_id, target_id, profile, edge,
source_id, target_id, profile, edge, repo_root,
));
}
}
@ -206,6 +390,7 @@ fn memory_edge_from_graph_edge(
target_id: String,
profile: EpiphanyMemoryProfile,
edge: &EpiphanyGraphEdge,
repo_root: &Path,
) -> EpiphanyMemoryEdge {
let id = edge.id.clone().unwrap_or_else(|| {
memory_graph_edge_id(
@ -215,6 +400,12 @@ fn memory_edge_from_graph_edge(
edge.code_refs.iter().map(code_ref_key),
)
});
let anchors = anchors_from_code_refs(
edge.id.as_deref().unwrap_or("edge"),
&edge.code_refs,
repo_root,
);
let grounded = edge.code_refs.is_empty() || anchors.is_ok();
EpiphanyMemoryEdge {
id,
source_id,
@ -222,9 +413,13 @@ fn memory_edge_from_graph_edge(
kind: memory_edge_kind(edge.kind.as_str()),
profile,
claim: graph_edge_claim(edge),
anchors: anchors_from_code_refs(edge.id.as_deref().unwrap_or("edge"), &edge.code_refs),
lifecycle: EpiphanyMemoryLifecycle::Accepted,
confidence: 80,
anchors: anchors.unwrap_or_default(),
lifecycle: if grounded {
EpiphanyMemoryLifecycle::Accepted
} else {
EpiphanyMemoryLifecycle::Stale
},
confidence: if grounded { 80 } else { 0 },
}
}
@ -232,6 +427,7 @@ fn import_graph_links(
links: &[EpiphanyGraphLink],
node_map: &HashMap<String, String>,
edges: &mut Vec<EpiphanyMemoryEdge>,
repo_root: &Path,
) {
for link in links {
let Some(source_id) = node_map.get(&link.dataflow_node_id).cloned() else {
@ -240,6 +436,8 @@ fn import_graph_links(
let Some(target_id) = node_map.get(&link.architecture_node_id).cloned() else {
continue;
};
let anchors = anchors_from_code_refs("link", &link.code_refs, repo_root);
let grounded = link.code_refs.is_empty() || anchors.is_ok();
edges.push(EpiphanyMemoryEdge {
id: memory_graph_edge_id(
&source_id,
@ -255,9 +453,13 @@ fn import_graph_links(
.relationship
.clone()
.unwrap_or_else(|| "Dataflow node is linked to architecture node.".to_string()),
anchors: anchors_from_code_refs("link", &link.code_refs),
lifecycle: EpiphanyMemoryLifecycle::Accepted,
confidence: 75,
anchors: anchors.unwrap_or_default(),
lifecycle: if grounded {
EpiphanyMemoryLifecycle::Accepted
} else {
EpiphanyMemoryLifecycle::Stale
},
confidence: if grounded { 75 } else { 0 },
});
}
}
@ -306,17 +508,23 @@ fn repo_summary(
fn anchors_from_code_refs(
prefix: &str,
code_refs: &[EpiphanyCodeRef],
) -> Vec<EpiphanyMemoryAnchor> {
repo_root: &Path,
) -> Result<Vec<EpiphanyMemoryAnchor>> {
code_refs
.iter()
.enumerate()
.map(|(index, code_ref)| EpiphanyMemoryAnchor {
id: format!("anchor-{prefix}-{index}"),
kind: "code_ref".to_string(),
target: code_ref_key(code_ref),
code_ref: Some(code_ref.clone()),
source_hash: Some(code_ref_key(code_ref)),
..Default::default()
.map(|(index, code_ref)| {
let bytes = fs::read(repo_root.join(&code_ref.path))
.with_context(|| format!("missing source anchor {}", code_ref.path.display()))?;
let digest = format!("sha256:{:x}", Sha256::digest(&bytes));
Ok(EpiphanyMemoryAnchor {
id: format!("anchor-{prefix}-{index}"),
kind: "code_ref".to_string(),
target: code_ref_key(code_ref),
code_ref: Some(code_ref.clone()),
source_hash: Some(digest),
..Default::default()
})
})
.collect()
}
@ -429,7 +637,14 @@ mod tests {
..Default::default()
};
let snapshot = memory_graph_from_epiphany_graphs("repo-profile", &graphs);
let snapshot = memory_graph_from_epiphany_graphs(
"repo-profile",
&graphs,
"test-state",
1,
Path::new("."),
)
.unwrap();
let errors = validate_memory_graph_snapshot(&snapshot);
assert!(errors.is_empty(), "{errors:?}");
@ -458,7 +673,14 @@ mod tests {
..Default::default()
};
let snapshot = memory_graph_from_epiphany_graphs("repo-profile", &graphs);
let snapshot = memory_graph_from_epiphany_graphs(
"repo-profile",
&graphs,
"test-state",
1,
Path::new("."),
)
.unwrap();
let errors = validate_memory_graph_snapshot(&snapshot);
assert!(
@ -472,4 +694,109 @@ mod tests {
.any(|error| error.message.contains("missing target node"))
);
}
fn anchored_state(path: &str, revision: u64) -> EpiphanyThreadState {
EpiphanyThreadState {
revision,
graphs: EpiphanyGraphs {
architecture: EpiphanyGraph {
nodes: vec![EpiphanyGraphNode {
id: "body".to_string(),
title: "Body".to_string(),
purpose: "Grounded body anatomy.".to_string(),
code_refs: vec![EpiphanyCodeRef {
path: PathBuf::from(path),
..Default::default()
}],
..Default::default()
}],
..Default::default()
},
..Default::default()
},
..Default::default()
}
}
#[test]
fn repo_cache_reuse_requires_revision_and_anchor_bytes() -> anyhow::Result<()> {
let temp = tempfile::tempdir()?;
std::fs::write(temp.path().join("body.rs"), "one")?;
let store = temp.path().join("memory-graph.msgpack");
let state = anchored_state("body.rs", 1);
let (first, first_refresh) =
refresh_or_validate_repo_memory_graph(&store, "thread", &state, temp.path(), "repo")?;
assert_eq!(first_refresh, RepoMemoryGraphRefresh::Refreshed);
assert!(first.nodes[0].source_hashes[0].starts_with("sha256:"));
let (same, same_refresh) =
refresh_or_validate_repo_memory_graph(&store, "thread", &state, temp.path(), "repo")?;
assert_eq!(same_refresh, RepoMemoryGraphRefresh::Reused);
assert_eq!(first.source, same.source);
std::fs::write(temp.path().join("body.rs"), "two")?;
let (changed, bytes_refresh) =
refresh_or_validate_repo_memory_graph(&store, "thread", &state, temp.path(), "repo")?;
assert_eq!(bytes_refresh, RepoMemoryGraphRefresh::Refreshed);
assert_ne!(first.nodes[0].source_hashes, changed.nodes[0].source_hashes);
let next = anchored_state("body.rs", 2);
let (revised, revision_refresh) =
refresh_or_validate_repo_memory_graph(&store, "thread", &next, temp.path(), "repo")?;
assert_eq!(revision_refresh, RepoMemoryGraphRefresh::Refreshed);
assert_eq!(revised.source.unwrap().revision, 2);
Ok(())
}
#[test]
fn missing_anchor_cannot_emit_ready_summary() -> anyhow::Result<()> {
let temp = tempfile::tempdir()?;
let snapshot = memory_graph_from_epiphany_graphs(
"repo",
&anchored_state("gone.rs", 1).graphs,
"thread",
1,
temp.path(),
)?;
assert_eq!(
snapshot.freshness.unwrap().status,
EpiphanyMemoryFreshnessStatus::Stale
);
assert!(
snapshot
.summaries
.iter()
.all(|summary| summary.freshness == EpiphanyMemoryFreshnessStatus::Stale)
);
Ok(())
}
#[test]
fn missing_edge_anchor_stales_edge_summary_and_snapshot() -> anyhow::Result<()> {
let temp = tempfile::tempdir()?;
std::fs::write(temp.path().join("body.rs"), "body")?;
let mut state = anchored_state("body.rs", 1);
state.graphs.architecture.edges.push(EpiphanyGraphEdge {
id: Some("edge-body".to_string()),
source_id: "body".to_string(),
target_id: "body".to_string(),
kind: "owns".to_string(),
code_refs: vec![EpiphanyCodeRef {
path: PathBuf::from("missing-edge.rs"),
..Default::default()
}],
..Default::default()
});
let snapshot =
memory_graph_from_epiphany_graphs("repo", &state.graphs, "thread", 1, temp.path())?;
assert_eq!(snapshot.edges[0].lifecycle, EpiphanyMemoryLifecycle::Stale);
assert_eq!(
snapshot.summaries[0].freshness,
EpiphanyMemoryFreshnessStatus::Stale
);
let freshness = snapshot.freshness.expect("freshness");
assert_eq!(freshness.status, EpiphanyMemoryFreshnessStatus::Stale);
assert_eq!(freshness.stale_edge_ids, vec!["edge-body".to_string()]);
Ok(())
}
}

View file

@ -1,4 +1,6 @@
use super::EpiphanyMemoryFreshnessStatus;
use super::EpiphanyMemoryGraphSnapshot;
use super::derive_memory_graph_freshness;
use super::validate_memory_graph_snapshot;
use anyhow::Result;
use anyhow::anyhow;
@ -9,7 +11,7 @@ use std::path::Path;
pub const MEMORY_GRAPH_TYPE: &str = "epiphany.memory_graph";
pub const MEMORY_GRAPH_KEY: &str = "default";
pub const MEMORY_GRAPH_SCHEMA_VERSION: &str = "epiphany.memory_graph.v0";
pub const MEMORY_GRAPH_SCHEMA_VERSION: &str = "epiphany.memory_graph.v1";
#[derive(Clone, Debug, PartialEq, DatabaseEntry)]
#[cultcache(type = "epiphany.memory_graph", schema = "EpiphanyMemoryGraphEntry")]
@ -89,6 +91,13 @@ pub fn validate_memory_graph_entry(entry: &EpiphanyMemoryGraphEntry) -> Result<(
));
}
let snapshot = entry.snapshot()?;
if snapshot.schema_version.as_deref() != Some(MEMORY_GRAPH_SCHEMA_VERSION) {
return Err(anyhow!(
"memory graph snapshot schema_version is {:?}, expected {:?}",
snapshot.schema_version,
MEMORY_GRAPH_SCHEMA_VERSION
));
}
if entry.graph_id != snapshot.graph_id {
return Err(anyhow!(
"memory graph entry graph_id {:?} does not match snapshot graph_id {:?}",
@ -105,5 +114,29 @@ pub fn validate_memory_graph_entry(entry: &EpiphanyMemoryGraphEntry) -> Result<(
.join("; ");
return Err(anyhow!("memory graph validation failed: {message}"));
}
let dirty = snapshot
.freshness
.as_ref()
.map(|freshness| freshness.dirty_source_hashes.as_slice())
.unwrap_or(&[]);
let derived = derive_memory_graph_freshness(&snapshot, dirty);
if snapshot.freshness.as_ref().is_some_and(|stored| {
stored.status != derived.status
|| stored.stale_node_ids != derived.stale_node_ids
|| stored.stale_edge_ids != derived.stale_edge_ids
|| stored.stale_summary_ids != derived.stale_summary_ids
}) {
return Err(anyhow!(
"memory graph stored freshness disagrees with derived lifecycle/source freshness"
));
}
if snapshot.summaries.iter().any(|summary| {
derived.stale_summary_ids.contains(&summary.id)
&& summary.freshness != EpiphanyMemoryFreshnessStatus::Stale
}) {
return Err(anyhow!(
"memory graph has a ready summary covering stale anatomy"
));
}
Ok(())
}

View file

@ -23,6 +23,7 @@ fn fixture_snapshot() -> EpiphanyMemoryGraphSnapshot {
let node_b = memory_graph_node_id(&domain_id, "module", "src/memory_graph.rs", Some("b"));
let edge = memory_graph_edge_id(&node_a, &node_b, "owns", ["src/memory_graph.rs"]);
EpiphanyMemoryGraphSnapshot {
schema_version: Some(MEMORY_GRAPH_SCHEMA_VERSION.to_string()),
graph_id: "graph".to_string(),
domains: vec![EpiphanyMemoryDomain {
id: domain_id.clone(),
@ -197,6 +198,15 @@ fn memory_graph_entry_validation_rejects_mismatched_identity() {
assert!(error.to_string().contains("does not match snapshot"));
}
#[test]
fn memory_graph_entry_rejects_embedded_legacy_schema() {
let mut snapshot = fixture_snapshot();
snapshot.schema_version = Some("epiphany.memory_graph.v0".to_string());
let entry = EpiphanyMemoryGraphEntry::from_snapshot(&snapshot).expect("snapshot entry");
let error = validate_memory_graph_entry(&entry).expect_err("embedded v0 is invalid");
assert!(error.to_string().contains("snapshot schema_version"));
}
#[test]
fn memory_graph_profile_law_keeps_role_memory_from_repo_lifecycle() {
assert!(lifecycle_allowed_for_profile(
@ -362,6 +372,33 @@ fn memory_graph_context_cut_descends_when_summary_is_stale() {
);
}
#[test]
fn memory_graph_context_cut_rederives_forged_ready_freshness() {
let mut snapshot = fixture_snapshot();
snapshot.nodes[0].lifecycle = EpiphanyMemoryLifecycle::Stale;
snapshot.freshness = Some(EpiphanyMemoryFreshness {
status: EpiphanyMemoryFreshnessStatus::Ready,
note: Some("forged cache claim".to_string()),
..Default::default()
});
let packet = plan_memory_graph_context_cut(
&snapshot,
&EpiphanyMemoryContextQuery {
id: "query".to_string(),
profile: Some(EpiphanyMemoryProfile::RepoArchitecture),
text: Some("shared graph law".to_string()),
..Default::default()
},
);
assert!(packet.summaries.is_empty());
assert!(
packet
.warnings
.iter()
.any(|warning| warning.contains("descended"))
);
}
#[test]
fn memory_graph_context_cut_reports_missing_explicit_ids() {
let snapshot = fixture_snapshot();