lean-ctx 3.8.4

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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//! Feature config sections (autonomy, providers, loop-detection, updates,
//! boundary-policy, secret-detection, cloud, gain).
//! Split out of `schema/mod.rs`; `use super::*` re-imports helpers + `SectionSchema`.

#[allow(clippy::wildcard_imports)]
use super::*;
use std::collections::BTreeMap;

pub(super) fn build(sections: &mut BTreeMap<String, SectionSchema>) {
    let cfg = crate::core::config::Config::default();
    let mut autonomy = BTreeMap::new();
    autonomy.insert(
        "enabled".into(),
        key(
            "bool",
            serde_json::json!(cfg.autonomy.enabled),
            "Enable autonomous background behaviors",
        ),
    );
    autonomy.insert(
        "auto_preload".into(),
        key(
            "bool",
            serde_json::json!(cfg.autonomy.auto_preload),
            "Auto-preload related files on first read",
        ),
    );
    autonomy.insert(
        "auto_dedup".into(),
        key(
            "bool",
            serde_json::json!(cfg.autonomy.auto_dedup),
            "Auto-deduplicate repeated reads",
        ),
    );
    autonomy.insert(
        "auto_related".into(),
        key(
            "bool",
            serde_json::json!(cfg.autonomy.auto_related),
            "Auto-load graph-related files",
        ),
    );
    autonomy.insert(
        "auto_consolidate".into(),
        key(
            "bool",
            serde_json::json!(cfg.autonomy.auto_consolidate),
            "Auto-consolidate knowledge periodically",
        ),
    );
    autonomy.insert(
        "silent_preload".into(),
        key(
            "bool",
            serde_json::json!(cfg.autonomy.silent_preload),
            "Suppress preload notifications in output",
        ),
    );
    autonomy.insert(
        "dedup_threshold".into(),
        key(
            "usize",
            serde_json::json!(cfg.autonomy.dedup_threshold),
            "Number of repeated reads before dedup triggers",
        ),
    );
    autonomy.insert(
        "consolidate_every_calls".into(),
        key(
            "u32",
            serde_json::json!(cfg.autonomy.consolidate_every_calls),
            "Consolidate knowledge every N tool calls",
        ),
    );
    autonomy.insert(
        "consolidate_cooldown_secs".into(),
        key(
            "u64",
            serde_json::json!(cfg.autonomy.consolidate_cooldown_secs),
            "Minimum seconds between consolidation runs",
        ),
    );
    autonomy.insert(
        "cognition_loop_enabled".into(),
        key_with_env(
            "bool",
            serde_json::json!(cfg.autonomy.cognition_loop_enabled),
            "Enable the background cognition loop (periodic knowledge consolidation)",
            "LEAN_CTX_COGNITION_LOOP_ENABLED",
        ),
    );
    autonomy.insert(
        "cognition_loop_interval_secs".into(),
        key_with_env(
            "u64",
            serde_json::json!(cfg.autonomy.cognition_loop_interval_secs),
            "Seconds between cognition loop iterations",
            "LEAN_CTX_COGNITION_LOOP_INTERVAL_SECS",
        ),
    );
    autonomy.insert(
        "cognition_loop_max_steps".into(),
        key_with_env(
            "u8",
            serde_json::json!(cfg.autonomy.cognition_loop_max_steps),
            "Maximum steps per cognition loop iteration",
            "LEAN_CTX_COGNITION_LOOP_MAX_STEPS",
        ),
    );
    sections.insert(
        "autonomy".into(),
        SectionSchema {
            description: "Controls autonomous background behaviors (preload, dedup, consolidation)"
                .into(),
            keys: autonomy,
        },
    );

    let mut providers = BTreeMap::new();
    providers.insert(
        "enabled".into(),
        key(
            "bool",
            serde_json::json!(cfg.providers.enabled),
            "Master switch for the provider subsystem (GitHub, GitLab, etc.)",
        ),
    );
    providers.insert(
        "auto_index".into(),
        key(
            "bool",
            serde_json::json!(cfg.providers.auto_index),
            "Auto-ingest provider results into BM25/embedding indexes",
        ),
    );
    providers.insert(
        "cache_ttl_secs".into(),
        key(
            "u64",
            serde_json::json!(cfg.providers.cache_ttl_secs),
            "Default cache TTL for provider results (seconds)",
        ),
    );
    providers.insert(
        "github.enabled".into(),
        key(
            "bool",
            serde_json::json!(cfg.providers.github.enabled),
            "Enable/disable GitHub provider",
        ),
    );
    providers.insert(
        "github.api_url".into(),
        key(
            "string",
            serde_json::json!(cfg.providers.github.api_url),
            "GitHub API base URL (for GitHub Enterprise)",
        ),
    );
    providers.insert(
        "gitlab.enabled".into(),
        key(
            "bool",
            serde_json::json!(cfg.providers.gitlab.enabled),
            "Enable/disable GitLab provider",
        ),
    );
    providers.insert(
        "gitlab.api_url".into(),
        key(
            "string",
            serde_json::json!(cfg.providers.gitlab.api_url),
            "GitLab API base URL (for self-hosted instances)",
        ),
    );
    providers.insert(
        "mcp_bridges.<name>.url".into(),
        key(
            "string",
            serde_json::json!(null),
            "HTTP/SSE URL for a remote MCP server",
        ),
    );
    providers.insert(
        "mcp_bridges.<name>.command".into(),
        key(
            "string",
            serde_json::json!(null),
            "Command to spawn a local MCP server (stdio transport)",
        ),
    );
    providers.insert(
        "mcp_bridges.<name>.args".into(),
        key(
            "array",
            serde_json::json!([]),
            "Arguments for the MCP server command",
        ),
    );
    providers.insert(
        "mcp_bridges.<name>.auth_env".into(),
        key(
            "string",
            serde_json::json!(null),
            "Environment variable name containing auth token for MCP server",
        ),
    );
    sections.insert(
            "providers".into(),
            SectionSchema {
                description:
                    "External context providers (GitHub, GitLab, Jira, MCP bridges, etc.). Set tokens via env vars (GITHUB_TOKEN, GITLAB_TOKEN). MCP bridges connect external MCP servers as context sources."
                        .into(),
                keys: providers,
            },
        );

    let mut loop_det = BTreeMap::new();
    loop_det.insert(
        "normal_threshold".into(),
        key(
            "u32",
            serde_json::json!(cfg.loop_detection.normal_threshold),
            "Repetitions before reducing output",
        ),
    );
    loop_det.insert(
        "reduced_threshold".into(),
        key(
            "u32",
            serde_json::json!(cfg.loop_detection.reduced_threshold),
            "Repetitions before further reducing output",
        ),
    );
    loop_det.insert(
        "blocked_threshold".into(),
        key(
            "u32",
            serde_json::json!(cfg.loop_detection.blocked_threshold),
            "Repetitions before blocking. 0 = disabled",
        ),
    );
    loop_det.insert(
        "window_secs".into(),
        key(
            "u64",
            serde_json::json!(cfg.loop_detection.window_secs),
            "Time window in seconds for loop detection",
        ),
    );
    loop_det.insert(
        "search_group_limit".into(),
        key(
            "u32",
            serde_json::json!(cfg.loop_detection.search_group_limit),
            "Maximum unique searches within a loop window",
        ),
    );
    loop_det.insert(
            "tool_total_limits".into(),
            key(
                "table",
                serde_json::json!({"ctx_read": 100, "ctx_search": 80, "ctx_shell": 50, "ctx_semantic_search": 60}),
                "Per-tool total call limits within a session. Keys are tool names, values are max calls",
            ),
        );
    sections.insert(
        "loop_detection".into(),
        SectionSchema {
            description: "Loop detection settings for preventing repeated identical tool calls"
                .into(),
            keys: loop_det,
        },
    );

    let mut updates = BTreeMap::new();
    updates.insert(
        "auto_update".into(),
        key(
            "bool",
            serde_json::json!(cfg.updates.auto_update),
            "Enable automatic updates (requires explicit opt-in)",
        ),
    );
    updates.insert(
        "check_interval_hours".into(),
        key(
            "u64",
            serde_json::json!(cfg.updates.check_interval_hours),
            "How often to check for updates (hours)",
        ),
    );
    updates.insert(
        "notify_only".into(),
        key(
            "bool",
            serde_json::json!(cfg.updates.notify_only),
            "Only notify about updates, don't install automatically",
        ),
    );
    sections.insert(
        "updates".into(),
        SectionSchema {
            description: "Automatic update configuration".into(),
            keys: updates,
        },
    );

    let mut boundary = BTreeMap::new();
    boundary.insert(
        "cross_project_search".into(),
        key(
            "bool",
            serde_json::json!(cfg.boundary_policy.cross_project_search),
            "Allow searching across project boundaries",
        ),
    );
    boundary.insert(
        "cross_project_import".into(),
        key(
            "bool",
            serde_json::json!(cfg.boundary_policy.cross_project_import),
            "Allow importing knowledge from other projects",
        ),
    );
    boundary.insert(
        "audit_cross_access".into(),
        key(
            "bool",
            serde_json::json!(cfg.boundary_policy.audit_cross_access),
            "Log audit events when cross-project access occurs",
        ),
    );
    boundary.insert(
        "universal_gotchas_enabled".into(),
        key(
            "bool",
            serde_json::json!(cfg.boundary_policy.universal_gotchas_enabled),
            "Load universal (cross-project) gotchas",
        ),
    );
    sections.insert(
        "boundary_policy".into(),
        SectionSchema {
            description: "Cross-project boundary and access control policies".into(),
            keys: boundary,
        },
    );

    let mut secret_det = BTreeMap::new();
    secret_det.insert(
        "enabled".into(),
        key(
            "bool",
            serde_json::json!(cfg.secret_detection.enabled),
            "Enable secret/credential detection in tool outputs",
        ),
    );
    secret_det.insert(
        "redact".into(),
        key(
            "bool",
            serde_json::json!(cfg.secret_detection.redact),
            "Redact detected secrets from output",
        ),
    );
    secret_det.insert(
        "custom_patterns".into(),
        key(
            "array",
            serde_json::json!(cfg.secret_detection.custom_patterns),
            "Additional regex patterns to detect as secrets",
        ),
    );
    sections.insert(
        "secret_detection".into(),
        SectionSchema {
            description: "Secret/credential detection and redaction settings".into(),
            keys: secret_det,
        },
    );

    let mut sensitivity = BTreeMap::new();
    sensitivity.insert(
        "enabled".into(),
        key(
            "bool",
            serde_json::json!(cfg.sensitivity.enabled),
            "Enable the per-item sensitivity policy floor (no-op when false)",
        ),
    );
    sensitivity.insert(
        "policy_floor".into(),
        key(
            "string",
            serde_json::json!(cfg.sensitivity.policy_floor.as_str()),
            "Block items at/above this level: public|internal|confidential|secret",
        ),
    );
    sensitivity.insert(
        "action".into(),
        key(
            "string",
            serde_json::json!(cfg.sensitivity.action.as_str()),
            "How to enforce the floor: redact (mask spans) or drop (withhold item)",
        ),
    );
    sections.insert(
        "sensitivity".into(),
        SectionSchema {
            description: "Per-item sensitivity model with a uniform policy floor (#212)".into(),
            keys: sensitivity,
        },
    );

    let mut gateway = BTreeMap::new();
    gateway.insert(
        "enabled".into(),
        key(
            "bool",
            serde_json::json!(cfg.gateway.enabled),
            "Enable the MCP Tool-Catalog Gateway (no-op when false)",
        ),
    );
    gateway.insert(
        "top_n".into(),
        key(
            "integer",
            serde_json::json!(cfg.gateway.top_n),
            "How many tools `ctx_tools find` returns per query (clamped 1..=50)",
        ),
    );
    gateway.insert(
        "cache_ttl_secs".into(),
        key(
            "integer",
            serde_json::json!(cfg.gateway.cache_ttl_secs),
            "Aggregated-catalog cache lifetime in seconds",
        ),
    );
    gateway.insert(
        "call_timeout_secs".into(),
        key(
            "integer",
            serde_json::json!(cfg.gateway.call_timeout_secs),
            "Per-operation timeout for downstream connect/list/call (seconds)",
        ),
    );
    sections.insert(
        "gateway".into(),
        SectionSchema {
            description: "MCP Tool-Catalog Gateway: aggregate + query-route downstream MCP servers (#210). Global-only.".into(),
            keys: gateway,
        },
    );

    let mut gateway_servers = BTreeMap::new();
    gateway_servers.insert(
        "name".into(),
        key(
            "string",
            serde_json::json!(""),
            "Stable server id; becomes the catalog namespace (`name::tool`)",
        ),
    );
    gateway_servers.insert(
        "transport".into(),
        key(
            "string",
            serde_json::json!("stdio"),
            "Transport: stdio (spawn command) or http (connect to url)",
        ),
    );
    gateway_servers.insert(
        "enabled".into(),
        key(
            "bool",
            serde_json::json!(true),
            "Per-server switch (default true)",
        ),
    );
    gateway_servers.insert(
        "command".into(),
        key(
            "string",
            serde_json::json!(""),
            "Executable to spawn (stdio transport)",
        ),
    );
    gateway_servers.insert(
        "args".into(),
        key(
            "array",
            serde_json::json!([]),
            "Arguments for the spawned command (stdio transport)",
        ),
    );
    gateway_servers.insert(
        "env".into(),
        key(
            "table",
            serde_json::json!({}),
            "Extra environment variables for the child process (stdio transport)",
        ),
    );
    gateway_servers.insert(
        "url".into(),
        key(
            "string",
            serde_json::json!(""),
            "Streamable-HTTP endpoint (http transport)",
        ),
    );
    gateway_servers.insert(
        "headers".into(),
        key(
            "table",
            serde_json::json!({}),
            "Extra request headers, e.g. Authorization (http transport)",
        ),
    );
    sections.insert(
        "gateway.servers".into(),
        SectionSchema {
            description: "Downstream MCP servers (array of tables: `[[gateway.servers]]`)".into(),
            keys: gateway_servers,
        },
    );

    let mut cloud = BTreeMap::new();
    cloud.insert(
        "contribute_enabled".into(),
        key(
            "bool",
            serde_json::json!(cfg.cloud.contribute_enabled),
            "Enable contributing anonymized stats to lean-ctx cloud",
        ),
    );
    cloud.insert(
        "auto_sync".into(),
        key(
            "bool",
            serde_json::json!(cfg.cloud.auto_sync),
            "Push the Personal Cloud (knowledge, commands, CEP, gotchas, buddy, feedback) silently once per day at session end (Pro; toggle: `lean-ctx cloud autosync on|off`)",
        ),
    );
    sections.insert(
        "cloud".into(),
        SectionSchema {
            description: "Cloud feature settings".into(),
            keys: cloud,
        },
    );

    let mut gain = BTreeMap::new();
    gain.insert(
            "auto_publish".into(),
            key(
                "bool",
                serde_json::json!(cfg.gain.auto_publish),
                "Automatically (re)publish your Wrapped recap when you run `lean-ctx gain` (opt-in, off by default; throttled and sends only an aggregate payload)",
            ),
        );
    gain.insert(
        "leaderboard".into(),
        key(
            "bool",
            serde_json::json!(cfg.gain.leaderboard),
            "When auto-publishing, also list the card on the public opt-in leaderboard",
        ),
    );
    gain.insert(
        "display_name".into(),
        key(
            "string?",
            serde_json::json!(cfg.gain.display_name),
            "Optional display name shown on your published card / leaderboard entry",
        ),
    );
    gain.insert(
        "auto_publish_interval_hours".into(),
        key(
            "u64",
            serde_json::json!(cfg.gain.auto_publish_interval_hours),
            "Minimum hours between automatic publishes (throttle; default 24)",
        ),
    );
    gain.insert(
        "last_auto_publish".into(),
        key(
            "string?",
            serde_json::json!(null),
            "Timestamp of the last automatic publish (written by lean-ctx for throttling — not meant to be edited)",
        ),
    );
    sections.insert(
        "gain".into(),
        SectionSchema {
            description: "Token-savings recap publishing (gain --publish / auto-publish)".into(),
            keys: gain,
        },
    );
}