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
48 changes: 29 additions & 19 deletions core/continuum-cognition/src/cognition/context_streams/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
//! shelter: Anwen parked fabricated receipts in one within minutes), or acts happened whose
//! details aged out (denying her own work is the inverse failure, 2026-07-13: Asha).

use continuum_context::source::{Item, Source};
use continuum_context::cause::{Cause, Content, Origin};
use continuum_context::source::{Item, Source, StoredWhole};

use super::{priority, ContextMessage};
use super::{element, priority, ContextMessage, Producer};
use crate::cognition::working_memory::WorkingMemory;

pub(crate) struct Actions<'a> {
Expand All @@ -19,9 +20,12 @@ pub(crate) struct Actions<'a> {
}

impl Actions<'_> {
fn standing(key: &str, text: String) -> Item<ContextMessage> {
let message = ContextMessage::user(text);
Item { key: key.to_owned(), full: message.clone(), summary: None, pointer: Some(message), stored: None }
fn standing(&self, key: &str, text: String) -> Item<ContextMessage> {
super::standing(self.persona, "actions", key, ContextMessage::user(text))
}

fn act(&self, seq: u64) -> continuum_context::cause::ElementId {
element(self.persona, Producer::Act, &seq.to_be_bytes())
}
}

Expand All @@ -35,7 +39,7 @@ impl Source<ContextMessage> for Actions<'_> {
}

fn items(&self) -> Vec<Item<ContextMessage>> {
let banner = Self::standing(
let banner = self.standing(
"banner",
"[actions] What I have done this session, oldest first: each #n is one act; a \
collapsed one names the handle that reopens it."
Expand Down Expand Up @@ -63,25 +67,31 @@ impl Source<ContextMessage> for Actions<'_> {
if taken == 1 { "" } else { "s" }
)
};
return vec![banner, Self::standing("none", none)];
return vec![banner, self.standing("none", none)];
}
let mut items = vec![banner];
items.extend(acts.into_iter().map(|(seq, full, summary, pointer, stored)| Item {
stored: stored.map(|(chars, handle)| continuum_context::source::StoredWhole { chars, handle }),
key: seq.to_string(),
full: ContextMessage::user(full),
summary: summary.map(ContextMessage::user),
pointer: Some(ContextMessage::user(pointer)),
}));
// Her acts in one room are one chain: each follows the act before it there; the first is
// where that activity's chain starts.
let mut last_in_room: std::collections::HashMap<Option<uuid::Uuid>, u64> = std::collections::HashMap::new();
for act in acts {
let cause = last_in_room.insert(act.room, act.seq).map_or(Cause::Root(Origin::ActivityStart), |prev| Cause::Of(self.act(prev)));
let stored = act.stored.map(|(chars, handle)| StoredWhole { chars, handle, content: Content::Absent });
items.push(
Item::rendered(self.act(act.seq), cause, act.seq.to_string(), ContextMessage::user(act.full), act.content)
.with_summary(act.summary.map(ContextMessage::user))
.with_pointer(ContextMessage::user(act.pointer))
.with_stored(stored),
);
}
items
}

