lean-ctx-engine 3.12.2

LeanCTX | OSS library: the open-source runtime for the Context Gateway for AI agents behind the lean-ctx CLI, MCP server and 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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// SPDX-License-Identifier: Apache-2.0
use super::*;
use crate::core::policy::runtime::{self, TestPolicyOverride};

fn policy() -> TestPolicyOverride {
    TestPolicyOverride::set(Some(crate::core::policy::load(
        "name='archive-test'\nversion='1.0.0'\ndescription='test'\n[filters]\nclassification='block'\nblocked_labels=['CONFIDENTIAL']\n",
    ).unwrap()))
}

fn scope<T>(root: &std::path::Path, operation: impl FnOnce() -> T) -> T {
    runtime::REQUEST_PROJECT
        .sync_scope(std::cell::RefCell::new(Some(root.to_path_buf())), operation)
}

fn publish(root: &std::path::Path, path: &std::path::Path, output: &str) -> String {
    scope(root, || {
        runtime::with_source_view(|| {
            let mut remaining = crate::core::limits::max_read_bytes();
            let read = crate::core::rooted_read::read_file_for_tool_rooted_with_path(
                path.to_str().unwrap(),
                root.to_str().unwrap(),
                "ctx_execute",
                &mut remaining,
            )
            .unwrap();
            let authority = authority::ArchiveAuthority::file(
                root,
                &read.canonical_path,
                "ctx_execute",
                &read.content,
            )
            .unwrap();
            store_with_authority(
                "ctx_execute",
                path.to_str().unwrap(),
                output,
                None,
                Some(&authority),
            )
            .unwrap()
            .id
        })
        .unwrap()
    })
}

#[test]
fn file_archive_rechecks_source_bytes_and_allows_identical_restoration() {
    let _data = crate::core::data_dir::isolated_data_dir();
    let _policy = policy();
    let root = tempfile::tempdir().unwrap();
    let path = root.path().join("source.txt");
    std::fs::write(&path, "PUBLIC source").unwrap();
    let id = publish(root.path(), &path, "archived original payload");
    let original = std::fs::read(content_path(&id)).unwrap();
    scope(root.path(), || {
        assert_eq!(retrieve(&id).as_deref(), Some("archived original payload"));
        for source in ["CONFIDENTIAL\nsource", "different PUBLIC source"] {
            std::fs::write(&path, source).unwrap();
            assert!(retrieve(&id).is_none());
            assert!(list_entries(None).is_empty());
            assert!(crate::core::archive_fts::search("payload", 10).is_empty());
        }
        std::fs::remove_file(&path).unwrap();
        assert!(retrieve(&id).is_none());
        std::fs::write(&path, "PUBLIC source").unwrap();
        assert_eq!(retrieve(&id).as_deref(), Some("archived original payload"));
        assert_eq!(list_entries(None).len(), 1);
        let found = crate::core::archive_fts::search("payload", 10);
        assert_eq!(found.len(), 1);
        assert!(found[0].snippet.contains("payload"));
    });
    assert_eq!(std::fs::read(content_path(&id)).unwrap(), original);
}

#[test]
fn identical_output_does_not_rebind_another_projects_archive() {
    let _data = crate::core::data_dir::isolated_data_dir();
    let _policy = policy();
    let a = tempfile::tempdir().unwrap();
    let b = tempfile::tempdir().unwrap();
    let pa = a.path().join("source.txt");
    let pb = b.path().join("source.txt");
    std::fs::write(&pa, "same source").unwrap();
    std::fs::write(&pb, "same source").unwrap();
    let aid = publish(a.path(), &pa, "same payload");
    scope(b.path(), || assert!(retrieve(&aid).is_none()));
    let bid = publish(b.path(), &pb, "same payload");
    assert_ne!(aid, bid);
    scope(a.path(), || {
        assert_eq!(retrieve(&aid).as_deref(), Some("same payload"));
        assert!(retrieve(&bid).is_none());
        assert_eq!(list_entries(None).len(), 1);
        assert_eq!(crate::core::archive_fts::search("payload", 10).len(), 1);
    });
    scope(b.path(), || {
        assert_eq!(retrieve(&bid).as_deref(), Some("same payload"));
    });
}

