lean-ctx-engine 3.12.1

LeanCTX | OSS library: the open-source runtime for the Context Gateway for AI agents behind the lean-ctx CLI, MCP server and 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
//! Native LeanCTX context optimization adapter.

use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use std::time::Instant;

use lean_ctx_protocol::CapabilityManifestV1;
use sha2::{Digest, Sha256};

use super::read_context_paths;
use crate::core::compressor;
use crate::core::ocla::invocation::{
    CapabilityAdapter, CapabilityInput, CapabilityInvocation, CapabilityObservationV1,
    CapabilityResult, evidence_ref,
};
use crate::core::ocla::{OclaError, OclaResult};
use crate::core::tokens;

/// Repository-relative manifest used by this adapter.
pub const MANIFEST_PATH: &str =
    "docs/contracts/ocla/capability-manifests/leanctx/context-optimization-v1.json";
const MANIFEST_JSON: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/assets/ocla/capability-manifests/leanctx/context-optimization-v1.json"
));

pub(crate) fn manifest() -> &'static CapabilityManifestV1 {
    static MANIFEST: OnceLock<CapabilityManifestV1> = OnceLock::new();
    MANIFEST.get_or_init(|| {
        let manifest: CapabilityManifestV1 =
            serde_json::from_str(MANIFEST_JSON).expect("native context manifest is valid JSON");
        manifest
            .validate()
            .expect("native context manifest satisfies the protocol");
        manifest
    })
}

/// Local adapter that combines existing file reads and compression primitives.
pub struct NativeContextAdapter {
    root: PathBuf,
    #[cfg(test)]
    materialized_test_control: Option<std::sync::Arc<MaterializedTestControl>>,
}

#[cfg(test)]
pub(crate) struct MaterializedTestControl {
    pub(crate) release: std::sync::Barrier,
    pub(crate) completed: std::sync::Barrier,
}

#[cfg(test)]
impl MaterializedTestControl {
    pub(crate) fn new() -> Self {
        Self {
            release: std::sync::Barrier::new(2),
            completed: std::sync::Barrier::new(2),
        }
    }
}

/// Payload-free result used by the internal Engine proof path.
///
/// Native OCLA consumers keep receiving [`CapabilityResult`]. The Engine needs
/// a SHA-256 identity for the exact derived output, but must not retain or
/// expose that output in a receipt.
#[allow(dead_code)]
pub(crate) struct NativeContextInvocationResult {
    pub result: CapabilityResult,
    pub input_digest: String,
    pub output_digest: String,
    /// Exact derived bytes are kept inside the Engine boundary solely to
    /// persist a local, integrity-addressed output artifact. They are never
    /// serialized, logged, or returned from the OCLA adapter contract.
    pub(crate) output: Vec<u8>,
}

/// Typed internal failure boundary for the native Engine proof path.
///
/// The public OCLA adapter trait continues to expose `OclaError`; the Engine
/// must not infer its stable failure taxonomy by parsing those error strings.
#[derive(Debug)]
pub(crate) enum NativeContextInvocationFailure {
    SourceUnavailable(String),
    ResourceLimit(String),
    UnsupportedInput,
    InvalidRequest(String),
}

impl NativeContextInvocationFailure {
    fn into_ocla_error(self) -> OclaError {
        let message = match self {
            Self::SourceUnavailable(message)
            | Self::ResourceLimit(message)
            | Self::InvalidRequest(message) => message,
            Self::UnsupportedInput => {
                "native context optimization accepts ContextRequest only".to_owned()
            }
        };
        OclaError::InvalidRequest(message)
    }
}

impl NativeContextAdapter {
    #[must_use]
    pub fn new() -> Self {
        Self {
            root: std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")),
            #[cfg(test)]
            materialized_test_control: None,
        }
    }

    #[must_use]
    pub fn with_root(root: impl AsRef<Path>) -> Self {
        Self {
            root: root.as_ref().to_path_buf(),
            #[cfg(test)]
            materialized_test_control: None,
        }
    }

