lean-ctx 3.9.19

Context Runtime for AI Agents with CCP. 79 MCP tools, 10 read modes, 95+ compression patterns, cross-session memory (CCP), persistent AI knowledge with temporal facts + contradiction detection, multi-agent context sharing, LITM-aware positioning, AAAK compact format, adaptive compression with Thompson Sampling bandits. Supports 24+ AI tools. Reduces LLM token consumption by up to 99%.
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
//! Frozen, deterministic OSS reference scheduler.
//!
//! This implementation is Class B/C reference behavior only. Production
//! Class D code may implement [`SchedulerService`] with private decision
//! intelligence, but this scheduler performs no adaptation, learning, or
//! ranking from private data.

use std::cmp::Ordering;

use lean_ctx_protocol::{
    CapabilityId, CapabilityManifestV1, ContextStrategy, ExecutionPlanV1, PlanId, StopCondition,
    TaskEnvelopeV1,
};

use super::catalogue::{ProviderEntry, TechnicalCatalogue};
use super::policy_constraints::PolicyConstraints;
use super::scheduler_service::{ExecutionCandidate, SchedulerDecision, SchedulerService};
use super::types::{OclaError, OclaResult};

/// Maximum number of public candidates enumerated by the reference scheduler.
pub const MAX_CANDIDATES: usize = 100;

const SCHEDULER_REF: &str = "scheduler:reference-v1";
const FALLBACK_CAPABILITY: &str = "capability://leanctx/passthrough";
const FALLBACK_MODEL: &str = "manual";
const FALLBACK_PROVIDER: &str = "leanctx";

/// Deterministic, non-adaptive scheduler supplied for OSS conformance.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ReferenceScheduler;

impl ReferenceScheduler {
    /// Construct the frozen reference scheduler.
    #[must_use]
    pub const fn new() -> Self {
        Self
    }

    /// Produce a recommendation, including a fallback, from public inputs.
    pub fn schedule(
        &self,
        envelope: &TaskEnvelopeV1,
        eligible: &[CapabilityManifestV1],
        catalogue: &TechnicalCatalogue,
        policy: &PolicyConstraints,
    ) -> OclaResult<SchedulerDecision> {
        let candidates = self.generate_candidates(envelope, eligible, catalogue)?;
        let fallback = self.fallback_candidate(envelope)?;
        let (filtered, excluded) = filter_with_report(candidates, policy);
        let mut decision = self.select_plan(&filtered, &fallback);
        decision.candidates_evaluated = filtered
            .len()
            .saturating_add(excluded.len())
            .try_into()
            .unwrap_or(u32::MAX);
        decision.candidates_excluded = excluded.len().try_into().unwrap_or(u32::MAX);
        decision.decision_ref = decision_ref(&filtered, &excluded, &fallback);
        Ok(decision)
    }

    /// Alias suitable for callers that use recommendation terminology.
    pub fn recommend(
        &self,
        envelope: &TaskEnvelopeV1,
        eligible: &[CapabilityManifestV1],
        catalogue: &TechnicalCatalogue,
        policy: &PolicyConstraints,
    ) -> OclaResult<SchedulerDecision> {
        self.schedule(envelope, eligible, catalogue, policy)
    }

    /// Build the deterministic fallback plan used when no candidate survives.
    pub fn fallback_plan(&self, envelope: &TaskEnvelopeV1) -> OclaResult<ExecutionPlanV1> {
        Ok(self.fallback_candidate(envelope)?.plan)
    }

    fn fallback_candidate(&self, envelope: &TaskEnvelopeV1) -> OclaResult<ExecutionCandidate> {
        envelope.validate().map_err(|error| {
            OclaError::InvalidRequest(format!("invalid task envelope: {error}"))
        })?;
        let capability_id =
            CapabilityId::try_from(FALLBACK_CAPABILITY.to_owned()).map_err(|error| {
                OclaError::InvalidRequest(format!("invalid fallback capability: {error}"))
            })?;
        let plan = plan_for(
            envelope,
            &capability_id,
            FALLBACK_MODEL,
            FALLBACK_PROVIDER,
            Some("fallback"),
        )?;
        Ok(ExecutionCandidate::new(
            plan,
            FALLBACK_CAPABILITY,
            FALLBACK_MODEL,
            FALLBACK_PROVIDER,
            Some(0),
            envelope.quality_requirement_milli.map(u32::from),
            envelope.latency_budget_ms,
        ))
    }
}

