lean-ctx 3.9.19

Context Runtime for AI Agents with CCP. 79 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%.
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
//! Shareable reports for the proxy compression benchmark.

use std::{
    env, fs,
    path::{Path, PathBuf},
};

use chrono::Local;
use serde_json::{Value, json};

use crate::{
    core::{benchmark_compare::system_info, share::copy_to_clipboard},
    proxy::pipeline_bench::{BenchmarkReport, run_benchmark},
};

const REPORTS_DIRECTORY: &str = ".local/share/lean-ctx/reports";
const VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ReportFormat {
    Markdown,
    Json,
    Text,
}

#[derive(Debug, Clone)]
struct BenchmarkOptions {
    format: ReportFormat,
    output: Option<PathBuf>,
    share: bool,
}

/// Runs the pipeline benchmark and writes a shareable report.
pub(crate) fn cmd_benchmark_real(args: &[String]) {
    if args
        .iter()
        .any(|arg| matches!(arg.as_str(), "-h" | "--help"))
    {
        usage();
        return;
    }

    let options = match parse(args) {
        Ok(options) => options,
        Err(error) => {
            eprintln!("benchmark: {error}");
            usage();
            std::process::exit(2);
        }
    };
    let report = run_benchmark();
    let markdown = generate_markdown_report(&report);
    let rendered = match options.format {
        ReportFormat::Markdown => markdown.clone(),
        ReportFormat::Json => generate_json_report(&report),
        ReportFormat::Text => generate_text_report(&report),
    };
    let path = match options.output {
        Some(path) => path,
        None => match default_report_path() {
            Ok(path) => path,
            Err(error) => {
                eprintln!("benchmark: {error}");
                std::process::exit(2);
            }
        },
    };
    let contents_to_save = if path.extension().is_some_and(|extension| extension == "md") {
        &markdown
    } else {
        &rendered
    };

    if let Err(error) = save_report(&path, contents_to_save) {
        eprintln!("benchmark: {error}");
        std::process::exit(2);
    }
    eprintln!("Report saved to: {}", path.display());

    if options.share {
        if copy_to_clipboard(&markdown) {
            eprintln!("Report copied to clipboard.");
        } else {
            eprintln!("benchmark: could not copy report to the clipboard");
        }
    }
    eprintln!("Share: lean-ctx benchmark --share (copies to clipboard)");
    print!("{rendered}");
}

/// Produces a Markdown report ready to paste into a pull request or post.
pub(crate) fn generate_markdown_report(report: &BenchmarkReport) -> String {
    let machine = system_info::collect();
    let generated_at = report_timestamp();
    let average_latency_ms = average_latency_ms(report);
    let tokens_saved = report
        .total_tokens_before
        .saturating_sub(report.total_tokens_after);
    let retained_pct = percentage(report.total_tokens_after, report.total_tokens_before);
    let mut output = format!(
        "## lean-ctx Compression Benchmark Results\n\n**Date:** {generated_at}\n\
         **Machine:** {machine}\n\n### Per-scenario results\n\n\
         | Scenario | Messages | Before | After | Savings | Latency |\n\
         | --- | ---: | ---: | ---: | ---: | ---: |\n"
    );

    for result in &report.results {
        output.push_str(&format!(
            "| {} | {} | {} | {} | {:.1}% | {:.2}ms |\n",
            result.scenario,
            result.message_count,
            result.total_tokens_before,
            result.total_tokens_after,
            result.savings_pct,
            result.latency_us as f64 / 1_000.0,
        ));
    }

    output.push_str(&format!(
        "\n### Per-stage contribution breakdown\n\n\
         | Stage | Tokens | Share of input | Contribution |\n\
         | --- | ---: | ---: | --- |\n\
         | Input context | {} | 100.0% | Baseline context received |\n\
         | Compression pipeline | {} | {:.1}% | Tokens removed |\n\
         | Forwarded context | {} | {:.1}% | Tokens retained |\n\n\
         **TOTAL: {:.1}% average savings, {:.2}ms average latency**\n\n\
         vs typical proxy overhead: 500ms+ (lean-ctx: <1ms)\n\n\
         Generated by lean-ctx v{VERSION} | https://github.com/yvgude/lean-ctx\n",
        report.total_tokens_before,
        tokens_saved,
        report.average_savings_pct,
        report.total_tokens_after,
        retained_pct,
        report.average_savings_pct,
        average_latency_ms,
    ));
    output
}

/// Produces the Shields.io badge users can embed beside the full report.
pub(crate) fn generate_badge_markdown(report: &BenchmarkReport) -> String {
    let savings = if report.average_savings_pct.is_finite() {
        report.average_savings_pct.round().clamp(0.0, 100.0) as u8
    } else {
        0
    };
    format!(
        "![lean-ctx savings](https://img.shields.io/badge/lean--ctx-{savings}%25_savings-brightgreen)"
    )
}

