vellaveto-types 6.1.1

Core type definitions for Vellaveto policy engine
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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright 2026 Paolo Vella
// SPDX-License-Identifier: MPL-2.0

//! Agent identity types — attested identity, call chain entries,
//! evaluation context, and context builder.

use crate::capability::CapabilityToken;
use crate::verification::VerificationTier;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;

/// Cryptographically attested agent identity from a signed JWT.
///
/// This type represents a validated identity extracted from the `X-Agent-Identity`
/// header. Unlike the simple `agent_id` string, this provides cryptographic
/// attestation of the agent's identity via JWT signature verification.
///
/// # Security (OWASP ASI07 - Agent Identity Attestation)
///
/// - All claims are extracted from a signature-verified JWT
/// - The proxy validates the JWT before populating this struct
/// - Policies can match on issuer, subject, and custom claims
/// - This provides stronger identity guarantees than the legacy `agent_id` field
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct AgentIdentity {
    /// JWT issuer (`iss` claim). Identifies the identity provider.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub issuer: Option<String>,
    /// JWT subject (`sub` claim). Identifies the specific agent.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub subject: Option<String>,
    /// JWT audience (`aud` claim). May be a single string or array.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub audience: Vec<String>,
    /// Additional custom claims from the JWT payload.
    /// Common claims: `role`, `team`, `environment`, `permissions`.
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub claims: HashMap<String, serde_json::Value>,
}

impl AgentIdentity {
    /// Returns true if this identity has any populated fields.
    pub fn is_populated(&self) -> bool {
        self.issuer.is_some()
            || self.subject.is_some()
            || !self.audience.is_empty()
            || !self.claims.is_empty()
    }

    /// Get a claim value as a string, if present and is a string.
    pub fn claim_str(&self, key: &str) -> Option<&str> {
        self.claims.get(key).and_then(|v| v.as_str())
    }

    /// Get a claim value as an array of strings, if present and is an array.
    pub fn claim_str_array(&self, key: &str) -> Option<Vec<&str>> {
        self.claims.get(key).and_then(|v| {
            v.as_array()
                .map(|arr| arr.iter().filter_map(|item| item.as_str()).collect())
        })
    }

    /// Maximum number of claims to prevent memory abuse.
    pub const MAX_CLAIMS: usize = 64;

    /// Maximum number of audience entries to prevent memory abuse.
    pub const MAX_AUDIENCE: usize = 64;

    /// Maximum byte length of a single claim key.
    ///
    /// SECURITY (FIND-R111-002): Prevents memory exhaustion from attacker-controlled
    /// claim keys supplied in JWTs.
    pub const MAX_CLAIM_KEY_LEN: usize = 256;

    /// Maximum byte length of a serialized claim value.
    ///
    /// SECURITY (FIND-R111-002): Prevents memory exhaustion from attacker-controlled
    /// claim values supplied in JWTs. Measured on the JSON-serialized value to cover
    /// both string and structured values.
    pub const MAX_CLAIM_VALUE_LEN: usize = 4096;

    /// SECURITY (FIND-R49-006, FIND-R111-002): Validate AgentIdentity bounds.
    /// Maximum byte length per audience entry.
    /// SECURITY (FIND-R141-002): Prevents memory amplification via oversized audience entries.
    const MAX_AUDIENCE_ENTRY_LEN: usize = 1024;
    /// Maximum byte length for issuer field.
    /// SECURITY (FIND-R141-002): Prevents memory amplification via oversized issuer.
    const MAX_ISSUER_LEN: usize = 1024;
    /// Maximum byte length for subject field.
    /// SECURITY (FIND-R141-002): Prevents memory amplification via oversized subject.
    const MAX_SUBJECT_LEN: usize = 1024;

