Type heartbeat sleep cycle

This commit is contained in:
Meta Vens 2026-05-18 08:05:04 +01:00
commit 02ab94e74a
7 changed files with 68 additions and 29 deletions

View file

@ -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::<Vec<_>>();
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<String> {

View file

@ -57,7 +57,7 @@ pub struct EpiphanyHeartbeatCognitionEntry {
#[cultcache(key = 4, default)]
pub source: Option<String>,
#[cultcache(key = 5, default)]
pub sleep_cycle: Option<Value>,
pub sleep_cycle: Option<HeartbeatSleepCycle>,
#[cultcache(key = 6, default)]
pub memory_resonance: Option<HeartbeatMemoryResonance>,
#[cultcache(key = 7, default)]
@ -146,6 +146,26 @@ pub struct HeartbeatMemoryResonance {
pub pairs: Vec<HeartbeatMemoryResonancePair>,
}
#[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<String>,
pub current_nap_ends_at: Option<String>,
pub next_nap_starts_at: String,
pub last_dream_at: Option<String>,
pub dream_count_in_current_nap: u64,
pub active_dream_themes: Vec<String>,
pub last_distillation_summary: String,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HeartbeatMemoryResonancePair {

View file

@ -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()),

View file

@ -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()),

View file

@ -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<Value>` 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<Value>` 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<Value>` 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<Value>` 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.

Binary file not shown.

View file

@ -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.