lean-ctx 3.9.5

Context Runtime for AI Agents with CCP. 71 MCP tools, 10 read modes, 95+ compression patterns, cross-session memory (CCP), persistent AI knowledge with temporal facts + contradiction detection, multi-agent context sharing, LITM-aware positioning, AAAK compact format, adaptive compression with Thompson Sampling bandits. Supports 24+ AI tools. Reduces LLM token consumption by up to 99%.
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
526
527
528
//! Conformance & reproducibility scorecard (`conformance-v1`, EPIC 12.17).
//!
//! A self-check any user or CI can run to prove this instance honors its own
//! contracts and that its extension surface behaves. It exercises three areas:
//!
//! * **contracts** — every machine-verified contract version is present.
//! * **reproducibility** — the public discovery documents (`/v1/capabilities`,
//!   `/v1/openapi.json`) are deterministic (same bytes across two builds).
//! * **extensions** — every registered compressor / chunker / read-mode in the
//!   [`extension_registry`](super::extension_registry) satisfies the stable
//!   invariants the engine relies on (determinism, budget honoring, coverage).
//!
//! The output is a [`Scorecard`]: a flat list of [`Check`]s plus a pass count.
//! It is data, not prose, so it can be rendered (CLI), shared (JSON), or gated
//! on (`all_passed()` in `tests/conformance_suite.rs`).

use serde::Serialize;
use serde_json::{Value, json};

/// A representative corpus the extension invariants run against. Mixes blank
/// lines, multibyte UTF-8, and paragraph boundaries to stress edge cases.
const CORPUS: &[&str] = &[
    "",
    "single line",
    "a\n\n\n\nb  \n",
    "para one\n\npara two\n\n\npara three",
    "mültibyte ä ö ü 漢字 \n\n end",
];

/// One conformance check result.
#[derive(Debug, Clone, Serialize)]
pub struct Check {
    pub name: String,
    pub category: String,
    pub passed: bool,
    pub detail: String,
}

impl Check {
    fn pass(category: &str, name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            category: category.to_string(),
            passed: true,
            detail: String::new(),
        }
    }

    fn fail(category: &str, name: impl Into<String>, detail: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            category: category.to_string(),
            passed: false,
            detail: detail.into(),
        }
    }

    fn from_bool(category: &str, name: impl Into<String>, ok: bool, fail_detail: &str) -> Self {
        if ok {
            Self::pass(category, name)
        } else {
            Self::fail(category, name, fail_detail)
        }
    }
}

/// The full result of a conformance run.
#[derive(Debug, Clone, Serialize)]
pub struct Scorecard {
    pub version: u32,
    pub checks: Vec<Check>,
}

impl Scorecard {
    #[must_use]
    pub fn passed(&self) -> usize {
        self.checks.iter().filter(|c| c.passed).count()
    }

    #[must_use]
    pub fn total(&self) -> usize {
        self.checks.len()
    }

    #[must_use]
    pub fn all_passed(&self) -> bool {
        self.checks.iter().all(|c| c.passed)
    }

    #[must_use]
    pub fn failures(&self) -> Vec<&Check> {
        self.checks.iter().filter(|c| !c.passed).collect()
    }

    #[must_use]
    pub fn to_json(&self) -> Value {
        json!({
            "version": self.version,
            "passed": self.passed(),
            "total": self.total(),
            "all_passed": self.all_passed(),
            "checks": self.checks,
        })
    }
}

/// Run the full conformance suite against this instance.
#[must_use]
pub fn run() -> Scorecard {
    let mut checks = Vec::new();
    checks.extend(contract_checks());
    checks.extend(reproducibility_checks());
    checks.extend(extension_checks());
    checks.extend(accuracy_checks());
    checks.extend(a2a_checks());
    Scorecard { version: 1, checks }
}

// ---------------------------------------------------------------------------
// A2A: agent card and JSON-RPC contract conformance (GL#449).
// ---------------------------------------------------------------------------

/// Fields the A2A spec requires on a published agent card.
const A2A_CARD_REQUIRED: &[&str] = &[
    "name",
    "description",
    "version",
    "protocolVersion",
    "capabilities",
    "skills",
    "defaultInputModes",
    "defaultOutputModes",
    "authentication",
];