    #[cfg(test)]
    pub(crate) fn with_materialized_test_control(
        mut self,
        control: std::sync::Arc<MaterializedTestControl>,
    ) -> Self {
        self.materialized_test_control = Some(control);
        self
    }

    #[must_use]
    pub fn root(&self) -> &Path {
        &self.root
    }

    fn optimize(&self, content: &str, paths: &[String], mode: &str) -> String {
        if matches!(mode, "full" | "raw" | "passthrough") {
            return content.to_owned();
        }

        let extension = paths
            .first()
            .and_then(|path| Path::new(path).extension())
            .and_then(|extension| extension.to_str());
        compressor::aggressive_compress(content, extension)
    }

    fn invoke_context(
        &self,
        invocation: &CapabilityInvocation,
        paths: &[String],
        mode: &str,
        budget_tokens: Option<u64>,
        start: Instant,
    ) -> Result<NativeContextInvocationResult, NativeContextInvocationFailure> {
        let input = read_context_paths(&self.root, paths).map_err(|error| {
            NativeContextInvocationFailure::SourceUnavailable(error.to_string())
        })?;
        self.invoke_materialized_context(invocation, paths, mode, budget_tokens, start, &input)
    }

    fn invoke_materialized_context(
        &self,
        invocation: &CapabilityInvocation,
        paths: &[String],
        mode: &str,
        budget_tokens: Option<u64>,
        start: Instant,
        input: &str,
    ) -> Result<NativeContextInvocationResult, NativeContextInvocationFailure> {
        let input_tokens = tokens::count_tokens(&input) as u64;
        if let Some(max) = invocation.policy_constraints.max_input_tokens
            && input_tokens > max
        {
            return Err(NativeContextInvocationFailure::ResourceLimit(format!(
                "input exceeds policy token limit ({input_tokens} > {max})"
            )));
        }

        let optimized = self.optimize(&input, paths, mode);
        let output = match budget_tokens {
            Some(budget) => truncate_to_tokens(&optimized, budget),
            None => optimized,
        };
        let output_tokens = tokens::count_tokens(&output) as u64;
        if let Some(max) = invocation.policy_constraints.max_output_tokens
            && output_tokens > max
        {
            return Err(NativeContextInvocationFailure::ResourceLimit(format!(
                "output exceeds policy token limit ({output_tokens} > {max})"
            )));
        }

        let latency_ms = super::super::invocation::check_timeout(start, invocation.timeout_ms)
            .map_err(|error| NativeContextInvocationFailure::ResourceLimit(error.to_string()))?;
        if let Some(max) = invocation.policy_constraints.max_latency_ms
            && latency_ms > max
        {
            return Err(NativeContextInvocationFailure::ResourceLimit(format!(
                "capability latency exceeds policy limit ({latency_ms} > {max})"
            )));
        }
        let output_ref = evidence_ref(&output);
        let mut observation = CapabilityObservationV1::success(
            invocation,
            input_tokens,
            output_tokens,
            latency_ms,
            Some(output_ref.clone()),
        );
        observation.metrics.insert(
            "compression_saved_tokens".into(),
            input_tokens.saturating_sub(output_tokens),
        );
        observation.metrics.insert(
            "compression_rate_milli".into(),
            compression_rate_milli(input_tokens, output_tokens),
        );
        Ok(NativeContextInvocationResult {
            result: CapabilityResult {
                success: true,
                output_tokens,
                latency_ms,
                observation,
                evidence_ref: Some(output_ref),
            },
            input_digest: format!(
                "sha256:{}",
                crate::core::agent_identity::hex_encode(&Sha256::digest(input.as_bytes()))
            ),
            output_digest: format!(
                "sha256:{}",
                crate::core::agent_identity::hex_encode(&Sha256::digest(output.as_bytes()))
            ),
            output: output.into_bytes(),
        })
    }

