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
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
// SPDX-License-Identifier: Apache-2.0

//! Candidate orchestration, scoring, and feedback artifacts for the context kernel.

use chrono::{DateTime, FixedOffset};
use std::collections::{BTreeMap, HashMap, HashSet};
use thiserror::Error;

use crate::core::context_compiler::{CompileCandidate, CompileMode, CompileResult, compile};
use crate::core::context_field::{
    ContextField, ContextKind, ContextState, FieldSignals, ViewCosts, ViewKind,
    normalize_token_cost,
};

use super::types::{
    CandidateProvider, ContextObjectKind, ContextObjectV1, ContextOriginV1, ContextPlanV1,
    ContextReceiptV1, ExcludedEntry, PlanBudget, PlanEntry, ProviderStat, QualitySignal,
    ReceiptOutcome, RetrievalContext,
};
use super::{enforce::BlockedEntry, policy::ContextPolicy};

#[derive(Debug, Clone)]
struct EnumeratedCandidate {
    provider_id: String,
    object: ContextObjectV1,
}

/// A compiler result cannot be bound to the kernel's candidate/accounting snapshot.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
pub enum KernelPlanError {
    #[error("compiler selected an unknown candidate")]
    CompilerSelectedUnknownCandidate,
    #[error("provider statistics are missing for a selected candidate")]
    MissingProviderStats,
    #[error("plan accounting is inconsistent")]
    InvalidPlanAccounting,
    #[error("selected candidate origin is missing or ambiguous")]
    InvalidPlanOrigin,
}

/// The Context Control Kernel — orchestrates candidate gathering, Phi scoring,
/// budget-optimal selection, and plan/receipt generation.
pub struct ContextKernel {
    providers: Vec<Box<dyn CandidateProvider>>,
    field: ContextField,
    relevance_floor: bool,
}

impl ContextKernel {
    /// Create a kernel with an explicit field policy.
    pub(crate) fn with_field(
        providers: Vec<Box<dyn CandidateProvider>>,
        field: ContextField,
    ) -> Self {
        Self {
            providers,
            field,
            relevance_floor: false,
        }
    }

    /// Leave out candidates unrelated to the task even when budget remains
    /// (explicit-source plans; see [`super::relevance_floor`]).
    pub(crate) fn with_relevance_floor(mut self) -> Self {
        self.relevance_floor = true;
        self
    }

    /// Create a new kernel with the given providers.
    pub fn new(providers: Vec<Box<dyn CandidateProvider>>) -> Self {
        Self::with_field(providers, ContextField::new())
    }

    /// Create a kernel with default providers for a project.
    pub fn for_project(project_root: &str) -> Self {
        Self::new(super::providers::default_providers(project_root))
    }

    /// Register an additional provider.
    pub fn register(&mut self, provider: Box<dyn CandidateProvider>) {
        self.providers.push(provider);
    }

    /// Gather and content-deduplicate candidates from all registered providers.
    pub fn gather(&self, ctx: &RetrievalContext) -> Vec<ContextObjectV1> {
        let mut all: Vec<_> = self
            .candidates(ctx)
            .into_iter()
            .map(|candidate| candidate.object)
            .collect();
        dedup_by_content_ref(&mut all);
        all
    }

    fn candidates(&self, ctx: &RetrievalContext) -> Vec<EnumeratedCandidate> {
        self.providers
            .iter()
            .flat_map(|provider| {
                let provider_id = provider.provider_id().to_owned();
                provider
                    .candidates(ctx)
                    .into_iter()
                    .map(move |object| EnumeratedCandidate {
                        provider_id: provider_id.clone(),
                        object,
                    })
            })
            .collect()
    }

    /// Score candidates, compile the best package under budget, and return its plan.
    pub fn plan(&self, ctx: &RetrievalContext) -> Result<ContextPlanV1, KernelPlanError> {
        self.plan_with_policy(ctx, &ContextPolicy::default())
            .map(|(plan, _)| plan)
    }

    /// Own policy filtering, selection, accounting and identity as one operation.
    pub(crate) fn plan_with_policy(
        &self,
        ctx: &RetrievalContext,
        policy: &ContextPolicy,
    ) -> Result<(ContextPlanV1, Vec<BlockedEntry>), KernelPlanError> {
        self.plan_with_policy_at(ctx, policy, None)
    }