fn a2a_checks() -> Vec<Check> {
    let mut checks = Vec::new();

    let card = crate::core::a2a::agent_card::build_agent_card("conformance");
    let missing: Vec<&&str> = A2A_CARD_REQUIRED
        .iter()
        .filter(|f| card.get(**f).is_none())
        .collect();
    checks.push(Check::from_bool(
        "a2a",
        "agent_card_required_fields",
        missing.is_empty(),
        &format!("agent card missing fields: {missing:?}"),
    ));

    checks.push(Check::from_bool(
        "a2a",
        "agent_card_deterministic",
        card == crate::core::a2a::agent_card::build_agent_card("conformance"),
        "two agent card builds differ",
    ));

    let skills_ok = card
        .get("skills")
        .and_then(serde_json::Value::as_array)
        .is_some_and(|skills| {
            !skills.is_empty()
                && skills.iter().all(|s| {
                    s.get("id").is_some()
                        && s.get("name").is_some()
                        && s.get("description").is_some()
                })
        });
    checks.push(Check::from_bool(
        "a2a",
        "agent_card_skills_complete",
        skills_ok,
        "skills missing id/name/description",
    ));

    // JSON-RPC error contract: wrong version → -32600, unknown method → -32601.
    let bad_version = crate::core::a2a::a2a_compat::handle_a2a_jsonrpc(
        &crate::core::a2a::a2a_compat::JsonRpcRequest {
            jsonrpc: "1.0".to_string(),
            id: serde_json::Value::Number(1.into()),
            method: "tasks/get".to_string(),
            params: serde_json::Value::Null,
        },
    );
    checks.push(Check::from_bool(
        "a2a",
        "jsonrpc_rejects_bad_version",
        bad_version.error.as_ref().is_some_and(|e| e.code == -32600),
        "jsonrpc 1.0 not rejected with -32600",
    ));

    let unknown_method = crate::core::a2a::a2a_compat::handle_a2a_jsonrpc(
        &crate::core::a2a::a2a_compat::JsonRpcRequest {
            jsonrpc: "2.0".to_string(),
            id: serde_json::Value::Number(2.into()),
            method: "tasks/nonexistent".to_string(),
            params: serde_json::Value::Null,
        },
    );
    checks.push(Check::from_bool(
        "a2a",
        "jsonrpc_unknown_method_code",
        unknown_method
            .error
            .as_ref()
            .is_some_and(|e| e.code == -32601),
        "unknown method not rejected with -32601",
    ));

    checks
}

// ---------------------------------------------------------------------------
// Accuracy: structural invariants of the lossy read modes (GL#441).
//
// Byte-golden snapshots would break on every intentional format improvement;
// these checks instead pin down what each mode must *preserve* (symbols,
// deps) and must *drop* (bodies), plus determinism and size bounds — the
// properties an agent's correctness actually depends on.
// ---------------------------------------------------------------------------

/// A stable Rust fixture exercising pub fns, a struct, imports, and a body
/// secret that lossy modes must strip.
const ACCURACY_FIXTURE: &str = r"use std::collections::HashMap;
use std::path::PathBuf;

pub struct Inventory {
    items: HashMap<String, u32>,
}

pub fn add_item(inv: &mut Inventory, name: &str, qty: u32) {
    let body_secret_alpha = qty + 1;
    inv.items.insert(name.to_string(), body_secret_alpha);
}

pub fn total_count(inv: &Inventory) -> u32 {
    let body_secret_beta: u32 = inv.items.values().sum();
    body_secret_beta
}

fn internal_rebalance(inv: &mut Inventory) {
    inv.items.retain(|_, qty| *qty > 0);
}
";

/// Symbols every lossy structural mode must keep visible.
const MUST_KEEP_SYMBOLS: &[&str] = &["add_item", "total_count", "Inventory"];

/// Body-local identifiers `signatures`/`map` must strip.
const MUST_DROP_BODIES: &[&str] = &["body_secret_alpha", "body_secret_beta"];

fn render_mode(mode: &str) -> String {
    render_mode_full(mode).0
}