#[test]
fn legacy_recovery_is_preserved_without_policy_and_withheld_under_policy() {
    let _data = crate::core::data_dir::isolated_data_dir();
    let root = tempfile::tempdir().unwrap();
    let id;
    {
        let _policy = TestPolicyOverride::set(None);
        id = store("ctx_shell", "legacy command", "legacy payload", None).unwrap();
        assert_eq!(retrieve(&id).as_deref(), Some("legacy payload"));
    }
    let bytes = std::fs::read(content_path(&id)).unwrap();
    {
        let _policy = policy();
        scope(root.path(), || {
            assert!(retrieve(&id).is_none());
            assert!(list_entries(None).is_empty());
            assert!(crate::core::archive_fts::search("payload", 10).is_empty());
            assert!(store("ctx_shell", "unbound", "new payload", None).is_none());
        });
    }
    let _policy = TestPolicyOverride::set(None);
    assert_eq!(retrieve(&id).as_deref(), Some("legacy payload"));
    assert_eq!(std::fs::read(content_path(&id)).unwrap(), bytes);
}

#[test]
fn bound_archive_rejects_corruption_and_untrusted_archive_ids() {
    let _data = crate::core::data_dir::isolated_data_dir();
    let _policy = policy();
    let root = tempfile::tempdir().unwrap();
    let path = root.path().join("source.txt");
    std::fs::write(&path, "public source").unwrap();
    let id = publish(root.path(), &path, "admitted output");
    std::fs::write(content_path(&id), "substituted output").unwrap();
    scope(root.path(), || {
        assert!(retrieve(&id).is_none());
        for invalid in ["../source", "é", "/tmp/file", "ffff/../../foo"] {
            assert!(retrieve(invalid).is_none());
        }
    });
}

#[test]
fn bound_namespace_never_downgrades_when_metadata_is_lost() {
    let _data = crate::core::data_dir::isolated_data_dir();
    let root = tempfile::tempdir().unwrap();
    let path = root.path().join("source.txt");
    std::fs::write(&path, "public source").unwrap();
    let id = {
        let _policy = policy();
        publish(root.path(), &path, "private archive marker")
    };
    let mut entry = read_entry(&id).unwrap();
    let original = std::fs::read(meta_path(&id)).unwrap();
    let _policy = TestPolicyOverride::set(None);
    scope(root.path(), || {
        assert!(retrieve(&id).is_some());
        let mut unsupported = serde_json::to_value(&entry).unwrap();
        unsupported["authority"]["version"] = serde_json::json!(255);
        std::fs::write(meta_path(&id), serde_json::to_vec(&unsupported).unwrap()).unwrap();
        assert!(retrieve(&id).is_none());
        entry.authority = None;
        write_metadata(&entry).unwrap();
        assert!(retrieve(&id).is_none());
        assert!(list_entries(None).is_empty());
        std::fs::write(meta_path(&id), "{broken").unwrap();
        assert!(retrieve(&id).is_none());
        std::fs::remove_file(meta_path(&id)).unwrap();
        assert!(retrieve(&id).is_none());
        assert!(crate::core::archive_fts::search("marker", 10).is_empty());
        std::fs::write(meta_path(&id), original).unwrap();
        assert!(retrieve(&id).is_some());
    });
}