    /// Own policy filtering, selection, accounting and identity at an explicit time.
    pub(crate) fn plan_with_policy_at(
        &self,
        ctx: &RetrievalContext,
        policy: &ContextPolicy,
        evaluation_time: Option<&DateTime<FixedOffset>>,
    ) -> Result<(ContextPlanV1, Vec<BlockedEntry>), KernelPlanError> {
        let mut offered = self.candidates(ctx);
        offered.sort_by(|a, b| {
            a.object
                .id
                .as_str()
                .cmp(b.object.id.as_str())
                .then_with(|| a.object.source.cmp(&b.object.source))
                .then_with(|| a.object.content_ref.cmp(&b.object.content_ref))
                .then_with(|| a.provider_id.cmp(&b.provider_id))
        });
        let mut origins = origins_from_candidates(&offered);
        let mut counts = HashMap::<String, usize>::new();
        let mut provider_stats = HashMap::<String, ProviderStat>::new();
        for candidate in &offered {
            *counts.entry(candidate.object.id.to_string()).or_default() += 1;
            provider_stats
                .entry(candidate.object.source.clone())
                .or_default()
                .candidates_offered += 1;
        }
        let mut candidates = Vec::new();
        let mut excluded = Vec::new();
        let mut rejected_ids = HashSet::new();
        for EnumeratedCandidate {
            provider_id,
            object,
        } in offered
        {
            let id = object.id.to_string();
            let reason = if provider_id.trim().is_empty() {
                Some("empty registered provider id; candidate rejected".to_owned())
            } else if counts.get(&id).copied() == Some(1) {
                evaluation_time.map_or_else(
                    || policy.candidate_violation(&object),
                    |evaluation_time| policy.candidate_violation_at(&object, Some(evaluation_time)),
                )
            } else {
                Some("ambiguous candidate id; all candidates with this id rejected".to_owned())
            };
            if let Some(reason) = reason {
                if rejected_ids.insert(id.clone()) {
                    excluded.push(ExcludedEntry {
                        object_id: id,
                        provider: object.source.clone(),
                        reason,
                    });
                }
            } else {
                let Some([origin]) = origins.get_mut(&id).map(Vec::as_mut_slice) else {
                    return Err(KernelPlanError::InvalidPlanOrigin);
                };
                origin.admitted = true;
                candidates.push(object);
            }
        }
        // A denied duplicate may never hide an eligible candidate with the same content.
        dedup_by_content_ref(&mut candidates);
        let set = CandidateSet::new(&candidates, ctx);
        let mut scored: Vec<_> = candidates
            .into_iter()
            .filter_map(|object| {
                let signals = signals_from_object(&object, ctx, &set);
                let phi = self.field.compute_phi(&signals);
                if phi.is_finite() {
                    Some((object, phi))
                } else {
                    excluded.push(ExcludedEntry {
                        object_id: object.id.to_string(),
                        provider: object.source.clone(),
                        reason: "phi is not finite".to_owned(),
                    });
                    None
                }
            })
            .collect();
        let observations = excluded
            .iter()
            .map(|entry| BlockedEntry {
                object_id: entry.object_id.clone(),
                reason: entry.reason.clone(),
            })
            .collect();
        // After the policy observations: leaving out an unrelated source is a
        // utility decision, not a block.
        if self.relevance_floor {
            let texts: Vec<_> = scored
                .iter()
                .map(|(object, _)| (object.title.as_str(), object.content.as_deref()))
                .collect();
            let kept = super::relevance_floor::kept(&texts, &ctx.query);
            let mut keep = kept.into_iter();
            scored.retain(|(object, _)| {
                let relevant = keep.next().unwrap_or(true);
                if !relevant {
                    excluded.push(ExcludedEntry {
                        object_id: object.id.to_string(),
                        provider: object.source.clone(),
                        reason:
                            "unrelated to the task: shares no term with it or a relevant source"
                                .to_owned(),
                    });
                }
                relevant
            });
        }
        let mut budget = ctx.budget;
        if let Some(cap) = policy.budget_cap_tokens {
            budget.total = budget.total.min(cap);
        }
        // Past usage cannot be undone: a lower ceiling permits zero new tokens.
        budget.total = budget.total.max(budget.used);
        let compile_candidates: Vec<_> = scored
            .iter()
            .map(|(object, phi)| to_compile_candidate(object, *phi))
            .collect();
        let result = compile(&compile_candidates, budget, CompileMode::HandleManifest);

        Ok((
            build_plan(
                ctx,
                policy,
                &scored,
                &result,
                excluded,
                provider_stats,
                origins,
                evaluation_time,
            )?,
            observations,
        ))
    }