/// Rendered view plus its pre-footer body token count. The compression-size
/// invariant measures `.1` (the body), since the reactive recovery footer and the
/// savings line are orthogonal affordances appended after compression, not part of
/// the compressed content.
fn render_mode_full(mode: &str) -> (String, usize) {
    crate::tools::ctx_read::render::process_mode(
        ACCURACY_FIXTURE,
        mode,
        "",
        "fixture.rs",
        "rs",
        crate::core::tokens::count_tokens(ACCURACY_FIXTURE),
        crate::tools::CrpMode::Off,
        "conformance/fixture.rs",
        None,
    )
}

fn accuracy_checks() -> Vec<Check> {
    let mut checks = Vec::new();

    for mode in ["map", "signatures", "aggressive", "entropy"] {
        checks.push(Check::from_bool(
            "accuracy",
            format!("read_mode_deterministic:{mode}"),
            render_mode(mode) == render_mode(mode),
            "two renders of the same fixture differ",
        ));
    }

    for mode in ["map", "signatures"] {
        let out = render_mode(mode);
        let missing: Vec<&&str> = MUST_KEEP_SYMBOLS
            .iter()
            .filter(|s| !out.contains(**s))
            .collect();
        checks.push(Check::from_bool(
            "accuracy",
            format!("read_mode_keeps_symbols:{mode}"),
            missing.is_empty(),
            &format!("symbols lost: {missing:?}"),
        ));
        let leaked: Vec<&&str> = MUST_DROP_BODIES
            .iter()
            .filter(|s| out.contains(**s))
            .collect();
        checks.push(Check::from_bool(
            "accuracy",
            format!("read_mode_strips_bodies:{mode}"),
            leaked.is_empty(),
            &format!("body content leaked: {leaked:?}"),
        ));
    }

    let fixture_tokens = crate::core::tokens::count_tokens(ACCURACY_FIXTURE);
    for mode in ["map", "signatures", "aggressive"] {
        let sent = render_mode_full(mode).1;
        checks.push(Check::from_bool(
            "accuracy",
            format!("read_mode_compresses:{mode}"),
            sent < fixture_tokens,
            &format!("no compression: {sent} >= {fixture_tokens} tokens"),
        ));
    }

    // Target-density mode (GL#444): the body (excluding header/savings lines)
    // must stay within the token budget, and the render must be deterministic.
    {
        let target = 0.4_f64;
        let result = crate::core::entropy::entropy_compress_to_density(ACCURACY_FIXTURE, target);
        let actual = result.compressed_tokens as f64 / fixture_tokens.max(1) as f64;
        checks.push(Check::from_bool(
            "accuracy",
            "density_respects_budget:0.4",
            actual <= target + 0.10,
            &format!("density {actual:.2} exceeds target {target:.2} (+0.10 tolerance)"),
        ));
        checks.push(Check::from_bool(
            "accuracy",
            "density_deterministic:0.4",
            render_mode("density:0.4") == render_mode("density:0.4"),
            "two density renders of the same fixture differ",
        ));
    }

    checks
}

fn contract_checks() -> Vec<Check> {
    let present = !crate::core::contracts::versions_kv().is_empty();
    vec![Check::from_bool(
        "contracts",
        "contract_versions_present",
        present,
        "versions_kv() is empty",
    )]
}

fn reproducibility_checks() -> Vec<Check> {
    let caps_stable = crate::core::server_capabilities::capabilities_value()
        == crate::core::server_capabilities::capabilities_value();
    let openapi_stable =
        crate::core::openapi::openapi_value() == crate::core::openapi::openapi_value();
    vec![
        Check::from_bool(
            "reproducibility",
            "capabilities_deterministic",
            caps_stable,
            "capabilities document differs across builds",
        ),
        Check::from_bool(
            "reproducibility",
            "openapi_deterministic",
            openapi_stable,
            "openapi document differs across builds",
        ),
    ]
}

fn extension_checks() -> Vec<Check> {
    let mut checks = Vec::new();
    let Ok(reg) = crate::core::extension_registry::global().read() else {
        checks.push(Check::fail(
            "extensions",
            "registry_readable",
            "extension registry lock poisoned",
        ));
        return checks;
    };

    for name in reg.compressor_names() {
        if let Some(c) = reg.compressor(&name) {
            checks.push(compressor_invariants(&name, c.as_ref()));
        }
    }
    for name in reg.chunker_names() {
        if let Some(c) = reg.chunker(&name) {
            checks.push(chunker_invariants(&name, c.as_ref()));
        }
    }
    for name in reg.read_mode_names() {
        if let Some(m) = reg.read_mode(&name) {
            checks.push(read_mode_invariants(&name, m.as_ref()));
        }
    }
    checks
}