    pub fn validate(&self) -> Result<(), String> {
        if self.claims.len() > Self::MAX_CLAIMS {
            return Err(format!(
                "AgentIdentity claims count {} exceeds max {}",
                self.claims.len(),
                Self::MAX_CLAIMS
            ));
        }
        if self.audience.len() > Self::MAX_AUDIENCE {
            return Err(format!(
                "AgentIdentity audience count {} exceeds max {}",
                self.audience.len(),
                Self::MAX_AUDIENCE
            ));
        }
        // SECURITY (FIND-R141-002): Per-entry length and control/format char validation
        // for audience entries. Prevents memory amplification and identity bypass via
        // zero-width characters in audience matching.
        for (i, aud) in self.audience.iter().enumerate() {
            if aud.len() > Self::MAX_AUDIENCE_ENTRY_LEN {
                return Err(format!(
                    "AgentIdentity audience[{}] length {} exceeds max {}",
                    i,
                    aud.len(),
                    Self::MAX_AUDIENCE_ENTRY_LEN
                ));
            }
            if aud
                .chars()
                .any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
            {
                return Err(format!(
                    "AgentIdentity audience[{i}] contains control or format characters"
                ));
            }
        }
        // SECURITY (FIND-R141-002): Validate issuer and subject fields for length
        // and control/format characters. These are used in policy matching and must
        // not contain invisible characters that could bypass identity comparisons.
        if let Some(ref iss) = self.issuer {
            if iss.len() > Self::MAX_ISSUER_LEN {
                return Err(format!(
                    "AgentIdentity issuer length {} exceeds max {}",
                    iss.len(),
                    Self::MAX_ISSUER_LEN
                ));
            }
            if iss
                .chars()
                .any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
            {
                return Err(
                    "AgentIdentity issuer contains control or format characters".to_string()
                );
            }
        }
        if let Some(ref sub) = self.subject {
            if sub.len() > Self::MAX_SUBJECT_LEN {
                return Err(format!(
                    "AgentIdentity subject length {} exceeds max {}",
                    sub.len(),
                    Self::MAX_SUBJECT_LEN
                ));
            }
            if sub
                .chars()
                .any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
            {
                return Err(
                    "AgentIdentity subject contains control or format characters".to_string(),
                );
            }
        }
        // SECURITY (FIND-R111-002): Per-key and per-value length bounds.
        for (key, value) in &self.claims {
            if key.len() > Self::MAX_CLAIM_KEY_LEN {
                return Err(format!(
                    "AgentIdentity claim key length {} exceeds max {} (key starts with: '{}')",
                    key.len(),
                    Self::MAX_CLAIM_KEY_LEN,
                    key.chars().take(32).collect::<String>()
                ));
            }
            // Reject control characters and Unicode format characters in claim keys.
            if crate::core::has_dangerous_chars(key) {
                return Err(format!(
                    "AgentIdentity claim key contains control or Unicode format character: '{}'",
                    key.chars().take(32).collect::<String>()
                ));
            }
            // Measure the serialized value length to cover all JSON value types.
            let serialized_value = serde_json::to_string(value).map_err(|e| {
                format!("AgentIdentity claim value for key '{key}' failed to serialize: {e}")
            })?;
            if serialized_value.len() > Self::MAX_CLAIM_VALUE_LEN {
                return Err(format!(
                    "AgentIdentity claim value for key '{}' length {} exceeds max {}",
                    key,
                    serialized_value.len(),
                    Self::MAX_CLAIM_VALUE_LEN
                ));
            }
        }
        Ok(())
    }
}

/// An entry in a multi-agent call chain, tracking the path of a request
/// through multiple agents in a multi-hop MCP scenario.
///
/// OWASP ASI08: Multi-agent communication monitoring requires tracking
/// the full chain of tool calls to detect privilege escalation patterns.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct CallChainEntry {
    /// The agent that made this call (from X-Upstream-Agent header or OAuth subject).
    pub agent_id: String,
    /// The tool being called.
    pub tool: String,
    /// The function being called.
    pub function: String,
    /// ISO 8601 timestamp when the call was made.
    pub timestamp: String,
    /// HMAC-SHA256 signature over the entry content (FIND-015).
    /// Present when the entry was signed by a Vellaveto instance with a configured HMAC key.
    /// Hex-encoded. Omitted from serialization when `None` for backward compatibility.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub hmac: Option<String>,
    /// Whether the HMAC on this entry has been verified (FIND-015).
    /// `None` = not checked (no key configured or entry has no HMAC).
    /// `Some(true)` = HMAC verified successfully.
    /// `Some(false)` = HMAC verification failed (entry marked as unverified).
    /// Excluded from serialization — this is local verification state only.
    #[serde(skip)]
    pub verified: Option<bool>,
}