    /// Record delivery outcome and provider-level feedback for a completed plan.
    pub fn record_receipt(
        &self,
        plan: &ContextPlanV1,
        delivered_tokens: usize,
        outcome: ReceiptOutcome,
    ) -> ContextReceiptV1 {
        let outcome_value = outcome_value(outcome);
        let total_phi: f64 = plan.selected.iter().map(|entry| entry.phi.max(0.0)).sum();
        let mut feedback_attribution = HashMap::new();
        if let Some(outcome_value) = outcome_value
            && total_phi > 0.0
        {
            for entry in &plan.selected {
                let contribution = entry.phi.max(0.0) / total_phi * outcome_value;
                *feedback_attribution
                    .entry(entry.provider.clone())
                    .or_insert(0.0) += contribution;
            }
        }

        let receipt_material = format!(
            "{}|{}|{}",
            plan.plan_id,
            delivered_tokens,
            receipt_outcome_name(outcome)
        );
        ContextReceiptV1 {
            receipt_id: format!("receipt_{}", short_hash(&receipt_material)),
            plan_id: plan.plan_id.clone(),
            task_id: None,
            delivered_tokens,
            cache_hits: 0,
            cache_misses: 0,
            outcome,
            quality_signals: outcome_value
                .into_iter()
                .map(|value| QualitySignal {
                    signal_type: "outcome".to_string(),
                    value,
                })
                .collect(),
            feedback_attribution,
        }
    }
}

fn dedup_by_content_ref(objects: &mut Vec<ContextObjectV1>) {
    let mut retained = HashMap::<String, usize>::new();
    let mut deduplicated: Vec<ContextObjectV1> = Vec::with_capacity(objects.len());
    for object in objects.drain(..) {
        match retained.get(&object.content_ref).copied() {
            Some(index) if object.confidence > deduplicated[index].confidence => {
                deduplicated[index] = object;
            }
            Some(_) => {}
            None => {
                retained.insert(object.content_ref.clone(), deduplicated.len());
                deduplicated.push(object);
            }
        }
    }
    *objects = deduplicated;
}

/// Neutral value of a signal this candidate set cannot measure.
const UNMEASURED: f64 = 0.5;

/// Signals that depend on the whole admitted candidate set rather than on
/// one object. Each is measured from what the providers supplied; where
/// they supplied nothing to measure against, the signal stays neutral.
struct CandidateSet {
    /// Files this session already delivered (the ledger's candidates).
    delivered: HashSet<String>,
    /// Files the query's search hits point at.
    anchors: HashSet<String>,
    /// Highest term overlap with a more relevant candidate, by object id.
    redundancy: HashMap<String, f64>,
}

impl CandidateSet {
    fn new(candidates: &[ContextObjectV1], ctx: &RetrievalContext) -> Self {
        let paths_of = |source: &str| -> HashSet<String> {
            candidates
                .iter()
                .filter(|object| object.source == source)
                .filter_map(object_path)
                .collect()
        };
        // Rank by relevance, then id, so redundancy does not depend on
        // provider order: each candidate is compared with those ranked above.
        let mut ranked: Vec<(f64, &ContextObjectV1, HashSet<String>)> = candidates
            .iter()
            .map(|object| {
                let mut words = terms(&object.title);
                if let Some(content) = &object.content {
                    words.extend(terms(content));
                }
                (
                    keyword_overlap(&object.title, object.content.as_deref(), &ctx.query),
                    object,
                    words,
                )
            })
            .collect();
        ranked.sort_by(|left, right| {
            right
                .0
                .total_cmp(&left.0)
                .then_with(|| left.1.id.as_str().cmp(right.1.id.as_str()))
        });
        let redundancy = ranked
            .iter()
            .enumerate()
            .map(|(index, (_, object, words))| {
                let overlap = ranked[..index]
                    .iter()
                    .map(|(_, _, other)| jaccard(words, other))
                    .fold(0.0, f64::max);
                (object.id.to_string(), overlap)
            })
            .collect();
        Self {
            delivered: paths_of("context.ledger"),
            anchors: paths_of("index.bm25"),
            redundancy,
        }
    }

