Extract Epiphany runtime result adapters

This commit is contained in:
Meta Vens 2026-05-13 01:46:38 +01:00
commit c68abb2efa
6 changed files with 266 additions and 246 deletions

View file

@ -1,5 +1,6 @@
mod epiphany_mutation_routes;
mod epiphany_read_routes;
mod epiphany_runtime_results;
use self::epiphany_mutation_routes::epiphany_job_launch_changed_fields;
use self::epiphany_mutation_routes::epiphany_promote_changed_fields;
@ -10,6 +11,15 @@ use self::epiphany_mutation_routes::reorient_finding_code_refs;
use self::epiphany_mutation_routes::reorient_finding_investigation_checkpoint;
use self::epiphany_mutation_routes::reorient_finding_scratch;
use self::epiphany_mutation_routes::role_finding_summary;
use self::epiphany_runtime_results::latest_epiphany_runtime_link_for_binding;
use self::epiphany_runtime_results::load_epiphany_reorient_result_from_runtime_spine_job;
use self::epiphany_runtime_results::load_epiphany_reorient_result_snapshot;
use self::epiphany_runtime_results::load_epiphany_role_result_from_runtime_spine_job;
use self::epiphany_runtime_results::load_epiphany_role_result_snapshot;
use self::epiphany_runtime_results::reorient_finding_runtime_job_id;
use self::epiphany_runtime_results::reorient_finding_runtime_result_id;
use self::epiphany_runtime_results::role_finding_runtime_job_id;
use self::epiphany_runtime_results::role_finding_runtime_result_id;
use crate::bespoke_event_handling::apply_bespoke_event_handling;
use crate::bespoke_event_handling::maybe_emit_hook_prompt_item_completed;
use crate::command_exec::CommandExecManager;
@ -10250,248 +10260,6 @@ fn epiphany_imagination_finding_has_reviewable_state_patch(
.is_some_and(|patch| imagination_role_accept_patch_errors(patch).is_empty())
}
fn role_finding_runtime_result_id(finding: &ThreadEpiphanyRoleFinding) -> Option<String> {
finding.runtime_result_id.clone()
}
fn role_finding_runtime_job_id(finding: &ThreadEpiphanyRoleFinding) -> Option<String> {
finding.runtime_job_id.clone()
}
fn reorient_finding_runtime_result_id(finding: &ThreadEpiphanyReorientFinding) -> Option<String> {
finding.runtime_result_id.clone()
}
fn reorient_finding_runtime_job_id(finding: &ThreadEpiphanyReorientFinding) -> Option<String> {
finding.runtime_job_id.clone()
}
fn load_epiphany_role_result_from_runtime_spine_job(
job_id: &str,
runtime_store_path: Option<&Path>,
role_id: ThreadEpiphanyRoleId,
) -> (
ThreadEpiphanyRoleResultStatus,
Option<ThreadEpiphanyRoleFinding>,
String,
) {
let Some(runtime_store_path) = runtime_store_path else {
return (
ThreadEpiphanyRoleResultStatus::Pending,
None,
"Heartbeat activation owns this role specialist; no loaded runtime-spine store is available yet."
.to_string(),
);
};
let snapshot = match runtime_job_snapshot(runtime_store_path, job_id) {
Ok(Some(snapshot)) => snapshot,
Ok(None) => {
return (
ThreadEpiphanyRoleResultStatus::Pending,
None,
format!(
"Heartbeat runtime job {:?} has not reported typed state yet.",
job_id
),
);
}
Err(err) => {
return (
ThreadEpiphanyRoleResultStatus::BackendUnavailable,
None,
format!(
"Failed to read heartbeat runtime-spine job {:?}: {err}",
job_id
),
);
}
};
let status = map_runtime_role_result_status(&snapshot);
let finding = snapshot.result.as_ref().map(|result| {
map_protocol_role_finding(
role_id,
interpret_role_runtime_job_result(map_core_role_result_role_id(role_id), result),
)
});
let note = render_epiphany_role_result_note(role_id, status, finding.as_ref(), None);
(status, finding, note)
}
fn load_epiphany_reorient_result_from_runtime_spine_job(
job_id: &str,
runtime_store_path: Option<&Path>,
) -> (
ThreadEpiphanyReorientResultStatus,
Option<ThreadEpiphanyReorientFinding>,
String,
) {
let Some(runtime_store_path) = runtime_store_path else {
return (
ThreadEpiphanyReorientResultStatus::Pending,
None,
"Heartbeat activation owns this reorientation worker; no loaded runtime-spine store is available yet."
.to_string(),
);
};
let snapshot = match runtime_job_snapshot(runtime_store_path, job_id) {
Ok(Some(snapshot)) => snapshot,
Ok(None) => {
return (
ThreadEpiphanyReorientResultStatus::Pending,
None,
format!(
"Heartbeat runtime job {:?} has not reported typed state yet.",
job_id
),
);
}
Err(err) => {
return (
ThreadEpiphanyReorientResultStatus::BackendUnavailable,
None,
format!(
"Failed to read heartbeat runtime-spine job {:?}: {err}",
job_id
),
);
}
};
let status = map_runtime_reorient_result_status(&snapshot);
let finding = snapshot
.result
.as_ref()
.map(|result| map_protocol_reorient_finding(interpret_reorient_runtime_job_result(result)));
let note = render_epiphany_reorient_result_note(status, finding.as_ref(), None);
(status, finding, note)
}
fn map_runtime_role_result_status(
snapshot: &EpiphanyRuntimeJobSnapshot,
) -> ThreadEpiphanyRoleResultStatus {
match snapshot.job.status {
EpiphanyRuntimeJobStatus::Queued => ThreadEpiphanyRoleResultStatus::Pending,
EpiphanyRuntimeJobStatus::Running | EpiphanyRuntimeJobStatus::WaitingForReview => {
ThreadEpiphanyRoleResultStatus::Running
}
EpiphanyRuntimeJobStatus::Completed => {
if snapshot.result.is_some() {
ThreadEpiphanyRoleResultStatus::Completed
} else {
ThreadEpiphanyRoleResultStatus::Pending
}
}
EpiphanyRuntimeJobStatus::Failed => ThreadEpiphanyRoleResultStatus::Failed,
EpiphanyRuntimeJobStatus::Cancelled => ThreadEpiphanyRoleResultStatus::Cancelled,
}
}
fn map_runtime_reorient_result_status(
snapshot: &EpiphanyRuntimeJobSnapshot,
) -> ThreadEpiphanyReorientResultStatus {
match snapshot.job.status {
EpiphanyRuntimeJobStatus::Queued => ThreadEpiphanyReorientResultStatus::Pending,
EpiphanyRuntimeJobStatus::Running | EpiphanyRuntimeJobStatus::WaitingForReview => {
ThreadEpiphanyReorientResultStatus::Running
}
EpiphanyRuntimeJobStatus::Completed => {
if snapshot.result.is_some() {
ThreadEpiphanyReorientResultStatus::Completed
} else {
ThreadEpiphanyReorientResultStatus::Pending
}
}
EpiphanyRuntimeJobStatus::Failed => ThreadEpiphanyReorientResultStatus::Failed,
EpiphanyRuntimeJobStatus::Cancelled => ThreadEpiphanyReorientResultStatus::Cancelled,
}
}
async fn load_epiphany_role_result_snapshot(
state: &EpiphanyThreadState,
runtime_store_path: Option<&Path>,
role_id: ThreadEpiphanyRoleId,
binding_id: &str,
) -> (
ThreadEpiphanyRoleResultStatus,
Option<ThreadEpiphanyRoleFinding>,
String,
) {
if let Some(link) = latest_epiphany_runtime_link_for_binding(state, binding_id) {
return load_epiphany_role_result_from_runtime_spine_job(
link.runtime_job_id.as_str(),
runtime_store_path,
role_id,
);
}
if !state
.job_bindings
.iter()
.any(|binding| binding.id == binding_id)
{
return (
ThreadEpiphanyRoleResultStatus::MissingBinding,
None,
"No matching Epiphany role specialist binding exists.".to_string(),
);
}
(
ThreadEpiphanyRoleResultStatus::BackendUnavailable,
None,
"Role binding has no runtime-spine job id; launch a runtime-linked role worker for typed results.".to_string(),
)
}
async fn load_epiphany_reorient_result_snapshot(
state: Option<&EpiphanyThreadState>,
runtime_store_path: Option<&Path>,
binding_id: &str,
) -> (
ThreadEpiphanyReorientResultStatus,
Option<ThreadEpiphanyReorientFinding>,
String,
) {
let Some(state) = state else {
return (
ThreadEpiphanyReorientResultStatus::MissingState,
None,
"No authoritative Epiphany state exists for this thread.".to_string(),
);
};
if let Some(link) = latest_epiphany_runtime_link_for_binding(state, binding_id) {
return load_epiphany_reorient_result_from_runtime_spine_job(
link.runtime_job_id.as_str(),
runtime_store_path,
);
}
if !state
.job_bindings
.iter()
.any(|binding| binding.id == binding_id)
{
return (
ThreadEpiphanyReorientResultStatus::MissingBinding,
None,
"No matching Epiphany reorientation worker binding exists.".to_string(),
);
}
(
ThreadEpiphanyReorientResultStatus::BackendUnavailable,
None,
"Reorientation binding has no runtime-spine job id; launch a runtime-linked reorient worker for typed results.".to_string(),
)
}
fn latest_epiphany_runtime_link_for_binding<'a>(
state: &'a EpiphanyThreadState,
binding_id: &str,
) -> Option<&'a EpiphanyRuntimeLink> {
state
.runtime_links
.iter()
.find(|link| link.binding_id == binding_id && !link.runtime_job_id.trim().is_empty())
}
pub(crate) async fn maybe_run_epiphany_coordinator_automation_for_turn_boundary(
thread_id: ThreadId,
thread: Arc<CodexThread>,

View file

@ -0,0 +1,249 @@
use super::*;
pub(super) fn role_finding_runtime_result_id(
finding: &ThreadEpiphanyRoleFinding,
) -> Option<String> {
finding.runtime_result_id.clone()
}
pub(super) fn role_finding_runtime_job_id(finding: &ThreadEpiphanyRoleFinding) -> Option<String> {
finding.runtime_job_id.clone()
}
pub(super) fn reorient_finding_runtime_result_id(
finding: &ThreadEpiphanyReorientFinding,
) -> Option<String> {
finding.runtime_result_id.clone()
}
pub(super) fn reorient_finding_runtime_job_id(
finding: &ThreadEpiphanyReorientFinding,
) -> Option<String> {
finding.runtime_job_id.clone()
}
pub(super) fn load_epiphany_role_result_from_runtime_spine_job(
job_id: &str,
runtime_store_path: Option<&Path>,
role_id: ThreadEpiphanyRoleId,
) -> (
ThreadEpiphanyRoleResultStatus,
Option<ThreadEpiphanyRoleFinding>,
String,
) {
let Some(runtime_store_path) = runtime_store_path else {
return (
ThreadEpiphanyRoleResultStatus::Pending,
None,
"Heartbeat activation owns this role specialist; no loaded runtime-spine store is available yet."
.to_string(),
);
};
let snapshot = match runtime_job_snapshot(runtime_store_path, job_id) {
Ok(Some(snapshot)) => snapshot,
Ok(None) => {
return (
ThreadEpiphanyRoleResultStatus::Pending,
None,
format!(
"Heartbeat runtime job {:?} has not reported typed state yet.",
job_id
),
);
}
Err(err) => {
return (
ThreadEpiphanyRoleResultStatus::BackendUnavailable,
None,
format!(
"Failed to read heartbeat runtime-spine job {:?}: {err}",
job_id
),
);
}
};
let status = map_runtime_role_result_status(&snapshot);
let finding = snapshot.result.as_ref().map(|result| {
map_protocol_role_finding(
role_id,
interpret_role_runtime_job_result(map_core_role_result_role_id(role_id), result),
)
});
let note = render_epiphany_role_result_note(role_id, status, finding.as_ref(), None);
(status, finding, note)
}
pub(super) fn load_epiphany_reorient_result_from_runtime_spine_job(
job_id: &str,
runtime_store_path: Option<&Path>,
) -> (
ThreadEpiphanyReorientResultStatus,
Option<ThreadEpiphanyReorientFinding>,
String,
) {
let Some(runtime_store_path) = runtime_store_path else {
return (
ThreadEpiphanyReorientResultStatus::Pending,
None,
"Heartbeat activation owns this reorientation worker; no loaded runtime-spine store is available yet."
.to_string(),
);
};
let snapshot = match runtime_job_snapshot(runtime_store_path, job_id) {
Ok(Some(snapshot)) => snapshot,
Ok(None) => {
return (
ThreadEpiphanyReorientResultStatus::Pending,
None,
format!(
"Heartbeat runtime job {:?} has not reported typed state yet.",
job_id
),
);
}
Err(err) => {
return (
ThreadEpiphanyReorientResultStatus::BackendUnavailable,
None,
format!(
"Failed to read heartbeat runtime-spine job {:?}: {err}",
job_id
),
);
}
};
let status = map_runtime_reorient_result_status(&snapshot);
let finding = snapshot
.result
.as_ref()
.map(|result| map_protocol_reorient_finding(interpret_reorient_runtime_job_result(result)));
let note = render_epiphany_reorient_result_note(status, finding.as_ref(), None);
(status, finding, note)
}
fn map_runtime_role_result_status(
snapshot: &EpiphanyRuntimeJobSnapshot,
) -> ThreadEpiphanyRoleResultStatus {
match snapshot.job.status {
EpiphanyRuntimeJobStatus::Queued => ThreadEpiphanyRoleResultStatus::Pending,
EpiphanyRuntimeJobStatus::Running | EpiphanyRuntimeJobStatus::WaitingForReview => {
ThreadEpiphanyRoleResultStatus::Running
}
EpiphanyRuntimeJobStatus::Completed => {
if snapshot.result.is_some() {
ThreadEpiphanyRoleResultStatus::Completed
} else {
ThreadEpiphanyRoleResultStatus::Pending
}
}
EpiphanyRuntimeJobStatus::Failed => ThreadEpiphanyRoleResultStatus::Failed,
EpiphanyRuntimeJobStatus::Cancelled => ThreadEpiphanyRoleResultStatus::Cancelled,
}
}
fn map_runtime_reorient_result_status(
snapshot: &EpiphanyRuntimeJobSnapshot,
) -> ThreadEpiphanyReorientResultStatus {
match snapshot.job.status {
EpiphanyRuntimeJobStatus::Queued => ThreadEpiphanyReorientResultStatus::Pending,
EpiphanyRuntimeJobStatus::Running | EpiphanyRuntimeJobStatus::WaitingForReview => {
ThreadEpiphanyReorientResultStatus::Running
}
EpiphanyRuntimeJobStatus::Completed => {
if snapshot.result.is_some() {
ThreadEpiphanyReorientResultStatus::Completed
} else {
ThreadEpiphanyReorientResultStatus::Pending
}
}
EpiphanyRuntimeJobStatus::Failed => ThreadEpiphanyReorientResultStatus::Failed,
EpiphanyRuntimeJobStatus::Cancelled => ThreadEpiphanyReorientResultStatus::Cancelled,
}
}
pub(super) async fn load_epiphany_role_result_snapshot(
state: &EpiphanyThreadState,
runtime_store_path: Option<&Path>,
role_id: ThreadEpiphanyRoleId,
binding_id: &str,
) -> (
ThreadEpiphanyRoleResultStatus,
Option<ThreadEpiphanyRoleFinding>,
String,
) {
if let Some(link) = latest_epiphany_runtime_link_for_binding(state, binding_id) {
return load_epiphany_role_result_from_runtime_spine_job(
link.runtime_job_id.as_str(),
runtime_store_path,
role_id,
);
}
if !state
.job_bindings
.iter()
.any(|binding| binding.id == binding_id)
{
return (
ThreadEpiphanyRoleResultStatus::MissingBinding,
None,
"No matching Epiphany role specialist binding exists.".to_string(),
);
}
(
ThreadEpiphanyRoleResultStatus::BackendUnavailable,
None,
"Role binding has no runtime-spine job id; launch a runtime-linked role worker for typed results.".to_string(),
)
}
pub(super) async fn load_epiphany_reorient_result_snapshot(
state: Option<&EpiphanyThreadState>,
runtime_store_path: Option<&Path>,
binding_id: &str,
) -> (
ThreadEpiphanyReorientResultStatus,
Option<ThreadEpiphanyReorientFinding>,
String,
) {
let Some(state) = state else {
return (
ThreadEpiphanyReorientResultStatus::MissingState,
None,
"No authoritative Epiphany state exists for this thread.".to_string(),
);
};
if let Some(link) = latest_epiphany_runtime_link_for_binding(state, binding_id) {
return load_epiphany_reorient_result_from_runtime_spine_job(
link.runtime_job_id.as_str(),
runtime_store_path,
);
}
if !state
.job_bindings
.iter()
.any(|binding| binding.id == binding_id)
{
return (
ThreadEpiphanyReorientResultStatus::MissingBinding,
None,
"No matching Epiphany reorientation worker binding exists.".to_string(),
);
}
(
ThreadEpiphanyReorientResultStatus::BackendUnavailable,
None,
"Reorientation binding has no runtime-spine job id; launch a runtime-linked reorient worker for typed results.".to_string(),
)
}
pub(super) fn latest_epiphany_runtime_link_for_binding<'a>(
state: &'a EpiphanyThreadState,
binding_id: &str,
) -> Option<&'a EpiphanyRuntimeLink> {
state
.runtime_links
.iter()
.find(|link| link.binding_id == binding_id && !link.runtime_job_id.trim().is_empty())
}