Make resident Self own receipt-free settlement

This commit is contained in:
Meta Vens 2026-08-10 15:39:19 +01:00
commit 2e66433f51
6 changed files with 239 additions and 131 deletions

View file

@ -6,25 +6,22 @@ use epiphany_core::{
ResidentSelfOutcome, ResidentSelfPolicy, ResidentSelfPorts, ResidentSelfPressure,
ResidentSelfState, acknowledge_resident_self_launch, acquire_resident_process_singleton,
authenticate_resident_self_policy, bind_runtime_repository_domain,
bridge_admitted_persona_feedback_to_heartbeat, cancel_resident_self_turn,
capture_process_instance, complete_resident_self_turn_after_death,
bridge_admitted_persona_feedback_to_heartbeat, capture_process_instance,
coordinator_run_receipts, derive_resident_cognition_readiness,
enqueue_resident_self_pressure, import_bifrost_persona_feedback_deliveries,
ingest_resident_self_domain_pressure, load_epiphany_cultmesh_swarm_brake,
load_resident_self_state, observe_process_instance, pending_resident_self_acks,
prepare_resident_self_launch, publish_resident_provider_readiness,
recover_receipt_free_dead_coordinator_session,
recover_dead_resident_typed_worker,
resident_cognitive_runtime_id,
resident_prepared_launch_thread_id, resident_self_child_claim,
resident_self_grant_has_typed_request, resident_self_local_provider_status,
resident_self_typed_attempt_exists,
resident_self_local_provider_status,
retain_completed_runtime_sessions,
retain_coordinator_run_receipts, retain_resident_self_lifecycles,
settle_resident_self_exited_coordinator, terminate_process_instance,
settle_resident_self_exited_coordinator,
settle_resident_self_receipt_free_dead_coordinator, terminate_process_instance,
validate_persona_feedback_store_separation,
validate_resident_self_coordinator_receipt_binding,
validate_resident_self_store_separation, verify_resident_self_grant_fulfillment,
validate_resident_self_store_separation,
};
use serde_json::json;
use std::collections::{BTreeMap, BTreeSet};
@ -227,30 +224,6 @@ fn wait_for_shutdown(requested: &AtomicBool, duration: Duration) {
}
}
fn cancelled_turn_status(
shutdown_requested: bool,
brake_engaged: bool,
timed_out: bool,
) -> &'static str {
if shutdown_requested {
"shutdown-cancelled"
} else if brake_engaged {
"brake-cancelled"
} else if timed_out {
"timed-out"
} else {
"process-failed"
}
}
fn outcome_after_cancel(shutdown_requested: bool) -> ResidentSelfOutcome {
if shutdown_requested {
ResidentSelfOutcome::Braked
} else {
ResidentSelfOutcome::Failed
}
}
fn exact_resident_coordinator_receipt(
runtime_store: &std::path::Path,
lease: &epiphany_core::ResidentSelfTurnLease,
@ -275,83 +248,6 @@ fn exact_resident_coordinator_receipt(
Ok(Some(receipt))
}
fn settle_receipt_free_dead_coordinator(
args: &Args,
lease: &epiphany_core::ResidentSelfTurnLease,
observation: ChildObservation,
shutdown_requested: bool,
brake_engaged: bool,
timed_out: bool,
now: u64,
) -> Result<ResidentSelfOutcome> {
let typed = resident_self_grant_has_typed_request(&args.state_store, &lease.grant_id)?;
if typed {
match verify_resident_self_grant_fulfillment(
&args.state_store,
&args.policy.runtime_store,
&lease.grant_id,
)? {
epiphany_core::ResidentSelfGrantFulfillment::Fulfilled => {
let recovery = recover_receipt_free_dead_coordinator_session(
&args.state_store,
&args.policy.runtime_store,
lease,
observation,
now,
)?
.ok_or_else(|| {
anyhow!(
"typed fulfillment cannot predate its coordinator runtime incarnation"
)
})?;
complete_resident_self_turn_after_death(
&args.state_store,
lease,
&recovery,
now,
args.policy.cooldown_seconds,
)?;
return Ok(ResidentSelfOutcome::Completed);
}
epiphany_core::ResidentSelfGrantFulfillment::Pending => {
if resident_self_typed_attempt_exists(
&args.state_store,
&args.policy.runtime_store,
&lease.grant_id,
)? {
if !recover_dead_resident_typed_worker(
&args.state_store,
&args.policy.runtime_store,
&lease.grant_id,
now,
)? {
return Ok(ResidentSelfOutcome::AwaitingFulfillment);
}
}
}
}
}
let recovery = recover_receipt_free_dead_coordinator_session(
&args.state_store,
&args.policy.runtime_store,
lease,
observation,
now,
)?;
cancel_resident_self_turn(
&args.state_store,
lease,
cancelled_turn_status(shutdown_requested, brake_engaged, timed_out),
if recovery.is_some() {
"exact coordinator process died after atomic opening and before a terminal receipt"
} else {
"exact coordinator process died before atomic runtime opening"
},
now,
)?;
Ok(outcome_after_cancel(shutdown_requested))
}
fn cycle(
args: &Args,
state: &mut ResidentSelfState,
@ -471,14 +367,16 @@ fn cycle(
*state = load_resident_self_state(&args.state_store)?;
return Ok(outcome);
}
let outcome = settle_receipt_free_dead_coordinator(
args,
let outcome = settle_resident_self_receipt_free_dead_coordinator(
&args.state_store,
&args.policy.runtime_store,
&lease,
observation,
shutdown_requested,
brake_engaged,
timed_out,
now,
args.policy.cooldown_seconds,
)?;
*state = load_resident_self_state(&args.state_store)?;
Ok(outcome)
@ -891,23 +789,6 @@ mod brake_tests {
Ok(())
}
#[test]
fn shutdown_preempts_other_cancellation_causes() {
assert_eq!(
cancelled_turn_status(true, true, true),
"shutdown-cancelled"
);
assert_eq!(cancelled_turn_status(false, true, true), "brake-cancelled");
assert_eq!(cancelled_turn_status(false, false, true), "timed-out");
assert_eq!(cancelled_turn_status(false, false, false), "process-failed");
}
#[test]
fn shutdown_cancellation_is_a_braked_outcome_not_a_failure() {
assert_eq!(outcome_after_cancel(true), ResidentSelfOutcome::Braked);
assert_eq!(outcome_after_cancel(false), ResidentSelfOutcome::Failed);
}
#[test]
fn terminal_fulfillment_has_explicit_operator_projection() {
assert_eq!(

View file

@ -1394,6 +1394,94 @@ pub fn resident_self_grant_has_typed_request(
Ok(resident_self_typed_request_ref(&grant)?.is_some())
}
pub fn settle_resident_self_receipt_free_dead_coordinator(
resident_store: &Path,
runtime_store: &Path,
lease: &ResidentSelfTurnLease,
observation: ChildObservation,
shutdown_requested: bool,
brake_engaged: bool,
timed_out: bool,
now_millis: u64,
cooldown_seconds: u64,
) -> Result<ResidentSelfOutcome> {
let typed = resident_self_grant_has_typed_request(resident_store, &lease.grant_id)?;
if typed {
match verify_resident_self_grant_fulfillment(
resident_store,
runtime_store,
&lease.grant_id,
)? {
ResidentSelfGrantFulfillment::Fulfilled => {
let recovery = recover_receipt_free_dead_coordinator_session(
resident_store,
runtime_store,
lease,
observation,
now_millis,
)?
.ok_or_else(|| anyhow!(
"typed fulfillment cannot predate its coordinator runtime incarnation"
))?;
complete_resident_self_turn_after_death(
resident_store,
lease,
&recovery,
now_millis,
cooldown_seconds,
)?;
return Ok(ResidentSelfOutcome::Completed);
}
ResidentSelfGrantFulfillment::Pending => {
if resident_self_typed_attempt_exists(
resident_store,
runtime_store,
&lease.grant_id,
)? && !recover_dead_resident_typed_worker(
resident_store,
runtime_store,
&lease.grant_id,
now_millis,
)? {
return Ok(ResidentSelfOutcome::AwaitingFulfillment);
}
}
}
}
let recovery = recover_receipt_free_dead_coordinator_session(
resident_store,
runtime_store,
lease,
observation,
now_millis,
)?;
let status = if shutdown_requested {
"shutdown-cancelled"
} else if brake_engaged {
"brake-cancelled"
} else if timed_out {
"timed-out"
} else {
"process-failed"
};
cancel_resident_self_turn(
resident_store,
lease,
status,
if recovery.is_some() {
"exact coordinator process died after atomic opening and before a terminal receipt"
} else {
"exact coordinator process died before atomic runtime opening"
},
now_millis,
)?;
Ok(if shutdown_requested {
ResidentSelfOutcome::Braked
} else {
ResidentSelfOutcome::Failed
})
}
pub fn settle_resident_self_exited_coordinator(
resident_store: &Path,
runtime_store: &Path,
@ -3562,6 +3650,11 @@ mod tests {
let temp = tempfile::tempdir()?;
let resident_store = temp.path().join("resident-self.cc");
let runtime_store = temp.path().join("runtime.cc");
let coordinator = temp.path().join("epiphany-mvp-coordinator");
std::fs::write(&coordinator, b"witnessed executable")?;
let mut resident_policy = policy();
resident_policy.coordinator_bin = coordinator.clone();
resident_policy.runtime_store = runtime_store.clone();
seed_model_direction_request(
&runtime_store,
"request-model-fulfillment",
@ -3591,6 +3684,37 @@ mod tests {
2,
)?
.expect("model direction grant");
let prepared = prepare_resident_self_launch(&resident_store, &resident_policy, 3)?
.expect("model direction preparation");
let process = LaunchedCoordinator {
process_id: u32::MAX - 11,
process_creation_token: 77,
process_executable_path: coordinator,
};
claim_resident_self_preparation_as_child(
&resident_store,
&prepared.preparation_id,
&process,
4,
)?;
let lease = acknowledge_resident_self_launch(
&resident_store,
&prepared.preparation_id,
&process,
5,
)?;
let session_id = crate::coordinator_run_session_id(
&lease.turn_id,
Some(&lease.launch_digest),
)?;
crate::open_coordinator_run(
&runtime_store,
&session_id,
&lease.turn_id,
Some(&lease.launch_digest),
&format!("Resident coordinator launch {}.", lease.objective_digest),
"1970-01-01T00:00:00.005Z",
)?;
assert_eq!(
verify_resident_self_grant_fulfillment(
&resident_store,
@ -3642,6 +3766,62 @@ mod tests {
)?,
ResidentSelfGrantFulfillment::Fulfilled
);
assert_eq!(
settle_resident_self_receipt_free_dead_coordinator(
&resident_store,
&runtime_store,
&lease,
ChildObservation::Missing,
false,
false,
false,
6,
resident_policy.cooldown_seconds,
)?,
ResidentSelfOutcome::Completed
);
let settled = load_resident_self_state(&resident_store)?;
assert!(settled.active_turn.is_none());
let terminal_grant = state_cache(&resident_store)?
.get::<ResidentSelfHeartbeatGrant>(&grant.grant_id)?
.expect("terminal model direction grant");
assert_eq!(
terminal_grant.terminal_status.as_deref(),
Some("recovered-fulfilled")
);
let pressure = state_cache(&resident_store)?
.get::<ResidentSelfPressure>("pressure-model-fulfillment")?
.expect("consumed model direction pressure");
assert_eq!(pressure.status, "consumed");
assert_eq!(
pressure.consumed_by_grant_id.as_deref(),
Some(grant.grant_id.as_str())
);
let settled_bytes = std::fs::read(&resident_store)?;
assert!(
settle_resident_self_receipt_free_dead_coordinator(
&resident_store,
&runtime_store,
&lease,
ChildObservation::Missing,
false,
false,
false,
8,
resident_policy.cooldown_seconds,
)
.is_err()
);
assert_eq!(std::fs::read(&resident_store)?, settled_bytes);
assert!(
heartbeat_issue_resident_self_grant(
&resident_store,
"heartbeat-must-not-retry",
"action-must-not-retry",
7,
)?
.is_none()
);
Ok(())
}