lean-ctx 3.12.5

LeanCTX | OSS: the open-source runtime for the Context Gateway for AI agents. CLI, MCP server, and supported proxy integrations.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// SPDX-License-Identifier: Apache-2.0
//! Import AI coding-session history into the project knowledge store.

use std::path::Path;

use lean_ctx::core::import;
use lean_ctx::core::knowledge::{KnowledgeFact, ProjectKnowledge};

pub fn cmd_import(args: &[String]) {
    let code = run(args);
    if code != 0 {
        std::process::exit(code);
    }
}

/// Exit status: 0 on success, 2 for a usage error, 1 when the import itself
/// fails (F9c: an error never exits 0). Per-source read warnings stay
/// warnings.
fn run(args: &[String]) -> i32 {
    if args
        .iter()
        .any(|arg| matches!(arg.as_str(), "--help" | "-h"))
    {
        print_help();
        return 0;
    }

    let dry_run = args.iter().any(|arg| arg == "--dry-run");
    let all = args.iter().any(|arg| arg == "--all");
    let limit_value = args
        .iter()
        .position(|arg| arg == "--limit")
        .and_then(|index| args.get(index + 1));
    let sources: Vec<&str> = args
        .iter()
        .filter(|arg| Some(*arg) != limit_value)
        .filter_map(|arg| (!arg.starts_with('-')).then_some(arg.as_str()))
        .collect();

    // The project's own sources (K1): `docs [paths…]` and `git [--limit N]`.
    let repo_source = matches!(sources.first().copied(), Some("docs" | "git"));
    if !repo_source && ((all && !sources.is_empty()) || sources.len() > 1) {
        eprintln!("Use one source (claude-code, codex, cursor, opencode, docs, git) or --all.");
        print_help();
        return 2;
    }

    let (label, mut result) = if repo_source {
        let project_root = match super::common::require_project_root() {
            Ok(root) => root,
            Err(error) => {
                eprintln!("Import failed: {error}");
                return 1;
            }
        };
        let root = std::path::Path::new(&project_root);
        if sources[0] == "docs" {
            let paths: Vec<std::path::PathBuf> =
                sources[1..].iter().map(std::path::PathBuf::from).collect();
            ("docs", import::markdown::import(root, &paths))
        } else {
            let limit = match limit_value.map(|value| value.parse::<usize>()) {
                None => import::git_history::DEFAULT_LIMIT,
                Some(Ok(limit)) if limit > 0 => limit,
                Some(_) => {
                    eprintln!("--limit takes a positive number of commits.");
                    return 2;
                }
            };
            ("git", import::git_history::import(root, limit))
        }
    } else if all {
        let mut result = import::claude_code::import();
        result.merge(import::codex::import());
        result.merge(import::cursor::import());
        result.merge(import::opencode::import());
        ("all sources", result)
    } else {
        match sources.first().copied() {
            Some("claude-code") => ("claude-code", import::claude_code::import()),
            Some("codex") => ("codex", import::codex::import()),
            Some("cursor") => ("cursor", import::cursor::import()),
            Some("opencode") => ("opencode", import::opencode::import()),
            _ => {
                eprintln!(
                    "Choose a source: claude-code, codex, cursor, opencode, docs, git, or --all."
                );
                print_help();
                return 2;
            }
        }
    };

    let extracted = result.facts_extracted;
    let persisted = if dry_run {
        0
    } else {
        let project_root = match super::common::require_project_root() {
            Ok(root) => root,
            Err(error) => {
                eprintln!("Import failed: {error}");
                return 1;
            }
        };
        let scope = result.repo_scope.take();
        match persist_facts(
            &project_root,
            std::mem::take(&mut result.facts),
            scope.as_ref(),
        ) {
            Ok(count) => count,
            Err(error) => {
                eprintln!("Import failed: {error}");
                return 1;
            }
        }
    };

    let summary = import_summary(dry_run, label, persisted, extracted, &result);
    super::common::print_released(&released_summary(summary));
    for error in &result.errors {
        eprintln!("Import warning: {error}");
    }
    if repo_source && !dry_run {
        queue_import_drafts();
    }
    0
}

