pub struct Context { /* private fields */ }Expand description
The entire context window state. Fully serializable for persistence.
Implementations§
Source§impl Context
impl Context
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty context with the default SystemLayout.
Sourcepub fn with_layout(layout: SystemLayout) -> Self
pub fn with_layout(layout: SystemLayout) -> Self
Creates a new empty context whose system blocks follow layout.
Sourcepub fn set_layout(&mut self, layout: SystemLayout)
pub fn set_layout(&mut self, layout: SystemLayout)
Lays the system blocks out in layout’s order.
§Panics
Panics if a block already present has no slot in layout.
Sourcepub const fn instructions(&self) -> &SystemRegion<Instruction>
pub const fn instructions(&self) -> &SystemRegion<Instruction>
The instruction blocks.
Sourcepub const fn instructions_mut(&mut self) -> &mut SystemRegion<Instruction>
pub const fn instructions_mut(&mut self) -> &mut SystemRegion<Instruction>
The instruction blocks, for change.
Inserting or replacing a block rewrites the cacheable prefix
immediately; content that changes mid-session goes through
update_live instead.
Sourcepub const fn external(&self) -> &SystemRegion<External>
pub const fn external(&self) -> &SystemRegion<External>
The external-content blocks.
Sourcepub const fn external_mut(&mut self) -> &mut SystemRegion<External>
pub const fn external_mut(&mut self) -> &mut SystemRegion<External>
The external-content blocks, for change.
Inserting or replacing a block rewrites the cacheable prefix
immediately; content that changes mid-session goes through
update_live instead.
Sourcepub fn update_live(
&mut self,
content: &LiveContent,
update: impl FnOnce(&mut LiveUpdate<'_>),
) -> bool
pub fn update_live( &mut self, content: &LiveContent, update: impl FnOnce(&mut LiveUpdate<'_>), ) -> bool
Brings live content — system blocks that change while the session
runs, such as a repository’s AGENTS.md or the command table — to
the versions update gives, without rewriting a prefix a provider
may have cached.
Before any request has carried the prefix, the blocks are written
into it. After, the prefix is left byte-identical: the changes are
staged for the next reassembly and announced in one durable alert
naming content, carrying the diff from what the model knows (the
prefix with every earlier announced change applied) to the new
versions. The next reassembly — a compaction, or a cache gone cold —
folds the clean versions in and removes every such alert, so nothing
restates a change. An update that leaves the model’s view as it is
announces nothing, so a host may call this every turn.
Returns whether an alert was appended.
§Panics
Panics if update names a slot the layout does not have.
Sourcepub const fn live_source_blocks(&self) -> &SystemRegion<Sourced>
pub const fn live_source_blocks(&self) -> &SystemRegion<Sourced>
The blocks live sources rendered, as the prefix holds them.
Sourcepub fn system_block_count(&self) -> usize
pub fn system_block_count(&self) -> usize
Returns the number of persistent system blocks, live source blocks included.
Sourcepub fn system_prefix(&self) -> Option<String>
pub fn system_prefix(&self) -> Option<String>
Renders the cacheable system prefix: the instruction blocks, then the
external-content notice and blocks. None when there is no block.
Sourcepub fn stable_prefix_fingerprint(&self) -> u64
pub fn stable_prefix_fingerprint(&self) -> u64
Returns a stable fingerprint of the rendered system prefix and the live source blocks after it.
The fingerprint changes if and only if those bytes change. Hosts can use this to detect cache-invalidating modifications to the stable prefix (for example, to track KV-cache hit rate over time).
Sourcepub fn has_staged_system_updates(&self) -> bool
pub fn has_staged_system_updates(&self) -> bool
Whether any system block or live source block rewrite is waiting for reassembly.
Sourcepub fn set_handoff(&mut self, handoff: Handoff)
pub fn set_handoff(&mut self, handoff: Handoff)
Sets or replaces the compaction handoff.
Sourcepub fn clear_handoff(&mut self)
pub fn clear_handoff(&mut self)
Clears the compaction handoff.
Sourcepub const fn handoff(&self) -> Option<&Handoff>
pub const fn handoff(&self) -> Option<&Handoff>
Returns the current compaction handoff document.
Sourcepub fn handoff_file_index(&self) -> &[String]
pub fn handoff_file_index(&self) -> &[String]
The file index of the current handoff, empty when there is none.
Sourcepub fn push(&mut self, message: Message) -> u64
pub fn push(&mut self, message: Message) -> u64
Appends a message to the window and returns its sequence number.
Sourcepub fn push_kept(&mut self, message: Message) -> u64
pub fn push_kept(&mut self, message: Message) -> u64
Appends a tool result that closes an exchange kept verbatim through compaction, and returns its sequence number.
Sourcepub fn extend(&mut self, messages: impl IntoIterator<Item = Message>)
pub fn extend(&mut self, messages: impl IntoIterator<Item = Message>)
Appends several messages to the window.
Sourcepub fn push_alert<T: Template>(&mut self, value: &T) -> u64
pub fn push_alert<T: Template>(&mut self, value: &T) -> u64
Appends a durable alert, rendered from its typed template as a system message.
The alert enters the conversation and stays there until reassembly or
compaction prunes it — the channel for content that must reach the
next request rather than being re-rendered by a
ContextSource every time.
§Panics
Panics if the template fails to render.
Sourcepub fn push_block_alert(&mut self, block: &Block) -> u64
pub fn push_block_alert(&mut self, block: &Block) -> u64
Appends a durable alert rendered by a context source.
Sourcepub const fn fork_point(&self) -> u64
pub const fn fork_point(&self) -> u64
Returns the sequence number the next item will receive.
This is the fork point handed to a background compaction: every item numbered at or above it arrived after the fork and must survive the handoff that replaces the rest.
Sourcepub fn attachment_catalog(&self, fork_point: u64) -> Vec<AttachmentCatalogEntry>
pub fn attachment_catalog(&self, fork_point: u64) -> Vec<AttachmentCatalogEntry>
The catalog of attachments the user messages below fork_point
carry, in conversation order.
Offered to the compaction model so its handoff can name the
attachments the next turns still need;
apply_handoff resolves the names back to
the reference each entry carries.
Sourcepub fn items_since(&self, seq: u64) -> Vec<Message>
pub fn items_since(&self, seq: u64) -> Vec<Message>
Returns the items added at or after seq.
Sourcepub const fn len_recent(&self) -> usize
pub const fn len_recent(&self) -> usize
Returns number of conversation messages.
Sourcepub fn len_below(&self, fork_point: u64) -> usize
pub fn len_below(&self, fork_point: u64) -> usize
The number of conversation messages older than fork_point: those a
handoff forked there replaces.
Sourcepub fn count_recent_system_messages(&self) -> usize
pub fn count_recent_system_messages(&self) -> usize
Returns the number of system messages in the conversation.
Sourcepub fn configured_effort(&self) -> Option<ReasoningEffort>
pub fn configured_effort(&self) -> Option<ReasoningEffort>
The reasoning effort the window’s last configuration update set, if it holds one.
Sourcepub const fn is_conversation_empty(&self) -> bool
pub const fn is_conversation_empty(&self) -> bool
Returns whether the conversation is empty.
Sourcepub fn conversation_messages(&self) -> Vec<Message>
pub fn conversation_messages(&self) -> Vec<Message>
Returns the conversation messages, cloned.
Sourcepub fn drain_oldest(&mut self, keep: usize) -> Vec<Message>
pub fn drain_oldest(&mut self, keep: usize) -> Vec<Message>
Drains the oldest messages while keeping the latest keep of them.
Sourcepub fn build_request(&self, tail: &[Block]) -> Vec<Message>
pub fn build_request(&self, tail: &[Block]) -> Vec<Message>
Builds the request message list, with tail appended after the
conversation.
Layout:
- persistent system blocks, as one cacheable system message,
- each live source block, as a system message of its own,
- the compaction handoff, if any,
- the append-only conversation,
- one system message holding every live tail block.
Steps 1-4 are byte-identical to the previous request whenever nothing was staged or compacted, which is exactly the condition a provider prefix cache needs.
Sourcepub fn build_messages(&self) -> Vec<Message>
pub fn build_messages(&self) -> Vec<Message>
Builds the request message list with no live tail.
Used for measurement and for the compaction fork, which must reproduce the prefix the main request already paid to cache.
Sourcepub fn stable_chars(&self) -> usize
pub fn stable_chars(&self) -> usize
The number of characters the cacheable part of the window renders to.
Sourcepub fn conversation_chars(&self) -> usize
pub fn conversation_chars(&self) -> usize
The number of characters the conversation renders to.
Sourcepub fn clear_recent(&mut self)
pub fn clear_recent(&mut self)
Clears only the conversation, keeping system blocks and the handoff.
Sourcepub fn clear_history(&mut self)
pub fn clear_history(&mut self)
Clears all non-persistent context: the handoff and the 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 the handoff and conversation from a checkpoint.
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.
Sourcepub fn apply_handoff(
&mut self,
handoff: Handoff,
fork_point: u64,
user_message_chars: usize,
kept_attachments: &HashSet<AttachmentRef>,
)
pub fn apply_handoff( &mut self, handoff: Handoff, fork_point: u64, user_message_chars: usize, kept_attachments: &HashSet<AttachmentRef>, )
Replaces the conversation with a compaction result.
handoff becomes the new anchor and only the items numbered at or
above fork_point are kept: they arrived while the handoff was being
written and are not described by it.
An alert below the fork that no request has carried yet stays too: the model has not seen it, whatever the handoff says of it.
The user’s own words outlive the handoff beside it, verbatim and in
order: every ask_user exchange from below the fork, whatever its
size, and the newest user messages that fit user_message_chars
characters. An exchange is the question and its answer together, so it
is kept or dropped whole, and it is dropped never.
The last configuration update below the fork is kept too, in its place, so the effort it set stays in force: the handoff restates it.
A kept user message carries only the attachments kept_attachments
names, resolved from the catalog the compaction’s instruction
rendered; a message whose attachment is named is kept even when it
falls outside user_message_chars, and every unnamed attachment is
gone with the conversation the handoff replaced.
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,
- removes durable system alerts,
- tombstones tool results superseded by a later identical call or an identical later result.
Reassembly invalidates the cacheable prefix, so it runs as part of applying a compaction, which rewrites the prefix anyway.
Sourcepub fn reassemble_below(&mut self, fork_point: u64) -> ReassemblyReport
pub fn reassemble_below(&mut self, fork_point: u64) -> ReassemblyReport
reassemble that removes only the durable system
alerts numbered below fork_point, and every live-content and state
alert.
Applying a compaction handoff uses it: alerts at or after the fork arrived while the handoff was written, are not described by it and must reach the model. A live-content alert is the exception: the change it announced is folded into the prefix here; so is a state alert, whose source restates the current value after it. An alert no request has carried yet stays wherever it is, so an event is dropped only once it was delivered.
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_system_messages_below(&mut self, fork_point: u64) -> usize
pub fn prune_system_messages_below(&mut self, fork_point: u64) -> usize
Removes the delivered durable system alerts numbered below
fork_point and returns how many were pruned. An alert no request
has carried yet stays, as it does in
reassemble_below.