impl SchedulerService for ReferenceScheduler {
    fn generate_candidates(
        &self,
        envelope: &TaskEnvelopeV1,
        eligible: &[CapabilityManifestV1],
        catalogue: &TechnicalCatalogue,
    ) -> OclaResult<Vec<ExecutionCandidate>> {
        envelope.validate().map_err(|error| {
            OclaError::InvalidRequest(format!("invalid task envelope: {error}"))
        })?;

        let mut manifests = eligible
            .iter()
            .filter(|manifest| catalogue_allows_manifest(catalogue, manifest))
            .cloned()
            .collect::<Vec<_>>();
        manifests.sort_by_key(manifest_key);

        let mut candidates = Vec::new();
        for manifest in manifests {
            manifest.validate().map_err(|error| {
                OclaError::InvalidRequest(format!("invalid capability manifest: {error}"))
            })?;

            let provider_options = provider_options(&manifest, catalogue);
            for (provider, models) in provider_options {
                for model in models {
                    if candidates.len() == MAX_CANDIDATES {
                        return Ok(candidates);
                    }
                    let plan =
                        plan_for(envelope, &manifest.capability_id, &model, &provider, None)?;
                    candidates.push(ExecutionCandidate::new(
                        plan,
                        manifest.capability_id.as_str(),
                        model,
                        provider.clone(),
                        // Public catalogues do not contain pricing data.
                        None,
                        envelope.quality_requirement_milli.map(u32::from),
                        envelope.latency_budget_ms,
                    ));
                }
            }
        }
        Ok(candidates)
    }

    fn filter_candidates(
        &self,
        candidates: Vec<ExecutionCandidate>,
        policy: &PolicyConstraints,
    ) -> Vec<ExecutionCandidate> {
        filter_with_report(candidates, policy).0
    }

    fn select_plan(
        &self,
        filtered: &[ExecutionCandidate],
        fallback: &ExecutionCandidate,
    ) -> SchedulerDecision {
        let selected = filtered
            .iter()
            .min_by(|left, right| compare_candidates(left, right))
            .unwrap_or(fallback);
        let has_candidate = !filtered.is_empty();
        let rationale_code = if has_candidate {
            "reference_score_cost_latency_quality"
        } else {
            "fallback_no_permitted_candidates"
        };
        SchedulerDecision {
            selected: selected.plan.clone(),
            fallback: fallback.plan.clone(),
            decision_ref: decision_ref(filtered, &[], fallback),
            rationale_code: rationale_code.to_owned(),
            confidence_milli: if has_candidate { 1000 } else { 0 },
            candidates_evaluated: filtered.len().try_into().unwrap_or(u32::MAX),
            candidates_excluded: 0,
        }
    }
}

fn catalogue_allows_manifest(
    catalogue: &TechnicalCatalogue,
    manifest: &CapabilityManifestV1,
) -> bool {
    catalogue
        .capability(manifest.capability_id.as_str(), &manifest.version)
        .is_none_or(|entry| entry.available)
}

fn manifest_key(manifest: &CapabilityManifestV1) -> (String, String, String) {
    (
        manifest.capability_id.as_str().to_owned(),
        manifest.version.clone(),
        manifest.provider.clone(),
    )
}

fn provider_options(
    manifest: &CapabilityManifestV1,
    catalogue: &TechnicalCatalogue,
) -> Vec<(String, Vec<String>)> {
    let mut entries = catalogue
        .providers
        .iter()
        .filter(|entry| entry.provider_id == manifest.provider)
        .collect::<Vec<_>>();
    entries.sort_by(|left, right| left.provider_id.cmp(&right.provider_id));

    if entries.is_empty() {
        return vec![(manifest.provider.clone(), model_options(catalogue, None))];
    }

    entries
        .into_iter()
        .map(|entry| {
            (
                entry.provider_id.clone(),
                model_options(catalogue, Some(entry)),
            )
        })
        .collect()
}

fn model_options(catalogue: &TechnicalCatalogue, provider: Option<&ProviderEntry>) -> Vec<String> {
    let mut models = match provider {
        Some(provider) if !provider.models_available.is_empty() => {
            provider.models_available.clone()
        }
        _ => catalogue
            .models
            .iter()
            .map(|model| model.model_id.clone())
            .collect(),
    };
    models.retain(|model| !model.is_empty());
    models.sort();
    models.dedup();
    if models.is_empty() {
        models.push(FALLBACK_MODEL.to_owned());
    }
    models
}