/// A connected machine proposes what the repository import brought to the
/// project's Hive repo (queued only; `lean-ctx hive sync` or the
/// scheduler sends it). Counts only on stdout.
fn queue_import_drafts() {
    use lean_ctx_hive::HiveStore;
    use lean_ctx_hive::knowledge::{import_draft, screening::Screen};
    let Ok(store) = HiveStore::open() else {
        return;
    };
    if !matches!(store.instance(), Ok(Some(_))) {
        return;
    }
    let Ok(project_root) = super::common::require_project_root() else {
        return;
    };
    let config = lean_ctx::core::config::Config::load().hive.clone();
    let mut screen = Screen::for_installation();
    match import_draft::enqueue_import_drafts(
        &store,
        &config,
        Path::new(&project_root),
        "cli-import",
        &mut screen,
        chrono::Utc::now(),
    ) {
        Ok(ids) if !ids.is_empty() => println!(
            "Queued {} import draft(s) for Hive review (sent with `lean-ctx hive sync`).",
            ids.len()
        ),
        Ok(_) => {}
        Err(error) => eprintln!("Hive: import drafts not queued: {error}"),
    }
}

/// What the command reports on stdout. Counts and the source label only: a
/// fact that admission did not let into the store appears as a number
/// (`facts skipped`), never by its value; a history source that was not
/// read appears as a number (`sources skipped` — above the read cap or
/// unreadable; `sources refused` — outside every named root or withheld),
/// never by its path.
fn import_summary(
    dry_run: bool,
    label: &str,
    persisted: usize,
    extracted: usize,
    result: &import::ImportResult,
) -> String {
    let outcome = if dry_run {
        format!("[DRY RUN] {label}: no facts written")
    } else {
        format!("Imported {persisted} new fact(s) from {label}.")
    };
    format!(
        "{outcome}\nSessions: {}; facts extracted: {extracted}; facts skipped: {}; \
         sources skipped: {}; sources refused: {}; files touched: {}",
        result.sessions_found,
        result.facts_skipped,
        result.sources_skipped,
        result.sources_refused,
        result.files_touched
    )
}

/// The import summary is counts and the source label — a report for the
/// person at the terminal: released, never recorded as a delivery.
fn released_summary(summary: String) -> String {
    let root = super::common::detect_project_root(&[]);
    super::common::CliToolRun::new("ctx_knowledge", &root, None)
        .human_facing()
        .deliver_text(|| summary)
}

fn persist_facts(
    project_root: &str,
    facts: Vec<KnowledgeFact>,
    scope: Option<&import::RepoScope>,
) -> Result<usize, String> {
    let now = chrono::Utc::now();
    ProjectKnowledge::mutate_locked(project_root, |knowledge| {
        if let Some(scope) = scope {
            mark_removed_sections(knowledge, &facts, scope, Path::new(project_root), now);
        }
        let mut added = 0;
        // The facts arrive admitted for a store: `core::import` admits each
        // value where the fact is built, for every caller (E3).
        for fact in facts {
            let exists = knowledge.facts.iter().any(|existing| {
                existing.category == fact.category
                    && existing.value == fact.value
                    && existing.imported_from == fact.imported_from
            });
            if exists {
                // Found again: a section that was gone is back.
                for existing in knowledge.facts.iter_mut().filter(|existing| {
                    existing.key == fact.key && existing.imported_from == fact.imported_from
                }) {
                    clear_source_removed(existing);
                    if !fact.anchors.is_empty() {
                        existing.anchors.clone_from(&fact.anchors);
                    }
                }
                continue;
            }
            // A repository source (docs, git) anchors each fact (`key`): a
            // changed text at the same anchor updates that fact instead of
            // adding a second one next to it.
            let anchored = (!fact.key.is_empty())
                .then(|| {
                    knowledge.facts.iter_mut().find(|existing| {
                        existing.is_current()
                            && existing.key == fact.key
                            && existing.imported_from == fact.imported_from
                    })
                })
                .flatten();
            match anchored {
                Some(existing) => {
                    existing.category.clone_from(&fact.category);
                    existing.value = fact.value;
                    existing.archetype = fact.archetype;
                    existing.last_confirmed = fact.last_confirmed;
                    existing.revision_count += 1;
                    existing.anchors = fact.anchors;
                    clear_source_removed(existing);
                }
                None => knowledge.facts.push(fact),
            }
            added += 1;
        }
        added
    })
    .map(|(_, added)| added)
}

