Skip to main content

merry_runtime/
context.rs

1//! Deterministic context compiler and provider-neutral context state.
2//!
3//! This module is the stable facade for context contracts. Implementation
4//! responsibilities live in focused child modules: budget calculation, owned
5//! context input models, and deterministic provider-visible projection.
6
7use thiserror::Error;
8
9#[path = "context/budget.rs"]
10mod budget;
11#[path = "context/input.rs"]
12mod input;
13#[path = "context/projection.rs"]
14mod projection;
15
16pub use budget::{
17    AUTO_COMPACT_THRESHOLD_PERCENT, CheckpointDecision, ContextBudget, ResolvedContextWindow,
18    bundled_model_context_window, decide_checkpoint, resolve_context_window,
19};
20pub use input::{
21    CompactedCheckpoint, CompactedCheckpointSummary, ContextEntry, ContextSummary, ProjectRules,
22    SessionContextSnapshot, TaskAnchor,
23};
24pub(crate) use projection::compacted_checkpoint_wrapper_token_ceiling;
25pub use projection::{CompiledContext, CompiledContextSection, ContextCompiler};
26
27/// Coding-agent fallback used when config, provider metadata, and model catalogs are absent.
28pub const DEFAULT_CONTEXT_WINDOW_FALLBACK_TOKENS: u64 = 272_000;
29
30/// Errors raised while constructing or compiling structured context.
31#[derive(Debug, Error, PartialEq, Eq)]
32pub enum ContextError {
33    /// A required context field was blank.
34    #[error("{field} must not be blank")]
35    BlankField {
36        /// Name of the invalid field.
37        field: &'static str,
38    },
39
40    /// A context field contained unsupported control characters.
41    #[error("{field} must not contain control characters other than newline or tab")]
42    InvalidControlCharacter {
43        /// Name of the invalid field.
44        field: &'static str,
45    },
46
47    /// Context budget inputs could not produce a valid body budget.
48    #[error("invalid context budget: {reason}")]
49    InvalidBudget {
50        /// Actionable reason the budget was rejected.
51        reason: &'static str,
52    },
53
54    /// Context window metadata could not produce a valid window.
55    #[error("invalid context window: {reason}")]
56    InvalidContextWindow {
57        /// Actionable reason the window was rejected.
58        reason: &'static str,
59    },
60
61    /// Internal memory text was provided without provenance metadata.
62    #[error("memory item {memory_id} has no provenance metadata")]
63    MemoryWithoutEvidence {
64        /// Memory identifier that failed provenance validation.
65        memory_id: String,
66    },
67}
68
69#[cfg(test)]
70mod tests;