pub struct Region {
pub name: String,
pub kind: RegionKind,
pub content: Vec<RegionEntry>,
pub max_tokens: usize,
pub current_tokens: usize,
pub schema: Option<RegionSchema>,
pub taint: Option<RegionTaint>,
pub needs_message_compaction: bool,
}Expand description
A single region in the context window with its content and metadata.
Each region tracks its own token budget, current usage, and optional validation schema to enforce content format requirements.
Fields§
§name: StringUnique name identifying this region
kind: RegionKindLifecycle policy for this region
content: Vec<RegionEntry>Content entries stored in this region
max_tokens: usizeMaximum tokens allowed in this region
current_tokens: usizeCurrent token count
schema: Option<RegionSchema>Optional validation schema enforcing content format
taint: Option<RegionTaint>Taint tracking state. Present when taint tracking is enabled.
needs_message_compaction: boolWhen true, the Compact eviction strategy has determined that oldest entries should be summarized. The runtime checks this flag and performs the compaction externally (requires an LLM call).
Implementations§
Source§impl Region
impl Region
Sourcepub fn new(name: String, kind: RegionKind, max_tokens: usize) -> Self
pub fn new(name: String, kind: RegionKind, max_tokens: usize) -> Self
Create a new region with the specified configuration.
Sourcepub fn with_taint_tracking(self) -> Self
pub fn with_taint_tracking(self) -> Self
Enable taint tracking for this region.
Sourcepub fn enable_taint_tracking(&mut self)
pub fn enable_taint_tracking(&mut self)
Enable taint tracking on this region (mutable).
Sourcepub fn taint_level(&self) -> Option<TaintLevel>
pub fn taint_level(&self) -> Option<TaintLevel>
Get the current taint level of this region, if taint tracking is enabled.
Sourcepub fn add_tainted_entry(
&mut self,
content: String,
tokens: usize,
taint_level: TaintLevel,
) -> Result<()>
pub fn add_tainted_entry( &mut self, content: String, tokens: usize, taint_level: TaintLevel, ) -> Result<()>
Add an entry with a taint level. Used when taint tracking is enabled.
Sourcepub fn add_typed_tainted_entry(
&mut self,
content: String,
tokens: usize,
kind: EntryKind,
taint_level: TaintLevel,
) -> Result<()>
pub fn add_typed_tainted_entry( &mut self, content: String, tokens: usize, kind: EntryKind, taint_level: TaintLevel, ) -> Result<()>
Add a typed entry with a taint level.
Combines add_typed_entry (the entry carries a
typed EntryKind so eviction can group turns) with
add_tainted_entry (the entry contributes a
specific taint level rather than defaulting to Public). Used for tool
results when taint tracking is enabled, so a sensitive tool’s output
both keeps its ToolResult kind and raises the region’s taint level.
Sourcepub fn with_schema(self, schema: RegionSchema) -> Self
pub fn with_schema(self, schema: RegionSchema) -> Self
Add a validation schema to this region.
Sourcepub fn add_entry(&mut self, content: String, tokens: usize) -> Result<()>
pub fn add_entry(&mut self, content: String, tokens: usize) -> Result<()>
Add an entry to this region.
Validates content against schema if present, checks token budget, and adds the entry to the region.
Sourcepub fn add_entry_with_metadata(
&mut self,
content: String,
tokens: usize,
metadata: Value,
) -> Result<()>
pub fn add_entry_with_metadata( &mut self, content: String, tokens: usize, metadata: Value, ) -> Result<()>
Add an entry with metadata.
Sourcepub fn add_typed_entry(
&mut self,
content: String,
tokens: usize,
kind: EntryKind,
) -> Result<()>
pub fn add_typed_entry( &mut self, content: String, tokens: usize, kind: EntryKind, ) -> Result<()>
Sourcepub fn carry_entry(&mut self, entry: RegionEntry) -> Result<()>
pub fn carry_entry(&mut self, entry: RegionEntry) -> Result<()>
Carry an already-accepted entry into this region verbatim, preserving
its EntryKind, metadata, key, and timestamp.
Used when a stage-layout swap rebuilds a region and moves its surviving
content across: re-adding through add_entry would
stamp every carried entry EntryKind::Text, destroying the typed
tool_use/tool_result pairing the assembler needs (the orphan
sanitizer would then strip the whole history). Skips schema validation
deliberately - the entry passed it when first accepted - but keeps the
budget check and sliding-window enforcement so the destination region’s
limits still hold. Taint is not touched per entry: a carry copies the
region-level crate::taint::RegionTaint wholesale instead of
re-accumulating it.
Sourcepub fn upsert_by_key(
&mut self,
key: &str,
content: String,
tokens: usize,
) -> Result<(), String>
pub fn upsert_by_key( &mut self, key: &str, content: String, tokens: usize, ) -> Result<(), String>
Upsert an entry by key. If key exists, replace content and update timestamp/tokens. If key doesn’t exist, add new entry. Enforces max_tokens and max_entries via LRU eviction.
Sourcepub fn get_by_key(&self, key: &str) -> Option<&RegionEntry>
pub fn get_by_key(&self, key: &str) -> Option<&RegionEntry>
Get entry by key.
Sourcepub fn remove_by_key(&mut self, key: &str) -> bool
pub fn remove_by_key(&mut self, key: &str) -> bool
Remove entry by key.
Sourcepub fn remove_oldest(&mut self) -> Option<RegionEntry>
pub fn remove_oldest(&mut self) -> Option<RegionEntry>
Remove the oldest entry (for Temporary regions).
Sourcepub fn remove_entries_by_prefix(&mut self, prefix: &str)
pub fn remove_entries_by_prefix(&mut self, prefix: &str)
Remove all entries whose content starts with the given prefix.
Used to clear tagged entries (e.g. stage instructions) before injecting replacements, so stale instructions don’t accumulate across stage transitions.
Sourcepub fn entry_count(&self) -> usize
pub fn entry_count(&self) -> usize
Get the number of entries in this region.
Sourcepub fn needs_compaction(&self) -> bool
pub fn needs_compaction(&self) -> bool
Check if region needs compaction (for Compacting regions).