    /// Execute the native context capability and return its payload-free
    /// SHA-256 output identity for the internal Engine bridge.
    pub(crate) fn invoke_with_output_identity(
        &self,
        invocation: &CapabilityInvocation,
    ) -> Result<NativeContextInvocationResult, NativeContextInvocationFailure> {
        self.validate_engine_invocation(invocation)?;
        let start = Instant::now();
        match &invocation.input {
            CapabilityInput::ContextRequest {
                paths,
                mode,
                budget_tokens,
            } => self.invoke_context(invocation, paths, mode, *budget_tokens, start),
            CapabilityInput::ShellCommand { .. }
            | CapabilityInput::ModelRequest { .. }
            | CapabilityInput::AgentTask(_) => {
                Err(NativeContextInvocationFailure::UnsupportedInput)
            }
        }
    }

    /// Execute against the exact source snapshot already acquired by a
    /// production caller. This prevents a second file read and binds the
    /// Engine digest to the same bytes the caller will render.
    fn invoke_materialized_with_output_identity(
        &self,
        invocation: &CapabilityInvocation,
        input: &str,
    ) -> Result<NativeContextInvocationResult, NativeContextInvocationFailure> {
        self.validate_engine_invocation(invocation)?;
        let start = Instant::now();
        match &invocation.input {
            CapabilityInput::ContextRequest {
                paths,
                mode,
                budget_tokens,
            } => self.invoke_materialized_context(
                invocation,
                paths,
                mode,
                *budget_tokens,
                start,
                input,
            ),
            CapabilityInput::ShellCommand { .. }
            | CapabilityInput::ModelRequest { .. }
            | CapabilityInput::AgentTask(_) => {
                Err(NativeContextInvocationFailure::UnsupportedInput)
            }
        }
    }

    /// Execute a materialized invocation behind an actual host deadline.
    /// Timed-out workers can finish computation, but cannot persist Engine
    /// artifacts because persistence remains in the receiving Engine layer.
    pub(crate) fn invoke_materialized_bounded(
        &self,
        invocation: CapabilityInvocation,
        input: String,
    ) -> Result<NativeContextInvocationResult, NativeContextInvocationFailure> {
        if invocation.timeout_ms == 0 {
            return Err(NativeContextInvocationFailure::ResourceLimit(
                "native context deadline expired before dispatch".to_owned(),
            ));
        }
        let timeout = std::time::Duration::from_millis(invocation.timeout_ms);
        let adapter = Self::with_root(&self.root);
        #[cfg(test)]
        let adapter = {
            let mut adapter = adapter;
            adapter.materialized_test_control = self.materialized_test_control.clone();
            adapter
        };
        let (tx, rx) = std::sync::mpsc::sync_channel(1);
        std::thread::Builder::new()
            .name("native-context-engine".to_owned())
            .spawn(move || {
                #[cfg(test)]
                if let Some(control) = adapter.materialized_test_control.as_ref() {
                    control.release.wait();
                }
                let result = adapter.invoke_materialized_with_output_identity(&invocation, &input);
                #[cfg(test)]
                if let Some(control) = adapter.materialized_test_control.as_ref() {
                    control.completed.wait();
                }
                let _ = tx.send(result);
            })
            .map_err(|error| {
                NativeContextInvocationFailure::InvalidRequest(format!(
                    "start native context worker: {error}"
                ))
            })?;
        rx.recv_timeout(timeout).map_err(|error| match error {
            std::sync::mpsc::RecvTimeoutError::Timeout => {
                NativeContextInvocationFailure::ResourceLimit(
                    "native context execution exceeded its deadline".to_owned(),
                )
            }
            std::sync::mpsc::RecvTimeoutError::Disconnected => {
                NativeContextInvocationFailure::InvalidRequest(
                    "native context worker disconnected".to_owned(),
                )
            }
        })?
    }

