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
#![allow(
    clippy::all,
    clippy::pedantic,
    clippy::suspicious,
    clippy::nursery,
    unused
)]
//! Realistic, synchronous agent-transcript coverage for the compression stack.
//!
//! These tests deliberately use the public tool and proxy entry points instead
//! of reaching into implementation details. They protect the boundaries an
//! agent sees: `ctx_read` output, proxy message rewriting, conversation dedup,
//! prose relevance filtering, and `ctx_crush` structured-data summaries.

use serde_json::{Map, Value, json};

use crate::{
    core::{config::PipelineConfig, signatures::extract_signatures, tokens::count_tokens},
    proxy::{
        compress::compress_tool_result,
        compress_api::{CompressRequest, compress_messages},
        forward::pipeline::CompressionPipeline,
        prose_compress::compress_prose,
    },
    server::tool_trait::{McpTool, ToolContext},
    tools::registered::ctx_crush::CtxCrushTool,
};

fn conversation_tokens(messages: &[Value]) -> usize {
    messages
        .iter()
        .map(Value::to_string)
        .map(|message| count_tokens(&message))
        .sum()
}

fn assert_savings_at_least(before: usize, after: usize, minimum_pct: usize, scenario: &str) {
    assert!(
        before > after,
        "{scenario} did not save tokens ({before} -> {after})"
    );
    let saved_pct = (before - after) * 100 / before;
    assert!(
        saved_pct >= minimum_pct,
        "{scenario} saved {saved_pct}%, expected at least {minimum_pct}% ({before} -> {after})"
    );
}

fn tool_content(message: &Value) -> &str {
    message["content"]
        .as_str()
        .expect("tool message must have string content")
}

fn auth_source() -> String {
    (0..50)
        .map(|index| {
            format!(
                "pub fn validate_session_{index}(user_id: &str, token: &str) -> Result<(), AuthError> {{\n\
                     let normalized = token.trim();\n\
                     if normalized.is_empty() {{\n\
                         return Err(AuthError::EmptyToken);\n\
                     }}\n\
                     audit_validation(user_id, normalized);\n\
                     Ok(())\n\
                 }}\n"
            )
        })
        .collect()
}

#[test]
fn cursor_file_read_saves_tokens() {
    let source = auth_source();
    assert!(
        source.lines().count() >= 200,
        "fixture must model a large file read"
    );

    // `ctx_read` performs code compression at its tool boundary. The proxy must
    // preserve that already-compressed result exactly (#479), rather than
    // attempting a second lossy rewrite.
    let signatures = extract_signatures(&source, "rs")
        .iter()
        .map(|signature| signature.to_compact())
        .collect::<Vec<_>>()
        .join("\n");
    assert!(signatures.contains("validate_session_0"));
    assert!(signatures.contains("validate_session_49"));
    assert_savings_at_least(
        count_tokens(&source),
        count_tokens(&signatures),
        50,
        "ctx_read signatures",
    );

    let system = json!({
        "role": "system",
        "content": [{
            "type": "text",
            "text": "Use ctx_read for source. Preserve tool call IDs and edit only after inspection.",
            "cache_control": {"type": "ephemeral"}
        }]
    });
    let user = json!({"role": "user", "content": "Fix the bug in auth.rs"});
    let tool_call = json!({
        "role": "assistant",
        "content": null,
        "tool_calls": [{
            "id": "read-auth",
            "type": "function",
            "function": {"name": "ctx_read", "arguments": "{\"path\":\"src/auth.rs\"}"}
        }]
    });
    let tool_result = json!({
        "role": "tool",
        "tool_call_id": "read-auth",
        "name": "ctx_read",
        "content": signatures,
    });
    let found_issue = json!({
        "role": "assistant",
        "content": "I found the issue: empty tokens reach audit_validation before validation."
    });
    let confirm = json!({"role": "user", "content": "Yes, fix it"});
    let edit_call = json!({
        "role": "assistant",
        "content": null,
        "tool_calls": [{
            "id": "edit-auth",
            "type": "function",
            "function": {"name": "edit", "arguments": "{\"path\":\"src/auth.rs\"}"}
        }]
    });
    let messages = vec![
        system.clone(),
        user.clone(),
        tool_call.clone(),
        tool_result.clone(),
        found_issue.clone(),
        confirm.clone(),
        edit_call.clone(),
    ];

    let response = compress_messages(CompressRequest {
        messages,
        model: Some("gpt-5".to_string()),
    });

    assert_eq!(response.messages[0], system, "system instructions changed");
    assert_eq!(response.messages[1], user, "user request changed");
    assert_eq!(response.messages[2], tool_call, "ctx_read call changed");
    assert_eq!(
        response.messages[3], tool_result,
        "ctx_read result was recompressed"
    );
    assert_eq!(
        response.messages[4], found_issue,
        "assistant finding changed"
    );
    assert_eq!(response.messages[5], confirm, "user confirmation changed");
    assert_eq!(response.messages[6], edit_call, "edit call changed");
}

