Prove decision-state reentry
This commit is contained in:
parent
2bb71c91dc
commit
d3300bba95
5 changed files with 472 additions and 194 deletions
|
|
@ -3191,6 +3191,24 @@ mod tests {
|
|||
model_basis_with_payload(vec![1])
|
||||
}
|
||||
|
||||
fn assert_current_work_reentry_is_read_only(
|
||||
store: &Path,
|
||||
expected: &EpiphanyCurrentWorkProjection,
|
||||
) -> Result<()> {
|
||||
let mut before = crate::runtime_spine_cache(store)?;
|
||||
before.pull_all_backing_stores()?;
|
||||
let before = before.snapshot_envelopes();
|
||||
assert_eq!(&project_current_work(store)?, expected);
|
||||
let mut after = crate::runtime_spine_cache(store)?;
|
||||
after.pull_all_backing_stores()?;
|
||||
let after = after.snapshot_envelopes();
|
||||
assert_eq!(after, before);
|
||||
assert!(after.iter().all(|envelope| {
|
||||
envelope.r#type != "epiphany.thread_state" && envelope.r#type != "epiphany.memory_graph"
|
||||
}));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn body_generation_owns_work_identity_while_repo_model_is_projection_cargo() -> Result<()> {
|
||||
let first = EpiphanyBodyModelingWorkProjection::derive(
|
||||
|
|
@ -3314,6 +3332,7 @@ mod tests {
|
|||
crate::assemble_mind_view(&store)?.projection_digest
|
||||
);
|
||||
assert_eq!(crate::repository_body_read_counters(), (0, 0));
|
||||
assert_current_work_reentry_is_read_only(&store, ¤t_work)?;
|
||||
let resident_pressure = crate::resident_self_body_modeling_pressure(&store, 1)?
|
||||
.expect("unresolved Body work must create Resident Self pressure");
|
||||
assert_eq!(
|
||||
|
|
@ -3339,6 +3358,7 @@ mod tests {
|
|||
scheduled_work.body_modeling_action,
|
||||
Some(EpiphanyAgentPassContinuationAction::Wait)
|
||||
);
|
||||
assert_current_work_reentry_is_read_only(&store, &scheduled_work)?;
|
||||
assert!(crate::resident_self_body_modeling_pressure(&store, 2)?.is_none());
|
||||
let mut scheduled_cache = crate::runtime_spine_cache(&store)?;
|
||||
scheduled_cache.pull_all_backing_stores()?;
|
||||
|
|
@ -3485,6 +3505,7 @@ mod tests {
|
|||
.is_none()
|
||||
);
|
||||
assert_eq!(crate::repository_body_read_counters(), (0, 0));
|
||||
assert_current_work_reentry_is_read_only(&store, &completed_work)?;
|
||||
|
||||
let proposal = crate::intake_user_repo_frontier_proposal(
|
||||
&store,
|
||||
|
|
@ -5414,6 +5435,7 @@ mod tests {
|
|||
assert!(after_research.body_modeling_action.is_none());
|
||||
assert!(after_research.proposal_modeling.is_none());
|
||||
assert!(after_research.frontier_verdict_modeling.is_none());
|
||||
assert_current_work_reentry_is_read_only(&store, &after_research)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,11 +45,7 @@ pub fn admit_persona_pass_input(
|
|||
));
|
||||
}
|
||||
let cache = runtime_spine_cache(runtime_store)?;
|
||||
let write = crate::mind_documents::prepare_mind_document(
|
||||
&cache,
|
||||
&document.turn_id,
|
||||
document,
|
||||
)?;
|
||||
let write = crate::mind_documents::prepare_mind_document(&cache, &document.turn_id, document)?;
|
||||
match crate::reasoning_context::commit_external_typed_observation_mind_mutation(
|
||||
runtime_store,
|
||||
"Persona",
|
||||
|
|
@ -76,6 +72,59 @@ pub fn admit_persona_pass_input(
|
|||
crate::EpiphanyMindDocumentVersion::from_envelope("epiphany-mind", &envelope)
|
||||
}
|
||||
|
||||
pub fn load_admitted_persona_pass_input(
|
||||
runtime_store: &Path,
|
||||
turn_id: &str,
|
||||
) -> Result<
|
||||
Option<(
|
||||
crate::EpiphanyMindPersonaPassInputDocument,
|
||||
crate::EpiphanyMindDocumentVersion,
|
||||
)>,
|
||||
> {
|
||||
let mut cache = runtime_spine_cache(runtime_store)?;
|
||||
cache.pull_all_backing_stores()?;
|
||||
let Some(document) = cache.get::<crate::EpiphanyMindPersonaPassInputDocument>(turn_id)? else {
|
||||
return Ok(None);
|
||||
};
|
||||
document.validate()?;
|
||||
let envelope = cache
|
||||
.get_envelope::<crate::EpiphanyMindPersonaPassInputDocument>(turn_id)?
|
||||
.ok_or_else(|| anyhow!("Persona pass input lost its exact envelope"))?;
|
||||
let source = crate::EpiphanyMindDocumentVersion::from_envelope("epiphany-mind", &envelope)?;
|
||||
let candidates = cache
|
||||
.get_all::<crate::EpiphanyMindCommitReceipt>()?
|
||||
.into_iter()
|
||||
.filter(|receipt| {
|
||||
receipt.invariant_owner == "Persona.pass_input"
|
||||
&& receipt.writes.iter().any(|write| write == &source)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
for receipt in &candidates {
|
||||
receipt.validate()?;
|
||||
}
|
||||
let receipts = candidates
|
||||
.into_iter()
|
||||
.filter(|receipt| {
|
||||
receipt.committed_at == document.admitted_at
|
||||
&& receipt.strong_reads.is_empty()
|
||||
&& receipt.writes == vec![source.clone()]
|
||||
&& matches!(
|
||||
&receipt.authority,
|
||||
crate::EpiphanyMindCommitAuthority::TypedOrganProvenance {
|
||||
organ,
|
||||
provenance,
|
||||
} if organ == "Persona" && document.observed_sources.contains(provenance)
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
if receipts.len() != 1 {
|
||||
return Err(anyhow!(
|
||||
"Persona pass input does not have one exact causal Mind commit receipt"
|
||||
));
|
||||
}
|
||||
Ok(Some((document, source)))
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DatabaseEntry)]
|
||||
#[cultcache(
|
||||
type = "epiphany.persona_effect_execution_intent.v0",
|
||||
|
|
@ -1090,9 +1139,7 @@ pub(crate) fn admit_persona_state_notes(
|
|||
writes,
|
||||
&document.created_at,
|
||||
)? {
|
||||
crate::EpiphanyMindCommitOutcome::Committed(_) => {
|
||||
Ok(("admitted".into(), Vec::new()))
|
||||
}
|
||||
crate::EpiphanyMindCommitOutcome::Committed(_) => Ok(("admitted".into(), Vec::new())),
|
||||
crate::EpiphanyMindCommitOutcome::Conflict {
|
||||
document_identities,
|
||||
} => Err(anyhow!(
|
||||
|
|
@ -1620,7 +1667,8 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn persona_pass_input_is_admitted_once_and_substitution_refuses_byte_identically() -> Result<()> {
|
||||
fn persona_pass_input_is_admitted_once_and_substitution_refuses_byte_identically() -> Result<()>
|
||||
{
|
||||
let temp = tempfile::tempdir()?;
|
||||
let runtime = temp.path().join("runtime.cc");
|
||||
crate::initialize_runtime_spine(
|
||||
|
|
@ -1636,10 +1684,8 @@ mod tests {
|
|||
.into_iter()
|
||||
.find(|entry| entry.r#type == crate::RUNTIME_IDENTITY_TYPE)
|
||||
.expect("runtime identity provenance");
|
||||
let provenance = crate::EpiphanyMindDocumentVersion::from_envelope(
|
||||
"epiphany-heartbeat",
|
||||
&identity,
|
||||
)?;
|
||||
let provenance =
|
||||
crate::EpiphanyMindDocumentVersion::from_envelope("epiphany-heartbeat", &identity)?;
|
||||
let document = crate::EpiphanyMindPersonaPassInputDocument {
|
||||
turn_id: "persona-pass-1".into(),
|
||||
projector_input: crate::PersonaProjectorInput {
|
||||
|
|
@ -1661,11 +1707,25 @@ mod tests {
|
|||
admit_persona_pass_input(&runtime, provenance.clone(), &document)?,
|
||||
first
|
||||
);
|
||||
assert_eq!(
|
||||
load_admitted_persona_pass_input(&runtime, &document.turn_id)?,
|
||||
Some((document.clone(), first.clone()))
|
||||
);
|
||||
let before = SingleFileMessagePackBackingStore::new(&runtime).pull_all()?;
|
||||
let mut substituted = document;
|
||||
let mut substituted = document.clone();
|
||||
substituted.allowed_channel_ids = vec!["elsewhere".into()];
|
||||
assert!(admit_persona_pass_input(&runtime, provenance, &substituted).is_err());
|
||||
assert_eq!(SingleFileMessagePackBackingStore::new(&runtime).pull_all()?, before);
|
||||
assert!(admit_persona_pass_input(&runtime, provenance.clone(), &substituted).is_err());
|
||||
assert_eq!(
|
||||
SingleFileMessagePackBackingStore::new(&runtime).pull_all()?,
|
||||
before
|
||||
);
|
||||
|
||||
let mut naked = document;
|
||||
naked.turn_id = "persona-pass-without-commit".into();
|
||||
let mut cache = crate::runtime_spine_cache(&runtime)?;
|
||||
cache.pull_all_backing_stores()?;
|
||||
cache.put(&naked.turn_id, &naked)?;
|
||||
assert!(load_admitted_persona_pass_input(&runtime, &naked.turn_id).is_err());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -447,6 +447,45 @@ pub struct EpiphanyMindCommitReceipt {
|
|||
pub committed_at: String,
|
||||
}
|
||||
|
||||
impl EpiphanyMindCommitReceipt {
|
||||
pub fn validate(&self) -> Result<()> {
|
||||
if self.schema_version != MIND_COMMIT_RECEIPT_SCHEMA_VERSION {
|
||||
return Err(anyhow!("Mind commit receipt schema is unsupported"));
|
||||
}
|
||||
require_non_empty(&self.invariant_owner, "Mind commit invariant owner")?;
|
||||
chrono::DateTime::parse_from_rfc3339(&self.committed_at)
|
||||
.map_err(|error| anyhow!("Mind commit time is invalid: {error}"))?;
|
||||
if self.writes.is_empty() {
|
||||
return Err(anyhow!("Mind commit receipt has no writes"));
|
||||
}
|
||||
match &self.authority {
|
||||
EpiphanyMindCommitAuthority::ModelDecisionContext {
|
||||
decision_context_id,
|
||||
} => require_non_empty(decision_context_id, "Mind decision context")?,
|
||||
EpiphanyMindCommitAuthority::OperatorProvenance { provenance } => {
|
||||
provenance.validate()?
|
||||
}
|
||||
EpiphanyMindCommitAuthority::TypedOrganProvenance { organ, provenance } => {
|
||||
require_non_empty(organ, "Mind provenance organ")?;
|
||||
provenance.validate()?;
|
||||
}
|
||||
}
|
||||
validate_mind_document_versions(&self.strong_reads, "strong read")?;
|
||||
validate_mind_document_versions(&self.writes, "write")?;
|
||||
if self.receipt_id
|
||||
!= mind_commit_receipt_id(
|
||||
&self.authority,
|
||||
&self.invariant_owner,
|
||||
&self.strong_reads,
|
||||
&self.writes,
|
||||
)?
|
||||
{
|
||||
return Err(anyhow!("Mind commit receipt identity digest mismatch"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum EpiphanyMindCommitAuthority {
|
||||
ModelDecisionContext {
|
||||
|
|
@ -1057,6 +1096,7 @@ fn commit_authorized_mind_mutation(
|
|||
writes: write_versions,
|
||||
committed_at: committed_at.to_string(),
|
||||
};
|
||||
receipt.validate()?;
|
||||
if let Some(existing) = cache.get::<EpiphanyMindCommitReceipt>(&receipt_id)? {
|
||||
if existing != receipt {
|
||||
return Err(anyhow!("Mind commit receipt identity collision"));
|
||||
|
|
@ -1258,6 +1298,20 @@ fn validate_unique_envelope_identities(entries: &[CultCacheEnvelope], label: &st
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_mind_document_versions(
|
||||
entries: &[EpiphanyMindDocumentVersion],
|
||||
label: &str,
|
||||
) -> Result<()> {
|
||||
let mut identities = BTreeSet::new();
|
||||
for entry in entries {
|
||||
entry.validate()?;
|
||||
if !identities.insert(entry.identity()) {
|
||||
return Err(anyhow!("Mind commit receipt repeats {label} identity"));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn current_write_collisions(
|
||||
current: &[CultCacheEnvelope],
|
||||
writes: &[EpiphanyMindDocumentVersion],
|
||||
|
|
@ -1553,8 +1607,8 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn decision_context_reuses_runtime_binding_owner_and_survives_transcript_deletion()
|
||||
-> Result<()> {
|
||||
fn decision_context_reuses_runtime_binding_owner_and_survives_transcript_deletion() -> Result<()>
|
||||
{
|
||||
let temp = tempdir()?;
|
||||
let store = temp.path().join("mind.cc");
|
||||
initialize_runtime_spine(
|
||||
|
|
@ -1677,12 +1731,15 @@ mod tests {
|
|||
cache = runtime_spine_cache(&store)?;
|
||||
cache.put("request-2:00000000", &native_delta)?;
|
||||
cache.put("request-2:00000000", &provider_delta)?;
|
||||
assert!(cache.delete::<epiphany_model_adapter::EpiphanyModelStreamEvent>(
|
||||
"request-2:00000000"
|
||||
)?);
|
||||
assert!(cache.delete::<epiphany_openai_adapter::EpiphanyOpenAiStreamEvent>(
|
||||
"request-2:00000000"
|
||||
)?);
|
||||
assert!(
|
||||
cache
|
||||
.delete::<epiphany_model_adapter::EpiphanyModelStreamEvent>("request-2:00000000")?
|
||||
);
|
||||
assert!(
|
||||
cache.delete::<epiphany_openai_adapter::EpiphanyOpenAiStreamEvent>(
|
||||
"request-2:00000000"
|
||||
)?
|
||||
);
|
||||
cache.pull_all_backing_stores()?;
|
||||
let retained = cache
|
||||
.get::<EpiphanyDecisionContext>(&context.context_id)?
|
||||
|
|
@ -1699,7 +1756,10 @@ mod tests {
|
|||
cache.put(&intent.intent_id, &hostile_binding)?;
|
||||
let before = SingleFileMessagePackBackingStore::new(&store).pull_all()?;
|
||||
assert!(seal_model_decision_context(&store, &terminal_native.request_id).is_err());
|
||||
assert_eq!(SingleFileMessagePackBackingStore::new(&store).pull_all()?, before);
|
||||
assert_eq!(
|
||||
SingleFileMessagePackBackingStore::new(&store).pull_all()?,
|
||||
before
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
Reference in a new issue