/// A whole her working memory evicted from RAM (storage, never visibility: FOCUS-CONTRACT
/// rule 7), read back from its spill file when her view grants it whole, labelled as it
/// read resident. `None` (the resident pointer renders) when the file is gone or unreadable.
fn load(&self, item: &Item<ContextMessage>) -> Option<ContextMessage> {
let stored = item.stored.as_ref()?;
let seq: u64 = item.key.parse().ok()?;
let stored = item.stored()?;
let seq: u64 = item.key().parse().ok()?;
let label = self.memory.action_whole_label(seq)?;
let body = crate::cognition::tool_executor::spill::saved_whole(self.persona, &stored.handle).ok()?;
Some(ContextMessage::user(format!("{label}{body}")))
Expand All @@ -95,8 +105,8 @@ impl Source<ContextMessage> for Actions<'_> {
fn fold(&self, folded: &[Item<ContextMessage>]) -> Option<ContextMessage> {
// The banner stands as itself ahead of the run (the view never folds a standing head),
// so the run is her acts alone, keyed by their #n.
let first: u64 = folded.first()?.key.parse().ok()?;
let last: u64 = folded.last()?.key.parse().ok()?;
let first: u64 = folded.first()?.key().parse().ok()?;
let last: u64 = folded.last()?.key().parse().ok()?;
let count = folded.len();
Some(ContextMessage::user(format!(
"[actions #{first}-#{last}: {count} earlier acts folded; reopen one with tool/output act=<n>]"
Expand All @@ -110,6 +120,6 @@ impl Source<ContextMessage> for Actions<'_> {
if acts.is_empty() && !in_flight {
return Vec::new();
}
vec![Self::standing("none", "(that was before my first act; my acts follow)".to_owned())]
vec![self.standing("none", "(that was before my first act; my acts follow)".to_owned())]
}
}
31 changes: 17 additions & 14 deletions core/continuum-cognition/src/cognition/context_streams/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use crate::cognition::rag_budget::RagItem;
use super::{priority, ContextMessage};

pub(crate) struct Grounding {
/// Whose block this is (its id, `super::standing`).
pub persona: uuid::Uuid,
pub faculty: String,
/// The whole block.
pub text: String,
Expand All @@ -26,9 +28,15 @@ pub(crate) struct Grounding {

impl Grounding {
/// A block with no units that is shown whole or not at all (a contract in force).
pub fn atomic(faculty: impl Into<String>, text: impl Into<String>) -> Self {
pub fn atomic(persona: uuid::Uuid, faculty: impl Into<String>, text: impl Into<String>) -> Self {
let text = text.into();
Self { faculty: faculty.into(), minimum: text.clone(), text, units: std::sync::Arc::from(Vec::new()) }
Self { persona, faculty: faculty.into(), minimum: text.clone(), text, units: std::sync::Arc::from(Vec::new()) }
}

/// The block as a standing item: the root of its activity; a board update is new content, not a
/// new identity.
fn block(&self, full: ContextMessage, summary: Option<ContextMessage>, pointer: ContextMessage) -> Item<ContextMessage> {
super::standing(self.persona, &self.faculty, "block", full).with_summary(summary).with_pointer(pointer)
}

fn render(&self, body: String) -> ContextMessage {
Expand Down Expand Up @@ -56,7 +64,7 @@ impl Source<ContextMessage> for Grounding {
// reopen it by, so collapsed it is still its truthful minimum, never a one-liner.
let full = self.render(self.text.clone());
let pointer = self.render(self.minimum.clone());
return vec![Item { key: "block".to_owned(), full, summary: None, pointer: Some(pointer), stored: None }];
return vec![self.block(full, None, pointer)];
}
let whole = self.at(|unit| &unit.content);
let summary = self.units.iter().any(|unit| unit.summary.is_some()).then(|| {
Expand All @@ -65,13 +73,8 @@ impl Source<ContextMessage> for Grounding {
let pointer = self.at(|unit| unit.pointer.as_deref().or(unit.summary.as_deref()).unwrap_or(&unit.content)); // the cheapest level each unit offers
let summary = summary.filter(|summary| *summary != whole);
let pointer = (pointer != whole).then_some(pointer);
vec![Item {
key: "block".to_owned(),
full: self.render(whole),
summary: summary.map(|summary| self.render(summary)),
pointer: Some(pointer.map_or_else(|| self.render(self.minimum.clone()), |pointer| self.render(pointer))), // map_or_else: units with no coarser level keep the block's truthful minimum,
stored: None,
}]
let pointer = pointer.map_or_else(|| self.render(self.minimum.clone()), |pointer| self.render(pointer)); // map_or_else: units with no coarser level keep the block's truthful minimum
vec![self.block(self.render(whole), summary.map(|summary| self.render(summary)), pointer)]
}
}

Expand All @@ -81,7 +84,7 @@ mod tests {

fn grounding(units: Vec<RagItem>) -> Grounding {
let text = units.iter().map(|u| u.content.clone()).collect::<Vec<_>>().join("\n");
Grounding { faculty: "room-board".into(), minimum: text.clone(), text, units: units.into() }
Grounding { persona: uuid::Uuid::nil(), faculty: "room-board".into(), minimum: text.clone(), text, units: units.into() }
}

// what this catches: a source's levels never reaching her view (the block had only
Expand All @@ -96,8 +99,8 @@ mod tests {
};
let item = grounding(vec![card("a"), card("b")]).items().remove(0);
let text = |m: &ContextMessage| m.text.clone();
assert_eq!(text(&item.full), "[room-board]\ncard a: a long description of the work\ncard b: a long description of the work");
assert_eq!(item.summary.as_ref().map(text).as_deref(), Some("[room-board]\ncard a: short\ncard b: short"));
assert_eq!(item.pointer.as_ref().map(text).as_deref(), Some("[room-board]\n[card a]\n[card b]"));
assert_eq!(text(item.full()), "[room-board]\ncard a: a long description of the work\ncard b: a long description of the work");
assert_eq!(item.summary().map(text).as_deref(), Some("[room-board]\ncard a: short\ncard b: short"));
assert_eq!(item.pointer().map(text).as_deref(), Some("[room-board]\n[card a]\n[card b]"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
//! in her view until the next epoch start. Collapsed at a start, an older turn leaves the view;
//! the room's history keeps it (`chat/history` rereads it).

use continuum_context::cause::{Cause, Origin};
use continuum_context::source::{Item, Source};

use super::{priority, ContextMessage, Role};
use super::{element, priority, ContextMessage, Producer, Role};

pub(crate) struct Turn {
pub key: String,
Expand All @@ -18,6 +19,8 @@ pub(crate) struct Turn {

#[derive(Default)]
pub(crate) struct Conversation {
/// Whose conversation this is (its turns' ids, `super::element`).
pub persona: uuid::Uuid,
pub turns: Vec<Turn>,
}

Expand All @@ -31,18 +34,21 @@ impl Source<ContextMessage> for Conversation {
}

fn items(&self) -> Vec<Item<ContextMessage>> {
// A room's conversation is one chain in its order: each turn follows the one before it
// (who answered whom is not recorded yet); the first came from outside her.
let mut previous = None;
self.turns
.iter()
.map(|turn| Item {
key: turn.key.clone(),
full: ContextMessage { role: turn.role, text: turn.line.clone() },
.map(|turn| {
let id = element(self.persona, Producer::Turn, turn.key.as_bytes());
let cause = previous.replace(id).map_or(Cause::Root(Origin::External), Cause::Of);
let full = ContextMessage { role: turn.role, text: turn.line.clone() };
let pointer = turn.pointer.clone().map(|text| ContextMessage { role: turn.role, text });
// Collapsed, a turn leaves her view: the room's history holds it, and the
// `[context]` fact says how many turns the room offered. A one-line pointer would
// weigh about what a short turn does, saving nothing. The room-update index keeps
// itself, so an update too large to show is never unknown.
summary: None,
pointer: turn.pointer.clone().map(|text| ContextMessage { role: turn.role, text }),
stored: None,
Item::whole(id, cause, turn.key.clone(), full).with_pointer(pointer)
})
.collect()
}
Expand Down
37 changes: 31 additions & 6 deletions core/continuum-cognition/src/cognition/context_streams/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,32 @@ use crate::cognition::working_memory::WorkingMemory;
/// The streams her working memory feeds her view: her actions, her thoughts, her open views
/// and the notices her substrate posted. Built once per use from her memory, never assembled
/// by hand.
/// Which producer named an occurrence: part of its id, so an act and a turn never share one.
#[derive(Clone, Copy, Debug)]
pub(crate) enum Producer {
Act,
Turn,
OpenView,
Thought,
Notice,
Standing,
}

/// An occurrence's id from its producer's own stable identity: whose (`persona`), which producer,
/// and the producer's id for it (an act's `seq`, a turn's event, a view's key). Deterministic, so it
/// survives epochs and restarts exactly as long as the producer's identity does.
pub(crate) fn element(persona: uuid::Uuid, producer: Producer, local: &[u8]) -> continuum_context::cause::ElementId {
continuum_context::cause::ElementId::of(&[persona.as_bytes(), &[producer as u8], local])
}

/// A standing item of `source` (a banner, a block that leads her view): the root of its activity,
/// resident whole. Its id is namespaced by the source, so two sources' banners never share one.
pub(crate) fn standing(persona: uuid::Uuid, source: &str, key: &str, message: ContextMessage) -> continuum_context::source::Item<ContextMessage> {
use continuum_context::cause::{Cause, Origin};
let id = element(persona, Producer::Standing, format!("{source}/{key}").as_bytes());
continuum_context::source::Item::whole(id, Cause::Root(Origin::ActivityStart), key, message.clone()).with_pointer(message)
}

pub(crate) struct MemoryStreams<'a> {
pub actions: actions::Actions<'a>,
pub thoughts: thoughts::Thoughts<'a>,
Expand All @@ -33,9 +59,9 @@ impl<'a> MemoryStreams<'a> {
pub fn of(memory: &'a WorkingMemory, persona: uuid::Uuid) -> Self {
Self {
actions: actions::Actions { memory, persona },
thoughts: thoughts::Thoughts { memory },
open_views: open_views::OpenViews { memory },
notices: notices::Notices { memory },
thoughts: thoughts::Thoughts { memory, persona },
open_views: open_views::OpenViews { memory, persona },
notices: notices::Notices { memory, persona },
}
}
}
Expand Down Expand Up @@ -75,9 +101,8 @@ impl ContextMessage {
}

impl Block for ContextMessage {
fn chars(&self) -> usize {
// `str::chars`, not this trait's `chars`, which `String` also implements.
self.text.as_str().chars().count()
fn text(&self) -> &str {
&self.text
}
}

Expand Down
25 changes: 17 additions & 8 deletions core/continuum-cognition/src/cognition/context_streams/notices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@
//! M5, 2026-10-24: the largest new text per act, ahead of her own results). Keyed, an unchanged
//! notice is appended once per epoch and a changed one appends its new version.

use continuum_context::cause::{Cause, Origin};
use continuum_context::source::{Item, Source};

use super::{priority, ContextMessage};
use super::{element, priority, ContextMessage, Producer};
use crate::cognition::framing_echo::NOTICES_HEADER;
use crate::cognition::working_memory::WorkingMemory;

pub(crate) struct Notices<'a> {
pub memory: &'a WorkingMemory,
/// Whose occurrences these are (`super::element`).
pub persona: uuid::Uuid,
}

/// A notice is required input: collapsed, it is still itself, so no grant can drop it.
fn required(key: String, text: String) -> Item<ContextMessage> {
let message = ContextMessage::user(text);
Item { key, full: message.clone(), summary: None, pointer: Some(message), stored: None }
impl Notices<'_> {
/// A notice comes from her situation, outside her: an external origin.
fn notice(&self, key: String, text: String) -> Item<ContextMessage> {
let message = ContextMessage::user(text);
Item::whole(element(self.persona, Producer::Notice, key.as_bytes()), Cause::Root(Origin::External), key, message.clone()).with_pointer(message)
}
}

impl Source<ContextMessage> for Notices<'_> {
Expand All @@ -41,8 +47,8 @@ impl Source<ContextMessage> for Notices<'_> {
let banner = format!(
"{NOTICES_HEADER} (status observations about my situation — not a message from anyone, and not my own words):"
);
let mut items = vec![required("banner".to_owned(), banner)];
items.extend(notices.into_iter().map(|(key, text)| required(key, format!("- {text}"))));
let mut items = vec![super::standing(self.persona, "notices", "banner", ContextMessage::user(banner))];
items.extend(notices.into_iter().map(|(key, text)| self.notice(key, format!("- {text}"))));
items
}

Expand All @@ -53,8 +59,11 @@ impl Source<ContextMessage> for Notices<'_> {
.ended_notice_keys()
.into_iter()
.map(|key| {
let notice = format!("- [ended] the notice {key} no longer holds");
required(key, notice)
// The end of a notice follows from that notice.
let message = ContextMessage::user(format!("- [ended] the notice {key} no longer holds"));
let id = element(self.persona, Producer::Notice, format!("ended/{key}").as_bytes());
let cause = Cause::Of(element(self.persona, Producer::Notice, key.as_bytes()));
Item::whole(id, cause, key, message.clone()).with_pointer(message)
})
.collect()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
//! every act (~1.5k chars on the M5, 2026-10-24) though a view's line changes only when she
//! pages it. Keyed by subject, paging one file appends that file's line alone.

use continuum_context::cause::{Cause, Origin};
use continuum_context::source::{Item, Source};

use super::{priority, ContextMessage};
use super::{element, priority, ContextMessage, Producer};
use crate::cognition::open_views::OpenViews as Views;
use crate::cognition::working_memory::WorkingMemory;

pub(crate) struct OpenViews<'a> {
pub memory: &'a WorkingMemory,
/// Whose occurrences these are (`super::element`).
pub persona: uuid::Uuid,
}

/// Standing notices contain no allocatable detail.
fn required(key: String, text: String) -> Item<ContextMessage> {
let message = ContextMessage::user(text);
Item { key, full: message.clone(), summary: None, pointer: Some(message), stored: None }
impl OpenViews<'_> {
fn standing(&self, key: &str, text: String) -> Item<ContextMessage> {
super::standing(self.persona, "open-views", key, ContextMessage::user(text))
}
}

impl Source<ContextMessage> for OpenViews<'_> {
Expand All @@ -37,13 +41,13 @@ impl Source<ContextMessage> for OpenViews<'_> {
if lines.is_empty() {
return Vec::new();
}
let mut items = vec![required("banner".to_owned(), Views::BANNER.to_owned())];
items.extend(lines.into_iter().map(|(key, full, pointer)| Item {
key,
full: ContextMessage::user(format!("- {full}")),
summary: None,
pointer: Some(ContextMessage::user(format!("- {pointer}"))),
stored: None,
let mut items = vec![self.standing("banner", Views::BANNER.to_owned())];
// A view follows from the act that last read it.
items.extend(lines.into_iter().map(|line| {
let id = element(self.persona, Producer::OpenView, format!("{}#{}", line.key, line.touched_seq).as_bytes());
let cause = Cause::Of(element(self.persona, Producer::Act, &line.touched_seq.to_be_bytes()));
Item::whole(id, cause, line.key, ContextMessage::user(format!("- {}", line.full)))
.with_pointer(ContextMessage::user(format!("- {}", line.pointer)))
}));
items
}
Expand All @@ -55,8 +59,9 @@ impl Source<ContextMessage> for OpenViews<'_> {
.open_views_out_of_scope()
.into_iter()
.map(|key| {
let notice = format!("- [not visible from here] {key}");
required(key, notice)
let id = element(self.persona, Producer::Notice, key.as_bytes());
let message = ContextMessage::user(format!("- [not visible from here] {key}"));
Item::whole(id, Cause::Root(Origin::External), key, message.clone()).with_pointer(message)
})
.collect()
}
Expand Down
Loading
Loading