Add keyed RepoModel document foundation

This commit is contained in:
Meta Vens 2026-08-14 11:52:11 +02:00
commit 0fbf862cb3
7 changed files with 596 additions and 38 deletions

View file

@ -40,6 +40,7 @@ mod prompt_context;
mod proposal;
mod repo_model_gateway;
mod repository_body_observer;
mod repo_model_documents;
mod repository_readiness;
mod resident_readiness;
mod resident_self;
@ -984,6 +985,7 @@ pub use proposal::EpiphanyMapProposalInput;
pub use proposal::propose_map_update;
pub use repo_model_gateway::*;
pub use repository_body_observer::*;
pub use repo_model_documents::*;
pub use repository_readiness::*;
pub use resident_readiness::*;
pub use resident_self::*;

View file

@ -3,9 +3,10 @@ use std::path::Path;
use anyhow::{Result, anyhow};
use cultcache_rs::{CultCache, CultCacheEnvelope, DatabaseEntry};
use epiphany_state_model::{
EpiphanyBacklogItem, EpiphanyEvidenceRecord, EpiphanyInvestigationCheckpoint,
EpiphanyInvariant, EpiphanyModeState, EpiphanyObjectiveDraft, EpiphanyObservation,
EpiphanyPlanningCapture, EpiphanyPlanningState, EpiphanyRoadmapStream, EpiphanySubgoal,
EpiphanyBacklogItem, EpiphanyEvidenceRecord, EpiphanyInvariant,
EpiphanyInvestigationCheckpoint, EpiphanyModeState, EpiphanyObjectiveDraft,
EpiphanyObservation, EpiphanyPlanningCapture, EpiphanyPlanningState, EpiphanyRoadmapStream,
EpiphanySubgoal,
};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
@ -27,7 +28,10 @@ pub struct EpiphanyMindIdentity {
}
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
#[cultcache(type = "epiphany.mind.objective.v1", schema = "EpiphanyMindObjectiveDocument")]
#[cultcache(
type = "epiphany.mind.objective.v1",
schema = "EpiphanyMindObjectiveDocument"
)]
pub struct EpiphanyMindObjectiveDocument {
#[cultcache(key = 0)]
pub objective: String,
@ -50,28 +54,40 @@ pub struct EpiphanyMindModeDocument {
}
#[derive(Clone, Debug, PartialEq, DatabaseEntry)]
#[cultcache(type = "epiphany.mind.subgoal.v1", schema = "EpiphanyMindSubgoalDocument")]
#[cultcache(
type = "epiphany.mind.subgoal.v1",
schema = "EpiphanyMindSubgoalDocument"
)]
pub struct EpiphanyMindSubgoalDocument {
#[cultcache(key = 0)]
pub value: EpiphanySubgoal,
}
#[derive(Clone, Debug, PartialEq, DatabaseEntry)]
#[cultcache(type = "epiphany.mind.invariant.v1", schema = "EpiphanyMindInvariantDocument")]
#[cultcache(
type = "epiphany.mind.invariant.v1",
schema = "EpiphanyMindInvariantDocument"
)]
pub struct EpiphanyMindInvariantDocument {
#[cultcache(key = 0)]
pub value: EpiphanyInvariant,
}
#[derive(Clone, Debug, PartialEq, DatabaseEntry)]
#[cultcache(type = "epiphany.mind.observation.v1", schema = "EpiphanyMindObservationDocument")]
#[cultcache(
type = "epiphany.mind.observation.v1",
schema = "EpiphanyMindObservationDocument"
)]
pub struct EpiphanyMindObservationDocument {
#[cultcache(key = 0)]
pub value: EpiphanyObservation,
}
#[derive(Clone, Debug, PartialEq, DatabaseEntry)]
#[cultcache(type = "epiphany.mind.evidence.v1", schema = "EpiphanyMindEvidenceDocument")]
#[cultcache(
type = "epiphany.mind.evidence.v1",
schema = "EpiphanyMindEvidenceDocument"
)]
pub struct EpiphanyMindEvidenceDocument {
#[cultcache(key = 0)]
pub value: EpiphanyEvidenceRecord,
@ -88,28 +104,40 @@ pub struct EpiphanyMindInvestigationCheckpointDocument {
}
#[derive(Clone, Debug, PartialEq, DatabaseEntry)]
#[cultcache(type = "epiphany.mind.planning_capture.v1", schema = "EpiphanyMindPlanningCaptureDocument")]
#[cultcache(
type = "epiphany.mind.planning_capture.v1",
schema = "EpiphanyMindPlanningCaptureDocument"
)]
pub struct EpiphanyMindPlanningCaptureDocument {
#[cultcache(key = 0)]
pub value: EpiphanyPlanningCapture,
}
#[derive(Clone, Debug, PartialEq, DatabaseEntry)]
#[cultcache(type = "epiphany.mind.backlog_item.v1", schema = "EpiphanyMindBacklogItemDocument")]
#[cultcache(
type = "epiphany.mind.backlog_item.v1",
schema = "EpiphanyMindBacklogItemDocument"
)]
pub struct EpiphanyMindBacklogItemDocument {
#[cultcache(key = 0)]
pub value: EpiphanyBacklogItem,
}
#[derive(Clone, Debug, PartialEq, DatabaseEntry)]
#[cultcache(type = "epiphany.mind.roadmap_stream.v1", schema = "EpiphanyMindRoadmapStreamDocument")]
#[cultcache(
type = "epiphany.mind.roadmap_stream.v1",
schema = "EpiphanyMindRoadmapStreamDocument"
)]
pub struct EpiphanyMindRoadmapStreamDocument {
#[cultcache(key = 0)]
pub value: EpiphanyRoadmapStream,
}
#[derive(Clone, Debug, PartialEq, DatabaseEntry)]
#[cultcache(type = "epiphany.mind.objective_draft.v1", schema = "EpiphanyMindObjectiveDraftDocument")]
#[cultcache(
type = "epiphany.mind.objective_draft.v1",
schema = "EpiphanyMindObjectiveDraftDocument"
)]
pub struct EpiphanyMindObjectiveDraftDocument {
#[cultcache(key = 0)]
pub value: EpiphanyObjectiveDraft,
@ -128,6 +156,7 @@ pub struct EpiphanyMindView {
pub investigation_checkpoint: Option<EpiphanyInvestigationCheckpoint>,
pub mode: Option<EpiphanyModeState>,
pub planning: EpiphanyPlanningState,
pub repo_model: Option<crate::EpiphanyRepoModelView>,
}
pub(crate) fn register_mind_document_types(cache: &mut CultCache) -> Result<()> {
@ -144,10 +173,19 @@ pub(crate) fn register_mind_document_types(cache: &mut CultCache) -> Result<()>
cache.register_entry_type::<EpiphanyMindBacklogItemDocument>()?;
cache.register_entry_type::<EpiphanyMindRoadmapStreamDocument>()?;
cache.register_entry_type::<EpiphanyMindObjectiveDraftDocument>()?;
crate::repo_model_documents::register_repo_model_document_types(cache)?;
Ok(())
}
pub(crate) fn validate_mind_write_envelope(envelope: &CultCacheEnvelope) -> Result<()> {
if let Some(expected_key) = crate::repo_model_documents::repo_model_write_key(envelope)? {
if envelope.key != expected_key {
return Err(anyhow!(
"RepoModel document semantic identity does not match its envelope key"
));
}
return Ok(());
}
let expected_key = if envelope.r#type == EpiphanyMindIdentity::TYPE {
let value: EpiphanyMindIdentity = rmp_serde::from_slice(&envelope.payload)?;
if value.schema_epoch != MIND_SCHEMA_EPOCH || value.runtime_id.trim().is_empty() {
@ -167,25 +205,41 @@ pub(crate) fn validate_mind_write_envelope(envelope: &CultCacheEnvelope) -> Resu
let _: EpiphanyMindModeDocument = rmp_serde::from_slice(&envelope.payload)?;
MIND_MODE_KEY.to_string()
} else if envelope.r#type == EpiphanyMindSubgoalDocument::TYPE {
rmp_serde::from_slice::<EpiphanyMindSubgoalDocument>(&envelope.payload)?.value.id
rmp_serde::from_slice::<EpiphanyMindSubgoalDocument>(&envelope.payload)?
.value
.id
} else if envelope.r#type == EpiphanyMindInvariantDocument::TYPE {
rmp_serde::from_slice::<EpiphanyMindInvariantDocument>(&envelope.payload)?.value.id
rmp_serde::from_slice::<EpiphanyMindInvariantDocument>(&envelope.payload)?
.value
.id
} else if envelope.r#type == EpiphanyMindObservationDocument::TYPE {
rmp_serde::from_slice::<EpiphanyMindObservationDocument>(&envelope.payload)?.value.id
rmp_serde::from_slice::<EpiphanyMindObservationDocument>(&envelope.payload)?
.value
.id
} else if envelope.r#type == EpiphanyMindEvidenceDocument::TYPE {
rmp_serde::from_slice::<EpiphanyMindEvidenceDocument>(&envelope.payload)?.value.id
rmp_serde::from_slice::<EpiphanyMindEvidenceDocument>(&envelope.payload)?
.value
.id
} else if envelope.r#type == EpiphanyMindInvestigationCheckpointDocument::TYPE {
rmp_serde::from_slice::<EpiphanyMindInvestigationCheckpointDocument>(&envelope.payload)?
.value
.checkpoint_id
} else if envelope.r#type == EpiphanyMindPlanningCaptureDocument::TYPE {
rmp_serde::from_slice::<EpiphanyMindPlanningCaptureDocument>(&envelope.payload)?.value.id
rmp_serde::from_slice::<EpiphanyMindPlanningCaptureDocument>(&envelope.payload)?
.value
.id
} else if envelope.r#type == EpiphanyMindBacklogItemDocument::TYPE {
rmp_serde::from_slice::<EpiphanyMindBacklogItemDocument>(&envelope.payload)?.value.id
rmp_serde::from_slice::<EpiphanyMindBacklogItemDocument>(&envelope.payload)?
.value
.id
} else if envelope.r#type == EpiphanyMindRoadmapStreamDocument::TYPE {
rmp_serde::from_slice::<EpiphanyMindRoadmapStreamDocument>(&envelope.payload)?.value.id
rmp_serde::from_slice::<EpiphanyMindRoadmapStreamDocument>(&envelope.payload)?
.value
.id
} else if envelope.r#type == EpiphanyMindObjectiveDraftDocument::TYPE {
rmp_serde::from_slice::<EpiphanyMindObjectiveDraftDocument>(&envelope.payload)?.value.id
rmp_serde::from_slice::<EpiphanyMindObjectiveDraftDocument>(&envelope.payload)?
.value
.id
} else {
return Err(anyhow!(
"Mind mutation cannot write non-canonical document type {:?}",
@ -207,7 +261,9 @@ pub fn assemble_mind_view(store_path: impl AsRef<Path>) -> Result<EpiphanyMindVi
.get::<EpiphanyMindIdentity>(MIND_SCHEMA_EPOCH)?
.ok_or_else(|| anyhow!("writable Mind store has no v1 identity"))?;
if identity.schema_epoch != MIND_SCHEMA_EPOCH {
return Err(anyhow!("writable Mind store has an unsupported schema epoch"));
return Err(anyhow!(
"writable Mind store has an unsupported schema epoch"
));
}
let objective = cache
@ -222,10 +278,8 @@ pub fn assemble_mind_view(store_path: impl AsRef<Path>) -> Result<EpiphanyMindVi
let mut observations =
values::<EpiphanyMindObservationDocument, _>(&cache, |value| value.value)?;
let mut evidence = values::<EpiphanyMindEvidenceDocument, _>(&cache, |value| value.value)?;
let checkpoints = values::<EpiphanyMindInvestigationCheckpointDocument, _>(
&cache,
|value| value.value,
)?;
let checkpoints =
values::<EpiphanyMindInvestigationCheckpointDocument, _>(&cache, |value| value.value)?;
let mut captures =
values::<EpiphanyMindPlanningCaptureDocument, _>(&cache, |value| value.value)?;
let mut backlog_items =
@ -234,6 +288,14 @@ pub fn assemble_mind_view(store_path: impl AsRef<Path>) -> Result<EpiphanyMindVi
values::<EpiphanyMindRoadmapStreamDocument, _>(&cache, |value| value.value)?;
let mut objective_drafts =
values::<EpiphanyMindObjectiveDraftDocument, _>(&cache, |value| value.value)?;
let repo_model = if cache
.get::<crate::EpiphanyRepoModelIdentityDocument>(crate::REPO_MODEL_IDENTITY_KEY)?
.is_some()
{
Some(crate::assemble_repo_model_view(store_path.as_ref())?)
} else {
None
};
subgoals.sort_by(|left, right| left.id.cmp(&right.id));
invariants.sort_by(|left, right| left.id.cmp(&right.id));
observations.sort_by(|left, right| left.id.cmp(&right.id));
@ -256,10 +318,7 @@ pub fn assemble_mind_view(store_path: impl AsRef<Path>) -> Result<EpiphanyMindVi
};
let mut source_documents = canonical_mind_versions(&cache.snapshot_envelopes())?;
source_documents.sort_by(|left, right| {
(&left.document_type, &left.document_key).cmp(&(
&right.document_type,
&right.document_key,
))
(&left.document_type, &left.document_key).cmp(&(&right.document_type, &right.document_key))
});
let projection_digest = digest_versions(&source_documents)?;
Ok(EpiphanyMindView {
@ -280,6 +339,7 @@ pub fn assemble_mind_view(store_path: impl AsRef<Path>) -> Result<EpiphanyMindVi
roadmap_streams,
objective_drafts,
},
repo_model,
})
}
@ -359,5 +419,4 @@ mod tests {
assert!(first.projection_digest.starts_with("sha256:"));
Ok(())
}
}

