Skip to main content

lean_ctx/core/config/
value_display.rs

1//! `[value_display]` — how lean-ctx shows its measured value outside the
2//! dashboard (status line, turn/session recaps, prompt segment, milestones).
3//!
4//! None of these channels reach the model's context; they are user-only.
5
6use serde::{Deserialize, Serialize};
7
8/// How much of lean-ctx's value is surfaced.
9///
10/// - `off`: nothing (no snapshot is written either)
11/// - `minimal` (default): status line / prompt segment, plus a recap when a
12///   turn window crossed its threshold
13/// - `milestones`: `minimal` plus OS notifications for milestones
14/// - `verbose`: every recap window, regardless of the token threshold
15#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
16#[serde(rename_all = "lowercase")]
17pub enum ValueDisplayMode {
18    Off,
19    #[default]
20    Minimal,
21    Milestones,
22    Verbose,
23}
24
25impl ValueDisplayMode {
26    pub fn parse(value: &str) -> Option<Self> {
27        match value.trim().to_ascii_lowercase().as_str() {
28            "off" | "0" | "false" | "none" => Some(Self::Off),
29            "minimal" | "on" | "1" | "true" => Some(Self::Minimal),
30            "milestones" => Some(Self::Milestones),
31            "verbose" => Some(Self::Verbose),
32            _ => None,
33        }
34    }
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
38#[serde(default)]
39pub struct ValueDisplayConfig {
40    /// Override via `LEAN_CTX_VALUE_DISPLAY`.
41    pub mode: ValueDisplayMode,
42    /// A turn recap is considered every N agent turns.
43    pub recap_every_turns: u32,
44    /// …and only shown when the window saved at least this many tokens (or a
45    /// security event happened).
46    pub recap_min_tokens: u64,
47    /// OS notifications for milestones (at most one per day) in mode
48    /// `milestones` or `verbose`; `false` silences them there.
49    pub notifications: bool,
50    /// Opt-in `lean-ctx:` trailer in commit messages.
51    pub git_trailer: bool,
52}
53
54impl Default for ValueDisplayConfig {
55    fn default() -> Self {
56        Self {
57            mode: ValueDisplayMode::default(),
58            recap_every_turns: 10,
59            recap_min_tokens: 50_000,
60            notifications: true,
61            git_trailer: false,
62        }
63    }
64}
65
66impl ValueDisplayConfig {
67    /// The configured mode, with `LEAN_CTX_VALUE_DISPLAY` taking precedence.
68    pub fn effective_mode(&self) -> ValueDisplayMode {
69        std::env::var("LEAN_CTX_VALUE_DISPLAY")
70            .ok()
71            .and_then(|v| ValueDisplayMode::parse(&v))
72            .unwrap_or(self.mode)
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn defaults_are_subtle_and_on() {
82        let cfg: ValueDisplayConfig = toml::from_str("").unwrap();
83        assert_eq!(cfg, ValueDisplayConfig::default());
84        assert_eq!(cfg.mode, ValueDisplayMode::Minimal);
85        assert!(!cfg.git_trailer);
86    }
87
88    #[test]
89    fn parses_toml_section() {
90        let cfg: ValueDisplayConfig =
91            toml::from_str("mode = \"off\"\nrecap_every_turns = 5").unwrap();
92        assert_eq!(cfg.mode, ValueDisplayMode::Off);
93        assert_eq!(cfg.recap_every_turns, 5);
94        assert_eq!(cfg.recap_min_tokens, 50_000);
95    }
96
97    #[test]
98    fn env_values_parse() {
99        assert_eq!(ValueDisplayMode::parse("OFF"), Some(ValueDisplayMode::Off));
100        assert_eq!(ValueDisplayMode::parse("0"), Some(ValueDisplayMode::Off));
101        assert_eq!(
102            ValueDisplayMode::parse("verbose"),
103            Some(ValueDisplayMode::Verbose)
104        );
105        assert_eq!(ValueDisplayMode::parse("loud"), None);
106    }
107}