lean-ctx 3.9.20

Local Context SDK for AI agents. Select, shape, reuse, recover, and measure context before inference.
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
//! Pure runtime verification for signed invocation-context admissions.
//!
//! The protocol crate owns the wire contract and canonical decoder. This
//! module owns trusted-key lookup, revocation, public-key binding, Ed25519
//! verification, expected-runtime-scope binding, and injected time gates.
//! Verification has no replay or one-time-consumption state; admission
//! consumption belongs to the caller's separate ledger gate.

use std::collections::BTreeMap;
use std::fmt;

use base64::{Engine as _, engine::general_purpose::STANDARD};
use ed25519_dalek::{Signature, Verifier, VerifyingKey};
use lean_ctx_protocol::{
    EngineInvocationIdV1, InvocationCapabilityBindingV1, InvocationContextBindingV1,
    InvocationSourceBindingV1, PlanId, ProtocolReference, Sha256Digest, TaskId, UtcTimestamp,
};
use sha2::{Digest, Sha256};

/// Verification failure for a signed invocation admission.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum InvocationAdmissionVerificationError {
    /// The input was not a strict canonical protocol binding.
    CanonicalDecode(String),
    /// The binding does not equal the caller's expected runtime scope.
    ///
    /// This intentionally carries no field values or input material.
    ScopeMismatch,
    /// The binding names no trusted key.
    UnknownKey { key_id: String },
    /// The binding names a key which has been revoked.
    RevokedKey { key_id: String },
    /// The trusted key does not have the digest committed by the binding.
    PublicKeyDigestMismatch {
        key_id: String,
        expected: Sha256Digest,
        actual: Sha256Digest,
    },
    /// A trusted key could not be represented as a valid Ed25519 key.
    InvalidPublicKey { key_id: String },
    /// The signature field could not be decoded as exactly 64 bytes.
    InvalidSignatureEncoding,
    /// The signature did not verify for the binding domain and unsigned bytes.
    InvalidSignature,
    /// The binding is not active at the injected current time.
    NotYetValid {
        now: UtcTimestamp,
        not_before: UtcTimestamp,
    },
    /// The binding was issued after the injected current time.
    NotYetIssued {
        now: UtcTimestamp,
        issued_at: UtcTimestamp,
    },
    /// The binding has expired at the injected current time.
    Expired {
        now: UtcTimestamp,
        expires_at: UtcTimestamp,
    },
    /// A trust-store entry is malformed or conflicts with an existing pin.
    InvalidTrustedKey(String),
    /// An operation addressed a key which is not present in the trust store.
    TrustStoreKeyNotFound { key_id: String },
}

impl fmt::Display for InvocationAdmissionVerificationError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::CanonicalDecode(error) => {
                write!(formatter, "canonical admission decode: {error}")
            }
            Self::ScopeMismatch => formatter.write_str("invocation admission scope mismatch"),
            Self::UnknownKey { key_id } => {
                write!(
                    formatter,
                    "invocation admission signer key is untrusted: {key_id}"
                )
            }
            Self::RevokedKey { key_id } => {
                write!(
                    formatter,
                    "invocation admission signer key is revoked: {key_id}"
                )
            }
            Self::PublicKeyDigestMismatch {
                key_id,
                expected,
                actual,
            } => write!(
                formatter,
                "invocation admission public-key digest mismatch for {key_id}: expected {}, got {}",
                expected.as_str(),
                actual.as_str()
            ),
            Self::InvalidPublicKey { key_id } => {
                write!(
                    formatter,
                    "trusted invocation admission key is invalid: {key_id}"
                )
            }
            Self::InvalidSignatureEncoding => {
                formatter.write_str("invocation admission signature is not 64-byte base64")
            }
            Self::InvalidSignature => {
                formatter.write_str("invocation admission signature verification failed")
            }
            Self::NotYetValid { now, not_before } => write!(
                formatter,
                "invocation admission is not valid yet: now {}, not_before {}",
                now.as_str(),
                not_before.as_str()
            ),
            Self::NotYetIssued { now, issued_at } => write!(
                formatter,
                "invocation admission is not issued yet: now {}, issued_at {}",
                now.as_str(),
                issued_at.as_str()
            ),
            Self::Expired { now, expires_at } => write!(
                formatter,
                "invocation admission is expired: now {}, expires_at {}",
                now.as_str(),
                expires_at.as_str()
            ),
            Self::InvalidTrustedKey(error) => write!(formatter, "invalid trusted key: {error}"),
            Self::TrustStoreKeyNotFound { key_id } => {
                write!(
                    formatter,
                    "trusted invocation admission key not found: {key_id}"
                )
            }
        }
    }
}

