Skip to main content

ContextWindow

Struct ContextWindow 

Source
pub struct ContextWindow {
    pub regions: Vec<Region>,
    pub current_tokens: usize,
    pub max_tokens: usize,
    pub region_scripts: HashMap<String, Arc<RegionScript>>,
}
Expand description

Context window component storing the agent’s memory regions.

Fields§

§regions: Vec<Region>

All regions in this context window

§current_tokens: usize

Current total token usage

§max_tokens: usize

Maximum token budget

§region_scripts: HashMap<String, Arc<RegionScript>>

Compiled custom-region scripts, keyed by the script path each RegionKind::Custom carries. Populated once at spawn by the CLI (which resolves blueprint-dir-relative paths and compile-checks the files); a stage-layout swap rebuilds regions but leaves this table untouched, so per-stage custom regions keep working. Empty when no custom regions exist - every hook lookup then misses and the region renders its fallback shape.

Implementations§

Source§

impl ContextWindow

Source

pub fn new(max_tokens: usize) -> Self

Create a new context window with the specified budget.

Source

pub fn get_region(&self, name: &str) -> Option<&Region>

Get a region by name.

Source

pub fn get_region_mut(&mut self, name: &str) -> Option<&mut Region>

Get a mutable reference to a region by name.

Source

pub fn add_region(&mut self, region: Region)

Add a region to this context window.

Source

pub fn add_to_region( &mut self, region_name: &str, content: String, tokens: usize, ) -> Result<()>

Add content to a specific region.

Source

pub fn replace_region( &mut self, region_name: &str, content: String, tokens: usize, ) -> bool

Replace a region’s entire content with a single entry (clear, then add). Returns false (no-op) if the region does not exist. Used to keep an authoritative document region (e.g. the plan) holding only its current version, so revisions build on it instead of accumulating stale copies.

Source

pub fn add_typed_entry( &mut self, region_name: &str, kind: EntryKind, content: String, tokens: usize, ) -> Result<()>

Add a typed entry to a specific region.

Like add_to_region but the entry carries an EntryKind so message roles are determined by type, not text-prefix parsing.

Source

pub fn calculate_tokens(&self) -> usize

Calculate current token usage across all regions.

Source

pub fn needs_eviction(&self, threshold: f32) -> bool

Check if the context window needs eviction.

Source

pub fn try_evict(&mut self, target_free_tokens: usize) -> Result<EvictionResult>

Execute eviction cascade to free up space.

Returns an EvictionResult with tokens freed and any regions that need LLM-based compaction. The caller is responsible for performing compaction on the listed regions (since it requires async LLM access).

Source

pub fn assemble(&self) -> AssembledContext

Result of assembling the context window into system blocks + messages.

System-bound regions become system_blocks; the messages region becomes messages with proper typed entries (no text-prefix parsing).

Thin wrapper over assemble_with_meta with no stage metadata - custom-region scripts see empty stage fields.

Source

pub fn assemble_with_meta(&self, meta: &AssembleMeta) -> AssembledContext

assemble with stage metadata for custom-region render(ctx) hooks (stage name, per-stage iteration count, model). The inference path (build_request) threads real values; other callers use the default.

Source

pub fn enable_taint_tracking(&mut self)

Enable taint tracking on all regions in this context window.

Source

pub fn add_tainted_to_region( &mut self, region_name: &str, content: String, tokens: usize, taint_level: TaintLevel, ) -> Result<()>

Add tainted content to a specific region.

Source

pub fn add_typed_tainted_to_region( &mut self, region_name: &str, kind: EntryKind, content: String, tokens: usize, taint_level: TaintLevel, ) -> Result<()>

Add a typed entry to a region with a specific taint level.

The typed+tainted counterpart of add_typed_entry and add_tainted_to_region: the entry keeps its EntryKind (so turn-group eviction stays intact) while contributing the given taint level (so the taint gate sees sensitive tool output).

Source