#[test]
fn bound_discovery_does_not_limit_legacy_community_archives() {
    let _data = crate::core::data_dir::isolated_data_dir();
    let root = tempfile::tempdir().unwrap();
    let other = tempfile::tempdir().unwrap();
    let path = root.path().join("source.txt");
    std::fs::write(&path, "public source").unwrap();
    let id = {
        let _policy = policy();
        publish(root.path(), &path, "boundmarker payload")
    };
    let _policy = TestPolicyOverride::set(None);
    let mut legacy = Vec::new();
    scope(root.path(), || {
        for n in 0..140 {
            legacy.push(
                store(
                    "ctx_shell",
                    "legacy",
                    &format!("legacymarker payload {n}"),
                    None,
                )
                .unwrap(),
            );
        }
    });
    // Simulate the obsolete candidate's derived FTS rows, even if its metadata
    // subsequently disappears. Search must remove their ranking/snippet input.
    let db = rusqlite::Connection::open(archive_base_dir().join("index.db")).unwrap();
    db.execute(
        "INSERT INTO archive_meta VALUES (?1,'ctx_execute','private path','2026-01-01')",
        [&id],
    )
    .unwrap();
    db.execute("INSERT INTO archive_fts (archive_id,tool,command,content) VALUES (?1,'ctx_execute','private path','boundmarker payload')", [&id]).unwrap();
    // Even attributed to this project, the obsolete row is never served.
    db.execute(
        "INSERT INTO archive_project (archive_id, project) VALUES (?1, ?2)",
        rusqlite::params![
            id,
            crate::core::project_hash::hash_project_root(&root.path().to_string_lossy())
        ],
    )
    .unwrap();
    scope(root.path(), || {
        let entries = list_entries(None);
        assert_eq!(entries.len(), 141);
        let found = crate::core::archive_fts::search("legacymarker", 200);
        assert_eq!(found.len(), 140);
        assert!(found.iter().any(|entry| entry.archive_id == legacy[0]));
        assert_eq!(crate::core::archive_fts::entry_count(), 141);
        let mixed = crate::core::archive_fts::search("payload", 2);
        assert_eq!(mixed.len(), 2);
        assert_eq!(mixed[0].archive_id, id);
        assert!(legacy.contains(&mixed[1].archive_id));
        assert_eq!(
            crate::core::archive_fts::search("boundmarker", 200).len(),
            1
        );
    });
    // Community archives belong to the project that stored them (AP05-G4).
    scope(other.path(), || {
        let entries = list_entries(None);
        assert!(!entries.iter().any(|entry| entry.id == id));
        assert!(entries.is_empty());
        assert!(crate::core::archive_fts::search("legacymarker", 200).is_empty());
        assert!(crate::core::archive_fts::search("boundmarker", 200).is_empty());
        assert_eq!(crate::core::archive_fts::entry_count(), 0);
    });
    assert_eq!(
        db.query_row(
            "SELECT COUNT(*) FROM archive_fts WHERE length(archive_id)=64",
            [],
            |row| row.get::<_, i64>(0)
        )
        .unwrap(),
        0
    );
    db.execute_batch("BEGIN IMMEDIATE").unwrap();
    scope(root.path(), || {
        assert_eq!(
            crate::core::archive_fts::search("legacymarker", 200).len(),
            140
        );
        assert_eq!(crate::core::archive_fts::entry_count(), 141);
    });
    db.execute_batch("ROLLBACK").unwrap();
    assert!(content_path(&id).exists());
}

#[test]
fn known_alias_is_authorized_independently_of_discovery_limit() {
    let _data = crate::core::data_dir::isolated_data_dir();
    let _policy = policy();
    let root = tempfile::tempdir().unwrap();
    let path = root.path().join("source.txt");
    std::fs::write(&path, "public source").unwrap();
    let oldest = publish(root.path(), &path, "oldest payload");
    let mut entry = read_entry(&oldest).unwrap();
    entry.aliases.push("oldest_job".into());
    write_metadata(&entry).unwrap();
    for n in 0..130 {
        publish(root.path(), &path, &format!("new payload {n}"));
    }
    scope(root.path(), || {
        assert_eq!(list_entries(None).len(), 128);
        assert_eq!(
            resolve_alias("oldest_job").as_deref(),
            Some(oldest.as_str())
        );
        std::fs::remove_file(&path).unwrap();
        assert!(resolve_alias("oldest_job").is_none());
    });
}

