Skip to main content

lean_ctx/core/
cloud_gate.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Whether this engine may use LeanCTX | Cloud: the release switch, the preview
3//! switch for a loopback Cloud, and the lines printed while Cloud is off.
4//! The engine reads it to keep Cloud tools and commands out of every list;
5//! the Cloud connector re-exports it.
6
7/// Release switch: the connector shipped switched off in 3.12.0 and 3.12.1
8/// (`cloud connect`/`cloud link` refused, `cloud` hidden from help, suggestions
9/// and completions). Owner 2026-11-04: LeanCTX | Cloud opens with 3.12.2.
10pub const CLOUD_AVAILABLE: bool = true;
11/// Test switch: `LEAN_CTX_CLOUD_PREVIEW=1` opens the connector before the
12/// release, for a loopback Cloud only (the FakeCloud of the test suite).
13pub const PREVIEW_ENV: &str = "LEAN_CTX_CLOUD_PREVIEW";
14/// The one line `cloud connect`/`cloud link` print while the switch is off.
15pub const NOT_AVAILABLE: &str =
16    "LeanCTX | Cloud comes with 3.12.2; this version does not connect to a Cloud.";
17/// The one line a call to a Cloud MCP tool (`ctx_tasks`)
18/// answers while the switch is off; the tools stay out of every tool list.
19pub const TOOL_NOT_AVAILABLE: &str =
20    "LeanCTX | Cloud comes with 3.12.2; this tool is not available in this version.";
21/// Cloud MCP tools. They ship with the connector but follow its release switch
22/// ([`available_locally`], owner 28.10.): while it is off they are in no tool
23/// list and every call answers [`TOOL_NOT_AVAILABLE`].
24pub const CLOUD_TOOLS: &[&str] = &["ctx_tasks"];
25
26fn preview() -> bool {
27    std::env::var(PREVIEW_ENV).as_deref() == Ok("1")
28}
29
30/// Whether `origin` names this machine (`127.0.0.1`, `localhost`, `::1`).
31pub fn is_loopback(origin: &str) -> bool {
32    let rest = origin
33        .strip_prefix("http://")
34        .or_else(|| origin.strip_prefix("https://"))
35        .unwrap_or(origin);
36    let host = if let Some(v6) = rest.strip_prefix('[') {
37        v6.split(']').next().unwrap_or_default()
38    } else {
39        rest.split(['/', ':']).next().unwrap_or_default()
40    };
41    matches!(host, "127.0.0.1" | "localhost" | "::1")
42}
43
44/// Whether this engine may connect to the Cloud at `origin`.
45pub fn available_for(origin: &str) -> bool {
46    CLOUD_AVAILABLE || (preview() && is_loopback(origin))
47}
48
49/// Whether Cloud commands that name no origin (`cloud link`) may run.
50pub fn available_locally() -> bool {
51    CLOUD_AVAILABLE || preview()
52}