lean-ctx 3.12.0

LeanCTX Engine: the open-source runtime for the Context Gateway for AI Systems. CLI, MCP server, and supported proxy integrations.
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
// SPDX-License-Identifier: Apache-2.0
//! Which local Cockpit server belongs to this data dir, and how a new
//! `lean-ctx cockpit` decides between reusing, replacing and starting one.
//!
//! The server records itself in `dashboard.launch.json` in the state dir
//! (PID, process identity, port, version, foreground or background) right
//! after it bound its port and removes the record when it ends. The record
//! holds no secret: the bearer token stays in `dashboard.token` (0600).
//!
//! A start first looks at the record, then at the port: a healthy instance
//! of this data dir is reused; a hung, token-rejecting or outdated
//! background instance of this data dir is replaced (stopped by its
//! recorded PID, never by name); a port held by another program or another
//! LeanCTX data dir is skipped for the next free one unless the port was
//! asked for explicitly. [`plan`] is that decision, free of I/O.

use std::io::{Read, Write};
use std::net::{SocketAddr, TcpStream};
use std::path::PathBuf;
use std::time::{Duration, Instant};

use serde::{Deserialize, Serialize};

use crate::ipc::process::ProcessIdentity;

/// The record file in the state dir.
pub(crate) const RECORD_FILE: &str = "dashboard.launch.json";
/// Set by a background start for its server child.
pub(crate) const DETACHED_ENV: &str = "LEAN_CTX_COCKPIT_DETACHED";
/// How many ports after the default a start tries when it is taken.
pub(crate) const PORT_SCAN: u16 = 20;
/// One health probe: connect, request and read.
pub(crate) const PROBE_TIMEOUT: Duration = Duration::from_millis(400);

/// Where the Cockpit server of this data dir runs.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct LaunchRecord {
    pub(crate) schema_version: u32,
    pub(crate) pid: u32,
    /// Defends the PID against reuse by an unrelated process.
    pub(crate) identity: Option<ProcessIdentity>,
    pub(crate) host: String,
    pub(crate) port: u16,
    pub(crate) base_path: String,
    pub(crate) version: String,
    /// Started by `lean-ctx cockpit` in the background (stoppable with
    /// `lean-ctx cockpit stop`), not in someone's terminal.
    pub(crate) detached: bool,
}

impl LaunchRecord {
    /// The record of this process, serving `host:port`.
    pub(crate) fn of_this_process(host: &str, port: u16, base_path: &str) -> Self {
        let pid = std::process::id();
        Self {
            schema_version: 1,
            pid,
            identity: crate::ipc::process::identity(pid),
            host: host.to_string(),
            port,
            base_path: base_path.to_string(),
            version: env!("CARGO_PKG_VERSION").to_string(),
            detached: std::env::var_os(DETACHED_ENV).is_some(),
        }
    }

    /// The recorded process still runs (and is the one recorded, when the
    /// platform can tell).
    pub(crate) fn process_alive(&self) -> bool {
        if !crate::ipc::process::is_alive(self.pid) {
            return false;
        }
        match (&self.identity, crate::ipc::process::identity(self.pid)) {
            (Some(recorded), Some(current)) => *recorded == current,
            _ => true,
        }
    }
}

fn record_path() -> Option<PathBuf> {
    crate::core::paths::state_dir()
        .ok()
        .map(|dir| dir.join(RECORD_FILE))
}

pub(crate) fn read_record() -> Option<LaunchRecord> {
    let bytes = std::fs::read(record_path()?).ok()?;
    if bytes.len() > 16 * 1024 {
        return None;
    }
    serde_json::from_slice::<LaunchRecord>(&bytes)
        .ok()
        .filter(|record| record.schema_version == 1)
}

pub(crate) fn write_record(record: &LaunchRecord) {
    let Some(path) = record_path() else {
        return;
    };
    if let Some(dir) = path.parent() {
        let _ = std::fs::create_dir_all(dir);
    }
    if let Ok(bytes) = serde_json::to_vec_pretty(record) {
        let _ = crate::core::updater::atomic_write_bytes(&path, &bytes);
    }
}

