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
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions core/continuum-cognition/src/cognition/context_streams/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ use crate::cognition::working_memory::WorkingMemory;

pub(crate) struct Actions<'a> {
pub memory: &'a WorkingMemory,
/// Whose spill directory a stored whole is read from (`load`).
pub persona: uuid::Uuid,
}

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) }
Item { key: key.to_owned(), full: message.clone(), summary: None, pointer: Some(message), stored: None }
}
}

Expand Down Expand Up @@ -64,7 +66,8 @@ impl Source<ContextMessage> for Actions<'_> {
return vec![banner, Self::standing("none", none)];
}
let mut items = vec![banner];
items.extend(acts.into_iter().map(|(seq, full, summary, pointer)| Item {
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),
Expand All @@ -73,6 +76,19 @@ impl Source<ContextMessage> for Actions<'_> {
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 label = self.memory.action_whole_label(seq)?;
let path = crate::cognition::tool_executor::spill::resolve(self.persona, &stored.handle).ok()?;
let body = std::fs::read_to_string(path).ok()?;
// the record keeps its result trimmed (`record_receipt_inner`); loaded back, it reads the same
Some(ContextMessage::user(format!("{label}{}", body.trim())))
}

/// Once she has acted, the void said earlier in this epoch is superseded, never edited:
/// her view reads as the timeline it is (the statement was true when it was made).
fn retired(&self) -> Vec<Item<ContextMessage>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,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) }];
return vec![Item { key: "block".to_owned(), full, summary: None, pointer: Some(pointer), stored: None }];
}
let whole = self.at(|unit| &unit.content);
let summary = self.units.iter().any(|unit| unit.summary.is_some()).then(|| {
Expand All @@ -69,7 +69,8 @@ impl Source<ContextMessage> for Grounding {
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
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,
}]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl Source<ContextMessage> for Conversation {
// 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,
})
.collect()
}
Expand Down
4 changes: 2 additions & 2 deletions core/continuum-cognition/src/cognition/context_streams/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ pub(crate) struct MemoryStreams<'a> {
}