impl std::error::Error for InvocationAdmissionVerificationError {}

/// Exact runtime scope which the caller expects the signed binding to cover.
///
/// Source and capability vectors retain protocol order. Equality therefore
/// requires the complete ordered collections, not merely a set of entries.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct InvocationAdmissionExpectedScopeV1 {
    session_identity_ref: Sha256Digest,
    task_id: TaskId,
    task_ref: Sha256Digest,
    plan_id: PlanId,
    plan_ref: Sha256Digest,
    invocation_id: EngineInvocationIdV1,
    invocation_ref: Sha256Digest,
    policy_ref: ProtocolReference,
    policy_digest: Sha256Digest,
    source_bindings: Vec<InvocationSourceBindingV1>,
    capability_bindings: Vec<InvocationCapabilityBindingV1>,
}

impl InvocationAdmissionExpectedScopeV1 {
    /// Construct the exact scope expected by the runtime admission caller.
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn new(
        session_identity_ref: Sha256Digest,
        task_id: TaskId,
        task_ref: Sha256Digest,
        plan_id: PlanId,
        plan_ref: Sha256Digest,
        invocation_id: EngineInvocationIdV1,
        invocation_ref: Sha256Digest,
        policy_ref: ProtocolReference,
        policy_digest: Sha256Digest,
        source_bindings: Vec<InvocationSourceBindingV1>,
        capability_bindings: Vec<InvocationCapabilityBindingV1>,
    ) -> Self {
        Self {
            session_identity_ref,
            task_id,
            task_ref,
            plan_id,
            plan_ref,
            invocation_id,
            invocation_ref,
            policy_ref,
            policy_digest,
            source_bindings,
            capability_bindings,
        }
    }
}

/// A trusted Ed25519 verification key identified by its protocol key id.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct InvocationAdmissionTrustedKeyV1 {
    key_id: String,
    public_key: [u8; 32],
    revoked: bool,
}

impl InvocationAdmissionTrustedKeyV1 {
    /// Construct a non-revoked trusted key pin.
    pub(crate) fn new(
        key_id: impl Into<String>,
        public_key: [u8; 32],
    ) -> Result<Self, InvocationAdmissionVerificationError> {
        let key_id = key_id.into();
        validate_key_id(&key_id)?;
        VerifyingKey::from_bytes(&public_key).map_err(|_| {
            InvocationAdmissionVerificationError::InvalidTrustedKey(format!(
                "{key_id}: invalid Ed25519 public key"
            ))
        })?;
        Ok(Self {
            key_id,
            public_key,
            revoked: false,
        })
    }

    pub(crate) fn key_id(&self) -> &str {
        &self.key_id
    }

    pub(crate) fn public_key(&self) -> &[u8; 32] {
        &self.public_key
    }

    pub(crate) fn is_revoked(&self) -> bool {
        self.revoked
    }

    /// Return this key with its revocation bit set.
    pub(crate) fn revoked(mut self) -> Self {
        self.revoked = true;
        self
    }
}

/// In-memory trusted-key and revocation view for admission verification.
///
/// This type has no replay, consumption, or mutable admission state.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct InvocationAdmissionTrustStoreV1 {
    keys: BTreeMap<String, InvocationAdmissionTrustedKeyV1>,
}

impl InvocationAdmissionTrustStoreV1 {
    /// Construct an empty trust store.
    pub(crate) fn new() -> Self {
        Self::default()
    }

    /// Add a trusted key, rejecting a conflicting replacement for its id.
    pub(crate) fn insert(
        &mut self,
        key: InvocationAdmissionTrustedKeyV1,
    ) -> Result<(), InvocationAdmissionVerificationError> {
        if let Some(existing) = self.keys.get(key.key_id()) {
            if existing != &key {
                return Err(InvocationAdmissionVerificationError::InvalidTrustedKey(
                    format!("conflicting key pin for {}", key.key_id()),
                ));
            }
            return Ok(());
        }
        self.keys.insert(key.key_id.clone(), key);
        Ok(())
    }

