Collapse reorientation result projection

This commit is contained in:
Meta Vens 2026-08-25 01:07:39 +02:00
commit 125d77a266
6 changed files with 62 additions and 126 deletions

View file

@ -1800,7 +1800,7 @@ fn read_reorient_result(runtime_store: &Path, _thread_id: &str) -> Result<Value>
"note": "The keyed reorientation request is ready to launch."
}));
};
let snapshot = epiphany_core::read_runtime_reorient_result(Some(runtime_store), &job_id);
let snapshot = epiphany_core::read_runtime_reorient_result(runtime_store, &job_id)?;
let mut value = json!({
"source": "native",
"status": snapshot.status,

View file

@ -1,98 +1,68 @@
use crate::*;
use anyhow::{Result, anyhow};
use serde::{Deserialize, Serialize};
use std::path::Path;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum EpiphanyReorientResultStatus {
Pending,
Completed,
Failed,
}
#[derive(Debug, Clone)]
pub struct EpiphanyCoordinatorReorientResultSnapshot {
pub status: EpiphanyCrrcResultStatus,
pub status: EpiphanyReorientResultStatus,
pub finding: Option<EpiphanyReorientFindingInterpretation>,
pub note: String,
}
pub fn read_runtime_reorient_result(
runtime_store_path: Option<&Path>,
runtime_store_path: &Path,
job_id: &str,
) -> EpiphanyCoordinatorReorientResultSnapshot {
let Some(runtime_store_path) = runtime_store_path else {
return reorient_snapshot(
EpiphanyCrrcResultStatus::Pending,
None,
Some(
"Resident Self owns this reorientation worker; no loaded runtime-spine store is available yet.",
),
);
};
let job = match runtime_job(runtime_store_path, job_id) {
Ok(Some(job)) => job,
Ok(None) => {
return reorient_snapshot(
EpiphanyCrrcResultStatus::Pending,
) -> Result<EpiphanyCoordinatorReorientResultSnapshot> {
let mut cache = runtime_spine_cache(runtime_store_path)?;
cache.pull_all_backing_stores()?;
let job = match cache.get::<EpiphanyRuntimeJob>(job_id)? {
Some(job) => job,
None => {
return Ok(reorient_snapshot(
EpiphanyReorientResultStatus::Pending,
None,
Some(&format!(
"Resident runtime job {job_id:?} has not reported typed state yet."
)),
);
}
Err(error) => {
return reorient_snapshot(
EpiphanyCrrcResultStatus::BackendUnavailable,
None,
Some(&format!(
"Failed to read resident runtime-spine job {job_id:?}: {error}"
)),
);
));
}
};
let status = reorient_result_status(&job.status);
let finding = match status {
EpiphanyCrrcResultStatus::Completed => {
match runtime_reorient_worker_result(runtime_store_path, job_id) {
Ok(Some(result)) => Some(interpret_runtime_reorient_worker_result(&result)),
Ok(None) => {
return reorient_snapshot(
EpiphanyCrrcResultStatus::BackendUnavailable,
None,
Some(&format!(
"Resident runtime job {job_id:?} completed without an EpiphanyRuntimeReorientWorkerResult typed document; generic lifecycle receipts are not reviewable findings."
)),
);
}
Err(error) => {
return reorient_snapshot(
EpiphanyCrrcResultStatus::BackendUnavailable,
None,
Some(&format!(
"Failed to read typed reorientation worker result for resident runtime job {job_id:?}: {error}"
)),
);
}
}
}
EpiphanyCrrcResultStatus::Failed | EpiphanyCrrcResultStatus::Cancelled => {
match runtime_reorient_worker_result(runtime_store_path, job_id) {
Ok(result) => result
.as_ref()
.map(interpret_runtime_reorient_worker_result),
Err(_) => None,
}
}
_ => None,
EpiphanyReorientResultStatus::Completed => Some(interpret_runtime_reorient_worker_result(
&cache
.get::<EpiphanyRuntimeReorientWorkerResult>(job_id)?
.ok_or_else(|| {
anyhow!(
"resident runtime job {job_id:?} completed without its typed reorientation result"
)
})?,
)),
EpiphanyReorientResultStatus::Failed => cache
.get::<EpiphanyRuntimeReorientWorkerResult>(job_id)?
.as_ref()
.map(interpret_runtime_reorient_worker_result),
EpiphanyReorientResultStatus::Pending => None,
};
reorient_snapshot(status, finding, None)
Ok(reorient_snapshot(status, finding, None))
}
fn reorient_snapshot(
status: EpiphanyCrrcResultStatus,
status: EpiphanyReorientResultStatus,
finding: Option<EpiphanyReorientFindingInterpretation>,
note_override: Option<&str>,
) -> EpiphanyCoordinatorReorientResultSnapshot {
let note = note_override.map(str::to_string).unwrap_or_else(|| {
render_reorient_result_note(
status,
finding.as_ref(),
finding
.as_ref()
.and_then(|finding| finding.job_error.as_deref()),
)
});
let note = note_override
.map(str::to_string)
.unwrap_or_else(|| render_reorient_result_note(status, finding.as_ref()));
EpiphanyCoordinatorReorientResultSnapshot {
status,
finding,
@ -100,50 +70,31 @@ fn reorient_snapshot(
}
}
fn reorient_result_status(status: &EpiphanyRuntimeJobStatus) -> EpiphanyCrrcResultStatus {
fn reorient_result_status(status: &EpiphanyRuntimeJobStatus) -> EpiphanyReorientResultStatus {
match status {
EpiphanyRuntimeJobStatus::Queued => EpiphanyCrrcResultStatus::Pending,
EpiphanyRuntimeJobStatus::Completed => EpiphanyCrrcResultStatus::Completed,
EpiphanyRuntimeJobStatus::Failed => EpiphanyCrrcResultStatus::Failed,
EpiphanyRuntimeJobStatus::Queued => EpiphanyReorientResultStatus::Pending,
EpiphanyRuntimeJobStatus::Completed => EpiphanyReorientResultStatus::Completed,
EpiphanyRuntimeJobStatus::Failed => EpiphanyReorientResultStatus::Failed,
}
}
fn render_reorient_result_note(
status: EpiphanyCrrcResultStatus,
status: EpiphanyReorientResultStatus,
finding: Option<&EpiphanyReorientFindingInterpretation>,
item_error: Option<&str>,
) -> String {
match status {
EpiphanyCrrcResultStatus::Completed => finding.map_or_else(
|| "Reorientation worker completed, but no structured result was recorded.".to_string(),
|finding| {
format!(
"Reorientation worker completed. Next safe move: {}",
finding.next_safe_move.as_deref().unwrap_or("not supplied")
)
},
EpiphanyReorientResultStatus::Completed => format!(
"Reorientation worker completed. Next safe move: {}",
finding
.and_then(|finding| finding.next_safe_move.as_deref())
.unwrap_or("not supplied")
),
EpiphanyCrrcResultStatus::Failed => item_error
EpiphanyReorientResultStatus::Failed => finding
.and_then(|finding| finding.job_error.as_deref())
.map(|error| format!("Reorientation worker failed: {error}"))
.unwrap_or_else(|| "Reorientation worker failed.".to_string()),
EpiphanyCrrcResultStatus::Cancelled => {
"Reorientation worker was cancelled before producing a result.".to_string()
}
EpiphanyCrrcResultStatus::Running => "Reorientation worker is still running.".to_string(),
EpiphanyCrrcResultStatus::Pending => {
EpiphanyReorientResultStatus::Pending => {
"Reorientation worker has not produced a result yet.".to_string()
}
EpiphanyCrrcResultStatus::MissingState => {
"No authoritative Epiphany state exists for this thread.".to_string()
}
EpiphanyCrrcResultStatus::MissingBinding => {
"No matching Epiphany reorientation worker binding exists.".to_string()
}
EpiphanyCrrcResultStatus::BackendUnavailable => {
"The bound runtime backend is unavailable.".to_string()
}
EpiphanyCrrcResultStatus::BackendMissing => {
"The bound runtime backend job or item is missing.".to_string()
}
}
}

View file

@ -79,6 +79,7 @@ pub use coordinator_objective_intake::UserObjectiveIntakeApplied;
pub use coordinator_objective_intake::UserObjectiveIntakeInput;
pub use coordinator_objective_intake::intake_user_objective;
pub use coordinator_results::EpiphanyCoordinatorReorientResultSnapshot;
pub use coordinator_results::EpiphanyReorientResultStatus;
pub use coordinator_results::read_runtime_reorient_result;
pub use cultcache_rs::CacheBackingStore;
pub use cultcache_rs::CultCache;
@ -298,7 +299,6 @@ pub use surfaces::EpiphanyCoordinatorAction;
pub use surfaces::EpiphanyCoordinatorDecision;
pub use surfaces::EpiphanyCoordinatorInput;
pub use surfaces::EpiphanyCoordinatorRoleStatus;
pub use surfaces::EpiphanyCrrcResultStatus;
pub use surfaces::EpiphanyReorientFindingInterpretation;
pub use surfaces::EpiphanyReorientWorkerLaunchDocument;
pub use surfaces::EpiphanyResearchDecision;

View file

@ -1001,11 +1001,14 @@ mod tests {
decision_context_id: context.context_id.clone(),
};
crate::put_runtime_reorient_worker_result(&store, &result)?;
let projected_result = crate::read_runtime_reorient_result(&store, &job_id)?;
assert_eq!(
crate::runtime_job(&store, &job_id)?
.expect("reorientation runtime job")
.status,
EpiphanyRuntimeJobStatus::Completed
projected_result.status,
crate::EpiphanyReorientResultStatus::Completed
);
assert_eq!(
projected_result.finding.and_then(|finding| finding.mode),
Some("regather".into())
);
cache.pull_all_backing_stores()?;

View file

@ -1,16 +0,0 @@
use serde::Deserialize;
use serde::Serialize;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum EpiphanyCrrcResultStatus {
MissingState,
MissingBinding,
BackendUnavailable,
BackendMissing,
Pending,
Running,
Completed,
Failed,
Cancelled,
}

View file

@ -1,6 +1,5 @@
mod coordinator_contract;
mod coordinator_decision;
mod crrc;
mod research_decision;
mod role_board;
mod role_result;
@ -8,7 +7,6 @@ mod worker_launch;
pub use coordinator_contract::*;
pub use coordinator_decision::recommend_coordinator_action;
pub use crrc::EpiphanyCrrcResultStatus;
pub use research_decision::EpiphanyResearchDecision;
pub use role_board::EpiphanyRoleBoardInput;
pub use role_board::EpiphanyRoleBoardLane;