/// Session-level context for policy evaluation.
///
/// Separate from [`Action`] because Action = "what to do" (from the agent),
/// while Context = "session state" (from the proxy). This security boundary
/// ensures agents don't control context fields like call counts or timestamps.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(deny_unknown_fields)]
pub struct EvaluationContext {
    /// ISO 8601 timestamp for the evaluation. When `None`, the engine uses
    /// the current wall-clock time. Providing an explicit timestamp enables
    /// deterministic testing of time-window policies.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub timestamp: Option<String>,
    /// Identity of the agent making the request (e.g., OAuth subject, API key hash).
    /// This is the legacy identity field — prefer `agent_identity` for stronger guarantees.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub agent_id: Option<String>,
    /// Cryptographically attested agent identity from a signed JWT (OWASP ASI07).
    ///
    /// When present, this provides stronger identity guarantees than `agent_id`.
    /// Populated from the `X-Agent-Identity` header after JWT signature verification.
    /// Policies can use `agent_identity` context conditions to match on claims.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub agent_identity: Option<AgentIdentity>,
    /// Per-tool call counts for the current session (tool_name → count).
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub call_counts: HashMap<String, u64>,
    /// History of tool names called in this session (most recent last).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub previous_actions: Vec<String>,
    /// OWASP ASI08: Call chain for multi-agent communication monitoring.
    /// Records the path of the current request through multiple agents.
    /// The first entry is the originating agent, subsequent entries are
    /// intermediary agents in multi-hop scenarios.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub call_chain: Vec<CallChainEntry>,
    /// Tenant identifier for multi-tenancy support.
    /// When set, policies are scoped to this tenant. Extracted from:
    /// 1. JWT claims (`tenant_id` or `org_id`)
    /// 2. Request header (`X-Tenant-ID`)
    /// 3. Subdomain (`{tenant}.vellaveto.example.com`)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tenant_id: Option<String>,
    /// Verification tier of the agent making the request.
    /// Used by `min_verification_tier` context condition.
    /// When `None`, policies requiring a minimum tier will deny (fail-closed).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub verification_tier: Option<VerificationTier>,
    /// Capability delegation token for the agent.
    /// Used by `require_capability_token` context condition.
    /// When `None`, policies requiring a capability token will deny (fail-closed).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub capability_token: Option<CapabilityToken>,
    /// Current session state from the SessionGuard state machine (Phase 23.5).
    /// Used by `session_state_required` context condition.
    /// When `None`, policies requiring a session state will deny (fail-closed).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub session_state: Option<String>,
}

impl EvaluationContext {
    /// Returns true if any context field is populated with meaningful data.
    /// Used by the engine to decide whether falling back to the legacy path
    /// (which cannot evaluate context conditions) is safe.
    // SECURITY (R16-TYPES-2): Include timestamp so time-window policies
    // fail-closed when compiled policies are unavailable, rather than
    // silently falling back to the legacy path that ignores time constraints.
    pub fn has_any_meaningful_fields(&self) -> bool {
        self.timestamp.is_some()
            || self.agent_id.is_some()
            || self
                .agent_identity
                .as_ref()
                .is_some_and(|id| id.is_populated())
            || !self.call_counts.is_empty()
            || !self.previous_actions.is_empty()
            || !self.call_chain.is_empty()
            || self.tenant_id.is_some()
            || self.verification_tier.is_some()
            || self.capability_token.is_some()
            || self.session_state.is_some()
    }

    /// Returns the depth of the current call chain (number of agents in the chain).
    /// A depth of 0 means no multi-hop scenario (direct call).
    /// A depth of 1 means there is one upstream agent.
    pub fn call_chain_depth(&self) -> usize {
        self.call_chain.len()
    }

    /// Returns the originating agent ID if this is a multi-hop request.
    /// This is the first agent in the call chain (the one that initiated the request).
    pub fn originating_agent(&self) -> Option<&str> {
        self.call_chain.first().map(|e| e.agent_id.as_str())
    }

    /// Returns the principal type from the agent identity claims.
    ///
    /// Extracts `claims["type"]` from the agent identity, defaulting to `"Agent"`
    /// when no identity is present or the claim is missing. This centralizes the
    /// extraction pattern used across all ABAC evaluation sites (HTTP, WebSocket,
    /// gRPC, stdio handlers).
    ///
    /// DEDUP (IMP-R198-012): Extracted from 15 identical inline blocks across
    /// handlers.rs, websocket/mod.rs, grpc/service.rs, and relay.rs.
    pub fn principal_type(&self) -> &str {
        self.agent_identity
            .as_ref()
            .and_then(|id| id.claims.get("type"))
            .and_then(|v| v.as_str())
            .unwrap_or("Agent")
    }

    /// Create a new builder for constructing an `EvaluationContext`.
    ///
    /// # Example
    ///
    /// ```
    /// use vellaveto_types::EvaluationContext;
    ///
    /// let ctx = EvaluationContext::builder()
    ///     .agent_id("agent-123")
    ///     .tenant_id("tenant-abc")
    ///     .build();
    /// ```
    pub fn builder() -> EvaluationContextBuilder {
        EvaluationContextBuilder::default()
    }