    fn validate_engine_invocation(
        &self,
        invocation: &CapabilityInvocation,
    ) -> Result<(), NativeContextInvocationFailure> {
        invocation
            .validate()
            .map_err(|error| NativeContextInvocationFailure::InvalidRequest(error.to_string()))?;
        if invocation.capability_id != self.manifest().capability_id.as_str()
            || invocation.capability_version != self.manifest().version
        {
            return Err(NativeContextInvocationFailure::InvalidRequest(
                "invocation capability identity does not match adapter manifest".into(),
            ));
        }
        Ok(())
    }
}

impl Default for NativeContextAdapter {
    fn default() -> Self {
        Self::new()
    }
}

impl CapabilityAdapter for NativeContextAdapter {
    fn manifest(&self) -> &CapabilityManifestV1 {
        manifest()
    }

    fn invoke(&self, invocation: CapabilityInvocation) -> OclaResult<CapabilityResult> {
        self.invoke_with_output_identity(&invocation)
            .map(|result| result.result)
            .map_err(NativeContextInvocationFailure::into_ocla_error)
    }

    fn health_check(&self) -> OclaResult<bool> {
        Ok(self.root.is_dir() && self.manifest().validate().is_ok())
    }
}

fn truncate_to_tokens(content: &str, budget: u64) -> String {
    if budget == 0 {
        return String::new();
    }
    if tokens::count_tokens(content) as u64 <= budget {
        return content.to_owned();
    }
    crate::core::budget::truncate_to_token_budget(
        content,
        usize::try_from(budget).unwrap_or(usize::MAX),
    )
}

fn compression_rate_milli(input_tokens: u64, output_tokens: u64) -> u64 {
    if input_tokens == 0 {
        return 0;
    }
    input_tokens
        .saturating_sub(output_tokens)
        .saturating_mul(1000)
        .checked_div(input_tokens)
        .unwrap_or(0)
        .min(1000)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::ocla::invocation::{CapabilityAdapter, PolicyConstraints};

    #[test]
    fn manifest_is_pinned_to_native_path() {
        assert!(MANIFEST_PATH.ends_with("context-optimization-v1.json"));
        assert_eq!(manifest().provider, "leanctx");
    }

    #[test]
    fn rejects_non_context_inputs() {
        let adapter = NativeContextAdapter::new();
        let invocation = CapabilityInvocation {
            task_id: "task-1".into(),
            capability_id: adapter.manifest().capability_id.as_str().into(),
            capability_version: adapter.manifest().version.clone(),
            input: CapabilityInput::ShellCommand {
                command: "printf test".into(),
                workdir: None,
            },
            policy_constraints: PolicyConstraints::default(),
            timeout_ms: 100,
        };
        assert!(adapter.invoke(invocation).is_err());
    }

    #[test]
    fn internal_output_identity_is_canonical_sha256_without_payload() {
        let root = tempfile::tempdir().expect("temporary adapter root");
        std::fs::write(root.path().join("fixture.md"), "hello context").expect("fixture write");
        let adapter = NativeContextAdapter::with_root(root.path());
        let invocation = CapabilityInvocation {
            task_id: "task-1".into(),
            capability_id: adapter.manifest().capability_id.as_str().into(),
            capability_version: adapter.manifest().version.clone(),
            input: CapabilityInput::ContextRequest {
                paths: vec!["fixture.md".into()],
                mode: "raw".into(),
                budget_tokens: None,
            },
            policy_constraints: PolicyConstraints::default(),
            timeout_ms: 0,
        };

        let result = adapter
            .invoke_with_output_identity(&invocation)
            .expect("native invocation succeeds");
        assert_eq!(
            result.output_digest,
            format!(
                "sha256:{}",
                crate::core::agent_identity::hex_encode(&Sha256::digest(b"hello context"))
            )
        );
        assert!(result.result.evidence_ref.is_some());
    }
}