pub struct Context { /* private fields */ }Expand description
The entire context window state. Fully serializable for persistence.
Implementations§
Source§impl Context
impl Context
Sourcepub fn insert_system<T: Serialize>(&mut self, value: &T)
pub fn insert_system<T: Serialize>(&mut self, value: &T)
Inserts or replaces a persistent system block.
Identity is derived from the snake_case type name of T.
Sourcepub fn insert_system_named(
&mut self,
tag: impl Into<String>,
content: impl Into<String>,
)
pub fn insert_system_named( &mut self, tag: impl Into<String>, content: impl Into<String>, )
Inserts or replaces a persistent system block with an explicit tag.
Sourcepub fn remove_system<T>(&mut self)
pub fn remove_system<T>(&mut self)
Removes a persistent system block by type.
Sourcepub fn remove_system_named(&mut self, tag: &str)
pub fn remove_system_named(&mut self, tag: &str)
Removes a persistent system block by explicit tag.
Sourcepub fn system_block_count(&self) -> usize
pub fn system_block_count(&self) -> usize
Returns the number of persistent system blocks.
Sourcepub fn has_system_block(&self, tag: &str) -> bool
pub fn has_system_block(&self, tag: &str) -> bool
Returns whether a persistent system block with tag exists.
Sourcepub const fn system_blocks(&self) -> &IndexMap<String, String>
pub const fn system_blocks(&self) -> &IndexMap<String, String>
Returns an immutable view of persistent system blocks.
Sourcepub const fn system_blocks_mut(&mut self) -> &mut IndexMap<String, String>
pub const fn system_blocks_mut(&mut self) -> &mut IndexMap<String, String>
Returns a mutable view of persistent system blocks.
Sourcepub fn insert_system_text(
&mut self,
tag: impl Into<String>,
content: impl Into<String>,
)
pub fn insert_system_text( &mut self, tag: impl Into<String>, content: impl Into<String>, )
Inserts or replaces a persistent system block with raw text.
Unlike insert_system and
insert_system_named, the content is
stored verbatim without any XML wrapping. This is the preferred entry
point for prose system blocks such as workspace descriptions, runtime
metadata, and environment hints, where XML structure adds tokens
without providing semantic value.
Use insert_system only when the block is a
structured payload that genuinely needs XML delimiters to separate
context from instructions (for example, attached documents).
Sourcepub fn stable_prefix_fingerprint(&self) -> u64
pub fn stable_prefix_fingerprint(&self) -> u64
Returns a stable fingerprint of the current system blocks in order.
The fingerprint changes if and only if the ordered set of
(tag, content) pairs changes. Hosts can use this to detect
cache-invalidating modifications to the stable system prefix
(for example, to track KV-cache hit rate over time).
Sourcepub fn insert_reminder<T: Serialize>(&mut self, value: &T)
pub fn insert_reminder<T: Serialize>(&mut self, value: &T)
Inserts an ephemeral reminder.
Reminders are transient by design: clear_reminders runs at the top of
every request’s reminder repopulation, so a reminder reaches the model
only when it is (re)generated inside that populate pass. Anything the
agent must actually deliver — mid-turn corrections, queued alerts —
belongs in push_alert, which survives
repopulation and is pruned by reassembly/compaction instead.
Sourcepub fn push_alert<T: Serialize>(&mut self, value: &T)
pub fn push_alert<T: Serialize>(&mut self, value: &T)
Pushes a durable system alert, rendered like a reminder.
The alert enters the conversation stream as a system message and
stays there until reassembly or compaction prunes system alerts — the
channel to use for content that must reach the next request rather
than being regenerated by reminder repopulation.
Sourcepub fn clear_reminders(&mut self)
pub fn clear_reminders(&mut self)
Clears all ephemeral reminders.
Sourcepub fn set_handoff(&mut self, handoff: impl Into<String>)
pub fn set_handoff(&mut self, handoff: impl Into<String>)
Sets or replaces the compaction handoff document.
Sourcepub fn set_handoff_document(&mut self, handoff: &HandoffDocument)
pub fn set_handoff_document(&mut self, handoff: &HandoffDocument)
Sets the compaction handoff document from a structured handoff artifact.
Sourcepub fn clear_handoff(&mut self)
pub fn clear_handoff(&mut self)
Clears the compaction handoff document.
Sourcepub fn extend(&mut self, messages: impl IntoIterator<Item = Message>)
pub fn extend(&mut self, messages: impl IntoIterator<Item = Message>)
Extends recent conversation with multiple messages.
Sourcepub fn len_recent(&self) -> usize
pub fn len_recent(&self) -> usize
Returns number of recent conversation messages.
Sourcepub fn count_recent_system_messages(&self) -> usize
pub fn count_recent_system_messages(&self) -> usize
Returns the number of recent system messages.
Sourcepub fn is_conversation_empty(&self) -> bool
pub fn is_conversation_empty(&self) -> bool
Returns whether recent conversation is empty.
Sourcepub fn drain_oldest(&mut self, keep: usize) -> Vec<Message>
pub fn drain_oldest(&mut self, keep: usize) -> Vec<Message>
Drains oldest recent messages while keeping the latest keep messages.
Sourcepub fn conversation_messages(&self) -> Vec<Message>
pub fn conversation_messages(&self) -> Vec<Message>
Returns recent conversation messages cloned.
Sourcepub fn build_messages(&self) -> Vec<Message>
pub fn build_messages(&self) -> Vec<Message>
Builds the full LLM message list.
Layout:
- Persistent system blocks (single cacheable prefix)
- Compaction handoff (optional system message)
- Ordered runtime items (reminders and conversation messages)
Sourcepub fn clear_recent(&mut self)
pub fn clear_recent(&mut self)
Clears only recent conversation messages.
Sourcepub fn clear_history(&mut self)
pub fn clear_history(&mut self)
Clears all non-persistent context: reminders, handoff, recent conversation.
Sourcepub fn checkpoint(&self) -> ContextCheckpoint
pub fn checkpoint(&self) -> ContextCheckpoint
Creates a checkpoint that can be restored later.
Sourcepub fn restore(&mut self, checkpoint: ContextCheckpoint)
pub fn restore(&mut self, checkpoint: ContextCheckpoint)
Restores reminders, handoff, and recent conversation from checkpoint.
Sourcepub fn stage_system_text(
&mut self,
tag: impl Into<String>,
content: impl Into<String>,
) -> bool
pub fn stage_system_text( &mut self, tag: impl Into<String>, content: impl Into<String>, ) -> bool
Stages a replacement for a system block, deferring the cache-invalidating
rewrite until the next reassemble.
If no block with tag exists yet, the content is inserted immediately
(nothing to preserve in the cache) and false is returned. Otherwise the
update is staged and true is returned; the host should surface the
change to the model as an appended diff alert.
Sourcepub fn staged_system_update(&self, tag: &str) -> Option<&str>
pub fn staged_system_update(&self, tag: &str) -> Option<&str>
Returns the staged replacement for tag, if any.
Sourcepub fn reassemble(&mut self) -> ReassemblyReport
pub fn reassemble(&mut self) -> ReassemblyReport
Performs a lightweight context reassembly.
This rewrites the window without calling any LLM:
- folds staged system block updates into the stable prefix,
- clears all ephemeral reminders,
- removes recent system messages (system alerts),
- tombstones tool results superseded by a later identical call or an identical later result.
Reassembly invalidates the KV-cache prefix, so hosts should run it when the cache is already cold (idle gap) or right before compaction.
Sourcepub fn estimate_reassembly_savings(&self) -> usize
pub fn estimate_reassembly_savings(&self) -> usize
Estimates how many bytes a reassemble would free,
without mutating the context.
Sourcepub fn prune_recent_system_messages(&mut self) -> usize
pub fn prune_recent_system_messages(&mut self) -> usize
Removes recent system messages and returns how many were pruned.
Sourcepub fn restore_runtime_state(&mut self, restored: Self)
pub fn restore_runtime_state(&mut self, restored: Self)
Restores runtime-managed state from another serialized context while preserving the current persistent system blocks.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Context
impl<'de> Deserialize<'de> for Context
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for Context
impl RefUnwindSafe for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl UnsafeUnpin for Context
impl UnwindSafe for Context
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more