    /// Validate that string identity fields (session_state, agent_id, tenant_id)
    /// do not contain control characters and are not empty-but-present.
    ///
    /// SECURITY (FIND-R46-017): Prevents log injection and confusion attacks
    /// via control characters in identity fields. A present-but-empty identity
    /// field may bypass agent-matching logic that treats `None` as "no identity"
    /// but matches `Some("")` against wildcard patterns.
    /// Maximum number of entries in `call_counts` before validation fails.
    const MAX_CALL_COUNTS: usize = 10_000;
    /// Maximum number of entries in `previous_actions` before validation fails.
    ///
    /// SECURITY (FIND-CREATIVE-004): Made `pub` so that `vellaveto-engine` can emit
    /// a warning when `ForbiddenActionSequence` checks run on a history at capacity,
    /// since truncated history means earlier forbidden actions may have aged out.
    pub const MAX_PREVIOUS_ACTIONS: usize = 10_000;
    /// Maximum number of entries in `call_chain` before validation fails.
    const MAX_CALL_CHAIN: usize = 100;
    /// SECURITY (FIND-R50-064): Maximum byte length per `previous_actions` entry.
    const MAX_ACTION_NAME_LEN: usize = 256;
    /// Maximum byte length for call_chain timestamp fields.
    /// SECURITY (FIND-R56-CORE-007): ISO 8601 timestamps with timezone are typically
    /// 25-35 bytes; 64 provides ample headroom while bounding memory.
    const MAX_TIMESTAMP_LEN: usize = 64;
    /// Maximum byte length for call_chain entry fields (agent_id, tool, function).
    /// SECURITY (FIND-R56-CORE-008): Prevents memory abuse via oversized call chain fields.
    const MAX_CALL_CHAIN_FIELD_LEN: usize = 512;
    /// Maximum byte length for call_chain entry HMAC field.
    /// HMAC-SHA256 is exactly 64 hex chars; 128 provides headroom for future algorithms.
    const MAX_HMAC_LEN: usize = 128;

    pub fn validate(&self) -> Result<(), String> {
        Self::validate_optional_id_field(&self.agent_id, "agent_id")?;
        Self::validate_optional_id_field(&self.tenant_id, "tenant_id")?;
        Self::validate_optional_id_field(&self.session_state, "session_state")?;

        // SECURITY (FIND-R141-001): Validate top-level timestamp for length and
        // control/format characters. The call_chain timestamps are validated below,
        // but the top-level evaluation timestamp was missing validation, allowing
        // log injection via newlines or memory amplification via oversized values.
        if let Some(ref ts) = self.timestamp {
            if ts.len() > Self::MAX_TIMESTAMP_LEN {
                return Err(format!(
                    "EvaluationContext timestamp length {} exceeds max {}",
                    ts.len(),
                    Self::MAX_TIMESTAMP_LEN,
                ));
            }
            if ts
                .chars()
                .any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
            {
                return Err(
                    "EvaluationContext timestamp contains control or format characters".to_string(),
                );
            }
        }

        // SECURITY (FIND-R49-001): Bound collection sizes to prevent memory exhaustion
        // from oversized deserialized payloads.
        if self.call_counts.len() > Self::MAX_CALL_COUNTS {
            return Err(format!(
                "EvaluationContext call_counts has {} entries, max {}",
                self.call_counts.len(),
                Self::MAX_CALL_COUNTS,
            ));
        }
        if self.previous_actions.len() > Self::MAX_PREVIOUS_ACTIONS {
            return Err(format!(
                "EvaluationContext previous_actions has {} entries, max {}",
                self.previous_actions.len(),
                Self::MAX_PREVIOUS_ACTIONS,
            ));
        }
        // SECURITY (FIND-R50-064): Bound individual previous_actions entry length
        // to prevent memory amplification via to_ascii_lowercase() allocations
        // in sequence/workflow evaluation.
        for (i, action) in self.previous_actions.iter().enumerate() {
            if action.len() > Self::MAX_ACTION_NAME_LEN {
                return Err(format!(
                    "EvaluationContext previous_actions[{}] length {} exceeds max {}",
                    i,
                    action.len(),
                    Self::MAX_ACTION_NAME_LEN,
                ));
            }
            // SECURITY (FIND-R52-008, FIND-R56-CORE-002): Reject control characters
            // and Unicode format characters in previous_actions entries.
            // Compiled sequences reject control characters at compile time, so a history
            // entry with embedded control characters (e.g., "read_secret\x00") would
            // never match a compiled sequence entry via eq_ignore_ascii_case, allowing
            // ForbiddenActionSequence bypass. Format characters (zero-width, bidi) are
            // rejected for parity with call_counts key validation.
            if action
                .chars()
                .any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
            {
                return Err(format!(
                    "EvaluationContext previous_actions[{i}] contains control or format characters",
                ));
            }
        }
        // SECURITY (FIND-R52-006): Bound individual call_counts key length
        // to prevent memory amplification via oversized HashMap keys.
        // SECURITY (FIND-R55-CORE-013): Reject control and Unicode format characters
        // in call_counts keys to prevent pattern matching bypass (matching the
        // previous_actions validation pattern from FIND-R52-008).
        for key in self.call_counts.keys() {
            if key.len() > Self::MAX_ACTION_NAME_LEN {
                return Err(format!(
                    "EvaluationContext call_counts key length {} exceeds max {}",
                    key.len(),
                    Self::MAX_ACTION_NAME_LEN,
                ));
            }
            if key
                .chars()
                .any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
            {
                return Err(
                    "EvaluationContext call_counts key contains control or format characters"
                        .to_string(),
                );
            }
        }
        if self.call_chain.len() > Self::MAX_CALL_CHAIN {
            return Err(format!(
                "EvaluationContext call_chain has {} entries, max {}",
                self.call_chain.len(),
                Self::MAX_CALL_CHAIN,
            ));
        }

        // SECURITY (FIND-R51-005): Validate call_chain entry contents to prevent
        // log injection and confusion attacks via control characters or oversized fields.
        for (i, entry) in self.call_chain.iter().enumerate() {
            Self::validate_call_chain_field(&entry.agent_id, "agent_id", i)?;
            Self::validate_call_chain_field(&entry.tool, "tool", i)?;
            Self::validate_call_chain_field(&entry.function, "function", i)?;
            if entry.timestamp.len() > Self::MAX_TIMESTAMP_LEN {
                return Err(format!(
                    "EvaluationContext call_chain[{}].timestamp length {} exceeds max {}",
                    i,
                    entry.timestamp.len(),
                    Self::MAX_TIMESTAMP_LEN,
                ));
            }
            // SECURITY (FIND-R85-001): Check both control and Unicode format characters,
            // matching validate_call_chain_field() parity for agent_id/tool/function.
            if entry
                .timestamp
                .chars()
                .any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
            {
                return Err(format!(
                    "EvaluationContext call_chain[{i}].timestamp contains control or format characters",
                ));
            }
            // SECURITY: Validate HMAC field length if present.
            // HMAC-SHA256 produces 64 hex chars; cap at 128 for future headroom.
            if let Some(ref hmac) = entry.hmac {
                if hmac.len() > Self::MAX_HMAC_LEN {
                    return Err(format!(
                        "EvaluationContext call_chain[{}].hmac length {} exceeds max {}",
                        i,
                        hmac.len(),
                        Self::MAX_HMAC_LEN,
                    ));
                }
            }
        }

        // SECURITY (FIND-R55-CORE-002): Validate nested AgentIdentity bounds
        // (claims count cap) to prevent memory exhaustion via oversized identity payloads.
        if let Some(ref identity) = self.agent_identity {
            identity
                .validate()
                .map_err(|e| format!("EvaluationContext agent_identity: {e}"))?;
        }

        // SECURITY (FIND-R141-005): Validate nested CapabilityToken structural bounds.
        // Without this check, a deserialized EvaluationContext with a CapabilityToken
        // containing thousands of grants or oversized fields would pass validate()
        // unchecked, causing O(n*m) computation in the capability checking logic.
        if let Some(ref token) = self.capability_token {
            token
                .validate_structure()
                .map_err(|e| format!("EvaluationContext capability_token: {e}"))?;
        }

        Ok(())
    }