/// A docs fact whose file this run read (or, for a whole-project run, whose
/// file is gone) but whose anchor did not come back lost its section: it is
/// *to verify* (`source_removed`), still recalled, never deleted (K1).
fn mark_removed_sections(
    knowledge: &mut ProjectKnowledge,
    found: &[KnowledgeFact],
    scope: &import::RepoScope,
    root: &Path,
    now: chrono::DateTime<chrono::Utc>,
) {
    use lean_ctx::core::knowledge::{FactReviewReason, FactReviewState};
    let docs = Some(import::ImportSource::Docs.as_str());
    let keys: std::collections::HashSet<&str> = found
        .iter()
        .filter(|fact| fact.imported_from.as_deref() == docs)
        .map(|fact| fact.key.as_str())
        .collect();
    for fact in knowledge.facts.iter_mut().filter(|fact| {
        fact.is_current()
            && fact.imported_from.as_deref() == docs
            && !keys.contains(fact.key.as_str())
    }) {
        let Some((file, _)) = fact.key.split_once('#') else {
            continue;
        };
        let covered = scope.files.iter().any(|read| read == file)
            || (scope.whole && !root.join(file).exists());
        if covered {
            fact.mark_review(
                FactReviewState::ToVerify,
                FactReviewReason::SourceRemoved,
                now,
            );
        }
    }
}

fn clear_source_removed(fact: &mut KnowledgeFact) {
    if fact.review.as_ref().is_some_and(|review| {
        review.reason == lean_ctx::core::knowledge::FactReviewReason::SourceRemoved
    }) {
        fact.clear_review();
    }
}

fn print_help() {
    println!("Usage: lean-ctx import <claude-code|codex|cursor|opencode> [--dry-run]");
    println!("       lean-ctx import --all [--dry-run]");
    println!("       lean-ctx import docs [<file|dir>...] [--dry-run]");
    println!("         AGENTS.md, CLAUDE.md, .cursorrules, .cursor/rules and docs/ by default");
    println!(
        "       lean-ctx import git [--limit N] [--dry-run]   (reverts, fixes and decisions; default {})",
        import::git_history::DEFAULT_LIMIT
    );
}

#[cfg(test)]
mod tests {
    use lean_ctx::core::import::ImportSource;

    #[test]
    fn source_names_match_import_engine() {
        assert_eq!(ImportSource::ClaudeCode.as_str(), "claude-code");
        assert_eq!(ImportSource::Codex.as_str(), "codex");
        assert_eq!(ImportSource::Cursor.as_str(), "cursor");
        assert_eq!(ImportSource::OpenCode.as_str(), "opencode");
    }

    /// F9c: a usage error exits non-zero instead of printing and exiting 0.
    #[test]
    fn a_usage_error_exits_non_zero() {
        let args = |list: &[&str]| list.iter().map(ToString::to_string).collect::<Vec<_>>();
        assert_eq!(super::run(&args(&[])), 2, "no source");
        assert_eq!(super::run(&args(&["codex", "cursor"])), 2, "two sources");
        assert_eq!(super::run(&args(&["nowhere"])), 2, "unknown source");
        assert_eq!(super::run(&args(&["--help"])), 0);
    }

    use crate::cli::common::carrier_tests_support::{AWS_KEY, governed, lineage};

    /// The import summary is counts and a label: released, never registered.
    #[test]
    fn the_import_summary_is_human_facing() {
        let _data = lean_ctx::core::data_dir::isolated_data_dir();
        let _community = lean_ctx::core::policy::runtime::TestPolicyOverride::set(None);
        governed();
        let root = crate::cli::common::detect_project_root(&[]);
        let summary =
            "Imported 3 new fact(s) from codex.\nSessions: 2; facts extracted: 5; files touched: 4";
        let printed = super::released_summary(summary.to_string());
        assert_eq!(printed, summary, "the summary keeps its bytes");
        assert_eq!(lineage(&root, &printed), None);
        let leaked = super::released_summary(format!("Imported 1 new fact(s) from {AWS_KEY}."));
        assert!(!leaked.contains(AWS_KEY), "{leaked}");
    }