    /// Already delivered content carries no news; otherwise unknown.
    fn surprise(&self, object: &ContextObjectV1) -> f64 {
        let delivered = object.source == "context.ledger"
            || object_path(object).is_some_and(|path| self.delivered.contains(&path));
        if delivered { 0.0 } else { UNMEASURED }
    }

    /// Closeness to the files the query hit: a hit itself, a graph neighbour
    /// by its link weight, anything else unknown.
    fn graph_proximity(&self, object: &ContextObjectV1) -> f64 {
        if object.source == "index.graph" {
            return f64::from(object.confidence.clamp(0.0, 1.0));
        }
        match object_path(object) {
            Some(path) if self.anchors.contains(&path) => 1.0,
            _ => UNMEASURED,
        }
    }
}

/// The file an object refers to, if any.
fn object_path(object: &ContextObjectV1) -> Option<String> {
    if let Some(path) = object.metadata.get("path") {
        return Some(path.clone());
    }
    let reference = object.content_ref.strip_prefix("file:")?;
    Some(reference.split('#').next().unwrap_or(reference).to_owned())
}

fn jaccard(left: &HashSet<String>, right: &HashSet<String>) -> f64 {
    let union = left.union(right).count();
    if union == 0 {
        return 0.0;
    }
    left.intersection(right).count() as f64 / union as f64
}

fn signals_from_object(
    object: &ContextObjectV1,
    ctx: &RetrievalContext,
    set: &CandidateSet,
) -> FieldSignals {
    FieldSignals {
        relevance: keyword_overlap(&object.title, object.content.as_deref(), &ctx.query),
        surprise: set.surprise(object),
        graph_proximity: set.graph_proximity(object),
        history_signal: object.confidence.clamp(0.0, 1.0) as f64,
        token_cost_norm: normalize_token_cost(object.token_estimate, ctx.budget.total),
        redundancy: set
            .redundancy
            .get(object.id.as_str())
            .copied()
            .unwrap_or(0.0),
    }
}

fn keyword_overlap(title: &str, content: Option<&str>, query: &str) -> f64 {
    let query_terms = terms(query);
    if query_terms.is_empty() {
        return 0.0;
    }
    let mut object_terms = terms(title);
    if let Some(content) = content {
        object_terms.extend(terms(content));
    }
    let overlap = query_terms.intersection(&object_terms).count();
    overlap as f64 / query_terms.len() as f64
}

fn terms(text: &str) -> HashSet<String> {
    text.split(|character: char| !character.is_alphanumeric())
        .filter(|term| !term.is_empty())
        .map(str::to_lowercase)
        .collect()
}

fn to_compile_candidate(object: &ContextObjectV1, phi: f64) -> CompileCandidate {
    let view_costs = if object.view_costs.estimates.is_empty() {
        ViewCosts::from_full_tokens(object.token_estimate.max(1))
    } else {
        object.view_costs.clone()
    };
    let (selected_view, selected_tokens) = view_costs
        .cheapest_content_view()
        .unwrap_or((ViewKind::Full, object.token_estimate.max(1)));

    CompileCandidate {
        id: object.id.clone(),
        kind: context_kind(object.kind),
        path: object.content_ref.clone(),
        state: if object.freshness.stale {
            ContextState::Stale
        } else {
            ContextState::Candidate
        },
        phi,
        view_costs,
        selected_view,
        selected_tokens,
        pinned: false,
        content_sketch: object
            .semantic_fingerprint
            .clone()
            .or_else(|| Some(object.content_ref.clone())),
    }
}

fn context_kind(kind: ContextObjectKind) -> ContextKind {
    match kind {
        ContextObjectKind::File => ContextKind::File,
        ContextObjectKind::Fact => ContextKind::Knowledge,
        ContextObjectKind::Episode
        | ContextObjectKind::Procedure
        | ContextObjectKind::SessionItem => ContextKind::Memory,
        ContextObjectKind::SearchChunk => ContextKind::Provider,
    }
}

