Skip to main content

lean_ctx/core/value/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Value surface: makes what lean-ctx did visible outside the dashboard —
3//! subtly, outside the model's context, and with every number provable.
4//!
5//! * [`snapshot`] — a tiny per-session file the MCP server keeps current, so
6//!   status lines and prompt segments render in microseconds without touching
7//!   the ledger.
8//! * [`mod@format`] — the shared one-line renderer.
9//! * [`proof`] — recomputes each number from the hash-chained savings ledger
10//!   and audit trail and verifies both chains (`lean-ctx value`).
11//! * [`recap`] — when a host hook speaks up (turn and session recaps, weekly
12//!   digest), always as a user-only message, never into the model's context.
13//! * [`milestones`] — a native desktop notification ([`notify`]) when a
14//!   verified lifetime threshold is first crossed; at most one a day.
15
16pub mod format;
17pub mod milestones;
18pub mod notify;
19pub mod proof;
20pub mod recap;
21pub mod snapshot;
22
23use std::sync::RwLock;
24
25static CURRENT_SESSION: RwLock<Option<String>> = RwLock::new(None);
26
27/// Marks `id` as the session this process is serving. Ledger events and
28/// security events recorded afterwards commit it into their hashes.
29pub fn set_current_session(id: &str) {
30    if id.is_empty() {
31        return;
32    }
33    if let Ok(mut guard) = CURRENT_SESSION.write()
34        && guard.as_deref() != Some(id)
35    {
36        *guard = Some(id.to_string());
37    }
38}
39
40/// The session this process is serving, if it is an MCP server that has
41/// handled a tool call. `None` for CLI, hook and proxy processes.
42pub fn current_session() -> Option<String> {
43    CURRENT_SESSION.read().ok().and_then(|guard| guard.clone())
44}
45
46/// Whether any value display is on (`[value_display] mode`, overridable via
47/// `LEAN_CTX_VALUE_DISPLAY`). The proof chains are written regardless; this
48/// only gates the display snapshot and the user-facing channels.
49pub fn display_enabled() -> bool {
50    crate::core::config::Config::load_arc()
51        .value_display
52        .effective_mode()
53        != crate::core::config::ValueDisplayMode::Off
54}