lean-ctx 3.11.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
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::process::Command;

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub(crate) struct AgentInfo {
    pub name: String,
    pub version: Option<String>,
    pub path: PathBuf,
    pub capabilities: Vec<String>,
    pub available: bool,
}

/// Shared task input for CLI connectors and the OCLA agent adapter.
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct TaskRequest {
    pub id: String,
    pub prompt: String,
    pub working_dir: PathBuf,
    pub timeout_ms: u64,
    pub model: Option<String>,
    pub max_turns: Option<u32>,
    /// Profile selected for this isolated agent invocation.
    ///
    /// Connectors pass this through as `LEAN_CTX_PROFILE` so the child agent
    /// and every LeanCTX tool it launches resolve the same profile.
    #[serde(default)]
    pub profile_name: Option<String>,
    pub profile_hash: Option<String>,
    /// Explicit host-provided child identity; never inherited from the parent.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub delivery_profile: Option<Box<ChildDeliveryProfileV1>>,
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ChildDeliveryProfileV1 {
    pub task_id: String,
    pub expires_at: chrono::DateTime<chrono::Utc>,
    pub profile: crate::core::a2a::task::delivery_authority::DeliverySigningProfileV1,
}

impl ChildDeliveryProfileV1 {
    fn environment_for(&self, request: &TaskRequest) -> Option<String> {
        if self.task_id != request.id
            || self.expires_at <= chrono::Utc::now()
            || self.profile.schema_version != 1
            || !self.profile.project_root.is_absolute()
        {
            return None;
        }
        let root = self.profile.project_root.canonicalize().ok()?;
        if self.profile.delegation.as_ref().is_some_and(|certificate| {
            certificate.execution.task_id != request.id
                || self.expires_at > certificate.expires_at
                || certificate.schema_version != 1
                || certificate.issued_at > chrono::Utc::now()
                || certificate.project_root != root
                || certificate.privacy != self.profile.privacy
                || certificate.child_agent != self.profile.agent_id
                || certificate.child_key_id != self.profile.key_id
                || certificate.recipient != self.profile.recipient
                || certificate.tenant_id != self.profile.tenant_id
                || certificate.project_id != self.profile.project_id
                || certificate.read_grant_id != self.profile.read_grant_id
                || certificate
                    .write_grant_id
                    .as_ref()
                    .is_some_and(|grant| grant != &self.profile.write_grant_id)
                || !crate::core::agent_identity::get_stored_public_key(&self.profile.agent_id)
                    .is_ok_and(|key| {
                        crate::core::agent_identity::hex_encode(key.as_bytes())
                            == certificate.child_public_key
                    })
        }) {
            return None;
        }
        if !root.is_dir() || root != request.working_dir.canonicalize().ok()? {
            return None;
        }
        let raw = serde_json::to_string(&self.profile).ok()?;
        (raw.len() <= 16_384).then_some(raw)
    }
}

pub(crate) fn apply_profile_environment(command: &mut Command, request: &TaskRequest) {
    // CLI children are distinct agents. Only an explicit, task/project-bound
    // host profile may replace the fail-closed default.
    // An invalid explicit profile disables delivery without legacy fallback;
    // removing the variable would silently re-enable unscoped delivery.
    command.env("LEAN_CTX_DELIVERY_PROFILE", "{}");
    // Child delivery permission never includes the parent's issuer configuration.
    command.env_remove("LEAN_CTX_DELIVERY_ISSUER_PROFILE");
    if let Some(profile) = &request.delivery_profile {
        if let Some(raw) = profile.environment_for(request) {
            command.env("LEAN_CTX_DELIVERY_PROFILE", raw);
        } else {
            tracing::warn!(
                event = "invalid_child_delivery_profile",
                "Child context sharing disabled: explicit profile failed launch validation"
            );
        }
    }
    if let Some(profile_name) = request
        .profile_name
        .as_deref()
        .filter(|profile_name| !profile_name.trim().is_empty())
    {
        command.env("LEAN_CTX_PROFILE", profile_name);
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct TaskResult {
    pub task_id: String,
    pub agent: String,
    pub model: String,
    pub success: bool,
    pub exit_code: i32,
    pub stdout: String,
    pub stderr: String,
    pub duration_ms: u64,
    pub tokens_used: Option<TokenUsage>,
    /// Explicit provider-reported charge, never a table-derived estimate.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub provider_cost_micros: Option<u64>,
    /// Canonical receipt emitted from explicit provider usage and cost evidence.
    /// It remains absent when CLI output does not carry both observations.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub execution_receipt_ref: Option<String>,
    /// Process outcome, independent of stderr text and provider task quality.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub termination: Option<TaskTermination>,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum TaskTermination {
    Exited,
    Signalled,
    TimedOut,
}

impl TaskTermination {
    pub(crate) fn from_process(status: std::process::ExitStatus, timed_out: bool) -> Self {
        if timed_out {
            Self::TimedOut
        } else if status.code().is_none() {
            Self::Signalled
        } else {
            Self::Exited
        }
    }
}

/// Reject controls the chosen CLI cannot enforce before any process is started.
pub(crate) fn validate_request(
    request: &TaskRequest,
    supports_turn_limit: bool,
) -> anyhow::Result<()> {
    anyhow::ensure!(!request.id.trim().is_empty(), "task ID is required");
    anyhow::ensure!(!request.prompt.trim().is_empty(), "task prompt is required");
    anyhow::ensure!(request.timeout_ms > 0, "task timeout must be positive");
    anyhow::ensure!(
        request
            .model
            .as_ref()
            .is_none_or(|model| !model.trim().is_empty()),
        "model must not be empty"
    );
    anyhow::ensure!(request.max_turns != Some(0), "turn limit must be positive");
    anyhow::ensure!(
        supports_turn_limit || request.max_turns.is_none(),
        "connector does not support a turn limit"
    );
    Ok(())
}

#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub(crate) struct TokenUsage {
    pub input_tokens: u64,
    pub output_tokens: u64,
    pub cache_read_tokens: u64,
    pub cache_write_tokens: u64,
}

pub(crate) trait AgentConnector: Send + Sync {
    fn info(&self) -> AgentInfo;
    fn health_check(&self) -> anyhow::Result<bool> {
        self.health_check_with_timeout(2_000)
    }
    fn health_check_with_timeout(&self, timeout_ms: u64) -> anyhow::Result<bool>;
    fn execute(&self, request: &TaskRequest) -> anyhow::Result<TaskResult>;
    fn name(&self) -> &'static str;
    fn supports_model_selection(&self) -> bool {
        false
    }
    fn supports_turn_limit(&self) -> bool {
        false
    }
}

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

    #[test]
    fn task_request_roundtrip() {
        let req = TaskRequest {
            id: "t1".into(),
            prompt: "Explore codebase".into(),
            working_dir: PathBuf::from("/tmp/test"),
            timeout_ms: 60_000,
            model: Some("gpt-4".into()),
            max_turns: Some(10),
            profile_name: Some("benchmark-candidate".into()),
            profile_hash: Some("abc".into()),
            delivery_profile: None,
        };
        let json = serde_json::to_string(&req).unwrap();
        let restored: TaskRequest = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.id, "t1");
        assert_eq!(
            restored.profile_name.as_deref(),
            Some("benchmark-candidate")
        );
    }

    #[test]
    fn profile_environment_is_set_only_for_named_profiles() {
        let mut command = Command::new("echo");
        let request = TaskRequest {
            id: "t1".into(),
            prompt: "Explore codebase".into(),
            working_dir: PathBuf::from("/tmp/test"),
            timeout_ms: 60_000,
            model: None,
            max_turns: None,
            profile_name: Some("benchmark-candidate".into()),
            profile_hash: None,
            delivery_profile: None,
        };

        apply_profile_environment(&mut command, &request);
        let profile = command
            .get_envs()
            .find(|(key, _)| *key == std::ffi::OsStr::new("LEAN_CTX_PROFILE"))
            .and_then(|(_, value)| value);
        assert_eq!(profile, Some(std::ffi::OsStr::new("benchmark-candidate")));
        let delivery_profile = command
            .get_envs()
            .find(|(key, _)| *key == std::ffi::OsStr::new("LEAN_CTX_DELIVERY_PROFILE"))
            .and_then(|(_, value)| value);
        assert_eq!(delivery_profile, Some(std::ffi::OsStr::new("{}")));
    }

    #[test]
    fn child_cannot_inherit_parent_delivery_authority() {
        let mut command = Command::new("echo");
        command.env("LEAN_CTX_DELIVERY_PROFILE", "parent-authority");
        let request = TaskRequest {
            id: "child-task".into(),
            prompt: "Inspect".into(),
            working_dir: PathBuf::from("/tmp/test"),
            timeout_ms: 1000,
            model: None,
            max_turns: None,
            profile_name: None,
            profile_hash: None,
            delivery_profile: None,
        };
        apply_profile_environment(&mut command, &request);
        let (_, value) = command
            .get_envs()
            .find(|(key, _)| *key == std::ffi::OsStr::new("LEAN_CTX_DELIVERY_PROFILE"))
            .unwrap();
        let value = value.unwrap().to_str().unwrap();
        assert_eq!(value, "{}");
        assert!(
            serde_json::from_str::<
                crate::core::a2a::task::delivery_authority::DeliverySigningProfileV1,
            >(value)
            .is_err()
        );
    }

    #[test]
    fn task_result_roundtrip() {
        let result = TaskResult {
            task_id: "t1".into(),
            agent: "codex".into(),
            model: "gpt-4".into(),
            success: true,
            exit_code: 0,
            stdout: "done".into(),
            stderr: String::new(),
            duration_ms: 5000,
            tokens_used: Some(TokenUsage {
                input_tokens: 1000,
                output_tokens: 200,
                cache_read_tokens: 500,
                cache_write_tokens: 100,
            }),
            provider_cost_micros: Some(1_500),
            execution_receipt_ref: Some("receipt:test".into()),
            termination: None,
        };
        let json = serde_json::to_string(&result).unwrap();
        let restored: TaskResult = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.agent, "codex");
        assert_eq!(restored.provider_cost_micros, Some(1_500));
        assert!(restored.termination.is_none());
        assert!(!json.contains("termination"));
    }

    #[test]
    fn child_delivery_profile_is_bound_to_task_project_and_expiry() {
        let _isolated = crate::core::data_dir::isolated_data_dir();
        let root = tempfile::tempdir().unwrap();
        let other = tempfile::tempdir().unwrap();
        let binding = ChildDeliveryProfileV1 {
            task_id: "child-task".into(),
            expires_at: chrono::Utc::now() + chrono::Duration::minutes(2),
            profile: crate::core::a2a::task::delivery_authority::DeliverySigningProfileV1 {
                schema_version: 1,
                agent_id: "child-agent".into(),
                delegation: None,
                recipient: "host".into(),
                tenant_id: "tenant".into(),
                project_id: "project".into(),
                project_root: root.path().to_path_buf(),
                key_id: "child-key".into(),
                read_grant_id: "child-read".into(),
                write_grant_id: "child-write".into(),
                privacy: lean_ctx_ocla::delivery_scope::DeliveryPrivacyV1::Project,
            },
        };
        let mut request = TaskRequest {
            id: "child-task".into(),
            prompt: "Inspect".into(),
            working_dir: root.path().to_path_buf(),
            timeout_ms: 1000,
            model: None,
            max_turns: None,
            profile_name: None,
            profile_hash: None,
            delivery_profile: Some(Box::new(binding.clone())),
        };
        let profile_value = |request: &TaskRequest| {
            let mut command = Command::new("echo");
            command.env("LEAN_CTX_DELIVERY_PROFILE", "parent-authority");
            command.env("LEAN_CTX_DELIVERY_ISSUER_PROFILE", "parent-issuer");
            apply_profile_environment(&mut command, request);
            assert!(command.get_envs().any(|(key, value)| key
                == std::ffi::OsStr::new("LEAN_CTX_DELIVERY_ISSUER_PROFILE")
                && value.is_none()));
            command
                .get_envs()
                .find(|(key, _)| *key == std::ffi::OsStr::new("LEAN_CTX_DELIVERY_PROFILE"))
                .unwrap()
                .1
                .unwrap()
                .to_str()
                .unwrap()
                .to_owned()
        };
        let raw = profile_value(&request);
        let profile: crate::core::a2a::task::delivery_authority::DeliverySigningProfileV1 =
            serde_json::from_str(&raw).unwrap();
        assert_eq!(profile.agent_id, "child-agent");
        assert_eq!(profile, binding.profile);
        let encoded = serde_json::to_value(&request).unwrap();
        assert_eq!(
            serde_json::from_value::<TaskRequest>(encoded.clone()).unwrap(),
            request
        );
        let mut legacy = encoded;
        legacy.as_object_mut().unwrap().remove("delivery_profile");
        assert!(
            serde_json::from_value::<TaskRequest>(legacy)
                .unwrap()
                .delivery_profile
                .is_none()
        );

        let (mut certificate, _, host_key) =
            crate::core::a2a::task::delivery_delegation::tests::fixture();
        certificate.execution.task_id = request.id.clone();
        certificate.child_agent = binding.profile.agent_id.clone();
        certificate.child_key_id = binding.profile.key_id.clone();
        certificate.recipient = binding.profile.recipient.clone();
        certificate.tenant_id = binding.profile.tenant_id.clone();
        certificate.project_id = binding.profile.project_id.clone();
        certificate.project_root = root.path().canonicalize().unwrap();
        certificate.privacy = binding.profile.privacy;
        certificate.read_grant_id = binding.profile.read_grant_id.clone();
        let child_key =
            crate::core::agent_identity::get_or_create_keypair(&certificate.child_agent).unwrap();
        certificate.child_public_key =
            crate::core::agent_identity::hex_encode(child_key.verifying_key().as_bytes());
        certificate.sign(&host_key);
        request
            .delivery_profile
            .as_mut()
            .unwrap()
            .profile
            .delegation = Some(Box::new(certificate.clone()));
        assert_ne!(profile_value(&request), "{}");
        let delegated_profile: crate::core::a2a::task::delivery_authority::DeliverySigningProfileV1 =
            serde_json::from_str(&profile_value(&request)).unwrap();
        assert_eq!(delegated_profile.delegation.as_deref(), Some(&certificate));
        for field in [
            "child_agent",
            "child_key_id",
            "recipient",
            "tenant_id",
            "project_id",
            "read_grant_id",
            "child_public_key",
        ] {
            let mut value = serde_json::to_value(&certificate).unwrap();
            value[field] = serde_json::json!("mismatch");
            request
                .delivery_profile
                .as_mut()
                .unwrap()
                .profile
                .delegation = Some(Box::new(serde_json::from_value(value).unwrap()));
            assert_eq!(profile_value(&request), "{}", "{field}");
        }
        certificate.execution.task_id = "other-attempt".into();
        certificate.sign(&host_key);
        request
            .delivery_profile
            .as_mut()
            .unwrap()
            .profile
            .delegation = Some(Box::new(certificate.clone()));
        assert_eq!(profile_value(&request), "{}");
        certificate.execution.task_id = request.id.clone();
        certificate.expires_at = binding.expires_at - chrono::Duration::seconds(1);
        certificate.sign(&host_key);
        request
            .delivery_profile
            .as_mut()
            .unwrap()
            .profile
            .delegation = Some(Box::new(certificate));
        assert_eq!(profile_value(&request), "{}");
        request.delivery_profile = Some(Box::new(binding.clone()));

        request.delivery_profile.as_mut().unwrap().profile.recipient = "x".repeat(16_384);
        assert_eq!(profile_value(&request), "{}");
        request.delivery_profile = Some(Box::new(binding.clone()));
        request.id = "different-task".into();
        assert_eq!(profile_value(&request), "{}");
        request.id = binding.task_id.clone();
        request.working_dir = other.path().to_path_buf();
        assert_eq!(profile_value(&request), "{}");
        request.working_dir = root.path().to_path_buf();
        request.delivery_profile.as_mut().unwrap().expires_at =
            chrono::Utc::now() - chrono::Duration::seconds(1);
        assert_eq!(profile_value(&request), "{}");
        request.delivery_profile = Some(Box::new(binding));
        request
            .delivery_profile
            .as_mut()
            .unwrap()
            .profile
            .schema_version = 2;
        assert_eq!(profile_value(&request), "{}");
    }
}