lean-ctx-cloud 3.12.12

The LeanCTX connector: enrollment, bus, knowledge sync, tasks and policy on top of LeanCTX | OSS.
Documentation
// SPDX-License-Identifier: Apache-2.0
//! Delivery of a place's knowledge into an agent's context (SPEC-ENGINE
//! §3.5): which folded nodes a host session may see, in which bucket, and a
//! deterministic rendering (#498) — the same replica and query give the
//! same bytes, whatever the clock, the scores or the trust revision say.

use serde_json::Value;

use crate::replica::fold::{Graph, Node};
use crate::untrusted::{UntrustedKind, wrap};

pub mod knowledge_state;

/// Bucket a delivered node is shown in (§3.5.2), in delivery order. The
/// last two hold knowledge that is not current truth (v5 2.2.1): it is
/// delivered after everything else and labelled, never as a plain item.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Bucket {
    High,
    Normal,
    /// An anchor changed or is gone: re-verify before relying on it.
    Outdated,
    /// An open contradiction with another live claim.
    Conflicting,
}

impl Bucket {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::High => "high",
            Self::Normal => "normal",
            Self::Outdated => "outdated",
            Self::Conflicting => "conflicting",
        }
    }
}

/// Node type order inside a bucket (§3.5.3).
fn type_rank(node_type: &str) -> u8 {
    match node_type {
        "gotcha" => 0,
        "decision" => 1,
        "procedure" => 2,
        "fact" => 3,
        "finding" => 4,
        "episode" => 5,
        _ => 6,
    }
}

/// One deliverable node.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Item {
    pub node_id: String,
    pub node_type: String,
    pub bucket: Bucket,
    /// `low` trust sorts last within `normal`.
    pub low: bool,
    /// Knowledge state `provisional` (no observed evidence): served, but
    /// labelled so an agent does not take a claim for an observation.
    pub provisional: bool,
    pub own: bool,
    pub text: String,
    /// Relevance tier 0 (best) – 3 for the query.
    pub tier: u8,
}

/// The text a node is delivered as: the statement, a procedure's title
/// with numbered steps, an episode's summary. Never truncated (R70).
fn text_of(node: &Node) -> Option<String> {
    let f = |k: &str| node.fields.get(k).and_then(Value::as_str);
    match node.node_type.as_str() {
        "procedure" => {
            let mut t = f("title")?.to_string();
            if let Some(steps) = node.fields.get("steps").and_then(Value::as_array) {
                for (i, s) in steps.iter().filter_map(Value::as_str).enumerate() {
                    t.push_str(&format!("\n  {}. {s}", i + 1));
                }
            }
            Some(t)
        }
        "episode" => f("summary").map(str::to_string),
        "gotcha" => f("statement").map(|s| format!("Gotcha: {s}")),
        "decision" => f("statement").map(|s| match f("chosen") {
            Some(chosen) => format!("{s} (chosen: {chosen})"),
            None => s.to_string(),
        }),
        _ => f("statement").map(str::to_string),
    }
}

/// Bucket of a node for a session of the owning workspace, from its
/// provenance and the trust bucket (absent until the trust job publishes
/// one).
pub fn bucket_of(node: &Node, trust_bucket: Option<&str>) -> (Bucket, bool) {
    let own = node.provenance.as_deref() == Some("own");
    match trust_bucket {
        Some("high") if !own => (Bucket::High, false),
        Some("low") => (Bucket::Normal, true),
        _ => (Bucket::Normal, false),
    }
}

/// Relevance tier of `text` for `query` terms: share of query terms the
/// text contains, cut at fixed quantiles (no raw scores in the output).
fn tier(text: &str, terms: &[String]) -> Option<u8> {
    if terms.is_empty() {
        return Some(0);
    }
    let lower = text.to_lowercase();
    let hits = terms.iter().filter(|t| lower.contains(t.as_str())).count();
    if hits == 0 {
        return None;
    }
    let share = hits * 100 / terms.len();
    Some(match share {
        100 => 0,
        67..=99 => 1,
        34..=66 => 2,
        _ => 3,
    })
}