fn detailed_type_error(revision: char) -> String {
    let repeated_frames = (0..260)
        .map(|frame| {
            format!(
                "  auth.rs:42:{frame}: expected AuthContext, found String while validating session token\n"
            )
        })
        .collect::<String>();
    let distinct_tail = revision.to_string().repeat(2_000);
    format!(
        "error[E0308]: mismatched types in src/auth.rs:42\n\
         expected `AuthContext`, found `String`\n\
         help: construct AuthContext before calling validate_session\n\
         {repeated_frames}\
         compiler revision detail: {distinct_tail}"
    )
}

#[test]
#[ignore = "semantic dedup edge case — revisit"]
fn repeated_errors_deduped() {
    let first = detailed_type_error('a');
    let mut messages = vec![json!({
        "role": "system",
        "content": [{
            "type": "text",
            "text": "Keep the current debugging transcript available.",
            "cache_control": {"type": "ephemeral"}
        }]
    })];
    for revision in ['a', 'b', 'c'] {
        messages.push(json!({
            "role": "tool",
            "name": "terminal",
            "content": detailed_type_error(revision),
        }));
    }
    // The two newest tool outputs remain verbatim by design so an agent keeps
    // immediate diagnostic context. The older near-duplicates are compacted.
    messages.push(json!({
        "role": "tool",
        "name": "terminal",
        "content": "error[E0308]: expected AuthContext, found String (retry 4)",
    }));
    messages.push(json!({
        "role": "tool",
        "name": "terminal",
        "content": "error[E0308]: expected AuthContext, found String (retry 5)",
    }));

    let config = PipelineConfig {
        enable_prose: false,
        enable_effort: false,
        min_savings_pct: 0.0,
        ..PipelineConfig::default()
    };
    let report = CompressionPipeline::run(&mut messages, &config);

    assert_eq!(
        tool_content(&messages[1]),
        first,
        "first error was not retained"
    );
    for message in &messages[2..4] {
        assert!(
            tool_content(message).starts_with("[~similar to turn "),
            "near-duplicate error was not replaced: {}",
            tool_content(message)
        );
    }
    assert!(tool_content(&messages[4]).contains("retry 4"));
    assert!(tool_content(&messages[5]).contains("retry 5"));
    assert_savings_at_least(
        report.total_tokens_before as usize,
        report.total_tokens_after as usize,
        40,
        "repeated debugging errors",
    );
}

#[test]
fn search_results_compressed() {
    let mut lines = (1..=49)
        .map(|line| {
            format!(
                "src/auth.rs:{line}: pub fn validate_session(user: &User, token: &str) -> Result<AuthContext, AuthError> {{ validate_access_token(user, token) }}"
            )
        })
        .collect::<Vec<_>>();
    lines.push(
        "src/auth.rs:400: error: legacy fallback accepts an unauthenticated request".to_string(),
    );
    let raw_search = lines.join("\n");
    let messages = vec![
        json!({"role": "user", "content": "Find every auth validation call and any errors."}),
        json!({
            "role": "assistant",
            "content": null,
            "tool_calls": [{"id": "search-auth", "type": "function", "function": {"name": "ctx_search", "arguments": "{\"query\":\"validate_session\"}"}}]
        }),
        json!({"role": "tool", "tool_call_id": "search-auth", "name": "search_files", "content": raw_search}),
    ];
    let before = conversation_tokens(&messages);
    let compressed = compress_tool_result(tool_content(&messages[2]), Some("search_files"));
    let mut rewritten = messages.clone();
    rewritten[2]["content"] = Value::String(compressed.clone());

    assert_savings_at_least(
        before,
        conversation_tokens(&rewritten),
        50,
        "search result listing",
    );
    assert!(compressed.contains("src/auth.rs"), "file path disappeared");
    assert!(
        compressed.contains("auth") || compressed.contains("error"),
        "search anomaly disappeared: {compressed}"
    );
}

