Skip to main content

enact_context/
lib.rs

1//! # enact-context
2//!
3//! Context window management and compaction for long-running agentic executions.
4//!
5//! ## Key Components
6//!
7//! - **ContextWindow**: Manages the context segments within token limits
8//! - **ContextBudget**: Token allocation across different segment types
9//! - **Compactor**: Strategies for reducing context size when approaching limits
10//! - **TokenCounter**: Token counting using tiktoken
11//! - **PromptCalibrator**: Constructs calibrated prompts for spawned callables
12//! - **StepContextBuilder**: Extracts learnings when steps are discovered
13//! - **ResultCondenser**: Condenses child traces to 1-2k token summaries
14//!
15//! ## Architecture
16//!
17//! ```text
18//! ┌─────────────────────────────────────────────────────────────┐
19//! │                     ContextWindow                            │
20//! ├─────────────────────────────────────────────────────────────┤
21//! │  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐         │
22//! │  │   System    │  │   History   │  │   Tools     │  ...    │
23//! │  │  (critical) │  │   (high)    │  │  (medium)   │         │
24//! │  └─────────────┘  └─────────────┘  └─────────────┘         │
25//! │                                                              │
26//! │  ContextBudget: total=128K, used=45K, available=83K         │
27//! ├─────────────────────────────────────────────────────────────┤
28//! │  Compactor: summarize | truncate | sliding_window           │
29//! └─────────────────────────────────────────────────────────────┘
30//!
31//! ┌─────────────────────────────────────────────────────────────┐
32//! │                   Callable Spawning Flow                     │
33//! ├─────────────────────────────────────────────────────────────┤
34//! │  Parent Context ──► PromptCalibrator ──► Child Prompt       │
35//! │       │                                       │              │
36//! │       │          StepContextBuilder           │              │
37//! │       ▼                 │                     ▼              │
38//! │  Step Discovered ──────►│◄───── Child Execution             │
39//! │       │                 │              │                     │
40//! │       ▼                 ▼              ▼                     │
41//! │  Learnings ◄──── ResultCondenser ◄── Child Trace            │
42//! └─────────────────────────────────────────────────────────────┘
43//! ```
44//!
45//! ## Usage
46//!
47//! ```rust,ignore
48//! use enact_context::{ContextWindow, ContextBudget, Compactor};
49//!
50//! // Create a context window with GPT-4 128K budget
51//! let budget = ContextBudget::preset_gpt4_128k();
52//! let mut window = ContextWindow::new(budget);
53//!
54//! // Add segments
55//! window.add_segment(ContextSegment::system("You are a helpful assistant..."))?;
56//! window.add_segment(ContextSegment::user_input("Hello!"))?;
57//!
58//! // Check if compaction is needed
59//! if window.needs_compaction() {
60//!     let compactor = Compactor::summarize();
61//!     window.compact(&compactor).await?;
62//! }
63//! ```
64
65pub mod budget;
66pub mod calibrator;
67pub mod compactor;
68pub mod condenser;
69pub mod segment;
70pub mod step_context;
71pub mod token_counter;
72pub mod window;
73
74// Re-exports
75pub use budget::{ContextBudget, SegmentBudget};
76pub use calibrator::{CalibratedPrompt, CalibrationConfig, PromptCalibrator};
77pub use compactor::{CompactionResult, CompactionStrategy, Compactor};
78pub use condenser::{CondensedResult, CondenserConfig, ExecutionTrace, ResultCondenser};
79pub use segment::{ContextPriority, ContextSegment, ContextSegmentType};
80pub use step_context::{
81    StepContextBuilder, StepContextConfig, StepContextResult, StepLearning, ToolCallInfo,
82};
83pub use token_counter::TokenCounter;
84pub use window::ContextWindow;
85
86/// Prelude for common imports
87pub mod prelude {
88    pub use crate::budget::{ContextBudget, SegmentBudget};
89    pub use crate::calibrator::{CalibratedPrompt, CalibrationConfig, PromptCalibrator};
90    pub use crate::compactor::{CompactionResult, CompactionStrategy, Compactor};
91    pub use crate::condenser::{CondensedResult, CondenserConfig, ExecutionTrace, ResultCondenser};
92    pub use crate::segment::{ContextPriority, ContextSegment, ContextSegmentType};
93    pub use crate::step_context::{
94        StepContextBuilder, StepContextConfig, StepContextResult, StepLearning,
95    };
96    pub use crate::token_counter::TokenCounter;
97    pub use crate::window::ContextWindow;
98}