fn compressor_invariants(name: &str, c: &dyn crate::core::extension_registry::Compressor) -> Check {
    for input in CORPUS {
        // Determinism.
        if c.compress(input, None) != c.compress(input, None) {
            return Check::fail(
                "extensions",
                format!("compressor:{name}"),
                "non-deterministic",
            );
        }
        // Budget is a hard byte ceiling, never split mid-char (valid UTF-8).
        let budget = 4;
        let out = c.compress(input, Some(budget));
        if out.len() > budget {
            return Check::fail(
                "extensions",
                format!("compressor:{name}"),
                format!("exceeded byte budget: {} > {budget}", out.len()),
            );
        }
    }
    Check::pass("extensions", format!("compressor:{name}"))
}

fn chunker_invariants(name: &str, c: &dyn crate::core::extension_registry::Chunker) -> Check {
    // Empty input ⇒ no chunks.
    if !c.chunk("").is_empty() {
        return Check::fail(
            "extensions",
            format!("chunker:{name}"),
            "empty input produced chunks",
        );
    }
    for input in CORPUS.iter().filter(|s| !s.trim().is_empty()) {
        // Determinism.
        if c.chunk(input) != c.chunk(input) {
            return Check::fail("extensions", format!("chunker:{name}"), "non-deterministic");
        }
        let chunks = c.chunk(input);
        // Non-empty input ⇒ at least one chunk, none empty after trim.
        if chunks.is_empty() {
            return Check::fail(
                "extensions",
                format!("chunker:{name}"),
                "non-empty input produced no chunks",
            );
        }
        if chunks.iter().any(|c| c.trim().is_empty()) {
            return Check::fail(
                "extensions",
                format!("chunker:{name}"),
                "produced an empty chunk",
            );
        }
    }
    Check::pass("extensions", format!("chunker:{name}"))
}

fn read_mode_invariants(name: &str, m: &dyn crate::core::extension_registry::ReadMode) -> Check {
    for input in CORPUS {
        if m.render(input, "x.txt") != m.render(input, "x.txt") {
            return Check::fail(
                "extensions",
                format!("read_mode:{name}"),
                "non-deterministic",
            );
        }
    }
    // The byte-faithful `full` mode must round-trip source verbatim.
    if name == "full" {
        let sample = "verbatim\nsource\n漢字";
        if m.render(sample, "x.txt") != sample {
            return Check::fail("extensions", "read_mode:full", "full mode altered source");
        }
    }
    Check::pass("extensions", format!("read_mode:{name}"))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn builtin_suite_passes() {
        let card = run();
        assert!(
            card.all_passed(),
            "conformance failures: {:?}",
            card.failures()
        );
        assert!(card.total() >= 6, "expected a meaningful number of checks");
    }

    #[test]
    fn scorecard_json_shape() {
        let v = run().to_json();
        assert_eq!(v["version"], 1);
        assert!(v["checks"].is_array());
        // Shape only: `passed == total` is covered by builtin_suite_passes.
        // Asserting it here races with tests that register an intentionally
        // broken compressor in the global extension registry.
        let passed = v["passed"].as_u64().expect("passed is a number");
        let total = v["total"].as_u64().expect("total is a number");
        assert!(passed <= total);
        assert_eq!(v["checks"].as_array().map(|c| c.len() as u64), Some(total));
    }

    #[test]
    fn detects_a_nondeterministic_compressor() {
        use std::sync::atomic::{AtomicU64, Ordering};
        struct Flaky(AtomicU64);
        impl crate::core::extension_registry::Compressor for Flaky {
            #[allow(clippy::unnecessary_literal_bound)]
            fn name(&self) -> &str {
                "flaky"
            }
            fn compress(&self, _input: &str, _budget: Option<usize>) -> String {
                self.0.fetch_add(1, Ordering::SeqCst).to_string()
            }
        }
        let check = compressor_invariants("flaky", &Flaky(AtomicU64::new(0)));
        assert!(!check.passed);
        assert!(check.detail.contains("non-deterministic"));
    }
}