    /// Validate a call_chain entry field: reject control/format chars, enforce max length.
    ///
    /// SECURITY (FIND-R51-005): Prevents log injection via control characters
    /// and memory abuse via oversized call chain fields.
    fn validate_call_chain_field(
        value: &str,
        field_name: &str,
        index: usize,
    ) -> Result<(), String> {
        if value.len() > Self::MAX_CALL_CHAIN_FIELD_LEN {
            return Err(format!(
                "EvaluationContext call_chain[{}].{} length {} exceeds max {}",
                index,
                field_name,
                value.len(),
                Self::MAX_CALL_CHAIN_FIELD_LEN,
            ));
        }
        // SECURITY (FIND-R52-005): Also reject Unicode format characters (category Cf)
        // which include zero-width chars (U+200B-U+200F), bidi overrides (U+202A-U+202E,
        // U+2066-U+2069), and BOM (U+FEFF). These can cause identity confusion in logs.
        if value
            .chars()
            .any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
        {
            return Err(format!(
                "EvaluationContext call_chain[{index}].{field_name} contains control or format characters",
            ));
        }
        Ok(())
    }

    /// Maximum byte length for optional identity fields (agent_id, tenant_id, session_state).
    ///
    /// SECURITY: Prevents memory amplification via enormous field values in
    /// deserialized EvaluationContext payloads.
    pub(crate) const MAX_OPTIONAL_ID_FIELD_LEN: usize = 256;

