use lean_ctx_time::UtcDateTime;
use sha2::{Digest, Sha256};
use crate::capture::{self, CaptureRun, ReasonCode, dedupe};
use lean_ctx::core::knowledge::{KnowledgeFact, NodeType, ProjectKnowledge};
use lean_ctx::core::sensitivity::SensitivityLevel;
use lean_ctx_protocol::cloud::knowledge::{FactV1, ResearchContextDocumentV1};
#[derive(Debug, Clone, Default)]
pub struct LearnPlan {
pub added: Vec<FactV1>,
pub dropped: Vec<(String, ReasonCode)>,
pub merges: Vec<(String, Vec<String>)>,
pub watermark: Option<UtcDateTime>,
pub import_sources: Vec<String>,
pub captured: Vec<capture::CapturedFact>,
}
fn slug(s: &str, max: usize) -> String {
let mut out = String::new();
for c in s.chars() {
if c.is_ascii_alphanumeric() {
out.push(c.to_ascii_lowercase());
} else if !out.ends_with('-') && !out.is_empty() {
out.push('-');
}
}
let mut out: String = out.trim_matches('-').chars().take(max).collect();
while out.ends_with('-') {
out.pop();
}
out
}
fn short_hash(parts: &[&str]) -> String {
let mut h = Sha256::new();
for p in parts {
h.update(p.as_bytes());
h.update([0u8]);
}
hex::encode(&h.finalize()[..4])
}
pub fn fact_id(category: &str, key: &str, value: &str) -> String {
let hash = short_hash(&[category, key, value]);
let s = slug(key, 60);
if s.is_empty() {
format!("lc-{hash}")
} else {
format!("lc-{s}-{hash}")
}
}
pub fn kind_of(node_type: NodeType) -> &'static str {
match node_type {
NodeType::Fact | NodeType::Finding => "observation",
NodeType::Decision => "decision",
NodeType::Gotcha => "gotcha",
NodeType::Procedure => "procedure",
}
}
fn node_type_of_kind(kind: &str) -> NodeType {
match kind {
"decision" => NodeType::Decision,
"gotcha" => NodeType::Gotcha,
"procedure" => NodeType::Procedure,
_ => NodeType::Fact,
}
}
fn statement_of(c: &capture::CaptureCandidate) -> String {
let mut text = c.statement.clone();
if c.node_type != NodeType::Procedure {
return text;
}
for (i, step) in c.steps.iter().enumerate() {
text.push_str(&format!("\n{}. {step}", i + 1));
}
text
}
fn humanize(key: &str) -> String {
key.split(['-', '_', '.', ':', '/'])
.filter(|s| !s.is_empty())
.collect::<Vec<_>>()
.join(" ")
}
fn eligible(f: &KnowledgeFact) -> bool {
f.imported_from.is_none()
&& f.is_current()
&& !lean_ctx::core::knowledge::is_machine_derived(&f.category, &f.key)
}
fn eligible_import(f: &KnowledgeFact) -> bool {
matches!(f.imported_from.as_deref(), Some("docs" | "git"))
&& f.review.is_none()
&& f.is_current()
&& !lean_ctx::core::knowledge::is_machine_derived(&f.category, &f.key)
&& f.sensitivity == SensitivityLevel::Public
}
pub fn import_source(f: &KnowledgeFact) -> Option<String> {
let source = match f.imported_from.as_deref()? {
"git" => format!("import:git:{}", f.key.strip_prefix("git:")?),
"docs" => {
let (path, section) = f.key.split_once('#')?;
let section = section.rsplit_once('/').map_or(section, |(head, _)| head);
format!(
"import:docs:{path}#{}",
lean_ctx_protocol::cloud::ctxpkg::import_slug(§ion.replace('/', " "))
)
}
_ => return None,
};
lean_ctx_protocol::cloud::knowledge::import_source_ok(&source).then_some(source)
}
pub fn plan_learn(
knowledge: &ProjectKnowledge,
repo: &ResearchContextDocumentV1,
since: Option<UtcDateTime>,
limit: usize,
run: &mut CaptureRun<'_>,
) -> LearnPlan {
plan_facts(knowledge, repo, since, limit, run, false)
}
pub fn plan_import(
knowledge: &ProjectKnowledge,
repo: &ResearchContextDocumentV1,
since: Option<UtcDateTime>,
limit: usize,
run: &mut CaptureRun<'_>,
) -> LearnPlan {
plan_facts(knowledge, repo, since, limit, run, true)
}
fn changed_at(f: &KnowledgeFact) -> UtcDateTime {
f.created_at.max(f.last_confirmed)
}
fn import_reference(source: &str) -> Option<String> {
if let Some(sha) = source.strip_prefix("import:git:") {
return Some(format!("commit:{}", sha.to_ascii_lowercase()));
}
let path = source.strip_prefix("import:docs:")?.split('#').next()?;
Some(format!("file:{path}"))
}
fn held_index(pk_id: &[u8; 32], repo: &ResearchContextDocumentV1) -> (dedupe::Index, Vec<String>) {
let mut index = dedupe::Index::default();
let mut ids = Vec::new();
for f in &repo.facts {
let node_type = node_type_of_kind(&f.kind);
let refs = f
.anchors
.iter()
.map(|a| format!("file:{}", a.path))
.chain(f.sources.iter().cloned())
.collect();
index.insert(
dedupe::exact_key(pk_id, node_type, &f.statement),
dedupe::Known {
node_type,
signature: dedupe::signature(pk_id, &f.statement),
refs,
},
);
ids.push(f.id.clone());
}
(index, ids)
}
fn plan_facts(
knowledge: &ProjectKnowledge,
repo: &ResearchContextDocumentV1,
since: Option<UtcDateTime>,
limit: usize,
run: &mut CaptureRun<'_>,
imports: bool,
) -> LearnPlan {
let mut plan = LearnPlan::default();
let stamp = |f: &KnowledgeFact| {
if imports {
changed_at(f)
} else {
f.capture
.changed_at
.map_or(f.created_at, |at| at.max(f.created_at))
}
};
let mut facts: Vec<&KnowledgeFact> = knowledge
.facts
.iter()
.filter(|f| {
(if imports {
eligible_import(f)
} else {
eligible(f)
}) && (since.is_none_or(|since| stamp(f) > since) || run.is_pending(f))
})
.collect();
facts.sort_by(|a, b| {
(stamp(a), &a.category, &a.key, &a.value).cmp(&(stamp(b), &b.category, &b.key, &b.value))
});
let (mut index, mut held_ids) = held_index(&run.pk_id, repo);
let mut seen_ids: std::collections::HashSet<String> = held_ids.iter().cloned().collect();
for f in facts {
if plan.added.len() == limit {
run.counts.reject(ReasonCode::BudgetBatchFull);
break;
}
plan.watermark = plan.watermark.max(Some(stamp(f)));
let import = if imports {
let Some(source) = import_source(f) else {
continue;
};
Some(source)
} else {
None
};
run.counts.considered += 1;
let label = format!("{}/{}", f.category, f.key);
let verdict = capture_one(f, import.as_deref(), run, &mut index);
let Captured {
candidate,
refs,
anchors,
} = match verdict {
Ok(ok) => ok,
Err(Rejected::Reason(reason)) => {
run.counts.reject(reason);
plan.dropped.push((label, reason));
continue;
}
Err(Rejected::Merge { index: i, refs }) => {
run.counts.reject(ReasonCode::NearDuplicateMerged);
plan.dropped.push((label, ReasonCode::NearDuplicateMerged));
if let Some(id) = held_ids.get(i) {
plan.merges.push((id.clone(), refs));
}
continue;
}
};
let statement = statement_of(&candidate);
let id = fact_id(&f.category, &f.key, &statement);
if !seen_ids.insert(id.clone()) {
run.counts.reject(ReasonCode::Duplicate);
plan.dropped.push((label, ReasonCode::Duplicate));
continue;
}
held_ids.push(id.clone());
let applies = humanize(&f.key);
let applies_to = if applies.is_empty() || applies.chars().count() > 500 {
Vec::new()
} else {
vec![applies]
};
let mut sources = vec![format!(
"lean-ctx:knowledge/{}/{}#{}",
slug(&f.category, 60),
slug(&f.key, 80),
short_hash(&[&f.category, &f.key, &statement])
)];
sources.extend(refs.iter().cloned());
run.counts.captured += 1;
plan.import_sources.extend(import);
plan.captured.push(capture::CapturedFact {
candidate: candidate.clone(),
refs,
category: f.category.clone(),
key: f.key.clone(),
value: f.value.clone(),
derived_from: Vec::new(),
session: (!imports && !f.source_session.trim().is_empty())
.then(|| f.source_session.trim().to_string()),
});
plan.added.push(FactV1 {
id,
statement,
kind: kind_of(candidate.node_type).to_string(),
applies_to,
sources,
from_contributions: Vec::new(),
anchors,
});
}
plan
}
enum Rejected {
Reason(ReasonCode),
Merge { index: usize, refs: Vec<String> },
}
impl From<ReasonCode> for Rejected {
fn from(reason: ReasonCode) -> Self {
Self::Reason(reason)
}
}
fn capture_one(
f: &KnowledgeFact,
import: Option<&str>,
run: &mut CaptureRun<'_>,
index: &mut dedupe::Index,
) -> Result<Captured, Rejected> {
let anchors_ok = f.anchors.iter().all(|a| {
run.screen
.check(&format!(
"{}\n{}",
a.path,
a.symbol.as_deref().unwrap_or_default()
))
.is_ok()
});
let mut shaped = f.clone();
if !anchors_ok {
shaped.anchors.clear();
}
let candidate = capture::shape(&shaped, import.and_then(import_reference))?;
let refs = match capture::gate(&candidate, &mut run.checkout) {
Ok(refs) => refs,
Err(reason) => {
if reason == ReasonCode::UnpublishedRef {
run.unpublished(f);
} else {
run.settled(f);
}
return Err(reason.into());
}
};
run.settled(f);
let mut texts = vec![
f.category.clone(),
f.key.clone(),
candidate.statement.clone(),
];
texts.extend(candidate.steps.iter().cloned());
texts.extend(candidate.commands.iter().map(|c| c.cmd.clone()));
texts.extend(refs.iter().cloned());
for text in &texts {
if lean_ctx::core::context_admission::carrier::admit_for_store_under(text, None)
.ok()
.is_none_or(|admitted| admitted != *text)
{
return Err(ReasonCode::Admission.into());
}
}
run.screen
.check(&texts.join("\n"))
.map_err(|b| ReasonCode::of_screen(&b))?;
let key = dedupe::exact_key(&run.pk_id, candidate.node_type, &candidate.statement);
let known = dedupe::Known {
node_type: candidate.node_type,
signature: dedupe::signature(&run.pk_id, &candidate.statement),
refs: refs.clone(),
};
match index.verdict(&key, &known) {
dedupe::Verdict::Duplicate => Err(ReasonCode::Duplicate.into()),
dedupe::Verdict::Merge { index: i, refs } => Err(Rejected::Merge { index: i, refs }),
dedupe::Verdict::New => {
index.insert(key, known);
Ok(Captured {
candidate,
refs,
anchors: shaped.anchors,
})
}
}
}
struct Captured {
candidate: capture::CaptureCandidate,
refs: Vec<String>,
anchors: Vec<lean_ctx::core::knowledge::anchors::CodeAnchor>,
}
pub fn knowledge_category(ns: &str, name: &str) -> String {
format!("cloud/{ns}/{name}")
}
pub use lean_ctx::core::knowledge::import_tag::{IMPORT_TAG_SCHEME, import_tag_source};
pub(crate) fn import_tag_prefix(ns: &str, name: &str) -> String {
format!("@{ns}/{name}@r")
}
fn pulled_fact_key(id: &str) -> String {
let bytes = id.as_bytes();
let strict = !bytes.is_empty()
&& bytes.len() <= 80
&& bytes[0].is_ascii_alphanumeric()
&& bytes
.iter()
.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'-'));
if strict {
id.to_string()
} else {
format!("fact-{}", short_hash(&[id]))
}
}
pub fn served_by_replica(ns: &str, name: &str) -> bool {
let Ok(store) = crate::CloudStore::open() else {
return false;
};
crate::vault::VaultMap::load(store.data_dir())
.vaults
.values()
.find(|r| r.ns == ns && r.name == name)
.is_some_and(|r| {
crate::replica::store::place_dir(store.data_dir(), &r.vault_id)
.join("replica.db")
.exists()
})
}
pub fn remove_pulled(project_root: &str, ns: &str, name: &str) -> Result<usize, String> {
let prefix = import_tag_prefix(ns, name);
let (_, removed) = ProjectKnowledge::mutate_locked(project_root, |k| {
let before = k.facts.len();
k.facts.retain(|f| {
!f.imported_from
.as_deref()
.and_then(import_tag_source)
.is_some_and(|source| source.starts_with(&prefix))
});
let removed = before - k.facts.len();
if removed > 0 {
k.rebuild_index();
}
removed
})?;
Ok(removed)
}
pub fn remove_all_pulled(project_root: &str) -> Result<usize, String> {
ProjectKnowledge::remove_facts_locked(project_root, |f| {
f.imported_from
.as_deref()
.and_then(import_tag_source)
.is_some()
})
}
pub fn sync_into_knowledge(
project_root: &str,
ns: &str,
name: &str,
revision: u64,
doc: &ResearchContextDocumentV1,
) -> Result<(usize, usize), String> {
if served_by_replica(ns, name) {
return Ok((0, remove_pulled(project_root, ns, name)?));
}
let policy = lean_ctx::core::memory_policy::MemoryPolicy::default();
let prefix = import_tag_prefix(ns, name);
let tag = format!("{IMPORT_TAG_SCHEME}{prefix}{revision}");
let category = knowledge_category(ns, name);
let (_, counts) = ProjectKnowledge::mutate_locked(project_root, |k| {
let before = k.facts.len();
k.facts.retain(|f| {
!f.imported_from
.as_deref()
.and_then(import_tag_source)
.is_some_and(|source| source.starts_with(&prefix))
});
let removed = before - k.facts.len();
if removed > 0 {
k.rebuild_index();
}
let mut loaded = 0usize;
for fact in &doc.facts {
let key = pulled_fact_key(&fact.id);
let value = crate::untrusted::wrap(
crate::untrusted::UntrustedKind::Knowledge,
&tag,
&fact.statement,
);
k.remember(&category, &key, &value, &tag, 0.8, &policy);
if let Some(last) = k
.facts
.iter_mut()
.rev()
.find(|f| f.category == category && f.key == key && f.is_current())
{
last.imported_from = Some(tag.clone());
last.anchors.clone_from(&fact.anchors);
loaded += 1;
}
}
(loaded, removed)
})?;
Ok(counts)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ids_and_kinds_are_stable() {
assert_eq!(
fact_id("decision", "db-choice", "Postgres"),
fact_id("decision", "db-choice", "Postgres")
);
assert_ne!(
fact_id("decision", "db-choice", "Postgres"),
fact_id("decision", "db-choice", "MySQL")
);
assert!(fact_id("decision", "DB Choice!", "x").starts_with("lc-db-choice-"));
assert_eq!(kind_of(NodeType::Decision), "decision");
assert_eq!(kind_of(NodeType::Finding), "observation");
}
#[test]
fn pulled_copies_of_a_vault_are_removed_and_nothing_else() {
let _data = lean_ctx::core::data_dir::isolated_data_dir();
let dir = tempfile::tempdir().unwrap();
let root = dir.path().to_string_lossy().into_owned();
let doc = |statement: &str| ResearchContextDocumentV1 {
facts: vec![FactV1 {
id: "f1".into(),
statement: statement.into(),
kind: "observation".into(),
applies_to: vec![],
sources: vec![],
from_contributions: vec![],
anchors: vec![],
}],
..ResearchContextDocumentV1::default()
};
sync_into_knowledge(&root, "acme", "pay", 3, &doc("Pay copies this claim.")).unwrap();
sync_into_knowledge(&root, "acme", "other", 1, &doc("Other vault claim here.")).unwrap();
let policy = lean_ctx::core::memory_policy::MemoryPolicy::default();
ProjectKnowledge::mutate_locked(&root, |k| {
k.remember("arch", "own", "My own claim stays here.", "s", 0.9, &policy);
})
.unwrap();
assert_eq!(remove_pulled(&root, "acme", "pay").unwrap(), 1);
let k = ProjectKnowledge::load(&root).unwrap();
let values: Vec<&str> = k.facts.iter().map(|f| f.value.as_str()).collect();
assert!(values.iter().any(|v| v.contains("Other vault claim")));
assert!(values.contains(&"My own claim stays here."));
assert!(!values.iter().any(|v| v.contains("Pay copies")));
assert_eq!(remove_pulled(&root, "acme", "pay").unwrap(), 0);
}
}