Skip to main content

lean_ctx/core/context_admission/
stored.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Current rights for stored and derived copies (G03, G04).
3//!
4//! A persisted copy is never its own authorization, and neither is a digest
5//! of it. What decides a read is the **lineage**: the sources the copy was
6//! derived from. A derived summary can lose the marking of its original, and
7//! nothing in the copy changes when the original is deleted, restricted or
8//! reclassified — so re-detecting the stored text proves nothing about the
9//! reader's rights today. Every read therefore
10//!
11//! 1. resolves the project authority (bound root against the reader's root),
12//! 2. re-reads **each source** through the admitted rooted reader under the
13//!    policy and role current *now* and compares it with what the copy was
14//!    derived from — a source that is gone, changed, reclassified, outside the
15//!    reader's project or no longer permitted withholds the copy,
16//! 3. checks the classification the original was admitted with, and
17//! 4. only then re-admits the stored text itself as an additional check.
18//!
19//! Two weaker cases are explicit, never implied:
20//!
21//! - `Lineage::CallerSupplied`: a value an agent or user typed (a bus
22//!   message, a manually recorded note). No source exists to re-authorize; the
23//!   copy is governed by content rules and by the project authority of its
24//!   writer. That is *not* a source-bound guarantee.
25//! - no binding at all (a record written before bindings existed): withheld
26//!   under an active project policy and in governed or sovereign mode with the
27//!   tee store's wording; Community developer mode re-admits it, exactly as
28//!   the originless tee rule (`core/ccr/storage.rs`) does.
29
30use lean_ctx_protocol::context_gateway::ClassificationV1;
31use serde::{Deserialize, Serialize};
32
33use super::carrier::{RESTRICTED_CODE, Withheld, admit_carrier_text_under, current_policy};
34use super::{AdmissionPolicy, fingerprint_digest};
35
36const BINDING_VERSION: u8 = 2;
37const BINDING_MISMATCH_CODE: &str = "store.binding_mismatch";
38const PROJECT_MISMATCH_CODE: &str = "store.project_mismatch";
39const PROJECT_UNRESOLVED_CODE: &str = "store.project_unresolved";
40const SOURCE_CHANGED_CODE: &str = "store.source_changed";
41const LINEAGE_UNBOUNDED_CODE: &str = "store.lineage_unbounded";
42/// Same bound as `archive::authority::MAX_SOURCES`.
43const MAX_SOURCES: usize = crate::core::archive::authority::MAX_SOURCES;
44
45/// One source a stored copy was derived from: the identity the rooted reader
46/// resolved, what it delivered, and how it was classified then.
47#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
48pub struct SourceRef {
49    /// Path as it was handed to the rooted reader (inside the bound project).
50    path: String,
51    /// Tool identity the read was authorized for (path jail, role rules).
52    tool: String,
53    /// BLAKE3 of the admitted text the reader returned.
54    admitted_digest: String,
55    /// Classification of the original at derivation time.
56    classification: ClassificationV1,
57}
58
59impl SourceRef {
60    /// Read `path` under `root` through the admitted rooted reader and bind
61    /// what it returned. This is the only way to obtain a source reference:
62    /// a path alone, or text the caller read some other way, is not one.
63    pub(crate) fn read(path: &str, root: &str, tool: &str) -> Result<(String, Self), Withheld> {
64        let mut remaining = crate::core::limits::max_read_bytes();
65        let read = Self::current(path, root, tool, &mut remaining)?;
66        let source = Self {
67            path: path.to_owned(),
68            tool: tool.to_owned(),
69            admitted_digest: content_digest(&read.content),
70            classification: read.classification,
71        };
72        Ok((read.content, source))
73    }
74
75    /// [`Self::read`] for a source the current call has already been given:
76    /// bound only if the reader returns now what it returned then
77    /// (`admitted_digest`, BLAKE3 hex of that text).
78    pub(crate) fn read_as_admitted(
79        path: &str,
80        root: &str,
81        tool: &str,
82        admitted_digest: &str,
83    ) -> Option<Self> {
84        Self::read(path, root, tool)
85            .ok()
86            .map(|(_, source)| source)
87            .filter(|source| source.admitted_digest == admitted_digest)
88    }
89
90    /// [`Self::read_as_admitted`] against a budget shared by several sources,
91    /// as [`serve_stored`] re-reads a whole lineage: a set of sources that
92    /// fits one by one but not together is not bound.
93    pub(crate) fn read_as_admitted_within(
94        path: &str,
95        root: &str,
96        tool: &str,
97        admitted_digest: &str,
98        remaining: &mut usize,
99    ) -> Option<Self> {
100        let read = Self::current(path, root, tool, remaining).ok()?;
101        let source = Self {
102            path: path.to_owned(),
103            tool: tool.to_owned(),
104            admitted_digest: content_digest(&read.content),
105            classification: read.classification,
106        };
107        (source.admitted_digest == admitted_digest).then_some(source)
108    }
109
110    /// The common rooted read, with the outcome of admitting the ORIGINAL
111    /// bytes. Classification and storability are inherited from that outcome,
112    /// never inferred from the delivered (possibly redacted) text: a source
113    /// that is restricted or only lossily decodable is not a storable source.
114    fn current(
115        path: &str,
116        root: &str,
117        tool: &str,
118        remaining: &mut usize,
119    ) -> Result<crate::core::rooted_read::AdmittedRootedRead, Withheld> {
120        let read = crate::core::rooted_read::read_file_for_tool_rooted_admitted(
121            path, root, tool, remaining,
122        )
123        .map_err(|_| Withheld::source_unavailable())?;
124        if !read.storable || !read.complete {
125            return Err(Withheld::new(&[RESTRICTED_CODE]));
126        }
127        Ok(read)
128    }
129
130    /// The reader's rights to this source *now*: still present, still inside
131    /// the project, still permitted for the current role and policy, still
132    /// delivering the same admitted bytes, original still storable and not
133    /// classified higher than when the copy was derived.
134    fn reauthorize(&self, root: &str, remaining: &mut usize) -> Result<(), Withheld> {
135        let read = Self::current(&self.path, root, &self.tool, remaining)?;
136        if content_digest(&read.content) != self.admitted_digest
137            || read.classification > self.classification
138        {
139            return Err(Withheld::new(&[SOURCE_CHANGED_CODE]));
140        }
141        Ok(())
142    }
143}
144
145/// Where a stored copy comes from.
146#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
147#[serde(tag = "kind", rename_all = "snake_case")]
148pub(crate) enum Lineage {
149    /// Typed by a caller; there is no source to re-authorize (weaker).
150    CallerSupplied,
151    /// Derived from these sources; all of them are re-authorized on a read.
152    Sources { sources: Vec<SourceRef> },
153}
154
155impl Lineage {
156    /// Lineage of a derivation: caller-supplied when it used no source.
157    /// Sources are deduplicated. More than [`MAX_SOURCES`] is refused — a
158    /// lineage is never truncated to the sources that happened to come first.
159    pub(crate) fn of(mut sources: Vec<SourceRef>) -> Result<Self, Withheld> {
160        let mut unique: Vec<SourceRef> = Vec::with_capacity(sources.len());
161        for source in sources.drain(..) {
162            if !unique.contains(&source) {
163                unique.push(source);
164            }
165        }
166        if unique.len() > MAX_SOURCES {
167            return Err(Withheld::new(&[LINEAGE_UNBOUNDED_CODE]));
168        }
169        Ok(if unique.is_empty() {
170            Self::CallerSupplied
171        } else {
172            Self::Sources { sources: unique }
173        })
174    }
175}
176
177/// What a derived store keeps next to an admitted copy.
178#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
179pub struct StoredBinding {
180    version: u8,
181    /// Who produced the copy (`agent:<id>`, `handoff`, `hook:<tool>`).
182    origin: String,
183    /// Canonical project root the copy belongs to, when one was bound.
184    #[serde(default, skip_serializing_if = "Option::is_none")]
185    project: Option<String>,
186    /// Role fingerprint of the writer (provenance; the reader's own rights
187    /// are established by re-reading the sources under the reader's role).
188    #[serde(default, skip_serializing_if = "Option::is_none")]
189    writer_role: Option<String>,
190    /// Hex digest of the gateway policy snapshot the copy was admitted under.
191    policy_digest: String,
192    /// BLAKE3 of the admitted text, so an edited record is not served.
193    content_digest: String,
194    /// Most sensitive classification over the copy and all its sources.
195    classification: ClassificationV1,
196    lineage: Lineage,
197}
198
199impl StoredBinding {
200    /// Whether this is a binding of exactly `text`. For a store that keeps
201    /// bindings apart from its items (`archive::bindings`): a binding of
202    /// another text is not this item's binding.
203    // pub only for lean-ctx-engine-ext; not public API
204    #[doc(hidden)]
205    pub fn binds(&self, text: &str) -> bool {
206        self.version == BINDING_VERSION && self.content_digest == content_digest(text)
207    }
208
209    /// Who produced the copy, as its writer named it ([`admit_and_bind`]). A
210    /// store that keeps more than one kind of copy tells them apart by it
211    /// (`handoff_ledger`). It selects a rule of that store; it is never an
212    /// authorization and never widens what [`serve_stored`] decides.
213    pub(crate) fn origin(&self) -> &str {
214        &self.origin
215    }
216
217    /// The files this binding names and the project they are read in, as
218    /// `(project, [(path, tool, admitted digest)])`, for a store that derives
219    /// a new item from the bound one (`archive::bindings::carry`). `None` for
220    /// caller-supplied text and for a binding without a project. Read only:
221    /// what is returned is not a source until the admitted reader returns it
222    /// again ([`SourceRef::read_as_admitted`]).
223    pub(crate) fn file_sources(&self) -> Option<(&str, Vec<(&str, &str, &str)>)> {
224        let (Lineage::Sources { sources }, Some(project)) =
225            (&self.lineage, self.project.as_deref())
226        else {
227            return None;
228        };
229        let files = sources
230            .iter()
231            .map(|source| {
232                (
233                    source.path.as_str(),
234                    source.tool.as_str(),
235                    source.admitted_digest.as_str(),
236                )
237            })
238            .collect();
239        Some((project, files))
240    }
241
242    /// Whether the copy is bound to sources (and not merely caller-supplied).
243    #[cfg(test)]
244    pub(crate) fn source_bound(&self) -> bool {
245        matches!(self.lineage, Lineage::Sources { .. })
246    }
247}
248
249/// Publish what a stored item that was just SERVED derives from into the
250/// current call's source capture (`archive::authority`), so that a tool
251/// result delivering the item has an ordinary file lineage
252/// (`archive::delivered`). The one function for every store.
253///
254/// Call it only after `serve_stored` returned `Ok` for this `binding`: that
255/// is the stricter check (project authority, every source re-read, no
256/// classification raise, content rules). An item it refuses is not served
257/// and publishes nothing. What is published is then re-checked like any
258/// file source — unchanged, complete, storable under the current policy and
259/// role; a later classification raise below `Restricted` is caught by
260/// `serve_stored` on the next read of the item, not by the file lineage.
261///
262/// - source-bound item with a project: every source is published;
263/// - no binding (originless), caller-supplied text, or no bound project:
264///   the capture is marked incomplete. Such an item contributes no lineage,
265///   and a result that contains it stays unknown. No source is ever inferred.
266///
267/// Without a capture, or without protection in any layer, nothing happens.
268pub fn publish_served(binding: Option<&StoredBinding>) {
269    use crate::core::archive::authority::{self, ArchiveAuthority};
270    if authority::current_capture().is_none() || !source_authority_required() {
271        return;
272    }
273    let bound = binding.and_then(|binding| match (&binding.lineage, &binding.project) {
274        (Lineage::Sources { sources }, Some(project)) => Some((project, sources)),
275        _ => None,
276    });
277    let Some((project, sources)) = bound else {
278        authority::mark_unbound();
279        return;
280    };
281    for source in sources {
282        // A source that cannot be bound marks the capture incomplete itself.
283        if let Some(source) = ArchiveAuthority::admitted_source(
284            std::path::Path::new(project),
285            &source.path,
286            &source.tool,
287            &source.admitted_digest,
288        ) {
289            source.publish();
290        }
291    }
292}
293
294fn canonical_project(root: &str) -> Option<String> {
295    crate::core::pathutil::canonicalize_secure(std::path::Path::new(root))
296        .ok()
297        .and_then(|path| path.to_str().map(str::to_owned))
298}
299
300fn resolve_project(project: Option<&str>) -> Option<String> {
301    project
302        .filter(|root| !root.is_empty())
303        .map(|root| canonical_project(root).unwrap_or_else(|| root.to_owned()))
304}
305
306fn content_digest(text: &str) -> String {
307    blake3::hash(text.as_bytes()).to_hex().to_string()
308}
309
310fn policy_digest(policy: &AdmissionPolicy) -> String {
311    fingerprint_digest(&policy.fingerprint()).hex().to_owned()
312}
313
314/// The one "source authority required" predicate: protection is active in
315/// some layer — a project policy pack, the global gateway in governed or
316/// sovereign mode, or a global configuration that could not be loaded (mode
317/// unknown). Then every stored or derived copy needs a current source
318/// authority to be written or served. Otherwise (Community, developer mode)
319/// originless copies keep the behaviour of the tee store.
320#[must_use]
321pub fn source_authority_required() -> bool {
322    protected(&current_policy())
323}
324
325/// [`source_authority_required`] under a policy snapshot the caller holds,
326/// so that one operation decides protection and content under the same rules.
327#[must_use]
328pub(crate) fn source_authority_required_under(policy: &AdmissionPolicy) -> bool {
329    protected(policy)
330}
331
332fn protected(policy: &AdmissionPolicy) -> bool {
333    policy.config_unloadable()
334        || policy.detectors_mandatory()
335        || crate::core::policy::runtime::is_active()
336}
337
338/// Admit `text` for a derived store and bind the admitted copy to `lineage`.
339///
340/// Admission and binding use one policy snapshot: the digest in the binding
341/// is the policy the text was admitted under. Restricted content — in the
342/// text or in any source — is never stored. Under an active project policy
343/// and in governed or sovereign mode a copy without a project is refused: it
344/// could never be re-authorized.
345pub(crate) fn admit_and_bind(
346    text: &str,
347    origin: &str,
348    project: Option<&str>,
349    lineage: Lineage,
350) -> Result<(String, StoredBinding), Withheld> {
351    let policy = current_policy();
352    let project = resolve_project(project);
353    if project.is_none() && protected(&policy) {
354        return Err(Withheld::new(&[PROJECT_UNRESOLVED_CODE]));
355    }
356    let admission = super::semantic_deferred::for_stored_context(|| {
357        admit_carrier_text_under(text, Some(&policy), &super::carrier::STORED)
358    })?;
359    let mut classification = admission.classification;
360    if let Lineage::Sources { sources } = &lineage {
361        for source in sources {
362            classification = classification.join(source.classification);
363        }
364    }
365    if !admission.storable || classification >= ClassificationV1::Restricted {
366        return Err(Withheld::new(&[RESTRICTED_CODE]));
367    }
368    let binding = StoredBinding {
369        version: BINDING_VERSION,
370        origin: origin.to_owned(),
371        project,
372        writer_role: crate::core::policy::runtime::role_fingerprint(
373            &crate::core::roles::active_role(),
374        )
375        .ok()
376        .map(|fingerprint| fingerprint.to_hex().to_string()),
377        policy_digest: policy_digest(&policy),
378        content_digest: content_digest(&admission.text),
379        classification,
380        lineage,
381    };
382    Ok((admission.text, binding))
383}
384
385/// Serve a stored copy as a new delivery to a reader working in `project`.
386/// See the module documentation for the order of checks.
387pub(crate) fn serve_stored(
388    text: &str,
389    binding: Option<&StoredBinding>,
390    project: Option<&str>,
391) -> Result<String, Withheld> {
392    let policy = current_policy();
393    let protected = protected(&policy);
394    let Some(binding) = binding else {
395        // Legacy record: the originless rule.
396        if protected {
397            return Err(Withheld::no_source_authority());
398        }
399        return admit_stored_text(text, &policy);
400    };
401    if binding.version != BINDING_VERSION || binding.content_digest != content_digest(text) {
402        return Err(Withheld::new(&[BINDING_MISMATCH_CODE]));
403    }
404    // Project authority. Protected deployments refuse anything unresolved;
405    // Community developer mode refuses only a proven mismatch.
406    let reader = resolve_project(project);
407    match (binding.project.as_deref(), reader.as_deref()) {
408        (Some(bound), Some(reader)) if bound == reader => {}
409        (Some(_), Some(_)) => return Err(Withheld::new(&[PROJECT_MISMATCH_CODE])),
410        _ if protected => return Err(Withheld::new(&[PROJECT_UNRESOLVED_CODE])),
411        _ => {}
412    }
413    // Source authority: the proof of current rights.
414    if let Lineage::Sources { sources } = &binding.lineage {
415        let Some(root) = binding.project.as_deref() else {
416            return Err(Withheld::no_source_authority());
417        };
418        let mut remaining = crate::core::limits::max_read_bytes();
419        // A check, not a delivery: the re-reads stay out of the call's source
420        // capture. A refused item publishes nothing — otherwise the sources
421        // that passed before the failing one would give the refusal itself a
422        // file lineage — and a served one is published by `publish_served`.
423        crate::core::archive::authority::without_capture(|| {
424            sources
425                .iter()
426                .try_for_each(|source| source.reauthorize(root, &mut remaining))
427        })?;
428    }
429    if binding.classification >= ClassificationV1::Restricted {
430        return Err(Withheld::new(&[RESTRICTED_CODE]));
431    }
432    // Additional check, never the proof: the copy under today's content rules.
433    admit_stored_text(text, &policy)
434}
435
436/// Bind `text`, derived from `sources` (files of `project`), for a store
437/// kept outside this crate. Admission runs as in `admit_and_bind`; a text
438/// that admission would change is refused (`policy.content_redacted`): the
439/// store must hold exactly the text that is bound. No sources give the
440/// caller-supplied lineage every derivation without a source gets
441/// (`Lineage::of`).
442pub fn bind_derived_text(
443    text: &str,
444    origin: &str,
445    project: &str,
446    sources: Vec<SourceRef>,
447) -> Result<StoredBinding, Withheld> {
448    let lineage = Lineage::of(sources)?;
449    let (admitted, binding) = admit_and_bind(text, origin, Some(project), lineage)?;
450    if admitted == text {
451        Ok(binding)
452    } else {
453        Err(Withheld::new(&["policy.content_redacted"]))
454    }
455}
456
457/// The checks of `serve_stored` for a stored copy of `text` that the
458/// caller already holds: `Ok` when it may be served now to a reader working
459/// in `project`. Nothing is handed out.
460pub fn check_stored_copy(
461    text: &str,
462    binding: Option<&StoredBinding>,
463    project: Option<&str>,
464) -> Result<(), Withheld> {
465    serve_stored(text, binding, project).map(|_| ())
466}
467
468impl StoredBinding {
469    /// Whether every file this binding names still delivers, through the
470    /// admitted reader, what it was bound to; `true` for a binding without
471    /// files. A check, not a delivery: the re-reads stay out of the call's
472    /// source capture.
473    pub fn file_sources_hold(&self) -> bool {
474        self.file_sources().is_none_or(|(project, files)| {
475            crate::core::archive::authority::without_capture(|| {
476                files.into_iter().all(|(path, tool, digest)| {
477                    SourceRef::read_as_admitted(path, project, tool, digest).is_some()
478                })
479            })
480        })
481    }
482}
483
484fn admit_stored_text(text: &str, policy: &AdmissionPolicy) -> Result<String, Withheld> {
485    let admission = super::semantic_deferred::for_stored_context(|| {
486        admit_carrier_text_under(text, Some(policy), &super::carrier::STORED)
487    })?;
488    if admission.storable {
489        Ok(admission.text)
490    } else {
491        Err(Withheld::new(&[RESTRICTED_CODE]))
492    }
493}
494
495#[cfg(test)]
496mod tests;
497
498#[cfg(any(test, feature = "test-support"))]
499#[cfg_attr(not(test), allow(unused_imports))]
500pub mod lineage_support;