lean-ctx 3.9.13

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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#[cfg(any(target_os = "macos", target_os = "linux"))]
use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream};
#[cfg(any(target_os = "macos", target_os = "linux"))]
use std::path::PathBuf;
#[cfg(any(target_os = "macos", target_os = "linux"))]
use std::time::{Duration, Instant};

#[cfg(target_os = "macos")]
const PLIST_LABEL: &str = "com.leanctx.proxy";
#[cfg(target_os = "linux")]
const SYSTEMD_SERVICE: &str = "lean-ctx-proxy";

#[cfg(any(target_os = "macos", target_os = "linux", test))]
fn proxy_pid_from_health(body: &str) -> Option<u32> {
    let health: serde_json::Value = serde_json::from_str(body).ok()?;
    if health.get("status").and_then(serde_json::Value::as_str) != Some("ok") {
        return None;
    }
    let pid = u32::try_from(health.get("pid")?.as_u64()?).ok()?;
    (pid > 0).then_some(pid)
}

#[cfg(any(target_os = "macos", target_os = "linux", test))]
fn health_identifies_lean_ctx_proxy(body: &str) -> bool {
    serde_json::from_str::<serde_json::Value>(body)
        .ok()
        .and_then(|health| health.get("service")?.as_str().map(str::to_owned))
        .is_some_and(|service| service == "lean-ctx-proxy")
}

#[cfg(any(target_os = "macos", target_os = "linux"))]
fn port_is_open(port: u16) -> bool {
    let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port);
    TcpStream::connect_timeout(&addr, Duration::from_millis(150)).is_ok()
}

#[cfg(any(target_os = "macos", target_os = "linux"))]
fn proxy_pid_on_port(port: u16) -> Option<u32> {
    let health_url = format!("http://127.0.0.1:{port}/health");
    let response = ureq::get(&health_url)
        .config()
        .timeout_global(Some(Duration::from_millis(500)))
        .build()
        .call()
        .ok()?;
    let body = response.into_body().read_to_string().ok()?;
    let pid = proxy_pid_from_health(&body)?;
    (health_identifies_lean_ctx_proxy(&body)
        || crate::ipc::process::find_pids_by_name("lean-ctx").contains(&pid))
    .then_some(pid)
}

/// Stop a proxy already owning `port` before a managed service is started.
///
/// Returns `false` when the listener cannot be positively identified as a
/// lean-ctx proxy or when it does not release the port. Managed startup must
/// fail closed in either case; otherwise launchd/systemd would enter a restart
/// loop on `EADDRINUSE`.
#[cfg(any(target_os = "macos", target_os = "linux"))]
fn release_proxy_port(port: u16, quiet: bool) -> bool {
    if !port_is_open(port) {
        return true;
    }

    let Some(pid) = proxy_pid_on_port(port) else {
        if !quiet {
            eprintln!(
                "  Refusing managed proxy startup: port {port} is occupied by an unidentified service."
            );
        }
        return false;
    };
    if pid == std::process::id() {
        if !quiet {
            eprintln!("  Refusing to stop the current process while handing off port {port}.");
        }
        return false;
    }

    let _ = crate::ipc::process::terminate_gracefully(pid);
    let graceful_deadline = Instant::now() + Duration::from_secs(2);
    while Instant::now() < graceful_deadline {
        if !crate::ipc::process::is_alive(pid) && !port_is_open(port) {
            if !quiet {
                eprintln!(
                    "  Handed port {port} from standalone proxy PID {pid} to managed service."
                );
            }
            return true;
        }
        std::thread::sleep(Duration::from_millis(50));
    }

    if crate::ipc::process::is_alive(pid) {
        let _ = crate::ipc::process::force_kill(pid);
    }
    let force_deadline = Instant::now() + Duration::from_secs(1);
    while Instant::now() < force_deadline {
        if !port_is_open(port) {
            if !quiet {
                eprintln!(
                    "  Handed port {port} from standalone proxy PID {pid} to managed service."
                );
            }
            return true;
        }
        std::thread::sleep(Duration::from_millis(50));
    }

    if !quiet {
        eprintln!("  Refusing managed proxy startup: port {port} was not released by PID {pid}.");
    }
    false
}

