From c7412998b54b3ba3e14de2e8b9f00f96ff097b0a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 18 Aug 2026 00:34:51 +0200 Subject: [PATCH] Migrate Research admission to keyed Mind state --- epiphany-core/src/agent_launch.rs | 35 +- .../src/bin/epiphany-mvp-coordinator.rs | 87 +- epiphany-core/src/coordinator_acceptance.rs | 136 +--- epiphany-core/src/coordinator_launch.rs | 7 + .../src/coordinator_objective_intake.rs | 15 +- epiphany-core/src/current_work.rs | 743 +++++++++++++++++- epiphany-core/src/eyes_gateway.rs | 14 +- epiphany-core/src/mind_documents.rs | 14 + epiphany-core/src/reasoning_context.rs | 41 +- epiphany-core/src/repo_model_gateway.rs | 11 +- epiphany-core/src/repository_body_observer.rs | 3 +- epiphany-core/src/runtime_spine.rs | 469 +++++++---- epiphany-core/src/runtime_worker_attempt.rs | 12 + epiphany-core/src/surfaces/coordinator.rs | 192 +---- epiphany-core/src/surfaces/role_result.rs | 7 +- epiphany-core/src/surfaces/worker_launch.rs | 6 +- epiphany-openai-runtime/src/lib.rs | 15 +- vendor/cultcache-rs | 2 +- 18 files changed, 1272 insertions(+), 537 deletions(-) diff --git a/epiphany-core/src/agent_launch.rs b/epiphany-core/src/agent_launch.rs index 37d59b4a..21ea4723 100644 --- a/epiphany-core/src/agent_launch.rs +++ b/epiphany-core/src/agent_launch.rs @@ -342,20 +342,6 @@ fn evidence_output_schema() -> serde_json::Value { }) } -fn scratch_output_schema() -> serde_json::Value { - serde_json::json!({ - "type": "object", - "required": ["summary"], - "properties": { - "summary": {"type": "string", "minLength": 1}, - "hypothesis": {"type": "string"}, - "next_probe": {"type": "string"}, - "notes": {"type": "array", "items": {"type": "string"}} - }, - "additionalProperties": false - }) -} - fn investigation_checkpoint_output_schema() -> serde_json::Value { serde_json::json!({ "type": "object", @@ -623,7 +609,7 @@ pub fn epiphany_role_launch_output_schema(role_id: EpiphanyRoleResultRoleId) -> "statePatch".to_string(), serde_json::json!({ "type": "object", - "description": "Required reviewable statePatch for Mind admission from research/Eyes. Use only observations, evidence, scratch, and optional investigationCheckpoint. The patch must include at least one evidence record and one observation that cites it.", + "description": "Required reviewable statePatch for Mind admission from Research/Eyes. Use only keyed observations, keyed evidence, and an optional investigationCheckpoint. Scratch is pass-local and is not durable Mind state. The patch must include at least one evidence record and one observation that cites it.", "required": ["observations", "evidence"], "properties": { "observations": { @@ -636,13 +622,8 @@ pub fn epiphany_role_launch_output_schema(role_id: EpiphanyRoleResultRoleId) -> "minItems": 1, "items": evidence_output_schema() }, - "scratch": scratch_output_schema(), "investigationCheckpoint": investigation_checkpoint_output_schema() }, - "anyOf": [ - {"required": ["scratch"]}, - {"required": ["investigationCheckpoint"]} - ], "additionalProperties": false }), ); @@ -2009,12 +1990,14 @@ mod tests { let imagination = epiphany_proposal_modeling_output_schema( crate::RepoFrontierProposalSourceKind::Imagination, ); - assert!(!imagination["properties"]["proposalFrontierDraft"]["properties"] - ["recommendedNextOrgan"]["enum"] - .as_array() - .expect("organ enum") - .iter() - .any(|organ| organ == "Hands")); + assert!( + !imagination["properties"]["proposalFrontierDraft"]["properties"] + ["recommendedNextOrgan"]["enum"] + .as_array() + .expect("organ enum") + .iter() + .any(|organ| organ == "Hands") + ); } #[test] diff --git a/epiphany-core/src/bin/epiphany-mvp-coordinator.rs b/epiphany-core/src/bin/epiphany-mvp-coordinator.rs index 0406d5fd..c5cc2ffe 100644 --- a/epiphany-core/src/bin/epiphany-mvp-coordinator.rs +++ b/epiphany-core/src/bin/epiphany-mvp-coordinator.rs @@ -799,6 +799,43 @@ fn run_coordinator(args: &Args) -> Result { "reviewResearchResult" | "reviewModelingResult" | "reviewVerificationResult" => { let role_id = role_id_for_coordinator_action(&action) .ok_or_else(|| anyhow!("unsupported review action {action}"))?; + if role_id == "research" + && let Some(job_id) = + epiphany_core::current_frontier_research_review_job_id(&runtime_store)? + { + let result = + epiphany_core::runtime_role_worker_result(&runtime_store, &job_id)? + .ok_or_else(|| { + anyhow!("frontier Research review lost its typed result") + })?; + push_event( + &mut step, + json!({"type": "frontierResearchResult", "roleId": role_id, "result": result}), + ); + if !args.auto_review { + final_action = json!({ + "action": "reviewResearchResult", + "reason": "The exact frontier Research result awaits keyed Mind admission.", + "runtimeJobId": job_id, + }); + append_operator_step_jsonl(&steps_path, &step)?; + steps.push(step); + break; + } + let accepted = epiphany_core::accept_frontier_research_result( + &runtime_store, + &job_id, + &now(), + )?; + push_event( + &mut step, + json!({"type": "frontierResearchAccept", "roleId": role_id, "commit": accepted}), + ); + final_status = collect_coordinator_status(&runtime_store, &thread_id)?; + append_operator_step_jsonl(&steps_path, &step)?; + steps.push(step); + continue; + } if role_id == "modeling" && let Some(job_id) = epiphany_core::current_proposal_modeling_review_job_id(&runtime_store)? @@ -1086,11 +1123,21 @@ fn run_coordinator(args: &Args) -> Result { } else { None }; - let thread_free_modeling = role_id == "modeling" - && (proposal_modeling_request_id.is_some() - || status["currentWork"]["bodyModeling"].is_object() - || status["currentWork"]["frontierVerdictModeling"].is_object()); - let launch = if role_id == "modeling" + let thread_free_role = role_id == "research" + || (role_id == "modeling" + && (proposal_modeling_request_id.is_some() + || status["currentWork"]["bodyModeling"].is_object() + || status["currentWork"]["frontierVerdictModeling"].is_object())); + let launch = if role_id == "research" { + let job_id = epiphany_core::launch_current_frontier_research_work( + &runtime_store, + &now(), + )?; + json!({ + "bindingId": epiphany_core::EPIPHANY_RESEARCH_ROLE_BINDING_ID, + "backendJobId": job_id, + }) + } else if role_id == "modeling" && let Some(request_id) = proposal_modeling_request_id { let current = epiphany_core::project_current_work(&runtime_store)? @@ -1177,7 +1224,7 @@ fn run_coordinator(args: &Args) -> Result { &mut step, json!({"type": "workerRuntime", "roleId": role_id, "run": worker_run}), ); - let result = if thread_free_modeling { + let result = if thread_free_role { match epiphany_core::runtime_role_worker_result( &runtime_store, &worker_job_id, @@ -1188,7 +1235,7 @@ fn run_coordinator(args: &Args) -> Result { }), None => json!({ "status": "running", - "note": "The exact keyed Modeling attempt has not produced a terminal result.", + "note": "The exact keyed role attempt has not produced a terminal result.", }), } } else { @@ -1727,16 +1774,11 @@ fn launch_role( .ok_or_else(|| anyhow!("cannot launch role without native coordinator state"))?; let state_loaded_ms = started.elapsed().as_millis(); let role = parse_role_id(role_id)?; - let repo_frontier_research_request = if role - == epiphany_core::EpiphanyRoleResultRoleId::Research - && epiphany_core::runtime_has_actionable_eyes_frontier(runtime_store)? - { - Some( - epiphany_core::select_and_commit_repo_frontier_research_request(runtime_store, &now())?, - ) - } else { - None - }; + if role == epiphany_core::EpiphanyRoleResultRoleId::Research { + return Err(anyhow!( + "Research launch must use the keyed frontier Research current-work owner" + )); + } let expected_revision = expected_revision.and_then(|value| u64::try_from(value).ok()); let focus = epiphany_core::role_launch_context_focus(&state, epiphany_core::epiphany_role_label(role)); @@ -1766,7 +1808,7 @@ fn launch_role( .map_err(anyhow::Error::msg)?; } let role_context_augmented_ms = started.elapsed().as_millis(); - let mut request = epiphany_core::build_epiphany_role_launch_request_with_dynamic_context( + let request = epiphany_core::build_epiphany_role_launch_request_with_dynamic_context( thread_id, role, expected_revision, @@ -1775,15 +1817,6 @@ fn launch_role( Some(context), ) .map_err(anyhow::Error::msg)?; - request.repo_frontier_research_request_id = repo_frontier_research_request - .as_ref() - .map(|selected| selected.request_id.clone()); - if let (Some(selected), epiphany_core::EpiphanyWorkerLaunchDocument::Role(document)) = ( - repo_frontier_research_request.as_ref(), - &mut request.launch_document, - ) { - document.frontier_research_context = Some(selected.into()); - } let request_built_ms = started.elapsed().as_millis(); let launched = service.launch_job( thread_id, diff --git a/epiphany-core/src/coordinator_acceptance.rs b/epiphany-core/src/coordinator_acceptance.rs index 8cfd20d5..d017e26c 100644 --- a/epiphany-core/src/coordinator_acceptance.rs +++ b/epiphany-core/src/coordinator_acceptance.rs @@ -3,7 +3,6 @@ use crate::coordinator_state::changed_fields; use crate::*; use anyhow::Context; use epiphany_state_model::EpiphanyThreadState; -use std::collections::BTreeSet; use std::path::Path; #[derive(Debug, Clone, PartialEq, Eq)] @@ -178,6 +177,11 @@ pub fn accept_coordinator_role_finding( accepted_at: String, nonce: &str, ) -> anyhow::Result { + if role_id == EpiphanyRoleResultRoleId::Research { + return Err(anyhow::anyhow!( + "generic role acceptance cannot admit Research; the exact frontier Research current-work owner must commit it" + )); + } let snapshot = read_role_result_snapshot(Some(state), Some(store), role_id, binding_id); if snapshot.status != EpiphanyCoordinatorRoleResultStatus::Completed { return Err(anyhow::anyhow!( @@ -203,57 +207,7 @@ pub fn accept_coordinator_role_finding( .map_err(|error| anyhow::anyhow!(error.to_string()))?; let mut available = vec![MIND_GATEWAY_REVIEW_TYPE.to_string()]; let mut prerequisites = Vec::new(); - if role_id == EpiphanyRoleResultRoleId::Research { - let public_source_lookups = runtime_authenticated_public_source_lookups_for_worker( - store, - finding.runtime_job_id.as_deref().unwrap_or_default(), - )?; - for lookup in &public_source_lookups { - if !finding - .evidence_ids - .iter() - .any(|evidence_id| evidence_id == &lookup.receipt_id) - || !finding - .files_inspected - .iter() - .any(|source_ref| source_ref == &lookup.source_ref) - { - return Err(anyhow::anyhow!( - "Research finding did not cite its authenticated public source receipt and source ref" - )); - } - } - if finding - .evidence_ids - .iter() - .any(|evidence_id| evidence_id.starts_with("eyes-source-")) - && public_source_lookups.is_empty() - { - return Err(anyhow::anyhow!( - "Research finding cited public source evidence without an authenticated lookup" - )); - } - let packet = eyes_evidence_packet_from_research_finding( - format!("eyes-packet-{}", update.accepted_receipt_id), - &finding, - &update.applied_patch, - &public_source_lookups, - accepted_at.clone(), - ); - available.push(EYES_EVIDENCE_PACKET_TYPE.to_string()); - prerequisites.push(EpiphanyAcceptancePrerequisite::Eyes(packet)); - for lookup in public_source_lookups { - available.push(EYES_SOURCE_LOOKUP_RECEIPT_TYPE.to_string()); - prerequisites.push(EpiphanyAcceptancePrerequisite::EyesSourceLookup(lookup)); - } - let grant_id = format!( - "substrate-grant-{}", - finding.runtime_job_id.as_deref().unwrap_or_default() - ); - if runtime_substrate_gate_repo_access_grant_receipt(store, &grant_id)?.is_some() { - available.push(SUBSTRATE_GATE_REPO_ACCESS_GRANT_RECEIPT_TYPE.to_string()); - } - } else if role_id == EpiphanyRoleResultRoleId::Verification { + if role_id == EpiphanyRoleResultRoleId::Verification { let request_id = finding .verification_request_id .as_deref() @@ -614,9 +568,6 @@ pub fn read_accepted_coordinator_state( #[derive(Debug, Clone, PartialEq, Eq)] pub enum EpiphanyAcceptancePrerequisite { - Eyes(EyesEvidencePacket), - EyesSourceLookup(EyesSourceLookupReceipt), - SubstrateGate(SubstrateGateRepoAccessGrantReceipt), SoulFrontierVerdict { verdict: SoulVerdictReceipt, modeling_request: crate::RepoFrontierModelingRequest, @@ -651,7 +602,6 @@ fn commit_state_with_mind_witness( store_path, expected_state, )?; - let mut eyes_packet_jobs = BTreeSet::new(); for prerequisite in prerequisites { if let EpiphanyAcceptancePrerequisite::SoulFrontierVerdict { verdict, @@ -665,85 +615,11 @@ fn commit_state_with_mind_witness( )); } } - for prerequisite in prerequisites { - if let EpiphanyAcceptancePrerequisite::Eyes(packet) = prerequisite { - if packet.source_role_id != "research" - || !eyes_packet_jobs.insert(packet.source_job_id.clone()) - { - return Err(anyhow::anyhow!( - "Mind commit requires one exact Research packet per source job" - )); - } - let authenticated = - crate::runtime_spine::authenticated_requested_public_source_lookups_for_worker( - &cache, - &packet.source_job_id, - )?; - let authenticated_ids = authenticated - .iter() - .map(|lookup| lookup.receipt_id.as_str()) - .collect::>(); - let packet_ids = packet - .source_lookup_receipt_ids - .iter() - .map(String::as_str) - .collect::>(); - let published = prerequisites - .iter() - .filter_map(|candidate| match candidate { - EpiphanyAcceptancePrerequisite::EyesSourceLookup(lookup) - if lookup.source_job_id == packet.source_job_id => - { - Some(lookup) - } - _ => None, - }) - .collect::>(); - let published_ids = published - .iter() - .map(|lookup| lookup.receipt_id.as_str()) - .collect::>(); - if packet.source_lookup_receipt_ids.len() != packet_ids.len() - || published.len() != published_ids.len() - || packet_ids != authenticated_ids - || published_ids != authenticated_ids - || authenticated.iter().any(|lookup| { - !packet - .source_refs - .iter() - .any(|source_ref| source_ref == &lookup.source_ref) - || !published.iter().any(|candidate| *candidate == lookup) - }) - { - return Err(anyhow::anyhow!( - "Eyes packet lost exact frontier public-source coverage before Mind commit" - )); - } - } - } - for prerequisite in prerequisites { - if let EpiphanyAcceptancePrerequisite::EyesSourceLookup(document) = prerequisite - && !eyes_packet_jobs.contains(&document.source_job_id) - { - return Err(anyhow::anyhow!( - "Eyes source lookup has no owning Research packet" - )); - } - } let (review_envelope, _) = cache.prepare_entry(&mind_review.gateway_id, mind_review)?; let (commit_envelope, _) = cache.prepare_entry(&commit_receipt.receipt_id, commit_receipt)?; let mut batch = vec![review_envelope, commit_envelope]; for prerequisite in prerequisites { let envelope = match prerequisite { - EpiphanyAcceptancePrerequisite::Eyes(document) => { - cache.prepare_entry(&document.packet_id, document)?.0 - } - EpiphanyAcceptancePrerequisite::EyesSourceLookup(document) => { - cache.prepare_entry(&document.receipt_id, document)?.0 - } - EpiphanyAcceptancePrerequisite::SubstrateGate(document) => { - cache.prepare_entry(&document.receipt_id, document)?.0 - } EpiphanyAcceptancePrerequisite::SoulFrontierVerdict { verdict, modeling_request, diff --git a/epiphany-core/src/coordinator_launch.rs b/epiphany-core/src/coordinator_launch.rs index 5469759d..d97cee21 100644 --- a/epiphany-core/src/coordinator_launch.rs +++ b/epiphany-core/src/coordinator_launch.rs @@ -27,6 +27,13 @@ pub fn commit_coordinator_job_launch( "proposal Modeling launch is owned by keyed current work, not the aggregate coordinator transaction" )); } + if request.owner_role == EPIPHANY_RESEARCH_OWNER_ROLE + || request.repo_frontier_research_request_id.is_some() + { + return Err(anyhow!( + "frontier Research launch is owned by keyed current work, not the aggregate coordinator transaction" + )); + } let body_basis = if request.owner_role == EPIPHANY_MODELING_OWNER_ROLE { Some(observe_runtime_repository_body_basis(store)?) } else { diff --git a/epiphany-core/src/coordinator_objective_intake.rs b/epiphany-core/src/coordinator_objective_intake.rs index 040a0b24..004cfeeb 100644 --- a/epiphany-core/src/coordinator_objective_intake.rs +++ b/epiphany-core/src/coordinator_objective_intake.rs @@ -139,14 +139,13 @@ pub fn intake_user_objective( contract: USER_OBJECTIVE_INTAKE_CONTRACT.into(), }; let provenance = cache.prepare_entry(&intake_id, &intake)?.0; - let objective_write = cache - .prepare_entry( - MIND_OBJECTIVE_KEY, - &EpiphanyMindObjectiveDocument { - objective: objective.to_string(), - }, - )? - .0; + let objective_write = crate::mind_documents::prepare_mind_document( + &cache, + MIND_OBJECTIVE_KEY, + &EpiphanyMindObjectiveDocument { + objective: objective.to_string(), + }, + )?; let commit_receipt = match commit_operator_mind_mutation( store, provenance, diff --git a/epiphany-core/src/current_work.rs b/epiphany-core/src/current_work.rs index 1b4193f5..1519ec1b 100644 --- a/epiphany-core/src/current_work.rs +++ b/epiphany-core/src/current_work.rs @@ -615,6 +615,382 @@ pub fn current_frontier_verdict_modeling_review_job_id( .and_then(|work| work.job_id)) } +pub fn current_frontier_research_review_job_id( + store_path: impl AsRef, +) -> Result> { + let lifecycle = crate::runtime_repo_frontier_research_lifecycle(store_path)?; + Ok( + (lifecycle.stage == crate::RepoFrontierResearchLifecycleStage::ResultReady) + .then_some(lifecycle.worker_job_id) + .flatten(), + ) +} + +pub fn launch_current_frontier_research_work( + store_path: impl AsRef, + created_at: &str, +) -> Result { + let store_path = store_path.as_ref(); + chrono::DateTime::parse_from_rfc3339(created_at) + .map_err(|_| anyhow!("frontier Research launch time is invalid"))?; + if crate::runtime_repo_frontier_research_lifecycle(store_path)?.stage + != crate::RepoFrontierResearchLifecycleStage::LaunchReady + { + return Err(anyhow!("Mind has no launchable frontier Research work")); + } + let request = crate::select_and_commit_repo_frontier_research_request(store_path, created_at)?; + let mut cache = crate::runtime_spine_cache(store_path)?; + cache.pull_all_backing_stores()?; + let request_envelope = cache + .get_envelope::(&request.request_id)? + .ok_or_else(|| anyhow!("frontier Research launch lost its request"))?; + let identity = cache + .get::(crate::RUNTIME_IDENTITY_KEY)? + .ok_or_else(|| anyhow!("frontier Research launch requires runtime identity"))?; + let attempt_ordinal = cache + .get_all::()? + .into_iter() + .filter(|launch| { + launch.repo_frontier_research_request_id.as_deref() == Some(request.request_id.as_str()) + }) + .count() + + cache + .get_all::()? + .into_iter() + .filter(|attempt| { + attempt.request_kind == "frontier-research" + && attempt.request_id == request.request_id + }) + .count(); + let job_id = format!( + "frontier-research-{}-attempt-{attempt_ordinal}", + request.request_id + ); + let launch_document = + crate::EpiphanyWorkerLaunchDocument::Role(crate::EpiphanyRoleWorkerLaunchDocument { + thread_id: job_id.clone(), + role_id: "research".into(), + state_revision: 0, + objective: None, + dynamic_prompt_context: None, + repository_body_observation_basis: None, + proposal_modeling_context: None, + frontier_verdict_modeling_context: None, + frontier_planning_context: None, + frontier_research_context: Some((&request).into()), + frontier_plan_mind_context: None, + imagination_consideration_context: None, + admitted_model_direction_consideration_context: None, + active_subgoal_id: None, + active_subgoals: Vec::new(), + active_graph_node_ids: Vec::new(), + investigation_checkpoint: None, + scratch: None, + invariants: Vec::new(), + graphs: None, + recent_evidence: Vec::new(), + recent_observations: Vec::new(), + graph_frontier: None, + graph_checkpoint: None, + planning: None, + churn: None, + }); + let authority_scope = "epiphany.role.research".to_string(); + let output_contract_id = launch_document.output_contract_id().to_string(); + let prepared = crate::prepare_runtime_spine_heartbeat_job( + &cache, + crate::RuntimeSpineHeartbeatJobOptions { + runtime_id: identity.runtime_id, + display_name: "Epiphany Local".into(), + session_id: crate::EPIPHANY_RUNTIME_ROOT_SESSION_ID.into(), + objective: "Gather evidence for one exact external-evidence obligation".into(), + coordinator_note: "Research current-work launch transaction opened this session." + .into(), + job_id: job_id.clone(), + role: crate::EPIPHANY_RESEARCH_OWNER_ROLE.into(), + binding_id: crate::EPIPHANY_RESEARCH_ROLE_BINDING_ID.into(), + authority_scope: authority_scope.clone(), + instruction: "Act as Epiphany Research. Gather only the evidence required by the exact frontier request and return keyed evidence and observations for Mind review.".into(), + launch_document, + output_contract_id: output_contract_id.clone(), + organ_launch_contract: crate::default_launch_organ_contract( + &authority_scope, + "role", + &output_contract_id, + ), + proposal_modeling_request_id: None, + frontier_planning_request_id: None, + frontier_plan_mind_request_id: None, + imagination_consideration_request_id: None, + admitted_model_direction_consideration_request_id: None, + repo_frontier_modeling_request_id: None, + repo_frontier_research_request_id: Some(request.request_id.clone()), + created_at: created_at.to_string(), + }, + )?; + let grant = crate::substrate_gate::substrate_gate_repo_access_grant_for_worker( + format!("substrate-grant-{job_id}"), + job_id.clone(), + crate::EPIPHANY_RESEARCH_ROLE_BINDING_ID.into(), + crate::EPIPHANY_RESEARCH_OWNER_ROLE.into(), + authority_scope, + true, + created_at.to_string(), + ); + let snapshot = cache.snapshot_envelopes(); + let mut expected = vec![request_envelope]; + for source in &request.frontier_authority_documents { + let envelope = snapshot + .iter() + .find(|value| value.r#type == source.document_type && value.key == source.document_key) + .ok_or_else(|| anyhow!("frontier Research launch lost a strong source"))?; + if EpiphanyMindDocumentVersion::from_envelope("epiphany-mind", envelope)? != *source { + return Err(anyhow!("frontier Research launch strong source changed")); + } + expected.push(envelope.clone()); + } + commit_current_work_launch( + store_path, + &cache, + expected, + prepared.envelopes, + vec![cache.prepare_entry(&grant.receipt_id, &grant)?.0], + "frontier Research", + )?; + Ok(job_id) +} + +pub fn accept_frontier_research_result( + store_path: impl AsRef, + job_id: &str, + accepted_at: &str, +) -> Result { + let store_path = store_path.as_ref(); + chrono::DateTime::parse_from_rfc3339(accepted_at) + .map_err(|_| anyhow!("frontier Research acceptance time is invalid"))?; + let mut cache = crate::runtime_spine_cache(store_path)?; + cache.pull_all_backing_stores()?; + if let Some(result) = cache.get::(job_id)? { + let mut receipts = cache + .get_all::()? + .into_iter() + .filter(|receipt| { + receipt.invariant_owner == "Eyes.frontier_research" + && matches!( + &receipt.authority, + crate::EpiphanyMindCommitAuthority::ModelDecisionContext { + decision_context_id + } if decision_context_id == &result.decision_context_id + ) + }) + .collect::>(); + if receipts.len() > 1 { + return Err(anyhow!( + "frontier Research result has multiple Mind commit authorities" + )); + } + if let Some(receipt) = receipts.pop() { + let request_id = result + .repo_frontier_research_request_id + .as_deref() + .ok_or_else(|| anyhow!("frontier Research replay lost its request identity"))?; + let packet_id = format!("eyes-packet-{request_id}"); + let packet = cache + .get::(&packet_id)? + .ok_or_else(|| anyhow!("frontier Research replay lost its evidence packet"))?; + if packet.research_request_id != request_id + || packet.decision_context_id != result.decision_context_id + || !receipt + .writes + .iter() + .any(|write| write.document_type == crate::EpiphanyMindEvidenceDocument::TYPE) + { + return Err(anyhow!( + "frontier Research replay does not preserve its exact accepted decision" + )); + } + return Ok(receipt); + } + } + if current_frontier_research_review_job_id(store_path)?.as_deref() != Some(job_id) { + return Err(anyhow!( + "frontier Research result is not current review work" + )); + } + let job = cache + .get::(job_id)? + .ok_or_else(|| anyhow!("frontier Research acceptance lost its runtime job"))?; + if job.status != crate::EpiphanyRuntimeJobStatus::Completed { + return Err(anyhow!("frontier Research runtime job is not completed")); + } + let launch = cache + .get::(job_id)? + .ok_or_else(|| anyhow!("frontier Research acceptance lost its launch"))?; + let request = crate::runtime_spine::frontier_research_request_for_launch(&cache, &launch)? + .ok_or_else(|| anyhow!("frontier Research acceptance lost its exact request"))?; + let fulfillment = crate::runtime_typed_request_fulfillment( + store_path, + crate::RuntimeTypedRequestRef::FrontierResearch(&request.request_id), + )? + .ok_or_else(|| anyhow!("frontier Research result is not exact typed fulfillment"))?; + if fulfillment.job_id != job_id { + return Err(anyhow!( + "frontier Research fulfillment belongs to another attempt" + )); + } + let result = cache + .get::(job_id)? + .ok_or_else(|| anyhow!("frontier Research acceptance lost its typed result"))?; + if !result.role_id.eq_ignore_ascii_case("research") + || result.repo_frontier_research_request_id.as_deref() != Some(request.request_id.as_str()) + || result.item_error.is_some() + { + return Err(anyhow!("frontier Research result crossed family authority")); + } + let patch = result + .state_patch()? + .ok_or_else(|| anyhow!("frontier Research result has no keyed state proposal"))?; + let policy_errors = crate::research_role_state_patch_policy_errors(&patch); + if !policy_errors.is_empty() { + return Err(anyhow!( + "frontier Research state proposal is invalid: {}", + policy_errors.join("; ") + )); + } + let evidence_ids = patch + .evidence + .iter() + .map(|evidence| evidence.id.as_str()) + .collect::>(); + if evidence_ids.len() != patch.evidence.len() + || patch.observations.iter().any(|observation| { + observation.evidence_ids.is_empty() + || observation + .evidence_ids + .iter() + .any(|id| !evidence_ids.contains(id.as_str())) + }) + { + return Err(anyhow!( + "frontier Research observations do not cite the exact proposed evidence set" + )); + } + let lookups = + crate::runtime_authenticated_public_source_lookups_for_worker(store_path, job_id)?; + for lookup in &lookups { + if !result.evidence_ids.contains(&lookup.receipt_id) + || !result.files_inspected.contains(&lookup.source_ref) + { + return Err(anyhow!( + "frontier Research result omitted authenticated public-source provenance" + )); + } + } + if patch + .evidence + .iter() + .any(|evidence| !result.evidence_ids.contains(&evidence.id)) + { + return Err(anyhow!( + "frontier Research result did not expose every proposed evidence identity" + )); + } + let finding = crate::interpret_runtime_role_worker_result( + crate::EpiphanyRoleResultRoleId::Research, + &result, + ); + let packet_id = format!("eyes-packet-{}", request.request_id); + let packet = crate::eyes_evidence_packet_from_research_finding( + packet_id.clone(), + request.request_id.clone(), + result.decision_context_id.clone(), + &finding, + &patch, + &lookups, + accepted_at.to_string(), + ); + let mut writes = Vec::new(); + for evidence in &patch.evidence { + writes.push(crate::mind_documents::prepare_mind_document( + &cache, + &evidence.id, + &crate::EpiphanyMindEvidenceDocument { + value: evidence.clone(), + }, + )?); + } + for observation in &patch.observations { + writes.push(crate::mind_documents::prepare_mind_document( + &cache, + &observation.id, + &crate::EpiphanyMindObservationDocument { + value: observation.clone(), + }, + )?); + } + if let Some(checkpoint) = patch.investigation_checkpoint.as_ref() { + writes.push(crate::mind_documents::prepare_mind_document( + &cache, + &checkpoint.checkpoint_id, + &crate::EpiphanyMindInvestigationCheckpointDocument { + value: checkpoint.clone(), + }, + )?); + } + let snapshot = cache.snapshot_envelopes(); + let mut strong_reads = Vec::new(); + for (document_type, document_key) in [ + ( + crate::RepoFrontierResearchRequest::TYPE, + request.request_id.as_str(), + ), + (crate::EpiphanyRuntimeWorkerLaunchRequest::TYPE, job_id), + (crate::EpiphanyRuntimeJob::TYPE, job_id), + (crate::EpiphanyRuntimeRoleWorkerResult::TYPE, job_id), + ( + crate::EpiphanyDecisionContext::TYPE, + result.decision_context_id.as_str(), + ), + ] { + strong_reads.push( + snapshot + .iter() + .find(|value| value.r#type == document_type && value.key == document_key) + .cloned() + .ok_or_else(|| anyhow!("frontier Research admission lost a strong source"))?, + ); + } + for source in &request.frontier_authority_documents { + let envelope = snapshot + .iter() + .find(|value| value.r#type == source.document_type && value.key == source.document_key) + .ok_or_else(|| anyhow!("frontier Research admission lost frontier authority"))?; + if EpiphanyMindDocumentVersion::from_envelope("epiphany-mind", envelope)? != *source { + return Err(anyhow!( + "frontier Research output cannot be rebased onto changed frontier authority" + )); + } + strong_reads.push(envelope.clone()); + } + let packet_envelope = cache.prepare_entry(&packet_id, &packet)?.0; + match crate::reasoning_context::commit_mind_mutation_with_derived_companions( + store_path, + &result.decision_context_id, + "Eyes.frontier_research", + strong_reads, + writes, + vec![packet_envelope], + accepted_at, + )? { + crate::EpiphanyMindCommitOutcome::Committed(receipt) => Ok(receipt), + crate::EpiphanyMindCommitOutcome::Conflict { + document_identities, + } => Err(anyhow!( + "frontier Research admission lost exact keyed reads: {document_identities:?}" + )), + } +} + pub fn accept_body_modeling_result( store_path: impl AsRef, job_id: &str, @@ -1516,7 +1892,7 @@ mod tests { } #[test] - fn keyed_modeling_lifecycles_ignore_thread_state() -> Result<()> { + fn keyed_modeling_and_research_lifecycles_ignore_thread_state() -> Result<()> { let temp = tempfile::tempdir()?; let store = temp.path().join("body-decision.cc"); crate::initialize_runtime_spine( @@ -1686,6 +2062,7 @@ mod tests { frontier_route_id: None, repo_frontier_modeling_request_id: None, proposal_modeling_request_id: None, + repo_frontier_research_request_id: None, frontier_planning_request_id: None, frontier_plan_candidate_msgpack: None, frontier_plan_mind_request_id: None, @@ -1938,6 +2315,7 @@ mod tests { frontier_route_id: None, repo_frontier_modeling_request_id: None, proposal_modeling_request_id: Some(request.request_id.clone()), + repo_frontier_research_request_id: None, frontier_planning_request_id: None, frontier_plan_candidate_msgpack: None, frontier_plan_mind_request_id: None, @@ -2080,6 +2458,7 @@ mod tests { frontier_route_id: Some(route.route_id.clone()), repo_frontier_modeling_request_id: None, proposal_modeling_request_id: None, + repo_frontier_research_request_id: None, frontier_planning_request_id: None, frontier_plan_candidate_msgpack: None, frontier_plan_mind_request_id: None, @@ -2275,6 +2654,7 @@ mod tests { frontier_route_id: None, repo_frontier_modeling_request_id: Some(verdict_request.request_id.clone()), proposal_modeling_request_id: None, + repo_frontier_research_request_id: None, frontier_planning_request_id: None, frontier_plan_candidate_msgpack: None, frontier_plan_mind_request_id: None, @@ -2408,6 +2788,367 @@ mod tests { .iter() .any(|node| node.id == "concurrent-unrelated-node") ); + + // Eyes is its own exact current-work family. It launches from an + // explicit external-evidence obligation, survives unrelated keyed + // Mind commits, and conflicts if that exact frontier changes. + let eyes_frontier = RepoFrontierItem { + id: "eyes-frontier".into(), + migration_body: "epiphany".into(), + question: "What external evidence resolves this claim?".into(), + gap: "The current Body cannot answer this external question.".into(), + target_claim_ids: vec!["body-node".into()], + source_scope: vec!["epiphany-core/src".into()], + recommended_next_organ: "Eyes".into(), + status: RepoFrontierStatus::Active, + evidence_refs: vec![verdict.receipt_id.clone()], + ..Default::default() + }; + let eyes_proposal = crate::EpiphanyRepoModelMutationProposal::new( + "repo-model-mutation-proposal-eyes-frontier", + "eyes-frontier-fixture-request", + "eyes-frontier-fixture-result", + vec![verdict.receipt_id.clone()], + body.clone(), + vec![crate::EpiphanyRepoModelMutationOperation::PutFrontier { + item: eyes_frontier.clone(), + }], + )?; + let eyes_plan = crate::plan_repo_model_mutation(&store, &eyes_proposal)?; + let mut eyes_cache = crate::runtime_spine_cache(&store)?; + eyes_cache.pull_all_backing_stores()?; + let eyes_provenance = eyes_cache + .get_envelope::(&verdict.receipt_id)? + .expect("typed provenance for Eyes obligation"); + assert!(matches!( + crate::commit_typed_organ_mind_mutation( + &store, + "Modeling", + eyes_provenance, + "Modeling.external_evidence_obligation_fixture", + eyes_plan.strong_reads, + eyes_plan.writes, + "2026-08-17T00:00:22Z", + )?, + crate::EpiphanyMindCommitOutcome::Committed(_) + )); + assert_eq!( + project_current_work(&store)?.research_continuation_action, + Some(RepoFrontierResearchContinuationAction::LaunchResearch) + ); + let research_job = launch_current_frontier_research_work(&store, "2026-08-17T00:00:23Z")?; + let mut research_cache = crate::runtime_spine_cache(&store)?; + research_cache.pull_all_backing_stores()?; + assert!( + research_cache + .get::(crate::THREAD_STATE_KEY)? + .is_none() + ); + let research_launch = research_cache + .get::(&research_job)? + .expect("exact Research launch"); + let research_request_id = research_launch + .repo_frontier_research_request_id + .clone() + .expect("exact Research request identity"); + let research_request = research_cache + .get::(&research_request_id)? + .expect("exact Research request"); + assert_eq!(research_request.frontier_item_id, eyes_frontier.id); + assert!( + research_request.frontier_authority_documents.len() + < research_request.model_source_documents.len() + ); + assert_eq!( + project_current_work(&store)?.research_continuation_action, + None + ); + let research_basis = crate::worker_reasoning_basis(&store, &research_launch)?; + crate::put_reasoning_basis(&store, &research_basis)?; + let mut research_native = epiphany_model_adapter::EpiphanyModelRequest::new( + "research-request", + "research-conversation", + "openai-codex", + "gpt-test", + "model", + ); + research_native.reasoning_basis_id = Some(research_basis.basis_id.clone()); + research_native.source_worker_job_id = Some(research_job.clone()); + let research_provider = epiphany_openai_adapter::request_from_native(&research_native); + let research_context = crate::EpiphanyDecisionContext::new( + &research_basis, + research_native, + research_provider, + Vec::new(), + )?; + research_cache.put(&research_context.context_id, &research_context)?; + let stale_research_store = temp.path().join("stale-research-output.cc"); + std::fs::copy(&store, &stale_research_store)?; + let evidence = epiphany_state_model::EpiphanyEvidenceRecord { + id: "research-evidence-1".into(), + kind: "external-source".into(), + status: "ok".into(), + summary: "The bounded external claim is supported.".into(), + code_refs: vec![epiphany_state_model::EpiphanyCodeRef { + path: "epiphany-core/src/current_work.rs".into(), + start_line: Some(1), + end_line: None, + symbol: Some("accept_frontier_research_result".into()), + note: None, + }], + }; + let observation = epiphany_state_model::EpiphanyObservation { + id: "research-observation-1".into(), + summary: "External evidence answers the exact frontier question.".into(), + source_kind: "research".into(), + status: "ok".into(), + code_refs: Vec::new(), + evidence_ids: vec![evidence.id.clone()], + }; + let research_patch = crate::EpiphanyRoleStatePatchDocument { + evidence: vec![evidence.clone()], + observations: vec![observation.clone()], + ..Default::default() + }; + let research_result = crate::EpiphanyRuntimeRoleWorkerResult { + schema_version: crate::RUNTIME_ROLE_WORKER_RESULT_SCHEMA_VERSION.into(), + result_id: "research-result".into(), + job_id: research_job.clone(), + role_id: "research".into(), + verdict: "evidence-ready".into(), + summary: "gathered exact external evidence".into(), + next_safe_move: "admit evidence".into(), + checkpoint_summary: None, + scratch_summary: None, + files_inspected: vec!["epiphany-core/src/current_work.rs".into()], + frontier_node_ids: vec!["body-node".into()], + evidence_ids: vec![evidence.id.clone()], + artifact_refs: Vec::new(), + open_questions: Vec::new(), + evidence_gaps: Vec::new(), + risks: Vec::new(), + state_patch_msgpack: Some(rmp_serde::to_vec_named(&research_patch)?), + self_patch_msgpack: None, + item_error: None, + metadata: Default::default(), + repo_model_mutation_proposal_msgpack: None, + verification_request_id: None, + frontier_route_id: None, + repo_frontier_modeling_request_id: None, + proposal_modeling_request_id: None, + repo_frontier_research_request_id: Some(research_request_id.clone()), + frontier_planning_request_id: None, + frontier_plan_candidate_msgpack: None, + frontier_plan_mind_request_id: None, + frontier_plan_mind_decision_msgpack: None, + repository_body_observation_basis: None, + imagination_consideration_request_id: None, + imagination_consideration_candidate_msgpack: None, + admitted_model_direction_consideration_request_id: None, + admitted_model_direction_consideration_result_msgpack: None, + decision_context_id: research_context.context_id.clone(), + }; + let research_process = crate::ProcessInstanceIdentity { + process_id: 44, + creation_token: 9, + created_at_rfc3339: Some("2026-08-17T00:00:24Z".into()), + executable_path: "research-worker".into(), + }; + let research_activation = "research-activation"; + crate::claim_runtime_worker_process( + &store, + &research_job, + &research_process, + &format!("{:x}", Sha256::digest(research_activation.as_bytes())), + "2026-08-17T00:00:24Z", + )?; + crate::activate_runtime_worker_process( + &store, + &research_job, + &research_process, + research_activation, + "2026-08-17T00:00:25Z", + )?; + crate::put_runtime_role_worker_result(&store, &research_result)?; + crate::complete_runtime_job( + &store, + crate::RuntimeSpineJobResultOptions { + result_id: format!("runtime-result-{research_job}"), + job_id: research_job.clone(), + completed_at: "2026-08-17T00:00:26Z".into(), + verdict: research_result.verdict.clone(), + summary: research_result.summary.clone(), + next_safe_move: research_result.next_safe_move.clone(), + evidence_refs: research_result.evidence_ids.clone(), + artifact_refs: Vec::new(), + decision_context_id: Some(research_context.context_id.clone()), + }, + )?; + assert_eq!( + project_current_work(&store)?.research_continuation_action, + Some(RepoFrontierResearchContinuationAction::ReviewResearchResult) + ); + + let mut unrelated_research_node = final_model + .nodes + .iter() + .find(|node| node.id == "body-node") + .cloned() + .expect("seeded Body node"); + unrelated_research_node.id = "research-concurrent-node".into(); + unrelated_research_node.title = "Research concurrent node".into(); + unrelated_research_node.claim = "Unrelated Mind work does not stale Eyes".into(); + let disjoint_research_proposal = crate::EpiphanyRepoModelMutationProposal::new( + "repo-model-mutation-proposal-research-concurrent", + "research-concurrent-request", + "research-concurrent-result", + vec![verdict.receipt_id.clone()], + body.clone(), + vec![crate::EpiphanyRepoModelMutationOperation::PutNode { + node: unrelated_research_node, + }], + )?; + let disjoint_research_plan = + crate::plan_repo_model_mutation(&store, &disjoint_research_proposal)?; + let mut disjoint_cache = crate::runtime_spine_cache(&store)?; + disjoint_cache.pull_all_backing_stores()?; + let disjoint_provenance = disjoint_cache + .get_envelope::(&verdict.receipt_id)? + .expect("typed provenance for disjoint Research mutation"); + assert!(matches!( + crate::commit_typed_organ_mind_mutation( + &store, + "Modeling", + disjoint_provenance, + "Modeling.concurrent_with_research_fixture", + disjoint_research_plan.strong_reads, + disjoint_research_plan.writes, + "2026-08-17T00:00:26.500Z", + )?, + crate::EpiphanyMindCommitOutcome::Committed(_) + )); + + let mut competing_eyes_frontier = eyes_frontier.clone(); + competing_eyes_frontier.gap = "The exact external question changed.".into(); + competing_eyes_frontier.updated_at = Some("2026-08-17T00:00:27Z".into()); + let competing_eyes_proposal = crate::EpiphanyRepoModelMutationProposal::new( + "repo-model-mutation-proposal-competing-eyes-frontier", + "competing-eyes-frontier-request", + "competing-eyes-frontier-result", + vec![verdict.receipt_id.clone()], + body.clone(), + vec![crate::EpiphanyRepoModelMutationOperation::PutFrontier { + item: competing_eyes_frontier, + }], + )?; + let competing_eyes_plan = + crate::plan_repo_model_mutation(&stale_research_store, &competing_eyes_proposal)?; + let mut stale_research_cache = crate::runtime_spine_cache(&stale_research_store)?; + stale_research_cache.pull_all_backing_stores()?; + let competing_eyes_provenance = stale_research_cache + .get_envelope::(&verdict.receipt_id)? + .expect("copied typed provenance"); + assert!(matches!( + crate::commit_typed_organ_mind_mutation( + &stale_research_store, + "Modeling", + competing_eyes_provenance, + "Modeling.competing_eyes_frontier_fixture", + competing_eyes_plan.strong_reads, + competing_eyes_plan.writes, + "2026-08-17T00:00:27Z", + )?, + crate::EpiphanyMindCommitOutcome::Committed(_) + )); + let stale_process = crate::ProcessInstanceIdentity { + process_id: 45, + creation_token: 10, + created_at_rfc3339: Some("2026-08-17T00:00:27Z".into()), + executable_path: "stale-research-worker".into(), + }; + let stale_activation = "stale-research-activation"; + crate::claim_runtime_worker_process( + &stale_research_store, + &research_job, + &stale_process, + &format!("{:x}", Sha256::digest(stale_activation.as_bytes())), + "2026-08-17T00:00:27Z", + )?; + crate::activate_runtime_worker_process( + &stale_research_store, + &research_job, + &stale_process, + stale_activation, + "2026-08-17T00:00:27.100Z", + )?; + crate::put_runtime_role_worker_result(&stale_research_store, &research_result)?; + crate::complete_runtime_job( + &stale_research_store, + crate::RuntimeSpineJobResultOptions { + result_id: format!("runtime-result-{research_job}"), + job_id: research_job.clone(), + completed_at: "2026-08-17T00:00:27.200Z".into(), + verdict: research_result.verdict.clone(), + summary: research_result.summary.clone(), + next_safe_move: research_result.next_safe_move.clone(), + evidence_refs: research_result.evidence_ids.clone(), + artifact_refs: Vec::new(), + decision_context_id: Some(research_context.context_id.clone()), + }, + )?; + assert_eq!( + crate::runtime_role_worker_result(&stale_research_store, &research_job)? + .expect("stale output remains a durable decision") + .decision_context_id, + research_context.context_id + ); + let mut research_conflict_before = crate::runtime_spine_cache(&stale_research_store)?; + research_conflict_before.pull_all_backing_stores()?; + let research_conflict_before = research_conflict_before.snapshot_envelopes(); + assert!( + accept_frontier_research_result( + &stale_research_store, + &research_job, + "2026-08-17T00:00:28Z", + ) + .is_err() + ); + let mut research_conflict_after = crate::runtime_spine_cache(&stale_research_store)?; + research_conflict_after.pull_all_backing_stores()?; + assert_eq!( + research_conflict_after.snapshot_envelopes(), + research_conflict_before + ); + + let research_commit = + accept_frontier_research_result(&store, &research_job, "2026-08-17T00:00:28Z")?; + assert_eq!(research_commit.invariant_owner, "Eyes.frontier_research"); + assert_eq!( + accept_frontier_research_result(&store, &research_job, "2026-08-17T00:00:29Z")?, + research_commit + ); + let mut admitted_research_cache = crate::runtime_spine_cache(&store)?; + admitted_research_cache.pull_all_backing_stores()?; + assert!( + admitted_research_cache + .get::(&evidence.id)? + .is_some() + ); + assert!( + admitted_research_cache + .get::(&observation.id)? + .is_some() + ); + let packet = admitted_research_cache + .get::(&format!("eyes-packet-{research_request_id}"))? + .expect("request-bound Eyes packet"); + assert_eq!(packet.research_request_id, research_request_id); + assert_eq!(packet.decision_context_id, research_context.context_id); + let after_research = project_current_work(&store)?; + assert!(after_research.research_continuation_action.is_none()); + assert!(after_research.body_modeling_action.is_none()); + assert!(after_research.proposal_modeling.is_none()); + assert!(after_research.frontier_verdict_modeling.is_none()); Ok(()) } } diff --git a/epiphany-core/src/eyes_gateway.rs b/epiphany-core/src/eyes_gateway.rs index 528ede33..3cb22fb7 100644 --- a/epiphany-core/src/eyes_gateway.rs +++ b/epiphany-core/src/eyes_gateway.rs @@ -13,7 +13,7 @@ pub const EYES_EVIDENCE_REQUEST_SCHEMA_VERSION: &str = "epiphany.eyes.evidence_r pub const EYES_EVIDENCE_REVIEW_SCHEMA_VERSION: &str = "epiphany.eyes.evidence_review.v0"; pub const EYES_SOURCE_LOOKUP_RECEIPT_SCHEMA_VERSION: &str = "epiphany.eyes.source_lookup_receipt.v0"; -pub const EYES_EVIDENCE_PACKET_SCHEMA_VERSION: &str = "epiphany.eyes.evidence_packet.v0"; +pub const EYES_EVIDENCE_PACKET_SCHEMA_VERSION: &str = "epiphany.eyes.evidence_packet.v1"; pub const EYES_EVIDENCE_REFUSAL_RECEIPT_SCHEMA_VERSION: &str = "epiphany.eyes.evidence_refusal_receipt.v0"; @@ -84,6 +84,10 @@ pub struct EyesEvidencePacket { pub contract: String, #[cultcache(key = 12, default)] pub source_lookup_receipt_ids: Vec, + #[cultcache(key = 13)] + pub research_request_id: String, + #[cultcache(key = 14)] + pub decision_context_id: String, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] @@ -169,6 +173,8 @@ pub fn default_eyes_cultnet_contracts() -> Vec { pub fn eyes_evidence_packet_from_research_finding( packet_id: String, + research_request_id: String, + decision_context_id: String, finding: &EpiphanyRoleFindingInterpretation, patch: &EpiphanyRoleStatePatchDocument, source_lookups: &[EyesSourceLookupReceipt], @@ -235,6 +241,8 @@ pub fn eyes_evidence_packet_from_research_finding( .iter() .map(|lookup| lookup.receipt_id.clone()) .collect(), + research_request_id, + decision_context_id, } } @@ -332,12 +340,16 @@ mod tests { let packet = eyes_evidence_packet_from_research_finding( "eyes-packet-1".to_string(), + "research-request-1".to_string(), + "decision-context-1".to_string(), &finding, &patch, &[], "2026-05-30T00:00:00Z".to_string(), ); assert_eq!(packet.source_role_id, "research"); + assert_eq!(packet.research_request_id, "research-request-1"); + assert_eq!(packet.decision_context_id, "decision-context-1"); assert!(packet.evidence_ids.contains(&"ev-source".to_string())); assert!(packet.observation_ids.contains(&"obs-source".to_string())); assert!( diff --git a/epiphany-core/src/mind_documents.rs b/epiphany-core/src/mind_documents.rs index 2f407c54..fba29daf 100644 --- a/epiphany-core/src/mind_documents.rs +++ b/epiphany-core/src/mind_documents.rs @@ -234,6 +234,20 @@ pub(crate) fn register_mind_document_types(cache: &mut CultCache) -> Result<()> Ok(()) } +pub(crate) fn prepare_mind_document( + cache: &CultCache, + key: impl Into, + value: &T, +) -> Result { + if !T::TYPE.starts_with("epiphany.mind.") { + return Err(anyhow!( + "Mind document preparation refuses non-Mind type {:?}", + T::TYPE + )); + } + Ok(cache.prepare_entry_named(key, value)?.0) +} + 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 { diff --git a/epiphany-core/src/reasoning_context.rs b/epiphany-core/src/reasoning_context.rs index 5920a646..8a6d8568 100644 --- a/epiphany-core/src/reasoning_context.rs +++ b/epiphany-core/src/reasoning_context.rs @@ -549,11 +549,16 @@ pub fn put_reasoning_basis( return Err(anyhow!("role reasoning basis cites a non-role launch")); }; let current_mind = crate::assemble_mind_view(store_path)?; + let modeling_body_mismatch = projection + .authority + .role_id + .eq_ignore_ascii_case("modeling") + && projection.authority.repository_body_observation_basis + != current_mind.repository_body_observation; if projection.authority != document.into() || projection.mind != current_mind.clone().into() || basis.source_documents != current_mind.source_documents - || projection.authority.repository_body_observation_basis - != current_mind.repository_body_observation + || modeling_body_mismatch { return Err(anyhow!( "role reasoning projection diverges from its exact launch or keyed Mind sources" @@ -797,6 +802,38 @@ pub fn commit_mind_mutation( ) } +pub(crate) fn commit_mind_mutation_with_derived_companions( + store_path: &Path, + decision_context_id: &str, + invariant_owner: &str, + strong_reads: Vec, + writes: Vec, + derived_companions: Vec, + committed_at: &str, +) -> Result { + require_non_empty(decision_context_id, "Mind mutation decision context id")?; + let mut cache = runtime_spine_cache(store_path)?; + cache.pull_all_backing_stores()?; + let context = cache + .get::(decision_context_id)? + .ok_or_else(|| anyhow!("Mind mutation decision context does not exist"))?; + let basis = cache + .get::(&context.basis_id)? + .ok_or_else(|| anyhow!("Mind mutation decision context lost its basis"))?; + context.validate(&basis)?; + commit_authorized_mind_mutation( + store_path, + EpiphanyMindCommitAuthority::ModelDecisionContext { + decision_context_id: decision_context_id.to_string(), + }, + invariant_owner, + strong_reads, + writes, + derived_companions, + committed_at, + ) +} + pub fn commit_operator_mind_mutation( store_path: &Path, provenance: CultCacheEnvelope, diff --git a/epiphany-core/src/repo_model_gateway.rs b/epiphany-core/src/repo_model_gateway.rs index a9d7cba0..6174c84f 100644 --- a/epiphany-core/src/repo_model_gateway.rs +++ b/epiphany-core/src/repo_model_gateway.rs @@ -72,9 +72,9 @@ pub const LEGACY_REPO_FRONTIER_PLAN_DECISION_RECEIPT_SCHEMA_VERSION: &str = pub const REPO_FRONTIER_PLAN_DECISION_CONTRACT: &str = "epiphany.repo_frontier_plan_decision.v0"; pub const REPO_FRONTIER_PLANNING_CONTRACT: &str = "epiphany.repo_frontier_planning.v2"; pub const REPO_FRONTIER_RESEARCH_REQUEST_SCHEMA_VERSION: &str = - "epiphany.self.repo_frontier_research_request.v2"; + "epiphany.self.repo_frontier_research_request.v3"; pub const REPO_FRONTIER_RESEARCH_REQUEST_CONTRACT: &str = - "epiphany.repo_frontier_research_request.v2"; + "epiphany.repo_frontier_research_request.v3"; pub const REPO_FRONTIER_WORK_PROPOSAL_CONTRACT: &str = "epiphany.repo_frontier_work_proposal.inert.v0"; pub const REPO_FRONTIER_AUTONOMOUS_PROPOSAL_BINDING_SCHEMA_VERSION: &str = @@ -493,8 +493,6 @@ pub struct RepoFrontierResearchRequest { pub requested_at: String, #[cultcache(key = 8)] pub runtime_id: String, - #[cultcache(key = 9)] - pub thread_id: String, #[cultcache(key = 10)] pub contract: String, /// Immutable public source identities already owned by the admitted @@ -502,6 +500,11 @@ pub struct RepoFrontierResearchRequest { /// evidence or a search hint. #[cultcache(key = 11, default)] pub public_source_refs: Vec, + /// Exact current documents that own whether this work remains actionable. + /// The complete model sources above are immutable audit cargo; unrelated + /// keyed Mind changes must not stale this request. + #[cultcache(key = 12)] + pub frontier_authority_documents: Vec, } #[derive(Debug, Clone, PartialEq, Eq, DatabaseEntry)] diff --git a/epiphany-core/src/repository_body_observer.rs b/epiphany-core/src/repository_body_observer.rs index 044c7c59..945025ff 100644 --- a/epiphany-core/src/repository_body_observer.rs +++ b/epiphany-core/src/repository_body_observer.rs @@ -424,7 +424,8 @@ pub fn admit_repository_body_observation( } return Ok(existing); } - let write = cache.prepare_entry(&basis.observation_id, &document)?.0; + let write = + crate::mind_documents::prepare_mind_document(&cache, &basis.observation_id, &document)?; match crate::commit_external_typed_observation_mind_mutation( runtime_store, "Body", diff --git a/epiphany-core/src/runtime_spine.rs b/epiphany-core/src/runtime_spine.rs index 9c21f0d0..852ff8b5 100644 --- a/epiphany-core/src/runtime_spine.rs +++ b/epiphany-core/src/runtime_spine.rs @@ -620,6 +620,8 @@ pub struct EpiphanyRuntimeRoleWorkerResult { pub repo_frontier_modeling_request_id: Option, #[cultcache(key = 24, default)] pub proposal_modeling_request_id: Option, + #[cultcache(key = 25, default)] + pub repo_frontier_research_request_id: Option, #[cultcache(key = 26, default)] pub frontier_planning_request_id: Option, #[cultcache(key = 27, default)] @@ -3851,7 +3853,7 @@ pub fn runtime_requested_public_source_refs_for_worker( .unwrap_or_default()) } -fn frontier_research_request_for_launch( +pub(crate) fn frontier_research_request_for_launch( cache: &CultCache, launch: &EpiphanyRuntimeWorkerLaunchRequest, ) -> Result> { @@ -3870,38 +3872,8 @@ fn frontier_research_request_for_launch( .get::(request_id)? .ok_or_else(|| anyhow!("frontier Research launch lost its typed request"))?; let runtime = require_identity(cache)?; - let expected_request_id = crate::frontier_research_request_id( - &request.runtime_id, - &request.frontier_item_id, - &request.frontier_item_hash, - ); - if request.schema_version != REPO_FRONTIER_RESEARCH_REQUEST_SCHEMA_VERSION - || request.contract != REPO_FRONTIER_RESEARCH_REQUEST_CONTRACT - || request.request_id != request_id - || request.request_id != expected_request_id - || request.runtime_id != runtime.runtime_id - || (crate::EpiphanyRepoModelBasis { - projection_digest: request.model_projection_digest.clone(), - source_documents: request.model_source_documents.clone(), - }) - .validate_against_cache(cache) - .is_err() - || request.frontier_item_id.is_empty() - || request.frontier_item_hash.is_empty() - || request.thread_id.is_empty() - || chrono::DateTime::parse_from_rfc3339(&request.requested_at).is_err() - || request.source_scope.is_empty() - || !safe_sorted_unique_paths(&request.source_scope) - || !request - .public_source_refs - .windows(2) - .all(|pair| pair[0] < pair[1]) - || !request.public_source_refs.iter().all(|source_ref| { - crate::ImmutableGithubSource::parse(source_ref) - .map(|source| source.to_string() == *source_ref) - .unwrap_or(false) - }) - { + validate_repo_frontier_research_request(&request)?; + if request.request_id != request_id || request.runtime_id != runtime.runtime_id { return Err(anyhow!( "frontier Research launch names an invalid typed request" )); @@ -4606,6 +4578,29 @@ pub fn put_runtime_role_worker_result( "Modeling result must exactly preserve its runtime-owned request authority" )); } + let is_frontier_research = worker_launch.repo_frontier_research_request_id.is_some(); + if is_frontier_research + != result + .repo_frontier_research_request_id + .as_ref() + .is_some_and(|id| !id.trim().is_empty()) + || result.repo_frontier_research_request_id + != worker_launch.repo_frontier_research_request_id + || (is_frontier_research && !result.role_id.eq_ignore_ascii_case("research")) + || (!is_frontier_research && result.repo_frontier_research_request_id.is_some()) + { + return Err(anyhow!( + "Research result must exactly preserve its runtime-owned frontier request" + )); + } + if is_frontier_research { + let request = frontier_research_request_for_launch(&cache, &worker_launch)? + .ok_or_else(|| anyhow!("Research result lost its typed request"))?; + if result.repo_frontier_research_request_id.as_deref() != Some(request.request_id.as_str()) + { + return Err(anyhow!("Research result substituted its typed request")); + } + } if is_modeling && (result.verification_request_id.is_some() || result.frontier_route_id.is_some()) { @@ -5435,6 +5430,7 @@ pub fn runtime_typed_request_fulfillment( validate_proposal_modeling_worker_fulfillment(&cache, result)?; } RuntimeTypedRequestRef::FrontierVerdictModeling(_) => {} + RuntimeTypedRequestRef::FrontierResearch(_) => {} RuntimeTypedRequestRef::ImaginationConsideration(_) => { let candidate = result .imagination_consideration_candidate()? @@ -7468,6 +7464,184 @@ pub fn runtime_has_actionable_eyes_frontier(runtime_store: impl AsRef) -> runtime_has_actionable_frontier_for_organ(runtime_store, "Eyes") } +fn research_frontier_authority_documents( + cache: &CultCache, + item: &crate::RepoFrontierItem, +) -> Result> { + let mut ids = vec![item.id.as_str()]; + ids.extend(item.dependency_item_ids.iter().map(String::as_str)); + ids.sort_unstable(); + ids.dedup(); + let mut documents = ids + .into_iter() + .map(|id| { + let envelope = cache + .get_envelope::(id)? + .ok_or_else(|| anyhow!("Research frontier authority document {id:?} is absent"))?; + crate::EpiphanyMindDocumentVersion::from_envelope("epiphany-mind", &envelope) + }) + .collect::>>()?; + documents.sort_by(|left, right| { + left.document_type + .cmp(&right.document_type) + .then(left.document_key.cmp(&right.document_key)) + }); + Ok(documents) +} + +fn research_authority_envelopes( + cache: &CultCache, + documents: &[crate::EpiphanyMindDocumentVersion], +) -> Result> { + let snapshot = cache.snapshot_envelopes(); + documents + .iter() + .map(|document| { + if document.store_id != "epiphany-mind" + || document.document_type != crate::EpiphanyRepoModelFrontierDocument::TYPE + { + return Err(anyhow!( + "Research authority contains a non-frontier Mind document" + )); + } + let envelope = snapshot + .iter() + .find(|envelope| { + envelope.r#type == document.document_type + && envelope.key == document.document_key + }) + .ok_or_else(|| anyhow!("Research frontier authority document is absent"))?; + if crate::EpiphanyMindDocumentVersion::from_envelope("epiphany-mind", envelope)? + != *document + { + return Err(anyhow!("Research frontier authority document changed")); + } + Ok(envelope.clone()) + }) + .collect() +} + +fn validate_repo_frontier_research_request( + request: &RepoFrontierResearchRequest, +) -> Result { + if request.schema_version != REPO_FRONTIER_RESEARCH_REQUEST_SCHEMA_VERSION + || request.contract != REPO_FRONTIER_RESEARCH_REQUEST_CONTRACT + || request.runtime_id.is_empty() + || chrono::DateTime::parse_from_rfc3339(&request.requested_at).is_err() + || request.frontier_item_id.is_empty() + || request.frontier_item_hash.is_empty() + || request.source_scope.is_empty() + || !safe_sorted_unique_paths(&request.source_scope) + || request.frontier_authority_documents.is_empty() + { + return Err(anyhow!("invalid frontier Research request")); + } + let basis = crate::EpiphanyRepoModelBasis { + projection_digest: request.model_projection_digest.clone(), + source_documents: request.model_source_documents.clone(), + }; + basis.validate()?; + if request.frontier_authority_documents.iter().any(|document| { + document.validate().is_err() + || document.store_id != "epiphany-mind" + || document.document_type != crate::EpiphanyRepoModelFrontierDocument::TYPE + || !request.model_source_documents.contains(document) + }) || !request.frontier_authority_documents.windows(2).all(|pair| { + ( + pair[0].document_type.as_str(), + pair[0].document_key.as_str(), + ) < ( + pair[1].document_type.as_str(), + pair[1].document_key.as_str(), + ) + }) { + return Err(anyhow!( + "Research frontier authority is not one canonical subset of its sealed model projection" + )); + } + let frontier_source = request + .frontier_authority_documents + .iter() + .find(|document| document.document_key == request.frontier_item_id) + .ok_or_else(|| anyhow!("Research request lost its owning frontier document"))?; + let frontier = rmp_serde::from_slice::( + &frontier_source.payload_msgpack, + )? + .value()?; + let mut expected_authority_ids = vec![frontier.id.as_str()]; + expected_authority_ids.extend(frontier.dependency_item_ids.iter().map(String::as_str)); + expected_authority_ids.sort_unstable(); + expected_authority_ids.dedup(); + if request + .frontier_authority_documents + .iter() + .map(|document| document.document_key.as_str()) + .collect::>() + != expected_authority_ids + || request.frontier_item_id != frontier.id + || request.frontier_item_hash != repo_frontier_item_hash(&frontier)? + || request.source_scope != frontier.source_scope + || request.public_source_refs + != crate::ImmutableGithubSource::canonicalize_set( + frontier.public_source_refs.iter().map(String::as_str), + )? + || request.public_source_refs != frontier.public_source_refs + || request.request_id + != crate::frontier_research_request_id( + &request.runtime_id, + &request.frontier_item_id, + &request.frontier_item_hash, + ) + { + return Err(anyhow!( + "frontier Research request diverges from its sealed frontier authority" + )); + } + Ok(frontier) +} + +fn current_repo_frontier_research_request( + cache: &CultCache, + request: &RepoFrontierResearchRequest, +) -> Result> { + let sealed_frontier = validate_repo_frontier_research_request(request)?; + let identity = require_identity(cache)?; + if request.runtime_id != identity.runtime_id { + return Ok(None); + } + if research_authority_envelopes(cache, &request.frontier_authority_documents).is_err() { + return Ok(None); + } + let frontier = cache + .get::(&request.frontier_item_id)? + .ok_or_else(|| anyhow!("Research request frontier document is absent"))? + .value()?; + let expected_authority = research_frontier_authority_documents(cache, &frontier)?; + if expected_authority != request.frontier_authority_documents { + return Ok(None); + } + let dependencies_ready = frontier.dependency_item_ids.iter().all(|dependency_id| { + cache + .get::(dependency_id) + .ok() + .flatten() + .and_then(|document| document.value().ok()) + .is_some_and(|dependency| { + matches!( + dependency.status, + crate::RepoFrontierStatus::Resolved + | crate::RepoFrontierStatus::Retired + | crate::RepoFrontierStatus::Superseded + ) + }) + }); + Ok((frontier == sealed_frontier + && frontier.status == crate::RepoFrontierStatus::Active + && frontier.recommended_next_organ == "Eyes" + && dependencies_ready) + .then_some(frontier)) +} + pub fn select_and_commit_repo_frontier_research_request( runtime_store: impl AsRef, at: &str, @@ -7480,28 +7654,22 @@ pub fn select_and_commit_repo_frontier_research_request( let mut cache = runtime_spine_cache(runtime_store)?; cache.pull_all_backing_stores()?; let identity = require_identity(&cache)?; - let thread = cache - .get::(crate::THREAD_STATE_KEY)? - .ok_or_else(|| anyhow!("frontier Research requires authoritative thread state"))?; - thread.state()?; let (view, basis) = current_keyed_repo_model(&cache)?; let model = view.memory_context_projection(); - let challenges = current_repo_model_claim_challenges(&cache, &model, &basis)?; let launches = cache.get_all::()?; let packets = cache.get_all::()?; - let item = - match next_repo_frontier_research_work(&cache, &model, &challenges, &launches, &packets)? { - Some(NextRepoFrontierResearchWork::Existing(request)) => return Ok(request), - Some(NextRepoFrontierResearchWork::Unrequested(item)) => item, - None => { - return Err(anyhow!( - "current model has no uncovered actionable Eyes frontier" - )); - } - }; + let item = match next_repo_frontier_research_work(&cache, &model, &[], &launches, &packets)? { + Some(NextRepoFrontierResearchWork::Existing(request)) => return Ok(request), + Some(NextRepoFrontierResearchWork::Unrequested(item)) => item, + None => { + return Err(anyhow!( + "current model has no uncovered actionable Eyes frontier" + )); + } + }; let request = repo_frontier_research_request_for_admitted_item( &identity.runtime_id, - &thread.thread_id, + &cache, &model, &basis, &item, @@ -7511,10 +7679,6 @@ pub fn select_and_commit_repo_frontier_research_request( if let Some(existing) = cache.get::(&request_id)? { let mut replay = request.clone(); replay.requested_at = existing.requested_at.clone(); - // The frontier owns this request. The thread records where it was - // created, but later coordinator incarnations must not fork or - // invalidate the same frontier request. - replay.thread_id = existing.thread_id.clone(); return if existing == replay { Ok(existing) } else { @@ -7522,11 +7686,13 @@ pub fn select_and_commit_repo_frontier_research_request( }; } let (request_envelope, _) = cache.prepare_entry(&request_id, &request)?; - let expected = keyed_repo_model_basis_envelopes(&cache, &basis)?; + let expected = research_authority_envelopes(&cache, &request.frontier_authority_documents)?; let mut writes = expected.clone(); writes.push(request_envelope); if !backing.compare_and_swap_batch(&expected, writes)? { - return Err(anyhow!("frontier Research request lost current-model CAS")); + return Err(anyhow!( + "frontier Research request lost exact frontier authority CAS" + )); } Ok(request) } @@ -7540,14 +7706,13 @@ fn repo_frontier_item_hash(item: &crate::RepoFrontierItem) -> Result { fn repo_frontier_research_request_for_admitted_item( runtime_id: &str, - thread_id: &str, + cache: &CultCache, model: &crate::EpiphanyMemoryGraphSnapshot, basis: &crate::EpiphanyRepoModelBasis, item: &crate::RepoFrontierItem, requested_at: &str, ) -> Result { if runtime_id.is_empty() - || thread_id.is_empty() || basis.validate().is_err() || model .frontier @@ -7560,6 +7725,7 @@ fn repo_frontier_research_request_for_admitted_item( )); } let item_hash = repo_frontier_item_hash(item)?; + let frontier_authority_documents = research_frontier_authority_documents(cache, item)?; let public_source_refs = crate::ImmutableGithubSource::canonicalize_set( item.public_source_refs.iter().map(String::as_str), )?; @@ -7573,12 +7739,12 @@ fn repo_frontier_research_request_for_admitted_item( request_id: crate::frontier_research_request_id(runtime_id, &item.id, &item_hash), model_projection_digest: basis.projection_digest.clone(), model_source_documents: basis.source_documents.clone(), + frontier_authority_documents, frontier_item_id: item.id.clone(), frontier_item_hash: item_hash, source_scope: item.source_scope.clone(), requested_at: requested_at.to_string(), runtime_id: runtime_id.to_string(), - thread_id: thread_id.to_string(), contract: REPO_FRONTIER_RESEARCH_REQUEST_CONTRACT.to_string(), public_source_refs, }) @@ -7661,15 +7827,10 @@ pub fn runtime_repo_frontier_research_lifecycle( let jobs = cache.get_all::()?; let job_results = cache.get_all::()?; let role_results = cache.get_all::()?; - let state = cache - .get::(crate::THREAD_STATE_KEY)? - .map(|entry| entry.state()) - .transpose()?; - let (view, basis) = current_keyed_repo_model(&cache)?; + let (view, _basis) = current_keyed_repo_model(&cache)?; let model = view.memory_context_projection(); - let challenges = current_repo_model_claim_challenges(&cache, &model, &basis)?; - let work = next_repo_frontier_research_work(&cache, &model, &challenges, &launches, &packets)?; + let work = next_repo_frontier_research_work(&cache, &model, &[], &launches, &packets)?; let request = match work { Some(NextRepoFrontierResearchWork::Unrequested(item)) => { return Ok(RepoFrontierResearchLifecycle { @@ -7728,67 +7889,37 @@ pub fn runtime_repo_frontier_research_lifecycle( | EpiphanyRuntimeJobStatus::WaitingForReview => { RepoFrontierResearchLifecycleStage::WorkerRunning } - EpiphanyRuntimeJobStatus::Completed - | EpiphanyRuntimeJobStatus::Failed - | EpiphanyRuntimeJobStatus::Cancelled => { - let result_id = role_results + EpiphanyRuntimeJobStatus::Completed => { + let result = role_results .iter() .find(|result| result.job_id == job.job_id) - .map(|result| result.result_id.as_str()) - .or_else(|| { - job_results - .iter() - .filter(|result| result.job_id == job.job_id) - .max_by(|left, right| { - left.completed_at - .cmp(&right.completed_at) - .then_with(|| left.result_id.cmp(&right.result_id)) - }) - .map(|result| result.result_id.as_str()) - }) - .ok_or_else(|| { - anyhow!("terminal frontier Research job lost its reviewable result") - })?; - let matching_receipts = state - .as_ref() - .map(|state| { - state - .acceptance_receipts - .iter() - .filter(|receipt| { - receipt.result_id == result_id - && receipt.job_id == job.job_id - && receipt.role_id == "research" - }) - .collect::>() - }) - .unwrap_or_default(); - if matching_receipts.len() > 1 { + .ok_or_else(|| anyhow!("completed frontier Research job lost its typed result"))?; + if result.repo_frontier_research_request_id.as_deref() + != Some(request.request_id.as_str()) + || !result.role_id.eq_ignore_ascii_case("research") + { return Err(anyhow!( - "frontier Research result has multiple review authorities" + "frontier Research result crossed request-family authority" )); } - match matching_receipts.first() { - Some(receipt) - if receipt.surface == "roleFailureReview" && receipt.status == "superseded" => - { - RepoFrontierResearchLifecycleStage::LaunchReady - } - Some(receipt) - if receipt.surface == "roleAccept" && receipt.status == "accepted" => - { - return Err(anyhow!( - "accepted frontier Research result lost its Eyes evidence packet" - )); - } - Some(_) => { - return Err(anyhow!( - "frontier Research result has conflicting review authority" - )); - } - None => RepoFrontierResearchLifecycleStage::ResultReady, + if result.item_error.is_some() { + RepoFrontierResearchLifecycleStage::LaunchReady + } else { + RepoFrontierResearchLifecycleStage::ResultReady } } + EpiphanyRuntimeJobStatus::Failed | EpiphanyRuntimeJobStatus::Cancelled => { + let terminal = job_results + .iter() + .filter(|result| result.job_id == job.job_id) + .count(); + if terminal != 1 { + return Err(anyhow!( + "failed frontier Research attempt lost its exact terminal result" + )); + } + RepoFrontierResearchLifecycleStage::LaunchReady + } }; Ok(RepoFrontierResearchLifecycle { stage, @@ -7865,9 +7996,31 @@ fn repo_frontier_research_request_is_covered( } matching_jobs.insert(launch.job_id.as_str()); } - Ok(packets + for packet in packets .iter() - .any(|packet| matching_jobs.contains(packet.source_job_id.as_str()))) + .filter(|packet| packet.research_request_id == request.request_id) + { + let result = cache.get::(&packet.source_job_id)?; + let archived = cache.get::(&packet.source_job_id)?; + let exact_live = result.as_ref().is_some_and(|result| { + matching_jobs.contains(result.job_id.as_str()) + && result.result_id == packet.source_result_id + && result.decision_context_id == packet.decision_context_id + && result.repo_frontier_research_request_id.as_deref() + == Some(request.request_id.as_str()) + }); + let exact_archived = archived.as_ref().is_some_and(|attempt| { + attempt.request_kind == "frontier-research" + && attempt.request_id == request.request_id + && attempt.result_id.as_deref() == Some(packet.source_result_id.as_str()) + && attempt.decision_context_id.as_deref() + == Some(packet.decision_context_id.as_str()) + }); + if exact_live || exact_archived { + return Ok(true); + } + } + Ok(false) } fn actionable_repo_frontier_research_requests( @@ -7897,47 +8050,7 @@ fn repo_frontier_research_request_is_actionable( { return Err(anyhow!("invalid frontier Research request")); } - let identity = require_identity(cache)?; - let view = match require_keyed_repo_model_basis( - cache, - &request.model_projection_digest, - &request.model_source_documents, - ) { - Ok(view) => view, - Err(_) => return Ok(false), - }; - let model = view.memory_context_projection(); - let basis = view.reasoning_basis(); - if request.runtime_id != identity.runtime_id || request.thread_id.is_empty() { - return Ok(false); - } - let challenges = current_repo_model_claim_challenges(cache, &model, &basis)?; - let Some(item) = model - .frontier - .iter() - .find(|item| item.id == request.frontier_item_id) - else { - return Ok(false); - }; - if !frontier_item_is_actionable_for_organ(&model, &challenges, item, "Eyes", false) { - return Ok(false); - } - let item_hash = repo_frontier_item_hash(item)?; - let expected_public_source_refs = crate::ImmutableGithubSource::canonicalize_set( - item.public_source_refs.iter().map(String::as_str), - )?; - if expected_public_source_refs != item.public_source_refs { - return Err(anyhow!( - "frontier Research public source authority is not canonical" - )); - } - let expected_request_id = - crate::frontier_research_request_id(&identity.runtime_id, &item.id, &item_hash); - Ok(request.request_id == expected_request_id - && request.frontier_item_id == item.id - && request.frontier_item_hash == item_hash - && request.source_scope == item.source_scope - && request.public_source_refs == expected_public_source_refs) + Ok(current_repo_frontier_research_request(cache, request)?.is_some()) } /// Read-only Self signal for proposal planning. It uses the same eligibility @@ -8304,7 +8417,11 @@ fn runtime_has_actionable_frontier_for_organ( return Ok(false); }; let model = view.memory_context_projection(); - let challenges = current_repo_model_claim_challenges(&cache, &model, &basis)?; + let challenges = if organ == "Hands" { + current_repo_model_claim_challenges(&cache, &model, &basis)? + } else { + Vec::new() + }; Ok(actionable_frontier_item_for_organ(&model, &challenges, organ, organ == "Hands").is_some()) } @@ -8968,11 +9085,24 @@ fn worker_result_has_keyed_mind_commit( crate::EpiphanyMindCommitAuthority::ModelDecisionContext { ref decision_context_id } if decision_context_id == &result.decision_context_id - ) && receipt.invariant_owner.starts_with("Modeling.") - && receipt - .writes - .iter() - .any(|write| write.document_type.starts_with("epiphany.mind.repo_model.")) + ) && if result.role_id.eq_ignore_ascii_case("modeling") { + receipt.invariant_owner.starts_with("Modeling.") + && receipt + .writes + .iter() + .any(|write| write.document_type.starts_with("epiphany.mind.repo_model.")) + } else if result.role_id.eq_ignore_ascii_case("research") { + receipt.invariant_owner == "Eyes.frontier_research" + && receipt.writes.iter().any(|write| { + matches!( + write.document_type.as_str(), + crate::EpiphanyMindEvidenceDocument::TYPE + | crate::EpiphanyMindObservationDocument::TYPE + ) + }) + } else { + false + } })) } @@ -10411,6 +10541,7 @@ where "frontier-verdict-modeling" => { RuntimeTypedRequestRef::FrontierVerdictModeling(request_id) } + "frontier-research" => RuntimeTypedRequestRef::FrontierResearch(request_id), "imagination-consideration" => { RuntimeTypedRequestRef::ImaginationConsideration(request_id) } @@ -10432,7 +10563,7 @@ where } if matches!( request_kind, - "proposal-modeling" | "frontier-verdict-modeling" + "proposal-modeling" | "frontier-verdict-modeling" | "frontier-research" ) && !worker_result_has_keyed_mind_commit( &cache, role_result diff --git a/epiphany-core/src/runtime_worker_attempt.rs b/epiphany-core/src/runtime_worker_attempt.rs index ed2f2ae2..a83329f9 100644 --- a/epiphany-core/src/runtime_worker_attempt.rs +++ b/epiphany-core/src/runtime_worker_attempt.rs @@ -58,6 +58,7 @@ impl WorkerProcessStatus { pub enum RuntimeTypedRequestRef<'a> { ProposalModeling(&'a str), FrontierVerdictModeling(&'a str), + FrontierResearch(&'a str), ImaginationConsideration(&'a str), AdmittedModelDirection(&'a str), } @@ -67,6 +68,7 @@ impl<'a> RuntimeTypedRequestRef<'a> { match self { Self::ProposalModeling(id) | Self::FrontierVerdictModeling(id) + | Self::FrontierResearch(id) | Self::ImaginationConsideration(id) | Self::AdmittedModelDirection(id) => id, } @@ -76,6 +78,7 @@ impl<'a> RuntimeTypedRequestRef<'a> { match self { Self::ProposalModeling(_) => "proposal-modeling", Self::FrontierVerdictModeling(_) => "frontier-verdict-modeling", + Self::FrontierResearch(_) => "frontier-research", Self::ImaginationConsideration(_) => "imagination-consideration", Self::AdmittedModelDirection(_) => "admitted-model-direction", } @@ -89,6 +92,9 @@ impl<'a> RuntimeTypedRequestRef<'a> { Self::FrontierVerdictModeling(id) => { launch.repo_frontier_modeling_request_id.as_deref() == Some(id) } + Self::FrontierResearch(id) => { + launch.repo_frontier_research_request_id.as_deref() == Some(id) + } Self::ImaginationConsideration(id) => { launch.imagination_consideration_request_id.as_deref() == Some(id) } @@ -109,6 +115,9 @@ impl<'a> RuntimeTypedRequestRef<'a> { Self::FrontierVerdictModeling(id) => { result.repo_frontier_modeling_request_id.as_deref() == Some(id) } + Self::FrontierResearch(id) => { + result.repo_frontier_research_request_id.as_deref() == Some(id) + } Self::ImaginationConsideration(id) => { result.imagination_consideration_request_id.as_deref() == Some(id) } @@ -131,6 +140,9 @@ impl crate::EpiphanyRuntimeWorkerLaunchRequest { self.repo_frontier_modeling_request_id .as_deref() .map(RuntimeTypedRequestRef::FrontierVerdictModeling), + self.repo_frontier_research_request_id + .as_deref() + .map(RuntimeTypedRequestRef::FrontierResearch), self.imagination_consideration_request_id .as_deref() .map(RuntimeTypedRequestRef::ImaginationConsideration), diff --git a/epiphany-core/src/surfaces/coordinator.rs b/epiphany-core/src/surfaces/coordinator.rs index 8c5e03fa..5f00062e 100644 --- a/epiphany-core/src/surfaces/coordinator.rs +++ b/epiphany-core/src/surfaces/coordinator.rs @@ -799,72 +799,29 @@ pub fn recommend_coordinator_action( }; } - if input.signals.research_result_status == EpiphanyCoordinatorRoleResultStatus::Completed - && !input.research_result_accepted + if input.implementation_evidence_after_verification + && matches!( + input.signals.verification_result_status, + EpiphanyCoordinatorRoleResultStatus::MissingBinding + | EpiphanyCoordinatorRoleResultStatus::BackendUnavailable + | EpiphanyCoordinatorRoleResultStatus::BackendMissing + | EpiphanyCoordinatorRoleResultStatus::Cancelled + | EpiphanyCoordinatorRoleResultStatus::Failed + ) { - if !input.research_result_reviewable && input.research_result_failure_reviewed { - return build( - EpiphanyCoordinatorAction::LaunchResearch, - Some(EpiphanyCoordinatorRoleId::Research), - Some(EpiphanyCoordinatorSceneAction::RoleLaunch), - false, - true, - "The unreviewable Eyes result has a supersession receipt; relaunch Research against the current source contract.", - ); - } return build( - EpiphanyCoordinatorAction::ReviewResearchResult, - Some(EpiphanyCoordinatorRoleId::Research), - Some(EpiphanyCoordinatorSceneAction::RoleResult), - input.research_result_reviewable, + EpiphanyCoordinatorAction::LaunchVerification, + Some(EpiphanyCoordinatorRoleId::Verification), + Some(EpiphanyCoordinatorSceneAction::RoleLaunch), false, - "An Eyes/source-gathering finding is complete and must be reviewed before modeling or implementation can cite it.", - ); - } - - if input.signals.research_result_status == EpiphanyCoordinatorRoleResultStatus::Failed - && !input.research_result_accepted - { - if input.research_result_failure_reviewed { - return build( - EpiphanyCoordinatorAction::LaunchResearch, - Some(EpiphanyCoordinatorRoleId::Research), - Some(EpiphanyCoordinatorSceneAction::RoleLaunch), - false, - true, - "The failed Eyes result has a supersession receipt; relaunch Research against the current source contract.", - ); - } - return build( - EpiphanyCoordinatorAction::ReviewResearchResult, - Some(EpiphanyCoordinatorRoleId::Research), - Some(EpiphanyCoordinatorSceneAction::RoleResult), - false, - false, - "The Eyes/source-gathering worker failed; inspect the failed result before routing another lane from stale evidence.", - ); - } - - if matches!( - input.signals.research_result_status, - EpiphanyCoordinatorRoleResultStatus::Pending | EpiphanyCoordinatorRoleResultStatus::Running - ) { - return build( - EpiphanyCoordinatorAction::ReviewResearchResult, - Some(EpiphanyCoordinatorRoleId::Research), - Some(EpiphanyCoordinatorSceneAction::RoleResult), - false, - false, - "An Eyes/source-gathering specialist is already running; wait for its evidence packet before relaunching modeling.", + true, + "A complete Hands consequence chain awaits Soul independently of any Eyes lifecycle.", ); } if input.signals.modeling_result_status == EpiphanyCoordinatorRoleResultStatus::Completed && !input.modeling_result_accepted && !(input.modeling_result_failure_reviewed && input.hands_frontier_ready) - && !(input.modeling_result_failure_reviewed - && input.research_result_accepted - && input.implementation_evidence_after_verification) { if input.modeling_result_failure_reviewed { if input.modeling_result_proposal_bound && !input.hands_frontier_ready { @@ -889,26 +846,6 @@ pub fn recommend_coordinator_action( } } if !input.modeling_result_reviewable { - if input.modeling_result_requests_regather - && !input.research_result_accepted - && matches!( - input.signals.research_result_status, - EpiphanyCoordinatorRoleResultStatus::MissingBinding - | EpiphanyCoordinatorRoleResultStatus::BackendUnavailable - | EpiphanyCoordinatorRoleResultStatus::BackendMissing - | EpiphanyCoordinatorRoleResultStatus::Cancelled - | EpiphanyCoordinatorRoleResultStatus::Failed - ) - { - return build( - EpiphanyCoordinatorAction::LaunchResearch, - Some(EpiphanyCoordinatorRoleId::Research), - Some(EpiphanyCoordinatorSceneAction::RoleLaunch), - false, - true, - "The modeling/checkpoint lane requested source regather and returned no acceptable modeling patch; launch Eyes to gather citable source evidence instead of relaunching the blind modeler.", - ); - } return build( EpiphanyCoordinatorAction::ReviewModelingResult, Some(EpiphanyCoordinatorRoleId::Modeling), @@ -1091,59 +1028,21 @@ pub fn recommend_coordinator_action( ); } + if input.hands_frontier_ready { + return build( + EpiphanyCoordinatorAction::ContinueImplementation, + Some(EpiphanyCoordinatorRoleId::Implementation), + None, + false, + false, + "Mind has admitted the current RepoModel and Self has an actionable Hands frontier route.", + ); + } + if input.recommendation.action == EpiphanyCrrcAction::RegatherManually && role_status(&input.roles, EpiphanyCoordinatorRoleId::Implementation) == Some(EpiphanyCoordinatorRoleStatus::Blocked) { - if input.research_result_accepted - && input.implementation_evidence_after_verification - && matches!( - input.signals.verification_result_status, - EpiphanyCoordinatorRoleResultStatus::MissingBinding - | EpiphanyCoordinatorRoleResultStatus::BackendUnavailable - | EpiphanyCoordinatorRoleResultStatus::BackendMissing - | EpiphanyCoordinatorRoleResultStatus::Cancelled - | EpiphanyCoordinatorRoleResultStatus::Failed - ) - { - return build( - EpiphanyCoordinatorAction::LaunchVerification, - Some(EpiphanyCoordinatorRoleId::Verification), - Some(EpiphanyCoordinatorSceneAction::RoleLaunch), - false, - true, - "Eyes refreshed source after CRRC regather and a complete Hands consequence still awaits Soul; verify that exact consequence before Modeling evolves its active route.", - ); - } - if input.research_result_accepted && input.hands_frontier_ready { - return build( - EpiphanyCoordinatorAction::ContinueImplementation, - Some(EpiphanyCoordinatorRoleId::Implementation), - None, - false, - false, - "Mind has admitted a current Hands frontier after source regather; execute that exact route before launching unrelated Modeling.", - ); - } - if !input.research_result_accepted - && matches!( - input.signals.research_result_status, - EpiphanyCoordinatorRoleResultStatus::MissingBinding - | EpiphanyCoordinatorRoleResultStatus::BackendUnavailable - | EpiphanyCoordinatorRoleResultStatus::BackendMissing - | EpiphanyCoordinatorRoleResultStatus::Cancelled - | EpiphanyCoordinatorRoleResultStatus::Failed - ) - { - return build( - EpiphanyCoordinatorAction::LaunchResearch, - Some(EpiphanyCoordinatorRoleId::Research), - Some(EpiphanyCoordinatorSceneAction::RoleLaunch), - false, - true, - "CRRC says regather is required; launch Eyes to rebuild citable source evidence before another coding turn.", - ); - } return build( EpiphanyCoordinatorAction::RegatherManually, Some(EpiphanyCoordinatorRoleId::Research), @@ -1207,23 +1106,12 @@ pub fn recommend_coordinator_action( if input.recommendation.action == EpiphanyCrrcAction::RegatherManually { return build( - EpiphanyCoordinatorAction::LaunchResearch, + EpiphanyCoordinatorAction::RegatherManually, Some(EpiphanyCoordinatorRoleId::Research), - Some(EpiphanyCoordinatorSceneAction::RoleLaunch), - false, + Some(EpiphanyCoordinatorSceneAction::RoleResult), true, - "CRRC requires regather; Eyes is the fixed specialist lane that can advance source gathering.", - ); - } - - if input.hands_frontier_ready { - return build( - EpiphanyCoordinatorAction::ContinueImplementation, - Some(EpiphanyCoordinatorRoleId::Implementation), - None, false, - false, - "Mind has admitted the current RepoModel and Self has an actionable Hands frontier route.", + "CRRC requires explicit operator regather; only an exact external-evidence obligation may launch Eyes.", ); } @@ -1718,7 +1606,7 @@ mod tests { } #[test] - fn reviewed_failed_research_relaunches_eyes() { + fn legacy_failed_research_cannot_relaunch_eyes() { let mut state = EpiphanyThreadState::default(); state.acceptance_receipts.push(EpiphanyAcceptanceReceipt { id: "failure-review-research".to_string(), @@ -1747,7 +1635,7 @@ mod tests { research_result_failure_reviewed: true, ..base }); - assert_eq!(decision.action, EpiphanyCoordinatorAction::LaunchResearch); + assert_eq!(decision.action, EpiphanyCoordinatorAction::LaunchModeling); } #[test] @@ -1854,7 +1742,7 @@ mod tests { } #[test] - fn uses_fixed_lanes_before_manual_regather() { + fn exact_work_preempts_but_cannot_be_invented_by_manual_regather() { let launch_modeling = recommend_coordinator_action(EpiphanyCoordinatorInput { recommendation: recommendation(EpiphanyCrrcAction::RegatherManually), signals: EpiphanyCoordinatorSignals { @@ -1906,7 +1794,7 @@ mod tests { }); assert_eq!( blocked_regather.action, - EpiphanyCoordinatorAction::LaunchResearch + EpiphanyCoordinatorAction::RegatherManually ); } @@ -1958,7 +1846,7 @@ mod tests { } #[test] - fn accepted_eyes_routes_complete_hands_consequence_to_soul_before_model_evolution() { + fn hands_consequence_routes_to_soul_without_eyes_authority() { let decision = recommend_coordinator_action(EpiphanyCoordinatorInput { recommendation: recommendation(EpiphanyCrrcAction::RegatherManually), roles: vec![role( @@ -1966,11 +1854,10 @@ mod tests { EpiphanyCoordinatorRoleStatus::Blocked, )], signals: EpiphanyCoordinatorSignals { - research_result_status: EpiphanyCoordinatorRoleResultStatus::Completed, + research_result_status: EpiphanyCoordinatorRoleResultStatus::MissingBinding, modeling_result_status: EpiphanyCoordinatorRoleResultStatus::Completed, verification_result_status: EpiphanyCoordinatorRoleResultStatus::MissingBinding, }, - research_result_accepted: true, modeling_result_failure_reviewed: true, implementation_evidence_after_verification: true, reorient_finding_accepted: true, @@ -1989,7 +1876,7 @@ mod tests { } #[test] - fn routes_regather_needed_modeling_to_eyes() { + fn modeling_regather_hint_cannot_create_eyes_work() { let decision = recommend_coordinator_action(EpiphanyCoordinatorInput { signals: EpiphanyCoordinatorSignals { research_result_status: EpiphanyCoordinatorRoleResultStatus::MissingBinding, @@ -2001,12 +1888,15 @@ mod tests { ..input() }); - assert_eq!(decision.action, EpiphanyCoordinatorAction::LaunchResearch); + assert_eq!( + decision.action, + EpiphanyCoordinatorAction::ReviewModelingResult + ); assert_eq!( decision.target_role, - Some(EpiphanyCoordinatorRoleId::Research) + Some(EpiphanyCoordinatorRoleId::Modeling) ); - assert!(decision.reason.contains("source regather")); + assert!(decision.reason.contains("no acceptable RepoModel proposal")); } #[test] diff --git a/epiphany-core/src/surfaces/role_result.rs b/epiphany-core/src/surfaces/role_result.rs index 038750f3..5d2ebf72 100644 --- a/epiphany-core/src/surfaces/role_result.rs +++ b/epiphany-core/src/surfaces/role_result.rs @@ -798,10 +798,9 @@ pub fn research_role_state_patch_policy_errors( )); } } - if patch.scratch.is_none() && patch.investigation_checkpoint.is_none() { - errors.push( - "research statePatch must include scratch or an investigationCheckpoint to bank the source-gathering seam".to_string(), - ); + if patch.scratch.is_some() { + errors + .push("research scratch is pass-local and cannot enter durable Mind state".to_string()); } errors } diff --git a/epiphany-core/src/surfaces/worker_launch.rs b/epiphany-core/src/surfaces/worker_launch.rs index 3f9e48eb..85f99c4a 100644 --- a/epiphany-core/src/surfaces/worker_launch.rs +++ b/epiphany-core/src/surfaces/worker_launch.rs @@ -35,9 +35,9 @@ pub const REPO_FRONTIER_PLANNING_CONTEXT_SCHEMA_VERSION: &str = pub const REPO_FRONTIER_PLANNING_CONTEXT_CONTRACT: &str = "epiphany.repo_frontier_planning_context.v1"; pub const REPO_FRONTIER_RESEARCH_CONTEXT_SCHEMA_VERSION: &str = - "epiphany.worker.repo_frontier_research_context.v2"; + "epiphany.worker.repo_frontier_research_context.v3"; pub const REPO_FRONTIER_RESEARCH_CONTEXT_CONTRACT: &str = - "epiphany.repo_frontier_research_context.v2"; + "epiphany.repo_frontier_research_context.v3"; pub const REPO_FRONTIER_PLAN_MIND_CONTEXT_SCHEMA_VERSION: &str = "epiphany.worker.repo_frontier_plan_mind_context.v0"; pub const REPO_FRONTIER_PLAN_MIND_CONTEXT_CONTRACT: &str = @@ -153,6 +153,7 @@ pub struct RepoFrontierResearchContextProjection { pub request_id: String, pub model_projection_digest: String, pub model_source_documents: Vec, + pub frontier_authority_documents: Vec, pub frontier_item_id: String, pub frontier_item_hash: String, pub source_scope: Vec, @@ -168,6 +169,7 @@ impl From<&crate::RepoFrontierResearchRequest> for RepoFrontierResearchContextPr request_id: request.request_id.clone(), model_projection_digest: request.model_projection_digest.clone(), model_source_documents: request.model_source_documents.clone(), + frontier_authority_documents: request.frontier_authority_documents.clone(), frontier_item_id: request.frontier_item_id.clone(), frontier_item_hash: request.frontier_item_hash.clone(), source_scope: request.source_scope.clone(), diff --git a/epiphany-openai-runtime/src/lib.rs b/epiphany-openai-runtime/src/lib.rs index 786e86e5..feec0f82 100644 --- a/epiphany-openai-runtime/src/lib.rs +++ b/epiphany-openai-runtime/src/lib.rs @@ -2227,6 +2227,7 @@ fn role_worker_result_from_ingress( frontier_route_id: clean_optional_string(result.frontier_route_id.as_deref()), repo_frontier_modeling_request_id: launch_request.repo_frontier_modeling_request_id.clone(), proposal_modeling_request_id: launch_request.proposal_modeling_request_id.clone(), + repo_frontier_research_request_id: launch_request.repo_frontier_research_request_id.clone(), frontier_planning_request_id: launch_request.frontier_planning_request_id.clone(), frontier_plan_candidate_msgpack, frontier_plan_mind_request_id: frontier_plan_mind_context @@ -2290,6 +2291,7 @@ pub fn failed_frontier_planning_role_result( frontier_route_id: None, repo_frontier_modeling_request_id: None, proposal_modeling_request_id: None, + repo_frontier_research_request_id: None, frontier_planning_request_id: None, frontier_plan_candidate_msgpack: None, frontier_plan_mind_request_id: None, @@ -2681,17 +2683,9 @@ mod tests { assert_eq!(state_patch["additionalProperties"], false); assert_eq!( state_patch["required"], - serde_json::json!([ - "evidence", - "investigationCheckpoint", - "observations", - "scratch" - ]) - ); - assert_eq!( - state_patch["properties"]["scratch"]["anyOf"][0]["additionalProperties"], - false + serde_json::json!(["evidence", "investigationCheckpoint", "observations"]) ); + assert!(state_patch["properties"].get("scratch").is_none()); assert_eq!( state_patch["properties"]["investigationCheckpoint"]["anyOf"][0]["additionalProperties"], false @@ -3846,6 +3840,7 @@ mod tests { frontier_route_id: Some(route.route_id.clone()), repo_frontier_modeling_request_id: None, proposal_modeling_request_id: None, + repo_frontier_research_request_id: None, frontier_planning_request_id: None, frontier_plan_candidate_msgpack: None, frontier_plan_mind_request_id: None, diff --git a/vendor/cultcache-rs b/vendor/cultcache-rs index 0ba87453..ba6a4874 160000 --- a/vendor/cultcache-rs +++ b/vendor/cultcache-rs @@ -1 +1 @@ -Subproject commit 0ba87453115e3d44e8fcb7419866cd7dd70fb096 +Subproject commit ba6a4874fc61c69605dd0dc0b004f340e8a76368