fn build_plan(
    ctx: &RetrievalContext,
    policy: &ContextPolicy,
    scored: &[(ContextObjectV1, f64)],
    result: &CompileResult,
    mut excluded: Vec<ExcludedEntry>,
    mut provider_stats: HashMap<String, ProviderStat>,
    origins: BTreeMap<String, Vec<ContextOriginV1>>,
    evaluation_time: Option<&DateTime<FixedOffset>>,
) -> Result<ContextPlanV1, KernelPlanError> {
    let objects: HashMap<_, _> = scored
        .iter()
        .map(|(object, phi)| (object.id.to_string(), (object, *phi)))
        .collect();
    let mut selected = Vec::with_capacity(result.selected.len());
    for item in &result.selected {
        let (object, phi) = objects
            .get(&item.id)
            .copied()
            .ok_or(KernelPlanError::CompilerSelectedUnknownCandidate)?;
        let stat = provider_stats
            .get_mut(&object.source)
            .ok_or(KernelPlanError::MissingProviderStats)?;
        stat.candidates_selected += 1;
        stat.tokens_used = stat.tokens_used.saturating_add(item.tokens);
        selected.push(PlanEntry {
            object_id: item.id.clone(),
            provider: object.source.clone(),
            view: item.view.clone(),
            tokens: item.tokens,
            phi,
            reason: selected_display(object).unwrap_or_default(),
        });
    }
    excluded.extend(result.excluded_reasons.iter().map(|item| ExcludedEntry {
        object_id: item.id.clone(),
        provider: objects.get(&item.id).map_or_else(
            || "unknown".to_string(),
            |(object, _)| object.source.clone(),
        ),
        reason: item.reason.clone(),
    }));
    selected.sort_by(|a, b| a.object_id.cmp(&b.object_id));
    excluded.sort_by(|a, b| a.object_id.cmp(&b.object_id));
    let used_tokens = ctx.budget.used.saturating_add(result.budget_used);
    let mut plan = ContextPlanV1 {
        plan_id: String::new(),
        intent: ctx.task.clone().unwrap_or_else(|| ctx.query.clone()),
        budget: PlanBudget {
            total_tokens: result.budget_total,
            used_tokens,
            remaining_tokens: result.budget_total.saturating_sub(used_tokens),
        },
        selected,
        excluded,
        deferred: Vec::new(),
        provider_stats,
        origins,
    };
    let material = plan_material(ctx, policy, scored, result, &plan, evaluation_time);
    plan.plan_id = canonical_plan_id(&material);
    Ok(plan)
}

fn origins_from_candidates(
    candidates: &[EnumeratedCandidate],
) -> BTreeMap<String, Vec<ContextOriginV1>> {
    let mut origins: BTreeMap<String, Vec<ContextOriginV1>> = BTreeMap::new();
    for candidate in candidates {
        origins
            .entry(candidate.object.id.to_string())
            .or_default()
            .push(ContextOriginV1 {
                provider_id: candidate.provider_id.clone(),
                source: candidate.object.source.clone(),
                content_ref: candidate.object.content_ref.clone(),
                sensitivity: candidate.object.sensitivity,
                provenance: candidate.object.provenance.clone(),
                admitted: false,
            });
    }
    for records in origins.values_mut() {
        records.sort_by(|left, right| {
            left.provider_id
                .cmp(&right.provider_id)
                .then_with(|| left.source.cmp(&right.source))
                .then_with(|| left.content_ref.cmp(&right.content_ref))
                .then_with(|| {
                    sensitivity_rank(left.sensitivity).cmp(&sensitivity_rank(right.sensitivity))
                })
                .then_with(|| left.provenance.tool.cmp(&right.provenance.tool))
                .then_with(|| left.provenance.agent_id.cmp(&right.provenance.agent_id))
                .then_with(|| {
                    left.provenance
                        .client_name
                        .cmp(&right.provenance.client_name)
                })
                .then_with(|| left.provenance.timestamp.cmp(&right.provenance.timestamp))
        });
    }
    origins
}

fn sensitivity_rank(sensitivity: super::types::SensitivityLevel) -> u8 {
    match sensitivity {
        super::types::SensitivityLevel::Public => 0,
        super::types::SensitivityLevel::Internal => 1,
        super::types::SensitivityLevel::Confidential => 2,
        super::types::SensitivityLevel::Restricted => 3,
    }
}