    /// Add a non-revoked trusted key from raw Ed25519 bytes.
    pub(crate) fn add(
        &mut self,
        key_id: impl Into<String>,
        public_key: [u8; 32],
    ) -> Result<(), InvocationAdmissionVerificationError> {
        self.insert(InvocationAdmissionTrustedKeyV1::new(key_id, public_key)?)
    }

    /// Revoke a previously pinned key.
    pub(crate) fn revoke(
        &mut self,
        key_id: &str,
    ) -> Result<(), InvocationAdmissionVerificationError> {
        let Some(key) = self.keys.get_mut(key_id) else {
            return Err(
                InvocationAdmissionVerificationError::TrustStoreKeyNotFound {
                    key_id: key_id.to_string(),
                },
            );
        };
        key.revoked = true;
        Ok(())
    }

    /// Verify one canonical binding at an injected current time and scope.
    pub(crate) fn verify(
        &self,
        canonical_bytes: &[u8],
        expected_scope: &InvocationAdmissionExpectedScopeV1,
        now: &UtcTimestamp,
    ) -> Result<VerifiedInvocationAdmissionV1, InvocationAdmissionVerificationError> {
        verify_invocation_admission(canonical_bytes, self, expected_scope, now)
    }
}

/// The only value which may cross the runtime admission boundary.
///
/// Fields are private and the type is deliberately not `Clone`; callers can
/// obtain it only after every verifier gate, including expected-scope equality.
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct VerifiedInvocationAdmissionV1 {
    binding: InvocationContextBindingV1,
    canonical_bytes: Vec<u8>,
    binding_digest: Sha256Digest,
}

impl VerifiedInvocationAdmissionV1 {
    /// Return the validated protocol binding.
    pub(crate) fn binding(&self) -> &InvocationContextBindingV1 {
        &self.binding
    }

    /// Return the exact canonical bytes which were verified.
    pub(crate) fn canonical_bytes(&self) -> &[u8] {
        &self.canonical_bytes
    }

    /// Return the SHA-256 identity of the complete signed binding bytes.
    pub(crate) fn binding_digest(&self) -> &Sha256Digest {
        &self.binding_digest
    }

    /// Alias used by evidence adapters when naming the admission artifact.
    pub(crate) fn admission_digest(&self) -> &Sha256Digest {
        self.binding_digest()
    }

    /// Return the signer key id committed by the binding.
    pub(crate) fn signer_key_id(&self) -> &str {
        &self.binding.signer.key_id
    }

    /// Return the signer public-key digest committed by the binding.
    pub(crate) fn signer_public_key_digest(&self) -> &Sha256Digest {
        &self.binding.signer.public_key_digest
    }
}

/// Verify a signed invocation-context binding without consuming it.
///
/// `expected_scope` is mandatory: no authorizing API exists without it.
pub(crate) fn verify_invocation_admission(
    canonical_bytes: &[u8],
    trust_store: &InvocationAdmissionTrustStoreV1,
    expected_scope: &InvocationAdmissionExpectedScopeV1,
    now: &UtcTimestamp,
) -> Result<VerifiedInvocationAdmissionV1, InvocationAdmissionVerificationError> {
    let binding =
        InvocationContextBindingV1::from_canonical_bytes(canonical_bytes).map_err(|error| {
            InvocationAdmissionVerificationError::CanonicalDecode(error.to_string())
        })?;

    if expected_scope != &expected_scope_for_binding(&binding) {
        return Err(InvocationAdmissionVerificationError::ScopeMismatch);
    }

    let trusted_key = trust_store.resolve(&binding.signer.key_id).ok_or_else(|| {
        InvocationAdmissionVerificationError::UnknownKey {
            key_id: binding.signer.key_id.clone(),
        }
    })?;
    if trusted_key.is_revoked() {
        return Err(InvocationAdmissionVerificationError::RevokedKey {
            key_id: trusted_key.key_id().to_string(),
        });
    }

    // Digest the exact 32 bytes pinned in the trust store, before constructing
    // an Ed25519 key object or verifying any signature.
    let actual_digest = digest_public_key(trusted_key.public_key())?;
    if actual_digest != binding.signer.public_key_digest {
        return Err(
            InvocationAdmissionVerificationError::PublicKeyDigestMismatch {
                key_id: trusted_key.key_id().to_string(),
                expected: binding.signer.public_key_digest.clone(),
                actual: actual_digest,
            },
        );
    }

    let verifying_key = VerifyingKey::from_bytes(trusted_key.public_key()).map_err(|_| {
        InvocationAdmissionVerificationError::InvalidPublicKey {
            key_id: trusted_key.key_id().to_string(),
        }
    })?;
    let signature_bytes = STANDARD
        .decode(binding.signature.as_bytes())
        .map_err(|_| InvocationAdmissionVerificationError::InvalidSignatureEncoding)?;
    let signature_array: [u8; 64] = signature_bytes
        .try_into()
        .map_err(|_| InvocationAdmissionVerificationError::InvalidSignatureEncoding)?;
    let signature = Signature::from_bytes(&signature_array);
    let signing_bytes = binding.signing_bytes().map_err(|error| {
        InvocationAdmissionVerificationError::CanonicalDecode(error.to_string())
    })?;
    verifying_key
        .verify(&signing_bytes, &signature)
        .map_err(|_| InvocationAdmissionVerificationError::InvalidSignature)?;

    if now < &binding.not_before {
        return Err(InvocationAdmissionVerificationError::NotYetValid {
            now: now.clone(),
            not_before: binding.not_before.clone(),
        });
    }
    if now < &binding.issued_at {
        return Err(InvocationAdmissionVerificationError::NotYetIssued {
            now: now.clone(),
            issued_at: binding.issued_at.clone(),
        });
    }
    if now >= &binding.expires_at {
        return Err(InvocationAdmissionVerificationError::Expired {
            now: now.clone(),
            expires_at: binding.expires_at.clone(),
        });
    }

    let binding_digest = digest_bytes(canonical_bytes)?;
    Ok(VerifiedInvocationAdmissionV1 {
        binding,
        canonical_bytes: canonical_bytes.to_vec(),
        binding_digest,
    })
}