/// Remove the record when it names `pid` (a newer server may own it).
pub(crate) fn remove_record_of(pid: u32) {
    if read_record().is_some_and(|record| record.pid == pid)
        && let Some(path) = record_path()
    {
        let _ = std::fs::remove_file(path);
    }
}

/// What answers on a port.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Health {
    /// A LeanCTX Cockpit server that accepted this data dir's token; its
    /// engine version.
    Healthy { version: String },
    /// A LeanCTX Cockpit server that rejected the token: another data dir,
    /// or a pinned token this data dir does not hold.
    Rejected,
    /// Something else answers, or nothing answers in time.
    Foreign,
    /// Nothing listens.
    Closed,
}

/// Classify the raw response to `GET /api/version`.
pub(crate) fn classify_response(response: &str) -> Health {
    let status_ok = response.starts_with("HTTP/1.1 200") || response.starts_with("HTTP/1.0 200");
    let ours = response.contains(r#""current":"#)
        && response.contains(r#""latest":"#)
        && response.contains(r#""update_available":"#);
    if status_ok && ours {
        let version = response
            .split(r#""current":""#)
            .nth(1)
            .and_then(|rest| rest.split('"').next())
            .unwrap_or_default()
            .to_string();
        return Health::Healthy { version };
    }
    if response.starts_with("HTTP/1.1 401") && response.contains(r#"{"error":"unauthorized"}"#) {
        return Health::Rejected;
    }
    Health::Foreign
}

/// The loopback address that reaches a server bound to `host:port`.
pub(crate) fn probe_address(host: &str, port: u16) -> SocketAddr {
    let ip = match host.trim_matches(|c| c == '[' || c == ']') {
        "::1" | "::" => std::net::IpAddr::V6(std::net::Ipv6Addr::LOCALHOST),
        other => other
            .parse::<std::net::IpAddr>()
            .ok()
            .filter(|ip| !ip.is_unspecified())
            .unwrap_or(std::net::IpAddr::V4(std::net::Ipv4Addr::LOCALHOST)),
    };
    SocketAddr::new(ip, port)
}

/// Probe `GET /api/version` on `host:port` with `token`.
pub(crate) fn probe(host: &str, port: u16, token: Option<&str>) -> Health {
    let address = probe_address(host, port);
    let mut stream = match TcpStream::connect_timeout(&address, PROBE_TIMEOUT) {
        Ok(stream) => stream,
        Err(error) if error.kind() == std::io::ErrorKind::ConnectionRefused => {
            return Health::Closed;
        }
        Err(_) => return Health::Foreign,
    };
    let _ = stream.set_read_timeout(Some(PROBE_TIMEOUT));
    let _ = stream.set_write_timeout(Some(PROBE_TIMEOUT));
    let auth = token
        .filter(|t| !t.is_empty())
        .map(|t| format!("Authorization: Bearer {t}\r\n"))
        .unwrap_or_default();
    let request =
        format!("GET /api/version HTTP/1.1\r\nHost: localhost\r\n{auth}Connection: close\r\n\r\n");
    if stream.write_all(request.as_bytes()).is_err() {
        return Health::Foreign;
    }
    // Read until the peer closes, bounded so a rogue peer can't stream forever.
    let mut response = Vec::new();
    let mut buf = [0u8; 1024];
    while response.len() < 8 * 1024 {
        match stream.read(&mut buf) {
            Ok(0) | Err(_) => break,
            Ok(n) => response.extend_from_slice(&buf[..n]),
        }
    }
    classify_response(&String::from_utf8_lossy(&response))
}

/// `probe`, retried for `patience` while it does not answer healthy: a
/// server that just started or is busy gets a moment before it counts as
/// hung.
pub(crate) fn probe_patiently(
    host: &str,
    port: u16,
    token: impl Fn() -> Option<String>,
    patience: Duration,
) -> Health {
    let deadline = Instant::now() + patience;
    loop {
        let health = probe(host, port, token().as_deref());
        if matches!(health, Health::Healthy { .. }) || Instant::now() >= deadline {
            return health;
        }
        std::thread::sleep(Duration::from_millis(200));
    }
}

/// Nothing listens on `port`, on IPv4 or IPv6 loopback — so the browser's
/// `localhost` cannot reach another program instead of the new server.
pub(crate) fn port_free(port: u16) -> bool {
    let v4 = probe_address("127.0.0.1", port);
    let v6 = probe_address("::1", port);
    let refused = |address: &SocketAddr| match TcpStream::connect_timeout(address, PROBE_TIMEOUT) {
        Ok(_) => false,
        Err(error) => !matches!(error.kind(), std::io::ErrorKind::TimedOut),
    };
    refused(&v4) && refused(&v6)
}

/// This data dir's live recorded instance, probed with a little patience.
/// A record whose process is gone is removed.
pub(crate) fn observe(record: Option<&LaunchRecord>) -> Option<RecordView> {
    let record = record?;
    if !record.process_alive() {
        remove_record_of(record.pid);
        return None;
    }
    let health = probe_patiently(
        &record.host,
        record.port,
        super::load_saved_token,
        Duration::from_secs(2),
    );
    Some(RecordView {
        pid: record.pid,
        port: record.port,
        detached: record.detached,
        health,
    })
}

/// [`plan`] for this data dir now, with the record it saw.
pub(crate) fn plan_now(requested: Option<u16>) -> (Plan, Option<LaunchRecord>) {
    let record = read_record();
    let view = observe(record.as_ref());
    let token = super::load_saved_token();
    let decided = plan(
        view.as_ref(),
        requested,
        super::DEFAULT_PORT,
        |port| probe("127.0.0.1", port, token.as_deref()),
        port_free,
    );
    (decided, record.filter(|_| view.is_some()))
}

/// Stop a recorded instance by its PID — SIGTERM, then a kill after a
/// grace period. Never by name; a PID whose process identity changed is
/// left alone. True when it is gone.
pub(crate) fn stop_instance(record: &LaunchRecord) -> bool {
    use crate::ipc::process::{force_kill, is_alive, terminate_gracefully};
    if !record.process_alive() {
        remove_record_of(record.pid);
        return true;
    }
    let _ = terminate_gracefully(record.pid);
    let gone = |within: Duration| {
        let deadline = Instant::now() + within;
        while Instant::now() < deadline {
            if !is_alive(record.pid) {
                return true;
            }
            std::thread::sleep(Duration::from_millis(100));
        }
        !is_alive(record.pid)
    };
    let stopped = gone(Duration::from_secs(5)) || {
        let _ = force_kill(record.pid);
        gone(Duration::from_secs(2))
    };
    if stopped {
        remove_record_of(record.pid);
    }
    stopped
}

/// What a start knows about the recorded instance of this data dir.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct RecordView {
    pub(crate) pid: u32,
    pub(crate) port: u16,
    pub(crate) detached: bool,
    pub(crate) health: Health,
}

/// What a start does.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Plan {
    /// Open the healthy instance on `port`; `other_version` when it runs a
    /// different engine version than this one (a foreground instance is
    /// never stopped for that).
    Reuse {
        port: u16,
        other_version: Option<String>,
    },
    /// Stop this data dir's background instance (`pid`), then start on
    /// `port`.
    Replace { pid: u32, port: u16 },
    /// Start on `port`; `skipped` names the taken port it moved past.
    Start { port: u16, skipped: Option<u16> },
    /// The explicitly requested `port` (or every scanned one) is taken.
    PortTaken { port: u16, by: Health },
}

/// Decide a start. `record` is this data dir's live recorded instance (a
/// dead one is `None`); `requested` an explicit `--port`/`LEAN_CTX_PORT`;
/// `port_health` probes a port; `free` says whether nothing listens there.
pub(crate) fn plan(
    record: Option<&RecordView>,
    requested: Option<u16>,
    default_port: u16,
    port_health: impl Fn(u16) -> Health,
    free: impl Fn(u16) -> bool,
) -> Plan {
    let ours = env!("CARGO_PKG_VERSION");
    if let Some(record) = record
        && requested.is_none_or(|port| port == record.port)
    {
        match &record.health {
            Health::Healthy { version } if version == ours || !record.detached => {
                return Plan::Reuse {
                    port: record.port,
                    other_version: (version != ours).then(|| version.clone()),
                };
            }
            // Outdated, hung or token-rejecting: ours to restart.
            _ if record.detached => {
                return Plan::Replace {
                    pid: record.pid,
                    port: record.port,
                };
            }
            // Someone's foreground instance that does not answer: leave it
            // and pick a port below.
            _ => {}
        }
    }
    let base = requested.unwrap_or(default_port);
    let skip_foreground = record
        .filter(|record| !record.detached && !matches!(record.health, Health::Healthy { .. }))
        .map(|record| record.port);
    let health = if skip_foreground == Some(base) {
        Health::Foreign
    } else {
        port_health(base)
    };
    match health {
        Health::Healthy { version } => Plan::Reuse {
            port: base,
            other_version: (version != ours).then_some(version),
        },
        Health::Closed if free(base) => Plan::Start {
            port: base,
            skipped: None,
        },
        by if requested.is_some() => Plan::PortTaken {
            port: base,
            by: if by == Health::Closed {
                Health::Foreign
            } else {
                by
            },
        },
        by => (1..=PORT_SCAN)
            .filter_map(|step| base.checked_add(step))
            .find(|port| Some(*port) != skip_foreground && free(*port))
            .map_or(
                Plan::PortTaken {
                    port: base,
                    by: by.clone(),
                },
                |port| Plan::Start {
                    port,
                    skipped: Some(base),
                },
            ),
    }
}

/// One line for a port another program or data dir holds.
pub(crate) fn port_taken_message(port: u16, by: &Health) -> String {
    let who = match by {
        Health::Rejected => "another LeanCTX Cockpit (another data dir or a pinned token)",
        _ => "another program",
    };
    format!("Port {port} is used by {who} — choose another one: lean-ctx cockpit --port <N>")
}

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

    const OURS: &str = env!("CARGO_PKG_VERSION");

    fn healthy(version: &str) -> Health {
        Health::Healthy {
            version: version.to_string(),
        }
    }

    fn view(detached: bool, health: Health) -> RecordView {
        RecordView {
            pid: 42,
            port: 3333,
            detached,
            health,
        }
    }

    fn nothing_listens(_: u16) -> Health {
        Health::Closed
    }

    #[test]
    fn version_response_is_classified_without_trusting_lookalikes() {
        let ok = format!(
            "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n\
             {{\"current\":\"{OURS}\",\"latest\":\"{OURS}\",\"update_available\":false}}"
        );
        assert_eq!(classify_response(&ok), healthy(OURS));
        assert_eq!(
            classify_response(
                "HTTP/1.1 401 Unauthorized\r\nWWW-Authenticate: Bearer\r\n\r\n{\"error\":\"unauthorized\"}"
            ),
            Health::Rejected
        );
        assert_eq!(
            classify_response("HTTP/1.1 200 OK\r\n\r\n<html>other app</html>"),
            Health::Foreign
        );
        assert_eq!(
            classify_response("HTTP/1.1 401 Unauthorized\r\n\r\n"),
            Health::Foreign
        );
        assert_eq!(classify_response(""), Health::Foreign);
    }

    #[test]
    fn a_healthy_instance_of_this_data_dir_is_reused() {
        let record = view(true, healthy(OURS));
        assert_eq!(
            plan(Some(&record), None, 3333, nothing_listens, |_| true),
            Plan::Reuse {
                port: 3333,
                other_version: None
            }
        );
        // Without a record, a port that accepts our token is ours as well.
        assert_eq!(
            plan(None, None, 3333, |_| healthy(OURS), |_| false),
            Plan::Reuse {
                port: 3333,
                other_version: None
            }
        );
    }

    #[test]
    fn an_outdated_or_hung_background_instance_is_replaced_but_a_foreground_one_never() {
        for health in [healthy("0.0.1"), Health::Rejected, Health::Foreign] {
            assert_eq!(
                plan(
                    Some(&view(true, health.clone())),
                    None,
                    3333,
                    nothing_listens,
                    |_| true
                ),
                Plan::Replace {
                    pid: 42,
                    port: 3333
                },
                "{health:?}"
            );
        }
        assert_eq!(
            plan(
                Some(&view(false, healthy("0.0.1"))),
                None,
                3333,
                nothing_listens,
                |_| true
            ),
            Plan::Reuse {
                port: 3333,
                other_version: Some("0.0.1".into())
            }
        );
        // A hung foreground instance keeps its port; the start moves on.
        assert_eq!(
            plan(
                Some(&view(false, Health::Foreign)),
                None,
                3333,
                nothing_listens,
                |p| p != 3333
            ),
            Plan::Start {
                port: 3334,
                skipped: Some(3333)
            }
        );
    }

    #[test]
    fn a_taken_default_port_moves_to_the_next_free_one() {
        for by in [Health::Foreign, Health::Rejected] {
            let by_ref = by.clone();
            assert_eq!(
                plan(
                    None,
                    None,
                    3333,
                    move |p| if p == 3333 {
                        by_ref.clone()
                    } else {
                        Health::Closed
                    },
                    |p| p > 3334
                ),
                Plan::Start {
                    port: 3335,
                    skipped: Some(3333)
                },
                "{by:?}"
            );
        }
        // IPv6-only listener: refused on 127.0.0.1 but not free.
        assert_eq!(
            plan(None, None, 3333, nothing_listens, |p| p != 3333),
            Plan::Start {
                port: 3334,
                skipped: Some(3333)
            }
        );
        assert_eq!(
            plan(None, None, 3333, |_| Health::Foreign, |_| false),
            Plan::PortTaken {
                port: 3333,
                by: Health::Foreign
            }
        );
    }

    #[test]
    fn an_explicit_port_is_never_swapped_silently() {
        assert_eq!(
            plan(None, Some(4000), 3333, |_| Health::Rejected, |_| true),
            Plan::PortTaken {
                port: 4000,
                by: Health::Rejected
            }
        );
        assert_eq!(
            plan(None, Some(4000), 3333, nothing_listens, |_| true),
            Plan::Start {
                port: 4000,
                skipped: None
            }
        );
        // A recorded instance on another port is left alone.
        assert_eq!(
            plan(
                Some(&view(true, healthy(OURS))),
                Some(4000),
                3333,
                nothing_listens,
                |_| true
            ),
            Plan::Start {
                port: 4000,
                skipped: None
            }
        );
    }

    #[test]
    fn probe_addresses_use_loopback_for_wildcards_and_keep_ipv6() {
        assert_eq!(probe_address("0.0.0.0", 1).to_string(), "127.0.0.1:1");
        assert_eq!(probe_address("localhost", 1).to_string(), "127.0.0.1:1");
        assert_eq!(probe_address("::1", 1).to_string(), "[::1]:1");
        assert_eq!(probe_address("[::]", 1).to_string(), "[::1]:1");
    }

    #[test]
    fn the_record_round_trips_and_carries_no_token() {
        let record = LaunchRecord::of_this_process("127.0.0.1", 3333, "");
        let json = serde_json::to_string(&record).unwrap();
        assert!(!json.contains("token"));
        assert_eq!(serde_json::from_str::<LaunchRecord>(&json).unwrap(), record);
        assert!(record.process_alive());
    }

    #[test]
    fn port_taken_names_the_fix() {
        let line = port_taken_message(3333, &Health::Rejected);
        assert!(line.contains("another LeanCTX Cockpit") && line.contains("--port"));
        assert!(port_taken_message(3333, &Health::Foreign).contains("another program"));
    }
}