diff --git a/Cargo.toml b/Cargo.toml index 34940c3f8f..34cacbbd57 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -284,7 +284,7 @@ candle-transformers = { version = "0.9" } # Serialization serde = { version = "1", features = ["derive"] } -serde_json = "1" +serde_json = { version = "1", features = ["raw_value"] } # raw_value: a derived field is held as its exact text until decoded, so u128 ids never pass through Value (#5659) # Database rusqlite = { version = "0.38", features = ["bundled"] } diff --git a/core/continuum-cognition/src/cognition/context_streams/tests.rs b/core/continuum-cognition/src/cognition/context_streams/tests.rs index 4155df02a1..c64b56f6a4 100644 --- a/core/continuum-cognition/src/cognition/context_streams/tests.rs +++ b/core/continuum-cognition/src/cognition/context_streams/tests.rs @@ -67,6 +67,65 @@ fn her_view_survives_a_restart_in_her_checkpoint() { assert!(restored.take_view().is_none(), "taken once, by the act that resumes it"); } +// what this catches: the 2026-10-31 M5 outage. #5639 added `level` to the view's events, so every +// resident's checkpoint (written by the build before) failed to decode and registration refused all +// four for 32 minutes. A real view with only its `level` fields removed must be dropped (it is +// derived) while everything authoritative restores and saves again; a corrupt AUTHORITATIVE field +// must still refuse (Codex on #5659). +#[test] +fn a_view_from_an_older_build_is_dropped_and_authoritative_state_survives() { + use continuum_context::view::{Claim, View}; + use crate::cognition::working_memory::VolatileSnapshot; + + let memory = WorkingMemory::new(8); + memory.record_receipt("I ran code/list Result: 4 files"); + memory.record_settlement("listed the four files", None); + let actions = Actions { memory: &memory, persona: uuid::Uuid::nil() }; + let whole = |claims: &[Claim]| claims.iter().map(|claim| claim.full_chars).collect(); + let (view, _) = View::start(Vec::new(), &[&actions as &dyn Source<_>], whole).unwrap(); + memory.keep_view(Some(view)); + // Text, never `serde_json::Value`: the view's element ids are u128, which a Value cannot hold. + let good = serde_json::to_string(&memory.snapshot()).expect("encode"); + + // The older build's shape: the same real view, its `level` pairs removed and nothing else. + let (head, view_and_rest) = good.split_at(good.find("\"context_view\":").expect("the checkpoint carries her view")); + let mut older_view = view_and_rest.to_owned(); + let mut stripped = 0; + for variant in ["Full", "Summary", "Pointer", "Folded"] { + for pattern in [format!("\"level\":\"{variant}\","), format!(",\"level\":\"{variant}\"")] { + stripped += older_view.matches(pattern.as_str()).count(); + older_view = older_view.replace(pattern.as_str(), ""); + } + } + assert!(stripped > 0, "the fixture reproduces the missing-level view"); + let older = format!("{head}{older_view}"); + + let snap: VolatileSnapshot = serde_json::from_str(&older).expect("an older view never refuses the checkpoint"); + let restored = WorkingMemory::new(8); + restored.restore(snap); + assert!(restored.take_view().is_none(), "the stale view is dropped: her next act opens a new epoch"); + assert!(restored.has_receipt(), "her newest result survives"); + let settled = |m: &WorkingMemory| m.recent_entries().iter().any(|entry| entry.text.contains("listed the four files")); + assert!(settled(&restored), "her settlement survives"); + + // A VALID view (u128 ids and all) still restores whole: the derived decoder is lossless. + let current: VolatileSnapshot = serde_json::from_str(&good).expect("the current view decodes"); + let resumed = WorkingMemory::new(8); + resumed.restore(current); + assert!(resumed.take_view().is_some(), "a view that decodes is kept"); + + // The next save keeps every authoritative field and carries no view until she renders one. + let saved = serde_json::to_string(&restored.snapshot()).expect("encode"); + assert!(!saved.contains("\"context_view\"")); + let again = WorkingMemory::new(8); + again.restore(serde_json::from_str(&saved).expect("the repaired save decodes")); + assert!(again.has_receipt() && settled(&again), "authoritative state survives the repaired save"); + + // A corrupt authoritative field is still refused: only the derived view is forgiven. + let corrupt = good.replacen("\"next_action_seq\":", "\"next_action_seq\":\"not a number\",\"_was\":", 1); + assert!(serde_json::from_str::(&corrupt).is_err()); +} + // what this catches (Cormac on #5255): a result kept in memory but never saved under a handle // collapsing at a turnover to its one-line ledger head, which nothing can reopen (goal 7). Its // collapsed form is the whole result, so the view can only keep it, never cut it. diff --git a/core/continuum-cognition/src/cognition/working_memory.rs b/core/continuum-cognition/src/cognition/working_memory.rs index 4704878d98..ecf1588731 100644 --- a/core/continuum-cognition/src/cognition/working_memory.rs +++ b/core/continuum-cognition/src/cognition/working_memory.rs @@ -426,9 +426,9 @@ pub struct VolatileSnapshot { /// for another build: this is the first version, Joel 2026-10-31). #[serde(default, skip_serializing_if = "Vec::is_empty")] pub(crate) actions: Vec, - /// Her context view ([`WorkingMemory`]'s `view`). Absent on snapshots written before it, - /// and then her first act after the restore starts a new one. - #[serde(default, skip_serializing_if = "Option::is_none")] + /// Her context view ([`WorkingMemory`]'s `view`), derived: absent, or no longer decodable + /// (`derived_view`), her first act after the restore starts a new one. + #[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "derived_view::or_new_epoch")] pub(crate) context_view: Option, /// Each retained receipt's per-call dependencies ([`WorkingMemory`]'s `receipt_children`), /// so a restart keeps what a restored receipt points at reachable. Empty on snapshots @@ -600,6 +600,7 @@ pub struct WmEntry { } +mod derived_view; mod action_log; mod continuity; mod ledger; diff --git a/core/continuum-cognition/src/cognition/working_memory/derived_view.rs b/core/continuum-cognition/src/cognition/working_memory/derived_view.rs new file mode 100644 index 0000000000..ccc9145480 --- /dev/null +++ b/core/continuum-cognition/src/cognition/working_memory/derived_view.rs @@ -0,0 +1,33 @@ +//! A checkpoint's context view is DERIVED: her view is re-rendered at a new epoch from her working +//! memory, so it is a cache of what she last saw, never the record of it. A view that no longer +//! decodes (its shape changed in a later build) is dropped like an absent one, with a probe, and her +//! next act opens a new epoch. It never fails the checkpoint it rides in: on 2026-10-31 a new field on +//! the view's events (#5639) made every resident's checkpoint undecodable and the M5 hosted no one +//! for 32 minutes ("workspace registration failed: missing field `level`"). Nothing old is read here; +//! the stale view is discarded. + +use serde::{Deserialize, Deserializer}; +use serde_json::value::RawValue; + +use super::ContextView; + +/// `context_view`'s deserializer: the view when it decodes, else none (and a probe saying why). +/// The view is read as its exact source text first (`RawValue`), never as a `serde_json::Value`: +/// a view's element ids are u128, which a `Value` cannot hold ("number out of range"), so a +/// `Value` intermediate would refuse every valid view (Codex on #5659, executed test). +pub(crate) fn or_new_epoch<'de, D: Deserializer<'de>>(deserializer: D) -> Result, D::Error> { + let Some(raw) = Option::>::deserialize(deserializer)? else { + return Ok(None); + }; + match serde_json::from_str::(raw.get()) { + Ok(view) => Ok(Some(view)), + Err(error) => { + continuum_base::probe!( + class = "persona.checkpoint.view_dropped", + error = %error, + "a checkpoint's context view no longer decodes (its shape changed); it is derived, so it is dropped and her next act opens a new epoch" + ); + Ok(None) + } + } +} diff --git a/core/continuum-cognition/src/cognition/working_memory/tests/trail.rs b/core/continuum-cognition/src/cognition/working_memory/tests/trail.rs index 470361f018..3f20024dc9 100644 --- a/core/continuum-cognition/src/cognition/working_memory/tests/trail.rs +++ b/core/continuum-cognition/src/cognition/working_memory/tests/trail.rs @@ -289,3 +289,4 @@ fn volatile_snapshot_round_trips_and_renders_the_interruption() { "no fabricated save age on legacy snapshots: {q:?}" ); } +