/// The deliverable items of `graph` for `query` (all when empty), in the
/// order of §3.5.3: bucket → node type → relevance tier → node id.
pub fn items(graph: &Graph, trust: &dyn Fn(&str) -> Option<String>, query: &str) -> Vec<Item> {
    items_in(graph, &knowledge_state::Index::new(graph), trust, query)
}

/// [`items`] with the knowledge states of `states` (e.g. an index that
/// also resolves fact links from the save history).
pub fn items_in(
    graph: &Graph,
    states: &knowledge_state::Index,
    trust: &dyn Fn(&str) -> Option<String>,
    query: &str,
) -> Vec<Item> {
    let terms: Vec<String> = query
        .split_whitespace()
        .map(str::to_lowercase)
        .filter(|t| t.chars().count() >= 2)
        .collect();
    let mut out: Vec<Item> = graph
        .live_nodes()
        .filter_map(|(id, node)| {
            // Rejected and superseded knowledge leaves delivery (SPEC-ENGINE
            // §3.4 edge table); outdated and conflicting go last, labelled.
            let state = states.state(id, node);
            let (bucket, low) = match state {
                (knowledge_state::State::Rejected, _)
                | (knowledge_state::State::Outdated, Some("superseded")) => return None,
                (knowledge_state::State::Outdated, _) => (Bucket::Outdated, false),
                (knowledge_state::State::Conflicting, _) => (Bucket::Conflicting, false),
                _ => bucket_of(node, trust(id).as_deref()),
            };
            let text = text_of(node)?;
            Some(Item {
                node_id: id.clone(),
                node_type: node.node_type.clone(),
                bucket,
                low,
                provisional: state.0 == knowledge_state::State::Provisional,
                own: node.provenance.as_deref() == Some("own"),
                tier: tier(&text, &terms)?,
                text,
            })
        })
        .collect();
    out.sort_by(|a, b| {
        (a.bucket, a.low, type_rank(&a.node_type), a.tier, &a.node_id).cmp(&(
            b.bucket,
            b.low,
            type_rank(&b.node_type),
            b.tier,
            &b.node_id,
        ))
    });
    out
}

/// Render items for the agent within `budget_tokens` (an item that does
/// not fit is skipped whole). Header `vault @<ns>/<name> · <d>`, `d` a
/// digest of the delivered (node id, bucket) pairs; other members' text is
/// fenced as data.
///
/// Every item whose id follows the reference grammar carries its
/// `node:<id>` (v5 2.6.1), and the section says once how to cite it: a
/// claim that names the id in its refs gets a `derived_from` edge
/// (`capture::derived`), the only evidence of verified reuse (BAUPLAN
/// §5.3). The hint line is counted against the budget with the first
/// citable item; a section without one carries no hint.
pub fn render(vault: &str, items: &[Item], budget_tokens: usize) -> String {
    let source = format!("vault:{vault}");
    let mut lines = Vec::new();
    let mut delivered: Vec<(String, &str)> = Vec::new();
    let mut used = 0;
    let mut cited = false;
    for item in items {
        // Only an id the capture path accepts back is shown: a malformed
        // target from a foreign event is neither citable nor rendered
        // outside the data fence.
        let citable = matches!(
            crate::capture::refs::parse(&format!("node:{}", item.node_id)),
            Some(crate::capture::refs::Reference::Node(_))
        );
        let id = if citable {
            format!(" node:{}", item.node_id)
        } else {
            String::new()
        };
        let label = match item.bucket {
            Bucket::Outdated => "[outdated: re-verify] ",
            Bucket::Conflicting => "[conflicting] ",
            _ if item.low && item.provisional => "[low trust, provisional] ",
            _ if item.low => "[low trust] ",
            _ if item.provisional => "[provisional] ",
            _ => "",
        };
        let body = if item.own {
            item.text.clone()
        } else {
            wrap(UntrustedKind::Knowledge, &source, &item.text)
        };
        let badge = if item.own { "" } else { " (from workspace)" };
        let line = format!("- {label}{}{id}{badge}: {body}", item.node_type);
        let mut cost = lean_ctx::core::tokens::count_tokens(&line);
        if citable && !cited {
            cost += lean_ctx::core::tokens::count_tokens(CITE_HINT);
        }
        if used + cost > budget_tokens {
            continue;
        }
        used += cost;
        cited |= citable;
        lines.push(line);
        delivered.push((item.node_id.clone(), item.bucket.as_str()));
    }
    if lines.is_empty() {
        return String::new();
    }
    delivered.sort();
    let mut h = blake3::Hasher::new();
    for (id, bucket) in &delivered {
        h.update(id.as_bytes());
        h.update(&[0]);
        h.update(bucket.as_bytes());
        h.update(&[0]);
    }
    let digest = &h.finalize().to_hex()[..8];
    let hint = if cited {
        format!("{CITE_HINT}\n")
    } else {
        String::new()
    };
    format!("vault @{vault} · {digest}\n{hint}{}\n", lines.join("\n"))
}

