Skip to main content

lean_ctx/core/config/
value_display.rs

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