#[test]
fn oversized_discovery_entry_does_not_hide_smaller_older_entries() {
    let _data = crate::core::data_dir::isolated_data_dir();
    let _policy = policy();
    let root = tempfile::tempdir().unwrap();
    let path = root.path().join("source.txt");
    std::fs::write(&path, "public source").unwrap();
    // Sizes derive from the real bounds (the test build caps an archive at
    // 1 MiB): newer entries leave less room than `skipped` needs, while the
    // small oldest entry still fits behind it.
    let budget = crate::core::policy::content::MAX_PROTECTED_CONTENT_BYTES;
    let newer_size = MAX_ARCHIVE_SIZE * 9 / 10;
    let newer_count = budget / newer_size;
    let remaining = budget - newer_count * newer_size;
    assert!(
        remaining < MAX_ARCHIVE_SIZE && remaining > 64,
        "test premise"
    );
    let oldest = publish(root.path(), &path, "small oldest payload");
    let skipped = publish(root.path(), &path, &"b".repeat(MAX_ARCHIVE_SIZE));
    let newest: Vec<String> = (0..newer_count)
        .map(|n| {
            publish(
                root.path(),
                &path,
                &format!("{n:08}{}", "a".repeat(newer_size - 8)),
            )
        })
        .collect();
    scope(root.path(), || {
        let ids: Vec<_> = list_entries(None)
            .into_iter()
            .map(|entry| entry.id)
            .collect();
        assert!(ids.contains(&oldest));
        for id in &newest {
            assert!(ids.contains(id));
        }
        assert!(!ids.contains(&skipped));
        assert!(retrieve(&skipped).is_some());
    });
}

#[test]
fn source_checks_share_the_original_byte_budget() {
    let _data = crate::core::data_dir::isolated_data_dir();
    let _policy = policy();
    let root = tempfile::tempdir().unwrap();
    let path = root.path().join("source.txt");
    std::fs::write(&path, "four").unwrap();
    let path = std::fs::canonicalize(path).unwrap();
    let authority = authority::ArchiveAuthority::file(
        root.path(),
        path.to_str().unwrap(),
        "ctx_execute",
        "four",
    )
    .unwrap();
    scope(root.path(), || {
        let mut remaining = 8;
        assert!(authority.admitted_budgeted(&mut remaining));
        assert_eq!(remaining, 4);
        assert!(authority.admitted_budgeted(&mut remaining));
        assert_eq!(remaining, 0);
        assert!(!authority.admitted_budgeted(&mut remaining));
    });
}

fn governed_without_pack() -> crate::core::data_dir::IsolatedDataDir {
    let data = crate::core::data_dir::isolated_data_dir();
    let dir = crate::core::paths::config_dir_read_only().unwrap();
    std::fs::create_dir_all(&dir).unwrap();
    std::fs::write(
        dir.join("config.toml"),
        "[context_gateway]\nmode = \"governed\"\n",
    )
    .unwrap();
    data
}

fn source_authority(root: &std::path::Path, path: &std::path::Path) -> authority::ArchiveAuthority {
    let mut remaining = crate::core::limits::max_read_bytes();
    let read = crate::core::rooted_read::read_file_for_tool_rooted_with_path(
        path.to_str().unwrap(),
        root.to_str().unwrap(),
        "ctx_read",
        &mut remaining,
    )
    .unwrap();
    authority::ArchiveAuthority::file(root, &read.canonical_path, "ctx_read", &read.content)
        .expect("governed mode binds sources without a policy pack")
}