    /// Validate a single optional identity field: if present, must be non-empty,
    /// within length bounds, and must not contain control or Unicode format characters.
    ///
    /// SECURITY (FIND-R52-005): Also reject Unicode format characters (category Cf)
    /// to prevent identity confusion via zero-width chars, bidi overrides, or BOM.
    fn validate_optional_id_field(field: &Option<String>, name: &str) -> Result<(), String> {
        if let Some(value) = field {
            if value.is_empty() {
                return Err(format!("EvaluationContext {name} is present but empty"));
            }
            if value.len() > Self::MAX_OPTIONAL_ID_FIELD_LEN {
                return Err(format!(
                    "EvaluationContext {name} length {} exceeds max {}",
                    value.len(),
                    Self::MAX_OPTIONAL_ID_FIELD_LEN,
                ));
            }
            if value
                .chars()
                .any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
            {
                return Err(format!(
                    "EvaluationContext {name} contains control or format characters"
                ));
            }
        }
        Ok(())
    }
}

/// Builder for constructing [`EvaluationContext`] instances.
///
/// Provides a fluent API for setting context fields, with sensible defaults
/// for fields that aren't explicitly set.
#[derive(Debug, Default)]
pub struct EvaluationContextBuilder {
    timestamp: Option<String>,
    agent_id: Option<String>,
    agent_identity: Option<AgentIdentity>,
    call_counts: HashMap<String, u64>,
    previous_actions: Vec<String>,
    call_chain: Vec<CallChainEntry>,
    tenant_id: Option<String>,
    verification_tier: Option<VerificationTier>,
    capability_token: Option<CapabilityToken>,
    session_state: Option<String>,
}

impl EvaluationContextBuilder {
    /// Set the evaluation timestamp (ISO 8601 format).
    pub fn timestamp(mut self, timestamp: impl Into<String>) -> Self {
        self.timestamp = Some(timestamp.into());
        self
    }

    /// Set the agent ID (legacy identity field).
    pub fn agent_id(mut self, agent_id: impl Into<String>) -> Self {
        self.agent_id = Some(agent_id.into());
        self
    }

    /// Set the cryptographically attested agent identity.
    pub fn agent_identity(mut self, identity: AgentIdentity) -> Self {
        self.agent_identity = Some(identity);
        self
    }

    /// Set the per-tool call counts for the session.
    pub fn call_counts(mut self, counts: HashMap<String, u64>) -> Self {
        self.call_counts = counts;
        self
    }

    /// Add a single tool call count.
    pub fn call_count(mut self, tool: impl Into<String>, count: u64) -> Self {
        self.call_counts.insert(tool.into(), count);
        self
    }

    /// Set the history of previous tool calls.
    pub fn previous_actions(mut self, actions: Vec<String>) -> Self {
        self.previous_actions = actions;
        self
    }

    /// Add a single previous action to the history.
    pub fn previous_action(mut self, action: impl Into<String>) -> Self {
        self.previous_actions.push(action.into());
        self
    }

    /// Set the call chain for multi-agent scenarios.
    pub fn call_chain(mut self, chain: Vec<CallChainEntry>) -> Self {
        self.call_chain = chain;
        self
    }

    /// Add a single entry to the call chain.
    pub fn call_chain_entry(mut self, entry: CallChainEntry) -> Self {
        self.call_chain.push(entry);
        self
    }

    /// Set the tenant ID for multi-tenancy.
    pub fn tenant_id(mut self, tenant_id: impl Into<String>) -> Self {
        self.tenant_id = Some(tenant_id.into());
        self
    }

    /// Set the verification tier for identity verification policies.
    pub fn verification_tier(mut self, tier: VerificationTier) -> Self {
        self.verification_tier = Some(tier);
        self
    }

    /// Set the capability delegation token for capability-based access control.
    pub fn capability_token(mut self, token: CapabilityToken) -> Self {
        self.capability_token = Some(token);
        self
    }

    /// Set the session state from the SessionGuard state machine.
    pub fn session_state(mut self, state: impl Into<String>) -> Self {
        self.session_state = Some(state.into());
        self
    }

    /// Build the [`EvaluationContext`] without validation.
    ///
    /// For trusted internal callers where inputs are already validated.
    /// At trust boundaries (HTTP handlers, deserialization), prefer
    /// [`build_validated`] which rejects invalid identity fields.
    pub fn build(self) -> EvaluationContext {
        EvaluationContext {
            timestamp: self.timestamp,
            agent_id: self.agent_id,
            agent_identity: self.agent_identity,
            call_counts: self.call_counts,
            previous_actions: self.previous_actions,
            call_chain: self.call_chain,
            tenant_id: self.tenant_id,
            verification_tier: self.verification_tier,
            capability_token: self.capability_token,
            session_state: self.session_state,
        }
    }

    /// Build the [`EvaluationContext`] with validation on identity fields.
    ///
    /// SECURITY (FIND-R46-017): Validates that agent_id, tenant_id, and
    /// session_state (if present) are non-empty and free of control characters.
    /// Use this at trust boundaries where inputs come from external sources.
    pub fn build_validated(self) -> Result<EvaluationContext, String> {
        let ctx = self.build();
        ctx.validate()?;
        Ok(ctx)
    }
}

