Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
59 changes: 59 additions & 0 deletions core/continuum-cognition/src/cognition/context_streams/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<VolatileSnapshot>(&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.
Expand Down
7 changes: 4 additions & 3 deletions core/continuum-cognition/src/cognition/working_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<action_log::ActionRecord>,
/// 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<ContextView>,
/// 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
Expand Down Expand Up @@ -600,6 +600,7 @@ pub struct WmEntry {
}


mod derived_view;
mod action_log;
mod continuity;
mod ledger;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Option<ContextView>, D::Error> {
let Some(raw) = Option::<Box<RawValue>>::deserialize(deserializer)? else {
return Ok(None);
};
match serde_json::from_str::<ContextView>(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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,4 @@ fn volatile_snapshot_round_trips_and_renders_the_interruption() {
"no fabricated save age on legacy snapshots: {q:?}"
);
}

Loading