fn plan_for(
    envelope: &TaskEnvelopeV1,
    capability_id: &CapabilityId,
    model: &str,
    provider: &str,
    fallback_marker: Option<&str>,
) -> OclaResult<ExecutionPlanV1> {
    let seed = format!(
        "reference-v1:{}:{}:{}:{}",
        envelope.task_id.as_str(),
        capability_id.as_str(),
        model,
        provider
    );
    let plan_id = PlanId::try_from(format!("plan:{}", blake3::hash(seed.as_bytes()).to_hex()))
        .map_err(|error| {
            OclaError::InvalidRequest(format!("invalid deterministic plan id: {error}"))
        })?;
    let mut fallback_refs = Vec::new();
    if let Some(marker) = fallback_marker {
        fallback_refs.push(marker.to_owned());
    }
    let plan = ExecutionPlanV1 {
        schema_version: 1,
        plan_id,
        task_id: envelope.task_id.clone(),
        context_budget_tokens: 0,
        context_strategy: ContextStrategy::Balanced,
        knowledge_refs: Vec::new(),
        capability_ids: vec![capability_id.clone()],
        model: model.to_owned(),
        provider: provider.to_owned(),
        reasoning_allocation_milli: 0,
        max_retries: 0,
        fallback_refs,
        stop_condition: StopCondition::OnCompletion,
        expected_cost_micros: 0,
        expected_quality_milli: envelope.quality_requirement_milli.unwrap_or(0),
        expected_latency_ms: envelope.latency_budget_ms.unwrap_or(0),
        policy_decision_ref: None,
        scheduler_decision_ref: Some(SCHEDULER_REF.to_owned()),
    };
    plan.validate().map_err(|error| {
        OclaError::InvalidRequest(format!("invalid deterministic execution plan: {error}"))
    })?;
    Ok(plan)
}

fn filter_with_report(
    candidates: Vec<ExecutionCandidate>,
    policy: &PolicyConstraints,
) -> (Vec<ExecutionCandidate>, Vec<ExecutionCandidate>) {
    let mut permitted = Vec::with_capacity(candidates.len());
    let mut excluded = Vec::new();
    for mut candidate in candidates {
        match policy.permits(&candidate) {
            Ok(()) => {
                candidate.exclusion_reason = None;
                permitted.push(candidate);
            }
            Err(error) => {
                candidate.exclusion_reason = Some(error.to_string());
                excluded.push(candidate);
            }
        }
    }
    (permitted, excluded)
}

fn compare_candidates(left: &ExecutionCandidate, right: &ExecutionCandidate) -> Ordering {
    let left_cost = left
        .expected_cost_micros
        .unwrap_or(left.plan.expected_cost_micros);
    let right_cost = right
        .expected_cost_micros
        .unwrap_or(right.plan.expected_cost_micros);
    let left_latency = left
        .expected_latency_ms
        .unwrap_or(left.plan.expected_latency_ms);
    let right_latency = right
        .expected_latency_ms
        .unwrap_or(right.plan.expected_latency_ms);
    let left_quality = left
        .expected_quality_milli
        .unwrap_or(u32::from(left.plan.expected_quality_milli));
    let right_quality = right
        .expected_quality_milli
        .unwrap_or(u32::from(right.plan.expected_quality_milli));

    left_cost
        .cmp(&right_cost)
        .then_with(|| left_latency.cmp(&right_latency))
        .then_with(|| right_quality.cmp(&left_quality))
}