pub fn overall_taint(&self) -> Option<TaintLevel>

Get the overall taint level (max across all regions). Returns None if no region has taint tracking enabled.

Source

pub fn taint_summary(&self) -> Vec<(String, TaintLevel)>

Get a summary of taint levels across all regions (for dashboard/audit).

Trait Implementations§

Source§

impl Clone for ContextWindow

Source§

fn clone(&self) -> ContextWindow

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Component for ContextWindow
where Self: Send + Sync + 'static,

Source§

const STORAGE_TYPE: StorageType = bevy_ecs::component::StorageType::Table

A constant indicating the storage type used for this component.
Source§

type Mutability = Mutable

A marker type to assist Bevy with determining if this component is mutable, or immutable. Mutable components will have Component<Mutability = Mutable>, while immutable components will instead have Component<Mutability = Immutable>. Read more
Source§

fn register_required_components( _requiree: ComponentId, required_components: &mut RequiredComponentsRegistrator<'_, '_>, )

Registers required components. Read more
Source§

fn clone_behavior() -> ComponentCloneBehavior

Called when registering this component, allowing to override clone function (or disable cloning altogether) for this component. Read more
Source§

fn relationship_accessor() -> Option<ComponentRelationshipAccessor<Self>>

Returns ComponentRelationshipAccessor required for working with relationships in dynamic contexts. Read more
Source§

fn on_add() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>

Gets the on_add ComponentHook for this Component if one is defined.
Source§

fn on_insert() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>

Gets the on_insert ComponentHook for this Component if one is defined.
Source§

fn on_discard() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>

Gets the on_discard ComponentHook for this Component if one is defined.
Source§

fn on_remove() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>

Gets the on_remove ComponentHook for this Component if one is defined.
Source§

fn on_despawn() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>

Gets the on_despawn ComponentHook for this Component if one is defined.
Source§

fn map_entities<E>(_this: &mut Self, _mapper: &mut E)
where E: EntityMapper,

Maps the entities on this component using the given EntityMapper. This is used to remap entities in contexts like scenes and entity cloning. When deriving Component, this is populated by annotating fields containing entities with #[entities] Read more
Source§

impl Debug for ContextWindow

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<C> Bundle for C
where C: Component,

Source§

fn component_ids( components: &mut ComponentsRegistrator<'_>, ) -> impl Iterator<Item = ComponentId> + use<C>

Source§

fn get_component_ids( components: &Components, ) -> impl Iterator<Item = Option<ComponentId>>

Return a iterator over this Bundle’s component ids. This will be None if the component has not been registered.
Source§

impl<C> BundleFromComponents for C
where C: Component,

Source§

unsafe fn from_components<T, F>(ctx: &mut T, func: &mut F) -> C
where F: for<'a> FnMut(&'a mut T) -> OwningPtr<'a>, C: Sized,

Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ConditionalSend for T
where T: Send,

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<C> DynamicBundle for C
where C: Component,

Source§

type Effect = ()

An operation on the entity that happens after inserting this bundle.
Source§

unsafe fn get_components( ptr: MovingPtr<'_, C>, func: &mut impl FnMut(StorageType, OwningPtr<'_>), ) -> <C as DynamicBundle>::Effect

Moves the components out of the bundle. Read more
Source§

unsafe fn apply_effect( _ptr: MovingPtr<'_, MaybeUninit<C>>, _entity: &mut EntityWorldMut<'_>, )

Applies the after-effects of spawning this bundle. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoResult<T> for T

Source§

fn into_result(self) -> Result<T, RunSystemError>

Converts this type into the system output type.
Source§

impl<A> Is for A
where A: Any,

Source§

fn is<T>() -> bool
where T: Any,

Checks if the current type “is” another type, using a TypeId equality comparison. This is most useful in the context of generic logic. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> TypeData for T
where T: 'static + Send + Sync + Clone,

Source§

fn clone_type_data(&self) -> Box<dyn TypeData>

Creates a type-erased clone of this value.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more