// ═══════════════════════════════════════════════════════════════════
// Phase 25.6: Stateless protocol abstraction
// ═══════════════════════════════════════════════════════════════════

/// Abstraction over session state access for policy evaluation.
///
/// Stateful mode (current behavior): reads from in-memory `SessionStore`.
/// Stateless mode (future): reads from a signed per-request context blob.
///
/// This trait boundary allows security-critical code to remain unchanged
/// while supporting both session-based and stateless HTTP transports.
///
/// # Security
///
/// Implementors must ensure that:
/// - `call_counts()` and `previous_actions()` are tamper-proof
/// - In stateless mode, the context blob is signed and verified
/// - `record_action()` correctly persists state (or returns it in the response)
pub trait RequestContext: Send + Sync {
    /// Per-tool call counts for the current session/request chain.
    fn call_counts(&self) -> &HashMap<String, u64>;

    /// History of tool names called in this session/request chain.
    fn previous_actions(&self) -> &[String];

    /// Multi-agent call chain entries.
    fn call_chain(&self) -> &[CallChainEntry];

    /// Cryptographically attested agent identity (if available).
    fn agent_identity(&self) -> Option<&AgentIdentity>;

    /// Session guard state machine state (if tracked).
    fn session_guard_state(&self) -> Option<&str>;

    /// Current risk score from continuous authorization (if available).
    fn risk_score(&self) -> Option<&crate::abac::RiskScore>;

    /// Build an `EvaluationContext` from this request context.
    ///
    /// Populates all session-derived fields so the policy engine can
    /// evaluate context conditions (call limits, action sequences, etc.).
    fn to_evaluation_context(&self) -> EvaluationContext {
        EvaluationContext {
            call_counts: self.call_counts().clone(),
            previous_actions: self.previous_actions().to_vec(),
            call_chain: self.call_chain().to_vec(),
            agent_identity: self.agent_identity().cloned(),
            session_state: self.session_guard_state().map(|s| s.to_string()),
            ..Default::default()
        }
    }
}

/// Signed context carried per-request in stateless mode.
///
/// In stateless HTTP mode (MCP June 2026+), there is no server-side session.
/// Instead, the proxy issues this blob in responses and the client echoes it
/// back in subsequent requests. The HMAC-SHA256 signature prevents tampering.
///
/// # Security
///
/// - `signature` covers all other fields via HMAC-SHA256 with a server-side key
/// - `issued_at` enables expiry checking (reject stale blobs)
/// - `version` enables forward-compatible format changes
/// - Maximum blob size is bounded to prevent memory abuse
///
/// # Future
///
/// This struct is defined now to lock the wire format. The actual
/// serialization, signing, and verification logic will be implemented
/// when the June 2026 MCP spec is finalized.
#[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct StatelessContextBlob {
    /// Wire format version. Current: 1.
    pub version: u8,
    /// Agent identifier.
    pub agent_id: String,
    /// Per-tool call counts.
    pub call_counts: HashMap<String, u64>,
    /// Last N actions (bounded to prevent unbounded growth).
    pub recent_actions: Vec<String>,
    /// Multi-agent call chain.
    pub call_chain: Vec<CallChainEntry>,
    /// Risk score from continuous authorization.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub risk_score: Option<crate::abac::RiskScore>,
    /// Unix timestamp when this blob was issued.
    pub issued_at: u64,
    /// HMAC-SHA256 signature over the serialized content (hex-encoded).
    pub signature: String,
}

impl fmt::Debug for StatelessContextBlob {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("StatelessContextBlob")
            .field("version", &self.version)
            .field("agent_id", &self.agent_id)
            .field("call_counts", &self.call_counts)
            .field("recent_actions", &self.recent_actions)
            .field("call_chain", &self.call_chain)
            .field("risk_score", &self.risk_score)
            .field("issued_at", &self.issued_at)
            .field("signature", &"[REDACTED]")
            .finish()
    }
}

impl StatelessContextBlob {
    /// Maximum age of a stateless context blob before it's considered expired.
    pub const MAX_AGE_SECS: u64 = 300; // 5 minutes

    /// Maximum number of recent actions stored in the blob.
    pub const MAX_RECENT_ACTIONS: usize = 100;

    /// Maximum number of entries in `call_counts` before validation fails.
    const MAX_BLOB_CALL_COUNTS: usize = 10_000;

    /// Maximum number of entries in `call_chain` before validation fails.
    const MAX_BLOB_CALL_CHAIN: usize = 100;

    /// Check if this blob has expired based on the current time.
    pub fn is_expired(&self, now_unix_secs: u64) -> bool {
        now_unix_secs.saturating_sub(self.issued_at) > Self::MAX_AGE_SECS
    }