pub fn install(port: u16, quiet: bool) -> bool {
    let binary = find_binary();
    if binary.is_empty() {
        if !quiet {
            tracing::error!("Cannot find lean-ctx binary for autostart");
        }
        return false;
    }

    #[cfg(target_os = "macos")]
    {
        install_launchagent(&binary, port, quiet)
    }

    #[cfg(target_os = "linux")]
    {
        install_systemd(&binary, port, quiet)
    }

    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        let _ = (&binary, quiet);
        println!("  Autostart not supported on this platform");
        println!("  Run manually: lean-ctx proxy start --port={port}");
        false
    }
}

pub fn stop() {
    #[cfg(target_os = "macos")]
    {
        let plist_path = launchagent_path();
        if plist_path.exists() {
            crate::core::launchd::bootout(PLIST_LABEL, &plist_path);
        }
    }

    #[cfg(target_os = "linux")]
    {
        let _ = std::process::Command::new("systemctl")
            .args(["--user", "stop", SYSTEMD_SERVICE])
            .output();
    }
}

pub fn start() -> bool {
    start_on_port(crate::proxy_setup::default_port())
}

/// Start the installed manager after atomically taking ownership of `port`.
/// This also handles the case where an IDE/dashboard spawned a standalone
/// proxy while the manager was temporarily unloaded.
pub fn start_on_port(port: u16) -> bool {
    #[cfg(target_os = "macos")]
    {
        let plist_path = launchagent_path();
        if plist_path.exists() {
            crate::core::launchd::bootout(PLIST_LABEL, &plist_path);
            if !release_proxy_port(port, false) {
                return false;
            }
            return crate::core::launchd::bootstrap(PLIST_LABEL, &plist_path);
        }
        false
    }

    #[cfg(target_os = "linux")]
    {
        let _ = std::process::Command::new("systemctl")
            .args(["--user", "stop", SYSTEMD_SERVICE])
            .output();
        if release_proxy_port(port, false) {
            std::process::Command::new("systemctl")
                .args(["--user", "start", SYSTEMD_SERVICE])
                .status()
                .is_ok_and(|status| status.success())
        } else {
            false
        }
    }

    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        let _ = port;
        false
    }
}

pub fn uninstall(_quiet: bool) {
    #[cfg(target_os = "macos")]
    uninstall_launchagent(_quiet);

    #[cfg(target_os = "linux")]
    uninstall_systemd(_quiet);
}

/// Whether this platform has a proxy-autostart backend (LaunchAgent on macOS,
/// systemd user service on Linux). Windows and other targets have none, so a
/// missing autostart there must not be treated as a failure by `doctor` (#416).
pub fn is_supported() -> bool {
    cfg!(any(target_os = "macos", target_os = "linux"))
}

/// Returns true if the proxy autostart is installed (plist/systemd service file exists).
pub fn is_installed() -> bool {
    #[cfg(target_os = "macos")]
    {
        launchagent_path().exists()
    }
    #[cfg(target_os = "linux")]
    {
        systemd_path().exists()
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        false
    }
}

/// Returns true when the installed manager is actively responsible for the proxy.
/// Callers must not spawn a second foreground proxy while this is true.
pub fn is_loaded() -> bool {
    #[cfg(target_os = "macos")]
    {
        is_installed() && crate::core::launchd::is_loaded(PLIST_LABEL)
    }
    #[cfg(target_os = "linux")]
    {
        is_installed()
            && std::process::Command::new("systemctl")
                .args(["--user", "is-active", "--quiet", SYSTEMD_SERVICE])
                .status()
                .is_ok_and(|status| status.success())
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        false
    }
}