/// No project pack, global gateway governed. A neutral excerpt of A and B (no
/// marking, no secret pattern) is stored as a source-bound recovery copy and
/// as a reference. After A is classified, revoked by a rule, or deleted, EVERY
/// copy is withheld — archive retrieval, the content lookup, the listing, the
/// reference — although the stored text never changes and B stays allowed.
/// Each retrieval reads the store from disk again, as a new process would.
///
/// Before: without a pack no authority was captured at all, and a result of
/// two sources was authorized by whichever source was captured last.
#[test]
fn every_copy_of_a_two_source_excerpt_is_withheld_when_one_source_loses_its_rights() {
    let _data = governed_without_pack();
    let community = TestPolicyOverride::set(None);
    let project = tempfile::tempdir().unwrap();
    let root = project.path().canonicalize().unwrap();
    let (path_a, path_b) = (root.join("a.md"), root.join("b.md"));
    // `CONFIDENTIAL` as a banner classifies A as confidential: storable.
    let original_a = "CONFIDENTIAL\nalpha is exported\n";
    std::fs::write(&path_a, original_a).unwrap();
    std::fs::write(&path_b, "beta is exported\n").unwrap();
    let excerpt = "alpha and beta are exported";
    let only_b = "beta is exported, nothing else";

    // The reference store never parks behind its lock; other tests share it.
    let reference = |text: &str, authority: &authority::ArchiveAuthority| {
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2);
        loop {
            let stored = crate::core::reference_store::store_with_authority(text, Some(authority));
            if stored.is_some() || std::time::Instant::now() >= deadline {
                return stored.expect("reference stored");
            }
            std::thread::sleep(std::time::Duration::from_millis(1));
        }
    };
    let (both_id, b_id, both_ref, b_ref) = scope(&root, || {
        let mut both = source_authority(&root, &path_a);
        both.join(source_authority(&root, &path_b));
        let b = source_authority(&root, &path_b);
        (
            store_recovery_copy("ctx_read", "excerpt", excerpt, &both).expect("stored"),
            store_recovery_copy("ctx_read", "b", only_b, &b).expect("stored"),
            reference(excerpt, &both),
            reference(only_b, &b),
        )
    });
    assert!(
        store("ctx_read", "originless", "no source known", None).is_none(),
        "content without a known source keeps no recovery copy under protection"
    );
    assert_eq!(
        bound_recovery_id(excerpt).as_deref(),
        Some(both_id.as_str())
    );

    let served = |expected: bool, case: &str| {
        scope(&root, || {
            assert_eq!(retrieve(&both_id).is_some(), expected, "{case}: archive");
            assert_eq!(
                bound_recovery_id(excerpt)
                    .and_then(|id| retrieve_checked(&id).ok())
                    .is_some(),
                expected,
                "{case}: content lookup"
            );
            assert_eq!(
                list_entries(None).iter().any(|entry| entry.id == both_id),
                expected,
                "{case}: listing"
            );
            assert_eq!(
                crate::core::reference_store::resolve(&both_ref).is_some(),
                expected,
                "{case}: reference"
            );
            // B alone stays readable through its own single-source copies.
            assert_eq!(
                retrieve(&b_id).as_deref(),
                Some(only_b),
                "{case}: B archive"
            );
            assert_eq!(
                crate::core::reference_store::resolve(&b_ref).as_deref(),
                Some(only_b),
                "{case}: B reference"
            );
        });
    };
    served(true, "authorized");
    scope(&root, || {
        assert_eq!(
            retrieve(&both_id).as_deref(),
            Some(excerpt),
            "byte-identical"
        );
    });

    // A is reclassified after the write.
    std::fs::write(&path_a, format!("*** TOP SECRET ***\n{original_a}")).unwrap();
    served(false, "A classified");
    std::fs::write(&path_a, original_a).unwrap();
    served(true, "A restored");

    // A is revoked by a rule: a pack that blocks its label is activated.
    drop(community);
    {
        let _pack = policy();
        served(false, "A revoked by rule");
    }
    let _community = TestPolicyOverride::set(None);
    served(true, "rule removed");

    // A is deleted.
    std::fs::remove_file(&path_a).unwrap();
    served(false, "A deleted");
    let refusal = scope(&root, || retrieve_checked(&both_id)).expect_err("withheld");
    assert!(
        matches!(refusal, ArchiveResolveError::Refused(reason) if !reason.contains("alpha")),
        "typed, content-free refusal"
    );
}

#[test]
fn invalid_policy_does_not_fall_back_to_legacy_discovery_or_storage() {
    let _data = crate::core::data_dir::isolated_data_dir();
    let _policy = TestPolicyOverride::set(None);
    let root = tempfile::tempdir().unwrap();
    let id = store("ctx_shell", "legacy command", "legacy payload", None).unwrap();
    std::fs::create_dir(root.path().join(".lean-ctx")).unwrap();
    std::fs::write(root.path().join(".lean-ctx/policy.toml"), "[invalid").unwrap();
    scope(root.path(), || {
        assert!(retrieve(&id).is_none());
        assert!(list_entries(None).is_empty());
        assert!(crate::core::archive_fts::search("payload", 10).is_empty());
        assert_eq!(crate::core::archive_fts::entry_count(), 0);
        assert!(store_background("ctx_shell", "shell_invalid", "new payload", None).is_none());
    });
    assert_eq!(
        std::fs::read_to_string(content_path(&id)).unwrap(),
        "legacy payload"
    );
}