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