    /// Validate the blob's collection sizes and identity fields.
    ///
    /// SECURITY (FIND-R49-002): Prevents memory exhaustion from oversized
    /// deserialized blobs and rejects malformed agent identifiers.
    pub fn validate(&self) -> Result<(), String> {
        if self.agent_id.is_empty() {
            return Err("StatelessContextBlob agent_id is empty".to_string());
        }
        if self.agent_id.len() > 256 {
            return Err(format!(
                "StatelessContextBlob agent_id length {} exceeds max 256",
                self.agent_id.len(),
            ));
        }
        // SECURITY (FIND-R112-001): Reject control/format chars in agent_id to
        // prevent log injection and ForbiddenActionSequence bypass.
        if crate::core::has_dangerous_chars(&self.agent_id) {
            return Err(
                "StatelessContextBlob agent_id contains control or format characters".to_string(),
            );
        }
        if self.call_counts.len() > Self::MAX_BLOB_CALL_COUNTS {
            return Err(format!(
                "StatelessContextBlob call_counts has {} entries, max {}",
                self.call_counts.len(),
                Self::MAX_BLOB_CALL_COUNTS,
            ));
        }
        if self.recent_actions.len() > Self::MAX_RECENT_ACTIONS {
            return Err(format!(
                "StatelessContextBlob recent_actions has {} entries, max {}",
                self.recent_actions.len(),
                Self::MAX_RECENT_ACTIONS,
            ));
        }
        if self.call_chain.len() > Self::MAX_BLOB_CALL_CHAIN {
            return Err(format!(
                "StatelessContextBlob call_chain has {} entries, max {}",
                self.call_chain.len(),
                Self::MAX_BLOB_CALL_CHAIN,
            ));
        }

        // SECURITY (FIND-R52-019): Validate per-entry lengths for call_counts keys
        // and recent_actions entries to prevent memory amplification.
        const MAX_ENTRY_LEN: usize = 256;
        for key in self.call_counts.keys() {
            if key.len() > MAX_ENTRY_LEN {
                return Err(format!(
                    "StatelessContextBlob call_counts key length {} exceeds max {}",
                    key.len(),
                    MAX_ENTRY_LEN,
                ));
            }
            // SECURITY (FIND-R112-001): Reject control/format chars in call_counts keys
            // to prevent ForbiddenActionSequence bypass via homoglyph tool names.
            if key
                .chars()
                .any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
            {
                return Err(
                    "StatelessContextBlob call_counts key contains control or format characters"
                        .to_string(),
                );
            }
        }
        for (i, action) in self.recent_actions.iter().enumerate() {
            if action.len() > MAX_ENTRY_LEN {
                return Err(format!(
                    "StatelessContextBlob recent_actions[{}] length {} exceeds max {}",
                    i,
                    action.len(),
                    MAX_ENTRY_LEN,
                ));
            }
            // SECURITY (FIND-R112-001): Reject control/format chars in recent_actions
            // to prevent ForbiddenActionSequence bypass via invisible characters.
            if action
                .chars()
                .any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
            {
                return Err(format!(
                    "StatelessContextBlob recent_actions[{i}] contains control or format characters"
                ));
            }
        }

        // SECURITY (FIND-R51-007): Validate signature format.
        // HMAC-SHA256 produces 32 bytes = 64 hex characters.
        if self.signature.is_empty() {
            return Err("StatelessContextBlob signature must not be empty".to_string());
        }
        if self.signature.len() != 64 {
            return Err(format!(
                "StatelessContextBlob signature length {} is not 64 (expected HMAC-SHA256 hex)",
                self.signature.len(),
            ));
        }
        // SECURITY (FIND-R52-004): Reject non-lowercase hex to ensure canonical
        // representation for consistent comparison and log analysis.
        if !self
            .signature
            .bytes()
            .all(|b| b.is_ascii_digit() || matches!(b, b'a'..=b'f'))
        {
            return Err(
                "StatelessContextBlob signature must be lowercase hex characters".to_string(),
            );
        }

        Ok(())
    }
}

impl RequestContext for StatelessContextBlob {
    fn call_counts(&self) -> &HashMap<String, u64> {
        &self.call_counts
    }

    fn previous_actions(&self) -> &[String] {
        &self.recent_actions
    }

    fn call_chain(&self) -> &[CallChainEntry] {
        &self.call_chain
    }

    fn agent_identity(&self) -> Option<&AgentIdentity> {
        None // Stateless blobs don't carry full identity (it's in the JWT)
    }

    fn session_guard_state(&self) -> Option<&str> {
        None // Session guard is stateful-only for now
    }

    fn risk_score(&self) -> Option<&crate::abac::RiskScore> {
        self.risk_score.as_ref()
    }
}