fn expected_scope_for_binding(
    binding: &InvocationContextBindingV1,
) -> InvocationAdmissionExpectedScopeV1 {
    InvocationAdmissionExpectedScopeV1::new(
        binding.session_identity_ref.clone(),
        binding.task_id.clone(),
        binding.task_ref.clone(),
        binding.plan_id.clone(),
        binding.plan_ref.clone(),
        binding.invocation_id.clone(),
        binding.invocation_ref.clone(),
        binding.policy_ref.clone(),
        binding.policy_digest.clone(),
        binding.source_bindings.clone(),
        binding.capability_bindings.clone(),
    )
}

fn digest_bytes(bytes: &[u8]) -> Result<Sha256Digest, InvocationAdmissionVerificationError> {
    let digest = Sha256::digest(bytes);
    let mut hex = String::with_capacity(64);
    for byte in digest {
        use fmt::Write as _;
        write!(hex, "{byte:02x}").map_err(|_| {
            InvocationAdmissionVerificationError::InvalidTrustedKey(
                "failed to encode SHA-256 digest".to_string(),
            )
        })?;
    }
    Sha256Digest::new(format!("sha256:{hex}"))
        .map_err(|error| InvocationAdmissionVerificationError::InvalidTrustedKey(error.to_string()))
}

fn digest_public_key(
    public_key: &[u8; 32],
) -> Result<Sha256Digest, InvocationAdmissionVerificationError> {
    digest_bytes(public_key)
}

fn validate_key_id(key_id: &str) -> Result<(), InvocationAdmissionVerificationError> {
    if key_id.is_empty()
        || key_id.len() > 128
        || !key_id.is_ascii()
        || !key_id.as_bytes()[0].is_ascii_alphanumeric()
        || !key_id
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || b"._:/-".contains(&byte))
        || key_id.starts_with("base64:")
        || key_id.starts_with("hex:")
    {
        return Err(InvocationAdmissionVerificationError::InvalidTrustedKey(
            "key id must be a bounded non-material identifier".to_string(),
        ));
    }
    Ok(())
}

impl InvocationAdmissionTrustStoreV1 {
    pub(crate) fn resolve(&self, key_id: &str) -> Option<&InvocationAdmissionTrustedKeyV1> {
        self.keys.get(key_id)
    }
}

#[cfg(test)]
mod tests {
    use base64::engine::general_purpose::STANDARD;
    use ed25519_dalek::{Signer, SigningKey};
    use lean_ctx_protocol::{
        CapabilityId, EngineInvocationIdV1, InvocationContextBindingV1, ProtocolReference,
        SemanticVersion, UtcTimestamp,
    };
    use serde_json::{Value, json};