    /// End to end through the shared import and the CLI's store step: a
    /// clean fact is stored with its bytes, a key-shaped value is never
    /// stored raw, and the facts the store receives are the admitted ones.
    #[test]
    fn imported_facts_reach_the_store_admitted() {
        let _data = lean_ctx::core::data_dir::isolated_data_dir();
        let _community = lean_ctx::core::policy::runtime::TestPolicyOverride::set(None);
        let history = tempfile::tempdir().expect("history dir");
        let history_root = history.path().canonicalize().expect("history root");
        let session_dir = history_root.join("project-a");
        std::fs::create_dir_all(&session_dir).expect("session dir");
        let line = |text: &str| {
            serde_json::json!({ "message": { "role": "assistant", "content": text } }).to_string()
        };
        std::fs::write(
            session_dir.join("session.jsonl"),
            format!(
                "{}\n{}\n",
                line("We decided to use JWT RS256 for the session tokens."),
                line(&format!(
                    "We decided to deploy with the key {AWS_KEY} today."
                )),
            ),
        )
        .expect("transcript");
        let mut result = lean_ctx::core::import::claude_code::import_from_root(&history_root);
        assert!(result.errors.is_empty(), "{:?}", result.errors);
        assert!(
            result
                .facts
                .iter()
                .all(|fact| !fact.value.contains(AWS_KEY))
        );

        // The summary states counts only.
        let summary =
            super::import_summary(false, "claude-code", 1, result.facts_extracted, &result);
        assert!(
            summary.contains(&format!("facts skipped: {}", result.facts_skipped)),
            "{summary}"
        );
        assert!(!summary.contains(AWS_KEY), "{summary}");
        assert!(!summary.contains("JWT"), "{summary}");

        // Sources that were not read are counts too — no path of theirs.
        let refused = lean_ctx::core::import::ImportResult {
            sources_refused: 2,
            sources_skipped: 1,
            ..Default::default()
        };
        let summary = super::import_summary(true, "opencode", 0, 0, &refused);
        assert_eq!(
            summary,
            "[DRY RUN] opencode: no facts written\nSessions: 0; facts extracted: 0; \
             facts skipped: 0; sources skipped: 1; sources refused: 2; files touched: 0"
        );

        let project = tempfile::tempdir().expect("project dir");
        let root = project.path().to_string_lossy().into_owned();
        let facts = std::mem::take(&mut result.facts);
        let offered = facts.len();
        let added = super::persist_facts(&root, facts, None).expect("persist");
        assert_eq!(added, offered, "one store step, no second filter");
        let stored = lean_ctx::core::knowledge::ProjectKnowledge::load(&root).expect("stored");
        assert!(
            stored
                .facts
                .iter()
                .any(|fact| fact.value.contains("JWT RS256")),
            "a clean fact is stored"
        );
        assert!(
            stored
                .facts
                .iter()
                .all(|fact| !fact.value.contains(AWS_KEY)),
            "a masked value is never stored raw"
        );
    }

    /// K1 + J2: an imported section is anchored; a section that disappears
    /// leaves its facts *to verify* (`source_removed`), and they are current
    /// again when it comes back.
    #[test]
    fn a_removed_section_leaves_its_facts_to_verify_until_it_returns() {
        use lean_ctx::core::knowledge::{FactReviewReason, FactReviewState, ProjectKnowledge};
        let _data = lean_ctx::core::data_dir::isolated_data_dir();
        let _community = lean_ctx::core::policy::runtime::TestPolicyOverride::set(None);
        let project = tempfile::tempdir().unwrap();
        let root = project.path().canonicalize().unwrap();
        let root_text = root.to_string_lossy().into_owned();
        let agents = root.join("AGENTS.md");
        let both = "# Agents\n\n## Conventions\n\n- Run cargo fmt before every commit.\n\n\
                    ## Gotchas\n\n- Never run lean-ctx stop before a build.\n";
        let import = |root: &std::path::Path| {
            let mut result = lean_ctx::core::import::markdown::import(root, &[]);
            let scope = result.repo_scope.take();
            super::persist_facts(
                &root_text,
                std::mem::take(&mut result.facts),
                scope.as_ref(),
            )
            .unwrap();
        };
        let fact = |needle: &str| {
            ProjectKnowledge::load(&root_text)
                .unwrap()
                .facts
                .into_iter()
                .find(|fact| fact.value.contains(needle))
                .unwrap()
        };
        std::fs::write(&agents, both).unwrap();
        import(&root);
        let gotcha = fact("lean-ctx stop");
        assert_eq!(gotcha.key, "AGENTS.md#agents/gotchas/1");
        assert_eq!(gotcha.anchors.len(), 1);
        assert_eq!(gotcha.anchors[0].symbol.as_deref(), Some("agents/gotchas"));
        assert_eq!(gotcha.review, None);

        std::fs::write(
            &agents,
            "# Agents\n\n## Conventions\n\n- Run cargo fmt before every commit.\n",
        )
        .unwrap();
        import(&root);
        let review = fact("lean-ctx stop").review.expect("marked");
        assert_eq!(
            (review.state, review.reason),
            (FactReviewState::ToVerify, FactReviewReason::SourceRemoved)
        );
        assert_eq!(
            fact("cargo fmt").review,
            None,
            "the kept section stays current"
        );

        std::fs::write(&agents, both).unwrap();
        import(&root);
        assert_eq!(fact("lean-ctx stop").review, None, "back again: current");

        std::fs::remove_file(&agents).unwrap();
        import(&root);
        assert!(
            fact("cargo fmt").review.is_some(),
            "a removed file loses all its facts"
        );
    }
}