From bcdc2f2b4d73e2d08822deaa9f9545d46aba3c75 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 18 May 2026 07:38:39 +0100 Subject: [PATCH] Store heartbeat adaptive pacing as typed state --- epiphany-core/src/heartbeat_state.rs | 61 +++++++++--------- .../heartbeat_state/heartbeat_documents.rs | 30 +++++++++ .../src/heartbeat_state/heartbeat_pacing.rs | 43 ++++++------ .../heartbeat_state/heartbeat_projection.rs | 15 ++++- .../src/heartbeat_state/heartbeat_roles.rs | 5 +- notes/fresh-workspace-handoff.md | 1 + state/ledgers.msgpack | Bin 147192 -> 147540 bytes state/map.yaml | 3 + 8 files changed, 103 insertions(+), 55 deletions(-) diff --git a/epiphany-core/src/heartbeat_state.rs b/epiphany-core/src/heartbeat_state.rs index 8b04ca3f..4825a48b 100644 --- a/epiphany-core/src/heartbeat_state.rs +++ b/epiphany-core/src/heartbeat_state.rs @@ -14,8 +14,21 @@ mod heartbeat_projection; mod heartbeat_roles; mod heartbeat_store; pub use heartbeat_documents::*; +use heartbeat_pacing::adaptive_swarm_pacing; +use heartbeat_pacing::effective_cooldown_multiplier; +use heartbeat_pacing::mood_cooldown_multiplier; +use heartbeat_pacing::personality_cooldown_multiplier; +use heartbeat_pacing::running_turn_count; pub use heartbeat_projection::heartbeat_status_projection; +use heartbeat_projection::history_event_json; +use heartbeat_projection::pending_turn_json; +use heartbeat_projection::schedule_participant_json; +use heartbeat_projection::selection_policy_json; +use heartbeat_roles::ROLE_ORDER; +use heartbeat_roles::agent_id_for_role; pub use heartbeat_roles::default_heartbeat_state; +use heartbeat_roles::default_participant; +use heartbeat_roles::display_name_for_role; pub use heartbeat_roles::ghostlight_scene_heartbeat_state; pub use heartbeat_roles::initialize_ghostlight_scene_heartbeat_store; pub use heartbeat_roles::initialize_heartbeat_store; @@ -26,19 +39,6 @@ pub use heartbeat_store::validate_heartbeat_cognition; pub use heartbeat_store::validate_heartbeat_state; pub use heartbeat_store::write_heartbeat_cognition_entry; pub use heartbeat_store::write_heartbeat_state_entry; -use heartbeat_projection::history_event_json; -use heartbeat_projection::pending_turn_json; -use heartbeat_projection::schedule_participant_json; -use heartbeat_projection::selection_policy_json; -use heartbeat_pacing::adaptive_swarm_pacing; -use heartbeat_pacing::effective_cooldown_multiplier; -use heartbeat_pacing::mood_cooldown_multiplier; -use heartbeat_pacing::personality_cooldown_multiplier; -use heartbeat_pacing::running_turn_count; -use heartbeat_roles::ROLE_ORDER; -use heartbeat_roles::agent_id_for_role; -use heartbeat_roles::default_participant; -use heartbeat_roles::display_name_for_role; pub(super) const HEARTBEAT_ARENA_MAINTENANCE: &str = "maintenance"; pub(super) const HEARTBEAT_ARENA_SCENE: &str = "scene"; @@ -73,8 +73,12 @@ pub fn run_void_routine_store( apply_personality_timing_profiles(&mut state, &appraisal_profiles); let resonance = build_memory_resonance(&memory_records); let incubation = build_incubation( - &previous_cognition.as_ref().and_then(|entry| entry.incubation.clone()), - &previous_cognition.as_ref().and_then(|entry| entry.bridge.clone()), + &previous_cognition + .as_ref() + .and_then(|entry| entry.incubation.clone()), + &previous_cognition + .as_ref() + .and_then(|entry| entry.bridge.clone()), &previous_cognition .as_ref() .and_then(|entry| entry.candidate_interventions.clone()), @@ -83,7 +87,9 @@ pub fn run_void_routine_store( ); let thought_lanes = build_thought_lanes(&resonance, &incubation, &memory_records); let bridge = build_thought_bridge( - &previous_cognition.as_ref().and_then(|entry| entry.bridge.clone()), + &previous_cognition + .as_ref() + .and_then(|entry| entry.bridge.clone()), &thought_lanes, &resonance, &incubation, @@ -231,19 +237,16 @@ pub fn pump_heartbeat_store( let pacing = adaptive_swarm_pacing(&state, &options); state.target_heartbeat_rate = pacing.effective_heartbeat_rate; - state.extra.insert( - "adaptivePacing".to_string(), - serde_json::json!({ - "schema_version": "epiphany.adaptive_heartbeat_pacing.v0", - "contract": "Swarm pressure controls both heartbeat tempo and concurrency. Relaxed systems sleep slow; urgent systems fill more lanes without re-waking unfinished turns.", - "pressure": pacing.pressure, - "effectiveHeartbeatRate": pacing.effective_heartbeat_rate, - "targetConcurrency": pacing.target_concurrency, - "runningTurns": pacing.running_turns, - "activeParticipants": pacing.active_participants, - "signals": pacing.signals, - }), - ); + state.adaptive_pacing = Some(HeartbeatAdaptivePacing { + schema_version: "epiphany.adaptive_heartbeat_pacing.v0".to_string(), + contract: "Swarm pressure controls both heartbeat tempo and concurrency. Relaxed systems sleep slow; urgent systems fill more lanes without re-waking unfinished turns.".to_string(), + pressure: pacing.pressure, + effective_heartbeat_rate: pacing.effective_heartbeat_rate, + target_concurrency: pacing.target_concurrency, + running_turns: pacing.running_turns, + active_participants: pacing.active_participants, + signals: pacing.signals.clone(), + }); let artifact_dir = artifact_dir.as_ref(); fs::create_dir_all(artifact_dir) diff --git a/epiphany-core/src/heartbeat_state/heartbeat_documents.rs b/epiphany-core/src/heartbeat_state/heartbeat_documents.rs index 4187e84a..734930f4 100644 --- a/epiphany-core/src/heartbeat_state/heartbeat_documents.rs +++ b/epiphany-core/src/heartbeat_state/heartbeat_documents.rs @@ -34,6 +34,8 @@ pub struct EpiphanyHeartbeatStateEntry { pub participants: Vec, #[cultcache(key = 6)] pub history: Vec, + #[cultcache(key = 7, default)] + pub adaptive_pacing: Option, #[cultcache(key = 15, default)] pub extra: BTreeMap, } @@ -135,6 +137,34 @@ pub struct HeartbeatPacingPolicy { pub extra: BTreeMap, } +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct HeartbeatAdaptivePacing { + #[serde(rename = "schema_version")] + pub schema_version: String, + pub contract: String, + pub pressure: f64, + pub effective_heartbeat_rate: f64, + pub target_concurrency: usize, + pub running_turns: usize, + pub active_participants: usize, + pub signals: HeartbeatAdaptivePacingSignals, +} + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct HeartbeatAdaptivePacingSignals { + pub external_urgency: f64, + pub max_anxiety: f64, + pub average_anxiety: f64, + pub max_urgency: f64, + pub max_arousal: f64, + pub max_thought_pressure: f64, + pub max_reaction_intensity: f64, + pub pending_pressure: f64, + pub contract: String, +} + #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] pub struct HeartbeatParticipant { pub agent_id: String, diff --git a/epiphany-core/src/heartbeat_state/heartbeat_pacing.rs b/epiphany-core/src/heartbeat_state/heartbeat_pacing.rs index 15f76a80..d9ac6f9c 100644 --- a/epiphany-core/src/heartbeat_state/heartbeat_pacing.rs +++ b/epiphany-core/src/heartbeat_state/heartbeat_pacing.rs @@ -1,4 +1,5 @@ use super::EpiphanyHeartbeatStateEntry; +use super::HeartbeatAdaptivePacingSignals; use super::HeartbeatParticipant; use super::HeartbeatPumpOptions; use super::round3; @@ -12,7 +13,7 @@ pub(super) struct AdaptiveSwarmPacing { pub(super) target_concurrency: usize, pub(super) running_turns: usize, pub(super) active_participants: usize, - pub(super) signals: Value, + pub(super) signals: HeartbeatAdaptivePacingSignals, } pub(super) fn adaptive_swarm_pacing( @@ -74,17 +75,17 @@ pub(super) fn adaptive_swarm_pacing( target_concurrency, running_turns, active_participants, - signals: serde_json::json!({ - "externalUrgency": round3(external_urgency), - "maxAnxiety": round3(mood_signals.max_anxiety), - "averageAnxiety": round3(mood_signals.average_anxiety), - "maxUrgency": round3(mood_signals.max_urgency), - "maxArousal": round3(mood_signals.max_arousal), - "maxThoughtPressure": round3(mood_signals.max_thought_pressure), - "maxReactionIntensity": round3(mood_signals.max_reaction_intensity), - "pendingPressure": round3(pending_pressure), - "contract": "Anxiety-like state raises tempo and concurrency; calm state lets the swarm sleep slow.", - }), + signals: HeartbeatAdaptivePacingSignals { + external_urgency: round3(external_urgency), + max_anxiety: round3(mood_signals.max_anxiety), + average_anxiety: round3(mood_signals.average_anxiety), + max_urgency: round3(mood_signals.max_urgency), + max_arousal: round3(mood_signals.max_arousal), + max_thought_pressure: round3(mood_signals.max_thought_pressure), + max_reaction_intensity: round3(mood_signals.max_reaction_intensity), + pending_pressure: round3(pending_pressure), + contract: "Anxiety-like state raises tempo and concurrency; calm state lets the swarm sleep slow.".to_string(), + }, } } @@ -177,16 +178,14 @@ mod tests { #[test] fn running_turn_count_reads_only_running_pending_turns() { let mut state = default_heartbeat_state(1.0); - state.participants[0].pending_turn = - Some(crate::heartbeat_state::HeartbeatPendingTurn { - status: "running".to_string(), - ..Default::default() - }); - state.participants[1].pending_turn = - Some(crate::heartbeat_state::HeartbeatPendingTurn { - status: "completed".to_string(), - ..Default::default() - }); + state.participants[0].pending_turn = Some(crate::heartbeat_state::HeartbeatPendingTurn { + status: "running".to_string(), + ..Default::default() + }); + state.participants[1].pending_turn = Some(crate::heartbeat_state::HeartbeatPendingTurn { + status: "completed".to_string(), + ..Default::default() + }); assert_eq!(running_turn_count(&state), 1); } diff --git a/epiphany-core/src/heartbeat_state/heartbeat_projection.rs b/epiphany-core/src/heartbeat_state/heartbeat_projection.rs index f4454ec5..58acfeac 100644 --- a/epiphany-core/src/heartbeat_state/heartbeat_projection.rs +++ b/epiphany-core/src/heartbeat_state/heartbeat_projection.rs @@ -58,6 +58,11 @@ pub fn heartbeat_status_projection( .rev() .map(history_event_json) .collect(); + let adaptive_pacing = state + .adaptive_pacing + .as_ref() + .and_then(|pacing| serde_json::to_value(pacing).ok()) + .or_else(|| state.extra.get("adaptivePacing").cloned()); Ok(serde_json::json!({ "schema_version": HEARTBEAT_STATUS_SCHEMA_VERSION, "ok": true, @@ -81,7 +86,7 @@ pub fn heartbeat_status_projection( "candidateInterventions": cognition.as_ref().and_then(|entry| entry.candidate_interventions.clone()), "appraisals": cognition.as_ref().and_then(|entry| entry.appraisals.clone()), "reactions": cognition.as_ref().and_then(|entry| entry.reactions.clone()), - "adaptivePacing": state.extra.get("adaptivePacing"), + "adaptivePacing": adaptive_pacing, "availableActions": ["init", "tick", "pump", "complete", "status", "routine"], })) } @@ -94,7 +99,10 @@ fn latest_json_artifacts(artifact_dir: impl AsRef, limit: usize) -> Vec = read_dir .filter_map(Result::ok) .map(|entry| entry.path()) - .filter(|path| path.extension().is_some_and(|extension| extension == "json")) + .filter(|path| { + path.extension() + .is_some_and(|extension| extension == "json") + }) .filter_map(|path| { let modified = path.metadata().and_then(|meta| meta.modified()).ok()?; Some((path, modified)) @@ -121,7 +129,8 @@ fn latest_json_artifacts(artifact_dir: impl AsRef, limit: usize) -> Vec Value { - if payload.get("schema_version") == Some(&Value::String(VOID_ROUTINE_SCHEMA_VERSION.to_string())) + if payload.get("schema_version") + == Some(&Value::String(VOID_ROUTINE_SCHEMA_VERSION.to_string())) { return serde_json::json!({ "runId": payload.get("runId"), diff --git a/epiphany-core/src/heartbeat_state/heartbeat_roles.rs b/epiphany-core/src/heartbeat_state/heartbeat_roles.rs index 0fec8355..e756be69 100644 --- a/epiphany-core/src/heartbeat_state/heartbeat_roles.rs +++ b/epiphany-core/src/heartbeat_state/heartbeat_roles.rs @@ -55,6 +55,7 @@ pub fn default_heartbeat_state(target_heartbeat_rate: f64) -> EpiphanyHeartbeatS .map(|role_id| default_participant(role_id)) .collect(), history: Vec::new(), + adaptive_pacing: None, extra: BTreeMap::new(), } } @@ -254,7 +255,9 @@ fn participant_constraints(role_id: &str) -> Vec<&'static str> { } "imagination" => "Synthesizes futures; must not adopt objectives.", "research" => "Scouts known work; must not turn research into procrastination.", - "modeling" => "Grows source-grounded maps and checkpoints; must not edit implementation code.", + "modeling" => { + "Grows source-grounded maps and checkpoints; must not edit implementation code." + } "implementation" => { "Touches source only with accepted guidance and verifier-readable evidence." } diff --git a/notes/fresh-workspace-handoff.md b/notes/fresh-workspace-handoff.md index cd8f3660..044c3a7e 100644 --- a/notes/fresh-workspace-handoff.md +++ b/notes/fresh-workspace-handoff.md @@ -65,6 +65,7 @@ remembers doctrine; the branch remembers the blade. - Vendored app-server no longer imports `epiphany_core` directly. Retrieval query normalization and raw route job-launch DTO construction are bridge-owned adapters; the remaining direct native render call is in Codex core's model-context prompt fragment host seam. - Coordinator automation input assembly and pre-compaction intervention text rendering now belong to `epiphany-codex-bridge`; vendored app-server keeps only host actions: submit Codex compaction, launch through bridge, steer active turns, and emit JSON-RPC notifications. - Epiphany prompt-state boundary tags now live in `epiphany-state-model` and are re-exported through `codex-protocol`; Codex compatibility may wrap the prompt fragment, but it no longer owns those marker constants. +- 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 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 46d8fc06220fd2c1b350c046086d499b3418faea..47e65ea52ff81b4a9d76bf3d7b3a43ce40b59fac 100644 GIT binary patch delta 324 zcmYL^u}T9$6h)a7Vw!w_g**_goF$1GVnk6wKvHNCwU9I~voE`2#+hMuR^7^gg>67| z{RKM(!6u(!?{C-$E+k@oFX!C*{j>NvE>BiU`%7=DPmE3OrtE&o*vFKW)^_ukO;*V( zd!+@|eE51^f3&Q88xuM|AoPGCS}(5%EDEV3FsTPKgpQRqMx7o06?+~%b+mU`KsVkY zbbH4~AhkDu;4@1hv{U8VC}M)4Bo)H7^zl4OWTa6gJCGEZG&un4%moQAjfN4CO=h@4 zZVNx^5EMlyGMKrBsMG+(D1b9^vY?0g>nU6A_Gvzo3*)HYZ=;J$;LW9i|A(9d=h}$k bXX+>zzt^kv2Cr`MTJxmZXl*oGwe9m?J&bbT delta 53 zcmcc8!1?1Z#{^|2yL}r~r!g}2H}7F=-^0kbeGem(nypyF79#^AGhG8yT|