    use super::*;

    const NOW: &str = "2026-08-24T12:00:00Z";
    const NOT_BEFORE: &str = "2026-08-24T11:59:00Z";
    const EXPIRES_AT: &str = "2026-08-24T12:05:00Z";

    fn key() -> SigningKey {
        SigningKey::from_bytes(&[7u8; 32])
    }

    fn public_key_digest(signing_key: &SigningKey) -> String {
        let digest = Sha256::digest(VerifyingKey::from(signing_key).as_bytes());
        let mut hex = String::with_capacity(64);
        for byte in digest {
            use fmt::Write as _;
            write!(hex, "{byte:02x}").expect("hex write");
        }
        format!("sha256:{hex}")
    }

    fn digest(value: char) -> String {
        format!("sha256:{}", value.to_string().repeat(64))
    }

    fn unsigned_value(signing_key: &SigningKey) -> Value {
        json!({
            "schema_version": 1,
            "admission_id": "admission-1",
            "session_identity_ref": digest('a'),
            "task_id": "task-1",
            "task_ref": digest('b'),
            "plan_id": "plan-1",
            "plan_ref": digest('c'),
            "invocation_id": "invocation-1",
            "invocation_ref": digest('d'),
            "policy_ref": "policy:invocation-admission",
            "policy_digest": digest('e'),
            "decision": "admitted",
            "source_bindings": [{
                "source_ref": "source:input",
                "digest": digest('f'),
                "role": "input"
            }],
            "capability_bindings": [{
                "capability_id": "capability:engine",
                "capability_version": "1.0.0",
                "manifest_digest": digest('1')
            }],
            "issued_at": NOW,
            "not_before": NOT_BEFORE,
            "expires_at": EXPIRES_AT,
            "signer": {
                "algorithm": "ed25519",
                "key_id": "test-key",
                "public_key_digest": public_key_digest(signing_key)
            },
            "signature": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=="
        })
    }

    fn signed_binding(signing_key: &SigningKey) -> Vec<u8> {
        let value = unsigned_value(signing_key);
        let mut binding: InvocationContextBindingV1 =
            serde_json::from_value(value).expect("binding fixture");
        let signature = signing_key.sign(&binding.signing_bytes().expect("signing bytes"));
        binding.signature = STANDARD.encode(signature.to_bytes());
        binding.canonical_bytes().expect("canonical binding")
    }

    fn binding(signing_key: &SigningKey) -> InvocationContextBindingV1 {
        InvocationContextBindingV1::from_canonical_bytes(&signed_binding(signing_key))
            .expect("canonical binding")
    }

    fn scope(signing_key: &SigningKey) -> InvocationAdmissionExpectedScopeV1 {
        expected_scope_for_binding(&binding(signing_key))
    }

    fn store(signing_key: &SigningKey) -> InvocationAdmissionTrustStoreV1 {
        let mut store = InvocationAdmissionTrustStoreV1::new();
        store
            .add("test-key", VerifyingKey::from(signing_key).to_bytes())
            .expect("trusted key");
        store
    }

    fn now(value: &str) -> UtcTimestamp {
        UtcTimestamp::new(value).expect("timestamp")
    }

    #[test]
    fn verifies_canonical_signature_scope_and_identity() {
        let signing_key = key();
        let bytes = signed_binding(&signing_key);
        let verified = store(&signing_key)
            .verify(&bytes, &scope(&signing_key), &now(NOW))
            .expect("verified admission");

        assert_eq!(verified.canonical_bytes(), bytes.as_slice());
        assert_eq!(verified.binding().admission_id.as_str(), "admission-1");
        assert_eq!(verified.binding().invocation_id.as_str(), "invocation-1");
        assert_eq!(verified.signer_key_id(), "test-key");
        assert_eq!(
            verified.signer_public_key_digest().as_str(),
            public_key_digest(&signing_key)
        );
        assert_eq!(verified.binding_digest(), verified.admission_digest());
        assert_eq!(
            verified.binding_digest(),
            &digest_bytes(&bytes).expect("digest")
        );
    }

    #[test]
    fn unknown_key_is_rejected() {
        let signing_key = key();
        let error = InvocationAdmissionTrustStoreV1::new()
            .verify(
                &signed_binding(&signing_key),
                &scope(&signing_key),
                &now(NOW),
            )
            .expect_err("unknown key");
        assert!(matches!(
            error,
            InvocationAdmissionVerificationError::UnknownKey { .. }
        ));
    }

