From dbbc5f11d3820c52e3a6e1fe21fe027ddfa0d3b3 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 18 May 2026 07:57:11 +0100 Subject: [PATCH] Type heartbeat candidate interventions --- epiphany-core/src/heartbeat_state.rs | 65 ++++++++++-------- .../heartbeat_state/heartbeat_documents.rs | 25 ++++++- .../heartbeat_state/heartbeat_projection.rs | 2 +- .../src/heartbeat_state/heartbeat_store.rs | 12 +++- .../src/memory_graph/profiles/heartbeat.rs | 35 +++++----- notes/fresh-workspace-handoff.md | 1 + state/ledgers.msgpack | Bin 148599 -> 149002 bytes state/map.yaml | 3 + 8 files changed, 91 insertions(+), 52 deletions(-) diff --git a/epiphany-core/src/heartbeat_state.rs b/epiphany-core/src/heartbeat_state.rs index d838a284..6c9bd685 100644 --- a/epiphany-core/src/heartbeat_state.rs +++ b/epiphany-core/src/heartbeat_state.rs @@ -1359,7 +1359,7 @@ fn build_memory_resonance(records: &[RoleMemoryRecord]) -> Value { fn build_incubation( previous: &Option, bridge: &Option, - candidate_interventions: &Option, + candidate_interventions: &Option, resonance: &Value, records: &[RoleMemoryRecord], ) -> Value { @@ -1846,7 +1846,10 @@ fn build_thought_bridge( }) } -fn build_candidate_interventions(bridge: &Value, incubation: &Value) -> Value { +fn build_candidate_interventions( + bridge: &Value, + incubation: &Value, +) -> HeartbeatCandidateInterventions { let decision = bridge .pointer("/decision/speakDecision") .and_then(Value::as_str) @@ -1863,27 +1866,37 @@ fn build_candidate_interventions(bridge: &Value, incubation: &Value) -> Value { let items = if decision == "draft" && strongest_status == "ripe" { strongest_theme .map(|theme| { - vec![serde_json::json!({ - "interventionId": format!("candidate-{}", theme.get("themeId").and_then(Value::as_str).unwrap_or("theme")), - "summary": "Possible Aquarium-facing thought-weather note", - "draft": format!("I keep seeing {} rhyme across the swarm; this one finally has enough blood to inspect in the open.", theme.get("themeId").and_then(Value::as_str).unwrap_or("an unnamed seam")), - "decision": decision, - "requiresFace": true, - "requiresReview": true, - "noveltyToRoom": theme.get("noveltyToRoom").cloned().unwrap_or(Value::Null), - "saturationScore": theme.get("saturationScore").cloned().unwrap_or(Value::Null), - "createdAt": now_iso(), - })] + let theme_id = theme + .get("themeId") + .and_then(Value::as_str) + .unwrap_or("theme"); + vec![HeartbeatCandidateIntervention { + intervention_id: format!("candidate-{theme_id}"), + summary: "Possible Aquarium-facing thought-weather note".to_string(), + draft: format!("I keep seeing {} rhyme across the swarm; this one finally has enough blood to inspect in the open.", theme.get("themeId").and_then(Value::as_str).unwrap_or("an unnamed seam")), + decision: decision.to_string(), + requires_face: true, + requires_review: true, + novelty_to_room: theme + .get("noveltyToRoom") + .and_then(Value::as_f64) + .unwrap_or_default(), + saturation_score: theme + .get("saturationScore") + .and_then(Value::as_f64) + .unwrap_or_default(), + created_at: now_iso(), + }] }) .unwrap_or_default() } else { Vec::new() }; - serde_json::json!({ - "schema_version": "epiphany.candidate_interventions.v0", - "updatedAt": now_iso(), - "items": items, - }) + HeartbeatCandidateInterventions { + schema_version: "epiphany.candidate_interventions.v0".to_string(), + updated_at: now_iso(), + items, + } } fn build_agent_appraisals( @@ -2398,29 +2411,21 @@ fn novelty_to_self( fn novelty_to_room( theme_id: &str, summary: &str, - previous_candidate_interventions: &Option, + previous_candidate_interventions: &Option, bridge: &Option, ) -> f64 { let mut score = 0.64_f64; for intervention in previous_candidate_interventions .as_ref() - .and_then(|value| value.get("items")) - .and_then(Value::as_array) .into_iter() - .flatten() + .flat_map(|value| value.items.iter()) .take(8) { let similarity = theme_similarity( theme_id, summary, - intervention - .get("interventionId") - .and_then(Value::as_str) - .unwrap_or(""), - intervention - .get("draft") - .and_then(Value::as_str) - .unwrap_or(""), + &intervention.intervention_id, + &intervention.draft, ); if similarity >= 0.42 { return 0.22; diff --git a/epiphany-core/src/heartbeat_state/heartbeat_documents.rs b/epiphany-core/src/heartbeat_state/heartbeat_documents.rs index 07183ead..3b4504fb 100644 --- a/epiphany-core/src/heartbeat_state/heartbeat_documents.rs +++ b/epiphany-core/src/heartbeat_state/heartbeat_documents.rs @@ -67,7 +67,7 @@ pub struct EpiphanyHeartbeatCognitionEntry { #[cultcache(key = 9, default)] pub bridge: Option, #[cultcache(key = 10, default)] - pub candidate_interventions: Option, + pub candidate_interventions: Option, #[cultcache(key = 11, default)] pub appraisals: Option, #[cultcache(key = 12, default)] @@ -126,6 +126,29 @@ pub struct HeartbeatAgentThoughtAppraisals { pub participant_appraisals: Vec, } +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct HeartbeatCandidateInterventions { + #[serde(rename = "schema_version")] + pub schema_version: String, + pub updated_at: String, + pub items: Vec, +} + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct HeartbeatCandidateIntervention { + pub intervention_id: String, + pub summary: String, + pub draft: String, + pub decision: String, + pub requires_face: bool, + pub requires_review: bool, + pub novelty_to_room: f64, + pub saturation_score: f64, + pub created_at: String, +} + #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct HeartbeatAgentThoughtAppraisal { diff --git a/epiphany-core/src/heartbeat_state/heartbeat_projection.rs b/epiphany-core/src/heartbeat_state/heartbeat_projection.rs index 612333d4..cf7cde0b 100644 --- a/epiphany-core/src/heartbeat_state/heartbeat_projection.rs +++ b/epiphany-core/src/heartbeat_state/heartbeat_projection.rs @@ -83,7 +83,7 @@ pub fn heartbeat_status_projection( "incubation": cognition.as_ref().and_then(|entry| entry.incubation.clone()), "thoughtLanes": cognition.as_ref().and_then(|entry| entry.thought_lanes.clone()), "bridge": cognition.as_ref().and_then(|entry| entry.bridge.clone()), - "candidateInterventions": cognition.as_ref().and_then(|entry| entry.candidate_interventions.clone()), + "candidateInterventions": cognition.as_ref().and_then(|entry| entry.candidate_interventions.as_ref()).and_then(typed_json), "appraisals": cognition.as_ref().and_then(|entry| entry.appraisals.as_ref()).and_then(typed_json), "reactions": cognition.as_ref().and_then(|entry| entry.reactions.as_ref()).and_then(typed_json), "adaptivePacing": adaptive_pacing, diff --git a/epiphany-core/src/heartbeat_state/heartbeat_store.rs b/epiphany-core/src/heartbeat_state/heartbeat_store.rs index 67ae8d3c..5c77203a 100644 --- a/epiphany-core/src/heartbeat_state/heartbeat_store.rs +++ b/epiphany-core/src/heartbeat_state/heartbeat_store.rs @@ -59,7 +59,8 @@ pub fn load_heartbeat_cognition_entry( ) -> Result> { let store_path = store_path.as_ref(); let cache = heartbeat_state_cache(store_path)?; - if let Some(cognition) = cache.get::(HEARTBEAT_COGNITION_KEY)? + if let Some(cognition) = + cache.get::(HEARTBEAT_COGNITION_KEY)? { return Ok(Some(cognition)); } @@ -131,7 +132,9 @@ fn legacy_heartbeat_cognition_entry( incubation: legacy.incubation, thought_lanes: legacy.thought_lanes, bridge: legacy.bridge, - candidate_interventions: legacy.candidate_interventions, + candidate_interventions: legacy + .candidate_interventions + .and_then(|value| serde_json::from_value(value).ok()), appraisals: legacy .appraisals .and_then(|value| serde_json::from_value(value).ok()), @@ -237,7 +240,10 @@ mod tests { let loaded_cognition = load_heartbeat_cognition_entry(&store_path)? .expect("heartbeat cognition should round-trip through CultCache"); - assert_eq!(loaded_cognition.schema_version, HEARTBEAT_COGNITION_SCHEMA_VERSION); + assert_eq!( + loaded_cognition.schema_version, + HEARTBEAT_COGNITION_SCHEMA_VERSION + ); assert_eq!(loaded_cognition.latest_run_id.as_deref(), Some("run-1")); Ok(()) } diff --git a/epiphany-core/src/memory_graph/profiles/heartbeat.rs b/epiphany-core/src/memory_graph/profiles/heartbeat.rs index 419413d5..b92d4727 100644 --- a/epiphany-core/src/memory_graph/profiles/heartbeat.rs +++ b/epiphany-core/src/memory_graph/profiles/heartbeat.rs @@ -170,24 +170,20 @@ fn import_candidate_interventions( let Some(candidates) = cognition.candidate_interventions.as_ref() else { return; }; - let Some(items) = candidates.get("items").and_then(Value::as_array) else { - return; - }; let domain_id = push_domain( domains, EpiphanyMemoryProfile::CandidateIntervention, "heartbeat candidate interventions", ); let before = nodes.len(); - for item in items.iter().take(8) { - let intervention_id = string_at(item, "interventionId"); + for item in candidates.items.iter().take(8) { nodes.push(EpiphanyMemoryNode { - id: memory_graph_node_id(&domain_id, "candidate", &intervention_id, None), + id: memory_graph_node_id(&domain_id, "candidate", &item.intervention_id, None), domain_id: domain_id.clone(), profile: EpiphanyMemoryProfile::CandidateIntervention, kind: EpiphanyMemoryNodeKind::CandidateIntervention, - title: string_at(item, "summary"), - claim: string_at(item, "draft"), + title: item.summary.clone(), + claim: item.draft.clone(), question: "Should this candidate intervention be spoken, deferred, or retired?" .to_string(), tension: "Candidate speech is not authority and still requires review.".to_string(), @@ -195,7 +191,7 @@ fn import_candidate_interventions( .to_string(), source_hashes: vec!["anchor:missing".to_string()], lifecycle: EpiphanyMemoryLifecycle::Queued, - salience: score_to_u32(number_at(item, "noveltyToRoom")), + salience: score_to_u32(item.novelty_to_room), confidence: 55, ..Default::default() }); @@ -372,6 +368,8 @@ fn incubation_lifecycle(status: &str) -> EpiphanyMemoryLifecycle { mod tests { use super::*; use crate::heartbeat_state::HEARTBEAT_COGNITION_SCHEMA_VERSION; + use crate::heartbeat_state::HeartbeatCandidateIntervention; + use crate::heartbeat_state::HeartbeatCandidateInterventions; use crate::memory_graph::validate_memory_graph_snapshot; #[test] @@ -416,14 +414,17 @@ mod tests { "reason": "The thought is live but not ready for speech." } })), - candidate_interventions: Some(serde_json::json!({ - "items": [{ - "interventionId": "candidate-theme-body-soul", - "summary": "Possible note", - "draft": "Evidence boundaries are lighting up.", - "noveltyToRoom": 0.7 - }] - })), + candidate_interventions: Some(HeartbeatCandidateInterventions { + schema_version: "epiphany.candidate_interventions.v0".to_string(), + updated_at: "2026-05-18T00:00:00Z".to_string(), + items: vec![HeartbeatCandidateIntervention { + intervention_id: "candidate-theme-body-soul".to_string(), + summary: "Possible note".to_string(), + draft: "Evidence boundaries are lighting up.".to_string(), + novelty_to_room: 0.7, + ..Default::default() + }], + }), appraisals: None, reactions: None, extra: Default::default(), diff --git a/notes/fresh-workspace-handoff.md b/notes/fresh-workspace-handoff.md index cd22ad06..74e94db8 100644 --- a/notes/fresh-workspace-handoff.md +++ b/notes/fresh-workspace-handoff.md @@ -68,6 +68,7 @@ remembers doctrine; the branch remembers the blade. - Heartbeat adaptive pacing now lives as typed CultCache state on `EpiphanyHeartbeatStateEntry` instead of being written into `state.extra["adaptivePacing"]` as JSON. Status projection still emits JSON and reads the old extra pocket as a legacy fallback. - Heartbeat participant scene id, personality timing, mood timing, and pending-turn cooldown multipliers now live as typed fields instead of durable `participant.extra` / `pendingTurn.extra` JSON pockets. Scheduler pacing and status projection read the typed fields first and keep legacy extra fallbacks only as sealed compatibility scars. - Heartbeat cognition appraisals and reactions now live as typed CultCache fields instead of `Option` blobs. The mood-timing scheduler consumes typed appraisals directly; legacy cognition migration parses old appraisal/reaction JSON only when loading prior stores. +- Heartbeat cognition candidate interventions now live as typed review-gated speech candidates instead of `Option` item lists. The memory graph heartbeat profile reads typed candidate items directly; legacy cognition migration parses old candidate JSON only when loading prior stores. - Heartbeat durable document/schema types now live in `epiphany-core/src/heartbeat_state/heartbeat_documents.rs`, role/default construction lives in `epiphany-core/src/heartbeat_state/heartbeat_roles.rs`, persistence/validation lives in `epiphany-core/src/heartbeat_state/heartbeat_store.rs`, and status/artifact JSON projection lives in `epiphany-core/src/heartbeat_state/heartbeat_projection.rs`. The root `heartbeat_state.rs` keeps scheduling and Void cognition orchestration for the next cuts. Focused heartbeat tests and `epiphany-core` check pass after the splits. - Heartbeat role/default and store seams now have module-local unit tests: fixed lane catalog, Ghostlight scene role projection, and typed state/cognition CultCache round-trip. Keep future heartbeat cuts aligned to those seam tests instead of growing root integration-only coverage. - Heartbeat adaptive pacing now lives in `epiphany-core/src/heartbeat_state/heartbeat_pacing.rs`, including running-turn count and effective cooldown multipliers. It has a module-local unit test for pending-turn counting; root scheduling calls the pacing seam instead of owning pressure/concurrency math directly. diff --git a/state/ledgers.msgpack b/state/ledgers.msgpack index 3d7bb639e6d947f16a8066146155f7e9f6c6fc65..175658ba2eeb24fef20897e6709835419c2744af 100644 GIT binary patch delta 239 zcmey~!P(WrIYF7p^} z^UG4F-(zADt4~hMOUX=0EJ;u KYG!3>5Cs4W16D2o delta 41 xcmeC`;r!mgIYF7p#df3WG)BhB&3hQz_b@VU-^0kX)M@$`S0+JLBP(OeC;&K|4ch