pub fn status() {
    #[cfg(target_os = "macos")]
    {
        let plist_path = launchagent_path();
        if plist_path.exists() {
            println!("  LaunchAgent: installed at {}", plist_path.display());
            if crate::core::launchd::is_loaded(PLIST_LABEL) {
                println!("  Status: loaded");
            } else {
                println!("  Status: not loaded (run: lean-ctx proxy start)");
            }
        } else {
            println!("  LaunchAgent: not installed");
        }
    }

    #[cfg(target_os = "linux")]
    {
        let service_path = systemd_path();
        if service_path.exists() {
            println!("  systemd user service: installed");
            let output = std::process::Command::new("systemctl")
                .args(["--user", "is-active", SYSTEMD_SERVICE])
                .output();
            match output {
                Ok(o) => {
                    let state = String::from_utf8_lossy(&o.stdout).trim().to_string();
                    println!("  Status: {state}");
                }
                Err(_) => println!("  Status: unknown"),
            }
        } else {
            println!("  systemd service: not installed");
        }
    }

    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        println!("  Autostart not available on this platform");
    }
}

#[cfg(target_os = "macos")]
fn launchagent_path() -> PathBuf {
    dirs::home_dir()
        .unwrap_or_else(|| PathBuf::from("/tmp"))
        .join("Library/LaunchAgents")
        .join(format!("{PLIST_LABEL}.plist"))
}

#[cfg(target_os = "macos")]
fn install_launchagent(binary: &str, port: u16, quiet: bool) -> bool {
    let plist_dir = dirs::home_dir()
        .unwrap_or_else(|| PathBuf::from("/tmp"))
        .join("Library/LaunchAgents");
    let _ = std::fs::create_dir_all(&plist_dir);

    let plist_path = plist_dir.join(format!("{PLIST_LABEL}.plist"));
    // Stop the registered job first, then take over any standalone proxy an
    // IDE/dashboard may have spawned while launchd was unloaded. Starting the
    // new job with an occupied port causes an unconditional KeepAlive loop.
    crate::core::launchd::bootout(PLIST_LABEL, &plist_path);
    if !release_proxy_port(port, quiet) {
        return false;
    }
    // GH #439: proxy logs are STATE — resolve through the typed dir so a
    // post-split install writes to $XDG_STATE_HOME/lean-ctx/logs instead of a
    // re-created ~/.lean-ctx. Legacy single-dir installs still resolve here.
    let log_dir = crate::core::paths::state_dir()
        .unwrap_or_else(|_| std::env::temp_dir().join("lean-ctx"))
        .join("logs");
    let _ = std::fs::create_dir_all(&log_dir);

    // #356: wrap the launchd invocation in a deny-~/Documents seatbelt sandbox
    // so the proxy (a TCC-standalone process) can never trip the privacy prompt.
    let port_arg = format!("--port={port}");
    let program_args = crate::core::tcc_guard_sandbox::program_args_xml(
        &crate::core::tcc_guard_sandbox::wrap_launchd_args(binary, &["proxy", "start", &port_arg]),
        "        ",
    );

    // #449: pin the directory layout. A launchd-spawned proxy inherits only
    // launchd's minimal environment (no HOME, no XDG vars), so it resolves a
    // *different* config/data dir than the CLI that installed it — it never sees
    // the user's config.toml edits (live-upstream reload reads nothing) and
    // derives a mismatched session token. Bake the exact dirs this CLI resolves
    // into the plist so the managed proxy always agrees with the CLI.
    let env_vars = crate::core::tcc_guard_sandbox::pinned_layout_env_xml();

    let plist = format!(
        r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>{PLIST_LABEL}</string>
    <key>ProgramArguments</key>
    <array>
{program_args}
    </array>
{env_vars}    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardOutPath</key>
    <string>{stdout}</string>
    <key>StandardErrorPath</key>
    <string>{stderr}</string>
</dict>
</plist>"#,
        stdout = log_dir.join("proxy.stdout.log").display(),
        stderr = log_dir.join("proxy.stderr.log").display(),
    );

    let _ = std::fs::write(&plist_path, &plist);

    let ok = crate::core::launchd::bootstrap(PLIST_LABEL, &plist_path);

    if !quiet {
        if ok {
            println!("  Installed LaunchAgent: {}", plist_path.display());
            println!("  Proxy will start on login and restart if stopped");
        } else {
            println!("  Created LaunchAgent at {}", plist_path.display());
            println!("  Load reported a problem; check: launchctl print {PLIST_LABEL}");
        }
    }
    ok
}