fn selected_display(object: &ContextObjectV1) -> Option<String> {
    let path = object
        .metadata
        .get("path")
        .or_else(|| object.metadata.get("file"))
        .map(String::as_str)
        .filter(|value| !value.trim().is_empty())
        .or_else(|| match object.kind {
            ContextObjectKind::File => {
                (!object.title.trim().is_empty()).then_some(object.title.as_str())
            }
            _ => None,
        });
    let snippet = object
        .content
        .as_deref()
        .filter(|value| !value.trim().is_empty());

    match (path, snippet) {
        (Some(path), Some(snippet)) => Some(format!("{path}: {snippet}")),
        (Some(path), None) => Some(path.to_owned()),
        (None, Some(snippet)) => Some(snippet.to_owned()),
        (None, None) => None,
    }
}

fn plan_material(
    ctx: &RetrievalContext,
    policy: &ContextPolicy,
    scored: &[(ContextObjectV1, f64)],
    result: &CompileResult,
    plan: &ContextPlanV1,
    evaluation_time: Option<&DateTime<FixedOffset>>,
) -> String {
    let mut entries: Vec<_> = scored
        .iter()
        .map(|(object, phi)| {
            (
                object.id.as_str(),
                object.content_ref.as_str(),
                phi.to_bits(),
            )
        })
        .collect();
    entries.sort_unstable();
    // V2 identity material is framed and binds request scope plus the final plan.
    // Existing V1 plans remain readable; their opaque IDs are never rewritten.
    let mut material = serde_json::Map::from_iter([
        (
            "schema".to_owned(),
            serde_json::json!("leanctx.context-plan-identity/v2"),
        ),
        ("query".to_owned(), serde_json::json!(ctx.query)),
        ("task".to_owned(), serde_json::json!(ctx.task)),
        (
            "project_root".to_owned(),
            serde_json::json!(ctx.project_root),
        ),
        (
            "requested_budget".to_owned(),
            serde_json::json!([ctx.budget.total, ctx.budget.used]),
        ),
        ("policy".to_owned(), serde_json::json!(policy)),
        (
            "compiled_budget".to_owned(),
            serde_json::json!([result.budget_total, result.budget_used]),
        ),
        ("candidates".to_owned(), serde_json::json!(entries)),
    ]);
    material.extend(plan_projection_material(plan));
    if policy.retention_days.is_some() {
        material.insert(
            "evaluation_time".to_owned(),
            serde_json::json!(evaluation_time.map(DateTime::to_rfc3339)),
        );
    }
    serde_json::Value::Object(material).to_string()
}

/// Hash one canonical framed plan representation for every kernel-owned plan.
fn canonical_plan_id(material: &str) -> String {
    format!("plan_{}", blake3::hash(material.as_bytes()).to_hex())
}

fn plan_projection_material(plan: &ContextPlanV1) -> [(String, serde_json::Value); 4] {
    [
        ("selected".to_owned(), serde_json::json!(plan.selected)),
        ("excluded".to_owned(), serde_json::json!(plan.excluded)),
        (
            "provider_stats".to_owned(),
            serde_json::json!(plan.provider_stats.iter().collect::<BTreeMap<_, _>>()),
        ),
        ("origins".to_owned(), serde_json::json!(plan.origins)),
    ]
}

fn short_hash(value: &str) -> String {
    // Preserve legacy receipt lookup/replay keys; plan identity is versioned separately.
    blake3::hash(value.as_bytes()).to_hex()[..16].to_string()
}

fn outcome_value(outcome: ReceiptOutcome) -> Option<f64> {
    match outcome {
        ReceiptOutcome::Accepted => Some(1.0),
        ReceiptOutcome::Partial => Some(0.5),
        ReceiptOutcome::Rejected => Some(0.0),
        ReceiptOutcome::Unknown => None,
    }
}

fn receipt_outcome_name(outcome: ReceiptOutcome) -> &'static str {
    match outcome {
        ReceiptOutcome::Accepted => "accepted",
        ReceiptOutcome::Rejected => "rejected",
        ReceiptOutcome::Partial => "partial",
        ReceiptOutcome::Unknown => "unknown",
    }
}

#[cfg(test)]
#[path = "orchestrator_tests.rs"]
pub mod tests;