Recover typed review after coordinator failure

This commit is contained in:
Meta Vens 2026-08-11 15:46:32 +02:00
commit 2e1ccaf7d0
5 changed files with 373 additions and 18 deletions

View file

@ -655,6 +655,7 @@ pub fn resident_cognitive_runtime_id(runtime_store: &Path) -> Result<String> {
}
fn resident_coordinator_binding_for_grant(
resident_store: &Path,
policy: &ResidentSelfPolicy,
grant: &ResidentSelfHeartbeatGrant,
) -> Result<ResidentCoordinatorBinding> {
@ -672,13 +673,18 @@ fn resident_coordinator_binding_for_grant(
let receipt = cache
.get::<crate::EpiphanyCoordinatorRunReceipt>(receipt_id)?
.ok_or_else(|| anyhow!("coordinator continuation receipt is missing"))?;
if !matches!(receipt.status.as_str(), "planned" | "needsReview" | "completed") {
return Err(anyhow!("coordinator continuation receipt is not successful"));
let required_action = if matches!(
receipt.status.as_str(),
"planned" | "needsReview" | "completed"
) {
resident_self_safe_continuation_action(&policy.runtime_store, &receipt)?
} else {
resident_self_failed_receipt_typed_continuation_action(
resident_store,
&policy.runtime_store,
&receipt,
)?
}
let required_action = resident_self_safe_continuation_action(
&policy.runtime_store,
&receipt,
)?
.ok_or_else(|| anyhow!("coordinator receipt no longer owns a safe continuation"))?;
return Ok(ResidentCoordinatorBinding {
runtime_id: runtime_identity.runtime_id,
@ -1275,6 +1281,106 @@ fn resident_self_safe_continuation_action(
Ok(action.map(str::to_string))
}
fn resident_self_failed_receipt_typed_continuation_action(
resident_store: &Path,
runtime_store: &Path,
receipt: &crate::EpiphanyCoordinatorRunReceipt,
) -> Result<Option<String>> {
if matches!(
receipt.status.as_str(),
"planned" | "needsReview" | "completed"
) {
return Ok(None);
}
let Some(grant_id) = receipt.resident_grant_id.as_deref() else {
return Ok(None);
};
let resident = state_cache(resident_store)?;
let Some(grant) = resident.get::<ResidentSelfHeartbeatGrant>(grant_id)? else {
return Ok(None);
};
if grant.consumed_at_millis.is_none()
|| grant.terminal_at_millis.is_none()
|| grant.private_state_exposed
{
return Ok(None);
}
let Some(crate::RuntimeTypedRequestRef::ProposalModeling(request_id)) =
resident_self_typed_request_ref(&grant)?
else {
return Ok(None);
};
let acknowledgements = resident
.get_all::<ResidentSelfTerminalAck>()?
.into_iter()
.filter(|ack| ack.grant_id == grant.grant_id)
.collect::<Vec<_>>();
if acknowledgements.len() != 1 {
return Err(anyhow!(
"failed coordinator receipt typed continuation requires one exact terminal acknowledgement"
));
}
let acknowledgement = &acknowledgements[0];
if acknowledgement.coordinator_receipt_id != receipt.receipt_id
|| receipt.resident_launch_digest.as_deref()
!= Some(acknowledgement.launch_digest.as_str())
{
return Err(anyhow!(
"failed coordinator receipt typed continuation lost its exact resident binding"
));
}
let Some(evidence) = crate::runtime_typed_request_fulfillment(
runtime_store,
crate::RuntimeTypedRequestRef::ProposalModeling(request_id),
)?
else {
return Ok(None);
};
let mut runtime = crate::runtime_spine_cache(runtime_store)?;
runtime.pull_all_backing_stores()?;
let thread = runtime
.get::<crate::EpiphanyThreadStateEntry>(crate::THREAD_STATE_KEY)?
.ok_or_else(|| anyhow!("typed continuation requires authoritative thread state"))?;
let state = thread.state()?;
if thread.thread_id != receipt.thread_id {
return Err(anyhow!(
"failed coordinator receipt typed continuation crossed thread authority"
));
}
let links = state
.runtime_links
.iter()
.filter(|link| {
link.binding_id == crate::EPIPHANY_MODELING_ROLE_BINDING_ID
&& link.runtime_job_id == evidence.job_id
&& link.runtime_result_id.as_deref() == Some(evidence.result_id.as_str())
})
.count();
if links != 1 {
return Err(anyhow!(
"typed proposal fulfillment does not own exactly one current Modeling result link"
));
}
let acceptances = state
.acceptance_receipts
.iter()
.filter(|accepted| {
accepted.result_id == evidence.result_id
&& accepted.job_id == evidence.job_id
&& accepted.binding_id == crate::EPIPHANY_MODELING_ROLE_BINDING_ID
&& accepted.surface == "roleAccept"
&& accepted.role_id == "modeling"
&& accepted.status == "accepted"
})
.count();
if acceptances > 1 {
return Err(anyhow!(
"typed proposal fulfillment has multiple Modeling acceptance authorities"
));
}
Ok((acceptances == 0).then(|| "reviewModelingResult".to_string()))
}
pub fn ingest_resident_self_coordinator_continuation_pressure(
resident_store: &Path,
runtime_store: &Path,
@ -1296,10 +1402,19 @@ pub fn ingest_resident_self_coordinator_continuation_pressure(
"resident continuation lost its last exact coordinator receipt"
));
};
if !matches!(receipt.status.as_str(), "planned" | "needsReview" | "completed") {
return Ok(false);
}
let Some(action) = resident_self_safe_continuation_action(runtime_store, &receipt)? else {
let action = if matches!(
receipt.status.as_str(),
"planned" | "needsReview" | "completed"
) {
resident_self_safe_continuation_action(runtime_store, &receipt)?
} else {
resident_self_failed_receipt_typed_continuation_action(
resident_store,
runtime_store,
&receipt,
)?
};
let Some(action) = action else {
return Ok(false);
};
let pressure_id = format!("resident-self-continuation-{receipt_id}-{action}");
@ -1987,7 +2102,7 @@ pub fn settle_resident_self_exited_coordinator(
resident_store,
lease,
&receipt.receipt_id,
&receipt.status,
if typed { "fulfilled" } else { &receipt.status },
now_millis,
cooldown_seconds,
false,
@ -2175,7 +2290,7 @@ pub fn prepare_resident_self_launch(
let Some(mut grant) = pending_resident_self_grant(path)? else {
return Ok(None);
};
let binding = resident_coordinator_binding_for_grant(policy, &grant)?;
let binding = resident_coordinator_binding_for_grant(path, policy, &grant)?;
let wake = ResidentSelfWake::Explicit {
objective: grant.objective.clone(),
};
@ -3939,7 +4054,7 @@ mod tests {
.get::<ResidentSelfHeartbeatGrant>(&grant.grant_id)?
.expect("completed grant");
assert_eq!(stale_grant.terminal_at_millis, Some(10));
assert_eq!(stale_grant.terminal_status.as_deref(), Some("planned"));
assert_eq!(stale_grant.terminal_status.as_deref(), Some("fulfilled"));
let expected_grant = cache
.snapshot_envelopes()
.into_iter()

View file

@ -14453,6 +14453,199 @@ pub(crate) mod tests {
Ok(())
}
#[test]
fn failed_coordinator_receipt_cannot_strand_authenticated_proposal_fulfillment()
-> Result<()> {
let root = tempfile::tempdir()?;
let (runtime_store, result, _) =
proposal_admission_fixture(root.path(), "failed-receipt-continuation")?;
let request_id = result
.proposal_modeling_request_id
.clone()
.expect("proposal request echo");
put_runtime_role_worker_result(&runtime_store, &result)?;
let mut runtime = runtime_spine_cache(&runtime_store)?;
runtime.pull_all_backing_stores()?;
let thread_entry = runtime
.get::<crate::EpiphanyThreadStateEntry>(crate::THREAD_STATE_KEY)?
.expect("proposal fixture thread state");
let mut thread_state = thread_entry.state()?;
let modeling_link = thread_state
.runtime_links
.iter_mut()
.find(|link| {
link.binding_id == crate::EPIPHANY_MODELING_ROLE_BINDING_ID
&& link.runtime_job_id == result.job_id
})
.expect("proposal fixture Modeling runtime link");
modeling_link.surface = "runtimeResult".into();
modeling_link.runtime_result_id = Some(result.result_id.clone());
runtime.put(
crate::THREAD_STATE_KEY,
&crate::EpiphanyThreadStateEntry::from_state(
&thread_entry.thread_id,
&thread_state,
)?,
)?;
let resident_store = root.path().join("resident.cc");
crate::enqueue_resident_self_pressure(
&resident_store,
&crate::ResidentSelfPressure {
schema_version: crate::RESIDENT_SELF_PRESSURE_SCHEMA_VERSION.into(),
pressure_id: "failed-receipt-proposal-pressure".into(),
kind: "repo-frontier-proposal-modeling".into(),
provenance_ref: format!(
"cultcache://repo-frontier-proposal-modeling/{request_id}"
),
objective: "Settle exact proposal Modeling fulfillment despite process failure."
.into(),
created_at_millis: 1,
status: "pending".into(),
consumed_by_grant_id: None,
private_state_exposed: false,
},
)?;
crate::heartbeat_issue_resident_self_grant(
&resident_store,
"failed-receipt-heartbeat",
"failed-receipt-action",
2,
)?
.expect("proposal Modeling grant");
let coordinator = root.path().join("epiphany-mvp-coordinator");
std::fs::write(&coordinator, b"witnessed executable")?;
let policy = crate::ResidentSelfPolicy {
workspace: root.path().to_path_buf(),
coordinator_bin: coordinator.clone(),
model_runtime_bin: root.path().join("epiphany-model-runtime"),
tool_adapter_bin: root.path().join("epiphany-tool-mcp-runtime"),
runtime_store: runtime_store.clone(),
local_verse_store: root.path().join("local-verse.cc"),
agent_memory_store: root.path().join("mind.cc"),
artifact_root: root.path().join("artifacts"),
codex_home: root.path().join("codex-home"),
mcp_config: root.path().join("mcp.toml"),
model_provider: "local".into(),
max_steps: 1,
turn_timeout_seconds: 60,
cooldown_seconds: 1,
idle_sleep_seconds: 5,
failure_backoff_seconds: 20,
release_commit: "failed-receipt-test-release".into(),
release_manifest_digest: "sha256:failed-receipt-test-manifest".into(),
release_store: root.path().join("release.cc"),
release_runtime_id: "deployment-runtime".into(),
release_id: "failed-receipt-test-release".into(),
release_witness_sha256: "sha256:failed-receipt-test-witness".into(),
};
let prepared = crate::prepare_resident_self_launch(&resident_store, &policy, 3)?
.expect("proposal Modeling preparation");
let process = crate::LaunchedCoordinator {
process_id: u32::MAX - 17,
process_creation_token: 83,
process_executable_path: coordinator,
};
crate::claim_resident_self_preparation_as_child(
&resident_store,
&prepared.preparation_id,
&process,
4,
)?;
let lease = crate::acknowledge_resident_self_launch(
&resident_store,
&prepared.preparation_id,
&process,
5,
)?;
let receipt = EpiphanyCoordinatorRunReceipt {
schema_version: COORDINATOR_RUN_RECEIPT_SCHEMA_VERSION.into(),
receipt_id: "failed-receipt-with-valid-proposal-result".into(),
session_id: coordinator_run_session_id(
&lease.turn_id,
Some(&lease.launch_digest),
)?,
thread_id: lease.turn_id.clone(),
mode: "execute".into(),
status: "failed".into(),
final_action: "waitForModelingResult".into(),
final_reason: Some("redundant proposal launch refused".into()),
step_count: 0,
created_at: "2026-08-11T13:00:00Z".into(),
model_provider: Some("local".into()),
runtime_store: runtime_store.display().to_string(),
artifact_refs: Vec::new(),
sealed_artifact_refs: Vec::new(),
metadata: BTreeMap::new(),
resident_grant_id: Some(lease.grant_id.clone()),
resident_launch_digest: Some(lease.launch_digest.clone()),
resident_policy_digest: Some(lease.policy_digest.clone()),
resident_argv_digest: Some(lease.argv_digest.clone()),
resident_objective_digest: Some(lease.objective_digest.clone()),
resident_release_commit: Some(lease.release_commit.clone()),
resident_release_manifest_digest: Some(lease.release_manifest_digest.clone()),
resident_executable_digest: Some(lease.coordinator_executable_digest.clone()),
};
runtime_spine_cache(&runtime_store)?.put(&receipt.receipt_id, &receipt)?;
assert_eq!(
crate::settle_resident_self_exited_coordinator(
&resident_store,
&runtime_store,
&lease,
&receipt,
false,
false,
false,
6,
policy.cooldown_seconds,
)?,
crate::ResidentSelfOutcome::Completed
);
let terminal_grant = crate::resident_self_grant_lifecycle_projection(
&resident_store,
Some(&lease.grant_id),
1,
)?
.pop()
.expect("terminal proposal grant");
assert_eq!(terminal_grant.terminal_status.as_deref(), Some("fulfilled"));
assert!(crate::ingest_resident_self_coordinator_continuation_pressure(
&resident_store,
&runtime_store,
7,
)?);
assert!(!crate::ingest_resident_self_coordinator_continuation_pressure(
&resident_store,
&runtime_store,
8,
)?);
let continuation = crate::resident_self_pressures(&resident_store)?
.into_iter()
.find(|pressure| {
pressure.kind == crate::RESIDENT_SELF_COORDINATOR_CONTINUATION_PRESSURE_KIND
})
.expect("typed fulfillment continuation pressure");
assert!(continuation.pressure_id.ends_with("-reviewModelingResult"));
assert_eq!(
continuation.provenance_ref,
format!("cultcache://coordinator-run-receipt/{}", receipt.receipt_id)
);
crate::heartbeat_issue_resident_self_grant(
&resident_store,
"failed-receipt-review-heartbeat",
"failed-receipt-review-action",
1_007,
)?
.expect("typed review continuation grant");
let review = crate::prepare_resident_self_launch(&resident_store, &policy, 1_008)?
.expect("typed review continuation preparation");
assert!(review.argv.windows(2).any(|pair| {
pair == ["--required-action", "reviewModelingResult"]
}));
assert!(!review.argv.iter().any(|arg| arg == "--objective"));
Ok(())
}
fn claim_repair_admission_fixture(
root: &Path,
suffix: &str,