From ea1d3c2e82336eb1dd19db291161b301c143769f Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 13 May 2026 08:42:13 +0100 Subject: [PATCH] Store typed worker result documents --- epiphany-codex-bridge/src/runtime_results.rs | 68 +++++-- epiphany-core/src/lib.rs | 12 ++ epiphany-core/src/runtime_spine.rs | 186 +++++++++++++++++++ epiphany-core/src/surfaces/mod.rs | 2 + epiphany-core/src/surfaces/role_result.rs | 84 +++++++++ epiphany-openai-runtime/Cargo.toml | 1 + epiphany-openai-runtime/src/lib.rs | 128 ++++++++++++- notes/fresh-workspace-handoff.md | 13 +- state/ledgers.msgpack | Bin 88180 -> 88689 bytes state/map.yaml | 4 +- 10 files changed, 473 insertions(+), 25 deletions(-) diff --git a/epiphany-codex-bridge/src/runtime_results.rs b/epiphany-codex-bridge/src/runtime_results.rs index 695971fa..4076f4bb 100644 --- a/epiphany-codex-bridge/src/runtime_results.rs +++ b/epiphany-codex-bridge/src/runtime_results.rs @@ -14,7 +14,11 @@ use epiphany_core::EpiphanyRuntimeJobSnapshot; use epiphany_core::EpiphanyRuntimeJobStatus; use epiphany_core::interpret_reorient_runtime_job_result; use epiphany_core::interpret_role_runtime_job_result; +use epiphany_core::interpret_runtime_reorient_worker_result; +use epiphany_core::interpret_runtime_role_worker_result; use epiphany_core::runtime_job_snapshot; +use epiphany_core::runtime_reorient_worker_result; +use epiphany_core::runtime_role_worker_result; use crate::results::map_core_role_result_role_id; use crate::results::map_protocol_reorient_finding; @@ -22,9 +26,7 @@ use crate::results::map_protocol_role_finding; use crate::results::render_epiphany_reorient_result_note; use crate::results::render_epiphany_role_result_note; -pub fn role_finding_runtime_result_id( - finding: &ThreadEpiphanyRoleFinding, -) -> Option { +pub fn role_finding_runtime_result_id(finding: &ThreadEpiphanyRoleFinding) -> Option { finding.runtime_result_id.clone() } @@ -38,9 +40,7 @@ pub fn reorient_finding_runtime_result_id( finding.runtime_result_id.clone() } -pub fn reorient_finding_runtime_job_id( - finding: &ThreadEpiphanyReorientFinding, -) -> Option { +pub fn reorient_finding_runtime_job_id(finding: &ThreadEpiphanyReorientFinding) -> Option { finding.runtime_job_id.clone() } @@ -85,12 +85,37 @@ pub fn load_epiphany_role_result_from_runtime_spine_job( } }; 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 finding = if status == ThreadEpiphanyRoleResultStatus::Completed { + match runtime_role_worker_result(runtime_store_path, job_id) { + Ok(Some(result)) => Some(map_protocol_role_finding( + role_id, + interpret_runtime_role_worker_result( + map_core_role_result_role_id(role_id), + &result, + ), + )), + Ok(None) => 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, + ), + ) + }), + Err(_) => 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, + ), + ) + }), + } + } else { + None + }; let note = render_epiphany_role_result_note(role_id, status, finding.as_ref(), None); (status, finding, note) } @@ -135,10 +160,21 @@ pub fn load_epiphany_reorient_result_from_runtime_spine_job( } }; 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 finding = if status == ThreadEpiphanyReorientResultStatus::Completed { + match runtime_reorient_worker_result(runtime_store_path, job_id) { + Ok(Some(result)) => Some(map_protocol_reorient_finding( + interpret_runtime_reorient_worker_result(&result), + )), + Ok(None) => snapshot.result.as_ref().map(|result| { + map_protocol_reorient_finding(interpret_reorient_runtime_job_result(result)) + }), + Err(_) => snapshot.result.as_ref().map(|result| { + map_protocol_reorient_finding(interpret_reorient_runtime_job_result(result)) + }), + } + } else { + None + }; let note = render_epiphany_reorient_result_note(status, finding.as_ref(), None); (status, finding, note) } diff --git a/epiphany-core/src/lib.rs b/epiphany-core/src/lib.rs index 075b52e0..6a342595 100644 --- a/epiphany-core/src/lib.rs +++ b/epiphany-core/src/lib.rs @@ -96,6 +96,8 @@ pub use runtime_spine::EpiphanyRuntimeJob; pub use runtime_spine::EpiphanyRuntimeJobResult; pub use runtime_spine::EpiphanyRuntimeJobSnapshot; pub use runtime_spine::EpiphanyRuntimeJobStatus; +pub use runtime_spine::EpiphanyRuntimeReorientWorkerResult; +pub use runtime_spine::EpiphanyRuntimeRoleWorkerResult; pub use runtime_spine::EpiphanyRuntimeSession; pub use runtime_spine::EpiphanyRuntimeSessionStatus; pub use runtime_spine::EpiphanyRuntimeSpineStatus; @@ -105,6 +107,10 @@ pub use runtime_spine::RUNTIME_IDENTITY_KEY; pub use runtime_spine::RUNTIME_IDENTITY_TYPE; pub use runtime_spine::RUNTIME_JOB_RESULT_TYPE; pub use runtime_spine::RUNTIME_JOB_TYPE; +pub use runtime_spine::RUNTIME_REORIENT_WORKER_RESULT_SCHEMA_VERSION; +pub use runtime_spine::RUNTIME_REORIENT_WORKER_RESULT_TYPE; +pub use runtime_spine::RUNTIME_ROLE_WORKER_RESULT_SCHEMA_VERSION; +pub use runtime_spine::RUNTIME_ROLE_WORKER_RESULT_TYPE; pub use runtime_spine::RUNTIME_SESSION_TYPE; pub use runtime_spine::RUNTIME_SPINE_SCHEMA_VERSION; pub use runtime_spine::RUNTIME_WORKER_LAUNCH_REQUEST_SCHEMA_VERSION; @@ -125,8 +131,12 @@ pub use runtime_spine::ensure_runtime_session; pub use runtime_spine::initialize_runtime_spine; pub use runtime_spine::open_runtime_spine_heartbeat_job; pub use runtime_spine::plan_runtime_spine_heartbeat_launch; +pub use runtime_spine::put_runtime_reorient_worker_result; +pub use runtime_spine::put_runtime_role_worker_result; pub use runtime_spine::runtime_hello_frame; pub use runtime_spine::runtime_job_snapshot; +pub use runtime_spine::runtime_reorient_worker_result; +pub use runtime_spine::runtime_role_worker_result; pub use runtime_spine::runtime_schema_catalog_response; pub use runtime_spine::runtime_spine_cache; pub use runtime_spine::runtime_spine_status; @@ -251,6 +261,8 @@ pub use surfaces::interpret_reorient_finding; pub use surfaces::interpret_reorient_runtime_job_result; pub use surfaces::interpret_role_finding; pub use surfaces::interpret_role_runtime_job_result; +pub use surfaces::interpret_runtime_reorient_worker_result; +pub use surfaces::interpret_runtime_role_worker_result; pub use surfaces::modeling_role_state_patch_policy_errors; pub use surfaces::recommend_coordinator_action; pub use surfaces::recommend_crrc_action; diff --git a/epiphany-core/src/runtime_spine.rs b/epiphany-core/src/runtime_spine.rs index eab468e4..07d05c51 100644 --- a/epiphany-core/src/runtime_spine.rs +++ b/epiphany-core/src/runtime_spine.rs @@ -40,6 +40,8 @@ pub const RUNTIME_IDENTITY_TYPE: &str = "epiphany.runtime.identity"; pub const RUNTIME_SESSION_TYPE: &str = "epiphany.runtime.session"; pub const RUNTIME_JOB_TYPE: &str = "epiphany.runtime.job"; pub const RUNTIME_WORKER_LAUNCH_REQUEST_TYPE: &str = "epiphany.runtime.worker_launch_request"; +pub const RUNTIME_ROLE_WORKER_RESULT_TYPE: &str = "epiphany.runtime.role_worker_result"; +pub const RUNTIME_REORIENT_WORKER_RESULT_TYPE: &str = "epiphany.runtime.reorient_worker_result"; pub const RUNTIME_JOB_RESULT_TYPE: &str = "epiphany.runtime.job_result"; pub const RUNTIME_EVENT_TYPE: &str = "epiphany.runtime.event"; pub const OPENAI_ADAPTER_STATUS_TYPE: &str = "epiphany.openai_adapter_status.v0"; @@ -69,6 +71,10 @@ pub const RUNTIME_IDENTITY_KEY: &str = "self"; pub const RUNTIME_SPINE_SCHEMA_VERSION: &str = "epiphany.runtime_spine.v0"; pub const RUNTIME_WORKER_LAUNCH_REQUEST_SCHEMA_VERSION: &str = "epiphany.runtime.worker_launch_request.v0"; +pub const RUNTIME_ROLE_WORKER_RESULT_SCHEMA_VERSION: &str = + "epiphany.runtime.role_worker_result.v0"; +pub const RUNTIME_REORIENT_WORKER_RESULT_SCHEMA_VERSION: &str = + "epiphany.runtime.reorient_worker_result.v0"; pub const OPENAI_ADAPTER_STATUS_SCHEMA_VERSION: &str = "epiphany.openai_adapter_status.v0"; pub const OPENAI_MODEL_REQUEST_SCHEMA_VERSION: &str = "epiphany.openai_model_request.v0"; pub const OPENAI_MODEL_STREAM_EVENT_SCHEMA_VERSION: &str = "epiphany.openai_model_stream_event.v0"; @@ -230,6 +236,105 @@ impl EpiphanyRuntimeWorkerLaunchRequest { } } +#[derive(Clone, Debug, PartialEq, DatabaseEntry)] +#[cultcache( + type = "epiphany.runtime.role_worker_result", + schema = "EpiphanyRuntimeRoleWorkerResult" +)] +pub struct EpiphanyRuntimeRoleWorkerResult { + #[cultcache(key = 0)] + pub schema_version: String, + #[cultcache(key = 1)] + pub result_id: String, + #[cultcache(key = 2)] + pub job_id: String, + #[cultcache(key = 3)] + pub role_id: String, + #[cultcache(key = 4)] + pub verdict: String, + #[cultcache(key = 5)] + pub summary: String, + #[cultcache(key = 6)] + pub next_safe_move: String, + #[cultcache(key = 7, default)] + pub checkpoint_summary: Option, + #[cultcache(key = 8, default)] + pub scratch_summary: Option, + #[cultcache(key = 9, default)] + pub files_inspected: Vec, + #[cultcache(key = 10, default)] + pub frontier_node_ids: Vec, + #[cultcache(key = 11, default)] + pub evidence_ids: Vec, + #[cultcache(key = 12, default)] + pub artifact_refs: Vec, + #[cultcache(key = 13, default)] + pub open_questions: Vec, + #[cultcache(key = 14, default)] + pub evidence_gaps: Vec, + #[cultcache(key = 15, default)] + pub risks: Vec, + #[cultcache(key = 16, default)] + pub state_patch_msgpack: Option>, + #[cultcache(key = 17, default)] + pub self_patch_msgpack: Option>, + #[cultcache(key = 18, default)] + pub item_error: Option, + #[cultcache(key = 19, default)] + pub metadata: BTreeMap, +} + +impl EpiphanyRuntimeRoleWorkerResult { + pub fn state_patch(&self) -> Result> { + decode_optional_msgpack( + self.state_patch_msgpack.as_deref(), + "role worker statePatch", + ) + } + + pub fn self_patch(&self) -> Result> { + decode_optional_msgpack(self.self_patch_msgpack.as_deref(), "role worker selfPatch") + } +} + +#[derive(Clone, Debug, PartialEq, DatabaseEntry)] +#[cultcache( + type = "epiphany.runtime.reorient_worker_result", + schema = "EpiphanyRuntimeReorientWorkerResult" +)] +pub struct EpiphanyRuntimeReorientWorkerResult { + #[cultcache(key = 0)] + pub schema_version: String, + #[cultcache(key = 1)] + pub result_id: String, + #[cultcache(key = 2)] + pub job_id: String, + #[cultcache(key = 3)] + pub mode: String, + #[cultcache(key = 4)] + pub summary: String, + #[cultcache(key = 5)] + pub next_safe_move: String, + #[cultcache(key = 6, default)] + pub checkpoint_still_valid: Option, + #[cultcache(key = 7, default)] + pub files_inspected: Vec, + #[cultcache(key = 8, default)] + pub frontier_node_ids: Vec, + #[cultcache(key = 9, default)] + pub evidence_ids: Vec, + #[cultcache(key = 10, default)] + pub artifact_refs: Vec, + #[cultcache(key = 11, default)] + pub open_questions: Vec, + #[cultcache(key = 12, default)] + pub continuity_risks: Vec, + #[cultcache(key = 13, default)] + pub item_error: Option, + #[cultcache(key = 14, default)] + pub metadata: BTreeMap, +} + #[derive(Clone, Debug, PartialEq, DatabaseEntry)] #[cultcache( type = "epiphany.runtime.job_result", @@ -424,6 +529,8 @@ pub fn runtime_spine_cache(store_path: impl AsRef) -> Result { cache.register_entry_type::()?; cache.register_entry_type::()?; cache.register_entry_type::()?; + cache.register_entry_type::()?; + cache.register_entry_type::()?; cache.register_entry_type::()?; cache.register_entry_type::()?; cache.register_entry_type::()?; @@ -737,6 +844,52 @@ pub fn runtime_job_snapshot( Ok(Some(EpiphanyRuntimeJobSnapshot { job, result })) } +pub fn put_runtime_role_worker_result( + store_path: impl AsRef, + result: &EpiphanyRuntimeRoleWorkerResult, +) -> Result<()> { + validate_non_empty(&result.job_id, "role worker result job id")?; + validate_non_empty(&result.result_id, "role worker result id")?; + validate_non_empty(&result.role_id, "role worker result role id")?; + let mut cache = runtime_spine_cache(store_path)?; + cache.pull_all_backing_stores()?; + cache.put(&result.job_id, result)?; + Ok(()) +} + +pub fn runtime_role_worker_result( + store_path: impl AsRef, + job_id: &str, +) -> Result> { + validate_non_empty(job_id, "role worker result job id")?; + let mut cache = runtime_spine_cache(store_path)?; + cache.pull_all_backing_stores()?; + cache.get::(job_id) +} + +pub fn put_runtime_reorient_worker_result( + store_path: impl AsRef, + result: &EpiphanyRuntimeReorientWorkerResult, +) -> Result<()> { + validate_non_empty(&result.job_id, "reorient worker result job id")?; + validate_non_empty(&result.result_id, "reorient worker result id")?; + validate_non_empty(&result.mode, "reorient worker result mode")?; + let mut cache = runtime_spine_cache(store_path)?; + cache.pull_all_backing_stores()?; + cache.put(&result.job_id, result)?; + Ok(()) +} + +pub fn runtime_reorient_worker_result( + store_path: impl AsRef, + job_id: &str, +) -> Result> { + validate_non_empty(job_id, "reorient worker result job id")?; + let mut cache = runtime_spine_cache(store_path)?; + cache.pull_all_backing_stores()?; + cache.get::(job_id) +} + pub fn complete_runtime_job( store_path: impl AsRef, options: RuntimeSpineJobResultOptions, @@ -1168,6 +1321,28 @@ fn epiphany_mutation_contracts() -> Vec { "Worker launch requests are typed task-intent documents; runtime jobs are lifecycle receipts, not the source of work intent.", ], ), + mutation_contract( + RUNTIME_ROLE_WORKER_RESULT_TYPE, + RUNTIME_ROLE_WORKER_RESULT_SCHEMA_VERSION, + vec![CultNetDocumentOperation::Snapshot], + CultNetMutationAuthority::ReadOnly, + vec![], + vec![], + vec![ + "Role worker results preserve the typed finding payload; generic runtime job results are lifecycle receipts.", + ], + ), + mutation_contract( + RUNTIME_REORIENT_WORKER_RESULT_TYPE, + RUNTIME_REORIENT_WORKER_RESULT_SCHEMA_VERSION, + vec![CultNetDocumentOperation::Snapshot], + CultNetMutationAuthority::ReadOnly, + vec![], + vec![], + vec![ + "Reorient worker results preserve continuity findings separately from generic runtime lifecycle receipts.", + ], + ), mutation_contract( RUNTIME_JOB_RESULT_TYPE, RUNTIME_SPINE_SCHEMA_VERSION, @@ -1508,6 +1683,17 @@ fn encode_worker_launch_document(document: &EpiphanyWorkerLaunchDocument) -> Res rmp_serde::to_vec_named(document).context("failed to encode worker launch document MessagePack") } +fn decode_optional_msgpack(payload: Option<&[u8]>, label: &str) -> Result> +where + T: for<'de> Deserialize<'de>, +{ + payload + .map(|payload| { + rmp_serde::from_slice(payload).with_context(|| format!("failed to decode {label}")) + }) + .transpose() +} + fn validate_heartbeat_launch_options( state: &EpiphanyThreadState, options: &RuntimeSpineHeartbeatLaunchPlanOptions, diff --git a/epiphany-core/src/surfaces/mod.rs b/epiphany-core/src/surfaces/mod.rs index 0781e333..0bf132d2 100644 --- a/epiphany-core/src/surfaces/mod.rs +++ b/epiphany-core/src/surfaces/mod.rs @@ -107,6 +107,8 @@ pub use role_result::interpret_reorient_finding; pub use role_result::interpret_reorient_runtime_job_result; pub use role_result::interpret_role_finding; pub use role_result::interpret_role_runtime_job_result; +pub use role_result::interpret_runtime_reorient_worker_result; +pub use role_result::interpret_runtime_role_worker_result; pub use role_result::modeling_role_state_patch_policy_errors; pub use role_result::review_role_self_patch; pub use role_result::role_self_memory_target; diff --git a/epiphany-core/src/surfaces/role_result.rs b/epiphany-core/src/surfaces/role_result.rs index 5d014303..cdb62f19 100644 --- a/epiphany-core/src/surfaces/role_result.rs +++ b/epiphany-core/src/surfaces/role_result.rs @@ -222,6 +222,71 @@ pub fn interpret_role_runtime_job_result( } } +pub fn interpret_runtime_role_worker_result( + role_id: EpiphanyRoleResultRoleId, + result: &crate::EpiphanyRuntimeRoleWorkerResult, +) -> EpiphanyRoleFindingInterpretation { + let state_patch_result = result.state_patch(); + let self_patch_result = result.self_patch(); + let state_patch = state_patch_result + .as_ref() + .ok() + .and_then(|patch| patch.clone()); + let self_patch = self_patch_result + .as_ref() + .ok() + .and_then(|patch| patch.clone()); + let item_error = merge_item_error( + result.item_error.clone(), + state_patch_result.as_ref().err().map(ToString::to_string), + ); + let item_error = merge_item_error( + item_error, + self_patch_result.as_ref().err().map(ToString::to_string), + ); + EpiphanyRoleFindingInterpretation { + verdict: Some(result.verdict.clone()), + summary: Some(result.summary.clone()), + next_safe_move: empty_string_as_none(&result.next_safe_move), + checkpoint_summary: result.checkpoint_summary.clone(), + scratch_summary: result.scratch_summary.clone(), + files_inspected: result.files_inspected.clone(), + frontier_node_ids: result.frontier_node_ids.clone(), + evidence_ids: result.evidence_ids.clone(), + artifact_refs: result.artifact_refs.clone(), + runtime_result_id: Some(result.result_id.clone()), + runtime_job_id: Some(result.job_id.clone()), + open_questions: result.open_questions.clone(), + evidence_gaps: result.evidence_gaps.clone(), + risks: result.risks.clone(), + state_patch: state_patch.clone(), + self_patch, + self_persistence: None, + job_error: None, + item_error: match role_id { + EpiphanyRoleResultRoleId::Modeling => merge_item_error( + item_error, + modeling_role_state_patch_error( + &serde_json::Value::Null, + state_patch.as_ref(), + None, + ), + ), + EpiphanyRoleResultRoleId::Imagination => merge_item_error( + item_error, + imagination_role_state_patch_error( + &serde_json::Value::Null, + state_patch.as_ref(), + None, + ), + ), + EpiphanyRoleResultRoleId::Implementation + | EpiphanyRoleResultRoleId::Verification + | EpiphanyRoleResultRoleId::Reorientation => item_error, + }, + } +} + pub fn interpret_reorient_runtime_job_result( result: &EpiphanyRuntimeJobResult, ) -> EpiphanyReorientFindingInterpretation { @@ -241,6 +306,25 @@ pub fn interpret_reorient_runtime_job_result( } } +pub fn interpret_runtime_reorient_worker_result( + result: &crate::EpiphanyRuntimeReorientWorkerResult, +) -> EpiphanyReorientFindingInterpretation { + EpiphanyReorientFindingInterpretation { + mode: Some(result.mode.clone()), + summary: Some(result.summary.clone()), + next_safe_move: Some(result.next_safe_move.clone()), + checkpoint_still_valid: result.checkpoint_still_valid, + files_inspected: result.files_inspected.clone(), + frontier_node_ids: result.frontier_node_ids.clone(), + evidence_ids: result.evidence_ids.clone(), + artifact_refs: result.artifact_refs.clone(), + runtime_result_id: Some(result.result_id.clone()), + runtime_job_id: Some(result.job_id.clone()), + job_error: None, + item_error: result.item_error.clone(), + } +} + pub fn build_role_acceptance_bundle( binding_id: &str, role: EpiphanyRoleAcceptanceFinding, diff --git a/epiphany-openai-runtime/Cargo.toml b/epiphany-openai-runtime/Cargo.toml index 625e1214..27c4a1bd 100644 --- a/epiphany-openai-runtime/Cargo.toml +++ b/epiphany-openai-runtime/Cargo.toml @@ -16,6 +16,7 @@ codex-login = { path = "../vendor/codex/codex-rs/login" } epiphany-core = { path = "../epiphany-core" } epiphany-openai-adapter = { path = "../epiphany-openai-adapter" } epiphany-openai-codex-spine = { path = "../epiphany-openai-codex-spine" } +rmp-serde = "1" serde = { version = "1", features = ["derive"] } serde_json = "1" tokio = { version = "1", features = ["macros", "rt-multi-thread"] } diff --git a/epiphany-openai-runtime/src/lib.rs b/epiphany-openai-runtime/src/lib.rs index 6b73e972..32891c6e 100644 --- a/epiphany-openai-runtime/src/lib.rs +++ b/epiphany-openai-runtime/src/lib.rs @@ -8,6 +8,8 @@ use anyhow::anyhow; use chrono::SecondsFormat; use codex_login::AuthCredentialsStoreMode; use codex_login::AuthManager; +use epiphany_core::EpiphanyRuntimeReorientWorkerResult; +use epiphany_core::EpiphanyRuntimeRoleWorkerResult; use epiphany_core::EpiphanyRuntimeWorkerLaunchRequest; use epiphany_core::EpiphanyWorkerLaunchDocument; use epiphany_core::RuntimeSpineEventOptions; @@ -20,6 +22,8 @@ use epiphany_core::complete_runtime_job; use epiphany_core::create_runtime_job; use epiphany_core::ensure_runtime_session; use epiphany_core::initialize_runtime_spine; +use epiphany_core::put_runtime_reorient_worker_result; +use epiphany_core::put_runtime_role_worker_result; use epiphany_core::runtime_spine_cache; use epiphany_core::runtime_spine_status; use epiphany_openai_adapter::EpiphanyOpenAiAdapterStatus; @@ -357,10 +361,34 @@ pub fn complete_worker_job_from_assistant_text( .map(|value| string_array_field(value, "artifactRefs")) .unwrap_or_default(); artifact_refs.push(format!("openai-result:{}", openai_summary.result_id)); + let result_id = format!("result-worker-{}", launch_request.job_id); + if let Some(parsed) = parsed.as_ref() { + match launch_request.launch_document()? { + EpiphanyWorkerLaunchDocument::Role(document) => { + let typed_result = role_worker_result_from_value( + launch_request, + &document.role_id, + &result_id, + parsed, + artifact_refs.clone(), + ); + put_runtime_role_worker_result(store_path.as_ref(), &typed_result)?; + } + EpiphanyWorkerLaunchDocument::Reorient(_) => { + let typed_result = reorient_worker_result_from_value( + launch_request, + &result_id, + parsed, + artifact_refs.clone(), + ); + put_runtime_reorient_worker_result(store_path.as_ref(), &typed_result)?; + } + } + } complete_runtime_job( store_path, RuntimeSpineJobResultOptions { - result_id: format!("result-worker-{}", launch_request.job_id), + result_id, job_id: launch_request.job_id.clone(), completed_at: now(), verdict, @@ -537,6 +565,99 @@ fn worker_output_contract_text(document: &EpiphanyWorkerLaunchDocument) -> &'sta } } +fn role_worker_result_from_value( + launch_request: &EpiphanyRuntimeWorkerLaunchRequest, + role_id: &str, + result_id: &str, + value: &Value, + artifact_refs: Vec, +) -> EpiphanyRuntimeRoleWorkerResult { + let (state_patch_msgpack, state_patch_error) = encode_optional_value_field::< + epiphany_core::EpiphanyRoleStatePatchDocument, + >(value, "statePatch"); + let (self_patch_msgpack, self_patch_error) = + encode_optional_value_field::(value, "selfPatch"); + EpiphanyRuntimeRoleWorkerResult { + schema_version: epiphany_core::RUNTIME_ROLE_WORKER_RESULT_SCHEMA_VERSION.to_string(), + result_id: result_id.to_string(), + job_id: launch_request.job_id.clone(), + role_id: string_field(value, "roleId").unwrap_or_else(|| role_id.to_string()), + verdict: string_field(value, "verdict").unwrap_or_else(|| "completed".to_string()), + summary: string_field(value, "summary") + .unwrap_or_else(|| "Worker completed without a structured summary.".to_string()), + next_safe_move: string_field(value, "nextSafeMove").unwrap_or_else(|| { + "Review the typed worker runtime result before accepting state.".to_string() + }), + checkpoint_summary: string_field(value, "checkpointSummary"), + scratch_summary: string_field(value, "scratchSummary"), + files_inspected: string_array_field(value, "filesInspected"), + frontier_node_ids: string_array_field(value, "frontierNodeIds"), + evidence_ids: string_array_field(value, "evidenceIds"), + artifact_refs, + open_questions: string_array_field(value, "openQuestions"), + evidence_gaps: string_array_field(value, "evidenceGaps"), + risks: string_array_field(value, "risks"), + state_patch_msgpack, + self_patch_msgpack, + item_error: merge_optional_errors(state_patch_error, self_patch_error), + metadata: std::collections::BTreeMap::new(), + } +} + +fn reorient_worker_result_from_value( + launch_request: &EpiphanyRuntimeWorkerLaunchRequest, + result_id: &str, + value: &Value, + artifact_refs: Vec, +) -> EpiphanyRuntimeReorientWorkerResult { + EpiphanyRuntimeReorientWorkerResult { + schema_version: epiphany_core::RUNTIME_REORIENT_WORKER_RESULT_SCHEMA_VERSION.to_string(), + result_id: result_id.to_string(), + job_id: launch_request.job_id.clone(), + mode: string_field(value, "mode").unwrap_or_else(|| "regather".to_string()), + summary: string_field(value, "summary").unwrap_or_else(|| { + "Reorient worker completed without a structured summary.".to_string() + }), + next_safe_move: string_field(value, "nextSafeMove").unwrap_or_else(|| { + "Review the typed reorient runtime result before accepting state.".to_string() + }), + checkpoint_still_valid: value.get("checkpointStillValid").and_then(Value::as_bool), + files_inspected: string_array_field(value, "filesInspected"), + frontier_node_ids: string_array_field(value, "frontierNodeIds"), + evidence_ids: string_array_field(value, "evidenceIds"), + artifact_refs, + open_questions: string_array_field(value, "openQuestions"), + continuity_risks: string_array_field(value, "continuityRisks"), + item_error: None, + metadata: std::collections::BTreeMap::new(), + } +} + +fn encode_optional_value_field(value: &Value, key: &str) -> (Option>, Option) +where + T: serde::Serialize + serde::de::DeserializeOwned, +{ + let Some(field) = value.get(key).cloned() else { + return (None, None); + }; + match serde_json::from_value::(field) { + Ok(document) => match rmp_serde::to_vec_named(&document) { + Ok(payload) => (Some(payload), None), + Err(err) => (None, Some(format!("failed to encode {key}: {err}"))), + }, + Err(err) => (None, Some(format!("failed to parse {key}: {err}"))), + } +} + +fn merge_optional_errors(left: Option, right: Option) -> Option { + match (left, right) { + (Some(left), Some(right)) => Some(format!("{left}; {right}")), + (Some(left), None) => Some(left), + (None, Some(right)) => Some(right), + (None, None) => None, + } +} + fn parse_assistant_json_object(text: &str) -> Result { let trimmed = text.trim(); let candidate = trimmed @@ -742,6 +863,11 @@ mod tests { assert_eq!(result.summary, "Mapped."); assert_eq!(result.next_safe_move, "Review the patch."); assert!(result.evidence_refs.contains(&"ev-1".to_string())); + let typed_result = epiphany_core::runtime_role_worker_result(&store, "worker-job-1")? + .expect("typed role worker result"); + assert_eq!(typed_result.verdict, "checkpoint-ready"); + assert_eq!(typed_result.files_inspected, vec!["src/lib.rs".to_string()]); + assert_eq!(typed_result.artifact_refs, result.artifact_refs); assert!( runtime_job_snapshot(&store, "worker-job-1")? .expect("snapshot") diff --git a/notes/fresh-workspace-handoff.md b/notes/fresh-workspace-handoff.md index 7349fc02..4bd59a44 100644 --- a/notes/fresh-workspace-handoff.md +++ b/notes/fresh-workspace-handoff.md @@ -588,12 +588,13 @@ results, and runtime events into the native runtime spine. Its `model-turn` command calls the Codex-backed typed transport; its `smoke` command proves the storage route without network. Its `run-worker` command now reads a durable `EpiphanyRuntimeWorkerLaunchRequest` by runtime job id, builds a typed OpenAI -model request, calls the native runtime route, and completes the original -heartbeat/specialist runtime job without Codex worker execution. The remaining -result wound is deliberate and named: rich role/reorient output is still parsed -from model-edge JSON into generic job-result fields. The next cut is a -first-class typed runtime worker result document plus roleResult/reorientResult -readback through that document, then carving down the `codex-api` dependency +model request, calls the native runtime route, persists typed +`EpiphanyRuntimeRoleWorkerResult` or `EpiphanyRuntimeReorientWorkerResult` +documents, and completes the original heartbeat/specialist runtime job without +Codex worker execution. `roleResult` and `reorientResult` now prefer those typed +worker result documents and use generic job summaries only as legacy fallback. +The next cut is wiring coordinator/heartbeat automation to invoke `run-worker` +for launched runtime job ids, then carving down the `codex-api` dependency weight. The runtime-spine job-opening mechanism for heartbeat/specialist launches has diff --git a/state/ledgers.msgpack b/state/ledgers.msgpack index ae06cf800a4dec53edc731bfacd2fc71f1c2f8c5..30cbe11fa5899b2bfe7a5ab457ecaf7a2a5a16db 100644 GIT binary patch delta 404 zcmZXQ&q@MO6o2f*zn-GcBU+Ip5*;o!{qU`eTy5T~D=3PaAiHykF1A^Nf&(hMpdf zrMXgh$1dCUxYFEAO&wink>yJog=LTt51Ega3K&P=;sPZ0nF>)VI0{%WaQUbfGd1C1 zQ^+C88mh<(!R6h53<}ARhkv-LfvYYZAan(de6v>jfcmvgviiP3=AFQ#nM?)Y0Db77 zVqJtja+HjM$9hOd)I&HE9b@+$ZSJM9i9PZ%2~D&Di9MA1utio^6o_+_;0dKhpd#OQ zjh-6c@js^UqC$hc@dKuEGi`a7IqpJ7NXJ4T}_IG Q;+|F5wd`%DkaXSt0?j0o00000 delta 49 zcmeykh4sq@)(Of?5mp;jr!g`