/// Produces the report as JSON for CI and other programmatic consumers.
pub(crate) fn generate_json_report(report: &BenchmarkReport) -> String {
    serde_json::to_string_pretty(&json_report(report))
        .expect("benchmark report contains only JSON-compatible values")
}

fn json_report(report: &BenchmarkReport) -> Value {
    let machine = system_info::collect();
    let average_latency_ms = average_latency_ms(report);
    let tokens_saved = report
        .total_tokens_before
        .saturating_sub(report.total_tokens_after);
    let retained_pct = percentage(report.total_tokens_after, report.total_tokens_before);
    json!({
        "title": "lean-ctx Compression Benchmark Results",
        "generated_at": report_timestamp(),
        "machine": {
            "os": machine.os,
            "arch": machine.arch,
            "cpu": machine.cpu_brand,
            "cores": machine.cpu_cores,
            "memory_gb": machine.memory_gb,
        },
        "scenarios": report.results.iter().map(|result| json!({
            "name": result.scenario,
            "message_count": result.message_count,
            "tokens_before": result.total_tokens_before,
            "tokens_after": result.total_tokens_after,
            "savings_pct": result.savings_pct,
            "latency_us": result.latency_us,
            "latency_ms": result.latency_us as f64 / 1_000.0,
            "estimated_cost_savings_usd": result.estimated_cost_savings_usd,
            "expected_savings_pct": {
                "min": result.expected_savings_min_pct,
                "max": result.expected_savings_max_pct,
            },
        })).collect::<Vec<_>>(),
        "stages": [
            {
                "name": "Input context",
                "tokens": report.total_tokens_before,
                "share_of_input_pct": 100.0,
                "contribution": "Baseline context received",
            },
            {
                "name": "Compression pipeline",
                "tokens": tokens_saved,
                "share_of_input_pct": report.average_savings_pct,
                "contribution": "Tokens removed",
            },
            {
                "name": "Forwarded context",
                "tokens": report.total_tokens_after,
                "share_of_input_pct": retained_pct,
                "contribution": "Tokens retained",
            }
        ],
        "summary": {
            "total_tokens_before": report.total_tokens_before,
            "total_tokens_after": report.total_tokens_after,
            "tokens_saved": tokens_saved,
            "average_savings_pct": report.average_savings_pct,
            "average_latency_ms": average_latency_ms,
            "max_latency_us": report.max_latency_us,
            "total_estimated_cost_savings_usd": report.total_estimated_cost_savings_usd,
        },
        "comparison": {
            "typical_proxy_overhead": "500ms+",
            "lean_ctx_overhead": "<1ms",
        },
        "badge_markdown": generate_badge_markdown(report),
        "footer": format!("Generated by lean-ctx v{VERSION} | https://github.com/yvgude/lean-ctx"),
    })
}

fn generate_text_report(report: &BenchmarkReport) -> String {
    let mut output = format!(
        "lean-ctx compression benchmark\n\n{:<24} {:>9} {:>9} {:>9} {:>10}\n{}\n",
        "Scenario",
        "Before",
        "After",
        "Savings",
        "Latency",
        "-".repeat(68),
    );
    for result in &report.results {
        output.push_str(&format!(
            "{:<24} {:>9} {:>9} {:>8.1}% {:>8.2}ms\n",
            result.scenario,
            result.total_tokens_before,
            result.total_tokens_after,
            result.savings_pct,
            result.latency_us as f64 / 1_000.0,
        ));
    }
    output.push_str(&format!(
        "\nTOTAL: {:.1}% average savings, {:.2}ms average latency\n",
        report.average_savings_pct,
        average_latency_ms(report),
    ));
    output
}

fn average_latency_ms(report: &BenchmarkReport) -> f64 {
    if report.results.is_empty() {
        return 0.0;
    }
    report
        .results
        .iter()
        .map(|result| result.latency_us as f64)
        .sum::<f64>()
        / report.results.len() as f64
        / 1_000.0
}

fn percentage(part: usize, total: usize) -> f64 {
    if total == 0 {
        0.0
    } else {
        part as f64 / total as f64 * 100.0
    }
}

fn report_timestamp() -> String {
    Local::now().format("%Y-%m-%d %H:%M:%S %Z").to_string()
}

fn default_report_path() -> Result<PathBuf, String> {
    let home = env::var_os("HOME")
        .map(PathBuf::from)
        .ok_or_else(|| "HOME is not set; pass --output <path> instead".to_string())?;
    let timestamp = Local::now().format("%Y%m%d_%H%M%S");
    Ok(home
        .join(REPORTS_DIRECTORY)
        .join(format!("benchmark_{timestamp}.md")))
}