View file

@ -644,13 +644,8 @@ fn commit_authorized_mind_mutation(
.collect::<BTreeSet<_>>();
let write_ids = writes
.iter()
.map(|entry| (entry.r#type.as_str(), entry.key.as_str()))
.map(|entry| (entry.r#type.clone(), entry.key.clone()))
.collect::<BTreeSet<_>>();
if !expected_ids.is_subset(&write_ids) {
return Err(anyhow!(
"Mind mutation must replace every strongly read identity"
));
}
let mut cache = runtime_spine_cache(store_path)?;
cache.pull_all_backing_stores()?;
let mut companion_expected = Vec::new();
@ -701,6 +696,12 @@ fn commit_authorized_mind_mutation(
return Ok(EpiphanyMindCommitOutcome::Committed(existing));
}
let mut replacements = writes;
replacements.extend(
strong_reads
.iter()
.filter(|entry| !write_ids.contains(&(entry.r#type.clone(), entry.key.clone())))
.cloned(),
);
replacements.extend(companion_replacements);
replacements.push(cache.prepare_entry(&receipt_id, &receipt)?.0);
let mut expected = strong_reads.clone();
@ -1117,6 +1118,53 @@ mod tests {
)?,
EpiphanyMindCommitOutcome::Conflict { .. }
));
let hands = backing
.pull_all()?
.into_iter()
.find(|entry| {
entry.r#type == crate::EpiphanyMindObservationDocument::TYPE && entry.key == "hands"
})
.unwrap();
assert!(matches!(
commit_mind_mutation(
&store,
&context.context_id,
"test-owner",
vec![hands.clone()],
vec![make("modeling", "read-only dependency")?],
"2026-08-14T00:00:06Z"
)?,
EpiphanyMindCommitOutcome::Committed(_)
));
assert!(matches!(
commit_mind_mutation(
&store,
&context.context_id,
"test-owner",
vec![hands.clone()],
vec![make("hands", "dependency changed")?],
"2026-08-14T00:00:07Z"
)?,
EpiphanyMindCommitOutcome::Committed(_)
));
assert!(matches!(
commit_mind_mutation(
&store,
&context.context_id,
"test-owner",
vec![hands],
vec![make("verification", "must not partially appear")?],
"2026-08-14T00:00:08Z"
)?,
EpiphanyMindCommitOutcome::Conflict { .. }
));
let mut cache = runtime_spine_cache(&store)?;
cache.pull_all_backing_stores()?;
assert!(
cache
.get::<crate::EpiphanyMindObservationDocument>("verification")?
.is_none()
);
let _ = EpiphanyRuntimeJobStatus::Completed;
Ok(())
}

View file

@ -0,0 +1,410 @@
use std::path::Path;
use anyhow::{Result, anyhow};
use cultcache_rs::{CultCache, CultCacheEnvelope, DatabaseEntry};
use epiphany_state_model::{
EpiphanyMemoryDomain, EpiphanyMemoryEdge, EpiphanyMemoryGraphSnapshot,
EpiphanyMemoryLifecycleReceipt, EpiphanyMemoryNode, EpiphanyMemorySummary, RepoFrontierItem,
};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use crate::{EpiphanyMindDocumentVersion, runtime_spine_cache, validate_memory_graph_snapshot};
pub const REPO_MODEL_SCHEMA_EPOCH: &str = "epiphany.repo_model.epoch.v1";
pub const REPO_MODEL_IDENTITY_KEY: &str = "repo-model";
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
#[cultcache(
type = "epiphany.mind.repo_model.identity.v1",
schema = "EpiphanyRepoModelIdentityDocument"
)]
pub struct EpiphanyRepoModelIdentityDocument {
#[cultcache(key = 0)]
pub schema_epoch: String,
#[cultcache(key = 1)]
pub graph_id: String,
#[cultcache(key = 2)]
pub runtime_id: String,
#[cultcache(key = 3)]
pub swarm_id: String,
#[cultcache(key = 4)]
pub workspace_id: String,
#[cultcache(key = 5)]
pub body_binding_sha256: String,
}
macro_rules! repo_document {
($name:ident, $type:literal, $schema:literal, $value:ty) => {
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
#[cultcache(type = $type, schema = $schema)]
pub struct $name {
#[cultcache(key = 0)]
pub payload_msgpack: Vec<u8>,
}
impl $name {
pub fn new(value: &$value) -> Result<Self> {
Ok(Self {
payload_msgpack: rmp_serde::to_vec_named(value)?,
})
}
pub fn value(&self) -> Result<$value> {
rmp_serde::from_slice(&self.payload_msgpack)
.map_err(|error| anyhow!("invalid {} payload: {error}", $schema))
}
}
};
}
repo_document!(
EpiphanyRepoModelDomainDocument,
"epiphany.mind.repo_model.domain.v1",
"EpiphanyRepoModelDomainDocument",
EpiphanyMemoryDomain
);
repo_document!(
EpiphanyRepoModelNodeDocument,
"epiphany.mind.repo_model.node.v1",
"EpiphanyRepoModelNodeDocument",
EpiphanyMemoryNode
);
repo_document!(
EpiphanyRepoModelEdgeDocument,
"epiphany.mind.repo_model.edge.v1",
"EpiphanyRepoModelEdgeDocument",
EpiphanyMemoryEdge
);
repo_document!(
EpiphanyRepoModelSummaryDocument,
"epiphany.mind.repo_model.summary.v1",
"EpiphanyRepoModelSummaryDocument",
EpiphanyMemorySummary
);
repo_document!(
EpiphanyRepoModelFrontierDocument,
"epiphany.mind.repo_model.frontier.v1",
"EpiphanyRepoModelFrontierDocument",
RepoFrontierItem
);
repo_document!(
EpiphanyRepoModelLifecycleReceiptDocument,
"epiphany.mind.repo_model.lifecycle_receipt.v1",
"EpiphanyRepoModelLifecycleReceiptDocument",
EpiphanyMemoryLifecycleReceipt
);
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
#[cultcache(
type = "epiphany.mind.repo_model.claim_obligations.v1",
schema = "EpiphanyRepoModelClaimObligationsDocument"
)]
pub struct EpiphanyRepoModelClaimObligationsDocument {
#[cultcache(key = 0)]
pub node_id: String,
#[cultcache(key = 1)]
pub unresolved_frontier_ids: Vec<String>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct EpiphanyRepoModelView {
pub identity: EpiphanyRepoModelIdentityDocument,
pub projection_digest: String,
pub source_documents: Vec<EpiphanyMindDocumentVersion>,
pub domains: Vec<EpiphanyMemoryDomain>,
pub nodes: Vec<EpiphanyMemoryNode>,
pub edges: Vec<EpiphanyMemoryEdge>,
pub summaries: Vec<EpiphanyMemorySummary>,
pub frontier: Vec<RepoFrontierItem>,
pub lifecycle_receipts: Vec<EpiphanyMemoryLifecycleReceipt>,
pub claim_obligations: Vec<EpiphanyRepoModelClaimObligationsDocument>,
}
pub(crate) fn register_repo_model_document_types(cache: &mut CultCache) -> Result<()> {
cache.register_entry_type::<EpiphanyRepoModelIdentityDocument>()?;
cache.register_entry_type::<EpiphanyRepoModelDomainDocument>()?;
cache.register_entry_type::<EpiphanyRepoModelNodeDocument>()?;
cache.register_entry_type::<EpiphanyRepoModelEdgeDocument>()?;
cache.register_entry_type::<EpiphanyRepoModelSummaryDocument>()?;
cache.register_entry_type::<EpiphanyRepoModelFrontierDocument>()?;
cache.register_entry_type::<EpiphanyRepoModelLifecycleReceiptDocument>()?;
cache.register_entry_type::<EpiphanyRepoModelClaimObligationsDocument>()?;
Ok(())
}
pub(crate) fn repo_model_write_key(envelope: &CultCacheEnvelope) -> Result<Option<String>> {
let key = if envelope.r#type == EpiphanyRepoModelIdentityDocument::TYPE {
let value: EpiphanyRepoModelIdentityDocument = rmp_serde::from_slice(&envelope.payload)?;
if value.schema_epoch != REPO_MODEL_SCHEMA_EPOCH
|| [
value.graph_id.as_str(),
value.runtime_id.as_str(),
value.swarm_id.as_str(),
value.workspace_id.as_str(),
value.body_binding_sha256.as_str(),
]
.into_iter()
.any(|value| value.trim().is_empty())
{
return Err(anyhow!("RepoModel identity write is invalid"));
}
REPO_MODEL_IDENTITY_KEY.to_string()
} else if envelope.r#type == EpiphanyRepoModelDomainDocument::TYPE {
rmp_serde::from_slice::<EpiphanyRepoModelDomainDocument>(&envelope.payload)?
.value()?
.id
} else if envelope.r#type == EpiphanyRepoModelNodeDocument::TYPE {
rmp_serde::from_slice::<EpiphanyRepoModelNodeDocument>(&envelope.payload)?
.value()?
.id
} else if envelope.r#type == EpiphanyRepoModelEdgeDocument::TYPE {
rmp_serde::from_slice::<EpiphanyRepoModelEdgeDocument>(&envelope.payload)?
.value()?
.id
} else if envelope.r#type == EpiphanyRepoModelSummaryDocument::TYPE {
rmp_serde::from_slice::<EpiphanyRepoModelSummaryDocument>(&envelope.payload)?
.value()?
.id
} else if envelope.r#type == EpiphanyRepoModelFrontierDocument::TYPE {
rmp_serde::from_slice::<EpiphanyRepoModelFrontierDocument>(&envelope.payload)?
.value()?
.id
} else if envelope.r#type == EpiphanyRepoModelLifecycleReceiptDocument::TYPE {
rmp_serde::from_slice::<EpiphanyRepoModelLifecycleReceiptDocument>(&envelope.payload)?
.value()?
.id
} else if envelope.r#type == EpiphanyRepoModelClaimObligationsDocument::TYPE {
let value: EpiphanyRepoModelClaimObligationsDocument =
rmp_serde::from_slice(&envelope.payload)?;
if value
.unresolved_frontier_ids
.windows(2)
.any(|pair| pair[0] >= pair[1])
{
return Err(anyhow!("RepoModel claim obligations are not canonical"));
}
value.node_id
} else {
return Ok(None);
};
if key.trim().is_empty() {
return Err(anyhow!("RepoModel semantic identity cannot be empty"));
}
Ok(Some(key))
}
pub fn assemble_repo_model_view(store_path: impl AsRef<Path>) -> Result<EpiphanyRepoModelView> {
let mut cache = runtime_spine_cache(store_path.as_ref())?;
cache.pull_all_backing_stores()?;
let identity = cache
.get::<EpiphanyRepoModelIdentityDocument>(REPO_MODEL_IDENTITY_KEY)?
.ok_or_else(|| anyhow!("writable Mind store has no keyed RepoModel identity"))?;
let mut domains = values::<EpiphanyRepoModelDomainDocument, _>(&cache, |entry| entry.value())?;
let mut nodes = values::<EpiphanyRepoModelNodeDocument, _>(&cache, |entry| entry.value())?;
let mut edges = values::<EpiphanyRepoModelEdgeDocument, _>(&cache, |entry| entry.value())?;
let mut summaries =
values::<EpiphanyRepoModelSummaryDocument, _>(&cache, |entry| entry.value())?;
let mut frontier =
values::<EpiphanyRepoModelFrontierDocument, _>(&cache, |entry| entry.value())?;
let mut lifecycle_receipts =
values::<EpiphanyRepoModelLifecycleReceiptDocument, _>(&cache, |entry| entry.value())?;
let mut claim_obligations = cache.get_all::<EpiphanyRepoModelClaimObligationsDocument>()?;
domains.sort_by(|left, right| left.id.cmp(&right.id));
nodes.sort_by(|left, right| left.id.cmp(&right.id));
edges.sort_by(|left, right| left.id.cmp(&right.id));
summaries.sort_by(|left, right| left.id.cmp(&right.id));
frontier.sort_by(|left, right| left.id.cmp(&right.id));
lifecycle_receipts.sort_by(|left, right| left.id.cmp(&right.id));
claim_obligations.sort_by(|left, right| left.node_id.cmp(&right.node_id));
let validation = EpiphanyMemoryGraphSnapshot {
graph_id: identity.graph_id.clone(),
domains: domains.clone(),
nodes: nodes.clone(),
edges: edges.clone(),
summaries: summaries.clone(),
lifecycle_receipts: lifecycle_receipts.clone(),
frontier: frontier.clone(),
..Default::default()
};
let errors = validate_memory_graph_snapshot(&validation);
if !errors.is_empty() {
return Err(anyhow!(
"keyed RepoModel view is invalid: {}",
errors
.iter()
.map(|error| format!("{}: {}", error.path, error.message))
.collect::<Vec<_>>()
.join("; ")
));
}
validate_claim_obligations(&nodes, &frontier, &claim_obligations)?;
let mut source_documents = cache
.snapshot_envelopes()
.into_iter()
.filter(|entry| entry.r#type.starts_with("epiphany.mind.repo_model."))
.map(|entry| EpiphanyMindDocumentVersion::from_envelope("epiphany-mind", &entry))
.collect::<Result<Vec<_>>>()?;
source_documents.sort_by(|left, right| {
(&left.document_type, &left.document_key).cmp(&(&right.document_type, &right.document_key))
});
let projection_digest = format!(
"sha256:{:x}",
Sha256::digest(rmp_serde::to_vec_named(&source_documents)?)
);
Ok(EpiphanyRepoModelView {
identity,
projection_digest,
source_documents,
domains,
nodes,
edges,
summaries,
frontier,
lifecycle_receipts,
claim_obligations,
})
}
fn values<T, V>(cache: &CultCache, take: impl Fn(T) -> Result<V>) -> Result<Vec<V>>
where
T: DatabaseEntry,
{
cache.get_all::<T>()?.into_iter().map(take).collect()
}
fn validate_claim_obligations(
nodes: &[EpiphanyMemoryNode],
frontier: &[RepoFrontierItem],
obligations: &[EpiphanyRepoModelClaimObligationsDocument],
) -> Result<()> {
for obligation in obligations {
if !nodes.iter().any(|node| node.id == obligation.node_id) {
return Err(anyhow!("RepoModel claim obligation names a missing node"));
}
for frontier_id in &obligation.unresolved_frontier_ids {
let item = frontier
.iter()
.find(|item| item.id == *frontier_id)
.ok_or_else(|| anyhow!("RepoModel claim obligation names a missing frontier"))?;
if !item.target_claim_ids.contains(&obligation.node_id)
|| !matches!(
item.status,
epiphany_state_model::RepoFrontierStatus::Proposed
| epiphany_state_model::RepoFrontierStatus::Active
| epiphany_state_model::RepoFrontierStatus::Blocked
)
{
return Err(anyhow!("RepoModel claim obligation is not live"));
}
}
}
for item in frontier.iter().filter(|item| {
matches!(
item.status,
epiphany_state_model::RepoFrontierStatus::Proposed
| epiphany_state_model::RepoFrontierStatus::Active
| epiphany_state_model::RepoFrontierStatus::Blocked
)
}) {
for node_id in &item.target_claim_ids {
if !obligations.iter().any(|obligation| {
obligation.node_id == *node_id
&& obligation.unresolved_frontier_ids.contains(&item.id)
}) {
return Err(anyhow!("unresolved frontier lost its claim obligation"));
}
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{RuntimeSpineInitOptions, initialize_runtime_spine};
use epiphany_state_model::{
EpiphanyMemoryLifecycle, EpiphanyMemoryNodeKind, EpiphanyMemoryProfile, RepoFrontierStatus,
};
#[test]
fn keyed_repo_model_view_is_deterministic_and_has_no_global_revision() -> Result<()> {
let temp = tempfile::tempdir()?;
let store = temp.path().join("repo-model.cc");
initialize_runtime_spine(
&store,
RuntimeSpineInitOptions {
runtime_id: "repo-model-view".into(),
display_name: "RepoModel view".into(),
created_at: "2026-08-14T00:00:00Z".into(),
},
)?;
let mut cache = runtime_spine_cache(&store)?;
cache.put(
REPO_MODEL_IDENTITY_KEY,
&EpiphanyRepoModelIdentityDocument {
schema_epoch: REPO_MODEL_SCHEMA_EPOCH.into(),
graph_id: "graph-1".into(),
runtime_id: "repo-model-view".into(),
swarm_id: "swarm-1".into(),
workspace_id: "workspace-1".into(),
body_binding_sha256: "sha256:body".into(),
},
)?;
cache.put(
"domain-1",
&EpiphanyRepoModelDomainDocument::new(&EpiphanyMemoryDomain {
id: "domain-1".into(),
profile: EpiphanyMemoryProfile::RepoArchitecture,
title: "Architecture".into(),
lifecycle: EpiphanyMemoryLifecycle::Accepted,
..Default::default()
})?,
)?;
cache.put(
"node-1",
&EpiphanyRepoModelNodeDocument::new(&EpiphanyMemoryNode {
id: "node-1".into(),
domain_id: "domain-1".into(),
profile: EpiphanyMemoryProfile::RepoArchitecture,
kind: EpiphanyMemoryNodeKind::Module,
title: "Mind".into(),
claim: "Mind state is keyed".into(),
question: "Which owner writes next?".into(),
action_implication: "Keep mutations disjoint".into(),
source_hashes: vec!["anchor:missing".into()],
lifecycle: EpiphanyMemoryLifecycle::Accepted,
..Default::default()
})?,
)?;
cache.put(
"frontier-1",
&EpiphanyRepoModelFrontierDocument::new(&RepoFrontierItem {
id: "frontier-1".into(),
migration_body: "repo".into(),
question: "Cut the aggregate?".into(),
target_claim_ids: vec!["node-1".into()],
recommended_next_organ: "Modeling".into(),
status: RepoFrontierStatus::Active,
..Default::default()
})?,
)?;
cache.put(
"node-1",
&EpiphanyRepoModelClaimObligationsDocument {
node_id: "node-1".into(),
unresolved_frontier_ids: vec!["frontier-1".into()],
},
)?;
let first = assemble_repo_model_view(&store)?;
let second = assemble_repo_model_view(&store)?;
assert_eq!(first, second);
assert_eq!(first.nodes[0].id, "node-1");
assert!(first.projection_digest.starts_with("sha256:"));
let mind = crate::assemble_mind_view(&store)?;
assert_eq!(mind.repo_model.as_ref(), Some(&first));
Ok(())
}
}