    #[test]
    fn revoked_key_is_rejected() {
        let signing_key = key();
        let trusted = InvocationAdmissionTrustedKeyV1::new(
            "test-key",
            VerifyingKey::from(&signing_key).to_bytes(),
        )
        .expect("trusted key")
        .revoked();
        let mut store = InvocationAdmissionTrustStoreV1::new();
        store.insert(trusted).expect("insert");
        let error = store
            .verify(
                &signed_binding(&signing_key),
                &scope(&signing_key),
                &now(NOW),
            )
            .expect_err("revoked key");
        assert!(matches!(
            error,
            InvocationAdmissionVerificationError::RevokedKey { .. }
        ));
    }

    #[test]
    fn public_key_digest_mismatch_is_rejected() {
        let signing_key = key();
        let other = SigningKey::from_bytes(&[8u8; 32]);
        let mut value = unsigned_value(&signing_key);
        value["signer"]["public_key_digest"] = json!(public_key_digest(&other));
        let mut binding: InvocationContextBindingV1 =
            serde_json::from_value(value).expect("binding fixture");
        binding.signature = STANDARD.encode(
            signing_key
                .sign(&binding.signing_bytes().expect("signing bytes"))
                .to_bytes(),
        );
        let bytes = binding.canonical_bytes().expect("canonical");
        let error = store(&signing_key)
            .verify(&bytes, &scope(&signing_key), &now(NOW))
            .expect_err("digest mismatch");
        assert!(matches!(
            error,
            InvocationAdmissionVerificationError::PublicKeyDigestMismatch { .. }
        ));
    }

    #[test]
    fn field_isolated_scope_mismatches_are_rejected_before_authorization() {
        let signing_key = key();
        let bytes = signed_binding(&signing_key);
        let expected = scope(&signing_key);
        let mut cases = Vec::new();

        let mut mismatch = expected.clone();
        mismatch.session_identity_ref = Sha256Digest::new(digest('9')).unwrap();
        cases.push(mismatch);
        let mut mismatch = expected.clone();
        mismatch.task_id = TaskId::new("task-other").unwrap();
        cases.push(mismatch);
        let mut mismatch = expected.clone();
        mismatch.task_ref = Sha256Digest::new(digest('8')).unwrap();
        cases.push(mismatch);
        let mut mismatch = expected.clone();
        mismatch.plan_id = PlanId::new("plan-other").unwrap();
        cases.push(mismatch);
        let mut mismatch = expected.clone();
        mismatch.plan_ref = Sha256Digest::new(digest('7')).unwrap();
        cases.push(mismatch);
        let mut mismatch = expected.clone();
        mismatch.invocation_id = EngineInvocationIdV1::new("invocation-other").unwrap();
        cases.push(mismatch);
        let mut mismatch = expected.clone();
        mismatch.invocation_ref = Sha256Digest::new(digest('6')).unwrap();
        cases.push(mismatch);
        let mut mismatch = expected.clone();
        mismatch.policy_ref = ProtocolReference::new("policy:other").unwrap();
        cases.push(mismatch);
        let mut mismatch = expected.clone();
        mismatch.policy_digest = Sha256Digest::new(digest('5')).unwrap();
        cases.push(mismatch);

        for mismatch in cases {
            assert!(matches!(
                store(&signing_key).verify(&bytes, &mismatch, &now(NOW)),
                Err(InvocationAdmissionVerificationError::ScopeMismatch)
            ));
        }
    }

    #[test]
    fn whole_source_and_capability_collections_are_scope_bound() {
        let signing_key = key();
        let bytes = signed_binding(&signing_key);
        let expected = scope(&signing_key);

        let mut source_cross_scope = expected.clone();
        let mut source = source_cross_scope.source_bindings[0].clone();
        source.source_ref = ProtocolReference::new("source:other").unwrap();
        source.digest = Sha256Digest::new(digest('4')).unwrap();
        source_cross_scope.source_bindings = vec![source];
        assert!(matches!(
            store(&signing_key).verify(&bytes, &source_cross_scope, &now(NOW)),
            Err(InvocationAdmissionVerificationError::ScopeMismatch)
        ));

        let mut capability_cross_scope = expected.clone();
        let mut capability = capability_cross_scope.capability_bindings[0].clone();
        capability.capability_id = CapabilityId::new("capability:other").unwrap();
        capability.capability_version = SemanticVersion::new("2.0.0").unwrap();
        capability.manifest_digest = Sha256Digest::new(digest('3')).unwrap();
        capability_cross_scope.capability_bindings = vec![capability];
        assert!(matches!(
            store(&signing_key).verify(&bytes, &capability_cross_scope, &now(NOW)),
            Err(InvocationAdmissionVerificationError::ScopeMismatch)
        ));
    }

