Skip to main content

lean_ctx_cloud/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2//! LeanCTX | Cloud connector: the engine side of the contract in
3//! `lean_ctx_protocol::cloud`.
4//!
5//! Rules every part keeps:
6//! - Local work is never gated: offline, disabled, a lapsed lease, a bus
7//!   `hold`/`stop`/`veto` or a damaged control file only pause Cloud traffic
8//!   ([`bus::control`]). The one exception is the organization's verified
9//!   policy ([`effective_floor`]): it is policy, not Cloud state, so its last
10//!   valid rules keep applying offline and after `not_after` (lifting them
11//!   would let a machine leave its policy by going offline), and a
12//!   company-bound project is never mapped into a personal workspace
13//!   ([`company_bound`]).
14//! - Cloud never blocks a tool call; network work runs in [`scheduler`], in
15//!   the in-process queue a tool call hands its Cloud requests to
16//!   ([`background`]) or in explicit `lean-ctx cloud …` commands.
17//! - Every outbound content item passes [`knowledge::screening`] before it
18//!   is queued in the [`outbox`].
19//! - Tokens and keys never reach logs, `Debug`, errors or crash output.
20
21pub mod auth;
22pub mod background;
23pub mod bring_local;
24pub mod bus;
25pub mod capture;
26pub mod company_bound;
27pub mod computers;
28pub mod config;
29pub mod connect;
30pub mod consent;
31pub mod cpao;
32pub mod delivery;
33pub mod disconnect;
34pub mod docs;
35pub use lean_ctx::core::policy::cloud_floor as effective_floor;
36pub mod effective_policy;
37pub mod error;
38pub mod events;
39pub mod friction;
40pub mod gateway;
41pub mod identity_v2;
42pub mod instance_key;
43pub mod keyset_v3;
44pub mod knowledge;
45pub mod lease;
46pub mod link;
47pub mod local_docs;
48pub mod mapping;
49pub mod metrics;
50pub mod metrics_v2;
51pub mod migrate;
52pub mod models_access;
53pub mod outbox;
54pub mod policy;
55pub mod policy_sessions;
56pub mod queens;
57pub mod receipts;
58pub mod replica;
59pub mod resolution;
60pub mod review;
61pub mod revoked;
62pub mod root_cert;
63pub mod runtime_lease;
64pub mod scheduler;
65pub mod session_ref;
66pub mod state;
67pub mod status;
68pub mod sync;
69pub mod tasks;
70pub mod team_decisions;
71pub mod team_stop;
72pub mod throttle;
73pub mod transport;
74pub mod unlink;
75pub mod unlinked;
76pub mod usage;
77pub use lean_ctx::core::untrusted;
78pub mod vault;
79pub mod vault_address;
80pub mod worker;
81pub mod workers;
82#[cfg(any(test, feature = "test-support"))]
83#[doc(hidden)]
84pub mod x5_guard_support;
85#[cfg(test)]
86mod x5_guard_tests;
87
88pub use config::{CloudConfig, CloudConfigExt};
89pub use error::{CloudError, Revocation, TaskRefusal};
90pub use state::CloudStore;
91
92/// Register the connector's hooks into the engine. Every entry calls this
93/// at start-up, before the first tool call (`cli::dispatch::run`, next to
94/// `proxy::register_shapers`, and the `lean-ctx-embed` engine builder).
95/// Calling it again changes nothing: the first registration wins. Every
96/// hook here is bookkeeping or telemetry and decides no refusal, except the
97/// subscription-model token source (`models_access`): without it the
98/// proxy refuses the `leanctx` provider (fail closed). The sign-in probe
99/// answers a question: the managed `cloud.require` gate in the engine
100/// decides, and refuses when no probe is registered.
101pub fn register_hooks() {
102    lean_ctx::core::config::managed::register_signed_in_probe(runtime_lease::signed_in);
103    // Config off switches, Engine receipts, the runtime lease source (it
104    // reads the Cloud lease on every request), the subscription-model token
105    // source of the proxy's `leanctx` provider.
106    lean_ctx::core::config::cloud::register_off_switches(receipts::apply_off_switches);
107    // Each receipt: queued for upload when allowed, and noted as evidence of
108    // the policy in force for this process's app session.
109    lean_ctx::core::engine_interface::receipt_sink::register_receipt_sink(
110        |root, receipt_id, invocation, observation, now| {
111            receipts::note_engine_receipt(root, receipt_id, invocation, observation, now);
112            policy_sessions::note_receipt(receipt_id, now);
113        },
114    );
115    runtime_lease::ensure_registered();
116    models_access::ensure_registered();
117    lean_ctx::core::agents::register_announce(bus::presence::request_announce);
118    // Signal recording and knowledge bookkeeping.
119    lean_ctx::core::cloud_signals::register_signal_recorder(
120        lean_ctx::core::cloud_signals::SignalRecorder {
121            recording: metrics_v2::signals::sink_open,
122            record: metrics_v2::signals::record,
123            secrets: metrics_v2::secrets::note,
124        },
125    );
126    lean_ctx::core::knowledge::hooks::register_knowledge_hooks(
127        lean_ctx::core::knowledge::hooks::KnowledgeHooks {
128            record_delivery: metrics::cloud_use::record_delivery,
129            session_ended: |project_root, session_id| {
130                knowledge::draft::on_session_event(
131                    project_root,
132                    session_id,
133                    knowledge::draft::Trigger::SessionEnd,
134                );
135                // Owner 09.11.: workers of ended app sessions do not count.
136                if let Ok(store) = state::CloudStore::open() {
137                    workers::register::on_session_end(&store);
138                }
139            },
140        },
141    );
142}
143
144// The availability gate lives in `core::cloud_gate`.
145pub use lean_ctx::core::cloud_gate::{
146    CLOUD_AVAILABLE, CLOUD_TOOLS, NOT_AVAILABLE, PREVIEW_ENV, TOOL_NOT_AVAILABLE, available_for,
147    available_locally, is_loopback,
148};
149
150#[cfg(test)]
151mod availability_tests {
152    #[test]
153    fn the_preview_never_opens_a_real_cloud() {
154        // The preview opens loopback origins only.
155        assert!(!super::is_loopback("https://cloud.leanctx.com"));
156        assert!(!super::is_loopback("https://127.0.0.1.example.com"));
157        assert!(super::is_loopback("http://127.0.0.1:4100"));
158        assert!(super::is_loopback("http://[::1]:4100"));
159        assert!(super::is_loopback("http://localhost"));
160        assert_eq!(
161            super::available_for("https://cloud.leanctx.com"),
162            super::CLOUD_AVAILABLE
163        );
164    }
165}