/// How an agent cites a delivered item (once per section, ~15 tokens).
pub const CITE_HINT: &str = "Building on an item? Put its node:<id> in remember refs.";

/// Knowledge section budget per call (SPEC-ENGINE §3.5.1: hard max).
pub const MAX_SECTION_TOKENS: usize = 1_500;

/// The machine replica key, read once per process (keychain or file).
fn master_key(config_dir: &std::path::Path) -> Option<&'static [u8; 32]> {
    static MK: std::sync::OnceLock<Option<[u8; 32]>> = std::sync::OnceLock::new();
    MK.get_or_init(|| {
        crate::replica::crypt::master_key(config_dir, false)
            .ok()
            .map(|(k, _)| *k)
    })
    .as_ref()
}

/// The folded graph of `vault_id`'s replica and its knowledge states
/// (edges and the save history's fact links, #286); `None` when there is
/// none (nothing is created here).
fn folded(store: &crate::CloudStore, vault_id: &str) -> Option<(Graph, knowledge_state::Index)> {
    let dir = crate::replica::store::place_dir(store.data_dir(), vault_id);
    if !dir.join("replica.db").exists() {
        return None;
    }
    let mk = master_key(store.config_dir())?;
    let (replica, _) = crate::replica::Replica::open(store.data_dir(), vault_id, mk).ok()?;
    let mut graph = Graph::default();
    // lean-ctx: folds the stored log per call; switch to the materialised
    // nodes table once replicas reach ~10k events.
    graph.fold(&replica.fold_events().ok()?).ok()?;
    let states = match replica.first_saves() {
        Ok(saves) => knowledge_state::Index::with_saves(&graph, &saves),
        Err(_) => knowledge_state::Index::new(&graph),
    };
    Some((graph, states))
}

/// The vault `root` is bound to through its identity; `None` for a folder
/// the person unlinked (rc4-6, bd 39936) and while the opt-out store is
/// damaged.
fn bound_vault(
    store: &crate::CloudStore,
    root: &std::path::Path,
) -> Option<crate::vault::VaultRecord> {
    if crate::unlinked::contains(store.data_dir(), root) {
        return None;
    }
    let stable =
        lean_ctx_protocol::cloud::vault::stable_identity(&crate::vault::project_identity(root)?);
    // Only a record of the connected Cloud and account (rc3-81).
    crate::vault::connected_map(store)
        .vaults
        .get(&stable)
        .cloned()
}

/// The vault section a recall in `project_root` delivers (§3.5.1): the
/// bound vault's replica, deterministic and within budget. `None` when the
/// checkout is not bound or has no replica yet (nothing is created here),
/// and for a folder the person unlinked (rc4-6, bd 39936).
pub fn for_project(project_root: &str, query: &str) -> Option<String> {
    let store = crate::CloudStore::open().ok()?;
    // Nothing while not connected, nothing of another account (3.12.8).
    store.instance().ok()??;
    let record = bound_vault(&store, std::path::Path::new(project_root))?;
    let (graph, states) = folded(&store, &record.vault_id)?;
    let list = items_in(&graph, &states, &|_| None, query);
    let out = render(
        &format!("{}/{}", record.ns, record.name),
        &list,
        MAX_SECTION_TOKENS,
    );
    (!out.is_empty()).then_some(out)
}

#[cfg(test)]
mod tests;