fn decision_ref(
    filtered: &[ExecutionCandidate],
    excluded: &[ExecutionCandidate],
    fallback: &ExecutionCandidate,
) -> String {
    let mut seed = String::from("reference-v1|");
    for candidate in filtered {
        seed.push_str("permit:");
        seed.push_str(&candidate.identity());
        seed.push('|');
    }
    for candidate in excluded {
        seed.push_str("exclude:");
        seed.push_str(&candidate.identity());
        seed.push(':');
        seed.push_str(candidate.exclusion_reason.as_deref().unwrap_or("unknown"));
        seed.push('|');
    }
    seed.push_str("fallback:");
    seed.push_str(&fallback.identity());
    format!(
        "decision:reference-v1:{}",
        blake3::hash(seed.as_bytes()).to_hex()
    )
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use super::*;
    use crate::core::ocla::catalogue::{CatalogueEntry, ModelEntry, ProviderEntry};
    use lean_ctx_protocol::{
        CapabilityKind, DataClassification, DataMovement, Determinism, MeasurementSupportV1,
        Reversibility, SurfaceSupportV1, TaskComplexity,
    };

    fn manifest(capability_id: &str, provider: &str) -> CapabilityManifestV1 {
        CapabilityManifestV1 {
            schema_version: 1,
            capability_id: CapabilityId::try_from(capability_id).expect("capability id"),
            provider: provider.to_owned(),
            kind: CapabilityKind::Tool,
            version: "1.0.0".to_owned(),
            surfaces: vec!["context".to_owned()],
            support_matrix: BTreeMap::from([(
                "context".to_owned(),
                SurfaceSupportV1 {
                    supported: true,
                    input_schema_ref: None,
                    output_schema_ref: None,
                },
            )]),
            local: true,
            remote: false,
            reversibility: Reversibility::Reversible,
            determinism: Determinism::Deterministic,
            data_movement: DataMovement::LocalOnly,
            supported_classifications: vec![DataClassification::Public],
            measurement_support: MeasurementSupportV1 {
                latency: true,
                tokens: true,
                quality: true,
            },
            input_schema_ref: None,
            output_schema_ref: None,
            conformance_version: 1,
            extra: BTreeMap::new(),
        }
    }

    fn envelope() -> TaskEnvelopeV1 {
        TaskEnvelopeV1 {
            schema_version: 1,
            task_id: "task-reference".try_into().expect("task id"),
            trace_id: "trace-reference".try_into().expect("trace id"),
            project_id: "project-reference".try_into().expect("project id"),
            session_id: "session-reference".try_into().expect("session id"),
            agent_id: "agent-reference".try_into().expect("agent id"),
            complexity: TaskComplexity::Low,
            created_at: "2026-08-09T00:00:00Z".to_owned(),
            parent_task_id: None,
            tenant_id: None,
            intent: Some("inspect".to_owned()),
            task_class: None,
            risk_class: None,
            quality_requirement_milli: Some(700),
            cost_budget_micros: None,
            latency_budget_ms: Some(500),
            data_classification: Some(DataClassification::Public),
            region_policy_ref: None,
            model_policy_ref: None,
            context_state_ref: None,
            outcome_contract_ref: None,
        }
    }

    #[test]
    fn generate_candidates_enumerates_models_and_is_capped() {
        let capability = manifest("capability://search", "provider-a");
        let mut catalogue = TechnicalCatalogue {
            capabilities: vec![CatalogueEntry {
                capability_id: capability.capability_id.as_str().to_owned(),
                version: capability.version.clone(),
                manifest: capability.clone(),
                available: true,
            }],
            models: vec![
                ModelEntry {
                    model_id: "model-b".to_owned(),
                    context_window: 8_000,
                    supports_reasoning: false,
                    supports_streaming: true,
                },
                ModelEntry {
                    model_id: "model-a".to_owned(),
                    context_window: 8_000,
                    supports_reasoning: true,
                    supports_streaming: true,
                },
            ],
            providers: vec![ProviderEntry {
                provider_id: "provider-a".to_owned(),
                models_available: vec!["model-b".to_owned(), "model-a".to_owned()],
                regions: vec!["CH".to_owned()],
            }],
        };
        catalogue.models.reverse();

        let candidates = ReferenceScheduler::new()
            .generate_candidates(&envelope(), &[capability], &catalogue)
            .expect("candidate generation");

        assert_eq!(candidates.len(), 2);
        assert_eq!(candidates[0].model, "model-a");
        assert_eq!(candidates[1].model, "model-b");
        assert!(candidates.len() <= MAX_CANDIDATES);
    }

    #[test]
    fn filter_candidates_applies_provider_policy() {
        let scheduler = ReferenceScheduler::new();
        let task = envelope();
        let allowed = manifest("capability://allowed", "provider-a");
        let blocked = manifest("capability://blocked", "provider-b");
        let candidates = scheduler
            .generate_candidates(
                &task,
                &[allowed.clone(), blocked],
                &TechnicalCatalogue::from_manifests([allowed]),
            )
            .expect("candidate generation");
        let policy = PolicyConstraints {
            allowed_providers: Some(vec!["provider-a".to_owned()]),
            ..PolicyConstraints::default()
        };

        let filtered = scheduler.filter_candidates(candidates, &policy);

        assert_eq!(filtered.len(), 1);
        assert_eq!(filtered[0].provider, "provider-a");
    }

    #[test]
    fn selection_is_deterministic_by_cost_then_latency() {
        let scheduler = ReferenceScheduler::new();
        let task = envelope();
        let first_capability = CapabilityId::try_from("capability://first").expect("capability");
        let second_capability = CapabilityId::try_from("capability://second").expect("capability");
        let first_plan = plan_for(&task, &first_capability, "model-first", "provider", None)
            .expect("first plan");
        let second_plan = plan_for(&task, &second_capability, "model-second", "provider", None)
            .expect("second plan");
        let candidates = vec![
            ExecutionCandidate::new(
                first_plan,
                "capability://first",
                "model-first",
                "provider",
                Some(20),
                Some(700),
                Some(100),
            ),
            ExecutionCandidate::new(
                second_plan,
                "capability://second",
                "model-second",
                "provider",
                Some(10),
                Some(600),
                Some(900),
            ),
        ];
        let fallback = scheduler
            .fallback_candidate(&task)
            .expect("fallback candidate");

        let decision = scheduler.select_plan(&candidates, &fallback);

        assert_eq!(decision.selected.model, "model-second");
        assert_eq!(decision.fallback, fallback.plan);
        assert_eq!(decision.confidence_milli, 1000);
    }
}