diff --git a/epiphany-core/src/heartbeat_state.rs b/epiphany-core/src/heartbeat_state.rs index 9474873c..9155a8a8 100644 --- a/epiphany-core/src/heartbeat_state.rs +++ b/epiphany-core/src/heartbeat_state.rs @@ -2882,20 +2882,21 @@ fn overlap_ratio_strings(left: &[String], right: &[String]) -> f64 { if union <= 0.0 { 0.0 } else { shared / union } } -fn update_sleep_cycle(previous: Option<&Value>, incubation: &Value, allow_dream: bool) -> Value { +fn update_sleep_cycle( + previous: Option<&HeartbeatSleepCycle>, + incubation: &Value, + allow_dream: bool, +) -> HeartbeatSleepCycle { let cycle_hours = previous - .and_then(|value| value.get("cycleHours")) - .and_then(Value::as_i64) + .map(|value| value.cycle_hours) .unwrap_or(4) .clamp(2, 12); let nap_duration_minutes = previous - .and_then(|value| value.get("napDurationMinutes")) - .and_then(Value::as_i64) + .map(|value| value.nap_duration_minutes) .unwrap_or(60) .clamp(15, cycle_hours * 60 - 5); let phase_offset_minutes = previous - .and_then(|value| value.get("phaseOffsetMinutesLocal")) - .and_then(Value::as_i64) + .map(|value| value.phase_offset_minutes_local) .unwrap_or(120) .clamp(0, cycle_hours * 60 - 1); let now = chrono::Utc::now(); @@ -2924,8 +2925,7 @@ fn update_sleep_cycle(previous: Option<&Value>, incubation: &Value, allow_dream: .map(str::to_string) .collect::>(); let previous_dream_count = previous - .and_then(|value| value.get("dreamCountInCurrentNap")) - .and_then(Value::as_u64) + .map(|value| value.dream_count_in_current_nap) .unwrap_or(0); let dream_count = if is_napping && allow_dream { previous_dream_count.saturating_add(1) @@ -2934,26 +2934,39 @@ fn update_sleep_cycle(previous: Option<&Value>, incubation: &Value, allow_dream: } else { 0 }; - serde_json::json!({ - "schema_version": "epiphany.sleep_cycle.v0", - "enabled": true, - "cycleHours": cycle_hours, - "napDurationMinutes": nap_duration_minutes, - "phaseOffsetMinutesLocal": phase_offset_minutes, - "replyMode": "sleep_rumination", - "isNapping": is_napping, - "currentNapStartedAt": if is_napping { Some(nap_start.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)) } else { None }, - "currentNapEndsAt": if is_napping { Some(nap_end.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)) } else { None }, - "nextNapStartsAt": next_nap_start.to_rfc3339_opts(chrono::SecondsFormat::Secs, true), - "lastDreamAt": if is_napping && allow_dream { Some(now.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)) } else { previous.and_then(|value| value.get("lastDreamAt")).cloned().and_then(|value| value.as_str().map(str::to_string)) }, - "dreamCountInCurrentNap": dream_count, - "activeDreamThemes": active_themes, - "lastDistillationSummary": if is_napping { + HeartbeatSleepCycle { + schema_version: "epiphany.sleep_cycle.v0".to_string(), + enabled: true, + cycle_hours, + nap_duration_minutes, + phase_offset_minutes_local: phase_offset_minutes, + reply_mode: "sleep_rumination".to_string(), + is_napping, + current_nap_started_at: if is_napping { + Some(nap_start.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)) + } else { + None + }, + current_nap_ends_at: if is_napping { + Some(nap_end.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)) + } else { + None + }, + next_nap_starts_at: next_nap_start.to_rfc3339_opts(chrono::SecondsFormat::Secs, true), + last_dream_at: if is_napping && allow_dream { + Some(now.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)) + } else { + previous.and_then(|value| value.last_dream_at.clone()) + }, + dream_count_in_current_nap: dream_count, + active_dream_themes: active_themes, + last_distillation_summary: if is_napping { "Sleep pass prefers memory compression, resonance cooling, and dream residue over active work." } else { "Awake pass keeps resonance/incubation fresh without speaking unless Face has a real surface reason." - }, - }) + } + .to_string(), + } } fn summary_tokens(summary: &str) -> BTreeSet { diff --git a/epiphany-core/src/heartbeat_state/heartbeat_documents.rs b/epiphany-core/src/heartbeat_state/heartbeat_documents.rs index ecf31672..071154ef 100644 --- a/epiphany-core/src/heartbeat_state/heartbeat_documents.rs +++ b/epiphany-core/src/heartbeat_state/heartbeat_documents.rs @@ -57,7 +57,7 @@ pub struct EpiphanyHeartbeatCognitionEntry { #[cultcache(key = 4, default)] pub source: Option, #[cultcache(key = 5, default)] - pub sleep_cycle: Option, + pub sleep_cycle: Option, #[cultcache(key = 6, default)] pub memory_resonance: Option, #[cultcache(key = 7, default)] @@ -146,6 +146,26 @@ pub struct HeartbeatMemoryResonance { pub pairs: Vec, } +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct HeartbeatSleepCycle { + #[serde(rename = "schema_version")] + pub schema_version: String, + pub enabled: bool, + pub cycle_hours: i64, + pub nap_duration_minutes: i64, + pub phase_offset_minutes_local: i64, + pub reply_mode: String, + pub is_napping: bool, + pub current_nap_started_at: Option, + pub current_nap_ends_at: Option, + pub next_nap_starts_at: String, + pub last_dream_at: Option, + pub dream_count_in_current_nap: u64, + pub active_dream_themes: Vec, + pub last_distillation_summary: String, +} + #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct HeartbeatMemoryResonancePair { diff --git a/epiphany-core/src/heartbeat_state/heartbeat_projection.rs b/epiphany-core/src/heartbeat_state/heartbeat_projection.rs index 6410e684..20caca62 100644 --- a/epiphany-core/src/heartbeat_state/heartbeat_projection.rs +++ b/epiphany-core/src/heartbeat_state/heartbeat_projection.rs @@ -78,7 +78,7 @@ pub fn heartbeat_status_projection( "history": history, "latestArtifacts": latest_json_artifacts(artifact_dir, artifact_limit), "cognitionQuarantine": cognition.as_ref().map(heartbeat_cognition_status_json), - "sleepCycle": cognition.as_ref().and_then(|entry| entry.sleep_cycle.clone()), + "sleepCycle": cognition.as_ref().and_then(|entry| entry.sleep_cycle.as_ref()).and_then(typed_json), "memoryResonance": cognition.as_ref().and_then(|entry| entry.memory_resonance.as_ref()).and_then(typed_json), "incubation": cognition.as_ref().and_then(|entry| entry.incubation.clone()), "thoughtLanes": cognition.as_ref().and_then(|entry| entry.thought_lanes.clone()), diff --git a/epiphany-core/src/heartbeat_state/heartbeat_store.rs b/epiphany-core/src/heartbeat_state/heartbeat_store.rs index 8d3fcddd..557ade27 100644 --- a/epiphany-core/src/heartbeat_state/heartbeat_store.rs +++ b/epiphany-core/src/heartbeat_state/heartbeat_store.rs @@ -127,7 +127,9 @@ fn legacy_heartbeat_cognition_entry( latest_run_id, latest_artifact_ref: None, source, - sleep_cycle: legacy.sleep_cycle, + sleep_cycle: legacy + .sleep_cycle + .and_then(|value| serde_json::from_value(value).ok()), memory_resonance: legacy .memory_resonance .and_then(|value| serde_json::from_value(value).ok()), diff --git a/notes/fresh-workspace-handoff.md b/notes/fresh-workspace-handoff.md index 714e868b..ff8a508d 100644 --- a/notes/fresh-workspace-handoff.md +++ b/notes/fresh-workspace-handoff.md @@ -70,6 +70,7 @@ remembers doctrine; the branch remembers the blade. - 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 cognition memory resonance now lives as typed resonance pairs instead of an `Option` blob. Incubation and memory graph import read typed pairs directly; temporary JSON projection remains only for still-untyped thought-lane/bridge builders and status/artifact output. +- Heartbeat cognition sleep cycle now lives as typed physiology state instead of an `Option` blob. Legacy sleep JSON is parsed only during old-store migration; status and routine artifacts receive JSON projections. - 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 c3107d6d..f1eb771e 100644 Binary files a/state/ledgers.msgpack and b/state/ledgers.msgpack differ diff --git a/state/map.yaml b/state/map.yaml index be26cd31..8bb40afe 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -59,6 +59,9 @@ objective: Heartbeat cognition memory resonance is a typed pair list consumed by incubation and memory-graph import; JSON resonance exists only as a local projection for still-untyped thought-lane/bridge builders and status output. + Heartbeat cognition sleep cycle is typed physiology state; old JSON sleep + blobs are parsed only during legacy-store migration and status/artifacts + receive JSON projections. constraints: - No frontier-scale training tithe.