Skip to main content

lean_ctx/core/policy/org/
mod.rs

1//! Org-level policy distribution (ADR-023, GL #674).
2//!
3//! An organisation distributes a centrally signed [`OrgPolicyV1`] artifact.
4//! When installed, signature-verified, and the signer's key is trust-pinned,
5//! the runtime folds it in as an un-bypassable enforcement floor beneath the
6//! local project pack.
7
8pub(crate) mod marker;
9pub mod model;
10pub mod store;
11pub mod trust;
12
13use std::path::PathBuf;
14
15pub use model::{OrgPolicyV1, OrgVerifyResult};
16pub use trust::{TrustStore, TrustedKey};
17
18use crate::core::policy::ResolvedPolicy;
19
20#[must_use]
21pub fn org_key_id(org: &str) -> String {
22    let safe: String = org
23        .chars()
24        .map(|c| {
25            if c.is_ascii_alphanumeric() || c == '-' {
26                c.to_ascii_lowercase()
27            } else {
28                '-'
29            }
30        })
31        .collect();
32    format!("org-{safe}")
33}
34
35/// Load + verify + trust-check the installed org policy artifact.
36/// Returns `Some(resolved)` only when all three hold:
37/// 1. An artifact is installed on disk
38/// 2. Its Ed25519 signature is valid
39/// 3. The signer's public key is trust-pinned
40/// 4. The admin declared `enforced = true`
41#[must_use]
42pub fn active_resolved() -> Option<ResolvedPolicy> {
43    let artifact = store::load_active()?;
44
45    let result = artifact.verify();
46    if !result.signature_valid {
47        tracing::warn!(
48            "org policy: signature invalid ({}), ignoring",
49            result.error.as_deref().unwrap_or("unknown")
50        );
51        return None;
52    }
53
54    let signer = artifact.signer_public_key.as_deref()?;
55    if !trust::is_trusted(signer) {
56        tracing::debug!("org policy: signer not trust-pinned, ignoring (fail-open)");
57        return None;
58    }
59
60    if !artifact.enforced {
61        tracing::debug!("org policy: admin did not set enforced=true, skipping");
62        return None;
63    }
64
65    match artifact.resolved() {
66        Ok(resolved) => Some(resolved),
67        Err(e) => {
68            tracing::warn!("org policy: failed to resolve pack: {e}");
69            None
70        }
71    }
72}
73
74/// Error-preserving resolution for enforcement callers. Only an absent policy
75/// or a verified, trusted policy explicitly marked unenforced yields `None`.
76///
77/// A recorded requirement (the required marker) changes both cases: without a
78/// policy it is an error, and a version-one policy does not satisfy it. A
79/// version-two artifact is not a version-one artifact and fails to load, which
80/// is an error as well. This is the same floor the admission snapshot applies
81/// (`runtime::refresh`), for callers that resolve the policy directly.
82pub fn active_resolved_checked() -> Result<Option<ResolvedPolicy>, String> {
83    AuthoritySnapshot::capture()?.resolve()
84}
85
86/// The bytes organization authority is read from, captured once: the selected
87/// policy source, the trust store and the required marker.
88///
89/// The fingerprint and the resolution both come from these captured bytes, so
90/// a caller that keeps a resolved policy under the fingerprint can never pair
91/// one authority's rules with another's identity: there is no second read
92/// between the two. Whoever reuses a kept resolution captures again and
93/// compares fingerprints first.
94pub(crate) struct AuthoritySnapshot {
95    fingerprint: blake3::Hash,
96    /// The fingerprint's state before the marker text.
97    before_marker: blake3::Hasher,
98    policy: Option<String>,
99    trust: Option<String>,
100    marker: Option<String>,
101}
102
103impl AuthoritySnapshot {
104    /// Reads the three inputs once. Paths are part of the fingerprint and
105    /// absence is distinct from emptiness; any read error is an error.
106    pub(crate) fn capture() -> Result<Self, String> {
107        let (policy_path, policy_required) = store::selected_source()?;
108        let mut hash = blake3::Hasher::new();
109        // The artifact alone is read at its own, larger bound; trust store
110        // and marker keep the usual one.
111        let mut read =
112            |path: PathBuf, required: bool, artifact: bool| -> Result<Option<String>, String> {
113                let path_bytes = path.as_os_str().as_encoded_bytes();
114                hash.update(&(path_bytes.len() as u64).to_le_bytes());
115                hash.update(path_bytes);
116                let text = if artifact {
117                    crate::core::policy::files::read_org_artifact(&path, required, false)?
118                } else {
119                    crate::core::policy::files::read(&path, required)?
120                };
121                hash_text(&mut hash, text.as_deref());
122                Ok(text)
123            };
124        let policy = read(policy_path, policy_required, true)?;
125        let trust = read(trust::trust_path_read_only()?, false, false)?;
126        // The marker goes last, path before text, so that the fingerprint of
127        // the same authority under another marker can be derived.
128        let marker_path = marker::marker_path_read_only()?;
129        let path_bytes = marker_path.as_os_str().as_encoded_bytes();
130        hash.update(&(path_bytes.len() as u64).to_le_bytes());
131        hash.update(path_bytes);
132        let marker = crate::core::policy::files::read(&marker_path, false)?;
133        let before_marker = hash.clone();
134        hash_text(&mut hash, marker.as_deref());
135        Ok(Self {
136            fingerprint: hash.finalize(),
137            before_marker,
138            policy,
139            trust,
140            marker,
141        })
142    }
143
144    #[cfg(any(test, feature = "enterprise"))]
145    pub(crate) fn fingerprint(&self) -> blake3::Hash {
146        self.fingerprint
147    }
148
149    /// Whether the captured marker records a requirement in force. A marker
150    /// that cannot be parsed is an error.
151    fn required(&self) -> Result<bool, String> {
152        Ok(self
153            .marker
154            .as_deref()
155            .map(marker::parse)
156            .transpose()?
157            .is_some_and(|marker| marker.is_required()))
158    }
159
160    /// Resolves the organization policy from the captured bytes, without
161    /// reading anything again. Same floor as the admission snapshot: under a
162    /// recorded requirement a missing policy is an error and a version-one
163    /// policy does not qualify; a version-two artifact is not a version-one
164    /// artifact and fails to parse, which is an error too.
165    pub(crate) fn resolve(&self) -> Result<Option<ResolvedPolicy>, String> {
166        self.resolve_observed().map(|(floor, _)| floor)
167    }
168
169    /// [`Self::resolve`], together with what the verifier's answer stated
170    /// when this very resolution asked it: handed out only after the
171    /// recapture and both bounds, with the floor it belongs to. `None`
172    /// whenever no verifier was asked. For a caller that records what it
173    /// decided under; `resolve` is this without the observation.
174    pub(crate) fn resolve_observed(
175        &self,
176    ) -> Result<(Option<ResolvedPolicy>, Option<Observation>), String> {
177        let required = self.required()?;
178        let Some(policy) = self.policy.as_deref() else {
179            if required {
180                return Err("required organization policy is missing".into());
181            }
182            return Ok((None, None));
183        };
184        // Version two is decided by the authenticated verifier for these
185        // captured bytes, on this call; nothing here decides it locally.
186        if is_v2(policy) {
187            let verified = verify_v2(
188                policy,
189                self.trust.as_deref(),
190                self.marker.as_deref(),
191                std::time::Instant::now() + RESOLVE_DEADLINE,
192            )?;
193            // The answer took time. Before the floor is used, the authority
194            // is read again and must be exactly what was captured — except
195            // for the marker, which must be exactly what this decision left
196            // stored. Any other change to policy, trust store or marker
197            // while waiting refuses.
198            let mut expected = self.before_marker.clone();
199            hash_text(&mut expected, Some(&verified.marker));
200            if Self::capture()?.fingerprint != expected.finalize() {
201                return Err("organization authority changed during verification".into());
202            }
203            // That read took time too: horizon and deadline once more, last.
204            verified.lifetime.check()?;
205            return Ok((verified.floor, Some(verified.observation)));
206        }
207        // Only a version-two artifact may exceed the usual input bound.
208        if policy.len() as u64 > crate::core::policy::files::MAX_BYTES {
209            return Err("policy input exceeds byte limit".into());
210        }
211        if required {
212            return Err("required organization policy is not verifiable".into());
213        }
214        let artifact = OrgPolicyV1::from_json(policy)?;
215        let trust: TrustStore = match self.trust.as_deref() {
216            Some(text) => {
217                toml::from_str(text).map_err(|_| "org trust input is invalid".to_string())?
218            }
219            None => TrustStore::default(),
220        };
221        resolve_checked(&artifact, &trust).map(|floor| (floor, None))
222    }
223}
224
225fn hash_text(hash: &mut blake3::Hasher, text: Option<&str>) {
226    match text {
227        Some(text) => {
228            hash.update(&[1]);
229            hash.update(&(text.len() as u64).to_le_bytes());
230            hash.update(text.as_bytes());
231        }
232        None => {
233            hash.update(&[0]);
234        }
235    }
236}
237
238/// Whether installed organization-policy text is a version-two artifact: one
239/// this Engine cannot verify by itself. Read from the artifact's own schema
240/// field; anything unreadable is simply not version two here and fails the
241/// version-one checks instead.
242pub(crate) fn is_v2(text: &str) -> bool {
243    let Ok(value) = serde_json::from_str::<serde_json::Value>(text) else {
244        return false;
245    };
246    let schema = value
247        .get("schema")
248        .or_else(|| value.get("statement").and_then(|s| s.get("schema")))
249        .and_then(serde_json::Value::as_str);
250    schema == Some(lean_ctx_protocol::org_policy_verifier::ORG_POLICY_V2_ARTIFACT_SCHEMA_ID)
251}
252
253/// A version-two organization policy as the verifier decides it for exactly
254/// these captured bytes, now.
255///
256/// The marker must already be stored: it names the organization and is the
257/// predecessor the decision is recorded on. Without one nothing is asked and
258/// nothing is created — the first marker of an endpoint is a separately
259/// authorized enrollment, not an outcome of a verification. The accepted
260/// state is recorded on that exact marker before the policy is used; a marker
261/// that changed, was damaged or was lost in between refuses. Nothing of an
262/// earlier answer is kept: every call asks again.
263///
264/// `deadline` is the caller's one monotonic bound for the whole decision:
265/// the verifier gets only the time left, the marker's replacement is not
266/// admitted at or after it, and nothing is returned at or after it. The
267/// bound is that admission: once admitted, the operating system's replace
268/// and directory sync run to their end, and a marker that was written is
269/// never taken back — a later error here does not undo it.
270pub(crate) fn verify_v2(
271    artifact: &str,
272    trust: Option<&str>,
273    marker_text: Option<&str>,
274    deadline: std::time::Instant,
275) -> Result<VerifiedV2, String> {
276    use crate::core::intelligence_runtime::org_policy_verifier::{Accepted, Snapshot, verify};
277    use lean_ctx_protocol::org_policy_verifier::{
278        OrgRequiredMarkerEntryV1, OrgRequiredMarkerStateV1,
279    };
280
281    let marker_text =
282        marker_text.ok_or("organization policy has no recorded requirement to verify against")?;
283    let stored = marker::parse(marker_text)?;
284    let accepted = verify(
285        &Snapshot {
286            organization: &stored.organization_id,
287            artifact: artifact.as_bytes(),
288            trust: trust.map(str::as_bytes),
289            marker: Some(marker_text.as_bytes()),
290        },
291        deadline,
292    )
293    .map_err(|_| "organization policy could not be verified")?;
294    // What this very answer states, taken before the answer is consumed: the
295    // observation below is made of nothing else.
296    let (state, sequence, pack, observed, statement) = match accepted {
297        Accepted::Enforce {
298            pack_toml,
299            policy_digest,
300            sequence,
301            valid_until,
302        } => {
303            let statement = policy_digest.as_str().to_owned();
304            (
305                OrgRequiredMarkerStateV1::Required { policy_digest },
306                sequence,
307                Some((pack_toml, valid_until)),
308                ObservedState::Required,
309                statement,
310            )
311        }
312        Accepted::NotRequired {
313            unpin_digest,
314            sequence,
315        } => {
316            let statement = unpin_digest.as_str().to_owned();
317            (
318                OrgRequiredMarkerStateV1::Unpinned { unpin_digest },
319                sequence,
320                None,
321                ObservedState::Unpinned,
322                statement,
323            )
324        }
325    };
326    let recorded_at = lean_ctx_protocol::TeamTimestampV1::new(
327        chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Nanos, true),
328    )
329    .map_err(|_| "organization policy decision could not be dated")?;
330    let (_, marker) = marker::advance_within(
331        &stored.organization_id,
332        OrgRequiredMarkerEntryV1 {
333            sequence,
334            recorded_at,
335            state,
336        },
337        marker::Predecessor::Stored(marker_text),
338        deadline,
339    )
340    .map_err(|_| "organization policy decision could not be recorded")?;
341    let (floor, horizon) = match pack {
342        Some((pack_toml, valid_until)) => {
343            let floor = crate::core::policy::parse(&pack_toml)
344                .and_then(|pack| crate::core::policy::resolve(&pack))
345                .map_err(|_| "verified organization policy is invalid")?;
346            // An unreadable horizon is an expired one.
347            let horizon = chrono::DateTime::parse_from_rfc3339(valid_until.as_str())
348                .map_err(|_| "verified organization policy has expired")?
349                .with_timezone(&chrono::Utc);
350            (Some(floor), Some(horizon))
351        }
352        None => (None, None),
353    };
354    let lifetime = Lifetime { horizon, deadline };
355    // Recording may have waited for the marker lock and compiling took time.
356    lifetime.check()?;
357    Ok(VerifiedV2 {
358        floor,
359        marker,
360        lifetime,
361        // Only here, after the decision was recorded on the marker, the pack
362        // compiled and both bounds checked: an answer that got no further
363        // leaves no observation behind.
364        observation: Observation {
365            state: observed,
366            statement,
367            policy_version: sequence,
368            valid_until: horizon,
369        },
370    })
371}
372
373/// What one verification established: the floor to enforce, if any, the
374/// marker text that is stored now — the captured one, or the one this very
375/// decision appended — how long the decision may be acted on, and what the
376/// answer stated, for the record of whatever is decided under it.
377pub(crate) struct VerifiedV2 {
378    pub(crate) floor: Option<ResolvedPolicy>,
379    pub(crate) marker: String,
380    pub(crate) lifetime: Lifetime,
381    pub(crate) observation: Observation,
382}
383
384/// Which of the two authenticated answers was observed.
385#[derive(Debug, Clone, Copy, PartialEq, Eq)]
386pub(crate) enum ObservedState {
387    /// A signed policy is enforced.
388    Required,
389    /// The requirement was lifted by a signed unpin. This is an organization
390    /// decision in force, not the absence of an organization: it is recorded
391    /// although it leaves no floor.
392    Unpinned,
393}
394
395/// The organization decision one verification observed, for a receipt's
396/// trace: which statement the verifier accepted, the policy version it
397/// enforces, and — for an enforced policy — the signed end that was checked.
398///
399/// Immutable and content-free: a digest, a number, a closed state and an
400/// instant, all from the same accepted answer. No policy text, trust store,
401/// marker, key, path or credential is in it. `policy_version` is the signed
402/// sequence of the organization's statement; it is NOT the `version` a policy
403/// pack carries in its own text, which stays the pack's.
404#[derive(Debug, Clone, PartialEq, Eq)]
405pub(crate) struct Observation {
406    state: ObservedState,
407    /// `sha256:<hex>` of the accepted statement: the policy, or the unpin.
408    statement: String,
409    policy_version: u64,
410    /// The signed horizon of an enforced policy as this Engine parsed and
411    /// checked it; `None` for an unpin, which has none.
412    valid_until: Option<chrono::DateTime<chrono::Utc>>,
413}
414
415impl Observation {
416    /// A signed unpin as a verification would have observed it. Tests only:
417    /// outside of them an observation comes from `verify_v2` and nowhere else.
418    #[cfg(test)]
419    pub(crate) fn unpinned_for_test(policy_version: u64, fill: char) -> Self {
420        Self {
421            state: ObservedState::Unpinned,
422            statement: format!("sha256:{}", fill.to_string().repeat(64)),
423            policy_version,
424            valid_until: None,
425        }
426    }
427
428    pub(crate) fn state(&self) -> ObservedState {
429        self.state
430    }
431
432    pub(crate) fn statement(&self) -> &str {
433        &self.statement
434    }
435
436    pub(crate) fn policy_version(&self) -> u64 {
437        self.policy_version
438    }
439
440    pub(crate) fn valid_until(&self) -> Option<chrono::DateTime<chrono::Utc>> {
441        self.valid_until
442    }
443}
444
445/// The two bounds of one verified decision: the signed horizon of an enforced
446/// policy (none when the requirement was lifted) and the request's own
447/// monotonic deadline. Whoever acts on the decision checks both on fresh
448/// clocks at the last moment before acting — after any further read or wait,
449/// not only when the answer arrived.
450#[derive(Debug, Clone, Copy)]
451pub(crate) struct Lifetime {
452    horizon: Option<chrono::DateTime<chrono::Utc>>,
453    deadline: std::time::Instant,
454}
455
456impl Lifetime {
457    /// A decision signed up to `horizon` and asked under `deadline`. Tests
458    /// only: outside of them a lifetime comes from `verify_v2` and nowhere else.
459    #[cfg(test)]
460    pub(crate) fn for_test(
461        horizon: Option<chrono::DateTime<chrono::Utc>>,
462        deadline: std::time::Instant,
463    ) -> Self {
464        Self { horizon, deadline }
465    }
466
467    /// The signed instant an enforced policy runs out at, for the compiled
468    /// policy to carry: whoever keeps that policy keeps its end with it.
469    pub(crate) fn horizon(&self) -> Option<chrono::DateTime<chrono::Utc>> {
470        self.horizon
471    }
472
473    /// Both bounds are exclusive, like the verifier's own check.
474    pub(crate) fn check(&self) -> Result<(), String> {
475        self.check_horizon()?;
476        if std::time::Instant::now() >= self.deadline {
477            return Err("organization policy verification deadline exceeded".into());
478        }
479        Ok(())
480    }
481
482    /// The signed horizon alone: for a holder that outlives the request which
483    /// obtained the decision, and so has no request deadline any more.
484    pub(crate) fn check_horizon(&self) -> Result<(), String> {
485        if self
486            .horizon
487            .is_some_and(|horizon| chrono::Utc::now() >= horizon)
488        {
489            return Err("verified organization policy has expired".into());
490        }
491        Ok(())
492    }
493}
494
495/// Bound for a verification asked for outside the admission worker, which
496/// brings its own.
497const RESOLVE_DEADLINE: std::time::Duration = std::time::Duration::from_secs(5);
498
499pub(crate) fn resolve_checked(
500    artifact: &OrgPolicyV1,
501    trusted: &TrustStore,
502) -> Result<Option<ResolvedPolicy>, String> {
503    if !artifact.verify().signature_valid {
504        return Err("org policy signature invalid".into());
505    }
506    let signer = artifact
507        .signer_public_key
508        .as_deref()
509        .ok_or_else(|| "org policy signer missing".to_string())?;
510    if !trusted
511        .keys
512        .iter()
513        .any(|key| key.public_key == signer && key.org == artifact.org)
514    {
515        return Err("org policy signer is not pinned for this organization".into());
516    }
517    let resolved = artifact.resolved().map_err(|error| error.to_string())?;
518    Ok(artifact.enforced.then_some(resolved))
519}
520
521/// Status and the captured-authority path against real files in an isolated
522/// data directory: what the status reports must be what enforcement decides.
523#[cfg(test)]
524mod authority_tests {
525    use super::*;
526    use lean_ctx_protocol::org_policy_verifier::{
527        OrgRequiredMarkerEntryV1, OrgRequiredMarkerStateV1,
528    };
529    use lean_ctx_protocol::{RecordDigest, TeamId, TeamTimestampV1};
530
531    const PACK: &str = "name = \"floor\"\nversion = \"1.0.0\"\ndescription = \"test\"\n[context]\ndeny_tools = [\"ctx_shell\"]\n";
532
533    /// A signed, enforced version-one policy for `acme`, installed.
534    fn install_policy() -> OrgPolicyV1 {
535        let key = ed25519_dalek::SigningKey::from_bytes(&[23; 32]);
536        let mut artifact = OrgPolicyV1::build("acme", "1", true, PACK).unwrap();
537        artifact.sign_with_key(&key);
538        store::install(&artifact).unwrap();
539        artifact
540    }
541
542    fn pin(org: &str, artifact: &OrgPolicyV1) {
543        trust::pin(org, artifact.signer_public_key.as_deref().unwrap()).unwrap();
544    }
545
546    fn require(sequence: u64) {
547        let entry = OrgRequiredMarkerEntryV1 {
548            sequence,
549            recorded_at: TeamTimestampV1::new("2026-10-10T09:00:00Z").unwrap(),
550            state: OrgRequiredMarkerStateV1::Required {
551                policy_digest: RecordDigest::new(format!("sha256:{}", "a".repeat(64))).unwrap(),
552            },
553        };
554        // The fixture stands in for an accepted decision made on the marker
555        // as it is stored now — or for the first one when none is stored.
556        let path = marker::marker_path_read_only().unwrap();
557        let stored = crate::core::policy::files::read(&path, false).unwrap();
558        let predecessor = stored.as_deref().map_or(
559            marker::Predecessor::NeverStored,
560            marker::Predecessor::Stored,
561        );
562        marker::advance(&TeamId::new("acme").unwrap(), entry, predecessor).unwrap();
563    }
564
565    #[test]
566    fn status_reports_a_key_pinned_for_another_organization_as_not_applied() {
567        let _data = crate::core::data_dir::isolated_data_dir();
568        let artifact = install_policy();
569        pin("other", &artifact);
570
571        let status = status();
572        assert!(status.present && status.signature_valid);
573        assert!(!status.trusted, "the key is pinned, but not for this org");
574        assert!(!status.applied);
575        assert!(
576            active_resolved_checked().is_err(),
577            "enforcement refuses the same state"
578        );
579
580        pin("acme", &artifact);
581        // `pin` keeps one entry per key: the wrong-org pin stays the only one.
582        assert!(!self::status().trusted);
583
584        // The exact assignment the commands admit on is the one enforcement
585        // uses: this key is pinned, but not for this organization — and
586        // pinning again says so instead of moving the pin.
587        let key = artifact.signer_public_key.as_deref().unwrap();
588        assert!(trust::is_trusted(key), "the key alone is pinned");
589        assert!(!trust::is_trusted_for("acme", key));
590        assert!(trust::is_trusted_for("other", key));
591        assert_eq!(trust::pinned_organization(key).as_deref(), Some("other"));
592        assert_eq!(
593            trust::pin_for("acme", key).unwrap(),
594            trust::PinOutcome::PinnedForAnother("other".into())
595        );
596        assert_eq!(
597            trust::pin_for("other", key).unwrap(),
598            trust::PinOutcome::AlreadyPinned
599        );
600        assert!(!trust::is_trusted_for("acme", key), "nothing was moved");
601        assert_eq!(trust::trusted_keys().len(), 1);
602    }
603
604    /// The status observes: it reads what is stored and leaves the endpoint as
605    /// it found it — no directory is created for it, with nothing stored and
606    /// with an explicit source that does not exist (an error, not "absent").
607    #[test]
608    fn status_creates_nothing_and_reports_a_missing_explicit_source() {
609        let data = crate::core::data_dir::isolated_data_dir();
610        let observed = || {
611            [
612                store::selected_source().unwrap().0,
613                trust::trust_path_read_only().unwrap(),
614                marker::marker_path_read_only().unwrap(),
615            ]
616            .map(|path| {
617                (
618                    path.exists(),
619                    path.parent().is_some_and(std::path::Path::exists),
620                )
621            })
622        };
623        let before = observed();
624        let status = status();
625        assert!(!status.present && status.resolve_error.is_none());
626        assert_eq!(status.recorded, RecordedRequirement::Absent);
627        assert_eq!(observed(), before, "the status left a trace on disk");
628
629        let missing = data.path().join("does-not-exist.json");
630        crate::test_env::set_var("LEANCTX_ORG_POLICY", &missing);
631        let status = self::status();
632        crate::test_env::remove_var("LEANCTX_ORG_POLICY");
633        assert!(
634            status.resolve_error.is_some(),
635            "an explicit source that is missing is not 'nothing installed'"
636        );
637        assert!(!missing.exists());
638    }
639
640    #[test]
641    fn status_and_enforcement_agree_for_a_correctly_pinned_policy() {
642        let _data = crate::core::data_dir::isolated_data_dir();
643        let artifact = install_policy();
644        pin("acme", &artifact);
645
646        let status = status();
647        assert!(status.trusted && status.applied && status.resolve_error.is_none());
648        assert!(active_resolved_checked().unwrap().is_some());
649    }
650
651    #[test]
652    fn a_recorded_requirement_or_a_damaged_marker_is_never_reported_as_applied() {
653        let _data = crate::core::data_dir::isolated_data_dir();
654        let artifact = install_policy();
655        pin("acme", &artifact);
656        require(1);
657
658        let status = status();
659        assert!(status.present && status.trusted);
660        assert!(!status.applied, "a version-one policy does not satisfy it");
661        assert!(status.resolve_error.is_some());
662        assert!(active_resolved_checked().is_err());
663
664        // Artifact removed: still required, still an error, not "absent".
665        store::uninstall().unwrap();
666        let status = self::status();
667        assert!(!status.present && !status.applied);
668        assert!(status.resolve_error.is_some());
669        assert!(active_resolved_checked().is_err());
670
671        // A damaged marker is an error for both, with or without a policy.
672        std::fs::write(marker::marker_path_read_only().unwrap(), b"{}").unwrap();
673        assert!(self::status().resolve_error.is_some());
674        assert!(active_resolved_checked().is_err());
675        store::install(&artifact).unwrap();
676        let status = self::status();
677        assert!(!status.applied && status.resolve_error.is_some());
678        assert!(AuthoritySnapshot::capture().unwrap().resolve().is_err());
679    }
680
681    /// The stored record is read as what it is. A signed unpin without any
682    /// artifact is a decision on record — neither an error nor "no central
683    /// policy" — and a record that cannot be read is unavailable, not absent.
684    #[test]
685    fn the_stored_record_is_read_as_recorded_absent_or_unavailable() {
686        let _data = crate::core::data_dir::isolated_data_dir();
687        assert_eq!(recorded_requirement(), RecordedRequirement::Absent);
688        assert_eq!(status().recorded, RecordedRequirement::Absent);
689
690        require(3);
691        match recorded_requirement() {
692            RecordedRequirement::Recorded {
693                organization,
694                required: true,
695                sequence: 3,
696                entries: 1,
697                ..
698            } => assert_eq!(organization, "acme"),
699            other => panic!("not the recorded requirement: {other:?}"),
700        }
701
702        let path = marker::marker_path_read_only().unwrap();
703        let stored = crate::core::policy::files::read(&path, false)
704            .unwrap()
705            .unwrap();
706        marker::advance(
707            &TeamId::new("acme").unwrap(),
708            OrgRequiredMarkerEntryV1 {
709                sequence: 4,
710                recorded_at: TeamTimestampV1::new("2026-10-11T09:00:00Z").unwrap(),
711                state: OrgRequiredMarkerStateV1::Unpinned {
712                    unpin_digest: RecordDigest::new(format!("sha256:{}", "b".repeat(64))).unwrap(),
713                },
714            },
715            marker::Predecessor::Stored(&stored),
716        )
717        .unwrap();
718        let status = status();
719        assert!(!status.present && status.source.is_none());
720        assert!(
721            status.resolve_error.is_none(),
722            "a lifted requirement refuses nothing"
723        );
724        match status.recorded {
725            RecordedRequirement::Recorded {
726                required: false,
727                sequence: 4,
728                entries: 2,
729                statement,
730                ..
731            } => assert_eq!(statement, format!("sha256:{}", "b".repeat(64))),
732            other => panic!("the unpin is not on record: {other:?}"),
733        }
734
735        std::fs::write(&path, b"{}").unwrap();
736        assert!(matches!(
737            recorded_requirement(),
738            RecordedRequirement::Unavailable(_)
739        ));
740    }
741
742    #[test]
743    fn a_capture_resolves_its_own_bytes_whatever_changes_afterwards() {
744        let _data = crate::core::data_dir::isolated_data_dir();
745        let artifact = install_policy();
746        pin("acme", &artifact);
747
748        let enforced = AuthoritySnapshot::capture().unwrap();
749        // Between capture and resolve: the policy is removed, the trust store
750        // emptied and a requirement recorded.
751        store::uninstall().unwrap();
752        trust::remove(artifact.signer_public_key.as_deref().unwrap()).unwrap();
753        require(1);
754        assert!(
755            enforced.resolve().unwrap().is_some(),
756            "the capture answers for the bytes it holds"
757        );
758
759        let now = AuthoritySnapshot::capture().unwrap();
760        assert_ne!(now.fingerprint(), enforced.fingerprint());
761        assert!(now.resolve().is_err(), "required, and nothing installed");
762
763        // Each of the three inputs is part of the identity.
764        let before = AuthoritySnapshot::capture().unwrap().fingerprint();
765        store::install(&artifact).unwrap();
766        let with_policy = AuthoritySnapshot::capture().unwrap().fingerprint();
767        pin("acme", &artifact);
768        let with_trust = AuthoritySnapshot::capture().unwrap().fingerprint();
769        require(2);
770        let with_marker = AuthoritySnapshot::capture().unwrap().fingerprint();
771        assert!(before != with_policy && with_policy != with_trust && with_trust != with_marker);
772    }
773
774    #[test]
775    fn an_unreadable_explicit_source_refuses_instead_of_resolving_to_no_policy() {
776        let data = crate::core::data_dir::isolated_data_dir();
777        let missing = data.path().join("does-not-exist.json");
778        crate::test_env::set_var("LEANCTX_ORG_POLICY", &missing);
779        let captured = AuthoritySnapshot::capture();
780        crate::test_env::remove_var("LEANCTX_ORG_POLICY");
781        assert!(captured.is_err(), "an explicit source that is absent");
782    }
783}
784
785#[cfg(test)]
786mod checked_tests {
787    use super::*;
788
789    fn fixture(enforced: bool) -> (OrgPolicyV1, TrustStore) {
790        let key = ed25519_dalek::SigningKey::from_bytes(&[19; 32]);
791        let mut artifact = OrgPolicyV1::build(
792            "acme", "1", enforced,
793            "name = \"floor\"\nversion = \"1.0.0\"\ndescription = \"test\"\n[context]\ndeny_tools = [\"ctx_shell\"]\n",
794        ).unwrap();
795        artifact.sign_with_key(&key);
796        let trusted = TrustStore {
797            keys: vec![TrustedKey {
798                org: "acme".into(),
799                public_key: artifact.signer_public_key.clone().unwrap(),
800                added_at: "2026-09-09T00:00:00Z".into(),
801            }],
802        };
803        (artifact, trusted)
804    }
805
806    #[test]
807    fn checked_resolution_requires_valid_signature_and_org_pin() {
808        let (mut artifact, mut trusted) = fixture(true);
809        assert!(resolve_checked(&artifact, &trusted).unwrap().is_some());
810        assert!(resolve_checked(&artifact, &TrustStore::default()).is_err());
811        trusted.keys[0].org = "other".into();
812        assert!(resolve_checked(&artifact, &trusted).is_err());
813        trusted.keys[0].org = "acme".into();
814        artifact.enforced = false;
815        assert!(resolve_checked(&artifact, &trusted).is_err());
816    }
817
818    #[test]
819    fn checked_resolution_allows_only_trusted_signed_disable() {
820        let (artifact, trusted) = fixture(false);
821        assert!(resolve_checked(&artifact, &trusted).unwrap().is_none());
822        assert!(resolve_checked(&artifact, &TrustStore::default()).is_err());
823    }
824}
825
826/// The endpoint's stored requirement record, as it reads now.
827///
828/// A record of the last decision that was accepted and stored — read from the
829/// marker file and nothing else. It is not a verification: no verifier is
830/// asked, nothing is appended, and what it shows may be older than what the
831/// enforcement pipeline would decide at the next use.
832#[derive(Debug, Clone, Default, PartialEq, Eq)]
833pub enum RecordedRequirement {
834    /// No record is stored on this endpoint.
835    #[default]
836    Absent,
837    /// A record is stored and cannot be read or parsed. Never "absent":
838    /// governed operations refuse while it is in this state.
839    Unavailable(String),
840    /// The newest stored entry.
841    Recorded {
842        organization: String,
843        /// `true`: a policy is required. `false`: the requirement was lifted
844        /// by a signed unpin — an organization decision that stays on record.
845        required: bool,
846        /// The signed sequence of the recorded statement.
847        sequence: u64,
848        /// `sha256:<hex>` of the recorded statement (the policy, or the unpin).
849        statement: String,
850        recorded_at: String,
851        /// Entries on record; this is the last of them.
852        entries: usize,
853    },
854}
855
856/// Reads the stored requirement record. Read-only: see [`RecordedRequirement`].
857#[must_use]
858pub fn recorded_requirement() -> RecordedRequirement {
859    use lean_ctx_protocol::org_policy_verifier::OrgRequiredMarkerStateV1;
860
861    let text = match marker::marker_path_read_only()
862        .and_then(|path| crate::core::policy::files::read(&path, false))
863    {
864        Ok(Some(text)) => text,
865        Ok(None) => return RecordedRequirement::Absent,
866        Err(error) => return RecordedRequirement::Unavailable(error),
867    };
868    let stored = match marker::parse(&text) {
869        Ok(stored) => stored,
870        Err(error) => return RecordedRequirement::Unavailable(error),
871    };
872    let current = stored.current();
873    let (required, statement) = match &current.state {
874        OrgRequiredMarkerStateV1::Required { policy_digest } => (true, policy_digest.as_str()),
875        OrgRequiredMarkerStateV1::Unpinned { unpin_digest } => (false, unpin_digest.as_str()),
876    };
877    RecordedRequirement::Recorded {
878        organization: stored.organization_id.as_str().to_owned(),
879        required,
880        sequence: current.sequence,
881        statement: statement.to_owned(),
882        recorded_at: current.recorded_at.as_str().to_owned(),
883        entries: stored.entries.len(),
884    }
885}
886
887#[derive(Debug, Clone, Default)]
888pub struct OrgStatus {
889    /// The stored requirement record, read as it is — not a verification.
890    pub recorded: RecordedRequirement,
891    pub present: bool,
892    pub source: Option<PathBuf>,
893    pub org: Option<String>,
894    pub policy_version: Option<String>,
895    pub enforced: bool,
896    pub issued_at: Option<String>,
897    pub signer_public_key: Option<String>,
898    pub signature_valid: bool,
899    pub trusted: bool,
900    pub applied: bool,
901    pub resolve_error: Option<String>,
902    pub pinned_anchors: usize,
903    /// The resolved pack of the version-one artifact this status read — what
904    /// a floor made of it would be. A preview of stored bytes, independent of
905    /// trust and of `applied`; `None` without such an artifact.
906    pub floor_preview: Option<ResolvedPolicy>,
907}
908
909#[must_use]
910pub fn status() -> OrgStatus {
911    // An observation and nothing else. Every input is read through the
912    // read-only resolutions the admission snapshot uses — the selected
913    // source, the trust store, the stored record — each once: this function
914    // creates and repairs no directory, asks no verifier and writes nothing.
915    // What it reports is a diagnosis of what is stored, not an authority.
916    let recorded = recorded_requirement();
917    // The requirement, from that one read of the record. A record that
918    // cannot be read is an error, never "no requirement".
919    let required = match &recorded {
920        RecordedRequirement::Absent => Ok(false),
921        RecordedRequirement::Recorded { required, .. } => Ok(*required),
922        RecordedRequirement::Unavailable(error) => Err(error.clone()),
923    };
924    // An unreadable trust store pins nothing here and is reported: it is
925    // not an empty store, and enforcement refuses on it.
926    let (anchors, trust_error) = match trust::load_read_only() {
927        Ok(store) => (store.keys, None),
928        Err(error) => (Vec::new(), Some(error)),
929    };
930    let pinned_anchors = anchors.len();
931    // The selected source, read once. An explicit source that is missing or
932    // unreadable is an error — it never falls back to another artifact and
933    // is never "nothing installed".
934    let (source, text) = match store::selected_source() {
935        Ok((path, explicit)) => {
936            match crate::core::policy::files::read_org_artifact(&path, explicit, false) {
937                Ok(text) => (text.as_ref().map(|_| path), text),
938                Err(error) => {
939                    return OrgStatus {
940                        recorded,
941                        present: true,
942                        source: Some(path),
943                        resolve_error: Some(error),
944                        pinned_anchors,
945                        ..Default::default()
946                    };
947                }
948            }
949        }
950        Err(error) => {
951            return OrgStatus {
952                recorded,
953                resolve_error: Some(error),
954                pinned_anchors,
955                ..Default::default()
956            };
957        }
958    };
959    let artifact = text
960        .as_deref()
961        .filter(|text| text.len() as u64 <= crate::core::policy::files::MAX_BYTES)
962        .and_then(|text| OrgPolicyV1::from_json(text).ok());
963    let Some(artifact) = artifact else {
964        // "No version-one policy" is not always "no organization policy":
965        // say so when a requirement is recorded, when the record is damaged,
966        // or when something is installed that this status cannot read.
967        let resolve_error = match required {
968            Ok(true) => Some(
969                "an organization policy is required on this endpoint, but none that can be \
970                 verified is installed"
971                    .to_string(),
972            ),
973            Ok(false) => source
974                .as_ref()
975                .map(|_| {
976                    "the installed organization policy is not a version-one artifact; it needs \
977                     a verifier"
978                        .to_string()
979                })
980                .or(trust_error),
981            Err(error) => Some(error),
982        };
983        return OrgStatus {
984            recorded,
985            // Something is installed even when this status cannot read it.
986            present: source.is_some(),
987            source,
988            resolve_error,
989            pinned_anchors,
990            ..Default::default()
991        };
992    };
993    let verify = artifact.verify();
994    let signer_key = artifact.signer_public_key.clone();
995    // Trusted means what enforcement means by it (`resolve_checked`): this key
996    // pinned for this artifact's organization. The same key pinned under
997    // another organization does not count.
998    let trusted = signer_key.as_deref().is_some_and(|signer| {
999        anchors
1000            .iter()
1001            .any(|key| key.public_key == signer && key.org == artifact.org)
1002    });
1003    let resolve_result = artifact.resolved();
1004    // A version-one policy is not applied under a recorded requirement, nor
1005    // while the record or the trust store cannot be read: enforcement refuses
1006    // in each case, and the status must not say otherwise.
1007    let authority_error = match required {
1008        Ok(true) => Some(
1009            "an organization policy is required on this endpoint; the installed version-one \
1010             policy does not satisfy it"
1011                .to_string(),
1012        ),
1013        Ok(false) => trust_error,
1014        Err(error) => Some(error),
1015    };
1016    let applied = authority_error.is_none()
1017        && trusted
1018        && verify.signature_valid
1019        && artifact.enforced
1020        && resolve_result.is_ok();
1021
1022    OrgStatus {
1023        recorded,
1024        present: true,
1025        source,
1026        org: Some(artifact.org.clone()),
1027        policy_version: Some(artifact.policy_version.clone()),
1028        enforced: artifact.enforced,
1029        issued_at: Some(artifact.issued_at.clone()),
1030        signer_public_key: signer_key,
1031        signature_valid: verify.signature_valid,
1032        trusted,
1033        applied,
1034        // The floor of the artifact read above, for a preview: the same
1035        // bytes this status describes, not a second load.
1036        floor_preview: resolve_result.as_ref().ok().cloned(),
1037        resolve_error: authority_error.or_else(|| resolve_result.err().map(|e| e.to_string())),
1038        pinned_anchors,
1039    }
1040}