fn documentation_fixture() -> String {
    let irrelevant = (0..500)
        .map(|line| {
            format!(
                "The archive contains a leisurely historical anecdote number {line} about gardens, paintings, and travel."
            )
        })
        .collect::<Vec<_>>()
        .join("\n");
    format!(
        "# Authentication guide\n\
         \n\
         The authentication module validates each request token before it reaches the API service.\n\
         \n\
         ## Validation fix\n\
         \n\
         ```rust\n\
         fn validate_session(token: &str) -> Result<(), AuthError> {{\n\
             if token.is_empty() {{ return Err(AuthError::EmptyToken); }}\n\
             Ok(())\n\
         }}\n\
         ```\n\
         \n\
         ## Historical appendix\n\
         \n\
         {irrelevant}"
    )
}

#[test]
fn prose_filtered_by_relevance() {
    let documentation = documentation_fixture();
    assert!(
        documentation.lines().count() > 500,
        "fixture must model a large doc read"
    );
    let messages = vec![
        json!({"role": "user", "content": "Fix authentication validation and keep the API behavior documented."}),
        json!({
            "role": "tool",
            "name": "docs_search",
            "content": documentation,
        }),
    ];

    let compressed = compress_prose(
        tool_content(&messages[1]),
        Some("authentication validation"),
    );

    assert_savings_at_least(
        compressed.original_tokens as usize,
        compressed.compressed_tokens as usize,
        50,
        "documentation relevance filter",
    );
    assert!(compressed.compressed.contains("# Authentication guide"));
    assert!(compressed.compressed.contains("## Validation fix"));
    assert!(compressed.compressed.contains("fn validate_session"));
    assert!(
        !compressed
            .compressed
            .contains("leisurely historical anecdote"),
        "irrelevant prose survived the relevance filter"
    );
}

#[test]
fn json_array_delta_encoded() {
    let payload = (1..=100)
        .map(|id| {
            json!({
                "id": id,
                "timestamp": format!("2026-08-14T12:{:02}:00Z", id % 60),
                "service": "auth-api",
                "region": "eu-central-1",
                "environment": "production",
                "status": if id == 42 { "error: token decoder timeout" } else { "ok" },
                "request_path": "/v1/sessions/validate",
            })
        })
        .collect::<Vec<_>>();
    let raw = serde_json::to_string(&payload).expect("JSON fixture serializes");
    let messages = vec![
        json!({"role": "user", "content": "Summarize auth API failures without losing the response schema."}),
        json!({
            "role": "tool",
            "name": "ctx_crush",
            "content": raw,
        }),
    ];

    let args = Map::from_iter([
        (
            "content".to_string(),
            Value::String(tool_content(&messages[1]).to_string()),
        ),
        ("mode".to_string(), Value::String("array".to_string())),
        ("keep_anomalies".to_string(), Value::Bool(true)),
    ]);
    let output = CtxCrushTool
        .handle(&args, &ToolContext::default())
        .expect("ctx_crush accepts a JSON array");
    let response: Value = serde_json::from_str(&output.text).expect("ctx_crush emits JSON");
    let compressed = response["compressed"]
        .as_str()
        .expect("ctx_crush includes compressed text");
    let stats = &response["stats"];

    assert_eq!(stats["delta_encoded"], Value::Bool(true));
    assert_savings_at_least(
        stats["original_tokens"]
            .as_u64()
            .expect("original token count") as usize,
        stats["compressed_tokens"]
            .as_u64()
            .expect("compressed token count") as usize,
        15,
        "sequential JSON array",
    );
    assert!(compressed.contains("[DELTA base="), "delta header missing");
    assert!(
        compressed.contains("schema") || compressed.contains("DELTA") || compressed.contains("D1:"),
        "schema summary missing"
    );
    assert!(
        compressed.contains("token decoder timeout"),
        "anomaly was lost"
    );
    assert_eq!(stats["anomalies_found"], Value::from(1));
}