#[cfg(target_os = "macos")]
fn uninstall_launchagent(quiet: bool) {
    let plist_path = launchagent_path();
    if !plist_path.exists() {
        if !quiet {
            println!("  LaunchAgent not installed, nothing to remove");
        }
        return;
    }

    crate::core::launchd::bootout(PLIST_LABEL, &plist_path);

    let _ = std::fs::remove_file(&plist_path);
    if !quiet {
        println!("  Removed LaunchAgent: {}", plist_path.display());
    }
}

#[cfg(target_os = "linux")]
fn systemd_path() -> PathBuf {
    dirs::home_dir()
        .unwrap_or_else(|| PathBuf::from("/tmp"))
        .join(".config/systemd/user")
        .join(format!("{SYSTEMD_SERVICE}.service"))
}

#[cfg(target_os = "linux")]
fn install_systemd(binary: &str, port: u16, quiet: bool) -> bool {
    let service_dir = dirs::home_dir()
        .unwrap_or_else(|| PathBuf::from("/tmp"))
        .join(".config/systemd/user");
    let _ = std::fs::create_dir_all(&service_dir);

    let service_path = service_dir.join(format!("{SYSTEMD_SERVICE}.service"));

    let _ = std::process::Command::new("systemctl")
        .args(["--user", "stop", SYSTEMD_SERVICE])
        .output();
    if !release_proxy_port(port, quiet) {
        return false;
    }

    let unit = format!(
        r"[Unit]
Description=lean-ctx API Proxy
After=network.target
StartLimitIntervalSec=300
StartLimitBurst=5

[Service]
Type=simple
ExecStart={binary} proxy start --port={port}
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
Environment=RUST_LOG=info

[Install]
WantedBy=default.target
"
    );

    let _ = std::fs::write(&service_path, &unit);

    let _ = std::process::Command::new("systemctl")
        .args(["--user", "daemon-reload"])
        .output();

    let result = std::process::Command::new("systemctl")
        .args(["--user", "enable", "--now", SYSTEMD_SERVICE])
        .output();

    if !quiet {
        match &result {
            Ok(o) if o.status.success() => {
                println!("  Installed systemd user service: {SYSTEMD_SERVICE}");
                println!("  Proxy will start on login and restart if stopped");
            }
            Ok(o) => {
                let err = String::from_utf8_lossy(&o.stderr);
                println!("  Created service file but enable failed: {err}");
            }
            Err(e) => {
                println!("  Created service file at {}", service_path.display());
                println!("  Could not enable: {e}");
            }
        }
    }
    result.is_ok_and(|output| output.status.success())
}

#[cfg(target_os = "linux")]
fn uninstall_systemd(quiet: bool) {
    let service_path = systemd_path();
    if !service_path.exists() {
        if !quiet {
            println!("  systemd service not installed, nothing to remove");
        }
        return;
    }

    let _ = std::process::Command::new("systemctl")
        .args(["--user", "stop", SYSTEMD_SERVICE])
        .output();
    let _ = std::process::Command::new("systemctl")
        .args(["--user", "disable", SYSTEMD_SERVICE])
        .output();
    let _ = std::fs::remove_file(&service_path);
    let _ = std::process::Command::new("systemctl")
        .args(["--user", "daemon-reload"])
        .output();

    if !quiet {
        println!("  Removed systemd service: {SYSTEMD_SERVICE}");
    }
}

pub fn find_binary() -> String {
    crate::core::portable_binary::resolve_portable_binary()
}

#[cfg(test)]
mod tests {
    use super::{health_identifies_lean_ctx_proxy, proxy_pid_from_health};
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    use super::{port_is_open, release_proxy_port};
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    use std::io::{Read, Write};
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    use std::net::TcpListener;