fn save_report(path: &Path, contents: &str) -> Result<(), String> {
    if let Some(parent) = path
        .parent()
        .filter(|parent| !parent.as_os_str().is_empty())
    {
        fs::create_dir_all(parent)
            .map_err(|error| format!("create report directory {}: {error}", parent.display()))?;
    }
    fs::write(path, contents).map_err(|error| format!("write report {}: {error}", path.display()))
}

fn parse(args: &[String]) -> Result<BenchmarkOptions, String> {
    let mut options = BenchmarkOptions {
        format: ReportFormat::Text,
        output: None,
        share: false,
    };
    let mut index = 0;
    while index < args.len() {
        match args[index].as_str() {
            "--real" => {}
            "--json" => options.format = ReportFormat::Json,
            "--share" => options.share = true,
            "--format" => {
                index += 1;
                let value = args
                    .get(index)
                    .ok_or_else(|| "--format requires markdown, json, or text".to_string())?;
                options.format = parse_format(value)?;
            }
            "--output" | "-o" => {
                index += 1;
                let value = args
                    .get(index)
                    .ok_or_else(|| "--output requires a path".to_string())?;
                options.output = Some(PathBuf::from(value));
            }
            unknown => return Err(format!("unknown argument {unknown:?}")),
        }
        index += 1;
    }
    Ok(options)
}

fn parse_format(value: &str) -> Result<ReportFormat, String> {
    match value {
        "markdown" | "md" => Ok(ReportFormat::Markdown),
        "json" => Ok(ReportFormat::Json),
        "text" => Ok(ReportFormat::Text),
        _ => Err(format!(
            "unsupported format {value:?}; expected markdown, json, or text"
        )),
    }
}

fn usage() {
    println!(
        "Run the proxy compression benchmark and save a shareable report.\n\n\
         Usage: lean-ctx benchmark [--real] [--format markdown|json|text] [--output <path>] [--share]\n\n\
         The default Markdown report is saved to ~/.local/share/lean-ctx/reports/."
    );
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::proxy::pipeline_bench::BenchmarkResult;

    fn sample_report() -> BenchmarkReport {
        BenchmarkReport {
            results: vec![
                BenchmarkResult {
                    scenario: "coding_session",
                    message_count: 20,
                    total_tokens_before: 1_000,
                    total_tokens_after: 200,
                    savings_pct: 80.0,
                    latency_us: 500,
                    estimated_cost_savings_usd: 0.0024,
                    expected_savings_min_pct: 60.0,
                    expected_savings_max_pct: 90.0,
                },
                BenchmarkResult {
                    scenario: "debugging_session",
                    message_count: 10,
                    total_tokens_before: 1_000,
                    total_tokens_after: 300,
                    savings_pct: 70.0,
                    latency_us: 1_000,
                    estimated_cost_savings_usd: 0.0021,
                    expected_savings_min_pct: 60.0,
                    expected_savings_max_pct: 90.0,
                },
            ],
            total_tokens_before: 2_000,
            total_tokens_after: 500,
            average_savings_pct: 75.0,
            total_estimated_cost_savings_usd: 0.0045,
            max_latency_us: 1_000,
        }
    }

    #[test]
    fn markdown_report_contains_expected_sections() {
        let markdown = generate_markdown_report(&sample_report());

        assert!(markdown.contains("## lean-ctx Compression Benchmark Results"));
        assert!(markdown.contains("### Per-scenario results"));
        assert!(markdown.contains("### Per-stage contribution breakdown"));
        assert!(markdown.contains("TOTAL: 75.0% average savings, 0.75ms average latency"));
        assert!(markdown.contains("vs typical proxy overhead: 500ms+ (lean-ctx: <1ms)"));
        assert!(markdown.contains("Generated by lean-ctx v"));
    }

    #[test]
    fn badge_markdown_uses_valid_shields_io_format() {
        assert_eq!(
            generate_badge_markdown(&sample_report()),
            "![lean-ctx savings](https://img.shields.io/badge/lean--ctx-75%25_savings-brightgreen)"
        );
    }

    #[test]
    fn json_report_is_valid_and_parseable() {
        let json = generate_json_report(&sample_report());
        let value: Value = serde_json::from_str(&json).unwrap();

        assert_eq!(value["title"], "lean-ctx Compression Benchmark Results");
        assert_eq!(value["summary"]["average_savings_pct"], 75.0);
        assert_eq!(value["scenarios"].as_array().map(Vec::len), Some(2));
        assert_eq!(value["stages"].as_array().map(Vec::len), Some(3));
    }

    #[test]
    fn parse_accepts_new_report_options() {
        let options = parse(&[
            "--format".to_string(),
            "markdown".to_string(),
            "--output".to_string(),
            "report.md".to_string(),
            "--share".to_string(),
        ])
        .unwrap();

        assert_eq!(options.format, ReportFormat::Markdown);
        assert_eq!(options.output, Some(PathBuf::from("report.md")));
        assert!(options.share);
    }
}