aither_agent/context_window.rs
1//! Structured snapshots of the currently assembled context window.
2
3use aither_core::llm::Message;
4use serde::{Deserialize, Serialize};
5
6/// Lifecycle phase of the assembled context window.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
8#[serde(rename_all = "snake_case")]
9pub enum ContextWindowPhase {
10 /// Usage is below the compaction threshold.
11 Stable,
12 /// The window should be rebuilt before the next request.
13 ///
14 /// A staged system-block rewrite is waiting for the next compaction to
15 /// fold it into the cacheable prefix.
16 ReassemblyDue,
17 /// A compaction is in flight alongside the run.
18 Compacting,
19 /// Usage reached the pause threshold and the run must wait for a handoff.
20 Exhausted,
21}
22
23/// Metrics describing the currently assembled context window.
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct ContextWindowMetrics {
26 /// Fraction of the calibrated context window currently in use.
27 pub usage_fraction: f32,
28 /// Usable context window in tokens.
29 pub context_window: u32,
30 /// Estimated prompt tokens currently held in the window.
31 pub estimated_tokens: usize,
32 /// Characters per token, calibrated against the provider's own counts.
33 pub chars_per_token: f64,
34 /// Number of persistent system blocks in the stable prefix.
35 pub system_block_count: usize,
36 /// Number of conversation messages.
37 pub recent_message_count: usize,
38 /// Number of durable system alerts that reassembly can prune.
39 pub recent_system_message_count: usize,
40 /// Whether a handoff document is present.
41 pub has_handoff: bool,
42 /// Whether a system block rewrite is waiting for reassembly.
43 pub has_staged_system_updates: bool,
44 /// How many times the cacheable prefix has been rewritten this session.
45 pub prompt_cache_epoch: u32,
46}
47
48/// Fully assembled LLM request window plus lifecycle metadata.
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct ContextWindowSnapshot {
51 /// Lifecycle phase derived from the current policy.
52 pub phase: ContextWindowPhase,
53 /// Metrics for observability and persistence.
54 pub metrics: ContextWindowMetrics,
55 /// Exact message list that would be sent to the model for the next turn.
56 pub messages: Vec<Message>,
57}