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
//! Structured snapshots of the currently assembled context window.
use aither_core::llm::Message;
use serde::{Deserialize, Serialize};
/// Lifecycle phase of the assembled context window.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContextWindowPhase {
/// Usage is below the compaction threshold.
Stable,
/// The window should be rebuilt before the next request.
///
/// A staged system-block rewrite is waiting for the next compaction to
/// fold it into the cacheable prefix.
ReassemblyDue,
/// A compaction is in flight alongside the run.
Compacting,
/// Usage reached the pause threshold and the run must wait for a handoff.
Exhausted,
}
/// Metrics describing the currently assembled context window.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContextWindowMetrics {
/// Fraction of the calibrated context window currently in use.
pub usage_fraction: f32,
/// Usable context window in tokens.
pub context_window: u32,
/// Estimated prompt tokens currently held in the window.
pub estimated_tokens: usize,
/// Characters per token, calibrated against the provider's own counts.
pub chars_per_token: f64,
/// Number of persistent system blocks in the stable prefix.
pub system_block_count: usize,
/// Number of conversation messages.
pub recent_message_count: usize,
/// Number of durable system alerts that reassembly can prune.
pub recent_system_message_count: usize,
/// Whether a handoff document is present.
pub has_handoff: bool,
/// Whether a system block rewrite is waiting for reassembly.
pub has_staged_system_updates: bool,
/// How many times the cacheable prefix has been rewritten this session.
pub prompt_cache_epoch: u32,
}
/// Fully assembled LLM request window plus lifecycle metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContextWindowSnapshot {
/// Lifecycle phase derived from the current policy.
pub phase: ContextWindowPhase,
/// Metrics for observability and persistence.
pub metrics: ContextWindowMetrics,
/// Exact message list that would be sent to the model for the next turn.
pub messages: Vec<Message>,
}