impl<'a> MemoryStreams<'a> {
pub fn of(memory: &'a WorkingMemory) -> Self {
pub fn of(memory: &'a WorkingMemory, persona: uuid::Uuid) -> Self {
Self {
actions: actions::Actions { memory },
actions: actions::Actions { memory, persona },
thoughts: thoughts::Thoughts { memory },
open_views: open_views::OpenViews { memory },
notices: notices::Notices { memory },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(crate) struct Notices<'a> {
/// 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) }
Item { key, full: message.clone(), summary: None, pointer: Some(message), stored: None }
}

impl Source<ContextMessage> for Notices<'_> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(crate) struct OpenViews<'a> {
/// turn (Benchy, the M5, 2026-10-17), so collapsed it is still itself.
fn required(key: String, text: String) -> Item<ContextMessage> {
let message = ContextMessage::user(text);
Item { key, full: message.clone(), summary: None, pointer: Some(message) }
Item { key, full: message.clone(), summary: None, pointer: Some(message), stored: None }
}

impl Source<ContextMessage> for OpenViews<'_> {
Expand Down
37 changes: 34 additions & 3 deletions core/continuum-cognition/src/cognition/context_streams/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::notices::Notices;
use crate::cognition::working_memory::WorkingMemory;

fn rendered(memory: &WorkingMemory) -> String {
Actions { memory }.items().iter().map(|item| item.full.text.clone()).collect::<Vec<_>>().join("\n")
Actions { memory, persona: uuid::Uuid::nil() }.items().iter().map(|item| item.full.text.clone()).collect::<Vec<_>>().join("\n")
}

// what this catches: her action stream going silent or lying about her acts, the three
Expand Down Expand Up @@ -53,7 +53,7 @@ fn her_view_survives_a_restart_in_her_checkpoint() {
let memory = WorkingMemory::new(8);
memory.record_receipt("I ran code/list Result: 4 files");
memory.record_settlement("listed", None);
let actions = Actions { memory: &memory };
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();
let rendered = |view: &View<super::ContextMessage>| view.render().map(|m| m.text.clone()).collect::<Vec<_>>();
Expand All @@ -76,11 +76,42 @@ fn an_unsaved_result_never_collapses_to_a_line_nothing_reopens() {
memory.record_receipt("I ran code/shell(cat Cargo.toml) Result: [package] name = \"x\"");
memory.record_settlement("read the manifest", None);
let (acts, _) = memory.action_renderings();
let (_, full, _, pointer) = acts.last().expect("the act is on record");
let (_, full, _, pointer, stored) = acts.last().expect("the act is on record");
assert!(stored.is_none(), "an unsaved result is resident: nothing to load");
assert!(full.contains("name = \"x\""), "{full}");
assert_eq!(pointer, full, "an unsaved result's pointer is the result itself");
}

// what this catches (FOCUS-CONTRACT rule 7, FLOW §2d): RAM storage deciding what she sees. A
// result's body evicted from RAM for its handle left only its pointer in her view, so the RAM
// bound was a visibility bound. Evicted, it is priced at its whole (`stored`) and loaded back
// whole when the allocation grants it, labelled exactly as it read resident.
#[test]
fn an_evicted_result_is_still_seen_whole_through_its_stored_level() {
let persona = uuid::Uuid::new_v4();
let memory = WorkingMemory::new(8);
let body = "a line of the evicted result\n".repeat(400);
let pin = crate::cognition::tool_executor::spill::LivePin::new();
memory.record_receipt_typed(&[], &body, None, pin.retain(persona, &body));
memory.record_receipt_typed(&[], "the newest stays resident", None, pin.retain(persona, "the newest stays resident"));
let (before, _) = memory.action_renderings();
let resident_whole = before.first().map(|act| act.1.clone()).expect("test: the first act");
memory.evict_resident_results_over(1);
let (acts, _) = memory.action_renderings();
let (seq, full, _, pointer, stored) = acts.first().cloned().expect("test: the first act");
let (chars, _) = stored.expect("evicted to its handle and priced");
assert_eq!(full, pointer, "what stays resident is its pointer");
assert_eq!(chars, resident_whole.chars().count(), "priced at exactly the whole it rendered");
let source = super::actions::Actions { memory: &memory, persona };
let item = continuum_context::source::Source::items(&source)
.into_iter()
.find(|item| item.key == seq.to_string())
.expect("test: the act's item");
assert_eq!(continuum_context::source::whole_chars(&item), chars);
let whole = continuum_context::source::whole(&source, item);
assert_eq!(whole.text, resident_whole, "loaded back whole, exactly as it read resident");
}

// what this catches: her substrate's notices re-rendered as new text on every act. They were one
// block after her view, ~3.6k chars re-prefilled per act though almost none of it changed
// (Benchy and Aiko on the M5, 2026-10-24). As items, an act with the same notices appends
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Source<ContextMessage> for Thoughts<'_> {
let banner = ContextMessage::user(
"My own recent thoughts (working memory, my interior state, not a message from anyone):".to_owned(),
);
let mut items = vec![Item { key: "banner".to_owned(), full: banner.clone(), summary: None, pointer: Some(banner) }];
let mut items = vec![Item { key: "banner".to_owned(), full: banner.clone(), summary: None, pointer: Some(banner), stored: None }];
items.extend(thoughts.into_iter().map(|text| {
// Keyed by what it says: a thought, once recorded, does not change, and neither may
// its rendering. Full is the whole thought; how much of her thoughts she reads is the
Expand All @@ -52,12 +52,13 @@ impl Source<ContextMessage> for Thoughts<'_> {
full: ContextMessage::user(format!("- {text}")),
summary: None,
pointer: Some(ContextMessage::user(format!("- {}", collapse_thought(&text, POINTER_KEEP)))),
stored: None,
}
}));
// Fresh each event, newest last; one item, so a status change appends one line set.
items.extend(dispatched.map(|text| {
let message = ContextMessage::user(text);
Item { key: "dispatched".to_owned(), full: message.clone(), summary: None, pointer: Some(message) }
Item { key: "dispatched".to_owned(), full: message.clone(), summary: None, pointer: Some(message), stored: None }
}));
items
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,6 @@ impl LlmDeliberationFaculty {
/// so an act that really needs more room earns it back within a turn.
const COMPLETION_COLD_PRIOR_TOKENS: u32 = crate::inference::planning::MIN_SERVE_CTX;

/// The action result's share of the window before its tail is DEMOTED to ordinary
/// history. Same denominator as the room payload for the same reason: no single
/// required item may claim more than a quarter of the window, because the moment one
/// does, every other required item is competing for what is left and the turn refuses.
const RESULT_SHARE_DENOM: u32 = 4;

/// The most an ACT turn may generate — a RUNAWAY bound, not a budget.
/// Measured acts that COMMITTED a tool call ran 4,188–9,818 tokens on the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,21 @@ impl LlmDeliberationFaculty {
/// weighs her reply also decides, once, how much of her context she reads.
pub(in super::super) fn view_claims(&self, streams: &Streams) -> Vec<crate::cognition::rag_budget::RagSourceBudget> {
let memory = self.working_memory.as_deref();
let memory_streams = memory.map(MemoryStreams::of);
let memory_streams = memory.map(|memory| MemoryStreams::of(memory, self.persona_id));
Self::view_sources(streams, memory_streams.as_ref())
.into_iter()
.map(|source| {
let items = source.items();
let cost = |message: &ContextMessage| Self::message_cost(&message.to_chat());
let floor: usize = items.iter().map(|item| item.pointer.as_ref().map_or(0, cost)).sum(); // map_or: an item that leaves when collapsed costs nothing
let want: usize = items.iter().map(|item| cost(&item.full)).sum();
// An item's WHOLE level, stored or resident (`whole_chars`): a body evicted from RAM
// is priced from its measured size, never as the pointer resident in its place,
// or the allocation would never grant room to see it (FOCUS-CONTRACT rule 7).
let whole = |item: &continuum_context::source::Item<ContextMessage>| {
let extra = continuum_context::source::whole_chars(item).saturating_sub(item.full.text.chars().count());
cost(&item.full) + extra / continuum_base::token_estimate::GUARD_CHARS_PER_TOKEN
};
let want: usize = items.iter().map(whole).sum();
crate::cognition::rag_budget::RagSourceBudget {
source_id: format!("{VIEW_CLAIM}{}", source.name()),
priority: source.priority(),
Expand Down Expand Up @@ -192,7 +199,7 @@ impl LlmDeliberationFaculty {
/// caller to size).
fn advance_view(&self, streams: &Streams) -> (Vec<ChatMessage>, bool) {
let memory = self.working_memory.as_deref();
let memory_streams = memory.map(MemoryStreams::of);
let memory_streams = memory.map(|memory| MemoryStreams::of(memory, self.persona_id));
let sources = Self::view_sources(streams, memory_streams.as_ref());
let mut held = self.context_view.lock();
// A restart resumes the view her checkpoint kept, the same bytes the engine's saved
Expand Down Expand Up @@ -242,7 +249,7 @@ impl LlmDeliberationFaculty {
/// the caller re-checks the result in tokens). `None` when there is no unsent act to redo.
fn admit_view(&self, streams: &Streams, view_tokens: usize, budget_tokens: usize) -> Option<Vec<ChatMessage>> {
let memory = self.working_memory.as_deref();
let memory_streams = memory.map(MemoryStreams::of);
let memory_streams = memory.map(|memory| MemoryStreams::of(memory, self.persona_id));
let sources = Self::view_sources(streams, memory_streams.as_ref());
let mut held = self.context_view.lock();
let view = held.as_mut()?;
Expand Down Expand Up @@ -271,7 +278,7 @@ impl LlmDeliberationFaculty {
/// Whether her view, started now under `allocate`'s grants, would show her more than it holds.
fn view_would_restore(&self, streams: &Streams, allocate: impl FnOnce(&[Claim]) -> Vec<usize>) -> Result<bool, continuum_context::view::GrantMismatch> {
let memory = self.working_memory.as_deref();
let memory_streams = memory.map(MemoryStreams::of);
let memory_streams = memory.map(|memory| MemoryStreams::of(memory, self.persona_id));
let sources = Self::view_sources(streams, memory_streams.as_ref());
self.context_view.lock().as_ref().map_or(Ok(false), |view| view.start_would_restore(&sources, allocate)) // map_or: no view yet holds nothing to restore
}
Expand All @@ -283,7 +290,7 @@ impl LlmDeliberationFaculty {
allocate: impl FnOnce(&[Claim]) -> Vec<usize>,
) -> Result<Vec<ChatMessage>, continuum_context::view::GrantMismatch> {
let memory = self.working_memory.as_deref();
let memory_streams = memory.map(MemoryStreams::of);
let memory_streams = memory.map(|memory| MemoryStreams::of(memory, self.persona_id));
let mut sources = Self::view_sources(streams, memory_streams.as_ref());
sources.retain(|source| !shed.contains(&source.name()));
let mut held = self.context_view.lock();
Expand Down Expand Up @@ -336,7 +343,7 @@ impl LlmDeliberationFaculty {
#[cfg(any(test, feature = "test-fixtures"))]
pub(in super::super) fn whole_view(&self, streams: &Streams) -> Vec<ChatMessage> {
let memory = self.working_memory.as_deref();
let memory_streams = memory.map(MemoryStreams::of);
let memory_streams = memory.map(|memory| MemoryStreams::of(memory, self.persona_id));
let sources = Self::view_sources(streams, memory_streams.as_ref());
let whole = |claims: &[Claim]| claims.iter().map(|claim| claim.full_chars).collect();
match View::start(Vec::new(), &sources, whole) {
Expand Down
Loading
Loading