    #[test]
    fn signature_and_domain_mutations_are_rejected() {
        let signing_key = key();
        let mut value = unsigned_value(&signing_key);
        let mut binding: InvocationContextBindingV1 =
            serde_json::from_value(value.clone()).expect("binding fixture");
        let mut wrong_domain = b"wrong-domain\0".to_vec();
        wrong_domain.extend_from_slice(&binding.unsigned_canonical_bytes().expect("unsigned"));
        binding.signature = STANDARD.encode(signing_key.sign(&wrong_domain).to_bytes());
        let wrong_domain_bytes = binding.canonical_bytes().expect("canonical");
        assert!(matches!(
            store(&signing_key).verify(&wrong_domain_bytes, &scope(&signing_key), &now(NOW)),
            Err(InvocationAdmissionVerificationError::InvalidSignature)
        ));

        value["task_id"] = json!("task-mutated");
        let mut mutated: InvocationContextBindingV1 =
            serde_json::from_value(value).expect("binding fixture");
        let original_signature = STANDARD
            .decode(
                InvocationContextBindingV1::from_canonical_bytes(&signed_binding(&signing_key))
                    .expect("original")
                    .signature,
            )
            .expect("signature");
        mutated.signature = STANDARD.encode(original_signature);
        let mutated_bytes = mutated.canonical_bytes().expect("canonical");
        assert!(matches!(
            store(&signing_key).verify(&mutated_bytes, &scope(&signing_key), &now(NOW)),
            Err(InvocationAdmissionVerificationError::ScopeMismatch)
        ));
    }

    #[test]
    fn validity_window_rejects_not_before_issued_and_expiry_boundaries() {
        let signing_key = key();
        let bytes = signed_binding(&signing_key);
        let scope = scope(&signing_key);
        assert!(matches!(
            store(&signing_key).verify(&bytes, &scope, &now("2026-08-24T11:58:59Z")),
            Err(InvocationAdmissionVerificationError::NotYetValid { .. })
        ));
        assert!(matches!(
            store(&signing_key).verify(&bytes, &scope, &now(NOT_BEFORE)),
            Err(InvocationAdmissionVerificationError::NotYetIssued { .. })
        ));
        assert!(
            store(&signing_key)
                .verify(&bytes, &scope, &now(NOW))
                .is_ok()
        );
        assert!(matches!(
            store(&signing_key).verify(&bytes, &scope, &now(EXPIRES_AT)),
            Err(InvocationAdmissionVerificationError::Expired { .. })
        ));
    }

    #[test]
    fn canonical_tamper_is_rejected_before_runtime_gates() {
        let signing_key = key();
        let mut bytes = signed_binding(&signing_key);
        let closing = bytes.pop().expect("json closing brace");
        bytes.extend_from_slice(b" ");
        bytes.push(closing);
        assert!(matches!(
            store(&signing_key).verify(&bytes, &scope(&signing_key), &now(NOW)),
            Err(InvocationAdmissionVerificationError::CanonicalDecode(_))
        ));
    }

    #[test]
    fn trust_store_rejects_conflicting_key_replacement() {
        let mut store = InvocationAdmissionTrustStoreV1::new();
        store
            .add("test-key", VerifyingKey::from(&key()).to_bytes())
            .expect("first pin");
        let error = store
            .add(
                "test-key",
                VerifyingKey::from(&SigningKey::from_bytes(&[8u8; 32])).to_bytes(),
            )
            .expect_err("conflicting pin");
        assert!(matches!(
            error,
            InvocationAdmissionVerificationError::InvalidTrustedKey(_)
        ));
    }

    #[test]
    fn invalid_key_ids_are_rejected() {
        assert!(InvocationAdmissionTrustedKeyV1::new("base64:secret", [1u8; 32]).is_err());
        assert!(InvocationAdmissionTrustedKeyV1::new("", [1u8; 32]).is_err());
    }
}