    #[test]
    fn health_pid_parser_accepts_only_well_formed_health() {
        assert_eq!(
            proxy_pid_from_health(r#"{"status":"ok","pid":4242}"#),
            Some(4242)
        );
        assert_eq!(
            proxy_pid_from_health(r#"{"status":"busy","pid":4242}"#),
            None
        );
        assert_eq!(proxy_pid_from_health(r#"{"status":"ok","pid":0}"#), None);
        assert_eq!(proxy_pid_from_health(r#"{"status":"ok"}"#), None);
        assert_eq!(proxy_pid_from_health("not json"), None);
        assert!(health_identifies_lean_ctx_proxy(
            r#"{"status":"ok","service":"lean-ctx-proxy","pid":4242}"#
        ));
        assert!(!health_identifies_lean_ctx_proxy(
            r#"{"status":"ok","pid":4242}"#
        ));
    }

    #[test]
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    fn managed_handoff_fails_closed_for_unidentified_listener() {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let port = listener.local_addr().unwrap().port();
        let server = std::thread::spawn(move || {
            for _ in 0..2 {
                let (mut stream, _) = listener.accept().unwrap();
                stream
                    .set_read_timeout(Some(std::time::Duration::from_secs(1)))
                    .unwrap();
                let mut request = [0_u8; 512];
                if stream.read(&mut request).unwrap_or(0) > 0 {
                    let body = r#"{"status":"ok"}"#;
                    write!(
                        stream,
                        "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
                        body.len()
                    )
                    .unwrap();
                }
            }
        });

        assert!(!release_proxy_port(port, true));
        server.join().unwrap();
    }

    #[cfg(unix)]
    struct ChildGuard(std::process::Child);

    #[cfg(unix)]
    impl Drop for ChildGuard {
        fn drop(&mut self) {
            let _ = self.0.kill();
            let _ = self.0.wait();
        }
    }

    #[test]
    #[cfg(unix)]
    fn managed_handoff_stops_identified_proxy_and_releases_port() {
        let reservation = TcpListener::bind("127.0.0.1:0").unwrap();
        let port = reservation.local_addr().unwrap().port();
        drop(reservation);

        let child = std::process::Command::new(std::env::current_exe().unwrap())
            .args([
                "--ignored",
                "--exact",
                "proxy_autostart::tests::managed_handoff_test_child",
                "--nocapture",
            ])
            .env("LEAN_CTX_HANDOFF_TEST_PORT", port.to_string())
            .spawn()
            .unwrap();
        let mut child = ChildGuard(child);

        let ready_deadline = std::time::Instant::now() + std::time::Duration::from_secs(3);
        while !port_is_open(port) && std::time::Instant::now() < ready_deadline {
            std::thread::sleep(std::time::Duration::from_millis(25));
        }
        assert!(port_is_open(port), "test proxy did not bind port {port}");
        assert!(release_proxy_port(port, true));
        let status = child.0.wait().unwrap();
        assert!(
            !status.success(),
            "handoff must terminate the standalone proxy"
        );
        assert!(!port_is_open(port), "handoff must release port {port}");
    }

    #[test]
    #[ignore = "helper process for managed_handoff_stops_identified_proxy_and_releases_port"]
    #[cfg(unix)]
    fn managed_handoff_test_child() {
        let port: u16 = std::env::var("LEAN_CTX_HANDOFF_TEST_PORT")
            .unwrap()
            .parse()
            .unwrap();
        let listener = TcpListener::bind(("127.0.0.1", port)).unwrap();
        loop {
            let (mut stream, _) = listener.accept().unwrap();
            stream
                .set_read_timeout(Some(std::time::Duration::from_secs(1)))
                .unwrap();
            let mut request = [0_u8; 512];
            if stream.read(&mut request).unwrap_or(0) > 0 {
                let body = format!(
                    r#"{{"status":"ok","service":"lean-ctx-proxy","pid":{}}}"#,
                    std::process::id()
                );
                write!(
                    stream,
                    "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
                    body.len()
                )
                .unwrap();
            }
        }
    }
}